-
Notifications
You must be signed in to change notification settings - Fork 1
/
msetsockopt.c
171 lines (139 loc) · 3.51 KB
/
msetsockopt.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>
#pragma noroot
#pragma optimize 79
static LongWord timeval_to_ticks(struct timeval tv)
{
LongWord rv;
rv = 0;
if (tv.tv_usec || tv.tv_sec)
{
if (tv.tv_sec)
rv = 60 * tv.tv_sec;
// usec = 1/1,000,000
// split into 60
if (tv.tv_usec)
rv += ((tv.tv_usec + 16666) / 16667);
if (!rv) rv = 1;
}
return rv;
}
static boolean get_flag(void *p, int size, Word *flag)
{
if (size == 4)
{
*flag = *(LongWord *)flag;
return true;
}
if (size == 2)
{
*flag = *(Word *)flag;
return true;
}
if (size == 1)
{
*flag = *(Byte *)flag;
return true;
}
return false;
}
static boolean get_flag_long(void *p, int size, LongWord *flag)
{
if (size == 4)
{
*flag = *(LongWord *)flag;
return true;
}
if (size == 2)
{
*flag = *(Word *)flag;
return true;
}
if (size == 1)
{
*flag = *(Byte *)flag;
return true;
}
return false;
}
int msetsockopt(Entry *e, void *p1, void *p2, void *p3, void *p4, void *p5)
{
Word terr;
Word t;
Word flag;
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 -- linger.
// todo -- oobinline (error if 0?)
switch(optname)
{
case SO_DEBUG:
if (!get_flag(optval, optlen, &flag))
return EINVAL;
e->_DEBUG = flag ? 1 : 0;
return 0;
case SO_REUSEADDR:
if (!get_flag(optval, optlen, &flag))
return EINVAL;
e->_REUSEADDR = flag ? 1 : 0;
return 0;
case SO_REUSEPORT:
if (!get_flag(optval, optlen, &flag))
return EINVAL;
e->_REUSEPORT = flag ? 1 : 0;
return 0;
case SO_KEEPALIVE:
if (!get_flag(optval, optlen, &flag))
return EINVAL;
e->_KEEPALIVE = flag ? 1 : 0;
return 0;
case SO_OOBINLINE:
// always 1.
if (!get_flag(optval, optlen, &flag))
return EINVAL;
if (!flag)
return EINVAL;
return 0;
break;
case SO_SNDLOWAT:
// 0 is valid.
if (!get_flag_long(optval, optlen, &e->_SNDLOWAT))
return EINVAL;
return 0;
break;
case SO_RCVLOWAT:
// min size = 1.
if (!get_flag_long(optval, optlen, &e->_RCVLOWAT))
return EINVAL;
if (e->_RCVLOWAT) return 0;
e->_RCVLOWAT = 1;
return EINVAL;
break;
case SO_SNDTIMEO:
if (optlen == sizeof(struct timeval))
{
// stored as ticks aka seconds * 60.
struct timeval *tv = (struct timeval *)optval;
e->_SNDTIMEO = timeval_to_ticks(*tv);
return 0;
}
break;
case SO_RCVTIMEO:
if (optlen == sizeof(struct timeval))
{
// stored as ticks aka seconds * 60.
struct timeval *tv = (struct timeval *)optval;
e->_RCVTIMEO = timeval_to_ticks(*tv);
return 0;
}
break;
}
return EINVAL;
}