David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #ifndef NET_DNS_DNS_SOCKET_POOL_H_ |
| 6 | #define NET_DNS_DNS_SOCKET_POOL_H_ |
| 7 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 8 | #include <memory> |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 11 | #include "base/macros.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 12 | #include "net/base/net_export.h" |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 13 | #include "net/base/rand_callback.h" |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
| 17 | class ClientSocketFactory; |
| 18 | class DatagramClientSocket; |
| 19 | class IPEndPoint; |
| 20 | class NetLog; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 21 | struct NetLogSource; |
| 22 | class StreamSocket; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 23 | |
| 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. |
| 27 | class NET_EXPORT_PRIVATE DnsSocketPool { |
| 28 | public: |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 29 | virtual ~DnsSocketPool(); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 30 | |
| 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 Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 34 | static std::unique_ptr<DnsSocketPool> CreateDefault( |
| 35 | ClientSocketFactory* factory, |
| 36 | const RandIntCallback& rand_int_callback); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 37 | |
| 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 Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 41 | static std::unique_ptr<DnsSocketPool> CreateNull( |
| 42 | ClientSocketFactory* factory, |
| 43 | const RandIntCallback& rand_int_callback); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 44 | |
| 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 Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 56 | // by |server_index|. May return a std::unique_ptr to NULL if no sockets are |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 57 | // available to reuse and the factory fails to produce a socket (or produces |
| 58 | // one on which Connect fails). |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 59 | virtual std::unique_ptr<DatagramClientSocket> AllocateSocket( |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 60 | unsigned server_index) = 0; |
| 61 | |
| 62 | // Frees a socket allocated by AllocateSocket. |server_index| must be the |
| 63 | // same index passed to AllocateSocket. |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 64 | 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 Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 71 | |
| 72 | protected: |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 73 | DnsSocketPool(ClientSocketFactory* socket_factory, |
| 74 | const RandIntCallback& rand_int_callback); |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 75 | |
| 76 | void InitializeInternal( |
| 77 | const std::vector<IPEndPoint>* nameservers, |
| 78 | NetLog* net_log); |
| 79 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 80 | std::unique_ptr<DatagramClientSocket> CreateConnectedSocket( |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 81 | unsigned server_index); |
| 82 | |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 83 | // Returns a random int in the specified range. |
| 84 | int GetRandomInt(int min, int max); |
| 85 | |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 86 | private: |
| 87 | ClientSocketFactory* socket_factory_; |
Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 88 | const RandIntCallback rand_int_callback_; |
David Ghandehari | 9e5b587 | 2016-07-28 09:50:04 -0700 | [diff] [blame] | 89 | 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_ |