blob: f4f00bba6e4480f06ecaef3e6f03c83aba9821a9 [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// 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.
4
5#ifndef NET_DNS_DNS_SOCKET_POOL_H_
6#define NET_DNS_DNS_SOCKET_POOL_H_
7
Andrew Top0d1858f2019-05-15 22:01:47 -07008#include <memory>
David Ghandehari9e5b5872016-07-28 09:50:04 -07009#include <vector>
10
Andrew Top0d1858f2019-05-15 22:01:47 -070011#include "base/macros.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070012#include "net/base/net_export.h"
Andrew Top0d1858f2019-05-15 22:01:47 -070013#include "net/base/rand_callback.h"
David Ghandehari9e5b5872016-07-28 09:50:04 -070014
15namespace net {
16
17class ClientSocketFactory;
18class DatagramClientSocket;
19class IPEndPoint;
20class NetLog;
Andrew Top0d1858f2019-05-15 22:01:47 -070021struct NetLogSource;
22class StreamSocket;
David Ghandehari9e5b5872016-07-28 09:50:04 -070023
24// A DnsSocketPool is an abstraction layer around a ClientSocketFactory that
25// allows preallocation, reuse, or other strategies to manage sockets connected
26// to DNS servers.
27class NET_EXPORT_PRIVATE DnsSocketPool {
28 public:
Andrew Top0d1858f2019-05-15 22:01:47 -070029 virtual ~DnsSocketPool();
David Ghandehari9e5b5872016-07-28 09:50:04 -070030
31 // Creates a DnsSocketPool that implements the default strategy for managing
32 // sockets. (This varies by platform; see DnsSocketPoolImpl in
33 // dns_socket_pool.cc for details.)
Andrew Top0d1858f2019-05-15 22:01:47 -070034 static std::unique_ptr<DnsSocketPool> CreateDefault(
35 ClientSocketFactory* factory,
36 const RandIntCallback& rand_int_callback);
David Ghandehari9e5b5872016-07-28 09:50:04 -070037
38 // Creates a DnsSocketPool that implements a "null" strategy -- no sockets are
39 // preallocated, allocation requests are satisfied by calling the factory
40 // directly, and returned sockets are deleted immediately.
Andrew Top0d1858f2019-05-15 22:01:47 -070041 static std::unique_ptr<DnsSocketPool> CreateNull(
42 ClientSocketFactory* factory,
43 const RandIntCallback& rand_int_callback);
David Ghandehari9e5b5872016-07-28 09:50:04 -070044
45 // Initializes the DnsSocketPool. |nameservers| is the list of nameservers
46 // for which the DnsSocketPool will manage sockets; |net_log| is the NetLog
47 // used when constructing sockets with the factory.
48 //
49 // Initialize may not be called more than once, and must be called before
50 // calling AllocateSocket or FreeSocket.
51 virtual void Initialize(
52 const std::vector<IPEndPoint>* nameservers,
53 NetLog* net_log) = 0;
54
55 // Allocates a socket that is already connected to the nameserver referenced
Andrew Top0d1858f2019-05-15 22:01:47 -070056 // by |server_index|. May return a std::unique_ptr to NULL if no sockets are
David Ghandehari9e5b5872016-07-28 09:50:04 -070057 // available to reuse and the factory fails to produce a socket (or produces
58 // one on which Connect fails).
Andrew Top0d1858f2019-05-15 22:01:47 -070059 virtual std::unique_ptr<DatagramClientSocket> AllocateSocket(
David Ghandehari9e5b5872016-07-28 09:50:04 -070060 unsigned server_index) = 0;
61
62 // Frees a socket allocated by AllocateSocket. |server_index| must be the
63 // same index passed to AllocateSocket.
Andrew Top0d1858f2019-05-15 22:01:47 -070064 virtual void FreeSocket(unsigned server_index,
65 std::unique_ptr<DatagramClientSocket> socket) = 0;
66
67 // Creates a StreamSocket from the factory for a transaction over TCP. These
68 // sockets are not pooled.
69 std::unique_ptr<StreamSocket> CreateTCPSocket(unsigned server_index,
70 const NetLogSource& source);
David Ghandehari9e5b5872016-07-28 09:50:04 -070071
72 protected:
Andrew Top0d1858f2019-05-15 22:01:47 -070073 DnsSocketPool(ClientSocketFactory* socket_factory,
74 const RandIntCallback& rand_int_callback);
David Ghandehari9e5b5872016-07-28 09:50:04 -070075
76 void InitializeInternal(
77 const std::vector<IPEndPoint>* nameservers,
78 NetLog* net_log);
79
Andrew Top0d1858f2019-05-15 22:01:47 -070080 std::unique_ptr<DatagramClientSocket> CreateConnectedSocket(
David Ghandehari9e5b5872016-07-28 09:50:04 -070081 unsigned server_index);
82
Andrew Top0d1858f2019-05-15 22:01:47 -070083 // Returns a random int in the specified range.
84 int GetRandomInt(int min, int max);
85
David Ghandehari9e5b5872016-07-28 09:50:04 -070086 private:
87 ClientSocketFactory* socket_factory_;
Andrew Top0d1858f2019-05-15 22:01:47 -070088 const RandIntCallback rand_int_callback_;
David Ghandehari9e5b5872016-07-28 09:50:04 -070089 NetLog* net_log_;
90 const std::vector<IPEndPoint>* nameservers_;
91 bool initialized_;
92
93 DISALLOW_COPY_AND_ASSIGN(DnsSocketPool);
94};
95
96} // namespace net
97
98#endif // NET_DNS_DNS_SOCKET_POOL_H_