//
#include "config.h"
+
+// Define allowed command line and config file options
Config::Config()
{
- // define valid command line parameters
+ // Define valid command line parameters
opt_desc_cmd = new po::options_description("Command line options");
-
opt_desc_cmd->add_options()
("help,?","Show help.")
("version,v","Show version.")
("password,p",po::value<string>(),"Set the password.")
;
- // define valid config file parameters
- opt_desc_conf = new po::options_description("Config file options");
+ // Define valid config file main section parameters
+ opt_desc_conf_main = new po::options_description("Config file main section options");
+ opt_desc_conf_main->add_options()
+ ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
+ ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
+ ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
+ ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
+ ;
- opt_desc_conf->add_options()
- ("help,?","Show help.")
- ("version,v","Show version.")
+ // Define valid config file main section parameters
+ opt_desc_conf_service = new po::options_description("Config file service section options");
+ opt_desc_conf_service->add_options()
+ ("service.protocol",po::value<string>(),"The service protocol.")
+ ("service.host",po::value<string>(),"The hostname to update.")
+ ("service.login",po::value<string>(),"Login name.")
+ ("service.password",po::value<string>(),"Corresponding password.")
;
}
+
Config::~Config()
{
delete opt_desc_cmd;
- delete opt_desc_conf;
+ delete opt_desc_conf_main;
+ delete opt_desc_conf_service;
}
+
int Config::parse_cmd_line(int argc, char *argv[])
{
try
{
po::variables_map vm;
- po::store(po::parse_command_line(argc, argv, *opt_desc_cmd), vm);
+ po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm);
po::notify(vm);
if(vm.count("help"))
else if(vm.count("version"))
return 2;
+ // Are all needed options set to create a Service Object
if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
{
- // get the cmd parameter values for protocol host login and password
+ // Get the cmd parameter values for protocol host login and password
string protocol = vm["protocol"].as<string>();
string host = vm["host"].as<string>();
string login = vm["login"].as<string>();
string password = vm["password"].as<string>();
- //TODO: convert all options to lowercase
+ //TODO: convert protocol option to lowercase
- // create Service object
+ // Create corresponding Service object TODO: swap to extra method
if(protocol == "dhs")
{
Service * service = new DHS(host,login,password);
return 1;
}
}
- catch(boost::program_options::unknown_option e)
+ catch(po::unknown_option e)
{
cout << "Unknown option set." << endl;
return 1;
return 0;
}
-void Config::print_usage()
+
+int Config::load_config_from_files(string config_path)
{
- cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
- cout << *opt_desc_cmd << endl;
+ // first load the main config file bpdyndnsd.conf
+ string main_conf_file = config_path.append("/bpdyndnsd.conf");
+ ifstream config_file (main_conf_file.c_str(),ifstream::in);
+ if(config_file.is_open())
+ {
+ try
+ {
+ // parse the config file
+ po::variables_map vm;
+ po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
+ po::store(parsed,vm);
+ po::notify(vm);
+
+ if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
+ {
+ // Get the options from main section
+ this->daemon_mode = vm["main.daemon_mode"].as<bool>();
+ this->logfile = vm["main.logfile"].as<string>();
+ this->loglevel = vm["main.loglevel"].as<int>();
+ this->syslog = vm["main.syslog"].as<bool>();
+ }
+ }
+ catch(po::unknown_option e)
+ {
+ // unknown option in config file detected
+ config_file.close();
+ cout << "Unknown option in config file detected!" << endl;
+ return 3;
+ }
+ config_file.close();
+ }
+ else
+ {
+ cout << "Can't open main config file for reading: " << main_conf_file << endl;
+ return 4;
+ }
+
+ // then load all service definition files in config path
+
+ // TODO: code to load service definition files in config path
}
-void Config::print_version(string version)
+
+list<Service*> Config::get_services()
{
- cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;
+ return this->services;
}
-void Config::load_config_file(string filename)
-{
+void Config::print_usage()
+{
+ cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
+ cout << *opt_desc_cmd << endl;
}
-list<Service*> Config::get_services()
+
+void Config::print_version(string version)
{
- return this->services;
+ cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;
}