lua: fix reload with -Xlua_script
[metze/wireshark/wip.git] / epan / capture_dissectors.c
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  * 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.
12  *
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.
17  *
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.
21  */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include "packet.h"
27
28 #include "capture_dissectors.h"
29
30 struct capture_dissector_table {
31     GHashTable *hash_table;
32     const char *ui_name;
33 };
34
35 struct capture_dissector_handle
36 {
37     guint32 pattern;
38     capture_dissector_t dissector;
39     protocol_t* protocol;
40 };
41
42 typedef struct capture_dissector_count
43 {
44     guint32 count;
45 } capture_dissector_count_t;
46
47 static GHashTable *capture_dissector_tables = NULL;
48
49 static void
50 destroy_capture_dissector_table(void *data)
51 {
52     struct capture_dissector_table *table = (struct capture_dissector_table *)data;
53
54     g_hash_table_destroy(table->hash_table);
55     g_free(data);
56 }
57
58 void capture_dissector_init(void)
59 {
60     capture_dissector_tables = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, destroy_capture_dissector_table);
61 }
62
63 void capture_dissector_cleanup(void)
64 {
65     g_hash_table_destroy(capture_dissector_tables);
66 }
67
68 void register_capture_dissector_table(const char *name, const char *ui_name)
69 {
70     struct capture_dissector_table*     sub_dissectors;
71
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);
75     }
76
77     sub_dissectors = g_new(struct capture_dissector_table, 1);
78
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 );
82
83 }
84
85 void register_capture_dissector(const char* name, const guint32 pattern, capture_dissector_t dissector, const int proto)
86 {
87     struct capture_dissector_table*     sub_dissectors;
88     struct capture_dissector_handle *handle;
89
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);
93
94     /* Make sure the registration is unique */
95     g_assert(g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern)) == NULL);
96
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);
101
102     g_hash_table_insert(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern), (gpointer) handle);
103 }
104
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)
106 {
107     struct capture_dissector_table*     sub_dissectors;
108     struct capture_dissector_handle* handle;
109
110     sub_dissectors = (struct capture_dissector_table*)g_hash_table_lookup( capture_dissector_tables, name );
111     if (sub_dissectors == NULL)
112     {
113         /* XXX - ASSERT? */
114         return FALSE;
115     }
116
117     handle = (struct capture_dissector_handle *)g_hash_table_lookup(sub_dissectors->hash_table, GUINT_TO_POINTER(pattern));
118     if (handle == NULL)
119         return FALSE;
120
121     return handle->dissector(pd, offset, len, cpinfo, pseudo_header);
122 }
123
124 guint32 capture_dissector_get_count(packet_counts* counts, const int proto)
125 {
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)
128         return 0;
129
130     return hash_count->count;
131 }
132
133 void capture_dissector_increment_count(capture_packet_info_t *cpinfo, const int proto)
134 {
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)
138     {
139         hash_count = g_new0(capture_dissector_count_t, 1);
140         g_hash_table_insert(cpinfo->counts, GUINT_TO_POINTER(proto), (gpointer)hash_count);
141     }
142
143     hash_count->count++;
144 }
145
146 /*
147  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
148  *
149  * Local variables:
150  * c-basic-offset: 4
151  * tab-width: 8
152  * indent-tabs-mode: nil
153  * End:
154  *
155  * vi: set shiftwidth=4 tabstop=8 expandtab:
156  * :indentSize=4:tabSize=8:noTabs=true:
157  */