1 /* capture_dissectors.c
2 * Routines for handling capture dissectors
3 *
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11 #include "config.h"
12 #define WS_LOG_DOMAIN LOG_DOMAIN_EPAN
13
14 #include <glib.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "packet.h"
18
19 #include "capture_dissectors.h"
20 #include <wsutil/ws_assert.h>
21
22 #include <wsutil/wslog.h>
23
24 struct capture_dissector_table {
25 GHashTable *hash_table;
26 const char *ui_name;
27 };
28
29 struct capture_dissector_handle
30 {
31 const char *name;
32 capture_dissector_t dissector;
33 protocol_t* protocol;
34 };
35
36 typedef struct capture_dissector_count
37 {
38 guint32 count;
39 } capture_dissector_count_t;
40
41 static GHashTable *registered_dissectors = NULL;
42
43 static GHashTable *capture_dissector_tables = NULL;
44
45 static void
destroy_capture_dissector_table(void * data)46 destroy_capture_dissector_table(void *data)
47 {
48 struct capture_dissector_table *table = (struct capture_dissector_table *)data;
49
50 g_hash_table_destroy(table->hash_table);
51 g_free(data);
52 }
53
capture_dissector_init(void)54 void capture_dissector_init(void)
55 {
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);
58 }
59
capture_dissector_cleanup(void)60 void capture_dissector_cleanup(void)
61 {
62 g_hash_table_destroy(capture_dissector_tables);
63 g_hash_table_destroy(registered_dissectors);
64 }
65
register_capture_dissector_table(const char * name,const char * ui_name)66 void register_capture_dissector_table(const char *name, const char *ui_name)
67 {
68 struct capture_dissector_table* sub_dissectors;
69
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);
73 }
74
75 sub_dissectors = g_new(struct capture_dissector_table, 1);
76
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, (gpointer)name, (gpointer) sub_dissectors );
80
81 }
82
83 static capture_dissector_handle_t
new_capture_dissector_handle(capture_dissector_t dissector,int proto,const char * name)84 new_capture_dissector_handle(capture_dissector_t dissector, int proto, const char *name)
85 {
86 struct capture_dissector_handle* handle;
87
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;
93 }
94
95 capture_dissector_handle_t
create_capture_dissector_handle(capture_dissector_t dissector,const int proto)96 create_capture_dissector_handle(capture_dissector_t dissector, const int proto)
97 {
98 return new_capture_dissector_handle(dissector, proto, NULL);
99 }
100
find_capture_dissector(const char * name)101 capture_dissector_handle_t find_capture_dissector(const char *name)
102 {
103 return (capture_dissector_handle_t)g_hash_table_lookup(registered_dissectors, name);
104 }
105
register_capture_dissector(const char * name,capture_dissector_t dissector,int proto)106 capture_dissector_handle_t register_capture_dissector(const char *name, capture_dissector_t dissector, int proto)
107 {
108 capture_dissector_handle_t handle;
109
110 /* Make sure the registration is unique */
111 ws_assert(g_hash_table_lookup(registered_dissectors, name) == NULL);
112
113 handle = new_capture_dissector_handle(dissector, proto, name);
114 g_hash_table_insert(registered_dissectors, (gpointer)name, handle);
115 return handle;
116 }
117
capture_dissector_add_uint(const char * name,const guint32 pattern,capture_dissector_handle_t handle)118 void capture_dissector_add_uint(const char *name, const guint32 pattern, capture_dissector_handle_t handle)
119 {
120 struct capture_dissector_table* sub_dissectors;
121
122 if (handle == NULL)
123 return;
124
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;
132 }
133
134 /* Make sure the registration is unique */
135 ws_assert(g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern)) == NULL);
136
137 g_hash_table_insert(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern), (gpointer) handle);
138 }
139
try_capture_dissector(const char * name,const guint32 pattern,const guint8 * pd,int offset,int len,capture_packet_info_t * cpinfo,const union wtap_pseudo_header * pseudo_header)140 gboolean try_capture_dissector(const char* name, const guint32 pattern, const guint8 *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
141 {
142 struct capture_dissector_table* sub_dissectors;
143 capture_dissector_handle_t handle;
144
145 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
146 if (sub_dissectors == NULL)
147 {
148 /* XXX - ASSERT? */
149 return FALSE;
150 }
151
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;
155
156 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
157 }
158
call_capture_dissector(capture_dissector_handle_t handle,const guint8 * pd,int offset,int len,capture_packet_info_t * cpinfo,const union wtap_pseudo_header * pseudo_header)159 gboolean call_capture_dissector(capture_dissector_handle_t handle, const guint8 *pd, int offset, int len, capture_packet_info_t *cpinfo, const union wtap_pseudo_header *pseudo_header)
160 {
161 if (handle == NULL)
162 return FALSE;
163 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
164 }
165
capture_dissector_get_count(packet_counts * counts,const int proto)166 guint32 capture_dissector_get_count(packet_counts* counts, const int proto)
167 {
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;
171
172 return hash_count->count;
173 }
174
capture_dissector_increment_count(capture_packet_info_t * cpinfo,const int proto)175 void capture_dissector_increment_count(capture_packet_info_t *cpinfo, const int proto)
176 {
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)
180 {
181 hash_count = g_new0(capture_dissector_count_t, 1);
182 g_hash_table_insert(cpinfo->counts, GINT_TO_POINTER(proto), (gpointer)hash_count);
183 }
184
185 hash_count->count++;
186 }
187
188 /*
189 * Editor modelines - https://www.wireshark.org/tools/modelines.html
190 *
191 * Local variables:
192 * c-basic-offset: 4
193 * tab-width: 8
194 * indent-tabs-mode: nil
195 * End:
196 *
197 * vi: set shiftwidth=4 tabstop=8 expandtab:
198 * :indentSize=4:tabSize=8:noTabs=true:
199 */
200