]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-atalk.c
Use more the EXTRACT_U_1() macro (40/n)
[tcpdump] / print-atalk.c
index c5eb78006d24572a34c93255b21c3d69642599d8..ce61a44c9de18802a83d62b5b40c39b0ed274e36 100644 (file)
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Format and print AppleTalk packets.
  */
 
+/* \summary: AppleTalk printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#include "interface.h"
+#include "netdissect.h"
 #include "addrtoname.h"
 #include "ethertype.h"
-#include "extract.h"                   /* must come after interface.h */
+#include "extract.h"
 #include "appletalk.h"
 
 static const char tstr[] = "[|atalk]";
@@ -77,7 +77,14 @@ u_int
 ltalk_if_print(netdissect_options *ndo,
                const struct pcap_pkthdr *h, const u_char *p)
 {
-       return (llap_print(ndo, p, h->caplen));
+       u_int hdrlen;
+
+       hdrlen = llap_print(ndo, p, h->len);
+       if (hdrlen == 0) {
+               /* Cut short by the snapshot length. */
+               return (h->caplen);
+       }
+       return (hdrlen);
 }
 
 /*
@@ -97,6 +104,10 @@ llap_print(netdissect_options *ndo,
                ND_PRINT((ndo, " [|llap %u]", length));
                return (length);
        }
+       if (!ND_TTEST2(*bp, sizeof(*lp))) {
+               ND_PRINT((ndo, " [|llap]"));
+               return (0);     /* cut short by the snapshot length */
+       }
        lp = (const struct LAP *)bp;
        bp += sizeof(*lp);
        length -= sizeof(*lp);
@@ -108,6 +119,10 @@ llap_print(netdissect_options *ndo,
                        ND_PRINT((ndo, " [|sddp %u]", length));
                        return (length);
                }
+               if (!ND_TTEST2(*bp, ddpSSize)) {
+                       ND_PRINT((ndo, " [|sddp]"));
+                       return (0);     /* cut short by the snapshot length */
+               }
                sdp = (const struct atShortDDP *)bp;
                ND_PRINT((ndo, "%s.%s",
                    ataddr_string(ndo, 0, lp->src), ddpskt_string(ndo, sdp->srcSkt)));
@@ -124,12 +139,16 @@ llap_print(netdissect_options *ndo,
                        ND_PRINT((ndo, " [|ddp %u]", length));
                        return (length);
                }
+               if (!ND_TTEST2(*bp, ddpSize)) {
+                       ND_PRINT((ndo, " [|ddp]"));
+                       return (0);     /* cut short by the snapshot length */
+               }
                dp = (const struct atDDP *)bp;
-               snet = EXTRACT_16BITS(&dp->srcNet);
+               snet = EXTRACT_BE_U_2(&dp->srcNet);
                ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
                    ddpskt_string(ndo, dp->srcSkt)));
                ND_PRINT((ndo, " > %s.%s:",
-                   ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
+                   ataddr_string(ndo, EXTRACT_BE_U_2(&dp->dstNet), dp->dstNode),
                    ddpskt_string(ndo, dp->dstSkt)));
                bp += ddpSize;
                length -= ddpSize;
@@ -170,12 +189,16 @@ atalk_print(netdissect_options *ndo,
                ND_PRINT((ndo, " [|ddp %u]", length));
                return;
        }
+       if (!ND_TTEST2(*bp, ddpSize)) {
+               ND_PRINT((ndo, " [|ddp]"));
+               return;
+       }
        dp = (const struct atDDP *)bp;
-       snet = EXTRACT_16BITS(&dp->srcNet);
+       snet = EXTRACT_BE_U_2(&dp->srcNet);
        ND_PRINT((ndo, "%s.%s", ataddr_string(ndo, snet, dp->srcNode),
               ddpskt_string(ndo, dp->srcSkt)));
        ND_PRINT((ndo, " > %s.%s: ",
-              ataddr_string(ndo, EXTRACT_16BITS(&dp->dstNet), dp->dstNode),
+              ataddr_string(ndo, EXTRACT_BE_U_2(&dp->dstNet), dp->dstNode),
               ddpskt_string(ndo, dp->dstSkt)));
        bp += ddpSize;
        length -= ddpSize;
