aboutsummaryrefslogtreecommitdiffstats
path: root/wiretap/libpcap.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>1999-08-19 05:31:38 +0000
committerGuy Harris <guy@alum.mit.edu>1999-08-19 05:31:38 +0000
commit137ba48d18e0957e5fc920e7b518ba95212789a8 (patch)
tree9ea3f0562b63feb029a13b9d17f856181e5ceecb /wiretap/libpcap.c
parent7843c01d38ea4e6e17ad9c36f94999fe2ff9866c (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/libpcap.c')
-rw-r--r--wiretap/libpcap.c59
1 files changed, 37 insertions, 22 deletions
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;
}