xref: /plan9/sys/src/cmd/ip/snoopy/aoecmd.c (revision e6dcbf51e935975016093545b6eab69976b6e257)
1 #include <u.h>
2 #include <libc.h>
3 #include <ip.h>
4 #include "dat.h"
5 #include "protos.h"
6 
7 typedef struct{
8 	uchar	bc[2];
9 	uchar	fw[2];
10 	uchar	sc;
11 	uchar	ccmd;
12 	uchar	len[2];
13 }Hdr;
14 
15 enum{
16 	Hsize	= 8,
17 };
18 
19 enum{
20 	Ocmd,
21 };
22 
23 static Field p_fields[] =
24 {
25 	{"cmd",		Fnum,	Ocmd,		"cmd",	},
26 	{0}
27 };
28 
29 static void
p_compile(Filter * f)30 p_compile(Filter *f)
31 {
32 	if(f->op == '='){
33 		compile_cmp(aoecmd.name, f, p_fields);
34 		return;
35 	}
36 	sysfatal("unknown aoecmd field: %s", f->s);
37 }
38 
39 static int
p_filter(Filter * f,Msg * m)40 p_filter(Filter *f, Msg *m)
41 {
42 	Hdr *h;
43 
44 	if(m->pe - m->ps < Hsize)
45 		return 0;
46 
47 	h = (Hdr*)m->ps;
48 	m->ps += Hsize;
49 
50 	switch(f->subop){
51 	case Ocmd:
52 		return (h->ccmd & 0xf) == f->ulv;
53 	}
54 	return 0;
55 }
56 
57 static int
p_seprint(Msg * m)58 p_seprint(Msg *m)
59 {
60 	Hdr *h;
61 
62 	if(m->pe - m->ps < Hsize)
63 		return 0;
64 
65 	h = (Hdr*)m->ps;
66 	m->ps += Hsize;
67 
68 	/* no next protocol */
69 	m->pr = nil;
70 
71 	m->p = seprint(m->p, m->e, "bc=%d fw=%.4x sc=%d ver=%d ccmd=%d len=%d cfg=",
72 		NetS(h->bc), NetS(h->fw), h->sc, h->ccmd >> 4, h->ccmd & 0xf,
73 		NetS(h->len));
74 	m->p = seprint(m->p, m->e, "%.*s", NetS(h->len), (char*)m->ps);
75 	return 0;
76 }
77 
78 Proto aoecmd =
79 {
80 	"aoecmd",
81 	p_compile,
82 	p_filter,
83 	p_seprint,
84 	nil,
85 	nil,
86 	p_fields,
87 	defaultframer,
88 };
89