tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / peer.c
index e58b96211c70e7635a58a53b7250bd85d3a77de0..00a259a3b79f8396ef3d97fe511bd71110cd7f07 100644 (file)
@@ -1,21 +1,21 @@
 /*
       This file is part of GNUnet
-     (C) 2006, 2008, 2009 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2006, 2008, 2009 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
  */
 
 /**
  * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
  * @author Christian Grothoff
  */
-
 #include "platform.h"
 #include "gnunet_peer_lib.h"
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
 
 
 struct PeerEntry
@@ -56,11 +55,11 @@ struct PeerEntry
 static struct PeerEntry **table;
 
 /**
- * Hashmap of PeerIdentities to "struct PeerEntry"
+ * Peermap of PeerIdentities to "struct PeerEntry"
  * (for fast lookup).  NULL until the library
  * is actually being used.
  */
-static struct GNUNET_CONTAINER_MultiHashMap *map;
+static struct GNUNET_CONTAINER_MultiPeerMap *map;
 
 /**
  * Size of the "table".
@@ -89,7 +88,7 @@ GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
     return 0;
   if (NULL == map)
     return 0;
-  e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
+  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
   if (NULL == e)
     return 0;
   GNUNET_assert (e->rc > 0);
@@ -114,8 +113,8 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   if (NULL == pid)
     return 0;
   if (NULL == map)
-    map = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
-  e = GNUNET_CONTAINER_multihashmap_get (map, &pid->hashPubKey);
+    map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
+  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
   if (NULL != e)
   {
     GNUNET_assert (e->rc > 0);
@@ -128,12 +127,13 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
     GNUNET_array_grow (table, size, size + 16);
     for (i = ret; i < size; i++)
     {
-      table[i] = GNUNET_malloc (sizeof (struct PeerEntry));
+      table[i] = GNUNET_new (struct PeerEntry);
       table[i]->pid = i + 1;
     }
   }
   if (0 == ret)
   {
+    memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
     table[0]->pid = 0;
     table[0]->rc = 1;
     ret = 1;
@@ -145,8 +145,8 @@ GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
   table[ret]->rc = 1;
   table[ret]->pid = ret;
   GNUNET_break (GNUNET_OK ==
-                GNUNET_CONTAINER_multihashmap_put (map, 
-                                                  &table[ret]->id.hashPubKey,
+                GNUNET_CONTAINER_multipeermap_put (map,
+                                                  &table[ret]->id,
                                                    table[ret],
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   return ret;
@@ -178,8 +178,8 @@ GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
     if (0 == table[id]->rc)
     {
       GNUNET_break (GNUNET_OK ==
-                    GNUNET_CONTAINER_multihashmap_remove (map,
-                                                          &table[id]->id.hashPubKey,
+                    GNUNET_CONTAINER_multipeermap_remove (map,
+                                                          &table[id]->id,
                                                           table[id]));
       table[id]->pid = free_list_start;
       free_list_start = id;
@@ -201,13 +201,14 @@ GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
     return;
   GNUNET_assert (id < size);
   GNUNET_assert (table[id]->rc > 0);
-  GNUNET_assert ((delta >= 0) || (table[id]->rc >= -delta));
+  GNUNET_assert ( (delta >= 0) ||
+                 (table[id]->rc >= (unsigned int) (-delta)) );
   table[id]->rc += delta;
   if (0 == table[id]->rc)
   {
     GNUNET_break (GNUNET_OK ==
-                  GNUNET_CONTAINER_multihashmap_remove (map,
-                                                        &table[id]->id.hashPubKey,
+                  GNUNET_CONTAINER_multipeermap_remove (map,
+                                                        &table[id]->id,
                                                         table[id]));
     table[id]->pid = free_list_start;
     free_list_start = id;
@@ -244,6 +245,8 @@ GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
 const struct GNUNET_PeerIdentity *
 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
 {
+  GNUNET_assert (id < size);
+  GNUNET_assert (table[id]->rc > 0);
   return &table[id]->id;
 }