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"
6 #include "net/socket/tcp_socket_win.h"
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "base/profiler/scoped_tracker.h"
13 #include "base/win/windows_version.h"
14 #include "net/base/address_list.h"
15 #include "net/base/connection_type_histograms.h"
16 #include "net/base/io_buffer.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/base/network_activity_monitor.h"
21 #include "net/base/network_change_notifier.h"
22 #include "net/base/winsock_init.h"
23 #include "net/base/winsock_util.h"
24 #include "net/socket/socket_descriptor.h"
25 #include "net/socket/socket_net_log_params.h"
31 const int kTCPKeepAliveSeconds
= 45;
33 int SetSocketReceiveBufferSize(SOCKET socket
, int32 size
) {
34 int rv
= setsockopt(socket
, SOL_SOCKET
, SO_RCVBUF
,
35 reinterpret_cast<const char*>(&size
), sizeof(size
));
36 int net_error
= (rv
== 0) ? OK
: MapSystemError(WSAGetLastError());
37 DCHECK(!rv
) << "Could not set socket receive buffer size: " << net_error
;
41 int SetSocketSendBufferSize(SOCKET socket
, int32 size
) {
42 int rv
= setsockopt(socket
, SOL_SOCKET
, SO_SNDBUF
,
43 reinterpret_cast<const char*>(&size
), sizeof(size
));
44 int net_error
= (rv
== 0) ? OK
: MapSystemError(WSAGetLastError());
45 DCHECK(!rv
) << "Could not set socket send buffer size: " << net_error
;
50 // The Nagle implementation on windows is governed by RFC 896. The idea
51 // behind Nagle is to reduce small packets on the network. When Nagle is
52 // enabled, if a partial packet has been sent, the TCP stack will disallow
53 // further *partial* packets until an ACK has been received from the other
54 // side. Good applications should always strive to send as much data as
55 // possible and avoid partial-packet sends. However, in most real world
56 // applications, there are edge cases where this does not happen, and two
57 // partial packets may be sent back to back. For a browser, it is NEVER
58 // a benefit to delay for an RTT before the second packet is sent.
60 // As a practical example in Chromium today, consider the case of a small
61 // POST. I have verified this:
62 // Client writes 649 bytes of header (partial packet #1)
63 // Client writes 50 bytes of POST data (partial packet #2)
64 // In the above example, with Nagle, a RTT delay is inserted between these
65 // two sends due to nagle. RTTs can easily be 100ms or more. The best
66 // fix is to make sure that for POSTing data, we write as much data as
67 // possible and minimize partial packets. We will fix that. But disabling
68 // Nagle also ensure we don't run into this delay in other edge cases.
70 // http://technet.microsoft.com/en-us/library/bb726981.aspx
71 bool DisableNagle(SOCKET socket
, bool disable
) {
72 BOOL val
= disable
? TRUE
: FALSE
;
73 int rv
= setsockopt(socket
, IPPROTO_TCP
, TCP_NODELAY
,
74 reinterpret_cast<const char*>(&val
),
76 DCHECK(!rv
) << "Could not disable nagle";
80 // Enable TCP Keep-Alive to prevent NAT routers from timing out TCP
81 // connections. See http://crbug.com/27400 for details.
82 bool SetTCPKeepAlive(SOCKET socket
, BOOL enable
, int delay_secs
) {
83 unsigned delay
= delay_secs
* 1000;
84 struct tcp_keepalive keepalive_vals
= {
85 enable
? 1u : 0u, // TCP keep-alive on.
86 delay
, // Delay seconds before sending first TCP keep-alive packet.
87 delay
, // Delay seconds between sending TCP keep-alive packets.
89 DWORD bytes_returned
= 0xABAB;
90 int rv
= WSAIoctl(socket
, SIO_KEEPALIVE_VALS
, &keepalive_vals
,
91 sizeof(keepalive_vals
), NULL
, 0,
92 &bytes_returned
, NULL
, NULL
);
93 DCHECK(!rv
) << "Could not enable TCP Keep-Alive for socket: " << socket
94 << " [error: " << WSAGetLastError() << "].";
96 // Disregard any failure in disabling nagle or enabling TCP Keep-Alive.
100 int MapConnectError(int os_error
) {
102 // connect fails with WSAEACCES when Windows Firewall blocks the
105 return ERR_NETWORK_ACCESS_DENIED
;
107 return ERR_CONNECTION_TIMED_OUT
;
109 int net_error
= MapSystemError(os_error
);
110 if (net_error
== ERR_FAILED
)
111 return ERR_CONNECTION_FAILED
; // More specific than ERR_FAILED.
113 // Give a more specific error when the user is offline.
114 if (net_error
== ERR_ADDRESS_UNREACHABLE
&&
115 NetworkChangeNotifier::IsOffline()) {
116 return ERR_INTERNET_DISCONNECTED
;
126 //-----------------------------------------------------------------------------
128 // Nothing to do for Windows since it doesn't support TCP FastOpen.
129 // TODO(jri): Remove these along with the corresponding global variables.
130 bool IsTCPFastOpenSupported() { return false; }
131 bool IsTCPFastOpenUserEnabled() { return false; }
132 void CheckSupportAndMaybeEnableTCPFastOpen(bool user_enabled
) {}
134 // This class encapsulates all the state that has to be preserved as long as
135 // there is a network IO operation in progress. If the owner TCPSocketWin is
136 // destroyed while an operation is in progress, the Core is detached and it
137 // lives until the operation completes and the OS doesn't reference any resource
138 // declared on this class anymore.
139 class TCPSocketWin::Core
: public base::RefCounted
<Core
> {
141 explicit Core(TCPSocketWin
* socket
);
143 // Start watching for the end of a read or write operation.
145 void WatchForWrite();
147 // The TCPSocketWin is going away.
148 void Detach() { socket_
= NULL
; }
150 // The separate OVERLAPPED variables for asynchronous operation.
151 // |read_overlapped_| is used for both Connect() and Read().
152 // |write_overlapped_| is only used for Write();
153 OVERLAPPED read_overlapped_
;
154 OVERLAPPED write_overlapped_
;
156 // The buffers used in Read() and Write().
157 scoped_refptr
<IOBuffer
> read_iobuffer_
;
158 scoped_refptr
<IOBuffer
> write_iobuffer_
;
159 int read_buffer_length_
;
160 int write_buffer_length_
;
162 bool non_blocking_reads_initialized_
;
165 friend class base::RefCounted
<Core
>;
167 class ReadDelegate
: public base::win::ObjectWatcher::Delegate
{
169 explicit ReadDelegate(Core
* core
) : core_(core
) {}
170 ~ReadDelegate() override
{}
172 // base::ObjectWatcher::Delegate methods:
173 void OnObjectSignaled(HANDLE object
) override
;
179 class WriteDelegate
: public base::win::ObjectWatcher::Delegate
{
181 explicit WriteDelegate(Core
* core
) : core_(core
) {}
182 ~WriteDelegate() override
{}
184 // base::ObjectWatcher::Delegate methods:
185 void OnObjectSignaled(HANDLE object
) override
;
193 // The socket that created this object.
194 TCPSocketWin
* socket_
;
196 // |reader_| handles the signals from |read_watcher_|.
197 ReadDelegate reader_
;
198 // |writer_| handles the signals from |write_watcher_|.
199 WriteDelegate writer_
;
201 // |read_watcher_| watches for events from Connect() and Read().
202 base::win::ObjectWatcher read_watcher_
;
203 // |write_watcher_| watches for events from Write();
204 base::win::ObjectWatcher write_watcher_
;
206 DISALLOW_COPY_AND_ASSIGN(Core
);
209 TCPSocketWin::Core::Core(TCPSocketWin
* socket
)
210 : read_buffer_length_(0),
211 write_buffer_length_(0),
212 non_blocking_reads_initialized_(false),
216 memset(&read_overlapped_
, 0, sizeof(read_overlapped_
));
217 memset(&write_overlapped_
, 0, sizeof(write_overlapped_
));
219 read_overlapped_
.hEvent
= WSACreateEvent();
220 write_overlapped_
.hEvent
= WSACreateEvent();
223 TCPSocketWin::Core::~Core() {
224 // Make sure the message loop is not watching this object anymore.
225 read_watcher_
.StopWatching();
226 write_watcher_
.StopWatching();
228 WSACloseEvent(read_overlapped_
.hEvent
);
229 memset(&read_overlapped_
, 0xaf, sizeof(read_overlapped_
));
230 WSACloseEvent(write_overlapped_
.hEvent
);
231 memset(&write_overlapped_
, 0xaf, sizeof(write_overlapped_
));
234 void TCPSocketWin::Core::WatchForRead() {
235 // We grab an extra reference because there is an IO operation in progress.
236 // Balanced in ReadDelegate::OnObjectSignaled().
238 read_watcher_
.StartWatching(read_overlapped_
.hEvent
, &reader_
);
241 void TCPSocketWin::Core::WatchForWrite() {
242 // We grab an extra reference because there is an IO operation in progress.
243 // Balanced in WriteDelegate::OnObjectSignaled().
245 write_watcher_
.StartWatching(write_overlapped_
.hEvent
, &writer_
);
248 void TCPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object
) {
249 DCHECK_EQ(object
, core_
->read_overlapped_
.hEvent
);
250 if (core_
->socket_
) {
251 if (core_
->socket_
->waiting_connect_
)
252 core_
->socket_
->DidCompleteConnect();
254 core_
->socket_
->DidSignalRead();
260 void TCPSocketWin::Core::WriteDelegate::OnObjectSignaled(
262 DCHECK_EQ(object
, core_
->write_overlapped_
.hEvent
);
264 core_
->socket_
->DidCompleteWrite();
269 //-----------------------------------------------------------------------------
271 TCPSocketWin::TCPSocketWin(net::NetLog
* net_log
,
272 const net::NetLog::Source
& source
)
273 : socket_(INVALID_SOCKET
),
274 accept_event_(WSA_INVALID_EVENT
),
275 accept_socket_(NULL
),
276 accept_address_(NULL
),
277 waiting_connect_(false),
278 waiting_read_(false),
279 waiting_write_(false),
280 connect_os_error_(0),
281 logging_multiple_connect_attempts_(false),
282 net_log_(BoundNetLog::Make(net_log
, NetLog::SOURCE_SOCKET
)) {
283 net_log_
.BeginEvent(NetLog::TYPE_SOCKET_ALIVE
,
284 source
.ToEventParametersCallback());
288 TCPSocketWin::~TCPSocketWin() {
290 net_log_
.EndEvent(NetLog::TYPE_SOCKET_ALIVE
);
293 int TCPSocketWin::Open(AddressFamily family
) {
294 DCHECK(CalledOnValidThread());
295 DCHECK_EQ(socket_
, INVALID_SOCKET
);
297 socket_
= CreatePlatformSocket(ConvertAddressFamily(family
), SOCK_STREAM
,
299 if (socket_
== INVALID_SOCKET
) {
300 PLOG(ERROR
) << "CreatePlatformSocket() returned an error";
301 return MapSystemError(WSAGetLastError());
304 if (SetNonBlocking(socket_
)) {
305 int result
= MapSystemError(WSAGetLastError());
313 int TCPSocketWin::AdoptConnectedSocket(SOCKET socket
,
314 const IPEndPoint
& peer_address
) {
315 DCHECK(CalledOnValidThread());
316 DCHECK_EQ(socket_
, INVALID_SOCKET
);
317 DCHECK(!core_
.get());
321 if (SetNonBlocking(socket_
)) {
322 int result
= MapSystemError(WSAGetLastError());
327 core_
= new Core(this);
328 peer_address_
.reset(new IPEndPoint(peer_address
));
333 int TCPSocketWin::AdoptListenSocket(SOCKET socket
) {
334 DCHECK(CalledOnValidThread());
335 DCHECK_EQ(socket_
, INVALID_SOCKET
);
339 if (SetNonBlocking(socket_
)) {
340 int result
= MapSystemError(WSAGetLastError());
345 // |core_| is not needed for sockets that are used to accept connections.
346 // The operation here is more like Open but with an existing socket.
351 int TCPSocketWin::Bind(const IPEndPoint
& address
) {
352 DCHECK(CalledOnValidThread());
353 DCHECK_NE(socket_
, INVALID_SOCKET
);
355 SockaddrStorage storage
;
356 if (!address
.ToSockAddr(storage
.addr
, &storage
.addr_len
))
357 return ERR_ADDRESS_INVALID
;
359 int result
= bind(socket_
, storage
.addr
, storage
.addr_len
);
361 PLOG(ERROR
) << "bind() returned an error";
362 return MapSystemError(WSAGetLastError());
368 int TCPSocketWin::Listen(int backlog
) {
369 DCHECK(CalledOnValidThread());
370 DCHECK_GT(backlog
, 0);
371 DCHECK_NE(socket_
, INVALID_SOCKET
);
372 DCHECK_EQ(accept_event_
, WSA_INVALID_EVENT
);
374 accept_event_
= WSACreateEvent();
375 if (accept_event_
== WSA_INVALID_EVENT
) {
376 PLOG(ERROR
) << "WSACreateEvent()";
377 return MapSystemError(WSAGetLastError());
380 int result
= listen(socket_
, backlog
);
382 PLOG(ERROR
) << "listen() returned an error";
383 return MapSystemError(WSAGetLastError());
389 int TCPSocketWin::Accept(scoped_ptr
<TCPSocketWin
>* socket
,
391 const CompletionCallback
& callback
) {
392 DCHECK(CalledOnValidThread());
395 DCHECK(!callback
.is_null());
396 DCHECK(accept_callback_
.is_null());
398 net_log_
.BeginEvent(NetLog::TYPE_TCP_ACCEPT
);
400 int result
= AcceptInternal(socket
, address
);
402 if (result
== ERR_IO_PENDING
) {
404 WSAEventSelect(socket_
, accept_event_
, FD_ACCEPT
);
405 accept_watcher_
.StartWatching(accept_event_
, this);
407 accept_socket_
= socket
;
408 accept_address_
= address
;
409 accept_callback_
= callback
;
415 int TCPSocketWin::Connect(const IPEndPoint
& address
,
416 const CompletionCallback
& callback
) {
417 DCHECK(CalledOnValidThread());
418 DCHECK_NE(socket_
, INVALID_SOCKET
);
419 DCHECK(!waiting_connect_
);
421 // |peer_address_| and |core_| will be non-NULL if Connect() has been called.
422 // Unless Close() is called to reset the internal state, a second call to
423 // Connect() is not allowed.
424 // Please note that we enforce this even if the previous Connect() has
425 // completed and failed. Although it is allowed to connect the same |socket_|
426 // again after a connection attempt failed on Windows, it results in
427 // unspecified behavior according to POSIX. Therefore, we make it behave in
428 // the same way as TCPSocketPosix.
429 DCHECK(!peer_address_
&& !core_
.get());
431 if (!logging_multiple_connect_attempts_
)
432 LogConnectBegin(AddressList(address
));
434 peer_address_
.reset(new IPEndPoint(address
));
436 int rv
= DoConnect();
437 if (rv
== ERR_IO_PENDING
) {
438 // Synchronous operation not supported.
439 DCHECK(!callback
.is_null());
440 read_callback_
= callback
;
441 waiting_connect_
= true;
443 DoConnectComplete(rv
);
449 bool TCPSocketWin::IsConnected() const {
450 DCHECK(CalledOnValidThread());
452 if (socket_
== INVALID_SOCKET
|| waiting_connect_
)
458 // Check if connection is alive.
460 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
463 if (rv
== SOCKET_ERROR
&& WSAGetLastError() != WSAEWOULDBLOCK
)
469 bool TCPSocketWin::IsConnectedAndIdle() const {
470 DCHECK(CalledOnValidThread());
472 if (socket_
== INVALID_SOCKET
|| waiting_connect_
)
478 // Check if connection is alive and we haven't received any data
481 int rv
= recv(socket_
, &c
, 1, MSG_PEEK
);
484 if (WSAGetLastError() != WSAEWOULDBLOCK
)
490 int TCPSocketWin::Read(IOBuffer
* buf
,
492 const CompletionCallback
& callback
) {
493 DCHECK(CalledOnValidThread());
494 DCHECK_NE(socket_
, INVALID_SOCKET
);
495 DCHECK(!waiting_read_
);
496 CHECK(read_callback_
.is_null());
497 DCHECK(!core_
->read_iobuffer_
.get());
499 return DoRead(buf
, buf_len
, callback
);
502 int TCPSocketWin::Write(IOBuffer
* buf
,
504 const CompletionCallback
& callback
) {
505 DCHECK(CalledOnValidThread());
506 DCHECK_NE(socket_
, INVALID_SOCKET
);
507 DCHECK(!waiting_write_
);
508 CHECK(write_callback_
.is_null());
509 DCHECK_GT(buf_len
, 0);
510 DCHECK(!core_
->write_iobuffer_
.get());
513 write_buffer
.len
= buf_len
;
514 write_buffer
.buf
= buf
->data();
516 // TODO(wtc): Remove the assertion after enough testing.
517 AssertEventNotSignaled(core_
->write_overlapped_
.hEvent
);
519 int rv
= WSASend(socket_
, &write_buffer
, 1, &num
, 0,
520 &core_
->write_overlapped_
, NULL
);
522 if (ResetEventIfSignaled(core_
->write_overlapped_
.hEvent
)) {
523 rv
= static_cast<int>(num
);
524 if (rv
> buf_len
|| rv
< 0) {
525 // It seems that some winsock interceptors report that more was written
526 // than was available. Treat this as an error. http://crbug.com/27870
527 LOG(ERROR
) << "Detected broken LSP: Asked to write " << buf_len
528 << " bytes, but " << rv
<< " bytes reported.";
529 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
531 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, rv
,
533 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv
);
537 int os_error
= WSAGetLastError();
538 if (os_error
!= WSA_IO_PENDING
) {
539 int net_error
= MapSystemError(os_error
);
540 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
541 CreateNetLogSocketErrorCallback(net_error
, os_error
));
545 waiting_write_
= true;
546 write_callback_
= callback
;
547 core_
->write_iobuffer_
= buf
;
548 core_
->write_buffer_length_
= buf_len
;
549 core_
->WatchForWrite();
550 return ERR_IO_PENDING
;
553 int TCPSocketWin::GetLocalAddress(IPEndPoint
* address
) const {
554 DCHECK(CalledOnValidThread());
557 SockaddrStorage storage
;
558 if (getsockname(socket_
, storage
.addr
, &storage
.addr_len
))
559 return MapSystemError(WSAGetLastError());
560 if (!address
->FromSockAddr(storage
.addr
, storage
.addr_len
))
561 return ERR_ADDRESS_INVALID
;
566 int TCPSocketWin::GetPeerAddress(IPEndPoint
* address
) const {
567 DCHECK(CalledOnValidThread());
570 return ERR_SOCKET_NOT_CONNECTED
;
571 *address
= *peer_address_
;
575 int TCPSocketWin::SetDefaultOptionsForServer() {
576 return SetExclusiveAddrUse();
579 void TCPSocketWin::SetDefaultOptionsForClient() {
580 // Increase the socket buffer sizes from the default sizes for WinXP. In
581 // performance testing, there is substantial benefit by increasing from 8KB
584 // http://support.microsoft.com/kb/823764/EN-US
585 // On Vista, if we manually set these sizes, Vista turns off its receive
586 // window auto-tuning feature.
587 // http://blogs.msdn.com/wndp/archive/2006/05/05/Winhec-blog-tcpip-2.aspx
588 // Since Vista's auto-tune is better than any static value we can could set,
589 // only change these on pre-vista machines.
590 if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
591 const int32 kSocketBufferSize
= 64 * 1024;
592 SetSocketReceiveBufferSize(socket_
, kSocketBufferSize
);
593 SetSocketSendBufferSize(socket_
, kSocketBufferSize
);
596 DisableNagle(socket_
, true);
597 SetTCPKeepAlive(socket_
, true, kTCPKeepAliveSeconds
);
600 int TCPSocketWin::SetExclusiveAddrUse() {
601 // On Windows, a bound end point can be hijacked by another process by
602 // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE
603 // was introduced in Windows NT 4.0 SP4. If the socket that is bound to the
604 // end point has SO_EXCLUSIVEADDRUSE enabled, it is not possible for another
605 // socket to forcibly bind to the end point until the end point is unbound.
606 // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
607 // MSDN: http://goo.gl/M6fjQ.
609 // Unlike on *nix, on Windows a TCP server socket can always bind to an end
610 // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
613 // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end
614 // point in TIME_WAIT status. It does not have this effect for a TCP server
618 int rv
= setsockopt(socket_
, SOL_SOCKET
, SO_EXCLUSIVEADDRUSE
,
619 reinterpret_cast<const char*>(&true_value
),
622 return MapSystemError(errno
);
626 int TCPSocketWin::SetReceiveBufferSize(int32 size
) {
627 DCHECK(CalledOnValidThread());
628 return SetSocketReceiveBufferSize(socket_
, size
);
631 int TCPSocketWin::SetSendBufferSize(int32 size
) {
632 DCHECK(CalledOnValidThread());
633 return SetSocketSendBufferSize(socket_
, size
);
636 bool TCPSocketWin::SetKeepAlive(bool enable
, int delay
) {
637 return SetTCPKeepAlive(socket_
, enable
, delay
);
640 bool TCPSocketWin::SetNoDelay(bool no_delay
) {
641 return DisableNagle(socket_
, no_delay
);
644 void TCPSocketWin::Close() {
645 DCHECK(CalledOnValidThread());
647 if (socket_
!= INVALID_SOCKET
) {
648 // Only log the close event if there's actually a socket to close.
649 net_log_
.AddEvent(NetLog::EventType::TYPE_SOCKET_CLOSED
);
651 // Note: don't use CancelIo to cancel pending IO because it doesn't work
652 // when there is a Winsock layered service provider.
654 // In most socket implementations, closing a socket results in a graceful
655 // connection shutdown, but in Winsock we have to call shutdown explicitly.
656 // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure"
657 // at http://msdn.microsoft.com/en-us/library/ms738547.aspx
658 shutdown(socket_
, SD_SEND
);
660 // This cancels any pending IO.
661 if (closesocket(socket_
) < 0)
662 PLOG(ERROR
) << "closesocket";
663 socket_
= INVALID_SOCKET
;
666 if (!accept_callback_
.is_null()) {
667 accept_watcher_
.StopWatching();
668 accept_socket_
= NULL
;
669 accept_address_
= NULL
;
670 accept_callback_
.Reset();
674 WSACloseEvent(accept_event_
);
675 accept_event_
= WSA_INVALID_EVENT
;
679 if (waiting_connect_
) {
680 // We closed the socket, so this notification will never come.
681 // From MSDN' WSAEventSelect documentation:
682 // "Closing a socket with closesocket also cancels the association and
683 // selection of network events specified in WSAEventSelect for the
691 waiting_connect_
= false;
692 waiting_read_
= false;
693 waiting_write_
= false;
695 read_callback_
.Reset();
696 write_callback_
.Reset();
697 peer_address_
.reset();
698 connect_os_error_
= 0;
701 void TCPSocketWin::StartLoggingMultipleConnectAttempts(
702 const AddressList
& addresses
) {
703 if (!logging_multiple_connect_attempts_
) {
704 logging_multiple_connect_attempts_
= true;
705 LogConnectBegin(addresses
);
711 void TCPSocketWin::EndLoggingMultipleConnectAttempts(int net_error
) {
712 if (logging_multiple_connect_attempts_
) {
713 LogConnectEnd(net_error
);
714 logging_multiple_connect_attempts_
= false;
720 int TCPSocketWin::AcceptInternal(scoped_ptr
<TCPSocketWin
>* socket
,
721 IPEndPoint
* address
) {
722 SockaddrStorage storage
;
723 int new_socket
= accept(socket_
, storage
.addr
, &storage
.addr_len
);
724 if (new_socket
< 0) {
725 int net_error
= MapSystemError(WSAGetLastError());
726 if (net_error
!= ERR_IO_PENDING
)
727 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT
, net_error
);
731 IPEndPoint ip_end_point
;
732 if (!ip_end_point
.FromSockAddr(storage
.addr
, storage
.addr_len
)) {
734 if (closesocket(new_socket
) < 0)
735 PLOG(ERROR
) << "closesocket";
736 int net_error
= ERR_ADDRESS_INVALID
;
737 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT
, net_error
);
740 scoped_ptr
<TCPSocketWin
> tcp_socket(new TCPSocketWin(
741 net_log_
.net_log(), net_log_
.source()));
742 int adopt_result
= tcp_socket
->AdoptConnectedSocket(new_socket
, ip_end_point
);
743 if (adopt_result
!= OK
) {
744 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT
, adopt_result
);
747 *socket
= tcp_socket
.Pass();
748 *address
= ip_end_point
;
749 net_log_
.EndEvent(NetLog::TYPE_TCP_ACCEPT
,
750 CreateNetLogIPEndPointCallback(&ip_end_point
));
754 void TCPSocketWin::OnObjectSignaled(HANDLE object
) {
756 if (WSAEnumNetworkEvents(socket_
, accept_event_
, &ev
) == SOCKET_ERROR
) {
757 PLOG(ERROR
) << "WSAEnumNetworkEvents()";
761 if (ev
.lNetworkEvents
& FD_ACCEPT
) {
762 int result
= AcceptInternal(accept_socket_
, accept_address_
);
763 if (result
!= ERR_IO_PENDING
) {
764 accept_socket_
= NULL
;
765 accept_address_
= NULL
;
766 base::ResetAndReturn(&accept_callback_
).Run(result
);
769 // This happens when a client opens a connection and closes it before we
770 // have a chance to accept it.
771 DCHECK(ev
.lNetworkEvents
== 0);
773 // Start watching the next FD_ACCEPT event.
774 WSAEventSelect(socket_
, accept_event_
, FD_ACCEPT
);
775 accept_watcher_
.StartWatching(accept_event_
, this);
779 int TCPSocketWin::DoConnect() {
780 DCHECK_EQ(connect_os_error_
, 0);
781 DCHECK(!core_
.get());
783 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
784 CreateNetLogIPEndPointCallback(peer_address_
.get()));
786 core_
= new Core(this);
788 // WSAEventSelect sets the socket to non-blocking mode as a side effect.
789 // Our connect() and recv() calls require that the socket be non-blocking.
790 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
, FD_CONNECT
);
792 SockaddrStorage storage
;
793 if (!peer_address_
->ToSockAddr(storage
.addr
, &storage
.addr_len
))
794 return ERR_ADDRESS_INVALID
;
798 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed.
799 tracked_objects::ScopedTracker
tracking_profile(
800 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 connect()"));
801 result
= connect(socket_
, storage
.addr
, storage
.addr_len
);
805 // Connected without waiting!
807 // The MSDN page for connect says:
808 // With a nonblocking socket, the connection attempt cannot be completed
809 // immediately. In this case, connect will return SOCKET_ERROR, and
810 // WSAGetLastError will return WSAEWOULDBLOCK.
811 // which implies that for a nonblocking socket, connect never returns 0.
812 // It's not documented whether the event object will be signaled or not
813 // if connect does return 0. So the code below is essentially dead code
814 // and we don't know if it's correct.
817 if (ResetEventIfSignaled(core_
->read_overlapped_
.hEvent
))
820 int os_error
= WSAGetLastError();
821 if (os_error
!= WSAEWOULDBLOCK
) {
822 LOG(ERROR
) << "connect failed: " << os_error
;
823 connect_os_error_
= os_error
;
824 int rv
= MapConnectError(os_error
);
825 CHECK_NE(ERR_IO_PENDING
, rv
);
830 // TODO(ricea): Remove ScopedTracker below once crbug.com/436634 is fixed.
831 tracked_objects::ScopedTracker
tracking_profile(
832 FROM_HERE_WITH_EXPLICIT_FUNCTION("436634 WatchForRead()"));
834 core_
->WatchForRead();
835 return ERR_IO_PENDING
;
838 void TCPSocketWin::DoConnectComplete(int result
) {
839 // Log the end of this attempt (and any OS error it threw).
840 int os_error
= connect_os_error_
;
841 connect_os_error_
= 0;
843 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
,
844 NetLog::IntegerCallback("os_error", os_error
));
846 net_log_
.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT
);
849 if (!logging_multiple_connect_attempts_
)
850 LogConnectEnd(result
);
853 void TCPSocketWin::LogConnectBegin(const AddressList
& addresses
) {
854 net_log_
.BeginEvent(NetLog::TYPE_TCP_CONNECT
,
855 addresses
.CreateNetLogCallback());
858 void TCPSocketWin::LogConnectEnd(int net_error
) {
860 UpdateConnectionTypeHistograms(CONNECTION_ANY
);
862 if (net_error
!= OK
) {
863 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, net_error
);
867 struct sockaddr_storage source_address
;
868 socklen_t addrlen
= sizeof(source_address
);
869 int rv
= getsockname(
870 socket_
, reinterpret_cast<struct sockaddr
*>(&source_address
), &addrlen
);
872 LOG(ERROR
) << "getsockname() [rv: " << rv
873 << "] error: " << WSAGetLastError();
875 net_log_
.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT
, rv
);
880 NetLog::TYPE_TCP_CONNECT
,
881 CreateNetLogSourceAddressCallback(
882 reinterpret_cast<const struct sockaddr
*>(&source_address
),
883 sizeof(source_address
)));
886 int TCPSocketWin::DoRead(IOBuffer
* buf
, int buf_len
,
887 const CompletionCallback
& callback
) {
888 if (!core_
->non_blocking_reads_initialized_
) {
889 WSAEventSelect(socket_
, core_
->read_overlapped_
.hEvent
,
891 core_
->non_blocking_reads_initialized_
= true;
893 int rv
= recv(socket_
, buf
->data(), buf_len
, 0);
894 if (rv
== SOCKET_ERROR
) {
895 int os_error
= WSAGetLastError();
896 if (os_error
!= WSAEWOULDBLOCK
) {
897 int net_error
= MapSystemError(os_error
);
899 NetLog::TYPE_SOCKET_READ_ERROR
,
900 CreateNetLogSocketErrorCallback(net_error
, os_error
));
904 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED
, rv
,
906 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv
);
910 waiting_read_
= true;
911 read_callback_
= callback
;
912 core_
->read_iobuffer_
= buf
;
913 core_
->read_buffer_length_
= buf_len
;
914 core_
->WatchForRead();
915 return ERR_IO_PENDING
;
918 void TCPSocketWin::DidCompleteConnect() {
919 DCHECK(waiting_connect_
);
920 DCHECK(!read_callback_
.is_null());
923 WSANETWORKEVENTS events
;
926 // TODO(pkasting): Remove ScopedTracker below once crbug.com/462784 is
928 tracked_objects::ScopedTracker
tracking_profile1(
929 FROM_HERE_WITH_EXPLICIT_FUNCTION(
930 "462784 TCPSocketWin::DidCompleteConnect -> WSAEnumNetworkEvents"));
931 rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
, &events
);
934 if (rv
== SOCKET_ERROR
) {
936 os_error
= WSAGetLastError();
937 result
= MapSystemError(os_error
);
938 } else if (events
.lNetworkEvents
& FD_CONNECT
) {
939 os_error
= events
.iErrorCode
[FD_CONNECT_BIT
];
940 result
= MapConnectError(os_error
);
943 result
= ERR_UNEXPECTED
;
946 connect_os_error_
= os_error
;
947 DoConnectComplete(result
);
948 waiting_connect_
= false;
950 // TODO(pkasting): Remove ScopedTracker below once crbug.com/462784 is fixed.
951 tracked_objects::ScopedTracker
tracking_profile4(
952 FROM_HERE_WITH_EXPLICIT_FUNCTION(
953 "462784 TCPSocketWin::DidCompleteConnect -> read_callback_"));
954 DCHECK_NE(result
, ERR_IO_PENDING
);
955 base::ResetAndReturn(&read_callback_
).Run(result
);
958 void TCPSocketWin::DidCompleteWrite() {
959 DCHECK(waiting_write_
);
960 DCHECK(!write_callback_
.is_null());
962 DWORD num_bytes
, flags
;
963 BOOL ok
= WSAGetOverlappedResult(socket_
, &core_
->write_overlapped_
,
964 &num_bytes
, FALSE
, &flags
);
965 WSAResetEvent(core_
->write_overlapped_
.hEvent
);
966 waiting_write_
= false;
969 int os_error
= WSAGetLastError();
970 rv
= MapSystemError(os_error
);
971 net_log_
.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR
,
972 CreateNetLogSocketErrorCallback(rv
, os_error
));
974 rv
= static_cast<int>(num_bytes
);
975 if (rv
> core_
->write_buffer_length_
|| rv
< 0) {
976 // It seems that some winsock interceptors report that more was written
977 // than was available. Treat this as an error. http://crbug.com/27870
978 LOG(ERROR
) << "Detected broken LSP: Asked to write "
979 << core_
->write_buffer_length_
<< " bytes, but " << rv
980 << " bytes reported.";
981 rv
= ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES
;
983 net_log_
.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT
, num_bytes
,
984 core_
->write_iobuffer_
->data());
985 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(num_bytes
);
989 core_
->write_iobuffer_
= NULL
;
991 DCHECK_NE(rv
, ERR_IO_PENDING
);
992 base::ResetAndReturn(&write_callback_
).Run(rv
);
995 void TCPSocketWin::DidSignalRead() {
996 DCHECK(waiting_read_
);
997 DCHECK(!read_callback_
.is_null());
1000 WSANETWORKEVENTS network_events
;
1001 int rv
= WSAEnumNetworkEvents(socket_
, core_
->read_overlapped_
.hEvent
,
1003 if (rv
== SOCKET_ERROR
) {
1004 os_error
= WSAGetLastError();
1005 rv
= MapSystemError(os_error
);
1006 } else if (network_events
.lNetworkEvents
) {
1007 // TODO(pkasting): Remove ScopedTracker below once crbug.com/462778 is
1009 tracked_objects::ScopedTracker
tracking_profile2(
1010 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1011 "462778 TCPSocketWin::DidSignalRead -> DoRead"));
1012 DCHECK_EQ(network_events
.lNetworkEvents
& ~(FD_READ
| FD_CLOSE
), 0);
1013 // If network_events.lNetworkEvents is FD_CLOSE and
1014 // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful
1015 // connection closure. It is tempting to directly set rv to 0 in
1016 // this case, but the MSDN pages for WSAEventSelect and
1017 // WSAAsyncSelect recommend we still call DoRead():
1018 // FD_CLOSE should only be posted after all data is read from a
1019 // socket, but an application should check for remaining data upon
1020 // receipt of FD_CLOSE to avoid any possibility of losing data.
1022 // If network_events.iErrorCode[FD_READ_BIT] or
1023 // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call
1024 // DoRead() because recv() reports a more accurate error code
1025 // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was
1027 rv
= DoRead(core_
->read_iobuffer_
.get(), core_
->read_buffer_length_
,
1029 if (rv
== ERR_IO_PENDING
)
1032 // This may happen because Read() may succeed synchronously and
1033 // consume all the received data without resetting the event object.
1034 core_
->WatchForRead();
1038 waiting_read_
= false;
1039 core_
->read_iobuffer_
= NULL
;
1040 core_
->read_buffer_length_
= 0;
1042 DCHECK_NE(rv
, ERR_IO_PENDING
);
1043 base::ResetAndReturn(&read_callback_
).Run(rv
);
1046 bool TCPSocketWin::GetEstimatedRoundTripTime(base::TimeDelta
* out_rtt
) const {
1048 // TODO(bmcquade): Consider implementing using
1049 // GetPerTcpConnectionEStats/GetPerTcp6ConnectionEStats.