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

Unified Diff: runtime/bin/socket_android.cc

Issue 14083007: Add new InternetAddress class with a static lookup function (including IPv6 results). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix new test. Created 7 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.cc ('k') | runtime/bin/socket_linux.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket_android.cc
diff --git a/runtime/bin/socket_android.cc b/runtime/bin/socket_android.cc
index 7e3871da5b5feaa707b9b539af7710d13efa08a7..cf3fe4475450845b10aa1c10268396a21c5e00e1 100644
--- a/runtime/bin/socket_android.cc
+++ b/runtime/bin/socket_android.cc
@@ -17,18 +17,44 @@
#include "bin/log.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 Android.
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;
@@ -37,24 +63,15 @@ intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
FDUtils::SetCloseOnExec(fd);
Socket::SetNonBlocking(fd);
- server = gethostbyname(host);
- if (server == NULL) {
- 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;
}
@@ -92,7 +109,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,
@@ -101,13 +118,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,
@@ -116,14 +133,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;
}
@@ -145,12 +172,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);
@@ -161,38 +191,30 @@ 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));
- if (fd < 0) {
- Log::PrintErr("Error CreateBind: %s\n", strerror(errno));
- return -1;
- }
+ fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0));
+ if (fd < 0) return -1;
FDUtils::SetCloseOnExec(fd);
@@ -200,22 +222,23 @@ intptr_t ServerSocket::CreateBindListen(const char* host,
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;
+ 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) {
TEMP_FAILURE_RETRY(close(fd));
- Log::PrintErr("Error Bind: %s\n", strerror(errno));
return -1;
}
- if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
- Log::PrintErr("Error Listen: %s\n", strerror(errno));
+ if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
+ TEMP_FAILURE_RETRY(close(fd));
return -1;
}
« no previous file with comments | « runtime/bin/socket.cc ('k') | runtime/bin/socket_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698