]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Use nd_ types for DNS.
authorGuy Harris <[email protected]>
Wed, 13 Dec 2017 17:32:44 +0000 (09:32 -0800)
committerGuy Harris <[email protected]>
Wed, 13 Dec 2017 17:32:44 +0000 (09:32 -0800)
Make some types unsigned, and fix some loops to no longer depend on a
zero count rolling over to -1 when decremented.

nameser.h
print-domain.c
print-lwres.c

index dd593c24471de089329dd3d60faa934b742a17b4..6610c6c7e3898a05bdc04898789962bd88424c1a 100644 (file)
--- a/nameser.h
+++ b/nameser.h
  * Structure for query header.
  */
 typedef struct {
-       uint16_t id;            /* query identification number */
-       uint8_t  flags1;        /* first byte of flags */
-       uint8_t  flags2;        /* second byte of flags */
-       uint16_t qdcount;       /* number of question entries */
-       uint16_t ancount;       /* number of answer entries */
-       uint16_t nscount;       /* number of authority entries */
-       uint16_t arcount;       /* number of resource entries */
+       nd_uint16_t id;         /* query identification number */
+       nd_uint16_t flags;      /* QR, Opcode, AA, TC, RD, RA, RCODE */
+       nd_uint16_t qdcount;    /* number of question entries */
+       nd_uint16_t ancount;    /* number of answer entries */
+       nd_uint16_t nscount;    /* number of authority entries */
+       nd_uint16_t arcount;    /* number of resource entries */
 } HEADER;
 
 /*
  * Macros for subfields of flag fields.
  */
-#define DNS_QR(np)     ((np)->flags1 & 0x80)           /* response flag */
-#define DNS_OPCODE(np) ((((np)->flags1) >> 3) & 0xF)   /* purpose of message */
-#define DNS_AA(np)     ((np)->flags1 & 0x04)           /* authoritative answer */
-#define DNS_TC(np)     ((np)->flags1 & 0x02)           /* truncated message */
-#define DNS_RD(np)     ((np)->flags1 & 0x01)           /* recursion desired */
-
-#define DNS_RA(np)     ((np)->flags2 & 0x80)   /* recursion available */
-#define DNS_AD(np)     ((np)->flags2 & 0x20)   /* authentic data from named */
-#define DNS_CD(np)     ((np)->flags2 & 0x10)   /* checking disabled by resolver */
-#define DNS_RCODE(np)  ((np)->flags2 & 0xF)    /* response code */
+#define DNS_QR(flags)          ((flags) & 0x8000)      /* response flag */
+#define DNS_OPCODE(flags)      (((flags) >> 11) & 0xF) /* purpose of message */
+#define DNS_AA(flags)          (flags & 0x0400)        /* authoritative answer */
+#define DNS_TC(flags)          (flags & 0x0200)        /* truncated message */
+#define DNS_RD(flags)          (flags & 0x0100)        /* recursion desired */
+#define DNS_RA(flags)          (flags & 0x0080)        /* recursion available */
+#define DNS_AD(flags)          (flags & 0x0020)        /* authentic data from named */
+#define DNS_CD(flags)          (flags & 0x0010)        /* checking disabled by resolver */
+#define DNS_RCODE(flags)       (flags & 0x000F)        /* response code */
 
 /*
  * Defines for handling compressed domain names, EDNS0 labels, etc.
index 5281df13fd3f80ee34dd143b6428a61d9b9073f2..303dc28c6f207ebe19fa2029a86faaf46ba9f998 100644 (file)
@@ -27,8 +27,6 @@
 
 #include <netdissect-stdinc.h>
 
-#include "nameser.h"
-
 #include <string.h>
 
 #include "netdissect.h"
@@ -36,6 +34,8 @@
 #include "addrtostr.h"
 #include "extract.h"
 
+#include "nameser.h"
+
 static const char *ns_ops[] = {
        "", " inv_q", " stat", " op3", " notify", " update", " op6", " op7",
        " op8", " updateA", " updateD", " updateDA",
@@ -588,35 +588,38 @@ domain_print(netdissect_options *ndo,
          register const u_char *bp, u_int length, int is_mdns)
 {
        register const HEADER *np;
-       register int qdcount, ancount, nscount, arcount;
+       u_int16_t flags;
+       u_int qdcount, ancount, nscount, arcount;
+       u_int i;
        register const u_char *cp;
        uint16_t b2;
 
        np = (const HEADER *)bp;
        ND_TCHECK(*np);
+       flags = EXTRACT_BE_U_2(np->flags);
        /* get the byte-order right */
