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

Fellow Monks
I have a script that parses a pcap dump file using net::pcap.
while (1) { my $hdr; my $len = read $handle, $hdr, $PKT_HDR_LEN; die "read failed: $!\n" if not defined $len; return undef if $len == 0; $TotalPkts++; my @vals = unpack "LLLL", $hdr; $pkt{secs} = $vals[0]; $pkt{usecs} = $vals[1]; $pkt{stamp} = ($vals[0] * 1.0) + ($vals[1] / 1000000); $pkt{len} = $vals[3]; $len = read $handle, $data, $vals[2]; die "read failed: $!\n" if not defined $len; die "file truncated: $!\n" if $len < $vals[2]; my $ipdata = eth_strip($data); $ip = NetPacket::IP->decode($ipdata); if ($$ip{proto} == 47) { $is_ce = ($$ip{tos} & 3 == 3); $ipdata = NetPacket::IP::strip($ipdata); $ipdata = substr $ipdata, 4; my $gre = NetPacket::IP->decode($ipdata); next if $$gre{src_ip} ne $ip1 && $$gre{src_ip} ne $ip2; $ip = $gre; }
This seems to take quite a long time to complete. I was wondering if anyone else had seen this problem, and found a more efficient solution

Replies are listed 'Best First'.
Re: net::pcap performance
by broomduster (Priest) on Aug 02, 2008 at 00:06 UTC
    It's not easy to start testing the code you show. You're using too many other modules that you are not saying what they are (e.g. NetPacket::IP is obvious, but probably also Net::PcapUtils, ... what else?).

    Some other things it would be nice to know:

    How big is the input file (both number of bytes and number of packets)?

    What was the snapshot length (i.e., the number of bytes captured for each packet)? Was it the default? Was it full packets? Something else?

Re: net::pcap performance
by GrandFather (Saint) on Aug 01, 2008 at 23:57 UTC

    How long is "quite a long time" and how big is the file? Have you profiled the code?


    Perl reduces RSI - it saves typing
Re: net::pcap performance
by Khen1950fx (Canon) on Aug 01, 2008 at 23:50 UTC
    You might try this script (untested): pcap-util
      good script...