2 * Routines for building lists of packets that are part of a "circuit"
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 * Hash table for circuits.
37 static GHashTable *circuit_hashtable = NULL;
39 static guint32 new_index;
42 * Protocol-specific data attached to a circuit_t structure - protocol
43 * index and opaque pointer.
45 typedef struct _circuit_proto_data {
51 * Compute the hash value for a circuit.
54 circuit_hash(gconstpointer v)
56 const circuit_key *key = (const circuit_key *)v;
58 return key->ctype ^ key->circuit_id;
62 * Compare two circuit keys.
65 circuit_match(gconstpointer v, gconstpointer w)
67 const circuit_key *v1 = (const circuit_key *)v;
68 const circuit_key *v2 = (const circuit_key *)w;
70 return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
74 * Destroy all existing circuits.
80 * Free up any space allocated for the circuit hashtable.
82 * We can free the hash as the structures pointed to in the
83 * hash are in "seasonal" memory which is freed separately.
84 * Note: circuit_cleanup() must be called only when
85 * seasonal memory is also freed.
88 if (circuit_hashtable != NULL)
89 g_hash_table_destroy(circuit_hashtable);
91 circuit_hashtable = NULL;
95 * Initialize some variables every time a file is loaded or re-loaded.
96 * Create a new hash table for the circuits in the new file.
101 circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
104 * Start the circuit indices over at 0.
110 * Given a circuit type and circuit ID for a packet, create a new circuit
111 * to contain packets for that circuit.
114 circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
116 circuit_t *circuit, *old_circuit;
117 circuit_key *new_key;
119 new_key = se_alloc(sizeof(struct circuit_key));
120 new_key->ctype = ctype;
121 new_key->circuit_id = circuit_id;
123 circuit = se_alloc(sizeof(circuit_t));
124 circuit->next = NULL;
125 circuit->first_frame = first_frame;
126 circuit->last_frame = 0; /* not known yet */
127 circuit->index = new_index;
128 circuit->data_list = NULL;
129 circuit->dissector_handle = NULL;
130 circuit->key_ptr = new_key;
135 * Is there already a circuit with this circuit ID?
137 old_circuit = g_hash_table_lookup(circuit_hashtable, new_key);
138 if (old_circuit != NULL) {
140 * Yes. Find the last circuit in the list of circuits
141 * with this circuit ID, and if its last frame isn't
142 * known, make it be the previous frame to this one.
144 while (old_circuit->next != NULL)
145 old_circuit = old_circuit->next;
146 if (old_circuit->last_frame == 0)
147 old_circuit->last_frame = first_frame - 1;
150 * Now put the new circuit after the last one in the
153 old_circuit->next = circuit;
156 * No. This is the first one with this circuit ID; add
157 * it to the hash table.
159 g_hash_table_insert(circuit_hashtable, new_key, circuit);
166 * Given a circuit type and ID, and a frame number, search for a circuit with
167 * that type and ID whose range of frames includes that frame number.
168 * Returns NULL if not found.
171 find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
177 key.circuit_id = circuit_id;
180 * OK, search the list of circuits with that type and ID for
181 * a circuit whose range of frames includes that frame number.
183 for (circuit = g_hash_table_lookup(circuit_hashtable, &key);
184 circuit != NULL; circuit = circuit->next) {
186 * The circuit includes that frame number if:
188 * the circuit's first frame is unknown or is at or
193 * the circuit's last frame is unknown or is at or
196 if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
197 && (circuit->last_frame == 0 || circuit->last_frame >= frame))
204 * Set the last frame of a circuit, if it's not already known,
205 * "closing" the circuit.
208 close_circuit(circuit_t *circuit, guint32 last_frame)
210 if (circuit->last_frame == 0)
211 circuit->last_frame = last_frame;
215 p_compare(gconstpointer a, gconstpointer b)
217 const circuit_proto_data *ap = (const circuit_proto_data *)a;
218 const circuit_proto_data *bp = (const circuit_proto_data *)b;
220 if (ap->proto > bp->proto)
222 else if (ap->proto == bp->proto)
229 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
231 circuit_proto_data *p1 = se_alloc(sizeof(circuit_proto_data));
234 p1->proto_data = proto_data;
236 /* Add it to the list of items for this circuit. */
238 conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
243 circuit_get_proto_data(circuit_t *conv, int proto)
245 circuit_proto_data temp, *p1;
249 temp.proto_data = NULL;
251 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
255 p1 = (circuit_proto_data *)item->data;
256 return p1->proto_data;
263 circuit_delete_proto_data(circuit_t *conv, int proto)
265 circuit_proto_data temp;
269 temp.proto_data = NULL;
271 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
275 conv->data_list = g_slist_remove(conv->data_list, item);
279 circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
281 circuit->dissector_handle = handle;
285 circuit_get_dissector(circuit_t *circuit)
287 return circuit->dissector_handle;
291 * Given a circuit type and ID for a packet, search for a matching
292 * circuit and, if found and it has a circuit dissector,
293 * call that dissector and return TRUE, otherwise return FALSE.
296 try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
297 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
301 circuit = find_circuit(ctype, circuit_id, frame);
303 if (circuit != NULL) {
304 if (circuit->dissector_handle == NULL)
306 call_dissector(circuit->dissector_handle, tvb, pinfo,