diff options
author | Guy Harris <guy@alum.mit.edu> | 1999-08-19 05:31:38 +0000 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 1999-08-19 05:31:38 +0000 |
commit | 137ba48d18e0957e5fc920e7b518ba95212789a8 (patch) | |
tree | 9ea3f0562b63feb029a13b9d17f856181e5ceecb /wiretap | |
parent | 7843c01d38ea4e6e17ad9c36f94999fe2ff9866c (diff) |
Have the per-capture-file-type open routines "wtap_open_offline()" calls
return 1 on success, -1 if they got an error, and 0 if the file isn't of
the type that file is checking for, and supply an error code if they
return -1; have "wtap_open_offline()" use that error code. Also, have
the per-capture-file-type open routines treat errors accessing the file
as errors, and return -1, rather than just returning 0 so that we try
another file type.
Have the per-capture-file-type read routines "wtap_loop()" calls return
-1 and supply an error code on error (and not, as they did in some
cases, call "g_error()" and abort), and have "wtap_loop()", if the read
routine returned an error, return FALSE (and pass an error-code-pointer
argument onto the read routines, so they fill it in), and return TRUE on
success.
Add some new error codes for them to return.
Now that "wtap_loop()" can return a success/failure indication and an
error code, in "read_cap_file()" put up a message box if we get an error
reading the file, and return the error code.
Handle the additional errors we can get when opening a capture file.
If the attempt to open a capture file succeeds, but the attempt to read
it fails, don't treat that as a complete failure - we may have managed
to read some of the capture file, and we should display what we managed
to read.
svn path=/trunk/; revision=516
Diffstat (limited to 'wiretap')
-rw-r--r-- | wiretap/file.c | 88 | ||||
-rw-r--r-- | wiretap/iptrace.c | 41 | ||||
-rw-r--r-- | wiretap/iptrace.h | 5 | ||||
-rw-r--r-- | wiretap/lanalyzer.c | 139 | ||||
-rw-r--r-- | wiretap/lanalyzer.h | 7 | ||||
-rw-r--r-- | wiretap/libpcap.c | 59 | ||||
-rw-r--r-- | wiretap/libpcap.h | 4 | ||||
-rw-r--r-- | wiretap/netmon.c | 58 | ||||
-rw-r--r-- | wiretap/netmon.h | 5 | ||||
-rw-r--r-- | wiretap/netxray.c | 58 | ||||
-rw-r--r-- | wiretap/netxray.h | 5 | ||||
-rw-r--r-- | wiretap/ngsniffer.c | 202 | ||||
-rw-r--r-- | wiretap/radcom.c | 138 | ||||
-rw-r--r-- | wiretap/radcom.h | 3 | ||||
-rw-r--r-- | wiretap/snoop.c | 59 | ||||
-rw-r--r-- | wiretap/snoop.h | 5 | ||||
-rw-r--r-- | wiretap/wtap.c | 14 | ||||
-rw-r--r-- | wiretap/wtap.h | 27 |
18 files changed, 595 insertions, 322 deletions
diff --git a/wiretap/file.c b/wiretap/file.c index cc53ea37ff..f120b4ccf4 100644 --- a/wiretap/file.c +++ b/wiretap/file.c @@ -1,6 +1,6 @@ /* file.c * - * $Id: file.c,v 1.16 1999/08/18 17:49:34 guy Exp $ + * $Id: file.c,v 1.17 1999/08/19 05:31:33 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -39,16 +39,42 @@ #include "netmon.h" #include "netxray.h" -/* The open_file_* routines should return the WTAP_FILE_* type - * that they are checking for if the file is successfully recognized - * as such. If the file is not of that type, the routine should return - * WTAP_FILE_UNKNOWN */ +/* The open_file_* routines should return: + * + * -1 on an I/O error; + * + * 1 if the file they're reading is one of the types it handles; + * + * 0 if the file they're reading isn't the type they're checking for. + * + * If the routine handles this type of file, it should set the "file_type" + * field in the "struct wtap" to the type of the file. + * + * XXX - I need to drag my damn ANSI C spec in to figure out how to + * declare a "const" array of pointers to functions; putting "const" + * right after "static" isn't the right answer, at least according + * to GCC, which whines if I do that. + */ + +static int (*open_routines[])(wtap *, int *) = { + libpcap_open, + lanalyzer_open, + ngsniffer_open, + snoop_open, + iptrace_open, + netmon_open, + netxray_open, + radcom_open +}; + +#define N_FILE_TYPES (sizeof open_routines / sizeof open_routines[0]) /* Opens a file and prepares a wtap struct */ wtap* wtap_open_offline(const char *filename, int *err) { struct stat statb; wtap *wth; + int i; /* First, make sure the file is valid */ if (stat(filename, &statb) < 0) { @@ -81,41 +107,27 @@ wtap* wtap_open_offline(const char *filename, int *err) wth->file_encap = WTAP_ENCAP_NONE; /* Try all file types */ - - /* WTAP_FILE_PCAP */ - if ((wth->file_type = libpcap_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_NGSNIFFER */ - if ((wth->file_type = ngsniffer_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_RADCOM */ - if ((wth->file_type = radcom_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_LANALYZER */ - if ((wth->file_type = lanalyzer_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_SNOOP */ - if ((wth->file_type = snoop_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_IPTRACE */ - if ((wth->file_type = iptrace_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_NETMON_xxx */ - if ((wth->file_type = netmon_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; - } - /* WTAP_FILE_NETXRAY_xxx */ - if ((wth->file_type = netxray_open(wth)) != WTAP_FILE_UNKNOWN) { - goto success; + for (i = 0; i < N_FILE_TYPES; i++) { + switch ((*open_routines[i])(wth, err)) { + + case -1: + /* I/O error - give up */ + *err = errno; + fclose(wth->fh); + free(wth); + return NULL; + + case 0: + /* No I/O error, but not that type of file */ + break; + + case 1: + /* We found the file type */ + goto success; + } } -/* failure: */ + /* Well, it's not one of the types of file we know about. */ fclose(wth->fh); free(wth); *err = WTAP_ERR_FILE_UNKNOWN_FORMAT; diff --git a/wiretap/iptrace.c b/wiretap/iptrace.c index 87fc5501b7..5c35a06fc5 100644 --- a/wiretap/iptrace.c +++ b/wiretap/iptrace.c @@ -1,6 +1,6 @@ /* iptrace.c * - * $Id: iptrace.c,v 1.5 1999/07/28 01:35:34 gerald Exp $ + * $Id: iptrace.c,v 1.6 1999/08/19 05:31:37 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -24,34 +24,42 @@ #include "config.h" #endif #include <stdlib.h> +#include <errno.h> #include <time.h> #include <string.h> #include "wtap.h" #include "buffer.h" #include "iptrace.h" -int iptrace_open(wtap *wth) +static int iptrace_read(wtap *wth, int *err); + +int iptrace_open(wtap *wth, int *err) { int bytes_read; char name[12]; fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(name, 1, 11, wth->fh); - if (bytes_read != 11) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } name[11] = 0; if (strcmp(name, "iptrace 2.0") != 0) { - return WTAP_FILE_UNKNOWN; + return 0; } - wth->subtype_read = iptrace_read; - return WTAP_FILE_IPTRACE; + wth->file_type = WTAP_FILE_IPTRACE; + wth->subtype_read = iptrace_read; + return 1; } /* Read the next packet */ -int iptrace_read(wtap *wth) +static int iptrace_read(wtap *wth, int *err) { int bytes_read; int data_offset; @@ -60,8 +68,13 @@ int iptrace_read(wtap *wth) char if_name1, if_name2; /* Read the descriptor data */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(header, 1, 40, wth->fh); if (bytes_read != 40) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } /* because of the way we have to kill the iptrace command, * the existence of a partial header or packet is probable, * and we should not complain about it. Simply return @@ -76,6 +89,7 @@ int iptrace_read(wtap *wth) /* Read the packet data */ buffer_assure_space(wth->frame_buffer, packet_size); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); @@ -84,10 +98,15 @@ int iptrace_read(wtap *wth) * pretend that we reached the end of the file * normally. If, however, there was a read error * because of some other reason, complain + * + * XXX - this was returning -1 even on a partial + * packet; should we return 0 on a partial packet, + * so that "wtap_loop()" quits and reports success? */ - if (ferror(wth->fh)) { - g_print("iptrace_read: fread for data: read error\n"); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/iptrace.h b/wiretap/iptrace.h index 2c883dc44b..e3d5dc7374 100644 --- a/wiretap/iptrace.h +++ b/wiretap/iptrace.h @@ -1,6 +1,6 @@ /* iptrace.h * - * $Id: iptrace.h,v 1.1 1999/01/03 04:30:13 gram Exp $ + * $Id: iptrace.h,v 1.2 1999/08/19 05:31:38 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -36,5 +36,4 @@ struct iptrace_pkt_hdr { #endif -int iptrace_open(wtap *wth); -int iptrace_read(wtap *wth); +int iptrace_open(wtap *wth, int *err); diff --git a/wiretap/lanalyzer.c b/wiretap/lanalyzer.c index 21b5b0b327..898d89e1ed 100644 --- a/wiretap/lanalyzer.c +++ b/wiretap/lanalyzer.c @@ -1,6 +1,6 @@ /* lanalyzer.c * - * $Id: lanalyzer.c,v 1.10 1999/07/13 02:53:24 gram Exp $ + * $Id: lanalyzer.c,v 1.11 1999/08/19 05:31:34 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -24,12 +24,32 @@ #include "config.h" #endif #include <stdlib.h> +#include <errno.h> #include <time.h> #include "wtap.h" #include "buffer.h" #include "lanalyzer.h" -int lanalyzer_open(wtap *wth) +/* The LANalyzer format is documented (at least in part) in Novell document + TID022037, which can be found at, among other places: + + http://www.hackzone.ru/nsp/info/nw/lan/trace.txt + */ + +/* Record types. */ +#define REC_TRACE_HEADER 0x1001 +#define REC_CYCLIC_TRACE_HEADER 0x1007 +#define REC_TRACE_SUMMARY 0x1002 +#define REC_TRACE_PACKET_DATA 0x1005 + +/* LANalyzer board types (which indicate the type of network on which + the capture was done). */ +#define BOARD_325 226 /* LANalyzer 325 (Ethernet) */ +#define BOARD_325TR 227 /* LANalyzer 325TR (Token-ring) */ + +static int lanalyzer_read(wtap *wth, int *err); + +int lanalyzer_open(wtap *wth, int *err) { int bytes_read; char record_type[2]; @@ -41,21 +61,26 @@ int lanalyzer_open(wtap *wth) struct tm tm; fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); bytes_read += fread(record_length, 1, 2, wth->fh); - type = pletohs(record_type); - length = pletohs(record_length); /* make sure to do this for while() loop */ - if (bytes_read != 4) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } + type = pletohs(record_type); + length = pletohs(record_length); /* make sure to do this for while() loop */ - if (type != 0x1001 && type != 0x1007) { - return WTAP_FILE_UNKNOWN; + if (type != REC_TRACE_HEADER && type != REC_CYCLIC_TRACE_HEADER) { + return 0; } /* If we made it this far, then the file is a LANAlyzer file. * Let's get some info from it */ + wth->file_type = WTAP_FILE_LANALYZER; wth->capture.lanalyzer = g_malloc(sizeof(lanalyzer_t)); wth->subtype_read = lanalyzer_read; /* wth->snapshot_length = 16384; */ /* available in header as 'mxslc' */ @@ -64,11 +89,17 @@ int lanalyzer_open(wtap *wth) while (1) { fseek(wth->fh, length, SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); bytes_read += fread(record_length, 1, 2, wth->fh); if (bytes_read != 4) { + if (ferror(wth->fh)) { + *err = errno; + free(wth->capture.lanalyzer); + return -1; + } free(wth->capture.lanalyzer); - return WTAP_FILE_UNKNOWN; + return 0; } type = pletohs(record_type); @@ -77,8 +108,19 @@ int lanalyzer_open(wtap *wth) /* g_message("Record 0x%04X Length %d", type, length);*/ switch (type) { /* Trace Summary Record */ - case 0x1002: - fread(summary, 1, 210, wth->fh); + case REC_TRACE_SUMMARY: + errno = WTAP_ERR_CANT_READ; + bytes_read = fread(summary, 1, sizeof summary, + wth->fh); + if (bytes_read != sizeof summary) { + if (ferror(wth->fh)) { + *err = errno; + free(wth->capture.lanalyzer); + return -1; + } + free(wth->capture.lanalyzer); + return 0; + } /* Assume that the date of the creation of the trace file * is the same date of the trace. Lanalyzer doesn't @@ -114,10 +156,10 @@ int lanalyzer_open(wtap *wth) length = 0; /* to fake the next iteration of while() */ board_type = pletohs(&summary[188]); switch (board_type) { - case 226: + case BOARD_325: wth->file_encap = WTAP_ENCAP_ETHERNET; break; - case 227: + case BOARD_325TR: wth->file_encap = WTAP_ENCAP_TR; break; default: @@ -126,9 +168,9 @@ int lanalyzer_open(wtap *wth) break; /* Trace Packet Data Record */ - case 0x1005: + case REC_TRACE_PACKET_DATA: wth->capture.lanalyzer->pkt_len = length - 32; - return WTAP_FILE_LANALYZER; + return 1; /* default: no default action */ /* printf("Record 0x%04X Length %d\n", type, length);*/ @@ -136,42 +178,63 @@ int lanalyzer_open(wtap *wth) } /* never gets here */ - return WTAP_FILE_LANALYZER; + return 0; } +#define DESCRIPTOR_LEN 32 + /* Read the next packet */ -int lanalyzer_read(wtap *wth) +static int lanalyzer_read(wtap *wth, int *err) { int packet_size = wth->capture.lanalyzer->pkt_len; /* slice, really */ int bytes_read; char record_type[2]; char record_length[2]; guint16 type, length; - gchar descriptor[32]; + gchar descriptor[DESCRIPTOR_LEN]; int data_offset; guint16 time_low, time_med, time_high, true_size; double t; /* If this is the very first packet, then the fh cursor will already - * be at the start of the packet data instead of at the start of the Trace - * Packet Data Record. Check for this */ + * be at the start of the packet data instead of at the start of the + * Trace Packet Data Record. Check for this */ if (!packet_size) { - - /* Increment fh cursor to next record */ + /* This isn't the first packet (the record type and length + * of which we've already read in the loop in the open + * routine); read the record type and length. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); - bytes_read += fread(record_length, 1, 2, wth->fh); - if (bytes_read != 4) { + if (bytes_read != 2) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + if (bytes_read != 0) { + *err = WTAP_ERR_SHORT_READ; + return -1; + } return 0; } + bytes_read = fread(record_length, 1, 2, wth->fh); + if (bytes_read != 2) { + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; + } type = pletohs(record_type); length = pletohs(record_length); - if (type != 0x1005) { + if (type != REC_TRACE_PACKET_DATA) { + /* XXX - return -1 and set "*err" to + * WTAP_ERR_BAD_RECORD? */ return 0; } else { - packet_size = length - 32; + packet_size = length - DESCRIPTOR_LEN; } } else { @@ -179,26 +242,28 @@ int lanalyzer_read(wtap *wth) } /* Read the descriptor data */ - bytes_read = fread(descriptor, 1, 32, wth->fh); - if (bytes_read != 32) { - g_error("lanalyzer_read: not enough descriptor data (%d bytes)", - bytes_read); - return 0; + errno = WTAP_ERR_CANT_READ; + bytes_read = fread(descriptor, 1, DESCRIPTOR_LEN, wth->fh); + if (bytes_read != DESCRIPTOR_LEN) { + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; } /* Read the packet data */ buffer_assure_space(wth->frame_buffer, packet_size); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); if (bytes_read != packet_size) { - if (ferror(wth->fh)) { - g_error("lanalyzer_read: fread for data: read error\n"); - } else { - g_error("lanalyzer_read: fread for data: %d bytes out of %d read", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/lanalyzer.h b/wiretap/lanalyzer.h index 3efd992ba1..336051bd1f 100644 --- a/wiretap/lanalyzer.h +++ b/wiretap/lanalyzer.h @@ -1,6 +1,6 @@ /* lanalyzer.h * - * $Id: lanalyzer.h,v 1.1 1998/11/12 06:01:23 gram Exp $ + * $Id: lanalyzer.h,v 1.2 1999/08/19 05:31:34 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -21,7 +21,4 @@ * */ - - -int lanalyzer_open(wtap *wth); -int lanalyzer_read(wtap *wth); +int lanalyzer_open(wtap *wth, int *err); diff --git a/wiretap/libpcap.c b/wiretap/libpcap.c index 9dffe50627..d2445db79e 100644 --- a/wiretap/libpcap.c +++ b/wiretap/libpcap.c @@ -1,6 +1,6 @@ /* libpcap.c * - * $Id: libpcap.c,v 1.9 1999/08/18 17:08:47 guy Exp $ + * $Id: libpcap.c,v 1.10 1999/08/19 05:31:37 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -70,7 +70,7 @@ struct pcaprec_hdr { guint32 orig_len; /* actual length of packet */ }; -static int libpcap_read(wtap *wth); +static int libpcap_read(wtap *wth, int *err); static int libpcap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr, const u_char *pd, int *err); static int libpcap_dump_close(wtap_dumper *wdh, int *err); @@ -99,8 +99,7 @@ static const int pcap_encap[] = { }; #define NUM_PCAP_ENCAPS (sizeof pcap_encap / sizeof pcap_encap[0]) -/* Returns WTAP_FILE_PCAP on success, WTAP_FILE_UNKNOWN on failure */ -int libpcap_open(wtap *wth) +int libpcap_open(wtap *wth, int *err) { int bytes_read; guint32 magic; @@ -109,10 +108,14 @@ int libpcap_open(wtap *wth) /* Read in the number that should be at the start of a "libpcap" file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&magic, 1, sizeof magic, wth->fh); - if (bytes_read != sizeof magic) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (magic == PCAP_SWAPPED_MAGIC) { @@ -121,13 +124,18 @@ int libpcap_open(wtap *wth) byte_swapped = 1; } if (magic != PCAP_MAGIC) { - return WTAP_FILE_UNKNOWN; + return 0; } /* Read the rest of the header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (byte_swapped) { @@ -139,14 +147,19 @@ int libpcap_open(wtap *wth) } if (hdr.version_major < 2) { /* We only support version 2.0 and later. */ - return WTAP_FILE_UNKNOWN; + g_message("pcap: major version %d unsupported", + hdr.version_major); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } if (hdr.network >= NUM_PCAP_ENCAPS) { - g_error("pcap: network type %d unknown", hdr.network); - return WTAP_FILE_UNKNOWN; + g_message("pcap: network type %d unknown", hdr.network); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } /* This is a libpcap file */ + wth->file_type = WTAP_FILE_PCAP; wth->capture.pcap = g_malloc(sizeof(libpcap_t)); wth->capture.pcap->byte_swapped = byte_swapped; wth->capture.pcap->version_major = hdr.version_major; @@ -154,12 +167,11 @@ int libpcap_open(wtap *wth) wth->subtype_read = libpcap_read; wth->file_encap = pcap_encap[hdr.network]; wth->snapshot_length = hdr.snaplen; - - return WTAP_FILE_PCAP; + return 1; } /* Read the next packet */ -static int libpcap_read(wtap *wth) +static int libpcap_read(wtap *wth, int *err) { int packet_size; int bytes_read; @@ -167,11 +179,15 @@ static int libpcap_read(wtap *wth) int data_offset; /* Read record header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } if (bytes_read != 0) { - g_error("pcap_read: not enough packet header data (%d bytes)", - bytes_read); + *err = WTAP_ERR_SHORT_READ; return -1; } return 0; @@ -207,16 +223,15 @@ static int libpcap_read(wtap *wth) packet_size = hdr.incl_len; buffer_assure_space(wth->frame_buffer, packet_size); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); if (bytes_read != packet_size) { - if (ferror(wth->fh)) { - g_error("pcap_read: fread for data: read error\n"); - } else { - g_error("pcap_read: fread for data: %d bytes out of %d", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/libpcap.h b/wiretap/libpcap.h index 2bd1449996..740f6a7a05 100644 --- a/wiretap/libpcap.h +++ b/wiretap/libpcap.h @@ -1,6 +1,6 @@ /* libpcap.h * - * $Id: libpcap.h,v 1.2 1999/08/18 04:17:36 guy Exp $ + * $Id: libpcap.h,v 1.3 1999/08/19 05:31:37 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -21,5 +21,5 @@ * */ -int libpcap_open(wtap *wth); +int libpcap_open(wtap *wth, int *err); int libpcap_dump_open(wtap_dumper *wdh, int *err); diff --git a/wiretap/netmon.c b/wiretap/netmon.c index 32ce63e1b1..77afdaeaf2 100644 --- a/wiretap/netmon.c +++ b/wiretap/netmon.c @@ -1,6 +1,6 @@ /* netmon.c * - * $Id: netmon.c,v 1.9 1999/08/18 04:17:38 guy Exp $ + * $Id: netmon.c,v 1.10 1999/08/19 05:31:36 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -23,6 +23,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include <errno.h> #include <time.h> #include "wtap.h" #include "buffer.h" @@ -89,8 +90,9 @@ struct netmonrec_2_x_hdr { guint32 incl_len; /* number of octets captured in file */ }; -/* Returns WTAP_FILE_NETMON on success, WTAP_FILE_UNKNOWN on failure */ -int netmon_open(wtap *wth) +static int netmon_read(wtap *wth, int *err); + +int netmon_open(wtap *wth, int *err) { int bytes_read; char magic[sizeof netmon_1_x_magic]; @@ -116,21 +118,30 @@ int netmon_open(wtap *wth) /* Read in the string that should be at the start of a Network * Monitor file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(magic, 1, sizeof magic, wth->fh); - if (bytes_read != sizeof magic) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (memcmp(magic, netmon_1_x_magic, sizeof netmon_1_x_magic) != 0 && memcmp(magic, netmon_2_x_magic, sizeof netmon_1_x_magic) != 0) { - return WTAP_FILE_UNKNOWN; + return 0; } /* Read the rest of the header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } switch (hdr.ver_major) { @@ -144,16 +155,20 @@ int netmon_open(wtap *wth) break; default: - return WTAP_FILE_UNKNOWN; + g_message("netmon: major version %d unsupported", hdr.ver_major); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } hdr.network = pletohs(&hdr.network); if (hdr.network >= NUM_NETMON_ENCAPS) { - g_error("netmon: network type %d unknown", hdr.network); - return WTAP_FILE_UNKNOWN; + g_message("netmon: network type %d unknown", hdr.network); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } /* This is a netmon file */ + wth->file_type = file_type; wth->capture.netmon = g_malloc(sizeof(netmon_t)); wth->subtype_read = netmon_read; wth->file_encap = netmon_encap[hdr.network]; @@ -198,11 +213,11 @@ int netmon_open(wtap *wth) /* Seek to the beginning of the data records. */ fseek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET); - return file_type; + return 1; } /* Read the next packet */ -int netmon_read(wtap *wth) +static int netmon_read(wtap *wth, int *err) { int packet_size = 0; int bytes_read; @@ -234,11 +249,15 @@ int netmon_read(wtap *wth) hdr_size = sizeof (struct netmonrec_2_x_hdr); break; } + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, hdr_size, wth->fh); if (bytes_read != hdr_size) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } if (bytes_read != 0) { - g_error("netmon_read: not enough packet header data (%d bytes)", - bytes_read); + *err = WTAP_ERR_SHORT_READ; return -1; } return 0; @@ -256,16 +275,15 @@ int netmon_read(wtap *wth) break; } buffer_assure_space(wth->frame_buffer, packet_size); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); if (bytes_read != packet_size) { - if (ferror(wth->fh)) { - g_error("netmon_read: fread for data: read error\n"); - } else { - g_error("netmon_read: fread for data: %d bytes out of %d", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/netmon.h b/wiretap/netmon.h index 6fef1cf350..a186261793 100644 --- a/wiretap/netmon.h +++ b/wiretap/netmon.h @@ -1,6 +1,6 @@ /* netmon.h * - * $Id: netmon.h,v 1.1 1999/01/18 21:34:54 gram Exp $ + * $Id: netmon.h,v 1.2 1999/08/19 05:31:36 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -21,5 +21,4 @@ * */ -int netmon_open(wtap *wth); -int netmon_read(wtap *wth); +int netmon_open(wtap *wth, int *err); diff --git a/wiretap/netxray.c b/wiretap/netxray.c index 17ce354b43..1f8391d41f 100644 --- a/wiretap/netxray.c +++ b/wiretap/netxray.c @@ -1,6 +1,6 @@ /* netxray.c * - * $Id: netxray.c,v 1.9 1999/08/18 04:17:37 guy Exp $ + * $Id: netxray.c,v 1.10 1999/08/19 05:31:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -25,6 +25,7 @@ #endif #include <stdlib.h> +#include <errno.h> #include <time.h> #include "wtap.h" #include "netxray.h" @@ -86,8 +87,9 @@ struct netxrayrec_2_x_hdr { guint32 xxx[7]; /* unknown */ }; -/* Returns WTAP_FILE_NETXRAY on success, WTAP_FILE_UNKNOWN on failure */ -int netxray_open(wtap *wth) +static int netxray_read(wtap *wth, int *err); + +int netxray_open(wtap *wth, int *err) { int bytes_read; char magic[sizeof netxray_magic]; @@ -114,20 +116,29 @@ int netxray_open(wtap *wth) /* Read in the string that should be at the start of a NetXRay * file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(magic, 1, sizeof magic, wth->fh); - if (bytes_read != sizeof magic) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (memcmp(magic, netxray_magic, sizeof netxray_magic) != 0) { - return WTAP_FILE_UNKNOWN; + return 0; } /* Read the rest of the header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } /* It appears that version 1.1 files (as produced by Windows @@ -150,16 +161,20 @@ int netxray_open(wtap *wth) version_major = 2; file_type = WTAP_FILE_NETXRAY_2_001; } else { - return WTAP_FILE_UNKNOWN; + g_message("netxray: version \"%.8s\" unsupported", hdr.version); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } hdr.network = pletohs(&hdr.network); if (hdr.network >= NUM_NETXRAY_ENCAPS) { - g_error("netxray: network type %d unknown", hdr.network); - return WTAP_FILE_UNKNOWN; + g_message("netxray: network type %d unknown", hdr.network); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } /* This is a netxray file */ + wth->file_type = file_type; wth->capture.netxray = g_malloc(sizeof(netxray_t)); wth->subtype_read = netxray_read; wth->file_encap = netxray_encap[hdr.network]; @@ -183,11 +198,11 @@ int netxray_open(wtap *wth) /* Seek to the beginning of the data records. */ fseek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET); - return file_type; + return 1; } /* Read the next packet */ -int netxray_read(wtap *wth) +static int netxray_read(wtap *wth, int *err) { int packet_size; int bytes_read; @@ -217,11 +232,15 @@ reread: hdr_size = sizeof (struct netxrayrec_2_x_hdr); break; } + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, hdr_size, wth->fh); if (bytes_read != hdr_size) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } if (bytes_read != 0) { - g_error("netxray_read: not enough packet header data (%d bytes)", - bytes_read); + *err = WTAP_ERR_SHORT_READ; return -1; } @@ -240,16 +259,15 @@ reread: packet_size = pletohs(&hdr.hdr_1_x.incl_len); buffer_assure_space(wth->frame_buffer, packet_size); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); if (bytes_read != packet_size) { - if (ferror(wth->fh)) { - g_error("netxray_read: fread for data: read error\n"); - } else { - g_error("netxray_read: fread for data: %d bytes out of %d", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/netxray.h b/wiretap/netxray.h index e66e324b94..a21744b2ff 100644 --- a/wiretap/netxray.h +++ b/wiretap/netxray.h @@ -1,6 +1,6 @@ /* netxray.h * - * $Id: netxray.h,v 1.1 1999/02/20 06:49:27 guy Exp $ + * $Id: netxray.h,v 1.2 1999/08/19 05:31:36 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -21,5 +21,4 @@ * */ -int netxray_open(wtap *wth); -int netxray_read(wtap *wth); +int netxray_open(wtap *wth, int *err); diff --git a/wiretap/ngsniffer.c b/wiretap/ngsniffer.c index 03b429a709..bd7bc0fe37 100644 --- a/wiretap/ngsniffer.c +++ b/wiretap/ngsniffer.c @@ -1,6 +1,6 @@ /* ngsniffer.c * - * $Id: ngsniffer.c,v 1.14 1999/08/02 02:04:37 guy Exp $ + * $Id: ngsniffer.c,v 1.15 1999/08/19 05:31:33 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -60,6 +60,7 @@ #endif #include <stdlib.h> +#include <errno.h> #include <time.h> #include "wtap.h" #include "buffer.h" @@ -259,11 +260,12 @@ static int sniffer_encap[] = { WTAP_ENCAP_NONE /* ATM */ }; -static int get_atm_linktype(wtap *wth); +static int ngsniffer_read(wtap *wth, int *err); + +static int get_atm_linktype(wtap *wth, int *err); static int linktype_for_packet(u_int app_traf_type, u_int app_hl_type); -/* Returns WTAP_FILE_NGSNIFFER on success, WTAP_FILE_UNKNOWN on failure */ -int ngsniffer_open(wtap *wth) +int ngsniffer_open(wtap *wth, int *err) { int bytes_read; char magic[18]; @@ -278,32 +280,35 @@ int ngsniffer_open(wtap *wth) /* Read in the string that should be at the start of a Sniffer file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(magic, 1, 17, wth->fh); - if (bytes_read != 17) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } magic[17] = 0; if (strcmp(magic, "TRSNIFF data \x1a")) { - return WTAP_FILE_UNKNOWN; + return 0; } - /* This is a ngsniffer file */ - wth->capture.ngsniffer = g_malloc(sizeof(ngsniffer_t)); - wth->subtype_read = ngsniffer_read; - wth->snapshot_length = 16384; /* not available in header, only in frame */ - /* * Read the first record, which the manual says is a version * record. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); bytes_read += fread(record_length, 1, 4, wth->fh); if (bytes_read != 6) { - free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } type = pletohs(record_type); @@ -311,39 +316,49 @@ int ngsniffer_open(wtap *wth) if (type != REC_VERS) { g_message("ngsniffer: Sniffer file doesn't start with a version record"); - free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; + *err = WTAP_ERR_BAD_RECORD; + return -1; } - fread(&version, 1, sizeof version, wth->fh); + errno = WTAP_ERR_CANT_READ; + bytes_read = fread(&version, 1, sizeof version, wth->fh); + if (bytes_read != sizeof version) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; + } /* Make sure this is an uncompressed Sniffer file */ if (version.format != 1) { g_message("ngsniffer: Compressed Sniffer files are not supported"); - free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; + *err = WTAP_ERR_UNSUPPORTED; + return -1; } - /* Get data link type */ + /* Check the data link type */ if (version.network >= NUM_NGSNIFF_ENCAPS) { g_message("ngsniffer: network type %d unknown", version.network); - free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; - } - else { - wth->file_encap = sniffer_encap[version.network]; + *err = WTAP_ERR_UNSUPPORTED; + return -1; } - /* Get time unit */ + /* Check the time unit */ if (version.timeunit >= NUM_NGSNIFF_TIMEUNITS) { g_message("ngsniffer: Unknown timeunit %d", version.timeunit); - free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; - } - else { - wth->capture.ngsniffer->timeunit = Usec[version.timeunit]; + *err = WTAP_ERR_UNSUPPORTED; + return -1; } + /* This is a ngsniffer file */ + wth->file_type = WTAP_FILE_NGSNIFFER; + wth->capture.ngsniffer = g_malloc(sizeof(ngsniffer_t)); + wth->subtype_read = ngsniffer_read; + wth->snapshot_length = 16384; /* not available in header, only in frame */ + wth->capture.ngsniffer->timeunit = Usec[version.timeunit]; + wth->file_encap = sniffer_encap[version.network]; + /* Get capture start time */ start_time = pletohs(&version.time); start_date = pletohs(&version.date); @@ -405,23 +420,28 @@ int ngsniffer_open(wtap *wth) * to cope with raw cells or other types of frames * anyway; the only place you lose is with FORE * SPANS. + * + * XXX - eventually add a "Sniffer ATM encapsulation" + * that returns the full data, including the ATM + * pseudo-header. */ - wth->file_encap = get_atm_linktype(wth); + *err = WTAP_ERR_UNSUPPORTED; + wth->file_encap = get_atm_linktype(wth, err); if (wth->file_encap == -1) { /* * Oops, we couldn't find a link type we can * handle. */ - g_message("ngsniffer: no LANE or RFC 1483 LLC-multiplexed frames found"); free(wth->capture.ngsniffer); - return WTAP_FILE_UNKNOWN; + g_message("ngsniffer: no LANE or RFC 1483 LLC-multiplexed frames found"); + return -1; } wth->capture.ngsniffer->is_atm = 1; } - return WTAP_FILE_NGSNIFFER; + return 1; } -static int get_atm_linktype(wtap *wth) +static int get_atm_linktype(wtap *wth, int *err) { int bytes_read; char record_type[2]; @@ -435,15 +455,40 @@ static int get_atm_linktype(wtap *wth) /* * Read the record header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); - bytes_read += fread(record_length, 1, 4, wth->fh); - if (bytes_read != 6) { + if (bytes_read != 2) { /* * End of file or error. Probably means there *are* * no LANE or RFC 1483 LLC-multiplexed frames, * which means we can't handle this in * "wiretap". Return an error. */ + if (ferror(wth->fh)) + *err = errno; + else { + /* + * If we read no bytes at all, treat + * that as an EOF, not a short read; + * "*err" got set initially to + * WTAP_ERR_UNSUPPORTED, which is the + * most appropriate error if we just + * ran out of packets without seeing + * any LANE or RFC 1483 LLC-multiplexed + * frames. + */ + if (bytes_read != 0) + *err = WTAP_ERR_SHORT_READ; + } + return -1; + } + errno = WTAP_ERR_CANT_READ; + bytes_read = fread(record_length, 1, 4, wth->fh); + if (bytes_read != 4) { + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } @@ -455,7 +500,11 @@ static int get_atm_linktype(wtap *wth) * End of file. Probably means there *are* * no LANE or RFC 1483 LLC-multiplexed frames, * which means we can't handle this in - * "wiretap". Return an error. + * "wiretap". "*err" got set initially to + * WTAP_ERR_UNSUPPORTED, which is the most + * appropriate error if we just ran out of + * packets without seeing any LANE or RFC + * 1483 LLC-multiplexed frames. */ return -1; } @@ -467,16 +516,19 @@ static int get_atm_linktype(wtap *wth) * Umm, we're not supposed to get these in an * ATM Sniffer file; return an error. */ + g_message("ngsniffer: REC_FRAME2 record in an ATM Sniffer file"); + *err = WTAP_ERR_BAD_RECORD; return -1; case REC_FRAME4: /* Read the f_frame4_struct */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&frame4, 1, sizeof frame4, wth->fh); if (bytes_read != sizeof frame4) { - /* - * Read error or short record. Return - * an error. - */ + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } time_low = pletohs(&frame4.time_low); @@ -572,9 +624,8 @@ static int linktype_for_packet(u_int app_traf_type, u_int app_hl_type) } /* Read the next packet */ -int ngsniffer_read(wtap *wth) +static int ngsniffer_read(wtap *wth, int *err) { - int packet_size = wth->capture.ngsniffer->pkt_len; int bytes_read; char record_type[2]; char record_length[4]; /* only 1st 2 bytes are length */ @@ -604,14 +655,28 @@ int ngsniffer_read(wtap *wth) /* * Read the record header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(record_type, 1, 2, wth->fh); - bytes_read += fread(record_length, 1, 4, wth->fh); - if (bytes_read != 6) { - /* - * End of file or error. - */ + if (bytes_read != 2) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + if (bytes_read != 0) { + *err = WTAP_ERR_SHORT_READ; + return -1; + } return 0; } + errno = WTAP_ERR_CANT_READ; + bytes_read = fread(record_length, 1, 4, wth->fh); + if (bytes_read != 4) { + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; + } type = pletohs(record_type); length = pletohs(record_length); @@ -620,15 +685,14 @@ int ngsniffer_read(wtap *wth) case REC_FRAME2: /* Read the f_frame2_struct */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&frame2, 1, sizeof frame2, wth->fh); if (bytes_read != sizeof frame2) { - /* - * Read error or short record. Return - * an error. - */ - g_message("ngsniffer_read: not enough frame2 data (%d bytes)", - bytes_read); - return 0; + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; } time_low = pletohs(&frame2.time_low); time_med = pletohs(&frame2.time_med); @@ -647,15 +711,14 @@ int ngsniffer_read(wtap *wth) case REC_FRAME4: /* Read the f_frame4_struct */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&frame4, 1, sizeof frame4, wth->fh); if (bytes_read != sizeof frame4) { - /* - * Read error or short record. Return - * an error. - */ - g_message("ngsniffer_read: not enough frame4 data (%d bytes)", - bytes_read); - return 0; + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; } time_low = pletohs(&frame4.time_low); time_med = pletohs(&frame4.time_med); @@ -732,16 +795,15 @@ found: */ buffer_assure_space(wth->frame_buffer, length); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, length, wth->fh); if (bytes_read != length) { - if (ferror(wth->fh)) { - g_message("ngsniffer_read: fread for data: read error\n"); - } else { - g_message("ngsniffer_read: fread for data: %d bytes out of %d", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/radcom.c b/wiretap/radcom.c index 4ec27bf4a8..09d47efb71 100644 --- a/wiretap/radcom.c +++ b/wiretap/radcom.c @@ -23,13 +23,11 @@ #endif #include <stdlib.h> +#include <errno.h> #include <time.h> #include "wtap.h" #include "buffer.h" #include "radcom.h" -#ifdef HAVE_NETINET_IN_H -#include <netinet/in.h> -#endif struct frame_date { guint16 year; @@ -43,8 +41,9 @@ static char radcom_magic[8] = { 0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88 }; -/* Returns WTAP_FILE_RADCOM on success, WTAP_FILE_UNKNOWN on failure */ -int radcom_open(wtap *wth) +static int radcom_read(wtap *wth, int *err); + +int radcom_open(wtap *wth, int *err) { int bytes_read; char magic[8]; @@ -56,40 +55,60 @@ int radcom_open(wtap *wth) /* Read in the string that should be at the start of a Sniffer file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(magic, 1, 8, wth->fh); - if (bytes_read != 8) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (memcmp(magic, radcom_magic, 8)) { - return WTAP_FILE_UNKNOWN; + return 0; } - /* This is a radcom file */ - wth->capture.radcom = g_malloc(sizeof(radcom_t)); - wth->subtype_read = radcom_read; - wth->snapshot_length = 16384; /* not available in header, only in frame */ fseek(wth->fh, 0x8B, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&byte, 1, 1, wth->fh); if (bytes_read != 1) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } while (byte) { + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&byte, 1, 1, wth->fh); if (bytes_read != 1) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } } fseek(wth->fh, 1, SEEK_CUR); /* Get capture start time */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&start_date, 1, sizeof(struct frame_date), wth->fh); - if (bytes_read != sizeof(struct frame_date)) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } + /* This is a radcom file */ + wth->file_type = WTAP_FILE_RADCOM; + wth->capture.radcom = g_malloc(sizeof(radcom_t)); + wth->subtype_read = radcom_read; + wth->snapshot_length = 16384; /* not available in header, only in frame */ + tm.tm_year = start_date.year-1900; tm.tm_mon = start_date.month-1; tm.tm_mday = start_date.day; @@ -101,40 +120,48 @@ int radcom_open(wtap *wth) fseek(wth->fh, sizeof(struct frame_date), SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(search_encap, 1, 7, wth->fh); if (bytes_read != 7) { - return WTAP_FILE_UNKNOWN; + goto read_error; } while (memcmp(encap_magic, search_encap, 7)) { fseek(wth->fh, -6, SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(search_encap, 1, 7, wth->fh); if (bytes_read != 7) { - return WTAP_FILE_UNKNOWN; + goto read_error; } } fseek(wth->fh, 12, SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(search_encap, 1, 4, wth->fh); if (bytes_read != 4) { - return WTAP_FILE_UNKNOWN; + goto read_error; } if (!memcmp(search_encap, "LAPB", 4)) wth->file_encap = WTAP_ENCAP_LAPB; else if (!memcmp(search_encap, "Ethe", 4)) wth->file_encap = WTAP_ENCAP_ETHERNET; - else - return WTAP_FILE_UNKNOWN; + else { + g_message("pcap: network type \"%.4s\" unknown", search_encap); + *err = WTAP_ERR_UNSUPPORTED; + return -1; + } /*bytes_read = fread(&next_date, 1, sizeof(struct frame_date), wth->fh); + errno = WTAP_ERR_CANT_READ; if (bytes_read != sizeof(struct frame_date)) { - return WTAP_FILE_UNKNOWN; + goto read_error; } while (memcmp(&start_date, &next_date, 4)) { fseek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&next_date, 1, sizeof(struct frame_date), wth->fh); if (bytes_read != sizeof(struct frame_date)) { - return WTAP_FILE_UNKNOWN; + goto read_error; } }*/ @@ -143,11 +170,20 @@ int radcom_open(wtap *wth) else if (wth->file_encap == WTAP_ENCAP_LAPB) fseek(wth->fh, 297, SEEK_CUR); - return WTAP_FILE_RADCOM; + return 1; + +read_error: + if (ferror(wth->fh)) { + *err = errno; + free(wth->capture.radcom); + return -1; + } + free(wth->capture.radcom); + return 0; } /* Read the next packet */ -int radcom_read(wtap *wth) +static int radcom_read(wtap *wth, int *err) { int bytes_read; guint16 length; @@ -161,30 +197,35 @@ int radcom_read(wtap *wth) /* * Read the frame size */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&length, 1, 2, wth->fh); if (bytes_read != 2) { - /* - * End of file or error. - */ - g_message("radcom_read: not enough frame data (%d bytes)", - bytes_read); + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + if (bytes_read != 0) { + *err = WTAP_ERR_SHORT_READ; + return -1; + } return 0; } - if (wth->file_encap == WTAP_ENCAP_LAPB) length -= 2; /* FCS */ + if (wth->file_encap == WTAP_ENCAP_LAPB) + length -= 2; /* FCS */ wth->phdr.len = length; wth->phdr.caplen = length; fseek(wth->fh, 5, SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&date, 1, sizeof(struct frame_date), wth->fh); if (bytes_read != sizeof(struct frame_date)) { - /* - * End of file or error. - */ - g_message("radcom_read: not enough frame data (%d bytes)", - bytes_read); - return 0; + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; } tm.tm_year = date.year-1900; @@ -198,14 +239,14 @@ int radcom_read(wtap *wth) wth->phdr.ts.tv_usec = date.usec; fseek(wth->fh, 6, SEEK_CUR); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&dce, 1, 1, wth->fh); if (bytes_read != 1) { - /* - * End of file or error. - */ - g_message("radcom_read: not enough frame data (%d bytes)", - bytes_read); - return 0; + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; + return -1; } wth->phdr.flags = (dce & 0x1) ? 0x00 : 0x80; @@ -216,16 +257,15 @@ int radcom_read(wtap *wth) */ buffer_assure_space(wth->frame_buffer, length); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, length, wth->fh); if (bytes_read != length) { - if (ferror(wth->fh)) { - g_message("radcom_read: fread for data: read error\n"); - } else { - g_message("radcom_read: fread for data: %d bytes out of %d", - bytes_read, length); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/radcom.h b/wiretap/radcom.h index 044ad6831a..d4492316c8 100644 --- a/wiretap/radcom.h +++ b/wiretap/radcom.h @@ -19,5 +19,4 @@ * */ -int radcom_open(wtap *wth); -int radcom_read(wtap *wth); +int radcom_open(wtap *wth, int *err); diff --git a/wiretap/snoop.c b/wiretap/snoop.c index ceb6aeac7e..c5661049ba 100644 --- a/wiretap/snoop.c +++ b/wiretap/snoop.c @@ -1,6 +1,6 @@ /* snoop.c * - * $Id: snoop.c,v 1.5 1999/07/13 02:53:26 gram Exp $ + * $Id: snoop.c,v 1.6 1999/08/19 05:31:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -23,6 +23,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include <errno.h> #include "wtap.h" #include "buffer.h" #include "snoop.h" @@ -53,8 +54,9 @@ struct snooprec_hdr { guint32 ts_usec; /* timestamp microseconds */ }; -/* Returns WTAP_FILE_SNOOP on success, WTAP_FILE_UNKNOWN on failure */ -int snoop_open(wtap *wth) +static int snoop_read(wtap *wth, int *err); + +int snoop_open(wtap *wth, int *err) { int bytes_read; char magic[sizeof snoop_magic]; @@ -75,43 +77,55 @@ int snoop_open(wtap *wth) /* Read in the string that should be at the start of a "snoop" file */ fseek(wth->fh, 0, SEEK_SET); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(magic, 1, sizeof magic, wth->fh); - if (bytes_read != sizeof magic) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) { - return WTAP_FILE_UNKNOWN; + return 0; } /* Read the rest of the header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { - return WTAP_FILE_UNKNOWN; + if (ferror(wth->fh)) { + *err = errno; + return -1; + } + return 0; } hdr.version = ntohl(hdr.version); if (hdr.version != 2) { /* We only support version 2. */ - return WTAP_FILE_UNKNOWN; + g_message("snoop: version %d unsupported", hdr.version); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } hdr.network = ntohl(hdr.network); if (hdr.network >= NUM_SNOOP_ENCAPS) { - g_error("snoop: network type %d unknown", hdr.network); - return WTAP_FILE_UNKNOWN; + g_message("snoop: network type %d unknown", hdr.network); + *err = WTAP_ERR_UNSUPPORTED; + return -1; } /* This is a snoop file */ + wth->file_type = WTAP_FILE_SNOOP; wth->subtype_read = snoop_read; wth->file_encap = snoop_encap[hdr.network]; wth->snapshot_length = 16384; /* XXX - not available in header */ - - return WTAP_FILE_SNOOP; + return 1; } /* Read the next packet */ -int snoop_read(wtap *wth) +static int snoop_read(wtap *wth, int *err) { int packet_size; int bytes_read; @@ -119,11 +133,15 @@ int snoop_read(wtap *wth) int data_offset; /* Read record header. */ + errno = WTAP_ERR_CANT_READ; bytes_read = fread(&hdr, 1, sizeof hdr, wth->fh); if (bytes_read != sizeof hdr) { + if (ferror(wth->fh)) { + *err = errno; + return -1; + } if (bytes_read != 0) { - g_error("snoop_read: not enough packet header data (%d bytes)", - bytes_read); + *err = WTAP_ERR_SHORT_READ; return -1; } return 0; @@ -132,16 +150,15 @@ int snoop_read(wtap *wth) packet_size = ntohl(hdr.incl_len); buffer_assure_space(wth->frame_buffer, packet_size); data_offset = ftell(wth->fh); + errno = WTAP_ERR_CANT_READ; bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1, packet_size, wth->fh); if (bytes_read != packet_size) { - if (ferror(wth->fh)) { - g_error("snoop_read: fread for data: read error\n"); - } else { - g_error("snoop_read: fread for data: %d bytes out of %d", - bytes_read, packet_size); - } + if (ferror(wth->fh)) + *err = errno; + else + *err = WTAP_ERR_SHORT_READ; return -1; } diff --git a/wiretap/snoop.h b/wiretap/snoop.h index d1d5461de2..20e559b09c 100644 --- a/wiretap/snoop.h +++ b/wiretap/snoop.h @@ -1,6 +1,6 @@ /* snoop.h * - * $Id: snoop.h,v 1.1 1998/11/15 05:29:15 guy Exp $ + * $Id: snoop.h,v 1.2 1999/08/19 05:31:35 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -21,5 +21,4 @@ * */ -int snoop_open(wtap *wth); -int snoop_read(wtap *wth); +int snoop_open(wtap *wth, int *err); diff --git a/wiretap/wtap.c b/wiretap/wtap.c index 49f9312f42..52e96dc318 100644 --- a/wiretap/wtap.c +++ b/wiretap/wtap.c @@ -1,6 +1,6 @@ /* wtap.c * - * $Id: wtap.c,v 1.14 1999/08/18 04:17:37 guy Exp $ + * $Id: wtap.c,v 1.15 1999/08/19 05:31:33 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -127,13 +127,19 @@ void wtap_close(wtap *wth) fclose(wth->fh); } -void wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user) +int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user, + int *err) { int data_offset, loop = 0; - while ((data_offset = wth->subtype_read(wth)) > 0) { + while ((data_offset = wth->subtype_read(wth, err)) > 0) { callback(user, &wth->phdr, data_offset, buffer_start_ptr(wth->frame_buffer)); - if (count > 0 && ++loop >= count) break; + if (count > 0 && ++loop >= count) + break; } + if (data_offset < 0) + return FALSE; /* failure */ + else + return TRUE; /* success */ } diff --git a/wiretap/wtap.h b/wiretap/wtap.h index ff9c689074..3f9bcc3c69 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1,6 +1,6 @@ /* wtap.h * - * $Id: wtap.h,v 1.26 1999/08/18 17:08:47 guy Exp $ + * $Id: wtap.h,v 1.27 1999/08/19 05:31:38 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu> @@ -153,7 +153,7 @@ struct wtap; struct bpf_instruction; struct Buffer; -typedef int (*subtype_func)(struct wtap*); +typedef int (*subtype_read_func)(struct wtap*, int*); typedef struct wtap { FILE* fh; int file_type; @@ -170,7 +170,7 @@ typedef struct wtap { netxray_t *netxray; } capture; - subtype_func subtype_read; + subtype_read_func subtype_read; int file_encap; /* per-file, for those file formats that have per-file encapsulation @@ -201,7 +201,7 @@ typedef struct wtap_dumper { * a negative number, indicating the type of error, on other failures. */ wtap* wtap_open_offline(const char *filename, int *err); -void wtap_loop(wtap *wth, int, wtap_handler, u_char*); +int wtap_loop(wtap *wth, int, wtap_handler, u_char*, int*); FILE* wtap_file(wtap *wth); int wtap_snapshot_length(wtap *wth); /* per file */ @@ -228,15 +228,24 @@ int wtap_pcap_encap_to_wtap_encap(int encap); /* The file being opened for reading isn't a plain file */ #define WTAP_ERR_FILE_UNKNOWN_FORMAT -2 /* The file being opened is not a capture file in a known format */ -#define WTAP_ERR_CANT_OPEN -3 +#define WTAP_ERR_UNSUPPORTED -3 + /* Supported file type, but there's something in the file we + can't support */ +#define WTAP_ERR_CANT_OPEN -4 /* The file couldn't be opened, reason unknown */ -#define WTAP_ERR_UNSUPPORTED_FILE_TYPE -4 +#define WTAP_ERR_UNSUPPORTED_FILE_TYPE -5 /* Wiretap can't save files in the specified format */ -#define WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED -5 +#define WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED -6 /* The specified format doesn't support per-packet encapsulations */ -#define WTAP_ERR_CANT_CLOSE -6 +#define WTAP_ERR_CANT_CLOSE -7 /* The file couldn't be closed, reason unknown */ -#define WTAP_ERR_SHORT_WRITE -7 +#define WTAP_ERR_CANT_READ -8 + /* An attempt to read failed, reason unknown */ +#define WTAP_ERR_SHORT_READ -9 + /* An attempt to read read less data than it should have */ +#define WTAP_ERR_BAD_RECORD -10 + /* We read an invalid record */ +#define WTAP_ERR_SHORT_WRITE -11 /* An attempt to write wrote less data than it should have */ /* Pointer versions of ntohs and ntohl. Given a pointer to a member of a |