2 * Routines for opening Apple's (Bluetooth) PacketLogger file format captures
3 * Copyright 2008-2009, Stephen Fisher (see AUTHORS file)
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * Based on commview.c, Linux's BlueZ-Gnome Analyzer program and hexdumps of
10 * the output files from Apple's PacketLogger tool.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
39 #include "file_wrappers.h"
40 #include "packetlogger.h"
42 typedef struct packetlogger_header {
45 } packetlogger_header_t;
47 static int packetlogger_read(wtap *wth, int *err, gchar **err_info,
49 static int packetlogger_seek_read(wtap *wth, gint64 seek_off,
50 struct wtap_pkthdr *phdr,
51 Buffer *buf, int *err, gchar **err_info);
52 static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
53 FILE_T fh, int *err, gchar **err_info);
54 static gboolean packetlogger_read_packet(FILE_T fh, struct wtap_pkthdr *phdr,
55 Buffer *buf, int *err,
58 int packetlogger_open(wtap *wth, int *err, gchar **err_info)
60 packetlogger_header_t pl_hdr;
63 if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info)) {
64 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
69 if (file_read(&type, 1, wth->fh) <= 0) {
70 *err = file_error(wth->fh, err_info);
71 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
76 /* Verify this file belongs to us */
77 if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
78 (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
81 /* No file header. Reset the fh to 0 so we can read the first packet */
82 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
85 /* Set up the pointers to the handlers for this file type */
86 wth->subtype_read = packetlogger_read;
87 wth->subtype_seek_read = packetlogger_seek_read;
89 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PACKETLOGGER;
90 wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
91 wth->tsprecision = WTAP_FILE_TSPREC_USEC;
93 return 1; /* Our kind of file */
97 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
99 *data_offset = file_tell(wth->fh);
101 if (!packetlogger_read_packet(wth->fh, &wth->phdr,
102 wth->frame_buffer, err, err_info))
104 return REC_TYPE_PACKET;
108 packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
109 Buffer *buf, int *err, gchar **err_info)
111 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
114 if(!packetlogger_read_packet(wth->random_fh, phdr, buf, err, err_info)) {
116 *err = WTAP_ERR_SHORT_READ;
120 return REC_TYPE_PACKET;
124 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
127 wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
128 wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
130 /* Convert multi-byte values from big endian to host endian */
131 pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
132 pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
138 packetlogger_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
139 int *err, gchar **err_info)
141 packetlogger_header_t pl_hdr;
143 if(!packetlogger_read_header(&pl_hdr, fh, err, err_info))
146 if (pl_hdr.len < 8) {
147 *err = WTAP_ERR_BAD_FILE;
148 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
151 if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
153 * Probably a corrupt capture file; don't blow up trying
154 * to allocate space for an immensely-large packet.
156 *err = WTAP_ERR_BAD_FILE;
157 *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
158 pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
162 phdr->presence_flags = WTAP_HAS_TS;
164 phdr->len = pl_hdr.len - 8;
165 phdr->caplen = pl_hdr.len - 8;
167 phdr->ts.secs = (time_t) (pl_hdr.ts >> 32);
168 phdr->ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
170 return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);