Chromium Code Reviews| Index: runtime/bin/socket_linux.cc |
| diff --git a/runtime/bin/socket_linux.cc b/runtime/bin/socket_linux.cc |
| index 23e492c930f58bb6446b0fb26c896678816d27ad..d824c86e3e5dc3e5e4f50b67394a72350ddeccce 100644 |
| --- a/runtime/bin/socket_linux.cc |
| +++ b/runtime/bin/socket_linux.cc |
| @@ -19,18 +19,31 @@ |
| #include "bin/socket.h" |
| +SocketAddress::SocketAddress(struct addrinfo* addrinfo) { |
| + char* buffer = new char[INET6_ADDRSTRLEN]; |
| + sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); |
| + const char* as_string = inet_ntop(addrinfo->ai_family, |
| + &sockaddr->sin_addr, |
| + buffer, |
| + INET6_ADDRSTRLEN); |
| + if (as_string == NULL) delete[] buffer; |
| + memmove(reinterpret_cast<void *>(&addr_), |
| + addrinfo->ai_addr, |
| + addrinfo->ai_addrlen); |
| + as_string_ = as_string; |
| +} |
| + |
| + |
| bool Socket::Initialize() { |
| // Nothing to do on Linux. |
| 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,28 +52,15 @@ intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { |
| FDUtils::SetCloseOnExec(fd); |
| Socket::SetNonBlocking(fd); |
| - static const size_t kTempBufSize = 1024; |
| - char temp_buf[kTempBufSize]; |
| - struct hostent *unused; |
| - int err; |
| - if (gethostbyname_r( |
| - host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { |
| - 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)); |
| + reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); |
|
Søren Gjesse
2013/04/18 12:59:24
We better switch on family here
if (addr.ss_famil
Anders Johnsen
2013/04/19 09:45:36
Done.
|
| intptr_t result = TEMP_FAILURE_RETRY( |
| connect(fd, |
| - reinterpret_cast<struct sockaddr *>(&server_address), |
| - sizeof(server_address))); |
| + reinterpret_cast<struct sockaddr*>(&addr), |
| + SOCKADDR_STORAGE_LENGTH(addr))); |
| if (result == 0 || errno == EINPROGRESS) { |
| return fd; |
| } |
| + TEMP_FAILURE_RETRY(close(fd)); |
| return -1; |
| } |
| @@ -160,14 +160,14 @@ intptr_t Socket::GetStdioHandle(int num) { |
| return static_cast<intptr_t>(num); |
| } |
| - |
| -const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { |
| +SocketAddresses* Socket::LookupAddress(char* host, OSError** os_error) { |
| // Perform a name lookup for an IPv4 address. |
| struct addrinfo hints; |
| memset(&hints, 0, sizeof(hints)); |
| - hints.ai_family = AF_INET; |
| + hints.ai_family = AF_UNSPEC; |
| hints.ai_socktype = SOCK_STREAM; |
| - hints.ai_protocol = IPPROTO_TCP; |
| + hints.ai_flags = 0; |
| + hints.ai_protocol = 0; |
|
Søren Gjesse
2013/04/18 12:59:24
Don't you still want only IPPROTO_TCP?
Anders Johnsen
2013/04/19 09:45:36
Done.
|
| struct addrinfo* info = NULL; |
| int status = getaddrinfo(host, 0, &hints, &info); |
| if (status != 0) { |
| @@ -177,34 +177,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++; |
| + } |
| + 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++; |
| + } |
| } |
| - ASSERT(result == buffer); |
| - 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) { |
| 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); |
| @@ -213,15 +208,17 @@ 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))); |
| + } |
| + reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); |
| if (TEMP_FAILURE_RETRY( |
| bind(fd, |
| - reinterpret_cast<struct sockaddr *>(&server_address), |
| - sizeof(server_address))) < 0) { |
| + reinterpret_cast<struct sockaddr*>(&addr), |
| + SOCKADDR_STORAGE_LENGTH(addr))) < 0) { |
| TEMP_FAILURE_RETRY(close(fd)); |
| return -1; |
| } |