Debian Codegraph @

Xpjproject/pjsip-apps/src/pjsua/ios/ipjsua/Reachability.m

raw
1 /*
2 Copyright (C) 2016 Apple Inc. All Rights Reserved.
3 See LICENSE.txt for this sample’s licensing information
4
5 Abstract:
6 Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
7 */
8
9 #import <arpa/inet.h>
10 #import <ifaddrs.h>
11 #import <netdb.h>
12 #import <sys/socket.h>
13 #import <netinet/in.h>
14
15 #import <CoreFoundation/CoreFoundation.h>
16
17 #import "Reachability.h"
18
19 #pragma mark IPv6 Support
20 //Reachability fully support IPv6. For full details, see ReadMe.md.
21
22
23 NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification";
24
25
26 #pragma mark - Supporting functions
27
28 #define kShouldPrintReachabilityFlags 1
29
30 static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment)
31 {
32 #if kShouldPrintReachabilityFlags
33
34 NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n",
35 (flags & kSCNetworkReachabilityFlagsIsWWAN) ? 'W' : '-',
36 (flags & kSCNetworkReachabilityFlagsReachable) ? 'R' : '-',
37
38 (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-',
39 (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-',
40 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-',
41 (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-',
42 (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-',
43 (flags & kSCNetworkReachabilityFlagsIsLocalAddress) ? 'l' : '-',
44 (flags & kSCNetworkReachabilityFlagsIsDirect) ? 'd' : '-',
45 comment
46 );
47 #endif
48 }
49
50
51 static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
52 {
53 #pragma unused (target, flags)
54 NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
55 NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");
56
57 Reachability* noteObject = (__bridge Reachability *)info;
58 // Post a notification to notify the client that the network reachability changed.
59 [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
60 }
61
62
63 #pragma mark - Reachability implementation
64
65 @implementation Reachability
66 {
67 SCNetworkReachabilityRef _reachabilityRef;
68 }
69
70 + (instancetype)reachabilityWithHostName:(NSString *)hostName
71 {
72 Reachability* returnValue = NULL;
73 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]);
74 if (reachability != NULL)
75 {
76 returnValue= [[self alloc] init];
77 if (returnValue != NULL)
78 {
79 returnValue->_reachabilityRef = reachability;
80 }
81 else {
82 CFRelease(reachability);
83 }
84 }
85 return returnValue;
86 }
87
88
89 + (instancetype)reachabilityWithAddress:(const struct sockaddr *)hostAddress
90 {
91 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, hostAddress);
92
93 Reachability* returnValue = NULL;
94
95 if (reachability != NULL)
96 {
97 returnValue = [[self alloc] init];
98 if (returnValue != NULL)
99 {
100 returnValue->_reachabilityRef = reachability;
101 }
102 else {
103 CFRelease(reachability);
104 }
105 }
106 return returnValue;
107 }
108
109
110 + (instancetype)reachabilityForInternetConnection
111 {
112 struct sockaddr_in zeroAddress;
113 bzero(&zeroAddress, sizeof(zeroAddress));
114 zeroAddress.sin_len = sizeof(zeroAddress);
115 zeroAddress.sin_family = AF_INET;
116
117 return [self reachabilityWithAddress: (const struct sockaddr *) &zeroAddress];
118 }
119
120 #pragma mark reachabilityForLocalWiFi
121 //reachabilityForLocalWiFi has been removed from the sample. See ReadMe.md for more information.
122 //+ (instancetype)reachabilityForLocalWiFi
123
124
125
126 #pragma mark - Start and stop notifier
127
128 - (BOOL)startNotifier
129 {
130 BOOL returnValue = NO;
131 SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
132
133 if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
134 {
135 if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
136 {
137 returnValue = YES;
138 }
139 }
140
141 return returnValue;
142 }
143
144
145 - (void)stopNotifier
146 {
147 if (_reachabilityRef != NULL)
148 {
149 SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
150 }
151 }
152
153
154 - (void)dealloc
155 {
156 [self stopNotifier];
157 if (_reachabilityRef != NULL)
158 {
159 CFRelease(_reachabilityRef);
160 }
161 }
162
163
164 #pragma mark - Network Flag Handling
165
166 - (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
167 {
168 PrintReachabilityFlags(flags, "networkStatusForFlags");
169 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
170 {
171 // The target host is not reachable.
172 return NotReachable;
173 }
174
175 NetworkStatus returnValue = NotReachable;
176
177 if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
178 {
179 /*
180 If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi...
181 */
182 returnValue = ReachableViaWiFi;
183 }
184
185 if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
186 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
187 {
188 /*
189 ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs...
190 */
191
192 if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
193 {
194 /*
195 ... and no [user] intervention is needed...
196 */
197 returnValue = ReachableViaWiFi;
198 }
199 }
200
201 if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
202 {
203 /*
204 ... but WWAN connections are OK if the calling application is using the CFNetwork APIs.
205 */
206 returnValue = ReachableViaWWAN;
207 }
208
209 return returnValue;
210 }
211
212
213 - (BOOL)connectionRequired
214 {
215 NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef");
216 SCNetworkReachabilityFlags flags;
217
218 if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
219 {
220 return (flags & kSCNetworkReachabilityFlagsConnectionRequired);
221 }
222
223 return NO;
224 }
225
226
227 - (NetworkStatus)currentReachabilityStatus
228 {
229 NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef");
230 NetworkStatus returnValue = NotReachable;
231 SCNetworkReachabilityFlags flags;
232
233 if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags))
234 {
235 returnValue = [self networkStatusForFlags:flags];
236 }
237
238 return returnValue;
239 }
240
241
242 @end