]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blobdiff - config/cfgroot/network-functions.pl
Merge remote-tracking branch 'origin/next'
[people/pmueller/ipfire-2.x.git] / config / cfgroot / network-functions.pl
index 7bd6466e0c91e3b6ece894e1cae9b71a1f7cf3ed..b7a840559ccd5ec52a4cf918065538690a2c32ed 100644 (file)
@@ -25,6 +25,7 @@ package Network;
 
 require "/var/ipfire/general-functions.pl";
 
+use experimental 'smartmatch';
 use Socket;
 
 # System ethernet configuration
@@ -331,13 +332,35 @@ sub setup_upstream_proxy() {
        }
 }
 
+sub list_wireless_interfaces() {
+       my %interfaces = ();
+
+       opendir(INTERFACES, "/sys/class/net");
+
+       my $intf;
+       while ($intf = readdir(INTERFACES)) {
+               # Is this a wireless interface?
+               opendir(PHY80211, "/sys/class/net/$intf/phy80211") or next;
+               closedir(PHY80211);
+
+               # Read the MAC address
+               my $address = &get_nic_property($intf, "address");
+
+               $interfaces{$address} = "$address ($intf)";
+       }
+
+       closedir(INTERFACES);
+
+       return %interfaces;
+}
+
 my %wireless_status = ();
 
 sub _get_wireless_status($) {
        my $intf = shift;
 
        if (!$wireless_status{$intf}) {
-               $wireless_status{$intf} = `iwconfig $intf`;
+               $wireless_status{$intf} = &General::system_output("iwconfig", "$intf");
        }
 
        return $wireless_status{$intf};
@@ -415,7 +438,7 @@ sub get_nic_property {
        my $property = shift;
        my $result;
 
-       open(FILE, "/sys/class/net/$nicname/$property") or die("Could not read property");
+       open(FILE, "/sys/class/net/$nicname/$property") or die("Could not read property $property for $nicname");
        $result = <FILE>;
        close(FILE);
 
@@ -430,6 +453,18 @@ sub valid_mac($) {
        return $mac =~ /^([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})$/;
 }
 
+# Compares two MAC addresses and returns true if they are equal
+sub is_mac_equal {
+       my $mac_1 = uc shift; # convert to upper case
+       my $mac_2 = uc shift;
+
+       if(valid_mac($mac_1) && valid_mac($mac_2) && ($mac_1 eq $mac_2)) {
+               return 1;
+       }
+
+       return 0;
+}
+
 sub random_mac {
        my $address = "02";
 
@@ -452,6 +487,28 @@ sub get_mac_by_name($) {
        return $mac;
 }
 
+sub get_intf_by_address($) {
+       my $address = shift;
+
+       opendir(INTERFACES, "/sys/class/net");
+
+       while (my $intf = readdir(INTERFACES)) {
+               next if ($intf eq "." or $intf eq "..");
+
+               my $intf_address = &get_nic_property($intf, "address");
+
+               # Skip interfaces without addresses
+               next if ($intf_address eq "");
+
+               # Return a match
+               return $intf if ($intf_address eq $address);
+       }
+
+       closedir(INTERFACES);
+
+       return undef;
+}
+
 #
 ## Function to get a list of all available network zones.
 #