]> git.saurik.com Git - apple/cf.git/blob - CFSocket.h
CF-550.13.tar.gz
[apple/cf.git] / CFSocket.h
1 /*
2 * Copyright (c) 2009 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 /* CFSocket.h
25 Copyright (c) 1999-2009, Apple Inc. All rights reserved.
26 */
27
28 #if !defined(__COREFOUNDATION_CFSOCKET__)
29 #define __COREFOUNDATION_CFSOCKET__ 1
30
31 #include <TargetConditionals.h>
32
33 #if TARGET_OS_WIN32
34 typedef uintptr_t CFSocketNativeHandle;
35 #else
36 typedef int CFSocketNativeHandle;
37 #endif
38
39 #include <CoreFoundation/CFRunLoop.h>
40 #include <CoreFoundation/CFData.h>
41
42 CF_EXTERN_C_BEGIN
43
44 typedef struct __CFSocket * CFSocketRef;
45
46 /* A CFSocket contains a native socket within a structure that can
47 be used to read from the socket in the background and make the data
48 thus read available using a runloop source. The callback used for
49 this may be of three types, as specified by the callBackTypes
50 argument when creating the CFSocket.
51
52 If kCFSocketReadCallBack is used, then data will not be
53 automatically read, but the callback will be called when data
54 is available to be read, or a new child socket is waiting to be
55 accepted.
56
57 If kCFSocketAcceptCallBack is used, then new child sockets will be
58 accepted and passed to the callback, with the data argument being
59 a pointer to a CFSocketNativeHandle. This is usable only with
60 connection rendezvous sockets.
61
62 If kCFSocketDataCallBack is used, then data will be read in chunks
63 in the background and passed to the callback, with the data argument
64 being a CFDataRef.
65
66 These three types are mutually exclusive, but any one of them may
67 have kCFSocketConnectCallBack added to it, if the socket will be
68 used to connect in the background. Connect in the background occurs
69 if CFSocketConnectToAddress is called with a negative timeout
70 value, in which case the call returns immediately, and a
71 kCFSocketConnectCallBack is generated when the connect finishes.
72 In this case the data argument is either NULL, or a pointer to
73 an SInt32 error code if the connect failed. kCFSocketConnectCallBack
74 will never be sent more than once for a given socket.
75
76 The callback types may also have kCFSocketWriteCallBack added to
77 them, if large amounts of data are to be sent rapidly over the
78 socket and notification is desired when there is space in the
79 kernel buffers so that the socket is writable again.
80
81 With a connection-oriented socket, if the connection is broken from the
82 other end, then one final kCFSocketReadCallBack or kCFSocketDataCallBack
83 will occur. In the case of kCFSocketReadCallBack, the underlying socket
84 will have 0 bytes available to read. In the case of kCFSocketDataCallBack,
85 the data argument will be a CFDataRef of length 0.
86
87 There are socket flags that may be set to control whether callbacks of
88 a given type are automatically reenabled after they are triggered, and
89 whether the underlying native socket will be closed when the CFSocket
90 is invalidated. By default read, accept, and data callbacks are
91 automatically reenabled; write callbacks are not, and connect callbacks
92 may not be, since they are sent once only. Be careful about automatically
93 reenabling read and write callbacks, since this implies that the
94 callbacks will be sent repeatedly if the socket remains readable or
95 writable respectively. Be sure to set these flags only for callbacks
96 that your CFSocket actually possesses; the result of setting them for
97 other callback types is undefined.
98
99 Individual callbacks may also be enabled and disabled manually, whether
100 they are automatically reenabled or not. If they are not automatically
101 reenabled, then they will need to be manually reenabled when the callback
102 is ready to be received again (and not sooner). Even if they are
103 automatically reenabled, there may be occasions when it will be useful
104 to be able to manually disable them temporarily and then reenable them.
105 Be sure to enable and disable only callbacks that your CFSocket actually
106 possesses; the result of enabling and disabling other callback types is
107 undefined.
108
109 By default the underlying native socket will be closed when the CFSocket
110 is invalidated, but it will not be if kCFSocketCloseOnInvalidate is
111 turned off. This can be useful in order to destroy a CFSocket but
112 continue to use the underlying native socket. The CFSocket must
113 still be invalidated when it will no longer be used. Do not in
114 either case close the underlying native socket without invalidating
115 the CFSocket.
116
117 Addresses are stored as CFDatas containing a struct sockaddr
118 appropriate for the protocol family; make sure that all fields are
119 filled in properly when passing in an address.
120
121 */
122
123 /* Values for CFSocketError */
124 enum {
125 kCFSocketSuccess = 0,
126 kCFSocketError = -1,
127 kCFSocketTimeout = -2
128 };
129 typedef CFIndex CFSocketError;
130
131 typedef struct {
132 SInt32 protocolFamily;
133 SInt32 socketType;
134 SInt32 protocol;
135 CFDataRef address;
136 } CFSocketSignature;
137
138 /* Values for CFSocketCallBackType */
139 enum {
140 kCFSocketNoCallBack = 0,
141 kCFSocketReadCallBack = 1,
142 kCFSocketAcceptCallBack = 2,
143 kCFSocketDataCallBack = 3,
144 kCFSocketConnectCallBack = 4
145 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
146 ,
147 kCFSocketWriteCallBack = 8
148 #endif
149 };
150 typedef CFOptionFlags CFSocketCallBackType;
151
152 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
153 /* Socket flags */
154 enum {
155 kCFSocketAutomaticallyReenableReadCallBack = 1,
156 kCFSocketAutomaticallyReenableAcceptCallBack = 2,
157 kCFSocketAutomaticallyReenableDataCallBack = 3,
158 kCFSocketAutomaticallyReenableWriteCallBack = 8,
159 #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED
160 kCFSocketLeaveErrors = 64,
161 #endif
162 kCFSocketCloseOnInvalidate = 128
163 };
164 #endif
165
166 typedef void (*CFSocketCallBack)(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info);
167 /* If the callback wishes to keep hold of address or data after the point that it returns, then it must copy them. */
168
169 typedef struct {
170 CFIndex version;
171 void * info;
172 const void *(*retain)(const void *info);
173 void (*release)(const void *info);
174 CFStringRef (*copyDescription)(const void *info);
175 } CFSocketContext;
176
177 CF_EXPORT CFTypeID CFSocketGetTypeID(void);
178
179 CF_EXPORT CFSocketRef CFSocketCreate(CFAllocatorRef allocator, SInt32 protocolFamily, SInt32 socketType, SInt32 protocol, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context);
180 CF_EXPORT CFSocketRef CFSocketCreateWithNative(CFAllocatorRef allocator, CFSocketNativeHandle sock, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context);
181 CF_EXPORT CFSocketRef CFSocketCreateWithSocketSignature(CFAllocatorRef allocator, const CFSocketSignature *signature, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context);
182 CF_EXPORT CFSocketRef CFSocketCreateConnectedToSocketSignature(CFAllocatorRef allocator, const CFSocketSignature *signature, CFOptionFlags callBackTypes, CFSocketCallBack callout, const CFSocketContext *context, CFTimeInterval timeout);
183 /* CFSocketCreateWithSignature creates a socket of the requested type and binds its address (using CFSocketSetAddress) to the requested address. If this fails, it returns NULL. CFSocketCreateConnectedToSignature creates a socket suitable for connecting to the requested type and address, and connects it (using CFSocketConnectToAddress). If this fails, it returns NULL. */
184
185 CF_EXPORT CFSocketError CFSocketSetAddress(CFSocketRef s, CFDataRef address);
186 CF_EXPORT CFSocketError CFSocketConnectToAddress(CFSocketRef s, CFDataRef address, CFTimeInterval timeout);
187 CF_EXPORT void CFSocketInvalidate(CFSocketRef s);
188
189 CF_EXPORT Boolean CFSocketIsValid(CFSocketRef s);
190 CF_EXPORT CFDataRef CFSocketCopyAddress(CFSocketRef s);
191 CF_EXPORT CFDataRef CFSocketCopyPeerAddress(CFSocketRef s);
192 CF_EXPORT void CFSocketGetContext(CFSocketRef s, CFSocketContext *context);
193 CF_EXPORT CFSocketNativeHandle CFSocketGetNative(CFSocketRef s);
194
195 CF_EXPORT CFRunLoopSourceRef CFSocketCreateRunLoopSource(CFAllocatorRef allocator, CFSocketRef s, CFIndex order);
196
197 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
198 CF_EXPORT CFOptionFlags CFSocketGetSocketFlags(CFSocketRef s);
199 CF_EXPORT void CFSocketSetSocketFlags(CFSocketRef s, CFOptionFlags flags);
200 CF_EXPORT void CFSocketDisableCallBacks(CFSocketRef s, CFOptionFlags callBackTypes);
201 CF_EXPORT void CFSocketEnableCallBacks(CFSocketRef s, CFOptionFlags callBackTypes);
202 #endif
203
204 /* For convenience, a function is provided to send data using the socket with a timeout. The timeout will be used only if the specified value is positive. The address should be left NULL if the socket is already connected. */
205 CF_EXPORT CFSocketError CFSocketSendData(CFSocketRef s, CFDataRef address, CFDataRef data, CFTimeInterval timeout);
206
207 /* Generic name registry functionality (CFSocketRegisterValue,
208 CFSocketCopyRegisteredValue) allows the registration of any property
209 list type. Functions specific to CFSockets (CFSocketRegisterSocketData,
210 CFSocketCopyRegisteredSocketData) register a CFData containing the
211 components of a socket signature (protocol family, socket type,
212 protocol, and address). In each function the nameServerSignature
213 may be NULL, or any component of it may be 0, to use default values
214 (TCP, INADDR_LOOPBACK, port as set). Name registration servers might
215 not allow registration with other than TCP and INADDR_LOOPBACK.
216 The actual address of the server responding to a query may be obtained
217 by using the nameServerAddress argument. This address, the address
218 returned by CFSocketCopyRegisteredSocketSignature, and the value
219 returned by CFSocketCopyRegisteredValue must (if non-null) be released
220 by the caller. CFSocketUnregister removes any registration associated
221 with the specified name.
222 */
223
224 CF_EXPORT CFSocketError CFSocketRegisterValue(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFPropertyListRef value);
225 CF_EXPORT CFSocketError CFSocketCopyRegisteredValue(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFPropertyListRef *value, CFDataRef *nameServerAddress);
226
227 CF_EXPORT CFSocketError CFSocketRegisterSocketSignature(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, const CFSocketSignature *signature);
228 CF_EXPORT CFSocketError CFSocketCopyRegisteredSocketSignature(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name, CFSocketSignature *signature, CFDataRef *nameServerAddress);
229
230 CF_EXPORT CFSocketError CFSocketUnregister(const CFSocketSignature *nameServerSignature, CFTimeInterval timeout, CFStringRef name);
231
232 CF_EXPORT void CFSocketSetDefaultNameRegistryPortNumber(UInt16 port);
233 CF_EXPORT UInt16 CFSocketGetDefaultNameRegistryPortNumber(void);
234
235 /* Constants used in name registry server communications */
236 CF_EXPORT const CFStringRef kCFSocketCommandKey;
237 CF_EXPORT const CFStringRef kCFSocketNameKey;
238 CF_EXPORT const CFStringRef kCFSocketValueKey;
239 CF_EXPORT const CFStringRef kCFSocketResultKey;
240 CF_EXPORT const CFStringRef kCFSocketErrorKey;
241 CF_EXPORT const CFStringRef kCFSocketRegisterCommand;
242 CF_EXPORT const CFStringRef kCFSocketRetrieveCommand;
243
244 CF_EXTERN_C_END
245
246 #endif /* ! __COREFOUNDATION_CFSOCKET__ */
247