xref: /plan9-contrib/sys/src/cmd/ip/snoopy/ttls.c (revision ed397113cdcf7aafea372b730bbcd17b05ee3f60)
1 #include <u.h>
2 #include <libc.h>
3 #include <ip.h>
4 #include "dat.h"
5 #include "protos.h"
6 
7 typedef struct Hdr	Hdr;
8 struct Hdr
9 {
10 	uchar	flags;
11 	uchar	ln[4];	/* optional, present if L flag set*/
12 };
13 
14 enum
15 {
16 	FLHDR=	1,	/* sizeof(flags) */
17 	LNHDR=	4,	/* sizeof(ln) */
18 };
19 
20 enum
21 {
22 	FlagL = 1<<7,
23 	FlagM = 1<<6,
24 	FlagS = 1<<5,
25 	Version = (1<<2)|(1<<1)|(1<<0),
26 };
27 
28 static Mux p_mux[] =
29 {
30 	{ "dump", 0, },
31 	{ 0 }
32 };
33 
34 static char*
flags(int f)35 flags(int f)
36 {
37 	static char fl[20];
38 	char *p;
39 
40 	p = fl;
41 	if(f & FlagS)
42 		*p++ = 'S';
43 	if(f & FlagM)
44 		*p++ = 'M';
45 	if(f & FlagL)
46 		*p++ = 'L';
47 	*p = 0;
48 	return fl;
49 }
50 
51 static int
p_seprint(Msg * m)52 p_seprint(Msg *m)
53 {
54 	Hdr *h;
55 
56 	if(m->pe - m->ps < FLHDR)
57 		return -1;
58 
59 	h = (Hdr*)m->ps;
60 	m->ps += FLHDR;
61 
62 	if (h->flags & FlagL) {
63 		if(m->pe - m->ps < LNHDR)
64 			return -1;
65 		else
66 			m->ps += LNHDR;
67 	}
68 
69 	/* next protocol  depending on type*/
70 	demux(p_mux, 0, 0, m, &dump);
71 
72 	m->p = seprint(m->p, m->e, "ver=%1d", h->flags & Version);
73 	m->p = seprint(m->p, m->e, " fl=%s", flags(h->flags));
74 
75 	if (h->flags & FlagL)
76 		m->p = seprint(m->p, m->e, " totallen=%1d", NetL(h->ln));
77 
78 	/* these are not in the header, just print them for our convenience */
79 	m->p = seprint(m->p, m->e, " dataln=%1ld", m->pe - m->ps);
80 	if ((h->flags & (FlagL|FlagS|FlagM)) == 0 && m->ps == m->pe)
81 		m->p = seprint(m->p, m->e, " ack");
82 
83 	return 0;
84 }
85 
86 Proto ttls =
87 {
88 	"ttls",
89 	nil,
90 	nil,
91 	p_seprint,
92 	p_mux, /* we need this to get the dump printed */
93 	"%lud",
94 	nil,
95 	defaultframer,
96 };
97