moved interfaces ui into a seperate php file.
[CBFWR.git] / libcbfwr / config.php
index 372bd04..70878f5 100644 (file)
@@ -41,6 +41,11 @@ class Config {
                $this->config = $config;
        }
        
+       function parseEthtool($interface)
+       {
+               // im not going to enjoy this, not one little bit
+       }
+       
        function findHardware()
        {
                
@@ -63,10 +68,22 @@ class Config {
                                        }
                                        fclose($fp);
                                }
-                               if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = file_get_contents("/sys/class/net/$fname/mtu");
-                               if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = file_get_contents("/sys/class/net/$fname/address");
+                               
+                               // find hardware 
+                               if(file_exists("/sys/class/net/$fname/mtu")) $this->config["hardware"]["netdev"][$fname]["mtu"] = trim(file_get_contents("/sys/class/net/$fname/mtu"));
+                               if(file_exists("/sys/class/net/$fname/address")) $this->config["hardware"]["netdev"][$fname]["hwaddress"] = trim(file_get_contents("/sys/class/net/$fname/address"));
                                if(file_exists("/sys/class/net/$fname/bonding")) $this->config["hardware"]["netdev"][$fname]["bonding"] = true;
                                if(file_exists("/sys/class/net/$fname/bridge")) $this->config["hardware"]["netdev"][$fname]["bridge"] = true;
+                               if(file_exists("/sys/class/net/$fname/duplex")) {
+                                       $myval = trim(file_get_contents("/sys/class/net/$fname/duplex"));
+                                       if($myval == null) $myval = "default";
+                                       $this->config["hardware"]["netdev"][$fname]["duplex"] = $myval; 
+                               }
+                               if(file_exists("/sys/class/net/$fname/speed")) {
+                                       $myval = trim(file_get_contents("/sys/class/net/$fname/speed"));
+                                       if($myval == null) $myval = "default";
+                                       $this->config["hardware"]["netdev"][$fname]["speed"] = $myval; 
+                               }
                        }
                }
        }
@@ -76,6 +93,10 @@ class Config {
                return $this->config;
        }
        
