From Ivan Sy (with minor modifications):
[obnox/wireshark/wip.git] / wiretap / i4btrace.c
index 0565cdae5250ece5bfffa2afb412e918b0b90f22..1b2bc080954189df02ec62c239006aa31c9e5644 100644 (file)
@@ -1,6 +1,6 @@
 /* i4btrace.c
  *
- * $Id: i4btrace.c,v 1.18 2002/03/05 05:58:40 guy Exp $
+ * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1999 by Bert Driehuis <driehuis@playbeing.org>
 #include "file_wrappers.h"
 #include "buffer.h"
 #include "i4b_trace.h"
+#include "i4btrace.h"
 
-static gboolean i4btrace_read(wtap *wth, int *err, long *data_offset);
-static int i4btrace_seek_read(wtap *wth, long seek_off,
-    union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err);
+static gboolean i4btrace_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset);
+static gboolean i4btrace_seek_read(wtap *wth, gint64 seek_off,
+    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
+    int *err, gchar **err_info);
 static int i4b_read_rec_header(FILE_T fh, i4b_trace_hdr_t *hdr, int *err);
 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,
+static gboolean i4b_read_rec_data(FILE_T fh, guchar *pd, int length, int *err);
+static void i4b_set_pseudo_header(i4b_trace_hdr_t *hdr,
     union wtap_pseudo_header *pseudo_header);
 static void i4btrace_close(wtap *wth);
 
@@ -46,11 +49,11 @@ static void i4btrace_close(wtap *wth);
  * 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))
+       (!((unsigned)hdr.length < 3 || (unsigned)hdr.length > 16384 || \
+           (unsigned)hdr.unit > 4 || (unsigned)hdr.type > 4 || \
+           (unsigned)hdr.dir > 2 || (unsigned)hdr.trunc > 2048))
 
-int i4btrace_open(wtap *wth, int *err)
+int i4btrace_open(wtap *wth, int *err, gchar **err_info _U_)
 {
        int bytes_read;
        i4b_trace_hdr_t hdr;
@@ -90,10 +93,8 @@ int i4btrace_open(wtap *wth, int *err)
                byte_swapped = TRUE;
        }
 
-       if (file_seek(wth->fh, 0, SEEK_SET) == -1) {
-               *err = file_error(wth->fh);
+       if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
                return -1;
-       }
        wth->data_offset = 0;
 
        /* Get capture start time */
@@ -105,25 +106,22 @@ int i4btrace_open(wtap *wth, int *err)
        wth->subtype_close = i4btrace_close;
        wth->snapshot_length = 0;       /* not known */
 
-       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;
+       wth->file_encap = WTAP_ENCAP_ISDN;
+       wth->tsprecision = WTAP_FILE_TSPREC_USEC;
 
        return 1;
 }
 
-#define V120SABME      "\010\001\177"
-
 /* Read the next packet */
-static gboolean i4btrace_read(wtap *wth, int *err, long *data_offset)
+static gboolean i4btrace_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset)
 {
        int     ret;
        i4b_trace_hdr_t hdr;
-       guint16 length;
+       guint32 length;
        void *bufp;
-       int channel;
 
        /* Read record header. */
        *data_offset = wth->data_offset;
@@ -136,22 +134,24 @@ static gboolean i4btrace_read(wtap *wth, int *err, long *data_offset)
        i4b_byte_swap_header(wth, &hdr);
        if (hdr.length < sizeof(hdr)) {
                *err = WTAP_ERR_BAD_RECORD;     /* record length < header! */
+               *err_info = g_strdup_printf("i4btrace: record length %u < header length %lu",
+                   hdr.length, (unsigned long)sizeof(hdr));
                return FALSE;
        }
-       length = hdr.length - sizeof(hdr);
+       length = hdr.length - (guint32)sizeof(hdr);
 
        wth->phdr.len = length;
        wth->phdr.caplen = length;
 
-       wth->phdr.ts.tv_sec = hdr.time.tv_sec;
-       wth->phdr.ts.tv_usec = hdr.time.tv_usec;
+       wth->phdr.ts.secs = hdr.ts_sec;
+       wth->phdr.ts.nsecs = hdr.ts_usec * 1000;
 
        /*
         * Read the packet data.
         */
        buffer_assure_space(wth->frame_buffer, length);
        bufp = buffer_start_ptr(wth->frame_buffer);
-       if (i4b_read_rec_data(wth->fh, bufp, length, err) < 0)
+       if (!i4b_read_rec_data(wth->fh, bufp, length, err))
                return FALSE;   /* Read error */
        wth->data_offset += length;
 
@@ -167,73 +167,30 @@ static gboolean i4btrace_read(wtap *wth, int *err, long *data_offset)
                break;
 
        case TRC_CH_D:
-               /*
-                * D channel, so it's LAPD.
-                */
-               wth->phdr.pkt_encap = WTAP_ENCAP_LAPD;
-               break;
-
        case TRC_CH_B1:
        case TRC_CH_B2:
                /*
-                * B channel, so it could be any of a number of things.
+                * D or B channel.
                 */
-               channel = hdr.type - TRC_CH_B1;
-
-               if (wth->capture.i4btrace->bchannel_prot[channel] == -1) {
-                       /*
-                        * We don't know yet whether the datastream is
-                        * V.120 or not; this heuristic tries to figure
-                        * that out.
-                        *
-                        * We cannot glean this from the Q.931 SETUP message,
-                        * because no commercial V.120 implementation I've
-                        * seen actually sets the V.120 protocol discriminator
-                        * (that, or I'm misreading the spec badly).
-                        *
-                        * TODO: reset the flag to -1 (unknown) after a close
-                        * on the B channel is detected.
-                        */
-                       if (memcmp(bufp, V120SABME, 3) == 0)
-                           wth->capture.i4btrace->bchannel_prot[channel] = 1;
-                       else
-                           wth->capture.i4btrace->bchannel_prot[channel] = 0;
-               }
-               if (wth->capture.i4btrace->bchannel_prot[channel] == 1) {
-                       /*
-                        * V.120.
-                        */
-                       wth->phdr.pkt_encap = WTAP_ENCAP_V120;
-               } else {
-                       /*
-                        * Not V.120.
-                        *
-                        * XXX - what is it?  It's probably not
-                        * WTAP_ENCAP_NULL, as that means it has a
-                        * 4-byte AF_ type as the encapsulation header.
-                        * If it's PPP, we should use WTAP_ENCAP_PPP here.
-                        */
-                       wth->phdr.pkt_encap = WTAP_ENCAP_NULL;
-               }
+               wth->phdr.pkt_encap = WTAP_ENCAP_ISDN;
                break;
        }
 
