Add "wtap_file_encap()", to return the encapsulation of packets in the
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.25 1999/10/06 03:29:35 guy 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
34 FILE* wtap_file(wtap *wth)
35 {
36         return wth->fh;
37 }
38
39 int wtap_fd(wtap *wth)
40 {
41         return wth->fd;
42 }
43
44 int wtap_file_type(wtap *wth)
45 {
46         return wth->file_type;
47 }
48
49 int wtap_snapshot_length(wtap *wth)
50 {
51         return wth->snapshot_length;
52 }
53
54 int wtap_file_encap(wtap *wth)
55 {
56         return wth->file_encap;
57 }
58
59 const char *wtap_file_type_string(wtap *wth)
60 {
61         switch (wth->file_type) {
62                 case WTAP_FILE_WTAP:
63                         return "wiretap";
64
65                 case WTAP_FILE_PCAP:
66                         return "pcap";
67
68                 case WTAP_FILE_LANALYZER:
69                         return "Novell LANalyzer";
70
71                 case WTAP_FILE_NGSNIFFER:
72                         return "Network Associates Sniffer (DOS-based)";
73
74                 case WTAP_FILE_SNOOP:
75                         return "snoop";
76
77                 case WTAP_FILE_IPTRACE:
78                         return "iptrace";
79
80                 case WTAP_FILE_NETMON_1_x:
81                         return "Microsoft Network Monitor 1.x";
82
83                 case WTAP_FILE_NETMON_2_x:
84                         return "Microsoft Network Monitor 2.x";
85
86                 case WTAP_FILE_NETXRAY_1_0:
87                         return "Cinco Networks NetXRay";
88
89                 case WTAP_FILE_NETXRAY_1_1:
90                         return "Network Associates Sniffer (Windows-based) 1.1";
91
92                 case WTAP_FILE_NETXRAY_2_001:
93                         return "Network Associates Sniffer (Windows-based) 2.001";
94
95                 case WTAP_FILE_RADCOM:
96                         return "RADCOM WAN/LAN analyzer";
97
98                 case WTAP_FILE_ASCEND:
99                         return "Lucent/Ascend access server trace";
100
101                 default:
102                         g_error("Unknown capture file type %d", wth->file_type);
103                         return NULL;
104         }
105 }
106
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",
111         NULL,
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",
115         NULL,
116         NULL,
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"
120 };
121 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
122
123 const char *wtap_strerror(int err)
124 {
125         static char errbuf[6+11+1];     /* "Error %d" */
126         int wtap_errlist_index;
127
128         if (err < 0) {
129 #ifdef HAVE_LIBZ
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));
134                         return errbuf;
135                 }
136 #endif
137                 wtap_errlist_index = -1 - err;
138                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
139                         sprintf(errbuf, "Error %d", err);
140                         return errbuf;
141                 }
142                 if (wtap_errlist[wtap_errlist_index] == NULL)
143                         return "Unknown reason";
144                 return wtap_errlist[wtap_errlist_index];
145         } else
146                 return strerror(err);
147 }
148
149 void wtap_close(wtap *wth)
150 {
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) {
156                 case WTAP_FILE_PCAP:
157                         g_free(wth->capture.pcap);
158                         break;
159
160                 case WTAP_FILE_LANALYZER:
161                         g_free(wth->capture.lanalyzer);
162                         break;
163
164                 case WTAP_FILE_NGSNIFFER:
165                         g_free(wth->capture.ngsniffer);
166                         break;
167
168                 case WTAP_FILE_RADCOM:
169                         g_free(wth->capture.radcom);
170                         break;
171
172                 case WTAP_FILE_NETMON_1_x:
173                 case WTAP_FILE_NETMON_2_x:
174                         g_free(wth->capture.netmon);
175                         break;
176
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);
181                         break;
182
183                 case WTAP_FILE_ASCEND:
184                         g_free(wth->capture.ascend);
185                         break;
186
187                 /* default:
188                          nothing */
189         }
190
191         file_close(wth->fh);
192 }
193
194 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
195         int *err)
196 {
197         int data_offset, loop = 0;
198
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)
203                         break;
204         }
205         if (data_offset < 0)
206                 return FALSE;   /* failure */
207         else
208                 return TRUE;    /* success */
209 }
210
211 int wtap_seek_read(int encaps, FILE *fh, int seek_off, guint8 *pd, int len)
212 {
213         switch (encaps) {
214
215         case WTAP_ENCAP_ASCEND:
216                 return ascend_seek_read(fh, seek_off, pd, len);
217
218         default:
219                 return wtap_def_seek_read(fh, seek_off, pd, len);
220         }
221 }