script/nf-cat.pl untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 182 CC 1 SCAR 6.9
CC
1
Cov
0%
END
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 #
3 # $Id: nf-cat.pl 349 2015-01-23 06:44:44Z gomor $
4 #
5 use strict;
6 use warnings;
7  
8 our $VERSION = '1.00';
9  
10 use Getopt::Std;
11 my %opts;
12 getopts('R234i:d:rL:v', \%opts);
13  
14 my $oDump;
15 my $oWrite;
16  
17 die("Usage: $0 [parameters]\n".
18 "\n".
19 " o Choose either one of these parameters:\n".
20 " -2 send at layer 2\n".
21 " -3 send at layer 3\n".
22 " -4 send at layer 4\n".
23 "\n".
24 " o Layer 2 specific parameters:\n".
25 " -i network interface to use\n".
26 "\n".
27 " o Layer 3 and layer 4 specific parameters:\n".
28 " -d target destination IP address\n".
29 "\n".
30 " o Common parameters:\n".
31 " -R send as a raw string\n".
32 " -r get and print frame reply\n".
33 " -L first layer contained in raw data (example: IPv4)\n".
34 " -v be more verbose\n".
35 ""
36 ) unless ($opts{2} || $opts{3} || $opts{4});
37  
38 use Net::Frame::Simple;
39 use Net::Frame::Dump::Online;
40 use Net::Frame::Device;
41  
42 my $oDevice = Net::Frame::Device->new;
43  
44 my $data;
45 while (<>) {
46 chomp;
47 $data = $_;
48 }
49  
50 # Try to guess first layer when -3 used
51 my $firstLayer = $opts{L};
52 if (! $opts{R} && $opts{3} && ! $firstLayer) {
53 if ($data =~ /^4/) {
54 $firstLayer = 'IPv4';
55 }
56 else {
57 die("Unable to guess first layer type, you should specify -L\n");
58 }
59 }
60  
61 # If -2 used, and firstLayer not specified, and no -R, we don't know
62 if (! $opts{R} && $opts{2} && ! $firstLayer) {
63 die("Unable to guess first layer type, you should specify -L\n");
64 }
65  
66 # Reassemble frame
67 my $oSimple;
68 if (! $opts{R}) {
69 $oSimple = Net::Frame::Simple->new(
70 raw => pack('H*', $data),
71 firstLayer => $firstLayer,
72 );
73 }
74  
75 if ($oSimple && $opts{v}) {
76 print $oSimple->print."\n";
77 }
78  
79 # Try to guess destination
80 my $dst = $opts{d};
81 if (! $opts{2} && $oSimple) {
82 if (! $dst) {
83 if (my $l = $oSimple->ref->{IPv4}) {
84 $dst = $l->dst;
85 }
86 else {
87 die("Unable to guess destination IP address, you should specify -d\n");
88 }
89 }
90 }
91  
92 # Try to guess network interface
93 my $int = $opts{i};
94 if (! $int) {
95 if ($dst) {
96 $oDevice->updateFromTarget($dst);
97 $int = $oDevice->dev;
98 }
99 else {
100 die("Unable to guess network interface, you should specify -i\n");
101 }
102 }
103  
104 if ($opts{2}) {
105 use Net::Write::Layer2;
106 $oWrite = Net::Write::Layer2->new(dev => $int);
107 }
108 elsif ($opts{3}) {
109 use Net::Write::Layer3;
110 $oWrite = Net::Write::Layer3->new(dst => $dst);
111 }
112 elsif ($opts{4}) {
113 use Net::Write::Layer4;
114 $oWrite = Net::Write::Layer4->new(dst => $dst);
115 }
116  
117 if ($opts{r}) {
118 $oDump = Net::Frame::Dump::Online->new(dev => $int);
119 $oDump->start;
120 }
121  
122 $oWrite->open;
123 $oWrite->send(pack('H*', $data));
124 $oWrite->close;
125  
126 if ($opts{r} && $oSimple) {
127 until ($oDump->timeout) {
128 if (my $reply = $oSimple->recv($oDump)) {
129 print $reply->print."\n";
130 last;
131 }
132 }
133 }
134  
135 END {
136 $oWrite && $oWrite->close;
137 $oDump && $oDump->isRunning && $oDump->stop;
138 }
139  
140 __END__