1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/base/network_change_notifier_mac.h"
#include <netinet/in.h>
#include <SystemConfiguration/SCDynamicStoreKey.h>
#include <SystemConfiguration/SCNetworkReachability.h>
#include <SystemConfiguration/SCSchemaDefinitions.h>
#include "base/mac/scoped_cftyperef.h"
#include "base/synchronization/lock.h"
namespace net {
static bool CalculateReachability(SCNetworkConnectionFlags flags) {
bool reachable = flags & kSCNetworkFlagsReachable;
bool connection_required = flags & kSCNetworkFlagsConnectionRequired;
return reachable && !connection_required;
}
NetworkChangeNotifierMac::NetworkChangeNotifierMac()
: forwarder_(this),
config_watcher_(&forwarder_),
network_reachable_(true) {}
NetworkChangeNotifierMac::~NetworkChangeNotifierMac() {
if (reachability_.get() && run_loop_.get()) {
SCNetworkReachabilityUnscheduleFromRunLoop(reachability_.get(),
run_loop_.get(),
kCFRunLoopCommonModes);
}
}
bool NetworkChangeNotifierMac::IsCurrentlyOffline() const {
base::AutoLock lock(network_reachable_lock_);
return !network_reachable_;
}
void NetworkChangeNotifierMac::SetDynamicStoreNotificationKeys(
SCDynamicStoreRef store) {
// Called on notifier thread.
run_loop_.reset(CFRunLoopGetCurrent());
CFRetain(run_loop_.get());
base::mac::ScopedCFTypeRef<CFMutableArrayRef> notification_keys(
CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks));
base::mac::ScopedCFTypeRef<CFStringRef> key(
SCDynamicStoreKeyCreateNetworkGlobalEntity(
NULL, kSCDynamicStoreDomainState, kSCEntNetInterface));
CFArrayAppendValue(notification_keys.get(), key.get());
key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
NULL, kSCDynamicStoreDomainState, kSCEntNetIPv4));
CFArrayAppendValue(notification_keys.get(), key.get());
key.reset(SCDynamicStoreKeyCreateNetworkGlobalEntity(
NULL, kSCDynamicStoreDomainState, kSCEntNetIPv6));
CFArrayAppendValue(notification_keys.get(), key.get());
// Set the notification keys. This starts us receiving notifications.
bool ret = SCDynamicStoreSetNotificationKeys(
store, notification_keys.get(), NULL);
// TODO(willchan): Figure out a proper way to handle this rather than crash.
CHECK(ret);
// Try to reach 0.0.0.0. This is the approach taken by Firefox:
//
// http://mxr.mozilla.org/mozilla2.0/source/netwerk/system/mac/nsNetworkLinkService.mm
//
// From my (adamk) testing on Snow Leopard, 0.0.0.0
// seems to be reachable if any network connection is available.
struct sockaddr_in addr = {0};
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
reachability_.reset(SCNetworkReachabilityCreateWithAddress(
kCFAllocatorDefault, reinterpret_cast<struct sockaddr*>(&addr)));
// 1. Get the initial network state synchronously.
SCNetworkConnectionFlags flags;
if (SCNetworkReachabilityGetFlags(reachability_.get(), &flags)) {
bool reachable = CalculateReachability(flags);
base::AutoLock lock(network_reachable_lock_);
network_reachable_ = reachable;
} else
LOG(ERROR) << "Could not get initial network state.";
// 2. Set up periodic callbacks when the state changes.
SCNetworkReachabilityContext reachability_context = {
0, // version
this, // user data
NULL, // retain
NULL, // release
NULL // description
};
if (!SCNetworkReachabilitySetCallback(
reachability_.get(),
&NetworkChangeNotifierMac::ReachabilityCallback,
&reachability_context)) {
LOG(DFATAL) << "Could not set network reachability callback";
reachability_.reset();
} else if (!SCNetworkReachabilityScheduleWithRunLoop(reachability_.get(),
run_loop_,
kCFRunLoopCommonModes)) {
LOG(DFATAL) << "Could not schedule network reachability on run loop";
reachability_.reset();
}
}
void NetworkChangeNotifierMac::OnNetworkConfigChange(CFArrayRef changed_keys) {
DCHECK_EQ(run_loop_.get(), CFRunLoopGetCurrent());
for (CFIndex i = 0; i < CFArrayGetCount(changed_keys); ++i) {
CFStringRef key = static_cast<CFStringRef>(
CFArrayGetValueAtIndex(changed_keys, i));
if (CFStringHasSuffix(key, kSCEntNetIPv4) ||
CFStringHasSuffix(key, kSCEntNetIPv6)) {
NotifyObserversOfIPAddressChange();
return;
}
if (CFStringHasSuffix(key, kSCEntNetInterface)) {
// TODO(willchan): Does not appear to be working. Look into this.
// Perhaps this isn't needed anyway.
} else {
NOTREACHED();
}
}
}
// static
void NetworkChangeNotifierMac::ReachabilityCallback(
SCNetworkReachabilityRef target,
SCNetworkConnectionFlags flags,
void* notifier) {
NetworkChangeNotifierMac* notifier_mac =
static_cast<NetworkChangeNotifierMac*>(notifier);
DCHECK_EQ(notifier_mac->run_loop_.get(), CFRunLoopGetCurrent());
bool new_reachability = CalculateReachability(flags);
bool old_reachability;
{
base::AutoLock lock(notifier_mac->network_reachable_lock_);
old_reachability = notifier_mac->network_reachable_;
notifier_mac->network_reachable_ = new_reachability;
}
if (old_reachability != new_reachability)
NotifyObserversOfOnlineStateChange();
}
} // namespace net
|