Core Foundation Sockets  v0.2(28.0)
 All Classes Files Functions Variables Properties
CFSocket.h
Go to the documentation of this file.
1 // CFSockets CFSocket.h
2 //
3 // Copyright © 2009–2013, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the “Software”), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import <Foundation/Foundation.h>
26 
27 @class CFSocket;
28 @class CFStreamPair;
29 
30 @protocol CFSocketDelegate<NSObject>
31 @optional
32 
33 - (void)socket:(CFSocket *)socket acceptNativeHandle:(NSSocketNativeHandle)nativeHandle;
34 - (void)socket:(CFSocket *)socket acceptStreamPair:(CFStreamPair *)streamPair;
35 
36 @end
37 
38 /**
39  * Wraps Core Foundation's sockets in Objective-C clothing.
40  *
41  * Handles ARC memory management issues for a socket and implements a
42  * delegation protocol. As an object, you can sub-class the socket. It papers
43  * over the toll-free bridging requirements necessary for interactions between
44  * Core Foundation and Foundation. Start by initialising a socket, either an
45  * explicit IPv6 or IPv4 TCP socket, or whichever version of TCP socket the
46  * system makes available to you. Note that IPv4 clients can also access IPv6
47  * sockets.
48  *
49  * Note, you can have an object class called CFSocket; it does not clash with
50  * Apple's Core Foundation C-based socket functions, externals and constants
51  * because those exist in the C name space, while CFSocket here exists in the
52  * Objective-C name space. They do not collide.
53  */
54 @interface CFSocket : NSObject
55 {
56  CFSocketRef _socket;
57  CFRunLoopSourceRef _runLoopSource;
58 }
59 
60 @property(weak, NS_NONATOMIC_IOSONLY) id<CFSocketDelegate> delegate;
61 
62 /**
63  * Designated initialiser.
64  *
65  * Initialisers also create the underlying Core Foundation socket. You
66  * cannot have a partially initialised Objective-C socket. When socket creation
67  * fails, initialisation fails also. All socket initialisers follow this
68  * pattern. Hence, you cannot initialise a socket with a `NULL` socket
69  * reference. In such cases, the initialiser answers `nil`.
70  *
71  * This approach creates a slight quandary. Creating a Core Foundation socket
72  * requires a socket context. The context needs to retain a bridging reference
73  * to `self`, the Objective-C object encapsulating the socket. Otherwise, the
74  * socket call-back function cannot springboard from C to Objective-C when
75  * call-backs trigger. When the initialiser returns successfully however, the
76  * answer overwrites `self`. What if `self` changes? If it changes to `nil`,
77  * no problem. But what if it changes to some other pointer address?
78  *
79  * TODO: Add more initialisers; specifically, socket signature initialisers.
80  *
81  * @param socket Reference to a CFSocket object.
82  */
83 - (id)initWithSocketRef:(CFSocketRef)socket;
84 
85 - (id)initWithProtocolFamily:(int)family socketType:(int)type protocol:(int)protocol;
86 - (id)initForTCPv6;
87 - (id)initForTCPv4;
88 
89 /**
90  * Instantiates and creates a TCP socket using Internet Protocol version
91  * 6 if available, else tries version 4.
92  *
93  * Starts by creating a socket for Internet Protocol version 6. This
94  * also supports IPv4 clients via IPv4-mapped IPv6 addresses. Falls back to IPv4
95  * only when IPv6 fails.
96  *
97  * @result Answers a TCP socket, version 6 or 4; returns `nil` if the socket
98  * fails to create.
99  */
100 - (id)initForTCP;
101 
102 /**
103  * Initialises using a native socket handle.
104  * @param nativeHandle Platform-specific native socket handle.
105  */
106 - (id)initWithNativeHandle:(NSSocketNativeHandle)nativeHandle;
107 
108 /**
109  * Binds an address to a socket.
110  *
111  * Despite the innocuous-sounding method name, this method irreversibly
112  * binds the socket; assuming success. Do this early when constructing a
113  * socket. Be aware that accessing the address also binds the socket, if not
114  * already bound. You cannot therefore subsequently bind it. If you want to bind
115  * to a specific port, do so by setting the socket address _before_ asking for
116  * the address; that is, before using the getter method.
117  */
118 - (BOOL)setAddress:(NSData *)addressData error:(NSError **)outError;
119 
120 - (BOOL)connectToAddress:(NSData *)addressData timeout:(NSTimeInterval)timeout error:(NSError **)outError;
121 
122 - (void)invalidate;
123 - (BOOL)isValid;
124 - (NSData *)address;
125 - (NSData *)peerAddress;
126 - (NSSocketNativeHandle)nativeHandle;
127 - (BOOL)setReuseAddressOption:(BOOL)flag;
128 
129 /**
130  * Answers the socket address family.
131  *
132  * For Internet-based sockets, answers either `AF_INET6` or
133  * `AF_INET`. The latter for IP version 4 addresses. Note, protocol and address
134  * family are one and the same for Internet addresses. Protocol families are
135  * defined in terms of their address family; the `PF_` equals its corresponding
136  * `AF_` manifest constant.
137  *
138  * You can use this for polymorphic behaviour. If behaviour depends on a
139  * particular kind of socket, you can ask this method for the underlying address
140  * family and respond accordingly. This method differs from -address which binds
141  * the address first. The implementation here obtains the address family
142  * non-destructively; socket state remains unchanged.
143  *
144  * @result Answers the socket address family, or `AF_MAX` when an error
145  * occurs. On error, standard library `errno` value indicates the problem.
146  */
147 - (int)addressFamily;
148 
149 /**
150  * Answers the port number.
151  *
152  * The answer depends on the socket's address family: version 4 or
153  * 6. Handles the network-to-host byte ordering. The resulting integer has
154  * correct ordering for the host machine.
155  */
156 - (int)port;
157 
158 - (void)addToCurrentRunLoopForCommonModes;
159 - (void)removeFromCurrentRunLoopForCommonModes;
160 - (void)disableAcceptCallBack;
161 - (void)enableAcceptCallBack;
162 
163 /**
164  * Handles the socket _accept_ call-back behaviour.
165  *
166  * Executes when Next Step meets Core Foundation. The argument specifies a
167  * native socket handle, an integer defining the Unix socket descriptor.
168  *
169  * Exists to allow for optional overriding. You do not need to deploy
170  * the delegate protocol if your sub-class handles "accept native handle" events
171  * directly; though delegation usually works best.
172  *
173  * @param nativeHandle Platform-specific native socket handle.
174  */
175 - (void)acceptNativeHandle:(NSSocketNativeHandle)nativeHandle;
176 
177 @end
178 
179 extern NSString *const CFSocketErrorDomain;
180 
181 void __CFSocketCallOut(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info);