| 1 | package Net::NfDump; | ||
| 2 | |||
| 3 | use 5.000001; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | use AutoLoader; | ||
| 10 | use if $] < 5.014000, Socket => qw(inet_aton AF_INET); | ||
| 11 | use if $] < 5.014000, Socket6 => qw(inet_ntop inet_pton AF_INET6); | ||
| 12 | use if $] >= 5.014000, Socket => qw(inet_ntop inet_pton inet_aton AF_INET6 AF_INET); | ||
| 13 | use Net::NfDump::Fields; | ||
| 14 | use threads; | ||
| 15 | |||
| 16 | our @ISA = qw(Exporter); | ||
| 17 | |||
| 18 | our $VERSION = '1.31'; | ||
| 19 | |||
| 20 | # XXX | ||
| 21 | # Items to export into callers namespace by default. Note: do not export | ||
| 22 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 23 | # Do not simply export all your public functions/methods/constants. | ||
| 24 | |||
| 25 | # This allows declaration use Net::NfDump ':all'; | ||
| 26 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 27 | # will save memory. | ||
| 28 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 29 | ip2txt txt2ip | ||
| 30 | mac2txt txt2mac | ||
| 31 | family2txt txt2family | ||
| 32 | mpls2txt txt2mpls | ||
| 33 | flow2txt txt2flow | ||
| 34 | file_info | ||
| 35 | ) ] ); | ||
| 36 | |||
| 37 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 38 | |||
| 39 | our @EXPORT = qw( | ||
| 40 | |||
| 41 | ); | ||
| 42 | |||
| 43 | |||
| 44 | sub AUTOLOAD { | ||
| 45 | # This AUTOLOAD is used to 'autoload' constants from the constant() | ||
| 46 | # XS function. | ||
| 47 | |||
| 48 | my $constname; | ||
| 49 | our $AUTOLOAD; | ||
| 50 | ($constname = $AUTOLOAD) =~ s/.*:://; | ||
| 51 | croak "&Net::NfDump::constant not defined" if $constname eq 'constant'; | ||
| 52 | my ($error, $val) = constant($constname); | ||
| 53 | if ($error) { croak $error; } | ||
| 54 | { | ||
| 55 | no strict 'refs'; | ||
| 56 | # Fixed between 5.005_53 and 5.005_61 | ||
| 57 | #XXX if ($] >= 5.00561) { | ||
| 58 | #XXX *$AUTOLOAD = sub () { $val }; | ||
| 59 | #XXX } | ||
| 60 | #XXX else { | ||
| 61 | *$AUTOLOAD = sub { $val }; | ||
| 62 | #XXX } | ||
| 63 | } | ||
| 64 | goto &$AUTOLOAD; | ||
| 65 | } | ||
| 66 | |||
| 67 | require XSLoader; | ||
| 68 | XSLoader::load('Net::NfDump', $VERSION); | ||
| 69 | |||
| 70 | # Preloaded methods go here. | ||
| 71 | |||
| 72 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||
| 73 | |||
| 74 | # Below is stub documentation for your module. You'd better edit it! | ||
| 75 | |||
| 76 | # how to convert particular type to | ||
| 77 | my %CVTTYPE = ( | ||
| 78 | 'ip' => 'ip', 'srcip' => 'ip', 'dstip' => 'ip', 'nexthop' => 'ip', 'bgpnexthop' => 'ip', 'router' => 'ip', | ||
| 79 | 'net' => 'ip', 'srcnet' => 'ip', 'dstnet' => 'ip', 'routerip' => 'ip', 'nextip' => 'ip', | ||
| 80 | 'insrcmac' => 'mac', 'outsrcmac' => 'mac', 'indstmac' => 'mac', 'outdstmac' => 'mac', | ||
| 81 | 'mpls' => 'mpls', | ||
| 82 | 'xsrcip' => 'ip', 'xdstip' => 'ip', 'nsrcip' => 'ip', 'ndstip' => 'ip', | ||
| 83 | 'inetfamily' => 'family' ); | ||
| 84 | |||
| 85 | =head1 NAME | ||
| 86 | |||
| 87 | Net::NfDump - Perl API for manipulating with nfdump files based on libnf.net library | ||
| 88 | |||
| 89 | =head1 SYNOPSIS | ||
| 90 | |||
| 91 | use Net::NfDump; | ||
| 92 | |||
| 93 | # | ||
| 94 | # | ||
| 95 | # Example 1: reading nfdump file(s) | ||
| 96 | # | ||
| 97 | |||
| 98 | $flow = new Net::NfDump( | ||
| 99 | InputFiles => [ 'nfdump_file1', 'nfdump_file2' ], | ||
| 100 | Filter => 'icmp and src net 10.0.0.0/8', | ||
| 101 | Fields => 'proto, bytes' ); | ||
| 102 | |||
| 103 | $flow->query(); | ||
| 104 | |||
| 105 | while (my ($proto, $bytes) = $flow->fetchrow_array() ) { | ||
| 106 | $h{$proto} += $bytes; | ||
| 107 | } | ||
| 108 | $flow->finish(); | ||
| 109 | |||
| 110 | foreach ( keys %h ) { | ||
| 111 | printf "%s %d\n", $_, $h{$_}; | ||
| 112 | } | ||
| 113 | |||
| 114 | # | ||
| 115 | # | ||
| 116 | # Example 2: reading nfdump file(s) with aggregation and sorting | ||
| 117 | # | ||
| 118 | |||
| 119 | $flow = new Net::NfDump( | ||
| 120 | InputFiles => [ 'nfdump_file1', 'nfdump_file2' ], | ||
| 121 | Filter => 'icmp and src net 10.0.0.0/8', | ||
| 122 | Fields => 'srcip/24/64, bytes', | ||
| 123 | Aggreg => 1, OrderBy => "bytes" ); | ||
| 124 | |||
| 125 | $flow->query(); | ||
| 126 | |||
| 127 | while (my ($ip, $bytes) = $flow->fetchrow_array() ) { | ||
| 128 | printf "%s %d\n", $ip, $bytes; | ||
| 129 | $h{$proto} += $bytes; | ||
| 130 | } | ||
| 131 | $flow->finish(); | ||
| 132 | |||
| 133 | |||
| 134 | # | ||
| 135 | # | ||
| 136 | # Example 3: creating and writing records to nfdump file | ||
| 137 | # | ||
| 138 | |||
| 139 | $flow = new Net::NfDump( | ||
| 140 | OutputFile => 'output.nfcap', | ||
| 141 | Fields => 'srcip,dstip' ); | ||
| 142 | |||
| 143 | $flow->storerow_arrayref( [ txt2ip('147.229.3.10'), txt2ip('1.2.3.4') ] ); | ||
| 144 | |||
| 145 | $flow->finish(); | ||
| 146 | |||
| 147 | |||
| 148 | # | ||
| 149 | # | ||
| 150 | # Example 4: reading/writing (merging two input files) and swap | ||
| 151 | # source and destination address if the destination port | ||
| 152 | # is 80/http (I know it doesn't make much sense). | ||
| 153 | # | ||
| 154 | |||
| 155 | $flow1 = new Net::NfDump( | ||
| 156 | InputFiles => [ 'nfdump_file1', 'nfdump_file2' ], | ||
| 157 | Fields => 'srcip, dstip, dstport' ); | ||
| 158 | |||
| 159 | $flow2 = new Net::NfDump( | ||
| 160 | OutputFile => 'nfdump_file_out', | ||
| 161 | Fields => 'srcip, dstip, dstport' ); | ||
| 162 | |||
| 163 | $flow1->query(); | ||
| 164 | $flow2->create(); | ||
| 165 | |||
| 166 | while (my $ref = $flow->fetchrow_arrayref() ) { | ||
| 167 | |||
| 168 | if ( $ref->[2] == 80 ) { | ||
| 169 | ($ref->[0], $ref->[1]) = ($ref->[1], $ref->[0]); | ||
| 170 | } | ||
| 171 | |||
| 172 | $flow2->clonerow($flow1); | ||
| 173 | $flow2->storerow_arrayref($ref); | ||
| 174 | |||
| 175 | } | ||
| 176 | |||
| 177 | $flow1->finish(); | ||
| 178 | $flow2->finish(); | ||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | =head1 DESCRIPTION | ||
| 184 | |||
| 185 | Nfdump L<http://nfdump.sourceforge.net/> is a very popular toolset | ||
| 186 | for collecting, storing and processing NetFlow/SFlow/IPFIX data. | ||
| 187 | One of the key tools is a command line utility bearing the same name | ||
| 188 | as the whole toolset (nfdump). Although this utility can process data | ||
| 189 | very fast, it is cumbersome for some applications. | ||
| 190 | |||
| 191 | This module implements basic operations and allows to | ||
| 192 | read, create and write flow records on binary files produced | ||
| 193 | with nfdump tool. The module tries to keep the same naming conventions for | ||
| 194 | methods as are used in DBI modules/API, so developers who | ||
| 195 | got used to work with such interface should remain familiar with the new one. | ||
| 196 | |||
| 197 | The module uses the original nfdump sources to implement necessary | ||
| 198 | functions. This enables to keep the compatibility with the original | ||
| 199 | nfdump quiet easily and to cope with future versions of the nfdump tool with a minimal effort. | ||
| 200 | |||
| 201 | The architecture is following: | ||
| 202 | |||
| 203 | |||
| 204 | APPLICATION | ||
| 205 | +------------------------+ | ||
| 206 | | | Implements all methods and functions | ||
| 207 | | Net::NfDump API (perl) | described in this document. | ||
| 208 | | | | ||
| 209 | +------------------------+ | ||
| 210 | | | The code converts internal nfdump | ||
| 211 | | libnf - glue code (C) | structures into perl and back to C. | ||
| 212 | | | See http://libnf.net for more information. | ||
| 213 | +------------------------+ | ||
| 214 | | | All original nfdump source files. There | ||
| 215 | | nfdump sources (C) | are no changes in these files. All | ||
| 216 | | | changes are placed into libnf code. | ||
| 217 | +------------------------+ | ||
| 218 | NFDUMP FILES | ||
| 219 | |||
| 220 | |||
| 221 | We always try to update Net::NfDump te lastest version of B<nfdump> available on L<https://github.com/phaag/nfdump>. Support for NSEL code is enabled. | ||
| 222 | |||
| 223 | =head1 WARNING FOR VERSION >= 0.13 | ||
| 224 | |||
| 225 | The files created by Net::NfDump version >= 0.13 can be read only with | ||
| 226 | nfdump 1.6.12 and newer. For reading it supports all formats | ||
| 227 | starting with nfdump 1.6. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | # converts comma seperated string to array reference | ||
| 232 | sub split_str($) { | ||
| 233 | my ($arg) = @_; | ||
| 234 | |||
| 235 | if (ref $arg eq 'ARRAY') { | ||
| 236 | # already is an array | ||
| 237 | return $arg; | ||
| 238 | } else { | ||
| 239 | my @arr = split(/,\s*/, $arg); | ||
| 240 | chomp @arr; | ||
| 241 | return \@arr; | ||
| 242 | } | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | # merge $opts with class default opts and return the resilt. | ||
| 248 | |||
| 249 | sub merge_opts { | ||
| 250 | my ($self, %opts) = @_; | ||
| 251 | |||
| 252 | my $ropts = {}; | ||
| 253 | while ( my ($key, $val) = each %{$self->{opts}} ) { | ||
| 254 | $ropts->{$key} = $val; | ||
| 255 | |||
| 256 | } | ||
| 257 | |||
| 258 | while ( my ($key, $val) = each %opts ) { | ||
| 259 | if ($key eq "InputFiles" || $key eq "Fields" ) { | ||
| 260 | $val = split_str($val); | ||
| 261 | } | ||
| 262 | $ropts->{$key} = $val; | ||
| 263 | } | ||
| 264 | |||
| 265 | return $ropts; | ||
| 266 | } | ||
| 267 | |||
| 268 | # Internal function to set output items/fields. At the input takes array that | ||
| 269 | # represents string names of the files | ||
| 270 | sub set_fields { | ||
| 271 | my ($self, $fieldsref) = @_; | ||
| 272 | |||
| 273 | $self->{fields_num} = []; | ||
| 274 | $self->{fields_txt} = []; | ||
| 275 | |||
| 276 | foreach (@{$fieldsref}) { | ||
| 277 | |||
| 278 | my $fld = lc($_); | ||
| 279 | |||
| 280 | # add all fields | ||
| 281 | if ($fld eq '*') { | ||
| 282 | push(@{$self->{fields_num}}, values %Net::NfDump::Fields::NFL_FIELDS_TXT ); | ||
| 283 | push(@{$self->{fields_txt}}, keys %Net::NfDump::Fields::NFL_FIELDS_TXT ); | ||
| 284 | # regular item | ||
| 285 | } else { | ||
| 286 | |||
| 287 | if ( !defined($Net::NfDump::Fields::NFL_FIELDS_TXT{$fld}) ) { | ||
| 288 | croak(sprintf("Unknown field \"%s\".", $_)); | ||
| 289 | } | ||
| 290 | |||
| 291 | push(@{$self->{fields_num}}, $Net::NfDump::Fields::NFL_FIELDS_TXT{$fld}); | ||
| 292 | push(@{$self->{fields_txt}}, $fld);; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | |||
| 296 | $self->{NAME} = $self->{fields_txt}; | ||
| 297 | $self->{NUM_OF_FIELDS} = scalar @{$self->{fields_txt}}; | ||
| 298 | |||
| 299 | $self->{opts}->{Fields} = $self->{fields_txt}; | ||
| 300 | |||
| 301 | return Net::NfDump::libnf_set_fields($self->{handle}, $self->{fields_num}); | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | |||
| 306 | =head1 METHODS, OPTIONS AND RELATED FUNCTIONS | ||
| 307 | |||
| 308 | =head2 Options | ||
| 309 | |||
| 310 | Options can be handled by various methods. The basic options can be handled | ||
| 311 | by the constructor and then modified by methods such as $obj->query() or $obj->create(). | ||
| 312 | |||
| 313 | The values after => indicate the default value for the item. | ||
| 314 | |||
| 315 | |||
| 316 | =over | ||
| 317 | |||
| 318 | =item * B<InputFiles> => [] | ||
| 319 | |||
| 320 | List of files to read (arrayref). | ||
| 321 | |||
| 322 | =item * B<Filter> => 'any' | ||
| 323 | |||
| 324 | Filter that is applied on input records. It uses nfdump/tcpdump syntax. | ||
| 325 | |||
| 326 | =item * B<Fields> => '*' | ||
| 327 | |||
| 328 | List of fields to read or to update. Any supported field can be used | ||
| 329 | here. See the chapter "Supported Fields" for the full list. | ||
| 330 | Special field * can be used to define all fields. | ||
| 331 | |||
| 332 | =item * B<Aggreg> => 0 | ||
| 333 | |||
| 334 | Create aggregated result. When the method ->query() is called | ||
| 335 | the library loads data into memory structure and | ||
| 336 | perform aggregation according the Fields attribute. | ||
| 337 | |||
| 338 | =item * B<OrderBy> => '<none>' | ||
| 339 | |||
| 340 | Sort the final result according the field specified. It can by | ||
| 341 | used only for aggregated results. | ||
| 342 | |||
| 343 | =item * B<TimeWindowStart>, B<TimeWindowEnd> => 0 | ||
| 344 | |||
| 345 | Filter flows that start or end in the specific time window. | ||
| 346 | The options use unix timestamp values or 0 if the filter should | ||
| 347 | not be applied. | ||
| 348 | |||
| 349 | =item * B<OutputFile> => undef | ||
| 350 | |||
| 351 | Output file for storerow_* methods. Default: undef | ||
| 352 | |||
| 353 | =item * B<Compressed> => 1 | ||
| 354 | |||
| 355 | Flag indicating whether the output files should be compressed or not. | ||
| 356 | |||
| 357 | =item * B<Anonymized> => 0 | ||
| 358 | |||
| 359 | Flag indicating that output file contains anonymized data. | ||
| 360 | |||
| 361 | =item * B<Ident> => '' | ||
| 362 | |||
| 363 | String identificator of files. The value is stored in the file header. | ||
| 364 | |||
| 365 | =item * B<CompatMode> => 0 | ||
| 366 | |||
| 367 | Enable nfdump compatibility features. Some features are implemented differently | ||
| 368 | comparing to original nfdump. Currently thi option enables only | ||
| 369 | LNF_OPT_COMP_STATSCMP for aggregated statistics computation. | ||
| 370 | |||
| 371 | =back | ||
| 372 | |||
| 373 | =head2 Constructor, status information methods | ||
| 374 | |||
| 375 | =over | ||
| 376 | |||
| 377 | =item * B<$obj = new Net::NfDump( %opts )> | ||
| 378 | |||
| 379 | |||
| 380 | my $obj = new Net::NfDump( InputFiles => [ 'file1'] ); | ||
| 381 | |||
| 382 | |||
| 383 | The constructor. It defines the way the parameter options can be specified. | ||
| 384 | |||
| 385 | |||
| 386 | =cut | ||
| 387 | |||
| 388 | sub new { | ||
| 389 | my ($self, %opts) = @_; | ||
| 390 | |||
| 391 | my ($class) = {}; | ||
| 392 | bless($class); | ||
| 393 | |||
| 394 | my $handle = Net::NfDump::libnf_init(); | ||
| 395 | |||
| 396 | if (!defined($handle) || $handle == 0) { | ||
| 397 | return undef; | ||
| 398 | } | ||
| 399 | |||
| 400 | $class->{opts} = { | ||
| 401 | InputFiles => [], | ||
| 402 | Filter => 'any', | ||
| 403 | Fields => [ '*' ], | ||
| 404 | TimeWindowStart => 0, | ||
| 405 | TimeWindowEnd => 0, | ||
| 406 | OutputFile => undef, | ||
| 407 | Compressed => 1, | ||
| 408 | Anonymized => 0, | ||
| 409 | Ident => "" | ||
| 410 | }; | ||
| 411 | |||
| 412 | $class->{opts} = $class->merge_opts(%opts); | ||
| 413 | |||
| 414 | $class->{handle} = $handle; | ||
| 415 | |||
| 416 | $class->{read_prepared} = 0; | ||
| 417 | $class->{write_prepared} = 0; | ||
| 418 | $class->{closed} = 0; | ||
| 419 | $class->{last_hashref_items} = ""; | ||
| 420 | |||
| 421 | return $class; | ||
| 422 | } | ||
| 423 | |||
| 424 | =pod | ||
| 425 | |||
| 426 | =item * B<< $ref = $obj->info() >> | ||
| 427 | |||
| 428 | |||
| 429 | my $i = $obj->info(); | ||
| 430 | print Dumper($i); | ||
| 431 | |||
| 432 | |||
| 433 | informs about the current state of processing input files. It | ||
| 434 | returns information about already processed files, blocks and records. The | ||
| 435 | information may be useful for estimating the time of processing the whole | ||
| 436 | dataset. Hashref returns following items: | ||
| 437 | |||
| 438 | total_files - total number of files to process | ||
| 439 | elapsed_time - elapsed time | ||
| 440 | remaining_time - estimated remaining time to process all records | ||
| 441 | percent - estimated percentage of processed records | ||
| 442 | |||
| 443 | processed_files - total number of processed files | ||
| 444 | processed_records - total number of processed records | ||
| 445 | processed_blocks - total number of processed blocks | ||
| 446 | processed_bytes - total number of processed bytes | ||
| 447 | number of bytes read from file | ||
| 448 | system after uncompressing | ||
| 449 | |||
| 450 | current_filename - the name of the file currently processed | ||
| 451 | current_total_blocks - the number of blocks in the currently | ||
| 452 | processed file | ||
| 453 | current_processed_blocks - the number of processed blocks in the | ||
| 454 | currently processed file | ||
| 455 | |||
| 456 | =cut | ||
| 457 | |||
| 458 | sub info { | ||
| 459 | my ($self) = @_; | ||
| 460 | |||
| 461 | my $ref = Net::NfDump::libnf_instance_info($self->{handle}); | ||
| 462 | |||
| 463 | if ($ref->{'total_files'} > 0 && $ref->{'current_total_blocks'} > 0) { | ||
| 464 | my $totf = $ref->{'total_files'}; | ||
| 465 | my $cp = $ref->{'current_processed_blocks'} / $ref->{'current_total_blocks'} * 100; | ||
| 466 | $ref->{'percent'} = ($ref->{'processed_files'} - 1 )/ $totf * 100 + $cp / $totf; | ||
| 467 | } | ||
| 468 | |||
| 469 | if (defined($self->{read_started})) { | ||
| 470 | my $etime = time() - $self->{read_started}; | ||
| 471 | $ref->{'elapsed_time'} = $etime; | ||
| 472 | $ref->{'remaining_time'} = $etime / ($ref->{'percent'} / 100) - $etime; | ||
| 473 | } | ||
| 474 | |||
| 475 | return $ref; | ||
| 476 | |||
| 477 | } | ||
| 478 | |||
| 479 | |||
| 480 | =pod | ||
| 481 | |||
| 482 | =item * B<< $obj->finish() >> | ||
| 483 | |||
| 484 | |||
| 485 | $obj->finish(); | ||
| 486 | |||
| 487 | |||
| 488 | closes all open file handles. It is necessary to call the method especially | ||
| 489 | when a new file is created. The method flushes the file records which remained in the memory | ||
| 490 | buffer and updates file statistics in the header. Without calling this method the | ||
| 491 | output file might be corrupted. | ||
| 492 | |||
| 493 | =back | ||
| 494 | |||
| 495 | =cut | ||
| 496 | |||
| 497 | sub finish { | ||
| 498 | my ($self) = @_; | ||
| 499 | |||
| 500 | # handle, row reference | ||
| 501 | Net::NfDump::libnf_finish($self->{handle}); | ||
| 502 | |||
| 503 | $self->{read_prepared} = 0; | ||
| 504 | $self->{write_prepared} = 0; | ||
| 505 | $self->{closed} = 1; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub DESTROY { | ||
| 509 | my ($self) = @_; | ||
| 510 | |||
| 511 | # handle, row reference | ||
| 512 | if (!$self->{closed}) { | ||
| 513 | Net::NfDump::libnf_finish($self->{handle}); | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | =pod | ||
| 518 | |||
| 519 | =head2 Methods for reading data | ||
| 520 | |||
| 521 | =over | ||
| 522 | |||
| 523 | =item * B<< $obj->query( %opts ) >> | ||
| 524 | |||
| 525 | |||
| 526 | $obj->query( Filter => 'src host 10.10.10.1' ); | ||
| 527 | |||
| 528 | |||
| 529 | This method has to be applied before any of the C<fetchrow_*> methods is used. Any option described before can be used as a parameter of the method. | ||
| 530 | |||
| 531 | After executing query command it possible to access $flow->{NUM_OF_FIELDS} and $flow->{NAME} variable to get returnd | ||
| 532 | number of fields and field names. Here is an exmaple of code to acces field names: | ||
| 533 | |||
| 534 | foreach $colno (0..$flow->{NUM_OF_FIELDS}-1) { | ||
| 535 | print $flow->{NAME}->[$colno]."\t"; | ||
| 536 | } | ||
| 537 | |||
| 538 | =cut | ||
| 539 | |||
| 540 | # Query method can be used in two ways. If the string argument is the | ||
| 541 | # flow query is handled. See section FLOW QUERY how to create flow | ||
| 542 | # queries. | ||
| 543 | |||
| 544 | # =cut# | ||
| 545 | |||
| 546 | sub query { | ||
| 547 | my ($self, %opts) = @_; | ||
| 548 | |||
| 549 | |||
| 550 | my $o = $self->merge_opts(%opts); | ||
| 551 | |||
| 552 | if (@{$o->{InputFiles}} == 0) { | ||
| 553 | croak("No input files defined"); | ||
| 554 | } | ||
| 555 | |||
| 556 | my @resfields = (); | ||
| 557 | foreach my $fld (@{$o->{Fields}}) { | ||
| 558 | my $numbits = 0; | ||
| 559 | my $numbits6 = 0; | ||
| 560 | if (defined($CVTTYPE{$fld}) && $CVTTYPE{$fld} eq 'ip') { | ||
| 561 | $numbits = 32; | ||
| 562 | $numbits6 = 128; | ||
| 563 | } | ||
| 564 | |||
| 565 | if ($fld =~ /\//) { | ||
| 566 | ($fld, $numbits, $numbits6) = split(/\//, $fld); | ||
| 567 | $numbits6 = $numbits if (!defined($numbits6)); | ||
| 568 | } | ||
| 569 | push(@resfields, $fld); | ||
| 570 | |||
| 571 | |||
| 572 | if (defined($o->{Aggreg}) && $o->{Aggreg}) { | ||
| 573 | if ($fld eq '*') { | ||
| 574 | croak("Symbol '*' is not allowed for aggregated items"); | ||
| 575 | } | ||
| 576 | |||
| 577 | if (!defined($Net::NfDump::Fields::NFL_FIELDS_TXT{$fld})) { | ||
| 578 | croak("Unknown field $fld"); | ||
| 579 | } | ||
| 580 | |||
| 581 | my $id = $Net::NfDump::Fields::NFL_FIELDS_TXT{$fld}; | ||
| 582 | # my $flags = $Net::NfDump::Fields::NFL_FIELDS_DEFAULT_AGGR{$id}; | ||
| 583 | my $flags = 0; | ||
| 584 | |||
| 585 | if (defined($o->{OrderBy}) && $fld eq $o->{OrderBy}) { | ||
| 586 | $flags |= $Net::NfDump::Fields::NFL_FIELDS_DEFAULT_SORT{$id}; | ||
| 587 | } | ||
| 588 | |||
| 589 | Net::NfDump::libnf_aggr_add($self->{handle}, $id, $flags, | ||
| 590 | $numbits, $numbits6); | ||
| 591 | } elsif (defined($o->{OrderBy}) && $fld eq $o->{OrderBy}) { | ||
| 592 | # sorting in list mode | ||
| 593 | |||
| 594 | my $id = $Net::NfDump::Fields::NFL_FIELDS_TXT{$fld}; | ||
| 595 | my $flags = $Net::NfDump::Fields::NFL_FIELDS_DEFAULT_SORT{$id}; | ||
| 596 | Net::NfDump::libnf_aggr_add($self->{handle}, $id, $flags, | ||
| 597 | $numbits, $numbits6); | ||
| 598 | |||
| 599 | Net::NfDump::libnf_listmode($self->{handle}); | ||
| 600 | } | ||
| 601 | |||
| 602 | } | ||
| 603 | |||
| 604 | $self->set_fields([ @resfields ]); | ||
| 605 | |||
| 606 | if (defined($o->{CompatMode}) && $o->{CompatMode}) { | ||
| 607 | Net::NfDump::libnf_compatmode($self->{handle}); | ||
| 608 | } | ||
| 609 | |||
| 610 | |||
| 611 | # handle, filter, windows start, windows end, ref to filelist | ||
| 612 | Net::NfDump::libnf_read_files($self->{handle}, $o->{Filter}, | ||
| 613 | $o->{TimeWindowStart}, $o->{TimeWindowEnd}, | ||
| 614 | $o->{InputFiles}); | ||
| 615 | |||
| 616 | $self->{read_prepared} = 1; | ||
| 617 | $self->{read_started} = time(); | ||
| 618 | |||
| 619 | } | ||
| 620 | |||
| 621 | =pod | ||
| 622 | |||
| 623 | =item * B<< $ref = $obj->fetchrow_arrayref() >> | ||
| 624 | |||
| 625 | |||
| 626 | while (my $ref = $obj->fetchrow_arrayref() ) { | ||
| 627 | print Dumper($ref); | ||
| 628 | } | ||
| 629 | |||
| 630 | |||
| 631 | This method has to be used after the query method. The method $obj->query() is called | ||
| 632 | automatically if it has not been called before. | ||
| 633 | |||
| 634 | It returns array reference with the record and skips to next record. It returns | ||
| 635 | "true" if there are more records to read or "undef" if the end of a record set has been reached. | ||
| 636 | |||
| 637 | =cut | ||
| 638 | |||
| 639 | sub fetchrow_arrayref { | ||
| 640 | my ($self) = @_; | ||
| 641 | |||
| 642 | if (!$self->{read_prepared}) { | ||
| 643 | $self->query(); | ||
| 644 | } | ||
| 645 | |||
| 646 | my $ret = Net::NfDump::libnf_read_row($self->{handle}); | ||
| 647 | |||
| 648 | #the end of the file/files we set back read prepared to 0 | ||
| 649 | if (!$ret) { | ||
| 650 | $self->{read_prepared} = 0; | ||
| 651 | } | ||
| 652 | |||
| 653 | return $ret; | ||
| 654 | } | ||
| 655 | |||
| 656 | =pod | ||
| 657 | |||
| 658 | =item * B<< @array = $obj->fetchrow_array() >> | ||
| 659 | |||
| 660 | |||
| 661 | while ( @array = $obj->fetchrow_arrayref() ) { | ||
| 662 | print Dumper(\@array); | ||
| 663 | } | ||
| 664 | |||
| 665 | |||
| 666 | It has the same function as fetchrow_arrayref; however, it returns items in array instead. | ||
| 667 | |||
| 668 | =cut | ||
| 669 | |||
| 670 | sub fetchrow_array { | ||
| 671 | my ($self) = @_; | ||
| 672 | |||
| 673 | my $ref = $self->fetchrow_arrayref(); | ||
| 674 | |||
| 675 | return if (!defined($ref)); | ||
| 676 | |||
| 677 | return @{$ref}; | ||
| 678 | } | ||
| 679 | |||
| 680 | =pod | ||
| 681 | |||
| 682 | =item * B<< $ref = $obj->fetchrow_hashref() >> | ||
| 683 | |||
| 684 | |||
| 685 | while ( $ref = $obj->fetchrow_hashref() ) { | ||
| 686 | print Dumper($ref); | ||
| 687 | } | ||
| 688 | |||
| 689 | |||
| 690 | The same case as fetchrow_arrayref; however, the items are returned in the hash reference as the | ||
| 691 | key => vallue tuples. | ||
| 692 | |||
| 693 | NOTE: This method can be very ineffective in some cases, please, see PERFORMANCE section. | ||
| 694 | |||
| 695 | =back | ||
| 696 | |||
| 697 | =cut | ||
| 698 | |||
| 699 | sub fetchrow_hashref { | ||
| 700 | my ($self) = @_; | ||
| 701 | |||
| 702 | my %res; | ||
| 703 | my $ref = $self->fetchrow_arrayref(); | ||
| 704 | |||
| 705 | return if (!defined($ref)); | ||
| 706 | |||
| 707 | my $numfields = scalar @{$self->{fields_txt}}; | ||
| 708 | for (my $x = 0; $x < $numfields; $x++) { | ||
| 709 | $res{$self->{fields_txt}->[$x]} = $ref->[$x] if defined($ref->[$x]); | ||
| 710 | } | ||
| 711 | |||
| 712 | return \%res; | ||
| 713 | } | ||
| 714 | |||
| 715 | =pod | ||
| 716 | |||
| 717 | =head2 Methods for writing data | ||
| 718 | |||
| 719 | =over | ||
| 720 | |||
| 721 | =item * B<< $obj->create( %opts ) >> | ||
| 722 | |||
| 723 | |||
| 724 | $obj->create( OutputFile => 'output.nfcapd' ); | ||
| 725 | |||
| 726 | |||
| 727 | This method creates a new nfdump file and has to be applied before any of $obj->storerow_* | ||
| 728 | method is called. | ||
| 729 | |||
| 730 | =cut | ||
| 731 | |||
| 732 | sub create { | ||
| 733 | my ($self, %opts) = @_; | ||
| 734 | |||
| 735 | my $o = $self->merge_opts(%opts); | ||
| 736 | |||
| 737 | if (!defined($o->{OutputFile}) || $o->{OutputFile} eq "") { | ||
| 738 | croak("No output file defined"); | ||
| 739 | } | ||
| 740 | |||
| 741 | $self->set_fields($o->{Fields}); | ||
| 742 | |||
| 743 | # handle, filename, compressed, anonyized, identifier | ||
| 744 | Net::NfDump::libnf_create_file($self->{handle}, | ||
| 745 | $o->{OutputFile}, | ||
| 746 | $o->{Compressed}, | ||
| 747 | $o->{Anonymized}, | ||
| 748 | $o->{Ident}); | ||
| 749 | |||
| 750 | $self->{write_prepared} = 1; | ||
| 751 | } | ||
| 752 | |||
| 753 | =pod | ||
| 754 | |||
| 755 | =item * B<< $obj->storerow_arrayref( [ @array ] ) >> | ||
| 756 | |||
| 757 | |||
| 758 | $obj->storerow_arrayref( [ $srcip, $dstip ] ); | ||
| 759 | |||
| 760 | |||
| 761 | The method inserts data defined in arrayref to the file opened by the method $obj->create(). The number of | ||
| 762 | fields and their order have to follow the order defined in the B<Fields> option | ||
| 763 | handled during $obj->new() or $obj->create() method. | ||
| 764 | |||
| 765 | =cut | ||
| 766 | |||
| 767 | sub storerow_arrayref { | ||
| 768 | my ($self, $row) = @_; | ||
| 769 | |||
| 770 | if (!$self->{write_prepared}) { | ||
| 771 | $self->create(); | ||
| 772 | } | ||
| 773 | |||
| 774 | return Net::NfDump::libnf_write_row($self->{handle}, $row); | ||
| 775 | } | ||
| 776 | |||
| 777 | =pod | ||
| 778 | |||
| 779 | =item * B<< $obj->storerow_array( @array ) >> | ||
| 780 | |||
| 781 | |||
| 782 | $obj->storerow_array( $srcip, $dstip ); | ||
| 783 | |||
| 784 | |||
| 785 | The same case as storerow_arrayref; however, the items are handled as a single array. | ||
| 786 | |||
| 787 | =cut | ||
| 788 | |||
| 789 | sub storerow_array { | ||
| 790 | my ($self, @row) = @_; | ||
| 791 | |||
| 792 | return $self->storerow_arrayref(\@row); | ||
| 793 | } | ||
| 794 | |||
| 795 | =pod | ||
| 796 | |||
| 797 | =item * B<< $obj->storerow_hashref ( \%hash ) >> | ||
| 798 | |||
| 799 | |||
| 800 | $obj->storerow_hashref( { 'srcip' => $srcip, 'dstip' => $dstip } ); | ||
| 801 | |||
| 802 | |||
| 803 | It inserts the structure defined as hash reference into output file. | ||
| 804 | |||
| 805 | NOTE: This method can be very ineffective in some cases, please, see PERFORMANCE section. | ||
| 806 | |||
| 807 | =cut | ||
| 808 | |||
| 809 | sub storerow_hashref { | ||
| 810 | my ($self, $row) = @_; | ||
| 811 | |||
| 812 | return undef if (!defined($row)); | ||
| 813 | |||
| 814 | if (join(',', keys %{$row}) ne $self->{last_hashref_items}) { | ||
| 815 | $self->set_fields( [ keys %{$row} ] ); | ||
| 816 | $self->{last_hashref_items} = join(',', keys %{$row}); | ||
| 817 | } | ||
| 818 | |||
| 819 | return $self->storerow_arrayref( [ values %{$row} ] ); | ||
| 820 | |||
| 821 | } | ||
| 822 | |||
| 823 | =pod | ||
| 824 | |||
| 825 | =item * B<< $obj->clonerow( $obj2 ) >> | ||
| 826 | |||
| 827 | |||
| 828 | $obj->clonerow( $obj2 ); | ||
| 829 | |||
| 830 | |||
| 831 | This method copies the full content of the row from the source object (instance). This method | ||
| 832 | is useful for writing effective scripts. See above the PERFORMANCE chapter. | ||
| 833 | |||
| 834 | =back | ||
| 835 | |||
| 836 | =cut | ||
| 837 | |||
| 838 | sub clonerow { | ||
| 839 | my ($self, $obj) = @_; | ||
| 840 | |||
| 841 | return undef if ( !defined($obj) || !defined($obj->{handle}) ); | ||
| 842 | |||
| 843 | if (!$self->{write_prepared}) { | ||
| 844 | $self->create(); | ||
| 845 | } | ||
| 846 | |||
| 847 | return Net::NfDump::libnf_copy_row($self->{handle}, $obj->{handle}); | ||
| 848 | } | ||
| 849 | |||
| 850 | =pod | ||
| 851 | |||
| 852 | =head2 Extra conversion and support functions | ||
| 853 | |||
| 854 | The module also provides extra convertion functions which allow to convert binnary format | ||
| 855 | of IP address, MAC address and MPLS labels tag into text format and back. | ||
| 856 | |||
| 857 | Those functions are not exported by default, therefore it has to be either called | ||
| 858 | with full module name or imported when the module is loaded. To import | ||
| 859 | all support function C<:all> a synonym may be used. | ||
| 860 | |||
| 861 | use Net::NfDump qw ':all'; | ||
| 862 | |||
| 863 | =over | ||
| 864 | |||
| 865 | =item * B<< $txt = ip2txt( $bin ) >> | ||
| 866 | |||
| 867 | =item * B<< $bin = txt2ip( $txt ) >> | ||
| 868 | |||
| 869 | |||
| 870 | $ip = txt2ip('10.10.10.1'); | ||
| 871 | print ip2txt($ip); | ||
| 872 | |||
| 873 | |||
| 874 | Converts both IPv4 and IPv6 addresses into text form and back. The standard | ||
| 875 | inet_ntop/inet_pton functions can be used instead to provide the same results. | ||
| 876 | |||
| 877 | Function txt2ip returns binnary format of IP address or "undef" | ||
| 878 | if the conversion is not possible. | ||
| 879 | |||
| 880 | =cut | ||
| 881 | |||
| 882 | sub ip2txt ($) { | ||
| 883 | my ($addr) = @_; | ||
| 884 | |||
| 885 | if (!defined($addr)) { | ||
| 886 | return undef; | ||
| 887 | } | ||
| 888 | |||
| 889 | my $type; | ||
| 890 | |||
| 891 | if (length($addr) == 4) { | ||
| 892 | $type = AF_INET; | ||
| 893 | } elsif (length($addr) == 16) { | ||
| 894 | $type = AF_INET6; | ||
| 895 | } else { | ||
| 896 | carp("Invalid IP address length in binary representation"); | ||
| 897 | return undef; | ||
| 898 | } | ||
| 899 | |||
| 900 | return inet_ntop($type, $addr); | ||
| 901 | } | ||
| 902 | |||
| 903 | |||
| 904 | sub txt2ip ($) { | ||
| 905 | my ($addr) = @_; | ||
| 906 | my $type; | ||
| 907 | |||
| 908 | if (!defined($addr)) { | ||
| 909 | return undef; | ||
| 910 | } | ||
| 911 | |||
| 912 | if (index($addr, ':') != -1) { | ||
| 913 | return inet_pton(AF_INET6, $addr); | ||
| 914 | } else { | ||
| 915 | # ubuntu have buggy implementation of inet_pton for IPv4 | ||
| 916 | return inet_aton($addr); | ||
| 917 | } | ||
| 918 | |||
| 919 | } | ||
| 920 | |||
| 921 | =pod | ||
| 922 | |||
| 923 | =item * B<< $txt = mac2txt( $bin ) >> | ||
| 924 | |||
| 925 | =item * B<< $bin = txt2mac( $txt ) >> | ||
| 926 | |||
| 927 | |||
| 928 | $mac = txt2mac('aa:02:c2:2d:e0:12'); | ||
| 929 | print mac2txt($mac); | ||
| 930 | |||
| 931 | |||
| 932 | It converts MAC address to xx:yy:xx:yy:xx:yy format and back. The function mac2txt | ||
| 933 | accepts an address of any following format: | ||
| 934 | |||
| 935 | aabbccddeeff | ||
| 936 | aa:bb:cc:dd:ee:ff | ||
| 937 | aa-bb-cc-dd-ee-ff | ||
| 938 | aabb-ccdd-eeff | ||
| 939 | |||
| 940 | It returns the binnary format of an address or "undef" if the conversion is not possible. | ||
| 941 | |||
| 942 | =cut | ||
| 943 | |||
| 944 | sub mac2txt ($) { | ||
| 945 | my ($addr) = @_; | ||
| 946 | |||
| 947 | if (!defined($addr)) { | ||
| 948 | return undef; | ||
| 949 | } | ||
| 950 | |||
| 951 | if (length($addr) != 6) { | ||
| 952 | carp("Invalid MAC address length in binary representation"); | ||
| 953 | return undef; | ||
| 954 | } | ||
| 955 | |||
| 956 | return sprintf("%s%s:%s%s:%s%s:%s%s:%s%s:%s%s", split('',unpack("H12", $addr))); | ||
| 957 | } | ||
| 958 | |||
| 959 | |||
| 960 | sub txt2mac ($) { | ||
| 961 | my ($addr) = @_; | ||
| 962 | |||
| 963 | if (!defined($addr)) { | ||
| 964 | return undef; | ||
| 965 | } | ||
| 966 | |||
| 967 | $addr =~ s/[\-|\:]//g; | ||
| 968 | |||
| 969 | if (length($addr) != 12) { | ||
| 970 | return undef; | ||
| 971 | } | ||
| 972 | |||
| 973 | return pack('H12', $addr); | ||
| 974 | } | ||
| 975 | |||
| 976 | =pod | ||
| 977 | |||
| 978 | =item * B<< $txt = family2txt( $bin ) >> | ||
| 979 | |||
| 980 | =item * B<< $bin = txt2family( $txt ) >> | ||
| 981 | |||
| 982 | |||
| 983 | $fam = txt2family('ipv6'); | ||
| 984 | print family2txt($fam); | ||
| 985 | |||
| 986 | |||
| 987 | It converts internall address family (AF_INET, AF_INET6) to ipv4 or ipv6 string (or back). | ||
| 988 | |||
| 989 | Function txt2family returns the binnary format of the family representation | ||
| 990 | on the particular platform or "undef" if the conversion is not possible. | ||
| 991 | |||
| 992 | =cut | ||
| 993 | |||
| 994 | sub family2txt ($) { | ||
| 995 | my ($fam) = @_; | ||
| 996 | |||
| 997 | if (!defined($fam)) { | ||
| 998 | return undef; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | if ($fam == AF_INET) { | ||
| 1002 | return 'ipv4'; | ||
| 1003 | } elsif ($fam == AF_INET6) { | ||
| 1004 | return 'ipv6'; | ||
| 1005 | } else { | ||
| 1006 | return undef; | ||
| 1007 | } | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | |||
| 1011 | sub txt2family ($) { | ||
| 1012 | my ($fam) = @_; | ||
| 1013 | |||
| 1014 | if (!defined($fam)) { | ||
| 1015 | return undef; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | if ($fam eq 'ipv4') { | ||
| 1019 | return AF_INET; | ||
| 1020 | } elsif ($fam eq 'ipv6') { | ||
| 1021 | return AF_INET6; | ||
| 1022 | } else { | ||
| 1023 | return undef; | ||
| 1024 | } | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | =pod | ||
| 1028 | |||
| 1029 | =item * B<< $txt = mpls2txt( $mpls ) >> | ||
| 1030 | |||
| 1031 | =item * B<< $mpls = txt2mpls( $txt ) >> | ||
| 1032 | |||
| 1033 | |||
| 1034 | $mpls = txt2mpls('1002-6-0 1003-6-0 1004-0-1'); | ||
| 1035 | print mpls2txt($mpls); | ||
| 1036 | |||
| 1037 | |||
| 1038 | It converts label information into format B<Lbl-Exp-S> and back. | ||
| 1039 | |||
| 1040 | Where: | ||
| 1041 | |||
| 1042 | Lbl - Value given to the MPLS label by the router. | ||
| 1043 | Exp - Value of the experimental bit. | ||
| 1044 | S - Value of the end-of-stack bit: Set to 1 for the oldest | ||
| 1045 | entry in the stack and to zero for all other entries. | ||
| 1046 | |||
| 1047 | =cut | ||
| 1048 | |||
| 1049 | sub mpls2txt ($) { | ||
| 1050 | my ($addr) = @_; | ||
| 1051 | |||
| 1052 | if (!defined($addr)) { | ||
| 1053 | return undef; | ||
| 1054 | } | ||
| 1055 | my @res; | ||
| 1056 | |||
| 1057 | foreach (unpack('I*', $addr)) { | ||
| 1058 | |||
| 1059 | my $lbl = $_ >> 12; | ||
| 1060 | my $exp = ($_ >> 9 ) & 0x7; | ||
| 1061 | my $eos = ($_ >> 8 ) & 0x1; | ||
| 1062 | |||
| 1063 | push(@res, sprintf "%d-%d-%d", $lbl, $exp, $eos) if ($_ != 0); | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | return join(' ', @res); | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | sub txt2mpls ($) { | ||
| 1070 | my ($addr) = @_; | ||
| 1071 | |||
| 1072 | if (!defined($addr)) { | ||
| 1073 | return undef; | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | my $res = ""; | ||
| 1077 | |||
| 1078 | my @labels = split(/\s+/, $addr); | ||
| 1079 | |||
| 1080 | foreach (@labels) { | ||
| 1081 | my ($lbl, $exp, $eos) = split(/\-/); | ||
| 1082 | |||
| 1083 | my $label = ($lbl << 12) | ( $exp << 9 ) | ($eos << 8 ); | ||
| 1084 | |||
| 1085 | $res .= pack("I", $label); | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | |||
| 1089 | $res .= pack("I", 0x0) x (10 - length($res) / 4); # alogn to 10 items (4 * 10 Bytes) | ||
| 1090 | return $res; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | =pod | ||
| 1094 | |||
| 1095 | =item * B<< $ref = flow2txt( \%row ) >> | ||
| 1096 | |||
| 1097 | =item * B<< $ref = txt2flow( \%row ) >> | ||
| 1098 | |||
| 1099 | |||
| 1100 | The function flow2txt gets hash reference to the items returned by fetchrow_hashref and | ||
| 1101 | converts all items into text format readable for human. It applies functions | ||
| 1102 | ip2txt, mac2txt, mpl2txt to the items for which it makes sense. The function | ||
| 1103 | txt2flow does the exact opossite. | ||
| 1104 | |||
| 1105 | =cut | ||
| 1106 | |||
| 1107 | |||
| 1108 | |||
| 1109 | sub flow2txt ($) { | ||
| 1110 | my ($row) = @_; | ||
| 1111 | my %res; | ||
| 1112 | |||
| 1113 | while ( my ($key, $val) = each %{$row}) { | ||
| 1114 | |||
| 1115 | if ( defined($CVTTYPE{$key}) ) { | ||
| 1116 | my $cvt = $CVTTYPE{$key}; | ||
| 1117 | if ($cvt eq 'ip') { | ||
| 1118 | $res{$key} = ip2txt($val); | ||
| 1119 | } elsif ($cvt eq 'mac') { | ||
| 1120 | $res{$key} = mac2txt($val); | ||
| 1121 | } elsif ($cvt eq 'family') { | ||
| 1122 | $res{$key} = family2txt($val); | ||
| 1123 | } elsif ($cvt eq 'mpls') { | ||
| 1124 | $res{$key} = mpls2txt($val); | ||
| 1125 | } else { | ||
| 1126 | croak("Invalid conversion type $cvt"); | ||
| 1127 | } | ||
| 1128 | } else { | ||
| 1129 | $res{$key} = $val; | ||
| 1130 | } | ||
| 1131 | } | ||
| 1132 | |||
| 1133 | return \%res; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | |||
| 1137 | sub txt2flow ($) { | ||
| 1138 | my ($row) = @_; | ||
| 1139 | my %res; | ||
| 1140 | |||
| 1141 | while ( my ($key, $val) = each %{$row}) { | ||
| 1142 | |||
| 1143 | if ( defined($CVTTYPE{$key}) ) { | ||
| 1144 | my $cvt = $CVTTYPE{$key}; | ||
| 1145 | if ($cvt eq 'ip') { | ||
| 1146 | $res{$key} = txt2ip($val); | ||
| 1147 | } elsif ($cvt eq 'mac') { | ||
| 1148 | $res{$key} = txt2mac($val); | ||
| 1149 | } elsif ($cvt eq 'family') { | ||
| 1150 | $res{$key} = txt2family($val); | ||
| 1151 | } elsif ($cvt eq 'mpls') { | ||
| 1152 | $res{$key} = txt2mpls($val); | ||
| 1153 | } else { | ||
| 1154 | croak("Invalid conversion type $cvt"); | ||
| 1155 | } | ||
| 1156 | } else { | ||
| 1157 | $res{$key} = $val; | ||
| 1158 | } | ||
| 1159 | } | ||
| 1160 | |||
| 1161 | return \%res; | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | =pod | ||
| 1165 | |||
| 1166 | =item * B<< $ref = file_info( $file_name ) >> | ||
| 1167 | |||
| 1168 | |||
| 1169 | $ref = file_info('file.nfcap'); | ||
| 1170 | print Dumper($ref); | ||
| 1171 | |||
| 1172 | |||
| 1173 | It reads information from the nfdump file header and provides various attributes | ||
| 1174 | such as number of blocks, version, flags, statistics, etc. As the result, the | ||
| 1175 | following items are returned: | ||
| 1176 | |||
| 1177 | version | ||
| 1178 | ident | ||
| 1179 | blocks | ||
| 1180 | catalog | ||
| 1181 | anonymized | ||
| 1182 | compressed | ||
| 1183 | sequence_failures | ||
| 1184 | |||
| 1185 | first | ||
| 1186 | last | ||
| 1187 | |||
| 1188 | flows, bytes, packets | ||
| 1189 | |||
| 1190 | flows_tcp, flows_udp, flows_icmp, flows_other | ||
| 1191 | bytes_tcp, bytes_udp, bytes_icmp, bytes_other | ||
| 1192 | packets_tcp, packets_udp, packets_icmp, packets_other | ||
| 1193 | |||
| 1194 | |||
| 1195 | =back | ||
| 1196 | |||
| 1197 | =cut | ||
| 1198 | |||
| 1199 | sub file_info { | ||
| 1200 | my ($file) = @_; | ||
| 1201 | |||
| 1202 | my $ref = Net::NfDump::libnf_file_info($file); | ||
| 1203 | |||
| 1204 | return $ref; | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | =pod | ||
| 1208 | |||
| 1209 | |||
| 1210 | =head1 SUPPORTED ITEMS | ||
| 1211 | |||
| 1212 | Up to date list of supported items is available on L<Net::NfDump::Fields> | ||
| 1213 | |||
| 1214 | Time items | ||
| 1215 | ===================== | ||
| 1216 | first - Timestamp of the first packet seen (in miliseconds) | ||
| 1217 | last - Timestamp of the last packet seen (in miliseconds) | ||
| 1218 | received - Timestamp regarding when the packet was received by collector | ||
| 1219 | |||
| 1220 | Statistical items | ||
| 1221 | ===================== | ||
| 1222 | bytes - The number of bytes | ||
| 1223 | pkts - The number of packets | ||
| 1224 | outbytes - The number of output bytes | ||
| 1225 | outpkts - The number of output packets | ||
| 1226 | flows - The number of flows (aggregated) | ||
| 1227 | |||
| 1228 | Layer 4 information | ||
| 1229 | ===================== | ||
| 1230 | srcport - Source port | ||
| 1231 | dstport - Destination port | ||
| 1232 | tcpflags - TCP flags | ||
| 1233 | |||
| 1234 | Layer 3 information | ||
| 1235 | ===================== | ||
| 1236 | srcip - Source IP address | ||
| 1237 | dstip - Destination IP address | ||
| 1238 | nexthop - IP next hop | ||
| 1239 | srcmask - Source mask | ||
| 1240 | dstmask - Destination mask | ||
| 1241 | tos - Source type of service | ||
| 1242 | dsttos - Destination type of service | ||
| 1243 | srcas - Source AS number | ||
| 1244 | dstas - Destination AS number | ||
| 1245 | nextas - BGP Next AS | ||
| 1246 | prevas - BGP Previous AS | ||
| 1247 | bgpnexthop - BGP next hop | ||
| 1248 | proto - IP protocol | ||
| 1249 | |||
| 1250 | Layer 2 information | ||
| 1251 | ===================== | ||
| 1252 | srcvlan - Source vlan label | ||
| 1253 | dstvlan - Destination vlan label | ||
| 1254 | insrcmac - In source MAC address | ||
| 1255 | outsrcmac - Out destination MAC address | ||
| 1256 | indstmac - In destination MAC address | ||
| 1257 | outdstmac - Out source MAC address | ||
| 1258 | |||
| 1259 | MPLS information | ||
| 1260 | ===================== | ||
| 1261 | mpls - MPLS labels | ||
| 1262 | |||
| 1263 | Layer 1 information | ||
| 1264 | ===================== | ||
| 1265 | inif - SNMP input interface number | ||
| 1266 | outif - SNMP output interface number | ||
| 1267 | dir - Flow directions ingress/egress | ||
| 1268 | fwd - Forwarding status | ||
| 1269 | |||
| 1270 | Exporter information | ||
| 1271 | ===================== | ||
| 1272 | router - Exporting router IP | ||
| 1273 | systype - Type of exporter | ||
| 1274 | sysid - Internal SysID of exporter | ||
| 1275 | |||
| 1276 | NSEL fields, see: http://www.cisco.com/en/US/docs/security/asa/asa81/netflow/netflow.html | ||
| 1277 | ===================== | ||
| 1278 | eventtime - NSEL The time that the flow was created | ||
| 1279 | connid - NSEL An identifier of a unique flow for the device | ||
| 1280 | icmpcode - NSEL ICMP code value | ||
| 1281 | icmptype - NSEL ICMP type value | ||
| 1282 | xevent - NSEL Extended event code | ||
| 1283 | xsrcip - NSEL Mapped source IPv4 address | ||
| 1284 | xdstip - NSEL Mapped destination IPv4 address | ||
| 1285 | xsrcport - NSEL Mapped source port | ||
| 1286 | xdstport - NSEL Mapped destination port | ||
| 1287 | NSEL The input ACL that permitted or denied the flow | ||
| 1288 | iacl - Hash value or ID of the ACL name | ||
| 1289 | iace - Hash value or ID of the ACL name | ||
| 1290 | ixace - Hash value or ID of an extended ACE configuration | ||
| 1291 | NSEL The output ACL that permitted or denied a flow | ||
| 1292 | eacl - Hash value or ID of the ACL name | ||
| 1293 | eace - Hash value or ID of the ACL name | ||
| 1294 | exace - Hash value or ID of an extended ACE configuration | ||
| 1295 | username - NSEL username | ||
| 1296 | |||
| 1297 | NEL (NetFlow Event Logging) fields | ||
| 1298 | ===================== | ||
| 1299 | ingressvrfid - NEL NAT ingress vrf id | ||
| 1300 | eventflag - NAT event flag (always set to 1 by nfdump) | ||
| 1301 | egressvrfid - NAT egress VRF ID | ||
| 1302 | |||
| 1303 | NEL Port Block Allocation (added 2014-04-19) | ||
| 1304 | ===================== | ||
| 1305 | blockstart - NAT pool block start | ||
| 1306 | blockend - NAT pool block end | ||
| 1307 | blockstep - NAT pool block step | ||
| 1308 | blocksize - NAT pool block size | ||
| 1309 | |||
| 1310 | Extra/special fields | ||
| 1311 | ===================== | ||
| 1312 | cl - nprobe latency client_nw_delay_usec | ||
| 1313 | sl - nprobe latency server_nw_delay_usec | ||
| 1314 | al - nprobe latency appl_latency_usec | ||
| 1315 | |||
| 1316 | =head1 PERFORMANCE | ||
| 1317 | |||
| 1318 | It is obvious that performance of the perl interface is lower in comparison to | ||
| 1319 | highly optimized nfdump utility. While nfdump is able to process up | ||
| 1320 | to 2 milion of records per second, the Net::NfDump is not able to process | ||
| 1321 | more than 1 milion. However, there are several rules to keep the code optimised: | ||
| 1322 | |||
| 1323 | =over | ||
| 1324 | |||
| 1325 | =item * | ||
| 1326 | |||
| 1327 | Use C<< $obj->fetchrow_arrayref() >> and C<< $obj->storerow_arrayref() >> instead of | ||
| 1328 | C<< *_array >> and C<< *_hashref >> equivalents. Arrayref handles only the reference | ||
| 1329 | to the structure with data. Avoid using C<< *_hashref >> functions, it can be 5-times | ||
| 1330 | slower. | ||
| 1331 | |||
| 1332 | =item * | ||
| 1333 | |||
| 1334 | Handle to the perl API only items which are necessary to be used in the code. It is always | ||
| 1335 | more effective to define in C<< Fields => 'srcip,dstip,...' >> instead of in C<< Fields => '*' >>. | ||
| 1336 | |||
| 1337 | =item * | ||
| 1338 | |||
| 1339 | Preference to using C<< $obj->clonerow($obj2) >> method is highly recommended. This method copies data between | ||
| 1340 | two instances directly in the C code in the libnf layer. | ||
| 1341 | |||
| 1342 | Following code: | ||
| 1343 | |||
| 1344 | $obj1->exec( Fields => '*' ); | ||
| 1345 | $obj2->create( Fields => '*' ); | ||
| 1346 | |||
| 1347 | while ( my $ref = $obj1->fetchrow_arrayref() ) { | ||
| 1348 | # do something with srcip | ||
| 1349 | $obj2->storerow_arrayref($ref); | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | can be written in a more effective way (several times faster): | ||
| 1353 | |||
| 1354 | $obj1->exec( Fields => 'srcip' ); | ||
| 1355 | $obj2->create( Fields => 'srcip' ); | ||
| 1356 | |||
| 1357 | while ( my $ref = $obj1->fetchrow_arrayref() ) { | ||
| 1358 | # do something with srcip | ||
| 1359 | $obj2->clonerow($obj1); | ||
| 1360 | $obj2->storerow_arrayref($ref); | ||
| 1361 | } | ||
| 1362 | |||
| 1363 | |||
| 1364 | =back | ||
| 1365 | |||
| 1366 | |||
| 1367 | |||
| 1368 | =cut | ||
| 1369 | |||
| 1370 | #=head1 FLOW QUERY - NOT IMPLEMENTED YET | ||
| 1371 | # | ||
| 1372 | #The flow query is a language very simmilar to SQL to query data on | ||
| 1373 | #nfdump files. However, the flow query has nothing to do with SQL. It uses | ||
| 1374 | #only similar command syntax. Example of flow query | ||
| 1375 | # | ||
| 1376 | # SELECT * FROM data/nfdump1.nfcap, data2/nfdump2.nfcap | ||
| 1377 | # WHERE src host 147.229.3.10 | ||
| 1378 | # TIME WINDOW BETWEEN '2012-06-03' AND '202-06-04' | ||
| 1379 | # ORDER BY bytes | ||
| 1380 | # LIMIT 100 | ||
| 1381 | # | ||
| 1382 | # | ||
| 1383 | # INSERT INTO data/nout_nfdump.nfcap (srcip, dstip, srcport, dstport) | ||
| 1384 | # | ||
| 1385 | # | ||
| 1386 | |||
| 1387 | =pod | ||
| 1388 | |||
| 1389 | =head1 NOTE ABOUT 32BIT PLATFORMS | ||
| 1390 | |||
| 1391 | Nfdump primary uses 64 bit counters and other items to store single integer value. However, | ||
| 1392 | the native 64 bit support is not compiled in every perl. For those cases where | ||
| 1393 | only 32 integer values are supported, the C<Net::NfDump> uses C<Math::Int64> module. | ||
| 1394 | |||
| 1395 | The build scripts detect the platform automatically and C<math::Int64> module is required | ||
| 1396 | only on platforms where an available perl does not support 64bit integer values. | ||
| 1397 | |||
| 1398 | =back | ||
| 1399 | |||
| 1400 | |||
| 1401 | =head1 EXAMPLES OF USE | ||
| 1402 | |||
| 1403 | There are several examples in the C<examples> and C<bin> directory. | ||
| 1404 | |||
| 1405 | |||
| 1406 | C<nfasnupd> - Is script for updating the information | ||
| 1407 | about AS numbers and country codes based on BGP and geolocation database. Every flow | ||
| 1408 | can be extended with src/dst AS number and alco can be | ||
| 1409 | extended with src/dst country code. | ||
| 1410 | |||
| 1411 | The C<nfasnupd> periodically checks and downloads the BGP database | ||
| 1412 | which is available as part of libn.net project. After that it updates | ||
| 1413 | the AS (or country code) information in the nfdump file. It can be | ||
| 1414 | run as the extra command (-x option of nfcapd) to update | ||
| 1415 | information when the new file is available. | ||
| 1416 | |||
| 1417 | The information about src/dst country works in a similar way. It uses maxmind database | ||
| 1418 | and C<Geo::IP> module. However, nfdump does not support any field to store such kind of | ||
| 1419 | information; the xsrcport and xdstport fields are used instead. The country code is | ||
| 1420 | converted into 16 bit information (8 bits for the first character of a country code and | ||
| 1421 | another 8 bits for the second one). | ||
| 1422 | |||
| 1423 | =back | ||
| 1424 | |||
| 1425 | |||
| 1426 | =head1 SEE ALSO | ||
| 1427 | |||
| 1428 | nfdump project - https://github.com/phaag/nfdump | ||
| 1429 | libnf C interface http://libnf.net/ | ||
| 1430 | |||
| 1431 | =head1 AUTHOR | ||
| 1432 | |||
| 1433 | Tomas Podermanski, E<lt>tpoder@vut.czE<gt>, Brno University of Technology | ||
| 1434 | NetX Networks a.s., E<lt>info@netx.as<gt> | ||
| 1435 | |||
| 1436 | =head1 COPYRIGHT AND LICENCE | ||
| 1437 | |||
| 1438 | Copyright (C) 2012 - 2019 by Brno University of Technology | ||
| 1439 | Copyright (C) 2020 by NetX Networks a.s. | ||
| 1440 | |||
| 1441 | This library is free software; you can redistribute it and modify | ||
| 1442 | it under the same terms as Perl itself. | ||
| 1443 | |||
| 1444 | If you are satisfied with using C<Net::NfDump>, please, send us a postcard, preferably with a picture of your location / city to: | ||
| 1445 | |||
| 1446 | Brno University of Technology | ||
| 1447 | CVIS | ||
| 1448 | Tomas Podermanski | ||
| 1449 | Antoninska 1 | ||
| 1450 | 601 90 | ||
| 1451 | Czech Republic | ||
| 1452 | |||
| 1453 | =cut | ||
| 1454 | |||
| 1455 | 1; | ||
| 1456 | |||
| 1457 | __END__ |