Handle the new LINKTYPE_CAN_SOCKETCAN_HOSTENDIAN.
[metze/wireshark/wip.git] / wiretap / iptrace.c
index 65ad4cd4632859f3e71c884796c03646d9137b9d..235b317251c9a7a26dc69fd4a19dedfc0e240d0b 100644 (file)
@@ -1,6 +1,4 @@
 /* iptrace.c
- *
- * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
@@ -26,7 +24,6 @@
 #include <string.h>
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
 #include "atm.h"
 #include "iptrace.h"
 
 static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean iptrace_seek_read_1_0(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
-    int *err, gchar **err_info);
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
 
 static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
-    int *err, gchar **err_info);
-
-static int iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len,
-    int *err, gchar **err_info);
-static gboolean iptrace_read_rec_data(FILE_T fh, guint8 *data_ptr,
-    int packet_size, int *err, gchar **err_info);
-static gboolean iptrace_process_rec_data(FILE_T fh, guint8 *data_ptr,
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
+
+static gboolean iptrace_read_rec_data(FILE_T fh, Buffer *buf,
     struct wtap_pkthdr *phdr, int *err, gchar **err_info);
 static void fill_in_pseudo_header(int encap,
     union wtap_pseudo_header *pseudo_header, guint8 *header);
 static int wtap_encap_ift(unsigned int  ift);
 
-int iptrace_open(wtap *wth, int *err, gchar **err_info)
+#define NAME_SIZE 11
+
+wtap_open_return_val iptrace_open(wtap *wth, int *err, gchar **err_info)
 {
-       int bytes_read;
-       char name[12];
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(name, 11, wth->fh);
-       if (bytes_read != 11) {
-               *err = file_error(wth->fh, err_info);
-               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+       char name[NAME_SIZE+1];
+
+       if (!wtap_read_bytes(wth->fh, name, NAME_SIZE, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
-       name[11] = '\0';
+       name[NAME_SIZE] = '\0';
 
        if (strcmp(name, "iptrace 1.0") == 0) {
-               wth->file_type = WTAP_FILE_IPTRACE_1_0;
+               wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_IPTRACE_1_0;
                wth->subtype_read = iptrace_read_1_0;
                wth->subtype_seek_read = iptrace_seek_read_1_0;
-               wth->tsprecision = WTAP_FILE_TSPREC_SEC;
+               wth->file_tsprec = WTAP_TSPREC_SEC;
        }
        else if (strcmp(name, "iptrace 2.0") == 0) {
-               wth->file_type = WTAP_FILE_IPTRACE_2_0;
+               wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_IPTRACE_2_0;
                wth->subtype_read = iptrace_read_2_0;
                wth->subtype_seek_read = iptrace_seek_read_2_0;
-               wth->tsprecision = WTAP_FILE_TSPREC_NSEC;
+               wth->file_tsprec = WTAP_TSPREC_NSEC;
        }
        else {
-               return 0;
+               return WTAP_OPEN_NOT_MINE;
        }
 
-       return 1;
+       return WTAP_OPEN_MINE;
 }
 
 /***********************************************************
@@ -125,18 +114,14 @@ typedef struct {
 #define IPTRACE_1_0_PDATA_SIZE 22      /* packet data */
 
 static gboolean
