From 3e8a443c3671baa37ae7870f08fb9b4bf386fd24 Mon Sep 17 00:00:00 2001
From: Guy Harris <guy@alum.mit.edu>
Date: Tue, 11 Nov 2014 18:37:35 -0800
Subject: [PATCH] Clean up error message printing.

Have "struct aodv_rerr" just be the header, not including the actual
destinations.

Simplify the logic somewhat, and make it similar in the print routines
for the three types of error messages.
---
 print-aodv.c | 88 +++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 46 insertions(+), 42 deletions(-)

Index: tcpdump-3.9.8/print-aodv.c
===================================================================
--- tcpdump-3.9.8.orig/print-aodv.c	2014-12-12 14:16:31.132015273 +0100
+++ tcpdump-3.9.8/print-aodv.c	2014-12-12 14:16:31.145015424 +0100
@@ -143,34 +143,31 @@ aodv_rrep(const struct aodv_rrep *ap, co
 }
 
 static void
-aodv_rerr(const struct aodv_rerr *ap, const u_char *dat, u_int length)
+aodv_rerr(netdissect_options *ndo, const struct aodv_rerr *ap, const u_char *dat, u_int length)
 {
-	u_int i;
-	const struct rerr_unreach *dp = NULL;
-	int n, trunc;
+	u_int i, dc;
+	const struct rerr_unreach *dp;
 
 	if (snapend < dat) {
 		printf(" [|aodv]");
 		return;
 	}
 	i = min(length, (u_int)(snapend - dat));
-	if (i < offsetof(struct aodv_rerr, r)) {
+	if (i < sizeof(*ap)) {
 		printf(" [|rerr]");
 		return;
 	}
-	i -= offsetof(struct aodv_rerr, r);
-	dp = &ap->r.dest[0];
-	n = ap->rerr_dc * sizeof(ap->r.dest[0]);
+	i -= sizeof(*ap);
 	printf(" rerr %s [items %u] [%u]:",
 	    ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
 	    ap->rerr_dc, length);
-	trunc = n - (i/sizeof(ap->r.dest[0]));
-	for (; i >= sizeof(ap->r.dest[0]);
-	    ++dp, i -= sizeof(ap->r.dest[0])) {
+	dp = (struct rerr_unreach *)(void *)(ap + 1);
+	for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp);
+	    ++dp, --dc, i -= sizeof(*dp)) {
 		printf(" {%s}(%ld)", ipaddr_string(&dp->u_da),
 		    (unsigned long)EXTRACT_32BITS(&dp->u_ds));
 	}
-	if (trunc)
+	if ((i % sizeof(*dp)) != 0)
 		printf("[|rerr]");
 }
 
