GNUnet debian-0.24.3-23-g589b01d60
messenger_api_epoch_membership.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--2025 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 */
27#include "gnunet_common.h"
28#include <string.h>
29
32{
33 struct GNUNET_MESSENGER_EpochMembership *membership;
34
35 membership = GNUNET_new (struct GNUNET_MESSENGER_EpochMembership);
36
37 if (! membership)
38 return NULL;
39
40 membership->size = size;
41 membership->count = 0;
43 GNUNET_NO);
44
45 memset (&(membership->announcement), 0, sizeof (membership->announcement));
46
47 return membership;
48}
49
50
51void
53{
54 GNUNET_assert (membership);
55
57
58 GNUNET_free (membership);
59}
60
61
62uint32_t
64 membership)
65{
66 GNUNET_assert (membership);
67
68 return membership->size;
69}
70
71
72uint32_t
74 membership)
75{
76 GNUNET_assert (membership);
77
78 return membership->count;
79}
80
81
84 membership)
85{
86 uint32_t size;
87
88 GNUNET_assert (membership);
89
90 size = get_epoch_membership_size (membership);
91
92 if (! size)
93 return GNUNET_NO;
94
95 if (get_epoch_membership_count (membership) >= size)
96 return GNUNET_YES;
97 else
98 return GNUNET_NO;
99}
100
101
104 const struct GNUNET_HashCode *hash,
105 void *value)
106{
107 const struct GNUNET_MESSENGER_Contact **search;
108 const struct GNUNET_MESSENGER_Contact *contact;
109
110 GNUNET_assert ((cls) && (value));
111
112 search = cls;
113 contact = value;
114
115 if (contact != *search)
116 return GNUNET_YES;
117
118 *search = NULL;
119 return GNUNET_NO;
120}
121
122
125 membership,
126 const struct GNUNET_MESSENGER_Contact *contact)
127{
128 const struct GNUNET_MESSENGER_Contact *search;
129
130 GNUNET_assert (membership);
131
132 if (! contact)
133 return GNUNET_CONTAINER_multihashmap_contains (membership->members, &(
134 membership->announcement));
135
136 search = contact;
137
140 &search);
141
142 return search == contact? GNUNET_NO : GNUNET_YES;
143}
144
145
149 struct GNUNET_HashCode *hash,
150 enum GNUNET_GenericReturnValue other)
151{
154
155 GNUNET_assert ((membership) && (hash));
156
157 if (GNUNET_NO == other)
158 {
159 if (GNUNET_YES != is_epoch_membership_member (membership, NULL))
160 return GNUNET_SYSERR;
161
162 GNUNET_memcpy (hash, &(membership->announcement), sizeof (membership->
163 announcement));
164 return GNUNET_OK;
165 }
166
168 membership->members);
169
170 if (! iterator)
171 return GNUNET_SYSERR;
172
175 hash, NULL))
176 {
177 if (0 != GNUNET_CRYPTO_hash_cmp (hash, &(membership->announcement)))
178 {
180 break;
181 }
182 }
183
185 return result;
186}
187
188
189uint32_t
192 membership)
193{
195 struct GNUNET_HashCode hash;
196 uint32_t position;
197
198 GNUNET_assert (membership);
199
200 position = get_epoch_membership_count (membership);
201
202 if (GNUNET_YES != is_epoch_membership_member (membership, NULL))
203 return position;
204
206
207 if (! iter)
208 return position;
209
210 position = 0;
211
213 NULL))
214 if (0 < GNUNET_CRYPTO_hash_cmp (&(membership->announcement), &hash))
215 position++;
216
218 return position;
219}
220
221
223{
225 const struct GNUNET_HashCode *hash;
227};
228
231 const struct GNUNET_HashCode *key,
232 void *value)
233{
234 struct GNUNET_MESSENGER_EpochMemberConfirmation *confirmation;
235 struct GNUNET_MESSENGER_Contact *contact;
236
237 GNUNET_assert ((cls) && (key) && (value));
238
239 confirmation = cls;
240 contact = value;
241
242 if (contact == confirmation->contact)
243 {
244 if (0 == GNUNET_CRYPTO_hash_cmp (key, confirmation->hash))
245 return GNUNET_NO;
246
248 confirmation->members, confirmation->hash,
249 confirmation->contact,
251 return GNUNET_NO;
252
253 confirmation->hash = NULL;
254 GNUNET_CONTAINER_multihashmap_remove (confirmation->members, key, contact);
255 return GNUNET_NO;
256 }
257
258 return GNUNET_YES;
259}
260
261
264 membership,
265 const struct GNUNET_HashCode *hash,
266 const struct GNUNET_MESSENGER_Message *
267 message,
268 struct GNUNET_MESSENGER_Contact *contact,
270{
272
274 (membership) &&
275 (hash) &&
276 (message) &&
278 (contact));
279
281 hash))
282 return GNUNET_NO;
283
284 confirmation.members = membership->members;
285 confirmation.hash = hash;
286 confirmation.contact = contact;
287
290 &confirmation);
291
292 if ((confirmation.hash) &&
294 confirmation.members, confirmation.hash,
295 confirmation.contact,
297 return GNUNET_SYSERR;
298
299 if (GNUNET_YES == sent)
300 GNUNET_memcpy (&(membership->announcement), hash,
301 sizeof (membership->announcement));
302
303 membership->count++;
304 return GNUNET_YES;
305}
306
307
310 membership,
311 const struct GNUNET_HashCode *hash,
313{
314 GNUNET_assert ((membership) && (hash) && (contact));
315
316 if (! membership->count)
317 return GNUNET_NO;
318
320 hash))
321 return GNUNET_NO;
322
324 hash, contact))
325 return GNUNET_NO;
326
327 if (0 == GNUNET_memcmp (hash, &(membership->announcement)))
328 memset (&(membership->announcement), 0, sizeof (membership->announcement));
329
330 membership->count--;
331 return GNUNET_YES;
332}
333
334
336{
338 void *cls;
339};
340
343 const struct GNUNET_HashCode *key,
344 void *value)
345{
347 struct GNUNET_MESSENGER_Contact *contact;
348
349 GNUNET_assert ((cls) && (value));
350
351 iteration = cls;
352 contact = value;
353
354 if (iteration->callback)
355 return iteration->callback (iteration->cls, contact);
356 else
357 return GNUNET_YES;
358}
359
360
361int
365 void *cls)
366{
368
369 GNUNET_assert (membership);
370
371 iteration.callback = callback;
372 iteration.cls = cls;
373
376 &iteration);
377}
struct GNUNET_HashCode key
The key used in the DHT.
static char * value
Value of the record to add/remove.
static int result
Global testing status.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:218
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MultiHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_iterator_next(struct GNUNET_CONTAINER_MultiHashMapIterator *iter, struct GNUNET_HashCode *key, const void **value)
Retrieve the next element from the hash map at the iterator's position.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_remove(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, const void *value)
Remove the given key-value pair from the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
void GNUNET_CONTAINER_multihashmap_iterator_destroy(struct GNUNET_CONTAINER_MultiHashMapIterator *iter)
Destroy a multihashmap iterator.
struct GNUNET_CONTAINER_MultiHashMapIterator * GNUNET_CONTAINER_multihashmap_iterator_create(const struct GNUNET_CONTAINER_MultiHashMap *map)
Create an iterator for a multihashmap.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#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.
@ GNUNET_MESSENGER_KIND_ANNOUNCEMENT
The announcement kind.
enum GNUNET_GenericReturnValue is_epoch_membership_member(const struct GNUNET_MESSENGER_EpochMembership *membership, const struct GNUNET_MESSENGER_Contact *contact)
Returns whether a specific contact is announced member of a given epoch membership.
enum GNUNET_GenericReturnValue revoke_epoch_membership_announcement(struct GNUNET_MESSENGER_EpochMembership *membership, const struct GNUNET_HashCode *hash, struct GNUNET_MESSENGER_Contact *contact)
Drops an announcement message with a provided hash from a given epoch membership to revoke the confir...
enum GNUNET_GenericReturnValue confirm_epoch_membership_announcment(struct GNUNET_MESSENGER_EpochMembership *membership, const struct GNUNET_HashCode *hash, const struct GNUNET_MESSENGER_Message *message, struct GNUNET_MESSENGER_Contact *contact, enum GNUNET_GenericReturnValue sent)
Adds an announcement message with its hash to a given epoch membership as confirmation for a specific...
int iterate_epoch_membership_members(const struct GNUNET_MESSENGER_EpochMembership *membership, GNUNET_MESSENGER_MembershipCallback callback, void *cls)
Iterate through all confirmed members of a given epoch membership and pass them through a provided ca...
uint32_t get_epoch_membership_size(const struct GNUNET_MESSENGER_EpochMembership *membership)
Returns the size of a given epoch membership.
enum GNUNET_GenericReturnValue is_epoch_membership_completed(const struct GNUNET_MESSENGER_EpochMembership *membership)
Returns whether a given epoch membership is complete, meaning that all of its intended members have p...
static enum GNUNET_GenericReturnValue it_update_epoch_member(void *cls, const struct GNUNET_HashCode *key, void *value)
struct GNUNET_MESSENGER_EpochMembership * create_epoch_membership(uint32_t size)
Creates and allocates a new membership for subgroups of an epoch with specified size.
enum GNUNET_GenericReturnValue get_epoch_membership_member_hash(const struct GNUNET_MESSENGER_EpochMembership *membership, struct GNUNET_HashCode *hash, enum GNUNET_GenericReturnValue other)
Provides an announcement hash of a member from a given epoch membership.
uint32_t get_epoch_membership_member_position(const struct GNUNET_MESSENGER_EpochMembership *membership)
Returns the index position of the client inside a given epoch in relation to its list of members.
static enum GNUNET_GenericReturnValue it_iterate_epoch_member(void *cls, const struct GNUNET_HashCode *key, void *value)
void destroy_epoch_membership(struct GNUNET_MESSENGER_EpochMembership *membership)
Destroys and frees resources of a given membership.
static enum GNUNET_GenericReturnValue it_search_epoch_member(void *cls, const struct GNUNET_HashCode *hash, void *value)
uint32_t get_epoch_membership_count(const struct GNUNET_MESSENGER_EpochMembership *membership)
Returns the current amount of individual members inside a given epoch membership.
enum GNUNET_GenericReturnValue(* GNUNET_MESSENGER_MembershipCallback)(void *cls, const struct GNUNET_MESSENGER_Contact *member)
static unsigned int size
Size of the "table".
Definition: peer.c:68
Internal representation of the hash map.
A 512-bit hashcode.
struct GNUNET_CONTAINER_MultiHashMap * members
GNUNET_MESSENGER_MembershipCallback callback
struct GNUNET_CONTAINER_MultiHashMap * members
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_MESSENGER_MessageHeader header
Header.