lib/Nmap/Scanner/Backend/Processor.pm

filter: Statementstmt 42.3% 25 / 59 Branchbran 16.6% 4 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 69.5% 16 / 23 Podpod 0.0% 0 / 20 totaltotal 34.8% 45 / 129 CC 14 SCAR 16.3
CC
14
Cov
34%
start_nmap
20.0
results
6.0
start_nmap2
6.0
CRAP 5.1
« lib/Nmap/Scanner/PortList.pm ↑ Summary lib/Nmap/Scanner/Backend/XML.pm »
1 package Nmap::Scanner::Backend::Processor;
2  
3 =pod
4  
5 =head1 NAME - Nmap::Scanner::Processor
6  
7 This is the base class for output processors for Nmap::Scanner.
8  
9 =cut
10  
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
16 2 my $class = shift;
17 2 my $you = {};
18 2 return bless $you, $class;
19 }
20  
21 =pod
22  
23 =head1 register_scan_complete_event()
24  
25 Use this to tell the backend processor you want
26 to be notified when the scan of a HOST is
27 complete.
28  
29 Pass in a reference to a function that will
30 receive two arguments when called: A reference
31 to the calling object and a reference to an
32 Nmap::Scanner::Host instance.
33  
34 =cut
35  
36 2 ▶sub register_scan_complete_event {
Subroutine register_scan_complete_event: called CC=1 SCAR=0.0
Pod undocumented
37 2 $_[0]->{'SCAN_COMPLETE_EVENT'} = $_[1];
38 }
39  
40 =pod
41  
42 =head1 register_scan_started_event()
43  
44 Use this to tell the backend processor you want
45 to be notified when the scan of a HOST has
46 started.
47  
48 Pass in a reference to a function that will
49 receive two arguments when called: A reference
50 to the calling object and a reference to an
51 Nmap::Scanner::Host instance.
52  
53 =cut
54  
55 2 ▶sub register_scan_started_event {
Subroutine register_scan_started_event: called CC=1 SCAR=0.0
Pod undocumented
56 2 $_[0]->{'SCAN_STARTED_EVENT'} = $_[1];
57 }
58  
59 =pod
60  
61 =head1 register_host_closed_event()
62  
63 Use this to tell the backend processor you want
64 to be notified when nmap has determined that the
65 current host is not available (up).
66  
67 Pass in a reference to a function that will
68 receive two arguments when called: A reference
69 to the calling object and a reference to an
70 Nmap::Scanner::Host instance.
71  
72 =cut
73  
74 2 ▶sub register_host_closed_event {
Subroutine register_host_closed_event: called CC=1 SCAR=0.0
Pod undocumented
75 2 $_[0]->{'HOST_CLOSED_EVENT'} = $_[1];
76 }
77  
78 =pod
79  
80 =head1 register_port_found_event()
81  
82 Use this to tell the backend processor you want
83 to be notified when an open port has been found
84 on the current host being scanned.
85  
86 Pass in a reference to a function that will
87 receive three arguments when called: A reference
88 to the calling object, a reference to an
89 Nmap::Scanner::Host instance, and a reference to
90 an Nmap::Scanner::Port containing information on
91 the port.
92  
93 =cut
94  
95 2 ▶sub register_port_found_event {
Subroutine register_port_found_event: called CC=1 SCAR=0.0
Pod undocumented
96 2 $_[0]->{'PORT_FOUND_EVENT'} = $_[1];
97 }
98  
99 =pod
100  
101 =head1 register_no_ports_open_event()
102  
103 Use this to tell the backend processor you want
104 to be notified when the scan of a HOST has
105 yielded NO open ports.
106  
107 Pass in a reference to a function that will
108 receive three arguments when called: A reference
109 to the calling object, a reference to an
110 Nmap::Scanner::Host instance, and a reference to
111 an Nmap::Scanner::ExtraPorts instance with some
112 information on the states of the non-open ports.
113  
114 =cut
115  
116 2 ▶sub register_no_ports_open_event {
Subroutine register_no_ports_open_event: called CC=1 SCAR=0.0
Pod undocumented
117 2 $_[0]->{'NO_PORTS_OPEN_EVENT'} = $_[1];
118 }
119  
120 =head1 register_task_started_event()
121  
122 Use this to tell the backend processor you want
123 to be notified when an nmap task has started.
124  
125 Pass in a reference to a function that will
126 receive two arguments when called: A reference
127 to the calling object and a reference to an
128 Nmap::Scanner::Task instance. Note that since
129 this is the begin part of a task end_time() will
130 be undefined.
131  
132 =cut
133  
134 2 ▶sub register_task_started_event {
Subroutine register_task_started_event: called CC=1 SCAR=0.0
Pod undocumented
135 2 $_[0]->{'TASK_STARTED_EVENT'} = $_[1];
136 }
137  
138 =head1 register_task_ended_event()
139  
140 Use this to tell the backend processor you want
141 to be notified when an nmap task has ended.
142  
143 Pass in a reference to a function that will
144 receive two arguments when called: A reference
145 to the calling object and a reference to an
146 Nmap::Scanner::Task instance.
147  
148 =cut
149  
150 2 ▶sub register_task_ended_event {
Subroutine register_task_ended_event: called CC=1 SCAR=0.0
Pod undocumented
151 2 $_[0]->{'TASK_ENDED_EVENT'} = $_[1];
152 }
153  
154 =head1 register_task_progress_event()
155  
156 Use this to tell the backend processor you want
157 to be notified when an nmap task progress event occurs.
158  
159 Pass in a reference to a function that will
160 receive two arguments when called: A reference
161 to the calling object and a reference to an
162 Nmap::Scanner::TaskProgress instance.
163  
164 =cut
165  
166 2 ▶sub register_task_progress_event {
Subroutine register_task_progress_event: called CC=1 SCAR=0.0
Pod undocumented
167 2 $_[0]->{'TASK_PROGRESS_EVENT'} = $_[1];
168 }
169  
170  
171 =pod
172  
173 =head1 results()
174  
175 Return the Nmap::Scanner::Results instance
176 created by the scan.
177  
178 =cut
179  
180 0 ▶sub results {
Subroutine results: not called CC=2 SCAR=17.9
Pod undocumented
181 0 ▶ (defined $_[1]) ? ($_[0]->{RESULTS} = $_[1]) : return $_[0]->{RESULTS};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 0 0 0
182 }
183  
184 2 ▶sub debug {
Subroutine debug: called CC=2 SCAR=7.6
Pod undocumented
185 2 ▶ (defined $_[1]) ? ($_[0]->{DEBUG} = $_[1]) : return $_[0]->{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 2 0 2
186 }
187  
188 =pod
189  
190 =head1 start_nmap()
191  
192 This method may be called by the user. It starts the nmap process using
193 the options set by the user via the scan() method or setters of
194 Nmap::Scanner::Scanner. The method returns the PID of the child
195 nmap process, a reader handle to read from the nmap process,
196 a write handle to write to nmap, and an error handle which will
197 contain data if nmap throws an error.
198  
199 Example code:
200  
201 sub process {
202  
203 my $self = shift;
204 my $cmdline = shift;
205  
206 my ($pid, $in, $out, $err) = $self->SUPER::start_nmap($cmdline);
207  
208 # Process filehandles
209  
210 }
211  
212 =cut
213  
214 0 ▶sub start_nmap {
Subroutine start_nmap: not called CC=4 SCAR=30.0
Pod undocumented
215  
216 0 my $self = shift;
217 0 my $cmdline = shift;
218  
219 0 my ($readfh, $writefh, $errorfh) =
220 (FileHandle->new(), FileHandle->new(), FileHandle->new());
221  
222 0 my $pid = 0;
223  
224 0 ▶ if (-f $cmdline) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cmdline) { } 0 0 0
225 0 ▶ open($readfh, "+< $cmdline") ||
Branch: 1 branch
BranchTrueFalseTotal
open $readfh, "+< $cmdline" or die "Can't read from input file ${cmdline}: $!\n" 0 0 0
226 die "Can't read from input file $cmdline: $!\n";
227 0 $writefh = $readfh;
228 0 my $error = "";
229 0 open($errorfh, '<', \$error);
230 } else {
231 0 ▶ $pid = open3($writefh, $readfh, $errorfh, $cmdline) ||
Condition0%0/2
open3($writefh, $readfh, $errorfh, $cmdline) || die("Can't open pipe to ${cmdline}: $!\n")
l!l
00
MC/DC0%0/1
open3($writefh, $readfh, $errorfh, $cmdline) || die("Can't open pipe to ${cmdline}: $!\n")
open3($writefh, $readfh, $errorfh, $cmdline)
Truth table0%0/2
A
A= open3($writefh, $readfh, $errorfh, $cmdline)
Aresult
11
00
232 die "Can't open pipe to $cmdline: $!\n";
233 0 $readfh->flush();
234 0 $errorfh->flush();
235 }
236  
237 0 return ($pid, $readfh, $writefh, $errorfh);
238
239 }
240  
241 =head1 start_nmap2()
242  
243 This method is called by the sub-classed processor to start the nmap
244 process using options set by the user via the scan() method or
245 setters of Nmap::Scanner::Scanner. The sub-classed processor is
246 returned the PID of the child nmap process and a reader handle to read
247 from the nmap process.
248  
249 Example code:
250  
251 sub process {
252  
253 my $self = shift;
254 my $cmdline = shift;
255  
256 my ($pid, $in) = $self->SUPER::start_nmap2($cmdline);
257  
258 # Process filehandles
259  
260 }
261  
262 =cut
263 0 ▶sub start_nmap2{
Subroutine start_nmap2: not called CC=2 SCAR=17.9
Pod undocumented
264  
265 0 my $self = shift;
266 0 my $cmdline = shift;
267  
268 0 local(*READ);
269  
270 0 my $pid = open(\*READ, "-|", "$cmdline 2>&1");
271 0 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
272 0 die "Can't open pipe to nmap: $!\n";
273 }
274  
275 0 my $read = *READ;
276 0 $read->flush();
277  
278
279 0 return ($pid, $read);
280
281 }
282  
283 =pod
284  
285 =head1 notify_scan_started()
286  
287 Notify the listener that a scan started
288 event has occurred. Caller is passed a
289 reference to the callers self reference
290 (object instance) and an Nmap::Scanner::Host
291 instance.
292  
293 =cut
294  
295 2 ▶sub notify_scan_started {
Subroutine notify_scan_started: called CC=2 SCAR=9.2
Pod undocumented
296  
297 0 &{$_[0]->{'SCAN_STARTED_EVENT'}->[1]}(
298 $_[0]->{'SCAN_STARTED_EVENT'}->[0], $_[1]
299 2 ▶ ) if defined $_[0]->{'SCAN_STARTED_EVENT'}->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"SCAN_STARTED_EVENT"}[1] 0 2 2
300  
301 }
302  
303 =pod
304  
305 =head1 notify_scan_complete()
306  
307 Notify the listener that a scan complete
308 event has occurred. Caller is passed a
309 reference to the callers self reference
310 (object instance) and an Nmap::Scanner::Host
311 instance.
312  
313 =cut
314  
315 2 ▶sub notify_scan_complete {
Subroutine notify_scan_complete: called CC=2 SCAR=9.2
Pod undocumented
316 0 &{$_[0]->{'SCAN_COMPLETE_EVENT'}->[1]}(
317 $_[0]->{'SCAN_COMPLETE_EVENT'}->[0], $_[1]
318 2 ▶ ) if (defined $_[0]->{'SCAN_COMPLETE_EVENT'}->[1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"SCAN_COMPLETE_EVENT"}[1] 0 2 2
319 }
320  
321 =pod
322  
323 =head1 notify_scan_started()
324  
325 Notify the listener that a port found
326 event has occurred. Caller is passed a
327 reference to the callers self reference
328 (object instance), an Nmap::Scanner::Host
329 instance, and an Nmap::Scanner::Port
330 instance.
331  
332 =cut
333  
334 12 ▶sub notify_port_found {
Subroutine notify_port_found: called CC=2 SCAR=9.2
Pod undocumented
335 0 &{$_[0]->{'PORT_FOUND_EVENT'}->[1]}(
336 $_[0]->{'PORT_FOUND_EVENT'}->[0], $_[1], $_[2]
337 12 ▶ ) if (defined $_[0]->{'PORT_FOUND_EVENT'}->[1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"PORT_FOUND_EVENT"}[1] 0 12 12
338 }
339  
340 =pod
341  
342 =head1 notify_no_ports_open()
343  
344 Notify the listener that a scan started
345 event has occurred. Caller is passed a
346 reference to the callers self reference
347 (object instance), an Nmap::Scanner::Host
348 instance, and an Nmap::Scanner::ExtraPorts
349 instance.
350  
351 =cut
352  
353 0 ▶sub notify_no_ports_open {
Subroutine notify_no_ports_open: not called CC=2 SCAR=17.9
Pod undocumented
354 0 &{$_[0]->{'NO_PORTS_OPEN_EVENT'}->[1]}(
355 $_[0]->{'NO_PORTS_OPEN_EVENT'}->[0], $_[1], $_[2]
356 0 ▶ ) if (defined $_[0]->{'NO_PORTS_OPEN_EVENT'}->[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"NO_PORTS_OPEN_EVENT"}[0] 0 0 0
357 }
358  
359 =head1 notify_task_started()
360  
361 Notify the listener that an nmap task begin (taskbegin)
362 event has occurred. Caller is passed a
363 reference to the callers self reference
364 (object instance) and an Nmap::Scanner::Task
365 instance.
366  
367 =cut
368  
369 0 ▶sub notify_task_started {
Subroutine notify_task_started: not called CC=2 SCAR=17.9
Pod undocumented
370  
371 0 &{$_[0]->{'TASK_STARTED_EVENT'}->[1]}(
372 $_[0]->{'TASK_STARTED_EVENT'}->[0], $_[1]
373 0 ▶ ) if defined $_[0]->{'TASK_STARTED_EVENT'}->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"TASK_STARTED_EVENT"}[1] 0 0 0
374  
375 }
376  
377 =head1 notify_task_ended()
378  
379 Notify the listener that an nmap task end (taskend)
380 event has occurred. Caller is passed a
381 reference to the callers self reference
382 (object instance) and an Nmap::Scanner::Task
383 instance.
384  
385 =cut
386  
387 0 ▶sub notify_task_ended {
Subroutine notify_task_ended: not called CC=2 SCAR=17.9
Pod undocumented
388  
389 0 &{$_[0]->{'TASK_ENDED_EVENT'}->[1]}(
390 $_[0]->{'TASK_ENDED_EVENT'}->[0], $_[1]
391 0 ▶ ) if defined $_[0]->{'TASK_ENDED_EVENT'}->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"TASK_ENDED_EVENT"}[1] 0 0 0
392  
393 }
394  
395 =head1 notify_task_progress()
396  
397 Notify the listener that an nmap task end (taskend)
398 event has occurred. Caller is passed a
399 reference to the callers self reference
400 (object instance) and an Nmap::Scanner::TaskProgress
401 instance.
402  
403 =cut
404  
405 0 ▶sub notify_task_progress {
Subroutine notify_task_progress: not called CC=2 SCAR=17.9
Pod undocumented
406  
407 0 &{$_[0]->{'TASK_PROGRESS_EVENT'}->[1]}(
408 $_[0]->{'TASK_PROGRESS_EVENT'}->[0], $_[1]
409 0 ▶ ) if defined $_[0]->{'TASK_PROGRESS_EVENT'}->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"TASK_PROGRESS_EVENT"}[1] 0 0 0
410  
411 }
412 3 1;
« lib/Nmap/Scanner/PortList.pm ↑ Summary lib/Nmap/Scanner/Backend/XML.pm »