Wireshark: Add option to choose format type of capture file
[metze/wireshark/wip.git] / capture_info.c
1 /* capture_info.c
2  * capture info functions
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #ifdef HAVE_LIBPCAP
28
29 #include <glib.h>
30
31 #include <epan/packet.h>
32 /* XXX - try to remove this later */
33 #include <epan/prefs.h>
34 /* XXX - try to remove this later */
35
36 #include <wiretap/wtap.h>
37
38 #include "capture_info.h"
39
40 #include <epan/dissectors/packet-ap1394.h>
41 #include <epan/dissectors/packet-atalk.h>
42 #include <epan/dissectors/packet-atm.h>
43 #include <epan/dissectors/packet-ax25.h>
44 #include <epan/dissectors/packet-clip.h>
45 #include <epan/dissectors/packet-eth.h>
46 #include <epan/dissectors/packet-fddi.h>
47 #include <epan/dissectors/packet-fr.h>
48 #include <epan/dissectors/packet-null.h>
49 #include <epan/dissectors/packet-ppi.h>
50 #include <epan/dissectors/packet-ppp.h>
51 #include <epan/dissectors/packet-raw.h>
52 #include <epan/dissectors/packet-sll.h>
53 #include <epan/dissectors/packet-tr.h>
54 #include <epan/dissectors/packet-ieee80211.h>
55 #include <epan/dissectors/packet-ieee80211-radiotap.h>
56 #include <epan/dissectors/packet-chdlc.h>
57 #include <epan/dissectors/packet-ipfc.h>
58 #include <epan/dissectors/packet-arcnet.h>
59 #include <epan/dissectors/packet-enc.h>
60 #include <epan/dissectors/packet-i2c.h>
61 #include <epan/dissectors/packet-ax25-kiss.h>
62
63 #include <wsutil/filesystem.h>
64
65 static void capture_info_packet(
66 packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header);
67
68
69
70 typedef struct _info_data {
71     packet_counts     counts;     /* several packet type counters */
72     struct wtap*      wtap;       /* current wtap file */
73     capture_info      ui;         /* user interface data */
74 } info_data_t;
75
76
77 static info_data_t info_data;
78
79
80 /* open the info */
81 void capture_info_open(capture_session *cap_session)
82 {
83     info_data.counts.total      = 0;
84     info_data.counts.sctp       = 0;
85     info_data.counts.tcp        = 0;
86     info_data.counts.udp        = 0;
87     info_data.counts.icmp       = 0;
88     info_data.counts.ospf       = 0;
89     info_data.counts.gre        = 0;
90     info_data.counts.ipx        = 0;
91     info_data.counts.netbios    = 0;
92     info_data.counts.vines      = 0;
93     info_data.counts.other      = 0;
94     info_data.counts.arp        = 0;
95     info_data.counts.i2c_event  = 0;
96     info_data.counts.i2c_data   = 0;
97
98     info_data.wtap = NULL;
99     info_data.ui.counts = &info_data.counts;
100
101     capture_info_ui_create(&info_data.ui, cap_session);
102 }
103
104
105 static const char *
106 cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
107                       int file_type)
108 {
109     const char *errmsg;
110     static char errmsg_errno[1024+1];
111
112     if (err < 0) {
113         /* Wiretap error. */
114         switch (err) {
115
116         case WTAP_ERR_NOT_REGULAR_FILE:
117             errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file.";
118             break;
119
120         case WTAP_ERR_FILE_UNKNOWN_FORMAT:
121             /* Seen only when opening a capture file for reading. */
122             errmsg = "The file \"%s\" isn't a capture file in a format TShark understands.";
123             break;
124
125         case WTAP_ERR_UNSUPPORTED:
126             /* Seen only when opening a capture file for reading. */
127             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
128                        "The file \"%%s\" isn't a capture file in a format TShark understands.\n"
129                        "(%s)", err_info);
130             g_free(err_info);
131             errmsg = errmsg_errno;
132             break;
133
134         case WTAP_ERR_CANT_WRITE_TO_PIPE:
135             /* Seen only when opening a capture file for writing. */
136             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
137                        "The file \"%%s\" is a pipe, and %s capture files can't be "
138                        "written to a pipe.", wtap_file_type_subtype_string(file_type));
139             errmsg = errmsg_errno;
140             break;
141
142         case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
143             /* Seen only when opening a capture file for writing. */
144             errmsg = "TShark doesn't support writing capture files in that format.";
145             break;
146
147         case WTAP_ERR_UNSUPPORTED_ENCAP:
148             if (for_writing)
149                 errmsg = "TShark can't save this capture in that format.";
150             else {
151                 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
152                            "The file \"%%s\" is a capture for a network type that TShark doesn't support.\n"
153                            "(%s)", err_info);
154                 g_free(err_info);
155                 errmsg = errmsg_errno;
156             }
157             break;
158
159         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
160             if (for_writing)
161                 errmsg = "TShark can't save this capture in that format.";
162             else
163                 errmsg = "The file \"%s\" is a capture for a network type that TShark doesn't support.";
164             break;
165
166         case WTAP_ERR_BAD_FILE:
167             /* Seen only when opening a capture file for reading. */
168             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
169                        "The file \"%%s\" appears to be damaged or corrupt.\n"
170                        "(%s)", err_info);
171             g_free(err_info);
172             errmsg = errmsg_errno;
173             break;
174
175         case WTAP_ERR_CANT_OPEN:
176             if (for_writing)
177                 errmsg = "The file \"%s\" could not be created for some unknown reason.";
178             else
179                 errmsg = "The file \"%s\" could not be opened for some unknown reason.";
180             break;
181
182         case WTAP_ERR_SHORT_READ:
183             errmsg = "The file \"%s\" appears to have been cut short"
184                 " in the middle of a packet or other data.";
185             break;
186
187         case WTAP_ERR_SHORT_WRITE:
188             errmsg = "A full header couldn't be written to the file \"%s\".";
189             break;
190
191         case WTAP_ERR_DECOMPRESS:
192             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
193                        "The compressed file \"%%s\" appears to be damaged or corrupt.\n"
194                        "(%s)", err_info);
195             g_free(err_info);
196             errmsg = errmsg_errno;
197             break;
198
199         default:
200             g_snprintf(errmsg_errno, sizeof(errmsg_errno),
201                        "The file \"%%s\" could not be %s: %s.",
202                        for_writing ? "created" : "opened",
203                        wtap_strerror(err));
204             errmsg = errmsg_errno;
205             break;
206         }
207     } else
208         errmsg = file_open_error_message(err, for_writing);
209     return errmsg;
210 }
211
212 /* new file arrived */
213 gboolean capture_info_new_file(const char *new_filename)
214 {
215     int err;
216     gchar *err_info;
217     gchar *err_msg;
218
219
220     if(info_data.wtap != NULL) {
221         wtap_close(info_data.wtap);
222     }
223
224     info_data.wtap = wtap_open_offline(new_filename, WTAP_TYPE_AUTO, &err, &err_info, FALSE);
225     if (!info_data.wtap) {
226         err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN),
227                                   new_filename);
228         g_warning("capture_info_new_file: %d (%s)", err, err_msg);
229         g_free (err_msg);
230         return FALSE;
231     } else
232         return TRUE;
233 }
234
235
236 /* new packets arrived */
237 void capture_info_new_packets(int to_read)
238 {
239     int err;
240     gchar *err_info;
241     gint64 data_offset;
242     struct wtap_pkthdr *phdr;
243     union wtap_pseudo_header *pseudo_header;
244     int wtap_linktype;
245     const guchar *buf;
246
247
248     info_data.ui.new_packets = to_read;
249
250     /*g_warning("new packets: %u", to_read);*/
251
252     while (to_read > 0) {
253         wtap_cleareof(info_data.wtap);
254         if (wtap_read(info_data.wtap, &err, &err_info, &data_offset)) {
255             phdr = wtap_phdr(info_data.wtap);
256             pseudo_header = &phdr->pseudo_header;
257             wtap_linktype = phdr->pkt_encap;
258             buf = wtap_buf_ptr(info_data.wtap);
259
260             capture_info_packet(&info_data.counts, wtap_linktype, buf, phdr->caplen, pseudo_header);
261
262             /*g_warning("new packet");*/
263             to_read--;
264         }
265     }
266
267     capture_info_ui_update(&info_data.ui);
268 }
269
270
271 /* close the info */
272 void capture_info_close(void)
273 {
274     capture_info_ui_destroy(&info_data.ui);
275     if(info_data.wtap)
276         wtap_close(info_data.wtap);
277 }
278
279
280 static void
281 capture_info_packet(packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header)
282 {
283     counts->total++;
284     switch (wtap_linktype) {
285     case WTAP_ENCAP_ETHERNET:
286         capture_eth(pd, 0, caplen, counts);
287         break;
288     case WTAP_ENCAP_FDDI:
289     case WTAP_ENCAP_FDDI_BITSWAPPED:
290         capture_fddi(pd, caplen, counts);
291         break;
292     case WTAP_ENCAP_IEEE_802_11_PRISM:
293         capture_prism(pd, 0, caplen, counts);
294         break;
295     case WTAP_ENCAP_TOKEN_RING:
296         capture_tr(pd, 0, caplen, counts);
297         break;
298     case WTAP_ENCAP_NULL:
299         capture_null(pd, caplen, counts);
300         break;
301     case WTAP_ENCAP_PPP:
302         capture_ppp_hdlc(pd, 0, caplen, counts);
303         break;
304     case WTAP_ENCAP_RAW_IP:
305         capture_raw(pd, caplen, counts);
306         break;
307     case WTAP_ENCAP_SLL:
308         capture_sll(pd, caplen, counts);
309         break;
310     case WTAP_ENCAP_LINUX_ATM_CLIP:
311         capture_clip(pd, caplen, counts);
312         break;
313     case WTAP_ENCAP_IEEE_802_11:
314     case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
315         capture_ieee80211(pd, 0, caplen, counts);
316         break;
317     case WTAP_ENCAP_IEEE_802_11_RADIOTAP:
318         capture_radiotap(pd, 0, caplen, counts);
319         break;
320     case WTAP_ENCAP_IEEE_802_11_AVS:
321         capture_wlancap(pd, 0, caplen, counts);
322         break;
323     case WTAP_ENCAP_CHDLC:
324         capture_chdlc(pd, 0, caplen, counts);
325         break;
326     case WTAP_ENCAP_LOCALTALK:
327         capture_llap(counts);
328         break;
329     case WTAP_ENCAP_ATM_PDUS:
330         capture_atm(pseudo_header, pd, caplen, counts);
331         break;
332     case WTAP_ENCAP_IP_OVER_FC:
333         capture_ipfc(pd, caplen, counts);
334         break;
335     case WTAP_ENCAP_ARCNET:
336         capture_arcnet(pd, caplen, counts, FALSE, TRUE);
337         break;
338     case WTAP_ENCAP_ARCNET_LINUX:
339         capture_arcnet(pd, caplen, counts, TRUE, FALSE);
340         break;
341     case WTAP_ENCAP_APPLE_IP_OVER_IEEE1394:
342         capture_ap1394(pd, 0, caplen, counts);
343         break;
344     case WTAP_ENCAP_FRELAY:
345     case WTAP_ENCAP_FRELAY_WITH_PHDR:
346         capture_fr(pd, 0, caplen, counts);
347         break;
348     case WTAP_ENCAP_ENC:
349         capture_enc(pd, caplen, counts);
350         break;
351     case WTAP_ENCAP_PPI:
352         capture_ppi(pd, caplen, counts);
353         break;
354     case WTAP_ENCAP_I2C:
355         capture_i2c(pseudo_header, counts);
356         break;
357     case WTAP_ENCAP_AX25_KISS:
358         capture_ax25_kiss(pd, 0, caplen, counts);
359         break;
360     case WTAP_ENCAP_AX25:
361         capture_ax25(pd, 0, caplen, counts);
362         break;
363         /* XXX - some ATM drivers on FreeBSD might prepend a 4-byte ATM
364            pseudo-header to DLT_ATM_RFC1483, with LLC header following;
365            we might have to implement that at some point. */
366     }
367 }
368
369
370 #endif /* HAVE_LIBPCAP */