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