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
3 // found in the LICENSE file.
5 #include "net/base/network_change_notifier_win.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/threading/thread.h"
14 #include "base/time/time.h"
15 #include "net/base/winsock_init.h"
16 #include "net/dns/dns_config_service.h"
18 #pragma comment(lib, "iphlpapi.lib")
24 // Time between NotifyAddrChange retries, on failure.
25 const int kWatchForAddressChangeRetryIntervalMs
= 500;
29 // Thread on which we can run DnsConfigService, which requires AssertIOAllowed
30 // to open registry keys and to handle FilePathWatcher updates.
31 class NetworkChangeNotifierWin::DnsConfigServiceThread
: public base::Thread
{
33 DnsConfigServiceThread() : base::Thread("DnsConfigService") {}
35 virtual ~DnsConfigServiceThread() {
39 virtual void Init() OVERRIDE
{
40 service_
= DnsConfigService::CreateSystemService();
41 service_
->WatchConfig(base::Bind(&NetworkChangeNotifier::SetDnsConfig
));
44 virtual void CleanUp() OVERRIDE
{
49 scoped_ptr
<DnsConfigService
> service_
;
51 DISALLOW_COPY_AND_ASSIGN(DnsConfigServiceThread
);
54 NetworkChangeNotifierWin::NetworkChangeNotifierWin()
55 : NetworkChangeNotifier(NetworkChangeCalculatorParamsWin()),
57 sequential_failures_(0),
59 dns_config_service_thread_(new DnsConfigServiceThread()),
60 last_computed_connection_type_(RecomputeCurrentConnectionType()),
61 last_announced_offline_(
62 last_computed_connection_type_
== CONNECTION_NONE
) {
63 memset(&addr_overlapped_
, 0, sizeof addr_overlapped_
);
64 addr_overlapped_
.hEvent
= WSACreateEvent();
65 dns_config_service_thread_
->StartWithOptions(
66 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0));
69 NetworkChangeNotifierWin::~NetworkChangeNotifierWin() {
71 CancelIPChangeNotify(&addr_overlapped_
);
72 addr_watcher_
.StopWatching();
74 WSACloseEvent(addr_overlapped_
.hEvent
);
78 NetworkChangeNotifier::NetworkChangeCalculatorParams
79 NetworkChangeNotifierWin::NetworkChangeCalculatorParamsWin() {
80 NetworkChangeCalculatorParams params
;
81 // Delay values arrived at by simple experimentation and adjusted so as to
82 // produce a single signal when switching between network connections.
83 params
.ip_address_offline_delay_
= base::TimeDelta::FromMilliseconds(1500);
84 params
.ip_address_online_delay_
= base::TimeDelta::FromMilliseconds(1500);
85 params
.connection_type_offline_delay_
=
86 base::TimeDelta::FromMilliseconds(1500);
87 params
.connection_type_online_delay_
= base::TimeDelta::FromMilliseconds(500);
91 // This implementation does not return the actual connection type but merely
92 // determines if the user is "online" (in which case it returns
93 // CONNECTION_UNKNOWN) or "offline" (and then it returns CONNECTION_NONE).
94 // This is challenging since the only thing we can test with certainty is
95 // whether a *particular* host is reachable.
97 // While we can't conclusively determine when a user is "online", we can at
98 // least reliably recognize some of the situtations when they are clearly
99 // "offline". For example, if the user's laptop is not plugged into an ethernet
100 // network and is not connected to any wireless networks, it must be offline.
102 // There are a number of different ways to implement this on Windows, each with
103 // their pros and cons. Here is a comparison of various techniques considered:
105 // (1) Use InternetGetConnectedState (wininet.dll). This function is really easy
106 // to use (literally a one-liner), and runs quickly. The drawback is it adds a
107 // dependency on the wininet DLL.
109 // (2) Enumerate all of the network interfaces using GetAdaptersAddresses
110 // (iphlpapi.dll), and assume we are "online" if there is at least one interface
111 // that is connected, and that interface is not a loopback or tunnel.
113 // Safari on Windows has a fairly simple implementation that does this:
114 // http://trac.webkit.org/browser/trunk/WebCore/platform/network/win/NetworkStateNotifierWin.cpp.
116 // Mozilla similarly uses this approach:
117 // http://mxr.mozilla.org/mozilla1.9.2/source/netwerk/system/win32/nsNotifyAddrListener.cpp
119 // The biggest drawback to this approach is it is quite complicated.
120 // WebKit's implementation for example doesn't seem to test for ICS gateways
121 // (internet connection sharing), whereas Mozilla's implementation has extra
122 // code to guess that.
124 // (3) The method used in this file comes from google talk, and is similar to
125 // method (2). The main difference is it enumerates the winsock namespace
126 // providers rather than the actual adapters.
128 // I ran some benchmarks comparing the performance of each on my Windows 7
129 // workstation. Here is what I found:
130 // * Approach (1) was pretty much zero-cost after the initial call.
131 // * Approach (2) took an average of 3.25 milliseconds to enumerate the
133 // * Approach (3) took an average of 0.8 ms to enumerate the providers.
135 // In terms of correctness, all three approaches were comparable for the simple
136 // experiments I ran... However none of them correctly returned "offline" when
137 // executing 'ipconfig /release'.
139 NetworkChangeNotifier::ConnectionType
140 NetworkChangeNotifierWin::RecomputeCurrentConnectionType() const {
141 DCHECK(CalledOnValidThread());
145 // The following code was adapted from:
146 // http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/net/notifier/base/win/async_network_alive_win32.cc?view=markup&pathrev=47343
147 // The main difference is we only call WSALookupServiceNext once, whereas
148 // the earlier code would traverse the entire list and pass LUP_FLUSHPREVIOUS
149 // to skip past the large results.
152 WSAQUERYSET query_set
= {0};
153 query_set
.dwSize
= sizeof(WSAQUERYSET
);
154 query_set
.dwNameSpace
= NS_NLA
;
155 // Initiate a client query to iterate through the
156 // currently connected networks.
157 if (0 != WSALookupServiceBegin(&query_set
, LUP_RETURN_ALL
,
159 LOG(ERROR
) << "WSALookupServiceBegin failed with: " << WSAGetLastError();
160 return NetworkChangeNotifier::CONNECTION_UNKNOWN
;
163 bool found_connection
= false;
165 // Retrieve the first available network. In this function, we only
166 // need to know whether or not there is network connection.
167 // Allocate 256 bytes for name, it should be enough for most cases.
168 // If the name is longer, it is OK as we will check the code returned and
169 // set correct network status.
170 char result_buffer
[sizeof(WSAQUERYSET
) + 256] = {0};
171 DWORD length
= sizeof(result_buffer
);
172 reinterpret_cast<WSAQUERYSET
*>(&result_buffer
[0])->dwSize
=
174 int result
= WSALookupServiceNext(
178 reinterpret_cast<WSAQUERYSET
*>(&result_buffer
[0]));
181 // Found a connection!
182 found_connection
= true;
184 DCHECK_EQ(SOCKET_ERROR
, result
);
185 result
= WSAGetLastError();
187 // Error code WSAEFAULT means there is a network connection but the
188 // result_buffer size is too small to contain the results. The
189 // variable "length" returned from WSALookupServiceNext is the minimum
190 // number of bytes required. We do not need to retrieve detail info,
191 // it is enough knowing there was a connection.
192 if (result
== WSAEFAULT
) {
193 found_connection
= true;
194 } else if (result
== WSA_E_NO_MORE
|| result
== WSAENOMORE
) {
195 // There was nothing to iterate over!
197 LOG(WARNING
) << "WSALookupServiceNext() failed with:" << result
;
201 result
= WSALookupServiceEnd(ws_handle
);
202 LOG_IF(ERROR
, result
!= 0)
203 << "WSALookupServiceEnd() failed with: " << result
;
205 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN.
206 return found_connection
? NetworkChangeNotifier::CONNECTION_UNKNOWN
:
207 NetworkChangeNotifier::CONNECTION_NONE
;
210 NetworkChangeNotifier::ConnectionType
211 NetworkChangeNotifierWin::GetCurrentConnectionType() const {
212 base::AutoLock
auto_lock(last_computed_connection_type_lock_
);
213 return last_computed_connection_type_
;
216 void NetworkChangeNotifierWin::SetCurrentConnectionType(
217 ConnectionType connection_type
) {
218 base::AutoLock
auto_lock(last_computed_connection_type_lock_
);
219 last_computed_connection_type_
= connection_type
;
222 void NetworkChangeNotifierWin::OnObjectSignaled(HANDLE object
) {
223 DCHECK(CalledOnValidThread());
224 DCHECK(is_watching_
);
225 is_watching_
= false;
227 // Start watching for the next address change.
228 WatchForAddressChange();
233 void NetworkChangeNotifierWin::NotifyObservers() {
234 DCHECK(CalledOnValidThread());
235 SetCurrentConnectionType(RecomputeCurrentConnectionType());
236 NotifyObserversOfIPAddressChange();
238 // Calling GetConnectionType() at this very moment is likely to give
239 // the wrong result, so we delay that until a little bit later.
241 // The one second delay chosen here was determined experimentally
242 // by adamk on Windows 7.
243 // If after one second we determine we are still offline, we will
246 timer_
.Start(FROM_HERE
, base::TimeDelta::FromSeconds(1), this,
247 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange
);
250 void NetworkChangeNotifierWin::WatchForAddressChange() {
251 DCHECK(CalledOnValidThread());
252 DCHECK(!is_watching_
);
254 // NotifyAddrChange occasionally fails with ERROR_OPEN_FAILED for unknown
255 // reasons. More rarely, it's also been observed failing with
256 // ERROR_NO_SYSTEM_RESOURCES. When either of these happens, we retry later.
257 if (!WatchForAddressChangeInternal()) {
258 ++sequential_failures_
;
260 // TODO(mmenke): If the UMA histograms indicate that this fixes
261 // http://crbug.com/69198, remove this histogram and consider reducing the
263 if (sequential_failures_
== 2000) {
264 UMA_HISTOGRAM_COUNTS_10000("Net.NotifyAddrChangeFailures",
265 sequential_failures_
);
268 base::MessageLoop::current()->PostDelayedTask(
270 base::Bind(&NetworkChangeNotifierWin::WatchForAddressChange
,
271 weak_factory_
.GetWeakPtr()),
272 base::TimeDelta::FromMilliseconds(
273 kWatchForAddressChangeRetryIntervalMs
));
277 // Treat the transition from NotifyAddrChange failing to succeeding as a
278 // network change event, since network changes were not being observed in
280 if (sequential_failures_
> 0)
283 if (sequential_failures_
< 2000) {
284 UMA_HISTOGRAM_COUNTS_10000("Net.NotifyAddrChangeFailures",
285 sequential_failures_
);
289 sequential_failures_
= 0;
292 bool NetworkChangeNotifierWin::WatchForAddressChangeInternal() {
293 DCHECK(CalledOnValidThread());
294 HANDLE handle
= NULL
;
295 DWORD ret
= NotifyAddrChange(&handle
, &addr_overlapped_
);
296 if (ret
!= ERROR_IO_PENDING
)
299 addr_watcher_
.StartWatching(addr_overlapped_
.hEvent
, this);
303 void NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange() {
304 SetCurrentConnectionType(RecomputeCurrentConnectionType());
305 bool current_offline
= IsOffline();
307 // If we continue to appear offline, delay sending out the notification in
308 // case we appear to go online within 20 seconds. UMA histogram data shows
309 // we may not detect the transition to online state after 1 second but within
310 // 20 seconds we generally do.
311 if (last_announced_offline_
&& current_offline
&& offline_polls_
<= 20) {
312 timer_
.Start(FROM_HERE
, base::TimeDelta::FromSeconds(1), this,
313 &NetworkChangeNotifierWin::NotifyParentOfConnectionTypeChange
);
316 if (last_announced_offline_
)
317 UMA_HISTOGRAM_CUSTOM_COUNTS("NCN.OfflinePolls", offline_polls_
, 1, 50, 50);
318 last_announced_offline_
= current_offline
;
320 NotifyObserversOfConnectionTypeChange();