forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.c
More file actions
404 lines (317 loc) · 9.04 KB
/
Copy pathudp.c
File metadata and controls
404 lines (317 loc) · 9.04 KB
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
#include <stdlib.h>
#include <minix/sysutil.h>
#include <sys/ioc_net.h>
#include <net/gen/in.h>
#include <net/gen/udp.h>
#include <net/gen/udp_io.h>
#include <net/gen/udp_io_hdr.h>
#include <lwip/udp.h>
#include <lwip/ip_addr.h>
#include <minix/netsock.h>
#include "proto.h"
#define UDP_BUF_SIZE (4 << 10)
#define sock_alloc_buf(s) debug_malloc(s)
#define sock_free_buf(x) debug_free(x)
#if 0
#define debug_udp_print(str, ...) printf("LWIP %s:%d : " str "\n", \
__func__, __LINE__, ##__VA_ARGS__)
#else
#define debug_udp_print(...) debug_print(__VA_ARGS__)
#endif
struct udp_recv_data {
ip_addr_t ip;
u16_t port;
struct pbuf * pbuf;
};
#define udp_recv_alloc() debug_malloc(sizeof(struct udp_recv_data))
static void udp_recv_free(void * data)
{
if (((struct udp_recv_data *)data)->pbuf)
pbuf_free(((struct udp_recv_data *)data)->pbuf);
debug_free(data);
}
static int udp_op_open(struct socket * sock)
{
struct udp_pcb * pcb;
debug_udp_print("socket num %ld", get_sock_num(sock));
if (!(pcb = udp_new()))
return ENOMEM;
sock->buf = NULL;
sock->buf_size = 0;
sock->pcb = pcb;
return OK;
}
static int udp_op_close(struct socket * sock)
{
debug_udp_print("socket num %ld", get_sock_num(sock));
/* deque and free all enqueued data before closing */
sock_dequeue_data_all(sock, udp_recv_free);
if (sock->pcb)
udp_remove(sock->pcb);
assert(sock->buf == NULL);
/* mark it as unused */
sock->ops = NULL;
return OK;
}
static int udp_do_receive(struct socket * sock,
struct sock_req * req,
struct udp_pcb *pcb,
struct pbuf *pbuf,
ip_addr_t *addr,
u16_t port)
{
struct pbuf * p;
size_t rem_len = req->size;
unsigned int written = 0, hdr_sz = 0;
int err;
debug_udp_print("user buffer size : %u", rem_len);
/* FIXME make it both a single copy */
if (!(sock->usr_flags & NWUO_RWDATONLY)) {
udp_io_hdr_t hdr;
hdr.uih_src_addr = addr->addr;
hdr.uih_src_port = htons(port);
hdr.uih_dst_addr = pcb->local_ip.addr;
hdr.uih_dst_port = htons(pcb->local_port);
hdr.uih_data_len = 0;
hdr.uih_ip_opt_len = 0;
err = copy_to_user(req->endpt, &hdr, sizeof(hdr), req->grant,
0);
if (err != OK)
return err;
rem_len -= (hdr_sz = sizeof(hdr));
}
for (p = pbuf; p && rem_len; p = p->next) {
size_t cp_len;
cp_len = (rem_len < p->len) ? rem_len : p->len;
err = copy_to_user(req->endpt, p->payload, cp_len, req->grant,
hdr_sz + written);
if (err != OK)
return err;
written += cp_len;
rem_len -= cp_len;
}
debug_udp_print("copied %d bytes", written + hdr_sz);
return written + hdr_sz;
}
static void udp_recv_callback(void *arg,
struct udp_pcb *pcb,
struct pbuf *pbuf,
ip_addr_t *addr,
u16_t port)
{
struct socket * sock = (struct socket *) arg;
struct udp_recv_data * data;
debug_udp_print("socket num : %ld addr : %x port : %d\n",
get_sock_num(sock), (unsigned int) addr->addr, port);
if (sock->flags & SOCK_FLG_OP_PENDING) {
/* we are resuming a suspended operation */
int ret;
ret = udp_do_receive(sock, &sock->req, pcb, pbuf, addr, port);
send_req_reply(&sock->req, ret);
sock->flags &= ~SOCK_FLG_OP_PENDING;
if (ret > 0) {
pbuf_free(pbuf);
return;
}
}
/* Do not enqueue more data than allowed */
if (sock->recv_data_size > UDP_BUF_SIZE) {
pbuf_free(pbuf);
return;
}
/*
* nobody is waiting for the data or an error occured above, we enqueue
* the packet
*/
if (!(data = udp_recv_alloc())) {
pbuf_free(pbuf);
return;
}
data->ip = *addr;
data->port = port;
data->pbuf = pbuf;
if (sock_enqueue_data(sock, data, data->pbuf->tot_len) != OK) {
udp_recv_free(data);
return;
}
/*
* We don't need to notify when somebody is already waiting, reviving
* read operation will do the trick for us. But we must announce new
* data available here.
*/
if (sock_select_read_set(sock))
sock_select_notify(sock);
}
static int udp_op_read(struct socket * sock, struct sock_req * req, int blk)
{
debug_udp_print("socket num %ld", get_sock_num(sock));
if (sock->recv_head) {
/* data available receive immeditely */
struct udp_recv_data * data;
int ret;
data = (struct udp_recv_data *) sock->recv_head->data;
ret = udp_do_receive(sock, req, (struct udp_pcb *) sock->pcb,
data->pbuf, &data->ip, data->port);
if (ret > 0) {
sock_dequeue_data(sock);
sock->recv_data_size -= data->pbuf->tot_len;
udp_recv_free(data);
}
return ret;
} else if (!blk)
return EAGAIN;
else {
/* store the message so we know how to reply */
sock->req = *req;
/* operation is being processes */
sock->flags |= SOCK_FLG_OP_PENDING;
debug_udp_print("no data to read, suspending\n");
return EDONTREPLY;
}
}
static int udp_op_send(struct socket * sock,
struct pbuf * pbuf,
size_t size)
{
int err;
debug_udp_print("pbuf len %d\n", pbuf->len);
if ((err = udp_send(sock->pcb, pbuf)) == ERR_OK)
return size;
else {
debug_udp_print("udp_send failed %d", err);
return EIO;
}
}
static int udp_op_sendto(struct socket * sock, struct pbuf * pbuf, size_t size)
{
int err;
udp_io_hdr_t hdr;
hdr = *(udp_io_hdr_t *) pbuf->payload;
pbuf_header(pbuf, -(s16_t)sizeof(udp_io_hdr_t));
debug_udp_print("data len %d pbuf len %d\n",
hdr.uih_data_len, pbuf->len);
if ((err = udp_sendto(sock->pcb, pbuf, (ip_addr_t *) &hdr.uih_dst_addr,
ntohs(hdr.uih_dst_port))) == ERR_OK)
return size;
else {
debug_udp_print("udp_sendto failed %d", err);
return EIO;
}
}
static int udp_op_write(struct socket * sock, struct sock_req * req,
__unused int blk)
{
int ret;
struct pbuf * pbuf;
debug_udp_print("socket num %ld data size %u",
get_sock_num(sock), req->size);
pbuf = pbuf_alloc(PBUF_TRANSPORT, req->size, PBUF_POOL);
if (!pbuf)
return ENOMEM;
if ((ret = copy_from_user(req->endpt, pbuf->payload, req->size,
req->grant, 0)) != OK) {
pbuf_free(pbuf);
return ret;
}
if (sock->usr_flags & NWUO_RWDATONLY)
ret = udp_op_send(sock, pbuf, req->size);
else
ret = udp_op_sendto(sock, pbuf, req->size);
if (pbuf_free(pbuf) == 0) {
panic("We cannot buffer udp packets yet!");
}
return ret;
}
static int udp_set_opt(struct socket * sock, endpoint_t endpt,
cp_grant_id_t grant)
{
int err;
nwio_udpopt_t udpopt;
struct udp_pcb * pcb = (struct udp_pcb *) sock->pcb;
ip_addr_t loc_ip = ip_addr_any;
assert(pcb);
err = copy_from_user(endpt, &udpopt, sizeof(udpopt), grant, 0);
if (err != OK)
return err;
debug_udp_print("udpopt.nwuo_flags = 0x%lx", udpopt.nwuo_flags);
debug_udp_print("udpopt.nwuo_remaddr = 0x%x",
(unsigned int) udpopt.nwuo_remaddr);
debug_udp_print("udpopt.nwuo_remport = 0x%x",
ntohs(udpopt.nwuo_remport));
debug_udp_print("udpopt.nwuo_locaddr = 0x%x",
(unsigned int) udpopt.nwuo_locaddr);
debug_udp_print("udpopt.nwuo_locport = 0x%x",
ntohs(udpopt.nwuo_locport));
sock->usr_flags = udpopt.nwuo_flags;
/*
* We will only get data from userspace and the remote address
* and port are being set which means that from now on we must
* know where to send data. Thus we should interpret this as
* connect() call
*/
if (sock->usr_flags & NWUO_RWDATONLY &&
sock->usr_flags & NWUO_RP_SET &&
sock->usr_flags & NWUO_RA_SET)
udp_connect(pcb, (ip_addr_t *) &udpopt.nwuo_remaddr,
ntohs(udpopt.nwuo_remport));
/* Setting local address means binding */
if (sock->usr_flags & NWUO_LP_SET)
udp_bind(pcb, &loc_ip, ntohs(udpopt.nwuo_locport));
/* We can only bind to random local port */
if (sock->usr_flags & NWUO_LP_SEL)
udp_bind(pcb, &loc_ip, 0);
/* register a receive hook */
udp_recv((struct udp_pcb *) sock->pcb, udp_recv_callback, sock);
return OK;
}
static int udp_get_opt(struct socket * sock, endpoint_t endpt,
cp_grant_id_t grant)
{
nwio_udpopt_t udpopt;
struct udp_pcb * pcb = (struct udp_pcb *) sock->pcb;
assert(pcb);
udpopt.nwuo_locaddr = pcb->local_ip.addr;
udpopt.nwuo_locport = htons(pcb->local_port);
udpopt.nwuo_remaddr = pcb->remote_ip.addr;
udpopt.nwuo_remport = htons(pcb->remote_port);
udpopt.nwuo_flags = sock->usr_flags;
debug_udp_print("udpopt.nwuo_flags = 0x%lx", udpopt.nwuo_flags);
debug_udp_print("udpopt.nwuo_remaddr = 0x%x",
(unsigned int) udpopt.nwuo_remaddr);
debug_udp_print("udpopt.nwuo_remport = 0x%x",
ntohs(udpopt.nwuo_remport));
debug_udp_print("udpopt.nwuo_locaddr = 0x%x",
(unsigned int) udpopt.nwuo_locaddr);
debug_udp_print("udpopt.nwuo_locport = 0x%x",
ntohs(udpopt.nwuo_locport));
return copy_to_user(endpt, &udpopt, sizeof(udpopt), grant, 0);
}
static int udp_op_ioctl(struct socket * sock, struct sock_req * req,
__unused int blk)
{
int r;
debug_udp_print("socket num %ld req %c %ld %ld",
get_sock_num(sock),
(unsigned char) (req->req >> 8),
req->req & 0xff, _MINIX_IOCTL_SIZE(req->req));
switch (req->req) {
case NWIOSUDPOPT:
r = udp_set_opt(sock, req->endpt, req->grant);
break;
case NWIOGUDPOPT:
r = udp_get_opt(sock, req->endpt, req->grant);
break;
default:
r = ENOTTY;
}
return r;
}
struct sock_ops sock_udp_ops = {
.open = udp_op_open,
.close = udp_op_close,
.read = udp_op_read,
.write = udp_op_write,
.ioctl = udp_op_ioctl,
.select = generic_op_select,
.select_reply = generic_op_select_reply
};