xref: /xnu-10002.81.5/tests/recvmsg_x_ctrunc.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2020-2022 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions /* -*- compile-command: "xcrun --sdk macosx.internal make -C tests recvmsg_x_test" -*- */
30*5e3eaea3SApple OSS Distributions 
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions #define __APPLE_USE_RFC_3542 1
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions #include <sys/errno.h>
35*5e3eaea3SApple OSS Distributions #include <sys/fcntl.h>
36*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
37*5e3eaea3SApple OSS Distributions #include <netinet/in.h>
38*5e3eaea3SApple OSS Distributions #include <stdbool.h>
39*5e3eaea3SApple OSS Distributions #include <err.h>
40*5e3eaea3SApple OSS Distributions #include <stdio.h>
41*5e3eaea3SApple OSS Distributions #include <stdlib.h>
42*5e3eaea3SApple OSS Distributions #include <string.h>
43*5e3eaea3SApple OSS Distributions #include <sysexits.h>
44*5e3eaea3SApple OSS Distributions #include <unistd.h>
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions #include <arpa/inet.h>
47*5e3eaea3SApple OSS Distributions 
48*5e3eaea3SApple OSS Distributions #include <darwintest.h>
49*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions #define NMSGS       10
52*5e3eaea3SApple OSS Distributions #define BUFFERLEN   1000
53*5e3eaea3SApple OSS Distributions 
54*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"));
55*5e3eaea3SApple OSS Distributions 
56*5e3eaea3SApple OSS Distributions static void
send_packets(int sendSocket,u_int packetCount,struct sockaddr * to,int proto)57*5e3eaea3SApple OSS Distributions send_packets(int sendSocket, u_int packetCount, struct sockaddr *to, int proto)
58*5e3eaea3SApple OSS Distributions {
59*5e3eaea3SApple OSS Distributions 	u_int nmsgs = NMSGS;
60*5e3eaea3SApple OSS Distributions 	ssize_t sentMsgsCount = 0;
61*5e3eaea3SApple OSS Distributions 	struct msghdr_x msgList[NMSGS];
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions 	struct msghdr_x *msg;
64*5e3eaea3SApple OSS Distributions 	struct iovec iovarray[NMSGS];
65*5e3eaea3SApple OSS Distributions 	char bytes[nmsgs][100];
66*5e3eaea3SApple OSS Distributions 	const socklen_t cmsg_size = (socklen_t)CMSG_SPACE(sizeof(int));
67*5e3eaea3SApple OSS Distributions 	char cmsgbuf[NMSGS][cmsg_size];
68*5e3eaea3SApple OSS Distributions 
69*5e3eaea3SApple OSS Distributions 	bzero(msgList, sizeof(msgList));
70*5e3eaea3SApple OSS Distributions 	bzero(cmsgbuf, sizeof(cmsgbuf));
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions 	for (int i = 0; i < NMSGS; i++) {
73*5e3eaea3SApple OSS Distributions 		msg = &msgList[i];
74*5e3eaea3SApple OSS Distributions 
75*5e3eaea3SApple OSS Distributions 		int dscp = (i % 64) << 2;
76*5e3eaea3SApple OSS Distributions 		struct cmsghdr *cm;
77*5e3eaea3SApple OSS Distributions 
78*5e3eaea3SApple OSS Distributions 		cm = (struct cmsghdr *)(void *)&cmsgbuf[i][0];
79*5e3eaea3SApple OSS Distributions 		if (proto == IPPROTO_IP) {
80*5e3eaea3SApple OSS Distributions 			cm->cmsg_len = CMSG_LEN(sizeof(int));
81*5e3eaea3SApple OSS Distributions 			cm->cmsg_level = IPPROTO_IP;
82*5e3eaea3SApple OSS Distributions 			cm->cmsg_type = IP_TOS;
83*5e3eaea3SApple OSS Distributions 			*(int *)(void *)CMSG_DATA(cm) = dscp;
84*5e3eaea3SApple OSS Distributions 			msg->msg_control = cmsgbuf[i];
85*5e3eaea3SApple OSS Distributions 			msg->msg_controllen = CMSG_SPACE(sizeof(int));
86*5e3eaea3SApple OSS Distributions 		} else if (proto == IPPROTO_IPV6) {
87*5e3eaea3SApple OSS Distributions 			cm->cmsg_len = CMSG_LEN(sizeof(sizeof(int)));
88*5e3eaea3SApple OSS Distributions 			cm->cmsg_level = IPPROTO_IPV6;
89*5e3eaea3SApple OSS Distributions 			cm->cmsg_type = IPV6_TCLASS;
90*5e3eaea3SApple OSS Distributions 			*(int *)(void *)CMSG_DATA(cm) = dscp;
91*5e3eaea3SApple OSS Distributions 			msg->msg_control = cmsgbuf[i];
92*5e3eaea3SApple OSS Distributions 			msg->msg_controllen = CMSG_SPACE(sizeof(int));
93*5e3eaea3SApple OSS Distributions 		}
94*5e3eaea3SApple OSS Distributions 
95*5e3eaea3SApple OSS Distributions 		msg->msg_name = (void *)to;
96*5e3eaea3SApple OSS Distributions 		msg->msg_namelen = to->sa_len;
97*5e3eaea3SApple OSS Distributions 		msg->msg_iov = &iovarray[i];
98*5e3eaea3SApple OSS Distributions 		msg->msg_iovlen = 1;
99*5e3eaea3SApple OSS Distributions 		iovarray[i].iov_base = &bytes[i];
100*5e3eaea3SApple OSS Distributions 		iovarray[i].iov_len = 100;
101*5e3eaea3SApple OSS Distributions 		msg->msg_flags = 0;
102*5e3eaea3SApple OSS Distributions 	}
103*5e3eaea3SApple OSS Distributions 
104*5e3eaea3SApple OSS Distributions 	while (1) {
105*5e3eaea3SApple OSS Distributions 		if (packetCount < nmsgs) {
106*5e3eaea3SApple OSS Distributions 			nmsgs = packetCount;
107*5e3eaea3SApple OSS Distributions 		}
108*5e3eaea3SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(sentMsgsCount = sendmsg_x(sendSocket, msgList, nmsgs, 0), "sendmsg_x()");
109*5e3eaea3SApple OSS Distributions 		if (sentMsgsCount < 0) {
110*5e3eaea3SApple OSS Distributions 			break;
111*5e3eaea3SApple OSS Distributions 		} else {
112*5e3eaea3SApple OSS Distributions 			packetCount -= sentMsgsCount;
113*5e3eaea3SApple OSS Distributions 		}
114*5e3eaea3SApple OSS Distributions 		if (packetCount == 0) {
115*5e3eaea3SApple OSS Distributions 			break;
116*5e3eaea3SApple OSS Distributions 		}
117*5e3eaea3SApple OSS Distributions 	}
118*5e3eaea3SApple OSS Distributions }
119*5e3eaea3SApple OSS Distributions 
120*5e3eaea3SApple OSS Distributions static bool
receive_packets(int recvSocket)121*5e3eaea3SApple OSS Distributions receive_packets(int recvSocket)
122*5e3eaea3SApple OSS Distributions {
123*5e3eaea3SApple OSS Distributions 	uint8_t maxPacketsToRead = NMSGS;
124*5e3eaea3SApple OSS Distributions 	int i;
125*5e3eaea3SApple OSS Distributions 	struct msghdr_x msglist[NMSGS];
126*5e3eaea3SApple OSS Distributions 	char bytes[NMSGS][100];
127*5e3eaea3SApple OSS Distributions 	struct iovec vec[NMSGS];
128*5e3eaea3SApple OSS Distributions 	const socklen_t cmsg_size = (socklen_t)MAX(CMSG_SPACE(sizeof(struct in_pktinfo)), CMSG_SPACE(sizeof(struct in6_pktinfo))) + CMSG_SPACE(sizeof(int));
129*5e3eaea3SApple OSS Distributions 	char cmsgbuf[NMSGS][cmsg_size];
130*5e3eaea3SApple OSS Distributions 	struct sockaddr_storage remoteAddress[NMSGS];
131*5e3eaea3SApple OSS Distributions 	bool success = true;
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions 	bzero(msglist, sizeof(msglist));
134*5e3eaea3SApple OSS Distributions 	bzero(vec, sizeof(vec));
135*5e3eaea3SApple OSS Distributions 	bzero(cmsgbuf, sizeof(cmsgbuf));
136*5e3eaea3SApple OSS Distributions 
137*5e3eaea3SApple OSS Distributions 
138*5e3eaea3SApple OSS Distributions 	ssize_t total_received = 0;
139*5e3eaea3SApple OSS Distributions 	while (1) {
140*5e3eaea3SApple OSS Distributions 		ssize_t npkts;
141*5e3eaea3SApple OSS Distributions 
142*5e3eaea3SApple OSS Distributions 		for (i = 0; i < maxPacketsToRead; i++) {
143*5e3eaea3SApple OSS Distributions 			struct msghdr_x *msg = &msglist[i];
144*5e3eaea3SApple OSS Distributions 			vec[i].iov_base = &bytes[i];
145*5e3eaea3SApple OSS Distributions 			vec[i].iov_len = 100;
146*5e3eaea3SApple OSS Distributions 
147*5e3eaea3SApple OSS Distributions 			msg->msg_name = &remoteAddress[i];
148*5e3eaea3SApple OSS Distributions 			msg->msg_namelen = sizeof(struct sockaddr_storage);
149*5e3eaea3SApple OSS Distributions 			msg->msg_iov = &vec[i];
150*5e3eaea3SApple OSS Distributions 			msg->msg_iovlen = 1;
151*5e3eaea3SApple OSS Distributions 			msg->msg_control = cmsgbuf[i];
152*5e3eaea3SApple OSS Distributions 			msg->msg_controllen = cmsg_size;
153*5e3eaea3SApple OSS Distributions 		}
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions 		npkts = recvmsg_x(recvSocket, msglist, maxPacketsToRead, 0);
156*5e3eaea3SApple OSS Distributions 		if (npkts < 0) {
157*5e3eaea3SApple OSS Distributions 			if (errno == EINTR || errno == EWOULDBLOCK) {
158*5e3eaea3SApple OSS Distributions 				continue;
159*5e3eaea3SApple OSS Distributions 			}
160*5e3eaea3SApple OSS Distributions 			T_EXPECT_POSIX_SUCCESS(npkts, "recvmsg_x() npkts %ld total_received %ld", npkts, total_received);
161*5e3eaea3SApple OSS Distributions 			break;
162*5e3eaea3SApple OSS Distributions 		}
163*5e3eaea3SApple OSS Distributions 		total_received += npkts;
164*5e3eaea3SApple OSS Distributions 
165*5e3eaea3SApple OSS Distributions 		for (i = 0; i < npkts; i++) {
166*5e3eaea3SApple OSS Distributions 			struct msghdr_x *msg = &msglist[i];
167*5e3eaea3SApple OSS Distributions 
168*5e3eaea3SApple OSS Distributions 			if ((msg->msg_controllen < (socklen_t)sizeof(struct cmsghdr)) || (msg->msg_flags & MSG_CTRUNC)) {
169*5e3eaea3SApple OSS Distributions 				success = false;
170*5e3eaea3SApple OSS Distributions 				T_LOG("msg[%d] bad  control message len=%d (< %u?) msg_flags 0x%x socket %d",
171*5e3eaea3SApple OSS Distributions 				    i, msg->msg_controllen, cmsg_size, msg->msg_flags, recvSocket);
172*5e3eaea3SApple OSS Distributions 			} else {
173*5e3eaea3SApple OSS Distributions 				T_LOG("msg[%d] good control message len=%d (< %u?) msg_flags 0x%x socket %d",
174*5e3eaea3SApple OSS Distributions 				    i, msg->msg_controllen, cmsg_size, msg->msg_flags, recvSocket);
175*5e3eaea3SApple OSS Distributions 			}
176*5e3eaea3SApple OSS Distributions 			for (struct cmsghdr *cm = (struct cmsghdr *)CMSG_FIRSTHDR(msg);
177*5e3eaea3SApple OSS Distributions 			    cm != NULL;
178*5e3eaea3SApple OSS Distributions 			    cm = (struct cmsghdr *)CMSG_NXTHDR(msg, cm)) {
179*5e3eaea3SApple OSS Distributions 				T_LOG(" cmsg_level %u cmsg_type %u cmsg_len %u",
180*5e3eaea3SApple OSS Distributions 				    cm->cmsg_level, cm->cmsg_type, cm->cmsg_len);
181*5e3eaea3SApple OSS Distributions 
182*5e3eaea3SApple OSS Distributions 				if (cm->cmsg_level == IPPROTO_IP &&
183*5e3eaea3SApple OSS Distributions 				    cm->cmsg_type == IP_RECVTOS &&
184*5e3eaea3SApple OSS Distributions 				    cm->cmsg_len == CMSG_LEN(sizeof(u_char))) {
185*5e3eaea3SApple OSS Distributions 					u_char ip_tos = *(u_char *)(void *)CMSG_DATA(cm);
186*5e3eaea3SApple OSS Distributions 
187*5e3eaea3SApple OSS Distributions 					T_LOG("   ip_tos 0x%x", ip_tos);
188*5e3eaea3SApple OSS Distributions 				} else if (cm->cmsg_level == IPPROTO_IPV6 &&
189*5e3eaea3SApple OSS Distributions 				    cm->cmsg_type == IPV6_TCLASS &&
190*5e3eaea3SApple OSS Distributions 				    cm->cmsg_len == CMSG_LEN(sizeof(int))) {
191*5e3eaea3SApple OSS Distributions 					int ipv6_tclass = *(int *)(void *)CMSG_DATA(cm);
192*5e3eaea3SApple OSS Distributions 
193*5e3eaea3SApple OSS Distributions 					T_LOG("   ipv6_tclass 0x%x", ipv6_tclass);
194*5e3eaea3SApple OSS Distributions 				} else if (cm->cmsg_level == IPPROTO_IPV6 &&
195*5e3eaea3SApple OSS Distributions 				    cm->cmsg_type == IPV6_PKTINFO &&
196*5e3eaea3SApple OSS Distributions 				    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
197*5e3eaea3SApple OSS Distributions 					struct in6_pktinfo *pktinfo = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
198*5e3eaea3SApple OSS Distributions 					char addr[40];
199*5e3eaea3SApple OSS Distributions 
200*5e3eaea3SApple OSS Distributions 					T_LOG("   pktinfo addr %s ifindex %u",
201*5e3eaea3SApple OSS Distributions 					    inet_ntop(AF_INET6, &pktinfo->ipi6_addr, addr, sizeof(addr)), pktinfo->ipi6_ifindex);
202*5e3eaea3SApple OSS Distributions 				} else if (cm->cmsg_level == IPPROTO_IP &&
203*5e3eaea3SApple OSS Distributions 				    cm->cmsg_type == IP_PKTINFO &&
204*5e3eaea3SApple OSS Distributions 				    cm->cmsg_len == CMSG_LEN(sizeof(struct in_pktinfo))) {
205*5e3eaea3SApple OSS Distributions 					struct in_pktinfo *pktinfo = (struct in_pktinfo *)(void *)CMSG_DATA(cm);
206*5e3eaea3SApple OSS Distributions 					char spec_dst[20];
207*5e3eaea3SApple OSS Distributions 					char addr[20];
208*5e3eaea3SApple OSS Distributions 
209*5e3eaea3SApple OSS Distributions 					inet_ntop(AF_INET, &pktinfo->ipi_spec_dst, spec_dst, sizeof(spec_dst));
210*5e3eaea3SApple OSS Distributions 					inet_ntop(AF_INET, &pktinfo->ipi_addr, addr, sizeof(addr));
211*5e3eaea3SApple OSS Distributions 
212*5e3eaea3SApple OSS Distributions 					T_LOG("   pktinfo ifindex %u spec_dest %s addr %s",
213*5e3eaea3SApple OSS Distributions 					    pktinfo->ipi_ifindex, spec_dst, addr);
214*5e3eaea3SApple OSS Distributions 				}
215*5e3eaea3SApple OSS Distributions 			}
216*5e3eaea3SApple OSS Distributions 		}
217*5e3eaea3SApple OSS Distributions 
218*5e3eaea3SApple OSS Distributions 		if (total_received >= maxPacketsToRead) {
219*5e3eaea3SApple OSS Distributions 			// Since we received max number of packets in the last loop, it is not clear if there
220*5e3eaea3SApple OSS Distributions 			// are any more left in the socket buffer. So we need to try again
221*5e3eaea3SApple OSS Distributions 			break;
222*5e3eaea3SApple OSS Distributions 		}
223*5e3eaea3SApple OSS Distributions 	}
224*5e3eaea3SApple OSS Distributions 	return success;
225*5e3eaea3SApple OSS Distributions }
226*5e3eaea3SApple OSS Distributions 
227*5e3eaea3SApple OSS Distributions T_DECL(recvmsg_x_ipv4_udp, "revcmsg_x() ipv4")
228*5e3eaea3SApple OSS Distributions {
229*5e3eaea3SApple OSS Distributions 	struct sockaddr_in addr = {
230*5e3eaea3SApple OSS Distributions 		.sin_len = sizeof(addr),
231*5e3eaea3SApple OSS Distributions 		.sin_family = AF_INET,
232*5e3eaea3SApple OSS Distributions 		.sin_addr.s_addr = htonl(0x7f000001),
233*5e3eaea3SApple OSS Distributions 		.sin_port = 0
234*5e3eaea3SApple OSS Distributions 	};
235*5e3eaea3SApple OSS Distributions 
236*5e3eaea3SApple OSS Distributions 	int recvSocket;
237*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "socket()");
238*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(bind(recvSocket, (const struct sockaddr *)&addr, sizeof(addr)), "bind()");
239*5e3eaea3SApple OSS Distributions 
240*5e3eaea3SApple OSS Distributions 	socklen_t addrLen = sizeof(addr);
241*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(getsockname(recvSocket, (struct sockaddr *)&addr, &addrLen), "getsockname()");
242*5e3eaea3SApple OSS Distributions 
243*5e3eaea3SApple OSS Distributions 	int one = 1;
244*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IP, IP_RECVPKTINFO, (void *)&one, sizeof(one)), "setsockopt(IP_RECVPKTINFO)");
245*5e3eaea3SApple OSS Distributions 
246*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IP, IP_RECVTOS, (void *)&one, sizeof(one)), "setsockopt(IP_RECVTOS)");
247*5e3eaea3SApple OSS Distributions 
248*5e3eaea3SApple OSS Distributions 	int flags = fcntl(recvSocket, F_GETFL, 0);
249*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(fcntl(recvSocket, F_SETFL, flags | O_NONBLOCK), "fcntl()");
250*5e3eaea3SApple OSS Distributions 
251*5e3eaea3SApple OSS Distributions 	int sendSocket;
252*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "sendSocket socket()");
253*5e3eaea3SApple OSS Distributions 
254*5e3eaea3SApple OSS Distributions 	send_packets(sendSocket, 10, (struct sockaddr *)&addr, IPPROTO_IP);
255*5e3eaea3SApple OSS Distributions 
256*5e3eaea3SApple OSS Distributions 	bool result;
257*5e3eaea3SApple OSS Distributions 	T_EXPECT_EQ(result = receive_packets(recvSocket), true, "receive_packets");
258*5e3eaea3SApple OSS Distributions 
259*5e3eaea3SApple OSS Distributions 	close(sendSocket);
260*5e3eaea3SApple OSS Distributions 	close(recvSocket);
261*5e3eaea3SApple OSS Distributions }
262*5e3eaea3SApple OSS Distributions 
263*5e3eaea3SApple OSS Distributions T_DECL(recvmsg_x_ipv6_udp, "exercise revcmsg_x() ")
264*5e3eaea3SApple OSS Distributions {
265*5e3eaea3SApple OSS Distributions 	struct sockaddr_in6 addr = {
266*5e3eaea3SApple OSS Distributions 		.sin6_len = sizeof(addr),
267*5e3eaea3SApple OSS Distributions 		.sin6_family = AF_INET6,
268*5e3eaea3SApple OSS Distributions 		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
269*5e3eaea3SApple OSS Distributions 		.sin6_flowinfo = 0,
270*5e3eaea3SApple OSS Distributions 		.sin6_scope_id = 0,
271*5e3eaea3SApple OSS Distributions 		.sin6_port = 0
272*5e3eaea3SApple OSS Distributions 	};
273*5e3eaea3SApple OSS Distributions 
274*5e3eaea3SApple OSS Distributions 	int recvSocket;
275*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(recvSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), "socket()");
276*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(bind(recvSocket, (const struct sockaddr *)&addr, sizeof(addr)), "bind()");
277*5e3eaea3SApple OSS Distributions 
278*5e3eaea3SApple OSS Distributions 	socklen_t addrLen = sizeof(addr);
279*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(getsockname(recvSocket, (struct sockaddr *)&addr, &addrLen), "getsockname()");
280*5e3eaea3SApple OSS Distributions 
281*5e3eaea3SApple OSS Distributions 	int one = 1;
282*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IPV6, IPV6_RECVPKTINFO, (void *)&one, sizeof(one)), "setsockopt(IPV6_RECVPKTINFO)");
283*5e3eaea3SApple OSS Distributions 
284*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IPV6, IPV6_RECVTCLASS, (void *)&one, sizeof(one)), "setsockopt(IPV6_RECVTCLASS)");
285*5e3eaea3SApple OSS Distributions 
286*5e3eaea3SApple OSS Distributions 	int flags = fcntl(recvSocket, F_GETFL, 0);
287*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(fcntl(recvSocket, F_SETFL, flags | O_NONBLOCK), "fcntl()");
288*5e3eaea3SApple OSS Distributions 
289*5e3eaea3SApple OSS Distributions 	int sendSocket;
290*5e3eaea3SApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(sendSocket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP), "sendSocket socket()");
291*5e3eaea3SApple OSS Distributions 
292*5e3eaea3SApple OSS Distributions 	send_packets(sendSocket, 10, (struct sockaddr *)&addr, IPPROTO_IPV6);
293*5e3eaea3SApple OSS Distributions 
294*5e3eaea3SApple OSS Distributions 	bool result;
295*5e3eaea3SApple OSS Distributions 	T_EXPECT_EQ(result = receive_packets(recvSocket), true, "receive_packets");
296*5e3eaea3SApple OSS Distributions 
297*5e3eaea3SApple OSS Distributions 	close(sendSocket);
298*5e3eaea3SApple OSS Distributions 	close(recvSocket);
299*5e3eaea3SApple OSS Distributions }
300