psock_lib.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2013 Google Inc.
  3. * Author: Willem de Bruijn <willemb@google.com>
  4. * Daniel Borkmann <dborkman@redhat.com>
  5. *
  6. * License (GPLv2):
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #ifndef PSOCK_LIB_H
  22. #define PSOCK_LIB_H
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <string.h>
  26. #include <arpa/inet.h>
  27. #include <unistd.h>
  28. #define DATA_LEN 100
  29. #define DATA_CHAR 'a'
  30. #define DATA_CHAR_1 'b'
  31. #define PORT_BASE 8000
  32. #ifndef __maybe_unused
  33. # define __maybe_unused __attribute__ ((__unused__))
  34. #endif
  35. static __maybe_unused void sock_setfilter(int fd, int lvl, int optnum)
  36. {
  37. struct sock_filter bpf_filter[] = {
  38. { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
  39. { 0x35, 0, 4, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/
  40. { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
  41. { 0x15, 1, 0, DATA_CHAR }, /* JEQ DATA_CHAR [t goto match]*/
  42. { 0x15, 0, 1, DATA_CHAR_1}, /* JEQ DATA_CHAR_1 [t goto match]*/
  43. { 0x06, 0, 0, 0x00000060 }, /* RET match */
  44. { 0x06, 0, 0, 0x00000000 }, /* RET no match */
  45. };
  46. struct sock_fprog bpf_prog;
  47. if (lvl == SOL_PACKET && optnum == PACKET_FANOUT_DATA)
  48. bpf_filter[5].code = 0x16; /* RET A */
  49. bpf_prog.filter = bpf_filter;
  50. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  51. if (setsockopt(fd, lvl, optnum, &bpf_prog,
  52. sizeof(bpf_prog))) {
  53. perror("setsockopt SO_ATTACH_FILTER");
  54. exit(1);
  55. }
  56. }
  57. static __maybe_unused void pair_udp_setfilter(int fd)
  58. {
  59. sock_setfilter(fd, SOL_SOCKET, SO_ATTACH_FILTER);
  60. }
  61. static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
  62. {
  63. struct sockaddr_in saddr, daddr;
  64. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  65. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  66. if (fds[0] == -1 || fds[1] == -1) {
  67. fprintf(stderr, "ERROR: socket dgram\n");
  68. exit(1);
  69. }
  70. memset(&saddr, 0, sizeof(saddr));
  71. saddr.sin_family = AF_INET;
  72. saddr.sin_port = htons(port);
  73. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  74. memset(&daddr, 0, sizeof(daddr));
  75. daddr.sin_family = AF_INET;
  76. daddr.sin_port = htons(port + 1);
  77. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  78. /* must bind both to get consistent hash result */
  79. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  80. perror("bind");
  81. exit(1);
  82. }
  83. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  84. perror("bind");
  85. exit(1);
  86. }
  87. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  88. perror("connect");
  89. exit(1);
  90. }
  91. }
  92. static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
  93. {
  94. char buf[DATA_LEN], rbuf[DATA_LEN];
  95. memset(buf, payload, sizeof(buf));
  96. while (num--) {
  97. /* Should really handle EINTR and EAGAIN */
  98. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  99. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  100. exit(1);
  101. }
  102. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  103. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  104. exit(1);
  105. }
  106. if (memcmp(buf, rbuf, sizeof(buf))) {
  107. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  108. exit(1);
  109. }
  110. }
  111. }
  112. static __maybe_unused void pair_udp_send(int fds[], int num)
  113. {
  114. return pair_udp_send_char(fds, num, DATA_CHAR);
  115. }
  116. static __maybe_unused void pair_udp_close(int fds[])
  117. {
  118. close(fds[0]);
  119. close(fds[1]);
  120. }
  121. #endif /* PSOCK_LIB_H */