2 * @brief Config class implementation. This class represents the actual configuration.
6 * @copyright Intra2net AG
13 * Default Constructor. Available command line arguments with their default values are defined here.
20 // Define valid command line parameters
21 Opt_desc_cmd = new po::options_description("Command line options");
22 Opt_desc_cmd->add_options()
23 ("help,?","Show help.")
24 ("version,v","Show version.")
25 ("protocol,q",po::value<string>(),"Set the service protocol type.")
26 ("host,h",po::value<string>(),"Set the hostname to update.")
27 ("login,l",po::value<string>(),"Set the login.")
28 ("password,p",po::value<string>(),"Set the password.")
31 // Define valid config file main section parameters
32 Opt_desc_conf_main = new po::options_description("Config file main section options");
33 Opt_desc_conf_main->add_options()
34 ("main.daemon_mode",po::value<bool>()->default_value(false),"Run as system daemon.")
35 ("main.logfile",po::value<string>()->default_value("/var/log/bpdyndns.log"),"Where to log.")
36 ("main.loglevel",po::value<int>()->default_value(0),"Loglevel.")
37 ("main.syslog",po::value<bool>()->default_value(false),"Use syslog facility.")
40 // Define valid config file main section parameters
41 Opt_desc_conf_service = new po::options_description("Config file service section options");
42 Opt_desc_conf_service->add_options()
43 ("service.protocol",po::value<string>(),"The service protocol.")
44 ("service.host",po::value<string>(),"The hostname to update.")
45 ("service.login",po::value<string>(),"Login name.")
46 ("service.password",po::value<string>(),"Corresponding password.")
57 delete Opt_desc_conf_main;
58 delete Opt_desc_conf_service;
63 * Parses the command line arguments and does the needed actions.
64 * @param argc Command line argument number given to main.
65 * @param argv[] Pointer to command line argument array given to main.
66 * @return 0 if all is fine,
68 int Config::parse_cmd_line(int argc, char *argv[])
73 po::store(po::parse_command_line(argc, argv, *this->Opt_desc_cmd), vm);
78 else if(vm.count("version"))
81 // Are all needed options set to create a Service Object
82 if(vm.count("protocol") && vm.count("host") && vm.count("login") && vm.count("password"))
84 // Get the cmd parameter values for protocol host login and password
85 string protocol = vm["protocol"].as<string>();
86 string host = vm["host"].as<string>();
87 string login = vm["login"].as<string>();
88 string password = vm["password"].as<string>();
90 //TODO: convert protocol option to lowercase
92 // Create corresponding Service object TODO: swap to extra method
95 Service * service = new DHS(host,login,password);
96 this->services.push_back(service);
98 else if(protocol == "ods")
100 Service * service = new ODS(host,login,password);
101 this->services.push_back(service);
106 cout << "Not all needed options set!\n" << endl;
110 catch(po::unknown_option e)
112 cout << "Unknown option set." << endl;
120 * Loads the main and the service config file and does the needed action.
121 * @param config_path The path to the config directory.
122 * @return 0 if all is fine.
124 int Config::load_config_from_files(string config_path)
126 fs::path full_config_path = fs::system_complete(fs::path(config_path));
128 if( fs::exists(full_config_path) && fs::is_directory(full_config_path))
130 fs::directory_iterator end_iter;
131 for(fs::directory_iterator dir_itr(full_config_path); dir_itr != end_iter; ++dir_itr)
133 if( fs::is_regular_file(dir_itr->status()))
135 string filename = dir_itr->path().filename();
136 if(filename == "bpdyndnsd.conf")
138 cout << filename << endl;
146 cout << "Path doesn't exist or is not a directory" << endl;
157 // first load the main config file bpdyndnsd.conf
158 string main_conf_file = config_path.append("/bpdyndnsd.conf");
159 ifstream config_file (main_conf_file.c_str(),ifstream::in);
160 if(config_file.is_open())
164 // parse the config file
165 po::variables_map vm;
166 po::parsed_options parsed = po::parse_config_file(config_file,*this->opt_desc_conf_main,true);
167 po::store(parsed,vm);
170 if(vm.count("daemon_mode") && vm.count("logfile") && vm.count("loglevel") && vm.count("syslog"))
172 // Get the options from main section
173 this->daemon_mode = vm["main.daemon_mode"].as<bool>();
174 this->logfile = vm["main.logfile"].as<string>();
175 this->loglevel = vm["main.loglevel"].as<int>();
176 this->syslog = vm["main.syslog"].as<bool>();
179 catch(po::unknown_option e)
181 // unknown option in config file detected
183 cout << "Unknown option in config file detected!" << endl;
190 cout << "Can't open main config file for reading: " << main_conf_file << endl;
194 // then load all service definition files in config path
196 // TODO: code to load service definition files in config path
202 * Getter method for Service list member.
203 * @return Pointer to a list of Service's.
205 list<Service*> Config::get_services()
207 return this->Services;
212 * Prints out the usage to the command line.
214 void Config::print_usage()
216 cout << "Usage: bpdyndnsd [Command line options]" << "\n" << endl;
217 cout << *Opt_desc_cmd << endl;
222 * Prints out the programm name and the given version string on stdout.
223 * @param version Version string.
225 void Config::print_version(string version)
227 cout << "Bullet proof dynamic dns daemon.\nbpdyndnsd " << version << endl;