2 ############################################################################
4 # This file is part of the IPFire Firewall. #
6 # IPFire is free software; you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation; either version 2 of the License, or #
9 # (at your option) any later version. #
11 # IPFire is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with IPFire; if not, write to the Free Software #
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
20 # Copyright (C) 2014 IPFire Team <info@ipfire.org>. #
22 ############################################################################
26 require "/var/ipfire/general-functions.pl";
30 my %PREFIX2NETMASK = (
31 32 => "255.255.255.255",
32 31 => "255.255.255.254",
33 30 => "255.255.255.252",
34 29 => "255.255.255.248",
35 28 => "255.255.255.240",
36 27 => "255.255.255.224",
37 26 => "255.255.255.192",
38 25 => "255.255.255.128",
39 24 => "255.255.255.0",
40 23 => "255.255.254.0",
41 22 => "255.255.252.0",
42 21 => "255.255.248.0",
43 20 => "255.255.240.0",
44 19 => "255.255.224.0",
45 18 => "255.255.192.0",
46 17 => "255.255.128.0",
66 my %NETMASK2PREFIX = reverse(%PREFIX2NETMASK);
68 # Takes an IP address in dotted decimal notation and
69 # returns a 32 bit integer representing that IP addresss.
70 # Will return undef for invalid inputs.
74 # This function returns undef for undefined input.
75 if (!defined $address) {
79 my $address_bin = &Socket
::inet_pton
(AF_INET
, $address);
81 $address_bin = unpack('N', $address_bin);
87 # Does the reverse of ip2bin().
88 # Will return undef for invalid inputs.
90 my $address_bin = shift;
92 # This function returns undef for undefined input.
93 if (!defined $address_bin) {
97 my $address = pack('N', $address_bin);
99 $address = &Socket
::inet_ntop
(AF_INET
, $address);
105 # Takes two network addresses, compares them against each other
106 # and returns true if equal or false if not
108 my $network1 = shift;
109 my $network2 = shift;
111 my @bin1 = &network2bin
($network1);
112 my @bin2 = &network2bin
($network2);
114 if (!defined $bin1 || !defined $bin2) {
118 if ($bin1[0] eq $bin2[0] && $bin1[1] eq $bin2[1]) {
125 # Takes a network in either a.b.c.d/a.b.c.d or a.b.c.d/e notation
126 # and will return an 32 bit integer representing the start
127 # address and an other one representing the network mask.
131 my ($address, $netmask) = split(/\//, $network, 2);
133 if (&check_prefix
($netmask)) {
134 $netmask = &convert_prefix2netmask
($netmask);
137 my $address_bin = &ip2bin
($address);
138 my $netmask_bin = &ip2bin
($netmask);
140 if (!defined $address_bin || !defined $netmask_bin) {
144 my $network_start = $address_bin & $netmask_bin;
146 return ($network_start, $netmask_bin);
149 # Deletes leading zeros in ip address
152 my @ip = split (/\./, $address);
154 foreach my $octet (@ip) {
155 $octet = int($octet);
158 $address = join (".", @ip);
162 # Returns True for all valid IP addresses
163 sub check_ip_address
($) {
166 # Normalise the IP address and compare the result with
167 # the input - which should obviously the same.
168 my $normalised_address = &_normalise_ip_address
($address);
170 return ((defined $normalised_address) && ($address eq $normalised_address));
173 # Returns True for all valid prefixes.
174 sub check_prefix
($) {
177 return (exists $PREFIX2NETMASK{$prefix});
180 # Returns True for all valid subnet masks.
181 sub check_netmask
($) {
184 return (exists $NETMASK2PREFIX{$netmask});
187 # Returns True for all valid inputs like a.b.c.d/a.b.c.d.
188 sub check_ip_address_and_netmask
($$) {
191 my ($address, $netmask) = split(/\//, $network, 2);
193 # Check if the IP address is fine.
195 my $result = &check_ip_address
($address);
200 return &check_netmask
($netmask);
203 # Returns True for all valid subnets like a.b.c.d/e or a.b.c.d/a.b.c.d
204 sub check_subnet
($) {
207 my ($address, $network) = split(/\//, $subnet, 2);
209 # Check if the IP address is fine.
210 my $result = &check_ip_address
($address);
215 return &check_prefix
($network) || &check_netmask
($network);
218 # For internal use only. Will take an IP address and
219 # return it in a normalised style. Like 8.8.8.010 -> 8.8.8.8.
220 sub _normalise_ip_address
($) {
223 my $address_bin = &ip2bin
($address);
224 if (!defined $address_bin) {
228 return &bin2ip
($address_bin);
231 # Returns the prefix for the given subnet mask.
232 sub convert_netmask2prefix
($) {
235 if (exists $NETMASK2PREFIX{$netmask}) {
236 return $NETMASK2PREFIX{$netmask};
242 # Returns the subnet mask for the given prefix.
243 sub convert_prefix2netmask
($) {
246 if (exists $PREFIX2NETMASK{$prefix}) {
247 return $PREFIX2NETMASK{$prefix};
253 # Takes an IP address and an offset and
254 # will return the offset'th IP address.
255 sub find_next_ip_address
($$) {
259 my $address_bin = &ip2bin
($address);
260 $address_bin += $offset;
262 return &bin2ip
($address_bin);
265 # Returns the network address of the given network.
266 sub get_netaddress
($) {
268 my ($network_bin, $netmask_bin) = &network2bin
($network);
270 if (defined $network_bin) {
271 return &bin2ip
($network_bin);
277 # Returns the broadcast of the given network.
278 sub get_broadcast
($) {
280 my ($network_bin, $netmask_bin) = &network2bin
($network);
282 return &bin2ip
($network_bin ^ ~$netmask_bin);
285 # Returns True if $address is in $network.
286 sub ip_address_in_network
($$) {
290 my $address_bin = &ip2bin
($address);
291 return undef unless (defined $address_bin);
293 my ($network_bin, $netmask_bin) = &network2bin
($network);
296 my $broadcast_bin = $network_bin ^ (~$netmask_bin % 2 ** 32);
298 return (($address_bin ge $network_bin) && ($address_bin le $broadcast_bin));
301 sub setup_upstream_proxy
() {
302 my %proxysettings = ();
303 &General
::readhash
("${General::swroot}/proxy/settings", \
%proxysettings);
305 if ($proxysettings{'UPSTREAM_PROXY'}) {
306 my $credentials = "";
308 if ($proxysettings{'UPSTREAM_USER'}) {
309 $credentials = $proxysettings{'UPSTREAM_USER'};
311 if ($proxysettings{'UPSTREAM_PASSWORD'}) {
312 $credentials .= ":" . $proxysettings{'UPSTREAM_PASSWORD'};
318 my $proxy = "http://" . $credentials . $proxysettings{'UPSTREAM_PROXY'};
320 $ENV{'http_proxy'} = $proxy;
321 $ENV{'https_proxy'} = $proxy;
322 $ENV{'ftp_proxy'} = $proxy;
326 my %wireless_status = ();
328 sub _get_wireless_status
($) {
331 if (!$wireless_status{$intf}) {
332 $wireless_status{$intf} = `iwconfig $intf`;
335 return $wireless_status{$intf};
338 sub wifi_get_essid
($) {
339 my $status = &_get_wireless_status
(shift);
341 my ($essid) = $status =~ /ESSID:\"(.*)\"/;
346 sub wifi_get_frequency
($) {
347 my $status = &_get_wireless_status
(shift);
349 my ($frequency) = $status =~ /Frequency:(\d+\.\d+ GHz)/;
354 sub wifi_get_access_point
($) {
355 my $status = &_get_wireless_status
(shift);
357 my ($access_point) = $status =~ /Access Point: ([0-9A-F:]+)/;
359 return $access_point;
362 sub wifi_get_bit_rate
($) {
363 my $status = &_get_wireless_status
(shift);
365 my ($bit_rate) = $status =~ /Bit Rate=(\d+ [GM]b\/s)/;
370 sub wifi_get_link_quality
($) {
371 my $status = &_get_wireless_status
(shift);
373 my ($cur, $max) = $status =~ /Link Quality=(\d+)\/(\d
+)/;
375 return $cur * 100 / $max;
378 sub wifi_get_signal_level
($) {
379 my $status = &_get_wireless_status
(shift);
381 my ($signal_level) = $status =~ /Signal level=(\-\d+ dBm)/;
383 return $signal_level;
386 sub get_hardware_address
($) {
387 my $ip_address = shift;
390 open(FILE
, "/proc/net/arp") or die("Could not read ARP table");
393 my ($ip_addr, $hwtype, $flags, $hwaddr, $mask, $device) = split(/\s+/, $_);
394 if ($ip_addr eq $ip_address) {
405 sub get_nic_property
{
407 my $property = shift;
410 open(FILE
, "/sys/class/net/$nicname/$property") or die("Could not read property");
422 return $mac =~ /^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$/;
429 $address = sprintf("$address:%02x", int(rand(255)));
435 sub get_mac_by_name
($) {
438 if ((!&valid_mac
($mac)) && ($mac ne "")) {
439 if (-e
"/sys/class/net/$mac/") {
440 $mac = get_nic_property
($mac, "address");
449 # Remove the next line to enable the testsuite
459 print "ASSERTION ERROR";
466 my $address1 = &ip2bin
("8.8.8.8");
467 assert
($address1 == 134744072);
469 my $address2 = &bin2ip
($address1);
470 assert
($address2 eq "8.8.8.8");
472 # Check if valid IP addresses are correctly recognised.
473 foreach my $address ("1.2.3.4", "192.168.180.1", "127.0.0.1") {
474 if (!&check_ip_address
($address)) {
475 print "$address is not correctly recognised as a valid IP address!\n";
480 # Check if invalid IP addresses are correctly found.
481 foreach my $address ("456.2.3.4", "192.768.180.1", "127.1", "1", "a.b.c.d", "1.2.3.4.5", "1.2.3.4/12") {
482 if (&check_ip_address
($address)) {
483 print "$address is recognised as a valid IP address!\n";
488 $result = &check_ip_address_and_netmask
("192.168.180.0/255.255.255.0");
491 $result = &convert_netmask2prefix
("255.255.254.0");
492 assert
($result == 23);
494 $result = &convert_prefix2netmask
(8);
495 assert
($result eq "255.0.0.0");
497 $result = &find_next_ip_address
("1.2.3.4", 2);
498 assert
($result eq "1.2.3.6");
500 $result = &network_equal
("192.168.0.0/24", "192.168.0.0/255.255.255.0");
503 $result = &network_equal
("192.168.0.0/24", "192.168.0.0/25");
506 $result = &network_equal
("192.168.0.0/24", "192.168.0.128/25");
509 $result = &network_equal
("192.168.0.1/24", "192.168.0.XXX/24");
512 $result = &ip_address_in_network
("10.0.1.4", "10.0.0.0/8");
515 $result = &ip_address_in_network
("192.168.30.11", "192.168.30.0/255.255.255.0");
518 print "Testsuite completed successfully!\n";