Repository navigation
Expand file tree
/
Copy pathsocket.c
More file actions
518 lines (426 loc) · 10.8 KB
/
Copy pathsocket.c
File metadata and controls
518 lines (426 loc) · 10.8 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* This file implements handling of socket-related requests from VFS
*/
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <minix/ipc.h>
#include <minix/com.h>
#include <minix/callnr.h>
#include <minix/sysutil.h>
#include <minix/netsock.h>
#include <lwip/tcp.h>
#include <sys/ioc_net.h>
char * netsock_user_name = NULL;
#define NETSOCK_USER_NAME (netsock_user_name ? netsock_user_name : "NETSOCK")
#define debug_print(str, ...) printf("%s : %s:%d : " str "\n", \
NETSOCK_USER_NAME, __func__, __LINE__, ##__VA_ARGS__)
#if 0
#define debug_sock_print(...) debug_print(__VA_ARGS__)
#else
#define debug_sock_print(...)
#endif
#if 0
#define debug_sock_select_print(...) debug_print(__VA_ARGS__)
#else
#define debug_sock_select_print(...) debug_sock_print(__VA_ARGS__)
#endif
#define netsock_panic(str, ...) panic("%s : " str, NETSOCK_USER_NAME, \
##__VA_ARGS__)
#define netsock_error(str, ...) printf("%s : " str, NETSOCK_USER_NAME, \
##__VA_ARGS__)
struct socket socket[MAX_SOCKETS];
#define recv_q_alloc() debug_malloc(sizeof(struct recv_q))
#define recv_q_free debug_free
struct mq {
message m;
struct mq * prev;
struct mq * next;
};
#define mq_alloc() debug_malloc(sizeof(struct mq))
#define mq_free debug_free
static struct mq * mq_head, *mq_tail;
int mq_enqueue(message * m)
{
struct mq * mq;
debug_sock_print("sock %d op %d", m->DEVICE, m->m_type);
mq = mq_alloc();
if (mq == NULL)
return -1;
mq->next = NULL;
mq->m = *m;
if (mq_head) {
mq->prev = mq_tail;
mq_tail->next = mq;
mq_tail = mq;
}
else {
mq->prev = NULL;
mq_head = mq_tail = mq;
}
return 0;
}
__unused static struct mq * mq_dequeue_head(void)
{
struct mq * ret;
if (!mq_head)
return NULL;
ret = mq_head;
if (mq_head != mq_tail) {
mq_head = mq_head->next;
mq_head->prev = NULL;
} else
mq_head = mq_tail = NULL;
debug_sock_print("socket %d\n", ret->m.DEVICE);
return ret;
}
static void mq_dequeue(struct mq * mq)
{
if (mq_head == mq_tail)
mq_head = mq_tail = NULL;
else {
if (mq->prev == NULL) {
mq_head = mq->next;
mq_head->prev = NULL;
} else
mq->prev->next = mq->next;
if (mq->next == NULL) {
mq_tail = mq->prev;
mq_tail->next = NULL;
} else
mq->next->prev = mq->prev;
}
}
static int mq_cancel(message * m)
{
struct mq * mq;
for (mq = mq_tail; mq; mq = mq->prev) {
if (m->DEVICE == mq->m.DEVICE &&
m->USER_ENDPT == mq->m.USER_ENDPT &&
m->IO_GRANT == mq->m.IO_GRANT) {
debug_sock_print("socket %d\n", mq->m.DEVICE);
break;
}
}
if (mq) {
mq_dequeue(mq);
mq_free(mq);
}
return 1;
}
int sock_enqueue_data(struct socket * sock, void * data, unsigned size)
{
struct recv_q * r;
if (!(r = recv_q_alloc()))
return ENOMEM;
r->data = data;
r->next = NULL;
if (sock->recv_head) {
sock->recv_tail->next = r;
sock->recv_tail = r;
} else {
sock->recv_head = sock->recv_tail = r;
}
assert(size > 0);
sock->recv_data_size += size;
return OK;
}
void * sock_dequeue_data(struct socket * sock)
{
void * data;
struct recv_q * r;
if ((r = sock->recv_head)) {
data = r->data;
if (!(sock->recv_head = r->next))
sock->recv_tail = NULL;
recv_q_free(r);
return data;
}
return NULL;
}
void sock_dequeue_data_all(struct socket * sock,
recv_data_free_fn data_free)
{
void * data;
while ((data = sock_dequeue_data(sock)))
data_free(data);
sock->recv_data_size = 0;
}
static void set_reply_msg(message * m, int status)
{
int proc, ref;
proc= m->USER_ENDPT;
ref= (int)m->IO_GRANT;
m->REP_ENDPT= proc;
m->REP_STATUS= status;
m->REP_IO_GRANT= ref;
}
void send_reply_type(message * m, int type, int status)
{
int result;
set_reply_msg(m, status);
m->m_type = type;
result = send(m->m_source, m);
if (result != OK)
netsock_panic("unable to send (err %d)", result);
}
void send_reply(message * m, int status)
{
debug_sock_print("status %d", status);
send_reply_type(m, DEV_REVIVE, status);
}
void send_reply_open(message * m, int status)
{
debug_sock_print("status %d", status);
send_reply_type(m, DEV_OPEN_REPL, status);
}
void send_reply_close(message * m, int status)
{
debug_sock_print("status %d", status);
send_reply_type(m, DEV_CLOSE_REPL, status);
}
void sock_reply_select(struct socket * sock, unsigned selops)
{
int result;
message msg;
debug_sock_select_print("selops %d", selops);
msg.m_type = DEV_SEL_REPL1;
msg.DEV_MINOR = get_sock_num(sock);
msg.DEV_SEL_OPS = selops;
result = send(sock->select_ep, &msg);
if (result != OK)
netsock_panic("unable to send (err %d)", result);
}
void sock_select_notify(struct socket * sock)
{
int result;
message msg;
debug_sock_select_print("socket num %ld", get_sock_num(sock));
assert(sock->select_ep != NONE);
msg.DEV_SEL_OPS = 0;
sock->ops->select_reply(sock, &msg);
if (msg.DEV_SEL_OPS == 0) {
debug_sock_select_print("called from %p sflags 0x%x TXsz %d RXsz %d\n",
__builtin_return_address(0), sock->flags,
sock->buf_size, sock->recv_data_size);
return;
}
msg.m_type = DEV_SEL_REPL2;
msg.DEV_MINOR = get_sock_num(sock);
debug_sock_select_print("socket num %d select result 0x%x sent",
msg.DEV_MINOR, msg.DEV_SEL_OPS);
result = send(sock->select_ep, &msg);
if (result != OK)
netsock_panic("unable to send (err %d)", result);
sock_clear_select(sock);
sock->select_ep = NONE;
}
static void sock_reply_type(struct socket * sock, int type, int status)
{
sock->mess.m_type = type;
send_reply_type(&sock->mess, type, status);
}
void sock_reply_close(struct socket * sock, int status)
{
debug_sock_print("sock %ld status %d", get_sock_num(sock), status);
sock_reply_type(sock, DEV_CLOSE_REPL, status);
}
void sock_reply(struct socket * sock, int status)
{
debug_sock_print("sock %ld status %d", get_sock_num(sock), status);
sock_reply_type(sock, DEV_REVIVE, status);
}
struct socket * get_unused_sock(void)
{
int i;
for (i = SOCK_TYPES + MAX_DEVS; i < MAX_SOCKETS; i++) {
if (socket[i].ops == NULL) {
/* clear it all */
memset(&socket[i], 0, sizeof(struct socket));
return &socket[i];
}
}
return NULL;
}
static void socket_request_socket(struct socket * sock, message * m)
{
int blocking = m->FLAGS & FLG_OP_NONBLOCK ? 0 : 1;
switch (m->m_type) {
case DEV_READ_S:
if (sock->ops && sock->ops->read)
sock->ops->read(sock, m, blocking);
else
send_reply(m, EINVAL);
return;
case DEV_WRITE_S:
if (sock->ops && sock->ops->write)
sock->ops->write(sock, m, blocking);
else
send_reply(m, EINVAL);
return;
case DEV_IOCTL_S:
if (sock->ops && sock->ops->ioctl)
sock->ops->ioctl(sock, m, blocking);
else
send_reply(m, EINVAL);
return;
default:
netsock_panic("cannot happen!");
}
}
void socket_request(message * m)
{
struct socket * sock;
debug_sock_print("request %d", m->m_type);
switch (m->m_type) {
case DEV_OPEN:
socket_open(m);
return;
case DEV_CLOSE:
sock = get_sock(m->DEVICE);
if (sock->ops && sock->ops->close) {
sock->flags &= ~SOCK_FLG_OP_PENDING;
sock->mess = *m;
sock->ops->close(sock, m);
} else
send_reply_close(m, EINVAL);
return;
case DEV_READ_S:
case DEV_WRITE_S:
case DEV_IOCTL_S:
sock = get_sock(m->DEVICE);
if (!sock) {
send_reply(m, EINVAL);
return;
}
/*
* If an operation is pending (blocking operation) or writing is
* still going and we want to read, suspend the new operation
*/
if ((sock->flags & SOCK_FLG_OP_PENDING) ||
(m->m_type == DEV_READ_S &&
sock->flags & SOCK_FLG_OP_WRITING)) {
char * o = "\0";
if (sock->flags & SOCK_FLG_OP_READING)
o = "READ";
else if (sock->flags & SOCK_FLG_OP_WRITING)
o = "WRITE";
else
o = "non R/W op";
debug_sock_print("socket %ld is busy by %s flgs 0x%x\n",
get_sock_num(sock), o, sock->flags);
if (mq_enqueue(m) != 0) {
debug_sock_print("Enqueuing suspended "
"call failed");
send_reply(m, ENOMEM);
}
return;
}
sock->mess = *m;
socket_request_socket(sock, m);
return;
case CANCEL:
sock = get_sock(m->DEVICE);
printf("socket num %ld\n", get_sock_num(sock));
debug_sock_print("socket num %ld", get_sock_num(sock));
/* Cancel the last operation in the queue */
if (mq_cancel(m)) {
send_reply(m, EINTR);
return;
/* ... or a blocked read */
} else if (sock->flags & SOCK_FLG_OP_PENDING &&
sock->flags & SOCK_FLG_OP_READING) {
sock->flags &= ~SOCK_FLG_OP_PENDING;
send_reply(m, EINTR);
return;
} else
netsock_panic("no operation to cancel");
return;
case DEV_SELECT:
/*
* Select is always executed immediately and is never suspended.
* Although, it sets actions which must be monitored
*/
sock = get_sock(m->DEVICE);
assert(sock->select_ep == NONE || sock->select_ep == m->m_source);
if (sock->ops && sock->ops->select) {
sock->select_ep = m->m_source;
sock->ops->select(sock, m);
if (!sock_select_set(sock))
sock->select_ep = NONE;
} else
send_reply(m, EINVAL);
return;
default:
netsock_error("unknown message from VFS, type %d\n",
m->m_type);
}
send_reply(m, EGENERIC);
}
void mq_process(void)
{
struct mq * mq;
struct socket * sock;
mq = mq_head;
while(mq) {
struct mq * next = mq->next;
sock = get_sock(mq->m.DEVICE);
if (!(sock->flags & SOCK_FLG_OP_PENDING) &&
!(mq->m.m_type == DEV_READ_S &&
sock->flags & SOCK_FLG_OP_WRITING)) {
debug_sock_print("resuming op on sock %ld\n",
get_sock_num(sock));
sock->mess = mq->m;
socket_request_socket(sock, &sock->mess);
mq_dequeue(mq);
mq_free(mq);
return;
}
mq = next;
}
}
void generic_op_select(struct socket * sock, message * m)
{
int retsel = 0, sel;
debug_sock_print("socket num %ld 0x%x", get_sock_num(sock), m->USER_ENDPT);
sel = m->USER_ENDPT;
/* in this case any operation would block, no error */
if (sock->flags & SOCK_FLG_OP_PENDING) {
if (sel & SEL_NOTIFY) {
if (sel & SEL_RD)
sock->flags |= SOCK_FLG_SEL_READ;
if (sel & SEL_WR)
sock->flags |= SOCK_FLG_SEL_WRITE;
/* FIXME we do not monitor error */
}
sock_reply_select(sock, 0);
return;
}
if (sel & SEL_RD) {
if (sock->recv_head)
retsel |= SEL_RD;
else if (sel & SEL_NOTIFY)
sock->flags |= SOCK_FLG_SEL_READ;
}
/* FIXME generic packet socket never blocks on write */
if (sel & SEL_WR)
retsel |= SEL_WR;
/* FIXME SEL_ERR is ignored, we do not generate exceptions */
sock_reply_select(sock, retsel);
}
void generic_op_select_reply(struct socket * sock, __unused message * m)
{
assert(sock->select_ep != NONE);
debug_sock_print("socket num %ld", get_sock_num(sock));
/* unused for generic packet socket, see generic_op_select() */
assert((sock->flags & (SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_ERROR)) == 0);
if (sock->flags & SOCK_FLG_OP_PENDING) {
debug_sock_print("WARNING socket still blocking!");
return;
}
if (sock->flags & SOCK_FLG_SEL_READ && sock->recv_head)
m->DEV_SEL_OPS |= SEL_RD;
if (m->DEV_SEL_OPS)
sock->flags &= ~(SOCK_FLG_SEL_WRITE | SOCK_FLG_SEL_READ |
SOCK_FLG_SEL_ERROR);
}