Give those capture file formats we can write a "short name", to be used
[obnox/wireshark/wip.git] / wiretap / wtap.c
1 /* wtap.c
2  *
3  * $Id: wtap.c,v 1.32 1999/12/04 08:32:13 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 #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 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",
64         NULL,
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",
68         NULL,
69         NULL,
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"
73 };
74 #define WTAP_ERRLIST_SIZE       (sizeof wtap_errlist / sizeof wtap_errlist[0])
75
76 const char *wtap_strerror(int err)
77 {
78         static char errbuf[6+11+1];     /* "Error %d" */
79         int wtap_errlist_index;
80
81         if (err < 0) {
82 #ifdef HAVE_LIBZ
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));
87                         return errbuf;
88                 }
89 #endif
90                 wtap_errlist_index = -1 - err;
91                 if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
92                         sprintf(errbuf, "Error %d", err);
93                         return errbuf;
94                 }
95                 if (wtap_errlist[wtap_errlist_index] == NULL)
96                         return "Unknown reason";
97                 return wtap_errlist[wtap_errlist_index];
98         } else
99                 return strerror(err);
100 }
101
102 void wtap_close(wtap *wth)
103 {
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) {
109                 case WTAP_FILE_PCAP:
110                 case WTAP_FILE_PCAP_MODIFIED:
111                         g_free(wth->capture.pcap);
112                         break;
113
114                 case WTAP_FILE_LANALYZER:
115                         g_free(wth->capture.lanalyzer);
116                         break;
117
118                 case WTAP_FILE_NGSNIFFER:
119                         g_free(wth->capture.ngsniffer);
120                         break;
121
122                 case WTAP_FILE_RADCOM:
123                         g_free(wth->capture.radcom);
124                         break;
125
126                 case WTAP_FILE_NETMON_1_x:
127                 case WTAP_FILE_NETMON_2_x:
128                         g_free(wth->capture.netmon);
129                         break;
130
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);
135                         break;
136
137                 case WTAP_FILE_ASCEND:
138                         g_free(wth->capture.ascend);
139                         break;
140
141                 case WTAP_FILE_NETTL:
142                         g_free(wth->capture.nettl);
143                         break;
144
145                 /* default:
146                          nothing */
147         }
148
149         file_close(wth->fh);
150
151         if (wth->frame_buffer) {
152                 buffer_free(wth->frame_buffer);
153                 g_free(wth->frame_buffer);
154         }
155
156         g_free(wth);
157 }
158
159 int wtap_loop(wtap *wth, int count, wtap_handler callback, u_char* user,
160         int *err)
161 {
162         int data_offset, loop = 0;
163
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)
168                         break;
169         }
170         if (data_offset < 0)
171                 return FALSE;   /* failure */
172         else
173                 return TRUE;    /* success */
174 }
175
176 int wtap_seek_read(int file_type, FILE *fh, int seek_off, guint8 *pd, int len)
177 {
178         switch (file_type) {
179
180         case WTAP_FILE_ASCEND:
181                 return ascend_seek_read(fh, seek_off, pd, len);
182
183         case WTAP_FILE_TOSHIBA:
184                 return toshiba_seek_read(fh, seek_off, pd, len);
185
186         default:
187                 return wtap_def_seek_read(fh, seek_off, pd, len);
188         }
189 }