diff options
author | Peter Wu <peter@lekensteyn.nl> | 2018-11-11 15:49:12 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2018-11-12 23:00:44 +0000 |
commit | 1e76e1355ab2cafba517a1ba556450ded397d885 (patch) | |
tree | 3a5f314fe1170be611080700655a7aab9f42fa17 | |
parent | 791a9a9b8e1ad1c5f2ac31fd5fb830f6b4d30135 (diff) |
wiretap: refactor common parameters for pcapng dump routines
Four variants of wtap_dump_open_ng exists, each of them take the same
three parameters for the SHB, IDB and NRB blocks that has to be written
before packets are even written. Similarly, a lot of tools always create
these arguments based on an existing capture file session (wth).
Address the former duplication by creating a new data structure to hold
the arguments. Address the second issue by creating new helper functions
to initialize the parameters based on a wth. This refactoring should
make it easier to add the new Decryption Secrets Block (DSB).
No functional change intended.
Change-Id: I42c019dc1d48a476773459212ca213de91a55684
Reviewed-on: https://code.wireshark.org/review/30578
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
-rw-r--r-- | debian/libwiretap0.symbols | 2 | ||||
-rw-r--r-- | editcap.c | 34 | ||||
-rw-r--r-- | file.c | 38 | ||||
-rw-r--r-- | reordercap.c | 25 | ||||
-rw-r--r-- | tshark.c | 49 | ||||
-rw-r--r-- | ui/tap_export_pdu.c | 8 | ||||
-rw-r--r-- | wiretap/file_access.c | 49 | ||||
-rw-r--r-- | wiretap/merge.c | 23 | ||||
-rw-r--r-- | wiretap/nettrace_3gpp_32_423.c | 6 | ||||
-rw-r--r-- | wiretap/wtap.c | 23 | ||||
-rw-r--r-- | wiretap/wtap.h | 85 |
11 files changed, 182 insertions, 160 deletions
diff --git a/debian/libwiretap0.symbols b/debian/libwiretap0.symbols index 0f7e397ab0..4a05225493 100644 --- a/debian/libwiretap0.symbols +++ b/debian/libwiretap0.symbols @@ -75,6 +75,8 @@ libwiretap.so.0 libwiretap0 #MINVER# wtap_dump_open_stdout_ng@Base 2.0.0 wtap_dump_open_tempfile@Base 2.0.0 wtap_dump_open_tempfile_ng@Base 2.0.0 + wtap_dump_params_cleanup@Base 2.9.0 + wtap_dump_params_init@Base 2.9.0 wtap_dump_set_addrinfo_list@Base 1.9.1 wtap_dump_supports_comment_types@Base 1.9.1 wtap_encap_requires_phdr@Base 1.9.1 @@ -936,9 +936,8 @@ failure_message_cont(const char *msg_format, va_list ap) static wtap_dumper * editcap_dump_open(const char *filename, guint32 snaplen, - GArray* shb_hdrs, - wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *write_err) + const wtapng_dump_params *ng_params, + int *write_err) { wtap_dumper *pdh; @@ -946,11 +945,11 @@ editcap_dump_open(const char *filename, guint32 snaplen, /* Write to the standard output. */ pdh = wtap_dump_open_stdout_ng(out_file_type_subtype, out_frame_type, snaplen, FALSE /* compressed */, - shb_hdrs, idb_inf, nrb_hdrs, write_err); + ng_params, write_err); } else { pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type, snaplen, FALSE /* compressed */, - shb_hdrs, idb_inf, nrb_hdrs, write_err); + ng_params, write_err); } return pdh; } @@ -998,9 +997,7 @@ real_main(int argc, char *argv[]) guint max_packet_number = 0; const wtap_rec *rec; wtap_rec temp_rec; - wtapng_iface_descriptions_t *idb_inf = NULL; - GArray *shb_hdrs = NULL; - GArray *nrb_hdrs = NULL; + wtapng_dump_params ng_params = WTAPNG_DUMP_PARAMS_INIT; char *shb_user_appl; gboolean do_mutation; guint32 caplen; @@ -1404,9 +1401,7 @@ real_main(int argc, char *argv[]) goto clean_exit; } - shb_hdrs = wtap_file_get_shb_for_new_file(wth); - idb_inf = wtap_file_get_idb_info(wth); - nrb_hdrs = wtap_file_get_nrb_for_new_file(wth); + wtap_dump_params_init(&ng_params, wth); /* * Now, process the rest, if any ... we only write if there is an extra @@ -1456,13 +1451,13 @@ real_main(int argc, char *argv[]) g_assert(filename); /* If we don't have an application name add Editcap */ - if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { - wtap_block_add_string_option_format(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION); + if (wtap_block_get_string_option_value(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { + wtap_block_add_string_option_format(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "Editcap " VERSION); } pdh = editcap_dump_open(filename, snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), - shb_hdrs, idb_inf, nrb_hdrs, &write_err); + &ng_params, &write_err); if (pdh == NULL) { cfile_dump_open_failure_message("editcap", filename, @@ -1504,7 +1499,7 @@ real_main(int argc, char *argv[]) pdh = editcap_dump_open(filename, snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), - shb_hdrs, idb_inf, nrb_hdrs, &write_err); + &ng_params, &write_err); if (pdh == NULL) { cfile_dump_open_failure_message("editcap", filename, @@ -1535,7 +1530,7 @@ real_main(int argc, char *argv[]) pdh = editcap_dump_open(filename, snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), - shb_hdrs, idb_inf, nrb_hdrs, &write_err); + &ng_params, &write_err); if (pdh == NULL) { cfile_dump_open_failure_message("editcap", filename, write_err, @@ -1907,7 +1902,7 @@ real_main(int argc, char *argv[]) pdh = editcap_dump_open(filename, snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth), - shb_hdrs, idb_inf, nrb_hdrs, &write_err); + &ng_params, &write_err); if (pdh == NULL) { cfile_dump_open_failure_message("editcap", filename, write_err, @@ -1942,9 +1937,8 @@ real_main(int argc, char *argv[]) } clean_exit: - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); - g_free(idb_inf); + g_free(ng_params.idb_inf); + wtap_dump_params_cleanup(&ng_params); if (wth != NULL) wtap_close(wth); wtap_cleanup(); @@ -4448,15 +4448,11 @@ cf_save_records(capture_file *cf, const char *fname, guint save_format, or moving the capture file, we have to do it by writing the packets out in Wiretap. */ - GArray *shb_hdrs = NULL; - wtapng_iface_descriptions_t *idb_inf = NULL; - GArray *nrb_hdrs = NULL; + wtapng_dump_params ng_params; int encap; - /* XXX: what free's this shb_hdr? */ - shb_hdrs = wtap_file_get_shb_for_new_file(cf->provider.wth); - idb_inf = wtap_file_get_idb_info(cf->provider.wth); - nrb_hdrs = wtap_file_get_nrb_for_new_file(cf->provider.wth); + /* XXX: what free's ng_params.shb_hdr? */ + wtap_dump_params_init(&ng_params, cf->provider.wth); /* Determine what file encapsulation type we should use. */ encap = wtap_dump_file_encap_type(cf->linktypes); @@ -4471,13 +4467,14 @@ cf_save_records(capture_file *cf, const char *fname, guint save_format, from which we're reading the packets that we're writing!) */ fname_new = g_strdup_printf("%s~", fname); pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap, - compressed, shb_hdrs, idb_inf, nrb_hdrs, &err); + compressed, &ng_params, &err); } else { pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap, - compressed, shb_hdrs, idb_inf, nrb_hdrs, &err); + compressed, &ng_params, &err); } - g_free(idb_inf); - idb_inf = NULL; + /* XXX idb_inf is documented to be used until wtap_dump_close. */ + g_free(ng_params.idb_inf); + ng_params.idb_inf = NULL; if (pdh == NULL) { cfile_dump_open_failure_alert_box(fname, err, save_format); @@ -4698,9 +4695,7 @@ cf_export_specified_packets(capture_file *cf, const char *fname, int err; wtap_dumper *pdh; save_callback_args_t callback_args; - GArray *shb_hdrs = NULL; - wtapng_iface_descriptions_t *idb_inf = NULL; - GArray *nrb_hdrs = NULL; + wtapng_dump_params ng_params; int encap; packet_range_process_init(range); @@ -4710,10 +4705,8 @@ cf_export_specified_packets(capture_file *cf, const char *fname, written, don't special-case the operation - read each packet and then write it out if it's one of the specified ones. */ - /* XXX: what free's this shb_hdr? */ - shb_hdrs = wtap_file_get_shb_for_new_file(cf->provider.wth); - idb_inf = wtap_file_get_idb_info(cf->provider.wth); - nrb_hdrs = wtap_file_get_nrb_for_new_file(cf->provider.wth); + /* XXX: what free's ng_params.shb_hdr? */ + wtap_dump_params_init(&ng_params, cf->provider.wth); /* Determine what file encapsulation type we should use. */ encap = wtap_dump_file_encap_type(cf->linktypes); @@ -4728,13 +4721,14 @@ cf_export_specified_packets(capture_file *cf, const char *fname, from which we're reading the packets that we're writing!) */ fname_new = g_strdup_printf("%s~", fname); pdh = wtap_dump_open_ng(fname_new, save_format, encap, cf->snap, - compressed, shb_hdrs, idb_inf, nrb_hdrs, &err); + compressed, &ng_params, &err); } else { pdh = wtap_dump_open_ng(fname, save_format, encap, cf->snap, - compressed, shb_hdrs, idb_inf, nrb_hdrs, &err); + compressed, &ng_params, &err); } - g_free(idb_inf); - idb_inf = NULL; + /* XXX idb_inf is documented to be used until wtap_dump_close. */ + g_free(ng_params.idb_inf); + ng_params.idb_inf = NULL; if (pdh == NULL) { cfile_dump_open_failure_alert_box(fname, err, save_format); diff --git a/reordercap.c b/reordercap.c index b9a9711e89..466579a171 100644 --- a/reordercap.c +++ b/reordercap.c @@ -177,9 +177,7 @@ main(int argc, char *argv[]) guint wrong_order_count = 0; gboolean write_output_regardless = TRUE; guint i; - GArray *shb_hdrs = NULL; - wtapng_iface_descriptions_t *idb_inf = NULL; - GArray *nrb_hdrs = NULL; + wtapng_dump_params ng_params; int ret = EXIT_SUCCESS; GPtrArray *frames; @@ -285,26 +283,23 @@ main(int argc, char *argv[]) } DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth)); - shb_hdrs = wtap_file_get_shb_for_new_file(wth); - idb_inf = wtap_file_get_idb_info(wth); - nrb_hdrs = wtap_file_get_nrb_for_new_file(wth); + wtap_dump_params_init(&ng_params, wth); /* Open outfile (same filetype/encap as input file) */ if (strcmp(outfile, "-") == 0) { pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth), - wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); + wtap_snapshot_length(wth), FALSE, &ng_params, &err); } else { pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth), - wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); + wtap_snapshot_length(wth), FALSE, &ng_params, &err); } - g_free(idb_inf); - idb_inf = NULL; + g_free(ng_params.idb_inf); + ng_params.idb_inf = NULL; if (pdh == NULL) { cfile_dump_open_failure_message("reordercap", outfile, err, wtap_file_type_subtype(wth)); - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); ret = OUTPUT_FILE_ERROR; goto clean_exit; } @@ -371,13 +366,11 @@ main(int argc, char *argv[]) /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { cfile_close_failure_message(outfile, err); - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); ret = OUTPUT_FILE_ERROR; goto clean_exit; } - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); /* Finally, close infile and release resources. */ wtap_close(wth); @@ -3096,9 +3096,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, gint64 data_offset; gboolean filtering_tap_listeners; guint tap_flags; - GArray *shb_hdrs = NULL; - wtapng_iface_descriptions_t *idb_inf = NULL; - GArray *nrb_hdrs = NULL; + wtapng_dump_params ng_params = WTAPNG_DUMP_PARAMS_INIT; wtap_rec rec; Buffer buf; epan_dissect_t *edt = NULL; @@ -3106,16 +3104,6 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, wtap_rec_init(&rec); - idb_inf = wtap_file_get_idb_info(cf->provider.wth); -#ifdef PCAP_NG_DEFAULT - if (idb_inf->interface_data->len > 1) { - linktype = WTAP_ENCAP_PER_PACKET; - } else { - linktype = wtap_file_encap(cf->provider.wth); - } -#else - linktype = wtap_file_encap(cf->provider.wth); -#endif if (save_file != NULL) { /* Set up to write to the capture file. */ snapshot_length = wtap_snapshot_length(cf->provider.wth); @@ -3125,13 +3113,21 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, } tshark_debug("tshark: snapshot_length = %d", snapshot_length); - shb_hdrs = wtap_file_get_shb_for_new_file(cf->provider.wth); - nrb_hdrs = wtap_file_get_nrb_for_new_file(cf->provider.wth); + wtap_dump_params_init(&ng_params, cf->provider.wth); +#ifdef PCAP_NG_DEFAULT + if (ng_params.idb_inf->interface_data->len > 1) { + linktype = WTAP_ENCAP_PER_PACKET; + } else { + linktype = wtap_file_encap(cf->provider.wth); + } +#else + linktype = wtap_file_encap(cf->provider.wth); +#endif /* If we don't have an application name add Tshark */ - if (wtap_block_get_string_option_value(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { + if (wtap_block_get_string_option_value(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, &shb_user_appl) != WTAP_OPTTYPE_SUCCESS) { /* this is free'd by wtap_block_free() later */ - wtap_block_add_string_option_format(g_array_index(shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "TShark (Wireshark) %s", get_ws_vcs_version_info()); + wtap_block_add_string_option_format(g_array_index(ng_params.shb_hdrs, wtap_block_t, 0), OPT_SHB_USERAPPL, "TShark (Wireshark) %s", get_ws_vcs_version_info()); } if (linktype != WTAP_ENCAP_PER_PACKET && @@ -3151,15 +3147,15 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, if (strcmp(save_file, "-") == 0) { /* Write to the standard output. */ pdh = wtap_dump_open_stdout_ng(out_file_type, linktype, - snapshot_length, FALSE /* compressed */, shb_hdrs, idb_inf, nrb_hdrs, &err); + snapshot_length, FALSE /* compressed */, &ng_params, &err); } else { pdh = wtap_dump_open_ng(save_file, out_file_type, linktype, - snapshot_length, FALSE /* compressed */, shb_hdrs, idb_inf, nrb_hdrs, &err); + snapshot_length, FALSE /* compressed */, &ng_params, &err); } } - g_free(idb_inf); - idb_inf = NULL; + g_free(ng_params.idb_inf); + ng_params.idb_inf = NULL; if (pdh == NULL) { /* We couldn't set up to write to the capture file. */ @@ -3176,8 +3172,6 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, goto out; } } - g_free(idb_inf); - idb_inf = NULL; pdh = NULL; } @@ -3327,8 +3321,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, err, err_info, framenum, out_file_type); wtap_dump_close(pdh, &err); - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); exit(2); } } @@ -3413,8 +3406,7 @@ process_cap_file(capture_file *cf, char *save_file, int out_file_type, cfile_write_failure_message("TShark", cf->filename, save_file, err, err_info, framenum, out_file_type); wtap_dump_close(pdh, &err); - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); exit(2); } } @@ -3503,8 +3495,7 @@ out: wtap_close(cf->provider.wth); cf->provider.wth = NULL; - wtap_block_array_free(shb_hdrs); - wtap_block_array_free(nrb_hdrs); + wtap_dump_params_cleanup(&ng_params); return success; } diff --git a/ui/tap_export_pdu.c b/ui/tap_export_pdu.c index 05e017f5a0..a6eea7d924 100644 --- a/ui/tap_export_pdu.c +++ b/ui/tap_export_pdu.c @@ -140,14 +140,18 @@ exp_pdu_open(exp_pdu_t *exp_pdu_tap_data, int fd, char *comment) exp_pdu_tap_data->shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t)); g_array_append_val(exp_pdu_tap_data->shb_hdrs, shb_hdr); + const wtapng_dump_params ng_params = { + .shb_hdrs = exp_pdu_tap_data->shb_hdrs, + .idb_inf = exp_pdu_tap_data->idb_inf, + }; if (fd == 1) { exp_pdu_tap_data->wdh = wtap_dump_open_stdout_ng(WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE_STANDARD, FALSE, - exp_pdu_tap_data->shb_hdrs, exp_pdu_tap_data->idb_inf, NULL, &err); + &ng_params, &err); } else { exp_pdu_tap_data->wdh = wtap_dump_fdopen_ng(fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, WTAP_MAX_PACKET_SIZE_STANDARD, FALSE, - exp_pdu_tap_data->shb_hdrs, exp_pdu_tap_data->idb_inf, NULL, &err); + &ng_params, &err); } if (exp_pdu_tap_data->wdh == NULL) { g_assert(err != 0); diff --git a/wiretap/file_access.c b/wiretap/file_access.c index 39e404d6a6..8a9ec42792 100644 --- a/wiretap/file_access.c +++ b/wiretap/file_access.c @@ -2233,12 +2233,12 @@ static int wtap_dump_file_close(wtap_dumper *wdh); static wtap_dumper * wtap_dump_init_dumper(int file_type_subtype, int encap, int snaplen, gboolean compressed, - GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err) + const wtapng_dump_params *ng_blocks, int *err) { wtap_dumper *wdh; wtap_block_t descr, file_int_data; wtapng_if_descr_mandatory_t *descr_mand, *file_int_data_mand; + GArray *interfaces = ng_blocks && ng_blocks->idb_inf ? ng_blocks->idb_inf->interface_data : NULL; /* Check whether we can open a capture file with that file type and that encapsulation. */ @@ -2251,17 +2251,17 @@ wtap_dump_init_dumper(int file_type_subtype, int encap, int snaplen, gboolean co return NULL; /* couldn't allocate it */ /* Set Section Header Block data */ - wdh->shb_hdrs = shb_hdrs; + wdh->shb_hdrs = ng_blocks ? ng_blocks->shb_hdrs : NULL; /* Set Name Resolution Block data */ - wdh->nrb_hdrs = nrb_hdrs; + wdh->nrb_hdrs = ng_blocks ? ng_blocks->nrb_hdrs : NULL; /* Set Interface Description Block data */ - if ((idb_inf != NULL) && (idb_inf->interface_data->len > 0)) { + if (interfaces && interfaces->len) { guint itf_count; /* Note: this memory is owned by wtap_dumper and will become * invalid after wtap_dump_close. */ - for (itf_count = 0; itf_count < idb_inf->interface_data->len; itf_count++) { - file_int_data = g_array_index(idb_inf->interface_data, wtap_block_t, itf_count); + for (itf_count = 0; itf_count < interfaces->len; itf_count++) { + file_int_data = g_array_index(interfaces, wtap_block_t, itf_count); file_int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(file_int_data); descr = wtap_block_create(WTAP_BLOCK_IF_DESCR); wtap_block_copy(descr, file_int_data); @@ -2308,20 +2308,18 @@ wtap_dumper * wtap_dump_open(const char *filename, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) { - return wtap_dump_open_ng(filename, file_type_subtype, encap,snaplen, compressed, NULL, NULL, NULL, err); + return wtap_dump_open_ng(filename, file_type_subtype, encap,snaplen, compressed, NULL, err); } wtap_dumper * wtap_dump_open_ng(const char *filename, int file_type_subtype, int encap, - int snaplen, gboolean compressed, GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err) + int snaplen, gboolean compressed, const wtapng_dump_params *ng_blocks, int *err) { wtap_dumper *wdh; WFILE_T fh; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, - shb_hdrs, idb_inf, nrb_hdrs, err); + wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, ng_blocks, err); if (wdh == NULL) return NULL; @@ -2352,16 +2350,15 @@ wtap_dump_open_tempfile(char **filenamep, const char *pfx, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) { - return wtap_dump_open_tempfile_ng(filenamep, pfx, file_type_subtype, encap,snaplen, compressed, NULL, NULL, NULL, err); + return wtap_dump_open_tempfile_ng(filenamep, pfx, file_type_subtype, encap,snaplen, compressed, NULL, err); } wtap_dumper * wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx, int file_type_subtype, int encap, int snaplen, gboolean compressed, - GArray* shb_hdrs, - wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err) + const wtapng_dump_params *ng_blocks, + int *err) { int fd; char *tmpname; @@ -2372,8 +2369,7 @@ wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx, *filenamep = NULL; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, - shb_hdrs, idb_inf, nrb_hdrs, err); + wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, ng_blocks, err); if (wdh == NULL) return NULL; @@ -2413,20 +2409,18 @@ wtap_dumper * wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) { - return wtap_dump_fdopen_ng(fd, file_type_subtype, encap, snaplen, compressed, NULL, NULL, NULL, err); + return wtap_dump_fdopen_ng(fd, file_type_subtype, encap, snaplen, compressed, NULL, err); } wtap_dumper * wtap_dump_fdopen_ng(int fd, int file_type_subtype, int encap, int snaplen, - gboolean compressed, GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err) + gboolean compressed, const wtapng_dump_params *ng_blocks, int *err) { wtap_dumper *wdh; WFILE_T fh; /* Allocate and initialize a data structure for the output stream. */ - wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, - shb_hdrs, idb_inf, nrb_hdrs, err); + wdh = wtap_dump_init_dumper(file_type_subtype, encap, snaplen, compressed, ng_blocks, err); if (wdh == NULL) return NULL; @@ -2453,14 +2447,12 @@ wtap_dumper * wtap_dump_open_stdout(int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err) { - return wtap_dump_open_stdout_ng(file_type_subtype, encap, snaplen, compressed, NULL, NULL, NULL, err); + return wtap_dump_open_stdout_ng(file_type_subtype, encap, snaplen, compressed, NULL, err); } wtap_dumper * wtap_dump_open_stdout_ng(int file_type_subtype, int encap, int snaplen, - gboolean compressed, GArray* shb_hdrs, - wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err) + gboolean compressed, const wtapng_dump_params *ng_blocks, int *err) { int new_fd; wtap_dumper *wdh; @@ -2491,8 +2483,7 @@ wtap_dump_open_stdout_ng(int file_type_subtype, int encap, int snaplen, } #endif - wdh = wtap_dump_fdopen_ng(new_fd, file_type_subtype, encap, snaplen, - compressed, shb_hdrs, idb_inf, nrb_hdrs, err); + wdh = wtap_dump_fdopen_ng(new_fd, file_type_subtype, encap, snaplen, compressed, ng_blocks, err); if (wdh == NULL) { /* Failed; close the new FD */ ws_close(new_fd); diff --git a/wiretap/merge.c b/wiretap/merge.c index 55ceed040c..e4cdfbb390 100644 --- a/wiretap/merge.c +++ b/wiretap/merge.c @@ -1015,9 +1015,13 @@ merge_files(const gchar* out_filename, const int file_type, idb_inf = generate_merged_idb(in_files, in_file_count, mode); merge_debug("merge_files: IDB merge operation complete, got %u IDBs", idb_inf ? idb_inf->interface_data->len : 0); + /* XXX other blocks like NRB are now discarded. */ + const wtapng_dump_params ng_params = { + .shb_hdrs = shb_hdrs, + .idb_inf = idb_inf, + }; pdh = wtap_dump_open_ng(out_filename, file_type, frame_type, snaplen, - FALSE /* compressed */, shb_hdrs, idb_inf, - NULL, err); + FALSE /* compressed */, &ng_params, err); } else { pdh = wtap_dump_open(out_filename, file_type, frame_type, snaplen, @@ -1118,10 +1122,15 @@ merge_files_to_tempfile(gchar **out_filenamep, const char *pfx, idb_inf = generate_merged_idb(in_files, in_file_count, mode); merge_debug("merge_files: IDB merge operation complete, got %u IDBs", idb_inf ? idb_inf->interface_data->len : 0); + /* XXX other blocks like NRB are now discarded. */ + const wtapng_dump_params ng_params = { + .shb_hdrs = shb_hdrs, + .idb_inf = idb_inf, + }; pdh = wtap_dump_open_tempfile_ng(out_filenamep, pfx, file_type, frame_type, snaplen, FALSE /* compressed */, - shb_hdrs, idb_inf, NULL, err); + &ng_params, err); } else { pdh = wtap_dump_open_tempfile(out_filenamep, pfx, file_type, frame_type, @@ -1217,9 +1226,13 @@ merge_files_to_stdout(const int file_type, const char *const *in_filenames, idb_inf = generate_merged_idb(in_files, in_file_count, mode); merge_debug("merge_files: IDB merge operation complete, got %u IDBs", idb_inf ? idb_inf->interface_data->len : 0); + /* XXX other blocks like NRB are now discarded. */ + const wtapng_dump_params ng_params = { + .shb_hdrs = shb_hdrs, + .idb_inf = idb_inf, + }; pdh = wtap_dump_open_stdout_ng(file_type, frame_type, snaplen, - FALSE /* compressed */, shb_hdrs, - idb_inf, NULL, err); + FALSE /* compressed */, &ng_params, err); } else { pdh = wtap_dump_open_stdout(file_type, frame_type, snaplen, diff --git a/wiretap/nettrace_3gpp_32_423.c b/wiretap/nettrace_3gpp_32_423.c index 52c6bf3a7c..c1c036b9ad 100644 --- a/wiretap/nettrace_3gpp_32_423.c +++ b/wiretap/nettrace_3gpp_32_423.c @@ -783,8 +783,12 @@ create_temp_pcapng_file(wtap *wth, int *err, gchar **err_info, nettrace_3gpp_32_ g_array_append_val(idb_inf->interface_data, int_data); + const wtapng_dump_params ng_params = { + .shb_hdrs = shb_hdrs, + .idb_inf = idb_inf, + }; wdh_exp_pdu = wtap_dump_fdopen_ng(import_file_fd, WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_ENCAP_WIRESHARK_UPPER_PDU, - WTAP_MAX_PACKET_SIZE_STANDARD, FALSE, shb_hdrs, idb_inf, NULL, &exp_pdu_file_err); + WTAP_MAX_PACKET_SIZE_STANDARD, FALSE, &ng_params, &exp_pdu_file_err); if (wdh_exp_pdu == NULL) { result = WTAP_OPEN_ERROR; goto end; diff --git a/wiretap/wtap.c b/wiretap/wtap.c index f43bf464ab..3b7ac5610d 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -315,6 +315,29 @@ wtap_file_get_nrb_for_new_file(wtap *wth) return nrb_hdrs; } +void +wtap_dump_params_init(wtapng_dump_params *params, wtap *wth) +{ + memset(params, 0, sizeof(*params)); + + if (wth == NULL) + return; + + params->shb_hdrs = wtap_file_get_shb_for_new_file(wth); + params->idb_inf = wtap_file_get_idb_info(wth); + params->nrb_hdrs = wtap_file_get_nrb_for_new_file(wth); +} + +void +wtap_dump_params_cleanup(wtapng_dump_params *params) +{ + wtap_block_array_free(params->shb_hdrs); + /* params->idb_inf is currently expected to be freed by the caller. */ + wtap_block_array_free(params->nrb_hdrs); + + memset(params, 0, sizeof(*params)); +} + /* Table of the encapsulation types we know about. */ struct encap_type_info { const char *name; diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 7cecb9b36d..99dec8876c 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1421,6 +1421,28 @@ typedef struct addrinfo_lists { GList *ipv6_addr_list; /**< A list of resolved hashipv6_t*/ } addrinfo_lists_t; +/** + * Parameters for various wtap_dump_*_ng functions, controlling additional + * blocks to be written. The structure itself is no longer used after returning + * from wtap_dump_*_ng, but its fields must remain valid until wtap_dump_close + * is called. + * + * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until + * wtap_dump_close() is called, but will not be free'd by the dumper. If + * you created them, you must free them yourself after wtap_dump_close(). + * + * @see wtapng_dump_params_init, wtapng_dump_params_cleanup. + */ +typedef struct wtapng_dump_params { + GArray *shb_hdrs; /**< The section header block(s) information, or NULL. */ + wtapng_iface_descriptions_t *idb_inf; /**< The interface description information, or NULL. */ + GArray *nrb_hdrs; /**< The name resolution blocks(s) comment/custom_opts information, or NULL. */ +} wtapng_dump_params; + +/* Zero-initializer for wtapng_dump_params. */ +#define WTAPNG_DUMP_PARAMS_INIT {.idb_inf=0} + + struct wtap_dumper; typedef struct wtap wtap; @@ -1850,6 +1872,25 @@ gboolean wtap_dump_has_name_resolution(int filetype); WS_DLL_PUBLIC gboolean wtap_dump_supports_comment_types(int filetype, guint32 comment_types); +/** + * Initialize the initial pcapng blocks based on an existing file. Its contents + * must be freed according to the requirements of wtapng_dump_params. + * + * @param params The parameters for wtap_dump_*_ng to initialize. + * @param wth The wiretap session. + */ +WS_DLL_PUBLIC +void wtap_dump_params_init(wtapng_dump_params *params, wtap *wth); + +/** + * Free memory associated with the wtapng_dump_params when it is no longer in + * use by wtap_dumper. + * + * @param params The parameters as initialized by wtapng_dump_params_init. + */ +WS_DLL_PUBLIC +void wtap_dump_params_cleanup(wtapng_dump_params *params); + WS_DLL_PUBLIC wtap_dumper* wtap_dump_open(const char *filename, int file_type_subtype, int encap, int snaplen, gboolean compressed, int *err); @@ -1857,25 +1898,18 @@ wtap_dumper* wtap_dump_open(const char *filename, int file_type_subtype, int enc /** * @brief Opens a new capture file for writing. * - * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until - * wtap_dump_close() is called, but will not be free'd by the dumper. If - * you created them, you must free them yourself after wtap_dump_close(). - * * @param filename The new file's name. * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) * @param snaplen The maximum packet capture length. * @param compressed True if file should be compressed. - * @param shb_hdrs The section header block(s) information, or NULL. - * @param idb_inf The interface description information, or NULL. - * @param nrb_hdrs The name resolution blocks(s) comment/custom_opts information, or NULL. + * @param ng_blocks The initial pcapng blocks to be written, or NULL for non-pcapng. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_ng(const char *filename, int file_type_subtype, int encap, - int snaplen, gboolean compressed, GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err); + int snaplen, gboolean compressed, const wtapng_dump_params *ng_blocks, int *err); WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, @@ -1885,10 +1919,6 @@ wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, /** * @brief Creates a dumper for a temporary file. * - * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until - * wtap_dump_close() is called, but will not be free'd by the dumper. If - * you created them, you must free them yourself after wtap_dump_close(). - * * @param filenamep Points to a pointer that's set to point to the * pathname of the temporary file; it's allocated with g_malloc() * @param pfx A string to be used as the prefix for the temporary file name @@ -1896,17 +1926,14 @@ wtap_dumper* wtap_dump_open_tempfile(char **filenamep, const char *pfx, * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) * @param snaplen The maximum packet capture length. * @param compressed True if file should be compressed. - * @param shb_hdrs The section header block(s) information, or NULL. - * @param idb_inf The interface description information, or NULL. - * @param nrb_hdrs The name resolution blocks(s) comment/custom_opts information, or NULL. + * @param ng_blocks The initial pcapng blocks to be written, or NULL for non-pcapng. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_tempfile_ng(char **filenamep, const char *pfx, int file_type_subtype, int encap, int snaplen, gboolean compressed, - GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err); + const wtapng_dump_params *ng_blocks, int *err); WS_DLL_PUBLIC wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snaplen, @@ -1915,25 +1942,18 @@ wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype, int encap, int snap /** * @brief Creates a dumper for an existing file descriptor. * - * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until - * wtap_dump_close() is called, but will not be free'd by the dumper. If - * you created them, you must free them yourself after wtap_dump_close(). - * * @param fd The file descriptor for which the dumper should be created. * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) * @param snaplen The maximum packet capture length. * @param compressed True if file should be compressed. - * @param shb_hdrs The section header block(s) information, or NULL. - * @param idb_inf The interface description information, or NULL. - * @param nrb_hdrs The name resolution blocks(s) comment/custom_opts information, or NULL. + * @param ng_blocks The initial pcapng blocks to be written, or NULL for non-pcapng. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_fdopen_ng(int fd, int file_type_subtype, int encap, int snaplen, - gboolean compressed, GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err); + gboolean compressed, const wtapng_dump_params *ng_blocks, int *err); WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_stdout(int file_type_subtype, int encap, int snaplen, @@ -1942,24 +1962,17 @@ wtap_dumper* wtap_dump_open_stdout(int file_type_subtype, int encap, int snaplen /** * @brief Creates a dumper for the standard output. * - * @note The shb_hdr, idb_inf, and nrb_hdr arguments will be used until - * wtap_dump_close() is called, but will not be free'd by the dumper. If - * you created them, you must free them yourself after wtap_dump_close(). - * * @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type. * @param encap The WTAP_ENCAP_XXX encapsulation type (WTAP_ENCAP_PER_PACKET for multi) * @param snaplen The maximum packet capture length. * @param compressed True if file should be compressed. - * @param shb_hdrs The section header block(s) information, or NULL. - * @param idb_inf The interface description information, or NULL. - * @param nrb_hdrs The name resolution blocks(s) comment/custom_opts information, or NULL. + * @param ng_blocks The initial pcapng blocks to be written, or NULL for non-pcapng. * @param[out] err Will be set to an error code on failure. * @return The newly created dumper object, or NULL on failure. */ WS_DLL_PUBLIC wtap_dumper* wtap_dump_open_stdout_ng(int file_type_subtype, int encap, int snaplen, - gboolean compressed, GArray* shb_hdrs, wtapng_iface_descriptions_t *idb_inf, - GArray* nrb_hdrs, int *err); + gboolean compressed, const wtapng_dump_params *ng_blocks, int *err); WS_DLL_PUBLIC gboolean wtap_dump(wtap_dumper *, const wtap_rec *, const guint8 *, |