diff options
author | Guy Harris <guy@alum.mit.edu> | 2014-10-06 18:00:57 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2014-10-07 01:01:59 +0000 |
commit | 670ebda4a6af0d30e033b0af48cfd15ce52c10eb (patch) | |
tree | b092e44c944c4eb7566964da4cfb914e6002bd6d /wiretap/netmon.c | |
parent | 6397ad43c2374ebde388041f2bd7ac925606a51e (diff) |
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wiretap/netmon.c')
-rw-r--r-- | wiretap/netmon.c | 48 |
1 files changed, 9 insertions, 39 deletions
diff --git a/wiretap/netmon.c b/wiretap/netmon.c index f2e7e5a5c5..65da2d8805 100644 --- a/wiretap/netmon.c +++ b/wiretap/netmon.c @@ -188,7 +188,6 @@ static gboolean netmon_dump_close(wtap_dumper *wdh, int *err); int netmon_open(wtap *wth, int *err, gchar **err_info) { - int bytes_read; char magic[MAGIC_SIZE]; struct netmon_hdr hdr; int file_type; @@ -205,10 +204,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info) /* Read in the string that should be at the start of a Network * Monitor file */ errno = WTAP_ERR_CANT_READ; - bytes_read = file_read(magic, MAGIC_SIZE, wth->fh); - if (bytes_read != MAGIC_SIZE) { - *err = file_error(wth->fh, err_info); - if (*err != 0 && *err != WTAP_ERR_SHORT_READ) + if (!wtap_read_bytes(wth->fh, magic, MAGIC_SIZE, err, err_info)) { + if (*err != WTAP_ERR_SHORT_READ) return -1; return 0; } @@ -220,13 +217,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info) /* Read the rest of the header. */ errno = WTAP_ERR_CANT_READ; - bytes_read = file_read(&hdr, sizeof hdr, wth->fh); - if (bytes_read != sizeof hdr) { - *err = file_error(wth->fh, err_info); - if (*err == 0) - *err = WTAP_ERR_SHORT_READ; + if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info)) return -1; - } switch (hdr.ver_major) { @@ -367,11 +359,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info) return -1; } errno = WTAP_ERR_CANT_READ; - bytes_read = file_read(frame_table, frame_table_length, wth->fh); - if ((guint32)bytes_read != frame_table_length) { - *err = file_error(wth->fh, err_info); - if (*err == 0) - *err = WTAP_ERR_SHORT_READ; + if (!wtap_read_bytes(wth->fh, frame_table, frame_table_length, + err, err_info)) { g_free(frame_table); return -1; } @@ -474,7 +463,6 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, struct netmonrec_1_x_hdr hdr_1_x; struct netmonrec_2_x_hdr hdr_2_x; } hdr; - int bytes_read; gint64 delta = 0; /* signed - frame times can be before the nominal start */ gint64 t; time_t secs; @@ -502,15 +490,8 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, break; } errno = WTAP_ERR_CANT_READ; - - bytes_read = file_read(&hdr, hdr_size, fh); - if (bytes_read != hdr_size) { - *err = file_error(fh, err_info); - if (*err == 0 && bytes_read != 0) { - *err = WTAP_ERR_SHORT_READ; - } + if (!wtap_read_bytes_or_eof(fh, &hdr, hdr_size, err, err_info)) return FAILURE; - } switch (netmon->version_major) { @@ -680,14 +661,8 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, } errno = WTAP_ERR_CANT_READ; - bytes_read = file_read(&trlr, trlr_size, fh); - if (bytes_read != trlr_size) { - *err = file_error(fh, err_info); - if (*err == 0 && bytes_read != 0) { - *err = WTAP_ERR_SHORT_READ; - } + if (!wtap_read_bytes(fh, &trlr, trlr_size, err, err_info)) return FAILURE; - } network = pletoh16(trlr.trlr_2_1.network); if ((network & 0xF000) == NETMON_NET_PCAP_BASE) { @@ -899,17 +874,12 @@ netmon_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info) { struct netmon_atm_hdr atm_phdr; - int bytes_read; guint16 vpi, vci; errno = WTAP_ERR_CANT_READ; - bytes_read = file_read(&atm_phdr, sizeof (struct netmon_atm_hdr), fh); - if (bytes_read != sizeof (struct netmon_atm_hdr)) { - *err = file_error(fh, err_info); - if (*err == 0) - *err = WTAP_ERR_SHORT_READ; + if (!wtap_read_bytes(fh, &atm_phdr, sizeof (struct netmon_atm_hdr), + err, err_info)) return FALSE; - } vpi = g_ntohs(atm_phdr.vpi); vci = g_ntohs(atm_phdr.vci); |