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 <sys/ioctl.h> | |
pauljensen
2015/01/13 13:14:30
I think this should be moved below <set> for alpha
derekjchow1
2015/01/13 21:32:39
Done.
| |
9 #include <set> | 10 #include <set> |
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" |
(...skipping 43 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 char* GetInterfaceName(unsigned int interface_index, char* buf) { | |
74 int ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0); | |
75 if (ioctl_socket < 0) { | |
76 strncpy(buf, "", IFNAMSIZ); | |
pauljensen
2015/01/13 13:14:30
Can we remove this strncpy and add a memset(buf, 0
derekjchow1
2015/01/13 21:32:35
Done.
| |
77 return buf; | |
78 } | |
79 static struct ifreq ifr; | |
80 memset(&ifr, 0, sizeof(ifr)); | |
81 ifr.ifr_ifindex = interface_index; | |
82 int rv = ioctl(ioctl_socket, SIOCGIFNAME, &ifr); | |
83 close(ioctl_socket); | |
84 if (rv != 0) { | |
85 strncpy(buf, "", IFNAMSIZ); | |
pauljensen
2015/01/13 13:14:30
Ditto.
derekjchow1
2015/01/13 21:32:35
Done.
| |
86 return buf; | |
87 } | |
88 strncpy(buf, ifr.ifr_name, IFNAMSIZ); | |
pauljensen
2015/01/13 13:14:30
This could result in an unterminated string causin
derekjchow1
2015/01/13 21:32:35
Done.
| |
89 return buf; | |
90 } | |
91 | |
92 NetworkChangeNotifier::ConnectionType GetInterfaceConnectionType( | |
93 std::string ifname) { | |
94 NetworkChangeNotifier::ConnectionType type = | |
95 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
96 | |
97 int s = socket(AF_INET, SOCK_STREAM, 0); | |
98 if (s == -1) { | |
99 return type; | |
100 } | |
101 | |
102 struct iwreq pwrq; | |
103 memset(&pwrq, 0, sizeof(pwrq)); | |
104 strncpy(pwrq.ifr_name, ifname.c_str(), IFNAMSIZ); | |
105 | |
106 if (ioctl(s, SIOCGIWNAME, &pwrq) != -1) { | |
107 type = NetworkChangeNotifier::CONNECTION_WIFI; | |
108 } else { | |
109 type = NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
110 } | |
111 | |
112 close(s); | |
113 return type; | |
114 } | |
115 | |
72 bool GetNetworkListImpl( | 116 bool GetNetworkListImpl( |
73 NetworkInterfaceList* networks, | 117 NetworkInterfaceList* networks, |
74 int policy, | 118 int policy, |
75 const base::hash_set<int>& online_links, | 119 const base::hash_set<int>& online_links, |
76 const internal::AddressTrackerLinux::AddressMap& address_map, | 120 const internal::AddressTrackerLinux::AddressMap& address_map, |
77 GetInterfaceNameFunction get_interface_name) { | 121 GetInterfaceNameFunction get_interface_name) { |
78 std::map<int, std::string> ifnames; | 122 std::map<int, std::string> ifnames; |
79 | 123 |
80 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = | 124 for (internal::AddressTrackerLinux::AddressMap::const_iterator it = |
81 address_map.begin(); | 125 address_map.begin(); |
(...skipping 23 matching lines...) Expand all Loading... | |
105 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, | 149 if (!TryConvertNativeToNetIPAttributes(it->second.ifa_flags, |
106 &ip_attributes)) | 150 &ip_attributes)) |
107 continue; | 151 continue; |
108 } | 152 } |
109 | 153 |
110 // Find the name of this link. | 154 // Find the name of this link. |
111 std::map<int, std::string>::const_iterator itname = | 155 std::map<int, std::string>::const_iterator itname = |
112 ifnames.find(it->second.ifa_index); | 156 ifnames.find(it->second.ifa_index); |
113 std::string ifname; | 157 std::string ifname; |
114 if (itname == ifnames.end()) { | 158 if (itname == ifnames.end()) { |
115 char buffer[IF_NAMESIZE] = {0}; | 159 char buffer[IFNAMSIZ] = {0}; |
116 if (get_interface_name(it->second.ifa_index, buffer)) { | 160 if (get_interface_name(it->second.ifa_index, buffer)) { |
117 ifname = ifnames[it->second.ifa_index] = buffer; | 161 ifname = ifnames[it->second.ifa_index] = buffer; |
118 } else { | 162 } else { |
119 // Ignore addresses whose interface name can't be retrieved. | 163 // Ignore addresses whose interface name can't be retrieved. |
120 continue; | 164 continue; |
121 } | 165 } |
122 } else { | 166 } else { |
123 ifname = itname->second; | 167 ifname = itname->second; |
124 } | 168 } |
125 | 169 |
126 // Based on the interface name and policy, determine whether we | 170 // Based on the interface name and policy, determine whether we |
127 // should ignore it. | 171 // should ignore it. |
128 if (ShouldIgnoreInterface(ifname, policy)) | 172 if (ShouldIgnoreInterface(ifname, policy)) |
129 continue; | 173 continue; |
130 | 174 |
175 NetworkChangeNotifier::ConnectionType type = | |
176 GetInterfaceConnectionType(ifname); | |
177 | |
131 networks->push_back( | 178 networks->push_back( |
132 NetworkInterface(ifname, ifname, it->second.ifa_index, | 179 NetworkInterface(ifname, ifname, it->second.ifa_index, type, it->first, |
133 NetworkChangeNotifier::CONNECTION_UNKNOWN, it->first, | |
134 it->second.ifa_prefixlen, ip_attributes)); | 180 it->second.ifa_prefixlen, ip_attributes)); |
135 } | 181 } |
136 | 182 |
137 return true; | 183 return true; |
138 } | 184 } |
139 | 185 |
140 } // namespace internal | 186 } // namespace internal |
141 | 187 |
142 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 188 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
143 if (networks == NULL) | 189 if (networks == NULL) |
144 return false; | 190 return false; |
145 | 191 |
146 internal::AddressTrackerLinux tracker; | 192 internal::AddressTrackerLinux tracker; |
147 tracker.Init(); | 193 tracker.Init(); |
148 | 194 |
149 return internal::GetNetworkListImpl(networks, policy, | 195 return internal::GetNetworkListImpl( |
150 tracker.GetOnlineLinks(), | 196 networks, policy, tracker.GetOnlineLinks(), tracker.GetAddressMap(), |
151 tracker.GetAddressMap(), &if_indextoname); | 197 &internal::GetInterfaceName); |
152 } | 198 } |
153 | 199 |
154 } // namespace net | 200 } // namespace net |
OLD | NEW |