]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-wb.c
DNS: Sync types with IANA
[tcpdump] / print-wb.c
index d1f39d28b9447e51a2ea6b67270f0383e6fb6e36..35b5a196d8bfe6096a9711fe8c4e612db01c6009 100644 (file)
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
-#ifndef lint
-static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-wb.c,v 1.25 1999-11-21 09:37:03 fenner Exp $ (LBL)";
-#endif
+/* \summary: White Board printer */
 
 #ifdef HAVE_CONFIG_H
-#include "config.h"
+#include <config.h>
 #endif
 
-#include <sys/types.h>
-#include <sys/time.h>
-
-#include <netinet/in.h>
+#include "netdissect-stdinc.h"
 
-#include <stdio.h>
-
-#include "interface.h"
+#define ND_LONGJMP_FROM_TCHECK
+#include "netdissect.h"
 #include "addrtoname.h"
+#include "extract.h"
 
-/* XXX need to add byte-swapping macros! */
 
+#if 0
 /*
  * Largest packet size.  Everything should fit within this space.
  * For instance, multiline objects are sent piecewise.
  */
 #define MAXFRAMESIZE 1024
+#endif
 
 /*
  * Multiple drawing ops can be sent in one packet.  Each one starts on a
  * an even multiple of DOP_ALIGN bytes, which must be a power of two.
  */
 #define DOP_ALIGN 4
-#define DOP_ROUNDUP(x) ((((int)(x)) + (DOP_ALIGN - 1)) & ~(DOP_ALIGN - 1))
+#define DOP_ROUNDUP(x) roundup2(x, DOP_ALIGN)
 #define DOP_NEXT(d)\
-       ((struct dophdr *)((u_char *)(d) + \
-                         DOP_ROUNDUP(ntohs((d)->dh_len) + sizeof(*(d)))))
+       ((const struct dophdr *)((const u_char *)(d) + \
+                               DOP_ROUNDUP(GET_BE_U_2((d)->dh_len) + sizeof(*(d)))))
 
 /*
  * Format of the whiteboard packet header.
  * The transport level header.
  */
 struct pkt_hdr {
-       u_int32_t ph_src;               /* site id of source */
-       u_int32_t ph_ts;                /* time stamp (for skew computation) */
-       u_short ph_version;     /* version number */
-       u_char ph_type;         /* message type */
-       u_char ph_flags;        /* message flags */
+       nd_uint32_t ph_src;     /* site id of source */
+       nd_uint32_t ph_ts;      /* time stamp (for skew computation) */
+       nd_uint16_t ph_version; /* version number */
+       nd_uint8_t ph_type;     /* message type */
+       nd_uint8_t ph_flags;    /* message flags */
 };
 
 /* Packet types */
