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