~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/samples/bpf/test_probe_write_user_user.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 #include <stdio.h>
  3 #include <assert.h>
  4 #include <unistd.h>
  5 #include <bpf/bpf.h>
  6 #include <bpf/libbpf.h>
  7 #include <sys/socket.h>
  8 #include <netinet/in.h>
  9 #include <arpa/inet.h>
 10 
 11 int main(int ac, char **argv)
 12 {
 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;
 20         char filename[256];
 21         char *ip;
 22 
 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;
 26 
 27         snprintf(filename, sizeof(filename), "%s.bpf.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");
 31                 return 0;
 32         }
 33 
 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");
 37                 goto cleanup;
 38         }
 39 
 40         /* load BPF program */
 41         if (bpf_object__load(obj)) {
 42                 fprintf(stderr, "ERROR: loading BPF object file failed\n");
 43                 goto cleanup;
 44         }
 45 
 46         map_fd = bpf_object__find_map_fd_by_name(obj, "dnat_map");
 47         if (map_fd < 0) {
 48                 fprintf(stderr, "ERROR: finding a map in obj file failed\n");
 49                 goto cleanup;
 50         }
 51 
 52         link = bpf_program__attach(prog);
 53         if (libbpf_get_error(link)) {
 54                 fprintf(stderr, "ERROR: bpf_program__attach failed\n");
 55                 link = NULL;
 56                 goto cleanup;
 57         }
 58 
 59         assert((serverfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
 60         assert((clientfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
 61 
 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);
 67 
 68         assert(bind(serverfd, &serv_addr, sizeof(serv_addr)) == 0);
 69 
 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));
 74 
 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");
 79 
 80         assert(!bpf_map_update_elem(map_fd, &mapped_addr, &serv_addr, BPF_ANY));
 81 
 82         assert(listen(serverfd, 5) == 0);
 83 
 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);
 88 
 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));
 94 
 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));
100 
101         /* Is the server's getsockname = the socket getpeername */
102         assert(memcmp(&serv_addr, &tmp_addr, sizeof(struct sockaddr_in)) == 0);
103 
104 cleanup:
105         bpf_link__destroy(link);
106         bpf_object__close(obj);
107         return 0;
108 }
109 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php