2 // C++ Implementation: config
7 // Author: Bjoern Sikora <bjoern.sikora@intra2net.com>, (C) 2009
9 // Copyright: See COPYING file that comes with this distribution
15 // Define allowed command line and config file options
21 // Define valid command line parameters
22 opt_desc_cmd = new po::options_description("Command line options");
23 opt_desc_cmd->add_options()
24 ("help,?","Show help.")
25 ("version,v","Show version.")
26 ("protocol,q",po::value<string>(),"Set the service protocol type.")
27 ("host,h",po::value<string>(),"Set the hostname to update.")
28 ("login,l",po::value<string>(),"Set the login.")
29 ("password,p",po::value<string>(),"Set the password.")
32 // Define valid config file main section parameters
33 opt_desc_conf_main = new po::options_description("Config file main section options");
34 opt_desc_conf_main->add_options()
35 ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
36 ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
37 ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
38 ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
41 // Define valid config file main section parameters
42 opt_desc_conf_service = new po::options_description("Config file service section options");
43 opt_desc_conf_service->add_options()
44 ("service.protocol",po::value<string>(),"The service protocol.")
45 ("service.host",po::value<string>(),"The hostname to update.")
46 ("service.login",po::value<string>(),"Login name.")
47 ("service.password",po::value<string>(),"Corresponding password.")
55 delete opt_desc_conf_main;
56 delete opt_desc_conf_service;
60 int Config::parse_cmd_line(int argc, char *argv[])
65 po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm);
70 else if(vm.count("version"))
73 // Are all needed options set to create a Service Object
74 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
76 // Get the cmd parameter values for protocol host login and password
77 string protocol = vm["protocol"].as<string>();
78 string host = vm["host"].as<string>();
79 string login = vm["login"].as<string>();
80 string password = vm["password"].as<string>();
82 //TODO: convert protocol option to lowercase
84 // Create corresponding Service object TODO: swap to extra method
87 Service * service = new DHS(host,login,password);
88 this->services.push_back(service);
90 else if(protocol == "ods")
92 Service * service = new ODS(host,login,password);
93 this->services.push_back(service);
98 cout << "Not all needed options set!\n" << endl;
102 catch(po::unknown_option e)
104 cout << "Unknown option set." << endl;
111 int Config::load_config_from_files(string config_path)
113 // first load the main config file bpdyndnsd.conf
114 string main_conf_file = config_path.append("/bpdyndnsd.conf");
115 ifstream config_file (main_conf_file.c_str(),ifstream::in);
116 if(config_file.is_open())
120 // parse the config file
121 po::variables_map vm;
122 po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
123 po::store(parsed,vm);
126 if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
128 // Get the options from main section
129 this->daemon_mode = vm["main.daemon_mode"].as<bool>();
130 this->logfile = vm["main.logfile"].as<string>();
131 this->loglevel = vm["main.loglevel"].as<int>();
132 this->syslog = vm["main.syslog"].as<bool>();
135 catch(po::unknown_option e)
137 // unknown option in config file detected
139 cout << "Unknown option in config file detected!" << endl;
146 cout << "Can't open main config file for reading: " << main_conf_file << endl;
150 // then load all service definition files in config path
152 // TODO: code to load service definition files in config path
157 list<Service*> Config::get_services()
159 return this->services;
163 void Config::print_usage()
165 cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
166 cout << *opt_desc_cmd << endl;
170 void Config::print_version(string version)
172 cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;