blob: 4512945f1ca159c002b690c5cf2e0c69e9ba276c [file] [log] [blame]
[email protected]21160f02013-09-01 23:04:271// Copyright 2013 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
tfarina4eb7aad82015-09-14 17:10:345#ifndef NET_SOCKET_TCP_SOCKET_POSIX_H_
6#define NET_SOCKET_TCP_SOCKET_POSIX_H_
[email protected]21160f02013-09-01 23:04:277
Avi Drissman13fc8932015-12-20 04:40:468#include <stdint.h>
9
danakj655b66c2016-04-16 00:51:3810#include <memory>
11
[email protected]659fd67c2013-09-18 21:50:2612#include "base/callback.h"
[email protected]21160f02013-09-01 23:04:2713#include "base/compiler_specific.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
[email protected]21160f02013-09-01 23:04:2715#include "net/base/address_family.h"
Brad Lassey3a814172018-04-26 03:30:2116#include "net/base/completion_once_callback.h"
[email protected]21160f02013-09-01 23:04:2717#include "net/base/net_export.h"
mikecironef22f9812016-10-04 03:40:1918#include "net/log/net_log_with_source.h"
rvera26f0a1392017-05-02 22:25:4419#include "net/socket/socket_descriptor.h"
tbansalca83c002016-04-28 20:56:2820#include "net/socket/socket_performance_watcher.h"
Paul Jensen0f49dec2017-12-12 23:39:5821#include "net/socket/socket_tag.h"
[email protected]a2b2cfc2017-12-06 09:06:0822#include "net/traffic_annotation/network_traffic_annotation.h"
[email protected]21160f02013-09-01 23:04:2723
tbansal7b403bcc2016-04-13 22:33:2124namespace base {
tbansal180587c2017-02-16 15:13:2325class TimeDelta;
tbansal7b403bcc2016-04-13 22:33:2126}
27
[email protected]21160f02013-09-01 23:04:2728namespace net {
29
[email protected]659fd67c2013-09-18 21:50:2630class AddressList;
31class IOBuffer;
[email protected]21160f02013-09-01 23:04:2732class IPEndPoint;
tfarina4eb7aad82015-09-14 17:10:3433class SocketPosix;
mikecironef22f9812016-10-04 03:40:1934class NetLog;
35struct NetLogSource;
Paul Jensen0f49dec2017-12-12 23:39:5836class SocketTag;
[email protected]21160f02013-09-01 23:04:2737
tfarina4eb7aad82015-09-14 17:10:3438class NET_EXPORT TCPSocketPosix {
[email protected]21160f02013-09-01 23:04:2739 public:
tbansal7b403bcc2016-04-13 22:33:2140 // |socket_performance_watcher| is notified of the performance metrics related
41 // to this socket. |socket_performance_watcher| may be null.
42 TCPSocketPosix(
danakj655b66c2016-04-16 00:51:3843 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
tbansal7b403bcc2016-04-13 22:33:2144 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:1945 const NetLogSource& source);
tfarina4eb7aad82015-09-14 17:10:3446 virtual ~TCPSocketPosix();
[email protected]21160f02013-09-01 23:04:2747
tfarinafdec3052017-04-25 13:36:2548 // Opens the socket.
49 // Returns a net error code.
[email protected]c9080d82013-09-15 15:14:1650 int Open(AddressFamily family);
tfarinaa0922c82017-04-15 11:54:4951
rvera26f0a1392017-05-02 22:25:4452 // Takes ownership of |socket|, which is known to already be connected to the
53 // given peer address. However, peer address may be the empty address, for
54 // compatibility. The given peer address will be returned by GetPeerAddress.
55 int AdoptConnectedSocket(SocketDescriptor socket,
56 const IPEndPoint& peer_address);
57 // Takes ownership of |socket|, which may or may not be open, bound, or
58 // listening. The caller must determine the state of the socket based on its
59 // provenance and act accordingly. The socket may have connections waiting
60 // to be accepted, but must not be actually connected.
61 int AdoptUnconnectedSocket(SocketDescriptor socket);
[email protected]659fd67c2013-09-18 21:50:2662
tfarinafdec3052017-04-25 13:36:2563 // Binds this socket to |address|. This is generally only used on a server.
64 // Should be called after Open(). Returns a net error code.
[email protected]21160f02013-09-01 23:04:2765 int Bind(const IPEndPoint& address);
[email protected]659fd67c2013-09-18 21:50:2666
tfarinafdec3052017-04-25 13:36:2567 // Put this socket on listen state with the given |backlog|.
68 // Returns a net error code.
[email protected]21160f02013-09-01 23:04:2769 int Listen(int backlog);
tfarinafdec3052017-04-25 13:36:2570
71 // Accepts incoming connection.
72 // Returns a net error code.
danakj655b66c2016-04-16 00:51:3873 int Accept(std::unique_ptr<TCPSocketPosix>* socket,
[email protected]21160f02013-09-01 23:04:2774 IPEndPoint* address,
Brad Lassey3a814172018-04-26 03:30:2175 CompletionOnceCallback callback);
[email protected]659fd67c2013-09-18 21:50:2676
tfarinafdec3052017-04-25 13:36:2577 // Connects this socket to the given |address|.
78 // Should be called after Open().
79 // Returns a net error code.
Brad Lassey3a814172018-04-26 03:30:2180 int Connect(const IPEndPoint& address, CompletionOnceCallback callback);
[email protected]659fd67c2013-09-18 21:50:2681 bool IsConnected() const;
82 bool IsConnectedAndIdle() const;
83
tfarinafdec3052017-04-25 13:36:2584 // IO:
[email protected]659fd67c2013-09-18 21:50:2685 // Multiple outstanding requests are not supported.
86 // Full duplex mode (reading and writing at the same time) is supported.
tfarinafdec3052017-04-25 13:36:2587
88 // Reads from the socket.
89 // Returns a net error code.
Brad Lassey3a814172018-04-26 03:30:2190 int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
91 int ReadIfReady(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
Helen Lia6d3b2c2018-05-08 16:09:0792 int CancelReadIfReady();
tfarinafdec3052017-04-25 13:36:2593
94 // Writes to the socket.
95 // Returns a net error code.
[email protected]a2b2cfc2017-12-06 09:06:0896 int Write(IOBuffer* buf,
97 int buf_len,
Brad Lassey3a814172018-04-26 03:30:2198 CompletionOnceCallback callback,
[email protected]690bbea2017-12-08 09:05:4699 const NetworkTrafficAnnotationTag& traffic_annotation);
[email protected]659fd67c2013-09-18 21:50:26100
tfarinafdec3052017-04-25 13:36:25101 // Copies the local tcp address into |address| and returns a net error code.
[email protected]659fd67c2013-09-18 21:50:26102 int GetLocalAddress(IPEndPoint* address) const;
tfarinafdec3052017-04-25 13:36:25103
104 // Copies the remote tcp code into |address| and returns a net error code.
[email protected]659fd67c2013-09-18 21:50:26105 int GetPeerAddress(IPEndPoint* address) const;
106
107 // Sets various socket options.
108 // The commonly used options for server listening sockets:
tfarinaa0922c82017-04-15 11:54:49109 // - AllowAddressReuse().
[email protected]21160f02013-09-01 23:04:27110 int SetDefaultOptionsForServer();
[email protected]659fd67c2013-09-18 21:50:26111 // The commonly used options for client sockets and accepted sockets:
112 // - SetNoDelay(true);
113 // - SetKeepAlive(true, 45).
114 void SetDefaultOptionsForClient();
tfarinaa0922c82017-04-15 11:54:49115 int AllowAddressReuse();
Avi Drissman13fc8932015-12-20 04:40:46116 int SetReceiveBufferSize(int32_t size);
117 int SetSendBufferSize(int32_t size);
[email protected]659fd67c2013-09-18 21:50:26118 bool SetKeepAlive(bool enable, int delay);
119 bool SetNoDelay(bool no_delay);
120
bmcquade8b62f472015-07-08 16:03:54121 // Gets the estimated RTT. Returns false if the RTT is
122 // unavailable. May also return false when estimated RTT is 0.
123 bool GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const
124 WARN_UNUSED_RESULT;
125
tfarinafdec3052017-04-25 13:36:25126 // Closes the socket.
[email protected]21160f02013-09-01 23:04:27127 void Close();
128
jridcb4ae922014-09-12 23:52:39129 void EnableTCPFastOpenIfSupported();
130
[email protected]2ef2b0e2014-07-09 21:12:34131 bool IsValid() const;
[email protected]659fd67c2013-09-18 21:50:26132
svaldez58804c402015-10-06 00:13:47133 // Detachs from the current thread, to allow the socket to be transferred to
134 // a new thread. Should only be called when the object is no longer used by
135 // the old thread.
136 void DetachFromThread();
137
[email protected]659fd67c2013-09-18 21:50:26138 // Marks the start/end of a series of connect attempts for logging purpose.
139 //
140 // TCPClientSocket may attempt to connect to multiple addresses until it
141 // succeeds in establishing a connection. The corresponding log will have
mikecirone8b85c432016-09-08 19:11:00142 // multiple NetLogEventType::TCP_CONNECT_ATTEMPT entries nested within a
143 // NetLogEventType::TCP_CONNECT. These methods set the start/end of
144 // NetLogEventType::TCP_CONNECT.
[email protected]659fd67c2013-09-18 21:50:26145 //
146 // TODO(yzshen): Change logging format and let TCPClientSocket log the
147 // start/end of a series of connect attempts itself.
148 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
149 void EndLoggingMultipleConnectAttempts(int net_error);
150
tfarina42834112016-09-22 13:38:20151 const NetLogWithSource& net_log() const { return net_log_; }
[email protected]21160f02013-09-01 23:04:27152
rvera26f0a1392017-05-02 22:25:44153 // Return the underlying SocketDescriptor and clean up this object, which may
154 // no longer be used. This method should be used only for testing. No read,
155 // write, or accept operations should be pending.
156 SocketDescriptor ReleaseSocketDescriptorForTesting();
157
Paul Jensen0f49dec2017-12-12 23:39:58158 // Apply |tag| to this socket.
159 void ApplySocketTag(const SocketTag& tag);
160
[email protected]21160f02013-09-01 23:04:27161 private:
jri23fdae22014-09-16 23:04:09162 // States that using a socket with TCP FastOpen can lead to.
jri764425442014-10-02 01:53:41163 enum TCPFastOpenStatus {
164 TCP_FASTOPEN_STATUS_UNKNOWN,
[email protected]659fd67c2013-09-18 21:50:26165
jri764425442014-10-02 01:53:41166 // The initial FastOpen connect attempted returned synchronously,
[email protected]659fd67c2013-09-18 21:50:26167 // indicating that we had and sent a cookie along with the initial data.
jri764425442014-10-02 01:53:41168 TCP_FASTOPEN_FAST_CONNECT_RETURN,
[email protected]659fd67c2013-09-18 21:50:26169
jri764425442014-10-02 01:53:41170 // The initial FastOpen connect attempted returned asynchronously,
[email protected]659fd67c2013-09-18 21:50:26171 // indicating that we did not have a cookie for the server.
jri764425442014-10-02 01:53:41172 TCP_FASTOPEN_SLOW_CONNECT_RETURN,
[email protected]659fd67c2013-09-18 21:50:26173
174 // Some other error occurred on connection, so we couldn't tell if
jri764425442014-10-02 01:53:41175 // FastOpen would have worked.
176 TCP_FASTOPEN_ERROR,
[email protected]659fd67c2013-09-18 21:50:26177
jri764425442014-10-02 01:53:41178 // An attempt to do a FastOpen succeeded immediately
179 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
[email protected]659fd67c2013-09-18 21:50:26180 // had acked the data we sent.
jri764425442014-10-02 01:53:41181 TCP_FASTOPEN_SYN_DATA_ACK,
[email protected]659fd67c2013-09-18 21:50:26182
jri764425442014-10-02 01:53:41183 // An attempt to do a FastOpen succeeded immediately
184 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
[email protected]659fd67c2013-09-18 21:50:26185 // had nacked the data we sent.
jri764425442014-10-02 01:53:41186 TCP_FASTOPEN_SYN_DATA_NACK,
[email protected]659fd67c2013-09-18 21:50:26187
jri764425442014-10-02 01:53:41188 // An attempt to do a FastOpen succeeded immediately
189 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and our probe to determine if the
190 // socket was using FastOpen failed.
191 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED,
[email protected]659fd67c2013-09-18 21:50:26192
jri764425442014-10-02 01:53:41193 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26194 // and we later confirmed that the server had acked initial data. This
195 // should never happen (we didn't send data, so it shouldn't have
196 // been acked).
jri764425442014-10-02 01:53:41197 TCP_FASTOPEN_NO_SYN_DATA_ACK,
[email protected]659fd67c2013-09-18 21:50:26198
jri764425442014-10-02 01:53:41199 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26200 // and we later discovered that the server had nacked initial data. This
jri764425442014-10-02 01:53:41201 // is the expected case results for TCP_FASTOPEN_SLOW_CONNECT_RETURN.
202 TCP_FASTOPEN_NO_SYN_DATA_NACK,
[email protected]659fd67c2013-09-18 21:50:26203
jri764425442014-10-02 01:53:41204 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26205 // and our later probe for ack/nack state failed.
jri764425442014-10-02 01:53:41206 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED,
[email protected]659fd67c2013-09-18 21:50:26207
jri764425442014-10-02 01:53:41208 // The initial FastOpen connect+write succeeded immediately
209 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and a subsequent attempt to read from
210 // the connection failed.
211 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED,
212
213 // The initial FastOpen connect+write failed
214 // (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
215 // and a subsequent attempt to read from the connection failed.
216 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED,
217
218 // We didn't try FastOpen because it had failed in the past
219 // (g_tcp_fastopen_has_failed was true.)
220 // NOTE: This status is currently registered before a connect/write call
221 // is attempted, and may capture some cases where the status is registered
222 // but no connect is subsequently attempted.
223 // TODO(jri): The expectation is that such cases are not the common case
224 // with TCP FastOpen for SSL sockets however. Change code to be more
225 // accurate when TCP FastOpen is used for more than just SSL sockets.
226 TCP_FASTOPEN_PREVIOUSLY_FAILED,
227
228 TCP_FASTOPEN_MAX_VALUE
[email protected]659fd67c2013-09-18 21:50:26229 };
230
danakj655b66c2016-04-16 00:51:38231 void AcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket,
[email protected]2ef2b0e2014-07-09 21:12:34232 IPEndPoint* address,
Brad Lassey3a814172018-04-26 03:30:21233 CompletionOnceCallback callback,
[email protected]2ef2b0e2014-07-09 21:12:34234 int rv);
danakj655b66c2016-04-16 00:51:38235 int HandleAcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket,
[email protected]2ef2b0e2014-07-09 21:12:34236 IPEndPoint* address,
237 int rv);
danakj655b66c2016-04-16 00:51:38238 int BuildTcpSocketPosix(std::unique_ptr<TCPSocketPosix>* tcp_socket,
tfarina4eb7aad82015-09-14 17:10:34239 IPEndPoint* address);
[email protected]659fd67c2013-09-18 21:50:26240
Brad Lassey3a814172018-04-26 03:30:21241 void ConnectCompleted(CompletionOnceCallback callback, int rv);
tbansal7b403bcc2016-04-13 22:33:21242 int HandleConnectCompleted(int rv);
[email protected]2ef2b0e2014-07-09 21:12:34243 void LogConnectBegin(const AddressList& addresses) const;
244 void LogConnectEnd(int net_error) const;
[email protected]659fd67c2013-09-18 21:50:26245
[email protected]573e6cc2014-07-12 00:33:03246 void ReadCompleted(const scoped_refptr<IOBuffer>& buf,
Brad Lassey3a814172018-04-26 03:30:21247 CompletionOnceCallback callback,
[email protected]2ef2b0e2014-07-09 21:12:34248 int rv);
Brad Lassey3a814172018-04-26 03:30:21249 void ReadIfReadyCompleted(CompletionOnceCallback callback, int rv);
[email protected]2ef2b0e2014-07-09 21:12:34250 int HandleReadCompleted(IOBuffer* buf, int rv);
xunjieli321a96f32017-03-07 19:42:17251 void HandleReadCompletedHelper(int rv);
[email protected]659fd67c2013-09-18 21:50:26252
[email protected]573e6cc2014-07-12 00:33:03253 void WriteCompleted(const scoped_refptr<IOBuffer>& buf,
Brad Lassey3a814172018-04-26 03:30:21254 CompletionOnceCallback callback,
jri764425442014-10-02 01:53:41255 int rv);
256 int HandleWriteCompleted(IOBuffer* buf, int rv);
[email protected]2ef2b0e2014-07-09 21:12:34257 int TcpFastOpenWrite(IOBuffer* buf,
258 int buf_len,
Brad Lassey3a814172018-04-26 03:30:21259 CompletionOnceCallback callback);
[email protected]659fd67c2013-09-18 21:50:26260
tbansal7b403bcc2016-04-13 22:33:21261 // Notifies |socket_performance_watcher_| of the latest RTT estimate available
262 // from the tcp_info struct for this TCP socket.
263 void NotifySocketPerformanceWatcher();
264
jri764425442014-10-02 01:53:41265 // Called after the first read completes on a TCP FastOpen socket.
266 void UpdateTCPFastOpenStatusAfterRead();
[email protected]659fd67c2013-09-18 21:50:26267
danakj655b66c2016-04-16 00:51:38268 std::unique_ptr<SocketPosix> socket_;
269 std::unique_ptr<SocketPosix> accept_socket_;
[email protected]659fd67c2013-09-18 21:50:26270
tbansal7b403bcc2016-04-13 22:33:21271 // Socket performance statistics (such as RTT) are reported to the
272 // |socket_performance_watcher_|. May be nullptr.
danakj655b66c2016-04-16 00:51:38273 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_;
tbansal7b403bcc2016-04-13 22:33:21274
[email protected]659fd67c2013-09-18 21:50:26275 // Enables experimental TCP FastOpen option.
jridcb4ae922014-09-12 23:52:39276 bool use_tcp_fastopen_;
[email protected]659fd67c2013-09-18 21:50:26277
jri764425442014-10-02 01:53:41278 // True when TCP FastOpen is in use and we have attempted the
279 // connect with write.
280 bool tcp_fastopen_write_attempted_;
281
[email protected]659fd67c2013-09-18 21:50:26282 // True when TCP FastOpen is in use and we have done the connect.
283 bool tcp_fastopen_connected_;
jri764425442014-10-02 01:53:41284
285 TCPFastOpenStatus tcp_fastopen_status_;
[email protected]659fd67c2013-09-18 21:50:26286
[email protected]659fd67c2013-09-18 21:50:26287 bool logging_multiple_connect_attempts_;
288
tfarina42834112016-09-22 13:38:20289 NetLogWithSource net_log_;
[email protected]21160f02013-09-01 23:04:27290
Paul Jensen0f49dec2017-12-12 23:39:58291 // Current socket tag if |socket_| is valid, otherwise the tag to apply when
292 // |socket_| is opened.
293 SocketTag tag_;
294
tfarina4eb7aad82015-09-14 17:10:34295 DISALLOW_COPY_AND_ASSIGN(TCPSocketPosix);
[email protected]21160f02013-09-01 23:04:27296};
297
298} // namespace net
299
tfarina4eb7aad82015-09-14 17:10:34300#endif // NET_SOCKET_TCP_SOCKET_POSIX_H_