PcapWriter.pm

filter: Statementstmt 90.7% 49 / 54 Branchbran 50.0% 5 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 14 / 14 Podpod 85.7% 6 / 7 totaltotal 85.2% 75 / 88 CC 7 SCAR 8.4
CC
7
Cov
83%
reopen
5.3
packet
3.3
layer2prefix
2.0
CRAP 2.3
« PcapWriter/ICMP_Echo.pm ↑ Summary PcapWriter/UDP.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Net::PcapWriter;
4 7 ▶use Time::HiRes 'gettimeofday';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use Net::PcapWriter::TCP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Net::PcapWriter::UDP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Net::PcapWriter::ICMP_Echo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.725';
10  
11 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
12 6 my ($class,$file) = @_;
13 6 my $self = bless { fh => undef },$class;
14 6 $self->reopen($file);
15 6 return $self;
16 }
17  
18 6 ▶sub reopen {
Subroutine reopen: called CC=4 SCAR=16.8
Pod documented
19 6 my ($self,$file) = @_;
20 6 my $fh;
21 6 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 6 0 6
22 6 ▶ if ( ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 6 0 6
23 6 $fh = $file
24 } else {
25 0 ▶ open($fh,'>',$file) or die "open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $file or die "open ${file}: $!" 0 0 0
26 0 binmode($fh);
27 }
28 } else {
29 0 $fh = \*STDOUT;
30 }
31 6 $self->{fh} = $fh;
32 6 $self->_header;
33 }
34  
35 # write pcap header
36 6 ▶sub _header {
Subroutine _header: called CC=1 SCAR=0.0
37 6 my $self = shift;
38  
39 # struct pcap_file_header {
40 # bpf_u_int32 magic;
41 # u_short version_major;
42 # u_short version_minor;
43 # bpf_int32 thiszone; /* gmt to local correction */
44 # bpf_u_int32 sigfigs; /* accuracy of timestamps */
45 # bpf_u_int32 snaplen; /* max length saved portion of each pkt */
46 # bpf_u_int32 linktype; /* data link type (LINKTYPE_*) */
47 # };
48  
49 6 print {$self->{fh}} pack('LSSlLLL',
50 0xa1b2c3d4, # magic
51 2,4, # major, minor
52 0,0, # timestamps correction and accuracy
53 0xffff, # snaplen
54 1, # DLT_EN10MB
55 );
56 }
57  
58 6 ▶sub layer2prefix {
Subroutine layer2prefix: called CC=2 SCAR=6.9
Pod undocumented
59 6 my $ip = pop;
60 6 ▶ return pack("NnNnn",
Branch: 1 branch
BranchTrueFalseTotal
$ip =~ /:/ ? : 3 3 6
61 0,1,0,1, # all macs 0:*
62 $ip =~m{:} ? 0x86dd: 0x0800, # ETH_TYPE_IP6 | ETH_TYPE_IP
63 );
64 }
65  
66 # write pcap packet
67 26 ▶sub packet {
Subroutine packet: called CC=3 SCAR=11.8
Pod documented
68 26 my ($self,$data,$timestamp) = @_;
69 26 ▶ $timestamp ||= [ gettimeofday() ];
Condition50%1/2
$timestamp ||= [&gettimeofday()]
l!l
026
MC/DC0%0/1
$timestamp ||= [&gettimeofday()]
$timestamp
Truth table50%1/2
A
A= $timestamp
Aresult
11
00
70  
71 # struct pcap_pkthdr {
72 # struct timeval ts; /* time stamp */
73 # bpf_u_int32 caplen; /* length of portion present */
74 # bpf_u_int32 len; /* length this packet (off wire) */
75 # };
76  
77 26 my ($tsec,$tmsec);
78 26 ▶ if (ref($timestamp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $timestamp) { } 26 0 26
79 # array like in Time::HiRes
80 26 ($tsec,$tmsec) = @$timestamp;
81 } else {
82 0 $tsec = int($timestamp);
83 0 $tmsec = int(($timestamp - $tsec)*1_000_000);
84 }
85  
86 26 print {$self->{fh}} pack('LLLLa*',
87 $tsec,$tmsec, # struct timeval ts
88 length($data), # caplen
89 length($data), # len
90 $data, # data
91 );
92 }
93  
94  
95 # return new TCP connection object
96 2 ▶sub tcp_conn {
Subroutine tcp_conn: called CC=1 SCAR=0.0
Pod documented
97 2 my ($self,$src,$sport,$dst,$dport) = @_;
98 2 return Net::PcapWriter::TCP->new($self,$src,$sport,$dst,$dport);
99 }
100  
101 # return new UDP connection object
102 2 ▶sub udp_conn {
Subroutine udp_conn: called CC=1 SCAR=0.0
Pod documented
103 2 my ($self,$src,$sport,$dst,$dport) = @_;
104 2 return Net::PcapWriter::UDP->new($self,$src,$sport,$dst,$dport);
105 }
106  
107 # return new ICMP_Echo "connection" object
108 2 ▶sub icmp_echo_conn {
Subroutine icmp_echo_conn: called CC=1 SCAR=0.0
Pod documented
109 2 my ($self,$src,$dst,$id) = @_;
110 2 return Net::PcapWriter::ICMP_Echo->new($self,$src,$dst,$id);
111 }
112  
113 7 1;
114  
115 __END__
« PcapWriter/ICMP_Echo.pm ↑ Summary PcapWriter/UDP.pm »