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