xref: /xnu-12377.41.6/bsd/net/sockaddr_utils.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions #ifndef _NET_SOCKADDR_UTILS_H_
29*bbb1b6f9SApple OSS Distributions #define _NET_SOCKADDR_UTILS_H_
30*bbb1b6f9SApple OSS Distributions 
31*bbb1b6f9SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
32*bbb1b6f9SApple OSS Distributions 
33*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
34*bbb1b6f9SApple OSS Distributions 
35*bbb1b6f9SApple OSS Distributions /*
36*bbb1b6f9SApple OSS Distributions  * Type conversion rules for socket address types
37*bbb1b6f9SApple OSS Distributions  *
38*bbb1b6f9SApple OSS Distributions  * 1. Context:
39*bbb1b6f9SApple OSS Distributions  *
40*bbb1b6f9SApple OSS Distributions  * XNU networking uses the "socket address" abstraction to represent
41*bbb1b6f9SApple OSS Distributions  * the addresses for the different protocol families, e.g. IPv4, IPv6,
42*bbb1b6f9SApple OSS Distributions  * UNIX domain sockets, ARP etc.
43*bbb1b6f9SApple OSS Distributions  *
44*bbb1b6f9SApple OSS Distributions  * Historically, the socket addresses were represented as a byte array,
45*bbb1b6f9SApple OSS Distributions  * starting with a uint16_t "family" discriminator.
46*bbb1b6f9SApple OSS Distributions  *
47*bbb1b6f9SApple OSS Distributions  * This was changed with the advent of XOpen UNIX standard: the uint16_t
48*bbb1b6f9SApple OSS Distributions  * "family" discriminator was split into 2 uint8_t fields: the length
49*bbb1b6f9SApple OSS Distributions  * and the protocol family.
50*bbb1b6f9SApple OSS Distributions  *
51*bbb1b6f9SApple OSS Distributions  * Since the different protocols have different addressing semantics,
52*bbb1b6f9SApple OSS Distributions  * the different addresses are represented by multiple structures.
53*bbb1b6f9SApple OSS Distributions  * For example, the IPv6 addresses are represented by `struct sockaddr_in6',
54*bbb1b6f9SApple OSS Distributions  * while the IPv4 addresses can be represented by `struct sockaddr_in' or by
55*bbb1b6f9SApple OSS Distributions  * `struct sockaddr_inifscope', depending on whether the address is bound
56*bbb1b6f9SApple OSS Distributions  * to a particular interface.
57*bbb1b6f9SApple OSS Distributions  *
58*bbb1b6f9SApple OSS Distributions  * The type `struct sockaddr' can be used interchangeably to represent any
59*bbb1b6f9SApple OSS Distributions  * of the above addresses. Essentially, the C types that represent
60*bbb1b6f9SApple OSS Distributions  * the different socket address families form a type hierarchy,
61*bbb1b6f9SApple OSS Distributions  * with the `struct sockaddr' being the root type.
62*bbb1b6f9SApple OSS Distributions  *
63*bbb1b6f9SApple OSS Distributions  * There are some exceptions to the hierarchy. Sometimes the socket addresses
64*bbb1b6f9SApple OSS Distributions  * are represented by a "container" type, e.g. `struct sockaddr_storage'
65*bbb1b6f9SApple OSS Distributions  * or `union sockaddr_in_4_6'. Finally, some protocol families, such as
66*bbb1b6f9SApple OSS Distributions  * the routing sockets, are represented by the plain `struct sockaddr'.
67*bbb1b6f9SApple OSS Distributions  *
68*bbb1b6f9SApple OSS Distributions  *
69*bbb1b6f9SApple OSS Distributions  *
70*bbb1b6f9SApple OSS Distributions  *                         +-------+                           +-----------+
71*bbb1b6f9SApple OSS Distributions  *      +------------------+ Base  |      +. - - . - - . - - . |Containers |
72*bbb1b6f9SApple OSS Distributions  *      | struct sockaddr  +----+--+      .                    +-----------+
73*bbb1b6f9SApple OSS Distributions  *      +-----------------------+         | +----------------------------+ |
74*bbb1b6f9SApple OSS Distributions  *                     ^                  . | union sockaddr_in_4_6      | .
75*bbb1b6f9SApple OSS Distributions  *                     +------------------+ +----------------------------+ |
76*bbb1b6f9SApple OSS Distributions  *                     |                  . +----------------------------+ .
77*bbb1b6f9SApple OSS Distributions  *                     |                  | | struct sockaddr_storage    | |
78*bbb1b6f9SApple OSS Distributions  *                     |                  . +----------------------------+ .
79*bbb1b6f9SApple OSS Distributions  *                     |    +----------+  | +----------------------------+ |
80*bbb1b6f9SApple OSS Distributions  *   + . - - . - - . - + - .| Concrete |  . | uint8_t * __bidi_indexable | .
81*bbb1b6f9SApple OSS Distributions  *   .                      +----------+  | +----------------------------+ |
82*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |   .                                .
83*bbb1b6f9SApple OSS Distributions  *   .  | struct sockaddr_ctl       | .   +. - - . - - . - - . - - . - - . +
84*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
85*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
86*bbb1b6f9SApple OSS Distributions  *   |  | struct sockaddr_dl        | |
87*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
88*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
89*bbb1b6f9SApple OSS Distributions  *   .  | struct sockaddr_in        | .
90*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
91*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
92*bbb1b6f9SApple OSS Distributions  *   |  | struct sockaddr_inarp     | |
93*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
94*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
95*bbb1b6f9SApple OSS Distributions  *   .  | struct sockaddr_inifscope | .
96*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
97*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
98*bbb1b6f9SApple OSS Distributions  *   |  | struct sockaddr_in6       | |
99*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
100*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
101*bbb1b6f9SApple OSS Distributions  *   .  | struct sockaddr_ndrv      | .
102*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
103*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
104*bbb1b6f9SApple OSS Distributions  *   |  | struct sockaddr_sys       | |
105*bbb1b6f9SApple OSS Distributions  *   .  +---------------------------+ .
106*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
107*bbb1b6f9SApple OSS Distributions  *   .   | struct sockaddr_un       | .
108*bbb1b6f9SApple OSS Distributions  *   |  +---------------------------+ |
109*bbb1b6f9SApple OSS Distributions  *   .                                .
110*bbb1b6f9SApple OSS Distributions  *   + . - - . - - . - - . - - . - - .+
111*bbb1b6f9SApple OSS Distributions  *
112*bbb1b6f9SApple OSS Distributions  *
113*bbb1b6f9SApple OSS Distributions  * 2. Challenges
114*bbb1b6f9SApple OSS Distributions  *
115*bbb1b6f9SApple OSS Distributions  * 2.1. Type safety challenges
116*bbb1b6f9SApple OSS Distributions  *
117*bbb1b6f9SApple OSS Distributions  * Since the pointer type `struct sockaddr *' can represent a pointer
118*bbb1b6f9SApple OSS Distributions  * to any concrete derived type, or to a container type,
119*bbb1b6f9SApple OSS Distributions  * the enforcement of bound checks can be tricky.
120*bbb1b6f9SApple OSS Distributions  *
121*bbb1b6f9SApple OSS Distributions  * In particular, one needs to safely support the following conversions:
122*bbb1b6f9SApple OSS Distributions  *
123*bbb1b6f9SApple OSS Distributions  * - From `struct sockaddr *' to any of the derived types, and vice versa.
124*bbb1b6f9SApple OSS Distributions  * - From `uint8_t *' to any of the derived types, and vice versa.
125*bbb1b6f9SApple OSS Distributions  * - From `union sockaddr_in_4_6 *' to either `struct sockaddr_in *'
126*bbb1b6f9SApple OSS Distributions  *   or to `struct sockaddr_in6 *', and vice versa.
127*bbb1b6f9SApple OSS Distributions  * - From `struct sockaddr_in *' to `struct sockaddr_inifscope *',
128*bbb1b6f9SApple OSS Distributions  *   and vice versa.
129*bbb1b6f9SApple OSS Distributions  *
130*bbb1b6f9SApple OSS Distributions  * At the same time, the system needs to make accidental conversions between
131*bbb1b6f9SApple OSS Distributions  * unrelated types difficult. Examples of such conversions include:
132*bbb1b6f9SApple OSS Distributions  *
133*bbb1b6f9SApple OSS Distributions  * - From `struct sockaddr_in *' to `struct sockaddr_un *' or vice versa.
134*bbb1b6f9SApple OSS Distributions  * - From `struct sockaddr_sys *' to `struct sockaddr_ndrv *' or vice versa.
135*bbb1b6f9SApple OSS Distributions  *
136*bbb1b6f9SApple OSS Distributions  * 2.2. ABI constraints.
137*bbb1b6f9SApple OSS Distributions  *
138*bbb1b6f9SApple OSS Distributions  * The concrete types that are listed above are used both in the kernel space,
139*bbb1b6f9SApple OSS Distributions  * the user space and by the drivers.
140*bbb1b6f9SApple OSS Distributions  *
141*bbb1b6f9SApple OSS Distributions  * 2.3. Pointer boundary challenges
142*bbb1b6f9SApple OSS Distributions  *
143*bbb1b6f9SApple OSS Distributions  * The transition between `__single' pointers, e.g. between
144*bbb1b6f9SApple OSS Distributions  * `struct sockaddr * __single' to `struct sockaddr_in6 * __single'
145*bbb1b6f9SApple OSS Distributions  * is currently assumed to be safe, as long as the concrete types
146*bbb1b6f9SApple OSS Distributions  * have determined sizes.
147*bbb1b6f9SApple OSS Distributions  *
148*bbb1b6f9SApple OSS Distributions  * The challenge occurs whenever one needs to serialize or to deserialize
149*bbb1b6f9SApple OSS Distributions  * the concrete socket address types into a byte arrays.
150*bbb1b6f9SApple OSS Distributions  *
151*bbb1b6f9SApple OSS Distributions  * 2.4. Runtime cost challenges
152*bbb1b6f9SApple OSS Distributions  *
153*bbb1b6f9SApple OSS Distributions  * The transition between the different socket address types
154*bbb1b6f9SApple OSS Distributions  * should not incure a significant CPU or memory cost in runtime.
155*bbb1b6f9SApple OSS Distributions  *
156*bbb1b6f9SApple OSS Distributions  *
157*bbb1b6f9SApple OSS Distributions  * 3. Implementation.
158*bbb1b6f9SApple OSS Distributions  *
159*bbb1b6f9SApple OSS Distributions  * This file implements a mechanism that:
160*bbb1b6f9SApple OSS Distributions  * - Enforces the type safety by ensuring that only the valid
161*bbb1b6f9SApple OSS Distributions  *   type conversions can be made.
162*bbb1b6f9SApple OSS Distributions  * - Ensures the ABI compatibility for the socket address types.
163*bbb1b6f9SApple OSS Distributions  * - Implements the conversion between the socket address types
164*bbb1b6f9SApple OSS Distributions  *   and the container types (including byte arrays) in a way
165*bbb1b6f9SApple OSS Distributions  *   that allows enforcing the boundary checks.
166*bbb1b6f9SApple OSS Distributions  * - Does not have a significant runtime impact.
167*bbb1b6f9SApple OSS Distributions  *
168*bbb1b6f9SApple OSS Distributions  * To achive that, the mechanism relies on the C generic dispatch
169*bbb1b6f9SApple OSS Distributions  * mechanism, which allows converting variables to and from
170*bbb1b6f9SApple OSS Distributions  * the desired types.
171*bbb1b6f9SApple OSS Distributions  *
172*bbb1b6f9SApple OSS Distributions  *
173*bbb1b6f9SApple OSS Distributions  * 4. Usage.
174*bbb1b6f9SApple OSS Distributions  *
175*bbb1b6f9SApple OSS Distributions  * In order to use the sockaddr_utils, the implementation
176*bbb1b6f9SApple OSS Distributions  * code needs to include this file *after* including the files
177*bbb1b6f9SApple OSS Distributions  * which define the relevant sockaddr structures.
178*bbb1b6f9SApple OSS Distributions  *
179*bbb1b6f9SApple OSS Distributions  * For example:
180*bbb1b6f9SApple OSS Distributions  *
181*bbb1b6f9SApple OSS Distributions  *    #include <netinet/in.h>
182*bbb1b6f9SApple OSS Distributions  *    #include <netinet/in_private.h>
183*bbb1b6f9SApple OSS Distributions  *    #include <net/sockaddr_utils.h>
184*bbb1b6f9SApple OSS Distributions  *
185*bbb1b6f9SApple OSS Distributions  *
186*bbb1b6f9SApple OSS Distributions  * Doing so will redefine the canonical macros such as `SA(s)`
187*bbb1b6f9SApple OSS Distributions  * and will allow for mostly seamless adoption.
188*bbb1b6f9SApple OSS Distributions  *
189*bbb1b6f9SApple OSS Distributions  * Once the adoption is mostly complete,
190*bbb1b6f9SApple OSS Distributions  * this file can be included in the "private" versions
191*bbb1b6f9SApple OSS Distributions  * of the header files, such as <netinet/in_private.h>
192*bbb1b6f9SApple OSS Distributions  */
193*bbb1b6f9SApple OSS Distributions 
194*bbb1b6f9SApple OSS Distributions #define __NET_SOCKADDR_UTILS_H_INCLUDED
195*bbb1b6f9SApple OSS Distributions #include <net/strict_type_cnv_private.h>
196*bbb1b6f9SApple OSS Distributions #undef __NET_SOCKADDR_UTILS_H_INCLUDED
197*bbb1b6f9SApple OSS Distributions 
198*bbb1b6f9SApple OSS Distributions #include <net/if_dl.h>
199*bbb1b6f9SApple OSS Distributions #include <sys/un.h>
200*bbb1b6f9SApple OSS Distributions #include <net/ndrv.h>
201*bbb1b6f9SApple OSS Distributions #include <netinet/in_private.h>
202*bbb1b6f9SApple OSS Distributions #include <netinet/if_ether.h>
203*bbb1b6f9SApple OSS Distributions #include <net/necp.h>
204*bbb1b6f9SApple OSS Distributions 
205*bbb1b6f9SApple OSS Distributions /*
206*bbb1b6f9SApple OSS Distributions  * Building blocks for the cast operations
207*bbb1b6f9SApple OSS Distributions  */
208*bbb1b6f9SApple OSS Distributions 
209*bbb1b6f9SApple OSS Distributions 
210*bbb1b6f9SApple OSS Distributions /*
211*bbb1b6f9SApple OSS Distributions  * Generic static cast for sockaddr subtypes.
212*bbb1b6f9SApple OSS Distributions  *
213*bbb1b6f9SApple OSS Distributions  * Defines a static cast expression that, given the expression EXPR
214*bbb1b6f9SApple OSS Distributions  * and the destination type DST_TYPENAME will:
215*bbb1b6f9SApple OSS Distributions  * 0. If EXPR represents a byte array, attempt to convert EXPR
216*bbb1b6f9SApple OSS Distributions  *    to DST_TYPENAME.
217*bbb1b6f9SApple OSS Distributions  * 1. If EXPR is compatible with `struct DST_TYPENAME *': return EXPR.
218*bbb1b6f9SApple OSS Distributions  * 2. If EXPR is compatible with `struct sockaddr *', perform type conversion
219*bbb1b6f9SApple OSS Distributions  *    from `struct sockaddr *' to `struct DST_TYPENAME  *'
220*bbb1b6f9SApple OSS Distributions  * 3. If EXPR is compatible with  `struct sockaddr_storage *',
221*bbb1b6f9SApple OSS Distributions  *    perform type conversion from `struct sockaddr_storage *'
222*bbb1b6f9SApple OSS Distributions  *    to `struct DST_TYPENAME *'.
223*bbb1b6f9SApple OSS Distributions  * 4. If additional conversions are enabled, attempt to apply those
224*bbb1b6f9SApple OSS Distributions  *    to the EXPR, and if successful, return the result of conversion.
225*bbb1b6f9SApple OSS Distributions  *
226*bbb1b6f9SApple OSS Distributions  * NOTE: The static cast preserves the CV qualifiers.
227*bbb1b6f9SApple OSS Distributions  */
228*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_STATIC_CAST(EXPR, DST_TYPENAME, ...)  _Generic((EXPR),                         \
229*bbb1b6f9SApple OSS Distributions 	__STC_BYTES_TO_OBJ_CNV_CLAUSE(DST_TYPENAME),                                     /* [0] */    \
230*bbb1b6f9SApple OSS Distributions 	__STC_IDENTITY_CNV_CLAUSE(struct, DST_TYPENAME),                                 /* [1] */    \
231*bbb1b6f9SApple OSS Distributions 	__STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr, DST_TYPENAME),                    /* [2] */    \
232*bbb1b6f9SApple OSS Distributions 	__STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_storage, DST_TYPENAME),            /* [3] */    \
233*bbb1b6f9SApple OSS Distributions     ##__VA_ARGS__                                                                    /* [4] */    \
234*bbb1b6f9SApple OSS Distributions )((EXPR))
235*bbb1b6f9SApple OSS Distributions 
236*bbb1b6f9SApple OSS Distributions 
237*bbb1b6f9SApple OSS Distributions /*
238*bbb1b6f9SApple OSS Distributions  * Generic const cast for sockaddr subtypes.
239*bbb1b6f9SApple OSS Distributions  *
240*bbb1b6f9SApple OSS Distributions  * Defines a const cast expression that, given the expression EXPR
241*bbb1b6f9SApple OSS Distributions  * and the destination type DST_TYPENAME will:
242*bbb1b6f9SApple OSS Distributions  * 0. If EXPR is compatible with `const struct DST_TYPENAME *':
243*bbb1b6f9SApple OSS Distributions  *    deconst EXPR and return the result.
244*bbb1b6f9SApple OSS Distributions  * 1. If EXPR is compatible with `const struct sockaddr  s*':
245*bbb1b6f9SApple OSS Distributions  *    convert EXPR to `const struct DST_TYPENAME *' and return deconsted result.
246*bbb1b6f9SApple OSS Distributions  * 2. If EXPR is compatible with `const struct sockaddr_storage *':
247*bbb1b6f9SApple OSS Distributions  *    convert EXPR to `const struct DST_TYPENAME* ' and return deconsted result.
248*bbb1b6f9SApple OSS Distributions  * 3. If additional conversions are enabled, attempt to apply those
249*bbb1b6f9SApple OSS Distributions  *    to the EXPR, and if successful, return the result of conversion.
250*bbb1b6f9SApple OSS Distributions  *
251*bbb1b6f9SApple OSS Distributions  * NOTE: The static cast preserves the CV qualifiers.
252*bbb1b6f9SApple OSS Distributions  */
253*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_CAST(EXPR, DST_TYPENAME, ...)  _Generic((EXPR),                        \
254*bbb1b6f9SApple OSS Distributions 	__STC_CONST_IDENTITY_CNV_CLAUSE(struct, DST_TYPENAME),                           /* [0] */    \
255*bbb1b6f9SApple OSS Distributions 	__STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr, DST_TYPENAME),              /* [1] */    \
256*bbb1b6f9SApple OSS Distributions 	__STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_storage, DST_TYPENAME),      /* [2] */    \
257*bbb1b6f9SApple OSS Distributions     ##__VA_ARGS__                                                                    /* [3] */    \
258*bbb1b6f9SApple OSS Distributions )((EXPR))
259*bbb1b6f9SApple OSS Distributions 
260*bbb1b6f9SApple OSS Distributions 
261*bbb1b6f9SApple OSS Distributions /*
262*bbb1b6f9SApple OSS Distributions  * Strict replacement for struct sockaddr
263*bbb1b6f9SApple OSS Distributions  */
264*bbb1b6f9SApple OSS Distributions 
265*bbb1b6f9SApple OSS Distributions /* Register the base types: struct sockaddr and struct sockaddr_storage */
266*bbb1b6f9SApple OSS Distributions __STC_DEFINE_SELF_CONVERTERS(struct, sockaddr);
267*bbb1b6f9SApple OSS Distributions __STC_DEFINE_OBJECT_CONVERTERS(struct, sockaddr, struct, sockaddr_storage);
268*bbb1b6f9SApple OSS Distributions __STC_DEFINE_OBJECT_CONVERTERS(struct, sockaddr, union, sockaddr_in_4_6);
269*bbb1b6f9SApple OSS Distributions __STC_DEFINE_OBJECT_CONVERTERS(struct, sockaddr, union, necp_sockaddr_union);
270*bbb1b6f9SApple OSS Distributions __STC_DEFINE_BYTE_TO_OBJ_CNVS(struct, sockaddr, 2, 255);
271*bbb1b6f9SApple OSS Distributions 
272*bbb1b6f9SApple OSS Distributions __STC_DEFINE_SELF_CONVERTERS(struct, sockaddr_storage);
273*bbb1b6f9SApple OSS Distributions __STC_DEFINE_BYTE_TO_OBJ_CNVS(struct, sockaddr_storage,
274*bbb1b6f9SApple OSS Distributions     sizeof(struct sockaddr_storage), sizeof(struct sockaddr_storage));
275*bbb1b6f9SApple OSS Distributions __STC_DEFINE_BYTE_TO_OBJ_CNVS(union, sockaddr_in_4_6,
276*bbb1b6f9SApple OSS Distributions     sizeof(union sockaddr_in_4_6), sizeof(union sockaddr_in_4_6));
277*bbb1b6f9SApple OSS Distributions __STC_DEFINE_BYTE_TO_OBJ_CNVS(union, necp_sockaddr_union,
278*bbb1b6f9SApple OSS Distributions     sizeof(union necp_sockaddr_union), sizeof(union necp_sockaddr_union));
279*bbb1b6f9SApple OSS Distributions 
280*bbb1b6f9SApple OSS Distributions 
281*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
282*bbb1b6f9SApple OSS Distributions  *  Generic converter to bytes.
283*bbb1b6f9SApple OSS Distributions  */
284*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_BYTES(X) _Generic((X),                                                 \
285*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr),                                      \
286*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_storage),                              \
287*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(union, sockaddr_in_4_6),                                \
288*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(union, necp_sockaddr_union),                            \
289*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_ctl),                                  \
290*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_dl),                                   \
291*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_in),                                   \
292*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_in6),                                  \
293*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_inarp),                                \
294*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_inifscope),                            \
295*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_ndrv),                                 \
296*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_sys),                                  \
297*bbb1b6f9SApple OSS Distributions 	        __STC_OBJ_TO_BYTES_CNV_CLAUSE(struct, sockaddr_un),                                   \
298*bbb1b6f9SApple OSS Distributions 	        __STC_BYTES_TO_BYTES_CNV_CLAUSE()                                                     \
299*bbb1b6f9SApple OSS Distributions 	)((X))
300*bbb1b6f9SApple OSS Distributions 
301*bbb1b6f9SApple OSS Distributions 
302*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
303*bbb1b6f9SApple OSS Distributions  * Converters to `struct sockaddr *'
304*bbb1b6f9SApple OSS Distributions  */
305*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR(X) _Generic((X),                                              \
306*bbb1b6f9SApple OSS Distributions 	        __STC_BYTES_TO_OBJ_CNV_CLAUSE(sockaddr),                                              \
307*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_storage, sockaddr),                     \
308*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(union, sockaddr_in_4_6, sockaddr),                       \
309*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(union, necp_sockaddr_union, sockaddr),                   \
310*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_ctl, sockaddr),                         \
311*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_dl, sockaddr),                          \
312*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_in, sockaddr),                          \
313*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_in6, sockaddr),                         \
314*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_inarp, sockaddr),                       \
315*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_inifscope, sockaddr),                   \
316*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_ndrv, sockaddr),                        \
317*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_sys, sockaddr),                         \
318*bbb1b6f9SApple OSS Distributions 	        __STC_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_un, sockaddr),                          \
319*bbb1b6f9SApple OSS Distributions 	        __STC_IDENTITY_CNV_CLAUSE(struct, sockaddr)                                           \
320*bbb1b6f9SApple OSS Distributions 	)((X))
321*bbb1b6f9SApple OSS Distributions 
322*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR(X) _Generic((X),                                  \
323*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_storage, sockaddr),               \
324*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(union, sockaddr_in_4_6, sockaddr),                 \
325*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(union, necp_sockaddr_union, sockaddr),             \
326*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_ctl, sockaddr),                   \
327*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_dl, sockaddr),                    \
328*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_in, sockaddr),                    \
329*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_in6, sockaddr),                   \
330*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_inarp, sockaddr),                 \
331*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_inifscope, sockaddr),             \
332*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_ndrv, sockaddr),                  \
333*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_sys, sockaddr),                   \
334*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_TYPE_TO_OBJ_CNV_CLAUSE(struct, sockaddr_un, sockaddr),                    \
335*bbb1b6f9SApple OSS Distributions 	        __STC_CONST_IDENTITY_CNV_CLAUSE(struct, sockaddr)                                     \
336*bbb1b6f9SApple OSS Distributions 	)((X))
337*bbb1b6f9SApple OSS Distributions 
338*bbb1b6f9SApple OSS Distributions 
339*bbb1b6f9SApple OSS Distributions #if defined(SA)
340*bbb1b6f9SApple OSS Distributions #undef SA
341*bbb1b6f9SApple OSS Distributions #endif /* defined(SA) */
342*bbb1b6f9SApple OSS Distributions #define SA(s)                          __SA_UTILS_CONV_TO_SOCKADDR((s))
343*bbb1b6f9SApple OSS Distributions #define __DECONST_SA(s)                __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR((s))
344*bbb1b6f9SApple OSS Distributions 
345*bbb1b6f9SApple OSS Distributions #define SA_BYTES(s) __SA_UTILS_CONV_TO_BYTES(s)
346*bbb1b6f9SApple OSS Distributions 
347*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
348*bbb1b6f9SApple OSS Distributions  * Replacements for `bcopy', `bcmp' and `bzero'.
349*bbb1b6f9SApple OSS Distributions  */
350*bbb1b6f9SApple OSS Distributions #define SOCKADDR_COPY(SRC, DST, LEN)  do {                                                        \
351*bbb1b6f9SApple OSS Distributions 	const uint8_t* __sau_sbytes = __SA_UTILS_CONV_TO_BYTES((SRC));                                \
352*bbb1b6f9SApple OSS Distributions 	uint8_t* __sau_dbytes = __SA_UTILS_CONV_TO_BYTES((DST));                                      \
353*bbb1b6f9SApple OSS Distributions 	bcopy(__sau_sbytes, __sau_dbytes, (LEN));                                                     \
354*bbb1b6f9SApple OSS Distributions } while(0)
355*bbb1b6f9SApple OSS Distributions 
356*bbb1b6f9SApple OSS Distributions 
357*bbb1b6f9SApple OSS Distributions #define SOCKADDR_ZERO(SRC, LEN)  do {                                                             \
358*bbb1b6f9SApple OSS Distributions 	uint8_t* __sau_src_bytes = __SA_UTILS_CONV_TO_BYTES((SRC));                                   \
359*bbb1b6f9SApple OSS Distributions 	bzero(__sau_src_bytes, (LEN));                                                                \
360*bbb1b6f9SApple OSS Distributions } while(0)
361*bbb1b6f9SApple OSS Distributions 
362*bbb1b6f9SApple OSS Distributions 
363*bbb1b6f9SApple OSS Distributions #define SOCKADDR_CMP(LH, RH, LEN)  ({                                                             \
364*bbb1b6f9SApple OSS Distributions 	int __sac_rv = 0;                                                                             \
365*bbb1b6f9SApple OSS Distributions 	const uint8_t* __sau_lhb = __SA_UTILS_CONV_TO_BYTES((LH));                                    \
366*bbb1b6f9SApple OSS Distributions 	const uint8_t* __sau_rhb = __SA_UTILS_CONV_TO_BYTES((RH));                                    \
367*bbb1b6f9SApple OSS Distributions 	__sac_rv = bcmp(__sau_lhb, __sau_rhb, (LEN));                                                 \
368*bbb1b6f9SApple OSS Distributions 	__sac_rv;                                                                                     \
369*bbb1b6f9SApple OSS Distributions })
370*bbb1b6f9SApple OSS Distributions 
371*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
372*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_ctl *'
373*bbb1b6f9SApple OSS Distributions  */
374*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_ctl)
375*bbb1b6f9SApple OSS Distributions 
376*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_CTL(X)              __SA_UTILS_STATIC_CAST(X, sockaddr_ctl)
377*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_CTL(X)  __SA_UTILS_DECONST_CAST(X, sockaddr_ctl)
378*bbb1b6f9SApple OSS Distributions 
379*bbb1b6f9SApple OSS Distributions #define SCTL(s)                        __SA_UTILS_CONV_TO_SOCKADDR_CTL((s))
380*bbb1b6f9SApple OSS Distributions #define __DECONST_SCTL(s)              __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_CTL((s))
381*bbb1b6f9SApple OSS Distributions 
382*bbb1b6f9SApple OSS Distributions 
383*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
384*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_dl *'
385*bbb1b6f9SApple OSS Distributions  */
386*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_VARIABLE_SIZE_SUBTYPE(struct, sockaddr_dl)
387*bbb1b6f9SApple OSS Distributions 
388*bbb1b6f9SApple OSS Distributions 
389*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_DL(X)             __SA_UTILS_STATIC_CAST(X, sockaddr_dl)
390*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_DL(X) __SA_UTILS_DECONST_CAST (X, sockaddr_dl)
391*bbb1b6f9SApple OSS Distributions 
392*bbb1b6f9SApple OSS Distributions #if defined(SDL)
393*bbb1b6f9SApple OSS Distributions #undef SDL
394*bbb1b6f9SApple OSS Distributions #endif /* defined(SDL) */
395*bbb1b6f9SApple OSS Distributions #define SDL(s)                         __SA_UTILS_CONV_TO_SOCKADDR_DL((s))
396*bbb1b6f9SApple OSS Distributions #define __DECONST_SDL(s)               __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_DL((s))
397*bbb1b6f9SApple OSS Distributions 
398*bbb1b6f9SApple OSS Distributions #if defined(LLADDR)
399*bbb1b6f9SApple OSS Distributions #undef LLADDR
400*bbb1b6f9SApple OSS Distributions #endif /* defined(LLADDR) */
401*bbb1b6f9SApple OSS Distributions #define LLADDR(s) ((caddr_t)(__SA_UTILS_CONV_TO_BYTES((s)) + __offsetof(struct sockaddr_dl, sdl_data) + (s)->sdl_nlen))
402*bbb1b6f9SApple OSS Distributions 
403*bbb1b6f9SApple OSS Distributions #if defined(CONST_LLADDR)
404*bbb1b6f9SApple OSS Distributions #undef CONST_LLADDR
405*bbb1b6f9SApple OSS Distributions #endif /* defined(CONST_LLADDR) */
406*bbb1b6f9SApple OSS Distributions #define CONST_LLADDR(s) ((const uint8_t *)LLADDR((s)))
407*bbb1b6f9SApple OSS Distributions 
408*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
409*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_in *'
410*bbb1b6f9SApple OSS Distributions  */
411*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_in,                                         \
412*bbb1b6f9SApple OSS Distributions         union, sockaddr_in_4_6,                                                                   \
413*bbb1b6f9SApple OSS Distributions         union, necp_sockaddr_union)
414*bbb1b6f9SApple OSS Distributions 
415*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_IN(X)                                                         \
416*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_in,                                                        \
417*bbb1b6f9SApple OSS Distributions 	__STC_ENABLE_STATIC_CAST(union, sockaddr_in_4_6, sockaddr_in),                                \
418*bbb1b6f9SApple OSS Distributions 	__STC_ENABLE_STATIC_CAST(union, necp_sockaddr_union, sockaddr_in))
419*bbb1b6f9SApple OSS Distributions 
420*bbb1b6f9SApple OSS Distributions 
421*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_IN(X)                                             \
422*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST (X, sockaddr_in,                                                      \
423*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, sockaddr_in_4_6, sockaddr_in),                           \
424*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, necp_sockaddr_union, sockaddr_in))
425*bbb1b6f9SApple OSS Distributions 
426*bbb1b6f9SApple OSS Distributions #if defined(SIN)
427*bbb1b6f9SApple OSS Distributions #undef SIN
428*bbb1b6f9SApple OSS Distributions #endif /* defined(SIN) */
429*bbb1b6f9SApple OSS Distributions #define SIN(s)                   __SA_UTILS_CONV_TO_SOCKADDR_IN((s))
430*bbb1b6f9SApple OSS Distributions #define __DECONST_SIN(s)         __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_IN((s))
431*bbb1b6f9SApple OSS Distributions 
432*bbb1b6f9SApple OSS Distributions #if defined(satosin)
433*bbb1b6f9SApple OSS Distributions #undef satosin
434*bbb1b6f9SApple OSS Distributions #endif /* defined(satosin) */
435*bbb1b6f9SApple OSS Distributions #define satosin(sa)     SIN(sa)
436*bbb1b6f9SApple OSS Distributions 
437*bbb1b6f9SApple OSS Distributions #if defined(sintosa)
438*bbb1b6f9SApple OSS Distributions #undef sintosa
439*bbb1b6f9SApple OSS Distributions #endif /* defined(sintosa) */
440*bbb1b6f9SApple OSS Distributions #define sintosa(sin)     SA(sin)
441*bbb1b6f9SApple OSS Distributions 
442*bbb1b6f9SApple OSS Distributions 
443*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
444*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_inarp *'
445*bbb1b6f9SApple OSS Distributions  */
446*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_inarp)
447*bbb1b6f9SApple OSS Distributions 
448*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_INARP(X)                                                      \
449*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_inarp)
450*bbb1b6f9SApple OSS Distributions 
451*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_INARP(X)                                          \
452*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_inarp)
453*bbb1b6f9SApple OSS Distributions 
454*bbb1b6f9SApple OSS Distributions #define SINARP(s)                __SA_UTILS_CONV_TO_SOCKADDR_INARP((s))
455*bbb1b6f9SApple OSS Distributions #define __DECONST_SINARP(s)      __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_INARP((s))
456*bbb1b6f9SApple OSS Distributions 
457*bbb1b6f9SApple OSS Distributions 
458*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
459*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_inifscope *'
460*bbb1b6f9SApple OSS Distributions  */
461*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_inifscope,                                  \
462*bbb1b6f9SApple OSS Distributions         union, sockaddr_in_4_6,                                                                   \
463*bbb1b6f9SApple OSS Distributions         union, necp_sockaddr_union)
464*bbb1b6f9SApple OSS Distributions 
465*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_INIFSCOPE(X)                                                  \
466*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_inifscope,                                                 \
467*bbb1b6f9SApple OSS Distributions 	   __STC_ENABLE_STATIC_CAST(union, sockaddr_in_4_6, sockaddr_inifscope),                      \
468*bbb1b6f9SApple OSS Distributions 	   __STC_ENABLE_STATIC_CAST(union, necp_sockaddr_union, sockaddr_inifscope))
469*bbb1b6f9SApple OSS Distributions 
470*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_INIFSCOPE(X)                                      \
471*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_inifscope,                                                \
472*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, sockaddr_in_4_6, sockaddr_inifscope),                    \
473*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, necp_sockaddr_union, sockaddr_inifscope))
474*bbb1b6f9SApple OSS Distributions 
475*bbb1b6f9SApple OSS Distributions #if defined(SINIFSCOPE)
476*bbb1b6f9SApple OSS Distributions #undef SINIFSCOPE
477*bbb1b6f9SApple OSS Distributions #endif /* defined(SINIFSCOPE) */
478*bbb1b6f9SApple OSS Distributions #define SINIFSCOPE(s)            __SA_UTILS_CONV_TO_SOCKADDR_INIFSCOPE((s))
479*bbb1b6f9SApple OSS Distributions #define __DECONST_SINIFSCOPE(s)  __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_INIFSCOPE((s))
480*bbb1b6f9SApple OSS Distributions 
481*bbb1b6f9SApple OSS Distributions 
482*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
483*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_in6 *'
484*bbb1b6f9SApple OSS Distributions  */
485*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_in6,                                        \
486*bbb1b6f9SApple OSS Distributions         union, sockaddr_in_4_6,                                                                   \
487*bbb1b6f9SApple OSS Distributions         union, necp_sockaddr_union)
488*bbb1b6f9SApple OSS Distributions 
489*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_IN6(X)                                                        \
490*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_in6,                                                       \
491*bbb1b6f9SApple OSS Distributions 	__STC_ENABLE_STATIC_CAST(union, sockaddr_in_4_6, sockaddr_in6),                               \
492*bbb1b6f9SApple OSS Distributions 	__STC_ENABLE_STATIC_CAST(union, necp_sockaddr_union, sockaddr_in6))
493*bbb1b6f9SApple OSS Distributions 
494*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_IN6(X)                                            \
495*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_in6,                                                      \
496*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, sockaddr_in_4_6, sockaddr_in6),                          \
497*bbb1b6f9SApple OSS Distributions 	    __STC_ENABLE_DECONST_CAST(union, necp_sockaddr_union, sockaddr_in6))
498*bbb1b6f9SApple OSS Distributions 
499*bbb1b6f9SApple OSS Distributions #if defined(SIN6)
500*bbb1b6f9SApple OSS Distributions #undef SIN6
501*bbb1b6f9SApple OSS Distributions #endif /* defined(SIN6) */
502*bbb1b6f9SApple OSS Distributions #define SIN6(s)                  __SA_UTILS_CONV_TO_SOCKADDR_IN6((s))
503*bbb1b6f9SApple OSS Distributions #define __DECONST_SIN6(s)        __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_IN6((s))
504*bbb1b6f9SApple OSS Distributions 
505*bbb1b6f9SApple OSS Distributions #if defined(satosin6)
506*bbb1b6f9SApple OSS Distributions #undef satosin6
507*bbb1b6f9SApple OSS Distributions #endif /* defined(satosin6) */
508*bbb1b6f9SApple OSS Distributions #define satosin6(sa)    SIN6(sa)
509*bbb1b6f9SApple OSS Distributions 
510*bbb1b6f9SApple OSS Distributions #if defined(sin6tosa)
511*bbb1b6f9SApple OSS Distributions #undef sin6tosa
512*bbb1b6f9SApple OSS Distributions #endif /* defined(sin6tosa) */
513*bbb1b6f9SApple OSS Distributions #define sin6tosa(sin6)   SA((sin6))
514*bbb1b6f9SApple OSS Distributions 
515*bbb1b6f9SApple OSS Distributions #if defined(SIN6IFSCOPE)
516*bbb1b6f9SApple OSS Distributions #undef SIN6IFSCOPE
517*bbb1b6f9SApple OSS Distributions #endif /* defined(SIN6IFSCOPE) */
518*bbb1b6f9SApple OSS Distributions #define SIN6IFSCOPE(s)  SIN6(s)
519*bbb1b6f9SApple OSS Distributions 
520*bbb1b6f9SApple OSS Distributions 
521*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
522*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_ndrv *'
523*bbb1b6f9SApple OSS Distributions  */
524*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_ndrv)
525*bbb1b6f9SApple OSS Distributions 
526*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_NDRV(X)                                                       \
527*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_ndrv)
528*bbb1b6f9SApple OSS Distributions 
529*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_NDRV(X)                                           \
530*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_ndrv)
531*bbb1b6f9SApple OSS Distributions 
532*bbb1b6f9SApple OSS Distributions #define SNDRV(s)                 __SA_UTILS_CONV_TO_SOCKADDR_NDRV((s))
533*bbb1b6f9SApple OSS Distributions #define __DECONST_SNDRV(s)       __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_NDRV((s))
534*bbb1b6f9SApple OSS Distributions 
535*bbb1b6f9SApple OSS Distributions 
536*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
537*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_sys *'
538*bbb1b6f9SApple OSS Distributions  */
539*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_FIXED_SIZE_SUBTYPE(struct, sockaddr_sys)
540*bbb1b6f9SApple OSS Distributions 
541*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_SYS(X)                                                        \
542*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_sys)
543*bbb1b6f9SApple OSS Distributions 
544*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_SYS(X)                                            \
545*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_sys)
546*bbb1b6f9SApple OSS Distributions 
547*bbb1b6f9SApple OSS Distributions #define SSYS(s)                  __SA_UTILS_CONV_TO_SOCKADDR_SYS((s))
548*bbb1b6f9SApple OSS Distributions #define __DECONST_SSYS(s)        __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_SYS((s))
549*bbb1b6f9SApple OSS Distributions 
550*bbb1b6f9SApple OSS Distributions 
551*bbb1b6f9SApple OSS Distributions /*************************************************************************************************
552*bbb1b6f9SApple OSS Distributions  * Strict replacement for `struct sockaddr_un *'
553*bbb1b6f9SApple OSS Distributions  */
554*bbb1b6f9SApple OSS Distributions __SA_UTILS_DEFINE_VARIABLE_SIZE_SUBTYPE(struct, sockaddr_un)
555*bbb1b6f9SApple OSS Distributions 
556*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_CONV_TO_SOCKADDR_UN(X)                                                         \
557*bbb1b6f9SApple OSS Distributions     __SA_UTILS_STATIC_CAST(X, sockaddr_un)
558*bbb1b6f9SApple OSS Distributions 
559*bbb1b6f9SApple OSS Distributions #define __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_UN(X)                                             \
560*bbb1b6f9SApple OSS Distributions     __SA_UTILS_DECONST_CAST(X, sockaddr_un)
561*bbb1b6f9SApple OSS Distributions 
562*bbb1b6f9SApple OSS Distributions #define SUN(s)                   __SA_UTILS_CONV_TO_SOCKADDR_UN((s))
563*bbb1b6f9SApple OSS Distributions #define __DECONST_SUN(s)         __SA_UTILS_DECONST_AND_CONV_TO_SOCKADDR_UN((s))
564*bbb1b6f9SApple OSS Distributions 
565*bbb1b6f9SApple OSS Distributions 
566*bbb1b6f9SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
567*bbb1b6f9SApple OSS Distributions 
568*bbb1b6f9SApple OSS Distributions #endif /* _NET_SOCKADDR_UTILS_H_ */
569