2 This file is part of GNUnet.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file rps/gnunet-service-rps_custommap.c
23 * @brief utilities for managing (information about) peers
24 * @author Julius Bünger
27 #include "gnunet_util_lib.h"
28 #include "gnunet-service-rps_custommap.h"
31 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-peers",__VA_ARGS__)
35 * Peer map to store peers with specialised use-cases (push_list, pull_list,
38 * It is aimed for use as unordered list-like structures that can be indexed.
41 * permut = GNUNET_CRYPTO_random_permute (GNUNET_CRYPTO_QUALITY_STRONG,
42 * CustomPeerMap_size (peer_map));
43 * for (i = 0; i < some_border; i++)
44 * some_array[i] = *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
45 * for (i = some_border; i < CustomPeerMap_size (peer_map); i++)
46 * other_array[i-some_border] =
47 * *CustomPeerMap_get_peer_by_index (peer_map, permut[i]);
49 * This list is expected to
50 * - be altered in small steps frequently
51 * - be cleared regularily
52 * - often being queried whether a peer is contained
53 * - alter indices of peers
54 * - contain continous indices 0 <= i < len
55 * - not contain duplicate peers
60 * Multihashmap to be able to access a random index
62 struct GNUNET_CONTAINER_MultiHashMap32 *hash_map;
65 * Peermap to quickly check whether a peer is contained
67 struct GNUNET_CONTAINER_MultiPeerMap *peer_map;
72 * Create an empty peermap.
74 * @param len the initial length for the internal maps
76 * @return the newly created custom peer map
78 struct CustomPeerMap *
79 CustomPeerMap_create (unsigned int len)
81 struct CustomPeerMap *c_peer_map;
83 c_peer_map = GNUNET_new (struct CustomPeerMap);
84 c_peer_map->hash_map = GNUNET_CONTAINER_multihashmap32_create (len);
85 c_peer_map->peer_map = GNUNET_CONTAINER_multipeermap_create (len, GNUNET_NO);
90 * Get the size of the custom peer map
92 * @param c_peer_map the custom peer map to look in
94 * @return size of the map
97 CustomPeerMap_size (const struct CustomPeerMap *c_peer_map)
99 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
100 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
101 return GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map);
105 * Insert peer into the custom peer map
107 * @param c_peer_map the custom peer map to insert peer
108 * @param peer the peer to insert
110 * @return GNUNET_OK if map did not contain peer previously
111 * GNUNET_NO if map did contain peer previously
114 CustomPeerMap_put (const struct CustomPeerMap *c_peer_map,
115 const struct GNUNET_PeerIdentity *peer)
118 struct GNUNET_PeerIdentity *p;
120 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
121 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
122 if (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map,
125 /* Need to store the index of the peer in the peermap to be able to remove
127 index = GNUNET_new (uint32_t);
128 *index = CustomPeerMap_size (c_peer_map);
129 p = GNUNET_new (struct GNUNET_PeerIdentity);
131 GNUNET_assert (p != peer);
132 GNUNET_assert (0 == memcmp (p, peer, sizeof(struct GNUNET_PeerIdentity)));
133 GNUNET_CONTAINER_multipeermap_put (c_peer_map->peer_map, p, index,
134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
135 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map, *index, p,
136 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
137 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
138 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
145 * Check whether custom peer map contains a peer
147 * @param c_peer_map the custom peer map to look in
148 * @param peer the peer to check for
150 * @return GNUNET_OK if map contains peer
151 * GNUNET_NO otherwise
154 CustomPeerMap_contains_peer (const struct CustomPeerMap *c_peer_map,
155 const struct GNUNET_PeerIdentity *peer)
157 return GNUNET_CONTAINER_multipeermap_contains (c_peer_map->peer_map, peer);
161 * Get index of peer in custom peer map
163 * @param c_peer_map the custom peer map to look in
164 * @param peer the peer to get the index from
169 CustomPeerMap_get_index_pointer (const struct CustomPeerMap *c_peer_map,
170 const struct GNUNET_PeerIdentity *peer)
174 GNUNET_assert (GNUNET_YES == CustomPeerMap_contains_peer (c_peer_map, peer));
175 index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, peer);
180 * Remove peer from custom peer map
182 * @param c_peer_map the custom peer map to remove the peer from
183 * @param peer the peer to remove
185 * @return GNUNET_OK if map contained peer and removed it successfully
186 * GNUNET_NO if map does not contain peer
189 CustomPeerMap_remove_peer (const struct CustomPeerMap *c_peer_map,
190 const struct GNUNET_PeerIdentity *peer)
193 struct GNUNET_PeerIdentity *p;
194 uint32_t *last_index;
195 struct GNUNET_PeerIdentity *last_p;
197 if (GNUNET_NO == CustomPeerMap_contains_peer (c_peer_map, peer))
201 index = CustomPeerMap_get_index_pointer (c_peer_map, peer);
202 GNUNET_assert (*index < CustomPeerMap_size (c_peer_map));
203 /* Need to get the pointer stored in the hashmap to free it */
204 p = GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, *index);
205 GNUNET_assert (NULL != p);
206 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map, *index);
207 // TODO wrong peerid?
208 GNUNET_CONTAINER_multipeermap_remove_all (c_peer_map->peer_map, peer);
209 if (*index != CustomPeerMap_size (c_peer_map))
210 { /* fill 'gap' with peer at last index */
212 GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map,
213 CustomPeerMap_size (c_peer_map));
214 GNUNET_assert (NULL != last_p);
215 last_index = GNUNET_CONTAINER_multipeermap_get (c_peer_map->peer_map, last_p);
216 GNUNET_assert (NULL != last_index);
217 GNUNET_assert (CustomPeerMap_size (c_peer_map) == *last_index);
218 GNUNET_CONTAINER_multihashmap32_put (c_peer_map->hash_map, *index, last_p,
219 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
220 GNUNET_CONTAINER_multihashmap32_remove_all (c_peer_map->hash_map, *last_index);
221 *last_index = *index;
224 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
225 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
231 * Get a peer by index
233 * @param c_peer_map the custom peer map to look in
234 * @param index the index of the peer to get
236 * @return peer to the corresponding index.
237 * if this index is not known, return NULL
239 struct GNUNET_PeerIdentity *
240 CustomPeerMap_get_peer_by_index (const struct CustomPeerMap *c_peer_map,
244 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
246 return GNUNET_CONTAINER_multihashmap32_get (c_peer_map->hash_map, index);
252 * Remove peer from custom peer map by index
254 * @param c_peer_map the custom peer map to remove the peer from
255 * @param index the index of the peer to remove
257 * @return GNUNET_OK if map contained peer and removed it successfully
258 * GNUNET_NO if map does not contain (index of) peer
261 CustomPeerMap_remove_peer_by_index (const struct CustomPeerMap *c_peer_map,
265 struct GNUNET_PeerIdentity *peer;
267 if (index >= CustomPeerMap_size (c_peer_map))
271 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
272 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
274 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map, index))
278 peer = CustomPeerMap_get_peer_by_index (c_peer_map, index);
279 GNUNET_assert (NULL != peer);
280 index_p = CustomPeerMap_get_index_pointer (c_peer_map, peer);
281 GNUNET_assert (index == *index_p);
282 CustomPeerMap_remove_peer (c_peer_map, peer);
283 GNUNET_assert (GNUNET_CONTAINER_multihashmap32_size (c_peer_map->hash_map) ==
284 GNUNET_CONTAINER_multipeermap_size (c_peer_map->peer_map));
289 * Clear the custom peer map
291 * @param c_peer_map the custom peer map to look in
293 * @return size of the map
296 CustomPeerMap_clear (const struct CustomPeerMap *c_peer_map)
298 while (0 < CustomPeerMap_size (c_peer_map))
300 GNUNET_assert (GNUNET_YES ==
301 GNUNET_CONTAINER_multihashmap32_contains (c_peer_map->hash_map,
302 CustomPeerMap_size (c_peer_map) -1));
303 GNUNET_assert (GNUNET_OK ==
304 CustomPeerMap_remove_peer_by_index (c_peer_map,
305 CustomPeerMap_size (c_peer_map) -1));
307 GNUNET_assert (0 == CustomPeerMap_size (c_peer_map));
313 * @param c_peer_map the map to destroy
316 CustomPeerMap_destroy (struct CustomPeerMap *c_peer_map)
318 CustomPeerMap_clear (c_peer_map);
319 GNUNET_CONTAINER_multihashmap32_destroy (c_peer_map->hash_map);
320 GNUNET_CONTAINER_multipeermap_destroy (c_peer_map->peer_map);
321 GNUNET_free (c_peer_map);
324 /* end of gnunet-service-rps_custommap.c */