- struct idb idb;
- struct option option;
- guint32 block_total_length;
- const guint32 padding = 0;
-
- block_total_length = sizeof(struct idb) + sizeof(guint32);
- if (strlen(name) > 0) {
- block_total_length += sizeof(struct option) + ADD_PADDING(strlen(name) + 1);
- }
- if (strlen(filter) > 0) {
- block_total_length += sizeof(struct option) + ADD_PADDING(strlen(filter) + 1);
- }
- if ((strlen(name) > 0) || (strlen(filter) > 0)) {
- block_total_length += sizeof(struct option);
- }
- idb.block_type = INTERFACE_DESCRIPTION_BLOCK_TYPE;
- idb.block_total_length = block_total_length;
- idb.link_type = link_type;
- idb.reserved = 0;
- idb.snap_len = snap_len;
- WRITE_DATA(fp, &idb, sizeof(struct idb), *bytes_written, err);
- /* write the options */
- if (strlen(name) > 0) {
- option.type = IDB_NAME;
- option.value_length = (guint16)(strlen(name) + 1);
- WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err);
- WRITE_DATA(fp, name, strlen(name) + 1, *bytes_written, err);
- if ((strlen(name) + 1) % 4) {
- WRITE_DATA(fp, &padding, 4 - (strlen(name) + 1) % 4 , *bytes_written, err);
- }
- }
- if (strlen(filter) > 0) {
- option.type = IDB_FILTER;
- option.value_length = (guint16)(strlen(filter) + 1);
- WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err);
- WRITE_DATA(fp, filter, strlen(filter) + 1, *bytes_written, err);
- if ((strlen(filter) + 1) % 4) {
- WRITE_DATA(fp, &padding, 4 - (strlen(filter) + 1) % 4 , *bytes_written, err);
- }
- }
- if ((strlen(name) > 0) || (strlen(filter) > 0)) {
- option.type = OPT_ENDOFOPT;
- option.value_length = 0;
- WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err);
- }
- /* write the trailing Block Total Length */
- WRITE_DATA(fp, &block_total_length, sizeof(guint32), *bytes_written, err);
- return TRUE;
+ struct idb idb;
+ struct option option;
+ guint32 block_total_length;
+ const guint32 padding = 0;
+ gboolean have_options = FALSE;
+
+ block_total_length = (guint32)(sizeof(struct idb) + sizeof(guint32));
+ /* 01 - OPT_COMMENT */
+ if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ (guint16)ADD_PADDING(strlen(comment)));
+ have_options = TRUE;
+ }
+
+ /* 02 - IDB_NAME */
+ if ((name != NULL) && (strlen(name) > 0) && (strlen(name) < G_MAXUINT16)) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ (guint16)ADD_PADDING(strlen(name)));
+ have_options = TRUE;
+ }
+
+ /* 03 - IDB_DESCRIPTION */
+ if ((descr != NULL) && (strlen(descr) > 0) && (strlen(descr) < G_MAXUINT16)) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ (guint16)ADD_PADDING(strlen(descr)));
+ have_options = TRUE;
+ }
+
+ /* 08 - IDB_IF_SPEED */
+ if (if_speed != 0) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ sizeof(guint64));
+ have_options = TRUE;
+ }
+
+ /* 09 - IDB_TSRESOL */
+ if (tsresol != 0) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ sizeof(struct option));
+ have_options = TRUE;
+ }
+
+ /* 11 - IDB_FILTER */
+ if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < G_MAXUINT16)) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ (guint16)(ADD_PADDING(strlen(filter)+ 1)));
+ have_options = TRUE;
+ }
+
+ /* 12 - IDB_OS */
+ if ((os != NULL) && (strlen(os) > 0) && (strlen(os) < G_MAXUINT16)) {
+ block_total_length += (guint32)(sizeof(struct option) +
+ (guint16)ADD_PADDING(strlen(os)));
+ have_options = TRUE;
+ }
+
+ /* If we have options add size of end-of-options */
+ if (have_options) {
+ block_total_length += (guint32)sizeof(struct option);
+ }
+
+ /* write block header */
+ idb.block_type = INTERFACE_DESCRIPTION_BLOCK_TYPE;
+ idb.block_total_length = block_total_length;
+ idb.link_type = link_type;
+ idb.reserved = 0;
+ idb.snap_len = snap_len;
+ if (!write_func(write_data_info, (const guint8*)&idb, sizeof(struct idb), bytes_written, err))
+ return FALSE;
+
+ /* 01 - OPT_COMMENT - write comment string if applicable */
+ if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) {
+ option.type = OPT_COMMENT;
+ option.value_length = (guint16)strlen(comment);
+
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)comment, (int) strlen(comment), bytes_written, err))
+ return FALSE;
+
+ if (strlen(comment) % 4) {
+ if (!write_func(write_data_info, (const guint8*)&padding, 4 - strlen(comment) % 4, bytes_written, err))
+ return FALSE;
+ }
+ }
+
+ /* 02 - IDB_NAME - write interface name string if applicable */
+ if ((name != NULL) && (strlen(name) > 0) && (strlen(name) < G_MAXUINT16)) {
+ option.type = IDB_NAME;
+ option.value_length = (guint16)strlen(name);
+
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)name, (int) strlen(name), bytes_written, err))
+ return FALSE;
+
+ if (strlen(name) % 4) {
+ if (!write_func(write_data_info, (const guint8*)&padding, 4 - strlen(name) % 4, bytes_written, err))
+ return FALSE;
+ }
+ }
+
+ /* 03 - IDB_DESCRIPTION */
+ /* write interface description string if applicable */
+ if ((descr != NULL) && (strlen(descr) > 0) && (strlen(descr) < G_MAXUINT16)) {
+ option.type = IDB_DESCRIPTION;
+ option.value_length = (guint16)strlen(descr);
+
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)descr, (int) strlen(descr), bytes_written, err))
+ return FALSE;
+
+ if (strlen(descr) % 4) {
+ if (!write_func(write_data_info, (const guint8*)&padding, 4 - strlen(descr) % 4, bytes_written, err))
+ return FALSE;
+ }
+ }
+
+ /* 08 - IDB_IF_SPEED */
+ if (if_speed != 0) {
+ option.type = IDB_IF_SPEED;
+ option.value_length = sizeof(guint64);
+
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)&if_speed, sizeof(guint64), bytes_written, err))
+ return FALSE;
+ }
+
+ /* 09 - IDB_TSRESOL */
+ if (tsresol != 0) {
+ option.type = IDB_TSRESOL;
+ option.value_length = sizeof(guint8);
+
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)&tsresol, sizeof(guint8), bytes_written, err))
+ return FALSE;
+
+ if (!write_func(write_data_info, (const guint8*)&padding, 3, bytes_written, err))
+ return FALSE;
+ }
+
+ /* 11 - IDB_FILTER - write filter string if applicable
+ * We only write version 1 of the filter, libpcap string
+ */
+ if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < G_MAXUINT16)) {
+ option.type = IDB_FILTER;
+ option.value_length = (guint16)(strlen(filter) + 1 );
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+
+ /* The first byte of the Option Data keeps a code of the filter used, 0 = lipbpcap filter string */
+ if (!write_func(write_data_info, (const guint8*)&padding, 1, bytes_written, err))
+ return FALSE;
+ if (!write_func(write_data_info, (const guint8*)filter, (int) strlen(filter), bytes_written, err))
+ return FALSE;
+ if ((strlen(filter) + 1) % 4) {
+ if (!write_func(write_data_info, (const guint8*)&padding, 4 - (strlen(filter) + 1) % 4, bytes_written, err))
+ return FALSE;
+ }
+ }
+
+ /* 12 - IDB_OS - write os string if applicable */
+ if ((os != NULL) && (strlen(os) > 0) && (strlen(os) < G_MAXUINT16)) {
+ option.type = IDB_OS;
+ option.value_length = (guint16)strlen(os);
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+ if (!write_func(write_data_info, (const guint8*)os, (int) strlen(os), bytes_written, err))
+ return FALSE;
+ if (strlen(os) % 4) {
+ if (!write_func(write_data_info, (const guint8*)&padding, 4 - strlen(os) % 4, bytes_written, err))
+ return FALSE;
+ }
+ }
+
+ if (have_options) {
+ /* write end of options */
+ option.type = OPT_ENDOFOPT;
+ option.value_length = 0;
+ if (!write_func(write_data_info, (const guint8*)&option, sizeof(struct option), bytes_written, err))
+ return FALSE;
+ }
+
+ /* write the trailing Block Total Length */
+ return write_func(write_data_info, (const guint8*)&block_total_length, sizeof(guint32), bytes_written, err);