Chromium Code Reviews| Index: runtime/bin/socket.cc |
| diff --git a/runtime/bin/socket.cc b/runtime/bin/socket.cc |
| index b0e384c8b18487f67f2b0664b0f63c785e6d1306..f7aa6d08718edfba28e5f6dd5b66b07e926603a0 100644 |
| --- a/runtime/bin/socket.cc |
| +++ b/runtime/bin/socket.cc |
| @@ -103,42 +103,46 @@ Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, |
| bool shared) { |
| MutexLocker ml(ListeningSocketRegistry::mutex_); |
|
Florian Schneider
2016/11/30 00:21:01
nit: no need to use qualified name here
MutexLock
kustermann
2016/11/30 10:51:10
Done.
|
| + OSSocket* first_os_socket = NULL; |
| intptr_t port = SocketAddress::GetAddrPort(addr); |
| - OSSocket* first_os_socket = LookupByPort(port); |
| - if (first_os_socket != NULL) { |
| - // There is already a socket listening on this port. We need to ensure |
| - // that if there is one also listening on the same address, it was created |
| - // with `shared = true`, ... |
| - OSSocket* os_socket = first_os_socket; |
| - OSSocket* os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); |
| - |
| - if (os_socket_same_addr != NULL) { |
| - if (!os_socket_same_addr->shared || !shared) { |
| - OSError os_error(-1, |
| - "The shared flag to bind() needs to be `true` if " |
| - "binding multiple times on the same (address, port) " |
| - "combination.", |
| - OSError::kUnknown); |
| - return DartUtils::NewDartOSError(&os_error); |
| - } |
| - if (os_socket_same_addr->v6_only != v6_only) { |
| - OSError os_error(-1, |
| - "The v6Only flag to bind() needs to be the same if " |
| - "binding multiple times on the same (address, port) " |
| - "combination.", |
| - OSError::kUnknown); |
| - return DartUtils::NewDartOSError(&os_error); |
| + if (port > 0) { |
| + first_os_socket = LookupByPort(port); |
| + if (first_os_socket != NULL) { |
| + // There is already a socket listening on this port. We need to ensure |
| + // that if there is one also listening on the same address, it was created |
| + // with `shared = true`, ... |
| + OSSocket* os_socket = first_os_socket; |
| + OSSocket* os_socket_same_addr = findOSSocketWithAddress(os_socket, addr); |
| + |
| + if (os_socket_same_addr != NULL) { |
| + if (!os_socket_same_addr->shared || !shared) { |
| + OSError os_error(-1, |
| + "The shared flag to bind() needs to be `true` if " |
| + "binding multiple times on the same (address, port) " |
| + "combination.", |
| + OSError::kUnknown); |
| + return DartUtils::NewDartOSError(&os_error); |
| + } |
| + if (os_socket_same_addr->v6_only != v6_only) { |
| + OSError os_error(-1, |
| + "The v6Only flag to bind() needs to be the same if " |
| + "binding multiple times on the same (address, port) " |
| + "combination.", |
| + OSError::kUnknown); |
| + return DartUtils::NewDartOSError(&os_error); |
| + } |
| + |
| + // This socket creation is the exact same as the one which originally |
| + // created the socket. We therefore increment the refcount and reuse |
| + // the file descriptor. |
| + os_socket->ref_count++; |
| + |
| + // We set as a side-effect the file descriptor on the dart |
| + // socket_object. |
| + Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); |
| + |
| + return Dart_True(); |
| } |
| - |
| - // This socket creation is the exact same as the one which originally |
| - // created the socket. We therefore increment the refcount and reuse |
| - // the file descriptor. |
| - os_socket->ref_count++; |
| - |
| - // We set as a side-effect the file descriptor on the dart socket_object. |
| - Socket::SetSocketIdNativeField(socket_object, os_socket->socketfd); |
| - |
| - return Dart_True(); |
| } |
| } |
| @@ -159,6 +163,11 @@ Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object, |
| intptr_t allocated_port = Socket::GetPort(socketfd); |
| ASSERT(allocated_port > 0); |
| + if (allocated_port != port) { |
| + ASSERT(port == 0); |
|
Florian Schneider
2016/11/30 00:21:01
Maybe add a short comment here about the scenario
kustermann
2016/11/30 10:51:10
Done.
|
| + first_os_socket = LookupByPort(allocated_port); |
| + } |
| + |
| OSSocket* os_socket = |
| new OSSocket(addr, allocated_port, v6_only, shared, socketfd); |
| os_socket->ref_count = 1; |