swaroop has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to decode the network packets using NetPacket::TCP module.How to get the TCP states. Please suggest.

Thanks.

Replies are listed 'Best First'.
Re: TCP States ( NetPacket::TCP module )
by tirwhan (Abbot) on Dec 19, 2005 at 07:09 UTC

    The $tcp_obj->{flags} value will give you the setting of the TCP flags. The state of a TCP connection cannot be determined reliably from a single packet though, you have to track the session to find out in which state it currently is.


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
      Thanks for the reply. I have tried to print the value of $tcp_obj->{falgs}. Its giving me 0. Do you know any procedure to parse the flags?

        You can binary AND them with the constants provided by the module to test for a single flag. The following will give you a comma-separated list of flags for a single packet.

        my %tcp_flags=(FIN => FIN, SYN => SYN, RST => RST, PSH => PSH, ACK => ACK, URG => URG, ECE => ECE, CWR => CWR); my $cur_flags=$tcp_obj->{flags}; my @set_flags = grep { $cur_flags & $tcp_flags{$_} } keys %tcp_flags; print join(",",@set_flags)."\n";

        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
        If you really wrote $tcp_obj->{falgs}, an error is normal, because you should write $tcp_obj->{flags}.
        The value you intend to print is as in the packet, in binary form, so you must use binary operators to scan each flag. The example given in the documentation reads :
        # set the syn flag $tcp_obj->{flags} |= SYN;
        NetPacket::TCP exports the different flags, so you can use them as above.
        You should consider reading about binary operators.

        Hope this helps.

        Gu