Have separate routines for signed and unsigned relative time stamps.
Have the routine for signed time stamps handle the case of -2^31, where
you can't negate it and have the result fit in a 32-bit signed integer.
There are currently no users of the signed relative time stamp routine,
but it's there in case anybody needs it (and is passing 32-bit
quantitites, so that there's a risk that a packet might contain -2^31 as
the time stamp).
Fixes an array overflow found with American Fuzzy Lop by Hanno Böck.
#define ND_DEFAULTPRINT(ap, length) (*ndo->ndo_default_print)(ndo, ap, length)
extern void ts_print(netdissect_options *, const struct timeval *);
-extern void relts_print(netdissect_options *, int);
+extern void signed_relts_print(netdissect_options *, int32_t);
+extern void unsigned_relts_print(netdissect_options *, uint32_t);
extern void fn_print_char(netdissect_options *, u_char);
extern int fn_print(netdissect_options *, const u_char *, const u_char *);
} else if (ndo->ndo_vflag > 2) {
/* print ttl */
ND_PRINT((ndo, " ["));
- relts_print(ndo, EXTRACT_32BITS(cp));
+ unsigned_relts_print(ndo, EXTRACT_32BITS(cp));
ND_PRINT((ndo, "]"));
cp += 4;
} else {
ND_PRINT((ndo, " src %s grp %s", ipaddr_string(ndo, bp), ipaddr_string(ndo, bp + 4)));
bp += 8;
ND_PRINT((ndo, " timer "));
- relts_print(ndo, EXTRACT_32BITS(bp));
+ unsigned_relts_print(ndo, EXTRACT_32BITS(bp));
return (0);
trunc:
return (-1);
ND_PRINT((ndo, "addr=%s", ipaddr_string(ndo, &hp->hsrp_virtaddr)));
if (ndo->ndo_vflag) {
ND_PRINT((ndo, " hellotime="));
- relts_print(ndo, hp->hsrp_hellotime);
+ unsigned_relts_print(ndo, hp->hsrp_hellotime);
ND_PRINT((ndo, " holdtime="));
- relts_print(ndo, hp->hsrp_holdtime);
+ unsigned_relts_print(ndo, hp->hsrp_holdtime);
ND_PRINT((ndo, " priority=%d", hp->hsrp_priority));
ND_PRINT((ndo, " auth=\""));
if (fn_printn(ndo, hp->hsrp_authdata, sizeof(hp->hsrp_authdata),
register const u_char *bp, register u_int len)
{
u_int mrc;
- int mrt;
+ u_int mrt;
u_int nsrcs;
register u_int i;
if (mrt < 600) {
ND_PRINT((ndo, "%.1fs", mrt * 0.1));
} else {
- relts_print(ndo, mrt / 10);
+ unsigned_relts_print(ndo, mrt / 10);
}
ND_PRINT((ndo, "]"));
}
EXTRACT_16BITS(&grbn->rdclass))));
}
ND_PRINT((ndo, " TTL "));
- relts_print(ndo, EXTRACT_32BITS(&grbn->ttl));
+ unsigned_relts_print(ndo, EXTRACT_32BITS(&grbn->ttl));
ND_PRINT((ndo, " %u/%u", EXTRACT_16BITS(&grbn->nrdatas),
EXTRACT_16BITS(&grbn->nsigs)));
hold = EXTRACT_16BITS(&bp[6]);
if (hold != 180) {
ND_PRINT((ndo, "Hold "));
- relts_print(ndo, hold);
+ unsigned_relts_print(ndo, hold);
}
ND_PRINT((ndo, "%s (%s/%d, %s", njoin ? "Join" : "Prune",
ipaddr_string(ndo, &bp[26]), bp[25] & 0x3f,
if (ndo->ndo_vflag > 1)
ND_PRINT((ndo, "\n"));
ND_PRINT((ndo, " Hold time: "));
- relts_print(ndo, EXTRACT_16BITS(&bp[6]));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(&bp[6]));
if (ndo->ndo_vflag < 2)
return;
bp += 8;
if (ndo->ndo_vflag) {
ND_TCHECK2(bp[10],2);
ND_PRINT((ndo, " (Hold-time "));
- relts_print(ndo, EXTRACT_16BITS(&bp[10]));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(&bp[10]));
ND_PRINT((ndo, ")"));
}
break;
if (EXTRACT_32BITS(&bp[12]) != 0xffffffff)
ND_PRINT((ndo, "/%s", ipaddr_string(ndo, &bp[12])));
ND_PRINT((ndo, " RP %s hold ", ipaddr_string(ndo, &bp[16])));
- relts_print(ndo, EXTRACT_16BITS(&bp[22]));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(&bp[22]));
}
break;
case PIMV1_TYPE_ASSERT:
ND_PRINT((ndo, " Hold "));
hold = EXTRACT_16BITS(&bp[2]);
if (hold)
- relts_print(ndo, EXTRACT_16BITS(&bp[2]));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(&bp[2]));
else
ND_PRINT((ndo, "FOREVER"));
switch (otype) {
case PIMV2_HELLO_OPTION_HOLDTIME:
- relts_print(ndo, EXTRACT_16BITS(bp));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(bp));
break;
case PIMV2_HELLO_OPTION_LANPRUNEDELAY:
ND_PRINT((ndo, "v%d", *bp));
if (*(bp+1) != 0) {
ND_PRINT((ndo, ", interval "));
- relts_print(ndo, *(bp+1));
+ unsigned_relts_print(ndo, *(bp+1));
}
if (EXTRACT_16BITS(bp+2) != 0) {
ND_PRINT((ndo, " ?0x%04x?", EXTRACT_16BITS(bp+2)));
if (holdtime == 0xffff)
ND_PRINT((ndo, "infinite"));
else
- relts_print(ndo, holdtime);
+ unsigned_relts_print(ndo, holdtime);
}
bp += 4; len -= 4;
for (i = 0; i < ngroup; i++) {
goto bs_done;
}
ND_PRINT((ndo, ",holdtime="));
- relts_print(ndo, EXTRACT_16BITS(bp));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(bp));
if (bp + 2 >= ep) {
ND_PRINT((ndo, "...)"));
goto bs_done;
ND_PRINT((ndo, " prio=%d", bp[1]));
if (bp + 3 >= ep) break;
ND_PRINT((ndo, " holdtime="));
- relts_print(ndo, EXTRACT_16BITS(&bp[2]));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(&bp[2]));
bp += 4;
/* Encoded-Unicast-RP-Address */
bp += advance;
ND_TCHECK2(bp[0], 2);
ND_PRINT((ndo, " TUNR "));
- relts_print(ndo, EXTRACT_16BITS(bp));
+ unsigned_relts_print(ndo, EXTRACT_16BITS(bp));
break;
}
/*
- * Print a relative number of seconds (e.g. hold time, prune timer)
+ * Print a signed relative number of seconds (e.g. hold time, prune timer)
* in the form 5m1s. This does no truncation, so 32230861 seconds
* is represented as 1y1w1d1h1m1s.
*/
void
-relts_print(netdissect_options *ndo,
- int secs)
+signed_relts_print(netdissect_options *ndo,
+ int32_t secs)
{
+ uint32_t secs_abs;
static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
- static const int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
+ static const u_int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
const char **l = lengths;
- const int *s = seconds;
+ const u_int *s = seconds;
if (secs == 0) {
ND_PRINT((ndo, "0s"));
return;
}
+ if (secs == -2147483648) {
+ /*
+ * -2^31; you can't fit its absolute value into a 32-bit
+ * signed integer.
+ *
+ * We calculate the right string by hand.
+ */
+ ND_PRINT((ndo, "-68y5w3h14m8s"));
+ return;
+ }
if (secs < 0) {
+ /*
+ * We now know -secs will fit into secs.
+ */
ND_PRINT((ndo, "-"));
secs = -secs;
}
+ secs_abs = secs;
+ while (secs_abs > 0) {
+ if (secs_abs >= *s) {
+ ND_PRINT((ndo, "%d%s", secs_abs / *s, *l));
+ secs_abs -= (secs_abs / *s) * *s;
+ }
+ s++;
+ l++;
+ }
+}
+
+/*
+ * Print an unsigned relative number of seconds (e.g. hold time, prune timer)
+ * in the form 5m1s. This does no truncation, so 32230861 seconds
+ * is represented as 1y1w1d1h1m1s.
+ */
+void
+unsigned_relts_print(netdissect_options *ndo,
+ uint32_t secs)
+{
+ static const char *lengths[] = {"y", "w", "d", "h", "m", "s"};
+ static const u_int seconds[] = {31536000, 604800, 86400, 3600, 60, 1};
+ const char **l = lengths;
+ const u_int *s = seconds;
+
+ if (secs == 0) {
+ ND_PRINT((ndo, "0s"));
+ return;
+ }
while (secs > 0) {
if (secs >= *s) {
ND_PRINT((ndo, "%d%s", secs / *s, *l));