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

I feel like I'm doing something stupid.
my $pcap_device='eth0'; my $pcap = Net::Pcap::Easy-> new( dev => $pcap_device, filter => 'port 6653', packets_per_loop => 1, timeout_in_ms => 1, promiscuous => 0, tcp_callback => sub { $tcp_total++; print "tcp seen\n"; }, udp_callback => sub { $udp_total++; print "udp seen\n"; }, ); while ( 1 ) { $pcap->loop; print "oink\n"; }
The filter was specifically written as to NOT capture stuff... In other words: I'm trying to write a bit of code that forces a timeout. A timeout never happens. If I put a filter in there that actually matches stuff, I can get Net::Pcap::Easy to work just fine.

Replies are listed 'Best First'.
Re: no timeout in Net::Pcap::Easy
by VinsWorldcom (Prior) on Mar 27, 2014 at 17:24 UTC

    Check the man page for 'pcap'. It specifies that the 'timeout' should not be relied upon as it is not supported on all platforms.

    To get around this and capture with a timeout, I've done the following with some success:

    use Net::Pcap; my $device = ... my $snaplen = ... my $timeout = ... * 1000 # in milliseconds * 1000 = seconds my $err; my $pcaprecv = Net::Pcap::pcap_open_live($device, $snaplen, 1, $timeou +t, \$err); my $recv_r; my %header; my $starttime = time; while (1) { my $ret = Net::Pcap::pcap_next_ex($pcaprecv, \%header, \$recv_r); if ($ret == 1) { # Found packet - do stuff } elsif ($ret == 0) { print "[Receive Timeout] "; last } else { print "[Receive Error] "; last } if ((time - $starttime) >= $timeout) { print "[Receive Timeout] "; last } }