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