Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: net/socket/tcp_socket_win.cc

Issue 22861033: Move server socket functionality from TCPServerSocket into TCPSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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;
akalin 2013/08/29 22:08:33 decomp into a helper function somewhere? one doesn
yzshen1 2013/08/29 23:04:18 Done. I put it in net/base/net_util.h. On 2013/08
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::SetDefaultOptionsForServer() {
169 return SetExclusiveAddrUse();
170 }
171
172 int TCPSocketWin::SetExclusiveAddrUse() {
127 // On Windows, a bound end point can be hijacked by another process by 173 // On Windows, a bound end point can be hijacked by another process by
128 // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE 174 // 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 175 // 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 176 // 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. 177 // 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. 178 // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
133 // MSDN: http://goo.gl/M6fjQ. 179 // MSDN: http://goo.gl/M6fjQ.
134 // 180 //
135 // Unlike on *nix, on Windows a TCP server socket can always bind to an end 181 // 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 182 // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
137 // needed here. 183 // needed here.
138 // 184 //
139 // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end 185 // 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 186 // point in TIME_WAIT status. It does not have this effect for a TCP server
141 // socket. 187 // socket.
142 188
143 BOOL true_value = 1; 189 BOOL true_value = 1;
144 int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 190 int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
145 reinterpret_cast<const char*>(&true_value), 191 reinterpret_cast<const char*>(&true_value),
146 sizeof(true_value)); 192 sizeof(true_value));
147 if (rv < 0) 193 if (rv < 0)
148 return MapSystemError(errno); 194 return MapSystemError(errno);
149 return OK; 195 return OK;
150 } 196 }
151 197
152 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { 198 void TCPSocketWin::Close() {
199 if (socket_ != INVALID_SOCKET) {
200 if (closesocket(socket_) < 0)
201 PLOG(ERROR) << "closesocket";
202 socket_ = INVALID_SOCKET;
203 }
204
205 if (socket_event_) {
206 WSACloseEvent(socket_event_);
207 socket_event_ = WSA_INVALID_EVENT;
208 }
209 }
210
211 int TCPSocketWin::AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
212 IPEndPoint* address) {
153 SockaddrStorage storage; 213 SockaddrStorage storage;
154 int new_socket = accept(socket_, storage.addr, &storage.addr_len); 214 int new_socket = accept(socket_, storage.addr, &storage.addr_len);
155 if (new_socket < 0) { 215 if (new_socket < 0) {
156 int net_error = MapSystemError(WSAGetLastError()); 216 int net_error = MapSystemError(WSAGetLastError());
157 if (net_error != ERR_IO_PENDING) 217 if (net_error != ERR_IO_PENDING)
158 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); 218 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
159 return net_error; 219 return net_error;
160 } 220 }
161 221
162 IPEndPoint address; 222 IPEndPoint ip_end_point;
163 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { 223 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
164 NOTREACHED(); 224 NOTREACHED();
165 if (closesocket(new_socket) < 0) 225 if (closesocket(new_socket) < 0)
166 PLOG(ERROR) << "closesocket"; 226 PLOG(ERROR) << "closesocket";
167 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); 227 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
168 return ERR_FAILED; 228 return ERR_FAILED;
169 } 229 }
170 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( 230 scoped_ptr<TCPSocketWin> tcp_socket(new TCPSocketWin(
171 AddressList(address),
172 net_log_.net_log(), net_log_.source())); 231 net_log_.net_log(), net_log_.source()));
173 int adopt_result = tcp_socket->AdoptSocket(new_socket); 232 int adopt_result = tcp_socket->Adopt(new_socket);
174 if (adopt_result != OK) { 233 if (adopt_result != OK) {
175 if (closesocket(new_socket) < 0)
176 PLOG(ERROR) << "closesocket";
177 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); 234 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
178 return adopt_result; 235 return adopt_result;
179 } 236 }
180 socket->reset(tcp_socket.release()); 237 *socket = tcp_socket.Pass();
238 if (address)
239 *address = ip_end_point;
181 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, 240 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
182 CreateNetLogIPEndPointCallback(&address)); 241 CreateNetLogIPEndPointCallback(&ip_end_point));
183 return OK; 242 return OK;
184 } 243 }
185 244
186 void TCPServerSocketWin::Close() { 245 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; 246 WSANETWORKEVENTS ev;
201 if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) { 247 if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) {
202 PLOG(ERROR) << "WSAEnumNetworkEvents()"; 248 PLOG(ERROR) << "WSAEnumNetworkEvents()";
203 return; 249 return;
204 } 250 }
205 251
206 if (ev.lNetworkEvents & FD_ACCEPT) { 252 if (ev.lNetworkEvents & FD_ACCEPT) {
207 int result = AcceptInternal(accept_socket_); 253 int result = AcceptInternal(accept_socket_, accept_address_);
208 if (result != ERR_IO_PENDING) { 254 if (result != ERR_IO_PENDING) {
209 accept_socket_ = NULL; 255 accept_socket_ = NULL;
256 accept_address_ = NULL;
210 CompletionCallback callback = accept_callback_; 257 CompletionCallback callback = accept_callback_;
211 accept_callback_.Reset(); 258 accept_callback_.Reset();
212 callback.Run(result); 259 callback.Run(result);
213 } 260 }
214 } 261 }
215 } 262 }
216 263
217 } // namespace net 264 } // namespace net
OLDNEW
« net/socket/tcp_socket_unittest.cc ('K') | « net/socket/tcp_socket_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698