#include "dhs.h"
#include <time.h>
+#include <boost/foreach.hpp>
using namespace std;
DHS::DHS(const string& _protocol, const string& _hostname, const string& _login, const string& _password, const Logger::Ptr& _logger, const int _update_interval, const int _max_updates_within_interval, const int _dns_cache_ttl, const string& _proxy, const int _proxy_port)
{
if ( _update_interval == -1 ) // If _update_interval is default po::option_desc (not specified via config)
- set_update_interval(0); // use default protocol value
+ set_update_interval(15); // use default protocol value
else
set_update_interval(_update_interval);
if ( _max_updates_within_interval == -1 )
- set_max_updates_within_interval(0);
+ set_max_updates_within_interval(3);
else
set_max_updates_within_interval(_max_updates_within_interval);
if ( _dns_cache_ttl == -1 )
- set_dns_cache_ttl(180);
+ set_dns_cache_ttl(7200);
else
set_dns_cache_ttl(_dns_cache_ttl);
set_logger(_logger);
// create http helper class
- HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port));
+ HTTPHelper::Ptr _http_help(new HTTPHelper(_logger,_proxy,_proxy_port,_login,_password));
HTTPHelp = _http_help;
_http_help.reset();
+
+ // extract domain part from hostname
+ list<string> host_domain_part = separate_domain_and_host_part(get_hostname());
+ if ( host_domain_part.size() <= 1 )
+ {
+ BaseUrl = assemble_base_url(host_domain_part.front(),"");
+ }
+ else
+ {
+ BaseUrl = assemble_base_url(host_domain_part.front(),host_domain_part.back());
+ }
}
/**
+ * Assemble the dhs update url from the given hostname and domain part
+ * @param hostname The hostname to update IP for.
+ * @param domain_part The domain_part in which the hostname is located.
+ * @return The assembled update url without IP.
+ */
+string DHS::assemble_base_url(const string& hostname, const string& domain_part) const
+{
+ string base_url;
+ if ( !domain_part.empty() )
+ {
+ base_url = "http://members.dhs.org";
+ base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&domain=");
+ base_url.append(domain_part);
+ base_url.append("&hostname=");
+ base_url.append(hostname);
+ base_url.append("&updatetype=Online&ip=");
+ }
+ else
+ {
+ base_url = "http://members.dhs.org";
+ base_url.append("/nic/hosts?hostscmd=edit&hostscmdstage=2&type=4&hostname=");
+ base_url.append(hostname);
+ base_url.append("&updatetype=Online&ip=");
+ }
+ return base_url;
+}
+
+
+/**
+ * Separates the hostname from the domain part.
+ * @param fqdn Hostname with domain part.
+ * @return A list with 2 elements (first element is the hostname, second element the domain part), or a list with 1 element if the domain part couldn't be determined.
+ */
+list<string> DHS::separate_domain_and_host_part(const string& fqdn) const
+{
+ list<string> splitted = split(fqdn,".");
+
+ if ( splitted.size() > 1 )
+ {
+ string host = splitted.front();
+ splitted.pop_front();
+
+ string domain = splitted.front();
+ splitted.pop_front();
+
+ BOOST_FOREACH(string domain_part, splitted)
+ {
+ domain.append(".");
+ domain.append(domain_part);
+ }
+
+ splitted.clear();
+ splitted.push_back(host);
+ splitted.push_back(domain);
+ }
+
+ return splitted;
+}
+
+
+/**
+ * Splitts a string into single tokens useing given delimiters
+ * @param str String to split
+ * @param delimiters Deliminters to use
+ * @return A list with the single tokens
+ */
+list<string> DHS::split(const string& str,const string& delimiters) const
+{
+ list<string> tokens;
+ // Skip delimiters at beginning.
+ string::size_type lastPos = str.find_first_not_of(delimiters, 0);
+ // Find first "non-delimiter".
+ string::size_type pos = str.find_first_of(delimiters, lastPos);
+
+ while (string::npos != pos || string::npos != lastPos)
+ {
+ // Found a token, add it to the list.
+ tokens.push_back(str.substr(lastPos, pos - lastPos));
+ // Skip delimiters. Note the "not_of"
+ lastPos = str.find_first_not_of(delimiters, pos);
+ // Find next "non-delimiter"
+ pos = str.find_first_of(delimiters, lastPos);
+ }
+ return tokens;
+}
+
+
+/**
* Performs the Service update.
* @param ip IP Address to set.
* @return 0 if all is fine, -1 otherwise.
*/
int DHS::perform_update(const std::string& ip)
{
+ BaseUrl.append(ip);
+
+ cout << "BASE URL: " << BaseUrl << endl;
+
+ string output = HTTPHelp->http_get(BaseUrl);
+
+ cout << "OUTPUT: " << output << endl;
+ // TODO: check output for failure, we have to parse html :-(
return 0;
}
{
}
-HTTPHelper::HTTPHelper(Logger::Ptr _log, string _proxy, int _proxy_port)
+HTTPHelper::HTTPHelper(Logger::Ptr _log, const string& _proxy, const int _proxy_port, const string& _username, const string& _password)
{
Log = _log;
Proxy = _proxy;
ProxyPort = _proxy_port;
CurlEasyHandle = init_curl(CurlWritedataBuff, CurlErrBuff);
+
+ set_curl_auth(_username,_password);
}
}
-
/**
* Perform a HTTP GET operation
* @param url URL for HTTP GET operation
/**
+ * Sets HTTP AUTH parameters
+ * @param username The username for HTTP AUTH
+ * @param password The password for HTTP AUTH
+ */
+void HTTPHelper::set_curl_auth(const string& username, const string& password)
+{
+ curl_easy_setopt(CurlEasyHandle,CURLOPT_USERNAME,username.c_str());
+ curl_easy_setopt(CurlEasyHandle,CURLOPT_PASSWORD,password.c_str());
+}
+
+
+/**
* 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
Logger::Ptr Log;
+ void set_curl_url(const std::string& url);
+
+ void set_curl_auth(const std::string& username, const std::string& password);
+
public:
typedef boost::shared_ptr<HTTPHelper> Ptr;
HTTPHelper();
- HTTPHelper(Logger::Ptr _log, std::string _proxy, int _proxy_port);
+ HTTPHelper(Logger::Ptr _log, const std::string& _proxy, const int _proxy_port, const std::string& _username, const std::string& _password);
~HTTPHelper();
CURL* init_curl(std::string& curl_writedata_buff, char* curl_err_buff) const;
-
- std::string http_get(const std::string& url);
-
- void set_curl_url(const std::string& url);
+ std::string http_get(const std::string& ip);
// 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);
-
};
#endif