trying to fix #4003
[oweals/gnunet.git] / src / transport / gnunet-service-transport_ats.c
index 5301090ff6903fe3182900ce6675466d9f35238b..b41ce8987723138195cd8221ecca275389ccbe53 100644 (file)
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 /**
  * @file transport/gnunet-service-transport_ats.h
@@ -80,6 +80,12 @@ struct AddressInfo
    */
   struct GNUNET_SCHEDULER_Task *unblock_task;
 
+  /**
+   * Set to #GNUNET_YES if the address has expired but we could
+   * not yet remove it because we still have a valid session.
+   */
+  int expired;
+
 };
 
 
@@ -193,7 +199,8 @@ find_ai (const struct GNUNET_HELLO_Address *address,
 
 
 /**
- * Find matching address info, ignoring sessions.
+ * Find matching address info, ignoring sessions and expired
+ * addresses.
  *
  * @param cls the `struct FindClosure`
  * @param key which peer is this about
@@ -208,6 +215,8 @@ find_ai_no_session_cb (void *cls,
   struct FindClosure *fc = cls;
   struct AddressInfo *ai = value;
 
+  if (ai->expired)
+    return GNUNET_YES; /* expired do not count here */
   if (0 ==
       GNUNET_HELLO_address_cmp (fc->address,
                                 ai->address))
@@ -243,11 +252,11 @@ find_ai_no_session (const struct GNUNET_HELLO_Address *address)
 
 
 /**
- * Test if ATS knows about this address.
+ * Test if ATS knows about this @a address and @a session.
  *
  * @param address the address
  * @param session the session
- * @return #GNUNET_YES if address is known, #GNUNET_NO if not.
+ * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
  */
 int
 GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
@@ -257,6 +266,23 @@ GST_ats_is_known (const struct GNUNET_HELLO_Address *address,
 }
 
 
+/**
+ * Test if ATS knows about this @a address.
+ *
+ * @param address the address
+ * @return #GNUNET_YES if @a address is known, #GNUNET_NO if not.
+ */
+int
+GST_ats_is_known_no_session (const struct GNUNET_HELLO_Address *address)
+{
+  struct AddressInfo *ai;
+
+  return (NULL != find_ai_no_session (address))
+    ? GNUNET_YES
+    : GNUNET_NO;
+}
+
+
 /**
  * The blocking time for an address has expired, allow ATS to
  * suggest it again.
@@ -303,7 +329,7 @@ GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
   ai = find_ai (address, session);
   if (NULL == ai)
   {
-    GNUNET_break (0);
+    GNUNET_assert (0);
     return;
   }
   if (NULL == ai->ar)
@@ -330,7 +356,8 @@ GST_ats_block_address (const struct GNUNET_HELLO_Address *address,
   /* destroy session and address */
   if ( (NULL == session) ||
        (GNUNET_NO ==
-        GNUNET_ATS_address_del_session (ai->ar, session)) )
+        GNUNET_ATS_address_del_session (ai->ar,
+                                        session)) )
     GNUNET_ATS_address_destroy (ai->ar);
   ai->ar = NULL;
 
@@ -458,6 +485,7 @@ GST_ats_add_address (const struct GNUNET_HELLO_Address *address,
                                                     GNUNET_HELLO_ADDRESS_INFO_INBOUND));
   ai = find_ai_no_session (address);
   GNUNET_assert (NULL == ai);
