3 * Wireshark Protocol Analyzer Library
5 * Copyright (c) 2001 by Gerald Combs <gerald@wireshark.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <wsutil/wsgcrypt.h>
29 #include <gnutls/gnutls.h>
30 #endif /* HAVE_LIBGNUTLS */
34 #include <wsutil/report_err.h>
36 #include <epan/exceptions.h>
40 #include "dfilter/dfilter.h"
41 #include "epan_dissect.h"
43 #include "conversation.h"
48 #include "column-utils.h"
50 #include "addr_resolv.h"
52 #include "wmem/wmem.h"
55 #include "capture_dissectors.h"
59 #include <wslua/wslua.h>
67 #include <ares_version.h>
70 static wmem_allocator_t *pinfo_pool_cache = NULL;
73 epan_get_version(void) {
78 * Register all the plugin types that are part of libwireshark, namely
79 * dissector and tap plugins.
81 * Must be called before init_plugins(), which must be called before
82 * any registration routines are called.
85 epan_register_plugin_types(void)
88 register_dissector_plugin_type();
89 register_tap_plugin_type();
94 epan_init(void (*register_all_protocols_func)(register_cb cb, gpointer client_data),
95 void (*register_all_handoffs_func)(register_cb cb, gpointer client_data),
99 volatile gboolean status = TRUE;
101 /* initialize memory allocation subsystem */
104 /* initialize the GUID to name mapping table */
107 /* initialize name resolution (addr_resolv.c) */
111 #ifdef HAVE_LIBGCRYPT
112 /* initialize libgcrypt (beware, it won't be thread-safe) */
113 gcry_check_version(NULL);
114 gcry_control (GCRYCTL_DISABLE_SECMEM, 0);
115 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
117 #ifdef HAVE_LIBGNUTLS
118 gnutls_global_init();
125 capture_dissector_init();
126 proto_init(register_all_protocols_func, register_all_handoffs_func,
128 packet_cache_proto_handles();
130 final_registration_all_protocols();
131 print_cache_field_handles();
132 expert_packet_init();
134 wslua_init(cb, client_data);
137 CATCH(DissectorError) {
139 * This is probably a dissector, or something it calls,
140 * calling REPORT_DISSECTOR_ERROR() in a registration
141 * routine or something else outside the normal dissection
144 const char *exception_message = GET_MESSAGE;
145 static const char dissector_error_nomsg[] =
146 "Dissector writer didn't bother saying what the error was";
148 report_failure("Dissector bug: %s",
149 exception_message == NULL ?
150 dissector_error_nomsg : exception_message);
151 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
167 capture_dissector_cleanup();
171 #ifdef HAVE_LIBGNUTLS
172 gnutls_global_deinit();
175 addr_resolv_cleanup();
177 if (pinfo_pool_cache != NULL) {
178 wmem_destroy_allocator(pinfo_pool_cache);
179 pinfo_pool_cache = NULL;
188 epan_t *session = g_slice_new(epan_t);
190 /* XXX, it should take session as param */
197 epan_get_user_comment(const epan_t *session, const frame_data *fd)
199 if (session->get_user_comment)
200 return session->get_user_comment(session->data, fd);
206 epan_get_interface_name(const epan_t *session, guint32 interface_id)
208 if (session->get_interface_name)
209 return session->get_interface_name(session->data, interface_id);
215 epan_get_frame_ts(const epan_t *session, guint32 frame_num)
217 const nstime_t *abs_ts = NULL;
219 if (session->get_frame_ts)
220 abs_ts = session->get_frame_ts(session->data, frame_num);
223 g_warning("!!! couldn't get frame ts for %u !!!\n", frame_num);
229 epan_free(epan_t *session)
232 /* XXX, it should take session as param */
233 cleanup_dissection();
235 g_slice_free(epan_t, session);
240 epan_conversation_init(void)
246 epan_conversation_cleanup(void)
248 conversation_cleanup();
252 epan_circuit_init(void)
258 epan_circuit_cleanup(void)
263 /* Overrides proto_tree_visible i epan_dissect_init to make all fields visible.
264 * This is > 0 if a Lua script wanted to see all fields all the time.
265 * This is ref-counted, so clearing it won't override other taps/scripts wanting it.
267 static gint always_visible_refcount = 0;
270 epan_set_always_visible(gboolean force)
273 always_visible_refcount++;
274 else if (always_visible_refcount > 0)
275 always_visible_refcount--;
279 epan_dissect_init(epan_dissect_t *edt, epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
283 edt->session = session;
285 memset(&edt->pi, 0, sizeof(edt->pi));
286 if (pinfo_pool_cache != NULL) {
287 edt->pi.pool = pinfo_pool_cache;
288 pinfo_pool_cache = NULL;
291 edt->pi.pool = wmem_allocator_new(WMEM_ALLOCATOR_BLOCK_FAST);
294 if (create_proto_tree) {
295 edt->tree = proto_tree_create_root(&edt->pi);
296 proto_tree_set_visible(edt->tree, (always_visible_refcount > 0) ? TRUE : proto_tree_visible);
308 epan_dissect_reset(epan_dissect_t *edt)
310 /* We have to preserve the pool pointer across the memzeroing */
311 wmem_allocator_t *tmp;
315 g_slist_free(edt->pi.proto_data);
316 g_slist_free(edt->pi.dependent_frames);
318 /* Free the data sources list. */
319 free_data_sources(&edt->pi);
322 /* Free all tvb's chained from this tvb */
323 tvb_free_chain(edt->tvb);
328 proto_tree_reset(edt->tree);
333 memset(&edt->pi, 0, sizeof(edt->pi));
338 epan_dissect_new(epan_t *session, const gboolean create_proto_tree, const gboolean proto_tree_visible)
342 edt = g_new0(epan_dissect_t, 1);
344 return epan_dissect_init(edt, session, create_proto_tree, proto_tree_visible);
348 epan_dissect_fake_protocols(epan_dissect_t *edt, const gboolean fake_protocols)
351 proto_tree_set_fake_protocols(edt->tree, fake_protocols);
355 epan_dissect_run(epan_dissect_t *edt, int file_type_subtype,
356 struct wtap_pkthdr *phdr, tvbuff_t *tvb, frame_data *fd,
360 wslua_prime_dfilter(edt); /* done before entering wmem scope */
362 wmem_enter_packet_scope();
363 dissect_record(edt, file_type_subtype, phdr, tvb, fd, cinfo);
365 /* free all memory allocated */
366 wmem_leave_packet_scope();
370 epan_dissect_run_with_taps(epan_dissect_t *edt, int file_type_subtype,
371 struct wtap_pkthdr *phdr, tvbuff_t *tvb, frame_data *fd,
374 wmem_enter_packet_scope();
376 dissect_record(edt, file_type_subtype, phdr, tvb, fd, cinfo);
377 tap_push_tapped_queue(edt);
379 /* free all memory allocated */
380 wmem_leave_packet_scope();
384 epan_dissect_file_run(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
385 tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
388 wslua_prime_dfilter(edt); /* done before entering wmem scope */
390 wmem_enter_packet_scope();
391 dissect_file(edt, phdr, tvb, fd, cinfo);
393 /* free all memory allocated */
394 wmem_leave_packet_scope();
398 epan_dissect_file_run_with_taps(epan_dissect_t *edt, struct wtap_pkthdr *phdr,
399 tvbuff_t *tvb, frame_data *fd, column_info *cinfo)
401 wmem_enter_packet_scope();
403 dissect_file(edt, phdr, tvb, fd, cinfo);
404 tap_push_tapped_queue(edt);
406 /* free all memory allocated */
407 wmem_leave_packet_scope();
411 epan_dissect_cleanup(epan_dissect_t* edt)
415 g_slist_free(edt->pi.proto_data);
416 g_slist_free(edt->pi.dependent_frames);
418 /* Free the data sources list. */
419 free_data_sources(&edt->pi);
422 /* Free all tvb's chained from this tvb */
423 tvb_free_chain(edt->tvb);
427 proto_tree_free(edt->tree);
430 if (pinfo_pool_cache == NULL) {
431 wmem_free_all(edt->pi.pool);
432 pinfo_pool_cache = edt->pi.pool;
435 wmem_destroy_allocator(edt->pi.pool);
440 epan_dissect_free(epan_dissect_t* edt)
442 epan_dissect_cleanup(edt);
447 epan_dissect_prime_dfilter(epan_dissect_t *edt, const dfilter_t* dfcode)
449 dfilter_prime_proto_tree(dfcode, edt->tree);
453 epan_dissect_prime_hfid(epan_dissect_t *edt, int hfid)
455 proto_tree_prime_hfid(edt->tree, hfid);
458 /* ----------------------- */
460 epan_custom_set(epan_dissect_t *edt, GSList *field_ids,
463 gchar *expr, const int size )
465 return proto_custom_set(edt->tree, field_ids, occurrence, result, expr, size);
469 epan_dissect_fill_in_columns(epan_dissect_t *edt, const gboolean fill_col_exprs, const gboolean fill_fd_colums)
471 col_custom_set_edt(edt, edt->pi.cinfo);
472 col_fill_in(&edt->pi, fill_col_exprs, fill_fd_colums);
476 epan_dissect_packet_contains_field(epan_dissect_t* edt,
477 const char *field_name)
481 gboolean contains_field;
483 if (!edt || !edt->tree)
485 field_id = proto_get_id_by_filter_name(field_name);
488 array = proto_find_finfo(edt->tree, field_id);
489 contains_field = (array->len > 0) ? TRUE : FALSE;
490 g_ptr_array_free(array, TRUE);
491 return contains_field;
495 * Get compile-time information for libraries used by libwireshark.
498 epan_get_compiled_version_info(GString *str)
501 g_string_append(str, ", ");
503 g_string_append(str, "with SMI " SMI_VERSION_STRING);
504 #else /* no SNMP library */
505 g_string_append(str, "without SMI");
509 g_string_append(str, ", ");
511 g_string_append(str, "with c-ares " ARES_VERSION_STR);
513 g_string_append(str, "without c-ares");
514 #endif /* HAVE_C_ARES */
517 g_string_append(str, ", ");
519 g_string_append(str, "with ");
520 g_string_append(str, LUA_VERSION);
522 g_string_append(str, "without Lua");
523 #endif /* HAVE_LUA */
526 g_string_append(str, ", ");
527 #ifdef HAVE_LIBGNUTLS
528 g_string_append(str, "with GnuTLS " LIBGNUTLS_VERSION);
530 g_string_append(str, "without GnuTLS");
531 #endif /* HAVE_LIBGNUTLS */
534 g_string_append(str, ", ");
535 #ifdef HAVE_LIBGCRYPT
536 g_string_append(str, "with Gcrypt " GCRYPT_VERSION);
538 g_string_append(str, "without Gcrypt");
539 #endif /* HAVE_LIBGCRYPT */
542 /* XXX - I don't see how to get the version number, at least for KfW */
543 g_string_append(str, ", ");
545 #ifdef HAVE_MIT_KERBEROS
546 g_string_append(str, "with MIT Kerberos");
548 /* HAVE_HEIMDAL_KERBEROS */
549 g_string_append(str, "with Heimdal Kerberos");
552 g_string_append(str, "without Kerberos");
553 #endif /* HAVE_KERBEROS */
556 g_string_append(str, ", ");
558 g_string_append(str, "with GeoIP");
560 g_string_append(str, "without GeoIP");
561 #endif /* HAVE_GEOIP */
566 * Get runtime information for libraries used by libwireshark.
569 epan_get_runtime_version_info(GString *str
570 #if !defined(HAVE_LIBGNUTLS) && !defined(HAVE_LIBGCRYPT)
576 #ifdef HAVE_LIBGNUTLS
577 g_string_append_printf(str, ", with GnuTLS %s", gnutls_check_version(NULL));
578 #endif /* HAVE_LIBGNUTLS */
581 #ifdef HAVE_LIBGCRYPT
582 g_string_append_printf(str, ", with Gcrypt %s", gcry_check_version(NULL));
583 #endif /* HAVE_LIBGCRYPT */
587 * Editor modelines - http://www.wireshark.org/tools/modelines.html
592 * indent-tabs-mode: t
595 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
596 * :indentSize=8:tabSize=8:noTabs=false: