OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_DNS_DNS_TRANSACTION_H_ | 5 #ifndef NET_DNS_DNS_TRANSACTION_H_ |
6 #define NET_DNS_DNS_TRANSACTION_H_ | 6 #define NET_DNS_DNS_TRANSACTION_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
| 9 #include <set> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
10 #include <vector> | 12 #include <vector> |
11 | 13 |
12 #include "base/memory/ref_counted.h" | 14 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/timer.h" | 16 #include "base/timer.h" |
15 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
16 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
17 #include "net/base/ip_endpoint.h" | 19 #include "net/base/ip_endpoint.h" |
18 #include "net/base/net_export.h" | 20 #include "net/base/net_export.h" |
19 #include "net/base/net_log.h" | 21 #include "net/base/net_log.h" |
20 #include "net/base/rand_callback.h" | 22 #include "net/base/rand_callback.h" |
21 | 23 |
22 namespace net { | 24 namespace net { |
23 | 25 |
| 26 class ClientSocketFactory; |
24 class DatagramClientSocket; | 27 class DatagramClientSocket; |
25 class DnsQuery; | 28 class DnsQuery; |
26 class DnsResponse; | 29 class DnsResponse; |
27 class DnsSession; | |
28 | 30 |
29 // Performs a single asynchronous DNS transaction over UDP, | 31 // Performs (with fixed retries) a single asynchronous DNS transaction, |
30 // which consists of sending out a DNS query, waiting for a response, and | 32 // which consists of sending out a DNS query, waiting for a response, and |
31 // returning the response that it matches. | 33 // parsing and returning the IP addresses that it matches. |
32 class NET_EXPORT_PRIVATE DnsTransaction : | 34 class NET_EXPORT_PRIVATE DnsTransaction : |
33 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 35 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
34 public: | 36 public: |
35 typedef base::Callback<void(DnsTransaction*, int)> ResultCallback; | 37 typedef std::pair<std::string, uint16> Key; |
36 | 38 |
37 // Create new transaction using the parameters and state in |session|. | 39 // Interface that should be implemented by DnsTransaction consumers and |
38 // Issues query for name |qname| (in DNS format) type |qtype| and class IN. | 40 // passed to the |Start| method to be notified when the transaction has |
39 // Calls |callback| on completion or timeout. | 41 // completed. |
40 // TODO(szym): change dependency to (IPEndPoint, Socket, DnsQuery, callback) | 42 class NET_EXPORT_PRIVATE Delegate { |
41 DnsTransaction(DnsSession* session, | 43 public: |
42 const base::StringPiece& qname, | 44 Delegate(); |
43 uint16 qtype, | 45 virtual ~Delegate(); |
44 const ResultCallback& callback, | 46 |
45 const BoundNetLog& source_net_log); | 47 // A consumer of DnsTransaction should override |OnTransactionComplete| |
| 48 // and call |set_delegate(this)|. The method will be called once the |
| 49 // resolution has completed, results passed in as arguments. |
| 50 virtual void OnTransactionComplete( |
| 51 int result, |
| 52 const DnsTransaction* transaction, |
| 53 const IPAddressList& ip_addresses); |
| 54 |
| 55 private: |
| 56 friend class DnsTransaction; |
| 57 |
| 58 void Attach(DnsTransaction* transaction); |
| 59 void Detach(DnsTransaction* transaction); |
| 60 |
| 61 std::set<DnsTransaction*> registered_transactions_; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 64 }; |
| 65 |
| 66 // |dns_server| is the address of the DNS server, |dns_name| is the |
| 67 // hostname (in DNS format) to be resolved, |query_type| is the type of |
| 68 // the query, either kDNS_A or kDNS_AAAA, |rand_int| is the PRNG used for |
| 69 // generating DNS query. |
| 70 DnsTransaction(const IPEndPoint& dns_server, |
| 71 const std::string& dns_name, |
| 72 uint16 query_type, |
| 73 const RandIntCallback& rand_int, |
| 74 ClientSocketFactory* socket_factory, |
| 75 const BoundNetLog& source_net_log, |
| 76 NetLog* net_log); |
46 ~DnsTransaction(); | 77 ~DnsTransaction(); |
47 | 78 void SetDelegate(Delegate* delegate); |
48 const DnsQuery* query() const { return query_.get(); } | 79 const Key& key() const { return key_; } |
49 | |
50 const DnsResponse* response() const { return response_.get(); } | |
51 | 80 |
52 // Starts the resolution process. Will return ERR_IO_PENDING and will | 81 // Starts the resolution process. Will return ERR_IO_PENDING and will |
53 // notify the caller via |delegate|. Should only be called once. | 82 // notify the caller via |delegate|. Should only be called once. |
54 int Start(); | 83 int Start(); |
55 | 84 |
56 private: | 85 private: |
| 86 FRIEND_TEST_ALL_PREFIXES(DnsTransactionTest, FirstTimeoutTest); |
| 87 FRIEND_TEST_ALL_PREFIXES(DnsTransactionTest, SecondTimeoutTest); |
| 88 FRIEND_TEST_ALL_PREFIXES(DnsTransactionTest, ThirdTimeoutTest); |
| 89 |
57 enum State { | 90 enum State { |
58 STATE_CONNECT, | 91 STATE_CONNECT, |
59 STATE_CONNECT_COMPLETE, | 92 STATE_CONNECT_COMPLETE, |
60 STATE_SEND_QUERY, | 93 STATE_SEND_QUERY, |
61 STATE_SEND_QUERY_COMPLETE, | 94 STATE_SEND_QUERY_COMPLETE, |
62 STATE_READ_RESPONSE, | 95 STATE_READ_RESPONSE, |
63 STATE_READ_RESPONSE_COMPLETE, | 96 STATE_READ_RESPONSE_COMPLETE, |
64 STATE_NONE, | 97 STATE_NONE, |
65 }; | 98 }; |
66 | 99 |
67 int DoLoop(int result); | 100 int DoLoop(int result); |
68 void DoCallback(int result); | 101 void DoCallback(int result); |
69 void OnIOComplete(int result); | 102 void OnIOComplete(int result); |
70 | 103 |
71 int DoConnect(); | 104 int DoConnect(); |
72 int DoConnectComplete(int result); | 105 int DoConnectComplete(int result); |
73 int DoSendQuery(); | 106 int DoSendQuery(); |
74 int DoSendQueryComplete(int result); | 107 int DoSendQueryComplete(int result); |
75 int DoReadResponse(); | 108 int DoReadResponse(); |
76 int DoReadResponseComplete(int result); | 109 int DoReadResponseComplete(int result); |
77 | 110 |
78 // Fixed number of attempts are made to send a query and read a response, | 111 // Fixed number of attempts are made to send a query and read a response, |
79 // and at the start of each, a timer is started with increasing delays. | 112 // and at the start of each, a timer is started with increasing delays. |
80 void StartTimer(base::TimeDelta delay); | 113 void StartTimer(base::TimeDelta delay); |
81 void RevokeTimer(); | 114 void RevokeTimer(); |
82 void OnTimeout(); | 115 void OnTimeout(); |
83 | 116 |
84 scoped_refptr<DnsSession> session_; | 117 // This is to be used by unit tests only. |
85 IPEndPoint dns_server_; | 118 void set_timeouts_ms(const std::vector<base::TimeDelta>& timeouts_ms); |
| 119 |
| 120 const IPEndPoint dns_server_; |
| 121 Key key_; |
| 122 IPAddressList ip_addresses_; |
| 123 Delegate* delegate_; |
| 124 |
86 scoped_ptr<DnsQuery> query_; | 125 scoped_ptr<DnsQuery> query_; |
87 ResultCallback callback_; | |
88 scoped_ptr<DnsResponse> response_; | 126 scoped_ptr<DnsResponse> response_; |
89 scoped_ptr<DatagramClientSocket> socket_; | 127 scoped_ptr<DatagramClientSocket> socket_; |
90 | 128 |
91 // Number of retry attempts so far. | 129 // Number of retry attempts so far. |
92 int attempts_; | 130 size_t attempts_; |
| 131 |
| 132 // Timeouts in milliseconds. |
| 133 std::vector<base::TimeDelta> timeouts_ms_; |
93 | 134 |
94 State next_state_; | 135 State next_state_; |
| 136 ClientSocketFactory* socket_factory_; |
95 base::OneShotTimer<DnsTransaction> timer_; | 137 base::OneShotTimer<DnsTransaction> timer_; |
96 OldCompletionCallbackImpl<DnsTransaction> io_callback_; | 138 OldCompletionCallbackImpl<DnsTransaction> io_callback_; |
97 | 139 |
98 BoundNetLog net_log_; | 140 BoundNetLog net_log_; |
99 | 141 |
100 DISALLOW_COPY_AND_ASSIGN(DnsTransaction); | 142 DISALLOW_COPY_AND_ASSIGN(DnsTransaction); |
101 }; | 143 }; |
102 | 144 |
103 } // namespace net | 145 } // namespace net |
104 | 146 |
105 #endif // NET_DNS_DNS_TRANSACTION_H_ | 147 #endif // NET_DNS_DNS_TRANSACTION_H_ |
OLD | NEW |