No need to initialize strings with "".
[bpdyndnsd] / src / updater.cpp
CommitLineData
b1be615b
BS
1/** @file
2 * @brief The updater class implementation. This class implements the updater logic.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
4545a371
BS
10#include "updater.h"
11
e0080b78
BS
12#include "serviceholder.h"
13#include "dhs.h"
14#include "ods.h"
15
88a594e8
BS
16#include <boost/foreach.hpp>
17
e0080b78
BS
18
19// Following boost macros are needed for serialization of derived classes through a base class pointer (Service *).
20BOOST_CLASS_EXPORT_GUID(ODS, "ODS")
21BOOST_CLASS_EXPORT_GUID(DHS, "DHS")
b6228761 22BOOST_CLASS_EXPORT_GUID(DYNDNS, "DYNDNS")
e0080b78
BS
23
24namespace fs = boost::filesystem;
25
8bca3c5d 26using namespace std;
527536b3 27
b1be615b
BS
28/**
29 * Default constructor which initializes the member Conf.
30 */
4545a371 31Updater::Updater()
019dc0d9 32 : IPHelp(new IPHelper)
4545a371 33{
2e956a36 34 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
88a594e8 35 Logger::Ptr _log(new Logger);
254bbf53 36 Log = _log;
0665b239 37 _log.reset();
4545a371 38
e0080b78
BS
39 // initialize Serviceholder
40 Serviceholder::Ptr _serviceholder(new Serviceholder(Log));
41 ServiceHolder = _serviceholder;
42 _serviceholder.reset();
43
254bbf53 44 // initialize Config
e0080b78 45 Config::Ptr _config(new Config(Log,ServiceHolder));
254bbf53 46 Conf = _config;
0665b239 47 _config.reset();
4545a371
BS
48}
49
527536b3 50
b1be615b
BS
51/**
52 * Default destructor.
53 */
4545a371
BS
54Updater::~Updater()
55{
56}
57
527536b3 58
efbde536
BS
59
60/**
61 * Load config and initialize helper classes.
62 * @param argc Number of arguments in array
63 * @param argv Array with cmd options
64 */
65int Updater::load_config(int argc, char *argv[])
66{
67 // load the cmd options
68 if ( init_config_from_cmd(argc,argv) != 0 )
69 return -1;
70
71 // load the config and service files
72 if ( init_config_from_files() != 0 )
73 return -1;
74
75 // init all helper classes
76 if ( init_helper_classes() != 0 )
77 return -1;
78
79 return 0;
80}
81
82
83/**
84 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
85 */
86int Updater::reload_config()
87{
88 // serialize all service objects
89 if ( ServiceHolder->serialize_services() != 0 )
90 return -1;
91
92 // delete all service objects
93 ServiceHolder->delete_services();
94
95 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
96 Conf->delete_variables_map();
97
98 // load only config files
99 if ( init_config_from_files() != 0 )
100 return -1;
101
102 // init all helper classes
103 if ( init_helper_classes() != 0 )
104 return -1;
105
106 return 0;
107}
108
109
b1be615b 110/**
254bbf53
BS
111 * Parse the command line arguments and initialize corresponding options.
112 * @param argc Number command line arguments.
113 * @param argv[] Array with arguments.
114 * @return 0 if cmd options successfully parsed, 1 if usage or version.
38060291 115 */
254bbf53 116int Updater::init_config_from_cmd(int argc, char *argv[])
38060291
BS
117{
118 // Load the command line parameters
e95a6634 119 if( Conf->parse_cmd_line( argc, argv) != 0)
667c672c 120 return -1;
38060291 121
59c8d63c
BS
122 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
123 init_log_facility();
124
254bbf53
BS
125 // successful parsed
126 Log->print_cmd_parsed();
38060291
BS
127 return 0;
128}
129
130
131/**
254bbf53
BS
132 * Load the main config and the service definition files in config path.
133 * @return 0 if all is fine,
b1be615b 134 */
254bbf53 135int Updater::init_config_from_files()
4545a371 136{
254bbf53 137 // Load the main and service config files in config path
e95a6634 138 if ( Conf->load_config_from_files() != 0 )
667c672c 139 return -1;
4545a371 140
59c8d63c
BS
141 // Re-init log facility, perhaps new config file options for logger are set.
142 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
143 init_log_facility();
144
27baf279 145 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
e0080b78 146 if ( ServiceHolder->deserialize_services() != 0 )
667c672c 147 return -1;
27baf279 148
254bbf53 149 // successful loaded
667c672c 150 Log->print_conf_files_parsed();
254bbf53 151 return 0;
4545a371
BS
152}
153
527536b3 154
b1be615b 155/**
efbde536
BS
156 * Init all Helper classes
157 * @return
3434b35f 158 */
efbde536 159int Updater::init_helper_classes()
3434b35f 160{
efbde536
BS
161 // Initialize IPHelper
162 if ( init_ip_helper() != 0 )
163 return -1;
3434b35f 164
efbde536 165 return 0;
3434b35f
BS
166}
167
168
169/**
0665b239
BS
170 * Init the IPHelp member with needed values.
171 * @return 0 if all is fine, -1 otherwise.
172 */
173int Updater::init_ip_helper()
174{
019dc0d9 175 // initialize IPHelper
4eb87664 176 IPHelper::Ptr _iphelp(new IPHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6(),Conf->get_proxy(),Conf->get_proxy_port()));
019dc0d9
BS
177 IPHelp = _iphelp;
178 _iphelp.reset();
0665b239
BS
179
180 return 0;
181}
182
183
184/**
efbde536
BS
185 * Getter for member Config.
186 * @return Member Config.
187 */
188Config::Ptr Updater::get_config() const
189{
190 return Conf;
191}
667c672c 192
efbde536
BS
193
194/**
195 * Getter for member Logger.
196 * @return Member Logger.
197 */
198Logger::Ptr Updater::get_logger() const
199{
200 return Log;
8bca3c5d
BS
201}
202
203
2bc1878a
BS
204/**
205 * Initialize the logging facility with loglevel and syslog.
206 */
8bca3c5d
BS
207void Updater::init_log_facility()
208{
cbbdeb6c 209 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level());
8bca3c5d 210 Log->print_init_log_facility();
c5675c01
BS
211}
212
213
214/**
b1be615b
BS
215 * Update all configured services.
216 */
4545a371
BS
217void Updater::update_services()
218{
e0080b78 219 list<Service::Ptr> services = ServiceHolder->get_services();
4545a371 220
0665b239 221 string ip = IPHelp->get_actual_ip();
4545a371 222
3c0cd271 223 if ( !ip.empty() )
4545a371 224 {
3c0cd271
BS
225 BOOST_FOREACH(Service::Ptr &service, services )
226 {
c3dea5dc
BS
227 string dns_recheck_ip = ip;
228 int current_time = time(NULL);
229
230 int lastupdated = 0;
231 if ( service->get_last_updates()->size() > 0 )
232 lastupdated = service->get_last_updates()->front();
233
d5a516ba
BS
234 // If the dns cache ttl is expired or the service is updated for the first time, then get the actual ip of the dns record (this should be the IP in the last update)
235 if ( ((lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time)) || (lastupdated == 0) )
0541cd71
BS
236 {
237 Log->print_recheck_dns_entry(service->get_hostname(),lastupdated,service->get_dns_cache_ttl(),current_time);
238 string _dns_recheck_ip = IPHelp->dns_query(service->get_hostname());
239 Log->print_cached_dns_entry(service->get_hostname(), _dns_recheck_ip, service->get_actual_ip());
240 if (!_dns_recheck_ip.empty())
241 dns_recheck_ip = _dns_recheck_ip;
242 }
c3dea5dc 243
4eb87664
BS
244 // In case the local hosts IP (ip) differ from the IP set in the last update (actual_ip) or
245 // the IP of the dns record (dns_recheck_ip) differs from the IP of the local host (ip)
246 // then perform an update. This implies that the update is not performed if actual_ip == dns_recheck_ip.
d5a516ba
BS
247 // Special case when latupdated == 0 then only perform the update if dns_rechek_ip differs from ip
248 if ( ((service->get_actual_ip() != ip) && (lastupdated != 0)) || (dns_recheck_ip != ip) )
c3dea5dc 249 service->update(ip,current_time);
d5a516ba
BS
250 else if ( (service->get_actual_ip() != ip) && (lastupdated == 0) )
251 service->set_actual_ip(ip);
3c0cd271 252 }
4545a371 253 }
b1be615b 254}
e0080b78
BS
255
256
257/**
258 * Getter for member ServiceHolder.
259 * @return ServiceHolder
260 */
261Serviceholder::Ptr Updater::get_service_holder() const
262{
263 return ServiceHolder;
264}