- cleanup
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.c
index 7412cee1931bb396888a40f2557b47a5974ea1f7..2a15d922f3762cb3effa742fac1b3be629158e80 100644 (file)
@@ -98,7 +98,7 @@ send_bw_notification (struct ATS_Address *aa)
  * @return GNUNET_OK (continue to iterate)
  */
 static int
-update_bw_simple_it (void *cls, const GNUNET_HashCode * key, void *value)
+update_bw_simple_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct ATS_Address *aa = value;
 
@@ -219,7 +219,7 @@ struct CompareAddressContext
 
 
 static int
-compare_address_it (void *cls, const GNUNET_HashCode * key, void *value)
+compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct CompareAddressContext *cac = cls;
   struct ATS_Address *aa = value;
@@ -276,20 +276,57 @@ find_address (const struct GNUNET_PeerIdentity *peer,
   GNUNET_CONTAINER_multihashmap_get_multiple (addresses, &peer->hashPubKey,
                                               &compare_address_it, &cac);
 
-/*
+#if 0
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
               "exact address: %s           base address: %s\n",
               (cac.exact_address != NULL) ? "YES" : "NO",
               (cac.base_address != NULL) ? "YES" : "NO");
-*/
+#endif
   if (cac.exact_address == NULL)
     return cac.base_address;
   return cac.exact_address;
 }
 
 
+static struct ATS_Address *
+lookup_address (const struct GNUNET_PeerIdentity *peer,
+                const char *plugin_name, const void *plugin_addr,
+                size_t plugin_addr_len, uint32_t session_id,
+                const struct GNUNET_ATS_Information *atsi,
+                uint32_t atsi_count)
+{
+  struct ATS_Address *aa;
+  struct ATS_Address *old;
+
+  aa = create_address (peer,
+                       plugin_name,
+                       plugin_addr, plugin_addr_len,
+                       session_id);
+
+  aa->mlp_information = NULL;
+  aa->ats = GNUNET_malloc (atsi_count * sizeof (struct GNUNET_ATS_Information));
+  aa->ats_count = atsi_count;
+  memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
+
+  /* Get existing address or address with session == 0 */
+  old = find_address (peer, aa);
+  if (old == NULL)
+  {
+    GNUNET_free (aa);
+    return NULL;
+  }
+  else if (old->session_id != session_id)
+  {
+    GNUNET_free (aa);
+    GNUNET_break (0);
+    return NULL;
+  }
+
+  return old;
+}
+
 static int
-compare_address_session_it (void *cls, const GNUNET_HashCode * key, void *value)
+compare_address_session_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct CompareAddressContext *cac = cls;
   struct ATS_Address *aa = value;
