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