Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/dns_client.h" |
| 6 | |
| 7 | #include <arpa/inet.h> |
| 8 | #include <netdb.h> |
| 9 | #include <netinet/in.h> |
| 10 | #include <sys/socket.h> |
| 11 | |
| 12 | #include <map> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 13 | #include <memory> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 14 | #include <set> |
| 15 | #include <string> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 18 | #include <base/bind.h> |
| 19 | #include <base/bind_helpers.h> |
| 20 | #include <base/stl_util.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 21 | #include <base/strings/string_number_conversions.h> |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 22 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 23 | #include "shill/logging.h" |
Darin Petkov | 89593a9 | 2012-02-21 10:59:48 +0100 | [diff] [blame] | 24 | #include "shill/shill_ares.h" |
| 25 | #include "shill/shill_time.h" |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 26 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 27 | using base::Bind; |
| 28 | using base::Unretained; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 29 | using std::map; |
| 30 | using std::set; |
| 31 | using std::string; |
| 32 | using std::vector; |
| 33 | |
| 34 | namespace shill { |
| 35 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 36 | const char DNSClient::kErrorNoData[] = "The query response contains no answers"; |
| 37 | const char DNSClient::kErrorFormErr[] = "The server says the query is bad"; |
| 38 | const char DNSClient::kErrorServerFail[] = "The server says it had a failure"; |
| 39 | const char DNSClient::kErrorNotFound[] = "The queried-for domain was not found"; |
| 40 | const char DNSClient::kErrorNotImp[] = "The server doesn't implement operation"; |
| 41 | const char DNSClient::kErrorRefused[] = "The server replied, refused the query"; |
| 42 | const char DNSClient::kErrorBadQuery[] = "Locally we could not format a query"; |
| 43 | const char DNSClient::kErrorNetRefused[] = "The network connection was refused"; |
| 44 | const char DNSClient::kErrorTimedOut[] = "The network connection was timed out"; |
| 45 | const char DNSClient::kErrorUnknown[] = "DNS Resolver unknown internal error"; |
| 46 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 47 | const int DNSClient::kDefaultDNSPort = 53; |
| 48 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 49 | // Private to the implementation of resolver so callers don't include ares.h |
| 50 | struct DNSClientState { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 51 | DNSClientState() : channel(nullptr), start_time{} {} |
Darin Petkov | e636c69 | 2012-05-31 10:22:17 +0200 | [diff] [blame] | 52 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 53 | ares_channel channel; |
Ben Chan | e2ee5e0 | 2014-09-19 19:29:42 -0700 | [diff] [blame] | 54 | map<ares_socket_t, std::shared_ptr<IOHandler>> read_handlers; |
| 55 | map<ares_socket_t, std::shared_ptr<IOHandler>> write_handlers; |
Darin Petkov | e636c69 | 2012-05-31 10:22:17 +0200 | [diff] [blame] | 56 | struct timeval start_time; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | DNSClient::DNSClient(IPAddress::Family family, |
| 60 | const string &interface_name, |
| 61 | const vector<string> &dns_servers, |
| 62 | int timeout_ms, |
| 63 | EventDispatcher *dispatcher, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 64 | const ClientCallback &callback) |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 65 | : address_(IPAddress(family)), |
| 66 | interface_name_(interface_name), |
| 67 | dns_servers_(dns_servers), |
| 68 | dispatcher_(dispatcher), |
| 69 | callback_(callback), |
| 70 | timeout_ms_(timeout_ms), |
| 71 | running_(false), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 72 | weak_ptr_factory_(this), |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 73 | ares_(Ares::GetInstance()), |
| 74 | time_(Time::GetInstance()) {} |
| 75 | |
| 76 | DNSClient::~DNSClient() { |
| 77 | Stop(); |
| 78 | } |
| 79 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 80 | bool DNSClient::Start(const string &hostname, Error *error) { |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 81 | if (running_) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 82 | Error::PopulateAndLog(error, Error::kInProgress, |
| 83 | "Only one DNS request is allowed at a time"); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if (!resolver_state_.get()) { |
| 88 | struct ares_options options; |
| 89 | memset(&options, 0, sizeof(options)); |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 90 | options.timeout = timeout_ms_; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 91 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 92 | if (dns_servers_.empty()) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 93 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 94 | "No valid DNS server addresses"); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 98 | resolver_state_.reset(new DNSClientState); |
| 99 | int status = ares_->InitOptions(&resolver_state_->channel, |
| 100 | &options, |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 101 | ARES_OPT_TIMEOUTMS); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 102 | if (status != ARES_SUCCESS) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 103 | Error::PopulateAndLog(error, Error::kOperationFailed, |
| 104 | "ARES initialization returns error code: " + |
| 105 | base::IntToString(status)); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 106 | resolver_state_.reset(); |
| 107 | return false; |
| 108 | } |
| 109 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 110 | // Format DNS server addresses string as "host:port[,host:port...]" to be |
| 111 | // used in call to ares_set_servers_csv for setting DNS server addresses. |
| 112 | // There is a bug in ares library when parsing IPv6 addresses, where it |
| 113 | // always assumes the port number are specified when address contains ":". |
| 114 | // So when IPv6 address are given without port number as "xx:xx:xx::yy",the |
| 115 | // parser would parse the address as "xx:xx:xx:" and port number as "yy". |
| 116 | // To work around this bug, port number are added to each address. |
| 117 | // |
| 118 | // Alternatively, we can use ares_set_servers instead, where we would |
| 119 | // explicitly construct a link list of ares_addr_node. |
| 120 | string server_addresses; |
| 121 | bool first = true; |
| 122 | for (const auto &ip : dns_servers_) { |
| 123 | if (!first) { |
| 124 | server_addresses += ","; |
| 125 | } else { |
| 126 | first = false; |
| 127 | } |
| 128 | server_addresses += (ip + ":" + base::IntToString(kDefaultDNSPort)); |
| 129 | } |
| 130 | status = ares_->SetServersCsv(resolver_state_->channel, |
| 131 | server_addresses.c_str()); |
| 132 | if (status != ARES_SUCCESS) { |
| 133 | Error::PopulateAndLog(error, Error::kOperationFailed, |
| 134 | "ARES set DNS servers error code: " + |
| 135 | base::IntToString(status)); |
| 136 | resolver_state_.reset(); |
| 137 | return false; |
| 138 | } |
| 139 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 140 | ares_->SetLocalDev(resolver_state_->channel, interface_name_.c_str()); |
| 141 | } |
| 142 | |
| 143 | running_ = true; |
Darin Petkov | e636c69 | 2012-05-31 10:22:17 +0200 | [diff] [blame] | 144 | time_->GetTimeMonotonic(&resolver_state_->start_time); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 145 | ares_->GetHostByName(resolver_state_->channel, hostname.c_str(), |
| 146 | address_.family(), ReceiveDNSReplyCB, this); |
| 147 | |
| 148 | if (!RefreshHandles()) { |
| 149 | LOG(ERROR) << "Impossibly short timeout."; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 150 | error->CopyFrom(error_); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 151 | Stop(); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | void DNSClient::Stop() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 159 | SLOG(DNS, 3) << "In " << __func__; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 160 | if (!resolver_state_.get()) { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | running_ = false; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 165 | weak_ptr_factory_.InvalidateWeakPtrs(); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 166 | error_.Reset(); |
| 167 | address_.SetAddressToDefault(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 168 | ares_->Destroy(resolver_state_->channel); |
| 169 | resolver_state_.reset(); |
| 170 | } |
| 171 | |
Prathmesh Prabhu | 3e452f8 | 2013-04-10 16:31:44 -0700 | [diff] [blame] | 172 | bool DNSClient::IsActive() const { |
| 173 | return running_; |
| 174 | } |
| 175 | |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 176 | // We delay our call to completion so that we exit all IOHandlers, and |
| 177 | // can clean up all of our local state before calling the callback, or |
| 178 | // during the process of the execution of the callee (which is free to |
| 179 | // call our destructor safely). |
| 180 | void DNSClient::HandleCompletion() { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 181 | SLOG(DNS, 3) << "In " << __func__; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 182 | Error error; |
| 183 | error.CopyFrom(error_); |
| 184 | IPAddress address(address_); |
| 185 | if (!error.IsSuccess()) { |
| 186 | // If the DNS request did not succeed, do not trust it for future |
| 187 | // attempts. |
| 188 | Stop(); |
| 189 | } else { |
| 190 | // Prepare our state for the next request without destroying the |
| 191 | // current ARES state. |
| 192 | error_.Reset(); |
| 193 | address_.SetAddressToDefault(); |
| 194 | } |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 195 | callback_.Run(error, address); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 198 | void DNSClient::HandleDNSRead(int fd) { |
| 199 | ares_->ProcessFd(resolver_state_->channel, fd, ARES_SOCKET_BAD); |
| 200 | RefreshHandles(); |
| 201 | } |
| 202 | |
| 203 | void DNSClient::HandleDNSWrite(int fd) { |
| 204 | ares_->ProcessFd(resolver_state_->channel, ARES_SOCKET_BAD, fd); |
| 205 | RefreshHandles(); |
| 206 | } |
| 207 | |
| 208 | void DNSClient::HandleTimeout() { |
| 209 | ares_->ProcessFd(resolver_state_->channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 210 | RefreshHandles(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void DNSClient::ReceiveDNSReply(int status, struct hostent *hostent) { |
| 214 | if (!running_) { |
| 215 | // We can be called during ARES shutdown -- ignore these events. |
| 216 | return; |
| 217 | } |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 218 | SLOG(DNS, 3) << "In " << __func__; |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 219 | running_ = false; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 220 | timeout_closure_.Cancel(); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 221 | dispatcher_->PostTask(Bind(&DNSClient::HandleCompletion, |
| 222 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 223 | |
| 224 | if (status == ARES_SUCCESS && |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 225 | hostent != nullptr && |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 226 | hostent->h_addrtype == address_.family() && |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 227 | static_cast<size_t>(hostent->h_length) == |
| 228 | IPAddress::GetAddressLength(address_.family()) && |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame^] | 229 | hostent->h_addr_list != nullptr && |
| 230 | hostent->h_addr_list[0] != nullptr) { |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 231 | address_ = IPAddress(address_.family(), |
| 232 | ByteString(reinterpret_cast<unsigned char *>( |
| 233 | hostent->h_addr_list[0]), hostent->h_length)); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 234 | } else { |
| 235 | switch (status) { |
| 236 | case ARES_ENODATA: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 237 | error_.Populate(Error::kOperationFailed, kErrorNoData); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 238 | break; |
| 239 | case ARES_EFORMERR: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 240 | error_.Populate(Error::kOperationFailed, kErrorFormErr); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 241 | break; |
| 242 | case ARES_ESERVFAIL: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 243 | error_.Populate(Error::kOperationFailed, kErrorServerFail); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 244 | break; |
| 245 | case ARES_ENOTFOUND: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 246 | error_.Populate(Error::kOperationFailed, kErrorNotFound); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 247 | break; |
| 248 | case ARES_ENOTIMP: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 249 | error_.Populate(Error::kOperationFailed, kErrorNotImp); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 250 | break; |
| 251 | case ARES_EREFUSED: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 252 | error_.Populate(Error::kOperationFailed, kErrorRefused); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 253 | break; |
| 254 | case ARES_EBADQUERY: |
| 255 | case ARES_EBADNAME: |
| 256 | case ARES_EBADFAMILY: |
| 257 | case ARES_EBADRESP: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 258 | error_.Populate(Error::kOperationFailed, kErrorBadQuery); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 259 | break; |
| 260 | case ARES_ECONNREFUSED: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 261 | error_.Populate(Error::kOperationFailed, kErrorNetRefused); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 262 | break; |
| 263 | case ARES_ETIMEOUT: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 264 | error_.Populate(Error::kOperationTimeout, kErrorTimedOut); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 265 | break; |
| 266 | default: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 267 | error_.Populate(Error::kOperationFailed, kErrorUnknown); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 268 | if (status == ARES_SUCCESS) { |
| 269 | LOG(ERROR) << "ARES returned success but hostent was invalid!"; |
| 270 | } else { |
| 271 | LOG(ERROR) << "ARES returned unhandled error status " << status; |
| 272 | } |
| 273 | break; |
| 274 | } |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | void DNSClient::ReceiveDNSReplyCB(void *arg, int status, |
| 279 | int /*timeouts*/, |
| 280 | struct hostent *hostent) { |
| 281 | DNSClient *res = static_cast<DNSClient *>(arg); |
| 282 | res->ReceiveDNSReply(status, hostent); |
| 283 | } |
| 284 | |
| 285 | bool DNSClient::RefreshHandles() { |
Ben Chan | e2ee5e0 | 2014-09-19 19:29:42 -0700 | [diff] [blame] | 286 | map<ares_socket_t, std::shared_ptr<IOHandler>> old_read = |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 287 | resolver_state_->read_handlers; |
Ben Chan | e2ee5e0 | 2014-09-19 19:29:42 -0700 | [diff] [blame] | 288 | map<ares_socket_t, std::shared_ptr<IOHandler>> old_write = |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 289 | resolver_state_->write_handlers; |
| 290 | |
| 291 | resolver_state_->read_handlers.clear(); |
| 292 | resolver_state_->write_handlers.clear(); |
| 293 | |
| 294 | ares_socket_t sockets[ARES_GETSOCK_MAXNUM]; |
| 295 | int action_bits = ares_->GetSock(resolver_state_->channel, sockets, |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 296 | ARES_GETSOCK_MAXNUM); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 297 | |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 298 | base::Callback<void(int)> read_callback( |
| 299 | Bind(&DNSClient::HandleDNSRead, weak_ptr_factory_.GetWeakPtr())); |
| 300 | base::Callback<void(int)> write_callback( |
| 301 | Bind(&DNSClient::HandleDNSWrite, weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 302 | for (int i = 0; i < ARES_GETSOCK_MAXNUM; i++) { |
| 303 | if (ARES_GETSOCK_READABLE(action_bits, i)) { |
| 304 | if (ContainsKey(old_read, sockets[i])) { |
| 305 | resolver_state_->read_handlers[sockets[i]] = old_read[sockets[i]]; |
| 306 | } else { |
| 307 | resolver_state_->read_handlers[sockets[i]] = |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 308 | std::shared_ptr<IOHandler> ( |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 309 | dispatcher_->CreateReadyHandler(sockets[i], |
| 310 | IOHandler::kModeInput, |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 311 | read_callback)); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | if (ARES_GETSOCK_WRITABLE(action_bits, i)) { |
| 315 | if (ContainsKey(old_write, sockets[i])) { |
| 316 | resolver_state_->write_handlers[sockets[i]] = old_write[sockets[i]]; |
| 317 | } else { |
| 318 | resolver_state_->write_handlers[sockets[i]] = |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 319 | std::shared_ptr<IOHandler> ( |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 320 | dispatcher_->CreateReadyHandler(sockets[i], |
| 321 | IOHandler::kModeOutput, |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 322 | write_callback)); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | if (!running_) { |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 328 | // We are here just to clean up socket handles, and the ARES state was |
| 329 | // cleaned up during the last call to ares_->ProcessFd(). |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
| 333 | // Schedule timer event for the earlier of our timeout or one requested by |
| 334 | // the resolver library. |
| 335 | struct timeval now, elapsed_time, timeout_tv; |
Paul Stewart | e692740 | 2012-01-23 16:11:30 -0800 | [diff] [blame] | 336 | time_->GetTimeMonotonic(&now); |
Darin Petkov | e636c69 | 2012-05-31 10:22:17 +0200 | [diff] [blame] | 337 | timersub(&now, &resolver_state_->start_time, &elapsed_time); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 338 | timeout_tv.tv_sec = timeout_ms_ / 1000; |
| 339 | timeout_tv.tv_usec = (timeout_ms_ % 1000) * 1000; |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 340 | timeout_closure_.Cancel(); |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 341 | |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 342 | if (timercmp(&elapsed_time, &timeout_tv, >=)) { |
| 343 | // There are 3 cases of interest: |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 344 | // - If we got here from Start(), when we return, Stop() will be |
| 345 | // called, so our cleanup task will not run, so we will not have the |
| 346 | // side-effect of both invoking the callback and returning False |
| 347 | // in Start(). |
| 348 | // - If we got here from the tail of an IO event, we can't call |
| 349 | // Stop() since that will blow away the IOHandler we are running |
| 350 | // in. We will perform the cleanup in the posted task below. |
| 351 | // - If we got here from a timeout handler, we will perform cleanup |
| 352 | // in the posted task. |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 353 | running_ = false; |
Paul Stewart | bdb02e6 | 2012-02-22 16:24:33 -0800 | [diff] [blame] | 354 | error_.Populate(Error::kOperationTimeout, kErrorTimedOut); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 355 | dispatcher_->PostTask(Bind(&DNSClient::HandleCompletion, |
| 356 | weak_ptr_factory_.GetWeakPtr())); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } else { |
| 359 | struct timeval max, ret_tv; |
| 360 | timersub(&timeout_tv, &elapsed_time, &max); |
| 361 | struct timeval *tv = ares_->Timeout(resolver_state_->channel, |
| 362 | &max, &ret_tv); |
Paul Stewart | f582b50 | 2012-04-04 21:39:22 -0700 | [diff] [blame] | 363 | timeout_closure_.Reset( |
| 364 | Bind(&DNSClient::HandleTimeout, weak_ptr_factory_.GetWeakPtr())); |
| 365 | dispatcher_->PostDelayedTask(timeout_closure_.callback(), |
| 366 | tv->tv_sec * 1000 + tv->tv_usec / 1000); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | return true; |
| 370 | } |
| 371 | |
| 372 | } // namespace shill |