3 * $Id: wtap.c,v 1.32 1999/12/04 08:32:13 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.
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 static const char *wtap_errlist[] = {
61 "The file isn't a plain file",
62 "The file isn't a capture file in a known format",
63 "File contains record data we don't support",
65 "Files can't be saved in that format",
66 "Files from that network type can't be saved in that format",
67 "That format doesn't support per-packet encapsulations",
70 "Less data was read than was expected",
71 "File contains a record that's not valid",
72 "Less data was written than was requested"
74 #define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
76 const char *wtap_strerror(int err)
78 static char errbuf[6+11+1]; /* "Error %d" */
79 int wtap_errlist_index;
83 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
84 /* Assume it's a zlib error. */
85 sprintf(errbuf, "Uncompression error: %s",
86 zError(err - WTAP_ERR_ZLIB));
90 wtap_errlist_index = -1 - err;
91 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
92 sprintf(errbuf, "Error %d", err);
95 if (wtap_errlist[wtap_errlist_index] == NULL)
96 return "Unknown reason";
97 return wtap_errlist[wtap_errlist_index];
102 void wtap_close(wtap *wth)
104 /* free up memory. If any capture structure ever allocates
105 * its own memory, it would be better to make a *close() function
106 * for each filetype, like pcap_close(0, lanalyzer_close(), etc.
107 * But for now this will work. */
108 switch(wth->file_type) {
110 case WTAP_FILE_PCAP_MODIFIED:
111 g_free(wth->capture.pcap);
114 case WTAP_FILE_LANALYZER:
115 g_free(wth->capture.lanalyzer);
118 case WTAP_FILE_NGSNIFFER:
119 g_free(wth->capture.ngsniffer);
122 case WTAP_FILE_RADCOM:
123 g_free(wth->capture.radcom);
126 case WTAP_FILE_NETMON_1_x:
127 case WTAP_FILE_NETMON_2_x:
128 g_free(wth->capture.netmon);
131 case WTAP_FILE_NETXRAY_1_0:
132 case WTAP_FILE_NETXRAY_1_1:
133 case WTAP_FILE_NETXRAY_2_001:
134 g_free(wth->capture.netxray);
137 case WTAP_FILE_ASCEND:
138 g_free(wth->capture.ascend);
141 case WTAP_FILE_NETTL:
142 g_free(wth->capture.nettl);
151 if (wth->frame_buffer) {
152 buffer_free(wth->frame_buffer);
153 g_free(wth->frame_buffer);
159 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
162 int data_offset, loop = 0;
164 while ((data_offset = wth->subtype_read(wth, err)) > 0) {
165 callback(user, &wth->phdr, data_offset,
166 buffer_start_ptr(wth->frame_buffer));
167 if (count > 0 && ++loop >= count)
171 return FALSE; /* failure */
173 return TRUE; /* success */
176 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
180 case WTAP_FILE_ASCEND:
181 return ascend_seek_read(fh, seek_off, pd, len);
183 case WTAP_FILE_TOSHIBA:
184 return toshiba_seek_read(fh, seek_off, pd, len);
187 return wtap_def_seek_read(fh, seek_off, pd, len);