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

Unified Diff: runtime/bin/socket_win.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_patch.dart ('k') | runtime/bin/vmstats_impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/socket_win.cc
diff --git a/runtime/bin/socket_win.cc b/runtime/bin/socket_win.cc
index 54f66390c00e94c0c8d0aa163b3dd625821be38c..ee25b500062ce6a30eb28116a7643fa5841ac7cb 100644
--- a/runtime/bin/socket_win.cc
+++ b/runtime/bin/socket_win.cc
@@ -11,6 +11,40 @@
#include "bin/log.h"
#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_storage *sockaddr =
+ reinterpret_cast<sockaddr_storage *>(addrinfo->ai_addr);
+
+ // Clear the port before calling WSAAddressToString as WSAAddressToString
+ // includes the port in the formatted string.
+ DWORD len = INET6_ADDRSTRLEN;
+ int err = WSAAddressToStringA(reinterpret_cast<LPSOCKADDR>(sockaddr),
+ sizeof(sockaddr_storage),
+ NULL,
+ as_string_,
+ &len);
+
+ if (err != 0) {
+ as_string_[0] = 0;
+ }
+ memmove(reinterpret_cast<void *>(&addr_),
+ addrinfo->ai_addr,
+ addrinfo->ai_addrlen);
+}
+
bool Socket::Initialize() {
static bool socket_initialized = false;
if (socket_initialized) return true;
@@ -47,34 +81,34 @@ intptr_t Socket::Write(intptr_t fd, const void* buffer, intptr_t num_bytes) {
intptr_t Socket::GetPort(intptr_t fd) {
ASSERT(reinterpret_cast<Handle*>(fd)->is_socket());
SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
- struct sockaddr_in socket_address;
+ struct sockaddr_storage socket_address;
socklen_t size = sizeof(socket_address);
if (getsockname(socket_handle->socket(),
reinterpret_cast<struct sockaddr *>(&socket_address),
- &size)) {
- Log::PrintErr("Error getsockname: %s\n", strerror(errno));
+ &size) == SOCKET_ERROR) {
+ Log::PrintErr("Error getsockname: %d\n", WSAGetLastError());
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(reinterpret_cast<Handle*>(fd)->is_socket());
SocketHandle* socket_handle = reinterpret_cast<SocketHandle*>(fd);
- struct sockaddr_in socket_address;
- socklen_t size = sizeof(socket_address);
+ struct sockaddr_storage socket_address;
+ socklen_t size = sizeof(sockaddr_storage);
if (getpeername(socket_handle->socket(),
reinterpret_cast<struct sockaddr *>(&socket_address),
&size)) {
- Log::PrintErr("Error getpeername: %s\n", strerror(errno));
+ Log::PrintErr("Error getpeername: %d\n", WSAGetLastError());
return false;
}
- *port = ntohs(socket_address.sin_port);
+ *port = SOCKADDR_STORAGE_GET_PORT(socket_address);
// Clear the port before calling WSAAddressToString as WSAAddressToString
// includes the port in the formatted string.
- socket_address.sin_port = 0;
- DWORD len = INET_ADDRSTRLEN;
+ SOCKADDR_STORAGE_SET_PORT(socket_address, 0);
+ DWORD len = INET6_ADDRSTRLEN;
int err = WSAAddressToStringA(reinterpret_cast<LPSOCKADDR>(&socket_address),
sizeof(socket_address),
NULL,
@@ -87,8 +121,8 @@ bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
return true;
}
-intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
- SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
+intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) {
+ SOCKET s = socket(addr.ss_family, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) {
return -1;
}
@@ -105,29 +139,11 @@ intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
FATAL("Failed setting SO_LINGER on socket");
}
- // Perform a name lookup for an IPv4 address.
- struct addrinfo hints;
- memset(&hints, 0, sizeof(hints));
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- struct addrinfo* result = NULL;
- status = getaddrinfo(host, 0, &hints, &result);
- if (status != NO_ERROR) {
- return -1;
- }
-
- // Copy IPv4 address and set the port.
- struct sockaddr_in server_address;
- memcpy(&server_address,
- reinterpret_cast<sockaddr_in *>(result->ai_addr),
- sizeof(server_address));
- server_address.sin_port = htons(port);
- freeaddrinfo(result); // Free data allocated by getaddrinfo.
+ SOCKADDR_STORAGE_SET_PORT(addr, port);
status = connect(
s,
- reinterpret_cast<struct sockaddr*>(&server_address),
- sizeof(server_address));
+ reinterpret_cast<struct sockaddr*>(&addr),
+ SocketAddress::GetAddrLength(addr));
if (status == SOCKET_ERROR) {
DWORD rc = WSAGetLastError();
closesocket(s);
@@ -192,13 +208,17 @@ intptr_t ServerSocket::Accept(intptr_t fd) {
}
-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) {
Initialize();
+
+ // 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);
@@ -209,35 +229,27 @@ const char* Socket::LookupIPv4Address(char* host, OSError** os_error) {
*os_error = new OSError();
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);
-
- // Clear the port before calling WSAAddressToString as WSAAddressToString
- // includes the port in the formatted string.
- DWORD len = INET_ADDRSTRLEN;
- int err = WSAAddressToStringA(reinterpret_cast<LPSOCKADDR>(sockaddr),
- sizeof(sockaddr_in),
- NULL,
- buffer,
- &len);
- if (err != 0) {
- 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++;
+ }
+ 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++;
+ }
}
- return buffer;
+ freeaddrinfo(info);
+ return addresses;
}
-intptr_t ServerSocket::CreateBindListen(const char* host,
+intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr,
intptr_t port,
intptr_t backlog) {
- unsigned long socket_addr = inet_addr(host); // NOLINT
- if (socket_addr == INADDR_NONE) {
- return -5;
- }
-
- SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ SOCKET s = socket(addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
return -1;
}
@@ -255,14 +267,19 @@ intptr_t ServerSocket::CreateBindListen(const char* host,
return -1;
}
- sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr.s_addr = socket_addr;
- addr.sin_port = htons(port);
+ if (addr.ss_family == AF_INET6) {
+ optval = false;
+ setsockopt(s,
+ IPPROTO_IPV6,
+ IPV6_V6ONLY,
+ reinterpret_cast<const char*>(&optval),
+ sizeof(optval));
+ }
+
+ SOCKADDR_STORAGE_SET_PORT(addr, port);
status = bind(s,
reinterpret_cast<struct sockaddr *>(&addr),
- sizeof(addr));
+ SocketAddress::GetAddrLength(addr));
if (status == SOCKET_ERROR) {
DWORD rc = WSAGetLastError();
closesocket(s);
« no previous file with comments | « runtime/bin/socket_patch.dart ('k') | runtime/bin/vmstats_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698