Subject: | Timeout doesn't seem to work |
If I run the attached perl code, I get inconsistent looping behavior. I've set the timeout to 1000 msec, should should be 1 second. But, I'm finding that, the timeout doesn't return (all examples end with my hitting ctrl+c):
scott@newluke [~/tmp]# time sudo perl show-pcap.pl
Tue Nov 11 08:46:13 2014.13363 entering loop
real 0m33.055s
user 0m0.030s
sys 0m0.013s
scott@newluke [~/tmp]#
Now, if I send it a single packet, the loop returns, as expected, but it doesn't start respecting timeouts:
scott@newluke [~/tmp]# time sudo perl show-pcap.pl
Tue Nov 11 08:47:12 2014.819306 entering loop
Tue Nov 11 08:47:16 2014.814889 Got a packet
Tue Nov 11 08:47:16 2014.814934 Looping
Tue Nov 11 08:47:16 2014.815024 Got a packet
Tue Nov 11 08:47:16 2014.815049 Looping
real 0m15.034s
user 0m0.029s
sys 0m0.011s
scott@newluke [~/tmp]#
The behavior I'm observing here is consistent with the pcap man page that suggests that timeout doesn't work consistently across various operating systems, and it seems like it's something that libpcap doesn't consider a bug:
https://github.com/the-tcpdump-group/libpcap/issues/86
This sort of behavior makes it challenging to build a single threaded, interactive perl script using Net::Pcap::Easy.
Is this a known shortcoming on Net::Pcap::Easy, a bug, or am I approaching this wrong?
Subject: | show-pcap.pl |
#!/usr/local/bin/perl
use strict;
use warnings;
use Net::Pcap::Easy;
use Time::HiRes;
my $npe = Net::Pcap::Easy->new( 'dev' => 'eth0',
'filter' => 'host 198.176.29.35 and icmp',
'packets_per_loop' => 1,
'timeout_in_ms' => 1000, #1 second
'promiscuous' => 0,
'icmp_callback' => \&got_a_packet );
print timestamp()." entering loop\n";
while( $npe->loop ) {
print timestamp()." Looping\n";
}
sub got_a_packet {
print timestamp()." Got a packet\n";
}
sub timestamp {
my ( $sec, $usec ) = Time::HiRes::gettimeofday();
return localtime().".$usec";
}