summaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_member.c
blob: 7f00e8438285c5f4558857ae55b7985ca7decbdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
   This file is part of GNUnet.
   Copyright (C) 2020--2021 GNUnet e.V.

   GNUnet is free software: you can redistribute it and/or modify it
   under the terms of the GNU Affero General Public License as published
   by the Free Software Foundation, either version 3 of the License,
   or (at your option) any later version.

   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

   SPDX-License-Identifier: AGPL3.0-or-later
 */
/**
 * @author Tobias Frisch
 * @file src/messenger/gnunet-service-messenger_member.c
 * @brief GNUnet MESSENGER service
 */

#include "gnunet-service-messenger_member.h"

#include "gnunet-service-messenger_member_session.h"

struct GNUNET_MESSENGER_Member*
create_member (struct GNUNET_MESSENGER_MemberStore *store, const struct GNUNET_ShortHashCode *id)
{
  GNUNET_assert (store);

  struct GNUNET_MESSENGER_Member *member = GNUNET_new(struct GNUNET_MESSENGER_Member);

  member->store = store;

  if (id)
    GNUNET_memcpy(&(member->id), id, sizeof(member->id));
  else if (GNUNET_YES != generate_free_member_id(&(member->id), store->members))
  {
    GNUNET_free (member);
    return NULL;
  }

  member->sessions = GNUNET_CONTAINER_multihashmap_create(2, GNUNET_NO);

  return member;
}

static int
iterate_destroy_session (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_MemberSession *session = value;
  destroy_member_session(session);
  return GNUNET_YES;
}

void
destroy_member (struct GNUNET_MESSENGER_Member *member)
{
  GNUNET_assert((member) && (member->sessions));

  GNUNET_CONTAINER_multihashmap_iterate (member->sessions, iterate_destroy_session, NULL);
  GNUNET_CONTAINER_multihashmap_destroy (member->sessions);

  GNUNET_free (member);
}

const struct GNUNET_ShortHashCode*
get_member_id (const struct GNUNET_MESSENGER_Member *member)
{
  GNUNET_assert (member);

  return &(member->id);
}

static int
callback_scan_for_sessions (void *cls, const char *filename)
{
  struct GNUNET_MESSENGER_Member *member = cls;

  if (GNUNET_YES == GNUNET_DISK_directory_test (filename, GNUNET_YES))
  {
    char *directory;

    GNUNET_asprintf (&directory, "%s%c", filename, DIR_SEPARATOR);

    load_member_session(member, directory);
  }

  return GNUNET_OK;
}

void
load_member (struct GNUNET_MESSENGER_MemberStore *store, const char *directory)
{
  GNUNET_assert ((store) && (directory));

  char *config_file;
  GNUNET_asprintf (&config_file, "%s%s", directory, "member.cfg");

  struct GNUNET_MESSENGER_Member *member = NULL;

  if (GNUNET_YES != GNUNET_DISK_file_test (config_file))
    goto free_config;

  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  if (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, config_file))
  {
    struct GNUNET_ShortHashCode id;

    if (GNUNET_OK != GNUNET_CONFIGURATION_get_data (cfg, "member", "id", &id, sizeof(id)))
      goto destroy_config;

    member = add_store_member(store, &id);
  }

destroy_config:

  GNUNET_CONFIGURATION_destroy (cfg);

free_config:
  GNUNET_free(config_file);

  if (!member)
    return;

  char *scan_dir;
  GNUNET_asprintf (&scan_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);

  if (GNUNET_OK == GNUNET_DISK_directory_test (scan_dir, GNUNET_YES))
    GNUNET_DISK_directory_scan (scan_dir, callback_scan_for_sessions, member);

  GNUNET_free(scan_dir);
}

static int
iterate_load_next_session (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  const char* sessions_directory = cls;

  char* load_dir;
  GNUNET_asprintf (&load_dir, "%s%s%c", sessions_directory, GNUNET_h2s(key), DIR_SEPARATOR);

  struct GNUNET_MESSENGER_MemberSession *session = value;

  if (GNUNET_YES == GNUNET_DISK_directory_test (load_dir, GNUNET_YES))
    load_member_session_next (session, load_dir);

  GNUNET_free (load_dir);
  return GNUNET_YES;
}

void
load_member_next_sessions (const struct GNUNET_MESSENGER_Member *member, const char *directory)
{
  GNUNET_assert ((member) && (directory));

  char* load_dir;
  GNUNET_asprintf (&load_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);

  GNUNET_CONTAINER_multihashmap_iterate (member->sessions, iterate_load_next_session, load_dir);

  GNUNET_free(load_dir);
}

