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