Move the new files to the same places as in automake.
[obnox/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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <glib.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <string.h>
39
40 #include "wtap.h"
41 #include "wtap-int.h"
42 #include "buffer.h"
43 #include "file_wrappers.h"
44 #include "packetlogger.h"
45
46 typedef struct packetlogger_header {
47         guint32 len;
48         guint64 ts;
49 } packetlogger_header_t;
50
51 static gboolean packetlogger_read(wtap *wth, int *err, gchar **err_info,
52                                   gint64 *data_offset);
53 static gboolean packetlogger_seek_read(wtap *wth, gint64 seek_off,
54                                        union wtap_pseudo_header *pseudo_header _U_,
55                                        guint8 *pd, int length, int *err,
56                                        gchar **err_info);
57 static gboolean packetlogger_read_header(packetlogger_header_t *pl_hdr,
58                                          FILE_T fh, int *err, gchar **err_info);
59
60
61 int packetlogger_open(wtap *wth, int *err, gchar **err_info)
62 {
63         packetlogger_header_t pl_hdr;
64         guint8 type;
65
66         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
67                 return -1;
68
69         if (file_read(&type, 1, wth->fh) <= 0)
70                 return -1;
71
72         /* Verify this file belongs to us */
73         if (!((8 <= pl_hdr.len) && (pl_hdr.len < 65536) &&
74               (type < 0x04 || type == 0xFB || type == 0xFC || type == 0xFE || type == 0xFF)))
75                 return 0;
76
77         /* No file header. Reset the fh to 0 so we can read the first packet */
78         if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
79                 return -1;
80
81         /* Set up the pointers to the handlers for this file type */
82         wth->subtype_read = packetlogger_read;
83         wth->subtype_seek_read = packetlogger_seek_read;
84
85         wth->data_offset = 0;
86         wth->file_type = WTAP_FILE_PACKETLOGGER;
87         wth->file_encap = WTAP_ENCAP_PACKETLOGGER;
88         wth->tsprecision = WTAP_FILE_TSPREC_USEC;
89
90         return 1; /* Our kind of file */
91 }
92
93 static gboolean
94 packetlogger_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
95 {
96         packetlogger_header_t pl_hdr;
97         guint bytes_read;
98
99         *data_offset = wth->data_offset;
100
101         if(!packetlogger_read_header(&pl_hdr, wth->fh, err, err_info))
102                 return FALSE;
103
104         if (pl_hdr.len < 8) {
105                 *err = WTAP_ERR_BAD_FILE;
106                 *err_info = g_strdup_printf("packetlogger: record length %u is too small", pl_hdr.len);
107                 return FALSE;
108         }
109         if (pl_hdr.len - 8 > WTAP_MAX_PACKET_SIZE) {
110                 /*
111                  * Probably a corrupt capture file; don't blow up trying
112                  * to allocate space for an immensely-large packet.
113                  */
114                 *err = WTAP_ERR_BAD_FILE;
115                 *err_info = g_strdup_printf("packetlogger: File has %u-byte packet, bigger than maximum of %u",
116                     pl_hdr.len - 8, WTAP_MAX_PACKET_SIZE);
117                 return FALSE;
118         }
119         
120         buffer_assure_space(wth->frame_buffer, pl_hdr.len - 8);
121         bytes_read = file_read(buffer_start_ptr(wth->frame_buffer),
122                                pl_hdr.len - 8,
123                                wth->fh);
124         if(bytes_read != pl_hdr.len - 8) {
125                 *err = file_error(wth->fh, err_info);
126                 if(*err == 0)
127                         *err = WTAP_ERR_SHORT_READ;
128
129                 return FALSE;
130         }
131
132         wth->data_offset += (pl_hdr.len + 4);
133
134         wth->phdr.presence_flags = WTAP_HAS_TS;
135
136         wth->phdr.len = pl_hdr.len - 8;
137         wth->phdr.caplen = pl_hdr.len - 8;
138
139         wth->phdr.ts.secs = (time_t) (pl_hdr.ts >> 32);
140         wth->phdr.ts.nsecs = (int)((pl_hdr.ts & 0xFFFFFFFF) * 1000);
141
142         return TRUE;
143 }
144
145 static gboolean
146 packetlogger_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header
147                        *pseudo_header _U_, guint8 *pd, int length, int *err,
148                        gchar **err_info)
149 {
150         packetlogger_header_t pl_hdr;
151         guint bytes_read;
152
153         if(file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
154                 return FALSE;
155
156         if(!packetlogger_read_header(&pl_hdr, wth->random_fh, err, err_info)) {
157                 if(*err == 0)
158                         *err = WTAP_ERR_SHORT_READ;
159
160                 return FALSE;
161         }
162
163         if(length != (int)pl_hdr.len - 8) {
164                 *err = WTAP_ERR_BAD_FILE;
165                 *err_info = g_strdup_printf("packetlogger: record length %u doesn't match requested length %d", pl_hdr.len, length);
166                 return FALSE;
167         }
168
169         bytes_read = file_read(pd, pl_hdr.len - 8, wth->random_fh);
170         if(bytes_read != (pl_hdr.len - 8)) {
171                 *err = file_error(wth->random_fh, err_info);
172                 if(*err == 0)
173                         *err = WTAP_ERR_SHORT_READ;
174
175                 return FALSE;
176         }
177
178         return TRUE;
179 }
180
181 static gboolean
182 packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
183                          gchar **err_info)
184 {
185         wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
186         wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
187
188         /* Convert multi-byte values from big endian to host endian */
189         pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
190         pl_hdr->ts = GUINT64_FROM_BE(pl_hdr->ts);
191
192         return TRUE;
193 }