GNUnet 0.21.2
gnunet-service-rps_custommap.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C)
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_util_lib.h"
29#include <inttypes.h>
30
31#define LOG(kind, ...) GNUNET_log_from (kind, "rps-peers", __VA_ARGS__)
32
33
58{
63
68};
69
70
78struct CustomPeerMap *
79CustomPeerMap_create (unsigned int len)
80{
81 struct CustomPeerMap *c_peer_map;
82
83 c_peer_map = GNUNET_new (struct CustomPeerMap);
86 GNUNET_NO);
87 return c_peer_map;
88}
89
90
98unsigned int
99CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
100{
103 return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map);
104}
105
106
116int
117CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
118 const struct GNUNET_PeerIdentity *peer)
119{
120 uint32_t *index;
121 struct GNUNET_PeerIdentity *p;
122
126 peer))
127 {
128 /* Need to store the index of the peer in the peermap to be able to remove
129 * it properly */
130 index = GNUNET_new (uint32_t);
131 *index = CustomPeerMap_size (c_peer_map);
133 *p = *peer;
134 GNUNET_assert (p != peer);
135 GNUNET_assert (0 == memcmp (p,
136 peer,
137 sizeof(struct GNUNET_PeerIdentity)));
139 p,
140 index,
143 *index,
144 p,
147 c_peer_map->hash_map) ==
149 return GNUNET_OK;
150 }
151 return GNUNET_NO;
152}
153
154
164int
166 const struct GNUNET_PeerIdentity *peer)
167{
168 return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer);
169}
170
171
180static uint32_t *
182 const struct GNUNET_PeerIdentity *peer)
183{
184 uint32_t *index;
185
187 index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer);
188 return index;
189}
190
191
201int
202CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
203 const struct GNUNET_PeerIdentity *peer)
204{
205 uint32_t *index;
206 struct GNUNET_PeerIdentity *p;
207 uint32_t *last_index;
208 struct GNUNET_PeerIdentity *last_p;
209 int ret;
210
211 if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map,
212 peer))
213 {
214 return GNUNET_NO;
215 }
216 index = CustomPeerMap_get_index_pointer (c_peer_map,
217 peer);
218 GNUNET_assert (*index < CustomPeerMap_size (c_peer_map));
219 /* Need to get the pointer stored in the hashmap to free it */
221 *index);
222 GNUNET_assert (NULL != p);
224 *index);
225 // TODO wrong peerid?
227 peer);
228 if (*index != CustomPeerMap_size (c_peer_map))
229 { /* fill 'gap' with peer at last index */
230 last_p =
232 CustomPeerMap_size (c_peer_map));
233 GNUNET_assert (NULL != last_p);
234 last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map,
235 last_p);
236 GNUNET_assert (NULL != last_index);
237 GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index);
239 *index,
240 last_p,
244 *last_index);
245 *last_index = *index;
246 }
247 GNUNET_free (index);
250 GNUNET_free (p);
251 return GNUNET_OK;
252}
253
254
264struct GNUNET_PeerIdentity *
266 uint32_t index)
267{
268 if (GNUNET_YES ==
270 {
271 return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index);
272 }
273 return NULL;
274}
275
276
286int
288 uint32_t index)
289{
290 uint32_t *index_p;
291 struct GNUNET_PeerIdentity *peer;
292
293 if (index >= CustomPeerMap_size (c_peer_map))
294 {
295 return GNUNET_NO;
296 }
299 if (GNUNET_NO ==
301 {
302 return GNUNET_NO;
303 }
304 peer = CustomPeerMap_get_peer_by_index (c_peer_map, index);
305 GNUNET_assert (NULL != peer);
306 index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer);
307 GNUNET_assert (index == *index_p);
308 CustomPeerMap_remove_peer (c_peer_map, peer);
311 return GNUNET_OK;
312}
313
314
322void
323CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
324{
325 while (0 < CustomPeerMap_size (c_peer_map))
326 {
329 c_peer_map->hash_map,
331 c_peer_map) - 1));
335 c_peer_map) - 1));
336 }
337 GNUNET_assert (0 == CustomPeerMap_size (c_peer_map));
338}
339
340
346void
348{
349 CustomPeerMap_clear (c_peer_map);
352 GNUNET_free (c_peer_map);
353}
354
355
356/* end of gnunet-service-rps_custommap.c */
static int ret
Final status code.
Definition: gnunet-arm.c:94
unsigned int CustomPeerMap_size(const struct CustomPeerMap *c_peer_map)
Get the size of the custom peer map.
int CustomPeerMap_remove_peer_by_index(const struct CustomPeerMap *c_peer_map, uint32_t index)
Remove peer from custom peer map by index.
int CustomPeerMap_contains_peer(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Check whether custom peer map contains a peer.
int CustomPeerMap_put(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Insert peer into the custom peer map.
static uint32_t * CustomPeerMap_get_index_pointer(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Get index of peer in custom peer map.
int CustomPeerMap_remove_peer(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Remove peer from custom peer map.
void CustomPeerMap_destroy(struct CustomPeerMap *c_peer_map)
Destroy peermap.
void CustomPeerMap_clear(const struct CustomPeerMap *c_peer_map)
Clear the custom peer map.
struct CustomPeerMap * CustomPeerMap_create(unsigned int len)
Create an empty peermap.
struct GNUNET_PeerIdentity * CustomPeerMap_get_peer_by_index(const struct CustomPeerMap *c_peer_map, uint32_t index)
Get a peer by index.
utilities for managing (information about) peers
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:38
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.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_put(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_contains(const struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Check if the map contains any value under the given key (including values that are NULL).
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap32_contains(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Check if the map contains any value under the given key (including values that are NULL).
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
void * GNUNET_CONTAINER_multihashmap32_get(const struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Given a key find a value in the map matching the key.
int GNUNET_CONTAINER_multipeermap_remove_all(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key)
Remove all entries for the given key from the map.
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).
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
int GNUNET_CONTAINER_multihashmap32_remove_all(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Remove all entries for the given key from the map.
unsigned int GNUNET_CONTAINER_multipeermap_size(const struct GNUNET_CONTAINER_MultiPeerMap *map)
Get the number of key-value pairs in the map.
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.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY
There must only be one value per key; storing a value should fail if a value under the same key alrea...
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
Peer map to store peers with specialised use-cases (push_list, pull_list, view, .....
struct GNUNET_CONTAINER_MultiPeerMap * peer_map
Peermap to quickly check whether a peer is contained.
struct GNUNET_CONTAINER_MultiHashMap32 * hash_map
Multihashmap to be able to access a random index.
Internal representation of the hash map.
Internal representation of the hash map.
The identity of the host (wraps the signing key of the peer).