aboutsummaryrefslogtreecommitdiff
path: root/src/rps/gnunet-service-rps_custommap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/gnunet-service-rps_custommap.c')
-rw-r--r--src/rps/gnunet-service-rps_custommap.c253
1 files changed, 126 insertions, 127 deletions
diff --git a/src/rps/gnunet-service-rps_custommap.c b/src/rps/gnunet-service-rps_custommap.c
index d3cc8d104..b842bc0a5 100644
--- a/src/rps/gnunet-service-rps_custommap.c
+++ b/src/rps/gnunet-service-rps_custommap.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 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/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file rps/gnunet-service-rps_custommap.c 22 * @file rps/gnunet-service-rps_custommap.c
@@ -28,7 +28,7 @@
28#include "gnunet-service-rps_custommap.h" 28#include "gnunet-service-rps_custommap.h"
29#include <inttypes.h> 29#include <inttypes.h>
30 30
31#define LOG(kind, ...) GNUNET_log_from(kind,"rps-peers",__VA_ARGS__) 31#define LOG(kind, ...) GNUNET_log_from(kind, "rps-peers", __VA_ARGS__)
32 32
33 33
34/** 34/**
@@ -54,8 +54,7 @@
54 * - contain continous indices 0 <= i < len 54 * - contain continous indices 0 <= i < len
55 * - not contain duplicate peers 55 * - not contain duplicate peers
56 */ 56 */
57struct CustomPeerMap 57struct CustomPeerMap {
58{
59 /** 58 /**
60 * Multihashmap to be able to access a random index 59 * Multihashmap to be able to access a random index
61 */ 60 */
@@ -76,14 +75,14 @@ struct CustomPeerMap
76 * @return the newly created custom peer map 75 * @return the newly created custom peer map
77 */ 76 */
78struct CustomPeerMap * 77struct CustomPeerMap *
79CustomPeerMap_create (unsigned int len) 78CustomPeerMap_create(unsigned int len)
80{ 79{
81 struct CustomPeerMap *c_peer_map; 80 struct CustomPeerMap *c_peer_map;
82 81
83 c_peer_map = GNUNET_new (struct CustomPeerMap); 82 c_peer_map = GNUNET_new(struct CustomPeerMap);
84 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create (len); 83 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create(len);
85 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create (len, 84 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create(len,
86 GNUNET_NO); 85 GNUNET_NO);
87 return c_peer_map; 86 return c_peer_map;
88} 87}
89 88
@@ -95,11 +94,11 @@ CustomPeerMap_create (unsigned int len)
95 * @return size of the map 94 * @return size of the map
96 */ 95 */
97unsigned int 96unsigned int
98CustomPeerMap_size (const struct CustomPeerMap *c_peer_map) 97CustomPeerMap_size(const struct CustomPeerMap *c_peer_map)
99{ 98{
100 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 99 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
101 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 100 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
102 return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map); 101 return GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map);
103} 102}
104 103
105/** 104/**
@@ -112,39 +111,39 @@ CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
112 * GNUNET_NO if map did contain peer previously 111 * GNUNET_NO if map did contain peer previously
113 */ 112 */
114int 113int
115CustomPeerMap_put (const struct CustomPeerMap *c_peer_map, 114CustomPeerMap_put(const struct CustomPeerMap *c_peer_map,
116 const struct GNUNET_PeerIdentity *peer) 115 const struct GNUNET_PeerIdentity *peer)
117{ 116{
118 uint32_t *index; 117 uint32_t *index;
119 struct GNUNET_PeerIdentity *p; 118 struct GNUNET_PeerIdentity *p;
120 119
121 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 120 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
122 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 121 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
123 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, 122 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains(c_peer_map->peer_map,
124 peer)) 123 peer))
125 { 124 {
126 /* Need to store the index of the peer in the peermap to be able to remove 125 /* Need to store the index of the peer in the peermap to be able to remove
127 * it properly */ 126 * it properly */
128 index = GNUNET_new (uint32_t); 127 index = GNUNET_new(uint32_t);
129 *index = CustomPeerMap_size (c_peer_map); 128 *index = CustomPeerMap_size(c_peer_map);
130 p = GNUNET_new (struct GNUNET_PeerIdentity); 129 p = GNUNET_new(struct GNUNET_PeerIdentity);
131 *p = *peer; 130 *p = *peer;
132 GNUNET_assert (p != peer); 131 GNUNET_assert(p != peer);
133 GNUNET_assert (0 == memcmp (p, 132 GNUNET_assert(0 == memcmp(p,
134 peer, 133 peer,
135 sizeof(struct GNUNET_PeerIdentity))); 134 sizeof(struct GNUNET_PeerIdentity)));
136 GNUNET_CONTAINER_multipeermap_put (c_peer_map->peer_map, 135 GNUNET_CONTAINER_multipeermap_put(c_peer_map->peer_map,
137 p, 136 p,
138 index, 137 index,
139 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
140 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map, 139 GNUNET_CONTAINER_multihashmap32_put(c_peer_map->hash_map,
141 *index, 140 *index,
142 p, 141 p,
143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); 142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
144 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 143 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
145 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 144 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
146 return GNUNET_OK; 145 return GNUNET_OK;
147 } 146 }
148 return GNUNET_NO; 147 return GNUNET_NO;
149} 148}
150 149
@@ -158,10 +157,10 @@ CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
158 * GNUNET_NO otherwise 157 * GNUNET_NO otherwise
159 */ 158 */
160int 159int
161CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map, 160CustomPeerMap_contains_peer(const struct CustomPeerMap *c_peer_map,
162 const struct GNUNET_PeerIdentity *peer) 161 const struct GNUNET_PeerIdentity *peer)
163{ 162{
164 return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer); 163 return GNUNET_CONTAINER_multipeermap_contains(c_peer_map->peer_map, peer);
165} 164}
166 165
167/** 166/**
@@ -173,13 +172,13 @@ CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
173 * @return the index 172 * @return the index
174 */ 173 */
175static uint32_t * 174static uint32_t *
176CustomPeerMap_get_index_pointer (const struct CustomPeerMap *c_peer_map, 175CustomPeerMap_get_index_pointer(const struct CustomPeerMap *c_peer_map,
177 const struct GNUNET_PeerIdentity *peer) 176 const struct GNUNET_PeerIdentity *peer)
178{ 177{
179 uint32_t *index; 178 uint32_t *index;
180 179
181 GNUNET_assert (GNUNET_YES == CustomPeerMap_contains_peer (c_peer_map, peer)); 180 GNUNET_assert(GNUNET_YES == CustomPeerMap_contains_peer(c_peer_map, peer));
182 index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer); 181 index = GNUNET_CONTAINER_multipeermap_get(c_peer_map->peer_map, peer);
183 return index; 182 return index;
184} 183}
185 184
@@ -193,52 +192,52 @@ CustomPeerMap_get_index_pointer (const struct CustomPeerMap *c_peer_map,
193 * GNUNET_NO if map does not contain peer 192 * GNUNET_NO if map does not contain peer
194 */ 193 */
195int 194int
196CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map, 195CustomPeerMap_remove_peer(const struct CustomPeerMap *c_peer_map,
197 const struct GNUNET_PeerIdentity *peer) 196 const struct GNUNET_PeerIdentity *peer)
198{ 197{
199 uint32_t *index; 198 uint32_t *index;
200 struct GNUNET_PeerIdentity *p; 199 struct GNUNET_PeerIdentity *p;
201 uint32_t *last_index; 200 uint32_t *last_index;
202 struct GNUNET_PeerIdentity *last_p; 201 struct GNUNET_PeerIdentity *last_p;
203 202
204 if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map, 203 if (GNUNET_NO == CustomPeerMap_contains_peer(c_peer_map,
205 peer)) 204 peer))
206 { 205 {
207 return GNUNET_NO; 206 return GNUNET_NO;
208 } 207 }
209 index = CustomPeerMap_get_index_pointer (c_peer_map, 208 index = CustomPeerMap_get_index_pointer(c_peer_map,
210 peer); 209 peer);
211 GNUNET_assert (*index < CustomPeerMap_size (c_peer_map)); 210 GNUNET_assert(*index < CustomPeerMap_size(c_peer_map));
212 /* Need to get the pointer stored in the hashmap to free it */ 211 /* Need to get the pointer stored in the hashmap to free it */
213 p = GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, 212 p = GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map,
214 *index); 213 *index);
215 GNUNET_assert (NULL != p); 214 GNUNET_assert(NULL != p);
216 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map, 215 GNUNET_CONTAINER_multihashmap32_remove_all(c_peer_map->hash_map,
217 *index); 216 *index);
218 // TODO wrong peerid? 217 // TODO wrong peerid?
219 GNUNET_CONTAINER_multipeermap_remove_all (c_peer_map->peer_map, 218 GNUNET_CONTAINER_multipeermap_remove_all(c_peer_map->peer_map,
220 peer); 219 peer);
221 if (*index != CustomPeerMap_size (c_peer_map)) 220 if (*index != CustomPeerMap_size(c_peer_map))
222 { /* fill 'gap' with peer at last index */ 221 { /* fill 'gap' with peer at last index */
223 last_p = 222 last_p =
224 GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, 223 GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map,
225 CustomPeerMap_size (c_peer_map)); 224 CustomPeerMap_size(c_peer_map));
226 GNUNET_assert (NULL != last_p); 225 GNUNET_assert(NULL != last_p);
227 last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, 226 last_index = GNUNET_CONTAINER_multipeermap_get(c_peer_map->peer_map,
228 last_p); 227 last_p);
229 GNUNET_assert (NULL != last_index); 228 GNUNET_assert(NULL != last_index);
230 GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index); 229 GNUNET_assert(CustomPeerMap_size(c_peer_map) == *last_index);
231 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map, 230 GNUNET_CONTAINER_multihashmap32_put(c_peer_map->hash_map,
232 *index, last_p, 231 *index, last_p,
233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); 232 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
234 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map, 233 GNUNET_CONTAINER_multihashmap32_remove_all(c_peer_map->hash_map,
235 *last_index); 234 *last_index);
236 *last_index = *index; 235 *last_index = *index;
237 } 236 }
238 GNUNET_free (index); 237 GNUNET_free(index);
239 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 238 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
240 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 239 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
241 GNUNET_free (p); 240 GNUNET_free(p);
242 return GNUNET_OK; 241 return GNUNET_OK;
243} 242}
244 243
@@ -252,14 +251,14 @@ CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
252 * if this index is not known, return NULL 251 * if this index is not known, return NULL
253 */ 252 */
254struct GNUNET_PeerIdentity * 253struct GNUNET_PeerIdentity *
255CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map, 254CustomPeerMap_get_peer_by_index(const struct CustomPeerMap *c_peer_map,
256 uint32_t index) 255 uint32_t index)
257{ 256{
258 if (GNUNET_YES == 257 if (GNUNET_YES ==
259 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index)) 258 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map, index))
260 { 259 {
261 return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index); 260 return GNUNET_CONTAINER_multihashmap32_get(c_peer_map->hash_map, index);
262 } 261 }
263 return NULL; 262 return NULL;
264} 263}
265 264
@@ -273,30 +272,30 @@ CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map,
273 * GNUNET_NO if map does not contain (index of) peer 272 * GNUNET_NO if map does not contain (index of) peer
274 */ 273 */
275int 274int
276CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map, 275CustomPeerMap_remove_peer_by_index(const struct CustomPeerMap *c_peer_map,
277 uint32_t index) 276 uint32_t index)
278{ 277{
279 uint32_t *index_p; 278 uint32_t *index_p;
280 struct GNUNET_PeerIdentity *peer; 279 struct GNUNET_PeerIdentity *peer;
281 280
282 if (index >= CustomPeerMap_size (c_peer_map)) 281 if (index >= CustomPeerMap_size(c_peer_map))
283 { 282 {
284 return GNUNET_NO; 283 return GNUNET_NO;
285 } 284 }
286 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 285 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
287 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 286 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
288 if (GNUNET_NO == 287 if (GNUNET_NO ==
289 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index)) 288 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map, index))
290 { 289 {
291 return GNUNET_NO; 290 return GNUNET_NO;
292 } 291 }
293 peer = CustomPeerMap_get_peer_by_index (c_peer_map, index); 292 peer = CustomPeerMap_get_peer_by_index(c_peer_map, index);
294 GNUNET_assert (NULL != peer); 293 GNUNET_assert(NULL != peer);
295 index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer); 294 index_p = CustomPeerMap_get_index_pointer(c_peer_map, peer);
296 GNUNET_assert (index == *index_p); 295 GNUNET_assert(index == *index_p);
297 CustomPeerMap_remove_peer (c_peer_map, peer); 296 CustomPeerMap_remove_peer(c_peer_map, peer);
298 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) == 297 GNUNET_assert(GNUNET_CONTAINER_multihashmap32_size(c_peer_map->hash_map) ==
299 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map)); 298 GNUNET_CONTAINER_multipeermap_size(c_peer_map->peer_map));
300 return GNUNET_OK; 299 return GNUNET_OK;
301} 300}
302 301
@@ -308,18 +307,18 @@ CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map,
308 * @return size of the map 307 * @return size of the map
309 */ 308 */
310void 309void
311CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map) 310CustomPeerMap_clear(const struct CustomPeerMap *c_peer_map)
312{ 311{
313 while (0 < CustomPeerMap_size (c_peer_map)) 312 while (0 < CustomPeerMap_size(c_peer_map))
314 { 313 {
315 GNUNET_assert (GNUNET_YES == 314 GNUNET_assert(GNUNET_YES ==
316 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, 315 GNUNET_CONTAINER_multihashmap32_contains(c_peer_map->hash_map,
317 CustomPeerMap_size (c_peer_map) -1)); 316 CustomPeerMap_size(c_peer_map) - 1));
318 GNUNET_assert (GNUNET_OK == 317 GNUNET_assert(GNUNET_OK ==
319 CustomPeerMap_remove_peer_by_index (c_peer_map, 318 CustomPeerMap_remove_peer_by_index(c_peer_map,
320 CustomPeerMap_size (c_peer_map) -1)); 319 CustomPeerMap_size(c_peer_map) - 1));
321 } 320 }
322 GNUNET_assert (0 == CustomPeerMap_size (c_peer_map)); 321 GNUNET_assert(0 == CustomPeerMap_size(c_peer_map));
323} 322}
324 323
325/** 324/**
@@ -328,12 +327,12 @@ CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
328 * @param c_peer_map the map to destroy 327 * @param c_peer_map the map to destroy
329 */ 328 */
330void 329void
331CustomPeerMap_destroy (struct CustomPeerMap *c_peer_map) 330CustomPeerMap_destroy(struct CustomPeerMap *c_peer_map)
332{ 331{
333 CustomPeerMap_clear (c_peer_map); 332 CustomPeerMap_clear(c_peer_map);
334 GNUNET_CONTAINER_multihashmap32_destroy (c_peer_map->hash_map); 333 GNUNET_CONTAINER_multihashmap32_destroy(c_peer_map->hash_map);
335 GNUNET_CONTAINER_multipeermap_destroy (c_peer_map->peer_map); 334 GNUNET_CONTAINER_multipeermap_destroy(c_peer_map->peer_map);
336 GNUNET_free (c_peer_map); 335 GNUNET_free(c_peer_map);
337} 336}
338 337
339/* end of gnunet-service-rps_custommap.c */ 338/* end of gnunet-service-rps_custommap.c */