1 /* capture_dissectors.c
2 * Routines for handling capture dissectors
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include "capture_dissectors.h"
20 struct capture_dissector_table {
21 GHashTable *hash_table;
25 struct capture_dissector_handle
28 capture_dissector_t dissector;
32 typedef struct capture_dissector_count
35 } capture_dissector_count_t;
37 static GHashTable *registered_dissectors = NULL;
39 static GHashTable *capture_dissector_tables = NULL;
42 destroy_capture_dissector_table(void *data)
44 struct capture_dissector_table *table = (struct capture_dissector_table *)data;
46 g_hash_table_destroy(table->hash_table);
50 void capture_dissector_init(void)
52 registered_dissectors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
53 capture_dissector_tables = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, destroy_capture_dissector_table);
56 void capture_dissector_cleanup(void)
58 g_hash_table_destroy(capture_dissector_tables);
59 g_hash_table_destroy(registered_dissectors);
62 void register_capture_dissector_table(const char *name, const char *ui_name)
64 struct capture_dissector_table* sub_dissectors;
66 /* Make sure the registration is unique */
67 if(g_hash_table_lookup( capture_dissector_tables, name )) {
68 g_error("The capture dissector table %s (%s) is already registered - are you using a buggy plugin?", name, ui_name);
71 sub_dissectors = g_new(struct capture_dissector_table, 1);
73 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash, g_direct_equal, NULL, NULL );
74 sub_dissectors->ui_name = ui_name;
75 g_hash_table_insert( capture_dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
79 static capture_dissector_handle_t
80 new_capture_dissector_handle(capture_dissector_t dissector, int proto, const char *name)
82 struct capture_dissector_handle* handle;
84 handle = wmem_new(wmem_epan_scope(), struct capture_dissector_handle);
86 handle->dissector = dissector;
87 handle->protocol = find_protocol_by_id(proto);
91 capture_dissector_handle_t
92 create_capture_dissector_handle(capture_dissector_t dissector, const int proto)
94 return new_capture_dissector_handle(dissector, proto, NULL);
97 capture_dissector_handle_t find_capture_dissector(const char *name)
99 return (capture_dissector_handle_t)g_hash_table_lookup(registered_dissectors, name);
102 capture_dissector_handle_t register_capture_dissector(const char *name, capture_dissector_t dissector, int proto)
104 capture_dissector_handle_t handle;
106 /* Make sure the registration is unique */
107 g_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
109 handle = new_capture_dissector_handle(dissector, proto, name);
110 g_hash_table_insert(registered_dissectors, (gpointer)name, handle);
114 void capture_dissector_add_uint(const char *name, const guint32 pattern, capture_dissector_handle_t handle)
116 struct capture_dissector_table* sub_dissectors;
121 /* Make sure table exists */
122 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
123 if (sub_dissectors == NULL) {
124 fprintf(stderr, "OOPS: Subdissector \"%s\" not found in capture_dissector_tables\n", name);
125 if (getenv("WIRESHARK_ABORT_ON_DISSECTOR_BUG") != NULL)
129 g_assert(sub_dissectors != NULL);
131 /* Make sure the registration is unique */
132 g_assert(g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern)) == NULL);
134 g_hash_table_insert(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern), (gpointer) handle);
137 gboolean try_capture_dissector(const char* name, const guint32 pattern, const guchar *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
139 struct capture_dissector_table* sub_dissectors;
140 capture_dissector_handle_t handle;
142 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
143 if (sub_dissectors == NULL)
149 handle = (capture_dissector_handle_t)g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
153 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
156 gboolean call_capture_dissector(capture_dissector_handle_t handle, const guchar *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
160 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
163 guint32 capture_dissector_get_count(packet_counts* counts, const int proto)
165 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(counts->counts_hash, GUINT_TO_POINTER(proto));
166 if (hash_count == NULL)
169 return hash_count->count;
172 void capture_dissector_increment_count(capture_packet_info_t *cpinfo, const int proto)
174 /* See if we already have a counter for the protocol */
175 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(cpinfo->counts, GUINT_TO_POINTER(proto));
176 if (hash_count == NULL)
178 hash_count = g_new0(capture_dissector_count_t, 1);
179 g_hash_table_insert(cpinfo->counts, GUINT_TO_POINTER(proto), (gpointer)hash_count);
186 * Editor modelines - http://www.wireshark.org/tools/modelines.html
191 * indent-tabs-mode: nil
194 * vi: set shiftwidth=4 tabstop=8 expandtab:
195 * :indentSize=4:tabSize=8:noTabs=true: