c141bb9693ef41977668d821d082d527ee8786e5
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.31 1999/11/26 17:57:14 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_PCAP_MODIFIED:
70                         return "pcap-modified";
71
72                 case WTAP_FILE_LANALYZER:
73                         return "Novell LANalyzer";
74
75                 case WTAP_FILE_NGSNIFFER:
76                         return "Network Associates Sniffer (DOS-based)";
77
78                 case WTAP_FILE_SNOOP:
79                         return "snoop";
80
81                 case WTAP_FILE_IPTRACE_1_0:
82                         return "AIX iptrace 1.0";
83
84                 case WTAP_FILE_IPTRACE_2_0:
85                         return "AIX iptrace 2.0";
86
87                 case WTAP_FILE_NETMON_1_x:
88                         return "Microsoft Network Monitor 1.x";
89
90                 case WTAP_FILE_NETMON_2_x:
91                         return "Microsoft Network Monitor 2.x";
92
93                 case WTAP_FILE_NETXRAY_1_0:
94                         return "Cinco Networks NetXRay";
95
96                 case WTAP_FILE_NETXRAY_1_1:
97                         return "Network Associates Sniffer (Windows-based) 1.1";
98
99                 case WTAP_FILE_NETXRAY_2_001:
100                         return "Network Associates Sniffer (Windows-based) 2.001";
101
102                 case WTAP_FILE_RADCOM:
103                         return "RADCOM WAN/LAN analyzer";
104
105                 case WTAP_FILE_ASCEND:
106                         return "Lucent/Ascend access server trace";
107
108                 case WTAP_FILE_NETTL:
109                         return "HP-UX nettl trace";
110
111                 case WTAP_FILE_TOSHIBA:
112                         return "Toshiba Compact ISDN Router snoop trace";
113
114                 default:
115                         g_error("Unknown capture file type %d", wth->file_type);
116                         return NULL;
117         }
118 }
119
120 static const char *wtap_errlist[] = {
121         "The file isn't a plain file",
122         "The file isn't a capture file in a known format",
123         "File contains record data we don't support",
124         NULL,
125         "Files can't be saved in that format",
126         "Files from that network type can't be saved in that format",
127         "That format doesn't support per-packet encapsulations",
128         NULL,
129         NULL,
130         "Less data was read than was expected",
131         "File contains a record that's not valid",
132         "Less data was written than was requested"
133 };
134 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
135
136 const char *wtap_strerror(int err)
137 {
138         static char errbuf[6+11+1];     /* "Error %d" */
139         int wtap_errlist_index;
140
141         if (err < 0) {
142 #ifdef HAVE_LIBZ
143                 if (err >= WTAP_ERR_ZLIB_MIN && err <= WTAP_ERR_ZLIB_MAX) {
144                         /* Assume it's a zlib error. */
145                         sprintf(errbuf, "Uncompression error: %s",
146                             zError(err - WTAP_ERR_ZLIB));
147                         return errbuf;
148                 }
149 #endif
150                 wtap_errlist_index = -1 - err;
151                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
152                         sprintf(errbuf, "Error %d", err);
153                         return errbuf;
154                 }
155                 if (wtap_errlist[wtap_errlist_index] == NULL)
156                         return "Unknown reason";
157                 return wtap_errlist[wtap_errlist_index];
158         } else
159                 return strerror(err);
160 }
161
162 void wtap_close(wtap *wth)
163 {
164         /* free up memory. If any capture structure ever allocates
165          * its own memory, it would be better to make a *close() function
166          * for each filetype, like pcap_close(0, lanalyzer_close(), etc.
167          * But for now this will work. */
168         switch(wth->file_type) {
169                 case WTAP_FILE_PCAP:
170                 case WTAP_FILE_PCAP_MODIFIED:
171                         g_free(wth->capture.pcap);
172                         break;
173
174                 case WTAP_FILE_LANALYZER:
175                         g_free(wth->capture.lanalyzer);
176                         break;
177
178                 case WTAP_FILE_NGSNIFFER:
179                         g_free(wth->capture.ngsniffer);
180                         break;
181
182                 case WTAP_FILE_RADCOM:
183                         g_free(wth->capture.radcom);
184                         break;
185
186                 case WTAP_FILE_NETMON_1_x:
187                 case WTAP_FILE_NETMON_2_x:
188                         g_free(wth->capture.netmon);
189                         break;
190
191                 case WTAP_FILE_NETXRAY_1_0:
192                 case WTAP_FILE_NETXRAY_1_1:
193                 case WTAP_FILE_NETXRAY_2_001:
194                         g_free(wth->capture.netxray);
195                         break;
196
197                 case WTAP_FILE_ASCEND:
198                         g_free(wth->capture.ascend);
199                         break;
200
201                 case WTAP_FILE_NETTL:
202                         g_free(wth->capture.nettl);
203                         break;
204
205                 /* default:
206                          nothing */
207         }
208
209         file_close(wth->fh);
210
211         if (wth->frame_buffer) {
212                 buffer_free(wth->frame_buffer);
213                 g_free(wth->frame_buffer);
214         }
215
216         g_free(wth);
217 }
218
219 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
220         int *err)
221 {
222         int data_offset, loop = 0;
223
224         while ((data_offset = wth->subtype_read(wth, err)) > 0) {
225                 callback(user, &wth->phdr, data_offset,
226                     buffer_start_ptr(wth->frame_buffer));
227                 if (count > 0 && ++loop >= count)
228                         break;
229         }
230         if (data_offset < 0)
231                 return FALSE;   /* failure */
232         else
233                 return TRUE;    /* success */
234 }
235
236 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
237 {
238         switch (file_type) {
239
240         case WTAP_FILE_ASCEND:
241                 return ascend_seek_read(fh, seek_off, pd, len);
242
243         case WTAP_FILE_TOSHIBA:
244                 return toshiba_seek_read(fh, seek_off, pd, len);
245
246         default:
247                 return wtap_def_seek_read(fh, seek_off, pd, len);
248         }
249 }