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

Side by Side Diff: net/base/net_util_posix.cc

Issue 7612012: Cleanup GetNetworkList. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 9 years, 4 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 if (addr->sa_family == AF_INET6) {
94 struct sockaddr_in6* addr_in6 =
95 reinterpret_cast<struct sockaddr_in6*>(addr);
96 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
97 if (IN6_IS_ADDR_LOOPBACK(sin6_addr))
98 continue;
99 } else if (addr->sa_family == AF_INET) {
100 struct sockaddr_in* addr_in =
101 reinterpret_cast<struct sockaddr_in*>(addr);
102 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK)
103 continue;
104 } else {
105 // Skip non-IP addresses.
106 continue;
107 }
108 IPEndPoint address;
109 std::string name = interface->ifa_name;
110 if (address.FromSockAddr(interface->ifa_addr,
111 sizeof(interface->ifa_addr))) {
wtc 2011/08/15 22:38:31 Change interface->ifa_addr (two occurrences) to yo
112 networks->push_back(NetworkInterface(name, address.address()));
89 } 113 }
90 } 114 }
91 115
92 freeifaddrs(ifaddr); 116 freeifaddrs(interfaces);
93 117
94 return true; 118 return true;
95 #endif 119 #endif
96 } 120 }
97 121
98 } // namespace net 122 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698