@@ -330,7 +367,7 @@ find_exact_address (const struct GNUNET_PeerIdentity *peer,
 
 
 void
-GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
+GAS_addresses_add (const struct GNUNET_PeerIdentity *peer,
                       const char *plugin_name, const void *plugin_addr,
                       size_t plugin_addr_len, uint32_t session_id,
                       const struct GNUNET_ATS_Information *atsi,
@@ -338,7 +375,6 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
 {
   struct ATS_Address *aa;
   struct ATS_Address *old;
-  uint32_t i;
 
   if (GNUNET_NO == running)
     return;
@@ -355,33 +391,26 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
   aa->ats_count = atsi_count;
   memcpy (aa->ats, atsi, atsi_count * sizeof (struct GNUNET_ATS_Information));
 
-#if DEBUG_ATS
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Updating address for peer `%s' %u\n",
-              GNUNET_i2s (peer),
-              session_id);
-#endif
   /* Get existing address or address with session == 0 */
   old = find_address (peer, aa);
   if (old == NULL)
   {
+    /* We have a new address */
     GNUNET_assert (GNUNET_OK ==
                    GNUNET_CONTAINER_multihashmap_put (addresses,
                                                       &peer->hashPubKey, aa,
                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
-#if DEBUG_ATS
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' %X\n",
-                GNUNET_i2s (peer), aa);
-#endif
-    old = aa;
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
+                GNUNET_i2s (peer), session_id, aa);
+    return;
   }
-  else
+
+  if (old->session_id == 0)
   {
-#if DEBUG_ATS
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Updated existing address for peer `%s' %p old session %u new session %u\n",
-                GNUNET_i2s (peer), old,
-                old->session_id, session_id);
-#endif
+    /* We have a base address with out an session, update this address */
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Updated existing address for peer `%s' %p with new session %u\n",
+              GNUNET_i2s (peer), old, session_id);
     GNUNET_free_non_null (old->ats);
     old->session_id = session_id;
     old->ats = NULL;
@@ -390,7 +419,43 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
     old->ats_count = aa->ats_count;
     GNUNET_free (aa->plugin);
     GNUNET_free (aa);
+    return;
   }
+
+  /* This address and session is already existing */
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+            "Added already existing address for peer `%s' `%s' %p with new session %u\n",
+            GNUNET_i2s (peer), plugin_name, session_id);
+  GNUNET_break (0);
+}
+
+
+void
+GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
+                      const char *plugin_name, const void *plugin_addr,
+                      size_t plugin_addr_len, uint32_t session_id,
+                      const struct GNUNET_ATS_Information *atsi,
+                      uint32_t atsi_count)
+{
+  struct ATS_Address *old;
+  uint32_t i;
+
+  if (GNUNET_NO == running)
+    return;
+
+  GNUNET_assert (NULL != addresses);
+
+  /* Get existing address */
+  old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len,
+                       session_id, atsi, atsi_count);
+  if (old == NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
+                GNUNET_i2s (peer), plugin_name, session_id);
+    GNUNET_break (0);
+    return;
+  }
+
   for (i = 0; i < atsi_count; i++)
     switch (ntohl (atsi[i].type))
     {
@@ -445,7 +510,7 @@ GAS_addresses_update (const struct GNUNET_PeerIdentity *peer,
  * @return GNUNET_OK (continue to iterate)
  */
 static int
-destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
+destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   const struct ATS_Address *info = cls;
   struct ATS_Address *aa = value;
@@ -458,11 +523,11 @@ destroy_by_session_id (void *cls, const GNUNET_HashCode * key, void *value)
       (aa->addr_len == info->addr_len) &&
       (0 == memcmp (info->addr, aa->addr, aa->addr_len)))
   {
-#if VERBOSE
+
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Deleting address for peer `%s': `%s' %u\n",
                 GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
-#endif
+
     if (GNUNET_YES == destroy_address (aa))
       recalculate_assigned_bw ();
     return GNUNET_OK;
@@ -515,10 +580,23 @@ GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
                        size_t plugin_addr_len, uint32_t session_id)
 {
   struct ATS_Address *aa;
+  struct ATS_Address *old;
 
   if (GNUNET_NO == running)
     return;
 
+  /* Get existing address */
+  old = lookup_address(peer, plugin_name, plugin_addr, plugin_addr_len,
+                       session_id, NULL, 0);
+  if (old == NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
+                GNUNET_i2s (peer), plugin_name, session_id);
+    GNUNET_break (0);
+    return;
+  }
+
+
   GNUNET_break (0 < strlen (plugin_name));
   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
 
@@ -540,7 +618,7 @@ GAS_addresses_destroy (const struct GNUNET_PeerIdentity *peer,
  * @return GNUNET_OK (continue to iterate)
  */
 static int
-find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
+find_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct ATS_Address **ap = cls;
   struct ATS_Address *aa = (struct ATS_Address *) value;
@@ -559,13 +637,36 @@ find_address_it (void *cls, const GNUNET_HashCode * key, void *value)
     return GNUNET_OK;
   }
 
-
   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
   aa->blocked_until = GNUNET_TIME_absolute_add (now, aa->block_interval);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Address %p ready for suggestion, block interval now %llu \n", aa, aa->block_interval);
 
+  /* FIXME this is a hack */
+
+
+  if (NULL != ab)
+  {
+    if ((0 == strcmp (ab->plugin, "tcp")) &&
+        (0 == strcmp (aa->plugin, "tcp")))
+    {
+      if ((0 != ab->addr_len) &&
+          (0 == aa->addr_len))
+      {
+        /* saved address was an outbound address, but we have an inbound address */
+        *ap = aa;
+        return GNUNET_OK;
+      }
+      if (0 == ab->addr_len)
+      {
+        /* saved address was an inbound address, so do not overwrite */
+        return GNUNET_OK;
+      }
+    }
+  }
+  /* FIXME end of hack */
+
   if (NULL == ab)
   {
     *ap = aa;
@@ -606,18 +707,19 @@ GAS_addresses_in_use (const struct GNUNET_PeerIdentity *peer,
               GNUNET_i2s (peer), in_use);
 #endif
 
-  struct ATS_Address *aa;
   struct ATS_Address *old;
 
   if (GNUNET_NO == running)
     return GNUNET_SYSERR;
 
-  aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
-  old = find_exact_address (peer, aa);
-  free_address (aa);
-
+  old = lookup_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id, NULL, 0);
   if (NULL == old)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Trying to set unknown address `%s', %s %u %s \n",
+                GNUNET_i2s (peer),
+                plugin_name, session_id,
+                (GNUNET_NO == in_use) ? "NO" : "YES");
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
@@ -739,7 +841,7 @@ GAS_addresses_request_address (const struct GNUNET_PeerIdentity *peer)
 
 
 static int
-reset_address_it (void *cls, const GNUNET_HashCode * key, void *value)
+reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct ATS_Address *aa = value;
 
@@ -881,7 +983,7 @@ GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
  * @return GNUNET_OK (continue to iterate)
  */
 static int
-free_address_it (void *cls, const GNUNET_HashCode * key, void *value)
+free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
 {
   struct ATS_Address *aa = value;