[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 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" |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 14 | #include "net/socket/socket_net_log_params.h" |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 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), |
[email protected] | 68f2a23 | 2012-09-17 06:59:40 | [diff] [blame] | 24 | reuse_address_(false), |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 25 | net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 26 | net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
| 27 | source.ToEventParametersCallback()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 28 | EnsureWinsockInit(); |
| 29 | } |
| 30 | |
| 31 | TCPServerSocketWin::~TCPServerSocketWin() { |
| 32 | Close(); |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 33 | net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 34 | } |
| 35 | |
[email protected] | 68f2a23 | 2012-09-17 06:59:40 | [diff] [blame] | 36 | void TCPServerSocketWin::AllowAddressReuse() { |
| 37 | DCHECK(CalledOnValidThread()); |
| 38 | DCHECK_EQ(socket_, INVALID_SOCKET); |
| 39 | DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
| 40 | |
| 41 | reuse_address_ = true; |
| 42 | } |
| 43 | |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 44 | int TCPServerSocketWin::Listen(const IPEndPoint& address, int backlog) { |
| 45 | DCHECK(CalledOnValidThread()); |
| 46 | DCHECK_GT(backlog, 0); |
| 47 | DCHECK_EQ(socket_, INVALID_SOCKET); |
| 48 | DCHECK_EQ(socket_event_, WSA_INVALID_EVENT); |
| 49 | |
| 50 | socket_event_ = WSACreateEvent(); |
| 51 | if (socket_event_ == WSA_INVALID_EVENT) { |
| 52 | PLOG(ERROR) << "WSACreateEvent()"; |
| 53 | return ERR_FAILED; |
| 54 | } |
| 55 | |
[email protected] | e466aaf | 2012-12-13 01:46:44 | [diff] [blame] | 56 | socket_ = socket(address.GetSockAddrFamily(), SOCK_STREAM, IPPROTO_TCP); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 57 | if (socket_ < 0) { |
| 58 | PLOG(ERROR) << "socket() returned an error"; |
| 59 | return MapSystemError(WSAGetLastError()); |
| 60 | } |
| 61 | |
| 62 | if (SetNonBlocking(socket_)) { |
| 63 | int result = MapSystemError(WSAGetLastError()); |
| 64 | Close(); |
| 65 | return result; |
| 66 | } |
| 67 | |
[email protected] | 68f2a23 | 2012-09-17 06:59:40 | [diff] [blame] | 68 | int result = SetSocketOptions(); |
| 69 | if (result != OK) |
| 70 | return result; |
| 71 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 72 | SockaddrStorage storage; |
| 73 | if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 74 | return ERR_INVALID_ARGUMENT; |
| 75 | |
[email protected] | 68f2a23 | 2012-09-17 06:59:40 | [diff] [blame] | 76 | result = bind(socket_, storage.addr, storage.addr_len); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 77 | if (result < 0) { |
| 78 | PLOG(ERROR) << "bind() returned an error"; |
| 79 | result = MapSystemError(WSAGetLastError()); |
| 80 | Close(); |
| 81 | return result; |
| 82 | } |
| 83 | |
| 84 | result = listen(socket_, backlog); |
| 85 | if (result < 0) { |
| 86 | PLOG(ERROR) << "listen() returned an error"; |
| 87 | result = MapSystemError(WSAGetLastError()); |
| 88 | Close(); |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | return OK; |
| 93 | } |
| 94 | |
| 95 | int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const { |
| 96 | DCHECK(CalledOnValidThread()); |
| 97 | DCHECK(address); |
| 98 | |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 99 | SockaddrStorage storage; |
| 100 | if (getsockname(socket_, storage.addr, &storage.addr_len)) |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 101 | return MapSystemError(WSAGetLastError()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 102 | if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 103 | return ERR_FAILED; |
| 104 | |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | int TCPServerSocketWin::Accept( |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 109 | scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 110 | DCHECK(CalledOnValidThread()); |
| 111 | DCHECK(socket); |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 112 | DCHECK(!callback.is_null()); |
| 113 | DCHECK(accept_callback_.is_null()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 114 | |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 115 | net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 116 | |
| 117 | int result = AcceptInternal(socket); |
| 118 | |
| 119 | if (result == ERR_IO_PENDING) { |
| 120 | // Start watching |
| 121 | WSAEventSelect(socket_, socket_event_, FD_ACCEPT); |
| 122 | accept_watcher_.StartWatching(socket_event_, this); |
| 123 | |
| 124 | accept_socket_ = socket; |
| 125 | accept_callback_ = callback; |
| 126 | } |
| 127 | |
| 128 | return result; |
| 129 | } |
| 130 | |
[email protected] | 68f2a23 | 2012-09-17 06:59:40 | [diff] [blame] | 131 | int TCPServerSocketWin::SetSocketOptions() { |
| 132 | BOOL true_value = 1; |
| 133 | if (reuse_address_) { |
| 134 | int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, |
| 135 | reinterpret_cast<const char*>(&true_value), |
| 136 | sizeof(true_value)); |
| 137 | if (rv < 0) |
| 138 | return MapSystemError(errno); |
| 139 | } |
| 140 | return OK; |
| 141 | } |
| 142 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 143 | int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 144 | SockaddrStorage storage; |
| 145 | int new_socket = accept(socket_, storage.addr, &storage.addr_len); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 146 | if (new_socket < 0) { |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 147 | int net_error = MapSystemError(WSAGetLastError()); |
| 148 | if (net_error != ERR_IO_PENDING) |
| 149 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
| 150 | return net_error; |
| 151 | } |
| 152 | |
| 153 | IPEndPoint address; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 154 | if (!address.FromSockAddr(storage.addr, storage.addr_len)) { |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 155 | NOTREACHED(); |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 156 | if (closesocket(new_socket) < 0) |
[email protected] | a75c745 | 2011-04-18 23:19:38 | [diff] [blame] | 157 | PLOG(ERROR) << "closesocket"; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 158 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); |
| 159 | return ERR_FAILED; |
| 160 | } |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 161 | scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 162 | AddressList(address), |
[email protected] | 03ec2538 | 2011-05-27 21:50:28 | [diff] [blame] | 163 | net_log_.net_log(), net_log_.source())); |
| 164 | int adopt_result = tcp_socket->AdoptSocket(new_socket); |
| 165 | if (adopt_result != OK) { |
| 166 | if (closesocket(new_socket) < 0) |
| 167 | PLOG(ERROR) << "closesocket"; |
| 168 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
| 169 | return adopt_result; |
| 170 | } |
| 171 | socket->reset(tcp_socket.release()); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 172 | net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
[email protected] | 3aa4af04 | 2012-06-14 21:02:31 | [diff] [blame] | 173 | CreateNetLogIPEndPointCallback(&address)); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 174 | return OK; |
| 175 | } |
| 176 | |
| 177 | void TCPServerSocketWin::Close() { |
| 178 | if (socket_ != INVALID_SOCKET) { |
[email protected] | a75c745 | 2011-04-18 23:19:38 | [diff] [blame] | 179 | if (closesocket(socket_) < 0) |
| 180 | PLOG(ERROR) << "closesocket"; |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 181 | socket_ = INVALID_SOCKET; |
| 182 | } |
| 183 | |
| 184 | if (socket_event_) { |
| 185 | WSACloseEvent(socket_event_); |
| 186 | socket_event_ = WSA_INVALID_EVENT; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void TCPServerSocketWin::OnObjectSignaled(HANDLE object) { |
| 191 | WSANETWORKEVENTS ev; |
| 192 | if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) { |
| 193 | PLOG(ERROR) << "WSAEnumNetworkEvents()"; |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | if (ev.lNetworkEvents & FD_ACCEPT) { |
| 198 | int result = AcceptInternal(accept_socket_); |
| 199 | if (result != ERR_IO_PENDING) { |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 200 | accept_socket_ = NULL; |
[email protected] | e756022 | 2012-03-31 00:35:38 | [diff] [blame] | 201 | CompletionCallback callback = accept_callback_; |
[email protected] | df7a30d | 2011-12-03 04:16:50 | [diff] [blame] | 202 | accept_callback_.Reset(); |
[email protected] | e756022 | 2012-03-31 00:35:38 | [diff] [blame] | 203 | callback.Run(result); |
[email protected] | 3871252 | 2011-04-18 23:03:32 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | } // namespace net |