Refactor 802.11 radio flags.
[metze/wireshark/wip.git] / wiretap / packetlogger.c
index 5c7e19c82e8db8875299b48404e69cb3292a24f3..bba8c4cbccdac0c3c3f7261d9d8b957ed1d87af5 100644 (file)
@@ -2,8 +2,6 @@
  * Routines for opening Apple's (Bluetooth) PacketLogger file format captures
  * Copyright 2008-2009, Stephen Fisher (see AUTHORS file)
  *
- * $Id$
- *
  * Wireshark - Network traffic analyzer
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  * USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
-#include <glib.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
 
-#include "wtap.h"
 #include "wtap-int.h"
-#include "buffer.h"
 #include "file_wrappers.h"
 #include "packetlogger.h"
 
+typedef struct {
+       gboolean little_endian;
+} packetlogger_t;
+
 typedef struct packetlogger_header {
        guint32 len;
-       guint64 ts;
+       guint32 ts_secs;
+       guint32 ts_usecs;
 } packetlogger_header_t;
 
-#define PACKETLOGGER_HEADER_SIZE 12
-
-static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info _U_,
+static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info,
                                  gint64 *data_offset);
 static gboolean packetlogger_seek_read(wtap *wth, gint64 seek_off,
-                                      union wtap_pseudo_header *pseudo_header _U_,
-                                      guchar *pd, int length, int *err,
-                                      gchar **err_info _U_);
+                                      struct wtap_pkthdr *phdr,
+                                      Buffer *buf, int *err, gchar **err_info);
 static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
-                                        FILE_T fh, int *err);
-
+                                        FILE_T fh, gboolean little_endian,
+                                        int *err, gchar **err_info);
+static gboolean packetlogger_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
+                                        Buffer *buf, int *err,
+                                        gchar **err_info);
 
-int packetlogger_open(wtap *wth, int *err, gchar **err_info _U_)
+wtap_open_return_val packetlogger_open(wtap *wth, int *err, gchar **err_info)
 {
+       gboolean little_endian = FALSE;
        packetlogger_header_t pl_hdr;
        guint8 type;
+       packetlogger_t *packetlogger;
 
-       if(!packetlogger_read_header(&pl_hdr, wth->fh, err))
-               return -1;
+       if(!packetlogger_read_header(&pl_hdr, wth->fh, little_endian,
+           err, err_info)) {
+               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
+       }
+
+       if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
+       }
 
-       if (file_read(&type, 1, 1, wth->fh) <= 0)
-               return -1;
+       /*
+        * If the upper 16 bits of the length are non-zero and the lower
+        * 16 bits are zero, assume the file is little-endian.
+        */
+       if ((pl_hdr.len & 0x0000FFFF) == 0 &&
+           (pl_hdr.len & 0xFFFF0000) != 0) {
+               /*
+                * Byte-swap the upper 16 bits (the lower 16 bits are
+                * zero, so we don't have to look at them).
+                */
+               pl_hdr.len = ((pl_hdr.len >> 24) & 0xFF) |
+                            (((pl_hdr.len >> 16) & 0xFF) << 8);
+               little_endian = TRUE;
+       }
 
        /* Verify this file belongs to us */
        if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
              (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
-               return 0;
+               return WTAP_OPEN_NOT_MINE;
 
        /* No file header. Reset the fh to 0 so we can read the first packet */
        if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
-               return -1;
+               return WTAP_OPEN_ERROR;
+
+       /* This is a PacketLogger file */
+       packetlogger = (packetlogger_t *)g_malloc(sizeof(packetlogger_t));
+       packetlogger->little_endian = little_endian;
+       wth->priv = (void *)packetlogger;
 
        /* Set up the pointers to the handlers for this file type */
        wth->subtype_read = packetlogger_read;
        wth->subtype_seek_read = packetlogger_seek_read;
 
-       wth->data_offset = 0;
-       wth->file_type = WTAP_FILE_PACKETLOGGER;
+       wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PACKETLOGGER;
        wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
-       wth->tsprecision = WTAP_FILE_TSPREC_USEC;
+       wth->file_tsprec = WTAP_TSPREC_USEC;
 
-       return 1; /* Our kind of file */
+       return WTAP_OPEN_MINE; /* Our kind of file */
 }
 
 static gboolean
 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
 {
-       packetlogger_header_t pl_hdr;
-       guint bytes_read;
+       *data_offset = file_tell(wth->fh);
 
-       *data_offset = wth->data_offset;
+       return packetlogger_read_packet(wth, wth->fh, &wth->phdr,
+           wth->frame_buffer, err, err_info);
+}
 
