1 /* $OpenBSD: socket.h,v 1.39 2001/09/07 16:45:25 itojun Exp $ */ 2 /* $NetBSD: socket.h,v 1.14 1996/02/09 18:25:36 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)socket.h 8.4 (Berkeley) 2/21/94 37 */ 38 39 #ifndef _SYS_SOCKET_H_ 40 #define _SYS_SOCKET_H_ 41 42 /* 43 * needed for ALIGNBYTES 44 */ 45 #include <machine/param.h> 46 47 /* 48 * Definitions related to sockets: types, address families, options. 49 */ 50 51 /* 52 * Types 53 */ 54 #define SOCK_STREAM 1 /* stream socket */ 55 #define SOCK_DGRAM 2 /* datagram socket */ 56 #define SOCK_RAW 3 /* raw-protocol interface */ 57 #define SOCK_RDM 4 /* reliably-delivered message */ 58 #define SOCK_SEQPACKET 5 /* sequenced packet stream */ 59 60 /* 61 * Option flags per-socket. 62 */ 63 #define SO_DEBUG 0x0001 /* turn on debugging info recording */ 64 #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */ 65 #define SO_REUSEADDR 0x0004 /* allow local address reuse */ 66 #define SO_KEEPALIVE 0x0008 /* keep connections alive */ 67 #define SO_DONTROUTE 0x0010 /* just use interface addresses */ 68 #define SO_BROADCAST 0x0020 /* permit sending of broadcast msgs */ 69 #define SO_USELOOPBACK 0x0040 /* bypass hardware when possible */ 70 #define SO_LINGER 0x0080 /* linger on close if data present */ 71 #define SO_OOBINLINE 0x0100 /* leave received OOB data in line */ 72 #define SO_REUSEPORT 0x0200 /* allow local address & port reuse */ 73 74 /* 75 * Additional options, not kept in so_options. 76 */ 77 #define SO_SNDBUF 0x1001 /* send buffer size */ 78 #define SO_RCVBUF 0x1002 /* receive buffer size */ 79 #define SO_SNDLOWAT 0x1003 /* send low-water mark */ 80 #define SO_RCVLOWAT 0x1004 /* receive low-water mark */ 81 #define SO_SNDTIMEO 0x1005 /* send timeout */ 82 #define SO_RCVTIMEO 0x1006 /* receive timeout */ 83 #define SO_ERROR 0x1007 /* get error status and clear */ 84 #define SO_TYPE 0x1008 /* get socket type */ 85 #define SO_NETPROC 0x1020 /* multiplex; network processing */ 86 87 /* 88 * Structure used for manipulating linger option. 89 */ 90 struct linger { 91 int l_onoff; /* option on/off */ 92 int l_linger; /* linger time */ 93 }; 94 95 /* 96 * Level number for (get/set)sockopt() to apply to socket itself. 97 */ 98 #define SOL_SOCKET 0xffff /* options for socket level */ 99 100 /* 101 * Address families. 102 */ 103 #define AF_UNSPEC 0 /* unspecified */ 104 #define AF_LOCAL 1 /* local to host (pipes, portals) */ 105 #define AF_UNIX AF_LOCAL /* backward compatibility */ 106 #define AF_INET 2 /* internetwork: UDP, TCP, etc. */ 107 #define AF_IMPLINK 3 /* arpanet imp addresses */ 108 #define AF_PUP 4 /* pup protocols: e.g. BSP */ 109 #define AF_CHAOS 5 /* mit CHAOS protocols */ 110 #define AF_NS 6 /* XEROX NS protocols */ 111 #define AF_ISO 7 /* ISO protocols */ 112 #define AF_OSI AF_ISO 113 #define AF_ECMA 8 /* european computer manufacturers */ 114 #define AF_DATAKIT 9 /* datakit protocols */ 115 #define AF_CCITT 10 /* CCITT protocols, X.25 etc */ 116 #define AF_SNA 11 /* IBM SNA */ 117 #define AF_DECnet 12 /* DECnet */ 118 #define AF_DLI 13 /* DEC Direct data link interface */ 119 #define AF_LAT 14 /* LAT */ 120 #define AF_HYLINK 15 /* NSC Hyperchannel */ 121 #define AF_APPLETALK 16 /* Apple Talk */ 122 #define AF_ROUTE 17 /* Internal Routing Protocol */ 123 #define AF_LINK 18 /* Link layer interface */ 124 #define pseudo_AF_XTP 19 /* eXpress Transfer Protocol (no AF) */ 125 #define AF_COIP 20 /* connection-oriented IP, aka ST II */ 126 #define AF_CNT 21 /* Computer Network Technology */ 127 #define pseudo_AF_RTIP 22 /* Help Identify RTIP packets */ 128 #define AF_IPX 23 /* Novell Internet Protocol */ 129 #define AF_INET6 24 /* IPv6 */ 130 #define pseudo_AF_PIP 25 /* Help Identify PIP packets */ 131 #define AF_ISDN 26 /* Integrated Services Digital Network*/ 132 #define AF_E164 AF_ISDN /* CCITT E.164 recommendation */ 133 #define AF_NATM 27 /* native ATM access */ 134 #define AF_ENCAP 28 135 #define AF_SIP 29 /* Simple Internet Protocol */ 136 #define AF_KEY 30 137 #define pseudo_AF_HDRCMPLT 31 /* Used by BPF to not rewrite headers 138 in interface output routine */ 139 #define AF_MAX 32 140 141 /* 142 * Structure used by kernel to store most 143 * addresses. 144 */ 145 struct sockaddr { 146 u_int8_t sa_len; /* total length */ 147 sa_family_t sa_family; /* address family */ 148 char sa_data[14]; /* actually longer; address value */ 149 }; 150 151 /* 152 * Sockaddr type which can hold any sockaddr type available 153 * in the system. 154 * 155 * Note: __ss_{len,family} is defined in RFC2553. During RFC2553 discussion 156 * the field name went back and forth between ss_len and __ss_len, 157 * and RFC2553 specifies it to be __ss_len. openbsd picked ss_len. 158 * For maximum portability, userland programmer would need to 159 * (1) make the code never touch ss_len portion (cast it into sockaddr and 160 * touch sa_len), or (2) add "-Dss_len=__ss_len" into CFLAGS to unify all 161 * occurences (including header file) to __ss_len. 162 */ 163 struct sockaddr_storage { 164 u_int8_t ss_len; /* total length */ 165 sa_family_t ss_family; /* address family */ 166 u_char __ss_pad1[6]; /* align to quad */ 167 u_int64_t __ss_pad2; /* force alignment for stupid compilers */ 168 u_char __ss_pad3[240]; /* pad to a total of 256 bytes */ 169 }; 170 171 /* 172 * Structure used by kernel to pass protocol 173 * information in raw sockets. 174 */ 175 struct sockproto { 176 u_short sp_family; /* address family */ 177 u_short sp_protocol; /* protocol */ 178 }; 179 180 /* 181 * Protocol families, same as address families for now. 182 */ 183 #define PF_UNSPEC AF_UNSPEC 184 #define PF_LOCAL AF_LOCAL 185 #define PF_UNIX PF_LOCAL /* backward compatibility */ 186 #define PF_INET AF_INET 187 #define PF_INET6 AF_INET6 188 #define PF_IMPLINK AF_IMPLINK 189 #define PF_PUP AF_PUP 190 #define PF_CHAOS AF_CHAOS 191 #define PF_NS AF_NS 192 #define PF_ISO AF_ISO 193 #define PF_OSI AF_ISO 194 #define PF_ECMA AF_ECMA 195 #define PF_DATAKIT AF_DATAKIT 196 #define PF_CCITT AF_CCITT 197 #define PF_SNA AF_SNA 198 #define PF_DECnet AF_DECnet 199 #define PF_DLI AF_DLI 200 #define PF_LAT AF_LAT 201 #define PF_HYLINK AF_HYLINK 202 #define PF_APPLETALK AF_APPLETALK 203 #define PF_ROUTE AF_ROUTE 204 #define PF_LINK AF_LINK 205 #define PF_XTP pseudo_AF_XTP /* really just proto family, no AF */ 206 #define PF_COIP AF_COIP 207 #define PF_CNT AF_CNT 208 #define PF_IPX AF_IPX /* same format as AF_NS */ 209 #define PF_INET6 AF_INET6 210 #define PF_RTIP pseudo_AF_FTIP /* same format as AF_INET */ 211 #define PF_PIP pseudo_AF_PIP 212 #define PF_ISDN AF_ISDN 213 #define PF_NATM AF_NATM 214 #define PF_ENCAP AF_ENCAP 215 #define PF_SIP AF_SIP 216 #define PF_KEY AF_KEY 217 #define PF_MAX AF_MAX 218 219 /* 220 * These are the valid values for the "how" field used by shutdown(2). 221 */ 222 #define SHUT_RD 0 223 #define SHUT_WR 1 224 #define SHUT_RDWR 2 225 226 /* 227 * Socket credentials. 228 */ 229 struct sockcred { 230 uid_t sc_uid; /* real user id */ 231 uid_t sc_euid; /* effective user id */ 232 gid_t sc_gid; /* real group id */ 233 gid_t sc_egid; /* effective group id */ 234 int sc_ngroups; /* number of supplemental groups */ 235 gid_t sc_groups[1]; /* variable length */ 236 }; 237 238 /* 239 * Compute size of a sockcred structure with groups. 240 */ 241 #define SOCKCREDSIZE(ngrps) \ 242 (sizeof(struct sockcred) + (sizeof(gid_t) * ((ngrps) - 1))) 243 244 /* 245 * Definitions for network related sysctl, CTL_NET. 246 * 247 * Second level is protocol family. 248 * Third level is protocol number. 249 * 250 * Further levels are defined by the individual families below. 251 */ 252 #define NET_MAXID AF_MAX 253 254 #define CTL_NET_NAMES { \ 255 { 0, 0 }, \ 256 { "unix", CTLTYPE_NODE }, \ 257 { "inet", CTLTYPE_NODE }, \ 258 { "implink", CTLTYPE_NODE }, \ 259 { "pup", CTLTYPE_NODE }, \ 260 { "chaos", CTLTYPE_NODE }, \ 261 { "xerox_ns", CTLTYPE_NODE }, \ 262 { "iso", CTLTYPE_NODE }, \ 263 { "emca", CTLTYPE_NODE }, \ 264 { "datakit", CTLTYPE_NODE }, \ 265 { "ccitt", CTLTYPE_NODE }, \ 266 { "ibm_sna", CTLTYPE_NODE }, \ 267 { "decnet", CTLTYPE_NODE }, \ 268 { "dec_dli", CTLTYPE_NODE }, \ 269 { "lat", CTLTYPE_NODE }, \ 270 { "hylink", CTLTYPE_NODE }, \ 271 { "appletalk", CTLTYPE_NODE }, \ 272 { "route", CTLTYPE_NODE }, \ 273 { "link_layer", CTLTYPE_NODE }, \ 274 { "xtp", CTLTYPE_NODE }, \ 275 { "coip", CTLTYPE_NODE }, \ 276 { "cnt", CTLTYPE_NODE }, \ 277 { "rtip", CTLTYPE_NODE }, \ 278 { "ipx", CTLTYPE_NODE }, \ 279 { "inet6", CTLTYPE_NODE }, \ 280 { "pip", CTLTYPE_NODE }, \ 281 { "isdn", CTLTYPE_NODE }, \ 282 { "natm", CTLTYPE_NODE }, \ 283 { "encap", CTLTYPE_NODE }, \ 284 { "sip", CTLTYPE_NODE }, \ 285 { "key", CTLTYPE_NODE }, \ 286 } 287 288 /* 289 * PF_ROUTE - Routing table 290 * 291 * Three additional levels are defined: 292 * Fourth: address family, 0 is wildcard 293 * Fifth: type of info, defined below 294 * Sixth: flag(s) to mask with for NET_RT_FLAGS 295 */ 296 #define NET_RT_DUMP 1 /* dump; may limit to a.f. */ 297 #define NET_RT_FLAGS 2 /* by flags, e.g. RESOLVING */ 298 #define NET_RT_IFLIST 3 /* survey interface list */ 299 #define NET_RT_MAXID 4 300 301 #define CTL_NET_RT_NAMES { \ 302 { 0, 0 }, \ 303 { "dump", CTLTYPE_STRUCT }, \ 304 { "flags", CTLTYPE_STRUCT }, \ 305 { "iflist", CTLTYPE_STRUCT }, \ 306 } 307 308 /* 309 * Maximum queue length specifiable by listen(2). 310 */ 311 #define SOMAXCONN 128 312 313 /* 314 * Message header for recvmsg and sendmsg calls. 315 * Used value-result for recvmsg, value only for sendmsg. 316 */ 317 struct msghdr { 318 caddr_t msg_name; /* optional address */ 319 socklen_t msg_namelen; /* size of address */ 320 struct iovec *msg_iov; /* scatter/gather array */ 321 u_int msg_iovlen; /* # elements in msg_iov */ 322 caddr_t msg_control; /* ancillary data, see below */ 323 socklen_t msg_controllen; /* ancillary data buffer len */ 324 int msg_flags; /* flags on received message */ 325 }; 326 327 #define MSG_OOB 0x1 /* process out-of-band data */ 328 #define MSG_PEEK 0x2 /* peek at incoming message */ 329 #define MSG_DONTROUTE 0x4 /* send without using routing tables */ 330 #define MSG_EOR 0x8 /* data completes record */ 331 #define MSG_TRUNC 0x10 /* data discarded before delivery */ 332 #define MSG_CTRUNC 0x20 /* control data lost before delivery */ 333 #define MSG_WAITALL 0x40 /* wait for full request or error */ 334 #define MSG_DONTWAIT 0x80 /* this message should be nonblocking */ 335 #define MSG_BCAST 0x100 /* this message rec'd as broadcast */ 336 #define MSG_MCAST 0x200 /* this message rec'd as multicast */ 337 338 /* 339 * Header for ancillary data objects in msg_control buffer. 340 * Used for additional information with/about a datagram 341 * not expressible by flags. The format is a sequence 342 * of message elements headed by cmsghdr structures. 343 */ 344 struct cmsghdr { 345 socklen_t cmsg_len; /* data byte count, including hdr */ 346 int cmsg_level; /* originating protocol */ 347 int cmsg_type; /* protocol-specific type */ 348 /* followed by u_char cmsg_data[]; */ 349 }; 350 351 /* given pointer to struct cmsghdr, return pointer to data */ 352 #define CMSG_DATA(cmsg) \ 353 ((u_char *)(cmsg) + __CMSG_ALIGN(sizeof(struct cmsghdr))) 354 355 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */ 356 #define CMSG_NXTHDR(mhdr, cmsg) \ 357 (((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len) + \ 358 __CMSG_ALIGN(sizeof(struct cmsghdr)) > \ 359 (mhdr)->msg_control + (mhdr)->msg_controllen) ? \ 360 (struct cmsghdr *)NULL : \ 361 (struct cmsghdr *)((caddr_t)(cmsg) + __CMSG_ALIGN((cmsg)->cmsg_len))) 362 363 /* 364 * RFC 2292 requires to check msg_controllen, in case that the kernel returns 365 * an empty list for some reasons. 366 */ 367 #define CMSG_FIRSTHDR(mhdr) \ 368 ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \ 369 (struct cmsghdr *)(mhdr)->msg_control : \ 370 (struct cmsghdr *)NULL) 371 372 /* Round len up to next alignment boundary */ 373 #define __CMSG_ALIGN(len) ALIGN(len) 374 #ifdef _KERNEL 375 #define CMSG_ALIGN(n) __CMSG_ALIGN(n) 376 #endif 377 378 /* Length of the contents of a control message of length len */ 379 #define CMSG_LEN(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) 380 381 /* Length of the space taken up by a padded control message of length len */ 382 #define CMSG_SPACE(len) (__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len)) 383 384 /* "Socket"-level control message types: */ 385 #define SCM_RIGHTS 0x01 /* access rights (array of int) */ 386 #define SCM_CREDS 0x02 /* credientials (struct sockcred) */ 387 388 /* 389 * 4.3 compat sockaddr, move to compat file later 390 */ 391 struct osockaddr { 392 u_short sa_family; /* address family */ 393 char sa_data[14]; /* up to 14 bytes of direct address */ 394 }; 395 396 /* 397 * 4.3-compat message header (move to compat file later). 398 */ 399 struct omsghdr { 400 caddr_t msg_name; /* optional address */ 401 int msg_namelen; /* size of address */ 402 struct iovec *msg_iov; /* scatter/gather array */ 403 int msg_iovlen; /* # elements in msg_iov */ 404 caddr_t msg_accrights; /* access rights sent/received */ 405 int msg_accrightslen; 406 }; 407 408 #define SA_LEN(x) ((x)->sa_len) 409 410 #ifndef _KERNEL 411 412 #include <sys/cdefs.h> 413 414 __BEGIN_DECLS 415 int accept __P((int, struct sockaddr *, socklen_t *)); 416 int bind __P((int, const struct sockaddr *, socklen_t)); 417 int connect __P((int, const struct sockaddr *, socklen_t)); 418 int getpeereid __P((int, uid_t *, gid_t *)); 419 int getpeername __P((int, struct sockaddr *, socklen_t *)); 420 int getsockname __P((int, struct sockaddr *, socklen_t *)); 421 int getsockopt __P((int, int, int, void *, socklen_t *)); 422 int listen __P((int, int)); 423 ssize_t recv __P((int, void *, size_t, int)); 424 ssize_t recvfrom __P((int, void *, size_t, int, struct sockaddr *, socklen_t *)); 425 ssize_t recvmsg __P((int, struct msghdr *, int)); 426 ssize_t send __P((int, const void *, size_t, int)); 427 ssize_t sendto __P((int, const void *, 428 size_t, int, const struct sockaddr *, socklen_t)); 429 ssize_t sendmsg __P((int, const struct msghdr *, int)); 430 int setsockopt __P((int, int, int, const void *, socklen_t)); 431 int shutdown __P((int, int)); 432 int socket __P((int, int, int)); 433 int socketpair __P((int, int, int, int *)); 434 __END_DECLS 435 #else 436 # if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_LINUX) || \ 437 defined(COMPAT_HPUX) || defined(COMPAT_FREEBSD) || defined(COMPAT_BSDOS) \ 438 || defined(COMPAT_OSF1) 439 # define COMPAT_OLDSOCK 440 # define MSG_COMPAT 0x8000 441 # endif 442 443 void pfctlinput __P((int, struct sockaddr *)); 444 #endif /* !_KERNEL */ 445 446 #endif /* !_SYS_SOCKET_H_ */ 447