Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
| 6 #if defined(TARGET_OS_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 7 | 7 |
| 8 #include <errno.h> // NOLINT | 8 #include <errno.h> // NOLINT |
| 9 #include <stdio.h> // NOLINT | 9 #include <stdio.h> // NOLINT |
| 10 #include <stdlib.h> // NOLINT | 10 #include <stdlib.h> // NOLINT |
| 11 #include <string.h> // NOLINT | 11 #include <string.h> // NOLINT |
| 12 #include <sys/stat.h> // NOLINT | 12 #include <sys/stat.h> // NOLINT |
| 13 #include <unistd.h> // NOLINT | 13 #include <unistd.h> // NOLINT |
| 14 #include <netinet/tcp.h> // NOLINT | 14 #include <netinet/tcp.h> // NOLINT |
| 15 | 15 |
| 16 #include "bin/fdutils.h" | 16 #include "bin/fdutils.h" |
| 17 #include "bin/file.h" | 17 #include "bin/file.h" |
| 18 #include "bin/log.h" | 18 #include "bin/log.h" |
| 19 #include "bin/socket.h" | 19 #include "bin/socket.h" |
| 20 | 20 |
| 21 | 21 |
| 22 bool Socket::Initialize() { | 22 bool Socket::Initialize() { |
| 23 // Nothing to do on Linux. | 23 // Nothing to do on Linux. |
| 24 return true; | 24 return true; |
| 25 } | 25 } |
| 26 | 26 |
| 27 | 27 |
| 28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 28 intptr_t Socket::CreateConnect(SocketAddress::Type type, |
|
Søren Gjesse
2013/04/18 09:07:40
I don't think we need the type. sockaddr_in has a
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
| 29 uint8_t* host_data, | |
|
Søren Gjesse
2013/04/18 09:07:40
sockaddr_storage* address or sockaddr_in* address
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
| 30 intptr_t host_length, | |
| 31 const intptr_t port) { | |
| 29 intptr_t fd; | 32 intptr_t fd; |
| 30 struct hostent server; | |
| 31 struct sockaddr_in server_address; | |
| 32 | 33 |
| 33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 34 fd = TEMP_FAILURE_RETRY( |
| 35 socket(type == SocketAddress::IPv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0)); | |
| 34 if (fd < 0) { | 36 if (fd < 0) { |
| 35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 37 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
| 36 return -1; | 38 return -1; |
| 37 } | 39 } |
| 38 | 40 |
| 39 FDUtils::SetCloseOnExec(fd); | 41 FDUtils::SetCloseOnExec(fd); |
| 40 Socket::SetNonBlocking(fd); | 42 Socket::SetNonBlocking(fd); |
| 41 | 43 |
| 42 static const size_t kTempBufSize = 1024; | 44 struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(host_data); |
| 43 char temp_buf[kTempBufSize]; | 45 in->sin_port = htons(port); |
| 44 struct hostent *unused; | |
| 45 int err; | |
| 46 if (gethostbyname_r( | |
| 47 host, &server, temp_buf, kTempBufSize, &unused, &err) != 0) { | |
| 48 TEMP_FAILURE_RETRY(close(fd)); | |
| 49 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | |
| 50 return -1; | |
| 51 } | |
| 52 | |
| 53 server_address.sin_family = AF_INET; | |
| 54 server_address.sin_port = htons(port); | |
| 55 bcopy(server.h_addr, &server_address.sin_addr.s_addr, server.h_length); | |
| 56 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | |
| 57 intptr_t result = TEMP_FAILURE_RETRY( | 46 intptr_t result = TEMP_FAILURE_RETRY( |
| 58 connect(fd, | 47 connect(fd, |
| 59 reinterpret_cast<struct sockaddr *>(&server_address), | 48 reinterpret_cast<struct sockaddr*>(host_data), |
| 60 sizeof(server_address))); | 49 host_length)); |
| 61 if (result == 0 || errno == EINPROGRESS) { | 50 if (result == 0 || errno == EINPROGRESS) { |
| 62 return fd; | 51 return fd; |
| 63 } | 52 } |
| 53 TEMP_FAILURE_RETRY(close(fd)); | |
| 64 return -1; | 54 return -1; |
| 65 } | 55 } |
| 66 | 56 |
| 67 | 57 |
| 68 intptr_t Socket::Available(intptr_t fd) { | 58 intptr_t Socket::Available(intptr_t fd) { |
| 69 return FDUtils::AvailableBytes(fd); | 59 return FDUtils::AvailableBytes(fd); |
| 70 } | 60 } |
| 71 | 61 |
| 72 | 62 |
| 73 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 63 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 if (S_ISREG(buf.st_mode)) return File::kFile; | 144 if (S_ISREG(buf.st_mode)) return File::kFile; |
| 155 return File::kOther; | 145 return File::kOther; |
| 156 } | 146 } |
| 157 | 147 |
| 158 | 148 |
| 159 intptr_t Socket::GetStdioHandle(int num) { | 149 intptr_t Socket::GetStdioHandle(int num) { |
| 160 return static_cast<intptr_t>(num); | 150 return static_cast<intptr_t>(num); |
| 161 } | 151 } |
| 162 | 152 |
| 163 | 153 |
| 164 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 154 SocketAddresses* Socket::LookupAddress(char* host, OSError** os_error) { |
| 165 // Perform a name lookup for an IPv4 address. | 155 // Perform a name lookup for an IPv4 address. |
| 166 struct addrinfo hints; | 156 struct addrinfo hints; |
| 167 memset(&hints, 0, sizeof(hints)); | 157 memset(&hints, 0, sizeof(hints)); |
| 168 hints.ai_family = AF_INET; | 158 hints.ai_family = AF_UNSPEC; |
| 169 hints.ai_socktype = SOCK_STREAM; | 159 hints.ai_socktype = SOCK_STREAM; |
| 170 hints.ai_protocol = IPPROTO_TCP; | 160 hints.ai_flags = 0; |
| 161 hints.ai_protocol = 0; | |
| 171 struct addrinfo* info = NULL; | 162 struct addrinfo* info = NULL; |
| 172 int status = getaddrinfo(host, 0, &hints, &info); | 163 int status = getaddrinfo(host, 0, &hints, &info); |
| 173 if (status != 0) { | 164 if (status != 0) { |
| 174 ASSERT(*os_error == NULL); | 165 ASSERT(*os_error == NULL); |
| 175 *os_error = new OSError(status, | 166 *os_error = new OSError(status, |
| 176 gai_strerror(status), | 167 gai_strerror(status), |
| 177 OSError::kGetAddressInfo); | 168 OSError::kGetAddressInfo); |
| 178 return NULL; | 169 return NULL; |
| 179 } | 170 } |
| 180 // Convert the address into IPv4 dotted decimal notation. | 171 intptr_t count = 0; |
| 181 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 172 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 182 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 173 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; |
| 183 const char* result = inet_ntop(AF_INET, | |
| 184 reinterpret_cast<void *>(&sockaddr->sin_addr), | |
| 185 buffer, | |
| 186 INET_ADDRSTRLEN); | |
| 187 if (result == NULL) { | |
| 188 free(buffer); | |
| 189 return NULL; | |
| 190 } | 174 } |
| 191 ASSERT(result == buffer); | 175 SocketAddresses* addresses = new SocketAddresses(count); |
| 192 return buffer; | 176 intptr_t i = 0; |
| 177 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { | |
| 178 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { | |
|
Søren Gjesse
2013/04/18 09:07:40
How about moving all this into the SocketAddress c
Anders Johnsen
2013/04/18 12:09:45
Done.
| |
| 179 char* buffer = new char[INET6_ADDRSTRLEN]; | |
| 180 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(c->ai_addr); | |
| 181 const char* as_string = inet_ntop(c->ai_family, | |
| 182 &sockaddr->sin_addr, | |
| 183 buffer, | |
| 184 INET6_ADDRSTRLEN); | |
| 185 if (as_string == NULL) delete[] buffer; | |
| 186 uint8_t* data = new uint8_t[c->ai_addrlen]; | |
| 187 memmove(data, reinterpret_cast<void *>(c->ai_addr), c->ai_addrlen); | |
| 188 SocketAddress::Type type = c->ai_family == AF_INET ? | |
| 189 SocketAddress::IPv4 : | |
| 190 SocketAddress::IPv6; | |
| 191 addresses->addresses[i] = | |
| 192 new SocketAddress(type, as_string, data, c->ai_addrlen); | |
| 193 i++; | |
| 194 } | |
| 195 } | |
| 196 freeaddrinfo(info); | |
| 197 return addresses; | |
| 193 } | 198 } |
| 194 | 199 |
| 195 | 200 |
| 196 intptr_t ServerSocket::CreateBindListen(const char* host, | 201 intptr_t ServerSocket::CreateBindListen(SocketAddress::Type type, |
| 202 uint8_t* host_data, | |
| 203 intptr_t host_len, | |
| 197 intptr_t port, | 204 intptr_t port, |
| 198 intptr_t backlog) { | 205 intptr_t backlog) { |
| 199 intptr_t fd; | 206 intptr_t fd; |
| 200 struct sockaddr_in server_address; | |
| 201 | 207 |
| 202 in_addr_t s_addr = inet_addr(host); | 208 fd = TEMP_FAILURE_RETRY( |
| 203 if (s_addr == INADDR_NONE) { | 209 socket(type == SocketAddress::IPv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0)); |
| 204 return -5; | |
| 205 } | |
| 206 | |
| 207 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | |
| 208 if (fd < 0) return -1; | 210 if (fd < 0) return -1; |
| 209 | 211 |
| 210 FDUtils::SetCloseOnExec(fd); | 212 FDUtils::SetCloseOnExec(fd); |
| 211 | 213 |
| 212 int optval = 1; | 214 int optval = 1; |
| 213 TEMP_FAILURE_RETRY( | 215 TEMP_FAILURE_RETRY( |
| 214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 216 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
| 215 | 217 |
| 216 server_address.sin_family = AF_INET; | 218 if (type == SocketAddress::IPv6) { |
| 217 server_address.sin_port = htons(port); | 219 optval = 0; |
| 218 server_address.sin_addr.s_addr = s_addr; | 220 TEMP_FAILURE_RETRY( |
| 219 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 221 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 222 } | |
| 220 | 223 |
| 224 struct sockaddr_in* in = reinterpret_cast<struct sockaddr_in*>(host_data); | |
| 225 in->sin_port = htons(port); | |
| 221 if (TEMP_FAILURE_RETRY( | 226 if (TEMP_FAILURE_RETRY( |
| 222 bind(fd, | 227 bind(fd, |
| 223 reinterpret_cast<struct sockaddr *>(&server_address), | 228 reinterpret_cast<struct sockaddr*>(host_data), |
| 224 sizeof(server_address))) < 0) { | 229 host_len)) < 0) { |
| 225 TEMP_FAILURE_RETRY(close(fd)); | 230 TEMP_FAILURE_RETRY(close(fd)); |
| 226 return -1; | 231 return -1; |
| 227 } | 232 } |
| 228 | 233 |
| 229 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 234 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
| 230 TEMP_FAILURE_RETRY(close(fd)); | 235 TEMP_FAILURE_RETRY(close(fd)); |
| 231 return -1; | 236 return -1; |
| 232 } | 237 } |
| 233 | 238 |
| 234 Socket::SetNonBlocking(fd); | 239 Socket::SetNonBlocking(fd); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { | 296 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
| 292 int on = enabled ? 1 : 0; | 297 int on = enabled ? 1 : 0; |
| 293 return TEMP_FAILURE_RETRY(setsockopt(fd, | 298 return TEMP_FAILURE_RETRY(setsockopt(fd, |
| 294 SOL_TCP, | 299 SOL_TCP, |
| 295 TCP_NODELAY, | 300 TCP_NODELAY, |
| 296 reinterpret_cast<char *>(&on), | 301 reinterpret_cast<char *>(&on), |
| 297 sizeof(on))) == 0; | 302 sizeof(on))) == 0; |
| 298 } | 303 } |
| 299 | 304 |
| 300 #endif // defined(TARGET_OS_LINUX) | 305 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |