1 // SPDX-License-Identifier: GPL-2.0
6 #include <bpf/libbpf.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
11 int main(int ac
, char **argv
)
13 struct sockaddr_in
*serv_addr_in
, *mapped_addr_in
, *tmp_addr_in
;
14 struct sockaddr serv_addr
, mapped_addr
, tmp_addr
;
15 int serverfd
, serverconnfd
, clientfd
, map_fd
;
16 struct bpf_link
*link
= NULL
;
17 struct bpf_program
*prog
;
18 struct bpf_object
*obj
;
19 socklen_t sockaddr_len
;
23 serv_addr_in
= (struct sockaddr_in
*)&serv_addr
;
24 mapped_addr_in
= (struct sockaddr_in
*)&mapped_addr
;
25 tmp_addr_in
= (struct sockaddr_in
*)&tmp_addr
;
27 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
28 obj
= bpf_object__open_file(filename
, NULL
);
29 if (libbpf_get_error(obj
)) {
30 fprintf(stderr
, "ERROR: opening BPF object file failed\n");
34 prog
= bpf_object__find_program_by_name(obj
, "bpf_prog1");
35 if (libbpf_get_error(prog
)) {
36 fprintf(stderr
, "ERROR: finding a prog in obj file failed\n");
40 /* load BPF program */
41 if (bpf_object__load(obj
)) {
42 fprintf(stderr
, "ERROR: loading BPF object file failed\n");
46 map_fd
= bpf_object__find_map_fd_by_name(obj
, "dnat_map");
48 fprintf(stderr
, "ERROR: finding a map in obj file failed\n");
52 link
= bpf_program__attach(prog
);
53 if (libbpf_get_error(link
)) {
54 fprintf(stderr
, "ERROR: bpf_program__attach failed\n");
59 assert((serverfd
= socket(AF_INET
, SOCK_STREAM
, 0)) > 0);
60 assert((clientfd
= socket(AF_INET
, SOCK_STREAM
, 0)) > 0);
62 /* Bind server to ephemeral port on lo */
63 memset(&serv_addr
, 0, sizeof(serv_addr
));
64 serv_addr_in
->sin_family
= AF_INET
;
65 serv_addr_in
->sin_port
= 0;
66 serv_addr_in
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
68 assert(bind(serverfd
, &serv_addr
, sizeof(serv_addr
)) == 0);
70 sockaddr_len
= sizeof(serv_addr
);
71 assert(getsockname(serverfd
, &serv_addr
, &sockaddr_len
) == 0);
72 ip
= inet_ntoa(serv_addr_in
->sin_addr
);
73 printf("Server bound to: %s:%d\n", ip
, ntohs(serv_addr_in
->sin_port
));
75 memset(&mapped_addr
, 0, sizeof(mapped_addr
));
76 mapped_addr_in
->sin_family
= AF_INET
;
77 mapped_addr_in
->sin_port
= htons(5555);
78 mapped_addr_in
->sin_addr
.s_addr
= inet_addr("255.255.255.255");
80 assert(!bpf_map_update_elem(map_fd
, &mapped_addr
, &serv_addr
, BPF_ANY
));
82 assert(listen(serverfd
, 5) == 0);
84 ip
= inet_ntoa(mapped_addr_in
->sin_addr
);
85 printf("Client connecting to: %s:%d\n",
86 ip
, ntohs(mapped_addr_in
->sin_port
));
87 assert(connect(clientfd
, &mapped_addr
, sizeof(mapped_addr
)) == 0);
89 sockaddr_len
= sizeof(tmp_addr
);
90 ip
= inet_ntoa(tmp_addr_in
->sin_addr
);
91 assert((serverconnfd
= accept(serverfd
, &tmp_addr
, &sockaddr_len
)) > 0);
92 printf("Server received connection from: %s:%d\n",
93 ip
, ntohs(tmp_addr_in
->sin_port
));
95 sockaddr_len
= sizeof(tmp_addr
);
96 assert(getpeername(clientfd
, &tmp_addr
, &sockaddr_len
) == 0);
97 ip
= inet_ntoa(tmp_addr_in
->sin_addr
);
98 printf("Client's peer address: %s:%d\n",
99 ip
, ntohs(tmp_addr_in
->sin_port
));
101 /* Is the server's getsockname = the socket getpeername */
102 assert(memcmp(&serv_addr
, &tmp_addr
, sizeof(struct sockaddr_in
)) == 0);
105 bpf_link__destroy(link
);
106 bpf_object__close(obj
);