]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/kpi_socket.h
f65b17472c8622eaa2aa7c3ee977ca096ecfe141
[apple/xnu.git] / bsd / sys / kpi_socket.h
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*!
29 @header kpi_socket.h
30 This header defines an API for creating and interacting with sockets
31 in the kernel. It is possible to create sockets in the kernel
32 without an associated file descriptor. In some cases, a reference to
33 the socket may be known while the file descriptor is not. These
34 functions can be used for interacting with sockets in the kernel.
35 The API is similar to the user space socket API.
36 */
37 #ifndef __KPI_SOCKET__
38 #define __KPI_SOCKET__
39
40 #include <sys/types.h>
41 #include <sys/kernel_types.h>
42
43 struct timeval;
44
45 /*!
46 @typedef sock_upcall
47
48 @discussion sock_upcall is used by a socket to notify an in kernel
49 client that data is waiting. Instead of making blocking calls in
50 the kernel, a client can specify an upcall which will be called
51 when data is available or the socket is ready for sending.
52
53 Calls to your upcall function are not serialized and may be
54 called concurrently from multiple threads in the kernel.
55
56 Your upcall function will be called when:
57
58 @param so A reference to the socket that's ready.
59 @param cookie The cookie passed in when the socket was created.
60 @param waitf Indicates whether or not it's safe to block.
61 */
62 typedef void (*sock_upcall)(socket_t so, void* cookie, int waitf);
63
64 /*!
65 @function sock_accept
66 @discussion Accepts an incoming connection on a socket. See 'man 2
67 accept' for more information. Allocating a socket in this manner
68 creates a socket with no associated file descriptor.
69 @param so The listening socket you'd like to accept a connection on.
70 @param from A pointer to a socket address that will be filled in
71 with the address the connection is from.
72 @param fromlen Maximum length of from.
73 @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If
74 MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are
75 no connections ready to be accepted. If MSG_USEUPCALL is set,
76 the created socket will use the same upcall function attached to
77 the original socket.
78 @param callback A notifier function to be called when an event
79 occurs on the socket. This may be NULL.
80 @param cookie A cookie passed directly to the callback.
81 @param new_so Upon success, *new_so will be a reference to a new
82 socket for tracking the connection.
83 @result 0 on success otherwise the errno error.
84 */
85 errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen,
86 int flags, sock_upcall callback, void* cookie,
87 socket_t *new_so);
88
89 /*!
90 @function sock_bind
91 @discussion Binds a socket to a specific address. See 'man 2 bind'
92 for more information.
93 @param so The socket to be bound.
94 @param to The local address the socket should be bound to.
95 @result 0 on success otherwise the errno error.
96 */
97 errno_t sock_bind(socket_t so, const struct sockaddr *to);
98
99 /*!
100 @function sock_connect
101 @discussion Initiates a connection on the socket. See 'man 2
102 connect' for more information.
103 @param so The socket to be connect.
104 @param to The remote address the socket should connect to.
105 @param flags Flags for connecting. The only flag supported so far is
106 MSG_DONTWAIT. MSG_DONTWAIT will perform a non-blocking connect.
107 sock_connect will return immediately with EINPROGRESS. The
108 upcall, if supplied, will be called when the connection is
109 completed.
110 @result 0 on success, EINPROGRESS for a non-blocking connect that
111 has not completed, otherwise the errno error.
112 */
113 errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags);
114
115 #ifdef KERNEL_PRIVATE
116 /*!
117 This function was added to support NFS. NFS does something funny,
118 setting a short timeout and checking to see if it should abort the
119 connect every two seconds. Ideally, NFS would use the upcall to be
120 notified when the connect is complete.
121
122 If you feel you need to use this function, please contact us to
123 explain why.
124
125 @function sock_connectwait
126 @discussion Allows a caller to wait on a socket connect.
127 @param so The socket being connected.
128 @param tv The amount of time to wait.
129 @result 0 on success otherwise the errno error. EINPROGRESS will be
130 returned if the connection did not complete in the timeout
131 specified.
132 */
133 errno_t sock_connectwait(socket_t so, const struct timeval *tv);
134 #endif KERNEL_PRIVATE
135
136 /*!
137 @function sock_getpeername
138 @discussion Retrieves the remote address of a connected socket. See
139 'man 2 getpeername'.
140 @param so The socket.
141 @param peername Storage for the peer name.
142 @param peernamelen Length of storage for the peer name.
143 @result 0 on success otherwise the errno error.
144 */
145 errno_t sock_getpeername(socket_t so, struct sockaddr *peername, int peernamelen);
146
147 /*!
148 @function sock_getsockname
149 @discussion Retrieves the local address of a socket. See 'man 2
150 getsockname'.
151 @param so The socket.
152 @param sockname Storage for the local name.
153 @param socknamelen Length of storage for the socket name.
154 @result 0 on success otherwise the errno error.
155 */
156 errno_t sock_getsockname(socket_t so, struct sockaddr *sockname, int socknamelen);
157
158 /*!
159 @function sock_getsockopt
160 @discussion Retrieves a socket option. See 'man 2 getsockopt'.
161 @param so The socket.
162 @param level Level of the socket option.
163 @param optname The option name.
164 @param optval The option value.
165 @param optlen The length of optval, returns the actual length.
166 @result 0 on success otherwise the errno error.
167 */
168 errno_t sock_getsockopt(socket_t so, int level, int optname, void *optval, int *optlen);
169
170 /*!
171 @function sock_ioctl
172 @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'.
173 @param so The socket.
174 @param request The ioctl name.
175 @param argp The argument.
176 @result 0 on success otherwise the errno error.
177 */
178 errno_t sock_ioctl(socket_t so, unsigned long request, void *argp);
179
180 /*!
181 @function sock_setsockopt
182 @discussion Sets a socket option. See 'man 2 setsockopt'.
183 @param so The socket.
184 @param level Level of the socket option.
185 @param optname The option name.
186 @param optval The option value.
187 @param optlen The length of optval.
188 @result 0 on success otherwise the errno error.
189 */
190 errno_t sock_setsockopt(socket_t so, int level, int optname, const void *optval, int optlen);
191
192 /*!
193 @function sock_listen
194 @discussion Indicate that the socket should start accepting incoming
195 connections. See 'man 2 listen'.
196 @param so The socket.
197 @param backlog The maximum length of the queue of pending connections.
198 @result 0 on success otherwise the errno error.
199 */
200 errno_t sock_listen(socket_t so, int backlog);
201
202 /*!
203 @function sock_receive
204 @discussion Receive data from a socket. Similar to recvmsg. See 'man
205 2 recvmsg' for more information about receiving data.
206 @param so The socket.
207 @param msg The msg describing how the data should be received.
208 @param flags See 'man 2 recvmsg'.
209 @param recvdlen Number of bytes received, same as return value of
210 userland recvmsg.
211 @result 0 on success, EWOULDBLOCK if non-blocking and operation
212 would cause the thread to block, otherwise the errno error.
213 */
214 errno_t sock_receive(socket_t so, struct msghdr *msg, int flags, size_t *recvdlen);
215
216 /*!
217 @function sock_receivembuf
218 @discussion Receive data from a socket. Similar to sock_receive
219 though data is returned as a chain of mbufs. See 'man 2 recvmsg'
220 for more information about receiving data.
221 @param so The socket.
222 @param msg The msg describing how the data should be received. May
223 be NULL. The msg_iov is ignored.
224 @param data Upon return *data will be a reference to an mbuf chain
225 containing the data received. This eliminates copying the data
226 out of the mbufs. Caller is responsible for freeing the mbufs.
227 @param flags See 'man 2 recvmsg'.
228 @param recvlen Maximum number of bytes to receive in the mbuf chain.
229 Upon return, this value will be set to the number of bytes
230 received, same as return value of userland recvmsg.
231 @result 0 on success, EWOULDBLOCK if non-blocking and operation
232 would cause the thread to block, otherwise the errno error.
233 */
234 errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data, int flags, size_t *recvlen);
235
236 /*!
237 @function sock_send
238 @discussion Send data on a socket. Similar to sendmsg. See 'man 2
239 sendmsg' for more information about sending data.
240 @param so The socket.
241 @param msg The msg describing how the data should be sent. Any
242 pointers must point to data in the kernel.
243 @param flags See 'man 2 sendmsg'.
244 @param sentlen The number of bytes sent.
245 @result 0 on success, EWOULDBLOCK if non-blocking and operation
246 would cause the thread to block, otherwise the errno error.
247 */
248 errno_t sock_send(socket_t so, const struct msghdr *msg, int flags, size_t *sentlen);
249
250 /*!
251 @function sock_sendmbuf
252 @discussion Send data in an mbuf on a socket. Similar to sock_send
253 only the data to be sent is taken from the mbuf chain.
254 @param so The socket.
255 @param msg The msg describing how the data should be sent. The
256 msg_iov is ignored. msg may be NULL.
257 @param data The mbuf chain of data to send.
258 @param flags See 'man 2 sendmsg'.
259 @param sentlen The number of bytes sent.
260 @result 0 on success, EWOULDBLOCK if non-blocking and operation
261 would cause the thread to block, otherwise the errno error.
262 Regardless of return value, the mbuf chain 'data' will be freed.
263 */
264 errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data, int flags, size_t *sentlen);
265
266 /*!
267 @function sock_shutdown
268 @discussion Shutdown one or both directions of a connection. See
269 'man 2 shutdown' for more information.
270 @param so The socket.
271 @param how SHUT_RD - shutdown receive. SHUT_WR - shutdown send. SHUT_RDWR - shutdown both.
272 @result 0 on success otherwise the errno error.
273 */
274 errno_t sock_shutdown(socket_t so, int how);
275
276 /*!
277 @function sock_socket
278 @discussion Allocate a socket. Allocating a socket in this manner
279 creates a socket with no associated file descriptor. For more
280 information, see 'man 2 socket'.
281 @param domain The socket domain (PF_INET, etc...).
282 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...).
283 @param protocol The socket protocol.
284 @param callback A notifier function to be called when an event
285 occurs on the socket. This may be NULL.
286 @param cookie A cookie passed directly to the callback.
287 @param new_so Upon success, a reference to the new socket.
288 @result 0 on success otherwise the errno error.
289 */
290 errno_t sock_socket(int domain, int type, int protocol, sock_upcall callback,
291 void* cookie, socket_t *new_so);
292
293 /*!
294 @function sock_close
295 @discussion Close the socket.
296 @param so The socket to close. This should only ever be a socket
297 created with sock_socket. Closing a socket created in user space
298 using sock_close may leave a file descriptor pointing to the closed
299 socket, resulting in undefined behavior.
300 */
301 void sock_close(socket_t so);
302
303 /*!
304 @function sock_retain
305 @discussion Prevents the socket from closing
306 @param so The socket to close. Increment a retain count on the
307 socket, preventing it from being closed when sock_close is
308 called. This is used when a File Descriptor is passed (and
309 closed) from userland and the kext wants to keep ownership of
310 that socket. It is used in conjunction with
311 sock_release(socket_t so).
312 */
313 void sock_retain(socket_t so);
314
315 /*!
316 @function sock_release
317 @discussion Decrement the retain count and close the socket if the
318 retain count reaches zero.
319 @param so The socket to release. This is used to release ownership
320 on a socket acquired with sock_retain. When the last retain
321 count is reached, this will call sock_close to close the socket.
322 */
323 void sock_release(socket_t so);
324
325 /*!
326 @function sock_setpriv
327 @discussion Set the privileged bit in the socket. Allows for
328 operations that require root privileges.
329 @param so The socket on which to modify the SS_PRIV flag.
330 @param on Indicate whether or not the SS_PRIV flag should be set.
331 @result 0 on success otherwise the errno error.
332 */
333 errno_t sock_setpriv(socket_t so, int on);
334
335 /*!
336 @function sock_isconnected
337 @discussion Returns whether or not the socket is connected.
338 @param so The socket to check.
339 @result 0 - socket is not connected. 1 - socket is connected.
340 */
341 int sock_isconnected(socket_t so);
342
343 /*!
344 @function sock_isnonblocking
345 @discussion Returns whether or not the socket is non-blocking. In
346 the context of this KPI, non-blocking means that functions to
347 perform operations on a socket will not wait for completion.
348
349 To enable or disable blocking, use the FIONBIO ioctl. The
350 parameter is an int. If the int is zero, the socket will block.
351 If the parameter is non-zero, the socket will not block.
352 @result 0 - socket will block. 1 - socket will not block.
353 */
354 int sock_isnonblocking(socket_t so);
355
356 /*!
357 @function sock_gettype
358 @discussion Retrieves information about the socket. This is the same
359 information that was used to create the socket. If any of the
360 parameters following so are NULL, that information is not
361 retrieved.
362 @param so The socket to check.
363 @param domain The domain of the socket (PF_INET, etc...). May be NULL.
364 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...). May be NULL.
365 @param protocol The socket protocol. May be NULL.
366 @result 0 on success otherwise the errno error.
367 */
368 errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol);
369
370 #ifdef KERNEL_PRIVATE
371 /*!
372 @function sock_nointerrupt
373 @discussion Disables interrupt on socket buffers (sets SB_NOINTR on
374 send and receive socket buffers).
375 @param so The socket to modify.
376 @param on Indicate whether or not the SB_NOINTR flag should be set.
377 @result 0 on success otherwise the errno error.
378 */
379 errno_t sock_nointerrupt(socket_t so, int on);
380 #endif KERNEL_PRIVATE
381 #endif __KPI_SOCKET__