summaryrefslogtreecommitdiff
path: root/src/messenger/gnunet-service-messenger_handle.c
blob: 2095f8b2930b43d0e658ae8bdff5062bb544871f (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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
   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_handle.c
 * @brief GNUnet MESSENGER service
 */

#include "gnunet-service-messenger_handle.h"

#include "gnunet-service-messenger.h"
#include "gnunet-service-messenger_message_kind.h"

#include "messenger_api_util.h"

struct GNUNET_MESSENGER_SrvHandle*
create_handle (struct GNUNET_MESSENGER_Service *service,
               struct GNUNET_MQ_Handle *mq)
{
  GNUNET_assert((service) && (mq));

  struct GNUNET_MESSENGER_SrvHandle *handle = GNUNET_new(struct GNUNET_MESSENGER_SrvHandle);

  handle->service = service;
  handle->mq = mq;

  handle->name = NULL;
  handle->ego = NULL;

  handle->member_ids = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);

  return handle;
}

int
iterate_free_member_ids (void *cls,
                         const struct GNUNET_HashCode *key,
                         void *value)
{
  GNUNET_free(value);

  return GNUNET_YES;
}

void
destroy_handle (struct GNUNET_MESSENGER_SrvHandle *handle)
{
  GNUNET_assert(handle);

  if (handle->service->dir)
    save_handle_configuration (handle);

  if (handle->name)
    GNUNET_free(handle->name);

  GNUNET_CONTAINER_multihashmap_iterate (handle->member_ids, iterate_free_member_ids, NULL);
  GNUNET_CONTAINER_multihashmap_destroy (handle->member_ids);

  GNUNET_free(handle);
}

void
get_handle_data_subdir (const struct GNUNET_MESSENGER_SrvHandle *handle,
                        const char *name,
                        char **dir)
{
  GNUNET_assert((handle) && (dir));

  if (name)
    GNUNET_asprintf (dir, "%s%s%c%s%c", handle->service->dir, "identities",
                     DIR_SEPARATOR, name, DIR_SEPARATOR);
  else
    GNUNET_asprintf (dir, "%s%s%c", handle->service->dir, "anonymous",
                     DIR_SEPARATOR);
}

static int
create_handle_member_id (const struct GNUNET_MESSENGER_SrvHandle *handle,
                         const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (key));

  struct GNUNET_ShortHashCode *random_id = GNUNET_new(struct GNUNET_ShortHashCode);

  if (!random_id)
    return GNUNET_NO;

  generate_free_member_id (random_id, NULL);

  if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (handle->member_ids, key, random_id,
                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
  {
    GNUNET_free(random_id);
    return GNUNET_NO;
  }

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Created a new member id (%s) for room: %s\n", GNUNET_sh2s (random_id),
             GNUNET_h2s (key));

  return GNUNET_YES;
}

const struct GNUNET_ShortHashCode*
get_handle_member_id (const struct GNUNET_MESSENGER_SrvHandle *handle,
                      const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (key));

  return GNUNET_CONTAINER_multihashmap_get (handle->member_ids, key);
}

int
change_handle_member_id (struct GNUNET_MESSENGER_SrvHandle *handle,
                         const struct GNUNET_HashCode *key,
                         const struct GNUNET_ShortHashCode *unique_id)
{
  GNUNET_assert((handle) && (key) && (unique_id));

  struct GNUNET_ShortHashCode *member_id = GNUNET_CONTAINER_multihashmap_get (handle->member_ids, key);

  if (!member_id)
  {
    member_id = GNUNET_new(struct GNUNET_ShortHashCode);
    GNUNET_memcpy(member_id, unique_id, sizeof(*member_id));

    if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put (handle->member_ids, key, member_id,
                                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
    {
      GNUNET_free(member_id);
      return GNUNET_SYSERR;
    }
  }

  if (0 == GNUNET_memcmp(unique_id, member_id))
    goto send_message_to_client;

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Change a member id (%s) for room (%s).\n", GNUNET_sh2s (member_id),
             GNUNET_h2s (key));

  GNUNET_memcpy(member_id, unique_id, sizeof(*unique_id));

  GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Member id changed to (%s).\n", GNUNET_sh2s (unique_id));

  struct GNUNET_MESSENGER_MemberMessage *msg;
  struct GNUNET_MQ_Envelope *env;

send_message_to_client:

  env = GNUNET_MQ_msg(msg, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_MEMBER_ID);

  GNUNET_memcpy(&(msg->key), key, sizeof(*key));
  GNUNET_memcpy(&(msg->id), member_id, sizeof(*member_id));

  GNUNET_MQ_send (handle->mq, env);
  return GNUNET_OK;
}

