HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / capture_info.c
blob42a9a16ade9a9c4fab79b310eccbb90209b45006
1 /* capture_info.c
2 * capture info functions
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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.
25 #include "config.h"
27 #ifdef HAVE_LIBPCAP
29 #include <glib.h>
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 #include <epan/filesystem.h>
37 #include "capture_info.h"
39 #include <epan/dissectors/packet-ap1394.h>
40 #include <epan/dissectors/packet-atalk.h>
41 #include <epan/dissectors/packet-atm.h>
42 #include <epan/dissectors/packet-ax25.h>
43 #include <epan/dissectors/packet-clip.h>
44 #include <epan/dissectors/packet-eth.h>
45 #include <epan/dissectors/packet-fddi.h>
46 #include <epan/dissectors/packet-fr.h>
47 #include <epan/dissectors/packet-null.h>
48 #include <epan/dissectors/packet-ppi.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-ieee80211-radiotap.h>
55 #include <epan/dissectors/packet-chdlc.h>
56 #include <epan/dissectors/packet-ipfc.h>
57 #include <epan/dissectors/packet-arcnet.h>
58 #include <epan/dissectors/packet-enc.h>
59 #include <epan/dissectors/packet-i2c.h>
60 #include <epan/dissectors/packet-ax25-kiss.h>
62 static void capture_info_packet(
63 packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header);
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;
74 static info_data_t info_data;
77 /* open the info */
78 void capture_info_open(capture_session *cap_session)
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;
95 info_data.wtap = NULL;
96 info_data.ui.counts = &info_data.counts;
98 capture_info_ui_create(&info_data.ui, cap_session);
102 static const char *
103 cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
104 int file_type)
106 const char *errmsg;
107 static char errmsg_errno[1024+1];
109 if (err < 0) {
110 /* Wiretap error. */
111 switch (err) {
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;
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;
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;
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_subtype_string(file_type));
136 errmsg = errmsg_errno;
137 break;
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;
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;
154 break;
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;
163 case WTAP_ERR_BAD_FILE:
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;
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;
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;
184 case WTAP_ERR_SHORT_WRITE:
185 errmsg = "A full header couldn't be written to the file \"%s\".";
186 break;
188 case WTAP_ERR_DECOMPRESS:
189 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
190 "The compressed file \"%%s\" appears to be damaged or corrupt.\n"
191 "(%s)", err_info);
192 g_free(err_info);
193 errmsg = errmsg_errno;
194 break;
196 default:
197 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
198 "The file \"%%s\" could not be %s: %s.",
199 for_writing ? "created" : "opened",
200 wtap_strerror(err));
201 errmsg = errmsg_errno;
202 break;
204 } else
205 errmsg = file_open_error_message(err, for_writing);
206 return errmsg;
209 /* new file arrived */
210 gboolean capture_info_new_file(const char *new_filename)
212 int err;
213 gchar *err_info;
214 gchar *err_msg;
217 if(info_data.wtap != NULL) {
218 wtap_close(info_data.wtap);
221 info_data.wtap = wtap_open_offline(new_filename, &err, &err_info, FALSE);
222 if (!info_data.wtap) {
223 err_msg = g_strdup_printf(cf_open_error_message(err, err_info, FALSE, WTAP_FILE_TYPE_SUBTYPE_UNKNOWN),
224 new_filename);
225 g_warning("capture_info_new_file: %d (%s)", err, err_msg);
226 g_free (err_msg);
227 return FALSE;
228 } else
229 return TRUE;
233 /* new packets arrived */
234 void capture_info_new_packets(int to_read)
236 int err;
237 gchar *err_info;
238 gint64 data_offset;
239 struct wtap_pkthdr *phdr;
240 union wtap_pseudo_header *pseudo_header;
241 int wtap_linktype;
242 const guchar *buf;
245 info_data.ui.new_packets = to_read;
247 /*g_warning("new packets: %u", to_read);*/
249 while (to_read > 0) {
250 wtap_cleareof(info_data.wtap);
251 if (wtap_read(info_data.wtap, &err, &err_info, &data_offset)) {
252 phdr = wtap_phdr(info_data.wtap);
253 pseudo_header = &phdr->pseudo_header;
254 wtap_linktype = phdr->pkt_encap;
255 buf = wtap_buf_ptr(info_data.wtap);
257 capture_info_packet(&info_data.counts, wtap_linktype, buf, phdr->caplen, pseudo_header);
259 /*g_warning("new packet");*/
260 to_read--;
264 capture_info_ui_update(&info_data.ui);
268 /* close the info */
269 void capture_info_close(void)
271 capture_info_ui_destroy(&info_data.ui);
272 if(info_data.wtap)
273 wtap_close(info_data.wtap);
277 static void
278 capture_info_packet(packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header)
280 counts->total++;
281 switch (wtap_linktype) {
282 case WTAP_ENCAP_ETHERNET:
283 capture_eth(pd, 0, caplen, counts);
284 break;
285 case WTAP_ENCAP_FDDI:
286 case WTAP_ENCAP_FDDI_BITSWAPPED:
287 capture_fddi(pd, caplen, counts);
288 break;
289 case WTAP_ENCAP_IEEE_802_11_PRISM:
290 capture_prism(pd, 0, caplen, counts);
291 break;
292 case WTAP_ENCAP_TOKEN_RING:
293 capture_tr(pd, 0, caplen, counts);
294 break;
295 case WTAP_ENCAP_NULL:
296 capture_null(pd, caplen, counts);
297 break;
298 case WTAP_ENCAP_PPP:
299 capture_ppp_hdlc(pd, 0, caplen, counts);
300 break;
301 case WTAP_ENCAP_RAW_IP:
302 capture_raw(pd, caplen, counts);
303 break;
304 case WTAP_ENCAP_SLL:
305 capture_sll(pd, caplen, counts);
306 break;
307 case WTAP_ENCAP_LINUX_ATM_CLIP:
308 capture_clip(pd, caplen, counts);
309 break;
310 case WTAP_ENCAP_IEEE_802_11:
311 case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
312 capture_ieee80211(pd, 0, caplen, counts);
313 break;
314 case WTAP_ENCAP_IEEE_802_11_RADIOTAP:
315 capture_radiotap(pd, 0, caplen, counts);
316 break;
317 case WTAP_ENCAP_IEEE_802_11_AVS:
318 capture_wlancap(pd, 0, caplen, counts);
319 break;
320 case WTAP_ENCAP_CHDLC:
321 capture_chdlc(pd, 0, caplen, counts);
322 break;
323 case WTAP_ENCAP_LOCALTALK:
324 capture_llap(counts);
325 break;
326 case WTAP_ENCAP_ATM_PDUS:
327 capture_atm(pseudo_header, pd, caplen, counts);
328 break;
329 case WTAP_ENCAP_IP_OVER_FC:
330 capture_ipfc(pd, caplen, counts);
331 break;
332 case WTAP_ENCAP_ARCNET:
333 capture_arcnet(pd, caplen, counts, FALSE, TRUE);
334 break;
335 case WTAP_ENCAP_ARCNET_LINUX:
336 capture_arcnet(pd, caplen, counts, TRUE, FALSE);
337 break;
338 case WTAP_ENCAP_APPLE_IP_OVER_IEEE1394:
339 capture_ap1394(pd, 0, caplen, counts);
340 break;
341 case WTAP_ENCAP_FRELAY:
342 case WTAP_ENCAP_FRELAY_WITH_PHDR:
343 capture_fr(pd, 0, caplen, counts);
344 break;
345 case WTAP_ENCAP_ENC:
346 capture_enc(pd, caplen, counts);
347 break;
348 case WTAP_ENCAP_PPI:
349 capture_ppi(pd, caplen, counts);
350 break;
351 case WTAP_ENCAP_I2C:
352 capture_i2c(pseudo_header, counts);
353 break;
354 case WTAP_ENCAP_AX25_KISS:
355 capture_ax25_kiss(pd, 0, caplen, counts);
356 break;
357 case WTAP_ENCAP_AX25:
358 capture_ax25(pd, 0, caplen, counts);
359 break;
360 /* XXX - some ATM drivers on FreeBSD might prepend a 4-byte ATM
361 pseudo-header to DLT_ATM_RFC1483, with LLC header following;
362 we might have to implement that at some point. */
367 #endif /* HAVE_LIBPCAP */