ISO14443: Fix Dead Store (Dead assignement/Dead increment) Warning found by Clang
[metze/wireshark/wip.git] / epan / circuit.c
index 3f8a192159fd8fdd57d3ace36812ffd52f8cc389..53b1c9d4134067f8be84c526b5412df65c8f0542 100644 (file)
@@ -1,10 +1,8 @@
 /* circuit.c
  * Routines for building lists of packets that are part of a "circuit"
  *
- * $Id: circuit.c,v 1.3 2002/10/31 07:12:38 guy Exp $
- *
- * Ethereal - Network traffic analyzer
- * By Gerald Combs <gerald@ethereal.com>
+ * 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
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <stdio.h>
+#include "config.h"
 
-#include <string.h>
 #include <glib.h>
 #include "packet.h"
 #include "circuit.h"
  */
 static GHashTable *circuit_hashtable = NULL;
 
-static GMemChunk *circuit_key_chunk = NULL;
-static GMemChunk *circuit_chunk = NULL;
-
 static guint32 new_index;
 
-static int circuit_init_count = 200;
-
 /*
  * Protocol-specific data attached to a circuit_t structure - protocol
  * index and opaque pointer.
@@ -54,15 +42,13 @@ typedef struct _circuit_proto_data {
        void    *proto_data;
 } circuit_proto_data;
 
-static GMemChunk *circuit_proto_data_area = NULL;
-
 /*
  * Compute the hash value for a circuit.
  */
 static guint
 circuit_hash(gconstpointer v)
 {
-       circuit_key *key = (circuit_key *)v;
+       const circuit_key *key = (const circuit_key *)v;
 
        return key->ctype ^ key->circuit_id;
 }
@@ -73,54 +59,42 @@ circuit_hash(gconstpointer v)
 static gint
 circuit_match(gconstpointer v, gconstpointer w)
 {
-       circuit_key *v1 = (circuit_key *)v;
-       circuit_key *v2 = (circuit_key *)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;
 }
 
 /*
- * Initialize some variables every time a file is loaded or re-loaded.
- * Destroy all existing circuits, and create a new hash table
- * for the circuits in the new file.
+ * Destroy all existing circuits.
  */
 void
-circuit_init(void)
+circuit_cleanup(void)
 {
-       if (circuit_hashtable != NULL)
-               g_hash_table_destroy(circuit_hashtable);
-       if (circuit_key_chunk != NULL)
-               g_mem_chunk_destroy(circuit_key_chunk);
-       if (circuit_chunk != NULL)
-               g_mem_chunk_destroy(circuit_chunk);
-
        /*
-        * Free up any space allocated for circuit protocol data
-        * areas.
+        * Free up any space allocated for the circuit hashtable.
         *
-        * We can free the space, as the structures it contains are
-        * pointed to by circuit data structures that were freed
-        * above.
+        * 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_proto_data_area != NULL)
-               g_mem_chunk_destroy(circuit_proto_data_area);
 
-       circuit_hashtable = g_hash_table_new(circuit_hash, circuit_match);
-       circuit_key_chunk = g_mem_chunk_new("circuit_key_chunk",
-           sizeof(circuit_key),
-           circuit_init_count * sizeof(struct circuit_key),
-           G_ALLOC_AND_FREE);
-       circuit_chunk = g_mem_chunk_new("circuit_chunk",
-           sizeof(circuit_t),
-           circuit_init_count * sizeof(circuit_t),
-           G_ALLOC_AND_FREE);
+       if (circuit_hashtable != NULL)
+               g_hash_table_destroy(circuit_hashtable);
 
-       /*
-        * Allocate a new area for circuit protocol data items.
-        */
-       circuit_proto_data_area = g_mem_chunk_new("circuit_proto_data_area",
-           sizeof(circuit_proto_data), 20 * sizeof(circuit_proto_data), /* FIXME*/
-           G_ALLOC_ONLY);
+       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.
@@ -133,52 +107,115 @@ circuit_init(void)
  * to contain packets for that circuit.
  */
 circuit_t *
-circuit_new(circuit_type ctype, guint32 circuit_id)
+circuit_new(circuit_type ctype, guint32 circuit_id, guint32 first_frame)
 {
-       circuit_t *circuit;
+       circuit_t *circuit, *old_circuit;
        circuit_key *new_key;
 
-       new_key = g_mem_chunk_alloc(circuit_key_chunk);
+       new_key = wmem_new(wmem_file_scope(), struct circuit_key);
        new_key->ctype = ctype;
        new_key->circuit_id = circuit_id;
 
-       circuit = g_mem_chunk_alloc(circuit_chunk);
+       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;
-
-/* clear dissector handle */
        circuit->dissector_handle = NULL;
-
-/* set the options and key pointer */
        circuit->key_ptr = new_key;
 
        new_index++;
 
-       g_hash_table_insert(circuit_hashtable, new_key, circuit);
+       /*
+        * 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, search for the corresponding 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)
+find_circuit(circuit_type ctype, guint32 circuit_id, guint32 frame)
 {
        circuit_key key;
+       circuit_t *circuit;
 
        key.ctype = ctype;
        key.circuit_id = circuit_id;
-       return g_hash_table_lookup(circuit_hashtable, &key);
+
+       /*
+        * 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)
 {
-       if (((circuit_proto_data *)a)->proto > ((circuit_proto_data *)b)->proto)
+       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 (((circuit_proto_data *)a)->proto == ((circuit_proto_data *)b)->proto)
+       else if (ap->proto == bp->proto)
                return 0;
        else
                return -1;
@@ -187,7 +224,7 @@ p_compare(gconstpointer a, gconstpointer b)
 void
 circuit_add_proto_data(circuit_t *conv, int proto, void *proto_data)
 {
-       circuit_proto_data *p1 = g_mem_chunk_alloc(circuit_proto_data_area);
+       circuit_proto_data *p1 = wmem_new(wmem_file_scope(), circuit_proto_data);
 
        p1->proto = proto;
        p1->proto_data = proto_data;
@@ -252,19 +289,32 @@ circuit_get_dissector(circuit_t *circuit)
  * call that dissector and return TRUE, otherwise return FALSE.
  */
 gboolean
-try_circuit_dissector(circuit_type ctype, guint32 circuit_id, tvbuff_t *tvb,
-    packet_info *pinfo, proto_tree *tree)
+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);
+       circuit = find_circuit(ctype, circuit_id, frame);
 
        if (circuit != NULL) {
                if (circuit->dissector_handle == NULL)
                        return FALSE;
-               call_dissector(circuit->dissector_handle, tvb, pinfo,
-                   tree);
+               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:
+ */