Chromium Code Reviews| Index: net/base/net_util_posix.cc |
| diff --git a/net/base/net_util_posix.cc b/net/base/net_util_posix.cc |
| index 0412792ebb2e38efcf67e3ad76ad53463508eb3b..13966497c2e2ed293f6785583deb7cfa54ee0075 100644 |
| --- a/net/base/net_util_posix.cc |
| +++ b/net/base/net_util_posix.cc |
| @@ -19,6 +19,9 @@ |
| #if !defined(OS_ANDROID) |
| #include <ifaddrs.h> |
| #endif |
| +//#include <netdb.h> |
|
Sergey Ulanov
2011/08/11 02:06:37
remove commented line.
|
| +#include <net/if.h> |
| +#include <netinet/in.h> |
| namespace net { |
| @@ -68,26 +71,47 @@ bool GetNetworkList(NetworkInterfaceList* networks) { |
| // getifaddrs() may require IO operations. |
| base::ThreadRestrictions::AssertIOAllowed(); |
| - ifaddrs *ifaddr; |
| - if (getifaddrs(&ifaddr) < 0) { |
| + ifaddrs *interfaces; |
| + if (getifaddrs(&interfaces) < 0) { |
| PLOG(ERROR) << "getifaddrs"; |
| return false; |
| } |
| - for (ifaddrs *ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
| - int family = ifa->ifa_addr->sa_family; |
| - if (family == AF_INET || family == AF_INET6) { |
| - IPEndPoint address; |
| - std::string name = ifa->ifa_name; |
| - if (address.FromSockAddr(ifa->ifa_addr, |
| - sizeof(ifa->ifa_addr)) && |
| - name.substr(0, 2) != "lo") { |
| - networks->push_back(NetworkInterface(name, address.address())); |
| - } |
| + // Enumerate the addresses assigned to network interfaces which are up. |
| + // We skip loopback addresses and interfaces. |
| + for (ifaddrs *interface = interfaces; |
| + interface != NULL; |
| + interface = interface->ifa_next) { |
| + if (!(IFF_UP & interface->ifa_flags)) |
| + continue; |
| + if (IFF_LOOPBACK & interface->ifa_flags) |
| + continue; |
| + struct sockaddr* addr = interface->ifa_addr; |
| + if (!addr) |
| + continue; |
| + if (addr->sa_family == AF_INET6) { |
|
Sergey Ulanov
2011/08/11 02:06:37
Add comment saying that we just filter loopback ad
|
| + struct sockaddr_in6* addr_in6 = |
| + reinterpret_cast<struct sockaddr_in6*>(addr); |
| + struct in6_addr* sin6_addr = &addr_in6->sin6_addr; |
| + if (IN6_IS_ADDR_LOOPBACK(sin6_addr)) |
| + continue; |
| + } else if (addr->sa_family == AF_INET) { |
| + struct sockaddr_in* addr_in = |
| + reinterpret_cast<struct sockaddr_in*>(addr); |
| + if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK) |
| + continue; |
| + } else { |
| + continue; |
| + } |
| + IPEndPoint address; |
| + std::string name = interface->ifa_name; |
| + if (address.FromSockAddr(interface->ifa_addr, |
| + sizeof(interface->ifa_addr))) { |
| + networks->push_back(NetworkInterface(name, address.address())); |
| } |
| } |
| - freeifaddrs(ifaddr); |
| + freeifaddrs(interfaces); |
| return true; |
| #endif |