Add a module to wiretap to be able to read trace files from Toshiba's
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.27 1999/10/31 17:46:10 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
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.
12  * 
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.
17  * 
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.
21  *
22  */
23 #include <string.h>
24 #include <errno.h>
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 #include "wtap.h"
30 #include "file.h"
31 #include "buffer.h"
32 #include "ascend.h"
33 #include "toshiba.h"
34
35 FILE* wtap_file(wtap *wth)
36 {
37         return wth->fh;
38 }
39
40 int wtap_fd(wtap *wth)
41 {
42         return wth->fd;
43 }
44
45 int wtap_file_type(wtap *wth)
46 {
47         return wth->file_type;
48 }
49
50 int wtap_snapshot_length(wtap *wth)
51 {
52         return wth->snapshot_length;
53 }
54
55 int wtap_file_encap(wtap *wth)
56 {
57         return wth->file_encap;
58 }
59
60 const char *wtap_file_type_string(wtap *wth)
61 {
62         switch (wth->file_type) {
63                 case WTAP_FILE_WTAP:
64                         return "wiretap";
65
66                 case WTAP_FILE_PCAP:
67                         return "pcap";
68
69                 case WTAP_FILE_LANALYZER:
70                         return "Novell LANalyzer";
71
72                 case WTAP_FILE_NGSNIFFER:
73                         return "Network Associates Sniffer (DOS-based)";
74
75                 case WTAP_FILE_SNOOP:
76                         return "snoop";
77
78                 case WTAP_FILE_IPTRACE:
79                         return "AIX iptrace";
80
81                 case WTAP_FILE_NETMON_1_x:
82                         return "Microsoft Network Monitor 1.x";
83
84                 case WTAP_FILE_NETMON_2_x:
85                         return "Microsoft Network Monitor 2.x";
86
87                 case WTAP_FILE_NETXRAY_1_0:
88                         return "Cinco Networks NetXRay";
89
90                 case WTAP_FILE_NETXRAY_1_1:
91                         return "Network Associates Sniffer (Windows-based) 1.1";
92
93                 case WTAP_FILE_NETXRAY_2_001:
94                         return "Network Associates Sniffer (Windows-based) 2.001";
95
96                 case WTAP_FILE_RADCOM:
97                         return "RADCOM WAN/LAN analyzer";
98
99                 case WTAP_FILE_ASCEND:
100                         return "Lucent/Ascend access server trace";
101
102                 case WTAP_FILE_NETTL:
103                         return "HP-UX nettl trace";
104
105                 case WTAP_FILE_TOSHIBA:
106                         return "Toshiba Compact ISDN Router snoop trace";
107
108                 default:
109                         g_error("Unknown capture file type %d", wth->file_type);
110                         return NULL;
111         }
112 }
113
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",
118         NULL,
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",
122         NULL,
123         NULL,
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"
127 };
128 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
129
130 const char *wtap_strerror(int err)
131 {
132         static char errbuf[6+11+1];     /* "Error %d" */
133         int wtap_errlist_index;
134
135         if (err < 0) {
136 #ifdef HAVE_LIBZ
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));
141                         return errbuf;
142                 }
143 #endif
144                 wtap_errlist_index = -1 - err;
145                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
146                         sprintf(errbuf, "Error %d", err);
147                         return errbuf;
148                 }
149                 if (wtap_errlist[wtap_errlist_index] == NULL)
150                         return "Unknown reason";
151                 return wtap_errlist[wtap_errlist_index];
152         } else
153                 return strerror(err);
154 }
155
156 void wtap_close(wtap *wth)
157 {
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) {
163                 case WTAP_FILE_PCAP:
164                         g_free(wth->capture.pcap);
165                         break;
166
167                 case WTAP_FILE_LANALYZER:
168                         g_free(wth->capture.lanalyzer);
169                         break;
170
171                 case WTAP_FILE_NGSNIFFER:
172                         g_free(wth->capture.ngsniffer);
173                         break;
174
175                 case WTAP_FILE_RADCOM:
176                         g_free(wth->capture.radcom);
177                         break;
178
179                 case WTAP_FILE_NETMON_1_x:
180                 case WTAP_FILE_NETMON_2_x:
181                         g_free(wth->capture.netmon);
182                         break;
183
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);
188                         break;
189
190                 case WTAP_FILE_ASCEND:
191                         g_free(wth->capture.ascend);
192                         break;
193
194                 case WTAP_FILE_NETTL:
195                         g_free(wth->capture.nettl);
196                         break;
197
198                 /* default:
199                          nothing */
200         }
201
202         file_close(wth->fh);
203 }
204
205 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
206         int *err)
207 {
208         int data_offset, loop = 0;
209
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)
214                         break;
215         }
216         if (data_offset < 0)
217                 return FALSE;   /* failure */
218         else
219                 return TRUE;    /* success */
220 }
221
222 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
223 {
224         switch (file_type) {
225
226         case WTAP_FILE_ASCEND:
227                 return ascend_seek_read(fh, seek_off, pd, len);
228
229         case WTAP_FILE_TOSHIBA:
230                 return toshiba_seek_read(fh, seek_off, pd, len);
231
232         default:
233                 return wtap_def_seek_read(fh, seek_off, pd, len);
234         }
235 }