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