3 * $Id: wtap.c,v 1.53 2001/07/05 00:34:42 guy Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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 const static 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" },
129 /* Name that should be somewhat descriptive. */
131 *wtap_encap_string(int encap)
133 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
136 return encap_table[encap].name;
139 /* Name to use in, say, a command-line flag specifying the type. */
141 *wtap_encap_short_string(int encap)
143 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
146 return encap_table[encap].short_name;
149 /* Translate a short name to a capture file type. */
151 wtap_short_string_to_encap(const char *short_name)
155 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
156 if (encap_table[encap].short_name != NULL &&
157 strcmp(short_name, encap_table[encap].short_name) == 0)
160 return -1; /* no such encapsulation type */
163 static const char *wtap_errlist[] = {
164 "The file isn't a plain file",
165 "The file isn't a capture file in a known format",
166 "File contains record data we don't support",
168 "Files can't be saved in that format",
169 "Files from that network type can't be saved in that format",
170 "That format doesn't support per-packet encapsulations",
173 "Less data was read than was expected",
174 "File contains a record that's not valid",
175 "Less data was written than was requested",
176 "Uncompression error: data oddly truncated",
177 "Uncompression error: data would overflow buffer",
178 "Uncompression error: bad LZ77 offset",
180 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
183 *wtap_strerror(int err)
185 static char errbuf[128];
186 int wtap_errlist_index;
190 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
191 /* Assume it's a zlib error. */
192 sprintf(errbuf, "Uncompression error: %s",
193 zError(err - WTAP_ERR_ZLIB));
197 wtap_errlist_index = -1 - err;
198 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
199 sprintf(errbuf, "Error %d", err);
202 if (wtap_errlist[wtap_errlist_index] == NULL)
203 return "Unknown reason";
204 return wtap_errlist[wtap_errlist_index];
206 return strerror(err);
209 /* Close only the sequential side, freeing up memory it uses.
211 Note that we do *not* want to call the subtype's close function,
212 as it would free any per-subtype data, and that data may be
213 needed by the random-access side.
215 Instead, if the subtype has a "sequential close" function, we call it,
216 to free up stuff used only by the sequential side. */
218 wtap_sequential_close(wtap *wth)
220 if (wth->subtype_sequential_close != NULL)
221 (*wth->subtype_sequential_close)(wth);
223 if (wth->fh != NULL) {
228 if (wth->frame_buffer) {
229 buffer_free(wth->frame_buffer);
230 g_free(wth->frame_buffer);
231 wth->frame_buffer = NULL;
236 wtap_close(wtap *wth)
238 wtap_sequential_close(wth);
240 if (wth->subtype_close != NULL)
241 (*wth->subtype_close)(wth);
243 if (wth->random_fh != NULL)
244 file_close(wth->random_fh);
250 wtap_read(wtap *wth, int *err, int *data_offset)
252 return wth->subtype_read(wth, err, data_offset);
261 union wtap_pseudo_header*
262 wtap_pseudoheader(wtap *wth)
264 return &wth->pseudo_header;
268 wtap_buf_ptr(wtap *wth)
270 return buffer_start_ptr(wth->frame_buffer);
274 wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user, int *err)
276 int data_offset, loop = 0;
278 /* Start by clearing error flag */
281 while ( (wtap_read(wth, err, &data_offset)) ) {
282 callback(user, &wth->phdr, data_offset,
283 &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
284 if (count > 0 && ++loop >= count)
289 return TRUE; /* success */
291 return FALSE; /* failure */
295 wtap_seek_read(wtap *wth, int seek_off,
296 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
298 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len);