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