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 #if !defined(DART_IO_DISABLED) | 5 #if !defined(DART_IO_DISABLED) |
6 | 6 |
7 #include "bin/socket.h" | 7 #include "bin/socket.h" |
8 | 8 |
9 #include "bin/dartutils.h" | 9 #include "bin/dartutils.h" |
10 #include "bin/eventhandler.h" | 10 #include "bin/eventhandler.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // the file descriptor. | 141 // the file descriptor. |
142 os_socket->ref_count++; | 142 os_socket->ref_count++; |
143 | 143 |
144 // The same Socket is used by a second Dart _NativeSocket object. | 144 // The same Socket is used by a second Dart _NativeSocket object. |
145 // It Retains a reference. | 145 // It Retains a reference. |
146 os_socket->socketfd->Retain(); | 146 os_socket->socketfd->Retain(); |
147 // We set as a side-effect the file descriptor on the dart | 147 // We set as a side-effect the file descriptor on the dart |
148 // socket_object. | 148 // socket_object. |
149 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, | 149 Socket::ReuseSocketIdNativeField(socket_object, os_socket->socketfd, |
150 true); | 150 true); |
| 151 |
151 return Dart_True(); | 152 return Dart_True(); |
152 } | 153 } |
153 } | 154 } |
154 } | 155 } |
155 | 156 |
156 // There is no socket listening on that (address, port), so we create new one. | 157 // There is no socket listening on that (address, port), so we create new one. |
157 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); | 158 intptr_t fd = ServerSocket::CreateBindListen(addr, backlog, v6_only); |
158 if (fd == -5) { | 159 if (fd == -5) { |
159 OSError os_error(-1, "Invalid host", OSError::kUnknown); | 160 OSError os_error(-1, "Invalid host", OSError::kUnknown); |
160 return DartUtils::NewDartOSError(&os_error); | 161 return DartUtils::NewDartOSError(&os_error); |
161 } | 162 } |
162 if (fd < 0) { | 163 if (fd < 0) { |
163 OSError error; | 164 OSError error; |
164 return DartUtils::NewDartOSError(&error); | 165 return DartUtils::NewDartOSError(&error); |
165 } | 166 } |
166 if (!ServerSocket::StartAccept(fd)) { | 167 if (!ServerSocket::StartAccept(fd)) { |
167 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); | 168 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); |
168 return DartUtils::NewDartOSError(&os_error); | 169 return DartUtils::NewDartOSError(&os_error); |
169 } | 170 } |
170 intptr_t allocated_port = SocketBase::GetPort(fd); | 171 intptr_t allocated_port = Socket::GetPort(fd); |
171 ASSERT(allocated_port > 0); | 172 ASSERT(allocated_port > 0); |
172 | 173 |
173 if (allocated_port != port) { | 174 if (allocated_port != port) { |
174 // There are two cases to consider: | 175 // There are two cases to consider: |
175 // | 176 // |
176 // a) The user requested (address, port) where port != 0 which means | 177 // a) The user requested (address, port) where port != 0 which means |
177 // we re-use an existing socket if available (and it is shared) or we | 178 // we re-use an existing socket if available (and it is shared) or we |
178 // create a new one. The new socket is guaranteed to have that | 179 // create a new one. The new socket is guaranteed to have that |
179 // selected port. | 180 // selected port. |
180 // | 181 // |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 return CloseOneSafe(os_socket, true); | 261 return CloseOneSafe(os_socket, true); |
261 } else { | 262 } else { |
262 // A finalizer may direct the event handler to close a listening socket | 263 // A finalizer may direct the event handler to close a listening socket |
263 // that it has never seen before. In this case, we return true to direct | 264 // that it has never seen before. In this case, we return true to direct |
264 // the eventhandler to clean up the socket. | 265 // the eventhandler to clean up the socket. |
265 return true; | 266 return true; |
266 } | 267 } |
267 } | 268 } |
268 | 269 |
269 | 270 |
| 271 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
| 272 const char* address = |
| 273 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 274 ASSERT(address != NULL); |
| 275 RawAddr raw; |
| 276 memset(&raw, 0, sizeof(raw)); |
| 277 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 |
| 278 : SocketAddress::TYPE_IPV6; |
| 279 if (type == SocketAddress::TYPE_IPV4) { |
| 280 raw.addr.sa_family = AF_INET; |
| 281 } else { |
| 282 raw.addr.sa_family = AF_INET6; |
| 283 } |
| 284 bool ok = Socket::ParseAddress(type, address, &raw); |
| 285 if (!ok) { |
| 286 Dart_SetReturnValue(args, Dart_Null()); |
| 287 } else { |
| 288 Dart_SetReturnValue(args, SocketAddress::ToTypedData(raw)); |
| 289 } |
| 290 } |
| 291 |
| 292 |
| 293 void FUNCTION_NAME(NetworkInterface_ListSupported)(Dart_NativeArguments args) { |
| 294 Dart_SetReturnValue(args, Dart_NewBoolean(Socket::ListInterfacesSupported())); |
| 295 } |
| 296 |
| 297 |
270 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { | 298 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { |
271 RawAddr addr; | 299 RawAddr addr; |
272 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 300 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
273 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 301 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
274 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 302 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
275 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | 303 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); |
276 intptr_t socket = Socket::CreateConnect(addr); | 304 intptr_t socket = Socket::CreateConnect(addr); |
277 OSError error; | 305 OSError error; |
278 if (socket >= 0) { | 306 if (socket >= 0) { |
279 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 307 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
(...skipping 17 matching lines...) Expand all Loading... |
297 OSError error; | 325 OSError error; |
298 if (socket >= 0) { | 326 if (socket >= 0) { |
299 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 327 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
300 false); | 328 false); |
301 Dart_SetReturnValue(args, Dart_True()); | 329 Dart_SetReturnValue(args, Dart_True()); |
302 } else { | 330 } else { |
303 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 331 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
304 } | 332 } |
305 } | 333 } |
306 | 334 |
| 335 void FUNCTION_NAME(Socket_IsBindError)(Dart_NativeArguments args) { |
| 336 intptr_t error_number = |
| 337 DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1)); |
| 338 bool is_bind_error = Socket::IsBindError(error_number); |
| 339 Dart_SetReturnValue(args, is_bind_error ? Dart_True() : Dart_False()); |
| 340 } |
307 | 341 |
308 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { | 342 void FUNCTION_NAME(Socket_CreateBindDatagram)(Dart_NativeArguments args) { |
309 RawAddr addr; | 343 RawAddr addr; |
310 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 344 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
311 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | 345 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); |
312 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | 346 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); |
313 SocketAddress::SetAddrPort(&addr, port); | 347 SocketAddress::SetAddrPort(&addr, port); |
314 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); | 348 bool reuse_addr = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3)); |
315 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); | 349 intptr_t socket = Socket::CreateBindDatagram(addr, reuse_addr); |
316 if (socket >= 0) { | 350 if (socket >= 0) { |
317 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 351 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
318 false); | 352 false); |
319 Dart_SetReturnValue(args, Dart_True()); | 353 Dart_SetReturnValue(args, Dart_True()); |
320 } else { | 354 } else { |
321 OSError error; | 355 OSError error; |
322 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | 356 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
323 } | 357 } |
324 } | 358 } |
325 | 359 |
326 | 360 |
327 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { | 361 void FUNCTION_NAME(Socket_Available)(Dart_NativeArguments args) { |
328 Socket* socket = | 362 Socket* socket = |
329 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 363 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
330 intptr_t available = SocketBase::Available(socket->fd()); | 364 intptr_t available = Socket::Available(socket->fd()); |
331 if (available >= 0) { | 365 if (available >= 0) { |
332 Dart_SetReturnValue(args, Dart_NewInteger(available)); | 366 Dart_SetReturnValue(args, Dart_NewInteger(available)); |
333 } else { | 367 } else { |
334 // Available failed. Mark socket as having data, to trigger a future read | 368 // Available failed. Mark socket as having data, to trigger a future read |
335 // event where the actual error can be reported. | 369 // event where the actual error can be reported. |
336 Dart_SetReturnValue(args, Dart_NewInteger(1)); | 370 Dart_SetReturnValue(args, Dart_NewInteger(1)); |
337 } | 371 } |
338 } | 372 } |
339 | 373 |
340 | 374 |
341 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { | 375 void FUNCTION_NAME(Socket_Read)(Dart_NativeArguments args) { |
342 Socket* socket = | 376 Socket* socket = |
343 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 377 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
344 int64_t length = 0; | 378 int64_t length = 0; |
345 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | 379 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { |
346 if (short_socket_read) { | 380 if (short_socket_read) { |
347 length = (length + 1) / 2; | 381 length = (length + 1) / 2; |
348 } | 382 } |
349 uint8_t* buffer = NULL; | 383 uint8_t* buffer = NULL; |
350 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | 384 Dart_Handle result = IOBuffer::Allocate(length, &buffer); |
351 if (Dart_IsError(result)) { | 385 if (Dart_IsError(result)) { |
352 Dart_PropagateError(result); | 386 Dart_PropagateError(result); |
353 } | 387 } |
354 ASSERT(buffer != NULL); | 388 ASSERT(buffer != NULL); |
355 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); | 389 intptr_t bytes_read = Socket::Read(socket->fd(), buffer, length); |
356 if (bytes_read == length) { | 390 if (bytes_read == length) { |
357 Dart_SetReturnValue(args, result); | 391 Dart_SetReturnValue(args, result); |
358 } else if (bytes_read > 0) { | 392 } else if (bytes_read > 0) { |
359 uint8_t* new_buffer = NULL; | 393 uint8_t* new_buffer = NULL; |
360 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | 394 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); |
361 if (Dart_IsError(new_result)) { | 395 if (Dart_IsError(new_result)) { |
362 Dart_PropagateError(new_result); | 396 Dart_PropagateError(new_result); |
363 } | 397 } |
364 ASSERT(new_buffer != NULL); | 398 ASSERT(new_buffer != NULL); |
365 memmove(new_buffer, buffer, bytes_read); | 399 memmove(new_buffer, buffer, bytes_read); |
(...skipping 19 matching lines...) Expand all Loading... |
385 | 419 |
386 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can | 420 // TODO(sgjesse): Use a MTU value here. Only the loopback adapter can |
387 // handle 64k datagrams. | 421 // handle 64k datagrams. |
388 IsolateData* isolate_data = | 422 IsolateData* isolate_data = |
389 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); | 423 reinterpret_cast<IsolateData*>(Dart_CurrentIsolateData()); |
390 if (isolate_data->udp_receive_buffer == NULL) { | 424 if (isolate_data->udp_receive_buffer == NULL) { |
391 isolate_data->udp_receive_buffer = | 425 isolate_data->udp_receive_buffer = |
392 reinterpret_cast<uint8_t*>(malloc(65536)); | 426 reinterpret_cast<uint8_t*>(malloc(65536)); |
393 } | 427 } |
394 RawAddr addr; | 428 RawAddr addr; |
395 intptr_t bytes_read = SocketBase::RecvFrom( | 429 intptr_t bytes_read = Socket::RecvFrom( |
396 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); | 430 socket->fd(), isolate_data->udp_receive_buffer, 65536, &addr); |
397 if (bytes_read == 0) { | 431 if (bytes_read == 0) { |
398 Dart_SetReturnValue(args, Dart_Null()); | 432 Dart_SetReturnValue(args, Dart_Null()); |
399 return; | 433 return; |
400 } | 434 } |
401 if (bytes_read < 0) { | 435 if (bytes_read < 0) { |
402 ASSERT(bytes_read == -1); | 436 ASSERT(bytes_read == -1); |
403 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 437 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
404 return; | 438 return; |
405 } | 439 } |
(...skipping 10 matching lines...) Expand all Loading... |
416 // Get the port and clear it in the sockaddr structure. | 450 // Get the port and clear it in the sockaddr structure. |
417 int port = SocketAddress::GetAddrPort(addr); | 451 int port = SocketAddress::GetAddrPort(addr); |
418 if (addr.addr.sa_family == AF_INET) { | 452 if (addr.addr.sa_family == AF_INET) { |
419 addr.in.sin_port = 0; | 453 addr.in.sin_port = 0; |
420 } else { | 454 } else { |
421 ASSERT(addr.addr.sa_family == AF_INET6); | 455 ASSERT(addr.addr.sa_family == AF_INET6); |
422 addr.in6.sin6_port = 0; | 456 addr.in6.sin6_port = 0; |
423 } | 457 } |
424 // Format the address to a string using the numeric format. | 458 // Format the address to a string using the numeric format. |
425 char numeric_address[INET6_ADDRSTRLEN]; | 459 char numeric_address[INET6_ADDRSTRLEN]; |
426 SocketBase::FormatNumericAddress(addr, numeric_address, INET6_ADDRSTRLEN); | 460 Socket::FormatNumericAddress(addr, numeric_address, INET6_ADDRSTRLEN); |
427 | 461 |
428 // Create a Datagram object with the data and sender address and port. | 462 // Create a Datagram object with the data and sender address and port. |
429 const int kNumArgs = 4; | 463 const int kNumArgs = 4; |
430 Dart_Handle dart_args[kNumArgs]; | 464 Dart_Handle dart_args[kNumArgs]; |
431 dart_args[0] = data; | 465 dart_args[0] = data; |
432 dart_args[1] = Dart_NewStringFromCString(numeric_address); | 466 dart_args[1] = Dart_NewStringFromCString(numeric_address); |
433 if (Dart_IsError(dart_args[1])) { | 467 if (Dart_IsError(dart_args[1])) { |
434 Dart_PropagateError(dart_args[1]); | 468 Dart_PropagateError(dart_args[1]); |
435 } | 469 } |
436 dart_args[2] = SocketAddress::ToTypedData(addr); | 470 dart_args[2] = SocketAddress::ToTypedData(addr); |
(...skipping 29 matching lines...) Expand all Loading... |
466 Dart_TypedData_Type type; | 500 Dart_TypedData_Type type; |
467 uint8_t* buffer = NULL; | 501 uint8_t* buffer = NULL; |
468 intptr_t len; | 502 intptr_t len; |
469 Dart_Handle result = Dart_TypedDataAcquireData( | 503 Dart_Handle result = Dart_TypedDataAcquireData( |
470 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 504 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
471 if (Dart_IsError(result)) { | 505 if (Dart_IsError(result)) { |
472 Dart_PropagateError(result); | 506 Dart_PropagateError(result); |
473 } | 507 } |
474 ASSERT((offset + length) <= len); | 508 ASSERT((offset + length) <= len); |
475 buffer += offset; | 509 buffer += offset; |
476 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); | 510 intptr_t bytes_written = Socket::Write(socket->fd(), buffer, length); |
477 if (bytes_written >= 0) { | 511 if (bytes_written >= 0) { |
478 Dart_TypedDataReleaseData(buffer_obj); | 512 Dart_TypedDataReleaseData(buffer_obj); |
479 if (short_write) { | 513 if (short_write) { |
480 // If the write was forced 'short', indicate by returning the negative | 514 // If the write was forced 'short', indicate by returning the negative |
481 // number of bytes. A forced short write may not trigger a write event. | 515 // number of bytes. A forced short write may not trigger a write event. |
482 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); | 516 Dart_SetReturnValue(args, Dart_NewInteger(-bytes_written)); |
483 } else { | 517 } else { |
484 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 518 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
485 } | 519 } |
486 } else { | 520 } else { |
(...skipping 21 matching lines...) Expand all Loading... |
508 Dart_TypedData_Type type; | 542 Dart_TypedData_Type type; |
509 uint8_t* buffer = NULL; | 543 uint8_t* buffer = NULL; |
510 intptr_t len; | 544 intptr_t len; |
511 Dart_Handle result = Dart_TypedDataAcquireData( | 545 Dart_Handle result = Dart_TypedDataAcquireData( |
512 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | 546 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); |
513 if (Dart_IsError(result)) { | 547 if (Dart_IsError(result)) { |
514 Dart_PropagateError(result); | 548 Dart_PropagateError(result); |
515 } | 549 } |
516 ASSERT((offset + length) <= len); | 550 ASSERT((offset + length) <= len); |
517 buffer += offset; | 551 buffer += offset; |
518 intptr_t bytes_written = | 552 intptr_t bytes_written = Socket::SendTo(socket->fd(), buffer, length, addr); |
519 SocketBase::SendTo(socket->fd(), buffer, length, addr); | |
520 if (bytes_written >= 0) { | 553 if (bytes_written >= 0) { |
521 Dart_TypedDataReleaseData(buffer_obj); | 554 Dart_TypedDataReleaseData(buffer_obj); |
522 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | 555 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); |
523 } else { | 556 } else { |
524 // Extract OSError before we release data, as it may override the error. | 557 // Extract OSError before we release data, as it may override the error. |
525 OSError os_error; | 558 OSError os_error; |
526 Dart_TypedDataReleaseData(buffer_obj); | 559 Dart_TypedDataReleaseData(buffer_obj); |
527 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 560 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
528 } | 561 } |
529 } | 562 } |
530 | 563 |
531 | 564 |
532 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { | 565 void FUNCTION_NAME(Socket_GetPort)(Dart_NativeArguments args) { |
533 Socket* socket = | 566 Socket* socket = |
534 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 567 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
535 OSError os_error; | 568 OSError os_error; |
536 intptr_t port = SocketBase::GetPort(socket->fd()); | 569 intptr_t port = Socket::GetPort(socket->fd()); |
537 if (port > 0) { | 570 if (port > 0) { |
538 Dart_SetReturnValue(args, Dart_NewInteger(port)); | 571 Dart_SetReturnValue(args, Dart_NewInteger(port)); |
539 } else { | 572 } else { |
540 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 573 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
541 } | 574 } |
542 } | 575 } |
543 | 576 |
544 | 577 |
545 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { | 578 void FUNCTION_NAME(Socket_GetRemotePeer)(Dart_NativeArguments args) { |
546 Socket* socket = | 579 Socket* socket = |
547 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 580 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
548 OSError os_error; | 581 OSError os_error; |
549 intptr_t port = 0; | 582 intptr_t port = 0; |
550 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port); | 583 SocketAddress* addr = Socket::GetRemotePeer(socket->fd(), &port); |
551 if (addr != NULL) { | 584 if (addr != NULL) { |
552 Dart_Handle list = Dart_NewList(2); | 585 Dart_Handle list = Dart_NewList(2); |
553 | 586 |
554 Dart_Handle entry = Dart_NewList(3); | 587 Dart_Handle entry = Dart_NewList(3); |
555 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | 588 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); |
556 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | 589 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); |
557 | 590 |
558 RawAddr raw = addr->addr(); | 591 RawAddr raw = addr->addr(); |
559 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | 592 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); |
560 | 593 |
561 Dart_ListSetAt(list, 0, entry); | 594 Dart_ListSetAt(list, 0, entry); |
562 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | 595 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); |
563 Dart_SetReturnValue(args, list); | 596 Dart_SetReturnValue(args, list); |
564 delete addr; | 597 delete addr; |
565 } else { | 598 } else { |
566 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 599 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
567 } | 600 } |
568 } | 601 } |
569 | 602 |
570 | 603 |
571 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { | 604 void FUNCTION_NAME(Socket_GetError)(Dart_NativeArguments args) { |
572 Socket* socket = | 605 Socket* socket = |
573 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 606 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
574 OSError os_error; | 607 OSError os_error; |
575 SocketBase::GetError(socket->fd(), &os_error); | 608 Socket::GetError(socket->fd(), &os_error); |
576 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | 609 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); |
577 } | 610 } |
578 | 611 |
579 | 612 |
580 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { | 613 void FUNCTION_NAME(Socket_GetType)(Dart_NativeArguments args) { |
581 Socket* socket = | 614 Socket* socket = |
582 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 615 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
583 OSError os_error; | 616 OSError os_error; |
584 intptr_t type = SocketBase::GetType(socket->fd()); | 617 intptr_t type = Socket::GetType(socket->fd()); |
585 if (type >= 0) { | 618 if (type >= 0) { |
586 Dart_SetReturnValue(args, Dart_NewInteger(type)); | 619 Dart_SetReturnValue(args, Dart_NewInteger(type)); |
587 } else { | 620 } else { |
588 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 621 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
589 } | 622 } |
590 } | 623 } |
591 | 624 |
592 | 625 |
593 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { | 626 void FUNCTION_NAME(Socket_GetStdioHandle)(Dart_NativeArguments args) { |
594 int64_t num = | 627 int64_t num = |
595 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); | 628 DartUtils::GetInt64ValueCheckRange(Dart_GetNativeArgument(args, 1), 0, 2); |
596 intptr_t socket = SocketBase::GetStdioHandle(num); | 629 intptr_t socket = Socket::GetStdioHandle(num); |
597 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, | 630 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket, |
598 false); | 631 false); |
599 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); | 632 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); |
600 } | 633 } |
601 | 634 |
602 | 635 |
603 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { | 636 void FUNCTION_NAME(Socket_GetSocketId)(Dart_NativeArguments args) { |
604 Socket* socket = | 637 Socket* socket = |
605 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 638 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
606 intptr_t id = reinterpret_cast<intptr_t>(socket); | 639 intptr_t id = reinterpret_cast<intptr_t>(socket); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 | 682 |
650 | 683 |
651 CObject* Socket::LookupRequest(const CObjectArray& request) { | 684 CObject* Socket::LookupRequest(const CObjectArray& request) { |
652 if ((request.Length() == 2) && request[0]->IsString() && | 685 if ((request.Length() == 2) && request[0]->IsString() && |
653 request[1]->IsInt32()) { | 686 request[1]->IsInt32()) { |
654 CObjectString host(request[0]); | 687 CObjectString host(request[0]); |
655 CObjectInt32 type(request[1]); | 688 CObjectInt32 type(request[1]); |
656 CObject* result = NULL; | 689 CObject* result = NULL; |
657 OSError* os_error = NULL; | 690 OSError* os_error = NULL; |
658 AddressList<SocketAddress>* addresses = | 691 AddressList<SocketAddress>* addresses = |
659 SocketBase::LookupAddress(host.CString(), type.Value(), &os_error); | 692 Socket::LookupAddress(host.CString(), type.Value(), &os_error); |
660 if (addresses != NULL) { | 693 if (addresses != NULL) { |
661 CObjectArray* array = | 694 CObjectArray* array = |
662 new CObjectArray(CObject::NewArray(addresses->count() + 1)); | 695 new CObjectArray(CObject::NewArray(addresses->count() + 1)); |
663 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | 696 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
664 for (intptr_t i = 0; i < addresses->count(); i++) { | 697 for (intptr_t i = 0; i < addresses->count(); i++) { |
665 SocketAddress* addr = addresses->GetAt(i); | 698 SocketAddress* addr = addresses->GetAt(i); |
666 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); | 699 CObjectArray* entry = new CObjectArray(CObject::NewArray(3)); |
667 | 700 |
668 CObjectInt32* type = | 701 CObjectInt32* type = |
669 new CObjectInt32(CObject::NewInt32(addr->GetType())); | 702 new CObjectInt32(CObject::NewInt32(addr->GetType())); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 } else { | 737 } else { |
705 ASSERT(len == sizeof(in6_addr)); | 738 ASSERT(len == sizeof(in6_addr)); |
706 addr.in6.sin6_family = AF_INET6; | 739 addr.in6.sin6_family = AF_INET6; |
707 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr), | 740 memmove(reinterpret_cast<void*>(&addr.in6.sin6_addr), |
708 addr_object.Buffer(), len); | 741 addr_object.Buffer(), len); |
709 } | 742 } |
710 | 743 |
711 OSError* os_error = NULL; | 744 OSError* os_error = NULL; |
712 const intptr_t kMaxHostLength = 1025; | 745 const intptr_t kMaxHostLength = 1025; |
713 char host[kMaxHostLength]; | 746 char host[kMaxHostLength]; |
714 if (SocketBase::ReverseLookup(addr, host, kMaxHostLength, &os_error)) { | 747 if (Socket::ReverseLookup(addr, host, kMaxHostLength, &os_error)) { |
715 return new CObjectString(CObject::NewString(host)); | 748 return new CObjectString(CObject::NewString(host)); |
716 } else { | 749 } else { |
717 CObject* result = CObject::NewOSError(os_error); | 750 CObject* result = CObject::NewOSError(os_error); |
718 delete os_error; | 751 delete os_error; |
719 return result; | 752 return result; |
720 } | 753 } |
721 } | 754 } |
722 return CObject::IllegalArgumentError(); | 755 return CObject::IllegalArgumentError(); |
723 } | 756 } |
724 | 757 |
725 | 758 |
726 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) { | 759 CObject* Socket::ListInterfacesRequest(const CObjectArray& request) { |
727 if ((request.Length() == 1) && request[0]->IsInt32()) { | 760 if ((request.Length() == 1) && request[0]->IsInt32()) { |
728 CObjectInt32 type(request[0]); | 761 CObjectInt32 type(request[0]); |
729 CObject* result = NULL; | 762 CObject* result = NULL; |
730 OSError* os_error = NULL; | 763 OSError* os_error = NULL; |
731 AddressList<InterfaceSocketAddress>* addresses = | 764 AddressList<InterfaceSocketAddress>* addresses = |
732 SocketBase::ListInterfaces(type.Value(), &os_error); | 765 Socket::ListInterfaces(type.Value(), &os_error); |
733 if (addresses != NULL) { | 766 if (addresses != NULL) { |
734 CObjectArray* array = | 767 CObjectArray* array = |
735 new CObjectArray(CObject::NewArray(addresses->count() + 1)); | 768 new CObjectArray(CObject::NewArray(addresses->count() + 1)); |
736 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); | 769 array->SetAt(0, new CObjectInt32(CObject::NewInt32(0))); |
737 for (intptr_t i = 0; i < addresses->count(); i++) { | 770 for (intptr_t i = 0; i < addresses->count(); i++) { |
738 InterfaceSocketAddress* interface = addresses->GetAt(i); | 771 InterfaceSocketAddress* interface = addresses->GetAt(i); |
739 SocketAddress* addr = interface->socket_address(); | 772 SocketAddress* addr = interface->socket_address(); |
740 CObjectArray* entry = new CObjectArray(CObject::NewArray(5)); | 773 CObjectArray* entry = new CObjectArray(CObject::NewArray(5)); |
741 | 774 |
742 CObjectInt32* type = | 775 CObjectInt32* type = |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { | 809 void FUNCTION_NAME(Socket_GetOption)(Dart_NativeArguments args) { |
777 Socket* socket = | 810 Socket* socket = |
778 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 811 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
779 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 812 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
780 intptr_t protocol = static_cast<intptr_t>( | 813 intptr_t protocol = static_cast<intptr_t>( |
781 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); | 814 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2))); |
782 bool ok = false; | 815 bool ok = false; |
783 switch (option) { | 816 switch (option) { |
784 case 0: { // TCP_NODELAY. | 817 case 0: { // TCP_NODELAY. |
785 bool enabled; | 818 bool enabled; |
786 ok = SocketBase::GetNoDelay(socket->fd(), &enabled); | 819 ok = Socket::GetNoDelay(socket->fd(), &enabled); |
787 if (ok) { | 820 if (ok) { |
788 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 821 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
789 } | 822 } |
790 break; | 823 break; |
791 } | 824 } |
792 case 1: { // IP_MULTICAST_LOOP. | 825 case 1: { // IP_MULTICAST_LOOP. |
793 bool enabled; | 826 bool enabled; |
794 ok = SocketBase::GetMulticastLoop(socket->fd(), protocol, &enabled); | 827 ok = Socket::GetMulticastLoop(socket->fd(), protocol, &enabled); |
795 if (ok) { | 828 if (ok) { |
796 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 829 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
797 } | 830 } |
798 break; | 831 break; |
799 } | 832 } |
800 case 2: { // IP_MULTICAST_TTL. | 833 case 2: { // IP_MULTICAST_TTL. |
801 int value; | 834 int value; |
802 ok = SocketBase::GetMulticastHops(socket->fd(), protocol, &value); | 835 ok = Socket::GetMulticastHops(socket->fd(), protocol, &value); |
803 if (ok) { | 836 if (ok) { |
804 Dart_SetReturnValue(args, Dart_NewInteger(value)); | 837 Dart_SetReturnValue(args, Dart_NewInteger(value)); |
805 } | 838 } |
806 break; | 839 break; |
807 } | 840 } |
808 case 3: { // IP_MULTICAST_IF. | 841 case 3: { // IP_MULTICAST_IF. |
809 UNIMPLEMENTED(); | 842 UNIMPLEMENTED(); |
810 break; | 843 break; |
811 } | 844 } |
812 case 4: { // IP_BROADCAST. | 845 case 4: { // IP_BROADCAST. |
813 bool enabled; | 846 bool enabled; |
814 ok = SocketBase::GetBroadcast(socket->fd(), &enabled); | 847 ok = Socket::GetBroadcast(socket->fd(), &enabled); |
815 if (ok) { | 848 if (ok) { |
816 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); | 849 Dart_SetReturnValue(args, enabled ? Dart_True() : Dart_False()); |
817 } | 850 } |
818 break; | 851 break; |
819 } | 852 } |
820 default: | 853 default: |
821 UNREACHABLE(); | 854 UNREACHABLE(); |
822 break; | 855 break; |
823 } | 856 } |
824 // In case of failure the return value is not set above. | 857 // In case of failure the return value is not set above. |
825 if (!ok) { | 858 if (!ok) { |
826 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 859 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
827 } | 860 } |
828 } | 861 } |
829 | 862 |
830 | 863 |
831 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { | 864 void FUNCTION_NAME(Socket_SetOption)(Dart_NativeArguments args) { |
832 bool result = false; | 865 bool result = false; |
833 Socket* socket = | 866 Socket* socket = |
834 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 867 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
835 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 868 int64_t option = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
836 int64_t protocol = DartUtils::GetInt64ValueCheckRange( | 869 int64_t protocol = DartUtils::GetInt64ValueCheckRange( |
837 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, | 870 Dart_GetNativeArgument(args, 2), SocketAddress::TYPE_IPV4, |
838 SocketAddress::TYPE_IPV6); | 871 SocketAddress::TYPE_IPV6); |
839 switch (option) { | 872 switch (option) { |
840 case 0: // TCP_NODELAY. | 873 case 0: // TCP_NODELAY. |
841 result = SocketBase::SetNoDelay( | 874 result = Socket::SetNoDelay( |
842 socket->fd(), | 875 socket->fd(), |
843 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 876 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
844 break; | 877 break; |
845 case 1: // IP_MULTICAST_LOOP. | 878 case 1: // IP_MULTICAST_LOOP. |
846 result = SocketBase::SetMulticastLoop( | 879 result = Socket::SetMulticastLoop( |
847 socket->fd(), protocol, | 880 socket->fd(), protocol, |
848 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 881 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
849 break; | 882 break; |
850 case 2: // IP_MULTICAST_TTL. | 883 case 2: // IP_MULTICAST_TTL. |
851 result = SocketBase::SetMulticastHops( | 884 result = Socket::SetMulticastHops( |
852 socket->fd(), protocol, | 885 socket->fd(), protocol, |
853 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); | 886 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3))); |
854 break; | 887 break; |
855 case 3: { // IP_MULTICAST_IF. | 888 case 3: { // IP_MULTICAST_IF. |
856 UNIMPLEMENTED(); | 889 UNIMPLEMENTED(); |
857 break; | 890 break; |
858 } | 891 } |
859 case 4: // IP_BROADCAST. | 892 case 4: // IP_BROADCAST. |
860 result = SocketBase::SetBroadcast( | 893 result = Socket::SetBroadcast( |
861 socket->fd(), | 894 socket->fd(), |
862 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); | 895 DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 3))); |
863 break; | 896 break; |
864 default: | 897 default: |
865 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); | 898 Dart_PropagateError(Dart_NewApiError("Value outside expected range")); |
866 break; | 899 break; |
867 } | 900 } |
868 if (result) { | 901 if (result) { |
869 Dart_SetReturnValue(args, Dart_Null()); | 902 Dart_SetReturnValue(args, Dart_Null()); |
870 } else { | 903 } else { |
871 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 904 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
872 } | 905 } |
873 } | 906 } |
874 | 907 |
875 | 908 |
876 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { | 909 void FUNCTION_NAME(Socket_JoinMulticast)(Dart_NativeArguments args) { |
877 Socket* socket = | 910 Socket* socket = |
878 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 911 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
879 RawAddr addr; | 912 RawAddr addr; |
880 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 913 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
881 RawAddr interface; | 914 RawAddr interface; |
882 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 915 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
883 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 916 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
884 } | 917 } |
885 int interfaceIndex = | 918 int interfaceIndex = |
886 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 919 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
887 if (SocketBase::JoinMulticast(socket->fd(), addr, interface, | 920 if (Socket::JoinMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
888 interfaceIndex)) { | |
889 Dart_SetReturnValue(args, Dart_Null()); | 921 Dart_SetReturnValue(args, Dart_Null()); |
890 } else { | 922 } else { |
891 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 923 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
892 } | 924 } |
893 } | 925 } |
894 | 926 |
895 | 927 |
896 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { | 928 void FUNCTION_NAME(Socket_LeaveMulticast)(Dart_NativeArguments args) { |
897 Socket* socket = | 929 Socket* socket = |
898 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 930 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
899 RawAddr addr; | 931 RawAddr addr; |
900 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 932 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
901 RawAddr interface; | 933 RawAddr interface; |
902 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { | 934 if (Dart_GetNativeArgument(args, 2) != Dart_Null()) { |
903 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); | 935 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 2), &interface); |
904 } | 936 } |
905 int interfaceIndex = | 937 int interfaceIndex = |
906 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 938 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
907 if (SocketBase::LeaveMulticast(socket->fd(), addr, interface, | 939 if (Socket::LeaveMulticast(socket->fd(), addr, interface, interfaceIndex)) { |
908 interfaceIndex)) { | |
909 Dart_SetReturnValue(args, Dart_Null()); | 940 Dart_SetReturnValue(args, Dart_Null()); |
910 } else { | 941 } else { |
911 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 942 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
912 } | 943 } |
913 } | 944 } |
914 | 945 |
915 | 946 |
916 static void SocketFinalizer(void* isolate_data, | 947 static void SocketFinalizer(void* isolate_data, |
917 Dart_WeakPersistentHandle handle, | 948 Dart_WeakPersistentHandle handle, |
918 void* data) { | 949 void* data) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 Dart_PropagateError(err); | 1006 Dart_PropagateError(err); |
976 } | 1007 } |
977 Socket* socket = reinterpret_cast<Socket*>(id); | 1008 Socket* socket = reinterpret_cast<Socket*>(id); |
978 return socket; | 1009 return socket; |
979 } | 1010 } |
980 | 1011 |
981 } // namespace bin | 1012 } // namespace bin |
982 } // namespace dart | 1013 } // namespace dart |
983 | 1014 |
984 #endif // !defined(DART_IO_DISABLED) | 1015 #endif // !defined(DART_IO_DISABLED) |
OLD | NEW |