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 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 24 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
25 Dart_EnterScope(); | 25 Dart_EnterScope(); |
26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
27 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 27 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1); |
| 28 Dart_TypedData_Type data_type; |
| 29 uint8_t* data = NULL; |
| 30 intptr_t len; |
| 31 Dart_Handle result = Dart_TypedDataAcquireData( |
| 32 host_obj, &data_type, reinterpret_cast<void**>(&data), &len); |
28 int64_t port = 0; | 33 int64_t port = 0; |
29 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { | 34 if (!Dart_IsError(result) && |
30 intptr_t socket = Socket::CreateConnect(host, port); | 35 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { |
| 36 sockaddr_storage addr; |
| 37 memmove(reinterpret_cast<void *>(&addr), |
| 38 data, |
| 39 len); |
| 40 intptr_t socket = Socket::CreateConnect(addr, port); |
| 41 Dart_TypedDataReleaseData(host_obj); |
31 if (socket >= 0) { | 42 if (socket >= 0) { |
32 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 43 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
33 if (Dart_IsError(err)) Dart_PropagateError(err); | 44 if (Dart_IsError(err)) Dart_PropagateError(err); |
34 Dart_SetReturnValue(args, Dart_True()); | 45 Dart_SetReturnValue(args, Dart_True()); |
35 } else { | 46 } else { |
36 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 47 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
37 } | 48 } |
38 } else { | 49 } else { |
39 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | 50 OSError os_error(-1, "Invalid argument", OSError::kUnknown); |
40 Dart_Handle err = DartUtils::NewDartOSError(&os_error); | 51 Dart_Handle err = DartUtils::NewDartOSError(&os_error); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 314 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
304 if (Dart_IsError(err)) Dart_PropagateError(err); | 315 if (Dart_IsError(err)) Dart_PropagateError(err); |
305 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 316 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
306 Dart_ExitScope(); | 317 Dart_ExitScope(); |
307 } | 318 } |
308 | 319 |
309 | 320 |
310 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { | 321 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { |
311 Dart_EnterScope(); | 322 Dart_EnterScope(); |
312 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); | 323 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); |
313 Dart_Handle bind_address_obj = Dart_GetNativeArgument(args, 1); | 324 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1); |
| 325 Dart_TypedData_Type data_type; |
| 326 uint8_t* data = NULL; |
| 327 intptr_t len; |
| 328 Dart_Handle result = Dart_TypedDataAcquireData( |
| 329 host_obj, &data_type, reinterpret_cast<void**>(&data), &len); |
314 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); | 330 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); |
315 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); | 331 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); |
316 int64_t port = 0; | 332 int64_t port = 0; |
317 int64_t backlog = 0; | 333 int64_t backlog = 0; |
318 if (Dart_IsString(bind_address_obj) && | 334 if (!Dart_IsError(result) && |
319 DartUtils::GetInt64Value(port_obj, &port) && | 335 DartUtils::GetInt64Value(port_obj, &port) && |
320 DartUtils::GetInt64Value(backlog_obj, &backlog)) { | 336 DartUtils::GetInt64Value(backlog_obj, &backlog)) { |
321 const char* bind_address = DartUtils::GetStringValue(bind_address_obj); | 337 sockaddr_storage addr; |
322 intptr_t socket = | 338 memmove(reinterpret_cast<void *>(&addr), data, len); |
323 ServerSocket::CreateBindListen(bind_address, port, backlog); | 339 intptr_t socket = ServerSocket::CreateBindListen(addr, port, backlog); |
| 340 Dart_TypedDataReleaseData(host_obj); |
324 if (socket >= 0) { | 341 if (socket >= 0) { |
325 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); | 342 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); |
326 if (Dart_IsError(err)) Dart_PropagateError(err); | 343 if (Dart_IsError(err)) Dart_PropagateError(err); |
327 Dart_SetReturnValue(args, Dart_True()); | 344 Dart_SetReturnValue(args, Dart_True()); |
328 } else { | 345 } else { |
329 if (socket == -5) { | 346 if (socket == -5) { |
330 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 347 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 348 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
332 } else { | 349 } else { |
333 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 350 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
(...skipping 29 matching lines...) Expand all Loading... |
363 } | 380 } |
364 Dart_ExitScope(); | 381 Dart_ExitScope(); |
365 } | 382 } |
366 | 383 |
367 | 384 |
368 static CObject* LookupRequest(const CObjectArray& request) { | 385 static CObject* LookupRequest(const CObjectArray& request) { |
369 if (request.Length() == 2 && request[1]->IsString()) { | 386 if (request.Length() == 2 && request[1]->IsString()) { |
370 CObjectString host(request[1]); | 387 CObjectString host(request[1]); |
371 CObject* result = NULL; | 388 CObject* result = NULL; |
372 OSError* os_error = NULL; | 389 OSError* os_error = NULL; |
373 const char* ip_address = | 390 SocketAddresses* addresses = |
374 Socket::LookupIPv4Address(host.CString(), &os_error); | 391 Socket::LookupAddress(host.CString(), &os_error); |
375 if (ip_address != NULL) { | 392 if (addresses != NULL) { |
376 result = new CObjectString(CObject::NewString(ip_address)); | 393 CObjectArray* array = new CObjectArray( |
377 free(const_cast<char*>(ip_address)); | 394 CObject::NewArray(addresses->GetCount())); |
| 395 for (intptr_t i = 0; i < addresses->GetCount(); i++) { |
| 396 SocketAddress* addr = addresses->GetAt(i); |
| 397 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); |
| 398 |
| 399 CObjectInt32* type = new CObjectInt32( |
| 400 CObject::NewInt32(addr->GetType())); |
| 401 entry->SetAt(0, type); |
| 402 |
| 403 CObjectString* as_string = new CObjectString(CObject::NewString( |
| 404 addr->AsString())); |
| 405 entry->SetAt(1, as_string); |
| 406 |
| 407 sockaddr_storage raw = addr->GetAddr(); |
| 408 CObjectUint8Array* data = new CObjectUint8Array(CObject::NewUint8Array( |
| 409 SOCKADDR_STORAGE_LENGTH(raw))); |
| 410 memmove(data->Buffer(), |
| 411 reinterpret_cast<void *>(&raw), |
| 412 SOCKADDR_STORAGE_LENGTH(raw)); |
| 413 |
| 414 entry->SetAt(2, data); |
| 415 array->SetAt(i, entry); |
| 416 } |
| 417 result = array; |
| 418 delete addresses; |
378 } else { | 419 } else { |
379 result = CObject::NewOSError(os_error); | 420 result = CObject::NewOSError(os_error); |
380 delete os_error; | 421 delete os_error; |
381 } | 422 } |
382 return result; | 423 return result; |
383 } | 424 } |
384 return CObject::IllegalArgumentError(); | 425 return CObject::IllegalArgumentError(); |
385 } | 426 } |
386 | 427 |
387 | 428 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 514 |
474 | 515 |
475 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 516 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { |
476 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 517 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
477 } | 518 } |
478 | 519 |
479 | 520 |
480 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { | 521 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { |
481 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); | 522 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); |
482 } | 523 } |
OLD | NEW |