changed how configs work
authorpaulr <me@pjr.cc>
Thu, 22 Sep 2011 06:43:14 +0000 (16:43 +1000)
committerpaulr <me@pjr.cc>
Thu, 22 Sep 2011 06:43:14 +0000 (16:43 +1000)
bin/cbfwrd.php
doco/configs.txt
libcbfwr/comms.php
libcbfwr/config.php
libcbfwr/lib.php
libcbfwr/web.php
unittests/readconfigfromshm.php
unittests/shmtest.php [new file with mode: 0644]

index b8360e5..4abcbbd 100644 (file)
@@ -19,14 +19,16 @@ glcas_pluginLoader();
 
 error_log("CBFWRD starting");
 
+$comms = new Comms;
 
-$config = new Config();
-global $config;
 
 if(isset($argv[1])) {
+       // we get called here on boot.
        if($argv[1] == "boot") {
-               // we get called here on boot.
-               $config->bootHardware();
+               $config = new Config(false);
+               $config->findHardware();
+               $conf = $config->getConfig();
+               $comms->putConfig($conf, 1);
                exit(0);
        }
 }
@@ -37,10 +39,18 @@ $cont = true;
 // setup our sem/shm stuff
 
 // do the initial stuff
-$comms = new Comms;
-$config->loadConfig();
+$config = new Config(true);
+$config->loadConfigFile();
 $rawconf = $config->getConfig();
 $comms->putConfig($rawconf);
+$config->applyConfig();
+$runconf["status"] = "nochange";
+$comms->putConfig($runconf, 2);
+$res = $comms->getConfig(3);
+if($res === false) {
+       $comms->putConfig($runconf, 3);
+}
+
 
 while($cont) {
        $msg = $comms->waitForMessage();
@@ -59,30 +69,9 @@ function cbfwd_commandProcessor($command)
        
        $comms->lockConfigs();
        switch($cmds[0]) {
-               case "goodone":
-                       break;
-                       
-               // interface change requests
-               case "interface":
-                       $int = $cmds[1];
-                       if($cmds[2] == "changename") {
-                               $rawconf = $comms->getConfig();
-                               if(isset($rawconf["interface"]["$int"]["name"])) $rawconf["interface"]["$int"]["name"] = $cmds[3];
-                               $comms->putConfig($rawconf);
-                       }
+               case "applyclientconfig":
                        break;
                        
-               case "zone":
-                       $cmd = $cmds[1];
-                       switch($cmd) {
-                               case "add":
-                                       $zonename = $cmds[2];
-                                       $rawconf = $comms->getConfig();
-                                       $rawconf["zone"][$zonename]["name"] = 1;
-                                       $comms->putConfig($rawconf);
-                                       break;
-                                       
-                       }
        }
        $comms->unlockConfigs();
 }
index 7184e3b..47d3e5b 100644 (file)
@@ -3,4 +3,9 @@ there are 3 areas in shared memory reserved for configuration.
 0 - fs configuration
 1 - boot hardware config
 2 - running config (deltas from 0)
-3 - web config (contains only deltas from 0).
\ No newline at end of file
+3 - web config (contains only deltas from 0).
+
+web config writes directly to 3 and messages cbrwrd about stuff.
+
+config layout is like this:
+
index a11f9b3..b0c4f04 100644 (file)
@@ -1,11 +1,12 @@
 <?php
 // C = msg key
 // L = locking key
-$MESSAGE_KEY = ftok(realpath(dirname(__FILE__)), "c");
+$MESSAGE_KEY = ftok(realpath(dirname(__FILE__)), "c"); // to daemon
 $LOCKING_KEY = ftok(realpath(dirname(__FILE__)), "l");
 $STORE_KEY = ftok(realpath(dirname(__FILE__)), "s");
+$CONF_STORE_SIZE = 16777216; // 16M by default
 
