Plan 9 from Bell Labs’s /usr/web/sources/extra/9hist/ip/loopbackmedium.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


## diffname ip/loopbackmedium.c 2001/0117
## diff -e /dev/null /n/emeliedump/2001/0117/sys/src/9/ip/loopbackmedium.c
0a
#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "../port/error.h"

#include "ip.h"

enum
{
	Maxtu=	16*1024,
};

typedef struct LB LB;
struct LB
{
	Proc	*readp;
	Queue	*q;
	Fs	*f;
};

static void loopbackread(void *a);

static void
loopbackbind(Ipifc *ifc, int, char**)
{
	LB *lb;

	lb = smalloc(sizeof(*lb));
	lb->f = ifc->conv->p->f;
	lb->q = qopen(128*1024, 1, nil, nil);
	ifc->arg = lb;

	kproc("loopbackread", loopbackread, ifc);

}

static void
loopbackunbind(Ipifc *ifc)
{
	LB *lb = ifc->arg;

	if(lb->readp)
		postnote(lb->readp, 1, "unbind", 0);

	/* wait for reader to die */
	while(lb->readp != 0)
		tsleep(&up->sleep, return0, 0, 300);

	/* clean up */
	qfree(lb->q);
	free(lb);
}

static void
loopbackbwrite(Ipifc *ifc, Block *bp, int, uchar*)
{
	LB *lb;

	lb = ifc->arg;
	if(qpass(lb->q, bp) < 0)
		ifc->outerr++;
	ifc->out++;
}

static void
loopbackread(void *a)
{
	Ipifc *ifc;
	Block *bp;
	LB *lb;

	ifc = a;
	lb = ifc->arg;
	lb->readp = up;	/* hide identity under a rock for unbind */
	if(waserror()){
		lb->readp = 0;
		pexit("hangup", 1);
	}
	for(;;){
		bp = qbread(lb->q, Maxtu);
		if(bp == nil)
			continue;
		ifc->in++;
		if(!canrlock(ifc)){
			freeb(bp);
			continue;
		}
		if(waserror()){
			runlock(ifc);
			nexterror();
		}
		if(ifc->lifc == nil)
			freeb(bp);
		else
			ipiput(lb->f, ifc->lifc->local, bp);
		runlock(ifc);
		poperror();
	}
}

Medium loopbackmedium =
{
.hsize=		0,
.minmtu=	0,
.maxmtu=	Maxtu,
.maclen=	0,
.name=		"loopback",
.bind=		loopbackbind,
.unbind=	loopbackunbind,
.bwrite=	loopbackbwrite,
};

void
loopbackmediumlink(void)
{
	addipmedium(&loopbackmedium);
}
.
## diffname ip/loopbackmedium.c 2001/0623
## diff -e /n/emeliedump/2001/0117/sys/src/9/ip/loopbackmedium.c /n/emeliedump/2001/0623/sys/src/9/ip/loopbackmedium.c
97c
			ipiput(lb->f, ifc, bp);
.
## diffname ip/loopbackmedium.c 2002/0507
## diff -e /n/emeliedump/2001/0623/sys/src/9/ip/loopbackmedium.c /n/emeliedump/2002/0507/sys/src/9/ip/loopbackmedium.c
97c
			ipiput4(lb->f, ifc, bp);
.
## diffname ip/loopbackmedium.c 2002/0711
## diff -e /n/emeliedump/2002/0507/sys/src/9/ip/loopbackmedium.c /n/emeliedump/2002/0711/sys/src/9/ip/loopbackmedium.c
32c
	lb->q = qopen(128*1024, Qmsg, nil, nil);
.
## diffname ip/loopbackmedium.c 2003/0209
## diff -e /n/emeliedump/2002/0711/sys/src/9/ip/loopbackmedium.c /n/emeliedump/2003/0209/sys/src/9/ip/loopbackmedium.c
106,107c
.mintu=		0,
.maxtu=		Maxtu,
.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].