-       qdcount = EXTRACT_BE_U_2(&np->qdcount);
-       ancount = EXTRACT_BE_U_2(&np->ancount);
-       nscount = EXTRACT_BE_U_2(&np->nscount);
-       arcount = EXTRACT_BE_U_2(&np->arcount);
+       qdcount = EXTRACT_BE_U_2(np->qdcount);
+       ancount = EXTRACT_BE_U_2(np->ancount);
+       nscount = EXTRACT_BE_U_2(np->nscount);
+       arcount = EXTRACT_BE_U_2(np->arcount);
 
-       if (DNS_QR(np)) {
+       if (DNS_QR(flags)) {
                /* this is a response */
                ND_PRINT((ndo, "%d%s%s%s%s%s%s",
-                       EXTRACT_BE_U_2(&np->id),
-                       ns_ops[DNS_OPCODE(np)],
-                       ns_resp[DNS_RCODE(np)],
-                       DNS_AA(np)? "*" : "",
-                       DNS_RA(np)? "" : "-",
-                       DNS_TC(np)? "|" : "",
-                       DNS_AD(np)? "$" : ""));
+                       EXTRACT_BE_U_2(np->id),
+                       ns_ops[DNS_OPCODE(flags)],
+                       ns_resp[DNS_RCODE(flags)],
+                       DNS_AA(flags)? "*" : "",
+                       DNS_RA(flags)? "" : "-",
+                       DNS_TC(flags)? "|" : "",
+                       DNS_AD(flags)? "$" : ""));
 
                if (qdcount != 1)
-                       ND_PRINT((ndo, " [%dq]", qdcount));
+                       ND_PRINT((ndo, " [%uq]", qdcount));
                /* Print QUESTION section on -vv */
                cp = (const u_char *)(np + 1);
