-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.c
661 lines (630 loc) · 13.9 KB
/
socket.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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* Application programming interface routines - based loosely on the
* "socket" model in Berkeley UNIX.
*/
#include <stdio.h>
#ifdef __STDC__
#include <stdarg.h>
#endif
#include <errno.h>
#include "global.h"
#include "mbuf.h"
#include "netuser.h"
#include "proc.h"
#include "lzw.h"
#include "usock.h"
#include "socket.h"
char *Socktypes[] = {
"Not Used",
"TCP",
"UDP",
"AX25 I",
"AX25 UI",
"Raw IP",
"NETROM3",
"NETROM",
"Loc St",
"Loc Dg"
};
char *Sock_errlist[] = {
"operation would block",
"not connected",
"socket type not supported",
"address family not supported",
"is connected",
"operation not supported",
"alarm",
"abort",
"interrupt",
"connection refused",
"message size",
"address in use"
};
char Badsocket[] = "Bad socket";
struct usock **Usock; /* Socket entry array */
/* Initialize user socket array */
void
sockinit(void)
{
if(Usock != (struct usock **)NULL)
return; /* Already initialized */
Usock = (struct usock **)callocw(Nsock,sizeof(struct usock *));
}
/* Create a user socket, return socket index
* The mapping to actual protocols is as follows:
*
*
* ADDRESS FAMILY Stream Datagram Raw Seq. Packet
*
* AF_INET TCP UDP IP
* AF_AX25 I-frames UI-frames
* AF_NETROM NET/ROM L3 NET/ROM L4
* AF_LOCAL stream loopback packet loopback
*/
int
socket(
int af, /* Address family */
int type, /* Stream or datagram */
int protocol /* Used for raw IP sockets */
){
register struct usock *up;
struct socklink *sp;
int s;
for(s=0;s<Nsock;s++)
if(Usock[s] == NULL)
break;
if(s == Nsock){
errno = EMFILE;
return -1;
}
Usock[s] = up = (struct usock *)calloc(1,sizeof(struct usock));
s =_mk_fd(s,_FL_SOCK);
up->index = s;
up->refcnt = 1;
errno = 0;
up->rdysock = -1;
up->owner = Curproc;
switch(af){
case AF_LOCAL:
switch(type){
case SOCK_STREAM:
up->type = TYPE_LOCAL_STREAM;
break;
case SOCK_DGRAM:
up->type = TYPE_LOCAL_DGRAM;
break;
default:
errno = ESOCKTNOSUPPORT;
break;
}
break;
case AF_AX25:
switch(type){
case SOCK_STREAM:
up->type = TYPE_AX25I;
break;
case SOCK_DGRAM:
up->type = TYPE_AX25UI;
break;
default:
errno = ESOCKTNOSUPPORT;
break;
}
break;
case AF_NETROM:
switch(type){
case SOCK_RAW:
up->type = TYPE_NETROML3;
break;
case SOCK_SEQPACKET:
up->type = TYPE_NETROML4;
break;
default:
errno = ESOCKTNOSUPPORT;
break;
}
break;
case AF_INET:
switch(type){
case SOCK_STREAM:
up->type = TYPE_TCP;
break;
case SOCK_DGRAM:
up->type = TYPE_UDP;
break;
case SOCK_RAW:
up->type = TYPE_RAW;
break;
default:
errno = ESOCKTNOSUPPORT;
break;
}
break;
default:
errno = EAFNOSUPPORT;
break;
}
/* Look for entry in protocol table */
for(sp = Socklink;sp->type != -1;sp++){
if(up->type == sp->type)
break;
}
up->sp = sp;
if(sp->type == -1 || sp->socket == NULL
||(*sp->socket)(up,protocol) == -1){
errno = ESOCKTNOSUPPORT;
return -1;
}
return s;
}
/* Attach a local address/port to a socket. If not issued before a connect
* or listen, will be issued automatically
*/
int
bind(
int s, /* Socket index */
struct sockaddr *name, /* Local name */
int namelen /* Length of name */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(name == NULL){
errno = EFAULT;
return -1;
}
if(up->name != NULL){
/* Bind has already been issued */
errno = EINVAL;
return -1;
}
sp = up->sp;
if(sp->check != NULL && (*sp->check)(name,namelen) == -1){
/* Incorrect length or family for chosen protocol */
errno = EAFNOSUPPORT;
return -1;
}
/* Stash name in an allocated block */
up->namelen = namelen;
up->name = mallocw(namelen);
memcpy(up->name,name,namelen);
/* a bind routine is optional - don't fail if it isn't present */
if(sp->bind != NULL && (*sp->bind)(up) == -1){
errno = EOPNOTSUPP;
return -1;
}
return 0;
}
/* Post a listen on a socket */
int
listen(
int s, /* Socket index */
int backlog /* 0 for a single connection, !=0 for multiple connections */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(up->cb.p != NULL){
errno = EISCONN;
return -1;
}
sp = up->sp;
/* Fail if listen routine isn't present */
if(sp->listen == NULL || (*sp->listen)(up,backlog) == -1){
errno = EOPNOTSUPP;
return -1;
}
return 0;
}
/* Initiate active open. For datagram sockets, merely bind the remote address. */
int
connect(
int s, /* Socket index */
struct sockaddr *peername, /* Peer name */
int peernamelen /* Length of peer name */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(peername == NULL){
/* Connect must specify a remote address */
errno = EFAULT;
return -1;
}
sp = up->sp;
/* Check name format, if checking routine is available */
if(sp->check != NULL && (*sp->check)(peername,peernamelen) == -1){
errno = EAFNOSUPPORT;
return -1;
}
if(up->peername != NULL)
free(up->peername);
up->peername = mallocw(peernamelen);
memcpy(up->peername,peername,peernamelen);
up->peernamelen = peernamelen;
/* a connect routine is optional - don't fail if it isn't present */
if(sp->connect != NULL && (*sp->connect)(up) == -1){
return -1;
}
return 0;
}
/* Wait for a connection. Valid only for connection-oriented sockets. */
int
accept(
int s, /* Socket index */
struct sockaddr *peername, /* Peer name */
int *peernamelen /* Length of peer name */
){
int i;
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(up->cb.p == NULL){
errno = EOPNOTSUPP;
return -1;
}
sp = up->sp;
/* Fail if accept flag isn't set */
if(sp->accept == FALSE){
errno = EOPNOTSUPP;
return -1;
}
/* Wait for the state-change upcall routine to signal us */
while(up->cb.p != NULL && up->rdysock == -1){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(up->cb.p == NULL){
/* Blown away */
errno = EBADF;
return -1;
}
i = up->rdysock;
up->rdysock = -1;
up = itop(i);
if(peername != NULL && peernamelen != NULL){
*peernamelen = min(up->peernamelen,*peernamelen);
memcpy(peername,up->peername,*peernamelen);
}
return i;
}
/* Low-level receive routine. Passes mbuf back to user; more efficient than
* higher-level functions recv() and recvfrom(). Datagram sockets ignore
* the len parameter.
*/
int
recv_mbuf(
int s, /* Socket index */
struct mbuf **bpp, /* Place to stash receive buffer */
int flags, /* Unused; will control out-of-band data, etc */
struct sockaddr *from, /* Peer address (only for datagrams) */
int *fromlen /* Length of peer address */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
sp = up->sp;
/* Fail if recv routine isn't present */
if(sp->recv == NULL){
errno = EOPNOTSUPP;
return -1;
}
return (*sp->recv)(up,bpp,from,fromlen);
}
/* Low level send routine; user supplies mbuf for transmission. More
* efficient than send() or sendto(), the higher level interfaces.
* The "to" and "tolen" parameters are ignored on connection-oriented
* sockets.
*
* In case of error, bp is freed so the caller doesn't have to worry about it.
*/
int
send_mbuf(
int s, /* Socket index */
struct mbuf **bpp, /* Buffer to send */
int flags, /* not currently used */
struct sockaddr *to, /* Destination, only for datagrams */
int tolen /* Length of destination */
){
register struct usock *up;
int cnt;
struct socklink *sp;
if((up = itop(s)) == NULL){
free_p(bpp);
errno = EBADF;
return -1;
}
sp = up->sp;
/* Fail if send routine isn't present (shouldn't happen) */
if(sp->send == NULL){
free_p(bpp);
return -1;
}
/* If remote address is supplied, check it */
if(to != NULL && (sp->check != NULL)
&& (*sp->check)(to,tolen) == -1){
free_p(bpp);
errno = EAFNOSUPPORT;
return -1;
}
/* The proto send routine is expected to free the buffer
* we pass it even if the send fails
*/
if((cnt = (*sp->send)(up,bpp,to)) == -1){
errno = EOPNOTSUPP;
return -1;
}
return cnt;
}
/* Return local name passed in an earlier bind() call */
int
getsockname(
int s, /* Socket index */
struct sockaddr *name, /* Place to stash name */
int *namelen /* Length of same */
){
register struct usock *up;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(name == NULL || namelen == (int *)NULL){
errno = EFAULT;
return -1;
}
if(up->name == NULL){
/* Not bound yet */
*namelen = 0;
return 0;
}
if(up->name != NULL){
*namelen = min(*namelen,up->namelen);
memcpy(name,up->name,*namelen);
}
return 0;
}
/* Get remote name, returning result of earlier connect() call. */
int
getpeername(
int s, /* Socket index */
struct sockaddr *peername, /* Place to stash name */
int *peernamelen /* Length of same */
){
register struct usock *up;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(up->peername == NULL){
errno = ENOTCONN;
return -1;
}
if(peername == NULL || peernamelen == (int *)NULL){
errno = EFAULT;
return -1;
}
*peernamelen = min(*peernamelen,up->peernamelen);
memcpy(peername,up->peername,*peernamelen);
return 0;
}
/* Return length of protocol queue, either send or receive. */
int
socklen(
int s, /* Socket index */
int rtx /* 0 = receive queue, 1 = transmit queue */
){
register struct usock *up;
struct socklink *sp;
int len = -1;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(up->cb.p == NULL){
errno = ENOTCONN;
return -1;
}
if(rtx < 0 || rtx > 1){
errno = EINVAL;
return -1;
}
sp = up->sp;
/* Fail if qlen routine isn't present */
if(sp->qlen == NULL || (len = (*sp->qlen)(up,rtx)) == -1){
errno = EOPNOTSUPP;
return -1;
}
return len;
}
/* Force retransmission. Valid only for connection-oriented sockets. */
int
sockkick(
int s /* Socket index */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
sp = up->sp;
/* Fail if kick routine isn't present */
if(sp->kick == NULL){
errno = EOPNOTSUPP;
return -1;
}
if((*sp->kick)(up) == -1)
return -1;
return 0;
}
/* Change owner of socket, return previous owner */
struct proc *
sockowner(
int s, /* Socket index */
struct proc *newowner /* Process table address of new owner */
){
register struct usock *up;
struct proc *pp;
if((up = itop(s)) == NULL){
errno = EBADF;
return NULL;
}
pp = up->owner;
if(newowner != NULL)
up->owner = newowner;
return pp;
}
/* Close down a socket three ways. Type 0 means "no more receives"; this
* replaces the incoming data upcall with a routine that discards further
* data. Type 1 means "no more sends", and obviously corresponds to sending
* a TCP FIN. Type 2 means "no more receives or sends". This I interpret
* as "abort the connection".
*/
int
shutdown(
int s, /* Socket index */
int how /* (see above) */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(up->cb.p == NULL){
errno = ENOTCONN;
return -1;
}
sp = up->sp;
/* Just close the socket if special shutdown routine not present */
if(sp->shut == NULL){
close_s(s);
} else if((*sp->shut)(up,how) == -1){
return -1;
}
ksignal(up,0);
return 0;
}
/* Close a socket, freeing it for reuse. Try to do a graceful close on a
* TCP socket, if possible
*/
int
close_s(
int s /* Socket index */
){
register struct usock *up;
struct socklink *sp;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
if(--up->refcnt > 0)
return 0; /* Others are still using it */
/* Call proto-specific close routine if there is one */
if((sp = up->sp) != NULL && sp->close != NULL)
(*sp->close)(up);
free(up->name);
free(up->peername);
ksignal(up,0); /* Wake up anybody doing an accept() or recv() */
Usock[_fd_seq(up->index)] = NULL;
free(up);
return 0;
}
/* Increment reference count for specified socket */
int
usesock(int s)
{
struct usock *up;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
up->refcnt++;
return 0;
}
/* Blow away all sockets belonging to a certain process. Used by killproc(). */
void
freesock(struct proc *pp)
{
register struct usock *up;
register int i;
for(i=0;i < Nsock;i++){
up = Usock[i];
if(up != NULL && up->type != NOTUSED && up->owner == pp)
shutdown(i,2);
}
}
/* Set Internet type-of-service to be used */
int
settos(int s, int tos)
{
struct usock *up;
if((up = itop(s)) == NULL){
errno = EBADF;
return -1;
}
up->tos = tos;
return 0;
}
/* Return a pair of mutually connected sockets in sv[0] and sv[1] */
int
socketpair(
int af,
int type,
int protocol,
int sv[]
){
struct usock *up0, *up1;
if(sv == NULL){
errno = EFAULT;
return -1;
}
if(af != AF_LOCAL){
errno = EAFNOSUPPORT;
return -1;
}
if(type != SOCK_STREAM && type != SOCK_DGRAM){
errno = ESOCKTNOSUPPORT;
return -1;
}
if((sv[0] = socket(af,type,protocol)) == -1)
return -1;
if((sv[1] = socket(af,type,protocol)) == -1){
close_s(sv[0]);
return -1;
}
up0 = itop(sv[0]);
up1 = itop(sv[1]);
up0->cb.local->peer = up1;
up1->cb.local->peer = up0;
return sv[1];
}
/* Return end-of-line convention for socket */
char *
eolseq(int s)
{
struct usock *up;
if((up = itop(s)) == NULL){
errno = EBADF;
return NULL;
}
return up->sp->eol;
}