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