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_base.h" | 10 #include "bin/socket_base.h" |
11 | 11 |
12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
13 #include <fcntl.h> // NOLINT | 13 #include <fcntl.h> // NOLINT |
14 #include <ifaddrs.h> // NOLINT | 14 #include <ifaddrs.h> // NOLINT |
15 #include <net/if.h> // NOLINT | 15 #include <net/if.h> // NOLINT |
16 #include <netinet/tcp.h> // NOLINT | 16 #include <netinet/tcp.h> // NOLINT |
17 #include <stdio.h> // NOLINT | 17 #include <stdio.h> // NOLINT |
18 #include <stdlib.h> // NOLINT | 18 #include <stdlib.h> // NOLINT |
19 #include <string.h> // NOLINT | 19 #include <string.h> // NOLINT |
20 #include <sys/ioctl.h> // NOLINT | 20 #include <sys/ioctl.h> // NOLINT |
21 #include <sys/stat.h> // NOLINT | 21 #include <sys/stat.h> // NOLINT |
22 #include <unistd.h> // NOLINT | 22 #include <unistd.h> // NOLINT |
23 | 23 |
| 24 #include "bin/eventhandler.h" |
24 #include "bin/fdutils.h" | 25 #include "bin/fdutils.h" |
25 #include "bin/file.h" | 26 #include "bin/file.h" |
26 #include "bin/socket_base_fuchsia.h" | 27 #include "bin/socket_base_fuchsia.h" |
27 #include "platform/signal_blocker.h" | 28 #include "platform/signal_blocker.h" |
28 | 29 |
29 // #define SOCKET_LOG_INFO 1 | 30 // #define SOCKET_LOG_INFO 1 |
30 // #define SOCKET_LOG_ERROR 1 | 31 // #define SOCKET_LOG_ERROR 1 |
31 | 32 |
32 // define SOCKET_LOG_ERROR to get log messages only for errors. | 33 // define SOCKET_LOG_ERROR to get log messages only for errors. |
33 // define SOCKET_LOG_INFO to get log messages for both information and errors. | 34 // define SOCKET_LOG_INFO to get log messages for both information and errors. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 } | 81 } |
81 | 82 |
82 | 83 |
83 bool SocketBase::IsBindError(intptr_t error_number) { | 84 bool SocketBase::IsBindError(intptr_t error_number) { |
84 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || | 85 return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL || |
85 error_number == EINVAL; | 86 error_number == EINVAL; |
86 } | 87 } |
87 | 88 |
88 | 89 |
89 intptr_t SocketBase::Available(intptr_t fd) { | 90 intptr_t SocketBase::Available(intptr_t fd) { |
90 intptr_t available = FDUtils::AvailableBytes(fd); | 91 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
91 LOG_INFO("SocketBase::Available(%ld) = %ld\n", fd, available); | 92 ASSERT(handle->fd() >= 0); |
| 93 intptr_t available = FDUtils::AvailableBytes(handle->fd()); |
| 94 LOG_INFO("SocketBase::Available(%ld) = %ld\n", handle->fd(), available); |
92 return available; | 95 return available; |
93 } | 96 } |
94 | 97 |
95 | 98 |
96 intptr_t SocketBase::Read(intptr_t fd, | 99 intptr_t SocketBase::Read(intptr_t fd, |
97 void* buffer, | 100 void* buffer, |
98 intptr_t num_bytes, | 101 intptr_t num_bytes, |
99 SocketOpKind sync) { | 102 SocketOpKind sync) { |
100 ASSERT(fd >= 0); | 103 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
101 LOG_INFO("SocketBase::Read: calling read(%ld, %p, %ld)\n", fd, buffer, | 104 ASSERT(handle->fd() >= 0); |
102 num_bytes); | 105 LOG_INFO("SocketBase::Read: calling read(%ld, %p, %ld)\n", handle->fd(), |
103 ssize_t read_bytes = NO_RETRY_EXPECTED(read(fd, buffer, num_bytes)); | 106 buffer, num_bytes); |
| 107 intptr_t read_bytes = handle->Read(buffer, num_bytes); |
104 ASSERT(EAGAIN == EWOULDBLOCK); | 108 ASSERT(EAGAIN == EWOULDBLOCK); |
105 if ((sync == kAsync) && (read_bytes == -1) && (errno == EWOULDBLOCK)) { | 109 if ((sync == kAsync) && (read_bytes == -1) && (errno == EWOULDBLOCK)) { |
106 // If the read would block we need to retry and therefore return 0 | 110 // If the read would block we need to retry and therefore return 0 |
107 // as the number of bytes written. | 111 // as the number of bytes written. |
108 read_bytes = 0; | 112 read_bytes = 0; |
109 } else if (read_bytes == -1) { | 113 } else if (read_bytes == -1) { |
110 LOG_ERR("SocketBase::Read: read(%ld, %p, %ld) failed\n", fd, buffer, | 114 LOG_ERR("SocketBase::Read: read(%ld, %p, %ld) failed\n", handle->fd(), |
111 num_bytes); | 115 buffer, num_bytes); |
112 } else { | 116 } else { |
113 LOG_INFO("SocketBase::Read: read(%ld, %p, %ld) succeeded\n", fd, buffer, | 117 LOG_INFO("SocketBase::Read: read(%ld, %p, %ld) succeeded\n", handle->fd(), |
114 num_bytes); | 118 buffer, num_bytes); |
115 } | 119 } |
116 return read_bytes; | 120 return read_bytes; |
117 } | 121 } |
118 | 122 |
119 | 123 |
120 intptr_t SocketBase::RecvFrom(intptr_t fd, | 124 intptr_t SocketBase::RecvFrom(intptr_t fd, |
121 void* buffer, | 125 void* buffer, |
122 intptr_t num_bytes, | 126 intptr_t num_bytes, |
123 RawAddr* addr, | 127 RawAddr* addr, |
124 SocketOpKind sync) { | 128 SocketOpKind sync) { |
125 LOG_ERR("SocketBase::RecvFrom is unimplemented\n"); | 129 LOG_ERR("SocketBase::RecvFrom is unimplemented\n"); |
126 UNIMPLEMENTED(); | 130 UNIMPLEMENTED(); |
127 return -1; | 131 return -1; |
128 } | 132 } |
129 | 133 |
130 | 134 |
131 intptr_t SocketBase::Write(intptr_t fd, | 135 intptr_t SocketBase::Write(intptr_t fd, |
132 const void* buffer, | 136 const void* buffer, |
133 intptr_t num_bytes, | 137 intptr_t num_bytes, |
134 SocketOpKind sync) { | 138 SocketOpKind sync) { |
135 ASSERT(fd >= 0); | 139 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
136 LOG_INFO("SocketBase::Write: calling write(%ld, %p, %ld)\n", fd, buffer, | 140 ASSERT(handle->fd() >= 0); |
137 num_bytes); | 141 LOG_INFO("SocketBase::Write: calling write(%ld, %p, %ld)\n", handle->fd(), |
138 ssize_t written_bytes = NO_RETRY_EXPECTED(write(fd, buffer, num_bytes)); | 142 buffer, num_bytes); |
| 143 intptr_t written_bytes = handle->Write(buffer, num_bytes); |
139 ASSERT(EAGAIN == EWOULDBLOCK); | 144 ASSERT(EAGAIN == EWOULDBLOCK); |
140 if ((sync == kAsync) && (written_bytes == -1) && (errno == EWOULDBLOCK)) { | 145 if ((sync == kAsync) && (written_bytes == -1) && (errno == EWOULDBLOCK)) { |
141 // If the would block we need to retry and therefore return 0 as | 146 // If the would block we need to retry and therefore return 0 as |
142 // the number of bytes written. | 147 // the number of bytes written. |
143 written_bytes = 0; | 148 written_bytes = 0; |
144 } else if (written_bytes == -1) { | 149 } else if (written_bytes == -1) { |
145 LOG_ERR("SocketBase::Write: write(%ld, %p, %ld) failed\n", fd, buffer, | 150 LOG_ERR("SocketBase::Write: write(%ld, %p, %ld) failed\n", handle->fd(), |
146 num_bytes); | 151 buffer, num_bytes); |
147 } else { | 152 } else { |
148 LOG_INFO("SocketBase::Write: write(%ld, %p, %ld) succeeded\n", fd, buffer, | 153 LOG_INFO("SocketBase::Write: write(%ld, %p, %ld) succeeded\n", handle->fd(), |
149 num_bytes); | 154 buffer, num_bytes); |
150 } | 155 } |
151 return written_bytes; | 156 return written_bytes; |
152 } | 157 } |
153 | 158 |
154 | 159 |
155 intptr_t SocketBase::SendTo(intptr_t fd, | 160 intptr_t SocketBase::SendTo(intptr_t fd, |
156 const void* buffer, | 161 const void* buffer, |
157 intptr_t num_bytes, | 162 intptr_t num_bytes, |
158 const RawAddr& addr, | 163 const RawAddr& addr, |
159 SocketOpKind sync) { | 164 SocketOpKind sync) { |
160 LOG_ERR("SocketBase::SendTo is unimplemented\n"); | 165 LOG_ERR("SocketBase::SendTo is unimplemented\n"); |
161 UNIMPLEMENTED(); | 166 UNIMPLEMENTED(); |
162 return -1; | 167 return -1; |
163 } | 168 } |
164 | 169 |
165 | 170 |
166 intptr_t SocketBase::GetPort(intptr_t fd) { | 171 intptr_t SocketBase::GetPort(intptr_t fd) { |
167 ASSERT(fd >= 0); | 172 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
| 173 ASSERT(handle->fd() >= 0); |
168 RawAddr raw; | 174 RawAddr raw; |
169 socklen_t size = sizeof(raw); | 175 socklen_t size = sizeof(raw); |
170 LOG_INFO("SocketBase::GetPort: calling getsockname(%ld)\n", fd); | 176 LOG_INFO("SocketBase::GetPort: calling getsockname(%ld)\n", handle->fd()); |
171 if (NO_RETRY_EXPECTED(getsockname(fd, &raw.addr, &size))) { | 177 if (NO_RETRY_EXPECTED(getsockname(handle->fd(), &raw.addr, &size))) { |
172 return 0; | 178 return 0; |
173 } | 179 } |
174 return SocketAddress::GetAddrPort(raw); | 180 return SocketAddress::GetAddrPort(raw); |
175 } | 181 } |
176 | 182 |
177 | 183 |
178 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { | 184 SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) { |
179 ASSERT(fd >= 0); | 185 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
| 186 ASSERT(handle->fd() >= 0); |
180 RawAddr raw; | 187 RawAddr raw; |
181 socklen_t size = sizeof(raw); | 188 socklen_t size = sizeof(raw); |
182 if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { | 189 if (NO_RETRY_EXPECTED(getpeername(handle->fd(), &raw.addr, &size))) { |
183 return NULL; | 190 return NULL; |
184 } | 191 } |
185 *port = SocketAddress::GetAddrPort(raw); | 192 *port = SocketAddress::GetAddrPort(raw); |
186 return new SocketAddress(&raw.addr); | 193 return new SocketAddress(&raw.addr); |
187 } | 194 } |
188 | 195 |
189 | 196 |
190 void SocketBase::GetError(intptr_t fd, OSError* os_error) { | 197 void SocketBase::GetError(intptr_t fd, OSError* os_error) { |
191 LOG_ERR("SocketBase::GetError is unimplemented\n"); | 198 LOG_ERR("SocketBase::GetError is unimplemented\n"); |
192 UNIMPLEMENTED(); | 199 UNIMPLEMENTED(); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 | 289 |
283 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( | 290 AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces( |
284 int type, | 291 int type, |
285 OSError** os_error) { | 292 OSError** os_error) { |
286 UNIMPLEMENTED(); | 293 UNIMPLEMENTED(); |
287 return NULL; | 294 return NULL; |
288 } | 295 } |
289 | 296 |
290 | 297 |
291 void SocketBase::Close(intptr_t fd) { | 298 void SocketBase::Close(intptr_t fd) { |
292 ASSERT(fd >= 0); | 299 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
293 NO_RETRY_EXPECTED(close(fd)); | 300 ASSERT(handle->fd() >= 0); |
| 301 NO_RETRY_EXPECTED(close(handle->fd())); |
294 } | 302 } |
295 | 303 |
296 | 304 |
297 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { | 305 bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) { |
298 LOG_ERR("SocketBase::GetNoDelay is unimplemented\n"); | 306 LOG_ERR("SocketBase::GetNoDelay is unimplemented\n"); |
299 UNIMPLEMENTED(); | 307 UNIMPLEMENTED(); |
300 return false; | 308 return false; |
301 } | 309 } |
302 | 310 |
303 | 311 |
304 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { | 312 bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) { |
| 313 IOHandle* handle = reinterpret_cast<IOHandle*>(fd); |
305 int on = enabled ? 1 : 0; | 314 int on = enabled ? 1 : 0; |
306 return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, | 315 return NO_RETRY_EXPECTED(setsockopt(handle->fd(), IPPROTO_TCP, TCP_NODELAY, |
307 reinterpret_cast<char*>(&on), | 316 reinterpret_cast<char*>(&on), |
308 sizeof(on))) == 0; | 317 sizeof(on))) == 0; |
309 } | 318 } |
310 | 319 |
311 | 320 |
312 bool SocketBase::GetMulticastLoop(intptr_t fd, | 321 bool SocketBase::GetMulticastLoop(intptr_t fd, |
313 intptr_t protocol, | 322 intptr_t protocol, |
314 bool* enabled) { | 323 bool* enabled) { |
315 LOG_ERR("SocketBase::GetMulticastLoop is unimplemented\n"); | 324 LOG_ERR("SocketBase::GetMulticastLoop is unimplemented\n"); |
316 UNIMPLEMENTED(); | 325 UNIMPLEMENTED(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 UNIMPLEMENTED(); | 382 UNIMPLEMENTED(); |
374 return false; | 383 return false; |
375 } | 384 } |
376 | 385 |
377 } // namespace bin | 386 } // namespace bin |
378 } // namespace dart | 387 } // namespace dart |
379 | 388 |
380 #endif // defined(HOST_OS_FUCHSIA) | 389 #endif // defined(HOST_OS_FUCHSIA) |
381 | 390 |
382 #endif // !defined(DART_IO_DISABLED) | 391 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |