From Cristian Constantin:
[obnox/wireshark/wip.git] / epan / conversation.c
index c7a44e9bf7c34eae258a17600864252422dadacb..63579f6a4d717c59d79ea059821b8064366cdc1d 100644 (file)
@@ -97,7 +97,7 @@ typedef struct _conv_proto_data {
  * options bits are set (NO_ADDR2 and NO_PORT2).
  */
 static conversation_t *
-conversation_create_from_template(conversation_t *conversation, address *addr2, guint32 port2)
+conversation_create_from_template(conversation_t *conversation, const address *addr2, const guint32 port2)
 {
    /*
     * Add a new conversation and keep the conversation template only if the
@@ -187,17 +187,11 @@ conversation_hash_exact(gconstpointer v)
 {
        const conversation_key *key = (const conversation_key *)v;
        guint hash_val;
-       int i;
 
        hash_val = 0;
-       for (i = 0; i < key->addr1.len; i++)
-               hash_val += key->addr1.data[i];
-
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
        hash_val += key->port1;
-
-       for (i = 0; i < key->addr2.len; i++)
-               hash_val += key->addr2.data[i];
-
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
        hash_val += key->port2;
 
        return hash_val;
@@ -264,14 +258,10 @@ conversation_hash_no_addr2(gconstpointer v)
 {
        const conversation_key *key = (const conversation_key *)v;
        guint hash_val;
-       int i;
 
        hash_val = 0;
-       for (i = 0; i < key->addr1.len; i++)
-               hash_val += key->addr1.data[i];
-
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
        hash_val += key->port1;
-
        hash_val += key->port2;
 
        return hash_val;
@@ -322,16 +312,11 @@ conversation_hash_no_port2(gconstpointer v)
 {
        const conversation_key *key = (const conversation_key *)v;
        guint hash_val;
-       int i;
 
        hash_val = 0;
-       for (i = 0; i < key->addr1.len; i++)
-               hash_val += key->addr1.data[i];
-
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
        hash_val += key->port1;
-
-       for (i = 0; i < key->addr2.len; i++)
-               hash_val += key->addr2.data[i];
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr2);
 
        return hash_val;
 }
@@ -381,12 +366,9 @@ conversation_hash_no_addr2_or_port2(gconstpointer v)
 {
        const conversation_key *key = (const conversation_key *)v;
        guint hash_val;
-       int i;
 
        hash_val = 0;
-       for (i = 0; i < key->addr1.len; i++)
-               hash_val += key->addr1.data[i];
-
+       ADD_ADDRESS_TO_HASH(hash_val, &key->addr1);
        hash_val += key->port1;
 
        return hash_val;
@@ -427,47 +409,62 @@ conversation_match_no_addr2_or_port2(gconstpointer v, gconstpointer w)
 }
 
 /*
- * Initialize some variables every time a file is loaded or re-loaded.
- * Destroy all existing conversations, and create a new hash table
- * for the conversations in the new file.
+ * Free the proto_data.  The conversation itself is se_allocated.
  */
 void
-conversation_init(void)
+free_data_list(gpointer key _U_, gpointer value, gpointer user_data _U_)
 {
-       conversation_key *key;
+       conversation_t *conv = value;
 
-       /*
-        * Free the addresses associated with the conversation keys.
+       /* TODO: se_slist? */
+       g_slist_free(conv->data_list);
+
+       /* Not really necessary, but... */
+       conv->data_list = NULL;
+
+}
+
+/*
+ * Destroy all existing conversations
+ */
+void
+conversation_cleanup(void)
+{
+       /*  Clean up the hash tables, but only after freeing any proto_data
+        *  that may be hanging off the conversations.
+        *  The conversation keys are se_ allocated so we don't have to clean them up.
         */
-       for (key = conversation_keys; key != NULL; key = key->next) {
-               /*
-                * Grr.  I guess the theory here is that freeing
-                * something sure as heck modifies it, so you
-                * want to ban attempts to free it, but, alas,
-                * if we make the "data" field of an "address"
-                * structure not a "const", the compiler whines if
-                * we try to make it point into the data for a packet,
-                * as that's a "const" array (and should be, as dissectors
-                * shouldn't trash it).
-                *
-                * So we cast the complaint into oblivion, and rely on
-                * the fact that these addresses are known to have had
-                * their data mallocated, i.e. they don't point into,
-                * say, the middle of the data for a packet.
-                */
-               g_free((gpointer)key->addr1.data);
-               g_free((gpointer)key->addr2.data);
-       }
        conversation_keys = NULL;
-       if (conversation_hashtable_exact != NULL)
+       if (conversation_hashtable_exact != NULL) {
+               g_hash_table_foreach(conversation_hashtable_exact, free_data_list, NULL);
                g_hash_table_destroy(conversation_hashtable_exact);
-       if (conversation_hashtable_no_addr2 != NULL)
+       }
+       if (conversation_hashtable_no_addr2 != NULL) {
+               g_hash_table_foreach(conversation_hashtable_no_addr2, free_data_list, NULL);
                g_hash_table_destroy(conversation_hashtable_no_addr2);
-       if (conversation_hashtable_no_port2 != NULL)
+       }
+       if (conversation_hashtable_no_port2 != NULL) {
+               g_hash_table_foreach(conversation_hashtable_no_port2, free_data_list, NULL);
                g_hash_table_destroy(conversation_hashtable_no_port2);
-       if (conversation_hashtable_no_addr2_or_port2 != NULL)
+       }
+       if (conversation_hashtable_no_addr2_or_port2 != NULL) {
+               g_hash_table_foreach(conversation_hashtable_no_addr2_or_port2, free_data_list, NULL);
                g_hash_table_destroy(conversation_hashtable_no_addr2_or_port2);
+       }
 
+       conversation_hashtable_exact = NULL;
+       conversation_hashtable_no_addr2 = NULL;
+       conversation_hashtable_no_port2 = NULL;
+       conversation_hashtable_no_addr2_or_port2 = NULL;
+}
+
+/*
+ * Initialize some variables every time a file is loaded or re-loaded.
+ * Create a new hash table for the conversations in the new file.
+ */
+void
+conversation_init(void)
+{
        /*
         * Free up any space allocated for conversation protocol data
         * areas.
@@ -504,18 +501,18 @@ conversation_init(void)
  * when searching for this conversation.
  */
 conversation_t *
-conversation_new(guint32 setup_frame, address *addr1, address *addr2, port_type ptype,
-    guint32 port1, guint32 port2, guint options)
+conversation_new(const guint32 setup_frame, const address *addr1, const address *addr2, const port_type ptype,
+    const guint32 port1, const guint32 port2, const guint options)
 {
 /*
        DISSECTOR_ASSERT(!(options | CONVERSATION_TEMPLATE) || ((options | (NO_ADDR2 | NO_PORT2 | NO_PORT2_FORCE))) &&
                                "A conversation template may not be constructed without wildcard options");
 */
        GHashTable* hashtable;
-       conversation_t *conversation;
-       conversation_t *tc;
+       conversation_t *conversation=NULL, *prev=NULL;
        conversation_key existing_key;
        conversation_key *new_key;
+       guint conv_in_ht=0;
 
        if (options & NO_ADDR2) {
                if (options & (NO_PORT2|NO_PORT2_FORCE)) {
@@ -538,27 +535,50 @@ conversation_new(guint32 setup_frame, address *addr1, address *addr2, port_type
        existing_key.port2 = port2;
 
        conversation = g_hash_table_lookup(hashtable, &existing_key);
-       tc = conversation; /* Remember if lookup was successful */
+       if(NULL!=conversation)
+               conv_in_ht=1;
 
        new_key = se_alloc(sizeof(struct conversation_key));
        new_key->next = conversation_keys;
        conversation_keys = new_key;
-       COPY_ADDRESS(&new_key->addr1, addr1);
-       COPY_ADDRESS(&new_key->addr2, addr2);
+       SE_COPY_ADDRESS(&new_key->addr1, addr1);
+       SE_COPY_ADDRESS(&new_key->addr2, addr2);
        new_key->ptype = ptype;
        new_key->port1 = port1;
        new_key->port2 = port2;
 
        if (conversation) {
-               for (; conversation->next; conversation = conversation->next)
-                       ;
-               conversation->next = se_alloc(sizeof(conversation_t));
-               conversation = conversation->next;
+               /* the list is ordered on setup_frame */
+               if(setup_frame>=conversation->last->setup_frame) {
+                       /* add it to the end */
+                       conversation->last->next=se_alloc(sizeof(conversation_t));
+                       prev=conversation->last->next;
+                       prev->next=NULL;
+                       conversation->last=prev;
+                       conversation = prev;
+               } else {
+                       for (prev=NULL; (setup_frame>conversation->setup_frame) && conversation->next; prev=conversation, conversation = conversation->next)
+                               ;
+                       if(prev) {
+                               prev->next = se_alloc(sizeof(conversation_t));
+                               prev->next->next = conversation;
+                               conversation = prev->next;
+                       } else {
+                               /* change the head of the list */
+                               prev = se_alloc(sizeof(conversation_t));
+                               prev->next = conversation;
+                               prev->last = conversation->last;
+                               conversation->last=NULL;
+                               conversation = prev;
+                               conv_in_ht=0;
+                       }
+               }
        } else {
                conversation = se_alloc(sizeof(conversation_t));
+               conversation->next = NULL;
+               conversation->last = conversation;
        }
 
-       conversation->next = NULL;
        conversation->index = new_index;
        conversation->setup_frame = setup_frame;
        conversation->data_list = NULL;
@@ -574,7 +594,7 @@ conversation_new(guint32 setup_frame, address *addr1, address *addr2, port_type
 
        /* only insert a hash table entry if this
         * is the first conversation with this key */
-       if (!tc)
+       if (!conv_in_ht)
                g_hash_table_insert(hashtable, new_key, conversation);
 
        return conversation;
@@ -585,7 +605,7 @@ conversation_new(guint32 setup_frame, address *addr1, address *addr2, port_type
  * update the options and port values, insert the updated key.
  */
 void
-conversation_set_port2(conversation_t *conv, guint32 port)
+conversation_set_port2(conversation_t *conv, const guint32 port)
 {
    DISSECTOR_ASSERT(!(conv->options & CONVERSATION_TEMPLATE) &&
             "Use the conversation_create_from_template function when the CONVERSATION_TEMPLATE bit is set in the options mask");
@@ -619,11 +639,11 @@ conversation_set_port2(conversation_t *conv, guint32 port)
  * table, update the options and port values, insert the updated key.
  */
 void
-conversation_set_addr2(conversation_t *conv, address *addr)
+conversation_set_addr2(conversation_t *conv, const address *addr)
 {
    DISSECTOR_ASSERT(!(conv->options & CONVERSATION_TEMPLATE) &&
             "Use the conversation_create_from_template function when the CONVERSATION_TEMPLATE bit is set in the options mask");
-   
+
        /*
         * If the address 2 value is not wildcarded, don't set it.
         */
@@ -638,7 +658,7 @@ conversation_set_addr2(conversation_t *conv, address *addr)
                    conv->key_ptr);
        }
        conv->options &= ~NO_ADDR2;
-       COPY_ADDRESS(&conv->key_ptr->addr2, addr);
+       SE_COPY_ADDRESS(&conv->key_ptr->addr2, addr);
        if (conv->options & NO_PORT2) {
                g_hash_table_insert(conversation_hashtable_no_port2,
                    conv->key_ptr, conv);
@@ -649,16 +669,17 @@ conversation_set_addr2(conversation_t *conv, address *addr)
 }
 
 /*
- * Search a particular hash table for a conversaton with the specified
- * addr1, port1, addr2, and port2.
+ * Search a particular hash table for a conversation with the specified
+ * {addr1, port1, addr2, port2} and set up before frame_num.
  */
 static conversation_t *
-conversation_lookup_hashtable(GHashTable *hashtable, guint32 frame_num, address *addr1, address *addr2,
-    port_type ptype, guint32 port1, guint32 port2)
+conversation_lookup_hashtable(GHashTable *hashtable, const guint32 frame_num, const address *addr1, const address *addr2,
+    const port_type ptype, const guint32 port1, const guint32 port2)
 {
-       conversation_t* conversation;
-       conversation_t* match;
+       conversation_t* conversation=NULL;
+       conversation_t* match=NULL;
        conversation_key key;
+       guint found=0;
 
        /*
         * We don't make a copy of the address data, we just copy the
@@ -671,13 +692,24 @@ conversation_lookup_hashtable(GHashTable *hashtable, guint32 frame_num, address
        key.port2 = port2;
 
        match = g_hash_table_lookup(hashtable, &key);
+               
+       if (match && (match->setup_frame > frame_num))
+               match = NULL;
 
        if (match) {
+               if(match->last->setup_frame<=frame_num)
+                       return match;
                for (conversation = match->next; conversation; conversation = conversation->next) {
-                       if ((conversation->setup_frame < frame_num)
-                               && (conversation->setup_frame > match->setup_frame))
-                               match = conversation;
+                       if ((conversation->setup_frame <= frame_num)
+                               && (conversation->setup_frame > match->setup_frame)) {
+                                       match = conversation;
+                                       found=1;
+                       } else if(conversation->setup_frame>frame_num)
+                               /* we are past the frame_num */
+                               break;
                }
+               if(!found)
+                       match=NULL;     
        }
 
        return match;
@@ -721,8 +753,8 @@ conversation_lookup_hashtable(GHashTable *hashtable, guint32 frame_num, address
  *     otherwise, we found no matching conversation, and return NULL.
  */
 conversation_t *
-find_conversation(guint32 frame_num, address *addr_a, address *addr_b, port_type ptype,
-    guint32 port_a, guint32 port_b, guint options)
+find_conversation(const guint32 frame_num, const address *addr_a, const address *addr_b, const port_type ptype,
+    const guint32 port_a, const guint32 port_b, const guint options)
 {
    conversation_t *conversation;
 
@@ -992,7 +1024,7 @@ find_conversation(guint32 frame_num, address *addr_a, address *addr_b, port_type
       conversation =
       conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
       frame_num, addr_b, addr_a, ptype, port_a, port_b);
-   else 
+   else
       conversation =
       conversation_lookup_hashtable(conversation_hashtable_no_addr2_or_port2,
       frame_num, addr_b, addr_a, ptype, port_b, port_a);
@@ -1042,7 +1074,7 @@ p_compare(gconstpointer a, gconstpointer b)
 }
 
 void
-conversation_add_proto_data(conversation_t *conv, int proto, void *proto_data)
+conversation_add_proto_data(conversation_t *conv, const int proto, void *proto_data)
 {
        conv_proto_data *p1 = se_alloc(sizeof(conv_proto_data));
 
@@ -1056,7 +1088,7 @@ conversation_add_proto_data(conversation_t *conv, int proto, void *proto_data)
 }
 
 void *
-conversation_get_proto_data(conversation_t *conv, int proto)
+conversation_get_proto_data(const conversation_t *conv, const int proto)
 {
        conv_proto_data temp, *p1;
        GSList *item;
@@ -1076,7 +1108,7 @@ conversation_get_proto_data(conversation_t *conv, int proto)
 }
 
 void
-conversation_delete_proto_data(conversation_t *conv, int proto)
+conversation_delete_proto_data(conversation_t *conv, const int proto)
 {
        conv_proto_data temp;
        GSList *item;
@@ -1094,7 +1126,7 @@ conversation_delete_proto_data(conversation_t *conv, int proto)
 }
 
 void
-conversation_set_dissector(conversation_t *conversation, dissector_handle_t handle)
+conversation_set_dissector(conversation_t *conversation, const dissector_handle_t handle)
 {
        conversation->dissector_handle = handle;
 }
@@ -1106,12 +1138,12 @@ conversation_set_dissector(conversation_t *conversation, dissector_handle_t hand
  *
  * This helper uses call_dissector_only which will NOT call the default
  * "data" dissector if the packet was rejected.
- * Our caller is responsible to call the data dissector explicitely in case 
+ * Our caller is responsible to call the data dissector explicitely in case
  * this function returns FALSE.
  */
 gboolean
-try_conversation_dissector(address *addr_a, address *addr_b, port_type ptype,
-    guint32 port_a, guint32 port_b, tvbuff_t *tvb, packet_info *pinfo,
+try_conversation_dissector(const address *addr_a, const address *addr_b, const port_type ptype,
+    const guint32 port_a, const guint32 port_b, tvbuff_t *tvb, packet_info *pinfo,
     proto_tree *tree)
 {
        conversation_t *conversation;
@@ -1136,3 +1168,27 @@ try_conversation_dissector(address *addr_a, address *addr_b, port_type ptype,
        }
        return FALSE;
 }
+
+/*  A helper function that calls find_conversation() and, if a conversation is
+ *  not found, calls conversation_new().
+ *  The frame number and addresses are taken from pinfo.
+ *  No options are used, though we could extend this API to include an options
+ *  parameter.
+ */
+conversation_t *
+find_or_create_conversation(packet_info *pinfo)
+{
+       conversation_t *conv=NULL;
+
+       /* Have we seen this conversation before? */
+       if((conv = find_conversation(pinfo->fd->num, &pinfo->src, &pinfo->dst,
+                                    pinfo->ptype, pinfo->srcport,
+                                    pinfo->destport, 0)) == NULL) {
+               /* No, this is a new conversation. */
+               conv = conversation_new(pinfo->fd->num, &pinfo->src,
+                                       &pinfo->dst, pinfo->ptype,
+                                       pinfo->srcport, pinfo->destport, 0);
+       }
+
+       return conv;
+}