Chromecast Android buildfix: rework CommandLine initialization logic.
[chromium-blink-merge.git] / net / base / net_util_posix.cc
blob73704cde69de5b5697acf1a5d621a5d880d5c1da
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
3 // found in the LICENSE file.
5 #include "net/base/net_util.h"
7 #include <set>
8 #include <sys/types.h>
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_tokenizer.h"
15 #include "base/strings/string_util.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "net/base/escape.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/net_errors.h"
20 #include "url/gurl.h"
22 #if !defined(OS_ANDROID) && !defined(OS_NACL)
23 #include <ifaddrs.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
26 #endif
28 #if defined(OS_MACOSX) && !defined(OS_IOS)
29 #include <net/if_media.h>
30 #include <netinet/in_var.h>
31 #include <sys/ioctl.h>
32 #endif
34 #if defined(OS_ANDROID)
35 #include "net/android/network_library.h"
36 #endif
38 namespace net {
40 namespace {
42 #if !defined(OS_ANDROID)
44 struct NetworkInterfaceInfo {
45 NetworkInterfaceInfo() : permanent(true) { }
47 bool permanent; // IPv6 has notion of temporary address. If the address is
48 // IPv6 and it's temporary this field will be false.
49 NetworkInterface interface;
52 // This method will remove permanent IPv6 addresses if a temporary address
53 // is available for same network interface.
54 void RemovePermanentIPv6AddressesWhereTemporaryExists(
55 std::vector<NetworkInterfaceInfo>* infos) {
56 if (!infos || infos->empty())
57 return;
59 // Build a set containing the names of interfaces with a temp IPv6 address
60 std::set<std::string> ifaces_with_temp_addrs;
61 std::vector<NetworkInterfaceInfo>::iterator i;
62 for (i = infos->begin(); i != infos->end(); ++i) {
63 if (!i->permanent && i->interface.address.size() == kIPv6AddressSize) {
64 ifaces_with_temp_addrs.insert(i->interface.name);
68 // If there are no such interfaces then there's no further work.
69 if (ifaces_with_temp_addrs.empty())
70 return;
72 // Search for permenent addresses belonging to same network interface.
73 for (i = infos->begin(); i != infos->end(); ) {
74 // If the address is IPv6 and it's permanent and there is temporary
75 // address for it, then we can remove this address.
76 if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent &&
77 (ifaces_with_temp_addrs.find(i->interface.name) !=
78 ifaces_with_temp_addrs.end())) {
79 i = infos->erase(i);
80 } else {
81 ++i;
86 #endif
88 #if defined(OS_MACOSX) && !defined(OS_IOS)
90 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
91 int addr_family, const std::string& interface_name) {
92 NetworkChangeNotifier::ConnectionType type =
93 NetworkChangeNotifier::CONNECTION_UNKNOWN;
95 struct ifmediareq ifmr = {};
96 strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1);
98 int s = socket(addr_family, SOCK_DGRAM, 0);
99 if (s == -1) {
100 return type;
103 if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) {
104 if (ifmr.ifm_current & IFM_IEEE80211) {
105 type = NetworkChangeNotifier::CONNECTION_WIFI;
106 } else if (ifmr.ifm_current & IFM_ETHER) {
107 type = NetworkChangeNotifier::CONNECTION_ETHERNET;
110 close(s);
111 return type;
114 #endif
116 } // namespace
118 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
119 #if defined(OS_NACL)
120 NOTIMPLEMENTED();
121 return false;
122 #elif defined(OS_ANDROID)
123 std::string network_list = android::GetNetworkList();
124 base::StringTokenizer network_interfaces(network_list, "\n");
125 while (network_interfaces.GetNext()) {
126 std::string network_item = network_interfaces.token();
127 base::StringTokenizer network_tokenizer(network_item, "\t");
128 CHECK(network_tokenizer.GetNext());
129 std::string name = network_tokenizer.token();
131 CHECK(network_tokenizer.GetNext());
132 std::string interface_address = network_tokenizer.token();
133 IPAddressNumber address;
134 size_t network_prefix = 0;
135 CHECK(ParseCIDRBlock(network_tokenizer.token(),
136 &address,
137 &network_prefix));
139 CHECK(network_tokenizer.GetNext());
140 uint32 index = 0;
141 CHECK(base::StringToUint(network_tokenizer.token(), &index));
143 networks->push_back(
144 NetworkInterface(name,
145 name,
146 index,
147 NetworkChangeNotifier::CONNECTION_UNKNOWN,
148 address,
149 network_prefix,
150 IP_ADDRESS_ATTRIBUTE_NONE));
152 return true;
153 #else
154 // getifaddrs() may require IO operations.
155 base::ThreadRestrictions::AssertIOAllowed();
157 #if defined(OS_MACOSX) && !defined(OS_IOS)
158 int ioctl_socket = -1;
159 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
160 // we need a socket to query information about temporary address.
161 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0);
162 DCHECK_GT(ioctl_socket, 0);
164 #endif
166 ifaddrs *interfaces;
167 if (getifaddrs(&interfaces) < 0) {
168 PLOG(ERROR) << "getifaddrs";
169 return false;
172 std::vector<NetworkInterfaceInfo> network_infos;
174 // Enumerate the addresses assigned to network interfaces which are up.
175 for (ifaddrs *interface = interfaces;
176 interface != NULL;
177 interface = interface->ifa_next) {
178 // Skip loopback interfaces, and ones which are down.
179 if (!(IFF_UP & interface->ifa_flags))
180 continue;
181 if (IFF_LOOPBACK & interface->ifa_flags)
182 continue;
183 // Skip interfaces with no address configured.
184 struct sockaddr* addr = interface->ifa_addr;
185 if (!addr)
186 continue;
188 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
189 // configured on non-loopback interfaces.
190 int addr_size = 0;
191 if (addr->sa_family == AF_INET6) {
192 struct sockaddr_in6* addr_in6 =
193 reinterpret_cast<struct sockaddr_in6*>(addr);
194 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
195 addr_size = sizeof(*addr_in6);
196 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) ||
197 IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
198 continue;
200 } else if (addr->sa_family == AF_INET) {
201 struct sockaddr_in* addr_in =
202 reinterpret_cast<struct sockaddr_in*>(addr);
203 addr_size = sizeof(*addr_in);
204 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
205 addr_in->sin_addr.s_addr == 0) {
206 continue;
208 } else {
209 // Skip non-IP addresses.
210 continue;
213 const std::string& name = interface->ifa_name;
214 // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
215 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) &&
216 ((name.find("vmnet") != std::string::npos) ||
217 (name.find("vnic") != std::string::npos))) {
218 continue;
221 NetworkInterfaceInfo network_info;
222 NetworkChangeNotifier::ConnectionType connection_type =
223 NetworkChangeNotifier::CONNECTION_UNKNOWN;
224 #if defined(OS_MACOSX) && !defined(OS_IOS)
225 // Check if this is a temporary address. Currently this is only supported
226 // on Mac.
227 if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) &&
228 ioctl_socket >= 0 && addr->sa_family == AF_INET6) {
229 struct in6_ifreq ifr = {};
230 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1);
231 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr,
232 interface->ifa_addr->sa_len);
233 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr);
234 if (rv >= 0) {
235 network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY);
239 connection_type = GetNetworkInterfaceType(addr->sa_family, name);
240 #endif
242 IPEndPoint address;
243 if (address.FromSockAddr(addr, addr_size)) {
244 uint8 net_mask = 0;
245 if (interface->ifa_netmask) {
246 // If not otherwise set, assume the same sa_family as ifa_addr.
247 if (interface->ifa_netmask->sa_family == 0) {
248 interface->ifa_netmask->sa_family = addr->sa_family;
250 IPEndPoint netmask;
251 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
252 net_mask = MaskPrefixLength(netmask.address());
255 network_info.interface = NetworkInterface(name,
256 name,
257 if_nametoindex(name.c_str()),
258 connection_type,
259 address.address(),
260 net_mask,
261 IP_ADDRESS_ATTRIBUTE_NONE);
263 network_infos.push_back(NetworkInterfaceInfo(network_info));
266 freeifaddrs(interfaces);
267 #if defined(OS_MACOSX) && !defined(OS_IOS)
268 if (ioctl_socket >= 0) {
269 close(ioctl_socket);
271 #endif
273 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
274 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos);
277 for (size_t i = 0; i < network_infos.size(); ++i) {
278 networks->push_back(network_infos[i].interface);
280 return true;
281 #endif
284 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
285 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
288 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) {
289 return scoped_ptr<ScopedWifiOptions>();
293 } // namespace net