editcap: Add --discard-packet-comments option and fix -a option

Added new `editcap` option `--discard-packet-comments` to discard all
packet comments when editing a pcap file and writing a new one.  This
behaves the same way as the existing `--discard-capture-comment`
option only it discards packet comments and not capture file comments.
Packet comments added with `-a` on the same command line are not
discarded.

Also, fix the existing `-a` option to work the way the documentation,
which says it should "Add or replace comment for given frame number",
describes it.  Namely, any existing comments for the packet are now
removed before the comment specified by the `-a` option is added.
This commit is contained in:
Niels Widger 2023-08-16 14:44:51 -04:00 committed by AndersBroman
parent f25421db6c
commit b0bf7a8e13
2 changed files with 39 additions and 1 deletions

View File

@ -33,6 +33,7 @@ editcap - Edit and/or translate the format of capture files
[ *--discard-all-secrets* ]
[ *--capture-comment* <comment> ]
[ *--discard-capture-comment* ]
[ *--discard-packet-comments* ]
__infile__
__outfile__
[ __packet#__[-__packet#__] ... ]
@ -473,6 +474,14 @@ As the unused bytes can be anything. When the packet traverses the device stack
for bonded interfaces on Linux for example.
--
--discard-packet-comments::
+
--
Discard all packet comments from the input file when writing the output
file. Does not discard comments added by *-a* in the same
command line.
--
include::diagnostic-options.adoc[]
== EXAMPLES

View File

@ -158,6 +158,7 @@ static gboolean skip_radiotap = FALSE;
static gboolean discard_all_secrets = FALSE;
static gboolean discard_cap_comments = FALSE;
static gboolean set_unused = FALSE;
static gboolean discard_pkt_comments = FALSE;
static int do_strict_time_adjustment = FALSE;
static struct time_adjustment strict_time_adj = {NSTIME_INIT_ZERO, 0}; /* strict time adjustment */
@ -903,6 +904,10 @@ print_usage(FILE *output)
fprintf(output, " when writing the output file. Does not discard\n");
fprintf(output, " comments added by \"--capture-comment\" in the same\n");
fprintf(output, " command line.\n");
fprintf(output, " --discard-packet-comments\n");
fprintf(output, " Discard all packet comments from the input file\n");
fprintf(output, " when writing the output file. Does not discard\n");
fprintf(output, " comments added by \"-a\" in the same command line.\n");
fprintf(output, "\n");
fprintf(output, "Miscellaneous:\n");
fprintf(output, " -h, --help display this help and exit.\n");
@ -1197,6 +1202,7 @@ main(int argc, char *argv[])
#define LONGOPT_CAPTURE_COMMENT LONGOPT_BASE_APPLICATION+6
#define LONGOPT_DISCARD_CAPTURE_COMMENT LONGOPT_BASE_APPLICATION+7
#define LONGOPT_SET_UNUSED LONGOPT_BASE_APPLICATION+8
#define LONGOPT_DISCARD_PACKET_COMMENTS LONGOPT_BASE_APPLICATION+9
static const struct ws_option long_options[] = {
{"novlan", ws_no_argument, NULL, LONGOPT_NO_VLAN},
@ -1209,6 +1215,7 @@ main(int argc, char *argv[])
{"capture-comment", ws_required_argument, NULL, LONGOPT_CAPTURE_COMMENT},
{"discard-capture-comment", ws_no_argument, NULL, LONGOPT_DISCARD_CAPTURE_COMMENT},
{"set-unused", ws_no_argument, NULL, LONGOPT_SET_UNUSED},
{"discard-packet-comments", ws_no_argument, NULL, LONGOPT_DISCARD_PACKET_COMMENTS},
{0, 0, 0, 0 }
};
@ -1393,6 +1400,12 @@ main(int argc, char *argv[])
break;
}
case LONGOPT_DISCARD_PACKET_COMMENTS:
{
discard_pkt_comments = TRUE;
break;
}
case 'a':
{
guint frame_number;
@ -2332,14 +2345,30 @@ main(int argc, char *argv[])
}
} /* random error mutation */
/* Discard all packet comments when writing */
if (discard_pkt_comments) {
temp_rec = *rec;
while (WTAP_OPTTYPE_SUCCESS == wtap_block_remove_nth_option_instance(rec->block, OPT_COMMENT, 0)) {
temp_rec.block_was_modified = TRUE;
continue;
}
rec = &temp_rec;
}
/* Find a packet comment we may need to write */
if (frames_user_comments) {
const char *comment =
(const char*)g_tree_lookup(frames_user_comments, GUINT_TO_POINTER(read_count));
/* XXX: What about comment changed to no comment? */
if (comment != NULL) {
/* Copy and change rather than modify returned rec */
temp_rec = *rec;
/* Erase any existing comments before adding the new one */
while (WTAP_OPTTYPE_SUCCESS == wtap_block_remove_nth_option_instance(rec->block, OPT_COMMENT, 0)) {
temp_rec.block_was_modified = TRUE;
continue;
}
/* The comment is not modified by dumper, cast away. */
wtap_block_add_string_option(rec->block, OPT_COMMENT, (char *)comment, strlen((char *)comment));
temp_rec.block_was_modified = TRUE;