Introduced new class IPHelper which should perform DNS and webcheck IP queries.
[bpdyndnsd] / src / iphelper.cpp
CommitLineData
0665b239
BS
1/** @file
2 * @brief IPHelper class implementation. This class represents a Helper to get the actual IP.
3 *
4 *
5 *
6 * @copyright Intra2net AG
7 * @license GPLv2
8*/
9
10#include "iphelper.h"
11#include <boost/asio.hpp>
12
13using namespace std;
14
15namespace net = boost::asio;
16
17/**
18 * Default constructor.
19 */
20IPHelper::IPHelper()
21 : Hostname("")
22 , WebcheckUrl("")
23 , WebcheckUrlAlt("")
24 , Log(new Logger)
25 , UseIPv6(false)
26{
27}
28
29
30/**
31 * Constructor.
32 */
33IPHelper::IPHelper(Logger::Ptr _log)
34 : Hostname("")
35 , WebcheckUrl("")
36 , WebcheckUrlAlt("")
37 , Log(new Logger)
38 , UseIPv6(false)
39{
40 Log = _log;
41}
42
43
44/**
45 * Default destructor
46 */
47IPHelper::~IPHelper()
48{
49}
50
51
52/**
53 * Initializes the ip helper.
54 */
55int IPHelper::init_hostname()
56{
57 Hostname = net::ip::host_name();
58
59 Log->print_hostname(Hostname);
60
61 return 0;
62}
63
64
65/**
66 * Get the actual IP of this host through a conventional DNS query or through a IP webcheck URL if configured so.
67 * @return A string representation of the actual IP in dotted format or an empty string if something went wrong.
68 */
69string IPHelper::get_actual_ip() const
70{
71 if ( WebcheckUrl == "" )
72 {
73 return dns_query();
74 }
75 else
76 {
77 return webcheck_ip();
78 }
79 return "";
80}
81
82void IPHelper::set_hostname(const string& _hostname)
83{
84 Hostname = _hostname;
85}
86
87
88/**
89 * Get the actual IP of this host through a DNS query.
90 * @return A string representation of the actual IP in dotted format or an empty string if something went wrong.
91 */
92string IPHelper::dns_query() const
93{
94 string ip_addr_v4 = "";
95 string ip_addr_v6 = "";
96
97 try
98 {
99 // BOOST asio isn't the simplest way to perform a DNS lookup, but it works just fine.
100 net::io_service io_service;
101 net::ip::tcp::resolver resolver(io_service);
102 net::ip::tcp::resolver::query query(Hostname, "0");
103 net::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
104 net::ip::tcp::resolver::iterator end;
105 while(endpoint_iterator != end)
106 {
107 net::ip::tcp::endpoint endpoint = endpoint_iterator->endpoint();
108 net::ip::address ip = endpoint.address();
109 if ( ip.is_v4() )
110 ip_addr_v4 = ip.to_string();
111 else if ( ip.is_v6() )
112 ip_addr_v6 = ip.to_string();
113 Log->print_own_ip(ip_addr_v4, ip_addr_v6);
114 endpoint_iterator++;
115 }
116 io_service.reset();
117 }
118 catch(exception& e)
119 {
120 Log->print_error_hostname_to_ip(e.what(),Hostname);
121 return "";
122 }
123
124 if ( (UseIPv6 == true) && (ip_addr_v6 != "") )
125 return ip_addr_v6;
126
127 return ip_addr_v4;
128}
129
130
131/**
132 * Get the actual IP of this host through a IP webcheck URL.
133 * @return A string representation of the actual IP in dotted format or an empty string if something went wrong.
134 */
135string IPHelper::webcheck_ip() const
136{
137 string ip_addr = "127.0.0.1";
138
139 return ip_addr;
140}
141
142
143/**
144 * Setter for both members WebcheckUrl and WebcheckUrlAlt
145 * @param _webcheck_url The primary webcheck URL.
146 * @param _webcheck_url_alt The fallback webcheck URL.
147 */
148void IPHelper::set_webcheck_urls(std::string& _webcheck_url, std::string& _webcheck_url_alt)
149{
150
151}