Search code examples
pythonsdnryu

check for TCP packet : Ryu - Python


Within a python application for an SDN controller (Ryu), how can I check if a packet in, is a TCP SYN, or SYN-ACK, in order to count these packets?

*if packet in is TCP*
self.total_packets += 1

Solution

  • I don't know this specific library, have you tried looking into the lib.packet.tcp module? I think that the method you need is has_flags().

    From https://github.com/faucetsdn/ryu/blob/master/ryu/lib/packet/tcp.py there is an example:

    >>> pkt = tcp.tcp(bits=(tcp.TCP_SYN | tcp.TCP_ACK))
    >>> pkt.has_flags(tcp.TCP_SYN, tcp.TCP_ACK)
        True
    

    EDIT:

    If a function returns a boolean, it can be used in an if as the condition:

    if pkt.has_flags(tcp.TCP_SYN) or pkt.has_flags(tcp.TCP_SYN, tcp.TCP_ACK):
        self.total_packets += 1
    

    Testing just for the SYN flag should also match both SYN and SYN-ACK packets.

    if pkt.has_flags(tcp.TCP_SYN):
        self.total_packets += 1