2019-04-12 19:27:00 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2019 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-05 14:00:49 -04:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORReachability.h"
|
2020-06-24 17:50:15 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORReachability_Private.h"
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2020-07-31 08:12:23 -07:00
|
|
|
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
|
2019-08-08 11:34:52 -07:00
|
|
|
|
2019-04-12 19:27:00 -07:00
|
|
|
#import <netinet/in.h>
|
|
|
|
|
|
|
|
|
|
/** Sets the _callbackFlag ivar whenever the network changes.
|
|
|
|
|
*
|
|
|
|
|
* @param reachability The reachability object calling back.
|
|
|
|
|
* @param flags The new flag values.
|
|
|
|
|
* @param info Any data that might be passed in by the callback.
|
|
|
|
|
*/
|
2020-04-24 15:19:43 -07:00
|
|
|
static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
|
|
|
|
|
GDTCORNetworkReachabilityFlags flags,
|
2019-09-09 15:21:29 -07:00
|
|
|
void *info);
|
2019-04-12 19:27:00 -07:00
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
@implementation GDTCORReachability {
|
2019-04-12 19:27:00 -07:00
|
|
|
/** The reachability object. */
|
2020-04-24 15:19:43 -07:00
|
|
|
GDTCORNetworkReachabilityRef _reachabilityRef;
|
2019-04-12 19:27:00 -07:00
|
|
|
|
|
|
|
|
/** The queue on which callbacks and all work will occur. */
|
|
|
|
|
dispatch_queue_t _reachabilityQueue;
|
|
|
|
|
|
|
|
|
|
/** Flags specified by reachability callbacks. */
|
2020-04-24 15:19:43 -07:00
|
|
|
GDTCORNetworkReachabilityFlags _callbackFlags;
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
2020-04-27 14:56:22 -07:00
|
|
|
+ (void)initialize {
|
2019-04-12 19:27:00 -07:00
|
|
|
[self sharedInstance];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance {
|
2019-09-09 15:21:29 -07:00
|
|
|
static GDTCORReachability *sharedInstance;
|
2019-04-12 19:27:00 -07:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
2019-09-09 15:21:29 -07:00
|
|
|
sharedInstance = [[GDTCORReachability alloc] init];
|
2019-04-12 19:27:00 -07:00
|
|
|
});
|
|
|
|
|
return sharedInstance;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 15:19:43 -07:00
|
|
|
+ (GDTCORNetworkReachabilityFlags)currentFlags {
|
|
|
|
|
__block GDTCORNetworkReachabilityFlags currentFlags;
|
|
|
|
|
#if !TARGET_OS_WATCH
|
2019-09-09 15:21:29 -07:00
|
|
|
dispatch_sync([GDTCORReachability sharedInstance] -> _reachabilityQueue, ^{
|
|
|
|
|
GDTCORReachability *reachability = [GDTCORReachability sharedInstance];
|
2020-02-21 09:56:18 -08:00
|
|
|
currentFlags =
|
|
|
|
|
reachability->_callbackFlags ? reachability->_callbackFlags : reachability->_flags;
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"Initial reachability flags determined: %d", currentFlags);
|
2019-04-12 19:27:00 -07:00
|
|
|
});
|
2020-04-24 15:19:43 -07:00
|
|
|
#else
|
|
|
|
|
currentFlags = kGDTCORNetworkReachabilityFlagsReachable;
|
|
|
|
|
#endif
|
2019-04-12 19:27:00 -07:00
|
|
|
return currentFlags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
|
self = [super init];
|
2020-04-24 15:19:43 -07:00
|
|
|
#if !TARGET_OS_WATCH
|
2019-04-12 19:27:00 -07:00
|
|
|
if (self) {
|
|
|
|
|
struct sockaddr_in zeroAddress;
|
|
|
|
|
bzero(&zeroAddress, sizeof(zeroAddress));
|
|
|
|
|
zeroAddress.sin_len = sizeof(zeroAddress);
|
|
|
|
|
zeroAddress.sin_family = AF_INET;
|
|
|
|
|
|
2019-09-09 15:21:29 -07:00
|
|
|
_reachabilityQueue =
|
|
|
|
|
dispatch_queue_create("com.google.GDTCORReachability", DISPATCH_QUEUE_SERIAL);
|
2019-04-12 19:27:00 -07:00
|
|
|
_reachabilityRef = SCNetworkReachabilityCreateWithAddress(
|
|
|
|
|
kCFAllocatorDefault, (const struct sockaddr *)&zeroAddress);
|
|
|
|
|
Boolean success = SCNetworkReachabilitySetDispatchQueue(_reachabilityRef, _reachabilityQueue);
|
2019-08-08 11:34:52 -07:00
|
|
|
if (!success) {
|
2019-09-09 15:21:29 -07:00
|
|
|
GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@", @"The reachability queue wasn't set.");
|
2019-08-08 11:34:52 -07:00
|
|
|
}
|
2019-09-09 15:21:29 -07:00
|
|
|
success = SCNetworkReachabilitySetCallback(_reachabilityRef, GDTCORReachabilityCallback, NULL);
|
2019-08-08 11:34:52 -07:00
|
|
|
if (!success) {
|
2019-09-09 15:21:29 -07:00
|
|
|
GDTCORLogWarning(GDTCORMCWReachabilityFailed, @"%@",
|
|
|
|
|
@"The reachability callback wasn't set.");
|
2019-08-08 11:34:52 -07:00
|
|
|
}
|
2019-04-12 19:27:00 -07:00
|
|
|
|
|
|
|
|
// Get the initial set of flags.
|
|
|
|
|
dispatch_async(_reachabilityQueue, ^{
|
|
|
|
|
Boolean valid = SCNetworkReachabilityGetFlags(self->_reachabilityRef, &self->_flags);
|
|
|
|
|
if (!valid) {
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"%@", @"Determining reachability failed.");
|
2019-04-12 19:27:00 -07:00
|
|
|
self->_flags = 0;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-24 15:19:43 -07:00
|
|
|
#endif
|
2019-04-12 19:27:00 -07:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 15:19:43 -07:00
|
|
|
- (void)setCallbackFlags:(GDTCORNetworkReachabilityFlags)flags {
|
2019-04-12 19:27:00 -07:00
|
|
|
if (_callbackFlags != flags) {
|
|
|
|
|
self->_callbackFlags = flags;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
2020-04-24 15:19:43 -07:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-function"
|
|
|
|
|
static void GDTCORReachabilityCallback(GDTCORNetworkReachabilityRef reachability,
|
|
|
|
|
GDTCORNetworkReachabilityFlags flags,
|
2019-09-09 15:21:29 -07:00
|
|
|
void *info) {
|
2020-04-24 15:19:43 -07:00
|
|
|
#pragma clang diagnostic pop
|
2020-04-14 12:30:29 -07:00
|
|
|
GDTCORLogDebug(@"Reachability changed, new flags: %d", flags);
|
2019-09-09 15:21:29 -07:00
|
|
|
[[GDTCORReachability sharedInstance] setCallbackFlags:flags];
|
2019-04-12 19:27:00 -07:00
|
|
|
}
|