blib/lib/Device/Router/RTX.pm

filter: Statementstmt 13.4% 29 / 216 Branchbran 0.0% 0 / 98 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 25.0% 5 / 20 Podpod 100% 8 / 8 totaltotal 12.1% 42 / 347 CC 53 SCAR 48.4
CC
53
Cov
9%
read_config
380.0
new
72.0
get_config
72.0
CRAP 126.2
↑ Summary
1 package Device::Router::RTX;
2 1 require Exporter;
3 1 @ISA = qw(Exporter);
4 1 @EXPORT_OK = qw//;
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '0.06';
8  
9 1 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # DEPENDS
11 1 ▶use Net::Telnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::TFTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # use Net::IP;
14 # END DEPENDS
15  
16 sub _read_config
17 0 ▶{
Subroutine _read_config: not called CC=7 SCAR=40.3
18 0 my ($configfile, $args) = @_;
19 0 ▶ if (!-f $configfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $configfile) 0 0 0
20 0 print <<EOF;
21 I was told that there was a configuration file in '$configfile' but
22 I can't seem to find it.
23 EOF
24 0 return;
25 }
26 0 ▶ open my $input, "<", $configfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<", $configfile or die $! 0 0 0
27 0 ▶ while (my $line = <$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input)) 0 0 0
28 0 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
29 0 ▶ if ($line =~ /^(\w+):\s*(.*?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\w+):\s*(.*?)\s*$/) { } 0 0 0
30 0 my $parameter = $1;
31 0 $parameter = lc $parameter;
32 0 my $value = $2;
33 0 $args->{$parameter} = $value;
34 } else {
35 0 die "$configfile:$.: parse error at '$line'";
36 }
37 }
38 0 ▶ close $input or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $input or die $! 0 0 0
39 }
40  
41 sub new
42 0 ▶{
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
43 0 my ($class, %args) = @_;
44 0 my $rtx = {};
45 0 for (keys %args) {
46 0 $_ = lc $_;
47 }
48 0 ▶ if ($args{config}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"config"}) 0 0 0
49 0 _read_config ($args{config}, \%args);
50 }
51 0 ▶ if (!$args{store}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $args{'store'}) { } 0 0 0
elsif (not -d $args{'store'}) { } 0 0 0
52 # carp "No file store specified";
53 }
54 elsif (! -d $args{store}) {
55 # carp "File store '$args{store}' is not a valid directory";
56 }
57 else {
58 0 $rtx->{store} = $args{store};
59 }
60 0 ▶ if (!$args{address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'address'}) { } 0 0 0
61 0 croak "No internet protocol address for router";
62 }
63 else {
64 0 $rtx->{address} = $args{address};
65 }
66 0 ▶ if (defined($args{password})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'password'}) { } 0 0 0
67 0 $rtx->{password} = $args{password};
68 }
69 else {
70 0 $rtx->{password} = "";
71 }
72 0 ▶ if (defined($args{admin_password})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'admin_password'}) { } 0 0 0
73 0 $rtx->{admin_password} = $args{admin_password};
74 }
75 else {
76 0 $rtx->{admin_password} = "";
77 }
78 0 $rtx->{verbose} = $args{verbose};
79 0 bless $rtx;
80 0 return $rtx;
81 }
82  
83 sub _check
84 0 ▶{
Subroutine _check: not called CC=4 SCAR=30.0
85 0 my ($rtx) = @_;
86 0 ▶ if (!$rtx || ref $rtx ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rtx or ref $rtx ne __PACKAGE__) 0 0 0
Condition0%0/3
not $rtx or ref $rtx ne __PACKAGE__
l!l&&r!l&&!r
000
MC/DC0%0/2
not $rtx or ref $rtx ne __PACKAGE__
not $rtx ref $rtx ne __PACKAGE__
Truth table0%0/3
not(A) or B
A= not $rtxB= ref $rtx ne __PACKAGE__
ABresult
1X1
011
000
87 0 die "Bad object passed";
88 }
89 0 ▶ if (!$rtx->{address}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rtx->{"address"}) 0 0 0
90 0 die "No internet protocol address for router in object";
91 }
92 }
93  
94 sub connect
95 0 ▶{
Subroutine connect: not called CC=4 SCAR=30.0
Pod documented
96 0 my ($rtx) = @_;
97 0 _check ($rtx);
98 # my $telnet_connection = new Net::Telnet (Dump_Log => "stuff.txt");
99 0 my $telnet_connection = new Net::Telnet();
100 # $telnet_connection->option_log (*STDERR);
101 # See documentation for Net::Telnet
102 0 $telnet_connection->open ($rtx->{address});
103 0 ▶ if ($rtx->{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtx->{"password"}) 0 0 0
104 # $telnet_connection->print ();
105 0 my $stuff = $telnet_connection->get ();
106 # print "stuff is: $stuff\n";
107 0 ▶ if ($stuff =~ m'Error: Other user logged in by telnet.') {
Branch: 1 branch
BranchTrueFalseTotal
if ($stuff =~ /Error: Other user logged in by telnet./) { } 0 0 0
108 0 die "Someone else is already logged in.\n";
109 }
110 else {
111 0 $telnet_connection->print ($rtx->{password});
112 }
113 0 ▶ if ($telnet_connection->eof()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($telnet_connection->eof) 0 0 0
114 0 die "Telnet connection cut off for some reason.\n";
115 }
116 0 my $response = $telnet_connection->get();
117 # print "Response is: $stuff\n";
118 }
119 0 $rtx->{telnet_connection} = $telnet_connection;
120 }
121  
122 1 my $_config_desc = "RTX1000 configuration file";
123  
124 sub get_config
125 0 ▶{
Subroutine get_config: not called CC=8 SCAR=42.8
Pod documented
126 0 my ($rtx, $filename) = @_;
127 0 ▶ $filename = "config" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
128 0 _check ($rtx);
129 0 my $tftp = Net::TFTP->new ($rtx->{address});
130 0 $tftp->ascii;
131 0 ▶ if (-f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 0 0
132 0 warn "$_config_desc '$filename' already exists.\n";
133 0 return;
134 }
135 0 my $remotefile = "config";
136 0 ▶ if ($rtx->{admin_password}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rtx->{'admin_password'}) { } 0 0 0
elsif ($rtx->{'password'}) { } 0 0 0
137 0 $remotefile .= "/$rtx->{admin_password}";
138 }
139 elsif ($rtx->{password}) {
140 0 $remotefile .= "/$rtx->{password}";
141 0 carp "Admin password is not set";
142 }
143 else {
144 0 carp "Neither admin nor user passwords are set";
145 }
146 0 ▶ if ($rtx->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rtx->{"verbose"}) 0 0 0
147 0 print "Getting $remotefile\n";
148 }
149 0 $tftp->get ($remotefile, $filename);
150 0 ▶ if ($tftp->error ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tftp->error) 0 0 0
151 0 die "tftp get '$remotefile failed': ",$tftp->error ();
152 }
153 0 ▶ die "TFTP failed" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
154 # open my $input, "<", $filename or die $!;
155 # while (<$input>) { print }
156 # close $input or die $!;
157 }
158  
159 1 my $ip_address = qr/(?:(?:\d+\.){3}(?:\d+))/;
160 # The following does not contain the full possibilities with "except" etc.
161 1 my $ip_range_re = qr:(($ip_address)-($ip_address)/(\d+)):;
162 1 my $mac_re = qr/(?:[0-9a-f]{2}:){5}[0-9a-f]{2}/;
163 # Reference: Cmdref.pdf page 211
164 1 my $protocol_re = qr/(?:esp|tcp|udp|icmp|\d+)/;
165 # Reference: Cmdref.pdf page 56
166 1 my %aliases = (qw/
167 ftp 20&21
168 ftpdata 20
169 telnet 23
170 smtp 25
171 domain 53
172 gopher 70
173 finger 73
174 www 80
175 pop3 110
176 sunrpc 111
177 ident 113
178 ntp 123
179 nntp 119
180 snmp 161
181 syslog 514
182 printer 515
183 talk 517
184 route 520
185 uucp 540/
186 );
187 1 my @alias_keys = sort {length $b <=> length $a} keys %aliases;
188 1 my $port_re = '(?:'.join ('|', @alias_keys).'|\\d+)';
189  
190 sub _add_mac
191 0 ▶{
Subroutine _add_mac: not called CC=1 SCAR=6.9
192 0 my ($config, $mac, $what) = @_;
193 0 $config->{mac}->{$mac} = $what;
194 0 $config->{mac_map}->{$what} = $mac;
195 }
196  
197 sub _check_range
198 0 ▶{
Subroutine _check_range: not called CC=1 SCAR=6.9
199 0 my ($range, $ip) = @_;
200 }
201  
202 sub read_config
203 0 ▶{
Subroutine read_config: not called CC=19 SCAR=59.4
Pod documented
204 0 my ($rtx, $filename) = @_;
205 0 ▶ $filename = "config" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
206 0 ▶ die "Cannot find $_config_desc '$filename'" unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
207 0 ▶ open my $input, "<", $filename or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<", $filename or die $! 0 0 0
208 0 my $config = {};
209 # Default value is to disallow tftp.
210 # Reference: Cmdrefs.pdf, page 57
211 0 $config->{tftp_host} = "none";
212 0 ▶ while (my $line = <$input>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $input)) 0 0 0
213 0 ▶ next if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
214 0 ▶ if ($line =~ /#\s+RTX1000\s+Rev\.([\d.]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /#\s+RTX1000\s+Rev\.([\d.]+)/) 0 0 0
215 0 $config->{firmware} = $1;
216 0 next;
217 }
218 0 ▶ if ($line =~ /mac address\s*:\s*($mac_re)\s*,\s*($mac_re)\s*,\s*($mac_re)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /mac address\s*:\s*($mac_re)\s*,\s*($mac_re)\s*,\s*($mac_re)/i) 0 0 0
219 0 my @macs = ($1, $2, $3);
220 0 _add_mac ($config, $macs[0], "lan1");
221 0 _add_mac ($config, $macs[1], "lan2");
222 0 _add_mac ($config, $macs[2], "lan3");
223 0 next;
224 }
225 0 ▶ if ($line =~ /tftp\s+host\s+($ip_address|any|none)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /tftp\s+host\s+($ip_address|any|none)/) 0 0 0
226 0 $config->{tftp_host} = $1;
227 0 next;
228 }
229 0 ▶ if ($line =~ /no\s+tftp\s+host/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /no\s+tftp\s+host/) 0 0 0
230 0 $config->{tftp_host} = "none";
231 0 next;
232 }
233 # Login password can be printable ascii characters
234 # Reference: Cmdref.pdf, page 43.
235 # Reference does not mention whether the space character is OK
236 # or not. Here I have assumed "not".
237 0 ▶ if ($line =~ /(administrator|login)\s+password\s+([[:graph:]]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /(administrator|login)\s+password\s+([[:graph:]]+)/) 0 0 0
238 0 $config->{$1."_password"} = $2;
239 0 next;
240 }
241 0 ▶ if ($line =~ m:ip\s+lan(\d)\s+address\s+($ip_address)/(\d+):) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[ip\s+lan(\d)\s+address\s+($ip_address)/(\d+)]) 0 0 0
242 0 my $lan = "lan$1";
243 0 $config->{$lan}->{address} = $2;
244 0 $config->{$lan}->{mask} = $3;
245 0 next;
246 }
247 0 ▶ if ($line =~ /nat\s+descriptor\s+type\s+(\d+)\s+(masquerade)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /nat\s+descriptor\s+type\s+(\d+)\s+(masquerade)/) 0 0 0
248 0 my $nat_descriptor = $1;
249 0 my $type = $2;
250 0 $config->{nat}->{$nat_descriptor}->{type} = $type;
251 0 next;
252 }
253 # Reference: Cmdref.pdf page 211
254 0 ▶ if ($line =~ /nat\s+descriptor\s+masquerade\s+(static)\s+(\d+)\s+(\d+)\s+($ip_address)\s+($protocol_re)\s+(?:($port_re)=)?($port_re)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /nat\s+descriptor\s+masquerade\s+(static)\s+(\d+)\s+(\d+)\s+($ip_address)\s+($protocol_re)\s+(?:($port_re)=)?($port_re)/) 0 0 0
255 0 my $nat_descriptor = $2;
256 0 my $id = $3;
257 0 my $ip = $4;
258 0 my $protocol = $5;
259 0 my $outer_port = $6;
260 0 my $inner_port = $7;
261 0 $config->{nat}->{$nat_descriptor}->{$id} =
262 {
263 id => $id,
264 ip => $ip,
265 protocol => $protocol,
266 outer_port => $outer_port,
267 inner_port => $inner_port
268 };
269 0 next;
270 }
271 0 ▶ if ($line =~ /dhcp\s+scope\s+(\d+)\s+$ip_range_re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /dhcp\s+scope\s+(\d+)\s+$ip_range_re/) 0 0 0
272 0 my $scope = $1;
273 0 my $range = $2;
274 0 $config->{dhcp}->{$scope}->{range} = $2;
275 0 next;
276 }
277 0 ▶ if ($line =~ /dhcp\s+scope\s+bind\s+(\d+)\s+($ip_address)\s+(ethernet\s+)?($mac_re)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /dhcp\s+scope\s+bind\s+(\d+)\s+($ip_address)\s+(ethernet\s+)?($mac_re)/i) 0 0 0
278 0 my $scope = $1;
279 0 my $scope_hash = $config->{dhcp}->{$scope};
280 0 ▶ if (!$scope_hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $scope_hash) { } 0 0 0
281 0 print "Warning: unknown scope $scope\n";
282 }
283 else {
284 0 my $ip = $2;
285 0 _check_range ($scope_hash->{range}, $ip);
286 0 my $mac = $4;
287 0 _add_mac ($config, $mac, $ip);
288 # Add the data to scope_hash
289 0 $scope_hash->{$ip} = {mac => $mac};
290 }
291 0 next;
292 }
293 0 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 0 0
294 0 $line =~ s/\s+$//;
295 0 print "Unrecognized line '$line'\n";
296 }
297 0 ▶ close $input or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $input or die $! 0 0 0
298 0 $rtx->{config} = $config;
299 }
300  
301 # Do one command
302  
303 sub _command
304 0 ▶{
Subroutine _command: not called CC=3 SCAR=24.8
305 0 my ($rtx, $command) = @_;
306 0 _check ($rtx);
307 0 ▶ $rtx->connect() unless $rtx->{telnet_connection};
Branch: 1 branch
BranchTrueFalseTotal
unless $rtx->{"telnet_connection"} 0 0 0
308 0 $rtx->_admin_login();
309 0 my @lines = $rtx->{telnet_connection}->cmd ("$command\n");
310 0 my $retval = join ("", @lines);
311 0 ▶ die "Error doing '$command': $retval" if $retval =~ /Error:/;
Branch: 1 branch
BranchTrueFalseTotal
if $retval =~ /Error:/ 0 0 0
312 0 return @lines;
313 }
314  
315 # Save the configuration to permanent memory
316  
317 sub save
318 0 ▶{
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
319 0 my ($rtx) = @_;
320 0 _check ($rtx);
321 0 my @lines = $rtx->_command ("save\n");
322 0 my $retval = join ("", @lines);
323 0 ▶ die "Save failed: $retval" unless $retval =~ /Saving.*Done/;
Branch: 1 branch
BranchTrueFalseTotal
unless $retval =~ /Saving.*Done/ 0 0 0
324 }
325  
326 sub _admin_login
327 0 ▶{
Subroutine _admin_login: not called CC=2 SCAR=17.9
328 0 my ($rtx) = @_;
329 0 _check ($rtx);
330 0 ▶ return if $rtx->{admin};
Branch: 1 branch
BranchTrueFalseTotal
if $rtx->{"admin"} 0 0 0
331 0 my $admin_login_cmd = "administrator\n$rtx->{admin_password}\n";
332 0 my @reply = $rtx->{telnet_connection}->cmd($admin_login_cmd);
333 0 $rtx->{admin} = 1;
334 }
335  
336 sub command
337 0 ▶{
Subroutine command: not called CC=1 SCAR=6.9
Pod documented
338 0 my ($rtx, $command) = @_;
339 0 _check ($rtx);
340 0 $rtx->_command ("$command\n");
341 0 $rtx->save ("$command\n");
342 }
343  
344 sub _check_mac
345 0 ▶{
Subroutine _check_mac: not called CC=2 SCAR=17.9
346 0 my ($mac) = @_;
347 0 ▶ die "Bad MAC address '$mac'" unless $mac =~ /$mac_re/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $mac =~ /$mac_re/i 0 0 0
348 }
349  
350 sub wake
351 0 ▶{
Subroutine wake: not called CC=1 SCAR=6.9
Pod documented
352 0 my ($rtx, $lan, $mac) = @_;
353 0 _check ($rtx);
354 0 _check_mac ($mac);
355 # Reference: Cmdrefs.pdf, page 305
356 0 my @output = $rtx->_command ("wol send lan$lan $mac");
357 0 print "@output\n";
358 }
359  
360 sub arp
361 0 ▶{
Subroutine arp: not called CC=4 SCAR=30.0
Pod documented
362 0 my ($rtx) = @_;
363 0 _check ($rtx);
364 0 my @output = $rtx->_command("show arp");
365 0 my @arp;
366 0 for my $line (@output) {
367 0 ▶ if ($line =~ /LAN(\d)\s+($ip_address)\s+($mac_re)\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /LAN(\d)\s+($ip_address)\s+($mac_re)\s+(\d+)/) 0 0 0
368 0 my %arp_data;
369 0 $arp_data{lan} = $1;
370 0 $arp_data{ip} = $2;
371 0 $arp_data{mac} = $3;
372 0 $arp_data{ttl} = $4;
373 0 push @arp, \%arp_data;
374 }
375 }
376 0 ▶ if (@arp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arp) 0 0 0
377 0 return \@arp;
378 }
379 0 return;
380 }
381  
382 1 1;
↑ Summary