socket.h revision 3438
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
331541Srgrimes *	@(#)socket.h	8.4 (Berkeley) 2/21/94
343438Sphk * $Id: socket.h,v 1.3 1994/10/02 17:24:56 phk Exp $
351541Srgrimes */
361541Srgrimes
371541Srgrimes#ifndef _SYS_SOCKET_H_
381541Srgrimes#define	_SYS_SOCKET_H_
391541Srgrimes
401541Srgrimes/*
411541Srgrimes * Definitions related to sockets: types, address families, options.
421541Srgrimes */
431541Srgrimes
441541Srgrimes/*
451541Srgrimes * Types
461541Srgrimes */
471541Srgrimes#define	SOCK_STREAM	1		/* stream socket */
481541Srgrimes#define	SOCK_DGRAM	2		/* datagram socket */
491541Srgrimes#define	SOCK_RAW	3		/* raw-protocol interface */
501541Srgrimes#define	SOCK_RDM	4		/* reliably-delivered message */
511541Srgrimes#define	SOCK_SEQPACKET	5		/* sequenced packet stream */
521541Srgrimes
531541Srgrimes/*
541541Srgrimes * Option flags per-socket.
551541Srgrimes */
561541Srgrimes#define	SO_DEBUG	0x0001		/* turn on debugging info recording */
571541Srgrimes#define	SO_ACCEPTCONN	0x0002		/* socket has had listen() */
581541Srgrimes#define	SO_REUSEADDR	0x0004		/* allow local address reuse */
591541Srgrimes#define	SO_KEEPALIVE	0x0008		/* keep connections alive */
601541Srgrimes#define	SO_DONTROUTE	0x0010		/* just use interface addresses */
611541Srgrimes#define	SO_BROADCAST	0x0020		/* permit sending of broadcast msgs */
621541Srgrimes#define	SO_USELOOPBACK	0x0040		/* bypass hardware when possible */
631541Srgrimes#define	SO_LINGER	0x0080		/* linger on close if data present */
641541Srgrimes#define	SO_OOBINLINE	0x0100		/* leave received OOB data in line */
651541Srgrimes#define	SO_REUSEPORT	0x0200		/* allow local address & port reuse */
661541Srgrimes
671541Srgrimes/*
681541Srgrimes * Additional options, not kept in so_options.
691541Srgrimes */
701541Srgrimes#define SO_SNDBUF	0x1001		/* send buffer size */
711541Srgrimes#define SO_RCVBUF	0x1002		/* receive buffer size */
721541Srgrimes#define SO_SNDLOWAT	0x1003		/* send low-water mark */
731541Srgrimes#define SO_RCVLOWAT	0x1004		/* receive low-water mark */
741541Srgrimes#define SO_SNDTIMEO	0x1005		/* send timeout */
751541Srgrimes#define SO_RCVTIMEO	0x1006		/* receive timeout */
761541Srgrimes#define	SO_ERROR	0x1007		/* get error status and clear */
771541Srgrimes#define	SO_TYPE		0x1008		/* get socket type */
781541Srgrimes
791541Srgrimes/*
801541Srgrimes * Structure used for manipulating linger option.
811541Srgrimes */
821541Srgrimesstruct	linger {
831541Srgrimes	int	l_onoff;		/* option on/off */
841541Srgrimes	int	l_linger;		/* linger time */
851541Srgrimes};
861541Srgrimes
871541Srgrimes/*
881541Srgrimes * Level number for (get/set)sockopt() to apply to socket itself.
891541Srgrimes */
901541Srgrimes#define	SOL_SOCKET	0xffff		/* options for socket level */
911541Srgrimes
921541Srgrimes/*
931541Srgrimes * Address families.
941541Srgrimes */
951541Srgrimes#define	AF_UNSPEC	0		/* unspecified */
961541Srgrimes#define	AF_LOCAL	1		/* local to host (pipes, portals) */
971541Srgrimes#define	AF_UNIX		AF_LOCAL	/* backward compatibility */
981541Srgrimes#define	AF_INET		2		/* internetwork: UDP, TCP, etc. */
991541Srgrimes#define	AF_IMPLINK	3		/* arpanet imp addresses */
1001541Srgrimes#define	AF_PUP		4		/* pup protocols: e.g. BSP */
1011541Srgrimes#define	AF_CHAOS	5		/* mit CHAOS protocols */
1021541Srgrimes#define	AF_NS		6		/* XEROX NS protocols */
1031541Srgrimes#define	AF_ISO		7		/* ISO protocols */
1041541Srgrimes#define	AF_OSI		AF_ISO
1051541Srgrimes#define	AF_ECMA		8		/* european computer manufacturers */
1061541Srgrimes#define	AF_DATAKIT	9		/* datakit protocols */
1071541Srgrimes#define	AF_CCITT	10		/* CCITT protocols, X.25 etc */
1081541Srgrimes#define	AF_SNA		11		/* IBM SNA */
1091541Srgrimes#define AF_DECnet	12		/* DECnet */
1101541Srgrimes#define AF_DLI		13		/* DEC Direct data link interface */
1111541Srgrimes#define AF_LAT		14		/* LAT */
1121541Srgrimes#define	AF_HYLINK	15		/* NSC Hyperchannel */
1131541Srgrimes#define	AF_APPLETALK	16		/* Apple Talk */
1141541Srgrimes#define	AF_ROUTE	17		/* Internal Routing Protocol */
1151541Srgrimes#define	AF_LINK		18		/* Link layer interface */
1161541Srgrimes#define	pseudo_AF_XTP	19		/* eXpress Transfer Protocol (no AF) */
1171541Srgrimes#define	AF_COIP		20		/* connection-oriented IP, aka ST II */
1181541Srgrimes#define	AF_CNT		21		/* Computer Network Technology */
1191541Srgrimes#define pseudo_AF_RTIP	22		/* Help Identify RTIP packets */
1201541Srgrimes#define	AF_IPX		23		/* Novell Internet Protocol */
1211541Srgrimes#define	AF_SIP		24		/* Simple Internet Protocol */
1221541Srgrimes#define pseudo_AF_PIP	25		/* Help Identify PIP packets */
1231541Srgrimes
1241541Srgrimes#define	AF_MAX		26
1251541Srgrimes
1261541Srgrimes/*
1271541Srgrimes * Structure used by kernel to store most
1281541Srgrimes * addresses.
1291541Srgrimes */
1301541Srgrimesstruct sockaddr {
1311541Srgrimes	u_char	sa_len;			/* total length */
1321541Srgrimes	u_char	sa_family;		/* address family */
1331541Srgrimes	char	sa_data[14];		/* actually longer; address value */
1341541Srgrimes};
1351541Srgrimes
1361541Srgrimes/*
1371541Srgrimes * Structure used by kernel to pass protocol
1381541Srgrimes * information in raw sockets.
1391541Srgrimes */
1401541Srgrimesstruct sockproto {
1411541Srgrimes	u_short	sp_family;		/* address family */
1421541Srgrimes	u_short	sp_protocol;		/* protocol */
1431541Srgrimes};
1441541Srgrimes
1451541Srgrimes/*
1461541Srgrimes * Protocol families, same as address families for now.
1471541Srgrimes */
1481541Srgrimes#define	PF_UNSPEC	AF_UNSPEC
1491541Srgrimes#define	PF_LOCAL	AF_LOCAL
1501541Srgrimes#define	PF_UNIX		PF_LOCAL	/* backward compatibility */
1511541Srgrimes#define	PF_INET		AF_INET
1521541Srgrimes#define	PF_IMPLINK	AF_IMPLINK
1531541Srgrimes#define	PF_PUP		AF_PUP
1541541Srgrimes#define	PF_CHAOS	AF_CHAOS
1551541Srgrimes#define	PF_NS		AF_NS
1561541Srgrimes#define	PF_ISO		AF_ISO
1571541Srgrimes#define	PF_OSI		AF_ISO
1581541Srgrimes#define	PF_ECMA		AF_ECMA
1591541Srgrimes#define	PF_DATAKIT	AF_DATAKIT
1601541Srgrimes#define	PF_CCITT	AF_CCITT
1611541Srgrimes#define	PF_SNA		AF_SNA
1621541Srgrimes#define PF_DECnet	AF_DECnet
1631541Srgrimes#define PF_DLI		AF_DLI
1641541Srgrimes#define PF_LAT		AF_LAT
1651541Srgrimes#define	PF_HYLINK	AF_HYLINK
1661541Srgrimes#define	PF_APPLETALK	AF_APPLETALK
1671541Srgrimes#define	PF_ROUTE	AF_ROUTE
1681541Srgrimes#define	PF_LINK		AF_LINK
1691541Srgrimes#define	PF_XTP		pseudo_AF_XTP	/* really just proto family, no AF */
1701541Srgrimes#define	PF_COIP		AF_COIP
1711541Srgrimes#define	PF_CNT		AF_CNT
1721541Srgrimes#define	PF_SIP		AF_SIP
1731541Srgrimes#define	PF_IPX		AF_IPX		/* same format as AF_NS */
1741541Srgrimes#define PF_RTIP		pseudo_AF_FTIP	/* same format as AF_INET */
1751541Srgrimes#define PF_PIP		pseudo_AF_PIP
1761541Srgrimes
1771541Srgrimes#define	PF_MAX		AF_MAX
1781541Srgrimes
1791541Srgrimes/*
1801541Srgrimes * Definitions for network related sysctl, CTL_NET.
1811541Srgrimes *
1821541Srgrimes * Second level is protocol family.
1831541Srgrimes * Third level is protocol number.
1841541Srgrimes *
1851541Srgrimes * Further levels are defined by the individual families below.
1861541Srgrimes */
1871541Srgrimes#define NET_MAXID	AF_MAX
1881541Srgrimes
1891541Srgrimes#define CTL_NET_NAMES { \
1901541Srgrimes	{ 0, 0 }, \
1911541Srgrimes	{ "unix", CTLTYPE_NODE }, \
1921541Srgrimes	{ "inet", CTLTYPE_NODE }, \
1931541Srgrimes	{ "implink", CTLTYPE_NODE }, \
1941541Srgrimes	{ "pup", CTLTYPE_NODE }, \
1951541Srgrimes	{ "chaos", CTLTYPE_NODE }, \
1961541Srgrimes	{ "xerox_ns", CTLTYPE_NODE }, \
1971541Srgrimes	{ "iso", CTLTYPE_NODE }, \
1981541Srgrimes	{ "emca", CTLTYPE_NODE }, \
1991541Srgrimes	{ "datakit", CTLTYPE_NODE }, \
2001541Srgrimes	{ "ccitt", CTLTYPE_NODE }, \
2011541Srgrimes	{ "ibm_sna", CTLTYPE_NODE }, \
2021541Srgrimes	{ "decnet", CTLTYPE_NODE }, \
2031541Srgrimes	{ "dec_dli", CTLTYPE_NODE }, \
2041541Srgrimes	{ "lat", CTLTYPE_NODE }, \
2051541Srgrimes	{ "hylink", CTLTYPE_NODE }, \
2061541Srgrimes	{ "appletalk", CTLTYPE_NODE }, \
2071541Srgrimes	{ "route", CTLTYPE_NODE }, \
2081541Srgrimes	{ "link_layer", CTLTYPE_NODE }, \
2091541Srgrimes	{ "xtp", CTLTYPE_NODE }, \
2101541Srgrimes	{ "coip", CTLTYPE_NODE }, \
2111541Srgrimes	{ "cnt", CTLTYPE_NODE }, \
2121541Srgrimes	{ "rtip", CTLTYPE_NODE }, \
2131541Srgrimes	{ "ipx", CTLTYPE_NODE }, \
2141541Srgrimes	{ "sip", CTLTYPE_NODE }, \
2151541Srgrimes	{ "pip", CTLTYPE_NODE }, \
2161541Srgrimes}
2171541Srgrimes
2181541Srgrimes/*
2191541Srgrimes * PF_ROUTE - Routing table
2201541Srgrimes *
2211541Srgrimes * Three additional levels are defined:
2221541Srgrimes *	Fourth: address family, 0 is wildcard
2231541Srgrimes *	Fifth: type of info, defined below
2241541Srgrimes *	Sixth: flag(s) to mask with for NET_RT_FLAGS
2251541Srgrimes */
2261541Srgrimes#define NET_RT_DUMP	1		/* dump; may limit to a.f. */
2271541Srgrimes#define NET_RT_FLAGS	2		/* by flags, e.g. RESOLVING */
2281541Srgrimes#define NET_RT_IFLIST	3		/* survey interface list */
2291541Srgrimes#define	NET_RT_MAXID	4
2301541Srgrimes
2311541Srgrimes#define CTL_NET_RT_NAMES { \
2321541Srgrimes	{ 0, 0 }, \
2331541Srgrimes	{ "dump", CTLTYPE_STRUCT }, \
2341541Srgrimes	{ "flags", CTLTYPE_STRUCT }, \
2351541Srgrimes	{ "iflist", CTLTYPE_STRUCT }, \
2361541Srgrimes}
2371541Srgrimes
2381541Srgrimes/*
2391541Srgrimes * Maximum queue length specifiable by listen.
2401541Srgrimes */
2411541Srgrimes#define	SOMAXCONN	5
2421541Srgrimes
2431541Srgrimes/*
2441541Srgrimes * Message header for recvmsg and sendmsg calls.
2451541Srgrimes * Used value-result for recvmsg, value only for sendmsg.
2461541Srgrimes */
2471541Srgrimesstruct msghdr {
2481541Srgrimes	caddr_t	msg_name;		/* optional address */
2491541Srgrimes	u_int	msg_namelen;		/* size of address */
2501541Srgrimes	struct	iovec *msg_iov;		/* scatter/gather array */
2511541Srgrimes	u_int	msg_iovlen;		/* # elements in msg_iov */
2521541Srgrimes	caddr_t	msg_control;		/* ancillary data, see below */
2531541Srgrimes	u_int	msg_controllen;		/* ancillary data buffer len */
2541541Srgrimes	int	msg_flags;		/* flags on received message */
2551541Srgrimes};
2561541Srgrimes
2571541Srgrimes#define	MSG_OOB		0x1		/* process out-of-band data */
2581541Srgrimes#define	MSG_PEEK	0x2		/* peek at incoming message */
2591541Srgrimes#define	MSG_DONTROUTE	0x4		/* send without using routing tables */
2601541Srgrimes#define	MSG_EOR		0x8		/* data completes record */
2611541Srgrimes#define	MSG_TRUNC	0x10		/* data discarded before delivery */
2621541Srgrimes#define	MSG_CTRUNC	0x20		/* control data lost before delivery */
2631541Srgrimes#define	MSG_WAITALL	0x40		/* wait for full request or error */
2641541Srgrimes#define	MSG_DONTWAIT	0x80		/* this message should be nonblocking */
2653304Sphk#define MSG_COMPAT      0x8000		/* used in sendit() */
2661541Srgrimes
2671541Srgrimes/*
2681541Srgrimes * Header for ancillary data objects in msg_control buffer.
2691541Srgrimes * Used for additional information with/about a datagram
2701541Srgrimes * not expressible by flags.  The format is a sequence
2711541Srgrimes * of message elements headed by cmsghdr structures.
2721541Srgrimes */
2731541Srgrimesstruct cmsghdr {
2741541Srgrimes	u_int	cmsg_len;		/* data byte count, including hdr */
2751541Srgrimes	int	cmsg_level;		/* originating protocol */
2761541Srgrimes	int	cmsg_type;		/* protocol-specific type */
2771541Srgrimes/* followed by	u_char  cmsg_data[]; */
2781541Srgrimes};
2791541Srgrimes
2801541Srgrimes/* given pointer to struct cmsghdr, return pointer to data */
2811541Srgrimes#define	CMSG_DATA(cmsg)		((u_char *)((cmsg) + 1))
2821541Srgrimes
2831541Srgrimes/* given pointer to struct cmsghdr, return pointer to next cmsghdr */
2841541Srgrimes#define	CMSG_NXTHDR(mhdr, cmsg)	\
2851541Srgrimes	(((caddr_t)(cmsg) + (cmsg)->cmsg_len + sizeof(struct cmsghdr) > \
2861541Srgrimes	    (mhdr)->msg_control + (mhdr)->msg_controllen) ? \
2871541Srgrimes	    (struct cmsghdr *)NULL : \
2881541Srgrimes	    (struct cmsghdr *)((caddr_t)(cmsg) + ALIGN((cmsg)->cmsg_len)))
2891541Srgrimes
2901541Srgrimes#define	CMSG_FIRSTHDR(mhdr)	((struct cmsghdr *)(mhdr)->msg_control)
2911541Srgrimes
2921541Srgrimes/* "Socket"-level control message types: */
2931541Srgrimes#define	SCM_RIGHTS	0x01		/* access rights (array of int) */
2941541Srgrimes
2951541Srgrimes/*
2961541Srgrimes * 4.3 compat sockaddr, move to compat file later
2971541Srgrimes */
2981541Srgrimesstruct osockaddr {
2991541Srgrimes	u_short	sa_family;		/* address family */
3001541Srgrimes	char	sa_data[14];		/* up to 14 bytes of direct address */
3011541Srgrimes};
3021541Srgrimes
3031541Srgrimes/*
3041541Srgrimes * 4.3-compat message header (move to compat file later).
3051541Srgrimes */
3061541Srgrimesstruct omsghdr {
3071541Srgrimes	caddr_t	msg_name;		/* optional address */
3081541Srgrimes	int	msg_namelen;		/* size of address */
3091541Srgrimes	struct	iovec *msg_iov;		/* scatter/gather array */
3101541Srgrimes	int	msg_iovlen;		/* # elements in msg_iov */
3111541Srgrimes	caddr_t	msg_accrights;		/* access rights sent/received */
3121541Srgrimes	int	msg_accrightslen;
3131541Srgrimes};
3141541Srgrimes
3151541Srgrimes#ifndef	KERNEL
3161541Srgrimes
3171541Srgrimes#include <sys/cdefs.h>
3181541Srgrimes
3191541Srgrimes__BEGIN_DECLS
3201541Srgrimesint	accept __P((int, struct sockaddr *, int *));
3211541Srgrimesint	bind __P((int, const struct sockaddr *, int));
3221541Srgrimesint	connect __P((int, const struct sockaddr *, int));
3231541Srgrimesint	getpeername __P((int, struct sockaddr *, int *));
3241541Srgrimesint	getsockname __P((int, struct sockaddr *, int *));
3251541Srgrimesint	getsockopt __P((int, int, int, void *, int *));
3261541Srgrimesint	listen __P((int, int));
3271541Srgrimesssize_t	recv __P((int, void *, size_t, int));
3281541Srgrimesssize_t	recvfrom __P((int, void *, size_t, int, struct sockaddr *, int *));
3291541Srgrimesssize_t	recvmsg __P((int, struct msghdr *, int));
3301541Srgrimesssize_t	send __P((int, const void *, size_t, int));
3311541Srgrimesssize_t	sendto __P((int, const void *,
3321541Srgrimes	    size_t, int, const struct sockaddr *, int));
3331541Srgrimesssize_t	sendmsg __P((int, const struct msghdr *, int));
3341541Srgrimesint	setsockopt __P((int, int, int, const void *, int));
3351541Srgrimesint	shutdown __P((int, int));
3361541Srgrimesint	socket __P((int, int, int));
3371541Srgrimesint	socketpair __P((int, int, int, int *));
3381541Srgrimes__END_DECLS
3391541Srgrimes
3403438Sphk#else /* KERNEL */
3413438Sphkvoid	pfctlinput __P((int, struct sockaddr *));
3421541Srgrimes#endif /* !KERNEL */
3431541Srgrimes#endif /* !_SYS_SOCKET_H_ */
344