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