print-wb.c 11 KB

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