Add support for DLT_APPLE_IP_OVER_IEEE_1394.
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.92 2004/03/23 01:02:41 guy Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #include <string.h>
24 #include <errno.h>
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "wtap-int.h"
30 #include "file_wrappers.h"
31 #include "buffer.h"
32
33 int
34 wtap_fd(wtap *wth)
35 {
36         return wth->fd;
37 }
38
39 int
40 wtap_file_type(wtap *wth)
41 {
42         return wth->file_type;
43 }
44
45 int
46 wtap_snapshot_length(wtap *wth)
47 {
48         return wth->snapshot_length;
49 }
50
51 int
52 wtap_file_encap(wtap *wth)
53 {
54         return wth->file_encap;
55 }
56
57 /* Table of the encapsulation types we know about. */
58 static const struct encap_type_info {
59         const char *name;
60         const char *short_name;
61 } encap_table[WTAP_NUM_ENCAP_TYPES] = {
62         /* WTAP_ENCAP_UNKNOWN */
63         { "Unknown", NULL },
64
65         /* WTAP_ENCAP_ETHERNET */
66         { "Ethernet", "ether" },
67
68         /* WTAP_ENCAP_TOKEN_RING */
69         { "Token Ring", "tr" },
70
71         /* WTAP_ENCAP_SLIP */
72         { "SLIP", "slip" },
73
74         /* WTAP_ENCAP_PPP */
75         { "PPP", "ppp" },
76
77         /* WTAP_ENCAP_FDDI */
78         { "FDDI", "fddi" },
79
80         /* WTAP_ENCAP_FDDI_BITSWAPPED */
81         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
82
83         /* WTAP_ENCAP_RAW_IP */
84         { "Raw IP", "rawip" },
85
86         /* WTAP_ENCAP_ARCNET */
87         { "ARCNET", "arcnet" },
88
89         /* WTAP_ENCAP_ARCNET_LINUX */
90         { "Linux ARCNET", "arcnet_linux" },
91
92         /* WTAP_ENCAP_ATM_RFC1483 */
93         { "RFC 1483 ATM", "atm-rfc1483" },
94
95         /* WTAP_ENCAP_LINUX_ATM_CLIP */
96         { "Linux ATM CLIP", "linux-atm-clip" },
97
98         /* WTAP_ENCAP_LAPB */
99         { "LAPB", "lapb" },
100
101         /* WTAP_ENCAP_ATM_PDUS */
102         { "ATM PDUs", "atm-pdus" },
103
104         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
105         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
106
107         /* WTAP_ENCAP_NULL */
108         { "NULL", "null" },
109
110         /* WTAP_ENCAP_ASCEND */
111         { "Lucent/Ascend access equipment", "ascend" },
112
113         /* WTAP_ENCAP_ISDN */
114         { "ISDN", "isdn" },
115
116         /* WTAP_ENCAP_IP_OVER_FC */
117         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
118
119         /* WTAP_ENCAP_PPP_WITH_PHDR */
120         { "PPP with Directional Info", "ppp-with-direction" },
121
122         /* WTAP_ENCAP_IEEE_802_11 */
123         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
124
125         /* WTAP_ENCAP_PRISM_HEADER */
126         { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
127
128         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
129         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
130
131         /* WTAP_ENCAP_IEEE_802_11_WLAN_BSD */
132         { "IEEE 802.11 plus BSD WLAN header", "ieee-802-11-bsd" },
133
134         /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
135         { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
136
137         /* WTAP_ENCAP_SLL */
138         { "Linux cooked-mode capture", "linux-sll" },
139
140         /* WTAP_ENCAP_FRELAY */
141         { "Frame Relay", "frelay" },
142
143         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
144         { "Frame Relay with Directional Info", "frelay-with-direction" },
145
146         /* WTAP_ENCAP_CHDLC */
147         { "Cisco HDLC", "chdlc" },
148
149         /* WTAP_ENCAP_CISCO_IOS */
150         { "Cisco IOS internal", "ios" },
151
152         /* WTAP_ENCAP_LOCALTALK */
153         { "Localtalk", "ltalk" },
154
155         /* WTAP_ENCAP_OLD_PFLOG  */
156         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
157
158         /* WTAP_ENCAP_HHDLC */
159         { "HiPath HDLC", "hhdlc" },
160
161         /* WTAP_ENCAP_DOCSIS */
162         { "Data Over Cable Service Interface Specification", "docsis" },
163
164         /* WTAP_ENCAP_COSINE */
165         { "CoSine L2 debug log", "cosine" },
166
167         /* WTAP_ENCAP_WFLEET_HDLC */
168         { "Wellfleet HDLC", "whdlc" },
169
170         /* WTAP_ENCAP_SDLC */
171         { "SDLC", "sdlc" },
172
173         /* WTAP_ENCAP_TZSP */
174         { "Tazmen sniffer protocol", "tzsp" },
175
176         /* WTAP_ENCAP_ENC */
177         { "OpenBSD enc(4) encapsulating interface", "enc" },
178
179         /* WTAP_ENCAP_PFLOG  */
180         { "OpenBSD PF Firewall logs", "pflog" },
181
182         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
183         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
184
185         /* WTAP_ENCAP_BLUETOOTH_H4 */
186         { "Bluetooth H4", "bluetooth-h4" },
187
188         /* WTAP_ENCAP_MTP2 */
189         { "SS7 MTP2", "mtp2" },
190
191         /* WTAP_ENCAP_MTP3 */
192         { "SS7 MTP3", "mtp3" },
193
194         /* WTAP_ENCAP_IRDA */
195         { "IrDA", "irda" },
196
197         /* WTAP_ENCAP_USER0 */
198         { "USER 0", "user0" },
199
200         /* WTAP_ENCAP_USER1 */
201         { "USER 1", "user1" },
202
203         /* WTAP_ENCAP_USER2 */
204         { "USER 2", "user2" },
205
206         /* WTAP_ENCAP_USER3 */
207         { "USER 3", "user3" },
208
209         /* WTAP_ENCAP_USER4 */
210         { "USER 4", "user4" },
211
212         /* WTAP_ENCAP_USER5 */
213         { "USER 5", "user5" },
214
215         /* WTAP_ENCAP_USER6 */
216         { "USER 6", "user6" },
217
218         /* WTAP_ENCAP_USER7 */
219         { "USER 7", "user7" },
220
221         /* WTAP_ENCAP_USER8 */
222         { "USER 8", "user8" },
223
224         /* WTAP_ENCAP_USER9 */
225         { "USER 9", "user9" },
226
227         /* WTAP_ENCAP_USER10 */
228         { "USER 10", "user10" },
229
230         /* WTAP_ENCAP_USER11 */
231         { "USER 11", "user11" },
232
233         /* WTAP_ENCAP_USER12 */
234         { "USER 12", "user12" },
235
236         /* WTAP_ENCAP_USER13 */
237         { "USER 13", "user13" },
238
239         /* WTAP_ENCAP_USER14 */
240         { "USER 14", "user14" },
241
242         /* WTAP_ENCAP_USER15 */
243         { "USER 15", "user15" },
244
245         /* WTAP_ENCAP_SYMANTEC */
246         { "Symantec Enterprise Firewall", "symantec" },
247
248         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
249         { "Apple IP-over-IEEE 1394", "ap1394" },
250 };
251
252 /* Name that should be somewhat descriptive. */
253 const char
254 *wtap_encap_string(int encap)
255 {
256         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
257                 return NULL;
258         else
259                 return encap_table[encap].name;
260 }
261
262 /* Name to use in, say, a command-line flag specifying the type. */
263 const char
264 *wtap_encap_short_string(int encap)
265 {
266         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
267                 return NULL;
268         else
269                 return encap_table[encap].short_name;
270 }
271
272 /* Translate a short name to a capture file type. */
273 int
274 wtap_short_string_to_encap(const char *short_name)
275 {
276         int encap;
277
278         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
279                 if (encap_table[encap].short_name != NULL &&
280                     strcmp(short_name, encap_table[encap].short_name) == 0)
281                         return encap;
282         }
283         return -1;      /* no such encapsulation type */
284 }
285
286 static const char *wtap_errlist[] = {
287         "The file isn't a plain file or pipe",
288         "The file is being opened for random access but is a pipe",
289         "The file isn't a capture file in a known format",
290         "File contains record data we don't support",
291         "That file format cannot be written to a pipe",
292         NULL,
293         "Files can't be saved in that format",
294         "Files from that network type can't be saved in that format",
295         "That file format doesn't support per-packet encapsulations",
296         NULL,
297         NULL,
298         "Less data was read than was expected",
299         "File contains a record that's not valid",
300         "Less data was written than was requested",
301         "Uncompression error: data oddly truncated",
302         "Uncompression error: data would overflow buffer",
303         "Uncompression error: bad LZ77 offset",
304 };
305 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
306
307 const char
308 *wtap_strerror(int err)
309 {
310         static char errbuf[128];
311         unsigned int wtap_errlist_index;
312
313         if (err < 0) {
314 #ifdef HAVE_LIBZ
315                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
316                         /* Assume it's a zlib error. */
317                         sprintf(errbuf, "Uncompression error: %s",
318                             zError(err - WTAP_ERR_ZLIB));
319                         return errbuf;
320                 }
321 #endif
322                 wtap_errlist_index = -1 - err;
323                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
324                         sprintf(errbuf, "Error %d", err);
325                         return errbuf;
326                 }
327                 if (wtap_errlist[wtap_errlist_index] == NULL)
328                         return "Unknown reason";
329                 return wtap_errlist[wtap_errlist_index];
330         } else
331                 return strerror(err);
332 }
333
334 /* Close only the sequential side, freeing up memory it uses.
335
336    Note that we do *not* want to call the subtype's close function,
337    as it would free any per-subtype data, and that data may be
338    needed by the random-access side.
339
340    Instead, if the subtype has a "sequential close" function, we call it,
341    to free up stuff used only by the sequential side. */
342 void
343 wtap_sequential_close(wtap *wth)
344 {
345         if (wth->subtype_sequential_close != NULL)
346                 (*wth->subtype_sequential_close)(wth);
347
348         if (wth->fh != NULL) {
349                 file_close(wth->fh);
350                 wth->fh = NULL;
351         }
352
353         if (wth->frame_buffer) {
354                 buffer_free(wth->frame_buffer);
355                 g_free(wth->frame_buffer);
356                 wth->frame_buffer = NULL;
357         }
358 }
359
360 void
361 wtap_close(wtap *wth)
362 {
363         wtap_sequential_close(wth);
364
365         if (wth->subtype_close != NULL)
366                 (*wth->subtype_close)(wth);
367
368         if (wth->random_fh != NULL)
369                 file_close(wth->random_fh);
370
371         g_free(wth);
372 }
373
374 gboolean
375 wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
376 {
377         /*
378          * Set the packet encapsulation to the file's encapsulation
379          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
380          * right answer (and means that the read routine for this
381          * capture file type doesn't have to set it), and if it
382          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
383          * anyway.
384          */
385         wth->phdr.pkt_encap = wth->file_encap;
386
387         if (!wth->subtype_read(wth, err, err_info, data_offset))
388                 return FALSE;   /* failure */
389
390         /*
391          * It makes no sense for the captured data length to be bigger
392          * than the actual data length.
393          */
394         if (wth->phdr.caplen > wth->phdr.len)
395                 wth->phdr.caplen = wth->phdr.len;
396
397         /*
398          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
399          * probably means the file has that encapsulation type
400          * but the read routine didn't set this packet's
401          * encapsulation type.
402          */
403         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
404
405         return TRUE;    /* success */
406 }
407
408 struct wtap_pkthdr*
409 wtap_phdr(wtap *wth)
410 {
411         return &wth->phdr;
412 }
413
414 union wtap_pseudo_header*
415 wtap_pseudoheader(wtap *wth)
416 {
417         return &wth->pseudo_header;
418 }
419
420 guint8*
421 wtap_buf_ptr(wtap *wth)
422 {
423         return buffer_start_ptr(wth->frame_buffer);
424 }
425
426 gboolean
427 wtap_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
428     gchar **err_info)
429 {
430         long            data_offset;
431         int             loop = 0;
432
433         /* Start by clearing error flag */
434         *err = 0;
435
436         while ( (wtap_read(wth, err, err_info, &data_offset)) ) {
437                 callback(user, &wth->phdr, data_offset,
438                     &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
439                 if (count > 0 && ++loop >= count)
440                         break;
441         }
442
443         if (*err == 0)
444                 return TRUE;    /* success */
445         else
446                 return FALSE;   /* failure */
447 }
448
449 gboolean
450 wtap_seek_read(wtap *wth, long seek_off,
451         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
452         int *err, gchar **err_info)
453 {
454         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
455                 err, err_info);
456 }