Add support for DLT_ value 99, as used by the Axent Raptor
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.91 2004/03/11 09:18:33 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
249 /* Name that should be somewhat descriptive. */
250 const char
251 *wtap_encap_string(int encap)
252 {
253         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
254                 return NULL;
255         else
256                 return encap_table[encap].name;
257 }
258
259 /* Name to use in, say, a command-line flag specifying the type. */
260 const char
261 *wtap_encap_short_string(int encap)
262 {
263         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
264                 return NULL;
265         else
266                 return encap_table[encap].short_name;
267 }
268
269 /* Translate a short name to a capture file type. */
270 int
271 wtap_short_string_to_encap(const char *short_name)
272 {
273         int encap;
274
275         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
276                 if (encap_table[encap].short_name != NULL &&
277                     strcmp(short_name, encap_table[encap].short_name) == 0)
278                         return encap;
279         }
280         return -1;      /* no such encapsulation type */
281 }
282
283 static const char *wtap_errlist[] = {
284         "The file isn't a plain file or pipe",
285         "The file is being opened for random access but is a pipe",
286         "The file isn't a capture file in a known format",
287         "File contains record data we don't support",
288         "That file format cannot be written to a pipe",
289         NULL,
290         "Files can't be saved in that format",
291         "Files from that network type can't be saved in that format",
292         "That file format doesn't support per-packet encapsulations",
293         NULL,
294         NULL,
295         "Less data was read than was expected",
296         "File contains a record that's not valid",
297         "Less data was written than was requested",
298         "Uncompression error: data oddly truncated",
299         "Uncompression error: data would overflow buffer",
300         "Uncompression error: bad LZ77 offset",
301 };
302 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
303
304 const char
305 *wtap_strerror(int err)
306 {
307         static char errbuf[128];
308         unsigned int wtap_errlist_index;
309
310         if (err < 0) {
311 #ifdef HAVE_LIBZ
312                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
313                         /* Assume it's a zlib error. */
314                         sprintf(errbuf, "Uncompression error: %s",
315                             zError(err - WTAP_ERR_ZLIB));
316                         return errbuf;
317                 }
318 #endif
319                 wtap_errlist_index = -1 - err;
320                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
321                         sprintf(errbuf, "Error %d", err);
322                         return errbuf;
323                 }
324                 if (wtap_errlist[wtap_errlist_index] == NULL)
325                         return "Unknown reason";
326                 return wtap_errlist[wtap_errlist_index];
327         } else
328                 return strerror(err);
329 }
330
331 /* Close only the sequential side, freeing up memory it uses.
332
333    Note that we do *not* want to call the subtype's close function,
334    as it would free any per-subtype data, and that data may be
335    needed by the random-access side.
336
337    Instead, if the subtype has a "sequential close" function, we call it,
338    to free up stuff used only by the sequential side. */
339 void
340 wtap_sequential_close(wtap *wth)
341 {
342         if (wth->subtype_sequential_close != NULL)
343                 (*wth->subtype_sequential_close)(wth);
344
345         if (wth->fh != NULL) {
346                 file_close(wth->fh);
347                 wth->fh = NULL;
348         }
349
350         if (wth->frame_buffer) {
351                 buffer_free(wth->frame_buffer);
352                 g_free(wth->frame_buffer);
353                 wth->frame_buffer = NULL;
354         }
355 }
356
357 void
358 wtap_close(wtap *wth)
359 {
360         wtap_sequential_close(wth);
361
362         if (wth->subtype_close != NULL)
363                 (*wth->subtype_close)(wth);
364
365         if (wth->random_fh != NULL)
366                 file_close(wth->random_fh);
367
368         g_free(wth);
369 }
370
371 gboolean
372 wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
373 {
374         /*
375          * Set the packet encapsulation to the file's encapsulation
376          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
377          * right answer (and means that the read routine for this
378          * capture file type doesn't have to set it), and if it
379          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
380          * anyway.
381          */
382         wth->phdr.pkt_encap = wth->file_encap;
383
384         if (!wth->subtype_read(wth, err, err_info, data_offset))
385                 return FALSE;   /* failure */
386
387         /*
388          * It makes no sense for the captured data length to be bigger
389          * than the actual data length.
390          */
391         if (wth->phdr.caplen > wth->phdr.len)
392                 wth->phdr.caplen = wth->phdr.len;
393
394         /*
395          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
396          * probably means the file has that encapsulation type
397          * but the read routine didn't set this packet's
398          * encapsulation type.
399          */
400         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
401
402         return TRUE;    /* success */
403 }
404
405 struct wtap_pkthdr*
406 wtap_phdr(wtap *wth)
407 {
408         return &wth->phdr;
409 }
410
411 union wtap_pseudo_header*
412 wtap_pseudoheader(wtap *wth)
413 {
414         return &wth->pseudo_header;
415 }
416
417 guint8*
418 wtap_buf_ptr(wtap *wth)
419 {
420         return buffer_start_ptr(wth->frame_buffer);
421 }
422
423 gboolean
424 wtap_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
425     gchar **err_info)
426 {
427         long            data_offset;
428         int             loop = 0;
429
430         /* Start by clearing error flag */
431         *err = 0;
432
433         while ( (wtap_read(wth, err, err_info, &data_offset)) ) {
434                 callback(user, &wth->phdr, data_offset,
435                     &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
436                 if (count > 0 && ++loop >= count)
437                         break;
438         }
439
440         if (*err == 0)
441                 return TRUE;    /* success */
442         else
443                 return FALSE;   /* failure */
444 }
445
446 gboolean
447 wtap_seek_read(wtap *wth, long seek_off,
448         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
449         int *err, gchar **err_info)
450 {
451         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
452                 err, err_info);
453 }