blob: 280527606b3f26c4dfb3fe73d317c0071a120813 [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"
16#include "net/base/completion_callback.h"
17#include "net/base/net_export.h"
mikecironef22f9812016-10-04 03:40:1918#include "net/log/net_log_with_source.h"
tbansalca83c002016-04-28 20:56:2819#include "net/socket/socket_performance_watcher.h"
[email protected]21160f02013-09-01 23:04:2720
tbansal7b403bcc2016-04-13 22:33:2121namespace base {
tbansal180587c2017-02-16 15:13:2322class TimeDelta;
tbansal7b403bcc2016-04-13 22:33:2123}
24
[email protected]21160f02013-09-01 23:04:2725namespace net {
26
[email protected]659fd67c2013-09-18 21:50:2627class AddressList;
28class IOBuffer;
[email protected]21160f02013-09-01 23:04:2729class IPEndPoint;
tfarina4eb7aad82015-09-14 17:10:3430class SocketPosix;
mikecironef22f9812016-10-04 03:40:1931class NetLog;
32struct NetLogSource;
[email protected]21160f02013-09-01 23:04:2733
tfarina4eb7aad82015-09-14 17:10:3434class NET_EXPORT TCPSocketPosix {
[email protected]21160f02013-09-01 23:04:2735 public:
tbansal7b403bcc2016-04-13 22:33:2136 // |socket_performance_watcher| is notified of the performance metrics related
37 // to this socket. |socket_performance_watcher| may be null.
38 TCPSocketPosix(
danakj655b66c2016-04-16 00:51:3839 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
tbansal7b403bcc2016-04-13 22:33:2140 NetLog* net_log,
mikecironef22f9812016-10-04 03:40:1941 const NetLogSource& source);
tfarina4eb7aad82015-09-14 17:10:3442 virtual ~TCPSocketPosix();
[email protected]21160f02013-09-01 23:04:2743
tfarinafdec3052017-04-25 13:36:2544 // Opens the socket.
45 // Returns a net error code.
[email protected]c9080d82013-09-15 15:14:1646 int Open(AddressFamily family);
tfarinaa0922c82017-04-15 11:54:4947
[email protected]2ef2b0e2014-07-09 21:12:3448 // Takes ownership of |socket_fd|.
49 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address);
[email protected]659fd67c2013-09-18 21:50:2650
tfarinafdec3052017-04-25 13:36:2551 // Binds this socket to |address|. This is generally only used on a server.
52 // Should be called after Open(). Returns a net error code.
[email protected]21160f02013-09-01 23:04:2753 int Bind(const IPEndPoint& address);
[email protected]659fd67c2013-09-18 21:50:2654
tfarinafdec3052017-04-25 13:36:2555 // Put this socket on listen state with the given |backlog|.
56 // Returns a net error code.
[email protected]21160f02013-09-01 23:04:2757 int Listen(int backlog);
tfarinafdec3052017-04-25 13:36:2558
59 // Accepts incoming connection.
60 // Returns a net error code.
danakj655b66c2016-04-16 00:51:3861 int Accept(std::unique_ptr<TCPSocketPosix>* socket,
[email protected]21160f02013-09-01 23:04:2762 IPEndPoint* address,
63 const CompletionCallback& callback);
[email protected]659fd67c2013-09-18 21:50:2664
tfarinafdec3052017-04-25 13:36:2565 // Connects this socket to the given |address|.
66 // Should be called after Open().
67 // Returns a net error code.
[email protected]659fd67c2013-09-18 21:50:2668 int Connect(const IPEndPoint& address, const CompletionCallback& callback);
69 bool IsConnected() const;
70 bool IsConnectedAndIdle() const;
71
tfarinafdec3052017-04-25 13:36:2572 // IO:
[email protected]659fd67c2013-09-18 21:50:2673 // Multiple outstanding requests are not supported.
74 // Full duplex mode (reading and writing at the same time) is supported.
tfarinafdec3052017-04-25 13:36:2575
76 // Reads from the socket.
77 // Returns a net error code.
[email protected]659fd67c2013-09-18 21:50:2678 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
xunjieli321a96f32017-03-07 19:42:1779 int ReadIfReady(IOBuffer* buf,
80 int buf_len,
81 const CompletionCallback& callback);
tfarinafdec3052017-04-25 13:36:2582
83 // Writes to the socket.
84 // Returns a net error code.
[email protected]659fd67c2013-09-18 21:50:2685 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
86
tfarinafdec3052017-04-25 13:36:2587 // Copies the local tcp address into |address| and returns a net error code.
[email protected]659fd67c2013-09-18 21:50:2688 int GetLocalAddress(IPEndPoint* address) const;
tfarinafdec3052017-04-25 13:36:2589
90 // Copies the remote tcp code into |address| and returns a net error code.
[email protected]659fd67c2013-09-18 21:50:2691 int GetPeerAddress(IPEndPoint* address) const;
92
93 // Sets various socket options.
94 // The commonly used options for server listening sockets:
tfarinaa0922c82017-04-15 11:54:4995 // - AllowAddressReuse().
[email protected]21160f02013-09-01 23:04:2796 int SetDefaultOptionsForServer();
[email protected]659fd67c2013-09-18 21:50:2697 // The commonly used options for client sockets and accepted sockets:
98 // - SetNoDelay(true);
99 // - SetKeepAlive(true, 45).
100 void SetDefaultOptionsForClient();
tfarinaa0922c82017-04-15 11:54:49101 int AllowAddressReuse();
Avi Drissman13fc8932015-12-20 04:40:46102 int SetReceiveBufferSize(int32_t size);
103 int SetSendBufferSize(int32_t size);
[email protected]659fd67c2013-09-18 21:50:26104 bool SetKeepAlive(bool enable, int delay);
105 bool SetNoDelay(bool no_delay);
106
bmcquade8b62f472015-07-08 16:03:54107 // Gets the estimated RTT. Returns false if the RTT is
108 // unavailable. May also return false when estimated RTT is 0.
109 bool GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const
110 WARN_UNUSED_RESULT;
111
tfarinafdec3052017-04-25 13:36:25112 // Closes the socket.
[email protected]21160f02013-09-01 23:04:27113 void Close();
114
jridcb4ae922014-09-12 23:52:39115 void EnableTCPFastOpenIfSupported();
116
[email protected]2ef2b0e2014-07-09 21:12:34117 bool IsValid() const;
[email protected]659fd67c2013-09-18 21:50:26118
svaldez58804c402015-10-06 00:13:47119 // Detachs from the current thread, to allow the socket to be transferred to
120 // a new thread. Should only be called when the object is no longer used by
121 // the old thread.
122 void DetachFromThread();
123
[email protected]659fd67c2013-09-18 21:50:26124 // Marks the start/end of a series of connect attempts for logging purpose.
125 //
126 // TCPClientSocket may attempt to connect to multiple addresses until it
127 // succeeds in establishing a connection. The corresponding log will have
mikecirone8b85c432016-09-08 19:11:00128 // multiple NetLogEventType::TCP_CONNECT_ATTEMPT entries nested within a
129 // NetLogEventType::TCP_CONNECT. These methods set the start/end of
130 // NetLogEventType::TCP_CONNECT.
[email protected]659fd67c2013-09-18 21:50:26131 //
132 // TODO(yzshen): Change logging format and let TCPClientSocket log the
133 // start/end of a series of connect attempts itself.
134 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
135 void EndLoggingMultipleConnectAttempts(int net_error);
136
tfarina42834112016-09-22 13:38:20137 const NetLogWithSource& net_log() const { return net_log_; }
[email protected]21160f02013-09-01 23:04:27138
[email protected]21160f02013-09-01 23:04:27139 private:
jri23fdae22014-09-16 23:04:09140 // States that using a socket with TCP FastOpen can lead to.
jri764425442014-10-02 01:53:41141 enum TCPFastOpenStatus {
142 TCP_FASTOPEN_STATUS_UNKNOWN,
[email protected]659fd67c2013-09-18 21:50:26143
jri764425442014-10-02 01:53:41144 // The initial FastOpen connect attempted returned synchronously,
[email protected]659fd67c2013-09-18 21:50:26145 // indicating that we had and sent a cookie along with the initial data.
jri764425442014-10-02 01:53:41146 TCP_FASTOPEN_FAST_CONNECT_RETURN,
[email protected]659fd67c2013-09-18 21:50:26147
jri764425442014-10-02 01:53:41148 // The initial FastOpen connect attempted returned asynchronously,
[email protected]659fd67c2013-09-18 21:50:26149 // indicating that we did not have a cookie for the server.
jri764425442014-10-02 01:53:41150 TCP_FASTOPEN_SLOW_CONNECT_RETURN,
[email protected]659fd67c2013-09-18 21:50:26151
152 // Some other error occurred on connection, so we couldn't tell if
jri764425442014-10-02 01:53:41153 // FastOpen would have worked.
154 TCP_FASTOPEN_ERROR,
[email protected]659fd67c2013-09-18 21:50:26155
jri764425442014-10-02 01:53:41156 // An attempt to do a FastOpen succeeded immediately
157 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
[email protected]659fd67c2013-09-18 21:50:26158 // had acked the data we sent.
jri764425442014-10-02 01:53:41159 TCP_FASTOPEN_SYN_DATA_ACK,
[email protected]659fd67c2013-09-18 21:50:26160
jri764425442014-10-02 01:53:41161 // An attempt to do a FastOpen succeeded immediately
162 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
[email protected]659fd67c2013-09-18 21:50:26163 // had nacked the data we sent.
jri764425442014-10-02 01:53:41164 TCP_FASTOPEN_SYN_DATA_NACK,
[email protected]659fd67c2013-09-18 21:50:26165
jri764425442014-10-02 01:53:41166 // An attempt to do a FastOpen succeeded immediately
167 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and our probe to determine if the
168 // socket was using FastOpen failed.
169 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED,
[email protected]659fd67c2013-09-18 21:50:26170
jri764425442014-10-02 01:53:41171 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26172 // and we later confirmed that the server had acked initial data. This
173 // should never happen (we didn't send data, so it shouldn't have
174 // been acked).
jri764425442014-10-02 01:53:41175 TCP_FASTOPEN_NO_SYN_DATA_ACK,
[email protected]659fd67c2013-09-18 21:50:26176
jri764425442014-10-02 01:53:41177 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26178 // and we later discovered that the server had nacked initial data. This
jri764425442014-10-02 01:53:41179 // is the expected case results for TCP_FASTOPEN_SLOW_CONNECT_RETURN.
180 TCP_FASTOPEN_NO_SYN_DATA_NACK,
[email protected]659fd67c2013-09-18 21:50:26181
jri764425442014-10-02 01:53:41182 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
[email protected]659fd67c2013-09-18 21:50:26183 // and our later probe for ack/nack state failed.
jri764425442014-10-02 01:53:41184 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED,
[email protected]659fd67c2013-09-18 21:50:26185
jri764425442014-10-02 01:53:41186 // The initial FastOpen connect+write succeeded immediately
187 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and a subsequent attempt to read from
188 // the connection failed.
189 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED,
190
191 // The initial FastOpen connect+write failed
192 // (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
193 // and a subsequent attempt to read from the connection failed.
194 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED,
195
196 // We didn't try FastOpen because it had failed in the past
197 // (g_tcp_fastopen_has_failed was true.)
198 // NOTE: This status is currently registered before a connect/write call
199 // is attempted, and may capture some cases where the status is registered
200 // but no connect is subsequently attempted.
201 // TODO(jri): The expectation is that such cases are not the common case
202 // with TCP FastOpen for SSL sockets however. Change code to be more
203 // accurate when TCP FastOpen is used for more than just SSL sockets.
204 TCP_FASTOPEN_PREVIOUSLY_FAILED,
205
206 TCP_FASTOPEN_MAX_VALUE
[email protected]659fd67c2013-09-18 21:50:26207 };
208
danakj655b66c2016-04-16 00:51:38209 void AcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket,
[email protected]2ef2b0e2014-07-09 21:12:34210 IPEndPoint* address,
211 const CompletionCallback& callback,
212 int rv);
danakj655b66c2016-04-16 00:51:38213 int HandleAcceptCompleted(std::unique_ptr<TCPSocketPosix>* tcp_socket,
[email protected]2ef2b0e2014-07-09 21:12:34214 IPEndPoint* address,
215 int rv);
danakj655b66c2016-04-16 00:51:38216 int BuildTcpSocketPosix(std::unique_ptr<TCPSocketPosix>* tcp_socket,
tfarina4eb7aad82015-09-14 17:10:34217 IPEndPoint* address);
[email protected]659fd67c2013-09-18 21:50:26218
tbansal7b403bcc2016-04-13 22:33:21219 void ConnectCompleted(const CompletionCallback& callback, int rv);
220 int HandleConnectCompleted(int rv);
[email protected]2ef2b0e2014-07-09 21:12:34221 void LogConnectBegin(const AddressList& addresses) const;
222 void LogConnectEnd(int net_error) const;
[email protected]659fd67c2013-09-18 21:50:26223
[email protected]573e6cc2014-07-12 00:33:03224 void ReadCompleted(const scoped_refptr<IOBuffer>& buf,
[email protected]2ef2b0e2014-07-09 21:12:34225 const CompletionCallback& callback,
226 int rv);
xunjieli321a96f32017-03-07 19:42:17227 void ReadIfReadyCompleted(const CompletionCallback& callback, int rv);
[email protected]2ef2b0e2014-07-09 21:12:34228 int HandleReadCompleted(IOBuffer* buf, int rv);
xunjieli321a96f32017-03-07 19:42:17229 void HandleReadCompletedHelper(int rv);
[email protected]659fd67c2013-09-18 21:50:26230
[email protected]573e6cc2014-07-12 00:33:03231 void WriteCompleted(const scoped_refptr<IOBuffer>& buf,
[email protected]2ef2b0e2014-07-09 21:12:34232 const CompletionCallback& callback,
jri764425442014-10-02 01:53:41233 int rv);
234 int HandleWriteCompleted(IOBuffer* buf, int rv);
[email protected]2ef2b0e2014-07-09 21:12:34235 int TcpFastOpenWrite(IOBuffer* buf,
236 int buf_len,
237 const CompletionCallback& callback);
[email protected]659fd67c2013-09-18 21:50:26238
tbansal7b403bcc2016-04-13 22:33:21239 // Notifies |socket_performance_watcher_| of the latest RTT estimate available
240 // from the tcp_info struct for this TCP socket.
241 void NotifySocketPerformanceWatcher();
242
jri764425442014-10-02 01:53:41243 // Called after the first read completes on a TCP FastOpen socket.
244 void UpdateTCPFastOpenStatusAfterRead();
[email protected]659fd67c2013-09-18 21:50:26245
danakj655b66c2016-04-16 00:51:38246 std::unique_ptr<SocketPosix> socket_;
247 std::unique_ptr<SocketPosix> accept_socket_;
[email protected]659fd67c2013-09-18 21:50:26248
tbansal7b403bcc2016-04-13 22:33:21249 // Socket performance statistics (such as RTT) are reported to the
250 // |socket_performance_watcher_|. May be nullptr.
danakj655b66c2016-04-16 00:51:38251 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher_;
tbansal7b403bcc2016-04-13 22:33:21252
[email protected]659fd67c2013-09-18 21:50:26253 // Enables experimental TCP FastOpen option.
jridcb4ae922014-09-12 23:52:39254 bool use_tcp_fastopen_;
[email protected]659fd67c2013-09-18 21:50:26255
jri764425442014-10-02 01:53:41256 // True when TCP FastOpen is in use and we have attempted the
257 // connect with write.
258 bool tcp_fastopen_write_attempted_;
259
[email protected]659fd67c2013-09-18 21:50:26260 // True when TCP FastOpen is in use and we have done the connect.
261 bool tcp_fastopen_connected_;
jri764425442014-10-02 01:53:41262
263 TCPFastOpenStatus tcp_fastopen_status_;
[email protected]659fd67c2013-09-18 21:50:26264
[email protected]659fd67c2013-09-18 21:50:26265 bool logging_multiple_connect_attempts_;
266
tfarina42834112016-09-22 13:38:20267 NetLogWithSource net_log_;
[email protected]21160f02013-09-01 23:04:27268
tfarina4eb7aad82015-09-14 17:10:34269 DISALLOW_COPY_AND_ASSIGN(TCPSocketPosix);
[email protected]21160f02013-09-01 23:04:27270};
271
272} // namespace net
273
tfarina4eb7aad82015-09-14 17:10:34274#endif // NET_SOCKET_TCP_SOCKET_POSIX_H_