1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/dns_transaction.h"
#include "base/bind.h"
#include "base/rand_util.h"
#include "base/values.h"
#include "net/base/dns_util.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/dns/dns_query.h"
#include "net/dns/dns_response.h"
#include "net/socket/client_socket_factory.h"
#include "net/udp/datagram_client_socket.h"
namespace net {
namespace {
// Retry timeouts.
const int kTimeoutsMs[] = {3000, 5000, 11000};
const int kMaxAttempts = arraysize(kTimeoutsMs);
// Returns the string representation of an IPAddressNumber.
std::string IPAddressToString(const IPAddressNumber& ip_address) {
IPEndPoint ip_endpoint(ip_address, 0);
struct sockaddr_storage addr;
size_t addr_len = sizeof(addr);
struct sockaddr* sockaddr = reinterpret_cast<struct sockaddr*>(&addr);
if (!ip_endpoint.ToSockAddr(sockaddr, &addr_len))
return "";
return NetAddressToString(sockaddr, addr_len);
}
}
DnsTransaction::Delegate::Delegate() {
}
DnsTransaction::Delegate::~Delegate() {
while (!registered_transactions_.empty()) {
DnsTransaction* transaction = *registered_transactions_.begin();
transaction->SetDelegate(NULL);
}
DCHECK(registered_transactions_.empty());
}
void DnsTransaction::Delegate::OnTransactionComplete(
int result,
const DnsTransaction* transaction,
const IPAddressList& ip_addresses) {
}
void DnsTransaction::Delegate::Attach(DnsTransaction* transaction) {
DCHECK(registered_transactions_.find(transaction) ==
registered_transactions_.end());
registered_transactions_.insert(transaction);
}
void DnsTransaction::Delegate::Detach(DnsTransaction* transaction) {
DCHECK(registered_transactions_.find(transaction) !=
registered_transactions_.end());
registered_transactions_.erase(transaction);
}
namespace {
class DnsTransactionStartParameters : public NetLog::EventParameters {
public:
DnsTransactionStartParameters(const IPEndPoint& dns_server,
const DnsTransaction::Key& key,
const NetLog::Source& source)
: dns_server_(dns_server), key_(key), source_(source) {}
virtual Value* ToValue() const {
std::string hostname;
DnsResponseBuffer(
reinterpret_cast<const uint8*>(key_.first.c_str()), key_.first.size()).
DNSName(&hostname);
DictionaryValue* dict = new DictionaryValue();
dict->SetString("dns_server", dns_server_.ToString());
dict->SetString("hostname", hostname);
dict->SetInteger("query_type", key_.second);
dict->Set("source_dependency", source_.ToValue());
return dict;
}
private:
const IPEndPoint dns_server_;
const DnsTransaction::Key key_;
const NetLog::Source source_;
};
class DnsTransactionFinishParameters : public NetLog::EventParameters {
public:
DnsTransactionFinishParameters(int net_error,
const IPAddressList& ip_address_list)
: net_error_(net_error), ip_address_list_(ip_address_list) {}
virtual Value* ToValue() const {
ListValue* list = new ListValue();
for (IPAddressList::const_iterator it = ip_address_list_.begin();
it != ip_address_list_.end(); ++it)
list->Append(Value::CreateStringValue(IPAddressToString(*it)));
DictionaryValue* dict = new DictionaryValue();
dict->SetInteger("net_error", net_error_);
dict->Set("address_list", list);
return dict;
}
private:
const int net_error_;
const IPAddressList ip_address_list_;
};
class DnsTransactionRetryParameters : public NetLog::EventParameters {
public:
DnsTransactionRetryParameters(int attempt_number,
const NetLog::Source& source)
: attempt_number_(attempt_number), source_(source) {}
virtual Value* ToValue() const {
DictionaryValue* dict = new DictionaryValue();
dict->SetInteger("attempt_number", attempt_number_);
dict->Set("source_dependency", source_.ToValue());
return dict;
}
private:
const int attempt_number_;
const NetLog::Source source_;
};
} // namespace
DnsTransaction::DnsTransaction(const IPEndPoint& dns_server,
const std::string& dns_name,
uint16 query_type,
const RandIntCallback& rand_int,
ClientSocketFactory* socket_factory,
const BoundNetLog& source_net_log,
NetLog* net_log)
: dns_server_(dns_server),
key_(dns_name, query_type),
delegate_(NULL),
query_(new DnsQuery(dns_name, query_type, rand_int)),
attempts_(0),
next_state_(STATE_NONE),
socket_factory_(socket_factory ? socket_factory :
ClientSocketFactory::GetDefaultFactory()),
ALLOW_THIS_IN_INITIALIZER_LIST(
io_callback_(this, &DnsTransaction::OnIOComplete)),
net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_DNS_TRANSACTION)) {
DCHECK(!rand_int.is_null());
for (size_t i = 0; i < arraysize(kTimeoutsMs); ++i)
timeouts_ms_.push_back(base::TimeDelta::FromMilliseconds(kTimeoutsMs[i]));
net_log_.BeginEvent(
NetLog::TYPE_DNS_TRANSACTION,
make_scoped_refptr(
new DnsTransactionStartParameters(dns_server_, key_,
source_net_log.source())));
}
DnsTransaction::~DnsTransaction() {
SetDelegate(NULL);
}
void DnsTransaction::SetDelegate(Delegate* delegate) {
if (delegate == delegate_)
return;
if (delegate_)
delegate_->Detach(this);
delegate_ = delegate;
if (delegate_)
delegate_->Attach(this);
}
int DnsTransaction::Start() {
DCHECK_EQ(STATE_NONE, next_state_);
next_state_ = STATE_CONNECT;
return DoLoop(OK);
}
int DnsTransaction::DoLoop(int result) {
DCHECK_NE(STATE_NONE, next_state_);
int rv = result;
do {
State state = next_state_;
next_state_ = STATE_NONE;
switch (state) {
case STATE_CONNECT:
rv = DoConnect();
break;
case STATE_CONNECT_COMPLETE:
rv = DoConnectComplete(rv);
break;
case STATE_SEND_QUERY:
rv = DoSendQuery();
break;
case STATE_SEND_QUERY_COMPLETE:
rv = DoSendQueryComplete(rv);
break;
case STATE_READ_RESPONSE:
rv = DoReadResponse();
break;
case STATE_READ_RESPONSE_COMPLETE:
rv = DoReadResponseComplete(rv);
break;
default:
NOTREACHED();
break;
}
} while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
return rv;
}
void DnsTransaction::DoCallback(int result) {
DCHECK_NE(result, ERR_IO_PENDING);
net_log_.EndEvent(
NetLog::TYPE_DNS_TRANSACTION,
make_scoped_refptr(
new DnsTransactionFinishParameters(result, ip_addresses_)));
if (delegate_)
delegate_->OnTransactionComplete(result, this, ip_addresses_);
}
void DnsTransaction::OnIOComplete(int result) {
int rv = DoLoop(result);
if (rv != ERR_IO_PENDING)
DoCallback(rv);
}
int DnsTransaction::DoConnect() {
next_state_ = STATE_CONNECT_COMPLETE;
DCHECK_LT(attempts_, timeouts_ms_.size());
StartTimer(timeouts_ms_[attempts_]);
attempts_++;
// TODO(agayev): keep all sockets around in case the server responds
// after its timeout; state machine will need to change to handle that.
socket_.reset(socket_factory_->CreateDatagramClientSocket(
DatagramSocket::RANDOM_BIND,
base::Bind(&base::RandInt),
NULL,
net_log_.source()));
net_log_.AddEvent(
NetLog::TYPE_DNS_TRANSACTION_ATTEMPT_STARTED,
make_scoped_refptr(
new DnsTransactionRetryParameters(attempts_,
socket_->NetLog().source())));
return socket_->Connect(dns_server_);
}
int DnsTransaction::DoConnectComplete(int rv) {
if (rv < 0)
return rv;
next_state_ = STATE_SEND_QUERY;
return OK;
}
int DnsTransaction::DoSendQuery() {
next_state_ = STATE_SEND_QUERY_COMPLETE;
return socket_->Write(query_->io_buffer(),
query_->io_buffer()->size(),
&io_callback_);
}
int DnsTransaction::DoSendQueryComplete(int rv) {
if (rv < 0)
return rv;
// Writing to UDP should not result in a partial datagram.
if (rv != query_->io_buffer()->size())
return ERR_NAME_NOT_RESOLVED;
next_state_ = STATE_READ_RESPONSE;
return OK;
}
int DnsTransaction::DoReadResponse() {
next_state_ = STATE_READ_RESPONSE_COMPLETE;
response_.reset(new DnsResponse(query_.get()));
return socket_->Read(response_->io_buffer(),
response_->io_buffer()->size(),
&io_callback_);
}
int DnsTransaction::DoReadResponseComplete(int rv) {
DCHECK_NE(ERR_IO_PENDING, rv);
RevokeTimer();
if (rv < 0)
return rv;
DCHECK(rv);
// TODO(agayev): when supporting EDNS0 we may need to do multiple reads
// to read the whole response.
return response_->Parse(rv, &ip_addresses_);
}
void DnsTransaction::StartTimer(base::TimeDelta delay) {
timer_.Start(delay, this, &DnsTransaction::OnTimeout);
}
void DnsTransaction::RevokeTimer() {
timer_.Stop();
}
void DnsTransaction::OnTimeout() {
DCHECK(next_state_ == STATE_SEND_QUERY_COMPLETE ||
next_state_ == STATE_READ_RESPONSE_COMPLETE);
if (attempts_ == timeouts_ms_.size()) {
DoCallback(ERR_DNS_TIMED_OUT);
return;
}
next_state_ = STATE_CONNECT;
query_.reset(query_->CloneWithNewId());
int rv = DoLoop(OK);
if (rv != ERR_IO_PENDING)
DoCallback(rv);
}
void DnsTransaction::set_timeouts_ms(
const std::vector<base::TimeDelta>& timeouts_ms) {
DCHECK_EQ(0u, attempts_);
timeouts_ms_ = timeouts_ms;
}
} // namespace net
|