2 * Routines for building lists of packets that are part of a "circuit"
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.
30 * Hash table for circuits.
32 static GHashTable *circuit_hashtable = NULL;
34 static guint32 new_index;
37 * Protocol-specific data attached to a circuit_t structure - protocol
38 * index and opaque pointer.
40 typedef struct _circuit_proto_data {
46 * Compute the hash value for a circuit.
49 circuit_hash(gconstpointer v)
51 const circuit_key *key = (const circuit_key *)v;
53 return key->ctype ^ key->circuit_id;
57 * Compare two circuit keys.
60 circuit_match(gconstpointer v, gconstpointer w)
62 const circuit_key *v1 = (const circuit_key *)v;
63 const circuit_key *v2 = (const circuit_key *)w;
65 return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
69 * Destroy all existing circuits.
75 * Free up any space allocated for the circuit hashtable.
77 * We can free the hash as the structures pointed to in the
78 * hash are in "seasonal" memory which is freed separately.
79 * Note: circuit_cleanup() must be called only when
80 * seasonal memory is also freed.
83 if (circuit_hashtable != NULL)
84 g_hash_table_destroy(circuit_hashtable);
86 circuit_hashtable = NULL;
90 * Initialize some variables every time a file is loaded or re-loaded.
91 * Create a new hash table for the circuits in the new file.
96 g_assert(circuit_hashtable == NULL);
97 circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
100 * Start the circuit indices over at 0.
106 * Given a circuit type and circuit ID for a packet, create a new circuit
107 * to contain packets for that circuit.
110 circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
112 circuit_t *circuit, *old_circuit;
113 circuit_key *new_key;
115 new_key = wmem_new(wmem_file_scope(), struct circuit_key);
116 new_key->ctype = ctype;
117 new_key->circuit_id = circuit_id;
119 circuit = wmem_new(wmem_file_scope(), circuit_t);
120 circuit->next = NULL;
121 circuit->first_frame = first_frame;
122 circuit->last_frame = 0; /* not known yet */
123 circuit->index = new_index;
124 circuit->data_list = NULL;
125 circuit->dissector_handle = NULL;
126 circuit->key_ptr = new_key;
131 * Is there already a circuit with this circuit ID?
133 old_circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, new_key);
134 if (old_circuit != NULL) {
136 * Yes. Find the last circuit in the list of circuits
137 * with this circuit ID, and if its last frame isn't
138 * known, make it be the previous frame to this one.
140 while (old_circuit->next != NULL)
141 old_circuit = old_circuit->next;
142 if (old_circuit->last_frame == 0)
143 old_circuit->last_frame = first_frame - 1;
146 * Now put the new circuit after the last one in the
149 old_circuit->next = circuit;
152 * No. This is the first one with this circuit ID; add
153 * it to the hash table.
155 g_hash_table_insert(circuit_hashtable, new_key, circuit);
162 * Given a circuit type and ID, and a frame number, search for a circuit with
163 * that type and ID whose range of frames includes that frame number.
164 * Returns NULL if not found.
167 find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
173 key.circuit_id = circuit_id;
176 * OK, search the list of circuits with that type and ID for
177 * a circuit whose range of frames includes that frame number.
179 for (circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, &key);
180 circuit != NULL; circuit = circuit->next) {
182 * The circuit includes that frame number if:
184 * the circuit's first frame is unknown or is at or
189 * the circuit's last frame is unknown or is at or
192 if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
193 && (circuit->last_frame == 0 || circuit->last_frame >= frame))
200 * Set the last frame of a circuit, if it's not already known,
201 * "closing" the circuit.
204 close_circuit(circuit_t *circuit, guint32 last_frame)
206 if (circuit->last_frame == 0)
207 circuit->last_frame = last_frame;
211 p_compare(gconstpointer a, gconstpointer b)
213 const circuit_proto_data *ap = (const circuit_proto_data *)a;
214 const circuit_proto_data *bp = (const circuit_proto_data *)b;
216 if (ap->proto > bp->proto)
218 else if (ap->proto == bp->proto)
225 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
227 circuit_proto_data *p1 = wmem_new(wmem_file_scope(), circuit_proto_data);
230 p1->proto_data = proto_data;
232 /* Add it to the list of items for this circuit. */
234 conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
239 circuit_get_proto_data(circuit_t *conv, int proto)
241 circuit_proto_data temp, *p1;
245 temp.proto_data = NULL;
247 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
251 p1 = (circuit_proto_data *)item->data;
252 return p1->proto_data;
259 circuit_delete_proto_data(circuit_t *conv, int proto)
261 circuit_proto_data temp;
265 temp.proto_data = NULL;
267 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
271 conv->data_list = g_slist_remove(conv->data_list, item);
275 circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
277 circuit->dissector_handle = handle;
281 circuit_get_dissector(circuit_t *circuit)
283 return circuit->dissector_handle;
287 * Given a circuit type and ID for a packet, search for a matching
288 * circuit and, if found and it has a circuit dissector,
289 * call that dissector and return TRUE, otherwise return FALSE.
292 try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
293 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
297 circuit = find_circuit(ctype, circuit_id, frame);
299 if (circuit != NULL) {
300 if (circuit->dissector_handle == NULL)
302 call_dissector_with_data(circuit->dissector_handle, tvb, pinfo,
310 * Editor modelines - http://www.wireshark.org/tools/modelines.html
315 * indent-tabs-mode: t
318 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
319 * :indentSize=8:tabSize=8:noTabs=false: