03974b1bfb23dcfffb0fe22028b3226a2d5e41a3
[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                                        union wtap_pseudo_header *pseudo_header _U_,
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                 return -1;
66
67         if (file_read(&type, 1, wth->fh) <= 0)
68                 return -1;
69
70         /* Verify this file belongs to us */
71         if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
72               (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
73                 return 0;
74
75         /* No file header. Reset the fh to 0 so we can read the first packet */
76         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
77                 return -1;
78
79         /* Set up the pointers to the handlers for this file type */
80         wth->subtype_read = packetlogger_read;
81         wth->subtype_seek_read = packetlogger_seek_read;
82
83         wth->file_type = WTAP_FILE_PACKETLOGGER;
84         wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
85         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
86
87         return 1; /* Our kind of file */
88 }
89
90 static gboolean
91 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
92 {
93         packetlogger_header_t pl_hdr;
94         guint bytes_read;
95
96         *data_offset = file_tell(wth->fh);
97
98         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
99                 return FALSE;
100
101         if (pl_hdr.len < 8) {
102                 *err = WTAP_ERR_BAD_FILE;
103                 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
104                 return FALSE;
105         }
106         if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
107                 /*
108                  * Probably a corrupt capture file; don't blow up trying
109                  * to allocate space for an immensely-large packet.
110                  */
111                 *err = WTAP_ERR_BAD_FILE;
112                 *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
113                     pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
114                 return FALSE;
115         }
116         
117         buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8);
118         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
119                                pl_hdr.len - 8,
120                                wth->fh);
121         if(bytes_read != pl_hdr.len - 8) {
122                 *err = file_error(wth->fh, err_info);
123                 if(*err == 0)
124                         *err = WTAP_ERR_SHORT_READ;
125
126                 return FALSE;
127         }
128
129         wth->phdr.presence_flags = WTAP_HAS_TS;
130
131         wth->phdr.len = pl_hdr.len - 8;
132         wth->phdr.caplen = pl_hdr.len - 8;
133
134         wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
135         wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
136
137         return TRUE;
138 }
139
140 static gboolean
141 packetlogger_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
142                        *pseudo_header _U_, guint8 *pd, int length, int *err,
143                        gchar **err_info)
144 {
145         packetlogger_header_t pl_hdr;
146         guint bytes_read;
147
148         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
149                 return FALSE;
150
151         if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err, err_info)) {
152                 if(*err == 0)
153                         *err = WTAP_ERR_SHORT_READ;
154
155                 return FALSE;
156         }
157
158         if(length != (int)pl_hdr.len - 8) {
159                 *err = WTAP_ERR_BAD_FILE;
160                 *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length);
161                 return FALSE;
162         }
163
164         bytes_read = file_read(pd, pl_hdr.len - 8, wth->random_fh);
165         if(bytes_read != (pl_hdr.len - 8)) {
166                 *err = file_error(wth->random_fh, err_info);
167                 if(*err == 0)
168                         *err = WTAP_ERR_SHORT_READ;
169
170                 return FALSE;
171         }
172
173         return TRUE;
174 }
175
176 static gboolean
177 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
178                          gchar **err_info)
179 {
180         wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
181         wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
182
183         /* Convert multi-byte values from big endian to host endian */
184         pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
185         pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
186
187         return TRUE;
188 }