3 * $Id: wtap.c,v 1.61 2002/02/07 20:41: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" },
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_ATM_RFC1483 */
90 { "RFC 1483 ATM", "atm-rfc1483" },
92 /* WTAP_ENCAP_LINUX_ATM_CLIP */
93 { "Linux ATM CLIP", "linux-atm-clip" },
98 /* WTAP_ENCAP_ATM_SNIFFER */
99 { "ATM Sniffer", "atm-sniffer" },
101 /* WTAP_ENCAP_NULL */
104 /* WTAP_ENCAP_ASCEND */
105 { "Lucent/Ascend access equipment", "ascend" },
107 /* WTAP_ENCAP_LAPD */
110 /* WTAP_ENCAP_V120 */
113 /* WTAP_ENCAP_PPP_WITH_PHDR */
114 { "PPP with Directional Info", "ppp-with-direction" },
116 /* WTAP_ENCAP_IEEE_802_11 */
117 { "IEEE 802.11 Wireless LAN", "ieee-802-11" },
120 { "Linux cooked-mode capture", "linux-sll" },
122 /* WTAP_ENCAP_FRELAY */
123 { "Frame Relay", "frelay" },
125 /* WTAP_ENCAP_CHDLC */
126 { "Cisco HDLC", "chdlc" },
128 /* WTAP_ENCAP_CISCO_IOS */
129 { "Cisco IOS internal", "ios" },
131 /* WTAP_ENCAP_LOCALTALK */
132 { "Localtalk", "ltalk" },
134 /* WTAP_ENCAP_PRISM_HEADER */
135 { "IEEE 802.11 plus Prism II monitor mode header", "prism" },
137 /* WTAP_ENCAP_PFLOG */
138 { "OpenBSD PF Firewall logs", "pflog" },
140 /* WTAP_ENCAP_AIROPEEK */
141 { "IEEE 802.11 plus AiroPeek header", "airopeek" },
143 /* WTAP_ENCAP_HHDLC */
144 { "HiPath HDLC", "hhdlc" },
147 /* Name that should be somewhat descriptive. */
149 *wtap_encap_string(int encap)
151 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
154 return encap_table[encap].name;
157 /* Name to use in, say, a command-line flag specifying the type. */
159 *wtap_encap_short_string(int encap)
161 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
164 return encap_table[encap].short_name;
167 /* Translate a short name to a capture file type. */
169 wtap_short_string_to_encap(const char *short_name)
173 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
174 if (encap_table[encap].short_name != NULL &&
175 strcmp(short_name, encap_table[encap].short_name) == 0)
178 return -1; /* no such encapsulation type */
181 static const char *wtap_errlist[] = {
182 "The file isn't a plain file",
183 "The file isn't a capture file in a known format",
184 "File contains record data we don't support",
186 "Files can't be saved in that format",
187 "Files from that network type can't be saved in that format",
188 "That format doesn't support per-packet encapsulations",
191 "Less data was read than was expected",
192 "File contains a record that's not valid",
193 "Less data was written than was requested",
194 "Uncompression error: data oddly truncated",
195 "Uncompression error: data would overflow buffer",
196 "Uncompression error: bad LZ77 offset",
198 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
201 *wtap_strerror(int err)
203 static char errbuf[128];
204 unsigned int wtap_errlist_index;
208 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
209 /* Assume it's a zlib error. */
210 sprintf(errbuf, "Uncompression error: %s",
211 zError(err - WTAP_ERR_ZLIB));
215 wtap_errlist_index = -1 - err;
216 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
217 sprintf(errbuf, "Error %d", err);
220 if (wtap_errlist[wtap_errlist_index] == NULL)
221 return "Unknown reason";
222 return wtap_errlist[wtap_errlist_index];
224 return strerror(err);
227 /* Close only the sequential side, freeing up memory it uses.
229 Note that we do *not* want to call the subtype's close function,
230 as it would free any per-subtype data, and that data may be
231 needed by the random-access side.
233 Instead, if the subtype has a "sequential close" function, we call it,
234 to free up stuff used only by the sequential side. */
236 wtap_sequential_close(wtap *wth)
238 if (wth->subtype_sequential_close != NULL)
239 (*wth->subtype_sequential_close)(wth);
241 if (wth->fh != NULL) {
246 if (wth->frame_buffer) {
247 buffer_free(wth->frame_buffer);
248 g_free(wth->frame_buffer);
249 wth->frame_buffer = NULL;
254 wtap_close(wtap *wth)
256 wtap_sequential_close(wth);
258 if (wth->subtype_close != NULL)
259 (*wth->subtype_close)(wth);
261 if (wth->random_fh != NULL)
262 file_close(wth->random_fh);
268 wtap_read(wtap *wth, int *err, long *data_offset)
270 return wth->subtype_read(wth, err, data_offset);
279 union wtap_pseudo_header*
280 wtap_pseudoheader(wtap *wth)
282 return &wth->pseudo_header;
286 wtap_buf_ptr(wtap *wth)
288 return buffer_start_ptr(wth->frame_buffer);
292 wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user, int *err)
297 /* Start by clearing error flag */
300 while ( (wtap_read(wth, err, &data_offset)) ) {
301 callback(user, &wth->phdr, data_offset,
302 &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
303 if (count > 0 && ++loop >= count)
308 return TRUE; /* success */
310 return FALSE; /* failure */
314 wtap_seek_read(wtap *wth, long seek_off,
315 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
317 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len);