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_libevent.h" | 5 #include "net/socket/tcp_socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netdb.h> | 9 #include <netdb.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
11 | 11 |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 | 13 |
14 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
15 #include <netinet/in.h> | 15 #include <netinet/in.h> |
16 #endif | 16 #endif |
17 | 17 |
| 18 #include "base/logging.h" |
18 #include "base/posix/eintr_wrapper.h" | 19 #include "base/posix/eintr_wrapper.h" |
19 #include "net/base/ip_endpoint.h" | 20 #include "net/base/ip_endpoint.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
22 #include "net/socket/socket_descriptor.h" | 23 #include "net/socket/socket_descriptor.h" |
23 #include "net/socket/socket_net_log_params.h" | 24 #include "net/socket/socket_net_log_params.h" |
24 #include "net/socket/tcp_client_socket.h" | |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 TCPServerSocketLibevent::TCPServerSocketLibevent( | 28 TCPSocketLibevent::TCPSocketLibevent(NetLog* net_log, |
29 net::NetLog* net_log, | 29 const NetLog::Source& source) |
30 const net::NetLog::Source& source) | |
31 : socket_(kInvalidSocket), | 30 : socket_(kInvalidSocket), |
32 accept_socket_(NULL), | 31 accept_socket_(NULL), |
| 32 accept_address_(NULL), |
33 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 33 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { |
34 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 34 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, |
35 source.ToEventParametersCallback()); | 35 source.ToEventParametersCallback()); |
36 } | 36 } |
37 | 37 |
38 TCPServerSocketLibevent::~TCPServerSocketLibevent() { | 38 TCPSocketLibevent::~TCPSocketLibevent() { |
39 if (socket_ != kInvalidSocket) | 39 if (socket_ != kInvalidSocket) |
40 Close(); | 40 Close(); |
41 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 41 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); |
42 } | 42 } |
43 | 43 |
44 int TCPServerSocketLibevent::Listen(const IPEndPoint& address, int backlog) { | 44 int TCPSocketLibevent::Create(AddressFamily family) { |
45 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
46 DCHECK_GT(backlog, 0); | |
47 DCHECK_EQ(socket_, kInvalidSocket); | 46 DCHECK_EQ(socket_, kInvalidSocket); |
48 | 47 |
49 socket_ = CreatePlatformSocket(address.GetSockAddrFamily(), SOCK_STREAM, | 48 socket_ = CreatePlatformSocket(ConvertAddressFamily(family), SOCK_STREAM, |
50 IPPROTO_TCP); | 49 IPPROTO_TCP); |
51 if (socket_ < 0) { | 50 if (socket_ < 0) { |
52 PLOG(ERROR) << "socket() returned an error"; | 51 PLOG(ERROR) << "CreatePlatformSocket() returned an error"; |
53 return MapSystemError(errno); | 52 return MapSystemError(errno); |
54 } | 53 } |
55 | 54 |
56 if (SetNonBlocking(socket_)) { | 55 if (SetNonBlocking(socket_)) { |
57 int result = MapSystemError(errno); | 56 int result = MapSystemError(errno); |
58 Close(); | 57 Close(); |
59 return result; | 58 return result; |
60 } | 59 } |
61 | 60 |
62 int result = SetSocketOptions(); | 61 return OK; |
63 if (result != OK) { | 62 } |
64 Close(); | |
65 return result; | |
66 } | |
67 | 63 |
68 SockaddrStorage storage; | 64 int TCPSocketLibevent::Adopt(int socket) { |
69 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) { | 65 DCHECK(CalledOnValidThread()); |
70 Close(); | 66 DCHECK_EQ(socket_, kInvalidSocket); |
71 return ERR_ADDRESS_INVALID; | |
72 } | |
73 | 67 |
74 result = bind(socket_, storage.addr, storage.addr_len); | 68 socket_ = socket; |
75 if (result < 0) { | |
76 PLOG(ERROR) << "bind() returned an error"; | |
77 result = MapSystemError(errno); | |
78 Close(); | |
79 return result; | |
80 } | |
81 | 69 |
82 result = listen(socket_, backlog); | 70 if (SetNonBlocking(socket_)) { |
83 if (result < 0) { | 71 int result = MapSystemError(errno); |
84 PLOG(ERROR) << "listen() returned an error"; | |
85 result = MapSystemError(errno); | |
86 Close(); | 72 Close(); |
87 return result; | 73 return result; |
88 } | 74 } |
89 | 75 |
90 return OK; | 76 return OK; |
91 } | 77 } |
92 | 78 |
93 int TCPServerSocketLibevent::GetLocalAddress(IPEndPoint* address) const { | 79 int TCPSocketLibevent::Release() { |
| 80 DCHECK(CalledOnValidThread()); |
| 81 DCHECK(accept_callback_.is_null()); |
| 82 |
| 83 int result = socket_; |
| 84 socket_ = kInvalidSocket; |
| 85 return result; |
| 86 } |
| 87 |
| 88 int TCPSocketLibevent::Bind(const IPEndPoint& address) { |
| 89 DCHECK(CalledOnValidThread()); |
| 90 DCHECK_NE(socket_, kInvalidSocket); |
| 91 |
| 92 SockaddrStorage storage; |
| 93 if (!address.ToSockAddr(storage.addr, &storage.addr_len)) |
| 94 return ERR_ADDRESS_INVALID; |
| 95 |
| 96 int result = bind(socket_, storage.addr, storage.addr_len); |
| 97 if (result < 0) { |
| 98 PLOG(ERROR) << "bind() returned an error"; |
| 99 return MapSystemError(errno); |
| 100 } |
| 101 |
| 102 return OK; |
| 103 } |
| 104 |
| 105 int TCPSocketLibevent::GetLocalAddress(IPEndPoint* address) const { |
94 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
95 DCHECK(address); | 107 DCHECK(address); |
96 | 108 |
97 SockaddrStorage storage; | 109 SockaddrStorage storage; |
98 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0) | 110 if (getsockname(socket_, storage.addr, &storage.addr_len) < 0) |
99 return MapSystemError(errno); | 111 return MapSystemError(errno); |
100 if (!address->FromSockAddr(storage.addr, storage.addr_len)) | 112 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
101 return ERR_FAILED; | 113 return ERR_FAILED; |
102 | 114 |
103 return OK; | 115 return OK; |
104 } | 116 } |
105 | 117 |
106 int TCPServerSocketLibevent::Accept( | 118 int TCPSocketLibevent::Listen(int backlog) { |
107 scoped_ptr<StreamSocket>* socket, const CompletionCallback& callback) { | 119 DCHECK(CalledOnValidThread()); |
| 120 DCHECK_GT(backlog, 0); |
| 121 DCHECK_NE(socket_, kInvalidSocket); |
| 122 |
| 123 int result = listen(socket_, backlog); |
| 124 if (result < 0) { |
| 125 PLOG(ERROR) << "listen() returned an error"; |
| 126 return MapSystemError(errno); |
| 127 } |
| 128 |
| 129 return OK; |
| 130 } |
| 131 |
| 132 int TCPSocketLibevent::Accept(scoped_ptr<TCPSocketLibevent>* socket, |
| 133 IPEndPoint* address, |
| 134 const CompletionCallback& callback) { |
108 DCHECK(CalledOnValidThread()); | 135 DCHECK(CalledOnValidThread()); |
109 DCHECK(socket); | 136 DCHECK(socket); |
| 137 DCHECK(address); |
110 DCHECK(!callback.is_null()); | 138 DCHECK(!callback.is_null()); |
111 DCHECK(accept_callback_.is_null()); | 139 DCHECK(accept_callback_.is_null()); |
112 | 140 |
113 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); | 141 net_log_.BeginEvent(NetLog::TYPE_TCP_ACCEPT); |
114 | 142 |
115 int result = AcceptInternal(socket); | 143 int result = AcceptInternal(socket, address); |
116 | 144 |
117 if (result == ERR_IO_PENDING) { | 145 if (result == ERR_IO_PENDING) { |
118 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 146 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
119 socket_, true, base::MessageLoopForIO::WATCH_READ, | 147 socket_, true, base::MessageLoopForIO::WATCH_READ, |
120 &accept_socket_watcher_, this)) { | 148 &accept_socket_watcher_, this)) { |
121 PLOG(ERROR) << "WatchFileDescriptor failed on read"; | 149 PLOG(ERROR) << "WatchFileDescriptor failed on read"; |
122 return MapSystemError(errno); | 150 return MapSystemError(errno); |
123 } | 151 } |
124 | 152 |
125 accept_socket_ = socket; | 153 accept_socket_ = socket; |
| 154 accept_address_ = address; |
126 accept_callback_ = callback; | 155 accept_callback_ = callback; |
127 } | 156 } |
128 | 157 |
129 return result; | 158 return result; |
130 } | 159 } |
131 | 160 |
132 int TCPServerSocketLibevent::SetSocketOptions() { | 161 int TCPSocketLibevent::SetDefaultOptionsForServer() { |
| 162 return SetAddressReuse(true); |
| 163 } |
| 164 |
| 165 int TCPSocketLibevent::SetAddressReuse(bool allow) { |
133 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound | 166 // SO_REUSEADDR is useful for server sockets to bind to a recently unbound |
134 // port. When a socket is closed, the end point changes its state to TIME_WAIT | 167 // port. When a socket is closed, the end point changes its state to TIME_WAIT |
135 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer | 168 // and wait for 2 MSL (maximum segment lifetime) to ensure the remote peer |
136 // acknowledges its closure. For server sockets, it is usually safe to | 169 // acknowledges its closure. For server sockets, it is usually safe to |
137 // bind to a TIME_WAIT end point immediately, which is a widely adopted | 170 // bind to a TIME_WAIT end point immediately, which is a widely adopted |
138 // behavior. | 171 // behavior. |
139 // | 172 // |
140 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to | 173 // Note that on *nix, SO_REUSEADDR does not enable the TCP socket to bind to |
141 // an end point that is already bound by another socket. To do that one must | 174 // an end point that is already bound by another socket. To do that one must |
142 // set SO_REUSEPORT instead. This option is not provided on Linux prior | 175 // set SO_REUSEPORT instead. This option is not provided on Linux prior |
143 // to 3.9. | 176 // to 3.9. |
144 // | 177 // |
145 // SO_REUSEPORT is provided in MacOS X and iOS. | 178 // SO_REUSEPORT is provided in MacOS X and iOS. |
146 int true_value = 1; | 179 int boolean_value = allow ? 1 : 0; |
147 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &true_value, | 180 int rv = setsockopt(socket_, SOL_SOCKET, SO_REUSEADDR, &boolean_value, |
148 sizeof(true_value)); | 181 sizeof(boolean_value)); |
149 if (rv < 0) | 182 if (rv < 0) |
150 return MapSystemError(errno); | 183 return MapSystemError(errno); |
151 return OK; | 184 return OK; |
152 } | 185 } |
153 | 186 |
154 int TCPServerSocketLibevent::AcceptInternal( | 187 void TCPSocketLibevent::Close() { |
155 scoped_ptr<StreamSocket>* socket) { | 188 if (socket_ != kInvalidSocket) { |
| 189 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); |
| 190 DCHECK(ok); |
| 191 if (HANDLE_EINTR(close(socket_)) < 0) |
| 192 PLOG(ERROR) << "close"; |
| 193 socket_ = kInvalidSocket; |
| 194 } |
| 195 } |
| 196 |
| 197 int TCPSocketLibevent::AcceptInternal(scoped_ptr<TCPSocketLibevent>* socket, |
| 198 IPEndPoint* address) { |
156 SockaddrStorage storage; | 199 SockaddrStorage storage; |
157 int new_socket = HANDLE_EINTR(accept(socket_, | 200 int new_socket = HANDLE_EINTR(accept(socket_, |
158 storage.addr, | 201 storage.addr, |
159 &storage.addr_len)); | 202 &storage.addr_len)); |
160 if (new_socket < 0) { | 203 if (new_socket < 0) { |
161 int net_error = MapSystemError(errno); | 204 int net_error = MapSystemError(errno); |
162 if (net_error != ERR_IO_PENDING) | 205 if (net_error != ERR_IO_PENDING) |
163 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); | 206 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, net_error); |
164 return net_error; | 207 return net_error; |
165 } | 208 } |
166 | 209 |
167 IPEndPoint address; | 210 IPEndPoint ip_end_point; |
168 if (!address.FromSockAddr(storage.addr, storage.addr_len)) { | 211 if (!ip_end_point.FromSockAddr(storage.addr, storage.addr_len)) { |
169 NOTREACHED(); | 212 NOTREACHED(); |
170 if (HANDLE_EINTR(close(new_socket)) < 0) | 213 if (HANDLE_EINTR(close(new_socket)) < 0) |
171 PLOG(ERROR) << "close"; | 214 PLOG(ERROR) << "close"; |
172 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); | 215 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, ERR_FAILED); |
173 return ERR_FAILED; | 216 return ERR_FAILED; |
174 } | 217 } |
175 scoped_ptr<TCPClientSocket> tcp_socket(new TCPClientSocket( | 218 scoped_ptr<TCPSocketLibevent> tcp_socket(new TCPSocketLibevent( |
176 AddressList(address), | |
177 net_log_.net_log(), net_log_.source())); | 219 net_log_.net_log(), net_log_.source())); |
178 int adopt_result = tcp_socket->AdoptSocket(new_socket); | 220 int adopt_result = tcp_socket->Adopt(new_socket); |
179 if (adopt_result != OK) { | 221 if (adopt_result != OK) { |
180 if (HANDLE_EINTR(close(new_socket)) < 0) | |
181 PLOG(ERROR) << "close"; | |
182 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); | 222 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_TCP_ACCEPT, adopt_result); |
183 return adopt_result; | 223 return adopt_result; |
184 } | 224 } |
185 socket->reset(tcp_socket.release()); | 225 *socket = tcp_socket.Pass(); |
| 226 *address = ip_end_point; |
186 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, | 227 net_log_.EndEvent(NetLog::TYPE_TCP_ACCEPT, |
187 CreateNetLogIPEndPointCallback(&address)); | 228 CreateNetLogIPEndPointCallback(&ip_end_point)); |
188 return OK; | 229 return OK; |
189 } | 230 } |
190 | 231 |
191 void TCPServerSocketLibevent::Close() { | 232 void TCPSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { |
192 if (socket_ != kInvalidSocket) { | |
193 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); | |
194 DCHECK(ok); | |
195 if (HANDLE_EINTR(close(socket_)) < 0) | |
196 PLOG(ERROR) << "close"; | |
197 socket_ = kInvalidSocket; | |
198 } | |
199 } | |
200 | |
201 void TCPServerSocketLibevent::OnFileCanReadWithoutBlocking(int fd) { | |
202 DCHECK(CalledOnValidThread()); | 233 DCHECK(CalledOnValidThread()); |
203 | 234 |
204 int result = AcceptInternal(accept_socket_); | 235 int result = AcceptInternal(accept_socket_, accept_address_); |
205 if (result != ERR_IO_PENDING) { | 236 if (result != ERR_IO_PENDING) { |
206 accept_socket_ = NULL; | 237 accept_socket_ = NULL; |
| 238 accept_address_ = NULL; |
207 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); | 239 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); |
208 DCHECK(ok); | 240 DCHECK(ok); |
209 CompletionCallback callback = accept_callback_; | 241 CompletionCallback callback = accept_callback_; |
210 accept_callback_.Reset(); | 242 accept_callback_.Reset(); |
211 callback.Run(result); | 243 callback.Run(result); |
212 } | 244 } |
213 } | 245 } |
214 | 246 |
215 void TCPServerSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { | 247 void TCPSocketLibevent::OnFileCanWriteWithoutBlocking(int fd) { |
216 NOTREACHED(); | 248 NOTREACHED(); |
217 } | 249 } |
218 | 250 |
219 } // namespace net | 251 } // namespace net |
OLD | NEW |