1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
3 #include <darwintest.h>
5 #include <sys/socket.h>
7 #include <netinet/in.h>
10 #include <TargetConditionals.h>
17 s
= socket(AF_INET6
, SOCK_DGRAM
, 0);
19 T_ASSERT_POSIX_SUCCESS(s
, "socket(AF_INET6, SOCK_DGRAM, 0)");
24 sockv6_bind(int s
, in_port_t port
)
26 struct sockaddr_in6 sin6
;
28 bzero(&sin6
, sizeof(sin6
));
29 sin6
.sin6_len
= sizeof(sin6
);
30 sin6
.sin6_family
= AF_INET6
;
31 sin6
.sin6_port
= port
;
32 return (bind(s
, (const struct sockaddr
*)&sin6
, sizeof(sin6
)));
36 sockv6_set_v6only(int s
)
41 ret
= setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &on
, sizeof(on
));
43 T_ASSERT_POSIX_SUCCESS(ret
, "setsockopt(%d, IPV6_ONLY)", s
);
47 alloc_and_bind_ports(in_port_t port_start
, in_port_t port_end
,
53 for (in_port_t i
= port_start
; success
&& i
<= port_end
; i
++) {
59 sockv6_set_v6only(s6
);
60 if (sockv6_bind(s6
, i
) != 0) {
61 /* find the next available port */
64 s6_other
= sockv6_open();
65 ret
= sockv6_bind(s6_other
, i
);
68 T_ASSERT_TRUE(ret
!= 0, "socket %d bind %d", s6_other
, i
);
70 * After bind fails, try binding to a different port.
71 * For non-root user, this will panic without the fix for
72 * <rdar://problem/35243417>.
74 if (sockv6_bind(s6_other
, i
+ 1) == 0) {
76 if (bound_count
>= bind_attempts
) {
88 T_ASSERT_TRUE(bound_count
== bind_attempts
,
89 "number of successful binds %d (out of %d)",
90 bound_count
, bind_attempts
);
95 T_DECL(socket_bind_35243417
,
96 "bind IPv6 only UDP socket, then bind IPv6 socket.",
98 T_META_CHECK_LEAKS(false))
101 T_SKIP("socket_bind_35243417 can't run on watch.");
103 alloc_and_bind_ports(1, 65534, 10);
107 T_DECL(socket_bind_35243417_root
,
108 "bind IPv6 only UDP socket, then bind IPv6 socket.",
112 T_SKIP("socket_bind_35243417_root can't run on watch.");
114 alloc_and_bind_ports(1, 65534, 10);