forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock.c
431 lines (400 loc) · 10.2 KB
/
sock.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
#include "net.h"
#include "ip.h"
#include "ip6.h"
#include "udp.h"
#include "tcp.h"
#include "sock.h"
static struct sock socks[128];
int
sockaddr_pton(unsigned short family, const char *p, struct sockaddr *n, size_t size)
{
struct ip_endpoint ep;
switch (family) {
case AF_INET:
if (ip_endpoint_pton(AF_INET, p, &ep) == 0) {
if (size < sizeof(struct sockaddr_in)) {
return -1;
}
((struct sockaddr_in *)n)->sin_family = AF_INET;
((struct sockaddr_in *)n)->sin_port = ep.port;
((struct sockaddr_in *)n)->sin_addr = ep.addr.s_addr4;
}
break;
case AF_INET6:
if (ip_endpoint_pton(AF_INET6, p, &ep) == 0) {
if (size < sizeof(struct sockaddr_in6)) {
return -1;
}
((struct sockaddr_in6 *)n)->sin6_family = AF_INET6;
((struct sockaddr_in6 *)n)->sin6_port = ep.port;
((struct sockaddr_in6 *)n)->sin6_addr = ep.addr.s_addr6;
}
break;
default:
errorf("not supported address family: %d", family);
return -1;
}
return 0;
}
char *
sockaddr_ntop(const struct sockaddr *n, char *p, size_t size)
{
struct ip_endpoint ep;
switch (n->sa_family) {
case AF_INET:
if (size < IP_ENDPOINT_STR_LEN) {
return NULL;
}
ep.port = ((struct sockaddr_in *)n)->sin_port;
ep.addr.family = AF_INET;
ep.addr.s_addr4 = ((struct sockaddr_in *)n)->sin_addr;
return ip_endpoint_ntop(&ep, p, size);
case AF_INET6:
if (size < IPV6_ENDPOINT_STR_LEN) {
return NULL;
}
ep.port = ((struct sockaddr_in6 *)n)->sin6_port;
ep.addr.family = AF_INET6;
ep.addr.s_addr6 = ((struct sockaddr_in6 *)n)->sin6_addr;
return ip_endpoint_ntop(&ep, p, size);
default:
errorf("not supported address family: %d", n->sa_family);
return NULL;
}
}
static struct sock *
sock_alloc(void)
{
struct sock *entry;
for (entry = socks; entry < tailof(socks); entry++) {
if (!entry->used) {
entry->used = 1;
return entry;
}
}
return NULL;
}
static int
sock_free(struct sock *s)
{
memset(s, 0, sizeof(*s));
return 0;
}
static struct sock *
sock_get(int id)
{
if (id < 0 || id >= (int)countof(socks)) {
/* out of range */
return NULL;
}
return &socks[id];
}
int
sock_open(int domain, int type, int protocol)
{
struct sock *s;
if (domain != AF_INET && domain != AF_INET6) {
return -1;
}
if (type != SOCK_STREAM && type != SOCK_DGRAM) {
return -1;
}
if (protocol != 0) {
return -1;
}
s = sock_alloc();
if (!s) {
return -1;
}
s->family = domain;
s->type = type;
switch (s->type) {
case SOCK_STREAM:
s->desc = tcp_open();
break;
case SOCK_DGRAM:
s->desc = udp_open();
break;
}
if (s->desc == -1) {
return -1;
}
return indexof(socks, s);
}
int
sock_close(int id)
{
struct sock *s;
s = sock_get(id);
if (!s) {
return -1;
}
switch (s->type) {
case SOCK_STREAM:
tcp_close(s->desc);
break;
case SOCK_DGRAM:
udp_close(s->desc);
break;
default:
return -1;
}
return sock_free(s);
}
ssize_t
sock_recvfrom(int id, void *buf, size_t n, struct sockaddr *addr, int *addrlen)
{
struct sock *s;
struct ip_endpoint ep;
int ret;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_DGRAM) {
return -1;
}
switch (s->family) {
case AF_INET:
ret = udp_recvfrom(s->desc, (uint8_t *)buf, n, &ep);
if (ret != -1) {
((struct sockaddr_in *)addr)->sin_addr = ep.addr.s_addr4;
((struct sockaddr_in *)addr)->sin_port = ep.port;
((struct sockaddr_in *)addr)->sin_family = AF_INET;
}
return ret;
case AF_INET6:
ret = udp_recvfrom(s->desc, (uint8_t *)buf, n, &ep);
if (ret != -1) {
((struct sockaddr_in6 *)addr)->sin6_addr = ep.addr.s_addr6;
((struct sockaddr_in6 *)addr)->sin6_port = ep.port;
((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
}
return ret;
default:
errorf("not supported address family");
break;
}
return -1;
}
ssize_t
sock_sendto(int id, const void *buf, size_t n, const struct sockaddr *addr, int addrlen)
{
struct sock *s;
struct ip_endpoint ep;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_DGRAM) {
return -1;
}
switch (s->family) {
case AF_INET:
ep.addr.s_addr4 = ((struct sockaddr_in *)addr)->sin_addr;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
ep.addr.family = AF_INET;
return udp_sendto(s->desc, (uint8_t *)buf, n, &ep);
case AF_INET6:
ep.addr.s_addr6 = ((struct sockaddr_in6 *)addr)->sin6_addr;
ep.port = ((struct sockaddr_in6 *)addr)->sin6_port;
ep.addr.family = AF_INET6;
return udp_sendto(s->desc, (uint8_t *)buf, n, &ep);
default:
errorf("not supported address family");
break;
}
return -1;
}
int
sock_bind(int id, const struct sockaddr *addr, int addrlen)
{
struct sock *s;
struct ip_endpoint ep;
s = sock_get(id);
if (!s) {
return -1;
}
switch (s->type) {
case SOCK_STREAM:
switch (s->family) {
case AF_INET:
ep.addr.s_addr4 = ((struct sockaddr_in *)addr)->sin_addr;
ep.addr.family = AF_INET;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return tcp_bind(s->desc, &ep);
case AF_INET6:
ep.addr.s_addr6 = ((struct sockaddr_in6 *)addr)->sin6_addr;
ep.addr.family = AF_INET6;
ep.port = ((struct sockaddr_in6 *)addr)->sin6_port;
return tcp_bind(s->desc, &ep);
default:
errorf("not supported address family");
break;
}
return -1;
case SOCK_DGRAM:
switch (s->family) {
case AF_INET:
ep.addr.s_addr4 = ((struct sockaddr_in *)addr)->sin_addr;
ep.addr.family = AF_INET;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return udp_bind(s->desc, &ep);
case AF_INET6:
ep.addr.s_addr6 = ((struct sockaddr_in6 *)addr)->sin6_addr;
ep.addr.family = AF_INET6;
ep.port = ((struct sockaddr_in6 *)addr)->sin6_port;
return udp_bind(s->desc, &ep);
default:
errorf("not supported address family");
break;
}
return -1;
}
return -1;
}
int
sock_listen(int id, int backlog)
{
struct sock *s;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_STREAM) {
return -1;
}
switch (s->family) {
case AF_INET:
return tcp_listen(s->desc, backlog);
case AF_INET6:
return tcp_listen(s->desc, backlog);
default:
errorf("not supported address family");
break;
}
return -1;
}
int
sock_accept(int id, struct sockaddr *addr, int *addrlen)
{
struct sock *s, *new_s;
struct ip_endpoint ep;
int ret;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_STREAM) {
return -1;
}
switch (s->family) {
case AF_INET:
ret = tcp_accept(s->desc, &ep);
if (ret == -1) {
return -1;
}
((struct sockaddr_in *)addr)->sin_family = AF_INET;
((struct sockaddr_in *)addr)->sin_addr = ep.addr.s_addr4;
((struct sockaddr_in *)addr)->sin_port = ep.port;
new_s = sock_alloc();
new_s->family = s->family;
new_s->type = s->type;
new_s->desc = ret;
return indexof(socks, new_s);
case AF_INET6:
ret = tcp_accept(s->desc, &ep);
if (ret == -1) {
return -1;
}
((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
((struct sockaddr_in6 *)addr)->sin6_addr = ep.addr.s_addr6;
((struct sockaddr_in6 *)addr)->sin6_port = ep.port;
new_s = sock_alloc();
new_s->family = s->family;
new_s->type = s->type;
new_s->desc = ret;
return indexof(socks, new_s);
default:
errorf("not supported address family");
break;
}
return -1;
}
int
sock_connect(int id, const struct sockaddr *addr, int addrlen)
{
struct sock *s;
struct ip_endpoint ep;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_STREAM) {
return -1;
}
switch (s->family) {
case AF_INET:
ep.addr.s_addr4 = ((struct sockaddr_in *)addr)->sin_addr;
ep.addr.family = AF_INET;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return tcp_connect(s->desc, &ep);
case AF_INET6:
ep.addr.s_addr6 = ((struct sockaddr_in6 *)addr)->sin6_addr;
ep.addr.family = AF_INET6;
ep.port = ((struct sockaddr_in6 *)addr)->sin6_port;
return tcp_connect(s->desc, &ep);
default:
errorf("not supported address family");
break;
}
return -1;
}
ssize_t
sock_recv(int id, void *buf, size_t n)
{
struct sock *s;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_STREAM) {
return -1;
}
switch (s->family) {
case AF_INET:
return tcp_receive(s->desc, (uint8_t *)buf, n);
case AF_INET6:
return tcp_receive(s->desc, (uint8_t *)buf, n);
default:
errorf("not supported address family");
break;
}
return -1;
}
ssize_t
sock_send(int id, const void *buf, size_t n)
{
struct sock *s;
s = sock_get(id);
if (!s) {
return -1;
}
if (s->type != SOCK_STREAM) {
return -1;
}
switch (s->family) {
case AF_INET:
return tcp_send(s->desc, (uint8_t *)buf, n);
case AF_INET6:
return tcp_send(s->desc, (uint8_t *)buf, n);
default:
errorf("not supported address family");
break;
}
return -1;
}