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

Unified Diff: runtime/bin/socket_base_macos.cc

Issue 2797993005: Re-land socket refactor with fixes for Windows. (Closed)
Patch Set: Rebased + reverted original revert Created 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/socket_base_macos.h ('k') | runtime/bin/socket_base_unsupported.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket_base_macos.cc
diff --git a/runtime/bin/socket_macos.cc b/runtime/bin/socket_base_macos.cc
similarity index 61%
copy from runtime/bin/socket_macos.cc
copy to runtime/bin/socket_base_macos.cc
index d1988693c1dca01460b3bd15622503d068ff25bc..b67d4efba1b06e4f56534e5f27724ce21583e34a 100644
--- a/runtime/bin/socket_macos.cc
+++ b/runtime/bin/socket_base_macos.cc
@@ -7,8 +7,7 @@
#include "platform/globals.h"
#if defined(HOST_OS_MACOS)
-#include "bin/socket.h"
-#include "bin/socket_macos.h"
+#include "bin/socket_base.h"
#include <errno.h> // NOLINT
#include <ifaddrs.h> // NOLINT
@@ -22,6 +21,7 @@
#include "bin/fdutils.h"
#include "bin/file.h"
+#include "bin/socket_base_macos.h"
#include "platform/signal_blocker.h"
namespace dart {
@@ -29,8 +29,8 @@ namespace bin {
SocketAddress::SocketAddress(struct sockaddr* sa) {
ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
- if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_,
- INET6_ADDRSTRLEN)) {
+ if (!SocketBase::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa),
+ as_string_, INET6_ADDRSTRLEN)) {
as_string_[0] = 0;
}
socklen_t salen = GetAddrLength(*reinterpret_cast<RawAddr*>(sa));
@@ -38,97 +38,33 @@ SocketAddress::SocketAddress(struct sockaddr* sa) {
}
-bool Socket::FormatNumericAddress(const RawAddr& addr, char* address, int len) {
- socklen_t salen = SocketAddress::GetAddrLength(addr);
- return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
- 0, NI_NUMERICHOST)) == 0);
-}
-
-
-Socket::Socket(intptr_t fd)
- : ReferenceCounted(), fd_(fd), port_(ILLEGAL_PORT) {}
-
-
-void Socket::SetClosedFd() {
- fd_ = kClosedFd;
-}
-
-
-bool Socket::Initialize() {
+bool SocketBase::Initialize() {
// Nothing to do on Mac OS.
return true;
}
-static intptr_t Create(const RawAddr& addr) {
- intptr_t fd;
- fd = NO_RETRY_EXPECTED(socket(addr.ss.ss_family, SOCK_STREAM, 0));
- if (fd < 0) {
- return -1;
- }
- if (!FDUtils::SetCloseOnExec(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
- if (!FDUtils::SetNonBlocking(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
- return fd;
-}
-
-
-static intptr_t Connect(intptr_t fd, const RawAddr& addr) {
- intptr_t result = TEMP_FAILURE_RETRY(
- connect(fd, &addr.addr, SocketAddress::GetAddrLength(addr)));
- if ((result == 0) || (errno == EINPROGRESS)) {
- return fd;
- }
- FDUtils::SaveErrorAndClose(fd);
- return -1;
-}
-
-
-intptr_t Socket::CreateConnect(const RawAddr& addr) {
- intptr_t fd = Create(addr);
- if (fd < 0) {
- return fd;
- }
-
- return Connect(fd, addr);
-}
-
-
-intptr_t Socket::CreateBindConnect(const RawAddr& addr,
- const RawAddr& source_addr) {
- intptr_t fd = Create(addr);
- if (fd < 0) {
- return fd;
- }
-
- intptr_t result = TEMP_FAILURE_RETRY(
- bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr)));
- if ((result != 0) && (errno != EINPROGRESS)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- return Connect(fd, addr);
+bool SocketBase::FormatNumericAddress(const RawAddr& addr,
+ char* address,
+ int len) {
+ socklen_t salen = SocketAddress::GetAddrLength(addr);
+ return (NO_RETRY_EXPECTED(getnameinfo(&addr.addr, salen, address, len, NULL,
+ 0, NI_NUMERICHOST)) == 0);
}
-bool Socket::IsBindError(intptr_t error_number) {
+bool SocketBase::IsBindError(intptr_t error_number) {
return error_number == EADDRINUSE || error_number == EADDRNOTAVAIL ||
error_number == EINVAL;
}
-intptr_t Socket::Available(intptr_t fd) {
+intptr_t SocketBase::Available(intptr_t fd) {
return FDUtils::AvailableBytes(fd);
}
-intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
+intptr_t SocketBase::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
ASSERT(fd >= 0);
ssize_t read_bytes = TEMP_FAILURE_RETRY(read(fd, buffer, num_bytes));
ASSERT(EAGAIN == EWOULDBLOCK);
@@ -141,10 +77,10 @@ intptr_t Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) {
}
-intptr_t Socket::RecvFrom(intptr_t fd,
- void* buffer,
- intptr_t num_bytes,
- RawAddr* addr) {
+intptr_t SocketBase::RecvFrom(intptr_t fd,
+ void* buffer,
+ intptr_t num_bytes,
+ RawAddr* addr) {
ASSERT(fd >= 0);
socklen_t addr_len = sizeof(addr->ss);
ssize_t read_bytes = TEMP_FAILURE_RETRY(
@@ -158,7 +94,9 @@ intptr_t Socket::RecvFrom(intptr_t fd,
}
-intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
+intptr_t SocketBase::Write(intptr_t fd,
+ const void* buffer,
+ intptr_t num_bytes) {
ASSERT(fd >= 0);
ssize_t written_bytes = TEMP_FAILURE_RETRY(write(fd, buffer, num_bytes));
ASSERT(EAGAIN == EWOULDBLOCK);
@@ -171,10 +109,10 @@ intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
}
-intptr_t Socket::SendTo(intptr_t fd,
- const void* buffer,
- intptr_t num_bytes,
- const RawAddr& addr) {
+intptr_t SocketBase::SendTo(intptr_t fd,
+ const void* buffer,
+ intptr_t num_bytes,
+ const RawAddr& addr) {
ASSERT(fd >= 0);
ssize_t written_bytes =
TEMP_FAILURE_RETRY(sendto(fd, buffer, num_bytes, 0, &addr.addr,
@@ -189,7 +127,7 @@ intptr_t Socket::SendTo(intptr_t fd,
}
-intptr_t Socket::GetPort(intptr_t fd) {
+intptr_t SocketBase::GetPort(intptr_t fd) {
ASSERT(fd >= 0);
RawAddr raw;
socklen_t size = sizeof(raw);
@@ -200,7 +138,7 @@ intptr_t Socket::GetPort(intptr_t fd) {
}
-SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
+SocketAddress* SocketBase::GetRemotePeer(intptr_t fd, intptr_t* port) {
ASSERT(fd >= 0);
RawAddr raw;
socklen_t size = sizeof(raw);
@@ -212,7 +150,7 @@ SocketAddress* Socket::GetRemotePeer(intptr_t fd, intptr_t* port) {
}
-void Socket::GetError(intptr_t fd, OSError* os_error) {
+void SocketBase::GetError(intptr_t fd, OSError* os_error) {
int len = sizeof(errno);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno,
reinterpret_cast<socklen_t*>(&len));
@@ -220,7 +158,7 @@ void Socket::GetError(intptr_t fd, OSError* os_error) {
}
-int Socket::GetType(intptr_t fd) {
+int SocketBase::GetType(intptr_t fd) {
struct stat buf;
int result = fstat(fd, &buf);
if (result == -1) {
@@ -239,14 +177,14 @@ int Socket::GetType(intptr_t fd) {
}
-intptr_t Socket::GetStdioHandle(intptr_t num) {
+intptr_t SocketBase::GetStdioHandle(intptr_t num) {
return num;
}
-AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
- int type,
- OSError** os_error) {
+AddressList<SocketAddress>* SocketBase::LookupAddress(const char* host,
+ int type,
+ OSError** os_error) {
// Perform a name lookup for a host name.
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
@@ -281,10 +219,10 @@ AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
}
-bool Socket::ReverseLookup(const RawAddr& addr,
- char* host,
- intptr_t host_len,
- OSError** os_error) {
+bool SocketBase::ReverseLookup(const RawAddr& addr,
+ char* host,
+ intptr_t host_len,
+ OSError** os_error) {
ASSERT(host_len >= NI_MAXHOST);
int status = NO_RETRY_EXPECTED(
getnameinfo(&addr.addr, SocketAddress::GetAddrLength(addr), host,
@@ -299,7 +237,7 @@ bool Socket::ReverseLookup(const RawAddr& addr,
}
-bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
+bool SocketBase::ParseAddress(int type, const char* address, RawAddr* addr) {
int result;
if (type == SocketAddress::TYPE_IPV4) {
result = inet_pton(AF_INET, address, &addr->in.sin_addr);
@@ -311,39 +249,6 @@ bool Socket::ParseAddress(int type, const char* address, RawAddr* addr) {
}
-intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) {
- intptr_t fd;
-
- fd = NO_RETRY_EXPECTED(socket(addr.addr.sa_family, SOCK_DGRAM, IPPROTO_UDP));
- if (fd < 0) {
- return -1;
- }
-
- if (!FDUtils::SetCloseOnExec(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- if (reuseAddress) {
- int optval = 1;
- VOID_NO_RETRY_EXPECTED(
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
- }
-
- if (NO_RETRY_EXPECTED(
- bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- if (!FDUtils::SetNonBlocking(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
- return fd;
-}
-
-
static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
if (ifa->ifa_addr == NULL) {
// OpenVPN's virtual device tun0.
@@ -356,12 +261,12 @@ static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
}
-bool Socket::ListInterfacesSupported() {
+bool SocketBase::ListInterfacesSupported() {
return true;
}
-AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
+AddressList<InterfaceSocketAddress>* SocketBase::ListInterfaces(
int type,
OSError** os_error) {
struct ifaddrs* ifaddr;
@@ -400,100 +305,13 @@ AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
}
-intptr_t ServerSocket::CreateBindListen(const RawAddr& addr,
- intptr_t backlog,
- bool v6_only) {
- intptr_t fd;
-
- fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
- if (fd < 0) {
- return -1;
- }
-
- if (!FDUtils::SetCloseOnExec(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- int optval = 1;
- VOID_NO_RETRY_EXPECTED(
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
-
- if (addr.ss.ss_family == AF_INET6) {
- optval = v6_only ? 1 : 0;
- VOID_NO_RETRY_EXPECTED(
- setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
- }
-
- if (NO_RETRY_EXPECTED(
- bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- // Test for invalid socket port 65535 (some browsers disallow it).
- if ((SocketAddress::GetAddrPort(addr) == 0) &&
- (Socket::GetPort(fd) == 65535)) {
- // Don't close the socket until we have created a new socket, ensuring
- // that we do not get the bad port number again.
- intptr_t new_fd = CreateBindListen(addr, backlog, v6_only);
- FDUtils::SaveErrorAndClose(fd);
- return new_fd;
- }
-
- if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
-
- if (!FDUtils::SetNonBlocking(fd)) {
- FDUtils::SaveErrorAndClose(fd);
- return -1;
- }
- return fd;
-}
-
-
-bool ServerSocket::StartAccept(intptr_t fd) {
- USE(fd);
- return true;
-}
-
-
-intptr_t ServerSocket::Accept(intptr_t fd) {
- intptr_t socket;
- struct sockaddr clientaddr;
- socklen_t addrlen = sizeof(clientaddr);
- socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
- if (socket == -1) {
- if (errno == EAGAIN) {
- // We need to signal to the caller that this is actually not an
- // error. We got woken up from the poll on the listening socket,
- // but there is no connection ready to be accepted.
- ASSERT(kTemporaryFailure != -1);
- socket = kTemporaryFailure;
- }
- } else {
- if (!FDUtils::SetCloseOnExec(socket)) {
- FDUtils::SaveErrorAndClose(socket);
- return -1;
- }
- if (!FDUtils::SetNonBlocking(socket)) {
- FDUtils::SaveErrorAndClose(socket);
- return -1;
- }
- }
- return socket;
-}
-
-
-void Socket::Close(intptr_t fd) {
+void SocketBase::Close(intptr_t fd) {
ASSERT(fd >= 0);
VOID_TEMP_FAILURE_RETRY(close(fd));
}
-bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
+bool SocketBase::GetNoDelay(intptr_t fd, bool* enabled) {
int on;
socklen_t len = sizeof(on);
int err = NO_RETRY_EXPECTED(getsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
@@ -505,7 +323,7 @@ bool Socket::GetNoDelay(intptr_t fd, bool* enabled) {
}
-bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
+bool SocketBase::SetNoDelay(intptr_t fd, bool enabled) {
int on = enabled ? 1 : 0;
return NO_RETRY_EXPECTED(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char*>(&on),
@@ -513,7 +331,9 @@ bool Socket::SetNoDelay(intptr_t fd, bool enabled) {
}
-bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
+bool SocketBase::GetMulticastLoop(intptr_t fd,
+ intptr_t protocol,
+ bool* enabled) {
uint8_t on;
socklen_t len = sizeof(on);
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
@@ -528,7 +348,9 @@ bool Socket::GetMulticastLoop(intptr_t fd, intptr_t protocol, bool* enabled) {
}
-bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
+bool SocketBase::SetMulticastLoop(intptr_t fd,
+ intptr_t protocol,
+ bool enabled) {
u_int on = enabled ? 1 : 0;
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_LOOP
@@ -539,7 +361,7 @@ bool Socket::SetMulticastLoop(intptr_t fd, intptr_t protocol, bool enabled) {
}
-bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
+bool SocketBase::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
uint8_t v;
socklen_t len = sizeof(v);
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
@@ -554,7 +376,7 @@ bool Socket::GetMulticastHops(intptr_t fd, intptr_t protocol, int* value) {
}
-bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
+bool SocketBase::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
int v = value;
int level = protocol == SocketAddress::TYPE_IPV4 ? IPPROTO_IP : IPPROTO_IPV6;
int optname = protocol == SocketAddress::TYPE_IPV4 ? IP_MULTICAST_TTL
@@ -564,7 +386,7 @@ bool Socket::SetMulticastHops(intptr_t fd, intptr_t protocol, int value) {
}
-bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
+bool SocketBase::GetBroadcast(intptr_t fd, bool* enabled) {
int on;
socklen_t len = sizeof(on);
int err = NO_RETRY_EXPECTED(getsockopt(fd, SOL_SOCKET, SO_BROADCAST,
@@ -576,7 +398,7 @@ bool Socket::GetBroadcast(intptr_t fd, bool* enabled) {
}
-bool Socket::SetBroadcast(intptr_t fd, bool enabled) {
+bool SocketBase::SetBroadcast(intptr_t fd, bool enabled) {
int on = enabled ? 1 : 0;
return NO_RETRY_EXPECTED(setsockopt(fd, SOL_SOCKET, SO_BROADCAST,
reinterpret_cast<char*>(&on),
@@ -619,18 +441,18 @@ static bool JoinOrLeaveMulticast(intptr_t fd,
}
}
-bool Socket::JoinMulticast(intptr_t fd,
- const RawAddr& addr,
- const RawAddr& interface,
- int interfaceIndex) {
+bool SocketBase::JoinMulticast(intptr_t fd,
+ const RawAddr& addr,
+ const RawAddr& interface,
+ int interfaceIndex) {
return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, true);
}
-bool Socket::LeaveMulticast(intptr_t fd,
- const RawAddr& addr,
- const RawAddr& interface,
- int interfaceIndex) {
+bool SocketBase::LeaveMulticast(intptr_t fd,
+ const RawAddr& addr,
+ const RawAddr& interface,
+ int interfaceIndex) {
return JoinOrLeaveMulticast(fd, addr, interface, interfaceIndex, false);
}
« no previous file with comments | « runtime/bin/socket_base_macos.h ('k') | runtime/bin/socket_base_unsupported.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698