Skip Menu |

This queue is for tickets about the POE-Component-Pcap CPAN distribution.

Report information
The Basics
Id: 1663
Status: resolved
Worked: 15 min
Priority: 0/
Queue: POE-Component-Pcap

People
Owner: Nobody in particular
Requestors: RCAPUTO [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: (no value)
Fixed in: (no value)



Subject: "make test" fails
3) eyrie:~/.cpanplus/build/POE-Component-Pcap-0.03# make test PERL_DL_NONLAZY=1 /usr/bin/perl -Iblib/arch -Iblib/lib \ -I/usr/local/perl-561-single/lib/i386-freebsd \ -I/usr/local/perl-561-single/lib -e \ 'use Test::Harness qw(&runtests $verbose); $verbose=0; \ runtests @ARGV;' t/*.t t/01basic....ok 1/2 ## Waiting for one ARP packet on rl0. . . Can't use an undefined value as a symbol reference at blib/lib/POE/Component/Pcap.pm line 174. t/01basic....dubious Test returned status 22 (wstat 5632, 0x1600) DIED. FAILED test 2 Failed 1/2 tests, 50.00% okay Failed Test Stat Wstat Total Fail Failed List of Failed ------------------------------------------------------------------------------- t/01basic.t 22 5632 2 1 50.00% 2 Failed 1/1 test scripts, 0.00% okay. 1/2 subtests failed, 50.00% okay. *** Error code 2 Stop in /root/.cpanplus/build/POE-Component-Pcap-0.03. ... perl -V output attached. -- Rocco Caputo / troc@pobox.com / poe.perl.org / poe.sf.net
Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration: Platform: osname=freebsd, osvers=4.3-rc, archname=i386-freebsd uname='freebsd eyrie.homenet 4.3-rc freebsd 4.3-rc #0: wed apr 11 01:15:54 edt 2001 troc@eyrie.homenet:usrobjusrsrcsysrc20010411 i386 ' config_args='' hint=recommended, useposix=true, d_sigaction=define usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef Compiler: cc='cc', ccflags ='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include', optimize='-O2 -g', cppflags='-DDEBUGGING -fno-strict-aliasing -I/usr/local/include' ccversion='', gccversion='2.95.3 [FreeBSD] 20010315 (release)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=4, usemymalloc=n, prototype=define Linker and Libraries: ld='cc', ldflags ='-Wl,-E -L/usr/local/lib' libpth=/usr/lib /usr/local/lib libs=-lm -lc -lcrypt -liconv -lutil perllibs=-lm -lc -lcrypt -liconv -lutil libc=, so=so, useshrplib=false, libperl=libperl.a Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' ' cccdlflags='-DPIC -fpic', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: DEBUGGING USE_LARGE_FILES Built under freebsd Compiled at Apr 16 2001 12:18:12 %ENV: PERL5LIB="/home/troc/perl/poe" @INC: /home/troc/perl/poe /usr/local/perl-561-single/lib/i386-freebsd /usr/local/perl-561-single/lib /usr/local/perl-561-single/site-lib/i386-freebsd /usr/local/perl-561-single/site-lib /usr/local/perl-561-single/site-lib .
Ooop, guess I need to poke around PAUSE more frequently. Hrmm, duplicated on my 4.5 FreeBSD box. Poking further. Need to do a release to clean up deprecated _signal states anyhow.
Subject: New version of Pcap.pm
Try the attached version which basically replaces: =pod ## Need an IO::Handle to $kernel->select_read() upon $heap->{fdh} = IO::Handle->new_from_fd( $heap->{fd}, "<+" ); =cut With this using open/gensym instead: $heap->{fdh} = gensym; open( $heap->{fdh}, "<&".$heap->{fd} ) or die "Can't dup handle from pcap fd: $!\n"; For some reason the IO::Handle doesn't want to get created and I wasn't checking for failure there (bad programmer, no skittles!) and that undef was getting passed on into the call to $kernel->select_read() furhter on. Once I clean up the _signal state stuff I'll package up a 0.04 if this works.
#!/usr/bin/perl ## ## $Id: Pcap.pm,v 1.2 2001/03/05 16:25:52 fletch Exp $ ## package POE::Component::Pcap; use strict; use Carp qw( croak carp ); $POE::Component::Pcap::VERSION = q{0.03}; use POE; use Symbol qw( gensym ); use IO::Handle; use Fcntl qw( F_GETFL F_SETFL O_NONBLOCK ); use Net::Pcap; ## ## POE::Component::Pcap->spawn( ## [ Alias => 'pcap' ], ## [ Device => 'eth0' ], ## [ Filter => 'pcap filter' ], ## [ Dispatch => dispatch_state ], ## [ Session => dispatch_session ], ## ) ## sub spawn { my $class = shift; my %args = @_; ## Set default alias if none was given $args{ Alias } ||= 'pcap'; POE::Session->create( inline_states => { _start => \&_start, _stop => \&_stop, # _signal => \&_signal, open_live => \&open_live, set_filter => \&set_filter, set_dispatch => \&set_dispatch, run => \&run, pause => \&pause, _dispatch => \&_dispatch, shutdown => \&shutdown, }, args => [ $args{Alias}, # ARG0 $args{Filter}, # ARG1 $args{Session}, # ARG2 $args{Dispatch},# ARG3 $args{Device}, # ARG4 ], ); return $args{ Alias }; } sub _start { my ($kernel, $heap, $session, $alias, $filter, $target_session, $target_state, $device ) = @_[ KERNEL, HEAP, SESSION, ARG0..ARG4 ]; # print "In state_start for sid ", $session->ID, ", alias $alias\n"; ## Set alias for ourselves and remember it $kernel->alias_set( $alias ); $heap->{Alias} = $alias; ## Set dispatch target session and state if it was given if( defined( $target_state ) ) { $heap->{'target_session'} = $target_session; $heap->{'target_state'} = $target_state; } ## Post an open_live event if device was passed $kernel->post( $session => open_live => $device ) if defined( $device ); ## Set filter if it was given $kernel->post( $session => set_filter => $filter ) if defined( $filter ); # print "Out state_start for sid ", $session->ID, ", alias $alias\n"; } ## ## $kernel->post( pcap => open_live => ## [device], [snaplen], [promisc?], [timeout] ) ## sub open_live { my ( $kernel, $heap, $device, $snaplen, $promisc, $timeout, ) = @_[ KERNEL, HEAP, ARG0..ARG3 ]; my $err; ## Lookup default device if undef was passed unless( $device ) { $device = Net::Pcap::lookupdev( \$err ) or croak "Can't lookupdev: $err\n"; } ## Set `reasonable' defaults for other values $snaplen = 80 unless defined( $snaplen ); $promisc = 1 unless defined( $promisc ); $timeout = 100 unless defined( $timeout ); $heap->{'pcap_t'} = Net::Pcap::open_live( $device, $snaplen, $promisc, $timeout, \$err ) or croak "Can't Net::Pcap::open_live $device: $err\n"; @{$heap}{ qw/device snaplen promisc timeout fd/ } = ( $device, $snaplen, $promisc, $timeout, Net::Pcap::fileno( $heap->{'pcap_t'} ), ); =pod ## Need an IO::Handle to $kernel->select_read() upon $heap->{fdh} = IO::Handle->new_from_fd( $heap->{fd}, "r" ) or die "Can't create IO::Handle from pcap fd: $!\n"; =cut $heap->{fdh} = gensym; open( $heap->{fdh}, "<&".$heap->{fd} ) or die "Can't dup handle from pcap fd: $!\n"; 1; } sub set_filter { my ( $kernel, $heap, $filter ) = @_[ KERNEL, HEAP, ARG0 ]; croak "open must be called before set_filter \n" unless exists $heap->{'pcap_t'}; my( $net, $netmask, $err ); Net::Pcap::lookupnet( $heap->{'device'}, \$net, \$netmask, \$err ); my $filter_t; Net::Pcap::compile( $heap->{'pcap_t'}, \$filter_t, $filter, 1, $netmask ) == 0 or die "Can't compile filter `$filter'\n"; Net::Pcap::setfilter( $heap->{'pcap_t'}, $filter_t ); } ## ## $kernel->post( pcap => set_dispatch => ## 'target_state', 'target_session' ) ## sub set_dispatch { my ( $heap, $sender, $target_state, $target_session ) = @_[ HEAP, SENDER, ARG0, ARG1 ]; ## Target session defaults to the sender $target_session ||= $sender; if( defined( $target_state ) ) { ## Remember whom to forward packets to $heap->{'target_session'} = $target_session; $heap->{'target_state'} = $target_state; } else { ## Clear target delete $heap->{'target_session'}; delete $heap->{'target_state'}; } } sub run { my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; my $flags; ## Can't run unless we've got a pcap_t to work with croak "open must be called before run \n" unless exists $heap->{'pcap_t'}; ## XXX Need to save off flags for OpenBSD if( $^O eq 'openbsd' ) { $flags = fcntl($heap->{fdh}, F_GETFL, 0) or croak "fcntl fails with F_GETFL: $!\n"; } $kernel->select_read( $heap->{fdh} => '_dispatch' ); ## XXX OpenBSD's pcap / bpf devices don't like being set to ## non-blocking for some reason, so restore the saved flags if( $^O eq 'openbsd' ) { $flags = fcntl($heap->{fdh}, F_SETFL, $flags ) or croak "fcntl fails with F_SETFL: $!\n"; } } sub _dispatch { my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; if( exists $heap->{'target_session'} ) { my @pending; ## Get Pcap to pass us any pending packets Net::Pcap::dispatch( $heap->{'pcap_t'}, -1, sub { push @{$_[0]}, [ @_[1,2] ] }, \@pending ); $kernel->post( $heap->{'target_session'}, $heap->{'target_state'}, \@pending, ); } } sub pause { ## Remove read select on pcap handle $_[KERNEL]->select_read( $_[HEAP]->{fdh} ) if exists $_[HEAP]->{fdh}; } sub shutdown { my ( $kernel, $heap ) = @_[ KERNEL, HEAP ]; ## Remove read select on pcap handle $kernel->select_read( $heap->{fdh} ) if exists $heap->{fdh}; ## Get Net::Pcap to shut down pcap_t if( exists $heap->{'pcap_t'} ) { Net::Pcap::close( $heap->{'pcap_t'} ); delete @{$heap}{qw/pcap_t fd fdh/} } $kernel->alias_remove( $heap->{Alias} ); } sub _stop { my ( $kernel, $heap, $session ) = @_[ KERNEL, HEAP, SESSION ]; my $alias = $heap->{Alias}; # print "In state_stop for sid ", $session->ID, ", alias $alias\n"; # print "Out state_stop for sid ", $session->ID, ", alias $alias\n"; } sub _signal { my ( $kernel, $heap, $session ) = @_[ KERNEL, HEAP, SESSION ]; # print "Got signal ", $_[ARG0], "\n"; return 1; } 1; __END__ =head1 NAME POE::Component::Pcap - POE Interface to Net::Pcap =head1 SYNOPSIS use POE::Component::Pcap; POE::Component::Pcap->spawn( Alias => 'pcap', Device => 'eth0', Filter => 'host fooble or host blort', Dispatch => 'got_packet', Session => $my_session_id, ); $poe_kernel->post( pcap => open_live => 'eth0', 80, 1, 100 ); $poe_kernel->post( pcap => set_filter => 'arp or host zooble' ); $poe_kernel->post( pcap => set_dispatch => 'target_state' ); $poe_kernel->post( pcap => 'run' ); $poe_kernel->post( pcap => 'shutdown' ); =head1 DESCRIPTION POE::Component::Pcap provides a wrapper for using the Net::Pcap module from POE programs. The component creates a separate session which posts events to a specified session and state when packets are available. =head2 ARGUMENTS =over 4 =item Alias The alias for the Pcap session. Used to post events such as C<run> and C<shutdown> to control the component. Defaults to C<pcap> if not specified. =item Device As a shortcut, the device for Net::Pcap to watch may be specified when creating the component. If this argument is used, Net::Pcap::open_live will be called with a snaplen of 80 octets, a timeout of 100ms, and the interface will be put in promiscuous mode. If these values are not suitable, post an C<open_live> event instead. =item Filter Another shortcut, calls Net::Pcap::compile and Net::Pcap::setfilter to set a packet filter. This can only be used if the B<Device> argument is also given; otherwise a C<set_filter> event should be posted after an C<open_live> event (since Net::Pcap must have a C<pcap_t> descriptor to work with). =item Dispatch =item Session These specify the session and state to which events should be posted when packets are received. =back =head2 EVENTS The following examples assume that the component's alias has been set to the default value of B<pcap>. =over 4 =item open_live $_[KERNEL]->post( pcap => open_live => 'device', [snaplen], [promsic?], [timeout] ); Calls Net::Pcap::open_live. The device name must be specified. The snaplen, promiscuous, and timeout parameters default to 80, 1, and 100 respectively. This event must be posted (or the B<Device> argument must have been passed to spawn()) before anything else can be done with the component. =item set_filter $_[KERNEL]->post( pcap => set_filter => 'host fooble or host blort' ) Sets the Net::Pcap capture filter. See tcpdump(8) for details on the filter language used by pcap(3). =item set_dispatch $_[KERNEL]->post( pcap => set_dispatch => 'target_state', 'target_session' ); Sets the state and session to which events are sent when packets are recevied. The target session will default to the sender of the event if not specified. The event posted will have a single argument (available as B<ARG0>) which will be an array reference containing the C<$hdr> and C<$pkt> parameters from Net::Pcap. See the Net::Pcap(3) documentation for more details. =item run $_[KERNEL]->post( pcap => 'run' ); Causes the component to register a select_read and start watching for packets. =item shutdown $_[KERNEL]->post( pcap => 'shutdown' ); Shuts the component down. Causes Net::Pcap::close to be called. =back =head1 SEE ALSO Net::Pcap(3), pcap(3), tcpdump(8), POE(3), POE::Component(3) =head1 AUTHOR Mike Fletcher, <fletch@phydeaux.org> =head1 COPYRIGHT Copyright 2000-2001, Mike Fletcher. All Rights Reserved. This is free software; you may redistribute it and/or modify it under the same terms as Perl itself. =cut
Put version 0.04 on CPAN.