1 // SPDX-License-Identifier: GPL-2.0
3 /* Test that sockets listening on a specific address are preferred
4 * over sockets listening on addr_any.
12 #include <linux/dccp.h>
14 #include <linux/unistd.h>
19 #include <sys/epoll.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
28 static const char *IP4_ADDR
= "127.0.0.1";
29 static const char *IP6_ADDR
= "::1";
30 static const char *IP4_MAPPED6
= "::ffff:127.0.0.1";
32 static const int PORT
= 8888;
34 static void build_rcv_fd(int family
, int proto
, int *rcv_fds
, int count
,
37 struct sockaddr_in addr4
= {0};
38 struct sockaddr_in6 addr6
= {0};
39 struct sockaddr
*addr
;
42 memset(&addr
, 0, sizeof(addr
));
46 addr4
.sin_family
= family
;
48 addr4
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
49 else if (!inet_pton(family
, addr_str
, &addr4
.sin_addr
.s_addr
))
50 error(1, errno
, "inet_pton failed: %s", addr_str
);
51 addr4
.sin_port
= htons(PORT
);
53 addr
= (struct sockaddr
*)&addr4
;
56 addr6
.sin6_family
= AF_INET6
;
58 addr6
.sin6_addr
= in6addr_any
;
59 else if (!inet_pton(family
, addr_str
, &addr6
.sin6_addr
))
60 error(1, errno
, "inet_pton failed: %s", addr_str
);
61 addr6
.sin6_port
= htons(PORT
);
63 addr
= (struct sockaddr
*)&addr6
;
66 error(1, 0, "Unsupported family %d", family
);
67 /* clang does not recognize error() above as terminating
68 * the program, so it complains that saddr, sz are
69 * not initialized when this code path is taken. Silence it.
74 for (i
= 0; i
< count
; ++i
) {
75 rcv_fds
[i
] = socket(family
, proto
, 0);
77 error(1, errno
, "failed to create receive socket");
80 if (setsockopt(rcv_fds
[i
], SOL_SOCKET
, SO_REUSEPORT
, &opt
,
82 error(1, errno
, "failed to set SO_REUSEPORT");
84 if (bind(rcv_fds
[i
], addr
, sz
))
85 error(1, errno
, "failed to bind receive socket");
87 if (proto
== SOCK_STREAM
&& listen(rcv_fds
[i
], 10))
88 error(1, errno
, "tcp: failed to listen on receive port");
89 else if (proto
== SOCK_DCCP
) {
90 if (setsockopt(rcv_fds
[i
], SOL_DCCP
,
92 &(int) {htonl(42)}, sizeof(int)))
93 error(1, errno
, "failed to setsockopt");
95 if (listen(rcv_fds
[i
], 10))
96 error(1, errno
, "dccp: failed to listen on receive port");
101 static int connect_and_send(int family
, int proto
)
103 struct sockaddr_in saddr4
= {0};
104 struct sockaddr_in daddr4
= {0};
105 struct sockaddr_in6 saddr6
= {0};
106 struct sockaddr_in6 daddr6
= {0};
107 struct sockaddr
*saddr
, *daddr
;
112 saddr4
.sin_family
= AF_INET
;
113 saddr4
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
116 daddr4
.sin_family
= AF_INET
;
117 if (!inet_pton(family
, IP4_ADDR
, &daddr4
.sin_addr
.s_addr
))
118 error(1, errno
, "inet_pton failed: %s", IP4_ADDR
);
119 daddr4
.sin_port
= htons(PORT
);
122 saddr
= (struct sockaddr
*)&saddr4
;
123 daddr
= (struct sockaddr
*)&daddr4
;
126 saddr6
.sin6_family
= AF_INET6
;
127 saddr6
.sin6_addr
= in6addr_any
;
129 daddr6
.sin6_family
= AF_INET6
;
130 if (!inet_pton(family
, IP6_ADDR
, &daddr6
.sin6_addr
))
131 error(1, errno
, "inet_pton failed: %s", IP6_ADDR
);
132 daddr6
.sin6_port
= htons(PORT
);
135 saddr
= (struct sockaddr
*)&saddr6
;
136 daddr
= (struct sockaddr
*)&daddr6
;
139 error(1, 0, "Unsupported family %d", family
);
140 /* clang does not recognize error() above as terminating
141 * the program, so it complains that saddr, daddr, sz are
142 * not initialized when this code path is taken. Silence it.
147 fd
= socket(family
, proto
, 0);
149 error(1, errno
, "failed to create send socket");
151 if (proto
== SOCK_DCCP
&&
152 setsockopt(fd
, SOL_DCCP
, DCCP_SOCKOPT_SERVICE
,
153 &(int){htonl(42)}, sizeof(int)))
154 error(1, errno
, "failed to setsockopt");
156 if (bind(fd
, saddr
, sz
))
157 error(1, errno
, "failed to bind send socket");
159 if (connect(fd
, daddr
, sz
))
160 error(1, errno
, "failed to connect send socket");
162 if (send(fd
, "a", 1, 0) < 0)
163 error(1, errno
, "failed to send message");
168 static int receive_once(int epfd
, int proto
)
170 struct epoll_event ev
;
174 i
= epoll_wait(epfd
, &ev
, 1, 3);
176 error(1, errno
, "epoll_wait failed");
178 if (proto
== SOCK_STREAM
|| proto
== SOCK_DCCP
) {
179 fd
= accept(ev
.data
.fd
, NULL
, NULL
);
181 error(1, errno
, "failed to accept");
182 i
= recv(fd
, buf
, sizeof(buf
), 0);
185 i
= recv(ev
.data
.fd
, buf
, sizeof(buf
), 0);
189 error(1, errno
, "failed to recv");
194 static void test(int *rcv_fds
, int count
, int family
, int proto
, int fd
)
196 struct epoll_event ev
;
197 int epfd
, i
, send_fd
, recv_fd
;
199 epfd
= epoll_create(1);
201 error(1, errno
, "failed to create epoll");
204 for (i
= 0; i
< count
; ++i
) {
205 ev
.data
.fd
= rcv_fds
[i
];
206 if (epoll_ctl(epfd
, EPOLL_CTL_ADD
, rcv_fds
[i
], &ev
))
207 error(1, errno
, "failed to register sock epoll");
210 send_fd
= connect_and_send(family
, proto
);
212 recv_fd
= receive_once(epfd
, proto
);
214 error(1, 0, "received on an unexpected socket");
221 static void run_one_test(int fam_send
, int fam_rcv
, int proto
,
222 const char *addr_str
)
224 /* Below we test that a socket listening on a specific address
225 * is always selected in preference over a socket listening
226 * on addr_any. Bugs where this is not the case often result
227 * in sockets created first or last to get picked. So below
228 * we make sure that there are always addr_any sockets created
229 * before and after a specific socket is created.
233 build_rcv_fd(AF_INET
, proto
, rcv_fds
, 2, NULL
);
234 build_rcv_fd(AF_INET6
, proto
, rcv_fds
+ 2, 2, NULL
);
235 build_rcv_fd(fam_rcv
, proto
, rcv_fds
+ 4, 1, addr_str
);
236 build_rcv_fd(AF_INET
, proto
, rcv_fds
+ 5, 2, NULL
);
237 build_rcv_fd(AF_INET6
, proto
, rcv_fds
+ 7, 2, NULL
);
238 test(rcv_fds
, 9, fam_send
, proto
, rcv_fds
[4]);
239 for (i
= 0; i
< 9; ++i
)
241 fprintf(stderr
, "pass\n");
244 static void test_proto(int proto
, const char *proto_str
)
246 if (proto
== SOCK_DCCP
) {
249 test_fd
= socket(AF_INET
, proto
, 0);
251 if (errno
== ESOCKTNOSUPPORT
) {
252 fprintf(stderr
, "DCCP not supported: skipping DCCP tests\n");
255 error(1, errno
, "failed to create a DCCP socket");
260 fprintf(stderr
, "%s IPv4 ... ", proto_str
);
261 run_one_test(AF_INET
, AF_INET
, proto
, IP4_ADDR
);
263 fprintf(stderr
, "%s IPv6 ... ", proto_str
);
264 run_one_test(AF_INET6
, AF_INET6
, proto
, IP6_ADDR
);
266 fprintf(stderr
, "%s IPv4 mapped to IPv6 ... ", proto_str
);
267 run_one_test(AF_INET
, AF_INET6
, proto
, IP4_MAPPED6
);
272 test_proto(SOCK_DGRAM
, "UDP");
273 test_proto(SOCK_STREAM
, "TCP");
274 test_proto(SOCK_DCCP
, "DCCP");
276 fprintf(stderr
, "SUCCESS\n");