add configuration file and command option parsing
2 files changed, 56 insertions(+), 2 deletions(-)

A => dhcpv6_pd.conf
M dhcpv6_pd.pike
A => dhcpv6_pd.conf +6 -0
@@ 0,0 1,6 @@ 
+{
+  "downstream_interface": "en0",
+  "upstream_interface": "en1",
+  "requested_prefix": 64,
+  "leasefile": "/var/run/dhcpv6_pd_leases"
+}

          
M dhcpv6_pd.pike +50 -2
@@ 1,5 1,11 @@ 
 import Protocols.DHCPv6;
 
+#ifdef DEBUG
+#define DWRITE(X...) werror(X)
+#else
+#define DWRITE(X...) 
+#endif /* DEBUG */
+
 constant DHCP_CLIENT_PORT = 546; 
 constant DHCP_SERVER_PORT = 547;
 

          
@@ 34,18 40,42 @@ IAID iaid;
 
 
 int current_iapd_confirmed;
+string config_path = "/etc/dhcpv6_pd.conf";
 
-object config = Config();
-object system_module = SystemTypes.SmartOS(config);
+object config;
+object system_module;
 
 class Config {
   string upstream_interface = "net0";
   string downstream_interface = "net1";
   int requested_prefix = 64;
   string leasefile = "/var/run/dhcpv6_pd_leases";
+
+  protected variant void create() {
+  }
+
+  protected variant void create(string config_path) {
+    DWRITE("Reading configuration from %s\n", config_path);
+    string cf = Stdio.read_file(config_path);
+    if(!cf) throw(Error.Generic(sprintf("Unable to read configuration file %s.\n", config_path)));
+    mixed json = Standards.JSON.decode(cf);
+    if(!json || !mappingp(json)) throw(Error.Generic(sprintf("No data in configuration file %s.\n", config_path)));
+    // we basically override defaults
+    foreach(json; string key; mixed value) {
+      if(has_index(this, key)) {
+         this[key] = value;
+      } else {
+         throw(Error.Generic(sprintf("Invalid parameter %s in configuration file %s.\n", key, config_path)));
+      }
+    }
+    DWRITE("Configuration: %O\n", this);
+  }
 }
 
 int main(int argc, array argv) {
+  parse_options(argc, argv);
+  load_config(config_path);
+
   identifier = system_module->get_identifier();
   duid = DUID(0, identifier);
   werror("Identifier: %O\n", identifier);

          
@@ 91,6 121,24 @@ int main(int argc, array argv) {
   return -1;
 }
 
+void parse_options(int argc, array argv) {
+    array opts = Getopt.find_all_options(argv, ({ ({"config", Getopt.HAS_ARG, ({"-f", "--config", "--config-file"}), ({"DHCP6_PD_CONFIG_FILE"}) }) }));
+    foreach(opts;;array opt) {
+      switch(opt[0]) {
+        case "config":
+          config_path = opt[1];
+          break;
+        default:
+          werror("ignoring unknown command option %s\n", opt[0]);
+      }
+    }
+}
+
+void load_config(string config_path) {
+  config = Config(config_path);
+  system_module = SystemTypes.get_module_for_system(config);
+}
+
 void do_shutdown() {
   werror("Shut down in progress.\n");
   keep_trying = 0;