2 * Test for the regression introduced by
4 * b9470c27607b ("inet: kill smallest_size and smallest_port")
6 * If we open an ipv4 socket on a port with reuseaddr we shouldn't reset the tb
7 * when we open the ipv6 conterpart, which is what was happening previously.
11 #include <arpa/inet.h>
12 #include <netinet/in.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
21 int open_port(int ipv6
, int any
)
28 struct sockaddr
*addr
;
29 int family
= ipv6
? AF_INET6
: AF_INET
;
31 struct sockaddr_in6 addr6
= {
32 .sin6_family
= AF_INET6
,
33 .sin6_port
= htons(PORT
),
34 .sin6_addr
= in6addr_any
36 struct sockaddr_in addr4
= {
37 .sin_family
= AF_INET
,
38 .sin_port
= htons(PORT
),
39 .sin_addr
.s_addr
= any
? htonl(INADDR_ANY
) : inet_addr("127.0.0.1"),
44 addr
= (struct sockaddr
*)&addr6
;
45 addrlen
= sizeof(addr6
);
47 addr
= (struct sockaddr
*)&addr4
;
48 addrlen
= sizeof(addr4
);
51 if ((fd
= socket(family
, SOCK_STREAM
, IPPROTO_TCP
)) < 0) {
56 if (ipv6
&& setsockopt(fd
, IPPROTO_IPV6
, IPV6_V6ONLY
, (void*)&v6only
,
57 sizeof(v6only
)) < 0) {
58 perror("setsockopt IPV6_V6ONLY");
62 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &reuseaddr
,
63 sizeof(reuseaddr
)) < 0) {
64 perror("setsockopt SO_REUSEADDR");
68 if (bind(fd
, addr
, addrlen
) < 0) {
76 if (listen(fd
, 1) < 0) {
91 fprintf(stderr
, "Opening 127.0.0.1:%d\n", PORT
);
92 listenfd
= open_port(0, 0);
94 error(1, errno
, "Couldn't open listen socket");
95 fprintf(stderr
, "Opening INADDR_ANY:%d\n", PORT
);
96 fd1
= open_port(0, 1);
98 error(1, 0, "Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket");
99 fprintf(stderr
, "Opening in6addr_any:%d\n", PORT
);
100 fd1
= open_port(1, 1);
102 error(1, errno
, "Couldn't open ipv6 reuseport");
103 fprintf(stderr
, "Opening INADDR_ANY:%d\n", PORT
);
104 fd2
= open_port(0, 1);
106 error(1, 0, "Was allowed to create an ipv4 reuseport on a already bound non-reuseport socket");
108 fprintf(stderr
, "Opening INADDR_ANY:%d after closing ipv6 socket\n", PORT
);
109 fd1
= open_port(0, 1);
111 error(1, 0, "Was allowed to create an ipv4 reuseport on an already bound non-reuseport socket with no ipv6");
112 fprintf(stderr
, "Success");