tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / transport / gnunet-service-transport_hello.c
index 4607902a280ca5c218f0d0d032b7714591b99da5..2d744065054fad2c879586cb02a7b66e8ebc926b 100644 (file)
@@ -1,21 +1,21 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2010,2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010,2011 GNUnet e.V.
 
-     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 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
+    
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-     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., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -76,6 +76,13 @@ struct OwnAddressList
    */
   struct GNUNET_CRYPTO_EddsaSignature pong_signature;
 
+  /**
+   * How often has this address been added/removed? Used as
+   * some plugins may learn the same external address from
+   * multiple origins.
+   */
+  unsigned int rc;
+
 };
 
 
@@ -133,7 +140,7 @@ struct GeneratorContext
 
 
 /**
- * Add an address from the 'OwnAddressList' to the buffer.
+ * Add an address from the `struct OwnAddressList` to the buffer.
  *
  * @param cls the `struct GeneratorContext`
  * @param max maximum number of bytes left
@@ -151,8 +158,10 @@ address_generator (void *cls,
 
   if (NULL == gc->addr_pos)
     return GNUNET_SYSERR; /* Done */
-  ret = GNUNET_HELLO_add_address (gc->addr_pos->address, gc->expiration, buf,
-                                max);
+  ret = GNUNET_HELLO_add_address (gc->addr_pos->address,
+                                  gc->expiration,
+                                  buf,
+                                  max);
   gc->addr_pos = gc->addr_pos->next;
   return ret;
 }
@@ -163,11 +172,9 @@ address_generator (void *cls,
  * all of the transports.
  *
  * @param cls unused
- * @param tc scheduler context
  */
 static void
-refresh_hello_task (void *cls,
-                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+refresh_hello_task (void *cls)
 {
   struct GeneratorContext gc;
 
@@ -234,7 +241,7 @@ GST_hello_start (int friend_only,
   hello_cb = cb;
   hello_cb_cls = cb_cls;
   friend_option = friend_only;
-  refresh_hello_task (NULL, NULL);
+  refresh_hello_task (NULL);
 }
 
 
@@ -267,7 +274,7 @@ GST_hello_stop ()
 const struct GNUNET_MessageHeader *
 GST_hello_get ()
 {
-  return (struct GNUNET_MessageHeader *) our_hello;
+  return (const struct GNUNET_MessageHeader *) our_hello;
 }
 
 
@@ -284,28 +291,44 @@ GST_hello_modify_addresses (int addremove,
   struct OwnAddressList *al;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              (addremove ==
-               GNUNET_YES) ? "Adding `%s' to the set of our addresses\n" :
-              "Removing `%s' from the set of our addresses\n",
+              (GNUNET_YES == addremove)
+              ? "Adding `%s' to the set of our addresses\n"
+              "Removing `%s' from the set of our addresses\n",
               GST_plugins_a2s (address));
-  GNUNET_assert (address != NULL);
+  GNUNET_assert (NULL != address);
+  for (al = oal_head; al != NULL; al = al->next)
+    if (0 == GNUNET_HELLO_address_cmp (address, al->address))
+      break;
   if (GNUNET_NO == addremove)
   {
-    for (al = oal_head; al != NULL; al = al->next)
-      if (0 == GNUNET_HELLO_address_cmp (address, al->address))
-      {
-        GNUNET_CONTAINER_DLL_remove (oal_head, oal_tail, al);
-        GNUNET_HELLO_address_free (al->address);
-        GNUNET_free (al);
-        refresh_hello ();
-        return;
-      }
-    /* address to be removed not found!? */
-    GNUNET_break (0);
+    if (NULL == al)
+    {
+      /* address to be removed not found!? */
+      GNUNET_break (0);
+      return;
+    }
+    al->rc--;
+    if (0 != al->rc)
+      return; /* RC not yet zero */
+    GNUNET_CONTAINER_DLL_remove (oal_head,
+                                 oal_tail,
+                                 al);
+    GNUNET_HELLO_address_free (al->address);
+    GNUNET_free (al);
+    refresh_hello ();
+    return;
+  }
+  if (NULL != al)
+  {
+    /* address added twice or more */
+    al->rc++;
     return;
   }
   al = GNUNET_new (struct OwnAddressList);
-  GNUNET_CONTAINER_DLL_insert (oal_head, oal_tail, al);
+  al->rc = 1;
+  GNUNET_CONTAINER_DLL_insert (oal_head,
+                               oal_tail,
+                               al);
   al->address = GNUNET_HELLO_address_copy (address);
   refresh_hello ();
 }