# HG changeset patch # User William Welliver # Date 1634763907 14400 # Wed Oct 20 17:05:07 2021 -0400 # Node ID 1710bbd7d33c02ba5e37669788f40c01e5189c88 # Parent 52faa1f6ea304b0a1e8b48988abb27a1f0442f14 add configuration file and command option parsing diff --git a/dhcpv6_pd.conf b/dhcpv6_pd.conf new file mode 100644 --- /dev/null +++ b/dhcpv6_pd.conf @@ -0,0 +1,6 @@ +{ + "downstream_interface": "en0", + "upstream_interface": "en1", + "requested_prefix": 64, + "leasefile": "/var/run/dhcpv6_pd_leases" +} diff --git a/dhcpv6_pd.pike b/dhcpv6_pd.pike --- a/dhcpv6_pd.pike +++ b/dhcpv6_pd.pike @@ -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 @@ 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 @@ 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;