static void
change_handle_name (struct GNUNET_MESSENGER_SrvHandle *handle,
                    const char *name)
{
  GNUNET_assert(handle);

  if (handle->name)
    GNUNET_free(handle->name);

  handle->name = name ? GNUNET_strdup(name) : NULL;

  const uint16_t name_len = handle->name ? strlen (handle->name) : 0;

  struct GNUNET_MESSENGER_NameMessage *msg;
  struct GNUNET_MQ_Envelope *env;

  env = GNUNET_MQ_msg_extra(msg, name_len + 1, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_GET_NAME);

  char *extra = ((char*) msg) + sizeof(*msg);

  if (name_len)
    GNUNET_memcpy(extra, handle->name, name_len);

  extra[name_len] = '\0';

  GNUNET_MQ_send (handle->mq, env);
}

static void
change_handle_ego (struct GNUNET_MESSENGER_SrvHandle *handle,
                   const struct GNUNET_MESSENGER_Ego *ego)
{
  GNUNET_assert(handle);

  handle->ego = ego;

  ego = get_handle_ego (handle);

  const uint16_t length = GNUNET_IDENTITY_key_get_length(&(ego->pub));

  struct GNUNET_MESSENGER_KeyMessage *msg;
  struct GNUNET_MQ_Envelope *env;

  env = GNUNET_MQ_msg_extra(msg, length, GNUNET_MESSAGE_TYPE_MESSENGER_CONNECTION_GET_KEY);

  char *extra = ((char*) msg) + sizeof(*msg);

  if (GNUNET_IDENTITY_write_key_to_buffer(&(ego->pub), extra, length) < 0)
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Could not write key to buffer.\n");

  GNUNET_MQ_send (handle->mq, env);
}

struct GNUNET_MESSENGER_MessageHandle
{
  struct GNUNET_MESSENGER_SrvHandle *handle;
  struct GNUNET_MESSENGER_Message *message;
};

static int
iterate_send_message (void *cls,
                      const struct GNUNET_HashCode *key,
                      void *value)
{
  struct GNUNET_MESSENGER_MessageHandle *msg_handle = cls;

  send_handle_message (msg_handle->handle, key, msg_handle->message);

  return GNUNET_YES;
}

void
set_handle_ego (struct GNUNET_MESSENGER_SrvHandle *handle,
                const struct GNUNET_MESSENGER_Ego *ego)
{
  GNUNET_assert((handle) && (ego));

  struct GNUNET_MESSENGER_MessageHandle msg_handle;

  msg_handle.handle = handle;
  msg_handle.message = create_message_key (&(ego->priv));

  GNUNET_CONTAINER_multihashmap_iterate (handle->member_ids, iterate_send_message, &msg_handle);

  destroy_message (msg_handle.message);

  change_handle_ego (handle, ego);
}

const struct GNUNET_MESSENGER_Ego*
get_handle_ego (const struct GNUNET_MESSENGER_SrvHandle *handle)
{
  GNUNET_assert(handle);

  static struct GNUNET_MESSENGER_Ego anonymous;
  static int read_keys = 0;

  if (handle->ego)
    return handle->ego;

  if (!read_keys)
  {
    struct GNUNET_IDENTITY_Ego *ego = GNUNET_IDENTITY_ego_get_anonymous ();
    GNUNET_memcpy(&(anonymous.priv), GNUNET_IDENTITY_ego_get_private_key (ego), sizeof(anonymous.priv));
    GNUNET_IDENTITY_ego_get_public_key (ego, &(anonymous.pub));
    read_keys = 1;
  }

  return &anonymous;
}

