GNUnet  0.11.x
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--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  */
27 
31 
32 void
34 {
35  GNUNET_assert ((store) && (room));
36 
37  store->room = room;
39 }
40 
41 static int
42 iterate_destroy_members (void *cls, const struct GNUNET_ShortHashCode *key, void *value)
43 {
44  struct GNUNET_MESSENGER_Member *member = value;
45  destroy_member(member);
46  return GNUNET_YES;
47 }
48 
49 void
51 {
52  GNUNET_assert ((store) && (store->members));
53 
56 }
57 
58 
61 {
62  GNUNET_assert ((store) && (store->room));
63 
64  struct GNUNET_MESSENGER_SrvRoom *room = store->room;
65 
66  return get_service_contact_store(room->service);
67 }
68 
69 const struct GNUNET_HashCode*
71 {
72  GNUNET_assert (store);
73 
74  return get_room_key((const struct GNUNET_MESSENGER_SrvRoom*) store->room);
75 }
76 
77 static int
78 callback_scan_for_members (void *cls, const char *filename)
79 {
80  struct GNUNET_MESSENGER_MemberStore *store = cls;
81 
83  {
84  char *directory;
85 
86  GNUNET_asprintf (&directory, "%s%c", filename, DIR_SEPARATOR);
87 
88  load_member(store, directory);
89 
90  GNUNET_free(directory);
91  }
92 
93  return GNUNET_OK;
94 }
95 
96 static int
97 iterate_load_next_member_sessions (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
98 {
99  const char *sync_dir = cls;
100 
101  struct GNUNET_MESSENGER_Member *member = value;
102 
103  if (!member)
104  return GNUNET_YES;
105 
106  char *member_dir;
107  GNUNET_asprintf (&member_dir, "%s%s%c", sync_dir, GNUNET_sh2s(id), DIR_SEPARATOR);
108 
109  if (GNUNET_YES == GNUNET_DISK_directory_test (member_dir, GNUNET_YES))
110  load_member_next_sessions (member, member_dir);
111 
112  GNUNET_free(member_dir);
113  return GNUNET_YES;
114 }
115 
116 static int
117 iterate_sync_member_contacts (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
118 {
119  struct GNUNET_MESSENGER_Member *member = value;
120 
121  if (!member)
122  return GNUNET_YES;
123 
124  sync_member_contacts (member);
125  return GNUNET_YES;
126 }
127 
128 void
129 load_member_store (struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
130 {
131  GNUNET_assert ((store) && (directory));
132 
133  char *scan_dir;
134  GNUNET_asprintf (&scan_dir, "%s%s%c", directory, "members", DIR_SEPARATOR);
135 
138 
141 
142  GNUNET_free(scan_dir);
143 }
144 
145 static int
146 iterate_save_members (void *cls, const struct GNUNET_ShortHashCode *id, void *value)
147 {
148  const char *save_dir = cls;
149 
150  struct GNUNET_MESSENGER_Member *member = value;
151 
152  if (!member)
153  return GNUNET_YES;
154 
155  char *member_dir;
156  GNUNET_asprintf (&member_dir, "%s%s%c", save_dir, GNUNET_sh2s(id), DIR_SEPARATOR);
157 
158  if ((GNUNET_YES == GNUNET_DISK_directory_test (member_dir, GNUNET_NO)) ||
159  (GNUNET_OK == GNUNET_DISK_directory_create (member_dir)))
160  save_member(member, member_dir);
161 
162  GNUNET_free(member_dir);
163  return GNUNET_YES;
164 }
165 
166 void
167 save_member_store (struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
168 {
169  GNUNET_assert ((store) && (directory));
170 
171  char* save_dir;
172  GNUNET_asprintf (&save_dir, "%s%s%c", directory, "members", DIR_SEPARATOR);
173 
174  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
175  (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
177 
178  GNUNET_free(save_dir);
179 }
180 
183 {
184  GNUNET_assert ((store) && (store->members) && (id));
185 
186  return GNUNET_CONTAINER_multishortmap_get (store->members, id);
187 }
188 
191 {
192  if ((GNUNET_MESSENGER_KIND_INFO == message->header.kind) ||
193  (GNUNET_MESSENGER_KIND_JOIN == message->header.kind))
194  return add_store_member(store, &(message->header.sender_id));
195  else
196  return get_store_member(store, &(message->header.sender_id));
197 }
198 
201 {
202  GNUNET_assert ((store) && (store->members));
203 
204  struct GNUNET_MESSENGER_Member *member = id? get_store_member(store, id) : NULL;
205 
206  if (member)
207  return member;
208 
209  member = create_member(store, id);
210 
211  if (!member)
212  return NULL;
213 
214  if (GNUNET_OK != GNUNET_CONTAINER_multishortmap_put (store->members, get_member_id(member), member,
216  {
217  destroy_member(member);
218  return NULL;
219  }
220 
221  return member;
222 }
223 
226  void *cls;
227 };
228 
229 static int
231 {
233  struct GNUNET_MESSENGER_Member *member = value;
234 
235  return iterate_member_sessions(member, iterate->it, iterate->cls);
236 }
237 
238 int
240  void* cls)
241 {
242  GNUNET_assert ((store) && (store->members) && (it));
243 
245 
246  iterate.it = it;
247  iterate.cls = cls;
248 
250 }
struct GNUNET_MESSENGER_MessageHeader header
Header.
const char * GNUNET_sh2s(const struct GNUNET_ShortHashCode *shc)
Convert a short hash value to a string (for printing debug messages).
struct GNUNET_MESSENGER_ContactStore * get_service_contact_store(struct GNUNET_MESSENGER_Service *service)
Returns the used contact store of a given service.
struct GNUNET_ShortHashCode sender_id
The senders id inside of the room the message was sent in.
GNUnet MESSENGER service.
GNUnet MESSENGER service.
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...
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...
struct GNUNET_CONTAINER_MultiShortmap * members
static int iterate_save_members(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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 init_member_store(struct GNUNET_MESSENGER_MemberStore *store, struct GNUNET_MESSENGER_SrvRoom *room)
Initializes a member store as fully empty connected to a room.
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition: disk.c:794
static int iterate_load_next_member_sessions(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
int(* GNUNET_MESSENGER_MemberIteratorCallback)(void *cls, const struct GNUNET_IDENTITY_PublicKey *public_key, struct GNUNET_MESSENGER_MemberSession *session)
A 256-bit hashcode.
void destroy_member(struct GNUNET_MESSENGER_Member *member)
Destroys a member and frees its memory fully.
void save_member_store(struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
Saves members from a member store into a directory.
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 ...
struct GNUNET_MESSENGER_Service * service
, &#39; bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_...
int GNUNET_CONTAINER_multishortmap_iterate(struct GNUNET_CONTAINER_MultiShortmap *map, GNUNET_CONTAINER_ShortmapIterator it, void *it_cls)
Iterate over all entries in the map.
static char * value
Value of the record to add/remove.
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...
GNUnet MESSENGER service.
static int iterate_destroy_members(void *cls, const struct GNUNET_ShortHashCode *key, void *value)
static char * filename
int 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 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...
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create(const char *dir)
Implementation of "mkdir -p".
Definition: disk.c:476
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).
A 512-bit hashcode.
static int iterate_sync_member_contacts(void *cls, const struct GNUNET_ShortHashCode *id, void *value)
static int callback_scan_for_members(void *cls, const char *filename)
void clear_member_store(struct GNUNET_MESSENGER_MemberStore *store)
Clears a member store, wipes its content and deallocates its memory.
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define DIR_SEPARATOR
Definition: platform.h:164
struct GNUNET_MESSENGER_MemberStore * store
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_MESSENGER_ContactStore * get_member_contact_store(struct GNUNET_MESSENGER_MemberStore *store)
Returns the used contact store of a given member store.
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...
void GNUNET_CONTAINER_multishortmap_destroy(struct GNUNET_CONTAINER_MultiShortmap *map)
Destroy a hash map.
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.
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.
void load_member_store(struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
Loads members from a directory into a member store.
const struct GNUNET_HashCode * get_room_key(const struct GNUNET_MESSENGER_SrvRoom *room)
Returns the shared secret you need to access a room.
static int iterate_store_members_it(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.
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.
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.
const struct GNUNET_ShortHashCode * get_member_id(const struct GNUNET_MESSENGER_Member *member)
Returns the current id of a given member.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition: disk.c:404
GNUnet MESSENGER service.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_MESSENGER_MessageKind kind
The kind of the message.