From 021c25e22c39c2b7f2b99e9109cfd04a97e0e725 Mon Sep 17 00:00:00 2001 From: John Thacker Date: Wed, 29 Dec 2021 10:18:57 -0500 Subject: [PATCH] text2pcap: Use standard log debug level Remove the '-d' option from text2pcap, and move the two levels of debug messages in text2pcap and text_import to either LOG_LEVEL_DEBUG or LOG_LEVEL_NOISY as appropriate. --- doc/text2pcap.adoc | 11 ++------ docbook/release-notes.adoc | 6 ++++- test/suite_text2pcap.py | 3 +-- text2pcap.c | 24 +++++++++-------- ui/text_import.c | 53 ++++++++++++++++++-------------------- ui/text_import.h | 2 -- ui/text_import_regex.c | 21 +++++---------- 7 files changed, 53 insertions(+), 67 deletions(-) diff --git a/doc/text2pcap.adoc b/doc/text2pcap.adoc index 97c380649f..1423c4a545 100644 --- a/doc/text2pcap.adoc +++ b/doc/text2pcap.adoc @@ -14,7 +14,6 @@ text2pcap - Generate a capture file from an ASCII hexdump of packets [manarg] *text2pcap* [ *-a* ] -[ *-d* ] [ *-D* ] [ *-e* ] [ *-h* ] @@ -116,13 +115,6 @@ the ASCII text dump and not include it in the packet even if it looks like HEX. *NOTE:* Do not enable it if the input file does not contain the ASCII text dump. -- --d:: -+ --- -Displays debugging information during the process. Can be used -multiple times to generate more debugging information. --- - -D:: + -- @@ -227,7 +219,8 @@ link layer type (252) is selected the dissector defaults to "data". -q:: + -- -Be completely quiet during the process. +Don't display the summary of the options selected at the beginning, +or the count of packets processed at the end. -- -s ,,:: diff --git a/docbook/release-notes.adoc b/docbook/release-notes.adoc index 3da5ea89ba..804b3db7cf 100644 --- a/docbook/release-notes.adoc +++ b/docbook/release-notes.adoc @@ -53,9 +53,13 @@ They previously shipped with Npcap 1.55. PCRE2 is compatible with PCRE so the user-visible changes should be minimal. Some exotic patterns may now be invalid and require rewriting. ** Adds a new strict equality operator "===" or "all_eq". The expression "a === b" is true if and only if all a's are equal to b. - The negation of "===" can now be written as "!==" (any_ne), in adittion to "~=" (introduced in Wireshark 3.6.0). + The negation of "===" can now be written as "!==" (any_ne), in addition to "~=" (introduced in Wireshark 3.6.0). ** Adds the aliases "any_eq" for "==" and "all_ne" for "!=". +* text2pcap has been updated to use the new logging output options and the + "-d" flag has been removed. The "debug" log level corresponds to the old + "-d" flag, and the "noisy" log level corresponds to using "-d" multiple times. + * HTTP2 dissector now supports using fake headers to parse the DATAs of streams captured without first HEADERS frames of a long-lived stream (like gRPC streaming call which allows sending many request or response messages in one HTTP2 stream). User can specify fake headers according to the server port, stream id and direction of the long-lived stream that we start capturing packets after it is established. diff --git a/test/suite_text2pcap.py b/test/suite_text2pcap.py index ce675755f9..d81575d70b 100644 --- a/test/suite_text2pcap.py +++ b/test/suite_text2pcap.py @@ -134,7 +134,7 @@ def check_text2pcap(cmd_tshark, cmd_text2pcap, capture_file): else: pcapng_flag = '' # XXX: -t ISO also works now too for this output - text2pcap_cmd = '{cmd} {ns} -d -l {linktype} -t "%Y-%m-%d %H:%M:%S.%f" {in_f} {out_f}'.format( + text2pcap_cmd = '{cmd} {ns} -l {linktype} -t "%Y-%m-%d %H:%M:%S.%f" {in_f} {out_f}'.format( cmd = cmd_text2pcap, ns = pcapng_flag, linktype = encap_to_link_type[pre_cap_info['encapsulation']], @@ -268,7 +268,6 @@ class case_text2pcap_parsing(subprocesstest.SubprocessTestCase): testout_file = self.filename_from_id(testout_pcap) self.assertRun((cmd_text2pcap, '-n', - '-d', '-t', '%Y-%m-%d %H:%M:%S.', capture_file(txt_fname), testout_file, diff --git a/text2pcap.c b/text2pcap.c index 88e0bcb87c..673131a851 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -323,14 +323,17 @@ print_usage (FILE *output) " as the payload PROTO_NAME tag.\n" " Automatically sets link type to Upper PDU Export.\n" " EXPORTED_PDU payload defaults to \"data\" otherwise.\n" - "\n" - "Miscellaneous:\n" - " -h display this help and exit.\n" - " -v print version information and exit.\n" - " -d show detailed debug of parser states.\n" - " -q generate no output at all (automatically disables -d).\n" - "", + "\n", WTAP_MAX_PACKET_SIZE_STANDARD); + + ws_log_print_usage(output); + + fprintf(output, "\n" + "Miscellaneous:\n" + " -h display this help and exit\n" + " -v print version information and exit\n" + " -q don't report processed packet counts\n" + ""); } /* @@ -380,16 +383,15 @@ parse_options(int argc, char *argv[], text_import_info_t * const info, wtap_dump ws_init_version_info("Text2pcap (Wireshark)", NULL, NULL, NULL); /* Scan CLI parameters */ - while ((c = ws_getopt_long(argc, argv, "aDdhqe:i:l:m:nN:o:u:P:s:S:t:T:v4:6:", long_options, NULL)) != -1) { + while ((c = ws_getopt_long(argc, argv, "aDhqe:i:l:m:nN:o:u:P:s:S:t:T:v4:6:", long_options, NULL)) != -1) { switch (c) { case 'h': show_help_header("Generate a capture file from an ASCII hexdump of packets."); print_usage(stdout); exit(0); break; - case 'd': if (!quiet) info->debug++; break; case 'D': has_direction = TRUE; break; - case 'q': quiet = TRUE; info->debug = 0; break; + case 'q': quiet = TRUE; break; case 'l': pcap_link_type = (guint32)strtol(ws_optarg, NULL, 0); break; case 'm': max_offset = (guint32)strtol(ws_optarg, NULL, 0); break; case 'n': use_pcapng = TRUE; break; @@ -896,7 +898,7 @@ main(int argc, char *argv[]) ret = text_import(&info); - if (info.debug) + if (ws_log_get_level() >= LOG_LEVEL_DEBUG) fprintf(stderr, "\n-------------------------\n"); if (!quiet) { bytes_written = wtap_get_bytes_dumped(wdh); diff --git a/ui/text_import.c b/ui/text_import.c index 92960f4ba1..bf26887c4d 100644 --- a/ui/text_import.c +++ b/ui/text_import.c @@ -108,11 +108,6 @@ /* maximum time precision we can handle = 10^(-SUBSEC_PREC) */ #define SUBSEC_PREC 9 -#define debug_printf(level, ...) \ - if (info_p->debug >= (level)) { \ - printf(__VA_ARGS__); \ - } - static text_import_info_t *info_p; /* Dummy Ethernet header */ @@ -220,7 +215,8 @@ static const char *token_str[] = {"", "Offset", "Directive", "Text", - "End-of-line" + "End-of-line", + "End-of-file" }; /* ----- Skeleton Packet Headers --------------------------------------------------*/ @@ -754,12 +750,12 @@ append_to_preamble(char *str) /* XXX: Just keep going? This is probably not a problem, as above.*/ (void) g_strlcpy(&packet_preamble[packet_preamble_len], str, PACKET_PREAMBLE_MAX_LEN); packet_preamble_len += (int) toklen; - if (info_p->debug >= 2) { + if (ws_log_get_level() >= LOG_LEVEL_NOISY) { char *c; char xs[PACKET_PREAMBLE_MAX_LEN]; (void) g_strlcpy(xs, packet_preamble, PACKET_PREAMBLE_MAX_LEN); while ((c = strchr(xs, '\r')) != NULL) *c=' '; - fprintf (stderr, "[[append_to_preamble: \"%s\"]]", xs); + ws_noisy("[[append_to_preamble: \"%s\"]]", xs); } } @@ -907,16 +903,19 @@ static int parse_plain_data(guchar** src, const guchar* src_end, */ guint64 val; int j; - debug_printf(3, "parsing data: "); + if (ws_log_get_level() >= LOG_LEVEL_NOISY) { + char* debug_str = wmem_strndup(NULL, *src, (src_end-*src)); + ws_noisy("parsing data: %s", debug_str); + wmem_free(NULL, debug_str); + } while (*src < src_end && *dest + encoding->bytes_per_unit <= dest_end) { - debug_printf(3, "%c", **src); val = encoding->table[**src]; switch (val) { case INVALID_VALUE: status = -1; goto remainder; case WHITESPACE_VALUE: - fprintf(stderr, "Unexpected char %d in data\n", **src); + ws_warning("Unexpected char %d in data", **src); break; default: c_val = c_val << encoding->bits_per_char | val; @@ -940,7 +939,6 @@ remainder: **dest = (gchar) (c_val >> (j - 8)); *dest += 1; } - debug_printf(3, "\n"); return status * units; } @@ -983,7 +981,7 @@ void parse_data(guchar* start_field, guchar* end_field, enum data_encoding encod } break; default: - fprintf(stderr, "not implemented/invalid encoding type\n"); + ws_critical("not implemented/invalid encoding type"); return; } } @@ -1113,7 +1111,7 @@ _parse_time(const guchar* start_field, const guchar* end_field, const gchar* _fo *nsec = nsec_buf; } - debug_printf(3, "parsed time %s Format(%s), time(%u), subsecs(%u)\n", field, _format, (guint32)*sec, (guint32)*nsec); + ws_noisy("parsed time %s Format(%s), time(%u), subsecs(%u)\n", field, _format, (guint32)*sec, (guint32)*nsec); return TRUE; } @@ -1182,11 +1180,11 @@ parse_preamble (void) ws_warning("Time conversion (%s) failed for %s on input packet %d.", info_p->timestamp_format, packet_preamble, info_p->num_packets_read); } } - if (info_p->debug >= 2) { + if (ws_log_get_level() >= LOG_LEVEL_NOISY) { char *c; while ((c = strchr(packet_preamble, '\r')) != NULL) *c=' '; - fprintf(stderr, "[[parse_preamble: \"%s\"]]\n", packet_preamble); - fprintf(stderr, "Format(%s), time(%u), subsecs(%u)\n", info_p->timestamp_format, (guint32)ts_sec, ts_nsec); + ws_noisy("[[parse_preamble: \"%s\"]]", packet_preamble); + ws_noisy("Format(%s), time(%u), subsecs(%u)", info_p->timestamp_format, (guint32)ts_sec, ts_nsec); } if (!got_time) { @@ -1209,8 +1207,7 @@ parse_preamble (void) static import_status_t start_new_packet(gboolean cont) { - if (info_p->debug>=1) - fprintf(stderr, "Start new packet (cont = %s).\n", cont ? "TRUE" : "FALSE"); + ws_debug("Start new packet (cont = %s).", cont ? "TRUE" : "FALSE"); /* Write out the current packet, if required */ if (write_current_packet(cont) != IMPORT_SUCCESS) @@ -1228,10 +1225,12 @@ start_new_packet(gboolean cont) * Process a directive */ static void -process_directive (char *str) +process_directive (char *str _U_) { - fprintf(stderr, "\n--- Directive [%s] currently unsupported ---\n", str+10); - + char **tokens; + tokens = g_strsplit_set(str+10, "\r\n", 2); + ws_message("--- Directive [%s] currently unsupported ---", tokens[0]); + g_strfreev(tokens); } /*---------------------------------------------------------------------- @@ -1256,12 +1255,12 @@ parse_token(token_t token, char *str) * scanner. The code should be self_documenting. */ - if (info_p->debug>=2) { + if (ws_log_get_level() >= LOG_LEVEL_NOISY) { /* Sanitize - remove all '\r' */ char *c; if (str!=NULL) { while ((c = strchr(str, '\r')) != NULL) *c=' '; } - fprintf(stderr, "(%s, %s \"%s\") -> (", + ws_noisy("(%s, %s \"%s\") -> (", state_str[state], token_str[token], str ? str : ""); } @@ -1368,8 +1367,7 @@ parse_token(token_t token, char *str) state = READ_OFFSET; } else { /* Bad offset; switch to INIT state */ - if (info_p->debug>=1) - fprintf(stderr, "Inconsistent offset. Expecting %0X, got %0X. Ignoring rest of packet\n", + ws_message("Inconsistent offset. Expecting %0X, got %0X. Ignoring rest of packet", curr_offset, num); if (write_current_packet(FALSE) != IMPORT_SUCCESS) return IMPORT_FAILURE; @@ -1518,8 +1516,7 @@ parse_token(token_t token, char *str) return IMPORT_FAILURE; } - if (info_p->debug>=2) - fprintf(stderr, ", %s)\n", state_str[state]); + ws_noisy(", %s)", state_str[state]); return IMPORT_SUCCESS; } diff --git a/ui/text_import.h b/ui/text_import.h index bfd9581fca..7e68fad2ca 100644 --- a/ui/text_import.h +++ b/ui/text_import.h @@ -63,8 +63,6 @@ enum text_import_mode { typedef struct { - int debug; - /* Input info */ // TODO: add const, as this way string constants can't be used // BUT: the other way clang-check complaines when you free them diff --git a/ui/text_import_regex.c b/ui/text_import_regex.c index 2c3c2316d9..7b1b26bfb4 100644 --- a/ui/text_import_regex.c +++ b/ui/text_import_regex.c @@ -25,17 +25,10 @@ typedef unsigned int uint; /*--- Options --------------------------------------------------------------------*/ -static int debug = 0; - -#define debug_printf(level, ...) \ - if (debug >= (level)) { \ - printf(__VA_ARGS__); \ - } - int text_import_regex(const text_import_info_t* info) { int status = 1; int parsed_packets = 0; - debug_printf(1, "starting import...\n"); + ws_debug("starting import..."); // IO GMappedFile* file = g_mapped_file_ref(info->regex.import_text_GMappedFile); @@ -68,9 +61,9 @@ int text_import_regex(const text_import_info_t* info) { } } - debug_printf(1, "regex has %s%s%s\n", re_dir ? "dir, " : "", - re_time ? "time, " : "", - re_seqno ? "seqno, " : ""); + ws_debug("regex has %s%s%s", re_dir ? "dir, " : "", + re_time ? "time, " : "", + re_seqno ? "seqno, " : ""); g_regex_match(info->regex.format, f_content, G_REGEX_MATCH_NOTEMPTY, &match); while (g_match_info_matches(match)) { /* parse the data */ @@ -97,9 +90,9 @@ int text_import_regex(const text_import_info_t* info) { g_match_info_fetch_named_pos(match, "seqno", &field_start, &field_end)) parse_seqno(f_content + field_start, f_content + field_end); - if (debug >= 2) { + if (ws_log_get_level() == LOG_LEVEL_NOISY) { g_match_info_fetch_pos(match, 0, &field_start, &field_end); - printf("Packet %d at %x to %x: %.*s\n", parsed_packets + 1, + ws_noisy("Packet %d at %x to %x: %.*s\n", parsed_packets + 1, field_start, field_end, field_end - field_start, f_content + field_start); } @@ -115,7 +108,7 @@ int text_import_regex(const text_import_info_t* info) { break; } } - debug_printf(1, "processed %d packets\n", parsed_packets); + ws_debug("processed %d packets", parsed_packets); g_match_info_unref(match); g_mapped_file_unref(file); return status * parsed_packets;