X-Git-Url: http://git.ipfire.org/?p=people%2Fpmueller%2Fipfire-2.x.git;a=blobdiff_plain;f=config%2Fcfgroot%2Fnetwork-functions.pl;h=93b8190305abb93a7d29e0c8a117e22d7e46b6e9;hp=9dd752d5e698735b9745c7ec408e9c99a4b05329;hb=dbfd2622f58727f4b0f9bed8934a770a2050491f;hpb=27957a3f2b770c6a46d958cd72af41096275687c diff --git a/config/cfgroot/network-functions.pl b/config/cfgroot/network-functions.pl index 9dd752d5e6..93b8190305 100644 --- a/config/cfgroot/network-functions.pl +++ b/config/cfgroot/network-functions.pl @@ -102,6 +102,22 @@ sub bin2ip($) { return $address; } +# Takes two network addresses, compares them against each other +# and returns true if equal or false if not +sub network_equal { + my $network1 = shift; + my $network2 = shift; + + my $bin1 = &network2bin($network1); + my $bin2 = &network2bin($network2); + + if ($bin1 eq $bin2) { + return 1; + } + + return 0; +} + # Takes a network in either a.b.c.d/a.b.c.d or a.b.c.d/e notation # and will return an 32 bit integer representing the start # address and an other one representing the network mask. @@ -122,6 +138,19 @@ sub network2bin($) { return ($network_start, $netmask_bin); } +# Deletes leading zeros in ip address +sub ip_remove_zero{ + my $address = shift; + my @ip = split (/\./, $address); + + foreach my $octet (@ip) { + $octet = int($octet); + } + + $address = join (".", @ip); + + return $address; +} # Returns True for all valid IP addresses sub check_ip_address($) { my $address = shift; @@ -256,7 +285,7 @@ sub ip_address_in_network($$) { my ($network_bin, $netmask_bin) = &network2bin($network); # Find end address - my $broadcast_bin = $network_bin ^ ~$netmask_bin; + my $broadcast_bin = $network_bin ^ (~$netmask_bin % 2 ** 32); return (($address_bin ge $network_bin) && ($address_bin le $broadcast_bin)); } @@ -286,6 +315,85 @@ sub setup_upstream_proxy() { } } +my %wireless_status = (); + +sub _get_wireless_status($) { + my $intf = shift; + + if (!$wireless_status{$intf}) { + $wireless_status{$intf} = `iwconfig $intf`; + } + + return $wireless_status{$intf}; +} + +sub wifi_get_essid($) { + my $status = &_get_wireless_status(shift); + + my ($essid) = $status =~ /ESSID:\"(.*)\"/; + + return $essid; +} + +sub wifi_get_frequency($) { + my $status = &_get_wireless_status(shift); + + my ($frequency) = $status =~ /Frequency:(\d+\.\d+ GHz)/; + + return $frequency; +} + +sub wifi_get_access_point($) { + my $status = &_get_wireless_status(shift); + + my ($access_point) = $status =~ /Access Point: ([0-9A-F:]+)/; + + return $access_point; +} + +sub wifi_get_bit_rate($) { + my $status = &_get_wireless_status(shift); + + my ($bit_rate) = $status =~ /Bit Rate=(\d+ [GM]b\/s)/; + + return $bit_rate; +} + +sub wifi_get_link_quality($) { + my $status = &_get_wireless_status(shift); + + my ($cur, $max) = $status =~ /Link Quality=(\d+)\/(\d+)/; + + return $cur * 100 / $max; +} + +sub wifi_get_signal_level($) { + my $status = &_get_wireless_status(shift); + + my ($signal_level) = $status =~ /Signal level=(\-\d+ dBm)/; + + return $signal_level; +} + +sub get_hardware_address($) { + my $ip_address = shift; + my $ret; + + open(FILE, "/proc/net/arp") or die("Could not read ARP table"); + + while () { + my ($ip_addr, $hwtype, $flags, $hwaddr, $mask, $device) = split(/\s+/, $_); + if ($ip_addr eq $ip_address) { + $ret = $hwaddr; + last; + } + } + + close(FILE); + + return $ret; +} + 1; # Remove the next line to enable the testsuite @@ -339,9 +447,26 @@ sub testsuite() { $result = &find_next_ip_address("1.2.3.4", 2); assert($result eq "1.2.3.6"); + $result = &network_equal("192.168.0.0/24", "192.168.0.0/255.255.255.0"); + assert($result); + + $result = &network_equal("192.168.0.0/24", "192.168.0.0/25"); + assert(!$result); + + $result = &network_equal("192.168.0.0/24", "192.168.0.128/25"); + assert(!$result); + + $result = &network_equal("192.168.0.1/24", "192.168.0.XXX/24"); + assert($result); + $result = &ip_address_in_network("10.0.1.4", "10.0.0.0/8"); assert($result); + $result = &ip_address_in_network("192.168.30.11", "192.168.30.0/255.255.255.0"); + assert($result); + + print "Testsuite completed successfully!\n"; + return 0; }