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,
38 #include <wsutil/buffer.h>
39 #include "file_wrappers.h"
40 #include "packetlogger.h"
42 typedef struct packetlogger_header {
45 } packetlogger_header_t;
47 static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info,
49 static gboolean 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 wtap_open_return_val 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)
65 return WTAP_OPEN_ERROR;
66 return WTAP_OPEN_NOT_MINE;
69 if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
70 if (*err != WTAP_ERR_SHORT_READ)
71 return WTAP_OPEN_ERROR;
72 return WTAP_OPEN_NOT_MINE;
75 /* Verify this file belongs to us */
76 if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
77 (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
78 return WTAP_OPEN_NOT_MINE;
80 /* No file header. Reset the fh to 0 so we can read the first packet */
81 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
82 return WTAP_OPEN_ERROR;
84 /* Set up the pointers to the handlers for this file type */
85 wth->subtype_read = packetlogger_read;
86 wth->subtype_seek_read = packetlogger_seek_read;
88 wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PACKETLOGGER;
89 wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
90 wth->file_tsprec = WTAP_TSPREC_USEC;
92 return WTAP_OPEN_MINE; /* Our kind of file */
96 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
98 *data_offset = file_tell(wth->fh);
100 return packetlogger_read_packet(wth->fh, &wth->phdr,
101 wth->frame_buffer, err, err_info);
105 packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
106 Buffer *buf, int *err, gchar **err_info)
108 if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
111 if(!packetlogger_read_packet(wth->random_fh, phdr, buf, err, err_info)) {
113 *err = WTAP_ERR_SHORT_READ;
121 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
124 if (!wtap_read_bytes_or_eof(fh, &pl_hdr->len, 4, err, err_info))
126 if (!wtap_read_bytes(fh, &pl_hdr->ts, 8, err, err_info))
129 /* Convert multi-byte values from big endian to host endian */
130 pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
131 pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
137 packetlogger_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
138 int *err, gchar **err_info)
140 packetlogger_header_t pl_hdr;
142 if(!packetlogger_read_header(&pl_hdr, fh, err, err_info))
145 if (pl_hdr.len < 8) {
146 *err = WTAP_ERR_BAD_FILE;
147 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
150 if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
152 * Probably a corrupt capture file; don't blow up trying
153 * to allocate space for an immensely-large packet.
155 *err = WTAP_ERR_BAD_FILE;
156 *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
157 pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
161 phdr->rec_type = REC_TYPE_PACKET;
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);