code for startup and read config file and probe network hardware
authorpaulr <me@pjr.cc>
Mon, 19 Sep 2011 17:36:09 +0000 (03:36 +1000)
committerpaulr <me@pjr.cc>
Mon, 19 Sep 2011 17:36:09 +0000 (03:36 +1000)
libcbfwr/config.php
var/fw.conf [new file with mode: 0644]

index c0873b5..e63f13c 100644 (file)
@@ -12,11 +12,15 @@ class Config {
                        $this->config_file = null;
                        if(file_exists("../var/fw.conf")) {
                                $this->config_file = realpath("../var/fw.conf");
+                               $this->config["status"] = "conf";
+                               
                        }
                        if(file_exists("/var/lib/fwd/fw.conf")) {
                                $this->config_file = "/var/lib/fwd/fw.conf";
+                               $this->config["status"] = "conf";
                        }
                        
+                       
                        if($this->config_file == null) {
                                $this->config["status"] = "noconf";
                                if(is_dir("/var/lib/fwd/")) {
@@ -30,11 +34,283 @@ class Config {
                                        exit(0);
                                }
                        }
+                       
+                       if($this->config["status"] == "conf") {
+                               $this->loadConfig($this->config_file);
+                               print_r($this->config);
+                               $this->config = array();
+                               $this->findHardware();
+                               print_r($this->config);
+                               $this->applyConfig();
+                       } else {
+                               // we go into firstrun mode
+                       }
+                       
+                       
                } else {
                        // config comes from shm... we'll get there
                }       
        }
        
+       function findHardware()
+       {
+               // first, network interfaces
+               $dh = opendir("/sys/class/net/");
+               while(($fname = readdir($dh)) !== false) {
+                       if($fname != "." && $fname != ".." && $fname != "lo") {
+                               $this->config["hardware"]["netdev"][$fname]["int"] = true;
+                               // now read drive name if you can
+                               $fp = fopen("/sys/class/net/$fname/device/uevent", "r");
+                               if($fp) while(!feof($fp)) {
+                                       $line = trim(fgets($fp));
+                                       $lpl = explode("=", $line);
+                                       if($lpl[0] == "DRIVER") {
+                                               $this->config["hardware"]["netdev"][$fname]["driver"] = $lpl[1];
+                                       }
+                               }
+                               fclose($fp);
+                       }
+               }
+       }
+       
+       function applyConfig()
+       {
+               global $AM_DAEMON;
+               if(!$AM_DAEMON) return true;
+               
+               // oh the joy
+               return true;
+       }
+       
+       function loadConfig($file)
+       {
+               global $AM_DAEMON;
+               if(!$AM_DAEMON) return true;
+               
+               $fp = fopen($file, "r");
+               
+               $i = 1;
+               while($line = fgets($fp)) {
+                       $line = trim($line);
+                       echo "read line $line\n";
+                       if($line != "") $this->parseLine($line, $i++);
+               }
+       }
+       
+       function parseLine($line, $lineno)
+       {
+               $expl = preg_split("/ +/", $line);
+               
+               echo "process command ".$expl[0]."\n";
+               
+               // find a description
+               $description = null;
+               $hasdescription = false;
+               for($i=0; $i<count($expl); $i++) {
+                       if($hasdescription !== false) {
+                               if($description != null) {
+                                       $description .= " ";
+                               }
+                               $description .= $expl[$i];
+                       } 
+                       if($expl[$i] == "#") {
+                               $hasdescription = $i;
+                       }
+               }
+               
+               // now rebuild the array if there was one
+               if($hasdescription) {
+                       for($i=0; $i<$hasdescription; $i++) {
+                               $expl_r[$i] = $expl[$i];
+                       }
+                       $expl = $expl_r;
+               }
+               
+               switch($expl[0]) {
+                       case "hostname":
+                               // set the hostname to $1
+                               $this->config["hostname"] = $expl[1];
+                               break;
+                               
+                       case "domainname":
+                               // set the hostname to $1
+                               $this->config["domainname"] = $expl[1];
+                               break;
+                               
+                       case "zone":
+                               if($hasdescription) {
+                                       $this->config["zone"][$expl[2]]["description"] = $description;
+                               }
+                               $this->config["zone"][$expl[2]]["name"] = true;
+                               break;
+                               
+                       case "interface":
+                               switch($expl[1]) {
+                                       case "dev":
+                                               $int = $expl[2];
+
+                                               if($hasdescription) {
+                                                       $this->config["interface"][$int]["description"] = $description;
+                                               }
+       
+                                               switch($expl[3]) {
+                                                       case "address4":
+                                                               $this->config["interface"]["$int"]["address4"] = $expl[4];
+                                                               break;
+                                                       case "address6":
+                                                               $this->config["interface"]["$int"]["address6"] = $expl[4];
+                                                               break;
+                                                       case "name":
+                                                               $this->config["interface"]["$int"]["name"] = $expl[4];
+                                                               break;
+                                                       case "status":
+                                                               $this->config["interface"]["$int"]["status"] = $expl[4];
+                                                               break;
+                                                       case "mtu":
+                                                               $this->config["interface"]["$int"]["mtu"] = $expl[4];
+                                                               break;
+                                                       case "zone":
+                                                               $this->config["interface"]["$int"]["zone"] = $expl[4];
+                                                               break;
+                                                       case "speed":
+                                                               $this->config["interface"]["$int"]["speed"] = $expl[4];
+                                                               break;
+                                                       case "duplex":
+                                                               $this->config["interface"]["$int"]["duplex"] = $expl[4];
+                                                               break;
+                                               }
+                                               break;
+                                               
+                                       case "vlan":
+                                               $vlanid = $expl[2];
+                                               $name = $expl[4];
+                                               $from = $expl[6];
+                                               $this->config["vlan"][$name]["parent"] = $from;
+                                               $this->config["vlan"][$name]["id"] = $vlanid;
+                                               if($hasdescription) {
+                                                       $this->config["vlan"][$name]["description"] = $description;
+                                               }
+                                               break;
+                                               
+                                       case "lag":
+                                               $name = $expl[3];
+                                               for($i=5; $i<count($expl); $i++) {
+                                                       $this->config["lag"][$name][$i-5] = $expl[$i];
+                                               }
+                                               if($hasdescription) {
+                                                       $this->config["lag"][$name]["description"] = $description;
+                                               }
+                                               break;
+
+                                       case "bridge":
+                                               $name = $expl[3];
+                                               for($i=5; $i<count($expl); $i++) {
+                                                       $this->config["bridge"][$name][$i-5] = $expl[$i];
+                                               }
+                                               if($hasdescription) {
+                                                       $this->config["bridge"][$name]["description"] = $description;
+                                               }
+                                               break;
+                                               
+                               }
+                               break;
+                               
+                               
+                       case "login":
+                               $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;
+                               } 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;
+                               }
+                               break;
+                               
+                               
+                       case "route6":
+                               $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;
+                               }
+                               
+                               // here we should check "$route"
+                               break;
+                               
+                               
+                       case "dns":
+                               if(isset($this->config["dns"]["nservers"])) {
+                                       $dns_servers = $this->config["dns"]["nservers"];
+                               } else {
+                                       $dns_servers = 0;
+                               }
+                               if($expl[1] == "server") $this->config["dns"]["server"][$dns_servers]["address"] = $expl[2];
+                               if($hasdescription) {
+                                       $this->config["dns"]["server"][$dns_servers]["description"] = $description;
+                               }
+                               $this->config["dns"]["nservers"] = $dns_servers+1;
+                               break;
+                               
+                               
+                       case "ntp":
+                               if(isset($this->config["ntp"]["nservers"])) {
+                                       $ntp_servers = $this->config["ntp"]["nservers"];
+                               } else {
+                                       $ntp_servers = 0;
+                               }
+                               if($expl[1] == "server") $this->config["ntp"]["server"][$ntp_servers]["address"] = $expl[2];
+                               if($hasdescription) {
+                                       $this->config["ntp"]["server"][$ntp_servers]["description"] = $description;
+                               }
+                               $this->config["ntp"]["nservers"] = $ntp_servers+1;
+                               break;
+                               
+                               
+                               
+                       default:
+                               echo "Errr, unknown config directive on line $lineno, $line\n";
+               }
+               
+
+       }
+       
+       function saveConfig()
+       {
+               global $AM_DAEMON;
+               if(!$AM_DAEMON) return true;
+               
+       }       
        
        private $config_file;
        private $config;
