Home | History | Annotate | Download | only in socket
      1 // Copyright (c) 2012 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.
      4 
      5 #include "net/socket/tcp_server_socket_win.h"
      6 
      7 #include <mstcpip.h>
      8 
      9 #include "net/base/ip_endpoint.h"
     10 #include "net/base/net_errors.h"
     11 #include "net/base/net_util.h"
     12 #include "net/base/winsock_init.h"
     13 #include "net/base/winsock_util.h"
     14 #include "net/socket/socket_net_log_params.h"
     15 #include "net/socket/tcp_client_socket.h"
     16 
     17 namespace net {
     18 
     19 TCPServerSocketWin::TCPServerSocketWin(net::NetLog* net_log,
     20                                        const net::NetLog::Source& source)
     21     : socket_(INVALID_SOCKET),
     22       socket_event_(WSA_INVALID_EVENT),
     23       accept_socket_(NULL),
     24       net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) {
     25   net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE,
     26                       source.ToEventParametersCallback());
     27   EnsureWinsockInit();
     28 }
     29 
     30 TCPServerSocketWin::~TCPServerSocketWin() {
     31   Close();
     32   net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE);
     33 }
     34 
     35 int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) {
     36   DCHECK(CalledOnValidThread());
     37   DCHECK_GT(backlog, 0);
     38   DCHECK_EQ(socket_, INVALID_SOCKET);
     39   DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
     40 
     41   socket_event_ = WSACreateEvent();
     42   if (socket_event_ == WSA_INVALID_EVENT) {
     43     PLOG(ERROR) << "WSACreateEvent()";
     44     return ERR_FAILED;
     45   }
     46 
     47   socket_ = socket(address.GetSockAddrFamily(), SOCK_STREAM, IPPROTO_TCP);
     48   if (socket_ == INVALID_SOCKET) {
     49     PLOG(ERROR) << "socket() returned an error";
     50     return MapSystemError(WSAGetLastError());
     51   }
     52 
     53   if (SetNonBlocking(socket_)) {
     54     int result = MapSystemError(WSAGetLastError());
     55     Close();
     56     return result;
     57   }
     58 
     59   int result = SetSocketOptions();
     60   if (result != OK) {
     61     Close();
     62     return result;
     63   }
     64 
     65   SockaddrStorage storage;
     66   if (!address.ToSockAddr(storage.addr, &storage.addr_len)) {
     67     Close();
     68     return ERR_ADDRESS_INVALID;
     69   }
     70 
     71   result = bind(socket_, storage.addr, storage.addr_len);
     72   if (result < 0) {
     73     PLOG(ERROR) << "bind() returned an error";
     74     result = MapSystemError(WSAGetLastError());
     75     Close();
     76     return result;
     77   }
     78 
     79   result = listen(socket_, backlog);
     80   if (result < 0) {
     81     PLOG(ERROR) << "listen() returned an error";
     82     result = MapSystemError(WSAGetLastError());
     83     Close();
     84     return result;
     85   }
     86 
     87   return OK;
     88 }
     89 
     90 int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const {
     91   DCHECK(CalledOnValidThread());
     92   DCHECK(address);
     93 
     94   SockaddrStorage storage;
     95   if (getsockname(socket_, storage.addr, &storage.addr_len))
     96     return MapSystemError(WSAGetLastError());
     97   if (!address->FromSockAddr(storage.addr, storage.addr_len))
     98     return ERR_FAILED;
     99 
    100   return OK;
    101 }
    102 
    103 int TCPServerSocketWin::Accept(
    104     scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) {
    105   DCHECK(CalledOnValidThread());
    106   DCHECK(socket);
    107   DCHECK(!callback.is_null());
    108   DCHECK(accept_callback_.is_null());
    109 
    110   net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
    111 
    112   int result = AcceptInternal(socket);
    113 
    114   if (result == ERR_IO_PENDING) {
    115     // Start watching
    116     WSAEventSelect(socket_, socket_event_, FD_ACCEPT);
    117     accept_watcher_.StartWatching(socket_event_, this);
    118 
    119     accept_socket_ = socket;
    120     accept_callback_ = callback;
    121   }
    122 
    123   return result;
    124 }
    125 
    126 int TCPServerSocketWin::SetSocketOptions() {
    127   // On Windows, a bound end point can be hijacked by another process by
    128   // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE
    129   // was introduced in Windows NT 4.0 SP4. If the socket that is bound to the
    130   // end point has SO_EXCLUSIVEADDRUSE enabled, it is not possible for another
    131   // socket to forcibly bind to the end point until the end point is unbound.
    132   // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
    133   // MSDN: http://goo.gl/M6fjQ.
    134   //
    135   // Unlike on *nix, on Windows a TCP server socket can always bind to an end
    136   // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
    137   // needed here.
    138   //
    139   // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end
    140   // point in TIME_WAIT status. It does not have this effect for a TCP server
    141   // socket.
    142 
    143   BOOL true_value = 1;
    144   int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
    145                       reinterpret_cast<const char*>(&true_value),
    146                       sizeof(true_value));
    147   if (rv < 0)
    148     return MapSystemError(errno);
    149   return OK;
    150 }
    151 
    152 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) {
    153   SockaddrStorage storage;
    154   int new_socket = accept(socket_, storage.addr, &storage.addr_len);
    155   if (new_socket < 0) {
    156     int net_error = MapSystemError(WSAGetLastError());
    157     if (net_error != ERR_IO_PENDING)
    158       net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
    159     return net_error;
    160   }
    161 
    162   IPEndPoint address;
    163   if (!address.FromSockAddr(storage.addr, storage.addr_len)) {
    164     NOTREACHED();
    165     if (closesocket(new_socket) < 0)
    166       PLOG(ERROR) << "closesocket";
    167     net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
    168     return ERR_FAILED;
    169   }
    170   scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket(
    171       AddressList(address),
    172       net_log_.net_log(), net_log_.source()));
    173   int adopt_result = tcp_socket->AdoptSocket(new_socket);
    174   if (adopt_result != OK) {
    175     if (closesocket(new_socket) < 0)
    176       PLOG(ERROR) << "closesocket";
    177     net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
    178     return adopt_result;
    179   }
    180   socket->reset(tcp_socket.release());
    181   net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
    182                     CreateNetLogIPEndPointCallback(&address));
    183   return OK;
    184 }
    185 
    186 void TCPServerSocketWin::Close() {
    187   if (socket_ != INVALID_SOCKET) {
    188     if (closesocket(socket_) < 0)
    189       PLOG(ERROR) << "closesocket";
    190     socket_ = INVALID_SOCKET;
    191   }
    192 
    193   if (socket_event_) {
    194     WSACloseEvent(socket_event_);
    195     socket_event_ = WSA_INVALID_EVENT;
    196   }
    197 }
    198 
    199 void TCPServerSocketWin::OnObjectSignaled(HANDLE object) {
    200   WSANETWORKEVENTS ev;
    201   if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) {
    202     PLOG(ERROR) << "WSAEnumNetworkEvents()";
    203     return;
    204   }
    205 
    206   if (ev.lNetworkEvents & FD_ACCEPT) {
    207     int result = AcceptInternal(accept_socket_);
    208     if (result != ERR_IO_PENDING) {
    209       accept_socket_ = NULL;
    210       CompletionCallback callback = accept_callback_;
    211       accept_callback_.Reset();
    212       callback.Run(result);
    213     }
    214   }
    215 }
    216 
    217 }  // namespace net
    218