-       i4b_set_pseudo_header(wth, &hdr, &wth->pseudo_header);
+       i4b_set_pseudo_header(&hdr, &wth->pseudo_header);
 
        return TRUE;
 }
 
-static int
-i4btrace_seek_read(wtap *wth, long seek_off,
-    union wtap_pseudo_header *pseudo_header, u_char *pd, int length, int *err)
+static gboolean
+i4btrace_seek_read(wtap *wth, gint64 seek_off,
+    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
+    int *err, gchar **err_info _U_)
 {
        int     ret;
        i4b_trace_hdr_t hdr;
 
-       if (file_seek(wth->random_fh, seek_off, SEEK_SET) == -1) {
-               *err = file_error(wth->random_fh);
-               return -1;
-       }
+       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
+               return FALSE;
 
        /* Read record header. */
        ret = i4b_read_rec_header(wth->random_fh, &hdr, err);
@@ -243,11 +200,11 @@ i4btrace_seek_read(wtap *wth, long seek_off,
                        /* EOF means "short read" in random-access mode */
                        *err = WTAP_ERR_SHORT_READ;
                }
-               return -1;
+               return FALSE;
        }
        i4b_byte_swap_header(wth, &hdr);
 
-       i4b_set_pseudo_header(wth, &hdr, pseudo_header);
+       i4b_set_pseudo_header(&hdr, pseudo_header);
 
        /*
         * Read the packet data.
@@ -288,13 +245,13 @@ i4b_byte_swap_header(wtap *wth, i4b_trace_hdr_t *hdr)
                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);
+               hdr->ts_sec = BSWAP32(hdr->ts_sec);
+               hdr->ts_usec = BSWAP32(hdr->ts_usec);
        }
 }
 
-static int
-i4b_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
+static gboolean
+i4b_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
 {
        int     bytes_read;
 
@@ -305,41 +262,37 @@ i4b_read_rec_data(FILE_T fh, u_char *pd, int length, int *err)
                *err = file_error(fh);
                if (*err == 0)
                        *err = WTAP_ERR_SHORT_READ;
-               return -1;
+               return FALSE;
        }
-       return 0;
+       return TRUE;
 }
 
 static void
-i4b_set_pseudo_header(wtap *wth, i4b_trace_hdr_t *hdr,
+i4b_set_pseudo_header(i4b_trace_hdr_t *hdr,
     union wtap_pseudo_header *pseudo_header)
 {
-       int channel;
-
+       pseudo_header->isdn.uton = (hdr->dir == FROM_TE);
        switch (hdr->type) {
 
        case TRC_CH_D:
                /*
                 * D channel, so it's LAPD; set "p2p.sent".
                 */
-               pseudo_header->p2p.sent = (hdr->dir == FROM_TE) ? TRUE : FALSE;
+               pseudo_header->isdn.channel = 0;
                break;
 
        case TRC_CH_B1:
-       case TRC_CH_B2:
                /*
-                * B channel, so it could be any of a number of things;
-                * if it's V.120, set "x25.flags".
+                * B channel 1.
                 */
-               channel = hdr->type - TRC_CH_B1;
+               pseudo_header->isdn.channel = 1;
+               break;
 
-               if (wth->capture.i4btrace->bchannel_prot[channel] == 1) {
-                       /*
-                        * V.120.
-                        */
-                       pseudo_header->x25.flags =
-                           (hdr->dir == FROM_TE) ? 0x00 : 0x80;
-               }
+       case TRC_CH_B2:
+               /*
+                * B channel 2.
+                */
+               pseudo_header->isdn.channel = 2;
                break;
        }
 }