@@ -253,28 +250,35 @@ aodv_v6_rrep(const struct aodv_rrep6 *ap
 
 static void
 #ifdef INET6
-aodv_v6_rerr(const struct aodv_rerr *ap, u_int length)
+aodv_v6_rerr(netdissect_options *ndo, const struct aodv_rerr *ap, const u_char *dat, u_int length)
 #else
-aodv_v6_rerr(const struct aodv_rerr *ap _U_, u_int length)
+aodv_v6_rerr(netdissect_options *ndo, const struct aodv_rerr *ap _U_, const u_char *dat, u_int length)
 #endif
 {
 #ifdef INET6
-	const struct rerr_unreach6 *dp6 = NULL;
-	int i, j, n, trunc;
+	u_int i, dc;
+	const struct rerr_unreach6 *dp6;
 
-	i = length - offsetof(struct aodv_rerr, r);
-	j = sizeof(ap->r.dest6[0]);
-	dp6 = &ap->r.dest6[0];
-	n = ap->rerr_dc * j;
+	if (ndo->ndo_snapend < dat) {
+		ND_PRINT((ndo, " [|aodv]"));
+		return;
+	}
+	i = min(length, (u_int)(ndo->ndo_snapend - dat));
+        if (i < sizeof(*ap)) {
+		ND_PRINT((ndo, " [|rerr]"));
+		return;
+	}
+	i -= sizeof(*ap);
 	printf(" rerr %s [items %u] [%u]:",
 	    ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
 	    ap->rerr_dc, length);
-	trunc = n - (i/j);
-	for (; i -= j >= 0; ++dp6) {
+	dp6 = (struct rerr_unreach6 *)(void *)(ap + 1);
+	for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp6);
+	    ++dp6, --dc, i -= sizeof(*dp6)) {
 		printf(" {%s}(%ld)", ip6addr_string(&dp6->u_da),
 		    (unsigned long)EXTRACT_32BITS(&dp6->u_ds));
 	}
-	if (trunc)
+	if ((i % sizeof(*dp6)) != 0)
 		printf("[|rerr]");
 #else
 	printf(" rerr %u", length);
@@ -361,28 +365,35 @@ aodv_v6_draft_01_rrep(const struct aodv_
 
 static void
 #ifdef INET6
-aodv_v6_draft_01_rerr(const struct aodv_rerr *ap, u_int length)
+aodv_v6_draft_01_rerr(netdissect_options *ndo, const struct aodv_rerr *ap, const u_char *dat, u_int length)
 #else
-aodv_v6_draft_01_rerr(const struct aodv_rerr *ap _U_, u_int length)
+aodv_v6_draft_01_rerr(netdissect_options *ndo, const struct aodv_rerr *ap _U_, const u_char *dat, u_int length)
 #endif
 {
 #ifdef INET6
-	const struct rerr_unreach6_draft_01 *dp6 = NULL;
-	int i, j, n, trunc;
+	u_int i, dc;
+	const struct rerr_unreach6_draft_01 *dp6;
 
-	i = length - offsetof(struct aodv_rerr, r);
-	j = sizeof(ap->r.dest6_draft_01[0]);
-	dp6 = &ap->r.dest6_draft_01[0];
-	n = ap->rerr_dc * j;
+	if (ndo->ndo_snapend < dat) {
+		ND_PRINT((ndo, " [|aodv]"));
+		return;
+	}
+	i = min(length, (u_int)(ndo->ndo_snapend - dat));
+	if (i < sizeof(*ap)) {
+		ND_PRINT((ndo, " [|rerr]"));
+		return;
+	}
+	i -= sizeof(*ap);
 	printf(" rerr %s [items %u] [%u]:",
 	    ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
 	    ap->rerr_dc, length);
-	trunc = n - (i/j);
-	for (; i -= j >= 0; ++dp6) {
+	dp6 = (struct rerr_unreach6_draft_01 *)(void *)(ap + 1);
+	for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp6);
+	    ++dp6, --dc, i -= sizeof(*dp6)) {
 		printf(" {%s}(%ld)", ip6addr_string(&dp6->u_da),
 		    (unsigned long)EXTRACT_32BITS(&dp6->u_ds));
 	}
-	if (trunc)
+	if ((i % sizeof(*dp6)) != 0)
 		printf("[|rerr]");
 #else
 	printf(" rerr %u", length);
@@ -421,9 +432,9 @@ aodv_print(netdissect_options *ndo,
 
 	case AODV_RERR:
 		if (is_ip6)
-			aodv_v6_rerr((const struct aodv_rerr *)dat, length);
+			aodv_v6_rerr(ndo, (const struct aodv_rerr *)dat, dat, length);
 		else
-			aodv_rerr((const struct aodv_rerr *)dat, dat, length);
+			aodv_rerr(ndo, (const struct aodv_rerr *)dat, dat, length);
 		break;
 
 	case AODV_RREP_ACK:
@@ -439,7 +450,7 @@ aodv_print(netdissect_options *ndo,
 		break;
 
 	case AODV_V6_DRAFT_01_RERR:
-		aodv_v6_draft_01_rerr((const struct aodv_rerr *)dat, length);
+		aodv_v6_draft_01_rerr(ndo, (const struct aodv_rerr *)dat, dat, length);
 		break;
 
 	case AODV_V6_DRAFT_01_RREP_ACK:
Index: tcpdump-3.9.8/print-ip6.c
===================================================================
--- tcpdump-3.9.8.orig/print-ip6.c	2014-12-12 14:16:31.132015273 +0100
+++ tcpdump-3.9.8/print-ip6.c	2014-12-12 14:27:05.611365246 +0100
@@ -167,7 +167,7 @@ ip6_print(register const u_char *bp, reg
 			tcp_print(cp, len, (const u_char *)ip6, fragmented);
 			return;
 		case IPPROTO_UDP:
-			udp_print(ndo, cp, len, (const u_char *)ip6, fragmented);
+			udp_print(gndo, cp, len, (const u_char *)ip6, fragmented);
 			return;
 		case IPPROTO_ICMPV6:
 			icmp6_print(cp, len, (const u_char *)ip6, fragmented);
