]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/connections.cgi
Merge remote-tracking branch 'origin/master' into next
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / connections.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 use Net::IPv4Addr qw( :all );
25 use Switch;
26
27 # enable only the following on debugging purpose
28 #use warnings;
29 #use CGI::Carp 'fatalsToBrowser';
30
31 require '/var/ipfire/general-functions.pl';
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
34
35 my $colour_multicast = "#A0A0A0";
36
37 # sort arguments for connection tracking table
38 # the sort field. eg. 1=src IP, 2=dst IP, 3=src port, 4=dst port
39 my $SORT_FIELD = 0;
40 # the sort order. (a)scending orr (d)escending
41 my $SORT_ORDER = 0;
42 # cgi query arguments
43 my %cgiin;
44 # debug mode
45 my $debug = 0;
46
47 # retrieve query arguments
48 # note: let a-z A-Z and 0-9 pass as value only
49 if (length ($ENV{'QUERY_STRING'}) > 0){
50 my $name;
51 my $value;
52 my $buffer = $ENV{'QUERY_STRING'};
53 my @pairs = split(/&/, $buffer);
54 foreach my $pair (@pairs){
55 ($name, $value) = split(/=/, $pair);
56 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " "
57 $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass
58 $cgiin{$name} = $value;
59 }
60 }
61
62 &Header::showhttpheaders();
63
64 my @network=();
65 my @masklen=();
66 my @colour=();
67
68 my %netsettings=();
69 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
70
71 # output cgi query arrguments to browser on debug
72 if ( $debug ){
73 &Header::openbox('100%', 'center', 'DEBUG');
74 my $debugCount = 0;
75 foreach my $line (sort keys %cgiin) {
76 print "$line = '$cgiin{$line}'<br />\n";
77 $debugCount++;
78 }
79 print "&nbsp;Count: $debugCount\n";
80 &Header::closebox();
81 }
82
83 #workaround to suppress a warning when a variable is used only once
84 my @dummy = ( ${Header::table1colour} );
85 undef (@dummy);
86
87 # check sorting arguments
88 if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) {
89 $SORT_FIELD = $cgiin{'sort_field'};
90
91 if ( $cgiin{'sort_order'} ~~ [ 'a','d','A','D' ] ) {
92 $SORT_ORDER = lc($cgiin{'sort_order'});
93 }
94 }
95
96 # Read and sort the connection tracking table
97 # do sorting
98 if ($SORT_FIELD and $SORT_ORDER) {
99 # field sorting when sorting arguments are sane
100 open(CONNTRACK, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table";
101 } else {
102 # default sorting with no query arguments
103 open(CONNTRACK, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table";
104 }
105 my @conntrack = <CONNTRACK>;
106 close(CONNTRACK);
107
108 # Collect data for the @network array.
109
110 # Add Firewall Localhost 127.0.0.1
111 push(@network, '127.0.0.1');
112 push(@masklen, '255.255.255.255');
113 push(@colour, ${Header::colourfw});
114
115 if (open(IP, "${General::swroot}/red/local-ipaddress")) {
116 my $redip = <IP>;
117 close(IP);
118
119 chomp $redip;
120 push(@network, $redip);
121 push(@masklen, '255.255.255.255');
122 push(@colour, ${Header::colourfw});
123 }
124
125 # Add STATIC RED aliases
126 if ($netsettings{'RED_DEV'}) {
127 my $aliasfile = "${General::swroot}/ethernet/aliases";
128 open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
129 my @aliases = <ALIASES>;
130 close(ALIASES);
131
132 # We have a RED eth iface
133 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
134 # We have a STATIC RED eth iface
135 foreach my $line (@aliases) {
136 chomp($line);
137 my @temp = split(/\,/,$line);
138 if ($temp[0]) {
139 push(@network, $temp[0]);
140 push(@masklen, $netsettings{'RED_NETMASK'} );
141 push(@colour, ${Header::colourfw} );
142 }
143 }
144 }
145 }
146
147 # Add Green Firewall Interface
148 push(@network, $netsettings{'GREEN_ADDRESS'});
149 push(@masklen, "255.255.255.255" );
150 push(@colour, ${Header::colourfw} );
151
152 # Add Green Network to Array
153 push(@network, $netsettings{'GREEN_NETADDRESS'});
154 push(@masklen, $netsettings{'GREEN_NETMASK'} );
155 push(@colour, ${Header::colourgreen} );
156
157 # Add Green Routes to Array
158 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
159 foreach my $route (@routes) {
160 chomp($route);
161 my @temp = split(/[\t ]+/, $route);
162 push(@network, $temp[0]);
163 push(@masklen, $temp[2]);
164 push(@colour, ${Header::colourgreen} );
165 }
166
167 # Add Blue Firewall Interface
168 push(@network, $netsettings{'BLUE_ADDRESS'});
169 push(@masklen, "255.255.255.255" );
170 push(@colour, ${Header::colourfw} );
171
172 # Add Blue Network
173 if ($netsettings{'BLUE_DEV'}) {
174 push(@network, $netsettings{'BLUE_NETADDRESS'});
175 push(@masklen, $netsettings{'BLUE_NETMASK'} );
176 push(@colour, ${Header::colourblue} );
177
178 # Add Blue Routes to Array
179 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
180 foreach my $route (@routes) {
181 chomp($route);
182 my @temp = split(/[\t ]+/, $route);
183 push(@network, $temp[0]);
184 push(@masklen, $temp[2]);
185 push(@colour, ${Header::colourblue} );
186 }
187 }
188
189 # Add Orange Firewall Interface
190 push(@network, $netsettings{'ORANGE_ADDRESS'});
191 push(@masklen, "255.255.255.255" );
192 push(@colour, ${Header::colourfw} );
193
194 # Add Orange Network
195 if ($netsettings{'ORANGE_DEV'}) {
196 push(@network, $netsettings{'ORANGE_NETADDRESS'});
197 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
198 push(@colour, ${Header::colourorange} );
199 # Add Orange Routes to Array
200 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
201 foreach my $route (@routes) {
202 chomp($route);
203 my @temp = split(/[\t ]+/, $route);
204 push(@network, $temp[0]);
205 push(@masklen, $temp[2]);
206 push(@colour, ${Header::colourorange} );
207 }
208 }
209
210 # Highlight multicast connections.
211 push(@network, "224.0.0.0");
212 push(@masklen, "239.0.0.0");
213 push(@colour, $colour_multicast);
214
215 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
216 if (-e "${General::swroot}/ovpn/settings") {
217 my %ovpnsettings = ();
218 &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings);
219 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
220
221 # add OpenVPN net
222 push(@network, $tempovpnsubnet[0]);
223 push(@masklen, $tempovpnsubnet[1]);
224 push(@colour, ${Header::colourovpn} );
225
226 # add BLUE:port / proto
227 if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
228 push(@network, $netsettings{'BLUE_ADDRESS'} );
229 push(@masklen, '255.255.255.255' );
230 push(@colour, ${Header::colourovpn});
231 }
232
233 # add ORANGE:port / proto
234 if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
235 push(@network, $netsettings{'ORANGE_ADDRESS'} );
236 push(@masklen, '255.255.255.255' );
237 push(@colour, ${Header::colourovpn} );
238 }
239 }
240
241 # Add OpenVPN net for custom OVPNs
242 if (-e "${General::swroot}/ovpn/ccd.conf") {
243 open(OVPNSUB, "${General::swroot}/ovpn/ccd.conf");
244 my @ovpnsub = <OVPNSUB>;
245 close(OVPNSUB);
246
247 foreach (@ovpnsub) {
248 my ($network, $mask) = split '/', (split ',', $_)[2];
249
250 $mask = ipv4_cidr2msk($mask) unless &General::validip($mask);
251
252 push(@network, $network);
253 push(@masklen, $mask);
254 push(@colour, ${Header::colourovpn});
255 }
256 }
257
258 open(IPSEC, "${General::swroot}/vpn/config");
259 my @ipsec = <IPSEC>;
260 close(IPSEC);
261
262 foreach my $line (@ipsec) {
263 my @vpn = split(',', $line);
264 my ($network, $mask) = split("/", $vpn[12]);
265
266 if (!&General::validip($mask)) {
267 $mask = ipv4_cidr2msk($mask);
268 }
269
270 push(@network, $network);
271 push(@masklen, $mask);
272 push(@colour, ${Header::colourvpn});
273 }
274
275 if (-e "${General::swroot}/ovpn/n2nconf") {
276 open(OVPNN2N, "${General::swroot}/ovpn/ovpnconfig");
277 my @ovpnn2n = <OVPNN2N>;
278 close(OVPNN2N);
279
280 foreach my $line (@ovpnn2n) {
281 my @ovpn = split(',', $line);
282 next if ($ovpn[4] ne 'net');
283
284 my ($network, $mask) = split("/", $ovpn[12]);
285 if (!&General::validip($mask)) {
286 $mask = ipv4_cidr2msk($mask);
287 }
288
289 push(@network, $network);
290 push(@masklen, $mask);
291 push(@colour, ${Header::colourovpn});
292 }
293 }
294
295 # Show the page.
296 &Header::openpage($Lang::tr{'connections'}, 1, '');
297 &Header::openbigbox('100%', 'left');
298 &Header::openbox('100%', 'left', $Lang::tr{'connection tracking'});
299
300 # Print legend.
301 print <<END;
302 <table style='width:100%'>
303 <tr>
304 <td style='text-align:center;'>
305 <b>$Lang::tr{'legend'} :</b>
306 </td>
307 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourgreen}; font-weight:bold;'>
308 <b>$Lang::tr{'lan'}</b>
309 </td>
310 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourred};'>
311 <b>$Lang::tr{'internet'}</b>
312 </td>
313 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourorange};'>
314 <b>$Lang::tr{'dmz'}</b>
315 </td>
316 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourblue};'>
317 <b>$Lang::tr{'wireless'}</b>
318 </td>
319 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourfw};'>
320 <b>IPFire</b>
321 </td>
322 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourvpn};'>
323 <b>$Lang::tr{'vpn'}</b>
324 </td>
325 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourovpn};'>
326 <b>$Lang::tr{'OpenVPN'}</b>
327 </td>
328 <td style='text-align:center; color:#FFFFFF; background-color:$colour_multicast;'>
329 <b>Multicast</b>
330 </td>
331 </tr>
332 </table>
333 <br>
334 END
335
336 if ($SORT_FIELD and $SORT_ORDER) {
337 my @sort_field_name = (
338 $Lang::tr{'source ip'},
339 $Lang::tr{'destination ip'},
340 $Lang::tr{'source port'},
341 $Lang::tr{'destination port'},
342 $Lang::tr{'protocol'},
343 $Lang::tr{'connection'}.' '.$Lang::tr{'status'},
344 $Lang::tr{'expires'}.' ('.$Lang::tr{'seconds'}.')',
345 $Lang::tr{'download'},
346 $Lang::tr{'upload'}
347 );
348 my $sort_order_name;
349 if (lc($SORT_ORDER) eq "a") {
350 $sort_order_name = $Lang::tr{'sort ascending'};
351 } else {
352 $sort_order_name = $Lang::tr{'sort descending'};
353 }
354
355 print <<END
356 <div style="font-weight:bold;margin:10px;font-size: 70%">
357 $sort_order_name: $sort_field_name[$SORT_FIELD-1]
358 </div>
359 END
360 ;
361 }
362
363 # Print table header.
364 print <<END;
365 <table style='width:100%'>
366 <tr>
367 <th style='text-align:center'>
368 <a href="?sort_field=5&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
369 <a href="?sort_field=5&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
370 </th>
371 <th style='text-align:center' colspan='2'>
372 <a href="?sort_field=1&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
373 <a href="?sort_field=1&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
374 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
375 <a href="?sort_field=3&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
376 <a href="?sort_field=3&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
377 </th>
378 <th style='text-align:center' colspan='2'>
379 <a href="?sort_field=2&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
380 <a href="?sort_field=2&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
381 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
382 <a href="?sort_field=4&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
383 <a href="?sort_field=4&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
384 </th>
385 <th style='text-align:center'>
386 <a href="?sort_field=8&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
387 <a href="?sort_field=8&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
388 &nbsp;&nbsp;&nbsp;&nbsp;
389 <a href="?sort_field=9&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
390 <a href="?sort_field=9&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
391 </th>
392 <th style='text-align:center'>
393 <a href="?sort_field=6&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
394 <a href="?sort_field=6&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
395 </th>
396 <th style='text-align:center'>
397 <a href="?sort_field=7&amp;sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
398 <a href="?sort_field=7&amp;sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
399 </th>
400 </tr>
401 <tr>
402 <th style='text-align:center'>
403 $Lang::tr{'protocol'}
404 </th>
405 <th style='text-align:center' colspan='2'>
406 $Lang::tr{'source ip and port'}
407 </th>
408 <th style='text-align:center' colspan='2'>
409 $Lang::tr{'dest ip and port'}
410 </th>
411 <th style='text-align:center'>
412 $Lang::tr{'download'} /
413 <br>$Lang::tr{'upload'}
414 </th>
415 <th style='text-align:center'>
416 $Lang::tr{'connection'}<br>$Lang::tr{'status'}
417 </th>
418 <th style='text-align:center'>
419 $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
420 </th>
421 </tr>
422 END
423
424 foreach my $line (@conntrack) {
425 my @conn = split(' ', $line);
426
427 # The first bit is the l3 protocol.
428 my $l3proto = $conn[0];
429
430 # Skip everything that is not IPv4.
431 if ($l3proto ne 'ipv4') {
432 next;
433 }
434
435 # L4 protocol (tcp, udp, ...).
436 my $l4proto = $conn[2];
437
438 # Translate unknown protocols.
439 if ($l4proto eq 'unknown') {
440 my $l4protonum = $conn[3];
441 if ($l4protonum eq '2') {
442 $l4proto = 'IGMP';
443 } elsif ($l4protonum eq '4') {
444 $l4proto = 'IPv4 Encap';
445 } elsif ($l4protonum eq '33') {
446 $l4proto = 'DCCP';
447 } elsif ($l4protonum eq '41') {
448 $l4proto = 'IPv6 Encap';
449 } elsif ($l4protonum eq '50') {
450 $l4proto = 'ESP';
451 } elsif ($l4protonum eq '51') {
452 $l4proto = 'AH';
453 } elsif ($l4protonum eq '132') {
454 $l4proto = 'SCTP';
455 } else {
456 $l4proto = $l4protonum;
457 }
458 } else {
459 $l4proto = uc($l4proto);
460 }
461
462 # Source and destination.
463 my $sip;
464 my $sip_ret;
465 my $dip;
466 my $dip_ret;
467 my $sport;
468 my $sport_ret;
469 my $dport;
470 my $dport_ret;
471 my @packets;
472 my @bytes;
473
474 my $ttl = $conn[4];
475 my $state;
476 if ($l4proto eq 'TCP') {
477 $state = $conn[5];
478 }
479
480 # Kick out everything that is not IPv4.
481 foreach my $item (@conn) {
482 my ($key, $val) = split('=', $item);
483
484 switch ($key) {
485 case "src" {
486 if ($sip == "") {
487 $sip = $val;
488 } else {
489 $dip_ret = $val;
490 }
491 }
492 case "dst" {
493 if ($dip == "") {
494 $dip = $val;
495 } else {
496 $sip_ret = $val;
497 }
498 }
499 case "sport" {
500 if ($sport == "") {
501 $sport = $val;
502 } else {
503 $dport_ret = $val;
504 }
505 }
506 case "dport" {
507 if ($dport == "") {
508 $dport = $val;
509 } else {
510 $sport_ret = $val;
511 }
512 }
513 case "packets" {
514 push(@packets, $val);
515 }
516 case "bytes" {
517 push(@bytes, $val);
518 }
519 }
520 }
521
522 my $sip_colour = ipcolour($sip);
523 # use colour of destination network for DNAT
524 my $dip_colour = $dip ne $dip_ret ? ipcolour($dip_ret) : ipcolour($dip);
525
526 my $sserv = '';
527 if ($sport < 1024) {
528 $sserv = uc(getservbyport($sport, lc($l4proto)));
529 }
530
531 my $dserv = '';
532 if ($dport < 1024) {
533 $dserv = uc(getservbyport($dport, lc($l4proto)));
534 }
535
536 my $bytes_in = format_bytes($bytes[0]);
537 my $bytes_out = format_bytes($bytes[1]);
538
539 # Format TTL
540 $ttl = format_time($ttl);
541
542 my $sip_extra;
543 if ($sip ne $sip_ret) {
544 $sip_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
545 $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
546 $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>";
547 $sip_extra .= "</a>";
548 }
549
550 my $dip_extra;
551 if ($dip ne $dip_ret) {
552 $dip_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
553 $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
554 $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>";
555 $dip_extra .= "</a>";
556 }
557
558
559 my $sport_extra;
560 if ($sport ne $sport_ret) {
561 my $sserv_ret = '';
562 if ($sport_ret < 1024) {
563 $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto)));
564 }
565
566 $sport_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
567 $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
568 $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>";
569 $sport_extra .= "</a>";
570 }
571
572 my $dport_extra;
573 if ($dport ne $dport_ret) {
574 my $dserv_ret = '';
575 if ($dport_ret < 1024) {
576 $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto)));
577 }
578
579 $dport_extra = "<span style='color:#FFFFFF;'>&gt;</span> ";
580 $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
581 $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>";
582 $dport_extra .= "</a>";
583 }
584
585 print <<END;
586 <tr>
587 <td style='text-align:center'>$l4proto</td>
588 <td style='text-align:center; background-color:$sip_colour;'>
589 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
590 <span style='color:#FFFFFF;'>$sip</span>
591 </a>
592 $sip_extra
593 </td>
594 <td style='text-align:center; background-color:$sip_colour;'>
595 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
596 <span style='color:#FFFFFF;'>$sport</span>
597 </a>
598 $sport_extra
599 </td>
600 <td style='text-align:center; background-color:$dip_colour;'>
601 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
602 <span style='color:#FFFFFF;'>$dip</span>
603 </a>
604 $dip_extra
605 </td>
606 <td style='text-align:center; background-color:$dip_colour;'>
607 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
608 <span style='color:#FFFFFF;'>$dport</span>
609 </a>
610 $dport_extra
611 </td>
612 <td style='text-align:center'>
613 $bytes_in / $bytes_out
614 </td>
615 <td style='text-align:center'>$state</td>
616 <td style='text-align:center'>$ttl</td>
617 </tr>
618 END
619 }
620
621 # Close the main table.
622 print "</table>";
623
624 &Header::closebox();
625 &Header::closebigbox();
626 &Header::closepage();
627
628 sub format_bytes($) {
629 my $bytes = shift;
630 my @units = ("B", "k", "M", "G", "T");
631
632 foreach my $unit (@units) {
633 if ($bytes < 1024) {
634 return sprintf("%d%s", $bytes, $unit);
635 }
636
637 $bytes /= 1024;
638 }
639
640 return sprintf("%d%s", $bytes, $units[$#units]);
641 }
642
643 sub format_time($) {
644 my $time = shift;
645
646 my $seconds = $time % 60;
647 my $minutes = $time / 60;
648
649 my $hours = 0;
650 if ($minutes >= 60) {
651 $hours = $minutes / 60;
652 $minutes %= 60;
653 }
654
655 return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
656 }
657
658 sub ipcolour($) {
659 my $id = 0;
660 my $colour = ${Header::colourred};
661 my ($ip) = $_[0];
662 my $found = 0;
663
664 foreach my $line (@network) {
665 if ($network[$id] eq '') {
666 $id++;
667 } else {
668 if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) {
669 $found = 1;
670 $colour = $colour[$id];
671 }
672 $id++;
673 }
674 }
675
676 return $colour;
677 }
678
679 1;