xref: /xnu-8020.121.3/tests/recvmsg_x_test.c (revision fdd8201d7b966f0c3ea610489d29bd841d358941)
1*fdd8201dSApple OSS Distributions /*
2*fdd8201dSApple OSS Distributions  * Copyright (c) 2020 Apple Inc. All rights reserved.
3*fdd8201dSApple OSS Distributions  *
4*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*fdd8201dSApple OSS Distributions  *
6*fdd8201dSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*fdd8201dSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*fdd8201dSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*fdd8201dSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*fdd8201dSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*fdd8201dSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*fdd8201dSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*fdd8201dSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*fdd8201dSApple OSS Distributions  *
15*fdd8201dSApple OSS Distributions  * Please obtain a copy of the License at
16*fdd8201dSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*fdd8201dSApple OSS Distributions  *
18*fdd8201dSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*fdd8201dSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*fdd8201dSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*fdd8201dSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*fdd8201dSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*fdd8201dSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*fdd8201dSApple OSS Distributions  * limitations under the License.
25*fdd8201dSApple OSS Distributions  *
26*fdd8201dSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*fdd8201dSApple OSS Distributions  */
28*fdd8201dSApple OSS Distributions 
29*fdd8201dSApple OSS Distributions /* -*- compile-command: "xcrun --sdk iphoneos.internal make recvmsg_x_test" -*- */
30*fdd8201dSApple OSS Distributions 
31*fdd8201dSApple OSS Distributions 
32*fdd8201dSApple OSS Distributions #include <sys/errno.h>
33*fdd8201dSApple OSS Distributions #include <sys/fcntl.h>
34*fdd8201dSApple OSS Distributions #include <sys/socket.h>
35*fdd8201dSApple OSS Distributions #include <netinet/in.h>
36*fdd8201dSApple OSS Distributions #include <stdbool.h>
37*fdd8201dSApple OSS Distributions #include <err.h>
38*fdd8201dSApple OSS Distributions #include <stdio.h>
39*fdd8201dSApple OSS Distributions #include <stdlib.h>
40*fdd8201dSApple OSS Distributions #include <string.h>
41*fdd8201dSApple OSS Distributions #include <sysexits.h>
42*fdd8201dSApple OSS Distributions #include <unistd.h>
43*fdd8201dSApple OSS Distributions 
44*fdd8201dSApple OSS Distributions #include <darwintest.h>
45*fdd8201dSApple OSS Distributions #include <darwintest_utils.h>
46*fdd8201dSApple OSS Distributions 
47*fdd8201dSApple OSS Distributions #define NMSGS       5
48*fdd8201dSApple OSS Distributions #define BUFFERLEN   1000
49*fdd8201dSApple OSS Distributions 
50*fdd8201dSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"));
51*fdd8201dSApple OSS Distributions 
52*fdd8201dSApple OSS Distributions static void
sendPackets(int s,struct sockaddr * dst,unsigned int numMsg,size_t bufferLen)53*fdd8201dSApple OSS Distributions sendPackets(int s, struct sockaddr *dst, unsigned int numMsg, size_t bufferLen)
54*fdd8201dSApple OSS Distributions {
55*fdd8201dSApple OSS Distributions 	ssize_t count = 0;
56*fdd8201dSApple OSS Distributions 	struct msghdr msg = {};
57*fdd8201dSApple OSS Distributions 	struct iovec vec = {};
58*fdd8201dSApple OSS Distributions 	char *bytes = calloc(1, bufferLen);
59*fdd8201dSApple OSS Distributions 	if (bytes == NULL) {
60*fdd8201dSApple OSS Distributions 		err(EX_OSERR, "calloc()");
61*fdd8201dSApple OSS Distributions 	}
62*fdd8201dSApple OSS Distributions 
63*fdd8201dSApple OSS Distributions 	vec.iov_base = bytes;
64*fdd8201dSApple OSS Distributions 	vec.iov_len = bufferLen;
65*fdd8201dSApple OSS Distributions 
66*fdd8201dSApple OSS Distributions 	msg.msg_name = (void *)dst;
67*fdd8201dSApple OSS Distributions 	msg.msg_namelen = dst->sa_len;
68*fdd8201dSApple OSS Distributions 	msg.msg_iov = &vec;
69*fdd8201dSApple OSS Distributions 	msg.msg_iovlen = 1;
70*fdd8201dSApple OSS Distributions 	msg.msg_flags = 0;
71*fdd8201dSApple OSS Distributions 
72*fdd8201dSApple OSS Distributions 	for (unsigned int i = 0; i < numMsg; i++) {
73*fdd8201dSApple OSS Distributions 		ssize_t n;
74*fdd8201dSApple OSS Distributions 		T_QUIET; T_EXPECT_POSIX_SUCCESS(n = sendmsg(s, &msg, 0), "sendmsg()");
75*fdd8201dSApple OSS Distributions 		T_LOG("Sent %ld bytes\n", n);
76*fdd8201dSApple OSS Distributions 		count += 1;
77*fdd8201dSApple OSS Distributions 	}
78*fdd8201dSApple OSS Distributions 
79*fdd8201dSApple OSS Distributions 	// Wait a bit to make sure the packets reach the receiver
80*fdd8201dSApple OSS Distributions 	usleep(100000);
81*fdd8201dSApple OSS Distributions 
82*fdd8201dSApple OSS Distributions 	T_LOG("Sent %ld packet\n", count);
83*fdd8201dSApple OSS Distributions 
84*fdd8201dSApple OSS Distributions 	free(bytes);
85*fdd8201dSApple OSS Distributions }
86*fdd8201dSApple OSS Distributions 
87*fdd8201dSApple OSS Distributions static void
recvPackets_x(int s,unsigned int numMsg,size_t buflen,socklen_t cmsgLen)88*fdd8201dSApple OSS Distributions recvPackets_x(int s, unsigned int numMsg, size_t buflen, socklen_t cmsgLen)
89*fdd8201dSApple OSS Distributions {
90*fdd8201dSApple OSS Distributions 	struct msghdr_x *msgList;
91*fdd8201dSApple OSS Distributions 	struct sockaddr_in *srcAddrs;
92*fdd8201dSApple OSS Distributions 	struct iovec *vec;
93*fdd8201dSApple OSS Distributions 	char *buffers;
94*fdd8201dSApple OSS Distributions 	char *cmsgBuf;
95*fdd8201dSApple OSS Distributions 
96*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(msgList = calloc(numMsg, sizeof(struct msghdr_x)), "msgList calloc()");
97*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(srcAddrs = calloc(numMsg, sizeof(struct sockaddr_in)), "srcAddrs calloc()");
98*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(vec = calloc(numMsg, sizeof(struct iovec)), "vec calloc()");
99*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(buffers = calloc(numMsg, buflen), "buffers calloc()");
100*fdd8201dSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(cmsgBuf = calloc(numMsg, ALIGN(cmsgLen)), "cmsgBuf calloc()");
101*fdd8201dSApple OSS Distributions 
102*fdd8201dSApple OSS Distributions 	u_int count = 0;
103*fdd8201dSApple OSS Distributions 	while (true) {
104*fdd8201dSApple OSS Distributions 		/*
105*fdd8201dSApple OSS Distributions 		 * Wrap around when we've exhausted the list
106*fdd8201dSApple OSS Distributions 		 */
107*fdd8201dSApple OSS Distributions 		if ((count % numMsg) == 0) {
108*fdd8201dSApple OSS Distributions 			for (unsigned int i = 0; i < numMsg; i++) {
109*fdd8201dSApple OSS Distributions 				struct msghdr_x *msg = &msgList[i];
110*fdd8201dSApple OSS Distributions 				msg->msg_name = &srcAddrs[i];
111*fdd8201dSApple OSS Distributions 				msg->msg_namelen = sizeof(srcAddrs[i]);
112*fdd8201dSApple OSS Distributions 				vec[i].iov_base = buffers + (i * buflen);
113*fdd8201dSApple OSS Distributions 				vec[i].iov_len = buflen;
114*fdd8201dSApple OSS Distributions 				msg->msg_iov = &vec[i];
115*fdd8201dSApple OSS Distributions 				msg->msg_iovlen = 1;
116*fdd8201dSApple OSS Distributions 				msg->msg_control = cmsgBuf + (i * ALIGN(cmsgLen));
117*fdd8201dSApple OSS Distributions 				msg->msg_controllen = cmsgLen;
118*fdd8201dSApple OSS Distributions 				msg->msg_flags = 0;
119*fdd8201dSApple OSS Distributions 
120*fdd8201dSApple OSS Distributions 				T_QUIET; T_EXPECT_TRUE((uintptr_t)msg->msg_control % sizeof(uint32_t) == 0, NULL);
121*fdd8201dSApple OSS Distributions 			}
122*fdd8201dSApple OSS Distributions 		}
123*fdd8201dSApple OSS Distributions 
124*fdd8201dSApple OSS Distributions 		ssize_t n = recvmsg_x(s, msgList + (count % numMsg), numMsg - (count % numMsg), 0);
125*fdd8201dSApple OSS Distributions 		if (n < 0) {
126*fdd8201dSApple OSS Distributions 			if (errno == EINTR) {
127*fdd8201dSApple OSS Distributions 				T_LOG("recvmsg_x(): %s", strerror(errno));
128*fdd8201dSApple OSS Distributions 				continue;
129*fdd8201dSApple OSS Distributions 			}
130*fdd8201dSApple OSS Distributions 			if (errno == EWOULDBLOCK) {
131*fdd8201dSApple OSS Distributions 				T_LOG("recvmsg_x(): %s", strerror(errno));
132*fdd8201dSApple OSS Distributions 				break;
133*fdd8201dSApple OSS Distributions 			}
134*fdd8201dSApple OSS Distributions 			T_FAIL("recvmsg_x() failed: %s", strerror(errno));
135*fdd8201dSApple OSS Distributions 		}
136*fdd8201dSApple OSS Distributions 		T_LOG("recvmsg_x returned %ld packets\n", n);
137*fdd8201dSApple OSS Distributions 
138*fdd8201dSApple OSS Distributions 		for (unsigned int i = count; i < count + (u_int)n; i++) {
139*fdd8201dSApple OSS Distributions 			struct msghdr_x *msg = &msgList[i % numMsg];
140*fdd8201dSApple OSS Distributions 
141*fdd8201dSApple OSS Distributions 			T_LOG("Received packet #%d %lu bytes with recvmsg_x(), msg_namelen = %u, msg_controllen = %d -> %d, msg_flags = 0x%x\n",
142*fdd8201dSApple OSS Distributions 			    i + 1, msg->msg_datalen, msg->msg_namelen, cmsgLen, msg->msg_controllen, msg->msg_flags);
143*fdd8201dSApple OSS Distributions 
144*fdd8201dSApple OSS Distributions 			struct cmsghdr *cmsg;
145*fdd8201dSApple OSS Distributions 
146*fdd8201dSApple OSS Distributions 			for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
147*fdd8201dSApple OSS Distributions 				T_QUIET; T_EXPECT_TRUE((uintptr_t)cmsg % sizeof(uint32_t) == 0, NULL);
148*fdd8201dSApple OSS Distributions 
149*fdd8201dSApple OSS Distributions 				T_LOG("level = %d, type = %d, length = %d\n", cmsg->cmsg_level, cmsg->cmsg_type, cmsg->cmsg_len);
150*fdd8201dSApple OSS Distributions 			}
151*fdd8201dSApple OSS Distributions 		}
152*fdd8201dSApple OSS Distributions 
153*fdd8201dSApple OSS Distributions 		count += (u_int)n;
154*fdd8201dSApple OSS Distributions 	}
155*fdd8201dSApple OSS Distributions 
156*fdd8201dSApple OSS Distributions 	free(msgList);
157*fdd8201dSApple OSS Distributions 	free(srcAddrs);
158*fdd8201dSApple OSS Distributions 	free(vec);
159*fdd8201dSApple OSS Distributions 	free(buffers);
160*fdd8201dSApple OSS Distributions 	free(cmsgBuf);
161*fdd8201dSApple OSS Distributions }
162*fdd8201dSApple OSS Distributions 
163*fdd8201dSApple OSS Distributions T_DECL(recvmsg_x_test, "exercise revcmsg_x() with various parameter")
164*fdd8201dSApple OSS Distributions {
165*fdd8201dSApple OSS Distributions 	struct sockaddr_in addr = {
166*fdd8201dSApple OSS Distributions 		.sin_len = sizeof(addr),
167*fdd8201dSApple OSS Distributions 		.sin_family = AF_INET,
168*fdd8201dSApple OSS Distributions 		.sin_addr.s_addr = htonl(0x7f000001),
169*fdd8201dSApple OSS Distributions 		.sin_port = 0
170*fdd8201dSApple OSS Distributions 	};
171*fdd8201dSApple OSS Distributions 
172*fdd8201dSApple OSS Distributions 	int recvSocket;
173*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "socket()");
174*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(bind(recvSocket, (const struct sockaddr *)&addr, sizeof(addr)), "bind()");
175*fdd8201dSApple OSS Distributions 
176*fdd8201dSApple OSS Distributions 	socklen_t addrLen = sizeof(addr);
177*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(getsockname(recvSocket, (struct sockaddr *)&addr, &addrLen), "getsockname()");
178*fdd8201dSApple OSS Distributions 
179*fdd8201dSApple OSS Distributions 	int one = 1;
180*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IP, IP_RECVPKTINFO, (void *)&one, sizeof(one)), "setsockopt(IP_RECVPKTINFO)");
181*fdd8201dSApple OSS Distributions 
182*fdd8201dSApple OSS Distributions 	int flags = fcntl(recvSocket, F_GETFL, 0);
183*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(fcntl(recvSocket, F_SETFL, flags | O_NONBLOCK), "fcntl()");
184*fdd8201dSApple OSS Distributions 
185*fdd8201dSApple OSS Distributions 	int sendSocket;
186*fdd8201dSApple OSS Distributions 	T_QUIET; T_EXPECT_POSIX_SUCCESS(sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "sendSocket socket()");
187*fdd8201dSApple OSS Distributions 
188*fdd8201dSApple OSS Distributions 	for (int dontTrunc = 0; dontTrunc <= 1; dontTrunc++) {
189*fdd8201dSApple OSS Distributions 		T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, SOL_SOCKET, SO_DONTTRUNC, (void *)&dontTrunc, sizeof(dontTrunc)), "setsockopt(SO_DONTTRUNC)");
190*fdd8201dSApple OSS Distributions 
191*fdd8201dSApple OSS Distributions 		T_LOG("\n================= recvmsg_x() test =================\n");
192*fdd8201dSApple OSS Distributions 		sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
193*fdd8201dSApple OSS Distributions 		recvPackets_x(recvSocket, NMSGS, BUFFERLEN, 50);
194*fdd8201dSApple OSS Distributions 
195*fdd8201dSApple OSS Distributions 		T_LOG("\n================= recvmsg_x() test =================\n");
196*fdd8201dSApple OSS Distributions 		sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
197*fdd8201dSApple OSS Distributions 		recvPackets_x(recvSocket, NMSGS, BUFFERLEN * 2, 50);
198*fdd8201dSApple OSS Distributions 
199*fdd8201dSApple OSS Distributions 		T_LOG("\n================= recvmsg_x() test =================\n");
200*fdd8201dSApple OSS Distributions 		sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
201*fdd8201dSApple OSS Distributions 		recvPackets_x(recvSocket, NMSGS, BUFFERLEN / 2, 50);
202*fdd8201dSApple OSS Distributions 
203*fdd8201dSApple OSS Distributions 		T_LOG("\n================= recvmsg_x() test =================\n");
204*fdd8201dSApple OSS Distributions 		sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
205*fdd8201dSApple OSS Distributions 		recvPackets_x(recvSocket, NMSGS, BUFFERLEN, 10);
206*fdd8201dSApple OSS Distributions 
207*fdd8201dSApple OSS Distributions 		T_LOG("\n================= recvmsg_x() test =================\n");
208*fdd8201dSApple OSS Distributions 		sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
209*fdd8201dSApple OSS Distributions 		recvPackets_x(recvSocket, NMSGS, BUFFERLEN / 2, 10);
210*fdd8201dSApple OSS Distributions 	}
211*fdd8201dSApple OSS Distributions 
212*fdd8201dSApple OSS Distributions 	close(sendSocket);
213*fdd8201dSApple OSS Distributions 	close(recvSocket);
214*fdd8201dSApple OSS Distributions 
215*fdd8201dSApple OSS Distributions 	T_LOG("\n================= PASS =================\n");
216*fdd8201dSApple OSS Distributions }
217