-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw-subnets
executable file
·459 lines (348 loc) · 12.5 KB
/
draw-subnets
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to draw subnet pngs
#
use strict;
use HOSTDB;
use GD;
use Getopt::Std;
use vars qw ($opt_d $opt_o);
getopts ('do:');
my $slash24_height = 32;
my $pixels_per_ip = 4;
my $x_pixels_per_char = 6;
my $y_pixels_per_char = 8;
my $hostdbini = Config::IniFiles->new (-file => HOSTDB::get_inifile ());
die ("$0: Config file access problem.\n") unless ($hostdbini);
my $debug = defined ($opt_d);
my $output_dir = $opt_o || $hostdbini->val ('subnet', 'output_dir');
if ($output_dir) {
die("$0: Specified output dir '$output_dir' is not a valid directory\n") if (! -d $output_dir);
}
my $showsubnet_path = $hostdbini->val ('subnet', 'showsubnet_uri');
my $whois_path = $hostdbini->val ('subnet', 'whois_uri');
die ("$0: Could not find path to showsubnet cgi-script.\n" .
"You should probably specify 'showsubnet_uri'\n" .
"in the 'subnet' section of '" . HOSTDB::get_inifile () . "'\n") if (! $showsubnet_path);
my $hostdb = HOSTDB::DB->new (ini => $hostdbini, debug => $debug);
my @drawsubnets = @ARGV;
if ($#drawsubnets == -1) {
my $in = $hostdbini->val ('subnet', 'draw_subnet_list');
$in =~ s/,/ /go; # get rid of comas
$in =~ s/\s+/ /go; # single spaces
@drawsubnets = split (' ', $in);
}
if ($#drawsubnets == -1) {
die ("Syntax: $0 [-d] netaddr/slash ...\n");
}
my %colors = load_colors ($hostdbini);
draw_misc($output_dir, \%colors);
my $html_file = ($output_dir?"$output_dir/":"") . "index.html";
open (HTML, "> $html_file") or die ("Could not open file '$html_file' for writing: $!\n");
print (HTML <<EOH);
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>Netplan</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
EOH
my $supernet;
foreach $supernet (@drawsubnets) {
my ($netaddr, $slash) = split ('/', $supernet);
if (int ($slash) > 24) {
warn ("Ignoring supernet '$supernet' - must be at least a /24\n");
next;
}
if ($hostdb->is_valid_subnet ($supernet)) {
do_supernet ($supernet, \%colors);
} else {
warn ("Ignoring invalid supernet '$supernet'\n");
}
}
print (HTML "</body>\n</html>\n");
close (HTML);
sub do_supernet
{
my $supernet = shift;
my $colors_ref = shift;
my @subnets;
my $no_subnets_counter = 0;
# start at
my $slash24 = $hostdb->aton ($hostdb->get_netaddr ($supernet));
my $last_slash24 = $hostdb->aton ($hostdb->get_broadcast ($supernet)) - 255;
# fetch all subnets in the supernet, not just for this /24
@subnets = $hostdb->findsubnetlongerprefix ($supernet);
print ("Found " . ($#subnets + 1) . " subnets matching supernet '$supernet'\n") if ($debug);
if ($#subnets != -1) {
print (HTML "<table BORDER='0' CELLSPACING='0' CELLPADDING='0'>\n<tr><th ALIGN='left' COLSPAN='2'>Net $supernet :</th></tr>\n");
#print (HTML "<h3>Net $supernet :</h3><p>\n<br>\n");
print_row (" ", "net_top.png", undef);
while ($slash24 <= $last_slash24) {
my $subnet_name = $hostdb->ntoa ($slash24) . "/24";
# get a list of the subnets STARTING in this /24.
my @slash24_subnets = get_subnets_starting_in_range (
$subnet_name, @subnets);
if ($#slash24_subnets != -1) {
# check if it was a while since we saw the last subnet
do_no_subnets ($no_subnets_counter);
$no_subnets_counter = 0;
print " $subnet_name: Drawing picture of " .
($#slash24_subnets + 1). " subnet(s)\n" if ($debug);
my $fn = $subnet_name;
$fn =~ s#[\./]#-#go;
$fn .= ".png";
if (1 == @slash24_subnets) {
# only one subnet, link directly to showsubnet.cgi
print_row ($subnet_name, $fn, "$showsubnet_path?;subnet=" . $slash24_subnets[0]->subnet());
} else {
# more than one subnet, link to whois.cgi which provides a list
print_row ($subnet_name, $fn, "$whois_path?;whoisdatatype=subnet;whoisdata=$subnet_name");
}
$fn = "$output_dir/$fn" if ($output_dir);
# draw_subnet() returns the number of addresses in what was drawn.
# this is typically 256 for a /24, but if what we got was a larger
# network (like a /23) this will be reflected.
$slash24 += draw_subnet ($fn, $subnet_name, \@slash24_subnets, $colors_ref);
} else {
# no subnets found in this /24, go to the next
$slash24 += 256;
$no_subnets_counter++;
}
}
print (HTML "</table><p>\n<br><hr>\n<p>\n");
}
}
sub print_row
{
my $left_side_text = shift;
my $img_name = shift;
my $link = shift;
if ($link) {
print (HTML <<EOH);
<tr><td ALIGN='left'>$left_side_text </td>
<td ALIGN='left'><a href='$link'>
<img SRC='$img_name' ALT='$left_side_text' BORDER=0></a></td></tr>
EOH
} else {
print (HTML <<EOH);
<tr><td ALIGN='left'>$left_side_text </td>
<td ALIGN='left'>
<img SRC='$img_name' ALT='$left_side_text' BORDER=0></td></tr>
EOH
}
}
sub do_no_subnets
{
my $counter = shift;
return if (! $counter);
if ($counter <= 10) {
while ($counter-- > 0) {
print_row ("...", "blank.png", undef);
}
} else {
# more than three, print nice
my $skipped_24s = $counter - 2;
print_row ("...", "blank.png", undef);
print(HTML "<tr><td ALIGN='left' COLSPAN='2'>Skipped $skipped_24s /24's</td></tr>\n");
print_row ("...", "blank.png", undef);
}
}
sub get_subnets_starting_in_range
{
my $supernet = shift;
my @subnets = @_;
my @result;
my $low = $hostdb->aton ($hostdb->get_netaddr ($supernet));
my $high = $hostdb->aton ($hostdb->get_broadcast ($supernet));
my $subnet;
foreach $subnet (@subnets) {
my $netaddr = $hostdb->aton ($subnet->netaddr ());
push (@result, $subnet) if ($netaddr >= $low and $netaddr <= $high);
}
return @result;
}
sub draw_subnet
{
my $output_filename = shift;
my $supernet = shift;
my $subnets_ref = shift;
my $colors_ref = shift;
my @subnets = @$subnets_ref;
my $total_address_count = 0;
my $supernet_slash = (split ('/', $supernet))[1];
# supernet is minimum a /24, possibly a /23 or even shorter prefix.
# in any case a division with 256 will not result in 0.
my $im_height = $slash24_height * int ($hostdb->get_num_addresses ($supernet_slash) / 256);
my $im_width = 256 * $pixels_per_ip;
print ("Creating image object ($im_width x $im_height)\n") if ($debug);
my $im = new GD::Image ($im_width, $im_height);
# allocate some colors - the first one becomes the background color
my $subnet_default_fill = color_resolve ($im, $colors_ref, "default", "#ffffff"); # default white
my $subnet_unused_frame = color_resolve ($im, $colors_ref, "subnet_unused_frame", "#000000"); # default black
my $subnet_frame = color_resolve ($im, $colors_ref, "subnet_frame", "#0000ff"); # default blue
my $subnet_text = color_resolve ($im, $colors_ref, "subnet_text", "#000000"); # default black
my $bg = color_resolve ($im, $colors_ref, "subnet_background", "#c8ffc8"); # default bright green
$im->filledRectangle (0, 0, $im_width - 1, $im_height - 1, $bg);
# Put a (black) frame around the picture
$im->rectangle (0, 0, $im_width - 1, $im_height - 1, $subnet_unused_frame);
my $subnet;
my $subnet_height = 0;
if (int ($supernet_slash) >= 24) {
$subnet_height = $slash24_height;
} else {
$subnet_height = $im_height;
}
foreach $subnet (@subnets) {
print (" Drawing subnet " . $subnet->subnet() . "\n") if ($debug);
my $subnet_left_side = ($hostdb->aton ($subnet->netaddr ()) -
$hostdb->aton ($hostdb->get_netaddr ($supernet))
) * $pixels_per_ip;
my $subnet_right_side = $subnet_left_side + ($subnet->addresses () * $pixels_per_ip) - 1;
my $color = $subnet_default_fill;
if ($subnet->htmlcolor ()) {
my $t = $subnet->htmlcolor ();
$color = color_resolve ($im, $colors_ref, "$t", undef);
}
# fill subnet with a color. this makes it easy to spot unallocated subnets.
$im->filledRectangle ($subnet_left_side, 0, $subnet_right_side, $im_height - 1, $color);
write_string ($im, $subnet_left_side, $subnet_right_side, $im_height, $subnet_text,
$subnet->short_description ());
# draw border around subnet
$im->rectangle ($subnet_left_side, 0, $subnet_right_side,
$im_height - 1, $subnet_frame);
$total_address_count += $subnet->addresses();
}
open (FILE, "> $output_filename") or die ("$0: Could not open file '$output_filename' for writing: $!\n");
binmode (FILE);
print (FILE $im->png ());
close (FILE);
return 256 if ($total_address_count < 256);
# if address count >= 256 it must be an even multiple of 256
return $total_address_count;
}
sub write_string
{
my $im = shift;
my $left_side = shift;
my $right_side = shift;
my $height = shift;
my $color = shift;
my $s = shift || '';
my $x_pixels_per_char = 6;
my $y_pixels_per_char = 8;
# use substr() to only get first part of short_description if the whole
# thing won't fit
$s = substr ($s, 0, ($right_side - $left_side) / $x_pixels_per_char);
# find middle of area height and subtract half a characters height
my $text_y_pos = ($height / 2) - ($y_pixels_per_char / 2) - 1;
# find middle of area and subtract half the length of the string in pixels
my $s_pixel_width = length ($s) * $x_pixels_per_char;
my $x_middle = $left_side + (($right_side - $left_side) / 2);
my $text_x_pos = $x_middle - ($s_pixel_width / 2) + 1;
$im->string (gdSmallFont, $text_x_pos, $text_y_pos, $s, $color);
}
sub draw_misc
{
my $output_dir = shift;
my $colors_ref = shift;
my $fn;
# draw a blank 'net'
$fn = ($output_dir?"$output_dir/":"") . "blank.png";
my $im_height = $slash24_height;
my $im_width = 256 * $pixels_per_ip;
draw_blank ($fn, $im_width, $im_height, $colors_ref);
$fn = ($output_dir?"$output_dir/":"") . "net_top.png";
draw_net_top ($fn, $im_width, $im_height, $colors_ref);
}
sub draw_blank
{
my $output_filename = shift;
my $im_width = shift;
my $im_height = shift;
my $colors_ref = shift;
print ("Creating blank image ($im_width x $im_height)\n") if ($debug);
my $im = new GD::Image ($im_width, $im_height);
# allocate some colors - the first one becomes the background color
my $background = color_resolve ($im, $colors_ref, "subnet_blank", "#ffffff");
# make the background transparent and interlaced
$im->transparent ($background);
$im->interlaced ('true');
open (FILE, "> $output_filename") or die ("$0: Could not open file '$output_filename' for writing: $!\n");
binmode (FILE);
print (FILE $im->png ());
close (FILE);
}
sub draw_net_top
{
my $output_filename = shift;
my $im_width = shift;
my $im_height = shift;
my $colors_ref = shift;
print ("Creating net_top image ($im_width x $im_height)\n") if ($debug);
my $im = new GD::Image ($im_width, $im_height);
# allocate some colors - the first one becomes the background color
my $subnet_top_background = color_resolve ($im, $colors_ref, "subnet_top_background", "#ffffff"); # default white
my $subnet_top_text = color_resolve ($im, $colors_ref, "subnet_top_text", "#000000"); # default black
my $subnet_top_frame = color_resolve ($im, $colors_ref, "subnet_top_frame", "#0000ff"); # default blue
my $step = 16;
my $i = 0;
while ($i < 256) {
my $left_side = $i * $pixels_per_ip;
my $right_side = ($i + $step) * $pixels_per_ip;
$im->rectangle ($left_side, 0, $right_side - 1,
$im_height - 1, $subnet_top_frame);
write_string ($im, $left_side, $right_side, $im_height, $subnet_top_text, $i);
$i += $step;
}
open (FILE, "> $output_filename") or die ("$0: Could not open file '$output_filename' for writing: $!\n");
binmode (FILE);
print (FILE $im->png ());
close (FILE);
}
sub load_colors
{
my $hostdbini = shift;
my %res;
my @colors = $hostdbini->Parameters ('subnet_colors');
my $t;
foreach $t (@colors) {
$res{$t} = $hostdbini->val ('subnet_colors', $t);
}
# make RED the default so that a non-specified color is obvious
$res{default} = "#ff0000" if (! defined ($res{default}));
return %res;
}
sub is_valid_color
{
my $in = shift;
return 1 if ($in =~ /^#[0-9a-f]{6,6}$/i);
return 0;
}
sub color_resolve
{
my $im = shift;
my $colors_ref = shift;
my $color_name = shift;
my $default = shift;
my ($pack, $file, $line, $subname, $hasargs, $wantarray) = caller (0);
my $c = $default;
if (defined ($colors_ref->{$color_name})) {
$c = $colors_ref->{$color_name};
if (! is_valid_color ($c)) {
die ("$0: Color lookup for '$color_name' resulted in invalid color '$c' ($file:$line).\n");
}
} else {
if (! is_valid_color ($c)) {
die ("$0: Oops, bad programmer. Default color '$c' " .
"(name '$color_name') is not valid ($file:$line).\n");
}
}
my $c1 = hex (substr($c, 1, 2));
my $c2 = hex (substr($c, 3, 2));
my $c3 = hex (substr($c, 5, 2));
return $im->colorResolve ($c1, $c2, $c3);
}