From ad0e5016b4940a49e24f5b61e737f47c54104294 Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Wed, 9 Sep 2009 15:55:02 +0200 Subject: [PATCH] Refactoring: Renamed class IPHelper to a more meaningful name IPAddrHelper. --- src/ip_addr_helper.cpp | 358 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ip_addr_helper.h | 57 ++++++++ src/iphelper.cpp | 358 ------------------------------------------------ src/iphelper.h | 57 -------- src/main.cpp | 2 +- src/updater.cpp | 12 +- src/updater.h | 4 +- 7 files changed, 424 insertions(+), 424 deletions(-) create mode 100644 src/ip_addr_helper.cpp create mode 100644 src/ip_addr_helper.h delete mode 100644 src/iphelper.cpp delete mode 100644 src/iphelper.h diff --git a/src/ip_addr_helper.cpp b/src/ip_addr_helper.cpp new file mode 100644 index 0000000..0c7894c --- /dev/null +++ b/src/ip_addr_helper.cpp @@ -0,0 +1,358 @@ +/** @file + * @brief IPHelper class implementation. This class represents a Helper to get the actual IP. + * + * + * + * @copyright Intra2net AG + * @license GPLv2 +*/ + +#include "ip_addr_helper.h" +#include +#include + +using namespace std; + +namespace net = boost::asio; + +/** + * Default constructor. + */ +IPAddrHelper::IPAddrHelper() + : Log(new Logger) + , ProxyPort(0) + , UseIPv6(false) +{ +} + + +/** + * Constructor. + */ +IPAddrHelper::IPAddrHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const bool _use_ipv6, const string& _proxy, const int _proxy_port) + : Log(_log) + , WebcheckIpUrl(_webcheck_url) + , WebcheckIpUrlAlt(_webcheck_url_alt) + , Proxy(_proxy) + , ProxyPort(_proxy_port) + , UseIPv6(_use_ipv6) +{ + Hostname = net::ip::host_name(); + Log->print_hostname(Hostname); +} + + +/** + * Default destructor + */ +IPAddrHelper::~IPAddrHelper() +{ +} + + +/** + * Tests if a given IP is a local address + * @param ip The IP to test + * @return true if given IP is local, false if not. + */ +bool IPAddrHelper::is_local(const string ip) const +{ + // 127.0.0.1 + boost::regex expr_loopback("127\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"); + + // 192.168.x.x + boost::regex expr_192("192\\.168\\.[0-9]{1,3}\\.[0-9]{1,3}"); + + // 10.x.x.x + boost::regex expr_10("10\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"); + + // 169.254.x.x + boost::regex expr_169_254("169\\.254\\.[0-9]{1,3}\\.[0-9]{1,3}"); + + // 172.16.x.x -> 172.31.x.x + boost::regex expr_172_1("172\\.1[6-9]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); + boost::regex expr_172_2("172\\.2[0-9]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); + boost::regex expr_172_3("172\\.3[0-1]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); + + // It's time to test against the regex patterns + if ( boost::regex_search(ip,expr_loopback) ) + { + Log->print_regex_match(expr_loopback.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_192) ) + { + Log->print_regex_match(expr_192.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_10) ) + { + Log->print_regex_match(expr_10.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_169_254) ) + { + Log->print_regex_match(expr_169_254.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_172_1) ) + { + Log->print_regex_match(expr_172_1.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_172_2) ) + { + Log->print_regex_match(expr_172_2.str(),ip); + return true; + } + else if ( boost::regex_search(ip,expr_172_3) ) + { + Log->print_regex_match(expr_172_3.str(),ip); + return true; + } + + return false; +} + + +/** + * Get the actual IP of this host through a conventional DNS query or through a IP webcheck URL if configured so. + * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. + */ +string IPAddrHelper::get_actual_ip() const +{ + string ip; + if ( WebcheckIpUrl.empty() ) + { + ip = dns_query(""); + } + else + { + ip = webcheck_ip(); + } + + // If IP is within a private range then return "" + if ( is_local(ip) ) + { + Log->print_ip_is_local(ip); + return ""; + } + + return ip; +} + + +/** + * Get the actual IP of this host through a DNS query. + * @param _hostname The hostname for the dns lookup, if empty string, then perform a dns lookup to the local hostname. + * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. + */ +string IPAddrHelper::dns_query(const string& _hostname) const +{ + string ip_addr_v4; + string ip_addr_v6; + + string hostname = Hostname; + if ( !_hostname.empty() ) + hostname = _hostname; + + try + { + // BOOST asio isn't the simplest way to perform a DNS lookup, but it works just fine. + net::io_service io_service; + net::ip::tcp::resolver resolver(io_service); + net::ip::tcp::resolver::query query(hostname, "0"); + net::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); + net::ip::tcp::resolver::iterator end; + while(endpoint_iterator != end) + { + net::ip::tcp::endpoint endpoint = endpoint_iterator->endpoint(); // this ends up in a compiler warning: cc1plus: warning: dereferencing pointer 'pretmp.37188' does break strict-aliasing rules + net::ip::address ip = endpoint.address(); // but why? + if ( ip.is_v4() ) + ip_addr_v4 = ip.to_string(); + else if ( ip.is_v6() ) + ip_addr_v6 = ip.to_string(); + Log->print_own_ipv4(ip_addr_v4, hostname); + if ( UseIPv6 == true ) + Log->print_own_ipv6(ip_addr_v6, hostname); + endpoint_iterator++; + } + io_service.reset(); + } + catch(exception& e) + { + Log->print_error_hostname_to_ip(e.what(),hostname); + return ""; + } + + if ( (UseIPv6 == true) && (ip_addr_v6 != "") ) + return ip_addr_v6; + + return ip_addr_v4; +} + + +/** + * Get the actual IP of this host through a IP webcheck URL. + * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. + */ +string IPAddrHelper::webcheck_ip() const +{ + string ip_addr = ""; + + // Init CURL buffers + string curl_writedata_buff; + char curl_err_buff[CURL_ERROR_SIZE]; + int curl_err_code=1; + + // Init URL List + list url_list; + url_list.push_back(WebcheckIpUrl); + url_list.push_back(WebcheckIpUrlAlt); + string actual_url; + + // Init CURL + CURL * curl_easy_handle = init_curl(curl_writedata_buff,curl_err_buff); + + // If perform_curl_operation returns a connection problem indicating return code, try the next ip webcheck url if there is one. + while ( (curl_err_code == 1) && (url_list.size() != 0) && (url_list.front() != "") ) + { + // Set URL + actual_url = url_list.front(); + url_list.pop_front(); + set_curl_url(curl_easy_handle,actual_url); + + // Perform curl operation + curl_err_code = perform_curl_operation(curl_easy_handle, curl_err_buff, actual_url); + } + + // Cleanup CURL handle + curl_easy_cleanup(curl_easy_handle); + + // If curl_err_code is not 0, the ip couldn't be determined through any configured webcheck url. + if ( curl_err_code != 0 ) + { + Log->print_webcheck_no_ip(); + // error handling + return ip_addr; + } + + Log->print_received_curl_data(curl_writedata_buff); + + ip_addr = parse_ip(curl_writedata_buff); + + return ip_addr; +} + + +/** + * Tries to find a valid IPv4 Address in dottet format in a given string and returns the IP-Address found. + * @param data The string data to search in for a valid IPv4-Address. + * @return The IP Address found or an empty string. + */ +string IPAddrHelper::parse_ip(const string& data) const +{ + string ip = ""; + + // regex for valid ip address + boost::regex expr("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"); + + boost::smatch matches; + + if ( boost::regex_search(data,matches,expr) ) + { + ip = matches[1]; + Log->print_regex_found_ip(ip); + } + else + { + Log->print_regex_ip_not_found(data); + } + + return ip; +} + + +/** + * Performs the curl operation. + * @param curl_easy_handle The initialized and configured curl handle. + * @param curl_err_buff The pointer to the curl error buffer to get error messages from. + * @param actual_url The actual configured URL. + * @return 0 if all is fine, 1 if an connection problem to the configured url occurs, -1 on other errors. + */ +int IPAddrHelper::perform_curl_operation(CURL * curl_easy_handle, char* curl_err_buff, const string& actual_url) const +{ + int curl_err_code; + if ( (curl_err_code = curl_easy_perform(curl_easy_handle) ) != 0 ) + { + // CURL error occured + if ( (curl_err_code == CURLE_COULDNT_CONNECT) || (curl_err_code == CURLE_OPERATION_TIMEOUTED) || (curl_err_code == CURLE_COULDNT_RESOLVE_HOST) ) + { + // In case of connection problems we should return 1, that the fallback url will be used. + Log->print_webcheck_url_connection_problem(curl_err_buff, actual_url); + return 1; + } + else + { + // other error + Log->print_webcheck_error(curl_err_buff, actual_url); + return -1; + } + } + // Operation performed without any problems + return 0; +} + + +/** + * 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. + */ +void IPAddrHelper::set_curl_url(CURL * curl_easy_handle, const string& url) const +{ + curl_easy_setopt(curl_easy_handle,CURLOPT_URL,url.c_str()); +} + + +/** + * 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. + */ +CURL * IPAddrHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const +{ + CURL *curl_easy_handle = curl_easy_init(); + + curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1); + curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5); + curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10); + curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024); + curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff); + curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive); + curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff); + + if ( !Proxy.empty() ) + { + curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str()); + curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort); + } + + return curl_easy_handle; +} + + +/** + * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer + * @param inBuffer Pointer to input. + * @param size How many mem blocks are received + * @param nmemb size of each memblock + * @param outBuffer Pointer to output stream. + * @return The size received. + */ +int IPAddrHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer ) +{ + outBuffer->append(inBuffer); + return (size*nmemb); +} diff --git a/src/ip_addr_helper.h b/src/ip_addr_helper.h new file mode 100644 index 0000000..5d7765b --- /dev/null +++ b/src/ip_addr_helper.h @@ -0,0 +1,57 @@ +/** @file + * @brief IPHelper class header. This class represents a Helper to get the actual IP. + * + * + * + * @copyright Intra2net AG + * @license GPLv2 +*/ + +#ifndef IPADDRHELPER_H +#define IPADDRHELPER_H + +#include "logger.h" + +#include +#include + + +class IPAddrHelper +{ +private: + + Logger::Ptr Log; + std::string WebcheckIpUrl; + std::string WebcheckIpUrlAlt; + std::string Proxy; + int ProxyPort; + bool UseIPv6; + std::string Hostname; + + bool is_local(const std::string ip) const; + std::string webcheck_ip() const; + CURL * init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const; + int perform_curl_operation(CURL * curl_easy_handle, char* curl_err_buff, const std::string& actual_url) const; + std::string parse_ip(const std::string& data) const; + +public: + + typedef boost::shared_ptr Ptr; + + IPAddrHelper(); + + IPAddrHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const bool _use_ipv6, const std::string& _proxy, const int _proxy_port); + + ~IPAddrHelper(); + + std::string dns_query(const std::string& _hostname) const; + + std::string get_actual_ip() const; + + // libcurl is a C library, so we have to make the callback member function static :-( + static int http_receive(char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer); + + void set_curl_url(CURL * curl_easy_handle, const std::string& url) const; +}; + +#endif diff --git a/src/iphelper.cpp b/src/iphelper.cpp deleted file mode 100644 index f06dcc4..0000000 --- a/src/iphelper.cpp +++ /dev/null @@ -1,358 +0,0 @@ -/** @file - * @brief IPHelper class implementation. This class represents a Helper to get the actual IP. - * - * - * - * @copyright Intra2net AG - * @license GPLv2 -*/ - -#include "iphelper.h" -#include -#include - -using namespace std; - -namespace net = boost::asio; - -/** - * Default constructor. - */ -IPHelper::IPHelper() - : Log(new Logger) - , ProxyPort(0) - , UseIPv6(false) -{ -} - - -/** - * Constructor. - */ -IPHelper::IPHelper(const Logger::Ptr _log, const string& _webcheck_url, const string& _webcheck_url_alt, const bool _use_ipv6, const string& _proxy, const int _proxy_port) - : Log(_log) - , WebcheckIpUrl(_webcheck_url) - , WebcheckIpUrlAlt(_webcheck_url_alt) - , Proxy(_proxy) - , ProxyPort(_proxy_port) - , UseIPv6(_use_ipv6) -{ - Hostname = net::ip::host_name(); - Log->print_hostname(Hostname); -} - - -/** - * Default destructor - */ -IPHelper::~IPHelper() -{ -} - - -/** - * Tests if a given IP is a local address - * @param ip The IP to test - * @return true if given IP is local, false if not. - */ -bool IPHelper::is_local(const string ip) const -{ - // 127.0.0.1 - boost::regex expr_loopback("127\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"); - - // 192.168.x.x - boost::regex expr_192("192\\.168\\.[0-9]{1,3}\\.[0-9]{1,3}"); - - // 10.x.x.x - boost::regex expr_10("10\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"); - - // 169.254.x.x - boost::regex expr_169_254("169\\.254\\.[0-9]{1,3}\\.[0-9]{1,3}"); - - // 172.16.x.x -> 172.31.x.x - boost::regex expr_172_1("172\\.1[6-9]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); - boost::regex expr_172_2("172\\.2[0-9]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); - boost::regex expr_172_3("172\\.3[0-1]{1}\\.[0-9]{1,3}\\.[0-9]{1,3}"); - - // It's time to test against the regex patterns - if ( boost::regex_search(ip,expr_loopback) ) - { - Log->print_regex_match(expr_loopback.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_192) ) - { - Log->print_regex_match(expr_192.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_10) ) - { - Log->print_regex_match(expr_10.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_169_254) ) - { - Log->print_regex_match(expr_169_254.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_172_1) ) - { - Log->print_regex_match(expr_172_1.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_172_2) ) - { - Log->print_regex_match(expr_172_2.str(),ip); - return true; - } - else if ( boost::regex_search(ip,expr_172_3) ) - { - Log->print_regex_match(expr_172_3.str(),ip); - return true; - } - - return false; -} - - -/** - * Get the actual IP of this host through a conventional DNS query or through a IP webcheck URL if configured so. - * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. - */ -string IPHelper::get_actual_ip() const -{ - string ip; - if ( WebcheckIpUrl.empty() ) - { - ip = dns_query(""); - } - else - { - ip = webcheck_ip(); - } - - // If IP is within a private range then return "" - if ( is_local(ip) ) - { - Log->print_ip_is_local(ip); - return ""; - } - - return ip; -} - - -/** - * Get the actual IP of this host through a DNS query. - * @param _hostname The hostname for the dns lookup, if empty string, then perform a dns lookup to the local hostname. - * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. - */ -string IPHelper::dns_query(const string& _hostname) const -{ - string ip_addr_v4; - string ip_addr_v6; - - string hostname = Hostname; - if ( !_hostname.empty() ) - hostname = _hostname; - - try - { - // BOOST asio isn't the simplest way to perform a DNS lookup, but it works just fine. - net::io_service io_service; - net::ip::tcp::resolver resolver(io_service); - net::ip::tcp::resolver::query query(hostname, "0"); - net::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); - net::ip::tcp::resolver::iterator end; - while(endpoint_iterator != end) - { - net::ip::tcp::endpoint endpoint = endpoint_iterator->endpoint(); // this ends up in a compiler warning: cc1plus: warning: dereferencing pointer 'pretmp.37188' does break strict-aliasing rules - net::ip::address ip = endpoint.address(); // but why? - if ( ip.is_v4() ) - ip_addr_v4 = ip.to_string(); - else if ( ip.is_v6() ) - ip_addr_v6 = ip.to_string(); - Log->print_own_ipv4(ip_addr_v4, hostname); - if ( UseIPv6 == true ) - Log->print_own_ipv6(ip_addr_v6, hostname); - endpoint_iterator++; - } - io_service.reset(); - } - catch(exception& e) - { - Log->print_error_hostname_to_ip(e.what(),hostname); - return ""; - } - - if ( (UseIPv6 == true) && (ip_addr_v6 != "") ) - return ip_addr_v6; - - return ip_addr_v4; -} - - -/** - * Get the actual IP of this host through a IP webcheck URL. - * @return A string representation of the actual IP in dotted format or an empty string if something went wrong. - */ -string IPHelper::webcheck_ip() const -{ - string ip_addr = ""; - - // Init CURL buffers - string curl_writedata_buff; - char curl_err_buff[CURL_ERROR_SIZE]; - int curl_err_code=1; - - // Init URL List - list url_list; - url_list.push_back(WebcheckIpUrl); - url_list.push_back(WebcheckIpUrlAlt); - string actual_url; - - // Init CURL - CURL * curl_easy_handle = init_curl(curl_writedata_buff,curl_err_buff); - - // If perform_curl_operation returns a connection problem indicating return code, try the next ip webcheck url if there is one. - while ( (curl_err_code == 1) && (url_list.size() != 0) && (url_list.front() != "") ) - { - // Set URL - actual_url = url_list.front(); - url_list.pop_front(); - set_curl_url(curl_easy_handle,actual_url); - - // Perform curl operation - curl_err_code = perform_curl_operation(curl_easy_handle, curl_err_buff, actual_url); - } - - // Cleanup CURL handle - curl_easy_cleanup(curl_easy_handle); - - // If curl_err_code is not 0, the ip couldn't be determined through any configured webcheck url. - if ( curl_err_code != 0 ) - { - Log->print_webcheck_no_ip(); - // error handling - return ip_addr; - } - - Log->print_received_curl_data(curl_writedata_buff); - - ip_addr = parse_ip(curl_writedata_buff); - - return ip_addr; -} - - -/** - * Tries to find a valid IPv4 Address in dottet format in a given string and returns the IP-Address found. - * @param data The string data to search in for a valid IPv4-Address. - * @return The IP Address found or an empty string. - */ -string IPHelper::parse_ip(const string& data) const -{ - string ip = ""; - - // regex for valid ip address - boost::regex expr("([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})"); - - boost::smatch matches; - - if ( boost::regex_search(data,matches,expr) ) - { - ip = matches[1]; - Log->print_regex_found_ip(ip); - } - else - { - Log->print_regex_ip_not_found(data); - } - - return ip; -} - - -/** - * Performs the curl operation. - * @param curl_easy_handle The initialized and configured curl handle. - * @param curl_err_buff The pointer to the curl error buffer to get error messages from. - * @param actual_url The actual configured URL. - * @return 0 if all is fine, 1 if an connection problem to the configured url occurs, -1 on other errors. - */ -int IPHelper::perform_curl_operation(CURL * curl_easy_handle, char* curl_err_buff, const string& actual_url) const -{ - int curl_err_code; - if ( (curl_err_code = curl_easy_perform(curl_easy_handle) ) != 0 ) - { - // CURL error occured - if ( (curl_err_code == CURLE_COULDNT_CONNECT) || (curl_err_code == CURLE_OPERATION_TIMEOUTED) || (curl_err_code == CURLE_COULDNT_RESOLVE_HOST) ) - { - // In case of connection problems we should return 1, that the fallback url will be used. - Log->print_webcheck_url_connection_problem(curl_err_buff, actual_url); - return 1; - } - else - { - // other error - Log->print_webcheck_error(curl_err_buff, actual_url); - return -1; - } - } - // Operation performed without any problems - return 0; -} - - -/** - * 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. - */ -void IPHelper::set_curl_url(CURL * curl_easy_handle, const string& url) const -{ - curl_easy_setopt(curl_easy_handle,CURLOPT_URL,url.c_str()); -} - - -/** - * 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. - */ -CURL * IPHelper::init_curl(string& curl_writedata_buff,char* curl_err_buff) const -{ - CURL *curl_easy_handle = curl_easy_init(); - - curl_easy_setopt(curl_easy_handle,CURLOPT_NOPROGRESS,1); - curl_easy_setopt(curl_easy_handle,CURLOPT_CONNECTTIMEOUT,5); - curl_easy_setopt(curl_easy_handle,CURLOPT_TIMEOUT,10); - curl_easy_setopt(curl_easy_handle,CURLOPT_BUFFERSIZE,1024); - curl_easy_setopt(curl_easy_handle,CURLOPT_ERRORBUFFER,curl_err_buff); - curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEFUNCTION,http_receive); - curl_easy_setopt(curl_easy_handle,CURLOPT_WRITEDATA,&curl_writedata_buff); - - if ( !Proxy.empty() ) - { - curl_easy_setopt(curl_easy_handle,CURLOPT_PROXY,Proxy.c_str()); - curl_easy_setopt(curl_easy_handle,CURLOPT_PROXYPORT,ProxyPort); - } - - return curl_easy_handle; -} - - -/** - * Callback Function is called every time CURL is receiving data from HTTPS-Server and will copy all received Data to the given stream pointer - * @param inBuffer Pointer to input. - * @param size How many mem blocks are received - * @param nmemb size of each memblock - * @param outBuffer Pointer to output stream. - * @return The size received. - */ -int IPHelper::http_receive( char *inBuffer, size_t size, size_t nmemb, string *outBuffer ) -{ - outBuffer->append(inBuffer); - return (size*nmemb); -} diff --git a/src/iphelper.h b/src/iphelper.h deleted file mode 100644 index c1ff7f6..0000000 --- a/src/iphelper.h +++ /dev/null @@ -1,57 +0,0 @@ -/** @file - * @brief IPHelper class header. This class represents a Helper to get the actual IP. - * - * - * - * @copyright Intra2net AG - * @license GPLv2 -*/ - -#ifndef IPHELPER_H -#define IPHELPER_H - -#include "logger.h" - -#include -#include - - -class IPHelper -{ -private: - - Logger::Ptr Log; - std::string WebcheckIpUrl; - std::string WebcheckIpUrlAlt; - std::string Proxy; - int ProxyPort; - bool UseIPv6; - std::string Hostname; - - bool is_local(const std::string ip) const; - std::string webcheck_ip() const; - CURL * init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const; - int perform_curl_operation(CURL * curl_easy_handle, char* curl_err_buff, const std::string& actual_url) const; - std::string parse_ip(const std::string& data) const; - -public: - - typedef boost::shared_ptr Ptr; - - IPHelper(); - - IPHelper(const Logger::Ptr _log, const std::string& _webcheck_url, const std::string& _webcheck_url_alt, const bool _use_ipv6, const std::string& _proxy, const int _proxy_port); - - ~IPHelper(); - - std::string dns_query(const std::string& _hostname) const; - - std::string get_actual_ip() const; - - // libcurl is a C library, so we have to make the callback member function static :-( - static int http_receive(char *inBuffer, size_t size, size_t nmemb, std::string *outBuffer); - - void set_curl_url(CURL * curl_easy_handle, const std::string& url) const; -}; - -#endif diff --git a/src/main.cpp b/src/main.cpp index a45dca2..6de0f65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,7 +33,7 @@ #include "service.cpp" #include "serviceholder.cpp" #include "updater.cpp" -#include "iphelper.cpp" +#include "ip_addr_helper.cpp" #include "httphelper.cpp" #include "serializeservicecontainer.cpp" #include "util.cpp" diff --git a/src/updater.cpp b/src/updater.cpp index 30e160a..7083d84 100644 --- a/src/updater.cpp +++ b/src/updater.cpp @@ -41,7 +41,7 @@ using namespace std; * Default constructor which initializes the member Conf. */ Updater::Updater() - : IPHelp(new IPHelper) + : IPAddrHelp(new IPAddrHelper) { // Initialize program wide Logger, at this point we don't know where to log and with which loglevel. Logger::Ptr _log(new Logger); @@ -185,9 +185,9 @@ int Updater::init_helper_classes() int Updater::init_ip_helper() { // initialize IPHelper - 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())); - IPHelp = _iphelp; - _iphelp.reset(); + IPAddrHelper::Ptr _ipaddrhelp(new IPAddrHelper(Log,Conf->get_webcheck_ip_url(),Conf->get_webcheck_ip_url_alt(),Conf->get_enable_ipv6(),Conf->get_proxy(),Conf->get_proxy_port())); + IPAddrHelp = _ipaddrhelp; + _ipaddrhelp.reset(); return 0; } @@ -230,7 +230,7 @@ void Updater::update_services() { list services = ServiceHolder->get_services(); - string ip = IPHelp->get_actual_ip(); + string ip = IPAddrHelp->get_actual_ip(); if ( !ip.empty() ) { @@ -247,7 +247,7 @@ void Updater::update_services() if ( ((lastupdated != 0) && ((lastupdated + service->get_dns_cache_ttl()) < current_time)) || (lastupdated == 0) ) { Log->print_recheck_dns_entry(service->get_hostname(),lastupdated,service->get_dns_cache_ttl(),current_time); - string _dns_recheck_ip = IPHelp->dns_query(service->get_hostname()); + string _dns_recheck_ip = IPAddrHelp->dns_query(service->get_hostname()); Log->print_cached_dns_entry(service->get_hostname(), _dns_recheck_ip, service->get_actual_ip()); if (!_dns_recheck_ip.empty()) dns_recheck_ip = _dns_recheck_ip; diff --git a/src/updater.h b/src/updater.h index cc9bf13..34e67f3 100644 --- a/src/updater.h +++ b/src/updater.h @@ -11,7 +11,7 @@ #define UPDATER_H #include "config.h" -#include "iphelper.h" +#include "ip_addr_helper.h" #include "httphelper.h" #include "logger.h" #include "serviceholder.h" @@ -25,7 +25,7 @@ class Updater private: Config::Ptr Conf; - IPHelper::Ptr IPHelp; + IPAddrHelper::Ptr IPAddrHelp; HTTPHelper::Ptr HTTPHelp; Logger::Ptr Log; Serviceholder::Ptr ServiceHolder; -- 1.7.1