1 | #include <assert.h>
|
---|
2 | #include <errno.h>
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <string.h>
|
---|
5 | #include <sys/ioctl.h>
|
---|
6 | #include <sys/socket.h>
|
---|
7 | #include <sys/types.h>
|
---|
8 | #include <netinet/tcp.h>
|
---|
9 |
|
---|
10 | #include <net/gen/in.h>
|
---|
11 | #include <net/gen/tcp.h>
|
---|
12 | #include <net/gen/tcp_io.h>
|
---|
13 | #include <net/gen/udp.h>
|
---|
14 | #include <net/gen/udp_io.h>
|
---|
15 |
|
---|
16 | #define DEBUG 0
|
---|
17 |
|
---|
18 | static int _tcp_getsockopt(int socket, int level, int option_name,
|
---|
19 | void *_RESTRICT option_value, socklen_t *_RESTRICT option_len);
|
---|
20 | static int _udp_getsockopt(int socket, int level, int option_name,
|
---|
21 | void *_RESTRICT option_value, socklen_t *_RESTRICT option_len);
|
---|
22 |
|
---|
23 | int getsockopt(int socket, int level, int option_name,
|
---|
24 | void *_RESTRICT option_value, socklen_t *_RESTRICT 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_getsockopt(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_getsockopt(socket, level, option_name,
|
---|
51 | option_value, option_len);
|
---|
52 | }
|
---|
53 |
|
---|
54 | #if DEBUG
|
---|
55 | fprintf(stderr, "getsockopt: not implemented for fd %d\n", socket);
|
---|
56 | #endif
|
---|
57 | errno= ENOTSOCK;
|
---|
58 | return -1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | static int _tcp_getsockopt(int socket, int level, int option_name,
|
---|
62 | void *_RESTRICT option_value, socklen_t *_RESTRICT option_len)
|
---|
63 | {
|
---|
64 | int i, r, err;
|
---|
65 |
|
---|
66 | if (level == SOL_SOCKET && option_name == SO_KEEPALIVE)
|
---|
67 | {
|
---|
68 | i= 1; /* Keepalive is always on */
|
---|
69 | if (*option_len < sizeof(i))
|
---|
70 | memcpy(option_value, &i, *option_len);
|
---|
71 | else
|
---|
72 | memcpy(option_value, &i, sizeof(i));
|
---|
73 | *option_len= sizeof(i);
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 | if (level == SOL_SOCKET && option_name == SO_ERROR)
|
---|
77 | {
|
---|
78 | r= ioctl(socket, NWIOTCPGERROR, &err);
|
---|
79 | if (r != 0)
|
---|
80 | return r;
|
---|
81 | if (*option_len < sizeof(err))
|
---|
82 | memcpy(option_value, &err, *option_len);
|
---|
83 | else
|
---|
84 | memcpy(option_value, &err, sizeof(err));
|
---|
85 | *option_len= sizeof(err);
|
---|
86 | return 0;
|
---|
87 | }
|
---|
88 | if (level == SOL_SOCKET && option_name == SO_RCVBUF)
|
---|
89 | {
|
---|
90 | i= 32*1024; /* Receive buffer in the current
|
---|
91 | * implementation
|
---|
92 | */
|
---|
93 | if (*option_len < sizeof(i))
|
---|
94 | memcpy(option_value, &i, *option_len);
|
---|
95 | else
|
---|
96 | memcpy(option_value, &i, sizeof(i));
|
---|
97 | *option_len= sizeof(i);
|
---|
98 | return 0;
|
---|
99 | }
|
---|
100 | if (level == SOL_SOCKET && option_name == SO_SNDBUF)
|
---|
101 | {
|
---|
102 | i= 32*1024; /* Send buffer in the current implementation */
|
---|
103 | if (*option_len < sizeof(i))
|
---|
104 | memcpy(option_value, &i, *option_len);
|
---|
105 | else
|
---|
106 | memcpy(option_value, &i, sizeof(i));
|
---|
107 | *option_len= sizeof(i);
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 | if (level == IPPROTO_TCP && option_name == TCP_NODELAY)
|
---|
111 | {
|
---|
112 | i= 0; /* nodelay is always off */
|
---|
113 | if (*option_len < sizeof(i))
|
---|
114 | memcpy(option_value, &i, *option_len);
|
---|
115 | else
|
---|
116 | memcpy(option_value, &i, sizeof(i));
|
---|
117 | *option_len= sizeof(i);
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 | #if DEBUG
|
---|
121 | fprintf(stderr, "_tcp_getsocketopt: level %d, name %d\n",
|
---|
122 | level, option_name);
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | errno= ENOPROTOOPT;
|
---|
126 | return -1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | static int _udp_getsockopt(int socket, int level, int option_name,
|
---|
130 | void *_RESTRICT option_value, socklen_t *_RESTRICT option_len)
|
---|
131 | {
|
---|
132 | int i;
|
---|
133 |
|
---|
134 | #if DEBUG
|
---|
135 | fprintf(stderr, "_udp_getsocketopt: level %d, name %d\n",
|
---|
136 | level, option_name);
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | errno= ENOSYS;
|
---|
140 | return -1;
|
---|
141 | }
|
---|