sockex2_user.c 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <linux/bpf.h>
  4. #include "libbpf.h"
  5. #include "bpf_load.h"
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. int main(int ac, char **argv)
  9. {
  10. char filename[256];
  11. FILE *f;
  12. int i, sock;
  13. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  14. if (load_bpf_file(filename)) {
  15. printf("%s", bpf_log_buf);
  16. return 1;
  17. }
  18. sock = open_raw_sock("lo");
  19. assert(setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, prog_fd,
  20. sizeof(prog_fd[0])) == 0);
  21. f = popen("ping -c5 localhost", "r");
  22. (void) f;
  23. for (i = 0; i < 5; i++) {
  24. int key = 0, next_key;
  25. long long value;
  26. while (bpf_get_next_key(map_fd[0], &key, &next_key) == 0) {
  27. bpf_lookup_elem(map_fd[0], &next_key, &value);
  28. printf("ip %s count %lld\n",
  29. inet_ntoa((struct in_addr){htonl(next_key)}),
  30. value);
  31. key = next_key;
  32. }
  33. sleep(1);
  34. }
  35. return 0;
  36. }