static int
iterate_save_session (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  const char* sessions_directory = cls;

  char* save_dir;
  GNUNET_asprintf (&save_dir, "%s%s%c", sessions_directory, GNUNET_h2s(key), DIR_SEPARATOR);

  struct GNUNET_MESSENGER_MemberSession *session = value;

  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
      (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
    save_member_session (session, save_dir);

  GNUNET_free (save_dir);
  return GNUNET_YES;
}

void
save_member (struct GNUNET_MESSENGER_Member *member, const char *directory)
{
  GNUNET_assert ((member) && (directory));

  char *config_file;
  GNUNET_asprintf (&config_file, "%s%s", directory, "member.cfg");

  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  char *id_data = GNUNET_STRINGS_data_to_string_alloc (&(member->id), sizeof(member->id));

  if (id_data)
  {
    GNUNET_CONFIGURATION_set_value_string (cfg, "member", "id", id_data);

    GNUNET_free(id_data);
  }

  GNUNET_CONFIGURATION_write (cfg, config_file);
  GNUNET_CONFIGURATION_destroy (cfg);

  GNUNET_free(config_file);

  char* save_dir;
  GNUNET_asprintf (&save_dir, "%s%s%c", directory, "sessions", DIR_SEPARATOR);

  if ((GNUNET_YES == GNUNET_DISK_directory_test (save_dir, GNUNET_NO)) ||
      (GNUNET_OK == GNUNET_DISK_directory_create (save_dir)))
    GNUNET_CONTAINER_multihashmap_iterate (member->sessions, iterate_save_session, save_dir);

  GNUNET_free(save_dir);
}

static void
sync_session_contact_from_next (struct GNUNET_MESSENGER_MemberSession *session, struct GNUNET_MESSENGER_MemberSession *next)
{
  GNUNET_assert((session) && (next));

  if (session == next)
    return;

  if (next->next)
    sync_session_contact_from_next (session, next->next);
  else
    session->contact = next->contact;
}

static int
iterate_sync_session_contact (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_MemberSession *session = value;

  if (session->next)
    sync_session_contact_from_next (session, session->next);

  return GNUNET_YES;
}

void
sync_member_contacts (struct GNUNET_MESSENGER_Member *member)
{
  GNUNET_assert ((member) && (member->sessions));

  GNUNET_CONTAINER_multihashmap_iterate (member->sessions, iterate_sync_session_contact, NULL);
}

struct GNUNET_MESSENGER_MemberSession*
get_member_session (const struct GNUNET_MESSENGER_Member *member, const struct GNUNET_IDENTITY_PublicKey *public_key)
{
  GNUNET_assert ((member) && (public_key));

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);

  return GNUNET_CONTAINER_multihashmap_get(member->sessions, &hash);
}

struct GNUNET_MESSENGER_ClosureSearchSession {
  const struct GNUNET_MESSENGER_Message *message;
  const struct GNUNET_HashCode *hash;

  struct GNUNET_MESSENGER_MemberSession *match;
};

static int
iterate_search_session (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_ClosureSearchSession* search = cls;
  struct GNUNET_MESSENGER_MemberSession *session = value;

  if (GNUNET_OK != verify_member_session_as_sender(session, search->message, search->hash))
    return GNUNET_YES;

  search->match = session;
  return GNUNET_NO;
}

static struct GNUNET_MESSENGER_MemberSession*
try_member_session (struct GNUNET_MESSENGER_Member *member, const struct GNUNET_IDENTITY_PublicKey *public_key)
{
  struct GNUNET_MESSENGER_MemberSession* session = get_member_session(member, public_key);

  if (session)
    return session;

  session = create_member_session(member, public_key);

  if (session)
    add_member_session(member, session);

  return session;
}

struct GNUNET_MESSENGER_MemberSession*
get_member_session_of (struct GNUNET_MESSENGER_Member *member, const struct GNUNET_MESSENGER_Message *message,
                       const struct GNUNET_HashCode *hash)
{
  GNUNET_assert ((member) && (message) && (hash) &&
                 (0 == GNUNET_memcmp(&(member->id), &(message->header.sender_id))));

  if (GNUNET_MESSENGER_KIND_INFO == message->header.kind)
    return try_member_session(member, &(message->body.info.host_key));
  else if (GNUNET_MESSENGER_KIND_JOIN == message->header.kind)
    return try_member_session(member, &(message->body.join.key));

  struct GNUNET_MESSENGER_ClosureSearchSession search;

  search.message = message;
  search.hash = hash;

  search.match = NULL;
  GNUNET_CONTAINER_multihashmap_iterate(member->sessions, iterate_search_session, &search);

  return search.match;
}

void
add_member_session (struct GNUNET_MESSENGER_Member *member, struct GNUNET_MESSENGER_MemberSession *session)
{
  if (!session)
    return;

  GNUNET_assert((member) && (session->member == member));

  const struct GNUNET_IDENTITY_PublicKey *public_key = get_member_session_public_key(session);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put(
      member->sessions, &hash, session,
      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Adding a member session failed: %s\n",
               GNUNET_h2s(&hash));
}

void
remove_member_session (struct GNUNET_MESSENGER_Member *member, struct GNUNET_MESSENGER_MemberSession *session)
{
  GNUNET_assert ((member) && (session) && (session->member == member));

  const struct GNUNET_IDENTITY_PublicKey *public_key = get_member_session_public_key(session);

  struct GNUNET_HashCode hash;
  GNUNET_CRYPTO_hash(public_key, sizeof(*public_key), &hash);

  if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove(member->sessions, &hash, session))
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Removing a member session failed: %s\n",
               GNUNET_h2s(&hash));
}

struct GNUNET_MESSENGER_ClosureIterateSessions {
  GNUNET_MESSENGER_MemberIteratorCallback it;
  void *cls;
};

static int
iterate_member_sessions_it (void *cls, const struct GNUNET_HashCode *key, void *value)
{
  struct GNUNET_MESSENGER_ClosureIterateSessions *iterate = cls;
  struct GNUNET_MESSENGER_MemberSession *session = value;

  return iterate->it (iterate->cls, get_member_session_public_key(session), session);
}

int
iterate_member_sessions (struct GNUNET_MESSENGER_Member *member, GNUNET_MESSENGER_MemberIteratorCallback it, void *cls)
{
  GNUNET_assert ((member) && (member->sessions) && (it));

  struct GNUNET_MESSENGER_ClosureIterateSessions iterate;

  iterate.it = it;
  iterate.cls = cls;

  return GNUNET_CONTAINER_multihashmap_iterate(member->sessions, iterate_member_sessions_it, &iterate);
}