1*1b191cb5SApple OSS Distributions /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2*1b191cb5SApple OSS Distributions
3*1b191cb5SApple OSS Distributions #include <darwintest.h>
4*1b191cb5SApple OSS Distributions #include <poll.h>
5*1b191cb5SApple OSS Distributions #include <sys/socket.h>
6*1b191cb5SApple OSS Distributions #include <unistd.h>
7*1b191cb5SApple OSS Distributions #include <netinet/in.h>
8*1b191cb5SApple OSS Distributions #include <arpa/inet.h>
9*1b191cb5SApple OSS Distributions #include <errno.h>
10*1b191cb5SApple OSS Distributions
11*1b191cb5SApple OSS Distributions static int
sockv6_open(void)12*1b191cb5SApple OSS Distributions sockv6_open(void)
13*1b191cb5SApple OSS Distributions {
14*1b191cb5SApple OSS Distributions int s;
15*1b191cb5SApple OSS Distributions
16*1b191cb5SApple OSS Distributions s = socket(AF_INET6, SOCK_DGRAM, 0);
17*1b191cb5SApple OSS Distributions T_QUIET;
18*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(s, "socket(AF_INET6, SOCK_DGRAM, 0)");
19*1b191cb5SApple OSS Distributions return s;
20*1b191cb5SApple OSS Distributions }
21*1b191cb5SApple OSS Distributions
22*1b191cb5SApple OSS Distributions static int
sockv6_bind(int s,in_port_t port)23*1b191cb5SApple OSS Distributions sockv6_bind(int s, in_port_t port)
24*1b191cb5SApple OSS Distributions {
25*1b191cb5SApple OSS Distributions struct sockaddr_in6 sin6;
26*1b191cb5SApple OSS Distributions
27*1b191cb5SApple OSS Distributions bzero(&sin6, sizeof(sin6));
28*1b191cb5SApple OSS Distributions sin6.sin6_len = sizeof(sin6);
29*1b191cb5SApple OSS Distributions sin6.sin6_family = AF_INET6;
30*1b191cb5SApple OSS Distributions sin6.sin6_port = port;
31*1b191cb5SApple OSS Distributions return bind(s, (const struct sockaddr *)&sin6, sizeof(sin6));
32*1b191cb5SApple OSS Distributions }
33*1b191cb5SApple OSS Distributions
34*1b191cb5SApple OSS Distributions static void
sockv6_set_v6only(int s)35*1b191cb5SApple OSS Distributions sockv6_set_v6only(int s)
36*1b191cb5SApple OSS Distributions {
37*1b191cb5SApple OSS Distributions int on = 1;
38*1b191cb5SApple OSS Distributions int ret;
39*1b191cb5SApple OSS Distributions
40*1b191cb5SApple OSS Distributions ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
41*1b191cb5SApple OSS Distributions T_QUIET;
42*1b191cb5SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ret, "setsockopt(%d, IPV6_ONLY)", s);
43*1b191cb5SApple OSS Distributions }
44*1b191cb5SApple OSS Distributions
45*1b191cb5SApple OSS Distributions static bool
alloc_and_bind_ports(in_port_t port_start,in_port_t port_end,int bind_attempts)46*1b191cb5SApple OSS Distributions alloc_and_bind_ports(in_port_t port_start, in_port_t port_end,
47*1b191cb5SApple OSS Distributions int bind_attempts)
48*1b191cb5SApple OSS Distributions {
49*1b191cb5SApple OSS Distributions int bound_count = 0;
50*1b191cb5SApple OSS Distributions bool success = true;
51*1b191cb5SApple OSS Distributions
52*1b191cb5SApple OSS Distributions for (in_port_t i = port_start; success && i <= port_end; i++) {
53*1b191cb5SApple OSS Distributions int s6 = -1;
54*1b191cb5SApple OSS Distributions int s6_other = -1;
55*1b191cb5SApple OSS Distributions int ret;
56*1b191cb5SApple OSS Distributions
57*1b191cb5SApple OSS Distributions s6 = sockv6_open();
58*1b191cb5SApple OSS Distributions sockv6_set_v6only(s6);
59*1b191cb5SApple OSS Distributions if (sockv6_bind(s6, i) != 0) {
60*1b191cb5SApple OSS Distributions /* find the next available port */
61*1b191cb5SApple OSS Distributions goto loop_done;
62*1b191cb5SApple OSS Distributions }
63*1b191cb5SApple OSS Distributions s6_other = sockv6_open();
64*1b191cb5SApple OSS Distributions ret = sockv6_bind(s6_other, i);
65*1b191cb5SApple OSS Distributions T_WITH_ERRNO;
66*1b191cb5SApple OSS Distributions T_QUIET;
67*1b191cb5SApple OSS Distributions T_ASSERT_TRUE(ret != 0, "socket %d bind %d", s6_other, i);
68*1b191cb5SApple OSS Distributions /*
69*1b191cb5SApple OSS Distributions * After bind fails, try binding to a different port.
70*1b191cb5SApple OSS Distributions * For non-root user, this will panic without the fix for
71*1b191cb5SApple OSS Distributions * <rdar://problem/35243417>.
72*1b191cb5SApple OSS Distributions */
73*1b191cb5SApple OSS Distributions if (sockv6_bind(s6_other, i + 1) == 0) {
74*1b191cb5SApple OSS Distributions bound_count++;
75*1b191cb5SApple OSS Distributions if (bound_count >= bind_attempts) {
76*1b191cb5SApple OSS Distributions break;
77*1b191cb5SApple OSS Distributions }
78*1b191cb5SApple OSS Distributions }
79*1b191cb5SApple OSS Distributions loop_done:
80*1b191cb5SApple OSS Distributions if (s6 >= 0) {
81*1b191cb5SApple OSS Distributions close(s6);
82*1b191cb5SApple OSS Distributions }
83*1b191cb5SApple OSS Distributions if (s6_other >= 0) {
84*1b191cb5SApple OSS Distributions close(s6_other);
85*1b191cb5SApple OSS Distributions }
86*1b191cb5SApple OSS Distributions }
87*1b191cb5SApple OSS Distributions T_ASSERT_TRUE(bound_count == bind_attempts,
88*1b191cb5SApple OSS Distributions "number of successful binds %d (out of %d)",
89*1b191cb5SApple OSS Distributions bound_count, bind_attempts);
90*1b191cb5SApple OSS Distributions return success;
91*1b191cb5SApple OSS Distributions }
92*1b191cb5SApple OSS Distributions
93*1b191cb5SApple OSS Distributions
94*1b191cb5SApple OSS Distributions T_DECL(socket_bind_35243417,
95*1b191cb5SApple OSS Distributions "bind IPv6 only UDP socket, then bind IPv6 socket.",
96*1b191cb5SApple OSS Distributions T_META_ASROOT(false),
97*1b191cb5SApple OSS Distributions T_META_CHECK_LEAKS(false))
98*1b191cb5SApple OSS Distributions {
99*1b191cb5SApple OSS Distributions alloc_and_bind_ports(1, 65534, 10);
100*1b191cb5SApple OSS Distributions }
101*1b191cb5SApple OSS Distributions
102*1b191cb5SApple OSS Distributions T_DECL(socket_bind_35243417_root,
103*1b191cb5SApple OSS Distributions "bind IPv6 only UDP socket, then bind IPv6 socket.",
104*1b191cb5SApple OSS Distributions T_META_ASROOT(true))
105*1b191cb5SApple OSS Distributions {
106*1b191cb5SApple OSS Distributions alloc_and_bind_ports(1, 65534, 10);
107*1b191cb5SApple OSS Distributions }
108