+  GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
   LOG (GNUNET_ERROR_TYPE_INFO,
        "Notifying ATS about peer `%s''s new address `%s'\n",
        GNUNET_i2s (&address->peer),
@@ -516,6 +544,34 @@ GST_ats_new_session (const struct GNUNET_HELLO_Address *address,
 }
 
 
+/**
+ * Release memory used by the given address data.
+ *
+ * @param ai the `struct AddressInfo`
+ */ 
+static void
+destroy_ai (struct AddressInfo *ai)
+{
+  GNUNET_assert (NULL == ai->session);
+  GNUNET_assert (NULL == ai->unblock_task);
+  GNUNET_assert (GNUNET_YES ==
+                 GNUNET_CONTAINER_multipeermap_remove (p2a,
+                                                       &ai->address->peer,
+                                                       ai));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Telling ATS to destroy address from peer %s\n",
+       GNUNET_i2s (&ai->address->peer));
+  if (NULL != ai->ar)
+  {
+    GNUNET_ATS_address_destroy (ai->ar);
+    ai->ar = NULL;
+  }
+  publish_p2a_stat_update ();
+  GNUNET_HELLO_address_free (ai->address);
+  GNUNET_free (ai);
+}
+
+
 /**
  * Notify ATS that the session (but not the address) of
  * a given address is no longer relevant.
@@ -534,7 +590,8 @@ GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
     GNUNET_break (0);
     return;
   }
-  ai = find_ai (address, session);
+  ai = find_ai (address,
+                session);
   if (NULL == ai)
   {
     /* We sometimes create sessions just for sending a PING,
@@ -553,20 +610,34 @@ GST_ats_del_session (const struct GNUNET_HELLO_Address *address,
        "Telling ATS to destroy session %p from peer %s\n",
        session,
        GNUNET_i2s (&address->peer));
+  if (GNUNET_YES == ai->expired) 
+  {
+    /* last reason to keep this 'ai' around is now gone, the
+       session is dead as well, clean up */
+    if (NULL != ai->ar)
+    {
+      GNUNET_break (GNUNET_YES ==
+                   GNUNET_ATS_address_del_session (ai->ar,
+                                                   session));
+    }
+    destroy_ai (ai);
+    return;
+  }
   if (NULL == ai->ar)
   {
-    /* If ATS doesn't know about the address/session, and this
-       was an inbound session that expired, then we must forget
-       about the address as well.  Otherwise, we are done as
-       we have set `ai->session` to NULL already. */
+    /* If ATS doesn't know about the address/session, and this was an
+       inbound session, so we must forget about the address as well.
+       Otherwise, we are done as we have set `ai->session` to NULL
+       already. */
     if (GNUNET_YES ==
-        GNUNET_HELLO_address_check_option (address,
-                                           GNUNET_HELLO_ADDRESS_INFO_INBOUND))
+       GNUNET_HELLO_address_check_option (address,
+                                          GNUNET_HELLO_ADDRESS_INFO_INBOUND))
       GST_ats_expire_address (address);
     return;
   }
   if (GNUNET_YES ==
-      GNUNET_ATS_address_del_session (ai->ar, session))
+      GNUNET_ATS_address_del_session (ai->ar,
+                                      session))
   {
     ai->ar = NULL;
     GST_ats_expire_address (address);
@@ -689,36 +760,18 @@ GST_ats_expire_address (const struct GNUNET_HELLO_Address *address)
     GNUNET_assert (0);
     return;
   }
-  GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multipeermap_remove (p2a,
-                                                       &address->peer,
-                                                       ai));
-  GNUNET_break (NULL == ai->session);
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Telling ATS to destroy address from peer %s\n",
-       GNUNET_i2s (&address->peer));
-  if (NULL != ai->ar)
-  {
-    /* We usually should not have a session here when we
-       expire an address, but during shutdown a session
-       may be active while validation causes the address
-       to 'expire'.  So clean up both if necessary. */
-    if ( (NULL == ai->session) ||
-         (GNUNET_NO ==
-          GNUNET_ATS_address_del_session (ai->ar,
-                                          ai->session)) )
-      GNUNET_ATS_address_destroy (ai->ar);
-    ai->ar = NULL;
-  }
   if (NULL != ai->unblock_task)
   {
     GNUNET_SCHEDULER_cancel (ai->unblock_task);
     ai->unblock_task = NULL;
     num_blocked--;
   }
-  publish_p2a_stat_update ();
-  GNUNET_HELLO_address_free (ai->address);
-  GNUNET_free (ai);
+  if (NULL != ai->session)
+  {
+    ai->expired = GNUNET_YES;
+    return;
+  }
+  destroy_ai (ai);
 }
 
 
@@ -741,29 +794,19 @@ GST_ats_init ()
  * @return #GNUNET_OK (continue to iterate)
  */
 static int
-destroy_ai (void *cls,
-            const struct GNUNET_PeerIdentity *key,
-            void *value)
+destroy_ai_cb (void *cls,
+              const struct GNUNET_PeerIdentity *key,
+              void *value)
 {
   struct AddressInfo *ai = value;
 
-  GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multipeermap_remove (p2a,
-                                                       key,
-                                                       ai));
   if (NULL != ai->unblock_task)
   {
     GNUNET_SCHEDULER_cancel (ai->unblock_task);
     ai->unblock_task = NULL;
     num_blocked--;
   }
-  if (NULL != ai->ar)
-  {
-    GNUNET_ATS_address_destroy (ai->ar);
-    ai->ar = NULL;
-  }
-  GNUNET_HELLO_address_free (ai->address);
-  GNUNET_free (ai);
+  destroy_ai (ai);
   return GNUNET_OK;
 }
 
@@ -775,7 +818,7 @@ void
 GST_ats_done ()
 {
   GNUNET_CONTAINER_multipeermap_iterate (p2a,
-                                         &destroy_ai,
+                                         &destroy_ai_cb,
                                          NULL);
   publish_p2a_stat_update ();
   GNUNET_CONTAINER_multipeermap_destroy (p2a);