2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #import <Foundation/Foundation.h>
27 #import <CloudKit/CloudKit.h>
29 #import <dispatch/dispatch.h>
31 #import <sys/socket.h>
32 #import <netinet/in.h>
34 #import "keychain/ckks/CKKS.h"
35 #import "keychain/ckks/CKKSGroupOperation.h"
36 #import "keychain/ckks/CKKSResultOperation.h"
37 #import "keychain/ckks/CKKSReachabilityTracker.h"
38 #import "keychain/ckks/CKKSAnalytics.h"
40 // force reachability timeout every now and then
41 #define REACHABILITY_TIMEOUT (12 * 3600 * NSEC_PER_SEC)
43 @interface CKKSReachabilityTracker ()
44 @property bool haveNetwork;
45 @property dispatch_queue_t queue;
46 @property NSOperationQueue* operationQueue;
47 @property (assign) SCNetworkReachabilityRef reachability;
48 @property dispatch_source_t timer;
51 @implementation CKKSReachabilityTracker
54 callout(SCNetworkReachabilityRef reachability,
55 SCNetworkReachabilityFlags flags,
58 CKKSReachabilityTracker *tracker = (__bridge id)context;
59 [tracker _onqueueRecheck:flags];
62 - (instancetype)init {
63 if((self = [super init])) {
64 _queue = dispatch_queue_create("reachabiltity-tracker", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
65 _operationQueue = [[NSOperationQueue alloc] init];
67 dispatch_sync(_queue, ^{
68 [self _onQueueResetReachabilityDependency];
71 __weak __typeof(self) weakSelf = self;
73 if(!SecCKKSTestsEnabled()) {
74 struct sockaddr_in zeroAddress;
75 bzero(&zeroAddress, sizeof(zeroAddress));
76 zeroAddress.sin_len = sizeof(zeroAddress);
77 zeroAddress.sin_family = AF_INET;
79 _reachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
81 SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
82 SCNetworkReachabilitySetDispatchQueue(_reachability, _queue);
83 SCNetworkReachabilitySetCallback(_reachability, callout, &context);
91 -(NSString*)description {
92 return [NSString stringWithFormat: @"<CKKSReachabilityTracker: %@>", self.haveNetwork ? @"online" : @"offline"];
95 -(bool)currentReachability {
96 __block bool currentReachability = false;
97 dispatch_sync(self.queue, ^{
98 currentReachability = self.haveNetwork;
100 return currentReachability;
103 -(void)_onQueueRunreachabilityDependency
105 dispatch_assert_queue(self.queue);
106 // We're have network now, or timer expired, either way, execute dependency
107 if (self.reachabilityDependency) {
108 [self.operationQueue addOperation: self.reachabilityDependency];
109 self.reachabilityDependency = nil;
112 dispatch_source_cancel(self.timer);
117 -(void)_onQueueResetReachabilityDependency {
118 dispatch_assert_queue(self.queue);
120 if(self.reachabilityDependency == nil || ![self.reachabilityDependency isPending]) {
121 __weak __typeof(self) weakSelf = self;
123 secnotice("ckksnetwork", "Network unavailable");
124 self.reachabilityDependency = [CKKSResultOperation named:@"network-available-dependency" withBlock: ^{
125 __typeof(self) strongSelf = weakSelf;
126 if (strongSelf == nil) {
129 if (strongSelf.haveNetwork) {
130 secnotice("ckksnetwork", "Network available");
132 secnotice("ckksnetwork", "Network still not available, retrying after waiting %2.1f hours",
133 ((float)(REACHABILITY_TIMEOUT/NSEC_PER_SEC)) / 3600);
138 * Make sure we are not stuck forever and retry every REACHABILITY_TIMEOUT
140 self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
142 (dispatch_source_timer_flags_t)0,
144 dispatch_source_set_event_handler(self.timer, ^{
145 __typeof(self) strongSelf = weakSelf;
146 if (strongSelf == nil) {
149 if (strongSelf.timer) {
150 [[CKKSAnalytics logger] noteEvent:CKKSEventReachabilityTimerExpired];
151 [strongSelf _onQueueRunreachabilityDependency];
155 dispatch_source_set_timer(self.timer,
156 dispatch_time(DISPATCH_TIME_NOW, REACHABILITY_TIMEOUT),
157 DISPATCH_TIME_FOREVER, //one-shot
159 dispatch_resume(self.timer);
163 -(void)_onqueueRecheck:(SCNetworkReachabilityFlags)flags {
164 dispatch_assert_queue(self.queue);
166 const SCNetworkReachabilityFlags reachabilityFlags =
167 kSCNetworkReachabilityFlagsReachable
168 | kSCNetworkReachabilityFlagsConnectionAutomatic
170 | kSCNetworkReachabilityFlagsIsWWAN
174 bool hadNetwork = self.haveNetwork;
175 self.haveNetwork = !!(flags & reachabilityFlags);
177 if(hadNetwork != self.haveNetwork) {
178 if(self.haveNetwork) {
179 // We're have network now
180 [self _onQueueRunreachabilityDependency];
182 [self _onQueueResetReachabilityDependency];
187 + (SCNetworkReachabilityFlags)getReachabilityFlags:(SCNetworkReachabilityRef)target
189 SCNetworkReachabilityFlags flags;
190 if (SCNetworkReachabilityGetFlags(target, &flags))
196 dispatch_sync(self.queue, ^{
197 SCNetworkReachabilityFlags flags = [CKKSReachabilityTracker getReachabilityFlags:self.reachability];
198 [self _onqueueRecheck:flags];
202 -(bool)isNetworkError:(NSError *)error {
205 return ([error.domain isEqualToString:CKErrorDomain] &&
206 (error.code == CKErrorNetworkUnavailable
207 || error.code == CKErrorNetworkFailure));