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.
38 * Hash table for circuits.
40 static GHashTable *circuit_hashtable = NULL;
42 static guint32 new_index;
45 * Protocol-specific data attached to a circuit_t structure - protocol
46 * index and opaque pointer.
48 typedef struct _circuit_proto_data {
54 * Compute the hash value for a circuit.
57 circuit_hash(gconstpointer v)
59 const circuit_key *key = (const circuit_key *)v;
61 return key->ctype ^ key->circuit_id;
65 * Compare two circuit keys.
68 circuit_match(gconstpointer v, gconstpointer w)
70 const circuit_key *v1 = (const circuit_key *)v;
71 const circuit_key *v2 = (const circuit_key *)w;
73 return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
77 * Initialize some variables every time a file is loaded or re-loaded.
78 * Destroy all existing circuits, and create a new hash table
79 * for the circuits in the new file.
84 if (circuit_hashtable != NULL)
85 g_hash_table_destroy(circuit_hashtable);
88 * Free up any space allocated for circuit protocol data
91 * We can free the space, as the structures it contains are
92 * pointed to by circuit data structures that were freed
96 circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
99 * Start the circuit indices over at 0.
105 * Given a circuit type and circuit ID for a packet, create a new circuit
106 * to contain packets for that circuit.
109 circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
111 circuit_t *circuit, *old_circuit;
112 circuit_key *new_key;
114 new_key = se_alloc(sizeof(struct circuit_key));
115 new_key->ctype = ctype;
116 new_key->circuit_id = circuit_id;
118 circuit = se_alloc(sizeof(circuit_t));
119 circuit->next = NULL;
120 circuit->first_frame = first_frame;
121 circuit->last_frame = 0; /* not known yet */
122 circuit->index = new_index;
123 circuit->data_list = NULL;
124 circuit->dissector_handle = NULL;
125 circuit->key_ptr = new_key;
130 * Is there already a circuit with this circuit ID?
132 old_circuit = g_hash_table_lookup(circuit_hashtable, new_key);
133 if (old_circuit != NULL) {
135 * Yes. Find the last circuit in the list of circuits
136 * with this circuit ID, and if its last frame isn't
137 * known, make it be the previous frame to this one.
139 while (old_circuit->next != NULL)
140 old_circuit = old_circuit->next;
141 if (old_circuit->last_frame == 0)
142 old_circuit->last_frame = first_frame - 1;
145 * Now put the new circuit after the last one in the
148 old_circuit->next = circuit;
151 * No. This is the first one with this circuit ID; add
152 * it to the hash table.
154 g_hash_table_insert(circuit_hashtable, new_key, circuit);
161 * Given a circuit type and ID, and a frame number, search for a circuit with
162 * that type and ID whose range of frames includes that frame number.
163 * Returns NULL if not found.
166 find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
172 key.circuit_id = circuit_id;
175 * OK, search the list of circuits with that type and ID for
176 * a circuit whose range of frames includes that frame number.
178 for (circuit = g_hash_table_lookup(circuit_hashtable, &key);
179 circuit != NULL; circuit = circuit->next) {
181 * The circuit includes that frame number if:
183 * the circuit's first frame is unknown or is at or
188 * the circuit's last frame is unknown or is at or
191 if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
192 && (circuit->last_frame == 0 || circuit->last_frame >= frame))
199 * Set the last frame of a circuit, if it's not already known,
200 * "closing" the circuit.
203 close_circuit(circuit_t *circuit, guint32 last_frame)
205 if (circuit->last_frame == 0)
206 circuit->last_frame = last_frame;
210 p_compare(gconstpointer a, gconstpointer b)
212 const circuit_proto_data *ap = (const circuit_proto_data *)a;
213 const circuit_proto_data *bp = (const circuit_proto_data *)b;
215 if (ap->proto > bp->proto)
217 else if (ap->proto == bp->proto)
224 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
226 circuit_proto_data *p1 = se_alloc(sizeof(circuit_proto_data));
229 p1->proto_data = proto_data;
231 /* Add it to the list of items for this circuit. */
233 conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
238 circuit_get_proto_data(circuit_t *conv, int proto)
240 circuit_proto_data temp, *p1;
244 temp.proto_data = NULL;
246 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
250 p1 = (circuit_proto_data *)item->data;
251 return p1->proto_data;
258 circuit_delete_proto_data(circuit_t *conv, int proto)
260 circuit_proto_data temp;
264 temp.proto_data = NULL;
266 item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
270 conv->data_list = g_slist_remove(conv->data_list, item);
274 circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
276 circuit->dissector_handle = handle;
280 circuit_get_dissector(circuit_t *circuit)
282 return circuit->dissector_handle;
286 * Given a circuit type and ID for a packet, search for a matching
287 * circuit and, if found and it has a circuit dissector,
288 * call that dissector and return TRUE, otherwise return FALSE.
291 try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
292 tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
296 circuit = find_circuit(ctype, circuit_id, frame);
298 if (circuit != NULL) {
299 if (circuit->dissector_handle == NULL)
301 call_dissector(circuit->dissector_handle, tvb, pinfo,