From Jakub Zawadzki via bug #4289: (Fix for) Frame arrival times (pcap)
[obnox/wireshark/wip.git] / epan / circuit.c
1 /* circuit.c
2  * Routines for building lists of packets that are part of a "circuit"
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <glib.h>
30 #include "packet.h"
31 #include "circuit.h"
32 #include "emem.h"
33
34 /*
35  * Hash table for circuits.
36  */
37 static GHashTable *circuit_hashtable = NULL;
38
39 static guint32 new_index;
40
41 /*
42  * Protocol-specific data attached to a circuit_t structure - protocol
43  * index and opaque pointer.
44  */
45 typedef struct _circuit_proto_data {
46         int     proto;
47         void    *proto_data;
48 } circuit_proto_data;
49
50 /*
51  * Compute the hash value for a circuit.
52  */
53 static guint
54 circuit_hash(gconstpointer v)
55 {
56         const circuit_key *key = (const circuit_key *)v;
57
58         return key->ctype ^ key->circuit_id;
59 }
60
61 /*
62  * Compare two circuit keys.
63  */
64 static gint
65 circuit_match(gconstpointer v, gconstpointer w)
66 {
67         const circuit_key *v1 = (const circuit_key *)v;
68         const circuit_key *v2 = (const circuit_key *)w;
69
70         return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
71 }
72
73 /*
74  * Destroy all existing circuits.
75  */
76 void
77 circuit_cleanup(void)
78 {
79         /*
80          * Free up any space allocated for the circuit hashtable.
81          *
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.
86          */
87
88         if (circuit_hashtable != NULL)
89                 g_hash_table_destroy(circuit_hashtable);
90
91         circuit_hashtable = NULL;
92 }
93
94 /*
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.
97  */
98 void
99 circuit_init(void)
100 {
101         circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
102
103         /*
104          * Start the circuit indices over at 0.
105          */
106         new_index = 0;
107 }
108
109 /*
110  * Given a circuit type and circuit ID for a packet, create a new circuit
111  * to contain packets for that circuit.
112  */
113 circuit_t *
114 circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
115 {
116         circuit_t *circuit, *old_circuit;
117         circuit_key *new_key;
118
119         new_key = se_alloc(sizeof(struct circuit_key));
120         new_key->ctype = ctype;
121         new_key->circuit_id = circuit_id;
122
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;
131
132         new_index++;
133
134         /*
135          * Is there already a circuit with this circuit ID?
136          */
137         old_circuit = g_hash_table_lookup(circuit_hashtable, new_key);
138         if (old_circuit != NULL) {
139                 /*
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.
143                  */
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;
148
149                 /*
150                  * Now put the new circuit after the last one in the
151                  * list.
152                  */
153                 old_circuit->next = circuit;
154         } else {
155                 /*
156                  * No.  This is the first one with this circuit ID; add
157                  * it to the hash table.
158                  */
159                 g_hash_table_insert(circuit_hashtable, new_key, circuit);
160         }
161
162         return circuit;
163 }
164
165 /*
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.
169  */
170 circuit_t *
171 find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
172 {
173         circuit_key key;
174         circuit_t *circuit;
175
176         key.ctype = ctype;
177         key.circuit_id = circuit_id;
178
179         /*
180          * OK, search the list of circuits with that type and ID for
181          * a circuit whose range of frames includes that frame number.
182          */
183         for (circuit = g_hash_table_lookup(circuit_hashtable, &key);
184             circuit != NULL; circuit = circuit->next) {
185                 /*
186                  * The circuit includes that frame number if:
187                  *
188                  *      the circuit's first frame is unknown or is at or
189                  *      before that frame
190                  *
191                  * and
192                  *
193                  *      the circuit's last frame is unknown or is at or
194                  *      after that frame.
195                  */
196                 if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
197                     && (circuit->last_frame == 0 || circuit->last_frame >= frame))
198                         break;
199         }
200         return circuit;
201 }
202
203 /*
204  * Set the last frame of a circuit, if it's not already known,
205  * "closing" the circuit.
206  */
207 void
208 close_circuit(circuit_t *circuit, guint32 last_frame)
209 {
210         if (circuit->last_frame == 0)
211                 circuit->last_frame = last_frame;
212 }
213
214 static gint
215 p_compare(gconstpointer a, gconstpointer b)
216 {
217         const circuit_proto_data *ap = (const circuit_proto_data *)a;
218         const circuit_proto_data *bp = (const circuit_proto_data *)b;
219
220         if (ap->proto > bp->proto)
221                 return 1;
222         else if (ap->proto == bp->proto)
223                 return 0;
224         else
225                 return -1;
226 }
227
228 void
229 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
230 {
231         circuit_proto_data *p1 = se_alloc(sizeof(circuit_proto_data));
232
233         p1->proto = proto;
234         p1->proto_data = proto_data;
235
236         /* Add it to the list of items for this circuit. */
237
238         conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
239             p_compare);
240 }
241
242 void *
243 circuit_get_proto_data(circuit_t *conv, int proto)
244 {
245         circuit_proto_data temp, *p1;
246         GSList *item;
247
248         temp.proto = proto;
249         temp.proto_data = NULL;
250
251         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
252             p_compare);
253
254         if (item != NULL) {
255                 p1 = (circuit_proto_data *)item->data;
256                 return p1->proto_data;
257         }
258
259         return NULL;
260 }
261
262 void
263 circuit_delete_proto_data(circuit_t *conv, int proto)
264 {
265         circuit_proto_data temp;
266         GSList *item;
267
268         temp.proto = proto;
269         temp.proto_data = NULL;
270
271         item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
272             p_compare);
273
274         if (item != NULL)
275                 conv->data_list = g_slist_remove(conv->data_list, item);
276 }
277
278 void
279 circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
280 {
281         circuit->dissector_handle = handle;
282 }
283
284 dissector_handle_t
285 circuit_get_dissector(circuit_t *circuit)
286 {
287         return circuit->dissector_handle;
288 }
289
290 /*
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.
294  */
295 gboolean
296 try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
297                       tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
298 {
299         circuit_t *circuit;
300
301         circuit = find_circuit(ctype, circuit_id, frame);
302
303         if (circuit != NULL) {
304                 if (circuit->dissector_handle == NULL)
305                         return FALSE;
306                 call_dissector(circuit->dissector_handle, tvb, pinfo,
307                     tree);
308                 return TRUE;
309         }
310         return FALSE;
311 }