wiretap: remove unused code, drop number_of_interfaces

While investigating an ASAN issue (fixed in
commit dcdd076ab0), I got greatly confused
by three different types having the same "interface_data" field name:

 * pcapng_t *pn stores an array of interface_data_t objects.
 * wtap *wth stores an array of wtapng_if_descr_t objects.
 * pcapng_dump_t should store an array of interface_data_t objects.

pcapng_dump_t and friends are unused since
commit c7f1a431d2, so drop it.

To fix the confusion, rename the interface_data_t type to
interface_info_t type and use the local variable "iface_info"
everywhere. Rename interface_data of pcapng_t to "interfaces" and
add a comment what this exactly means (interfaces listed in the capture
file).

Drop the number_of_interfaces field for interfaces as the array
length is already available from GArray. Now interface_data is always
initialized for wth (which also gets copied to idb).

s/int/guint/g and replace cast at some places.

There are no regressions for the in-tree test suite.

Change-Id: I2d5985c9f1e43f8230dbb4a73bd1e243c4858170
Reviewed-on: https://code.wireshark.org/review/1656
Reviewed-by: Evan Huus <eapache@gmail.com>
Tested-by: Evan Huus <eapache@gmail.com>
Reviewed-by: Michael Mann <mmann78@netscape.net>
This commit is contained in:
Peter Wu 2014-05-15 23:39:12 +02:00 committed by Michael Mann
parent 9fe221a42f
commit 3aee917058
12 changed files with 70 additions and 183 deletions

View File

@ -38,7 +38,7 @@ cap_file_get_interface_name(void *data, guint32 interface_id)
idb_info = wtap_file_get_idb_info(cf->wth); idb_info = wtap_file_get_idb_info(cf->wth);
if (interface_id < idb_info->number_of_interfaces) if (interface_id < idb_info->interface_data->len)
wtapng_if_descr = &g_array_index(idb_info->interface_data, wtapng_if_descr_t, interface_id); wtapng_if_descr = &g_array_index(idb_info->interface_data, wtapng_if_descr_t, interface_id);
g_free(idb_info); g_free(idb_info);

2
file.c
View File

@ -1382,7 +1382,7 @@ cf_merge_files(char **out_filenamep, int in_file_count,
/* create fake IDB info */ /* create fake IDB info */
idb_inf = g_new(wtapng_iface_descriptions_t,1); idb_inf = g_new(wtapng_iface_descriptions_t,1);
idb_inf->number_of_interfaces = in_file_count; /* TODO make this the number of DIFFERENT encapsulation types /* TODO make this the number of DIFFERENT encapsulation types
* check that snaplength is the same too? * check that snaplength is the same too?
*/ */
idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t)); idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));

View File

@ -172,7 +172,7 @@ summary_fill_in(capture_file *cf, summary_tally *st)
st->ifaces = g_array_new(FALSE, FALSE, sizeof(iface_options)); st->ifaces = g_array_new(FALSE, FALSE, sizeof(iface_options));
idb_info = wtap_file_get_idb_info(cf->wth); idb_info = wtap_file_get_idb_info(cf->wth);
for (i = 0; i < idb_info->number_of_interfaces; i++) { for (i = 0; i < idb_info->interface_data->len; i++) {
wtapng_if_descr = g_array_index(idb_info->interface_data, wtapng_if_descr_t, i); wtapng_if_descr = g_array_index(idb_info->interface_data, wtapng_if_descr_t, i);
iface.cfilter = g_strdup(wtapng_if_descr.if_filter_str); iface.cfilter = g_strdup(wtapng_if_descr.if_filter_str);
iface.name = g_strdup(wtapng_if_descr.if_name); iface.name = g_strdup(wtapng_if_descr.if_name);

View File

@ -3080,7 +3080,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
shb_hdr = wtap_file_get_shb_info(cf->wth); shb_hdr = wtap_file_get_shb_info(cf->wth);
idb_inf = wtap_file_get_idb_info(cf->wth); idb_inf = wtap_file_get_idb_info(cf->wth);
#ifdef PCAP_NG_DEFAULT #ifdef PCAP_NG_DEFAULT
if (idb_inf->number_of_interfaces > 1) { if (idb_inf->interface_data->len > 1) {
linktype = WTAP_ENCAP_PER_PACKET; linktype = WTAP_ENCAP_PER_PACKET;
} else { } else {
linktype = wtap_file_encap(cf->wth); linktype = wtap_file_encap(cf->wth);

View File

@ -493,7 +493,6 @@ file_import_open(text_import_info_t *info)
/* Create fake IDB info */ /* Create fake IDB info */
idb_inf = g_new(wtapng_iface_descriptions_t,1); idb_inf = g_new(wtapng_iface_descriptions_t,1);
idb_inf->number_of_interfaces = 1;
idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t)); idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
/* create the fake interface data */ /* create the fake interface data */

View File

@ -120,7 +120,6 @@ exp_pdu_file_open(exp_pdu_t *exp_pdu_tap_data)
/* Create fake IDB info */ /* Create fake IDB info */
idb_inf = g_new(wtapng_iface_descriptions_t,1); idb_inf = g_new(wtapng_iface_descriptions_t,1);
idb_inf->number_of_interfaces = 1;
idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t)); idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
/* create the fake interface data */ /* create the fake interface data */

