From Owen Kirby :
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #include "wtap-int.h"
39 #include "wtap.h"
40
41 #include "file_wrappers.h"
42 #include "file_util.h"
43 #include "buffer.h"
44
45 /*
46  * Return the size of the file, as reported by the OS.
47  * (gint64, in case that's 64 bits.)
48  */
49 gint64
50 wtap_file_size(wtap *wth, int *err)
51 {
52         struct stat statb;
53
54         if (fstat(wth->fd, &statb) == -1) {
55                 if (err != NULL)
56                         *err = errno;
57                 return -1;
58         }
59         return statb.st_size;
60 }
61
62 int
63 wtap_file_type(wtap *wth)
64 {
65         return wth->file_type;
66 }
67
68 int
69 wtap_snapshot_length(wtap *wth)
70 {
71         return wth->snapshot_length;
72 }
73
74 int
75 wtap_file_encap(wtap *wth)
76 {
77         return wth->file_encap;
78 }
79
80 int
81 wtap_file_tsprecision(wtap *wth)
82 {
83         return wth->tsprecision;
84 }
85
86 /* Table of the encapsulation types we know about. */
87 struct encap_type_info {
88         const char *name;
89         const char *short_name;
90 };
91
92 static struct encap_type_info encap_table_base[] = {
93         /* WTAP_ENCAP_UNKNOWN */
94         { "Unknown", NULL },
95
96         /* WTAP_ENCAP_ETHERNET */
97         { "Ethernet", "ether" },
98
99         /* WTAP_ENCAP_TOKEN_RING */
100         { "Token Ring", "tr" },
101
102         /* WTAP_ENCAP_SLIP */
103         { "SLIP", "slip" },
104
105         /* WTAP_ENCAP_PPP */
106         { "PPP", "ppp" },
107
108         /* WTAP_ENCAP_FDDI */
109         { "FDDI", "fddi" },
110
111         /* WTAP_ENCAP_FDDI_BITSWAPPED */
112         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
113
114         /* WTAP_ENCAP_RAW_IP */
115         { "Raw IP", "rawip" },
116
117         /* WTAP_ENCAP_ARCNET */
118         { "ARCNET", "arcnet" },
119
120         /* WTAP_ENCAP_ARCNET_LINUX */
121         { "Linux ARCNET", "arcnet_linux" },
122
123         /* WTAP_ENCAP_ATM_RFC1483 */
124         { "RFC 1483 ATM", "atm-rfc1483" },
125
126         /* WTAP_ENCAP_LINUX_ATM_CLIP */
127         { "Linux ATM CLIP", "linux-atm-clip" },
128
129         /* WTAP_ENCAP_LAPB */
130         { "LAPB", "lapb" },
131
132         /* WTAP_ENCAP_ATM_PDUS */
133         { "ATM PDUs", "atm-pdus" },
134
135         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
136         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
137
138         /* WTAP_ENCAP_NULL */
139         { "NULL", "null" },
140
141         /* WTAP_ENCAP_ASCEND */
142         { "Lucent/Ascend access equipment", "ascend" },
143
144         /* WTAP_ENCAP_ISDN */
145         { "ISDN", "isdn" },
146
147         /* WTAP_ENCAP_IP_OVER_FC */
148         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
149
150         /* WTAP_ENCAP_PPP_WITH_PHDR */
151         { "PPP with Directional Info", "ppp-with-direction" },
152
153         /* WTAP_ENCAP_IEEE_802_11 */
154         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
155
156         /* WTAP_ENCAP_PRISM_HEADER */
157         { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
158
159         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
160         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
161
162         /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
163         { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
164
165         /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
166         { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
167
168         /* WTAP_ENCAP_SLL */
169         { "Linux cooked-mode capture", "linux-sll" },
170
171         /* WTAP_ENCAP_FRELAY */
172         { "Frame Relay", "frelay" },
173
174         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
175         { "Frame Relay with Directional Info", "frelay-with-direction" },
176
177         /* WTAP_ENCAP_CHDLC */
178         { "Cisco HDLC", "chdlc" },
179
180         /* WTAP_ENCAP_CISCO_IOS */
181         { "Cisco IOS internal", "ios" },
182
183         /* WTAP_ENCAP_LOCALTALK */
184         { "Localtalk", "ltalk" },
185
186         /* WTAP_ENCAP_OLD_PFLOG  */
187         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
188
189         /* WTAP_ENCAP_HHDLC */
190         { "HiPath HDLC", "hhdlc" },
191
192         /* WTAP_ENCAP_DOCSIS */
193         { "Data Over Cable Service Interface Specification", "docsis" },
194
195         /* WTAP_ENCAP_COSINE */
196         { "CoSine L2 debug log", "cosine" },
197
198         /* WTAP_ENCAP_WFLEET_HDLC */
199         { "Wellfleet HDLC", "whdlc" },
200
201         /* WTAP_ENCAP_SDLC */
202         { "SDLC", "sdlc" },
203
204         /* WTAP_ENCAP_TZSP */
205         { "Tazmen sniffer protocol", "tzsp" },
206
207         /* WTAP_ENCAP_ENC */
208         { "OpenBSD enc(4) encapsulating interface", "enc" },
209
210         /* WTAP_ENCAP_PFLOG  */
211         { "OpenBSD PF Firewall logs", "pflog" },
212
213         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
214         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
215
216         /* WTAP_ENCAP_BLUETOOTH_H4 */
217         { "Bluetooth H4", "bluetooth-h4" },
218
219         /* WTAP_ENCAP_MTP2 */
220         { "SS7 MTP2", "mtp2" },
221
222         /* WTAP_ENCAP_MTP3 */
223         { "SS7 MTP3", "mtp3" },
224
225         /* WTAP_ENCAP_IRDA */
226         { "IrDA", "irda" },
227
228         /* WTAP_ENCAP_USER0 */
229         { "USER 0", "user0" },
230
231         /* WTAP_ENCAP_USER1 */
232         { "USER 1", "user1" },
233
234         /* WTAP_ENCAP_USER2 */
235         { "USER 2", "user2" },
236
237         /* WTAP_ENCAP_USER3 */
238         { "USER 3", "user3" },
239
240         /* WTAP_ENCAP_USER4 */
241         { "USER 4", "user4" },
242
243         /* WTAP_ENCAP_USER5 */
244         { "USER 5", "user5" },
245
246         /* WTAP_ENCAP_USER6 */
247         { "USER 6", "user6" },
248
249         /* WTAP_ENCAP_USER7 */
250         { "USER 7", "user7" },
251
252         /* WTAP_ENCAP_USER8 */
253         { "USER 8", "user8" },
254
255         /* WTAP_ENCAP_USER9 */
256         { "USER 9", "user9" },
257
258         /* WTAP_ENCAP_USER10 */
259         { "USER 10", "user10" },
260
261         /* WTAP_ENCAP_USER11 */
262         { "USER 11", "user11" },
263
264         /* WTAP_ENCAP_USER12 */
265         { "USER 12", "user12" },
266
267         /* WTAP_ENCAP_USER13 */
268         { "USER 13", "user13" },
269
270         /* WTAP_ENCAP_USER14 */
271         { "USER 14", "user14" },
272
273         /* WTAP_ENCAP_USER15 */
274         { "USER 15", "user15" },
275
276         /* WTAP_ENCAP_SYMANTEC */
277         { "Symantec Enterprise Firewall", "symantec" },
278
279         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
280         { "Apple IP-over-IEEE 1394", "ap1394" },
281
282         /* WTAP_ENCAP_BACNET_MS_TP */
283         { "BACnet MS/TP", "bacnet-ms-tp" },
284
285         /* WTAP_ENCAP_NETTL_RAW_ICMP */
286         { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
287
288         /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
289         { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
290
291         /* WTAP_ENCAP_GPRS_LLC */
292         { "GPRS LLC", "gprs-llc" },
293
294         /* WTAP_ENCAP_JUNIPER_ATM1 */
295         { "Juniper ATM1", "juniper-atm1" },
296
297         /* WTAP_ENCAP_JUNIPER_ATM2 */
298         { "Juniper ATM2", "juniper-atm2" },
299
300         /* WTAP_ENCAP_REDBACK */
301         { "Redback SmartEdge", "redback" },
302
303         /* WTAP_ENCAP_NETTL_RAW_IP */
304         { "Raw IP with nettl headers", "rawip-nettl" },
305
306         /* WTAP_ENCAP_NETTL_ETHERNET */
307         { "Ethernet with nettl headers", "ether-nettl" },
308
309         /* WTAP_ENCAP_NETTL_TOKEN_RING */
310         { "Token Ring with nettl headers", "tr-nettl" },
311
312         /* WTAP_ENCAP_NETTL_FDDI */
313         { "FDDI with nettl headers", "fddi-nettl" },
314
315         /* WTAP_ENCAP_NETTL_UNKNOWN */
316         { "Unknown link-layer type with nettl headers", "unknown-nettl" },
317
318         /* WTAP_ENCAP_MTP2_WITH_PHDR */
319         { "MTP2 with pseudoheader", "mtp2-with-phdr" },
320
321         /* WTAP_ENCAP_JUNIPER_PPPOE */
322         { "Juniper PPPoE", "juniper-pppoe" },
323
324         /* WTAP_ENCAP_GCOM_TIE1 */
325         { "GCOM TIE1", "gcom-tie1" },
326
327         /* WTAP_ENCAP_GCOM_SERIAL */
328         { "GCOM Serial", "gcom-serial" },
329
330         /* WTAP_ENCAP_NETTL_X25 */
331         { "X25 with nettl headers", "x25-nettl" },
332
333         /* WTAP_ENCAP_K12 */
334         { "K12 protocol analyzer", "k12" },
335
336         /* WTAP_ENCAP_JUNIPER_MLPPP */
337         { "Juniper MLPPP", "juniper-mlppp" },
338
339         /* WTAP_ENCAP_JUNIPER_MLFR */
340         { "Juniper MLFR", "juniper-mlfr" },
341
342         /* WTAP_ENCAP_JUNIPER_ETHER */
343         { "Juniper Ethernet", "juniper-ether" },
344
345         /* WTAP_ENCAP_JUNIPER_PPP */
346         { "Juniper PPP", "juniper-ppp" },
347
348         /* WTAP_ENCAP_JUNIPER_FRELAY */
349         { "Juniper Frame-Relay", "juniper-frelay" },
350
351         /* WTAP_ENCAP_JUNIPER_CHDLC */
352         { "Juniper C-HDLC", "juniper-chdlc" },
353
354         /* WTAP_ENCAP_JUNIPER_GGSN */
355         { "Juniper GGSN", "juniper-ggsn" },
356
357         /* WTAP_ENCAP_LINUX_LAPD */
358         { "LAPD", "lapd" },
359
360         /* WTAP_ENCAP_CATAPULT_DCT2000 */
361         { "Catapult DCT2000", "dct2000" },
362
363         /* WTAP_ENCAP_BER */
364         { "ASN.1 Basic Encoding Rules", "ber" },
365
366         /* WTAP_ENCAP_JUNIPER_VP */
367         { "Juniper Voice PIC", "juniper-vp" },
368
369         /* WTAP_ENCAP_USB */
370         { "Raw USB packets", "usb" },
371
372         /* WTAP_ENCAP_IEEE802_16_MAC_CPS */
373         { "IEEE 802.16 MAC Common Part Sublayer", "ieee-802-16-mac-cps" },
374
375         /* WTAP_ENCAP_NETTL_RAW_TELNET */
376         { "Raw telnet with nettl headers", "raw-telnet-nettl" },
377
378         /* WTAP_ENCAP_USB_LINUX */
379         { "USB packets with Linux header", "usb-linux" },
380
381         /* WTAP_ENCAP_MPEG */
382         { "MPEG", "mpeg" },
383
384         /* WTAP_ENCAP_PPI */
385         { "Per-Packet Information header", "ppi" },
386
387         /* WTAP_ENCAP_ERF */
388         { "Endace Record File", "erf" },
389
390         /* WTAP_ENCAP_BT_H4 */
391         { "Bluetooth H4 with linux header", "bluetooth-h4" },
392
393         /* WTAP_ENCAP_SITA */
394         { "SITA WAN packets", "sita-wan" },
395
396         /* WTAP_ENCAP_SCCP */
397         { "SS7 SCCP", "sccp" },
398
399         /* WTAP_ENCAP_BLUETOOTH_HCI */
400         { "Bluetooth without transport layer", "bluetooth-hci" },
401
402         /* WTAP_ENCAP_IPMB */
403         { "Intelligent Platform Management Bus", "ipmb" },
404
405         /* WTAP_ENCAP_IEEE802_15_4 */
406         { "IEEE 802.15.4 Wireless PAN", "wpan" }
407 };
408
409 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
410 static GArray* encap_table_arr = NULL;
411 static const struct encap_type_info* encap_table = NULL;
412
413 static void wtap_init_encap_types(void) {
414
415         if (encap_table_arr) return;
416
417         encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
418
419         g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
420
421         encap_table = (void*)encap_table_arr->data;
422 }
423
424 int wtap_get_num_encap_types(void) {
425         wtap_init_encap_types();
426         return wtap_num_encap_types;
427 }
428
429
430 int wtap_register_encap_type(char* name, char* short_name) {
431         struct encap_type_info* e = g_malloc(sizeof(struct encap_type_info));
432         wtap_init_encap_types();
433
434         e->name = g_strdup(name);
435         e->short_name = g_strdup(short_name);
436
437         g_array_append_val(encap_table_arr,e);
438
439         encap_table = (void*)encap_table_arr->data;
440         return wtap_num_encap_types++;
441 }
442
443
444 /* Name that should be somewhat descriptive. */
445 const char
446 *wtap_encap_string(int encap)
447 {
448         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
449                 return NULL;
450         else
451                 return encap_table[encap].name;
452 }
453
454 /* Name to use in, say, a command-line flag specifying the type. */
455 const char
456 *wtap_encap_short_string(int encap)
457 {
458         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
459                 return NULL;
460         else
461                 return encap_table[encap].short_name;
462 }
463
464 /* Translate a short name to a capture file type. */
465 int
466 wtap_short_string_to_encap(const char *short_name)
467 {
468         int encap;
469
470         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
471                 if (encap_table[encap].short_name != NULL &&
472                     strcmp(short_name, encap_table[encap].short_name) == 0)
473                         return encap;
474         }
475         return -1;      /* no such encapsulation type */
476 }
477
478 static const char *wtap_errlist[] = {
479         "The file isn't a plain file or pipe",
480         "The file is being opened for random access but is a pipe",
481         "The file isn't a capture file in a known format",
482         "File contains record data we don't support",
483         "That file format cannot be written to a pipe",
484         NULL,
485         "Files can't be saved in that format",
486         "Files from that network type can't be saved in that format",
487         "That file format doesn't support per-packet encapsulations",
488         NULL,
489         NULL,
490         "Less data was read than was expected",
491         "File contains a record that's not valid",
492         "Less data was written than was requested",
493         "Uncompression error: data oddly truncated",
494         "Uncompression error: data would overflow buffer",
495         "Uncompression error: bad LZ77 offset",
496         "The standard input cannot be opened for random access"
497 };
498 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
499
500 const char
501 *wtap_strerror(int err)
502 {
503         static char errbuf[128];
504         unsigned int wtap_errlist_index;
505
506         if (err < 0) {
507 #ifdef HAVE_LIBZ
508                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
509                         /* Assume it's a zlib error. */
510                         g_snprintf(errbuf, 128, "Uncompression error: %s",
511                             zError(err - WTAP_ERR_ZLIB));
512                         return errbuf;
513                 }
514 #endif
515                 wtap_errlist_index = -1 - err;
516                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
517                         g_snprintf(errbuf, 128, "Error %d", err);
518                         return errbuf;
519                 }
520                 if (wtap_errlist[wtap_errlist_index] == NULL)
521                         return "Unknown reason";
522                 return wtap_errlist[wtap_errlist_index];
523         } else
524                 return strerror(err);
525 }
526
527 /* Close only the sequential side, freeing up memory it uses.
528
529    Note that we do *not* want to call the subtype's close function,
530    as it would free any per-subtype data, and that data may be
531    needed by the random-access side.
532
533    Instead, if the subtype has a "sequential close" function, we call it,
534    to free up stuff used only by the sequential side. */
535 void
536 wtap_sequential_close(wtap *wth)
537 {
538         if (wth->subtype_sequential_close != NULL)
539                 (*wth->subtype_sequential_close)(wth);
540
541         if (wth->fh != NULL) {
542                 file_close(wth->fh);
543                 wth->fh = NULL;
544         }
545
546         if (wth->frame_buffer) {
547                 buffer_free(wth->frame_buffer);
548                 g_free(wth->frame_buffer);
549                 wth->frame_buffer = NULL;
550         }
551 }
552
553 void
554 wtap_close(wtap *wth)
555 {
556         wtap_sequential_close(wth);
557
558         if (wth->subtype_close != NULL)
559                 (*wth->subtype_close)(wth);
560
561         if (wth->random_fh != NULL)
562                 file_close(wth->random_fh);
563
564         g_free(wth);
565 }
566
567 gboolean
568 wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
569 {
570         /*
571          * Set the packet encapsulation to the file's encapsulation
572          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
573          * right answer (and means that the read routine for this
574          * capture file type doesn't have to set it), and if it
575          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
576          * anyway.
577          */
578         wth->phdr.pkt_encap = wth->file_encap;
579
580         if (!wth->subtype_read(wth, err, err_info, data_offset))
581                 return FALSE;   /* failure */
582
583         /*
584          * It makes no sense for the captured data length to be bigger
585          * than the actual data length.
586          */
587         if (wth->phdr.caplen > wth->phdr.len)
588                 wth->phdr.caplen = wth->phdr.len;
589
590         /*
591          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
592          * probably means the file has that encapsulation type
593          * but the read routine didn't set this packet's
594          * encapsulation type.
595          */
596         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
597
598         return TRUE;    /* success */
599 }
600
601 /*
602  * Return an approximation of the amount of data we've read sequentially
603  * from the file so far.  (gint64, in case that's 64 bits.)
604  */
605 gint64
606 wtap_read_so_far(wtap *wth, int *err)
607 {
608         off_t file_pos;
609
610         file_pos = eth_lseek(wth->fd, 0, SEEK_CUR);
611         if (file_pos == -1) {
612                 if (err != NULL)
613                         *err = errno;
614                 return -1;
615         }
616         return file_pos;
617 }
618
619 struct wtap_pkthdr*
620 wtap_phdr(wtap *wth)
621 {
622         return &wth->phdr;
623 }
624
625 union wtap_pseudo_header*
626 wtap_pseudoheader(wtap *wth)
627 {
628         return &wth->pseudo_header;
629 }
630
631 guint8*
632 wtap_buf_ptr(wtap *wth)
633 {
634         return buffer_start_ptr(wth->frame_buffer);
635 }
636
637 gboolean
638 wtap_seek_read(wtap *wth, gint64 seek_off,
639         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
640         int *err, gchar **err_info)
641 {
642         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
643                 err, err_info);
644 }