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