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