OSDN Git Service

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