OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <sys/types.h> | 7 #include <sys/types.h> |
8 | 8 |
9 #include "base/eintr_wrapper.h" | 9 #include "base/eintr_wrapper.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/escape.h" | 15 #include "net/base/escape.h" |
16 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
17 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
18 | 18 |
19 #if !defined(OS_ANDROID) | 19 #if !defined(OS_ANDROID) |
20 #include <ifaddrs.h> | 20 #include <ifaddrs.h> |
21 #endif | 21 #endif |
| 22 #include <net/if.h> |
| 23 #include <netinet/in.h> |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 | 26 |
25 bool FileURLToFilePath(const GURL& url, FilePath* path) { | 27 bool FileURLToFilePath(const GURL& url, FilePath* path) { |
26 *path = FilePath(); | 28 *path = FilePath(); |
27 std::string& file_path_str = const_cast<std::string&>(path->value()); | 29 std::string& file_path_str = const_cast<std::string&>(path->value()); |
28 file_path_str.clear(); | 30 file_path_str.clear(); |
29 | 31 |
30 if (!url.is_valid()) | 32 if (!url.is_valid()) |
31 return false; | 33 return false; |
(...skipping 29 matching lines...) Expand all Loading... |
61 // TODO: Android API doesn't support ifaddrs. This method was only used by | 63 // TODO: Android API doesn't support ifaddrs. This method was only used by |
62 // P2PMessage. Consider to implement it until really needed. The possible | 64 // P2PMessage. Consider to implement it until really needed. The possible |
63 // approach is implementing the similar feature by | 65 // approach is implementing the similar feature by |
64 // java.net.NetworkInterface through JNI. | 66 // java.net.NetworkInterface through JNI. |
65 NOTIMPLEMENTED(); | 67 NOTIMPLEMENTED(); |
66 return false; | 68 return false; |
67 #else | 69 #else |
68 // getifaddrs() may require IO operations. | 70 // getifaddrs() may require IO operations. |
69 base::ThreadRestrictions::AssertIOAllowed(); | 71 base::ThreadRestrictions::AssertIOAllowed(); |
70 | 72 |
71 ifaddrs *ifaddr; | 73 ifaddrs *interfaces; |
72 if (getifaddrs(&ifaddr) < 0) { | 74 if (getifaddrs(&interfaces) < 0) { |
73 PLOG(ERROR) << "getifaddrs"; | 75 PLOG(ERROR) << "getifaddrs"; |
74 return false; | 76 return false; |
75 } | 77 } |
76 | 78 |
77 for (ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 79 // Enumerate the addresses assigned to network interfaces which are up. |
78 if (!ifa->ifa_addr) | 80 for (ifaddrs *interface = interfaces; |
79 continue; // Interface has no IP addresses, so skip it. | 81 interface != NULL; |
80 int family = ifa->ifa_addr->sa_family; | 82 interface = interface->ifa_next) { |
81 if (family == AF_INET || family == AF_INET6) { | 83 // Skip loopback interfaces, and ones which are down. |
82 IPEndPoint address; | 84 if (!(IFF_UP & interface->ifa_flags)) |
83 std::string name = ifa->ifa_name; | 85 continue; |
84 if (address.FromSockAddr(ifa->ifa_addr, | 86 if (IFF_LOOPBACK & interface->ifa_flags) |
85 sizeof(ifa->ifa_addr)) && | 87 continue; |
86 name.substr(0, 2) != "lo") { | 88 // Skip interfaces with no address configured. |
87 networks->push_back(NetworkInterface(name, address.address())); | 89 struct sockaddr* addr = interface->ifa_addr; |
88 } | 90 if (!addr) |
| 91 continue; |
| 92 // Skip loopback addresses configured on non-loopback interfaces. |
| 93 int addr_size = 0; |
| 94 if (addr->sa_family == AF_INET6) { |
| 95 struct sockaddr_in6* addr_in6 = |
| 96 reinterpret_cast<struct sockaddr_in6*>(addr); |
| 97 struct in6_addr* sin6_addr = &addr_in6->sin6_addr; |
| 98 addr_size = sizeof(*addr_in6); |
| 99 if (IN6_IS_ADDR_LOOPBACK(sin6_addr)) |
| 100 continue; |
| 101 } else if (addr->sa_family == AF_INET) { |
| 102 struct sockaddr_in* addr_in = |
| 103 reinterpret_cast<struct sockaddr_in*>(addr); |
| 104 addr_size = sizeof(*addr_in); |
| 105 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK) |
| 106 continue; |
| 107 } else { |
| 108 // Skip non-IP addresses. |
| 109 continue; |
| 110 } |
| 111 IPEndPoint address; |
| 112 std::string name = interface->ifa_name; |
| 113 if (address.FromSockAddr(addr, addr_size)) { |
| 114 networks->push_back(NetworkInterface(name, address.address())); |
89 } | 115 } |
90 } | 116 } |
91 | 117 |
92 freeifaddrs(ifaddr); | 118 freeifaddrs(interfaces); |
93 | 119 |
94 return true; | 120 return true; |
95 #endif | 121 #endif |
96 } | 122 } |
97 | 123 |
98 } // namespace net | 124 } // namespace net |
OLD | NEW |