]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kpi_socket.h
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / sys / kpi_socket.h
CommitLineData
91447636 1/*
b0d623f7 2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
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>
0c530ab8 42#include <sys/socket.h>
91447636 43
2d21ac55
A
44__BEGIN_DECLS
45
91447636
A
46struct timeval;
47
48/*!
49 @typedef sock_upcall
2d21ac55 50
91447636
A
51 @discussion sock_upcall is used by a socket to notify an in kernel
52 client that data is waiting. Instead of making blocking calls in
53 the kernel, a client can specify an upcall which will be called
54 when data is available or the socket is ready for sending.
2d21ac55 55
91447636
A
56 Calls to your upcall function are not serialized and may be
57 called concurrently from multiple threads in the kernel.
2d21ac55 58
91447636 59 Your upcall function will be called when:
2d21ac55 60
91447636
A
61 @param so A reference to the socket that's ready.
62 @param cookie The cookie passed in when the socket was created.
63 @param waitf Indicates whether or not it's safe to block.
64*/
b0d623f7 65typedef void (*sock_upcall)(socket_t so, void *cookie, int waitf);
91447636
A
66
67/*!
68 @function sock_accept
69 @discussion Accepts an incoming connection on a socket. See 'man 2
70 accept' for more information. Allocating a socket in this manner
71 creates a socket with no associated file descriptor.
72 @param so The listening socket you'd like to accept a connection on.
73 @param from A pointer to a socket address that will be filled in
74 with the address the connection is from.
75 @param fromlen Maximum length of from.
76 @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If
77 MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are
78 no connections ready to be accepted. If MSG_USEUPCALL is set,
79 the created socket will use the same upcall function attached to
80 the original socket.
81 @param callback A notifier function to be called when an event
82 occurs on the socket. This may be NULL.
83 @param cookie A cookie passed directly to the callback.
84 @param new_so Upon success, *new_so will be a reference to a new
85 socket for tracking the connection.
86 @result 0 on success otherwise the errno error.
87 */
b0d623f7
A
88extern errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen,
89 int flags, sock_upcall callback, void *cookie, socket_t *new_so);
91447636
A
90
91/*!
92 @function sock_bind
93 @discussion Binds a socket to a specific address. See 'man 2 bind'
94 for more information.
95 @param so The socket to be bound.
96 @param to The local address the socket should be bound to.
97 @result 0 on success otherwise the errno error.
98 */
b0d623f7 99extern errno_t sock_bind(socket_t so, const struct sockaddr *to);
91447636
A
100
101/*!
102 @function sock_connect
103 @discussion Initiates a connection on the socket. See 'man 2
104 connect' for more information.
105 @param so The socket to be connect.
106 @param to The remote address the socket should connect to.
107 @param flags Flags for connecting. The only flag supported so far is
108 MSG_DONTWAIT. MSG_DONTWAIT will perform a non-blocking connect.
109 sock_connect will return immediately with EINPROGRESS. The
110 upcall, if supplied, will be called when the connection is
111 completed.
112 @result 0 on success, EINPROGRESS for a non-blocking connect that
113 has not completed, otherwise the errno error.
114 */
b0d623f7 115extern errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags);
91447636
A
116
117#ifdef KERNEL_PRIVATE
2d21ac55 118/*
91447636
A
119 This function was added to support NFS. NFS does something funny,
120 setting a short timeout and checking to see if it should abort the
121 connect every two seconds. Ideally, NFS would use the upcall to be
122 notified when the connect is complete.
2d21ac55 123
91447636
A
124 If you feel you need to use this function, please contact us to
125 explain why.
2d21ac55 126
91447636
A
127 @function sock_connectwait
128 @discussion Allows a caller to wait on a socket connect.
129 @param so The socket being connected.
130 @param tv The amount of time to wait.
131 @result 0 on success otherwise the errno error. EINPROGRESS will be
132 returned if the connection did not complete in the timeout
133 specified.
134 */
b0d623f7 135extern errno_t sock_connectwait(socket_t so, const struct timeval *tv);
2d21ac55 136#endif /* KERNEL_PRIVATE */
91447636
A
137
138/*!
139 @function sock_getpeername
140 @discussion Retrieves the remote address of a connected socket. See
141 'man 2 getpeername'.
142 @param so The socket.
143 @param peername Storage for the peer name.
144 @param peernamelen Length of storage for the peer name.
145 @result 0 on success otherwise the errno error.
146 */
b0d623f7 147extern errno_t sock_getpeername(socket_t so, struct sockaddr *peername,
2d21ac55 148 int peernamelen);
91447636
A
149
150/*!
151 @function sock_getsockname
152 @discussion Retrieves the local address of a socket. See 'man 2
153 getsockname'.
154 @param so The socket.
155 @param sockname Storage for the local name.
156 @param socknamelen Length of storage for the socket name.
157 @result 0 on success otherwise the errno error.
158 */
b0d623f7 159extern errno_t sock_getsockname(socket_t so, struct sockaddr *sockname,
2d21ac55 160 int socknamelen);
91447636
A
161
162/*!
163 @function sock_getsockopt
164 @discussion Retrieves a socket option. See 'man 2 getsockopt'.
165 @param so The socket.
166 @param level Level of the socket option.
167 @param optname The option name.
168 @param optval The option value.
169 @param optlen The length of optval, returns the actual length.
170 @result 0 on success otherwise the errno error.
171 */
b0d623f7
A
172extern errno_t sock_getsockopt(socket_t so, int level, int optname,
173 void *optval, int *optlen);
91447636
A
174
175/*!
176 @function sock_ioctl
177 @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'.
178 @param so The socket.
179 @param request The ioctl name.
180 @param argp The argument.
181 @result 0 on success otherwise the errno error.
182 */
b0d623f7 183extern errno_t sock_ioctl(socket_t so, unsigned long request, void *argp);
91447636
A
184
185/*!
186 @function sock_setsockopt
187 @discussion Sets a socket option. See 'man 2 setsockopt'.
188 @param so The socket.
189 @param level Level of the socket option.
190 @param optname The option name.
191 @param optval The option value.
192 @param optlen The length of optval.
193 @result 0 on success otherwise the errno error.
194 */
b0d623f7
A
195extern errno_t sock_setsockopt(socket_t so, int level, int optname,
196 const void *optval, int optlen);
197
198#ifdef KERNEL_PRIVATE
199/*
200 This function was added to support AFP setting the traffic class
201 for a backup stream within a wireless LAN or over link-local address.
202
203 If you feel you need to use this function, please contact us to
204 explain why.
205
206 @function sock_settclassopt
207 @discussion Allows a caller to set the traffic class.
208 @param so The socket.
209 @param optval The option value.
210 @param optlen The length of optval.
211 @result 0 on success otherwise the errno error.
212 */
213extern errno_t sock_settclassopt(socket_t so, const void* optval, size_t optlen);
214
215/*
216 This function was added to support AFP getting the traffic class
217 set on a stream.
218
219 This is also a private API, please contact us if you need to use it.
220
221 @function sockgettclassopt
222 @discussion Allows a caller to get the traffic class.
223 @param so The socket.
224 @param optval The option value.
225 @param optlen The length of optval, returns the actual length.
226 @result 0 on success otherwise the errno error.
227*/
228extern errno_t sock_gettclassopt(socket_t so, void* optval, size_t* optlen);
d1ecb069
A
229
230#ifdef BSD_KERNEL_PRIVATE
231extern void socket_set_traffic_mgt_flags(socket_t so, u_int32_t flags);
232extern void socket_clear_traffic_mgt_flags(socket_t so, u_int32_t flags);
233#endif /* BSD_KERNEL_PRIVATE */
b0d623f7 234#endif
91447636
A
235
236/*!
237 @function sock_listen
238 @discussion Indicate that the socket should start accepting incoming
239 connections. See 'man 2 listen'.
240 @param so The socket.
241 @param backlog The maximum length of the queue of pending connections.
242 @result 0 on success otherwise the errno error.
243 */
b0d623f7 244extern errno_t sock_listen(socket_t so, int backlog);
91447636
A
245
246/*!
247 @function sock_receive
248 @discussion Receive data from a socket. Similar to recvmsg. See 'man
249 2 recvmsg' for more information about receiving data.
250 @param so The socket.
251 @param msg The msg describing how the data should be received.
252 @param flags See 'man 2 recvmsg'.
253 @param recvdlen Number of bytes received, same as return value of
254 userland recvmsg.
255 @result 0 on success, EWOULDBLOCK if non-blocking and operation
256 would cause the thread to block, otherwise the errno error.
257 */
b0d623f7 258extern errno_t sock_receive(socket_t so, struct msghdr *msg, int flags,
2d21ac55 259 size_t *recvdlen);
91447636
A
260
261/*!
262 @function sock_receivembuf
263 @discussion Receive data from a socket. Similar to sock_receive
264 though data is returned as a chain of mbufs. See 'man 2 recvmsg'
265 for more information about receiving data.
266 @param so The socket.
267 @param msg The msg describing how the data should be received. May
268 be NULL. The msg_iov is ignored.
269 @param data Upon return *data will be a reference to an mbuf chain
270 containing the data received. This eliminates copying the data
271 out of the mbufs. Caller is responsible for freeing the mbufs.
272 @param flags See 'man 2 recvmsg'.
273 @param recvlen Maximum number of bytes to receive in the mbuf chain.
274 Upon return, this value will be set to the number of bytes
275 received, same as return value of userland recvmsg.
276 @result 0 on success, EWOULDBLOCK if non-blocking and operation
277 would cause the thread to block, otherwise the errno error.
278 */
b0d623f7 279extern errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data,
2d21ac55 280 int flags, size_t *recvlen);
91447636
A
281
282/*!
283 @function sock_send
284 @discussion Send data on a socket. Similar to sendmsg. See 'man 2
285 sendmsg' for more information about sending data.
286 @param so The socket.
287 @param msg The msg describing how the data should be sent. Any
288 pointers must point to data in the kernel.
289 @param flags See 'man 2 sendmsg'.
290 @param sentlen The number of bytes sent.
291 @result 0 on success, EWOULDBLOCK if non-blocking and operation
292 would cause the thread to block, otherwise the errno error.
293 */
b0d623f7 294extern errno_t sock_send(socket_t so, const struct msghdr *msg, int flags,
2d21ac55 295 size_t *sentlen);
91447636
A
296
297/*!
298 @function sock_sendmbuf
299 @discussion Send data in an mbuf on a socket. Similar to sock_send
300 only the data to be sent is taken from the mbuf chain.
301 @param so The socket.
302 @param msg The msg describing how the data should be sent. The
303 msg_iov is ignored. msg may be NULL.
304 @param data The mbuf chain of data to send.
305 @param flags See 'man 2 sendmsg'.
306 @param sentlen The number of bytes sent.
307 @result 0 on success, EWOULDBLOCK if non-blocking and operation
308 would cause the thread to block, otherwise the errno error.
309 Regardless of return value, the mbuf chain 'data' will be freed.
310 */
b0d623f7 311extern errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data,
2d21ac55 312 int flags, size_t *sentlen);
91447636
A
313
314/*!
315 @function sock_shutdown
316 @discussion Shutdown one or both directions of a connection. See
317 'man 2 shutdown' for more information.
318 @param so The socket.
b0d623f7
A
319 @param how SHUT_RD - shutdown receive.
320 SHUT_WR - shutdown send.
321 SHUT_RDWR - shutdown both.
91447636
A
322 @result 0 on success otherwise the errno error.
323 */
b0d623f7 324extern errno_t sock_shutdown(socket_t so, int how);
91447636
A
325
326/*!
327 @function sock_socket
328 @discussion Allocate a socket. Allocating a socket in this manner
329 creates a socket with no associated file descriptor. For more
330 information, see 'man 2 socket'.
331 @param domain The socket domain (PF_INET, etc...).
332 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...).
333 @param protocol The socket protocol.
334 @param callback A notifier function to be called when an event
335 occurs on the socket. This may be NULL.
336 @param cookie A cookie passed directly to the callback.
337 @param new_so Upon success, a reference to the new socket.
338 @result 0 on success otherwise the errno error.
339 */
b0d623f7
A
340extern errno_t sock_socket(int domain, int type, int protocol,
341 sock_upcall callback, void *cookie, socket_t *new_so);
91447636
A
342
343/*!
344 @function sock_close
345 @discussion Close the socket.
346 @param so The socket to close. This should only ever be a socket
347 created with sock_socket. Closing a socket created in user space
b0d623f7
A
348 using sock_close may leave a file descriptor pointing to the
349 closed socket, resulting in undefined behavior.
91447636 350 */
b0d623f7 351extern void sock_close(socket_t so);
91447636 352
2d21ac55
A
353#ifdef KERNEL_PRIVATE
354/*
91447636
A
355 @function sock_retain
356 @discussion Prevents the socket from closing
357 @param so The socket to close. Increment a retain count on the
358 socket, preventing it from being closed when sock_close is
359 called. This is used when a File Descriptor is passed (and
360 closed) from userland and the kext wants to keep ownership of
361 that socket. It is used in conjunction with
362 sock_release(socket_t so).
363 */
b0d623f7 364extern void sock_retain(socket_t so);
91447636 365
2d21ac55 366/*
91447636
A
367 @function sock_release
368 @discussion Decrement the retain count and close the socket if the
369 retain count reaches zero.
370 @param so The socket to release. This is used to release ownership
371 on a socket acquired with sock_retain. When the last retain
372 count is reached, this will call sock_close to close the socket.
373 */
b0d623f7 374extern void sock_release(socket_t so);
2d21ac55 375#endif /* KERNEL_PRIVATE */
91447636
A
376
377/*!
378 @function sock_setpriv
379 @discussion Set the privileged bit in the socket. Allows for
380 operations that require root privileges.
381 @param so The socket on which to modify the SS_PRIV flag.
382 @param on Indicate whether or not the SS_PRIV flag should be set.
383 @result 0 on success otherwise the errno error.
384 */
b0d623f7 385extern errno_t sock_setpriv(socket_t so, int on);
91447636
A
386
387/*!
388 @function sock_isconnected
389 @discussion Returns whether or not the socket is connected.
390 @param so The socket to check.
391 @result 0 - socket is not connected. 1 - socket is connected.
392 */
b0d623f7 393extern int sock_isconnected(socket_t so);
91447636
A
394
395/*!
396 @function sock_isnonblocking
397 @discussion Returns whether or not the socket is non-blocking. In
398 the context of this KPI, non-blocking means that functions to
399 perform operations on a socket will not wait for completion.
2d21ac55 400
91447636
A
401 To enable or disable blocking, use the FIONBIO ioctl. The
402 parameter is an int. If the int is zero, the socket will block.
403 If the parameter is non-zero, the socket will not block.
404 @result 0 - socket will block. 1 - socket will not block.
405 */
b0d623f7 406extern int sock_isnonblocking(socket_t so);
91447636
A
407
408/*!
409 @function sock_gettype
410 @discussion Retrieves information about the socket. This is the same
411 information that was used to create the socket. If any of the
412 parameters following so are NULL, that information is not
413 retrieved.
414 @param so The socket to check.
b0d623f7
A
415 @param domain The domain of the socket (PF_INET, ...). May be NULL.
416 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, ...). May be NULL.
91447636
A
417 @param protocol The socket protocol. May be NULL.
418 @result 0 on success otherwise the errno error.
419 */
b0d623f7 420extern errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol);
91447636
A
421
422#ifdef KERNEL_PRIVATE
2d21ac55 423/*
91447636
A
424 @function sock_nointerrupt
425 @discussion Disables interrupt on socket buffers (sets SB_NOINTR on
426 send and receive socket buffers).
427 @param so The socket to modify.
428 @param on Indicate whether or not the SB_NOINTR flag should be set.
429 @result 0 on success otherwise the errno error.
430 */
b0d623f7 431extern errno_t sock_nointerrupt(socket_t so, int on);
2d21ac55
A
432
433/*
434 @function sock_getlistener
435 @discussion Retrieves the listening socket of a pre-accepted socket,
436 i.e. a socket which is still in the incomplete/completed list.
437 Once a socket has been accepted, the information pertaining
438 to its listener is no longer available. Therefore, modules
439 interested in finding out the listening socket should install
440 the appropriate socket filter callback (sf_attach) which gets
441 invoked prior to the socket being fully accepted, and call
442 this routine at such a time to obtain the listener. Callers
443 are guaranteed that the listener socket will not go away
444 during the sf_attach callback, and therefore the value is
445 safe to be used only in that callback context. Callers should
446 therefore take note that the listening socket's lock will be
447 held throughout the duration of the callback.
448 @param so The pre-accepted socket.
449 @result Non-NULL value which indicates the listening socket; otherwise,
450 NULL if the socket is not in the incomplete/completed list
451 of a listener.
452 */
b0d623f7 453extern socket_t sock_getlistener(socket_t so);
2d21ac55
A
454
455/*
456 @function sock_getaddr
457 @discussion Retrieves the local or remote address of a socket.
458 This is a composite of sock_getpeername and sock_getsockname,
459 except that the allocated socket address is returned to the
460 caller, and that the caller is reponsible for calling
461 sock_freeaddr once finished with it.
462 @param so The socket.
463 @param psockname Pointer to the storage for the socket name.
464 @param peername 0 for local address, and non-zero for peer address.
465 @result 0 on success otherwise the errno error.
466 */
b0d623f7
A
467extern errno_t sock_getaddr(socket_t so, struct sockaddr **psockname,
468 int peername);
2d21ac55
A
469
470/*
471 @function sock_freeaddr
472 @discussion Frees the socket address allocated by sock_getaddr.
473 @param sockname The socket name to be freed.
474 */
b0d623f7 475extern void sock_freeaddr(struct sockaddr *sockname);
2d21ac55
A
476#endif /* KERNEL_PRIVATE */
477
478__END_DECLS
479#endif /* __KPI_SOCKET__ */