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