static void
callback_setup_handle_name (void *cls,
                            const char *name,
                            const struct GNUNET_MESSENGER_Ego *ego)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Setting up handle...\n");

  change_handle_name (handle, name);
  change_handle_ego (handle, ego);

  if (handle->service->dir)
    load_handle_configuration (handle);
}

void
setup_handle_name (struct GNUNET_MESSENGER_SrvHandle *handle,
                   const char *name)
{
  GNUNET_assert(handle);

  struct GNUNET_MESSENGER_EgoStore *store = get_service_ego_store(handle->service);

  lookup_store_ego (store, name, callback_setup_handle_name, handle);
}

static void
callback_update_handle (void *cls,
                        const char *name,
                        const struct GNUNET_MESSENGER_Ego *ego)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Updating handle...\n");

  struct GNUNET_MESSENGER_EgoStore *store = get_service_ego_store(handle->service);

  if (!ego)
    create_store_ego(store, handle->name, handle);
  else
    change_handle_ego (handle, ego);
}

void
update_handle (struct GNUNET_MESSENGER_SrvHandle *handle)
{
  GNUNET_assert(handle);

  if (!handle->name)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Updating handle failed: Name is required!\n");
    return;
  }

  struct GNUNET_MESSENGER_EgoStore *store = get_service_ego_store(handle->service);

  lookup_store_ego (store, handle->name, callback_update_handle, handle);
}

static void
callback_set_handle_name (void *cls,
                          const char *name,
                          const struct GNUNET_MESSENGER_Ego *ego)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = cls;

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Renaming handle...\n");

  if (ego)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Renaming handle failed: Name is occupied! (%s)\n", name);
    return;
  }

  struct GNUNET_MESSENGER_EgoStore *store = get_service_ego_store(handle->service);

  int rename_ego_in_store = handle->ego? GNUNET_YES : GNUNET_NO;

  char *old_dir;
  get_handle_data_subdir (handle, handle->name, &old_dir);

  char *new_dir;
  get_handle_data_subdir (handle, name, &new_dir);

  int result = 0;

  if (GNUNET_YES == GNUNET_DISK_directory_test (old_dir, GNUNET_YES))
  {
    GNUNET_DISK_directory_create_for_file (new_dir);

    result = rename (old_dir, new_dir);
  }
  else if (GNUNET_YES == GNUNET_DISK_directory_test (new_dir, GNUNET_NO))
    result = -1;

  if (0 == result)
  {
    struct GNUNET_MESSENGER_MessageHandle msg_handle;

    msg_handle.handle = handle;
    msg_handle.message = create_message_name (name);

    GNUNET_CONTAINER_multihashmap_iterate (handle->member_ids, iterate_send_message, &msg_handle);

    destroy_message (msg_handle.message);

    change_handle_name (handle, name);
  }
  else
    rename_ego_in_store = GNUNET_NO;

  GNUNET_free(old_dir);
  GNUNET_free(new_dir);

  if (GNUNET_YES == rename_ego_in_store)
    rename_store_ego(store, handle->name, name);
}

void
set_handle_name (struct GNUNET_MESSENGER_SrvHandle *handle,
                 const char *name)
{
  GNUNET_assert(handle);

  if (!name)
  {
    if (handle->ego)
      GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Renaming handle failed: Name is required!\n");
    else
      change_handle_name (handle, name);

    return;
  }

  struct GNUNET_MESSENGER_EgoStore *store = get_service_ego_store(handle->service);

  lookup_store_ego (store, name, callback_set_handle_name, handle);
}

int
open_handle_room (struct GNUNET_MESSENGER_SrvHandle *handle,
                  const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (key));

  if ((!get_handle_member_id (handle, key)) && (GNUNET_YES != create_handle_member_id (handle, key)))
    return GNUNET_NO;

  return open_service_room (handle->service, handle, key);
}

int
entry_handle_room (struct GNUNET_MESSENGER_SrvHandle *handle,
                   const struct GNUNET_PeerIdentity *door,
                   const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (door) && (key));

  if ((!get_handle_member_id (handle, key)) && (GNUNET_YES != create_handle_member_id (handle, key)))
    return GNUNET_NO;

  return entry_service_room (handle->service, handle, door, key);
}

