prune resources in MemoryCache
[chromium-blink-merge.git] / net / socket / tcp_socket_win.cc
blob0c3d26fa5df9169e921501d1b519429f1bca32ed
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"
8 #include <mstcpip.h>
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "base/metrics/stats_counters.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/win/windows_version.h"
15 #include "net/base/address_list.h"
16 #include "net/base/connection_type_histograms.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h"
20 #include "net/base/net_util.h"
21 #include "net/base/network_activity_monitor.h"
22 #include "net/base/network_change_notifier.h"
23 #include "net/base/winsock_init.h"
24 #include "net/base/winsock_util.h"
25 #include "net/socket/socket_descriptor.h"
26 #include "net/socket/socket_net_log_params.h"
28 namespace net {
30 namespace {
32 const int kTCPKeepAliveSeconds = 45;
34 int SetSocketReceiveBufferSize(SOCKET socket, int32 size) {
35 int rv = setsockopt(socket, SOL_SOCKET, SO_RCVBUF,
36 reinterpret_cast<const char*>(&size), sizeof(size));
37 int net_error = (rv == 0) ? OK : MapSystemError(WSAGetLastError());
38 DCHECK(!rv) << "Could not set socket receive buffer size: " << net_error;
39 return net_error;
42 int SetSocketSendBufferSize(SOCKET socket, int32 size) {
43 int rv = setsockopt(socket, SOL_SOCKET, SO_SNDBUF,
44 reinterpret_cast<const char*>(&size), sizeof(size));
45 int net_error = (rv == 0) ? OK : MapSystemError(WSAGetLastError());
46 DCHECK(!rv) << "Could not set socket send buffer size: " << net_error;
47 return net_error;
50 // Disable Nagle.
51 // The Nagle implementation on windows is governed by RFC 896. The idea
52 // behind Nagle is to reduce small packets on the network. When Nagle is
53 // enabled, if a partial packet has been sent, the TCP stack will disallow
54 // further *partial* packets until an ACK has been received from the other
55 // side. Good applications should always strive to send as much data as
56 // possible and avoid partial-packet sends. However, in most real world
57 // applications, there are edge cases where this does not happen, and two
58 // partial packets may be sent back to back. For a browser, it is NEVER
59 // a benefit to delay for an RTT before the second packet is sent.
61 // As a practical example in Chromium today, consider the case of a small
62 // POST. I have verified this:
63 // Client writes 649 bytes of header (partial packet #1)
64 // Client writes 50 bytes of POST data (partial packet #2)
65 // In the above example, with Nagle, a RTT delay is inserted between these
66 // two sends due to nagle. RTTs can easily be 100ms or more. The best
67 // fix is to make sure that for POSTing data, we write as much data as
68 // possible and minimize partial packets. We will fix that. But disabling
69 // Nagle also ensure we don't run into this delay in other edge cases.
70 // See also:
71 // http://technet.microsoft.com/en-us/library/bb726981.aspx
72 bool DisableNagle(SOCKET socket, bool disable) {
73 BOOL val = disable ? TRUE : FALSE;
74 int rv = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY,
75 reinterpret_cast<const char*>(&val),
76 sizeof(val));
77 DCHECK(!rv) << "Could not disable nagle";
78 return rv == 0;
81 // Enable TCP Keep-Alive to prevent NAT routers from timing out TCP
82 // connections. See http://crbug.com/27400 for details.
83 bool SetTCPKeepAlive(SOCKET socket, BOOL enable, int delay_secs) {
84 int delay = delay_secs * 1000;
85 struct tcp_keepalive keepalive_vals = {
86 enable ? 1 : 0, // TCP keep-alive on.
87 delay, // Delay seconds before sending first TCP keep-alive packet.
88 delay, // Delay seconds between sending TCP keep-alive packets.
90 DWORD bytes_returned = 0xABAB;
91 int rv = WSAIoctl(socket, SIO_KEEPALIVE_VALS, &keepalive_vals,
92 sizeof(keepalive_vals), NULL, 0,
93 &bytes_returned, NULL, NULL);
94 DCHECK(!rv) << "Could not enable TCP Keep-Alive for socket: " << socket
95 << " [error: " << WSAGetLastError() << "].";
97 // Disregard any failure in disabling nagle or enabling TCP Keep-Alive.
98 return rv == 0;
101 int MapConnectError(int os_error) {
102 switch (os_error) {
103 // connect fails with WSAEACCES when Windows Firewall blocks the
104 // connection.
105 case WSAEACCES:
106 return ERR_NETWORK_ACCESS_DENIED;
107 case WSAETIMEDOUT:
108 return ERR_CONNECTION_TIMED_OUT;
109 default: {
110 int net_error = MapSystemError(os_error);
111 if (net_error == ERR_FAILED)
112 return ERR_CONNECTION_FAILED; // More specific than ERR_FAILED.
114 // Give a more specific error when the user is offline.
115 if (net_error == ERR_ADDRESS_UNREACHABLE &&
116 NetworkChangeNotifier::IsOffline()) {
117 return ERR_INTERNET_DISCONNECTED;
120 return net_error;
125 } // namespace
127 //-----------------------------------------------------------------------------
129 // Nothing to do for Windows since it doesn't support TCP FastOpen.
130 // TODO(jri): Remove these along with the corresponding global variables.
131 bool IsTCPFastOpenSupported() { return false; }
132 bool IsTCPFastOpenUserEnabled() { return false; }
133 void CheckSupportAndMaybeEnableTCPFastOpen(bool user_enabled) {}
135 // This class encapsulates all the state that has to be preserved as long as
136 // there is a network IO operation in progress. If the owner TCPSocketWin is
137 // destroyed while an operation is in progress, the Core is detached and it
138 // lives until the operation completes and the OS doesn't reference any resource
139 // declared on this class anymore.
140 class TCPSocketWin::Core : public base::RefCounted<Core> {
141 public:
142 explicit Core(TCPSocketWin* socket);
144 // Start watching for the end of a read or write operation.
145 void WatchForRead();
146 void WatchForWrite();
148 // The TCPSocketWin is going away.
149 void Detach() { socket_ = NULL; }
151 // The separate OVERLAPPED variables for asynchronous operation.
152 // |read_overlapped_| is used for both Connect() and Read().
153 // |write_overlapped_| is only used for Write();
154 OVERLAPPED read_overlapped_;
155 OVERLAPPED write_overlapped_;
157 // The buffers used in Read() and Write().
158 scoped_refptr<IOBuffer> read_iobuffer_;
159 scoped_refptr<IOBuffer> write_iobuffer_;
160 int read_buffer_length_;
161 int write_buffer_length_;
163 bool non_blocking_reads_initialized_;
165 private:
166 friend class base::RefCounted<Core>;
168 class ReadDelegate : public base::win::ObjectWatcher::Delegate {
169 public:
170 explicit ReadDelegate(Core* core) : core_(core) {}
171 virtual ~ReadDelegate() {}
173 // base::ObjectWatcher::Delegate methods:
174 virtual void OnObjectSignaled(HANDLE object);
176 private:
177 Core* const core_;
180 class WriteDelegate : public base::win::ObjectWatcher::Delegate {
181 public:
182 explicit WriteDelegate(Core* core) : core_(core) {}
183 virtual ~WriteDelegate() {}
185 // base::ObjectWatcher::Delegate methods:
186 virtual void OnObjectSignaled(HANDLE object);
188 private:
189 Core* const core_;
192 ~Core();
194 // The socket that created this object.
195 TCPSocketWin* socket_;
197 // |reader_| handles the signals from |read_watcher_|.
198 ReadDelegate reader_;
199 // |writer_| handles the signals from |write_watcher_|.
200 WriteDelegate writer_;
202 // |read_watcher_| watches for events from Connect() and Read().
203 base::win::ObjectWatcher read_watcher_;
204 // |write_watcher_| watches for events from Write();
205 base::win::ObjectWatcher write_watcher_;
207 DISALLOW_COPY_AND_ASSIGN(Core);
210 TCPSocketWin::Core::Core(TCPSocketWin* socket)
211 : read_buffer_length_(0),
212 write_buffer_length_(0),
213 non_blocking_reads_initialized_(false),
214 socket_(socket),
215 reader_(this),
216 writer_(this) {
217 memset(&read_overlapped_, 0, sizeof(read_overlapped_));
218 memset(&write_overlapped_, 0, sizeof(write_overlapped_));
220 read_overlapped_.hEvent = WSACreateEvent();
221 write_overlapped_.hEvent = WSACreateEvent();
224 TCPSocketWin::Core::~Core() {
225 // Make sure the message loop is not watching this object anymore.
226 read_watcher_.StopWatching();
227 write_watcher_.StopWatching();
229 WSACloseEvent(read_overlapped_.hEvent);
230 memset(&read_overlapped_, 0xaf, sizeof(read_overlapped_));
231 WSACloseEvent(write_overlapped_.hEvent);
232 memset(&write_overlapped_, 0xaf, sizeof(write_overlapped_));
235 void TCPSocketWin::Core::WatchForRead() {
236 // We grab an extra reference because there is an IO operation in progress.
237 // Balanced in ReadDelegate::OnObjectSignaled().
238 AddRef();
239 read_watcher_.StartWatching(read_overlapped_.hEvent, &reader_);
242 void TCPSocketWin::Core::WatchForWrite() {
243 // We grab an extra reference because there is an IO operation in progress.
244 // Balanced in WriteDelegate::OnObjectSignaled().
245 AddRef();
246 write_watcher_.StartWatching(write_overlapped_.hEvent, &writer_);
249 void TCPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object) {
250 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed.
251 tracked_objects::ScopedTracker tracking_profile(
252 FROM_HERE_WITH_EXPLICIT_FUNCTION(
253 "TCPSocketWin_Core_ReadDelegate_OnObjectSignaled"));
255 DCHECK_EQ(object, core_->read_overlapped_.hEvent);
256 if (core_->socket_) {
257 if (core_->socket_->waiting_connect_)
258 core_->socket_->DidCompleteConnect();
259 else
260 core_->socket_->DidSignalRead();
263 core_->Release();
266 void TCPSocketWin::Core::WriteDelegate::OnObjectSignaled(
267 HANDLE object) {
268 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed.
269 tracked_objects::ScopedTracker tracking_profile(
270 FROM_HERE_WITH_EXPLICIT_FUNCTION(
271 "TCPSocketWin_Core_WriteDelegate_OnObjectSignaled"));
273 DCHECK_EQ(object, core_->write_overlapped_.hEvent);
274 if (core_->socket_)
275 core_->socket_->DidCompleteWrite();
277 core_->Release();
280 //-----------------------------------------------------------------------------
282 TCPSocketWin::TCPSocketWin(net::NetLog* net_log,
283 const net::NetLog::Source& source)
284 : socket_(INVALID_SOCKET),
285 accept_event_(WSA_INVALID_EVENT),
286 accept_socket_(NULL),
287 accept_address_(NULL),
288 waiting_connect_(false),
289 waiting_read_(false),
290 waiting_write_(false),
291 connect_os_error_(0),
292 logging_multiple_connect_attempts_(false),
293 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
294 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
295 source.ToEventParametersCallback());
296 EnsureWinsockInit();
299 TCPSocketWin::~TCPSocketWin() {
300 Close();
301 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
304 int TCPSocketWin::Open(AddressFamily family) {
305 DCHECK(CalledOnValidThread());
306 DCHECK_EQ(socket_, INVALID_SOCKET);
308 socket_ = CreatePlatformSocket(ConvertAddressFamily(family), SOCK_STREAM,
309 IPPROTO_TCP);
310 if (socket_ == INVALID_SOCKET) {
311 PLOG(ERROR) << "CreatePlatformSocket() returned an error";
312 return MapSystemError(WSAGetLastError());
315 if (SetNonBlocking(socket_)) {
316 int result = MapSystemError(WSAGetLastError());
317 Close();
318 return result;
321 return OK;
324 int TCPSocketWin::AdoptConnectedSocket(SOCKET socket,
325 const IPEndPoint& peer_address) {
326 DCHECK(CalledOnValidThread());
327 DCHECK_EQ(socket_, INVALID_SOCKET);
328 DCHECK(!core_.get());
330 socket_ = socket;
332 if (SetNonBlocking(socket_)) {
333 int result = MapSystemError(WSAGetLastError());
334 Close();
335 return result;
338 core_ = new Core(this);
339 peer_address_.reset(new IPEndPoint(peer_address));
341 return OK;
344 int TCPSocketWin::AdoptListenSocket(SOCKET socket) {
345 DCHECK(CalledOnValidThread());
346 DCHECK_EQ(socket_, INVALID_SOCKET);
348 socket_ = socket;
350 if (SetNonBlocking(socket_)) {
351 int result = MapSystemError(WSAGetLastError());
352 Close();
353 return result;
356 // |core_| is not needed for sockets that are used to accept connections.
357 // The operation here is more like Open but with an existing socket.
359 return OK;
362 int TCPSocketWin::Bind(const IPEndPoint& address) {
363 DCHECK(CalledOnValidThread());
364 DCHECK_NE(socket_, INVALID_SOCKET);
366 SockaddrStorage storage;
367 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
368 return ERR_ADDRESS_INVALID;
370 int result = bind(socket_, storage.addr, storage.addr_len);
371 if (result < 0) {
372 PLOG(ERROR) << "bind() returned an error";
373 return MapSystemError(WSAGetLastError());
376 return OK;
379 int TCPSocketWin::Listen(int backlog) {
380 DCHECK(CalledOnValidThread());
381 DCHECK_GT(backlog, 0);
382 DCHECK_NE(socket_, INVALID_SOCKET);
383 DCHECK_EQ(accept_event_, WSA_INVALID_EVENT);
385 accept_event_ = WSACreateEvent();
386 if (accept_event_ == WSA_INVALID_EVENT) {
387 PLOG(ERROR) << "WSACreateEvent()";
388 return MapSystemError(WSAGetLastError());
391 int result = listen(socket_, backlog);
392 if (result < 0) {
393 PLOG(ERROR) << "listen() returned an error";
394 return MapSystemError(WSAGetLastError());
397 return OK;
400 int TCPSocketWin::Accept(scoped_ptr<TCPSocketWin>* socket,
401 IPEndPoint* address,
402 const CompletionCallback& callback) {
403 DCHECK(CalledOnValidThread());
404 DCHECK(socket);
405 DCHECK(address);
406 DCHECK(!callback.is_null());
407 DCHECK(accept_callback_.is_null());
409 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
411 int result = AcceptInternal(socket, address);
413 if (result == ERR_IO_PENDING) {
414 // Start watching.
415 WSAEventSelect(socket_, accept_event_, FD_ACCEPT);
416 accept_watcher_.StartWatching(accept_event_, this);
418 accept_socket_ = socket;
419 accept_address_ = address;
420 accept_callback_ = callback;
423 return result;
426 int TCPSocketWin::Connect(const IPEndPoint& address,
427 const CompletionCallback& callback) {
428 DCHECK(CalledOnValidThread());
429 DCHECK_NE(socket_, INVALID_SOCKET);
430 DCHECK(!waiting_connect_);
432 // |peer_address_| and |core_| will be non-NULL if Connect() has been called.
433 // Unless Close() is called to reset the internal state, a second call to
434 // Connect() is not allowed.
435 // Please note that we enforce this even if the previous Connect() has
436 // completed and failed. Although it is allowed to connect the same |socket_|
437 // again after a connection attempt failed on Windows, it results in
438 // unspecified behavior according to POSIX. Therefore, we make it behave in
439 // the same way as TCPSocketLibevent.
440 DCHECK(!peer_address_ && !core_.get());
442 if (!logging_multiple_connect_attempts_)
443 LogConnectBegin(AddressList(address));
445 peer_address_.reset(new IPEndPoint(address));
447 int rv = DoConnect();
448 if (rv == ERR_IO_PENDING) {
449 // Synchronous operation not supported.
450 DCHECK(!callback.is_null());
451 read_callback_ = callback;
452 waiting_connect_ = true;
453 } else {
454 DoConnectComplete(rv);
457 return rv;
460 bool TCPSocketWin::IsConnected() const {
461 DCHECK(CalledOnValidThread());
463 if (socket_ == INVALID_SOCKET || waiting_connect_)
464 return false;
466 if (waiting_read_)
467 return true;
469 // Check if connection is alive.
470 char c;
471 int rv = recv(socket_, &c, 1, MSG_PEEK);
472 if (rv == 0)
473 return false;
474 if (rv == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK)
475 return false;
477 return true;
480 bool TCPSocketWin::IsConnectedAndIdle() const {
481 DCHECK(CalledOnValidThread());
483 if (socket_ == INVALID_SOCKET || waiting_connect_)
484 return false;
486 if (waiting_read_)
487 return true;
489 // Check if connection is alive and we haven't received any data
490 // unexpectedly.
491 char c;
492 int rv = recv(socket_, &c, 1, MSG_PEEK);
493 if (rv >= 0)
494 return false;
495 if (WSAGetLastError() != WSAEWOULDBLOCK)
496 return false;
498 return true;
501 int TCPSocketWin::Read(IOBuffer* buf,
502 int buf_len,
503 const CompletionCallback& callback) {
504 DCHECK(CalledOnValidThread());
505 DCHECK_NE(socket_, INVALID_SOCKET);
506 DCHECK(!waiting_read_);
507 CHECK(read_callback_.is_null());
508 DCHECK(!core_->read_iobuffer_.get());
510 return DoRead(buf, buf_len, callback);
513 int TCPSocketWin::Write(IOBuffer* buf,
514 int buf_len,
515 const CompletionCallback& callback) {
516 DCHECK(CalledOnValidThread());
517 DCHECK_NE(socket_, INVALID_SOCKET);
518 DCHECK(!waiting_write_);
519 CHECK(write_callback_.is_null());
520 DCHECK_GT(buf_len, 0);
521 DCHECK(!core_->write_iobuffer_.get());
523 base::StatsCounter writes("tcp.writes");
524 writes.Increment();
526 WSABUF write_buffer;
527 write_buffer.len = buf_len;
528 write_buffer.buf = buf->data();
530 // TODO(wtc): Remove the assertion after enough testing.
531 AssertEventNotSignaled(core_->write_overlapped_.hEvent);
532 DWORD num;
533 int rv = WSASend(socket_, &write_buffer, 1, &num, 0,
534 &core_->write_overlapped_, NULL);
535 if (rv == 0) {
536 if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) {
537 rv = static_cast<int>(num);
538 if (rv > buf_len || rv < 0) {
539 // It seems that some winsock interceptors report that more was written
540 // than was available. Treat this as an error. http://crbug.com/27870
541 LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len
542 << " bytes, but " << rv << " bytes reported.";
543 return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES;
545 base::StatsCounter write_bytes("tcp.write_bytes");
546 write_bytes.Add(rv);
547 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, rv,
548 buf->data());
549 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(rv);
550 return rv;
552 } else {
553 int os_error = WSAGetLastError();
554 if (os_error != WSA_IO_PENDING) {
555 int net_error = MapSystemError(os_error);
556 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR,
557 CreateNetLogSocketErrorCallback(net_error, os_error));
558 return net_error;
561 waiting_write_ = true;
562 write_callback_ = callback;
563 core_->write_iobuffer_ = buf;
564 core_->write_buffer_length_ = buf_len;
565 core_->WatchForWrite();
566 return ERR_IO_PENDING;
569 int TCPSocketWin::GetLocalAddress(IPEndPoint* address) const {
570 DCHECK(CalledOnValidThread());
571 DCHECK(address);
573 SockaddrStorage storage;
574 if (getsockname(socket_, storage.addr, &storage.addr_len))
575 return MapSystemError(WSAGetLastError());
576 if (!address->FromSockAddr(storage.addr, storage.addr_len))
577 return ERR_ADDRESS_INVALID;
579 return OK;
582 int TCPSocketWin::GetPeerAddress(IPEndPoint* address) const {
583 DCHECK(CalledOnValidThread());
584 DCHECK(address);
585 if (!IsConnected())
586 return ERR_SOCKET_NOT_CONNECTED;
587 *address = *peer_address_;
588 return OK;
591 int TCPSocketWin::SetDefaultOptionsForServer() {
592 return SetExclusiveAddrUse();
595 void TCPSocketWin::SetDefaultOptionsForClient() {
596 // Increase the socket buffer sizes from the default sizes for WinXP. In
597 // performance testing, there is substantial benefit by increasing from 8KB
598 // to 64KB.
599 // See also:
600 // http://support.microsoft.com/kb/823764/EN-US
601 // On Vista, if we manually set these sizes, Vista turns off its receive
602 // window auto-tuning feature.
603 // http://blogs.msdn.com/wndp/archive/2006/05/05/Winhec-blog-tcpip-2.aspx
604 // Since Vista's auto-tune is better than any static value we can could set,
605 // only change these on pre-vista machines.
606 if (base::win::GetVersion() < base::win::VERSION_VISTA) {
607 const int32 kSocketBufferSize = 64 * 1024;
608 SetSocketReceiveBufferSize(socket_, kSocketBufferSize);
609 SetSocketSendBufferSize(socket_, kSocketBufferSize);
612 DisableNagle(socket_, true);
613 SetTCPKeepAlive(socket_, true, kTCPKeepAliveSeconds);
616 int TCPSocketWin::SetExclusiveAddrUse() {
617 // On Windows, a bound end point can be hijacked by another process by
618 // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE
619 // was introduced in Windows NT 4.0 SP4. If the socket that is bound to the
620 // end point has SO_EXCLUSIVEADDRUSE enabled, it is not possible for another
621 // socket to forcibly bind to the end point until the end point is unbound.
622 // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
623 // MSDN: http://goo.gl/M6fjQ.
625 // Unlike on *nix, on Windows a TCP server socket can always bind to an end
626 // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
627 // needed here.
629 // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end
630 // point in TIME_WAIT status. It does not have this effect for a TCP server
631 // socket.
633 BOOL true_value = 1;
634 int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
635 reinterpret_cast<const char*>(&true_value),
636 sizeof(true_value));
637 if (rv < 0)
638 return MapSystemError(errno);
639 return OK;
642 int TCPSocketWin::SetReceiveBufferSize(int32 size) {
643 DCHECK(CalledOnValidThread());
644 return SetSocketReceiveBufferSize(socket_, size);
647 int TCPSocketWin::SetSendBufferSize(int32 size) {
648 DCHECK(CalledOnValidThread());
649 return SetSocketSendBufferSize(socket_, size);
652 bool TCPSocketWin::SetKeepAlive(bool enable, int delay) {
653 return SetTCPKeepAlive(socket_, enable, delay);
656 bool TCPSocketWin::SetNoDelay(bool no_delay) {
657 return DisableNagle(socket_, no_delay);
660 void TCPSocketWin::Close() {
661 DCHECK(CalledOnValidThread());
663 if (socket_ != INVALID_SOCKET) {
664 // Only log the close event if there's actually a socket to close.
665 net_log_.AddEvent(NetLog::EventType::TYPE_SOCKET_CLOSED);
667 // Note: don't use CancelIo to cancel pending IO because it doesn't work
668 // when there is a Winsock layered service provider.
670 // In most socket implementations, closing a socket results in a graceful
671 // connection shutdown, but in Winsock we have to call shutdown explicitly.
672 // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure"
673 // at http://msdn.microsoft.com/en-us/library/ms738547.aspx
674 shutdown(socket_, SD_SEND);
676 // This cancels any pending IO.
677 if (closesocket(socket_) < 0)
678 PLOG(ERROR) << "closesocket";
679 socket_ = INVALID_SOCKET;
682 if (!accept_callback_.is_null()) {
683 accept_watcher_.StopWatching();
684 accept_socket_ = NULL;
685 accept_address_ = NULL;
686 accept_callback_.Reset();
689 if (accept_event_) {
690 WSACloseEvent(accept_event_);
691 accept_event_ = WSA_INVALID_EVENT;
694 if (core_.get()) {
695 if (waiting_connect_) {
696 // We closed the socket, so this notification will never come.
697 // From MSDN' WSAEventSelect documentation:
698 // "Closing a socket with closesocket also cancels the association and
699 // selection of network events specified in WSAEventSelect for the
700 // socket".
701 core_->Release();
703 core_->Detach();
704 core_ = NULL;
707 waiting_connect_ = false;
708 waiting_read_ = false;
709 waiting_write_ = false;
711 read_callback_.Reset();
712 write_callback_.Reset();
713 peer_address_.reset();
714 connect_os_error_ = 0;
717 void TCPSocketWin::StartLoggingMultipleConnectAttempts(
718 const AddressList& addresses) {
719 if (!logging_multiple_connect_attempts_) {
720 logging_multiple_connect_attempts_ = true;
721 LogConnectBegin(addresses);
722 } else {
723 NOTREACHED();
727 void TCPSocketWin::EndLoggingMultipleConnectAttempts(int net_error) {
728 if (logging_multiple_connect_attempts_) {
729 LogConnectEnd(net_error);
730 logging_multiple_connect_attempts_ = false;
731 } else {
732 NOTREACHED();
736 int TCPSocketWin::AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
737 IPEndPoint* address) {
738 SockaddrStorage storage;
739 int new_socket = accept(socket_, storage.addr, &storage.addr_len);
740 if (new_socket < 0) {
741 int net_error = MapSystemError(WSAGetLastError());
742 if (net_error != ERR_IO_PENDING)
743 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
744 return net_error;
747 IPEndPoint ip_end_point;
748 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
749 NOTREACHED();
750 if (closesocket(new_socket) < 0)
751 PLOG(ERROR) << "closesocket";
752 int net_error = ERR_ADDRESS_INVALID;
753 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
754 return net_error;
756 scoped_ptr<TCPSocketWin> tcp_socket(new TCPSocketWin(
757 net_log_.net_log(), net_log_.source()));
758 int adopt_result = tcp_socket->AdoptConnectedSocket(new_socket, ip_end_point);
759 if (adopt_result != OK) {
760 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
761 return adopt_result;
763 *socket = tcp_socket.Pass();
764 *address = ip_end_point;
765 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
766 CreateNetLogIPEndPointCallback(&ip_end_point));
767 return OK;
770 void TCPSocketWin::OnObjectSignaled(HANDLE object) {
771 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed.
772 tracked_objects::ScopedTracker tracking_profile(
773 FROM_HERE_WITH_EXPLICIT_FUNCTION("TCPSocketWin_OnObjectSignaled"));
775 WSANETWORKEVENTS ev;
776 if (WSAEnumNetworkEvents(socket_, accept_event_, &ev) == SOCKET_ERROR) {
777 PLOG(ERROR) << "WSAEnumNetworkEvents()";
778 return;
781 if (ev.lNetworkEvents & FD_ACCEPT) {
782 int result = AcceptInternal(accept_socket_, accept_address_);
783 if (result != ERR_IO_PENDING) {
784 accept_socket_ = NULL;
785 accept_address_ = NULL;
786 base::ResetAndReturn(&accept_callback_).Run(result);
788 } else {
789 // This happens when a client opens a connection and closes it before we
790 // have a chance to accept it.
791 DCHECK(ev.lNetworkEvents == 0);
793 // Start watching the next FD_ACCEPT event.
794 WSAEventSelect(socket_, accept_event_, FD_ACCEPT);
795 accept_watcher_.StartWatching(accept_event_, this);
799 int TCPSocketWin::DoConnect() {
800 DCHECK_EQ(connect_os_error_, 0);
801 DCHECK(!core_.get());
803 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
804 CreateNetLogIPEndPointCallback(peer_address_.get()));
806 core_ = new Core(this);
807 // WSAEventSelect sets the socket to non-blocking mode as a side effect.
808 // Our connect() and recv() calls require that the socket be non-blocking.
809 WSAEventSelect(socket_, core_->read_overlapped_.hEvent, FD_CONNECT);
811 SockaddrStorage storage;
812 if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len))
813 return ERR_ADDRESS_INVALID;
814 if (!connect(socket_, storage.addr, storage.addr_len)) {
815 // Connected without waiting!
817 // The MSDN page for connect says:
818 // With a nonblocking socket, the connection attempt cannot be completed
819 // immediately. In this case, connect will return SOCKET_ERROR, and
820 // WSAGetLastError will return WSAEWOULDBLOCK.
821 // which implies that for a nonblocking socket, connect never returns 0.
822 // It's not documented whether the event object will be signaled or not
823 // if connect does return 0. So the code below is essentially dead code
824 // and we don't know if it's correct.
825 NOTREACHED();
827 if (ResetEventIfSignaled(core_->read_overlapped_.hEvent))
828 return OK;
829 } else {
830 int os_error = WSAGetLastError();
831 if (os_error != WSAEWOULDBLOCK) {
832 LOG(ERROR) << "connect failed: " << os_error;
833 connect_os_error_ = os_error;
834 int rv = MapConnectError(os_error);
835 CHECK_NE(ERR_IO_PENDING, rv);
836 return rv;
840 core_->WatchForRead();
841 return ERR_IO_PENDING;
844 void TCPSocketWin::DoConnectComplete(int result) {
845 // Log the end of this attempt (and any OS error it threw).
846 int os_error = connect_os_error_;
847 connect_os_error_ = 0;
848 if (result != OK) {
849 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT,
850 NetLog::IntegerCallback("os_error", os_error));
851 } else {
852 net_log_.EndEvent(NetLog::TYPE_TCP_CONNECT_ATTEMPT);
855 if (!logging_multiple_connect_attempts_)
856 LogConnectEnd(result);
859 void TCPSocketWin::LogConnectBegin(const AddressList& addresses) {
860 base::StatsCounter connects("tcp.connect");
861 connects.Increment();
863 net_log_.BeginEvent(NetLog::TYPE_TCP_CONNECT,
864 addresses.CreateNetLogCallback());
867 void TCPSocketWin::LogConnectEnd(int net_error) {
868 if (net_error == OK)
869 UpdateConnectionTypeHistograms(CONNECTION_ANY);
871 if (net_error != OK) {
872 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, net_error);
873 return;
876 struct sockaddr_storage source_address;
877 socklen_t addrlen = sizeof(source_address);
878 int rv = getsockname(
879 socket_, reinterpret_cast<struct sockaddr*>(&source_address), &addrlen);
880 if (rv != 0) {
881 LOG(ERROR) << "getsockname() [rv: " << rv
882 << "] error: " << WSAGetLastError();
883 NOTREACHED();
884 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_CONNECT, rv);
885 return;
888 net_log_.EndEvent(
889 NetLog::TYPE_TCP_CONNECT,
890 CreateNetLogSourceAddressCallback(
891 reinterpret_cast<const struct sockaddr*>(&source_address),
892 sizeof(source_address)));
895 int TCPSocketWin::DoRead(IOBuffer* buf, int buf_len,
896 const CompletionCallback& callback) {
897 if (!core_->non_blocking_reads_initialized_) {
898 WSAEventSelect(socket_, core_->read_overlapped_.hEvent,
899 FD_READ | FD_CLOSE);
900 core_->non_blocking_reads_initialized_ = true;
902 int rv = recv(socket_, buf->data(), buf_len, 0);
903 if (rv == SOCKET_ERROR) {
904 int os_error = WSAGetLastError();
905 if (os_error != WSAEWOULDBLOCK) {
906 int net_error = MapSystemError(os_error);
907 net_log_.AddEvent(
908 NetLog::TYPE_SOCKET_READ_ERROR,
909 CreateNetLogSocketErrorCallback(net_error, os_error));
910 return net_error;
912 } else {
913 base::StatsCounter read_bytes("tcp.read_bytes");
914 if (rv > 0)
915 read_bytes.Add(rv);
916 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_RECEIVED, rv,
917 buf->data());
918 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(rv);
919 return rv;
922 waiting_read_ = true;
923 read_callback_ = callback;
924 core_->read_iobuffer_ = buf;
925 core_->read_buffer_length_ = buf_len;
926 core_->WatchForRead();
927 return ERR_IO_PENDING;
930 void TCPSocketWin::DidCompleteConnect() {
931 DCHECK(waiting_connect_);
932 DCHECK(!read_callback_.is_null());
933 int result;
935 WSANETWORKEVENTS events;
936 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent,
937 &events);
938 int os_error = 0;
939 if (rv == SOCKET_ERROR) {
940 NOTREACHED();
941 os_error = WSAGetLastError();
942 result = MapSystemError(os_error);
943 } else if (events.lNetworkEvents & FD_CONNECT) {
944 os_error = events.iErrorCode[FD_CONNECT_BIT];
945 result = MapConnectError(os_error);
946 } else {
947 NOTREACHED();
948 result = ERR_UNEXPECTED;
951 connect_os_error_ = os_error;
952 DoConnectComplete(result);
953 waiting_connect_ = false;
955 DCHECK_NE(result, ERR_IO_PENDING);
956 base::ResetAndReturn(&read_callback_).Run(result);
959 void TCPSocketWin::DidCompleteWrite() {
960 DCHECK(waiting_write_);
961 DCHECK(!write_callback_.is_null());
963 DWORD num_bytes, flags;
964 BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_,
965 &num_bytes, FALSE, &flags);
966 WSAResetEvent(core_->write_overlapped_.hEvent);
967 waiting_write_ = false;
968 int rv;
969 if (!ok) {
970 int os_error = WSAGetLastError();
971 rv = MapSystemError(os_error);
972 net_log_.AddEvent(NetLog::TYPE_SOCKET_WRITE_ERROR,
973 CreateNetLogSocketErrorCallback(rv, os_error));
974 } else {
975 rv = static_cast<int>(num_bytes);
976 if (rv > core_->write_buffer_length_ || rv < 0) {
977 // It seems that some winsock interceptors report that more was written
978 // than was available. Treat this as an error. http://crbug.com/27870
979 LOG(ERROR) << "Detected broken LSP: Asked to write "
980 << core_->write_buffer_length_ << " bytes, but " << rv
981 << " bytes reported.";
982 rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES;
983 } else {
984 base::StatsCounter write_bytes("tcp.write_bytes");
985 write_bytes.Add(num_bytes);
986 net_log_.AddByteTransferEvent(NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes,
987 core_->write_iobuffer_->data());
988 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(num_bytes);
992 core_->write_iobuffer_ = NULL;
994 DCHECK_NE(rv, ERR_IO_PENDING);
995 base::ResetAndReturn(&write_callback_).Run(rv);
998 void TCPSocketWin::DidSignalRead() {
999 DCHECK(waiting_read_);
1000 DCHECK(!read_callback_.is_null());
1002 int os_error = 0;
1003 WSANETWORKEVENTS network_events;
1004 int rv = WSAEnumNetworkEvents(socket_, core_->read_overlapped_.hEvent,
1005 &network_events);
1006 if (rv == SOCKET_ERROR) {
1007 os_error = WSAGetLastError();
1008 rv = MapSystemError(os_error);
1009 } else if (network_events.lNetworkEvents) {
1010 DCHECK_EQ(network_events.lNetworkEvents & ~(FD_READ | FD_CLOSE), 0);
1011 // If network_events.lNetworkEvents is FD_CLOSE and
1012 // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful
1013 // connection closure. It is tempting to directly set rv to 0 in
1014 // this case, but the MSDN pages for WSAEventSelect and
1015 // WSAAsyncSelect recommend we still call DoRead():
1016 // FD_CLOSE should only be posted after all data is read from a
1017 // socket, but an application should check for remaining data upon
1018 // receipt of FD_CLOSE to avoid any possibility of losing data.
1020 // If network_events.iErrorCode[FD_READ_BIT] or
1021 // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call
1022 // DoRead() because recv() reports a more accurate error code
1023 // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was
1024 // reset.
1025 rv = DoRead(core_->read_iobuffer_.get(), core_->read_buffer_length_,
1026 read_callback_);
1027 if (rv == ERR_IO_PENDING)
1028 return;
1029 } else {
1030 // This may happen because Read() may succeed synchronously and
1031 // consume all the received data without resetting the event object.
1032 core_->WatchForRead();
1033 return;
1036 waiting_read_ = false;
1037 core_->read_iobuffer_ = NULL;
1038 core_->read_buffer_length_ = 0;
1040 DCHECK_NE(rv, ERR_IO_PENDING);
1041 // TODO(vadimt): Remove ScopedTracker below once crbug.com/418183 is fixed.
1042 tracked_objects::ScopedTracker tracking_profile(
1043 FROM_HERE_WITH_EXPLICIT_FUNCTION("TCPSocketWin::DidSignalRead"));
1044 base::ResetAndReturn(&read_callback_).Run(rv);
1047 } // namespace net