Commit | Line | Data |
---|---|---|
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 | ||
13 | using namespace std; | |
14 | ||
15 | namespace net = boost::asio; | |
16 | ||
17 | /** | |
18 | * Default constructor. | |
19 | */ | |
20 | IPHelper::IPHelper() | |
21 | : Hostname("") | |
22 | , WebcheckUrl("") | |
23 | , WebcheckUrlAlt("") | |
0665b239 | 24 | , UseIPv6(false) |
68c6b4af | 25 | , Log(new Logger) |
0665b239 BS |
26 | { |
27 | } | |
28 | ||
29 | ||
30 | /** | |
31 | * Constructor. | |
32 | */ | |
33 | IPHelper::IPHelper(Logger::Ptr _log) | |
34 | : Hostname("") | |
35 | , WebcheckUrl("") | |
36 | , WebcheckUrlAlt("") | |
0665b239 | 37 | , UseIPv6(false) |
68c6b4af | 38 | , Log(new Logger) |
0665b239 BS |
39 | { |
40 | Log = _log; | |
41 | } | |
42 | ||
43 | ||
44 | /** | |
45 | * Default destructor | |
46 | */ | |
47 | IPHelper::~IPHelper() | |
48 | { | |
49 | } | |
50 | ||
51 | ||
52 | /** | |
53 | * Initializes the ip helper. | |
54 | */ | |
55 | int 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 | */ | |
69 | string 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 | ||
82 | void 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 | */ | |
92 | string 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 | */ | |
135 | string 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 | */ | |
148 | void IPHelper::set_webcheck_urls(std::string& _webcheck_url, std::string& _webcheck_url_alt) | |
149 | { | |
150 | ||
151 | } |