2002-10-22 08:44:33 +00:00
|
|
|
/* circuit.c
|
|
|
|
* Routines for building lists of packets that are part of a "circuit"
|
|
|
|
*
|
2004-07-18 00:24:25 +00:00
|
|
|
* $Id$
|
2002-10-22 08:44:33 +00:00
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2002-10-22 08:44:33 +00:00
|
|
|
* 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
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2002-10-22 08:44:33 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include "packet.h"
|
|
|
|
#include "circuit.h"
|
2005-08-13 02:24:11 +00:00
|
|
|
#include "emem.h"
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2002-11-27 22:44:41 +00:00
|
|
|
const circuit_key *key = (const circuit_key *)v;
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
return key->ctype ^ key->circuit_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compare two circuit keys.
|
|
|
|
*/
|
|
|
|
static gint
|
|
|
|
circuit_match(gconstpointer v, gconstpointer w)
|
|
|
|
{
|
2002-11-27 22:44:41 +00:00
|
|
|
const circuit_key *v1 = (const circuit_key *)v;
|
|
|
|
const circuit_key *v2 = (const circuit_key *)w;
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
return v1->ctype == v2->ctype && v1->circuit_id == v2->circuit_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-09-06 18:25:23 +00:00
|
|
|
* Destroy all existing circuits.
|
2002-10-22 08:44:33 +00:00
|
|
|
*/
|
|
|
|
void
|
2009-09-06 18:25:23 +00:00
|
|
|
circuit_cleanup(void)
|
2002-10-22 08:44:33 +00:00
|
|
|
{
|
2009-10-12 19:27:37 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-10-22 08:44:33 +00:00
|
|
|
if (circuit_hashtable != NULL)
|
|
|
|
g_hash_table_destroy(circuit_hashtable);
|
|
|
|
|
2009-09-06 18:25:23 +00:00
|
|
|
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)
|
|
|
|
{
|
2010-03-25 19:06:49 +00:00
|
|
|
g_assert(circuit_hashtable == NULL);
|
2002-10-22 08:44:33 +00:00
|
|
|
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 *
|
2002-11-08 01:00:07 +00:00
|
|
|
circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
|
2002-10-22 08:44:33 +00:00
|
|
|
{
|
2002-11-08 01:00:07 +00:00
|
|
|
circuit_t *circuit, *old_circuit;
|
2002-10-22 08:44:33 +00:00
|
|
|
circuit_key *new_key;
|
|
|
|
|
2013-03-20 01:18:10 +00:00
|
|
|
new_key = se_new(struct circuit_key);
|
2002-10-22 08:44:33 +00:00
|
|
|
new_key->ctype = ctype;
|
|
|
|
new_key->circuit_id = circuit_id;
|
|
|
|
|
2013-03-20 01:18:10 +00:00
|
|
|
circuit = se_new(circuit_t);
|
2002-11-08 01:00:07 +00:00
|
|
|
circuit->next = NULL;
|
2002-11-28 08:25:59 +00:00
|
|
|
circuit->first_frame = first_frame;
|
2002-11-08 01:00:07 +00:00
|
|
|
circuit->last_frame = 0; /* not known yet */
|
2002-10-22 08:44:33 +00:00
|
|
|
circuit->index = new_index;
|
|
|
|
circuit->data_list = NULL;
|
|
|
|
circuit->dissector_handle = NULL;
|
|
|
|
circuit->key_ptr = new_key;
|
|
|
|
|
|
|
|
new_index++;
|
|
|
|
|
2002-11-08 01:00:07 +00:00
|
|
|
/*
|
|
|
|
* Is there already a circuit with this circuit ID?
|
|
|
|
*/
|
2013-03-20 01:18:10 +00:00
|
|
|
old_circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, new_key);
|
2002-11-08 01:00:07 +00:00
|
|
|
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
|
2004-01-09 01:02:10 +00:00
|
|
|
* known, make it be the previous frame to this one.
|
2002-11-08 01:00:07 +00:00
|
|
|
*/
|
|
|
|
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);
|
|
|
|
}
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
return circuit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-11-08 01:00:07 +00:00
|
|
|
* 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.
|
2002-10-22 08:44:33 +00:00
|
|
|
* Returns NULL if not found.
|
|
|
|
*/
|
|
|
|
circuit_t *
|
2002-11-08 01:00:07 +00:00
|
|
|
find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
|
2002-10-22 08:44:33 +00:00
|
|
|
{
|
|
|
|
circuit_key key;
|
2002-11-28 08:25:59 +00:00
|
|
|
circuit_t *circuit;
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
key.ctype = ctype;
|
|
|
|
key.circuit_id = circuit_id;
|
2002-11-08 01:00:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* OK, search the list of circuits with that type and ID for
|
|
|
|
* a circuit whose range of frames includes that frame number.
|
|
|
|
*/
|
2013-03-20 01:18:10 +00:00
|
|
|
for (circuit = (circuit_t *)g_hash_table_lookup(circuit_hashtable, &key);
|
2002-11-28 08:25:59 +00:00
|
|
|
circuit != NULL; circuit = circuit->next) {
|
2002-11-08 01:00:07 +00:00
|
|
|
/*
|
2005-02-03 01:24:07 +00:00
|
|
|
* The circuit includes that frame number if:
|
2002-11-08 01:00:07 +00:00
|
|
|
*
|
2002-11-28 08:25:59 +00:00
|
|
|
* the circuit's first frame is unknown or is at or
|
|
|
|
* before that frame
|
2002-11-08 01:00:07 +00:00
|
|
|
*
|
|
|
|
* and
|
|
|
|
*
|
|
|
|
* the circuit's last frame is unknown or is at or
|
|
|
|
* after that frame.
|
|
|
|
*/
|
2002-11-28 08:25:59 +00:00
|
|
|
if ((circuit->first_frame == 0 || circuit->first_frame <= frame)
|
2002-11-08 01:00:07 +00:00
|
|
|
&& (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;
|
2002-10-22 08:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
p_compare(gconstpointer a, gconstpointer b)
|
|
|
|
{
|
2002-11-27 22:44:41 +00:00
|
|
|
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)
|
2002-10-22 08:44:33 +00:00
|
|
|
return 1;
|
2002-11-27 22:44:41 +00:00
|
|
|
else if (ap->proto == bp->proto)
|
2002-10-22 08:44:33 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
|
|
|
|
{
|
2013-03-20 01:18:10 +00:00
|
|
|
circuit_proto_data *p1 = se_new(circuit_proto_data);
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
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
|
2002-10-31 07:12:42 +00:00
|
|
|
circuit_set_dissector(circuit_t *circuit, dissector_handle_t handle)
|
2002-10-22 08:44:33 +00:00
|
|
|
{
|
|
|
|
circuit->dissector_handle = handle;
|
|
|
|
}
|
|
|
|
|
2002-10-31 07:12:42 +00:00
|
|
|
dissector_handle_t
|
|
|
|
circuit_get_dissector(circuit_t *circuit)
|
|
|
|
{
|
|
|
|
return circuit->dissector_handle;
|
|
|
|
}
|
|
|
|
|
2002-10-22 08:44:33 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2002-11-08 01:00:07 +00:00
|
|
|
try_circuit_dissector(circuit_type ctype, guint32 circuit_id, guint32 frame,
|
2013-10-27 15:00:11 +00:00
|
|
|
tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void* data)
|
2002-10-22 08:44:33 +00:00
|
|
|
{
|
|
|
|
circuit_t *circuit;
|
|
|
|
|
2002-11-08 01:00:07 +00:00
|
|
|
circuit = find_circuit(ctype, circuit_id, frame);
|
2002-10-22 08:44:33 +00:00
|
|
|
|
|
|
|
if (circuit != NULL) {
|
2002-10-29 07:22:55 +00:00
|
|
|
if (circuit->dissector_handle == NULL)
|
2002-10-22 08:44:33 +00:00
|
|
|
return FALSE;
|
2013-10-27 15:00:11 +00:00
|
|
|
call_dissector_with_data(circuit->dissector_handle, tvb, pinfo,
|
|
|
|
tree, data);
|
2002-10-22 08:44:33 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|