-               while (qdcount--) {
-                       if (qdcount < EXTRACT_BE_U_2(&np->qdcount) - 1)
+               for (i = 0; i < qdcount; i++) {
+                       if (i != 0)
                                ND_PRINT((ndo, ","));
                        if (ndo->ndo_vflag > 1) {
                                ND_PRINT((ndo, " q:"));
@@ -628,126 +631,140 @@ domain_print(netdissect_options *ndo,
                                cp += 4;        /* skip QTYPE and QCLASS */
                        }
                }
-               ND_PRINT((ndo, " %d/%d/%d", ancount, nscount, arcount));
-               if (ancount--) {
+               ND_PRINT((ndo, " %u/%u/%u", ancount, nscount, arcount));
+               if (ancount) {
                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                goto trunc;
-                       while (cp < ndo->ndo_snapend && ancount--) {
+                       ancount--;
+                       while (cp < ndo->ndo_snapend && ancount) {
                                ND_PRINT((ndo, ","));
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
+                               ancount--;
                        }
                }
-               if (ancount > 0)
+               if (ancount)
                        goto trunc;
                /* Print NS and AR sections on -vv */
                if (ndo->ndo_vflag > 1) {
-                       if (cp < ndo->ndo_snapend && nscount--) {
+                       if (cp < ndo->ndo_snapend && nscount) {
                                ND_PRINT((ndo, " ns:"));
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
-                               while (cp < ndo->ndo_snapend && nscount--) {
+                               nscount--;
+                               while (cp < ndo->ndo_snapend && nscount) {
                                        ND_PRINT((ndo, ","));
                                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                                goto trunc;
+                                       nscount--;
                                }
                        }
-                       if (nscount > 0)
+                       if (nscount)
                                goto trunc;
-                       if (cp < ndo->ndo_snapend && arcount--) {
+                       if (cp < ndo->ndo_snapend && arcount) {
                                ND_PRINT((ndo, " ar:"));
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
-                               while (cp < ndo->ndo_snapend && arcount--) {
+                               arcount--;
+                               while (cp < ndo->ndo_snapend && arcount) {
                                        ND_PRINT((ndo, ","));
                                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                                goto trunc;
+                                       arcount--;
                                }
                        }
-                       if (arcount > 0)
+                       if (arcount)
                                goto trunc;
                }
        }
        else {
                /* this is a request */
-               ND_PRINT((ndo, "%d%s%s%s", EXTRACT_BE_U_2(&np->id), ns_ops[DNS_OPCODE(np)],
-                         DNS_RD(np) ? "+" : "",
-                         DNS_CD(np) ? "%" : ""));
+               ND_PRINT((ndo, "%d%s%s%s", EXTRACT_BE_U_2(np->id), ns_ops[DNS_OPCODE(flags)],
+                         DNS_RD(flags) ? "+" : "",
+                         DNS_CD(flags) ? "%" : ""));
 
                /* any weirdness? */
                b2 = EXTRACT_BE_U_2(((const u_short *)np) + 1);
                if (b2 & 0x6cf)
                        ND_PRINT((ndo, " [b2&3=0x%x]", b2));
 
-               if (DNS_OPCODE(np) == IQUERY) {
+               if (DNS_OPCODE(flags) == IQUERY) {
                        if (qdcount)
-                               ND_PRINT((ndo, " [%dq]", qdcount));
+                               ND_PRINT((ndo, " [%uq]", qdcount));
                        if (ancount != 1)
-                               ND_PRINT((ndo, " [%da]", ancount));
+                               ND_PRINT((ndo, " [%ua]", ancount));
                }
                else {
                        if (ancount)
-                               ND_PRINT((ndo, " [%da]", ancount));
+                               ND_PRINT((ndo, " [%ua]", ancount));
                        if (qdcount != 1)
-                               ND_PRINT((ndo, " [%dq]", qdcount));
+                               ND_PRINT((ndo, " [%uq]", qdcount));
                }
                if (nscount)
-                       ND_PRINT((ndo, " [%dn]", nscount));
+                       ND_PRINT((ndo, " [%un]", nscount));
                if (arcount)
-                       ND_PRINT((ndo, " [%dau]", arcount));
+                       ND_PRINT((ndo, " [%uau]", arcount));
 
                cp = (const u_char *)(np + 1);
-               if (qdcount--) {
+               if (qdcount) {
                        cp = ns_qprint(ndo, cp, (const u_char *)np, is_mdns);
                        if (!cp)
                                goto trunc;
-                       while (cp < ndo->ndo_snapend && qdcount--) {
+                       qdcount--;
+                       while (cp < ndo->ndo_snapend && qdcount) {
                                cp = ns_qprint(ndo, (const u_char *)cp,
                                               (const u_char *)np,
                                               is_mdns);
                                if (!cp)
                                        goto trunc;
+                               qdcount--;
                        }
                }
-               if (qdcount > 0)
+               if (qdcount)
                        goto trunc;
 
                /* Print remaining sections on -vv */
                if (ndo->ndo_vflag > 1) {
-                       if (ancount--) {
+                       if (ancount) {
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
-                               while (cp < ndo->ndo_snapend && ancount--) {
+                               ancount--;
+                               while (cp < ndo->ndo_snapend && ancount) {
                                        ND_PRINT((ndo, ","));
                                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                                goto trunc;
+                                       ancount--;
                                }
                        }
-                       if (ancount > 0)
+                       if (ancount)
                                goto trunc;
-                       if (cp < ndo->ndo_snapend && nscount--) {
+                       if (cp < ndo->ndo_snapend && nscount) {
                                ND_PRINT((ndo, " ns:"));
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
-                               while (nscount-- && cp < ndo->ndo_snapend) {
+                               nscount--;
+                               while (cp < ndo->ndo_snapend && nscount) {
                                        ND_PRINT((ndo, ","));
                                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                                goto trunc;
+                                       nscount--;
                                }
                        }
                        if (nscount > 0)
                                goto trunc;
-                       if (cp < ndo->ndo_snapend && arcount--) {
+                       if (cp < ndo->ndo_snapend && arcount) {
                                ND_PRINT((ndo, " ar:"));
                                if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                        goto trunc;
-                               while (cp < ndo->ndo_snapend && arcount--) {
+                               arcount--;
+                               while (cp < ndo->ndo_snapend && arcount) {
                                        ND_PRINT((ndo, ","));
                                        if ((cp = ns_rprint(ndo, cp, bp, is_mdns)) == NULL)
                                                goto trunc;
+                                       arcount--;
                                }
                        }
-                       if (arcount > 0)
+                       if (arcount)
                                goto trunc;
                }
        }
index 51f3a7a689db57676c962dbc4fb0098e8e547ab8..ff6d908c8a2077541a3a0e861b85c282c80145dc 100644 (file)
@@ -35,8 +35,6 @@
 
 #include <netdissect-stdinc.h>
 
-#include "nameser.h"
-
 #include <stdio.h>
 #include <string.h>
 
@@ -44,6 +42,8 @@
 #include "addrtoname.h"
 #include "extract.h"
 
+#include "nameser.h"
+
 /* BIND9 lib/lwres/include/lwres */
 typedef uint32_t lwres_uint32_t;
 typedef uint16_t lwres_uint16_t;