[9] | 1 | #include <assert.h>
|
---|
| 2 | #include <errno.h>
|
---|
| 3 | #include <stdio.h>
|
---|
| 4 | #include <sys/ioctl.h>
|
---|
| 5 | #include <sys/socket.h>
|
---|
| 6 | #include <sys/types.h>
|
---|
| 7 | #include <netinet/tcp.h>
|
---|
| 8 |
|
---|
| 9 | #include <net/gen/in.h>
|
---|
| 10 | #include <net/gen/tcp.h>
|
---|
| 11 | #include <net/gen/tcp_io.h>
|
---|
| 12 | #include <net/gen/udp.h>
|
---|
| 13 | #include <net/gen/udp_io.h>
|
---|
| 14 |
|
---|
| 15 | #define DEBUG 0
|
---|
| 16 |
|
---|
| 17 | static int _tcp_setsockopt(int socket, int level, int option_name,
|
---|
| 18 | const void *option_value, socklen_t option_len);
|
---|
| 19 |
|
---|
| 20 | static int _udp_setsockopt(int socket, int level, int option_name,
|
---|
| 21 | const void *option_value, socklen_t option_len);
|
---|
| 22 |
|
---|
| 23 | int setsockopt(int socket, int level, int option_name,
|
---|
| 24 | const void *option_value, socklen_t option_len)
|
---|
| 25 | {
|
---|
| 26 | int r;
|
---|
| 27 | nwio_tcpopt_t tcpopt;
|
---|
| 28 | nwio_udpopt_t udpopt;
|
---|
| 29 |
|
---|
| 30 | r= ioctl(socket, NWIOGTCPOPT, &tcpopt);
|
---|
| 31 | if (r != -1 || errno != ENOTTY)
|
---|
| 32 | {
|
---|
| 33 | if (r == -1)
|
---|
| 34 | {
|
---|
| 35 | /* Bad file descriptor */
|
---|
| 36 | return -1;
|
---|
| 37 | }
|
---|
| 38 | return _tcp_setsockopt(socket, level, option_name,
|
---|
| 39 | option_value, option_len);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | r= ioctl(socket, NWIOGUDPOPT, &udpopt);
|
---|
| 43 | if (r != -1 || errno != ENOTTY)
|
---|
| 44 | {
|
---|
| 45 | if (r == -1)
|
---|
| 46 | {
|
---|
| 47 | /* Bad file descriptor */
|
---|
| 48 | return -1;
|
---|
| 49 | }
|
---|
| 50 | return _udp_setsockopt(socket, level, option_name,
|
---|
| 51 | option_value, option_len);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | #if DEBUG
|
---|
| 55 | fprintf(stderr, "setsockopt: not implemented for fd %d\n", socket);
|
---|
| 56 | #endif
|
---|
| 57 | errno= ENOTSOCK;
|
---|
| 58 | return -1;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | static int _tcp_setsockopt(int socket, int level, int option_name,
|
---|
| 62 | const void *option_value, socklen_t option_len)
|
---|
| 63 | {
|
---|
| 64 | int i;
|
---|
| 65 |
|
---|
| 66 | if (level == SOL_SOCKET && option_name == SO_KEEPALIVE)
|
---|
| 67 | {
|
---|
| 68 | if (option_len != sizeof(i))
|
---|
| 69 | {
|
---|
| 70 | errno= EINVAL;
|
---|
| 71 | return -1;
|
---|
| 72 | }
|
---|
| 73 | i= *(int *)option_value;
|
---|
| 74 | if (!i)
|
---|
| 75 | {
|
---|
| 76 | /* At the moment there is no way to turn off
|
---|
| 77 | * keepalives.
|
---|
| 78 | */
|
---|
| 79 | errno= ENOSYS;
|
---|
| 80 | return -1;
|
---|
| 81 | }
|
---|
| 82 | return 0;
|
---|
| 83 | }
|
---|
| 84 | if (level == SOL_SOCKET && option_name == SO_RCVBUF)
|
---|
| 85 | {
|
---|
| 86 | if (option_len != sizeof(i))
|
---|
| 87 | {
|
---|
| 88 | errno= EINVAL;
|
---|
| 89 | return -1;
|
---|
| 90 | }
|
---|
| 91 | i= *(int *)option_value;
|
---|
| 92 | if (i > 32*1024)
|
---|
| 93 | {
|
---|
| 94 | /* The receive buffer is limited to 32K at the moment.
|
---|
| 95 | */
|
---|
| 96 | errno= ENOSYS;
|
---|
| 97 | return -1;
|
---|
| 98 | }
|
---|
| 99 | /* There is no way to reduce the receive buffer, do we have to
|
---|
| 100 | * let this call fail for smaller buffers?
|
---|
| 101 | */
|
---|
| 102 | return 0;
|
---|
| 103 | }
|
---|
| 104 | if (level == SOL_SOCKET && option_name == SO_SNDBUF)
|
---|
| 105 | {
|
---|
| 106 | if (option_len != sizeof(i))
|
---|
| 107 | {
|
---|
| 108 | errno= EINVAL;
|
---|
| 109 | return -1;
|
---|
| 110 | }
|
---|
| 111 | i= *(int *)option_value;
|
---|
| 112 | if (i > 32*1024)
|
---|
| 113 | {
|
---|
| 114 | /* The send buffer is limited to 32K at the moment.
|
---|
| 115 | */
|
---|
| 116 | errno= ENOSYS;
|
---|
| 117 | return -1;
|
---|
| 118 | }
|
---|
| 119 | /* There is no way to reduce the send buffer, do we have to
|
---|
| 120 | * let this call fail for smaller buffers?
|
---|
| 121 | */
|
---|
| 122 | return 0;
|
---|
| 123 | }
|
---|
| 124 | if (level == IPPROTO_TCP && option_name == TCP_NODELAY)
|
---|
| 125 | {
|
---|
| 126 | if (option_len != sizeof(i))
|
---|
| 127 | {
|
---|
| 128 | errno= EINVAL;
|
---|
| 129 | return -1;
|
---|
| 130 | }
|
---|
| 131 | i= *(int *)option_value;
|
---|
| 132 | if (i)
|
---|
| 133 | {
|
---|
| 134 | /* At the moment there is no way to turn on
|
---|
| 135 | * nodelay.
|
---|
| 136 | */
|
---|
| 137 | errno= ENOSYS;
|
---|
| 138 | return -1;
|
---|
| 139 | }
|
---|
| 140 | return 0;
|
---|
| 141 | }
|
---|
| 142 | #if DEBUG
|
---|
| 143 | fprintf(stderr, "_tcp_setsocketopt: level %d, name %d\n",
|
---|
| 144 | level, option_name);
|
---|
| 145 | #endif
|
---|
| 146 |
|
---|
| 147 | errno= ENOSYS;
|
---|
| 148 | return -1;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | static int _udp_setsockopt(int socket, int level, int option_name,
|
---|
| 152 | const void *option_value, socklen_t option_len)
|
---|
| 153 | {
|
---|
| 154 | int i;
|
---|
| 155 |
|
---|
| 156 | #if DEBUG
|
---|
| 157 | fprintf(stderr, "_udp_setsocketopt: level %d, name %d\n",
|
---|
| 158 | level, option_name);
|
---|
| 159 | #endif
|
---|
| 160 |
|
---|
| 161 | errno= ENOSYS;
|
---|
| 162 | return -1;
|
---|
| 163 | }
|
---|
| 164 |
|
---|