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