summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/snoopy/aoerr.c
blob: 4ef7f44fa9abae467cb0ceeb48c8d61030ab49b1 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <u.h>
#include <libc.h>
#include <ip.h>
#include "dat.h"
#include "protos.h"

typedef struct {
	uchar	cmd;
	uchar	nea;
} Hdr;

enum {
	Ocmd,
	Onea,
	Oea,

	Hsize	= 2,
};

static Field p_fields[] = {
	{"cmd",	Fnum,	Ocmd,	"command",	},
	{"nea",	Fnum,	Onea,	"ea count",	},
	{"ea",	Fnum,	Onea,	"ethernet addr", },
	nil
};

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

static int
p_filter(Filter *f, Msg *m)
{
	uchar buf[6];
	int i;
	Hdr *h;

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

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

	switch(f->subop){
	case Ocmd:
		return h->cmd == f->ulv;
	case Onea:
		return h->nea == f->ulv;
	case Oea:
		if(m->pe - m->ps < 6*h->nea)
			return 0;
		for(i = 0; i < 6; i++)
			buf[i] = f->ulv >> ((5 - i)*8);
		for(i = 0; i < h->nea; i++)
			if(memcmp(m->ps + 6*i, buf, 6) == 0)
				return 1;
		return 0;
	}
	return 0;
}

static char *ctab[] = {
	"read",
	"write",
	"force",
};

static int
p_seprint(Msg *m)
{
	char *s;
	int i;
	Hdr *h;

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

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

	/* no next protocol */
	m->pr = nil;

	s = "unk";
	if(h->cmd < nelem(ctab))
		s = ctab[h->cmd];
	m->p = seprint(m->p, m->e, "cmd=%d %s nea=%d", h->cmd, s, h->nea);
	for(i = 0;; i++){
		if(h->nea < i)
			break;
		if(i == 3){
			m->p = seprint(m->p, m->e, " ...");
			break;
		}
		if(m->pe - m->ps < 6*i){
			m->p = seprint(m->p, m->e, " *short*");
			break;
		}
		m->p = seprint(m->p, m->e, " %E", m->pe + 6*i);
	}
	m->p = seprint(m->p, m->e, "\n");
	return 0;
}

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