| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | 9 #include <string> |
| 10 #include <vector> | |
| 11 | 10 |
| 12 #include "base/memory/ref_counted.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/string_piece.h" | |
| 15 #include "base/timer.h" | |
| 16 #include "base/threading/non_thread_safe.h" | |
| 17 #include "net/base/completion_callback.h" | |
| 18 #include "net/base/ip_endpoint.h" | |
| 19 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 20 #include "net/base/net_log.h" | |
| 21 #include "net/base/rand_callback.h" | |
| 22 | 15 |
| 23 namespace net { | 16 namespace net { |
| 24 | 17 |
| 25 class DatagramClientSocket; | 18 class BoundNetLog; |
| 26 class DnsQuery; | |
| 27 class DnsResponse; | 19 class DnsResponse; |
| 28 class DnsSession; | 20 class DnsSession; |
| 29 | 21 |
| 30 // Performs a single asynchronous DNS transaction over UDP, | 22 // DnsTransaction implements a stub DNS resolver as defined in RFC 1034. |
| 31 // which consists of sending out a DNS query, waiting for a response, and | 23 // The DnsTransaction takes care of retransmissions, name server fallback (or |
| 32 // returning the response that it matches. | 24 // round-robin), suffix search, and simple response validation ("does it match |
| 33 class NET_EXPORT_PRIVATE DnsTransaction : | 25 // the query") to fight poisoning. |
| 34 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 26 // |
| 27 // Destroying DnsTransaction cancels the underlying network effort. |
| 28 class NET_EXPORT_PRIVATE DnsTransaction { |
| 35 public: | 29 public: |
| 36 typedef base::Callback<void(DnsTransaction*, int)> ResultCallback; | 30 virtual ~DnsTransaction() {} |
| 37 | 31 |
| 38 // Create new transaction using the parameters and state in |session|. | 32 // Returns the original |hostname|. |
| 39 // Issues query for name |qname| (in DNS format) type |qtype| and class IN. | 33 virtual const std::string& GetHostname() const = 0; |
| 40 // Calls |callback| on completion or timeout. | |
| 41 // TODO(szym): change dependency to (IPEndPoint, Socket, DnsQuery, callback) | |
| 42 DnsTransaction(DnsSession* session, | |
| 43 const base::StringPiece& qname, | |
| 44 uint16 qtype, | |
| 45 const ResultCallback& callback, | |
| 46 const BoundNetLog& source_net_log); | |
| 47 ~DnsTransaction(); | |
| 48 | 34 |
| 49 const DnsQuery* query() const { return query_.get(); } | 35 // Returns the |qtype|. |
| 36 virtual uint16 GetType() const = 0; |
| 50 | 37 |
| 51 const DnsResponse* response() const { return response_.get(); } | 38 // Starts the transaction. Returns the net error on synchronous failure or |
| 39 // ERR_IO_PENDING in which case the result will be passed via the callback. |
| 40 virtual int Start() = 0; |
| 41 }; |
| 52 | 42 |
| 53 // Starts the resolution process. Will return ERR_IO_PENDING and will | 43 // Creates DnsTransaction which performs asynchronous DNS search. |
| 54 // notify the caller via |delegate|. Should only be called once. | 44 // It does NOT perform caching, aggregation or prioritization of transactions. |
| 55 int Start(); | 45 // |
| 46 // Destroying the factory does NOT affect any already created DnsTransactions. |
| 47 class NET_EXPORT_PRIVATE DnsTransactionFactory { |
| 48 public: |
| 49 // Called with the response or NULL if no matching response was received. |
| 50 // Note that the |GetDottedName()| of the response may be different than the |
| 51 // original |hostname| as a result of suffix search. |
| 52 typedef base::Callback<void(DnsTransaction* transaction, |
| 53 int neterror, |
| 54 const DnsResponse* response)> CallbackType; |
| 56 | 55 |
| 57 private: | 56 virtual ~DnsTransactionFactory() {} |
| 58 enum State { | |
| 59 STATE_CONNECT, | |
| 60 STATE_CONNECT_COMPLETE, | |
| 61 STATE_SEND_QUERY, | |
| 62 STATE_SEND_QUERY_COMPLETE, | |
| 63 STATE_READ_RESPONSE, | |
| 64 STATE_READ_RESPONSE_COMPLETE, | |
| 65 STATE_NONE, | |
| 66 }; | |
| 67 | 57 |
| 68 int DoLoop(int result); | 58 // Creates DnsTransaction for the given |hostname| and |qtype| (assuming |
| 69 void DoCallback(int result); | 59 // QCLASS is IN). |hostname| should be in the dotted form. A dot at the end |
| 70 void OnIOComplete(int result); | 60 // implies the domain name is fully-qualified and will be exempt from suffix |
| 61 // search. |hostname| should not be an IP literal. |
| 62 // |
| 63 // The transaction will run |callback| upon asynchronous completion. |
| 64 // The source of |source_net_log| is used as source dependency in log. |
| 65 virtual scoped_ptr<DnsTransaction> CreateTransaction( |
| 66 const std::string& hostname, |
| 67 uint16 qtype, |
| 68 const CallbackType& callback, |
| 69 const BoundNetLog& source_net_log) WARN_UNUSED_RESULT = 0; |
| 71 | 70 |
| 72 int DoConnect(); | 71 // Creates a DnsTransactionFactory which creates DnsTransactionImpl using the |
| 73 int DoConnectComplete(int result); | 72 // |session|. |
| 74 int DoSendQuery(); | 73 static scoped_ptr<DnsTransactionFactory> CreateFactory( |
| 75 int DoSendQueryComplete(int result); | 74 DnsSession* session) WARN_UNUSED_RESULT; |
| 76 int DoReadResponse(); | |
| 77 int DoReadResponseComplete(int result); | |
| 78 | |
| 79 // Fixed number of attempts are made to send a query and read a response, | |
| 80 // and at the start of each, a timer is started with increasing delays. | |
| 81 void StartTimer(base::TimeDelta delay); | |
| 82 void RevokeTimer(); | |
| 83 void OnTimeout(); | |
| 84 | |
| 85 scoped_refptr<DnsSession> session_; | |
| 86 IPEndPoint dns_server_; | |
| 87 scoped_ptr<DnsQuery> query_; | |
| 88 ResultCallback callback_; | |
| 89 scoped_ptr<DnsResponse> response_; | |
| 90 scoped_ptr<DatagramClientSocket> socket_; | |
| 91 | |
| 92 // Number of retry attempts so far. | |
| 93 int attempts_; | |
| 94 | |
| 95 State next_state_; | |
| 96 base::OneShotTimer<DnsTransaction> timer_; | |
| 97 | |
| 98 BoundNetLog net_log_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(DnsTransaction); | |
| 101 }; | 75 }; |
| 102 | 76 |
| 103 } // namespace net | 77 } // namespace net |
| 104 | 78 |
| 105 #endif // NET_DNS_DNS_TRANSACTION_H_ | 79 #endif // NET_DNS_DNS_TRANSACTION_H_ |
| 80 |
| OLD | NEW |