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