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:

Test code:

#!/usr/bin/perl use strict; use warnings; use diagnostics; use Net::Pcap; $| = 1; # autoflush stdout my $dev = "eth1"; my $err = ''; my $pcap = Net::Pcap::open_live($dev, 1500, 1, 0, \$err); die("open_live error upon open of $dev: $err") if !defined $pcap; my $packet_counter = 0; my $maxcap = 10; # First try: just capture a few packets without any timeout: loopy($maxcap, \&capture, \$packet_counter, "1"); # Second try: capture a few packets with timeout inside an eval eval { local $SIG{'ALRM'} = \&AlarmHandler; alarm 10; loopy($maxcap, \&capture, \$packet_counter, "2"); alarm 0; }; sub loopy { my ($cnt, $callback, $udata, $id) = @_; print tim()."loop $id: listening...\n"; my $looprc = Net::Pcap::loop($pcap, $cnt, $callback, $udata); if ($looprc == 0) { print tim()."loop $id: $cnt packets captur +ed\n"; } elsif ($looprc == -1) { print tim()."loop $id: error\n"; } elsif ($looprc == -2) { print tim()."loop $id: breakloop (nothing +captured)\n"; } elsif ($looprc > 0) { print tim()."loop $id: breakloop ($looprc +captured)\n"; } else { print tim()."loop $id: unknown code $loopr +c\n"; } } sub capture { my ($user_data_ref, $header, $packet) = @_; $$user_data_ref++; print tim()."capture: got packet $$user_data_ref\n"; } sub tim { localtime().": "; } sub AlarmHandler { my $signal = shift; print STDERR tim()."AlarmHandler: $signal signal received\n"; Net::Pcap::breakloop($pcap) if defined($pcap); }

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

In reply to Re: Killing Net::pcap::loop by gmargo
in thread Killing Net::pcap::loop by umballah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.