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

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


## diffname ip/netlog.c 1997/0327
## diff -e /dev/null /n/emeliedump/1997/0327/sys/src/brazil/ip/netlog.c
0a
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"../port/error.h"
#include	"../ip/ip.h"

int logmask;				/* mask of things to debug */
Ipaddr iponly;				/* ip address to print debugging for */

enum {
	Nlog		= 4*1024,
};

/*
 *  action log
 */
typedef struct Log {
	Lock;
	int	opens;
	char*	buf;
	char	*end;
	char	*rptr;
	int	len;

	QLock;
	Rendez;
} Log;
static Log alog;

typedef struct Logflag {
	char*	name;
	int	mask;
} Logflag;
static Logflag flags[] =
{
	{ "ppp",	Logppp, },
	{ "ip",		Logip, },
	{ "fs",		Logfs, },
	{ "tcp",	Logtcp, },
	{ "il",		Logil, },
	{ "icmp",	Logicmp, },
	{ "udp",	Logudp, },
	{ "compress",	Logcompress, },
	{ "ilmsg",	Logil|Logilmsg, },
	{ "gre",	Loggre, },
	{ "tcpmsg",	Logtcp|Logtcpmsg, },
	{ nil,		0, },
};

static char Ebadnetctl[] = "unknown netlog ctl message";

void
netlogopen(void)
{
	lock(&alog);
	if(waserror()){
		unlock(&alog);
		nexterror();
	}
	if(alog.opens == 0){
		if(alog.buf == nil)
			alog.buf = malloc(Nlog);
		alog.rptr = alog.buf;
		alog.end = alog.buf + Nlog;
	}
	alog.opens++;
	unlock(&alog);
	poperror();
}

void
netlogclose(void)
{
	lock(&alog);
	if(waserror()){
		unlock(&alog);
		nexterror();
	}
	alog.opens--;
	if(alog.opens == 0){
		free(alog.buf);
		alog.buf = nil;
	}
	unlock(&alog);
	poperror();
}

static int
netlogready(void*)
{
	return alog.len;
}

long
netlogread(void* a, ulong, long n)
{
	int i, d;
	char *p, *rptr;

	qlock(&alog);
	if(waserror()){
		qunlock(&alog);
		nexterror();
	}

	for(;;){
		lock(&alog);
		if(alog.len){
			if(n > alog.len)
				n = alog.len;
			d = 0;
			rptr = alog.rptr;
			alog.rptr += n;
			if(alog.rptr >= alog.end){
				d = alog.rptr - alog.end;
				alog.rptr = alog.buf + d;
			}
			alog.len -= n;
			unlock(&alog);

			i = n;
			p = a;
			if(d){
				memmove(p, rptr, d);
				i -= d;
				p += d;
				rptr = alog.buf;
			}
			memmove(p, rptr, i);
			break;
		}
		else
			unlock(&alog);

		sleep(&alog, netlogready, 0);
	}

	qunlock(&alog);
	poperror();

	return n;
}

char*
netlogctl(char* s, int len)
{
	int i, n, set;
	Logflag *f;
	char *fields[10], *p, addr[4], buf[256];

	if(len == 0)
		return Ebadnetctl;

	if(len >= sizeof(buf))
		len = sizeof(buf)-1;
	strncpy(buf, s, len);
	buf[len] = 0;
	if(len > 0 && buf[len-1] == '\n')
		buf[len-1] = 0;

	n = parsefields(buf, fields, 10, " ");
	if(n < 2)
		return Ebadnetctl;

	if(strcmp("set", fields[0]) == 0)
		set = 1;
	else if(strcmp("clear", fields[0]) == 0)
		set = 0;
	else if(strcmp("only", fields[0]) == 0){
		iponly = parseip(addr, fields[1]);
		return nil;
	} else
		return Ebadnetctl;

	p = strchr(fields[n-1], '\n');
	if(p)
		*p = 0;

	for(i = 1; i < n; i++){
		for(f = flags; f->name; f++)
			if(strcmp(f->name, fields[i]) == 0)
				break;
		if(f->name == nil)
			continue;
		if(set)
			logmask |= f->mask;
		else
			logmask &= ~f->mask;
	}

	return nil;
}

