-ftbfs
[oweals/gnunet.git] / src / set / gnunet-service-set.c
index 9210e1ea83cd304f5086c57e5bb249fea5de1f0d..99deb3b23d7ddf2cb131ca32b0b524f8e4b803f1 100644 (file)
@@ -4,7 +4,7 @@
 
       GNUnet 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, or (at your
+      by the Free Software Foundation; either version 3, or (at your
       option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
@@ -291,7 +291,9 @@ static void
 set_destroy (struct Set *set)
 {
   /* If the client is not dead yet, destroy it.
-   * The client's destroy callback will destroy the set again. */
+   * The client's destroy callback will destroy the set again.
+   * We do this so that the tunnel end handler still has a valid set handle
+   * to destroy. */
   if (NULL != set->client)
   {
     struct GNUNET_SERVER_Client *client = set->client;
@@ -299,11 +301,20 @@ set_destroy (struct Set *set)
     GNUNET_SERVER_client_disconnect (client);
     return;
   }
+  GNUNET_assert (NULL != set->state);
+  set->vt->destroy_set (set->state);
+  set->state = NULL;
   if (NULL != set->client_mq)
   {
     GNUNET_MQ_destroy (set->client_mq);
     set->client_mq = NULL;
   }
+  if (NULL != set->iter)
+  {
+    GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
+    set->iter = NULL;
+  }
+  GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
   if (NULL != set->elements)
   {
     GNUNET_CONTAINER_multihashmap_iterate (set->elements,
@@ -311,15 +322,6 @@ set_destroy (struct Set *set)
     GNUNET_CONTAINER_multihashmap_destroy (set->elements);
     set->elements = NULL;
   }
-  if (NULL != set->iter)
-  {
-    GNUNET_CONTAINER_multihashmap_iterator_destroy (set->iter);
-    set->iter = NULL;
-  }
-  GNUNET_assert (NULL != set->state);
-  set->vt->destroy_set (set->state);
-  set->state = NULL;
-  GNUNET_CONTAINER_DLL_remove (sets_head, sets_tail, set);
   GNUNET_free (set);
 }
 
@@ -528,7 +530,7 @@ handle_client_iterate (void *cls,
                        const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  
+
   set = set_get (client);
   if (NULL == set)
   {
@@ -715,7 +717,7 @@ handle_client_add_remove (void *cls,
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
   msg = (const struct GNUNET_SET_ElementMessage *) m;
   el.size = ntohs (m->size) - sizeof *msg;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "client ins/rem element of size %u\n", el.size);
   el.data = &msg[1];
   if (GNUNET_MESSAGE_TYPE_SET_REMOVE == ntohs (m->type))
@@ -753,7 +755,7 @@ handle_client_add_remove (void *cls,
                                                 &ee->element_hash);
     if (NULL != ee_dup)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "element inserted twice, ignoring\n");
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "element inserted twice, ignoring\n");
       GNUNET_free (ee);
       return;
     }
@@ -827,7 +829,7 @@ handle_client_iter_ack (void *cls,
                    const struct GNUNET_MessageHeader *m)
 {
   struct Set *set;
-  
+
   set = set_get (client);
   if (NULL == set)
   {
@@ -1021,7 +1023,7 @@ tunnel_new_cb (void *cls,
   incoming->tc = GNUNET_new (struct TunnelContext);;
   incoming->tc->vt = &incoming_vt;
   incoming->tc->op = (struct OperationState *) incoming;
-  incoming->timeout_task = 
+  incoming->timeout_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, incoming_timeout_cb, incoming);
   GNUNET_CONTAINER_DLL_insert_tail (incoming_head, incoming_tail, incoming);
 
@@ -1055,7 +1057,7 @@ tunnel_end_cb (void *cls,
 /**
  * Functions with this signature are called whenever a message is
  * received.
- * 
+ *
  * Each time the function must call GNUNET_MESH_receive_done on the tunnel
  * in order to receive the next message. This doesn't need to be immediate:
  * can be delayed if some processing is done on the message.
@@ -1064,7 +1066,7 @@ tunnel_end_cb (void *cls,
  * @param tunnel Connection to the other end.
  * @param tunnel_ctx Place to store local state associated with the tunnel.
  * @param message The actual message.
- * 
+ *
  * @return GNUNET_OK to keep the tunnel open,
  *         GNUNET_SYSERR to close it (signal serious error).
  */
@@ -1079,7 +1081,7 @@ dispatch_p2p_message (void *cls,
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "dispatching mesh message (type: %u)\n",
               ntohs (message->type));
-  /* FIXME: do this before or after the handler? */
+  /* do this before the handler, as the handler might kill the tunnel */
   GNUNET_MESH_receive_done (tunnel);
   ret = tc->vt->msg_handler (tc->op, message);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "handled mesh message (type: %u)\n",