Rename WTAP_ERR_BAD_RECORD to WTAP_ERR_BAD_FILE; it really reports any
[metze/wireshark/wip.git] / wiretap / libpcap.c
index 484a79d5e702783a4691440169e4f617f0110fd4..c5d1b31738bf0749fa45a58775eb281c16d1b714 100644 (file)
@@ -30,7 +30,6 @@
 #include "wtap-int.h"
 #include "file_wrappers.h"
 #include "buffer.h"
-#include "atm.h"
 #include "pcap-common.h"
 #include "pcap-encap.h"
 #include "libpcap.h"
@@ -70,15 +69,15 @@ static libpcap_try_t libpcap_try(wtap *wth, int *err);
 static gboolean libpcap_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset);
 static gboolean libpcap_seek_read(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
+    union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
     int *err, gchar **err_info);
 static int libpcap_read_header(wtap *wth, int *err, gchar **err_info,
     struct pcaprec_ss990915_hdr *hdr);
 static void adjust_header(wtap *wth, struct pcaprec_hdr *hdr);
-static gboolean libpcap_read_rec_data(FILE_T fh, guchar *pd, int length,
-    int *err);
+static gboolean libpcap_read_rec_data(FILE_T fh, guint8 *pd, int length,
+    int *err, gchar **err_info);
 static gboolean libpcap_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
+    const union wtap_pseudo_header *pseudo_header, const guint8 *pd, int *err);
 
 int libpcap_open(wtap *wth, int *err, gchar **err_info)
 {
@@ -93,9 +92,9 @@ int libpcap_open(wtap *wth, int *err, gchar **err_info)
 
        /* Read in the number that should be at the start of a "libpcap" file */
        errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&magic, 1, sizeof magic, wth->fh);
+       bytes_read = file_read(&magic, sizeof magic, wth->fh);
        if (bytes_read != sizeof magic) {
-               *err = file_error(wth->fh);
+               *err = file_error(wth->fh, err_info);
                if (*err != 0)
                        return -1;
                return 0;
@@ -164,9 +163,9 @@ int libpcap_open(wtap *wth, int *err, gchar **err_info)
 
        /* Read the rest of the header. */
        errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, 1, sizeof hdr, wth->fh);
+       bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
        if (bytes_read != sizeof hdr) {
-               *err = file_error(wth->fh);
+               *err = file_error(wth->fh, err_info);
                if (*err != 0)
                        return -1;
                return 0;
@@ -529,7 +528,7 @@ static libpcap_try_t libpcap_try(wtap *wth, int *err)
                        return THIS_FORMAT;
                }
 
-               if (*err == WTAP_ERR_BAD_RECORD) {
+               if (*err == WTAP_ERR_BAD_FILE) {
                        /*
                         * The first record is bogus, so this is probably
                         * a corrupt file.  Assume the file is in this
@@ -568,7 +567,7 @@ static libpcap_try_t libpcap_try(wtap *wth, int *err)
                        return THIS_FORMAT;
                }
 
-               if (*err == WTAP_ERR_BAD_RECORD) {
+               if (*err == WTAP_ERR_BAD_FILE) {
                        /*
                         * The second record is bogus; maybe it's a
                         * Capture File From Hell, and what looks like
@@ -601,7 +600,7 @@ static gboolean libpcap_read(wtap *wth, int *err, gchar **err_info,
        guint packet_size;
        guint orig_size;
        int bytes_read;
-       guchar fddi_padding[3];
+       guint8 fddi_padding[3];
        int phdr_len;
        libpcap_t *libpcap;
 
@@ -635,7 +634,8 @@ static gboolean libpcap_read(wtap *wth, int *err, gchar **err_info,
                /*
                 * Read the padding.
                 */
-               if (!libpcap_read_rec_data(wth->fh, fddi_padding, 3, err))
+               if (!libpcap_read_rec_data(wth->fh, fddi_padding, 3, err,
+                   err_info))
                        return FALSE;   /* Read error */
        }
 
@@ -657,7 +657,7 @@ static gboolean libpcap_read(wtap *wth, int *err, gchar **err_info,
 
        buffer_assure_space(wth->frame_buffer, packet_size);
        if (!libpcap_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
-           packet_size, err))
+           packet_size, err, err_info))
                return FALSE;   /* Read error */
        wth->data_offset += packet_size;
 
