4 Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
8 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
9 ("Apple") in consideration of your agreement to the following terms, and your
10 use, installation, modification or redistribution of this Apple software
11 constitutes acceptance of these terms. If you do not agree with these terms,
12 please do not use, install, modify or redistribute this Apple software.
14 In consideration of your agreement to abide by the following terms, and subject
15 to these terms, Apple grants you a personal, non-exclusive license, under
16 Apple's copyrights in this original Apple software (the "Apple Software"), to
17 use, reproduce, modify and redistribute the Apple Software, with or without
18 modifications, in source and/or binary forms; provided that if you redistribute
19 the Apple Software in its entirety and without modifications, you must retain
20 this notice and the following text and disclaimers in all such redistributions
21 of the Apple Software.
22 Neither the name, trademarks, service marks or logos of Apple Inc. may be used
23 to endorse or promote products derived from the Apple Software without specific
24 prior written permission from Apple. Except as expressly stated in this notice,
25 no other rights or licenses, express or implied, are granted by Apple herein,
26 including but not limited to any patent rights that may be infringed by your
27 derivative works or by other works in which the Apple Software may be
30 The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
31 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
32 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
34 COMBINATION WITH YOUR PRODUCTS.
36 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
37 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
40 DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
41 CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
42 APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 Copyright (C) 2010 Apple Inc. All Rights Reserved.
48 #import <sys/socket.h>
49 #import <netinet/in.h>
50 #import <netinet6/in6.h>
55 #import <CoreFoundation/CoreFoundation.h>
57 #import "Reachability.h"
59 #define kShouldPrintReachabilityFlags 1
61 static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
63 #if kShouldPrintReachabilityFlags
65 NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
66 (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
67 (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
69 (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
70 (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
71 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
72 (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
73 (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
74 (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
75 (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',
82 @implementation Reachability
83 static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
85 #pragma unused (target, flags)
86 NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
87 NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");
89 //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
90 // in case someon uses the Reachablity object in a different thread.
91 NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init];
93 Reachability* noteObject = (Reachability*) info;
94 // Post a notification to notify the client that the network reachability changed.
95 [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
100 - (BOOL) startNotifier
103 SCNetworkReachabilityContext context = {0, self, NULL, NULL, NULL};
104 if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))
106 if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
114 - (void) stopNotifier
116 if(reachabilityRef!= NULL)
118 SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
125 if(reachabilityRef!= NULL)
127 CFRelease(reachabilityRef);
132 + (Reachability*) reachabilityWithHostName: (NSString*) hostName;
134 Reachability* retVal = NULL;
135 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
136 if(reachability!= NULL)
138 retVal= [[[self alloc] init] autorelease];
141 retVal->reachabilityRef = reachability;
142 retVal->localWiFiRef = NO;
148 + (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
150 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)hostAddress);
151 Reachability* retVal = NULL;
152 if(reachability!= NULL)
154 retVal= [[[self alloc] init] autorelease];
157 retVal->reachabilityRef = reachability;
158 retVal->localWiFiRef = NO;
164 + (Reachability*) reachabilityForInternetConnection;
166 struct sockaddr_in zeroAddress;
167 bzero(&zeroAddress, sizeof(zeroAddress));
168 zeroAddress.sin_len = sizeof(zeroAddress);
169 zeroAddress.sin_family = AF_INET;
170 return [self reachabilityWithAddress: &zeroAddress];
173 + (Reachability*) reachabilityForLocalWiFi;
175 struct sockaddr_in localWifiAddress;
176 bzero(&localWifiAddress, sizeof(localWifiAddress));
177 localWifiAddress.sin_len = sizeof(localWifiAddress);
178 localWifiAddress.sin_family = AF_INET;
179 // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0
180 localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM);
181 Reachability* retVal = [self reachabilityWithAddress: &localWifiAddress];
184 retVal->localWiFiRef = YES;
189 #pragma mark Network Flag Handling
191 - (NetworkStatus) localWiFiStatusForFlags: (SCNetworkReachabilityFlags) flags
193 PrintReachabilityFlags(flags, "localWiFiStatusForFlags");
195 NetworkStatus retVal = NotReachable;
196 if((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect))
198 retVal = ReachableViaWiFi;
203 - (NetworkStatus) networkStatusForFlags: (SCNetworkReachabilityFlags) flags
205 PrintReachabilityFlags(flags, "networkStatusForFlags");
206 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
208 // if target host is not reachable
212 NetworkStatus retVal = NotReachable;
214 if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
216 // if target host is reachable and no connection is required
217 // then we'll assume (for now) that your on Wi-Fi
218 retVal = ReachableViaWiFi;
222 if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
223 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
225 // ... and the connection is on-demand (or on-traffic) if the
226 // calling application is using the CFSocketStream or higher APIs
228 if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
230 // ... and no [user] intervention is needed
231 retVal = ReachableViaWiFi;
235 if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
237 // ... but WWAN connections are OK if the calling application
238 // is using the CFNetwork (CFSocketStream?) APIs.
239 retVal = ReachableViaWWAN;
244 - (BOOL) connectionRequired;
246 NSAssert(reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
247 SCNetworkReachabilityFlags flags;
248 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
250 return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
255 - (NetworkStatus) currentReachabilityStatus
257 NSAssert(reachabilityRef != NULL, @"currentNetworkStatus called with NULL reachabilityRef");
258 NetworkStatus retVal = NotReachable;
259 SCNetworkReachabilityFlags flags;
260 if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
264 retVal = [self localWiFiStatusForFlags: flags];
268 retVal = [self networkStatusForFlags: flags];