]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/ip/snoopy/cec.c
snoopy(8): avoid extra spaces in dhcp filter output
[plan9front.git] / sys / src / cmd / ip / snoopy / cec.c
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   type;
9         uchar   conn;
10         uchar   seq;
11         uchar   len;
12 }Hdr;
13
14 enum{
15         Hsize   = 4,
16 };
17
18 enum{
19         Otype,
20         Oconn,
21         Oseq,
22         Olen,
23 };
24
25 static Field p_fields[] =
26 {
27         {"type",        Fnum,   Otype,          "type", },
28         {"conn",        Fnum,   Oconn,          "conn", },
29         {"seq",         Fnum,   Oseq,           "seq",  },
30         {"len",         Fnum,   Olen,           "len",  },
31         {0}
32 };
33
34 static void
35 p_compile(Filter *f)
36 {
37         if(f->op == '='){
38                 compile_cmp(aoe.name, f, p_fields);
39                 return;
40         }
41         sysfatal("unknown aoe field: %s", f->s);
42 }
43
44 static int
45 p_filter(Filter *f, Msg *m)
46 {
47         Hdr *h;
48
49         if(m->pe - m->ps < Hsize)
50                 return 0;
51
52         h = (Hdr*)m->ps;
53         m->ps += Hsize;
54
55         switch(f->subop){
56         case Otype:
57                 return h->type == f->ulv;
58         case Oconn:
59                 return h->conn = f->ulv;
60         case Oseq:
61                 return h->seq = f->ulv;
62         case Olen:
63                 return h->len = f->ulv;
64         }
65         return 0;
66 }
67
68 static char* ttab[] = {
69         "Tinita",
70         "Tinitb",
71         "Tinitc",
72         "Tdata",
73         "Tack",
74         "Tdiscover",
75         "Toffer",
76         "Treset",
77 };
78
79 static int
80 p_seprint(Msg *m)
81 {
82         char *s, *p, buf[4];
83         Hdr *h;
84
85         if(m->pe - m->ps < Hsize)
86                 return 0;
87
88         h = (Hdr*)m->ps;
89         m->ps += Hsize;
90
91         m->pr = nil;
92
93         if(h->type < nelem(ttab))
94                 s = ttab[h->type];
95         else{
96                 snprint(buf, sizeof buf, "%d", h->type);
97                 s = buf;
98         }
99
100         p = (char*)m->ps;
101         m->p = seprint(m->p, m->e, "type=%s conn=%d seq=%d len=%d %.*s",
102                 s, h->conn, h->seq, h->len,
103                 (int)utfnlen(p, h->len), p);
104         return 0;
105 }
106
107 Proto cec =
108 {
109         "cec",
110         p_compile,
111         p_filter,
112         p_seprint,
113         nil,
114         nil,
115         p_fields,
116         defaultframer,
117 };