-global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
 
 class Comms {
        function __construct()
@@ -17,9 +18,9 @@ class Comms {
        
        function getConfig($type=0)
        {
-               global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+               global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
                sem_acquire($this->semres);
-               $shm_space = shm_attach($STORE_KEY, 16*1024*1024);
+               $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
                $config = shm_get_var($shm_space, $type);
                sem_release($this->semres);
                
@@ -43,9 +44,9 @@ class Comms {
        // type = 3 for web config
        function putConfig($config, $type=0)
        {
-               global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY;
+               global $MESSAGE_KEY, $LOCKING_KEY, $STORE_KEY, $CONF_STORE_SIZE;
                sem_acquire($this->semres);
-               $shm_space = shm_attach($STORE_KEY, 16*1024*1024);
+               $shm_space = shm_attach($STORE_KEY, $CONF_STORE_SIZE);
                shm_put_var($shm_space, $type, $config);
                sem_release($this->semres);             
        }
index 7bffc1d..3c7a54f 100644 (file)
@@ -4,11 +4,10 @@ class Config {
        // here we load a config if we can find it
        // there are two sides to every class, the fwd side
        // and the web page site (command line is web for all intents)
-       function __construct()
+       function __construct($look_for_config = false)
        {
-               global $AM_DAEMON;
                
-               if($AM_DAEMON) {
+               if($look_for_config) {
                        $this->config_file = null;
                        if(file_exists("../var/fw.conf")) {
                                $this->config_file = realpath("../var/fw.conf");
@@ -28,47 +27,18 @@ class Config {
                                        $this->config_file = "/var/lib/fwd/fw.conf";
                                } else if(is_dir("../var/")) {
                                        echo "no config file found. Will use ../var/fw.conf for now\n";
-                                       $this->config_file = "../var/";
+                                       $this->config_file = "../var/fw.conf";
                                } else {
                                        echo "No directory where i can create a config, bailing\n";
                                        $this->config["status"] = "nodir";
                                }
                        }
-                       
-                       
-                       
                }
        }
        
-       function bootHardware()
+       function loadConfig($config)
        {
-               $this->findHardware();
-
-               $mycomm = new Comms();
-               $mycomm->putConfig($this->config, 1);
-               
-       }
-       
-       function loadConfig()
-       {
-               global $AM_DAEMON;
-               
-               if($AM_DAEMON) {
-                       if($this->config["status"] == "conf") { 
-                               $this->loadConfigFile($this->config_file);
-                               $this->applyConfig();
-                               $this->findHardware();
-                               
-                               $mycomm = new Comms();
-                               $mycomm->putConfig($this->config);
-                               $this->boot_config = $mycomm->getConfig(1);
-                       }
-               } else {
-                       $mycomm = new Comms();
-                       $this->config = $mycomm->getConfig(0);
-                       $this->boot_config = $mycomm->getConfig(1);
-                       
-               }
+               $this->config = $config;
        }
        
        function findHardware()
@@ -105,11 +75,6 @@ class Config {
                return $this->config;
        }
        
-       function getBootConfig()
-       {
-               return $this->boot_config;
-       }
-       
        function applyConfig()
        {
                global $AM_DAEMON;
@@ -119,10 +84,21 @@ class Config {
                return true;
        }
        
-       function loadConfigFile($file)
+       function mergeConfig($configone, $configtwo)
        {
-               global $AM_DAEMON;
-               if(!$AM_DAEMON) return true;
+               // yep
+       }
+       
+       function loadConfigFile($file=null)
+       {
+               
+               if($file == null) {
+                       if($this->config["status"] == "nodir") {
+                               return "nodir";
+                       } else $file = $this->config_file;
+               }
+               
+               if(!file_exists($file)) return "noconf";
                
                $fp = fopen($file, "r");
                
@@ -351,7 +327,6 @@ class Config {
        
        private $config_file;
        private $config;
-       private $boot_config;
 };
 
 ?>
\ No newline at end of file
index a179924..67bf0e3 100644 (file)
@@ -18,6 +18,10 @@ error_log("added libglcas path as $adpath");
 set_include_path(get_include_path().PATH_SEPARATOR.$adpath);
 
 */
+
+global $BASE_URL;
+
+
 require_once("libcbfwr/config.php");
 require_once("libcbfwr/comms.php");
 require_once("libcbfwr/web.php");
index 66e7cdf..87e7bf4 100644 (file)
@@ -26,10 +26,15 @@ class CBFWWeb {
                $bodycontent = null;
                
                $conf = new Config();
-               $conf->loadConfig();
+               $comms = new Comms(); 
+               $conf->loadConfig($comms->getConfig(0));
                $rconfig = $conf->getConfig();
                
                if($rconfig["status"] != "conf") {
+                       if($rconfig["status"] == "nodir") {
+                               cbfw_startnodir();
+                               return 0;
+                       }
                        cbfw_startinstaller();
                        return 0;
                }
@@ -292,5 +297,15 @@ Now, tell me where you want me to create the webconfig file:<br>
 <?php 
 }
 
+function cbfw_startnodir()
+{
+?>
+<html>
+<h1>Cant Run</h1>
+There is no config and no directory where i can store one. Login to the server and either create /var/run/cbfwr or <?php echo $LIB_ROOT_FS?>/var
+</html>
+<?php
+}
+
 
 ?>
\ No newline at end of file
index ce5a3fe..d5c585e 100644 (file)
@@ -19,11 +19,20 @@ glcas_pluginLoader();
 
 $comms = new Comms();
 
-while(true) {
-       sleep(5);
-       $conf = $comms->getConfig();
+echo "0 - fs config";
+$conf = $comms->getConfig(0);
+print_r($conf);
 
-       print_r($conf);
-}
+echo "1 - boot hardware";
+$conf = $comms->getConfig(1);
+print_r($conf);
+
+echo "2 - running config";
+$conf = $comms->getConfig(2);
+print_r($conf);
+
+echo "3 - client config";
+$conf = $comms->getConfig(3);
+print_r($conf);
 
 ?>
\ No newline at end of file
diff --git a/unittests/shmtest.php b/unittests/shmtest.php
new file mode 100644 (file)
index 0000000..6636c22
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+$CMD_ROOT_FS = realpath(dirname(__FILE__));
+$AM_DAEMON = false;
+
+global $CMD_ROOT_FS;
+global $AM_DAEMON;
+
+// add libglcas as if it were a path in ../libglcas
+if(file_exists("../libcbfwr")) {
+       $path = realpath($CMD_ROOT_FS."/../");
+       error_log("added cbfwr path as $path");
+       set_include_path(get_include_path().PATH_SEPARATOR.$path);
+}
+
+// include the based library
+require_once("libcbfwr/lib.php");
+
+glcas_pluginLoader();
+
+$comms = new Comms();
+
+while(true) {
+       sleep(5);
+       $conf = $comms->getConfig(0);
+
+       print_r($conf);
+}
+
+?>
\ No newline at end of file