GNUnet  0.19.2
gnunet-service-messenger_member.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2020--2021 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 
30 
33  const struct GNUNET_ShortHashCode *id)
34 {
36 
38 
39  member->store = store;
40 
41  if (id)
42  GNUNET_memcpy(&(member->id), id, sizeof(member->id));
43  else if (GNUNET_YES != generate_free_member_id(&(member->id), store->members))
44  {
45  GNUNET_free (member);
46  return NULL;
47  }
48 
50 
51  return member;
52 }
53 
54 static int
56  const struct GNUNET_HashCode *key,
57  void *value)
58 {
59  struct GNUNET_MESSENGER_MemberSession *session = value;
60  destroy_member_session(session);
61  return GNUNET_YES;
62 }
63 
64 void
66 {
68 
71 
73 }
74 
75 const struct GNUNET_ShortHashCode*
76 get_member_id (const struct GNUNET_MESSENGER_Member *member)
77 {
78  GNUNET_assert (member);
79 
80  return &(member->id);
81 }
82 
83 static int
85  const char *filename)
86 {
87  struct GNUNET_MESSENGER_Member *member = cls;
88 
90  {
91  char *directory;
92 
93  GNUNET_asprintf (&directory, "%s%c", filename, DIR_SEPARATOR);
94 
95  load_member_session(member, directory);
96  GNUNET_free (directory);
97  }
98 
99  return GNUNET_OK;
100 }
101 
102 void
104  const char *directory)
105 {
106  GNUNET_assert ((store) && (directory));
107 
108  char *config_file;
109  GNUNET_asprintf (&config_file, "%s%s", directory, "member.cfg");
110 
111  struct GNUNET_MESSENGER_Member *member = NULL;
112 
114  goto free_config;
115 
116  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Load member configuration: %s\n", config_file);
117 
119 
121  {
122  struct GNUNET_ShortHashCode id;
123 
124  if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "member", "id", &id, sizeof(id)))
125  goto destroy_config;
126 
127  member = add_store_member(store, &id);
128  }
129 
130 destroy_config:
131 
133 
134 free_config:
136 
137  if (!member)
138  return;
139 
140  char *scan_dir;
141  GNUNET_asprintf (&scan_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);
142 
145 
147 }
148 
149 static int
151  const struct GNUNET_HashCode *key,
152  void *value)
153 {
154  const char* sessions_directory = cls;
155 
156  char* load_dir;
157  GNUNET_asprintf (&load_dir, "%s%s%c", sessions_directory, GNUNET_h2s(key), DIR_SEPARATOR);
158 
159  struct GNUNET_MESSENGER_MemberSession *session = value;
160 
162  load_member_session_next (session, load_dir);
163 
164  GNUNET_free (load_dir);
165  return GNUNET_YES;
166 }
167 
168 void
170  const char *directory)
171 {
172  GNUNET_assert ((member) && (directory));
173 
174  char* load_dir;
175  GNUNET_asprintf (&load_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);
176 
178 
179  GNUNET_free(load_dir);
180 }
181 
182 static int
184  const struct GNUNET_HashCode *key,
185  void *value)
186 {
187  const char* sessions_directory = cls;
188 
189  char* save_dir;
190  GNUNET_asprintf (&save_dir, "%s%s%c", sessions_directory, GNUNET_h2s(key), DIR_SEPARATOR);
191 
192  struct GNUNET_MESSENGER_MemberSession *session = value;
193 
194  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
195  (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
196  save_member_session (session, save_dir);
197 
198  GNUNET_free (save_dir);
199  return GNUNET_YES;
200 }
201 
202 void
204  const char *directory)
205 {
206  GNUNET_assert ((member) && (directory));
207 
208  char *config_file;
209  GNUNET_asprintf (&config_file, "%s%s", directory, "member.cfg");
210 
211  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Save member configuration: %s\n", config_file);
212 
214 
215  char *id_data = GNUNET_STRINGS_data_to_string_alloc (&(member->id), sizeof(member->id));
216 
217  if (id_data)
218  {
219  GNUNET_CONFIGURATION_set_value_string (cfg, "member", "id", id_data);
220 
221  GNUNET_free(id_data);
222  }
223 
226 
228 
229  char* save_dir;
230  GNUNET_asprintf (&save_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);
231 
232  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
233  (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
235 
236  GNUNET_free(save_dir);
237 }
238 
239 static void
241  struct GNUNET_MESSENGER_MemberSession *next)
242 {
243  GNUNET_assert((session) && (next));
244 
245  if (session == next)
246  return;
247 
248  if (next->next)
249  sync_session_contact_from_next (session, next->next);
250  else
251  session->contact = next->contact;
252 }
253 
254 static int
256  const struct GNUNET_HashCode *key,
257  void *value)
258 {
259  struct GNUNET_MESSENGER_MemberSession *session = value;
260 
261  if (session->next)
262  sync_session_contact_from_next (session, session->next);
263 
264  return GNUNET_YES;
265 }
266 
267 void
269 {
271 
273 }
274 
278 {
280 
281  struct GNUNET_HashCode hash;
282  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);
283 
284  return GNUNET_CONTAINER_multihashmap_get(member->sessions, &hash);
285 }
286 
289  const struct GNUNET_HashCode *hash;
290 
292 };
293 
294 static int
296  const struct GNUNET_HashCode *key,
297  void *value)
298 {
299  struct GNUNET_MESSENGER_ClosureSearchSession* search = cls;
300  struct GNUNET_MESSENGER_MemberSession *session = value;
301 
302  if (GNUNET_OK != verify_member_session_as_sender(session, search->message, search->hash))
303  return GNUNET_YES;
304 
305  search->match = session;
306  return GNUNET_NO;
307 }
308 
309 static struct GNUNET_MESSENGER_MemberSession*
312 {
314 
315  if (session)
316  return session;
317 
319 
320  if (session)
321  add_member_session(member, session);
322 
323  return session;
324 }
325 
328  const struct GNUNET_MESSENGER_Message *message,
329  const struct GNUNET_HashCode *hash)
330 {
331  GNUNET_assert ((member) && (message) && (hash) &&
332  (0 == GNUNET_memcmp(&(member->id), &(message->header.sender_id))));
333 
334  if (GNUNET_MESSENGER_KIND_INFO == message->header.kind)
335  return try_member_session(member, &(message->body.info.host_key));
336  else if (GNUNET_MESSENGER_KIND_JOIN == message->header.kind)
337  return try_member_session(member, &(message->body.join.key));
338 
340 
341  search.message = message;
342  search.hash = hash;
343 
344  search.match = NULL;
346 
347  return search.match;
348 }
349 
350 void
352  struct GNUNET_MESSENGER_MemberSession *session)
353 {
354  if (!session)
355  return;
356 
357  GNUNET_assert((member) && (session->member == member));
358 
359  const struct GNUNET_IDENTITY_PublicKey *public_key = get_member_session_public_key(session);
360 
361  struct GNUNET_HashCode hash;
362  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);
363 
365  member->sessions, &hash, session,
367  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding a member session failed: %s\n",
368  GNUNET_h2s(&hash));
369 }
370 
371 void
373  struct GNUNET_MESSENGER_MemberSession *session)
374 {
375  GNUNET_assert ((member) && (session) && (session->member == member));
376 
377  const struct GNUNET_IDENTITY_PublicKey *public_key = get_member_session_public_key(session);
378 
379  struct GNUNET_HashCode hash;
380  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);
381 
382  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(member->sessions, &hash, session))
383  GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Removing a member session failed: %s\n",
384  GNUNET_h2s(&hash));
385 }
386 
389  void *cls;
390 };
391 
392 static int
394  const struct GNUNET_HashCode *key,
395  void *value)
396 {
398  struct GNUNET_MESSENGER_MemberSession *session = value;
399 
400  return iterate->it (iterate->cls, get_member_session_public_key(session), session);
401 }
402 
403 int
406  void *cls)
407 {
408  GNUNET_assert ((member) && (member->sessions) && (it));
409 
411 
412  iterate.it = it;
413  iterate.cls = cls;
414 
416 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static char * config_file
Set to the name of the config file used.
Definition: gnunet-arm.c:84
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static char * value
Value of the record to add/remove.
static struct GNUNET_MESSENGER_MemberSession * try_member_session(struct GNUNET_MESSENGER_Member *member, const struct GNUNET_IDENTITY_PublicKey *public_key)
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 remove_member_session(struct GNUNET_MESSENGER_Member *member, struct GNUNET_MESSENGER_MemberSession *session)
Removes a given member session from its member.
static int iterate_sync_session_contact(void *cls, const struct GNUNET_HashCode *key, void *value)
static int iterate_search_session(void *cls, const struct GNUNET_HashCode *key, void *value)
static void sync_session_contact_from_next(struct GNUNET_MESSENGER_MemberSession *session, struct GNUNET_MESSENGER_MemberSession *next)
struct GNUNET_MESSENGER_MemberSession * get_member_session(const struct GNUNET_MESSENGER_Member *member, const struct GNUNET_IDENTITY_PublicKey *public_key)
Returns the member session of a member identified by a given public key.
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.
static int iterate_save_session(void *cls, const struct GNUNET_HashCode *key, void *value)
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 add_member_session(struct GNUNET_MESSENGER_Member *member, struct GNUNET_MESSENGER_MemberSession *session)
Adds a given member session to its member.
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...
static int callback_scan_for_sessions(void *cls, const char *filename)
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...
const struct GNUNET_ShortHashCode * get_member_id(const struct GNUNET_MESSENGER_Member *member)
Returns the current id of a given member.
static int iterate_destroy_session(void *cls, const struct GNUNET_HashCode *key, void *value)
static int iterate_load_next_session(void *cls, const struct GNUNET_HashCode *key, void *value)
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...
struct GNUNET_MESSENGER_MemberSession * get_member_session_of(struct GNUNET_MESSENGER_Member *member, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Returns the member session of a member using a public key which can verify the signature of a given m...
static int iterate_member_sessions_it(void *cls, const struct GNUNET_HashCode *key, void *value)
GNUnet MESSENGER service.
void destroy_member_session(struct GNUNET_MESSENGER_MemberSession *session)
Destroys a member session and frees its memory fully.
void load_member_session_next(struct GNUNET_MESSENGER_MemberSession *session, const char *directory)
Loads the connection from one session to another through the next attribute.
const struct GNUNET_IDENTITY_PublicKey * get_member_session_public_key(const struct GNUNET_MESSENGER_MemberSession *session)
Returns the public key from an EGO of a given member session.
int verify_member_session_as_sender(const struct GNUNET_MESSENGER_MemberSession *session, const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash)
Verifies a given member session as sender of a selected message and its hash.
void load_member_session(struct GNUNET_MESSENGER_Member *member, const char *directory)
Loads data from a directory into a new allocated and created member session of a member if the requir...
void save_member_session(struct GNUNET_MESSENGER_MemberSession *session, const char *directory)
Saves data from a member session into a directory which can be load to restore the member session com...
struct GNUNET_MESSENGER_MemberSession * create_member_session(struct GNUNET_MESSENGER_Member *member, const struct GNUNET_IDENTITY_PublicKey *pubkey)
Creates and allocates a new member session of a member with a given public key.
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.
int(* GNUNET_MESSENGER_MemberIteratorCallback)(void *cls, const struct GNUNET_IDENTITY_PublicKey *public_key, struct GNUNET_MESSENGER_MemberSession *session)
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, void *buf, size_t buf_size)
Get Crockford32-encoded fixed-size binary data from a configuration.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(void)
Create a new configuration object.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Parse a configuration file, add all of the options in the file to the configuration environment.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
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:481
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:403
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:495
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:813
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
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.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
int GNUNET_CONTAINER_multihashmap_iterate(struct GNUNET_CONTAINER_MultiHashMap *map, GNUNET_CONTAINER_MulitHashMapIteratorCallback it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
void * GNUNET_CONTAINER_multihashmap_get(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Given a key find a value in the map matching the key.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#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_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
@ GNUNET_MESSENGER_KIND_INFO
The info kind.
@ GNUNET_MESSENGER_KIND_JOIN
The join kind.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:763
int generate_free_member_id(struct GNUNET_ShortHashCode *id, const struct GNUNET_CONTAINER_MultiShortmap *members)
Tries to generate an unused member id and store it into the id parameter.
#define DIR_SEPARATOR
Definition: platform.h:164
A 512-bit hashcode.
An identity key as per LSD0001.
GNUNET_MESSENGER_MemberIteratorCallback it
const struct GNUNET_MESSENGER_Message * message
struct GNUNET_MESSENGER_MemberSession * match
struct GNUNET_MESSENGER_MemberSession * next
struct GNUNET_CONTAINER_MultiShortmap * members
struct GNUNET_CONTAINER_MultiHashMap * sessions
struct GNUNET_MESSENGER_MemberStore * store
struct GNUNET_ShortHashCode id
struct GNUNET_MESSENGER_MessageJoin join
struct GNUNET_MESSENGER_MessageInfo info
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_IDENTITY_PublicKey host_key
The senders key to verify its signatures.
struct GNUNET_IDENTITY_PublicKey key
The senders public key to verify its signatures.
struct GNUNET_MESSENGER_MessageHeader header
Header.
struct GNUNET_MESSENGER_MessageBody body
Body.
A 256-bit hashcode.