0001
0002
0003
0004
0005
0006
0007
0008 #ifndef PSOCK_LIB_H
0009 #define PSOCK_LIB_H
0010
0011 #include <sys/types.h>
0012 #include <sys/socket.h>
0013 #include <string.h>
0014 #include <arpa/inet.h>
0015 #include <unistd.h>
0016
0017 #define DATA_LEN 100
0018 #define DATA_CHAR 'a'
0019 #define DATA_CHAR_1 'b'
0020
0021 #define PORT_BASE 8000
0022
0023 #ifndef __maybe_unused
0024 # define __maybe_unused __attribute__ ((__unused__))
0025 #endif
0026
0027 static __maybe_unused void pair_udp_setfilter(int fd)
0028 {
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 struct sock_filter bpf_filter[] = {
0051 { 0x28, 0, 0, 0x0000000c },
0052 { 0x15, 0, 8, 0x00000800 },
0053 { 0x30, 0, 0, 0x00000017 },
0054 { 0x15, 0, 6, 0x00000011 },
0055 { 0x80, 0, 0, 0000000000 },
0056 { 0x35, 0, 4, 0x00000064 },
0057 { 0x30, 0, 0, 0x00000050 },
0058 { 0x15, 1, 0, 0x00000061 },
0059 { 0x15, 0, 1, 0x00000062 },
0060 { 0x06, 0, 0, 0xffffffff },
0061 { 0x06, 0, 0, 0000000000 },
0062 };
0063 struct sock_fprog bpf_prog;
0064
0065 bpf_prog.filter = bpf_filter;
0066 bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
0067
0068 if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
0069 sizeof(bpf_prog))) {
0070 perror("setsockopt SO_ATTACH_FILTER");
0071 exit(1);
0072 }
0073 }
0074
0075 static __maybe_unused void pair_udp_open(int fds[], uint16_t port)
0076 {
0077 struct sockaddr_in saddr, daddr;
0078
0079 fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
0080 fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
0081 if (fds[0] == -1 || fds[1] == -1) {
0082 fprintf(stderr, "ERROR: socket dgram\n");
0083 exit(1);
0084 }
0085
0086 memset(&saddr, 0, sizeof(saddr));
0087 saddr.sin_family = AF_INET;
0088 saddr.sin_port = htons(port);
0089 saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
0090
0091 memset(&daddr, 0, sizeof(daddr));
0092 daddr.sin_family = AF_INET;
0093 daddr.sin_port = htons(port + 1);
0094 daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
0095
0096
0097 if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
0098 perror("bind");
0099 exit(1);
0100 }
0101 if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
0102 perror("bind");
0103 exit(1);
0104 }
0105 if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
0106 perror("connect");
0107 exit(1);
0108 }
0109 }
0110
0111 static __maybe_unused void pair_udp_send_char(int fds[], int num, char payload)
0112 {
0113 char buf[DATA_LEN], rbuf[DATA_LEN];
0114
0115 memset(buf, payload, sizeof(buf));
0116 while (num--) {
0117
0118 if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
0119 fprintf(stderr, "ERROR: send failed left=%d\n", num);
0120 exit(1);
0121 }
0122 if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
0123 fprintf(stderr, "ERROR: recv failed left=%d\n", num);
0124 exit(1);
0125 }
0126 if (memcmp(buf, rbuf, sizeof(buf))) {
0127 fprintf(stderr, "ERROR: data failed left=%d\n", num);
0128 exit(1);
0129 }
0130 }
0131 }
0132
0133 static __maybe_unused void pair_udp_send(int fds[], int num)
0134 {
0135 return pair_udp_send_char(fds, num, DATA_CHAR);
0136 }
0137
0138 static __maybe_unused void pair_udp_close(int fds[])
0139 {
0140 close(fds[0]);
0141 close(fds[1]);
0142 }
0143
0144 #endif