fix for starting problems of SUID binaries
[oweals/gnunet.git] / src / util / peer.c
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
21 /**
22  * @file util/peer.c
23  * @brief peer-ID table that assigns integer IDs to peer-IDs to save memory
24  * @author Christian Grothoff
25  */
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 {
34   /**
35    * The identifier itself
36    */
37   struct GNUNET_PeerIdentity id;
38
39   /**
40    * Short version of the identifier; if the RC==0, then index of next
41    * free slot in table, otherwise equal to this slot in the table.
42    */
43   GNUNET_PEER_Id pid;
44
45   /**
46    * Reference counter, 0 if this slot is not used.
47    */
48   unsigned int rc;
49 };
50
51
52 /**
53  * Table with our interned peer IDs.
54  */
55 static struct PeerEntry **table;
56
57 /**
58  * Peermap of PeerIdentities to "struct PeerEntry"
59  * (for fast lookup).  NULL until the library
60  * is actually being used.
61  */
62 static struct GNUNET_CONTAINER_MultiPeerMap *map;
63
64 /**
65  * Size of the "table".
66  */
67 static unsigned int size;
68
69 /**
70  * Index of the beginning of the free list in the table; set to "size"
71  * if no slots are free in the table.
72  */
73 static unsigned int free_list_start;
74
75
76 /**
77  * Search for a peer identity. The reference counter is not changed.
78  *
79  * @param pid identity to find
80  * @return the interned identity or 0.
81  */
82 GNUNET_PEER_Id
83 GNUNET_PEER_search (const struct GNUNET_PeerIdentity *pid)
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
99 /**
100  * Intern an peer identity.  If the identity is already known, its
101  * reference counter will be increased by one.
102  *
103  * @param pid identity to intern
104  * @return the interned identity.
105  */
106 GNUNET_PEER_Id
107 GNUNET_PEER_intern (const struct GNUNET_PeerIdentity *pid)
108 {
109   GNUNET_PEER_Id ret;
110   struct PeerEntry *e;
111   unsigned int i;
112
113   if (NULL == pid)
114     return 0;
115   if (NULL == map)
116     map = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
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;
147   GNUNET_break (GNUNET_OK ==
148                 GNUNET_CONTAINER_multipeermap_put (map,
149                                                    &table[ret]->id,
150                                                    table[ret],
151                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
152   return ret;
153 }
154
155
156 /**
157  * Decrement multiple RCs of peer identities by one.
158  *
159  * @param ids array of PIDs to decrement the RCs of
160  * @param count size of the ids array
161  */
162 void
163 GNUNET_PEER_decrement_rcs (const GNUNET_PEER_Id *ids, unsigned int count)
164 {
165   int i;
166   GNUNET_PEER_Id id;
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     {
180       GNUNET_break (GNUNET_OK ==
181                     GNUNET_CONTAINER_multipeermap_remove (map,
182                                                           &table[id]->id,
183                                                           table[id]));
184       table[id]->pid = free_list_start;
185       free_list_start = id;
186     }
187   }
188 }
189
190
191 /**
192  * Change the reference counter of an interned PID.
193  *
194  * @param id identity to change the RC of
195  * @param delta how much to change the RC
196  */
197 void
198 GNUNET_PEER_change_rc (GNUNET_PEER_Id id, int delta)
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   {
209     GNUNET_break (GNUNET_OK ==
210                   GNUNET_CONTAINER_multipeermap_remove (map,
211                                                         &table[id]->id,
212                                                         table[id]));
213     table[id]->pid = free_list_start;
214     free_list_start = id;
215   }
216 }
217
218
219 /**
220  * Convert an interned PID to a normal peer identity.
221  *
222  * @param id interned PID to convert
223  * @param pid where to write the normal peer identity
224  */
225 void
226 GNUNET_PEER_resolve (GNUNET_PEER_Id id, struct GNUNET_PeerIdentity *pid)
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
239 /**
240  * Convert an interned PID to a normal peer identity.
241  *
242  * @param id interned PID to convert
243  * @return pointer to peer identity, valid as long 'id' is valid
244  */
245 const struct GNUNET_PeerIdentity *
246 GNUNET_PEER_resolve2 (GNUNET_PEER_Id id)
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 */