Effectively remove the tree memory pool and port some of its behaviour to the
[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-int.h"
40 #include "epan.h"
41 #include "epan_dissect.h"
42 #include "wsutil/report_err.h"
43
44 #include "conversation.h"
45 #include "circuit.h"
46 #include "except.h"
47 #include "packet.h"
48 #include "prefs.h"
49 #include "column-utils.h"
50 #include "tap.h"
51 #include "addr_resolv.h"
52 #include "oids.h"
53 #include "emem.h"
54 #include "wmem/wmem.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 static wmem_allocator_t *pinfo_pool_cache = NULL;
71
72 const gchar*
73 epan_get_version(void) {
74         return VERSION;
75 }
76
77 void
78 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
79           void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
80           register_cb cb,
81           gpointer client_data,
82           void (*report_failure_fcn_p)(const char *, va_list),
83           void (*report_open_failure_fcn_p)(const char *, int, gboolean),
84           void (*report_read_failure_fcn_p)(const char *, int),
85           void (*report_write_failure_fcn_p)(const char *, int))
86 {
87         init_report_err(report_failure_fcn_p, report_open_failure_fcn_p,
88             report_read_failure_fcn_p, report_write_failure_fcn_p);
89
90         /* initialize memory allocation subsystems */
91         emem_init();
92         wmem_init();
93
94         /* initialize the GUID to name mapping table */
95         guids_init();
96
97         /* initialize name resolution (addr_resolv.c) */
98         addr_resolv_init();
99
100         except_init();
101 #ifdef HAVE_LIBGCRYPT
102         /* initialize libgcrypt (beware, it won't be thread-safe) */
103         gcry_check_version(NULL);
104         gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
105         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
106 #endif
107 #ifdef HAVE_LIBGNUTLS
108         gnutls_global_init();
109 #endif
110         tap_init();
111         prefs_init();
112         expert_init();
113         packet_init();
114         proto_init(register_all_protocols_func, register_all_handoffs_func,
115             cb, client_data);
116         packet_cache_proto_handles();
117         dfilter_init();
118         final_registration_all_protocols();
119         expert_packet_init();
120 #ifdef HAVE_LUA
121         wslua_init(cb, client_data);
122 #endif
123 }
124
125 void
126 epan_cleanup(void)
127 {
128         dfilter_cleanup();
129         proto_cleanup();
130         prefs_cleanup();
131         packet_cleanup();
132         expert_cleanup();
133 #ifdef HAVE_LUA
134         wslua_cleanup();
135 #endif
136 #ifdef HAVE_LIBGNUTLS
137         gnutls_global_deinit();
138 #endif
139         except_deinit();
140         addr_resolv_cleanup();
141         wmem_cleanup();
142 }
143
144 epan_t *
145 epan_new(void)
146 {
147         epan_t *session = g_slice_new(epan_t);
148
149         /* XXX, it should take session as param */
150         init_dissection();
151
152         return session;
153 }
154
155 const char *
156 epan_get_user_comment(const epan_t *session, const frame_data *fd)
157 {
158         if (session->get_user_comment)
159                 return session->get_user_comment(session->data, fd);
160
161         return NULL;
162 }
163
164 const char *
165 epan_get_interface_name(const epan_t *session, guint32 interface_id)
166 {
167         if (session->get_interface_name)
168                 return session->get_interface_name(session->data, interface_id);
169
170         return NULL;
171 }
172
173 const nstime_t *
174 epan_get_frame_ts(const epan_t *session, guint32 frame_num)
175 {
176         const nstime_t *abs_ts = NULL;
177
178         if (session->get_frame_ts)
179                 abs_ts = session->get_frame_ts(session->data, frame_num);
180
181         if (!abs_ts)
182                 g_warning("!!! couldn't get frame ts for %u !!!\n", frame_num);
183
184         return abs_ts;
185 }
186
187 void
188 epan_free(epan_t *session)
189 {
190         if (session) {
191                 /* XXX, it should take session as param */
192                 cleanup_dissection();
193
194                 g_slice_free(epan_t, session);
195         }
196 }
197
198 void
199 epan_conversation_init(void)
200 {
201         conversation_init();
202 }
203
204 void
205 epan_conversation_cleanup(void)
206 {
207         conversation_cleanup();
208 }
209
210 void
211 epan_circuit_init(void)
212 {
213         circuit_init();
214 }
215
216 void
217 epan_circuit_cleanup(void)
218 {
219         circuit_cleanup();
220 }
221
222 epan_dissect_t*
223 epan_dissect_init(epan_dissect_t *edt, epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
224 {
225         g_assert(edt);
226
227         edt->session = session;
228
229         memset(&edt->pi, 0, sizeof(edt->pi));
230         if (pinfo_pool_cache != NULL) {
231                 edt->pi.pool = pinfo_pool_cache;
232                 pinfo_pool_cache = NULL;
233         }
234         else {
235                 edt->pi.pool = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK);
236         }
237
238         if (create_proto_tree) {
239                 edt->tree = proto_tree_create_root(&edt->pi);
240                 proto_tree_set_visible(edt->tree, proto_tree_visible);
241         }
242         else {
243                 edt->tree = NULL;
244         }
245
246         edt->tvb = NULL;
247
248         return edt;
249 }
250
251 void
252 epan_dissect_reset(epan_dissect_t *edt)
253 {
254         /* We have to preserve the pool pointer across the memzeroing */
255         wmem_allocator_t *tmp;
256
257         g_assert(edt);
258
259         g_slist_free(edt->pi.dependent_frames);
260
261         /* Free the data sources list. */
262         free_data_sources(&edt->pi);
263
264         if (edt->tvb) {
265                 /* Free all tvb's chained from this tvb */
266                 tvb_free_chain(edt->tvb);
267                 edt->tvb = NULL;
268         }
269
270         if (edt->tree)
271                 proto_tree_reset(edt->tree);
272
273         tmp = edt->pi.pool;
274         wmem_free_all(tmp);
275
276         memset(&edt->pi, 0, sizeof(edt->pi));
277         edt->pi.pool = tmp;
278 }
279
280 epan_dissect_t*
281 epan_dissect_new(epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
282 {
283         epan_dissect_t *edt;
284
285         edt = g_new0(epan_dissect_t, 1);
286
287         return epan_dissect_init(edt, session, create_proto_tree, proto_tree_visible);
288 }
289
290 void
291 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
292 {
293         if (edt)
294                 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
295 }
296
297 void
298 epan_dissect_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
299         tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
300 {
301 #ifdef HAVE_LUA
302         wslua_prime_dfilter(edt); /* done before entering wmem scope */
303 #endif
304         wmem_enter_packet_scope();
305         dissect_packet(edt, phdr, tvb, fd, cinfo);
306
307         /* free all memory allocated */
308         ep_free_all();
309         wmem_leave_packet_scope();
310 }
311
312 void
313 epan_dissect_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
314         tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
315 {
316         wmem_enter_packet_scope();
317         tap_queue_init(edt);
318         dissect_packet(edt, phdr, tvb, fd, cinfo);
319         tap_push_tapped_queue(edt);
320
321         /* free all memory allocated */
322         ep_free_all();
323         wmem_leave_packet_scope();
324 }
325
326 void
327 epan_dissect_cleanup(epan_dissect_t* edt)
328 {
329         g_assert(edt);
330
331         g_slist_free(edt->pi.dependent_frames);
332
333         /* Free the data sources list. */
334         free_data_sources(&edt->pi);
335
336         if (edt->tvb) {
337                 /* Free all tvb's chained from this tvb */
338                 tvb_free_chain(edt->tvb);
339         }
340
341         if (edt->tree) {
342                 proto_tree_free(edt->tree);
343         }
344
345         if (pinfo_pool_cache == NULL) {
346                 wmem_free_all(edt->pi.pool);
347                 pinfo_pool_cache = edt->pi.pool;
348         }
349         else {
350                 wmem_destroy_allocator(edt->pi.pool);
351         }
352 }
353
354 void
355 epan_dissect_free(epan_dissect_t* edt)
356 {
357         epan_dissect_cleanup(edt);
358         g_free(edt);
359 }
360
361 void
362 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
363 {
364     dfilter_prime_proto_tree(dfcode, edt->tree);
365 }
366
367 /* ----------------------- */
368 const gchar *
369 epan_custom_set(epan_dissect_t *edt, int field_id,
370                              gint occurrence,
371                              gchar *result,
372                              gchar *expr, const int size )
373 {
374     return proto_custom_set(edt->tree, field_id, occurrence, result, expr, size);
375 }
376
377 void
378 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
379 {
380     col_custom_set_edt(edt, edt->pi.cinfo);
381     col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
382 }
383
384 gboolean
385 epan_dissect_packet_contains_field(epan_dissect_t* edt,
386                                    const char *field_name)
387 {
388     GPtrArray* array;
389     int        field_id;
390     gboolean   contains_field;
391
392     if (!edt || !edt->tree)
393         return FALSE;
394     field_id = proto_get_id_by_filter_name(field_name);
395     if (field_id < 0)
396         return FALSE;
397     array = proto_find_finfo(edt->tree, field_id);
398     contains_field = (array->len > 0) ? TRUE : FALSE;
399     g_ptr_array_free(array, TRUE);
400     return contains_field;
401 }
402
403 /*
404  * Get compile-time information for libraries used by libwireshark.
405  */
406 void
407 epan_get_compiled_version_info(GString *str)
408 {
409         /* SNMP */
410         g_string_append(str, ", ");
411 #ifdef HAVE_LIBSMI
412         g_string_append(str, "with SMI " SMI_VERSION_STRING);
413 #else /* no SNMP library */
414         g_string_append(str, "without SMI");
415 #endif /* _SMI_H */
416
417         /* c-ares */
418         g_string_append(str, ", ");
419 #ifdef HAVE_C_ARES
420         g_string_append(str, "with c-ares " ARES_VERSION_STR);
421 #else
422         g_string_append(str, "without c-ares");
423
424         /* ADNS - only add if no c-ares */
425         g_string_append(str, ", ");
426 #ifdef HAVE_GNU_ADNS
427         g_string_append(str, "with ADNS");
428 #else
429         g_string_append(str, "without ADNS");
430 #endif /* HAVE_GNU_ADNS */
431 #endif /* HAVE_C_ARES */
432
433         /* LUA */
434         g_string_append(str, ", ");
435 #ifdef HAVE_LUA
436         g_string_append(str, "with ");
437         g_string_append(str, LUA_VERSION);
438 #else
439         g_string_append(str, "without Lua");
440 #endif /* HAVE_LUA */
441
442         g_string_append(str, ", ");
443 #ifdef HAVE_PYTHON
444         g_string_append(str, "with Python");
445 #ifdef PY_VERSION
446         g_string_append(str, " " PY_VERSION);
447 #endif /* PY_VERSION */
448 #else
449         g_string_append(str, "without Python");
450 #endif /* HAVE_PYTHON */
451
452         /* GnuTLS */
453         g_string_append(str, ", ");
454 #ifdef HAVE_LIBGNUTLS
455         g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
456 #else
457         g_string_append(str, "without GnuTLS");
458 #endif /* HAVE_LIBGNUTLS */
459
460         /* Gcrypt */
461         g_string_append(str, ", ");
462 #ifdef HAVE_LIBGCRYPT
463         g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
464 #else
465         g_string_append(str, "without Gcrypt");
466 #endif /* HAVE_LIBGCRYPT */
467
468         /* Kerberos */
469         /* XXX - I don't see how to get the version number, at least for KfW */
470         g_string_append(str, ", ");
471 #ifdef HAVE_KERBEROS
472 #ifdef HAVE_MIT_KERBEROS
473         g_string_append(str, "with MIT Kerberos");
474 #else
475         /* HAVE_HEIMDAL_KERBEROS */
476         g_string_append(str, "with Heimdal Kerberos");
477 #endif
478 #else
479         g_string_append(str, "without Kerberos");
480 #endif /* HAVE_KERBEROS */
481
482         /* GeoIP */
483         g_string_append(str, ", ");
484 #ifdef HAVE_GEOIP
485         g_string_append(str, "with GeoIP");
486 #else
487         g_string_append(str, "without GeoIP");
488 #endif /* HAVE_GEOIP */
489
490 }
491
492 /*
493  * Get runtime information for libraries used by libwireshark.
494  */
495 void
496 epan_get_runtime_version_info(GString *str
497 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
498 _U_
499 #endif
500 )
501 {
502         /* GnuTLS */
503 #ifdef HAVE_LIBGNUTLS
504         g_string_append_printf(str, ", GnuTLS %s", gnutls_check_version(NULL));
505 #endif /* HAVE_LIBGNUTLS */
506
507         /* Gcrypt */
508 #ifdef HAVE_LIBGCRYPT
509         g_string_append_printf(str, ", Gcrypt %s", gcry_check_version(NULL));
510 #endif /* HAVE_LIBGCRYPT */
511 }
512
513 /*
514  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
515  *
516  * Local variables:
517  * c-basic-offset: 8
518  * tab-width: 8
519  * indent-tabs-mode: t
520  * End:
521  *
522  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
523  * :indentSize=8:tabSize=8:noTabs=false:
524  */