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