include the wsutil/wsgcrypt.h instead of including gcrypt.h directly
[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 <wsutil/wsgcrypt.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 "wmem/wmem.h"
54 #include "expert.h"
55
56 #ifdef HAVE_LUA
57 #include <lua.h>
58 #include <wslua/wslua.h>
59 #endif
60
61 #ifdef HAVE_LIBSMI
62 #include <smi.h>
63 #endif
64
65 #ifdef HAVE_C_ARES
66 #include <ares_version.h>
67 #endif
68
69 const gchar*
70 epan_get_version(void) {
71         return VERSION;
72 }
73
74 void
75 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
76           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
77           register_cb cb,
78           gpointer client_data,
79           void (*report_failure_fcn_p)(const char *, va_list),
80           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
81           void (*report_read_failure_fcn_p)(const char *, int),
82           void (*report_write_failure_fcn_p)(const char *, int))
83 {
84         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
85             report_read_failure_fcn_p, report_write_failure_fcn_p);
86
87         /* initialize memory allocation subsystems */
88         emem_init();
89         wmem_init();
90
91         /* initialize the GUID to name mapping table */
92         guids_init();
93
94         except_init();
95 #ifdef HAVE_LIBGCRYPT
96         /* initialize libgcrypt (beware, it won't be thread-safe) */
97         gcry_check_version(NULL);
98         gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
99         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
100 #endif
101 #ifdef HAVE_LIBGNUTLS
102         gnutls_global_init();
103 #endif
104         tap_init();
105         prefs_init();
106         proto_init(register_all_protocols_func, register_all_handoffs_func,
107             cb, client_data);
108         packet_init();
109         dfilter_init();
110         final_registration_all_protocols();
111         host_name_lookup_init();
112         expert_init();
113 #ifdef HAVE_LUA
114         wslua_init(cb, client_data);
115 #endif
116 }
117
118 void
119 epan_cleanup(void)
120 {
121         cleanup_dissection();
122         dfilter_cleanup();
123         proto_cleanup();
124         prefs_cleanup();
125         packet_cleanup();
126         oid_resolv_cleanup();
127 #ifdef HAVE_LIBGNUTLS
128         gnutls_global_deinit();
129 #endif
130         except_deinit();
131         host_name_lookup_cleanup();
132         wmem_cleanup();
133 }
134
135 void
136 epan_conversation_init(void)
137 {
138         conversation_init();
139 }
140
141 void
142 epan_conversation_cleanup(void)
143 {
144         conversation_cleanup();
145 }
146
147 void
148 epan_circuit_init(void)
149 {
150         circuit_init();
151 }
152
153 void
154 epan_circuit_cleanup(void)
155 {
156         circuit_cleanup();
157 }
158
159 epan_dissect_t*
160 epan_dissect_init(epan_dissect_t *edt, const gboolean create_proto_tree, const gboolean proto_tree_visible)
161 {
162         g_assert(edt);
163
164         edt->pi.pool = wmem_allocator_new(WMEM_ALLOCATOR_SIMPLE);
165
166         if (create_proto_tree) {
167                 edt->tree = proto_tree_create_root(&edt->pi);
168                 proto_tree_set_visible(edt->tree, proto_tree_visible);
169         }
170         else {
171                 edt->tree = NULL;
172         }
173
174         edt->pi.dependent_frames = NULL;
175
176         return edt;
177 }
178
179 epan_dissect_t*
180 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
181 {
182         epan_dissect_t *edt;
183
184         edt = g_new0(epan_dissect_t, 1);
185
186         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
187 }
188
189 void
190 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
191 {
192         if (edt)
193                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
194 }
195
196 void
197 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
198         const guint8* data, frame_data *fd, column_info *cinfo)
199 {
200         wmem_enter_packet_scope();
201         dissect_packet(edt, phdr, data, fd, cinfo);
202
203         /* free all memory allocated */
204         ep_free_all();
205         wmem_leave_packet_scope();
206 }
207
208 void
209 epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
210         const guint8* data, frame_data *fd, column_info *cinfo)
211 {
212         wmem_enter_packet_scope();
213         tap_queue_init(edt);
214         dissect_packet(edt, phdr, data, fd, cinfo);
215         tap_push_tapped_queue(edt);
216
217         /* free all memory allocated */
218         ep_free_all();
219         wmem_leave_packet_scope();
220 }
221
222 void
223 epan_dissect_cleanup(epan_dissect_t* edt)
224 {
225         g_assert(edt);
226
227         g_slist_free(edt->pi.dependent_frames);
228
229         /* Free the data sources list. */
230         free_data_sources(&edt->pi);
231
232         /* Free all tvb's chained from this tvb */
233         tvb_free_chain(edt->tvb);
234
235         if (edt->tree) {
236                 proto_tree_free(edt->tree);
237         }
238
239         wmem_destroy_allocator(edt->pi.pool);
240 }
241
242 void
243 epan_dissect_free(epan_dissect_t* edt)
244 {
245         epan_dissect_cleanup(edt);
246         g_free(edt);
247 }
248
249 void
250 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
251 {
252     dfilter_prime_proto_tree(dfcode, edt->tree);
253 }
254
255 /* ----------------------- */
256 const gchar *
257 epan_custom_set(epan_dissect_t *edt, int field_id,
258                              gint occurrence,
259                              gchar *result,
260                              gchar *expr, const int size )
261 {
262     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
263 }
264
265 void
266 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
267 {
268     col_custom_set_edt(edt, edt->pi.cinfo);
269     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
270 }
271
272 /*
273  * Get compile-time information for libraries used by libwireshark.
274  */
275 void
276 epan_get_compiled_version_info(GString *str)
277 {
278         /* SNMP */
279         g_string_append(str, ", ");
280 #ifdef HAVE_LIBSMI
281         g_string_append(str, "with SMI " SMI_VERSION_STRING);
282 #else /* no SNMP library */
283         g_string_append(str, "without SMI");
284 #endif /* _SMI_H */
285
286         /* c-ares */
287         g_string_append(str, ", ");
288 #ifdef HAVE_C_ARES
289         g_string_append(str, "with c-ares " ARES_VERSION_STR);
290 #else
291         g_string_append(str, "without c-ares");
292
293         /* ADNS - only add if no c-ares */
294         g_string_append(str, ", ");
295 #ifdef HAVE_GNU_ADNS
296         g_string_append(str, "with ADNS");
297 #else
298         g_string_append(str, "without ADNS");
299 #endif /* HAVE_GNU_ADNS */
300 #endif /* HAVE_C_ARES */
301
302         /* LUA */
303         g_string_append(str, ", ");
304 #ifdef HAVE_LUA
305         g_string_append(str, "with ");
306         g_string_append(str, LUA_VERSION);
307 #else
308         g_string_append(str, "without Lua");
309 #endif /* HAVE_LUA */
310
311         g_string_append(str, ", ");
312 #ifdef HAVE_PYTHON
313         g_string_append(str, "with Python");
314 #ifdef PY_VERSION
315         g_string_append(str, " " PY_VERSION);
316 #endif /* PY_VERSION */
317 #else
318         g_string_append(str, "without Python");
319 #endif /* HAVE_PYTHON */
320
321         /* GnuTLS */
322         g_string_append(str, ", ");
323 #ifdef HAVE_LIBGNUTLS
324         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
325 #else
326         g_string_append(str, "without GnuTLS");
327 #endif /* HAVE_LIBGNUTLS */
328
329         /* Gcrypt */
330         g_string_append(str, ", ");
331 #ifdef HAVE_LIBGCRYPT
332         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
333 #else
334         g_string_append(str, "without Gcrypt");
335 #endif /* HAVE_LIBGCRYPT */
336
337         /* Kerberos */
338         /* XXX - I don't see how to get the version number, at least for KfW */
339         g_string_append(str, ", ");
340 #ifdef HAVE_KERBEROS
341 #ifdef HAVE_MIT_KERBEROS
342         g_string_append(str, "with MIT Kerberos");
343 #else
344         /* HAVE_HEIMDAL_KERBEROS */
345         g_string_append(str, "with Heimdal Kerberos");
346 #endif
347 #else
348         g_string_append(str, "without Kerberos");
349 #endif /* HAVE_KERBEROS */
350
351         /* GeoIP */
352         g_string_append(str, ", ");
353 #ifdef HAVE_GEOIP
354         g_string_append(str, "with GeoIP");
355 #else
356         g_string_append(str, "without GeoIP");
357 #endif /* HAVE_GEOIP */
358
359 }
360
361 /*
362  * Get runtime information for libraries used by libwireshark.
363  */
364 void
365 epan_get_runtime_version_info(GString *str
366 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
367 _U_
368 #endif
369 )
370 {
371         /* GnuTLS */
372 #ifdef HAVE_LIBGNUTLS
373         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
374 #endif /* HAVE_LIBGNUTLS */
375
376         /* Gcrypt */
377 #ifdef HAVE_LIBGCRYPT
378         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
379 #endif /* HAVE_LIBGCRYPT */
380 }
381
382 /*
383  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
384  *
385  * Local variables:
386  * c-basic-offset: 8
387  * tab-width: 8
388  * indent-tabs-mode: t
389  * End:
390  *
391  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
392  * :indentSize=8:tabSize=8:noTabs=false:
393  */