| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/tcp_socket.h" | 5 #include "net/socket/tcp_socket.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <netdb.h> | 9 #include <netdb.h> |
| 10 #include <netinet/in.h> | 10 #include <netinet/in.h> |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 } | 104 } |
| 105 return net_error; | 105 return net_error; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 } // namespace | 110 } // namespace |
| 111 | 111 |
| 112 //----------------------------------------------------------------------------- | 112 //----------------------------------------------------------------------------- |
| 113 | 113 |
| 114 TCPSocketLibevent::Watcher::Watcher( | 114 TCPSocketLibevent::Watcher::Watcher(const base::Closure& read_ready_callback, |
| 115 const base::Closure& read_ready_callback, | 115 const base::Closure& write_ready_callback) |
| 116 const base::Closure& write_ready_callback) | |
| 117 : read_ready_callback_(read_ready_callback), | 116 : read_ready_callback_(read_ready_callback), |
| 118 write_ready_callback_(write_ready_callback) { | 117 write_ready_callback_(write_ready_callback) { |
| 119 } | 118 } |
| 120 | 119 |
| 121 TCPSocketLibevent::Watcher::~Watcher() { | 120 TCPSocketLibevent::Watcher::~Watcher() { |
| 122 } | 121 } |
| 123 | 122 |
| 124 void TCPSocketLibevent::Watcher::OnFileCanReadWithoutBlocking(int /* fd */) { | 123 void TCPSocketLibevent::Watcher::OnFileCanReadWithoutBlocking(int /* fd */) { |
| 125 if (!read_ready_callback_.is_null()) | 124 if (!read_ready_callback_.is_null()) |
| 126 read_ready_callback_.Run(); | 125 read_ready_callback_.Run(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 logging_multiple_connect_attempts_(false), | 158 logging_multiple_connect_attempts_(false), |
| 160 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 159 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
| 161 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 160 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 162 source.ToEventParametersCallback()); | 161 source.ToEventParametersCallback()); |
| 163 } | 162 } |
| 164 | 163 |
| 165 TCPSocketLibevent::~TCPSocketLibevent() { | 164 TCPSocketLibevent::~TCPSocketLibevent() { |
| 166 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 165 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
| 167 if (tcp_fastopen_connected_) { | 166 if (tcp_fastopen_connected_) { |
| 168 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", | 167 UMA_HISTOGRAM_ENUMERATION("Net.TcpFastOpenSocketConnection", |
| 169 fast_open_status_, FAST_OPEN_MAX_VALUE); | 168 fast_open_status_, |
| 169 FAST_OPEN_MAX_VALUE); |
| 170 } | 170 } |
| 171 Close(); | 171 Close(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 int TCPSocketLibevent::Open(AddressFamily family) { | 174 int TCPSocketLibevent::Open(AddressFamily family) { |
| 175 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
| 176 DCHECK_EQ(socket_, kInvalidSocket); | 176 DCHECK_EQ(socket_, kInvalidSocket); |
| 177 | 177 |
| 178 socket_ = CreatePlatformSocket(ConvertAddressFamily(family), SOCK_STREAM, | 178 socket_ = CreatePlatformSocket( |
| 179 IPPROTO_TCP); | 179 ConvertAddressFamily(family), SOCK_STREAM, IPPROTO_TCP); |
| 180 if (socket_ < 0) { | 180 if (socket_ < 0) { |
| 181 PLOG(ERROR) << "CreatePlatformSocket() returned an error"; | 181 PLOG(ERROR) << "CreatePlatformSocket() returned an error"; |
| 182 return MapSystemError(errno); | 182 return MapSystemError(errno); |
| 183 } | 183 } |
| 184 | 184 |
| 185 if (SetNonBlocking(socket_)) { | 185 if (SetNonBlocking(socket_)) { |
| 186 int result = MapSystemError(errno); | 186 int result = MapSystemError(errno); |
| 187 Close(); | 187 Close(); |
| 188 return result; | 188 return result; |
| 189 } | 189 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 DCHECK(address); | 248 DCHECK(address); |
| 249 DCHECK(!callback.is_null()); | 249 DCHECK(!callback.is_null()); |
| 250 DCHECK(accept_callback_.is_null()); | 250 DCHECK(accept_callback_.is_null()); |
| 251 | 251 |
| 252 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); | 252 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); |
| 253 | 253 |
| 254 int result = AcceptInternal(socket, address); | 254 int result = AcceptInternal(socket, address); |
| 255 | 255 |
| 256 if (result == ERR_IO_PENDING) { | 256 if (result == ERR_IO_PENDING) { |
| 257 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 257 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 258 socket_, true, base::MessageLoopForIO::WATCH_READ, | 258 socket_, |
| 259 &accept_socket_watcher_, &accept_watcher_)) { | 259 true, |
| 260 base::MessageLoopForIO::WATCH_READ, |
| 261 &accept_socket_watcher_, |
| 262 &accept_watcher_)) { |
| 260 PLOG(ERROR) << "WatchFileDescriptor failed on read"; | 263 PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
| 261 return MapSystemError(errno); | 264 return MapSystemError(errno); |
| 262 } | 265 } |
| 263 | 266 |
| 264 accept_socket_ = socket; | 267 accept_socket_ = socket; |
| 265 accept_address_ = address; | 268 accept_address_ = address; |
| 266 accept_callback_ = callback; | 269 accept_callback_ = callback; |
| 267 } | 270 } |
| 268 | 271 |
| 269 return result; | 272 return result; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 DCHECK(!waiting_connect_); | 357 DCHECK(!waiting_connect_); |
| 355 DCHECK(read_callback_.is_null()); | 358 DCHECK(read_callback_.is_null()); |
| 356 // Synchronous operation not supported | 359 // Synchronous operation not supported |
| 357 DCHECK(!callback.is_null()); | 360 DCHECK(!callback.is_null()); |
| 358 DCHECK_GT(buf_len, 0); | 361 DCHECK_GT(buf_len, 0); |
| 359 | 362 |
| 360 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); | 363 int nread = HANDLE_EINTR(read(socket_, buf->data(), buf_len)); |
| 361 if (nread >= 0) { | 364 if (nread >= 0) { |
| 362 base::StatsCounter read_bytes("tcp.read_bytes"); | 365 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 363 read_bytes.Add(nread); | 366 read_bytes.Add(nread); |
| 364 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, | 367 net_log_.AddByteTransferEvent( |
| 365 buf->data()); | 368 NetLog::TYPE_SOCKET_BYTES_RECEIVED, nread, buf->data()); |
| 366 RecordFastOpenStatus(); | 369 RecordFastOpenStatus(); |
| 367 return nread; | 370 return nread; |
| 368 } | 371 } |
| 369 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 372 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 370 int net_error = MapSystemError(errno); | 373 int net_error = MapSystemError(errno); |
| 371 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 374 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 372 CreateNetLogSocketErrorCallback(net_error, errno)); | 375 CreateNetLogSocketErrorCallback(net_error, errno)); |
| 373 return net_error; | 376 return net_error; |
| 374 } | 377 } |
| 375 | 378 |
| 376 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 379 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 377 socket_, true, base::MessageLoopForIO::WATCH_READ, | 380 socket_, |
| 378 &read_socket_watcher_, &read_watcher_)) { | 381 true, |
| 382 base::MessageLoopForIO::WATCH_READ, |
| 383 &read_socket_watcher_, |
| 384 &read_watcher_)) { |
| 379 DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno; | 385 DVLOG(1) << "WatchFileDescriptor failed on read, errno " << errno; |
| 380 return MapSystemError(errno); | 386 return MapSystemError(errno); |
| 381 } | 387 } |
| 382 | 388 |
| 383 read_buf_ = buf; | 389 read_buf_ = buf; |
| 384 read_buf_len_ = buf_len; | 390 read_buf_len_ = buf_len; |
| 385 read_callback_ = callback; | 391 read_callback_ = callback; |
| 386 return ERR_IO_PENDING; | 392 return ERR_IO_PENDING; |
| 387 } | 393 } |
| 388 | 394 |
| 389 int TCPSocketLibevent::Write(IOBuffer* buf, | 395 int TCPSocketLibevent::Write(IOBuffer* buf, |
| 390 int buf_len, | 396 int buf_len, |
| 391 const CompletionCallback& callback) { | 397 const CompletionCallback& callback) { |
| 392 DCHECK(CalledOnValidThread()); | 398 DCHECK(CalledOnValidThread()); |
| 393 DCHECK_NE(kInvalidSocket, socket_); | 399 DCHECK_NE(kInvalidSocket, socket_); |
| 394 DCHECK(!waiting_connect_); | 400 DCHECK(!waiting_connect_); |
| 395 DCHECK(write_callback_.is_null()); | 401 DCHECK(write_callback_.is_null()); |
| 396 // Synchronous operation not supported | 402 // Synchronous operation not supported |
| 397 DCHECK(!callback.is_null()); | 403 DCHECK(!callback.is_null()); |
| 398 DCHECK_GT(buf_len, 0); | 404 DCHECK_GT(buf_len, 0); |
| 399 | 405 |
| 400 int nwrite = InternalWrite(buf, buf_len); | 406 int nwrite = InternalWrite(buf, buf_len); |
| 401 if (nwrite >= 0) { | 407 if (nwrite >= 0) { |
| 402 base::StatsCounter write_bytes("tcp.write_bytes"); | 408 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 403 write_bytes.Add(nwrite); | 409 write_bytes.Add(nwrite); |
| 404 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, nwrite, | 410 net_log_.AddByteTransferEvent( |
| 405 buf->data()); | 411 NetLog::TYPE_SOCKET_BYTES_SENT, nwrite, buf->data()); |
| 406 return nwrite; | 412 return nwrite; |
| 407 } | 413 } |
| 408 if (errno != EAGAIN && errno != EWOULDBLOCK) { | 414 if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 409 int net_error = MapSystemError(errno); | 415 int net_error = MapSystemError(errno); |
| 410 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 416 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 411 CreateNetLogSocketErrorCallback(net_error, errno)); | 417 CreateNetLogSocketErrorCallback(net_error, errno)); |
| 412 return net_error; | 418 return net_error; |
| 413 } | 419 } |
| 414 | 420 |
| 415 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 421 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 416 socket_, true, base::MessageLoopForIO::WATCH_WRITE, | 422 socket_, |
| 417 &write_socket_watcher_, &write_watcher_)) { | 423 true, |
| 424 base::MessageLoopForIO::WATCH_WRITE, |
| 425 &write_socket_watcher_, |
| 426 &write_watcher_)) { |
| 418 DVLOG(1) << "WatchFileDescriptor failed on write, errno " << errno; | 427 DVLOG(1) << "WatchFileDescriptor failed on write, errno " << errno; |
| 419 return MapSystemError(errno); | 428 return MapSystemError(errno); |
| 420 } | 429 } |
| 421 | 430 |
| 422 write_buf_ = buf; | 431 write_buf_ = buf; |
| 423 write_buf_len_ = buf_len; | 432 write_buf_len_ = buf_len; |
| 424 write_callback_ = callback; | 433 write_callback_ = callback; |
| 425 return ERR_IO_PENDING; | 434 return ERR_IO_PENDING; |
| 426 } | 435 } |
| 427 | 436 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 452 return SetAddressReuse(true); | 461 return SetAddressReuse(true); |
| 453 } | 462 } |
| 454 | 463 |
| 455 void TCPSocketLibevent::SetDefaultOptionsForClient() { | 464 void TCPSocketLibevent::SetDefaultOptionsForClient() { |
| 456 DCHECK(CalledOnValidThread()); | 465 DCHECK(CalledOnValidThread()); |
| 457 | 466 |
| 458 // This mirrors the behaviour on Windows. See the comment in | 467 // This mirrors the behaviour on Windows. See the comment in |
| 459 // tcp_socket_win.cc after searching for "NODELAY". | 468 // tcp_socket_win.cc after searching for "NODELAY". |
| 460 SetTCPNoDelay(socket_, true); // If SetTCPNoDelay fails, we don't care. | 469 SetTCPNoDelay(socket_, true); // If SetTCPNoDelay fails, we don't care. |
| 461 | 470 |
| 462 // TCP keep alive wakes up the radio, which is expensive on mobile. Do not | 471 // 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 | 472 // enable it there. It's useful to prevent TCP middleboxes from timing out |
| 464 // connection mappings. Packets for timed out connection mappings at | 473 // connection mappings. Packets for timed out connection mappings at |
| 465 // middleboxes will either lead to: | 474 // middleboxes will either lead to: |
| 466 // a) Middleboxes sending TCP RSTs. It's up to higher layers to check for this | 475 // 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. | 476 // and retry. The HTTP network transaction code does this. |
| 468 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP | 477 // b) Middleboxes just drop the unrecognized TCP packet. This leads to the TCP |
| 469 // stack retransmitting packets per TCP stack retransmission timeouts, which | 478 // stack retransmitting packets per TCP stack retransmission timeouts, which |
| 470 // are very high (on the order of seconds). Given the number of | 479 // are very high (on the order of seconds). Given the number of |
| 471 // retransmissions required before killing the connection, this can lead to | 480 // retransmissions required before killing the connection, this can lead to |
| 472 // tens of seconds or even minutes of delay, depending on OS. | 481 // tens of seconds or even minutes of delay, depending on OS. |
| 473 #if !defined(OS_ANDROID) && !defined(OS_IOS) | 482 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 474 const int kTCPKeepAliveSeconds = 45; | 483 const int kTCPKeepAliveSeconds = 45; |
| 475 | 484 |
| 476 SetTCPKeepAlive(socket_, true, kTCPKeepAliveSeconds); | 485 SetTCPKeepAlive(socket_, true, kTCPKeepAliveSeconds); |
| 477 #endif | 486 #endif |
| 478 } | 487 } |
| 479 | 488 |
| 480 int TCPSocketLibevent::SetAddressReuse(bool allow) { | 489 int TCPSocketLibevent::SetAddressReuse(bool allow) { |
| 481 DCHECK(CalledOnValidThread()); | 490 DCHECK(CalledOnValidThread()); |
| 482 | 491 |
| 483 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound | 492 // 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 | 493 // 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 | 494 // 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 | 495 // 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 | 496 // bind to a TIME_WAIT end point immediately, which is a widely adopted |
| 488 // behavior. | 497 // behavior. |
| 489 // | 498 // |
| 490 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to | 499 // 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 | 500 // 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 | 501 // set SO_REUSEPORT instead. This option is not provided on Linux prior |
| 493 // to 3.9. | 502 // to 3.9. |
| 494 // | 503 // |
| 495 // SO_REUSEPORT is provided in MacOS X and iOS. | 504 // SO_REUSEPORT is provided in MacOS X and iOS. |
| 496 int boolean_value = allow ? 1 : 0; | 505 int boolean_value = allow ? 1 : 0; |
| 497 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &boolean_value, | 506 int rv = setsockopt( |
| 498 sizeof(boolean_value)); | 507 socket_, SOL_SOCKET, SO_REUSEADDR, &boolean_value, sizeof(boolean_value)); |
| 499 if (rv < 0) | 508 if (rv < 0) |
| 500 return MapSystemError(errno); | 509 return MapSystemError(errno); |
| 501 return OK; | 510 return OK; |
| 502 } | 511 } |
| 503 | 512 |
| 504 int TCPSocketLibevent::SetReceiveBufferSize(int32 size) { | 513 int TCPSocketLibevent::SetReceiveBufferSize(int32 size) { |
| 505 DCHECK(CalledOnValidThread()); | 514 DCHECK(CalledOnValidThread()); |
| 506 int rv = setsockopt(socket_, SOL_SOCKET, SO_RCVBUF, | 515 int rv = setsockopt(socket_, |
| 507 reinterpret_cast<const char*>(&size), sizeof(size)); | 516 SOL_SOCKET, |
| 517 SO_RCVBUF, |
| 518 reinterpret_cast<const char*>(&size), |
| 519 sizeof(size)); |
| 508 return (rv == 0) ? OK : MapSystemError(errno); | 520 return (rv == 0) ? OK : MapSystemError(errno); |
| 509 } | 521 } |
| 510 | 522 |
| 511 int TCPSocketLibevent::SetSendBufferSize(int32 size) { | 523 int TCPSocketLibevent::SetSendBufferSize(int32 size) { |
| 512 DCHECK(CalledOnValidThread()); | 524 DCHECK(CalledOnValidThread()); |
| 513 int rv = setsockopt(socket_, SOL_SOCKET, SO_SNDBUF, | 525 int rv = setsockopt(socket_, |
| 514 reinterpret_cast<const char*>(&size), sizeof(size)); | 526 SOL_SOCKET, |
| 527 SO_SNDBUF, |
| 528 reinterpret_cast<const char*>(&size), |
| 529 sizeof(size)); |
| 515 return (rv == 0) ? OK : MapSystemError(errno); | 530 return (rv == 0) ? OK : MapSystemError(errno); |
| 516 } | 531 } |
| 517 | 532 |
| 518 bool TCPSocketLibevent::SetKeepAlive(bool enable, int delay) { | 533 bool TCPSocketLibevent::SetKeepAlive(bool enable, int delay) { |
| 519 DCHECK(CalledOnValidThread()); | 534 DCHECK(CalledOnValidThread()); |
| 520 return SetTCPKeepAlive(socket_, enable, delay); | 535 return SetTCPKeepAlive(socket_, enable, delay); |
| 521 } | 536 } |
| 522 | 537 |
| 523 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { | 538 bool TCPSocketLibevent::SetNoDelay(bool no_delay) { |
| 524 DCHECK(CalledOnValidThread()); | 539 DCHECK(CalledOnValidThread()); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 LogConnectEnd(net_error); | 600 LogConnectEnd(net_error); |
| 586 logging_multiple_connect_attempts_ = false; | 601 logging_multiple_connect_attempts_ = false; |
| 587 } else { | 602 } else { |
| 588 NOTREACHED(); | 603 NOTREACHED(); |
| 589 } | 604 } |
| 590 } | 605 } |
| 591 | 606 |
| 592 int TCPSocketLibevent::AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket, | 607 int TCPSocketLibevent::AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket, |
| 593 IPEndPoint* address) { | 608 IPEndPoint* address) { |
| 594 SockaddrStorage storage; | 609 SockaddrStorage storage; |
| 595 int new_socket = HANDLE_EINTR(accept(socket_, | 610 int new_socket = |
| 596 storage.addr, | 611 HANDLE_EINTR(accept(socket_, storage.addr, &storage.addr_len)); |
| 597 &storage.addr_len)); | |
| 598 if (new_socket < 0) { | 612 if (new_socket < 0) { |
| 599 int net_error = MapAcceptError(errno); | 613 int net_error = MapAcceptError(errno); |
| 600 if (net_error != ERR_IO_PENDING) | 614 if (net_error != ERR_IO_PENDING) |
| 601 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 615 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 602 return net_error; | 616 return net_error; |
| 603 } | 617 } |
| 604 | 618 |
| 605 IPEndPoint ip_end_point; | 619 IPEndPoint ip_end_point; |
| 606 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) { | 620 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) { |
| 607 NOTREACHED(); | 621 NOTREACHED(); |
| 608 if (IGNORE_EINTR(close(new_socket)) < 0) | 622 if (IGNORE_EINTR(close(new_socket)) < 0) |
| 609 PLOG(ERROR) << "close"; | 623 PLOG(ERROR) << "close"; |
| 610 int net_error = ERR_ADDRESS_INVALID; | 624 int net_error = ERR_ADDRESS_INVALID; |
| 611 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 625 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 612 return net_error; | 626 return net_error; |
| 613 } | 627 } |
| 614 scoped_ptr<TCPSocketLibevent> tcp_socket(new TCPSocketLibevent( | 628 scoped_ptr<TCPSocketLibevent> tcp_socket( |
| 615 net_log_.net_log(), net_log_.source())); | 629 new TCPSocketLibevent(net_log_.net_log(), net_log_.source())); |
| 616 int adopt_result = tcp_socket->AdoptConnectedSocket(new_socket, ip_end_point); | 630 int adopt_result = tcp_socket->AdoptConnectedSocket(new_socket, ip_end_point); |
| 617 if (adopt_result != OK) { | 631 if (adopt_result != OK) { |
| 618 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 632 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 619 return adopt_result; | 633 return adopt_result; |
| 620 } | 634 } |
| 621 *socket = tcp_socket.Pass(); | 635 *socket = tcp_socket.Pass(); |
| 622 *address = ip_end_point; | 636 *address = ip_end_point; |
| 623 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 637 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
| 624 CreateNetLogIPEndPointCallback(&ip_end_point)); | 638 CreateNetLogIPEndPointCallback(&ip_end_point)); |
| 625 return OK; | 639 return OK; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 648 } | 662 } |
| 649 | 663 |
| 650 // Check if the connect() failed synchronously. | 664 // Check if the connect() failed synchronously. |
| 651 connect_os_error_ = errno; | 665 connect_os_error_ = errno; |
| 652 if (connect_os_error_ != EINPROGRESS) | 666 if (connect_os_error_ != EINPROGRESS) |
| 653 return MapConnectError(connect_os_error_); | 667 return MapConnectError(connect_os_error_); |
| 654 | 668 |
| 655 // Otherwise the connect() is going to complete asynchronously, so watch | 669 // Otherwise the connect() is going to complete asynchronously, so watch |
| 656 // for its completion. | 670 // for its completion. |
| 657 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 671 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 658 socket_, true, base::MessageLoopForIO::WATCH_WRITE, | 672 socket_, |
| 659 &write_socket_watcher_, &write_watcher_)) { | 673 true, |
| 674 base::MessageLoopForIO::WATCH_WRITE, |
| 675 &write_socket_watcher_, |
| 676 &write_watcher_)) { |
| 660 connect_os_error_ = errno; | 677 connect_os_error_ = errno; |
| 661 DVLOG(1) << "WatchFileDescriptor failed: " << connect_os_error_; | 678 DVLOG(1) << "WatchFileDescriptor failed: " << connect_os_error_; |
| 662 return MapSystemError(connect_os_error_); | 679 return MapSystemError(connect_os_error_); |
| 663 } | 680 } |
| 664 | 681 |
| 665 return ERR_IO_PENDING; | 682 return ERR_IO_PENDING; |
| 666 } | 683 } |
| 667 | 684 |
| 668 void TCPSocketLibevent::DoConnectComplete(int result) { | 685 void TCPSocketLibevent::DoConnectComplete(int result) { |
| 669 // Log the end of this attempt (and any OS error it threw). | 686 // Log the end of this attempt (and any OS error it threw). |
| (...skipping 29 matching lines...) Expand all Loading... |
| 699 | 716 |
| 700 SockaddrStorage storage; | 717 SockaddrStorage storage; |
| 701 int rv = getsockname(socket_, storage.addr, &storage.addr_len); | 718 int rv = getsockname(socket_, storage.addr, &storage.addr_len); |
| 702 if (rv != 0) { | 719 if (rv != 0) { |
| 703 PLOG(ERROR) << "getsockname() [rv: " << rv << "] error: "; | 720 PLOG(ERROR) << "getsockname() [rv: " << rv << "] error: "; |
| 704 NOTREACHED(); | 721 NOTREACHED(); |
| 705 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); | 722 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv); |
| 706 return; | 723 return; |
| 707 } | 724 } |
| 708 | 725 |
| 709 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT, | 726 net_log_.EndEvent( |
| 710 CreateNetLogSourceAddressCallback(storage.addr, | 727 NetLog::TYPE_TCP_CONNECT, |
| 711 storage.addr_len)); | 728 CreateNetLogSourceAddressCallback(storage.addr, storage.addr_len)); |
| 712 } | 729 } |
| 713 | 730 |
| 714 void TCPSocketLibevent::DidCompleteRead() { | 731 void TCPSocketLibevent::DidCompleteRead() { |
| 715 RecordFastOpenStatus(); | 732 RecordFastOpenStatus(); |
| 716 if (read_callback_.is_null()) | 733 if (read_callback_.is_null()) |
| 717 return; | 734 return; |
| 718 | 735 |
| 719 int bytes_transferred; | 736 int bytes_transferred; |
| 720 bytes_transferred = HANDLE_EINTR(read(socket_, read_buf_->data(), | 737 bytes_transferred = |
| 721 read_buf_len_)); | 738 HANDLE_EINTR(read(socket_, read_buf_->data(), read_buf_len_)); |
| 722 | 739 |
| 723 int result; | 740 int result; |
| 724 if (bytes_transferred >= 0) { | 741 if (bytes_transferred >= 0) { |
| 725 result = bytes_transferred; | 742 result = bytes_transferred; |
| 726 base::StatsCounter read_bytes("tcp.read_bytes"); | 743 base::StatsCounter read_bytes("tcp.read_bytes"); |
| 727 read_bytes.Add(bytes_transferred); | 744 read_bytes.Add(bytes_transferred); |
| 728 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, | 745 net_log_.AddByteTransferEvent( |
| 729 read_buf_->data()); | 746 NetLog::TYPE_SOCKET_BYTES_RECEIVED, result, read_buf_->data()); |
| 730 } else { | 747 } else { |
| 731 result = MapSystemError(errno); | 748 result = MapSystemError(errno); |
| 732 if (result != ERR_IO_PENDING) { | 749 if (result != ERR_IO_PENDING) { |
| 733 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, | 750 net_log_.AddEvent(NetLog::TYPE_SOCKET_READ_ERROR, |
| 734 CreateNetLogSocketErrorCallback(result, errno)); | 751 CreateNetLogSocketErrorCallback(result, errno)); |
| 735 } | 752 } |
| 736 } | 753 } |
| 737 | 754 |
| 738 if (result != ERR_IO_PENDING) { | 755 if (result != ERR_IO_PENDING) { |
| 739 read_buf_ = NULL; | 756 read_buf_ = NULL; |
| 740 read_buf_len_ = 0; | 757 read_buf_len_ = 0; |
| 741 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); | 758 bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); |
| 742 DCHECK(ok); | 759 DCHECK(ok); |
| 743 base::ResetAndReturn(&read_callback_).Run(result); | 760 base::ResetAndReturn(&read_callback_).Run(result); |
| 744 } | 761 } |
| 745 } | 762 } |
| 746 | 763 |
| 747 void TCPSocketLibevent::DidCompleteWrite() { | 764 void TCPSocketLibevent::DidCompleteWrite() { |
| 748 if (write_callback_.is_null()) | 765 if (write_callback_.is_null()) |
| 749 return; | 766 return; |
| 750 | 767 |
| 751 int bytes_transferred; | 768 int bytes_transferred; |
| 752 bytes_transferred = HANDLE_EINTR(write(socket_, write_buf_->data(), | 769 bytes_transferred = |
| 753 write_buf_len_)); | 770 HANDLE_EINTR(write(socket_, write_buf_->data(), write_buf_len_)); |
| 754 | 771 |
| 755 int result; | 772 int result; |
| 756 if (bytes_transferred >= 0) { | 773 if (bytes_transferred >= 0) { |
| 757 result = bytes_transferred; | 774 result = bytes_transferred; |
| 758 base::StatsCounter write_bytes("tcp.write_bytes"); | 775 base::StatsCounter write_bytes("tcp.write_bytes"); |
| 759 write_bytes.Add(bytes_transferred); | 776 write_bytes.Add(bytes_transferred); |
| 760 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, result, | 777 net_log_.AddByteTransferEvent( |
| 761 write_buf_->data()); | 778 NetLog::TYPE_SOCKET_BYTES_SENT, result, write_buf_->data()); |
| 762 } else { | 779 } else { |
| 763 result = MapSystemError(errno); | 780 result = MapSystemError(errno); |
| 764 if (result != ERR_IO_PENDING) { | 781 if (result != ERR_IO_PENDING) { |
| 765 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, | 782 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR, |
| 766 CreateNetLogSocketErrorCallback(result, errno)); | 783 CreateNetLogSocketErrorCallback(result, errno)); |
| 767 } | 784 } |
| 768 } | 785 } |
| 769 | 786 |
| 770 if (result != ERR_IO_PENDING) { | 787 if (result != ERR_IO_PENDING) { |
| 771 write_buf_ = NULL; | 788 write_buf_ = NULL; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 820 int nwrite; | 837 int nwrite; |
| 821 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { | 838 if (use_tcp_fastopen_ && !tcp_fastopen_connected_) { |
| 822 SockaddrStorage storage; | 839 SockaddrStorage storage; |
| 823 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len)) { | 840 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len)) { |
| 824 // Set errno to EADDRNOTAVAIL so that MapSystemError will map it to | 841 // Set errno to EADDRNOTAVAIL so that MapSystemError will map it to |
| 825 // ERR_ADDRESS_INVALID later. | 842 // ERR_ADDRESS_INVALID later. |
| 826 errno = EADDRNOTAVAIL; | 843 errno = EADDRNOTAVAIL; |
| 827 return -1; | 844 return -1; |
| 828 } | 845 } |
| 829 | 846 |
| 830 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN. | 847 int flags = 0x20000000; // Magic flag to enable TCP_FASTOPEN. |
| 831 #if defined(OS_LINUX) | 848 #if defined(OS_LINUX) |
| 832 // sendto() will fail with EPIPE when the system doesn't support TCP Fast | 849 // 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 | 850 // Open. Theoretically that shouldn't happen since the caller should check |
| 834 // for system support on startup, but users may dynamically disable TCP Fast | 851 // for system support on startup, but users may dynamically disable TCP Fast |
| 835 // Open via sysctl. | 852 // Open via sysctl. |
| 836 flags |= MSG_NOSIGNAL; | 853 flags |= MSG_NOSIGNAL; |
| 837 #endif // defined(OS_LINUX) | 854 #endif // defined(OS_LINUX) |
| 838 nwrite = HANDLE_EINTR(sendto(socket_, | 855 nwrite = HANDLE_EINTR(sendto( |
| 839 buf->data(), | 856 socket_, buf->data(), buf_len, flags, storage.addr, storage.addr_len)); |
| 840 buf_len, | |
| 841 flags, | |
| 842 storage.addr, | |
| 843 storage.addr_len)); | |
| 844 tcp_fastopen_connected_ = true; | 857 tcp_fastopen_connected_ = true; |
| 845 | 858 |
| 846 if (nwrite < 0) { | 859 if (nwrite < 0) { |
| 847 DCHECK_NE(EPIPE, errno); | 860 DCHECK_NE(EPIPE, errno); |
| 848 | 861 |
| 849 // If errno == EINPROGRESS, that means the kernel didn't have a cookie | 862 // If errno == EINPROGRESS, that means the kernel didn't have a cookie |
| 850 // and would block. The kernel is internally doing a connect() though. | 863 // 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 | 864 // 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 | 865 // asynchronous cases. Note that the user buffer has not been copied to |
| 853 // kernel space. | 866 // kernel space. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 873 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_); | 886 DCHECK_NE(FAST_OPEN_STATUS_UNKNOWN, fast_open_status_); |
| 874 bool getsockopt_success(false); | 887 bool getsockopt_success(false); |
| 875 bool server_acked_data(false); | 888 bool server_acked_data(false); |
| 876 #if defined(TCP_INFO) | 889 #if defined(TCP_INFO) |
| 877 // Probe to see the if the socket used TCP Fast Open. | 890 // Probe to see the if the socket used TCP Fast Open. |
| 878 tcp_info info; | 891 tcp_info info; |
| 879 socklen_t info_len = sizeof(tcp_info); | 892 socklen_t info_len = sizeof(tcp_info); |
| 880 getsockopt_success = | 893 getsockopt_success = |
| 881 getsockopt(socket_, IPPROTO_TCP, TCP_INFO, &info, &info_len) == 0 && | 894 getsockopt(socket_, IPPROTO_TCP, TCP_INFO, &info, &info_len) == 0 && |
| 882 info_len == sizeof(tcp_info); | 895 info_len == sizeof(tcp_info); |
| 883 server_acked_data = getsockopt_success && | 896 server_acked_data = |
| 884 (info.tcpi_options & TCPI_OPT_SYN_DATA); | 897 getsockopt_success && (info.tcpi_options & TCPI_OPT_SYN_DATA); |
| 885 #endif | 898 #endif |
| 886 if (getsockopt_success) { | 899 if (getsockopt_success) { |
| 887 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) { | 900 if (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN) { |
| 888 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK : | 901 fast_open_status_ = (server_acked_data ? FAST_OPEN_SYN_DATA_ACK |
| 889 FAST_OPEN_SYN_DATA_NACK); | 902 : FAST_OPEN_SYN_DATA_NACK); |
| 890 } else { | 903 } else { |
| 891 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK : | 904 fast_open_status_ = (server_acked_data ? FAST_OPEN_NO_SYN_DATA_ACK |
| 892 FAST_OPEN_NO_SYN_DATA_NACK); | 905 : FAST_OPEN_NO_SYN_DATA_NACK); |
| 893 } | 906 } |
| 894 } else { | 907 } else { |
| 895 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN ? | 908 fast_open_status_ = (fast_open_status_ == FAST_OPEN_FAST_CONNECT_RETURN |
| 896 FAST_OPEN_SYN_DATA_FAILED : | 909 ? FAST_OPEN_SYN_DATA_FAILED |
| 897 FAST_OPEN_NO_SYN_DATA_FAILED); | 910 : FAST_OPEN_NO_SYN_DATA_FAILED); |
| 898 } | 911 } |
| 899 } | 912 } |
| 900 } | 913 } |
| 901 | 914 |
| 902 } // namespace net | 915 } // namespace net |
| OLD | NEW |