Reachability.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. Copyright (c) 2011, Tony Million.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  14. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  15. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  16. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  17. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  18. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  19. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  21. POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #import "Reachability.h"
  24. #import <sys/socket.h>
  25. #import <netinet/in.h>
  26. #import <netinet6/in6.h>
  27. #import <arpa/inet.h>
  28. #import <ifaddrs.h>
  29. #import <netdb.h>
  30. NSString *const kReachabilityChangedNotification = @"kReachabilityChangedNotification";
  31. @interface Reachability ()
  32. @property (nonatomic, assign) SCNetworkReachabilityRef reachabilityRef;
  33. @property (nonatomic, strong) dispatch_queue_t reachabilitySerialQueue;
  34. @property (nonatomic, strong) id reachabilityObject;
  35. -(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags;
  36. -(BOOL)isReachableWithFlags:(SCNetworkReachabilityFlags)flags;
  37. @end
  38. static NSString *reachabilityFlags(SCNetworkReachabilityFlags flags)
  39. {
  40. return [NSString stringWithFormat:@"%c%c %c%c%c%c%c%c%c",
  41. #if TARGET_OS_IPHONE
  42. (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
  43. #else
  44. 'X',
  45. #endif
  46. (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
  47. (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
  48. (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
  49. (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
  50. (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
  51. (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
  52. (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
  53. (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-'];
  54. }
  55. // Start listening for reachability notifications on the current run loop
  56. static void TMReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
  57. {
  58. #pragma unused (target)
  59. Reachability *reachability = ((__bridge Reachability*)info);
  60. // We probably don't need an autoreleasepool here, as GCD docs state each queue has its own autorelease pool,
  61. // but what the heck eh?
  62. @autoreleasepool
  63. {
  64. [reachability reachabilityChanged:flags];
  65. }
  66. }
  67. @implementation Reachability
  68. #pragma mark - Class Constructor Methods
  69. +(instancetype)reachabilityWithHostName:(NSString*)hostname
  70. {
  71. return [Reachability reachabilityWithHostname:hostname];
  72. }
  73. +(instancetype)reachabilityWithHostname:(NSString*)hostname
  74. {
  75. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithName(NULL, [hostname UTF8String]);
  76. if (ref)
  77. {
  78. id reachability = [[self alloc] initWithReachabilityRef:ref];
  79. return reachability;
  80. }
  81. return nil;
  82. }
  83. +(instancetype)reachabilityWithAddress:(void *)hostAddress
  84. {
  85. SCNetworkReachabilityRef ref = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
  86. if (ref)
  87. {
  88. id reachability = [[self alloc] initWithReachabilityRef:ref];
  89. return reachability;
  90. }
  91. return nil;
  92. }
  93. +(instancetype)reachabilityForInternetConnection
  94. {
  95. struct sockaddr_in zeroAddress;
  96. bzero(&zeroAddress, sizeof(zeroAddress));
  97. zeroAddress.sin_len = sizeof(zeroAddress);
  98. zeroAddress.sin_family = AF_INET;
  99. return [self reachabilityWithAddress:&zeroAddress];
  100. }
  101. +(instancetype)reachabilityForLocalWiFi
  102. {
  103. struct sockaddr_in localWifiAddress;
  104. bzero(&localWifiAddress, sizeof(localWifiAddress));
  105. localWifiAddress.sin_len = sizeof(localWifiAddress);
  106. localWifiAddress.sin_family = AF_INET;
  107. // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
  108. localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
  109. return [self reachabilityWithAddress:&localWifiAddress];
  110. }
  111. +(instancetype)reachabilityWithURL:(NSURL*)url
  112. {
  113. id reachability;
  114. NSString *host = url.host;
  115. BOOL isIpAddress = [self isIpAddress:host];
  116. if (isIpAddress)
  117. {
  118. NSNumber *port = url.port ?: [url.scheme isEqualToString:@"https"] ? @(443) : @(80);
  119. struct sockaddr_in address;
  120. address.sin_len = sizeof(address);
  121. address.sin_family = AF_INET;
  122. address.sin_port = htons([port intValue]);
  123. address.sin_addr.s_addr = inet_addr([host UTF8String]);
  124. reachability = [self reachabilityWithAddress:&address];
  125. }
  126. else
  127. {
  128. reachability = [self reachabilityWithHostname:host];
  129. }
  130. return reachability;
  131. }
  132. +(BOOL)isIpAddress:(NSString*)host
  133. {
  134. struct in_addr pin;
  135. return 1 == inet_aton([host UTF8String], &pin);
  136. }
  137. // Initialization methods
  138. -(instancetype)initWithReachabilityRef:(SCNetworkReachabilityRef)ref
  139. {
  140. self = [super init];
  141. if (self != nil)
  142. {
  143. self.reachableOnWWAN = YES;
  144. self.reachabilityRef = ref;
  145. // We need to create a serial queue.
  146. // We allocate this once for the lifetime of the notifier.
  147. self.reachabilitySerialQueue = dispatch_queue_create("com.tonymillion.reachability", NULL);
  148. }
  149. return self;
  150. }
  151. -(void)dealloc
  152. {
  153. [self stopNotifier];
  154. if(self.reachabilityRef)
  155. {
  156. CFRelease(self.reachabilityRef);
  157. self.reachabilityRef = nil;
  158. }
  159. self.reachableBlock = nil;
  160. self.unreachableBlock = nil;
  161. self.reachabilityBlock = nil;
  162. self.reachabilitySerialQueue = nil;
  163. }
  164. #pragma mark - Notifier Methods
  165. // Notifier
  166. // NOTE: This uses GCD to trigger the blocks - they *WILL NOT* be called on THE MAIN THREAD
  167. // - In other words DO NOT DO ANY UI UPDATES IN THE BLOCKS.
  168. // INSTEAD USE dispatch_async(dispatch_get_main_queue(), ^{UISTUFF}) (or dispatch_sync if you want)
  169. -(BOOL)startNotifier
  170. {
  171. // allow start notifier to be called multiple times
  172. if(self.reachabilityObject && (self.reachabilityObject == self))
  173. {
  174. return YES;
  175. }
  176. SCNetworkReachabilityContext context = { 0, NULL, NULL, NULL, NULL };
  177. context.info = (__bridge void *)self;
  178. if(SCNetworkReachabilitySetCallback(self.reachabilityRef, TMReachabilityCallback, &context))
  179. {
  180. // Set it as our reachability queue, which will retain the queue
  181. if(SCNetworkReachabilitySetDispatchQueue(self.reachabilityRef, self.reachabilitySerialQueue))
  182. {
  183. // this should do a retain on ourself, so as long as we're in notifier mode we shouldn't disappear out from under ourselves
  184. // woah
  185. self.reachabilityObject = self;
  186. return YES;
  187. }
  188. else
  189. {
  190. #ifdef DEBUG
  191. NSLog(@"SCNetworkReachabilitySetDispatchQueue() failed: %s", SCErrorString(SCError()));
  192. #endif
  193. // UH OH - FAILURE - stop any callbacks!
  194. SCNetworkReachabilitySetCallback(self.reachabilityRef, NULL, NULL);
  195. }
  196. }
  197. else
  198. {
  199. #ifdef DEBUG
  200. NSLog(@"SCNetworkReachabilitySetCallback() failed: %s", SCErrorString(SCError()));
  201. #endif
  202. }
  203. // if we get here we fail at the internet
  204. self.reachabilityObject = nil;
  205. return NO;
  206. }
  207. -(void)stopNotifier
  208. {
  209. // First stop, any callbacks!
  210. SCNetworkReachabilitySetCallback(self.reachabilityRef, NULL, NULL);
  211. // Unregister target from the GCD serial dispatch queue.
  212. SCNetworkReachabilitySetDispatchQueue(self.reachabilityRef, NULL);
  213. self.reachabilityObject = nil;
  214. }
  215. #pragma mark - reachability tests
  216. // This is for the case where you flick the airplane mode;
  217. // you end up getting something like this:
  218. //Reachability: WR ct-----
  219. //Reachability: -- -------
  220. //Reachability: WR ct-----
  221. //Reachability: -- -------
  222. // We treat this as 4 UNREACHABLE triggers - really apple should do better than this
  223. #define testcase (kSCNetworkReachabilityFlagsConnectionRequired | kSCNetworkReachabilityFlagsTransientConnection)
  224. -(BOOL)isReachableWithFlags:(SCNetworkReachabilityFlags)flags
  225. {
  226. BOOL connectionUP = YES;
  227. if(!(flags & kSCNetworkReachabilityFlagsReachable))
  228. connectionUP = NO;
  229. if( (flags & testcase) == testcase )
  230. connectionUP = NO;
  231. #if TARGET_OS_IPHONE
  232. if(flags & kSCNetworkReachabilityFlagsIsWWAN)
  233. {
  234. // We're on 3G.
  235. if(!self.reachableOnWWAN)
  236. {
  237. // We don't want to connect when on 3G.
  238. connectionUP = NO;
  239. }
  240. }
  241. #endif
  242. return connectionUP;
  243. }
  244. -(BOOL)isReachable
  245. {
  246. SCNetworkReachabilityFlags flags;
  247. if(!SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  248. return NO;
  249. return [self isReachableWithFlags:flags];
  250. }
  251. -(BOOL)isReachableViaWWAN
  252. {
  253. #if TARGET_OS_IPHONE
  254. SCNetworkReachabilityFlags flags = 0;
  255. if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  256. {
  257. // Check we're REACHABLE
  258. if(flags & kSCNetworkReachabilityFlagsReachable)
  259. {
  260. // Now, check we're on WWAN
  261. if(flags & kSCNetworkReachabilityFlagsIsWWAN)
  262. {
  263. return YES;
  264. }
  265. }
  266. }
  267. #endif
  268. return NO;
  269. }
  270. -(BOOL)isReachableViaWiFi
  271. {
  272. SCNetworkReachabilityFlags flags = 0;
  273. if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  274. {
  275. // Check we're reachable
  276. if((flags & kSCNetworkReachabilityFlagsReachable))
  277. {
  278. #if TARGET_OS_IPHONE
  279. // Check we're NOT on WWAN
  280. if((flags & kSCNetworkReachabilityFlagsIsWWAN))
  281. {
  282. return NO;
  283. }
  284. #endif
  285. return YES;
  286. }
  287. }
  288. return NO;
  289. }
  290. // WWAN may be available, but not active until a connection has been established.
  291. // WiFi may require a connection for VPN on Demand.
  292. -(BOOL)isConnectionRequired
  293. {
  294. return [self connectionRequired];
  295. }
  296. -(BOOL)connectionRequired
  297. {
  298. SCNetworkReachabilityFlags flags;
  299. if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  300. {
  301. return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
  302. }
  303. return NO;
  304. }
  305. // Dynamic, on demand connection?
  306. -(BOOL)isConnectionOnDemand
  307. {
  308. SCNetworkReachabilityFlags flags;
  309. if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  310. {
  311. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  312. (flags & (kSCNetworkReachabilityFlagsConnectionOnTraffic | kSCNetworkReachabilityFlagsConnectionOnDemand)));
  313. }
  314. return NO;
  315. }
  316. // Is user intervention required?
  317. -(BOOL)isInterventionRequired
  318. {
  319. SCNetworkReachabilityFlags flags;
  320. if (SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  321. {
  322. return ((flags & kSCNetworkReachabilityFlagsConnectionRequired) &&
  323. (flags & kSCNetworkReachabilityFlagsInterventionRequired));
  324. }
  325. return NO;
  326. }
  327. #pragma mark - reachability status stuff
  328. -(NetworkStatus)currentReachabilityStatus
  329. {
  330. if([self isReachable])
  331. {
  332. if([self isReachableViaWiFi])
  333. return ReachableViaWiFi;
  334. #if TARGET_OS_IPHONE
  335. return ReachableViaWWAN;
  336. #endif
  337. }
  338. return NotReachable;
  339. }
  340. -(SCNetworkReachabilityFlags)reachabilityFlags
  341. {
  342. SCNetworkReachabilityFlags flags = 0;
  343. if(SCNetworkReachabilityGetFlags(self.reachabilityRef, &flags))
  344. {
  345. return flags;
  346. }
  347. return 0;
  348. }
  349. -(NSString*)currentReachabilityString
  350. {
  351. NetworkStatus temp = [self currentReachabilityStatus];
  352. if(temp == ReachableViaWWAN)
  353. {
  354. // Updated for the fact that we have CDMA phones now!
  355. return NSLocalizedString(@"Cellular", @"");
  356. }
  357. if (temp == ReachableViaWiFi)
  358. {
  359. return NSLocalizedString(@"WiFi", @"");
  360. }
  361. return NSLocalizedString(@"No Connection", @"");
  362. }
  363. -(NSString*)currentReachabilityFlags
  364. {
  365. return reachabilityFlags([self reachabilityFlags]);
  366. }
  367. #pragma mark - Callback function calls this method
  368. -(void)reachabilityChanged:(SCNetworkReachabilityFlags)flags
  369. {
  370. if([self isReachableWithFlags:flags])
  371. {
  372. if(self.reachableBlock)
  373. {
  374. self.reachableBlock(self);
  375. }
  376. }
  377. else
  378. {
  379. if(self.unreachableBlock)
  380. {
  381. self.unreachableBlock(self);
  382. }
  383. }
  384. if(self.reachabilityBlock)
  385. {
  386. self.reachabilityBlock(self, flags);
  387. }
  388. // this makes sure the change notification happens on the MAIN THREAD
  389. dispatch_async(dispatch_get_main_queue(), ^{
  390. [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification
  391. object:self];
  392. });
  393. }
  394. #pragma mark - Debug Description
  395. - (NSString *) description
  396. {
  397. NSString *description = [NSString stringWithFormat:@"<%@: %p (%@)>",
  398. NSStringFromClass([self class]), self, [self currentReachabilityFlags]];
  399. return description;
  400. }
  401. @end