Add wtap_pseudo_header union to wtap_pkthdr structure.
[metze/wireshark/wip.git] / epan / epan.c
1 /* epan.c
2  *
3  * $Id$
4  *
5  * Wireshark Protocol Analyzer Library
6  *
7  * Copyright (c) 2001 by Gerald Combs <gerald@wireshark.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_PYTHON
27 #include <Python.h> /* to get the Python version number (PY_VERSION) */
28 #endif
29
30 #ifdef HAVE_LIBGCRYPT
31 #include <gcrypt.h>
32 #endif /* HAVE_LIBGCRYPT */
33
34 #ifdef HAVE_LIBGNUTLS
35 #include <gnutls/gnutls.h>
36 #endif /* HAVE_LIBGNUTLS */
37
38 #include <glib.h>
39 #include "epan.h"
40 #include "epan_dissect.h"
41 #include "report_err.h"
42
43 #include "conversation.h"
44 #include "circuit.h"
45 #include "except.h"
46 #include "packet.h"
47 #include "prefs.h"
48 #include "column-utils.h"
49 #include "tap.h"
50 #include "addr_resolv.h"
51 #include "oids.h"
52 #include "emem.h"
53 #include "expert.h"
54
55 #ifdef HAVE_LUA
56 #include <lua.h>
57 #include <wslua/wslua.h>
58 #endif
59
60 #ifdef HAVE_LIBSMI
61 #include <smi.h>
62 #endif
63
64 #ifdef HAVE_C_ARES
65 #include <ares_version.h>
66 #endif
67
68 const gchar*
69 epan_get_version(void) {
70         return VERSION;
71 }
72
73 void
74 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
75           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
76           register_cb cb,
77           gpointer client_data,
78           void (*report_failure_fcn_p)(const char *, va_list),
79           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
80           void (*report_read_failure_fcn_p)(const char *, int),
81           void (*report_write_failure_fcn_p)(const char *, int))
82 {
83         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
84             report_read_failure_fcn_p, report_write_failure_fcn_p);
85
86         /* initialize memory allocation subsystem */
87         emem_init();
88
89         /* initialize the GUID to name mapping table */
90         guids_init();
91
92         except_init();
93 #ifdef HAVE_LIBGCRYPT
94         /* initialize libgcrypt (beware, it won't be thread-safe) */
95         gcry_check_version(NULL);
96         gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
97         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
98 #endif
99 #ifdef HAVE_LIBGNUTLS
100         gnutls_global_init();
101 #endif
102         tap_init();
103         prefs_init();
104         proto_init(register_all_protocols_func, register_all_handoffs_func,
105             cb, client_data);
106         packet_init();
107         dfilter_init();
108         final_registration_all_protocols();
109         host_name_lookup_init();
110         expert_init();
111 #ifdef HAVE_LUA
112         wslua_init(cb, client_data);
113 #endif
114 }
115
116 void
117 epan_cleanup(void)
118 {
119         cleanup_dissection();
120         dfilter_cleanup();
121         proto_cleanup();
122         prefs_cleanup();
123         packet_cleanup();
124         oid_resolv_cleanup();
125 #ifdef HAVE_LIBGNUTLS
126         gnutls_global_deinit();
127 #endif
128         except_deinit();
129         host_name_lookup_cleanup();
130 }
131
132 void
133 epan_conversation_init(void)
134 {
135         conversation_init();
136 }
137
138 void
139 epan_conversation_cleanup(void)
140 {
141         conversation_cleanup();
142 }
143
144 void
145 epan_circuit_init(void)
146 {
147         circuit_init();
148 }
149
150 void
151 epan_circuit_cleanup(void)
152 {
153         circuit_cleanup();
154 }
155
156 epan_dissect_t*
157 epan_dissect_init(epan_dissect_t *edt, const gboolean create_proto_tree, const gboolean proto_tree_visible)
158 {
159         g_assert(edt);
160
161         if (create_proto_tree) {
162                 edt->tree = proto_tree_create_root(&edt->pi);
163                 proto_tree_set_visible(edt->tree, proto_tree_visible);
164         }
165         else {
166                 edt->tree = NULL;
167         }
168
169         edt->pi.dependent_frames = NULL;
170
171         return edt;
172 }
173
174 epan_dissect_t*
175 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
176 {
177         epan_dissect_t *edt;
178
179         edt = g_new0(epan_dissect_t, 1);
180
181         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
182 }
183
184 void
185 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
186 {
187         if (edt)
188                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
189 }
190
191 void
192 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
193         const guint8* data, frame_data *fd, column_info *cinfo)
194 {
195         /* free all memory allocated during previous packet */
196         ep_free_all();
197
198         dissect_packet(edt, phdr, data, fd, cinfo);
199 }
200
201 void
202 epan_dissect_cleanup(epan_dissect_t* edt)
203 {
204         g_assert(edt);
205
206         g_slist_free(edt->pi.dependent_frames);
207
208         /* Free the data sources list. */
209         free_data_sources(&edt->pi);
210
211         /* Free all tvb's chained from this tvb */
212         tvb_free_chain(edt->tvb);
213
214         if (edt->tree) {
215                 proto_tree_free(edt->tree);
216         }
217 }
218
219 void
220 epan_dissect_free(epan_dissect_t* edt)
221 {
222         epan_dissect_cleanup(edt);
223         g_free(edt);
224 }
225
226 void
227 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
228 {
229     dfilter_prime_proto_tree(dfcode, edt->tree);
230 }
231
232 /* ----------------------- */
233 const gchar *
234 epan_custom_set(epan_dissect_t *edt, int field_id,
235                              gint occurrence,
236                              gchar *result,
237                              gchar *expr, const int size )
238 {
239     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
240 }
241
242 void
243 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
244 {
245     col_custom_set_edt(edt, edt->pi.cinfo);
246     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
247 }
248
249 /*
250  * Get compile-time information for libraries used by libwireshark.
251  */
252 void
253 epan_get_compiled_version_info(GString *str)
254 {
255         /* SNMP */
256         g_string_append(str, ", ");
257 #ifdef HAVE_LIBSMI
258         g_string_append(str, "with SMI " SMI_VERSION_STRING);
259 #else /* no SNMP library */
260         g_string_append(str, "without SMI");
261 #endif /* _SMI_H */
262
263         /* c-ares */
264         g_string_append(str, ", ");
265 #ifdef HAVE_C_ARES
266         g_string_append(str, "with c-ares " ARES_VERSION_STR);
267 #else
268         g_string_append(str, "without c-ares");
269
270         /* ADNS - only add if no c-ares */
271         g_string_append(str, ", ");
272 #ifdef HAVE_GNU_ADNS
273         g_string_append(str, "with ADNS");
274 #else
275         g_string_append(str, "without ADNS");
276 #endif /* HAVE_GNU_ADNS */
277 #endif /* HAVE_C_ARES */
278
279         /* LUA */
280         g_string_append(str, ", ");
281 #ifdef HAVE_LUA
282         g_string_append(str, "with ");
283         g_string_append(str, LUA_VERSION);
284 #else
285         g_string_append(str, "without Lua");
286 #endif /* HAVE_LUA */
287
288         g_string_append(str, ", ");
289 #ifdef HAVE_PYTHON
290         g_string_append(str, "with Python");
291 #ifdef PY_VERSION
292         g_string_append(str, " " PY_VERSION);
293 #endif /* PY_VERSION */
294 #else
295         g_string_append(str, "without Python");
296 #endif /* HAVE_PYTHON */
297
298         /* GnuTLS */
299         g_string_append(str, ", ");
300 #ifdef HAVE_LIBGNUTLS
301         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
302 #else
303         g_string_append(str, "without GnuTLS");
304 #endif /* HAVE_LIBGNUTLS */
305
306         /* Gcrypt */
307         g_string_append(str, ", ");
308 #ifdef HAVE_LIBGCRYPT
309         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
310 #else
311         g_string_append(str, "without Gcrypt");
312 #endif /* HAVE_LIBGCRYPT */
313
314         /* Kerberos */
315         /* XXX - I don't see how to get the version number, at least for KfW */
316         g_string_append(str, ", ");
317 #ifdef HAVE_KERBEROS
318 #ifdef HAVE_MIT_KERBEROS
319         g_string_append(str, "with MIT Kerberos");
320 #else
321         /* HAVE_HEIMDAL_KERBEROS */
322         g_string_append(str, "with Heimdal Kerberos");
323 #endif
324 #else
325         g_string_append(str, "without Kerberos");
326 #endif /* HAVE_KERBEROS */
327
328         /* GeoIP */
329         g_string_append(str, ", ");
330 #ifdef HAVE_GEOIP
331         g_string_append(str, "with GeoIP");
332 #else
333         g_string_append(str, "without GeoIP");
334 #endif /* HAVE_GEOIP */
335
336 }
337
338 /*
339  * Get runtime information for libraries used by libwireshark.
340  */
341 void
342 epan_get_runtime_version_info(GString *str
343 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
344 _U_
345 #endif
346 )
347 {
348         /* GnuTLS */
349 #ifdef HAVE_LIBGNUTLS
350         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
351 #endif /* HAVE_LIBGNUTLS */
352
353         /* Gcrypt */
354 #ifdef HAVE_LIBGCRYPT
355         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
356 #endif /* HAVE_LIBGCRYPT */
357 }
358
359 /*
360  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
361  *
362  * Local variables:
363  * c-basic-offset: 8
364  * tab-width: 8
365  * indent-tabs-mode: t
366  * End:
367  *
368  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
369  * :indentSize=8:tabSize=8:noTabs=false:
370  */