From c730deea994d183e6a39d284fd151184d6322c92 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Mon, 17 May 2010 18:02:33 +0200 Subject: [PATCH] Various fixes. --- src/httphelper.cpp | 56 +++++++++++++++++++++++++++++++++-------------- src/httphelper.h | 4 +- src/ip_addr_helper.cpp | 27 +++++++++++++++++------ src/ip_addr_helper.h | 5 +-- src/logger.cpp | 20 ++++++++-------- src/logger.h | 8 +++--- src/main.cpp | 10 ++++---- src/service.cpp | 4 +- src/service.h | 2 +- src/service_gnudip.cpp | 20 ++++++++++++---- src/service_ods.cpp | 35 +++++++++++++++++++++++------ src/serviceholder.cpp | 8 +++--- src/serviceholder.h | 2 +- src/tcp_service.cpp | 39 ++++++++++++++++----------------- src/updater.cpp | 8 +++--- src/updater.h | 4 +- src/util.cpp | 33 ++++++++++++++++++++++++--- src/util.h | 2 +- 18 files changed, 187 insertions(+), 100 deletions(-) diff --git a/src/httphelper.cpp b/src/httphelper.cpp index 8b8369f..e4c2ba6 100644 --- a/src/httphelper.cpp +++ b/src/httphelper.cpp @@ -19,8 +19,8 @@ HTTPHelper::HTTPHelper() : Log(new Logger) , ProxyPort(0) , CurlError(CURLE_OK) - , CurlEasyHandle(NULL) { + CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff); } @@ -41,7 +41,11 @@ HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_ CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff); if ( CurlEasyHandle != NULL ) { - set_curl_auth(_username,_password); + if ( set_curl_auth(_username,_password) != CURLE_OK ) + { + curl_easy_cleanup(CurlEasyHandle); + CurlEasyHandle = NULL; + } } } @@ -83,22 +87,25 @@ HTTPHelper::~HTTPHelper() */ long HTTPHelper::http_get(const string& url) { - CURLcode curl_err_code; long curl_info; if ( CurlEasyHandle != NULL ) { - set_curl_url(url); - - if ( (curl_err_code = curl_easy_perform(CurlEasyHandle) ) != CURLE_OK ) + if ( (CurlError = set_curl_url(url)) != CURLE_OK ) + { + Log->print_curl_error(url,CurlError,CurlErrBuff); + CurlWritedataBuff.clear(); + return -1; + } + if ( (CurlError = curl_easy_perform(CurlEasyHandle) ) != CURLE_OK ) { - Log->print_curl_error(url,curl_err_code,CurlErrBuff); + Log->print_curl_error(url,CurlError,CurlErrBuff); CurlWritedataBuff.clear(); return -1; } - if ( (curl_err_code = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != CURLE_OK ) + if ( (CurlError = curl_easy_getinfo(CurlEasyHandle,CURLINFO_RESPONSE_CODE,&curl_info)) != CURLE_OK ) { - Log->print_curl_error(url,curl_err_code); + Log->print_curl_error(url,CurlError); CurlWritedataBuff.clear(); return -1; } @@ -130,7 +137,7 @@ string HTTPHelper::get_curl_data() const * Initialized curl easy handle with a few options. * @param curl_writedata_buff Reference to a string wich will be filled with the curl result * @param curl_err_buff A pointer to an char array which will be filled with error message. - * @return A pointer to the easy curl handle. + * @return A pointer to the easy curl handle or NULL if something went wrong. */ CURL* HTTPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) { @@ -199,13 +206,22 @@ bool HTTPHelper::is_initialized() /** * Sets a url to the easy curl handle * @param url The url to set. + * @return CURLcode CURLE_OK if everything is right. */ -void HTTPHelper::set_curl_url(const string& url) +CURLcode HTTPHelper::set_curl_url(const string& url) { - if ( CurlEasyHandle != NULL ) + if ( (CurlEasyHandle != NULL) && (CurlError == CURLE_OK) ) { - curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str()); + CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_URL,url.c_str()); + if ( CurlError != CURLE_OK ) + { + // Some options could not be set, so destroy the CURL handle. + Log->print_curl_error_init("Could not set CURL URL properly.",CurlError); + curl_easy_cleanup(CurlEasyHandle); + CurlEasyHandle = NULL; + } } + return CurlError; } @@ -213,14 +229,20 @@ void HTTPHelper::set_curl_url(const string& url) * Sets HTTP AUTH parameters * @param username The username for HTTP AUTH * @param password The password for HTTP AUTH + * @return CURLcode CURLE_OK if everything is right. */ -void HTTPHelper::set_curl_auth(const string& username, const string& password) +CURLcode HTTPHelper::set_curl_auth(const string& username, const string& password) { - if ( CurlEasyHandle != NULL ) + if ( (CurlEasyHandle != NULL) && (CurlError == CURLE_OK) ) { - curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str()); - curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str()); + CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str()); + if ( CurlError != CURLE_OK ) + Log->print_curl_error_init("Could not set CURL username properly.",CurlError); + CurlError = curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str()); + if ( CurlError != CURLE_OK ) + Log->print_curl_error_init("Could not set CURL password properly.",CurlError); } + return CurlError; } diff --git a/src/httphelper.h b/src/httphelper.h index dd08dd4..9a1e047 100644 --- a/src/httphelper.h +++ b/src/httphelper.h @@ -30,8 +30,8 @@ private: std::string ReceivedCurlData; char CurlErrBuff[CURL_ERROR_SIZE]; - void set_curl_url(const std::string& url); - void set_curl_auth(const std::string& username, const std::string& password); + CURLcode set_curl_url(const std::string& url); + CURLcode set_curl_auth(const std::string& username, const std::string& password); CURL* init_curl(std::string& curl_writedata_buff, char* curl_err_buff); public: diff --git a/src/ip_addr_helper.cpp b/src/ip_addr_helper.cpp index 5a02737..e0e8dd1 100644 --- a/src/ip_addr_helper.cpp +++ b/src/ip_addr_helper.cpp @@ -377,7 +377,7 @@ string IPAddrHelper::webcheck_ip() time_t current_time = time(NULL); // Test if webcheck is allowed due to webcheck_interval. - if ( (LastWebcheck + (WebcheckInterval*60)) >= current_time ) + if ( (LastWebcheck + ((time_t)(WebcheckInterval*60))) >= current_time ) { // Webcheck not allowed, log it and return empty string. Log->print_webcheck_exceed_interval( LastWebcheck, (WebcheckInterval*60), current_time ); @@ -408,10 +408,11 @@ string IPAddrHelper::webcheck_ip() // Set URL actual_url = url_list.front(); url_list.pop_front(); - set_curl_url(curl_easy_handle,actual_url); - - // Perform curl operation, err_code of 1 indicated connection problem, so try next url. - curl_err_code = perform_curl_operation(curl_easy_handle, curl_err_buff, actual_url); + if (set_curl_url(curl_easy_handle,actual_url) == CURLE_OK ) + { + // Perform curl operation, err_code of 1 indicated connection problem, so try next url. + curl_err_code = perform_curl_operation(curl_easy_handle, curl_err_buff, actual_url); + } } // Cleanup CURL handle @@ -521,10 +522,22 @@ int IPAddrHelper::perform_curl_operation(CURL * curl_easy_handle, const char* cu * Sets a url to the easy curl handle * @param curl_easy_handle The easy curl handle to set the url for. * @param url The url to set. + * @return CURLcode Curl Error code, CURLE_OK if everything is right. */ -void IPAddrHelper::set_curl_url(CURL * curl_easy_handle, const string& url) const +CURLcode IPAddrHelper::set_curl_url(CURL * curl_easy_handle, const string& url) const { - curl_easy_setopt(curl_easy_handle,CURLOPT_URL,url.c_str()); + CURLcode curl_error = CURLE_OK; + + if ( curl_easy_handle != NULL ) + { + curl_error = curl_easy_setopt(curl_easy_handle,CURLOPT_URL,url.c_str()); + if ( curl_error != CURLE_OK ) + { + // Some options could not be set, so destroy the CURL handle. + Log->print_curl_error_init("Could not set CURL URL properly.",curl_error); + } + } + return curl_error; } diff --git a/src/ip_addr_helper.h b/src/ip_addr_helper.h index f8fcaf1..3cb339d 100644 --- a/src/ip_addr_helper.h +++ b/src/ip_addr_helper.h @@ -35,7 +35,7 @@ private: std::string WebcheckIpUrl; std::string WebcheckIpUrlAlt; int WebcheckInterval; - int LastWebcheck; + time_t LastWebcheck; std::string Proxy; int ProxyPort; bool UseIPv6; @@ -47,6 +47,7 @@ private: CURL * init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const; int perform_curl_operation(CURL * curl_easy_handle, const char* curl_err_buff, const std::string& actual_url) const; std::string parse_ipv4(const std::string& data) const; + CURLcode set_curl_url(CURL * curl_easy_handle, const std::string& url) const; public: @@ -67,8 +68,6 @@ public: // libcurl is a C library, so we have to make the callback member function static :-( static size_t http_receive(const char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer); - void set_curl_url(CURL * curl_easy_handle, const std::string& url) const; - int get_last_webcheck() const; }; diff --git a/src/logger.cpp b/src/logger.cpp index 4c9075c..e6f1f15 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -1161,7 +1161,7 @@ void Logger::print_service_not_initialized(const std::string& service) const * @param msg The error message * @param curl_err_code The resulting curl error code */ -void Logger::print_curl_error_init(const std::string& msg, const CURLcode curl_err_code) const +void Logger::print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const { string curl_err = ""; @@ -1174,7 +1174,7 @@ void Logger::print_curl_error_init(const std::string& msg, const CURLcode curl_e if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error: " << msg << " Curl error code: " << curl_err_code << " "<< curl_err << endl; + msg << "Curl error: " << err_msg << " Curl error code: " << curl_err_code << " "<< curl_err << endl; // lint !e 641 log_error(msg.str()); } } @@ -1202,7 +1202,7 @@ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) c if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << endl; + msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << endl; // lint !e 641 log_warning(msg.str(),level); } } @@ -1214,15 +1214,15 @@ void Logger::print_curl_error(const string& url, const CURLcode curl_err_code) c * @param curl_err_code The resulting curl error code * @param curl_err_buff The curl error buffer */ -void Logger::print_curl_error(const string& url, const int curl_err_code, const char * curl_err_buff) const +void Logger::print_curl_error(const string& url, const CURLcode curl_err_code, const char * curl_err_buff) const { string curl_err = ""; - if ( curl_err_code == 3 ) + if ( curl_err_code == CURLE_URL_MALFORMAT ) curl_err = "CURLE_URL_MALFORMAT"; - else if ( curl_err_code == 6 ) + else if ( curl_err_code == CURLE_COULDNT_RESOLVE_HOST ) curl_err = "CURLE_COULDNT_RESOLVE_HOST"; - else if ( curl_err_code == 7 ) + else if ( curl_err_code == CURLE_COULDNT_CONNECT ) curl_err = "CURLE_COULDNT_CONNECT"; else curl_err = "UNKNOWN"; @@ -1231,7 +1231,7 @@ void Logger::print_curl_error(const string& url, const int curl_err_code, const if ( (level <= Loglevel) || ((level <= ExternalWarningLevel) && (!ExternalWarningLog.empty())) ) { ostringstream msg; - msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << " " << curl_err_buff << endl; + msg << "Curl error while requesting following url: " << url << " Curl error code: " << curl_err_code << " "<< curl_err << " " << curl_err_buff << endl; // lint !e 641 log_warning(msg.str(),level); } } @@ -1533,7 +1533,7 @@ void Logger::print_error_parsing_cmd(const string& error) const * @param webcheck_interval Webcheck interval time. * @param current_time Current system time. */ -void Logger::print_webcheck_exceed_interval( const int last_webcheck, const int webcheck_interval, const time_t current_time ) const +void Logger::print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const { int level = 0; if ( level <= Loglevel ) @@ -1551,7 +1551,7 @@ void Logger::print_webcheck_exceed_interval( const int last_webcheck, const int * @param current_time Current time * @param lastupdated Last updated */ -void Logger::print_check_service_update(const string& hostname, const int current_time, const int lastupdated) const +void Logger::print_check_service_update(const string& hostname, const time_t current_time, const time_t lastupdated) const { int level = 1; if ( level <= Loglevel ) diff --git a/src/logger.h b/src/logger.h index 5ab2f0d..3c3c85d 100644 --- a/src/logger.h +++ b/src/logger.h @@ -180,11 +180,11 @@ public: void print_no_domain_part(const std::string& hostname) const; - void print_curl_error_init(const std::string& msg, const CURLcode curl_err_code) const; + void print_curl_error_init(const std::string& err_msg, const CURLcode curl_err_code) const; void print_curl_error(const std::string& url, const CURLcode curl_err_code) const; - void print_curl_error(const std::string& url, const int curl_err_code, const char * curl_err_buff) const; + void print_curl_error(const std::string& url, const CURLcode curl_err_code, const char * curl_err_buff) const; void print_curl_data(const std::string& curl_writedata_buff) const; @@ -224,9 +224,9 @@ public: void print_error_parsing_cmd(const std::string& error) const; - void print_webcheck_exceed_interval( const int last_webcheck, const int webcheck_interval, const time_t current_time ) const; + void print_webcheck_exceed_interval( const time_t last_webcheck, const int webcheck_interval, const time_t current_time ) const; - void print_check_service_update(const std::string& hostname, const int current_time, const int lastupdated) const; + void print_check_service_update(const std::string& hostname, const time_t current_time, const time_t lastupdated) const; void print_cached_dns_entry(const std::string& hostname, const std::string& ip_dns_recheck, const std::string& ip_last_update, const std::string& ip_host) const; diff --git a/src/main.cpp b/src/main.cpp index bc9ebb9..42a40b3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -221,16 +221,16 @@ int init_daemon_mode(bool daemon_mode) if ( kill(pid,SIGTERM) != 0 ) { updater->get_logger()->print_error_kill_child(pid); - shutdown_parent(false,-1); // keep pidfile + shutdown_parent(false,-1); // lint !e 534 // keep pidfile } else { updater->get_logger()->print_child_killed(pid); - shutdown_parent(true,-1); // remove pidfile + shutdown_parent(true,-1); // lint !e 534 // remove pidfile } } updater->get_logger()->print_runnig_as_daemon(pid); - shutdown_parent(false,0); // keep pidfile + shutdown_parent(false,0); // lint !e 534 // keep pidfile } // child starts here } @@ -238,7 +238,7 @@ int init_daemon_mode(bool daemon_mode) { if ( write_pidfile(getpid()) != 0 ) { - shutdown(); + shutdown(); // lint !e 534 exit(-1); } } @@ -288,7 +288,7 @@ int main(int argc, char *argv[]) // Snore, snore... don't hug the cpu if we are in daemon_mode. if ( updater->get_config()->get_daemon_mode() == 1 ) - sleep(10); + sleep(10); // lint !e 534 }while ( updater->get_config()->get_daemon_mode() == 1 ); diff --git a/src/service.cpp b/src/service.cpp index 8fb7752..519c655 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -147,7 +147,7 @@ void Service::set_last_updates(std::list _last_updates) * Getter for member Lastupdated. * @return Value of member Lastupdated. */ -list Service::get_last_updates() const +const list Service::get_last_updates() const { return LastUpdates; } @@ -210,7 +210,7 @@ bool Service::update_allowed(const time_t current_time) for (iter = LastUpdates.begin(); (iter != LastUpdates.end()) && ( i < MaxUpdatesWithinInterval ); iter++) { - if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + (UpdateInterval*60)) >= current_time ) ) + if ( (i == (MaxUpdatesWithinInterval-1)) && ( (*iter + ((time_t)(UpdateInterval*60))) >= current_time ) ) { Log->print_update_not_allowed(current_time,*iter,MaxUpdatesWithinInterval,get_service_name()); return false; diff --git a/src/service.h b/src/service.h index e2858f4..850ddb9 100644 --- a/src/service.h +++ b/src/service.h @@ -92,7 +92,7 @@ public: std::string get_password() const; void set_last_updates(std::list _last_updates); - std::list get_last_updates() const; + const std::list get_last_updates() const; void set_actual_ip(const std::string& _actual_ip); std::string get_actual_ip() const; diff --git a/src/service_gnudip.cpp b/src/service_gnudip.cpp index 5a2cfd5..a63a787 100644 --- a/src/service_gnudip.cpp +++ b/src/service_gnudip.cpp @@ -108,7 +108,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the salt out of received http data if ( boost::regex_search(curl_data,matches,expr_salt) ) { - response.insert(pair("salt",matches[1].str())); + response.insert(pair("salt",matches[1].str())); // lint !e 534 get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else @@ -121,7 +121,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the time out of received http data if ( boost::regex_search(curl_data,matches,expr_time) ) { - response.insert(pair("time",matches[1].str())); + response.insert(pair("time",matches[1].str())); // lint !e 534 get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else @@ -134,7 +134,7 @@ map ServiceGnudip::parse_initial_request(const string& curl_data) // Get the sign out of received http data if ( boost::regex_search(curl_data,matches,expr_sign) ) { - response.insert(pair("sign",matches[1].str())); + response.insert(pair("sign",matches[1].str())); // lint !e 534 get_logger()->print_regex_match(expr_salt.str(),matches[1].str()); } else @@ -232,11 +232,16 @@ int ServiceGnudip::perform_update(const std::string& ip) { pw_md5_hex = Util::compute_md5_digest(get_password()); } - catch ( invalid_argument& e ) + catch ( exception& e ) { get_logger()->print_exception_md5_sum(e.what()); return -1; } + catch ( ... ) + { + get_logger()->print_exception_md5_sum("Unknown exception"); + return -1; + } // append "." and salt and compute md5 sum and get the HEX representation pw_md5_hex.append("."); @@ -247,11 +252,16 @@ int ServiceGnudip::perform_update(const std::string& ip) { secret = Util::compute_md5_digest(pw_md5_hex); } - catch ( invalid_argument& e ) + catch ( exception& e ) { get_logger()->print_exception_md5_sum(e.what()); return -1; } + catch ( ... ) + { + get_logger()->print_exception_md5_sum("Unknown exception"); + return -1; + } // Now its time to issue the second http_get operation string url = assemble_update_url(salt, sign_time, sign, secret, ip); diff --git a/src/service_ods.cpp b/src/service_ods.cpp index 26d862d..7515e0f 100644 --- a/src/service_ods.cpp +++ b/src/service_ods.cpp @@ -11,8 +11,6 @@ #include "net_helper.h" #include "util.h" -#include - using namespace std; @@ -82,18 +80,27 @@ int ServiceOds::perform_update(const std::string& ip) // Receive the server greeting. string server_greeting = connection->receive_data(); if ( server_greeting.empty() ) + { + connection->close_connection(); return -1; + } // Send login command ostringstream login; login << "LOGIN " << get_login() << " " << get_password() << endl; - if (connection->send_data(login.str())) + if (connection->send_data(login.str()) != 0) + { + connection->close_connection(); return -1; + } // Receive login reply string login_reply = connection->receive_data(); if ( login_reply.empty() ) + { + connection->close_connection(); return -1; + } else { // Get the return code out of the received data @@ -117,34 +124,46 @@ int ServiceOds::perform_update(const std::string& ip) // Successfully loged in, status_code should be 225 // Perform delete operation until there is no RR or we have performed it 10 times. - int count = 0; + int del_count = 0; string delete_reply, reply_code; ostringstream delete_request; delete_request << "DELRR " << get_hostname() << " A " << endl; do { // Send delete request - if (connection->send_data(delete_request.str())) + if (connection->send_data(delete_request.str()) != 0) + { + connection->close_connection(); return -1; + } // Get delete reply delete_reply = connection->receive_data(); if ( delete_reply.empty() ) + { + connection->close_connection(); return -1; - count++; + } + del_count++; reply_code = Util::parse_status_code(delete_reply); - }while ( (count < 10) && (reply_code != "300") ); + }while ( (del_count < 10) && (reply_code != "300") ); // Send update request ostringstream update_request; update_request << "ADDRR " << get_hostname() << " A " << ip << endl; - if (connection->send_data(update_request.str())) + if (connection->send_data(update_request.str()) != 0) + { + connection->close_connection(); return -1; + } // Receive update request server reply string update_reply = connection->receive_data(); if ( update_reply.empty() ) + { + connection->close_connection(); return -1; + } else { // Get the return code out of the received data diff --git a/src/serviceholder.cpp b/src/serviceholder.cpp index fbf1ba3..9cde9fe 100644 --- a/src/serviceholder.cpp +++ b/src/serviceholder.cpp @@ -53,21 +53,21 @@ Serviceholder::~Serviceholder() * This function serializes all Service objects in Services and OldServices (where the update Timeout isn't expired) into a specified file. * @return 0 if all is fine, -1 if output file could not be opened for reading or error while serializing. */ -int Serviceholder::serialize_services() +int Serviceholder::serialize_services() const { // Put Services and OldServices into Serviceholder. SerializeServiceContainer::Ptr service_container(new SerializeServiceContainer); - BOOST_FOREACH(Service::Ptr &service, Services) + BOOST_FOREACH(const Service::Ptr &service, Services) { service_container->add_service(service); } time_t current_time = time(NULL); - BOOST_FOREACH(Service::Ptr &service, OldServices) + BOOST_FOREACH(const Service::Ptr &service, OldServices) { - if ( ( service->get_last_updates().front() + (service->get_update_interval()*60) ) >= current_time ) // UpdateInterval timeout of service isn't expired. + if ( ( service->get_last_updates().front() + ((time_t)(service->get_update_interval()*60) )) >= current_time ) // UpdateInterval timeout of service isn't expired. service_container->add_service(service); } diff --git a/src/serviceholder.h b/src/serviceholder.h index c267966..5b3c5b7 100644 --- a/src/serviceholder.h +++ b/src/serviceholder.h @@ -45,7 +45,7 @@ public: int deserialize_services(); - int serialize_services(); + int serialize_services() const; void delete_services(); diff --git a/src/tcp_service.cpp b/src/tcp_service.cpp index 7a6e3d0..32056f9 100644 --- a/src/tcp_service.cpp +++ b/src/tcp_service.cpp @@ -42,20 +42,20 @@ TCPService::~TCPService() void TCPService::connect(const std::string& _host, const std::string& _port) throw (boost::system::system_error) { // Init boost::system::error_code - boost::system::error_code error_code; + boost::system::error_code err_code; // Create service object. The IO_Service has to be a member //boost::asio::io_service io_service; // Init DNS query object - boost::asio::ip::tcp::resolver resolver(IOService); - boost::asio::ip::tcp::resolver::query query(_host, _port); - boost::asio::ip::tcp::resolver::iterator end; + boost::asio::ip::tcp::resolver service_resolver(IOService); + boost::asio::ip::tcp::resolver::query dns_query(_host, _port); + boost::asio::ip::tcp::resolver::iterator end_iter; // Perform the DNS query - boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query, error_code); - if ( error_code ) - throw boost::system::system_error(error_code); + boost::asio::ip::tcp::resolver::iterator endpoint_iterator = service_resolver.resolve(dns_query, err_code); + if ( err_code ) + throw boost::system::system_error(err_code); // Init ssl context for service object boost::asio::ssl::context ctx(IOService, boost::asio::ssl::context::sslv23); @@ -65,21 +65,21 @@ void TCPService::connect(const std::string& _host, const std::string& _port) thr Socket = SocketPtr(new SSLStream(IOService,ctx)); // Try to connect the Socket through all available endpoints - if (endpoint_iterator != end) + if (endpoint_iterator != end_iter) { do { Socket->lowest_layer().close(); - Socket->lowest_layer().connect(*endpoint_iterator++, error_code); - } while ( error_code && (endpoint_iterator != end) ); + Socket->lowest_layer().connect(*endpoint_iterator++, err_code); + } while ( err_code && (endpoint_iterator != end_iter) ); } - if ( error_code ) - throw boost::system::system_error(error_code); + if ( err_code ) + throw boost::system::system_error(err_code); // Perform SSL handshake - Socket->handshake(boost::asio::ssl::stream_base::client,error_code); - if ( error_code ) - throw boost::system::system_error(error_code); + Socket->handshake(boost::asio::ssl::stream_base::client,err_code); + if ( err_code ) + throw boost::system::system_error(err_code); return; } @@ -98,12 +98,11 @@ std::string TCPService::read_from_socket() throw (boost::system::system_error) // Do not limit the receive buffer size for reusability. By default, max_size == 18446744073709551615 boost::asio::streambuf stream_buffer; - // Init bytes_read and the error_code - unsigned int bytes_read = 0; + // Init error_code boost::system::error_code error_code; // Is blocking until all available data was read or buffer is full - bytes_read = boost::asio::read(*Socket.get(),stream_buffer,boost::asio::transfer_at_least(1),error_code); + size_t bytes_read = boost::asio::read(*Socket.get(),stream_buffer,boost::asio::transfer_at_least(1),error_code); if ( error_code == boost::asio::error::eof ) throw boost::system::system_error(boost::system::error_code(ECONNABORTED,boost::system::system_category),"Connection closed by peer."); else if (error_code) @@ -129,7 +128,7 @@ std::string TCPService::read_from_socket() throw (boost::system::system_error) void TCPService::write_to_socket(const string& data) throw (boost::system::system_error) { // Get the data size which will be written - unsigned int data_size = data.size(); + size_t data_size = data.size(); // Init stream_buffer with data boost::asio::streambuf stream_buffer(data_size); @@ -137,7 +136,7 @@ void TCPService::write_to_socket(const string& data) throw (boost::system::syste request_stream << data; // Init bytes_read and the error_code - unsigned int bytes_wrote = 0; + size_t bytes_wrote = 0; boost::system::error_code error_code; bytes_wrote = boost::asio::write(*Socket.get(), stream_buffer, boost::asio::transfer_at_least(data_size), error_code); diff --git a/src/updater.cpp b/src/updater.cpp index 5a65c68..5c7e2ac 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -203,7 +203,7 @@ Logger::Ptr Updater::get_logger() const /** * Initialize the logging facility with loglevel and syslog. */ -void Updater::init_log_facility() +void Updater::init_log_facility() const { Log->set_log_facility(Conf->get_loglevel(),Conf->get_syslog(),Conf->get_external_warning_log(),Conf->get_external_warning_level()); Log->print_init_log_facility(); @@ -213,7 +213,7 @@ void Updater::init_log_facility() /** * Update all configured services. */ -void Updater::update_services() +void Updater::update_services() const { // Get all services from the ServiceHolder. list services = ServiceHolder->get_services(); @@ -227,8 +227,8 @@ void Updater::update_services() { string ip_last_update = service->get_actual_ip(); string hostname = service->get_hostname(); - int lastupdated = 0; - int current_time = time(NULL); + time_t lastupdated = 0; + time_t current_time = time(NULL); // Try to get the lastupdated time of the actual service if there is one. if ( service->get_last_updates().size() > 0 ) diff --git a/src/updater.h b/src/updater.h index 34e67f3..c64ad20 100644 --- a/src/updater.h +++ b/src/updater.h @@ -38,7 +38,7 @@ public: ~Updater(); - void update_services(); + void update_services() const; int init_config_from_cmd(int argc, char *argv[]); @@ -56,7 +56,7 @@ public: int init_helper_classes(); - void init_log_facility(); + void init_log_facility() const; int init_ip_helper(); diff --git a/src/util.cpp b/src/util.cpp index a043dda..77e8963 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -20,7 +20,7 @@ namespace Util * @param data The string to compute the md5 for * @return The computed md5 in hex */ -std::string compute_md5_digest(std::string data) throw (std::invalid_argument) +std::string compute_md5_digest(std::string data) { // compute an MD5 digest. @@ -43,21 +43,44 @@ std::string compute_md5_digest(std::string data) throw (std::invalid_argument) EVP_MD_CTX_init(&mdctx); // Now we can call init_ex. - EVP_DigestInit_ex(&mdctx, md, NULL); + if ( EVP_DigestInit_ex(&mdctx, md, NULL) == 0 ) + { + EVP_MD_CTX_cleanup(&mdctx); + EVP_cleanup(); + throw std::invalid_argument("Could not set up digest context correctly"); + } // Test if data is empty. if ( data.empty() ) + { + EVP_MD_CTX_cleanup(&mdctx); + EVP_cleanup(); throw std::invalid_argument("Passed data is empty"); + } // Hash the data. At this point data is not empty and &mdctx is initialized. - EVP_DigestUpdate(&mdctx, data.c_str(), data.size()); + if ( EVP_DigestUpdate(&mdctx, data.c_str(), data.size()) == 0 ) + { + EVP_MD_CTX_cleanup(&mdctx); + EVP_cleanup(); + throw std::invalid_argument("Could not hash data into digest context"); + } // Retrieve the digest value from &mdctx and place it in md_value. - EVP_DigestFinal_ex(&mdctx, md_value, &md_len); + if ( EVP_DigestFinal_ex(&mdctx, md_value, &md_len) == 0 ) + { + EVP_MD_CTX_cleanup(&mdctx); + EVP_cleanup(); + throw std::invalid_argument("Could not retrieve digest value"); + } // Test if md_value is filled correctly and md_len is not zero. if ( (md_len == 0) || (EVP_MD_CTX_size(&mdctx) == 0) ) + { + EVP_MD_CTX_cleanup(&mdctx); + EVP_cleanup(); throw std::invalid_argument("Retrieved invalid digest value"); + } // Internal cleanup of the digest content. EVP_MD_CTX_cleanup(&mdctx); @@ -85,6 +108,8 @@ std::string parse_status_code(std::string data) { std::list tokens; boost::algorithm::split(tokens,data,boost::is_any_of(" ")); + if ( tokens.empty() ) + return ""; return tokens.front(); } diff --git a/src/util.h b/src/util.h index a77f33c..5593ef2 100644 --- a/src/util.h +++ b/src/util.h @@ -17,7 +17,7 @@ namespace Util { - std::string compute_md5_digest(std::string data) throw (std::invalid_argument); + std::string compute_md5_digest(std::string data); std::string parse_status_code(std::string data); std::string parse_status_code(std::string data, std::string delimiter); } -- 1.7.1