diff --git a/wiretap/ascend.c b/wiretap/ascend.c index 28b69da107..25e75546e8 100644 --- a/wiretap/ascend.c +++ b/wiretap/ascend.c @@ -1,6 +1,6 @@ /* ascend.c * - * $Id: ascend.c,v 1.26 2001/11/13 23:55:43 gram Exp $ + * $Id: ascend.c,v 1.27 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -29,6 +29,8 @@ #include "ascend-int.h" #include "file_wrappers.h" +#include + #ifdef HAVE_SYS_STAT_H #include #endif @@ -112,8 +114,7 @@ static void ascend_close(wtap *wth); it sets "wth->capture.ascend->next_packet_seek_start" to the point at which the seek pointer should be set before this routine is called to find the packet *after* the packet it finds. */ -/* XXX - Handle I/O errors. */ -static long ascend_seek(wtap *wth, int max_seek) +static long ascend_seek(wtap *wth, int max_seek, int *err) { int byte, bytes_read = 0; long date_off = -1, cur_off, packet_off; @@ -125,6 +126,11 @@ static long ascend_seek(wtap *wth, int max_seek) if (x_level >= ASCEND_X_SIZE) { /* At what offset are we now? */ cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } /* Back up over the header we just read; that's where a read of this packet should start. */ @@ -139,6 +145,11 @@ static long ascend_seek(wtap *wth, int max_seek) if (r_level >= ASCEND_R_SIZE) { /* At what offset are we now? */ cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } /* Back up over the header we just read; that's where a read of this packet should start. */ @@ -152,7 +163,14 @@ static long ascend_seek(wtap *wth, int max_seek) w1_level++; if (w1_level >= ASCEND_W1_SIZE) { /* Get the offset at which the "Date:" header started. */ - date_off = file_tell(wth->fh) - ASCEND_W1_SIZE; + cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } + + date_off = cur_off - ASCEND_W1_SIZE; } } else { w1_level = 0; @@ -162,6 +180,12 @@ static long ascend_seek(wtap *wth, int max_seek) if (w2_level >= ASCEND_W2_SIZE) { /* At what offset are we now? */ cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } + if (date_off != -1) { /* This packet has a date/time header; a read of it should start at the beginning of *that* header. */ @@ -179,6 +203,14 @@ static long ascend_seek(wtap *wth, int max_seek) } bytes_read++; } + if (byte != EOF || file_eof(wth->fh)) { + /* Either we didn't find the offset, or we got an EOF. */ + *err = 0; + } else { + /* We (presumably) got an error (there's no equivalent to "ferror()" + in zlib, alas, so we don't have a wrapper to check for an error). */ + *err = file_error(wth->fh); + } return -1; found: @@ -195,11 +227,13 @@ found: * Move to where the read for this packet should start, and return * that seek offset. */ - file_seek(wth->fh, packet_off, SEEK_SET); + if (file_seek(wth->fh, packet_off, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } return packet_off; } -/* XXX - return -1 on I/O error and actually do something with 'err'. */ int ascend_open(wtap *wth, int *err) { long offset; @@ -210,9 +244,12 @@ int ascend_open(wtap *wth, int *err) fill it in. */ wth->capture.ascend = NULL; - offset = ascend_seek(wth, ASCEND_MAX_SEEK); + offset = ascend_seek(wth, ASCEND_MAX_SEEK, err); if (offset == -1) { - return 0; + if (*err == 0) + return 0; + else + return -1; } wth->data_offset = offset; @@ -234,7 +271,11 @@ int ascend_open(wtap *wth, int *err) packet's timestamp from the capture file's ctime, which gives us an offset that we can apply to each packet. */ - fstat(wtap_fd(wth), &statbuf); + if (fstat(wtap_fd(wth), &statbuf) == -1) { + *err = errno; + g_free(wth->capture.ascend); + return -1; + } wth->capture.ascend->inittime = statbuf.st_ctime; wth->capture.ascend->adjusted = 0; @@ -254,12 +295,14 @@ static gboolean ascend_read(wtap *wth, int *err, long *data_offset) give us the correct location of the packet. Instead, we seek to the offset after the header of the previous packet and try to find the next packet. */ - file_seek(wth->fh, wth->capture.ascend->next_packet_seek_start, SEEK_SET); - offset = ascend_seek(wth, ASCEND_MAX_SEEK); - if (offset == -1) { - *err = 0; /* XXX - assume, for now, that it's an EOF */ + if (file_seek(wth->fh, wth->capture.ascend->next_packet_seek_start, + SEEK_SET) == -1) { + *err = file_error(wth->fh); return FALSE; } + offset = ascend_seek(wth, ASCEND_MAX_SEEK, err); + if (offset == -1) + return FALSE; if (! parse_ascend(wth->fh, buf, &wth->pseudo_header.ascend, &header, 0)) { *err = WTAP_ERR_BAD_RECORD; return FALSE; diff --git a/wiretap/csids.c b/wiretap/csids.c index 3efba8608f..992998c36f 100644 --- a/wiretap/csids.c +++ b/wiretap/csids.c @@ -1,6 +1,6 @@ /* csids.c * - * $Id: csids.c,v 1.10 2002/03/02 20:41:07 guy Exp $ + * $Id: csids.c,v 1.11 2002/03/04 00:25:35 guy Exp $ * * Copyright (c) 2000 by Mike Hall * Copyright (c) 2000 by Cisco Systems @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -40,13 +40,14 @@ * of data following for that packet. * * For a time there was an error in iplogging and the ip length, flags, and id - * were byteswapped. We will check for this and handle it before handing to ethereal. - * + * were byteswapped. We will check for this and handle it before handing to + * ethereal. */ static gboolean csids_read(wtap *wth, int *err, long *data_offset); static int csids_seek_read(wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header, guint8 *pd, int len); +static void csids_close(wtap *wth); struct csids_header { guint32 seconds; /* seconds since epoch */ @@ -122,17 +123,21 @@ int csids_open(wtap *wth, int *err) byteswap = FALSE; } + /* no file header. So reset the fh to 0 so we can read the first packet */ + if (file_seek(wth->fh, 0, SEEK_SET) == -1) { + *err = file_error( wth->fh ); + return -1; + } + wth->data_offset = 0; wth->capture.csids = g_malloc(sizeof(csids_t)); wth->capture.csids->byteswapped = byteswap; wth->file_encap = WTAP_ENCAP_RAW_IP; wth->file_type = WTAP_FILE_CSIDS; wth->snapshot_length = 0; /* not known */ - wth->subtype_read = csids_read; - wth->subtype_seek_read = csids_seek_read; - - /* no file header. So reset the fh to 0 so we can read the first packet */ - file_seek(wth->fh, 0, SEEK_SET); + wth->subtype_read = csids_read; + wth->subtype_seek_read = csids_seek_read; + wth->subtype_close = csids_close; return 1; } @@ -242,5 +247,8 @@ csids_seek_read (wtap *wth, return 0; } - - +static void +csids_close(wtap *wth) +{ + g_free(wth->capture.csids); +} diff --git a/wiretap/dbs-etherwatch.c b/wiretap/dbs-etherwatch.c index 5f0edc3ad9..bd69b78628 100644 --- a/wiretap/dbs-etherwatch.c +++ b/wiretap/dbs-etherwatch.c @@ -1,6 +1,6 @@ /* dbs-etherwatch.c * - * $Id: dbs-etherwatch.c,v 1.4 2002/03/02 20:41:07 guy Exp $ + * $Id: dbs-etherwatch.c,v 1.5 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 2001 by Marc Milgram @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -43,14 +43,14 @@ ETHERWATCH X5-008 42 names and addresses were loaded Reading recorded data from PERSISTENCE ------------------------------------------------------------------------------ ->From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB] +From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB] Protocol 08-00 00 00-00-00-00-00, 60 byte buffer at 10-OCT-2001 10:20:45.16 [E..<8.....Ò.....]- 0-[45 00 00 3C 38 93 00 00 1D 06 D2 12 80 93 11 1A] [...Ö.Ò...(¤.....]- 16-[80 93 80 D6 02 D2 02 03 00 28 A4 90 00 00 00 00] [.....½.....´....]- 32-[A0 02 FF FF 95 BD 00 00 02 04 05 B4 03 03 04 01] [......å..... ]- 48-[01 01 08 0A 90 90 E5 14 00 00 00 00] ------------------------------------------------------------------------------ ->From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB] +From 00-D0-C0-D2-4D-60 [MF1] to AA-00-04-00-FC-94 [PSERVB] Protocol 08-00 00 00-00-00-00-00, 50 byte buffer at 10-OCT-2001 10:20:45.17 [E..(8.....Ò%....]- 0-[45 00 00 28 38 94 00 00 1D 06 D2 25 80 93 11 1A] [...Ö.Ò...(¤.Z.4w]- 16-[80 93 80 D6 02 D2 02 03 00 28 A4 91 5A 1C 34 77] @@ -84,24 +84,38 @@ static int parse_dbs_etherwatch_rec_hdr(wtap *wth, FILE_T fh, int *err); /* Seeks to the beginning of the next packet, and returns the - byte offset. Returns -1 on failure. */ -/* XXX - Handle I/O errors. */ -static long dbs_etherwatch_seek_next_packet(wtap *wth) + byte offset. Returns -1 on failure, and sets "*err" to the error. */ +static long dbs_etherwatch_seek_next_packet(wtap *wth, int *err) { int byte; unsigned int level = 0; + long cur_off; while ((byte = file_getc(wth->fh)) != EOF) { if (byte == dbs_etherwatch_rec_magic[level]) { level++; if (level >= DBS_ETHERWATCH_REC_MAGIC_SIZE) { - /* note: we're leaving file pointer right after the magic characters */ - return file_tell(wth->fh) + 1; + /* note: we're leaving file pointer right after the magic characters */ + cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } + return cur_off + 1; } } else { level = 0; } } + if (file_eof(wth->fh)) { + /* We got an EOF. */ + *err = 0; + } else { + /* We (presumably) got an error (there's no equivalent to "ferror()" + in zlib, alas, so we don't have a wrapper to check for an error). */ + *err = file_error(wth->fh); + } return -1; } @@ -111,9 +125,10 @@ static long dbs_etherwatch_seek_next_packet(wtap *wth) /* Look through the first part of a file to see if this is * a DBS Ethertrace text trace file. * - * Returns TRUE if it is, FALSE if it isn't. + * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error; + * if we get an I/O error, "*err" will be set to a non-zero value. */ -static gboolean dbs_etherwatch_check_file_type(wtap *wth) +static gboolean dbs_etherwatch_check_file_type(wtap *wth, int *err) { char buf[DBS_ETHERWATCH_LINE_LENGTH]; int line, byte; @@ -142,19 +157,28 @@ static gboolean dbs_etherwatch_check_file_type(wtap *wth) level = 0; } } - else + else { + /* EOF or error. */ + if (file_eof(wth->fh)) + *err = 0; + else + *err = file_error(wth->fh); return FALSE; + } } + *err = 0; return FALSE; } -/* XXX - return -1 on I/O error and actually do something with 'err'. */ int dbs_etherwatch_open(wtap *wth, int *err) { /* Look for DBS ETHERWATCH header */ - if (!dbs_etherwatch_check_file_type(wth)) { - return 0; + if (!dbs_etherwatch_check_file_type(wth, err)) { + if (*err == 0) + return 0; + else + return -1; } wth->data_offset = 0; @@ -175,11 +199,9 @@ static gboolean dbs_etherwatch_read(wtap *wth, int *err, long *data_offset) int pkt_len; /* Find the next packet */ - offset = dbs_etherwatch_seek_next_packet(wth); - if (offset < 1) { - *err = 0; /* XXX - assume, for now, that it's an EOF */ + offset = dbs_etherwatch_seek_next_packet(wth, err); + if (offset < 1) return FALSE; - } /* Parse the header */ pkt_len = parse_dbs_etherwatch_rec_hdr(wth, wth->fh, err); diff --git a/wiretap/file.c b/wiretap/file.c index 07cfb1d2bf..1458ed4783 100644 --- a/wiretap/file.c +++ b/wiretap/file.c @@ -1,6 +1,6 @@ /* file.c * - * $Id: file.c,v 1.82 2002/03/02 20:41:07 guy Exp $ + * $Id: file.c,v 1.83 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -227,7 +227,15 @@ wtap* wtap_open_offline(const char *filename, int *err, gboolean do_random) to start reading at the beginning. Initialize the data offset while we're at it. */ - file_seek(wth->fh, 0, SEEK_SET); + if (file_seek(wth->fh, 0, SEEK_SET) == -1) { + /* I/O error - give up */ + *err = file_error(wth->fh); + if (wth->random_fh != NULL) + file_close(wth->random_fh); + file_close(wth->fh); + g_free(wth); + return NULL; + } wth->data_offset = 0; switch ((*open_routines[i])(wth, err)) { @@ -451,8 +459,7 @@ gboolean wtap_dump_can_write_encap(int filetype, int encap) static gboolean wtap_dump_open_check(int filetype, int encap, int *err); static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen, int *err); -static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, - int encap, int snaplen, int *err); +static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, int *err); wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap, int snaplen, int *err) @@ -480,7 +487,7 @@ wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap, } wdh->fh = fh; - if (!wtap_dump_open_finish(wdh, filetype, encap, snaplen, err)) { + if (!wtap_dump_open_finish(wdh, filetype, err)) { /* Get rid of the file we created; we couldn't finish opening it. */ unlink(filename); @@ -515,7 +522,7 @@ wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen, } wdh->fh = fh; - if (!wtap_dump_open_finish(wdh, filetype, encap, snaplen, err)) + if (!wtap_dump_open_finish(wdh, filetype, err)) return NULL; return wdh; } @@ -559,8 +566,7 @@ static wtap_dumper* wtap_dump_alloc_wdh(int filetype, int encap, int snaplen, return wdh; } -static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, - int encap, int snaplen, int *err) +static gboolean wtap_dump_open_finish(wtap_dumper *wdh, int filetype, int *err) { /* Now try to open the file for writing. */ if (!(*dump_open_table[filetype].dump_open)(wdh, err)) { diff --git a/wiretap/file_wrappers.h b/wiretap/file_wrappers.h index 97566e0ce4..efeb96e35a 100644 --- a/wiretap/file_wrappers.h +++ b/wiretap/file_wrappers.h @@ -1,6 +1,6 @@ /* file_wrappers.h * - * $Id: file_wrappers.h,v 1.7 2002/02/06 09:58:30 guy Exp $ + * $Id: file_wrappers.h,v 1.8 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -25,6 +25,7 @@ #define __FILE_H__ #ifdef HAVE_LIBZ + #define file_open gzopen #define filed_open gzdopen extern long file_seek(void *stream, long offset, int whence); @@ -35,6 +36,7 @@ extern long file_tell(void *stream); #define file_getc gzgetc #define file_gets(buf, len, file) gzgets((file), (buf), (len)) extern int file_error(void *fh); +#define file_eof gzeof #else /* No zLib */ #define file_open fopen @@ -47,6 +49,8 @@ extern int file_error(FILE *fh); #define file_tell ftell #define file_getc fgetc #define file_gets fgets +#define file_eof feof + #endif /* HAVE_LIBZ */ #endif /* __FILE_H__ */ diff --git a/wiretap/i4btrace.c b/wiretap/i4btrace.c index 7b4b88c380..0d5dec1c50 100644 --- a/wiretap/i4btrace.c +++ b/wiretap/i4btrace.c @@ -1,6 +1,6 @@ /* i4btrace.c * - * $Id: i4btrace.c,v 1.16 2002/02/08 10:07:40 guy Exp $ + * $Id: i4btrace.c,v 1.17 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1999 by Bert Driehuis @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -40,6 +40,7 @@ static void i4b_byte_swap_header(wtap *wth, i4b_trace_hdr_t *hdr); static int i4b_read_rec_data(FILE_T fh, u_char *pd, int length, int *err); static void i4b_set_pseudo_header(wtap *wth, i4b_trace_hdr_t *hdr, union wtap_pseudo_header *pseudo_header); +static void i4btrace_close(wtap *wth); /* * Test some fields in the header to see if they make sense. @@ -89,7 +90,10 @@ int i4btrace_open(wtap *wth, int *err) byte_swapped = TRUE; } - file_seek(wth->fh, 0, SEEK_SET); + if (file_seek(wth->fh, 0, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset = 0; /* Get capture start time */ @@ -98,6 +102,7 @@ int i4btrace_open(wtap *wth, int *err) wth->capture.i4btrace = g_malloc(sizeof(i4btrace_t)); wth->subtype_read = i4btrace_read; wth->subtype_seek_read = i4btrace_seek_read; + wth->subtype_close = i4btrace_close; wth->snapshot_length = 0; /* not known */ wth->capture.i4btrace->bchannel_prot[0] = -1; @@ -332,3 +337,9 @@ i4b_set_pseudo_header(wtap *wth, i4b_trace_hdr_t *hdr, break; } } + +static void +i4btrace_close(wtap *wth) +{ + g_free(wth->capture.i4btrace); +} diff --git a/wiretap/lanalyzer.c b/wiretap/lanalyzer.c index 659b04ce2f..e5a1ff7318 100644 --- a/wiretap/lanalyzer.c +++ b/wiretap/lanalyzer.c @@ -1,6 +1,6 @@ /* lanalyzer.c * - * $Id: lanalyzer.c,v 1.30 2001/11/13 23:55:43 gram Exp $ + * $Id: lanalyzer.c,v 1.31 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -90,7 +90,11 @@ int lanalyzer_open(wtap *wth, int *err) /* Read records until we find the start of packets */ while (1) { - file_seek(wth->fh, record_length, SEEK_CUR); + if (file_seek(wth->fh, record_length, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.lanalyzer); + return -1; + } wth->data_offset += record_length; errno = WTAP_ERR_CANT_READ; bytes_read = file_read(LE_record_type, 1, 2, wth->fh); @@ -180,7 +184,11 @@ int lanalyzer_open(wtap *wth, int *err) case REC_TRACE_PACKET_DATA: /* Go back header number ob ytes so that lanalyzer_read * can read this header */ - file_seek(wth->fh, -bytes_read, SEEK_CUR); + if (file_seek(wth->fh, -bytes_read, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.lanalyzer); + return -1; + } wth->data_offset -= bytes_read; return 1; diff --git a/wiretap/libpcap.c b/wiretap/libpcap.c index 06b2a047b6..6e19e7203e 100644 --- a/wiretap/libpcap.c +++ b/wiretap/libpcap.c @@ -1,6 +1,6 @@ /* libpcap.c * - * $Id: libpcap.c,v 1.67 2002/03/02 20:41:07 guy Exp $ + * $Id: libpcap.c,v 1.68 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -38,7 +38,6 @@ #define BIT_SWAPPED_MAC_ADDRS #endif - /* Try to read the first two records of the capture file. */ typedef enum { THIS_FORMAT, /* the reads succeeded, assume it's this format */ @@ -594,6 +593,7 @@ int libpcap_open(wtap *wth, int *err) * Well, we couldn't even read it. * Give up. */ + g_free(wth->capture.pcap); return -1; case THIS_FORMAT: @@ -601,7 +601,11 @@ int libpcap_open(wtap *wth, int *err) * Well, it looks as if it might be 991029. * Put the seek pointer back, and return success. */ - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } return 1; case OTHER_FORMAT: @@ -619,7 +623,11 @@ int libpcap_open(wtap *wth, int *err) * it as 990915. */ wth->file_type = WTAP_FILE_PCAP_SS990915; - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } } else { /* * Well, we have the standard magic number. @@ -634,6 +642,7 @@ int libpcap_open(wtap *wth, int *err) * Well, we couldn't even read it. * Give up. */ + g_free(wth->capture.pcap); return -1; case THIS_FORMAT: @@ -642,7 +651,11 @@ int libpcap_open(wtap *wth, int *err) * libpcap file. * Put the seek pointer back, and return success. */ - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } return 1; case OTHER_FORMAT: @@ -658,7 +671,11 @@ int libpcap_open(wtap *wth, int *err) * ss990417. */ wth->file_type = WTAP_FILE_PCAP_SS990417; - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } switch (libpcap_try(wth, err)) { case BAD_READ: @@ -666,6 +683,7 @@ int libpcap_open(wtap *wth, int *err) * Well, we couldn't even read it. * Give up. */ + g_free(wth->capture.pcap); return -1; case THIS_FORMAT: @@ -673,7 +691,11 @@ int libpcap_open(wtap *wth, int *err) * Well, it looks as if it might be ss990417. * Put the seek pointer back, and return success. */ - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } return 1; case OTHER_FORMAT: @@ -691,7 +713,11 @@ int libpcap_open(wtap *wth, int *err) * and treat it as a Nokia file. */ wth->file_type = WTAP_FILE_PCAP_NOKIA; - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.pcap); + return -1; + } } return 1; @@ -741,7 +767,10 @@ static libpcap_try_t libpcap_try(wtap *wth, int *err) * Now skip over the first record's data, under the assumption * that the header is sane. */ - file_seek(wth->fh, first_rec_hdr.hdr.incl_len, SEEK_CUR); + if (file_seek(wth->fh, first_rec_hdr.hdr.incl_len, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return BAD_READ; + } /* * Now attempt to read the second record's header. diff --git a/wiretap/netmon.c b/wiretap/netmon.c index 4a4143aa1a..c2e7a30aca 100644 --- a/wiretap/netmon.c +++ b/wiretap/netmon.c @@ -1,6 +1,6 @@ /* netmon.c * - * $Id: netmon.c,v 1.48 2002/02/27 08:57:25 guy Exp $ + * $Id: netmon.c,v 1.49 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -266,9 +266,13 @@ int netmon_open(wtap *wth, int *err) *err = WTAP_ERR_UNSUPPORTED; return -1; } + if (file_seek(wth->fh, frame_table_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.netmon); + return -1; + } frame_table = g_malloc(frame_table_length); errno = WTAP_ERR_CANT_READ; - file_seek(wth->fh, frame_table_offset, SEEK_SET); bytes_read = file_read(frame_table, 1, frame_table_length, wth->fh); if ((guint32)bytes_read != frame_table_length) { *err = file_error(wth->fh); @@ -327,7 +331,10 @@ static gboolean netmon_read(wtap *wth, int *err, long *data_offset) rec_offset = netmon->frame_table[netmon->current_frame]; if (wth->data_offset != rec_offset) { wth->data_offset = rec_offset; - file_seek(wth->fh, wth->data_offset, SEEK_SET); + if (file_seek(wth->fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return FALSE; + } } netmon->current_frame++; @@ -566,7 +573,10 @@ gboolean netmon_dump_open(wtap_dumper *wdh, int *err) haven't yet written any packets. As we'll have to rewrite the header when we've written out all the packets, we just skip over the header for now. */ - fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET); + if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) { + *err = errno; + return FALSE; + } wdh->dump.netmon = g_malloc(sizeof(netmon_dump_t)); wdh->dump.netmon->frame_table_offset = CAPTUREFILE_HEADER_SIZE; diff --git a/wiretap/nettl.c b/wiretap/nettl.c index 8ebe1a0523..1c4a843363 100644 --- a/wiretap/nettl.c +++ b/wiretap/nettl.c @@ -1,6 +1,6 @@ /* nettl.c * - * $Id: nettl.c,v 1.22 2002/02/08 10:07:40 guy Exp $ + * $Id: nettl.c,v 1.23 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -95,17 +95,23 @@ int nettl_open(wtap *wth, int *err) return 0; } - file_seek(wth->fh, 0x63, SEEK_SET); + if (file_seek(wth->fh, 0x63, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset = 0x63; bytes_read = file_read(os_vers, 1, 2, wth->fh); if (bytes_read != 2) { - *err = file_error(wth->fh); + *err = file_error(wth->fh); if (*err != 0) return -1; return 0; } - file_seek(wth->fh, 0x80, SEEK_SET); + if (file_seek(wth->fh, 0x80, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset = 0x80; /* This is an nettl file */ diff --git a/wiretap/netxray.c b/wiretap/netxray.c index 658be15e5a..871c10aaf8 100644 --- a/wiretap/netxray.c +++ b/wiretap/netxray.c @@ -1,6 +1,6 @@ /* netxray.c * - * $Id: netxray.c,v 1.46 2002/03/02 20:41:07 guy Exp $ + * $Id: netxray.c,v 1.47 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -224,7 +224,11 @@ int netxray_open(wtap *wth, int *err) wth->capture.netxray->end_offset = pletohl(&hdr.end_offset); /* Seek to the beginning of the data records. */ - file_seek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET); + if (file_seek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET) == -1) { + *err = file_error(wth->fh); + g_free(wth->capture.netxray); + return -1; + } wth->data_offset = pletohl(&hdr.start_offset); return 1; @@ -275,7 +279,11 @@ reread: if (!wth->capture.netxray->wrapped) { /* Yes. Remember that we did. */ wth->capture.netxray->wrapped = 1; - file_seek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET); + if (file_seek(wth->fh, CAPTUREFILE_HEADER_SIZE, + SEEK_SET) == -1) { + *err = file_error(wth->fh); + return FALSE; + } wth->data_offset = CAPTUREFILE_HEADER_SIZE; goto reread; } @@ -364,7 +372,10 @@ gboolean netxray_dump_open_1_1(wtap_dumper *wdh, int *err) haven't yet written any packets. As we'll have to rewrite the header when we've written out all the packets, we just skip over the header for now. */ - fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET); + if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) { + *err = errno; + return FALSE; + } wdh->dump.netxray = g_malloc(sizeof(netxray_dump_t)); wdh->dump.netxray->first_frame = TRUE; diff --git a/wiretap/ngsniffer.c b/wiretap/ngsniffer.c index a41d6cccde..099058ac60 100644 --- a/wiretap/ngsniffer.c +++ b/wiretap/ngsniffer.c @@ -1,6 +1,6 @@ /* ngsniffer.c * - * $Id: ngsniffer.c,v 1.73 2002/03/02 20:41:07 guy Exp $ + * $Id: ngsniffer.c,v 1.74 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -316,8 +316,8 @@ static int ng_file_read(void *buffer, size_t elementsize, size_t numelements, wtap *wth, gboolean is_random, int *err); static int read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream, int *err); -static long ng_file_seek_seq(wtap *wth, long offset, int whence); -static long ng_file_seek_rand(wtap *wth, long offset, int whence); +static long ng_file_seek_seq(wtap *wth, long offset, int whence, int *err); +static long ng_file_seek_rand(wtap *wth, long offset, int whence, int *err); int ngsniffer_open(wtap *wth, int *err) { @@ -456,8 +456,12 @@ int ngsniffer_open(wtap *wth, int *err) * or REC_EOF after this? If not, we can get rid of the loop in * "ngsniffer_read()". */ - if (wth->random_fh != NULL) - file_seek(wth->random_fh, wth->data_offset, SEEK_SET); + if (wth->random_fh != NULL) { + if (file_seek(wth->random_fh, wth->data_offset, SEEK_SET) == -1) { + *err = file_error(wth->random_fh); + return -1; + } + } /* This is a ngsniffer file */ wth->capture.ngsniffer = g_malloc(sizeof(ngsniffer_t)); @@ -559,7 +563,10 @@ skip_header_records(wtap *wth, int *err, gint16 version) * which implies data. Seek backwards over the * two bytes we read, and return. */ - file_seek(wth->fh, -2, SEEK_CUR); + if (file_seek(wth->fh, -2, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } return 0; } @@ -603,8 +610,11 @@ skip_header_records(wtap *wth, int *err, gint16 version) * Skip the rest of the record. */ if (length > sizeof buffer) { - file_seek(wth->fh, length - sizeof buffer, - SEEK_CUR); + if (file_seek(wth->fh, length - sizeof buffer, + SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } } /* @@ -637,7 +647,10 @@ skip_header_records(wtap *wth, int *err, gint16 version) } else { /* Nope, just skip over the data. */ - file_seek(wth->fh, length, SEEK_CUR); + if (file_seek(wth->fh, length, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } } wth->data_offset += length; } @@ -779,7 +792,8 @@ static gboolean ngsniffer_read(wtap *wth, int *err, long *data_offset) * it is but can't handle it. Skip past the data * portion, and keep looping. */ - ng_file_seek_seq(wth, length, SEEK_CUR); + if (ng_file_seek_seq(wth, length, SEEK_CUR, err) == -1) + return FALSE; wth->data_offset += length; } @@ -867,7 +881,7 @@ static int ngsniffer_seek_read(wtap *wth, long seek_off, struct frame4_rec frame4; struct frame6_rec frame6; - ng_file_seek_rand(wth, seek_off, SEEK_SET); + ng_file_seek_rand(wth, seek_off, SEEK_SET, &err); ret = ngsniffer_read_rec_header(wth, TRUE, &type, &length, &err); if (ret <= 0) { @@ -1682,15 +1696,19 @@ read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream, int *err) /* Seek in the sequential data stream; we can only seek forward, and we do it on compressed files by skipping forward. */ static long -ng_file_seek_seq(wtap *wth, long offset, int whence) +ng_file_seek_seq(wtap *wth, long offset, int whence, int *err) { - long delta; - char buf[65536]; - long amount_to_read; - int err; + long ret; + long delta; + char buf[65536]; + long amount_to_read; - if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) - return file_seek(wth->fh, offset, whence); + if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) { + ret = file_seek(wth->fh, offset, whence); + if (ret == -1) + *err = file_error(wth->fh); + return ret; + } switch (whence) { @@ -1714,7 +1732,7 @@ ng_file_seek_seq(wtap *wth, long offset, int whence) amount_to_read = delta; if ((unsigned long)amount_to_read > sizeof buf) amount_to_read = sizeof buf; - if (ng_file_read(buf, 1, amount_to_read, wth, FALSE, &err) < 0) + if (ng_file_read(buf, 1, amount_to_read, wth, FALSE, err) < 0) return -1; /* error */ delta -= amount_to_read; } @@ -1730,16 +1748,20 @@ ng_file_seek_seq(wtap *wth, long offset, int whence) position within the blob we have in memory (whether it's the blob we already had in memory or, if necessary, the one we read in). */ static long -ng_file_seek_rand(wtap *wth, long offset, int whence) +ng_file_seek_rand(wtap *wth, long offset, int whence, int *err) { - ngsniffer_t *ngsniffer; - long delta; - int err; - GList *new, *next; - blob_info_t *next_blob, *new_blob; + long ret; + ngsniffer_t *ngsniffer; + long delta; + GList *new, *next; + blob_info_t *next_blob, *new_blob; - if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) - return file_seek(wth->random_fh, offset, whence); + if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) { + ret = file_seek(wth->random_fh, offset, whence); + if (ret == -1) + *err = file_error(wth->random_fh); + return ret; + } ngsniffer = wth->capture.ngsniffer; @@ -1814,8 +1836,10 @@ ng_file_seek_rand(wtap *wth, long offset, int whence) /* Seek in the compressed file to the offset in the compressed file of the beginning of that blob. */ - if (file_seek(wth->random_fh, new_blob->blob_comp_offset, SEEK_SET) == -1) + if (file_seek(wth->random_fh, new_blob->blob_comp_offset, SEEK_SET) == -1) { + *err = file_error(wth->random_fh); return -1; + } /* Make the blob we found the current one. */ ngsniffer->current_blob = new; @@ -1826,7 +1850,7 @@ ng_file_seek_rand(wtap *wth, long offset, int whence) ngsniffer->rand.comp_offset = new_blob->blob_comp_offset; /* Now fill the buffer. */ - if (read_blob(wth->random_fh, &ngsniffer->rand, &err) < 0) + if (read_blob(wth->random_fh, &ngsniffer->rand, err) < 0) return -1; /* Set "delta" to the amount to move within this blob; it had diff --git a/wiretap/pppdump.c b/wiretap/pppdump.c index 7768e42746..950c4e4e02 100644 --- a/wiretap/pppdump.c +++ b/wiretap/pppdump.c @@ -1,6 +1,6 @@ /* pppdump.c * - * $Id: pppdump.c,v 1.14 2002/03/02 20:41:07 guy Exp $ + * $Id: pppdump.c,v 1.15 2002/03/04 00:25:35 guy Exp $ * * Copyright (c) 2000 by Gilbert Ramirez * @@ -17,8 +17,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -195,6 +195,11 @@ pppdump_open(wtap *wth, int *err) my_file_type: + if (file_seek(wth->fh, 5, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } + state = wth->capture.generic = g_malloc(sizeof(pppdump_t)); state->timestamp = pntohl(&buffer[1]); state->tenths = 0; @@ -202,7 +207,6 @@ pppdump_open(wtap *wth, int *err) init_state(state); state->offset = 5; - file_seek(wth->fh, 5, SEEK_SET); wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR; wth->file_type = WTAP_FILE_PPPDUMP; @@ -589,10 +593,10 @@ pppdump_close(wtap *wth) } if (state->pids) { /* should always be TRUE */ - unsigned int i; - for (i = 0; i < g_ptr_array_len(state->pids); i++) { - g_free(g_ptr_array_index(state->pids, i)); - } + unsigned int i; + for (i = 0; i < g_ptr_array_len(state->pids); i++) { + g_free(g_ptr_array_index(state->pids, i)); + } g_ptr_array_free(state->pids, TRUE); } diff --git a/wiretap/radcom.c b/wiretap/radcom.c index e25ea3f014..3bf9adf2f4 100644 --- a/wiretap/radcom.c +++ b/wiretap/radcom.c @@ -1,6 +1,6 @@ /* radcom.c * - * $Id: radcom.c,v 1.32 2002/02/08 10:07:41 guy Exp $ + * $Id: radcom.c,v 1.33 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -99,7 +99,10 @@ int radcom_open(wtap *wth, int *err) return 0; } - file_seek(wth->fh, 0x8B, SEEK_SET); + if (file_seek(wth->fh, 0x8B, SEEK_SET) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset = 0x8B; errno = WTAP_ERR_CANT_READ; bytes_read = file_read(&byte, 1, 1, wth->fh); @@ -148,7 +151,10 @@ int radcom_open(wtap *wth, int *err) tm.tm_sec = sec%60; tm.tm_isdst = -1; - file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR); + if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset += sizeof(struct frame_date); errno = WTAP_ERR_CANT_READ; @@ -158,7 +164,10 @@ int radcom_open(wtap *wth, int *err) } wth->data_offset += 4; while (memcmp(encap_magic, search_encap, 4)) { - file_seek(wth->fh, -3, SEEK_CUR); + if (file_seek(wth->fh, -3, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset -= 3; errno = WTAP_ERR_CANT_READ; bytes_read = file_read(search_encap, 1, 4, wth->fh); @@ -167,7 +176,10 @@ int radcom_open(wtap *wth, int *err) } wth->data_offset += 4; } - file_seek(wth->fh, 12, SEEK_CUR); + if (file_seek(wth->fh, 12, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset += 12; errno = WTAP_ERR_CANT_READ; bytes_read = file_read(search_encap, 1, 4, wth->fh); @@ -192,7 +204,10 @@ int radcom_open(wtap *wth, int *err) } while (memcmp(&start_date, &next_date, 4)) { - file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR); + if (file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } errno = WTAP_ERR_CANT_READ; bytes_read = file_read(&next_date, 1, sizeof(struct frame_date), wth->fh); @@ -202,10 +217,16 @@ int radcom_open(wtap *wth, int *err) }*/ if (wth->file_encap == WTAP_ENCAP_ETHERNET) { - file_seek(wth->fh, 294, SEEK_CUR); + if (file_seek(wth->fh, 294, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset += 294; } else if (wth->file_encap == WTAP_ENCAP_LAPB) { - file_seek(wth->fh, 297, SEEK_CUR); + if (file_seek(wth->fh, 297, SEEK_CUR) == -1) { + *err = file_error(wth->fh); + return -1; + } wth->data_offset += 297; } diff --git a/wiretap/toshiba.c b/wiretap/toshiba.c index b5b0f895a2..a644f5e566 100644 --- a/wiretap/toshiba.c +++ b/wiretap/toshiba.c @@ -1,6 +1,6 @@ /* toshiba.c * - * $Id: toshiba.c,v 1.20 2002/02/08 10:07:41 guy Exp $ + * $Id: toshiba.c,v 1.21 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -118,24 +118,38 @@ static int parse_toshiba_rec_hdr(wtap *wth, FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err); /* Seeks to the beginning of the next packet, and returns the - byte offset. Returns -1 on failure. */ -/* XXX - Handle I/O errors. */ -static long toshiba_seek_next_packet(wtap *wth) + byte offset. Returns -1 on failure, and sets "*err" to the error. */ +static long toshiba_seek_next_packet(wtap *wth, int *err) { int byte; guint level = 0; + long cur_off; while ((byte = file_getc(wth->fh)) != EOF) { if (byte == toshiba_rec_magic[level]) { level++; if (level >= TOSHIBA_REC_MAGIC_SIZE) { /* note: we're leaving file pointer right after the magic characters */ - return file_tell(wth->fh) + 1; + cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } + return cur_off + 1; } } else { level = 0; } } + if (file_eof(wth->fh)) { + /* We got an EOF. */ + *err = 0; + } else { + /* We (presumably) got an error (there's no equivalent to "ferror()" + in zlib, alas, so we don't have a wrapper to check for an error). */ + *err = file_error(wth->fh); + } return -1; } @@ -145,9 +159,10 @@ static long toshiba_seek_next_packet(wtap *wth) /* Look through the first part of a file to see if this is * a Toshiba trace file. * - * Returns TRUE if it is, FALSE if it isn't. + * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error; + * if we get an I/O error, "*err" will be set to a non-zero value. */ -static gboolean toshiba_check_file_type(wtap *wth) +static gboolean toshiba_check_file_type(wtap *wth, int *err) { char buf[TOSHIBA_LINE_LENGTH]; guint i, reclen, level, line; @@ -178,19 +193,27 @@ static gboolean toshiba_check_file_type(wtap *wth) } } else { + /* EOF or error. */ + if (file_eof(wth->fh)) + *err = 0; + else + *err = file_error(wth->fh); return FALSE; } } + *err = 0; return FALSE; } -/* XXX - return -1 on I/O error and actually do something with 'err'. */ int toshiba_open(wtap *wth, int *err) { /* Look for Toshiba header */ - if (!toshiba_check_file_type(wth)) { - return 0; + if (!toshiba_check_file_type(wth, err)) { + if (*err == 0) + return 0; + else + return -1; } wth->data_offset = 0; @@ -211,11 +234,9 @@ static gboolean toshiba_read(wtap *wth, int *err, long *data_offset) int pkt_len; /* Find the next packet */ - offset = toshiba_seek_next_packet(wth); - if (offset < 1) { - *err = 0; /* XXX - assume, for now, that it's an EOF */ + offset = toshiba_seek_next_packet(wth, err); + if (offset < 1) return FALSE; - } /* Parse the header */ pkt_len = parse_toshiba_rec_hdr(wth, wth->fh, diff --git a/wiretap/visual.c b/wiretap/visual.c index 5e2c286268..2a75c93ce6 100644 --- a/wiretap/visual.c +++ b/wiretap/visual.c @@ -2,7 +2,7 @@ * File read and write routines for Visual Networks cap files. * Copyright (c) 2001, Tom Nisbet tnisbet@visualnetworks.com * - * $Id: visual.c,v 1.2 2002/02/27 08:57:25 guy Exp $ + * $Id: visual.c,v 1.3 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -421,7 +421,10 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err) /* All of the fields in the file header aren't known yet so just skip over it for now. It will be created after all of the packets have been written. */ - fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET); + if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) { + *err = errno; + return FALSE; + } return TRUE; } diff --git a/wiretap/vms.c b/wiretap/vms.c index f1e0760bf2..19af1bfbc1 100644 --- a/wiretap/vms.c +++ b/wiretap/vms.c @@ -1,6 +1,6 @@ /* vms.c * - * $Id: vms.c,v 1.7 2002/03/02 20:41:08 guy Exp $ + * $Id: vms.c,v 1.8 2002/03/04 00:25:35 guy Exp $ * * Wiretap Library * Copyright (c) 2001 by Marc Milgram @@ -18,8 +18,8 @@ * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - * */ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -80,12 +80,12 @@ static int parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err); /* Seeks to the beginning of the next packet, and returns the - byte offset. Returns -1 on failure. */ -/* XXX - Handle I/O errors. */ -static long vms_seek_next_packet(wtap *wth) + byte offset. Returns -1 on failure, and sets "*err" to the error. */ +static long vms_seek_next_packet(wtap *wth, int *err) { int byte; unsigned int level = 0; + long cur_off; while ((byte = file_getc(wth->fh)) != EOF) { if ((level == 3) && (byte != vms_rec_magic[level])) @@ -93,13 +93,27 @@ static long vms_seek_next_packet(wtap *wth) if (byte == vms_rec_magic[level]) { level++; if (level >= VMS_REC_MAGIC_SIZE) { - /* note: we're leaving file pointer right after the magic characters */ - return file_tell(wth->fh) + 1; + /* note: we're leaving file pointer right after the magic characters */ + cur_off = file_tell(wth->fh); + if (cur_off == -1) { + /* Error. */ + *err = file_error(wth->fh); + return -1; + } + return cur_off + 1; } } else { level = 0; } } + if (file_eof(wth->fh)) { + /* We got an EOF. */ + *err = 0; + } else { + /* We (presumably) got an error (there's no equivalent to "ferror()" + in zlib, alas, so we don't have a wrapper to check for an error). */ + *err = file_error(wth->fh); + } return -1; } @@ -109,12 +123,13 @@ static long vms_seek_next_packet(wtap *wth) /* Look through the first part of a file to see if this is * a VMS trace file. * - * Returns TRUE if it is, FALSE if it isn't. + * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error; + * if we get an I/O error, "*err" will be set to a non-zero value. * * Leaves file handle at begining of line that contains the VMS Magic * identifier. */ -static gboolean vms_check_file_type(wtap *wth) +static gboolean vms_check_file_type(wtap *wth, int *err) { char buf[VMS_LINE_LENGTH]; int line, byte; @@ -125,6 +140,11 @@ static gboolean vms_check_file_type(wtap *wth) for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) { mpos = file_tell(wth->fh); + if (mpos == -1) { + /* Error. */ + *err = file_error(wth->fh); + return FALSE; + } if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) { reclen = strlen(buf); @@ -139,7 +159,11 @@ static gboolean vms_check_file_type(wtap *wth) if (byte == vms_hdr_magic[level]) { level++; if (level >= VMS_HDR_MAGIC_SIZE) { - file_seek(wth->fh, mpos, SEEK_SET); + if (file_seek(wth->fh, mpos, SEEK_SET) == -1) { + /* Error. */ + *err = file_error(wth->fh); + return FALSE; + } return TRUE; } } @@ -147,19 +171,28 @@ static gboolean vms_check_file_type(wtap *wth) level = 0; } } - else + else { + /* EOF or error. */ + if (file_eof(wth->fh)) + *err = 0; + else + *err = file_error(wth->fh); return FALSE; + } } + *err = 0; return FALSE; } -/* XXX - return -1 on I/O error and actually do something with 'err'. */ int vms_open(wtap *wth, int *err) { /* Look for VMS header */ - if (!vms_check_file_type(wth)) { - return 0; + if (!vms_check_file_type(wth, err)) { + if (*err == 0) + return 0; + else + return -1; } wth->data_offset = 0; @@ -180,11 +213,9 @@ static gboolean vms_read(wtap *wth, int *err, long *data_offset) int pkt_len; /* Find the next packet */ - offset = vms_seek_next_packet(wth); - if (offset < 1) { - *err = 0; /* XXX - assume, for now, that it's an EOF */ + offset = vms_seek_next_packet(wth, err); + if (offset < 1) return FALSE; - } /* Parse the header */ pkt_len = parse_vms_rec_hdr(wth, wth->fh, err);