]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCNetworkReachability.h
configd-888.20.5.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCNetworkReachability.h
1 /*
2 * Copyright (c) 2003-2005, 2008-2010, 2015-2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _SCNETWORKREACHABILITY_H
25 #define _SCNETWORKREACHABILITY_H
26
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>
35
36 CF_IMPLICIT_BRIDGING_ENABLED
37 CF_ASSUME_NONNULL_BEGIN
38
39 /*!
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.
46
47 "Reachability" reflects whether a data packet, sent by
48 an application into the network stack, can leave the local
49 computer.
50 Note that reachability does <i>not</i> guarantee that the data
51 packet will actually be received by the host.
52
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
59 leverage DNS results.
60
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.
68 */
69
70 /*!
71 @typedef SCNetworkReachabilityRef
72 @discussion This is the handle to a network address or name.
73 */
74 typedef const struct CF_BRIDGED_TYPE(id) __SCNetworkReachability * SCNetworkReachabilityRef;
75
76
77 /*!
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
92 the info field.
93 */
94 typedef struct {
95 CFIndex version;
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;
101
102 /*!
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.
118
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.
127
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
135 token, etc.
136
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
144 has intervened.
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
150 CFSocketStream APIs.
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
155 system.
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 #if TARGET_OS_IPHONE
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
165 */
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,
175 #if TARGET_OS_IPHONE
176 kSCNetworkReachabilityFlagsIsWWAN = 1<<18,
177 #endif // TARGET_OS_IPHONE
178
179 kSCNetworkReachabilityFlagsConnectionAutomatic = kSCNetworkReachabilityFlagsConnectionOnTraffic
180 };
181
182 /*!
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
187 for changes.
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.
191 */
192 typedef void (*SCNetworkReachabilityCallBack) (
193 SCNetworkReachabilityRef target,
194 SCNetworkReachabilityFlags flags,
195 void * __nullable info
196 );
197
198 __BEGIN_DECLS
199
200 /*!
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.
207
208 You must release the returned value.
209 */
210 SCNetworkReachabilityRef __nullable
211 SCNetworkReachabilityCreateWithAddress (
212 CFAllocatorRef __nullable allocator,
213 const struct sockaddr *address
214 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
215
216 /*!
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.
226
227 You must release the returned value.
228 */
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);
235
236 /*!
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.
245
246 You must release the returned value.
247 */
248 SCNetworkReachabilityRef __nullable
249 SCNetworkReachabilityCreateWithName (
250 CFAllocatorRef __nullable allocator,
251 const char *nodename
252 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
253
254 /*!
255 @function SCNetworkReachabilityGetTypeID
256 @discussion Returns the type identifier of all SCNetworkReachability
257 instances.
258 */
259 CFTypeID
260 SCNetworkReachabilityGetTypeID (void) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
261
262
263 /*!
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.
274 */
275 Boolean
276 SCNetworkReachabilityGetFlags (
277 SCNetworkReachabilityRef target,
278 SCNetworkReachabilityFlags *flags
279 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
280
281 /*!
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
289 is removed.
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.
293 */
294 Boolean
295 SCNetworkReachabilitySetCallback (
296 SCNetworkReachabilityRef target,
297 SCNetworkReachabilityCallBack __nullable callout,
298 SCNetworkReachabilityContext * __nullable context
299 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
300
301 /*!
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.
309 Must be non-NULL.
310 @result Returns TRUE if the target is scheduled successfully;
311 FALSE otherwise.
312 */
313 Boolean
314 SCNetworkReachabilityScheduleWithRunLoop (
315 SCNetworkReachabilityRef target,
316 CFRunLoopRef runLoop,
317 CFStringRef runLoopMode
318 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
319
320 /*!
321 @function SCNetworkReachabilityUnscheduleFromRunLoop
322 @discussion Unschedules the given target from the given run loop
323 and mode.
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.
329 Must be non-NULL.
330 @result Returns TRUE if the target is unscheduled successfully;
331 FALSE otherwise.
332 */
333 Boolean
334 SCNetworkReachabilityUnscheduleFromRunLoop (
335 SCNetworkReachabilityRef target,
336 CFRunLoopRef runLoop,
337 CFStringRef runLoopMode
338 ) __OSX_AVAILABLE_STARTING(__MAC_10_3,__IPHONE_2_0);
339
340 /*!
341 @function SCNetworkReachabilitySetDispatchQueue
342 @discussion Schedule or unschedule callbacks for the given target on the given
343 dispatch queue.
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;
349 FALSE otherwise.
350 */
351 Boolean
352 SCNetworkReachabilitySetDispatchQueue (
353 SCNetworkReachabilityRef target,
354 dispatch_queue_t __nullable queue
355 ) __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0);
356
357 __END_DECLS
358
359 CF_ASSUME_NONNULL_END
360 CF_IMPLICIT_BRIDGING_DISABLED
361
362 #endif /* _SCNETWORKREACHABILITY_H */