attr_dissector_fn_t
[wireshark-sm.git] / epan / capture_dissectors.c
blobe91dedd4007d94bf42b0ea6010de0eef5e07c125
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
9 */
11 #include "config.h"
12 #define WS_LOG_DOMAIN LOG_DOMAIN_EPAN
14 #include <glib.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "packet.h"
19 #include "capture_dissectors.h"
20 #include <wsutil/ws_assert.h>
22 #include <wsutil/wslog.h>
24 struct capture_dissector_table {
25 GHashTable *hash_table;
26 const char *ui_name;
29 struct capture_dissector_handle
31 const char *name;
32 capture_dissector_t dissector;
33 protocol_t* protocol;
36 typedef struct capture_dissector_count
38 uint32_t count;
39 } capture_dissector_count_t;
41 static GHashTable *registered_dissectors;
43 static GHashTable *capture_dissector_tables;
45 static void
46 destroy_capture_dissector_table(void *data)
48 struct capture_dissector_table *table = (struct capture_dissector_table *)data;
50 g_hash_table_destroy(table->hash_table);
51 g_free(data);
54 void capture_dissector_init(void)
56 registered_dissectors = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, NULL);
57 capture_dissector_tables = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, destroy_capture_dissector_table);
60 void capture_dissector_cleanup(void)
62 g_hash_table_destroy(capture_dissector_tables);
63 g_hash_table_destroy(registered_dissectors);
66 void register_capture_dissector_table(const char *name, const char *ui_name)
68 struct capture_dissector_table* sub_dissectors;
70 /* Make sure the registration is unique */
71 if(g_hash_table_lookup( capture_dissector_tables, name )) {
72 ws_error("The capture dissector table %s (%s) is already registered - are you using a buggy plugin?", name, ui_name);
75 sub_dissectors = g_new(struct capture_dissector_table, 1);
77 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash, g_direct_equal, NULL, NULL );
78 sub_dissectors->ui_name = ui_name;
79 g_hash_table_insert( capture_dissector_tables, (void *)name, (void *) sub_dissectors );
83 static capture_dissector_handle_t
84 new_capture_dissector_handle(capture_dissector_t dissector, int proto, const char *name)
86 struct capture_dissector_handle* handle;
88 handle = wmem_new(wmem_epan_scope(), struct capture_dissector_handle);
89 handle->name = name;
90 handle->dissector = dissector;
91 handle->protocol = find_protocol_by_id(proto);
92 return handle;
95 capture_dissector_handle_t
96 create_capture_dissector_handle(capture_dissector_t dissector, const int proto)
98 return new_capture_dissector_handle(dissector, proto, NULL);
101 capture_dissector_handle_t find_capture_dissector(const char *name)
103 return (capture_dissector_handle_t)g_hash_table_lookup(registered_dissectors, name);
106 capture_dissector_handle_t register_capture_dissector(const char *name, capture_dissector_t dissector, int proto)
108 capture_dissector_handle_t handle;
110 /* Make sure the registration is unique */
111 ws_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
113 handle = new_capture_dissector_handle(dissector, proto, name);
114 g_hash_table_insert(registered_dissectors, (void *)name, handle);
115 return handle;
118 void capture_dissector_add_uint(const char *name, const uint32_t pattern, capture_dissector_handle_t handle)
120 struct capture_dissector_table* sub_dissectors;
122 if (handle == NULL)
123 return;
125 /* Make sure table exists */
126 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
127 if (sub_dissectors == NULL) {
128 fprintf(stderr, "OOPS: Subdissector \"%s\" not found in capture_dissector_tables\n", name);
129 if (wireshark_abort_on_dissector_bug)
130 abort();
131 return;
134 /* Make sure the registration is unique */
135 ws_assert(g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern)) == NULL);
137 g_hash_table_insert(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern), (void *) handle);
140 bool try_capture_dissector(const char* name, const uint32_t pattern, const uint8_t *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
142 struct capture_dissector_table* sub_dissectors;
143 capture_dissector_handle_t handle;
145 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
146 if (sub_dissectors == NULL)
148 /* XXX - ASSERT? */
149 return false;
152 handle = (capture_dissector_handle_t)g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
153 if (handle == NULL)
154 return false;
156 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
159 bool call_capture_dissector(capture_dissector_handle_t handle, const uint8_t *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
161 if (handle == NULL)
162 return false;
163 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
166 uint32_t capture_dissector_get_count(packet_counts* counts, const int proto)
168 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(counts->counts_hash, GINT_TO_POINTER(proto));
169 if (hash_count == NULL)
170 return 0;
172 return hash_count->count;
175 void capture_dissector_increment_count(capture_packet_info_t *cpinfo, const int proto)
177 /* See if we already have a counter for the protocol */
178 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(cpinfo->counts, GINT_TO_POINTER(proto));
179 if (hash_count == NULL)
181 hash_count = g_new0(capture_dissector_count_t, 1);
182 g_hash_table_insert(cpinfo->counts, GINT_TO_POINTER(proto), (void *)hash_count);
185 hash_count->count++;
189 * Editor modelines - https://www.wireshark.org/tools/modelines.html
191 * Local variables:
192 * c-basic-offset: 4
193 * tab-width: 8
194 * indent-tabs-mode: nil
195 * End:
197 * vi: set shiftwidth=4 tabstop=8 expandtab:
198 * :indentSize=4:tabSize=8:noTabs=true: