Home | History | Annotate | Line # | Download | only in dist
print-wb.c revision 1.4
      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 __RCSID("$NetBSD: print-wb.c,v 1.4 2014/11/20 03:05:03 christos Exp $");
     25 #endif
     26 
     27 #define NETDISSECT_REWORKED
     28 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include <tcpdump-stdinc.h>
     33 
     34 #include "interface.h"
     35 #include "addrtoname.h"
     36 #include "extract.h"
     37 
     38 static const char tstr[] = "[|wb]";
     39 
     40 /* XXX need to add byte-swapping macros! */
     41 /* XXX - you mean like the ones in "extract.h"? */
     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(EXTRACT_16BITS(&(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 	uint32_t ph_src;		/* site id of source */
     65 	uint32_t ph_ts;		/* time stamp (for skew computation) */
     66 	uint16_t 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 	uint32_t p_sid;		/* session id of initiator */
     90 	uint32_t p_uid;		/* page number */
     91 };
     92 
     93 struct dophdr {
     94 	uint32_t  dh_ts;		/* sender's timestamp */
     95 	uint16_t	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 	uint32_t	pd_sseq;	/* start sequence number */
    125 	uint32_t	pd_eseq;	/* end sequence number */
    126 	/* drawing ops follow */
    127 };
    128 
    129 /*
    130  * A repair request.
    131  */
    132 struct pkt_rreq {
    133         uint32_t pr_id;           /* source id of drawops to be repaired */
    134         struct PageID pr_page;           /* page of drawops */
    135         uint32_t pr_sseq;         /* start seqno */
    136         uint32_t pr_eseq;         /* end seqno */
    137 };
    138 
    139 /*
    140  * A repair reply.
    141  */
    142 struct pkt_rrep {
    143 	uint32_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         uint32_t id;
    150         uint32_t off;
    151 };
    152 
    153 struct pgstate {
    154 	uint32_t slot;
    155 	struct PageID page;
    156 	uint16_t nid;
    157 	uint16_t rsvd;
    158         /* seqptr's */
    159 };
    160 
    161 /*
    162  * An announcement packet.
    163  */
    164 struct pkt_id {
    165 	uint32_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         uint32_t  pp_low;
    175         uint32_t  pp_high;
    176 };
    177 
    178 struct pkt_prep {
    179         uint32_t  pp_n;           /* size of pageid array */
    180         /* pgstate's follow */
    181 };
    182 
    183 static int
    184 wb_id(netdissect_options *ndo,
    185       const struct pkt_id *id, u_int len)
    186 {
    187 	int i;
    188 	const char *cp;
    189 	const struct id_off *io;
    190 	char c;
    191 	int nid;
    192 
    193 	ND_PRINT((ndo, " wb-id:"));
    194 	if (len < sizeof(*id) || (u_char *)(id + 1) > ndo->ndo_snapend)
    195 		return (-1);
    196 	len -= sizeof(*id);
    197 
    198 	ND_PRINT((ndo, " %u/%s:%u (max %u/%s:%u) ",
    199 	       EXTRACT_32BITS(&id->pi_ps.slot),
    200 	       ipaddr_string(ndo, &id->pi_ps.page.p_sid),
    201 	       EXTRACT_32BITS(&id->pi_ps.page.p_uid),
    202 	       EXTRACT_32BITS(&id->pi_mslot),
    203 	       ipaddr_string(ndo, &id->pi_mpage.p_sid),
    204 	       EXTRACT_32BITS(&id->pi_mpage.p_uid)));
    205 
    206 	nid = EXTRACT_16BITS(&id->pi_ps.nid);
    207 	len -= sizeof(*io) * nid;
    208 	io = (struct id_off *)(id + 1);
    209 	cp = (char *)(io + nid);
    210 	if ((u_char *)cp + len <= ndo->ndo_snapend) {
    211 		ND_PRINT((ndo, "\""));
    212 		fn_print(ndo, (u_char *)cp, (u_char *)cp + len);
    213 		ND_PRINT((ndo, "\""));
    214 	}
    215 
    216 	c = '<';
    217 	for (i = 0; i < nid && (u_char *)(io + 1) <= ndo->ndo_snapend; ++io, ++i) {
    218 		ND_PRINT((ndo, "%c%s:%u",
    219 		    c, ipaddr_string(ndo, &io->id), EXTRACT_32BITS(&io->off)));
    220 		c = ',';
    221 	}
    222 	if (i >= nid) {
    223 		ND_PRINT((ndo, ">"));
    224 		return (0);
    225 	}
    226 	return (-1);
    227 }
    228 
    229 static int
    230 wb_rreq(netdissect_options *ndo,
    231         const struct pkt_rreq *rreq, u_int len)
    232 {
    233 	ND_PRINT((ndo, " wb-rreq:"));
    234 	if (len < sizeof(*rreq) || (u_char *)(rreq + 1) > ndo->ndo_snapend)
    235 		return (-1);
    236 
    237 	ND_PRINT((ndo, " please repair %s %s:%u<%u:%u>",
    238 	       ipaddr_string(ndo, &rreq->pr_id),
    239 	       ipaddr_string(ndo, &rreq->pr_page.p_sid),
    240 	       EXTRACT_32BITS(&rreq->pr_page.p_uid),
    241 	       EXTRACT_32BITS(&rreq->pr_sseq),
    242 	       EXTRACT_32BITS(&rreq->pr_eseq)));
    243 	return (0);
    244 }
    245 
    246 static int
    247 wb_preq(netdissect_options *ndo,
    248         const struct pkt_preq *preq, u_int len)
    249 {
    250 	ND_PRINT((ndo, " wb-preq:"));
    251 	if (len < sizeof(*preq) || (u_char *)(preq + 1) > ndo->ndo_snapend)
    252 		return (-1);
    253 
    254 	ND_PRINT((ndo, " need %u/%s:%u",
    255 	       EXTRACT_32BITS(&preq->pp_low),
    256 	       ipaddr_string(ndo, &preq->pp_page.p_sid),
    257 	       EXTRACT_32BITS(&preq->pp_page.p_uid)));
    258 	return (0);
    259 }
    260 
    261 static int
    262 wb_prep(netdissect_options *ndo,
    263         const struct pkt_prep *prep, u_int len)
    264 {
    265 	int n;
    266 	const struct pgstate *ps;
    267 	const u_char *ep = ndo->ndo_snapend;
    268 
    269 	ND_PRINT((ndo, " 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 		ND_PRINT((ndo, " %u/%s:%u",
    280 		    EXTRACT_32BITS(&ps->slot),
    281 		    ipaddr_string(ndo, &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 			ND_PRINT((ndo, "%c%s:%u", c, ipaddr_string(ndo, &io->id),
    286 			    EXTRACT_32BITS(&io->off)));
    287 			c = ',';
    288 		}
    289 		ND_PRINT((ndo, ">"));
    290 		ps = (struct pgstate *)io;
    291 	}
    292 	return ((u_char *)ps <= ep? 0 : -1);
    293 }
    294 
    295 
    296 static 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(netdissect_options *ndo,
    317         const struct dophdr *dh, uint32_t ss, uint32_t es)
    318 {
    319 	ND_PRINT((ndo, " <"));
    320 	for ( ; ss <= es; ++ss) {
    321 		register int t = dh->dh_type;
    322 
    323 		if (t > DT_MAXTYPE)
    324 			ND_PRINT((ndo, " dop-%d!", t));
    325 		else {
    326 			ND_PRINT((ndo, " %s", dopstr[t]));
    327 			if (t == DT_SKIP || t == DT_HOLE) {
    328 				uint32_t ts = EXTRACT_32BITS(&dh->dh_ts);
    329 				ND_PRINT((ndo, "%d", ts - ss + 1));
    330 				if (ss > ts || ts > es) {
    331 					ND_PRINT((ndo, "[|]"));
    332 					if (ts < ss)
    333 						return (0);
    334 				}
    335 				ss = ts;
    336 			}
    337 		}
    338 		dh = DOP_NEXT(dh);
    339 		if ((u_char *)dh > ndo->ndo_snapend) {
    340 			ND_PRINT((ndo, "%s", tstr));
    341 			break;
    342 		}
    343 	}
    344 	ND_PRINT((ndo, " >"));
    345 	return (0);
    346 }
    347 
    348 static int
    349 wb_rrep(netdissect_options *ndo,
    350         const struct pkt_rrep *rrep, u_int len)
    351 {
    352 	const struct pkt_dop *dop = &rrep->pr_dop;
    353 
    354 	ND_PRINT((ndo, " wb-rrep:"));
    355 	if (len < sizeof(*rrep) || (u_char *)(rrep + 1) > ndo->ndo_snapend)
    356 		return (-1);
    357 	len -= sizeof(*rrep);
    358 
    359 	ND_PRINT((ndo, " for %s %s:%u<%u:%u>",
    360 	    ipaddr_string(ndo, &rrep->pr_id),
    361 	    ipaddr_string(ndo, &dop->pd_page.p_sid),
    362 	    EXTRACT_32BITS(&dop->pd_page.p_uid),
    363 	    EXTRACT_32BITS(&dop->pd_sseq),
    364 	    EXTRACT_32BITS(&dop->pd_eseq)));
    365 
    366 	if (ndo->ndo_vflag)
    367 		return (wb_dops(ndo, (const struct dophdr *)(dop + 1),
    368 		    EXTRACT_32BITS(&dop->pd_sseq),
    369 		    EXTRACT_32BITS(&dop->pd_eseq)));
    370 	return (0);
    371 }
    372 
    373 static int
    374 wb_drawop(netdissect_options *ndo,
    375           const struct pkt_dop *dop, u_int len)
    376 {
    377 	ND_PRINT((ndo, " wb-dop:"));
    378 	if (len < sizeof(*dop) || (u_char *)(dop + 1) > ndo->ndo_snapend)
    379 		return (-1);
    380 	len -= sizeof(*dop);
    381 
    382 	ND_PRINT((ndo, " %s:%u<%u:%u>",
    383 	    ipaddr_string(ndo, &dop->pd_page.p_sid),
    384 	    EXTRACT_32BITS(&dop->pd_page.p_uid),
    385 	    EXTRACT_32BITS(&dop->pd_sseq),
    386 	    EXTRACT_32BITS(&dop->pd_eseq)));
    387 
    388 	if (ndo->ndo_vflag)
    389 		return (wb_dops(ndo, (const struct dophdr *)(dop + 1),
    390 				EXTRACT_32BITS(&dop->pd_sseq),
    391 				EXTRACT_32BITS(&dop->pd_eseq)));
    392 	return (0);
    393 }
    394 
    395 /*
    396  * Print whiteboard multicast packets.
    397  */
    398 void
    399 wb_print(netdissect_options *ndo,
    400          register const void *hdr, register u_int len)
    401 {
    402 	register const struct pkt_hdr *ph;
    403 
    404 	ph = (const struct pkt_hdr *)hdr;
    405 	if (len < sizeof(*ph) || (u_char *)(ph + 1) > ndo->ndo_snapend) {
    406 		ND_PRINT((ndo, "%s", tstr));
    407 		return;
    408 	}
    409 	len -= sizeof(*ph);
    410 
    411 	if (ph->ph_flags)
    412 		ND_PRINT((ndo, "*"));
    413 	switch (ph->ph_type) {
    414 
    415 	case PT_KILL:
    416 		ND_PRINT((ndo, " wb-kill"));
    417 		return;
    418 
    419 	case PT_ID:
    420 		if (wb_id(ndo, (struct pkt_id *)(ph + 1), len) >= 0)
    421 			return;
    422 		break;
    423 
    424 	case PT_RREQ:
    425 		if (wb_rreq(ndo, (struct pkt_rreq *)(ph + 1), len) >= 0)
    426 			return;
    427 		break;
    428 
    429 	case PT_RREP:
    430 		if (wb_rrep(ndo, (struct pkt_rrep *)(ph + 1), len) >= 0)
    431 			return;
    432 		break;
    433 
    434 	case PT_DRAWOP:
    435 		if (wb_drawop(ndo, (struct pkt_dop *)(ph + 1), len) >= 0)
    436 			return;
    437 		break;
    438 
    439 	case PT_PREQ:
    440 		if (wb_preq(ndo, (struct pkt_preq *)(ph + 1), len) >= 0)
    441 			return;
    442 		break;
    443 
    444 	case PT_PREP:
    445 		if (wb_prep(ndo, (struct pkt_prep *)(ph + 1), len) >= 0)
    446 			return;
    447 		break;
    448 
    449 	default:
    450 		ND_PRINT((ndo, " wb-%d!", ph->ph_type));
    451 		return;
    452 	}
    453 }
    454