@@ -673,39 +673,15 @@ static gboolean libpcap_read(wtap *wth, int *err, gchar **err_info,
        wth->phdr.caplen = packet_size;
        wth->phdr.len = orig_size;
 
-       if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
-               if (wth->file_type == WTAP_FILE_PCAP_NOKIA) {
-                       /*
-                        * Nokia IPSO ATM.
-                        *
-                        * Guess the traffic type based on the packet
-                        * contents.
-                        */
-                       atm_guess_traffic_type(buffer_start_ptr(wth->frame_buffer),
-                           wth->phdr.caplen, &wth->pseudo_header);
-               } else {
-                       /*
-                        * SunATM.
-                        *
-                        * If this is ATM LANE traffic, try to guess what
-                        * type of LANE traffic it is based on the packet
-                        * contents.
-                        */
-                       if (wth->pseudo_header.atm.type == TRAF_LANE) {
-                               atm_guess_lane_type(buffer_start_ptr(wth->frame_buffer),
-                                   wth->phdr.caplen, &wth->pseudo_header);
-                       }
-               }
-       }
-
-       pcap_read_post_process(wth->file_encap, wth->phdr.caplen,
-           libpcap->byte_swapped, buffer_start_ptr(wth->frame_buffer));
+       pcap_read_post_process(wth->file_type, wth->file_encap,
+           &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer),
+           wth->phdr.caplen, libpcap->byte_swapped, -1);
        return TRUE;
 }
 
 static gboolean
 libpcap_seek_read(wtap *wth, gint64 seek_off,
-    union wtap_pseudo_header *pseudo_header, guchar *pd, int length,
+    union wtap_pseudo_header *pseudo_header, guint8 *pd, int length,
     int *err, gchar **err_info)
 {
        int phdr_len;
@@ -723,32 +699,11 @@ libpcap_seek_read(wtap *wth, gint64 seek_off,
        /*
         * Read the packet data.
         */
-       if (!libpcap_read_rec_data(wth->random_fh, pd, length, err))
+       if (!libpcap_read_rec_data(wth->random_fh, pd, length, err, err_info))
                return FALSE;   /* failed */
 
-       if (wth->file_encap == WTAP_ENCAP_ATM_PDUS) {
-               if (wth->file_type == WTAP_FILE_PCAP_NOKIA) {
-                       /*
-                        * Nokia IPSO ATM.
-                        *
-                        * Guess the traffic type based on the packet
-                        * contents.
-                        */
-                       atm_guess_traffic_type(pd, length, pseudo_header);
-               } else {
-                       /*
-                        * SunATM.
-                        *
-                        * If this is ATM LANE traffic, try to guess what
-                        * type of LANE traffic it is based on the packet
-                        * contents.
-                        */
-                       if (pseudo_header->atm.type == TRAF_LANE)
-                               atm_guess_lane_type(pd, length, pseudo_header);
-               }
-       }
-       pcap_read_post_process(wth->file_encap, length,
-           libpcap->byte_swapped, pd);
+       pcap_read_post_process(wth->file_type, wth->file_encap,
+           pseudo_header, pd, length, libpcap->byte_swapped, -1);
        return TRUE;
 }
 
@@ -787,9 +742,9 @@ static int libpcap_read_header(wtap *wth, int *err, gchar **err_info,
                g_assert_not_reached();
                bytes_to_read = 0;
        }
-       bytes_read = file_read(hdr, 1, bytes_to_read, wth->fh);
+       bytes_read = file_read(hdr, bytes_to_read, wth->fh);
        if (bytes_read != bytes_to_read) {
-               *err = file_error(wth->fh);
+               *err = file_error(wth->fh, err_info);
                if (*err == 0 && bytes_read != 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
@@ -807,7 +762,7 @@ static int libpcap_read_header(wtap *wth, int *err, gchar **err_info,
                 * this is can tell when it's not the type we're guessing
                 * it is.
                 */
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                if (err_info != NULL) {
                        *err_info = g_strdup_printf("pcap: File has %u-byte packet, bigger than maximum of %u",
                            hdr->hdr.incl_len, WTAP_MAX_PACKET_SIZE);
@@ -824,7 +779,7 @@ static int libpcap_read_header(wtap *wth, int *err, gchar **err_info,
                 * this is can tell when it's not the type we're guessing
                 * it is.
                 */
-               *err = WTAP_ERR_BAD_RECORD;
+               *err = WTAP_ERR_BAD_FILE;
                if (err_info != NULL) {
                        *err_info = g_strdup_printf("pcap: File has %u-byte packet, bigger than maximum of %u",
                            hdr->hdr.orig_len, WTAP_MAX_PACKET_SIZE);
@@ -875,15 +830,16 @@ adjust_header(wtap *wth, struct pcaprec_hdr *hdr)
 }
 
 static gboolean
-libpcap_read_rec_data(FILE_T fh, guchar *pd, int length, int *err)
+libpcap_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
+    gchar **err_info)
 {
        int     bytes_read;
 
        errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(pd, 1, length, fh);
+       bytes_read = file_read(pd, length, fh);
 
        if (bytes_read != length) {
-               *err = file_error(fh);
+               *err = file_error(fh, err_info);
                if (*err == 0)
                        *err = WTAP_ERR_SHORT_READ;
                return FALSE;
@@ -907,7 +863,7 @@ int libpcap_dump_can_write_encap(int encap)
 
 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
    failure */
-gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
+gboolean libpcap_dump_open(wtap_dumper *wdh, int *err)
 {
        guint32 magic;
        struct pcap_hdr file_hdr;
@@ -979,7 +935,7 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
 static gboolean libpcap_dump(wtap_dumper *wdh,
        const struct wtap_pkthdr *phdr,
        const union wtap_pseudo_header *pseudo_header,
-       const guchar *pd, int *err)
+       const guint8 *pd, int *err)
 {
        struct pcaprec_ss990915_hdr rec_hdr;
        size_t hdr_size;
@@ -995,6 +951,12 @@ static gboolean libpcap_dump(wtap_dumper *wdh,
        }
        rec_hdr.hdr.incl_len = phdr->caplen + phdrsize;
        rec_hdr.hdr.orig_len = phdr->len + phdrsize;
+
+       if (rec_hdr.hdr.incl_len > WTAP_MAX_PACKET_SIZE || rec_hdr.hdr.orig_len > WTAP_MAX_PACKET_SIZE) {
+               *err = WTAP_ERR_BAD_FILE;
+               return FALSE;
+       }
+
        switch (wdh->file_type) {
 
        case WTAP_FILE_PCAP: