From 9658c33db60e0185272c6ab30879b79da9cf69a0 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 15 Dec 1999 02:25:50 +0000 Subject: Handle i4btrace captures if they're from a machine with the opposite byte order from the machine that's reading them. svn path=/trunk/; revision=1338 --- wiretap/i4btrace.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++------- wiretap/libpcap.c | 12 +----------- wiretap/wtap.h | 13 ++++++++++++- 3 files changed, 62 insertions(+), 19 deletions(-) diff --git a/wiretap/i4btrace.c b/wiretap/i4btrace.c index 2445b9c490..80df4367b7 100644 --- a/wiretap/i4btrace.c +++ b/wiretap/i4btrace.c @@ -1,6 +1,6 @@ /* i4btrace.c * - * $Id: i4btrace.c,v 1.1 1999/12/12 22:40:09 gram Exp $ + * $Id: i4btrace.c,v 1.2 1999/12/15 02:25:50 guy Exp $ * * Wiretap Library * Copyright (c) 1999 by Bert Driehuis @@ -34,10 +34,19 @@ static int i4btrace_read(wtap *wth, int *err); +/* + * Test some fields in the header to see if they make sense. + */ +#define I4B_HDR_IS_OK(hdr) \ + (!((unsigned)hdr.length < 3 || (unsigned)hdr.unit > 4 || \ + (unsigned)hdr.type > 4 || (unsigned)hdr.dir > 2 || \ + (unsigned)hdr.trunc > 2048)) + int i4btrace_open(wtap *wth, int *err) { int bytes_read; i4b_trace_hdr_t hdr; + gboolean byte_swapped = FALSE; /* I4B trace files have no magic in the header... Sigh */ file_seek(wth->fh, 0, SEEK_SET); @@ -51,10 +60,28 @@ int i4btrace_open(wtap *wth, int *err) } /* Silly heuristic... */ - if ((unsigned)hdr.length < 3 || (unsigned)hdr.unit > 4 || - (unsigned)hdr.type > 4 || (unsigned)hdr.dir > 2 || - (unsigned)hdr.trunc > 2048) - return 0; + if (!I4B_HDR_IS_OK(hdr)) { + /* + * OK, try byte-swapping the header fields. + */ + hdr.length = BSWAP32(hdr.length); + hdr.unit = BSWAP32(hdr.unit); + hdr.type = BSWAP32(hdr.type); + hdr.dir = BSWAP32(hdr.dir); + hdr.trunc = BSWAP32(hdr.trunc); + if (!I4B_HDR_IS_OK(hdr)) { + /* + * It doesn't look valid in either byte order. + */ + return 0; + } + + /* + * It looks valid byte-swapped, so assume it's a + * trace written in the opposite byte order. + */ + byte_swapped = TRUE; + } file_seek(wth->fh, 0, SEEK_SET); wth->data_offset = 0; @@ -69,6 +96,7 @@ int i4btrace_open(wtap *wth, int *err) wth->capture.i4btrace->start = hdr.time.tv_sec; wth->capture.i4btrace->bchannel_prot[0] = -1; wth->capture.i4btrace->bchannel_prot[1] = -1; + wth->capture.i4btrace->byte_swapped = byte_swapped; wth->file_encap = WTAP_ENCAP_PER_PACKET; @@ -100,8 +128,22 @@ static int i4btrace_read(wtap *wth, int *err) return 0; } wth->data_offset += sizeof hdr; - length = pletohs(&hdr.length) - sizeof(hdr); - if (length == 0) return 0; + if (wth->capture.i4btrace->byte_swapped) { + /* + * Byte-swap the header. + */ + hdr.length = BSWAP32(hdr.length); + hdr.unit = BSWAP32(hdr.unit); + hdr.type = BSWAP32(hdr.type); + hdr.dir = BSWAP32(hdr.dir); + hdr.trunc = BSWAP32(hdr.trunc); + hdr.count = BSWAP32(hdr.count); + hdr.time.tv_sec = BSWAP32(hdr.time.tv_sec); + hdr.time.tv_usec = BSWAP32(hdr.time.tv_usec); + } + length = hdr.length - sizeof(hdr); + if (length == 0) + return 0; wth->phdr.len = length; wth->phdr.caplen = length; diff --git a/wiretap/libpcap.c b/wiretap/libpcap.c index e290a3d790..92da968682 100644 --- a/wiretap/libpcap.c +++ b/wiretap/libpcap.c @@ -1,6 +1,6 @@ /* libpcap.c * - * $Id: libpcap.c,v 1.27 1999/12/11 00:40:39 guy Exp $ + * $Id: libpcap.c,v 1.28 1999/12/15 02:25:50 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -53,16 +53,6 @@ #define PCAP_MODIFIED_MAGIC 0xa1b2cd34 #define PCAP_SWAPPED_MODIFIED_MAGIC 0x34cdb2a1 -/* Macros to byte-swap 32-bit and 16-bit quantities. */ -#define BSWAP32(x) \ - ((((x)&0xFF000000)>>24) | \ - (((x)&0x00FF0000)>>8) | \ - (((x)&0x0000FF00)<<8) | \ - (((x)&0x000000FF)<<24)) -#define BSWAP16(x) \ - ((((x)&0xFF00)>>8) | \ - (((x)&0x00FF)<<8)) - /* On some systems, the FDDI MAC addresses are bit-swapped. */ #if !defined(ultrix) && !defined(__alpha) && !defined(__bsdi__) #define BIT_SWAPPED_MAC_ADDRS diff --git a/wiretap/wtap.h b/wiretap/wtap.h index da07c6fa3d..36eaca4707 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1,6 +1,6 @@ /* wtap.h * - * $Id: wtap.h,v 1.61 1999/12/15 01:34:16 guy Exp $ + * $Id: wtap.h,v 1.62 1999/12/15 02:25:50 guy Exp $ * * Wiretap Library * Copyright (c) 1998 by Gilbert Ramirez @@ -160,6 +160,7 @@ typedef struct { } radcom_t; typedef struct { + gboolean byte_swapped; time_t start; int bchannel_prot[2]; /* For the V.120 heuristic */ } i4btrace_t; @@ -475,6 +476,16 @@ int wtap_pcap_encap_to_wtap_encap(int encap); #define WTAP_ERR_ZLIB_MAX -100 #define WTAP_ERR_ZLIB_MIN -300 +/* Macros to byte-swap 32-bit and 16-bit quantities. */ +#define BSWAP32(x) \ + ((((x)&0xFF000000)>>24) | \ + (((x)&0x00FF0000)>>8) | \ + (((x)&0x0000FF00)<<8) | \ + (((x)&0x000000FF)<<24)) +#define BSWAP16(x) \ + ((((x)&0xFF00)>>8) | \ + (((x)&0x00FF)<<8)) + /* Turn host-byte-order values into little-endian values. */ #ifdef WORDS_BIGENDIAN #define htoles(s) ((guint16) \ -- cgit v1.2.3