From Tomasz Mon via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8503 :
[metze/wireshark/wip.git] / wiretap / packetlogger.c
1 /* packetlogger.c
2  * Routines for opening Apple's (Bluetooth) PacketLogger file format captures
3  * Copyright 2008-2009, Stephen Fisher (see AUTHORS file)
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  * Based on commview.c, Linux's BlueZ-Gnome Analyzer program and hexdumps of
12  * the output files from Apple's PacketLogger tool.
13  *
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.
18  *
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.
23  *
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,
27  * USA.
28  */
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37
38 #include "wtap.h"
39 #include "wtap-int.h"
40 #include "buffer.h"
41 #include "file_wrappers.h"
42 #include "packetlogger.h"
43
44 typedef struct packetlogger_header {
45         guint32 len;
46         guint64 ts;
47 } packetlogger_header_t;
48
49 static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info,
50                                   gint64 *data_offset);
51 static gboolean packetlogger_seek_read(wtap *wth, gint64 seek_off,
52                                        struct wtap_pkthdr *phdr,
53                                        guint8 *pd, int length, int *err,
54                                        gchar **err_info);
55 static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
56                                          FILE_T fh, int *err, gchar **err_info);
57
58
59 int packetlogger_open(wtap *wth, int *err, gchar **err_info)
60 {
61         packetlogger_header_t pl_hdr;
62         guint8 type;
63
64         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info)) {
65                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
66                         return -1;
67                 return 0;
68         }
69
70         if (file_read(&type, 1, wth->fh) <= 0) {
71                 *err = file_error(wth->fh, err_info);
72                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
73                         return -1;
74                 return 0;
75         }
76
77         /* Verify this file belongs to us */
78         if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
79               (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
80                 return 0;
81
82         /* No file header. Reset the fh to 0 so we can read the first packet */
83         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
84                 return -1;
85
86         /* Set up the pointers to the handlers for this file type */
87         wth->subtype_read = packetlogger_read;
88         wth->subtype_seek_read = packetlogger_seek_read;
89
90         wth->file_type = WTAP_FILE_PACKETLOGGER;
91         wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
92         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
93
94         return 1; /* Our kind of file */
95 }
96
97 static gboolean
98 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
99 {
100         packetlogger_header_t pl_hdr;
101         guint bytes_read;
102
103         *data_offset = file_tell(wth->fh);
104
105         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
106                 return FALSE;
107
108         if (pl_hdr.len < 8) {
109                 *err = WTAP_ERR_BAD_FILE;
110                 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
111                 return FALSE;
112         }
113         if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
114                 /*
115                  * Probably a corrupt capture file; don't blow up trying
116                  * to allocate space for an immensely-large packet.
117                  */
118                 *err = WTAP_ERR_BAD_FILE;
119                 *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
120                     pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
121                 return FALSE;
122         }
123         
124         buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8);
125         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
126                                pl_hdr.len - 8,
127                                wth->fh);
128         if(bytes_read != pl_hdr.len - 8) {
129                 *err = file_error(wth->fh, err_info);
130                 if(*err == 0)
131                         *err = WTAP_ERR_SHORT_READ;
132
133                 return FALSE;
134         }
135
136         wth->phdr.presence_flags = WTAP_HAS_TS;
137
138         wth->phdr.len = pl_hdr.len - 8;
139         wth->phdr.caplen = pl_hdr.len - 8;
140
141         wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
142         wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
143
144         return TRUE;
145 }
146
147 static gboolean
148 packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr _U_,
149                        guint8 *pd, int length, int *err,
150                        gchar **err_info)
151 {
152         packetlogger_header_t pl_hdr;
153         guint bytes_read;
154
155         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
156                 return FALSE;
157
158         if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err, err_info)) {
159                 if(*err == 0)
160                         *err = WTAP_ERR_SHORT_READ;
161
162                 return FALSE;
163         }
164
165         if(length != (int)pl_hdr.len - 8) {
166                 *err = WTAP_ERR_BAD_FILE;
167                 *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length);
168                 return FALSE;
169         }
170
171         bytes_read = file_read(pd, pl_hdr.len - 8, wth->random_fh);
172         if(bytes_read != (pl_hdr.len - 8)) {
173                 *err = file_error(wth->random_fh, err_info);
174                 if(*err == 0)
175                         *err = WTAP_ERR_SHORT_READ;
176
177                 return FALSE;
178         }
179
180         return TRUE;
181 }
182
183 static gboolean
184 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
185                          gchar **err_info)
186 {
187         wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
188         wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
189
190         /* Convert multi-byte values from big endian to host endian */
191         pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
192         pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
193
194         return TRUE;
195 }