xref: /netbsd/external/bsd/tcpdump/dist/print-wb.c (revision 6550d01e)
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 #include <sys/cdefs.h>
23 #ifndef lint
24 #if 0
25 static const char rcsid[] _U_ =
26     "@(#) Header: /tcpdump/master/tcpdump/print-wb.c,v 1.33 2004-03-24 04:06:28 guy Exp (LBL)";
27 #else
28 __RCSID("$NetBSD: print-wb.c,v 1.2 2010/12/05 05:11:31 christos Exp $");
29 #endif
30 #endif
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <tcpdump-stdinc.h>
37 
38 #include <stdio.h>
39 
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h"
43 
44 /* XXX need to add byte-swapping macros! */
45 /* XXX - you mean like the ones in "extract.h"? */
46 
47 /*
48  * Largest packet size.  Everything should fit within this space.
49  * For instance, multiline objects are sent piecewise.
50  */
51 #define MAXFRAMESIZE 1024
52 
53 /*
54  * Multiple drawing ops can be sent in one packet.  Each one starts on a
55  * an even multiple of DOP_ALIGN bytes, which must be a power of two.
56  */
57 #define DOP_ALIGN 4
58 #define DOP_ROUNDUP(x)	((((int)(x)) + (DOP_ALIGN - 1)) & ~(DOP_ALIGN - 1))
59 #define DOP_NEXT(d)\
60 	((struct dophdr *)((u_char *)(d) + \
61 			  DOP_ROUNDUP(EXTRACT_16BITS(&(d)->dh_len) + sizeof(*(d)))))
62 
63 /*
64  * Format of the whiteboard packet header.
65  * The transport level header.
66  */
67 struct pkt_hdr {
68 	u_int32_t ph_src;		/* site id of source */
69 	u_int32_t ph_ts;		/* time stamp (for skew computation) */
70 	u_int16_t ph_version;	/* version number */
71 	u_char ph_type;		/* message type */
72 	u_char ph_flags;	/* message flags */
73 };
74 
75 /* Packet types */
76 #define PT_DRAWOP	0	/* drawing operation */
77 #define PT_ID		1	/* announcement packet */
78 #define PT_RREQ		2	/* repair request */
79 #define PT_RREP		3	/* repair reply */
80 #define PT_KILL		4	/* terminate participation */
81 #define PT_PREQ         5       /* page vector request */
82 #define PT_PREP         7       /* page vector reply */
83 
84 #ifdef PF_USER
85 #undef PF_USER			/* {Digital,Tru64} UNIX define this, alas */
86 #endif
87 
88 /* flags */
89 #define PF_USER		0x01	/* hint that packet has interactive data */
90 #define PF_VIS		0x02	/* only visible ops wanted */
91 
92 struct PageID {
93 	u_int32_t p_sid;		/* session id of initiator */
94 	u_int32_t p_uid;		/* page number */
95 };
96 
97 struct dophdr {
98 	u_int32_t  dh_ts;		/* sender's timestamp */
99 	u_int16_t	dh_len;		/* body length */
100 	u_char	dh_flags;
101 	u_char	dh_type;	/* body type */
102 	/* body follows */
103 };
104 /*
105  * Drawing op sub-types.
106  */
107 #define DT_RECT         2
108 #define DT_LINE         3
109 #define DT_ML           4
110 #define DT_DEL          5
111 #define DT_XFORM        6
112 #define DT_ELL          7
113 #define DT_CHAR         8
114 #define DT_STR          9
115 #define DT_NOP          10
116 #define DT_PSCODE       11
117 #define DT_PSCOMP       12
118 #define DT_REF          13
119 #define DT_SKIP         14
120 #define DT_HOLE         15
121 #define DT_MAXTYPE      15
122 
123 /*
124  * A drawing operation.
125  */
126 struct pkt_dop {
127 	struct PageID pd_page;	/* page that operations apply to */
128 	u_int32_t	pd_sseq;	/* start sequence number */
129 	u_int32_t	pd_eseq;	/* end sequence number */
130 	/* drawing ops follow */
131 };
132 
133 /*
134  * A repair request.
135  */
136 struct pkt_rreq {
137         u_int32_t pr_id;           /* source id of drawops to be repaired */
138         struct PageID pr_page;           /* page of drawops */
139         u_int32_t pr_sseq;         /* start seqno */
140         u_int32_t pr_eseq;         /* end seqno */
141 };
142 
143 /*
144  * A repair reply.
145  */
146 struct pkt_rrep {
147 	u_int32_t pr_id;	/* original site id of ops  */
148 	struct pkt_dop pr_dop;
149 	/* drawing ops follow */
150 };
151 
152 struct id_off {
153         u_int32_t id;
154         u_int32_t off;
155 };
156 
157 struct pgstate {
158 	u_int32_t slot;
159 	struct PageID page;
160 	u_int16_t nid;
161 	u_int16_t rsvd;
162         /* seqptr's */
163 };
164 
165 /*
166  * An announcement packet.
167  */
168 struct pkt_id {
169 	u_int32_t pi_mslot;
170         struct PageID    pi_mpage;        /* current page */
171 	struct pgstate pi_ps;
172         /* seqptr's */
173         /* null-terminated site name */
174 };
175 
176 struct pkt_preq {
177         struct PageID  pp_page;
178         u_int32_t  pp_low;
179         u_int32_t  pp_high;
180 };
181 
182 struct pkt_prep {
183         u_int32_t  pp_n;           /* size of pageid array */
184         /* pgstate's follow */
185 };
186 
187 static int
188 wb_id(const struct pkt_id *id, u_int len)
189 {
190 	int i;
191 	const char *cp;
192 	const struct id_off *io;
193 	char c;
194 	int nid;
195 
196 	printf(" wb-id:");
197 	if (len < sizeof(*id) || (u_char *)(id + 1) > snapend)
198 		return (-1);
199 	len -= sizeof(*id);
200 
201 	printf(" %u/%s:%u (max %u/%s:%u) ",
202 	       EXTRACT_32BITS(&id->pi_ps.slot),
203 	       ipaddr_string(&id->pi_ps.page.p_sid),
204 	       EXTRACT_32BITS(&id->pi_ps.page.p_uid),
205 	       EXTRACT_32BITS(&id->pi_mslot),
206 	       ipaddr_string(&id->pi_mpage.p_sid),
207 	       EXTRACT_32BITS(&id->pi_mpage.p_uid));
208 
209 	nid = EXTRACT_16BITS(&id->pi_ps.nid);
210 	len -= sizeof(*io) * nid;
211 	io = (struct id_off *)(id + 1);
212 	cp = (char *)(io + nid);
213 	if ((u_char *)cp + len <= snapend) {
214 		putchar('"');
215 		(void)fn_print((u_char *)cp, (u_char *)cp + len);
216 		putchar('"');
217 	}
218 
219 	c = '<';
220 	for (i = 0; i < nid && (u_char *)(io + 1) <= snapend; ++io, ++i) {
221 		printf("%c%s:%u",
222 		    c, ipaddr_string(&io->id), EXTRACT_32BITS(&io->off));
223 		c = ',';
224 	}
225 	if (i >= nid) {
226 		printf(">");
227 		return (0);
228 	}
229 	return (-1);
230 }
231 
232 static int
233 wb_rreq(const struct pkt_rreq *rreq, u_int len)
234 {
235 	printf(" wb-rreq:");
236 	if (len < sizeof(*rreq) || (u_char *)(rreq + 1) > snapend)
237 		return (-1);
238 
239 	printf(" please repair %s %s:%u<%u:%u>",
240 	       ipaddr_string(&rreq->pr_id),
241 	       ipaddr_string(&rreq->pr_page.p_sid),
242 	       EXTRACT_32BITS(&rreq->pr_page.p_uid),
243 	       EXTRACT_32BITS(&rreq->pr_sseq),
244 	       EXTRACT_32BITS(&rreq->pr_eseq));
245 	return (0);
246 }
247 
248 static int
249 wb_preq(const struct pkt_preq *preq, u_int len)
250 {
251 	printf(" wb-preq:");
252 	if (len < sizeof(*preq) || (u_char *)(preq + 1) > snapend)
253 		return (-1);
254 
255 	printf(" need %u/%s:%u",
256 	       EXTRACT_32BITS(&preq->pp_low),
257 	       ipaddr_string(&preq->pp_page.p_sid),
258 	       EXTRACT_32BITS(&preq->pp_page.p_uid));
259 	return (0);
260 }
261 
262 static int
263 wb_prep(const struct pkt_prep *prep, u_int len)
264 {
265 	int n;
266 	const struct pgstate *ps;
267 	const u_char *ep = snapend;
268 
269 	printf(" wb-prep:");
270 	if (len < sizeof(*prep)) {
271 		return (-1);
272 	}
273 	n = EXTRACT_32BITS(&prep->pp_n);
274 	ps = (const struct pgstate *)(prep + 1);
275 	while (--n >= 0 && (u_char *)(ps + 1) <= ep) {
276 		const struct id_off *io, *ie;
277 		char c = '<';
278 
279 		printf(" %u/%s:%u",
280 		    EXTRACT_32BITS(&ps->slot),
281 		    ipaddr_string(&ps->page.p_sid),
282 		    EXTRACT_32BITS(&ps->page.p_uid));
283 		io = (struct id_off *)(ps + 1);
284 		for (ie = io + ps->nid; io < ie && (u_char *)(io + 1) <= ep; ++io) {
285 			printf("%c%s:%u", c, ipaddr_string(&io->id),
286 			    EXTRACT_32BITS(&io->off));
287 			c = ',';
288 		}
289 		printf(">");
290 		ps = (struct pgstate *)io;
291 	}
292 	return ((u_char *)ps <= ep? 0 : -1);
293 }
294 
295 
296 const char *dopstr[] = {
297 	"dop-0!",
298 	"dop-1!",
299 	"RECT",
300 	"LINE",
301 	"ML",
302 	"DEL",
303 	"XFORM",
304 	"ELL",
305 	"CHAR",
306 	"STR",
307 	"NOP",
308 	"PSCODE",
309 	"PSCOMP",
310 	"REF",
311 	"SKIP",
312 	"HOLE",
313 };
314 
315 static int
316 wb_dops(const struct dophdr *dh, u_int32_t ss, u_int32_t es)
317 {
318 	printf(" <");
319 	for ( ; ss <= es; ++ss) {
320 		register int t = dh->dh_type;
321 
322 		if (t > DT_MAXTYPE)
323 			printf(" dop-%d!", t);
324 		else {
325 			printf(" %s", dopstr[t]);
326 			if (t == DT_SKIP || t == DT_HOLE) {
327 				u_int32_t ts = EXTRACT_32BITS(&dh->dh_ts);
328 				printf("%d", ts - ss + 1);
329 				if (ss > ts || ts > es) {
330 					printf("[|]");
331 					if (ts < ss)
332 						return (0);
333 				}
334 				ss = ts;
335 			}
336 		}
337 		dh = DOP_NEXT(dh);
338 		if ((u_char *)dh > snapend) {
339 			printf("[|wb]");
340 			break;
341 		}
342 	}
343 	printf(" >");
344 	return (0);
345 }
346 
347 static int
348 wb_rrep(const struct pkt_rrep *rrep, u_int len)
349 {
350 	const struct pkt_dop *dop = &rrep->pr_dop;
351 
352 	printf(" wb-rrep:");
353 	if (len < sizeof(*rrep) || (u_char *)(rrep + 1) > snapend)
354 		return (-1);
355 	len -= sizeof(*rrep);
356 
357 	printf(" for %s %s:%u<%u:%u>",
358 	    ipaddr_string(&rrep->pr_id),
359 	    ipaddr_string(&dop->pd_page.p_sid),
360 	    EXTRACT_32BITS(&dop->pd_page.p_uid),
361 	    EXTRACT_32BITS(&dop->pd_sseq),
362 	    EXTRACT_32BITS(&dop->pd_eseq));
363 
364 	if (vflag)
365 		return (wb_dops((const struct dophdr *)(dop + 1),
366 		    EXTRACT_32BITS(&dop->pd_sseq),
367 		    EXTRACT_32BITS(&dop->pd_eseq)));
368 	return (0);
369 }
370 
371 static int
372 wb_drawop(const struct pkt_dop *dop, u_int len)
373 {
374 	printf(" wb-dop:");
375 	if (len < sizeof(*dop) || (u_char *)(dop + 1) > snapend)
376 		return (-1);
377 	len -= sizeof(*dop);
378 
379 	printf(" %s:%u<%u:%u>",
380 	    ipaddr_string(&dop->pd_page.p_sid),
381 	    EXTRACT_32BITS(&dop->pd_page.p_uid),
382 	    EXTRACT_32BITS(&dop->pd_sseq),
383 	    EXTRACT_32BITS(&dop->pd_eseq));
384 
385 	if (vflag)
386 		return (wb_dops((const struct dophdr *)(dop + 1),
387 				EXTRACT_32BITS(&dop->pd_sseq),
388 				EXTRACT_32BITS(&dop->pd_eseq)));
389 	return (0);
390 }
391 
392 /*
393  * Print whiteboard multicast packets.
394  */
395 void
396 wb_print(register const void *hdr, register u_int len)
397 {
398 	register const struct pkt_hdr *ph;
399 
400 	ph = (const struct pkt_hdr *)hdr;
401 	if (len < sizeof(*ph) || (u_char *)(ph + 1) > snapend) {
402 		printf("[|wb]");
403 		return;
404 	}
405 	len -= sizeof(*ph);
406 
407 	if (ph->ph_flags)
408 		printf("*");
409 	switch (ph->ph_type) {
410 
411 	case PT_KILL:
412 		printf(" wb-kill");
413 		return;
414 
415 	case PT_ID:
416 		if (wb_id((struct pkt_id *)(ph + 1), len) >= 0)
417 			return;
418 		break;
419 
420 	case PT_RREQ:
421 		if (wb_rreq((struct pkt_rreq *)(ph + 1), len) >= 0)
422 			return;
423 		break;
424 
425 	case PT_RREP:
426 		if (wb_rrep((struct pkt_rrep *)(ph + 1), len) >= 0)
427 			return;
428 		break;
429 
430 	case PT_DRAWOP:
431 		if (wb_drawop((struct pkt_dop *)(ph + 1), len) >= 0)
432 			return;
433 		break;
434 
435 	case PT_PREQ:
436 		if (wb_preq((struct pkt_preq *)(ph + 1), len) >= 0)
437 			return;
438 		break;
439 
440 	case PT_PREP:
441 		if (wb_prep((struct pkt_prep *)(ph + 1), len) >= 0)
442 			return;
443 		break;
444 
445 	default:
446 		printf(" wb-%d!", ph->ph_type);
447 		return;
448 	}
449 }
450