Reachability.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. File: Reachability.h
  3. Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  4. Version: 2.0.4ddg
  5. */
  6. /*
  7. Significant additions made by Andrew W. Donoho, August 11, 2009.
  8. This is a derived work of Apple's Reachability v2.0 class.
  9. The below license is the new BSD license with the OSI recommended personalizations.
  10. <http://www.opensource.org/licenses/bsd-license.php>
  11. Extensions Copyright (C) 2009 Donoho Design Group, LLC. All Rights Reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are
  14. met:
  15. * Redistributions of source code must retain the above copyright notice,
  16. this list of conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright
  18. notice, this list of conditions and the following disclaimer in the
  19. documentation and/or other materials provided with the distribution.
  20. * Neither the name of Andrew W. Donoho nor Donoho Design Group, L.L.C.
  21. may be used to endorse or promote products derived from this software
  22. without specific prior written permission.
  23. THIS SOFTWARE IS PROVIDED BY DONOHO DESIGN GROUP, L.L.C. "AS IS" AND ANY
  24. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  26. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. Apple's Original License on Reachability v2.0
  37. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  38. ("Apple") in consideration of your agreement to the following terms, and your
  39. use, installation, modification or redistribution of this Apple software
  40. constitutes acceptance of these terms. If you do not agree with these terms,
  41. please do not use, install, modify or redistribute this Apple software.
  42. In consideration of your agreement to abide by the following terms, and subject
  43. to these terms, Apple grants you a personal, non-exclusive license, under
  44. Apple's copyrights in this original Apple software (the "Apple Software"), to
  45. use, reproduce, modify and redistribute the Apple Software, with or without
  46. modifications, in source and/or binary forms; provided that if you redistribute
  47. the Apple Software in its entirety and without modifications, you must retain
  48. this notice and the following text and disclaimers in all such redistributions
  49. of the Apple Software.
  50. Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  51. to endorse or promote products derived from the Apple Software without specific
  52. prior written permission from Apple. Except as expressly stated in this notice,
  53. no other rights or licenses, express or implied, are granted by Apple herein,
  54. including but not limited to any patent rights that may be infringed by your
  55. derivative works or by other works in which the Apple Software may be
  56. incorporated.
  57. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  58. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  59. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  60. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  61. COMBINATION WITH YOUR PRODUCTS.
  62. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  63. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  64. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  66. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  67. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  68. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. Copyright (C) 2009 Apple Inc. All Rights Reserved.
  70. */
  71. /*
  72. DDG extensions include:
  73. Each reachability object now has a copy of the key used to store it in a
  74. dictionary. This allows each observer to quickly determine if the event is
  75. important to them.
  76. -currentReachabilityStatus also has a significantly different decision criteria than
  77. Apple's code.
  78. A multiple convenience test methods have been added.
  79. */
  80. #import <Foundation/Foundation.h>
  81. #import <SystemConfiguration/SystemConfiguration.h>
  82. #import <netinet/in.h>
  83. #define USE_DDG_EXTENSIONS 1 // Use DDG's Extensions to test network criteria.
  84. // Since NSAssert and NSCAssert are used in this code,
  85. // I recommend you set NS_BLOCK_ASSERTIONS=1 in the release versions of your projects.
  86. enum {
  87. // DDG NetworkStatus Constant Names.
  88. kNotReachable = 0, // Apple's code depends upon 'NotReachable' being the same value as 'NO'.
  89. kReachableViaWWAN, // Switched order from Apple's enum. WWAN is active before WiFi.
  90. kReachableViaWiFi
  91. };
  92. typedef uint32_t NetworkStatus;
  93. enum {
  94. // Apple NetworkStatus Constant Names.
  95. NotReachable = kNotReachable,
  96. ReachableViaWiFi = kReachableViaWiFi,
  97. ReachableViaWWAN = kReachableViaWWAN
  98. };
  99. extern NSString *const kInternetConnection;
  100. extern NSString *const kLocalWiFiConnection;
  101. extern NSString *const kReachabilityChangedNotification;
  102. @interface Reachability: NSObject {
  103. @private
  104. NSString *key_;
  105. SCNetworkReachabilityRef reachabilityRef;
  106. }
  107. @property (copy) NSString *key; // Atomic because network operations are asynchronous.
  108. // Designated Initializer.
  109. - (Reachability *) initWithReachabilityRef: (SCNetworkReachabilityRef) ref;
  110. // Use to check the reachability of a particular host name.
  111. + (Reachability *) reachabilityWithHostName: (NSString*) hostName;
  112. // Use to check the reachability of a particular IP address.
  113. + (Reachability *) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
  114. // Use to check whether the default route is available.
  115. // Should be used to, at minimum, establish network connectivity.
  116. + (Reachability *) reachabilityForInternetConnection;
  117. // Use to check whether a local wifi connection is available.
  118. + (Reachability *) reachabilityForLocalWiFi;
  119. //Start listening for reachability notifications on the current run loop.
  120. - (BOOL) startNotifier;
  121. - (void) stopNotifier;
  122. // Comparison routines to enable choosing actions in a notification.
  123. - (BOOL) isEqual: (Reachability *) r;
  124. // These are the status tests.
  125. - (NetworkStatus) currentReachabilityStatus;
  126. // The main direct test of reachability.
  127. - (BOOL) isReachable;
  128. // WWAN may be available, but not active until a connection has been established.
  129. // WiFi may require a connection for VPN on Demand.
  130. - (BOOL) isConnectionRequired; // Identical DDG variant.
  131. - (BOOL) connectionRequired; // Apple's routine.
  132. // Dynamic, on demand connection?
  133. - (BOOL) isConnectionOnDemand;
  134. // Is user intervention required?
  135. - (BOOL) isInterventionRequired;
  136. // Routines for specific connection testing by your app.
  137. - (BOOL) isReachableViaWWAN;
  138. - (BOOL) isReachableViaWiFi;
  139. - (SCNetworkReachabilityFlags) reachabilityFlags;
  140. @end