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