1 { CFSocket.h
2 Copyright (c) 1999-2013, Apple Inc. All rights reserved.
3 }
4 {
5 Modified for use with Free Pascal
6 Version 308
7 Please report any bugs to <gpc@microbizz.nl>
8 }
9
10 {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
11 {$mode macpas}
12 {$modeswitch cblocks}
13 {$packenum 1}
14 {$macro on}
15 {$inline on}
16 {$calling mwpascal}
17
18 unit CFSocket;
19 interface
20 {$setc UNIVERSAL_INTERFACES_VERSION := $0400}
21 {$setc GAP_INTERFACES_VERSION := $0308}
22
23 {$ifc not defined USE_CFSTR_CONSTANT_MACROS}
24 {$setc USE_CFSTR_CONSTANT_MACROS := TRUE}
25 {$endc}
26
27 {$ifc defined CPUPOWERPC and defined CPUI386}
28 {$error Conflicting initial definitions for CPUPOWERPC and CPUI386}
29 {$endc}
30 {$ifc defined FPC_BIG_ENDIAN and defined FPC_LITTLE_ENDIAN}
31 {$error Conflicting initial definitions for FPC_BIG_ENDIAN and FPC_LITTLE_ENDIAN}
32 {$endc}
33
34 {$ifc not defined __ppc__ and defined CPUPOWERPC32}
35 {$setc __ppc__ := 1}
36 {$elsec}
37 {$setc __ppc__ := 0}
38 {$endc}
39 {$ifc not defined __ppc64__ and defined CPUPOWERPC64}
40 {$setc __ppc64__ := 1}
41 {$elsec}
42 {$setc __ppc64__ := 0}
43 {$endc}
44 {$ifc not defined __i386__ and defined CPUI386}
45 {$setc __i386__ := 1}
46 {$elsec}
47 {$setc __i386__ := 0}
48 {$endc}
49 {$ifc not defined __x86_64__ and defined CPUX86_64}
50 {$setc __x86_64__ := 1}
51 {$elsec}
52 {$setc __x86_64__ := 0}
53 {$endc}
54 {$ifc not defined __arm__ and defined CPUARM}
55 {$setc __arm__ := 1}
56 {$elsec}
57 {$setc __arm__ := 0}
58 {$endc}
59 {$ifc not defined __arm64__ and defined CPUAARCH64}
60 {$setc __arm64__ := 1}
61 {$elsec}
62 {$setc __arm64__ := 0}
63 {$endc}
64
65 {$ifc defined cpu64}
66 {$setc __LP64__ := 1}
67 {$elsec}
68 {$setc __LP64__ := 0}
69 {$endc}
70
71
72 {$ifc defined __ppc__ and __ppc__ and defined __i386__ and __i386__}
73 {$error Conflicting definitions for __ppc__ and __i386__}
74 {$endc}
75
76 {$ifc defined __ppc__ and __ppc__}
77 {$setc TARGET_CPU_PPC := TRUE}
78 {$setc TARGET_CPU_PPC64 := FALSE}
79 {$setc TARGET_CPU_X86 := FALSE}
80 {$setc TARGET_CPU_X86_64 := FALSE}
81 {$setc TARGET_CPU_ARM := FALSE}
82 {$setc TARGET_CPU_ARM64 := FALSE}
83 {$setc TARGET_OS_MAC := TRUE}
84 {$setc TARGET_OS_IPHONE := FALSE}
85 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
86 {$setc TARGET_OS_EMBEDDED := FALSE}
87 {$elifc defined __ppc64__ and __ppc64__}
88 {$setc TARGET_CPU_PPC := FALSE}
89 {$setc TARGET_CPU_PPC64 := TRUE}
90 {$setc TARGET_CPU_X86 := FALSE}
91 {$setc TARGET_CPU_X86_64 := FALSE}
92 {$setc TARGET_CPU_ARM := FALSE}
93 {$setc TARGET_CPU_ARM64 := FALSE}
94 {$setc TARGET_OS_MAC := TRUE}
95 {$setc TARGET_OS_IPHONE := FALSE}
96 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
97 {$setc TARGET_OS_EMBEDDED := FALSE}
98 {$elifc defined __i386__ and __i386__}
99 {$setc TARGET_CPU_PPC := FALSE}
100 {$setc TARGET_CPU_PPC64 := FALSE}
101 {$setc TARGET_CPU_X86 := TRUE}
102 {$setc TARGET_CPU_X86_64 := FALSE}
103 {$setc TARGET_CPU_ARM := FALSE}
104 {$setc TARGET_CPU_ARM64 := FALSE}
105 {$ifc defined iphonesim}
106 {$setc TARGET_OS_MAC := FALSE}
107 {$setc TARGET_OS_IPHONE := TRUE}
108 {$setc TARGET_IPHONE_SIMULATOR := TRUE}
109 {$elsec}
110 {$setc TARGET_OS_MAC := TRUE}
111 {$setc TARGET_OS_IPHONE := FALSE}
112 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
113 {$endc}
114 {$setc TARGET_OS_EMBEDDED := FALSE}
115 {$elifc defined __x86_64__ and __x86_64__}
116 {$setc TARGET_CPU_PPC := FALSE}
117 {$setc TARGET_CPU_PPC64 := FALSE}
118 {$setc TARGET_CPU_X86 := FALSE}
119 {$setc TARGET_CPU_X86_64 := TRUE}
120 {$setc TARGET_CPU_ARM := FALSE}
121 {$setc TARGET_CPU_ARM64 := FALSE}
122 {$ifc defined iphonesim}
123 {$setc TARGET_OS_MAC := FALSE}
124 {$setc TARGET_OS_IPHONE := TRUE}
125 {$setc TARGET_IPHONE_SIMULATOR := TRUE}
126 {$elsec}
127 {$setc TARGET_OS_MAC := TRUE}
128 {$setc TARGET_OS_IPHONE := FALSE}
129 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
130 {$endc}
131 {$setc TARGET_OS_EMBEDDED := FALSE}
132 {$elifc defined __arm__ and __arm__}
133 {$setc TARGET_CPU_PPC := FALSE}
134 {$setc TARGET_CPU_PPC64 := FALSE}
135 {$setc TARGET_CPU_X86 := FALSE}
136 {$setc TARGET_CPU_X86_64 := FALSE}
137 {$setc TARGET_CPU_ARM := TRUE}
138 {$setc TARGET_CPU_ARM64 := FALSE}
139 {$setc TARGET_OS_MAC := FALSE}
140 {$setc TARGET_OS_IPHONE := TRUE}
141 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
142 {$setc TARGET_OS_EMBEDDED := TRUE}
143 {$elifc defined __arm64__ and __arm64__}
144 {$setc TARGET_CPU_PPC := FALSE}
145 {$setc TARGET_CPU_PPC64 := FALSE}
146 {$setc TARGET_CPU_X86 := FALSE}
147 {$setc TARGET_CPU_X86_64 := FALSE}
148 {$setc TARGET_CPU_ARM := FALSE}
149 {$setc TARGET_CPU_ARM64 := TRUE}
150 {$ifc defined ios}
151 {$setc TARGET_OS_MAC := FALSE}
152 {$setc TARGET_OS_IPHONE := TRUE}
153 {$setc TARGET_OS_EMBEDDED := TRUE}
154 {$elsec}
155 {$setc TARGET_OS_MAC := TRUE}
156 {$setc TARGET_OS_IPHONE := FALSE}
157 {$setc TARGET_OS_EMBEDDED := FALSE}
158 {$endc}
159 {$setc TARGET_IPHONE_SIMULATOR := FALSE}
160 {$elsec}
161 {$error __ppc__ nor __ppc64__ nor __i386__ nor __x86_64__ nor __arm__ nor __arm64__ is defined.}
162 {$endc}
163
164 {$ifc defined __LP64__ and __LP64__ }
165 {$setc TARGET_CPU_64 := TRUE}
166 {$elsec}
167 {$setc TARGET_CPU_64 := FALSE}
168 {$endc}
169
170 {$ifc defined FPC_BIG_ENDIAN}
171 {$setc TARGET_RT_BIG_ENDIAN := TRUE}
172 {$setc TARGET_RT_LITTLE_ENDIAN := FALSE}
173 {$elifc defined FPC_LITTLE_ENDIAN}
174 {$setc TARGET_RT_BIG_ENDIAN := FALSE}
175 {$setc TARGET_RT_LITTLE_ENDIAN := TRUE}
176 {$elsec}
177 {$error Neither FPC_BIG_ENDIAN nor FPC_LITTLE_ENDIAN are defined.}
178 {$endc}
179 {$setc ACCESSOR_CALLS_ARE_FUNCTIONS := TRUE}
180 {$setc CALL_NOT_IN_CARBON := FALSE}
181 {$setc OLDROUTINENAMES := FALSE}
182 {$setc OPAQUE_TOOLBOX_STRUCTS := TRUE}
183 {$setc OPAQUE_UPP_TYPES := TRUE}
184 {$setc OTCARBONAPPLICATION := TRUE}
185 {$setc OTKERNEL := FALSE}
186 {$setc PM_USE_SESSION_APIS := TRUE}
187 {$setc TARGET_API_MAC_CARBON := TRUE}
188 {$setc TARGET_API_MAC_OS8 := FALSE}
189 {$setc TARGET_API_MAC_OSX := TRUE}
190 {$setc TARGET_CARBON := TRUE}
191 {$setc TARGET_CPU_68K := FALSE}
192 {$setc TARGET_CPU_MIPS := FALSE}
193 {$setc TARGET_CPU_SPARC := FALSE}
194 {$setc TARGET_OS_UNIX := FALSE}
195 {$setc TARGET_OS_WIN32 := FALSE}
196 {$setc TARGET_RT_MAC_68881 := FALSE}
197 {$setc TARGET_RT_MAC_CFM := FALSE}
198 {$setc TARGET_RT_MAC_MACHO := TRUE}
199 {$setc TYPED_FUNCTION_POINTERS := TRUE}
200 {$setc TYPE_BOOL := FALSE}
201 {$setc TYPE_EXTENDED := FALSE}
202 {$setc TYPE_LONGLONG := TRUE}
203 uses MacTypes,CFBase,CFData,CFString,CFRunLoop,CFDate;
204 {$endc} {not MACOSALLINCLUDE}
205
206 {$ALIGN POWER}
207
208
209 type
210 CFSocketNativeHandle = SInt32;
211
212
213 type
214 CFSocketRef = ^__CFSocket; { an opaque type }
215 __CFSocket = record end;
216
217 { A CFSocket contains a native socket within a structure that can
218 be used to read from the socket in the background and make the data
219 thus read available using a runloop source. The callback used for
220 this may be of three types, as specified by the callBackTypes
221 argument when creating the CFSocket.
222
223 If kCFSocketReadCallBack is used, then data will not be
224 automatically read, but the callback will be called when data
225 is available to be read, or a new child socket is waiting to be
226 accepted.
227
228 If kCFSocketAcceptCallBack is used, then new child sockets will be
229 accepted and passed to the callback, with the data argument being
230 a pointer to a CFSocketNativeHandle. This is usable only with
231 connection rendezvous sockets.
232
233 If kCFSocketDataCallBack is used, then data will be read in chunks
234 in the background and passed to the callback, with the data argument
235 being a CFDataRef.
236
237 These three types are mutually exclusive, but any one of them may
238 have kCFSocketConnectCallBack added to it, if the socket will be
239 used to connect in the background. Connect in the background occurs
240 if CFSocketConnectToAddress is called with a negative timeout
241 value, in which case the call returns immediately, and a
242 kCFSocketConnectCallBack is generated when the connect finishes.
243 In this case the data argument is either NULL, or a pointer to
244 an SInt32 error code if the connect failed. kCFSocketConnectCallBack
245 will never be sent more than once for a given socket.
246
247 The callback types may also have kCFSocketWriteCallBack added to
248 them, if large amounts of data are to be sent rapidly over the
249 socket and notification is desired when there is space in the
250 kernel buffers so that the socket is writable again.
251
252 With a connection-oriented socket, if the connection is broken from the
253 other end, then one final kCFSocketReadCallBack or kCFSocketDataCallBack
254 will occur. In the case of kCFSocketReadCallBack, the underlying socket
255 will have 0 bytes available to read. In the case of kCFSocketDataCallBack,
256 the data argument will be a CFDataRef of length 0.
257
258 There are socket flags that may be set to control whether callbacks of
259 a given type are automatically reenabled after they are triggered, and
260 whether the underlying native socket will be closed when the CFSocket
261 is invalidated. By default read, accept, and data callbacks are
262 automatically reenabled; write callbacks are not, and connect callbacks
263 may not be, since they are sent once only. Be careful about automatically
264 reenabling read and write callbacks, since this implies that the
265 callbacks will be sent repeatedly if the socket remains readable or
266 writable respectively. Be sure to set these flags only for callbacks
267 that your CFSocket actually possesses; the result of setting them for
268 other callback types is undefined.
269
270 Individual callbacks may also be enabled and disabled manually, whether
271 they are automatically reenabled or not. If they are not automatically
272 reenabled, then they will need to be manually reenabled when the callback
273 is ready to be received again (and not sooner). Even if they are
274 automatically reenabled, there may be occasions when it will be useful
275 to be able to manually disable them temporarily and then reenable them.
276 Be sure to enable and disable only callbacks that your CFSocket actually
277 possesses; the result of enabling and disabling other callback types is
278 undefined.
279
280 By default the underlying native socket will be closed when the CFSocket
281 is invalidated, but it will not be if kCFSocketCloseOnInvalidate is
282 turned off. This can be useful in order to destroy a CFSocket but
283 continue to use the underlying native socket. The CFSocket must
284 still be invalidated when it will no longer be used. Do not in
285 either case close the underlying native socket without invalidating
286 the CFSocket.
287
288 Addresses are stored as CFDatas containing a struct sockaddr
289 appropriate for the protocol family; make sure that all fields are
290 filled in properly when passing in an address.
291
292 }
293
294 type
295 CFSocketError = CFIndex;
296 const
297 kCFSocketSuccess = 0;
298 kCFSocketError = -1;
299 kCFSocketTimeout = -2;
300
301 type
302 CFSocketSignature = record
303 protocolFamily: SInt32;
304 socketType: SInt32;
305 protocol: SInt32;
306 address: CFDataRef;
307 end;
308
309 type
310 CFSocketCallBackType = CFOptionFlags;
311 const
312 kCFSocketNoCallBack = 0;
313 kCFSocketReadCallBack = 1;
314 kCFSocketAcceptCallBack = 2;
315 kCFSocketDataCallBack = 3;
316 kCFSocketConnectCallBack = 4;
317 {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
318 kCFSocketWriteCallBack = 8;
319 {#endif}
320
321 {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
322 { Socket flags }
323 const
324 kCFSocketAutomaticallyReenableReadCallBack = 1;
325 kCFSocketAutomaticallyReenableAcceptCallBack = 2;
326 kCFSocketAutomaticallyReenableDataCallBack = 3;
327 kCFSocketAutomaticallyReenableWriteCallBack = 8;
328 {#if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED}
329 kCFSocketLeaveErrors = 64;
330 {#endif}
331 kCFSocketCloseOnInvalidate = 128; (* CF_AVAILABLE_STARTING(10_5, 2_0) *)
332 {#endif}
333
334 type
335 CFSocketCallBack = procedure( s: CFSocketRef; typ: CFSocketCallBackType; address: CFDataRef; data: {const} UnivPtr; info: UnivPtr );
336 { If the callback wishes to keep hold of address or data after the point that it returns, then it must copy them. }
337
338 type
339 CFSocketContext = record
340 version: CFIndex;
341 info: UnivPtr;
infonull342 retain: function( info: {const} UnivPtr ): UnivPtr;
343 release: procedure( info: {const} UnivPtr );
infonull344 copyDescription: function( info: {const} UnivPtr ): CFStringRef;
345 end;
346
CFSocketGetTypeIDnull347 function CFSocketGetTypeID: CFTypeID; external name '_CFSocketGetTypeID';
348
CFSocketCreatenull349 function CFSocketCreate( allocator: CFAllocatorRef; protocolFamily: SInt32; socketType: SInt32; protocol: SInt32; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreate';
CFSocketCreateWithNativenull350 function CFSocketCreateWithNative( allocator: CFAllocatorRef; sock: CFSocketNativeHandle; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreateWithNative';
CFSocketCreateWithSocketSignaturenull351 function CFSocketCreateWithSocketSignature( allocator: CFAllocatorRef; const (*var*) signature: CFSocketSignature; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext ): CFSocketRef; external name '_CFSocketCreateWithSocketSignature';
352 { CFSocketCreateWithSocketSignature() creates a socket of the requested type and binds its address (using CFSocketSetAddress()) to the requested address. If this fails, it returns NULL. }
CFSocketCreateConnectedToSocketSignaturenull353 function CFSocketCreateConnectedToSocketSignature( allocator: CFAllocatorRef; const (*var*) signature: CFSocketSignature; callBackTypes: CFOptionFlags; callout: CFSocketCallBack; const (*var*) context: CFSocketContext; timeout: CFTimeInterval ): CFSocketRef; external name '_CFSocketCreateConnectedToSocketSignature';
354 { CFSocketCreateConnectedToSocketSignature() creates a socket suitable for connecting to the requested type and address, and connects it (using CFSocketConnectToAddress()). If this fails, it returns NULL. }
355
CFSocketSetAddressnull356 function CFSocketSetAddress( s: CFSocketRef; address: CFDataRef ): CFSocketError; external name '_CFSocketSetAddress';
CFSocketConnectToAddressnull357 function CFSocketConnectToAddress( s: CFSocketRef; address: CFDataRef; timeout: CFTimeInterval ): CFSocketError; external name '_CFSocketConnectToAddress';
358 procedure CFSocketInvalidate( s: CFSocketRef ); external name '_CFSocketInvalidate';
359
CFSocketIsValidnull360 function CFSocketIsValid( s: CFSocketRef ): Boolean; external name '_CFSocketIsValid';
CFSocketCopyAddressnull361 function CFSocketCopyAddress( s: CFSocketRef ): CFDataRef; external name '_CFSocketCopyAddress';
CFSocketCopyPeerAddressnull362 function CFSocketCopyPeerAddress( s: CFSocketRef ): CFDataRef; external name '_CFSocketCopyPeerAddress';
363 procedure CFSocketGetContext( s: CFSocketRef; var context: CFSocketContext ); external name '_CFSocketGetContext';
CFSocketGetNativenull364 function CFSocketGetNative( s: CFSocketRef ): CFSocketNativeHandle; external name '_CFSocketGetNative';
365
CFSocketCreateRunLoopSourcenull366 function CFSocketCreateRunLoopSource( allocator: CFAllocatorRef; s: CFSocketRef; order: CFIndex ): CFRunLoopSourceRef; external name '_CFSocketCreateRunLoopSource';
367
368 {#if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED}
CFSocketGetSocketFlagsnull369 function CFSocketGetSocketFlags( s: CFSocketRef ): CFOptionFlags; external name '_CFSocketGetSocketFlags';
370 procedure CFSocketSetSocketFlags( s: CFSocketRef; flags: CFOptionFlags ); external name '_CFSocketSetSocketFlags';
371 procedure CFSocketDisableCallBacks( s: CFSocketRef; callBackTypes: CFOptionFlags ); external name '_CFSocketDisableCallBacks';
372 procedure CFSocketEnableCallBacks( s: CFSocketRef; callBackTypes: CFOptionFlags ); external name '_CFSocketEnableCallBacks';
373 {#endif}
374
375 { 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. }
CFSocketSendDatanull376 function CFSocketSendData( s: CFSocketRef; address: CFDataRef; data: CFDataRef; timeout: CFTimeInterval ): CFSocketError; external name '_CFSocketSendData';
377
378 { Generic name registry functionality (CFSocketRegisterValue,
379 CFSocketCopyRegisteredValue) allows the registration of any property
380 list type. Functions specific to CFSockets (CFSocketRegisterSocketData,
381 CFSocketCopyRegisteredSocketData) register a CFData containing the
382 components of a socket signature (protocol family, socket type,
383 protocol, and address). In each function the nameServerSignature
384 may be NULL, or any component of it may be 0, to use default values
385 (TCP, INADDR_LOOPBACK, port as set). Name registration servers might
386 not allow registration with other than TCP and INADDR_LOOPBACK.
387 The actual address of the server responding to a query may be obtained
388 by using the nameServerAddress argument. This address, the address
389 returned by CFSocketCopyRegisteredSocketSignature, and the value
390 returned by CFSocketCopyRegisteredValue must (if non-null) be released
391 by the caller. CFSocketUnregister removes any registration associated
392 with the specified name.
393 }
394
CFSocketRegisterValuenull395 function CFSocketRegisterValue( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; value: CFPropertyListRef ): CFSocketError; external name '_CFSocketRegisterValue';
CFSocketCopyRegisteredValuenull396 function CFSocketCopyRegisteredValue( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; var value: CFPropertyListRef; var nameServerAddress: CFDataRef ): CFSocketError; external name '_CFSocketCopyRegisteredValue';
397
CFSocketRegisterSocketSignaturenull398 function CFSocketRegisterSocketSignature( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; const (*var*) signature: CFSocketSignature ): CFSocketError; external name '_CFSocketRegisterSocketSignature';
CFSocketCopyRegisteredSocketSignaturenull399 function CFSocketCopyRegisteredSocketSignature( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef; var signature: CFSocketSignature; var nameServerAddress: CFDataRef ): CFSocketError; external name '_CFSocketCopyRegisteredSocketSignature';
400
CFSocketUnregisternull401 function CFSocketUnregister( const (*var*) nameServerSignature: CFSocketSignature; timeout: CFTimeInterval; name: CFStringRef ): CFSocketError; external name '_CFSocketUnregister';
402
403 procedure CFSocketSetDefaultNameRegistryPortNumber( port: UInt16 ); external name '_CFSocketSetDefaultNameRegistryPortNumber';
CFSocketGetDefaultNameRegistryPortNumbernull404 function CFSocketGetDefaultNameRegistryPortNumber: UInt16; external name '_CFSocketGetDefaultNameRegistryPortNumber';
405
406 { Constants used in name registry server communications }
407 var kCFSocketCommandKey: CFStringRef; external name '_kCFSocketCommandKey'; (* attribute const *)
408 var kCFSocketNameKey: CFStringRef; external name '_kCFSocketNameKey'; (* attribute const *)
409 var kCFSocketValueKey: CFStringRef; external name '_kCFSocketValueKey'; (* attribute const *)
410 var kCFSocketResultKey: CFStringRef; external name '_kCFSocketResultKey'; (* attribute const *)
411 var kCFSocketErrorKey: CFStringRef; external name '_kCFSocketErrorKey'; (* attribute const *)
412 var kCFSocketRegisterCommand: CFStringRef; external name '_kCFSocketRegisterCommand'; (* attribute const *)
413 var kCFSocketRetrieveCommand: CFStringRef; external name '_kCFSocketRetrieveCommand'; (* attribute const *)
414
415 {$ifc not defined MACOSALLINCLUDE or not MACOSALLINCLUDE}
416
417 end.
418 {$endc} {not MACOSALLINCLUDE}
419