OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_DNS_DNS_CLIENT_H_ | |
6 #define NET_DNS_DNS_CLIENT_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/callback.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/string_piece.h" | |
15 #include "net/base/net_export.h" | |
16 | |
17 namespace net { | |
18 | |
19 class BoundNetLog; | |
20 class ClientSocketFactory; | |
21 class DnsResponse; | |
22 class DnsSession; | |
23 | |
24 // DnsClient performs asynchronous DNS queries. DnsClient takes care of | |
25 // retransmissions, DNS server fallback (or round-robin), suffix search, and | |
26 // simple response validation ("does it match the query") to fight poisoning. | |
27 // It does NOT perform caching, aggregation or prioritization of requests. | |
28 // | |
29 // Destroying DnsClient does NOT affect any already created Requests. | |
30 // | |
31 // TODO(szym): consider adding flags to MakeRequest to indicate options: | |
32 // -- don't perform suffix search | |
33 // -- query both A and AAAA at once | |
34 // -- accept truncated response (and/or forbid TCP) | |
35 class NET_EXPORT_PRIVATE DnsClient { | |
36 public: | |
37 class Request; | |
38 // Callback for complete requests. Note that DnsResponse might be NULL if | |
39 // the DNS server(s) could not be reached. | |
40 typedef base::Callback<void(Request* req, | |
41 int result, | |
42 const DnsResponse* resp)> RequestCallback; | |
43 | |
44 // A data-holder for a request made to the DnsClient. | |
45 // Destroying the request cancels the underlying network effort. | |
46 class NET_EXPORT_PRIVATE Request { | |
47 public: | |
48 Request(const base::StringPiece& qname, | |
49 uint16 qtype, | |
50 const RequestCallback& callback); | |
51 virtual ~Request(); | |
52 | |
53 const std::string& qname() const { return qname_; } | |
54 | |
55 uint16 qtype() const { return qtype_; } | |
56 | |
57 virtual int Start() = 0; | |
58 | |
59 void DoCallback(int result, const DnsResponse* response) { | |
60 callback_.Run(this, result, response); | |
61 } | |
62 | |
63 private: | |
64 std::string qname_; | |
65 uint16 qtype_; | |
66 RequestCallback callback_; | |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(Request); | |
69 }; | |
70 | |
71 virtual ~DnsClient() {} | |
72 | |
73 // Makes asynchronous DNS query for the given |qname| and |qtype| (assuming | |
74 // QCLASS == IN). The caller is responsible for destroying the returned | |
75 // request whether to cancel it or after its completion. | |
76 // (Destroying DnsClient does not abort the requests.) | |
77 virtual Request* CreateRequest( | |
78 const base::StringPiece& qname, | |
79 uint16 qtype, | |
80 const RequestCallback& callback, | |
81 const BoundNetLog& source_net_log) WARN_UNUSED_RESULT = 0; | |
82 | |
83 // Creates a socket-based DnsClient using the |session|. | |
84 static DnsClient* CreateClient(DnsSession* session) WARN_UNUSED_RESULT; | |
85 }; | |
86 | |
87 } // namespace net | |
88 | |
89 #endif // NET_DNS_DNS_CLIENT_H_ | |
90 | |
OLD | NEW |