freebsd needs to define AF_INET6, seems to need sys/socket.h
[obnox/wireshark/wip.git] / capture_info.c
1 /* capture_info.c
2  * capture info functions
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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
38 #include "capture_info.h"
39
40 #include <epan/dissectors/packet-ap1394.h>
41 #include <epan/dissectors/packet-atalk.h>
42 #include <epan/dissectors/packet-atm.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-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-radiotap.h>
54 #include <epan/dissectors/packet-chdlc.h>
55 #include <epan/dissectors/packet-prism.h>
56 #include <epan/dissectors/packet-ipfc.h>
57 #include <epan/dissectors/packet-arcnet.h>
58 #include <epan/dissectors/packet-enc.h>
59
60 static void capture_info_packet(
61 packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header);
62
63
64
65 typedef struct _info_data {
66     packet_counts     counts;     /* several packet type counters */
67     struct wtap*      wtap;       /* current wtap file */
68     capture_info      ui;         /* user interface data */
69 } info_data_t;
70
71
72 info_data_t info_data;
73
74
75 /* open the info */
76 void capture_info_open(const char *iface)
77 {
78     info_data.counts.total      = 0;
79     info_data.counts.sctp       = 0;
80     info_data.counts.tcp        = 0;
81     info_data.counts.udp        = 0;
82     info_data.counts.icmp       = 0;
83     info_data.counts.ospf       = 0;
84     info_data.counts.gre        = 0;
85     info_data.counts.ipx        = 0;
86     info_data.counts.netbios    = 0;
87     info_data.counts.vines      = 0;
88     info_data.counts.other      = 0;
89     info_data.counts.arp        = 0;
90
91     info_data.wtap = NULL;
92     info_data.ui.counts = &info_data.counts;
93
94     capture_info_ui_create(&info_data.ui, iface);
95 }
96
97
98 /* new file arrived */
99 void capture_info_new_file(const char *new_filename)
100 {
101     int err;
102     gchar *err_info;
103
104
105     if(info_data.wtap != NULL) {
106         wtap_close(info_data.wtap);
107     }
108
109     info_data.wtap = wtap_open_offline(new_filename, &err, &err_info, FALSE);
110     if (!info_data.wtap) {
111         g_warning("capture_info_new_file: wtap open failed: %s", err_info);
112     }
113
114 }
115
116
117 /* new packets arrived */
118 void capture_info_new_packets(int to_read)
119 {
120     int err;
121     gchar *err_info;
122     long data_offset;
123     const struct wtap_pkthdr *phdr;
124     union wtap_pseudo_header *pseudo_header;
125     int wtap_linktype;
126     const guchar *buf;
127
128
129     info_data.ui.new_packets = to_read;
130
131     /*g_warning("new packets: %u", to_read);*/
132
133     while (to_read != 0 && (wtap_read(info_data.wtap, &err, &err_info, &data_offset))) {
134         phdr = wtap_phdr(info_data.wtap);
135         pseudo_header = wtap_pseudoheader(info_data.wtap);
136         wtap_linktype = phdr->pkt_encap;
137         buf = wtap_buf_ptr(info_data.wtap);
138
139         capture_info_packet(&info_data.counts, wtap_linktype, buf, phdr->caplen, pseudo_header);
140
141         /*g_warning("new packet");*/
142         to_read--;
143     }
144
145     capture_info_ui_update(&info_data.ui);
146 }
147
148
149 /* close the info */
150 void capture_info_close(void)
151 {
152     capture_info_ui_destroy(&info_data.ui);
153     if(info_data.wtap)
154         wtap_close(info_data.wtap);
155 }
156
157
158 static void
159 capture_info_packet(packet_counts *counts, gint wtap_linktype, const guchar *pd, guint32 caplen, union wtap_pseudo_header *pseudo_header)
160 {
161   counts->total++;
162   switch (wtap_linktype) {
163     case WTAP_ENCAP_ETHERNET:
164       capture_eth(pd, 0, caplen, counts);
165       break;
166     case WTAP_ENCAP_FDDI:
167     case WTAP_ENCAP_FDDI_BITSWAPPED:
168       capture_fddi(pd, caplen, counts);
169       break;
170     case WTAP_ENCAP_PRISM_HEADER:
171       capture_prism(pd, 0, caplen, counts);
172       break;
173     case WTAP_ENCAP_TOKEN_RING:
174       capture_tr(pd, 0, caplen, counts);
175       break;
176     case WTAP_ENCAP_NULL:
177       capture_null(pd, caplen, counts);
178       break;
179     case WTAP_ENCAP_PPP:
180       capture_ppp_hdlc(pd, 0, caplen, counts);
181       break;
182     case WTAP_ENCAP_RAW_IP:
183       capture_raw(pd, caplen, counts);
184       break;
185     case WTAP_ENCAP_SLL:
186       capture_sll(pd, caplen, counts);
187       break;
188     case WTAP_ENCAP_LINUX_ATM_CLIP:
189       capture_clip(pd, caplen, counts);
190       break;
191     case WTAP_ENCAP_IEEE_802_11:
192     case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
193       capture_ieee80211(pd, 0, caplen, counts);
194       break;
195     case WTAP_ENCAP_IEEE_802_11_WLAN_RADIOTAP:
196       capture_radiotap(pd, 0, caplen, counts);
197       break;
198     case WTAP_ENCAP_CHDLC:
199       capture_chdlc(pd, 0, caplen, counts);
200       break;
201     case WTAP_ENCAP_LOCALTALK:
202       capture_llap(counts);
203       break;
204     case WTAP_ENCAP_ATM_PDUS:
205       capture_atm(pseudo_header, pd, caplen, counts);
206       break;
207     case WTAP_ENCAP_IP_OVER_FC:
208       capture_ipfc(pd, caplen, counts);
209       break;
210     case WTAP_ENCAP_ARCNET:
211       capture_arcnet(pd, caplen, counts, FALSE, TRUE);
212       break;
213     case WTAP_ENCAP_ARCNET_LINUX:
214       capture_arcnet(pd, caplen, counts, TRUE, FALSE);
215       break;
216     case WTAP_ENCAP_APPLE_IP_OVER_IEEE1394:
217       capture_ap1394(pd, 0, caplen, counts);
218       break;
219     case WTAP_ENCAP_FRELAY:
220     case WTAP_ENCAP_FRELAY_WITH_PHDR:
221       capture_fr(pd, 0, caplen, counts);
222       break;
223     case WTAP_ENCAP_ENC:
224       capture_enc(pd, caplen, counts);
225       break;
226     /* XXX - some ATM drivers on FreeBSD might prepend a 4-byte ATM
227        pseudo-header to DLT_ATM_RFC1483, with LLC header following;
228        we might have to implement that at some point. */
229   }
230 }
231
232
233 #endif /* HAVE_LIBPCAP */