2 * Copyright (c) 2003-2005, 2008, 2009 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>
35 #include <dispatch/dispatch.h>
36 #endif // !TARGET_OS_IPHONE
39 @header SCNetworkReachability
40 @discussion The SCNetworkReachability API allows an application to
41 determine the status of a system's current network
42 configuration and the reachability of a target host.
43 In addition, reachability can be monitored with notifications
44 that are sent when the status has changed.
46 "Reachability" reflects whether a data packet, sent by
47 an application into the network stack, can leave the local
49 Note that reachability does <i>not</i> guarantee that the data
50 packet will actually be received by the host.
54 @typedef SCNetworkReachabilityRef
55 @discussion This is the handle to a network address or name.
57 typedef const struct __SCNetworkReachability
* SCNetworkReachabilityRef
;
61 @typedef SCNetworkReachabilityContext
62 Structure containing user-specified data and callbacks for SCNetworkReachability.
63 @field version The version number of the structure type being passed
64 in as a parameter to the SCDynamicStore creation function.
65 This structure is version 0.
66 @field info A C pointer to a user-specified block of data.
67 @field retain The callback used to add a retain for the info field.
68 If this parameter is not a pointer to a function of the correct
69 prototype, the behavior is undefined. The value may be NULL.
70 @field release The calllback used to remove a retain previously added
71 for the info field. If this parameter is not a pointer to a
72 function of the correct prototype, the behavior is undefined.
73 The value may be NULL.
74 @field copyDescription The callback used to provide a description of
80 const void *(*retain
)(const void *info
);
81 void (*release
)(const void *info
);
82 CFStringRef (*copyDescription
)(const void *info
);
83 } SCNetworkReachabilityContext
;
86 @enum SCNetworkReachabilityFlags
87 @discussion Flags that indicate whether the specified network
88 nodename or address is reachable, whether a connection is
89 required, and whether some user intervention may be required
90 when establishing a connection.
91 @constant kSCNetworkReachabilityFlagsTransientConnection
92 This flag indicates that the specified nodename or address can
93 be reached via a transient connection, such as PPP.
94 @constant kSCNetworkReachabilityFlagsReachable
95 This flag indicates that the specified nodename or address can
96 be reached using the current network configuration.
97 @constant kSCNetworkReachabilityFlagsConnectionRequired
98 This flag indicates that the specified nodename or address can
99 be reached using the current network configuration, but a
100 connection must first be established.
102 As an example, this status would be returned for a dialup
103 connection that was not currently active, but could handle
104 network traffic for the target system.
105 @constant kSCNetworkReachabilityFlagsConnectionOnTraffic
106 This flag indicates that the specified nodename or address can
107 be reached using the current network configuration, but a
108 connection must first be established. Any traffic directed
109 to the specified name or address will initiate the connection.
111 Note: this flag was previously named kSCNetworkReachabilityFlagsConnectionAutomatic
112 @constant kSCNetworkReachabilityFlagsInterventionRequired
113 This flag indicates that the specified nodename or address can
114 be reached using the current network configuration, but a
115 connection must first be established. In addition, some
116 form of user intervention will be required to establish this
117 connection, such as providing a password, an authentication
120 Note: At the present time, this flag will only be returned
121 in the case where you have a dial-on-traffic configuration
122 (ConnectionOnTraffic), where an attempt to connect has
123 already been made, and where some error (e.g. no dial tone,
124 no answer, bad password, ...) was encountered during the
125 automatic connection attempt. In this case the PPP controller
126 will stop attempting to establish a connection until the user
128 @constant kSCNetworkReachabilityFlagsConnectionOnDemand
129 This flag indicates that the specified nodename or address can
130 be reached using the current network configuration, but a
131 connection must first be established.
132 The connection will be established "On Demand" by the
134 Other APIs will not establish the connection.
135 @constant kSCNetworkReachabilityFlagsIsLocalAddress
136 This flag indicates that the specified nodename or address
137 is one associated with a network interface on the current
139 @constant kSCNetworkReachabilityFlagsIsDirect
140 This flag indicates that network traffic to the specified
141 nodename or address will not go through a gateway, but is
142 routed directly to one of the interfaces in the system.
144 @constant kSCNetworkReachabilityFlagsIsWWAN
145 This flag indicates that the specified nodename or address can
146 be reached via an EDGE, GPRS, or other "cell" connection.
147 #endif // TARGET_OS_IPHONE
150 kSCNetworkReachabilityFlagsTransientConnection
= 1<<0,
151 kSCNetworkReachabilityFlagsReachable
= 1<<1,
152 kSCNetworkReachabilityFlagsConnectionRequired
= 1<<2,
153 kSCNetworkReachabilityFlagsConnectionOnTraffic
= 1<<3,
154 kSCNetworkReachabilityFlagsInterventionRequired
= 1<<4,
155 #if (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) || (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) || TARGET_IPHONE_SIMULATOR
156 kSCNetworkReachabilityFlagsConnectionOnDemand
= 1<<5,
158 kSCNetworkReachabilityFlagsIsLocalAddress
= 1<<16,
159 kSCNetworkReachabilityFlagsIsDirect
= 1<<17,
161 kSCNetworkReachabilityFlagsIsWWAN
= 1<<18,
162 #endif // TARGET_OS_IPHONE
164 kSCNetworkReachabilityFlagsConnectionAutomatic
= kSCNetworkReachabilityFlagsConnectionOnTraffic
166 typedef uint32_t SCNetworkReachabilityFlags
;
169 @typedef SCNetworkReachabilityCallBack
170 @discussion Type of the callback function used when the
171 reachability of a network address or name changes.
172 @param target The SCNetworkReachability reference being monitored
174 @param flags The new SCNetworkReachabilityFlags representing the
175 reachability status of the network address/name.
176 @param info A C pointer to a user-specified block of data.
178 typedef void (*SCNetworkReachabilityCallBack
) (
179 SCNetworkReachabilityRef target
,
180 SCNetworkReachabilityFlags flags
,
187 @function SCNetworkReachabilityCreateWithAddress
188 @discussion Creates a reference to the specified network
189 address. This reference can be used later to monitor the
190 reachability of the target host.
191 @param address The address of the desired host.
192 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
194 You must release the returned value.
196 SCNetworkReachabilityRef
197 SCNetworkReachabilityCreateWithAddress (
198 CFAllocatorRef allocator
,
199 const struct sockaddr
*address
200 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
203 @function SCNetworkReachabilityCreateWithAddressPair
204 @discussion Creates a reference to the specified network
205 address. This reference can be used later to monitor the
206 reachability of the target host.
207 @param localAddress The local address associated with a network
208 connection. If NULL, only the remote address is of interest.
209 @param remoteAddress The remote address associated with a network
210 connection. If NULL, only the local address is of interest.
211 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
213 You must release the returned value.
215 SCNetworkReachabilityRef
216 SCNetworkReachabilityCreateWithAddressPair (
217 CFAllocatorRef allocator
,
218 const struct sockaddr
*localAddress
,
219 const struct sockaddr
*remoteAddress
220 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
223 @function SCNetworkReachabilityCreateWithName
224 @discussion Creates a reference to the specified network host or node
225 name. This reference can be used later to monitor the
226 reachability of the target host.
227 @param nodename The node name of the desired host.
228 This name would be the same as that passed to the
229 gethostbyname(3) or getaddrinfo(3) functions.
230 @result Returns a reference to the new immutable SCNetworkReachabilityRef.
232 You must release the returned value.
234 SCNetworkReachabilityRef
235 SCNetworkReachabilityCreateWithName (
236 CFAllocatorRef allocator
,
238 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
241 @function SCNetworkReachabilityGetTypeID
242 @discussion Returns the type identifier of all SCNetworkReachability
246 SCNetworkReachabilityGetTypeID (void) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
250 @function SCNetworkReachabilityGetFlags
251 @discussion Determines if the given target is reachable using the
252 current network configuration.
253 @param target The network reference associated with the address or name
254 to be checked for reachability.
255 @param flags A pointer to memory that will be filled with the
256 SCNetworkReachabilityFlags detailing the reachability
257 of the specified target.
258 @result Returns TRUE if the network connection flags are valid;
259 FALSE if the status could not be determined.
262 SCNetworkReachabilityGetFlags (
263 SCNetworkReachabilityRef target
,
264 SCNetworkReachabilityFlags
*flags
265 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
268 @function SCNetworkReachabilitySetCallback
269 @discussion Assigns a client to a target, which receives callbacks
270 when the reachability of the target changes.
271 @param target The network reference associated with the address or
272 name to be checked for reachability.
273 @param callout The function to be called when the reachability of the
274 target changes. If NULL, the current client for the target
276 @param context The SCNetworkReachabilityContext associated with
277 the callout. The value may be NULL.
278 @result Returns TRUE if the notification client was successfully set.
281 SCNetworkReachabilitySetCallback (
282 SCNetworkReachabilityRef target
,
283 SCNetworkReachabilityCallBack callout
,
284 SCNetworkReachabilityContext
*context
285 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
288 @function SCNetworkReachabilityScheduleWithRunLoop
289 @discussion Schedules the given target with the given run loop and mode.
290 @param target The address or name that is set up for asynchronous
291 notifications. Must be non-NULL.
292 @param runLoop A reference to a run loop on which the target should
293 be scheduled. Must be non-NULL.
294 @param runLoopMode The mode on which to schedule the target.
296 @result Returns TRUE if the target is scheduled successfully;
300 SCNetworkReachabilityScheduleWithRunLoop (
301 SCNetworkReachabilityRef target
,
302 CFRunLoopRef runLoop
,
303 CFStringRef runLoopMode
304 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
307 @function SCNetworkReachabilityUnscheduleFromRunLoop
308 @discussion Unschedules the given target from the given run loop
310 @param target The address or name that is set up for asynchronous
311 notifications. Must be non-NULL.
312 @param runLoop A reference to a run loop from which the target
313 should be unscheduled. Must be non-NULL.
314 @param runLoopMode The mode on which to unschedule the target.
316 @result Returns TRUE if the target is unscheduled successfully;
320 SCNetworkReachabilityUnscheduleFromRunLoop (
321 SCNetworkReachabilityRef target
,
322 CFRunLoopRef runLoop
,
323 CFStringRef runLoopMode
324 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
);
326 #if !TARGET_OS_IPHONE
328 @function SCNetworkReachabilitySetDispatchQueue
329 @discussion Schedules callbacks for the given target on the given
331 @param target The address or name that is set up for asynchronous
332 notifications. Must be non-NULL.
333 @param queue A libdispatch queue to run the callback on. Pass NULL to disable notifications, and release queue.
334 @result Returns TRUE if the target is unscheduled successfully;
338 SCNetworkReachabilitySetDispatchQueue (
339 SCNetworkReachabilityRef target
,
340 dispatch_queue_t queue
341 ) __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
);
342 #endif // !TARGET_OS_IPHONE
346 #endif /* _SCNETWORKREACHABILITY_H */