~kris/9p

9hist

ref: 2e8fd57dcdcfdee6467c48d030a10f4cff786954 9hist/port/tcpif.c -rw-r--r-- 1.3 KiB
2e8fd57d — David du Colombier Plan 9 from Bell Labs 1991-08-13 35 years ago
                                                                                
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
#include	"u.h"
#include	"lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"errno.h"
#include 	"arp.h"
#include 	"ipdat.h"

extern int tcpdbg;
#define DPRINT	if(tcpdbg) print

void
state_upcall(Ipconv *s, char oldstate, char newstate)
{
	Block *bp;
	int len;
	char *ptr = 0;

	SET(len);

	DPRINT("state_upcall: %s -> %s err %d\n", 
	      tcpstate[oldstate], tcpstate[newstate], s->err);

	if(oldstate == newstate)
		return;

	switch(newstate) {
	case CLOSED:
		s->psrc = 0;
		s->pdst = 0;
		s->dst = 0;
	case CLOSE_WAIT:		/* Remote closes */
		if(s->readq == 0)
			break;
		if(s->err) {
			ptr = errstrtab[s->err];
			len = strlen(ptr)+1;
			bp = allocb(len);
		}
		else
			bp = allocb(0);

		if(bp) {
			if(ptr) {
				strcpy((char *)bp->wptr, ptr);
				bp->wptr += len;
			}
			bp->flags |= S_DELIM;
			bp->type = M_HANGUP;
			PUTNEXT(s->readq, bp);
		}
	}
}

void
open_tcp(Ipconv *s, int mode, ushort window, char tos)
{
	Tcpctl *tcb = &s->tcpctl;

	if(tcb->state != CLOSED)
		return;

	init_tcpctl(s);

	tcb->window = tcb->rcv.wnd = window;
	tcb->tos = tos;

	switch(mode){
	case TCP_PASSIVE:
		tcb->flags |= CLONE;
		setstate(s, LISTEN);
		break;
	case TCP_ACTIVE:
		/* Send SYN, go into SYN_SENT state */
		tcb->flags |= ACTIVE;
		qlock(tcb);
		send_syn(tcb);
		setstate(s, SYN_SENT);
		tcp_output(s);
		qunlock(tcb);
		break;
	}
}