Client/Tcpdump.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Client/Tcp.pm ↑ Summary Client/Telnet.pm »
1 #
2 # $Id$
3 #
4 # client::tcpdump Brik
5 #
6 package Metabrik::Client::Tcpdump;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Network::Read);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 output => [ qw(output) ],
21 device => [ qw(device) ], # Inherited
22 layer => [ qw(2|3|4) ], # Inherited
23 filter => [ qw(pcap_filter) ], # Inherited
24 count => [ qw(count) ], # Inherited
25 _sp => [ qw(INTERNAL) ],
26 _pidfile => [ qw(INTERNAL) ],
27 },
28 commands => {
29 capture => [ qw(output layer|OPTIONAL device|OPTIONAL filter|OPTIONAL count|OPTIONAL) ],
30 capture_in_background => [ qw(output layer|OPTIONAL device|OPTIONAL filter|OPTIONAL count|OPTIONAL) ],
31 stop => [ ],
32 },
33 require_modules => {
34 'Metabrik::File::Pcap' => [ ],
35 'Metabrik::System::File' => [ ],
36 'Metabrik::System::Process' => [ ],
37 },
38 };
39 }
40  
41 sub brik_use_properties {
42 my $self = shift;
43  
44 return {
45 attributes_default => {
46 device => defined($self->global) && $self->global->device || 'eth0',
47 },
48 };
49 }
50  
51 sub capture {
52 my $self = shift;
53 my ($output, $layer, $device, $filter, $count) = @_;
54  
55 $layer ||= $self->layer;
56 $device ||= $self->device;
57 $filter ||= $self->filter;
58 $count ||= $self->count;
59 $self->brik_help_run_undef_arg('capture', $output) or return;
60  
61 my $fp = Metabrik::File::Pcap->new_from_brik_init($self) or return;
62 $fp->open($output, 'write') or return;
63  
64 $self->open($layer, $device, $filter) or return;
65  
66 my $read_count = 0;
67 while (1) {
68 if (my $next = $self->read or next) {
69 if (@$next > 0) {
70 $read_count += @$next;
71 $fp->write($next);
72 }
73 }
74 $self->log->debug("capture: read returned");
75  
76 # We need to reset the timeout, otherwise read() will
77 # always return immediately after each call, causing a full CPU
78 # to become busy. Yes, read() is blocking until a timeout occurs.
79 if ($self->has_timeout) {
80 $self->reset_timeout;
81 }
82  
83 last if $count && $read_count >= $count;
84 }
85  
86 $self->close;
87  
88 $fp->close;
89  
90 return $read_count;
91 }
92  
93 sub capture_in_background {
94 my $self = shift;
95 my ($output, $layer, $device, $filter, $count) = @_;
96  
97 $layer ||= $self->layer;
98 $device ||= $self->device;
99 $filter ||= $self->filter;
100 $count ||= $self->count;
101 $self->brik_help_run_undef_arg('capture_in_background', $output) or return;
102  
103 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
104  
105 my $datadir = $self->datadir;
106 if ($sf->is_relative($output)) {
107 my $basefile = $sf->basefile($output) or return;
108 $output = $datadir.'/'.$basefile;
109 }
110  
111 $self->log->info("capture_in_background: writing to output [$output]");
112  
113 my $sp = Metabrik::System::Process->new_from_brik_init($self) or return;
114 $sp->close_output_on_start(0);
115  
116 my $pidfile = $sp->start_with_pidfile(sub {
117 $self->capture($output, $layer, $device, $filter, $count)
118 });
119  
120 $self->_sp($sp);
121 $self->_pidfile($pidfile);
122  
123 return $pidfile;
124 }
125  
126 sub stop {
127 my $self = shift;
128  
129 my $sp = $self->_sp;
130 my $pidfile = $self->_pidfile;
131  
132 if (defined($sp)) {
133 $sp->kill_from_pidfile($pidfile);
134 $self->_sp(undef);
135 $self->_pidfile(undef);
136 }
137  
138 return 1;
139 }
140  
141 1;
142  
143 __END__
« Client/Tcp.pm ↑ Summary Client/Telnet.pm »