xref: /freebsd/contrib/tcpdump/print-wb.c (revision 2a4a1db342263067035ce69a4017c645da63455d)
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.25 1999/11/21 09:37:03 fenner 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 /* flags */
81 #define PF_USER		0x01	/* hint that packet has interactive data */
82 #define PF_VIS		0x02	/* only visible ops wanted */
83 
84 struct PageID {
85 	u_int32_t p_sid;		/* session id of initiator */
86 	u_int32_t p_uid;		/* page number */
87 };
88 
89 struct dophdr {
90 	u_int32_t  dh_ts;		/* sender's timestamp */
91 	u_short	dh_len;		/* body length */
92 	u_char	dh_flags;
93 	u_char	dh_type;	/* body type */
94 	/* body follows */
95 };
96 /*
97  * Drawing op sub-types.
98  */
99 #define DT_RECT         2
100 #define DT_LINE         3
101 #define DT_ML           4
102 #define DT_DEL          5
103 #define DT_XFORM        6
104 #define DT_ELL          7
105 #define DT_CHAR         8
106 #define DT_STR          9
107 #define DT_NOP          10
108 #define DT_PSCODE       11
109 #define DT_PSCOMP       12
110 #define DT_REF          13
111 #define DT_SKIP         14
112 #define DT_HOLE         15
113 #define DT_MAXTYPE      15
114 
115 /*
116  * A drawing operation.
117  */
118 struct pkt_dop {
119 	struct PageID pd_page;	/* page that operations apply to */
120 	u_int32_t	pd_sseq;	/* start sequence number */
121 	u_int32_t	pd_eseq;	/* end sequence number */
122 	/* drawing ops follow */
123 };
124 
125 /*
126  * A repair request.
127  */
128 struct pkt_rreq {
129         u_int32_t pr_id;           /* source id of drawops to be repaired */
130         struct PageID pr_page;           /* page of drawops */
131         u_int32_t pr_sseq;         /* start seqno */
132         u_int32_t pr_eseq;         /* end seqno */
133 };
134 
135 /*
136  * A repair reply.
137  */
138 struct pkt_rrep {
139 	u_int32_t pr_id;	/* original site id of ops  */
140 	struct pkt_dop pr_dop;
141 	/* drawing ops follow */
142 };
143 
144 struct id_off {
145         u_int32_t id;
146         u_int32_t off;
147 };
148 
149 struct pgstate {
150 	u_int32_t slot;
151 	struct PageID page;
152 	u_short nid;
153 	u_short rsvd;
154         /* seqptr's */
155 };
156 
157 /*
158  * An announcement packet.
159  */
160 struct pkt_id {
161 	u_int32_t pi_mslot;
162         struct PageID    pi_mpage;        /* current page */
163 	struct pgstate pi_ps;
164         /* seqptr's */
165         /* null-terminated site name */
166 };
167 
168 struct pkt_preq {
169         struct PageID  pp_page;
170         u_int32_t  pp_low;
171         u_int32_t  pp_high;
172 };
173 
174 struct pkt_prep {
175         u_int32_t  pp_n;           /* size of pageid array */
176         /* pgstate's follow */
177 };
178 
179 static int
180 wb_id(const struct pkt_id *id, u_int len)
181 {
182 	int i;
183 	const char *cp;
184 	const struct id_off *io;
185 	char c;
186 	int nid;
187 
188 	printf(" wb-id:");
189 	if (len < sizeof(*id) || (u_char *)(id + 1) > snapend)
190 		return (-1);
191 	len -= sizeof(*id);
192 
193 	printf(" %u/%s:%u (max %u/%s:%u) ",
194 	       (u_int32_t)ntohl(id->pi_ps.slot),
195 	       ipaddr_string(&id->pi_ps.page.p_sid),
196 	       (u_int32_t)ntohl(id->pi_ps.page.p_uid),
197 	       (u_int32_t)ntohl(id->pi_mslot),
198 	       ipaddr_string(&id->pi_mpage.p_sid),
199 	       (u_int32_t)ntohl(id->pi_mpage.p_uid));
200 
201 	nid = ntohs(id->pi_ps.nid);
202 	len -= sizeof(*io) * nid;
203 	io = (struct id_off *)(id + 1);
204 	cp = (char *)(io + nid);
205 	if ((u_char *)cp + len <= snapend) {
206 		putchar('"');
207 		(void)fn_print((u_char *)cp, (u_char *)cp + len);
208 		putchar('"');
209 	}
210 
211 	c = '<';
212 	for (i = 0; i < nid && (u_char *)io < snapend; ++io, ++i) {
213 		printf("%c%s:%u",
214 		    c, ipaddr_string(&io->id), (u_int32_t)ntohl(io->off));
215 		c = ',';
216 	}
217 	if (i >= nid) {
218 		printf(">");
219 		return (0);
220 	}
221 	return (-1);
222 }
223 
224 static int
225 wb_rreq(const struct pkt_rreq *rreq, u_int len)
226 {
227 	printf(" wb-rreq:");
228 	if (len < sizeof(*rreq) || (u_char *)(rreq + 1) > snapend)
229 		return (-1);
230 
231 	printf(" please repair %s %s:%u<%u:%u>",
232 	       ipaddr_string(&rreq->pr_id),
233 	       ipaddr_string(&rreq->pr_page.p_sid),
234 	       (u_int32_t)ntohl(rreq->pr_page.p_uid),
235 	       (u_int32_t)ntohl(rreq->pr_sseq),
236 	       (u_int32_t)ntohl(rreq->pr_eseq));
237 	return (0);
238 }
239 
240 static int
241 wb_preq(const struct pkt_preq *preq, u_int len)
242 {
243 	printf(" wb-preq:");
244 	if (len < sizeof(*preq) || (u_char *)(preq + 1) > snapend)
245 		return (-1);
246 
247 	printf(" need %u/%s:%u",
248 	       (u_int32_t)ntohl(preq->pp_low),
249 	       ipaddr_string(&preq->pp_page.p_sid),
250 	       (u_int32_t)ntohl(preq->pp_page.p_uid));
251 	return (0);
252 }
253 
254 static int
255 wb_prep(const struct pkt_prep *prep, u_int len)
256 {
257 	int n;
258 	const struct pgstate *ps;
259 	const u_char *ep = snapend;
260 
261 	printf(" wb-prep:");
262 	if (len < sizeof(*prep)) {
263 		return (-1);
264 	}
265 	n = ntohl(prep->pp_n);
266 	ps = (const struct pgstate *)(prep + 1);
267 	while (--n >= 0 && (u_char *)ps < ep) {
268 		const struct id_off *io, *ie;
269 		char c = '<';
270 
271 		printf(" %u/%s:%u",
272 		    (u_int32_t)ntohl(ps->slot),
273 		    ipaddr_string(&ps->page.p_sid),
274 		    (u_int32_t)ntohl(ps->page.p_uid));
275 		io = (struct id_off *)(ps + 1);
276 		for (ie = io + ps->nid; io < ie && (u_char *)io < ep; ++io) {
277 			printf("%c%s:%u", c, ipaddr_string(&io->id),
278 			    (u_int32_t)ntohl(io->off));
279 			c = ',';
280 		}
281 		printf(">");
282 		ps = (struct pgstate *)io;
283 	}
284 	return ((u_char *)ps <= ep? 0 : -1);
285 }
286 
287 
288 char *dopstr[] = {
289 	"dop-0!",
290 	"dop-1!",
291 	"RECT",
292 	"LINE",
293 	"ML",
294 	"DEL",
295 	"XFORM",
296 	"ELL",
297 	"CHAR",
298 	"STR",
299 	"NOP",
300 	"PSCODE",
301 	"PSCOMP",
302 	"REF",
303 	"SKIP",
304 	"HOLE",
305 };
306 
307 static int
308 wb_dops(const struct dophdr *dh, u_int32_t ss, u_int32_t es)
309 {
310 	printf(" <");
311 	for ( ; ss <= es; ++ss) {
312 		register int t = dh->dh_type;
313 
314 		if (t > DT_MAXTYPE)
315 			printf(" dop-%d!", t);
316 		else {
317 			printf(" %s", dopstr[t]);
318 			if (t == DT_SKIP || t == DT_HOLE) {
319 				int ts = ntohl(dh->dh_ts);
320 				printf("%d", ts - ss + 1);
321 				if (ss > ts || ts > es) {
322 					printf("[|]");
323 					if (ts < ss)
324 						return (0);
325 				}
326 				ss = ts;
327 			}
328 		}
329 		dh = DOP_NEXT(dh);
330 		if ((u_char *)dh > snapend) {
331 			printf("[|wb]");
332 			break;
333 		}
334 	}
335 	printf(" >");
336 	return (0);
337 }
338 
339 static int
340 wb_rrep(const struct pkt_rrep *rrep, u_int len)
341 {
342 	const struct pkt_dop *dop = &rrep->pr_dop;
343 
344 	printf(" wb-rrep:");
345 	if (len < sizeof(*rrep) || (u_char *)(rrep + 1) > snapend)
346 		return (-1);
347 	len -= sizeof(*rrep);
348 
349 	printf(" for %s %s:%u<%u:%u>",
350 	    ipaddr_string(&rrep->pr_id),
351 	    ipaddr_string(&dop->pd_page.p_sid),
352 	    (u_int32_t)ntohl(dop->pd_page.p_uid),
353 	    (u_int32_t)ntohl(dop->pd_sseq),
354 	    (u_int32_t)ntohl(dop->pd_eseq));
355 
356 	if (vflag)
357 		return (wb_dops((const struct dophdr *)(dop + 1),
358 		    ntohl(dop->pd_sseq), ntohl(dop->pd_eseq)));
359 	return (0);
360 }
361 
362 static int
363 wb_drawop(const struct pkt_dop *dop, u_int len)
364 {
365 	printf(" wb-dop:");
366 	if (len < sizeof(*dop) || (u_char *)(dop + 1) > snapend)
367 		return (-1);
368 	len -= sizeof(*dop);
369 
370 	printf(" %s:%u<%u:%u>",
371 	    ipaddr_string(&dop->pd_page.p_sid),
372 	    (u_int32_t)ntohl(dop->pd_page.p_uid),
373 	    (u_int32_t)ntohl(dop->pd_sseq),
374 	    (u_int32_t)ntohl(dop->pd_eseq));
375 
376 	if (vflag)
377 		return (wb_dops((const struct dophdr *)(dop + 1),
378 				ntohl(dop->pd_sseq), ntohl(dop->pd_eseq)));
379 	return (0);
380 }
381 
382 /*
383  * Print whiteboard multicast packets.
384  */
385 void
386 wb_print(register const void *hdr, register u_int len)
387 {
388 	register const struct pkt_hdr *ph;
389 
390 	ph = (const struct pkt_hdr *)hdr;
391 	if (len < sizeof(*ph) || (u_char *)(ph + 1) > snapend) {
392 		printf("[|wb]");
393 		return;
394 	}
395 	len -= sizeof(*ph);
396 
397 	if (ph->ph_flags)
398 		printf("*");
399 	switch (ph->ph_type) {
400 
401 	case PT_KILL:
402 		printf(" wb-kill");
403 		return;
404 
405 	case PT_ID:
406 		if (wb_id((struct pkt_id *)(ph + 1), len) >= 0)
407 			return;
408 		break;
409 
410 	case PT_RREQ:
411 		if (wb_rreq((struct pkt_rreq *)(ph + 1), len) >= 0)
412 			return;
413 		break;
414 
415 	case PT_RREP:
416 		if (wb_rrep((struct pkt_rrep *)(ph + 1), len) >= 0)
417 			return;
418 		break;
419 
420 	case PT_DRAWOP:
421 		if (wb_drawop((struct pkt_dop *)(ph + 1), len) >= 0)
422 			return;
423 		break;
424 
425 	case PT_PREQ:
426 		if (wb_preq((struct pkt_preq *)(ph + 1), len) >= 0)
427 			return;
428 		break;
429 
430 	case PT_PREP:
431 		if (wb_prep((struct pkt_prep *)(ph + 1), len) >= 0)
432 			return;
433 		break;
434 
435 	default:
436 		printf(" wb-%d!", ph->ph_type);
437 		return;
438 	}
439 }
440