]> git.saurik.com Git - apple/xnu.git/blame - bsd/sys/kpi_socket.h
xnu-7195.50.7.100.1.tar.gz
[apple/xnu.git] / bsd / sys / kpi_socket.h
CommitLineData
91447636 1/*
5ba3f43e 2 * Copyright (c) 2008-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 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.
39236c6e 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.
39236c6e 17 *
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
91447636
A
27 */
28/*!
0a7de745
A
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.
91447636
A
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
cb323159
A
44#ifndef PRIVATE
45#include <Availability.h>
46#define __NKE_API_DEPRECATED __API_DEPRECATED("Network Kernel Extension KPI is deprecated", macos(10.4, 10.15))
47#else
48#define __NKE_API_DEPRECATED
49#endif /* PRIVATE */
50
2d21ac55
A
51__BEGIN_DECLS
52
91447636
A
53struct timeval;
54
55/*!
0a7de745
A
56 * @typedef sock_upcall
57 *
58 * @discussion sock_upcall is used by a socket to notify an in kernel
59 * client that data is waiting. Instead of making blocking calls in
60 * the kernel, a client can specify an upcall which will be called
61 * when data is available or the socket is ready for sending.
62 *
63 * Calls to your upcall function are not serialized and may be
64 * called concurrently from multiple threads in the kernel.
65 *
66 * Your upcall function will be called:
67 * when there is data more than the low water mark for reading,
68 * or when there is space for a write,
69 * or when there is a connection to accept,
70 * or when a socket is connected,
71 * or when a socket is closed or disconnected
72 *
73 * @param so A reference to the socket that's ready.
74 * @param cookie The cookie passed in when the socket was created.
75 * @param waitf Indicates whether or not it's safe to block.
76 */
b0d623f7 77typedef void (*sock_upcall)(socket_t so, void *cookie, int waitf);
91447636 78
39236c6e
A
79#ifdef KERNEL_PRIVATE
80/*!
0a7de745
A
81 * @typedef sock_evupcall
82 *
83 * @discussion sock_evupcall is used by a socket to notify an in kernel
84 * client when an event occurs. Instead of making blocking calls in
85 * the kernel, a client can specify an upcall which will be called
86 * when an event status is available.
87 * @param so A reference to the socket that's ready.
88 * @param cookie The cookie passed in when the socket was created.
89 * @param event Indicates the event as defined by SO_FILT_HINT_*
90 */
f427ee49 91typedef void (*sock_evupcall)(socket_t so, void *cookie, long event);
39236c6e
A
92#endif /* KERNEL_PRIVATE */
93
91447636 94/*!
0a7de745
A
95 * @function sock_accept
96 * @discussion Accepts an incoming connection on a socket. See 'man 2
97 * accept' for more information. Allocating a socket in this manner
98 * creates a socket with no associated file descriptor.
99 * @param so The listening socket you'd like to accept a connection on.
100 * @param from A pointer to a socket address that will be filled in
101 * with the address the connection is from.
102 * @param fromlen Maximum length of from.
103 * @param flags Supports MSG_DONTWAIT and MSG_USEUPCALL. If
104 * MSG_DONTWAIT is set, accept will return EWOULDBLOCK if there are
105 * no connections ready to be accepted. If MSG_USEUPCALL is set,
106 * the created socket will use the same upcall function attached to
107 * the original socket.
108 * @param callback A notifier function to be called when an event
109 * occurs on the socket. This may be NULL.
110 * @param cookie A cookie passed directly to the callback.
111 * @param new_so Upon success, *new_so will be a reference to a new
112 * socket for tracking the connection.
113 * @result 0 on success otherwise the errno error.
91447636 114 */
5ba3f43e
A
115#ifdef KERNEL_PRIVATE
116extern errno_t sock_accept_internal(socket_t so, struct sockaddr *from, int fromlen,
117 int flags, sock_upcall callback, void *cookie, socket_t *new_so);
118
0a7de745 119#define sock_accept(so, from, fromlen, flags, callback, cookie, new_so) \
5ba3f43e
A
120 sock_accept_internal((so), (from), (fromlen), (flags), (callback), \
121 (cookie), (new_so))
122#else
b0d623f7 123extern errno_t sock_accept(socket_t so, struct sockaddr *from, int fromlen,
cb323159
A
124 int flags, sock_upcall callback, void *cookie, socket_t *new_so)
125__NKE_API_DEPRECATED;
5ba3f43e 126#endif /* KERNEL_PRIVATE */
91447636
A
127
128/*!
0a7de745
A
129 * @function sock_bind
130 * @discussion Binds a socket to a specific address. See 'man 2 bind'
131 * for more information.
132 * @param so The socket to be bound.
133 * @param to The local address the socket should be bound to.
134 * @result 0 on success otherwise the errno error.
91447636 135 */
cb323159
A
136extern errno_t sock_bind(socket_t so, const struct sockaddr *to)
137__NKE_API_DEPRECATED;
91447636
A
138
139/*!
0a7de745
A
140 * @function sock_connect
141 * @discussion Initiates a connection on the socket. See 'man 2
142 * connect' for more information.
143 * @param so The socket to be connect.
144 * @param to The remote address the socket should connect to.
145 * @param flags Flags for connecting. The only flag supported so far is
146 * MSG_DONTWAIT. MSG_DONTWAIT will perform a non-blocking connect.
147 * sock_connect will return immediately with EINPROGRESS. The
148 * upcall, if supplied, will be called when the connection is
149 * completed.
150 * @result 0 on success, EINPROGRESS for a non-blocking connect that
151 * has not completed, otherwise the errno error.
91447636 152 */
cb323159
A
153extern errno_t sock_connect(socket_t so, const struct sockaddr *to, int flags)
154__NKE_API_DEPRECATED;
91447636
A
155
156#ifdef KERNEL_PRIVATE
2d21ac55 157/*
0a7de745
A
158 * This function was added to support NFS. NFS does something funny,
159 * setting a short timeout and checking to see if it should abort the
160 * connect every two seconds. Ideally, NFS would use the upcall to be
161 * notified when the connect is complete.
162 *
163 * If you feel you need to use this function, please contact us to
164 * explain why.
165 *
166 * @function sock_connectwait
167 * @discussion Allows a caller to wait on a socket connect.
168 * @param so The socket being connected.
169 * @param tv The amount of time to wait.
170 * @result 0 on success otherwise the errno error. EINPROGRESS will be
171 * returned if the connection did not complete in the timeout
172 * specified.
91447636 173 */
b0d623f7 174extern errno_t sock_connectwait(socket_t so, const struct timeval *tv);
2d21ac55 175#endif /* KERNEL_PRIVATE */
91447636
A
176
177/*!
0a7de745
A
178 * @function sock_getpeername
179 * @discussion Retrieves the remote address of a connected socket. See
180 * 'man 2 getpeername'.
181 * @param so The socket.
182 * @param peername Storage for the peer name.
183 * @param peernamelen Length of storage for the peer name.
184 * @result 0 on success otherwise the errno error.
91447636 185 */
b0d623f7 186extern errno_t sock_getpeername(socket_t so, struct sockaddr *peername,
cb323159
A
187 int peernamelen)
188__NKE_API_DEPRECATED;
91447636
A
189
190/*!
0a7de745
A
191 * @function sock_getsockname
192 * @discussion Retrieves the local address of a socket. See 'man 2
193 * getsockname'.
194 * @param so The socket.
195 * @param sockname Storage for the local name.
196 * @param socknamelen Length of storage for the socket name.
197 * @result 0 on success otherwise the errno error.
91447636 198 */
b0d623f7 199extern errno_t sock_getsockname(socket_t so, struct sockaddr *sockname,
cb323159
A
200 int socknamelen)
201__NKE_API_DEPRECATED;
91447636
A
202
203/*!
0a7de745
A
204 * @function sock_getsockopt
205 * @discussion Retrieves a socket option. See 'man 2 getsockopt'.
206 * @param so The socket.
207 * @param level Level of the socket option.
208 * @param optname The option name.
209 * @param optval The option value.
210 * @param optlen The length of optval, returns the actual length.
211 * @result 0 on success otherwise the errno error.
91447636 212 */
b0d623f7 213extern errno_t sock_getsockopt(socket_t so, int level, int optname,
cb323159
A
214 void *optval, int *optlen)
215__NKE_API_DEPRECATED;
91447636
A
216
217/*!
0a7de745
A
218 * @function sock_ioctl
219 * @discussion Performs an ioctl operation on a socket. See 'man 2 ioctl'.
220 * @param so The socket.
221 * @param request The ioctl name.
222 * @param argp The argument.
223 * @result 0 on success otherwise the errno error.
91447636 224 */
cb323159
A
225extern errno_t sock_ioctl(socket_t so, unsigned long request, void *argp)
226__NKE_API_DEPRECATED;
91447636
A
227
228/*!
0a7de745
A
229 * @function sock_setsockopt
230 * @discussion Sets a socket option. See 'man 2 setsockopt'.
231 * @param so The socket.
232 * @param level Level of the socket option.
233 * @param optname The option name.
234 * @param optval The option value.
235 * @param optlen The length of optval.
236 * @result 0 on success otherwise the errno error.
91447636 237 */
b0d623f7 238extern errno_t sock_setsockopt(socket_t so, int level, int optname,
cb323159
A
239 const void *optval, int optlen)
240__NKE_API_DEPRECATED;
b0d623f7
A
241
242#ifdef KERNEL_PRIVATE
243/*
0a7de745
A
244 * This function was added to support AFP setting the traffic class
245 * for a backup stream within a wireless LAN or over link-local address.
246 *
247 * If you feel you need to use this function, please contact us to
248 * explain why.
249 *
250 * @function sock_settclassopt
251 * @discussion Allows a caller to set the traffic class.
252 * @param so The socket.
253 * @param optval The option value.
254 * @param optlen The length of optval.
255 * @result 0 on success otherwise the errno error.
b0d623f7
A
256 */
257extern errno_t sock_settclassopt(socket_t so, const void* optval, size_t optlen);
258
259/*
0a7de745
A
260 * This function was added to support AFP getting the traffic class
261 * set on a stream.
262 *
263 * This is also a private API, please contact us if you need to use it.
264 *
265 * @function sockgettclassopt
266 * @discussion Allows a caller to get the traffic class.
267 * @param so The socket.
268 * @param optval The option value.
269 * @param optlen The length of optval, returns the actual length.
270 * @result 0 on success otherwise the errno error.
271 */
b0d623f7 272extern errno_t sock_gettclassopt(socket_t so, void* optval, size_t* optlen);
d1ecb069 273
6d2010ae 274#ifdef XNU_KERNEL_PRIVATE
3e170ce0
A
275extern void socket_set_traffic_mgt_flags_locked(socket_t so, u_int8_t flags);
276extern void socket_clear_traffic_mgt_flags_locked(socket_t so, u_int8_t flags);
6d2010ae 277#endif /* XNU_KERNEL_PRIVATE */
d1ecb069 278#ifdef BSD_KERNEL_PRIVATE
3e170ce0
A
279extern void socket_set_traffic_mgt_flags(socket_t so, u_int8_t flags);
280extern void socket_clear_traffic_mgt_flags(socket_t so, u_int8_t flags);
6d2010ae 281extern errno_t socket_defunct(struct proc *, socket_t so, int);
39236c6e
A
282extern errno_t sock_receive_internal(socket_t, struct msghdr *, mbuf_t *,
283 int, size_t *);
d1ecb069 284#endif /* BSD_KERNEL_PRIVATE */
6d2010ae 285#endif /* KERNEL_PRIVATE */
91447636
A
286
287/*!
0a7de745
A
288 * @function sock_listen
289 * @discussion Indicate that the socket should start accepting incoming
290 * connections. See 'man 2 listen'.
291 * @param so The socket.
292 * @param backlog The maximum length of the queue of pending connections.
293 * @result 0 on success otherwise the errno error.
91447636 294 */
cb323159
A
295extern errno_t sock_listen(socket_t so, int backlog)
296__NKE_API_DEPRECATED;
91447636
A
297
298/*!
0a7de745
A
299 * @function sock_receive
300 * @discussion Receive data from a socket. Similar to recvmsg. See 'man
301 * 2 recvmsg' for more information about receiving data.
302 * @param so The socket.
303 * @param msg The msg describing how the data should be received.
304 * @param flags See 'man 2 recvmsg'.
305 * @param recvdlen Number of bytes received, same as return value of
306 * userland recvmsg.
307 * @result 0 on success, EWOULDBLOCK if non-blocking and operation
308 * would cause the thread to block, otherwise the errno error.
91447636 309 */
b0d623f7 310extern errno_t sock_receive(socket_t so, struct msghdr *msg, int flags,
cb323159
A
311 size_t *recvdlen)
312__NKE_API_DEPRECATED;
91447636
A
313
314/*!
0a7de745
A
315 * @function sock_receivembuf
316 * @discussion Receive data from a socket. Similar to sock_receive
317 * though data is returned as a chain of mbufs. See 'man 2 recvmsg'
318 * for more information about receiving data.
319 * @param so The socket.
320 * @param msg The msg describing how the data should be received. May
321 * be NULL. The msg_iov is ignored.
322 * @param data Upon return *data will be a reference to an mbuf chain
323 * containing the data received. This eliminates copying the data
324 * out of the mbufs. Caller is responsible for freeing the mbufs.
325 * @param flags See 'man 2 recvmsg'.
326 * @param recvlen Maximum number of bytes to receive in the mbuf chain.
327 * Upon return, this value will be set to the number of bytes
328 * received, same as return value of userland recvmsg.
329 * @result 0 on success, EWOULDBLOCK if non-blocking and operation
330 * would cause the thread to block, otherwise the errno error.
91447636 331 */
b0d623f7 332extern errno_t sock_receivembuf(socket_t so, struct msghdr *msg, mbuf_t *data,
cb323159
A
333 int flags, size_t *recvlen)
334__NKE_API_DEPRECATED;
91447636
A
335
336/*!
0a7de745
A
337 * @function sock_send
338 * @discussion Send data on a socket. Similar to sendmsg. See 'man 2
339 * sendmsg' for more information about sending data.
340 * @param so The socket.
341 * @param msg The msg describing how the data should be sent. Any
342 * pointers must point to data in the kernel.
343 * @param flags See 'man 2 sendmsg'.
344 * @param sentlen The number of bytes sent.
345 * @result 0 on success, EWOULDBLOCK if non-blocking and operation
346 * would cause the thread to block, otherwise the errno error.
91447636 347 */
b0d623f7 348extern errno_t sock_send(socket_t so, const struct msghdr *msg, int flags,
cb323159
A
349 size_t *sentlen)
350__NKE_API_DEPRECATED;
91447636
A
351
352/*!
0a7de745
A
353 * @function sock_sendmbuf
354 * @discussion Send data in an mbuf on a socket. Similar to sock_send
355 * only the data to be sent is taken from the mbuf chain.
356 * @param so The socket.
357 * @param msg The msg describing how the data should be sent. The
358 * msg_iov is ignored. msg may be NULL.
359 * @param data The mbuf chain of data to send.
360 * @param flags See 'man 2 sendmsg'.
361 * @param sentlen The number of bytes sent.
362 * @result 0 on success, EWOULDBLOCK if non-blocking and operation
363 * would cause the thread to block, otherwise the errno error.
364 * Regardless of return value, the mbuf chain 'data' will be freed.
91447636 365 */
b0d623f7 366extern errno_t sock_sendmbuf(socket_t so, const struct msghdr *msg, mbuf_t data,
cb323159
A
367 int flags, size_t *sentlen)
368__NKE_API_DEPRECATED;
91447636
A
369
370/*!
0a7de745
A
371 * @function sock_shutdown
372 * @discussion Shutdown one or both directions of a connection. See
373 * 'man 2 shutdown' for more information.
374 * @param so The socket.
375 * @param how SHUT_RD - shutdown receive.
376 * SHUT_WR - shutdown send.
377 * SHUT_RDWR - shutdown both.
378 * @result 0 on success otherwise the errno error.
91447636 379 */
cb323159
A
380extern errno_t sock_shutdown(socket_t so, int how)
381__NKE_API_DEPRECATED;
91447636
A
382
383/*!
0a7de745
A
384 * @function sock_socket
385 * @discussion Allocate a socket. Allocating a socket in this manner
386 * creates a socket with no associated file descriptor. For more
387 * information, see 'man 2 socket'.
388 * @param domain The socket domain (PF_INET, etc...).
389 * @param type The socket type (SOCK_STREAM, SOCK_DGRAM, etc...).
390 * @param protocol The socket protocol.
391 * @param callback A notifier function to be called when an event
392 * occurs on the socket. This may be NULL.
393 * @param cookie A cookie passed directly to the callback.
394 * @param new_so Upon success, a reference to the new socket.
395 * @result 0 on success otherwise the errno error.
91447636 396 */
5ba3f43e
A
397#ifdef KERNEL_PRIVATE
398extern errno_t sock_socket_internal(int domain, int type, int protocol,
399 sock_upcall callback, void *cookie, socket_t *new_so);
0a7de745
A
400
401#define sock_socket(domain, type, protocol, callback, cookie, new_so) \
5ba3f43e
A
402 sock_socket_internal((domain), (type), (protocol), \
403 (callback), (cookie), (new_so))
404#else
b0d623f7 405extern errno_t sock_socket(int domain, int type, int protocol,
cb323159
A
406 sock_upcall callback, void *cookie, socket_t *new_so)
407__NKE_API_DEPRECATED;
5ba3f43e 408#endif /* KERNEL_PRIVATE */
91447636
A
409
410/*!
0a7de745
A
411 * @function sock_close
412 * @discussion Close the socket.
413 * @param so The socket to close. This should only ever be a socket
414 * created with sock_socket. Closing a socket created in user space
415 * using sock_close may leave a file descriptor pointing to the
416 * closed socket, resulting in undefined behavior.
91447636 417 */
cb323159
A
418extern void sock_close(socket_t so)
419__NKE_API_DEPRECATED;
91447636 420
2d21ac55
A
421#ifdef KERNEL_PRIVATE
422/*
0a7de745
A
423 * @function sock_retain
424 * @discussion Prevents the socket from closing
425 * @param so The socket to close. Increment a retain count on the
426 * socket, preventing it from being closed when sock_close is
427 * called. This is used when a File Descriptor is passed (and
428 * closed) from userland and the kext wants to keep ownership of
429 * that socket. It is used in conjunction with
430 * sock_release(socket_t so).
91447636 431 */
b0d623f7 432extern void sock_retain(socket_t so);
91447636 433
2d21ac55 434/*
0a7de745
A
435 * @function sock_release
436 * @discussion Decrement the retain count and close the socket if the
437 * retain count reaches zero.
438 * @param so The socket to release. This is used to release ownership
439 * on a socket acquired with sock_retain. When the last retain
440 * count is reached, this will call sock_close to close the socket.
91447636 441 */
b0d623f7 442extern void sock_release(socket_t so);
2d21ac55 443#endif /* KERNEL_PRIVATE */
91447636
A
444
445/*!
0a7de745
A
446 * @function sock_setpriv
447 * @discussion Set the privileged bit in the socket. Allows for
448 * operations that require root privileges.
449 * @param so The socket on which to modify the SS_PRIV flag.
450 * @param on Indicate whether or not the SS_PRIV flag should be set.
451 * @result 0 on success otherwise the errno error.
91447636 452 */
cb323159
A
453extern errno_t sock_setpriv(socket_t so, int on)
454__NKE_API_DEPRECATED;
91447636
A
455
456/*!
0a7de745
A
457 * @function sock_isconnected
458 * @discussion Returns whether or not the socket is connected.
459 * @param so The socket to check.
460 * @result 0 - socket is not connected. 1 - socket is connected.
91447636 461 */
cb323159
A
462extern int sock_isconnected(socket_t so)
463__NKE_API_DEPRECATED;
91447636
A
464
465/*!
0a7de745
A
466 * @function sock_isnonblocking
467 * @discussion Returns whether or not the socket is non-blocking. In
468 * the context of this KPI, non-blocking means that functions to
469 * perform operations on a socket will not wait for completion.
470 *
471 * To enable or disable blocking, use the FIONBIO ioctl. The
472 * parameter is an int. If the int is zero, the socket will block.
473 * If the parameter is non-zero, the socket will not block.
474 * @result 0 - socket will block. 1 - socket will not block.
91447636 475 */
cb323159
A
476extern int sock_isnonblocking(socket_t so)
477__NKE_API_DEPRECATED;
91447636
A
478
479/*!
0a7de745
A
480 * @function sock_gettype
481 * @discussion Retrieves information about the socket. This is the same
482 * information that was used to create the socket. If any of the
483 * parameters following so are NULL, that information is not
484 * retrieved.
485 * @param so The socket to check.
486 * @param domain The domain of the socket (PF_INET, ...). May be NULL.
487 * @param type The socket type (SOCK_STREAM, SOCK_DGRAM, ...). May be NULL.
488 * @param protocol The socket protocol. May be NULL.
489 * @result 0 on success otherwise the errno error.
91447636 490 */
cb323159
A
491extern errno_t sock_gettype(socket_t so, int *domain, int *type, int *protocol)
492__NKE_API_DEPRECATED;
91447636
A
493
494#ifdef KERNEL_PRIVATE
2d21ac55 495/*
0a7de745
A
496 * @function sock_nointerrupt
497 * @discussion Disables interrupt on socket buffers (sets SB_NOINTR on
498 * send and receive socket buffers).
499 * @param so The socket to modify.
500 * @param on Indicate whether or not the SB_NOINTR flag should be set.
501 * @result 0 on success otherwise the errno error.
91447636 502 */
b0d623f7 503extern errno_t sock_nointerrupt(socket_t so, int on);
2d21ac55
A
504
505/*
0a7de745
A
506 * @function sock_getlistener
507 * @discussion Retrieves the listening socket of a pre-accepted socket,
508 * i.e. a socket which is still in the incomplete/completed list.
509 * Once a socket has been accepted, the information pertaining
510 * to its listener is no longer available. Therefore, modules
511 * interested in finding out the listening socket should install
512 * the appropriate socket filter callback (sf_attach) which gets
513 * invoked prior to the socket being fully accepted, and call
514 * this routine at such a time to obtain the listener. Callers
515 * are guaranteed that the listener socket will not go away
516 * during the sf_attach callback, and therefore the value is
517 * safe to be used only in that callback context. Callers should
518 * therefore take note that the listening socket's lock will be
519 * held throughout the duration of the callback.
520 * @param so The pre-accepted socket.
521 * @result Non-NULL value which indicates the listening socket; otherwise,
522 * NULL if the socket is not in the incomplete/completed list
523 * of a listener.
2d21ac55 524 */
b0d623f7 525extern socket_t sock_getlistener(socket_t so);
2d21ac55
A
526
527/*
0a7de745
A
528 * @function sock_getaddr
529 * @discussion Retrieves the local or remote address of a socket.
530 * This is a composite of sock_getpeername and sock_getsockname,
531 * except that the allocated socket address is returned to the
532 * caller, and that the caller is reponsible for calling
533 * sock_freeaddr once finished with it.
534 * @param so The socket.
535 * @param psockname Pointer to the storage for the socket name.
536 * @param peername 0 for local address, and non-zero for peer address.
537 * @result 0 on success otherwise the errno error.
2d21ac55 538 */
b0d623f7
A
539extern errno_t sock_getaddr(socket_t so, struct sockaddr **psockname,
540 int peername);
2d21ac55
A
541
542/*
0a7de745
A
543 * @function sock_freeaddr
544 * @discussion Frees the socket address allocated by sock_getaddr.
545 * @param sockname The socket name to be freed.
2d21ac55 546 */
b0d623f7 547extern void sock_freeaddr(struct sockaddr *sockname);
6d2010ae
A
548
549/*
0a7de745
A
550 * @function sock_setupcall
551 * @discussion Set the notifier function to be called when an event
552 * occurs on the socket. This may be set to NULL to disable
553 * further notifications. Setting the function does not
554 * affect currently notifications about to be sent or being sent.
555 * Note: When this function is used on a socket passed from
556 * userspace it is crucial to call sock_retain() on the socket
557 * otherwise a callback could be dispatched on a closed socket
558 * and cause a crash.
559 * @param sock The socket.
560 * @param callback The notifier function
561 * @param context A cookie passed directly to the callback
562 */
39236c6e
A
563extern errno_t sock_setupcall(socket_t sock, sock_upcall callback,
564 void *context);
565
566/*
0a7de745
A
567 * @function sock_setupcalls
568 * @discussion Set the notifier function to be called when an event
569 * occurs on the socket. This may be set to NULL to disable
570 * further notifications. Setting the function does not
571 * affect currently notifications about to be sent or being sent.
572 * Note: When this function is used on a socket passed from
573 * userspace it is crucial to call sock_retain() on the socket
574 * otherwise a callback could be dispatched on a closed socket
575 * and cause a crash.
576 * @param sock The socket.
577 * @param read_callback The read notifier function
578 * @param read_context A cookie passed directly to the read callback
579 * @param write_callback The write notifier function
580 * @param write_context A cookie passed directly to the write callback
581 */
39236c6e
A
582extern errno_t sock_setupcalls(socket_t sock, sock_upcall read_callback,
583 void *read_context, sock_upcall write_callback, void *write_context);
6d2010ae 584
5ba3f43e 585/*
0a7de745
A
586 * @function sock_setupcalls_locked
587 * @discussion The locked version of sock_setupcalls
588 * @param locked: When sets, indicates that the callbacks expect to be
589 * on a locked socket. Thus, no unlock is done prior to
590 * calling the callback.
5ba3f43e
A
591 */
592extern void sock_setupcalls_locked(socket_t sock,
593 sock_upcall rcallback, void *rcontext,
594 sock_upcall wcallback, void *wcontext, int locked);
595
39236c6e 596/*
0a7de745
A
597 * @function sock_catchevents
598 * @discussion Set the notifier function to be called when an event
599 * occurs on the socket. This may be set to NULL to disable
600 * further notifications. Setting the function does not
601 * affect currently notifications about to be sent or being sent.
602 * @param sock The socket.
603 * @param event_callback The event notifier function
604 * @param event_context A cookie passed directly to the event callback
605 * @param event_mask One or more SO_FILT_HINT_* values OR'ed together,
606 * indicating the registered event(s).
607 */
39236c6e 608extern errno_t sock_catchevents(socket_t sock, sock_evupcall event_callback,
f427ee49 609 void *event_context, long event_mask);
5ba3f43e
A
610
611extern void sock_catchevents_locked(socket_t sock, sock_evupcall ecallback,
f427ee49 612 void *econtext, long emask);
5ba3f43e
A
613
614
fe8ab488 615/*
0a7de745
A
616 * @function sock_iskernel
617 * @discussion Returns true if the socket was created by the kernel or
618 * is owned by the kernel.
619 * @param sock The socket.
620 * @result True if the kernel owns the socket.
621 */
fe8ab488 622extern int sock_iskernel(socket_t);
2d21ac55
A
623#endif /* KERNEL_PRIVATE */
624
625__END_DECLS
f427ee49 626#undef __NKE_API_DEPRECATED
2d21ac55 627#endif /* __KPI_SOCKET__ */