+       function setConfig($config) {
+               $this->config = $config;
+       }
+       
        function applyConfig()
        {
                global $AM_DAEMON;
@@ -85,87 +106,7 @@ class Config {
                return true;
        }
        
-       function mergeConfig($configone, $configtwo)
-       {
-               // yep this means apply changes in configtwo to configone
-               $newconf = $configone;
-               
-               // this is gunna be tough its gunna be a meet on meet sandwitch
-               foreach($configtwo as $key => $var) {
-                       if($key != "status") {
-                               error_log("apply change $key");
-                               foreach($var as $key2=>$var2) {
-                                       error_log("which is $key2");
-                                       $splits = explode(" ", $var2);
-                                       
-                                       // find a description
-                                       $description = null;
-                                       $hasdescription = false;
-                                       for($i=0; $i<count($splits); $i++) {
-                                               if($hasdescription !== false) {
-                                                       if($description != null) {
-                                                               $description .= " ";
-                                                       }
-                                                       $description .= $splits[$i];
-                                               } 
-                                               if($splits[$i] == "#") {
-                                                       $hasdescription = $i;
-                                               }
-                                       }
-                                       
-                                       
-                                       switch($key2) {
-                                               case "add":
-                                                       error_log("would add $var2");
-                                                       $thing = $splits[0];
-                                                       switch($thing) {
-                                                               case "zone":
-                                                                       $newconf["zone"][$splits[1]]["name"] = 1;
-                                                                       if($hasdescription) {
-                                                                               $newconf["zone"][$splits[1]]["description"] = $description;
-                                                                       }
-                                                                       break;
-                                                               
-                                                       }
-                                                       break;
-                                               case "modify":
-                                                       error_log("would modify $var2");
-                                                       $thing = $splits[0];
-                                                       switch($thing) {
-                                                               case "zone":
-                                                                       $zonename = $splits[1];
-                                                                       if($hasdescription) {
-                                                                               $newconf["zone"][$splits[1]]["description"] = $description;
-                                                                       }
-                                                                       if($splits[2] == "rename") {
-                                                                               $newname = $splits[3];
-                                                                               $tree = $newconf["zone"][$splits[1]];
-                                                                               unset($newconf["zone"][$splits[1]]);
-                                                                               $newconf["zone"][$newname] = $tree;
-                                                                       }
-                                                                       break;
-                                                               
-                                                       }
-                                                       break;
-                                                       
-                                                       
-                                               case "delete":
-                                                       error_log("would delete $var2");
-                                                       $thing = $splits[0];
-                                                       switch($thing) {
-                                                               case "zone":
-                                                                       $deletezone = $splits[1];
-                                                                       unset($newconf["zone"][$deletezone]);
-                                                                       break;
-                                                               
-                                                       }
-                                                       break;
-                                       }
-                               } 
-                       }
-               }
-               return $newconf;
-       }
+
        
        function loadConfigFile($file=null)
        {
@@ -184,16 +125,45 @@ class Config {
                while($line = fgets($fp)) {
                        $line = trim($line);
                        echo "read line $line\n";
-                       if($line != "") $this->parseLine($line, $i++);
+                       if($line != "") $this->addConfigLine($line, $i++);
                }
        }
        
-       function parseLine($line, $lineno)
+       function resolveInterfaceName($name)
        {
+               // $config only ever contains real interface names
+               if(isset($this->config["interface"]["dev"][$name])) return $name;
+               
+               if(isset($this->config["interface"])) {
+                       foreach($this->config["interface"]["dev"] as $key => $var) {
+                               if(isset($var["name"])) {
+                                       if($var["name"] == $name) {
+                                               return $key;
+                                       }
+                               }
+                       }
+               } else {
+                       // have to assume its a hardware name and check /sys
+                       if(file_exists("/sys/class/net/$name")) return $name;
+               }
+               
+               return null;
+       }
+       
+       function addConfigLine($line, $lineno=0)
+       {
+               $line_n = trim($line);
+               $line = $line_n;
                $expl = preg_split("/ +/", $line);
                
+               if(count($expl) < 2) {
+                       $c = count($expl);
+                       error_log("Not a valid config line ($c), $line");
+                       return 0;
+               }
                echo "process command ".$expl[0]."\n";
                
+               // TODO: when dealing with descriptions, we need to deal with a null description (i.e. delete the description)
                // find a description
                $description = null;
                $hasdescription = false;
@@ -209,7 +179,7 @@ class Config {
                        }
                }
                
-               // now rebuild the array if there was one
+               // now rebuild the array if there was a description
                if($hasdescription) {
                        for($i=0; $i<$hasdescription; $i++) {
                                $expl_r[$i] = $expl[$i];
@@ -217,54 +187,109 @@ class Config {
                        $expl = $expl_r;
                }
                
+               // this is a special case for debugging
+               if($expl[0] == "hold") {
+                       error_log("command is $line");
+                       return 0;
+               }
+               
+               $delete = false;
+               // check for delete on the line
+               if($expl[0] == "delete") {
+                       $delete = true;
+                       
+                       // re-order the array
+                       for($i=0; $i < (count($expl)-1); $i++) {
+                       $expl[$i] = $expl[$i+1];
+                       }
+                       unset($expl[count($expl)-1]);
+               }
+               
+               // now for modify
+               $modify = false;
+               // check for delete on the line
+               if($expl[0] == "modify") {
+                       $modify = true;
+                       
+                       // re-order the array
+                       for($i=0; $i < (count($expl)-1); $i++) {
+                       $expl[$i] = $expl[$i+1];
+                       }
+                       unset($expl[count($expl)-1]);
+               }
+               
                switch($expl[0]) {
                        case "hostname":
                                // set the hostname to $1
-                               $this->config["hostname"] = $expl[1];
+                               if($delete) {
+                                       unset($this->config["hostname"]);                               
+                               } else {
+                                       $this->config["hostname"] = $expl[1];
+                               }
                                break;
                                
                        case "domainname":
                                // set the hostname to $1
-                               $this->config["domainname"] = $expl[1];
+                               if($delete) {
+                                       unset($this->config["domainname"]);
+                               } else {
+                                       $this->config["domainname"] = $expl[1];
+                               }
                                break;
                                
                        case "zone":
-                               if($hasdescription) {
-                                       $this->config["zone"][$expl[2]]["description"] = $description;
+                               if($delete) {
+                                       error_log("delete zone ".$expl[2]);
+                                       unset($this->config["zone"][$expl[2]]);
+                                       if(count($this->config["zone"]) < 1) {
+                                               error_log("zone now empty, delete zones");
+                                               unset($this->config["zone"]);
+                                       }
+                               } else {
+                                       if($hasdescription) {
+                                               $this->config["zone"][$expl[2]]["description"] = $description;
+                                       }
+                                       $this->config["zone"][$expl[2]]["name"] = true;
                                }
-                               $this->config["zone"][$expl[2]]["name"] = true;
                                break;
                                
                        case "interface":
                                switch($expl[1]) {
                                        case "dev":
-                                               $int = $expl[2];
-
+                                               $int = $this->resolveInterfaceName($expl[2]);
+                                               
+                                               if($int != $expl[2]) {
+                                                       error_log("resolved int as $int from ".$expl[2]);
+                                               }
+                                               if($int == null) {
+                                                       error_log("got config for interface ".$expl[2]." but i cannot find it, i will assume its hardware for now");
+                                                       $int = $expl[2];
+                                               }
                                                if($hasdescription) {
-                                                       $this->config["interface"][$int]["description"] = $description;
+                                                       $this->config["interface"]["dev"][$int]["description"] = $description;
                                                }
        
                                                switch($expl[3]) {
                                                        case "address4":
-                                                               $this->config["interface"]["$int"]["address4"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["address4"] = $expl[4];
                                                                break;
                                                        case "address6":
-                                                               $this->config["interface"]["$int"]["address6"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["address6"] = $expl[4];
                                                                break;
                                                        case "name":
-                                                               $this->config["interface"]["$int"]["name"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["name"] = $expl[4];
                                                                break;
                                                        case "status":
-                                                               $this->config["interface"]["$int"]["status"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["status"] = $expl[4];
                                                                break;
                                                        case "mtu":
-                                                               $this->config["interface"]["$int"]["mtu"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["mtu"] = $expl[4];
                                                                break;
                                                        case "zone":
-                                                               $this->config["interface"]["$int"]["zone"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["zone"] = $expl[4];
                                                                break;
                                                        case "speed":
-                                                               $this->config["interface"]["$int"]["speed"] = $expl[4];
+                                                               $this->config["interface"]["dev"]["$int"]["speed"] = $expl[4];
                                                                break;
                                                        case "duplex":
                                                                $this->config["interface"]["$int"]["duplex"] = $expl[4];
@@ -308,54 +333,65 @@ class Config {
                                
                                
                        case "login":
-                               $this->config["login"][$expl[1]] = $expl[3];
-                               if($hasdescription) {
-                                       $this->config["login"][$expl[1]]["description"] = $description;
+                               if($delete) {
+                                       unset($this->config["login"][$expl[1]]);
+                               } else {
+                                       $this->config["login"][$expl[1]] = $expl[3];
+                                       if($hasdescription) {
+                                               $this->config["login"][$expl[1]]["description"] = $description;
+                                       }
                                }
                                break;
                        
                        case "route4":
-                               $route = $expl[1];
-                               $via = $expl[2];
-                               $dest = $expl[3];
-                               if($via == "to") {
-                                       $this->config["route4"][$route]["address"] = $dest;
+                               if($delete) {
+                                       unset($this->config["route4"][$expl[1]]);
                                } else {
-                                       $this->config["route4"][$route]["device"] = $dest;
-                               }
-                               if(isset($expl[4])) {
-                                       if($expl[4] == "dev") {
-                                               if(isset($expl[5])) {
-                                                       $this->config["route4"][$route]["device"] = $expl[5];
+                                       $route = $expl[1];
+                                       $via = $expl[2];
+                                       $dest = $expl[3];
+                                       if($via == "to") {
+                                               $this->config["route4"][$route]["address"] = $dest;
+                                       } else {
+                                               $this->config["route4"][$route]["device"] = $dest;
+                                       }
+                                       if(isset($expl[4])) {
+                                               if($expl[4] == "dev") {
+                                                       if(isset($expl[5])) {
+                                                               $this->config["route4"][$route]["device"] = $expl[5];
+                                                       }
                                                }
                                        }
-                               }
-                               if($hasdescription) {
-                                       $this->config["route4"][$route]["description"] = $description;
+                                       if($hasdescription) {
+                                               $this->config["route4"][$route]["description"] = $description;
+                                       }
                                }
                                break;
                                
                                
                        case "route6":
-                               $route = $expl[1];
-                               $via = $expl[2];
-                               $dest = $expl[3];
-                               if($via == "to") {
-                                       $this->config["route6"][$route]["address"] = $dest;
+                               if($delete) {
+                                       unset($this->config["route6"][$expl[1]]);
                                } else {
-                                       $this->config["route6"][$route]["device"] = $dest;
-                               }
-                               if(isset($expl[4])) {
-                                       if($expl[4] == "dev") {
-                                               if(isset($expl[5])) {
-                                                       $this->config["route6"][$route]["device"] = $expl[5];
+                                       $route = $expl[1];
+                                       $via = $expl[2];
+                                       $dest = $expl[3];
+                                       if($via == "to") {
+                                               $this->config["route6"][$route]["address"] = $dest;
+                                       } else {
+                                               $this->config["route6"][$route]["device"] = $dest;
+                                       }
+                                       if(isset($expl[4])) {
+                                               if($expl[4] == "dev") {
+                                                       if(isset($expl[5])) {
+                                                               $this->config["route6"][$route]["device"] = $expl[5];
+                                                       }
                                                }
                                        }
+                                       if($hasdescription) {
+                                               $this->config["route6"][$route]["description"] = $description;
+                                       }
                                }
-                               if($hasdescription) {
-                                       $this->config["route6"][$route]["description"] = $description;
-                               }
-                               
                                // here we should check "$route"
                                break;