OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 #include "bin/io_buffer.h" | 5 #include "bin/io_buffer.h" |
6 #include "bin/socket.h" | 6 #include "bin/socket.h" |
7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
8 #include "bin/thread.h" | 8 #include "bin/thread.h" |
9 #include "bin/utils.h" | 9 #include "bin/utils.h" |
10 | 10 |
11 #include "platform/globals.h" | 11 #include "platform/globals.h" |
12 #include "platform/thread.h" | 12 #include "platform/thread.h" |
13 #include "platform/utils.h" | 13 #include "platform/utils.h" |
14 | 14 |
15 #include "include/dart_api.h" | 15 #include "include/dart_api.h" |
16 | 16 |
17 static const int kSocketIdNativeField = 0; | 17 static const int kSocketIdNativeField = 0; |
18 | 18 |
19 dart::Mutex Socket::mutex_; | 19 dart::Mutex Socket::mutex_; |
20 int Socket::service_ports_size_ = 0; | 20 int Socket::service_ports_size_ = 0; |
21 Dart_Port* Socket::service_ports_ = NULL; | 21 Dart_Port* Socket::service_ports_ = NULL; |
22 int Socket::service_ports_index_ = 0; | 22 int Socket::service_ports_index_ = 0; |
23 | 23 |
24 | |
25 static Dart_Handle GetSockAddr(Dart_Handle obj, struct sockaddr_storage* addr) { | |
26 Dart_TypedData_Type data_type; | |
27 uint8_t* data = NULL; | |
28 intptr_t len; | |
29 Dart_Handle result = Dart_TypedDataAcquireData( | |
30 obj, &data_type, reinterpret_cast<void**>(&data), &len); | |
31 if (Dart_IsError(result)) return result; | |
32 memmove(reinterpret_cast<void *>(addr), data, len); | |
33 return Dart_Null(); | |
34 } | |
35 | |
36 | |
37 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 24 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
38 Dart_EnterScope(); | 25 Dart_EnterScope(); |
39 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
40 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1); | 27 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
41 struct sockaddr_storage addr; | |
42 Dart_Handle result = GetSockAddr(host_obj, &addr); | |
43 int64_t port = 0; | 28 int64_t port = 0; |
44 if (!Dart_IsError(result) && | 29 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { |
45 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { | 30 intptr_t socket = Socket::CreateConnect(host, port); |
46 intptr_t socket = Socket::CreateConnect(addr, port); | |
47 OSError error; | |
48 Dart_TypedDataReleaseData(host_obj); | |
49 if (socket >= 0) { | 31 if (socket >= 0) { |
50 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 32 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
51 if (Dart_IsError(err)) Dart_PropagateError(err); | 33 if (Dart_IsError(err)) Dart_PropagateError(err); |
52 Dart_SetReturnValue(args, Dart_True()); | 34 Dart_SetReturnValue(args, Dart_True()); |
53 } else { | 35 } else { |
54 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 36 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
55 } | 37 } |
56 } else { | 38 } else { |
57 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 39 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
58 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 40 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
59 if (Dart_IsError(err)) Dart_PropagateError(err); | 41 if (Dart_IsError(err)) Dart_PropagateError(err); |
60 Dart_SetReturnValue(args, err); | 42 Dart_SetReturnValue(args, err); |
61 } | 43 } |
62 Dart_ExitScope(); | 44 Dart_ExitScope(); |
63 } | 45 } |
64 | 46 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 244 |
263 | 245 |
264 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { | 246 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { |
265 Dart_EnterScope(); | 247 Dart_EnterScope(); |
266 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 248 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
267 intptr_t socket = 0; | 249 intptr_t socket = 0; |
268 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); | 250 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); |
269 if (Dart_IsError(err)) Dart_PropagateError(err); | 251 if (Dart_IsError(err)) Dart_PropagateError(err); |
270 OSError os_error; | 252 OSError os_error; |
271 intptr_t port = 0; | 253 intptr_t port = 0; |
272 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); | 254 char host[INET_ADDRSTRLEN]; |
273 char host[INET6_ADDRSTRLEN]; | |
274 if (Socket::GetRemotePeer(socket, host, &port)) { | 255 if (Socket::GetRemotePeer(socket, host, &port)) { |
275 Dart_Handle list = Dart_NewList(2); | 256 Dart_Handle list = Dart_NewList(2); |
276 Dart_ListSetAt(list, 0, Dart_NewStringFromCString(host)); | 257 Dart_ListSetAt(list, 0, Dart_NewStringFromCString(host)); |
277 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 258 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); |
278 Dart_SetReturnValue(args, list); | 259 Dart_SetReturnValue(args, list); |
279 } else { | 260 } else { |
280 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 261 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
281 } | 262 } |
282 Dart_ExitScope(); | 263 Dart_ExitScope(); |
283 } | 264 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 303 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
323 if (Dart_IsError(err)) Dart_PropagateError(err); | 304 if (Dart_IsError(err)) Dart_PropagateError(err); |
324 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 305 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
325 Dart_ExitScope(); | 306 Dart_ExitScope(); |
326 } | 307 } |
327 | 308 |
328 | 309 |
329 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 310 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
330 Dart_EnterScope(); | 311 Dart_EnterScope(); |
331 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 312 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
332 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1); | 313 Dart_Handle bind_address_obj = Dart_GetNativeArgument(args, 1); |
333 struct sockaddr_storage addr; | |
334 Dart_Handle result = GetSockAddr(host_obj, &addr); | |
335 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); | 314 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); |
336 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); | 315 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); |
337 int64_t port = 0; | 316 int64_t port = 0; |
338 int64_t backlog = 0; | 317 int64_t backlog = 0; |
339 if (!Dart_IsError(result) && | 318 if (Dart_IsString(bind_address_obj) && |
340 DartUtils::GetInt64Value(port_obj, &port) && | 319 DartUtils::GetInt64Value(port_obj, &port) && |
341 DartUtils::GetInt64Value(backlog_obj, &backlog)) { | 320 DartUtils::GetInt64Value(backlog_obj, &backlog)) { |
342 intptr_t socket = ServerSocket::CreateBindListen(addr, port, backlog); | 321 const char* bind_address = DartUtils::GetStringValue(bind_address_obj); |
343 OSError error; | 322 intptr_t socket = |
344 Dart_TypedDataReleaseData(host_obj); | 323 ServerSocket::CreateBindListen(bind_address, port, backlog); |
345 if (socket >= 0) { | 324 if (socket >= 0) { |
346 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 325 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
347 if (Dart_IsError(err)) Dart_PropagateError(err); | 326 if (Dart_IsError(err)) Dart_PropagateError(err); |
348 Dart_SetReturnValue(args, Dart_True()); | 327 Dart_SetReturnValue(args, Dart_True()); |
349 } else { | 328 } else { |
350 if (socket == -5) { | 329 if (socket == -5) { |
351 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 330 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
352 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
353 } else { | 332 } else { |
354 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 333 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
355 } | 334 } |
356 } | 335 } |
357 } else { | 336 } else { |
358 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 337 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
359 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 338 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
360 if (Dart_IsError(err)) Dart_PropagateError(err); | 339 if (Dart_IsError(err)) Dart_PropagateError(err); |
361 Dart_SetReturnValue(args, err); | 340 Dart_SetReturnValue(args, err); |
362 } | 341 } |
363 Dart_ExitScope(); | 342 Dart_ExitScope(); |
364 } | 343 } |
(...skipping 15 matching lines...) Expand all Loading... |
380 } else if (new_socket == ServerSocket::kTemporaryFailure) { | 359 } else if (new_socket == ServerSocket::kTemporaryFailure) { |
381 Dart_SetReturnValue(args, Dart_False()); | 360 Dart_SetReturnValue(args, Dart_False()); |
382 } else { | 361 } else { |
383 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 362 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
384 } | 363 } |
385 Dart_ExitScope(); | 364 Dart_ExitScope(); |
386 } | 365 } |
387 | 366 |
388 | 367 |
389 static CObject* LookupRequest(const CObjectArray& request) { | 368 static CObject* LookupRequest(const CObjectArray& request) { |
390 if (request.Length() == 3 && | 369 if (request.Length() == 2 && request[1]->IsString()) { |
391 request[1]->IsString() && | |
392 request[2]->IsInt32()) { | |
393 CObjectString host(request[1]); | 370 CObjectString host(request[1]); |
394 CObjectInt32 type(request[2]); | |
395 CObject* result = NULL; | 371 CObject* result = NULL; |
396 OSError* os_error = NULL; | 372 OSError* os_error = NULL; |
397 SocketAddresses* addresses = | 373 const char* ip_address = |
398 Socket::LookupAddress(host.CString(), type.Value(), &os_error); | 374 Socket::LookupIPv4Address(host.CString(), &os_error); |
399 if (addresses != NULL) { | 375 if (ip_address != NULL) { |
400 CObjectArray* array = new CObjectArray( | 376 result = new CObjectString(CObject::NewString(ip_address)); |
401 CObject::NewArray(addresses->count() + 1)); | 377 free(const_cast<char*>(ip_address)); |
402 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | |
403 for (intptr_t i = 0; i < addresses->count(); i++) { | |
404 SocketAddress* addr = addresses->GetAt(i); | |
405 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); | |
406 | |
407 CObjectInt32* type = new CObjectInt32( | |
408 CObject::NewInt32(addr->GetType())); | |
409 entry->SetAt(0, type); | |
410 | |
411 CObjectString* as_string = new CObjectString(CObject::NewString( | |
412 addr->as_string())); | |
413 entry->SetAt(1, as_string); | |
414 | |
415 sockaddr_storage raw = addr->addr(); | |
416 CObjectUint8Array* data = new CObjectUint8Array(CObject::NewUint8Array( | |
417 SocketAddress::GetAddrLength(raw))); | |
418 memmove(data->Buffer(), | |
419 reinterpret_cast<void *>(&raw), | |
420 SocketAddress::GetAddrLength(raw)); | |
421 | |
422 entry->SetAt(2, data); | |
423 array->SetAt(i + 1, entry); | |
424 } | |
425 result = array; | |
426 delete addresses; | |
427 } else { | 378 } else { |
428 result = CObject::NewOSError(os_error); | 379 result = CObject::NewOSError(os_error); |
429 delete os_error; | 380 delete os_error; |
430 } | 381 } |
431 return result; | 382 return result; |
432 } | 383 } |
433 return CObject::IllegalArgumentError(); | 384 return CObject::IllegalArgumentError(); |
434 } | 385 } |
435 | 386 |
436 | 387 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 | 473 |
523 | 474 |
524 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 475 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { |
525 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 476 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
526 } | 477 } |
527 | 478 |
528 | 479 |
529 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 480 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
530 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 481 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
531 } | 482 } |
OLD | NEW |