-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpsock.c
433 lines (398 loc) · 10.1 KB
/
tcpsock.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
#include "global.h"
#include "tcp.h"
#include "socket.h"
#include "usock.h"
static void s_trcall(struct tcb *tcb,int32 cnt);
static void s_tscall(struct tcb *tcb,int old,int new);
static void s_ttcall(struct tcb *tcb,int32 cnt);
static void trdiscard(struct tcb *tcb,int32 cnt);
static void autobind(struct usock *up);
uint16 Lport = 1024;
int
so_tcp(struct usock *up,int protocol)
{
up->type = TYPE_TCP;
return 0;
}
int
so_tcp_listen(struct usock *up,int backlog)
{
struct sockaddr_in *local;
struct socket lsock;
if(up->name == NULL)
autobind(up);
local = (struct sockaddr_in *)up->name;
lsock.address = local->sin_addr.s_addr;
lsock.port = local->sin_port;
up->cb.tcb = open_tcp(&lsock,NULL,
backlog ? TCP_SERVER:TCP_PASSIVE,0,
s_trcall,s_ttcall,s_tscall,up->tos,up->index);
return 0;
}
int
so_tcp_conn(struct usock *up)
{
int s;
struct tcb *tcb;
struct socket lsock,fsock;
struct sockaddr_in *local,*remote;
if(up->name == NULL){
autobind(up);
}
if(checkipaddr(up->peername,up->peernamelen) == -1){
errno = EAFNOSUPPORT;
return -1;
}
s = up->index;
/* Construct the TCP-style ports from the sockaddr structs */
local = (struct sockaddr_in *)up->name;
remote = (struct sockaddr_in *)up->peername;
if(local->sin_addr.s_addr == INADDR_ANY)
/* Choose a local address */
local->sin_addr.s_addr = locaddr(remote->sin_addr.s_addr);
lsock.address = local->sin_addr.s_addr;
lsock.port = local->sin_port;
fsock.address = remote->sin_addr.s_addr;
fsock.port = remote->sin_port;
/* Open the TCB in active mode */
up->cb.tcb = open_tcp(&lsock,&fsock,TCP_ACTIVE,0,
s_trcall,s_ttcall,s_tscall,up->tos,s);
/* Wait for the connection to complete */
while((tcb = up->cb.tcb) != NULL && tcb->state != TCP_ESTABLISHED){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(tcb == NULL){
/* Probably got refused */
FREE(up->peername);
errno = ECONNREFUSED;
return -1;
}
return 0;
}
int
so_tcp_recv(struct usock *up,struct mbuf **bpp,struct sockaddr *from,
int *fromlen)
{
long cnt;
struct tcb *tcb;
while((tcb = up->cb.tcb) != NULL && tcb->r_upcall != trdiscard
&& (cnt = recv_tcp(tcb,bpp,0)) == -1){
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(tcb == NULL){
/* Connection went away */
errno = ENOTCONN;
return -1;
} else if(tcb->r_upcall == trdiscard){
/* Receive shutdown has been done */
errno = ENOTCONN; /* CHANGE */
return -1;
}
return cnt;
}
int
so_tcp_send(struct usock *up,struct mbuf **bpp,struct sockaddr *to)
{
struct tcb *tcb;
long cnt;
if((tcb = up->cb.tcb) == NULL){
free_p(bpp);
errno = ENOTCONN;
return -1;
}
cnt = send_tcp(tcb,bpp);
while((tcb = up->cb.tcb) != NULL &&
tcb->sndcnt > tcb->window){
/* Send queue is full */
if(up->noblock){
errno = EWOULDBLOCK;
return -1;
} else if((errno = kwait(up)) != 0){
return -1;
}
}
if(tcb == NULL){
errno = ENOTCONN;
return -1;
}
return cnt;
}
int
so_tcp_qlen(struct usock *up,int rtx)
{
int len;
switch(rtx){
case 0:
len = up->cb.tcb->rcvcnt;
break;
case 1:
len = up->cb.tcb->sndcnt;
break;
}
return len;
}
int
so_tcp_kick(struct usock *up)
{
kick_tcp(up->cb.tcb);
return 0;
}
int
so_tcp_shut(struct usock *up,int how)
{
switch(how){
case 0: /* No more receives -- replace upcall */
up->cb.tcb->r_upcall = trdiscard;
break;
case 1: /* Send EOF */
close_tcp(up->cb.tcb);
break;
case 2: /* Blow away TCB */
reset_tcp(up->cb.tcb);
up->cb.tcb = NULL;
break;
}
return 0;
}
int
so_tcp_close(struct usock *up)
{
if(up->cb.tcb != NULL){ /* In case it's been reset */
up->cb.tcb->r_upcall = trdiscard;
/* Tell the TCP_CLOSED upcall there's no more socket */
up->cb.tcb->user = -1;
close_tcp(up->cb.tcb);
}
return 0;
}
/* TCP receive upcall routine */
static void
s_trcall(struct tcb *tcb,int32 cnt)
{
/* Wake up anybody waiting for data, and let them run */
ksignal(itop(tcb->user),1);
kwait(NULL);
}
/* TCP transmit upcall routine */
static void
s_ttcall(struct tcb *tcb,int32 cnt)
{
/* Wake up anybody waiting to send data, and let them run */
ksignal(itop(tcb->user),1);
kwait(NULL);
}
/* TCP state change upcall routine */
static void
s_tscall(struct tcb *tcb,int old,int new)
{
int s,ns;
struct usock *up,*nup,*oup;
union sp sp;
s = tcb->user;
oup = up = itop(s);
switch(new){
case TCP_CLOSED:
/* Clean up. If the user has already closed the socket,
* then up will be null (s was set to -1 by the close routine).
* If not, then this is an abnormal close (e.g., a reset)
* and clearing out the pointer in the socket structure will
* prevent any further operations on what will be a freed
* control block. Also wake up anybody waiting on events
* related to this tcb so they will notice it disappearing.
*/
if(up != NULL){
up->cb.tcb = NULL;
up->errcodes[0] = tcb->reason;
up->errcodes[1] = tcb->type;
up->errcodes[2] = tcb->code;
ksignal(up,0); /* Wake up anybody waiting */
}
del_tcp(tcb);
break;
case TCP_SYN_RECEIVED:
/* Handle an incoming connection. If this is a server TCB,
* then we're being handed a "clone" TCB and we need to
* create a new socket structure for it. In either case,
* find out who we're talking to and wake up the guy waiting
* for the connection.
*/
if(tcb->flags.clone){
/* Clone the socket */
ns = socket(AF_INET,SOCK_STREAM,0);
nup = itop(ns);
ASSIGN(*nup,*up);
tcb->user = ns;
nup->cb.tcb = tcb;
/* Allocate new memory for the name areas */
nup->name = mallocw(SOCKSIZE);
nup->peername = mallocw(SOCKSIZE);
nup->index = ns;
/* Store the new socket # in the old one */
up->rdysock = ns;
up = nup;
s = ns;
} else {
/* Allocate space for the peer's name */
up->peername = mallocw(SOCKSIZE);
/* Store the old socket # in the old socket */
up->rdysock = s;
}
/* Load the addresses. Memory for the name has already
* been allocated, either above or in the original bind.
*/
sp.sa = up->name;
sp.in->sin_family = AF_INET;
sp.in->sin_addr.s_addr = up->cb.tcb->conn.local.address;
sp.in->sin_port = up->cb.tcb->conn.local.port;
up->namelen = SOCKSIZE;
sp.sa = up->peername;
sp.in->sin_family = AF_INET;
sp.in->sin_addr.s_addr = up->cb.tcb->conn.remote.address;
sp.in->sin_port = up->cb.tcb->conn.remote.port;
up->peernamelen = SOCKSIZE;
/* Wake up the guy accepting it, and let him run */
ksignal(oup,1);
kwait(NULL);
break;
default: /* Ignore all other state transitions */
break;
}
ksignal(up,0); /* In case anybody's waiting */
}
/* Discard data received on a TCP connection. Used after a receive shutdown or
* close_s until the TCB disappears.
*/
static void
trdiscard(struct tcb *tcb,int32 cnt)
{
struct mbuf *bp;
recv_tcp(tcb,&bp,cnt);
free_p(&bp);
}
/* Issue an automatic bind of a local address */
static void
autobind(struct usock *up)
{
struct sockaddr_in local;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = Lport++;
bind(up->index,(struct sockaddr *)&local,sizeof(struct sockaddr_in));
}
char *
tcpstate(struct usock *up)
{
if(up->cb.tcb == NULL)
return NULL;
return Tcpstates[up->cb.tcb->state];
}
int
so_tcp_stat(struct usock *up)
{
st_tcp(up->cb.tcb);
return 0;
}
struct inet {
struct inet *next;
struct tcb *tcb;
char *name;
int stack;
void (*task)(int,void *,void *);
};
#define NULLINET (struct inet *)0
struct inet *Inet_list;
static void i_upcall(struct tcb *tcb,int oldstate,int newstate);
/* Start a TCP server. Create TCB in listen state and post upcall for
* when a connection comes in
*/
int
start_tcp(uint16 port,char *name,void (*task)(int,void *,void *),int stack)
{
struct inet *in;
struct socket lsocket;
in = (struct inet *)calloc(1,sizeof(struct inet));
lsocket.address = INADDR_ANY;
lsocket.port = port;
in->tcb = open_tcp(&lsocket,NULL,TCP_SERVER,0,NULL,NULL,i_upcall,0,-1);
if(in->tcb == NULL){
free(in);
return -1;
}
in->stack = stack;
in->task = task;
in->name = strdup(name);
in->next = Inet_list;
Inet_list = in;
return 0;
}
/* State change upcall that takes incoming TCP connections */
static void
i_upcall(struct tcb *tcb,int oldstate,int newstate)
{
struct inet *in;
struct sockaddr_in sock;
struct usock *up;
int s;
if(oldstate != TCP_LISTEN)
return; /* "can't happen" */
if(newstate == TCP_CLOSED){
/* Called when server is shut down */
del_tcp(tcb);
return;
}
for(in = Inet_list;in != NULLINET;in = in->next)
if(in->tcb->conn.local.port == tcb->conn.local.port)
break;
if(in == NULLINET)
return; /* not in table - "can't happen" */
/* Create a socket, hook it up with the TCB */
s = socket(AF_INET,SOCK_STREAM,0);
up = itop(s);
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = tcb->conn.local.address;
sock.sin_port = tcb->conn.local.port;
bind(s,(struct sockaddr *)&sock,SOCKSIZE);
sock.sin_addr.s_addr = tcb->conn.remote.address;
sock.sin_port = tcb->conn.remote.port;
up->peernamelen = SOCKSIZE;
up->peername = mallocw(up->peernamelen);
memcpy(up->peername,&sock,SOCKSIZE);
up->cb.tcb = tcb;
tcb->user = s;
/* Set the normal upcalls */
tcb->r_upcall = s_trcall;
tcb->t_upcall = s_ttcall;
tcb->s_upcall = s_tscall;
/* And spawn the server task */
newproc(in->name,in->stack,in->task,s,NULL,NULL,0);
}
/* Close down a TCP server created earlier by inet_start */
int
stop_tcp(uint16 port)
{
struct inet *in,*inprev;
inprev = NULLINET;
for(in = Inet_list;in != NULLINET;inprev=in,in = in->next)
if(in->tcb->conn.local.port == port)
break;
if(in == NULLINET)
return -1;
close_tcp(in->tcb);
free(in->name);
if(inprev != NULLINET)
inprev->next = in->next;
else
Inet_list = in->next;
free(in);
return 0;
}