forked from pandax381/microps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sock.c
324 lines (293 loc) · 6.29 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
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "util.h"
#include "net.h"
#include "ip.h"
#include "udp.h"
#include "tcp.h"
#include "sock.h"
static struct sock socks[128];
int
sockaddr_pton(const char *p, struct sockaddr *n, size_t size)
{
struct ip_endpoint ep;
if (ip_endpoint_pton(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;
return 0;
}
return -1;
}
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 = ((struct sockaddr_in *)n)->sin_addr;
return ip_endpoint_ntop(&ep, p, size);
}
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) {
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;
((struct sockaddr_in *)addr)->sin_port = ep.port;
}
return ret;
}
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 = ((struct sockaddr_in *)addr)->sin_addr;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return udp_sendto(s->desc, (uint8_t *)buf, n, &ep);
}
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 = ((struct sockaddr_in *)addr)->sin_addr;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return tcp_bind(s->desc, &ep);
}
return -1;
case SOCK_DGRAM:
switch (s->family) {
case AF_INET:
ep.addr = ((struct sockaddr_in *)addr)->sin_addr;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return udp_bind(s->desc, &ep);
}
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);
}
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 = s->family;
((struct sockaddr_in *)addr)->sin_addr = ep.addr;
((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);
}
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 = ((struct sockaddr_in *)addr)->sin_addr;
ep.port = ((struct sockaddr_in *)addr)->sin_port;
return tcp_connect(s->desc, &ep);
}
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);
}
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);
}
return -1;
}