Plan 9 from Bell Labs’s /usr/web/sources/patch/applied/snoopy-aoe/cec.c.new

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


#include <u.h>
#include <libc.h>
#include <ip.h>
#include "dat.h"
#include "protos.h"

typedef struct{
	uchar	type;
	uchar	conn;
	uchar	seq;
	uchar	len;
}Hdr;

enum{
	Hsize	= 4,
};

enum{
	Otype,
	Oconn,
	Oseq,
	Olen,
};

static Field p_fields[] = 
{
	{"type",		Fnum,	Otype,		"type",	},
	{"conn",		Fnum,	Oconn,		"conn",	},
	{"seq",		Fnum,	Oseq,		"seq",	},
	{"len",		Fnum,	Olen,		"len",	},
	{0}
};

static void
p_compile(Filter *f)
{
	if(f->op == '='){
		compile_cmp(aoe.name, f, p_fields);
		return;
	}
	sysfatal("unknown aoe field: %s", f->s);
}

static int
p_filter(Filter *f, Msg *m)
{
	Hdr *h;

	if(m->pe-m->ps < Hsize)
		return 0;

	h = (Hdr*)m->ps;
	m->ps += Hsize;

	switch(f->subop){
	case Otype:
		return h->type == f->ulv;
	case Oconn:
		return h->conn = f->ulv;
	case Oseq:
		return h->seq = f->ulv;
	case Olen:
		return h->len = f->ulv;
	}
	return 0;
}

static char* ttab[] = {
	"Tinita",
	"Tinitb",
	"Tinitc",
	"Tdata",
	"Tack",
	"Tdiscover",
	"Toffer",
	"Treset",
};

static int
p_seprint(Msg *m)
{
	Hdr *h;
	char *s, buf[4];

	if(m->pe-m->ps < Hsize)
		return 0;

	h = (Hdr*)m->ps;
	m->ps += Hsize;

	m->pr = nil;

	if(h->type < nelem(ttab))
		s = ttab[h->type];
	else{
		snprint(buf, sizeof buf, "%d", h->type);
		s = buf;
	}

	m->p = seprint(m->p, m->e, "type=%s conn=%d seq=%d len=%d %.*s",
		s, h->conn, h->seq, h->len,
		h->len, (char*)m->ps);
	return 0;
}

Proto cec =
{
	"cec",
	p_compile,
	p_filter,
	p_seprint,
	nil,
	nil,
	p_fields,
	defaultframer,
};

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.