Allow wtap_read() and wtap_seek_read() to return non-packet records.
[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  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * Based on commview.c, Linux's BlueZ-Gnome Analyzer program and hexdumps of
10  * the output files from Apple's PacketLogger tool.
11  *
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.
16  *
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.
21  *
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,
25  * USA.
26  */
27
28 #include "config.h"
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #include "wtap.h"
37 #include "wtap-int.h"
38 #include "buffer.h"
39 #include "file_wrappers.h"
40 #include "packetlogger.h"
41
42 typedef struct packetlogger_header {
43         guint32 len;
44         guint64 ts;
45 } packetlogger_header_t;
46
47 static int packetlogger_read(wtap *wth, int *err, gchar **err_info,
48                              gint64 *data_offset);
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,
56                                          gchar **err_info);
57
58 int packetlogger_open(wtap *wth, int *err, gchar **err_info)
59 {
60         packetlogger_header_t pl_hdr;
61         guint8 type;
62
63         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info)) {
64                 if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
65                         return -1;
66                 return 0;
67         }
68
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)
72                         return -1;
73                 return 0;
74         }
75
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)))
79                 return 0;
80
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)
83                 return -1;
84
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;
88
89         wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PACKETLOGGER;
90         wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
91         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
92
93         return 1; /* Our kind of file */
94 }
95
96 static int
97 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
98 {
99         *data_offset = file_tell(wth->fh);
100
101         if (!packetlogger_read_packet(wth->fh, &wth->phdr,
102             wth->frame_buffer, err, err_info))
103                 return -1;
104         return REC_TYPE_PACKET;
105 }
106
107 static int
108 packetlogger_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
109                        Buffer *buf, int *err, gchar **err_info)
110 {
111         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
112                 return -1;
113
114         if(!packetlogger_read_packet(wth->random_fh, phdr, buf, err, err_info)) {
115                 if(*err == 0)
116                         *err = WTAP_ERR_SHORT_READ;
117
118                 return -1;
119         }
120         return REC_TYPE_PACKET;
121 }
122
123 static gboolean
124 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
125                          gchar **err_info)
126 {
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);
129
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);
133
134         return TRUE;
135 }
136
137 static gboolean
138 packetlogger_read_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
139                          int *err, gchar **err_info)
140 {
141         packetlogger_header_t pl_hdr;
142
143         if(!packetlogger_read_header(&pl_hdr, fh, err, err_info))
144                 return FALSE;
145
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);
149                 return FALSE;
150         }
151         if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
152                 /*
153                  * Probably a corrupt capture file; don't blow up trying
154                  * to allocate space for an immensely-large packet.
155                  */
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);
159                 return FALSE;
160         }
161
162         phdr->presence_flags = WTAP_HAS_TS;
163
164         phdr->len = pl_hdr.len - 8;
165         phdr->caplen = pl_hdr.len - 8;
166
167         phdr->ts.secs = (time_t) (pl_hdr.ts >> 32);
168         phdr->ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
169
170         return wtap_read_packet_bytes(fh, buf, phdr->caplen, err, err_info);
171 }