Save one more indent/if level
[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
4de6a9b8 10#include "updater.hpp"
4545a371 11
4de6a9b8 12#include "serviceholder.hpp"
e0080b78 13
e0080b78 14
ca5d6889
BS
15#include <boost/foreach.hpp>
16#include <boost/serialization/export.hpp>
b30f392d 17
e0080b78 18
8bca3c5d 19using namespace std;
527536b3 20
ca5d6889 21
b1be615b
BS
22/**
23 * Default constructor which initializes the member Conf.
24 */
4545a371 25Updater::Updater()
ad0e5016 26 : IPAddrHelp(new IPAddrHelper)
4545a371 27{
2e956a36 28 // Initialize program wide Logger, at this point we don't know where to log and with which loglevel.
c3c84086 29 Log = Logger::Ptr(new Logger);
4545a371 30
e0080b78 31 // initialize Serviceholder
c3c84086 32 ServiceHolder = Serviceholder::Ptr(new Serviceholder(Log));
e0080b78 33
254bbf53 34 // initialize Config
c3c84086 35 Conf = Config::Ptr(new Config(Log,ServiceHolder));
4545a371
BS
36}
37
527536b3 38
b1be615b
BS
39/**
40 * Default destructor.
41 */
4545a371
BS
42Updater::~Updater()
43{
44}
45
527536b3 46
efbde536
BS
47
48/**
49 * Load config and initialize helper classes.
50 * @param argc Number of arguments in array
51 * @param argv Array with cmd options
52 */
53int Updater::load_config(int argc, char *argv[])
54{
55 // load the cmd options
56 if ( init_config_from_cmd(argc,argv) != 0 )
57 return -1;
58
59 // load the config and service files
60 if ( init_config_from_files() != 0 )
61 return -1;
62
63 // init all helper classes
64 if ( init_helper_classes() != 0 )
65 return -1;
66
67 return 0;
68}
69
70
71/**
72 * Reloading the config. Delete all Service objects and then init new Service objects from config files.
73 */
74int Updater::reload_config()
75{
76 // serialize all service objects
77 if ( ServiceHolder->serialize_services() != 0 )
78 return -1;
79
80 // delete all service objects
81 ServiceHolder->delete_services();
82
83 // delete the actual Variables_map, perhaps with old cmd options which would overwrite new config file options.
84 Conf->delete_variables_map();
85
e8787e2e
BS
86 // clear the external send messages.
87 Log->clear_external_send_messages();
88
efbde536
BS
89 // load only config files
90 if ( init_config_from_files() != 0 )
91 return -1;
92
93 // init all helper classes
94 if ( init_helper_classes() != 0 )
95 return -1;
96
97 return 0;
98}
99
100
b1be615b 101/**
254bbf53
BS
102 * Parse the command line arguments and initialize corresponding options.
103 * @param argc Number command line arguments.
104 * @param argv[] Array with arguments.
105 * @return 0 if cmd options successfully parsed, 1 if usage or version.
38060291 106 */
08a5a621 107int Updater::init_config_from_cmd(int argc, char *argv[]) const
38060291
BS
108{
109 // Load the command line parameters
e95a6634 110 if( Conf->parse_cmd_line( argc, argv) != 0)
667c672c 111 return -1;
38060291 112
59c8d63c
BS
113 // If we have loaded the cmd options we need to init the log facility immediately in case debugging is enabled from cmd.
114 init_log_facility();
115
254bbf53
BS
116 // successful parsed
117 Log->print_cmd_parsed();
38060291
BS
118 return 0;
119}
120
121
122/**
254bbf53 123 * Load the main config and the service definition files in config path.
4de6a9b8 124 * @return 0 if all is fine,
b1be615b 125 */
08a5a621 126int Updater::init_config_from_files() const
4545a371 127{
254bbf53 128 // Load the main and service config files in config path
e95a6634 129 if ( Conf->load_config_from_files() != 0 )
667c672c 130 return -1;
4545a371 131
59c8d63c
BS
132 // Re-init log facility, perhaps new config file options for logger are set.
133 // These config file options will only overwrite the cmd options if the SIGHUP (reload config) is caught.
134 init_log_facility();
135
27baf279 136 // Here we are. All Service Objects should be initialized, so it is time to deserialize the old service objects and compare them.
e0080b78 137 if ( ServiceHolder->deserialize_services() != 0 )
667c672c 138 return -1;
27baf279 139
254bbf53 140 // successful loaded
667c672c 141 Log->print_conf_files_parsed();
254bbf53 142 return 0;
4545a371
BS
143}
144
527536b3 145
b1be615b 146/**
efbde536 147 * Init all Helper classes
4de6a9b8 148 * @return
3434b35f 149 */
efbde536 150int Updater::init_helper_classes()
3434b35f 151{
efbde536
BS
152 // Initialize IPHelper
153 if ( init_ip_helper() != 0 )
154 return -1;
3434b35f 155
efbde536 156 return 0;
3434b35f
BS
157}
158
159
160/**
0665b239
BS
161 * Init the IPHelp member with needed values.
162 * @return 0 if all is fine, -1 otherwise.
163 */
164int Updater::init_ip_helper()
165{
20399847
BS
166 // Try to get deserialized IPAddrHelper from ServiceHolder
167 IPAddrHelper::Ptr _ip_addr_help = ServiceHolder->get_ip_addr_helper();
168 if ( _ip_addr_help.use_count() != 0 )
169 {
170 // Initialize IPHelper
c3c84086 171 IPAddrHelp = IPAddrHelper::Ptr( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), _ip_addr_help->get_last_webcheck(), Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) );
20399847
BS
172 }
173 else
174 {
175 // IPAddrHelper from ServiceHolder was not declared, so init oen with LastWebcheck 0
08a5a621 176 IPAddrHelp = IPAddrHelper::Ptr( new IPAddrHelper( Log, Conf->get_webcheck_ip_url(), Conf->get_webcheck_ip_url_alt(), Conf->get_webcheck_interval(), (size_t)0, Conf->get_enable_ipv6(), Conf->get_proxy(), Conf->get_proxy_port() ) );
20399847
BS
177 }
178
179 // Put the IPAddrHelper into ServiceHolder, so the LastWebcheck state will be serialized too.
180 ServiceHolder->set_ip_addr_helper(IPAddrHelp);
0665b239
BS
181
182 return 0;
183}
184
185
186/**
efbde536
BS
187 * Getter for member Config.
188 * @return Member Config.
189 */
190Config::Ptr Updater::get_config() const
191{
192 return Conf;
193}
667c672c 194
efbde536
BS
195
196/**
197 * Getter for member Logger.
198 * @return Member Logger.
199 */
200Logger::Ptr Updater::get_logger() const
201{
202 return Log;
8bca3c5d
BS
203}
204
205
2bc1878a
BS
206/**
207 * Initialize the logging facility with loglevel and syslog.
208 */
c730deea 209void Updater::init_log_facility() const
8bca3c5d 210{
e8787e2e 211 Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level(),Conf->get_external_log_only_once());
8bca3c5d 212 Log->print_init_log_facility();
c5675c01
BS
213}
214
215
216/**
b1be615b
BS
217 * Update all configured services.
218 */
c730deea 219void Updater::update_services() const
4545a371 220{
1af7c124 221 // Get all services from the ServiceHolder.
e0080b78 222 list<Service::Ptr> services = ServiceHolder->get_services();
4545a371 223
1af7c124 224 // Get the actual IP of this host.
46fffdd6 225 string ip_host = IPAddrHelp->get_actual_ip(Conf->get_webcheck_enabled());
a7beeb20
TJ
226 if ( ip_host.empty() )
227 {
228 Log->print_no_wan_ip();
229 return;
230 }
4545a371 231
a7beeb20 232 BOOST_FOREACH(Service::Ptr &service, services )
4545a371 233 {
a7beeb20
TJ
234 string ip_last_update = service->get_actual_ip();
235 string hostname = service->get_hostname();
236 time_t lastupdated = 0;
237 time_t current_time = time(NULL);
c3dea5dc 238
a7beeb20
TJ
239 // Try to get the lastupdated time of the actual service if there is one.
240 if ( service->get_last_updates().size() > 0 )
241 lastupdated = service->get_last_updates().front(); /*lint !e1793 */
c3dea5dc 242
a7beeb20 243 Log->print_check_service_update(hostname, current_time, lastupdated);
1af7c124 244
a7beeb20
TJ
245 // Do a DNS Query for the dynamic hostname.
246 string ip_dns_recheck = IPAddrHelp->dns_query(hostname);
1af7c124 247
a7beeb20 248 Log->print_cached_dns_entry(hostname, ip_dns_recheck, ip_last_update, ip_host);
1af7c124 249
a7beeb20
TJ
250 // Test if the DNS-Record could not be found.
251 if ( ip_dns_recheck.empty() )
3fdee948
TJ
252 continue;
253
254 // Test if the actual DNS-Record differs from the host's IP.
255 if (ip_host == ip_dns_recheck )
a7beeb20 256 {
3fdee948
TJ
257 Log->print_no_update_needed(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
258 continue;
a7beeb20 259 }
3fdee948
TJ
260
261 // Check if the service will be updated for the first time.
262 if ( lastupdated == 0 )
263 {
264 // Update for the firt time.
265 Log->print_update_service_firttime(hostname, ip_dns_recheck, ip_host);
266 service->update(ip_host,current_time);
267 }
268 else
a7beeb20 269 {
3fdee948
TJ
270 // We already have updated, check if the IP set in last update differs from the actual host's ip.
271 if ( ip_last_update != ip_host )
a7c1e271 272 {
3fdee948
TJ
273 // Update
274 Log->print_update_service(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated);
a7beeb20 275 service->update(ip_host,current_time);
a7c1e271 276 }
a7beeb20 277 else
0541cd71 278 {
3fdee948
TJ
279 int dns_cache_ttl = service->get_dns_cache_ttl();
280 // Check if DNS Cache TTL is expired, if so, then update the same IP again.
281 if ( (lastupdated + dns_cache_ttl) < current_time )
1af7c124 282 {
a7beeb20 283 // Update
3fdee948 284 Log->print_update_service_ttl_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
1af7c124
BS
285 service->update(ip_host,current_time);
286 }
287 else
288 {
3fdee948
TJ
289 // DNS cache TTL isn't expired
290 Log->print_update_service_ttl_not_expired(hostname, ip_dns_recheck, ip_last_update, ip_host, lastupdated, dns_cache_ttl, current_time);
1af7c124
BS
291 }
292 }
3c0cd271 293 }
1d2e2f56 294 }
b1be615b 295}
e0080b78
BS
296
297
298/**
299 * Getter for member ServiceHolder.
300 * @return ServiceHolder
301 */
302Serviceholder::Ptr Updater::get_service_holder() const
303{
304 return ServiceHolder;
305}