View File

@ -726,10 +726,6 @@ int erf_populate_interfaces(wtap *wth)
if (!wth) if (!wth)
return -1; return -1;
if (!wth->interface_data) {
wth->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
}
memset(&int_data, 0, sizeof(int_data)); /* Zero all fields */ memset(&int_data, 0, sizeof(int_data)); /* Zero all fields */
int_data.wtap_encap = WTAP_ENCAP_ERF; int_data.wtap_encap = WTAP_ENCAP_ERF;
@ -762,7 +758,6 @@ int erf_populate_interfaces(wtap *wth)
int_data.if_description = g_strdup_printf("ERF Interface Id %d (Port %c)", i, 'A'+i); int_data.if_description = g_strdup_printf("ERF Interface Id %d (Port %c)", i, 'A'+i);
g_array_append_val(wth->interface_data, int_data); g_array_append_val(wth->interface_data, int_data);
wth->number_of_interfaces++;
} }
return 0; return 0;

View File

@ -798,6 +798,11 @@ wtap* wtap_open_offline(const char *filename, unsigned int type, int *err, char
wth->priv = NULL; wth->priv = NULL;
wth->wslua_data = NULL; wth->wslua_data = NULL;
/* Initialize the array containing a list of interfaces. pcapng_open and
* erf_open needs this (and libpcap_open for ERF encapsulation types).
* Always initing it here saves checking for a NULL ptr later. */
wth->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
if (wth->random_fh) { if (wth->random_fh) {
wth->fast_seek = g_ptr_array_new(); wth->fast_seek = g_ptr_array_new();
@ -1021,8 +1026,6 @@ success:
descr.if_fcslen = -1; descr.if_fcslen = -1;
descr.num_stat_entries = 0; /* Number of ISB:s */ descr.num_stat_entries = 0; /* Number of ISB:s */
descr.interface_statistics = NULL; descr.interface_statistics = NULL;
wth->number_of_interfaces= 1;
wth->interface_data= g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
g_array_append_val(wth->interface_data, descr); g_array_append_val(wth->interface_data, descr);
} }
@ -1984,8 +1987,7 @@ wtap_dump_init_dumper(int file_type_subtype, int encap, int snaplen, gboolean co
/* Set Section Header Block data */ /* Set Section Header Block data */
wdh->shb_hdr = shb_hdr; wdh->shb_hdr = shb_hdr;
/* Set Interface Description Block data */ /* Set Interface Description Block data */
if ((idb_inf != NULL) && (idb_inf->number_of_interfaces > 0)) { if ((idb_inf != NULL) && (idb_inf->interface_data->len > 0)) {
wdh->number_of_interfaces = idb_inf->number_of_interfaces;
wdh->interface_data = idb_inf->interface_data; wdh->interface_data = idb_inf->interface_data;
} else { } else {
wtapng_if_descr_t descr; wtapng_if_descr_t descr;
@ -2006,8 +2008,6 @@ wtap_dump_init_dumper(int file_type_subtype, int encap, int snaplen, gboolean co
descr.if_fcslen = -1; descr.if_fcslen = -1;
descr.num_stat_entries = 0; /* Number of ISB:s */ descr.num_stat_entries = 0; /* Number of ISB:s */
descr.interface_statistics = NULL; descr.interface_statistics = NULL;
wdh->number_of_interfaces= 1;
wdh->interface_data= g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
g_array_append_val(wdh->interface_data, descr); g_array_append_val(wdh->interface_data, descr);
} }
return wdh; return wdh;

View File

@ -358,19 +358,18 @@ typedef struct wtapng_block_s {
} wtapng_block_t; } wtapng_block_t;
/* Interface data in private struct */ /* Interface data in private struct */
typedef struct interface_data_s { typedef struct interface_info_s {
int wtap_encap; int wtap_encap;
guint32 snap_len; guint32 snap_len;
guint64 time_units_per_second; guint64 time_units_per_second;
} interface_data_t; } interface_info_t;
typedef struct { typedef struct {
gboolean shb_read; /**< Set when first SHB read, second read will fail */ gboolean shb_read; /**< Set when first SHB read, second read will fail */
gboolean byte_swapped; gboolean byte_swapped;
guint16 version_major; guint16 version_major;
guint16 version_minor; guint16 version_minor;
GArray *interface_data; GArray *interfaces; /**< Interfaces found in the capture file. */
guint number_of_interfaces;
gint8 if_fcslen; gint8 if_fcslen;
wtap_new_ipv4_callback_t add_new_ipv4; wtap_new_ipv4_callback_t add_new_ipv4;
wtap_new_ipv6_callback_t add_new_ipv6; wtap_new_ipv6_callback_t add_new_ipv6;
@ -944,7 +943,7 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
wtapng_packet_t packet; wtapng_packet_t packet;
guint32 block_total_length; guint32 block_total_length;
guint32 padding; guint32 padding;
interface_data_t int_data; interface_info_t iface_info;
guint64 ts; guint64 ts;
pcapng_option_header_t oh; pcapng_option_header_t oh;
int pseudo_header_len; int pseudo_header_len;
@ -1101,28 +1100,28 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
packet.cap_len, packet.cap_len,
packet.interface_id); packet.interface_id);
if (packet.interface_id >= pn->number_of_interfaces) { if (packet.interface_id >= pn->interfaces->len) {
*err = WTAP_ERR_BAD_FILE; *err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup_printf("pcapng: interface index %u is not less than interface count %u", *err_info = g_strdup_printf("pcapng: interface index %u is not less than interface count %u",
packet.interface_id, pn->number_of_interfaces); packet.interface_id, pn->interfaces->len);
return 0; return 0;
} }
int_data = g_array_index(pn->interface_data, interface_data_t, iface_info = g_array_index(pn->interfaces, interface_info_t,
packet.interface_id); packet.interface_id);
wblock->packet_header->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID; wblock->packet_header->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
pcapng_debug3("pcapng_read_packet_block: encapsulation = %d (%s), pseudo header size = %d.", pcapng_debug3("pcapng_read_packet_block: encapsulation = %d (%s), pseudo header size = %d.",
int_data.wtap_encap, iface_info.wtap_encap,
wtap_encap_string(int_data.wtap_encap), wtap_encap_string(iface_info.wtap_encap),
pcap_get_phdr_size(int_data.wtap_encap, &wblock->packet_header->pseudo_header)); pcap_get_phdr_size(iface_info.wtap_encap, &wblock->packet_header->pseudo_header));
wblock->packet_header->interface_id = packet.interface_id; wblock->packet_header->interface_id = packet.interface_id;
wblock->packet_header->pkt_encap = int_data.wtap_encap; wblock->packet_header->pkt_encap = iface_info.wtap_encap;
memset((void *)&wblock->packet_header->pseudo_header, 0, sizeof(union wtap_pseudo_header)); memset((void *)&wblock->packet_header->pseudo_header, 0, sizeof(union wtap_pseudo_header));
pseudo_header_len = pcap_process_pseudo_header(fh, pseudo_header_len = pcap_process_pseudo_header(fh,
WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
int_data.wtap_encap, iface_info.wtap_encap,
packet.cap_len, packet.cap_len,
TRUE, TRUE,
wblock->packet_header, wblock->packet_header,
@ -1132,7 +1131,7 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
return 0; return 0;
} }
block_read += pseudo_header_len; block_read += pseudo_header_len;
if (pseudo_header_len != pcap_get_phdr_size(int_data.wtap_encap, &wblock->packet_header->pseudo_header)) { if (pseudo_header_len != pcap_get_phdr_size(iface_info.wtap_encap, &wblock->packet_header->pseudo_header)) {
pcapng_debug1("pcapng_read_packet_block: Could only read %d bytes for pseudo header.", pcapng_debug1("pcapng_read_packet_block: Could only read %d bytes for pseudo header.",
pseudo_header_len); pseudo_header_len);
} }
@ -1141,8 +1140,8 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
/* Combine the two 32-bit pieces of the timestamp into one 64-bit value */ /* Combine the two 32-bit pieces of the timestamp into one 64-bit value */
ts = (((guint64)packet.ts_high) << 32) | ((guint64)packet.ts_low); ts = (((guint64)packet.ts_high) << 32) | ((guint64)packet.ts_low);
wblock->packet_header->ts.secs = (time_t)(ts / int_data.time_units_per_second); wblock->packet_header->ts.secs = (time_t)(ts / iface_info.time_units_per_second);
wblock->packet_header->ts.nsecs = (int)(((ts % int_data.time_units_per_second) * 1000000000) / int_data.time_units_per_second); wblock->packet_header->ts.nsecs = (int)(((ts % iface_info.time_units_per_second) * 1000000000) / iface_info.time_units_per_second);
/* "(Enhanced) Packet Block" read capture data */ /* "(Enhanced) Packet Block" read capture data */
errno = WTAP_ERR_CANT_READ; errno = WTAP_ERR_CANT_READ;
@ -1263,7 +1262,7 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
g_free(option_content); g_free(option_content);
pcap_read_post_process(WTAP_FILE_TYPE_SUBTYPE_PCAPNG, int_data.wtap_encap, pcap_read_post_process(WTAP_FILE_TYPE_SUBTYPE_PCAPNG, iface_info.wtap_encap,
wblock->packet_header, buffer_start_ptr(wblock->frame_buffer), wblock->packet_header, buffer_start_ptr(wblock->frame_buffer),
pn->byte_swapped, fcslen); pn->byte_swapped, fcslen);
return block_read; return block_read;
@ -1276,7 +1275,7 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
int bytes_read; int bytes_read;
guint block_read; guint block_read;
guint64 file_offset64; guint64 file_offset64;
interface_data_t int_data; interface_info_t iface_info;
pcapng_simple_packet_block_t spb; pcapng_simple_packet_block_t spb;
wtapng_simple_packet_t simple_packet; wtapng_simple_packet_t simple_packet;
guint32 block_total_length; guint32 block_total_length;
@ -1321,12 +1320,12 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
} }
block_read = bytes_read; block_read = bytes_read;
if (0 >= pn->number_of_interfaces) { if (0 >= pn->interfaces->len) {
*err = WTAP_ERR_BAD_FILE; *err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup_printf("pcapng: SPB appeared before any IDBs"); *err_info = g_strdup_printf("pcapng: SPB appeared before any IDBs");
return 0; return 0;
} }
int_data = g_array_index(pn->interface_data, interface_data_t, 0); iface_info = g_array_index(pn->interfaces, interface_info_t, 0);
if (pn->byte_swapped) { if (pn->byte_swapped) {
simple_packet.packet_len = GUINT32_SWAP_LE_BE(spb.packet_len); simple_packet.packet_len = GUINT32_SWAP_LE_BE(spb.packet_len);
@ -1340,8 +1339,8 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
* IDB and the packet length, as per the pcap-ng spec. * IDB and the packet length, as per the pcap-ng spec.
*/ */
simple_packet.cap_len = simple_packet.packet_len; simple_packet.cap_len = simple_packet.packet_len;
if (simple_packet.cap_len > int_data.snap_len) if (simple_packet.cap_len > iface_info.snap_len)
simple_packet.cap_len = int_data.snap_len; simple_packet.cap_len = iface_info.snap_len;
/* /*
* How much padding is there at the end of the packet data? * How much padding is there at the end of the packet data?
@ -1386,12 +1385,12 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
simple_packet.packet_len); simple_packet.packet_len);
pcapng_debug1("pcapng_read_simple_packet_block: Need to read pseudo header of size %d", pcapng_debug1("pcapng_read_simple_packet_block: Need to read pseudo header of size %d",
pcap_get_phdr_size(int_data.wtap_encap, &wblock->packet_header->pseudo_header)); pcap_get_phdr_size(iface_info.wtap_encap, &wblock->packet_header->pseudo_header));
/* No time stamp in a simple packet block; no options, either */ /* No time stamp in a simple packet block; no options, either */
wblock->packet_header->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID; wblock->packet_header->presence_flags = WTAP_HAS_CAP_LEN|WTAP_HAS_INTERFACE_ID;
wblock->packet_header->interface_id = 0; wblock->packet_header->interface_id = 0;
wblock->packet_header->pkt_encap = int_data.wtap_encap; wblock->packet_header->pkt_encap = iface_info.wtap_encap;
wblock->packet_header->ts.secs = 0; wblock->packet_header->ts.secs = 0;
wblock->packet_header->ts.nsecs = 0; wblock->packet_header->ts.nsecs = 0;
wblock->packet_header->interface_id = 0; wblock->packet_header->interface_id = 0;
@ -1402,7 +1401,7 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
memset((void *)&wblock->packet_header->pseudo_header, 0, sizeof(union wtap_pseudo_header)); memset((void *)&wblock->packet_header->pseudo_header, 0, sizeof(union wtap_pseudo_header));
pseudo_header_len = pcap_process_pseudo_header(fh, pseudo_header_len = pcap_process_pseudo_header(fh,
WTAP_FILE_TYPE_SUBTYPE_PCAPNG, WTAP_FILE_TYPE_SUBTYPE_PCAPNG,
int_data.wtap_encap, iface_info.wtap_encap,
simple_packet.cap_len, simple_packet.cap_len,
TRUE, TRUE,
wblock->packet_header, wblock->packet_header,
@ -1414,7 +1413,7 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
wblock->packet_header->caplen = simple_packet.cap_len - pseudo_header_len; wblock->packet_header->caplen = simple_packet.cap_len - pseudo_header_len;
wblock->packet_header->len = simple_packet.packet_len - pseudo_header_len; wblock->packet_header->len = simple_packet.packet_len - pseudo_header_len;
block_read += pseudo_header_len; block_read += pseudo_header_len;
if (pseudo_header_len != pcap_get_phdr_size(int_data.wtap_encap, &wblock->packet_header->pseudo_header)) { if (pseudo_header_len != pcap_get_phdr_size(iface_info.wtap_encap, &wblock->packet_header->pseudo_header)) {
pcapng_debug1("pcapng_read_simple_packet_block: Could only read %d bytes for pseudo header.", pcapng_debug1("pcapng_read_simple_packet_block: Could only read %d bytes for pseudo header.",
pseudo_header_len); pseudo_header_len);
} }
@ -1439,7 +1438,7 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
block_read += 4 - (simple_packet.cap_len % 4); block_read += 4 - (simple_packet.cap_len % 4);
} }
pcap_read_post_process(WTAP_FILE_TYPE_SUBTYPE_PCAPNG, int_data.wtap_encap, pcap_read_post_process(WTAP_FILE_TYPE_SUBTYPE_PCAPNG, iface_info.wtap_encap,
wblock->packet_header, buffer_start_ptr(wblock->frame_buffer), wblock->packet_header, buffer_start_ptr(wblock->frame_buffer),
pn->byte_swapped, pn->if_fcslen); pn->byte_swapped, pn->if_fcslen);
return block_read; return block_read;
@ -2099,7 +2098,7 @@ static void
pcapng_process_idb(wtap *wth, pcapng_t *pcapng, wtapng_block_t *wblock) pcapng_process_idb(wtap *wth, pcapng_t *pcapng, wtapng_block_t *wblock)
{ {
wtapng_if_descr_t int_data; wtapng_if_descr_t int_data;
interface_data_t interface_data; interface_info_t iface_info;
int_data.wtap_encap = wblock->data.if_descr.wtap_encap; int_data.wtap_encap = wblock->data.if_descr.wtap_encap;
int_data.time_units_per_second = wblock->data.if_descr.time_units_per_second; int_data.time_units_per_second = wblock->data.if_descr.time_units_per_second;
@ -2127,14 +2126,12 @@ pcapng_process_idb(wtap *wth, pcapng_t *pcapng, wtapng_block_t *wblock)
int_data.interface_statistics = NULL; int_data.interface_statistics = NULL;
g_array_append_val(wth->interface_data, int_data); g_array_append_val(wth->interface_data, int_data);
wth->number_of_interfaces++;
interface_data.wtap_encap = wblock->data.if_descr.wtap_encap; iface_info.wtap_encap = wblock->data.if_descr.wtap_encap;
interface_data.snap_len = wblock->data.if_descr.snap_len; iface_info.snap_len = wblock->data.if_descr.snap_len;
interface_data.time_units_per_second = wblock->data.if_descr.time_units_per_second; iface_info.time_units_per_second = wblock->data.if_descr.time_units_per_second;
g_array_append_val(pcapng->interface_data, interface_data); g_array_append_val(pcapng->interfaces, iface_info);
pcapng->number_of_interfaces++;
} }
/* classic wtap: open capture file */ /* classic wtap: open capture file */
@ -2154,8 +2151,7 @@ pcapng_open(wtap *wth, int *err, gchar **err_info)
pn.if_fcslen = -1; pn.if_fcslen = -1;
pn.version_major = -1; pn.version_major = -1;
pn.version_minor = -1; pn.version_minor = -1;
pn.interface_data = g_array_new(FALSE, FALSE, sizeof(interface_data_t)); pn.interfaces = g_array_new(FALSE, FALSE, sizeof(interface_info_t));
pn.number_of_interfaces = 0;
/* we don't expect any packet blocks yet */ /* we don't expect any packet blocks yet */
@ -2209,10 +2205,6 @@ pcapng_open(wtap *wth, int *err, gchar **err_info)
wth->subtype_close = pcapng_close; wth->subtype_close = pcapng_close;
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG;
/* Read IDBs */
wth->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
wth->number_of_interfaces = 0;
/* Loop over all IDB:s that appear before any packets */ /* Loop over all IDB:s that appear before any packets */
while (1) { while (1) {
/* peek at next block */ /* peek at next block */
@ -2257,7 +2249,8 @@ pcapng_open(wtap *wth, int *err, gchar **err_info)
return -1; return -1;
} }
pcapng_process_idb(wth, pcapng, &wblock); pcapng_process_idb(wth, pcapng, &wblock);
pcapng_debug2("pcapng_open: Read IDB number_of_interfaces %u, wtap_encap %i", wth->number_of_interfaces, *wblock.file_encap); pcapng_debug2("pcapng_open: Read IDB number_of_interfaces %u, wtap_encap %i",
wth->interface_data->len, *wblock.file_encap);
} }
return 1; return 1;
} }
@ -2325,7 +2318,7 @@ pcapng_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
pcapng_debug0("pcapng_read: block type BLOCK_TYPE_ISB"); pcapng_debug0("pcapng_read: block type BLOCK_TYPE_ISB");
*data_offset += bytes_read; *data_offset += bytes_read;
pcapng_debug1("pcapng_read: *data_offset is updated to %" G_GINT64_MODIFIER "d", *data_offset); pcapng_debug1("pcapng_read: *data_offset is updated to %" G_GINT64_MODIFIER "d", *data_offset);
if (wth->number_of_interfaces < wblock.data.if_stats.interface_id) { if (wth->interface_data->len < wblock.data.if_stats.interface_id) {
pcapng_debug1("pcapng_read: BLOCK_TYPE_ISB wblock.if_stats.interface_id %u > number_of_interfaces", wblock.data.if_stats.interface_id); pcapng_debug1("pcapng_read: BLOCK_TYPE_ISB wblock.if_stats.interface_id %u > number_of_interfaces", wblock.data.if_stats.interface_id);
} else { } else {
/* Get the interface description */ /* Get the interface description */
@ -2422,18 +2415,10 @@ pcapng_close(wtap *wth)
pcapng_t *pcapng = (pcapng_t *)wth->priv; pcapng_t *pcapng = (pcapng_t *)wth->priv;
pcapng_debug0("pcapng_close: closing file"); pcapng_debug0("pcapng_close: closing file");
if (pcapng->interface_data != NULL) { g_array_free(pcapng->interfaces, TRUE);
g_array_free(pcapng->interface_data, TRUE);
}
} }
typedef struct {
GArray *interface_data;
guint number_of_interfaces;
} pcapng_dump_t;
static gboolean static gboolean
pcapng_write_section_header_block(wtap_dumper *wdh, int *err) pcapng_write_section_header_block(wtap_dumper *wdh, int *err)
{ {
@ -3297,7 +3282,7 @@ pcapng_write_enhanced_packet_block(wtap_dumper *wdh,
* Split the 64-bit timestamp into two 32-bit pieces, using * Split the 64-bit timestamp into two 32-bit pieces, using
* the time stamp resolution for the interface. * the time stamp resolution for the interface.
*/ */
if (epb.interface_id >= wdh->number_of_interfaces) { if (epb.interface_id >= wdh->interface_data->len) {
/* /*
* Our caller is doing something bad. * Our caller is doing something bad.
*/ */
@ -3557,32 +3542,11 @@ pcapng_write_name_resolution_block(wtap_dumper *wdh, int *err)
return TRUE; return TRUE;
} }
#if 0
static guint32
pcapng_lookup_interface_id_by_encap(int wtap_encap, wtap_dumper *wdh)
{
gint i;
interface_data_t int_data;
pcapng_dump_t *pcapng = (pcapng_dump_t *)wdh->priv;
for(i = 0; i < (gint)pcapng->number_of_interfaces; i++) {
int_data = g_array_index(pcapng->interface_data, interface_data_t, i);
if (wtap_encap == int_data.wtap_encap) {
return (guint32)i;
}
}
return G_MAXUINT32;
}
#endif
static gboolean pcapng_dump(wtap_dumper *wdh, static gboolean pcapng_dump(wtap_dumper *wdh,
const struct wtap_pkthdr *phdr, const struct wtap_pkthdr *phdr,
const guint8 *pd, int *err) const guint8 *pd, int *err)
{ {
const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header; const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
/*interface_data_t int_data;*/
/* pcapng_dump_t *pcapng = (pcapng_dump_t *)wdh->priv; */
/*int pcap_encap;*/
pcapng_debug2("pcapng_dump: encap = %d (%s)", pcapng_debug2("pcapng_dump: encap = %d (%s)",
phdr->pkt_encap, phdr->pkt_encap,
@ -3603,17 +3567,15 @@ static gboolean pcapng_dump(wtap_dumper *wdh,
Returns TRUE on success, FALSE on failure. */ Returns TRUE on success, FALSE on failure. */
static gboolean pcapng_dump_close(wtap_dumper *wdh, int *err _U_) static gboolean pcapng_dump_close(wtap_dumper *wdh, int *err _U_)
{ {
int i, j; guint i, j;
pcapng_dump_t *pcapng = (pcapng_dump_t *)wdh->priv;
if ((wdh->number_of_interfaces > 0) && (wdh->interface_data != NULL)) { for (i = 0; i < wdh->interface_data->len; i++) {
for (i = 0; i < (int)wdh->number_of_interfaces; i++) {
/* Get the interface description */ /* Get the interface description */
wtapng_if_descr_t int_data; wtapng_if_descr_t int_data;
int_data = g_array_index(wdh->interface_data, wtapng_if_descr_t, i); int_data = g_array_index(wdh->interface_data, wtapng_if_descr_t, i);
for (j = 0; j < (int)int_data.num_stat_entries; j++) { for (j = 0; j < int_data.num_stat_entries; j++) {
wtapng_if_stats_t if_stats; wtapng_if_stats_t if_stats;
if_stats = g_array_index(int_data.interface_statistics, wtapng_if_stats_t, j); if_stats = g_array_index(int_data.interface_statistics, wtapng_if_stats_t, j);
@ -3623,11 +3585,8 @@ static gboolean pcapng_dump_close(wtap_dumper *wdh, int *err _U_)
} }
} }
} }
}
pcapng_debug0("pcapng_dump_close"); pcapng_debug0("pcapng_dump_close");
g_array_free(pcapng->interface_data, TRUE);
pcapng->number_of_interfaces = 0;
return TRUE; return TRUE;
} }
@ -3637,19 +3596,14 @@ static gboolean pcapng_dump_close(wtap_dumper *wdh, int *err _U_)
gboolean gboolean
pcapng_dump_open(wtap_dumper *wdh, int *err) pcapng_dump_open(wtap_dumper *wdh, int *err)
{ {
pcapng_dump_t *pcapng; guint i;
int i;
interface_data_t interface_data;
pcapng_debug0("pcapng_dump_open"); pcapng_debug0("pcapng_dump_open");
/* This is a pcapng file */ /* This is a pcapng file */
wdh->subtype_write = pcapng_dump; wdh->subtype_write = pcapng_dump;
wdh->subtype_close = pcapng_dump_close; wdh->subtype_close = pcapng_dump_close;
pcapng = (pcapng_dump_t *)g_malloc0(sizeof(pcapng_dump_t));
wdh->priv = (void *)pcapng;
pcapng->interface_data = g_array_new(FALSE, FALSE, sizeof(interface_data_t));
if ((wdh->number_of_interfaces == 0) || (wdh->interface_data == NULL)) { if (wdh->interface_data->len == 0) {
pcapng_debug0("There are no interfaces. Can't handle that..."); pcapng_debug0("There are no interfaces. Can't handle that...");
*err = WTAP_ERR_INTERNAL; *err = WTAP_ERR_INTERNAL;
return FALSE; return FALSE;
@ -3662,76 +3616,21 @@ pcapng_dump_open(wtap_dumper *wdh, int *err)
pcapng_debug0("pcapng_dump_open: wrote section header block."); pcapng_debug0("pcapng_dump_open: wrote section header block.");
/* Write the Interface description blocks */ /* Write the Interface description blocks */
pcapng_debug1("pcapng_dump_open: Number of IDB:s to write (number of interfaces) %u", wdh->number_of_interfaces); pcapng_debug1("pcapng_dump_open: Number of IDB:s to write (number of interfaces) %u",
wdh->interface_data->len);
for (i = 0; i < (int)wdh->number_of_interfaces; i++) { for (i = 0; i < wdh->interface_data->len; i++) {
/* Get the interface description */ /* Get the interface description */
wtapng_if_descr_t int_data; wtapng_if_descr_t int_data;
int_data = g_array_index(wdh->interface_data, wtapng_if_descr_t, i); int_data = g_array_index(wdh->interface_data, wtapng_if_descr_t, i);
interface_data.wtap_encap = int_data.wtap_encap;
interface_data.time_units_per_second = int_data.time_units_per_second;
g_array_append_val(pcapng->interface_data, interface_data);
pcapng->number_of_interfaces++;
if (!pcapng_write_if_descr_block(wdh, &int_data, err)) { if (!pcapng_write_if_descr_block(wdh, &int_data, err)) {
return FALSE; return FALSE;
} }
} }
#if 0
interface_id = pcapng_lookup_interface_id_by_encap(phdr->pkt_encap, wdh);
if (interface_id == G_MAXUINT32) {
/*
* We haven't yet written out an interface description
* block for an interface with this encapsulation.
*
* Is this encapsulation even supported in pcap-ng?
*/
pcap_encap = wtap_wtap_encap_to_pcap_encap(phdr->pkt_encap);
if (pcap_encap == -1) {
/*
* No. Fail.
*/
*err = WTAP_ERR_UNSUPPORTED_ENCAP;
return FALSE;
}
/* write the interface description block */
wblock.frame_buffer = NULL;
wblock.pseudo_header = NULL;
wblock.packet_header = NULL;
wblock.file_encap = NULL;
wblock.type = BLOCK_TYPE_IDB;
wblock.data.if_descr.link_type = pcap_encap;
wblock.data.if_descr.snap_len = (wdh->snaplen != 0) ? wdh->snaplen :
WTAP_MAX_PACKET_SIZE; /* XXX */
/* XXX - options unused */
wblock.data.if_descr.if_speed = -1;
/*wblock.data.if_descr.if_tsresol = 6;*/ /* default: usec */
wblock.data.if_descr.if_os = NULL;
wblock.data.if_descr.if_fcslen = -1;
if (!pcapng_write_if_descr_block(wdh, &wblock, err)) {
return FALSE;
}
interface_id = pcapng->number_of_interfaces;
int_data.wtap_encap = phdr->pkt_encap;
int_data.time_units_per_second = 0;
g_array_append_val(pcapng->interface_data, int_data);
pcapng->number_of_interfaces++;
pcapng_debug3("pcapng_dump: added interface description block with index %u for encap = %d (%s).",
interface_id,
phdr->pkt_encap,
wtap_encap_string(phdr->pkt_encap));
}
#endif
return TRUE; return TRUE;
} }

View File

@ -109,7 +109,6 @@ struct wtap_dumper {
*/ */
addrinfo_lists_t *addrinfo_lists; /**< Struct containing lists of resolved addresses */ addrinfo_lists_t *addrinfo_lists; /**< Struct containing lists of resolved addresses */
struct wtapng_section_s *shb_hdr; struct wtapng_section_s *shb_hdr;
guint number_of_interfaces; /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
GArray *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/ GArray *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
}; };

View File

@ -201,7 +201,6 @@ wtap_file_get_idb_info(wtap *wth)
idb_info = g_new(wtapng_iface_descriptions_t,1); idb_info = g_new(wtapng_iface_descriptions_t,1);
idb_info->number_of_interfaces = wth->number_of_interfaces;
idb_info->interface_data = wth->interface_data; idb_info->interface_data = wth->interface_data;
return idb_info; return idb_info;
@ -907,7 +906,7 @@ wtap_fdclose(wtap *wth)
void void
wtap_close(wtap *wth) wtap_close(wtap *wth)
{ {
gint i, j; guint i, j;
wtapng_if_descr_t *wtapng_if_descr; wtapng_if_descr_t *wtapng_if_descr;
wtapng_if_stats_t *if_stats; wtapng_if_stats_t *if_stats;
@ -926,7 +925,7 @@ wtap_close(wtap *wth)
g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL); g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
g_ptr_array_free(wth->fast_seek, TRUE); g_ptr_array_free(wth->fast_seek, TRUE);
} }
for(i = 0; i < (gint)wth->number_of_interfaces; i++) { for(i = 0; i < wth->interface_data->len; i++) {
wtapng_if_descr = &g_array_index(wth->interface_data, wtapng_if_descr_t, i); wtapng_if_descr = &g_array_index(wth->interface_data, wtapng_if_descr_t, i);
if(wtapng_if_descr->opt_comment != NULL){ if(wtapng_if_descr->opt_comment != NULL){
g_free(wtapng_if_descr->opt_comment); g_free(wtapng_if_descr->opt_comment);
@ -946,7 +945,7 @@ wtap_close(wtap *wth)
if(wtapng_if_descr->if_os != NULL){ if(wtapng_if_descr->if_os != NULL){
g_free(wtapng_if_descr->if_os); g_free(wtapng_if_descr->if_os);
} }
for(j = 0; j < (gint)wtapng_if_descr->num_stat_entries; j++) { for(j = 0; j < wtapng_if_descr->num_stat_entries; j++) {
if_stats = &g_array_index(wtapng_if_descr->interface_statistics, wtapng_if_stats_t, j); if_stats = &g_array_index(wtapng_if_descr->interface_statistics, wtapng_if_stats_t, j);
if(if_stats->opt_comment != NULL){ if(if_stats->opt_comment != NULL){
g_free(if_stats->opt_comment); g_free(if_stats->opt_comment);
@ -956,9 +955,7 @@ wtap_close(wtap *wth)
g_array_free(wtapng_if_descr->interface_statistics, TRUE); g_array_free(wtapng_if_descr->interface_statistics, TRUE);
} }
} }
if(wth->number_of_interfaces != 0){
g_array_free(wth->interface_data, TRUE); g_array_free(wth->interface_data, TRUE);
}
g_free(wth); g_free(wth);
} }

View File

@ -990,7 +990,6 @@ typedef struct wtapng_section_s {
* one per interface. * one per interface.
*/ */
typedef struct wtapng_iface_descriptions_s { typedef struct wtapng_iface_descriptions_s {
guint number_of_interfaces;
GArray *interface_data; GArray *interface_data;
} wtapng_iface_descriptions_t; } wtapng_iface_descriptions_t;