1 // 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.
5 #include "net/socket/tcp_socket.h"
10 #include <netinet/in.h>
11 #include <netinet/tcp.h>
12 #include <sys/socket.h>
14 #include "base/callback_helpers.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/metrics/stats_counters.h"
18 #include "base/posix/eintr_wrapper.h"
19 #include "build/build_config.h"
20 #include "net/base/address_list.h"
21 #include "net/base/connection_type_histograms.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/ip_endpoint.h"
24 #include "net/base/net_errors.h"
25 #include "net/base/net_util.h"
26 #include "net/base/network_change_notifier.h"
27 #include "net/socket/socket_net_log_params.h"
29 // If we don't have a definition for TCPI_OPT_SYN_DATA, create one.
30 #ifndef TCPI_OPT_SYN_DATA
31 #define TCPI_OPT_SYN_DATA 32
38 // SetTCPNoDelay turns on/off buffering in the kernel. By default, TCP sockets
39 // will wait up to 200ms for more data to complete a packet before transmitting.
40 // After calling this function, the kernel will not wait. See TCP_NODELAY in
42 bool SetTCPNoDelay(int fd, bool no_delay) {
43 int on = no_delay ? 1 : 0;
44 int error = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on));
48 // SetTCPKeepAlive sets SO_KEEPALIVE.
49 bool SetTCPKeepAlive(int fd, bool enable, int delay) {
50 int on = enable ? 1 : 0;
51 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof(on))) {
52 PLOG(ERROR) << "Failed to set SO_KEEPALIVE on fd: " << fd;
56 // If we disabled TCP keep alive, our work is done here.
60 #if defined(OS_LINUX) || defined(OS_ANDROID)
61 // Set seconds until first TCP keep alive.
62 if (setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &delay, sizeof(delay))) {
63 PLOG(ERROR) << "Failed to set TCP_KEEPIDLE on fd: " << fd;
66 // Set seconds between TCP keep alives.
67 if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &delay, sizeof(delay))) {
68 PLOG(ERROR) << "Failed to set TCP_KEEPINTVL on fd: " << fd;
75 int MapAcceptError(int os_error) {
77 // If the client aborts the connection before the server calls accept,
78 // POSIX specifies accept should fail with ECONNABORTED. The server can
79 // ignore the error and just call accept again, so we map the error to
80 // ERR_IO_PENDING. See UNIX Network Programming, Vol. 1, 3rd Ed., Sec.
81 // 5.11, "Connection Abort before accept Returns".
83 return ERR_IO_PENDING;
85 return MapSystemError(os_error);
89 int MapConnectError(int os_error) {
92 return ERR_NETWORK_ACCESS_DENIED;
94 return ERR_CONNECTION_TIMED_OUT;
96 int net_error = MapSystemError(os_error);
97 if (net_error == ERR_FAILED)
98 return ERR_CONNECTION_FAILED; // More specific than ERR_FAILED.
100 // Give a more specific error when the user is offline.
101 if (net_error == ERR_ADDRESS_UNREACHABLE &&
102 NetworkChangeNotifier::IsOffline()) {
103 return ERR_INTERNET_DISCONNECTED;
112 //-----------------------------------------------------------------------------
114 TCPSocketLibevent::Watcher::Watcher(
115 const base::Closure& read_ready_callback,
116 const base::Closure& write_ready_callback)
117 : read_ready_callback_(read_ready_callback),
118 write_ready_callback_(write_ready_callback) {
121 TCPSocketLibevent::Watcher::~Watcher() {
124 void TCPSocketLibevent::Watcher::OnFileCanReadWithoutBlocking(int /* fd */) {
125 if (!read_ready_callback_.is_null())
126 read_ready_callback_.Run();
131 void TCPSocketLibevent::Watcher::OnFileCanWriteWithoutBlocking(int /* fd */) {
132 if (!write_ready_callback_.is_null())
133 write_ready_callback_.Run();
138 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log,
139 const NetLog::Source& source)
140 : socket_(kInvalidSocket),
141 accept_watcher_(base::Bind(&TCPSocketLibevent::DidCompleteAccept,
142 base::Unretained(this)),
144 accept_socket_(NULL),
145 accept_address_(NULL),
146 read_watcher_(base::Bind(&TCPSocketLibevent::DidCompleteRead,
147 base::Unretained(this)),
149 write_watcher_(base::Closure(),
150 base::Bind(&TCPSocketLibevent::DidCompleteConnectOrWrite,
151 base::Unretained(this))),
154 use_tcp_fastopen_(IsTCPFastOpenEnabled()),
155 tcp_fastopen_connected_(false),
156 fast_open_status_(FAST_OPEN_STATUS_UNKNOWN),
157 waiting_connect_(false),
158 connect_os_error_(0),
159 logging_multiple_connect_attempts_(false),
160 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
161 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
162 source.ToEventParametersCallback());
165 TCPSocketLibevent::~TCPSocketLibevent() {
166 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
167 if (tcp_fastopen_connected_) {
168 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection",
169 fast_open_status_, FAST_OPEN_MAX_VALUE);
174 int TCPSocketLibevent::Open(AddressFamily family) {
175 DCHECK(CalledOnValidThread());
176 DCHECK_EQ(socket_, kInvalidSocket);
178 socket_ = CreatePlatformSocket(ConvertAddressFamily(family), SOCK_STREAM,
181 PLOG(ERROR) << "CreatePlatformSocket() returned an error";
182 return MapSystemError(errno);
185 if (SetNonBlocking(socket_)) {
186 int result = MapSystemError(errno);
194 int TCPSocketLibevent::AdoptConnectedSocket(int socket,
195 const IPEndPoint& peer_address) {
196 DCHECK(CalledOnValidThread());
197 DCHECK_EQ(socket_, kInvalidSocket);
201 if (SetNonBlocking(socket_)) {
202 int result = MapSystemError(errno);
207 peer_address_.reset(new IPEndPoint(peer_address));
212 int TCPSocketLibevent::Bind(const IPEndPoint& address) {
213 DCHECK(CalledOnValidThread());
214 DCHECK_NE(socket_, kInvalidSocket);
216 SockaddrStorage storage;
217 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
218 return ERR_ADDRESS_INVALID;
220 int result = bind(socket_, storage.addr, storage.addr_len);
222 PLOG(ERROR) << "bind() returned an error";
223 return MapSystemError(errno);
229 int TCPSocketLibevent::Listen(int backlog) {
230 DCHECK(CalledOnValidThread());
231 DCHECK_GT(backlog, 0);
232 DCHECK_NE(socket_, kInvalidSocket);
234 int result = listen(socket_, backlog);
236 PLOG(ERROR) << "listen() returned an error";
237 return MapSystemError(errno);
243 int TCPSocketLibevent::Accept(scoped_ptr<TCPSocketLibevent>* socket,
245 const CompletionCallback& callback) {
246 DCHECK(CalledOnValidThread());
249 DCHECK(!callback.is_null());
250 DCHECK(accept_callback_.is_null());
252 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
254 int result = AcceptInternal(socket, address);
256 if (result == ERR_IO_PENDING) {
257 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
258 socket_, true, base::MessageLoopForIO::WATCH_READ,
259 &accept_socket_watcher_, &accept_watcher_)) {
260 PLOG(ERROR) << "WatchFileDescriptor failed on read";
261 return MapSystemError(errno);
264 accept_socket_ = socket;
265 accept_address_ = address;
266 accept_callback_ = callback;
272 int TCPSocketLibevent::Connect(const IPEndPoint& address,
273 const CompletionCallback& callback) {
274 DCHECK(CalledOnValidThread());
275 DCHECK_NE(socket_, kInvalidSocket);
276 DCHECK(!waiting_connect_);
278 // |peer_address_| will be non-NULL if Connect() has been called. Unless
279 // Close() is called to reset the internal state, a second call to Connect()
281 // Please note that we don't allow a second Connect() even if the previous
282 // Connect() has failed. Connecting the same |socket_| again after a
283 // connection attempt failed results in unspecified behavior according to
285 DCHECK(!peer_address_);
287 if (!logging_multiple_connect_attempts_)
288 LogConnectBegin(AddressList(address));
290 peer_address_.reset(new IPEndPoint(address));
292 int rv = DoConnect();
293 if (rv == ERR_IO_PENDING) {
294 // Synchronous operation not supported.
295 DCHECK(!callback.is_null());
296 write_callback_ = callback;
297 waiting_connect_ = true;
299 DoConnectComplete(rv);
305 bool TCPSocketLibevent::IsConnected() const {
306 DCHECK(CalledOnValidThread());
308 if (socket_ == kInvalidSocket || waiting_connect_)
311 if (use_tcp_fastopen_ && !tcp_fastopen_connected_ && peer_address_) {
312 // With TCP FastOpen, we pretend that the socket is connected.
313 // This allows GetPeerAddress() to return peer_address_.
317 // Check if connection is alive.
319 int rv = HANDLE_EINTR(recv(socket_, &c, 1, MSG_PEEK));
322 if (rv == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
328 bool TCPSocketLibevent::IsConnectedAndIdle() const {
329 DCHECK(CalledOnValidThread());
331 if (socket_ == kInvalidSocket || waiting_connect_)
334 // TODO(wtc): should we also handle the TCP FastOpen case here,
335 // as we do in IsConnected()?
337 // Check if connection is alive and we haven't received any data
340 int rv = HANDLE_EINTR(recv(socket_, &c, 1, MSG_PEEK));
343 if (errno != EAGAIN && errno != EWOULDBLOCK)
349 int TCPSocketLibevent::Read(IOBuffer* buf,
351 const CompletionCallback& callback) {
352 DCHECK(CalledOnValidThread());
353 DCHECK_NE(kInvalidSocket, socket_);
354 DCHECK(!waiting_connect_);
355 DCHECK(read_callback_.is_null());
356 // Synchronous operation not supported
357 DCHECK(!callback.is_null());
358 DCHECK_GT(buf_len, 0);
360 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len));
362 base::StatsCounter read_bytes("tcp.read_bytes");
363 read_bytes.Add(nread);
364 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread,
366 RecordFastOpenStatus();
369 if (errno != EAGAIN && errno != EWOULDBLOCK) {
370 int net_error = MapSystemError(errno);
371 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR,
372 CreateNetLogSocketErrorCallback(net_error, errno));
376 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
377 socket_, true, base::MessageLoopForIO::WATCH_READ,
378 &read_socket_watcher_, &read_watcher_)) {
379 DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno;
380 return MapSystemError(errno);
384 read_buf_len_ = buf_len;
385 read_callback_ = callback;
386 return ERR_IO_PENDING;
389 int TCPSocketLibevent::Write(IOBuffer* buf,
391 const CompletionCallback& callback) {
392 DCHECK(CalledOnValidThread());
393 DCHECK_NE(kInvalidSocket, socket_);
394 DCHECK(!waiting_connect_);
395 DCHECK(write_callback_.is_null());
396 // Synchronous operation not supported
397 DCHECK(!callback.is_null());
398 DCHECK_GT(buf_len, 0);
400 int nwrite = InternalWrite(buf, buf_len);
402 base::StatsCounter write_bytes("tcp.write_bytes");
403 write_bytes.Add(nwrite);
404 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, nwrite,
408 if (errno != EAGAIN && errno != EWOULDBLOCK) {
409 int net_error = MapSystemError(errno);
410 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR,
411 CreateNetLogSocketErrorCallback(net_error, errno));
415 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
416 socket_, true, base::MessageLoopForIO::WATCH_WRITE,
417 &write_socket_watcher_, &write_watcher_)) {
418 DVLOG(1) << "WatchFileDescriptor failed on write, errno " << errno;
419 return MapSystemError(errno);
423 write_buf_len_ = buf_len;
424 write_callback_ = callback;
425 return ERR_IO_PENDING;
428 int TCPSocketLibevent::GetLocalAddress(IPEndPoint* address) const {
429 DCHECK(CalledOnValidThread());
432 SockaddrStorage storage;
433 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0)
434 return MapSystemError(errno);
435 if (!address->FromSockAddr(storage.addr, storage.addr_len))
436 return ERR_ADDRESS_INVALID;
441 int TCPSocketLibevent::GetPeerAddress(IPEndPoint* address) const {
442 DCHECK(CalledOnValidThread());
445 return ERR_SOCKET_NOT_CONNECTED;
446 *address = *peer_address_;
450 int TCPSocketLibevent::SetDefaultOptionsForServer() {
451 DCHECK(CalledOnValidThread());
452 return SetAddressReuse(true);
455 void TCPSocketLibevent::SetDefaultOptionsForClient() {
456 DCHECK(CalledOnValidThread());
458 // This mirrors the behaviour on Windows. See the comment in
459 // tcp_socket_win.cc after searching for "NODELAY".
460 SetTCPNoDelay(socket_, true); // If SetTCPNoDelay fails, we don't care.
462 // TCP keep alive wakes up the radio, which is expensive on mobile. Do not
463 // enable it there. It's useful to prevent TCP middleboxes from timing out
464 // connection mappings. Packets for timed out connection mappings at
465 // middleboxes will either lead to:
466 // a) Middleboxes sending TCP RSTs. It's up to higher layers to check for this
467 // and retry. The HTTP network transaction code does this.
468 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP
469 // stack retransmitting packets per TCP stack retransmission timeouts, which
470 // are very high (on the order of seconds). Given the number of
471 // retransmissions required before killing the connection, this can lead to
472 // tens of seconds or even minutes of delay, depending on OS.
473 #if !defined(OS_ANDROID) && !defined(OS_IOS)
474 const int kTCPKeepAliveSeconds = 45;
476 SetTCPKeepAlive(socket_, true, kTCPKeepAliveSeconds);
480 int TCPSocketLibevent::SetAddressReuse(bool allow) {
481 DCHECK(CalledOnValidThread());
483 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound
484 // port. When a socket is closed, the end point changes its state to TIME_WAIT
485 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer
486 // acknowledges its closure. For server sockets, it is usually safe to
487 // bind to a TIME_WAIT end point immediately, which is a widely adopted
490 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to
491 // an end point that is already bound by another socket. To do that one must
492 // set SO_REUSEPORT instead. This option is not provided on Linux prior
495 // SO_REUSEPORT is provided in MacOS X and iOS.
496 int boolean_value = allow ? 1 : 0;
497 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &boolean_value,
498 sizeof(boolean_value));
500 return MapSystemError(errno);
504 int TCPSocketLibevent::SetReceiveBufferSize(int32 size) {
505 DCHECK(CalledOnValidThread());
506 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF,
507 reinterpret_cast<const char*>(&size), sizeof(size));
508 return (rv == 0) ? OK : MapSystemError(errno);
511 int TCPSocketLibevent::SetSendBufferSize(int32 size) {
512 DCHECK(CalledOnValidThread());
513 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF,
514 reinterpret_cast<const char*>(&size), sizeof(size));
515 return (rv == 0) ? OK : MapSystemError(errno);
518 bool TCPSocketLibevent::SetKeepAlive(bool enable, int delay) {
519 DCHECK(CalledOnValidThread());
520 return SetTCPKeepAlive(socket_, enable, delay);
523 bool TCPSocketLibevent::SetNoDelay(bool no_delay) {
524 DCHECK(CalledOnValidThread());
525 return SetTCPNoDelay(socket_, no_delay);
528 void TCPSocketLibevent::Close() {
529 DCHECK(CalledOnValidThread());
531 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
533 ok = read_socket_watcher_.StopWatchingFileDescriptor();
535 ok = write_socket_watcher_.StopWatchingFileDescriptor();
538 if (socket_ != kInvalidSocket) {
539 if (IGNORE_EINTR(close(socket_)) < 0)
540 PLOG(ERROR) << "close";
541 socket_ = kInvalidSocket;
544 if (!accept_callback_.is_null()) {
545 accept_socket_ = NULL;
546 accept_address_ = NULL;
547 accept_callback_.Reset();
550 if (!read_callback_.is_null()) {
553 read_callback_.Reset();
556 if (!write_callback_.is_null()) {
559 write_callback_.Reset();
562 tcp_fastopen_connected_ = false;
563 fast_open_status_ = FAST_OPEN_STATUS_UNKNOWN;
564 waiting_connect_ = false;
565 peer_address_.reset();
566 connect_os_error_ = 0;
569 bool TCPSocketLibevent::UsingTCPFastOpen() const {
570 return use_tcp_fastopen_;
573 void TCPSocketLibevent::StartLoggingMultipleConnectAttempts(
574 const AddressList& addresses) {
575 if (!logging_multiple_connect_attempts_) {
576 logging_multiple_connect_attempts_ = true;
577 LogConnectBegin(addresses);
583 void TCPSocketLibevent::EndLoggingMultipleConnectAttempts(int net_error) {
584 if (logging_multiple_connect_attempts_) {
585 LogConnectEnd(net_error);
586 logging_multiple_connect_attempts_ = false;
592 int TCPSocketLibevent::AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket,
593 IPEndPoint* address) {
594 SockaddrStorage storage;
595 int new_socket = HANDLE_EINTR(accept(socket_,
598 if (new_socket < 0) {
599 int net_error = MapAcceptError(errno);
600 if (net_error != ERR_IO_PENDING)
601 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
605 IPEndPoint ip_end_point;
606 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
608 if (IGNORE_EINTR(close(new_socket)) < 0)
609 PLOG(ERROR) << "close";
610 int net_error = ERR_ADDRESS_INVALID;
611 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
614 scoped_ptr<TCPSocketLibevent> tcp_socket(new TCPSocketLibevent(
615 net_log_.net_log(), net_log_.source()));
616 int adopt_result = tcp_socket->AdoptConnectedSocket(new_socket, ip_end_point);
617 if (adopt_result != OK) {
618 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
621 *socket = tcp_socket.Pass();
622 *address = ip_end_point;
623 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
624 CreateNetLogIPEndPointCallback(&ip_end_point));
628 int TCPSocketLibevent::DoConnect() {
629 DCHECK_EQ(0, connect_os_error_);
631 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
632 CreateNetLogIPEndPointCallback(peer_address_.get()));
634 // Connect the socket.
635 if (!use_tcp_fastopen_) {
636 SockaddrStorage storage;
637 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len))
638 return ERR_ADDRESS_INVALID;
640 if (!HANDLE_EINTR(connect(socket_, storage.addr, storage.addr_len))) {
641 // Connected without waiting!
645 // With TCP FastOpen, we pretend that the socket is connected.
646 DCHECK(!tcp_fastopen_connected_);
650 // Check if the connect() failed synchronously.
651 connect_os_error_ = errno;
652 if (connect_os_error_ != EINPROGRESS)
653 return MapConnectError(connect_os_error_);
655 // Otherwise the connect() is going to complete asynchronously, so watch
656 // for its completion.
657 if (!base::MessageLoopForIO::current()->WatchFileDescriptor(
658 socket_, true, base::MessageLoopForIO::WATCH_WRITE,
659 &write_socket_watcher_, &write_watcher_)) {
660 connect_os_error_ = errno;
661 DVLOG(1) << "WatchFileDescriptor failed: " << connect_os_error_;
662 return MapSystemError(connect_os_error_);
665 return ERR_IO_PENDING;
668 void TCPSocketLibevent::DoConnectComplete(int result) {
669 // Log the end of this attempt (and any OS error it threw).
670 int os_error = connect_os_error_;
671 connect_os_error_ = 0;
673 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
674 NetLog::IntegerCallback("os_error", os_error));
676 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT);
679 if (!logging_multiple_connect_attempts_)
680 LogConnectEnd(result);
683 void TCPSocketLibevent::LogConnectBegin(const AddressList& addresses) {
684 base::StatsCounter connects("tcp.connect");
685 connects.Increment();
687 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT,
688 addresses.CreateNetLogCallback());
691 void TCPSocketLibevent::LogConnectEnd(int net_error) {
693 UpdateConnectionTypeHistograms(CONNECTION_ANY);
695 if (net_error != OK) {
696 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, net_error);
700 SockaddrStorage storage;
701 int rv = getsockname(socket_, storage.addr, &storage.addr_len);
703 PLOG(ERROR) << "getsockname() [rv: " << rv << "] error: ";
705 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv);
709 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT,
710 CreateNetLogSourceAddressCallback(storage.addr,
714 void TCPSocketLibevent::DidCompleteRead() {
715 RecordFastOpenStatus();
716 if (read_callback_.is_null())
719 int bytes_transferred;
720 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(),
724 if (bytes_transferred >= 0) {
725 result = bytes_transferred;
726 base::StatsCounter read_bytes("tcp.read_bytes");
727 read_bytes.Add(bytes_transferred);
728 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result,
731 result = MapSystemError(errno);
732 if (result != ERR_IO_PENDING) {
733 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR,
734 CreateNetLogSocketErrorCallback(result, errno));
738 if (result != ERR_IO_PENDING) {
741 bool ok = read_socket_watcher_.StopWatchingFileDescriptor();
743 base::ResetAndReturn(&read_callback_).Run(result);
747 void TCPSocketLibevent::DidCompleteWrite() {
748 if (write_callback_.is_null())
751 int bytes_transferred;
752 bytes_transferred = HANDLE_EINTR(write(socket_, write_buf_->data(),
756 if (bytes_transferred >= 0) {
757 result = bytes_transferred;
758 base::StatsCounter write_bytes("tcp.write_bytes");
759 write_bytes.Add(bytes_transferred);
760 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, result,
763 result = MapSystemError(errno);
764 if (result != ERR_IO_PENDING) {
765 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR,
766 CreateNetLogSocketErrorCallback(result, errno));
770 if (result != ERR_IO_PENDING) {
773 write_socket_watcher_.StopWatchingFileDescriptor();
774 base::ResetAndReturn(&write_callback_).Run(result);
778 void TCPSocketLibevent::DidCompleteConnect() {
779 DCHECK(waiting_connect_);
781 // Get the error that connect() completed with.
783 socklen_t len = sizeof(os_error);
784 if (getsockopt(socket_, SOL_SOCKET, SO_ERROR, &os_error, &len) < 0)
787 int result = MapConnectError(os_error);
788 connect_os_error_ = os_error;
789 if (result != ERR_IO_PENDING) {
790 DoConnectComplete(result);
791 waiting_connect_ = false;
792 write_socket_watcher_.StopWatchingFileDescriptor();
793 base::ResetAndReturn(&write_callback_).Run(result);
797 void TCPSocketLibevent::DidCompleteConnectOrWrite() {
798 if (waiting_connect_)
799 DidCompleteConnect();
804 void TCPSocketLibevent::DidCompleteAccept() {
805 DCHECK(CalledOnValidThread());
807 int result = AcceptInternal(accept_socket_, accept_address_);
808 if (result != ERR_IO_PENDING) {
809 accept_socket_ = NULL;
810 accept_address_ = NULL;
811 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
813 CompletionCallback callback = accept_callback_;
814 accept_callback_.Reset();
815 callback.Run(result);
819 int TCPSocketLibevent::InternalWrite(IOBuffer* buf, int buf_len) {
821 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) {
822 SockaddrStorage storage;
823 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len)) {
824 // Set errno to EADDRNOTAVAIL so that MapSystemError will map it to
825 // ERR_ADDRESS_INVALID later.
826 errno = EADDRNOTAVAIL;
830 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN.
831 #if defined(OS_LINUX)
832 // sendto() will fail with EPIPE when the system doesn't support TCP Fast
833 // Open. Theoretically that shouldn't happen since the caller should check
834 // for system support on startup, but users may dynamically disable TCP Fast
836 flags |= MSG_NOSIGNAL;
837 #endif // defined(OS_LINUX)
838 nwrite = HANDLE_EINTR(sendto(socket_,
844 tcp_fastopen_connected_ = true;
847 DCHECK_NE(EPIPE, errno);
849 // If errno == EINPROGRESS, that means the kernel didn't have a cookie
850 // and would block. The kernel is internally doing a connect() though.
851 // Remap EINPROGRESS to EAGAIN so we treat this the same as our other
852 // asynchronous cases. Note that the user buffer has not been copied to
854 if (errno == EINPROGRESS) {
856 fast_open_status_ = FAST_OPEN_SLOW_CONNECT_RETURN;
858 fast_open_status_ = FAST_OPEN_ERROR;
861 fast_open_status_ = FAST_OPEN_FAST_CONNECT_RETURN;
864 nwrite = HANDLE_EINTR(write(socket_, buf->data(), buf_len));
869 void TCPSocketLibevent::RecordFastOpenStatus() {
870 if (use_tcp_fastopen_ &&
871 (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ||
872 fast_open_status_ == FAST_OPEN_SLOW_CONNECT_RETURN)) {
873 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_);
874 bool getsockopt_success(false);
875 bool server_acked_data(false);
876 #if defined(TCP_INFO)
877 // Probe to see the if the socket used TCP Fast Open.
879 socklen_t info_len = sizeof(tcp_info);
881 getsockopt(socket_, IPPROTO_TCP, TCP_INFO, &info, &info_len) == 0 &&
882 info_len == sizeof(tcp_info);
883 server_acked_data = getsockopt_success &&
884 (info.tcpi_options & TCPI_OPT_SYN_DATA);
886 if (getsockopt_success) {
887 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) {
888 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK :
889 FAST_OPEN_SYN_DATA_NACK);
891 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK :
892 FAST_OPEN_NO_SYN_DATA_NACK);
895 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ?
896 FAST_OPEN_SYN_DATA_FAILED :
897 FAST_OPEN_NO_SYN_DATA_FAILED);