diff options
author | Michael Mann <mmann78@netscape.net> | 2016-05-22 00:02:33 -0400 |
---|---|---|
committer | Michael Mann <mmann78@netscape.net> | 2016-05-26 12:44:58 +0000 |
commit | 7ca8e46ffb95a31b8df15019298f8404723e68b4 (patch) | |
tree | 9529fc3b27072e7bad671b2ee405d2b8ea91ab09 | |
parent | c0e679e0a991103db30bb8d57e8172ab29671884 (diff) |
Remove write capabilities from wtap_optionblocks.
The write functionality was too PCAPNG-specific and the intention is to
keep the option blocks as generic as possible.
So moved the write functionality back to pcapng.c and added a
wtap_opttype API to loop through all options in the block
(wtap_optionblock_foreach_option)
Change-Id: Iaf49126a1a3e2ed60ae02c52878ca22671dac335
Reviewed-on: https://code.wireshark.org/review/15525
Petri-Dish: Michael Mann <mmann78@netscape.net>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Michael Mann <mmann78@netscape.net>
-rw-r--r-- | wiretap/pcapng.c | 613 | ||||
-rw-r--r-- | wiretap/wtap_opttypes.c | 445 | ||||
-rw-r--r-- | wiretap/wtap_opttypes.h | 34 |
3 files changed, 635 insertions, 457 deletions
diff --git a/wiretap/pcapng.c b/wiretap/pcapng.c index fa483367d1..19204952d1 100644 --- a/wiretap/pcapng.c +++ b/wiretap/pcapng.c @@ -2702,25 +2702,166 @@ pcapng_close(wtap *wth) g_array_free(pcapng->interfaces, TRUE); } +typedef struct pcapng_optionblock_size_t +{ + guint32 size; +} pcapng_optionblock_size_t; + +static guint32 pcapng_compute_option_string_size(char *str) +{ + guint32 size = 0, pad; + + if (str == NULL) + return 0; + + size = (guint32)strlen(str) & 0xffff; + if ((size % 4)) { + pad = 4 - (size % 4); + } else { + pad = 0; + } + + size += pad; + + return size; +} + +static void compute_shb_option_size(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_optionblock_size_t* block_size = (pcapng_optionblock_size_t*)user_data; + guint32 size = 0; + + switch(option_id) + { + case OPT_COMMENT: + case OPT_SHB_HARDWARE: + case OPT_SHB_OS: + case OPT_SHB_USERAPPL: + if (option != NULL) + size = pcapng_compute_option_string_size(option->stringval); + break; + default: + /* Unknown options - size by datatype? */ + break; + } + + block_size->size += size; + /* Add bytes for option header if option should be written */ + if (size > 0) { + /* Add optional padding to 32 bits */ + if ((block_size->size & 0x03) != 0) + { + block_size->size += 4 - (block_size->size & 0x03); + } + block_size->size += 4; + } +} + +typedef struct pcapng_write_block_t +{ + wtap_dumper *wdh; + int *err; + gboolean success; +} +pcapng_write_block_t; + +static gboolean pcapng_write_option_string(wtap_dumper *wdh, guint option_id, char *str, int *err) +{ + struct pcapng_option_header option_hdr; + guint32 size = (guint32)strlen(str) & 0xffff; + const guint32 zero_pad = 0; + guint32 pad; + + if (size == 0) + return TRUE; + + /* String options don't consider pad bytes part of the length */ + option_hdr.type = (guint16)option_id; + option_hdr.value_length = (guint16)size; + if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) + return FALSE; + wdh->bytes_dumped += 4; + if (!wtap_dump_file_write(wdh, str, size, err)) + return FALSE; + wdh->bytes_dumped += size; + + if ((size % 4)) { + pad = 4 - (size % 4); + } else { + pad = 0; + } + + /* write padding (if any) */ + if (pad != 0) { + if (!wtap_dump_file_write(wdh, &zero_pad, pad, err)) + return FALSE; + + wdh->bytes_dumped += pad; + } + + return TRUE; +} + +static void write_wtap_shb_block(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_write_block_t* write_block = (pcapng_write_block_t*)user_data; + + /* Don't continue if there has been an error */ + if (!write_block->success) + return; + + switch(option_id) + { + case OPT_COMMENT: + case OPT_SHB_HARDWARE: + case OPT_SHB_OS: + case OPT_SHB_USERAPPL: + if ((option != NULL) && (option->stringval != NULL)) { + if (!pcapng_write_option_string(write_block->wdh, option_id, option->stringval, write_block->err)) { + write_block->success = FALSE; + return; + } + } + break; + default: + /* Unknown options - write by datatype? */ + break; + } +} + +/* Write a section header block. + * If we don't have a section block header already, create a default + * one with no options. + */ static gboolean pcapng_write_section_header_block(wtap_dumper *wdh, int *err) { pcapng_block_header_t bh; pcapng_section_header_block_t shb; + pcapng_optionblock_size_t block_size; + struct pcapng_option_header option_hdr; + block_size.size = 0; + bh.block_total_length = (guint32)(sizeof(bh) + sizeof(shb) + 4); if (wdh->shb_hdr) { pcapng_debug("pcapng_write_section_header_block: Have shb_hdr"); - return wtap_optionblock_write(wdh, wdh->shb_hdr, err); + /* Compute block size */ + wtap_optionblock_foreach_option(wdh->shb_hdr, compute_shb_option_size, &block_size); + + if (block_size.size > 0) { + /* End-of-options tag */ + block_size.size += 4; + } + + bh.block_total_length += block_size.size; } - /* we don't have a section block header already, so create a default one with no options */ + pcapng_debug("pcapng_write_section_header_block: Total len %u", bh.block_total_length); /* write block header */ bh.block_type = BLOCK_TYPE_SHB; - bh.block_total_length = (guint32)(sizeof(bh) + sizeof(shb) + 4); - pcapng_debug("pcapng_write_section_header_block: Total len %u", bh.block_total_length); if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) return FALSE; @@ -2730,12 +2871,39 @@ pcapng_write_section_header_block(wtap_dumper *wdh, int *err) shb.magic = 0x1A2B3C4D; shb.version_major = 1; shb.version_minor = 0; - shb.section_length = -1; + if (wdh->shb_hdr) { + wtapng_mandatory_section_t* section_data = (wtapng_mandatory_section_t*)wtap_optionblock_get_mandatory_data(wdh->shb_hdr); + shb.section_length = section_data->section_length; + } else { + shb.section_length = -1; + } if (!wtap_dump_file_write(wdh, &shb, sizeof shb, err)) return FALSE; wdh->bytes_dumped += sizeof shb; + if (wdh->shb_hdr) { + pcapng_write_block_t block_data; + + if (block_size.size > 0) { + /* Write options */ + block_data.wdh = wdh; + block_data.err = err; + block_data.success = TRUE; + wtap_optionblock_foreach_option(wdh->shb_hdr, write_wtap_shb_block, &block_data); + + if (!block_data.success) + return FALSE; + + /* Write end of options */ + option_hdr.type = OPT_EOFOPT; + option_hdr.value_length = 0; + if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) + return FALSE; + wdh->bytes_dumped += 4; + } + } + /* write block footer */ if (!wtap_dump_file_write(wdh, &bh.block_total_length, sizeof bh.block_total_length, err)) @@ -3193,6 +3361,437 @@ pcapng_write_name_resolution_block(wtap_dumper *wdh, int *err) return TRUE; } +static void compute_isb_option_size(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_optionblock_size_t* block_size = (pcapng_optionblock_size_t*)user_data; + guint32 size = 0; + + switch(option_id) + { + case OPT_COMMENT: + if (option != NULL) { + size = pcapng_compute_option_string_size(option->stringval); + } + break; + case OPT_ISB_STARTTIME: + case OPT_ISB_ENDTIME: + if ((option != NULL) && (option->uint64val != 0)) { + size = 8; + } + break; + case OPT_ISB_IFRECV: + case OPT_ISB_IFDROP: + case OPT_ISB_FILTERACCEPT: + case OPT_ISB_OSDROP: + case OPT_ISB_USRDELIV: + if ((option != NULL) && (option->uint64val != G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF))) { + size = 8; + } + break; + default: + /* Unknown options - size by datatype? */ + break; + } + + block_size->size += size; + /* Add bytes for option header if option should be written */ + if (size > 0) { + /* Add optional padding to 32 bits */ + if ((block_size->size & 0x03) != 0) + { + block_size->size += 4 - (block_size->size & 0x03); + } + block_size->size += 4; + } +} + +static void write_wtap_isb_block(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_write_block_t* write_block = (pcapng_write_block_t*)user_data; + struct pcapng_option_header option_hdr; + + /* Don't continue if there has been an error */ + if (!write_block->success) + return; + + switch(option_id) + { + case OPT_COMMENT: + if ((option != NULL) && (option->stringval != NULL)) { + if (!pcapng_write_option_string(write_block->wdh, option_id, option->stringval, write_block->err)) { + write_block->success = FALSE; + return; + } + } + break; + case OPT_ISB_STARTTIME: + case OPT_ISB_ENDTIME: + if ((option != NULL) && (option->uint64val != 0)) { + guint32 high, low; + + option_hdr.type = option_id; + option_hdr.value_length = 8; + if (!wtap_dump_file_write(write_block->wdh, &option_hdr, 4, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + + high = (guint32)(option->uint64val >> 32); + low = (guint32)(option->uint64val >> 0); + if (!wtap_dump_file_write(write_block->wdh, &high, sizeof(guint32), write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + if (!wtap_dump_file_write(write_block->wdh, &low, sizeof(guint32), write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + } + break; + case OPT_ISB_IFRECV: + case OPT_ISB_IFDROP: + case OPT_ISB_FILTERACCEPT: + case OPT_ISB_OSDROP: + case OPT_ISB_USRDELIV: + if ((option != NULL) && (option->uint64val != G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF))) { + option_hdr.type = option_id; + option_hdr.value_length = 8; + if (!wtap_dump_file_write(write_block->wdh, &option_hdr, 4, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + + if (!wtap_dump_file_write(write_block->wdh, &option->uint64val, sizeof(guint64), write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 8; + } + break; + default: + /* Unknown options - write by datatype? */ + break; + } +} + +static gboolean +pcapng_write_interface_statistics_block(wtap_dumper *wdh, wtap_optionblock_t if_stats, int *err) +{ + pcapng_block_header_t bh; + pcapng_interface_statistics_block_t isb; + pcapng_optionblock_size_t block_size; + pcapng_write_block_t block_data; + struct pcapng_option_header option_hdr; + wtapng_if_stats_mandatory_t* mand_data = (wtapng_if_stats_mandatory_t*)wtap_optionblock_get_mandatory_data(if_stats); + + pcapng_debug("pcapng_write_interface_statistics_block"); + + /* Compute block size */ + block_size.size = 0; + wtap_optionblock_foreach_option(if_stats, compute_isb_option_size, &block_size); + + if (block_size.size > 0) { + /* End-of-options tag */ + block_size.size += 4; + } + + /* write block header */ + bh.block_type = BLOCK_TYPE_ISB; + bh.block_total_length = (guint32)(sizeof(bh) + sizeof(isb) + block_size.size + 4); + pcapng_debug("pcapng_write_interface_statistics_block: Total len %u", bh.block_total_length); + + if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) + return FALSE; + wdh->bytes_dumped += sizeof bh; + + /* write block fixed content */ + isb.interface_id = mand_data->interface_id; + isb.timestamp_high = mand_data->ts_high; + isb.timestamp_low = mand_data->ts_low; + + if (!wtap_dump_file_write(wdh, &isb, sizeof isb, err)) + return FALSE; + wdh->bytes_dumped += sizeof isb; + + /* Write options */ + if (block_size.size > 0) { + block_data.wdh = wdh; + block_data.err = err; + block_data.success = TRUE; + wtap_optionblock_foreach_option(if_stats, write_wtap_isb_block, &block_data); + + if (!block_data.success) + return FALSE; + + /* Write end of options */ + option_hdr.type = OPT_EOFOPT; + option_hdr.value_length = 0; + if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) + return FALSE; + wdh->bytes_dumped += 4; + } + + /* write block footer */ + if (!wtap_dump_file_write(wdh, &bh.block_total_length, + sizeof bh.block_total_length, err)) + return FALSE; + wdh->bytes_dumped += sizeof bh.block_total_length; + return TRUE; +} + +static void compute_idb_option_size(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_optionblock_size_t* block_size = (pcapng_optionblock_size_t*)user_data; + guint32 size = 0; + + switch(option_id) + { + case OPT_COMMENT: + case OPT_IDB_NAME: + case OPT_IDB_DESCR: + case OPT_IDB_OS: + if (option != NULL) { + size = pcapng_compute_option_string_size(option->stringval); + } + break; + case OPT_IDB_SPEED: + if ((option != NULL) && (option->uint64val != 0)) { + size = 8; + } + break; + case OPT_IDB_TSRESOL: + if ((option != NULL) && (option->uint8val != 0)) { + size = 1; + } + break; + case OPT_IDB_FILTER: + if (option != NULL) { + wtapng_if_descr_filter_t* filter = (wtapng_if_descr_filter_t*)option->customval.data; + guint32 pad; + if ((filter != NULL) && (filter->if_filter_str != NULL)) { + size = (guint32)(strlen(filter->if_filter_str) + 1) & 0xffff; + if ((size % 4)) { + pad = 4 - (size % 4); + } else { + pad = 0; + } + + size += pad; + } + } + break; + case OPT_IDB_FCSLEN: + /* XXX - Not currently writing value */ + break; + default: + /* Unknown options - size by datatype? */ + break; + } + + block_size->size += size; + /* Add bytes for option header if option should be written */ + if (size > 0) { + /* Add optional padding to 32 bits */ + if ((block_size->size & 0x03) != 0) + { + block_size->size += 4 - (block_size->size & 0x03); + } + block_size->size += 4; + } +} + +static void write_wtap_idb_block(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data) +{ + pcapng_write_block_t* write_block = (pcapng_write_block_t*)user_data; + struct pcapng_option_header option_hdr; + const guint32 zero_pad = 0; + + switch(option_id) + { + case OPT_COMMENT: + case OPT_IDB_NAME: + case OPT_IDB_DESCR: + case OPT_IDB_OS: + if ((option != NULL) && (option->stringval != NULL)) { + if (!pcapng_write_option_string(write_block->wdh, option_id, option->stringval, write_block->err)) { + write_block->success = FALSE; + return; + } + } + break; + case OPT_IDB_SPEED: + if ((option != NULL) && (option->uint64val != 0)) { + option_hdr.type = option_id; + option_hdr.value_length = 8; + if (!wtap_dump_file_write(write_block->wdh, &option_hdr, 4, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + + if (!wtap_dump_file_write(write_block->wdh, &option->uint64val, sizeof(guint64), write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 8; + } + break; + case OPT_IDB_TSRESOL: + if ((option != NULL) && (option->uint8val != 0)) { + option_hdr.type = option_id; + option_hdr.value_length = 1; + if (!wtap_dump_file_write(write_block->wdh, &option_hdr, 4, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + + if (!wtap_dump_file_write(write_block->wdh, &option->uint8val, 1, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 1; + + if (!wtap_dump_file_write(write_block->wdh, &zero_pad, 3, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 3; + } + break; + case OPT_IDB_FILTER: + if (option != NULL) { + wtapng_if_descr_filter_t* filter = (wtapng_if_descr_filter_t*)option->customval.data; + guint32 size, pad; + if ((filter != NULL) && (filter->if_filter_str != NULL)) { + size = (guint32)(strlen(filter->if_filter_str) + 1) & 0xffff; + if ((size % 4)) { + pad = 4 - (size % 4); + } else { + pad = 0; + } + + option_hdr.type = option_id; + option_hdr.value_length = size; + if (!wtap_dump_file_write(write_block->wdh, &option_hdr, 4, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 4; + + /* Write the zero indicating libpcap filter variant */ + if (!wtap_dump_file_write(write_block->wdh, &zero_pad, 1, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += 1; + + /* if_filter_str_len includes the leading byte indicating filter type (libpcap str or BPF code) */ + if (!wtap_dump_file_write(write_block->wdh, filter->if_filter_str, size-1, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += size - 1; + + /* write padding (if any) */ + if (pad != 0) { + if (!wtap_dump_file_write(write_block->wdh, &zero_pad, pad, write_block->err)) { + write_block->success = FALSE; + return; + } + write_block->wdh->bytes_dumped += pad; + } + + } + } + break; + case OPT_IDB_FCSLEN: + /* XXX - Not currently writing value */ + break; + default: + /* Unknown options - size by datatype? */ + break; + } +} + +static gboolean +pcapng_write_if_descr_block(wtap_dumper *wdh, wtap_optionblock_t int_data, int *err) +{ + pcapng_block_header_t bh; + pcapng_interface_description_block_t idb; + pcapng_optionblock_size_t block_size; + pcapng_write_block_t block_data; + struct pcapng_option_header option_hdr; + wtapng_if_descr_mandatory_t* mand_data = (wtapng_if_descr_mandatory_t*)wtap_optionblock_get_mandatory_data(int_data); + + pcapng_debug("pcapng_write_if_descr_block: encap = %d (%s), snaplen = %d", + mand_data->link_type, + wtap_encap_string(wtap_pcap_encap_to_wtap_encap(mand_data->link_type)), + mand_data->snap_len); + + if (mand_data->link_type == (guint16)-1) { + *err = WTAP_ERR_UNWRITABLE_ENCAP; + return FALSE; + } + + /* Compute block size */ + block_size.size = 0; + wtap_optionblock_foreach_option(int_data, compute_idb_option_size, &block_size); + + if (block_size.size > 0) { + /* End-of-options tag */ + block_size.size += 4; + } + + /* write block header */ + bh.block_type = BLOCK_TYPE_IDB; + bh.block_total_length = (guint32)(sizeof(bh) + sizeof(idb) + block_size.size + 4); + pcapng_debug("pcapng_write_if_descr_block: Total len %u", bh.block_total_length); + + if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) + return FALSE; + wdh->bytes_dumped += sizeof bh; + + /* write block fixed content */ + idb.linktype = mand_data->link_type; + idb.reserved = 0; + idb.snaplen = mand_data->snap_len; + + if (!wtap_dump_file_write(wdh, &idb, sizeof idb, err)) + return FALSE; + wdh->bytes_dumped += sizeof idb; + + if (block_size.size > 0) { + /* Write options */ + block_data.wdh = wdh; + block_data.err = err; + block_data.success = TRUE; + wtap_optionblock_foreach_option(int_data, write_wtap_idb_block, &block_data); + + if (!block_data.success) + return FALSE; + + /* Write end of options */ + option_hdr.type = OPT_EOFOPT; + option_hdr.value_length = 0; + if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) + return FALSE; + wdh->bytes_dumped += 4; + } + + /* write block footer */ + if (!wtap_dump_file_write(wdh, &bh.block_total_length, + sizeof bh.block_total_length, err)) + return FALSE; + + wdh->bytes_dumped += sizeof bh.block_total_length; + return TRUE; +} + static gboolean pcapng_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr, const guint8 *pd, int *err, gchar **err_info _U_) @@ -3268,7 +3867,7 @@ static gboolean pcapng_dump_finish(wtap_dumper *wdh, int *err) if_stats = g_array_index(int_data_mand->interface_statistics, wtap_optionblock_t, j); pcapng_debug("pcapng_dump_finish: write ISB for interface %u", ((wtapng_if_stats_mandatory_t*)wtap_optionblock_get_mandatory_data(if_stats))->interface_id); - if (!wtap_optionblock_write(wdh, if_stats, err)) { + if (!pcapng_write_interface_statistics_block(wdh, if_stats, err)) { return FALSE; } } @@ -3314,7 +3913,7 @@ pcapng_dump_open(wtap_dumper *wdh, int *err) idb = g_array_index(wdh->interface_data, wtap_optionblock_t, i); - if (!wtap_optionblock_write(wdh, idb, err)) { + if (!pcapng_write_if_descr_block(wdh, idb, err)) { return FALSE; } diff --git a/wiretap/wtap_opttypes.c b/wiretap/wtap_opttypes.c index 9c53b48a9d..87c874a1a4 100644 --- a/wiretap/wtap_opttypes.c +++ b/wiretap/wtap_opttypes.c @@ -40,7 +40,6 @@ typedef struct wtap_opt_register const char *name; /**< name of block */ const char *description; /**< human-readable description of block */ wtap_block_create_func create; - wtap_write_func write; wtap_mand_free_func free_mand; wtap_mand_copy_func copy_mand; } wtap_opt_register_t; @@ -50,8 +49,6 @@ typedef struct wtap_optblock_internal { const char *description; /**< human-readable description of option */ guint number; /**< Option index */ wtap_opttype_e type; /**< type of that option */ - wtap_opttype_option_write_size write_size; /**< Number of bytes to write to file (0 for don't write) */ - wtap_opttype_option_write write_data; /**< write option data to dumper */ } wtap_optblock_internal_t; typedef struct wtap_optblock_value { @@ -93,7 +90,7 @@ static void wtap_opttype_block_register(int block_type, wtap_opt_register_t *blo } int wtap_opttype_register_custom_block_type(const char* name, const char* description, wtap_block_create_func create, - wtap_write_func write_func, wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand) + wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand) { int block_type; @@ -110,7 +107,6 @@ int wtap_opttype_register_custom_block_type(const char* name, const char* descri custom_block_list[num_custom_blocks].name = name; custom_block_list[num_custom_blocks].description = description; custom_block_list[num_custom_blocks].create = create; - custom_block_list[num_custom_blocks].write = write_func; custom_block_list[num_custom_blocks].free_mand = free_mand; custom_block_list[num_custom_blocks].copy_mand = copy_mand; block_list[block_type] = &custom_block_list[num_custom_blocks]; @@ -224,8 +220,6 @@ void wtap_optionblock_copy_options(wtap_optionblock_t dest_block, wtap_optionblo reg_optblock.name = src_internal->name; reg_optblock.description = src_internal->description; reg_optblock.type = src_internal->type; - reg_optblock.write_size_func = src_internal->write_size; - reg_optblock.write_func = src_internal->write_data; reg_optblock.option = src_value->option; reg_optblock.default_val = src_value->default_val; @@ -256,87 +250,18 @@ void wtap_optionblock_copy_options(wtap_optionblock_t dest_block, wtap_optionblo } } -gboolean wtap_optionblock_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err) -{ - if ((block == NULL) || (block->info->write == NULL)) - { - *err = WTAP_ERR_INTERNAL; - return FALSE; - } - - return block->info->write(wdh, block, err); -} - -static guint32 wtap_optionblock_get_option_write_size(wtap_optionblock_t block) +void wtap_optionblock_foreach_option(wtap_optionblock_t block, wtap_optionblock_foreach_func func, void* user_data) { guint i; - guint32 options_total_length = 0, length; + wtap_optblock_internal_t *internal_data; wtap_optblock_value_t *value; for (i = 0; i < block->option_values->len; i++) { + internal_data = g_array_index(block->option_infos, wtap_optblock_internal_t*, i); value = g_array_index(block->option_values, wtap_optblock_value_t*, i); - if ((value->info->write_size != NULL) && (value->info->write_data != NULL)) - { - length = value->info->write_size(&value->option); - options_total_length += length; - /* Add bytes for option header if option should be written */ - if (length > 0) { - /* Add optional padding to 32 bits */ - if ((options_total_length & 0x03) != 0) - { - options_total_length += 4 - (options_total_length & 0x03); - } - options_total_length += 4; - } - } + func(block, internal_data->number, value->info->type, &value->option, user_data); } - - return options_total_length; -} - -static gboolean wtap_optionblock_write_options(struct wtap_dumper *wdh, wtap_optionblock_t block, guint32 options_total_length, int *err) -{ - guint i; - wtap_optblock_value_t *value; - struct pcapng_option_header option_hdr; - guint32 length; - - /* Check if we have at least 1 option to write */ - if (options_total_length == 0) - return TRUE; - - for (i = 0; i < block->option_values->len; i++) - { - value = g_array_index(block->option_values, wtap_optblock_value_t*, i); - if ((value->info->write_size != NULL) && (value->info->write_data != NULL) && - ((length = value->info->write_size(&value->option)) > 0)) - { - /* Write the option */ - wtap_debug("wtap_optionblock_write %s, field:'%s' length: %u", block->description, value->info->description, length); - - /* String options don't consider pad bytes part of the length, so readjust here */ - if (value->info->type == WTAP_OPTTYPE_STRING) - length = (guint32)strlen(value->option.stringval) & 0xffff; - - option_hdr.type = value->info->number; - option_hdr.value_length = length; - if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) - return FALSE; - wdh->bytes_dumped += 4; - - if (!value->info->write_data(wdh, &value->option, err)) - return FALSE; - } - } - - /* Write end of options */ - option_hdr.type = OPT_EOFOPT; - option_hdr.value_length = 0; - if (!wtap_dump_file_write(wdh, &option_hdr, 4, err)) - return FALSE; - wdh->bytes_dumped += 4; - return TRUE; } int wtap_optionblock_add_option(wtap_optionblock_t block, guint option_id, wtap_optblock_reg_t* option) @@ -355,8 +280,6 @@ int wtap_optionblock_add_option(wtap_optionblock_t block, guint option_id, wtap_ opt_internal->description = option->description; opt_internal->number = option_id; opt_internal->type = option->type; - opt_internal->write_size = option->write_size_func; - opt_internal->write_data = option->write_func; opt_value->info = opt_internal; @@ -439,48 +362,6 @@ int wtap_optionblock_get_option_string(wtap_optionblock_t block, guint option_id return WTAP_OPTTYPE_SUCCESS; } -guint32 wtap_opttype_write_size_string(wtap_option_type* data) -{ - guint32 size, pad; - if ((data == NULL) ||(data->stringval == NULL)) - return 0; - - size = (guint32)strlen(data->stringval) & 0xffff; - if ((size % 4)) { - pad = 4 - (size % 4); - } else { - pad = 0; - } - - return size+pad; -} - -gboolean wtap_opttype_write_data_string(struct wtap_dumper* wdh, wtap_option_type* data, int *err) -{ - guint32 size = (guint32)strlen(data->stringval) & 0xffff; - guint32 pad; - const guint32 zero_pad = 0; - - if (!wtap_dump_file_write(wdh, data->stringval, size, err)) - return FALSE; - wdh->bytes_dumped += size; - - if ((size % 4)) { - pad = 4 - (size % 4); - } else { - pad = 0; - } - - /* write padding (if any) */ - if (pad != 0) { - if (!wtap_dump_file_write(wdh, &zero_pad, pad, err)) - return FALSE; - wdh->bytes_dumped += pad; - } - - return TRUE; -} - int wtap_optionblock_set_option_uint64(wtap_optionblock_t block, guint option_id, guint64 value) { wtap_optblock_value_t* opt_value = wtap_optionblock_get_option(block, option_id); @@ -511,52 +392,6 @@ int wtap_optionblock_get_option_uint64(wtap_optionblock_t block, guint option_id return WTAP_OPTTYPE_SUCCESS; } -guint32 wtap_opttype_write_uint64_not0(wtap_option_type* data) -{ - if (data == NULL) - return 0; - - if (data->uint64val == 0) - return 0; - - /* value */ - return 8; -} - -guint32 wtap_opttype_write_uint64_not_minus1(wtap_option_type* data) -{ - if (data == NULL) - return 0; - - if (data->uint64val == G_GUINT64_CONSTANT(0xFFFFFFFFFFFFFFFF)) - return 0; - - /* value */ - return 8; -} - -gboolean wtap_opttype_write_data_uint64(struct wtap_dumper* wdh, wtap_option_type* data, int *err) -{ - if (!wtap_dump_file_write(wdh, &data->uint64val, sizeof(guint64), err)) - return FALSE; - wdh->bytes_dumped += 8; - return TRUE; -} - -static gboolean wtap_opttype_write_data_uint64_timestamp(struct wtap_dumper* wdh, wtap_option_type* data, int *err) -{ - guint32 high, low; - - high = (guint32)(data->uint64val >> 32); - low = (guint32)(data->uint64val >> 0); - if (!wtap_dump_file_write(wdh, &high, sizeof(guint32), err)) - return FALSE; - wdh->bytes_dumped += 4; - if (!wtap_dump_file_write(wdh, &low, sizeof(guint32), err)) - return FALSE; - wdh->bytes_dumped += 4; - return TRUE; -} int wtap_optionblock_set_option_uint8(wtap_optionblock_t block, guint option_id, guint8 value) { @@ -588,32 +423,6 @@ int wtap_optionblock_get_option_uint8(wtap_optionblock_t block, guint option_id, return WTAP_OPTTYPE_SUCCESS; } -guint32 wtap_opttype_write_uint8_not0(wtap_option_type* data) -{ - if (data == NULL) - return 0; - - if (data->uint8val == 0) - return 0; - - return 1; -} - -gboolean wtap_opttype_write_data_uint8(struct wtap_dumper* wdh, wtap_option_type* data, int *err) -{ - const guint32 zero_pad = 0; - - if (!wtap_dump_file_write(wdh, &data->uint8val, 1, err)) - return FALSE; - wdh->bytes_dumped += 1; - - if (!wtap_dump_file_write(wdh, &zero_pad, 3, err)) - return FALSE; - wdh->bytes_dumped += 3; - - return TRUE; -} - int wtap_optionblock_set_option_custom(wtap_optionblock_t block, guint option_id, void* value) { wtap_optblock_value_t* opt_value = wtap_optionblock_get_option(block, option_id); @@ -650,10 +459,10 @@ int wtap_optionblock_get_option_custom(wtap_optionblock_t block, guint option_id static void shb_create(wtap_optionblock_t block) { - static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t hardware_option = {"hardware", "SBH Hardware", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t os_option = {"os", "SBH Operating System", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t user_appl_option = {"user_appl", "SBH User Application", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; + static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t hardware_option = {"hardware", "SBH Hardware", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t os_option = {"os", "SBH Operating System", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t user_appl_option = {"user_appl", "SBH User Application", WTAP_OPTTYPE_STRING, {0}, {0}}; wtapng_mandatory_section_t* section_mand = g_new(wtapng_mandatory_section_t, 1); @@ -677,52 +486,6 @@ static void shb_create(wtap_optionblock_t block) wtap_optionblock_add_option(block, OPT_SHB_USERAPPL, &user_appl_option); } -static gboolean shb_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err) -{ - pcapng_block_header_t bh; - pcapng_section_header_block_t shb; - wtapng_mandatory_section_t* mand_data = (wtapng_mandatory_section_t*)block->mandatory_data; - guint32 options_total_length; - - wtap_debug("write_section_header_block: Have shb_hdr"); - - options_total_length = wtap_optionblock_get_option_write_size(block); - if (options_total_length > 0) - { - /* End-of-options tag */ - options_total_length += 4; - } - - /* write block header */ - bh.block_type = BLOCK_TYPE_SHB; - bh.block_total_length = (guint32)(sizeof(bh) + sizeof(shb) + options_total_length + 4); - - if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh; - - /* write block fixed content */ - shb.magic = 0x1A2B3C4D; - shb.version_major = 1; - shb.version_minor = 0; - shb.section_length = mand_data->section_length; - - if (!wtap_dump_file_write(wdh, &shb, sizeof shb, err)) - return FALSE; - wdh->bytes_dumped += sizeof shb; - - if (!wtap_optionblock_write_options(wdh, block, options_total_length, err)) - return FALSE; - - /* write block footer */ - if (!wtap_dump_file_write(wdh, &bh.block_total_length, - sizeof bh.block_total_length, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh.block_total_length; - - return TRUE; -} - static void shb_copy_mand(wtap_optionblock_t dest_block, wtap_optionblock_t src_block) { memcpy(dest_block->mandatory_data, src_block->mandatory_data, sizeof(wtapng_mandatory_section_t)); @@ -730,7 +493,7 @@ static void shb_copy_mand(wtap_optionblock_t dest_block, wtap_optionblock_t src_ static void nrb_create(wtap_optionblock_t block) { - static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; + static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, {0}, {0}}; /* Set proper values for the union */ comment_option.option.stringval = NULL; @@ -743,14 +506,14 @@ static void nrb_create(wtap_optionblock_t block) static void isb_create(wtap_optionblock_t block) { - static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t starttime_option = {"start_time", "Start Time", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not0, wtap_opttype_write_data_uint64_timestamp, {0}, {0}}; - static wtap_optblock_reg_t endtime_option = {"end_time", "End Time", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not0, wtap_opttype_write_data_uint64_timestamp, {0}, {0}}; - static wtap_optblock_reg_t rcv_pkt_option = {"recv", "Receive Packets", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not_minus1, wtap_opttype_write_data_uint64, {0}, {0}}; - static wtap_optblock_reg_t drop_pkt_option = {"drop", "Dropped Packets", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not_minus1, wtap_opttype_write_data_uint64, {0}, {0}}; - static wtap_optblock_reg_t filteraccept_option = {"filter_accept", "Filter Accept", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not_minus1, wtap_opttype_write_data_uint64, {0}, {0}}; - static wtap_optblock_reg_t os_drop_option = {"os_drop", "OS Dropped Packets", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not_minus1, wtap_opttype_write_data_uint64, {0}, {0}}; - static wtap_optblock_reg_t user_deliv_option = {"user_deliv", "User Delivery", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not_minus1, wtap_opttype_write_data_uint64, {0}, {0}}; + static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t starttime_option = {"start_time", "Start Time", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t endtime_option = {"end_time", "End Time", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t rcv_pkt_option = {"recv", "Receive Packets", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t drop_pkt_option = {"drop", "Dropped Packets", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t filteraccept_option = {"filter_accept", "Filter Accept", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t os_drop_option = {"os_drop", "OS Dropped Packets", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t user_deliv_option = {"user_deliv", "User Delivery", WTAP_OPTTYPE_UINT64, {0}, {0}}; block->mandatory_data = g_new0(wtapng_if_stats_mandatory_t, 1); @@ -782,51 +545,6 @@ static void isb_create(wtap_optionblock_t block) wtap_optionblock_add_option(block, OPT_ISB_USRDELIV, &user_deliv_option); } -static gboolean isb_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err) -{ - pcapng_block_header_t bh; - pcapng_interface_statistics_block_t isb; - guint32 options_total_length; - wtapng_if_stats_mandatory_t* mand_data = (wtapng_if_stats_mandatory_t*)block->mandatory_data; - - wtap_debug("write_interface_statistics_block"); - - options_total_length = wtap_optionblock_get_option_write_size(block); - if (options_total_length > 0) - { - /* End-of-options tag */ - options_total_length += 4; - } - - /* write block header */ - bh.block_type = BLOCK_TYPE_ISB; - bh.block_total_length = (guint32)(sizeof(bh) + sizeof(isb) + options_total_length + 4); - - if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh; - - /* write block fixed content */ - isb.interface_id = mand_data->interface_id; - isb.timestamp_high = mand_data->ts_high; - isb.timestamp_low = mand_data->ts_low; - - if (!wtap_dump_file_write(wdh, &isb, sizeof isb, err)) - return FALSE; - wdh->bytes_dumped += sizeof isb; - - if (!wtap_optionblock_write_options(wdh, block, options_total_length, err)) - return FALSE; - - /* write block footer */ - if (!wtap_dump_file_write(wdh, &bh.block_total_length, - sizeof bh.block_total_length, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh.block_total_length; - - return TRUE; -} - static void isb_copy_mand(wtap_optionblock_t dest_block, wtap_optionblock_t src_block) { memcpy(dest_block->mandatory_data, src_block->mandatory_data, sizeof(wtapng_if_stats_mandatory_t)); @@ -839,71 +557,16 @@ static void idb_filter_free(void* data) g_free(filter->if_filter_bpf_bytes); } -static guint32 idb_filter_write_size(wtap_option_type* data) -{ - wtapng_if_descr_filter_t* filter; - guint32 size, pad; - - if (data == NULL) - return 0; - - filter = (wtapng_if_descr_filter_t*)data->customval.data; - if ((filter == NULL) || (filter->if_filter_str == NULL)) - return 0; - - size = (guint32)(strlen(filter->if_filter_str) + 1) & 0xffff; - if ((size % 4)) { - pad = 4 - (size % 4); - } else { - pad = 0; - } - - return pad + size; -} - -static gboolean idb_filter_write(struct wtap_dumper* wdh, wtap_option_type* data, int *err) -{ - wtapng_if_descr_filter_t* filter = (wtapng_if_descr_filter_t*)data->customval.data; - guint32 size, pad; - const guint32 zero_pad = 0; - - size = (guint32)(strlen(filter->if_filter_str) + 1) & 0xffff; - if ((size % 4)) { - pad = 4 - (size % 4); - } else { - pad = 0; - } - - /* Write the zero indicating libpcap filter variant */ - if (!wtap_dump_file_write(wdh, &zero_pad, 1, err)) - return FALSE; - wdh->bytes_dumped += 1; - - /* if_filter_str_len includes the leading byte indicating filter type (libpcap str or BPF code) */ - if (!wtap_dump_file_write(wdh, filter->if_filter_str, size-1, err)) - return FALSE; - wdh->bytes_dumped += size - 1; - - /* write padding (if any) */ - if (pad != 0) { - if (!wtap_dump_file_write(wdh, &zero_pad, pad, err)) - return FALSE; - wdh->bytes_dumped += pad; - } - - return TRUE; -} - static void idb_create(wtap_optionblock_t block) { - static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t name_option = {"name", "Device name", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t description_option = {"description", "Device description", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t speed_option = {"speed", "Interface speed (in bps)", WTAP_OPTTYPE_UINT64, wtap_opttype_write_uint64_not0, wtap_opttype_write_data_uint64, {0}, {0}}; - static wtap_optblock_reg_t tsresol_option = {"ts_resolution", "Resolution of timestamps", WTAP_OPTTYPE_UINT8, wtap_opttype_write_uint8_not0, wtap_opttype_write_data_uint8, {0}, {0}}; - static wtap_optblock_reg_t filter_option = {"filter", "Filter string", WTAP_OPTTYPE_CUSTOM, idb_filter_write_size, idb_filter_write, {0}, {0}}; - static wtap_optblock_reg_t os_option = {"os", "Operating System", WTAP_OPTTYPE_STRING, wtap_opttype_write_size_string, wtap_opttype_write_data_string, {0}, {0}}; - static wtap_optblock_reg_t fcslen_option = {"fcslen", "FCS Length", WTAP_OPTTYPE_UINT8, NULL, NULL, {0}, {0}}; + static wtap_optblock_reg_t comment_option = {"opt_comment", "Optional comment", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t name_option = {"name", "Device name", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t description_option = {"description", "Device description", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t speed_option = {"speed", "Interface speed (in bps)", WTAP_OPTTYPE_UINT64, {0}, {0}}; + static wtap_optblock_reg_t tsresol_option = {"ts_resolution", "Resolution of timestamps", WTAP_OPTTYPE_UINT8, {0}, {0}}; + static wtap_optblock_reg_t filter_option = {"filter", "Filter string", WTAP_OPTTYPE_CUSTOM, {0}, {0}}; + static wtap_optblock_reg_t os_option = {"os", "Operating System", WTAP_OPTTYPE_STRING, {0}, {0}}; + static wtap_optblock_reg_t fcslen_option = {"fcslen", "FCS Length", WTAP_OPTTYPE_UINT8, {0}, {0}}; wtapng_if_descr_filter_t default_filter; memset(&default_filter, 0, sizeof(default_filter)); @@ -942,58 +605,6 @@ static void idb_create(wtap_optionblock_t block) wtap_optionblock_add_option(block, OPT_IDB_FCSLEN, &fcslen_option); } -static gboolean idb_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err) -{ - pcapng_block_header_t bh; - pcapng_interface_description_block_t idb; - wtapng_if_descr_mandatory_t* mand_data = (wtapng_if_descr_mandatory_t*)block->mandatory_data; - guint32 options_total_length; - - wtap_debug("write_interface_description_block: encap = %d (%s), snaplen = %d", - mand_data->link_type, - wtap_encap_string(wtap_pcap_encap_to_wtap_encap(mand_data->link_type)), - mand_data->snap_len); - - if (mand_data->link_type == (guint16)-1) { - *err = WTAP_ERR_UNWRITABLE_ENCAP; - return FALSE; - } - - options_total_length = wtap_optionblock_get_option_write_size(block); - if (options_total_length > 0) - { - /* End-of-options tag */ - options_total_length += 4; - } - - /* write block header */ - bh.block_type = BLOCK_TYPE_IDB; - bh.block_total_length = (guint32)(sizeof(bh) + sizeof(idb) + options_total_length + 4); - - if (!wtap_dump_file_write(wdh, &bh, sizeof bh, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh; - - /* write block fixed content */ - idb.linktype = mand_data->link_type; - idb.reserved = 0; - idb.snaplen = mand_data->snap_len; - - if (!wtap_dump_file_write(wdh, &idb, sizeof idb, err)) - return FALSE; - wdh->bytes_dumped += sizeof idb; - - if (!wtap_optionblock_write_options(wdh, block, options_total_length, err)) - return FALSE; - - /* write block footer */ - if (!wtap_dump_file_write(wdh, &bh.block_total_length, - sizeof bh.block_total_length, err)) - return FALSE; - wdh->bytes_dumped += sizeof bh.block_total_length; - return TRUE; -} - static void idb_free_mand(wtap_optionblock_t block) { guint j; @@ -1040,7 +651,6 @@ void wtap_opttypes_initialize(void) "SHB", /* name */ "Section Header Block", /* description */ shb_create, /* create */ - shb_write, /* write */ NULL, /* free_mand */ shb_copy_mand, /* copy_mand */ }; @@ -1049,7 +659,6 @@ void wtap_opttypes_initialize(void) "NRB", /* name */ "Name Resolution Block", /* description */ nrb_create, /* create */ - NULL, /* write */ NULL, /* free_mand */ NULL, /* copy_mand */ }; @@ -1058,7 +667,6 @@ void wtap_opttypes_initialize(void) "ISB", /* name */ "Interface Statistics Block", /* description */ isb_create, /* create */ - isb_write, /* write */ NULL, /* free_mand */ isb_copy_mand, /* copy_mand */ }; @@ -1067,7 +675,6 @@ void wtap_opttypes_initialize(void) "IDB", /* name */ "Interface Description Block", /* description */ idb_create, /* create */ - idb_write, /* write */ idb_free_mand, /* free_mand */ idb_copy_mand, /* copy_mand */ }; diff --git a/wiretap/wtap_opttypes.h b/wiretap/wtap_opttypes.h index 56d0c29e19..cffbdc83c0 100644 --- a/wiretap/wtap_opttypes.h +++ b/wiretap/wtap_opttypes.h @@ -72,17 +72,11 @@ struct wtap_dumper; typedef void (*wtap_block_create_func)(wtap_optionblock_t block); typedef void (*wtap_mand_free_func)(wtap_optionblock_t block); typedef void (*wtap_mand_copy_func)(wtap_optionblock_t dest_block, wtap_optionblock_t src_block); -typedef gboolean (*wtap_write_func)(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err); - -typedef guint32 (*wtap_opttype_option_write_size)(wtap_option_type* data); /**< does the option have data worth writing (Ex string option != NULL */ -typedef gboolean (*wtap_opttype_option_write)(struct wtap_dumper* wdh, wtap_option_type* data, int *err); /**< does the option have data worth writing (Ex string option != NULL */ typedef struct wtap_optblock_reg { const char *name; /**< name of option */ const char *description; /**< human-readable description of option */ wtap_opttype_e type; /**< type of that option */ - wtap_opttype_option_write_size write_size_func; /**< Size of option in file (0 to not write option) */ - wtap_opttype_option_write write_func; /**< write option data to dumper */ wtap_option_type option; /**< pointer to variable storing the value */ wtap_option_type default_val; /**< the default value of the option */ } wtap_optblock_reg_t; @@ -221,34 +215,12 @@ WS_DLL_PUBLIC int wtap_optionblock_get_option_custom(wtap_optionblock_t block, g */ void wtap_optionblock_copy_options(wtap_optionblock_t dest_block, wtap_optionblock_t src_block); -/** Write an option block - * - * Will write all mandatory data as well as "valid" options - * - * @param[in] wdh writing assistant - * @param[in] block Block to be written - * @param[in] err Any errors that occurred - * @return TRUE if successful, FALSE will populate err - */ -gboolean wtap_optionblock_write(struct wtap_dumper *wdh, wtap_optionblock_t block, int *err); - -/* Some utility functions for option types */ - -guint32 wtap_opttype_write_size_string(wtap_option_type* data); -gboolean wtap_opttype_write_data_string(struct wtap_dumper* wdh, wtap_option_type* data, int *err); - -/* if option value = 0, write size = 0, otherwise 4 */ -guint32 wtap_opttype_write_uint8_not0(wtap_option_type* data); -gboolean wtap_opttype_write_data_uint8(struct wtap_dumper* wdh, wtap_option_type* data, int *err); -/* if option value = 0, write size = 0, otherwise 8 */ -guint32 wtap_opttype_write_uint64_not0(wtap_option_type* data); -/* if option value = -1 (0xFFFFFFFFFFFFFFFF), write size = 0, otherwise 8 */ -guint32 wtap_opttype_write_uint64_not_minus1(wtap_option_type* data); -gboolean wtap_opttype_write_data_uint64(struct wtap_dumper* wdh, wtap_option_type* data, int *err); +typedef void (*wtap_optionblock_foreach_func)(wtap_optionblock_t block, guint option_id, wtap_opttype_e option_type, wtap_option_type* option, void* user_data); +void wtap_optionblock_foreach_option(wtap_optionblock_t block, wtap_optionblock_foreach_func func, void* user_data); WS_DLL_PUBLIC int wtap_opttype_register_custom_block_type(const char* name, const char* description, wtap_block_create_func create, - wtap_write_func write_func, wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand); + wtap_mand_free_func free_mand, wtap_mand_copy_func copy_mand); #endif /* WTAP_OPT_TYPES_H */ |