* USA.
*/
-#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
#include <glib.h>
#include <stdio.h>
guint64 ts;
} 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 length, int *err,
+ gchar **err_info);
static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
- FILE_T fh, int *err);
-
+ FILE_T fh, int *err, gchar **err_info);
+static gboolean packetlogger_read_packet(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_)
+int packetlogger_open(wtap *wth, int *err, gchar **err_info)
{
packetlogger_header_t pl_hdr;
guint8 type;
- if(!packetlogger_read_header(&pl_hdr, wth->fh, err))
- return -1;
- file_read(&type, 1, 1, wth->fh);
+ if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info)) {
+ if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ return -1;
+ return 0;
+ }
+
+ if (file_read(&type, 1, wth->fh) <= 0) {
+ *err = file_error(wth->fh, err_info);
+ if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ return -1;
+ return 0;
+ }
/* Verify this file belongs to us */
if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
- (type < 0x04 || type == 0xFB || type == 0xFE || type == 0xFF)))
+ (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
return 0;
/* No file header. Reset the fh to 0 so we can read the first packet */
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;
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->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 length _U_, 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->random_fh, phdr, buf, err, err_info)) {
if(*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
+ return TRUE;
+}
- wth->data_offset += (pl_hdr.len + 4);
-
- wth->phdr.len = pl_hdr.len - 8;
- wth->phdr.caplen = pl_hdr.len - 8;
+static gboolean
+packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
+ gchar **err_info)
+{
+ wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
+ wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
- wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
- wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
+ /* 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);
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_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
+ 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)
- return FALSE;
-
- if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err)) {
- if(*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if(!packetlogger_read_header(&pl_hdr, fh, 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 (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;
}
-
- 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;
-
+ 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;
-}
-
-static gboolean
-packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err)
-{
- guint bytes_read = 0;
+ phdr->presence_flags = WTAP_HAS_TS;
- bytes_read += file_read(&pl_hdr->len, 4, 1, fh);
- bytes_read += file_read(&pl_hdr->ts, 8, 1, fh);
+ phdr->len = pl_hdr.len - 8;
+ phdr->caplen = pl_hdr.len - 8;
- /* 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);
+ phdr->ts.secs = (time_t) (pl_hdr.ts >> 32);
+ phdr->ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
- if(bytes_read < PACKETLOGGER_HEADER_SIZE) {
- *err = file_error(fh);
- if(*err == 0 && bytes_read > 0)
- *err = WTAP_ERR_SHORT_READ;
-
- return FALSE;
- }
-
- return TRUE;
+ return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
}