3 * $Id: wtap.c,v 1.51 2001/01/08 22:18:22 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"
46 wtap_file_type(wtap *wth)
48 return wth->file_type;
52 wtap_snapshot_length(wtap *wth)
54 return wth->snapshot_length;
58 wtap_file_encap(wtap *wth)
60 return wth->file_encap;
63 /* Table of the encapsulation types we know about. */
64 const static struct encap_type_info {
66 const char *short_name;
67 } encap_table[WTAP_NUM_ENCAP_TYPES] = {
68 /* WTAP_ENCAP_UNKNOWN */
71 /* WTAP_ENCAP_ETHERNET */
72 { "Ethernet", "ether" },
75 { "Token Ring", "tr" },
86 /* WTAP_ENCAP_FDDI_BITSWAPPED */
87 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
89 /* WTAP_ENCAP_RAW_IP */
90 { "Raw IP", "rawip" },
92 /* WTAP_ENCAP_ARCNET */
93 { "ARCNET", "arcnet" },
95 /* WTAP_ENCAP_ATM_RFC1483 */
96 { "RFC 1483 ATM", "atm-rfc1483" },
98 /* WTAP_ENCAP_LINUX_ATM_CLIP */
99 { "Linux ATM CLIP", "linux-atm-clip" },
101 /* WTAP_ENCAP_LAPB */
104 /* WTAP_ENCAP_ATM_SNIFFER */
105 { "ATM Sniffer", "atm-sniffer" },
107 /* WTAP_ENCAP_NULL */
110 /* WTAP_ENCAP_ASCEND */
111 { "Lucent/Ascend access equipment", "ascend" },
113 /* WTAP_ENCAP_LAPD */
116 /* WTAP_ENCAP_V120 */
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" },
126 { "Linux cooked-mode capture", "linux-sll" },
128 /* WTAP_ENCAP_FRELAY */
129 { "Frame Relay", "frelay" },
132 /* Name that should be somewhat descriptive. */
134 *wtap_encap_string(int encap)
136 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
139 return encap_table[encap].name;
142 /* Name to use in, say, a command-line flag specifying the type. */
144 *wtap_encap_short_string(int encap)
146 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
149 return encap_table[encap].short_name;
152 /* Translate a short name to a capture file type. */
154 wtap_short_string_to_encap(const char *short_name)
158 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
159 if (encap_table[encap].short_name != NULL &&
160 strcmp(short_name, encap_table[encap].short_name) == 0)
163 return -1; /* no such encapsulation type */
166 static const char *wtap_errlist[] = {
167 "The file isn't a plain file",
168 "The file isn't a capture file in a known format",
169 "File contains record data we don't support",
171 "Files can't be saved in that format",
172 "Files from that network type can't be saved in that format",
173 "That format doesn't support per-packet encapsulations",
176 "Less data was read than was expected",
177 "File contains a record that's not valid",
178 "Less data was written than was requested",
179 "Uncompression error: data oddly truncated",
180 "Uncompression error: data would overflow buffer",
181 "Uncompression error: bad LZ77 offset",
183 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
186 *wtap_strerror(int err)
188 static char errbuf[128];
189 int wtap_errlist_index;
193 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
194 /* Assume it's a zlib error. */
195 sprintf(errbuf, "Uncompression error: %s",
196 zError(err - WTAP_ERR_ZLIB));
200 wtap_errlist_index = -1 - err;
201 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
202 sprintf(errbuf, "Error %d", err);
205 if (wtap_errlist[wtap_errlist_index] == NULL)
206 return "Unknown reason";
207 return wtap_errlist[wtap_errlist_index];
209 return strerror(err);
212 /* Close only the sequential side, freeing up memory it uses.
214 Note that we do *not* want to call the subtype's close function,
215 as it would free any per-subtype data, and that data may be
216 needed by the random-access side.
218 Instead, if the subtype has a "sequential close" function, we call it,
219 to free up stuff used only by the sequential side. */
221 wtap_sequential_close(wtap *wth)
223 if (wth->subtype_sequential_close != NULL)
224 (*wth->subtype_sequential_close)(wth);
226 if (wth->fh != NULL) {
231 if (wth->frame_buffer) {
232 buffer_free(wth->frame_buffer);
233 g_free(wth->frame_buffer);
234 wth->frame_buffer = NULL;
239 wtap_close(wtap *wth)
241 wtap_sequential_close(wth);
243 if (wth->subtype_close != NULL)
244 (*wth->subtype_close)(wth);
246 if (wth->random_fh != NULL)
247 file_close(wth->random_fh);
253 wtap_read(wtap *wth, int *err, int *data_offset)
255 return wth->subtype_read(wth, err, data_offset);
264 union wtap_pseudo_header*
265 wtap_pseudoheader(wtap *wth)
267 return &wth->pseudo_header;
271 wtap_buf_ptr(wtap *wth)
273 return buffer_start_ptr(wth->frame_buffer);
277 wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user, int *err)
279 int data_offset, loop = 0;
281 /* Start by clearing error flag */
284 while ( (wtap_read(wth, err, &data_offset)) ) {
285 callback(user, &wth->phdr, data_offset,
286 &wth->pseudo_header, buffer_start_ptr(wth->frame_buffer));
287 if (count > 0 && ++loop >= count)
292 return TRUE; /* success */
294 return FALSE; /* failure */
298 wtap_seek_read(wtap *wth, int seek_off,
299 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len)
301 return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len);