aboutsummaryrefslogtreecommitdiffstats
path: root/epan/circuit.c
blob: 53b1c9d4134067f8be84c526b5412df65c8f0542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* circuit.c
 * Routines for building lists of packets that are part of a "circuit"
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib.h>
#include "packet.h"
#include "circuit.h"

/*
 * Hash table for circuits.
 */
static GHashTable *circuit_hashtable = NULL;

static guint32 new_index;

/*
 * Protocol-specific data attached to a circuit_t structure - protocol
 * index and opaque pointer.
 */
typedef struct _circuit_proto_data {
	int	proto;
	void	*proto_data;
} circuit_proto_data;

/*
 * Compute the hash value for a circuit.
 */
static guint
circuit_hash(gconstpointer v)
{
	const circuit_key *key = (const circuit_key *)v;

	return key->ctype ^ key->circuit_id;
}

/*
 * Compare two circuit keys.
 */
static gint
circuit_match(gconstpointer v, gconstpointer w)
{
	const circuit_key *v1 = (const circuit_key *)v;
	const circuit_key *v2 = (const circuit_key *)w;

	return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
}

/*
 * Destroy all existing circuits.
 */
void
circuit_cleanup(void)
{
	/*
	 * Free up any space allocated for the circuit hashtable.
	 *
	 * We can free the hash as the structures pointed to in the
	 * hash are in "seasonal" memory which is freed separately.
	 * Note: circuit_cleanup() must be called only when
	 *       seasonal memory is also freed.
	 */

	if (circuit_hashtable != NULL)
		g_hash_table_destroy(circuit_hashtable);

	circuit_hashtable = NULL;
}

/*
 * Initialize some variables every time a file is loaded or re-loaded.
 * Create a new hash table for the circuits in the new file.
 */
void
circuit_init(void)
{
	g_assert(circuit_hashtable == NULL);
	circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);

	/*
	 * Start the circuit indices over at 0.
	 */
	new_index = 0;
}

/*
 * Given a circuit type and circuit ID for a packet, create a new circuit
 * to contain packets for that circuit.
 */
circuit_t *
circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
{
	circuit_t *circuit, *old_circuit;
	circuit_key *new_key;

	new_key = wmem_new(wmem_file_scope(), struct circuit_key);
	new_key->ctype = ctype;
	new_key->circuit_id = circuit_id;

	circuit = wmem_new(wmem_file_scope(), circuit_t);
	circuit->next = NULL;
	circuit->first_frame = first_frame;
	circuit->last_frame = 0;	/* not known yet */
	circuit->index = new_index;
	circuit->data_list = NULL;
	circuit->dissector_handle = NULL;
	circuit->key_ptr = new_key;

	new_index++;

	/*
	 * Is there already a circuit with this circuit ID?
	 */
	old_circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, new_key);
	if (old_circuit != NULL) {
		/*
		 * Yes.  Find the last circuit in the list of circuits
		 * with this circuit ID, and if its last frame isn't
		 * known, make it be the previous frame to this one.
		 */
		while (old_circuit->next != NULL)
			old_circuit = old_circuit->next;
		if (old_circuit->last_frame == 0)
			old_circuit->last_frame = first_frame - 1;

		/*
		 * Now put the new circuit after the last one in the
		 * list.
		 */
		old_circuit->next = circuit;
	} else {
		/*
		 * No.  This is the first one with this circuit ID; add
		 * it to the hash table.
		 */
		g_hash_table_insert(circuit_hashtable, new_key, circuit);
	}

	return circuit;
}

/*
 * Given a circuit type and ID, and a frame number, search for a circuit with
 * that type and ID whose range of frames includes that frame number.
 * Returns NULL if not found.
 */
circuit_t *
find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
{
	circuit_key key;
	circuit_t *circuit;

	key.ctype = ctype;
	key.circuit_id = circuit_id;

	/*
	 * OK, search the list of circuits with that type and ID for
	 * a circuit whose range of frames includes that frame number.
	 */
	for (circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, &key);
	    circuit != NULL; circuit = circuit->next) {
		/*
		 * The circuit includes that frame number if:
		 *
		 *	the circuit's first frame is unknown or is at or
		 *	before that frame
		 *
		 * and
		 *
		 *	the circuit's last frame is unknown or is at or
		 *	after that frame.
		 */
		if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
		    && (circuit->last_frame == 0 || circuit->last_frame >= frame))
			break;
	}
	return circuit;
}

/*
 * Set the last frame of a circuit, if it's not already known,
 * "closing" the circuit.
 */
void
close_circuit(circuit_t *circuit, guint32 last_frame)
{
	if (circuit->last_frame == 0)
		circuit->last_frame = last_frame;
}

static gint
p_compare(gconstpointer a, gconstpointer b)
{
	const circuit_proto_data *ap = (const circuit_proto_data *)a;
	const circuit_proto_data *bp = (const circuit_proto_data *)b;

	if (ap->proto > bp->proto)
		return 1;
	else if (ap->proto == bp->proto)
		return 0;
	else
		return -1;
}

void
circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
{
	circuit_proto_data *p1 = wmem_new(wmem_file_scope(), circuit_proto_data);

	p1->proto = proto;
	p1->proto_data = proto_data;

	/* Add it to the list of items for this circuit. */

	conv->data_list = g_slist_insert_sorted(conv->data_list, (gpointer *)p1,
	    p_compare);
}

void *
circuit_get_proto_data(circuit_t *conv, int proto)
{
	circuit_proto_data temp, *p1;
	GSList *item;

	temp.proto = proto;
	temp.proto_data = NULL;

	item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
	    p_compare);

	if (item != NULL) {
		p1 = (circuit_proto_data *)item->data;
		return p1->proto_data;
	}

	return NULL;
}

void
circuit_delete_proto_data(circuit_t *conv, int proto)
{
	circuit_proto_data temp;
	GSList *item;

	temp.proto = proto;
	temp.proto_data = NULL;

	item = g_slist_find_custom(conv->data_list, (gpointer *)&temp,
	    p_compare);

	if (item != NULL)
		conv->data_list = g_slist_remove(conv->data_list, item);
}

void
circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
{
	circuit->dissector_handle = handle;
}

dissector_handle_t
circuit_get_dissector(circuit_t *circuit)
{
	return circuit->dissector_handle;
}

/*
 * Given a circuit type and ID for a packet, search for a matching
 * circuit and, if found and it has a circuit dissector,
 * call that dissector and return TRUE, otherwise return FALSE.
 */
gboolean
try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
		      tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
{
	circuit_t *circuit;

	circuit = find_circuit(ctype, circuit_id, frame);

	if (circuit != NULL) {
		if (circuit->dissector_handle == NULL)
			return FALSE;
		call_dissector_with_data(circuit->dissector_handle, tvb, pinfo,
		    tree, data);
		return TRUE;
	}
	return FALSE;
}

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */