X-Git-Url: http://git.samba.org/?a=blobdiff_plain;f=pcapio.c;h=ce2a90d0c1f70145003e7deeda9f8688b6a980be;hb=46be2049f366125c5320fb8a14e5662ecf25376f;hp=b06b2a8510dde61c510d6e8700d7c3d655b20a32;hpb=197ac6704ddbcebc42281ea421792b75ddda2f0d;p=metze%2Fwireshark%2Fwip.git diff --git a/pcapio.c b/pcapio.c index b06b2a8510..ce2a90d0c1 100644 --- a/pcapio.c +++ b/pcapio.c @@ -16,8 +16,6 @@ * * Libpcap's pcap_dump() also doesn't return any error indications. * - * $Id$ - * * Wireshark - Network traffic analyzer * By Gerald Combs * Copyright 1998 Gerald Combs @@ -40,7 +38,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "config.h" +#include #include #include @@ -119,7 +117,7 @@ struct shb { }; #define SECTION_HEADER_BLOCK_TYPE 0x0A0D0D0A -/* Interface Decription Block without options and trailing Block Total Length */ +/* Interface Description Block without options and trailing Block Total Length */ struct idb { guint32 block_type; guint32 block_total_length; @@ -178,32 +176,36 @@ struct option { #define ISB_USRDELIV 8 #define ADD_PADDING(x) ((((x) + 3) >> 2) << 2) -#define WRITE_DATA(file_pointer, data_pointer, data_length, written_length, error_pointer) \ -{ \ - do { \ - size_t nwritten; \ - \ - nwritten = fwrite(data_pointer, data_length, 1, file_pointer); \ - if (nwritten != 1) { \ - if (ferror(file_pointer)) { \ - *error_pointer = errno; \ - } else { \ - *error_pointer = 0; \ - } \ - return FALSE; \ - } \ - written_length += (long)nwritten; \ - } while (0); \ +/* Write to capture file */ +static gboolean +write_to_file(FILE* pfile, const guint8* data, size_t data_length, + guint64 *bytes_written, int *err) +{ + size_t nwritten; + + nwritten = fwrite(data, data_length, 1, pfile); + if (nwritten != 1) { + if (ferror(pfile)) { + *err = errno; + } else { + *err = 0; + } + return FALSE; + } + + (*bytes_written) += data_length; + return TRUE; } +/* Writing pcap files */ + /* Write the file header to a dump file. Returns TRUE on success, FALSE on failure. Sets "*err" to an error code, or 0 for a short write, on failure*/ gboolean -libpcap_write_file_header(FILE *fp, int linktype, int snaplen, gboolean ts_nsecs, long *bytes_written, int *err) +libpcap_write_file_header(FILE* pfile, int linktype, int snaplen, gboolean ts_nsecs, guint64 *bytes_written, int *err) { struct pcap_hdr file_hdr; - size_t nwritten; file_hdr.magic = ts_nsecs ? PCAP_NSEC_MAGIC : PCAP_MAGIC; /* current "libpcap" format is 2.4 */ @@ -213,100 +215,104 @@ libpcap_write_file_header(FILE *fp, int linktype, int snaplen, gboolean ts_nsecs file_hdr.sigfigs = 0; /* unknown, but also apparently unused */ file_hdr.snaplen = snaplen; file_hdr.network = linktype; - nwritten = fwrite(&file_hdr, sizeof(file_hdr), 1, fp); - if (nwritten != 1) { - if (ferror(fp)) - *err = errno; - else - *err = 0; /* short write */ - return FALSE; - } - *bytes_written += sizeof(file_hdr); - return TRUE; + return write_to_file(pfile, (const guint8*)&file_hdr, sizeof(file_hdr), bytes_written, err); } /* Write a record for a packet to a dump file. Returns TRUE on success, FALSE on failure. */ gboolean -libpcap_write_packet(FILE *fp, - guint32 sec, guint32 usec, +libpcap_write_packet(FILE* pfile, + time_t sec, guint32 usec, guint32 caplen, guint32 len, - const unsigned char *pd, - long *bytes_written, int *err) + const guint8 *pd, + guint64 *bytes_written, int *err) { struct pcaprec_hdr rec_hdr; - size_t nwritten; - rec_hdr.ts_sec = sec; + rec_hdr.ts_sec = (guint32)sec; /* Y2.038K issue in pcap format.... */ rec_hdr.ts_usec = usec; rec_hdr.incl_len = caplen; rec_hdr.orig_len = len; - nwritten = fwrite(&rec_hdr, sizeof(rec_hdr), 1, fp); - if (nwritten != 1) { - if (ferror(fp)) - *err = errno; - else - *err = 0; /* short write */ + if (!write_to_file(pfile, (const guint8*)&rec_hdr, sizeof(rec_hdr), bytes_written, err)) return FALSE; + + return write_to_file(pfile, pd, caplen, bytes_written, err); +} + +/* Writing pcap-ng files */ + +static guint32 +pcapng_count_string_option(const char *option_value) +{ + if ((option_value != NULL) && (strlen(option_value) > 0) && (strlen(option_value) < G_MAXUINT16)) { + /* There's a value to write; get its length */ + return (guint32)(sizeof(struct option) + + (guint16)ADD_PADDING(strlen(option_value))); } - *bytes_written += sizeof rec_hdr; + return 0; /* nothing to write */ +} - nwritten = fwrite(pd, caplen, 1, fp); - if (nwritten != 1) { - if (ferror(fp)) - *err = errno; - else - *err = 0; /* short write */ - return FALSE; +static gboolean +pcapng_write_string_option(FILE* pfile, + guint16 option_type, const char *option_value, + guint64 *bytes_written, int *err) +{ + size_t option_value_length; + struct option option; + const guint32 padding = 0; + + if (option_value == NULL) + return TRUE; /* nothing to write */ + option_value_length = strlen(option_value); + if ((option_value_length > 0) && (option_value_length < G_MAXUINT16)) { + /* something to write */ + option.type = option_type; + option.value_length = (guint16)option_value_length; + + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)option_value, (int) option_value_length, bytes_written, err)) + return FALSE; + + if (option_value_length % 4) { + if (!write_to_file(pfile, (const guint8*)&padding, 4 - option_value_length % 4, bytes_written, err)) + return FALSE; + } } - *bytes_written += caplen; return TRUE; } gboolean -libpcap_write_session_header_block(FILE *fp, - const char *comment, - const char *hw, - const char *os, - const char *appname, - guint64 section_length, - long *bytes_written, - int *err) +pcapng_write_session_header_block(FILE* pfile, + const char *comment, + const char *hw, + const char *os, + const char *appname, + guint64 section_length, + guint64 *bytes_written, + int *err) { struct shb shb; struct option option; guint32 block_total_length; - const guint32 padding = 0; - gboolean have_options = FALSE; + guint32 options_length; /* Size of base header */ block_total_length = sizeof(struct shb) + sizeof(guint32); - if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(comment)); - have_options = TRUE; - } - if ((hw != NULL) && (strlen(hw) > 0) && (strlen(hw) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(hw)); - have_options = TRUE; - } - if ((os != NULL) && (strlen(os) > 0) && (strlen(os) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(os)); - have_options = TRUE; - } - if ((appname != NULL) && (strlen(appname) > 0) && (strlen(appname) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(appname)); - have_options = TRUE; - } + options_length = 0; + options_length += pcapng_count_string_option(comment); + options_length += pcapng_count_string_option(hw); + options_length += pcapng_count_string_option(os); + options_length += pcapng_count_string_option(appname); /* If we have options add size of end-of-options */ - if (have_options) { - block_total_length += sizeof(struct option); + if (options_length != 0) { + options_length += (guint32)sizeof(struct option); } + block_total_length += options_length; + /* write shb header */ shb.block_type = SECTION_HEADER_BLOCK_TYPE; shb.block_total_length = block_total_length; @@ -314,132 +320,92 @@ libpcap_write_session_header_block(FILE *fp, shb.major_version = PCAPNG_MAJOR_VERSION; shb.minor_version = PCAPNG_MINOR_VERSION; shb.section_length = section_length; - WRITE_DATA(fp, &shb, sizeof(struct shb), *bytes_written, err); - - if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) { - /* write opt_comment options */ - option.type = OPT_COMMENT; - option.value_length = (guint16)strlen(comment); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, comment, strlen(comment), *bytes_written, err); - if (strlen(comment) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(comment) % 4, *bytes_written, err); - } - } - if ((hw != NULL) && (strlen(hw) > 0) && (strlen(hw) < G_MAXUINT16)) { - /* write shb_hardware options */ - option.type = SHB_HARDWARE; - option.value_length = (guint16)strlen(hw); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, hw, strlen(hw), *bytes_written, err); - if ((strlen(hw) + 1) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(hw) % 4, *bytes_written, err); - } - } - if ((os != NULL) && (strlen(os) > 0) && (strlen(os) < G_MAXUINT16)) { - /* write shb_os options */ - option.type = SHB_OS; - option.value_length = (guint16)strlen(os); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, os, strlen(os), *bytes_written, err); - if (strlen(os) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(os) % 4, *bytes_written, err); - } - } - if ((appname != NULL) && (strlen(appname) > 0) && (strlen(appname) < G_MAXUINT16)) { - /* write shb_userappl options */ - option.type = SHB_USERAPPL; - option.value_length = (guint16)strlen(appname); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, appname, strlen(appname), *bytes_written, err); - if (strlen(appname) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(appname) % 4, *bytes_written, err); - } - } - if (have_options) { + + if (!write_to_file(pfile, (const guint8*)&shb, sizeof(struct shb), bytes_written, err)) + return FALSE; + + if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment, + bytes_written, err)) + return FALSE; + if (!pcapng_write_string_option(pfile, SHB_HARDWARE, hw, + bytes_written, err)) + return FALSE; + if (!pcapng_write_string_option(pfile, SHB_OS, os, + bytes_written, err)) + return FALSE; + if (!pcapng_write_string_option(pfile, SHB_USERAPPL, appname, + bytes_written, err)) + return FALSE; + if (options_length != 0) { /* write end of options */ option.type = OPT_ENDOFOPT; option.value_length = 0; - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; } /* write the trailing block total length */ - WRITE_DATA(fp, &block_total_length, sizeof(guint32), *bytes_written, err); - return TRUE; + return write_to_file(pfile, (const guint8*)&block_total_length, sizeof(guint32), bytes_written, err); } gboolean -libpcap_write_interface_description_block(FILE *fp, - const char *comment, /* OPT_COMMENT 1 */ - const char *name, /* IDB_NAME 2 */ - const char *descr, /* IDB_DESCRIPTION 3 */ - const char *filter, /* IDB_FILTER 11 */ - const char *os, /* IDB_OS 12 */ - int link_type, - int snap_len, - long *bytes_written, - guint64 if_speed, /* IDB_IF_SPEED 8 */ - guint8 tsresol, /* IDB_TSRESOL 9 */ - int *err) +pcapng_write_interface_description_block(FILE* pfile, + const char *comment, /* OPT_COMMENT 1 */ + const char *name, /* IDB_NAME 2 */ + const char *descr, /* IDB_DESCRIPTION 3 */ + const char *filter, /* IDB_FILTER 11 */ + const char *os, /* IDB_OS 12 */ + int link_type, + int snap_len, + guint64 *bytes_written, + guint64 if_speed, /* IDB_IF_SPEED 8 */ + guint8 tsresol, /* IDB_TSRESOL 9 */ + int *err) { struct idb idb; struct option option; guint32 block_total_length; + guint32 options_length; const guint32 padding = 0; - gboolean have_options = FALSE; - block_total_length = sizeof(struct idb) + sizeof(guint32); + block_total_length = (guint32)(sizeof(struct idb) + sizeof(guint32)); + options_length = 0; /* 01 - OPT_COMMENT */ - if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(comment)); - have_options = TRUE; - } + options_length += pcapng_count_string_option(comment); /* 02 - IDB_NAME */ - if ((name != NULL) && (strlen(name) > 0) && (strlen(name) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(name)); - have_options = TRUE; - } + options_length += pcapng_count_string_option(name); /* 03 - IDB_DESCRIPTION */ - if ((descr != NULL) && (strlen(descr) > 0) && (strlen(descr) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(descr)); - have_options = TRUE; - } + options_length += pcapng_count_string_option(descr); /* 08 - IDB_IF_SPEED */ if (if_speed != 0) { - block_total_length += sizeof(struct option) + sizeof(guint64); - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(guint64)); } /* 09 - IDB_TSRESOL */ if (tsresol != 0) { - block_total_length += sizeof(struct option) + sizeof(struct option); - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(struct option)); } /* 11 - IDB_FILTER */ if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)(ADD_PADDING(strlen(filter)+ 1)); - have_options = TRUE; + /* No, this isn't a string, it has an extra type byte */ + options_length += (guint32)(sizeof(struct option) + + (guint16)(ADD_PADDING(strlen(filter)+ 1))); } /* 12 - IDB_OS */ - if ((os != NULL) && (strlen(os) > 0) && (strlen(os) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(os)); - have_options = TRUE; - } + options_length += pcapng_count_string_option(os); /* If we have options add size of end-of-options */ - if (have_options) { - block_total_length += sizeof(struct option); + if (options_length != 0) { + options_length += (guint32)sizeof(struct option); } + block_total_length += options_length; /* write block header */ idb.block_type = INTERFACE_DESCRIPTION_BLOCK_TYPE; @@ -447,123 +413,124 @@ libpcap_write_interface_description_block(FILE *fp, idb.link_type = link_type; idb.reserved = 0; idb.snap_len = snap_len; - WRITE_DATA(fp, &idb, sizeof(struct idb), *bytes_written, err); + if (!write_to_file(pfile, (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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, comment, strlen(comment), *bytes_written, err); - if (strlen(comment) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(comment) % 4 , *bytes_written, err); - } - } + if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment, + 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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, name, strlen(name), *bytes_written, err); - if (strlen(name) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(name) % 4 , *bytes_written, err); - } - } + if (!pcapng_write_string_option(pfile, IDB_NAME, name, + 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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, descr, strlen(descr), *bytes_written, err); - if (strlen(descr) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(descr) % 4 , *bytes_written, err); - } - } + if (!pcapng_write_string_option(pfile, IDB_DESCRIPTION, descr, + bytes_written, err)) + return FALSE; /* 08 - IDB_IF_SPEED */ if (if_speed != 0) { option.type = IDB_IF_SPEED; option.value_length = sizeof(guint64); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &if_speed, sizeof(guint64), *bytes_written, err); + + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &tsresol, sizeof(guint8), *bytes_written, err); - WRITE_DATA(fp, &padding, 3 , *bytes_written, err); + + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&tsresol, sizeof(guint8), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (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 + * We only write version 1 of the filter, pcapng string */ - if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < G_MAXUINT16)) { + if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < G_MAXUINT16 - 1)) { option.type = IDB_FILTER; option.value_length = (guint16)(strlen(filter) + 1 ); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); + if (!write_to_file(pfile, (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 */ - WRITE_DATA(fp, &padding, 1, *bytes_written, err); - WRITE_DATA(fp, filter, strlen(filter), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&padding, 1, bytes_written, err)) + return FALSE; + if (!write_to_file(pfile, (const guint8*)filter, (int) strlen(filter), bytes_written, err)) + return FALSE; if ((strlen(filter) + 1) % 4) { - WRITE_DATA(fp, &padding, 4 - (strlen(filter) + 1) % 4 , *bytes_written, err); + if (!write_to_file(pfile, (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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, os, strlen(os), *bytes_written, err); - if (strlen(os) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(os) % 4 , *bytes_written, err); - } - } + if (!pcapng_write_string_option(pfile, IDB_OS, os, + bytes_written, err)) + return FALSE; - if (have_options) { + if (options_length != 0) { /* write end of options */ option.type = OPT_ENDOFOPT; option.value_length = 0; - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; } /* write the trailing Block Total Length */ - WRITE_DATA(fp, &block_total_length, sizeof(guint32), *bytes_written, err); - return TRUE; + return write_to_file(pfile, (const guint8*)&block_total_length, sizeof(guint32), bytes_written, err); } /* Write a record for a packet to a dump file. Returns TRUE on success, FALSE on failure. */ gboolean -libpcap_write_enhanced_packet_block(FILE *fp, - guint32 sec, guint32 usec, - guint32 caplen, guint32 len, - guint32 interface_id, - guint ts_mul, - const unsigned char *pd, - guint32 flags, - long *bytes_written, - int *err) +pcapng_write_enhanced_packet_block(FILE* pfile, + const char *comment, + time_t sec, guint32 usec, + guint32 caplen, guint32 len, + guint32 interface_id, + guint ts_mul, + const guint8 *pd, + guint32 flags, + guint64 *bytes_written, + int *err) { struct epb epb; struct option option; guint32 block_total_length; guint64 timestamp; + guint32 options_length; const guint32 padding = 0; - block_total_length = sizeof(struct epb) + - ADD_PADDING(caplen) + - sizeof(guint32); + block_total_length = (guint32)(sizeof(struct epb) + + ADD_PADDING(caplen) + + sizeof(guint32)); + options_length = 0; + options_length += pcapng_count_string_option(comment); if (flags != 0) { - block_total_length += 2 * sizeof(struct option) + sizeof(guint32); + options_length += (guint32)(sizeof(struct option) + + sizeof(guint32)); + } + /* If we have options add size of end-of-options */ + if (options_length != 0) { + options_length += (guint32)sizeof(struct option); } + block_total_length += options_length; timestamp = (guint64)sec * ts_mul + (guint64)usec; epb.block_type = ENHANCED_PACKET_BLOCK_TYPE; epb.block_total_length = block_total_length; @@ -572,34 +539,46 @@ libpcap_write_enhanced_packet_block(FILE *fp, epb.timestamp_low = (guint32)(timestamp & 0xffffffff); epb.captured_len = caplen; epb.packet_len = len; - WRITE_DATA(fp, &epb, sizeof(struct epb), *bytes_written, err); - WRITE_DATA(fp, pd, caplen, *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&epb, sizeof(struct epb), bytes_written, err)) + return FALSE; + if (!write_to_file(pfile, pd, caplen, bytes_written, err)) + return FALSE; if (caplen % 4) { - WRITE_DATA(fp, &padding, 4 - caplen % 4, *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&padding, 4 - caplen % 4, bytes_written, err)) + return FALSE; } + if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment, + bytes_written, err)) + return FALSE; if (flags != 0) { option.type = EPB_FLAGS; option.value_length = sizeof(guint32); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &flags, sizeof(guint32), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + if (!write_to_file(pfile, (const guint8*)&flags, sizeof(guint32), bytes_written, err)) + return FALSE; + } + if (options_length != 0) { + /* write end of options */ option.type = OPT_ENDOFOPT; option.value_length = 0; - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - } - WRITE_DATA(fp, &block_total_length, sizeof(guint32), *bytes_written, err); - return TRUE; + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + } + + return write_to_file(pfile, (const guint8*)&block_total_length, sizeof(guint32), bytes_written, err); } gboolean -libpcap_write_interface_statistics_block(FILE *fp, - guint32 interface_id, - long *bytes_written, - const char *comment, /* OPT_COMMENT 1 */ - guint64 isb_starttime, /* ISB_STARTTIME 2 */ - guint64 isb_endtime, /* ISB_ENDTIME 3 */ - guint64 isb_ifrecv, /* ISB_IFRECV 4 */ - guint64 isb_ifdrop, /* ISB_IFDROP 5 */ - int *err) +pcapng_write_interface_statistics_block(FILE* pfile, + guint32 interface_id, + guint64 *bytes_written, + const char *comment, /* OPT_COMMENT 1 */ + guint64 isb_starttime, /* ISB_STARTTIME 2 */ + guint64 isb_endtime, /* ISB_ENDTIME 3 */ + guint64 isb_ifrecv, /* ISB_IFRECV 4 */ + guint64 isb_ifdrop, /* ISB_IFDROP 5 */ + int *err) { struct isb isb; #ifdef _WIN32 @@ -609,9 +588,9 @@ libpcap_write_interface_statistics_block(FILE *fp, #endif struct option option; guint32 block_total_length; + guint32 options_length; guint64 timestamp; - gboolean have_options = FALSE; - const guint32 padding = 0; + #ifdef _WIN32 /* * Current time, represented as 100-nanosecond intervals since @@ -637,7 +616,7 @@ libpcap_write_interface_statistics_block(FILE *fp, * Subtract difference, in microseconds, between January 1, 1601 * 00:00:00 UTC and January 1, 1970, 00:00:00 UTC. */ - timestamp -= G_GINT64_CONSTANT(11644473600000000U); + timestamp -= G_GUINT64_CONSTANT(11644473600000000); #else /* * Current time, represented as seconds and microseconds since @@ -651,51 +630,44 @@ libpcap_write_interface_statistics_block(FILE *fp, timestamp = (guint64)(now.tv_sec) * 1000000 + (guint64)(now.tv_usec); #endif - block_total_length = sizeof(struct isb) + sizeof(guint32); + block_total_length = (guint32)(sizeof(struct isb) + sizeof(guint32)); + options_length = 0; if (isb_ifrecv != G_MAXUINT64) { - block_total_length += sizeof(struct option) + sizeof(guint64); - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(guint64)); } if (isb_ifdrop != G_MAXUINT64) { - block_total_length += sizeof(struct option) + sizeof(guint64); - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(guint64)); } /* OPT_COMMENT */ - if ((comment != NULL) && (strlen(comment) > 0) && (strlen(comment) < G_MAXUINT16)) { - block_total_length += sizeof(struct option) + - (guint16)ADD_PADDING(strlen(comment)); - have_options = TRUE; - } + options_length += pcapng_count_string_option(comment); if (isb_starttime !=0) { - block_total_length += sizeof(struct option) + sizeof(guint64); /* ISB_STARTTIME */ - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(guint64)); /* ISB_STARTTIME */ } if (isb_endtime !=0) { - block_total_length += sizeof(struct option) + sizeof(guint64); /* ISB_ENDTIME */ - have_options = TRUE; + options_length += (guint32)(sizeof(struct option) + + sizeof(guint64)); /* ISB_ENDTIME */ } /* If we have options add size of end-of-options */ - if (have_options) { - block_total_length += sizeof(struct option); + if (options_length != 0) { + options_length += (guint32)sizeof(struct option); } + block_total_length += options_length; isb.block_type = INTERFACE_STATISTICS_BLOCK_TYPE; isb.block_total_length = block_total_length; isb.interface_id = interface_id; isb.timestamp_high = (guint32)((timestamp>>32) & 0xffffffff); isb.timestamp_low = (guint32)(timestamp & 0xffffffff); - WRITE_DATA(fp, &isb, sizeof(struct isb), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&isb, sizeof(struct isb), bytes_written, err)) + return FALSE; /* 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); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, comment, strlen(comment), *bytes_written, err); - if (strlen(comment) % 4) { - WRITE_DATA(fp, &padding, 4 - strlen(comment) % 4 , *bytes_written, err); - } - } + if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment, + bytes_written, err)) + return FALSE; if (isb_starttime !=0) { guint32 high, low; @@ -704,9 +676,14 @@ libpcap_write_interface_statistics_block(FILE *fp, option.value_length = sizeof(guint64); high = (guint32)((isb_starttime>>32) & 0xffffffff); low = (guint32)(isb_starttime & 0xffffffff); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &high, sizeof(guint32), *bytes_written, err); - WRITE_DATA(fp, &low, sizeof(guint32), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&high, sizeof(guint32), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&low, sizeof(guint32), bytes_written, err)) + return FALSE; } if (isb_endtime !=0) { guint32 high, low; @@ -715,43 +692,53 @@ libpcap_write_interface_statistics_block(FILE *fp, option.value_length = sizeof(guint64); high = (guint32)((isb_endtime>>32) & 0xffffffff); low = (guint32)(isb_endtime & 0xffffffff); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &high, sizeof(guint32), *bytes_written, err); - WRITE_DATA(fp, &low, sizeof(guint32), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&high, sizeof(guint32), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&low, sizeof(guint32), bytes_written, err)) + return FALSE; } if (isb_ifrecv != G_MAXUINT64) { option.type = ISB_IFRECV; option.value_length = sizeof(guint64); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &isb_ifrecv, sizeof(guint64), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&isb_ifrecv, sizeof(guint64), bytes_written, err)) + return FALSE; } if (isb_ifdrop != G_MAXUINT64) { option.type = ISB_IFDROP; option.value_length = sizeof(guint64); - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); - WRITE_DATA(fp, &isb_ifdrop, sizeof(guint64), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; + + if (!write_to_file(pfile, (const guint8*)&isb_ifdrop, sizeof(guint64), bytes_written, err)) + return FALSE; } - if (have_options) { + if (options_length != 0) { /* write end of options */ option.type = OPT_ENDOFOPT; option.value_length = 0; - WRITE_DATA(fp, &option, sizeof(struct option), *bytes_written, err); + if (!write_to_file(pfile, (const guint8*)&option, sizeof(struct option), bytes_written, err)) + return FALSE; } - WRITE_DATA(fp, &block_total_length, sizeof(guint32), *bytes_written, err); - - return TRUE; + return write_to_file(pfile, (const guint8*)&block_total_length, sizeof(guint32), bytes_written, err); } /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * * Local variables: - * c-basic-offset: 4 + * c-basic-offset: 8 * tab-width: 8 * indent-tabs-mode: nil * End: * - * vi: set shiftwidth=4 tabstop=8 expandtab: - * :indentSize=4:tabSize=8:noTabs=true: + * vi: set shiftwidth=8 tabstop=8 expandtab: + * :indentSize=8:tabSize=8:noTabs=true: */