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

Hi,

Question about Net::Pcap which may also be a general PERL question.
What I want to do is:
1. Capture a fixed number of ethernet packets
2. If the fixed number of ethernet packets doesn't arrive move on gracefully trapping the fact that they didn't arrive.

Here's my code attempt:
eval { local $SIG{'ALRM'} = sub {Net::pcap::breakloop($$ref2pcap_mas +ter);}; alarm 10; # Capture two messages outgoing and incoming Net::Pcap::loop($$ref2pcap_master, 2, \&do_eth_init_process, +\$init_fail); alarm 0; };
Note: $$ref2pcap_master is my pcap object.

I've managed to get 1) to work i.e. my Net::Pcap callback function is called correctly, I'm happy with this.

My problem is catching the case when no packets arrive. The alarm() function doesn't seem to work for me i.e. I would have thought after 10 seconds if I didn't get two packets that I would have moved on because I call Net::Pcap::breakloop in the alarm signal function.

Apologies for my lack of terminology, i code FPGAs and design PCBs for a living, I am a PERL hacker in the true sense of the word "hack".

Thanks,
umballah.

Replies are listed 'Best First'.
Re: Killing Net::pcap::loop
by Corion (Patriarch) on Nov 09, 2009 at 11:27 UTC

    Net::Pcap points out the pcap_breakloop($pcap) function. Maybe that is of help to you. Alternatively, look at the timeout option of pcap_open.

Re: Killing Net::pcap::loop
by Jenda (Abbot) on Nov 09, 2009 at 11:27 UTC

    I think the whole problem is that you are attempting to call Net::pcap::breakloop() while you should be calling Net::Pcap::breakloop(). Perl is case sensitive!

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Thanks jenda,

      i tried this but no joy. I still hang on the Net::Pcap::loop.

      umballah.
        There was a workaround to this problem for me in that I could use Net::Pcap::dispatch to collect all frames in the buffer (using argument $cnt = -1) and if not what I expected then report an error.

        Thanks.
Re: Killing Net::pcap::loop
by gmargo (Hermit) on Nov 09, 2009 at 19:20 UTC

    I got the alarm to work. I did encounter one problem: after the pcap_breakloop() executed, the pcap_loop() return code would indicate that no packets were captured, even if there were some. I may have an error, or it may be an older pcap library.

    Versions I used:

    • os: Ubuntu 8.04 LTS
    • perl: v5.8.8 built for i486-linux-gnu-thread-multi
    • Net-Pcap: 0.16 (latest & greatest)
    • pcap: 0.9.8 (aka libpcap0.8 under Ubuntu 8.04)

    Test code:

    Sample output (case one - nothing captured with timeout):

    Mon Nov 9 11:09:16 2009: loop 1: listening... Mon Nov 9 11:10:00 2009: capture: got packet 1 Mon Nov 9 11:10:00 2009: capture: got packet 2 Mon Nov 9 11:10:05 2009: capture: got packet 3 Mon Nov 9 11:10:05 2009: capture: got packet 4 Mon Nov 9 11:10:19 2009: capture: got packet 5 Mon Nov 9 11:10:19 2009: capture: got packet 6 Mon Nov 9 11:10:22 2009: capture: got packet 7 Mon Nov 9 11:10:22 2009: capture: got packet 8 Mon Nov 9 11:10:24 2009: capture: got packet 9 Mon Nov 9 11:10:24 2009: capture: got packet 10 Mon Nov 9 11:10:24 2009: loop 1: 10 packets captured Mon Nov 9 11:10:24 2009: loop 2: listening... Mon Nov 9 11:10:34 2009: AlarmHandler: ALRM signal received Mon Nov 9 11:10:34 2009: loop 2: breakloop (nothing captured)

    Sample output (case two - something captured, then timeout):

    Mon Nov 9 11:12:49 2009: loop 1: listening... Mon Nov 9 11:13:12 2009: capture: got packet 1 Mon Nov 9 11:13:12 2009: capture: got packet 2 Mon Nov 9 11:13:17 2009: capture: got packet 3 Mon Nov 9 11:13:17 2009: capture: got packet 4 Mon Nov 9 11:14:15 2009: capture: got packet 5 Mon Nov 9 11:14:15 2009: capture: got packet 6 Mon Nov 9 11:14:20 2009: capture: got packet 7 Mon Nov 9 11:14:20 2009: capture: got packet 8 Mon Nov 9 11:15:20 2009: capture: got packet 9 Mon Nov 9 11:15:20 2009: capture: got packet 10 Mon Nov 9 11:15:20 2009: loop 1: 10 packets captured Mon Nov 9 11:15:20 2009: loop 2: listening... Mon Nov 9 11:15:25 2009: capture: got packet 11 Mon Nov 9 11:15:25 2009: capture: got packet 12 Mon Nov 9 11:15:30 2009: AlarmHandler: ALRM signal received Mon Nov 9 11:15:30 2009: loop 2: breakloop (nothing captured) <== +problem: should say 2 captured