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