Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(119)

Side by Side Diff: runtime/bin/socket.cc

Issue 14083007: Add new InternetAddress class with a static lookup function (including IPv6 results). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix new test. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/bin/socket_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
24 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { 37 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) {
25 Dart_EnterScope(); 38 Dart_EnterScope();
26 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); 39 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0);
27 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 40 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1);
41 struct sockaddr_storage addr;
42 Dart_Handle result = GetSockAddr(host_obj, &addr);
28 int64_t port = 0; 43 int64_t port = 0;
29 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) { 44 if (!Dart_IsError(result) &&
30 intptr_t socket = Socket::CreateConnect(host, port); 45 DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 2), &port)) {
46 intptr_t socket = Socket::CreateConnect(addr, port);
47 OSError error;
48 Dart_TypedDataReleaseData(host_obj);
31 if (socket >= 0) { 49 if (socket >= 0) {
32 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); 50 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket);
33 if (Dart_IsError(err)) Dart_PropagateError(err); 51 if (Dart_IsError(err)) Dart_PropagateError(err);
34 Dart_SetReturnValue(args, Dart_True()); 52 Dart_SetReturnValue(args, Dart_True());
35 } else { 53 } else {
36 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 54 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
37 } 55 }
38 } else { 56 } else {
39 OSError os_error(-1, "Invalid argument", OSError::kUnknown); 57 OSError os_error(-1, "Invalid argument", OSError::kUnknown);
40 Dart_Handle err = DartUtils::NewDartOSError(&os_error); 58 Dart_Handle err = DartUtils::NewDartOSError(&os_error);
41 if (Dart_IsError(err)) Dart_PropagateError(err); 59 if (Dart_IsError(err)) Dart_PropagateError(err);
42 Dart_SetReturnValue(args, err); 60 Dart_SetReturnValue(args, err);
43 } 61 }
44 Dart_ExitScope(); 62 Dart_ExitScope();
45 } 63 }
46 64
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 262
245 263
246 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { 264 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) {
247 Dart_EnterScope(); 265 Dart_EnterScope();
248 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); 266 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0);
249 intptr_t socket = 0; 267 intptr_t socket = 0;
250 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket); 268 Dart_Handle err = Socket::GetSocketIdNativeField(socket_obj, &socket);
251 if (Dart_IsError(err)) Dart_PropagateError(err); 269 if (Dart_IsError(err)) Dart_PropagateError(err);
252 OSError os_error; 270 OSError os_error;
253 intptr_t port = 0; 271 intptr_t port = 0;
254 char host[INET_ADDRSTRLEN]; 272 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
273 char host[INET6_ADDRSTRLEN];
255 if (Socket::GetRemotePeer(socket, host, &port)) { 274 if (Socket::GetRemotePeer(socket, host, &port)) {
256 Dart_Handle list = Dart_NewList(2); 275 Dart_Handle list = Dart_NewList(2);
257 Dart_ListSetAt(list, 0, Dart_NewStringFromCString(host)); 276 Dart_ListSetAt(list, 0, Dart_NewStringFromCString(host));
258 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); 277 Dart_ListSetAt(list, 1, Dart_NewInteger(port));
259 Dart_SetReturnValue(args, list); 278 Dart_SetReturnValue(args, list);
260 } else { 279 } else {
261 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 280 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
262 } 281 }
263 Dart_ExitScope(); 282 Dart_ExitScope();
264 } 283 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); 322 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket);
304 if (Dart_IsError(err)) Dart_PropagateError(err); 323 if (Dart_IsError(err)) Dart_PropagateError(err);
305 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); 324 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0));
306 Dart_ExitScope(); 325 Dart_ExitScope();
307 } 326 }
308 327
309 328
310 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) { 329 void FUNCTION_NAME(ServerSocket_CreateBindListen)(Dart_NativeArguments args) {
311 Dart_EnterScope(); 330 Dart_EnterScope();
312 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0); 331 Dart_Handle socket_obj = Dart_GetNativeArgument(args, 0);
313 Dart_Handle bind_address_obj = Dart_GetNativeArgument(args, 1); 332 Dart_Handle host_obj = Dart_GetNativeArgument(args, 1);
333 struct sockaddr_storage addr;
334 Dart_Handle result = GetSockAddr(host_obj, &addr);
314 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2); 335 Dart_Handle port_obj = Dart_GetNativeArgument(args, 2);
315 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3); 336 Dart_Handle backlog_obj = Dart_GetNativeArgument(args, 3);
316 int64_t port = 0; 337 int64_t port = 0;
317 int64_t backlog = 0; 338 int64_t backlog = 0;
318 if (Dart_IsString(bind_address_obj) && 339 if (!Dart_IsError(result) &&
319 DartUtils::GetInt64Value(port_obj, &port) && 340 DartUtils::GetInt64Value(port_obj, &port) &&
320 DartUtils::GetInt64Value(backlog_obj, &backlog)) { 341 DartUtils::GetInt64Value(backlog_obj, &backlog)) {
321 const char* bind_address = DartUtils::GetStringValue(bind_address_obj); 342 intptr_t socket = ServerSocket::CreateBindListen(addr, port, backlog);
322 intptr_t socket = 343 OSError error;
323 ServerSocket::CreateBindListen(bind_address, port, backlog); 344 Dart_TypedDataReleaseData(host_obj);
324 if (socket >= 0) { 345 if (socket >= 0) {
325 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket); 346 Dart_Handle err = Socket::SetSocketIdNativeField(socket_obj, socket);
326 if (Dart_IsError(err)) Dart_PropagateError(err); 347 if (Dart_IsError(err)) Dart_PropagateError(err);
327 Dart_SetReturnValue(args, Dart_True()); 348 Dart_SetReturnValue(args, Dart_True());
328 } else { 349 } else {
329 if (socket == -5) { 350 if (socket == -5) {
330 OSError os_error(-1, "Invalid host", OSError::kUnknown); 351 OSError os_error(-1, "Invalid host", OSError::kUnknown);
331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); 352 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
332 } else { 353 } else {
333 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 354 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error));
334 } 355 }
335 } 356 }
336 } else { 357 } else {
337 OSError os_error(-1, "Invalid argument", OSError::kUnknown); 358 OSError os_error(-1, "Invalid argument", OSError::kUnknown);
338 Dart_Handle err = DartUtils::NewDartOSError(&os_error); 359 Dart_Handle err = DartUtils::NewDartOSError(&os_error);
339 if (Dart_IsError(err)) Dart_PropagateError(err); 360 if (Dart_IsError(err)) Dart_PropagateError(err);
340 Dart_SetReturnValue(args, err); 361 Dart_SetReturnValue(args, err);
341 } 362 }
342 Dart_ExitScope(); 363 Dart_ExitScope();
343 } 364 }
(...skipping 15 matching lines...) Expand all
359 } else if (new_socket == ServerSocket::kTemporaryFailure) { 380 } else if (new_socket == ServerSocket::kTemporaryFailure) {
360 Dart_SetReturnValue(args, Dart_False()); 381 Dart_SetReturnValue(args, Dart_False());
361 } else { 382 } else {
362 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); 383 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
363 } 384 }
364 Dart_ExitScope(); 385 Dart_ExitScope();
365 } 386 }
366 387
367 388
368 static CObject* LookupRequest(const CObjectArray& request) { 389 static CObject* LookupRequest(const CObjectArray& request) {
369 if (request.Length() == 2 && request[1]->IsString()) { 390 if (request.Length() == 3 &&
391 request[1]->IsString() &&
392 request[2]->IsInt32()) {
370 CObjectString host(request[1]); 393 CObjectString host(request[1]);
394 CObjectInt32 type(request[2]);
371 CObject* result = NULL; 395 CObject* result = NULL;
372 OSError* os_error = NULL; 396 OSError* os_error = NULL;
373 const char* ip_address = 397 SocketAddresses* addresses =
374 Socket::LookupIPv4Address(host.CString(), &os_error); 398 Socket::LookupAddress(host.CString(), type.Value(), &os_error);
375 if (ip_address != NULL) { 399 if (addresses != NULL) {
376 result = new CObjectString(CObject::NewString(ip_address)); 400 CObjectArray* array = new CObjectArray(
377 free(const_cast<char*>(ip_address)); 401 CObject::NewArray(addresses->count() + 1));
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;
378 } else { 427 } else {
379 result = CObject::NewOSError(os_error); 428 result = CObject::NewOSError(os_error);
380 delete os_error; 429 delete os_error;
381 } 430 }
382 return result; 431 return result;
383 } 432 }
384 return CObject::IllegalArgumentError(); 433 return CObject::IllegalArgumentError();
385 } 434 }
386 435
387 436
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 522
474 523
475 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { 524 Dart_Handle Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) {
476 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); 525 return Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id);
477 } 526 }
478 527
479 528
480 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) { 529 Dart_Handle Socket::GetSocketIdNativeField(Dart_Handle socket, intptr_t* id) {
481 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id); 530 return Dart_GetNativeInstanceField(socket, kSocketIdNativeField, id);
482 } 531 }
OLDNEW
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/bin/socket_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698