-
Notifications
You must be signed in to change notification settings - Fork 1
/
mgetsockopt.c
171 lines (134 loc) · 3.4 KB
/
mgetsockopt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "marignotti.h"
#include <gno/kerntool.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <intmath.h>
#pragma noroot
#pragma optimize 79
static void ticks_to_timeval(LongWord ticks, struct timeval *tv)
{
LongDivRec qr;
if (ticks == 0)
{
tv->tv_sec = 0;
tv->tv_usec = 0;
}
else
{
qr = LongDivide(ticks, 60);
tv->tv_sec = qr.quotient;
tv->tv_usec = qr.remainder * 16667;
// qr.remainder * 1,000,000 / 60
}
}
static int set_flag(void *p, int size, int flag)
{
if (size == 4)
{
*(long *)p = flag;
return 0;
}
if (size == 2)
{
*(int *)p = flag;
return 0;
}
return EINVAL;
}
static int set_flag_long(void *p, int size, long flag)
{
if (size == 4)
{
*(long *)p = flag;
return 0;
}
if (size == 2)
{
*(int *)p = flag;
return 0;
}
return EINVAL;
}
int mgetsockopt(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
{
Word terr;
Word t;
int level = *(int *)p1;
int optname = *(int *)p2;
void *optval = (void *)p3;
int optlen = p4 ? *(int *)p4 : 0;
if (level != SOL_SOCKET) return EINVAL;
if (!optval) return EINVAL;
// todo -- should set optlen = returned size.
// todo -- linger
switch (optname)
{
case SO_TYPE:
// todo... non-stream
return set_flag(optval, optlen, e->_TYPE);
break;
case SO_DEBUG:
return set_flag(optval, optlen, e->_DEBUG);
break;
case SO_REUSEADDR:
return set_flag(optval, optlen, e->_REUSEADDR);
break;
case SO_REUSEPORT:
return set_flag(optval, optlen, e->_REUSEPORT);
break;
case SO_KEEPALIVE:
return set_flag(optval, optlen, e->_KEEPALIVE);
break;
case SO_OOBINLINE:
return set_flag(optval, optlen, 1);
break;
case SO_SNDLOWAT:
return set_flag(optval, optlen, e->_SNDLOWAT);
break;
case SO_RCVLOWAT:
return set_flag(optval, optlen, e->_RCVLOWAT);
break;
case SO_SNDTIMEO:
if (optlen == sizeof(struct timeval))
{
// stored as ticks aka seconds * 60.
struct timeval *tv = (struct timeval *)optval;
ticks_to_timeval(e->_SNDTIMEO, tv);
return 0;
}
break;
case SO_RCVTIMEO:
if (optlen == sizeof(struct timeval))
{
// stored as ticks aka seconds * 60.
struct timeval *tv = (struct timeval *)optval;
ticks_to_timeval(e->_RCVTIMEO, tv);
return 0;
}
break;
#ifdef SO_NREAD
case SO_NREAD:
IncBusy();
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
e->terr = terr;
DecBusy();
return set_flag_long(optval, optlen, e->sr.srRcvQueued);
break;
#endif
#ifdef SO_NWRITE
case SO_NWRITE:
IncBusy();
terr = TCPIPStatusTCP(e->ipid, &e->sr);
t = _toolErr;
if (t) terr = t;
e->terr = terr;
DecBusy();
return set_flag_long(optval, optlen, e->sr.srSndQueued);
break;
#endif
}
return EINVAL;
}