Create init and cleanup functions for wmem as a whole.
[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 "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         if (create_proto_tree) {
165                 edt->tree = proto_tree_create_root(&edt->pi);
166                 proto_tree_set_visible(edt->tree, proto_tree_visible);
167         }
168         else {
169                 edt->tree = NULL;
170         }
171
172         edt->pi.dependent_frames = NULL;
173
174         return edt;
175 }
176
177 epan_dissect_t*
178 epan_dissect_new(const gboolean create_proto_tree, const gboolean proto_tree_visible)
179 {
180         epan_dissect_t *edt;
181
182         edt = g_new0(epan_dissect_t, 1);
183
184         return epan_dissect_init(edt, create_proto_tree, proto_tree_visible);
185 }
186
187 void
188 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
189 {
190         if (edt)
191                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
192 }
193
194 void
195 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
196         const guint8* data, frame_data *fd, column_info *cinfo)
197 {
198         dissect_packet(edt, phdr, data, fd, cinfo);
199
200         /* free all memory allocated */
201         ep_free_all();
202 }
203
204 void
205 epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
206         const guint8* data, frame_data *fd, column_info *cinfo)
207 {
208         tap_queue_init(edt);
209         dissect_packet(edt, phdr, data, fd, cinfo);
210         tap_push_tapped_queue(edt);
211
212         /* free all memory allocated */
213         ep_free_all();
214 }
215
216 void
217 epan_dissect_reset(epan_dissect_t *edt)
218 {
219         /* epan_dissect_cleanup(edt) without freeing tree */
220         g_slist_free(edt->pi.dependent_frames);
221         free_data_sources(&edt->pi);
222         tvb_free_chain(edt->tvb);
223
224         /* epan_dissect_init(edt, create_proto, visible_proto) */
225         edt->pi.dependent_frames = NULL;
226         if (edt->tree)
227                 proto_tree_reset(edt->tree);
228 }
229
230 void
231 epan_dissect_cleanup(epan_dissect_t* edt)
232 {
233         g_assert(edt);
234
235         g_slist_free(edt->pi.dependent_frames);
236
237         /* Free the data sources list. */
238         free_data_sources(&edt->pi);
239
240         /* Free all tvb's chained from this tvb */
241         tvb_free_chain(edt->tvb);
242
243         if (edt->tree) {
244                 proto_tree_free(edt->tree);
245         }
246 }
247
248 void
249 epan_dissect_free(epan_dissect_t* edt)
250 {
251         epan_dissect_cleanup(edt);
252         g_free(edt);
253 }
254
255 void
256 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
257 {
258     dfilter_prime_proto_tree(dfcode, edt->tree);
259 }
260
261 /* ----------------------- */
262 const gchar *
263 epan_custom_set(epan_dissect_t *edt, int field_id,
264                              gint occurrence,
265                              gchar *result,
266                              gchar *expr, const int size )
267 {
268     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
269 }
270
271 void
272 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
273 {
274     col_custom_set_edt(edt, edt->pi.cinfo);
275     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
276 }
277
278 /*
279  * Get compile-time information for libraries used by libwireshark.
280  */
281 void
282 epan_get_compiled_version_info(GString *str)
283 {
284         /* SNMP */
285         g_string_append(str, ", ");
286 #ifdef HAVE_LIBSMI
287         g_string_append(str, "with SMI " SMI_VERSION_STRING);
288 #else /* no SNMP library */
289         g_string_append(str, "without SMI");
290 #endif /* _SMI_H */
291
292         /* c-ares */
293         g_string_append(str, ", ");
294 #ifdef HAVE_C_ARES
295         g_string_append(str, "with c-ares " ARES_VERSION_STR);
296 #else
297         g_string_append(str, "without c-ares");
298
299         /* ADNS - only add if no c-ares */
300         g_string_append(str, ", ");
301 #ifdef HAVE_GNU_ADNS
302         g_string_append(str, "with ADNS");
303 #else
304         g_string_append(str, "without ADNS");
305 #endif /* HAVE_GNU_ADNS */
306 #endif /* HAVE_C_ARES */
307
308         /* LUA */
309         g_string_append(str, ", ");
310 #ifdef HAVE_LUA
311         g_string_append(str, "with ");
312         g_string_append(str, LUA_VERSION);
313 #else
314         g_string_append(str, "without Lua");
315 #endif /* HAVE_LUA */
316
317         g_string_append(str, ", ");
318 #ifdef HAVE_PYTHON
319         g_string_append(str, "with Python");
320 #ifdef PY_VERSION
321         g_string_append(str, " " PY_VERSION);
322 #endif /* PY_VERSION */
323 #else
324         g_string_append(str, "without Python");
325 #endif /* HAVE_PYTHON */
326
327         /* GnuTLS */
328         g_string_append(str, ", ");
329 #ifdef HAVE_LIBGNUTLS
330         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
331 #else
332         g_string_append(str, "without GnuTLS");
333 #endif /* HAVE_LIBGNUTLS */
334
335         /* Gcrypt */
336         g_string_append(str, ", ");
337 #ifdef HAVE_LIBGCRYPT
338         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
339 #else
340         g_string_append(str, "without Gcrypt");
341 #endif /* HAVE_LIBGCRYPT */
342
343         /* Kerberos */
344         /* XXX - I don't see how to get the version number, at least for KfW */
345         g_string_append(str, ", ");
346 #ifdef HAVE_KERBEROS
347 #ifdef HAVE_MIT_KERBEROS
348         g_string_append(str, "with MIT Kerberos");
349 #else
350         /* HAVE_HEIMDAL_KERBEROS */
351         g_string_append(str, "with Heimdal Kerberos");
352 #endif
353 #else
354         g_string_append(str, "without Kerberos");
355 #endif /* HAVE_KERBEROS */
356
357         /* GeoIP */
358         g_string_append(str, ", ");
359 #ifdef HAVE_GEOIP
360         g_string_append(str, "with GeoIP");
361 #else
362         g_string_append(str, "without GeoIP");
363 #endif /* HAVE_GEOIP */
364
365 }
366
367 /*
368  * Get runtime information for libraries used by libwireshark.
369  */
370 void
371 epan_get_runtime_version_info(GString *str
372 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
373 _U_
374 #endif
375 )
376 {
377         /* GnuTLS */
378 #ifdef HAVE_LIBGNUTLS
379         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
380 #endif /* HAVE_LIBGNUTLS */
381
382         /* Gcrypt */
383 #ifdef HAVE_LIBGCRYPT
384         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
385 #endif /* HAVE_LIBGCRYPT */
386 }
387
388 /*
389  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
390  *
391  * Local variables:
392  * c-basic-offset: 8
393  * tab-width: 8
394  * indent-tabs-mode: t
395  * End:
396  *
397  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
398  * :indentSize=8:tabSize=8:noTabs=false:
399  */