3 * $Id: wtap.c,v 1.25 1999/10/06 03:29:35 guy 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.
34 FILE* wtap_file(wtap *wth)
39 int wtap_fd(wtap *wth)
44 int wtap_file_type(wtap *wth)
46 return wth->file_type;
49 int wtap_snapshot_length(wtap *wth)
51 return wth->snapshot_length;
54 int wtap_file_encap(wtap *wth)
56 return wth->file_encap;
59 const char *wtap_file_type_string(wtap *wth)
61 switch (wth->file_type) {
68 case WTAP_FILE_LANALYZER:
69 return "Novell LANalyzer";
71 case WTAP_FILE_NGSNIFFER:
72 return "Network Associates Sniffer (DOS-based)";
77 case WTAP_FILE_IPTRACE:
80 case WTAP_FILE_NETMON_1_x:
81 return "Microsoft Network Monitor 1.x";
83 case WTAP_FILE_NETMON_2_x:
84 return "Microsoft Network Monitor 2.x";
86 case WTAP_FILE_NETXRAY_1_0:
87 return "Cinco Networks NetXRay";
89 case WTAP_FILE_NETXRAY_1_1:
90 return "Network Associates Sniffer (Windows-based) 1.1";
92 case WTAP_FILE_NETXRAY_2_001:
93 return "Network Associates Sniffer (Windows-based) 2.001";
95 case WTAP_FILE_RADCOM:
96 return "RADCOM WAN/LAN analyzer";
98 case WTAP_FILE_ASCEND:
99 return "Lucent/Ascend access server trace";
102 g_error("Unknown capture file type %d", wth->file_type);
107 static const char *wtap_errlist[] = {
108 "The file isn't a plain file",
109 "The file isn't a capture file in a known format",
110 "File contains record data we don't support",
112 "Files can't be saved in that format",
113 "Files from that network type can't be saved in that format",
114 "That format doesn't support per-packet encapsulations",
117 "Less data was read than was expected",
118 "File contains a record that's not valid",
119 "Less data was written than was requested"
121 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
123 const char *wtap_strerror(int err)
125 static char errbuf[6+11+1]; /* "Error %d" */
126 int wtap_errlist_index;
130 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
131 /* Assume it's a zlib error. */
132 sprintf(errbuf, "Uncompression error: %s",
133 zError(err - WTAP_ERR_ZLIB));
137 wtap_errlist_index = -1 - err;
138 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
139 sprintf(errbuf, "Error %d", err);
142 if (wtap_errlist[wtap_errlist_index] == NULL)
143 return "Unknown reason";
144 return wtap_errlist[wtap_errlist_index];
146 return strerror(err);
149 void wtap_close(wtap *wth)
151 /* free up memory. If any capture structure ever allocates
152 * its own memory, it would be better to make a *close() function
153 * for each filetype, like pcap_close(0, lanalyzer_close(), etc.
154 * But for now this will work. */
155 switch(wth->file_type) {
157 g_free(wth->capture.pcap);
160 case WTAP_FILE_LANALYZER:
161 g_free(wth->capture.lanalyzer);
164 case WTAP_FILE_NGSNIFFER:
165 g_free(wth->capture.ngsniffer);
168 case WTAP_FILE_RADCOM:
169 g_free(wth->capture.radcom);
172 case WTAP_FILE_NETMON_1_x:
173 case WTAP_FILE_NETMON_2_x:
174 g_free(wth->capture.netmon);
177 case WTAP_FILE_NETXRAY_1_0:
178 case WTAP_FILE_NETXRAY_1_1:
179 case WTAP_FILE_NETXRAY_2_001:
180 g_free(wth->capture.netxray);
183 case WTAP_FILE_ASCEND:
184 g_free(wth->capture.ascend);
194 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
197 int data_offset, loop = 0;
199 while ((data_offset = wth->subtype_read(wth, err)) > 0) {
200 callback(user, &wth->phdr, data_offset,
201 buffer_start_ptr(wth->frame_buffer));
202 if (count > 0 && ++loop >= count)
206 return FALSE; /* failure */
208 return TRUE; /* success */
211 int wtap_seek_read(int encaps, FILE *fh, int seek_off, guint8 *pd, int len)
215 case WTAP_ENCAP_ASCEND:
216 return ascend_seek_read(fh, seek_off, pd, len);
219 return wtap_def_seek_read(fh, seek_off, pd, len);