GNUnet  0.10.x
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 
78 struct CustomPeerMap *
79 CustomPeerMap_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 
97 unsigned int
98 CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
99 {
102  return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map);
103 }
104 
114 int
115 CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
116  const struct GNUNET_PeerIdentity *peer)
117 {
118  uint32_t *index;
119  struct GNUNET_PeerIdentity *p;
120 
124  peer))
125  {
126  /* Need to store the index of the peer in the peermap to be able to remove
127  * it properly */
128  index = GNUNET_new (uint32_t);
129  *index = CustomPeerMap_size (c_peer_map);
130  p = GNUNET_new (struct GNUNET_PeerIdentity);
131  *p = *peer;
132  GNUNET_assert (p != peer);
133  GNUNET_assert (0 == memcmp (p,
134  peer,
135  sizeof(struct GNUNET_PeerIdentity)));
137  p,
138  index,
141  *index,
142  p,
146  return GNUNET_OK;
147  }
148  return GNUNET_NO;
149 }
150 
160 int
161 CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
162  const struct GNUNET_PeerIdentity *peer)
163 {
164  return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer);
165 }
166 
175 static uint32_t *
177  const struct GNUNET_PeerIdentity *peer)
178 {
179  uint32_t *index;
180 
181  GNUNET_assert (GNUNET_YES == CustomPeerMap_contains_peer (c_peer_map, peer));
182  index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer);
183  return index;
184 }
185 
195 int
196 CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
197  const struct GNUNET_PeerIdentity *peer)
198 {
199  uint32_t *index;
200  struct GNUNET_PeerIdentity *p;
201  uint32_t *last_index;
202  struct GNUNET_PeerIdentity *last_p;
203 
204  if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map,
205  peer))
206  {
207  return GNUNET_NO;
208  }
209  index = CustomPeerMap_get_index_pointer (c_peer_map,
210  peer);
211  GNUNET_assert (*index < CustomPeerMap_size (c_peer_map));
212  /* Need to get the pointer stored in the hashmap to free it */
214  *index);
215  GNUNET_assert (NULL != p);
217  *index);
218  // TODO wrong peerid?
220  peer);
221  if (*index != CustomPeerMap_size (c_peer_map))
222  { /* fill 'gap' with peer at last index */
223  last_p =
225  CustomPeerMap_size (c_peer_map));
226  GNUNET_assert (NULL != last_p);
227  last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map,
228  last_p);
229  GNUNET_assert (NULL != last_index);
230  GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index);
232  *index, last_p,
235  *last_index);
236  *last_index = *index;
237  }
238  GNUNET_free (index);
241  GNUNET_free (p);
242  return GNUNET_OK;
243 }
244 
254 struct GNUNET_PeerIdentity *
256  uint32_t index)
257 {
258  if (GNUNET_YES ==
260  {
261  return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index);
262  }
263  return NULL;
264 }
265 
275 int
277  uint32_t index)
278 {
279  uint32_t *index_p;
280  struct GNUNET_PeerIdentity *peer;
281 
282  if (index >= CustomPeerMap_size (c_peer_map))
283  {
284  return GNUNET_NO;
285  }
288  if (GNUNET_NO ==
290  {
291  return GNUNET_NO;
292  }
293  peer = CustomPeerMap_get_peer_by_index (c_peer_map, index);
294  GNUNET_assert (NULL != peer);
295  index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer);
296  GNUNET_assert (index == *index_p);
297  CustomPeerMap_remove_peer (c_peer_map, peer);
300  return GNUNET_OK;
301 }
302 
310 void
311 CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
312 {
313  while (0 < CustomPeerMap_size (c_peer_map))
314  {
317  CustomPeerMap_size (c_peer_map) -1));
320  CustomPeerMap_size (c_peer_map) -1));
321  }
322  GNUNET_assert (0 == CustomPeerMap_size (c_peer_map));
323 }
324 
330 void
332 {
333  CustomPeerMap_clear (c_peer_map);
336  GNUNET_free (c_peer_map);
337 }
338 
339 /* end of gnunet-service-rps_custommap.c */
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.
struct GNUNET_PeerIdentity * CustomPeerMap_get_peer_by_index(const struct CustomPeerMap *c_peer_map, uint32_t index)
Get a peer by index.
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.
utilities for managing (information about) peers
Peer map to store peers with specialised use-cases (push_list, pull_list, view, ...)
int CustomPeerMap_contains_peer(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Check whether custom peer map contains a peer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_CONTAINER_MultiPeerMap * peer_map
Peermap to quickly check whether a peer is contained.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
int CustomPeerMap_remove_peer_by_index(const struct CustomPeerMap *c_peer_map, uint32_t index)
Remove peer from custom peer map by index.
void CustomPeerMap_destroy(struct CustomPeerMap *c_peer_map)
Destroy peermap.
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).
int 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.
int GNUNET_CONTAINER_multihashmap32_remove_all(struct GNUNET_CONTAINER_MultiHashMap32 *map, uint32_t key)
Remove all entries for the given key from the map.
int CustomPeerMap_remove_peer(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Remove peer from custom peer map.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
struct CustomPeerMap * CustomPeerMap_create(unsigned int len)
Create an empty peermap.
int CustomPeerMap_put(const struct CustomPeerMap *c_peer_map, const struct GNUNET_PeerIdentity *peer)
Insert peer into the custom peer map.
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int 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)...
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.
void CustomPeerMap_clear(const struct CustomPeerMap *c_peer_map)
Clear the custom peer map.
unsigned int GNUNET_CONTAINER_multihashmap32_size(const struct GNUNET_CONTAINER_MultiHashMap32 *map)
Get the number of key-value pairs in the map.
Internal representation of the hash map.
Internal representation of the hash map.
struct GNUNET_CONTAINER_MultiHashMap32 * GNUNET_CONTAINER_multihashmap32_create(unsigned int len)
Create a 32-bit key multi hash map.
void GNUNET_CONTAINER_multihashmap32_destroy(struct GNUNET_CONTAINER_MultiHashMap32 *map)
Destroy a 32-bit key hash map.
There must only be one value per key; storing a value should fail if a value under the same key alrea...
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
unsigned int CustomPeerMap_size(const struct CustomPeerMap *c_peer_map)
Get the size of the custom peer 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.
The identity of the host (wraps the signing key of the peer).
struct GNUNET_CONTAINER_MultiHashMap32 * hash_map
Multihashmap to be able to access a random index.
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.
#define GNUNET_YES
Definition: gnunet_common.h:80
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_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)...
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...