2 * Copyright (c) 2003-2005, 2008-2010, 2015-2016 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 <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.
161 @constant kSCNetworkReachabilityFlagsIsWWAN
162 This flag indicates that the specified nodename or address can
163 be reached via an EDGE, GPRS, or other "cell" connection.
164 #endif // TARGET_OS_IPHONE
166 typedef CF_OPTIONS(uint32_t, SCNetworkReachabilityFlags
) {
167 kSCNetworkReachabilityFlagsTransientConnection
= 1<<0,
168 kSCNetworkReachabilityFlagsReachable
= 1<<1,
169 kSCNetworkReachabilityFlagsConnectionRequired
= 1<<2,
170 kSCNetworkReachabilityFlagsConnectionOnTraffic
= 1<<3,
171 kSCNetworkReachabilityFlagsInterventionRequired
= 1<<4,
172 kSCNetworkReachabilityFlagsConnectionOnDemand
= 1<<5, // __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_3_0)
173 kSCNetworkReachabilityFlagsIsLocalAddress
= 1<<16,
174 kSCNetworkReachabilityFlagsIsDirect
= 1<<17,
176 kSCNetworkReachabilityFlagsIsWWAN
= 1<<18,
177 #endif // TARGET_OS_IPHONE
179 kSCNetworkReachabilityFlagsConnectionAutomatic
= kSCNetworkReachabilityFlagsConnectionOnTraffic
183 @typedef SCNetworkReachabilityCallBack
184 @discussion Type of the callback function used when the
185 reachability of a network address or name changes.
186 @param target The SCNetworkReachability reference being monitored
188 @param flags The new SCNetworkReachabilityFlags representing the
189 reachability status of the network address/name.
190 @param info A C pointer to a user-specified block of data.
192 typedef void (*SCNetworkReachabilityCallBack
) (
193 SCNetworkReachabilityRef target
,
194 SCNetworkReachabilityFlags flags
,
195 void * __nullable info
201 @function SCNetworkReachabilityCreateWithAddress
202 @discussion Creates a reference to the specified network
203 address. This reference can be used later to monitor the
204 reachability of the target host.
205 @param address The address of the desired host.
206 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
208 You must release the returned value.
210 SCNetworkReachabilityRef __nullable
211 SCNetworkReachabilityCreateWithAddress (
212 CFAllocatorRef __nullable allocator
,
213 const struct sockaddr
*address
214 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
217 @function SCNetworkReachabilityCreateWithAddressPair
218 @discussion Creates a reference to the specified network
219 address. This reference can be used later to monitor the
220 reachability of the target host.
221 @param localAddress The local address associated with a network
222 connection. If NULL, only the remote address is of interest.
223 @param remoteAddress The remote address associated with a network
224 connection. If NULL, only the local address is of interest.
225 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
227 You must release the returned value.
229 SCNetworkReachabilityRef __nullable
230 SCNetworkReachabilityCreateWithAddressPair (
231 CFAllocatorRef __nullable allocator
,
232 const struct sockaddr
* __nullable localAddress
,
233 const struct sockaddr
* __nullable remoteAddress
234 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
237 @function SCNetworkReachabilityCreateWithName
238 @discussion Creates a reference to the specified network host or node
239 name. This reference can be used later to monitor the
240 reachability of the target host.
241 @param nodename The node name of the desired host.
242 This name would be the same as that passed to the
243 gethostbyname(3) or getaddrinfo(3) functions.
244 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
246 You must release the returned value.
248 SCNetworkReachabilityRef __nullable
249 SCNetworkReachabilityCreateWithName (
250 CFAllocatorRef __nullable allocator
,
252 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
255 @function SCNetworkReachabilityGetTypeID
256 @discussion Returns the type identifier of all SCNetworkReachability
260 SCNetworkReachabilityGetTypeID (void) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
264 @function SCNetworkReachabilityGetFlags
265 @discussion Determines if the given target is reachable using the
266 current network configuration.
267 @param target The network reference associated with the address or name
268 to be checked for reachability.
269 @param flags A pointer to memory that will be filled with the
270 SCNetworkReachabilityFlags detailing the reachability
271 of the specified target.
272 @result Returns TRUE if the network connection flags are valid;
273 FALSE if the status could not be determined.
276 SCNetworkReachabilityGetFlags (
277 SCNetworkReachabilityRef target
,
278 SCNetworkReachabilityFlags
*flags
279 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
282 @function SCNetworkReachabilitySetCallback
283 @discussion Assigns a client to a target, which receives callbacks
284 when the reachability of the target changes.
285 @param target The network reference associated with the address or
286 name to be checked for reachability.
287 @param callout The function to be called when the reachability of the
288 target changes. If NULL, the current client for the target
290 @param context The SCNetworkReachabilityContext associated with
291 the callout. The value may be NULL.
292 @result Returns TRUE if the notification client was successfully set.
295 SCNetworkReachabilitySetCallback (
296 SCNetworkReachabilityRef target
,
297 SCNetworkReachabilityCallBack __nullable callout
,
298 SCNetworkReachabilityContext
* __nullable context
299 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
302 @function SCNetworkReachabilityScheduleWithRunLoop
303 @discussion Schedules the given target with the given run loop and mode.
304 @param target The address or name that is set up for asynchronous
305 notifications. Must be non-NULL.
306 @param runLoop A reference to a run loop on which the target should
307 be scheduled. Must be non-NULL.
308 @param runLoopMode The mode on which to schedule the target.
310 @result Returns TRUE if the target is scheduled successfully;
314 SCNetworkReachabilityScheduleWithRunLoop (
315 SCNetworkReachabilityRef target
,
316 CFRunLoopRef runLoop
,
317 CFStringRef runLoopMode
318 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
321 @function SCNetworkReachabilityUnscheduleFromRunLoop
322 @discussion Unschedules the given target from the given run loop
324 @param target The address or name that is set up for asynchronous
325 notifications. Must be non-NULL.
326 @param runLoop A reference to a run loop from which the target
327 should be unscheduled. Must be non-NULL.
328 @param runLoopMode The mode on which to unschedule the target.
330 @result Returns TRUE if the target is unscheduled successfully;
334 SCNetworkReachabilityUnscheduleFromRunLoop (
335 SCNetworkReachabilityRef target
,
336 CFRunLoopRef runLoop
,
337 CFStringRef runLoopMode
338 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
341 @function SCNetworkReachabilitySetDispatchQueue
342 @discussion Schedule or unschedule callbacks for the given target on the given
344 @param target The address or name that is set up for asynchronous
345 notifications. Must be non-NULL.
346 @param queue A libdispatch queue to run the callback on.
347 Pass NULL to unschedule callbacks.
348 @result Returns TRUE if the target is scheduled or unscheduled successfully;
352 SCNetworkReachabilitySetDispatchQueue (
353 SCNetworkReachabilityRef target
,
354 dispatch_queue_t __nullable queue
355 ) __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
);
359 CF_ASSUME_NONNULL_END
360 CF_IMPLICIT_BRIDGING_DISABLED
362 #endif /* _SCNETWORKREACHABILITY_H */