diff --git a/var/fw.conf b/var/fw.conf
new file mode 100644 (file)
index 0000000..c17b2d1
--- /dev/null
@@ -0,0 +1,44 @@
+hostname hostname
+
+domainname domain.name
+
+login admin auth $4$QqcI5xWa$Ty8Vs3aAVBwPF0IpKO5hvgUi4wA$
+
+zone name internet
+zone name office
+zone name tier1
+zone name tier2
+zone name tier3
+
+interface dev eth0 name twatter # twatter is our outbound interface
+interface dev eth1 name mixer
+
+interface dev twatter address4 10.172.192.10/24
+interface dev twatter address6 2003:12:12:12::1/64
+interface dev twatter zone internet
+
+interface dev mixer zone office # description
+interface dev mixer mtu 1500
+interface dev mixer speed 1000
+interface dev mixer duplex full
+interface dev mixer address4 1.2.22.3/24
+
+interface lag name mybond with eth2 eth3 eth4 eth5 eth6 # description
+interface vlan 10 name vlan10 from mybond # description
+
+interface bridge name mybridge with eth7 eth8 eth9 # description
+
+route4 default to 10.172.192.1 # description
+route4 10.1.2.3/24 to 10.172.192.1 dev mixer # description
+route4 10.192.10.0/24 to 10.172.193.1 # description
+
+route6 default to fe80::1 dev twatter # description
+route6 2003:13:14:15::/64 to fe80::1 dev mixer # description
+
+dns server 192.168.1.1
+dns server 192.168.1.2
+dns server 192.168.1.3
+dns server 192.168.1.4 # main dns server
+
+ntp server 192.168.1.1 # main ntp server
+ntp server 192.168.1.2 # secondary ntp server