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