3 * $Id: wtap.c,v 1.91 2004/03/11 09:18:33 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" },
245 /* WTAP_ENCAP_SYMANTEC */
246 { "Symantec Enterprise Firewall", "symantec" },
249 /* Name that should be somewhat descriptive. */
251 *wtap_encap_string(int encap)
253 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
256 return encap_table[encap].name;
259 /* Name to use in, say, a command-line flag specifying the type. */
261 *wtap_encap_short_string(int encap)
263 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
266 return encap_table[encap].short_name;
269 /* Translate a short name to a capture file type. */
271 wtap_short_string_to_encap(const char *short_name)
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)
280 return -1; /* no such encapsulation type */
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",
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",
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",
302 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
305 *wtap_strerror(int err)
307 static char errbuf[128];
308 unsigned int wtap_errlist_index;
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));
319 wtap_errlist_index = -1 - err;
320 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
321 sprintf(errbuf, "Error %d", err);
324 if (wtap_errlist[wtap_errlist_index] == NULL)
325 return "Unknown reason";
326 return wtap_errlist[wtap_errlist_index];
328 return strerror(err);
331 /* Close only the sequential side, freeing up memory it uses.
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.
337 Instead, if the subtype has a "sequential close" function, we call it,
338 to free up stuff used only by the sequential side. */
340 wtap_sequential_close(wtap *wth)
342 if (wth->subtype_sequential_close != NULL)
343 (*wth->subtype_sequential_close)(wth);
345 if (wth->fh != NULL) {
350 if (wth->frame_buffer) {
351 buffer_free(wth->frame_buffer);
352 g_free(wth->frame_buffer);
353 wth->frame_buffer = NULL;
358 wtap_close(wtap *wth)
360 wtap_sequential_close(wth);
362 if (wth->subtype_close != NULL)
363 (*wth->subtype_close)(wth);
365 if (wth->random_fh != NULL)
366 file_close(wth->random_fh);
372 wtap_read(wtap *wth, int *err, gchar **err_info, long *data_offset)
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
382 wth->phdr.pkt_encap = wth->file_encap;
384 if (!wth->subtype_read(wth, err, err_info, data_offset))
385 return FALSE; /* failure */
388 * It makes no sense for the captured data length to be bigger
389 * than the actual data length.
391 if (wth->phdr.caplen > wth->phdr.len)
392 wth->phdr.caplen = wth->phdr.len;
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.
400 g_assert(wth->phdr.pkt_encap != WTAP_ENCAP_PER_PACKET);
402 return TRUE; /* success */
411 union wtap_pseudo_header*
412 wtap_pseudoheader(wtap *wth)
414 return &wth->pseudo_header;
418 wtap_buf_ptr(wtap *wth)
420 return buffer_start_ptr(wth->frame_buffer);
424 wtap_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
430 /* Start by clearing error flag */
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)
441 return TRUE; /* success */
443 return FALSE; /* failure */
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)
451 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,