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 <nw/private.h>
36 #import "keychain/ckks/CKKS.h"
37 #import "keychain/ckks/CKKSGroupOperation.h"
38 #import "keychain/ckks/CKKSResultOperation.h"
39 #import "keychain/ckks/CKKSReachabilityTracker.h"
40 #import "keychain/ckks/CKKSAnalytics.h"
41 #import "keychain/ot/ObjCImprovements.h"
43 // force reachability timeout every now and then
44 #define REACHABILITY_TIMEOUT (12 * 3600 * NSEC_PER_SEC)
46 @interface CKKSReachabilityTracker ()
47 @property bool haveNetwork;
48 @property dispatch_queue_t queue;
49 @property NSOperationQueue* operationQueue;
50 @property nw_path_monitor_t networkMonitor;
51 @property dispatch_source_t timer;
54 @implementation CKKSReachabilityTracker
56 - (instancetype)init {
57 if((self = [super init])) {
58 _queue = dispatch_queue_create("reachabiltity-tracker", DISPATCH_QUEUE_SERIAL_WITH_AUTORELEASE_POOL);
59 _operationQueue = [[NSOperationQueue alloc] init];
61 dispatch_sync(_queue, ^{
62 [self _onQueueResetReachabilityDependency];
67 if(!SecCKKSTestsEnabled()) {
68 _networkMonitor = nw_path_monitor_create();
69 nw_path_monitor_set_queue(self.networkMonitor, _queue);
70 nw_path_monitor_set_update_handler(self.networkMonitor, ^(nw_path_t _Nonnull path) {
72 bool networkAvailable = (nw_path_get_status(path) == nw_path_status_satisfied);
74 secinfo("ckksnetwork", "nw_path update: network is %@", networkAvailable ? @"available" : @"unavailable");
75 [self _onqueueSetNetworkReachability:networkAvailable];
77 nw_path_monitor_start(self.networkMonitor);
83 - (NSString*)description {
84 return [NSString stringWithFormat: @"<CKKSReachabilityTracker: %@>", self.haveNetwork ? @"online" : @"offline"];
87 - (bool)currentReachability {
88 __block bool currentReachability = false;
89 dispatch_sync(self.queue, ^{
90 currentReachability = self.haveNetwork;
92 return currentReachability;
95 - (void)_onQueueRunReachabilityDependency
97 dispatch_assert_queue(self.queue);
98 // We have network now, or the timer expired. Either way, execute dependency
99 if (self.reachabilityDependency) {
100 [self.operationQueue addOperation: self.reachabilityDependency];
101 self.reachabilityDependency = nil;
104 dispatch_source_cancel(self.timer);
109 - (void)_onQueueResetReachabilityDependency {
110 dispatch_assert_queue(self.queue);
112 if(self.reachabilityDependency == nil || ![self.reachabilityDependency isPending]) {
115 secnotice("ckksnetwork", "Network unavailable");
116 self.reachabilityDependency = [CKKSResultOperation named:@"network-available-dependency" withBlock: ^{
118 if (self.haveNetwork) {
119 secnotice("ckksnetwork", "Network available");
121 secnotice("ckksnetwork", "Network still not available, retrying after waiting %2.1f hours",
122 ((float)(REACHABILITY_TIMEOUT/NSEC_PER_SEC)) / 3600);
127 * Make sure we are not stuck forever and retry every REACHABILITY_TIMEOUT
129 self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
131 (dispatch_source_timer_flags_t)0,
133 dispatch_source_set_event_handler(self.timer, ^{
139 [[CKKSAnalytics logger] noteEvent:CKKSEventReachabilityTimerExpired];
140 [self _onQueueRunReachabilityDependency];
144 dispatch_source_set_timer(self.timer,
145 dispatch_time(DISPATCH_TIME_NOW, REACHABILITY_TIMEOUT),
146 DISPATCH_TIME_FOREVER, //one-shot
148 dispatch_resume(self.timer);
152 - (void)_onqueueSetNetworkReachability:(bool)haveNetwork {
153 dispatch_assert_queue(self.queue);
155 bool hadNetwork = self.haveNetwork;
156 self.haveNetwork = !!(haveNetwork);
158 if(hadNetwork != self.haveNetwork) {
159 if(self.haveNetwork) {
160 // We're have network now
161 [self _onQueueRunReachabilityDependency];
163 [self _onQueueResetReachabilityDependency];
168 - (void)setNetworkReachability:(bool)reachable
170 dispatch_sync(self.queue, ^{
171 [self _onqueueSetNetworkReachability:reachable];
175 - (bool)isNetworkError:(NSError *)error {
176 return [CKKSReachabilityTracker isNetworkError:error];
179 + (bool)isNetworkError:(NSError *)error {
184 if([CKKSReachabilityTracker isNetworkFailureError:error]) {
188 if ([error.domain isEqualToString:CKErrorDomain] &&
189 (error.code == CKErrorNetworkUnavailable)) {
193 if ([error.domain isEqualToString:NSURLErrorDomain] &&
194 error.code == NSURLErrorTimedOut) {
201 + (bool)isNetworkFailureError:(NSError *)error
206 if ([error.domain isEqualToString:CKErrorDomain] && error.code == CKErrorNetworkFailure) {