Rename crypt-xxx to xxx
[obnox/wireshark/wip.git] / epan / conversation.c
index 95e62c532ec50509ca8b30253e8f151f5f864c44..d40ee7677d51fa4249fdf88324b054c16112c9ab 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
@@ -409,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.
@@ -486,8 +501,8 @@ 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))) &&
@@ -525,8 +540,8 @@ conversation_new(guint32 setup_frame, address *addr1, address *addr2, port_type
        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;
@@ -567,7 +582,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");
@@ -601,11 +616,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.
         */
@@ -620,7 +635,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);
@@ -631,12 +646,12 @@ 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;
@@ -656,10 +671,12 @@ conversation_lookup_hashtable(GHashTable *hashtable, guint32 frame_num, address
 
        if (match) {
                for (conversation = match->next; conversation; conversation = conversation->next) {
-                       if ((conversation->setup_frame < frame_num)
+                       if ((conversation->setup_frame <= frame_num)
                                && (conversation->setup_frame > match->setup_frame))
                                match = conversation;
                }
+               if (match->setup_frame > frame_num)
+                       match = NULL;
        }
 
        return match;
@@ -703,8 +720,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;
 
@@ -974,7 +991,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);
@@ -1024,7 +1041,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));
 
@@ -1038,7 +1055,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;
@@ -1058,7 +1075,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;
@@ -1076,7 +1093,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;
 }
@@ -1088,12 +1105,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;
@@ -1118,3 +1135,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;
+}