X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/d77ac851c76ff95e6542e93715786d29ba86c51f..a24cccfd4abcda51db9f73f46d425c7c1e357a87:/util-print.c diff --git a/util-print.c b/util-print.c index 06844898..83bcca66 100644 --- a/util-print.c +++ b/util-print.c @@ -57,12 +57,8 @@ #include "ascii_strcasecmp.h" #include "timeval-operations.h" -/* invalid string to print '(invalid)' for malformed or corrupted packets */ -const char istr[] = " (invalid)"; - #define TOKBUFSIZE 128 - enum date_flag { WITHOUT_DATE = 0, WITH_DATE = 1 }; enum time_flag { UTC_TIME = 0, LOCAL_TIME = 1 }; @@ -231,6 +227,32 @@ nd_printzp(netdissect_options *ndo, return (n == 0) ? 0 : ret; } +/* + * Print the timestamp .FRAC part (Microseconds/nanoseconds) + */ +static void +ts_frac_print(netdissect_options *ndo, int usec) +{ +#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION + switch (ndo->ndo_tstamp_precision) { + + case PCAP_TSTAMP_PRECISION_MICRO: + ND_PRINT(".%06u", (unsigned)usec); + break; + + case PCAP_TSTAMP_PRECISION_NANO: + ND_PRINT(".%09u", (unsigned)usec); + break; + + default: + ND_PRINT(".{unknown}"); + break; + } +#else + ND_PRINT(".%06u", (unsigned)usec); +#endif +} + /* * Print the timestamp as [YY:MM:DD] HH:MM:SS.FRAC. * if time_flag == LOCAL_TIME print local time else UTC/GMT time @@ -244,6 +266,11 @@ ts_date_hmsfrac_print(netdissect_options *ndo, int sec, int usec, struct tm *tm; char timestr[32]; + if ((unsigned)sec & 0x80000000) { + ND_PRINT("[Error converting time]"); + return; + } + if (time_flag == LOCAL_TIME) tm = localtime(&Time); else @@ -259,24 +286,7 @@ ts_date_hmsfrac_print(netdissect_options *ndo, int sec, int usec, strftime(timestr, sizeof(timestr), "%H:%M:%S", tm); ND_PRINT("%s", timestr); -#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION - switch (ndo->ndo_tstamp_precision) { - - case PCAP_TSTAMP_PRECISION_MICRO: - ND_PRINT(".%06u", usec); - break; - - case PCAP_TSTAMP_PRECISION_NANO: - ND_PRINT(".%09u", usec); - break; - - default: - ND_PRINT(".{unknown}"); - break; - } -#else - ND_PRINT(".%06u", usec); -#endif + ts_frac_print(ndo, usec); } /* @@ -285,24 +295,13 @@ ts_date_hmsfrac_print(netdissect_options *ndo, int sec, int usec, static void ts_unix_print(netdissect_options *ndo, int sec, int usec) { -#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION - switch (ndo->ndo_tstamp_precision) { - - case PCAP_TSTAMP_PRECISION_MICRO: - ND_PRINT("%u.%06u", (unsigned)sec, (unsigned)usec); - break; - - case PCAP_TSTAMP_PRECISION_NANO: - ND_PRINT("%u.%09u", (unsigned)sec, (unsigned)usec); - break; - - default: - ND_PRINT("%u.{unknown}", (unsigned)sec); - break; + if ((unsigned)sec & 0x80000000) { + ND_PRINT("[Error converting time]"); + return; } -#else - ND_PRINT("%u.%06u", (unsigned)sec, (unsigned)usec); -#endif + + ND_PRINT("%u", (unsigned)sec); + ts_frac_print(ndo, usec); } /* @@ -446,6 +445,12 @@ void nd_print_trunc(netdissect_options *ndo) ND_PRINT(" [|%s]", ndo->ndo_protocol); } +/* Print the invalid string */ +void nd_print_invalid(netdissect_options *ndo) +{ + ND_PRINT(" (invalid)"); +} + /* * this is a generic routine for printing unknown data; * we pass on the linefeed plus indentation string to @@ -747,8 +752,8 @@ fetch_token(netdissect_options *ndo, const u_char *pptr, u_int idx, u_int len, * the line ending. */ static u_int -print_txt_line(netdissect_options *ndo, const char *protoname, - const char *prefix, const u_char *pptr, u_int idx, u_int len) +print_txt_line(netdissect_options *ndo, const char *prefix, + const u_char *pptr, u_int idx, u_int len) { u_int startidx; u_int linelen; @@ -808,8 +813,8 @@ print_txt_line(netdissect_options *ndo, const char *protoname, */ trunc: linelen = idx - startidx; - ND_PRINT("%s%.*s[!%s]", prefix, (int)linelen, pptr + startidx, - protoname); + ND_PRINT("%s%.*s", prefix, (int)linelen, pptr + startidx); + nd_print_trunc(ndo); return (0); print: @@ -817,9 +822,10 @@ print: return (idx); } +/* Assign needed before calling txtproto_print(): ndo->ndo_protocol = "proto" */ void txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len, - const char *protoname, const char **cmds, u_int flags) + const char **cmds, u_int flags) { u_int idx, eol; u_char token[MAX_TOKEN+1]; @@ -891,8 +897,8 @@ txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len, } /* Capitalize the protocol name */ - for (pnp = protoname; *pnp != '\0'; pnp++) - ND_PRINT("%c", toupper((u_char)*pnp)); + for (pnp = ndo->ndo_protocol; *pnp != '\0'; pnp++) + ND_PRINT("%c", ND_TOUPPER((u_char)*pnp)); if (print_this) { /* @@ -911,14 +917,14 @@ txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len, */ ND_PRINT(", length: %u", len); for (idx = 0; - idx < len && (eol = print_txt_line(ndo, protoname, "\n\t", pptr, idx, len)) != 0; + idx < len && (eol = print_txt_line(ndo, "\n\t", pptr, idx, len)) != 0; idx = eol) ; } else { /* * Just print the first text line. */ - print_txt_line(ndo, protoname, ": ", pptr, 0, len); + print_txt_line(ndo, ": ", pptr, 0, len); } } }