replace *a lot* of file related calls by their GLib counterparts. This is necessary...
[obnox/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 #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 static const struct encap_type_info {
88         const char *name;
89         const char *short_name;
90 } encap_table[WTAP_NUM_ENCAP_TYPES] = {
91         /* WTAP_ENCAP_UNKNOWN */
92         { "Unknown", NULL },
93
94         /* WTAP_ENCAP_ETHERNET */
95         { "Ethernet", "ether" },
96
97         /* WTAP_ENCAP_TOKEN_RING */
98         { "Token Ring", "tr" },
99
100         /* WTAP_ENCAP_SLIP */
101         { "SLIP", "slip" },
102
103         /* WTAP_ENCAP_PPP */
104         { "PPP", "ppp" },
105
106         /* WTAP_ENCAP_FDDI */
107         { "FDDI", "fddi" },
108
109         /* WTAP_ENCAP_FDDI_BITSWAPPED */
110         { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
111
112         /* WTAP_ENCAP_RAW_IP */
113         { "Raw IP", "rawip" },
114
115         /* WTAP_ENCAP_ARCNET */
116         { "ARCNET", "arcnet" },
117
118         /* WTAP_ENCAP_ARCNET_LINUX */
119         { "Linux ARCNET", "arcnet_linux" },
120
121         /* WTAP_ENCAP_ATM_RFC1483 */
122         { "RFC 1483 ATM", "atm-rfc1483" },
123
124         /* WTAP_ENCAP_LINUX_ATM_CLIP */
125         { "Linux ATM CLIP", "linux-atm-clip" },
126
127         /* WTAP_ENCAP_LAPB */
128         { "LAPB", "lapb" },
129
130         /* WTAP_ENCAP_ATM_PDUS */
131         { "ATM PDUs", "atm-pdus" },
132
133         /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
134         { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
135
136         /* WTAP_ENCAP_NULL */
137         { "NULL", "null" },
138
139         /* WTAP_ENCAP_ASCEND */
140         { "Lucent/Ascend access equipment", "ascend" },
141
142         /* WTAP_ENCAP_ISDN */
143         { "ISDN", "isdn" },
144
145         /* WTAP_ENCAP_IP_OVER_FC */
146         { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
147
148         /* WTAP_ENCAP_PPP_WITH_PHDR */
149         { "PPP with Directional Info", "ppp-with-direction" },
150
151         /* WTAP_ENCAP_IEEE_802_11 */
152         { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
153
154         /* WTAP_ENCAP_PRISM_HEADER */
155         { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
156
157         /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
158         { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
159
160         /* WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP */
161         { "IEEE 802.11 plus radiotap WLAN header", "ieee-802-11-radiotap" },
162
163         /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
164         { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
165
166         /* WTAP_ENCAP_SLL */
167         { "Linux cooked-mode capture", "linux-sll" },
168
169         /* WTAP_ENCAP_FRELAY */
170         { "Frame Relay", "frelay" },
171
172         /* WTAP_ENCAP_FRELAY_WITH_PHDR */
173         { "Frame Relay with Directional Info", "frelay-with-direction" },
174
175         /* WTAP_ENCAP_CHDLC */
176         { "Cisco HDLC", "chdlc" },
177
178         /* WTAP_ENCAP_CISCO_IOS */
179         { "Cisco IOS internal", "ios" },
180
181         /* WTAP_ENCAP_LOCALTALK */
182         { "Localtalk", "ltalk" },
183
184         /* WTAP_ENCAP_OLD_PFLOG  */
185         { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
186
187         /* WTAP_ENCAP_HHDLC */
188         { "HiPath HDLC", "hhdlc" },
189
190         /* WTAP_ENCAP_DOCSIS */
191         { "Data Over Cable Service Interface Specification", "docsis" },
192
193         /* WTAP_ENCAP_COSINE */
194         { "CoSine L2 debug log", "cosine" },
195
196         /* WTAP_ENCAP_WFLEET_HDLC */
197         { "Wellfleet HDLC", "whdlc" },
198
199         /* WTAP_ENCAP_SDLC */
200         { "SDLC", "sdlc" },
201
202         /* WTAP_ENCAP_TZSP */
203         { "Tazmen sniffer protocol", "tzsp" },
204
205         /* WTAP_ENCAP_ENC */
206         { "OpenBSD enc(4) encapsulating interface", "enc" },
207
208         /* WTAP_ENCAP_PFLOG  */
209         { "OpenBSD PF Firewall logs", "pflog" },
210
211         /* WTAP_ENCAP_CHDLC_WITH_PHDR */
212         { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
213
214         /* WTAP_ENCAP_BLUETOOTH_H4 */
215         { "Bluetooth H4", "bluetooth-h4" },
216
217         /* WTAP_ENCAP_MTP2 */
218         { "SS7 MTP2", "mtp2" },
219
220         /* WTAP_ENCAP_MTP3 */
221         { "SS7 MTP3", "mtp3" },
222
223         /* WTAP_ENCAP_IRDA */
224         { "IrDA", "irda" },
225
226         /* WTAP_ENCAP_USER0 */
227         { "USER 0", "user0" },
228
229         /* WTAP_ENCAP_USER1 */
230         { "USER 1", "user1" },
231
232         /* WTAP_ENCAP_USER2 */
233         { "USER 2", "user2" },
234
235         /* WTAP_ENCAP_USER3 */
236         { "USER 3", "user3" },
237
238         /* WTAP_ENCAP_USER4 */
239         { "USER 4", "user4" },
240
241         /* WTAP_ENCAP_USER5 */
242         { "USER 5", "user5" },
243
244         /* WTAP_ENCAP_USER6 */
245         { "USER 6", "user6" },
246
247         /* WTAP_ENCAP_USER7 */
248         { "USER 7", "user7" },
249
250         /* WTAP_ENCAP_USER8 */
251         { "USER 8", "user8" },
252
253         /* WTAP_ENCAP_USER9 */
254         { "USER 9", "user9" },
255
256         /* WTAP_ENCAP_USER10 */
257         { "USER 10", "user10" },
258
259         /* WTAP_ENCAP_USER11 */
260         { "USER 11", "user11" },
261
262         /* WTAP_ENCAP_USER12 */
263         { "USER 12", "user12" },
264
265         /* WTAP_ENCAP_USER13 */
266         { "USER 13", "user13" },
267
268         /* WTAP_ENCAP_USER14 */
269         { "USER 14", "user14" },
270
271         /* WTAP_ENCAP_USER15 */
272         { "USER 15", "user15" },
273
274         /* WTAP_ENCAP_SYMANTEC */
275         { "Symantec Enterprise Firewall", "symantec" },
276
277         /* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
278         { "Apple IP-over-IEEE 1394", "ap1394" },
279
280         /* WTAP_ENCAP_BACNET_MS_TP */
281         { "BACnet MS/TP", "bacnet-ms-tp" },
282
283         /* WTAP_ENCAP_NETTL_RAW_ICMP */
284         { "Raw ICMP with nettl headers", "raw-icmp-nettl" },
285
286         /* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
287         { "Raw ICMPv6 with nettl headers", "raw-icmpv6-nettl" },
288
289         /* WTAP_ENCAP_GPRS_LLC */
290         { "GPRS LLC", "gprs-llc" },
291
292         /* WTAP_ENCAP_JUNIPER_ATM1 */
293         { "Juniper ATM1", "juniper-atm1" },
294
295         /* WTAP_ENCAP_JUNIPER_ATM2 */
296         { "Juniper ATM2", "juniper-atm2" },
297
298         /* WTAP_ENCAP_REDBACK */
299         { "Redback SmartEdge", "redback" },
300
301         /* WTAP_ENCAP_NETTL_RAW_IP */
302         { "Raw IP with nettl headers", "rawip-nettl" },
303
304         /* WTAP_ENCAP_NETTL_ETHERNET */
305         { "Ethernet with nettl headers", "ether-nettl" },
306
307         /* WTAP_ENCAP_NETTL_TOKEN_RING */
308         { "Token Ring with nettl headers", "tr-nettl" },
309
310         /* WTAP_ENCAP_NETTL_FDDI */
311         { "FDDI with nettl headers", "fddi-nettl" },
312
313         /* WTAP_ENCAP_NETTL_UNKNOWN */
314         { "Unknown link-layer type with nettl headers", "unknown-nettl" },
315
316         /* WTAP_ENCAP_MTP2_WITH_PHDR */
317         { "MTP2 with pseudoheader", "mtp2-with-phdr" },
318
319         /* WTAP_ENCAP_JUNIPER_PPPOE */
320         { "Juniper PPPoE", "juniper-pppoe" },
321
322         /* WTAP_ENCAP_GCOM_TIE1 */
323         { "GCOM TIE1", "gcom-tie1" },
324
325         /* WTAP_ENCAP_GCOM_SERIAL */
326         { "GCOM Serial", "gcom-serial" },
327
328         /* WTAP_ENCAP_NETTL_X25 */
329         { "X25 with nettl headers", "x25-nettl" },
330
331         /* WTAP_ENCAP_JUNIPER_MLPPP */
332         { "Juniper MLPPP", "juniper-mlppp" },
333
334         /* WTAP_ENCAP_JUNIPER_MLFR */
335         { "Juniper MLFR", "juniper-mlfr" },
336
337         /* WTAP_ENCAP_JUNIPER_ETHER */
338         { "Juniper Ethernet", "juniper-ether" },
339
340         /* WTAP_ENCAP_JUNIPER_PPP */
341         { "Juniper PPP", "juniper-ppp" },
342
343         /* WTAP_ENCAP_JUNIPER_FRELAY */
344         { "Juniper Frame-Relay", "juniper-frelay" },
345
346         /* WTAP_ENCAP_JUNIPER_CHDLC */
347         { "Juniper C-HDLC", "juniper-chdlc" },
348 };
349
350 /* Name that should be somewhat descriptive. */
351 const char
352 *wtap_encap_string(int encap)
353 {
354         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
355                 return NULL;
356         else
357                 return encap_table[encap].name;
358 }
359
360 /* Name to use in, say, a command-line flag specifying the type. */
361 const char
362 *wtap_encap_short_string(int encap)
363 {
364         if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
365                 return NULL;
366         else
367                 return encap_table[encap].short_name;
368 }
369
370 /* Translate a short name to a capture file type. */
371 int
372 wtap_short_string_to_encap(const char *short_name)
373 {
374         int encap;
375
376         for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
377                 if (encap_table[encap].short_name != NULL &&
378                     strcmp(short_name, encap_table[encap].short_name) == 0)
379                         return encap;
380         }
381         return -1;      /* no such encapsulation type */
382 }
383
384 static const char *wtap_errlist[] = {
385         "The file isn't a plain file or pipe",
386         "The file is being opened for random access but is a pipe",
387         "The file isn't a capture file in a known format",
388         "File contains record data we don't support",
389         "That file format cannot be written to a pipe",
390         NULL,
391         "Files can't be saved in that format",
392         "Files from that network type can't be saved in that format",
393         "That file format doesn't support per-packet encapsulations",
394         NULL,
395         NULL,
396         "Less data was read than was expected",
397         "File contains a record that's not valid",
398         "Less data was written than was requested",
399         "Uncompression error: data oddly truncated",
400         "Uncompression error: data would overflow buffer",
401         "Uncompression error: bad LZ77 offset",
402         "The standard input cannot be opened for random access",
403 };
404 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
405
406 const char
407 *wtap_strerror(int err)
408 {
409         static char errbuf[128];
410         unsigned int wtap_errlist_index;
411
412         if (err < 0) {
413 #ifdef HAVE_LIBZ
414                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
415                         /* Assume it's a zlib error. */
416                         sprintf(errbuf, "Uncompression error: %s",
417                             zError(err - WTAP_ERR_ZLIB));
418                         return errbuf;
419                 }
420 #endif
421                 wtap_errlist_index = -1 - err;
422                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
423                         sprintf(errbuf, "Error %d", err);
424                         return errbuf;
425                 }
426                 if (wtap_errlist[wtap_errlist_index] == NULL)
427                         return "Unknown reason";
428                 return wtap_errlist[wtap_errlist_index];
429         } else
430                 return strerror(err);
431 }
432
433 /* Close only the sequential side, freeing up memory it uses.
434
435    Note that we do *not* want to call the subtype's close function,
436    as it would free any per-subtype data, and that data may be
437    needed by the random-access side.
438
439    Instead, if the subtype has a "sequential close" function, we call it,
440    to free up stuff used only by the sequential side. */
441 void
442 wtap_sequential_close(wtap *wth)
443 {
444         if (wth->subtype_sequential_close != NULL)
445                 (*wth->subtype_sequential_close)(wth);
446
447         if (wth->fh != NULL) {
448                 file_close(wth->fh);
449                 wth->fh = NULL;
450         }
451
452         if (wth->frame_buffer) {
453                 buffer_free(wth->frame_buffer);
454                 g_free(wth->frame_buffer);
455                 wth->frame_buffer = NULL;
456         }
457 }
458
459 void
460 wtap_close(wtap *wth)
461 {
462         wtap_sequential_close(wth);
463
464         if (wth->subtype_close != NULL)
465                 (*wth->subtype_close)(wth);
466
467         if (wth->random_fh != NULL)
468                 file_close(wth->random_fh);
469
470         g_free(wth);
471 }
472
473 gboolean
474 wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
475 {
476         /*
477          * Set the packet encapsulation to the file's encapsulation
478          * value; if that's not WTAP_ENCAP_PER_PACKET, it's the
479          * right answer (and means that the read routine for this
480          * capture file type doesn't have to set it), and if it
481          * *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
482          * anyway.
483          */
484         wth->phdr.pkt_encap = wth->file_encap;
485
486         if (!wth->subtype_read(wth, err, err_info, data_offset))
487                 return FALSE;   /* failure */
488
489         /*
490          * It makes no sense for the captured data length to be bigger
491          * than the actual data length.
492          */
493         if (wth->phdr.caplen > wth->phdr.len)
494                 wth->phdr.caplen = wth->phdr.len;
495
496         /*
497          * Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
498          * probably means the file has that encapsulation type
499          * but the read routine didn't set this packet's
500          * encapsulation type.
501          */
502         g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
503
504         return TRUE;    /* success */
505 }
506
507 /*
508  * Return an approximation of the amount of data we've read sequentially
509  * from the file so far.  (gint64, in case that's 64 bits.)
510  */
511 gint64
512 wtap_read_so_far(wtap *wth, int *err)
513 {
514         off_t file_pos;
515
516         file_pos = eth_lseek(wth->fd, 0, SEEK_CUR);
517         if (file_pos == -1) {
518                 if (err != NULL)
519                         *err = errno;
520                 return -1;
521         }
522         return file_pos;
523 }
524
525 struct wtap_pkthdr*
526 wtap_phdr(wtap *wth)
527 {
528         return &wth->phdr;
529 }
530
531 union wtap_pseudo_header*
532 wtap_pseudoheader(wtap *wth)
533 {
534         return &wth->pseudo_header;
535 }
536
537 guint8*
538 wtap_buf_ptr(wtap *wth)
539 {
540         return buffer_start_ptr(wth->frame_buffer);
541 }
542
543 gboolean
544 wtap_seek_read(wtap *wth, long seek_off,
545         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
546         int *err, gchar **err_info)
547 {
548         return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
549                 err, err_info);
550 }