2 * Routines for opening Apple's (Bluetooth) PacketLogger file format captures
3 * Copyright 2008-2009, Stephen Fisher (see AUTHORS file)
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * Based on commview.c, Linux's BlueZ-Gnome Analyzer program and hexdumps of
12 * the output files from Apple's PacketLogger tool.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 #include "file_wrappers.h"
44 #include "packetlogger.h"
46 typedef struct packetlogger_header {
49 } packetlogger_header_t;
51 #define PACKETLOGGER_HEADER_SIZE 12
53 static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info,
55 static gboolean packetlogger_seek_read(wtap *wth, gint64 seek_off,
56 union wtap_pseudo_header *pseudo_header _U_,
57 guchar *pd, int length, int *err,
59 static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
60 FILE_T fh, int *err, gchar **err_info);
63 int packetlogger_open(wtap *wth, int *err, gchar **err_info)
65 packetlogger_header_t pl_hdr;
68 if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
71 if (file_read(&type, 1, wth->fh) <= 0)
74 /* Verify this file belongs to us */
75 if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
76 (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
79 /* No file header. Reset the fh to 0 so we can read the first packet */
80 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
83 /* Set up the pointers to the handlers for this file type */
84 wth->subtype_read = packetlogger_read;
85 wth->subtype_seek_read = packetlogger_seek_read;
88 wth->file_type = WTAP_FILE_PACKETLOGGER;
89 wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
90 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
92 return 1; /* Our kind of file */
96 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
98 packetlogger_header_t pl_hdr;
101 *data_offset = wth->data_offset;
103 if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
106 if (pl_hdr.len < 8) {
107 *err = WTAP_ERR_BAD_RECORD;
108 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
112 buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8);
113 bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
116 if(bytes_read != pl_hdr.len - 8) {
117 *err = file_error(wth->fh, err_info);
119 *err = WTAP_ERR_SHORT_READ;
124 wth->data_offset += (pl_hdr.len + 4);
126 wth->phdr.len = pl_hdr.len - 8;
127 wth->phdr.caplen = pl_hdr.len - 8;
129 wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
130 wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
136 packetlogger_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
137 *pseudo_header _U_, guchar *pd, int length, int *err,
140 packetlogger_header_t pl_hdr;
143 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
146 if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err, err_info)) {
148 *err = WTAP_ERR_SHORT_READ;
153 if(length != (int)pl_hdr.len - 8) {
154 *err = WTAP_ERR_BAD_RECORD;
155 *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length);
159 bytes_read = file_read(pd, pl_hdr.len - 8, wth->random_fh);
160 if(bytes_read != (pl_hdr.len - 8)) {
161 *err = file_error(wth->random_fh, err_info);
163 *err = WTAP_ERR_SHORT_READ;
172 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
175 guint bytes_read = 0;
177 bytes_read += file_read(&pl_hdr->len, 4, fh);
178 bytes_read += file_read(&pl_hdr->ts, 8, fh);
180 /* Convert multi-byte values from big endian to host endian */
181 pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
182 pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
184 if(bytes_read < PACKETLOGGER_HEADER_SIZE) {
185 *err = file_error(fh, err_info);
186 if(*err == 0 && bytes_read > 0)
187 *err = WTAP_ERR_SHORT_READ;