int
close_handle_room (struct GNUNET_MESSENGER_SrvHandle *handle,
                   const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (key));

  if (!get_handle_member_id (handle, key))
    return GNUNET_NO;

  return close_service_room (handle->service, handle, key);
}

int
send_handle_message (struct GNUNET_MESSENGER_SrvHandle *handle,
                     const struct GNUNET_HashCode *key,
                     const struct GNUNET_MESSENGER_Message *message)
{
  GNUNET_assert((handle) && (key) && (message));

  const struct GNUNET_ShortHashCode *id = get_handle_member_id (handle, key);

  if (!id)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "It is required to be a member of a room to send messages!\n");
    return GNUNET_NO;
  }

  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room (handle->service, key);

  if (!room)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "The room (%s) is unknown!\n", GNUNET_h2s (key));
    return GNUNET_NO;
  }

  struct GNUNET_MESSENGER_Message *msg = copy_message(message);

  GNUNET_memcpy(&(msg->header.sender_id), id, sizeof(*id));

  return send_room_message (room, handle, msg);
}

static const struct GNUNET_HashCode*
get_next_member_session_contect(const struct GNUNET_MESSENGER_MemberSession *session)
{
  if (session->next)
    return get_next_member_session_contect (session->next);
  else
    return get_member_session_context(session);
}

static const struct GNUNET_MESSENGER_MemberSession*
get_handle_member_session (struct GNUNET_MESSENGER_SrvHandle *handle,
                           const struct GNUNET_HashCode *key)
{
  GNUNET_assert((handle) && (key) && (handle->service));

  const struct GNUNET_ShortHashCode *id = get_handle_member_id(handle, key);
  struct GNUNET_MESSENGER_SrvRoom *room = get_service_room(handle->service, key);

  if ((!id) || (!room))
    return NULL;

  struct GNUNET_MESSENGER_MemberStore *store = get_room_member_store(room);
  struct GNUNET_MESSENGER_Member *member = get_store_member(store, id);

  const struct GNUNET_MESSENGER_Ego *ego = get_handle_ego(handle);

  if (!ego)
    return NULL;

  return get_member_session(member, &(ego->pub));
}

void
notify_handle_message (struct GNUNET_MESSENGER_SrvHandle *handle,
                       const struct GNUNET_HashCode *key,
                       const struct GNUNET_MESSENGER_MemberSession *session,
                       const struct GNUNET_MESSENGER_Message *message,
                       const struct GNUNET_HashCode *hash)
{
  GNUNET_assert((handle) && (key) && (session) && (message) && (hash));

  if ((!handle->mq) || (!get_handle_member_id (handle, key)))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Notifying client about message requires membership!\n");
    return;
  }

  const struct GNUNET_IDENTITY_PublicKey *pubkey = get_contact_key(session->contact);

  struct GNUNET_HashCode sender;
  GNUNET_CRYPTO_hash(pubkey, sizeof(*pubkey), &sender);

  const struct GNUNET_HashCode *context = get_next_member_session_contect (session);

  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Notifying client about message: %s\n", GNUNET_h2s (hash));

  struct GNUNET_MESSENGER_Message *private_message = NULL;

  if (GNUNET_MESSENGER_KIND_PRIVATE == message->header.kind)
  {
    private_message = copy_message(message);

    if (GNUNET_YES != decrypt_message(private_message, &(get_handle_ego(handle)->priv)))
    {
      destroy_message(private_message);
      private_message = NULL;
    }
    else
      message = private_message;
  }

  struct GNUNET_MESSENGER_RecvMessage *msg;
  struct GNUNET_MQ_Envelope *env;

  uint16_t length = get_message_size (message, GNUNET_YES);

  env = GNUNET_MQ_msg_extra(msg, length, GNUNET_MESSAGE_TYPE_MESSENGER_ROOM_RECV_MESSAGE);

  GNUNET_memcpy(&(msg->key), key, sizeof(msg->key));
  GNUNET_memcpy(&(msg->sender), &sender, sizeof(msg->sender));
  GNUNET_memcpy(&(msg->context), context, sizeof(msg->context));
  GNUNET_memcpy(&(msg->hash), hash, sizeof(msg->hash));

  msg->flags = (uint32_t) GNUNET_MESSENGER_FLAG_NONE;

  if (get_handle_member_session(handle, key) == session)
    msg->flags |= (uint32_t) GNUNET_MESSENGER_FLAG_SENT;

  if (private_message)
    msg->flags |= (uint32_t) GNUNET_MESSENGER_FLAG_PRIVATE;

  char *buffer = ((char*) msg) + sizeof(*msg);
  encode_message (message, length, buffer, GNUNET_YES);

  if (private_message)
    destroy_message(private_message);

  GNUNET_MQ_send (handle->mq, env);
}

