]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-lwres.c
Update the GitHub issue template
[tcpdump] / print-lwres.c
index 474633c775985134d95aa51d9c259e812bc1d81f..29b49c6e7be843c24866b1a5ae6ccb9a1859ec21 100644 (file)
 
 /* \summary: BIND9 Lightweight Resolver protocol printer */
 
-#ifdef HAVE_CONFIG_H
 #include <config.h>
-#endif
 
 #include "netdissect-stdinc.h"
 
-#include <stdio.h>
-#include <string.h>
-
+#define ND_LONGJMP_FROM_TCHECK
 #include "netdissect.h"
 #include "addrtoname.h"
 #include "extract.h"
@@ -96,7 +92,7 @@ typedef struct lwres_addr lwres_addr_t;
 struct lwres_addr {
        nd_uint32_t                     family;
        nd_uint16_t                     length;
-       /* address folows */
+       /* address follows */
 };
 #define LWRES_ADDR_LEN                 6
 
@@ -184,58 +180,38 @@ static const struct tok opcode[] = {
        { LWRES_OPCODE_GETADDRSBYNAME,  "getaddrsbyname", },
        { LWRES_OPCODE_GETNAMEBYADDR,   "getnamebyaddr", },
        { LWRES_OPCODE_GETRDATABYNAME,  "getrdatabyname", },
-       { 0,                            NULL, },
+       { 0,                            NULL, },
 };
 
 /* print-domain.c */
 extern const struct tok ns_type2str[];
 extern const struct tok ns_class2str[];
 
-static int
+static unsigned
 lwres_printname(netdissect_options *ndo,
-                size_t l, const u_char *p0)
+                u_int l, const u_char *p0)
 {
-       const u_char *p;
-       size_t i;
-
-       p = p0;
-       /* + 1 for terminating \0 */
-       if (p + l + 1 > ndo->ndo_snapend)
-               goto trunc;
-
        ND_PRINT(" ");
-       for (i = 0; i < l; i++) {
-               fn_print_char(ndo, GET_U_1(p));
-               p++;
-       }
-       p++;    /* skip terminating \0 */
-
-       return ND_BYTES_BETWEEN(p, p0);
-
-  trunc:
-       return -1;
+       nd_printjn(ndo, p0, l);
+       p0 += l;
+       if (GET_U_1(p0))
+               ND_PRINT(" (not NUL-terminated!)");
+       return l + 1;
 }
 
-static int
+static unsigned
 lwres_printnamelen(netdissect_options *ndo,
                    const u_char *p)
 {
        uint16_t l;
        int advance;
 
-       if (p + 2 > ndo->ndo_snapend)
-               goto trunc;
        l = GET_BE_U_2(p);
        advance = lwres_printname(ndo, l, p + 2);
-       if (advance < 0)
-               goto trunc;
        return 2 + advance;
-
-  trunc:
-       return -1;
 }
 
-static int
+static unsigned
 lwres_printbinlen(netdissect_options *ndo,
                   const u_char *p0)
 {
@@ -244,20 +220,13 @@ lwres_printbinlen(netdissect_options *ndo,
        int i;
 
        p = p0;
-       if (p + 2 > ndo->ndo_snapend)
-               goto trunc;
        l = GET_BE_U_2(p);
-       if (p + 2 + l > ndo->ndo_snapend)
-               goto trunc;
        p += 2;
        for (i = 0; i < l; i++) {
                ND_PRINT("%02x", GET_U_1(p));
                p++;
        }
-       return ND_BYTES_BETWEEN(p, p0);
-
-  trunc:
-       return -1;
+       return 2 + l;
 }
 
 static int
@@ -296,10 +265,7 @@ lwres_printaddr(netdissect_options *ndo,
                }
        }
 
-       return ND_BYTES_BETWEEN(p, p0);
-
-  trunc:
-       return -1;
+       return ND_BYTES_BETWEEN(p0, p);
 }
 
 void