@@ -77,20 +72,26 @@ struct pkt_hdr {
 #define PT_PREQ         5       /* page vector request */
 #define PT_PREP         7       /* page vector reply */
 
+#if 0
+#ifdef PF_USER
+#undef PF_USER                 /* {Digital,Tru64} UNIX define this, alas */
+#endif
+
 /* flags */
 #define PF_USER                0x01    /* hint that packet has interactive data */
 #define PF_VIS         0x02    /* only visible ops wanted */
+#endif
 
 struct PageID {
-       u_int32_t p_sid;                /* session id of initiator */
-       u_int32_t p_uid;                /* page number */
+       nd_uint32_t p_sid;              /* session id of initiator */
+       nd_uint32_t p_uid;              /* page number */
 };
 
 struct dophdr {
-       u_int32_t  dh_ts;               /* sender's timestamp */
-       u_short dh_len;         /* body length */
-       u_char  dh_flags;
-       u_char  dh_type;        /* body type */
+       nd_uint32_t     dh_ts;          /* sender's timestamp */
+       nd_uint16_t     dh_len;         /* body length */
+       nd_uint8_t      dh_flags;
+       nd_uint8_t      dh_type;        /* body type */
        /* body follows */
 };
 /*
@@ -110,15 +111,31 @@ struct dophdr {
 #define DT_REF          13
 #define DT_SKIP         14
 #define DT_HOLE         15
-#define DT_MAXTYPE      15
+static const struct tok dop_str[] = {
+       { DT_RECT,   "RECT"   },
+       { DT_LINE,   "LINE"   },
+       { DT_ML,     "ML"     },
+       { DT_DEL,    "DEL"    },
+       { DT_XFORM,  "XFORM"  },
+       { DT_ELL,    "ELL"    },
+       { DT_CHAR,   "CHAR"   },
+       { DT_STR,    "STR"    },
+       { DT_NOP,    "NOP"    },
+       { DT_PSCODE, "PSCODE" },
+       { DT_PSCOMP, "PSCOMP" },
+       { DT_REF,    "REF"    },
+       { DT_SKIP,   "SKIP"   },
+       { DT_HOLE,   "HOLE"   },
+       { 0, NULL }
+};
 
 /*
  * A drawing operation.
  */
 struct pkt_dop {
        struct PageID pd_page;  /* page that operations apply to */
-       u_int32_t       pd_sseq;        /* start sequence number */
-       u_int32_t       pd_eseq;        /* end sequence number */
+       nd_uint32_t     pd_sseq;        /* start sequence number */
+       nd_uint32_t     pd_eseq;        /* end sequence number */
        /* drawing ops follow */
 };
 
@@ -126,31 +143,31 @@ struct pkt_dop {
  * A repair request.
  */
 struct pkt_rreq {
-        u_int32_t pr_id;           /* source id of drawops to be repaired */
-        struct PageID pr_page;           /* page of drawops */
-        u_int32_t pr_sseq;         /* start seqno */
-        u_int32_t pr_eseq;         /* end seqno */
+        nd_uint32_t pr_id;        /* source id of drawops to be repaired */
+        struct PageID pr_page;    /* page of drawops */
+        nd_uint32_t pr_sseq;      /* start seqno */
+        nd_uint32_t pr_eseq;      /* end seqno */
 };
 
 /*
  * A repair reply.
  */
 struct pkt_rrep {
-       u_int32_t pr_id;        /* original site id of ops  */
+       nd_uint32_t pr_id;      /* original site id of ops  */
        struct pkt_dop pr_dop;
        /* drawing ops follow */
 };
 
 struct id_off {
-        u_int32_t id;
-        u_int32_t off;
+        nd_uint32_t id;
+        nd_uint32_t off;
 };
 
 struct pgstate {
-       u_int32_t slot;
+       nd_uint32_t slot;
        struct PageID page;
-       u_short nid;
-       u_short rsvd;
+       nd_uint16_t nid;
+       nd_uint16_t rsvd;
         /* seqptr's */
 };
 
@@ -158,7 +175,7 @@ struct pgstate {
  * An announcement packet.
  */
 struct pkt_id {
-       u_int32_t pi_mslot;
+       nd_uint32_t pi_mslot;
         struct PageID    pi_mpage;        /* current page */
        struct pgstate pi_ps;
         /* seqptr's */
@@ -167,215 +184,200 @@ struct pkt_id {
 
 struct pkt_preq {
         struct PageID  pp_page;
-        u_int32_t  pp_low;
-        u_int32_t  pp_high;
+        nd_uint32_t  pp_low;
+        nd_uint32_t  pp_high;
 };
 
 struct pkt_prep {
-        u_int32_t  pp_n;           /* size of pageid array */
+        nd_uint32_t  pp_n;           /* size of pageid array */
         /* pgstate's follow */
 };
 
 static int
-wb_id(const struct pkt_id *id, u_int len)
+wb_id(netdissect_options *ndo,
+      const struct pkt_id *id, u_int len)
 {
-       int i;
-       const char *cp;
+       u_int i;
+       const u_char *sitename;
        const struct id_off *io;
        char c;
-       int nid;
+       u_int nid;
 
-       printf(" wb-id:");
-       if (len < sizeof(*id) || (u_char *)(id + 1) > snapend)
+       ND_PRINT(" wb-id:");
+       if (len < sizeof(*id))
                return (-1);
        len -= sizeof(*id);
 
-       printf(" %u/%s:%u (max %u/%s:%u) ",
-              (u_int32_t)ntohl(id->pi_ps.slot),
-              ipaddr_string(&id->pi_ps.page.p_sid),
-              (u_int32_t)ntohl(id->pi_ps.page.p_uid),
-              (u_int32_t)ntohl(id->pi_mslot),
-              ipaddr_string(&id->pi_mpage.p_sid),
-              (u_int32_t)ntohl(id->pi_mpage.p_uid));
-
-       nid = ntohs(id->pi_ps.nid);
+       ND_PRINT(" %u/%s:%u (max %u/%s:%u) ",
+              GET_BE_U_4(id->pi_ps.slot),
+              GET_IPADDR_STRING(id->pi_ps.page.p_sid),
+              GET_BE_U_4(id->pi_ps.page.p_uid),
+              GET_BE_U_4(id->pi_mslot),
+              GET_IPADDR_STRING(id->pi_mpage.p_sid),
+              GET_BE_U_4(id->pi_mpage.p_uid));
+       /* now the rest of the fixed-size part of struct pkt_id */
+       ND_TCHECK_SIZE(id);
+
+       nid = GET_BE_U_2(id->pi_ps.nid);
+       if (len < sizeof(*io) * nid)
+               return (-1);
        len -= sizeof(*io) * nid;
-       io = (struct id_off *)(id + 1);
-       cp = (char *)(io + nid);
-       if ((u_char *)cp + len <= snapend) {
-               putchar('"');
-               (void)fn_print((u_char *)cp, (u_char *)cp + len);
-               putchar('"');
-       }
+       io = (const struct id_off *)(id + 1);
+       sitename = (const u_char *)(io + nid);
 
        c = '<';
-       for (i = 0; i < nid && (u_char *)io < snapend; ++io, ++i) {
-               printf("%c%s:%u",
-                   c, ipaddr_string(&io->id), (u_int32_t)ntohl(io->off));
+       for (i = 0; i < nid; ++io, ++i) {
+               ND_PRINT("%c%s:%u",
+                   c, GET_IPADDR_STRING(io->id), GET_BE_U_4(io->off));
                c = ',';
        }
-       if (i >= nid) {
-               printf(">");
-               return (0);
-       }
-       return (-1);
+       ND_PRINT("> \"");
+       nd_printjnp(ndo, sitename, len);
+       ND_PRINT("\"");
+       return (0);
 }
 
 static int
-wb_rreq(const struct pkt_rreq *rreq, u_int len)
+wb_rreq(netdissect_options *ndo,
+        const struct pkt_rreq *rreq, u_int len)
 {
-       printf(" wb-rreq:");
-       if (len < sizeof(*rreq) || (u_char *)(rreq + 1) > snapend)
+       ND_PRINT(" wb-rreq:");
+       if (len < sizeof(*rreq))
                return (-1);
 
-       printf(" please repair %s %s:%u<%u:%u>",
-              ipaddr_string(&rreq->pr_id),
-              ipaddr_string(&rreq->pr_page.p_sid),
-              (u_int32_t)ntohl(rreq->pr_page.p_uid),
-              (u_int32_t)ntohl(rreq->pr_sseq),
-              (u_int32_t)ntohl(rreq->pr_eseq));
+       ND_PRINT(" please repair %s %s:%u<%u:%u>",
+              GET_IPADDR_STRING(rreq->pr_id),
+              GET_IPADDR_STRING(rreq->pr_page.p_sid),
+              GET_BE_U_4(rreq->pr_page.p_uid),
+              GET_BE_U_4(rreq->pr_sseq),
+              GET_BE_U_4(rreq->pr_eseq));
        return (0);
 }
 
 static int
-wb_preq(const struct pkt_preq *preq, u_int len)
+wb_preq(netdissect_options *ndo,
+        const struct pkt_preq *preq, u_int len)
 {
-       printf(" wb-preq:");
-       if (len < sizeof(*preq) || (u_char *)(preq + 1) > snapend)
+       ND_PRINT(" wb-preq:");
+       if (len < sizeof(*preq))
                return (-1);
 
-       printf(" need %u/%s:%u",
-              (u_int32_t)ntohl(preq->pp_low),
-              ipaddr_string(&preq->pp_page.p_sid),
-              (u_int32_t)ntohl(preq->pp_page.p_uid));
+       ND_PRINT(" need %u/%s:%u",
+              GET_BE_U_4(preq->pp_low),
+              GET_IPADDR_STRING(preq->pp_page.p_sid),
+              GET_BE_U_4(preq->pp_page.p_uid));
+       /* now the rest of the fixed-size part of struct pkt_req */
+       ND_TCHECK_SIZE(preq);
        return (0);
 }
 
 static int
-wb_prep(const struct pkt_prep *prep, u_int len)
+wb_prep(netdissect_options *ndo,
+        const struct pkt_prep *prep, u_int len)
 {
-       int n;
+       u_int n;
        const struct pgstate *ps;
-       const u_char *ep = snapend;
 
-       printf(" wb-prep:");
-       if (len < sizeof(*prep)) {
+       ND_PRINT(" wb-prep:");
+       if (len < sizeof(*prep))
                return (-1);
-       }
-       n = ntohl(prep->pp_n);
+       n = GET_BE_U_4(prep->pp_n);
        ps = (const struct pgstate *)(prep + 1);
-       while (--n >= 0 && (u_char *)ps < ep) {
+       while (n != 0) {
                const struct id_off *io, *ie;
                char c = '<';
 
-               printf(" %u/%s:%u",
-                   (u_int32_t)ntohl(ps->slot),
-                   ipaddr_string(&ps->page.p_sid),
-                   (u_int32_t)ntohl(ps->page.p_uid));
-               io = (struct id_off *)(ps + 1);
-               for (ie = io + ps->nid; io < ie && (u_char *)io < ep; ++io) {
-                       printf("%c%s:%u", c, ipaddr_string(&io->id),
-                           (u_int32_t)ntohl(io->off));
+               ND_PRINT(" %u/%s:%u",
+                   GET_BE_U_4(ps->slot),
+                   GET_IPADDR_STRING(ps->page.p_sid),
+                   GET_BE_U_4(ps->page.p_uid));
+               /* now the rest of the fixed-size part of struct pgstate */
+               ND_TCHECK_SIZE(ps);
+               io = (const struct id_off *)(ps + 1);
+               for (ie = io + GET_U_1(ps->nid); io < ie; ++io) {
+                       ND_PRINT("%c%s:%u", c, GET_IPADDR_STRING(io->id),
+                           GET_BE_U_4(io->off));
                        c = ',';
                }
-               printf(">");
-               ps = (struct pgstate *)io;
+               ND_PRINT(">");
+               ps = (const struct pgstate *)io;
+               n--;
        }
-       return ((u_char *)ps <= ep? 0 : -1);
+       return 0;
 }
 
-
-char *dopstr[] = {
-       "dop-0!",
-       "dop-1!",
-       "RECT",
-       "LINE",
-       "ML",
-       "DEL",
-       "XFORM",
-       "ELL",
-       "CHAR",
-       "STR",
-       "NOP",
-       "PSCODE",
-       "PSCOMP",
-       "REF",
-       "SKIP",
-       "HOLE",
-};
-
-static int
-wb_dops(const struct dophdr *dh, u_int32_t ss, u_int32_t es)
+static void
+wb_dops(netdissect_options *ndo, const struct pkt_dop *dop,
+        uint32_t ss, uint32_t es)
 {
-       printf(" <");
+       const struct dophdr *dh = (const struct dophdr *)((const u_char *)dop + sizeof(*dop));
+
+       ND_PRINT(" <");
        for ( ; ss <= es; ++ss) {
-               register int t = dh->dh_type;
-
-               if (t > DT_MAXTYPE)
-                       printf(" dop-%d!", t);
-               else {
-                       printf(" %s", dopstr[t]);
-                       if (t == DT_SKIP || t == DT_HOLE) {
-                               int ts = ntohl(dh->dh_ts);
-                               printf("%d", ts - ss + 1);
-                               if (ss > ts || ts > es) {
-                                       printf("[|]");
-                                       if (ts < ss)
-                                               return (0);
-                               }
-                               ss = ts;
+               u_int t;
+
+               t = GET_U_1(dh->dh_type);
+
+               ND_PRINT(" %s", tok2str(dop_str, "dop-%u!", t));
+               if (t == DT_SKIP || t == DT_HOLE) {
+                       uint32_t ts = GET_BE_U_4(dh->dh_ts);
+                       ND_PRINT("%u", ts - ss + 1);
+                       if (ss > ts || ts > es) {
+                               ND_PRINT("[|]");
+                               if (ts < ss)
+                                       return;
                        }
+                       ss = ts;
                }
                dh = DOP_NEXT(dh);
-               if ((u_char *)dh > snapend) {
-                       printf("[|wb]");
-                       break;
-               }
        }
-       printf(" >");
-       return (0);
+       ND_PRINT(" >");
 }
 
 static int
-wb_rrep(const struct pkt_rrep *rrep, u_int len)
+wb_rrep(netdissect_options *ndo,
+        const struct pkt_rrep *rrep, u_int len)
 {
        const struct pkt_dop *dop = &rrep->pr_dop;
 
-       printf(" wb-rrep:");
-       if (len < sizeof(*rrep) || (u_char *)(rrep + 1) > snapend)
+       ND_PRINT(" wb-rrep:");
+       if (len < sizeof(*rrep))
                return (-1);
        len -= sizeof(*rrep);
 
-       printf(" for %s %s:%u<%u:%u>",
-           ipaddr_string(&rrep->pr_id),
-           ipaddr_string(&dop->pd_page.p_sid),
-           (u_int32_t)ntohl(dop->pd_page.p_uid),
-           (u_int32_t)ntohl(dop->pd_sseq),
-           (u_int32_t)ntohl(dop->pd_eseq));
-
-       if (vflag)
-               return (wb_dops((const struct dophdr *)(dop + 1),
-                   ntohl(dop->pd_sseq), ntohl(dop->pd_eseq)));
+       ND_PRINT(" for %s %s:%u<%u:%u>",
+           GET_IPADDR_STRING(rrep->pr_id),
+           GET_IPADDR_STRING(dop->pd_page.p_sid),
+           GET_BE_U_4(dop->pd_page.p_uid),
+           GET_BE_U_4(dop->pd_sseq),
+           GET_BE_U_4(dop->pd_eseq));
+
+       if (ndo->ndo_vflag)
+               wb_dops(ndo, dop,
+                       GET_BE_U_4(dop->pd_sseq),
+                       GET_BE_U_4(dop->pd_eseq));
        return (0);
 }
 
 static int
-wb_drawop(const struct pkt_dop *dop, u_int len)
+wb_drawop(netdissect_options *ndo,
+          const struct pkt_dop *dop, u_int len)
 {
-       printf(" wb-dop:");
-       if (len < sizeof(*dop) || (u_char *)(dop + 1) > snapend)
+       ND_PRINT(" wb-dop:");
+       if (len < sizeof(*dop))
                return (-1);
        len -= sizeof(*dop);
 
-       printf(" %s:%u<%u:%u>",
-           ipaddr_string(&dop->pd_page.p_sid),
-           (u_int32_t)ntohl(dop->pd_page.p_uid),
-           (u_int32_t)ntohl(dop->pd_sseq),
-           (u_int32_t)ntohl(dop->pd_eseq));
+       ND_PRINT(" %s:%u<%u:%u>",
+           GET_IPADDR_STRING(dop->pd_page.p_sid),
+           GET_BE_U_4(dop->pd_page.p_uid),
+           GET_BE_U_4(dop->pd_sseq),
+           GET_BE_U_4(dop->pd_eseq));
 
-       if (vflag)
-               return (wb_dops((const struct dophdr *)(dop + 1),
-                               ntohl(dop->pd_sseq), ntohl(dop->pd_eseq)));
+       if (ndo->ndo_vflag)
+               wb_dops(ndo, dop,
+                       GET_BE_U_4(dop->pd_sseq),
+                       GET_BE_U_4(dop->pd_eseq));
        return (0);
 }
 
@@ -383,57 +385,61 @@ wb_drawop(const struct pkt_dop *dop, u_int len)
  * Print whiteboard multicast packets.
  */
 void
-wb_print(register const void *hdr, register u_int len)
+wb_print(netdissect_options *ndo,
+         const u_char *hdr, u_int len)
 {
-       register const struct pkt_hdr *ph;
+       const struct pkt_hdr *ph;
+       uint8_t type;
+       int print_result;
 
+       ndo->ndo_protocol = "wb";
        ph = (const struct pkt_hdr *)hdr;
-       if (len < sizeof(*ph) || (u_char *)(ph + 1) > snapend) {
-               printf("[|wb]");
-               return;
-       }
+       if (len < sizeof(*ph))
+               goto invalid;
+       ND_TCHECK_SIZE(ph);
        len -= sizeof(*ph);
 
-       if (ph->ph_flags)
-               printf("*");
-       switch (ph->ph_type) {
+       if (GET_U_1(ph->ph_flags))
+               ND_PRINT("*");
+       type = GET_U_1(ph->ph_type);
+       switch (type) {
 
        case PT_KILL:
-               printf(" wb-kill");
+               ND_PRINT(" wb-kill");
                return;
 
        case PT_ID:
-               if (wb_id((struct pkt_id *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_id(ndo, (const struct pkt_id *)(ph + 1), len);
                break;
 
        case PT_RREQ:
-               if (wb_rreq((struct pkt_rreq *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_rreq(ndo, (const struct pkt_rreq *)(ph + 1), len);
                break;
 
        case PT_RREP:
-               if (wb_rrep((struct pkt_rrep *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_rrep(ndo, (const struct pkt_rrep *)(ph + 1), len);
                break;
 
        case PT_DRAWOP:
-               if (wb_drawop((struct pkt_dop *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_drawop(ndo, (const struct pkt_dop *)(ph + 1), len);
                break;
 
        case PT_PREQ:
-               if (wb_preq((struct pkt_preq *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_preq(ndo, (const struct pkt_preq *)(ph + 1), len);
                break;
 
        case PT_PREP:
-               if (wb_prep((struct pkt_prep *)(ph + 1), len) >= 0)
-                       return;
+               print_result = wb_prep(ndo, (const struct pkt_prep *)(ph + 1), len);
                break;
 
        default:
-               printf(" wb-%d!", ph->ph_type);
-               return;
+               ND_PRINT(" wb-%u!", type);
+               print_result = -1;
        }
+       if (print_result < 0)
+               goto invalid;
+       return;
+
+invalid:
+       nd_print_invalid(ndo);
 }