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/eventhandler.h" |
15 #include "bin/fdutils.h" | 15 #include "bin/fdutils.h" |
16 #include "platform/signal_blocker.h" | 16 #include "platform/signal_blocker.h" |
17 | 17 |
18 // #define SOCKET_LOG_INFO 1 | 18 // #define SOCKET_LOG_INFO 1 |
19 // #define SOCKET_LOG_ERROR 1 | 19 // #define SOCKET_LOG_ERROR 1 |
20 | 20 |
21 // define SOCKET_LOG_ERROR to get log messages only for errors. | 21 // define SOCKET_LOG_ERROR to get log messages only for errors. |
22 // 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. |
(...skipping 15 matching lines...) Expand all Loading... |
38 #define LOG_ERR(msg, ...) | 38 #define LOG_ERR(msg, ...) |
39 #define LOG_INFO(msg, ...) | 39 #define LOG_INFO(msg, ...) |
40 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) | 40 #endif // defined(SOCKET_LOG_INFO) || defined(SOCKET_LOG_ERROR) |
41 | 41 |
42 namespace dart { | 42 namespace dart { |
43 namespace bin { | 43 namespace bin { |
44 | 44 |
45 Socket::Socket(intptr_t fd) | 45 Socket::Socket(intptr_t fd) |
46 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} | 46 : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {} |
47 | 47 |
48 | |
49 void Socket::SetClosedFd() { | 48 void Socket::SetClosedFd() { |
50 ASSERT(fd_ != kClosedFd); | 49 ASSERT(fd_ != kClosedFd); |
51 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); | 50 IOHandle* handle = reinterpret_cast<IOHandle*>(fd_); |
52 ASSERT(handle != NULL); | 51 ASSERT(handle != NULL); |
53 handle->Release(); | 52 handle->Release(); |
54 fd_ = kClosedFd; | 53 fd_ = kClosedFd; |
55 } | 54 } |
56 | 55 |
57 | |
58 static intptr_t Create(const RawAddr& addr) { | 56 static intptr_t Create(const RawAddr& addr) { |
59 LOG_INFO("Create: calling socket(SOCK_STREAM)\n"); | 57 LOG_INFO("Create: calling socket(SOCK_STREAM)\n"); |
60 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 58 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
61 if (fd < 0) { | 59 if (fd < 0) { |
62 LOG_ERR("Create: socket(SOCK_STREAM) failed\n"); | 60 LOG_ERR("Create: socket(SOCK_STREAM) failed\n"); |
63 return -1; | 61 return -1; |
64 } | 62 } |
65 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd); | 63 LOG_INFO("Create: socket(SOCK_STREAM) -> fd %ld\n", fd); |
66 if (!FDUtils::SetCloseOnExec(fd)) { | 64 if (!FDUtils::SetCloseOnExec(fd)) { |
67 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd); | 65 LOG_ERR("Create: FDUtils::SetCloseOnExec(%ld) failed\n", fd); |
68 FDUtils::SaveErrorAndClose(fd); | 66 FDUtils::SaveErrorAndClose(fd); |
69 return -1; | 67 return -1; |
70 } | 68 } |
71 IOHandle* io_handle = new IOHandle(fd); | 69 IOHandle* io_handle = new IOHandle(fd); |
72 return reinterpret_cast<intptr_t>(io_handle); | 70 return reinterpret_cast<intptr_t>(io_handle); |
73 } | 71 } |
74 | 72 |
75 | |
76 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { | 73 static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
77 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); | 74 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
78 LOG_INFO("Connect: calling connect(%ld)\n", handle->fd()); | 75 LOG_INFO("Connect: calling connect(%ld)\n", handle->fd()); |
79 intptr_t result = NO_RETRY_EXPECTED( | 76 intptr_t result = NO_RETRY_EXPECTED( |
80 connect(handle->fd(), &addr.addr, SocketAddress::GetAddrLength(addr))); | 77 connect(handle->fd(), &addr.addr, SocketAddress::GetAddrLength(addr))); |
81 if ((result == 0) || (errno == EINPROGRESS)) { | 78 if ((result == 0) || (errno == EINPROGRESS)) { |
82 return reinterpret_cast<intptr_t>(handle); | 79 return reinterpret_cast<intptr_t>(handle); |
83 } | 80 } |
84 LOG_ERR("Connect: connect(%ld) failed\n", handle->fd()); | 81 LOG_ERR("Connect: connect(%ld) failed\n", handle->fd()); |
85 FDUtils::SaveErrorAndClose(handle->fd()); | 82 FDUtils::SaveErrorAndClose(handle->fd()); |
86 handle->Release(); | 83 handle->Release(); |
87 return -1; | 84 return -1; |
88 } | 85 } |
89 | 86 |
90 | |
91 intptr_t Socket::CreateConnect(const RawAddr& addr) { | 87 intptr_t Socket::CreateConnect(const RawAddr& addr) { |
92 intptr_t fd = Create(addr); | 88 intptr_t fd = Create(addr); |
93 if (fd < 0) { | 89 if (fd < 0) { |
94 return fd; | 90 return fd; |
95 } | 91 } |
96 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); | 92 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
97 if (!FDUtils::SetNonBlocking(handle->fd())) { | 93 if (!FDUtils::SetNonBlocking(handle->fd())) { |
98 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", | 94 LOG_ERR("CreateConnect: FDUtils::SetNonBlocking(%ld) failed\n", |
99 handle->fd()); | 95 handle->fd()); |
100 FDUtils::SaveErrorAndClose(handle->fd()); | 96 FDUtils::SaveErrorAndClose(handle->fd()); |
101 handle->Release(); | 97 handle->Release(); |
102 return -1; | 98 return -1; |
103 } | 99 } |
104 return Connect(fd, addr); | 100 return Connect(fd, addr); |
105 } | 101 } |
106 | 102 |
107 | |
108 intptr_t Socket::CreateBindConnect(const RawAddr& addr, | 103 intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
109 const RawAddr& source_addr) { | 104 const RawAddr& source_addr) { |
110 LOG_ERR("SocketBase::CreateBindConnect is unimplemented\n"); | 105 LOG_ERR("SocketBase::CreateBindConnect is unimplemented\n"); |
111 UNIMPLEMENTED(); | 106 UNIMPLEMENTED(); |
112 return -1; | 107 return -1; |
113 } | 108 } |
114 | 109 |
115 | |
116 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { | 110 intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
117 LOG_ERR("SocketBase::CreateBindDatagram is unimplemented\n"); | 111 LOG_ERR("SocketBase::CreateBindDatagram is unimplemented\n"); |
118 UNIMPLEMENTED(); | 112 UNIMPLEMENTED(); |
119 return -1; | 113 return -1; |
120 } | 114 } |
121 | 115 |
122 | |
123 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, | 116 intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
124 intptr_t backlog, | 117 intptr_t backlog, |
125 bool v6_only) { | 118 bool v6_only) { |
126 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n"); | 119 LOG_INFO("ServerSocket::CreateBindListen: calling socket(SOCK_STREAM)\n"); |
127 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); | 120 intptr_t fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0)); |
128 if (fd < 0) { | 121 if (fd < 0) { |
129 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n"); | 122 LOG_ERR("ServerSocket::CreateBindListen: socket() failed\n"); |
130 return -1; | 123 return -1; |
131 } | 124 } |
132 LOG_INFO("ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd); | 125 LOG_INFO("ServerSocket::CreateBindListen: socket(SOCK_STREAM) -> %ld\n", fd); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 175 |
183 if (!FDUtils::SetNonBlocking(fd)) { | 176 if (!FDUtils::SetNonBlocking(fd)) { |
184 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd); | 177 LOG_ERR("CreateBindListen: FDUtils::SetNonBlocking(%ld) failed\n", fd); |
185 FDUtils::SaveErrorAndClose(fd); | 178 FDUtils::SaveErrorAndClose(fd); |
186 io_handle->Release(); | 179 io_handle->Release(); |
187 return -1; | 180 return -1; |
188 } | 181 } |
189 return reinterpret_cast<intptr_t>(io_handle); | 182 return reinterpret_cast<intptr_t>(io_handle); |
190 } | 183 } |
191 | 184 |
192 | |
193 bool ServerSocket::StartAccept(intptr_t fd) { | 185 bool ServerSocket::StartAccept(intptr_t fd) { |
194 USE(fd); | 186 USE(fd); |
195 return true; | 187 return true; |
196 } | 188 } |
197 | 189 |
198 | |
199 static bool IsTemporaryAcceptError(int error) { | 190 static bool IsTemporaryAcceptError(int error) { |
200 // On Linux a number of protocol errors should be treated as EAGAIN. | 191 // On Linux a number of protocol errors should be treated as EAGAIN. |
201 // These are the ones for TCP/IP. | 192 // These are the ones for TCP/IP. |
202 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || | 193 return (error == EAGAIN) || (error == ENETDOWN) || (error == EPROTO) || |
203 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || | 194 (error == ENOPROTOOPT) || (error == EHOSTDOWN) || (error == ENONET) || |
204 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || | 195 (error == EHOSTUNREACH) || (error == EOPNOTSUPP) || |
205 (error == ENETUNREACH); | 196 (error == ENETUNREACH); |
206 } | 197 } |
207 | 198 |
208 | |
209 intptr_t ServerSocket::Accept(intptr_t fd) { | 199 intptr_t ServerSocket::Accept(intptr_t fd) { |
210 IOHandle* listen_handle = reinterpret_cast<IOHandle*>(fd); | 200 IOHandle* listen_handle = reinterpret_cast<IOHandle*>(fd); |
211 intptr_t socket; | 201 intptr_t socket; |
212 struct sockaddr clientaddr; | 202 struct sockaddr clientaddr; |
213 socklen_t addrlen = sizeof(clientaddr); | 203 socklen_t addrlen = sizeof(clientaddr); |
214 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", listen_fd); | 204 LOG_INFO("ServerSocket::Accept: calling accept(%ld)\n", listen_fd); |
215 socket = listen_handle->Accept(&clientaddr, &addrlen); | 205 socket = listen_handle->Accept(&clientaddr, &addrlen); |
216 if (socket == -1) { | 206 if (socket == -1) { |
217 if (IsTemporaryAcceptError(errno)) { | 207 if (IsTemporaryAcceptError(errno)) { |
218 // We need to signal to the caller that this is actually not an | 208 // We need to signal to the caller that this is actually not an |
(...skipping 24 matching lines...) Expand all Loading... |
243 } | 233 } |
244 return socket; | 234 return socket; |
245 } | 235 } |
246 | 236 |
247 } // namespace bin | 237 } // namespace bin |
248 } // namespace dart | 238 } // namespace dart |
249 | 239 |
250 #endif // defined(HOST_OS_FUCHSIA) | 240 #endif // defined(HOST_OS_FUCHSIA) |
251 | 241 |
252 #endif // !defined(DART_IO_DISABLED) | 242 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |