Harald Welte | 2aa6d02 | 2020-09-04 14:57:52 +0200 | [diff] [blame] | 1 | |
| 2 | #include <stdint.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <string.h> |
| 6 | #include <stdio.h> |
| 7 | #include <fcntl.h> |
| 8 | |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/socket.h> |
| 11 | |
| 12 | static int set_int_opt(int fd, int opt, int val) |
| 13 | { |
| 14 | return setsockopt(fd, SOL_SOCKET, opt, &val, sizeof(val)); |
| 15 | } |
| 16 | |
| 17 | static int get_int_opt(int fd, int opt) |
| 18 | { |
| 19 | int ret, rc; |
| 20 | socklen_t optlen = sizeof(ret); |
| 21 | |
| 22 | rc = getsockopt(fd, SOL_SOCKET, opt, &ret, &optlen); |
| 23 | if (rc < 0) |
| 24 | return rc; |
| 25 | return ret; |
| 26 | } |
| 27 | |
| 28 | static int set_nonblock(int fd) |
| 29 | { |
| 30 | int flags; |
| 31 | flags = fcntl(fd, F_GETFL); |
| 32 | if (flags < 0) |
| 33 | return flags; |
| 34 | flags |= O_NONBLOCK; |
| 35 | |
| 36 | return fcntl(fd, F_SETFL, flags); |
| 37 | } |
| 38 | |
| 39 | static int run_test_socketpair(int tx_buf) |
| 40 | { |
| 41 | int rc, sd[2]; |
| 42 | uint8_t buf[1024*1024]; |
| 43 | |
| 44 | memset(buf, 0, sizeof(buf)); |
| 45 | |
| 46 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sd); |
| 47 | if (rc < 0) |
| 48 | return rc; |
| 49 | |
| 50 | /* set the lowest possible transmit socket buffer */ |
| 51 | set_int_opt(sd[0], SO_SNDBUF, tx_buf); |
| 52 | set_nonblock(sd[0]); |
| 53 | |
| 54 | rc = write(sd[0], buf, sizeof(buf)); |
| 55 | printf("socketpair: tx_buf %7d: written %7d of %ld\n", tx_buf, rc, sizeof(buf)); |
| 56 | |
| 57 | close(sd[0]); |
| 58 | close(sd[1]); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static int run_test_pipe(int tx_buf) |
| 63 | { |
| 64 | int rc, sd[2]; |
| 65 | uint8_t buf[1024*1024]; |
| 66 | |
| 67 | memset(buf, 0, sizeof(buf)); |
| 68 | |
| 69 | rc = pipe(sd); |
| 70 | if (rc < 0) |
| 71 | return rc; |
| 72 | |
| 73 | set_nonblock(sd[1]); |
| 74 | |
| 75 | rc = write(sd[1], buf, sizeof(buf)); |
| 76 | printf("pipe: tx_buf %7d: written %7d of %ld\n", tx_buf, rc, sizeof(buf)); |
| 77 | |
| 78 | close(sd[0]); |
| 79 | close(sd[1]); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | int main(int argc, char **argv) |
| 87 | { |
| 88 | int i; |
| 89 | |
| 90 | for (i = 0; i < 20; i++) { |
| 91 | run_test_socketpair(1<<i); |
| 92 | } |
| 93 | run_test_pipe(0); |
| 94 | } |