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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
{
49 * Compute the hash value for a circuit.
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.
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.
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.
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.
109 * Given a circuit type and circuit ID for a packet, create a new circuit
110 * to contain packets for that circuit.
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
;
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
152 old_circuit
->next
= circuit
;
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
);
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.
170 find_circuit(circuit_type ctype
, guint32 circuit_id
, guint32 frame
)
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
192 * the circuit's last frame is unknown or is at or
195 if ((circuit
->first_frame
== 0 || circuit
->first_frame
<= frame
)
196 && (circuit
->last_frame
== 0 || circuit
->last_frame
>= frame
))
203 * Set the last frame of a circuit, if it's not already known,
204 * "closing" the circuit.
207 close_circuit(circuit_t
*circuit
, guint32 last_frame
)
209 if (circuit
->last_frame
== 0)
210 circuit
->last_frame
= last_frame
;
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
)
221 else if (ap
->proto
== bp
->proto
)
228 circuit_add_proto_data(circuit_t
*conv
, int proto
, void *proto_data
)
230 circuit_proto_data
*p1
= se_new(circuit_proto_data
);
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
,
242 circuit_get_proto_data(circuit_t
*conv
, int proto
)
244 circuit_proto_data temp
, *p1
;
248 temp
.proto_data
= NULL
;
250 item
= g_slist_find_custom(conv
->data_list
, (gpointer
*)&temp
,
254 p1
= (circuit_proto_data
*)item
->data
;
255 return p1
->proto_data
;
262 circuit_delete_proto_data(circuit_t
*conv
, int proto
)
264 circuit_proto_data temp
;
268 temp
.proto_data
= NULL
;
270 item
= g_slist_find_custom(conv
->data_list
, (gpointer
*)&temp
,
274 conv
->data_list
= g_slist_remove(conv
->data_list
, item
);
278 circuit_set_dissector(circuit_t
*circuit
, dissector_handle_t handle
)
280 circuit
->dissector_handle
= handle
;
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.
295 try_circuit_dissector(circuit_type ctype
, guint32 circuit_id
, guint32 frame
,
296 tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data
)
300 circuit
= find_circuit(ctype
, circuit_id
, frame
);
302 if (circuit
!= NULL
) {
303 if (circuit
->dissector_handle
== NULL
)
305 call_dissector_with_data(circuit
->dissector_handle
, tvb
, pinfo
,