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