@@ -193,10 +216,19 @@ aarp_print(netdissect_options *ndo,
 
        ND_PRINT((ndo, "aarp "));
        ap = (const struct aarp *)bp;
-       if (EXTRACT_16BITS(&ap->htype) == 1 &&
-           EXTRACT_16BITS(&ap->ptype) == ETHERTYPE_ATALK &&
+       if (!ND_TTEST(*ap)) {
+               /* Just bail if we don't have the whole chunk. */
+               ND_PRINT((ndo, " [|aarp]"));
+               return;
+       }
+       if (length < sizeof(*ap)) {
+               ND_PRINT((ndo, " [|aarp %u]", length));
+               return;
+       }
+       if (EXTRACT_BE_U_2(&ap->htype) == 1 &&
+           EXTRACT_BE_U_2(&ap->ptype) == ETHERTYPE_ATALK &&
            ap->halen == 6 && ap->palen == 4 )
-               switch (EXTRACT_16BITS(&ap->op)) {
+               switch (EXTRACT_BE_U_2(&ap->op)) {
 
                case 1:                         /* request */
                        ND_PRINT((ndo, "who-has %s tell %s", AT(pdaddr), AT(psaddr)));
@@ -211,8 +243,8 @@ aarp_print(netdissect_options *ndo,
                        return;
                }
        ND_PRINT((ndo, "len %u op %u htype %u ptype %#x halen %u palen %u",
-           length, EXTRACT_16BITS(&ap->op), EXTRACT_16BITS(&ap->htype),
-           EXTRACT_16BITS(&ap->ptype), ap->halen, ap->palen));
+           length, EXTRACT_BE_U_2(&ap->op), EXTRACT_BE_U_2(&ap->htype),
+           EXTRACT_BE_U_2(&ap->ptype), ap->halen, ap->palen));
 }
 
 /*
@@ -248,7 +280,6 @@ static void
 atp_print(netdissect_options *ndo,
           register const struct atATP *ap, u_int length)
 {
-       char c;
        uint32_t data;
 
        if ((const u_char *)(ap + 1) > ndo->ndo_snapend) {
@@ -266,7 +297,7 @@ atp_print(netdissect_options *ndo,
        case atpReqCode:
                ND_PRINT((ndo, " atp-req%s %d",
                             ap->control & atpXO? " " : "*",
-                            EXTRACT_16BITS(&ap->transID)));
+                            EXTRACT_BE_U_2(&ap->transID)));
 
                atp_bitmap_print(ndo, ap->bitmap);
 
@@ -289,7 +320,7 @@ atp_print(netdissect_options *ndo,
        case atpRspCode:
                ND_PRINT((ndo, " atp-resp%s%d:%d (%u)",
                             ap->control & atpEOM? "*" : " ",
-                            EXTRACT_16BITS(&ap->transID), ap->bitmap, length));
+                            EXTRACT_BE_U_2(&ap->transID), ap->bitmap, length));
                switch (ap->control & (atpXO|atpSTS)) {
                case atpXO:
                        ND_PRINT((ndo, " [XO]"));
@@ -304,7 +335,7 @@ atp_print(netdissect_options *ndo,
                break;
 
        case atpRelCode:
-               ND_PRINT((ndo, " atp-rel  %d", EXTRACT_16BITS(&ap->transID)));
+               ND_PRINT((ndo, " atp-rel  %d", EXTRACT_BE_U_2(&ap->transID)));
 
                atp_bitmap_print(ndo, ap->bitmap);
 
@@ -314,7 +345,7 @@ atp_print(netdissect_options *ndo,
 
                /* there shouldn't be any control flags */
                if (ap->control & (atpXO|atpEOM|atpSTS)) {
-                       c = '[';
+                       char c = '[';
                        if (ap->control & atpXO) {
                                ND_PRINT((ndo, "%cXO", c));
                                c = ',';
@@ -325,7 +356,6 @@ atp_print(netdissect_options *ndo,
                        }
                        if (ap->control & atpSTS) {
                                ND_PRINT((ndo, "%cSTS", c));
-                               c = ',';
                        }
                        ND_PRINT((ndo, "]"));
                }
@@ -333,10 +363,10 @@ atp_print(netdissect_options *ndo,
 
        default:
                ND_PRINT((ndo, " atp-0x%x  %d (%u)", ap->control,
-                            EXTRACT_16BITS(&ap->transID), length));
+                            EXTRACT_BE_U_2(&ap->transID), length));
                break;
        }
-       data = EXTRACT_32BITS(&ap->userData);
+       data = EXTRACT_BE_U_4(&ap->userData);
        if (data != 0)
                ND_PRINT((ndo, " 0x%x", data));
 }
