Skip to content

Commit 26f2776

Browse files
committed
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.
1 parent 04a951d commit 26f2776

File tree

2 files changed

+47
-43
lines changed

2 files changed

+47
-43
lines changed

aodv.h

-7
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ struct aodv_rerr {
134134
u_int8_t rerr_flags; /* various flags */
135135
u_int8_t rerr_zero0; /* reserved, set to zero */
136136
u_int8_t rerr_dc; /* destination count */
137-
union {
138-
struct rerr_unreach dest[1];
139-
#ifdef INET6
140-
struct rerr_unreach6 dest6[1];
141-
struct rerr_unreach6_draft_01 dest6_draft_01[1];
142-
#endif
143-
} r;
144137
};
145138

146139
#define RERR_NODELETE 0x80 /* don't delete the link */

print-aodv.c

+47-36
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,29 @@ aodv_rrep(const struct aodv_rrep *ap, const u_char *dat, u_int length)
145145
static void
146146
aodv_rerr(const struct aodv_rerr *ap, const u_char *dat, u_int length)
147147
{
148-
u_int i;
149-
const struct rerr_unreach *dp = NULL;
150-
int n, trunc;
148+
u_int i, dc;
149+
const struct rerr_unreach *dp;
151150

152151
if (snapend < dat) {
153152
printf(" [|aodv]");
154153
return;
155154
}
156155
i = min(length, (u_int)(snapend - dat));
157-
if (i < offsetof(struct aodv_rerr, r)) {
156+
if (i < sizeof(*ap)) {
158157
printf(" [|rerr]");
159158
return;
160159
}
161-
i -= offsetof(struct aodv_rerr, r);
162-
dp = &ap->r.dest[0];
163-
n = ap->rerr_dc * sizeof(ap->r.dest[0]);
160+
i -= sizeof(*ap);
164161
printf(" rerr %s [items %u] [%u]:",
165162
ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
166163
ap->rerr_dc, length);
167-
trunc = n - (i/sizeof(ap->r.dest[0]));
168-
for (; i >= sizeof(ap->r.dest[0]);
169-
++dp, i -= sizeof(ap->r.dest[0])) {
164+
dp = (struct rerr_unreach *)(void *)(ap + 1);
165+
for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp);
166+
++dp, --dc, i -= sizeof(*dp)) {
170167
printf(" {%s}(%ld)", ipaddr_string(&dp->u_da),
171168
(unsigned long)EXTRACT_32BITS(&dp->u_ds));
172169
}
173-
if (trunc)
170+
if ((i % sizeof(*dp)) != 0)
174171
printf("[|rerr]");
175172
}
176173

