3 * $Id: wtap.c,v 1.89 2004/01/29 10:58:28 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
30 #include "file_wrappers.h"
40 wtap_file_type(wtap *wth)
42 return wth->file_type;
46 wtap_snapshot_length(wtap *wth)
48 return wth->snapshot_length;
52 wtap_file_encap(wtap *wth)
54 return wth->file_encap;
57 /* Table of the encapsulation types we know about. */
58 static const struct encap_type_info {
60 const char *short_name;
61 } encap_table[WTAP_NUM_ENCAP_TYPES] = {
62 /* WTAP_ENCAP_UNKNOWN */
65 /* WTAP_ENCAP_ETHERNET */
66 { "Ethernet", "ether" },
68 /* WTAP_ENCAP_TOKEN_RING */
69 { "Token Ring", "tr" },
80 /* WTAP_ENCAP_FDDI_BITSWAPPED */
81 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
83 /* WTAP_ENCAP_RAW_IP */
84 { "Raw IP", "rawip" },
86 /* WTAP_ENCAP_ARCNET */
87 { "ARCNET", "arcnet" },
89 /* WTAP_ENCAP_ARCNET_LINUX */
90 { "Linux ARCNET", "arcnet_linux" },
92 /* WTAP_ENCAP_ATM_RFC1483 */
93 { "RFC 1483 ATM", "atm-rfc1483" },
95 /* WTAP_ENCAP_LINUX_ATM_CLIP */
96 { "Linux ATM CLIP", "linux-atm-clip" },
101 /* WTAP_ENCAP_ATM_PDUS */
102 { "ATM PDUs", "atm-pdus" },
104 /* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
105 { "ATM PDUs - untruncated", "atm-pdus-untruncated" },
107 /* WTAP_ENCAP_NULL */
110 /* WTAP_ENCAP_ASCEND */
111 { "Lucent/Ascend access equipment", "ascend" },
113 /* WTAP_ENCAP_ISDN */
116 /* WTAP_ENCAP_IP_OVER_FC */
117 { "RFC 2625 IP-over-Fibre Channel", "ip-over-fc" },
119 /* WTAP_ENCAP_PPP_WITH_PHDR */
120 { "PPP with Directional Info", "ppp-with-direction" },
122 /* WTAP_ENCAP_IEEE_802_11 */
123 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
125 /* WTAP_ENCAP_PRISM_HEADER */
126 { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
128 /* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
129 { "IEEE 802.11 Wireless LAN with radio information", "ieee-802-11-radio" },
131 /* WTAP_ENCAP_IEEE_802_11_WLAN_BSD */
132 { "IEEE 802.11 plus BSD WLAN header", "ieee-802-11-bsd" },
134 /* WTAP_ENCAP_IEEE_802_11_WLAN_AVS */
135 { "IEEE 802.11 plus AVS WLAN header", "ieee-802-11-avs" },
138 { "Linux cooked-mode capture", "linux-sll" },
140 /* WTAP_ENCAP_FRELAY */
141 { "Frame Relay", "frelay" },
143 /* WTAP_ENCAP_FRELAY_WITH_PHDR */
144 { "Frame Relay with Directional Info", "frelay-with-direction" },
146 /* WTAP_ENCAP_CHDLC */
147 { "Cisco HDLC", "chdlc" },
149 /* WTAP_ENCAP_CISCO_IOS */
150 { "Cisco IOS internal", "ios" },
152 /* WTAP_ENCAP_LOCALTALK */
153 { "Localtalk", "ltalk" },
155 /* WTAP_ENCAP_OLD_PFLOG */
156 { "OpenBSD PF Firewall logs, pre-3.4", "pflog-old" },
158 /* WTAP_ENCAP_HHDLC */
159 { "HiPath HDLC", "hhdlc" },
161 /* WTAP_ENCAP_DOCSIS */
162 { "Data Over Cable Service Interface Specification", "docsis" },
164 /* WTAP_ENCAP_COSINE */
165 { "CoSine L2 debug log", "cosine" },
167 /* WTAP_ENCAP_WFLEET_HDLC */
168 { "Wellfleet HDLC", "whdlc" },
170 /* WTAP_ENCAP_SDLC */
173 /* WTAP_ENCAP_TZSP */
174 { "Tazmen sniffer protocol", "tzsp" },
177 { "OpenBSD enc(4) encapsulating interface", "enc" },
179 /* WTAP_ENCAP_PFLOG */
180 { "OpenBSD PF Firewall logs", "pflog" },
182 /* WTAP_ENCAP_CHDLC_WITH_PHDR */
183 { "Cisco HDLC with Directional Info", "chdlc-with-direction" },
185 /* WTAP_ENCAP_BLUETOOTH_H4 */
186 { "Bluetooth H4", "bluetooth-h4" },
188 /* WTAP_ENCAP_MTP2 */
189 { "SS7 MTP2", "mtp2" },
191 /* WTAP_ENCAP_MTP3 */
192 { "SS7 MTP3", "mtp3" },
194 /* WTAP_ENCAP_IRDA */
197 /* WTAP_ENCAP_USER0 */
198 { "USER 0", "user0" },
200 /* WTAP_ENCAP_USER1 */
201 { "USER 1", "user1" },
203 /* WTAP_ENCAP_USER2 */
204 { "USER 2", "user2" },
206 /* WTAP_ENCAP_USER3 */
207 { "USER 3", "user3" },
209 /* WTAP_ENCAP_USER4 */
210 { "USER 4", "user4" },
212 /* WTAP_ENCAP_USER5 */
213 { "USER 5", "user5" },
215 /* WTAP_ENCAP_USER6 */
216 { "USER 6", "user6" },
218 /* WTAP_ENCAP_USER7 */
219 { "USER 7", "user7" },
221 /* WTAP_ENCAP_USER8 */
222 { "USER 8", "user8" },
224 /* WTAP_ENCAP_USER9 */
225 { "USER 9", "user9" },
227 /* WTAP_ENCAP_USER10 */
228 { "USER 10", "user10" },
230 /* WTAP_ENCAP_USER11 */
231 { "USER 11", "user11" },
233 /* WTAP_ENCAP_USER12 */
234 { "USER 12", "user12" },
236 /* WTAP_ENCAP_USER13 */
237 { "USER 13", "user13" },
239 /* WTAP_ENCAP_USER14 */
240 { "USER 14", "user14" },
242 /* WTAP_ENCAP_USER15 */
243 { "USER 15", "user15" },
246 /* Name that should be somewhat descriptive. */
248 *wtap_encap_string(int encap)
250 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
253 return encap_table[encap].name;
256 /* Name to use in, say, a command-line flag specifying the type. */
258 *wtap_encap_short_string(int encap)
260 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
263 return encap_table[encap].short_name;
266 /* Translate a short name to a capture file type. */
268 wtap_short_string_to_encap(const char *short_name)
272 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
273 if (encap_table[encap].short_name != NULL &&
274 strcmp(short_name, encap_table[encap].short_name) == 0)
277 return -1; /* no such encapsulation type */
280 static const char *wtap_errlist[] = {
281 "The file isn't a plain file or pipe",
282 "The file is being opened for random access but is a pipe",
283 "The file isn't a capture file in a known format",
284 "File contains record data we don't support",
285 "That file format cannot be written to a pipe",
287 "Files can't be saved in that format",
288 "Files from that network type can't be saved in that format",
289 "That file format doesn't support per-packet encapsulations",
292 "Less data was read than was expected",
293 "File contains a record that's not valid",
294 "Less data was written than was requested",
295 "Uncompression error: data oddly truncated",
296 "Uncompression error: data would overflow buffer",
297 "Uncompression error: bad LZ77 offset",
299 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
302 *wtap_strerror(int err)
304 static char errbuf[128];
305 unsigned int wtap_errlist_index;
309 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
310 /* Assume it's a zlib error. */
311 sprintf(errbuf, "Uncompression error: %s",
312 zError(err - WTAP_ERR_ZLIB));
316 wtap_errlist_index = -1 - err;
317 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
318 sprintf(errbuf, "Error %d", err);
321 if (wtap_errlist[wtap_errlist_index] == NULL)
322 return "Unknown reason";
323 return wtap_errlist[wtap_errlist_index];
325 return strerror(err);
328 /* Close only the sequential side, freeing up memory it uses.
330 Note that we do *not* want to call the subtype's close function,
331 as it would free any per-subtype data, and that data may be
332 needed by the random-access side.
334 Instead, if the subtype has a "sequential close" function, we call it,
335 to free up stuff used only by the sequential side. */
337 wtap_sequential_close(wtap *wth)
339 if (wth->subtype_sequential_close != NULL)
340 (*wth->subtype_sequential_close)(wth);
342 if (wth->fh != NULL) {
347 if (wth->frame_buffer) {
348 buffer_free(wth->frame_buffer);
349 g_free(wth->frame_buffer);
350 wth->frame_buffer = NULL;
355 wtap_close(wtap *wth)
357 wtap_sequential_close(wth);
359 if (wth->subtype_close != NULL)
360 (*wth->subtype_close)(wth);
362 if (wth->random_fh != NULL)
363 file_close(wth->random_fh);
369 wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
371 if (!wth->subtype_read(wth, err, err_info, data_offset))
372 return FALSE; /* failure */
375 * It makes no sense for the captured data length to be bigger
376 * than the actual data length.
378 if (wth->phdr.caplen > wth->phdr.len)
379 wth->phdr.caplen = wth->phdr.len;
380 return TRUE; /* success */
389 union wtap_pseudo_header*
390 wtap_pseudoheader(wtap *wth)
392 return &wth->pseudo_header;
396 wtap_buf_ptr(wtap *wth)
398 return buffer_start_ptr(wth->frame_buffer);
402 wtap_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
408 /* Start by clearing error flag */
411 while ( (wtap_read(wth, err, err_info, &data_offset)) ) {
412 callback(user, &wth->phdr, data_offset,
413 &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
414 if (count > 0 && ++loop >= count)
419 return TRUE; /* success */
421 return FALSE; /* failure */
425 wtap_seek_read(wtap *wth, long seek_off,
426 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
427 int *err, gchar **err_info)
429 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,