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
18 // Define valid command line parameters
19 opt_desc_cmd = new po::options_description("Command line options");
20 opt_desc_cmd->add_options()
21 ("help,?","Show help.")
22 ("version,v","Show version.")
23 ("protocol,q",po::value<string>(),"Set the service protocol type.")
24 ("host,h",po::value<string>(),"Set the hostname to update.")
25 ("login,l",po::value<string>(),"Set the login.")
26 ("password,p",po::value<string>(),"Set the password.")
29 // Define valid config file main section parameters
30 opt_desc_conf_main = new po::options_description("Config file main section options");
31 opt_desc_conf_main->add_options()
32 ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
33 ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
34 ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
35 ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
38 // Define valid config file main section parameters
39 opt_desc_conf_service = new po::options_description("Config file service section options");
40 opt_desc_conf_service->add_options()
41 ("service.protocol",po::value<string>(),"The service protocol.")
42 ("service.host",po::value<string>(),"The hostname to update.")
43 ("service.login",po::value<string>(),"Login name.")
44 ("service.password",po::value<string>(),"Corresponding password.")
52 delete opt_desc_conf_main;
53 delete opt_desc_conf_service;
57 int Config::parse_cmd_line(int argc, char *argv[])
62 po::store(po::parse_command_line(argc, argv, *this->opt_desc_cmd), vm);
67 else if(vm.count("version"))
70 // Are all needed options set to create a Service Object
71 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
73 // Get the cmd parameter values for protocol host login and password
74 string protocol = vm["protocol"].as<string>();
75 string host = vm["host"].as<string>();
76 string login = vm["login"].as<string>();
77 string password = vm["password"].as<string>();
79 //TODO: convert protocol option to lowercase
81 // Create corresponding Service object TODO: swap to extra method
84 Service * service = new DHS(host,login,password);
85 this->services.push_back(service);
87 else if(protocol == "ods")
89 Service * service = new ODS(host,login,password);
90 this->services.push_back(service);
95 cout << "Not all needed options set!\n" << endl;
99 catch(po::unknown_option e)
101 cout << "Unknown option set." << endl;
108 int Config::load_config_from_files(string config_path)
110 // first load the main config file bpdyndnsd.conf
111 string main_conf_file = config_path.append("/bpdyndnsd.conf");
112 ifstream config_file (main_conf_file.c_str(),ifstream::in);
113 if(config_file.is_open())
117 // parse the config file
118 po::variables_map vm;
119 po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
120 po::store(parsed,vm);
123 if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
125 // Get the options from main section
126 this->daemon_mode = vm["main.daemon_mode"].as<bool>();
127 this->logfile = vm["main.logfile"].as<string>();
128 this->loglevel = vm["main.loglevel"].as<int>();
129 this->syslog = vm["main.syslog"].as<bool>();
132 catch(po::unknown_option e)
134 // unknown option in config file detected
136 cout << "Unknown option in config file detected!" << endl;
143 cout << "Can't open main config file for reading: " << main_conf_file << endl;
147 // then load all service definition files in config path
149 // TODO: code to load service definition files in config path
153 list<Service*> Config::get_services()
155 return this->services;
159 void Config::print_usage()
161 cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
162 cout << *opt_desc_cmd << endl;
166 void Config::print_version(string version)
168 cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;