]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kpi_socket.h
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / sys / kpi_socket.h
CommitLineData
91447636 1/*
8ad349bb 2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
91447636 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>
44
45struct timeval;
46
47/*!
48 @typedef sock_upcall
49
50 @discussion sock_upcall is used by a socket to notify an in kernel
51 client that data is waiting. Instead of making blocking calls in
52 the kernel, a client can specify an upcall which will be called
53 when data is available or the socket is ready for sending.
54
55 Calls to your upcall function are not serialized and may be
56 called concurrently from multiple threads in the kernel.
57
58 Your upcall function will be called when:
59
60 @param so A reference to the socket that's ready.
61 @param cookie The cookie passed in when the socket was created.
62 @param waitf Indicates whether or not it's safe to block.
63*/
64typedef void (*sock_upcall)(socket_t so, void* cookie, int waitf);
65
66/*!
67 @function sock_accept
68 @discussion Accepts an incoming connection on a socket. See 'man 2
69 accept' for more information. Allocating a socket in this manner
70 creates a socket with no associated file descriptor.
71 @param so The listening socket you'd like to accept a connection on.
72 @param from A pointer to a socket address that will be filled in
73 with the address the connection is from.
74 @param fromlen Maximum length of from.
75 @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If
76 MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are
77 no connections ready to be accepted. If MSG_USEUPCALL is set,
78 the created socket will use the same upcall function attached to
79 the original socket.
80 @param callback A notifier function to be called when an event
81 occurs on the socket. This may be NULL.
82 @param cookie A cookie passed directly to the callback.
83 @param new_so Upon success, *new_so will be a reference to a new
84 socket for tracking the connection.
85 @result 0 on success otherwise the errno error.
86 */
87errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen,
88 int flags, sock_upcall callback, void* cookie,
89 socket_t *new_so);
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 */
99errno_t sock_bind(socket_t so, const struct sockaddr *to);
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 */
115errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags);
116
117#ifdef KERNEL_PRIVATE
118/*!
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.
123
124 If you feel you need to use this function, please contact us to
125 explain why.
126
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 */
135errno_t sock_connectwait(socket_t so, const struct timeval *tv);
136#endif KERNEL_PRIVATE
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 */
147errno_t sock_getpeername(socket_t so, struct sockaddr *peername, int peernamelen);
148
149/*!
150 @function sock_getsockname
151 @discussion Retrieves the local address of a socket. See 'man 2
152 getsockname'.
153 @param so The socket.
154 @param sockname Storage for the local name.
155 @param socknamelen Length of storage for the socket name.
156 @result 0 on success otherwise the errno error.
157 */
158errno_t sock_getsockname(socket_t so, struct sockaddr *sockname, int socknamelen);
159
160/*!
161 @function sock_getsockopt
162 @discussion Retrieves a socket option. See 'man 2 getsockopt'.
163 @param so The socket.
164 @param level Level of the socket option.
165 @param optname The option name.
166 @param optval The option value.
167 @param optlen The length of optval, returns the actual length.
168 @result 0 on success otherwise the errno error.
169 */
170errno_t sock_getsockopt(socket_t so, int level, int optname, void *optval, int *optlen);
171
172/*!
173 @function sock_ioctl
174 @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'.
175 @param so The socket.
176 @param request The ioctl name.
177 @param argp The argument.
178 @result 0 on success otherwise the errno error.
179 */
180errno_t sock_ioctl(socket_t so, unsigned long request, void *argp);
181
182/*!
183 @function sock_setsockopt
184 @discussion Sets a socket option. See 'man 2 setsockopt'.
185 @param so The socket.
186 @param level Level of the socket option.
187 @param optname The option name.
188 @param optval The option value.
189 @param optlen The length of optval.
190 @result 0 on success otherwise the errno error.
191 */
192errno_t sock_setsockopt(socket_t so, int level, int optname, const void *optval, int optlen);
193
194/*!
195 @function sock_listen
196 @discussion Indicate that the socket should start accepting incoming
197 connections. See 'man 2 listen'.
198 @param so The socket.
199 @param backlog The maximum length of the queue of pending connections.
200 @result 0 on success otherwise the errno error.
201 */
202errno_t sock_listen(socket_t so, int backlog);
203
204/*!
205 @function sock_receive
206 @discussion Receive data from a socket. Similar to recvmsg. See 'man
207 2 recvmsg' for more information about receiving data.
208 @param so The socket.
209 @param msg The msg describing how the data should be received.
210 @param flags See 'man 2 recvmsg'.
211 @param recvdlen Number of bytes received, same as return value of
212 userland recvmsg.
213 @result 0 on success, EWOULDBLOCK if non-blocking and operation
214 would cause the thread to block, otherwise the errno error.
215 */
216errno_t sock_receive(socket_t so, struct msghdr *msg, int flags, size_t *recvdlen);
217
218/*!
219 @function sock_receivembuf
220 @discussion Receive data from a socket. Similar to sock_receive
221 though data is returned as a chain of mbufs. See 'man 2 recvmsg'
222 for more information about receiving data.
223 @param so The socket.
224 @param msg The msg describing how the data should be received. May
225 be NULL. The msg_iov is ignored.
226 @param data Upon return *data will be a reference to an mbuf chain
227 containing the data received. This eliminates copying the data
228 out of the mbufs. Caller is responsible for freeing the mbufs.
229 @param flags See 'man 2 recvmsg'.
230 @param recvlen Maximum number of bytes to receive in the mbuf chain.
231 Upon return, this value will be set to the number of bytes
232 received, same as return value of userland recvmsg.
233 @result 0 on success, EWOULDBLOCK if non-blocking and operation
234 would cause the thread to block, otherwise the errno error.
235 */
236errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data, int flags, size_t *recvlen);
237
238/*!
239 @function sock_send
240 @discussion Send data on a socket. Similar to sendmsg. See 'man 2
241 sendmsg' for more information about sending data.
242 @param so The socket.
243 @param msg The msg describing how the data should be sent. Any
244 pointers must point to data in the kernel.
245 @param flags See 'man 2 sendmsg'.
246 @param sentlen The number of bytes sent.
247 @result 0 on success, EWOULDBLOCK if non-blocking and operation
248 would cause the thread to block, otherwise the errno error.
249 */
250errno_t sock_send(socket_t so, const struct msghdr *msg, int flags, size_t *sentlen);
251
252/*!
253 @function sock_sendmbuf
254 @discussion Send data in an mbuf on a socket. Similar to sock_send
255 only the data to be sent is taken from the mbuf chain.
256 @param so The socket.
257 @param msg The msg describing how the data should be sent. The
258 msg_iov is ignored. msg may be NULL.
259 @param data The mbuf chain of data to send.
260 @param flags See 'man 2 sendmsg'.
261 @param sentlen The number of bytes sent.
262 @result 0 on success, EWOULDBLOCK if non-blocking and operation
263 would cause the thread to block, otherwise the errno error.
264 Regardless of return value, the mbuf chain 'data' will be freed.
265 */
266errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data, int flags, size_t *sentlen);
267
268/*!
269 @function sock_shutdown
270 @discussion Shutdown one or both directions of a connection. See
271 'man 2 shutdown' for more information.
272 @param so The socket.
273 @param how SHUT_RD - shutdown receive. SHUT_WR - shutdown send. SHUT_RDWR - shutdown both.
274 @result 0 on success otherwise the errno error.
275 */
276errno_t sock_shutdown(socket_t so, int how);
277
278/*!
279 @function sock_socket
280 @discussion Allocate a socket. Allocating a socket in this manner
281 creates a socket with no associated file descriptor. For more
282 information, see 'man 2 socket'.
283 @param domain The socket domain (PF_INET, etc...).
284 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...).
285 @param protocol The socket protocol.
286 @param callback A notifier function to be called when an event
287 occurs on the socket. This may be NULL.
288 @param cookie A cookie passed directly to the callback.
289 @param new_so Upon success, a reference to the new socket.
290 @result 0 on success otherwise the errno error.
291 */
292errno_t sock_socket(int domain, int type, int protocol, sock_upcall callback,
293 void* cookie, socket_t *new_so);
294
295/*!
296 @function sock_close
297 @discussion Close the socket.
298 @param so The socket to close. This should only ever be a socket
299 created with sock_socket. Closing a socket created in user space
300 using sock_close may leave a file descriptor pointing to the closed
301 socket, resulting in undefined behavior.
302 */
303void sock_close(socket_t so);
304
305/*!
306 @function sock_retain
307 @discussion Prevents the socket from closing
308 @param so The socket to close. Increment a retain count on the
309 socket, preventing it from being closed when sock_close is
310 called. This is used when a File Descriptor is passed (and
311 closed) from userland and the kext wants to keep ownership of
312 that socket. It is used in conjunction with
313 sock_release(socket_t so).
314 */
315void sock_retain(socket_t so);
316
317/*!
318 @function sock_release
319 @discussion Decrement the retain count and close the socket if the
320 retain count reaches zero.
321 @param so The socket to release. This is used to release ownership
322 on a socket acquired with sock_retain. When the last retain
323 count is reached, this will call sock_close to close the socket.
324 */
325void sock_release(socket_t so);
326
327/*!
328 @function sock_setpriv
329 @discussion Set the privileged bit in the socket. Allows for
330 operations that require root privileges.
331 @param so The socket on which to modify the SS_PRIV flag.
332 @param on Indicate whether or not the SS_PRIV flag should be set.
333 @result 0 on success otherwise the errno error.
334 */
335errno_t sock_setpriv(socket_t so, int on);
336
337/*!
338 @function sock_isconnected
339 @discussion Returns whether or not the socket is connected.
340 @param so The socket to check.
341 @result 0 - socket is not connected. 1 - socket is connected.
342 */
343int sock_isconnected(socket_t so);
344
345/*!
346 @function sock_isnonblocking
347 @discussion Returns whether or not the socket is non-blocking. In
348 the context of this KPI, non-blocking means that functions to
349 perform operations on a socket will not wait for completion.
350
351 To enable or disable blocking, use the FIONBIO ioctl. The
352 parameter is an int. If the int is zero, the socket will block.
353 If the parameter is non-zero, the socket will not block.
354 @result 0 - socket will block. 1 - socket will not block.
355 */
356int sock_isnonblocking(socket_t so);
357
358/*!
359 @function sock_gettype
360 @discussion Retrieves information about the socket. This is the same
361 information that was used to create the socket. If any of the
362 parameters following so are NULL, that information is not
363 retrieved.
364 @param so The socket to check.
365 @param domain The domain of the socket (PF_INET, etc...). May be NULL.
366 @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...). May be NULL.
367 @param protocol The socket protocol. May be NULL.
368 @result 0 on success otherwise the errno error.
369 */
370errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol);
371
372#ifdef KERNEL_PRIVATE
373/*!
374 @function sock_nointerrupt
375 @discussion Disables interrupt on socket buffers (sets SB_NOINTR on
376 send and receive socket buffers).
377 @param so The socket to modify.
378 @param on Indicate whether or not the SB_NOINTR flag should be set.
379 @result 0 on success otherwise the errno error.
380 */
381errno_t sock_nointerrupt(socket_t so, int on);
382#endif KERNEL_PRIVATE
383#endif __KPI_SOCKET__