]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-atalk.c
More UNALIGNED_MEM{CPY,CMP} on IP addresses.
[tcpdump] / print-atalk.c
index 9bce53fe1b1b960d42b5453928bc496866a548a7..7546543bce9d456e505b2a5b129284c93be5fd72 100644 (file)
  * Format and print AppleTalk packets.
  */
 
-#ifndef lint
-static const char rcsid[] =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-atalk.c,v 1.57 2000-09-23 08:54:25 guy Exp $ (LBL)";
-#endif
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <sys/param.h>
-#include <sys/time.h>
-#include <sys/socket.h>
-
-#if __STDC__
-struct mbuf;
-struct rtentry;
-#endif
-#include <net/if.h>
-
-#include <netinet/in.h>
-#include <netinet/in_systm.h>
+#include <tcpdump-stdinc.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <pcap.h>
 
 #include "interface.h"
 #include "addrtoname.h"
 #include "ethertype.h"
 #include "extract.h"                   /* must come after interface.h */
 #include "appletalk.h"
-#include "savestr.h"
 
-static struct tok type2str[] = {
+static const char tstr[] = "[|atalk]";
+
+static const struct tok type2str[] = {
        { ddpRTMP,              "rtmp" },
        { ddpRTMPrequest,       "rtmpReq" },
        { ddpECHO,              "echo" },
@@ -74,8 +60,6 @@ struct aarp {
        u_int8_t        pdaddr[4];
 };
 
-static char tstr[] = "[|atalk]";
-
 static void atp_print(const struct atATP *, u_int);
 static void atp_bitmap_print(u_char);
 static void nbp_print(const struct atNBP *, u_int, u_short, u_char, u_char);
@@ -90,25 +74,40 @@ static void ddp_print(const u_char *, u_int, int, u_short, u_char, u_char);
 static const char *ddpskt_string(int);
 
 /*
- * Print AppleTalk Datagram Delivery Protocol packets.
+ * Print LLAP packets received on a physical LocalTalk interface.
  */
-void
-atalk_print(register const u_char *bp, u_int length)
+u_int
+ltalk_if_print(const struct pcap_pkthdr *h, const u_char *p)
+{
+       return (llap_print(p, h->caplen));
+}
+
+/*
+ * Print AppleTalk LLAP packets.
+ */
+u_int
+llap_print(register const u_char *bp, u_int length)
 {
        register const struct LAP *lp;
        register const struct atDDP *dp;
        register const struct atShortDDP *sdp;
        u_short snet;
+       u_int hdrlen;
 
-       lp = (struct LAP *)bp;
+       if (length < sizeof(*lp)) {
+               (void)printf(" [|llap %u]", length);
+               return (length);
+       }
+       lp = (const struct LAP *)bp;
        bp += sizeof(*lp);
        length -= sizeof(*lp);
+       hdrlen = sizeof(*lp);
        switch (lp->type) {
 
        case lapShortDDP:
                if (length < ddpSSize) {
-                       (void)printf(" [|sddp %d]", length);
-                       return;
+                       (void)printf(" [|sddp %u]", length);
+                       return (length);
                }
                sdp = (const struct atShortDDP *)bp;
                printf("%s.%s",
@@ -117,13 +116,14 @@ atalk_print(register const u_char *bp, u_int length)
                    ataddr_string(0, lp->dst), ddpskt_string(sdp->dstSkt));
                bp += ddpSSize;
                length -= ddpSSize;
+               hdrlen += ddpSSize;
                ddp_print(bp, length, sdp->type, 0, lp->src, sdp->srcSkt);
                break;
 
        case lapDDP:
                if (length < ddpSize) {
-                       (void)printf(" [|ddp %d]", length);
-                       return;
+                       (void)printf(" [|ddp %u]", length);
+                       return (length);
                }
                dp = (const struct atDDP *)bp;
                snet = EXTRACT_16BITS(&dp->srcNet);
@@ -134,6 +134,7 @@ atalk_print(register const u_char *bp, u_int length)
                    ddpskt_string(dp->dstSkt));
                bp += ddpSize;
                length -= ddpSize;
+               hdrlen += ddpSize;
                ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
                break;
 
@@ -144,10 +145,41 @@ atalk_print(register const u_char *bp, u_int length)
 #endif
 
        default:
-               printf("%d > %d at-lap#%d %d",
+               printf("%d > %d at-lap#%d %u",
                    lp->src, lp->dst, lp->type, length);
                break;
        }
+       return (hdrlen);
+}
+
+/*
+ * Print EtherTalk/TokenTalk packets (or FDDITalk, or whatever it's called
+ * when it runs over FDDI; yes, I've seen FDDI captures with AppleTalk
+ * packets in them).
+ */
+void
+atalk_print(register const u_char *bp, u_int length)
+{
+       register const struct atDDP *dp;
+       u_short snet;
+
+        if(!eflag)
+            printf("AT ");
+
+       if (length < ddpSize) {
+               (void)printf(" [|ddp %u]", length);
+               return;
+       }
+       dp = (const struct atDDP *)bp;
+       snet = EXTRACT_16BITS(&dp->srcNet);
+       printf("%s.%s", ataddr_string(snet, dp->srcNode),
+              ddpskt_string(dp->srcSkt));
+       printf(" > %s.%s: ",
+              ataddr_string(EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
+              ddpskt_string(dp->dstSkt));
+       bp += ddpSize;
+       length -= ddpSize;
+       ddp_print(bp, length, dp->type, snet, dp->srcNode, dp->srcSkt);
 }
 
 /* XXX should probably pass in the snap header and do checks like arp_print() */
@@ -160,9 +192,10 @@ aarp_print(register const u_char *bp, u_int length)
 
        printf("aarp ");
        ap = (const struct aarp *)bp;
-       if (ntohs(ap->htype) == 1 && ntohs(ap->ptype) == ETHERTYPE_ATALK &&
+       if (EXTRACT_16BITS(&ap->htype) == 1 &&
+           EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
            ap->halen == 6 && ap->palen == 4 )
-               switch (ntohs(ap->op)) {
+               switch (EXTRACT_16BITS(&ap->op)) {
 
                case 1:                         /* request */
                        (void)printf("who-has %s tell %s",
@@ -171,7 +204,7 @@ aarp_print(register const u_char *bp, u_int length)
 
                case 2:                         /* response */
                        (void)printf("reply %s is-at %s",
-                           AT(pdaddr), etheraddr_string(ap->hdaddr));
+                           AT(psaddr), etheraddr_string(ap->hsaddr));
                        return;
 
                case 3:                         /* probe (oy!) */
@@ -180,10 +213,13 @@ aarp_print(register const u_char *bp, u_int length)
                        return;
                }
        (void)printf("len %u op %u htype %u ptype %#x halen %u palen %u",
-           length, ntohs(ap->op), ntohs(ap->htype), ntohs(ap->ptype),
-           ap->halen, ap->palen);
+           length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
+           EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen);
 }
 
+/*
+ * Print AppleTalk Datagram Delivery Protocol packets.
+ */
 static void
 ddp_print(register const u_char *bp, register u_int length, register int t,
          register u_short snet, register u_char snode, u_char skt)
@@ -199,6 +235,10 @@ ddp_print(register const u_char *bp, register u_int length, register int t,
                atp_print((const struct atATP *)bp, length);
                break;
 
+       case ddpEIGRP:
+               eigrp_print(bp, length);
+               break;
+
        default:
                (void)printf(" at-%s %d", tok2str(type2str, NULL, t), length);
                break;
@@ -216,6 +256,10 @@ atp_print(register const struct atATP *ap, u_int length)
                fputs(tstr, stdout);
                return;
        }
+       if (length < sizeof(*ap)) {
+               (void)printf(" [|atp %u]", length);
+               return;
+       }
        length -= sizeof(*ap);
        switch (ap->control & 0xc0) {
 
@@ -227,7 +271,7 @@ atp_print(register const struct atATP *ap, u_int length)
                atp_bitmap_print(ap->bitmap);
 
                if (length != 0)
-                       (void)printf(" [len=%d]", length);
+                       (void)printf(" [len=%u]", length);
 
                switch (ap->control & (atpEOM|atpSTS)) {
                case atpEOM:
@@ -243,7 +287,7 @@ atp_print(register const struct atATP *ap, u_int length)
                break;
 
        case atpRspCode:
-               (void)printf(" atp-resp%s%d:%d (%d)",
+               (void)printf(" atp-resp%s%d:%d (%u)",
                             ap->control & atpEOM? "*" : " ",
                             EXTRACT_16BITS(&ap->transID), ap->bitmap, length);
                switch (ap->control & (atpXO|atpSTS)) {
@@ -266,7 +310,7 @@ atp_print(register const struct atATP *ap, u_int length)
 
                /* length should be zero */
                if (length)
-                       (void)printf(" [len=%d]", length);
+                       (void)printf(" [len=%u]", length);
 
                /* there shouldn't be any control flags */
                if (ap->control & (atpXO|atpEOM|atpSTS)) {
@@ -288,7 +332,7 @@ atp_print(register const struct atATP *ap, u_int length)
                break;
 
        default:
-               (void)printf(" atp-0x%x  %d (%d)", ap->control,
+               (void)printf(" atp-0x%x  %d (%u)", ap->control,
                             EXTRACT_16BITS(&ap->transID), length);
                break;
        }
@@ -333,14 +377,19 @@ nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
          register u_char snode, register u_char skt)
 {
        register const struct atNBPtuple *tp =
-                       (struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
+               (const struct atNBPtuple *)((u_char *)np + nbpHeaderSize);
        int i;
        const u_char *ep;
 
+       if (length < nbpHeaderSize) {
+               (void)printf(" truncated-nbp %u", length);
+               return;
+       }
+
        length -= nbpHeaderSize;
        if (length < 8) {
                /* must be room for at least one tuple */
-               (void)printf(" truncated-nbp %d", length + nbpHeaderSize);
+               (void)printf(" truncated-nbp %u", length + nbpHeaderSize);
                return;
        }
        /* ep points to end of available data */
@@ -385,7 +434,7 @@ nbp_print(register const struct atNBP *np, u_int length, register u_short snet,
                break;
 
        default:
-               (void)printf(" nbp-0x%x  %d (%d)", np->control, np->id,
+               (void)printf(" nbp-0x%x  %d (%u)", np->control, np->id,
                                length);
                break;
        }
@@ -409,7 +458,7 @@ print_cstring(register const char *cp, register const u_char *ep)
                return (0);
        }
        while ((int)--length >= 0) {
-               if (cp >= (char *)ep) {
+               if (cp >= (const char *)ep) {
                        fputs(tstr, stdout);
                        return (0);
                }
@@ -486,7 +535,7 @@ ataddr_string(u_short atnet, u_char athost)
 {
        register struct hnamemem *tp, *tp2;
        register int i = (atnet << 8) | athost;
-       char nambuf[256];
+       char nambuf[256+1];
        static int first = 1;
        FILE *fp;
 
@@ -497,28 +546,26 @@ ataddr_string(u_short atnet, u_char athost)
        if (first && (first = 0, !nflag)
            && (fp = fopen("/etc/atalk.names", "r"))) {
                char line[256];
-               int i1, i2, i3;
+               int i1, i2;
 
                while (fgets(line, sizeof(line), fp)) {
                        if (line[0] == '\n' || line[0] == 0 || line[0] == '#')
                                continue;
-                       if (sscanf(line, "%d.%d.%d %256s", &i1, &i2, &i3,
-                                    nambuf) == 4)
+                       if (sscanf(line, "%d.%d %256s", &i1, &i2, nambuf) == 3)
                                /* got a hostname. */
-                               i3 |= ((i1 << 8) | i2) << 8;
-                       else if (sscanf(line, "%d.%d %256s", &i1, &i2,
-                                       nambuf) == 3)
+                               i2 |= (i1 << 8);
+                       else if (sscanf(line, "%d %256s", &i1, nambuf) == 2)
                                /* got a net name */
-                               i3 = (((i1 << 8) | i2) << 8) | 255;
+                               i2 = (i1 << 8) | 255;
                        else
                                continue;
 
-                       for (tp = &hnametable[i3 & (HASHNAMESIZE-1)];
+                       for (tp = &hnametable[i2 & (HASHNAMESIZE-1)];
                             tp->nxt; tp = tp->nxt)
                                ;
-                       tp->addr = i3;
+                       tp->addr = i2;
                        tp->nxt = newhnamemem();
-                       tp->name = savestr(nambuf);
+                       tp->name = strdup(nambuf);
                }
                fclose(fp);
        }
@@ -535,24 +582,22 @@ ataddr_string(u_short atnet, u_char athost)
                        tp->nxt = newhnamemem();
                        (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
                            tp2->name, athost);
-                       tp->name = savestr(nambuf);
+                       tp->name = strdup(nambuf);
                        return (tp->name);
                }
 
        tp->addr = (atnet << 8) | athost;
        tp->nxt = newhnamemem();
        if (athost != 255)
-               (void)snprintf(nambuf, sizeof(nambuf), "%d.%d.%d",
-                   atnet >> 8, atnet & 0xff, athost);
+               (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet, athost);
        else
-               (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet >> 8,
-                   atnet & 0xff);
-       tp->name = savestr(nambuf);
+               (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
+       tp->name = strdup(nambuf);
 
        return (tp->name);
 }
 
-static struct tok skt2str[] = {
+static const struct tok skt2str[] = {
        { rtmpSkt,      "rtmp" },       /* routing table maintenance */
        { nbpSkt,       "nis" },        /* name info socket */
        { echoSkt,      "echo" },       /* AppleTalk echo protocol */