From b83fb067418310edb6fed414c449902f710fffeb Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Tue, 22 Nov 2016 12:49:01 -0800 Subject: [PATCH] Fuchsia sockets: - Enable FDUtils::AvailableBytes. - Disable Socket::SetNoDelay. - Implement Socket::GetRemotePeer same as Linux. - Implement Socket::ParseAddress same as Linux. R=asiva@google.com Review URL: https://codereview.chromium.org/2522923002 . --- runtime/bin/fdutils_fuchsia.cc | 5 --- runtime/bin/socket_fuchsia.cc | 28 ++++++++++++---- runtime/tests/vm/dart/hello_fuchsia_test.dart | 32 +++++++++++++++++-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/runtime/bin/fdutils_fuchsia.cc b/runtime/bin/fdutils_fuchsia.cc index dd9e881c8d5..4aa4b29f573 100644 --- a/runtime/bin/fdutils_fuchsia.cc +++ b/runtime/bin/fdutils_fuchsia.cc @@ -71,8 +71,6 @@ bool FDUtils::IsBlocking(intptr_t fd, bool* is_blocking) { intptr_t FDUtils::AvailableBytes(intptr_t fd) { -// TODO(MG-364): Enable this code when it is supported. -#if 0 int available; // ioctl for FIONREAD expects an 'int*' argument. int result = NO_RETRY_EXPECTED(ioctl(fd, FIONREAD, &available)); if (result < 0) { @@ -80,9 +78,6 @@ intptr_t FDUtils::AvailableBytes(intptr_t fd) { } ASSERT(available >= 0); return static_cast(available); -#endif - errno = ENOTSUP; - return -1; } diff --git a/runtime/bin/socket_fuchsia.cc b/runtime/bin/socket_fuchsia.cc index a6498ce1b59..0109bea3290 100644 --- a/runtime/bin/socket_fuchsia.cc +++ b/runtime/bin/socket_fuchsia.cc @@ -242,9 +242,14 @@ intptr_t Socket::GetPort(intptr_t fd) { SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) { - LOG_ERR("Socket::GetRemotePeer is unimplemented\n"); - UNIMPLEMENTED(); - return NULL; + ASSERT(fd >= 0); + RawAddr raw; + socklen_t size = sizeof(raw); + if (NO_RETRY_EXPECTED(getpeername(fd, &raw.addr, &size))) { + return NULL; + } + *port = SocketAddress::GetAddrPort(raw); + return new SocketAddress(&raw.addr); } @@ -324,9 +329,15 @@ bool Socket::ReverseLookup(const RawAddr& addr, bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) { - LOG_ERR("Socket::ParseAddress is unimplemented\n"); - UNIMPLEMENTED(); - return false; + int result; + if (type == SocketAddress::TYPE_IPV4) { + result = NO_RETRY_EXPECTED(inet_pton(AF_INET, address, &addr->in.sin_addr)); + } else { + ASSERT(type == SocketAddress::TYPE_IPV6); + result = + NO_RETRY_EXPECTED(inet_pton(AF_INET6, address, &addr->in6.sin6_addr)); + } + return (result == 1); } @@ -478,10 +489,15 @@ bool Socket::GetNoDelay(intptr_t fd, bool* enabled) { bool Socket::SetNoDelay(intptr_t fd, bool enabled) { +// TODO(US-94): Enable. +#if 0 int on = enabled ? 1 : 0; return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&on), sizeof(on))) == 0; +#else + return true; +#endif } diff --git a/runtime/tests/vm/dart/hello_fuchsia_test.dart b/runtime/tests/vm/dart/hello_fuchsia_test.dart index 9ed439b3e0a..100cb04cd2c 100644 --- a/runtime/tests/vm/dart/hello_fuchsia_test.dart +++ b/runtime/tests/vm/dart/hello_fuchsia_test.dart @@ -5,6 +5,20 @@ import "dart:async"; import "dart:io"; +testAddressParse() async { + print(new InternetAddress("1.0.2.3").rawAddress); + print(new InternetAddress("1.0.2.3").type); + + print(new InternetAddress("::1").rawAddress); + print(new InternetAddress("::1").type); + + try { + print(new InternetAddress("localhost")); + } catch (e) { + print(e); + } +} + testSimpleBind() async { var s = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); print("port = ${s.port}"); @@ -20,6 +34,13 @@ testSimpleConnect() async { }); var socket = await RawSocket.connect("127.0.0.1", server.port); print("socket port = ${socket.port}"); + if (socket.remoteAddress.address != "127.0.0.1" || + socket.remoteAddress.type != InternetAddressType.IP_V4) { + throw "Bad remote address ${socket.remoteAddress}"; + } + if (socket.remotePort is! int) { + throw "Bad remote port ${socket.remotePort}"; + } await server.close(); await socket.close(); } @@ -197,6 +218,10 @@ Future testGoogleUrl(SecurityContext context, String outcome) async { main() async { print("Hello, Fuchsia!"); + print("testAddressParse"); + await testAddressParse(); + print("testAddressParse done"); + print("testSimpleBind"); await testSimpleBind(); print("testSimpleBind done"); @@ -205,13 +230,14 @@ main() async { await testSimpleConnect(); print("testSimpleConnect done"); + // TODO(US-81): Enable. // print("testSimpleReadWrite"); // await testSimpleReadWrite(dropReads: false); // print("testSimpleReadWrite done"); - // print("testGoogleUrl"); - // await testGoogleUrl(null, 'pass'); - // print("testGoogleUrl done"); + print("testGoogleUrl"); + await testGoogleUrl(null, 'pass'); + print("testGoogleUrl done"); print("Goodbyte, Fuchsia!"); }