psock_lib.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 PORT_BASE 8000
  31. #ifndef __maybe_unused
  32. # define __maybe_unused __attribute__ ((__unused__))
  33. #endif
  34. static __maybe_unused void pair_udp_setfilter(int fd)
  35. {
  36. struct sock_filter bpf_filter[] = {
  37. { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
  38. { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/
  39. { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
  40. { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  41. { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
  42. { 0x15, 0, 1, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
  43. { 0x06, 0, 0, 0x00000060 }, /* RET match */
  44. { 0x06, 0, 0, 0x00000000 }, /* RET no match */
  45. };
  46. struct sock_fprog bpf_prog;
  47. bpf_prog.filter = bpf_filter;
  48. bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  49. if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
  50. sizeof(bpf_prog))) {
  51. perror("setsockopt SO_ATTACH_FILTER");
  52. exit(1);
  53. }
  54. }
  55. static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
  56. {
  57. struct sockaddr_in saddr, daddr;
  58. fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
  59. fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
  60. if (fds[0] == -1 || fds[1] == -1) {
  61. fprintf(stderr, "ERROR: socket dgram\n");
  62. exit(1);
  63. }
  64. memset(&saddr, 0, sizeof(saddr));
  65. saddr.sin_family = AF_INET;
  66. saddr.sin_port = htons(port);
  67. saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  68. memset(&daddr, 0, sizeof(daddr));
  69. daddr.sin_family = AF_INET;
  70. daddr.sin_port = htons(port + 1);
  71. daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  72. /* must bind both to get consistent hash result */
  73. if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
  74. perror("bind");
  75. exit(1);
  76. }
  77. if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
  78. perror("bind");
  79. exit(1);
  80. }
  81. if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
  82. perror("connect");
  83. exit(1);
  84. }
  85. }
  86. static __maybe_unused void pair_udp_send(int fds[], int num)
  87. {
  88. char buf[DATA_LEN], rbuf[DATA_LEN];
  89. memset(buf, DATA_CHAR, sizeof(buf));
  90. while (num--) {
  91. /* Should really handle EINTR and EAGAIN */
  92. if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
  93. fprintf(stderr, "ERROR: send failed left=%d\n", num);
  94. exit(1);
  95. }
  96. if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
  97. fprintf(stderr, "ERROR: recv failed left=%d\n", num);
  98. exit(1);
  99. }
  100. if (memcmp(buf, rbuf, sizeof(buf))) {
  101. fprintf(stderr, "ERROR: data failed left=%d\n", num);
  102. exit(1);
  103. }
  104. }
  105. }
  106. static __maybe_unused void pair_udp_close(int fds[])
  107. {
  108. close(fds[0]);
  109. close(fds[1]);
  110. }
  111. #endif /* PSOCK_LIB_H */