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 #define SOCKADDR_STORAGE_SET_PORT(addr, port) \ |
| 23 if (addr.ss_family == AF_INET) { \ |
| 24 reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port = htons(port); \ |
| 25 } else { \ |
| 26 reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port = htons(port); \ |
| 27 } |
| 28 |
| 29 |
| 30 #define SOCKADDR_STORAGE_GET_PORT(addr) \ |
| 31 addr.ss_family == AF_INET ? \ |
| 32 ntohs(reinterpret_cast<struct sockaddr_in*>(&addr)->sin_port) : \ |
| 33 ntohs(reinterpret_cast<struct sockaddr_in6*>(&addr)->sin6_port) |
| 34 |
| 35 |
| 36 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { |
| 37 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
| 38 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(addrinfo->ai_addr); |
| 39 const char* result = inet_ntop(addrinfo->ai_family, |
| 40 &sockaddr->sin_addr, |
| 41 as_string_, |
| 42 INET6_ADDRSTRLEN); |
| 43 if (result == NULL) as_string_[0] = 0; |
| 44 memmove(reinterpret_cast<void *>(&addr_), |
| 45 addrinfo->ai_addr, |
| 46 addrinfo->ai_addrlen); |
| 47 } |
| 48 |
| 49 |
22 bool Socket::Initialize() { | 50 bool Socket::Initialize() { |
23 // Nothing to do on Linux. | 51 // Nothing to do on Linux. |
24 return true; | 52 return true; |
25 } | 53 } |
26 | 54 |
27 | 55 |
28 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { | 56 intptr_t Socket::CreateConnect(sockaddr_storage addr, const intptr_t port) { |
29 intptr_t fd; | 57 intptr_t fd; |
30 struct hostent server; | |
31 struct sockaddr_in server_address; | |
32 | 58 |
33 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | 59 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
34 if (fd < 0) { | 60 if (fd < 0) { |
35 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); | 61 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno)); |
36 return -1; | 62 return -1; |
37 } | 63 } |
38 | 64 |
39 FDUtils::SetCloseOnExec(fd); | 65 FDUtils::SetCloseOnExec(fd); |
40 Socket::SetNonBlocking(fd); | 66 Socket::SetNonBlocking(fd); |
41 | 67 |
42 static const size_t kTempBufSize = 1024; | 68 SOCKADDR_STORAGE_SET_PORT(addr, port); |
43 char temp_buf[kTempBufSize]; | |
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( | 69 intptr_t result = TEMP_FAILURE_RETRY( |
58 connect(fd, | 70 connect(fd, |
59 reinterpret_cast<struct sockaddr *>(&server_address), | 71 reinterpret_cast<struct sockaddr*>(&addr), |
60 sizeof(server_address))); | 72 SocketAddress::GetAddrLength(addr))); |
61 if (result == 0 || errno == EINPROGRESS) { | 73 if (result == 0 || errno == EINPROGRESS) { |
62 return fd; | 74 return fd; |
63 } | 75 } |
| 76 TEMP_FAILURE_RETRY(close(fd)); |
64 return -1; | 77 return -1; |
65 } | 78 } |
66 | 79 |
67 | 80 |
68 intptr_t Socket::Available(intptr_t fd) { | 81 intptr_t Socket::Available(intptr_t fd) { |
69 return FDUtils::AvailableBytes(fd); | 82 return FDUtils::AvailableBytes(fd); |
70 } | 83 } |
71 | 84 |
72 | 85 |
73 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { | 86 int Socket::Read(intptr_t fd, void* buffer, intptr_t num_bytes) { |
(...skipping 17 matching lines...) Expand all Loading... |
91 // If the would block we need to retry and therefore return 0 as | 104 // If the would block we need to retry and therefore return 0 as |
92 // the number of bytes written. | 105 // the number of bytes written. |
93 written_bytes = 0; | 106 written_bytes = 0; |
94 } | 107 } |
95 return written_bytes; | 108 return written_bytes; |
96 } | 109 } |
97 | 110 |
98 | 111 |
99 intptr_t Socket::GetPort(intptr_t fd) { | 112 intptr_t Socket::GetPort(intptr_t fd) { |
100 ASSERT(fd >= 0); | 113 ASSERT(fd >= 0); |
101 struct sockaddr_in socket_address; | 114 struct sockaddr_storage socket_address; |
102 socklen_t size = sizeof(socket_address); | 115 socklen_t size = sizeof(socket_address); |
103 if (TEMP_FAILURE_RETRY( | 116 if (TEMP_FAILURE_RETRY( |
104 getsockname(fd, | 117 getsockname(fd, |
105 reinterpret_cast<struct sockaddr *>(&socket_address), | 118 reinterpret_cast<struct sockaddr *>(&socket_address), |
106 &size))) { | 119 &size))) { |
107 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); | 120 Log::PrintErr("Error getsockname: %s\n", strerror(errno)); |
108 return 0; | 121 return 0; |
109 } | 122 } |
110 return ntohs(socket_address.sin_port); | 123 return SOCKADDR_STORAGE_GET_PORT(socket_address); |
111 } | 124 } |
112 | 125 |
113 | 126 |
114 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { | 127 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { |
115 ASSERT(fd >= 0); | 128 ASSERT(fd >= 0); |
116 struct sockaddr_in socket_address; | 129 struct sockaddr_storage socket_address; |
117 socklen_t size = sizeof(socket_address); | 130 socklen_t size = sizeof(socket_address); |
118 if (TEMP_FAILURE_RETRY( | 131 if (TEMP_FAILURE_RETRY( |
119 getpeername(fd, | 132 getpeername(fd, |
120 reinterpret_cast<struct sockaddr *>(&socket_address), | 133 reinterpret_cast<struct sockaddr *>(&socket_address), |
121 &size))) { | 134 &size))) { |
122 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); | 135 Log::PrintErr("Error getpeername: %s\n", strerror(errno)); |
123 return false; | 136 return false; |
124 } | 137 } |
125 if (inet_ntop(socket_address.sin_family, | 138 const void* src; |
126 reinterpret_cast<const void *>(&socket_address.sin_addr), | 139 if (socket_address.ss_family == AF_INET6) { |
| 140 const sockaddr_in6* in6 = |
| 141 reinterpret_cast<const sockaddr_in6*>(&socket_address); |
| 142 src = reinterpret_cast<const void*>(&in6->sin6_addr); |
| 143 } else { |
| 144 const sockaddr_in* in = |
| 145 reinterpret_cast<const sockaddr_in*>(&socket_address); |
| 146 src = reinterpret_cast<const void*>(&in->sin_addr); |
| 147 } |
| 148 if (inet_ntop(socket_address.ss_family, |
| 149 src, |
127 host, | 150 host, |
128 INET_ADDRSTRLEN) == NULL) { | 151 INET_ADDRSTRLEN) == NULL) { |
129 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); | 152 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno)); |
130 return false; | 153 return false; |
131 } | 154 } |
132 *port = ntohs(socket_address.sin_port); | 155 *port = SOCKADDR_STORAGE_GET_PORT(socket_address); |
133 return true; | 156 return true; |
134 } | 157 } |
135 | 158 |
136 | 159 |
137 void Socket::GetError(intptr_t fd, OSError* os_error) { | 160 void Socket::GetError(intptr_t fd, OSError* os_error) { |
138 int len = sizeof(errno); | 161 int len = sizeof(errno); |
139 getsockopt(fd, | 162 getsockopt(fd, |
140 SOL_SOCKET, | 163 SOL_SOCKET, |
141 SO_ERROR, | 164 SO_ERROR, |
142 &errno, | 165 &errno, |
(...skipping 11 matching lines...) Expand all Loading... |
154 if (S_ISREG(buf.st_mode)) return File::kFile; | 177 if (S_ISREG(buf.st_mode)) return File::kFile; |
155 return File::kOther; | 178 return File::kOther; |
156 } | 179 } |
157 | 180 |
158 | 181 |
159 intptr_t Socket::GetStdioHandle(int num) { | 182 intptr_t Socket::GetStdioHandle(int num) { |
160 return static_cast<intptr_t>(num); | 183 return static_cast<intptr_t>(num); |
161 } | 184 } |
162 | 185 |
163 | 186 |
164 const char* Socket::LookupIPv4Address(char* host, OSError** os_error) { | 187 SocketAddresses* Socket::LookupAddress(const char* host, |
165 // Perform a name lookup for an IPv4 address. | 188 int type, |
| 189 OSError** os_error) { |
| 190 // Perform a name lookup for a host name. |
166 struct addrinfo hints; | 191 struct addrinfo hints; |
167 memset(&hints, 0, sizeof(hints)); | 192 memset(&hints, 0, sizeof(hints)); |
168 hints.ai_family = AF_INET; | 193 hints.ai_family = SocketAddress::FromType(type); |
169 hints.ai_socktype = SOCK_STREAM; | 194 hints.ai_socktype = SOCK_STREAM; |
| 195 hints.ai_flags = 0; |
170 hints.ai_protocol = IPPROTO_TCP; | 196 hints.ai_protocol = IPPROTO_TCP; |
171 struct addrinfo* info = NULL; | 197 struct addrinfo* info = NULL; |
172 int status = getaddrinfo(host, 0, &hints, &info); | 198 int status = getaddrinfo(host, 0, &hints, &info); |
173 if (status != 0) { | 199 if (status != 0) { |
174 ASSERT(*os_error == NULL); | 200 ASSERT(*os_error == NULL); |
175 *os_error = new OSError(status, | 201 *os_error = new OSError(status, |
176 gai_strerror(status), | 202 gai_strerror(status), |
177 OSError::kGetAddressInfo); | 203 OSError::kGetAddressInfo); |
178 return NULL; | 204 return NULL; |
179 } | 205 } |
180 // Convert the address into IPv4 dotted decimal notation. | 206 intptr_t count = 0; |
181 char* buffer = reinterpret_cast<char*>(malloc(INET_ADDRSTRLEN)); | 207 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
182 sockaddr_in *sockaddr = reinterpret_cast<sockaddr_in *>(info->ai_addr); | 208 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 } | 209 } |
191 ASSERT(result == buffer); | 210 SocketAddresses* addresses = new SocketAddresses(count); |
192 return buffer; | 211 intptr_t i = 0; |
| 212 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { |
| 213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { |
| 214 addresses->SetAt(i, new SocketAddress(c)); |
| 215 i++; |
| 216 } |
| 217 } |
| 218 freeaddrinfo(info); |
| 219 return addresses; |
193 } | 220 } |
194 | 221 |
195 | 222 |
196 intptr_t ServerSocket::CreateBindListen(const char* host, | 223 intptr_t ServerSocket::CreateBindListen(sockaddr_storage addr, |
197 intptr_t port, | 224 intptr_t port, |
198 intptr_t backlog) { | 225 intptr_t backlog) { |
199 intptr_t fd; | 226 intptr_t fd; |
200 struct sockaddr_in server_address; | |
201 | 227 |
202 in_addr_t s_addr = inet_addr(host); | 228 fd = TEMP_FAILURE_RETRY(socket(addr.ss_family, SOCK_STREAM, 0)); |
203 if (s_addr == INADDR_NONE) { | |
204 return -5; | |
205 } | |
206 | |
207 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); | |
208 if (fd < 0) return -1; | 229 if (fd < 0) return -1; |
209 | 230 |
210 FDUtils::SetCloseOnExec(fd); | 231 FDUtils::SetCloseOnExec(fd); |
211 | 232 |
212 int optval = 1; | 233 int optval = 1; |
213 TEMP_FAILURE_RETRY( | 234 TEMP_FAILURE_RETRY( |
214 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); | 235 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); |
215 | 236 |
216 server_address.sin_family = AF_INET; | 237 if (addr.ss_family == AF_INET6) { |
217 server_address.sin_port = htons(port); | 238 optval = 0; |
218 server_address.sin_addr.s_addr = s_addr; | 239 TEMP_FAILURE_RETRY( |
219 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); | 240 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); |
| 241 } |
220 | 242 |
| 243 SOCKADDR_STORAGE_SET_PORT(addr, port); |
221 if (TEMP_FAILURE_RETRY( | 244 if (TEMP_FAILURE_RETRY( |
222 bind(fd, | 245 bind(fd, |
223 reinterpret_cast<struct sockaddr *>(&server_address), | 246 reinterpret_cast<struct sockaddr*>(&addr), |
224 sizeof(server_address))) < 0) { | 247 SocketAddress::GetAddrLength(addr))) < 0) { |
225 TEMP_FAILURE_RETRY(close(fd)); | 248 TEMP_FAILURE_RETRY(close(fd)); |
226 return -1; | 249 return -1; |
227 } | 250 } |
228 | 251 |
229 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { | 252 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
230 TEMP_FAILURE_RETRY(close(fd)); | 253 TEMP_FAILURE_RETRY(close(fd)); |
231 return -1; | 254 return -1; |
232 } | 255 } |
233 | 256 |
234 Socket::SetNonBlocking(fd); | 257 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) { | 314 bool Socket::SetNoDelay(intptr_t fd, bool enabled) { |
292 int on = enabled ? 1 : 0; | 315 int on = enabled ? 1 : 0; |
293 return TEMP_FAILURE_RETRY(setsockopt(fd, | 316 return TEMP_FAILURE_RETRY(setsockopt(fd, |
294 SOL_TCP, | 317 SOL_TCP, |
295 TCP_NODELAY, | 318 TCP_NODELAY, |
296 reinterpret_cast<char *>(&on), | 319 reinterpret_cast<char *>(&on), |
297 sizeof(on))) == 0; | 320 sizeof(on))) == 0; |
298 } | 321 } |
299 | 322 |
300 #endif // defined(TARGET_OS_LINUX) | 323 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |