Chromium Code Reviews| 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 <netdb.h> | |
|
Sergey Ulanov
2011/08/11 02:06:37
remove commented line.
| |
| 23 #include <net/if.h> | |
| 24 #include <netinet/in.h> | |
| 22 | 25 |
| 23 namespace net { | 26 namespace net { |
| 24 | 27 |
| 25 bool FileURLToFilePath(const GURL& url, FilePath* path) { | 28 bool FileURLToFilePath(const GURL& url, FilePath* path) { |
| 26 *path = FilePath(); | 29 *path = FilePath(); |
| 27 std::string& file_path_str = const_cast<std::string&>(path->value()); | 30 std::string& file_path_str = const_cast<std::string&>(path->value()); |
| 28 file_path_str.clear(); | 31 file_path_str.clear(); |
| 29 | 32 |
| 30 if (!url.is_valid()) | 33 if (!url.is_valid()) |
| 31 return false; | 34 return false; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 61 // TODO: Android API doesn't support ifaddrs. This method was only used by | 64 // TODO: Android API doesn't support ifaddrs. This method was only used by |
| 62 // P2PMessage. Consider to implement it until really needed. The possible | 65 // P2PMessage. Consider to implement it until really needed. The possible |
| 63 // approach is implementing the similar feature by | 66 // approach is implementing the similar feature by |
| 64 // java.net.NetworkInterface through JNI. | 67 // java.net.NetworkInterface through JNI. |
| 65 NOTIMPLEMENTED(); | 68 NOTIMPLEMENTED(); |
| 66 return false; | 69 return false; |
| 67 #else | 70 #else |
| 68 // getifaddrs() may require IO operations. | 71 // getifaddrs() may require IO operations. |
| 69 base::ThreadRestrictions::AssertIOAllowed(); | 72 base::ThreadRestrictions::AssertIOAllowed(); |
| 70 | 73 |
| 71 ifaddrs *ifaddr; | 74 ifaddrs *interfaces; |
| 72 if (getifaddrs(&ifaddr) < 0) { | 75 if (getifaddrs(&interfaces) < 0) { |
| 73 PLOG(ERROR) << "getifaddrs"; | 76 PLOG(ERROR) << "getifaddrs"; |
| 74 return false; | 77 return false; |
| 75 } | 78 } |
| 76 | 79 |
| 77 for (ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { | 80 // Enumerate the addresses assigned to network interfaces which are up. |
| 78 int family = ifa->ifa_addr->sa_family; | 81 // We skip loopback addresses and interfaces. |
| 79 if (family == AF_INET || family == AF_INET6) { | 82 for (ifaddrs *interface = interfaces; |
| 80 IPEndPoint address; | 83 interface != NULL; |
| 81 std::string name = ifa->ifa_name; | 84 interface = interface->ifa_next) { |
| 82 if (address.FromSockAddr(ifa->ifa_addr, | 85 if (!(IFF_UP & interface->ifa_flags)) |
| 83 sizeof(ifa->ifa_addr)) && | 86 continue; |
| 84 name.substr(0, 2) != "lo") { | 87 if (IFF_LOOPBACK & interface->ifa_flags) |
| 85 networks->push_back(NetworkInterface(name, address.address())); | 88 continue; |
| 86 } | 89 struct sockaddr* addr = interface->ifa_addr; |
| 90 if (!addr) | |
| 91 continue; | |
| 92 if (addr->sa_family == AF_INET6) { | |
|
Sergey Ulanov
2011/08/11 02:06:37
Add comment saying that we just filter loopback ad
| |
| 93 struct sockaddr_in6* addr_in6 = | |
| 94 reinterpret_cast<struct sockaddr_in6*>(addr); | |
| 95 struct in6_addr* sin6_addr = &addr_in6->sin6_addr; | |
| 96 if (IN6_IS_ADDR_LOOPBACK(sin6_addr)) | |
| 97 continue; | |
| 98 } else if (addr->sa_family == AF_INET) { | |
| 99 struct sockaddr_in* addr_in = | |
| 100 reinterpret_cast<struct sockaddr_in*>(addr); | |
| 101 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK) | |
| 102 continue; | |
| 103 } else { | |
| 104 continue; | |
| 105 } | |
| 106 IPEndPoint address; | |
| 107 std::string name = interface->ifa_name; | |
| 108 if (address.FromSockAddr(interface->ifa_addr, | |
| 109 sizeof(interface->ifa_addr))) { | |
| 110 networks->push_back(NetworkInterface(name, address.address())); | |
| 87 } | 111 } |
| 88 } | 112 } |
| 89 | 113 |
| 90 freeifaddrs(ifaddr); | 114 freeifaddrs(interfaces); |
| 91 | 115 |
| 92 return true; | 116 return true; |
| 93 #endif | 117 #endif |
| 94 } | 118 } |
| 95 | 119 |
| 96 } // namespace net | 120 } // namespace net |
| OLD | NEW |