socket.h revision 33006
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 3433006Salex * $Id: socket.h,v 1.23 1997/12/21 16:35:11 bde 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 */ 6615701Swollman#define SO_TIMESTAMP 0x0400 /* timestamp received dgram traffic */ 671541Srgrimes 681541Srgrimes/* 691541Srgrimes * Additional options, not kept in so_options. 701541Srgrimes */ 711541Srgrimes#define SO_SNDBUF 0x1001 /* send buffer size */ 721541Srgrimes#define SO_RCVBUF 0x1002 /* receive buffer size */ 731541Srgrimes#define SO_SNDLOWAT 0x1003 /* send low-water mark */ 741541Srgrimes#define SO_RCVLOWAT 0x1004 /* receive low-water mark */ 751541Srgrimes#define SO_SNDTIMEO 0x1005 /* send timeout */ 761541Srgrimes#define SO_RCVTIMEO 0x1006 /* receive timeout */ 771541Srgrimes#define SO_ERROR 0x1007 /* get error status and clear */ 781541Srgrimes#define SO_TYPE 0x1008 /* get socket type */ 7925311Swollman/*efine SO_PRIVSTATE 0x1009 get/deny privileged state */ 801541Srgrimes 811541Srgrimes/* 821541Srgrimes * Structure used for manipulating linger option. 831541Srgrimes */ 841541Srgrimesstruct linger { 851541Srgrimes int l_onoff; /* option on/off */ 861541Srgrimes int l_linger; /* linger time */ 871541Srgrimes}; 881541Srgrimes 891541Srgrimes/* 901541Srgrimes * Level number for (get/set)sockopt() to apply to socket itself. 911541Srgrimes */ 921541Srgrimes#define SOL_SOCKET 0xffff /* options for socket level */ 931541Srgrimes 941541Srgrimes/* 951541Srgrimes * Address families. 961541Srgrimes */ 971541Srgrimes#define AF_UNSPEC 0 /* unspecified */ 981541Srgrimes#define AF_LOCAL 1 /* local to host (pipes, portals) */ 991541Srgrimes#define AF_UNIX AF_LOCAL /* backward compatibility */ 1001541Srgrimes#define AF_INET 2 /* internetwork: UDP, TCP, etc. */ 1011541Srgrimes#define AF_IMPLINK 3 /* arpanet imp addresses */ 1021541Srgrimes#define AF_PUP 4 /* pup protocols: e.g. BSP */ 1031541Srgrimes#define AF_CHAOS 5 /* mit CHAOS protocols */ 1041541Srgrimes#define AF_NS 6 /* XEROX NS protocols */ 1051541Srgrimes#define AF_ISO 7 /* ISO protocols */ 1061541Srgrimes#define AF_OSI AF_ISO 10713765Smpp#define AF_ECMA 8 /* European computer manufacturers */ 1081541Srgrimes#define AF_DATAKIT 9 /* datakit protocols */ 1091541Srgrimes#define AF_CCITT 10 /* CCITT protocols, X.25 etc */ 1101541Srgrimes#define AF_SNA 11 /* IBM SNA */ 1111541Srgrimes#define AF_DECnet 12 /* DECnet */ 1121541Srgrimes#define AF_DLI 13 /* DEC Direct data link interface */ 1131541Srgrimes#define AF_LAT 14 /* LAT */ 1141541Srgrimes#define AF_HYLINK 15 /* NSC Hyperchannel */ 1151541Srgrimes#define AF_APPLETALK 16 /* Apple Talk */ 1161541Srgrimes#define AF_ROUTE 17 /* Internal Routing Protocol */ 1171541Srgrimes#define AF_LINK 18 /* Link layer interface */ 1181541Srgrimes#define pseudo_AF_XTP 19 /* eXpress Transfer Protocol (no AF) */ 1191541Srgrimes#define AF_COIP 20 /* connection-oriented IP, aka ST II */ 1201541Srgrimes#define AF_CNT 21 /* Computer Network Technology */ 1211541Srgrimes#define pseudo_AF_RTIP 22 /* Help Identify RTIP packets */ 1221541Srgrimes#define AF_IPX 23 /* Novell Internet Protocol */ 1231541Srgrimes#define AF_SIP 24 /* Simple Internet Protocol */ 1245413Sse#define pseudo_AF_PIP 25 /* Help Identify PIP packets */ 1255413Sse#define AF_ISDN 26 /* Integrated Services Digital Network*/ 1265413Sse#define AF_E164 AF_ISDN /* CCITT E.164 recommendation */ 12716368Swollman#define pseudo_AF_KEY 27 /* Internal key-management function */ 12817938Speter#define AF_INET6 28 /* IPv6 */ 12925609Skjc#define AF_NATM 29 /* native ATM access */ 1301541Srgrimes 13125609Skjc#define AF_MAX 30 1321541Srgrimes 1331541Srgrimes/* 1341541Srgrimes * Structure used by kernel to store most 1351541Srgrimes * addresses. 1361541Srgrimes */ 1371541Srgrimesstruct sockaddr { 1381541Srgrimes u_char sa_len; /* total length */ 1391541Srgrimes u_char sa_family; /* address family */ 1401541Srgrimes char sa_data[14]; /* actually longer; address value */ 1411541Srgrimes}; 14228270Swollman#define SOCK_MAXADDRLEN 255 /* longest possible addresses */ 1431541Srgrimes 1441541Srgrimes/* 1451541Srgrimes * Structure used by kernel to pass protocol 1461541Srgrimes * information in raw sockets. 1471541Srgrimes */ 1481541Srgrimesstruct sockproto { 1491541Srgrimes u_short sp_family; /* address family */ 1501541Srgrimes u_short sp_protocol; /* protocol */ 1511541Srgrimes}; 1521541Srgrimes 1531541Srgrimes/* 1541541Srgrimes * Protocol families, same as address families for now. 1551541Srgrimes */ 1561541Srgrimes#define PF_UNSPEC AF_UNSPEC 1571541Srgrimes#define PF_LOCAL AF_LOCAL 1581541Srgrimes#define PF_UNIX PF_LOCAL /* backward compatibility */ 1591541Srgrimes#define PF_INET AF_INET 1601541Srgrimes#define PF_IMPLINK AF_IMPLINK 1611541Srgrimes#define PF_PUP AF_PUP 1621541Srgrimes#define PF_CHAOS AF_CHAOS 1631541Srgrimes#define PF_NS AF_NS 1641541Srgrimes#define PF_ISO AF_ISO 1651541Srgrimes#define PF_OSI AF_ISO 1661541Srgrimes#define PF_ECMA AF_ECMA 1671541Srgrimes#define PF_DATAKIT AF_DATAKIT 1681541Srgrimes#define PF_CCITT AF_CCITT 1691541Srgrimes#define PF_SNA AF_SNA 1701541Srgrimes#define PF_DECnet AF_DECnet 1711541Srgrimes#define PF_DLI AF_DLI 1721541Srgrimes#define PF_LAT AF_LAT 1731541Srgrimes#define PF_HYLINK AF_HYLINK 1741541Srgrimes#define PF_APPLETALK AF_APPLETALK 1751541Srgrimes#define PF_ROUTE AF_ROUTE 1761541Srgrimes#define PF_LINK AF_LINK 1771541Srgrimes#define PF_XTP pseudo_AF_XTP /* really just proto family, no AF */ 1781541Srgrimes#define PF_COIP AF_COIP 1791541Srgrimes#define PF_CNT AF_CNT 1801541Srgrimes#define PF_SIP AF_SIP 1811541Srgrimes#define PF_IPX AF_IPX /* same format as AF_NS */ 18217603Sjdp#define PF_RTIP pseudo_AF_RTIP /* same format as AF_INET */ 1831541Srgrimes#define PF_PIP pseudo_AF_PIP 1845413Sse#define PF_ISDN AF_ISDN 18516480Swollman#define PF_KEY pseudo_AF_KEY 18617938Speter#define PF_INET6 AF_INET6 18725609Skjc#define PF_NATM AF_NATM 1881541Srgrimes 1891541Srgrimes#define PF_MAX AF_MAX 1901541Srgrimes 1911541Srgrimes/* 1921541Srgrimes * Definitions for network related sysctl, CTL_NET. 1931541Srgrimes * 1941541Srgrimes * Second level is protocol family. 1951541Srgrimes * Third level is protocol number. 1961541Srgrimes * 1971541Srgrimes * Further levels are defined by the individual families below. 1981541Srgrimes */ 1991541Srgrimes#define NET_MAXID AF_MAX 2001541Srgrimes 2011541Srgrimes#define CTL_NET_NAMES { \ 2021541Srgrimes { 0, 0 }, \ 2031541Srgrimes { "unix", CTLTYPE_NODE }, \ 2041541Srgrimes { "inet", CTLTYPE_NODE }, \ 2051541Srgrimes { "implink", CTLTYPE_NODE }, \ 2061541Srgrimes { "pup", CTLTYPE_NODE }, \ 2071541Srgrimes { "chaos", CTLTYPE_NODE }, \ 2081541Srgrimes { "xerox_ns", CTLTYPE_NODE }, \ 2091541Srgrimes { "iso", CTLTYPE_NODE }, \ 2101541Srgrimes { "emca", CTLTYPE_NODE }, \ 2111541Srgrimes { "datakit", CTLTYPE_NODE }, \ 2121541Srgrimes { "ccitt", CTLTYPE_NODE }, \ 2131541Srgrimes { "ibm_sna", CTLTYPE_NODE }, \ 2141541Srgrimes { "decnet", CTLTYPE_NODE }, \ 2151541Srgrimes { "dec_dli", CTLTYPE_NODE }, \ 2161541Srgrimes { "lat", CTLTYPE_NODE }, \ 2171541Srgrimes { "hylink", CTLTYPE_NODE }, \ 2181541Srgrimes { "appletalk", CTLTYPE_NODE }, \ 2191541Srgrimes { "route", CTLTYPE_NODE }, \ 2201541Srgrimes { "link_layer", CTLTYPE_NODE }, \ 2211541Srgrimes { "xtp", CTLTYPE_NODE }, \ 2221541Srgrimes { "coip", CTLTYPE_NODE }, \ 2231541Srgrimes { "cnt", CTLTYPE_NODE }, \ 2241541Srgrimes { "rtip", CTLTYPE_NODE }, \ 2251541Srgrimes { "ipx", CTLTYPE_NODE }, \ 2261541Srgrimes { "sip", CTLTYPE_NODE }, \ 2271541Srgrimes { "pip", CTLTYPE_NODE }, \ 22816368Swollman { "isdn", CTLTYPE_NODE }, \ 22916368Swollman { "key", CTLTYPE_NODE }, \ 23033006Salex { "inet6", CTLTYPE_NODE }, \ 23125609Skjc { "natm", CTLTYPE_NODE }, \ 2321541Srgrimes} 2331541Srgrimes 2341541Srgrimes/* 2351541Srgrimes * PF_ROUTE - Routing table 2361541Srgrimes * 2371541Srgrimes * Three additional levels are defined: 2381541Srgrimes * Fourth: address family, 0 is wildcard 2391541Srgrimes * Fifth: type of info, defined below 2401541Srgrimes * Sixth: flag(s) to mask with for NET_RT_FLAGS 2411541Srgrimes */ 2421541Srgrimes#define NET_RT_DUMP 1 /* dump; may limit to a.f. */ 2431541Srgrimes#define NET_RT_FLAGS 2 /* by flags, e.g. RESOLVING */ 2441541Srgrimes#define NET_RT_IFLIST 3 /* survey interface list */ 2451541Srgrimes#define NET_RT_MAXID 4 2461541Srgrimes 2471541Srgrimes#define CTL_NET_RT_NAMES { \ 2481541Srgrimes { 0, 0 }, \ 2491541Srgrimes { "dump", CTLTYPE_STRUCT }, \ 2501541Srgrimes { "flags", CTLTYPE_STRUCT }, \ 2511541Srgrimes { "iflist", CTLTYPE_STRUCT }, \ 2521541Srgrimes} 2531541Srgrimes 2541541Srgrimes/* 2551541Srgrimes * Maximum queue length specifiable by listen. 2561541Srgrimes */ 25713258Sdg#define SOMAXCONN 128 2581541Srgrimes 2591541Srgrimes/* 2601541Srgrimes * Message header for recvmsg and sendmsg calls. 2611541Srgrimes * Used value-result for recvmsg, value only for sendmsg. 2621541Srgrimes */ 2631541Srgrimesstruct msghdr { 2641541Srgrimes caddr_t msg_name; /* optional address */ 2651541Srgrimes u_int msg_namelen; /* size of address */ 2661541Srgrimes struct iovec *msg_iov; /* scatter/gather array */ 2671541Srgrimes u_int msg_iovlen; /* # elements in msg_iov */ 2681541Srgrimes caddr_t msg_control; /* ancillary data, see below */ 2691541Srgrimes u_int msg_controllen; /* ancillary data buffer len */ 2701541Srgrimes int msg_flags; /* flags on received message */ 2711541Srgrimes}; 2721541Srgrimes 2731541Srgrimes#define MSG_OOB 0x1 /* process out-of-band data */ 2741541Srgrimes#define MSG_PEEK 0x2 /* peek at incoming message */ 2751541Srgrimes#define MSG_DONTROUTE 0x4 /* send without using routing tables */ 2761541Srgrimes#define MSG_EOR 0x8 /* data completes record */ 2771541Srgrimes#define MSG_TRUNC 0x10 /* data discarded before delivery */ 2781541Srgrimes#define MSG_CTRUNC 0x20 /* control data lost before delivery */ 2791541Srgrimes#define MSG_WAITALL 0x40 /* wait for full request or error */ 2801541Srgrimes#define MSG_DONTWAIT 0x80 /* this message should be nonblocking */ 2816223Swollman#define MSG_EOF 0x100 /* data completes connection */ 2823304Sphk#define MSG_COMPAT 0x8000 /* used in sendit() */ 2831541Srgrimes 2841541Srgrimes/* 2851541Srgrimes * Header for ancillary data objects in msg_control buffer. 2861541Srgrimes * Used for additional information with/about a datagram 2871541Srgrimes * not expressible by flags. The format is a sequence 2881541Srgrimes * of message elements headed by cmsghdr structures. 2891541Srgrimes */ 2901541Srgrimesstruct cmsghdr { 2911541Srgrimes u_int cmsg_len; /* data byte count, including hdr */ 2921541Srgrimes int cmsg_level; /* originating protocol */ 2931541Srgrimes int cmsg_type; /* protocol-specific type */ 2941541Srgrimes/* followed by u_char cmsg_data[]; */ 2951541Srgrimes}; 2961541Srgrimes 29724083Swpaul/* 29824083Swpaul * While we may have more groups than this, the cmsgcred struct must 29924083Swpaul * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow 30024083Swpaul * this. 30124083Swpaul*/ 30224083Swpaul#define CMGROUP_MAX 16 30324083Swpaul 30424083Swpaul/* 30524083Swpaul * Credentials structure, used to verify the identity of a peer 30624083Swpaul * process that has sent us a message. This is allocated by the 30724083Swpaul * peer process but filled in by the kernel. This prevents the 30824083Swpaul * peer from lying about its identity. (Note that cmcred_groups[0] 30924083Swpaul * is the effective GID.) 31024083Swpaul */ 31124083Swpaulstruct cmsgcred { 31224083Swpaul pid_t cmcred_pid; /* PID of sending process */ 31324083Swpaul uid_t cmcred_uid; /* real UID of sending process */ 31424083Swpaul uid_t cmcred_euid; /* effective UID of sending process */ 31524083Swpaul gid_t cmcred_gid; /* real GID of sending process */ 31624083Swpaul short cmcred_ngroups; /* number or groups */ 31724083Swpaul gid_t cmcred_groups[CMGROUP_MAX]; /* groups */ 31824083Swpaul}; 31924083Swpaul 3201541Srgrimes/* given pointer to struct cmsghdr, return pointer to data */ 3211541Srgrimes#define CMSG_DATA(cmsg) ((u_char *)((cmsg) + 1)) 3221541Srgrimes 3231541Srgrimes/* given pointer to struct cmsghdr, return pointer to next cmsghdr */ 3241541Srgrimes#define CMSG_NXTHDR(mhdr, cmsg) \ 3251541Srgrimes (((caddr_t)(cmsg) + (cmsg)->cmsg_len + sizeof(struct cmsghdr) > \ 3261541Srgrimes (mhdr)->msg_control + (mhdr)->msg_controllen) ? \ 3271541Srgrimes (struct cmsghdr *)NULL : \ 3281541Srgrimes (struct cmsghdr *)((caddr_t)(cmsg) + ALIGN((cmsg)->cmsg_len))) 3291541Srgrimes 3301541Srgrimes#define CMSG_FIRSTHDR(mhdr) ((struct cmsghdr *)(mhdr)->msg_control) 3311541Srgrimes 3321541Srgrimes/* "Socket"-level control message types: */ 3331541Srgrimes#define SCM_RIGHTS 0x01 /* access rights (array of int) */ 33415701Swollman#define SCM_TIMESTAMP 0x02 /* timestamp (struct timeval) */ 33524083Swpaul#define SCM_CREDS 0x03 /* process creds (struct cmsgcred) */ 3361541Srgrimes 3371541Srgrimes/* 3381541Srgrimes * 4.3 compat sockaddr, move to compat file later 3391541Srgrimes */ 3401541Srgrimesstruct osockaddr { 3411541Srgrimes u_short sa_family; /* address family */ 3421541Srgrimes char sa_data[14]; /* up to 14 bytes of direct address */ 3431541Srgrimes}; 3441541Srgrimes 3451541Srgrimes/* 3461541Srgrimes * 4.3-compat message header (move to compat file later). 3471541Srgrimes */ 3481541Srgrimesstruct omsghdr { 3491541Srgrimes caddr_t msg_name; /* optional address */ 3501541Srgrimes int msg_namelen; /* size of address */ 3511541Srgrimes struct iovec *msg_iov; /* scatter/gather array */ 3521541Srgrimes int msg_iovlen; /* # elements in msg_iov */ 3531541Srgrimes caddr_t msg_accrights; /* access rights sent/received */ 3541541Srgrimes int msg_accrightslen; 3551541Srgrimes}; 3561541Srgrimes 3571541Srgrimes#ifndef KERNEL 3581541Srgrimes 3591541Srgrimes#include <sys/cdefs.h> 3601541Srgrimes 3611541Srgrimes__BEGIN_DECLS 3621541Srgrimesint accept __P((int, struct sockaddr *, int *)); 3631541Srgrimesint bind __P((int, const struct sockaddr *, int)); 3641541Srgrimesint connect __P((int, const struct sockaddr *, int)); 3651541Srgrimesint getpeername __P((int, struct sockaddr *, int *)); 3661541Srgrimesint getsockname __P((int, struct sockaddr *, int *)); 3671541Srgrimesint getsockopt __P((int, int, int, void *, int *)); 3681541Srgrimesint listen __P((int, int)); 3691541Srgrimesssize_t recv __P((int, void *, size_t, int)); 3701541Srgrimesssize_t recvfrom __P((int, void *, size_t, int, struct sockaddr *, int *)); 3711541Srgrimesssize_t recvmsg __P((int, struct msghdr *, int)); 3721541Srgrimesssize_t send __P((int, const void *, size_t, int)); 3731541Srgrimesssize_t sendto __P((int, const void *, 3741541Srgrimes size_t, int, const struct sockaddr *, int)); 3751541Srgrimesssize_t sendmsg __P((int, const struct msghdr *, int)); 3761541Srgrimesint setsockopt __P((int, int, int, const void *, int)); 3771541Srgrimesint shutdown __P((int, int)); 3781541Srgrimesint socket __P((int, int, int)); 3791541Srgrimesint socketpair __P((int, int, int, int *)); 3801541Srgrimes__END_DECLS 3811541Srgrimes 3821541Srgrimes#endif /* !KERNEL */ 38331927Sbde 3841541Srgrimes#endif /* !_SYS_SOCKET_H_ */ 385