zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

socket.h (32534B) - Raw


      1 /*
      2  * Copyright (c) 2000-2022 Apple Inc. All rights reserved.
      3  *
      4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
      5  *
      6  * This file contains Original Code and/or Modifications of Original Code
      7  * as defined in and that are subject to the Apple Public Source License
      8  * Version 2.0 (the 'License'). You may not use this file except in
      9  * compliance with the License. The rights granted to you under the License
     10  * may not be used to create, or enable the creation or redistribution of,
     11  * unlawful or unlicensed copies of an Apple operating system, or to
     12  * circumvent, violate, or enable the circumvention or violation of, any
     13  * terms of an Apple operating system software license agreement.
     14  *
     15  * Please obtain a copy of the License at
     16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
     17  *
     18  * The Original Code and all software distributed under the License are
     19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
     20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
     21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
     23  * Please see the License for the specific language governing rights and
     24  * limitations under the License.
     25  *
     26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
     27  */
     28 /* Copyright (c) 1998, 1999 Apple Computer, Inc. All Rights Reserved */
     29 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
     30 /*
     31  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
     32  *	The Regents of the University of California.  All rights reserved.
     33  *
     34  * Redistribution and use in source and binary forms, with or without
     35  * modification, are permitted provided that the following conditions
     36  * are met:
     37  * 1. Redistributions of source code must retain the above copyright
     38  *    notice, this list of conditions and the following disclaimer.
     39  * 2. Redistributions in binary form must reproduce the above copyright
     40  *    notice, this list of conditions and the following disclaimer in the
     41  *    documentation and/or other materials provided with the distribution.
     42  * 3. All advertising materials mentioning features or use of this software
     43  *    must display the following acknowledgement:
     44  *	This product includes software developed by the University of
     45  *	California, Berkeley and its contributors.
     46  * 4. Neither the name of the University nor the names of its contributors
     47  *    may be used to endorse or promote products derived from this software
     48  *    without specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     60  * SUCH DAMAGE.
     61  *
     62  *	@(#)socket.h	8.4 (Berkeley) 2/21/94
     63  * $FreeBSD: src/sys/sys/socket.h,v 1.39.2.7 2001/07/03 11:02:01 ume Exp $
     64  */
     65 /*
     66  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
     67  * support for mandatory and extensible security protections.  This notice
     68  * is included in support of clause 2.2 (b) of the Apple Public License,
     69  * Version 2.0.
     70  */
     71 
     72 #ifndef _SYS_SOCKET_H_
     73 #define _SYS_SOCKET_H_
     74 
     75 #include <sys/types.h>
     76 #include <sys/cdefs.h>
     77 #include <sys/constrained_ctypes.h>
     78 #include <machine/_param.h>
     79 #include <net/net_kev.h>
     80 
     81 #include <Availability.h>
     82 
     83 /*
     84  * Definitions related to sockets: types, address families, options.
     85  */
     86 
     87 /*
     88  * Data types.
     89  */
     90 
     91 #include <sys/_types/_gid_t.h>
     92 #include <sys/_types/_off_t.h>
     93 #include <sys/_types/_pid_t.h>
     94 #include <sys/_types/_sa_family_t.h>
     95 #include <sys/_types/_socklen_t.h>
     96 
     97 /* XXX Not explicitly defined by POSIX, but function return types are */
     98 #include <sys/_types/_size_t.h>
     99 
    100 /* XXX Not explicitly defined by POSIX, but function return types are */
    101 #include <sys/_types/_ssize_t.h>
    102 
    103 /*
    104  * [XSI] The iovec structure shall be defined as described in <sys/uio.h>.
    105  */
    106 #include <sys/_types/_iovec_t.h>
    107 
    108 /*
    109  * Types
    110  */
    111 #define SOCK_STREAM     1               /* stream socket */
    112 #define SOCK_DGRAM      2               /* datagram socket */
    113 #define SOCK_RAW        3               /* raw-protocol interface */
    114 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    115 #define SOCK_RDM        4               /* reliably-delivered message */
    116 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    117 #define SOCK_SEQPACKET  5               /* sequenced packet stream */
    118 
    119 /*
    120  * Option flags per-socket.
    121  */
    122 #define SO_DEBUG        0x0001          /* turn on debugging info recording */
    123 #define SO_ACCEPTCONN   0x0002          /* socket has had listen() */
    124 #define SO_REUSEADDR    0x0004          /* allow local address reuse */
    125 #define SO_KEEPALIVE    0x0008          /* keep connections alive */
    126 #define SO_DONTROUTE    0x0010          /* just use interface addresses */
    127 #define SO_BROADCAST    0x0020          /* permit sending of broadcast msgs */
    128 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    129 #define SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
    130 #define SO_LINGER       0x0080          /* linger on close if data present (in ticks) */
    131 #define SO_LINGER_SEC   0x1080          /* linger on close if data present (in seconds) */
    132 #else
    133 #define SO_LINGER       0x1080          /* linger on close if data present (in seconds) */
    134 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    135 #define SO_OOBINLINE    0x0100          /* leave received OOB data in line */
    136 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    137 #define SO_REUSEPORT    0x0200          /* allow local address & port reuse */
    138 #define SO_TIMESTAMP    0x0400          /* timestamp received dgram traffic */
    139 #define SO_TIMESTAMP_MONOTONIC  0x0800  /* Monotonically increasing timestamp on rcvd dgram */
    140 #ifndef __APPLE__
    141 #define SO_ACCEPTFILTER 0x1000          /* there is an accept filter */
    142 #else
    143 #define SO_DONTTRUNC    0x2000          /* APPLE: Retain unread data */
    144                                         /*  (ATOMIC proto) */
    145 #define SO_WANTMORE     0x4000          /* APPLE: Give hint when more data ready */
    146 #define SO_WANTOOBFLAG  0x8000          /* APPLE: Want OOB in MSG_FLAG on receive */
    147 
    148 
    149 #endif  /* (!__APPLE__) */
    150 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    151 
    152 /*
    153  * Additional options, not kept in so_options.
    154  */
    155 #define SO_SNDBUF       0x1001          /* send buffer size */
    156 #define SO_RCVBUF       0x1002          /* receive buffer size */
    157 #define SO_SNDLOWAT     0x1003          /* send low-water mark */
    158 #define SO_RCVLOWAT     0x1004          /* receive low-water mark */
    159 #define SO_SNDTIMEO     0x1005          /* send timeout */
    160 #define SO_RCVTIMEO     0x1006          /* receive timeout */
    161 #define SO_ERROR        0x1007          /* get error status and clear */
    162 #define SO_TYPE         0x1008          /* get socket type */
    163 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    164 #define SO_LABEL        0x1010          /* deprecated */
    165 #define SO_PEERLABEL    0x1011          /* deprecated */
    166 #ifdef __APPLE__
    167 #define SO_NREAD        0x1020          /* APPLE: get 1st-packet byte count */
    168 #define SO_NKE          0x1021          /* APPLE: Install socket-level NKE */
    169 #define SO_NOSIGPIPE    0x1022          /* APPLE: No SIGPIPE on EPIPE */
    170 #define SO_NOADDRERR    0x1023          /* APPLE: Returns EADDRNOTAVAIL when src is not available anymore */
    171 #define SO_NWRITE       0x1024          /* APPLE: Get number of bytes currently in send socket buffer */
    172 #define SO_REUSESHAREUID        0x1025          /* APPLE: Allow reuse of port/socket by different userids */
    173 #ifdef __APPLE_API_PRIVATE
    174 #define SO_NOTIFYCONFLICT       0x1026  /* APPLE: send notification if there is a bind on a port which is already in use */
    175 #define SO_UPCALLCLOSEWAIT      0x1027  /* APPLE: block on close until an upcall returns */
    176 #endif
    177 #define SO_RANDOMPORT   0x1082  /* APPLE: request local port randomization */
    178 #define SO_NP_EXTENSIONS        0x1083  /* To turn off some POSIX behavior */
    179 #endif
    180 
    181 #define SO_NUMRCVPKT            0x1112  /* number of datagrams in receive socket buffer */
    182 #define SO_NET_SERVICE_TYPE     0x1116  /* Network service type */
    183 
    184 
    185 #define SO_NETSVC_MARKING_LEVEL    0x1119  /* Get QoS marking in effect for socket */
    186 
    187 
    188 #define SO_RESOLVER_SIGNATURE      0x1131  /* A signed data blob from the system resolver */
    189 
    190 #define SO_BINDTODEVICE            0x1134  /* bind socket to a network device (max valid option length IFNAMSIZ) */
    191 
    192 /* When adding new socket-options, you need to make sure MPTCP supports these as well! */
    193 
    194 /*
    195  * Network Service Type for option SO_NET_SERVICE_TYPE
    196  *
    197  * The vast majority of sockets should use Best Effort that is the default
    198  * Network Service Type. Other Network Service Types have to be used only if
    199  * the traffic actually matches the description of the Network Service Type.
    200  *
    201  * Network Service Types do not represent priorities but rather describe
    202  * different categories of delay, jitter and loss parameters.
    203  * Those parameters may influence protocols from layer 4 protocols like TCP
    204  * to layer 2 protocols like Wi-Fi. The Network Service Type can determine
    205  * how the traffic is queued and scheduled by the host networking stack and
    206  * by other entities on the network like switches and routers. For example
    207  * for Wi-Fi, the Network Service Type can select the marking of the
    208  * layer 2 packet with the appropriate WMM Access Category.
    209  *
    210  * There is no point in attempting to game the system and use
    211  * a Network Service Type that does not correspond to the actual
    212  * traffic characteristic but one that seems to have a higher precedence.
    213  * The reason is that for service classes that have lower tolerance
    214  * for delay and jitter, the queues size is lower than for service
    215  * classes that are more tolerant to delay and jitter.
    216  *
    217  * For example using a voice service type for bulk data transfer will lead
    218  * to disastrous results as soon as congestion happens because the voice
    219  * queue overflows and packets get dropped. This is not only bad for the bulk
    220  * data transfer but it is also bad for VoIP apps that legitimately are using
    221  * the voice  service type.
    222  *
    223  * The characteristics of the Network Service Types are based on the service
    224  * classes defined in RFC 4594 "Configuration Guidelines for DiffServ Service
    225  * Classes"
    226  *
    227  * When system detects the outgoing interface belongs to a DiffServ domain
    228  * that follows the recommendation of the IETF draft "Guidelines for DiffServ to
    229  * IEEE 802.11 Mapping", the packet will marked at layer 3 with a DSCP value
    230  * that corresponds to Network Service Type.
    231  *
    232  * NET_SERVICE_TYPE_BE
    233  *	"Best Effort", unclassified/standard.  This is the default service
    234  *	class and cover the majority of the traffic.
    235  *
    236  * NET_SERVICE_TYPE_BK
    237  *	"Background", high delay tolerant, loss tolerant. elastic flow,
    238  *	variable size & long-lived. E.g: non-interactive network bulk transfer
    239  *	like synching or backup.
    240  *
    241  * NET_SERVICE_TYPE_RD
    242  *	"Responsive Data", a notch higher than "Best Effort", medium delay
    243  *	tolerant, elastic & inelastic flow, bursty, long-lived. E.g. email,
    244  *	instant messaging, for which there is a sense of interactivity and
    245  *	urgency (user waiting for output).
    246  *
    247  * NET_SERVICE_TYPE_OAM
    248  *	"Operations, Administration, and Management", medium delay tolerant,
    249  *	low-medium loss tolerant, elastic & inelastic flows, variable size.
    250  *	E.g. VPN tunnels.
    251  *
    252  * NET_SERVICE_TYPE_AV
    253  *	"Multimedia Audio/Video Streaming", medium delay tolerant, low-medium
    254  *	loss tolerant, elastic flow, constant packet interval, variable rate
    255  *	and size. E.g. video and audio playback with buffering.
    256  *
    257  * NET_SERVICE_TYPE_RV
    258  *	"Responsive Multimedia Audio/Video", low delay tolerant, low-medium
    259  *	loss tolerant, elastic flow, variable packet interval, rate and size.
    260  *	E.g. screen sharing.
    261  *
    262  * NET_SERVICE_TYPE_VI
    263  *	"Interactive Video", low delay tolerant, low-medium loss tolerant,
    264  *	elastic flow, constant packet interval, variable rate & size. E.g.
    265  *	video telephony.
    266  *
    267  * NET_SERVICE_TYPE_SIG
    268  *	"Signaling", low delay tolerant, low loss tolerant, inelastic flow,
    269  *	jitter tolerant, rate is bursty but short, variable size. E.g. SIP.
    270  *
    271  * NET_SERVICE_TYPE_VO
    272  *	"Interactive Voice", very low delay tolerant, very low loss tolerant,
    273  *	inelastic flow, constant packet rate, somewhat fixed size.
    274  *	E.g. VoIP.
    275  */
    276 
    277 #define NET_SERVICE_TYPE_BE     0 /* Best effort */
    278 #define NET_SERVICE_TYPE_BK     1 /* Background system initiated */
    279 #define NET_SERVICE_TYPE_SIG    2 /* Signaling */
    280 #define NET_SERVICE_TYPE_VI     3 /* Interactive Video */
    281 #define NET_SERVICE_TYPE_VO     4 /* Interactive Voice */
    282 #define NET_SERVICE_TYPE_RV     5 /* Responsive Multimedia Audio/Video */
    283 #define NET_SERVICE_TYPE_AV     6 /* Multimedia Audio/Video Streaming */
    284 #define NET_SERVICE_TYPE_OAM    7 /* Operations, Administration, and Management */
    285 #define NET_SERVICE_TYPE_RD     8 /* Responsive Data */
    286 
    287 
    288 /* These are supported values for SO_NETSVC_MARKING_LEVEL */
    289 #define NETSVC_MRKNG_UNKNOWN            0       /* The outgoing network interface is not known */
    290 #define NETSVC_MRKNG_LVL_L2             1       /* Default marking at layer 2 (for example Wi-Fi WMM) */
    291 #define NETSVC_MRKNG_LVL_L3L2_ALL       2       /* Layer 3 DSCP marking and layer 2 marking for all Network Service Types */
    292 #define NETSVC_MRKNG_LVL_L3L2_BK        3       /* The system policy limits layer 3 DSCP marking and layer 2 marking
    293 	                                         * to background Network Service Types */
    294 
    295 
    296 typedef __uint32_t sae_associd_t;
    297 #define SAE_ASSOCID_ANY 0
    298 #define SAE_ASSOCID_ALL ((sae_associd_t)(-1ULL))
    299 
    300 typedef __uint32_t sae_connid_t;
    301 #define SAE_CONNID_ANY  0
    302 #define SAE_CONNID_ALL  ((sae_connid_t)(-1ULL))
    303 
    304 /* connectx() flag parameters */
    305 #define CONNECT_RESUME_ON_READ_WRITE    0x1 /* resume connect() on read/write */
    306 #define CONNECT_DATA_IDEMPOTENT         0x2 /* data is idempotent */
    307 #define CONNECT_DATA_AUTHENTICATED      0x4 /* data includes security that replaces the TFO-cookie */
    308 
    309 /* sockaddr endpoints */
    310 typedef struct sa_endpoints {
    311 	unsigned int            sae_srcif;      /* optional source interface */
    312 	const struct sockaddr   *sae_srcaddr;   /* optional source address */
    313 	socklen_t               sae_srcaddrlen; /* size of source address */
    314 	const struct sockaddr   *sae_dstaddr;   /* destination address */
    315 	socklen_t               sae_dstaddrlen; /* size of destination address */
    316 } sa_endpoints_t;
    317 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    318 
    319 /*
    320  * Structure used for manipulating linger option.
    321  */
    322 struct  linger {
    323 	int     l_onoff;                /* option on/off */
    324 	int     l_linger;               /* linger time */
    325 };
    326 
    327 #ifndef __APPLE__
    328 struct  accept_filter_arg {
    329 	char    af_name[16];
    330 	char    af_arg[256 - 16];
    331 };
    332 #endif
    333 
    334 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    335 #ifdef __APPLE__
    336 
    337 /*
    338  * Structure to control non-portable Sockets extension to POSIX
    339  */
    340 struct so_np_extensions {
    341 	u_int32_t       npx_flags;
    342 	u_int32_t       npx_mask;
    343 };
    344 
    345 #define SONPX_SETOPTSHUT        0x000000001     /* flag for allowing setsockopt after shutdown */
    346 
    347 
    348 #endif
    349 #endif
    350 
    351 /*
    352  * Level number for (get/set)sockopt() to apply to socket itself.
    353  */
    354 #define SOL_SOCKET      0xffff          /* options for socket level */
    355 
    356 
    357 /*
    358  * Address families.
    359  */
    360 #define AF_UNSPEC       0               /* unspecified */
    361 #define AF_UNIX         1               /* local to host (pipes) */
    362 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    363 #define AF_LOCAL        AF_UNIX         /* backward compatibility */
    364 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    365 #define AF_INET         2               /* internetwork: UDP, TCP, etc. */
    366 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    367 #define AF_IMPLINK      3               /* arpanet imp addresses */
    368 #define AF_PUP          4               /* pup protocols: e.g. BSP */
    369 #define AF_CHAOS        5               /* mit CHAOS protocols */
    370 #define AF_NS           6               /* XEROX NS protocols */
    371 #define AF_ISO          7               /* ISO protocols */
    372 #define AF_OSI          AF_ISO
    373 #define AF_ECMA         8               /* European computer manufacturers */
    374 #define AF_DATAKIT      9               /* datakit protocols */
    375 #define AF_CCITT        10              /* CCITT protocols, X.25 etc */
    376 #define AF_SNA          11              /* IBM SNA */
    377 #define AF_DECnet       12              /* DECnet */
    378 #define AF_DLI          13              /* DEC Direct data link interface */
    379 #define AF_LAT          14              /* LAT */
    380 #define AF_HYLINK       15              /* NSC Hyperchannel */
    381 #define AF_APPLETALK    16              /* Apple Talk */
    382 #define AF_ROUTE        17              /* Internal Routing Protocol */
    383 #define AF_LINK         18              /* Link layer interface */
    384 #define pseudo_AF_XTP   19              /* eXpress Transfer Protocol (no AF) */
    385 #define AF_COIP         20              /* connection-oriented IP, aka ST II */
    386 #define AF_CNT          21              /* Computer Network Technology */
    387 #define pseudo_AF_RTIP  22              /* Help Identify RTIP packets */
    388 #define AF_IPX          23              /* Novell Internet Protocol */
    389 #define AF_SIP          24              /* Simple Internet Protocol */
    390 #define pseudo_AF_PIP   25              /* Help Identify PIP packets */
    391 #define AF_NDRV         27              /* Network Driver 'raw' access */
    392 #define AF_ISDN         28              /* Integrated Services Digital Network */
    393 #define AF_E164         AF_ISDN         /* CCITT E.164 recommendation */
    394 #define pseudo_AF_KEY   29              /* Internal key-management function */
    395 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    396 #define AF_INET6        30              /* IPv6 */
    397 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    398 #define AF_NATM         31              /* native ATM access */
    399 #define AF_SYSTEM       32              /* Kernel event messages */
    400 #define AF_NETBIOS      33              /* NetBIOS */
    401 #define AF_PPP          34              /* PPP communication protocol */
    402 #define pseudo_AF_HDRCMPLT 35           /* Used by BPF to not rewrite headers
    403 	                                 *  in interface output routine */
    404 #define AF_RESERVED_36  36              /* Reserved for internal usage */
    405 #define AF_IEEE80211    37              /* IEEE 802.11 protocol */
    406 #define AF_UTUN         38
    407 #define AF_VSOCK        40              /* VM Sockets */
    408 #define AF_MAX          41
    409 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    410 
    411 /*
    412  * [XSI] Structure used by kernel to store most addresses.
    413  */
    414 struct sockaddr {
    415 	__uint8_t       sa_len;         /* total length */
    416 	sa_family_t     sa_family;      /* [XSI] address family */
    417 	char            sa_data[14];    /* [XSI] addr value */
    418 };
    419 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct sockaddr, sockaddr);
    420 
    421 /*
    422  * Least amount of information that a sockaddr requires.
    423  * Sockaddr_header is a compatible prefix structure of
    424  * all sockaddr objects.
    425  */
    426 struct __sockaddr_header {
    427 	__uint8_t           sa_len;
    428 	sa_family_t         sa_family;
    429 };
    430 
    431 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    432 #define SOCK_MAXADDRLEN 255             /* longest possible addresses */
    433 
    434 /*
    435  * Structure used by kernel to pass protocol
    436  * information in raw sockets.
    437  */
    438 struct sockproto {
    439 	__uint16_t      sp_family;              /* address family */
    440 	__uint16_t      sp_protocol;            /* protocol */
    441 };
    442 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    443 
    444 /*
    445  * RFC 2553: protocol-independent placeholder for socket addresses
    446  */
    447 #define _SS_MAXSIZE     128
    448 #define _SS_ALIGNSIZE   (sizeof(__int64_t))
    449 #define _SS_PAD1SIZE    \
    450 	        (_SS_ALIGNSIZE - sizeof(__uint8_t) - sizeof(sa_family_t))
    451 #define _SS_PAD2SIZE    \
    452 	        (_SS_MAXSIZE - sizeof(__uint8_t) - sizeof(sa_family_t) - \
    453 	                        _SS_PAD1SIZE - _SS_ALIGNSIZE)
    454 
    455 /*
    456  * [XSI] sockaddr_storage
    457  */
    458 struct sockaddr_storage {
    459 	__uint8_t       ss_len;         /* address length */
    460 	sa_family_t     ss_family;      /* [XSI] address family */
    461 	char                    __ss_pad1[_SS_PAD1SIZE];
    462 	__int64_t       __ss_align;     /* force structure storage alignment */
    463 	char                    __ss_pad2[_SS_PAD2SIZE];
    464 };
    465 __CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct sockaddr_storage, sockaddr_storage);
    466 
    467 /*
    468  * Protocol families, same as address families for now.
    469  */
    470 #define PF_UNSPEC       AF_UNSPEC
    471 #define PF_LOCAL        AF_LOCAL
    472 #define PF_UNIX         PF_LOCAL        /* backward compatibility */
    473 #define PF_INET         AF_INET
    474 #define PF_IMPLINK      AF_IMPLINK
    475 #define PF_PUP          AF_PUP
    476 #define PF_CHAOS        AF_CHAOS
    477 #define PF_NS           AF_NS
    478 #define PF_ISO          AF_ISO
    479 #define PF_OSI          AF_ISO
    480 #define PF_ECMA         AF_ECMA
    481 #define PF_DATAKIT      AF_DATAKIT
    482 #define PF_CCITT        AF_CCITT
    483 #define PF_SNA          AF_SNA
    484 #define PF_DECnet       AF_DECnet
    485 #define PF_DLI          AF_DLI
    486 #define PF_LAT          AF_LAT
    487 #define PF_HYLINK       AF_HYLINK
    488 #define PF_APPLETALK    AF_APPLETALK
    489 #define PF_ROUTE        AF_ROUTE
    490 #define PF_LINK         AF_LINK
    491 #define PF_XTP          pseudo_AF_XTP   /* really just proto family, no AF */
    492 #define PF_COIP         AF_COIP
    493 #define PF_CNT          AF_CNT
    494 #define PF_SIP          AF_SIP
    495 #define PF_IPX          AF_IPX          /* same format as AF_NS */
    496 #define PF_RTIP         pseudo_AF_RTIP  /* same format as AF_INET */
    497 #define PF_PIP          pseudo_AF_PIP
    498 #define PF_NDRV         AF_NDRV
    499 #define PF_ISDN         AF_ISDN
    500 #define PF_KEY          pseudo_AF_KEY
    501 #define PF_INET6        AF_INET6
    502 #define PF_NATM         AF_NATM
    503 #define PF_SYSTEM       AF_SYSTEM
    504 #define PF_NETBIOS      AF_NETBIOS
    505 #define PF_PPP          AF_PPP
    506 #define PF_RESERVED_36  AF_RESERVED_36
    507 #define PF_UTUN         AF_UTUN
    508 #define PF_VSOCK        AF_VSOCK
    509 #define PF_MAX          AF_MAX
    510 
    511 /*
    512  * These do not have socket-layer support:
    513  */
    514 #define PF_VLAN         ((uint32_t)0x766c616e)  /* 'vlan' */
    515 #define PF_BOND         ((uint32_t)0x626f6e64)  /* 'bond' */
    516 
    517 /*
    518  * Definitions for network related sysctl, CTL_NET.
    519  *
    520  * Second level is protocol family.
    521  * Third level is protocol number.
    522  *
    523  * Further levels are defined by the individual families below.
    524  */
    525 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    526 #define NET_MAXID       AF_MAX
    527 
    528 /*
    529  * PF_ROUTE - Routing table
    530  *
    531  * Three additional levels are defined:
    532  *	Fourth: address family, 0 is wildcard
    533  *	Fifth: type of info, defined below
    534  *	Sixth: flag(s) to mask with for NET_RT_FLAGS
    535  */
    536 #define NET_RT_DUMP             1       /* dump; may limit to a.f. */
    537 #define NET_RT_FLAGS            2       /* by flags, e.g. RESOLVING */
    538 #define NET_RT_IFLIST           3       /* survey interface list */
    539 #define NET_RT_STAT             4       /* routing statistics */
    540 #define NET_RT_TRASH            5       /* routes not in table but not freed */
    541 #define NET_RT_IFLIST2          6       /* interface list with addresses */
    542 #define NET_RT_DUMP2            7       /* dump; may limit to a.f. */
    543 /*
    544  * Allows read access non-local host's MAC address
    545  * if the process has neighbor cache entitlement.
    546  */
    547 #define NET_RT_FLAGS_PRIV       10
    548 #define NET_RT_MAXID            11
    549 #endif /* (_POSIX_C_SOURCE && !_DARWIN_C_SOURCE) */
    550 
    551 /*
    552  * Maximum queue length specifiable by listen.
    553  */
    554 #define SOMAXCONN       128
    555 
    556 /*
    557  * [XSI] Message header for recvmsg and sendmsg calls.
    558  * Used value-result for recvmsg, value only for sendmsg.
    559  */
    560 struct msghdr {
    561 	void            *__sized_by(msg_namelen) msg_name; /* [XSI] optional address */
    562 	socklen_t       msg_namelen;    /* [XSI] size of address */
    563 	struct          iovec *msg_iov; /* [XSI] scatter/gather array */
    564 	int             msg_iovlen;     /* [XSI] # elements in msg_iov */
    565 	void            *__sized_by(msg_controllen) msg_control; /* [XSI] ancillary data, see below */
    566 	socklen_t       msg_controllen; /* [XSI] ancillary data buffer len */
    567 	int             msg_flags;      /* [XSI] flags on received message */
    568 };
    569 
    570 #define MSG_OOB         0x1             /* process out-of-band data */
    571 #define MSG_PEEK        0x2             /* peek at incoming message */
    572 #define MSG_DONTROUTE   0x4             /* send without using routing tables */
    573 #define MSG_EOR         0x8             /* data completes record */
    574 #define MSG_TRUNC       0x10            /* data discarded before delivery */
    575 #define MSG_CTRUNC      0x20            /* control data lost before delivery */
    576 #define MSG_WAITALL     0x40            /* wait for full request or error */
    577 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    578 #define MSG_DONTWAIT    0x80            /* this message should be nonblocking */
    579 #define MSG_EOF         0x100           /* data completes connection */
    580 #ifdef __APPLE__
    581 #ifdef __APPLE_API_OBSOLETE
    582 #define MSG_WAITSTREAM  0x200           /* wait up to full request.. may return partial */
    583 #endif
    584 #define MSG_FLUSH       0x400           /* Start of 'hold' seq; dump so_temp, deprecated */
    585 #define MSG_HOLD        0x800           /* Hold frag in so_temp, deprecated */
    586 #define MSG_SEND        0x1000          /* Send the packet in so_temp, deprecated */
    587 #define MSG_HAVEMORE    0x2000          /* Data ready to be read */
    588 #define MSG_RCVMORE     0x4000          /* Data remains in current pkt */
    589 #endif
    590 #define MSG_NEEDSA      0x10000         /* Fail receive if socket address cannot be allocated */
    591 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    592 
    593 #if __DARWIN_C_LEVEL >= 200809L
    594 #define MSG_NOSIGNAL    0x80000         /* do not generate SIGPIPE on EOF */
    595 #endif /* __DARWIN_C_LEVEL */
    596 
    597 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    598 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    599 
    600 /*
    601  * Header for ancillary data objects in msg_control buffer.
    602  * Used for additional information with/about a datagram
    603  * not expressible by flags.  The format is a sequence
    604  * of message elements headed by cmsghdr structures.
    605  */
    606 struct cmsghdr {
    607 	socklen_t       cmsg_len;       /* [XSI] data byte count, including hdr */
    608 	int             cmsg_level;     /* [XSI] originating protocol */
    609 	int             cmsg_type;      /* [XSI] protocol-specific type */
    610 /* followed by	unsigned char  cmsg_data[]; */
    611 };
    612 
    613 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    614 #ifndef __APPLE__
    615 /*
    616  * While we may have more groups than this, the cmsgcred struct must
    617  * be able to fit in an mbuf, and NGROUPS_MAX is too large to allow
    618  * this.
    619  */
    620 #define CMGROUP_MAX 16
    621 
    622 /*
    623  * Credentials structure, used to verify the identity of a peer
    624  * process that has sent us a message. This is allocated by the
    625  * peer process but filled in by the kernel. This prevents the
    626  * peer from lying about its identity. (Note that cmcred_groups[0]
    627  * is the effective GID.)
    628  */
    629 struct cmsgcred {
    630 	pid_t   cmcred_pid;             /* PID of sending process */
    631 	uid_t   cmcred_uid;             /* real UID of sending process */
    632 	uid_t   cmcred_euid;            /* effective UID of sending process */
    633 	gid_t   cmcred_gid;             /* real GID of sending process */
    634 	short   cmcred_ngroups;         /* number or groups */
    635 	gid_t   cmcred_groups[CMGROUP_MAX];     /* groups */
    636 };
    637 #endif
    638 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    639 
    640 /* given pointer to struct cmsghdr, return pointer to data */
    641 #define CMSG_DATA(cmsg)         ((unsigned char *)(cmsg) + \
    642 	__DARWIN_ALIGN32(sizeof(struct cmsghdr)))
    643 
    644 /*
    645  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
    646  * an empty list for some reasons.
    647  */
    648 #define CMSG_FIRSTHDR(mhdr) \
    649 	((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
    650 	    (struct cmsghdr *)(mhdr)->msg_control : \
    651 	    (struct cmsghdr *)0L)
    652 
    653 
    654 /*
    655  * Given pointer to struct cmsghdr, return pointer to next cmsghdr
    656  * RFC 2292 says that CMSG_NXTHDR(mhdr, NULL) is equivalent to CMSG_FIRSTHDR(mhdr)
    657  */
    658 #define CMSG_NXTHDR(mhdr, cmsg)                                         \
    659 	((char *)(cmsg) == (char *)0L ? CMSG_FIRSTHDR(mhdr) :           \
    660 	    ((((unsigned char *)(cmsg) +                                \
    661 	    __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len) +            \
    662 	    __DARWIN_ALIGN32(sizeof(struct cmsghdr))) >                 \
    663 	    ((unsigned char *)(mhdr)->msg_control +                     \
    664 	    (mhdr)->msg_controllen)) ?                                  \
    665 	        (struct cmsghdr *)0L /* NULL */ :                       \
    666 	        (struct cmsghdr *)(void *)((unsigned char *)(cmsg) +    \
    667 	            __DARWIN_ALIGN32((__uint32_t)(cmsg)->cmsg_len))))
    668 
    669 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    670 /* RFC 2292 additions */
    671 #define CMSG_SPACE(l)           (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + __DARWIN_ALIGN32(l))
    672 #define CMSG_LEN(l)             (__DARWIN_ALIGN32(sizeof(struct cmsghdr)) + (l))
    673 
    674 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    675 
    676 /* "Socket"-level control message types: */
    677 #define SCM_RIGHTS                      0x01    /* access rights (array of int) */
    678 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    679 #define SCM_TIMESTAMP                   0x02    /* timestamp (struct timeval) */
    680 #define SCM_CREDS                       0x03    /* process creds (struct cmsgcred) */
    681 #define SCM_TIMESTAMP_MONOTONIC         0x04    /* timestamp (uint64_t) */
    682 
    683 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    684 
    685 /*
    686  * howto arguments for shutdown(2), specified by Posix.1g.
    687  */
    688 #define SHUT_RD         0               /* shut down the reading side */
    689 #define SHUT_WR         1               /* shut down the writing side */
    690 #define SHUT_RDWR       2               /* shut down both sides */
    691 
    692 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    693 /*
    694  * sendfile(2) header/trailer struct
    695  */
    696 struct sf_hdtr {
    697 	struct iovec *headers;  /* pointer to an array of header struct iovec's */
    698 	int hdr_cnt;            /* number of header iovec's */
    699 	struct iovec *trailers; /* pointer to an array of trailer struct iovec's */
    700 	int trl_cnt;            /* number of trailer iovec's */
    701 };
    702 
    703 
    704 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    705 
    706 __BEGIN_DECLS
    707 
    708 int     accept(int, struct sockaddr * __restrict, socklen_t * __restrict)
    709 __DARWIN_ALIAS_C(accept);
    710 int     bind(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS(bind);
    711 int     connect(int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(connect);
    712 int     getpeername(int, struct sockaddr * __restrict, socklen_t * __restrict)
    713 __DARWIN_ALIAS(getpeername);
    714 int     getsockname(int, struct sockaddr * __restrict, socklen_t * __restrict)
    715 __DARWIN_ALIAS(getsockname);
    716 int     getsockopt(int, int, int, void * __restrict, socklen_t * __restrict);
    717 int     listen(int, int) __DARWIN_ALIAS(listen);
    718 ssize_t recv(int, void *, size_t, int) __DARWIN_ALIAS_C(recv);
    719 ssize_t recvfrom(int, void *, size_t, int, struct sockaddr * __restrict,
    720     socklen_t * __restrict) __DARWIN_ALIAS_C(recvfrom);
    721 ssize_t recvmsg(int, struct msghdr *, int) __DARWIN_ALIAS_C(recvmsg);
    722 ssize_t send(int, const void *, size_t, int) __DARWIN_ALIAS_C(send);
    723 ssize_t sendmsg(int, const struct msghdr *, int) __DARWIN_ALIAS_C(sendmsg);
    724 ssize_t sendto(int, const void *, size_t,
    725     int, const struct sockaddr *, socklen_t) __DARWIN_ALIAS_C(sendto);
    726 int     setsockopt(int, int, int, const void *, socklen_t);
    727 int     shutdown(int, int);
    728 int     sockatmark(int) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
    729 int     socket(int, int, int);
    730 int     socketpair(int, int, int, int *) __DARWIN_ALIAS(socketpair);
    731 
    732 #if !defined(_POSIX_C_SOURCE)
    733 int     sendfile(int, int, off_t, off_t *, struct sf_hdtr *, int);
    734 #endif  /* !_POSIX_C_SOURCE */
    735 
    736 #if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
    737 void    pfctlinput(int, struct sockaddr *);
    738 
    739 __API_AVAILABLE(macosx(10.11), ios(9.0), tvos(9.0), watchos(2.0))
    740 int connectx(int, const sa_endpoints_t *, sae_associd_t, unsigned int,
    741     const struct iovec *, unsigned int, size_t *, sae_connid_t *);
    742 
    743 __API_AVAILABLE(macosx(10.11), ios(9.0), tvos(9.0), watchos(2.0))
    744 int disconnectx(int, sae_associd_t, sae_connid_t);
    745 #endif  /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
    746 __END_DECLS
    747 
    748 
    749 
    750 #endif /* !_SYS_SOCKET_H_ */