@@ -323,7 +289,9 @@ lwres_print(netdissect_options *ndo,
        if (ndo->ndo_vflag || v != LWRES_LWPACKETVERSION_0)
                ND_PRINT(" v%u", v);
        if (v != LWRES_LWPACKETVERSION_0) {
-               s = bp + GET_BE_U_4(np->length);
+               uint32_t pkt_len = GET_BE_U_4(np->length);
+               ND_TCHECK_LEN(bp, pkt_len);
+               s = bp + pkt_len;
                goto tail;
        }
 
@@ -399,8 +367,6 @@ lwres_print(netdissect_options *ndo,
                        s = p + LWRES_GABNREQUEST_LEN;
                        l = GET_BE_U_2(gabn->namelen);
                        advance = lwres_printname(ndo, l, s);
-                       if (advance < 0)
-                               goto trunc;
                        s += advance;
                        break;
                case LWRES_OPCODE_GETNAMEBYADDR:
@@ -416,7 +382,7 @@ lwres_print(netdissect_options *ndo,
                        s = p + LWRES_GNBAREQUEST_LEN;
                        advance = lwres_printaddr(ndo, s);
                        if (advance < 0)
-                               goto trunc;
+                               goto invalid;
                        s += advance;
                        break;
                case LWRES_OPCODE_GETRDATABYNAME:
@@ -440,8 +406,6 @@ lwres_print(netdissect_options *ndo,
                        s = p + LWRES_GRBNREQUEST_LEN;
                        l = GET_BE_U_2(grbn->namelen);
                        advance = lwres_printname(ndo, l, s);
-                       if (advance < 0)
-                               goto trunc;
                        s += advance;
                        break;
                default:
@@ -484,16 +448,12 @@ lwres_print(netdissect_options *ndo,
                        s = p + LWRES_GABNRESPONSE_LEN;
                        l = GET_BE_U_2(gabn->realnamelen);
                        advance = lwres_printname(ndo, l, s);
-                       if (advance < 0)
-                               goto trunc;
                        s += advance;
 
                        /* aliases */
                        na = GET_BE_U_2(gabn->naliases);
                        for (i = 0; i < na; i++) {
                                advance = lwres_printnamelen(ndo, s);
-                               if (advance < 0)
-                                       goto trunc;
                                s += advance;
                        }
 
@@ -502,7 +462,7 @@ lwres_print(netdissect_options *ndo,
                        for (i = 0; i < na; i++) {
                                advance = lwres_printaddr(ndo, s);
                                if (advance < 0)
-                                       goto trunc;
+                                       goto invalid;
                                s += advance;
                        }
                        break;
@@ -521,16 +481,12 @@ lwres_print(netdissect_options *ndo,
                        s = p + LWRES_GNBARESPONSE_LEN;
                        l = GET_BE_U_2(gnba->realnamelen);
                        advance = lwres_printname(ndo, l, s);
-                       if (advance < 0)
-                               goto trunc;
                        s += advance;
 
                        /* aliases */
                        na = GET_BE_U_2(gnba->naliases);
                        for (i = 0; i < na; i++) {
                                advance = lwres_printnamelen(ndo, s);
-                               if (advance < 0)
-                                       goto trunc;
                                s += advance;
                        }
                        break;
@@ -559,8 +515,6 @@ lwres_print(netdissect_options *ndo,
 
                        s = p + LWRES_GRBNRESPONSE_LEN;
                        advance = lwres_printnamelen(ndo, s);
-                       if (advance < 0)
-                               goto trunc;
                        s += advance;
 
                        /* rdatas */
@@ -568,8 +522,6 @@ lwres_print(netdissect_options *ndo,
                        for (i = 0; i < na; i++) {
                                /* XXX should decode resource data */
                                advance = lwres_printbinlen(ndo, s);
-                               if (advance < 0)
-                                       goto trunc;
                                s += advance;
                        }
 
@@ -578,8 +530,6 @@ lwres_print(netdissect_options *ndo,
                        for (i = 0; i < na; i++) {
                                /* XXX how should we print it? */
                                advance = lwres_printbinlen(ndo, s);
-                               if (advance < 0)
-                                       goto trunc;
                                s += advance;
                        }
                        break;
@@ -596,10 +546,10 @@ lwres_print(netdissect_options *ndo,
                ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np->length),
                          length);
        }
-       if (!unsupported && s < bp + GET_BE_U_4(np->length))
+       if (!unsupported && ND_BYTES_BETWEEN(bp, s) < GET_BE_U_4(np->length))
                ND_PRINT("[extra]");
        return;
 
-  trunc:
-       nd_print_trunc(ndo);
+  invalid:
+       nd_print_invalid(ndo);
 }