xref: /linux/tools/testing/selftests/net/sk_connect_zero_addr.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*1be9ac87SJoanne Koong // SPDX-License-Identifier: GPL-2.0
2*1be9ac87SJoanne Koong 
3*1be9ac87SJoanne Koong #include <arpa/inet.h>
4*1be9ac87SJoanne Koong #include <error.h>
5*1be9ac87SJoanne Koong #include <errno.h>
6*1be9ac87SJoanne Koong #include <unistd.h>
7*1be9ac87SJoanne Koong 
main(void)8*1be9ac87SJoanne Koong int main(void)
9*1be9ac87SJoanne Koong {
10*1be9ac87SJoanne Koong 	int fd1, fd2, one = 1;
11*1be9ac87SJoanne Koong 	struct sockaddr_in6 bind_addr = {
12*1be9ac87SJoanne Koong 		.sin6_family = AF_INET6,
13*1be9ac87SJoanne Koong 		.sin6_port = htons(20000),
14*1be9ac87SJoanne Koong 		.sin6_flowinfo = htonl(0),
15*1be9ac87SJoanne Koong 		.sin6_addr = {},
16*1be9ac87SJoanne Koong 		.sin6_scope_id = 0,
17*1be9ac87SJoanne Koong 	};
18*1be9ac87SJoanne Koong 
19*1be9ac87SJoanne Koong 	inet_pton(AF_INET6, "::", &bind_addr.sin6_addr);
20*1be9ac87SJoanne Koong 
21*1be9ac87SJoanne Koong 	fd1 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP);
22*1be9ac87SJoanne Koong 	if (fd1 < 0) {
23*1be9ac87SJoanne Koong 		error(1, errno, "socket fd1");
24*1be9ac87SJoanne Koong 		return -1;
25*1be9ac87SJoanne Koong 	}
26*1be9ac87SJoanne Koong 
27*1be9ac87SJoanne Koong 	if (setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
28*1be9ac87SJoanne Koong 		error(1, errno, "setsockopt(SO_REUSEADDR) fd1");
29*1be9ac87SJoanne Koong 		goto out_err1;
30*1be9ac87SJoanne Koong 	}
31*1be9ac87SJoanne Koong 
32*1be9ac87SJoanne Koong 	if (bind(fd1, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) {
33*1be9ac87SJoanne Koong 		error(1, errno, "bind fd1");
34*1be9ac87SJoanne Koong 		goto out_err1;
35*1be9ac87SJoanne Koong 	}
36*1be9ac87SJoanne Koong 
37*1be9ac87SJoanne Koong 	if (listen(fd1, 0)) {
38*1be9ac87SJoanne Koong 		error(1, errno, "listen");
39*1be9ac87SJoanne Koong 		goto out_err1;
40*1be9ac87SJoanne Koong 	}
41*1be9ac87SJoanne Koong 
42*1be9ac87SJoanne Koong 	fd2 = socket(AF_INET6, SOCK_STREAM, IPPROTO_IP);
43*1be9ac87SJoanne Koong 	if (fd2 < 0) {
44*1be9ac87SJoanne Koong 		error(1, errno, "socket fd2");
45*1be9ac87SJoanne Koong 		goto out_err1;
46*1be9ac87SJoanne Koong 	}
47*1be9ac87SJoanne Koong 
48*1be9ac87SJoanne Koong 	if (connect(fd2, (struct sockaddr *)&bind_addr, sizeof(bind_addr))) {
49*1be9ac87SJoanne Koong 		error(1, errno, "bind fd2");
50*1be9ac87SJoanne Koong 		goto out_err2;
51*1be9ac87SJoanne Koong 	}
52*1be9ac87SJoanne Koong 
53*1be9ac87SJoanne Koong 	close(fd2);
54*1be9ac87SJoanne Koong 	close(fd1);
55*1be9ac87SJoanne Koong 	return 0;
56*1be9ac87SJoanne Koong 
57*1be9ac87SJoanne Koong out_err2:
58*1be9ac87SJoanne Koong 	close(fd2);
59*1be9ac87SJoanne Koong out_err1:
60*1be9ac87SJoanne Koong 	close(fd1);
61*1be9ac87SJoanne Koong 	return -1;
62*1be9ac87SJoanne Koong }
63