OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "platform/globals.h" | 7 #include "platform/globals.h" |
8 #if defined(HOST_OS_FUCHSIA) | 8 #if defined(HOST_OS_FUCHSIA) |
9 | 9 |
10 #include "bin/socket.h" | 10 #include "bin/socket.h" |
11 | 11 |
12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
13 | 13 |
| 14 #include "bin/eventhandler.h" |
14 #include "bin/fdutils.h" | 15 #include "bin/fdutils.h" |
15 #include "platform/signal_blocker.h" | 16 #include "platform/signal_blocker.h" |
16 | 17 |
17 // #define SOCKET_LOG_INFO 1 | 18 // #define SOCKET_LOG_INFO 1 |
18 // #define SOCKET_LOG_ERROR 1 | 19 // #define SOCKET_LOG_ERROR 1 |
19 | 20 |
20 // define SOCKET_LOG_ERROR to get log messages only for errors. | 21 // define SOCKET_LOG_ERROR to get log messages only for errors. |
21 // define SOCKET_LOG_INFO to get log messages for both information and errors. | 22 // define SOCKET_LOG_INFO to get log messages for both information and errors. |
22 #if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) | 23 #if defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) |
23 #define LOG_ERR(msg, ...) \ | 24 #define LOG_ERR(msg, ...) \ |
(...skipping 15 matching lines...) Expand all Loading... |
39 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) | 40 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) |
40 | 41 |
41 namespace dart { | 42 namespace dart { |
42 namespace bin { | 43 namespace bin { |
43 | 44 |
44 Socket::Socket(intptr_t fd) | 45 Socket::Socket(intptr_t fd) |
45 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 46 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
46 | 47 |
47 | 48 |
48 void Socket::SetClosedFd() { | 49 void Socket::SetClosedFd() { |
| 50 ASSERT(fd_ != kClosedFd); |
| 51 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
| 52 ASSERT(handle != NULL); |
| 53 handle->Release(); |
49 fd_ = kClosedFd; | 54 fd_ = kClosedFd; |
50 } | 55 } |
51 | 56 |
52 | 57 |
53 static intptr_t Create(const RawAddr& addr) { | 58 static intptr_t Create(const RawAddr& addr) { |
54 LOG_INFO("Create: calling socket(SOCK_STREAM)\n"); | 59 LOG_INFO("Create: calling socket(SOCK_STREAM)\n"); |
55 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 60 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
56 if (fd < 0) { | 61 if (fd < 0) { |
57 LOG_ERR("Create: socket(SOCK_STREAM) failed\n"); | 62 LOG_ERR("Create: socket(SOCK_STREAM) failed\n"); |
58 return -1; | 63 return -1; |
59 } | 64 } |
60 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd); | 65 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd); |
61 if (!FDUtils::SetCloseOnExec(fd)) { | 66 if (!FDUtils::SetCloseOnExec(fd)) { |
62 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd); | 67 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd); |
63 FDUtils::SaveErrorAndClose(fd); | 68 FDUtils::SaveErrorAndClose(fd); |
64 return -1; | 69 return -1; |
65 } | 70 } |
66 return fd; | 71 IOHandle* io_handle = new IOHandle(fd); |
| 72 return reinterpret_cast<intptr_t>(io_handle); |
67 } | 73 } |
68 | 74 |
69 | 75 |
70 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 76 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
71 LOG_INFO("Connect: calling connect(%ld)\n", fd); | 77 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
| 78 LOG_INFO("Connect: calling connect(%ld)\n", handle->fd()); |
72 intptr_t result = NO_RETRY_EXPECTED( | 79 intptr_t result = NO_RETRY_EXPECTED( |
73 connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr))); | 80 connect(handle->fd(), &addr.addr, SocketAddress::GetAddrLength(addr))); |
74 if ((result == 0) || (errno == EINPROGRESS)) { | 81 if ((result == 0) || (errno == EINPROGRESS)) { |
75 return fd; | 82 return reinterpret_cast<intptr_t>(handle); |
76 } | 83 } |
77 LOG_ERR("Connect: connect(%ld) failed\n", fd); | 84 LOG_ERR("Connect: connect(%ld) failed\n", handle->fd()); |
78 FDUtils::SaveErrorAndClose(fd); | 85 FDUtils::SaveErrorAndClose(handle->fd()); |
| 86 handle->Release(); |
79 return -1; | 87 return -1; |
80 } | 88 } |
81 | 89 |
82 | 90 |
83 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 91 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
84 intptr_t fd = Create(addr); | 92 intptr_t fd = Create(addr); |
85 if (fd < 0) { | 93 if (fd < 0) { |
86 return fd; | 94 return fd; |
87 } | 95 } |
88 if (!FDUtils::SetNonBlocking(fd)) { | 96 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
89 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", fd); | 97 if (!FDUtils::SetNonBlocking(handle->fd())) { |
90 FDUtils::SaveErrorAndClose(fd); | 98 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", |
| 99 handle->fd()); |
| 100 FDUtils::SaveErrorAndClose(handle->fd()); |
| 101 handle->Release(); |
91 return -1; | 102 return -1; |
92 } | 103 } |
93 return Connect(fd, addr); | 104 return Connect(fd, addr); |
94 } | 105 } |
95 | 106 |
96 | 107 |
97 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 108 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
98 const RawAddr& source_addr) { | 109 const RawAddr& source_addr) { |
99 LOG_ERR("SocketBase::CreateBindConnect is unimplemented\n"); | 110 LOG_ERR("SocketBase::CreateBindConnect is unimplemented\n"); |
100 UNIMPLEMENTED(); | 111 UNIMPLEMENTED(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 151 |
141 LOG_INFO("ServerSocket::CreateBindListen: calling bind(%ld)\n", fd); | 152 LOG_INFO("ServerSocket::CreateBindListen: calling bind(%ld)\n", fd); |
142 if (NO_RETRY_EXPECTED( | 153 if (NO_RETRY_EXPECTED( |
143 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { | 154 bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
144 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd); | 155 LOG_ERR("ServerSocket::CreateBindListen: bind(%ld) failed\n", fd); |
145 FDUtils::SaveErrorAndClose(fd); | 156 FDUtils::SaveErrorAndClose(fd); |
146 return -1; | 157 return -1; |
147 } | 158 } |
148 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd); | 159 LOG_INFO("ServerSocket::CreateBindListen: bind(%ld) succeeded\n", fd); |
149 | 160 |
| 161 IOHandle* io_handle = new IOHandle(fd); |
| 162 |
150 // Test for invalid socket port 65535 (some browsers disallow it). | 163 // Test for invalid socket port 65535 (some browsers disallow it). |
151 if ((SocketAddress::GetAddrPort(addr) == 0) && | 164 if ((SocketAddress::GetAddrPort(addr) == 0) && |
152 (SocketBase::GetPort(fd) == 65535)) { | 165 (SocketBase::GetPort(reinterpret_cast<intptr_t>(io_handle)) == 65535)) { |
153 // Don't close the socket until we have created a new socket, ensuring | 166 // Don't close the socket until we have created a new socket, ensuring |
154 // that we do not get the bad port number again. | 167 // that we do not get the bad port number again. |
155 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); | 168 intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
156 FDUtils::SaveErrorAndClose(fd); | 169 FDUtils::SaveErrorAndClose(fd); |
| 170 io_handle->Release(); |
157 return new_fd; | 171 return new_fd; |
158 } | 172 } |
159 | 173 |
160 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd); | 174 LOG_INFO("ServerSocket::CreateBindListen: calling listen(%ld)\n", fd); |
161 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 175 if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
162 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd); | 176 LOG_ERR("ServerSocket::CreateBindListen: listen failed(%ld)\n", fd); |
163 FDUtils::SaveErrorAndClose(fd); | 177 FDUtils::SaveErrorAndClose(fd); |
| 178 io_handle->Release(); |
164 return -1; | 179 return -1; |
165 } | 180 } |
166 LOG_INFO("ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd); | 181 LOG_INFO("ServerSocket::CreateBindListen: listen(%ld) succeeded\n", fd); |
167 | 182 |
168 if (!FDUtils::SetNonBlocking(fd)) { | 183 if (!FDUtils::SetNonBlocking(fd)) { |
169 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd); | 184 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd); |
170 FDUtils::SaveErrorAndClose(fd); | 185 FDUtils::SaveErrorAndClose(fd); |
| 186 io_handle->Release(); |
171 return -1; | 187 return -1; |
172 } | 188 } |
173 return fd; | 189 return reinterpret_cast<intptr_t>(io_handle); |
174 } | 190 } |
175 | 191 |
176 | 192 |
177 bool ServerSocket::StartAccept(intptr_t fd) { | 193 bool ServerSocket::StartAccept(intptr_t fd) { |
178 USE(fd); | 194 USE(fd); |
179 return true; | 195 return true; |
180 } | 196 } |
181 | 197 |
182 | 198 |
183 static bool IsTemporaryAcceptError(int error) { | 199 static bool IsTemporaryAcceptError(int error) { |
184 // On Linux a number of protocol errors should be treated as EAGAIN. | 200 // On Linux a number of protocol errors should be treated as EAGAIN. |
185 // These are the ones for TCP/IP. | 201 // These are the ones for TCP/IP. |
186 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 202 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
187 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 203 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
188 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 204 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
189 (error == ENETUNREACH); | 205 (error == ENETUNREACH); |
190 } | 206 } |
191 | 207 |
192 | 208 |
193 intptr_t ServerSocket::Accept(intptr_t fd) { | 209 intptr_t ServerSocket::Accept(intptr_t fd) { |
| 210 IOHandle* listen_handle = reinterpret_cast<IOHandle*>(fd); |
194 intptr_t socket; | 211 intptr_t socket; |
195 struct sockaddr clientaddr; | 212 struct sockaddr clientaddr; |
196 socklen_t addrlen = sizeof(clientaddr); | 213 socklen_t addrlen = sizeof(clientaddr); |
197 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", fd); | 214 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", listen_fd); |
198 socket = NO_RETRY_EXPECTED(accept(fd, &clientaddr, &addrlen)); | 215 socket = listen_handle->Accept(&clientaddr, &addrlen); |
199 if (socket == -1) { | 216 if (socket == -1) { |
200 if (IsTemporaryAcceptError(errno)) { | 217 if (IsTemporaryAcceptError(errno)) { |
201 // We need to signal to the caller that this is actually not an | 218 // We need to signal to the caller that this is actually not an |
202 // error. We got woken up from the poll on the listening socket, | 219 // error. We got woken up from the poll on the listening socket, |
203 // but there is no connection ready to be accepted. | 220 // but there is no connection ready to be accepted. |
204 ASSERT(kTemporaryFailure != -1); | 221 ASSERT(kTemporaryFailure != -1); |
205 socket = kTemporaryFailure; | 222 socket = kTemporaryFailure; |
206 } else { | 223 } else { |
207 LOG_ERR("ServerSocket::Accept: accept(%ld) failed\n", fd); | 224 LOG_ERR("ServerSocket::Accept: accept(%ld) failed\n", listen_fd); |
208 } | 225 } |
209 } else { | 226 } else { |
210 LOG_INFO("ServerSocket::Accept: accept(%ld) -> socket %ld\n", fd, socket); | 227 IOHandle* io_handle = new IOHandle(socket); |
| 228 LOG_INFO("ServerSocket::Accept: accept(%ld) -> socket %ld\n", listen_fd, |
| 229 socket); |
211 if (!FDUtils::SetCloseOnExec(socket)) { | 230 if (!FDUtils::SetCloseOnExec(socket)) { |
212 LOG_ERR("FDUtils::SetCloseOnExec(%ld) failed\n", socket); | 231 LOG_ERR("FDUtils::SetCloseOnExec(%ld) failed\n", socket); |
213 FDUtils::SaveErrorAndClose(socket); | 232 FDUtils::SaveErrorAndClose(socket); |
| 233 io_handle->Release(); |
214 return -1; | 234 return -1; |
215 } | 235 } |
216 if (!FDUtils::SetNonBlocking(socket)) { | 236 if (!FDUtils::SetNonBlocking(socket)) { |
217 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket); | 237 LOG_ERR("FDUtils::SetNonBlocking(%ld) failed\n", socket); |
218 FDUtils::SaveErrorAndClose(socket); | 238 FDUtils::SaveErrorAndClose(socket); |
| 239 io_handle->Release(); |
219 return -1; | 240 return -1; |
220 } | 241 } |
| 242 socket = reinterpret_cast<intptr_t>(io_handle); |
221 } | 243 } |
222 return socket; | 244 return socket; |
223 } | 245 } |
224 | 246 |
225 } // namespace bin | 247 } // namespace bin |
226 } // namespace dart | 248 } // namespace dart |
227 | 249 |
228 #endif // defined(HOST_OS_FUCHSIA) | 250 #endif // defined(HOST_OS_FUCHSIA) |
229 | 251 |
230 #endif // !defined(DART_IO_DISABLED) | 252 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |