From d18e1f19e9de19a76647f4df46b83a60be47e785 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 24 Mar 2021 23:36:29 -0700 Subject: [PATCH] Add dumpcap options to set the name and description for a capture source. Add --ifname and --ifdescr to allow the name and description for an interface or pipe to be set; this overrides the specified name or reported description for an interface, and overrides the pipe path name and provides a description for a pipe. Provide those arguments when capturing from an extcap program. This is mainly for extcaps, so you have something more meaningful than some random path name as the interface name and something descriptive for the description. --- capchild/capture_sync.c | 9 +++++++ capture_opts.c | 3 +++ capture_opts.h | 1 + doc/dumpcap.pod | 12 +++++++++ dumpcap.c | 55 +++++++++++++++++++++++++++++++++++++++-- 5 files changed, 78 insertions(+), 2 deletions(-) diff --git a/capchild/capture_sync.c b/capchild/capture_sync.c index f31914886a..937e411ed7 100644 --- a/capchild/capture_sync.c +++ b/capchild/capture_sync.c @@ -342,6 +342,15 @@ sync_pipe_start(capture_options *capture_opts, capture_session *cap_session, inf #else argv = sync_pipe_add_arg(argv, &argc, interface_opts->extcap_fifo); #endif + /* Add a name for the interface, to put into an IDB. */ + argv = sync_pipe_add_arg(argv, &argc, "--ifname"); + argv = sync_pipe_add_arg(argv, &argc, interface_opts->name); + if (interface_opts->descr != NULL) + { + /* Add a description for the interface, to put into an IDB. */ + argv = sync_pipe_add_arg(argv, &argc, "--ifdescr"); + argv = sync_pipe_add_arg(argv, &argc, interface_opts->descr); + } } else argv = sync_pipe_add_arg(argv, &argc, interface_opts->name); diff --git a/capture_opts.c b/capture_opts.c index 48a7a81b1d..ac08fcf687 100644 --- a/capture_opts.c +++ b/capture_opts.c @@ -563,6 +563,7 @@ fill_in_interface_opts_from_ifinfo(interface_options *interface_opts, interface_opts->descr = NULL; interface_opts->display_name = g_strdup(if_info->name); } + interface_opts->ifname = NULL; interface_opts->if_type = if_info->type; interface_opts->extcap = g_strdup(if_info->extcap); } @@ -740,6 +741,7 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg_str interface_opts.descr = NULL; interface_opts.hardware = NULL; interface_opts.display_name = g_strdup(optarg_str_p); + interface_opts.ifname = NULL; interface_opts.if_type = capture_opts->default_options.if_type; interface_opts.extcap = g_strdup(capture_opts->default_options.extcap); } @@ -1206,6 +1208,7 @@ capture_opts_del_iface(capture_options *capture_opts, guint if_index) g_free(interface_opts->descr); g_free(interface_opts->hardware); g_free(interface_opts->display_name); + g_free(interface_opts->ifname); g_free(interface_opts->cfilter); g_free(interface_opts->timestamp_type); g_free(interface_opts->extcap); diff --git a/capture_opts.h b/capture_opts.h index 948b343d1f..825c3256fa 100644 --- a/capture_opts.h +++ b/capture_opts.h @@ -197,6 +197,7 @@ typedef struct interface_options_tag { gchar *descr; /* a more user-friendly description of the interface; may be NULL if none */ gchar *hardware; /* description of the hardware */ gchar *display_name; /* the name displayed in the console and title bar */ + gchar *ifname; /* if not null, name to use instead of the interface naem in IDBs */ gchar *cfilter; gboolean has_snaplen; int snaplen; diff --git a/doc/dumpcap.pod b/doc/dumpcap.pod index 2ade8310e1..19d16c5dfd 100644 --- a/doc/dumpcap.pod +++ b/doc/dumpcap.pod @@ -29,6 +29,8 @@ S<[ B<-M> ]> S<[ B<-n> ]> S<[ B<-N> Epacket limitE ]> S<[ B<-p>|B<--no-promiscuous-mode> ]> +S<[ B<--ifdescr> EdescriptionE ]> +S<[ B<--ifname> EnameE ]> S<[ B<-P> ]> S<[ B<-q> ]> S<[ B<-s>|B<--snapshot-length> Ecapture snaplenE ]> @@ -248,6 +250,16 @@ endianness as the capturing host. This option can occur multiple times. When capturing from multiple interfaces, the capture file will be saved in pcapng format. +=item --ifdescr> EdescriptionE + +Use I as the description in the capture file for the +interface or pipe specified before it with B<-i>. + +=item --ifname> EnameE + +Use I as the name in the capture file for the the interface or +pipe specified before it with B<-i>. + =item -I|--monitor-mode Put the interface in "monitor mode"; this is supported only on IEEE diff --git a/dumpcap.c b/dumpcap.c index 19cf9f3818..609efa488a 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -367,6 +367,11 @@ print_usage(FILE *output) " or for remote capturing, use one of these formats:\n" " rpcap:///\n" " TCP@:\n"); + fprintf(output, " --ifname name to use in the capture file for a pipe from which\n"); + fprintf(output, " we're capturing\n"); + fprintf(output, " --ifdescr \n"); + fprintf(output, " description to use in the capture file for a pipe\n"); + fprintf(output, " from which we're capturing\n"); fprintf(output, " -f packet filter in libpcap filter syntax\n"); fprintf(output, " -s , --snapshot-length \n"); #ifdef HAVE_PCAP_CREATE @@ -3146,8 +3151,8 @@ capture_loop_init_pcapng_output(capture_options *capture_opts, loop_data *ld, pcap_src->snaplen = pcap_snapshot(pcap_src->pcap_h); } successful = pcapng_write_interface_description_block(global_ld.pdh, - NULL, /* OPT_COMMENT 1 */ - interface_opts->name, /* IDB_NAME 2 */ + NULL, /* OPT_COMMENT 1 */ + (interface_opts->ifname != NULL) ? interface_opts->ifname : interface_opts->name, /* IDB_NAME 2 */ interface_opts->descr, /* IDB_DESCRIPTION 3 */ interface_opts->cfilter, /* IDB_FILTER 11 */ os_info_str->str, /* IDB_OS 12 */ @@ -4820,6 +4825,9 @@ get_dumpcap_runtime_info(GString *str) get_runtime_caplibs_version(str); } +#define LONGOPT_IFNAME LONGOPT_BASE_APPLICATION+1 +#define LONGOPT_IFDESCR LONGOPT_BASE_APPLICATION+2 + /* And now our feature presentation... [ fade to music ] */ int main(int argc, char *argv[]) @@ -4830,6 +4838,8 @@ main(int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, LONGOPT_CAPTURE_COMMON + {"ifname", required_argument, NULL, LONGOPT_IFNAME}, + {"ifdescr", required_argument, NULL, LONGOPT_IFDESCR}, {0, 0, 0, 0 } }; @@ -5190,6 +5200,28 @@ main(int argc, char *argv[]) } break; /*** hidden option: Wireshark child mode (using binary output messages) ***/ + case LONGOPT_IFNAME: + if (global_capture_opts.ifaces->len > 0) { + interface_options *interface_opts; + + interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, global_capture_opts.ifaces->len - 1); + interface_opts->ifname = g_strdup(optarg); + } else { + cmdarg_err("--ifname must be specified after a -i option"); + exit_main(1); + } + break; + case LONGOPT_IFDESCR: + if (global_capture_opts.ifaces->len > 0) { + interface_options *interface_opts; + + interface_opts = &g_array_index(global_capture_opts.ifaces, interface_options, global_capture_opts.ifaces->len - 1); + interface_opts->descr = g_strdup(optarg); + } else { + cmdarg_err("--ifdescr must be specified after a -i option"); + exit_main(1); + } + break; case 'Z': capture_child = TRUE; #ifdef _WIN32 @@ -5530,6 +5562,25 @@ main(int argc, char *argv[]) g_string_append_printf(str, "and "); } } + if (interface_opts->ifname != NULL) { + /* + * Re-generate the display name based on the strins + * we were handed. + */ + g_free(interface_opts->display_name); + if (interface_opts->descr != NULL) { +#ifdef _WIN32 + interface_opts->display_name = g_strdup_printf("%s", + interface_opts->descr); +#else + interface_opts->display_name = g_strdup_printf("%s: %s", + interface_opts->descr, interface_opts->ifname); +#endif + } else { + interface_opts->display_name = g_strdup_printf("%s", + interface_opts->ifname); + } + } g_string_append_printf(str, "'%s'", interface_opts->display_name); } } else {