xref: /freebsd/contrib/tcpdump/print-wb.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  */
21 
22 /* \summary: White Board printer */
23 
24 #include <config.h>
25 
26 #include "netdissect-stdinc.h"
27 
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
30 #include "addrtoname.h"
31 #include "extract.h"
32 
33 
34 #if 0
35 /*
36  * Largest packet size.  Everything should fit within this space.
37  * For instance, multiline objects are sent piecewise.
38  */
39 #define MAXFRAMESIZE 1024
40 #endif
41 
42 /*
43  * Multiple drawing ops can be sent in one packet.  Each one starts on a
44  * an even multiple of DOP_ALIGN bytes, which must be a power of two.
45  */
46 #define DOP_ALIGN 4
47 #define DOP_ROUNDUP(x)	roundup2(x, DOP_ALIGN)
48 #define DOP_NEXT(d)\
49 	((const struct dophdr *)((const u_char *)(d) + \
50 				DOP_ROUNDUP(GET_BE_U_2((d)->dh_len) + sizeof(*(d)))))
51 
52 /*
53  * Format of the whiteboard packet header.
54  * The transport level header.
55  */
56 struct pkt_hdr {
57 	nd_uint32_t ph_src;	/* site id of source */
58 	nd_uint32_t ph_ts;	/* time stamp (for skew computation) */
59 	nd_uint16_t ph_version;	/* version number */
60 	nd_uint8_t ph_type;	/* message type */
61 	nd_uint8_t ph_flags;	/* message flags */
62 };
63 
64 /* Packet types */
65 #define PT_DRAWOP	0	/* drawing operation */
66 #define PT_ID		1	/* announcement packet */
67 #define PT_RREQ		2	/* repair request */
68 #define PT_RREP		3	/* repair reply */
69 #define PT_KILL		4	/* terminate participation */
70 #define PT_PREQ         5       /* page vector request */
71 #define PT_PREP         7       /* page vector reply */
72 
73 #if 0
74 #ifdef PF_USER
75 #undef PF_USER			/* {Digital,Tru64} UNIX define this, alas */
76 #endif
77 
78 /* flags */
79 #define PF_USER		0x01	/* hint that packet has interactive data */
80 #define PF_VIS		0x02	/* only visible ops wanted */
81 #endif
82 
83 struct PageID {
84 	nd_uint32_t p_sid;		/* session id of initiator */
85 	nd_uint32_t p_uid;		/* page number */
86 };
87 
88 struct dophdr {
89 	nd_uint32_t	dh_ts;		/* sender's timestamp */
90 	nd_uint16_t	dh_len;		/* body length */
91 	nd_uint8_t	dh_flags;
92 	nd_uint8_t	dh_type;	/* body type */
93 	/* body follows */
94 };
95 /*
96  * Drawing op sub-types.
97  */
98 #define DT_RECT         2
99 #define DT_LINE         3
100 #define DT_ML           4
101 #define DT_DEL          5
102 #define DT_XFORM        6
103 #define DT_ELL          7
104 #define DT_CHAR         8
105 #define DT_STR          9
106 #define DT_NOP          10
107 #define DT_PSCODE       11
108 #define DT_PSCOMP       12
109 #define DT_REF          13
110 #define DT_SKIP         14
111 #define DT_HOLE         15
112 static const struct tok dop_str[] = {
113 	{ DT_RECT,   "RECT"   },
114 	{ DT_LINE,   "LINE"   },
115 	{ DT_ML,     "ML"     },
116 	{ DT_DEL,    "DEL"    },
117 	{ DT_XFORM,  "XFORM"  },
118 	{ DT_ELL,    "ELL"    },
119 	{ DT_CHAR,   "CHAR"   },
120 	{ DT_STR,    "STR"    },
121 	{ DT_NOP,    "NOP"    },
122 	{ DT_PSCODE, "PSCODE" },
123 	{ DT_PSCOMP, "PSCOMP" },
124 	{ DT_REF,    "REF"    },
125 	{ DT_SKIP,   "SKIP"   },
126 	{ DT_HOLE,   "HOLE"   },
127 	{ 0, NULL }
128 };
129 
130 /*
131  * A drawing operation.
132  */
133 struct pkt_dop {
134 	struct PageID pd_page;	/* page that operations apply to */
135 	nd_uint32_t	pd_sseq;	/* start sequence number */
136 	nd_uint32_t	pd_eseq;	/* end sequence number */
137 	/* drawing ops follow */
138 };
139 
140 /*
141  * A repair request.
142  */
143 struct pkt_rreq {
144         nd_uint32_t pr_id;        /* source id of drawops to be repaired */
145         struct PageID pr_page;    /* page of drawops */
146         nd_uint32_t pr_sseq;      /* start seqno */
147         nd_uint32_t pr_eseq;      /* end seqno */
148 };
149 
150 /*
151  * A repair reply.
152  */
153 struct pkt_rrep {
154 	nd_uint32_t pr_id;	/* original site id of ops  */
155 	struct pkt_dop pr_dop;
156 	/* drawing ops follow */
157 };
158 
159 struct id_off {
160         nd_uint32_t id;
161         nd_uint32_t off;
162 };
163 
164 struct pgstate {
165 	nd_uint32_t slot;
166 	struct PageID page;
167 	nd_uint16_t nid;
168 	nd_uint16_t rsvd;
169         /* seqptr's */
170 };
171 
172 /*
173  * An announcement packet.
174  */
175 struct pkt_id {
176 	nd_uint32_t pi_mslot;
177         struct PageID    pi_mpage;        /* current page */
178 	struct pgstate pi_ps;
179         /* seqptr's */
180         /* null-terminated site name */
181 };
182 
183 struct pkt_preq {
184         struct PageID  pp_page;
185         nd_uint32_t  pp_low;
186         nd_uint32_t  pp_high;
187 };
188 
189 struct pkt_prep {
190         nd_uint32_t  pp_n;           /* size of pageid array */
191         /* pgstate's follow */
192 };
193 
194 static int
wb_id(netdissect_options * ndo,const struct pkt_id * id,u_int len)195 wb_id(netdissect_options *ndo,
196       const struct pkt_id *id, u_int len)
197 {
198 	u_int i;
199 	const u_char *sitename;
200 	const struct id_off *io;
201 	char c;
202 	u_int nid;
203 
204 	ND_PRINT(" wb-id:");
205 	if (len < sizeof(*id))
206 		return (-1);
207 	len -= sizeof(*id);
208 
209 	ND_PRINT(" %u/%s:%u (max %u/%s:%u) ",
210 	       GET_BE_U_4(id->pi_ps.slot),
211 	       GET_IPADDR_STRING(id->pi_ps.page.p_sid),
212 	       GET_BE_U_4(id->pi_ps.page.p_uid),
213 	       GET_BE_U_4(id->pi_mslot),
214 	       GET_IPADDR_STRING(id->pi_mpage.p_sid),
215 	       GET_BE_U_4(id->pi_mpage.p_uid));
216 	/* now the rest of the fixed-size part of struct pkt_id */
217 	ND_TCHECK_SIZE(id);
218 
219 	nid = GET_BE_U_2(id->pi_ps.nid);
220 	if (len < sizeof(*io) * nid)
221 		return (-1);
222 	len -= sizeof(*io) * nid;
223 	io = (const struct id_off *)(id + 1);
224 	sitename = (const u_char *)(io + nid);
225 
226 	c = '<';
227 	for (i = 0; i < nid; ++io, ++i) {
228 		ND_PRINT("%c%s:%u",
229 		    c, GET_IPADDR_STRING(io->id), GET_BE_U_4(io->off));
230 		c = ',';
231 	}
232 	ND_PRINT("> \"");
233 	nd_printjnp(ndo, sitename, len);
234 	ND_PRINT("\"");
235 	return (0);
236 }
237 
238 static int
wb_rreq(netdissect_options * ndo,const struct pkt_rreq * rreq,u_int len)239 wb_rreq(netdissect_options *ndo,
240         const struct pkt_rreq *rreq, u_int len)
241 {
242 	ND_PRINT(" wb-rreq:");
243 	if (len < sizeof(*rreq))
244 		return (-1);
245 
246 	ND_PRINT(" please repair %s %s:%u<%u:%u>",
247 	       GET_IPADDR_STRING(rreq->pr_id),
248 	       GET_IPADDR_STRING(rreq->pr_page.p_sid),
249 	       GET_BE_U_4(rreq->pr_page.p_uid),
250 	       GET_BE_U_4(rreq->pr_sseq),
251 	       GET_BE_U_4(rreq->pr_eseq));
252 	return (0);
253 }
254 
255 static int
wb_preq(netdissect_options * ndo,const struct pkt_preq * preq,u_int len)256 wb_preq(netdissect_options *ndo,
257         const struct pkt_preq *preq, u_int len)
258 {
259 	ND_PRINT(" wb-preq:");
260 	if (len < sizeof(*preq))
261 		return (-1);
262 
263 	ND_PRINT(" need %u/%s:%u",
264 	       GET_BE_U_4(preq->pp_low),
265 	       GET_IPADDR_STRING(preq->pp_page.p_sid),
266 	       GET_BE_U_4(preq->pp_page.p_uid));
267 	/* now the rest of the fixed-size part of struct pkt_req */
268 	ND_TCHECK_SIZE(preq);
269 	return (0);
270 }
271 
272 static int
wb_prep(netdissect_options * ndo,const struct pkt_prep * prep,u_int len)273 wb_prep(netdissect_options *ndo,
274         const struct pkt_prep *prep, u_int len)
275 {
276 	u_int n;
277 	const struct pgstate *ps;
278 
279 	ND_PRINT(" wb-prep:");
280 	if (len < sizeof(*prep))
281 		return (-1);
282 	n = GET_BE_U_4(prep->pp_n);
283 	ps = (const struct pgstate *)(prep + 1);
284 	while (n != 0) {
285 		const struct id_off *io, *ie;
286 		char c = '<';
287 
288 		ND_PRINT(" %u/%s:%u",
289 		    GET_BE_U_4(ps->slot),
290 		    GET_IPADDR_STRING(ps->page.p_sid),
291 		    GET_BE_U_4(ps->page.p_uid));
292 		/* now the rest of the fixed-size part of struct pgstate */
293 		ND_TCHECK_SIZE(ps);
294 		io = (const struct id_off *)(ps + 1);
295 		for (ie = io + GET_U_1(ps->nid); io < ie; ++io) {
296 			ND_PRINT("%c%s:%u", c, GET_IPADDR_STRING(io->id),
297 			    GET_BE_U_4(io->off));
298 			c = ',';
299 		}
300 		ND_PRINT(">");
301 		ps = (const struct pgstate *)io;
302 		n--;
303 	}
304 	return 0;
305 }
306 
307 static void
wb_dops(netdissect_options * ndo,const struct pkt_dop * dop,uint32_t ss,uint32_t es)308 wb_dops(netdissect_options *ndo, const struct pkt_dop *dop,
309         uint32_t ss, uint32_t es)
310 {
311 	const struct dophdr *dh = (const struct dophdr *)((const u_char *)dop + sizeof(*dop));
312 
313 	ND_PRINT(" <");
314 	for ( ; ss <= es; ++ss) {
315 		u_int t;
316 
317 		t = GET_U_1(dh->dh_type);
318 
319 		ND_PRINT(" %s", tok2str(dop_str, "dop-%u!", t));
320 		if (t == DT_SKIP || t == DT_HOLE) {
321 			uint32_t ts = GET_BE_U_4(dh->dh_ts);
322 			ND_PRINT("%u", ts - ss + 1);
323 			if (ss > ts || ts > es) {
324 				ND_PRINT("[|]");
325 				if (ts < ss)
326 					return;
327 			}
328 			ss = ts;
329 		}
330 		dh = DOP_NEXT(dh);
331 	}
332 	ND_PRINT(" >");
333 }
334 
335 static int
wb_rrep(netdissect_options * ndo,const struct pkt_rrep * rrep,u_int len)336 wb_rrep(netdissect_options *ndo,
337         const struct pkt_rrep *rrep, u_int len)
338 {
339 	const struct pkt_dop *dop = &rrep->pr_dop;
340 
341 	ND_PRINT(" wb-rrep:");
342 	if (len < sizeof(*rrep))
343 		return (-1);
344 	len -= sizeof(*rrep);
345 
346 	ND_PRINT(" for %s %s:%u<%u:%u>",
347 	    GET_IPADDR_STRING(rrep->pr_id),
348 	    GET_IPADDR_STRING(dop->pd_page.p_sid),
349 	    GET_BE_U_4(dop->pd_page.p_uid),
350 	    GET_BE_U_4(dop->pd_sseq),
351 	    GET_BE_U_4(dop->pd_eseq));
352 
353 	if (ndo->ndo_vflag)
354 		wb_dops(ndo, dop,
355 		        GET_BE_U_4(dop->pd_sseq),
356 		        GET_BE_U_4(dop->pd_eseq));
357 	return (0);
358 }
359 
360 static int
wb_drawop(netdissect_options * ndo,const struct pkt_dop * dop,u_int len)361 wb_drawop(netdissect_options *ndo,
362           const struct pkt_dop *dop, u_int len)
363 {
364 	ND_PRINT(" wb-dop:");
365 	if (len < sizeof(*dop))
366 		return (-1);
367 	len -= sizeof(*dop);
368 
369 	ND_PRINT(" %s:%u<%u:%u>",
370 	    GET_IPADDR_STRING(dop->pd_page.p_sid),
371 	    GET_BE_U_4(dop->pd_page.p_uid),
372 	    GET_BE_U_4(dop->pd_sseq),
373 	    GET_BE_U_4(dop->pd_eseq));
374 
375 	if (ndo->ndo_vflag)
376 		wb_dops(ndo, dop,
377 		        GET_BE_U_4(dop->pd_sseq),
378 		        GET_BE_U_4(dop->pd_eseq));
379 	return (0);
380 }
381 
382 /*
383  * Print whiteboard multicast packets.
384  */
385 void
wb_print(netdissect_options * ndo,const u_char * hdr,u_int len)386 wb_print(netdissect_options *ndo,
387          const u_char *hdr, u_int len)
388 {
389 	const struct pkt_hdr *ph;
390 	uint8_t type;
391 	int print_result;
392 
393 	ndo->ndo_protocol = "wb";
394 	ph = (const struct pkt_hdr *)hdr;
395 	if (len < sizeof(*ph))
396 		goto invalid;
397 	ND_TCHECK_SIZE(ph);
398 	len -= sizeof(*ph);
399 
400 	if (GET_U_1(ph->ph_flags))
401 		ND_PRINT("*");
402 	type = GET_U_1(ph->ph_type);
403 	switch (type) {
404 
405 	case PT_KILL:
406 		ND_PRINT(" wb-kill");
407 		return;
408 
409 	case PT_ID:
410 		print_result = wb_id(ndo, (const struct pkt_id *)(ph + 1), len);
411 		break;
412 
413 	case PT_RREQ:
414 		print_result = wb_rreq(ndo, (const struct pkt_rreq *)(ph + 1), len);
415 		break;
416 
417 	case PT_RREP:
418 		print_result = wb_rrep(ndo, (const struct pkt_rrep *)(ph + 1), len);
419 		break;
420 
421 	case PT_DRAWOP:
422 		print_result = wb_drawop(ndo, (const struct pkt_dop *)(ph + 1), len);
423 		break;
424 
425 	case PT_PREQ:
426 		print_result = wb_preq(ndo, (const struct pkt_preq *)(ph + 1), len);
427 		break;
428 
429 	case PT_PREP:
430 		print_result = wb_prep(ndo, (const struct pkt_prep *)(ph + 1), len);
431 		break;
432 
433 	default:
434 		ND_PRINT(" wb-%u!", type);
435 		print_result = -1;
436 	}
437 	if (print_result < 0)
438 		goto invalid;
439 	return;
440 
441 invalid:
442 	nd_print_invalid(ndo);
443 }
444