1*94d3b452SApple OSS Distributions /* 2*94d3b452SApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved. 3*94d3b452SApple OSS Distributions * 4*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*94d3b452SApple OSS Distributions * 6*94d3b452SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*94d3b452SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*94d3b452SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*94d3b452SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*94d3b452SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*94d3b452SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*94d3b452SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*94d3b452SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*94d3b452SApple OSS Distributions * 15*94d3b452SApple OSS Distributions * Please obtain a copy of the License at 16*94d3b452SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*94d3b452SApple OSS Distributions * 18*94d3b452SApple OSS Distributions * The Original Code and all software distributed under the License are 19*94d3b452SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*94d3b452SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*94d3b452SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*94d3b452SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*94d3b452SApple OSS Distributions * Please see the License for the specific language governing rights and 24*94d3b452SApple OSS Distributions * limitations under the License. 25*94d3b452SApple OSS Distributions * 26*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*94d3b452SApple OSS Distributions */ 28*94d3b452SApple OSS Distributions 29*94d3b452SApple OSS Distributions #include <sys/errno.h> 30*94d3b452SApple OSS Distributions #include <sys/fcntl.h> 31*94d3b452SApple OSS Distributions #include <sys/socket.h> 32*94d3b452SApple OSS Distributions #include <stdio.h> 33*94d3b452SApple OSS Distributions #include <stdint.h> 34*94d3b452SApple OSS Distributions #include <string.h> 35*94d3b452SApple OSS Distributions #include <sysexits.h> 36*94d3b452SApple OSS Distributions #include <unistd.h> 37*94d3b452SApple OSS Distributions #include <darwintest.h> 38*94d3b452SApple OSS Distributions #include <darwintest_utils.h> 39*94d3b452SApple OSS Distributions 40*94d3b452SApple OSS Distributions /* -*- compile-command: "xcrun --sdk macosx.internal make -C tests sendmsg_x_test" -*- */ 41*94d3b452SApple OSS Distributions 42*94d3b452SApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.net")); 43*94d3b452SApple OSS Distributions 44*94d3b452SApple OSS Distributions #define DATAGRAM_SIZE 64 45*94d3b452SApple OSS Distributions 46*94d3b452SApple OSS Distributions T_DECL(sendmsg_x_test, "exercise sendmsg_x() return value") 47*94d3b452SApple OSS Distributions { 48*94d3b452SApple OSS Distributions int socket_fds[2]; 49*94d3b452SApple OSS Distributions 50*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(socketpair(AF_UNIX, SOCK_DGRAM, 0, socket_fds), "socketpair"); 51*94d3b452SApple OSS Distributions 52*94d3b452SApple OSS Distributions /* 53*94d3b452SApple OSS Distributions * The receive buffer size is too small for more than one datagram. 54*94d3b452SApple OSS Distributions */ 55*94d3b452SApple OSS Distributions int send_buffer_size = DATAGRAM_SIZE; 56*94d3b452SApple OSS Distributions int receive_buffer_size = DATAGRAM_SIZE + (DATAGRAM_SIZE >> 1); 57*94d3b452SApple OSS Distributions socklen_t opt_len = sizeof(int); 58*94d3b452SApple OSS Distributions 59*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[0], SOL_SOCKET, SO_SNDBUF, &send_buffer_size, opt_len), "setsockopt() SO_SNDBUF"); 60*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[0], SOL_SOCKET, SO_RCVBUF, &receive_buffer_size, opt_len), "setsockopt() SO_RCVBUF"); 61*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[1], SOL_SOCKET, SO_SNDBUF, &send_buffer_size, opt_len), "setsockopt() SO_SNDBUF"); 62*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(setsockopt(socket_fds[1], SOL_SOCKET, SO_RCVBUF, &receive_buffer_size, opt_len), "setsockopt() SO_RCVBUF"); 63*94d3b452SApple OSS Distributions 64*94d3b452SApple OSS Distributions /* 65*94d3b452SApple OSS Distributions * Send two datagram at once, only one must be sent and received 66*94d3b452SApple OSS Distributions */ 67*94d3b452SApple OSS Distributions struct msghdr_x message_headers[2] = {}; 68*94d3b452SApple OSS Distributions 69*94d3b452SApple OSS Distributions uint8_t buffer1[DATAGRAM_SIZE]; 70*94d3b452SApple OSS Distributions memset(buffer1, 0x12, DATAGRAM_SIZE); 71*94d3b452SApple OSS Distributions 72*94d3b452SApple OSS Distributions struct iovec iovec1 = { 73*94d3b452SApple OSS Distributions .iov_base = buffer1, 74*94d3b452SApple OSS Distributions .iov_len = DATAGRAM_SIZE, 75*94d3b452SApple OSS Distributions }; 76*94d3b452SApple OSS Distributions message_headers[0].msg_iov = &iovec1; 77*94d3b452SApple OSS Distributions message_headers[0].msg_iovlen = 1; 78*94d3b452SApple OSS Distributions 79*94d3b452SApple OSS Distributions uint8_t buffer2[DATAGRAM_SIZE]; 80*94d3b452SApple OSS Distributions memset(buffer2, 0x34, DATAGRAM_SIZE); 81*94d3b452SApple OSS Distributions 82*94d3b452SApple OSS Distributions struct iovec iovec2 = { 83*94d3b452SApple OSS Distributions .iov_base = buffer2, 84*94d3b452SApple OSS Distributions .iov_len = DATAGRAM_SIZE, 85*94d3b452SApple OSS Distributions }; 86*94d3b452SApple OSS Distributions 87*94d3b452SApple OSS Distributions message_headers[1].msg_iov = &iovec2; 88*94d3b452SApple OSS Distributions message_headers[1].msg_iovlen = 1; 89*94d3b452SApple OSS Distributions 90*94d3b452SApple OSS Distributions ssize_t sendmsg_x_result = sendmsg_x(socket_fds[0], message_headers, 2, 0); 91*94d3b452SApple OSS Distributions if (sendmsg_x_result < 0) { 92*94d3b452SApple OSS Distributions T_FAIL("sendmsg_x() failed: %s", strerror(errno)); 93*94d3b452SApple OSS Distributions } 94*94d3b452SApple OSS Distributions if (sendmsg_x_result != 1) { 95*94d3b452SApple OSS Distributions T_FAIL("sendmsg_x() failed: return %zd instead of 1", sendmsg_x_result); 96*94d3b452SApple OSS Distributions } 97*94d3b452SApple OSS Distributions T_PASS("sendmsg_x() result: %zd == 1\n", sendmsg_x_result); 98*94d3b452SApple OSS Distributions 99*94d3b452SApple OSS Distributions /* 100*94d3b452SApple OSS Distributions * Receive the datagram we expect 101*94d3b452SApple OSS Distributions */ 102*94d3b452SApple OSS Distributions uint8_t receive_buffer1[DATAGRAM_SIZE * 2]; 103*94d3b452SApple OSS Distributions ssize_t recv_result = recv(socket_fds[1], receive_buffer1, sizeof(receive_buffer1), 0); 104*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(recv_result, "first recv() recv_result %zd errno %d", recv_result, errno); 105*94d3b452SApple OSS Distributions 106*94d3b452SApple OSS Distributions if (recv_result != DATAGRAM_SIZE) { 107*94d3b452SApple OSS Distributions T_FAIL("recv() failed: return %zd instead of DATAGRAM_SIZE", recv_result); 108*94d3b452SApple OSS Distributions } 109*94d3b452SApple OSS Distributions T_PASS("recv() result: %zd == DATAGRAM_SIZE\n", recv_result); 110*94d3b452SApple OSS Distributions 111*94d3b452SApple OSS Distributions int i; 112*94d3b452SApple OSS Distributions for (i = 0; i < DATAGRAM_SIZE; i++) { 113*94d3b452SApple OSS Distributions if (receive_buffer1[i] != 0x12) { 114*94d3b452SApple OSS Distributions T_FAIL("receive_buffer1[%d] 0x%x == 0x12", i, receive_buffer1[i]); 115*94d3b452SApple OSS Distributions } 116*94d3b452SApple OSS Distributions } 117*94d3b452SApple OSS Distributions T_PASS("First datagram successfully received.\n"); 118*94d3b452SApple OSS Distributions 119*94d3b452SApple OSS Distributions /* 120*94d3b452SApple OSS Distributions * Set the receive socket as non-blocking and verify no more data is pending 121*94d3b452SApple OSS Distributions */ 122*94d3b452SApple OSS Distributions int flags = fcntl(socket_fds[1], F_GETFL, 0); 123*94d3b452SApple OSS Distributions T_EXPECT_POSIX_SUCCESS(fcntl(socket_fds[1], F_SETFL, flags | O_NONBLOCK), "fcntl() O_NONBLOCK"); 124*94d3b452SApple OSS Distributions 125*94d3b452SApple OSS Distributions recv_result = recv(socket_fds[1], receive_buffer1, sizeof(receive_buffer1), 0); 126*94d3b452SApple OSS Distributions T_EXPECT_POSIX_ERROR(errno, EWOULDBLOCK, "second recv() recv_result %zd errno %d", recv_result, errno); 127*94d3b452SApple OSS Distributions 128*94d3b452SApple OSS Distributions close(socket_fds[0]); 129*94d3b452SApple OSS Distributions close(socket_fds[1]); 130*94d3b452SApple OSS Distributions } 131