1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2021-2022 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions *
15*1031c584SApple OSS Distributions * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions *
18*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions * limitations under the License.
25*1031c584SApple OSS Distributions *
26*1031c584SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions */
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions #define __APPLE_USE_RFC_3542 1
30*1031c584SApple OSS Distributions #include <stdio.h>
31*1031c584SApple OSS Distributions #include <stdlib.h>
32*1031c584SApple OSS Distributions #include <string.h>
33*1031c584SApple OSS Distributions #include <sys/types.h>
34*1031c584SApple OSS Distributions #include <sys/errno.h>
35*1031c584SApple OSS Distributions #include <sys/socket.h>
36*1031c584SApple OSS Distributions #include <sys/sysctl.h>
37*1031c584SApple OSS Distributions #include <sys/event.h>
38*1031c584SApple OSS Distributions #include <sys/time.h>
39*1031c584SApple OSS Distributions #include <arpa/inet.h>
40*1031c584SApple OSS Distributions #include <netdb.h>
41*1031c584SApple OSS Distributions #include <semaphore.h>
42*1031c584SApple OSS Distributions #include <darwintest.h>
43*1031c584SApple OSS Distributions
44*1031c584SApple OSS Distributions #define LOGSOCKOPT(x) { \
45*1031c584SApple OSS Distributions int __retv = (x); \
46*1031c584SApple OSS Distributions if (__retv == -1) { \
47*1031c584SApple OSS Distributions T_LOG("[:(] SETSOCKOPT FAILED: %s = -1, errno = %d\n", #x, errno); \
48*1031c584SApple OSS Distributions } \
49*1031c584SApple OSS Distributions }
50*1031c584SApple OSS Distributions
51*1031c584SApple OSS Distributions #define LISTENER_PORT 9999
52*1031c584SApple OSS Distributions
53*1031c584SApple OSS Distributions void do_kqueue(int kq, int sockfd);
54*1031c584SApple OSS Distributions void handle_recv(struct msghdr *recvmsghdr, int packet_len);
55*1031c584SApple OSS Distributions
56*1031c584SApple OSS Distributions sem_t mutex;
57*1031c584SApple OSS Distributions
58*1031c584SApple OSS Distributions static void *
listener_thread(void * unused)59*1031c584SApple OSS Distributions listener_thread(void *unused)
60*1031c584SApple OSS Distributions {
61*1031c584SApple OSS Distributions int sockfd;
62*1031c584SApple OSS Distributions int clientlen;
63*1031c584SApple OSS Distributions struct sockaddr_in serveraddr;
64*1031c584SApple OSS Distributions struct sockaddr_in clientaddr;
65*1031c584SApple OSS Distributions struct hostent *hostp;
66*1031c584SApple OSS Distributions char *buf;
67*1031c584SApple OSS Distributions char *hostaddrp;
68*1031c584SApple OSS Distributions int optval;
69*1031c584SApple OSS Distributions int n;
70*1031c584SApple OSS Distributions
71*1031c584SApple OSS Distributions sockfd = socket(AF_INET, SOCK_DGRAM, 0);
72*1031c584SApple OSS Distributions if (sockfd < 0) {
73*1031c584SApple OSS Distributions T_SKIP("cannot open listener sock");
74*1031c584SApple OSS Distributions }
75*1031c584SApple OSS Distributions
76*1031c584SApple OSS Distributions bzero((char *)&serveraddr, sizeof(serveraddr));
77*1031c584SApple OSS Distributions serveraddr.sin_family = AF_INET;
78*1031c584SApple OSS Distributions serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
79*1031c584SApple OSS Distributions serveraddr.sin_port = htons((unsigned short)LISTENER_PORT);
80*1031c584SApple OSS Distributions serveraddr.sin_len = sizeof(struct sockaddr_in);
81*1031c584SApple OSS Distributions
82*1031c584SApple OSS Distributions if (bind(sockfd, (struct sockaddr *)&serveraddr,
83*1031c584SApple OSS Distributions sizeof(serveraddr)) < 0) {
84*1031c584SApple OSS Distributions T_SKIP("ERROR on binding");
85*1031c584SApple OSS Distributions }
86*1031c584SApple OSS Distributions
87*1031c584SApple OSS Distributions clientlen = sizeof(clientaddr);
88*1031c584SApple OSS Distributions buf = malloc(5);
89*1031c584SApple OSS Distributions T_LOG("listener started \n");
90*1031c584SApple OSS Distributions sem_post(&mutex);
91*1031c584SApple OSS Distributions
92*1031c584SApple OSS Distributions while (1) {
93*1031c584SApple OSS Distributions n = recvfrom(sockfd, buf, 5, 0,
94*1031c584SApple OSS Distributions (struct sockaddr *)&clientaddr, &clientlen);
95*1031c584SApple OSS Distributions if (n < 0) {
96*1031c584SApple OSS Distributions T_SKIP("ERROR in recvfrom");
97*1031c584SApple OSS Distributions }
98*1031c584SApple OSS Distributions T_LOG("received from sender: %.5s", buf);
99*1031c584SApple OSS Distributions
100*1031c584SApple OSS Distributions
101*1031c584SApple OSS Distributions hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
102*1031c584SApple OSS Distributions sizeof(clientaddr.sin_addr.s_addr),
103*1031c584SApple OSS Distributions AF_INET);
104*1031c584SApple OSS Distributions if (hostp == NULL) {
105*1031c584SApple OSS Distributions T_SKIP("ERROR on gethostbyaddr");
106*1031c584SApple OSS Distributions }
107*1031c584SApple OSS Distributions hostaddrp = inet_ntoa(clientaddr.sin_addr);
108*1031c584SApple OSS Distributions if (hostaddrp == NULL) {
109*1031c584SApple OSS Distributions T_SKIP("ERROR on inet_ntoa\n");
110*1031c584SApple OSS Distributions }
111*1031c584SApple OSS Distributions
112*1031c584SApple OSS Distributions n = sendto(sockfd, buf, n, 0,
113*1031c584SApple OSS Distributions (struct sockaddr *)&clientaddr, clientlen);
114*1031c584SApple OSS Distributions if (n < 0) {
115*1031c584SApple OSS Distributions T_SKIP("ERROR in sendto");
116*1031c584SApple OSS Distributions }
117*1031c584SApple OSS Distributions T_LOG("sent response to sender");
118*1031c584SApple OSS Distributions }
119*1031c584SApple OSS Distributions }
120*1031c584SApple OSS Distributions
121*1031c584SApple OSS Distributions T_DECL(v4mappedv6_recvpktinfo, "Test setting and using IPV6_RECVPKTINFO on a v4-mapped-v6 address socket")
122*1031c584SApple OSS Distributions {
123*1031c584SApple OSS Distributions pthread_t t;
124*1031c584SApple OSS Distributions sem_init(&mutex, 0, 1);
125*1031c584SApple OSS Distributions int error = pthread_create(&t, NULL, &listener_thread, NULL);
126*1031c584SApple OSS Distributions if (error != 0) {
127*1031c584SApple OSS Distributions T_SKIP("cannot start listener thread");
128*1031c584SApple OSS Distributions }
129*1031c584SApple OSS Distributions
130*1031c584SApple OSS Distributions sem_wait(&mutex);
131*1031c584SApple OSS Distributions
132*1031c584SApple OSS Distributions struct sockaddr_in6 local_addr = { .sin6_family = AF_INET6, .sin6_len = sizeof(struct sockaddr_in6) };
133*1031c584SApple OSS Distributions struct sockaddr_in6 remote_addr = { };
134*1031c584SApple OSS Distributions
135*1031c584SApple OSS Distributions int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
136*1031c584SApple OSS Distributions if (sockfd == -1) {
137*1031c584SApple OSS Distributions T_SKIP("Couldn't create socket. errno = %d\n", errno);
138*1031c584SApple OSS Distributions }
139*1031c584SApple OSS Distributions
140*1031c584SApple OSS Distributions int Option = 0;
141*1031c584SApple OSS Distributions LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &Option, sizeof(Option)));
142*1031c584SApple OSS Distributions
143*1031c584SApple OSS Distributions Option = 1;
144*1031c584SApple OSS Distributions LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &Option, sizeof(Option)));
145*1031c584SApple OSS Distributions
146*1031c584SApple OSS Distributions int ret = bind(sockfd, (const struct sockaddr *)&local_addr, sizeof(local_addr));
147*1031c584SApple OSS Distributions if (ret == -1) {
148*1031c584SApple OSS Distributions T_SKIP("Couldn't bind. errno = %d\n", ret);
149*1031c584SApple OSS Distributions }
150*1031c584SApple OSS Distributions
151*1031c584SApple OSS Distributions struct in_addr v4addr = {
152*1031c584SApple OSS Distributions .s_addr = htonl(INADDR_LOOPBACK)
153*1031c584SApple OSS Distributions };
154*1031c584SApple OSS Distributions
155*1031c584SApple OSS Distributions // map v4 to v6
156*1031c584SApple OSS Distributions remote_addr.sin6_family = AF_INET6;
157*1031c584SApple OSS Distributions remote_addr.sin6_port = htons(LISTENER_PORT);
158*1031c584SApple OSS Distributions remote_addr.sin6_len = sizeof(struct sockaddr_in6);
159*1031c584SApple OSS Distributions memset(&(remote_addr.sin6_addr.s6_addr[10]), 0xff, 2);
160*1031c584SApple OSS Distributions memcpy(&(remote_addr.sin6_addr.s6_addr[12]), &v4addr.s_addr, 4);
161*1031c584SApple OSS Distributions
162*1031c584SApple OSS Distributions // now remote_addr is a v4-mapped v6 address
163*1031c584SApple OSS Distributions ret = connect(sockfd, (const struct sockaddr *)&remote_addr, sizeof(remote_addr));
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions if (ret == -1) {
166*1031c584SApple OSS Distributions T_SKIP("Couldn't connect. ret = %d, errno = %d\n", ret, errno);
167*1031c584SApple OSS Distributions }
168*1031c584SApple OSS Distributions
169*1031c584SApple OSS Distributions T_LOG("Socket established. Binded & connected.\n");
170*1031c584SApple OSS Distributions
171*1031c584SApple OSS Distributions int kq = kqueue();
172*1031c584SApple OSS Distributions
173*1031c584SApple OSS Distributions if (kq == -1) {
174*1031c584SApple OSS Distributions T_SKIP("Failed to kqueue. errno = %d\n", errno);
175*1031c584SApple OSS Distributions }
176*1031c584SApple OSS Distributions
177*1031c584SApple OSS Distributions // add fd to kqueue
178*1031c584SApple OSS Distributions struct kevent evSet = { };
179*1031c584SApple OSS Distributions EV_SET(&evSet, sockfd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void *)NULL);
180*1031c584SApple OSS Distributions if (kevent(kq, &evSet, 1, NULL, 0, NULL) < 0) {
181*1031c584SApple OSS Distributions T_SKIP("kevent failed??\n");
182*1031c584SApple OSS Distributions }
183*1031c584SApple OSS Distributions
184*1031c584SApple OSS Distributions // send pkt to notify remote
185*1031c584SApple OSS Distributions sendto(sockfd, "ping", 4, 0, NULL, 0);
186*1031c584SApple OSS Distributions do_kqueue(kq, sockfd);
187*1031c584SApple OSS Distributions }
188*1031c584SApple OSS Distributions
189*1031c584SApple OSS Distributions void
do_kqueue(int kq,int sockfd)190*1031c584SApple OSS Distributions do_kqueue(int kq, int sockfd)
191*1031c584SApple OSS Distributions {
192*1031c584SApple OSS Distributions int ret = 0;
193*1031c584SApple OSS Distributions
194*1031c584SApple OSS Distributions char control_space[CMSG_SPACE(8192)] = {};
195*1031c584SApple OSS Distributions struct msghdr recvmsghdr = {};
196*1031c584SApple OSS Distributions char packet_space[1500] = {};
197*1031c584SApple OSS Distributions
198*1031c584SApple OSS Distributions struct iovec recv_iov;
199*1031c584SApple OSS Distributions recv_iov.iov_len = 1200; // just a safe buffer from the 1500 mut
200*1031c584SApple OSS Distributions recv_iov.iov_base = &packet_space;
201*1031c584SApple OSS Distributions
202*1031c584SApple OSS Distributions recvmsghdr.msg_iov = &recv_iov;
203*1031c584SApple OSS Distributions recvmsghdr.msg_iovlen = 1;
204*1031c584SApple OSS Distributions recvmsghdr.msg_control = &control_space;
205*1031c584SApple OSS Distributions recvmsghdr.msg_controllen = sizeof(control_space);
206*1031c584SApple OSS Distributions recvmsghdr.msg_flags = 0;
207*1031c584SApple OSS Distributions
208*1031c584SApple OSS Distributions struct kevent evSet;
209*1031c584SApple OSS Distributions struct kevent evList[32];
210*1031c584SApple OSS Distributions
211*1031c584SApple OSS Distributions struct timespec timeout;
212*1031c584SApple OSS Distributions timeout.tv_sec = 10;
213*1031c584SApple OSS Distributions timeout.tv_nsec = 0;
214*1031c584SApple OSS Distributions
215*1031c584SApple OSS Distributions int nev = kevent(kq, NULL, 0, evList, 32, &timeout);
216*1031c584SApple OSS Distributions
217*1031c584SApple OSS Distributions for (int i = 0; i < nev; i++) {
218*1031c584SApple OSS Distributions T_LOG("[kevent] ident = %zd, filter = %hu, flags = %hd, fflags = %d, data = %zd, udata = %p\n",
219*1031c584SApple OSS Distributions evList[i].ident, evList[i].filter, evList[i].flags, evList[i].fflags, evList[i].data, evList[i].udata);
220*1031c584SApple OSS Distributions
221*1031c584SApple OSS Distributions if (evList[i].filter == EVFILT_READ) {
222*1031c584SApple OSS Distributions ret = recvmsg(sockfd, &recvmsghdr, 0);
223*1031c584SApple OSS Distributions if (ret != -1) {
224*1031c584SApple OSS Distributions handle_recv(&recvmsghdr, ret);
225*1031c584SApple OSS Distributions } else {
226*1031c584SApple OSS Distributions T_SKIP("sender recv failed");
227*1031c584SApple OSS Distributions }
228*1031c584SApple OSS Distributions return;
229*1031c584SApple OSS Distributions }
230*1031c584SApple OSS Distributions }
231*1031c584SApple OSS Distributions
232*1031c584SApple OSS Distributions T_SKIP("timeout after 10s");
233*1031c584SApple OSS Distributions }
234*1031c584SApple OSS Distributions
235*1031c584SApple OSS Distributions void
handle_recv(struct msghdr * recvmsghdr,int packet_len)236*1031c584SApple OSS Distributions handle_recv(struct msghdr *recvmsghdr, int packet_len)
237*1031c584SApple OSS Distributions {
238*1031c584SApple OSS Distributions T_LOG("[!!] Received %d bytes...", packet_len);
239*1031c584SApple OSS Distributions
240*1031c584SApple OSS Distributions for (int i = 0; i < MIN(packet_len, 5); i++) {
241*1031c584SApple OSS Distributions T_LOG("%02x ", ((char *)recvmsghdr->msg_iov->iov_base)[i]);
242*1031c584SApple OSS Distributions }
243*1031c584SApple OSS Distributions
244*1031c584SApple OSS Distributions T_LOG("\n");
245*1031c584SApple OSS Distributions
246*1031c584SApple OSS Distributions struct cmsghdr *cmsg;
247*1031c584SApple OSS Distributions int received_cmsg_header = 0;
248*1031c584SApple OSS Distributions for (cmsg = CMSG_FIRSTHDR(recvmsghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(recvmsghdr, cmsg)) {
249*1031c584SApple OSS Distributions // this never gets hit
250*1031c584SApple OSS Distributions T_LOG("cmsg_level: %d, cmsg_type: %d\n", cmsg->cmsg_level, cmsg->cmsg_type);
251*1031c584SApple OSS Distributions received_cmsg_header = 1;
252*1031c584SApple OSS Distributions }
253*1031c584SApple OSS Distributions
254*1031c584SApple OSS Distributions T_EXPECT_TRUE(received_cmsg_header == 1, "recved cmsg hdr");
255*1031c584SApple OSS Distributions T_PASS("Recvd cmsg hdr");
256*1031c584SApple OSS Distributions }
257