blib/lib/Netflow/Parser.pm

filter: Statementstmt 88.5% 154 / 174 Branchbran 48.3% 29 / 60 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.8% 24 / 27 Podpod 100% 4 / 4 totaltotal 77.4% 213 / 275 CC 37 SCAR 22.4
CC
37
Cov
77%
_parse_v9
31.2
_parse_template_v9
10.0
_parse_flowset_v9
6.3
CRAP 9.4
↑ Summary
1 package Netflow::Parser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use fields qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
7 templates
8 flow_cb
9 verbose
10 3 /;
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Netflow::Parser - NetFlow datagram parser
16  
17 =head1 DESCRIPTION
18  
19 Netflow Parser supports currently L<NetFlow|https://en.wikipedia.org/wiki/NetFlow> V9 only
20  
21 =head1 VERSION
22  
23 Version 0.06.002
24  
25 =cut
26  
27 3 $Netflow::Parser::VERSION = '0.06.002';
28  
29 =head1 SYNOPSIS
30  
31 use Netflow::Parser;
32  
33 my $nfp = Netflow::Parser->new(
34 flow_cb => sub {my ($flow_hr) = @_; ...},
35 templates_data => pack('H*', '01020002011b000400e60001')
36 );
37  
38 while(my $packet = take_packet_from_socket()) {
39 my $pp = $nfp->parse($packet);
40  
41 # version, count, sysuptime, unix_secs, seqno and source_id
42 $pp->header;
43  
44 # parsed flowsets
45 $pp->parsed;
46  
47 # unparsed flowsets
48 $pp->unparsed && persist_for_later($pp->unparsed);
49 }
50  
51 # persist templates if you want
52 my @templates = $nfp->templates;
53 foreach (@templates) {
54 my ($id, $content) = each(%{$_});
55 }
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 new(%opts)
60  
61 options:
62  
63 =over
64  
65 =item
66  
67 C<templates_data>
68  
69 [raw template piece]
70  
71 =item
72  
73 C<flow_cb>
74  
75 callback method will be applied to each parsed flow
76  
77 =item
78  
79 C<verbose>
80  
81 =back
82  
83 =cut
84  
85 1 ▶sub new {
Subroutine new: called CC=5 SCAR=16.3
Pod documented
86 1 my Netflow::Parser $self = shift;
87 1 my (%opts) = @_;
88 1 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 0 1
89 1 $self = fields::new($self);
90 }
91  
92 1 $self->{'verbose'} = delete $opts{'verbose'};
93 1 ▶ if ($opts{'flow_cb'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"flow_cb"}) 1 0 1
94 1 $self->{'flow_cb'} = delete $opts{'flow_cb'};
95 }
96  
97 1 my $templates = delete $opts{'templates_data'};
98 1 foreach (@{$templates}) {
99 1 $self->_parse_template_v9($_);
100 }
101  
102 %opts
103 1 ▶ && warn(sprintf "unsupported parameter(s) '%s'", join ', ', keys %opts);
Branch: 1 branch
BranchTrueFalseTotal
%opts and warn sprintf(q[unsupported parameter(s) '%s'], join(", ", keys %opts)) 0 1 1
104  
105 1 return $self;
106 } ## end sub new
107  
108 =head2 parse($packet)
109  
110 currently only NetFlow V9 supported
111  
112 unpack packet, try to parse flowsets content.
113  
114 return {
115 'header' => {
116 'count',
117 'seqno',
118 'source_id',
119 'sysuptime',
120 'unix_secs',
121 'version' => 9
122 },
123 'flows' => [flow_cb result],
124 'flowsets' => ?, # flowsets number
125 'templates' => [], # templates contains in the packet
126 'unparsed_flowsets' => [] # no template
127 }
128  
129  
130 =cut
131  
132 1 ▶sub parse {
Subroutine parse: called CC=1 SCAR=0.0
Pod documented
133 1 my ($self, $packet) = @_;
134  
135 #my ($version) = unpack("n", $packet);
136 1 return $self->_parse_v9($packet);
137 } ## end sub parse
138  
139 =head2 templates()
140  
141 return [ { template_id => content} ]
142  
143 =cut
144  
145 1 ▶sub templates {
Subroutine templates: called CC=2 SCAR=6.9
Pod documented
146 1 my ($self) = @_;
147 1 my @templates = ();
148 1 foreach my $id (keys %{ $self->{'templates'} }) {
149 1 push @templates, { $id => $self->template($id) };
150 }
151 1 return @templates;
152 } ## end sub templates
153  
154 =head2 template($template_id)
155  
156 return hex dump of template for given $template_id
157  
158 =cut
159  
160 2 ▶sub template {
Subroutine template: called CC=3 SCAR=13.5
Pod documented
161 2 my ($self, $template_id) = @_;
162 2 ▶ unless ($self->{'templates'}->{$template_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"templates"}{$template_id}) 0 2 2
163 0 ▶ $self->{'verbose'} && $self->_debug("no template $template_id");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and $self->_debug("no template $template_id") 0 0 0
164 0 return;
165 }
166  
167 return pack('n*',
168 $template_id,
169 2 scalar(@{ $self->{'templates'}->{$template_id}->{'content'} }) / 2,
170 2 @{ $self->{'templates'}->{$template_id}->{'content'} });
171 } ## end sub template
172  
173 #=head2 _parse_v9($packet)
174 #
175 #parse a C<$packet> and return content of them
176 #
177 #return {
178 # 'header' => {
179 # 'version' => $version,
180 # 'count' => $count,
181 # 'sysuptime' => $sysuptime,
182 # 'unix_secs' => $unix_secs,
183 # 'seqno' => $seqno,
184 # 'source_id' => $source_id,
185 # },
186 # 'templates' => [parsed templates],
187 # 'flows' => [parsed flows],
188 # 'unparsed_flowsets' => [flowset couldn't be parsed],
189 # 'flowsets' => scalar(@flowsets),
190 # }
191 #
192 #=cut
193  
194 1 ▶sub _parse_v9 {
Subroutine _parse_v9: called CC=12 SCAR=34.4
195 1 my ($self, $packet) = @_;
196 my (
197 1 $version, $count, $sysuptime, $unix_secs,
198 $seqno, $source_id, @flowsets
199 ) = unpack("nnNNNN(nnX4/a)*", $packet);
200  
201 1 ▶ eval { $version == 9 } || Carp::croak("the version of packet is not v9");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $version == 9 } } or Carp::croak("the version of packet is not v9") 1 0 1
202  
203 1 my $pp = Netflow::Parser::Packet->new(
204 'flowsets' => scalar(@flowsets),
205 'header' => {
206 'version' => $version,
207 'count' => $count,
208 'sysuptime' => $sysuptime,
209 'unix_secs' => $unix_secs,
210 'seqno' => $seqno,
211 'source_id' => $source_id,
212 }
213 );
214  
215 1 ▶ scalar(@flowsets) > length($packet)
Branch: 1 branch
BranchTrueFalseTotal
scalar @flowsets > length $packet and warn sprintf("estimated %d flowsets > paket length %d", scalar @flowsets, length $packet) 0 1 1
216 && warn sprintf("estimated %d flowsets > paket length %d",
217 scalar(@flowsets), length($packet));
218 1 ▶ for (my $i = 0; $i < scalar(@flowsets); $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @flowsets) 1 1 2
219 1 my $flowset_id = $flowsets[$i];
220  
221 # chop off id/length
222 1 my $flowset = substr($flowsets[$i + 1], 4);
223 1 ▶ if ($flowset_id == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flowset_id == 0) { } 0 1 1
elsif ($flowset_id == 1) { } 0 1 1
elsif ($flowset_id > 255) { } 1 0 1
224 0 ▶ if ($flowset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flowset) 0 0 0
225 0 my @tmpl = $self->_parse_template_v9($flowset);
226 0 ▶ if (@tmpl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmpl) { } 0 0 0
227 0 $pp->add_template(@tmpl);
228 }
229 else {
230 0 $pp->add_unparsed({ $flowset_id => $flowset });
231 }
232 } ## end if ($flowset)
233 } ## end if ($flowset_id == 0)
234 elsif ($flowset_id == 1) {
235  
236 # 1 - Options Template FlowSet
237 0 ▶ $self->{'verbose'} && $self->_debug("do nothing for flowset id 1");
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and $self->_debug("do nothing for flowset id 1") 0 0 0
238  
239 0 $pp->add_unparsed({ $flowset_id => $flowset });
240 } ## end elsif ($flowset_id == 1)
241 elsif ($flowset_id > 255) {
242 1 my @flows = $self->_parse_flowset_v9($flowset_id, $flowset);
243 1 ▶ if (scalar(@flows)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @flows) { } 1 0 1
244 1 $pp->add_parsed({ $flowset_id => [@flows] });
245 }
246 else {
247 0 $pp->add_unparsed({ $flowset_id => $flowset });
248 }
249 } ## end elsif ($flowset_id > 255)
250 else {
251 # reserved FlowSet
252 0 ▶ $self->{'verbose'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and $self->_debug("Unknown FlowSet ID $flowset_id found") 0 0 0
253 && $self->_debug("Unknown FlowSet ID $flowset_id found");
254 }
255 } ## end for (my $i = 0; $i < scalar...)
256  
257 1 return $pp;
258 } ## end sub _parse_v9
259  
260 #=head2 _parse_flowset_v9 ($flowset_id, $flowset)
261 #
262 #parse flowset if defined instance template for $flowset_id
263 #
264 #apply C<flow_cb> to each flow
265 #
266 #=over
267 #
268 #=item C<$flowset_id>
269 #
270 #is a template id number
271 #
272 #=item C<$flowset>
273 #
274 #flowset data
275 #
276 #=back
277 #
278 #return [{flow}]
279 #
280 #=cut
281  
282 1 ▶sub _parse_flowset_v9 {
Subroutine _parse_flowset_v9: called CC=6 SCAR=18.4
283 1 my ($self, $flowset_id, $flowset) = @_;
284 1 ▶ if (!defined($self->{'templates'}->{$flowset_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"templates"}{$flowset_id}) 0 1 1
285 0 ▶ $self->{'verbose'}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"verbose"} and $self->_debug("unknown template id $flowset_id") 0 0 0
286 && $self->_debug("unknown template id $flowset_id");
287 0 return;
288 }
289  
290 my ($tmpl_length, @template) = (
291 $self->{'templates'}->{$flowset_id}->{'length'},
292 1 @{ $self->{'templates'}->{$flowset_id}->{'content'} }
293 );
294  
295 1 my $cb = $self->{flow_cb};
296 1 my ($datalen, $ofs) = (length($flowset), 0);
297  
298 1 my @flows = ();
299 1 ▶ while (($ofs + $tmpl_length) <= $datalen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ofs + $tmpl_length <= $datalen) 27 1 28
300 27 my $flow = {};
301 27 ▶ for (my $i = 0; $i < scalar @template; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @template) 324 27 351
302 324 my $fld_type = $template[$i];
303 324 my $fld_len = $template[$i + 1];
304 324 my $fld_val = substr($flowset, $ofs, $fld_len);
305 324 $ofs += $fld_len;
306  
307 324 $flow->{$fld_type} = $fld_val;
308 } ## end for (my $i = 0; $i < scalar...)
309  
310 27 ▶ $cb && $cb->($flow);
Branch: 1 branch
BranchTrueFalseTotal
$cb and &$cb($flow) 27 0 27
311  
312 27 push @flows, $flow;
313 } ## end while (($ofs + $tmpl_length...))
314  
315 1 return @flows;
316 } ## end sub _parse_flowset_v9
317  
318 #=head2 _parse_template_v9($flowset)
319 #
320 #parse $flowset data, update instance templates
321 #
322 #return ({template_id => [template content]})
323 #
324 #=cut
325  
326 1 ▶sub _parse_template_v9 {
Subroutine _parse_template_v9: called CC=9 SCAR=23.0
327 1 my ($self, $flowset) = @_;
328 1 my @template_ints = unpack("n*", $flowset);
329 1 my ($i, $count, @tmpl) = (0, scalar(@template_ints));
330 1 ▶ while ($i < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $count) 1 1 2
331 1 my $template_id = $template_ints[$i];
332 1 my $fld_count = $template_ints[$i + 1];
333 1 ▶ last if (!defined($template_id) || !defined($fld_count));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template_id and defined $fld_count 0 1 1
Condition33%1/3
defined $template_id and defined $fld_count
!ll&&!rl&&r
001
MC/DC0%0/2
defined $template_id and defined $fld_count
defined $template_id defined $fld_count
Truth table33%1/3
A and B
A= defined $template_idB= defined $fld_count
ABresult
0X0
100
111
334  
335 #TODO $template_id < 255 || $template_id > 300; is 300 enough?
336 1 ▶ ($template_id < 255 || $template_id > 300)
Branch: 1 branch
BranchTrueFalseTotal
$template_id < 255 || $template_id > 300 and Carp::croak("wrong template id: $template_id") 0 1 1
Condition33%1/3
$template_id < 255 or $template_id > 300
l!l&&r!l&&!r
001
MC/DC0%0/2
$template_id < 255 or $template_id > 300
$template_id < 255 $template_id > 300
Truth table33%1/3
A or B
A= $template_id < 255B= $template_id > 300
ABresult
1X1
011
000
337 && Carp::croak("wrong template id: $template_id");
338  
339 1 my $content
340 = [@template_ints[($i + 2) .. ($i + 2 + $fld_count * 2 - 1)]];
341 1 my $totallen = 0;
342 1 ▶ for (my $j = 1; $j < scalar @$content; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @$content) 12 1 13
343 12 $totallen += $content->[$j];
344 }
345  
346 $self->{'verbose'}
347 && $self->_debug(
348 1 ▶ $self->{'templates'}->{$template_id}
Branch: 2 branches
BranchTrueFalseTotal
$self->{"verbose"} and $self->_debug($self->{"templates"}{$template_id} ? "update templates item $template_id" : "add $template_id to templates", "content: $totallen", "length: $totallen") 1 0 1
$self->{'templates'}{$template_id} ? : 0 1 1
349 ? "update templates item $template_id"
350 : "add $template_id to templates",
351 "content: $totallen",
352 "length: $totallen"
353 );
354  
355 1 $self->{'templates'}->{$template_id} = {
356 'content' => $content,
357 'length' => $totallen
358 };
359  
360 1 $i += (2 + $fld_count * 2);
361  
362 1 push @tmpl, { $template_id => $content };
363 } ## end while ($i < $count)
364  
365 1 return @tmpl;
366 } ## end sub _parse_template_v9
367  
368 1 ▶sub _debug {
Subroutine _debug: called CC=1 SCAR=0.0
369 1 my ($self, @msg) = @_;
370 1 (undef, undef, my $line) = caller;
371 1 print join(' ', "LINE[$line]:", @msg, $/);
372 }
373  
374 =head1 EXAMPLE - L<Netflow Collector|https://metacpan.org/pod/Netflow::Collector>
375  
376 my $p = Netflow::Parser->new(
377 verbose => 1,
378 flow_cb => sub {
379 my ($hr) = @_;
380 ...
381 }
382  
383 Netflow::Collector->new(
384 port => $port,
385 dispatch => sub {
386 $p->parse(@_)
387 })->run();
388  
389 =head1 AUTHOR
390  
391 Alexei Pastuchov E<lt>palik at cpan dot orgE<gt>.
392  
393 =head1 REPOSITORY
394  
395 L<https://github.com/p-alik/Netflow-Parser>
396  
397 =head1 LICENSE AND COPYRIGHT
398  
399 Copyright 2014-2016 by Alexei Pastuchov E<lt>palik at cpan dot orgE<gt>.
400  
401 This library is free software; you can redistribute it and/or modify
402 it under the same terms as Perl itself.
403  
404 =cut
405  
406 3 1; # End of Netflow::Parser
407  
408 {
409  
410 package Netflow::Parser::Packet;
411 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
413  
414 3 ▶ use fields qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
415 flowsets
416 header
417 templates
418 parsed
419 unparsed
420 3 /;
421  
422 {
423 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
424 3 foreach my $k (keys %{'Netflow::Parser::Packet::FIELDS'}) {
425 1 ▶ *{$k} = sub { shift->{$k} }
Subroutine __ANON__: called CC=1 SCAR=0.0
426 15 }
427 };
428  
429 1 ▶ sub new {
Subroutine new: called CC=3 SCAR=11.0
430 1 my Netflow::Parser::Packet $self = shift;
431 1 my (%opts) = @_;
432 1 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 0 1
433 1 $self = fields::new($self);
434 }
435  
436 $self->{header}
437 1 = Netflow::Parser::Packet::Header->new(%{ delete $opts{header} });
438  
439 1 $self->{flowsets} = delete $opts{flowsets};
440  
441 1 foreach (qw/templates parsed unparsed/) {
442 3 $self->{$_} = [];
443 }
444  
445 1 return $self;
446 } ## end sub new
447  
448 0 ▶ sub add_template {
Subroutine add_template: not called CC=1 SCAR=6.9
449 0 my ($self, $tmpl) = @_;
450 0 push @{ $self->{templates} }, $tmpl;
451 }
452  
453 1 ▶ sub add_parsed {
Subroutine add_parsed: called CC=1 SCAR=0.0
454 1 my ($self, $hr) = @_;
455 1 push @{ $self->{parsed} }, $hr;
456 }
457  
458 0 ▶ sub add_unparsed {
Subroutine add_unparsed: not called CC=1 SCAR=6.9
459 0 my ($self, $hr) = @_;
460 0 push @{ $self->{unparsed} }, $hr;
461 }
462  
463 }
464  
465 3 1; # End of Netflow::Parser::Packet
466  
467 {
468  
469 package Netflow::Parser::Packet::Header;
470 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
471 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
472  
473 3 ▶ use fields qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
474 version
475 count
476 sysuptime
477 unix_secs
478 seqno
479 source_id
480 3 /;
481  
482 3 my @fields;
483 {
484 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
485 3 @fields = keys %{'Netflow::Parser::Packet::Header::FIELDS'};
486 3 foreach (@fields) {
487 0 ▶ *{$_} = sub { shift->{$_} }
Subroutine __ANON__: not called CC=1 SCAR=1.2
488 18 }
489 };
490  
491 1 ▶ sub new {
Subroutine new: called CC=4 SCAR=14.0
492 1 my Netflow::Parser::Packet::Header $self = shift;
493 1 my (%opts) = @_;
494 1 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 0 1
495 1 $self = fields::new($self);
496 }
497  
498 1 foreach (@fields) {
499 6 ▶ $opts{$_} || next;
Branch: 1 branch
BranchTrueFalseTotal
$opts{$_} or next 6 0 6
500 6 $self->{$_} = delete $opts{$_};
501 }
502  
503 1 return $self;
504 } ## end sub new
505 }
506  
507 3 1; # End of Netflow::Parser::Packet::Header
↑ Summary