-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrfc3442.route-4-dhcp.pl
executable file
·79 lines (60 loc) · 2.21 KB
/
rfc3442.route-4-dhcp.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use diagnostics;
use v5.10.0;
my ($net, $gw, $aggregate) = ("", "", "");
my $re_ip = "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
foreach (@ARGV) {
unless ($net) { $net = $_; next; }
unless ($gw) {
$gw = $_;
my ($network, $subnetmask) = split("/", $net);
if (($subnetmask>=0 && $subnetmask<=32) && $network =~ /^$re_ip$/ && $gw =~/^$re_ip$/ ) {
my (@destination) = split( /\./, $network );
my $destination = "";
my (@router) = split( /\./, $gw );
my $router = "";
my $networklen = to_hex($subnetmask);
my $significantoctets = 0;
$significantoctets = 1 if $subnetmask>=1 && $subnetmask<=8;
$significantoctets = 2 if $subnetmask>=9 && $subnetmask<=16;
$significantoctets = 3 if $subnetmask>=17 && $subnetmask<=24;
$significantoctets = 4 if $subnetmask>=25 && $subnetmask<=32;
if ($significantoctets>0) {
foreach my $index (1..$significantoctets) {
$destination .= to_hex($destination[$index-1]);
}
} else {
$destination .= "";
}
foreach my $r (@router) {
$router .= to_hex($r);
}
$aggregate .= sprintf("%s%s%s", $networklen, $destination, $router);
printf(
"option_121_route_%s_via_%s : 0x%s%s%s\n",
$net, $gw, $networklen, $destination, $router
);
printf(
"option_249_route_%s_via_%s : 0x%s%s%s\n",
$net, $gw, $networklen, $destination, $router
);
} else {
print STDERR sprintf("Mask %d, network %s or gateway %s error\n");
}
}
$net = "";
$gw = "";
}
if ($aggregate) {
printf("aggregate_opt_121 : 0x%s\n", $aggregate);
printf("aggregate_opt_249 : 0x%s\n", $aggregate);
}
sub to_hex {
my $n = shift;
return "00" unless $n;
$n = sprintf("%x", $n);
($n) = "0$n" =~ m/.*(..)$/;
return $n;
}