Move the new files to the same places as in automake.
[obnox/wireshark/wip.git] / wiretap / libpcap.c
index 117409c25a0c90afebf778884661570c43d606a8..22ace095aece11fe9877e1c2cfb5d8e9e2a6faa2 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,10 +657,12 @@ 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;
 
+       wth->phdr.presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
+
        /* Update the Timestamp, if not already done */
        if (wth->file_encap != WTAP_ENCAP_ERF) {
          wth->phdr.ts.secs = hdr.hdr.ts_sec;
@@ -673,39 +675,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 +701,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 +744,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 +764,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 +781,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 +832,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,11 +865,10 @@ 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;
-       size_t nwritten;
 
        /* This is a libpcap file */
        wdh->subtype_write = libpcap_dump;
@@ -945,14 +902,8 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
                return FALSE;
        }
 
-       nwritten = wtap_dump_file_write(wdh, &magic, sizeof magic);
-       if (nwritten != sizeof magic) {
-               if (nwritten == 0 && wtap_dump_file_ferror(wdh))
-                       *err = wtap_dump_file_ferror(wdh);
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, &magic, sizeof magic, err))
                return FALSE;
-       }
        wdh->bytes_dumped += sizeof magic;
 
        /* current "libpcap" format is 2.4 */
@@ -974,14 +925,8 @@ gboolean libpcap_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
        file_hdr.snaplen = (wdh->snaplen != 0) ? wdh->snaplen :
                                                 WTAP_MAX_PACKET_SIZE;
        file_hdr.network = wtap_wtap_encap_to_pcap_encap(wdh->encap);
-       nwritten = wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr);
-       if (nwritten != sizeof file_hdr) {
-               if (nwritten == 0 && wtap_dump_file_ferror(wdh))
-                       *err = wtap_dump_file_ferror(wdh);
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
                return FALSE;
-       }
        wdh->bytes_dumped += sizeof file_hdr;
 
        return TRUE;
@@ -992,11 +937,10 @@ 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;
-       size_t nwritten;
        int phdrsize;
 
        phdrsize = pcap_get_phdr_size(wdh->encap, pseudo_header);
@@ -1009,6 +953,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:
@@ -1069,27 +1019,15 @@ static gboolean libpcap_dump(wtap_dumper *wdh,
                return FALSE;
        }
 
-       nwritten = wtap_dump_file_write(wdh, &rec_hdr, hdr_size);
-       if (nwritten != hdr_size) {
-               if (nwritten == 0 && wtap_dump_file_ferror(wdh))
-                       *err = wtap_dump_file_ferror(wdh);
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, &rec_hdr, hdr_size, err))
                return FALSE;
-       }
        wdh->bytes_dumped += hdr_size;
 
        if (!pcap_write_phdr(wdh, wdh->encap, pseudo_header, err))
                return FALSE;
 
-       nwritten = wtap_dump_file_write(wdh, pd, phdr->caplen);
-       if (nwritten != phdr->caplen) {
-               if (nwritten == 0 && wtap_dump_file_ferror(wdh))
-                       *err = wtap_dump_file_ferror(wdh);
-               else
-                       *err = WTAP_ERR_SHORT_WRITE;
+       if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
                return FALSE;
-       }
         wdh->bytes_dumped += phdr->caplen;
        return TRUE;
 }