Rename crypt-xxx to xxx
[obnox/wireshark/wip.git] / epan / conversation.c
index 3f0fac3126c0b6fd36deff9ee52d03c760496a7f..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,24 +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_t *conv = value;
+
+       /* 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)
 {
-       /* The conversation keys are se_ allocated so they are already gone */
+       /*  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.
+        */
        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.
@@ -463,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))) &&
@@ -544,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");
@@ -578,7 +616,7 @@ 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");
@@ -612,8 +650,8 @@ conversation_set_addr2(conversation_t *conv, address *addr)
  * {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;
@@ -682,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;
 
@@ -1003,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));
 
@@ -1017,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;
@@ -1037,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;
@@ -1055,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;
 }
@@ -1071,8 +1109,8 @@ conversation_set_dissector(conversation_t *conversation, dissector_handle_t hand
  * 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;
@@ -1097,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;
+}