@@ -345,7 +375,6 @@ static void
 atp_bitmap_print(netdissect_options *ndo,
                  register u_char bm)
 {
-       register char c;
        register int i;
 
        /*
@@ -354,7 +383,7 @@ atp_bitmap_print(netdissect_options *ndo,
         * (gcc is smart enough to eliminate it, at least on the Sparc).
         */
        if ((bm + 1) & (bm & 0xff)) {
-               c = '<';
+               register char c = '<';
                for (i = 0; bm; ++i) {
                        if (bm & 1) {
                                ND_PRINT((ndo, "%c%d", c, i));
@@ -419,11 +448,11 @@ nbp_print(netdissect_options *ndo,
                        ND_PRINT((ndo, " [ntup=%d]", np->control & 0xf));
                if (tp->enumerator)
                        ND_PRINT((ndo, " [enum=%d]", tp->enumerator));
-               if (EXTRACT_16BITS(&tp->net) != snet ||
+               if (EXTRACT_BE_U_2(&tp->net) != snet ||
                    tp->node != snode || tp->skt != skt)
                        ND_PRINT((ndo, " [addr=%s.%d]",
-                           ataddr_string(ndo, EXTRACT_16BITS(&tp->net),
-                           tp->node), tp->skt));
+                           ataddr_string(ndo, EXTRACT_BE_U_2(&tp->net),
+                                         tp->node), tp->skt));
                break;
 
        case nbpLkUpReply:
@@ -451,19 +480,22 @@ print_cstring(netdissect_options *ndo,
                ND_PRINT((ndo, "%s", tstr));
                return (0);
        }
-       length = *cp++;
+       length = EXTRACT_U_1(cp);
+       cp++;
 
        /* Spec says string can be at most 32 bytes long */
        if (length > 32) {
                ND_PRINT((ndo, "[len=%u]", length));
                return (0);
        }
-       while ((int)--length >= 0) {
+       while (length != 0) {
                if (cp >= (const char *)ep) {
                        ND_PRINT((ndo, "%s", tstr));
                        return (0);
                }
-               ND_PRINT((ndo, "%c", *cp++));
+               ND_PRINT((ndo, "%c", EXTRACT_U_1(cp)));
+               cp++;
+               length--;
        }
        return (cp);
 }
@@ -490,9 +522,9 @@ nbp_tuple_print(netdissect_options *ndo,
                ND_PRINT((ndo, " %d", tp->skt));
 
        /* if the address doesn't match the src address, it's an anomaly */
-       if (EXTRACT_16BITS(&tp->net) != snet || tp->node != snode)
+       if (EXTRACT_BE_U_2(&tp->net) != snet || tp->node != snode)
                ND_PRINT((ndo, " [addr=%s]",
-                   ataddr_string(ndo, EXTRACT_16BITS(&tp->net), tp->node)));
+                   ataddr_string(ndo, EXTRACT_BE_U_2(&tp->net), tp->node)));
 
        return (tpn);
 }
@@ -566,8 +598,11 @@ ataddr_string(netdissect_options *ndo,
                             tp->nxt; tp = tp->nxt)
                                ;
                        tp->addr = i2;
-                       tp->nxt = newhnamemem();
+                       tp->nxt = newhnamemem(ndo);
                        tp->name = strdup(nambuf);
+                       if (tp->name == NULL)
+                               (*ndo->ndo_error)(ndo,
+                                                 "ataddr_string: strdup(nambuf)");
                }
                fclose(fp);
        }
@@ -581,20 +616,25 @@ ataddr_string(netdissect_options *ndo,
        for (tp2 = &hnametable[i & (HASHNAMESIZE-1)]; tp2->nxt; tp2 = tp2->nxt)
                if (tp2->addr == i) {
                        tp->addr = (atnet << 8) | athost;
-                       tp->nxt = newhnamemem();
+                       tp->nxt = newhnamemem(ndo);
                        (void)snprintf(nambuf, sizeof(nambuf), "%s.%d",
                            tp2->name, athost);
                        tp->name = strdup(nambuf);
+                       if (tp->name == NULL)
+                               (*ndo->ndo_error)(ndo,
+                                                 "ataddr_string: strdup(nambuf)");
                        return (tp->name);
                }
 
        tp->addr = (atnet << 8) | athost;
-       tp->nxt = newhnamemem();
+       tp->nxt = newhnamemem(ndo);
        if (athost != 255)
                (void)snprintf(nambuf, sizeof(nambuf), "%d.%d", atnet, athost);
        else
                (void)snprintf(nambuf, sizeof(nambuf), "%d", atnet);
        tp->name = strdup(nambuf);
+       if (tp->name == NULL)
+               (*ndo->ndo_error)(ndo, "ataddr_string: strdup(nambuf)");
 
        return (tp->name);
 }