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

Side by Side Diff: runtime/bin/socket_linux.cc

Issue 16514010: Add NetworkAdaptor to dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add support for win, mac and android. Created 7 years, 6 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include <ifaddrs.h> // NOLINT
15 16
16 #include "bin/fdutils.h" 17 #include "bin/fdutils.h"
17 #include "bin/file.h" 18 #include "bin/file.h"
18 #include "bin/log.h" 19 #include "bin/log.h"
19 #include "bin/socket.h" 20 #include "bin/socket.h"
20 21
21 22
22 namespace dart { 23 namespace dart {
23 namespace bin { 24 namespace bin {
24 25
25 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { 26 SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 27 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
27 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); 28 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr);
28 const char* result = inet_ntop(addrinfo->ai_family, 29 const char* result = inet_ntop(sockaddr->sa_family,
29 &raw->in.sin_addr, 30 &raw->in.sin_addr,
30 as_string_, 31 as_string_,
31 INET6_ADDRSTRLEN); 32 INET6_ADDRSTRLEN);
32 if (result == NULL) as_string_[0] = 0; 33 if (result == NULL) as_string_[0] = 0;
33 memmove(reinterpret_cast<void *>(&addr_), 34 memmove(reinterpret_cast<void *>(&addr_),
34 addrinfo->ai_addr, 35 sockaddr,
35 addrinfo->ai_addrlen); 36 GetAddrLength(raw));
36 } 37 }
37 38
38 39
39 bool Socket::Initialize() { 40 bool Socket::Initialize() {
40 // Nothing to do on Linux. 41 // Nothing to do on Linux.
41 return true; 42 return true;
42 } 43 }
43 44
44 45
45 intptr_t Socket::Create(RawAddr addr) { 46 intptr_t Socket::Create(RawAddr addr) {
46 intptr_t fd; 47 intptr_t fd;
47 48
48 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 49 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
49 if (fd < 0) { 50 if (fd < 0) {
50 Log::PrintErr("Error Create: %s\n", strerror(errno)); 51 Log::PrintErr("Error Create: %s\n", strerror(errno));
51 return -1; 52 return -1;
52 } 53 }
53 54
54 FDUtils::SetCloseOnExec(fd); 55 FDUtils::SetCloseOnExec(fd);
55 return fd; 56 return fd;
56 } 57 }
57 58
58 59
59 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) { 60 intptr_t Socket::Connect(intptr_t fd, RawAddr addr, const intptr_t port) {
60 SocketAddress::SetAddrPort(&addr, port); 61 SocketAddress::SetAddrPort(&addr, port);
61 intptr_t result = TEMP_FAILURE_RETRY( 62 intptr_t result = TEMP_FAILURE_RETRY(
62 connect(fd, 63 connect(fd,
63 &addr.addr, 64 &addr.addr,
64 SocketAddress::GetAddrLength(addr))); 65 SocketAddress::GetAddrLength(&addr)));
65 if (result == 0 || errno == EINPROGRESS) { 66 if (result == 0 || errno == EINPROGRESS) {
66 return fd; 67 return fd;
67 } 68 }
68 VOID_TEMP_FAILURE_RETRY(close(fd)); 69 VOID_TEMP_FAILURE_RETRY(close(fd));
69 return -1; 70 return -1;
70 } 71 }
71 72
72 73
73 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) { 74 intptr_t Socket::CreateConnect(RawAddr addr, const intptr_t port) {
74 intptr_t fd = Socket::Create(addr); 75 intptr_t fd = Socket::Create(addr);
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (S_ISREG(buf.st_mode)) return File::kFile; 178 if (S_ISREG(buf.st_mode)) return File::kFile;
178 return File::kOther; 179 return File::kOther;
179 } 180 }
180 181
181 182
182 intptr_t Socket::GetStdioHandle(int num) { 183 intptr_t Socket::GetStdioHandle(int num) {
183 return static_cast<intptr_t>(num); 184 return static_cast<intptr_t>(num);
184 } 185 }
185 186
186 187
187 SocketAddresses* Socket::LookupAddress(const char* host, 188 AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
188 int type, 189 int type,
189 OSError** os_error) { 190 OSError** os_error) {
190 // Perform a name lookup for a host name. 191 // Perform a name lookup for a host name.
191 struct addrinfo hints; 192 struct addrinfo hints;
192 memset(&hints, 0, sizeof(hints)); 193 memset(&hints, 0, sizeof(hints));
193 hints.ai_family = SocketAddress::FromType(type); 194 hints.ai_family = SocketAddress::FromType(type);
194 hints.ai_socktype = SOCK_STREAM; 195 hints.ai_socktype = SOCK_STREAM;
195 hints.ai_flags = 0; 196 hints.ai_flags = 0;
196 hints.ai_protocol = IPPROTO_TCP; 197 hints.ai_protocol = IPPROTO_TCP;
197 struct addrinfo* info = NULL; 198 struct addrinfo* info = NULL;
198 int status = getaddrinfo(host, 0, &hints, &info); 199 int status = getaddrinfo(host, 0, &hints, &info);
199 if (status != 0) { 200 if (status != 0) {
200 ASSERT(*os_error == NULL); 201 ASSERT(*os_error == NULL);
201 *os_error = new OSError(status, 202 *os_error = new OSError(status,
202 gai_strerror(status), 203 gai_strerror(status),
203 OSError::kGetAddressInfo); 204 OSError::kGetAddressInfo);
204 return NULL; 205 return NULL;
205 } 206 }
206 intptr_t count = 0; 207 intptr_t count = 0;
207 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 208 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
208 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; 209 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++;
209 } 210 }
210 SocketAddresses* addresses = new SocketAddresses(count); 211 int i = 0;
211 intptr_t i = 0; 212 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count);
212 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 213 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { 214 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) {
214 addresses->SetAt(i, new SocketAddress(c)); 215 addresses->SetAt(i, new SocketAddress(c->ai_addr));
215 i++; 216 i++;
216 } 217 }
217 } 218 }
218 freeaddrinfo(info); 219 freeaddrinfo(info);
219 return addresses; 220 return addresses;
220 } 221 }
221 222
222 223
224 static bool ShouldIncludeIfaAddrs(struct ifaddrs* ifa, int lookup_family) {
225 int family = ifa->ifa_addr->sa_family;
226 if (lookup_family == family) return true;
227 if (lookup_family == AF_UNSPEC &&
228 (family == AF_INET || family == AF_INET6)) {
229 return true;
230 }
231 return false;
232 }
233
234
235 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
236 int type,
237 OSError** os_error) {
238 struct ifaddrs* ifaddr;
239
240 int status = getifaddrs(&ifaddr);
241 if (status != 0) {
242 ASSERT(*os_error == NULL);
243 *os_error = new OSError(status,
244 gai_strerror(status),
245 OSError::kGetAddressInfo);
246 return NULL;
247 }
248
249 int lookup_family = SocketAddress::FromType(type);
250
251 intptr_t count = 0;
252 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
253 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) count++;
254 }
255
256 AddressList<InterfaceSocketAddress>* addresses =
257 new AddressList<InterfaceSocketAddress>(count);
258 int i = 0;
259 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
260 if (ShouldIncludeIfaAddrs(ifa, lookup_family)) {
261 addresses->SetAt(i, new InterfaceSocketAddress(
262 ifa->ifa_addr, strdup(ifa->ifa_name)));
263 i++;
264 }
265 }
266 freeifaddrs(ifaddr);
267 return addresses;
268 }
269
270
223 intptr_t ServerSocket::CreateBindListen(RawAddr addr, 271 intptr_t ServerSocket::CreateBindListen(RawAddr addr,
224 intptr_t port, 272 intptr_t port,
225 intptr_t backlog, 273 intptr_t backlog,
226 bool v6_only) { 274 bool v6_only) {
227 intptr_t fd; 275 intptr_t fd;
228 276
229 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 277 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
230 if (fd < 0) return -1; 278 if (fd < 0) return -1;
231 279
232 FDUtils::SetCloseOnExec(fd); 280 FDUtils::SetCloseOnExec(fd);
233 281
234 int optval = 1; 282 int optval = 1;
235 TEMP_FAILURE_RETRY( 283 TEMP_FAILURE_RETRY(
236 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 284 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
237 285
238 if (addr.ss.ss_family == AF_INET6) { 286 if (addr.ss.ss_family == AF_INET6) {
239 optval = v6_only ? 1 : 0; 287 optval = v6_only ? 1 : 0;
240 TEMP_FAILURE_RETRY( 288 TEMP_FAILURE_RETRY(
241 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval))); 289 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof(optval)));
242 } 290 }
243 291
244 SocketAddress::SetAddrPort(&addr, port); 292 SocketAddress::SetAddrPort(&addr, port);
245 if (TEMP_FAILURE_RETRY( 293 if (TEMP_FAILURE_RETRY(
246 bind(fd, 294 bind(fd,
247 &addr.addr, 295 &addr.addr,
248 SocketAddress::GetAddrLength(addr))) < 0) { 296 SocketAddress::GetAddrLength(&addr))) < 0) {
249 TEMP_FAILURE_RETRY(close(fd)); 297 TEMP_FAILURE_RETRY(close(fd));
250 return -1; 298 return -1;
251 } 299 }
252 300
253 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { 301 if (TEMP_FAILURE_RETRY(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) {
254 TEMP_FAILURE_RETRY(close(fd)); 302 TEMP_FAILURE_RETRY(close(fd));
255 return -1; 303 return -1;
256 } 304 }
257 305
258 Socket::SetNonBlocking(fd); 306 Socket::SetNonBlocking(fd);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 SOL_TCP, 366 SOL_TCP,
319 TCP_NODELAY, 367 TCP_NODELAY,
320 reinterpret_cast<char *>(&on), 368 reinterpret_cast<char *>(&on),
321 sizeof(on))) == 0; 369 sizeof(on))) == 0;
322 } 370 }
323 371
324 } // namespace bin 372 } // namespace bin
325 } // namespace dart 373 } // namespace dart
326 374
327 #endif // defined(TARGET_OS_LINUX) 375 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698