-       if(!packetlogger_read_header(&pl_hdr, wth->fh, err))
+static gboolean
+packetlogger_seek_read(wtap *wth, gint64 seek_off, 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;
 
-       if (pl_hdr.len < 8) {
-               *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
-               *err = WTAP_ERR_BAD_RECORD;
-               return FALSE;
-       }
-       
-       buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8);
-       bytes_read = file_read(buffer_start_ptr(wth->frame_buffer), 1,
-                              pl_hdr.len - 8,
-                              wth->fh);
-       if(bytes_read != pl_hdr.len - 8) {
-               *err = file_error(wth->fh);
+       if(!packetlogger_read_packet(wth, wth->random_fh, phdr, buf, err, err_info)) {
                if(*err == 0)
                        *err = WTAP_ERR_SHORT_READ;
 
                return FALSE;
        }
-
-       wth->data_offset += (pl_hdr.len + 4);
-
-       wth->phdr.len = pl_hdr.len - 8;
-       wth->phdr.caplen = pl_hdr.len - 8;
-
-       wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
-       wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
-
        return TRUE;
 }
 
 static gboolean
-packetlogger_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
-                      *pseudo_header _U_, guchar *pd, int length, int *err,
-                      gchar **err_info _U_)
+packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh,
+                        gboolean little_endian, int *err, gchar **err_info)
 {
-       packetlogger_header_t pl_hdr;
-       guint bytes_read;
-
-       if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
+       if (!wtap_read_bytes_or_eof(fh, &pl_hdr->len, 4, err, err_info))
                return FALSE;
-
-       if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err)) {
-               if(*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-
+       if (!wtap_read_bytes(fh, &pl_hdr->ts_secs, 4, err, err_info))
                return FALSE;
-       }
-
-       if(length != (int)pl_hdr.len - 8) {
-               *err = WTAP_ERR_BAD_RECORD;
-               *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length);
+       if (!wtap_read_bytes(fh, &pl_hdr->ts_usecs, 4, err, err_info))
                return FALSE;
-       }
 
-       bytes_read = file_read(pd, 1, pl_hdr.len - 8, wth->random_fh);
-       if(bytes_read != (pl_hdr.len - 8)) {
-               *err = file_error(wth->random_fh);
-               if(*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-
-               return FALSE;
+       /* Convert multi-byte values to host endian */
+       if (little_endian) {
+               pl_hdr->len = GUINT32_FROM_LE(pl_hdr->len);
+               pl_hdr->ts_secs = GUINT32_FROM_LE(pl_hdr->ts_secs);
+               pl_hdr->ts_usecs = GUINT32_FROM_LE(pl_hdr->ts_usecs);
+       } else {
+               pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
+               pl_hdr->ts_secs = GUINT32_FROM_BE(pl_hdr->ts_secs);
+               pl_hdr->ts_usecs = GUINT32_FROM_BE(pl_hdr->ts_usecs);
        }
 
        return TRUE;
 }
 
 static gboolean
-packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err)
+packetlogger_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
+                        int *err, gchar **err_info)
 {
-       guint bytes_read = 0;
-
-       bytes_read += file_read(&pl_hdr->len, 4, 1, fh);
-       bytes_read += file_read(&pl_hdr->ts, 8, 1, fh);
-
-       /* Convert multi-byte values from big endian to host endian */
-       pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
-       pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
+       packetlogger_t *packetlogger = (packetlogger_t *)wth->priv;
+       packetlogger_header_t pl_hdr;
 
-       if(bytes_read < PACKETLOGGER_HEADER_SIZE) {
-               *err = file_error(fh);
-               if(*err == 0 && bytes_read > 0)
-                       *err = WTAP_ERR_SHORT_READ;
+       if(!packetlogger_read_header(&pl_hdr, fh, packetlogger->little_endian,
+           err, err_info))
+               return FALSE;
 
+       if (pl_hdr.len < 8) {
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
+               return FALSE;
+       }
+       if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
+               /*
+                * Probably a corrupt capture file; don't blow up trying
+                * to allocate space for an immensely-large packet.
+                */
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
+                   pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
                return FALSE;
        }
 
-       return TRUE;
+       phdr->rec_type = REC_TYPE_PACKET;
+       phdr->pkt_encap = wth->file_encap;
+       phdr->presence_flags = WTAP_HAS_TS;
+
+       phdr->len = pl_hdr.len - 8;
+       phdr->caplen = pl_hdr.len - 8;
+
+       phdr->ts.secs = (time_t)pl_hdr.ts_secs;
+       phdr->ts.nsecs = (int)(pl_hdr.ts_usecs * 1000);
+
+       return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
 }
+
+/*
+ * 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:
+ */