static int
callback_scan_for_rooms (void *cls,
                         const char *filename)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = cls;

  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  if ((GNUNET_YES == GNUNET_DISK_file_test (filename)) && (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))
  {
    struct GNUNET_HashCode key;
    struct GNUNET_ShortHashCode member_id;

    if ((GNUNET_OK == GNUNET_CONFIGURATION_get_data (cfg, "room", "key", &key, sizeof(key))) &&
        (GNUNET_OK == GNUNET_CONFIGURATION_get_data (cfg, "room", "member_id", &member_id, sizeof(member_id))))
      change_handle_member_id (handle, &key, &member_id);
  }

  GNUNET_CONFIGURATION_destroy (cfg);
  return GNUNET_OK;
}

void
load_handle_configuration (struct GNUNET_MESSENGER_SrvHandle *handle)
{
  GNUNET_assert(handle);

  char *id_dir;
  get_handle_data_subdir (handle, handle->name, &id_dir);

  if (GNUNET_YES == GNUNET_DISK_directory_test (id_dir, GNUNET_YES))
  {
    char *scan_dir;
    GNUNET_asprintf (&scan_dir, "%s%s%c", id_dir, "rooms", DIR_SEPARATOR);

    if (GNUNET_OK == GNUNET_DISK_directory_test (scan_dir, GNUNET_YES))
      GNUNET_DISK_directory_scan (scan_dir, callback_scan_for_rooms, handle);

    GNUNET_free(scan_dir);
  }

  GNUNET_free(id_dir);
}

static int
iterate_save_rooms (void *cls,
                    const struct GNUNET_HashCode *key,
                    void *value)
{
  struct GNUNET_MESSENGER_SrvHandle *handle = cls;
  struct GNUNET_ShortHashCode *member_id = value;

  char *id_dir;
  get_handle_data_subdir (handle, handle->name, &id_dir);

  char *filename;
  GNUNET_asprintf (&filename, "%s%s%c%s.cfg", id_dir, "rooms", DIR_SEPARATOR, GNUNET_h2s (key));

  GNUNET_free(id_dir);

  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  char *key_data = GNUNET_STRINGS_data_to_string_alloc (key, sizeof(*key));

  if (key_data)
  {
    GNUNET_CONFIGURATION_set_value_string (cfg, "room", "key", key_data);

    GNUNET_free(key_data);
  }

  char *member_id_data = GNUNET_STRINGS_data_to_string_alloc (member_id, sizeof(*member_id));

  if (member_id_data)
  {
    GNUNET_CONFIGURATION_set_value_string (cfg, "room", "member_id", member_id_data);

    GNUNET_free(member_id_data);
  }

  GNUNET_CONFIGURATION_write (cfg, filename);
  GNUNET_CONFIGURATION_destroy (cfg);

  GNUNET_free(filename);

  return GNUNET_YES;
}

void
save_handle_configuration (struct GNUNET_MESSENGER_SrvHandle *handle)
{
  GNUNET_assert(handle);

  char *id_dir;
  get_handle_data_subdir (handle, handle->name, &id_dir);

  if ((GNUNET_YES == GNUNET_DISK_directory_test (id_dir, GNUNET_NO)) || (GNUNET_OK
      == GNUNET_DISK_directory_create (id_dir)))
  {
    char *save_dir;
    GNUNET_asprintf (&save_dir, "%s%s%c", id_dir, "rooms", 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 (handle->member_ids, iterate_save_rooms, handle);

    GNUNET_free(save_dir);
  }

  GNUNET_free(id_dir);
}