]> The Tcpdump Group git mirrors - tcpdump/blob - print-wb.c
OpenFlow: Have a function for each message type.
[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 /* XXX need to add byte-swapping macros! */
36 /* XXX - you mean like the ones in "extract.h"? */
37
38 /*
39 * Largest packet size. Everything should fit within this space.
40 * For instance, multiline objects are sent piecewise.
41 */
42 #define MAXFRAMESIZE 1024
43
44 /*
45 * Multiple drawing ops can be sent in one packet. Each one starts on a
46 * an even multiple of DOP_ALIGN bytes, which must be a power of two.
47 */
48 #define DOP_ALIGN 4
49 #define DOP_ROUNDUP(x) roundup2(x, DOP_ALIGN)
50 #define DOP_NEXT(d)\
51 ((const struct dophdr *)((const u_char *)(d) + \
52 DOP_ROUNDUP(GET_BE_U_2((d)->dh_len) + sizeof(*(d)))))
53
54 /*
55 * Format of the whiteboard packet header.
56 * The transport level header.
57 */
58 struct pkt_hdr {
59 nd_uint32_t ph_src; /* site id of source */
60 nd_uint32_t ph_ts; /* time stamp (for skew computation) */
61 nd_uint16_t ph_version; /* version number */
62 nd_uint8_t ph_type; /* message type */
63 nd_uint8_t ph_flags; /* message flags */
64 };
65
66 /* Packet types */
67 #define PT_DRAWOP 0 /* drawing operation */
68 #define PT_ID 1 /* announcement packet */
69 #define PT_RREQ 2 /* repair request */
70 #define PT_RREP 3 /* repair reply */
71 #define PT_KILL 4 /* terminate participation */
72 #define PT_PREQ 5 /* page vector request */
73 #define PT_PREP 7 /* page vector reply */
74
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
83 struct PageID {
84 nd_uint32_t p_sid; /* session id of initiator */
85 nd_uint32_t p_uid; /* page number */
86 };
87
88 struct dophdr {
89 nd_uint32_t dh_ts; /* sender's timestamp */
90 nd_uint16_t dh_len; /* body length */
91 nd_uint8_t dh_flags;
92 nd_uint8_t dh_type; /* body type */
93 /* body follows */
94 };
95 /*
96 * Drawing op sub-types.
97 */
98 #define DT_RECT 2
99 #define DT_LINE 3
100 #define DT_ML 4
101 #define DT_DEL 5
102 #define DT_XFORM 6
103 #define DT_ELL 7
104 #define DT_CHAR 8
105 #define DT_STR 9
106 #define DT_NOP 10
107 #define DT_PSCODE 11
108 #define DT_PSCOMP 12
109 #define DT_REF 13
110 #define DT_SKIP 14
111 #define DT_HOLE 15
112 #define DT_MAXTYPE 15
113
114 /*
115 * A drawing operation.
116 */
117 struct pkt_dop {
118 struct PageID pd_page; /* page that operations apply to */
119 nd_uint32_t pd_sseq; /* start sequence number */
120 nd_uint32_t pd_eseq; /* end sequence number */
121 /* drawing ops follow */
122 };
123
124 /*
125 * A repair request.
126 */
127 struct pkt_rreq {
128 nd_uint32_t pr_id; /* source id of drawops to be repaired */
129 struct PageID pr_page; /* page of drawops */
130 nd_uint32_t pr_sseq; /* start seqno */
131 nd_uint32_t pr_eseq; /* end seqno */
132 };
133
134 /*
135 * A repair reply.
136 */
137 struct pkt_rrep {
138 nd_uint32_t pr_id; /* original site id of ops */
139 struct pkt_dop pr_dop;
140 /* drawing ops follow */
141 };
142
143 struct id_off {
144 nd_uint32_t id;
145 nd_uint32_t off;
146 };
147
148 struct pgstate {
149 nd_uint32_t slot;
150 struct PageID page;
151 nd_uint16_t nid;
152 nd_uint16_t rsvd;
153 /* seqptr's */
154 };
155
156 /*
157 * An announcement packet.
158 */
159 struct pkt_id {
160 nd_uint32_t pi_mslot;
161 struct PageID pi_mpage; /* current page */
162 struct pgstate pi_ps;
163 /* seqptr's */
164 /* null-terminated site name */
165 };
166
167 struct pkt_preq {
168 struct PageID pp_page;
169 nd_uint32_t pp_low;
170 nd_uint32_t pp_high;
171 };
172
173 struct pkt_prep {
174 nd_uint32_t pp_n; /* size of pageid array */
175 /* pgstate's follow */
176 };
177
178 static int
179 wb_id(netdissect_options *ndo,
180 const struct pkt_id *id, u_int len)
181 {
182 u_int i;
183 const u_char *sitename;
184 const struct id_off *io;
185 char c;
186 u_int nid;
187
188 ND_PRINT(" wb-id:");
189 if (len < sizeof(*id) || !ND_TTEST_SIZE(id))
190 return (-1);
191 len -= sizeof(*id);
192
193 ND_PRINT(" %u/%s:%u (max %u/%s:%u) ",
194 GET_BE_U_4(id->pi_ps.slot),
195 GET_IPADDR_STRING(id->pi_ps.page.p_sid),
196 GET_BE_U_4(id->pi_ps.page.p_uid),
197 GET_BE_U_4(id->pi_mslot),
198 GET_IPADDR_STRING(id->pi_mpage.p_sid),
199 GET_BE_U_4(id->pi_mpage.p_uid));
200
201 nid = GET_BE_U_2(id->pi_ps.nid);
202 if (len < sizeof(*io) * nid)
203 return (-1);
204 len -= sizeof(*io) * nid;
205 io = (const struct id_off *)(id + 1);
206 sitename = (const u_char *)(io + nid);
207
208 c = '<';
209 for (i = 0; i < nid && ND_TTEST_SIZE(io); ++io, ++i) {
210 ND_PRINT("%c%s:%u",
211 c, GET_IPADDR_STRING(io->id), GET_BE_U_4(io->off));
212 c = ',';
213 }
214 if (i >= nid) {
215 ND_PRINT("> \"");
216 (void)nd_print(ndo, sitename, sitename + len);
217 ND_PRINT("\"");
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(" wb-rreq:");
228 if (len < sizeof(*rreq) || !ND_TTEST_SIZE(rreq))
229 return (-1);
230
231 ND_PRINT(" please repair %s %s:%u<%u:%u>",
232 GET_IPADDR_STRING(rreq->pr_id),
233 GET_IPADDR_STRING(rreq->pr_page.p_sid),
234 GET_BE_U_4(rreq->pr_page.p_uid),
235 GET_BE_U_4(rreq->pr_sseq),
236 GET_BE_U_4(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(" wb-preq:");
245 if (len < sizeof(*preq) || !ND_TTEST_SIZE(preq))
246 return (-1);
247
248 ND_PRINT(" need %u/%s:%u",
249 GET_BE_U_4(preq->pp_low),
250 GET_IPADDR_STRING(preq->pp_page.p_sid),
251 GET_BE_U_4(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 u_int n;
260 const struct pgstate *ps;
261 const u_char *ep = ndo->ndo_snapend;
262
263 ND_PRINT(" wb-prep:");
264 if (len < sizeof(*prep) || !ND_TTEST_SIZE(prep))
265 return (-1);
266 n = GET_BE_U_4(prep->pp_n);
267 ps = (const struct pgstate *)(prep + 1);
268 while (n != 0 && ND_TTEST_SIZE(ps)) {
269 const struct id_off *io, *ie;
270 char c = '<';
271
272 ND_PRINT(" %u/%s:%u",
273 GET_BE_U_4(ps->slot),
274 GET_IPADDR_STRING(ps->page.p_sid),
275 GET_BE_U_4(ps->page.p_uid));
276 io = (const struct id_off *)(ps + 1);
277 for (ie = io + GET_U_1(ps->nid); io < ie && ND_TTEST_SIZE(io); ++io) {
278 ND_PRINT("%c%s:%u", c, GET_IPADDR_STRING(io->id),
279 GET_BE_U_4(io->off));
280 c = ',';
281 }
282 ND_PRINT(">");
283 ps = (const struct pgstate *)io;
284 n--;
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(" <");
316 for ( ; ss <= es; ++ss) {
317 u_int t;
318
319 if (!ND_TTEST_SIZE(dh)) {
320 nd_print_trunc(ndo);
321 break;
322 }
323 t = GET_U_1(dh->dh_type);
324
325 if (t > DT_MAXTYPE)
326 ND_PRINT(" dop-%u!", t);
327 else {
328 ND_PRINT(" %s", dopstr[t]);
329 if (t == DT_SKIP || t == DT_HOLE) {
330 uint32_t ts = GET_BE_U_4(dh->dh_ts);
331 ND_PRINT("%u", ts - ss + 1);
332 if (ss > ts || ts > es) {
333 ND_PRINT("[|]");
334 if (ts < ss)
335 return (0);
336 }
337 ss = ts;
338 }
339 }
340 dh = DOP_NEXT(dh);
341 }
342 ND_PRINT(" >");
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(" wb-rrep:");
353 if (len < sizeof(*rrep) || !ND_TTEST_SIZE(rrep))
354 return (-1);
355 len -= sizeof(*rrep);
356
357 ND_PRINT(" for %s %s:%u<%u:%u>",
358 GET_IPADDR_STRING(rrep->pr_id),
359 GET_IPADDR_STRING(dop->pd_page.p_sid),
360 GET_BE_U_4(dop->pd_page.p_uid),
361 GET_BE_U_4(dop->pd_sseq),
362 GET_BE_U_4(dop->pd_eseq));
363
364 if (ndo->ndo_vflag)
365 return (wb_dops(ndo, dop,
366 GET_BE_U_4(dop->pd_sseq),
367 GET_BE_U_4(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(" wb-dop:");
376 if (len < sizeof(*dop) || !ND_TTEST_SIZE(dop))
377 return (-1);
378 len -= sizeof(*dop);
379
380 ND_PRINT(" %s:%u<%u:%u>",
381 GET_IPADDR_STRING(dop->pd_page.p_sid),
382 GET_BE_U_4(dop->pd_page.p_uid),
383 GET_BE_U_4(dop->pd_sseq),
384 GET_BE_U_4(dop->pd_eseq));
385
386 if (ndo->ndo_vflag)
387 return (wb_dops(ndo, dop,
388 GET_BE_U_4(dop->pd_sseq),
389 GET_BE_U_4(dop->pd_eseq)));
390 return (0);
391 }
392
393 /*
394 * Print whiteboard multicast packets.
395 */
396 void
397 wb_print(netdissect_options *ndo,
398 const u_char *hdr, u_int len)
399 {
400 const struct pkt_hdr *ph;
401 uint8_t type;
402
403 ndo->ndo_protocol = "wb";
404 ph = (const struct pkt_hdr *)hdr;
405 if (len < sizeof(*ph) || !ND_TTEST_SIZE(ph)) {
406 nd_print_trunc(ndo);
407 return;
408 }
409 len -= sizeof(*ph);
410
411 if (GET_U_1(ph->ph_flags))
412 ND_PRINT("*");
413 type = GET_U_1(ph->ph_type);
414 switch (type) {
415
416 case PT_KILL:
417 ND_PRINT(" wb-kill");
418 return;
419
420 case PT_ID:
421 if (wb_id(ndo, (const struct pkt_id *)(ph + 1), len) >= 0)
422 return;
423 nd_print_trunc(ndo);
424 break;
425
426 case PT_RREQ:
427 if (wb_rreq(ndo, (const struct pkt_rreq *)(ph + 1), len) >= 0)
428 return;
429 nd_print_trunc(ndo);
430 break;
431
432 case PT_RREP:
433 if (wb_rrep(ndo, (const struct pkt_rrep *)(ph + 1), len) >= 0)
434 return;
435 nd_print_trunc(ndo);
436 break;
437
438 case PT_DRAWOP:
439 if (wb_drawop(ndo, (const struct pkt_dop *)(ph + 1), len) >= 0)
440 return;
441 nd_print_trunc(ndo);
442 break;
443
444 case PT_PREQ:
445 if (wb_preq(ndo, (const struct pkt_preq *)(ph + 1), len) >= 0)
446 return;
447 nd_print_trunc(ndo);
448 break;
449
450 case PT_PREP:
451 if (wb_prep(ndo, (const struct pkt_prep *)(ph + 1), len) >= 0)
452 return;
453 nd_print_trunc(ndo);
454 break;
455
456 default:
457 ND_PRINT(" wb-%u!", type);
458 return;
459 }
460 }