root/samples/bpf/test_probe_write_user_user.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 // SPDX-License-Identifier: GPL-2.0
   2 #include <stdio.h>
   3 #include <assert.h>
   4 #include <linux/bpf.h>
   5 #include <unistd.h>
   6 #include <bpf/bpf.h>
   7 #include "bpf_load.h"
   8 #include <sys/socket.h>
   9 #include <string.h>
  10 #include <netinet/in.h>
  11 #include <arpa/inet.h>
  12 
  13 int main(int ac, char **argv)
  14 {
  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;
  19         char filename[256];
  20         char *ip;
  21 
  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;
  25 
  26         snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  27 
  28         if (load_bpf_file(filename)) {
  29                 printf("%s", bpf_log_buf);
  30                 return 1;
  31         }
  32 
  33         assert((serverfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
  34         assert((clientfd = socket(AF_INET, SOCK_STREAM, 0)) > 0);
  35 
  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);
  41 
  42         assert(bind(serverfd, &serv_addr, sizeof(serv_addr)) == 0);
  43 
  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));
  48 
  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");
  53 
  54         assert(!bpf_map_update_elem(map_fd[0], &mapped_addr, &serv_addr, BPF_ANY));
  55 
  56         assert(listen(serverfd, 5) == 0);
  57 
  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);
  62 
  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));
  68 
  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));
  74 
  75         /* Is the server's getsockname = the socket getpeername */
  76         assert(memcmp(&serv_addr, &tmp_addr, sizeof(struct sockaddr_in)) == 0);
  77 
  78         return 0;
  79 }

/* [<][>][^][v][top][bottom][index][help] */