xref: /openbsd/sys/sys/socket.h (revision 4f052010)
1 /*	$OpenBSD: socket.h,v 1.105 2022/09/03 21:13:48 mbuhl Exp $	*/
2 /*	$NetBSD: socket.h,v 1.14 1996/02/09 18:25:36 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)socket.h	8.4 (Berkeley) 2/21/94
33  */
34 
35 #ifndef _SYS_SOCKET_H_
36 #define	_SYS_SOCKET_H_
37 
38 /* get the definitions for struct iovec, size_t, ssize_t, and <sys/cdefs.h> */
39 #include <sys/uio.h>
40 
41 #if __BSD_VISIBLE
42 #include <sys/types.h>			/* for off_t, uid_t, and gid_t */
43 #endif
44 
45 #ifndef	_SOCKLEN_T_DEFINED_
46 #define	_SOCKLEN_T_DEFINED_
47 typedef	__socklen_t	socklen_t;	/* length type for network syscalls */
48 #endif
49 
50 #ifndef	_SA_FAMILY_T_DEFINED_
51 #define	_SA_FAMILY_T_DEFINED_
52 typedef	__sa_family_t	sa_family_t;	/* sockaddr address family type */
53 #endif
54 
55 
56 /*
57  * Definitions related to sockets: types, address families, options.
58  */
59 
60 /*
61  * Types
62  */
63 #define	SOCK_STREAM	1		/* stream socket */
64 #define	SOCK_DGRAM	2		/* datagram socket */
65 #define	SOCK_RAW	3		/* raw-protocol interface */
66 #define	SOCK_RDM	4		/* reliably-delivered message */
67 #define	SOCK_SEQPACKET	5		/* sequenced packet stream */
68 #ifdef _KERNEL
69 #define	SOCK_TYPE_MASK	0x000F		/* mask that covers the above */
70 #endif
71 
72 /*
73  * Socket creation flags
74  */
75 #if __BSD_VISIBLE
76 #define	SOCK_CLOEXEC		0x8000	/* set FD_CLOEXEC */
77 #define	SOCK_NONBLOCK		0x4000	/* set O_NONBLOCK */
78 #ifdef _KERNEL
79 #define	SOCK_NONBLOCK_INHERIT	0x2000	/* inherit O_NONBLOCK from listener */
80 #endif
81 #define	SOCK_DNS		0x1000	/* set SS_DNS */
82 #endif /* __BSD_VISIBLE */
83 
84 /*
85  * Option flags per-socket.
86  */
87 #define	SO_DEBUG	0x0001		/* turn on debugging info recording */
88 #define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
89 #define	SO_REUSEADDR	0x0004		/* allow local address reuse */
90 #define	SO_KEEPALIVE	0x0008		/* keep connections alive */
91 #define	SO_DONTROUTE	0x0010		/* just use interface addresses */
92 #define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
93 #define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
94 #define	SO_LINGER	0x0080		/* linger on close if data present */
95 #define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
96 #define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
97 #define SO_TIMESTAMP	0x0800		/* timestamp received dgram traffic */
98 #define SO_BINDANY	0x1000		/* allow bind to any address */
99 #define SO_ZEROIZE	0x2000		/* zero out all mbufs sent over socket */
100 
101 /*
102  * Additional options, not kept in so_options.
103  */
104 #define	SO_SNDBUF	0x1001		/* send buffer size */
105 #define	SO_RCVBUF	0x1002		/* receive buffer size */
106 #define	SO_SNDLOWAT	0x1003		/* send low-water mark */
107 #define	SO_RCVLOWAT	0x1004		/* receive low-water mark */
108 #define	SO_SNDTIMEO	0x1005		/* send timeout */
109 #define	SO_RCVTIMEO	0x1006		/* receive timeout */
110 #define	SO_ERROR	0x1007		/* get error status and clear */
111 #define	SO_TYPE		0x1008		/* get socket type */
112 #define	SO_NETPROC	0x1020		/* multiplex; network processing */
113 #define	SO_RTABLE	0x1021		/* routing table to be used */
114 #define	SO_PEERCRED	0x1022		/* get connect-time credentials */
115 #define	SO_SPLICE	0x1023		/* splice data to other socket */
116 #define	SO_DOMAIN	0x1024		/* get socket domain */
117 #define	SO_PROTOCOL	0x1025		/* get socket protocol */
118 
119 /*
120  * Structure used for manipulating linger option.
121  */
122 struct	linger {
123 	int	l_onoff;		/* option on/off */
124 	int	l_linger;		/* linger time */
125 };
126 
127 #if __BSD_VISIBLE
128 
129 #ifndef _TIMEVAL_DECLARED
130 #define _TIMEVAL_DECLARED
131 struct timeval {
132 	time_t		tv_sec;		/* seconds */
133 	suseconds_t	tv_usec;	/* and microseconds */
134 };
135 #endif
136 
137 /*
138  * Structure used for manipulating splice option.
139  */
140 struct	splice {
141 	int	sp_fd;			/* drain socket file descriptor */
142 	off_t	sp_max;			/* if set, maximum bytes to splice */
143 	struct	timeval	sp_idle;	/* idle timeout */
144 };
145 
146 /*
147  * Maximum number of alternate routing tables
148  */
149 #define	RT_TABLEID_MAX		255
150 #define	RT_TABLEID_BITS		8
151 #define	RT_TABLEID_MASK		0xff
152 
153 #endif /* __BSD_VISIBLE */
154 
155 /*
156  * Level number for (get/set)sockopt() to apply to socket itself.
157  */
158 #define	SOL_SOCKET	0xffff		/* options for socket level */
159 
160 /*
161  * Address families.
162  */
163 #define	AF_UNSPEC	0		/* unspecified */
164 #define	AF_UNIX		1		/* local to host */
165 #define	AF_LOCAL	AF_UNIX		/* draft POSIX compatibility */
166 #define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
167 #define	AF_IMPLINK	3		/* arpanet imp addresses */
168 #define	AF_PUP		4		/* pup protocols: e.g. BSP */
169 #define	AF_CHAOS	5		/* mit CHAOS protocols */
170 #define	AF_NS		6		/* XEROX NS protocols */
171 #define	AF_ISO		7		/* ISO protocols */
172 #define	AF_OSI		AF_ISO
173 #define	AF_ECMA		8		/* european computer manufacturers */
174 #define	AF_DATAKIT	9		/* datakit protocols */
175 #define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
176 #define	AF_SNA		11		/* IBM SNA */
177 #define AF_DECnet	12		/* DECnet */
178 #define AF_DLI		13		/* DEC Direct data link interface */
179 #define AF_LAT		14		/* LAT */
180 #define	AF_HYLINK	15		/* NSC Hyperchannel */
181 #define	AF_APPLETALK	16		/* Apple Talk */
182 #define	AF_ROUTE	17		/* Internal Routing Protocol */
183 #define	AF_LINK		18		/* Link layer interface */
184 #define	pseudo_AF_XTP	19		/* eXpress Transfer Protocol (no AF) */
185 #define	AF_COIP		20		/* connection-oriented IP, aka ST II */
186 #define	AF_CNT		21		/* Computer Network Technology */
187 #define pseudo_AF_RTIP	22		/* Help Identify RTIP packets */
188 #define	AF_IPX		23		/* Novell Internet Protocol */
189 #define	AF_INET6	24		/* IPv6 */
190 #define pseudo_AF_PIP	25		/* Help Identify PIP packets */
191 #define AF_ISDN		26		/* Integrated Services Digital Network*/
192 #define AF_E164		AF_ISDN		/* CCITT E.164 recommendation */
193 #define AF_NATM		27		/* native ATM access */
194 #define	AF_ENCAP	28
195 #define	AF_SIP		29		/* Simple Internet Protocol */
196 #define AF_KEY		30
197 #define pseudo_AF_HDRCMPLT 31		/* Used by BPF to not rewrite headers
198 					   in interface output routine */
199 #define	AF_BLUETOOTH	32		/* Bluetooth */
200 #define AF_MPLS         33              /* MPLS */
201 #define pseudo_AF_PFLOW 34		/* pflow */
202 #define pseudo_AF_PIPEX 35		/* PIPEX */
203 #define AF_MAX          36
204 
205 /*
206  * Structure used by kernel to store most
207  * addresses.
208  */
209 struct sockaddr {
210 	__uint8_t    sa_len;		/* total length */
211 	sa_family_t sa_family;		/* address family */
212 	char	    sa_data[14];	/* actually longer; address value */
213 };
214 
215 /*
216  * Sockaddr type which can hold any sockaddr type available
217  * in the system.
218  *
219  * Note: __ss_{len,family} is defined in RFC2553.  During RFC2553 discussion
220  * the field name went back and forth between ss_len and __ss_len,
221  * and RFC2553 specifies it to be __ss_len.  openbsd picked ss_len.
222  * For maximum portability, userland programmer would need to
223  * (1) make the code never touch ss_len portion (cast it into sockaddr and
224  * touch sa_len), or (2) add "-Dss_len=__ss_len" into CFLAGS to unify all
225  * occurrences (including header file) to __ss_len.
226  */
227 struct sockaddr_storage {
228 	__uint8_t	ss_len;		/* total length */
229 	sa_family_t	ss_family;	/* address family */
230 	unsigned char	__ss_pad1[6];	/* align to quad */
231 	__uint64_t	__ss_pad2;	/* force alignment for stupid compilers */
232 	unsigned char	__ss_pad3[240];	/* pad to a total of 256 bytes */
233 };
234 
235 #ifdef _KERNEL
236 /*
237  * Structure used by kernel to pass protocol
238  * information in raw sockets.
239  */
240 struct sockproto {
241 	unsigned short	sp_family;	/* address family */
242 	unsigned short	sp_protocol;	/* protocol */
243 };
244 #endif /* _KERNEL */
245 
246 /*
247  * Protocol families, same as address families for now.
248  */
249 #define	PF_UNSPEC	AF_UNSPEC
250 #define	PF_LOCAL	AF_LOCAL
251 #define	PF_UNIX		AF_UNIX
252 #define	PF_INET		AF_INET
253 #define	PF_IMPLINK	AF_IMPLINK
254 #define	PF_PUP		AF_PUP
255 #define	PF_CHAOS	AF_CHAOS
256 #define	PF_NS		AF_NS
257 #define	PF_ISO		AF_ISO
258 #define	PF_OSI		AF_ISO
259 #define	PF_ECMA		AF_ECMA
260 #define	PF_DATAKIT	AF_DATAKIT
261 #define	PF_CCITT	AF_CCITT
262 #define	PF_SNA		AF_SNA
263 #define PF_DECnet	AF_DECnet
264 #define PF_DLI		AF_DLI
265 #define PF_LAT		AF_LAT
266 #define	PF_HYLINK	AF_HYLINK
267 #define	PF_APPLETALK	AF_APPLETALK
268 #define	PF_ROUTE	AF_ROUTE
269 #define	PF_LINK		AF_LINK
270 #define	PF_XTP		pseudo_AF_XTP	/* really just proto family, no AF */
271 #define	PF_COIP		AF_COIP
272 #define	PF_CNT		AF_CNT
273 #define	PF_IPX		AF_IPX		/* same format as AF_NS */
274 #define PF_INET6	AF_INET6
275 #define PF_RTIP		pseudo_AF_RTIP	/* same format as AF_INET */
276 #define PF_PIP		pseudo_AF_PIP
277 #define PF_ISDN		AF_ISDN
278 #define PF_NATM		AF_NATM
279 #define PF_ENCAP	AF_ENCAP
280 #define	PF_SIP		AF_SIP
281 #define PF_KEY		AF_KEY
282 #define PF_BPF		pseudo_AF_HDRCMPLT
283 #define	PF_BLUETOOTH	AF_BLUETOOTH
284 #define PF_MPLS		AF_MPLS
285 #define PF_PFLOW	pseudo_AF_PFLOW
286 #define PF_PIPEX	pseudo_AF_PIPEX
287 #define	PF_MAX		AF_MAX
288 
289 /*
290  * These are the valid values for the "how" field used by shutdown(2).
291  */
292 #define	SHUT_RD		0
293 #define	SHUT_WR		1
294 #define	SHUT_RDWR	2
295 
296 #if __BSD_VISIBLE
297 #define SA_LEN(x) ((x)->sa_len)
298 
299 /* Read using getsockopt() with SOL_SOCKET, SO_PEERCRED */
300 struct sockpeercred {
301 	uid_t		uid;		/* effective user id */
302 	gid_t		gid;		/* effective group id */
303 	pid_t		pid;
304 };
305 
306 /*
307  * Definitions for network related sysctl, CTL_NET.
308  *
309  * Second level is protocol family.
310  * Third level is protocol number.
311  *
312  * Further levels are defined by the individual families below.
313  */
314 #define NET_MAXID	AF_MAX
315 
316 #define CTL_NET_NAMES { \
317 	{ 0, 0 }, \
318 	{ "unix", CTLTYPE_NODE }, \
319 	{ "inet", CTLTYPE_NODE }, \
320 	{ "implink", CTLTYPE_NODE }, \
321 	{ "pup", CTLTYPE_NODE }, \
322 	{ "chaos", CTLTYPE_NODE }, \
323 	{ "xerox_ns", CTLTYPE_NODE }, \
324 	{ "iso", CTLTYPE_NODE }, \
325 	{ "ecma", CTLTYPE_NODE }, \
326 	{ "datakit", CTLTYPE_NODE }, \
327 	{ "ccitt", CTLTYPE_NODE }, \
328 	{ "ibm_sna", CTLTYPE_NODE }, \
329 	{ "decnet", CTLTYPE_NODE }, \
330 	{ "dec_dli", CTLTYPE_NODE }, \
331 	{ "lat", CTLTYPE_NODE }, \
332 	{ "hylink", CTLTYPE_NODE }, \
333 	{ "appletalk", CTLTYPE_NODE }, \
334 	{ "route", CTLTYPE_NODE }, \
335 	{ "link", CTLTYPE_NODE }, \
336 	{ "xtp", CTLTYPE_NODE }, \
337 	{ "coip", CTLTYPE_NODE }, \
338 	{ "cnt", CTLTYPE_NODE }, \
339 	{ "rtip", CTLTYPE_NODE }, \
340 	{ "ipx", CTLTYPE_NODE }, \
341 	{ "inet6", CTLTYPE_NODE }, \
342 	{ "pip", CTLTYPE_NODE }, \
343 	{ "isdn", CTLTYPE_NODE }, \
344 	{ "natm", CTLTYPE_NODE }, \
345 	{ "encap", CTLTYPE_NODE }, \
346 	{ "sip", CTLTYPE_NODE }, \
347 	{ "key", CTLTYPE_NODE }, \
348 	{ "bpf", CTLTYPE_NODE }, \
349 	{ "bluetooth", CTLTYPE_NODE }, \
350 	{ "mpls", CTLTYPE_NODE }, \
351 	{ "pflow", CTLTYPE_NODE }, \
352 	{ "pipex", CTLTYPE_NODE }, \
353 }
354 
355 /*
356  * PF_ROUTE - Routing table
357  *
358  * Four additional levels are defined:
359  *	Fourth: address family, 0 is wildcard
360  *	Fifth: type of info, defined below
361  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
362  *	Seventh: routing table to use (facultative, defaults to 0)
363  *		 NET_RT_TABLE has the table id as sixth element.
364  */
365 #define NET_RT_DUMP	1		/* dump; may limit to a.f. */
366 #define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
367 #define NET_RT_IFLIST	3		/* survey interface list */
368 #define	NET_RT_STATS	4		/* routing table statistics */
369 #define	NET_RT_TABLE	5
370 #define	NET_RT_IFNAMES	6
371 #define	NET_RT_SOURCE	7
372 #define	NET_RT_MAXID	8
373 
374 #define CTL_NET_RT_NAMES { \
375 	{ 0, 0 }, \
376 	{ "dump", CTLTYPE_STRUCT }, \
377 	{ "flags", CTLTYPE_STRUCT }, \
378 	{ "iflist", CTLTYPE_STRUCT }, \
379 	{ "stats", CTLTYPE_STRUCT }, \
380 	{ "table", CTLTYPE_STRUCT }, \
381 	{ "ifnames", CTLTYPE_STRUCT }, \
382 	{ "source", CTLTYPE_STRUCT }, \
383 }
384 
385 /*
386  * PF_UNIX - unix socket tunables
387  */
388 #define NET_UNIX_INFLIGHT	6
389 #define NET_UNIX_DEFERRED	7
390 #define NET_UNIX_MAXID		8
391 
392 #define CTL_NET_UNIX_NAMES { \
393 	{ 0, 0 }, \
394 	{ "stream", CTLTYPE_NODE }, \
395 	{ "dgram", CTLTYPE_NODE }, \
396 	{ 0, 0 }, \
397 	{ 0, 0 }, \
398 	{ "seqpacket", CTLTYPE_NODE }, \
399 	{ "inflight", CTLTYPE_INT }, \
400 	{ "deferred", CTLTYPE_INT }, \
401 }
402 
403 #define UNPCTL_RECVSPACE	1
404 #define UNPCTL_SENDSPACE	2
405 #define NET_UNIX_PROTO_MAXID	3
406 
407 #define CTL_NET_UNIX_PROTO_NAMES { \
408 	{ 0, 0 }, \
409 	{ "recvspace", CTLTYPE_INT }, \
410 	{ "sendspace", CTLTYPE_INT }, \
411 }
412 
413 /*
414  * PF_LINK - link layer or device tunables
415  */
416 #define NET_LINK_IFRXQ		1	/* net.link.ifrxq */
417 #define NET_LINK_MAXID		2
418 
419 #define CTL_NET_LINK_NAMES { \
420 	{ 0, 0 }, \
421 	{ "ifrxq", CTLTYPE_NODE }, \
422 }
423 
424 #define NET_LINK_IFRXQ_PRESSURE_RETURN \
425 				1	/* net.link.ifrxq.pressure_return */
426 #define NET_LINK_IFRXQ_PRESSURE_DROP \
427 				2	/* net.link.ifrxq.pressure_drop */
428 #define NET_LINK_IFRXQ_MAXID	3
429 
430 #define CTL_NET_LINK_IFRXQ_NAMES { \
431 	{ 0, 0 }, \
432 	{ "pressure_return", CTLTYPE_INT }, \
433 	{ "pressure_drop", CTLTYPE_INT }, \
434 }
435 
436 /*
437  * PF_KEY - Key Management
438  */
439 #define NET_KEY_SADB_DUMP	1	/* return SADB */
440 #define NET_KEY_SPD_DUMP	2	/* return SPD */
441 #define NET_KEY_MAXID		3
442 
443 #define CTL_NET_KEY_NAMES { \
444 	{ 0, 0 }, \
445 	{ "sadb_dump", CTLTYPE_STRUCT }, \
446 	{ "spd_dump", CTLTYPE_STRUCT }, \
447 }
448 
449 /*
450  * PF_BPF  not really a family, but connected under CTL_NET
451  */
452 #define NET_BPF_BUFSIZE		1		/* default buffer size */
453 #define NET_BPF_MAXBUFSIZE	2		/* maximum buffer size */
454 #define NET_BPF_MAXID		3
455 
456 #define CTL_NET_BPF_NAMES { \
457 	{ 0, 0 }, \
458 	{ "bufsize", CTLTYPE_INT }, \
459 	{ "maxbufsize", CTLTYPE_INT }, \
460 }
461 
462 /*
463  * PF_PFLOW not really a family, but connected under CTL_NET
464  */
465 #define NET_PFLOW_STATS		1		/* statistics */
466 #define NET_PFLOW_MAXID		2
467 
468 #define CTL_NET_PFLOW_NAMES { \
469 	{ 0, 0 }, \
470 	{ "stats", CTLTYPE_STRUCT }, \
471 }
472 #endif /* __BSD_VISIBLE */
473 
474 /*
475  * Maximum queue length specifiable by listen(2).
476  */
477 #define	SOMAXCONN	128
478 
479 /*
480  * Message header for recvmsg and sendmsg calls.
481  * Used value-result for recvmsg, value only for sendmsg.
482  */
483 struct msghdr {
484 	void		*msg_name;	/* optional address */
485 	socklen_t	msg_namelen;	/* size of address */
486 	struct		iovec *msg_iov;	/* scatter/gather array */
487 	unsigned int	msg_iovlen;	/* # elements in msg_iov */
488 	void		*msg_control;	/* ancillary data, see below */
489 	socklen_t	msg_controllen;	/* ancillary data buffer len */
490 	int		msg_flags;	/* flags on received message */
491 };
492 
493 struct mmsghdr {
494 	struct msghdr msg_hdr;
495 	unsigned int msg_len;
496 };
497 
498 struct timespec;
499 
500 #define	MSG_OOB			0x1	/* process out-of-band data */
501 #define	MSG_PEEK		0x2	/* peek at incoming message */
502 #define	MSG_DONTROUTE		0x4	/* send without using routing tables */
503 #define	MSG_EOR			0x8	/* data completes record */
504 #define	MSG_TRUNC		0x10	/* data discarded before delivery */
505 #define	MSG_CTRUNC		0x20	/* control data lost before delivery */
506 #define	MSG_WAITALL		0x40	/* wait for full request or error */
507 #define	MSG_DONTWAIT		0x80	/* this message should be nonblocking */
508 #define	MSG_BCAST		0x100	/* this message rec'd as broadcast */
509 #define	MSG_MCAST		0x200	/* this message rec'd as multicast */
510 #define	MSG_NOSIGNAL		0x400	/* do not send SIGPIPE */
511 #define	MSG_CMSG_CLOEXEC	0x800	/* set FD_CLOEXEC on received fds */
512 #define	MSG_WAITFORONE		0x1000	/* nonblocking but wait for one msg */
513 
514 /*
515  * Header for ancillary data objects in msg_control buffer.
516  * Used for additional information with/about a datagram
517  * not expressible by flags.  The format is a sequence
518  * of message elements headed by cmsghdr structures.
519  */
520 struct cmsghdr {
521 	socklen_t	cmsg_len;	/* data byte count, including hdr */
522 	int		cmsg_level;	/* originating protocol */
523 	int		cmsg_type;	/* protocol-specific type */
524 /* followed by	u_char  cmsg_data[]; */
525 };
526 
527 /* given pointer to struct cmsghdr, return pointer to data */
528 #define	CMSG_DATA(cmsg) \
529 	((unsigned char *)(cmsg) + _ALIGN(sizeof(struct cmsghdr)))
530 
531 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */
532 #define	CMSG_NXTHDR(mhdr, cmsg)	\
533 	(((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len) + \
534 			    _ALIGN(sizeof(struct cmsghdr)) > \
535 	    ((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
536 	    (struct cmsghdr *)NULL : \
537 	    (struct cmsghdr *)((char *)(cmsg) + _ALIGN((cmsg)->cmsg_len)))
538 
539 /*
540  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
541  * an empty list for some reasons.
542  */
543 #define	CMSG_FIRSTHDR(mhdr) \
544 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
545 	 (struct cmsghdr *)(mhdr)->msg_control : \
546 	 (struct cmsghdr *)NULL)
547 
548 /* Round len up to next alignment boundary */
549 #ifdef _KERNEL
550 #define CMSG_ALIGN(n)		_ALIGN(n)
551 #endif
552 
553 /* Length of the contents of a control message of length len */
554 #define	CMSG_LEN(len)	(_ALIGN(sizeof(struct cmsghdr)) + (len))
555 
556 /* Length of the space taken up by a padded control message of length len */
557 #define	CMSG_SPACE(len)	(_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(len))
558 
559 /* "Socket"-level control message types: */
560 #define	SCM_RIGHTS	0x01		/* access rights (array of int) */
561 #define	SCM_TIMESTAMP	0x04		/* timestamp (struct timeval) */
562 
563 #ifndef _KERNEL
564 
565 __BEGIN_DECLS
566 int	accept(int, struct sockaddr *, socklen_t *);
567 int	bind(int, const struct sockaddr *, socklen_t);
568 int	connect(int, const struct sockaddr *, socklen_t);
569 int	getpeername(int, struct sockaddr *, socklen_t *);
570 int	getsockname(int, struct sockaddr *, socklen_t *);
571 int	getsockopt(int, int, int, void *, socklen_t *);
572 int	listen(int, int);
573 ssize_t	recv(int, void *, size_t, int);
574 ssize_t	recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
575 ssize_t	recvmsg(int, struct msghdr *, int);
576 int	recvmmsg(int, struct mmsghdr *, unsigned int, int, struct timespec *);
577 ssize_t	send(int, const void *, size_t, int);
578 ssize_t	sendto(int, const void *,
579 	    size_t, int, const struct sockaddr *, socklen_t);
580 ssize_t	sendmsg(int, const struct msghdr *, int);
581 int	sendmmsg(int, struct mmsghdr *, unsigned int, int);
582 int	setsockopt(int, int, int, const void *, socklen_t);
583 int	shutdown(int, int);
584 int	sockatmark(int);
585 int	socket(int, int, int);
586 int	socketpair(int, int, int, int *);
587 
588 #if __BSD_VISIBLE
589 int	accept4(int, struct sockaddr *__restrict, socklen_t *__restrict, int);
590 #endif
591 
592 #if __BSD_VISIBLE
593 int	getpeereid(int, uid_t *, gid_t *);
594 int	getrtable(void);
595 int	setrtable(int);
596 #endif /* __BSD_VISIBLE */
597 
598 __END_DECLS
599 
600 #else
601 
602 static inline struct sockaddr *
603 sstosa(struct sockaddr_storage *ss)
604 {
605 	return ((struct sockaddr *)(ss));
606 }
607 
608 #endif /* !_KERNEL */
609 
610 #endif /* !_SYS_SOCKET_H_ */
611