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

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

Powered by Google App Engine
This is Rietveld 408576698