PageRenderTime 741ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/External/Reachability/Reachability.m

https://gitlab.com/intelij/ChattAR-ios
Objective C | 287 lines | 193 code | 36 blank | 58 comment | 30 complexity | 61ccbf3c0f0e5fa903d18e3739491cee MD5 | raw file
  1. /*
  2. File: Reachability.m
  3. Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  4. Version: 2.2
  5. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  6. ("Apple") in consideration of your agreement to the following terms, and your
  7. use, installation, modification or redistribution of this Apple software
  8. constitutes acceptance of these terms. If you do not agree with these terms,
  9. please do not use, install, modify or redistribute this Apple software.
  10. In consideration of your agreement to abide by the following terms, and subject
  11. to these terms, Apple grants you a personal, non-exclusive license, under
  12. Apple's copyrights in this original Apple software (the "Apple Software"), to
  13. use, reproduce, modify and redistribute the Apple Software, with or without
  14. modifications, in source and/or binary forms; provided that if you redistribute
  15. the Apple Software in its entirety and without modifications, you must retain
  16. this notice and the following text and disclaimers in all such redistributions
  17. of the Apple Software.
  18. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  19. to endorse or promote products derived from the Apple Software without specific
  20. prior written permission from Apple. Except as expressly stated in this notice,
  21. no other rights or licenses, express or implied, are granted by Apple herein,
  22. including but not limited to any patent rights that may be infringed by your
  23. derivative works or by other works in which the Apple Software may be
  24. incorporated.
  25. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  26. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  27. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  29. COMBINATION WITH YOUR PRODUCTS.
  30. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  31. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  34. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  35. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  36. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. Copyright (C) 2010 Apple Inc. All Rights Reserved.
  38. */
  39. #import <sys/socket.h>
  40. #import <netinet/in.h>
  41. #import <netinet6/in6.h>
  42. #import <arpa/inet.h>
  43. #import <ifaddrs.h>
  44. #import <netdb.h>
  45. #import <CoreFoundation/CoreFoundation.h>
  46. #import "Reachability.h"
  47. #define kShouldPrintReachabilityFlags 1
  48. static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
  49. {
  50. #if kShouldPrintReachabilityFlags
  51. NSLog(@"QBReachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
  52. (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  53. (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  54. (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  55. (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  56. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
  57. (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  58. (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
  59. (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  60. (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',
  61. comment
  62. );
  63. #endif
  64. }
  65. @implementation Reachability
  66. static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
  67. {
  68. #pragma unused (target, flags)
  69. NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
  70. NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");
  71. //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
  72. // in case someon uses the Reachablity object in a different thread.
  73. NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init];
  74. Reachability* noteObject = (Reachability*) info;
  75. // Post a notification to notify the client that the network reachability changed.
  76. [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
  77. [myPool release];
  78. }
  79. - (BOOL) startNotifier
  80. {
  81. BOOL retVal = NO;
  82. SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
  83. if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))
  84. {
  85. if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
  86. {
  87. retVal = YES;
  88. }
  89. }
  90. return retVal;
  91. }
  92. - (void) stopNotifier
  93. {
  94. if(reachabilityRef!= NULL)
  95. {
  96. SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  97. }
  98. }
  99. - (void) dealloc
  100. {
  101. [self stopNotifier];
  102. if(reachabilityRef!= NULL)
  103. {
  104. CFRelease(reachabilityRef);
  105. }
  106. [super dealloc];
  107. }
  108. + (Reachability*) reachabilityWithHostName: (NSString*) hostName;
  109. {
  110. Reachability* retVal = NULL;
  111. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
  112. if(reachability!= NULL)
  113. {
  114. retVal= [[[self alloc] init] autorelease];
  115. if(retVal!= NULL)
  116. {
  117. retVal->reachabilityRef = reachability;
  118. retVal->localWiFiRef = NO;
  119. }
  120. }
  121. return retVal;
  122. }
  123. + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
  124. {
  125. SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
  126. Reachability* retVal = NULL;
  127. if(reachability!= NULL)
  128. {
  129. retVal= [[[self alloc] init] autorelease];
  130. if(retVal!= NULL)
  131. {
  132. retVal->reachabilityRef = reachability;
  133. retVal->localWiFiRef = NO;
  134. }
  135. }
  136. return retVal;
  137. }
  138. + (Reachability*) reachabilityForInternetConnection;
  139. {
  140. struct sockaddr_in zeroAddress;
  141. bzero(&zeroAddress, sizeof(zeroAddress));
  142. zeroAddress.sin_len = sizeof(zeroAddress);
  143. zeroAddress.sin_family = AF_INET;
  144. return [self reachabilityWithAddress: &zeroAddress];
  145. }
  146. + (Reachability*) reachabilityForLocalWiFi;
  147. {
  148. struct sockaddr_in localWifiAddress;
  149. bzero(&localWifiAddress, sizeof(localWifiAddress));
  150. localWifiAddress.sin_len = sizeof(localWifiAddress);
  151. localWifiAddress.sin_family = AF_INET;
  152. // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
  153. localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  154. Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress];
  155. if(retVal!= NULL)
  156. {
  157. retVal->localWiFiRef = YES;
  158. }
  159. return retVal;
  160. }
  161. #pragma mark Network Flag Handling
  162. - (QBNetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags
  163. {
  164. PrintReachabilityFlags(flags, "localWiFiStatusForFlags");
  165. BOOL retVal = NotReachable;
  166. if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
  167. {
  168. retVal = ReachableViaWiFi;
  169. }
  170. return retVal;
  171. }
  172. - (QBNetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
  173. {
  174. PrintReachabilityFlags(flags, "networkStatusForFlags");
  175. if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
  176. {
  177. // if target host is not reachable
  178. return NotReachable;
  179. }
  180. BOOL retVal = NotReachable;
  181. if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
  182. {
  183. // if target host is reachable and no connection is required
  184. // then we'll assume (for now) that your on Wi-Fi
  185. retVal = ReachableViaWiFi;
  186. }
  187. if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
  188. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
  189. {
  190. // ... and the connection is on-demand (or on-traffic) if the
  191. // calling application is using the CFSocketStream or higher APIs
  192. if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
  193. {
  194. // ... and no [user] intervention is needed
  195. retVal = ReachableViaWiFi;
  196. }
  197. }
  198. if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
  199. {
  200. // ... but WWAN connections are OK if the calling application
  201. // is using the CFNetwork (CFSocketStream?) APIs.
  202. retVal = ReachableViaWWAN;
  203. }
  204. return retVal;
  205. }
  206. - (BOOL) connectionRequired;
  207. {
  208. NSAssert(reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
  209. SCNetworkReachabilityFlags flags;
  210. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
  211. {
  212. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  213. }
  214. return NO;
  215. }
  216. - (QBNetworkStatus) currentReachabilityStatus
  217. {
  218. NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef");
  219. QBNetworkStatus retVal = NotReachable;
  220. SCNetworkReachabilityFlags flags;
  221. if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
  222. {
  223. if(localWiFiRef)
  224. {
  225. retVal = [self localWiFiStatusForFlags: flags];
  226. }
  227. else
  228. {
  229. retVal = [self networkStatusForFlags: flags];
  230. }
  231. }
  232. return retVal;
  233. }
  234. #pragma mark Check Internet connect
  235. + (BOOL)internetConnected {
  236. Reachability* reachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
  237. QBNetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];
  238. if(remoteHostStatus == NotReachable) {
  239. return NO;
  240. }
  241. return YES;
  242. }
  243. @end