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

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 socket_ = socket(ConvertAddressFamily(family), SOCK_STREAM, IPPROTO_TCP);
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) { 41 if (socket_ == INVALID_SOCKET) {
49 PLOG(ERROR) << "socket() returned an error"; 42 PLOG(ERROR) << "socket() returned an error";
50 return MapSystemError(WSAGetLastError()); 43 return MapSystemError(WSAGetLastError());
51 } 44 }
52 45
53 if (SetNonBlocking(socket_)) { 46 if (SetNonBlocking(socket_)) {
54 int result = MapSystemError(WSAGetLastError()); 47 int result = MapSystemError(WSAGetLastError());
55 Close(); 48 Close();
56 return result; 49 return result;
57 } 50 }
58 51
59 int result = SetSocketOptions(); 52 return OK;
60 if (result != OK) { 53 }
61 Close();
62 return result;
63 }
64 54
65 SockaddrStorage storage; 55 int TCPSocketWin::Adopt(SOCKET socket) {
66 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) { 56 DCHECK(CalledOnValidThread());
67 Close(); 57 DCHECK_EQ(socket_, INVALID_SOCKET);
68 return ERR_ADDRESS_INVALID;
69 }
70 58
71 result = bind(socket_, storage.addr, storage.addr_len); 59 socket_ = socket;
72 if (result < 0) {
73 PLOG(ERROR) << "bind() returned an error";
74 result = MapSystemError(WSAGetLastError());
75 Close();
76 return result;
77 }
78 60
79 result = listen(socket_, backlog); 61 if (SetNonBlocking(socket_)) {
80 if (result < 0) { 62 int result = MapSystemError(WSAGetLastError());
81 PLOG(ERROR) << "listen() returned an error";
82 result = MapSystemError(WSAGetLastError());
83 Close(); 63 Close();
84 return result; 64 return result;
85 } 65 }
86 66
87 return OK; 67 return OK;
88 } 68 }
89 69
90 int TCPServerSocketWin::GetLocalAddress(IPEndPoint* address) const { 70 SOCKET TCPSocketWin::Release() {
71 DCHECK(CalledOnValidThread());
72 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
73 DCHECK(accept_callback_.is_null());
74
75 SOCKET result = socket_;
76 socket_ = INVALID_SOCKET;
77 return result;
78 }
79
80 int TCPSocketWin::Bind(const IPEndPoint& address) {
81 DCHECK(CalledOnValidThread());
82 DCHECK_NE(socket_, INVALID_SOCKET);
83
84 SockaddrStorage storage;
85 if (!address.ToSockAddr(storage.addr, &storage.addr_len))
86 return ERR_ADDRESS_INVALID;
87
88 int result = bind(socket_, storage.addr, storage.addr_len);
89 if (result < 0) {
90 PLOG(ERROR) << "bind() returned an error";
91 return MapSystemError(WSAGetLastError());
92 }
93
94 return OK;
95 }
96
97 int TCPSocketWin::GetLocalAddress(IPEndPoint* address) const {
91 DCHECK(CalledOnValidThread()); 98 DCHECK(CalledOnValidThread());
92 DCHECK(address); 99 DCHECK(address);
93 100
94 SockaddrStorage storage; 101 SockaddrStorage storage;
95 if (getsockname(socket_, storage.addr, &storage.addr_len)) 102 if (getsockname(socket_, storage.addr, &storage.addr_len))
96 return MapSystemError(WSAGetLastError()); 103 return MapSystemError(WSAGetLastError());
97 if (!address->FromSockAddr(storage.addr, storage.addr_len)) 104 if (!address->FromSockAddr(storage.addr, storage.addr_len))
98 return ERR_FAILED; 105 return ERR_FAILED;
99 106
100 return OK; 107 return OK;
101 } 108 }
102 109
103 int TCPServerSocketWin::Accept( 110 int TCPSocketWin::Listen(int backlog) {
104 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { 111 DCHECK(CalledOnValidThread());
112 DCHECK_GT(backlog, 0);
113 DCHECK_NE(socket_, INVALID_SOCKET);
114 DCHECK_EQ(socket_event_, WSA_INVALID_EVENT);
115
116 socket_event_ = WSACreateEvent();
117 if (socket_event_ == WSA_INVALID_EVENT) {
118 PLOG(ERROR) << "WSACreateEvent()";
119 return ERR_FAILED;
120 }
121
122 int result = listen(socket_, backlog);
123 if (result < 0) {
124 PLOG(ERROR) << "listen() returned an error";
125 result = MapSystemError(WSAGetLastError());
126 return result;
127 }
128
129 return OK;
130 }
131
132 int TCPSocketWin::Accept(scoped_ptr<TCPSocketWin>* socket,
133 IPEndPoint* address,
134 const CompletionCallback& callback) {
105 DCHECK(CalledOnValidThread()); 135 DCHECK(CalledOnValidThread());
106 DCHECK(socket); 136 DCHECK(socket);
137 DCHECK(address);
107 DCHECK(!callback.is_null()); 138 DCHECK(!callback.is_null());
108 DCHECK(accept_callback_.is_null()); 139 DCHECK(accept_callback_.is_null());
109 140
110 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); 141 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT);
111 142
112 int result = AcceptInternal(socket); 143 int result = AcceptInternal(socket, address);
113 144
114 if (result == ERR_IO_PENDING) { 145 if (result == ERR_IO_PENDING) {
115 // Start watching 146 // Start watching.
116 WSAEventSelect(socket_, socket_event_, FD_ACCEPT); 147 WSAEventSelect(socket_, socket_event_, FD_ACCEPT);
117 accept_watcher_.StartWatching(socket_event_, this); 148 accept_watcher_.StartWatching(socket_event_, this);
118 149
119 accept_socket_ = socket; 150 accept_socket_ = socket;
151 accept_address_ = address;
120 accept_callback_ = callback; 152 accept_callback_ = callback;
121 } 153 }
122 154
123 return result; 155 return result;
124 } 156 }
125 157
126 int TCPServerSocketWin::SetSocketOptions() { 158 int TCPSocketWin::SetDefaultOptionsForServer() {
159 return SetExclusiveAddrUse();
160 }
161
162 int TCPSocketWin::SetExclusiveAddrUse() {
127 // On Windows, a bound end point can be hijacked by another process by 163 // On Windows, a bound end point can be hijacked by another process by
128 // setting SO_REUSEADDR. Therefore a Windows-only option SO_EXCLUSIVEADDRUSE 164 // 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 165 // 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 166 // 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. 167 // 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. 168 // It is recommend that all server applications must use SO_EXCLUSIVEADDRUSE.
133 // MSDN: http://goo.gl/M6fjQ. 169 // MSDN: http://goo.gl/M6fjQ.
134 // 170 //
135 // Unlike on *nix, on Windows a TCP server socket can always bind to an end 171 // 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 172 // point in TIME_WAIT state without setting SO_REUSEADDR, therefore it is not
137 // needed here. 173 // needed here.
138 // 174 //
139 // SO_EXCLUSIVEADDRUSE will prevent a TCP client socket from binding to an end 175 // 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 176 // point in TIME_WAIT status. It does not have this effect for a TCP server
141 // socket. 177 // socket.
142 178
143 BOOL true_value = 1; 179 BOOL true_value = 1;
144 int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, 180 int rv = setsockopt(socket_, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
145 reinterpret_cast<const char*>(&true_value), 181 reinterpret_cast<const char*>(&true_value),
146 sizeof(true_value)); 182 sizeof(true_value));
147 if (rv < 0) 183 if (rv < 0)
148 return MapSystemError(errno); 184 return MapSystemError(errno);
149 return OK; 185 return OK;
150 } 186 }
151 187
152 int TCPServerSocketWin::AcceptInternal(scoped_ptr<StreamSocket>* socket) { 188 void TCPSocketWin::Close() {
189 if (socket_ != INVALID_SOCKET) {
190 if (closesocket(socket_) < 0)
191 PLOG(ERROR) << "closesocket";
192 socket_ = INVALID_SOCKET;
193 }
194
195 if (socket_event_) {
196 WSACloseEvent(socket_event_);
197 socket_event_ = WSA_INVALID_EVENT;
198 }
199 }
200
201 int TCPSocketWin::AcceptInternal(scoped_ptr<TCPSocketWin>* socket,
202 IPEndPoint* address) {
153 SockaddrStorage storage; 203 SockaddrStorage storage;
154 int new_socket = accept(socket_, storage.addr, &storage.addr_len); 204 int new_socket = accept(socket_, storage.addr, &storage.addr_len);
155 if (new_socket < 0) { 205 if (new_socket < 0) {
156 int net_error = MapSystemError(WSAGetLastError()); 206 int net_error = MapSystemError(WSAGetLastError());
157 if (net_error != ERR_IO_PENDING) 207 if (net_error != ERR_IO_PENDING)
158 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); 208 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error);
159 return net_error; 209 return net_error;
160 } 210 }
161 211
162 IPEndPoint address; 212 IPEndPoint ip_end_point;
163 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { 213 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) {
164 NOTREACHED(); 214 NOTREACHED();
165 if (closesocket(new_socket) < 0) 215 if (closesocket(new_socket) < 0)
166 PLOG(ERROR) << "closesocket"; 216 PLOG(ERROR) << "closesocket";
167 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); 217 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED);
168 return ERR_FAILED; 218 return ERR_FAILED;
169 } 219 }
170 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( 220 scoped_ptr<TCPSocketWin> tcp_socket(new TCPSocketWin(
171 AddressList(address),
172 net_log_.net_log(), net_log_.source())); 221 net_log_.net_log(), net_log_.source()));
173 int adopt_result = tcp_socket->AdoptSocket(new_socket); 222 int adopt_result = tcp_socket->Adopt(new_socket);
174 if (adopt_result != OK) { 223 if (adopt_result != OK) {
175 if (closesocket(new_socket) < 0)
176 PLOG(ERROR) << "closesocket";
177 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); 224 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result);
178 return adopt_result; 225 return adopt_result;
179 } 226 }
180 socket->reset(tcp_socket.release()); 227 *socket = tcp_socket.Pass();
228 *address = ip_end_point;
181 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, 229 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT,
182 CreateNetLogIPEndPointCallback(&address)); 230 CreateNetLogIPEndPointCallback(&ip_end_point));
183 return OK; 231 return OK;
184 } 232 }
185 233
186 void TCPServerSocketWin::Close() { 234 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; 235 WSANETWORKEVENTS ev;
201 if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) { 236 if (WSAEnumNetworkEvents(socket_, socket_event_, &ev) == SOCKET_ERROR) {
202 PLOG(ERROR) << "WSAEnumNetworkEvents()"; 237 PLOG(ERROR) << "WSAEnumNetworkEvents()";
203 return; 238 return;
204 } 239 }
205 240
206 if (ev.lNetworkEvents & FD_ACCEPT) { 241 if (ev.lNetworkEvents & FD_ACCEPT) {
207 int result = AcceptInternal(accept_socket_); 242 int result = AcceptInternal(accept_socket_, accept_address_);
208 if (result != ERR_IO_PENDING) { 243 if (result != ERR_IO_PENDING) {
209 accept_socket_ = NULL; 244 accept_socket_ = NULL;
245 accept_address_ = NULL;
210 CompletionCallback callback = accept_callback_; 246 CompletionCallback callback = accept_callback_;
211 accept_callback_.Reset(); 247 accept_callback_.Reset();
212 callback.Run(result); 248 callback.Run(result);
213 } 249 }
214 } 250 }
215 } 251 }
216 252
217 } // namespace net 253 } // namespace net
OLDNEW
« net/socket/tcp_socket_libevent.h ('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