3 * $Id: wtap.c,v 1.37 2000/01/22 06:22:44 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"
35 FILE* wtap_file(wtap *wth)
40 int wtap_fd(wtap *wth)
45 int wtap_file_type(wtap *wth)
47 return wth->file_type;
50 int wtap_snapshot_length(wtap *wth)
52 return wth->snapshot_length;
55 int wtap_file_encap(wtap *wth)
57 return wth->file_encap;
60 /* Table of the encapsulation types we know about. */
61 const static struct encap_type_info {
63 const char *short_name;
64 } encap_table[WTAP_NUM_ENCAP_TYPES] = {
65 /* WTAP_ENCAP_UNKNOWN */
68 /* WTAP_ENCAP_ETHERNET */
69 { "Ethernet", "ether" },
72 { "Token Ring", "tr" },
83 /* WTAP_ENCAP_FDDI_BITSWAPPED */
84 { "FDDI with bit-swapped MAC addresses", "fddi-swapped" },
86 /* WTAP_ENCAP_RAW_IP */
87 { "Raw IP", "rawip" },
89 /* WTAP_ENCAP_ARCNET */
90 { "ARCNET", "arcnet" },
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_SNIFFER */
102 { "ATM Sniffer", "atm-sniffer" },
104 /* WTAP_ENCAP_NULL */
107 /* WTAP_ENCAP_ASCEND */
108 { "Lucent/Ascend access equipment", "ascend" },
110 /* WTAP_ENCAP_LAPD */
113 /* WTAP_ENCAP_V120 */
117 /* Name that should be somewhat descriptive. */
118 const char *wtap_encap_string(int encap)
120 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
123 return encap_table[encap].name;
126 /* Name to use in, say, a command-line flag specifying the type. */
127 const char *wtap_encap_short_string(int encap)
129 if (encap < 0 || encap >= WTAP_NUM_ENCAP_TYPES)
132 return encap_table[encap].short_name;
135 /* Translate a short name to a capture file type. */
136 int wtap_short_string_to_encap(const char *short_name)
140 for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
141 if (encap_table[encap].short_name != NULL &&
142 strcmp(short_name, encap_table[encap].short_name) == 0)
145 return -1; /* no such encapsulation type */
148 static const char *wtap_errlist[] = {
149 "The file isn't a plain file",
150 "The file isn't a capture file in a known format",
151 "File contains record data we don't support",
153 "Files can't be saved in that format",
154 "Files from that network type can't be saved in that format",
155 "That format doesn't support per-packet encapsulations",
158 "Less data was read than was expected",
159 "File contains a record that's not valid",
160 "Less data was written than was requested"
162 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
164 const char *wtap_strerror(int err)
166 static char errbuf[6+11+1]; /* "Error %d" */
167 int wtap_errlist_index;
171 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
172 /* Assume it's a zlib error. */
173 sprintf(errbuf, "Uncompression error: %s",
174 zError(err - WTAP_ERR_ZLIB));
178 wtap_errlist_index = -1 - err;
179 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
180 sprintf(errbuf, "Error %d", err);
183 if (wtap_errlist[wtap_errlist_index] == NULL)
184 return "Unknown reason";
185 return wtap_errlist[wtap_errlist_index];
187 return strerror(err);
190 void wtap_close(wtap *wth)
192 /* free up memory. If any capture structure ever allocates
193 * its own memory, it would be better to make a *close() function
194 * for each filetype, like pcap_close(0, lanalyzer_close(), etc.
195 * But for now this will work. */
196 switch(wth->file_type) {
198 case WTAP_FILE_PCAP_MODIFIED:
199 g_free(wth->capture.pcap);
202 case WTAP_FILE_LANALYZER:
203 g_free(wth->capture.lanalyzer);
206 case WTAP_FILE_NGSNIFFER:
207 g_free(wth->capture.ngsniffer);
210 case WTAP_FILE_RADCOM:
211 g_free(wth->capture.radcom);
214 case WTAP_FILE_NETMON_1_x:
215 case WTAP_FILE_NETMON_2_x:
216 g_free(wth->capture.netmon);
219 case WTAP_FILE_NETXRAY_1_0:
220 case WTAP_FILE_NETXRAY_1_1:
221 case WTAP_FILE_NETXRAY_2_001:
222 g_free(wth->capture.netxray);
225 case WTAP_FILE_ASCEND:
226 g_free(wth->capture.ascend);
229 case WTAP_FILE_NETTL:
230 g_free(wth->capture.nettl);
239 if (wth->frame_buffer) {
240 buffer_free(wth->frame_buffer);
241 g_free(wth->frame_buffer);
247 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
250 int data_offset, loop = 0;
252 while ((data_offset = wth->subtype_read(wth, err)) > 0) {
253 callback(user, &wth->phdr, data_offset,
254 buffer_start_ptr(wth->frame_buffer));
255 if (count > 0 && ++loop >= count)
259 return FALSE; /* failure */
261 return TRUE; /* success */
264 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
268 case WTAP_FILE_ASCEND:
269 return ascend_seek_read(fh, seek_off, pd, len);
271 case WTAP_FILE_TOSHIBA:
272 return toshiba_seek_read(fh, seek_off, pd, len);
275 return wtap_def_seek_read(fh, seek_off, pd, len);