2 * Copyright (c) 2003-2006, 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 _SCNETWORKCONNECTION_H
25 #ifdef USE_SYSTEMCONFIGURATION_PRIVATE_HEADERS
26 #include <SystemConfiguration/_SCNetworkConnection.h>
27 #else /* USE_SYSTEMCONFIGURATION_PRIVATE_HEADERS */
28 #define _SCNETWORKCONNECTION_H
30 #include <Availability.h>
31 #include <TargetConditionals.h>
32 #include <sys/cdefs.h>
34 #include <dispatch/dispatch.h>
35 #endif // !TARGET_OS_IPHONE
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <CoreFoundation/CoreFoundation.h>
39 #include <SystemConfiguration/SystemConfiguration.h>
43 @header SCNetworkConnection
44 @discussion The SCNetworkConnection API contains functions that allow
45 an application to control connection-oriented services defined
46 in the system and get connection-status information.
48 The functions in the SCNetworkConnection API allow you to
49 control and get information about existing services only.
50 If you need to create, change, or remove services, you
51 should use the SCNetworkConfiguration API instead.
53 Note: Currently, only PPP services can be controlled.
58 @typedef SCNetworkConnectionRef
59 @discussion This is the handle to manage a connection-oriented service.
61 typedef const struct __SCNetworkConnection
* SCNetworkConnectionRef
;
65 @typedef SCNetworkConnectionContext
66 @discussion Structure containing user-specified data and callbacks
67 for a SCNetworkConnection.
68 @field version The version number of the structure type being passed
69 in as a parameter to the SCNetworkConnectionCreateWithServiceID
70 function. This structure is version 0.
71 @field info A C pointer to a user-specified block of data.
72 @field retain The callback used to add a retain for the info field.
73 If this parameter is not a pointer to a function of the correct
74 prototype, the behavior is undefined. The value may be NULL.
75 @field release The calllback used to remove a retain previously added
76 for the info field. If this parameter is not a pointer to a
77 function of the correct prototype, the behavior is undefined.
78 The value may be NULL.
79 @field copyDescription The callback used to provide a description of
85 const void *(*retain
)(const void *info
);
86 void (*release
)(const void *info
);
87 CFStringRef (*copyDescription
)(const void *info
);
88 } SCNetworkConnectionContext
;
93 @enum SCNetworkConnectionStatus
94 @discussion Status of the network connection.
95 This status is intended to be generic and high level.
96 An extended status, specific to the type of network
97 connection is also available for applications that
98 need additonal information.
99 @constant kSCNetworkConnectionInvalid
100 The network connection refers to an invalid service.
101 @constant kSCNetworkConnectionDisconnected
102 The network connection is disconnected.
103 @constant kSCNetworkConnectionConnecting
104 The network connection is connecting.
105 @constant kSCNetworkConnectionConnected
106 The network connection is connected.
107 @constant kSCNetworkConnectionDisconnecting
108 The network connection is disconnecting.
111 kSCNetworkConnectionInvalid
= -1,
112 kSCNetworkConnectionDisconnected
= 0,
113 kSCNetworkConnectionConnecting
= 1,
114 kSCNetworkConnectionConnected
= 2,
115 kSCNetworkConnectionDisconnecting
= 3
117 typedef int32_t SCNetworkConnectionStatus
;
121 @enum SCNetworkConnectionPPPStatus
122 @discussion PPP-specific status of the network connection.
123 This status is returned as part of the extended information
125 Note: additional status might be returned in the future.
126 Your application should be prepared to receive an unknown value.
127 @constant kSCNetworkConnectionPPPDisconnected
129 @constant kSCNetworkConnectionPPPInitializing
131 @constant kSCNetworkConnectionPPPConnectingLink
132 PPP is connecting the lower connection layer (for example,
133 the modem is dialing out).
134 @constant kSCNetworkConnectionPPPDialOnTraffic
135 PPP is waiting for networking traffic to automatically
136 establish the connection.
137 @constant kSCNetworkConnectionPPPNegotiatingLink
138 The PPP lower layer is connected and PPP is negotiating the
139 link layer (LCP protocol).
140 @constant kSCNetworkConnectionPPPAuthenticating
141 PPP is authenticating to the server (PAP, CHAP, MS-CHAP or
143 @constant kSCNetworkConnectionPPPWaitingForCallBack
144 PPP is waiting for the server to call back.
145 @constant kSCNetworkConnectionPPPNegotiatingNetwork
146 PPP is now authenticated and negotiating the networking
147 layer (IPCP or IPv6CP protocols)
148 @constant kSCNetworkConnectionPPPConnected
149 PPP is now fully connected for at least one networking layer.
150 Additional networking protocol might still be negotiating.
151 @constant kSCNetworkConnectionPPPTerminating
152 PPP networking and link protocols are terminating.
153 @constant kSCNetworkConnectionPPPDisconnectingLink
154 PPP is disconnecting the lower level (for example, the modem
156 @constant kSCNetworkConnectionPPPHoldingLinkOff
157 PPP is disconnected and maintaining the link temporarily off.
158 @constant kSCNetworkConnectionPPPSuspended
159 PPP is suspended as a result of the suspend command (for
160 example, when a V.92 Modem is On Hold).
161 @constant kSCNetworkConnectionPPPWaitingForRedial
162 PPP has found a busy server and is waiting for redial.
165 kSCNetworkConnectionPPPDisconnected
= 0,
166 kSCNetworkConnectionPPPInitializing
= 1,
167 kSCNetworkConnectionPPPConnectingLink
= 2,
168 kSCNetworkConnectionPPPDialOnTraffic
= 3,
169 kSCNetworkConnectionPPPNegotiatingLink
= 4,
170 kSCNetworkConnectionPPPAuthenticating
= 5,
171 kSCNetworkConnectionPPPWaitingForCallBack
= 6,
172 kSCNetworkConnectionPPPNegotiatingNetwork
= 7,
173 kSCNetworkConnectionPPPConnected
= 8,
174 kSCNetworkConnectionPPPTerminating
= 9,
175 kSCNetworkConnectionPPPDisconnectingLink
= 10,
176 kSCNetworkConnectionPPPHoldingLinkOff
= 11,
177 kSCNetworkConnectionPPPSuspended
= 12,
178 kSCNetworkConnectionPPPWaitingForRedial
= 13
180 typedef int32_t SCNetworkConnectionPPPStatus
;
184 @typedef SCNetworkConnectionCallBack
185 @discussion Type of the callback function used when a
186 status event is delivered.
187 @param status The connection status.
188 @param connection The connection reference.
189 @param info Application-specific information.
191 typedef void (*SCNetworkConnectionCallBack
) (
192 SCNetworkConnectionRef connection
,
193 SCNetworkConnectionStatus status
,
200 Keys for the statistics dictionary
203 #define kSCNetworkConnectionBytesIn CFSTR("BytesIn") /* CFNumber */
204 #define kSCNetworkConnectionBytesOut CFSTR("BytesOut") /* CFNumber */
205 #define kSCNetworkConnectionPacketsIn CFSTR("PacketsIn") /* CFNumber */
206 #define kSCNetworkConnectionPacketsOut CFSTR("PacketsOut") /* CFNumber */
207 #define kSCNetworkConnectionErrorsIn CFSTR("ErrorsIn") /* CFNumber */
208 #define kSCNetworkConnectionErrorsOut CFSTR("ErrorsOut") /* CFNumber */
212 Keys for the SCNetworkConnectionCopyUserPreferences() "selectionOptions"
217 @define kSCNetworkConnectionSelectionOptionOnDemandHostName
218 @discussion A host name that will be used to select the
219 "best" SCNetworkConnection.
221 #if (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) || (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) || TARGET_IPHONE_SIMULATOR
222 #define kSCNetworkConnectionSelectionOptionOnDemandHostName CFSTR("OnDemandHostName") /* CFString */
226 @define kSCNetworkConnectionSelectionOptionOnDemandRetry
227 @discussion A boolean value used to indicate whether a DNS query has
228 already been issued for the specified OnDemand host name.
230 #if (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) || (__IPHONE_OS_VERSION_MIN_REQUIRED >= 30000) || TARGET_IPHONE_SIMULATOR
231 #define kSCNetworkConnectionSelectionOptionOnDemandRetry CFSTR("OnDemandRetry") /* CFBoolean */
237 @function SCNetworkConnectionGetTypeID
238 @discussion Returns the type identifier of all SCNetworkConnection
243 SCNetworkConnectionGetTypeID (void) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
247 @function SCNetworkConnectionCopyUserPreferences
248 @discussion Provides the default service ID and a dictionary of user
249 options for the connection. Applications can use the
250 returned serviceID and userOptions values to open a
251 connection on the fly.
252 @param selectionOptions Currently unimplemented. Pass NULL for this
254 @param serviceID Reference to the default serviceID for starting
255 connections, this value will be returned by the function.
256 @param userOptions Reference to default userOptions for starting
257 connections, this will be returned by the function.
258 @result Returns TRUE if there is a valid service to dial;
259 FALSE if the function was unable to retrieve a service to dial.
262 SCNetworkConnectionCopyUserPreferences (
263 CFDictionaryRef selectionOptions
,
264 CFStringRef
*serviceID
,
265 CFDictionaryRef
*userOptions
266 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
270 @function SCNetworkConnectionCreateWithServiceID
271 @discussion Creates a new connection reference to use for getting
272 the status or for connecting or disconnecting the associated
274 @param allocator The CFAllocator that should be used to allocate
275 memory for the connection structure. This parameter may be
276 NULL in which case the current default CFAllocator is used.
277 If this reference is not a valid CFAllocator, the behavior
279 @param serviceID A string that defines the service identifier
280 of the connection. Service identifiers uniquely identify
281 services in the system configuration database.
282 @param callout The function to be called when the status
283 of the connection changes. If this parameter is NULL, the
284 application will not receive notifications of status change
285 and will need to poll for updates.
286 @param context The SCNetworkConnectionContext associated with the
288 @result Returns a reference to the new SCNetworkConnection.
290 SCNetworkConnectionRef
291 SCNetworkConnectionCreateWithServiceID (
292 CFAllocatorRef allocator
,
293 CFStringRef serviceID
,
294 SCNetworkConnectionCallBack callout
,
295 SCNetworkConnectionContext
*context
296 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
300 @function SCNetworkConnectionCopyServiceID
301 @discussion Returns the service ID associated with the SCNetworkConnection.
302 @param connection The SCNetworkConnection to obtain status from.
303 @result Returns the service ID associated with the SCNetworkConnection.
306 SCNetworkConnectionCopyServiceID (
307 SCNetworkConnectionRef connection
308 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
312 @function SCNetworkConnectionGetStatus
313 @discussion Returns the status of the SCNetworkConnection.
314 A status is one of the following values:
318 kSCNetworkConnectionInvalid
319 kSCNetworkConnectionDisconnected
320 kSCNetworkConnectionConnecting
321 kSCNetworkConnectionDisconnecting
322 kSCNetworkConnectionConnected
325 @param connection The SCNetworkConnection to obtain status from.
326 @result Returns the status value.
328 SCNetworkConnectionStatus
329 SCNetworkConnectionGetStatus (
330 SCNetworkConnectionRef connection
331 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
335 @function SCNetworkConnectionCopyExtendedStatus
336 @discussion Returns the extended status of the connection.
337 An extended status dictionary contains specific dictionaries
338 describing the status for each subcomponent of the service.
340 For example, a status dictionary will contain the following
341 sub-dictionaries, keys, and values:
345 IPv4 : Addresses : the assigned IP address.
347 PPP : Status : the PPP-specific status of type
348 SCNetworkConnectionPPPStatus.
350 LastCause : Available when the status is "Disconnected"
351 and contains the last error associated with
352 connecting or disconnecting.
354 ConnectTime : the time when the connection was
357 Modem : ConnectSpeed : the speed of the modem connection
360 IPSec : Status : the IPSec-specific status of type
361 SCNetworkConnectionIPSecStatus
363 ConnectTime : the time when the connection was
368 Other dictionaries could be present for PPPoE, PPTP, and L2TP.
370 The status dictionary may be extended in the future to contain
371 additional information.
372 @param connection The SCNetworkConnection to obtain status from.
373 @result Returns the status dictionary.
374 If NULL is returned, the error can be retrieved using the SCError function.
377 SCNetworkConnectionCopyExtendedStatus (
378 SCNetworkConnectionRef connection
379 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
383 @function SCNetworkConnectionCopyStatistics
384 @discussion Returns the statistics of the SCNetworkConnection.
385 A statistic dictionary contains specific dictionaries
386 with statistics for each subcomponent of the service.
388 For example, a statistics dictionary will contain the following
389 sub-dictionaries, keys, and values:
394 PPP : BytesOut : Contains the number of bytes going up into
395 (or coming out of) the network stack for
396 any networking protocol without the PPP
397 headers and trailers.
400 PPP : PacketsOut : Contains the number of packets going up into
401 (or coming out of) the network stack for
402 any networking protocol without the PPP
403 headers and trailers.
406 PPP : ErrorsOut : Contains the number of errors going up into
407 (or coming out of) the network stack for
408 any networking protocol without the PPP
409 headers and trailers.
412 The statistics dictionary may be extended in the future to
413 contain additional information.
414 @param connection The SCNetworkConnection to obtained statistics from.
415 @result Returns the statistics dictionary.
416 If NULL is returned, the error can be retrieved using the SCError function.
419 SCNetworkConnectionCopyStatistics (
420 SCNetworkConnectionRef connection
421 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
425 @function SCNetworkConnectionStart
426 @discussion Starts the connection for the SCNetworkConnection.
427 The connection process is asynchronous and the function will
428 return immediately. The connection status can be obtained
429 by polling or by callback. The connection is made with the
430 default settings from the administrator. Some of the settings
431 can be overridden for the duration of the connection. These
432 are specified in an options dictionary. The options dictionary
433 uses the same format as a network service defined in the system
434 configuration preferences schema.
436 Note: Starting and stopping of connections is implicitly
437 arbitrated. Calling SCNetworkConnectionStart on a connection
438 already started will indicate that the application has
439 interest in the connection and it shouldn't be stopped by
441 @param connection The SCNetworkConnection to start.
442 @param userOptions The options dictionary to start the connection with.
443 If userOptions is NULL, the default settings will be used.
444 If userOptions are specified, they must be in the same format
445 as network services stored in the system configuration
446 preferences schema. The options will override the default
447 settings defined for the service.
449 For security reasons, not all options can be overridden; the
450 appropriate merging of all settings will be done before the
451 connection is established, and inappropriate options will be
453 @param linger This parameter indicates whether or not the connection
454 can stay around when the application no longer has interest
455 in it. A typical application should pass FALSE, and the
456 connection will be automatically stopped when the reference
457 is released or if the application quits. If the application
458 passes TRUE, the application can release the reference or
459 exit and the connection will be maintained until a timeout
460 event, until a specific stop request occurs, or until an
461 error is encountered.
462 @result Returns TRUE if the connection was correctly started (the
463 actual connection is not established yet, and the connection
464 status needs to be periodically checked); FALSE if the
465 connection request was not started. The error must be
466 retrieved from the SCError function.
469 SCNetworkConnectionStart (
470 SCNetworkConnectionRef connection
,
471 CFDictionaryRef userOptions
,
473 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
477 @function SCNetworkConnectionStop
478 @discussion Stops the connection for the SCNetworkConnection.
479 The disconnection process is asynchronous and the function
480 will return immediately. The connection status can be
481 obtained by polling or by callback. This function performs
482 an arbitrated stop of the connection. If several applications
483 have marked their interest in the connection, by calling
484 SCNetworkConnectionStart, the call will succeed but the
485 actual connection will be maintained until the last interested
486 application calls SCNetworkConnectionStop.
488 In certain cases, you might want to stop the connection anyway.
489 In these cases, you set the forceDisconnect argument to TRUE.
490 @param connection The SCNetworkConnection to stop.
491 @result Returns TRUE if the disconnection request succeeded;
492 FALSE if the disconnection request failed.
493 The error must be retrieved from the SCError function.
496 SCNetworkConnectionStop (
497 SCNetworkConnectionRef connection
,
498 Boolean forceDisconnect
499 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
503 @function SCNetworkConnectionCopyUserOptions
504 @discussion Copies the user options used to start the connection.
505 This is a mechanism a client can use to retrieve the user options
506 previously passed to the SCNetworkConnectionStart function.
507 @param connection The SCNetworkConnection to obtain options from.
508 @result Returns the service dictionary containing the connection options.
509 The dictionary can be empty if no user options were used.
510 If NULL is returned, the error can be retrieved using the SCError function.
513 SCNetworkConnectionCopyUserOptions (
514 SCNetworkConnectionRef connection
515 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
519 @function SCNetworkConnectionScheduleWithRunLoop
520 @discussion Schedules a connection with the run loop.
521 @param connection The SCNetworkConnection to schedule.
522 @param runLoop The run loop to schedule with.
523 @param runLoopMode The run loop mode.
524 @result Returns TRUE if the connection is scheduled successfully;
525 FALSE if the scheduling failed.
526 The error can be retrieved using the SCError function.
529 SCNetworkConnectionScheduleWithRunLoop (
530 SCNetworkConnectionRef connection
,
531 CFRunLoopRef runLoop
,
532 CFStringRef runLoopMode
533 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
537 @function SCNetworkConnectionUnscheduleFromRunLoop
538 @discussion Unschedules a connection from the run loop.
539 @param connection The SCNetworkConnection to unschedule.
540 @param runLoop The run loop to unschedule from.
541 @param runLoopMode The run loop mode.
542 @result Returns TRUE if the connection is unscheduled successfully;
543 FALSE if the unscheduling failed.
544 The error can be retrieved using the SCError function.
547 SCNetworkConnectionUnscheduleFromRunLoop (
548 SCNetworkConnectionRef connection
,
549 CFRunLoopRef runLoop
,
550 CFStringRef runLoopMode
551 ) __OSX_AVAILABLE_STARTING(__MAC_10_3
,__IPHONE_2_0
/*SPI*/);
554 #if !TARGET_OS_IPHONE
556 @function SCNetworkConnectionSetDispatchQueue
557 @discussion Caller provides a dispatch queue on which the callback contained in connection will run.
558 @param connection The SCNetworkConnection to notify.
559 @param queue The libdispatch queue to run the callback on.
560 Pass NULL to disable notifications, and release queue.
561 @result Returns TRUE if the notifications have been enabled/disabled as desired;
563 The error can be retrieved using the SCError function.
566 SCNetworkConnectionSetDispatchQueue (
567 SCNetworkConnectionRef connection
,
568 dispatch_queue_t queue
569 ) __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
);
570 #endif // !TARGET_OS_IPHONE
574 #endif /* USE_SYSTEMCONFIGURATION_PRIVATE_HEADERS */
575 #endif /* _SCNETWORKCONNECTION_H */