Commit | Line | Data |
---|---|---|
36ad976b CH |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | ||
20 | Christian Herdtweck, Intra2net AG 2015 | |
4e7b6ff9 CH |
21 | |
22 | with code copied from boost::net::dns::resolve.hpp | |
23 | by Andreas Haberstroh (andreas at ibusy dot com) | |
24 | from https://github.com/softwareace/Boost.DNS | |
36ad976b CH |
25 | */ |
26 | ||
c5b4902d CH |
27 | #include "dns/dnsresolver.h" |
28 | ||
29 | #include <stdint.h> | |
e91538f0 | 30 | #include <sstream> |
36ad976b | 31 | |
4e7b6ff9 CH |
32 | #include <boost/foreach.hpp> |
33 | #include <boost/bind.hpp> | |
34 | #include <boost/function.hpp> | |
35 | #include <boost/net/dns.hpp> | |
36 | #include <boost/date_time/posix_time/posix_time.hpp> | |
ad83004d CH |
37 | #include <boost/uuid/uuid.hpp> |
38 | #include <boost/uuid/uuid_io.hpp> | |
4e7b6ff9 | 39 | |
36ad976b | 40 | #include <logfunc.hpp> |
4e7b6ff9 | 41 | |
36ad976b | 42 | using I2n::Logger::GlobalLogger; |
4e7b6ff9 | 43 | using boost::posix_time::seconds; |
36ad976b CH |
44 | |
45 | namespace Config | |
46 | { | |
fd62d09f CH |
47 | const int ResolveTimeoutSeconds = 2; |
48 | const int PauseBeforeRetrySeconds = 1; | |
49 | const int StaleDataLongtermSeconds = 5*60; | |
36ad976b | 50 | const int DNS_PORT = 53; |
36ad976b CH |
51 | } |
52 | ||
4e7b6ff9 CH |
53 | DnsResolver::DnsResolver(IoServiceItem &io_serv, |
54 | const std::string &hostname, | |
923626c0 | 55 | const DnsIpProtocol &protocol, |
96779587 | 56 | const DnsCacheItem cache, |
96779587 | 57 | const boost::asio::ip::address &name_server) |
4e7b6ff9 | 58 | : ResolverBase( io_serv, hostname, cache ) |
e91538f0 CH |
59 | , Socket( *io_serv, ip::udp::endpoint(ip::udp::v4(), 0)) |
60 | // just connect to anything, will specify sender/receiver later | |
4e7b6ff9 | 61 | , ReceiveBuffer() |
ad83004d | 62 | , RequestBuffer() |
923626c0 | 63 | , Protocol( protocol ) |
4e7b6ff9 | 64 | , NameServer( name_server, Config::DNS_PORT ) |
36ad976b CH |
65 | , ResolveTimeoutTimer( *io_serv ) |
66 | , PauseBeforeRetryTimer( *io_serv ) | |
67 | , StaleDataLongtermTimer( *io_serv ) | |
4e7b6ff9 | 68 | , NextIpIndex( 0 ) |
36ad976b CH |
69 | , RetryCount( 0 ) |
70 | , IsResolving( false ) | |
e91538f0 | 71 | , LogPrefix( "DnsResolver" ) |
ad83004d CH |
72 | , RandomIdGenerator() |
73 | , RequestId( 0 ) | |
e18c1337 | 74 | , OperationCancelled( false ) |
fd62d09f | 75 | , LongtermTimerIsActive( false ) |
e91538f0 CH |
76 | { |
77 | std::stringstream temp; | |
78 | temp << "Dns(" << ResolverBase::Hostname << "): "; | |
79 | LogPrefix = temp.str(); | |
ad83004d | 80 | |
e91538f0 | 81 | } |
96779587 | 82 | |
c5b4902d CH |
83 | DnsResolver::~DnsResolver() |
84 | { | |
85 | boost::system::error_code error; | |
e18c1337 CH |
86 | //Socket.shutdown(boost::asio::ip::udp::socket::shutdown_both, error); |
87 | //if ( error ) | |
49b82a1d | 88 | // GlobalLogger.info() << LogPrefix << "Received error " << error |
e18c1337 CH |
89 | // << " when shutting down socket for DNS"; |
90 | // in IcmpPinger always gave an error system:9 (EBADF: Bad file descriptor) | |
91 | // Here gives error system:107 ENOTCONN: Transport endpoint is not connected | |
c5b4902d CH |
92 | |
93 | Socket.close(error); | |
94 | if ( error ) | |
49b82a1d CH |
95 | GlobalLogger.info() << LogPrefix << "Received error " << error |
96 | << " when closing socket for DNS"; | |
c5b4902d CH |
97 | } |
98 | ||
99 | ||
96779587 CH |
100 | |
101 | //============================================================================== | |
102 | // ASYNC RESOLVE | |
103 | //============================================================================== | |
36ad976b CH |
104 | |
105 | /** | |
c5b4902d | 106 | * copied here code from boost::net::dns::resolve.hpp, since want async |
36ad976b | 107 | * operation and that is used only internally, there |
36ad976b | 108 | */ |
cd71d095 | 109 | void DnsResolver::do_resolve(const int recursion_count) |
36ad976b | 110 | { |
36ad976b | 111 | // check if resolving already |
4e7b6ff9 | 112 | if (IsResolving) |
36ad976b | 113 | { |
e91538f0 | 114 | GlobalLogger.info() << LogPrefix |
4e7b6ff9 | 115 | << "Call to do_resolve ignored since resolving already"; |
36ad976b CH |
116 | return; |
117 | } | |
ad83004d | 118 | IsResolving = true; |
e18c1337 | 119 | OperationCancelled = false; |
36ad976b | 120 | |
e18c1337 CH |
121 | GlobalLogger.info() << LogPrefix << "start resolving for IPs of type " |
122 | << to_string(Protocol) << " using name server " << NameServer; | |
e91538f0 | 123 | |
36ad976b CH |
124 | // just to be sure: cancel timers |
125 | ResolveTimeoutTimer.cancel(); | |
126 | PauseBeforeRetryTimer.cancel(); | |
127 | StaleDataLongtermTimer.cancel(); | |
fd62d09f | 128 | LongtermTimerIsActive = false; |
36ad976b CH |
129 | |
130 | // create DNS request | |
e18c1337 | 131 | boost::net::dns::message dns_message( ResolverBase::Hostname, Protocol ); |
23f51766 | 132 | dns_message.recursive(true); |
4e7b6ff9 CH |
133 | dns_message.action(boost::net::dns::message::query); |
134 | dns_message.opcode(boost::net::dns::message::squery); | |
ad83004d CH |
135 | |
136 | // create random ID for message | |
137 | boost::uuids::uuid message_id = RandomIdGenerator(); | |
138 | memcpy( &RequestId, message_id.data, sizeof(RequestId) ); | |
139 | dns_message.id( RequestId ); | |
140 | GlobalLogger.debug() << LogPrefix << "Request has ID " | |
cd71d095 | 141 | << std::showbase << std::hex << dns_message.id(); |
36ad976b CH |
142 | |
143 | // setup receipt of reply | |
144 | Socket.async_receive_from( | |
145 | boost::asio::buffer(ReceiveBuffer.get_array()), | |
146 | NameServer, | |
147 | boost::bind( &DnsResolver::handle_dns_result, this, | |
cd71d095 | 148 | recursion_count, |
36ad976b CH |
149 | boost::asio::placeholders::error, |
150 | boost::asio::placeholders::bytes_transferred) | |
4e7b6ff9 | 151 | ); |
36ad976b CH |
152 | |
153 | // schedule timeout | |
4e7b6ff9 CH |
154 | (void) ResolveTimeoutTimer.expires_from_now( |
155 | seconds(Config::ResolveTimeoutSeconds)); | |
156 | ResolveTimeoutTimer.async_wait( boost::bind( | |
157 | &DnsResolver::handle_resolve_timeout, | |
cd71d095 CH |
158 | this, recursion_count, |
159 | boost::asio::placeholders::error) ); | |
36ad976b CH |
160 | |
161 | // send dns request | |
ad83004d | 162 | dns_message.encode(RequestBuffer); |
e91538f0 CH |
163 | size_t bytes_sent; |
164 | try | |
165 | { | |
166 | bytes_sent = Socket.send_to( | |
ad83004d | 167 | boost::asio::buffer(RequestBuffer.get_array()), |
e91538f0 CH |
168 | NameServer ); |
169 | } | |
170 | catch (boost::system::system_error &err) | |
171 | { | |
49b82a1d | 172 | GlobalLogger.info() << LogPrefix |
e91538f0 CH |
173 | << "Sending of DNS request message failed: " |
174 | << err.what(); | |
cd71d095 | 175 | schedule_retry(recursion_count); |
e91538f0 CH |
176 | return; |
177 | } | |
178 | ||
179 | if ( bytes_sent == 0 ) | |
180 | { | |
49b82a1d | 181 | GlobalLogger.info() << LogPrefix << "Empty DNS request sent!"; |
cd71d095 | 182 | schedule_retry(recursion_count); |
e91538f0 CH |
183 | return; |
184 | } | |
36ad976b CH |
185 | } |
186 | ||
187 | ||
cd71d095 CH |
188 | void DnsResolver::handle_dns_result(const int recursion_count, |
189 | const boost::system::error_code &error, | |
ad83004d | 190 | const std::size_t bytes_transferred) |
36ad976b | 191 | { |
e18c1337 | 192 | if (error) |
36ad976b | 193 | { |
e91538f0 | 194 | GlobalLogger.info() << LogPrefix << "DNS resolve resulted in error " |
fd62d09f | 195 | << error << " --> request retry"; |
cd71d095 | 196 | schedule_retry(recursion_count); |
e18c1337 CH |
197 | return; |
198 | } | |
199 | else if ( OperationCancelled ) | |
200 | { // async_resolve was cancelled --> callbacks already called | |
201 | GlobalLogger.info() << LogPrefix | |
202 | << "Ignoring DNS results since we were cancelled"; | |
36ad976b CH |
203 | return; |
204 | } | |
205 | ||
ad83004d CH |
206 | GlobalLogger.debug() << LogPrefix << "Handling DNS result (" |
207 | << bytes_transferred << " bytes transferred)"; | |
208 | ||
36ad976b CH |
209 | // next 3(+1) lines copied from boost/net/dns/resolver.hpp: |
210 | // clamp the recvBuffer with the number of bytes transferred or decode buffr | |
211 | ReceiveBuffer.length(bytes_transferred); | |
4e7b6ff9 | 212 | boost::net::dns::message result_message; |
36ad976b CH |
213 | result_message.decode( ReceiveBuffer ); |
214 | ||
ad83004d | 215 | // check ID |
3f489614 CH |
216 | if (RequestId == 0) |
217 | { // system DNS or firewall might have longer timeout than me | |
218 | // --> might receive replies for old requests whose time-out has expired | |
219 | // --> have already called callbacks etc, so nothing to do with result | |
220 | // TODO: did receive probably same data several times --> reset buffer? | |
221 | GlobalLogger.info() << LogPrefix << "Ignoring reply to old DNS request " | |
222 | << "(reply has ID " << std::showbase << std::hex | |
223 | << result_message.id() << " or buffer not reset)"; | |
224 | return; | |
225 | } | |
226 | else if (RequestId != result_message.id()) | |
227 | { | |
49b82a1d | 228 | GlobalLogger.info() << LogPrefix << "Received answer for request ID " |
ad83004d | 229 | << std::showbase << std::hex << result_message.id() |
3f489614 CH |
230 | << " but expected ID " << RequestId << " --> ignore and retry"; |
231 | schedule_retry(recursion_count); | |
232 | return; | |
233 | } | |
ad83004d CH |
234 | else |
235 | GlobalLogger.debug() << LogPrefix << "Result has correct ID " | |
236 | << std::showbase << std::hex << RequestId; | |
e18c1337 | 237 | RequestId = 0; |
ad83004d CH |
238 | |
239 | // loop over answers, remembering ips and cnames | |
4e7b6ff9 CH |
240 | // work with a regular pointer to list of answers since result_message is |
241 | // owner of data and that exists until end of function | |
242 | // Items in answers list are shared_ptr to resource_base_t | |
e18c1337 | 243 | std::vector<host_addr_pair> result_ips; |
dbe986b9 CH |
244 | std::vector<src_cname_pair> result_cnames; |
245 | std::vector<string_pair> result_name_servers; | |
36ad976b | 246 | |
c1abff61 | 247 | GlobalLogger.debug() << LogPrefix <<"Checking ANSWERS section of dns reply"; |
e18c1337 | 248 | gather_results(result_message.answers(), &result_ips, &result_cnames, |
dbe986b9 | 249 | &result_name_servers); |
ad83004d | 250 | |
e18c1337 | 251 | // remember cname list (if there were any) |
cd71d095 CH |
252 | // results should have the logical order |
253 | // Hostname [ --> cname1 --> cname2 --> ... --> cnameN ] [ --> ips ]; | |
254 | // otherwise just have unneccessary cnames in cache | |
dbe986b9 | 255 | BOOST_FOREACH( const src_cname_pair &host_and_cname, result_cnames ) |
e18c1337 | 256 | ResolverBase::update_cache(host_and_cname.first, host_and_cname.second); |
ad83004d | 257 | |
e18c1337 | 258 | if ( !result_ips.empty() ) |
cd71d095 | 259 | handle_ips( recursion_count, result_ips ); |
ad83004d | 260 | else if ( !result_cnames.empty() ) |
e18c1337 CH |
261 | // no IPs but at least one cname --> find the "last" cname and |
262 | // re-start resolving with that | |
cd71d095 | 263 | handle_cname(recursion_count, result_cnames); |
ad83004d | 264 | else |
23f51766 | 265 | { // no answers --> cannot proceed |
49b82a1d | 266 | GlobalLogger.info() << LogPrefix << "No IP nor CNAME received! " |
fd62d09f | 267 | << "--> request retry"; |
cd71d095 | 268 | schedule_retry(recursion_count); |
ad83004d | 269 | } |
ad83004d CH |
270 | } |
271 | ||
23f51766 CH |
272 | /** |
273 | * gather IPs, CNAMEs and name servers from list of resource records; | |
274 | * | |
275 | * can be run on anwers(), autorities() and additional() sections of dns reply | |
276 | * messages | |
cd71d095 | 277 | * |
23f51766 CH |
278 | * @param rr_list: input list of resource records |
279 | * @param result_ips: output vector of ips | |
280 | * @param result_cnames: output vector of cnames | |
281 | * @param result_name_servers: output vector of name servers | |
282 | */ | |
283 | void DnsResolver::gather_results(const boost::net::dns::rr_list_t *rr_list, | |
e18c1337 | 284 | std::vector<host_addr_pair> *result_ips, |
dbe986b9 CH |
285 | std::vector<src_cname_pair> *result_cnames, |
286 | std::vector<string_pair> *result_name_servers) | |
ad83004d CH |
287 | const |
288 | { | |
4e7b6ff9 | 289 | using boost::net::dns::resource_base_t; |
dbe986b9 | 290 | boost::posix_time::ptime now =boost::posix_time::second_clock::local_time(); |
23f51766 | 291 | BOOST_FOREACH( boost::shared_ptr<resource_base_t> rr_item, *rr_list ) |
36ad976b | 292 | { |
4e7b6ff9 | 293 | boost::net::dns::type_t rr_type = rr_item->rtype(); |
ad83004d CH |
294 | uint32_t ttl = rr_item->ttl(); |
295 | std::string domain = rr_item->domain(); | |
dbe986b9 CH |
296 | std::string expiry = |
297 | boost::posix_time::to_simple_string(now + seconds(ttl)); | |
36ad976b CH |
298 | |
299 | if (rr_type == boost::net::dns::type_a) | |
300 | { // 'A' resource records carry IPv4 addresses | |
923626c0 CH |
301 | if (Protocol == DNS_IPv6) |
302 | { | |
e91538f0 CH |
303 | GlobalLogger.info() << LogPrefix << "Ignoring IPv4 address " |
304 | << "because resolver was configured to only use IPv6."; | |
923626c0 CH |
305 | continue; |
306 | } | |
4e7b6ff9 CH |
307 | boost::asio::ip::address_v4 ip = |
308 | ( dynamic_cast<boost::net::dns::a_resource *> (rr_item.get()) ) | |
309 | ->address(); | |
e18c1337 | 310 | result_ips->push_back(host_addr_pair(domain, HostAddress(ip, ttl))); |
dbe986b9 CH |
311 | GlobalLogger.debug() << LogPrefix << domain << ": IPv4 " << ip |
312 | << " with TTL " << ttl << "s (until " | |
313 | << expiry << ")"; | |
36ad976b CH |
314 | } |
315 | else if (rr_type == boost::net::dns::type_a6) | |
316 | { // 'AAAA' resource records carry IPv6 addresses | |
923626c0 CH |
317 | if (Protocol == DNS_IPv4) |
318 | { | |
e91538f0 CH |
319 | GlobalLogger.info() << LogPrefix << "Ignoring IPv6 address " |
320 | << "because resolver was configured to only use IPv4."; | |
923626c0 CH |
321 | continue; |
322 | } | |
4e7b6ff9 CH |
323 | boost::asio::ip::address_v6 ip = |
324 | ( dynamic_cast<boost::net::dns::a6_resource *> (rr_item.get()) ) | |
325 | ->address(); | |
e18c1337 | 326 | result_ips->push_back(host_addr_pair(domain, HostAddress(ip, ttl))); |
dbe986b9 CH |
327 | GlobalLogger.debug() << LogPrefix << domain << ": IPv6 " << ip |
328 | << " with TTL " << ttl << "s (until " | |
329 | << expiry << ")"; | |
36ad976b CH |
330 | } |
331 | else if (rr_type == boost::net::dns::type_cname) | |
332 | { // 'CNAME' resource records that carry aliases | |
4e7b6ff9 CH |
333 | std::string cname = |
334 | (dynamic_cast<boost::net::dns::cname_resource *>(rr_item.get())) | |
335 | ->canonicalname(); | |
dbe986b9 CH |
336 | result_cnames->push_back( src_cname_pair(domain, |
337 | Cname(cname, ttl)) ); | |
338 | GlobalLogger.debug() << LogPrefix << domain << ": CNAME to " | |
339 | << cname << " with TTL " << ttl << "s (until " | |
340 | << expiry << ")"; | |
36ad976b CH |
341 | } |
342 | else if (rr_type == boost::net::dns::type_ns) | |
dbe986b9 CH |
343 | { // NS (name_server) resource records |
344 | std::string name_server = | |
ad83004d CH |
345 | (dynamic_cast<boost::net::dns::ns_resource *>(rr_item.get())) |
346 | ->nameserver(); | |
dbe986b9 CH |
347 | result_name_servers->push_back( string_pair(domain, name_server) ); |
348 | GlobalLogger.debug() << LogPrefix << "NameServer " << name_server | |
349 | << " for " << domain << " with TTL " << ttl | |
350 | << "s (until " << expiry << ")"; | |
ad83004d | 351 | } |
36ad976b | 352 | else if (rr_type == boost::net::dns::type_soa) |
e91538f0 | 353 | GlobalLogger.debug() << LogPrefix << "SOA resource"; |
36ad976b | 354 | else if (rr_type == boost::net::dns::type_ptr) |
e91538f0 | 355 | GlobalLogger.debug() << LogPrefix << "ptr resource"; |
36ad976b | 356 | else if (rr_type == boost::net::dns::type_hinfo) |
e91538f0 | 357 | GlobalLogger.debug() << LogPrefix << "hinfo resource"; |
36ad976b | 358 | else if (rr_type == boost::net::dns::type_mx) |
e91538f0 | 359 | GlobalLogger.debug() << LogPrefix << "mx resource"; |
36ad976b | 360 | else if (rr_type == boost::net::dns::type_txt) |
e91538f0 | 361 | GlobalLogger.debug() << LogPrefix << "txt resource"; |
36ad976b | 362 | else if (rr_type == boost::net::dns::type_srv) |
e91538f0 | 363 | GlobalLogger.debug() << LogPrefix << "srv resource"; |
36ad976b | 364 | else if (rr_type == boost::net::dns::type_axfr) |
e91538f0 | 365 | GlobalLogger.debug() << LogPrefix << "axfr resource"; |
36ad976b | 366 | else |
ad83004d CH |
367 | GlobalLogger.debug() << LogPrefix << "unknown resource type: " |
368 | << std::showbase << std::hex | |
369 | << static_cast<unsigned>(rr_item->rtype()); | |
36ad976b | 370 | } |
36ad976b CH |
371 | } |
372 | ||
373 | ||
cd71d095 | 374 | void DnsResolver::handle_unavailable(const int recursion_count) |
36ad976b CH |
375 | { |
376 | // schedule new attempt in quite a while | |
4e7b6ff9 | 377 | StaleDataLongtermTimer.expires_from_now( |
fd62d09f | 378 | seconds(Config::StaleDataLongtermSeconds)); |
36ad976b | 379 | StaleDataLongtermTimer.async_wait( |
cd71d095 CH |
380 | boost::bind( &DnsResolver::wait_timer_timeout_handler, this, |
381 | recursion_count, | |
382 | boost::asio::placeholders::error | |
36ad976b CH |
383 | ) |
384 | ); | |
fd62d09f | 385 | LongtermTimerIsActive = true; |
36ad976b CH |
386 | |
387 | // for now, admit failure | |
3f489614 | 388 | RequestId = 0; // do not accept answers from old requests |
36ad976b | 389 | bool was_success = false; |
cd71d095 | 390 | finalize_resolve(was_success, recursion_count); |
36ad976b CH |
391 | } |
392 | ||
e18c1337 | 393 | |
cd71d095 CH |
394 | void DnsResolver::handle_ips(const int recursion_count, |
395 | const std::vector<host_addr_pair> &result_ips) | |
36ad976b | 396 | { |
cd71d095 | 397 | // received at least one IP which could be for the queried host name |
e18c1337 CH |
398 | // or the cname at the "end" of the cname list; |
399 | // but all IPs should be for the same | |
400 | HostAddressVec addr_list; | |
401 | std::string only_host_for_ips = result_ips[0].first; | |
402 | BOOST_FOREACH( const host_addr_pair &host_and_addr, result_ips) | |
403 | { | |
404 | if ( host_and_addr.first != only_host_for_ips ) | |
49b82a1d | 405 | GlobalLogger.info() << LogPrefix |
e18c1337 CH |
406 | << "Received IPs for different hosts " << only_host_for_ips |
407 | << " and " << host_and_addr.first << " in one DNS result! " | |
408 | << "--> ignore second"; | |
409 | else | |
410 | { | |
49b82a1d | 411 | GlobalLogger.info() << LogPrefix << "Found IP " |
e18c1337 CH |
412 | << host_and_addr.second.get_ip() << " with TTL " |
413 | << host_and_addr.second.get_ttl().get_value() << "s"; | |
414 | addr_list.push_back(host_and_addr.second); | |
415 | } | |
416 | } | |
417 | ResolverBase::update_cache( only_host_for_ips, addr_list ); | |
36ad976b | 418 | |
e18c1337 CH |
419 | // clean up |
420 | bool was_success = true; | |
cd71d095 | 421 | finalize_resolve(was_success, recursion_count); |
e18c1337 CH |
422 | } |
423 | ||
424 | ||
cd71d095 CH |
425 | void DnsResolver::handle_cname(const int recursion_count, |
426 | const std::vector<src_cname_pair> &result_cnames) | |
e18c1337 CH |
427 | { |
428 | // find the "last" cname in the list | |
429 | // Hostname --> cname1 --> cname2 --> ... --> cnameN | |
430 | // We assume here that this list might not be in order but that all cnames | |
cd71d095 CH |
431 | // form a single list (form one connected list and not several independent |
432 | // lists) | |
dbe986b9 | 433 | |
e18c1337 CH |
434 | std::string last_cname = ""; |
435 | bool could_be_last; | |
dbe986b9 | 436 | BOOST_REVERSE_FOREACH( const src_cname_pair &host_and_cname, result_cnames ) |
e18c1337 CH |
437 | { |
438 | could_be_last = true; | |
dbe986b9 | 439 | BOOST_REVERSE_FOREACH( const src_cname_pair &other, result_cnames ) |
e18c1337 | 440 | { |
23f51766 | 441 | if (other.first == host_and_cname.second.Host) |
e18c1337 CH |
442 | { // found cname for current cname |
443 | could_be_last = false; | |
444 | break; | |
445 | } | |
446 | } | |
447 | if (could_be_last) | |
448 | { | |
23f51766 | 449 | last_cname = host_and_cname.second.Host; |
e18c1337 CH |
450 | break; |
451 | } | |
452 | } | |
453 | ||
454 | if (last_cname.empty()) | |
455 | { | |
49b82a1d | 456 | GlobalLogger.info() << LogPrefix |
e18c1337 CH |
457 | << "Could not identify \"last\" CNAME to handle -- " |
458 | << "maybe we encountered a CNAME loop? Anyway, cannot proceed!"; | |
459 | GlobalLogger.info() << LogPrefix << "Result CNAMEs were:"; | |
dbe986b9 | 460 | BOOST_FOREACH( const src_cname_pair &host_and_cname, result_cnames ) |
e18c1337 | 461 | GlobalLogger.info() << LogPrefix << host_and_cname.first << " --> " |
23f51766 | 462 | << host_and_cname.second.Host; |
cd71d095 | 463 | handle_unavailable(recursion_count); |
e18c1337 CH |
464 | } |
465 | else | |
466 | { // check cache for IP for this cname | |
467 | bool check_up_to_date = true; | |
468 | HostAddressVec cached_data = Cache->get_ips_recursive(last_cname, | |
469 | check_up_to_date); | |
470 | if ( !cached_data.empty() ) | |
471 | { | |
472 | bool was_success = true; | |
cd71d095 | 473 | finalize_resolve(was_success, recursion_count+1); |
e18c1337 CH |
474 | } |
475 | else | |
476 | { // get resolver for canonical name | |
e18c1337 CH |
477 | ResolverItem resolver = DnsMaster::get_instance() |
478 | ->get_resolver_for(last_cname, Protocol); | |
479 | callback_type callback = boost::bind( | |
480 | &DnsResolver::cname_resolve_callback, | |
481 | this, _1, _2 ); | |
cd71d095 | 482 | resolver->async_resolve( callback, recursion_count+1 ); |
e18c1337 CH |
483 | |
484 | // treat a CNAME as a partial result: not enough to run callbacks | |
485 | // from finalize_resolve, but enough to stop timers and reset | |
486 | // RetryCount --> name resolution can take longer | |
72be9e7d | 487 | stop_trying(true); |
e18c1337 CH |
488 | } |
489 | } | |
36ad976b CH |
490 | } |
491 | ||
492 | ||
cd71d095 CH |
493 | /** |
494 | * the recursion_count here is really the one from the recursion, not the one | |
495 | * forwarded from async_resolve! | |
496 | */ | |
e18c1337 | 497 | void DnsResolver::cname_resolve_callback(const bool was_success, |
cd71d095 | 498 | const int recursion_count) |
36ad976b | 499 | { |
e18c1337 CH |
500 | if ( OperationCancelled ) |
501 | { // async_resolve was cancelled --> callbacks already called | |
502 | GlobalLogger.info() << LogPrefix | |
503 | << "Ignoring CNAME results since we were cancelled"; | |
504 | return; | |
505 | } | |
506 | else if (was_success) | |
72be9e7d | 507 | { |
cd71d095 CH |
508 | GlobalLogger.debug() << LogPrefix << "CNAME resolution succeeded after " |
509 | << recursion_count << " recursions"; | |
510 | finalize_resolve(was_success, recursion_count); | |
72be9e7d | 511 | } |
96779587 | 512 | else |
72be9e7d | 513 | { |
cd71d095 CH |
514 | GlobalLogger.info() << LogPrefix << "CNAME resolution failed after " |
515 | << recursion_count << " recursions"; | |
e18c1337 | 516 | // no use to schedule retry in this case since cname resolver must have |
23f51766 | 517 | // failed several times and we can only re-start the same procedure with |
72be9e7d | 518 | // the same information. But can re-try later |
cd71d095 | 519 | handle_unavailable(recursion_count); |
72be9e7d | 520 | } |
dbe986b9 CH |
521 | } |
522 | ||
523 | ||
cd71d095 CH |
524 | /** |
525 | * @brief always called at end of resolving process | |
526 | * | |
527 | * runs callbacks, resets timers and checks state consistency; only thing that | |
528 | * is "left alive" is the long-term timer that might cause a re-start of | |
529 | * resolution after a while | |
530 | * | |
531 | * @param was_success: indicates whether resolution was successfull | |
532 | * @param recursion_count number of recursions or (if not successfull) negative | |
533 | * value indicating who called this function | |
534 | */ | |
96779587 | 535 | void DnsResolver::finalize_resolve(const bool was_success, |
cd71d095 | 536 | const int recursion_count) |
96779587 | 537 | { |
e18c1337 CH |
538 | // some consistency checks; failure might indicate a situation I had not |
539 | // anticipated during programming but might not be harmfull yet | |
540 | if ( !IsResolving ) | |
541 | GlobalLogger.warning() << LogPrefix << "Consistency check failed: " | |
542 | << "not resolving any more!"; | |
543 | if ( OperationCancelled ) | |
544 | GlobalLogger.warning() << LogPrefix << "Consistency check failed: " | |
545 | << " was cancelled!"; | |
546 | if ( ResolverBase::CallbackList.empty() ) | |
547 | GlobalLogger.warning() << LogPrefix << "Consistency check failed: " | |
548 | << "no callbacks!"; | |
549 | if ( RequestId != 0 ) | |
550 | GlobalLogger.warning() << LogPrefix << "Consistency check failed: " | |
551 | << "waiting for DNS reply!"; | |
552 | ||
96779587 | 553 | // stop timers |
72be9e7d | 554 | stop_trying(was_success); |
96779587 CH |
555 | |
556 | // schedule callbacks, clearing callback list | |
cd71d095 | 557 | ResolverBase::schedule_callbacks(was_success, recursion_count); |
96779587 CH |
558 | |
559 | // finalize | |
49b82a1d | 560 | GlobalLogger.info() << LogPrefix << "finalized resolve" |
96779587 | 561 | << " with success = " << was_success |
cd71d095 | 562 | << " and recursion_count = " << recursion_count; |
96779587 CH |
563 | IsResolving = false; |
564 | } | |
565 | ||
e18c1337 | 566 | |
72be9e7d CH |
567 | /** |
568 | * arg was_success determines if stop trying forever or just for the moment | |
569 | * --> determines if we cancel StaleDataLongtermTimer or not | |
570 | */ | |
571 | void DnsResolver::stop_trying(bool was_success) | |
96779587 CH |
572 | { |
573 | // cancel timers | |
e91538f0 | 574 | GlobalLogger.debug() << LogPrefix << "Cancelling timers"; |
96779587 CH |
575 | ResolveTimeoutTimer.cancel(); |
576 | PauseBeforeRetryTimer.cancel(); | |
72be9e7d CH |
577 | |
578 | if (was_success) | |
579 | { | |
580 | StaleDataLongtermTimer.cancel(); | |
fd62d09f | 581 | LongtermTimerIsActive = false; |
72be9e7d | 582 | } |
96779587 CH |
583 | |
584 | // clean up | |
585 | RetryCount = 0; | |
586 | } | |
587 | ||
e18c1337 | 588 | |
72be9e7d CH |
589 | /** |
590 | * return true if resolver is currently resolving | |
591 | * | |
592 | * Is true from call to async_resolve until callbacks | |
593 | * --> returns true if waiting for result or (short-term) retry | |
594 | * | |
cd71d095 | 595 | * However, does NOT tell you if the (long-term) stale timeout is active! |
72be9e7d CH |
596 | * That timer has no effect on result, need to check is_waiting_to_resolve |
597 | * for that | |
598 | */ | |
599 | bool DnsResolver::is_resolving() const | |
e18c1337 CH |
600 | { |
601 | return IsResolving; | |
602 | } | |
603 | ||
604 | ||
dbe986b9 | 605 | /** |
72be9e7d | 606 | * returns true if either is_resolving or the long-term timer is active |
cd71d095 | 607 | * |
72be9e7d CH |
608 | * is_resolving returns true if the short-term retry timer is active |
609 | */ | |
610 | bool DnsResolver::is_waiting_to_resolve() const | |
611 | { | |
fd62d09f | 612 | return IsResolving || LongtermTimerIsActive; |
72be9e7d CH |
613 | } |
614 | ||
615 | ||
616 | /** | |
dbe986b9 CH |
617 | * cancel a earlier call to async_resolve |
618 | * | |
619 | * callbacks will be called with was_success=false; all internal operations | |
620 | * will be cancelled and internal callbacks (timers, dns results) have no | |
72be9e7d | 621 | * effect any more; cancels also the long-term stale-data timer |
dbe986b9 | 622 | */ |
e18c1337 CH |
623 | void DnsResolver::cancel_resolve() |
624 | { | |
fd62d09f | 625 | if ( !IsResolving && !LongtermTimerIsActive) |
e18c1337 | 626 | { |
fd62d09f CH |
627 | GlobalLogger.info() << LogPrefix << "Cancel called on non-resolving, " |
628 | << "non-waiting resolver -- ignore"; | |
e18c1337 CH |
629 | return; |
630 | } | |
631 | else if (OperationCancelled) | |
632 | { | |
633 | GlobalLogger.info() << LogPrefix | |
634 | << "Cancel called on cancelled resolver -- ignore"; | |
635 | return; | |
636 | } | |
fd62d09f | 637 | GlobalLogger.info() << LogPrefix << "Cancel resolver"; |
e18c1337 | 638 | |
e18c1337 | 639 | // set before finalize_resolve so can check in finalize_resolve that ID is |
dbe986b9 | 640 | // always 0; ID is not used any more since handle_dns_result stops if |
e18c1337 CH |
641 | // OperationCancelled is true |
642 | RequestId = 0; | |
643 | ||
fd62d09f CH |
644 | if ( IsResolving ) |
645 | { | |
646 | bool was_success = false; | |
cd71d095 CH |
647 | int recursion_count = -1; |
648 | finalize_resolve(was_success, recursion_count); | |
fd62d09f | 649 | } |
e18c1337 | 650 | |
72be9e7d CH |
651 | // also cancel the long-term timer |
652 | StaleDataLongtermTimer.cancel(); | |
fd62d09f | 653 | LongtermTimerIsActive = false; |
72be9e7d | 654 | |
cd71d095 | 655 | // set after finalize_resolve, so can check in finalize_resolve that |
dbe986b9 | 656 | // OperationCancelled is never true |
e18c1337 CH |
657 | OperationCancelled = true; |
658 | ||
659 | } | |
660 | ||
661 | ||
cd71d095 CH |
662 | void DnsResolver::handle_resolve_timeout(const int recursion_count, |
663 | const boost::system::error_code &error) | |
96779587 CH |
664 | { |
665 | if ( error == boost::asio::error::operation_aborted ) // cancelled | |
666 | { | |
fd62d09f CH |
667 | GlobalLogger.debug() << LogPrefix |
668 | << "Resolve timeout timer was cancelled!"; | |
96779587 CH |
669 | return; |
670 | } | |
671 | else if (error) | |
672 | { | |
49b82a1d | 673 | GlobalLogger.info() << LogPrefix |
e91538f0 | 674 | << "resolve timeout handler received error " |
fd62d09f | 675 | << error << " --> request retry"; |
cd71d095 | 676 | schedule_retry(recursion_count); |
96779587 | 677 | } |
e18c1337 CH |
678 | else if ( OperationCancelled ) |
679 | { // async_resolve was cancelled --> callbacks already called | |
680 | GlobalLogger.info() << LogPrefix | |
dbe986b9 | 681 | << "Ignoring DNS timeout since we were cancelled"; |
e18c1337 CH |
682 | return; |
683 | } | |
684 | else | |
685 | { | |
49b82a1d | 686 | GlobalLogger.info() << LogPrefix << "DNS resolving timed out"; |
cd71d095 | 687 | schedule_retry(recursion_count); |
e18c1337 CH |
688 | } |
689 | } | |
690 | ||
96779587 | 691 | |
cd71d095 | 692 | void DnsResolver::schedule_retry(const int recursion_count) |
e18c1337 | 693 | { |
23f51766 | 694 | // cancel timers |
e18c1337 CH |
695 | ResolveTimeoutTimer.cancel(); |
696 | PauseBeforeRetryTimer.cancel(); | |
36ad976b | 697 | |
96779587 CH |
698 | // increment timer |
699 | ++RetryCount; | |
700 | ||
923626c0 CH |
701 | if ( RetryCount > DnsMaster::get_instance() |
702 | ->get_max_address_resolution_attempts() ) | |
e18c1337 CH |
703 | { // too many re-tries |
704 | GlobalLogger.info() << LogPrefix << "Not scheduling a retry since " | |
705 | << "RetryCount " << RetryCount << " too high"; | |
cd71d095 CH |
706 | handle_unavailable(recursion_count); // will call stop_trying |
707 | } // --> reset RetryCount | |
96779587 CH |
708 | else |
709 | { // schedule retry | |
e18c1337 CH |
710 | GlobalLogger.info() << LogPrefix << "Scheduling a retry (RetryCount=" |
711 | << RetryCount << ")"; | |
96779587 | 712 | PauseBeforeRetryTimer.expires_from_now( |
4e7b6ff9 | 713 | seconds(Config::PauseBeforeRetrySeconds)); |
96779587 | 714 | PauseBeforeRetryTimer.async_wait( |
4e7b6ff9 | 715 | boost::bind( &DnsResolver::wait_timer_timeout_handler, |
cd71d095 CH |
716 | this, recursion_count, |
717 | boost::asio::placeholders::error) ); | |
96779587 CH |
718 | } |
719 | } | |
720 | ||
cd71d095 | 721 | void DnsResolver::wait_timer_timeout_handler( const int recursion_count, |
96779587 CH |
722 | const boost::system::error_code &error) |
723 | { | |
724 | if ( error == boost::asio::error::operation_aborted ) // cancelled | |
e18c1337 CH |
725 | { // assume that our code cancelled this timer, so callbacks will be |
726 | // taken care of! | |
fd62d09f CH |
727 | GlobalLogger.debug() << LogPrefix |
728 | << "Resolve wait timer was cancelled! "; | |
e18c1337 | 729 | } |
96779587 | 730 | else if (error) |
e18c1337 CH |
731 | { // not sure what to do here, but callers waiting forever for a callback |
732 | // is probably the worst thing to happen, so call finalize_resolve | |
49b82a1d | 733 | GlobalLogger.info() << LogPrefix |
e18c1337 CH |
734 | << "resolve wait handler received error " |
735 | << error << "! Try to finalize resolve"; | |
736 | bool was_success = false; | |
cd71d095 | 737 | finalize_resolve(was_success, recursion_count); |
e18c1337 CH |
738 | } |
739 | else if ( OperationCancelled ) | |
740 | { // async_resolve was cancelled --> callbacks already called | |
741 | GlobalLogger.info() << LogPrefix | |
742 | << "Ignoring waiting timeout since we were cancelled"; | |
743 | return; | |
744 | } | |
96779587 CH |
745 | else |
746 | { | |
e91538f0 | 747 | GlobalLogger.info() << LogPrefix << "Done waiting --> re-try resolve"; |
fd62d09f | 748 | IsResolving = false; // will be set to true immediately in do_resolve |
cd71d095 | 749 | do_resolve(recursion_count); |
96779587 | 750 | } |
36ad976b CH |
751 | } |
752 | ||
753 | ||
96779587 CH |
754 | //============================================================================== |
755 | // RETRIEVAL | |
756 | //============================================================================== | |
757 | ||
26b0f687 | 758 | HostAddress DnsResolver::get_next_ip(bool check_up_to_date) |
36ad976b CH |
759 | { |
760 | // get cached data | |
26b0f687 | 761 | // (do not use arg check_up_to_date here in order to give NextIpIndex |
fd62d09f | 762 | // a chance to stay above number of outdated IPs) |
e18c1337 | 763 | HostAddressVec cached_data = ResolverBase::get_cached_ips_recursively(); |
96779587 CH |
764 | |
765 | // if no results cached, return default-constructed HostAddress (0.0.0.0) | |
26b0f687 | 766 | HostAddress return_candidate; |
96779587 | 767 | if ( cached_data.empty() ) |
4e7b6ff9 | 768 | { |
26b0f687 CH |
769 | GlobalLogger.debug() << LogPrefix << "Get next IP: nothing cached"; |
770 | return return_candidate; | |
4e7b6ff9 | 771 | } |
36ad976b | 772 | |
fd62d09f | 773 | std::size_t n_iter = 0; |
26b0f687 CH |
774 | std::size_t n_ips = cached_data.size(); |
775 | uint32_t ttl_thresh = static_cast<uint32_t>( DnsMaster::get_instance() | |
776 | ->get_resolved_ip_ttl_threshold() ); | |
36ad976b | 777 | |
26b0f687 CH |
778 | GlobalLogger.info() << LogPrefix << "Get next IP from cached result of " |
779 | << n_ips << " IPs; first index to consider is " << NextIpIndex | |
72be9e7d | 780 | << "; TTL thresh=" << ttl_thresh << "s is used: " << check_up_to_date; |
26b0f687 CH |
781 | |
782 | // loop until we have found a cached result (that is up to date) | |
783 | // or until we have tried all cached IPs | |
784 | while (true) | |
785 | { | |
786 | // check index since cache size may have changed since last call | |
787 | if (NextIpIndex >= n_ips) | |
72be9e7d CH |
788 | { |
789 | GlobalLogger.debug() << LogPrefix << "Reset NextIpIndex"; | |
26b0f687 | 790 | NextIpIndex = 0; |
72be9e7d | 791 | } |
26b0f687 | 792 | else if ( n_iter >= n_ips) |
72be9e7d CH |
793 | { |
794 | GlobalLogger.debug() << LogPrefix << "No IP found"; | |
26b0f687 | 795 | return HostAddress(); // have checked all candidates |
72be9e7d | 796 | } |
26b0f687 CH |
797 | else |
798 | { // there are candidates left to consider | |
fd62d09f CH |
799 | GlobalLogger.debug() << LogPrefix << "Check IP candidate at index " |
800 | << NextIpIndex; | |
26b0f687 CH |
801 | return_candidate = cached_data[NextIpIndex++]; |
802 | if (!check_up_to_date) | |
fd62d09f CH |
803 | { |
804 | GlobalLogger.debug() << LogPrefix << "not checking ttl, accept"; | |
26b0f687 | 805 | return return_candidate; |
fd62d09f CH |
806 | } |
807 | else if (return_candidate.get_ttl().get_updated_value() | |
26b0f687 | 808 | > ttl_thresh) |
fd62d09f CH |
809 | { |
810 | GlobalLogger.debug() << LogPrefix << "is up to date, accept"; | |
72be9e7d | 811 | return return_candidate; |
fd62d09f | 812 | } |
26b0f687 | 813 | else |
fd62d09f CH |
814 | { |
815 | GlobalLogger.debug() << LogPrefix << "is out of date (" | |
816 | << return_candidate.get_ttl().get_updated_value() | |
817 | << "s <= " << ttl_thresh << "s), continue"; | |
26b0f687 | 818 | ++n_iter; |
fd62d09f | 819 | } |
26b0f687 CH |
820 | } |
821 | } | |
36ad976b CH |
822 | } |
823 | ||
923626c0 CH |
824 | bool DnsResolver::have_up_to_date_ip() |
825 | { | |
fd62d09f | 826 | return get_resolved_ip_count(true) > 0; |
923626c0 CH |
827 | } |
828 | ||
fd62d09f | 829 | int DnsResolver::get_resolved_ip_count(const bool check_up_to_date) |
923626c0 | 830 | { |
fd62d09f CH |
831 | // run with empty hostname --> uses internal var Hostname |
832 | return ResolverBase::get_cached_ips_recursively("",check_up_to_date).size(); | |
923626c0 CH |
833 | } |
834 |