@@ -253,28 +250,35 @@ aodv_v6_rrep(const struct aodv_rrep6 *ap _U_, const u_char *dat _U_, u_int lengt
253250

254251
static void
255252
#ifdef INET6
256-
aodv_v6_rerr(const struct aodv_rerr *ap, u_int length)
253+
aodv_v6_rerr(const struct aodv_rerr *ap, const u_char *dat, u_int length)
257254
#else
258-
aodv_v6_rerr(const struct aodv_rerr *ap _U_, u_int length)
255+
aodv_v6_rerr(const struct aodv_rerr *ap _U_, const u_char *dat, u_int length)
259256
#endif
260257
{
261258
#ifdef INET6
262-
const struct rerr_unreach6 *dp6 = NULL;
263-
int i, j, n, trunc;
259+
u_int i, dc;
260+
const struct rerr_unreach6 *dp6;
264261

265-
i = length - offsetof(struct aodv_rerr, r);
266-
j = sizeof(ap->r.dest6[0]);
267-
dp6 = &ap->r.dest6[0];
268-
n = ap->rerr_dc * j;
262+
if (snapend < dat) {
263+
printf(" [|aodv]");
264+
return;
265+
}
266+
i = min(length, (u_int)(snapend - dat));
267+
if (i < sizeof(*ap)) {
268+
printf(" [|rerr]");
269+
return;
270+
}
271+
i -= sizeof(*ap);
269272
printf(" rerr %s [items %u] [%u]:",
270273
ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
271274
ap->rerr_dc, length);
272-
trunc = n - (i/j);
273-
for (; i -= j >= 0; ++dp6) {
275+
dp6 = (struct rerr_unreach6 *)(void *)(ap + 1);
276+
for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp6);
277+
++dp6, --dc, i -= sizeof(*dp6)) {
274278
printf(" {%s}(%ld)", ip6addr_string(&dp6->u_da),
275279
(unsigned long)EXTRACT_32BITS(&dp6->u_ds));
276280
}
277-
if (trunc)
281+
if ((i % sizeof(*dp6)) != 0)
278282
printf("[|rerr]");
279283
#else
280284
printf(" rerr %u", length);
@@ -360,28 +364,35 @@ aodv_v6_draft_01_rrep(const struct aodv_rrep6_draft_01 *ap _U_, const u_char *da
360364

361365
static void
362366
#ifdef INET6
363-
aodv_v6_draft_01_rerr(const struct aodv_rerr *ap, u_int length)
367+
aodv_v6_draft_01_rerr(const struct aodv_rerr *ap, const u_char *dat, u_int length)
364368
#else
365-
aodv_v6_draft_01_rerr(const struct aodv_rerr *ap _U_, u_int length)
369+
aodv_v6_draft_01_rerr(const struct aodv_rerr *ap _U_, const u_char *dat, u_int length)
366370
#endif
367371
{
368372
#ifdef INET6
369-
const struct rerr_unreach6_draft_01 *dp6 = NULL;
370-
int i, j, n, trunc;
373+
u_int i, dc;
374+
const struct rerr_unreach6_draft_01 *dp6;
371375

372-
i = length - offsetof(struct aodv_rerr, r);
373-
j = sizeof(ap->r.dest6_draft_01[0]);
374-
dp6 = &ap->r.dest6_draft_01[0];
375-
n = ap->rerr_dc * j;
376+
if (snapend < dat) {
377+
printf(" [|aodv]");
378+
return;
379+
}
380+
i = min(length, (u_int)(snapend - dat));
381+
if (i < sizeof(*ap)) {
382+
printf(" [|rerr]");
383+
return;
384+
}
385+
i -= sizeof(*ap);
376386
printf(" rerr %s [items %u] [%u]:",
377387
ap->rerr_flags & RERR_NODELETE ? "[D]" : "",
378388
ap->rerr_dc, length);
379-
trunc = n - (i/j);
380-
for (; i -= j >= 0; ++dp6) {
389+
dp6 = (struct rerr_unreach6_draft_01 *)(void *)(ap + 1);
390+
for (dc = ap->rerr_dc; dc != 0 && i >= sizeof(*dp6);
391+
++dp6, --dc, i -= sizeof(*dp6)) {
381392
printf(" {%s}(%ld)", ip6addr_string(&dp6->u_da),
382393
(unsigned long)EXTRACT_32BITS(&dp6->u_ds));
383394
}
384-
if (trunc)
395+
if ((i % sizeof(*dp6)) != 0)
385396
printf("[|rerr]");
386397
#else
387398
printf(" rerr %u", length);
@@ -391,7 +402,7 @@ aodv_v6_draft_01_rerr(const struct aodv_rerr *ap _U_, u_int length)
391402
void
392403
aodv_print(const u_char *dat, u_int length, int is_ip6)
393404
{
394-
uint8_t msg_type;
405+
u_int8_t msg_type;
395406

396407
/*
397408
* The message type is the first byte; make sure we have it
@@ -419,7 +430,7 @@ aodv_print(const u_char *dat, u_int length, int is_ip6)
419430

420431
case AODV_RERR:
421432
if (is_ip6)
422-
aodv_v6_rerr((const struct aodv_rerr *)dat, length);
433+
aodv_v6_rerr((const struct aodv_rerr *)dat, dat, length);
423434
else
424435
aodv_rerr((const struct aodv_rerr *)dat, dat, length);
425436
break;
@@ -437,7 +448,7 @@ aodv_print(const u_char *dat, u_int length, int is_ip6)
437448
break;
438449

439450
case AODV_V6_DRAFT_01_RERR:
440-
aodv_v6_draft_01_rerr((const struct aodv_rerr *)dat, length);
451+
aodv_v6_draft_01_rerr((const struct aodv_rerr *)dat, dat, length);
441452
break;
442453

443454
case AODV_V6_DRAFT_01_RREP_ACK:

0 commit comments

Comments
 (0)