socket.h revision 39271
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)socket.h 8.4 (Berkeley) 2/21/94 34 * $Id: socket.h,v 1.25 1998/09/12 21:14:25 wollman Exp $ 35 */ 36 37#ifndef _SYS_SOCKET_H_ 38#define _SYS_SOCKET_H_ 39 40/* 41 * Definitions related to sockets: types, address families, options. 42 */ 43 44/* 45 * Types 46 */ 47#define SOCK_STREAM 1 /* stream socket */ 48#define SOCK_DGRAM 2 /* datagram socket */ 49#define SOCK_RAW 3 /* raw-protocol interface */ 50#define SOCK_RDM 4 /* reliably-delivered message */ 51#define SOCK_SEQPACKET 5 /* sequenced packet stream */ 52 53/* 54 * Option flags per-socket. 55 */ 56#define SO_DEBUG 0x0001 /* turn on debugging info recording */ 57#define SO_ACCEPTCONN 0x0002 /* socket has had listen() */ 58#define SO_REUSEADDR 0x0004 /* allow local address reuse */ 59#define SO_KEEPALIVE 0x0008 /* keep connections alive */ 60#define SO_DONTROUTE 0x0010 /* just use interface addresses */ 61#define SO_BROADCAST 0x0020 /* permit sending of broadcast msgs */ 62#define SO_USELOOPBACK 0x0040 /* bypass hardware when possible */ 63#define SO_LINGER 0x0080 /* linger on close if data present */ 64#define SO_OOBINLINE 0x0100 /* leave received OOB data in line */ 65#define SO_REUSEPORT 0x0200 /* allow local address & port reuse */ 66#define SO_TIMESTAMP 0x0400 /* timestamp received dgram traffic */ 67 68/* 69 * Additional options, not kept in so_options. 70 */ 71#define SO_SNDBUF 0x1001 /* send buffer size */ 72#define SO_RCVBUF 0x1002 /* receive buffer size */ 73#define SO_SNDLOWAT 0x1003 /* send low-water mark */ 74#define SO_RCVLOWAT 0x1004 /* receive low-water mark */ 75#define SO_SNDTIMEO 0x1005 /* send timeout */ 76#define SO_RCVTIMEO 0x1006 /* receive timeout */ 77#define SO_ERROR 0x1007 /* get error status and clear */ 78#define SO_TYPE 0x1008 /* get socket type */ 79/*efine SO_PRIVSTATE 0x1009 get/deny privileged state */ 80 81/* 82 * Structure used for manipulating linger option. 83 */ 84struct linger { 85 int l_onoff; /* option on/off */ 86 int l_linger; /* linger time */ 87}; 88 89/* 90 * Level number for (get/set)sockopt() to apply to socket itself. 91 */ 92#define SOL_SOCKET 0xffff /* options for socket level */ 93 94/* 95 * Address families. 96 */ 97#define AF_UNSPEC 0 /* unspecified */ 98#define AF_LOCAL 1 /* local to host (pipes, portals) */ 99#define AF_UNIX AF_LOCAL /* backward compatibility */ 100#define AF_INET 2 /* internetwork: UDP, TCP, etc. */ 101#define AF_IMPLINK 3 /* arpanet imp addresses */ 102#define AF_PUP 4 /* pup protocols: e.g. BSP */ 103#define AF_CHAOS 5 /* mit CHAOS protocols */ 104#define AF_NS 6 /* XEROX NS protocols */ 105#define AF_ISO 7 /* ISO protocols */ 106#define AF_OSI AF_ISO 107#define AF_ECMA 8 /* European computer manufacturers */ 108#define AF_DATAKIT 9 /* datakit protocols */ 109#define AF_CCITT 10 /* CCITT protocols, X.25 etc */ 110#define AF_SNA 11 /* IBM SNA */ 111#define AF_DECnet 12 /* DECnet */ 112#define AF_DLI 13 /* DEC Direct data link interface */ 113#define AF_LAT 14 /* LAT */ 114#define AF_HYLINK 15 /* NSC Hyperchannel */ 115#define AF_APPLETALK 16 /* Apple Talk */ 116#define AF_ROUTE 17 /* Internal Routing Protocol */ 117#define AF_LINK 18 /* Link layer interface */ 118#define pseudo_AF_XTP 19 /* eXpress Transfer Protocol (no AF) */ 119#define AF_COIP 20 /* connection-oriented IP, aka ST II */ 120#define AF_CNT 21 /* Computer Network Technology */ 121#define pseudo_AF_RTIP 22 /* Help Identify RTIP packets */ 122#define AF_IPX 23 /* Novell Internet Protocol */ 123#define AF_SIP 24 /* Simple Internet Protocol */ 124#define pseudo_AF_PIP 25 /* Help Identify PIP packets */ 125#define AF_ISDN 26 /* Integrated Services Digital Network*/ 126#define AF_E164 AF_ISDN /* CCITT E.164 recommendation */ 127#define pseudo_AF_KEY 27 /* Internal key-management function */ 128#define AF_INET6 28 /* IPv6 */ 129#define AF_NATM 29 /* native ATM access */ 130#define AF_ATM 30 /* ATM */ 131 132#define AF_MAX 31 133 134/* 135 * Structure used by kernel to store most 136 * addresses. 137 */ 138struct sockaddr { 139 u_char sa_len; /* total length */ 140 u_char sa_family; /* address family */ 141 char sa_data[14]; /* actually longer; address value */ 142}; 143#define SOCK_MAXADDRLEN 255 /* longest possible addresses */ 144 145/* 146 * Structure used by kernel to pass protocol 147 * information in raw sockets. 148 */ 149struct sockproto { 150 u_short sp_family; /* address family */ 151 u_short sp_protocol; /* protocol */ 152}; 153 154/* 155 * Protocol families, same as address families for now. 156 */ 157#define PF_UNSPEC AF_UNSPEC 158#define PF_LOCAL AF_LOCAL 159#define PF_UNIX PF_LOCAL /* backward compatibility */ 160#define PF_INET AF_INET 161#define PF_IMPLINK AF_IMPLINK 162#define PF_PUP AF_PUP 163#define PF_CHAOS AF_CHAOS 164#define PF_NS AF_NS 165#define PF_ISO AF_ISO 166#define PF_OSI AF_ISO 167#define PF_ECMA AF_ECMA 168#define PF_DATAKIT AF_DATAKIT 169#define PF_CCITT AF_CCITT 170#define PF_SNA AF_SNA 171#define PF_DECnet AF_DECnet 172#define PF_DLI AF_DLI 173#define PF_LAT AF_LAT 174#define PF_HYLINK AF_HYLINK 175#define PF_APPLETALK AF_APPLETALK 176#define PF_ROUTE AF_ROUTE 177#define PF_LINK AF_LINK 178#define PF_XTP pseudo_AF_XTP /* really just proto family, no AF */ 179#define PF_COIP AF_COIP 180#define PF_CNT AF_CNT 181#define PF_SIP AF_SIP 182#define PF_IPX AF_IPX /* same format as AF_NS */ 183#define PF_RTIP pseudo_AF_RTIP /* same format as AF_INET */ 184#define PF_PIP pseudo_AF_PIP 185#define PF_ISDN AF_ISDN 186#define PF_KEY pseudo_AF_KEY 187#define PF_INET6 AF_INET6 188#define PF_NATM AF_NATM 189#define PF_ATM AF_ATM 190 191#define PF_MAX AF_MAX 192 193/* 194 * Definitions for network related sysctl, CTL_NET. 195 * 196 * Second level is protocol family. 197 * Third level is protocol number. 198 * 199 * Further levels are defined by the individual families below. 200 */ 201#define NET_MAXID AF_MAX 202 203#define CTL_NET_NAMES { \ 204 { 0, 0 }, \ 205 { "unix", CTLTYPE_NODE }, \ 206 { "inet", CTLTYPE_NODE }, \ 207 { "implink", CTLTYPE_NODE }, \ 208 { "pup", CTLTYPE_NODE }, \ 209 { "chaos", CTLTYPE_NODE }, \ 210 { "xerox_ns", CTLTYPE_NODE }, \ 211 { "iso", CTLTYPE_NODE }, \ 212 { "emca", CTLTYPE_NODE }, \ 213 { "datakit", CTLTYPE_NODE }, \ 214 { "ccitt", CTLTYPE_NODE }, \ 215 { "ibm_sna", CTLTYPE_NODE }, \ 216 { "decnet", CTLTYPE_NODE }, \ 217 { "dec_dli", CTLTYPE_NODE }, \ 218 { "lat", CTLTYPE_NODE }, \ 219 { "hylink", CTLTYPE_NODE }, \ 220 { "appletalk", CTLTYPE_NODE }, \ 221 { "route", CTLTYPE_NODE }, \ 222 { "link_layer", CTLTYPE_NODE }, \ 223 { "xtp", CTLTYPE_NODE }, \ 224 { "coip", CTLTYPE_NODE }, \ 225 { "cnt", CTLTYPE_NODE }, \ 226 { "rtip", CTLTYPE_NODE }, \ 227 { "ipx", CTLTYPE_NODE }, \ 228 { "sip", CTLTYPE_NODE }, \ 229 { "pip", CTLTYPE_NODE }, \ 230 { "isdn", CTLTYPE_NODE }, \ 231 { "key", CTLTYPE_NODE }, \ 232 { "inet6", CTLTYPE_NODE }, \ 233 { "natm", CTLTYPE_NODE }, \ 234} 235 236/* 237 * PF_ROUTE - Routing table 238 * 239 * Three additional levels are defined: 240 * Fourth: address family, 0 is wildcard 241 * Fifth: type of info, defined below 242 * Sixth: flag(s) to mask with for NET_RT_FLAGS 243 */ 244#define NET_RT_DUMP 1 /* dump; may limit to a.f. */ 245#define NET_RT_FLAGS 2 /* by flags, e.g. RESOLVING */ 246#define NET_RT_IFLIST 3 /* survey interface list */ 247#define NET_RT_MAXID 4 248 249#define CTL_NET_RT_NAMES { \ 250 { 0, 0 }, \ 251 { "dump", CTLTYPE_STRUCT }, \ 252 { "flags", CTLTYPE_STRUCT }, \ 253 { "iflist", CTLTYPE_STRUCT }, \ 254} 255 256/* 257 * Maximum queue length specifiable by listen. 258 */ 259#define SOMAXCONN 128 260 261/* 262 * Message header for recvmsg and sendmsg calls. 263 * Used value-result for recvmsg, value only for sendmsg. 264 */ 265struct msghdr { 266 caddr_t msg_name; /* optional address */ 267 u_int msg_namelen; /* size of address */ 268 struct iovec *msg_iov; /* scatter/gather array */ 269 u_int msg_iovlen; /* # elements in msg_iov */ 270 caddr_t msg_control; /* ancillary data, see below */ 271 u_int msg_controllen; /* ancillary data buffer len */ 272 int msg_flags; /* flags on received message */ 273}; 274 275#define MSG_OOB 0x1 /* process out-of-band data */ 276#define MSG_PEEK 0x2 /* peek at incoming message */ 277#define MSG_DONTROUTE 0x4 /* send without using routing tables */ 278#define MSG_EOR 0x8 /* data completes record */ 279#define MSG_TRUNC 0x10 /* data discarded before delivery */ 280#define MSG_CTRUNC 0x20 /* control data lost before delivery */ 281#define MSG_WAITALL 0x40 /* wait for full request or error */ 282#define MSG_DONTWAIT 0x80 /* this message should be nonblocking */ 283#define MSG_EOF 0x100 /* data completes connection */ 284#define MSG_COMPAT 0x8000 /* used in sendit() */ 285 286/* 287 * Header for ancillary data objects in msg_control buffer. 288 * Used for additional information with/about a datagram 289 * not expressible by flags. The format is a sequence 290 * of message elements headed by cmsghdr structures. 291 */ 292struct cmsghdr { 293 u_int cmsg_len; /* data byte count, including hdr */ 294 int cmsg_level; /* originating protocol */ 295 int cmsg_type; /* protocol-specific type */ 296/* followed by u_char cmsg_data[]; */ 297}; 298 299/* 300 * While we may have more groups than this, the cmsgcred struct must 301 * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow 302 * this. 303*/ 304#define CMGROUP_MAX 16 305 306/* 307 * Credentials structure, used to verify the identity of a peer 308 * process that has sent us a message. This is allocated by the 309 * peer process but filled in by the kernel. This prevents the 310 * peer from lying about its identity. (Note that cmcred_groups[0] 311 * is the effective GID.) 312 */ 313struct cmsgcred { 314 pid_t cmcred_pid; /* PID of sending process */ 315 uid_t cmcred_uid; /* real UID of sending process */ 316 uid_t cmcred_euid; /* effective UID of sending process */ 317 gid_t cmcred_gid; /* real GID of sending process */ 318 short cmcred_ngroups; /* number or groups */ 319 gid_t cmcred_groups[CMGROUP_MAX]; /* groups */ 320}; 321 322/* given pointer to struct cmsghdr, return pointer to data */ 323#define CMSG_DATA(cmsg) ((u_char *)((cmsg) + 1)) 324 325/* given pointer to struct cmsghdr, return pointer to next cmsghdr */ 326#define CMSG_NXTHDR(mhdr, cmsg) \ 327 (((caddr_t)(cmsg) + (cmsg)->cmsg_len + sizeof(struct cmsghdr) > \ 328 (mhdr)->msg_control + (mhdr)->msg_controllen) ? \ 329 (struct cmsghdr *)NULL : \ 330 (struct cmsghdr *)((caddr_t)(cmsg) + ALIGN((cmsg)->cmsg_len))) 331 332#define CMSG_FIRSTHDR(mhdr) ((struct cmsghdr *)(mhdr)->msg_control) 333 334/* "Socket"-level control message types: */ 335#define SCM_RIGHTS 0x01 /* access rights (array of int) */ 336#define SCM_TIMESTAMP 0x02 /* timestamp (struct timeval) */ 337#define SCM_CREDS 0x03 /* process creds (struct cmsgcred) */ 338 339/* 340 * 4.3 compat sockaddr, move to compat file later 341 */ 342struct osockaddr { 343 u_short sa_family; /* address family */ 344 char sa_data[14]; /* up to 14 bytes of direct address */ 345}; 346 347/* 348 * 4.3-compat message header (move to compat file later). 349 */ 350struct omsghdr { 351 caddr_t msg_name; /* optional address */ 352 int msg_namelen; /* size of address */ 353 struct iovec *msg_iov; /* scatter/gather array */ 354 int msg_iovlen; /* # elements in msg_iov */ 355 caddr_t msg_accrights; /* access rights sent/received */ 356 int msg_accrightslen; 357}; 358 359/* 360 * howto arguments for shutdown(2), specified by Posix.1g. 361 */ 362#define SHUT_RD 0 /* shut down the reading side */ 363#define SHUT_WR 1 /* shut down the writing side */ 364#define SHUT_RDWR 2 /* shut down both sides */ 365 366#ifndef KERNEL 367 368#include <sys/cdefs.h> 369 370__BEGIN_DECLS 371int accept __P((int, struct sockaddr *, int *)); 372int bind __P((int, const struct sockaddr *, int)); 373int connect __P((int, const struct sockaddr *, int)); 374int getpeername __P((int, struct sockaddr *, int *)); 375int getsockname __P((int, struct sockaddr *, int *)); 376int getsockopt __P((int, int, int, void *, int *)); 377int listen __P((int, int)); 378ssize_t recv __P((int, void *, size_t, int)); 379ssize_t recvfrom __P((int, void *, size_t, int, struct sockaddr *, int *)); 380ssize_t recvmsg __P((int, struct msghdr *, int)); 381ssize_t send __P((int, const void *, size_t, int)); 382ssize_t sendto __P((int, const void *, 383 size_t, int, const struct sockaddr *, int)); 384ssize_t sendmsg __P((int, const struct msghdr *, int)); 385int setsockopt __P((int, int, int, const void *, int)); 386int shutdown __P((int, int)); 387int socket __P((int, int, int)); 388int socketpair __P((int, int, int, int *)); 389__END_DECLS 390 391#endif /* !KERNEL */ 392 393#endif /* !_SYS_SOCKET_H_ */ 394