OLD | NEW |
---|---|
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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_linux.h" | 5 #include "net/base/net_util_linux.h" |
6 | 6 |
7 #include <net/if.h> | 7 #include <linux/if.h> |
8 #include <netinet/in.h> | 8 #include <linux/wireless.h> |
9 #include <set> | 9 #include <set> |
10 #include <sys/ioctl.h> | |
10 #include <sys/types.h> | 11 #include <sys/types.h> |
11 | 12 |
12 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/strings/string_tokenizer.h" | 17 #include "base/strings/string_tokenizer.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
19 #include "net/base/address_tracker_linux.h" | 20 #include "net/base/address_tracker_linux.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 namespace internal { | 63 namespace internal { |
63 | 64 |
64 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { | 65 inline const unsigned char* GetIPAddressData(const IPAddressNumber& ip) { |
65 #if defined(OS_ANDROID) | 66 #if defined(OS_ANDROID) |
66 return ip.begin(); | 67 return ip.begin(); |
67 #else | 68 #else |
68 return ip.data(); | 69 return ip.data(); |
69 #endif | 70 #endif |
70 } | 71 } |
71 | 72 |
73 // Gets the connection type for interface |ifname| by checking for wireless | |
74 // extensions. | |
75 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( | |
76 const std::string& ifname) { | |
77 NetworkChangeNotifier::ConnectionType type = | |
78 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
79 | |
80 int s = socket(AF_INET, SOCK_STREAM, 0); | |
81 if (s == -1) | |
82 return type; | |
83 | |
84 struct iwreq pwrq; | |
85 memset(&pwrq, 0, sizeof(pwrq)); | |
86 strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ); | |
87 | |
88 if (ioctl(s, SIOCGIWNAME, &pwrq) != -1) { | |
89 type = NetworkChangeNotifier::CONNECTION_WIFI; | |
90 } else { | |
91 type = NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
pauljensen
2015/01/27 20:25:17
I think this else clause can be deleted
derekjchow1
2015/01/27 21:50:32
Done.
| |
92 } | |
93 | |
94 close(s); | |
95 return type; | |
96 } | |
97 | |
72 bool GetNetworkListImpl( | 98 bool GetNetworkListImpl( |
73 NetworkInterfaceList* networks, | 99 NetworkInterfaceList* networks, |
74 int policy, | 100 int policy, |
75 const base::hash_set<int>& online_links, | 101 const base::hash_set<int>& online_links, |
76 const internal::AddressTrackerLinux::AddressMap& address_map, | 102 const internal::AddressTrackerLinux::AddressMap& address_map, |
77 GetInterfaceNameFunction get_interface_name) { | 103 GetInterfaceNameFunction get_interface_name) { |
78 std::map<int, std::string> ifnames; | 104 std::map<int, std::string> ifnames; |
79 | 105 |
80 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 106 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
81 address_map.begin(); | 107 address_map.begin(); |
(...skipping 23 matching lines...) Expand all Loading... | |
105 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, | 131 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, |
106 &ip_attributes)) | 132 &ip_attributes)) |
107 continue; | 133 continue; |
108 } | 134 } |
109 | 135 |
110 // Find the name of this link. | 136 // Find the name of this link. |
111 std::map<int, std::string>::const_iterator itname = | 137 std::map<int, std::string>::const_iterator itname = |
112 ifnames.find(it->second.ifa_index); | 138 ifnames.find(it->second.ifa_index); |
113 std::string ifname; | 139 std::string ifname; |
114 if (itname == ifnames.end()) { | 140 if (itname == ifnames.end()) { |
115 char buffer[IF_NAMESIZE] = {0}; | 141 char buffer[IFNAMSIZ] = {0}; |
116 if (get_interface_name(it->second.ifa_index, buffer)) { | 142 if (get_interface_name(it->second.ifa_index, buffer)) { |
117 ifname = ifnames[it->second.ifa_index] = buffer; | 143 ifname = ifnames[it->second.ifa_index] = buffer; |
118 } else { | 144 } else { |
119 // Ignore addresses whose interface name can't be retrieved. | 145 // Ignore addresses whose interface name can't be retrieved. |
120 continue; | 146 continue; |
121 } | 147 } |
122 } else { | 148 } else { |
123 ifname = itname->second; | 149 ifname = itname->second; |
124 } | 150 } |
125 | 151 |
126 // Based on the interface name and policy, determine whether we | 152 // Based on the interface name and policy, determine whether we |
127 // should ignore it. | 153 // should ignore it. |
128 if (ShouldIgnoreInterface(ifname, policy)) | 154 if (ShouldIgnoreInterface(ifname, policy)) |
129 continue; | 155 continue; |
130 | 156 |
157 NetworkChangeNotifier::ConnectionType type = | |
158 GetInterfaceConnectionType(ifname); | |
159 | |
131 networks->push_back( | 160 networks->push_back( |
132 NetworkInterface(ifname, ifname, it->second.ifa_index, | 161 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
133 NetworkChangeNotifier::CONNECTION_UNKNOWN, it->first, | |
134 it->second.ifa_prefixlen, ip_attributes)); | 162 it->second.ifa_prefixlen, ip_attributes)); |
135 } | 163 } |
136 | 164 |
137 return true; | 165 return true; |
138 } | 166 } |
139 | 167 |
140 } // namespace internal | 168 } // namespace internal |
141 | 169 |
142 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 170 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
143 if (networks == NULL) | 171 if (networks == NULL) |
144 return false; | 172 return false; |
145 | 173 |
146 internal::AddressTrackerLinux tracker; | 174 internal::AddressTrackerLinux tracker; |
147 tracker.Init(); | 175 tracker.Init(); |
148 | 176 |
149 return internal::GetNetworkListImpl(networks, policy, | 177 return internal::GetNetworkListImpl( |
150 tracker.GetOnlineLinks(), | 178 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
151 tracker.GetAddressMap(), &if_indextoname); | 179 &internal::AddressTrackerLinux::GetInterfaceName); |
152 } | 180 } |
153 | 181 |
154 } // namespace net | 182 } // namespace net |
OLD | NEW |