xref: /xnu-8020.140.41/tests/socket_v4mappedv6.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions #include <darwintest.h>
2*27b03b36SApple OSS Distributions #include <poll.h>
3*27b03b36SApple OSS Distributions #include <sys/socket.h>
4*27b03b36SApple OSS Distributions #include <unistd.h>
5*27b03b36SApple OSS Distributions #include <netinet/in.h>
6*27b03b36SApple OSS Distributions #include <arpa/inet.h>
7*27b03b36SApple OSS Distributions #include <errno.h>
8*27b03b36SApple OSS Distributions 
9*27b03b36SApple OSS Distributions static int
sockv6_open(void)10*27b03b36SApple OSS Distributions sockv6_open(void)
11*27b03b36SApple OSS Distributions {
12*27b03b36SApple OSS Distributions 	int     s;
13*27b03b36SApple OSS Distributions 
14*27b03b36SApple OSS Distributions 	s = socket(AF_INET6, SOCK_DGRAM, 0);
15*27b03b36SApple OSS Distributions 	T_QUIET;
16*27b03b36SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(s, "socket(AF_INET6, SOCK_DGRAM, 0)");
17*27b03b36SApple OSS Distributions 	return s;
18*27b03b36SApple OSS Distributions }
19*27b03b36SApple OSS Distributions 
20*27b03b36SApple OSS Distributions T_DECL(v4_mapped_v6_ops,
21*27b03b36SApple OSS Distributions     "v4 mapped v6 sock operations around bind/connect",
22*27b03b36SApple OSS Distributions     T_META_ASROOT(false),
23*27b03b36SApple OSS Distributions     T_META_CHECK_LEAKS(false))
24*27b03b36SApple OSS Distributions {
25*27b03b36SApple OSS Distributions 	int     s6 = -1;
26*27b03b36SApple OSS Distributions 	int     ret = 0;
27*27b03b36SApple OSS Distributions 	uint16_t port = 12345;
28*27b03b36SApple OSS Distributions 	struct sockaddr_in6 local = {};
29*27b03b36SApple OSS Distributions 	struct sockaddr_in6 remote = {};
30*27b03b36SApple OSS Distributions 
31*27b03b36SApple OSS Distributions 	s6 = sockv6_open();
32*27b03b36SApple OSS Distributions 
33*27b03b36SApple OSS Distributions 	local.sin6_family = AF_INET;
34*27b03b36SApple OSS Distributions 	local.sin6_len = sizeof(local);
35*27b03b36SApple OSS Distributions 	local.sin6_port = htons(port);
36*27b03b36SApple OSS Distributions 
37*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(inet_pton(AF_INET6, "::ffff:c000:201", &local.sin6_addr), 1, NULL);
38*27b03b36SApple OSS Distributions 	T_EXPECT_POSIX_FAILURE((ret = bind(s6, (const struct sockaddr *)&local, sizeof(local))), EADDRNOTAVAIL, NULL);
39*27b03b36SApple OSS Distributions 
40*27b03b36SApple OSS Distributions 	remote.sin6_family = AF_INET6;
41*27b03b36SApple OSS Distributions 	remote.sin6_len = sizeof(remote);
42*27b03b36SApple OSS Distributions 	remote.sin6_port = htons(port);
43*27b03b36SApple OSS Distributions 
44*27b03b36SApple OSS Distributions 	T_ASSERT_EQ(inet_pton(AF_INET6, "::", &remote.sin6_addr), 1, NULL);
45*27b03b36SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(connect(s6, (struct sockaddr *)&remote, sizeof(remote)), NULL);
46*27b03b36SApple OSS Distributions 	T_PASS("System didn't panic!");
47*27b03b36SApple OSS Distributions }
48