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

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: Clean up implementation and add tests. 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
16 #include <net/if.h> // NOLINT
15 17
16 #include "bin/fdutils.h" 18 #include "bin/fdutils.h"
17 #include "bin/file.h" 19 #include "bin/file.h"
18 #include "bin/log.h" 20 #include "bin/log.h"
19 #include "bin/socket.h" 21 #include "bin/socket.h"
20 22
21 23
22 namespace dart { 24 namespace dart {
23 namespace bin { 25 namespace bin {
24 26
25 SocketAddress::SocketAddress(struct addrinfo* addrinfo) { 27 SocketAddress::SocketAddress(struct sockaddr* sockaddr) {
26 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); 28 ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN);
27 RawAddr* raw = reinterpret_cast<RawAddr*>(addrinfo->ai_addr); 29 RawAddr* raw = reinterpret_cast<RawAddr*>(sockaddr);
28 const char* result = inet_ntop(addrinfo->ai_family, 30 const char* result = inet_ntop(sockaddr->sa_family,
29 &raw->in.sin_addr, 31 &raw->in.sin_addr,
30 as_string_, 32 as_string_,
31 INET6_ADDRSTRLEN); 33 INET6_ADDRSTRLEN);
32 if (result == NULL) as_string_[0] = 0; 34 if (result == NULL) as_string_[0] = 0;
33 memmove(reinterpret_cast<void *>(&addr_), 35 memmove(reinterpret_cast<void *>(&addr_),
34 addrinfo->ai_addr, 36 sockaddr,
35 addrinfo->ai_addrlen); 37 GetAddrLength(*raw));
36 } 38 }
37 39
38 40
39 bool Socket::Initialize() { 41 bool Socket::Initialize() {
40 // Nothing to do on Linux. 42 // Nothing to do on Linux.
41 return true; 43 return true;
42 } 44 }
43 45
44 46
45 intptr_t Socket::Create(RawAddr addr) { 47 intptr_t Socket::Create(RawAddr addr) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 if (S_ISREG(buf.st_mode)) return File::kFile; 179 if (S_ISREG(buf.st_mode)) return File::kFile;
178 return File::kOther; 180 return File::kOther;
179 } 181 }
180 182
181 183
182 intptr_t Socket::GetStdioHandle(int num) { 184 intptr_t Socket::GetStdioHandle(int num) {
183 return static_cast<intptr_t>(num); 185 return static_cast<intptr_t>(num);
184 } 186 }
185 187
186 188
187 SocketAddresses* Socket::LookupAddress(const char* host, 189 AddressList<SocketAddress>* Socket::LookupAddress(const char* host,
188 int type, 190 int type,
189 OSError** os_error) { 191 OSError** os_error) {
190 // Perform a name lookup for a host name. 192 // Perform a name lookup for a host name.
191 struct addrinfo hints; 193 struct addrinfo hints;
192 memset(&hints, 0, sizeof(hints)); 194 memset(&hints, 0, sizeof(hints));
193 hints.ai_family = SocketAddress::FromType(type); 195 hints.ai_family = SocketAddress::FromType(type);
194 hints.ai_socktype = SOCK_STREAM; 196 hints.ai_socktype = SOCK_STREAM;
195 hints.ai_flags = 0; 197 hints.ai_flags = 0;
196 hints.ai_protocol = IPPROTO_TCP; 198 hints.ai_protocol = IPPROTO_TCP;
197 struct addrinfo* info = NULL; 199 struct addrinfo* info = NULL;
198 int status = getaddrinfo(host, 0, &hints, &info); 200 int status = getaddrinfo(host, 0, &hints, &info);
199 if (status != 0) { 201 if (status != 0) {
200 ASSERT(*os_error == NULL); 202 ASSERT(*os_error == NULL);
201 *os_error = new OSError(status, 203 *os_error = new OSError(status,
202 gai_strerror(status), 204 gai_strerror(status),
203 OSError::kGetAddressInfo); 205 OSError::kGetAddressInfo);
204 return NULL; 206 return NULL;
205 } 207 }
206 intptr_t count = 0; 208 intptr_t count = 0;
207 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 209 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
208 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++; 210 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) count++;
209 } 211 }
210 SocketAddresses* addresses = new SocketAddresses(count); 212 int i = 0;
211 intptr_t i = 0; 213 AddressList<SocketAddress>* addresses = new AddressList<SocketAddress>(count);
212 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) { 214 for (struct addrinfo* c = info; c != NULL; c = c->ai_next) {
213 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) { 215 if (c->ai_family == AF_INET || c->ai_family == AF_INET6) {
214 addresses->SetAt(i, new SocketAddress(c)); 216 addresses->SetAt(i, new SocketAddress(c->ai_addr));
215 i++; 217 i++;
216 } 218 }
217 } 219 }
218 freeaddrinfo(info); 220 freeaddrinfo(info);
219 return addresses; 221 return addresses;
220 } 222 }
221 223
222 224
225 AddressList<InterfaceSocketAddress>* Socket::ListInterfaces(
226 int type,
227 OSError** os_error) {
228 struct ifaddrs* ifaddr;
229
230 int status = getifaddrs(&ifaddr);
231 if (status != 0) {
232 ASSERT(*os_error == NULL);
233 *os_error = new OSError(status,
234 gai_strerror(status),
235 OSError::kGetAddressInfo);
236 return NULL;
237 }
238
239 int lookup_family = SocketAddress::FromType(type);
240
241 intptr_t count = 0;
242 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
243 int family = ifa->ifa_addr->sa_family;
244 if ((lookup_family == family ||
245 (lookup_family == AF_UNSPEC &&
246 (family == AF_INET || family == AF_INET6)))) {
Søren Gjesse 2013/06/20 14:38:17 Consider adding a static function static bool XXX
Anders Johnsen 2013/06/20 15:00:25 Done.
247 count++;
248 }
249 }
250
251 AddressList<InterfaceSocketAddress>* addresses =
252 new AddressList<InterfaceSocketAddress>(count);
253 int i = 0;
254 for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
255 int family = ifa->ifa_addr->sa_family;
256 if ((lookup_family == family ||
257 (lookup_family == AF_UNSPEC &&
258 (family == AF_INET || family == AF_INET6)))) {
259 addresses->SetAt(i, new InterfaceSocketAddress(
260 ifa->ifa_addr, strdup(ifa->ifa_name)));
261 i++;
262 }
263 }
264 freeifaddrs(ifaddr);
265 return addresses;
266 }
267
268
223 intptr_t ServerSocket::CreateBindListen(RawAddr addr, 269 intptr_t ServerSocket::CreateBindListen(RawAddr addr,
224 intptr_t port, 270 intptr_t port,
225 intptr_t backlog, 271 intptr_t backlog,
226 bool v6_only) { 272 bool v6_only) {
227 intptr_t fd; 273 intptr_t fd;
228 274
229 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0)); 275 fd = TEMP_FAILURE_RETRY(socket(addr.ss.ss_family, SOCK_STREAM, 0));
230 if (fd < 0) return -1; 276 if (fd < 0) return -1;
231 277
232 FDUtils::SetCloseOnExec(fd); 278 FDUtils::SetCloseOnExec(fd);
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 SOL_TCP, 364 SOL_TCP,
319 TCP_NODELAY, 365 TCP_NODELAY,
320 reinterpret_cast<char *>(&on), 366 reinterpret_cast<char *>(&on),
321 sizeof(on))) == 0; 367 sizeof(on))) == 0;
322 } 368 }
323 369
324 } // namespace bin 370 } // namespace bin
325 } // namespace dart 371 } // namespace dart
326 372
327 #endif // defined(TARGET_OS_LINUX) 373 #endif // defined(TARGET_OS_LINUX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698