-iptrace_process_rec_header_1_0(FILE_T fh, struct wtap_pkthdr *phdr,
+iptrace_read_rec_1_0(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
     int *err, gchar **err_info)
 {
        guint8                  header[IPTRACE_1_0_PHDR_SIZE];
-       int                     ret;
        iptrace_1_0_phdr        pkt_hdr;
        guint32                 packet_size;
-       guint8                  fddi_padding[3];
 
-       ret = iptrace_read_rec_header(fh, header, IPTRACE_1_0_PHDR_SIZE,
-           err, err_info);
-       if (ret <= 0) {
+       if (!wtap_read_bytes_or_eof(fh, header, sizeof header, err, err_info)) {
                /* Read error or EOF */
                return FALSE;
        }
@@ -149,14 +134,14 @@ iptrace_process_rec_header_1_0(FILE_T fh, struct wtap_pkthdr *phdr,
        pkt_hdr.if_type = header[28];
        phdr->pkt_encap = wtap_encap_ift(pkt_hdr.if_type);
        if (phdr->pkt_encap == WTAP_ENCAP_UNKNOWN) {
-               *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+               *err = WTAP_ERR_UNSUPPORTED;
                *err_info = g_strdup_printf("iptrace: interface type IFT=0x%02x unknown or unsupported",
                    pkt_hdr.if_type);
                return FALSE;
        }
 
        /* Read the packet metadata */
-       packet_size = pntohl(&header[0]);
+       packet_size = pntoh32(&header[0]);
        if (packet_size < IPTRACE_1_0_PDATA_SIZE) {
                /*
                 * Uh-oh, the record isn't big enough to even have a
@@ -191,10 +176,10 @@ iptrace_process_rec_header_1_0(FILE_T fh, struct wtap_pkthdr *phdr,
                packet_size -= 3;
 
                /*
-                * Read the padding.
+                * Skip the padding.
                 */
-               if (!iptrace_read_rec_data(fh, fddi_padding, 3, err, err_info))
-                       return FALSE;   /* Read error */
+               if (!file_skip(fh, 3, err))
+                       return FALSE;
        }
        if (packet_size > WTAP_MAX_PACKET_SIZE) {
                /*
@@ -207,38 +192,33 @@ iptrace_process_rec_header_1_0(FILE_T fh, struct wtap_pkthdr *phdr,
                return FALSE;
        }
 
+       phdr->rec_type = REC_TYPE_PACKET;
        phdr->presence_flags = WTAP_HAS_TS;
        phdr->len = packet_size;
        phdr->caplen = packet_size;
-       phdr->ts.secs = pntohl(&header[4]);
+       phdr->ts.secs = pntoh32(&header[4]);
        phdr->ts.nsecs = 0;
 
        /* Fill in the pseudo-header. */
        fill_in_pseudo_header(phdr->pkt_encap, &phdr->pseudo_header, header);
 
-       return TRUE;
+       /* Get the packet data */
+       return iptrace_read_rec_data(fh, buf, phdr, err, err_info);
 }
 
 /* Read the next packet */
 static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
-       guint8                  *data_ptr;
-
-       /* Read the descriptor data */
        *data_offset = file_tell(wth->fh);
-       if (!iptrace_process_rec_header_1_0(wth->fh, &wth->phdr, err, err_info)) {
+
+       /* Read the packet */
+       if (!iptrace_read_rec_1_0(wth->fh, &wth->phdr, wth->frame_buffer,
+           err, err_info)) {
                /* Read error or EOF */
                return FALSE;
        }
 
-       /* Read the packet data */
-       buffer_assure_space( wth->frame_buffer, wth->phdr.caplen );
-       data_ptr = buffer_start_ptr( wth->frame_buffer );
-       if (!iptrace_process_rec_data(wth->fh, data_ptr, &wth->phdr,
-           err, err_info))
-               return FALSE;   /* Read error */
-
        /* If the per-file encapsulation isn't known, set it to this
           packet's encapsulation.
 
@@ -256,26 +236,18 @@ static gboolean iptrace_read_1_0(wtap *wth, int *err, gchar **err_info,
 }
 
 static gboolean iptrace_seek_read_1_0(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
-    int *err, gchar **err_info)
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
 {
        if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
                return FALSE;
 
-       /* Read the descriptor data */
-       if (!iptrace_process_rec_header_1_0(wth->random_fh, phdr, err, err_info))
-               return FALSE;
-
-       if (packet_size != (int)phdr->caplen) {
-               *err = WTAP_ERR_BAD_FILE;
-               *err_info = g_strdup_printf("iptrace: record length %u doesn't match requested length %d",
-                   phdr->caplen, packet_size);
+       /* Read the packet */
+       if (!iptrace_read_rec_1_0(wth->random_fh, phdr, buf, err, err_info)) {
+               if (*err == 0)
+                       *err = WTAP_ERR_SHORT_READ;
                return FALSE;
        }
-
-       /* Get the packet data */
-       return iptrace_process_rec_data(wth->random_fh, pd, phdr,
-           err, err_info);
+       return TRUE;
 }
 
 /***********************************************************
@@ -317,18 +289,14 @@ typedef struct {
 #define IPTRACE_2_0_PDATA_SIZE 32      /* packet data */
 
 static gboolean
-iptrace_process_rec_header_2_0(FILE_T fh, struct wtap_pkthdr *phdr,
+iptrace_read_rec_2_0(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
     int *err, gchar **err_info)
 {
        guint8                  header[IPTRACE_2_0_PHDR_SIZE];
-       int                     ret;
        iptrace_2_0_phdr        pkt_hdr;
        guint32                 packet_size;
-       guint8                  fddi_padding[3];
 
-       ret = iptrace_read_rec_header(fh, header, IPTRACE_2_0_PHDR_SIZE,
-           err, err_info);
-       if (ret <= 0) {
+       if (!wtap_read_bytes_or_eof(fh, header, sizeof header, err, err_info)) {
                /* Read error or EOF */
                return FALSE;
        }
@@ -358,7 +326,7 @@ iptrace_process_rec_header_2_0(FILE_T fh, struct wtap_pkthdr *phdr,
         * that we don't handle?
         */
        if (phdr->pkt_encap == WTAP_ENCAP_UNKNOWN) {
-               *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+               *err = WTAP_ERR_UNSUPPORTED;
                *err_info = g_strdup_printf("iptrace: interface type IFT=0x%02x unknown or unsupported",
                    pkt_hdr.if_type);
                return FALSE;
@@ -366,7 +334,7 @@ iptrace_process_rec_header_2_0(FILE_T fh, struct wtap_pkthdr *phdr,
 #endif
 
        /* Read the packet metadata */
-       packet_size = pntohl(&header[0]);
+       packet_size = pntoh32(&header[0]);
        if (packet_size < IPTRACE_2_0_PDATA_SIZE) {
                /*
                 * Uh-oh, the record isn't big enough to even have a
@@ -401,10 +369,10 @@ iptrace_process_rec_header_2_0(FILE_T fh, struct wtap_pkthdr *phdr,
                packet_size -= 3;
 
                /*
-                * Read the padding.
+                * Skip the padding.
                 */
-               if (!iptrace_read_rec_data(fh, fddi_padding, 3, err, err_info))
-                       return FALSE;   /* Read error */
+               if (!file_skip(fh, 3, err))
+                       return FALSE;
        }
        if (packet_size > WTAP_MAX_PACKET_SIZE) {
                /*
@@ -417,27 +385,29 @@ iptrace_process_rec_header_2_0(FILE_T fh, struct wtap_pkthdr *phdr,
                return FALSE;
        }
 
+       phdr->rec_type = REC_TYPE_PACKET;
        phdr->presence_flags = WTAP_HAS_TS;
        phdr->len = packet_size;
        phdr->caplen = packet_size;
-       phdr->ts.secs = pntohl(&header[32]);
-       phdr->ts.nsecs = pntohl(&header[36]);
+       phdr->ts.secs = pntoh32(&header[32]);
+       phdr->ts.nsecs = pntoh32(&header[36]);
 
        /* Fill in the pseudo_header. */
        fill_in_pseudo_header(phdr->pkt_encap, &phdr->pseudo_header, header);
 
-       return TRUE;
+       /* Get the packet data */
+       return iptrace_read_rec_data(fh, buf, phdr, err, err_info);
 }
 
 /* Read the next packet */
 static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
-       guint8                  *data_ptr;
-
-       /* Read the descriptor data */
        *data_offset = file_tell(wth->fh);
-       if (!iptrace_process_rec_header_2_0(wth->fh, &wth->phdr, err, err_info)) {
+
+       /* Read the packet */
+       if (!iptrace_read_rec_2_0(wth->fh, &wth->phdr, wth->frame_buffer,
+           err, err_info)) {
                /* Read error or EOF */
                return FALSE;
        }
@@ -455,84 +425,17 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
                        wth->file_encap = WTAP_ENCAP_PER_PACKET;
        }
 
-       /* Read the packet data */
-       buffer_assure_space( wth->frame_buffer, wth->phdr.caplen );
-       data_ptr = buffer_start_ptr( wth->frame_buffer );
-       if (!iptrace_process_rec_data(wth->fh, data_ptr, &wth->phdr,
-           err, err_info))
-               return FALSE;   /* Read error */
-
-       /* If the per-file encapsulation isn't known, set it to this
-          packet's encapsulation.
-
-          If it *is* known, and it isn't this packet's encapsulation,
-          set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
-          have a single encapsulation for all packets in the file. */
-       if (wth->file_encap == WTAP_ENCAP_UNKNOWN)
-               wth->file_encap = wth->phdr.pkt_encap;
-       else {
-               if (wth->file_encap != wth->phdr.pkt_encap)
-                       wth->file_encap = WTAP_ENCAP_PER_PACKET;
-       }
-
        return TRUE;
 }
 
 static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
-    struct wtap_pkthdr *phdr, guint8 *pd, int packet_size,
-    int *err, gchar **err_info)
+    struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
 {
        if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
                return FALSE;
 
-       /* Read the descriptor data */
-       if (!iptrace_process_rec_header_2_0(wth->random_fh, phdr, err, err_info))
-               return FALSE;
-
-       if (packet_size != (int)phdr->caplen) {
-               *err = WTAP_ERR_BAD_FILE;
-               *err_info = g_strdup_printf("iptrace: record length %u doesn't match requested length %d",
-                   phdr->caplen, packet_size);
-               return FALSE;
-       }
-
-       /* Get the packet data */
-       return iptrace_process_rec_data(wth->random_fh, pd, phdr, err,
-           err_info);
-}
-
-static int
-iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len, int *err,
-    gchar **err_info)
-{
-       int     bytes_read;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(header, header_len, fh);
-       if (bytes_read != header_len) {
-               *err = file_error(fh, err_info);
-               if (*err != 0)
-                       return -1;
-               if (bytes_read != 0) {
-                       *err = WTAP_ERR_SHORT_READ;
-                       return -1;
-               }
-               return 0;
-       }
-       return 1;
-}
-
-static gboolean
-iptrace_read_rec_data(FILE_T fh, guint8 *data_ptr, int packet_size, int *err,
-    gchar **err_info)
-{
-       int     bytes_read;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read( data_ptr, packet_size, fh );
-
-       if (bytes_read != packet_size) {
-               *err = file_error(fh, err_info);
+       /* Read the packet */
+       if (!iptrace_read_rec_2_0(wth->random_fh, phdr, buf, err, err_info)) {
                if (*err == 0)
                        *err = WTAP_ERR_SHORT_READ;
                return FALSE;
@@ -541,10 +444,10 @@ iptrace_read_rec_data(FILE_T fh, guint8 *data_ptr, int packet_size, int *err,
 }
 
 static gboolean
-iptrace_process_rec_data(FILE_T fh, guint8 *data_ptr, struct wtap_pkthdr *phdr,
+iptrace_read_rec_data(FILE_T fh, Buffer *buf, struct wtap_pkthdr *phdr,
     int *err, gchar **err_info)
 {
-       if (!iptrace_read_rec_data(fh, data_ptr, phdr->caplen, err, err_info))
+       if (!wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info))
                return FALSE;
 
        if (phdr->pkt_encap == WTAP_ENCAP_ATM_PDUS) {
@@ -552,8 +455,7 @@ iptrace_process_rec_data(FILE_T fh, guint8 *data_ptr, struct wtap_pkthdr *phdr,
                 * Attempt to guess from the packet data, the VPI,
                 * and the VCI information about the type of traffic.
                 */
-               atm_guess_traffic_type(data_ptr, phdr->caplen,
-                   &phdr->pseudo_header);
+               atm_guess_traffic_type(phdr, ws_buffer_start_ptr(buf));
        }
 
        return TRUE;
@@ -701,3 +603,16 @@ wtap_encap_ift(unsigned int  ift)
                }
        }
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */