| 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_SESSION_H_ | 5 #ifndef NET_DNS_DNS_SESSION_H_ |
| 6 #define NET_DNS_DNS_SESSION_H_ | 6 #define NET_DNS_DNS_SESSION_H_ |
| 7 | 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 8 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/time.h" | 13 #include "base/time.h" |
| 10 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 11 #include "net/base/rand_callback.h" | 15 #include "net/base/rand_callback.h" |
| 12 #include "net/dns/dns_config_service.h" | 16 #include "net/dns/dns_config_service.h" |
| 17 #include "net/udp/datagram_client_socket.h" |
| 13 | 18 |
| 14 namespace net { | 19 namespace net { |
| 15 | 20 |
| 16 class ClientSocketFactory; | 21 class ClientSocketFactory; |
| 17 class NetLog; | 22 class NetLog; |
| 18 | 23 |
| 19 // Session parameters and state shared between DNS transactions. | 24 // Session parameters and state shared between DNS transactions. |
| 20 // Ref-counted so that DnsClient::Request can keep working in absence of | 25 // Ref-counted so that DnsClient::Request can keep working in absence of |
| 21 // DnsClient. A DnsSession must be recreated when DnsConfig changes. | 26 // DnsClient. A DnsSession must be recreated when DnsConfig changes. |
| 22 class NET_EXPORT_PRIVATE DnsSession | 27 class NET_EXPORT_PRIVATE DnsSession |
| 23 : NON_EXPORTED_BASE(public base::RefCounted<DnsSession>) { | 28 : NON_EXPORTED_BASE(public base::RefCounted<DnsSession>) { |
| 24 public: | 29 public: |
| 25 typedef base::Callback<int()> RandCallback; | 30 typedef base::Callback<int()> RandCallback; |
| 26 | 31 |
| 32 class SocketLease { |
| 33 public: |
| 34 SocketLease(scoped_refptr<DnsSession> session, |
| 35 int server_index, |
| 36 scoped_ptr<DatagramClientSocket> socket); |
| 37 ~SocketLease(); |
| 38 |
| 39 DatagramClientSocket* socket() { return socket_.get(); } |
| 40 |
| 41 private: |
| 42 scoped_refptr<DnsSession> session_; |
| 43 int server_index_; |
| 44 scoped_ptr<DatagramClientSocket> socket_; |
| 45 }; |
| 46 |
| 27 DnsSession(const DnsConfig& config, | 47 DnsSession(const DnsConfig& config, |
| 28 ClientSocketFactory* factory, | 48 ClientSocketFactory* factory, |
| 29 const RandIntCallback& rand_int_callback, | 49 const RandIntCallback& rand_int_callback, |
| 30 NetLog* net_log); | 50 NetLog* net_log); |
| 31 | 51 |
| 32 const DnsConfig& config() const { return config_; } | 52 const DnsConfig& config() const { return config_; } |
| 33 NetLog* net_log() const { return net_log_; } | 53 NetLog* net_log() const { return net_log_; } |
| 34 | 54 |
| 35 ClientSocketFactory* socket_factory() { return socket_factory_; } | |
| 36 | |
| 37 // Return the next random query ID. | 55 // Return the next random query ID. |
| 38 int NextQueryId() const; | 56 int NextQueryId() const; |
| 39 | 57 |
| 40 // Return the index of the first configured server to use on first attempt. | 58 // Return the index of the first configured server to use on first attempt. |
| 41 int NextFirstServerIndex(); | 59 int NextFirstServerIndex(); |
| 42 | 60 |
| 43 // Return the timeout for the next query. | 61 // Return the timeout for the next query. |
| 44 base::TimeDelta NextTimeout(int attempt); | 62 base::TimeDelta NextTimeout(int attempt); |
| 45 | 63 |
| 64 // Allocate a socket, already connected to the server address. |
| 65 // TODO(szym): add NetLog param? |
| 66 scoped_ptr<SocketLease> AllocateSocket(int server_index); |
| 67 |
| 46 private: | 68 private: |
| 47 friend class base::RefCounted<DnsSession>; | 69 friend class base::RefCounted<DnsSession>; |
| 70 typedef std::vector<DatagramClientSocket*> SocketVector; |
| 71 typedef std::vector<SocketVector> SocketPool; |
| 72 |
| 48 ~DnsSession(); | 73 ~DnsSession(); |
| 49 | 74 |
| 75 // Release a socket. |
| 76 void FreeSocket(int server_index, scoped_ptr<DatagramClientSocket> socket); |
| 77 |
| 50 const DnsConfig config_; | 78 const DnsConfig config_; |
| 51 ClientSocketFactory* socket_factory_; | 79 ClientSocketFactory* socket_factory_; |
| 52 RandCallback rand_callback_; | 80 RandCallback rand_callback_; |
| 53 NetLog* net_log_; | 81 NetLog* net_log_; |
| 54 | 82 |
| 83 SocketPool sockets_; |
| 84 |
| 55 // Current index into |config_.nameservers| to begin resolution with. | 85 // Current index into |config_.nameservers| to begin resolution with. |
| 56 int server_index_; | 86 int server_index_; |
| 57 | 87 |
| 58 // TODO(szym): Add current RTT estimate. | 88 // TODO(szym): Add current RTT estimate. |
| 59 // TODO(szym): Add TCP connection pool to support DNS over TCP. | 89 // TODO(szym): Add TCP connection pool to support DNS over TCP. |
| 60 // TODO(szym): Add UDP port pool to avoid NAT table overload. | |
| 61 | 90 |
| 62 DISALLOW_COPY_AND_ASSIGN(DnsSession); | 91 DISALLOW_COPY_AND_ASSIGN(DnsSession); |
| 63 }; | 92 }; |
| 64 | 93 |
| 65 } // namespace net | 94 } // namespace net |
| 66 | 95 |
| 67 #endif // NET_DNS_DNS_SESSION_H_ | 96 #endif // NET_DNS_DNS_SESSION_H_ |
| OLD | NEW |