void
netlog(int mask, char *fmt, ...)
{
	char buf[128], *t, *f;
	int i, n;
	va_list arg;

	if(alog.opens == 0 || !(logmask & mask))
		return;

	va_start(arg, fmt);
	n = doprint(buf, buf+sizeof(buf), fmt, arg) - buf;
	va_end(arg);

	if(alog.opens == 0)
		return;

	lock(&alog);
	i = alog.len + n - Nlog;
	if(i > 0){
		alog.len -= i;
		alog.rptr += i;
		if(alog.rptr >= alog.end)
			alog.rptr = alog.buf + (alog.rptr - alog.end);
	}
	t = alog.rptr + alog.len;
	f = buf;
	alog.len += n;
	while(n-- > 0){
		if(t >= alog.end)
			t = alog.buf + (t - alog.end);
		*t++ = *f++;
	}
	unlock(&alog);

	wakeup(&alog);
}
.
## diffname ip/netlog.c 1997/0423
## diff -e /n/emeliedump/1997/0327/sys/src/brazil/ip/netlog.c /n/emeliedump/1997/0423/sys/src/brazil/ip/netlog.c
151c
	char *fields[10], *p, buf[256];
	uchar addr[Ipaddrlen];
.
## diffname ip/netlog.c 1997/0806
## diff -e /n/emeliedump/1997/0423/sys/src/brazil/ip/netlog.c /n/emeliedump/1997/0806/sys/src/brazil/ip/netlog.c
48a
	{ "udpmsg",	Logudp|Logudpmsg, },
	{ "ipmsg",	Logip|Logipmsg, },
.
## diffname ip/netlog.c 1997/0815
## diff -e /n/emeliedump/1997/0806/sys/src/brazil/ip/netlog.c /n/emeliedump/1997/0815/sys/src/brazil/ip/netlog.c
154c
	uchar addr[IPaddrlen];
.
## diffname ip/netlog.c 1997/1104
## diff -e /n/emeliedump/1997/0815/sys/src/brazil/ip/netlog.c /n/emeliedump/1997/1104/sys/src/brazil/ip/netlog.c
66a
			if(alog.buf == nil)
				error(Enomem);
		}
