xref: /xnu-12377.41.6/tests/sendmsg_test.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2025 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
30*bbb1b6f9SApple OSS Distributions #include <sys/errno.h>
31*bbb1b6f9SApple OSS Distributions #include <sys/socketvar.h>
32*bbb1b6f9SApple OSS Distributions #include <sys/unpcb.h>
33*bbb1b6f9SApple OSS Distributions 
34*bbb1b6f9SApple OSS Distributions #include <err.h>
35*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
36*bbb1b6f9SApple OSS Distributions #include <stdint.h>
37*bbb1b6f9SApple OSS Distributions #include <stdio.h>
38*bbb1b6f9SApple OSS Distributions #include <string.h>
39*bbb1b6f9SApple OSS Distributions #include <sysexits.h>
40*bbb1b6f9SApple OSS Distributions #include <unistd.h>
41*bbb1b6f9SApple OSS Distributions 
42*bbb1b6f9SApple OSS Distributions #include <darwintest/darwintest.h>
43*bbb1b6f9SApple OSS Distributions 
44*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
45*bbb1b6f9SApple OSS Distributions 	T_META_NAMESPACE("xnu.net"),
46*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
47*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("networking"),
48*bbb1b6f9SApple OSS Distributions 	T_META_CHECK_LEAKS(false));
49*bbb1b6f9SApple OSS Distributions 
50*bbb1b6f9SApple OSS Distributions static void
do_test(int type)51*bbb1b6f9SApple OSS Distributions do_test(int type)
52*bbb1b6f9SApple OSS Distributions {
53*bbb1b6f9SApple OSS Distributions 	ssize_t retval;
54*bbb1b6f9SApple OSS Distributions 	int sock[2];
55*bbb1b6f9SApple OSS Distributions 
56*bbb1b6f9SApple OSS Distributions 	T_LOG("test socket type %d", type);
57*bbb1b6f9SApple OSS Distributions 
58*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(socketpair(PF_LOCAL, type, 0, sock), "socketpair()");
59*bbb1b6f9SApple OSS Distributions 
60*bbb1b6f9SApple OSS Distributions 	T_LOG("socketpair: [%d, %d]", sock[0], sock[1]);
61*bbb1b6f9SApple OSS Distributions 
62*bbb1b6f9SApple OSS Distributions 	int optval = 1;
63*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_DEBUG, &optval, sizeof(optval)), "setsockopt(SO_DEBUG)");
64*bbb1b6f9SApple OSS Distributions 
65*bbb1b6f9SApple OSS Distributions 	struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
66*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setsockopt(sock[0], SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)), "setsockopt(SO_RCVTIMEO)");
67*bbb1b6f9SApple OSS Distributions 
68*bbb1b6f9SApple OSS Distributions 	struct iovec iov0 = { .iov_base = NULL, .iov_len = 0 };
69*bbb1b6f9SApple OSS Distributions 
70*bbb1b6f9SApple OSS Distributions 	char cmsg_buf[CMSG_SPACE(sizeof(int))];
71*bbb1b6f9SApple OSS Distributions 	struct msghdr msghdr1 = {
72*bbb1b6f9SApple OSS Distributions 		.msg_name = NULL,
73*bbb1b6f9SApple OSS Distributions 		.msg_namelen = 0,
74*bbb1b6f9SApple OSS Distributions 		.msg_iov = &iov0,
75*bbb1b6f9SApple OSS Distributions 		.msg_iovlen = 1,
76*bbb1b6f9SApple OSS Distributions 		.msg_control = cmsg_buf,
77*bbb1b6f9SApple OSS Distributions 		.msg_controllen = sizeof(cmsg_buf),
78*bbb1b6f9SApple OSS Distributions 		.msg_flags = 0
79*bbb1b6f9SApple OSS Distributions 	};
80*bbb1b6f9SApple OSS Distributions 
81*bbb1b6f9SApple OSS Distributions 	struct cmsghdr * cmsg = CMSG_FIRSTHDR(&msghdr1);
82*bbb1b6f9SApple OSS Distributions 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
83*bbb1b6f9SApple OSS Distributions 	cmsg->cmsg_level = SOL_SOCKET;
84*bbb1b6f9SApple OSS Distributions 	cmsg->cmsg_type = SCM_RIGHTS;
85*bbb1b6f9SApple OSS Distributions 	*((int *) CMSG_DATA(cmsg)) = sock[0];
86*bbb1b6f9SApple OSS Distributions 
87*bbb1b6f9SApple OSS Distributions 	retval = sendmsg(sock[1], &msghdr1, 0);
88*bbb1b6f9SApple OSS Distributions 	if (retval == -1) {
89*bbb1b6f9SApple OSS Distributions 		T_LOG("sendmsg(msghdr1) error: %s", strerror(errno));
90*bbb1b6f9SApple OSS Distributions 	} else {
91*bbb1b6f9SApple OSS Distributions 		T_LOG("sendmsg msghdr1 %ld", retval);
92*bbb1b6f9SApple OSS Distributions 	}
93*bbb1b6f9SApple OSS Distributions 
94*bbb1b6f9SApple OSS Distributions 	struct iovec iov1 = { .iov_base = NULL, .iov_len = 0 };
95*bbb1b6f9SApple OSS Distributions 	struct msghdr msghdr2 = {
96*bbb1b6f9SApple OSS Distributions 		.msg_name = NULL,
97*bbb1b6f9SApple OSS Distributions 		.msg_namelen = 0,
98*bbb1b6f9SApple OSS Distributions 		.msg_iov = &iov1,
99*bbb1b6f9SApple OSS Distributions 		.msg_iovlen = 1,
100*bbb1b6f9SApple OSS Distributions 		.msg_control = NULL,
101*bbb1b6f9SApple OSS Distributions 		.msg_controllen = 0,
102*bbb1b6f9SApple OSS Distributions 		.msg_flags = 0
103*bbb1b6f9SApple OSS Distributions 	};
104*bbb1b6f9SApple OSS Distributions 
105*bbb1b6f9SApple OSS Distributions 	retval = recvmsg(sock[0], &msghdr2, MSG_WAITALL);
106*bbb1b6f9SApple OSS Distributions 	if (retval == -1) {
107*bbb1b6f9SApple OSS Distributions 		T_LOG("recvmsg(msghdr2) error: %s", strerror(errno));
108*bbb1b6f9SApple OSS Distributions 	} else {
109*bbb1b6f9SApple OSS Distributions 		T_LOG("recvmsg msghdr2 %ld", retval);
110*bbb1b6f9SApple OSS Distributions 	}
111*bbb1b6f9SApple OSS Distributions 
112*bbb1b6f9SApple OSS Distributions 	char * buf[0x10] = { 0 };
113*bbb1b6f9SApple OSS Distributions 	struct iovec iov2 = {
114*bbb1b6f9SApple OSS Distributions 		.iov_base = buf,
115*bbb1b6f9SApple OSS Distributions 		.iov_len = sizeof(buf),
116*bbb1b6f9SApple OSS Distributions 	};
117*bbb1b6f9SApple OSS Distributions 
118*bbb1b6f9SApple OSS Distributions 	struct msghdr msghdr3 = {
119*bbb1b6f9SApple OSS Distributions 		.msg_name = NULL,
120*bbb1b6f9SApple OSS Distributions 		.msg_namelen = 0,
121*bbb1b6f9SApple OSS Distributions 		.msg_iov = &iov2,
122*bbb1b6f9SApple OSS Distributions 		.msg_iovlen = 1,
123*bbb1b6f9SApple OSS Distributions 		.msg_control = NULL,
124*bbb1b6f9SApple OSS Distributions 		.msg_controllen = 0,
125*bbb1b6f9SApple OSS Distributions 		.msg_flags = 0
126*bbb1b6f9SApple OSS Distributions 	};
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions 	retval = recvmsg(sock[0], &msghdr3, MSG_WAITALL);
129*bbb1b6f9SApple OSS Distributions 	if (retval == -1) {
130*bbb1b6f9SApple OSS Distributions 		T_LOG("recvmsg(msghdr3) error: %s", strerror(errno));
131*bbb1b6f9SApple OSS Distributions 	} else {
132*bbb1b6f9SApple OSS Distributions 		T_LOG("recvmsg msghdr3 %ld", retval);
133*bbb1b6f9SApple OSS Distributions 	}
134*bbb1b6f9SApple OSS Distributions 
135*bbb1b6f9SApple OSS Distributions 	close(sock[0]);
136*bbb1b6f9SApple OSS Distributions 	close(sock[1]);
137*bbb1b6f9SApple OSS Distributions 
138*bbb1b6f9SApple OSS Distributions 	T_PASS("%s", __func__);
139*bbb1b6f9SApple OSS Distributions }
140*bbb1b6f9SApple OSS Distributions 
141*bbb1b6f9SApple OSS Distributions T_DECL(send_zero_payload_dgram, "repro-124040738 SOCK_DGRAM", T_META_ASROOT(true))
142*bbb1b6f9SApple OSS Distributions {
143*bbb1b6f9SApple OSS Distributions 	do_test(SOCK_DGRAM);
144*bbb1b6f9SApple OSS Distributions }
145*bbb1b6f9SApple OSS Distributions 
146*bbb1b6f9SApple OSS Distributions T_DECL(send_zero_payload_stream, "repro-124040738 SOCK_STREAM", T_META_ASROOT(true))
147*bbb1b6f9SApple OSS Distributions {
148*bbb1b6f9SApple OSS Distributions 	do_test(SOCK_STREAM);
149*bbb1b6f9SApple OSS Distributions }
150