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 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "capture_dissectors.h"
30 struct capture_dissector_table {
31 GHashTable *hash_table;
35 struct capture_dissector_handle
38 capture_dissector_t dissector;
42 typedef struct capture_dissector_count
45 } capture_dissector_count_t;
47 static GHashTable *capture_dissector_tables = NULL;
50 destroy_capture_dissector_table(void *data)
52 struct capture_dissector_table *table = (struct capture_dissector_table *)data;
54 g_hash_table_destroy(table->hash_table);
58 void capture_dissector_init(void)
60 capture_dissector_tables = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, destroy_capture_dissector_table);
63 void capture_dissector_cleanup(void)
65 g_hash_table_destroy(capture_dissector_tables);
68 void register_capture_dissector_table(const char *name, const char *ui_name)
70 struct capture_dissector_table* sub_dissectors;
72 /* Make sure the registration is unique */
73 if(g_hash_table_lookup( capture_dissector_tables, name )) {
74 g_error("The capture dissector table %s (%s) is already registered - are you using a buggy plugin?", name, ui_name);
77 sub_dissectors = g_new(struct capture_dissector_table, 1);
79 sub_dissectors->hash_table = g_hash_table_new_full( g_direct_hash, g_direct_equal, NULL, NULL );
80 sub_dissectors->ui_name = ui_name;
81 g_hash_table_insert( capture_dissector_tables, (gpointer)name, (gpointer) sub_dissectors );
85 void register_capture_dissector(const char* name, const guint32 pattern, capture_dissector_t dissector, const int proto)
87 struct capture_dissector_table* sub_dissectors;
88 struct capture_dissector_handle *handle;
90 /* Make sure table exists */
91 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
92 g_assert(sub_dissectors != NULL);
94 /* Make sure the registration is unique */
95 g_assert(g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern)) == NULL);
97 handle = wmem_new(wmem_epan_scope(), struct capture_dissector_handle);
98 handle->pattern = pattern;
99 handle->dissector = dissector;
100 handle->protocol = find_protocol_by_id(proto);
102 g_hash_table_insert(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern), (gpointer) handle);
105 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)
107 struct capture_dissector_table* sub_dissectors;
108 struct capture_dissector_handle* handle;
110 sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
111 if (sub_dissectors == NULL)
117 handle = (struct capture_dissector_handle *)g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
121 return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
124 guint32 capture_dissector_get_count(packet_counts* counts, const int proto)
126 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(counts->counts_hash, GUINT_TO_POINTER(proto));
127 if (hash_count == NULL)
130 return hash_count->count;
133 void capture_dissector_increment_count(capture_packet_info_t *cpinfo, const int proto)
135 /* See if we already have a counter for the protocol */
136 capture_dissector_count_t* hash_count = (capture_dissector_count_t*)g_hash_table_lookup(cpinfo->counts, GUINT_TO_POINTER(proto));
137 if (hash_count == NULL)
139 hash_count = g_new0(capture_dissector_count_t, 1);
140 g_hash_table_insert(cpinfo->counts, GUINT_TO_POINTER(proto), (gpointer)hash_count);
147 * Editor modelines - http://www.wireshark.org/tools/modelines.html
152 * indent-tabs-mode: nil
155 * vi: set shiftwidth=4 tabstop=8 expandtab:
156 * :indentSize=4:tabSize=8:noTabs=true: