GNUnet  0.10.x
peer.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2006, 2008, 2009 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_peer_lib.h"
28 
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-peer", __VA_ARGS__)
30 
31 
32 struct PeerEntry
33 {
37  struct GNUNET_PeerIdentity id;
38 
44 
48  unsigned int rc;
49 };
50 
51 
55 static struct PeerEntry **table;
56 
63 
67 static unsigned int size;
68 
73 static unsigned int free_list_start;
74 
75 
84 {
85  struct PeerEntry *e;
86 
87  if (NULL == pid)
88  return 0;
89  if (NULL == map)
90  return 0;
91  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
92  if (NULL == e)
93  return 0;
94  GNUNET_assert (e->rc > 0);
95  return e->pid;
96 }
97 
98 
108 {
110  struct PeerEntry *e;
111  unsigned int i;
112 
113  if (NULL == pid)
114  return 0;
115  if (NULL == map)
117  e = GNUNET_CONTAINER_multipeermap_get (map, pid);
118  if (NULL != e)
119  {
120  GNUNET_assert (e->rc > 0);
121  e->rc++;
122  return e->pid;
123  }
124  ret = free_list_start;
125  if (ret == size)
126  {
127  GNUNET_array_grow (table, size, size + 16);
128  for (i = ret; i < size; i++)
129  {
130  table[i] = GNUNET_new (struct PeerEntry);
131  table[i]->pid = i + 1;
132  }
133  }
134  if (0 == ret)
135  {
136  memset (&table[0]->id, 0, sizeof (struct GNUNET_PeerIdentity));
137  table[0]->pid = 0;
138  table[0]->rc = 1;
139  ret = 1;
140  }
141  GNUNET_assert (ret < size);
142  GNUNET_assert (0 == table[ret]->rc);
143  free_list_start = table[ret]->pid;
144  table[ret]->id = *pid;
145  table[ret]->rc = 1;
146  table[ret]->pid = ret;
149  &table[ret]->id,
150  table[ret],
152  return ret;
153 }
154 
155 
162 void
163 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
164 {
165  int i;
167 
168  if (0 == count)
169  return;
170  for (i = count - 1; i >= 0; i--)
171  {
172  id = ids[i];
173  if (0 == id)
174  continue;
175  GNUNET_assert (id < size);
176  GNUNET_assert (table[id]->rc > 0);
177  table[id]->rc--;
178  if (0 == table[id]->rc)
179  {
182  &table[id]->id,
183  table[id]));
184  table[id]->pid = free_list_start;
186  }
187  }
188 }
189 
190 
197 void
199 {
200  if (0 == id)
201  return;
202  GNUNET_assert (id < size);
203  GNUNET_assert (table[id]->rc > 0);
204  GNUNET_assert ( (delta >= 0) ||
205  (table[id]->rc >= (unsigned int) (-delta)) );
206  table[id]->rc += delta;
207  if (0 == table[id]->rc)
208  {
211  &table[id]->id,
212  table[id]));
213  table[id]->pid = free_list_start;
215  }
216 }
217 
218 
225 void
227 {
228  if (0 == id)
229  {
230  memset (pid, 0, sizeof (struct GNUNET_PeerIdentity));
231  return;
232  }
233  GNUNET_assert (id < size);
234  GNUNET_assert (table[id]->rc > 0);
235  *pid = table[id]->id;
236 }
237 
238 
245 const struct GNUNET_PeerIdentity *
247 {
248  GNUNET_assert (id < size);
249  GNUNET_assert (table[id]->rc > 0);
250  return &table[id]->id;
251 }
252 
253 
254 
255 /* end of peer.c */
const struct GNUNET_PeerIdentity * GNUNET_PEER_resolve2(GNUNET_PEER_Id id)
Convert an interned PID to a normal peer identity.
Definition: peer.c:246
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
Per-peer information.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct Experiment * e
GNUNET_PEER_Id GNUNET_PEER_search(const struct GNUNET_PeerIdentity *pid)
Search for a peer identity.
Definition: peer.c:83
int GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
unsigned int rc
Reference counter, 0 if this slot is not used.
Definition: peer.c:48
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
static int ret
Final status code.
Definition: gnunet-arm.c:89
void GNUNET_PEER_resolve(GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
Convert an interned PID to a normal peer identity.
Definition: peer.c:226
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
unsigned int GNUNET_PEER_Id
A GNUNET_PEER_Id is simply a shorter version of a "struct GNUNET_PeerIdentifier" that can be used ins...
static struct GNUNET_CONTAINER_MultiPeerMap * ids
GNUNET_PeerIdentity -> CadetPeer.
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
Internal representation of the hash map.
void GNUNET_PEER_decrement_rcs(const GNUNET_PEER_Id *ids, unsigned int count)
Decrement multiple RCs of peer identities by one.
Definition: peer.c:163
static struct GNUNET_CONTAINER_MultiPeerMap * map
Peermap of PeerIdentities to "struct PeerEntry" (for fast lookup).
Definition: peer.c:62
There must only be one value per key; storing a value should fail if a value under the same key alrea...
static unsigned int size
Size of the "table".
Definition: peer.c:67
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
The identity of the host (wraps the signing key of the peer).
void * GNUNET_CONTAINER_multipeermap_get(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Given a key find a value in the map matching the key.
void GNUNET_PEER_change_rc(GNUNET_PEER_Id id, int delta)
Change the reference counter of an interned PID.
Definition: peer.c:198
#define GNUNET_YES
Definition: gnunet_common.h:80
GNUNET_PEER_Id pid
Short version of the identifier; if the RC==0, then index of next free slot in table, otherwise equal to this slot in the table.
Definition: peer.c:43
struct GNUNET_PeerIdentity id
What is the identity of the peer?
static unsigned int free_list_start
Index of the beginning of the free list in the table; set to "size" if no slots are free in the table...
Definition: peer.c:73
GNUNET_PEER_Id GNUNET_PEER_intern(const struct GNUNET_PeerIdentity *pid)
Intern an peer identity.
Definition: peer.c:107