GNUnet 0.22.2
gnunet-service-messenger_member_store.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020--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 */
26#include "platform.h"
28
32
34
35void
37 struct GNUNET_MESSENGER_SrvRoom *room)
38{
39 GNUNET_assert ((store) && (room));
40
41 store->room = room;
43}
44
45
48 const struct GNUNET_ShortHashCode *key,
49 void *value)
50{
51 struct GNUNET_MESSENGER_Member *member;
52
54
55 member = value;
56
57 destroy_member (member);
58 return GNUNET_YES;
59}
60
61
62void
64{
66
67 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Clear member store of room: %s\n",
69
73}
74
75
78{
79 struct GNUNET_MESSENGER_SrvRoom *room;
80
81 GNUNET_assert ((store) && (store->room));
82
83 room = store->room;
84
86}
87
88
89const struct GNUNET_HashCode*
91{
92 GNUNET_assert (store);
93
94 return get_srv_room_key ((const struct
96}
97
98
101 const char *filename)
102{
103 struct GNUNET_MESSENGER_MemberStore *store;
104
105 GNUNET_assert ((cls) && (filename));
106
107 store = cls;
108
110 {
111 char *directory;
112
113 GNUNET_asprintf (&directory, "%s%c", filename, DIR_SEPARATOR);
114
115 load_member (store, directory);
116
117 GNUNET_free (directory);
118 }
119
120 return GNUNET_OK;
121}
122
123
126 const struct GNUNET_ShortHashCode *id,
127 void *value)
128{
129 struct GNUNET_MESSENGER_Member *member;
130 const char *sync_dir;
131 char *member_dir;
132
133 GNUNET_assert ((value) && (id) && (cls));
134
135 member = value;
136 sync_dir = cls;
137
138 if (! member)
139 return GNUNET_YES;
140
141 GNUNET_asprintf (&member_dir, "%s%s%c", sync_dir, GNUNET_sh2s (id),
143
145 load_member_next_sessions (member, member_dir);
146
147 GNUNET_free (member_dir);
148 return GNUNET_YES;
149}
150
151
154 const struct GNUNET_ShortHashCode *id,
155 void *value)
156{
157 struct GNUNET_MESSENGER_Member *member;
158
160
161 member = value;
162
163 if (! member)
164 return GNUNET_YES;
165
166 sync_member_contacts (member);
167 return GNUNET_YES;
168}
169
170
171void
173 const char *directory)
174{
175 char *scan_dir;
176
177 GNUNET_assert ((store) && (directory));
178
179 GNUNET_asprintf (&scan_dir, "%s%s%c", directory, "members", DIR_SEPARATOR);
180
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Load member store from directory: %s\n",
182 scan_dir);
183
186
189 scan_dir);
192
194}
195
196
199 const struct GNUNET_ShortHashCode *id,
200 void *value)
201{
202 struct GNUNET_MESSENGER_Member *member;
203 const char *save_dir;
204 char *member_dir;
205
206 GNUNET_assert ((value) && (id) && (cls));
207
208 member = value;
209 save_dir = cls;
210
211 if (! member)
212 return GNUNET_YES;
213
214 GNUNET_asprintf (&member_dir, "%s%s%c", save_dir, GNUNET_sh2s (id),
216
217 if ((GNUNET_YES == GNUNET_DISK_directory_test (member_dir, GNUNET_NO)) ||
218 (GNUNET_OK == GNUNET_DISK_directory_create (member_dir)))
219 save_member (member, member_dir);
220
221 GNUNET_free (member_dir);
222 return GNUNET_YES;
223}
224
225
226void
228 const char *directory)
229{
230 char *save_dir;
231
232 GNUNET_assert ((store) && (directory));
233
234 GNUNET_asprintf (&save_dir, "%s%s%c", directory, "members", DIR_SEPARATOR);
235
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Save member store to directory: %s\n",
237 save_dir);
238
239 if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
242 iterate_save_members, save_dir);
243
244 GNUNET_free (save_dir);
245}
246
247
250 const struct GNUNET_ShortHashCode *id)
251{
252 GNUNET_assert ((store) && (store->members) && (id));
253
255}
256
257
260 const struct GNUNET_MESSENGER_Message *message)
261{
262 GNUNET_assert ((store) && (message));
263
264 if (GNUNET_YES == is_peer_message (message))
265 return NULL;
266
267 if (GNUNET_MESSENGER_KIND_JOIN == message->header.kind)
268 return add_store_member (store, &(message->header.sender_id));
269 else
270 return get_store_member (store, &(message->header.sender_id));
271}
272
273
276 const struct GNUNET_ShortHashCode *id)
277{
278 struct GNUNET_MESSENGER_Member *member;
279
281
282 member = id? get_store_member (store, id) : NULL;
283
284 if (member)
285 return member;
286
287 member = create_member (store, id);
288
289 if (! member)
290 return NULL;
291
293 get_member_id (member),
294 member,
296 {
297 destroy_member (member);
298 return NULL;
299 }
300
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added store member with id: %s\n",
302 GNUNET_sh2s (get_member_id (member)));
303
304 return member;
305}
306
307
309{
311 void *cls;
312};
313
314static int
316 const struct GNUNET_ShortHashCode *key,
317 void *value)
318{
320 struct GNUNET_MESSENGER_Member *member;
321
322 GNUNET_assert ((cls) && (value));
323
324 iterate = cls;
325 member = value;
326
327 return iterate_member_sessions (member, iterate->it, iterate->cls);
328}
329
330
331int
334 void *cls)
335{
337
338 GNUNET_assert ((store) && (store->members) && (it));
339
340 iterate.it = it;
341 iterate.cls = cls;
342
345 &iterate);
346}
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static char * value
Value of the record to add/remove.
const struct GNUNET_ShortHashCode * get_member_id(const struct GNUNET_MESSENGER_Member *member)
Returns the current id of a given member.
void save_member(struct GNUNET_MESSENGER_Member *member, const char *directory)
Saves data from a member into a directory which can be load to restore the member completely.
void destroy_member(struct GNUNET_MESSENGER_Member *member)
Destroys a member and frees its memory fully.
struct GNUNET_MESSENGER_Member * create_member(struct GNUNET_MESSENGER_MemberStore *store, const struct GNUNET_ShortHashCode *id)
Creates and allocates a new member of a room with an optionally defined or random id.
void load_member_next_sessions(const struct GNUNET_MESSENGER_Member *member, const char *directory)
Loads data about next sessions from a directory into an empty loaded member which does not contain a ...
void sync_member_contacts(struct GNUNET_MESSENGER_Member *member)
Synchronizes contacts between all sessions from a given member and other sessions which are linked to...
int iterate_member_sessions(struct GNUNET_MESSENGER_Member *member, GNUNET_MESSENGER_MemberIteratorCallback it, void *cls)
Iterate through all member sessions currently connected to a given member and call the provided itera...
void load_member(struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
Loads data from a directory into a new allocated and created member of a store if the required inform...
static enum GNUNET_GenericReturnValue callback_scan_for_members(void *cls, const char *filename)
static enum GNUNET_GenericReturnValue iterate_load_next_member_sessions(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
struct GNUNET_MESSENGER_ContactStore * get_member_contact_store(struct GNUNET_MESSENGER_MemberStore *store)
Returns the used contact store of a given member store.
struct GNUNET_MESSENGER_Member * get_store_member_of(struct GNUNET_MESSENGER_MemberStore *store, const struct GNUNET_MESSENGER_Message *message)
Returns the member of a store using a sender id of a given message.
int iterate_store_members(struct GNUNET_MESSENGER_MemberStore *store, GNUNET_MESSENGER_MemberIteratorCallback it, void *cls)
Iterate through all member sessions currently connected to the members of the given member store and ...
void save_member_store(struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
Saves members from a member store into a directory.
void load_member_store(struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
Loads members from a directory into a member store.
static enum GNUNET_GenericReturnValue iterate_destroy_members(void *cls, const struct GNUNET_ShortHashCode *key, void *value)
const struct GNUNET_HashCode * get_member_store_key(const struct GNUNET_MESSENGER_MemberStore *store)
Returns the shared secret you need to access a room of the store.
static enum GNUNET_GenericReturnValue iterate_sync_member_contacts(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
static int iterate_store_members_it(void *cls, const struct GNUNET_ShortHashCode *key, void *value)
struct GNUNET_MESSENGER_Member * add_store_member(struct GNUNET_MESSENGER_MemberStore *store, const struct GNUNET_ShortHashCode *id)
Adds a member to a store under a specific id and returns it on success.
void init_member_store(struct GNUNET_MESSENGER_MemberStore *store, struct GNUNET_MESSENGER_SrvRoom *room)
Initializes a member store as fully empty connected to a room.
void clear_member_store(struct GNUNET_MESSENGER_MemberStore *store)
Clears a member store, wipes its content and deallocates its memory.
static enum GNUNET_GenericReturnValue iterate_save_members(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
struct GNUNET_MESSENGER_Member * get_store_member(const struct GNUNET_MESSENGER_MemberStore *store, const struct GNUNET_ShortHashCode *id)
Returns the member in a store identified by a given id.
enum GNUNET_GenericReturnValue(* GNUNET_MESSENGER_MemberIteratorCallback)(void *cls, const struct GNUNET_CRYPTO_PublicKey *public_key, struct GNUNET_MESSENGER_MemberSession *session)
const struct GNUNET_HashCode * get_srv_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
struct GNUNET_MESSENGER_ContactStore * get_service_contact_store(struct GNUNET_MESSENGER_Service *service)
Returns the used contact store of a given service.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:427
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:520
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:811
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.
void * GNUNET_CONTAINER_multishortmap_get(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Given a key find a value in the map matching the key.
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.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#define GNUNET_log(kind,...)
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).
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_free(ptr)
Wrapper around free.
@ GNUNET_MESSENGER_KIND_JOIN
The join 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...
#define DIR_SEPARATOR
Definition: platform.h:165
A 512-bit hashcode.
struct GNUNET_CONTAINER_MultiShortmap * members
struct GNUNET_MESSENGER_MemberStore * store
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_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_Service * service
A 256-bit hashcode.