From: Guy Harris Date: Wed, 13 Dec 2017 17:32:44 +0000 (-0800) Subject: Use nd_ types for DNS. X-Git-Tag: tcpdump-4.99-bp~1642 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/37185d198ea75a8ee6e81890f6f52a2042cf2b03 Use nd_ types for DNS. Make some types unsigned, and fix some loops to no longer depend on a zero count rolling over to -1 when decremented. --- diff --git a/nameser.h b/nameser.h index dd593c24..6610c6c7 100644 --- a/nameser.h +++ b/nameser.h @@ -210,28 +210,26 @@ * 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. diff --git a/print-domain.c b/print-domain.c index 5281df13..303dc28c 100644 --- a/print-domain.c +++ b/print-domain.c @@ -27,8 +27,6 @@ #include -#include "nameser.h" - #include #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; } } diff --git a/print-lwres.c b/print-lwres.c index 51f3a7a6..ff6d908c 100644 --- a/print-lwres.c +++ b/print-lwres.c @@ -35,8 +35,6 @@ #include -#include "nameser.h" - #include #include @@ -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;