Witness: add pidl output
[wireshark-wip.git] / epan / circuit.c
blob9bdc76e2f3f282f3916713c39d35562558771956
1 /* circuit.c
2 * Routines for building lists of packets that are part of a "circuit"
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "config.h"
27 #include <glib.h>
28 #include "packet.h"
29 #include "circuit.h"
30 #include "emem.h"
33 * Hash table for circuits.
35 static GHashTable *circuit_hashtable = NULL;
37 static guint32 new_index;
40 * Protocol-specific data attached to a circuit_t structure - protocol
41 * index and opaque pointer.
43 typedef struct _circuit_proto_data {
44 int proto;
45 void *proto_data;
46 } circuit_proto_data;
49 * Compute the hash value for a circuit.
51 static guint
52 circuit_hash(gconstpointer v)
54 const circuit_key *key = (const circuit_key *)v;
56 return key->ctype ^ key->circuit_id;
60 * Compare two circuit keys.
62 static gint
63 circuit_match(gconstpointer v, gconstpointer w)
65 const circuit_key *v1 = (const circuit_key *)v;
66 const circuit_key *v2 = (const circuit_key *)w;
68 return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
72 * Destroy all existing circuits.
74 void
75 circuit_cleanup(void)
78 * Free up any space allocated for the circuit hashtable.
80 * We can free the hash as the structures pointed to in the
81 * hash are in "seasonal" memory which is freed separately.
82 * Note: circuit_cleanup() must be called only when
83 * seasonal memory is also freed.
86 if (circuit_hashtable != NULL)
87 g_hash_table_destroy(circuit_hashtable);
89 circuit_hashtable = NULL;
93 * Initialize some variables every time a file is loaded or re-loaded.
94 * Create a new hash table for the circuits in the new file.
96 void
97 circuit_init(void)
99 g_assert(circuit_hashtable == NULL);
100 circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
103 * Start the circuit indices over at 0.
105 new_index = 0;
109 * Given a circuit type and circuit ID for a packet, create a new circuit
110 * to contain packets for that circuit.
112 circuit_t *
113 circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
115 circuit_t *circuit, *old_circuit;
116 circuit_key *new_key;
118 new_key = se_new(struct circuit_key);
119 new_key->ctype = ctype;
120 new_key->circuit_id = circuit_id;
122 circuit = se_new(circuit_t);
123 circuit->next = NULL;
124 circuit->first_frame = first_frame;
125 circuit->last_frame = 0; /* not known yet */
126 circuit->index = new_index;
127 circuit->data_list = NULL;
128 circuit->dissector_handle = NULL;
129 circuit->key_ptr = new_key;
131 new_index++;
134 * Is there already a circuit with this circuit ID?
136 old_circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, new_key);
137 if (old_circuit != NULL) {
139 * Yes. Find the last circuit in the list of circuits
140 * with this circuit ID, and if its last frame isn't
141 * known, make it be the previous frame to this one.
143 while (old_circuit->next != NULL)
144 old_circuit = old_circuit->next;
145 if (old_circuit->last_frame == 0)
146 old_circuit->last_frame = first_frame - 1;
149 * Now put the new circuit after the last one in the
150 * list.
152 old_circuit->next = circuit;
153 } else {
155 * No. This is the first one with this circuit ID; add
156 * it to the hash table.
158 g_hash_table_insert(circuit_hashtable, new_key, circuit);
161 return circuit;
165 * Given a circuit type and ID, and a frame number, search for a circuit with
166 * that type and ID whose range of frames includes that frame number.
167 * Returns NULL if not found.
169 circuit_t *
170 find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
172 circuit_key key;
173 circuit_t *circuit;
175 key.ctype = ctype;
176 key.circuit_id = circuit_id;
179 * OK, search the list of circuits with that type and ID for
180 * a circuit whose range of frames includes that frame number.
182 for (circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, &key);
183 circuit != NULL; circuit = circuit->next) {
185 * The circuit includes that frame number if:
187 * the circuit's first frame is unknown or is at or
188 * before that frame
190 * and
192 * the circuit's last frame is unknown or is at or
193 * after that frame.
195 if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
196 && (circuit->last_frame == 0 || circuit->last_frame >= frame))
197 break;
199 return circuit;
203 * Set the last frame of a circuit, if it's not already known,
204 * "closing" the circuit.
206 void
207 close_circuit(circuit_t *circuit, guint32 last_frame)
209 if (circuit->last_frame == 0)
210 circuit->last_frame = last_frame;
213 static gint
214 p_compare(gconstpointer a, gconstpointer b)
216 const circuit_proto_data *ap = (const circuit_proto_data *)a;
217 const circuit_proto_data *bp = (const circuit_proto_data *)b;
219 if (ap->proto > bp->proto)
220 return 1;
221 else if (ap->proto == bp->proto)
222 return 0;
223 else
224 return -1;
227 void
228 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
230 circuit_proto_data *p1 = se_new(circuit_proto_data);
232 p1->proto = proto;
233 p1->proto_data = proto_data;
235 /* Add it to the list of items for this circuit. */
237 conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
238 p_compare);
241 void *
242 circuit_get_proto_data(circuit_t *conv, int proto)
244 circuit_proto_data temp, *p1;
245 GSList *item;
247 temp.proto = proto;
248 temp.proto_data = NULL;
250 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
251 p_compare);
253 if (item != NULL) {
254 p1 = (circuit_proto_data *)item->data;
255 return p1->proto_data;
258 return NULL;
261 void
262 circuit_delete_proto_data(circuit_t *conv, int proto)
264 circuit_proto_data temp;
265 GSList *item;
267 temp.proto = proto;
268 temp.proto_data = NULL;
270 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
271 p_compare);
273 if (item != NULL)
274 conv->data_list = g_slist_remove(conv->data_list, item);
277 void
278 circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
280 circuit->dissector_handle = handle;
283 dissector_handle_t
284 circuit_get_dissector(circuit_t *circuit)
286 return circuit->dissector_handle;
290 * Given a circuit type and ID for a packet, search for a matching
291 * circuit and, if found and it has a circuit dissector,
292 * call that dissector and return TRUE, otherwise return FALSE.
294 gboolean
295 try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
296 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
298 circuit_t *circuit;
300 circuit = find_circuit(ctype, circuit_id, frame);
302 if (circuit != NULL) {
303 if (circuit->dissector_handle == NULL)
304 return FALSE;
305 call_dissector_with_data(circuit->dissector_handle, tvb, pinfo,
306 tree, data);
307 return TRUE;
309 return FALSE;