Index: runtime/bin/socket_macos.cc |
diff --git a/runtime/bin/socket_macos.cc b/runtime/bin/socket_macos.cc |
index 67201713dcbbc94acc39aee1e275aecc8580a5d2..5385d8491275fa1effac4c97c1e8c41b8ea95fe2 100644 |
--- a/runtime/bin/socket_macos.cc |
+++ b/runtime/bin/socket_macos.cc |
@@ -19,18 +19,44 @@ |
#include "bin/socket.h" |
+#define SOCKADDR_STORAGE_SET_PORT(addr, port) \ |
+ if (addr.ss_family == AF_INET) { \ |
+ reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); \ |
+ } else { \ |
+ reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port = htons(port); \ |
+ } |
+ |
+ |
+#define SOCKADDR_STORAGE_GET_PORT(addr) \ |
+ addr.ss_family == AF_INET ? \ |
+ ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \ |
+ ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port) |
+ |
+ |
+SocketAddress::SocketAddress(struct addrinfo* addrinfo) { |
+ ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
+ sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); |
+ const char* result = inet_ntop(addrinfo->ai_family, |
+ &sockaddr->sin_addr, |
+ as_string_, |
+ INET6_ADDRSTRLEN); |
+ if (result == NULL) as_string_[0] = 0; |
+ memmove(reinterpret_cast<void *>(&addr_), |
+ addrinfo->ai_addr, |
+ addrinfo->ai_addrlen); |
+} |
+ |
+ |
bool Socket::Initialize() { |
// Nothing to do on Mac OS. |
return true; |
} |
-intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
+intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { |
intptr_t fd; |
- struct hostent* server; |
- struct sockaddr_in server_address; |
- fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
+ fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
if (fd < 0) { |
Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
return -1; |
@@ -39,24 +65,15 @@ intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
FDUtils::SetCloseOnExec(fd); |
Socket::SetNonBlocking(fd); |
- server = gethostbyname(host); |
- if (server == NULL) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
- Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
- return -1; |
- } |
- |
- server_address.sin_family = AF_INET; |
- server_address.sin_port = htons(port); |
- bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); |
- memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
+ SOCKADDR_STORAGE_SET_PORT(addr, port); |
intptr_t result = TEMP_FAILURE_RETRY( |
connect(fd, |
- reinterpret_cast<struct sockaddr *>(&server_address), |
- sizeof(server_address))); |
+ reinterpret_cast<struct sockaddr*>(&addr), |
+ SocketAddress::GetAddrLength(addr))); |
if (result == 0 || errno == EINPROGRESS) { |
return fd; |
} |
+ TEMP_FAILURE_RETRY(close(fd)); |
return -1; |
} |
@@ -94,7 +111,7 @@ int Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) { |
intptr_t Socket::GetPort(intptr_t fd) { |
ASSERT(fd >= 0); |
- struct sockaddr_in socket_address; |
+ struct sockaddr_storage socket_address; |
socklen_t size = sizeof(socket_address); |
if (TEMP_FAILURE_RETRY( |
getsockname(fd, |
@@ -103,13 +120,13 @@ intptr_t Socket::GetPort(intptr_t fd) { |
Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
return 0; |
} |
- return ntohs(socket_address.sin_port); |
+ return SOCKADDR_STORAGE_GET_PORT(socket_address); |
} |
bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
ASSERT(fd >= 0); |
- struct sockaddr_in socket_address; |
+ struct sockaddr_storage socket_address; |
socklen_t size = sizeof(socket_address); |
if (TEMP_FAILURE_RETRY( |
getpeername(fd, |
@@ -118,14 +135,24 @@ bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
return false; |
} |
- if (inet_ntop(socket_address.sin_family, |
- reinterpret_cast<const void *>(&socket_address.sin_addr), |
+ const void* src; |
+ if (socket_address.ss_family == AF_INET6) { |
+ const sockaddr_in6* in6 = |
+ reinterpret_cast<const sockaddr_in6*>(&socket_address); |
+ src = reinterpret_cast<const void*>(&in6->sin6_addr); |
+ } else { |
+ const sockaddr_in* in = |
+ reinterpret_cast<const sockaddr_in*>(&socket_address); |
+ src = reinterpret_cast<const void*>(&in->sin_addr); |
+ } |
+ if (inet_ntop(socket_address.ss_family, |
+ src, |
host, |
INET_ADDRSTRLEN) == NULL) { |
Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
return false; |
} |
- *port = ntohs(socket_address.sin_port); |
+ *port = SOCKADDR_STORAGE_GET_PORT(socket_address); |
return true; |
} |
@@ -157,12 +184,15 @@ intptr_t Socket::GetStdioHandle(int num) { |
} |
-const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
- // Perform a name lookup for an IPv4 address. |
+SocketAddresses* Socket::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)); |
- hints.ai_family = AF_INET; |
+ hints.ai_family = SocketAddress::FromType(type); |
hints.ai_socktype = SOCK_STREAM; |
+ hints.ai_flags = 0; |
hints.ai_protocol = IPPROTO_TCP; |
struct addrinfo* info = NULL; |
int status = getaddrinfo(host, 0, &hints, &info); |
@@ -173,34 +203,29 @@ const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
OSError::kGetAddressInfo); |
return NULL; |
} |
- // Convert the address into IPv4 dotted decimal notation. |
- char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); |
- sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); |
- const char* result = inet_ntop(AF_INET, |
- reinterpret_cast<void *>(&sockaddr->sin_addr), |
- buffer, |
- INET_ADDRSTRLEN); |
- if (result == NULL) { |
- free(buffer); |
- return NULL; |
+ intptr_t count = 0; |
+ for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
+ if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
} |
- ASSERT(result == buffer); |
- return buffer; |
+ SocketAddresses* addresses = new SocketAddresses(count); |
+ intptr_t i = 0; |
+ for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
+ if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
+ addresses->SetAt(i, new SocketAddress(c)); |
+ i++; |
+ } |
+ } |
+ freeaddrinfo(info); |
+ return addresses; |
} |
-intptr_t ServerSocket::CreateBindListen(const char* host, |
+intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, |
intptr_t port, |
intptr_t backlog) { |
intptr_t fd; |
- struct sockaddr_in server_address; |
- in_addr_t s_addr = inet_addr(host); |
- if (s_addr == INADDR_NONE) { |
- return -5; |
- } |
- |
- fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); |
+ fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
if (fd < 0) return -1; |
FDUtils::SetCloseOnExec(fd); |
@@ -209,15 +234,17 @@ intptr_t ServerSocket::CreateBindListen(const char* host, |
VOID_TEMP_FAILURE_RETRY( |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
- server_address.sin_family = AF_INET; |
- server_address.sin_port = htons(port); |
- server_address.sin_addr.s_addr = s_addr; |
- memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); |
+ if (addr.ss_family == AF_INET6) { |
+ optval = 0; |
+ VOID_TEMP_FAILURE_RETRY( |
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
+ } |
+ SOCKADDR_STORAGE_SET_PORT(addr, port); |
if (TEMP_FAILURE_RETRY( |
bind(fd, |
- reinterpret_cast<struct sockaddr *>(&server_address), |
- sizeof(server_address))) < 0) { |
+ reinterpret_cast<struct sockaddr*>(&addr), |
+ SocketAddress::GetAddrLength(addr))) < 0) { |
VOID_TEMP_FAILURE_RETRY(close(fd)); |
return -1; |
} |