changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > plan9front / changeset: sshnet: get rid of Announced state, simplify

changeset 7132: f7a2615a3bca
parent 7131: 764656b4f929
child 7133: 1b8391d51793
author: cinap_lenrek@felloff.net
date: Wed, 03 Apr 2019 23:49:22 +0200
files: sys/src/cmd/sshnet.c
description: sshnet: get rid of Announced state, simplify
     1.1--- a/sys/src/cmd/sshnet.c
     1.2+++ b/sys/src/cmd/sshnet.c
     1.3@@ -45,7 +45,6 @@ enum
     1.4 {
     1.5 	Closed,
     1.6 	Dialing,
     1.7-	Announced,
     1.8 	Listen,
     1.9 	Established,
    1.10 	Teardown,
    1.11@@ -55,7 +54,6 @@ enum
    1.12 char *statestr[] = {
    1.13 	"Closed",
    1.14 	"Dialing",
    1.15-	"Announced",
    1.16 	"Listen",
    1.17 	"Established",
    1.18 	"Teardown",
    1.19@@ -310,14 +308,26 @@ getclient(int num)
    1.20 }
    1.21 
    1.22 Client*
    1.23-getlistener(char *host, int port)
    1.24+acceptclient(char *lhost, int lport, char *rhost, int rport)
    1.25 {
    1.26+	Client *c, *nc;
    1.27 	int i;
    1.28 
    1.29-	USED(host);
    1.30 	for(i = 0; i < nclient; i++){
    1.31-		if(client[i]->state == Listen && client[i]->lport == port)
    1.32-			return client[i];
    1.33+		c = client[i];
    1.34+		if(c->state == Listen && c->lport == lport && c->wq != nil){
    1.35+			nc = client[newclient()];
    1.36+			nc->wq = c->wq;
    1.37+			c->wq = nc->wq->aux;
    1.38+			nc->wq->aux = nil;
    1.39+			free(nc->lhost);
    1.40+			nc->lhost = lhost;
    1.41+			nc->lport = lport;
    1.42+			free(nc->rhost);
    1.43+			nc->rhost = rhost;
    1.44+			nc->rport = rport;
    1.45+			return nc;
    1.46+		}
    1.47 	}
    1.48 	return nil;
    1.49 }
    1.50@@ -493,15 +503,13 @@ closeclient(Client *c)
    1.51 		c->state = Closed;
    1.52 		sendmsg(pack(nil, "bu", MSG_CHANNEL_CLOSE, c->servernum));
    1.53 		break;
    1.54-	case Announced:
    1.55+	case Listen:
    1.56+		c->state = Closed;
    1.57 		sendmsg(pack(nil, "bsbsu", MSG_GLOBAL_REQUEST,
    1.58 			"cancel-tcpip-forward", 20,
    1.59 			0,
    1.60 			c->lhost, strlen(c->lhost),
    1.61 			c->lport));
    1.62-		/* wet floor */
    1.63-	case Listen:
    1.64-		c->state = Closed;
    1.65 		break;
    1.66 	}
    1.67 	while((m = c->mq) != nil){
    1.68@@ -859,7 +867,7 @@ ctlwrite(Req *r, Client *c)
    1.69 		free(c->rhost);
    1.70 		c->rhost = estrdup9p("::");
    1.71 		c->rport = 0;
    1.72-		c->state = Announced;
    1.73+		c->state = Listen;
    1.74 		sendmsg(pack(nil, "bsbsu", MSG_GLOBAL_REQUEST,
    1.75 			"tcpip-forward", 13, 0,
    1.76 			c->lhost, strlen(c->lhost), c->lport));
    1.77@@ -1045,20 +1053,11 @@ fsopen(Req *r)
    1.78 		respond(r, nil);
    1.79 		break;
    1.80 	case Qlisten:
    1.81-		if(client[NUM(path)]->state != Announced){
    1.82-			respond(r, "not announced");
    1.83+		if(client[NUM(path)]->state != Listen){
    1.84+			respond(r, "no address set");
    1.85 			break;
    1.86 		}
    1.87-		n = newclient();
    1.88-		free(client[n]->lhost);
    1.89-		client[n]->lhost = estrdup9p(client[NUM(path)]->lhost);
    1.90-		client[n]->lport = client[NUM(path)]->lport;
    1.91-		r->fid->qid.path = PATH(Qctl, n);
    1.92-		r->ofcall.qid.path = r->fid->qid.path;
    1.93-		r->aux = nil;
    1.94-		client[n]->wq = r;
    1.95-		client[n]->ref++;
    1.96-		client[n]->state = Listen;
    1.97+		queuewreq(client[NUM(path)], r);
    1.98 		break;
    1.99 	case Qclone:
   1.100 		n = newclient();
   1.101@@ -1199,19 +1198,13 @@ handlemsg(Msg *m)
   1.102 		}
   1.103 		lhost = smprint("%.*s", utfnlen(lhost, ln), lhost);
   1.104 		rhost = smprint("%.*s", utfnlen(rhost, rn), rhost);
   1.105-		c = getlistener(lhost, lport);
   1.106+		c = acceptclient(lhost, lport, rhost, rport);
   1.107 		if(c == nil){
   1.108 			free(lhost);
   1.109 			free(rhost);
   1.110 			n = 2, s = "connection refused";
   1.111 			goto Reject;
   1.112 		}
   1.113-		free(c->lhost);
   1.114-		c->lhost = lhost;
   1.115-		c->lport = lport;
   1.116-		free(c->rhost);
   1.117-		c->rhost = rhost;
   1.118-		c->rport = rport;
   1.119 		c->servernum = chan;
   1.120 		c->recvwin = WinPackets*MaxPacket;
   1.121 		c->recvacc = 0;
   1.122@@ -1219,12 +1212,13 @@ handlemsg(Msg *m)
   1.123 		c->sendpkt = pkt;
   1.124 		c->sendwin = win;
   1.125 		c->state = Established;
   1.126+
   1.127 		sendmsg(pack(nil, "buuuu", MSG_CHANNEL_OPEN_CONFIRMATION,
   1.128 			c->servernum, c->num, c->recvwin, MaxPacket));
   1.129-		if(c->wq == nil){
   1.130-			teardownclient(c);
   1.131-			break;
   1.132-		}
   1.133+
   1.134+		c->ref++;
   1.135+		c->wq->fid->qid.path = PATH(Qctl, c->num);
   1.136+		c->wq->ofcall.qid.path = c->wq->fid->qid.path;
   1.137 		respond(c->wq, nil);
   1.138 		c->wq = nil;
   1.139 		break;