1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions * Copyright (c) 2025 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 #include <sys/socket.h>
30*043036a2SApple OSS Distributions #include <sys/errno.h>
31*043036a2SApple OSS Distributions #include <sys/socketvar.h>
32*043036a2SApple OSS Distributions #include <sys/unpcb.h>
33*043036a2SApple OSS Distributions
34*043036a2SApple OSS Distributions #include <err.h>
35*043036a2SApple OSS Distributions #include <stdlib.h>
36*043036a2SApple OSS Distributions #include <stdint.h>
37*043036a2SApple OSS Distributions #include <stdio.h>
38*043036a2SApple OSS Distributions #include <string.h>
39*043036a2SApple OSS Distributions #include <sysexits.h>
40*043036a2SApple OSS Distributions #include <unistd.h>
41*043036a2SApple OSS Distributions
42*043036a2SApple OSS Distributions #include <darwintest/darwintest.h>
43*043036a2SApple OSS Distributions
44*043036a2SApple OSS Distributions T_GLOBAL_META(
45*043036a2SApple OSS Distributions T_META_NAMESPACE("xnu.net"),
46*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
47*043036a2SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
48*043036a2SApple OSS Distributions T_META_CHECK_LEAKS(false));
49*043036a2SApple OSS Distributions
50*043036a2SApple OSS Distributions static void
do_test(int type)51*043036a2SApple OSS Distributions do_test(int type)
52*043036a2SApple OSS Distributions {
53*043036a2SApple OSS Distributions ssize_t retval;
54*043036a2SApple OSS Distributions int sock[2];
55*043036a2SApple OSS Distributions
56*043036a2SApple OSS Distributions T_LOG("test socket type %d", type);
57*043036a2SApple OSS Distributions
58*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(socketpair(PF_LOCAL, type, 0, sock), "socketpair()");
59*043036a2SApple OSS Distributions
60*043036a2SApple OSS Distributions T_LOG("socketpair: [%d, %d]", sock[0], sock[1]);
61*043036a2SApple OSS Distributions
62*043036a2SApple OSS Distributions int optval = 1;
63*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_DEBUG, &optval, sizeof(optval)), "setsockopt(SO_DEBUG)");
64*043036a2SApple OSS Distributions
65*043036a2SApple OSS Distributions struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
66*043036a2SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)), "setsockopt(SO_RCVTIMEO)");
67*043036a2SApple OSS Distributions
68*043036a2SApple OSS Distributions struct iovec iov0 = { .iov_base = NULL, .iov_len = 0 };
69*043036a2SApple OSS Distributions
70*043036a2SApple OSS Distributions char cmsg_buf[CMSG_SPACE(sizeof(int))];
71*043036a2SApple OSS Distributions struct msghdr msghdr1 = {
72*043036a2SApple OSS Distributions .msg_name = NULL,
73*043036a2SApple OSS Distributions .msg_namelen = 0,
74*043036a2SApple OSS Distributions .msg_iov = &iov0,
75*043036a2SApple OSS Distributions .msg_iovlen = 1,
76*043036a2SApple OSS Distributions .msg_control = cmsg_buf,
77*043036a2SApple OSS Distributions .msg_controllen = sizeof(cmsg_buf),
78*043036a2SApple OSS Distributions .msg_flags = 0
79*043036a2SApple OSS Distributions };
80*043036a2SApple OSS Distributions
81*043036a2SApple OSS Distributions struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msghdr1);
82*043036a2SApple OSS Distributions cmsg->cmsg_len = CMSG_LEN(sizeof(int));
83*043036a2SApple OSS Distributions cmsg->cmsg_level = SOL_SOCKET;
84*043036a2SApple OSS Distributions cmsg->cmsg_type = SCM_RIGHTS;
85*043036a2SApple OSS Distributions *((int *) CMSG_DATA(cmsg)) = sock[0];
86*043036a2SApple OSS Distributions
87*043036a2SApple OSS Distributions retval = sendmsg(sock[1], &msghdr1, 0);
88*043036a2SApple OSS Distributions if (retval == -1) {
89*043036a2SApple OSS Distributions T_LOG("sendmsg(msghdr1) error: %s", strerror(errno));
90*043036a2SApple OSS Distributions } else {
91*043036a2SApple OSS Distributions T_LOG("sendmsg msghdr1 %ld", retval);
92*043036a2SApple OSS Distributions }
93*043036a2SApple OSS Distributions
94*043036a2SApple OSS Distributions struct iovec iov1 = { .iov_base = NULL, .iov_len = 0 };
95*043036a2SApple OSS Distributions struct msghdr msghdr2 = {
96*043036a2SApple OSS Distributions .msg_name = NULL,
97*043036a2SApple OSS Distributions .msg_namelen = 0,
98*043036a2SApple OSS Distributions .msg_iov = &iov1,
99*043036a2SApple OSS Distributions .msg_iovlen = 1,
100*043036a2SApple OSS Distributions .msg_control = NULL,
101*043036a2SApple OSS Distributions .msg_controllen = 0,
102*043036a2SApple OSS Distributions .msg_flags = 0
103*043036a2SApple OSS Distributions };
104*043036a2SApple OSS Distributions
105*043036a2SApple OSS Distributions retval = recvmsg(sock[0], &msghdr2, MSG_WAITALL);
106*043036a2SApple OSS Distributions if (retval == -1) {
107*043036a2SApple OSS Distributions T_LOG("recvmsg(msghdr2) error: %s", strerror(errno));
108*043036a2SApple OSS Distributions } else {
109*043036a2SApple OSS Distributions T_LOG("recvmsg msghdr2 %ld", retval);
110*043036a2SApple OSS Distributions }
111*043036a2SApple OSS Distributions
112*043036a2SApple OSS Distributions char * buf[0x10] = { 0 };
113*043036a2SApple OSS Distributions struct iovec iov2 = {
114*043036a2SApple OSS Distributions .iov_base = buf,
115*043036a2SApple OSS Distributions .iov_len = sizeof(buf),
116*043036a2SApple OSS Distributions };
117*043036a2SApple OSS Distributions
118*043036a2SApple OSS Distributions struct msghdr msghdr3 = {
119*043036a2SApple OSS Distributions .msg_name = NULL,
120*043036a2SApple OSS Distributions .msg_namelen = 0,
121*043036a2SApple OSS Distributions .msg_iov = &iov2,
122*043036a2SApple OSS Distributions .msg_iovlen = 1,
123*043036a2SApple OSS Distributions .msg_control = NULL,
124*043036a2SApple OSS Distributions .msg_controllen = 0,
125*043036a2SApple OSS Distributions .msg_flags = 0
126*043036a2SApple OSS Distributions };
127*043036a2SApple OSS Distributions
128*043036a2SApple OSS Distributions retval = recvmsg(sock[0], &msghdr3, MSG_WAITALL);
129*043036a2SApple OSS Distributions if (retval == -1) {
130*043036a2SApple OSS Distributions T_LOG("recvmsg(msghdr3) error: %s", strerror(errno));
131*043036a2SApple OSS Distributions } else {
132*043036a2SApple OSS Distributions T_LOG("recvmsg msghdr3 %ld", retval);
133*043036a2SApple OSS Distributions }
134*043036a2SApple OSS Distributions
135*043036a2SApple OSS Distributions close(sock[0]);
136*043036a2SApple OSS Distributions close(sock[1]);
137*043036a2SApple OSS Distributions
138*043036a2SApple OSS Distributions T_PASS("%s", __func__);
139*043036a2SApple OSS Distributions }
140*043036a2SApple OSS Distributions
141*043036a2SApple OSS Distributions T_DECL(send_zero_payload_dgram, "repro-124040738 SOCK_DGRAM", T_META_ASROOT(true))
142*043036a2SApple OSS Distributions {
143*043036a2SApple OSS Distributions do_test(SOCK_DGRAM);
144*043036a2SApple OSS Distributions }
145*043036a2SApple OSS Distributions
146*043036a2SApple OSS Distributions T_DECL(send_zero_payload_stream, "repro-124040738 SOCK_STREAM", T_META_ASROOT(true))
147*043036a2SApple OSS Distributions {
148*043036a2SApple OSS Distributions do_test(SOCK_STREAM);
149*043036a2SApple OSS Distributions }
150