Dump/Writer.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
« Dump/Online2.pm ↑ Summary
1 #
2 # $Id: Writer.pm,v a93481b098ec 2020/05/01 08:55:56 gomor $
3 #
4 package Net::Frame::Dump::Writer;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use base qw(Net::Frame::Dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 our @AS = qw(
10 append
11 _fd
12 );
13 __PACKAGE__->cgBuildIndices;
14 __PACKAGE__->cgBuildAccessorsScalar(\@AS);
15  
16 use Net::Frame::Dump qw(:consts);
17  
18 sub new {
19 my $self = shift->SUPER::new(
20 firstLayer => 'RAW',
21 append => 0,
22 overwrite => 0,
23 @_,
24 );
25  
26 return $self;
27 }
28  
29 my $mapLinks = {
30 'NULL' => NF_DUMP_LAYER_NULL(),
31 'ETH' => NF_DUMP_LAYER_ETH(),
32 'PPP' => NF_DUMP_LAYER_PPP(),
33 'RAW' => NF_DUMP_LAYER_RAW(),
34 '80211' => NF_DUMP_LAYER_80211(),
35 'SLL' => NF_DUMP_LAYER_SLL(),
36 '80211::Radiotap' => NF_DUMP_LAYER_80211_RADIOTAP(),
37 'ERF' => NF_DUMP_LAYER_ERF(),
38 };
39  
40 sub _getPcapHeader {
41 my $self = shift;
42  
43 my $dlt = $mapLinks->{$self->firstLayer} or do {
44 print("[-] ".__PACKAGE__.": Can't get pcap header information for ".
45 "this layer type\n");
46 return;
47 };
48  
49 # http://wiki.wireshark.org/Development/LibpcapFileFormat
50 return CORE::pack('VvvVVVV',
51 0xa1b2c3d4, # magic number
52 2, # major version number
53 4, # minor version number
54 0, # GMT to local correction
55 0, # accuracy of timestamps
56 1500, # max length of captured packets, in octets
57 $dlt, # data link type
58 );
59 }
60  
61 sub _openFile {
62 my $self = shift;
63  
64 my $file = $self->file;
65 if (-f $self->file && $self->append) {
66 open(my $fd, '>>', $file) or do {
67 print("[-] ".__PACKAGE__.": open[append]: $file: $!\n");
68 return;
69 };
70 $self->_fd($fd);
71 }
72 elsif (!-f $self->file || $self->overwrite) {
73 my $hdr = $self->_getPcapHeader;
74 open(my $fd, '>', $file) or do {
75 print("[-] ".__PACKAGE__.": open[overwrite]: $file: $!\n");
76 return;
77 };
78 my $r = syswrite($fd, $hdr, length($hdr));
79 if (!defined($r)) {
80 print("[-] ".__PACKAGE__.": syswrite: $file: $!\n");
81 return;
82 }
83 $self->_fd($fd);
84 }
85  
86 return 1;
87 }
88  
89 sub start {
90 my $self = shift;
91  
92 $self->isRunning(1);
93  
94 if (-f $self->file && !$self->overwrite && !$self->append) {
95 print("[-] ".__PACKAGE__.": We will not overwrite a file by default. ".
96 "Use `overwrite' attribute to do it or use `append' mode\n");
97 return;
98 }
99  
100 $self->_openFile;
101  
102 return 1;
103 }
104  
105 sub stop {
106 my $self = shift;
107  
108 if (!$self->isRunning) {
109 return;
110 }
111  
112 if (defined($self->_fd)) {
113 close($self->_fd);
114 $self->_fd(undef);
115 }
116  
117 $self->isRunning(0);
118  
119 return 1;
120 }
121  
122 sub write {
123 my $self = shift;
124 my ($h) = @_;
125  
126 if (!defined($self->_fd)) {
127 print("[-] ".__PACKAGE__.": file @{[$self->file]} not open for ".
128 "writing\n");
129 return;
130 }
131  
132 my $raw = $h->{raw};
133 my $ts = $h->{timestamp};
134 my $len = length($raw);
135  
136 # Create record header
137 my ($sec, $usec) = split('\.', $ts);
138 my $recHdr = CORE::pack('VVVV',
139 $sec,
140 $usec,
141 $len,
142 $len,
143 );
144 my $r = syswrite($self->_fd, $recHdr.$raw, length($recHdr.$raw));
145 if (!defined($r)) {
146 print("[-] ".__PACKAGE__.": syswrite: @{[$self->file]}: $!\n");
147 return;
148 }
149  
150 return $r;
151 }
152  
153 1;
154  
155 __END__
« Dump/Online2.pm ↑ Summary