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