CB-11230 CB-11505 iOS: Add compatibility with IPv6

Remove the logic associated with the local wi-fi because of the narrow use case for this API and the large potential for misuse

See also docs https://developer.apple.com/library/content/samplecode/Reachability/Listings/ReadMe_md.html#//apple_ref/doc/uid/DTS40007324-ReadMe_md-DontLinkElementID_11

 This closes #51
This commit is contained in:
Nikita Matrosov 2016-11-14 17:05:21 +03:00 committed by Vladimir Kotikov
parent f4dc63cb76
commit c911ef883c
2 changed files with 7 additions and 41 deletions

View File

@ -57,7 +57,6 @@ typedef enum {
@interface CDVReachability : NSObject @interface CDVReachability : NSObject
{ {
BOOL localWiFiRef;
SCNetworkReachabilityRef reachabilityRef; SCNetworkReachabilityRef reachabilityRef;
} }
@ -65,15 +64,12 @@ typedef enum {
+ (CDVReachability*)reachabilityWithHostName:(NSString*)hostName; + (CDVReachability*)reachabilityWithHostName:(NSString*)hostName;
// reachabilityWithAddress- Use to check the reachability of a particular IP address. // reachabilityWithAddress- Use to check the reachability of a particular IP address.
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr_in*)hostAddress; + (CDVReachability*)reachabilityWithAddress:(const struct sockaddr*)hostAddress;
// reachabilityForInternetConnection- checks whether the default route is available. // reachabilityForInternetConnection- checks whether the default route is available.
// Should be used by applications that do not connect to a particular host // Should be used by applications that do not connect to a particular host
+ (CDVReachability*)reachabilityForInternetConnection; + (CDVReachability*)reachabilityForInternetConnection;
// reachabilityForLocalWiFi- checks whether a local wifi connection is available.
+ (CDVReachability*)reachabilityForLocalWiFi;
// Start listening for reachability notifications on the current run loop // Start listening for reachability notifications on the current run loop
- (BOOL)startNotifier; - (BOOL)startNotifier;
- (void)stopNotifier; - (void)stopNotifier;

View File

@ -140,7 +140,6 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
retVal = [[self alloc] init]; retVal = [[self alloc] init];
if (retVal != NULL) { if (retVal != NULL) {
retVal->reachabilityRef = reachability; retVal->reachabilityRef = reachability;
retVal->localWiFiRef = NO;
} }
else { else {
CFRelease(reachability); CFRelease(reachability);
@ -149,15 +148,14 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
return retVal; return retVal;
} }
+ (CDVReachability*)reachabilityWithAddress:(const struct sockaddr_in*)hostAddress; + (CDVReachability*)reachabilityWithAddress:(const struct sockaddr*)hostAddress;
{ {
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress); SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress);
CDVReachability* retVal = NULL; CDVReachability* retVal = NULL;
if (reachability != NULL) { if (reachability != NULL) {
retVal = [[self alloc] init]; retVal = [[self alloc] init];
if (retVal != NULL) { if (retVal != NULL) {
retVal->reachabilityRef = reachability; retVal->reachabilityRef = reachability;
retVal->localWiFiRef = NO;
} }
else { else {
CFRelease(reachability); CFRelease(reachability);
@ -166,43 +164,19 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
return retVal; return retVal;
} }
// Reachability treats the 0.0.0.0 address as a special token that causes it to monitor the general routing
// status of the device, both IPv4 and IPv6.
+ (CDVReachability*)reachabilityForInternetConnection; + (CDVReachability*)reachabilityForInternetConnection;
{ {
struct sockaddr_in zeroAddress; struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress)); bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress); zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET; zeroAddress.sin_family = AF_INET;
return [self reachabilityWithAddress:&zeroAddress]; return [self reachabilityWithAddress:(const struct sockaddr*) &zeroAddress];
}
+ (CDVReachability*)reachabilityForLocalWiFi;
{
struct sockaddr_in localWifiAddress;
bzero(&localWifiAddress, sizeof(localWifiAddress));
localWifiAddress.sin_len = sizeof(localWifiAddress);
localWifiAddress.sin_family = AF_INET;
// IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
CDVReachability* retVal = [self reachabilityWithAddress:&localWifiAddress];
if (retVal != NULL) {
retVal->localWiFiRef = YES;
}
return retVal;
} }
#pragma mark Network Flag Handling #pragma mark Network Flag Handling
- (NetworkStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags
{
CDVPrintReachabilityFlags(flags, "localWiFiStatusForFlags");
BOOL retVal = NotReachable;
if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) {
retVal = ReachableViaWiFi;
}
return retVal;
}
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{ {
CDVPrintReachabilityFlags(flags, "networkStatusForFlags"); CDVPrintReachabilityFlags(flags, "networkStatusForFlags");
@ -254,11 +228,7 @@ static void CDVReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkRe
NetworkStatus retVal = NotReachable; NetworkStatus retVal = NotReachable;
SCNetworkReachabilityFlags flags; SCNetworkReachabilityFlags flags;
if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) { if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags)) {
if (localWiFiRef) { retVal = [self networkStatusForFlags:flags];
retVal = [self localWiFiStatusForFlags:flags];
} else {
retVal = [self networkStatusForFlags:flags];
}
} }
return retVal; return retVal;
} }