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