[email protected] | 398dca8 | 2012-04-25 17:54:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 5 | #ifndef NET_SOCKET_UDP_SOCKET_POSIX_H_ |
| 6 | #define NET_SOCKET_UDP_SOCKET_POSIX_H_ |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 7 | |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 8 | #include <stdint.h> |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | #include <sys/types.h> |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 11 | |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 12 | #include <memory> |
| 13 | |
Hans Wennborg | 725d043 | 2020-06-18 13:54:16 | [diff] [blame] | 14 | #include "base/logging.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 17 | #include "base/message_loop/message_pump_for_io.h" |
gab | d43afa1 | 2017-05-30 18:26:24 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 19 | #include "base/timer/timer.h" |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 20 | #include "build/build_config.h" |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 21 | #include "net/base/address_family.h" |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 22 | #include "net/base/completion_once_callback.h" |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 23 | #include "net/base/datagram_buffer.h" |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 24 | #include "net/base/io_buffer.h" |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 25 | #include "net/base/ip_endpoint.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 26 | #include "net/base/net_export.h" |
tfarina | 42f7ce83 | 2015-10-08 20:45:53 | [diff] [blame] | 27 | #include "net/base/network_change_notifier.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 28 | #include "net/log/net_log_with_source.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 29 | #include "net/socket/datagram_socket.h" |
| 30 | #include "net/socket/diff_serv_code_point.h" |
[email protected] | bbfef1f | 2013-08-28 03:00:51 | [diff] [blame] | 31 | #include "net/socket/socket_descriptor.h" |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 32 | #include "net/socket/socket_tag.h" |
Eric Roman | 5a84192 | 2020-08-13 01:28:25 | [diff] [blame] | 33 | #include "net/socket/udp_socket_global_limits.h" |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 34 | #include "net/traffic_annotation/network_traffic_annotation.h" |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 35 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 36 | #if defined(__ANDROID__) && defined(__aarch64__) |
| 37 | #define HAVE_SENDMMSG 1 |
Xiaohan Wang | 2a6845b | 2022-01-08 04:40:57 | [diff] [blame] | 38 | #elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 39 | #define HAVE_SENDMMSG 1 |
| 40 | #else |
| 41 | #define HAVE_SENDMMSG 0 |
| 42 | #endif |
| 43 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 44 | namespace net { |
| 45 | |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 46 | class IPAddress; |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 47 | class NetLog; |
| 48 | struct NetLogSource; |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 49 | class SocketTag; |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 50 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 51 | // Sendresult is inspired by sendmmsg, but unlike sendmmsg it is not |
| 52 | // convenient to require that a positive |write_count| and a negative |
| 53 | // error code are mutually exclusive. |
| 54 | struct NET_EXPORT SendResult { |
| 55 | explicit SendResult(); |
| 56 | ~SendResult(); |
| 57 | SendResult(int rv, int write_count, DatagramBuffers buffers); |
| 58 | SendResult(SendResult& other) = delete; |
| 59 | SendResult& operator=(SendResult& other) = delete; |
| 60 | SendResult(SendResult&& other); |
| 61 | SendResult& operator=(SendResult&& other) = default; |
| 62 | int rv; |
| 63 | // number of successful writes. |
| 64 | int write_count; |
| 65 | DatagramBuffers buffers; |
| 66 | }; |
| 67 | |
| 68 | // Don't delay writes more than this. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 69 | const base::TimeDelta kWriteAsyncMsThreshold = base::Milliseconds(1); |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 70 | // Prefer local if number of writes is not more than this. |
| 71 | const int kWriteAsyncMinBuffersThreshold = 2; |
| 72 | // Don't allow more than this many outstanding async writes. |
| 73 | const int kWriteAsyncMaxBuffersThreshold = 16; |
| 74 | // PostTask immediately when unwritten buffers reaches this. |
| 75 | const int kWriteAsyncPostBuffersThreshold = kWriteAsyncMaxBuffersThreshold / 2; |
| 76 | // Don't unblock writer unless pending async writes are less than this. |
| 77 | const int kWriteAsyncCallbackBuffersThreshold = kWriteAsyncMaxBuffersThreshold; |
| 78 | |
| 79 | // To allow mock |Send|/|Sendmsg| in testing. This has to be |
| 80 | // reference counted thread safe because |SendBuffers| and |
| 81 | // |SendmmsgBuffers| may be invoked in another thread via PostTask*. |
| 82 | class NET_EXPORT UDPSocketPosixSender |
| 83 | : public base::RefCountedThreadSafe<UDPSocketPosixSender> { |
| 84 | public: |
Eric Orth | 7141dbc | 2018-10-26 20:10:24 | [diff] [blame] | 85 | UDPSocketPosixSender(); |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 86 | |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 87 | UDPSocketPosixSender(const UDPSocketPosixSender&) = delete; |
| 88 | UDPSocketPosixSender& operator=(const UDPSocketPosixSender&) = delete; |
| 89 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 90 | SendResult SendBuffers(int fd, DatagramBuffers buffers); |
| 91 | |
| 92 | void SetSendmmsgEnabled(bool enabled) { |
| 93 | #if HAVE_SENDMMSG |
| 94 | sendmmsg_enabled_ = enabled; |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | protected: |
| 99 | friend class base::RefCountedThreadSafe<UDPSocketPosixSender>; |
| 100 | |
| 101 | virtual ~UDPSocketPosixSender(); |
| 102 | virtual ssize_t Send(int sockfd, |
| 103 | const void* buf, |
| 104 | size_t len, |
| 105 | int flags) const; |
| 106 | #if HAVE_SENDMMSG |
| 107 | virtual int Sendmmsg(int sockfd, |
| 108 | struct mmsghdr* msgvec, |
| 109 | unsigned int vlen, |
| 110 | unsigned int flags) const; |
| 111 | #endif |
| 112 | |
| 113 | SendResult InternalSendBuffers(int fd, DatagramBuffers buffers) const; |
| 114 | #if HAVE_SENDMMSG |
| 115 | SendResult InternalSendmmsgBuffers(int fd, DatagramBuffers buffers) const; |
| 116 | #endif |
| 117 | |
| 118 | private: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 119 | bool sendmmsg_enabled_ = false; |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 120 | }; |
| 121 | |
gab | d43afa1 | 2017-05-30 18:26:24 | [diff] [blame] | 122 | class NET_EXPORT UDPSocketPosix { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 123 | public: |
Francois Doray | 32ad367 | 2021-04-15 15:37:39 | [diff] [blame] | 124 | // Performance helper for net::activity_monitor, it batches |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 125 | // throughput samples, subject to a byte limit threshold (64 KB) or |
| 126 | // timer (100 ms), whichever comes first. The batching is subject |
| 127 | // to a minimum number of samples (2) required by NQE to update its |
| 128 | // throughput estimate. |
Francois Doray | 5146d2f0 | 2021-03-30 18:11:57 | [diff] [blame] | 129 | class ReceivedActivityMonitor { |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 130 | public: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 131 | ReceivedActivityMonitor() = default; |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 132 | |
| 133 | ReceivedActivityMonitor(const ReceivedActivityMonitor&) = delete; |
| 134 | ReceivedActivityMonitor& operator=(const ReceivedActivityMonitor&) = delete; |
| 135 | |
Francois Doray | 5146d2f0 | 2021-03-30 18:11:57 | [diff] [blame] | 136 | ~ReceivedActivityMonitor() = default; |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 137 | // Provided by sent/received subclass. |
Francois Doray | 32ad367 | 2021-04-15 15:37:39 | [diff] [blame] | 138 | // Update throughput, but batch to limit overhead of net::activity_monitor. |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 139 | void Increment(uint32_t bytes); |
| 140 | // For flushing cached values. |
| 141 | void OnClose(); |
| 142 | |
| 143 | private: |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 144 | void Update(); |
| 145 | void OnTimerFired(); |
| 146 | |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 147 | uint32_t bytes_ = 0; |
| 148 | uint32_t increments_ = 0; |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 149 | base::RepeatingTimer timer_; |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 150 | }; |
| 151 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 152 | UDPSocketPosix(DatagramSocket::BindType bind_type, |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 153 | net::NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 154 | const net::NetLogSource& source); |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 155 | |
| 156 | UDPSocketPosix(const UDPSocketPosix&) = delete; |
| 157 | UDPSocketPosix& operator=(const UDPSocketPosix&) = delete; |
| 158 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 159 | virtual ~UDPSocketPosix(); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 160 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 161 | // Opens the socket. |
| 162 | // Returns a net error code. |
| 163 | int Open(AddressFamily address_family); |
| 164 | |
pauljensen | be5bc323 | 2015-10-05 20:39:27 | [diff] [blame] | 165 | // Binds this socket to |network|. All data traffic on the socket will be sent |
| 166 | // and received via |network|. Must be called before Connect(). This call will |
| 167 | // fail if |network| has disconnected. Communication using this socket will |
| 168 | // fail if |network| disconnects. |
| 169 | // Returns a net error code. |
| 170 | int BindToNetwork(NetworkChangeNotifier::NetworkHandle network); |
| 171 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 172 | // Connects the socket to connect with a certain |address|. |
| 173 | // Should be called after Open(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 174 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 175 | int Connect(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 176 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 177 | // Binds the address/port for this socket to |address|. This is generally |
| 178 | // only used on a server. Should be called after Open(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 179 | // Returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 180 | int Bind(const IPEndPoint& address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 181 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 182 | // Closes the socket. |
| 183 | // TODO(rvargas, hidehiko): Disallow re-Open() after Close(). |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 184 | void Close(); |
| 185 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 186 | // Copies the remote udp address into |address| and returns a net error code. |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 187 | int GetPeerAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 188 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 189 | // Copies the local udp address into |address| and returns a net error code. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 190 | // (similar to getsockname) |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 191 | int GetLocalAddress(IPEndPoint* address) const; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 192 | |
| 193 | // IO: |
| 194 | // Multiple outstanding read requests are not supported. |
| 195 | // Full duplex mode (reading and writing at the same time) is supported |
| 196 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 197 | // Reads from the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 198 | // Only usable from the client-side of a UDP socket, after the socket |
| 199 | // has been connected. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 200 | int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 201 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 202 | // Writes to the socket. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 203 | // Only usable from the client-side of a UDP socket, after the socket |
| 204 | // has been connected. |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 205 | int Write(IOBuffer* buf, |
| 206 | int buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 207 | CompletionOnceCallback callback, |
[email protected] | cda4b7b | 2017-12-20 06:04:43 | [diff] [blame] | 208 | const NetworkTrafficAnnotationTag& traffic_annotation); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 209 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 210 | // Refer to datagram_client_socket.h |
| 211 | int WriteAsync(DatagramBuffers buffers, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 212 | CompletionOnceCallback callback, |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 213 | const NetworkTrafficAnnotationTag& traffic_annotation); |
| 214 | int WriteAsync(const char* buffer, |
| 215 | size_t buf_len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 216 | CompletionOnceCallback callback, |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 217 | const NetworkTrafficAnnotationTag& traffic_annotation); |
| 218 | |
| 219 | DatagramBuffers GetUnwrittenBuffers(); |
| 220 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 221 | // Reads from a socket and receive sender address information. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 222 | // |buf| is the buffer to read data into. |
| 223 | // |buf_len| is the maximum amount of data to read. |
| 224 | // |address| is a buffer provided by the caller for receiving the sender |
| 225 | // address information about the received data. This buffer must be kept |
| 226 | // alive by the caller until the callback is placed. |
[email protected] | 73c5b69 | 2014-07-01 12:43:41 | [diff] [blame] | 227 | // |callback| is the callback on completion of the RecvFrom. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 228 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
Matthew Denton | 111df5ad | 2018-10-19 20:39:25 | [diff] [blame] | 229 | // If ERR_IO_PENDING is returned, this socket takes a ref to |buf| to keep |
| 230 | // it alive until the data is received. However, the caller must keep |
| 231 | // |address| alive until the callback is called. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 232 | int RecvFrom(IOBuffer* buf, |
| 233 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 234 | IPEndPoint* address, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 235 | CompletionOnceCallback callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 236 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 237 | // Sends to a socket with a particular destination. |
tfarina | 39524dd | 2016-02-19 10:52:10 | [diff] [blame] | 238 | // |buf| is the buffer to send. |
| 239 | // |buf_len| is the number of bytes to send. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 240 | // |address| is the recipient address. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 241 | // |callback| is the user callback function to call on complete. |
| 242 | // Returns a net error code, or ERR_IO_PENDING if the IO is in progress. |
Matthew Denton | 111df5ad | 2018-10-19 20:39:25 | [diff] [blame] | 243 | // If ERR_IO_PENDING is returned, this socket copies |address| for |
| 244 | // asynchronous sending, and takes a ref to |buf| to keep it alive until the |
| 245 | // data is sent. |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 246 | int SendTo(IOBuffer* buf, |
| 247 | int buf_len, |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 248 | const IPEndPoint& address, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 249 | CompletionOnceCallback callback); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 250 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 251 | // Sets the receive buffer size (in bytes) for the socket. |
| 252 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 253 | int SetReceiveBufferSize(int32_t size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 254 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 255 | // Sets the send buffer size (in bytes) for the socket. |
| 256 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 257 | int SetSendBufferSize(int32_t size); |
[email protected] | df31da4 | 2011-10-18 01:44:40 | [diff] [blame] | 258 | |
rch | ff006a1 | 2016-08-24 23:56:31 | [diff] [blame] | 259 | // Requests that packets sent by this socket not be fragment, either locally |
| 260 | // by the host, or by routers (via the DF bit in the IPv4 packet header). |
Momoka Yamamoto | 3f916b8 | 2022-02-10 18:26:52 | [diff] [blame] | 261 | // May not be supported by all platforms. Returns a network error code if |
| 262 | // there was a problem, but the socket will still be usable. Can not |
rch | ff006a1 | 2016-08-24 23:56:31 | [diff] [blame] | 263 | // return ERR_IO_PENDING. |
| 264 | int SetDoNotFragment(); |
| 265 | |
Yixin Wang | 3ae76ca | 2018-03-15 17:18:52 | [diff] [blame] | 266 | // If |confirm| is true, then the MSG_CONFIRM flag will be passed to |
| 267 | // subsequent writes if it's supported by the platform. |
| 268 | void SetMsgConfirm(bool confirm); |
| 269 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 270 | // Returns true if the socket is already connected or bound. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 271 | bool is_connected() const { return is_connected_; } |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 272 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 273 | const NetLogWithSource& NetLog() const { return net_log_; } |
[email protected] | eaf10dc | 2011-07-18 21:47:35 | [diff] [blame] | 274 | |
tfarina | 6f2f3beb | 2017-04-19 11:53:14 | [diff] [blame] | 275 | // Call this to enable SO_REUSEADDR on the underlying socket. |
| 276 | // Should be called between Open() and Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 277 | // Returns a net error code. |
| 278 | int AllowAddressReuse(); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 279 | |
tfarina | 6f2f3beb | 2017-04-19 11:53:14 | [diff] [blame] | 280 | // Call this to allow or disallow sending and receiving packets to and from |
| 281 | // broadcast addresses. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 282 | // Returns a net error code. |
| 283 | int SetBroadcast(bool broadcast); |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 284 | |
Eric Orth | 7141dbc | 2018-10-26 20:10:24 | [diff] [blame] | 285 | // Sets socket options to allow the socket to share the local address to which |
| 286 | // the socket will be bound with other processes and attempt to allow all such |
| 287 | // sockets to receive the same multicast messages. Returns a net error code. |
| 288 | // |
| 289 | // Ability and requirements for different sockets to receive the same messages |
| 290 | // varies between POSIX platforms. For best results in allowing the messages |
| 291 | // to be shared, all sockets sharing the same address should join the same |
| 292 | // multicast group and interface. Also, the socket should listen to the |
| 293 | // specific multicast address rather than a wildcard address (e.g. 0.0.0.0). |
| 294 | // |
| 295 | // Should be called between Open() and Bind(). |
| 296 | int AllowAddressSharingForMulticast(); |
| 297 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 298 | // Joins the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 299 | // |group_address| is the group address to join, could be either |
| 300 | // an IPv4 or IPv6 address. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 301 | // Returns a net error code. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 302 | int JoinGroup(const IPAddress& group_address) const; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 303 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 304 | // Leaves the multicast group. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 305 | // |group_address| is the group address to leave, could be either |
| 306 | // an IPv4 or IPv6 address. If the socket hasn't joined the group, |
| 307 | // it will be ignored. |
| 308 | // It's optional to leave the multicast group before destroying |
| 309 | // the socket. It will be done by the OS. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 310 | // Returns a net error code. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 311 | int LeaveGroup(const IPAddress& group_address) const; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 312 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 313 | // Sets interface to use for multicast. If |interface_index| set to 0, |
| 314 | // default interface is used. |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 315 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 316 | // Returns a net error code. |
tfarina | c40df1c | 2015-11-30 22:31:47 | [diff] [blame] | 317 | int SetMulticastInterface(uint32_t interface_index); |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 318 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 319 | // Sets the time-to-live option for UDP packets sent to the multicast |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 320 | // group address. The default value of this option is 1. |
| 321 | // Cannot be negative or more than 255. |
| 322 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 323 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 324 | int SetMulticastTimeToLive(int time_to_live); |
| 325 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 326 | // Sets the loopback flag for UDP socket. If this flag is true, the host |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 327 | // will receive packets sent to the joined group from itself. |
| 328 | // The default value of this option is true. |
| 329 | // Should be called before Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 330 | // Returns a net error code. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 331 | // |
| 332 | // Note: the behavior of |SetMulticastLoopbackMode| is slightly |
| 333 | // different between Windows and Unix-like systems. The inconsistency only |
| 334 | // happens when there are more than one applications on the same host |
| 335 | // joined to the same multicast group while having different settings on |
| 336 | // multicast loopback mode. On Windows, the applications with loopback off |
| 337 | // will not RECEIVE the loopback packets; while on Unix-like systems, the |
| 338 | // applications with loopback off will not SEND the loopback packets to |
| 339 | // other applications on the same host. See MSDN: http://goo.gl/6vqbj |
| 340 | int SetMulticastLoopbackMode(bool loopback); |
| 341 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 342 | // Sets the differentiated services flags on outgoing packets. May not |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 343 | // do anything on some platforms. |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 344 | // Returns a net error code. |
[email protected] | a8a442b3 | 2013-10-22 00:34:41 | [diff] [blame] | 345 | int SetDiffServCodePoint(DiffServCodePoint dscp); |
| 346 | |
Stefano Duo | 157e10c2 | 2022-01-25 12:58:34 | [diff] [blame] | 347 | // Exposes the underlying socket descriptor for testing its state. Does not |
| 348 | // release ownership of the descriptor. |
| 349 | SocketDescriptor SocketDescriptorForTesting() const { return socket_; } |
| 350 | |
[email protected] | 1901dbcb | 2014-03-10 07:18:37 | [diff] [blame] | 351 | // Resets the thread to be used for thread-safety checks. |
| 352 | void DetachFromThread(); |
| 353 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 354 | // Apply |tag| to this socket. |
| 355 | void ApplySocketTag(const SocketTag& tag); |
| 356 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 357 | void SetWriteAsyncEnabled(bool enabled) { write_async_enabled_ = enabled; } |
| 358 | bool WriteAsyncEnabled() { return write_async_enabled_; } |
| 359 | |
| 360 | void SetMaxPacketSize(size_t max_packet_size); |
| 361 | |
| 362 | void SetWriteMultiCoreEnabled(bool enabled) { |
| 363 | write_multi_core_enabled_ = enabled; |
| 364 | } |
| 365 | |
| 366 | void SetSendmmsgEnabled(bool enabled) { |
| 367 | DCHECK(sender_ != nullptr); |
| 368 | sender_->SetSendmmsgEnabled(enabled); |
| 369 | } |
| 370 | |
| 371 | void SetWriteBatchingActive(bool active) { write_batching_active_ = active; } |
| 372 | |
| 373 | void SetWriteAsyncMaxBuffers(int value) { |
| 374 | LOG(INFO) << "SetWriteAsyncMaxBuffers: " << value; |
| 375 | write_async_max_buffers_ = value; |
| 376 | } |
| 377 | |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 378 | // Enables experimental optimization. This method should be called |
| 379 | // before the socket is used to read data for the first time. |
| 380 | void enable_experimental_recv_optimization() { |
| 381 | DCHECK_EQ(kInvalidSocket, socket_); |
| 382 | experimental_recv_optimization_enabled_ = true; |
Eric Orth | 7141dbc | 2018-10-26 20:10:24 | [diff] [blame] | 383 | } |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 384 | |
Yu Su | ca37f3c | 2020-10-09 20:02:23 | [diff] [blame] | 385 | // Sets iOS Network Service Type for option SO_NET_SERVICE_TYPE. |
| 386 | int SetIOSNetworkServiceType(int ios_network_service_type); |
| 387 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 388 | protected: |
| 389 | // WriteAsync batching etc. are to improve throughput of large high |
| 390 | // bandwidth uploads. |
| 391 | |
| 392 | // Watcher for WriteAsync paths. |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 393 | class WriteAsyncWatcher : public base::MessagePumpForIO::FdWatcher { |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 394 | public: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 395 | explicit WriteAsyncWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 396 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 397 | WriteAsyncWatcher(const WriteAsyncWatcher&) = delete; |
| 398 | WriteAsyncWatcher& operator=(const WriteAsyncWatcher&) = delete; |
| 399 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 400 | // MessagePumpForIO::FdWatcher methods |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 401 | |
| 402 | void OnFileCanReadWithoutBlocking(int /* fd */) override {} |
| 403 | |
| 404 | void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
| 405 | |
| 406 | void set_watching(bool watching) { watching_ = watching; } |
| 407 | |
| 408 | bool watching() { return watching_; } |
| 409 | |
| 410 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 411 | const raw_ptr<UDPSocketPosix> socket_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 412 | bool watching_ = false; |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | void IncreaseWriteAsyncOutstanding(int increment) { |
| 416 | write_async_outstanding_ += increment; |
| 417 | } |
| 418 | |
| 419 | virtual bool InternalWatchFileDescriptor(); |
| 420 | virtual void InternalStopWatchingFileDescriptor(); |
| 421 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 422 | void SetWriteCallback(CompletionOnceCallback callback) { |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 423 | write_callback_ = std::move(callback); |
| 424 | } |
| 425 | |
| 426 | void DidSendBuffers(SendResult buffers); |
| 427 | void FlushPending(); |
| 428 | |
| 429 | std::unique_ptr<WriteAsyncWatcher> write_async_watcher_; |
| 430 | scoped_refptr<UDPSocketPosixSender> sender_; |
| 431 | std::unique_ptr<DatagramBufferPool> datagram_buffer_pool_; |
| 432 | // |WriteAsync| pending writes, does not include buffers that have |
| 433 | // been |PostTask*|'d. |
| 434 | DatagramBuffers pending_writes_; |
| 435 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 436 | private: |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 437 | enum SocketOptions { |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 438 | SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 439 | }; |
| 440 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 441 | class ReadWatcher : public base::MessagePumpForIO::FdWatcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 442 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 443 | explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 444 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 445 | ReadWatcher(const ReadWatcher&) = delete; |
| 446 | ReadWatcher& operator=(const ReadWatcher&) = delete; |
| 447 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 448 | // MessagePumpForIO::FdWatcher methods |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 449 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 450 | void OnFileCanReadWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 451 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 452 | void OnFileCanWriteWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 453 | |
| 454 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 455 | const raw_ptr<UDPSocketPosix> socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 456 | }; |
| 457 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 458 | class WriteWatcher : public base::MessagePumpForIO::FdWatcher { |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 459 | public: |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 460 | explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 461 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 462 | WriteWatcher(const WriteWatcher&) = delete; |
| 463 | WriteWatcher& operator=(const WriteWatcher&) = delete; |
| 464 | |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 465 | // MessagePumpForIO::FdWatcher methods |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 466 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 467 | void OnFileCanReadWithoutBlocking(int /* fd */) override {} |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 468 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 469 | void OnFileCanWriteWithoutBlocking(int /* fd */) override; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 470 | |
| 471 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 472 | const raw_ptr<UDPSocketPosix> socket_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 473 | }; |
| 474 | |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 475 | int InternalWriteAsync(CompletionOnceCallback callback, |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 476 | const NetworkTrafficAnnotationTag& traffic_annotation); |
| 477 | bool WatchFileDescriptor(); |
| 478 | void StopWatchingFileDescriptor(); |
| 479 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 480 | void DoReadCallback(int rv); |
| 481 | void DoWriteCallback(int rv); |
| 482 | void DidCompleteRead(); |
| 483 | void DidCompleteWrite(); |
| 484 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 485 | // Handles stats and logging. |result| is the number of bytes transferred, on |
| 486 | // success, or the net error code on failure. On success, LogRead takes in a |
| 487 | // sockaddr and its length, which are mandatory, while LogWrite takes in an |
| 488 | // optional IPEndPoint. |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 489 | void LogRead(int result, |
| 490 | const char* bytes, |
| 491 | socklen_t addr_len, |
| 492 | const sockaddr* addr); |
| 493 | void LogWrite(int result, const char* bytes, const IPEndPoint* address); |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 494 | |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 495 | // Same as SendTo(), except that address is passed by pointer |
| 496 | // instead of by reference. It is called from Write() with |address| |
Bartek Nowierski | 3253b67 | 2020-06-01 20:37:12 | [diff] [blame] | 497 | // set to nullptr. |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 498 | int SendToOrWrite(IOBuffer* buf, |
| 499 | int buf_len, |
| 500 | const IPEndPoint* address, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 501 | CompletionOnceCallback callback); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 502 | |
[email protected] | 8866f62 | 2011-10-18 20:08:10 | [diff] [blame] | 503 | int InternalConnect(const IPEndPoint& address); |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 504 | |
| 505 | // Reads data from a UDP socket. Depending whether the socket is connected or |
| 506 | // not, the method delegates the call to InternalRecvFromConnectedSocket() |
| 507 | // or InternalRecvFromNonConnectedSocket() respectively. |
| 508 | // For proper detection of truncated reads, the |buf_len| should always be |
| 509 | // one byte longer than the expected maximum packet length. |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 510 | int InternalRecvFrom(IOBuffer* buf, int buf_len, IPEndPoint* address); |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 511 | |
| 512 | // A more efficient implementation of the InternalRecvFrom() method for |
| 513 | // reading data from connected sockets. Internally the method uses the read() |
| 514 | // system call. |
| 515 | int InternalRecvFromConnectedSocket(IOBuffer* buf, |
| 516 | int buf_len, |
| 517 | IPEndPoint* address); |
| 518 | |
| 519 | // An implementation of the InternalRecvFrom() method for reading data |
| 520 | // from non-connected sockets. Internally the method uses the recvmsg() |
| 521 | // system call. |
| 522 | int InternalRecvFromNonConnectedSocket(IOBuffer* buf, |
| 523 | int buf_len, |
| 524 | IPEndPoint* address); |
[email protected] | 06043ae | 2011-03-16 00:00:21 | [diff] [blame] | 525 | int InternalSendTo(IOBuffer* buf, int buf_len, const IPEndPoint* address); |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 526 | |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 527 | // Applies |socket_options_| to |socket_|. Should be called before |
| 528 | // Bind(). |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 529 | int SetMulticastOptions(); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 530 | int DoBind(const IPEndPoint& address); |
[email protected] | acfe8e8 | 2013-12-12 14:12:56 | [diff] [blame] | 531 | // Binds to a random port on |address|. |
martijn | d8dafab | 2016-03-14 18:28:49 | [diff] [blame] | 532 | int RandomBind(const IPAddress& address); |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 533 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 534 | // Helpers for |WriteAsync| |
| 535 | base::SequencedTaskRunner* GetTaskRunner(); |
| 536 | void OnWriteAsyncTimerFired(); |
| 537 | void LocalSendBuffers(); |
| 538 | void PostSendBuffers(); |
| 539 | int ResetLastAsyncResult(); |
| 540 | int ResetWrittenBytes(); |
| 541 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 542 | int socket_; |
sergeyu | 30ad5b3 | 2015-03-27 20:31:02 | [diff] [blame] | 543 | |
Zhongyi Shi | 351b544 | 2018-11-27 16:24:17 | [diff] [blame] | 544 | // Hash of |socket_| to verify that it is not corrupted when calling close(). |
| 545 | // Used to debug https://crbug.com/906005. |
| 546 | // TODO(crbug.com/906005): Remove this once the bug is fixed. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 547 | int socket_hash_ = 0; |
Zhongyi Shi | 351b544 | 2018-11-27 16:24:17 | [diff] [blame] | 548 | |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 549 | int addr_family_ = 0; |
| 550 | bool is_connected_ = false; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 551 | |
[email protected] | 7be809df | 2012-08-07 17:00:48 | [diff] [blame] | 552 | // Bitwise-or'd combination of SocketOptions. Specifies the set of |
| 553 | // options that should be applied to |socket_| before Bind(). |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 554 | int socket_options_ = SOCKET_OPTION_MULTICAST_LOOP; |
[email protected] | a1781d7b | 2012-07-16 11:52:34 | [diff] [blame] | 555 | |
Yixin Wang | 3ae76ca | 2018-03-15 17:18:52 | [diff] [blame] | 556 | // Flags passed to sendto(). |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 557 | int sendto_flags_ = 0; |
Yixin Wang | 3ae76ca | 2018-03-15 17:18:52 | [diff] [blame] | 558 | |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 559 | // Multicast interface. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 560 | uint32_t multicast_interface_ = 0; |
[email protected] | 7b29dac | 2013-12-05 11:19:42 | [diff] [blame] | 561 | |
hidehiko | 17fac55 | 2014-12-08 06:02:17 | [diff] [blame] | 562 | // Multicast socket options cached for SetMulticastOption. |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 563 | // Cannot be used after Bind(). |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 564 | int multicast_time_to_live_ = 1; |
[email protected] | 5f01ce2 | 2013-04-30 00:53:18 | [diff] [blame] | 565 | |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 566 | // How to do source port binding, used only when UDPSocket is part of |
| 567 | // UDPClientSocket, since UDPServerSocket provides Bind. |
| 568 | DatagramSocket::BindType bind_type_; |
| 569 | |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 570 | // These are mutable since they're just cached copies to make |
| 571 | // GetPeerAddress/GetLocalAddress smarter. |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 572 | mutable std::unique_ptr<IPEndPoint> local_address_; |
| 573 | mutable std::unique_ptr<IPEndPoint> remote_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 574 | |
tfarina | 4eb7aad8 | 2015-09-14 17:10:34 | [diff] [blame] | 575 | // The socket's posix wrappers |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame] | 576 | base::MessagePumpForIO::FdWatchController read_socket_watcher_; |
| 577 | base::MessagePumpForIO::FdWatchController write_socket_watcher_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 578 | |
| 579 | // The corresponding watchers for reads and writes. |
| 580 | ReadWatcher read_watcher_; |
| 581 | WriteWatcher write_watcher_; |
| 582 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 583 | // Various bits to support |WriteAsync()|. |
| 584 | bool write_async_enabled_ = false; |
| 585 | bool write_batching_active_ = false; |
| 586 | bool write_multi_core_enabled_ = false; |
| 587 | int write_async_max_buffers_ = 16; |
| 588 | int written_bytes_ = 0; |
| 589 | |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 590 | int last_async_result_ = 0; |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 591 | base::RepeatingTimer write_async_timer_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 592 | bool write_async_timer_running_ = false; |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 593 | // Total writes in flight, including those |PostTask*|'d. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 594 | int write_async_outstanding_ = 0; |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 595 | |
| 596 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 597 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 598 | // The buffer used by InternalRead() to retry Read requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 599 | scoped_refptr<IOBuffer> read_buf_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 600 | int read_buf_len_ = 0; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 601 | raw_ptr<IPEndPoint> recv_from_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 602 | |
[email protected] | 43d4a026 | 2011-03-09 19:26:04 | [diff] [blame] | 603 | // The buffer used by InternalWrite() to retry Write requests |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 604 | scoped_refptr<IOBuffer> write_buf_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 605 | int write_buf_len_ = 0; |
danakj | a9850e1 | 2016-04-18 22:28:08 | [diff] [blame] | 606 | std::unique_ptr<IPEndPoint> send_to_address_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 607 | |
| 608 | // External callback; called when read is complete. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 609 | CompletionOnceCallback read_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 610 | |
| 611 | // External callback; called when write is complete. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 612 | CompletionOnceCallback write_callback_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 613 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 614 | NetLogWithSource net_log_; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 615 | |
pauljensen | f0d9535a | 2016-06-10 19:51:19 | [diff] [blame] | 616 | // Network that this socket is bound to via BindToNetwork(). |
| 617 | NetworkChangeNotifier::NetworkHandle bound_network_; |
| 618 | |
Francois Doray | 32ad367 | 2021-04-15 15:37:39 | [diff] [blame] | 619 | // Whether net::activity_monitor should be updated every time bytes are |
| 620 | // received, without batching through |received_activity_monitor_|. This is |
| 621 | // initialized with the state of the "UdpSocketPosixAlwaysUpdateBytesReceived" |
| 622 | // feature. It is cached to avoid accessing the FeatureList every time bytes |
| 623 | // are received. |
| 624 | const bool always_update_bytes_received_; |
| 625 | |
Francois Doray | 5146d2f0 | 2021-03-30 18:11:57 | [diff] [blame] | 626 | // Used to lower the overhead updating activity monitor. |
Charles 'Buck' Krasic | e70ff45 | 2017-08-14 22:11:44 | [diff] [blame] | 627 | ReceivedActivityMonitor received_activity_monitor_; |
| 628 | |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 629 | // Current socket tag if |socket_| is valid, otherwise the tag to apply when |
| 630 | // |socket_| is opened. |
| 631 | SocketTag tag_; |
| 632 | |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 633 | // If set to true, the socket will use an optimized experimental code path. |
| 634 | // By default, the value is set to false. To use the optimization, the |
| 635 | // client of the socket has to opt-in by calling the |
| 636 | // enable_experimental_recv_optimization() method. |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame^] | 637 | bool experimental_recv_optimization_enabled_ = false; |
kapishnikov | 7f8dd1e | 2018-01-24 06:10:49 | [diff] [blame] | 638 | |
Eric Roman | 5a84192 | 2020-08-13 01:28:25 | [diff] [blame] | 639 | // Manages decrementing the global open UDP socket counter when this |
| 640 | // UDPSocket is destroyed. |
| 641 | OwnedUDPSocketCount owned_socket_count_; |
| 642 | |
gab | d43afa1 | 2017-05-30 18:26:24 | [diff] [blame] | 643 | THREAD_CHECKER(thread_checker_); |
| 644 | |
Charles 'Buck' Krasic | 762317f | 2018-03-30 20:08:09 | [diff] [blame] | 645 | // Used for alternate writes that are posted for concurrent execution. |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 646 | base::WeakPtrFactory<UDPSocketPosix> weak_factory_{this}; |
[email protected] | a2798d9 | 2011-03-02 22:56:18 | [diff] [blame] | 647 | }; |
| 648 | |
| 649 | } // namespace net |
| 650 | |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 651 | #endif // NET_SOCKET_UDP_SOCKET_POSIX_H_ |