ServiceWorker: Rename 'current' to 'controller'
[chromium-blink-merge.git] / net / base / net_util_posix.cc
blobfbc3fcc8f6eac39f64150c69a3cec11d8eb43f51
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/strings/string_number_conversions.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "base/strings/string_util.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "net/base/escape.h"
17 #include "net/base/ip_endpoint.h"
18 #include "net/base/net_errors.h"
19 #include "url/gurl.h"
21 #if !defined(OS_ANDROID) && !defined(OS_NACL)
22 #include <ifaddrs.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #endif
27 #if defined(OS_MACOSX) && !defined(OS_IOS)
28 #include <net/if_media.h>
29 #include <netinet/in_var.h>
30 #include <sys/ioctl.h>
31 #endif
33 #if defined(OS_ANDROID)
34 #include "net/android/network_library.h"
35 #endif
37 namespace net {
39 namespace {
41 #if !defined(OS_ANDROID)
43 struct NetworkInterfaceInfo {
44 NetworkInterfaceInfo() : permanent(true) { }
46 bool permanent; // IPv6 has notion of temporary address. If the address is
47 // IPv6 and it's temporary this field will be false.
48 NetworkInterface interface;
51 // This method will remove permanent IPv6 addresses if a temporary address
52 // is available for same network interface.
53 void RemovePermanentIPv6AddressesWhereTemporaryExists(
54 std::vector<NetworkInterfaceInfo>* infos) {
55 if (!infos || infos->empty())
56 return;
58 // Build a set containing the names of interfaces with a temp IPv6 address
59 std::set<std::string> ifaces_with_temp_addrs;
60 std::vector<NetworkInterfaceInfo>::iterator i;
61 for (i = infos->begin(); i != infos->end(); ++i) {
62 if (!i->permanent && i->interface.address.size() == kIPv6AddressSize) {
63 ifaces_with_temp_addrs.insert(i->interface.name);
67 // If there are no such interfaces then there's no further work.
68 if (ifaces_with_temp_addrs.empty())
69 return;
71 // Search for permenent addresses belonging to same network interface.
72 for (i = infos->begin(); i != infos->end(); ) {
73 // If the address is IPv6 and it's permanent and there is temporary
74 // address for it, then we can remove this address.
75 if ((i->interface.address.size() == kIPv6AddressSize) && i->permanent &&
76 (ifaces_with_temp_addrs.find(i->interface.name) !=
77 ifaces_with_temp_addrs.end())) {
78 i = infos->erase(i);
79 } else {
80 ++i;
85 #endif
87 #if defined(OS_MACOSX) && !defined(OS_IOS)
89 NetworkChangeNotifier::ConnectionType GetNetworkInterfaceType(
90 int addr_family, const std::string& interface_name) {
91 NetworkChangeNotifier::ConnectionType type =
92 NetworkChangeNotifier::CONNECTION_UNKNOWN;
94 struct ifmediareq ifmr = {};
95 strncpy(ifmr.ifm_name, interface_name.c_str(), sizeof(ifmr.ifm_name) - 1);
97 int s = socket(addr_family, SOCK_DGRAM, 0);
98 if (s == -1) {
99 return type;
102 if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1) {
103 if (ifmr.ifm_current & IFM_IEEE80211) {
104 type = NetworkChangeNotifier::CONNECTION_WIFI;
105 } else if (ifmr.ifm_current & IFM_ETHER) {
106 type = NetworkChangeNotifier::CONNECTION_ETHERNET;
109 close(s);
110 return type;
113 #endif
115 } // namespace
117 bool GetNetworkList(NetworkInterfaceList* networks, int policy) {
118 #if defined(OS_NACL)
119 NOTIMPLEMENTED();
120 return false;
121 #elif defined(OS_ANDROID)
122 std::string network_list = android::GetNetworkList();
123 base::StringTokenizer network_interfaces(network_list, "\n");
124 while (network_interfaces.GetNext()) {
125 std::string network_item = network_interfaces.token();
126 base::StringTokenizer network_tokenizer(network_item, "\t");
127 CHECK(network_tokenizer.GetNext());
128 std::string name = network_tokenizer.token();
130 CHECK(network_tokenizer.GetNext());
131 std::string interface_address = network_tokenizer.token();
132 IPAddressNumber address;
133 size_t network_prefix = 0;
134 CHECK(ParseCIDRBlock(network_tokenizer.token(),
135 &address,
136 &network_prefix));
138 CHECK(network_tokenizer.GetNext());
139 uint32 index = 0;
140 CHECK(base::StringToUint(network_tokenizer.token(), &index));
142 networks->push_back(
143 NetworkInterface(name, name, index,
144 NetworkChangeNotifier::CONNECTION_UNKNOWN,
145 address, network_prefix));
147 return true;
148 #else
149 // getifaddrs() may require IO operations.
150 base::ThreadRestrictions::AssertIOAllowed();
152 #if defined(OS_MACOSX) && !defined(OS_IOS)
153 int ioctl_socket = -1;
154 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
155 // we need a socket to query information about temporary address.
156 ioctl_socket = socket(AF_INET6, SOCK_DGRAM, 0);
157 DCHECK_GT(ioctl_socket, 0);
159 #endif
161 ifaddrs *interfaces;
162 if (getifaddrs(&interfaces) < 0) {
163 PLOG(ERROR) << "getifaddrs";
164 return false;
167 std::vector<NetworkInterfaceInfo> network_infos;
169 // Enumerate the addresses assigned to network interfaces which are up.
170 for (ifaddrs *interface = interfaces;
171 interface != NULL;
172 interface = interface->ifa_next) {
173 // Skip loopback interfaces, and ones which are down.
174 if (!(IFF_UP & interface->ifa_flags))
175 continue;
176 if (IFF_LOOPBACK & interface->ifa_flags)
177 continue;
178 // Skip interfaces with no address configured.
179 struct sockaddr* addr = interface->ifa_addr;
180 if (!addr)
181 continue;
183 // Skip unspecified addresses (i.e. made of zeroes) and loopback addresses
184 // configured on non-loopback interfaces.
185 int addr_size = 0;
186 if (addr->sa_family == AF_INET6) {
187 struct sockaddr_in6* addr_in6 =
188 reinterpret_cast<struct sockaddr_in6*>(addr);
189 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
190 addr_size = sizeof(*addr_in6);
191 if (IN6_IS_ADDR_LOOPBACK(sin6_addr) ||
192 IN6_IS_ADDR_UNSPECIFIED(sin6_addr)) {
193 continue;
195 } else if (addr->sa_family == AF_INET) {
196 struct sockaddr_in* addr_in =
197 reinterpret_cast<struct sockaddr_in*>(addr);
198 addr_size = sizeof(*addr_in);
199 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK ||
200 addr_in->sin_addr.s_addr == 0) {
201 continue;
203 } else {
204 // Skip non-IP addresses.
205 continue;
208 const std::string& name = interface->ifa_name;
209 // Filter out VMware interfaces, typically named vmnet1 and vmnet8.
210 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) &&
211 ((name.find("vmnet") != std::string::npos) ||
212 (name.find("vnic") != std::string::npos))) {
213 continue;
216 NetworkInterfaceInfo network_info;
217 NetworkChangeNotifier::ConnectionType connection_type =
218 NetworkChangeNotifier::CONNECTION_UNKNOWN;
219 #if defined(OS_MACOSX) && !defined(OS_IOS)
220 // Check if this is a temporary address. Currently this is only supported
221 // on Mac.
222 if ((policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) &&
223 ioctl_socket >= 0 && addr->sa_family == AF_INET6) {
224 struct in6_ifreq ifr = {};
225 strncpy(ifr.ifr_name, interface->ifa_name, sizeof(ifr.ifr_name) - 1);
226 memcpy(&ifr.ifr_ifru.ifru_addr, interface->ifa_addr,
227 interface->ifa_addr->sa_len);
228 int rv = ioctl(ioctl_socket, SIOCGIFAFLAG_IN6, &ifr);
229 if (rv >= 0) {
230 network_info.permanent = !(ifr.ifr_ifru.ifru_flags & IN6_IFF_TEMPORARY);
234 connection_type = GetNetworkInterfaceType(addr->sa_family, name);
235 #endif
237 IPEndPoint address;
238 if (address.FromSockAddr(addr, addr_size)) {
239 uint8 net_mask = 0;
240 if (interface->ifa_netmask) {
241 // If not otherwise set, assume the same sa_family as ifa_addr.
242 if (interface->ifa_netmask->sa_family == 0) {
243 interface->ifa_netmask->sa_family = addr->sa_family;
245 IPEndPoint netmask;
246 if (netmask.FromSockAddr(interface->ifa_netmask, addr_size)) {
247 net_mask = MaskPrefixLength(netmask.address());
250 network_info.interface = NetworkInterface(
251 name, name, if_nametoindex(name.c_str()),
252 connection_type, address.address(), net_mask);
254 network_infos.push_back(NetworkInterfaceInfo(network_info));
257 freeifaddrs(interfaces);
258 #if defined(OS_MACOSX) && !defined(OS_IOS)
259 if (ioctl_socket >= 0) {
260 close(ioctl_socket);
262 #endif
264 if (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE) {
265 RemovePermanentIPv6AddressesWhereTemporaryExists(&network_infos);
268 for (size_t i = 0; i < network_infos.size(); ++i) {
269 networks->push_back(network_infos[i].interface);
271 return true;
272 #endif
275 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() {
276 return WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
279 } // namespace net