3 * $Id: wtap.c,v 1.27 1999/10/31 17:46:10 gram Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.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.
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 const char *wtap_file_type_string(wtap *wth)
62 switch (wth->file_type) {
69 case WTAP_FILE_LANALYZER:
70 return "Novell LANalyzer";
72 case WTAP_FILE_NGSNIFFER:
73 return "Network Associates Sniffer (DOS-based)";
78 case WTAP_FILE_IPTRACE:
81 case WTAP_FILE_NETMON_1_x:
82 return "Microsoft Network Monitor 1.x";
84 case WTAP_FILE_NETMON_2_x:
85 return "Microsoft Network Monitor 2.x";
87 case WTAP_FILE_NETXRAY_1_0:
88 return "Cinco Networks NetXRay";
90 case WTAP_FILE_NETXRAY_1_1:
91 return "Network Associates Sniffer (Windows-based) 1.1";
93 case WTAP_FILE_NETXRAY_2_001:
94 return "Network Associates Sniffer (Windows-based) 2.001";
96 case WTAP_FILE_RADCOM:
97 return "RADCOM WAN/LAN analyzer";
99 case WTAP_FILE_ASCEND:
100 return "Lucent/Ascend access server trace";
102 case WTAP_FILE_NETTL:
103 return "HP-UX nettl trace";
105 case WTAP_FILE_TOSHIBA:
106 return "Toshiba Compact ISDN Router snoop trace";
109 g_error("Unknown capture file type %d", wth->file_type);
114 static const char *wtap_errlist[] = {
115 "The file isn't a plain file",
116 "The file isn't a capture file in a known format",
117 "File contains record data we don't support",
119 "Files can't be saved in that format",
120 "Files from that network type can't be saved in that format",
121 "That format doesn't support per-packet encapsulations",
124 "Less data was read than was expected",
125 "File contains a record that's not valid",
126 "Less data was written than was requested"
128 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
130 const char *wtap_strerror(int err)
132 static char errbuf[6+11+1]; /* "Error %d" */
133 int wtap_errlist_index;
137 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
138 /* Assume it's a zlib error. */
139 sprintf(errbuf, "Uncompression error: %s",
140 zError(err - WTAP_ERR_ZLIB));
144 wtap_errlist_index = -1 - err;
145 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
146 sprintf(errbuf, "Error %d", err);
149 if (wtap_errlist[wtap_errlist_index] == NULL)
150 return "Unknown reason";
151 return wtap_errlist[wtap_errlist_index];
153 return strerror(err);
156 void wtap_close(wtap *wth)
158 /* free up memory. If any capture structure ever allocates
159 * its own memory, it would be better to make a *close() function
160 * for each filetype, like pcap_close(0, lanalyzer_close(), etc.
161 * But for now this will work. */
162 switch(wth->file_type) {
164 g_free(wth->capture.pcap);
167 case WTAP_FILE_LANALYZER:
168 g_free(wth->capture.lanalyzer);
171 case WTAP_FILE_NGSNIFFER:
172 g_free(wth->capture.ngsniffer);
175 case WTAP_FILE_RADCOM:
176 g_free(wth->capture.radcom);
179 case WTAP_FILE_NETMON_1_x:
180 case WTAP_FILE_NETMON_2_x:
181 g_free(wth->capture.netmon);
184 case WTAP_FILE_NETXRAY_1_0:
185 case WTAP_FILE_NETXRAY_1_1:
186 case WTAP_FILE_NETXRAY_2_001:
187 g_free(wth->capture.netxray);
190 case WTAP_FILE_ASCEND:
191 g_free(wth->capture.ascend);
194 case WTAP_FILE_NETTL:
195 g_free(wth->capture.nettl);
205 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
208 int data_offset, loop = 0;
210 while ((data_offset = wth->subtype_read(wth, err)) > 0) {
211 callback(user, &wth->phdr, data_offset,
212 buffer_start_ptr(wth->frame_buffer));
213 if (count > 0 && ++loop >= count)
217 return FALSE; /* failure */
219 return TRUE; /* success */
222 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
226 case WTAP_FILE_ASCEND:
227 return ascend_seek_read(fh, seek_off, pd, len);
229 case WTAP_FILE_TOSHIBA:
230 return toshiba_seek_read(fh, seek_off, pd, len);
233 return wtap_def_seek_read(fh, seek_off, pd, len);