• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_
54 typedef	_BSD_SA_FAMILY_T_	sa_family_t;
55 #undef _BSD_SA_FAMILY_T_
56 #endif
57 
58 #ifdef	_BSD_SOCKLEN_T_
59 typedef	_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  */
108 struct linger {
109 	int	l_onoff;		/* option on/off */
110 	int	l_linger;		/* linger time */
111 };
112 
113 struct 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  */
191 struct 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  */
201 struct 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 
215 struct sockaddr_storage {
216 	unsigned char		ss_len;		/* address length */
217 	sa_family_t	ss_family;	/* address family */
218 	char		__ss_pad1[_SS_PAD1SIZE];
219 	int64_t		__ss_align;	/* force desired structure storage alignment */
220 	char		__ss_pad2[_SS_PAD2SIZE];
221 };
222 
223 /*
224  * Definitions for network related sysctl, CTL_NET.
225  *
226  * Second level is protocol family.
227  * Third level is protocol number.
228  *
229  * Further levels are defined by the individual families below.
230  */
231 #define NET_MAXID	AF_MAX
232 
233 #define CTL_NET_NAMES { \
234 	{ 0, 0 }, \
235 	{ "unix", CTLTYPE_NODE }, \
236 	{ "inet", CTLTYPE_NODE }, \
237 	{ "implink", CTLTYPE_NODE }, \
238 	{ "pup", CTLTYPE_NODE }, \
239 	{ "chaos", CTLTYPE_NODE }, \
240 	{ "xerox_ns", CTLTYPE_NODE }, \
241 	{ "iso", CTLTYPE_NODE }, \
242 	{ "emca", CTLTYPE_NODE }, \
243 	{ "datakit", CTLTYPE_NODE }, \
244 	{ "ccitt", CTLTYPE_NODE }, \
245 	{ "ibm_sna", CTLTYPE_NODE }, \
246 	{ "decnet", CTLTYPE_NODE }, \
247 	{ "dec_dli", CTLTYPE_NODE }, \
248 	{ "lat", CTLTYPE_NODE }, \
249 	{ "hylink", CTLTYPE_NODE }, \
250 	{ "appletalk", CTLTYPE_NODE }, \
251 	{ "route", CTLTYPE_NODE }, \
252 	{ "link_layer", CTLTYPE_NODE }, \
253 	{ "xtp", CTLTYPE_NODE }, \
254 	{ "coip", CTLTYPE_NODE }, \
255 	{ "cnt", CTLTYPE_NODE }, \
256 	{ "rtip", CTLTYPE_NODE }, \
257 	{ "ipx", CTLTYPE_NODE }, \
258 	{ "sip", CTLTYPE_NODE }, \
259 	{ "pip", CTLTYPE_NODE }, \
260 	{ "isdn", CTLTYPE_NODE }, \
261 	{ "key", CTLTYPE_NODE }, \
262 	{ "inet6", CTLTYPE_NODE }, \
263 	{ "natm", CTLTYPE_NODE }, \
264 	{ "atm", CTLTYPE_NODE }, \
265 	{ "hdrcomplete", CTLTYPE_NODE }, \
266 	{ "netgraph", CTLTYPE_NODE }, \
267 	{ "snp", CTLTYPE_NODE }, \
268 	{ "scp", CTLTYPE_NODE }, \
269 }
270 
271 /*
272  * PF_ROUTE - Routing table
273  *
274  * Three additional levels are defined:
275  *	Fourth: address family, 0 is wildcard
276  *	Fifth: type of info, defined below
277  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
278  */
279 #define NET_RT_DUMP	1		/* dump; may limit to a.f. */
280 #define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
281 #define NET_RT_IFLIST	3		/* survey interface list */
282 #define	NET_RT_MAXID	4
283 
284 #define CTL_NET_RT_NAMES { \
285 	{ 0, 0 }, \
286 	{ "dump", CTLTYPE_STRUCT }, \
287 	{ "flags", CTLTYPE_STRUCT }, \
288 	{ "iflist", CTLTYPE_STRUCT }, \
289 }
290 
291 /*
292  * Maximum queue length specifiable by listen.
293  */
294 #ifndef	SOMAXCONN
295 #define	SOMAXCONN	128
296 #endif
297 
298 /*
299  * Message header for recvmsg and sendmsg calls.
300  * Used value-result for recvmsg, value only for sendmsg.
301  */
302 struct msghdr {
303 	void		*msg_name;		/* optional address */
304 	socklen_t	 msg_namelen;		/* size of address */
305 	struct iovec	*msg_iov;		/* scatter/gather array */
306 	int		 msg_iovlen;		/* # elements in msg_iov */
307 	void		*msg_control;		/* ancillary data, see below */
308 	socklen_t	 msg_controllen;	/* ancillary data buffer len */
309 	int		 msg_flags;		/* flags on received message */
310 };
311 
312 #define	MSG_OOB		0x1		/* process out-of-band data */
313 #define	MSG_PEEK	0x2		/* peek at incoming message */
314 #define	MSG_DONTROUTE	0x4		/* send without using routing tables */
315 #define	MSG_EOR		0x8		/* data completes record */
316 #define	MSG_TRUNC	0x10		/* data discarded before delivery */
317 #define	MSG_CTRUNC	0x20		/* control data lost before delivery */
318 #define	MSG_WAITALL	0x40		/* wait for full request or error */
319 #define	MSG_DONTWAIT	0x80		/* this message should be nonblocking */
320 #define	MSG_EOF		0x100		/* data completes connection */
321 #define MSG_COMPAT      0x8000		/* used in sendit() */
322 
323 /*
324  * Header for ancillary data objects in msg_control buffer.
325  * Used for additional information with/about a datagram
326  * not expressible by flags.  The format is a sequence
327  * of message elements headed by cmsghdr structures.
328  */
329 struct cmsghdr {
330 	socklen_t	cmsg_len;		/* data byte count, including hdr */
331 	int		cmsg_level;		/* originating protocol */
332 	int		cmsg_type;		/* protocol-specific type */
333 /* followed by	unsigned char  cmsg_data[]; */
334 };
335 
336 /*
337  * While we may have more groups than this, the cmsgcred struct must
338  * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow
339  * this.
340 */
341 #define CMGROUP_MAX 16
342 
343 /*
344  * Credentials structure, used to verify the identity of a peer
345  * process that has sent us a message. This is allocated by the
346  * peer process but filled in by the kernel. This prevents the
347  * peer from lying about its identity. (Note that cmcred_groups[0]
348  * is the effective GID.)
349  */
350 struct cmsgcred {
351 	pid_t	cmcred_pid;		/* PID of sending process */
352 	uid_t	cmcred_uid;		/* real UID of sending process */
353 	uid_t	cmcred_euid;		/* effective UID of sending process */
354 	gid_t	cmcred_gid;		/* real GID of sending process */
355 	short	cmcred_ngroups;		/* number or groups */
356 	gid_t	cmcred_groups[CMGROUP_MAX];	/* groups */
357 };
358 
359 /* given pointer to struct cmsghdr, return pointer to data */
360 #define	CMSG_DATA(cmsg)		((unsigned char *)(cmsg) + \
361 				 _ALIGN(sizeof(struct cmsghdr)))
362 
363 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */
364 #define	CMSG_NXTHDR(mhdr, cmsg)	\
365 	(((caddr_t)(cmsg) + _ALIGN((cmsg)->cmsg_len) + \
366 	  _ALIGN(sizeof(struct cmsghdr)) > \
367 	    (caddr_t)(mhdr)->msg_control + (mhdr)->msg_controllen) ? \
368 	    (struct cmsghdr *)NULL : \
369 	    (struct cmsghdr *)((caddr_t)(cmsg) + _ALIGN((cmsg)->cmsg_len)))
370 
371 #define	CMSG_FIRSTHDR(mhdr)	((struct cmsghdr *)(mhdr)->msg_control)
372 
373 /* RFC 2292 additions */
374 
375 #define	CMSG_SPACE(l)		(_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(l))
376 #define	CMSG_LEN(l)		(_ALIGN(sizeof(struct cmsghdr)) + (l))
377 
378 #ifdef _KERNEL
379 #define	CMSG_ALIGN(n)	_ALIGN(n)
380 #endif
381 
382 /* "Socket"-level control message types: */
383 #define	SCM_RIGHTS	0x01		/* access rights (array of int) */
384 #define	SCM_TIMESTAMP	0x02		/* timestamp (struct timeval) */
385 #define	SCM_CREDS	0x03		/* process creds (struct cmsgcred) */
386 
387 /*
388  * 4.3 compat sockaddr, move to compat file later
389  */
390 struct osockaddr {
391 	u_short	sa_family;		/* address family */
392 	char	sa_data[14];		/* up to 14 bytes of direct address */
393 };
394 
395 /*
396  * 4.3-compat message header (move to compat file later).
397  */
398 struct omsghdr {
399 	caddr_t	msg_name;		/* optional address */
400 	int	msg_namelen;		/* size of address */
401 	struct	iovec *msg_iov;		/* scatter/gather array */
402 	int	msg_iovlen;		/* # elements in msg_iov */
403 	caddr_t	msg_accrights;		/* access rights sent/received */
404 	int	msg_accrightslen;
405 };
406 
407 /*
408  * howto arguments for shutdown(2), specified by Posix.1g.
409  */
410 #define	SHUT_RD		0		/* shut down the reading side */
411 #define	SHUT_WR		1		/* shut down the writing side */
412 #define	SHUT_RDWR	2		/* shut down both sides */
413 
414 /*
415  * sendfile(2) header/trailer struct
416  */
417 struct sf_hdtr {
418 	struct iovec *headers;	/* pointer to an array of header struct iovec's */
419 	int hdr_cnt;		/* number of header iovec's */
420 	struct iovec *trailers;	/* pointer to an array of trailer struct iovec's */
421 	int trl_cnt;		/* number of trailer iovec's */
422 };
423 
424 #include <sys/cdefs.h>
425 
426 __BEGIN_DECLS
427 int	accept(int, struct sockaddr *, socklen_t *);
428 int	bind(int, const struct sockaddr *, socklen_t);
429 int	connect(int, const struct sockaddr *, socklen_t);
430 int	getpeername(int, struct sockaddr *, socklen_t *);
431 int	getsockname(int, struct sockaddr *, socklen_t *);
432 int	getsockopt(int, int, int, void *, socklen_t *);
433 int	listen(int, int);
434 ssize_t	recv(int, void *, size_t, int);
435 ssize_t	recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *);
436 ssize_t	recvmsg(int, struct msghdr *, int);
437 ssize_t	send(int, const void *, size_t, int);
438 ssize_t	sendto(int, const void *,
439 	    size_t, int, const struct sockaddr *, socklen_t);
440 ssize_t	sendmsg(int, const struct msghdr *, int);
441 int	sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
442 int	setsockopt(int, int, int, const void *, socklen_t);
443 int	shutdown(int, int);
444 int	socket(int, int, int);
445 int	socketpair(int, int, int, int *);
446 __END_DECLS
447 
448 #endif /* !_SYS_SOCKET_H_ */
449