Subject: | Doesn't do live capture |
Date: | Fri, 20 Oct 2006 14:50:59 +0100 |
To: | bug-Net-Analysis [...] rt.cpan.org |
From: | David Pashley <david [...] davidpashley.com> |
I've added a patch to provide live capturing of network traffic:
It basically moves all the loop into a new function which gets passed the pcap
object. I then duplicated the loop_file function. It takes a filter
parameter.
--- /home/david/Net-Analysis-0.05/lib/Net/Analysis/EventLoop.pm 2006-09-02 15:53:02.000000000 +0100
+++ /usr/share/perl5/Net/Analysis/EventLoop.pm 2006-10-19 21:20:38.000000000 +0100
@@ -37,14 +37,51 @@
# {{{ loop_file
sub loop_file {
- my ($self) = shift;
- my %h = validate (@_, { filename => { type => SCALAR } });
+ my ($self) = shift;
+ my %h = validate (@_, { filename => { type => SCALAR } });
- my ($np_err);
- my ($pfd) = Net::Pcap::open_offline ($h{filename}, \$np_err);
+ my ($np_err);
+ my ($pfd) = Net::Pcap::open_offline ($h{filename}, \$np_err);
- carp "event_loop('$h{filename}') failed: '$np_err'\n" if (defined $np_err);
+ carp "event_loop('$h{filename}') failed: '$np_err'\n" if (defined $np_err);
+ $self->event_loop($pfd);
+}
+# }}}
+
+# {{{ loop_net
+sub loop_net {
+ my ($self) = shift;
+ my %h = validate (@_, { filter => { type => SCALAR } });
+
+ my $promisc = 0;
+ my $snaplen = 10240;
+ my $to_ms = 0; # timeout
+ my $opt=1;
+ my($err,$net,$mask,$filter_t);
+
+ my ($np_err);
+
+ my $dev = Net::Pcap::lookupdev(\$np_err);
+
+ Net::Pcap::lookupnet($dev, \$net, \$mask, \$np_err);
+
+ my $pfd = Net::Pcap::open_live($dev, $snaplen, $promisc, $to_ms, \$np_err);
+
+ if ( Net::Pcap::compile($pfd, \$filter_t, $h{filter}, $opt, $net) == -1 ) {
+ die "Unable to compile filter string '$h{filter}'\n";
+ }
+
+ Net::Pcap::setfilter($pfd, $filter_t);
+
+ carp "event_loop('$h{filter}') failed: '$np_err'\n" if (defined $np_err);
+ $self->event_loop($pfd);
+}
+# }}}
+# {{{ event_loop
+sub event_loop {
+ my ($self) = shift;
+ my $pfd = shift;
$self->{dispatcher}->emit_event (name => 'setup');
while (1) {
--
David Pashley
david@davidpashley.com
Nihil curo de ista tua stulta superstitione.