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 #include <sys/errno.h>
33*5e3eaea3SApple OSS Distributions #include <sys/fcntl.h>
34*5e3eaea3SApple OSS Distributions #include <sys/socket.h>
35*5e3eaea3SApple OSS Distributions #include <netinet/in.h>
36*5e3eaea3SApple OSS Distributions #include <stdbool.h>
37*5e3eaea3SApple OSS Distributions #include <err.h>
38*5e3eaea3SApple OSS Distributions #include <stdio.h>
39*5e3eaea3SApple OSS Distributions #include <stdlib.h>
40*5e3eaea3SApple OSS Distributions #include <string.h>
41*5e3eaea3SApple OSS Distributions #include <sysexits.h>
42*5e3eaea3SApple OSS Distributions #include <unistd.h>
43*5e3eaea3SApple OSS Distributions
44*5e3eaea3SApple OSS Distributions #include <darwintest.h>
45*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
46*5e3eaea3SApple OSS Distributions
47*5e3eaea3SApple OSS Distributions #define NMSGS 5
48*5e3eaea3SApple OSS Distributions #define BUFFERLEN 1000
49*5e3eaea3SApple OSS Distributions
50*5e3eaea3SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net"));
51*5e3eaea3SApple OSS Distributions
52*5e3eaea3SApple OSS Distributions static void
sendPackets(int s,struct sockaddr * dst,unsigned int numMsg,size_t bufferLen)53*5e3eaea3SApple OSS Distributions sendPackets(int s, struct sockaddr *dst, unsigned int numMsg, size_t bufferLen)
54*5e3eaea3SApple OSS Distributions {
55*5e3eaea3SApple OSS Distributions ssize_t count = 0;
56*5e3eaea3SApple OSS Distributions struct msghdr msg = {};
57*5e3eaea3SApple OSS Distributions struct iovec vec = {};
58*5e3eaea3SApple OSS Distributions char *bytes = calloc(1, bufferLen);
59*5e3eaea3SApple OSS Distributions if (bytes == NULL) {
60*5e3eaea3SApple OSS Distributions err(EX_OSERR, "calloc()");
61*5e3eaea3SApple OSS Distributions }
62*5e3eaea3SApple OSS Distributions
63*5e3eaea3SApple OSS Distributions vec.iov_base = bytes;
64*5e3eaea3SApple OSS Distributions vec.iov_len = bufferLen;
65*5e3eaea3SApple OSS Distributions
66*5e3eaea3SApple OSS Distributions msg.msg_name = (void *)dst;
67*5e3eaea3SApple OSS Distributions msg.msg_namelen = dst->sa_len;
68*5e3eaea3SApple OSS Distributions msg.msg_iov = &vec;
69*5e3eaea3SApple OSS Distributions msg.msg_iovlen = 1;
70*5e3eaea3SApple OSS Distributions msg.msg_flags = 0;
71*5e3eaea3SApple OSS Distributions
72*5e3eaea3SApple OSS Distributions for (unsigned int i = 0; i < numMsg; i++) {
73*5e3eaea3SApple OSS Distributions ssize_t n;
74*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(n = sendmsg(s, &msg, 0), "sendmsg()");
75*5e3eaea3SApple OSS Distributions T_LOG("Sent %ld bytes\n", n);
76*5e3eaea3SApple OSS Distributions count += 1;
77*5e3eaea3SApple OSS Distributions }
78*5e3eaea3SApple OSS Distributions
79*5e3eaea3SApple OSS Distributions // Wait a bit to make sure the packets reach the receiver
80*5e3eaea3SApple OSS Distributions usleep(100000);
81*5e3eaea3SApple OSS Distributions
82*5e3eaea3SApple OSS Distributions T_LOG("Sent %ld packet\n", count);
83*5e3eaea3SApple OSS Distributions
84*5e3eaea3SApple OSS Distributions free(bytes);
85*5e3eaea3SApple OSS Distributions }
86*5e3eaea3SApple OSS Distributions
87*5e3eaea3SApple OSS Distributions static void
recvPackets_x(int s,unsigned int numMsg,size_t buflen,socklen_t cmsgLen)88*5e3eaea3SApple OSS Distributions recvPackets_x(int s, unsigned int numMsg, size_t buflen, socklen_t cmsgLen)
89*5e3eaea3SApple OSS Distributions {
90*5e3eaea3SApple OSS Distributions struct msghdr_x *msgList;
91*5e3eaea3SApple OSS Distributions struct sockaddr_in *srcAddrs;
92*5e3eaea3SApple OSS Distributions struct iovec *vec;
93*5e3eaea3SApple OSS Distributions char *buffers;
94*5e3eaea3SApple OSS Distributions char *cmsgBuf;
95*5e3eaea3SApple OSS Distributions
96*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(msgList = calloc(numMsg, sizeof(struct msghdr_x)), "msgList calloc()");
97*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(srcAddrs = calloc(numMsg, sizeof(struct sockaddr_in)), "srcAddrs calloc()");
98*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(vec = calloc(numMsg, sizeof(struct iovec)), "vec calloc()");
99*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(buffers = calloc(numMsg, buflen), "buffers calloc()");
100*5e3eaea3SApple OSS Distributions T_QUIET; T_ASSERT_NOTNULL(cmsgBuf = calloc(numMsg, ALIGN(cmsgLen)), "cmsgBuf calloc()");
101*5e3eaea3SApple OSS Distributions
102*5e3eaea3SApple OSS Distributions u_int count = 0;
103*5e3eaea3SApple OSS Distributions while (true) {
104*5e3eaea3SApple OSS Distributions /*
105*5e3eaea3SApple OSS Distributions * Wrap around when we've exhausted the list
106*5e3eaea3SApple OSS Distributions */
107*5e3eaea3SApple OSS Distributions if ((count % numMsg) == 0) {
108*5e3eaea3SApple OSS Distributions for (unsigned int i = 0; i < numMsg; i++) {
109*5e3eaea3SApple OSS Distributions struct msghdr_x *msg = &msgList[i];
110*5e3eaea3SApple OSS Distributions msg->msg_name = &srcAddrs[i];
111*5e3eaea3SApple OSS Distributions msg->msg_namelen = sizeof(srcAddrs[i]);
112*5e3eaea3SApple OSS Distributions vec[i].iov_base = buffers + (i * buflen);
113*5e3eaea3SApple OSS Distributions vec[i].iov_len = buflen;
114*5e3eaea3SApple OSS Distributions msg->msg_iov = &vec[i];
115*5e3eaea3SApple OSS Distributions msg->msg_iovlen = 1;
116*5e3eaea3SApple OSS Distributions msg->msg_control = cmsgBuf + (i * ALIGN(cmsgLen));
117*5e3eaea3SApple OSS Distributions msg->msg_controllen = cmsgLen;
118*5e3eaea3SApple OSS Distributions msg->msg_flags = 0;
119*5e3eaea3SApple OSS Distributions
120*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_TRUE((uintptr_t)msg->msg_control % sizeof(uint32_t) == 0, NULL);
121*5e3eaea3SApple OSS Distributions }
122*5e3eaea3SApple OSS Distributions }
123*5e3eaea3SApple OSS Distributions
124*5e3eaea3SApple OSS Distributions ssize_t n = recvmsg_x(s, msgList + (count % numMsg), numMsg - (count % numMsg), 0);
125*5e3eaea3SApple OSS Distributions if (n < 0) {
126*5e3eaea3SApple OSS Distributions if (errno == EINTR) {
127*5e3eaea3SApple OSS Distributions T_LOG("recvmsg_x(): %s", strerror(errno));
128*5e3eaea3SApple OSS Distributions continue;
129*5e3eaea3SApple OSS Distributions }
130*5e3eaea3SApple OSS Distributions if (errno == EWOULDBLOCK) {
131*5e3eaea3SApple OSS Distributions T_LOG("recvmsg_x(): %s", strerror(errno));
132*5e3eaea3SApple OSS Distributions break;
133*5e3eaea3SApple OSS Distributions }
134*5e3eaea3SApple OSS Distributions T_FAIL("recvmsg_x() failed: %s", strerror(errno));
135*5e3eaea3SApple OSS Distributions }
136*5e3eaea3SApple OSS Distributions T_LOG("recvmsg_x returned %ld packets\n", n);
137*5e3eaea3SApple OSS Distributions
138*5e3eaea3SApple OSS Distributions for (unsigned int i = count; i < count + (u_int)n; i++) {
139*5e3eaea3SApple OSS Distributions struct msghdr_x *msg = &msgList[i % numMsg];
140*5e3eaea3SApple OSS Distributions
141*5e3eaea3SApple 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*5e3eaea3SApple OSS Distributions i + 1, msg->msg_datalen, msg->msg_namelen, cmsgLen, msg->msg_controllen, msg->msg_flags);
143*5e3eaea3SApple OSS Distributions
144*5e3eaea3SApple OSS Distributions struct cmsghdr *cmsg;
145*5e3eaea3SApple OSS Distributions
146*5e3eaea3SApple OSS Distributions for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
147*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_TRUE((uintptr_t)cmsg % sizeof(uint32_t) == 0, NULL);
148*5e3eaea3SApple OSS Distributions
149*5e3eaea3SApple OSS Distributions T_LOG("level = %d, type = %d, length = %d\n", cmsg->cmsg_level, cmsg->cmsg_type, cmsg->cmsg_len);
150*5e3eaea3SApple OSS Distributions }
151*5e3eaea3SApple OSS Distributions }
152*5e3eaea3SApple OSS Distributions
153*5e3eaea3SApple OSS Distributions count += (u_int)n;
154*5e3eaea3SApple OSS Distributions }
155*5e3eaea3SApple OSS Distributions
156*5e3eaea3SApple OSS Distributions free(msgList);
157*5e3eaea3SApple OSS Distributions free(srcAddrs);
158*5e3eaea3SApple OSS Distributions free(vec);
159*5e3eaea3SApple OSS Distributions free(buffers);
160*5e3eaea3SApple OSS Distributions free(cmsgBuf);
161*5e3eaea3SApple OSS Distributions }
162*5e3eaea3SApple OSS Distributions
163*5e3eaea3SApple OSS Distributions T_DECL(recvmsg_x_test, "exercise revcmsg_x() with various parameter")
164*5e3eaea3SApple OSS Distributions {
165*5e3eaea3SApple OSS Distributions struct sockaddr_in addr = {
166*5e3eaea3SApple OSS Distributions .sin_len = sizeof(addr),
167*5e3eaea3SApple OSS Distributions .sin_family = AF_INET,
168*5e3eaea3SApple OSS Distributions .sin_addr.s_addr = htonl(0x7f000001),
169*5e3eaea3SApple OSS Distributions .sin_port = 0
170*5e3eaea3SApple OSS Distributions };
171*5e3eaea3SApple OSS Distributions
172*5e3eaea3SApple OSS Distributions int recvSocket;
173*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(recvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "socket()");
174*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(bind(recvSocket, (const struct sockaddr *)&addr, sizeof(addr)), "bind()");
175*5e3eaea3SApple OSS Distributions
176*5e3eaea3SApple OSS Distributions socklen_t addrLen = sizeof(addr);
177*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(getsockname(recvSocket, (struct sockaddr *)&addr, &addrLen), "getsockname()");
178*5e3eaea3SApple OSS Distributions
179*5e3eaea3SApple OSS Distributions int one = 1;
180*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, IPPROTO_IP, IP_RECVPKTINFO, (void *)&one, sizeof(one)), "setsockopt(IP_RECVPKTINFO)");
181*5e3eaea3SApple OSS Distributions
182*5e3eaea3SApple OSS Distributions int flags = fcntl(recvSocket, F_GETFL, 0);
183*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(fcntl(recvSocket, F_SETFL, flags | O_NONBLOCK), "fcntl()");
184*5e3eaea3SApple OSS Distributions
185*5e3eaea3SApple OSS Distributions int sendSocket;
186*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(sendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP), "sendSocket socket()");
187*5e3eaea3SApple OSS Distributions
188*5e3eaea3SApple OSS Distributions for (int dontTrunc = 0; dontTrunc <= 1; dontTrunc++) {
189*5e3eaea3SApple OSS Distributions T_QUIET; T_EXPECT_POSIX_SUCCESS(setsockopt(recvSocket, SOL_SOCKET, SO_DONTTRUNC, (void *)&dontTrunc, sizeof(dontTrunc)), "setsockopt(SO_DONTTRUNC)");
190*5e3eaea3SApple OSS Distributions
191*5e3eaea3SApple OSS Distributions T_LOG("\n================= recvmsg_x() test =================\n");
192*5e3eaea3SApple OSS Distributions sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
193*5e3eaea3SApple OSS Distributions recvPackets_x(recvSocket, NMSGS, BUFFERLEN, 50);
194*5e3eaea3SApple OSS Distributions
195*5e3eaea3SApple OSS Distributions T_LOG("\n================= recvmsg_x() test =================\n");
196*5e3eaea3SApple OSS Distributions sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
197*5e3eaea3SApple OSS Distributions recvPackets_x(recvSocket, NMSGS, BUFFERLEN * 2, 50);
198*5e3eaea3SApple OSS Distributions
199*5e3eaea3SApple OSS Distributions T_LOG("\n================= recvmsg_x() test =================\n");
200*5e3eaea3SApple OSS Distributions sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
201*5e3eaea3SApple OSS Distributions recvPackets_x(recvSocket, NMSGS, BUFFERLEN / 2, 50);
202*5e3eaea3SApple OSS Distributions
203*5e3eaea3SApple OSS Distributions T_LOG("\n================= recvmsg_x() test =================\n");
204*5e3eaea3SApple OSS Distributions sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
205*5e3eaea3SApple OSS Distributions recvPackets_x(recvSocket, NMSGS, BUFFERLEN, 10);
206*5e3eaea3SApple OSS Distributions
207*5e3eaea3SApple OSS Distributions T_LOG("\n================= recvmsg_x() test =================\n");
208*5e3eaea3SApple OSS Distributions sendPackets(sendSocket, (struct sockaddr *)&addr, NMSGS, BUFFERLEN);
209*5e3eaea3SApple OSS Distributions recvPackets_x(recvSocket, NMSGS, BUFFERLEN / 2, 10);
210*5e3eaea3SApple OSS Distributions }
211*5e3eaea3SApple OSS Distributions
212*5e3eaea3SApple OSS Distributions close(sendSocket);
213*5e3eaea3SApple OSS Distributions close(recvSocket);
214*5e3eaea3SApple OSS Distributions }
215*5e3eaea3SApple OSS Distributions
216*5e3eaea3SApple OSS Distributions T_DECL(recvmsg_x_empty_packet, "exercise revcmsg_x() with an empty packet")
217*5e3eaea3SApple OSS Distributions {
218*5e3eaea3SApple OSS Distributions const size_t bufsize = 16;
219*5e3eaea3SApple OSS Distributions int sockets[2];
220*5e3eaea3SApple OSS Distributions
221*5e3eaea3SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets), "socketpair");
222*5e3eaea3SApple OSS Distributions
223*5e3eaea3SApple OSS Distributions void *buffer = calloc(1, bufsize);
224*5e3eaea3SApple OSS Distributions
225*5e3eaea3SApple OSS Distributions write(sockets[0], buffer, bufsize);
226*5e3eaea3SApple OSS Distributions write(sockets[0], NULL, 0);
227*5e3eaea3SApple OSS Distributions write(sockets[0], buffer, bufsize);
228*5e3eaea3SApple OSS Distributions
229*5e3eaea3SApple OSS Distributions struct iovec iov1 = {
230*5e3eaea3SApple OSS Distributions .iov_base = malloc(bufsize),
231*5e3eaea3SApple OSS Distributions .iov_len = bufsize,
232*5e3eaea3SApple OSS Distributions };
233*5e3eaea3SApple OSS Distributions struct iovec iov2 = {
234*5e3eaea3SApple OSS Distributions .iov_base = malloc(bufsize),
235*5e3eaea3SApple OSS Distributions .iov_len = bufsize,
236*5e3eaea3SApple OSS Distributions };
237*5e3eaea3SApple OSS Distributions struct iovec iov3 = {
238*5e3eaea3SApple OSS Distributions .iov_base = malloc(bufsize),
239*5e3eaea3SApple OSS Distributions .iov_len = bufsize,
240*5e3eaea3SApple OSS Distributions };
241*5e3eaea3SApple OSS Distributions struct msghdr_x headers[3] = {
242*5e3eaea3SApple OSS Distributions {
243*5e3eaea3SApple OSS Distributions .msg_iov = &iov1,
244*5e3eaea3SApple OSS Distributions .msg_iovlen = 1,
245*5e3eaea3SApple OSS Distributions },
246*5e3eaea3SApple OSS Distributions {
247*5e3eaea3SApple OSS Distributions .msg_iov = &iov2,
248*5e3eaea3SApple OSS Distributions .msg_iovlen = 1,
249*5e3eaea3SApple OSS Distributions },
250*5e3eaea3SApple OSS Distributions {
251*5e3eaea3SApple OSS Distributions .msg_iov = &iov3,
252*5e3eaea3SApple OSS Distributions .msg_iovlen = 1,
253*5e3eaea3SApple OSS Distributions },
254*5e3eaea3SApple OSS Distributions };
255*5e3eaea3SApple OSS Distributions ssize_t received = recvmsg_x(sockets[1], headers, 3, 0);
256*5e3eaea3SApple OSS Distributions T_LOG("received = %zd\n", received);
257*5e3eaea3SApple OSS Distributions T_LOG("%zu %zu %zu\n", headers[0].msg_datalen, headers[1].msg_datalen, headers[2].msg_datalen);
258*5e3eaea3SApple OSS Distributions
259*5e3eaea3SApple OSS Distributions T_ASSERT_EQ(received, 3L, "received 3 packets at once");
260*5e3eaea3SApple OSS Distributions }
261