| 1 | // Copyright (c) 2017, the Dart project authors.  Please see the AUTHORS file | 
|---|
| 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. | 
|---|
| 4 |  | 
|---|
| 5 | #include "bin/sync_socket.h" | 
|---|
| 6 |  | 
|---|
| 7 | #include "bin/dartutils.h" | 
|---|
| 8 | #include "bin/io_buffer.h" | 
|---|
| 9 | #include "bin/isolate_data.h" | 
|---|
| 10 | #include "bin/lockers.h" | 
|---|
| 11 | #include "bin/thread.h" | 
|---|
| 12 | #include "bin/utils.h" | 
|---|
| 13 |  | 
|---|
| 14 | #include "include/dart_api.h" | 
|---|
| 15 |  | 
|---|
| 16 | #include "platform/globals.h" | 
|---|
| 17 | #include "platform/utils.h" | 
|---|
| 18 |  | 
|---|
| 19 | #define DART_CHECK_ERROR_AND_CLEANUP(handle, ptr)                              \ | 
|---|
| 20 | do {                                                                         \ | 
|---|
| 21 | if (Dart_IsError((handle))) {                                              \ | 
|---|
| 22 | delete (ptr);                                                            \ | 
|---|
| 23 | Dart_SetReturnValue(args, (handle));                                     \ | 
|---|
| 24 | return;                                                                  \ | 
|---|
| 25 | }                                                                          \ | 
|---|
| 26 | } while (0) | 
|---|
| 27 |  | 
|---|
| 28 | #define DART_CHECK_ERROR(handle)                                               \ | 
|---|
| 29 | do {                                                                         \ | 
|---|
| 30 | if (Dart_IsError((handle))) {                                              \ | 
|---|
| 31 | Dart_SetReturnValue(args, (handle));                                     \ | 
|---|
| 32 | return;                                                                  \ | 
|---|
| 33 | }                                                                          \ | 
|---|
| 34 | } while (0) | 
|---|
| 35 |  | 
|---|
| 36 | namespace dart { | 
|---|
| 37 | namespace bin { | 
|---|
| 38 |  | 
|---|
| 39 | static const int kSocketIdNativeField = 0; | 
|---|
| 40 |  | 
|---|
| 41 | void FUNCTION_NAME(SynchronousSocket_LookupRequest)(Dart_NativeArguments args) { | 
|---|
| 42 | if (Dart_GetNativeArgumentCount(args) != 2) { | 
|---|
| 43 | Dart_SetReturnValue( | 
|---|
| 44 | args, DartUtils::NewDartArgumentError( "Invalid argument count.")); | 
|---|
| 45 | return; | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | char* peer = NULL; | 
|---|
| 49 | Dart_Handle host_arg = | 
|---|
| 50 | Dart_GetNativeStringArgument(args, 0, reinterpret_cast<void**>(&peer)); | 
|---|
| 51 | DART_CHECK_ERROR(host_arg); | 
|---|
| 52 |  | 
|---|
| 53 | char* host = NULL; | 
|---|
| 54 | host_arg = Dart_StringToCString(host_arg, const_cast<const char**>(&host)); | 
|---|
| 55 | DART_CHECK_ERROR(host_arg); | 
|---|
| 56 |  | 
|---|
| 57 | int64_t type = 0; | 
|---|
| 58 | Dart_Handle port_error = Dart_GetNativeIntegerArgument(args, 1, &type); | 
|---|
| 59 | DART_CHECK_ERROR(port_error); | 
|---|
| 60 |  | 
|---|
| 61 | OSError* os_error = NULL; | 
|---|
| 62 | AddressList<SocketAddress>* addresses = | 
|---|
| 63 | SocketBase::LookupAddress(host, type, &os_error); | 
|---|
| 64 | if (addresses == NULL) { | 
|---|
| 65 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(os_error)); | 
|---|
| 66 | return; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | Dart_Handle array = Dart_NewList(addresses->count()); | 
|---|
| 70 | DART_CHECK_ERROR_AND_CLEANUP(array, addresses); | 
|---|
| 71 |  | 
|---|
| 72 | for (intptr_t i = 0; i < addresses->count(); i++) { | 
|---|
| 73 | SocketAddress* addr = addresses->GetAt(i); | 
|---|
| 74 | Dart_Handle entry = Dart_NewList(3); | 
|---|
| 75 | DART_CHECK_ERROR_AND_CLEANUP(entry, addresses); | 
|---|
| 76 |  | 
|---|
| 77 | Dart_Handle type = Dart_NewInteger(addr->GetType()); | 
|---|
| 78 | DART_CHECK_ERROR_AND_CLEANUP(type, addresses); | 
|---|
| 79 | Dart_Handle error = Dart_ListSetAt(entry, 0, type); | 
|---|
| 80 | DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | 
|---|
| 81 |  | 
|---|
| 82 | Dart_Handle as_string = Dart_NewStringFromCString(addr->as_string()); | 
|---|
| 83 | DART_CHECK_ERROR_AND_CLEANUP(as_string, addresses); | 
|---|
| 84 | error = Dart_ListSetAt(entry, 1, as_string); | 
|---|
| 85 | DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | 
|---|
| 86 |  | 
|---|
| 87 | RawAddr raw = addr->addr(); | 
|---|
| 88 | Dart_Handle data = SocketAddress::ToTypedData(raw); | 
|---|
| 89 | DART_CHECK_ERROR_AND_CLEANUP(data, addresses); | 
|---|
| 90 |  | 
|---|
| 91 | error = Dart_ListSetAt(entry, 2, data); | 
|---|
| 92 | DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | 
|---|
| 93 | error = Dart_ListSetAt(array, i, entry); | 
|---|
| 94 | DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | 
|---|
| 95 | } | 
|---|
| 96 | delete addresses; | 
|---|
| 97 | Dart_SetReturnValue(args, array); | 
|---|
| 98 | return; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | void FUNCTION_NAME(SynchronousSocket_CreateConnectSync)( | 
|---|
| 102 | Dart_NativeArguments args) { | 
|---|
| 103 | RawAddr addr; | 
|---|
| 104 | SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 
|---|
| 105 | Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 
|---|
| 106 | DART_CHECK_ERROR(port_arg); | 
|---|
| 107 | int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 
|---|
| 108 | SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 
|---|
| 109 | intptr_t socket = SynchronousSocket::CreateConnect(addr); | 
|---|
| 110 | if (socket >= 0) { | 
|---|
| 111 | Dart_Handle error = SynchronousSocket::SetSocketIdNativeField( | 
|---|
| 112 | Dart_GetNativeArgument(args, 0), new SynchronousSocket(socket)); | 
|---|
| 113 | DART_CHECK_ERROR(error); | 
|---|
| 114 | Dart_SetBooleanReturnValue(args, true); | 
|---|
| 115 | } else { | 
|---|
| 116 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 117 | } | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | void FUNCTION_NAME(SynchronousSocket_WriteList)(Dart_NativeArguments args) { | 
|---|
| 121 | SynchronousSocket* socket = NULL; | 
|---|
| 122 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 123 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 124 | DART_CHECK_ERROR(result); | 
|---|
| 125 |  | 
|---|
| 126 | Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 
|---|
| 127 | if (!Dart_IsList(buffer_obj)) { | 
|---|
| 128 | Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | 
|---|
| 129 | "First parameter must be a List<int>")); | 
|---|
| 130 | return; | 
|---|
| 131 | } | 
|---|
| 132 | intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 
|---|
| 133 | intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 
|---|
| 134 | Dart_TypedData_Type type; | 
|---|
| 135 | uint8_t* buffer = NULL; | 
|---|
| 136 | intptr_t len; | 
|---|
| 137 | result = Dart_TypedDataAcquireData(buffer_obj, &type, | 
|---|
| 138 | reinterpret_cast<void**>(&buffer), &len); | 
|---|
| 139 | DART_CHECK_ERROR(result); | 
|---|
| 140 | ASSERT((offset + length) <= len); | 
|---|
| 141 | buffer += offset; | 
|---|
| 142 | intptr_t bytes_written = | 
|---|
| 143 | SynchronousSocket::Write(socket->fd(), buffer, length); | 
|---|
| 144 | Dart_TypedDataReleaseData(buffer_obj); | 
|---|
| 145 | if (bytes_written >= 0) { | 
|---|
| 146 | Dart_SetIntegerReturnValue(args, bytes_written); | 
|---|
| 147 | } else { | 
|---|
| 148 | OSError os_error; | 
|---|
| 149 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 
|---|
| 150 | } | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | void FUNCTION_NAME(SynchronousSocket_ReadList)(Dart_NativeArguments args) { | 
|---|
| 154 | SynchronousSocket* socket = NULL; | 
|---|
| 155 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 156 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 157 | DART_CHECK_ERROR(result); | 
|---|
| 158 |  | 
|---|
| 159 | Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 
|---|
| 160 | if (!Dart_IsList(buffer_obj)) { | 
|---|
| 161 | Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | 
|---|
| 162 | "First parameter must be a List<int>")); | 
|---|
| 163 | return; | 
|---|
| 164 | } | 
|---|
| 165 | intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | 
|---|
| 166 | intptr_t bytes = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | 
|---|
| 167 | intptr_t array_len = 0; | 
|---|
| 168 |  | 
|---|
| 169 | result = Dart_ListLength(buffer_obj, &array_len); | 
|---|
| 170 | DART_CHECK_ERROR(result); | 
|---|
| 171 |  | 
|---|
| 172 | uint8_t* buffer = Dart_ScopeAllocate(bytes); | 
|---|
| 173 | intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, bytes); | 
|---|
| 174 | if (bytes_read < 0) { | 
|---|
| 175 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 176 | return; | 
|---|
| 177 | } | 
|---|
| 178 | if (bytes_read > 0) { | 
|---|
| 179 | result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | 
|---|
| 180 | DART_CHECK_ERROR(result); | 
|---|
| 181 | } | 
|---|
| 182 | Dart_SetIntegerReturnValue(args, bytes_read); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) { | 
|---|
| 186 | SynchronousSocket* socket = NULL; | 
|---|
| 187 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 188 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 189 | DART_CHECK_ERROR(result); | 
|---|
| 190 |  | 
|---|
| 191 | intptr_t available = SynchronousSocket::Available(socket->fd()); | 
|---|
| 192 | if (available >= 0) { | 
|---|
| 193 | Dart_SetIntegerReturnValue(args, available); | 
|---|
| 194 | } else { | 
|---|
| 195 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 196 | } | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) { | 
|---|
| 200 | SynchronousSocket* socket = NULL; | 
|---|
| 201 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 202 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 203 | DART_CHECK_ERROR(result); | 
|---|
| 204 |  | 
|---|
| 205 | SynchronousSocket::Close(socket->fd()); | 
|---|
| 206 | socket->SetClosedFd(); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) { | 
|---|
| 210 | SynchronousSocket* socket = NULL; | 
|---|
| 211 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 212 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 213 | DART_CHECK_ERROR(result); | 
|---|
| 214 |  | 
|---|
| 215 | int64_t length = 0; | 
|---|
| 216 | if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length) || | 
|---|
| 217 | (length < 0)) { | 
|---|
| 218 | Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | 
|---|
| 219 | "First parameter must be an integer.")); | 
|---|
| 220 | return; | 
|---|
| 221 | } | 
|---|
| 222 | uint8_t* buffer = NULL; | 
|---|
| 223 | result = IOBuffer::Allocate(length, &buffer); | 
|---|
| 224 | if (Dart_IsNull(result)) { | 
|---|
| 225 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 226 | return; | 
|---|
| 227 | } | 
|---|
| 228 | ASSERT(buffer != NULL); | 
|---|
| 229 | intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, length); | 
|---|
| 230 | if (bytes_read == length) { | 
|---|
| 231 | Dart_SetReturnValue(args, result); | 
|---|
| 232 | } else if (bytes_read > 0) { | 
|---|
| 233 | uint8_t* new_buffer = NULL; | 
|---|
| 234 | Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 
|---|
| 235 | if (Dart_IsNull(new_result)) { | 
|---|
| 236 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 237 | return; | 
|---|
| 238 | } | 
|---|
| 239 | ASSERT(new_buffer != NULL); | 
|---|
| 240 | memmove(new_buffer, buffer, bytes_read); | 
|---|
| 241 | Dart_SetReturnValue(args, new_result); | 
|---|
| 242 | } else if (bytes_read == -1) { | 
|---|
| 243 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 244 | } | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | void FUNCTION_NAME(SynchronousSocket_ShutdownRead)(Dart_NativeArguments args) { | 
|---|
| 248 | SynchronousSocket* socket = NULL; | 
|---|
| 249 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 250 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 251 | DART_CHECK_ERROR(result); | 
|---|
| 252 |  | 
|---|
| 253 | SynchronousSocket::ShutdownRead(socket->fd()); | 
|---|
| 254 | } | 
|---|
| 255 |  | 
|---|
| 256 | void FUNCTION_NAME(SynchronousSocket_ShutdownWrite)(Dart_NativeArguments args) { | 
|---|
| 257 | SynchronousSocket* socket = NULL; | 
|---|
| 258 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 259 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 260 | DART_CHECK_ERROR(result); | 
|---|
| 261 |  | 
|---|
| 262 | SynchronousSocket::ShutdownWrite(socket->fd()); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) { | 
|---|
| 266 | SynchronousSocket* socket = NULL; | 
|---|
| 267 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 268 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 269 | DART_CHECK_ERROR(result); | 
|---|
| 270 |  | 
|---|
| 271 | intptr_t port = SynchronousSocket::GetPort(socket->fd()); | 
|---|
| 272 | if (port > 0) { | 
|---|
| 273 | Dart_SetReturnValue(args, Dart_NewInteger(port)); | 
|---|
| 274 | } else { | 
|---|
| 275 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 276 | } | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) { | 
|---|
| 280 | SynchronousSocket* socket = NULL; | 
|---|
| 281 | Dart_Handle result = SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 282 | Dart_GetNativeArgument(args, 0), &socket); | 
|---|
| 283 | DART_CHECK_ERROR(result); | 
|---|
| 284 |  | 
|---|
| 285 | intptr_t port = 0; | 
|---|
| 286 | SocketAddress* addr = SynchronousSocket::GetRemotePeer(socket->fd(), &port); | 
|---|
| 287 | if (addr == NULL) { | 
|---|
| 288 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 
|---|
| 289 | return; | 
|---|
| 290 | } | 
|---|
| 291 | Dart_Handle list = Dart_NewList(2); | 
|---|
| 292 | DART_CHECK_ERROR_AND_CLEANUP(list, addr); | 
|---|
| 293 |  | 
|---|
| 294 | Dart_Handle entry = Dart_NewList(3); | 
|---|
| 295 | DART_CHECK_ERROR_AND_CLEANUP(entry, addr); | 
|---|
| 296 |  | 
|---|
| 297 | Dart_Handle error = | 
|---|
| 298 | Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | 
|---|
| 299 | DART_CHECK_ERROR_AND_CLEANUP(error, addr); | 
|---|
| 300 | error = | 
|---|
| 301 | Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | 
|---|
| 302 | DART_CHECK_ERROR_AND_CLEANUP(error, addr); | 
|---|
| 303 |  | 
|---|
| 304 | RawAddr raw = addr->addr(); | 
|---|
| 305 | error = Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | 
|---|
| 306 | DART_CHECK_ERROR_AND_CLEANUP(error, addr); | 
|---|
| 307 |  | 
|---|
| 308 | error = Dart_ListSetAt(list, 0, entry); | 
|---|
| 309 | DART_CHECK_ERROR_AND_CLEANUP(error, addr); | 
|---|
| 310 | error = Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 
|---|
| 311 | DART_CHECK_ERROR_AND_CLEANUP(error, addr); | 
|---|
| 312 | Dart_SetReturnValue(args, list); | 
|---|
| 313 | delete addr; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | static void SynchronousSocketFinalizer(void* isolate_data, void* data) { | 
|---|
| 317 | SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data); | 
|---|
| 318 | if (socket->fd() >= 0) { | 
|---|
| 319 | SynchronousSocket::Close(socket->fd()); | 
|---|
| 320 | socket->SetClosedFd(); | 
|---|
| 321 | } | 
|---|
| 322 | delete socket; | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | Dart_Handle SynchronousSocket::SetSocketIdNativeField( | 
|---|
| 326 | Dart_Handle handle, | 
|---|
| 327 | SynchronousSocket* socket) { | 
|---|
| 328 | Dart_Handle error = Dart_SetNativeInstanceField( | 
|---|
| 329 | handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket)); | 
|---|
| 330 | if (Dart_IsError(error)) { | 
|---|
| 331 | delete socket; | 
|---|
| 332 | return error; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | Dart_NewFinalizableHandle(handle, reinterpret_cast<void*>(socket), | 
|---|
| 336 | sizeof(SynchronousSocket), | 
|---|
| 337 | SynchronousSocketFinalizer); | 
|---|
| 338 | return error; | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | Dart_Handle SynchronousSocket::GetSocketIdNativeField( | 
|---|
| 342 | Dart_Handle socket_obj, | 
|---|
| 343 | SynchronousSocket** socket) { | 
|---|
| 344 | ASSERT(socket != NULL); | 
|---|
| 345 | intptr_t id; | 
|---|
| 346 | Dart_Handle result = | 
|---|
| 347 | Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); | 
|---|
| 348 | if (Dart_IsError(result)) { | 
|---|
| 349 | return result; | 
|---|
| 350 | } | 
|---|
| 351 | *socket = reinterpret_cast<SynchronousSocket*>(id); | 
|---|
| 352 | if (*socket == NULL) { | 
|---|
| 353 | Dart_PropagateError(Dart_NewUnhandledExceptionError( | 
|---|
| 354 | DartUtils::NewInternalError( "No native peer"))); | 
|---|
| 355 | } | 
|---|
| 356 | return result; | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | }  // namespace bin | 
|---|
| 360 | }  // namespace dart | 
|---|
| 361 |  | 
|---|