.
65c
		if(alog.buf == nil){
.
## diffname ip/netlog.c 1998/0306
## diff -e /n/emeliedump/1997/1104/sys/src/brazil/ip/netlog.c /n/emeliedump/1998/0306/sys/src/brazil/ip/netlog.c
214a

print("%s", buf);
.
209c
	if(!(logmask & mask))
.
178c
		parseip(iponly, fields[1]);
		if(ipcmp(iponly, IPnoaddr) == 0)
			iponlyset = 0;
		else
			iponlyset = 1;
.
67,69d
65c
		if(alog.buf == nil)
.
10c
uchar iponly[IPaddrlen];		/* ip address to print debugging for */
int iponlyset;
.
## diffname ip/netlog.c 1998/0307
## diff -e /n/emeliedump/1998/0306/sys/src/brazil/ip/netlog.c /n/emeliedump/1998/0307/sys/src/brazil/ip/netlog.c
155d
## diffname ip/netlog.c 1998/0313
## diff -e /n/emeliedump/1998/0307/sys/src/brazil/ip/netlog.c /n/emeliedump/1998/0313/sys/src/brazil/ip/netlog.c
240c
	wakeup(f->alog);
.
238c
	unlock(f->alog);
.
234,236c
		if(t >= f->alog->end)
			t = f->alog->buf + (t - f->alog->end);
		*t++ = *fp++;
.
230,232c
	t = f->alog->rptr + f->alog->len;
	fp = buf;
	f->alog->len += n;
.
225,228c
		f->alog->len -= i;
		f->alog->rptr += i;
		if(f->alog->rptr >= f->alog->end)
			f->alog->rptr = f->alog->buf + (f->alog->rptr - f->alog->end);
.
222,223c
	lock(f->alog);
	i = f->alog->len + n - Nlog;
.
217,219c
	if(f->alog->opens == 0)
.
210c
	if(!(f->alog->logmask & mask))
.
206c
	char buf[128], *t, *fp;
.
204c
netlog(Fs *f, int mask, char *fmt, ...)
.
197c
			f->alog->logmask &= ~fp->mask;
.
195c
			f->alog->logmask |= fp->mask;
.
192c
		if(fp->name == nil)
.
189,190c
		for(fp = flags; fp->name; fp++)
			if(strcmp(fp->name, fields[i]) == 0)
.
179c
			f->alog->iponlyset = 1;
.
175,177c
		parseip(f->alog->iponly, fields[1]);
		if(ipcmp(f->alog->iponly, IPnoaddr) == 0)
			f->alog->iponlyset = 0;
.
153c
	Logflag *fp;
.
150c
netlogctl(Fs *f, char* s, int len)
.
143c
	qunlock(f->alog);
.
140c
		sleep(f->alog, netlogready, 0);
.
138c
			unlock(f->alog);
.
132c
				rptr = f->alog->buf;
.
123,124c
			f->alog->len -= n;
			unlock(f->alog);
.
117,121c
			rptr = f->alog->rptr;
			f->alog->rptr += n;
			if(f->alog->rptr >= f->alog->end){
				d = f->alog->rptr - f->alog->end;
				f->alog->rptr = f->alog->buf + d;
.
112,115c
		lock(f->alog);
		if(f->alog->len){
			if(n > f->alog->len)
				n = f->alog->len;
.
107c
		qunlock(f->alog);
.
105c
	qlock(f->alog);
.
100c
netlogread(Fs *f, void *a, ulong, long n)
.
96c
	Fs *f = a;

	return f->alog->len;
.
94c
netlogready(void *a)
.
89c
	unlock(f->alog);
.
84,87c
	f->alog->opens--;
	if(f->alog->opens == 0){
		free(f->alog->buf);
		f->alog->buf = nil;
.
81c
		unlock(f->alog);
.
79c
	lock(f->alog);
.
77c
netlogclose(Fs *f)
.
71,72c
	f->alog->opens++;
	unlock(f->alog);
.
65,69c
	if(f->alog->opens == 0){
		if(f->alog->buf == nil)
			f->alog->buf = malloc(Nlog);
		f->alog->rptr = f->alog->buf;
		f->alog->end = f->alog->buf + Nlog;
.
62c
		unlock(f->alog);
.
60c
	f->alog = smalloc(sizeof(Log));
}

void
netlogopen(Fs *f)
{
	lock(f->alog);
.
58c
netloginit(Fs *f)
.
36a

.
30,31c
};
.
27a
	int	logmask;			/* mask of things to debug */
	uchar	iponly[IPaddrlen];		/* ip address to print debugging for */
	int	iponlyset;

.
20c
struct Log {
.
9,12d
## diffname ip/netlog.c 1998/0314
## diff -e /n/emeliedump/1998/0313/sys/src/brazil/ip/netlog.c /n/emeliedump/1998/0314/sys/src/brazil/ip/netlog.c
148c
		sleep(f->alog, netlogready, f);
.
## diffname ip/netlog.c 1999/0316
## diff -e /n/emeliedump/1998/0314/sys/src/brazil/ip/netlog.c /n/emeliedump/1999/0316/sys/src/brazil/ip/netlog.c
161c
	Netlogflag *fp;
.
60c
	f->alog = smalloc(sizeof(Netlog));
.
51a
	{ "esp",	Logesp, },
.
37c
static Netlogflag flags[] =
.
35c
} Netlogflag;
.
32c
typedef struct Netlogflag {
.
16c
struct Netlog {
.
## diffname ip/netlog.c 1999/0731
## diff -e /n/emeliedump/1999/0316/sys/src/brazil/ip/netlog.c /n/emeliedump/1999/0731/sys/src/brazil/ip/netlog.c
143a
			memmove(p+i, f->alog->buf, d);
.
137,142d
135c
			i = n-d;
.
## diffname ip/netlog.c 2000/0308
## diff -e /n/emeliedump/1999/0731/sys/src/brazil/ip/netlog.c /n/emeliedump/2000/0308/sys/src/9/ip/netlog.c
170c
	n = getfields(buf, fields, 10, 1, " ");
.
## diffname ip/netlog.c 2001/1117
## diff -e /n/emeliedump/2000/0308/sys/src/9/ip/netlog.c /n/emeliedump/2001/1117/sys/src/9/ip/netlog.c
170c
	n = tokenize(buf, fields, 10);
.
## diffname ip/netlog.c 2001/1120
## diff -e /n/emeliedump/2001/1117/sys/src/9/ip/netlog.c /n/emeliedump/2001/1120/sys/src/9/ip/netlog.c
204c
	free(cb);
	poperror();
.
194c
			if(strcmp(fp->name, cb->f[i]) == 0)
.
188,190c
	default:
		cmderror(cb, "unknown ip control message");
	}
.
184,186c
		free(cb);
		return;
.
178,179c
		break;

	case CMonly:
		parseip(f->alog->iponly, cb->f[1]);
.
176c
		break;

	case CMclear:
.
174c
	SET(set);

	switch(ct->index){
	case CMset:
.
170,172c
	ct = lookupcmd(cb, routecmd, nelem(routecmd));
.
163,168c
	if(cb->nf < 2)
		error(Ebadnetctl);
.
160,161c
	cb = parsecmd(s, n);
	if(waserror()){
		free(cb);
		nexterror();
	}
.
158c
	Cmdbuf *cb;
	Cmdtab *ct;
.
156c
	int i, set;
.
153,154c
void
netlogctl(Fs *f, char* s, int n)
.
57a
enum
{
	CMset,
	CMclear,
	CMonly,
};

static
Cmdtab routecmd[] = {
	CMset,		"set",		0,
	CMclear,	"clear",	0,
	CMonly,		"only",		0,
};

.
56c
char Ebadnetctl[] = "too few arguments for netlog control message";
.
## diffname ip/netlog.c 2002/0217
## diff -e /n/emeliedump/2001/1120/sys/src/9/ip/netlog.c /n/emeliedump/2002/0217/sys/src/9/ip/netlog.c
241a

	va_start(arg, fmt);
	n = vseprint(buf, buf+sizeof(buf), fmt, arg) - buf;
	va_end(arg);
.
236,239d
## diffname ip/netlog.c 2002/0306
## diff -e /n/emeliedump/2002/0217/sys/src/9/ip/netlog.c /n/emeliedump/2002/0306/sys/src/9/ip/netlog.c
210c
	for(i = 1; i < cb->nf; i++){
.
## diffname ip/netlog.c 2002/0507
## diff -e /n/emeliedump/2002/0306/sys/src/9/ip/netlog.c /n/emeliedump/2002/0507/sys/src/9/ip/netlog.c
210c
	for(i = 1; i < n; i++){
.
## diffname ip/netlog.c 2002/0615
## diff -e /n/emeliedump/2002/0507/sys/src/9/ip/netlog.c /n/emeliedump/2002/0615/sys/src/9/ip/netlog.c
210c
	for(i = 1; i < cb->nf; i++){
.
## diffname ip/netlog.c 2002/0713
## diff -e /n/emeliedump/2002/0615/sys/src/9/ip/netlog.c /n/emeliedump/2002/0713/sys/src/9/ip/netlog.c
49c
	{ "tcpwin",	Logtcp|Logtcpwin, },
	{ "tcprxmt",	Logtcp|Logtcprxmt, },
.

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].