OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <iphlpapi.h> | 7 #include <iphlpapi.h> |
8 #include <wlanapi.h> | 8 #include <wlanapi.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 ::GetProcAddress(module, "WlanSetInterface")); | 78 ::GetProcAddress(module, "WlanSetInterface")); |
79 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( | 79 free_memory_func = reinterpret_cast<WlanFreeMemoryFunc>( |
80 ::GetProcAddress(module, "WlanFreeMemory")); | 80 ::GetProcAddress(module, "WlanFreeMemory")); |
81 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( | 81 close_handle_func = reinterpret_cast<WlanCloseHandleFunc>( |
82 ::GetProcAddress(module, "WlanCloseHandle")); | 82 ::GetProcAddress(module, "WlanCloseHandle")); |
83 initialized = open_handle_func && enum_interfaces_func && | 83 initialized = open_handle_func && enum_interfaces_func && |
84 query_interface_func && set_interface_func && | 84 query_interface_func && set_interface_func && |
85 free_memory_func && close_handle_func; | 85 free_memory_func && close_handle_func; |
86 } | 86 } |
87 | 87 |
88 } // namespace internal | 88 bool GetNetworkListImpl(NetworkInterfaceList* networks, |
89 | 89 int policy, |
90 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { | 90 bool is_xp, |
91 // GetAdaptersAddresses() may require IO operations. | 91 const IP_ADAPTER_ADDRESSES* adapters) { |
92 base::ThreadRestrictions::AssertIOAllowed(); | 92 for (const IP_ADAPTER_ADDRESSES* adapter = adapters; adapter != NULL; |
93 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; | |
94 ULONG len = 0; | |
95 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; | |
96 // First get number of networks. | |
97 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); | |
98 if (result != ERROR_BUFFER_OVERFLOW) { | |
99 // There are 0 networks. | |
100 return true; | |
101 } | |
102 scoped_ptr<char[]> buf(new char[len]); | |
103 IP_ADAPTER_ADDRESSES *adapters = | |
104 reinterpret_cast<IP_ADAPTER_ADDRESSES *>(buf.get()); | |
105 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); | |
106 if (result != NO_ERROR) { | |
107 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; | |
108 return false; | |
109 } | |
110 | |
111 // These two variables are used below when this method is asked to pick a | |
112 // IPv6 address which has the shortest lifetime. | |
113 ULONG ipv6_valid_lifetime = 0; | |
114 scoped_ptr<NetworkInterface> ipv6_address; | |
115 | |
116 for (IP_ADAPTER_ADDRESSES *adapter = adapters; adapter != NULL; | |
117 adapter = adapter->Next) { | 93 adapter = adapter->Next) { |
118 // Ignore the loopback device. | 94 // Ignore the loopback device. |
119 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { | 95 if (adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK) { |
120 continue; | 96 continue; |
121 } | 97 } |
122 | 98 |
123 if (adapter->OperStatus != IfOperStatusUp) { | 99 if (adapter->OperStatus != IfOperStatusUp) { |
124 continue; | 100 continue; |
125 } | 101 } |
126 | 102 |
127 // Ignore any HOST side vmware adapters with a description like: | 103 // Ignore any HOST side vmware adapters with a description like: |
128 // VMware Virtual Ethernet Adapter for VMnet1 | 104 // VMware Virtual Ethernet Adapter for VMnet1 |
129 // but don't ignore any GUEST side adapters with a description like: | 105 // but don't ignore any GUEST side adapters with a description like: |
130 // VMware Accelerated AMD PCNet Adapter #2 | 106 // VMware Accelerated AMD PCNet Adapter #2 |
131 if (policy == EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES && | 107 if ((policy & EXCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES) && |
132 strstr(adapter->AdapterName, "VMnet") != NULL) { | 108 strstr(adapter->AdapterName, "VMnet") != NULL) { |
133 continue; | 109 continue; |
134 } | 110 } |
135 | 111 |
136 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; | 112 for (IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress; |
137 address; address = address->Next) { | 113 address; address = address->Next) { |
138 int family = address->Address.lpSockaddr->sa_family; | 114 int family = address->Address.lpSockaddr->sa_family; |
139 if (family == AF_INET || family == AF_INET6) { | 115 if (family == AF_INET || family == AF_INET6) { |
140 IPEndPoint endpoint; | 116 IPEndPoint endpoint; |
141 if (endpoint.FromSockAddr(address->Address.lpSockaddr, | 117 if (endpoint.FromSockAddr(address->Address.lpSockaddr, |
142 address->Address.iSockaddrLength)) { | 118 address->Address.iSockaddrLength)) { |
143 // XP has no OnLinkPrefixLength field. | 119 // XP has no OnLinkPrefixLength field. |
144 size_t net_prefix = is_xp ? 0 : address->OnLinkPrefixLength; | 120 size_t prefix_length = is_xp ? 0 : address->OnLinkPrefixLength; |
145 if (is_xp) { | 121 if (is_xp) { |
146 // Prior to Windows Vista the FirstPrefix pointed to the list with | 122 // Prior to Windows Vista the FirstPrefix pointed to the list with |
147 // single prefix for each IP address assigned to the adapter. | 123 // single prefix for each IP address assigned to the adapter. |
148 // Order of FirstPrefix does not match order of FirstUnicastAddress, | 124 // Order of FirstPrefix does not match order of FirstUnicastAddress, |
149 // so we need to find corresponding prefix. | 125 // so we need to find corresponding prefix. |
150 for (IP_ADAPTER_PREFIX* prefix = adapter->FirstPrefix; prefix; | 126 for (IP_ADAPTER_PREFIX* prefix = adapter->FirstPrefix; prefix; |
151 prefix = prefix->Next) { | 127 prefix = prefix->Next) { |
152 int prefix_family = prefix->Address.lpSockaddr->sa_family; | 128 int prefix_family = prefix->Address.lpSockaddr->sa_family; |
153 IPEndPoint network_endpoint; | 129 IPEndPoint network_endpoint; |
154 if (prefix_family == family && | 130 if (prefix_family == family && |
155 network_endpoint.FromSockAddr(prefix->Address.lpSockaddr, | 131 network_endpoint.FromSockAddr(prefix->Address.lpSockaddr, |
156 prefix->Address.iSockaddrLength) && | 132 prefix->Address.iSockaddrLength) && |
157 IPNumberMatchesPrefix(endpoint.address(), | 133 IPNumberMatchesPrefix(endpoint.address(), |
158 network_endpoint.address(), | 134 network_endpoint.address(), |
159 prefix->PrefixLength)) { | 135 prefix->PrefixLength)) { |
160 net_prefix = std::max<size_t>(net_prefix, prefix->PrefixLength); | 136 prefix_length = |
| 137 std::max<size_t>(prefix_length, prefix->PrefixLength); |
161 } | 138 } |
162 } | 139 } |
163 } | 140 } |
| 141 |
| 142 // If the duplicate address detection (DAD) state is not changed to |
| 143 // Preferred, skip this address. |
| 144 if (address->DadState != IpDadStatePreferred) { |
| 145 continue; |
| 146 } |
| 147 |
164 uint32 index = | 148 uint32 index = |
165 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; | 149 (family == AF_INET) ? adapter->IfIndex : adapter->Ipv6IfIndex; |
166 // Pick one IPv6 address with least valid lifetime. | 150 |
167 // The reason we are checking |ValidLifeftime| as there is no other | 151 // From http://technet.microsoft.com/en-us/ff568768(v=vs.60).aspx, the |
168 // way identifying the interface type. Usually (and most likely) temp | 152 // way to identify a temporary IPv6 Address is to check if |
169 // IPv6 will have a shorter ValidLifetime value then the permanent | 153 // PrefixOrigin is equal to IpPrefixOriginRouterAdvertisement and |
170 // interface. | 154 // SuffixOrigin equal to IpSuffixOriginRandom. |
171 if (family == AF_INET6 && | 155 int ip_address_attributes = IP_ADDRESS_ATTRIBUTE_NONE; |
172 (policy & INCLUDE_ONLY_TEMP_IPV6_ADDRESS_IF_POSSIBLE)) { | 156 if (family == AF_INET6) { |
173 if (ipv6_valid_lifetime == 0 || | 157 if (address->PrefixOrigin == IpPrefixOriginRouterAdvertisement && |
174 ipv6_valid_lifetime > address->ValidLifetime) { | 158 address->SuffixOrigin == IpSuffixOriginRandom) { |
175 ipv6_valid_lifetime = address->ValidLifetime; | 159 ip_address_attributes |= IP_ADDRESS_ATTRIBUTE_TEMPORARY; |
176 ipv6_address.reset(new NetworkInterface( | 160 } |
177 adapter->AdapterName, | 161 if (address->PreferredLifetime == 0) { |
178 base::SysWideToNativeMB(adapter->FriendlyName), | 162 ip_address_attributes |= IP_ADDRESS_ATTRIBUTE_DEPRECATED; |
179 index, | |
180 GetNetworkInterfaceType(adapter->IfType), | |
181 endpoint.address(), | |
182 net_prefix, | |
183 IP_ADDRESS_ATTRIBUTE_NONE)); | |
184 continue; | |
185 } | 163 } |
186 } | 164 } |
187 networks->push_back( | 165 networks->push_back(NetworkInterface( |
188 NetworkInterface(adapter->AdapterName, | 166 adapter->AdapterName, |
189 base::SysWideToNativeMB(adapter->FriendlyName), | 167 base::SysWideToNativeMB(adapter->FriendlyName), index, |
190 index, | 168 GetNetworkInterfaceType(adapter->IfType), endpoint.address(), |
191 GetNetworkInterfaceType(adapter->IfType), | 169 prefix_length, ip_address_attributes)); |
192 endpoint.address(), | |
193 net_prefix, | |
194 IP_ADDRESS_ATTRIBUTE_NONE)); | |
195 } | 170 } |
196 } | 171 } |
197 } | 172 } |
198 } | 173 } |
199 | |
200 if (ipv6_address.get()) { | |
201 networks->push_back(*(ipv6_address.get())); | |
202 } | |
203 return true; | 174 return true; |
204 } | 175 } |
205 | 176 |
| 177 } // namespace internal |
| 178 |
| 179 bool GetNetworkList(NetworkInterfaceList* networks, int policy) { |
| 180 bool is_xp = base::win::GetVersion() < base::win::VERSION_VISTA; |
| 181 ULONG len = 0; |
| 182 ULONG flags = is_xp ? GAA_FLAG_INCLUDE_PREFIX : 0; |
| 183 // GetAdaptersAddresses() may require IO operations. |
| 184 base::ThreadRestrictions::AssertIOAllowed(); |
| 185 ULONG result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &len); |
| 186 if (result != ERROR_BUFFER_OVERFLOW) { |
| 187 // There are 0 networks. |
| 188 return true; |
| 189 } |
| 190 scoped_ptr<char[]> buf(new char[len]); |
| 191 IP_ADAPTER_ADDRESSES* adapters = |
| 192 reinterpret_cast<IP_ADAPTER_ADDRESSES*>(buf.get()); |
| 193 result = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, adapters, &len); |
| 194 if (result != NO_ERROR) { |
| 195 LOG(ERROR) << "GetAdaptersAddresses failed: " << result; |
| 196 return false; |
| 197 } |
| 198 |
| 199 return internal::GetNetworkListImpl(networks, policy, is_xp, adapters); |
| 200 } |
| 201 |
206 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { | 202 WifiPHYLayerProtocol GetWifiPHYLayerProtocol() { |
207 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); | 203 const internal::WlanApi& wlanapi = internal::WlanApi::GetInstance(); |
208 if (!wlanapi.initialized) | 204 if (!wlanapi.initialized) |
209 return WIFI_PHY_LAYER_PROTOCOL_NONE; | 205 return WIFI_PHY_LAYER_PROTOCOL_NONE; |
210 | 206 |
211 internal::WlanHandle client; | 207 internal::WlanHandle client; |
212 DWORD cur_version = 0; | 208 DWORD cur_version = 0; |
213 const DWORD kMaxClientVersion = 2; | 209 const DWORD kMaxClientVersion = 2; |
214 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); | 210 DWORD result = wlanapi.OpenHandle(kMaxClientVersion, &cur_version, &client); |
215 if (result != ERROR_SUCCESS) | 211 if (result != ERROR_SUCCESS) |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 | 314 |
319 private: | 315 private: |
320 internal::WlanHandle client_; | 316 internal::WlanHandle client_; |
321 }; | 317 }; |
322 | 318 |
323 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { | 319 scoped_ptr<ScopedWifiOptions> SetWifiOptions(int options) { |
324 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); | 320 return scoped_ptr<ScopedWifiOptions>(new WifiOptionSetter(options)); |
325 } | 321 } |
326 | 322 |
327 } // namespace net | 323 } // namespace net |
OLD | NEW |