socket.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1/*
2 * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)socket.h	8.4 (Berkeley) 2/21/94
30 * $FreeBSD: src/sys/sys/socket.h,v 1.60 2002/04/20 02:24:35 mike Exp $
31 */
32
33#ifndef _SYS_SOCKET_H_
34#define	_SYS_SOCKET_H_
35
36#include <sys/types.h>
37#include <sys/_types.h>
38#include <netinet/in.h>
39#include <netdb.h>
40
41/* Needed by linuxthreads. */
42
43# define __SOCKADDR_ARG		struct sockaddr *__restrict
44# define __CONST_SOCKADDR_ARG	__const struct sockaddr *
45
46/*
47 * Definitions related to sockets: types, address families, options.
48 */
49
50/*
51 * Data types.
52 */
53#ifdef _BSD_SA_FAMILY_T_
54typedef	_BSD_SA_FAMILY_T_	sa_family_t;
55#undef _BSD_SA_FAMILY_T_
56#endif
57
58#ifdef	_BSD_SOCKLEN_T_
59typedef	_BSD_SOCKLEN_T_	socklen_t;
60#undef	_BSD_SOCKLEN_T_
61#endif
62
63/*
64 * Types
65 */
66#define	SOCK_STREAM	1		/* stream socket */
67#define	SOCK_DGRAM	2		/* datagram socket */
68#define	SOCK_RAW	3		/* raw-protocol interface */
69#define	SOCK_RDM	4		/* reliably-delivered message */
70#define	SOCK_SEQPACKET	5		/* sequenced packet stream */
71
72/* Socket type flags that can be or'd with the above types */
73#define SOCK_CLOEXEC 0x100
74#define SOCK_NONBLOCK 0x200
75
76/*
77 * Option flags per-socket.
78 */
79#define	SO_DEBUG	0x0001		/* turn on debugging info recording */
80#define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
81#define	SO_REUSEADDR	0x0004		/* allow local address reuse */
82#define	SO_KEEPALIVE	0x0008		/* keep connections alive */
83#define	SO_DONTROUTE	0x0010		/* just use interface addresses */
84#define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
85#define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
86#define	SO_LINGER	0x0080		/* linger on close if data present */
87#define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
88#define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
89#define	SO_TIMESTAMP	0x0400		/* timestamp received dgram traffic */
90#define	SO_ACCEPTFILTER	0x1000		/* there is an accept filter */
91
92/*
93 * Additional options, not kept in so_options.
94 */
95#define SO_SNDBUF	0x1001		/* send buffer size */
96#define SO_RCVBUF	0x1002		/* receive buffer size */
97#define SO_SNDLOWAT	0x1003		/* send low-water mark */
98#define SO_RCVLOWAT	0x1004		/* receive low-water mark */
99#define SO_SNDTIMEO	0x1005		/* send timeout */
100#define SO_RCVTIMEO	0x1006		/* receive timeout */
101#define	SO_ERROR	0x1007		/* get error status and clear */
102#define	SO_TYPE		0x1008		/* get socket type */
103/*efine	SO_PRIVSTATE	0x1009		   get/deny privileged state */
104
105/*
106 * Structure used for manipulating linger option.
107 */
108struct linger {
109	int	l_onoff;		/* option on/off */
110	int	l_linger;		/* linger time */
111};
112
113struct accept_filter_arg {
114	char	af_name[16];
115	char	af_arg[256-16];
116};
117
118/*
119 * Level number for (get/set)sockopt() to apply to socket itself.
120 */
121#define	SOL_SOCKET	0xffff		/* options for socket level */
122
123/* Protocol families.  */
124#define	PF_UNSPEC	0	/* Unspecified.  */
125#define	PF_LOCAL	1	/* Local to host (pipes and file-domain).  */
126#define	PF_UNIX		PF_LOCAL /* Old BSD name for PF_LOCAL.  */
127#define	PF_FILE		PF_LOCAL /* Another non-standard name for PF_LOCAL.  */
128#define	PF_INET		2	/* IP protocol family.  */
129#define	PF_AX25		3	/* Amateur Radio AX.25.  */
130#define	PF_IPX		4	/* Novell Internet Protocol.  */
131#define	PF_APPLETALK	5	/* Appletalk DDP.  */
132#define	PF_NETROM	6	/* Amateur radio NetROM.  */
133#define	PF_BRIDGE	7	/* Multiprotocol bridge.  */
134#define	PF_ATMPVC	8	/* ATM PVCs.  */
135#define	PF_X25		9	/* Reserved for X.25 project.  */
136#define	PF_INET6	10	/* IP version 6.  */
137#define	PF_ROSE		11	/* Amateur Radio X.25 PLP.  */
138#define	PF_DECnet	12	/* Reserved for DECnet project.  */
139#define	PF_NETBEUI	13	/* Reserved for 802.2LLC project.  */
140#define	PF_SECURITY	14	/* Security callback pseudo AF.  */
141#define	PF_KEY		15	/* PF_KEY key management API.  */
142#define	PF_NETLINK	16
143#define	PF_ROUTE	PF_NETLINK /* Alias to emulate 4.4BSD.  */
144#define	PF_PACKET	17	/* Packet family.  */
145#define	PF_ASH		18	/* Ash.  */
146#define	PF_ECONET	19	/* Acorn Econet.  */
147#define	PF_ATMSVC	20	/* ATM SVCs.  */
148#define	PF_SNA		22	/* Linux SNA Project */
149#define	PF_IRDA		23	/* IRDA sockets.  */
150#define	PF_PPPOX	24	/* PPPoX sockets.  */
151#define	PF_WANPIPE	25	/* Wanpipe API sockets.  */
152#define	PF_BLUETOOTH	31	/* Bluetooth sockets.  */
153#define	PF_MAX		32	/* For now..  */
154
155/* Address families.  */
156#define	AF_UNSPEC	PF_UNSPEC
157#define	AF_LOCAL	PF_LOCAL
158#define	AF_UNIX		PF_UNIX
159#define	AF_FILE		PF_FILE
160#define	AF_INET		PF_INET
161#define	AF_AX25		PF_AX25
162#define	AF_IPX		PF_IPX
163#define	AF_APPLETALK	PF_APPLETALK
164#define	AF_NETROM	PF_NETROM
165#define	AF_BRIDGE	PF_BRIDGE
166#define	AF_ATMPVC	PF_ATMPVC
167#define	AF_X25		PF_X25
168#define	AF_INET6	PF_INET6
169#define	AF_ROSE		PF_ROSE
170#define	AF_DECnet	PF_DECnet
171#define	AF_NETBEUI	PF_NETBEUI
172#define	AF_SECURITY	PF_SECURITY
173#define	AF_KEY		PF_KEY
174#define	AF_NETLINK	PF_NETLINK
175#define	AF_ROUTE	PF_ROUTE
176#define	AF_PACKET	PF_PACKET
177#define	AF_ASH		PF_ASH
178#define	AF_ECONET	PF_ECONET
179#define	AF_ATMSVC	PF_ATMSVC
180#define	AF_SNA		PF_SNA
181#define	AF_IRDA		PF_IRDA
182#define	AF_PPPOX	PF_PPPOX
183#define	AF_WANPIPE	PF_WANPIPE
184#define	AF_BLUETOOTH	PF_BLUETOOTH
185#define	AF_MAX		PF_MAX
186
187/*
188 * Structure used by kernel to store most
189 * addresses.
190 */
191struct sockaddr {
192	sa_family_t	sa_family;	/* address family */
193	char		sa_data[14];	/* actually longer; address value */
194};
195#define	SOCK_MAXADDRLEN	255		/* longest possible addresses */
196
197/*
198 * Structure used by kernel to pass protocol
199 * information in raw sockets.
200 */
201struct sockproto {
202	u_short	sp_family;		/* address family */
203	u_short	sp_protocol;		/* protocol */
204};
205
206/*
207 * RFC 2553: protocol-independent placeholder for socket addresses
208 */
209#define	_SS_MAXSIZE	128U
210#define	_SS_ALIGNSIZE	(sizeof(int64_t))
211#define	_SS_PAD1SIZE	(_SS_ALIGNSIZE - sizeof(unsigned char) - sizeof(sa_family_t))
212#define	_SS_PAD2SIZE	(_SS_MAXSIZE - sizeof(unsigned char) - sizeof(sa_family_t) - \
213				_SS_PAD1SIZE - _SS_ALIGNSIZE)
214
215struct sockaddr_storage {
216	sa_family_t	ss_family;	/* address family */
217	char		__ss_pad1[_SS_PAD1SIZE];
218	int64_t		__ss_align;	/* force desired structure storage alignment */
219	char		__ss_pad2[_SS_PAD2SIZE];
220};
221
222/*
223 * Definitions for network related sysctl, CTL_NET.
224 *
225 * Second level is protocol family.
226 * Third level is protocol number.
227 *
228 * Further levels are defined by the individual families below.
229 */
230#define NET_MAXID	AF_MAX
231
232#define CTL_NET_NAMES { \
233	{ 0, 0 }, \
234	{ "unix", CTLTYPE_NODE }, \
235	{ "inet", CTLTYPE_NODE }, \
236	{ "implink", CTLTYPE_NODE }, \
237	{ "pup", CTLTYPE_NODE }, \
238	{ "chaos", CTLTYPE_NODE }, \
239	{ "xerox_ns", CTLTYPE_NODE }, \
240	{ "iso", CTLTYPE_NODE }, \
241	{ "emca", CTLTYPE_NODE }, \
242	{ "datakit", CTLTYPE_NODE }, \
243	{ "ccitt", CTLTYPE_NODE }, \
244	{ "ibm_sna", CTLTYPE_NODE }, \
245	{ "decnet", CTLTYPE_NODE }, \
246	{ "dec_dli", CTLTYPE_NODE }, \
247	{ "lat", CTLTYPE_NODE }, \
248	{ "hylink", CTLTYPE_NODE }, \
249	{ "appletalk", CTLTYPE_NODE }, \
250	{ "route", CTLTYPE_NODE }, \
251	{ "link_layer", CTLTYPE_NODE }, \
252	{ "xtp", CTLTYPE_NODE }, \
253	{ "coip", CTLTYPE_NODE }, \
254	{ "cnt", CTLTYPE_NODE }, \
255	{ "rtip", CTLTYPE_NODE }, \
256	{ "ipx", CTLTYPE_NODE }, \
257	{ "sip", CTLTYPE_NODE }, \
258	{ "pip", CTLTYPE_NODE }, \
259	{ "isdn", CTLTYPE_NODE }, \
260	{ "key", CTLTYPE_NODE }, \
261	{ "inet6", CTLTYPE_NODE }, \
262	{ "natm", CTLTYPE_NODE }, \
263	{ "atm", CTLTYPE_NODE }, \
264	{ "hdrcomplete", CTLTYPE_NODE }, \
265	{ "netgraph", CTLTYPE_NODE }, \
266	{ "snp", CTLTYPE_NODE }, \
267	{ "scp", CTLTYPE_NODE }, \
268}
269
270/*
271 * PF_ROUTE - Routing table
272 *
273 * Three additional levels are defined:
274 *	Fourth: address family, 0 is wildcard
275 *	Fifth: type of info, defined below
276 *	Sixth: flag(s) to mask with for NET_RT_FLAGS
277 */
278#define NET_RT_DUMP	1		/* dump; may limit to a.f. */
279#define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
280#define NET_RT_IFLIST	3		/* survey interface list */
281#define	NET_RT_MAXID	4
282
283#define CTL_NET_RT_NAMES { \
284	{ 0, 0 }, \
285	{ "dump", CTLTYPE_STRUCT }, \
286	{ "flags", CTLTYPE_STRUCT }, \
287	{ "iflist", CTLTYPE_STRUCT }, \
288}
289
290/*
291 * Maximum queue length specifiable by listen.
292 */
293#ifndef	SOMAXCONN
294#define	SOMAXCONN	128
295#endif
296
297/*
298 * Message header for recvmsg and sendmsg calls.
299 * Used value-result for recvmsg, value only for sendmsg.
300 */
301struct msghdr {
302	void		*msg_name;		/* optional address */
303	socklen_t	 msg_namelen;		/* size of address */
304	struct iovec	*msg_iov;		/* scatter/gather array */
305	int		 msg_iovlen;		/* # elements in msg_iov */
306	void		*msg_control;		/* ancillary data, see below */
307	socklen_t	 msg_controllen;	/* ancillary data buffer len */
308	int		 msg_flags;		/* flags on received message */
309};
310
311#define	MSG_OOB		0x1		/* process out-of-band data */
312#define	MSG_PEEK	0x2		/* peek at incoming message */
313#define	MSG_DONTROUTE	0x4		/* send without using routing tables */
314#define	MSG_EOR		0x8		/* data completes record */
315#define	MSG_TRUNC	0x10		/* data discarded before delivery */
316#define	MSG_CTRUNC	0x20		/* control data lost before delivery */
317#define	MSG_WAITALL	0x40		/* wait for full request or error */
318#define	MSG_DONTWAIT	0x80		/* this message should be nonblocking */
319#define	MSG_EOF		0x100		/* data completes connection */
320#define MSG_COMPAT      0x8000		/* used in sendit() */
321
322/*
323 * Header for ancillary data objects in msg_control buffer.
324 * Used for additional information with/about a datagram
325 * not expressible by flags.  The format is a sequence
326 * of message elements headed by cmsghdr structures.
327 */
328struct cmsghdr {
329	socklen_t	cmsg_len;		/* data byte count, including hdr */
330	int		cmsg_level;		/* originating protocol */
331	int		cmsg_type;		/* protocol-specific type */
332/* followed by	unsigned char  cmsg_data[]; */
333};
334
335/*
336 * While we may have more groups than this, the cmsgcred struct must
337 * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow
338 * this.
339*/
340#define CMGROUP_MAX 16
341
342/*
343 * Credentials structure, used to verify the identity of a peer
344 * process that has sent us a message. This is allocated by the
345 * peer process but filled in by the kernel. This prevents the
346 * peer from lying about its identity. (Note that cmcred_groups[0]
347 * is the effective GID.)
348 */
349struct cmsgcred {
350	pid_t	cmcred_pid;		/* PID of sending process */
351	uid_t	cmcred_uid;		/* real UID of sending process */
352	uid_t	cmcred_euid;		/* effective UID of sending process */
353	gid_t	cmcred_gid;		/* real GID of sending process */
354	short	cmcred_ngroups;		/* number or groups */
355	gid_t	cmcred_groups[CMGROUP_MAX];	/* groups */
356};
357
358/*
359 * Round p (pointer or byte index) up to a correctly-aligned value
360 * for all data types (int, long, ...).   The result is unsigned int
361 * and must be cast to any desired pointer type.
362 */
363#ifndef _ALIGNBYTES
364#define _ALIGNBYTES (sizeof(int) - 1)
365#endif
366#ifndef _ALIGN
367#define _ALIGN(p) (((unsigned)(p) + _ALIGNBYTES) & ~_ALIGNBYTES)
368#endif
369
370/* given pointer to struct cmsghdr, return pointer to data */
371#define	CMSG_DATA(cmsg)		((unsigned char *)(cmsg) + \
372				 _ALIGN(sizeof(struct cmsghdr)))
373
374/* given pointer to struct cmsghdr, return pointer to next cmsghdr */
375#define	CMSG_NXTHDR(mhdr, cmsg)	\
376	(((caddr_t)(cmsg) + _ALIGN((cmsg)->cmsg_len) + \
377	  _ALIGN(sizeof(struct cmsghdr)) > \
378	    (caddr_t)(mhdr)->msg_control + (mhdr)->msg_controllen) ? \
379	    (struct cmsghdr *)NULL : \
380	    (struct cmsghdr *)((caddr_t)(cmsg) + _ALIGN((cmsg)->cmsg_len)))
381
382#define	CMSG_FIRSTHDR(mhdr)	((struct cmsghdr *)(mhdr)->msg_control)
383
384/* RFC 2292 additions */
385
386#define	CMSG_SPACE(l)		(_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(l))
387#define	CMSG_LEN(l)		(_ALIGN(sizeof(struct cmsghdr)) + (l))
388
389#ifdef _KERNEL
390#define	CMSG_ALIGN(n)	_ALIGN(n)
391#endif
392
393/* "Socket"-level control message types: */
394#define	SCM_RIGHTS	0x01		/* access rights (array of int) */
395#define	SCM_TIMESTAMP	0x02		/* timestamp (struct timeval) */
396#define	SCM_CREDS	0x03		/* process creds (struct cmsgcred) */
397
398/*
399 * 4.3 compat sockaddr, move to compat file later
400 */
401struct osockaddr {
402	u_short	sa_family;		/* address family */
403	char	sa_data[14];		/* up to 14 bytes of direct address */
404};
405
406/*
407 * 4.3-compat message header (move to compat file later).
408 */
409struct omsghdr {
410	caddr_t	msg_name;		/* optional address */
411	int	msg_namelen;		/* size of address */
412	struct	iovec *msg_iov;		/* scatter/gather array */
413	int	msg_iovlen;		/* # elements in msg_iov */
414	caddr_t	msg_accrights;		/* access rights sent/received */
415	int	msg_accrightslen;
416};
417
418/*
419 * howto arguments for shutdown(2), specified by Posix.1g.
420 */
421#define	SHUT_RD		0		/* shut down the reading side */
422#define	SHUT_WR		1		/* shut down the writing side */
423#define	SHUT_RDWR	2		/* shut down both sides */
424
425/*
426 * sendfile(2) header/trailer struct
427 */
428struct sf_hdtr {
429	struct iovec *headers;	/* pointer to an array of header struct iovec's */
430	int hdr_cnt;		/* number of header iovec's */
431	struct iovec *trailers;	/* pointer to an array of trailer struct iovec's */
432	int trl_cnt;		/* number of trailer iovec's */
433};
434
435#include <sys/cdefs.h>
436
437__BEGIN_DECLS
438int	accept(int, struct sockaddr *, socklen_t *);
439int	bind(int, const struct sockaddr *, socklen_t);
440int	connect(int, const struct sockaddr *, socklen_t);
441int	getpeername(int, struct sockaddr *, socklen_t *);
442int	getsockname(int, struct sockaddr *, socklen_t *);
443int	getsockopt(int, int, int, void *, socklen_t *);
444int	listen(int, int);
445ssize_t	recv(int, void *, size_t, int);
446ssize_t	recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
447ssize_t	recvmsg(int, struct msghdr *, int);
448ssize_t	send(int, const void *, size_t, int);
449ssize_t	sendto(int, const void *,
450	    size_t, int, const struct sockaddr *, socklen_t);
451ssize_t	sendmsg(int, const struct msghdr *, int);
452int	sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
453int	setsockopt(int, int, int, const void *, socklen_t);
454int	shutdown(int, int);
455int	socket(int, int, int);
456int	socketpair(int, int, int, int *);
457__END_DECLS
458
459#endif /* !_SYS_SOCKET_H_ */
460