1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2023-2024 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
29*bbb1b6f9SApple OSS Distributions
30*bbb1b6f9SApple OSS Distributions /*
31*bbb1b6f9SApple OSS Distributions * Disconnect should happen when passed a NULL address
32*bbb1b6f9SApple OSS Distributions * Verify we can reconnect after a disconnect
33*bbb1b6f9SApple OSS Distributions */
34*bbb1b6f9SApple OSS Distributions
35*bbb1b6f9SApple OSS Distributions #include <sys/fcntl.h>
36*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
37*bbb1b6f9SApple OSS Distributions
38*bbb1b6f9SApple OSS Distributions #include <net/if.h>
39*bbb1b6f9SApple OSS Distributions #include <net/route.h>
40*bbb1b6f9SApple OSS Distributions
41*bbb1b6f9SApple OSS Distributions #include <netinet/in.h>
42*bbb1b6f9SApple OSS Distributions
43*bbb1b6f9SApple OSS Distributions #include <stdbool.h>
44*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
45*bbb1b6f9SApple OSS Distributions #include <string.h>
46*bbb1b6f9SApple OSS Distributions #include <unistd.h>
47*bbb1b6f9SApple OSS Distributions
48*bbb1b6f9SApple OSS Distributions #include <arpa/inet.h>
49*bbb1b6f9SApple OSS Distributions
50*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
51*bbb1b6f9SApple OSS Distributions
52*bbb1b6f9SApple OSS Distributions #include "net_test_lib.h"
53*bbb1b6f9SApple OSS Distributions
54*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
55*bbb1b6f9SApple OSS Distributions T_META_NAMESPACE("xnu.net"),
56*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
57*bbb1b6f9SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking")
58*bbb1b6f9SApple OSS Distributions );
59*bbb1b6f9SApple OSS Distributions
60*bbb1b6f9SApple OSS Distributions #define MAX_IPv6_STR_LEN 64
61*bbb1b6f9SApple OSS Distributions
62*bbb1b6f9SApple OSS Distributions static char l_addr_str[MAX_IPv6_STR_LEN];
63*bbb1b6f9SApple OSS Distributions static char f_addr_str[MAX_IPv6_STR_LEN];
64*bbb1b6f9SApple OSS Distributions
65*bbb1b6f9SApple OSS Distributions const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
66*bbb1b6f9SApple OSS Distributions #define s6_addr32 __u6_addr.__u6_addr32
67*bbb1b6f9SApple OSS Distributions
68*bbb1b6f9SApple OSS Distributions
69*bbb1b6f9SApple OSS Distributions static void
init_sin_address(struct sockaddr_in * sin)70*bbb1b6f9SApple OSS Distributions init_sin_address(struct sockaddr_in *sin)
71*bbb1b6f9SApple OSS Distributions {
72*bbb1b6f9SApple OSS Distributions memset(sin, 0, sizeof(struct sockaddr_in));
73*bbb1b6f9SApple OSS Distributions sin->sin_len = sizeof(struct sockaddr_in);
74*bbb1b6f9SApple OSS Distributions sin->sin_family = AF_INET;
75*bbb1b6f9SApple OSS Distributions }
76*bbb1b6f9SApple OSS Distributions
77*bbb1b6f9SApple OSS Distributions static void
init_sin6_address(struct sockaddr_in6 * sin6)78*bbb1b6f9SApple OSS Distributions init_sin6_address(struct sockaddr_in6 *sin6)
79*bbb1b6f9SApple OSS Distributions {
80*bbb1b6f9SApple OSS Distributions memset(sin6, 0, sizeof(struct sockaddr_in6));
81*bbb1b6f9SApple OSS Distributions sin6->sin6_len = sizeof(struct sockaddr_in6);
82*bbb1b6f9SApple OSS Distributions sin6->sin6_family = AF_INET6;
83*bbb1b6f9SApple OSS Distributions }
84*bbb1b6f9SApple OSS Distributions
85*bbb1b6f9SApple OSS Distributions static void
udp_disconnect_v4(int client_fd,struct sockaddr_in * sin_null,int expected_error)86*bbb1b6f9SApple OSS Distributions udp_disconnect_v4(int client_fd, struct sockaddr_in *sin_null, int expected_error)
87*bbb1b6f9SApple OSS Distributions {
88*bbb1b6f9SApple OSS Distributions if (expected_error == 0) {
89*bbb1b6f9SApple OSS Distributions socklen_t socklen;
90*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin_local = { 0 };
91*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin_peer = { 0 };
92*bbb1b6f9SApple OSS Distributions
93*bbb1b6f9SApple OSS Distributions // Disconnect
94*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(connect(client_fd, (struct sockaddr *)sin_null, sizeof(struct sockaddr_in)), EADDRNOTAVAIL, NULL);
95*bbb1b6f9SApple OSS Distributions
96*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin_local);
97*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(client_fd, (struct sockaddr *)&sin_local, &socklen), NULL);
98*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET, &sin_local.sin_addr, l_addr_str, sizeof(l_addr_str));
99*bbb1b6f9SApple OSS Distributions
100*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin_peer);
101*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(getpeername(client_fd, (struct sockaddr *)&sin_peer, &socklen), ENOTCONN, NULL);
102*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET, &sin_peer.sin_addr, f_addr_str, sizeof(f_addr_str));
103*bbb1b6f9SApple OSS Distributions
104*bbb1b6f9SApple OSS Distributions T_LOG("disconnected with %s:%u to %s:%u",
105*bbb1b6f9SApple OSS Distributions l_addr_str, ntohs(sin_local.sin_port),
106*bbb1b6f9SApple OSS Distributions f_addr_str, ntohs(sin_peer.sin_port));
107*bbb1b6f9SApple OSS Distributions } else {
108*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(connect(client_fd, (struct sockaddr *)sin_null, sizeof(struct sockaddr_in)), expected_error, NULL);
109*bbb1b6f9SApple OSS Distributions }
110*bbb1b6f9SApple OSS Distributions }
111*bbb1b6f9SApple OSS Distributions
112*bbb1b6f9SApple OSS Distributions static void
udp_connect_v4(int client_fd,struct sockaddr_in * sin_to)113*bbb1b6f9SApple OSS Distributions udp_connect_v4(int client_fd, struct sockaddr_in *sin_to)
114*bbb1b6f9SApple OSS Distributions {
115*bbb1b6f9SApple OSS Distributions int listen_fd = -1;
116*bbb1b6f9SApple OSS Distributions socklen_t socklen;
117*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin_local = { 0 };
118*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin_peer = { 0 };
119*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin;
120*bbb1b6f9SApple OSS Distributions
121*bbb1b6f9SApple OSS Distributions init_sin_address(&sin);
122*bbb1b6f9SApple OSS Distributions init_sin_address(&sin_local);
123*bbb1b6f9SApple OSS Distributions init_sin_address(&sin_peer);
124*bbb1b6f9SApple OSS Distributions
125*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), NULL);
126*bbb1b6f9SApple OSS Distributions
127*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)), NULL);
128*bbb1b6f9SApple OSS Distributions
129*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin);
130*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(listen_fd, (struct sockaddr *)&sin, &socklen), NULL);
131*bbb1b6f9SApple OSS Distributions
132*bbb1b6f9SApple OSS Distributions T_LOG("listening on port: %u", ntohs(sin.sin_port));
133*bbb1b6f9SApple OSS Distributions sin_to->sin_port = sin.sin_port;
134*bbb1b6f9SApple OSS Distributions
135*bbb1b6f9SApple OSS Distributions T_LOG("connect with sin_len: %u sin_family: %u sin_port: %u sin_addr: 0x%08x",
136*bbb1b6f9SApple OSS Distributions sin_to->sin_len, sin_to->sin_family, ntohs(sin_to->sin_port), ntohl(sin_to->sin_addr.s_addr));
137*bbb1b6f9SApple OSS Distributions
138*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(connect(client_fd, (struct sockaddr *)sin_to, sizeof(struct sockaddr_in)), NULL);
139*bbb1b6f9SApple OSS Distributions
140*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin_local);
141*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(client_fd, (struct sockaddr *)&sin_local, &socklen), NULL);
142*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET, &sin_local.sin_addr, l_addr_str, sizeof(l_addr_str));
143*bbb1b6f9SApple OSS Distributions
144*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin_peer);
145*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getpeername(client_fd, (struct sockaddr *)&sin_peer, &socklen), NULL);
146*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET, &sin_peer.sin_addr, f_addr_str, sizeof(f_addr_str));
147*bbb1b6f9SApple OSS Distributions
148*bbb1b6f9SApple OSS Distributions T_LOG("connected with %s:%u to %s:%u",
149*bbb1b6f9SApple OSS Distributions l_addr_str, ntohs(sin_local.sin_port),
150*bbb1b6f9SApple OSS Distributions f_addr_str, ntohs(sin_peer.sin_port));
151*bbb1b6f9SApple OSS Distributions
152*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(listen_fd), NULL);
153*bbb1b6f9SApple OSS Distributions }
154*bbb1b6f9SApple OSS Distributions
155*bbb1b6f9SApple OSS Distributions T_DECL(udp_disconnect_null_ipv4, "UDP connect with a IPv4 loopback address", T_META_TAG_VM_PREFERRED)
156*bbb1b6f9SApple OSS Distributions {
157*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin = { 0 };
158*bbb1b6f9SApple OSS Distributions init_sin_address(&sin);
159*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr), 1, NULL);
160*bbb1b6f9SApple OSS Distributions
161*bbb1b6f9SApple OSS Distributions struct sockaddr_in sin_null = { 0 };
162*bbb1b6f9SApple OSS Distributions init_sin_address(&sin_null);
163*bbb1b6f9SApple OSS Distributions
164*bbb1b6f9SApple OSS Distributions int s = -1;
165*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), NULL);
166*bbb1b6f9SApple OSS Distributions
167*bbb1b6f9SApple OSS Distributions udp_connect_v4(s, &sin);
168*bbb1b6f9SApple OSS Distributions udp_disconnect_v4(s, &sin_null, 0);
169*bbb1b6f9SApple OSS Distributions udp_connect_v4(s, &sin);
170*bbb1b6f9SApple OSS Distributions
171*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
172*bbb1b6f9SApple OSS Distributions }
173*bbb1b6f9SApple OSS Distributions
174*bbb1b6f9SApple OSS Distributions static void
udp_disconnect_v6(int client_fd,struct sockaddr_in6 * sin6_null,int expected_error)175*bbb1b6f9SApple OSS Distributions udp_disconnect_v6(int client_fd,
176*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 *sin6_null, int expected_error)
177*bbb1b6f9SApple OSS Distributions {
178*bbb1b6f9SApple OSS Distributions if (expected_error == 0) {
179*bbb1b6f9SApple OSS Distributions socklen_t socklen;
180*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_local = { 0 };
181*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_peer = { 0 };
182*bbb1b6f9SApple OSS Distributions
183*bbb1b6f9SApple OSS Distributions // Disconnect
184*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(connect(client_fd, (struct sockaddr *)sin6_null, sizeof(struct sockaddr_in6)), EADDRNOTAVAIL, NULL);
185*bbb1b6f9SApple OSS Distributions
186*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin6_local);
187*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(client_fd, (struct sockaddr *)&sin6_local, &socklen), NULL);
188*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET6, &sin6_local.sin6_addr, l_addr_str, sizeof(l_addr_str));
189*bbb1b6f9SApple OSS Distributions
190*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin6_peer);
191*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(getpeername(client_fd, (struct sockaddr *)&sin6_peer, &socklen), ENOTCONN, NULL);
192*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET6, &sin6_peer.sin6_addr, f_addr_str, sizeof(f_addr_str));
193*bbb1b6f9SApple OSS Distributions
194*bbb1b6f9SApple OSS Distributions T_LOG("re=connected from %s:%u to %s:%u",
195*bbb1b6f9SApple OSS Distributions l_addr_str, ntohs(sin6_local.sin6_port),
196*bbb1b6f9SApple OSS Distributions f_addr_str, ntohs(sin6_peer.sin6_port));
197*bbb1b6f9SApple OSS Distributions } else {
198*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_FAILURE(connect(client_fd, (struct sockaddr *)sin6_null, sizeof(struct sockaddr_in6)), expected_error, NULL);
199*bbb1b6f9SApple OSS Distributions }
200*bbb1b6f9SApple OSS Distributions }
201*bbb1b6f9SApple OSS Distributions
202*bbb1b6f9SApple OSS Distributions static void
udp_connect_v6(int client_fd,struct sockaddr_in6 * sin6_to)203*bbb1b6f9SApple OSS Distributions udp_connect_v6(int client_fd, struct sockaddr_in6 *sin6_to)
204*bbb1b6f9SApple OSS Distributions {
205*bbb1b6f9SApple OSS Distributions int listen_fd = -1;
206*bbb1b6f9SApple OSS Distributions socklen_t socklen;
207*bbb1b6f9SApple OSS Distributions int off = 0;
208*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_local = { 0 };
209*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_peer = { 0 };
210*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6;
211*bbb1b6f9SApple OSS Distributions
212*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6);
213*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6_local);
214*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6_peer);
215*bbb1b6f9SApple OSS Distributions
216*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(listen_fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), NULL);
217*bbb1b6f9SApple OSS Distributions
218*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(listen_fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off)), NULL);
219*bbb1b6f9SApple OSS Distributions
220*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bind(listen_fd, (struct sockaddr *)&sin6, sizeof(sin6)), NULL);
221*bbb1b6f9SApple OSS Distributions
222*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin6);
223*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(listen_fd, (struct sockaddr *)&sin6, &socklen), NULL);
224*bbb1b6f9SApple OSS Distributions
225*bbb1b6f9SApple OSS Distributions T_LOG("listening on port: %u", ntohs(sin6.sin6_port));
226*bbb1b6f9SApple OSS Distributions sin6_to->sin6_port = sin6.sin6_port;
227*bbb1b6f9SApple OSS Distributions
228*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET6, &sin6_to->sin6_addr, l_addr_str, sizeof(l_addr_str));
229*bbb1b6f9SApple OSS Distributions T_LOG("connecting with sin6_len: %u sin6_family: %u sin6_port: %u sin6_addr: %s",
230*bbb1b6f9SApple OSS Distributions sin6_to->sin6_len, sin6_to->sin6_family, ntohs(sin6_to->sin6_port), l_addr_str);
231*bbb1b6f9SApple OSS Distributions
232*bbb1b6f9SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(connect(client_fd, (struct sockaddr *)sin6_to, sizeof(struct sockaddr_in6)), NULL);
233*bbb1b6f9SApple OSS Distributions
234*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin6_local);
235*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getsockname(client_fd, (struct sockaddr *)&sin6_local, &socklen), NULL);
236*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET6, &sin6_local.sin6_addr, l_addr_str, sizeof(l_addr_str));
237*bbb1b6f9SApple OSS Distributions
238*bbb1b6f9SApple OSS Distributions socklen = sizeof(sin6_peer);
239*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(getpeername(client_fd, (struct sockaddr *)&sin6_peer, &socklen), NULL);
240*bbb1b6f9SApple OSS Distributions (void)inet_ntop(AF_INET6, &sin6_peer.sin6_addr, f_addr_str, sizeof(f_addr_str));
241*bbb1b6f9SApple OSS Distributions
242*bbb1b6f9SApple OSS Distributions T_LOG("connected with %s:%u to %s:%u",
243*bbb1b6f9SApple OSS Distributions l_addr_str, ntohs(sin6_local.sin6_port),
244*bbb1b6f9SApple OSS Distributions f_addr_str, ntohs(sin6_peer.sin6_port));
245*bbb1b6f9SApple OSS Distributions
246*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(listen_fd), NULL);
247*bbb1b6f9SApple OSS Distributions }
248*bbb1b6f9SApple OSS Distributions
249*bbb1b6f9SApple OSS Distributions T_DECL(udp_disconnect_null_ipv6, "UDP connect with IPv4 multicast mapped IPv6 address", T_META_TAG_VM_PREFERRED)
250*bbb1b6f9SApple OSS Distributions {
251*bbb1b6f9SApple OSS Distributions if (!has_ipv6_default_route()) {
252*bbb1b6f9SApple OSS Distributions T_SKIP("test require IPv4 default route");
253*bbb1b6f9SApple OSS Distributions }
254*bbb1b6f9SApple OSS Distributions
255*bbb1b6f9SApple OSS Distributions int s = -1;
256*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6 = { 0 };
257*bbb1b6f9SApple OSS Distributions
258*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6);
259*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "::1", &sin6.sin6_addr), 1, NULL);
260*bbb1b6f9SApple OSS Distributions
261*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_null = { 0 };
262*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6_null);
263*bbb1b6f9SApple OSS Distributions
264*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), NULL);
265*bbb1b6f9SApple OSS Distributions
266*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
267*bbb1b6f9SApple OSS Distributions udp_disconnect_v6(s, &sin6_null, 0);
268*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
269*bbb1b6f9SApple OSS Distributions
270*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
271*bbb1b6f9SApple OSS Distributions }
272*bbb1b6f9SApple OSS Distributions
273*bbb1b6f9SApple OSS Distributions T_DECL(udp_disconnect_null_ipv4_mapped_ipv6, "UDP connect with IPv4 multicast mapped IPv6 address", T_META_TAG_VM_PREFERRED)
274*bbb1b6f9SApple OSS Distributions {
275*bbb1b6f9SApple OSS Distributions if (!has_ipv4_default_route()) {
276*bbb1b6f9SApple OSS Distributions T_SKIP("test require IPv4 default route");
277*bbb1b6f9SApple OSS Distributions }
278*bbb1b6f9SApple OSS Distributions
279*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6 = { 0 };
280*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6);
281*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6.sin6_addr), 1, NULL);
282*bbb1b6f9SApple OSS Distributions
283*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_null = { 0 };
284*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6_null);
285*bbb1b6f9SApple OSS Distributions
286*bbb1b6f9SApple OSS Distributions int s = -1;
287*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), NULL);
288*bbb1b6f9SApple OSS Distributions
289*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
290*bbb1b6f9SApple OSS Distributions udp_disconnect_v6(s, &sin6_null, 0);
291*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
292*bbb1b6f9SApple OSS Distributions
293*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
294*bbb1b6f9SApple OSS Distributions }
295*bbb1b6f9SApple OSS Distributions
296*bbb1b6f9SApple OSS Distributions T_DECL(udp_disconnect_mapped_ipv4_mapped_ipv6, "UDP connect with IPv4 multicast mapped IPv6 address", T_META_TAG_VM_PREFERRED)
297*bbb1b6f9SApple OSS Distributions {
298*bbb1b6f9SApple OSS Distributions if (!has_ipv4_default_route()) {
299*bbb1b6f9SApple OSS Distributions T_SKIP("test require IPv4 default route");
300*bbb1b6f9SApple OSS Distributions }
301*bbb1b6f9SApple OSS Distributions
302*bbb1b6f9SApple OSS Distributions int s = -1;
303*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), NULL);
304*bbb1b6f9SApple OSS Distributions
305*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6 = { 0 };
306*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6);
307*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6.sin6_addr), 1, NULL);
308*bbb1b6f9SApple OSS Distributions
309*bbb1b6f9SApple OSS Distributions struct sockaddr_in6 sin6_null = { 0 };
310*bbb1b6f9SApple OSS Distributions init_sin6_address(&sin6_null);
311*bbb1b6f9SApple OSS Distributions sin6_null.sin6_addr.s6_addr[10] = 0xff;
312*bbb1b6f9SApple OSS Distributions sin6_null.sin6_addr.s6_addr[11] = 0xff;
313*bbb1b6f9SApple OSS Distributions
314*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
315*bbb1b6f9SApple OSS Distributions udp_disconnect_v6(s, &sin6_null, 0);
316*bbb1b6f9SApple OSS Distributions udp_connect_v6(s, &sin6);
317*bbb1b6f9SApple OSS Distributions
318*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(close(s), NULL);
319*bbb1b6f9SApple OSS Distributions }
320