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