Chromium Code Reviews| 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/isolate_data.h" | 6 #include "bin/isolate_data.h" |
| 7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
| 8 #include "bin/socket.h" | 8 #include "bin/socket.h" |
| 9 #include "bin/thread.h" | 9 #include "bin/thread.h" |
| 10 #include "bin/lockers.h" | |
| 10 #include "bin/utils.h" | 11 #include "bin/utils.h" |
| 11 | 12 |
| 12 #include "platform/globals.h" | 13 #include "platform/globals.h" |
| 13 #include "platform/utils.h" | 14 #include "platform/utils.h" |
| 14 | 15 |
| 15 #include "include/dart_api.h" | 16 #include "include/dart_api.h" |
| 16 | 17 |
| 17 namespace dart { | 18 namespace dart { |
| 18 namespace bin { | 19 namespace bin { |
| 19 | 20 |
| 21 Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, | |
| 22 RawAddr addr, | |
| 23 intptr_t port, | |
| 24 intptr_t backlog, | |
| 25 bool v6_only, | |
| 26 bool shared) { | |
| 27 MutexLocker ml(ListeningSocketRegistry::mutex_); | |
| 28 | |
| 29 SocketsIterator it = sockets_by_port_.find(port); | |
| 30 OSSocket *first_os_socket = NULL; | |
| 31 if (it != sockets_by_port_.end()) { | |
| 32 first_os_socket = it->second; | |
| 33 } | |
| 34 | |
| 35 if (first_os_socket != NULL) { | |
| 36 // There is already a socket listening on this port. We need to ensure | |
| 37 // that if there is one also listening on the same address, it was created | |
| 38 // with `shared = true`, ... | |
| 39 | |
| 40 OSSocket *os_socket = it->second; | |
| 41 OSSocket *os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); | |
| 42 | |
| 43 if (os_socket_same_addr != NULL) { | |
| 44 if (!os_socket_same_addr->shared || !shared) { | |
| 45 OSError os_error(-1, | |
| 46 "The shared flag to bind() needs to be `true` if " | |
| 47 "binding multiple times on the same (address, port) " | |
| 48 "combination.", | |
| 49 OSError::kUnknown); | |
| 50 return DartUtils::NewDartOSError(&os_error); | |
| 51 } | |
| 52 if (os_socket_same_addr->v6_only != v6_only) { | |
| 53 OSError os_error(-1, | |
| 54 "The v6Only flag to bind() needs to be the same if " | |
| 55 "binding multiple times on the same (address, port) " | |
| 56 "combination.", | |
| 57 OSError::kUnknown); | |
| 58 return DartUtils::NewDartOSError(&os_error); | |
| 59 } | |
| 60 | |
| 61 // This socket creation is the exact same as the one which created the | |
|
Søren Gjesse
2015/01/29 09:05:51
add "originally" after "which"
kustermann
2015/01/29 11:14:05
Done.
| |
| 62 // socket. We therefore increment the refcount and return the file | |
| 63 // descriptor. | |
| 64 os_socket->ref_count++; | |
| 65 | |
| 66 // We set as a side-effect the port on the dart socket_object. | |
|
Søren Gjesse
2015/01/29 09:05:51
port -> file descriptor
kustermann
2015/01/29 11:14:05
Done.
| |
| 67 Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); | |
| 68 | |
| 69 return Dart_True(); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // There is no socket listening on that (address, port), so we create new one. | |
| 74 intptr_t socketfd = ServerSocket::CreateBindListen( | |
| 75 addr, port, backlog, v6_only); | |
| 76 if (socketfd == -5) { | |
| 77 OSError os_error(-1, "Invalid host", OSError::kUnknown); | |
| 78 return DartUtils::NewDartOSError(&os_error); | |
| 79 } | |
| 80 if (socketfd < 0) { | |
| 81 OSError error; | |
| 82 return DartUtils::NewDartOSError(&error); | |
| 83 } | |
| 84 if (!ServerSocket::StartAccept(socketfd)) { | |
| 85 OSError os_error(-1, "Failed to start accept", OSError::kUnknown); | |
| 86 return DartUtils::NewDartOSError(&os_error); | |
| 87 } | |
| 88 intptr_t allocated_port = Socket::GetPort(socketfd); | |
| 89 ASSERT(allocated_port >= 0); | |
|
Søren Gjesse
2015/01/29 09:05:51
'>=' -> '>'
kustermann
2015/01/29 11:14:05
Done.
| |
| 90 | |
| 91 OSSocket *os_socket = | |
| 92 new OSSocket(addr, allocated_port, v6_only, shared, socketfd); | |
| 93 os_socket->ref_count = 1; | |
| 94 os_socket->next = first_os_socket; | |
| 95 sockets_by_port_[allocated_port] = os_socket; | |
| 96 sockets_by_fd_[socketfd] = os_socket; | |
| 97 | |
| 98 // We set as a side-effect the port on the dart socket_object. | |
| 99 Socket::SetSocketIdNativeField(socket_object, socketfd); | |
| 100 | |
| 101 return Dart_True(); | |
| 102 } | |
| 103 | |
| 104 bool ListeningSocketRegistry::CloseSafe(int socketfd) { | |
|
Søren Gjesse
2015/01/29 09:05:51
Maybe assert that the mutex is actually locked. As
kustermann
2015/01/29 11:14:05
Done.
| |
| 105 SocketsIterator it = sockets_by_fd_.find(socketfd); | |
| 106 if (it != sockets_by_fd_.end()) { | |
| 107 OSSocket *os_socket = it->second; | |
| 108 | |
| 109 ASSERT(os_socket->ref_count > 0); | |
| 110 os_socket->ref_count--; | |
| 111 if (os_socket->ref_count == 0) { | |
| 112 // We free the OS socket by removing it from two datastructures. | |
| 113 sockets_by_fd_.erase(socketfd); | |
| 114 | |
| 115 OSSocket *last = NULL; | |
|
Søren Gjesse
2015/01/29 09:05:51
Rename last to prev?
kustermann
2015/01/29 11:14:05
Done.
| |
| 116 OSSocket *current = sockets_by_port_[os_socket->port]; | |
| 117 while (current != os_socket) { | |
| 118 ASSERT(current != NULL); | |
| 119 last = current; | |
| 120 current = current->next; | |
| 121 } | |
| 122 | |
| 123 if (last == NULL && current->next == NULL) { | |
| 124 sockets_by_port_.erase(os_socket->port); | |
| 125 } else if (last == NULL) { | |
| 126 sockets_by_port_[os_socket->port] = current->next; | |
| 127 } else { | |
| 128 last->next = os_socket->next; | |
| 129 } | |
| 130 | |
| 131 delete os_socket; | |
| 132 return true; | |
| 133 } | |
| 134 return false; | |
| 135 } else { | |
| 136 // It should be impossible for the event handler to close something that | |
| 137 // hasn't been created before. | |
| 138 UNREACHABLE(); | |
| 139 return false; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 Dart_Handle ListeningSocketRegistry::MarkSocketFdAsSharableHack(int socketfd) { | |
| 144 MutexLocker ml(ListeningSocketRegistry::mutex_); | |
| 145 | |
| 146 SocketsIterator it = sockets_by_fd_.find(socketfd); | |
| 147 if (it != sockets_by_fd_.end()) { | |
| 148 it->second->shared = true; | |
| 149 return Dart_True(); | |
| 150 } else { | |
| 151 return Dart_False(); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 bool ListeningSocketRegistry::addressesAreEqual(const RawAddr& a, | |
|
Søren Gjesse
2015/01/29 09:05:51
Move this as a static on SocketAddress where we ha
| |
| 156 const RawAddr& b) { | |
| 157 if (a.ss.ss_family == AF_INET) { | |
| 158 if (b.ss.ss_family != AF_INET) return false; | |
| 159 return memcmp(&a.in.sin_addr, &b.in.sin_addr, sizeof(a.in.sin_addr)) == 0; | |
| 160 } else if (a.ss.ss_family == AF_INET6) { | |
| 161 if (b.ss.ss_family != AF_INET6) return false; | |
| 162 return memcmp(&a.in6.sin6_addr, | |
| 163 &b.in6.sin6_addr, | |
| 164 sizeof(a.in6.sin6_addr)) == 0; | |
| 165 } else { | |
| 166 UNREACHABLE(); | |
| 167 return false; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 ListeningSocketRegistry globalTcpListeningSocketRegistry; | |
| 172 | |
| 20 static const int kSocketIdNativeField = 0; | 173 static const int kSocketIdNativeField = 0; |
| 21 | 174 |
| 22 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { | 175 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { |
| 23 const char* address = | 176 const char* address = |
| 24 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | 177 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 25 ASSERT(address != NULL); | 178 ASSERT(address != NULL); |
| 26 RawAddr raw; | 179 RawAddr raw; |
| 27 memset(&raw, 0, sizeof(raw)); | 180 memset(&raw, 0, sizeof(raw)); |
| 28 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 | 181 int type = strchr(address, ':') == NULL ? SocketAddress::TYPE_IPV4 |
| 29 : SocketAddress::TYPE_IPV6; | 182 : SocketAddress::TYPE_IPV6; |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 369 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | 522 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); |
| 370 int64_t port = DartUtils::GetInt64ValueCheckRange( | 523 int64_t port = DartUtils::GetInt64ValueCheckRange( |
| 371 Dart_GetNativeArgument(args, 2), | 524 Dart_GetNativeArgument(args, 2), |
| 372 0, | 525 0, |
| 373 65535); | 526 65535); |
| 374 int64_t backlog = DartUtils::GetInt64ValueCheckRange( | 527 int64_t backlog = DartUtils::GetInt64ValueCheckRange( |
| 375 Dart_GetNativeArgument(args, 3), | 528 Dart_GetNativeArgument(args, 3), |
| 376 0, | 529 0, |
| 377 65535); | 530 65535); |
| 378 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); | 531 bool v6_only = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4)); |
| 379 intptr_t socket = ServerSocket::CreateBindListen( | 532 bool shared = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 5)); |
| 380 addr, port, backlog, v6_only); | 533 |
| 381 OSError error; | 534 Dart_Handle socket_object = Dart_GetNativeArgument(args, 0); |
| 382 if (socket >= 0 && ServerSocket::StartAccept(socket)) { | 535 Dart_Handle result = globalTcpListeningSocketRegistry.CreateBindListen( |
| 383 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), socket); | 536 socket_object, addr, port, backlog, v6_only, shared); |
| 384 Dart_SetReturnValue(args, Dart_True()); | 537 Dart_SetReturnValue(args, result); |
| 385 } else { | |
| 386 if (socket == -5) { | |
| 387 OSError os_error(-1, "Invalid host", OSError::kUnknown); | |
| 388 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | |
| 389 } else { | |
| 390 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | |
| 391 } | |
| 392 } | |
| 393 } | 538 } |
| 394 | 539 |
| 395 | 540 |
| 396 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { | 541 void FUNCTION_NAME(ServerSocket_Accept)(Dart_NativeArguments args) { |
| 397 intptr_t socket = | 542 intptr_t socket = |
| 398 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | 543 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); |
| 399 intptr_t new_socket = ServerSocket::Accept(socket); | 544 intptr_t new_socket = ServerSocket::Accept(socket); |
| 400 if (new_socket >= 0) { | 545 if (new_socket >= 0) { |
| 401 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); | 546 Socket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 1), new_socket); |
| 402 Dart_SetReturnValue(args, Dart_True()); | 547 Dart_SetReturnValue(args, Dart_True()); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 int interfaceIndex = | 816 int interfaceIndex = |
| 672 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 817 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 673 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) { | 818 if (Socket::LeaveMulticast(socket, &addr, &interface, interfaceIndex)) { |
| 674 Dart_SetReturnValue(args, Dart_Null()); | 819 Dart_SetReturnValue(args, Dart_Null()); |
| 675 } else { | 820 } else { |
| 676 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | 821 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 677 } | 822 } |
| 678 } | 823 } |
| 679 | 824 |
| 680 | 825 |
| 826 void FUNCTION_NAME(Socket_MarkSocketAsSharedHack)(Dart_NativeArguments args) { | |
| 827 intptr_t socketfd = | |
| 828 Socket::GetSocketIdNativeField(Dart_GetNativeArgument(args, 0)); | |
| 829 | |
| 830 Dart_SetReturnValue(args, | |
| 831 globalTcpListeningSocketRegistry.MarkSocketFdAsSharableHack(socketfd)); | |
| 832 } | |
| 833 | |
| 834 | |
| 681 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { | 835 void Socket::SetSocketIdNativeField(Dart_Handle socket, intptr_t id) { |
| 682 Dart_Handle err = | 836 Dart_Handle err = |
| 683 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); | 837 Dart_SetNativeInstanceField(socket, kSocketIdNativeField, id); |
| 684 if (Dart_IsError(err)) Dart_PropagateError(err); | 838 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 685 } | 839 } |
| 686 | 840 |
| 687 | 841 |
| 688 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { | 842 intptr_t Socket::GetSocketIdNativeField(Dart_Handle socket_obj) { |
| 689 intptr_t socket = 0; | 843 intptr_t socket = 0; |
| 690 Dart_Handle err = | 844 Dart_Handle err = |
| 691 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); | 845 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &socket); |
| 692 if (Dart_IsError(err)) Dart_PropagateError(err); | 846 if (Dart_IsError(err)) Dart_PropagateError(err); |
| 693 return socket; | 847 return socket; |
| 694 } | 848 } |
| 695 | 849 |
| 696 } // namespace bin | 850 } // namespace bin |
| 697 } // namespace dart | 851 } // namespace dart |
| OLD | NEW |