X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/306c2a0384af923a73bf234f3c8bc186ceff0b58..a63600a1fc28dbc7ae7ce9f996829c49a25fb33c:/print-lwres.c diff --git a/print-lwres.c b/print-lwres.c index ed1753a3..b8f9b49a 100644 --- a/print-lwres.c +++ b/print-lwres.c @@ -35,9 +35,7 @@ #include "netdissect-stdinc.h" -#include -#include - +#define ND_LONGJMP_FROM_TCHECK #include "netdissect.h" #include "addrtoname.h" #include "extract.h" @@ -96,7 +94,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 +182,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 +222,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 @@ -271,7 +242,6 @@ lwres_printaddr(netdissect_options *ndo, p = p0; ap = (const lwres_addr_t *)p; - ND_TCHECK_2(ap->length); l = GET_BE_U_2(ap->length); p += LWRES_ADDR_LEN; ND_TCHECK_LEN(p, l); @@ -298,9 +268,6 @@ lwres_printaddr(netdissect_options *ndo, } return ND_BYTES_BETWEEN(p, p0); - - trunc: - return -1; } void @@ -324,7 +291,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; } @@ -400,8 +369,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: @@ -417,7 +384,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: @@ -441,8 +408,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: @@ -485,16 +450,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; } @@ -503,7 +464,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; @@ -522,16 +483,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; @@ -560,8 +517,6 @@ lwres_print(netdissect_options *ndo, s = p + LWRES_GRBNRESPONSE_LEN; advance = lwres_printnamelen(ndo, s); - if (advance < 0) - goto trunc; s += advance; /* rdatas */ @@ -569,8 +524,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; } @@ -579,8 +532,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; @@ -597,10 +548,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(s, bp) < GET_BE_U_4(np->length)) ND_PRINT("[extra]"); return; - trunc: - nd_print_trunc(ndo); + invalid: + nd_print_invalid(ndo); }