xref: /aosp_15_r20/external/cronet/net/socket/tcp_socket_win.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/socket/tcp_socket.h"
6 #include "net/socket/tcp_socket_win.h"
7 
8 #include <errno.h>
9 #include <mstcpip.h>
10 
11 #include <memory>
12 #include <utility>
13 
14 #include "base/check_op.h"
15 #include "base/files/file_util.h"
16 #include "base/functional/bind.h"
17 #include "base/functional/callback_helpers.h"
18 #include "base/logging.h"
19 #include "base/memory/raw_ptr.h"
20 #include "net/base/address_list.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/ip_endpoint.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/network_activity_monitor.h"
25 #include "net/base/network_change_notifier.h"
26 #include "net/base/sockaddr_storage.h"
27 #include "net/base/winsock_init.h"
28 #include "net/base/winsock_util.h"
29 #include "net/log/net_log.h"
30 #include "net/log/net_log_event_type.h"
31 #include "net/log/net_log_source.h"
32 #include "net/log/net_log_source_type.h"
33 #include "net/log/net_log_values.h"
34 #include "net/socket/socket_descriptor.h"
35 #include "net/socket/socket_net_log_params.h"
36 #include "net/socket/socket_options.h"
37 #include "net/socket/socket_tag.h"
38 
39 namespace net {
40 
41 namespace {
42 
43 const int kTCPKeepAliveSeconds = 45;
44 
45 // Disable Nagle.
46 // Enable TCP Keep-Alive to prevent NAT routers from timing out TCP
47 // connections. See http://crbug.com/27400 for details.
SetTCPKeepAlive(SOCKET socket,BOOL enable,int delay_secs)48 bool SetTCPKeepAlive(SOCKET socket, BOOL enable, int delay_secs) {
49   unsigned delay = delay_secs * 1000;
50   struct tcp_keepalive keepalive_vals = {
51       enable ? 1u : 0u,  // TCP keep-alive on.
52       delay,  // Delay seconds before sending first TCP keep-alive packet.
53       delay,  // Delay seconds between sending TCP keep-alive packets.
54   };
55   DWORD bytes_returned = 0xABAB;
56   int rv = WSAIoctl(socket, SIO_KEEPALIVE_VALS, &keepalive_vals,
57                     sizeof(keepalive_vals), nullptr, 0, &bytes_returned,
58                     nullptr, nullptr);
59   int os_error = WSAGetLastError();
60   DCHECK(!rv) << "Could not enable TCP Keep-Alive for socket: " << socket
61               << " [error: " << os_error << "].";
62 
63   // Disregard any failure in disabling nagle or enabling TCP Keep-Alive.
64   return rv == 0;
65 }
66 
MapConnectError(int os_error)67 int MapConnectError(int os_error) {
68   switch (os_error) {
69     // connect fails with WSAEACCES when Windows Firewall blocks the
70     // connection.
71     case WSAEACCES:
72       return ERR_NETWORK_ACCESS_DENIED;
73     case WSAETIMEDOUT:
74       return ERR_CONNECTION_TIMED_OUT;
75     default: {
76       int net_error = MapSystemError(os_error);
77       if (net_error == ERR_FAILED)
78         return ERR_CONNECTION_FAILED;  // More specific than ERR_FAILED.
79 
80       // Give a more specific error when the user is offline.
81       if (net_error == ERR_ADDRESS_UNREACHABLE &&
82           NetworkChangeNotifier::IsOffline()) {
83         return ERR_INTERNET_DISCONNECTED;
84       }
85 
86       return net_error;
87     }
88   }
89 }
90 
SetNonBlockingAndGetError(int fd,int * os_error)91 bool SetNonBlockingAndGetError(int fd, int* os_error) {
92   bool ret = base::SetNonBlocking(fd);
93   *os_error = WSAGetLastError();
94 
95   return ret;
96 }
97 
98 }  // namespace
99 
100 //-----------------------------------------------------------------------------
101 
102 // This class encapsulates all the state that has to be preserved as long as
103 // there is a network IO operation in progress. If the owner TCPSocketWin is
104 // destroyed while an operation is in progress, the Core is detached and it
105 // lives until the operation completes and the OS doesn't reference any resource
106 // declared on this class anymore.
107 class TCPSocketWin::Core : public base::RefCounted<Core> {
108  public:
109   explicit Core(TCPSocketWin* socket);
110 
111   Core(const Core&) = delete;
112   Core& operator=(const Core&) = delete;
113 
114   // Start watching for the end of a read or write operation.
115   void WatchForRead();
116   void WatchForWrite();
117 
118   // Stops watching for read.
119   void StopWatchingForRead();
120 
121   // The TCPSocketWin is going away.
122   void Detach();
123 
124   // Event handle for monitoring connect and read events through WSAEventSelect.
125   HANDLE read_event_;
126 
127   // OVERLAPPED variable for overlapped writes.
128   // TODO(mmenke): Can writes be switched to WSAEventSelect as well? That would
129   // allow removing this class. The only concern is whether that would have a
130   // negative perf impact.
131   OVERLAPPED write_overlapped_;
132 
133   // The buffers used in Read() and Write().
134   scoped_refptr<IOBuffer> read_iobuffer_;
135   scoped_refptr<IOBuffer> write_iobuffer_;
136   int read_buffer_length_ = 0;
137   int write_buffer_length_ = 0;
138 
139   bool non_blocking_reads_initialized_ = false;
140 
141  private:
142   friend class base::RefCounted<Core>;
143 
144   class ReadDelegate : public base::win::ObjectWatcher::Delegate {
145    public:
ReadDelegate(Core * core)146     explicit ReadDelegate(Core* core) : core_(core) {}
147     ~ReadDelegate() override = default;
148 
149     // base::ObjectWatcher::Delegate methods:
150     void OnObjectSignaled(HANDLE object) override;
151 
152    private:
153     const raw_ptr<Core> core_;
154   };
155 
156   class WriteDelegate : public base::win::ObjectWatcher::Delegate {
157    public:
WriteDelegate(Core * core)158     explicit WriteDelegate(Core* core) : core_(core) {}
159     ~WriteDelegate() override = default;
160 
161     // base::ObjectWatcher::Delegate methods:
162     void OnObjectSignaled(HANDLE object) override;
163 
164    private:
165     const raw_ptr<Core> core_;
166   };
167 
168   ~Core();
169 
170   // The socket that created this object.
171   raw_ptr<TCPSocketWin> socket_;
172 
173   // |reader_| handles the signals from |read_watcher_|.
174   ReadDelegate reader_;
175   // |writer_| handles the signals from |write_watcher_|.
176   WriteDelegate writer_;
177 
178   // |read_watcher_| watches for events from Connect() and Read().
179   base::win::ObjectWatcher read_watcher_;
180   // |write_watcher_| watches for events from Write();
181   base::win::ObjectWatcher write_watcher_;
182 };
183 
Core(TCPSocketWin * socket)184 TCPSocketWin::Core::Core(TCPSocketWin* socket)
185     : read_event_(WSACreateEvent()),
186       socket_(socket),
187       reader_(this),
188       writer_(this) {
189   memset(&write_overlapped_, 0, sizeof(write_overlapped_));
190   write_overlapped_.hEvent = WSACreateEvent();
191 }
192 
~Core()193 TCPSocketWin::Core::~Core() {
194   // Detach should already have been called.
195   DCHECK(!socket_);
196 
197   // Stop the write watcher.  The read watcher should already have been stopped
198   // in Detach().
199   write_watcher_.StopWatching();
200   WSACloseEvent(write_overlapped_.hEvent);
201   memset(&write_overlapped_, 0xaf, sizeof(write_overlapped_));
202 }
203 
WatchForRead()204 void TCPSocketWin::Core::WatchForRead() {
205   // Reads use WSAEventSelect, which closesocket() cancels so unlike writes,
206   // there's no need to increment the reference count here.
207   read_watcher_.StartWatchingOnce(read_event_, &reader_);
208 }
209 
WatchForWrite()210 void TCPSocketWin::Core::WatchForWrite() {
211   // We grab an extra reference because there is an IO operation in progress.
212   // Balanced in WriteDelegate::OnObjectSignaled().
213   AddRef();
214   write_watcher_.StartWatchingOnce(write_overlapped_.hEvent, &writer_);
215 }
216 
StopWatchingForRead()217 void TCPSocketWin::Core::StopWatchingForRead() {
218   DCHECK(!socket_->waiting_connect_);
219 
220   read_watcher_.StopWatching();
221 }
222 
Detach()223 void TCPSocketWin::Core::Detach() {
224   // Stop watching the read watcher. A read won't be signalled after the Detach
225   // call, since the socket has been closed, but it's possible the event was
226   // signalled when the socket was closed, but hasn't been handled yet, so need
227   // to stop watching now to avoid trying to handle the event. See
228   // https://crbug.com/831149
229   read_watcher_.StopWatching();
230   WSACloseEvent(read_event_);
231 
232   socket_ = nullptr;
233 }
234 
OnObjectSignaled(HANDLE object)235 void TCPSocketWin::Core::ReadDelegate::OnObjectSignaled(HANDLE object) {
236   DCHECK_EQ(object, core_->read_event_);
237   DCHECK(core_->socket_);
238   if (core_->socket_->waiting_connect_)
239     core_->socket_->DidCompleteConnect();
240   else
241     core_->socket_->DidSignalRead();
242 }
243 
OnObjectSignaled(HANDLE object)244 void TCPSocketWin::Core::WriteDelegate::OnObjectSignaled(
245     HANDLE object) {
246   DCHECK_EQ(object, core_->write_overlapped_.hEvent);
247   if (core_->socket_)
248     core_->socket_->DidCompleteWrite();
249 
250   // Matches the AddRef() in WatchForWrite().
251   core_->Release();
252 }
253 
254 //-----------------------------------------------------------------------------
255 
TCPSocketWin(std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,net::NetLog * net_log,const net::NetLogSource & source)256 TCPSocketWin::TCPSocketWin(
257     std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
258     net::NetLog* net_log,
259     const net::NetLogSource& source)
260     : socket_(INVALID_SOCKET),
261       socket_performance_watcher_(std::move(socket_performance_watcher)),
262       accept_event_(WSA_INVALID_EVENT),
263       net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)) {
264   net_log_.BeginEventReferencingSource(NetLogEventType::SOCKET_ALIVE, source);
265   EnsureWinsockInit();
266 }
267 
TCPSocketWin(std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,NetLogWithSource net_log_source)268 TCPSocketWin::TCPSocketWin(
269     std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
270     NetLogWithSource net_log_source)
271     : socket_(INVALID_SOCKET),
272       socket_performance_watcher_(std::move(socket_performance_watcher)),
273       accept_event_(WSA_INVALID_EVENT),
274       net_log_(net_log_source) {
275   net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE);
276   EnsureWinsockInit();
277 }
278 
~TCPSocketWin()279 TCPSocketWin::~TCPSocketWin() {
280   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
281   Close();
282   net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE);
283 }
284 
Open(AddressFamily family)285 int TCPSocketWin::Open(AddressFamily family) {
286   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
287   DCHECK_EQ(socket_, INVALID_SOCKET);
288 
289   socket_ = CreatePlatformSocket(ConvertAddressFamily(family), SOCK_STREAM,
290                                  IPPROTO_TCP);
291   int os_error = WSAGetLastError();
292   if (socket_ == INVALID_SOCKET) {
293     PLOG(ERROR) << "CreatePlatformSocket() returned an error";
294     return MapSystemError(os_error);
295   }
296 
297   if (!SetNonBlockingAndGetError(socket_, &os_error)) {
298     int result = MapSystemError(os_error);
299     Close();
300     return result;
301   }
302 
303   return OK;
304 }
305 
AdoptConnectedSocket(SocketDescriptor socket,const IPEndPoint & peer_address)306 int TCPSocketWin::AdoptConnectedSocket(SocketDescriptor socket,
307                                        const IPEndPoint& peer_address) {
308   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
309   DCHECK_EQ(socket_, INVALID_SOCKET);
310   DCHECK(!core_.get());
311 
312   socket_ = socket;
313 
314   int os_error;
315   if (!SetNonBlockingAndGetError(socket_, &os_error)) {
316     int result = MapSystemError(os_error);
317     Close();
318     return result;
319   }
320 
321   core_ = base::MakeRefCounted<Core>(this);
322   peer_address_ = std::make_unique<IPEndPoint>(peer_address);
323 
324   return OK;
325 }
326 
AdoptUnconnectedSocket(SocketDescriptor socket)327 int TCPSocketWin::AdoptUnconnectedSocket(SocketDescriptor socket) {
328   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
329   DCHECK_EQ(socket_, INVALID_SOCKET);
330 
331   socket_ = socket;
332 
333   int os_error;
334   if (!SetNonBlockingAndGetError(socket_, &os_error)) {
335     int result = MapSystemError(os_error);
336     Close();
337     return result;
338   }
339 
340   // |core_| is not needed for sockets that are used to accept connections.
341   // The operation here is more like Open but with an existing socket.
342 
343   return OK;
344 }
345 
Bind(const IPEndPoint & address)346 int TCPSocketWin::Bind(const IPEndPoint& address) {
347   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
348   DCHECK_NE(socket_, INVALID_SOCKET);
349 
350   SockaddrStorage storage;
351   if (!address.ToSockAddr(storage.addr, &storage.addr_len))
352     return ERR_ADDRESS_INVALID;
353 
354   int result = bind(socket_, storage.addr, storage.addr_len);
355   int os_error = WSAGetLastError();
356   if (result < 0) {
357     PLOG(ERROR) << "bind() returned an error";
358     return MapSystemError(os_error);
359   }
360 
361   return OK;
362 }
363 
Listen(int backlog)364 int TCPSocketWin::Listen(int backlog) {
365   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
366   DCHECK_GT(backlog, 0);
367   DCHECK_NE(socket_, INVALID_SOCKET);
368   DCHECK_EQ(accept_event_, WSA_INVALID_EVENT);
369 
370   accept_event_ = WSACreateEvent();
371   int os_error = WSAGetLastError();
372   if (accept_event_ == WSA_INVALID_EVENT) {
373     PLOG(ERROR) << "WSACreateEvent()";
374     return MapSystemError(os_error);
375   }
376 
377   int result = listen(socket_, backlog);
378   os_error = WSAGetLastError();
379   if (result < 0) {
380     PLOG(ERROR) << "listen() returned an error";
381     return MapSystemError(os_error);
382   }
383 
384   return OK;
385 }
386 
Accept(std::unique_ptr<TCPSocketWin> * socket,IPEndPoint * address,CompletionOnceCallback callback)387 int TCPSocketWin::Accept(std::unique_ptr<TCPSocketWin>* socket,
388                          IPEndPoint* address,
389                          CompletionOnceCallback callback) {
390   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
391   DCHECK(socket);
392   DCHECK(address);
393   DCHECK(!callback.is_null());
394   DCHECK(accept_callback_.is_null());
395 
396   net_log_.BeginEvent(NetLogEventType::TCP_ACCEPT);
397 
398   int result = AcceptInternal(socket, address);
399 
400   if (result == ERR_IO_PENDING) {
401     // Start watching.
402     WSAEventSelect(socket_, accept_event_, FD_ACCEPT);
403     accept_watcher_.StartWatchingOnce(accept_event_, this);
404 
405     accept_socket_ = socket;
406     accept_address_ = address;
407     accept_callback_ = std::move(callback);
408   }
409 
410   return result;
411 }
412 
Connect(const IPEndPoint & address,CompletionOnceCallback callback)413 int TCPSocketWin::Connect(const IPEndPoint& address,
414                           CompletionOnceCallback callback) {
415   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
416   DCHECK_NE(socket_, INVALID_SOCKET);
417   DCHECK(!waiting_connect_);
418 
419   // |peer_address_| and |core_| will be non-NULL if Connect() has been called.
420   // Unless Close() is called to reset the internal state, a second call to
421   // Connect() is not allowed.
422   // Please note that we enforce this even if the previous Connect() has
423   // completed and failed. Although it is allowed to connect the same |socket_|
424   // again after a connection attempt failed on Windows, it results in
425   // unspecified behavior according to POSIX. Therefore, we make it behave in
426   // the same way as TCPSocketPosix.
427   DCHECK(!peer_address_ && !core_.get());
428 
429   if (!logging_multiple_connect_attempts_)
430     LogConnectBegin(AddressList(address));
431 
432   peer_address_ = std::make_unique<IPEndPoint>(address);
433 
434   int rv = DoConnect();
435   if (rv == ERR_IO_PENDING) {
436     // Synchronous operation not supported.
437     DCHECK(!callback.is_null());
438     read_callback_ = std::move(callback);
439     waiting_connect_ = true;
440   } else {
441     DoConnectComplete(rv);
442   }
443 
444   return rv;
445 }
446 
IsConnected() const447 bool TCPSocketWin::IsConnected() const {
448   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
449 
450   if (socket_ == INVALID_SOCKET || waiting_connect_)
451     return false;
452 
453   if (waiting_read_)
454     return true;
455 
456   // Check if connection is alive.
457   char c;
458   int rv = recv(socket_, &c, 1, MSG_PEEK);
459   int os_error = WSAGetLastError();
460   if (rv == 0)
461     return false;
462   if (rv == SOCKET_ERROR && os_error != WSAEWOULDBLOCK)
463     return false;
464 
465   return true;
466 }
467 
IsConnectedAndIdle() const468 bool TCPSocketWin::IsConnectedAndIdle() const {
469   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
470 
471   if (socket_ == INVALID_SOCKET || waiting_connect_)
472     return false;
473 
474   if (waiting_read_)
475     return true;
476 
477   // Check if connection is alive and we haven't received any data
478   // unexpectedly.
479   char c;
480   int rv = recv(socket_, &c, 1, MSG_PEEK);
481   int os_error = WSAGetLastError();
482   if (rv >= 0)
483     return false;
484   if (os_error != WSAEWOULDBLOCK)
485     return false;
486 
487   return true;
488 }
489 
Read(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)490 int TCPSocketWin::Read(IOBuffer* buf,
491                        int buf_len,
492                        CompletionOnceCallback callback) {
493   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
494   DCHECK(!core_->read_iobuffer_.get());
495   // base::Unretained() is safe because RetryRead() won't be called when |this|
496   // is gone.
497   int rv = ReadIfReady(
498       buf, buf_len,
499       base::BindOnce(&TCPSocketWin::RetryRead, base::Unretained(this)));
500   if (rv != ERR_IO_PENDING)
501     return rv;
502   read_callback_ = std::move(callback);
503   core_->read_iobuffer_ = buf;
504   core_->read_buffer_length_ = buf_len;
505   return ERR_IO_PENDING;
506 }
507 
ReadIfReady(IOBuffer * buf,int buf_len,CompletionOnceCallback callback)508 int TCPSocketWin::ReadIfReady(IOBuffer* buf,
509                               int buf_len,
510                               CompletionOnceCallback callback) {
511   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
512   DCHECK_NE(socket_, INVALID_SOCKET);
513   DCHECK(!waiting_read_);
514   DCHECK(read_if_ready_callback_.is_null());
515 
516   if (!core_->non_blocking_reads_initialized_) {
517     WSAEventSelect(socket_, core_->read_event_, FD_READ | FD_CLOSE);
518     core_->non_blocking_reads_initialized_ = true;
519   }
520   int rv = recv(socket_, buf->data(), buf_len, 0);
521   int os_error = WSAGetLastError();
522   if (rv == SOCKET_ERROR) {
523     if (os_error != WSAEWOULDBLOCK) {
524       int net_error = MapSystemError(os_error);
525       NetLogSocketError(net_log_, NetLogEventType::SOCKET_READ_ERROR, net_error,
526                         os_error);
527       return net_error;
528     }
529   } else {
530     net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_RECEIVED, rv,
531                                   buf->data());
532     activity_monitor::IncrementBytesReceived(rv);
533     return rv;
534   }
535 
536   waiting_read_ = true;
537   read_if_ready_callback_ = std::move(callback);
538   core_->WatchForRead();
539   return ERR_IO_PENDING;
540 }
541 
CancelReadIfReady()542 int TCPSocketWin::CancelReadIfReady() {
543   DCHECK(read_callback_.is_null());
544   DCHECK(!read_if_ready_callback_.is_null());
545   DCHECK(waiting_read_);
546 
547   core_->StopWatchingForRead();
548   read_if_ready_callback_.Reset();
549   waiting_read_ = false;
550   return net::OK;
551 }
552 
Write(IOBuffer * buf,int buf_len,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag &)553 int TCPSocketWin::Write(
554     IOBuffer* buf,
555     int buf_len,
556     CompletionOnceCallback callback,
557     const NetworkTrafficAnnotationTag& /* traffic_annotation */) {
558   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
559   DCHECK_NE(socket_, INVALID_SOCKET);
560   DCHECK(!waiting_write_);
561   CHECK(write_callback_.is_null());
562   DCHECK_GT(buf_len, 0);
563   DCHECK(!core_->write_iobuffer_.get());
564 
565   WSABUF write_buffer;
566   write_buffer.len = buf_len;
567   write_buffer.buf = buf->data();
568 
569   DWORD num;
570   int rv = WSASend(socket_, &write_buffer, 1, &num, 0,
571                    &core_->write_overlapped_, nullptr);
572   int os_error = WSAGetLastError();
573   if (rv == 0) {
574     if (ResetEventIfSignaled(core_->write_overlapped_.hEvent)) {
575       rv = static_cast<int>(num);
576       if (rv > buf_len || rv < 0) {
577         // It seems that some winsock interceptors report that more was written
578         // than was available. Treat this as an error.  http://crbug.com/27870
579         LOG(ERROR) << "Detected broken LSP: Asked to write " << buf_len
580                    << " bytes, but " << rv << " bytes reported.";
581         return ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES;
582       }
583       net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_SENT, rv,
584                                     buf->data());
585       return rv;
586     }
587   } else {
588     if (os_error != WSA_IO_PENDING) {
589       int net_error = MapSystemError(os_error);
590       NetLogSocketError(net_log_, NetLogEventType::SOCKET_WRITE_ERROR,
591                         net_error, os_error);
592       return net_error;
593     }
594   }
595   waiting_write_ = true;
596   write_callback_ = std::move(callback);
597   core_->write_iobuffer_ = buf;
598   core_->write_buffer_length_ = buf_len;
599   core_->WatchForWrite();
600   return ERR_IO_PENDING;
601 }
602 
GetLocalAddress(IPEndPoint * address) const603 int TCPSocketWin::GetLocalAddress(IPEndPoint* address) const {
604   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
605   DCHECK(address);
606 
607   SockaddrStorage storage;
608   if (getsockname(socket_, storage.addr, &storage.addr_len)) {
609     int os_error = WSAGetLastError();
610     return MapSystemError(os_error);
611   }
612   if (!address->FromSockAddr(storage.addr, storage.addr_len))
613     return ERR_ADDRESS_INVALID;
614 
615   return OK;
616 }
617 
GetPeerAddress(IPEndPoint * address) const618 int TCPSocketWin::GetPeerAddress(IPEndPoint* address) const {
619   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
620   DCHECK(address);
621   if (!IsConnected())
622     return ERR_SOCKET_NOT_CONNECTED;
623   *address = *peer_address_;
624   return OK;
625 }
626 
SetDefaultOptionsForServer()627 int TCPSocketWin::SetDefaultOptionsForServer() {
628   return SetExclusiveAddrUse();
629 }
630 
SetDefaultOptionsForClient()631 void TCPSocketWin::SetDefaultOptionsForClient() {
632   SetTCPNoDelay(socket_, /*no_delay=*/true);
633   SetTCPKeepAlive(socket_, true, kTCPKeepAliveSeconds);
634 }
635 
SetExclusiveAddrUse()636 int TCPSocketWin::SetExclusiveAddrUse() {
637   // On Windows, a bound end point can be hijacked by another process by
638   // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE
639   // was introduced in Windows NT 4.0 SP4. If the socket that is bound to the
640   // end point has SO_EXCLUSIVEADDRUSE enabled, it is not possible for another
641   // socket to forcibly bind to the end point until the end point is unbound.
642   // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
643   // MSDN: http://goo.gl/M6fjQ.
644   //
645   // Unlike on *nix, on Windows a TCP server socket can always bind to an end
646   // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
647   // needed here.
648   //
649   // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end
650   // point in TIME_WAIT status. It does not have this effect for a TCP server
651   // socket.
652 
653   BOOL true_value = 1;
654   int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
655                       reinterpret_cast<const char*>(&true_value),
656                       sizeof(true_value));
657   if (rv < 0)
658     return MapSystemError(errno);
659   return OK;
660 }
661 
SetReceiveBufferSize(int32_t size)662 int TCPSocketWin::SetReceiveBufferSize(int32_t size) {
663   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
664   return SetSocketReceiveBufferSize(socket_, size);
665 }
666 
SetSendBufferSize(int32_t size)667 int TCPSocketWin::SetSendBufferSize(int32_t size) {
668   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
669   return SetSocketSendBufferSize(socket_, size);
670 }
671 
SetKeepAlive(bool enable,int delay)672 bool TCPSocketWin::SetKeepAlive(bool enable, int delay) {
673   if (socket_ == INVALID_SOCKET)
674     return false;
675 
676   return SetTCPKeepAlive(socket_, enable, delay);
677 }
678 
SetNoDelay(bool no_delay)679 bool TCPSocketWin::SetNoDelay(bool no_delay) {
680   if (socket_ == INVALID_SOCKET)
681     return false;
682 
683   return SetTCPNoDelay(socket_, no_delay) == OK;
684 }
685 
SetIPv6Only(bool ipv6_only)686 int TCPSocketWin::SetIPv6Only(bool ipv6_only) {
687   return ::net::SetIPv6Only(socket_, ipv6_only);
688 }
689 
Close()690 void TCPSocketWin::Close() {
691   DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
692 
693   if (socket_ != INVALID_SOCKET) {
694     // Only log the close event if there's actually a socket to close.
695     net_log_.AddEvent(NetLogEventType::SOCKET_CLOSED);
696 
697     // Note: don't use CancelIo to cancel pending IO because it doesn't work
698     // when there is a Winsock layered service provider.
699 
700     // In most socket implementations, closing a socket results in a graceful
701     // connection shutdown, but in Winsock we have to call shutdown explicitly.
702     // See the MSDN page "Graceful Shutdown, Linger Options, and Socket Closure"
703     // at http://msdn.microsoft.com/en-us/library/ms738547.aspx
704     shutdown(socket_, SD_SEND);
705 
706     // This cancels any pending IO.
707     if (closesocket(socket_) < 0)
708       PLOG(ERROR) << "closesocket";
709     socket_ = INVALID_SOCKET;
710   }
711 
712   if (!accept_callback_.is_null()) {
713     accept_watcher_.StopWatching();
714     accept_socket_ = nullptr;
715     accept_address_ = nullptr;
716     accept_callback_.Reset();
717   }
718 
719   if (accept_event_) {
720     WSACloseEvent(accept_event_);
721     accept_event_ = WSA_INVALID_EVENT;
722   }
723 
724   if (core_.get()) {
725     core_->Detach();
726     core_ = nullptr;
727 
728     // |core_| may still exist and own a reference to itself, if there's a
729     // pending write. It has to stay alive until the operation completes, even
730     // when the socket is closed. This is not the case for reads.
731   }
732 
733   waiting_connect_ = false;
734   waiting_read_ = false;
735   waiting_write_ = false;
736 
737   read_callback_.Reset();
738   read_if_ready_callback_.Reset();
739   write_callback_.Reset();
740   peer_address_.reset();
741   connect_os_error_ = 0;
742 }
743 
DetachFromThread()744 void TCPSocketWin::DetachFromThread() {
745   DETACH_FROM_THREAD(thread_checker_);
746 }
747 
StartLoggingMultipleConnectAttempts(const AddressList & addresses)748 void TCPSocketWin::StartLoggingMultipleConnectAttempts(
749     const AddressList& addresses) {
750   if (!logging_multiple_connect_attempts_) {
751     logging_multiple_connect_attempts_ = true;
752     LogConnectBegin(addresses);
753   } else {
754     NOTREACHED();
755   }
756 }
757 
EndLoggingMultipleConnectAttempts(int net_error)758 void TCPSocketWin::EndLoggingMultipleConnectAttempts(int net_error) {
759   if (logging_multiple_connect_attempts_) {
760     LogConnectEnd(net_error);
761     logging_multiple_connect_attempts_ = false;
762   } else {
763     NOTREACHED();
764   }
765 }
766 
ReleaseSocketDescriptorForTesting()767 SocketDescriptor TCPSocketWin::ReleaseSocketDescriptorForTesting() {
768   SocketDescriptor socket_descriptor = socket_;
769   socket_ = INVALID_SOCKET;
770   Close();
771   return socket_descriptor;
772 }
773 
SocketDescriptorForTesting() const774 SocketDescriptor TCPSocketWin::SocketDescriptorForTesting() const {
775   return socket_;
776 }
777 
AcceptInternal(std::unique_ptr<TCPSocketWin> * socket,IPEndPoint * address)778 int TCPSocketWin::AcceptInternal(std::unique_ptr<TCPSocketWin>* socket,
779                                  IPEndPoint* address) {
780   SockaddrStorage storage;
781   int new_socket = accept(socket_, storage.addr, &storage.addr_len);
782   int os_error = WSAGetLastError();
783   if (new_socket < 0) {
784     int net_error = MapSystemError(os_error);
785     if (net_error != ERR_IO_PENDING)
786       net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error);
787     return net_error;
788   }
789 
790   IPEndPoint ip_end_point;
791   if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
792     NOTREACHED();
793     if (closesocket(new_socket) < 0)
794       PLOG(ERROR) << "closesocket";
795     int net_error = ERR_ADDRESS_INVALID;
796     net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT, net_error);
797     return net_error;
798   }
799   auto tcp_socket = std::make_unique<TCPSocketWin>(nullptr, net_log_.net_log(),
800                                                    net_log_.source());
801   int adopt_result = tcp_socket->AdoptConnectedSocket(new_socket, ip_end_point);
802   if (adopt_result != OK) {
803     net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_ACCEPT,
804                                       adopt_result);
805     return adopt_result;
806   }
807   *socket = std::move(tcp_socket);
808   *address = ip_end_point;
809   net_log_.EndEvent(NetLogEventType::TCP_ACCEPT, [&] {
810     return CreateNetLogIPEndPointParams(&ip_end_point);
811   });
812   return OK;
813 }
814 
OnObjectSignaled(HANDLE object)815 void TCPSocketWin::OnObjectSignaled(HANDLE object) {
816   WSANETWORKEVENTS ev;
817   if (WSAEnumNetworkEvents(socket_, accept_event_, &ev) == SOCKET_ERROR) {
818     PLOG(ERROR) << "WSAEnumNetworkEvents()";
819     return;
820   }
821 
822   if (ev.lNetworkEvents & FD_ACCEPT) {
823     int result = AcceptInternal(accept_socket_, accept_address_);
824     if (result != ERR_IO_PENDING) {
825       accept_socket_ = nullptr;
826       accept_address_ = nullptr;
827       std::move(accept_callback_).Run(result);
828     }
829   } else {
830     // This happens when a client opens a connection and closes it before we
831     // have a chance to accept it.
832     DCHECK(ev.lNetworkEvents == 0);
833 
834     // Start watching the next FD_ACCEPT event.
835     WSAEventSelect(socket_, accept_event_, FD_ACCEPT);
836     accept_watcher_.StartWatchingOnce(accept_event_, this);
837   }
838 }
839 
DoConnect()840 int TCPSocketWin::DoConnect() {
841   DCHECK_EQ(connect_os_error_, 0);
842   DCHECK(!core_.get());
843 
844   net_log_.BeginEvent(NetLogEventType::TCP_CONNECT_ATTEMPT, [&] {
845     return CreateNetLogIPEndPointParams(peer_address_.get());
846   });
847 
848   core_ = base::MakeRefCounted<Core>(this);
849 
850   // WSAEventSelect sets the socket to non-blocking mode as a side effect.
851   // Our connect() and recv() calls require that the socket be non-blocking.
852   WSAEventSelect(socket_, core_->read_event_, FD_CONNECT);
853 
854   SockaddrStorage storage;
855   if (!peer_address_->ToSockAddr(storage.addr, &storage.addr_len))
856     return ERR_ADDRESS_INVALID;
857 
858   if (!connect(socket_, storage.addr, storage.addr_len)) {
859     // Connected without waiting!
860     //
861     // The MSDN page for connect says:
862     //   With a nonblocking socket, the connection attempt cannot be completed
863     //   immediately. In this case, connect will return SOCKET_ERROR, and
864     //   WSAGetLastError will return WSAEWOULDBLOCK.
865     // which implies that for a nonblocking socket, connect never returns 0.
866     // It's not documented whether the event object will be signaled or not
867     // if connect does return 0.  So the code below is essentially dead code
868     // and we don't know if it's correct.
869     NOTREACHED();
870 
871     if (ResetEventIfSignaled(core_->read_event_))
872       return OK;
873   } else {
874     int os_error = WSAGetLastError();
875     if (os_error != WSAEWOULDBLOCK) {
876       LOG(ERROR) << "connect failed: " << os_error;
877       connect_os_error_ = os_error;
878       int rv = MapConnectError(os_error);
879       CHECK_NE(ERR_IO_PENDING, rv);
880       return rv;
881     }
882   }
883 
884   core_->WatchForRead();
885   return ERR_IO_PENDING;
886 }
887 
DoConnectComplete(int result)888 void TCPSocketWin::DoConnectComplete(int result) {
889   // Log the end of this attempt (and any OS error it threw).
890   int os_error = connect_os_error_;
891   connect_os_error_ = 0;
892   if (result != OK) {
893     net_log_.EndEventWithIntParams(NetLogEventType::TCP_CONNECT_ATTEMPT,
894                                    "os_error", os_error);
895   } else {
896     net_log_.EndEvent(NetLogEventType::TCP_CONNECT_ATTEMPT);
897   }
898 
899   if (!logging_multiple_connect_attempts_)
900     LogConnectEnd(result);
901 }
902 
LogConnectBegin(const AddressList & addresses)903 void TCPSocketWin::LogConnectBegin(const AddressList& addresses) {
904   net_log_.BeginEvent(NetLogEventType::TCP_CONNECT,
905                       [&] { return addresses.NetLogParams(); });
906 }
907 
LogConnectEnd(int net_error)908 void TCPSocketWin::LogConnectEnd(int net_error) {
909   if (net_error != OK) {
910     net_log_.EndEventWithNetErrorCode(NetLogEventType::TCP_CONNECT, net_error);
911     return;
912   }
913 
914   net_log_.EndEvent(NetLogEventType::TCP_CONNECT, [&] {
915     net::IPEndPoint local_address;
916     int net_error = GetLocalAddress(&local_address);
917     net::IPEndPoint remote_address;
918     if (net_error == net::OK)
919       net_error = GetPeerAddress(&remote_address);
920     if (net_error != net::OK)
921       return NetLogParamsWithInt("get_address_net_error", net_error);
922     return CreateNetLogAddressPairParams(local_address, remote_address);
923   });
924 }
925 
RetryRead(int rv)926 void TCPSocketWin::RetryRead(int rv) {
927   DCHECK(core_->read_iobuffer_);
928 
929   if (rv == OK) {
930     // base::Unretained() is safe because RetryRead() won't be called when
931     // |this| is gone.
932     rv = ReadIfReady(
933         core_->read_iobuffer_.get(), core_->read_buffer_length_,
934         base::BindOnce(&TCPSocketWin::RetryRead, base::Unretained(this)));
935     if (rv == ERR_IO_PENDING)
936       return;
937   }
938   core_->read_iobuffer_ = nullptr;
939   core_->read_buffer_length_ = 0;
940   std::move(read_callback_).Run(rv);
941 }
942 
DidCompleteConnect()943 void TCPSocketWin::DidCompleteConnect() {
944   DCHECK(waiting_connect_);
945   DCHECK(!read_callback_.is_null());
946   int result;
947 
948   WSANETWORKEVENTS events;
949   int rv = WSAEnumNetworkEvents(socket_, core_->read_event_, &events);
950   int os_error = WSAGetLastError();
951   if (rv == SOCKET_ERROR) {
952     DLOG(FATAL)
953         << "WSAEnumNetworkEvents() failed with SOCKET_ERROR, os_error = "
954         << os_error;
955     result = MapSystemError(os_error);
956   } else if (events.lNetworkEvents & FD_CONNECT) {
957     os_error = events.iErrorCode[FD_CONNECT_BIT];
958     result = MapConnectError(os_error);
959   } else {
960     DLOG(FATAL) << "WSAEnumNetworkEvents() failed, rv = " << rv;
961     result = ERR_UNEXPECTED;
962   }
963 
964   connect_os_error_ = os_error;
965   DoConnectComplete(result);
966   waiting_connect_ = false;
967 
968   DCHECK_NE(result, ERR_IO_PENDING);
969   std::move(read_callback_).Run(result);
970 }
971 
DidCompleteWrite()972 void TCPSocketWin::DidCompleteWrite() {
973   DCHECK(waiting_write_);
974   DCHECK(!write_callback_.is_null());
975 
976   DWORD num_bytes, flags;
977   BOOL ok = WSAGetOverlappedResult(socket_, &core_->write_overlapped_,
978                                    &num_bytes, FALSE, &flags);
979   int os_error = WSAGetLastError();
980   WSAResetEvent(core_->write_overlapped_.hEvent);
981   waiting_write_ = false;
982   int rv;
983   if (!ok) {
984     rv = MapSystemError(os_error);
985     NetLogSocketError(net_log_, NetLogEventType::SOCKET_WRITE_ERROR, rv,
986                       os_error);
987   } else {
988     rv = static_cast<int>(num_bytes);
989     if (rv > core_->write_buffer_length_ || rv < 0) {
990       // It seems that some winsock interceptors report that more was written
991       // than was available. Treat this as an error.  http://crbug.com/27870
992       LOG(ERROR) << "Detected broken LSP: Asked to write "
993                  << core_->write_buffer_length_ << " bytes, but " << rv
994                  << " bytes reported.";
995       rv = ERR_WINSOCK_UNEXPECTED_WRITTEN_BYTES;
996     } else {
997       net_log_.AddByteTransferEvent(NetLogEventType::SOCKET_BYTES_SENT,
998                                     num_bytes, core_->write_iobuffer_->data());
999     }
1000   }
1001 
1002   core_->write_iobuffer_ = nullptr;
1003 
1004   DCHECK_NE(rv, ERR_IO_PENDING);
1005   std::move(write_callback_).Run(rv);
1006 }
1007 
DidSignalRead()1008 void TCPSocketWin::DidSignalRead() {
1009   DCHECK(waiting_read_);
1010   DCHECK(!read_if_ready_callback_.is_null());
1011 
1012   int os_error = 0;
1013   WSANETWORKEVENTS network_events;
1014   int rv = WSAEnumNetworkEvents(socket_, core_->read_event_, &network_events);
1015   os_error = WSAGetLastError();
1016 
1017   if (rv == SOCKET_ERROR) {
1018     rv = MapSystemError(os_error);
1019   } else if (network_events.lNetworkEvents) {
1020     DCHECK_EQ(network_events.lNetworkEvents & ~(FD_READ | FD_CLOSE), 0);
1021     // If network_events.lNetworkEvents is FD_CLOSE and
1022     // network_events.iErrorCode[FD_CLOSE_BIT] is 0, it is a graceful
1023     // connection closure. It is tempting to directly set rv to 0 in
1024     // this case, but the MSDN pages for WSAEventSelect and
1025     // WSAAsyncSelect recommend we still call RetryRead():
1026     //   FD_CLOSE should only be posted after all data is read from a
1027     //   socket, but an application should check for remaining data upon
1028     //   receipt of FD_CLOSE to avoid any possibility of losing data.
1029     //
1030     // If network_events.iErrorCode[FD_READ_BIT] or
1031     // network_events.iErrorCode[FD_CLOSE_BIT] is nonzero, still call
1032     // RetryRead() because recv() reports a more accurate error code
1033     // (WSAECONNRESET vs. WSAECONNABORTED) when the connection was
1034     // reset.
1035     rv = OK;
1036   } else {
1037     // This may happen because Read() may succeed synchronously and
1038     // consume all the received data without resetting the event object.
1039     core_->WatchForRead();
1040     return;
1041   }
1042 
1043   DCHECK_NE(rv, ERR_IO_PENDING);
1044   waiting_read_ = false;
1045   std::move(read_if_ready_callback_).Run(rv);
1046 }
1047 
GetEstimatedRoundTripTime(base::TimeDelta * out_rtt) const1048 bool TCPSocketWin::GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const {
1049   DCHECK(out_rtt);
1050   // TODO(bmcquade): Consider implementing using
1051   // GetPerTcpConnectionEStats/GetPerTcp6ConnectionEStats.
1052   return false;
1053 }
1054 
ApplySocketTag(const SocketTag & tag)1055 void TCPSocketWin::ApplySocketTag(const SocketTag& tag) {
1056   // Windows does not support any specific SocketTags so fail if any non-default
1057   // tag is applied.
1058   CHECK(tag == SocketTag());
1059 }
1060 
BindToNetwork(handles::NetworkHandle network)1061 int TCPSocketWin::BindToNetwork(handles::NetworkHandle network) {
1062   NOTIMPLEMENTED();
1063   return ERR_NOT_IMPLEMENTED;
1064 }
1065 
1066 }  // namespace net
1067