2 * Copyright (c) 2003-2005, 2008-2010, 2015-2016, 2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _SCNETWORKREACHABILITY_H
25 #define _SCNETWORKREACHABILITY_H
27 #include <os/availability.h>
28 #include <TargetConditionals.h>
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <CoreFoundation/CoreFoundation.h>
33 #include <SystemConfiguration/SCNetwork.h>
34 #include <dispatch/dispatch.h>
36 CF_IMPLICIT_BRIDGING_ENABLED
37 CF_ASSUME_NONNULL_BEGIN
40 @header SCNetworkReachability
41 @discussion The SCNetworkReachability API allows an application to
42 determine the status of a system's current network
43 configuration and the reachability of a target host.
44 In addition, reachability can be monitored with notifications
45 that are sent when the status has changed.
47 "Reachability" reflects whether a data packet, sent by
48 an application into the network stack, can leave the local
50 Note that reachability does <i>not</i> guarantee that the data
51 packet will actually be received by the host.
53 When reachability is used without scheduling updates on a runloop
54 or dispatch queue, the system will not generate DNS traffic and
55 will be optimistic about its reply - it will assume that the target
56 is reachable if there is a default route but a DNS query is
57 needed to learn the address. When scheduled, the first callback
58 will behave like an unscheduled call but subsequent calls will
61 When used on IPv6-only (NAT64+DNS64) networks, reachability checks
62 for IPv4 address literals (either a struct sockaddr_in * or textual
63 representations such as "192.0.2.1") will automatically give the
64 result for the corresponding synthesized IPv6 address.
65 On those networks, creating a CFSocketStream or NSURLSession
66 to that address will send packets but trying to connect using
67 BSD socket APIs will fail.
71 @typedef SCNetworkReachabilityRef
72 @discussion This is the handle to a network address or name.
74 typedef const struct CF_BRIDGED_TYPE(id
) __SCNetworkReachability
* SCNetworkReachabilityRef
;
78 @typedef SCNetworkReachabilityContext
79 Structure containing user-specified data and callbacks for SCNetworkReachability.
80 @field version The version number of the structure type being passed
81 in as a parameter to the SCDynamicStore creation function.
82 This structure is version 0.
83 @field info A C pointer to a user-specified block of data.
84 @field retain The callback used to add a retain for the info field.
85 If this parameter is not a pointer to a function of the correct
86 prototype, the behavior is undefined. The value may be NULL.
87 @field release The calllback used to remove a retain previously added
88 for the info field. If this parameter is not a pointer to a
89 function of the correct prototype, the behavior is undefined.
90 The value may be NULL.
91 @field copyDescription The callback used to provide a description of
96 void * __nullable info
;
97 const void * __nonnull (* __nullable retain
)(const void *info
);
98 void (* __nullable release
)(const void *info
);
99 CFStringRef
__nonnull (* __nullable copyDescription
)(const void *info
);
100 } SCNetworkReachabilityContext
;
103 @enum SCNetworkReachabilityFlags
104 @discussion Flags that indicate whether the specified network
105 nodename or address is reachable, whether a connection is
106 required, and whether some user intervention may be required
107 when establishing a connection.
108 @constant kSCNetworkReachabilityFlagsTransientConnection
109 This flag indicates that the specified nodename or address can
110 be reached via a transient connection, such as PPP.
111 @constant kSCNetworkReachabilityFlagsReachable
112 This flag indicates that the specified nodename or address can
113 be reached using the current network configuration.
114 @constant kSCNetworkReachabilityFlagsConnectionRequired
115 This flag indicates that the specified nodename or address can
116 be reached using the current network configuration, but a
117 connection must first be established.
119 As an example, this status would be returned for a dialup
120 connection that was not currently active, but could handle
121 network traffic for the target system.
122 @constant kSCNetworkReachabilityFlagsConnectionOnTraffic
123 This flag indicates that the specified nodename or address can
124 be reached using the current network configuration, but a
125 connection must first be established. Any traffic directed
126 to the specified name or address will initiate the connection.
128 Note: this flag was previously named kSCNetworkReachabilityFlagsConnectionAutomatic
129 @constant kSCNetworkReachabilityFlagsInterventionRequired
130 This flag indicates that the specified nodename or address can
131 be reached using the current network configuration, but a
132 connection must first be established. In addition, some
133 form of user intervention will be required to establish this
134 connection, such as providing a password, an authentication
137 Note: At the present time, this flag will only be returned
138 in the case where you have a dial-on-traffic configuration
139 (ConnectionOnTraffic), where an attempt to connect has
140 already been made, and where some error (e.g. no dial tone,
141 no answer, bad password, ...) was encountered during the
142 automatic connection attempt. In this case the PPP controller
143 will stop attempting to establish a connection until the user
145 @constant kSCNetworkReachabilityFlagsConnectionOnDemand
146 This flag indicates that the specified nodename or address can
147 be reached using the current network configuration, but a
148 connection must first be established.
149 The connection will be established "On Demand" by the
151 Other APIs will not establish the connection.
152 @constant kSCNetworkReachabilityFlagsIsLocalAddress
153 This flag indicates that the specified nodename or address
154 is one associated with a network interface on the current
156 @constant kSCNetworkReachabilityFlagsIsDirect
157 This flag indicates that network traffic to the specified
158 nodename or address will not go through a gateway, but is
159 routed directly to one of the interfaces in the system.
160 @constant kSCNetworkReachabilityFlagsIsWWAN
161 This flag indicates that the specified nodename or address can
162 be reached via an EDGE, GPRS, or other "cell" connection.
164 typedef CF_OPTIONS(uint32_t, SCNetworkReachabilityFlags
) {
165 kSCNetworkReachabilityFlagsTransientConnection
= 1<<0,
166 kSCNetworkReachabilityFlagsReachable
= 1<<1,
167 kSCNetworkReachabilityFlagsConnectionRequired
= 1<<2,
168 kSCNetworkReachabilityFlagsConnectionOnTraffic
= 1<<3,
169 kSCNetworkReachabilityFlagsInterventionRequired
= 1<<4,
170 kSCNetworkReachabilityFlagsConnectionOnDemand
171 API_AVAILABLE(macos(6.0),ios(3.0)) = 1<<5,
172 kSCNetworkReachabilityFlagsIsLocalAddress
= 1<<16,
173 kSCNetworkReachabilityFlagsIsDirect
= 1<<17,
174 kSCNetworkReachabilityFlagsIsWWAN
175 API_UNAVAILABLE(macos
) API_AVAILABLE(ios(2.0)) = 1<<18,
177 kSCNetworkReachabilityFlagsConnectionAutomatic
= kSCNetworkReachabilityFlagsConnectionOnTraffic
181 @typedef SCNetworkReachabilityCallBack
182 @discussion Type of the callback function used when the
183 reachability of a network address or name changes.
184 @param target The SCNetworkReachability reference being monitored
186 @param flags The new SCNetworkReachabilityFlags representing the
187 reachability status of the network address/name.
188 @param info A C pointer to a user-specified block of data.
190 typedef void (*SCNetworkReachabilityCallBack
) (
191 SCNetworkReachabilityRef target
,
192 SCNetworkReachabilityFlags flags
,
193 void * __nullable info
199 @function SCNetworkReachabilityCreateWithAddress
200 @discussion Creates a reference to the specified network
201 address. This reference can be used later to monitor the
202 reachability of the target host.
203 @param address The address of the desired host.
204 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
206 You must release the returned value.
208 SCNetworkReachabilityRef __nullable
209 SCNetworkReachabilityCreateWithAddress (
210 CFAllocatorRef __nullable allocator
,
211 const struct sockaddr
*address
212 ) API_AVAILABLE(macos(10.3), ios(2.0));
215 @function SCNetworkReachabilityCreateWithAddressPair
216 @discussion Creates a reference to the specified network
217 address. This reference can be used later to monitor the
218 reachability of the target host.
219 @param localAddress The local address associated with a network
220 connection. If NULL, only the remote address is of interest.
221 @param remoteAddress The remote address associated with a network
222 connection. If NULL, only the local address is of interest.
223 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
225 You must release the returned value.
227 SCNetworkReachabilityRef __nullable
228 SCNetworkReachabilityCreateWithAddressPair (
229 CFAllocatorRef __nullable allocator
,
230 const struct sockaddr
* __nullable localAddress
,
231 const struct sockaddr
* __nullable remoteAddress
232 ) API_AVAILABLE(macos(10.3), ios(2.0));
235 @function SCNetworkReachabilityCreateWithName
236 @discussion Creates a reference to the specified network host or node
237 name. This reference can be used later to monitor the
238 reachability of the target host.
239 @param nodename The node name of the desired host.
240 This name would be the same as that passed to the
241 gethostbyname(3) or getaddrinfo(3) functions.
242 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
244 You must release the returned value.
246 SCNetworkReachabilityRef __nullable
247 SCNetworkReachabilityCreateWithName (
248 CFAllocatorRef __nullable allocator
,
250 ) API_AVAILABLE(macos(10.3), ios(2.0));
253 @function SCNetworkReachabilityGetTypeID
254 @discussion Returns the type identifier of all SCNetworkReachability
258 SCNetworkReachabilityGetTypeID (void) API_AVAILABLE(macos(10.3), ios(2.0));
262 @function SCNetworkReachabilityGetFlags
263 @discussion Determines if the given target is reachable using the
264 current network configuration.
265 @param target The network reference associated with the address or name
266 to be checked for reachability.
267 @param flags A pointer to memory that will be filled with the
268 SCNetworkReachabilityFlags detailing the reachability
269 of the specified target.
270 @result Returns TRUE if the network connection flags are valid;
271 FALSE if the status could not be determined.
274 SCNetworkReachabilityGetFlags (
275 SCNetworkReachabilityRef target
,
276 SCNetworkReachabilityFlags
*flags
277 ) API_AVAILABLE(macos(10.3), ios(2.0));
280 @function SCNetworkReachabilitySetCallback
281 @discussion Assigns a client to a target, which receives callbacks
282 when the reachability of the target changes.
283 @param target The network reference associated with the address or
284 name to be checked for reachability.
285 @param callout The function to be called when the reachability of the
286 target changes. If NULL, the current client for the target
288 @param context The SCNetworkReachabilityContext associated with
289 the callout. The value may be NULL.
290 @result Returns TRUE if the notification client was successfully set.
293 SCNetworkReachabilitySetCallback (
294 SCNetworkReachabilityRef target
,
295 SCNetworkReachabilityCallBack __nullable callout
,
296 SCNetworkReachabilityContext
* __nullable context
297 ) API_AVAILABLE(macos(10.3), ios(2.0));
300 @function SCNetworkReachabilityScheduleWithRunLoop
301 @discussion Schedules the given target with the given run loop and mode.
302 @param target The address or name that is set up for asynchronous
303 notifications. Must be non-NULL.
304 @param runLoop A reference to a run loop on which the target should
305 be scheduled. Must be non-NULL.
306 @param runLoopMode The mode on which to schedule the target.
308 @result Returns TRUE if the target is scheduled successfully;
312 SCNetworkReachabilityScheduleWithRunLoop (
313 SCNetworkReachabilityRef target
,
314 CFRunLoopRef runLoop
,
315 CFStringRef runLoopMode
316 ) API_AVAILABLE(macos(10.3), ios(2.0));
319 @function SCNetworkReachabilityUnscheduleFromRunLoop
320 @discussion Unschedules the given target from the given run loop
322 @param target The address or name that is set up for asynchronous
323 notifications. Must be non-NULL.
324 @param runLoop A reference to a run loop from which the target
325 should be unscheduled. Must be non-NULL.
326 @param runLoopMode The mode on which to unschedule the target.
328 @result Returns TRUE if the target is unscheduled successfully;
332 SCNetworkReachabilityUnscheduleFromRunLoop (
333 SCNetworkReachabilityRef target
,
334 CFRunLoopRef runLoop
,
335 CFStringRef runLoopMode
336 ) API_AVAILABLE(macos(10.3), ios(2.0));
339 @function SCNetworkReachabilitySetDispatchQueue
340 @discussion Schedule or unschedule callbacks for the given target on the given
342 @param target The address or name that is set up for asynchronous
343 notifications. Must be non-NULL.
344 @param queue A libdispatch queue to run the callback on.
345 Pass NULL to unschedule callbacks.
346 @result Returns TRUE if the target is scheduled or unscheduled successfully;
350 SCNetworkReachabilitySetDispatchQueue (
351 SCNetworkReachabilityRef target
,
352 dispatch_queue_t __nullable queue
353 ) API_AVAILABLE(macos(10.6), ios(4.0));
357 CF_ASSUME_NONNULL_END
358 CF_IMPLICIT_BRIDGING_DISABLED
360 #endif /* _SCNETWORKREACHABILITY_H */