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