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