]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - samples/bpf/test_probe_write_user_user.c
1 // SPDX-License-Identifier: GPL-2.0
8 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
13 int main(int ac
, char **argv
)
15 int serverfd
, serverconnfd
, clientfd
;
16 socklen_t sockaddr_len
;
17 struct sockaddr serv_addr
, mapped_addr
, tmp_addr
;
18 struct sockaddr_in
*serv_addr_in
, *mapped_addr_in
, *tmp_addr_in
;
22 serv_addr_in
= (struct sockaddr_in
*)&serv_addr
;
23 mapped_addr_in
= (struct sockaddr_in
*)&mapped_addr
;
24 tmp_addr_in
= (struct sockaddr_in
*)&tmp_addr
;
26 snprintf(filename
, sizeof(filename
), "%s_kern.o", argv
[0]);
28 if (load_bpf_file(filename
)) {
29 printf("%s", bpf_log_buf
);
33 assert((serverfd
= socket(AF_INET
, SOCK_STREAM
, 0)) > 0);
34 assert((clientfd
= socket(AF_INET
, SOCK_STREAM
, 0)) > 0);
36 /* Bind server to ephemeral port on lo */
37 memset(&serv_addr
, 0, sizeof(serv_addr
));
38 serv_addr_in
->sin_family
= AF_INET
;
39 serv_addr_in
->sin_port
= 0;
40 serv_addr_in
->sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
42 assert(bind(serverfd
, &serv_addr
, sizeof(serv_addr
)) == 0);
44 sockaddr_len
= sizeof(serv_addr
);
45 assert(getsockname(serverfd
, &serv_addr
, &sockaddr_len
) == 0);
46 ip
= inet_ntoa(serv_addr_in
->sin_addr
);
47 printf("Server bound to: %s:%d\n", ip
, ntohs(serv_addr_in
->sin_port
));
49 memset(&mapped_addr
, 0, sizeof(mapped_addr
));
50 mapped_addr_in
->sin_family
= AF_INET
;
51 mapped_addr_in
->sin_port
= htons(5555);
52 mapped_addr_in
->sin_addr
.s_addr
= inet_addr("255.255.255.255");
54 assert(!bpf_map_update_elem(map_fd
[0], &mapped_addr
, &serv_addr
, BPF_ANY
));
56 assert(listen(serverfd
, 5) == 0);
58 ip
= inet_ntoa(mapped_addr_in
->sin_addr
);
59 printf("Client connecting to: %s:%d\n",
60 ip
, ntohs(mapped_addr_in
->sin_port
));
61 assert(connect(clientfd
, &mapped_addr
, sizeof(mapped_addr
)) == 0);
63 sockaddr_len
= sizeof(tmp_addr
);
64 ip
= inet_ntoa(tmp_addr_in
->sin_addr
);
65 assert((serverconnfd
= accept(serverfd
, &tmp_addr
, &sockaddr_len
)) > 0);
66 printf("Server received connection from: %s:%d\n",
67 ip
, ntohs(tmp_addr_in
->sin_port
));
69 sockaddr_len
= sizeof(tmp_addr
);
70 assert(getpeername(clientfd
, &tmp_addr
, &sockaddr_len
) == 0);
71 ip
= inet_ntoa(tmp_addr_in
->sin_addr
);
72 printf("Client's peer address: %s:%d\n",
73 ip
, ntohs(tmp_addr_in
->sin_port
));
75 /* Is the server's getsockname = the socket getpeername */
76 assert(memcmp(&serv_addr
, &tmp_addr
, sizeof(struct sockaddr_in
)) == 0);