GNUnet 0.22.2
gnunet-service-messenger_peer_store.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2023--2024 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
29#include "gnunet_common.h"
31#include "messenger_api_util.h"
32
34{
37};
38
39void
42{
43 GNUNET_assert ((store) && (service));
44
45 store->service = service;
47}
48
49
51iterate_destroy_peers (void *cls, const struct GNUNET_ShortHashCode *id,
52 void *value)
53{
55
57
58 entry = value;
59
60 GNUNET_free (entry);
61 return GNUNET_YES;
62}
63
64
65void
67{
68 GNUNET_assert ((store) && (store->peers));
69
70 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Clear peer store\n");
71
73 NULL);
75
76 store->peers = NULL;
77}
78
79
80void
82 const char *path)
83{
86 struct GNUNET_PeerIdentity peer;
87 ssize_t len;
88
89 GNUNET_assert ((store) && (path));
90
92 return;
93
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load peer store from path: %s\n",
95 path);
96
97 {
98 enum GNUNET_DISK_AccessPermissions permission;
99
102 }
103
104 if (! handle)
105 return;
106
108
109 do {
111
112 len = GNUNET_DISK_file_read (handle, &peer, sizeof(peer));
113
114 if (len != sizeof(peer))
115 break;
116
117
119
120 if (! entry)
121 continue;
122
123 GNUNET_memcpy (&(entry->peer), &peer, sizeof(entry->peer));
124 entry->active = GNUNET_YES;
125
127
129 store->peers, &peer_id, entry,
131 continue;
132
133 GNUNET_free (entry);
134 } while (len == sizeof(peer));
135
137}
138
139
141iterate_save_peers (void *cls, const struct GNUNET_ShortHashCode *id,
142 void *value)
143{
146
147 GNUNET_assert ((cls) && (id) && (value));
148
149 handle = cls;
150 entry = value;
151
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save peer store entry: %s\n",
153 GNUNET_sh2s (id));
154
155 if ((! entry) || (GNUNET_YES != entry->active))
156 return GNUNET_YES;
157
158 GNUNET_DISK_file_write (handle, &(entry->peer), sizeof(entry->peer));
159 return GNUNET_YES;
160}
161
162
163void
165 const char *path)
166{
168
169 GNUNET_assert ((store) && (path));
170
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save peer store to path: %s\n",
172 path);
173
174 {
175 enum GNUNET_DISK_AccessPermissions permission;
176
180 );
181 }
182
183 if (! handle)
184 return;
185
188 handle);
189
192}
193
194
196{
198 const struct GNUNET_HashCode *hash;
200};
201
203verify_store_peer (void *cls, const struct GNUNET_ShortHashCode *id,
204 void *value)
205{
208
209 GNUNET_assert ((cls) && (value));
210
211 verify = cls;
212 entry = value;
213
214 if (! entry)
215 return GNUNET_YES;
216
217 if (GNUNET_OK == verify_message_by_peer (verify->message,
218 verify->hash, &(entry->peer)))
219 {
220 verify->sender = &(entry->peer);
221 return GNUNET_NO;
222 }
223
224 return GNUNET_YES;
225}
226
227
230 const struct GNUNET_PeerIdentity *peer,
231 const struct GNUNET_ShortHashCode *id,
233{
235
236 GNUNET_assert ((store) && (peer));
237
239
240 if (! entry)
241 return NULL;
242
243 GNUNET_memcpy (&(entry->peer), peer, sizeof(entry->peer));
244 entry->active = active;
245
247 store->peers, id, entry,
249 {
250 GNUNET_free (entry);
251 return NULL;
252 }
253
254 return entry;
255}
256
257
258static const struct GNUNET_PeerIdentity*
260{
261 static struct GNUNET_PeerIdentity peer;
262
263 if (GNUNET_OK != get_service_peer_identity (store->service, &peer))
264 return NULL;
265
266 return &peer;
267}
268
269
272 const struct GNUNET_MESSENGER_Message *message,
273 const struct GNUNET_HashCode *hash)
274{
275 const struct GNUNET_PeerIdentity *peer;
276 enum GNUNET_GenericReturnValue active;
278
279 GNUNET_assert ((store) && (store->peers) && (message) && (hash));
280
281 if (GNUNET_YES != is_peer_message (message))
282 return NULL;
283
284 {
286 verify.message = message;
287 verify.hash = hash;
288 verify.sender = NULL;
289
293
294 if (verify.sender)
295 return verify.sender;
296 }
297
299 {
300 peer = &(message->body.peer.peer);
301 active = GNUNET_YES;
302 }
304 {
305 peer = &(message->body.miss.peer);
306 active = GNUNET_NO;
307 }
308 else
309 {
311 "Peer message does not contain a peer identity\n");
312
313 peer = get_store_service_peer_identity (store);
314 active = GNUNET_NO;
315
316 if (! peer)
317 return NULL;
318 }
319
321
323 {
325 "Sender id does not match peer identity\n");
326 return NULL;
327 }
328
330 {
332 "Verification of message with peer identity failed!\n");
333 }
334
335 {
337 entry = add_peer_store_entry (store, peer, &peer_id, active);
338
339 if (! entry)
340 {
342 "Initialization of entry in peer store failed: %s\n",
344
345 return NULL;
346 }
347
348 return &(entry->peer);
349 }
350}
351
352
354{
357};
358
360find_store_peer (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
361{
364
365 GNUNET_assert ((cls) && (value));
366
367 find = cls;
368 entry = value;
369
370 if (! entry)
371 return GNUNET_YES;
372
373 if (0 == GNUNET_memcmp (find->requested, &(entry->peer)))
374 {
375 find->match = entry;
376 return GNUNET_NO;
377 }
378
379 return GNUNET_YES;
380}
381
382
383void
385 const struct GNUNET_PeerIdentity *peer,
387{
389
390 GNUNET_assert ((store) && (store->peers) && (peer));
391
393
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Update peer store entry: %s\n",
396
397 {
399 find.requested = peer;
400 find.match = NULL;
401
403 find_store_peer, &find);
404
405 if (find.match)
406 {
407 find.match->active = active;
408 return;
409 }
410 }
411
412 if (! add_peer_store_entry (store, peer, &peer_id, active))
414 "Initial update of entry in peer store failed: %s\n",
416}
static int verify
Verify mode.
Definition: gnunet-abd.c:128
static char * peer_id
Option –peer.
Definition: gnunet-cadet.c:42
static char * value
Value of the record to add/remove.
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
void update_store_peer(struct GNUNET_MESSENGER_PeerStore *store, const struct GNUNET_PeerIdentity *peer, enum GNUNET_GenericReturnValue active)
Adds a peer identity to the store if necessary.
static enum GNUNET_GenericReturnValue find_store_peer(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
struct GNUNET_PeerIdentity * get_store_peer_of(struct GNUNET_MESSENGER_PeerStore *store, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Returns the peer identity inside the store which verifies the signature of a given message as valid.
void load_peer_store(struct GNUNET_MESSENGER_PeerStore *store, const char *path)
Loads peer identities from a file into a peer store.
void clear_peer_store(struct GNUNET_MESSENGER_PeerStore *store)
Clears a peer store, wipes its content and deallocates its memory.
static const struct GNUNET_PeerIdentity * get_store_service_peer_identity(struct GNUNET_MESSENGER_PeerStore *store)
static enum GNUNET_GenericReturnValue iterate_destroy_peers(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
static struct GNUNET_MESSENGER_PeerStoreEntry * add_peer_store_entry(struct GNUNET_MESSENGER_PeerStore *store, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_ShortHashCode *id, enum GNUNET_GenericReturnValue active)
static enum GNUNET_GenericReturnValue verify_store_peer(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
static enum GNUNET_GenericReturnValue iterate_save_peers(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
void save_peer_store(const struct GNUNET_MESSENGER_PeerStore *store, const char *path)
Saves peer identities from a peer store into a file.
void init_peer_store(struct GNUNET_MESSENGER_PeerStore *store, struct GNUNET_MESSENGER_Service *service)
Initializes a peer store as fully empty.
enum GNUNET_GenericReturnValue get_service_peer_identity(struct GNUNET_MESSENGER_Service *service, struct GNUNET_PeerIdentity *peer)
Tries to write the peer identity of the peer running a service on to the peer parameter.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
Definition: gnunet-vpn.c:35
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1215
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:506
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition: disk.c:682
off_t GNUNET_DISK_file_seek(const struct GNUNET_DISK_FileHandle *h, off_t offset, enum GNUNET_DISK_Seek whence)
Move the read/write pointer in a file.
Definition: disk.c:206
GNUNET_DISK_AccessPermissions
File access permissions, UNIX-style.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition: disk.c:1408
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1289
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:646
@ GNUNET_DISK_OPEN_READ
Open the file for reading.
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
@ GNUNET_DISK_SEEK_SET
Seek an absolute position (from the start of the file).
struct GNUNET_CONTAINER_MultiShortmap * GNUNET_CONTAINER_multishortmap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multishortmap_put(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
int GNUNET_CONTAINER_multishortmap_iterate(struct GNUNET_CONTAINER_MultiShortmap *map, GNUNET_CONTAINER_ShortmapIterator it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
int GNUNET_CONTAINER_multishortmap_get_multiple(struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key, GNUNET_CONTAINER_ShortmapIterator it, void *it_cls)
Iterate over all entries in the map that match a particular key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
#define GNUNET_log(kind,...)
#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
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
@ GNUNET_MESSENGER_KIND_MISS
The miss kind.
@ GNUNET_MESSENGER_KIND_PEER
The peer kind.
enum GNUNET_GenericReturnValue is_peer_message(const struct GNUNET_MESSENGER_Message *message)
Returns whether a specific kind of message can be sent by the service without usage of a clients priv...
enum GNUNET_GenericReturnValue verify_message_by_peer(const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, const struct GNUNET_PeerIdentity *identity)
Verifies the signature of a given message and its hash with a specific peer's identity.
void convert_peer_identity_to_id(const struct GNUNET_PeerIdentity *identity, struct GNUNET_ShortHashCode *id)
Converts a peers identity to a short hash code which can be used as id to refer to a peer via sender ...
Handle used to access files (and pipes).
A 512-bit hashcode.
struct GNUNET_MESSENGER_PeerStoreEntry * match
const struct GNUNET_PeerIdentity * requested
const struct GNUNET_MESSENGER_Message * message
struct GNUNET_MESSENGER_MessageMiss miss
struct GNUNET_MESSENGER_MessagePeer peer
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
struct GNUNET_PeerIdentity peer
The peer identity of a disconnected door to a room.
struct GNUNET_PeerIdentity peer
The peer identity of the sender opening a room.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.
struct GNUNET_MESSENGER_Service * service
struct GNUNET_CONTAINER_MultiShortmap * peers
The identity of the host (wraps the signing key of the peer).
A 256-bit hashcode.