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
3 // found in the LICENSE file.
5 #ifndef NET_DNS_DNS_SESSION_H_
6 #define NET_DNS_DNS_SESSION_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time.h"
13 #include "net/base/net_export.h"
14 #include "net/base/rand_callback.h"
15 #include "net/dns/dns_config_service.h"
16 #include "net/dns/dns_socket_pool.h"
20 class ClientSocketFactory
;
21 class DatagramClientSocket
;
25 // Session parameters and state shared between DNS transactions.
26 // Ref-counted so that DnsClient::Request can keep working in absence of
27 // DnsClient. A DnsSession must be recreated when DnsConfig changes.
28 class NET_EXPORT_PRIVATE DnsSession
29 : NON_EXPORTED_BASE(public base::RefCounted
<DnsSession
>) {
31 typedef base::Callback
<int()> RandCallback
;
33 class NET_EXPORT_PRIVATE SocketLease
{
35 SocketLease(scoped_refptr
<DnsSession
> session
,
36 unsigned server_index
,
37 scoped_ptr
<DatagramClientSocket
> socket
);
40 unsigned server_index() const { return server_index_
; }
42 DatagramClientSocket
* socket() { return socket_
.get(); }
45 scoped_refptr
<DnsSession
> session_
;
46 unsigned server_index_
;
47 scoped_ptr
<DatagramClientSocket
> socket_
;
49 DISALLOW_COPY_AND_ASSIGN(SocketLease
);
52 DnsSession(const DnsConfig
& config
,
53 scoped_ptr
<DnsSocketPool
> socket_pool
,
54 const RandIntCallback
& rand_int_callback
,
57 const DnsConfig
& config() const { return config_
; }
58 NetLog
* net_log() const { return net_log_
; }
60 // Return the next random query ID.
61 int NextQueryId() const;
63 // Return the index of the first configured server to use on first attempt.
64 int NextFirstServerIndex();
66 // Return the timeout for the next query.
67 base::TimeDelta
NextTimeout(int attempt
);
69 // Allocate a socket, already connected to the server address.
70 // When the SocketLease is destroyed, the socket will be freed.
71 scoped_ptr
<SocketLease
> AllocateSocket(unsigned server_index
,
72 const NetLog::Source
& source
);
74 // Creates a StreamSocket from the factory for a transaction over TCP. These
75 // sockets are not pooled.
76 scoped_ptr
<StreamSocket
> CreateTCPSocket(unsigned server_index
,
77 const NetLog::Source
& source
);
80 friend class base::RefCounted
<DnsSession
>;
84 void FreeSocket(unsigned server_index
,
85 scoped_ptr
<DatagramClientSocket
> socket
);
87 const DnsConfig config_
;
88 scoped_ptr
<DnsSocketPool
> socket_pool_
;
89 RandCallback rand_callback_
;
92 // Current index into |config_.nameservers| to begin resolution with.
95 // TODO(szym): Add current RTT estimate.
96 // TODO(szym): Add TCP connection pool to support DNS over TCP.
98 DISALLOW_COPY_AND_ASSIGN(DnsSession
);
103 #endif // NET_DNS_DNS_SESSION_H_