summaryrefslogtreecommitdiff
path: root/src/experimentation/gnunet-daemon-experimentation_nodes.c
blob: 65b2d9c38d0a854fb39ef445d144f8c3b212c553 (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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
/*
     This file is part of GNUnet.
     (C) 2012-2013 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file experimentation/gnunet-daemon-experimentation_nodes.c
 * @brief experimentation daemon: node management
 * @author Christian Grothoff
 * @author Matthias Wachs
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_core_service.h"
#include "gnunet_statistics_service.h"
#include "gnunet-daemon-experimentation.h"


#define FAST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)

/**
 * Core handle
 */
static struct GNUNET_CORE_Handle *ch;

/**
 * Peer's own identity
 */
static struct GNUNET_PeerIdentity me;

/**
 * Nodes with a pending request
 */
static struct GNUNET_CONTAINER_MultiPeerMap *nodes_requested;

/**
 * Active experimentation nodes
 */
static struct GNUNET_CONTAINER_MultiPeerMap *nodes_active;

/**
 * Inactive experimentation nodes
 * To be excluded from future requests
 */
static struct GNUNET_CONTAINER_MultiPeerMap *nodes_inactive;


struct NodeComCtx
{
  struct NodeComCtx *prev;
  struct NodeComCtx *next;

  struct Node *n;
  struct Experiment *e;

  size_t size;
  GNUNET_CONNECTION_TransmitReadyNotify notify;
  void *notify_cls;
};


/**
 * Update statistics
 *
 * @param m peermap to update values from
 */
static void
update_stats (struct GNUNET_CONTAINER_MultiPeerMap *m)
{
  GNUNET_assert (NULL != m);
  GNUNET_assert (NULL != GED_stats);

  if (m == nodes_active)
  {
    GNUNET_STATISTICS_set (GED_stats, "# nodes active",
			   GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  }
  else if (m == nodes_inactive)
  {
    GNUNET_STATISTICS_set (GED_stats, "# nodes inactive",
			   GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  }
  else if (m == nodes_requested)
  {
    GNUNET_STATISTICS_set (GED_stats, "# nodes requested",
			   GNUNET_CONTAINER_multipeermap_size(m), GNUNET_NO);
  }
  else
    GNUNET_break (0);
}


/**
 * Clean up node
 *
 * @param cls the peermap to clean up
 * @param key key of the current node
 * @param value related node object
 * @return always #GNUNET_OK
 */
static int
cleanup_node (void *cls,
	      const struct GNUNET_PeerIdentity * key,
	      void *value)
{
  struct Node *n;
  struct NodeComCtx *e_cur;
  struct NodeComCtx *e_next;
  struct GNUNET_CONTAINER_MultiPeerMap *cur = cls;

  n = value;
  if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
  {
    GNUNET_SCHEDULER_cancel (n->timeout_task);
    n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  }

  if (NULL != n->cth)
  {
    GNUNET_CORE_notify_transmit_ready_cancel (n->cth);
    n->cth = NULL;
  }
  e_next = n->e_req_head;
  while (NULL != (e_cur = e_next))
  {
    e_next = e_cur->next;
    GNUNET_CONTAINER_DLL_remove (n->e_req_head, n->e_req_tail, e_cur);
    GNUNET_free (e_cur);
  }
  GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (cur, key, value));
  GNUNET_free (value);
  return GNUNET_OK;
}


/**
 * Check if id passed is my id
 *
 * @param id the id to check
 * @return GNUNET_YES or GNUNET_NO
 */
static int
is_me (const struct GNUNET_PeerIdentity *id)
{
  if (0 == memcmp (&me, id, sizeof (me)))
    return GNUNET_YES;
  else
    return GNUNET_NO;
}


/**
 * Core startup callback
 *
 * @param cls unused
 * @param my_identity my id
 */
static void
core_startup_handler (void *cls,
                      const struct GNUNET_PeerIdentity *my_identity)
{
  me = *my_identity;
}


static void
schedule_transmisson (struct NodeComCtx *e_ctx);


static size_t
transmit_read_wrapper (void *cls, size_t bufsize, void *buf)
{
  struct NodeComCtx *e_ctx = cls;
  struct NodeComCtx *next;

  size_t res = e_ctx->notify (e_ctx->notify_cls, bufsize, buf);
  e_ctx->n->cth = NULL;

  GNUNET_CONTAINER_DLL_remove (e_ctx->n->e_req_head, e_ctx->n->e_req_tail, e_ctx);
  next = e_ctx->n->e_req_head;
  GNUNET_free (e_ctx);

  if (NULL != next)
  {
    /* Schedule next message */
    schedule_transmisson (next);
  }
  return res;
}


static void
schedule_transmisson (struct NodeComCtx *e_ctx)
{
  if (NULL != e_ctx->n->cth)
    return;

  e_ctx->n->cth = GNUNET_CORE_notify_transmit_ready (ch, GNUNET_NO, 0, FAST_TIMEOUT,
						     &e_ctx->n->id, e_ctx->size,
						     transmit_read_wrapper, e_ctx);
  if (NULL == e_ctx->n->cth)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		_("Cannot send message to peer `%s' for experiment `%s'\n"),
		GNUNET_i2s(&e_ctx->n->id), e_ctx->e->name);
    GNUNET_free (e_ctx);
  }
}


/**
 * Remove experimentation request due to timeout
 *
 * @param cls the related node
 * @param tc scheduler's task context
 */
static void
remove_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct Node *n = cls;

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Removing request for peer %s due to timeout\n",
	      GNUNET_i2s (&n->id));
  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_requested, &n->id))
  {
    GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_requested, &n->id, n));
    update_stats (nodes_requested);
    GNUNET_CONTAINER_multipeermap_put (nodes_inactive, &n->id, n,
				       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
    update_stats (nodes_inactive);
  }
  n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
}


static int
append_public_key (void *cls,
		   const struct GNUNET_HashCode *key,
		   void *value)
{
  struct GNUNET_CRYPTO_EddsaPublicKey **issuers = cls;
  struct Issuer *issuer = value;

  *issuers[0] = issuer->pubkey;
  *issuers = &((*issuers)[1]);
  return GNUNET_OK;
}


/**
 * Core's transmit notify callback to send request
 *
 * @param cls the related node
 * @param bufsize buffer size
 * @param buf the buffer to copy to
 * @return bytes passed
 */
static size_t
send_experimentation_request_cb (void *cls, size_t bufsize, void *buf)
{
  struct Node *n = cls;
  struct Experimentation_Request msg;
  unsigned int my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  size_t msg_size = sizeof (msg);
  size_t ri_size = sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) * my_issuer_count;
  size_t total_size = msg_size + ri_size;
  struct GNUNET_CRYPTO_EddsaPublicKey *issuers;
	
  n->cth = NULL;
  if (NULL == buf)
  {
    /* client disconnected */
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Client disconnected\n");
    if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
    		GNUNET_SCHEDULER_cancel (n->timeout_task);
    GNUNET_SCHEDULER_add_now (&remove_request, n);
    return 0;
  }
  GNUNET_assert (bufsize >= total_size);
  msg.msg.size = htons (total_size);
  msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST);
  msg.capabilities = htonl (GSE_node_capabilities);
  msg.issuer_count = htonl (my_issuer_count);
  memcpy (buf, &msg, msg_size);
  issuers = (struct GNUNET_CRYPTO_EddsaPublicKey *) buf + msg_size;
  GNUNET_CONTAINER_multihashmap_iterate (valid_issuers,
					 &append_public_key,
					 &issuers);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _("Sending experimentation request to peer %s\n"),
	      GNUNET_i2s (&n->id));
  return total_size;
}


/**
 * Send request to peer to start add him to to the set of experimentation nodes
 *
 * @param peer the peer to send to
 */
static void
send_experimentation_request (const struct GNUNET_PeerIdentity *peer)
{
  struct Node *n;
  struct NodeComCtx *e_ctx;
  size_t size;
  size_t c_issuers;

  c_issuers = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  size = sizeof (struct Experimentation_Request) +
    c_issuers * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  n = GNUNET_new (struct Node);
  n->id = *peer;
  n->timeout_task = GNUNET_SCHEDULER_add_delayed (EXP_RESPONSE_TIMEOUT, &remove_request, n);
  n->capabilities = NONE;

  e_ctx = GNUNET_new (struct NodeComCtx);
  e_ctx->n = n;
  e_ctx->e = NULL;
  e_ctx->size = size;
  e_ctx->notify = &send_experimentation_request_cb;
  e_ctx->notify_cls = n;
  GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
  schedule_transmisson (e_ctx);

  GNUNET_assert (GNUNET_OK ==
		 GNUNET_CONTAINER_multipeermap_put (nodes_requested,
						    peer, n,
						    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
  update_stats (nodes_requested);
}


/**
 * Core's transmit notify callback to send response
 *
 * @param cls the related node
 * @param bufsize buffer size
 * @param buf the buffer to copy to
 * @return bytes passed
 */
static size_t
send_response_cb (void *cls, size_t bufsize, void *buf)
{
  struct Node *n = cls;
  struct Experimentation_Response msg;
  size_t c_issuers = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  size_t ri_size = c_issuers * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  size_t msg_size = sizeof (msg);
  size_t total_size = msg_size + ri_size;
  struct GNUNET_CRYPTO_EddsaPublicKey *issuers;

  n->cth = NULL;
  if (buf == NULL)
  {
    /* client disconnected */
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Client disconnected\n");
    return 0;
  }
  GNUNET_assert (bufsize >= total_size);

  msg.msg.size = htons (total_size);
  msg.msg.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE);
  msg.capabilities = htonl (GSE_node_capabilities);
  msg.issuer_count = htonl (c_issuers);
  memcpy (buf, &msg, msg_size);
  issuers = (struct GNUNET_CRYPTO_EddsaPublicKey *) buf + msg_size;
  GNUNET_CONTAINER_multihashmap_iterate (valid_issuers,
					 &append_public_key,
					 &issuers);
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Sending response to peer %s\n",
	      GNUNET_i2s (&n->id));
  return total_size;
}


static void
get_experiments_cb (struct Node *n, struct Experiment *e)
{
  static int counter = 0;
  if (NULL == e)
    return; /* Done */

  /* Tell the scheduler to add a node with an experiment */
  GED_scheduler_add (n, e, GNUNET_YES);
  counter ++;
}


struct Node *
get_node (const struct GNUNET_PeerIdentity *id)
{
  struct Node * res;
  struct Node * tmp;

  res = NULL;
  tmp = NULL;
  tmp = GNUNET_CONTAINER_multipeermap_get (nodes_active, id);
  if (res == NULL)
    res = tmp;

  tmp = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, id);
  if (res == NULL)
    res = tmp;
  else
    GNUNET_break (0); /* Multiple instances */

  tmp = GNUNET_CONTAINER_multipeermap_get (nodes_requested, id);
  if (res == NULL)
    res = tmp;
  else
    GNUNET_break (0); /* Multiple instances */

  return res;
}


/**
 * Set a specific node as active
 *
 * @param n the node
 */
static void
node_make_active (struct Node *n)
{
  int c1;

  GNUNET_CONTAINER_multipeermap_put (nodes_active,
				     &n->id, n, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  update_stats (nodes_active);
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
	      _("Added peer `%s' as active node\n"),
	      GNUNET_i2s (&n->id));
  /* Request experiments for this node to start them */
  for (c1 = 0; c1 < n->issuer_count; c1++)
  {
    GED_experiments_get (n, &n->issuer_id[c1], &get_experiments_cb);
  }
}


/**
 * Handle a request and send a response
 *
 * @param peer the source
 * @param message the message
 */
static void
handle_request (const struct GNUNET_PeerIdentity *peer,
		const struct GNUNET_MessageHeader *message)
{
  struct Node *n;
  struct NodeComCtx *e_ctx;
  const struct Experimentation_Request *rm = (const struct Experimentation_Request *) message;
  const struct GNUNET_CRYPTO_EddsaPublicKey *rmi = (const struct GNUNET_CRYPTO_EddsaPublicKey *) &rm[1];
  unsigned int my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
  int c1;
  int c2;
  uint32_t ic;
  uint32_t ic_accepted;
  int make_active;

  if (ntohs (message->size) < sizeof (struct Experimentation_Request))
  {
    GNUNET_break (0);
    return;
  }
  ic = ntohl (rm->issuer_count);
  if (ntohs (message->size) !=
      sizeof (struct Experimentation_Request) + ic * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))
  {
    GNUNET_break (0);
    return;
  }

  make_active = GNUNET_NO;
  if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
  {
    /* Nothing to do */
  }
  else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
  {
    GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (nodes_requested, peer, n));
    if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
      {
	GNUNET_SCHEDULER_cancel (n->timeout_task);
	n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
      }
    update_stats (nodes_requested);
    make_active = GNUNET_YES;
  }
  else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
  {
    GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_inactive, peer, n));
    update_stats (nodes_inactive);
    make_active = GNUNET_YES;
  }
  else
  {
    /* Create new node */
    n = GNUNET_new (struct Node);
    n->id = *peer;
    n->capabilities = NONE;
    make_active = GNUNET_YES;
  }

  /* Update node */
  n->capabilities = ntohl (rm->capabilities);

  /* Filter accepted issuer */
  ic_accepted = 0;
  for (c1 = 0; c1 < ic; c1++)
  {
    if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
      ic_accepted ++;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Request from peer `%s' with %u issuers, we accepted %u issuer \n",
	      GNUNET_i2s (peer), ic, ic_accepted);
  GNUNET_free_non_null (n->issuer_id);
  n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey));
  c2 = 0;
  for (c1 = 0; c1 < ic; c1++)
  {
    if (GNUNET_YES == GED_experiments_issuer_accepted (&rmi[c1]))
    {
      n->issuer_id[c2] = rmi[c1];
      c2 ++;
    }
  }
  n->issuer_count = ic_accepted;

  if (GNUNET_YES == make_active)
    node_make_active (n);

  /* Send response */
  e_ctx = GNUNET_new (struct NodeComCtx);
  e_ctx->n = n;
  e_ctx->e = NULL;
  e_ctx->size = sizeof (struct Experimentation_Response) +
    my_issuer_count * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey);
  e_ctx->notify = &send_response_cb;
  e_ctx->notify_cls = n;

  GNUNET_CONTAINER_DLL_insert_tail(n->e_req_head, n->e_req_tail, e_ctx);
  schedule_transmisson (e_ctx);
}


/**
 * Handle a response
 *
 * @param peer the source
 * @param message the message
 */
static void handle_response (const struct GNUNET_PeerIdentity *peer,
			     const struct GNUNET_MessageHeader *message)
{
  struct Node *n;
  const struct Experimentation_Response *rm = (const struct Experimentation_Response *) message;
  const struct GNUNET_CRYPTO_EddsaPublicKey *rmi = (const struct GNUNET_CRYPTO_EddsaPublicKey *) &rm[1];
  uint32_t ic;
  uint32_t ic_accepted;
  int make_active;
  unsigned int c1;
  unsigned int c2;

  if (ntohs (message->size) < sizeof (struct Experimentation_Response))
    {
      GNUNET_break (0);
      return;
    }
  ic = ntohl (rm->issuer_count);
  if (ntohs (message->size) != sizeof (struct Experimentation_Response) + ic * sizeof (struct GNUNET_CRYPTO_EddsaPublicKey))
  {
    GNUNET_break (0);
    return;
  }

  make_active = GNUNET_NO;
  if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Received %s from %s peer `%s'\n",
		"RESPONSE", "active", GNUNET_i2s (peer));
  }
  else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
		"RESPONSE", "requested", GNUNET_i2s (peer));
    GNUNET_assert (GNUNET_OK ==  GNUNET_CONTAINER_multipeermap_remove (nodes_requested, peer, n));
    if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
    {
      GNUNET_SCHEDULER_cancel (n->timeout_task);
      n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
    }
    update_stats (nodes_requested);
    make_active = GNUNET_YES;
  }
  else if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Received %s from peer `%s'\n",
		"RESPONSE", "inactive", GNUNET_i2s (peer));
    GNUNET_break (0 == GNUNET_CONTAINER_multipeermap_remove (nodes_inactive, peer, n));
    update_stats (nodes_inactive);
    make_active = GNUNET_YES;
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received %s from %s peer `%s'\n",
		"RESPONSE", "unknown", GNUNET_i2s (peer));
    return;
  }

  /* Update */
  n->capabilities = ntohl (rm->capabilities);

  /* Filter accepted issuer */
  ic_accepted = 0;
  for (c1 = 0; c1 < ic; c1++)
  {
    if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
      ic_accepted ++;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Response from peer `%s' with %u issuers, we accepted %u issuer \n",
	      GNUNET_i2s (peer), ic, ic_accepted);
  GNUNET_free_non_null (n->issuer_id);
  n->issuer_id = GNUNET_malloc (ic_accepted * sizeof (struct GNUNET_PeerIdentity));
  c2 = 0;
  for (c1 = 0; c1 < ic; c1++)
  {
    if (GNUNET_YES == GED_experiments_issuer_accepted(&rmi[c1]))
    {
      n->issuer_id[c2] = rmi[c1];
      c2 ++;
    }
  }
  n->issuer_count = ic_accepted;

  if (GNUNET_YES == make_active)
    node_make_active (n);
}


/**
 * Handle a response
 *
 * @param peer the source
 * @param message the message
 */
static void
handle_start (const struct GNUNET_PeerIdentity *peer,
	      const struct GNUNET_MessageHeader *message)
{
  uint16_t size;
  uint32_t name_len;
  const struct GED_start_message *msg;
  const char *name;
  struct Node *n;
  struct Experiment *e;

  if (NULL == peer)
  {
    GNUNET_break (0);
    return;
  }
  if (NULL == message)
  {
    GNUNET_break (0);
    return;
  }

  size = ntohs (message->size);
  if (size < sizeof (struct GED_start_message))
  {
    GNUNET_break (0);
    return;
  }
  msg = (const struct GED_start_message *) message;
  name_len = ntohl (msg->len_name);
  if (size != sizeof (struct GED_start_message) + name_len)
  {
    GNUNET_break (0);
    return;
  }

  n = get_node (peer);
  if (NULL == n)
  {
    GNUNET_break (0);
    return;
  }
  name = (const char *) &msg[1];
  if (name[name_len-1] != '\0')
  {
    GNUNET_break (0);
    return;
  }
  if (name_len != strlen (name) + 1)
  {
    GNUNET_break (0);
    return;
  }
  e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
  if (NULL == e)
  {
    GNUNET_break (0);
    return;
  }
  GED_scheduler_handle_start (n, e);
}


/**
 * Handle a response
 *
 * @param peer the source
 * @param message the message
 */
static void
handle_start_ack (const struct GNUNET_PeerIdentity *peer,
		  const struct GNUNET_MessageHeader *message)
{
  uint16_t size;
  uint32_t name_len;
  const struct GED_start_ack_message *msg;
  const char *name;
  struct Node *n;
  struct Experiment *e;

  if (NULL == peer)
  {
    GNUNET_break (0);
    return;
  }
  if (NULL == message)
  {
    GNUNET_break (0);
    return;
  }

  size = ntohs (message->size);
  if (size < sizeof (struct GED_start_ack_message))
  {
    GNUNET_break (0);
    return;
  }
  msg = (const struct GED_start_ack_message *) message;
  name_len = ntohl (msg->len_name);
  if (size != sizeof (struct GED_start_message) + name_len)
  {
    GNUNET_break (0);
    return;
  }

  n = get_node (peer);
  if (NULL == n)
  {
    GNUNET_break (0);
    return;
  }
  name = (const char *) &msg[1];
  if (name[name_len-1] != '\0')
  {
    GNUNET_break (0);
    return;
  }
  if (name_len != strlen (name) + 1)
  {
    GNUNET_break (0);
    return;
  }

  e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
  if (NULL == e)
  {
    GNUNET_break (0);
    return;
  }
  GED_scheduler_handle_start_ack (n, e);
}


/**
 * Handle a response
 *
 * @param peer the source
 * @param message the message
 */
static void
handle_stop (const struct GNUNET_PeerIdentity *peer,
	     const struct GNUNET_MessageHeader *message)
{
	uint16_t size;
	uint32_t name_len;
	const struct GED_stop_message *msg;
	const char *name;
	struct Node *n;
	struct Experiment *e;

	if (NULL == peer)
	{
		GNUNET_break (0);
		return;
	}
	if (NULL == message)
	{
		GNUNET_break (0);
		return;
	}

	size = ntohs (message->size);
	if (size < sizeof (struct GED_stop_message))
	{
		GNUNET_break (0);
		return;
	}
	msg = (const struct GED_stop_message *) message;
	name_len = ntohl (msg->len_name);
	if (size != sizeof (struct GED_start_message) + name_len)
	{
		GNUNET_break (0);
		return;
	}

	n = get_node (peer);
	if (NULL == n)
	{
		GNUNET_break (0);
		return;
	}
	name = (const char *) &msg[1];
	if (name[name_len-1] != '\0')
	{
		GNUNET_break (0);
		return;
	}

	if (name_len != strlen (name) + 1)
	{
		GNUNET_break (0);
		return;
	}

	e = GED_experiments_find (&msg->issuer, name, GNUNET_TIME_absolute_ntoh(msg->version_nbo));
	if (NULL == e)
	{
		GNUNET_break (0);
		return;
	}
	GED_scheduler_handle_stop (n, e);
}


/**
 * Method called whenever a given peer connects.
 *
 * @param cls closure
 * @param peer peer identity this notification is about
 */
static void
core_connect_handler (void *cls,
		      const struct GNUNET_PeerIdentity *peer)
{
  if (GNUNET_YES == is_me(peer))
    return;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Connected to peer %s\n"),
	      GNUNET_i2s (peer));

  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_requested, peer))
    return; /* We already sent a request */

  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_active, peer))
    return; /* This peer is known as active  */

  if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (nodes_inactive, peer))
    return; /* This peer is known as inactive  */

  send_experimentation_request (peer);
}


/**
 * Method called whenever a given peer disconnects.
 *
 * @param cls closure
 * @param peer peer identity this notification is about
 */
static void
core_disconnect_handler (void *cls,
			 const struct GNUNET_PeerIdentity * peer)
{
	struct Node *n;
	if (GNUNET_YES == is_me(peer))
		return;

	GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Disconnected from peer %s\n"),
			GNUNET_i2s (peer));

	if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_requested, peer)))
		cleanup_node (nodes_requested, peer, n);

	if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_active, peer)))
		cleanup_node (nodes_active, peer, n);

	if (NULL != (n = GNUNET_CONTAINER_multipeermap_get (nodes_inactive, peer)))
		cleanup_node (nodes_inactive, peer, n);
}


/**
 * Handle a request and send a response
 *
 * @param cls unused
 * @param other the sender
 * @param message the message
 * @return GNUNET_OK to keep connection, GNUNET_SYSERR on error
 */
static int
core_receive_handler (void *cls,
		      const struct GNUNET_PeerIdentity *other,
		      const struct GNUNET_MessageHeader *message)
{
	if (ntohs (message->size) < sizeof (struct GNUNET_MessageHeader))
	{
			GNUNET_break (0);
			return GNUNET_SYSERR;
	}

	switch (ntohs (message->type)) {
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_REQUEST:
			handle_request (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_RESPONSE:
			handle_response (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START:
			handle_start (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK:
			handle_start_ack (other, message);
			break;
		case GNUNET_MESSAGE_TYPE_EXPERIMENTATION_STOP:
			handle_stop (other, message);
			break;
		default:
			break;
	}

	return GNUNET_OK;
}


static size_t
node_experiment_start_cb (void *cls, size_t bufsize, void *buf)
{
	struct NodeComCtx *e_ctx = cls;
	struct GED_start_message *msg;
	size_t name_len;
	size_t size;

	if (NULL == buf)
		return 0;

	name_len = strlen(e_ctx->e->name) + 1;
	size = sizeof (struct GED_start_message) + name_len;

	msg = GNUNET_malloc (size);
	msg->header.size = htons (size);
	msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START);
	msg->issuer = e_ctx->e->issuer;
	msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
	msg->len_name = htonl (name_len);
	memcpy (&msg[1], e_ctx->e->name, name_len);

	memcpy (buf, msg, size);
	GNUNET_free (msg);
	return size;
}


static size_t
node_experiment_start_ack_cb (void *cls, size_t bufsize, void *buf)
{
	struct NodeComCtx *e_ctx = cls;
	struct GED_start_ack_message *msg;
	size_t name_len;
	size_t size;
	if (NULL == buf)
		return 0;

	name_len = strlen(e_ctx->e->name) + 1;
	size = sizeof (struct GED_start_ack_message) + name_len;

	msg = GNUNET_malloc (size);
	msg->header.size = htons (size);
	msg->header.type = htons (GNUNET_MESSAGE_TYPE_EXPERIMENTATION_START_ACK);
	msg->issuer = e_ctx->e->issuer;
	msg->version_nbo = GNUNET_TIME_absolute_hton(e_ctx->e->version);
	msg->len_name = htonl (name_len);
	memcpy (&msg[1], e_ctx->e->name, name_len);

	memcpy (buf, msg, size);
	GNUNET_free (msg);
	return size;
}




/**
 * Confirm a experiment START with a node
 *
 * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
 */
int
GED_nodes_send_start_ack (struct Node *n, struct Experiment *e)
{
	struct NodeComCtx *e_ctx;

	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
			"Sending %s for experiment request to peer `%s' for experiment `%s'\n",
			"START_ACK" ,GNUNET_i2s(&n->id), e->name);

	e_ctx = GNUNET_new (struct NodeComCtx);
	e_ctx->n = n;
	e_ctx->e = e;
	e_ctx->size = sizeof (struct GED_start_ack_message) + strlen (e->name) + 1;
	e_ctx->notify = &node_experiment_start_ack_cb;
	e_ctx->notify_cls = e_ctx;

	GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
	schedule_transmisson (e_ctx);
	return GNUNET_OK;
}


/**
 * Request a experiment to start with a node
 *
 * @return GNUNET_NO if core was busy with sending, GNUNET_OK otherwise
 */
int
GED_nodes_send_start (struct Node *n, struct Experiment *e)
{
	struct NodeComCtx *e_ctx;

	GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
			"Sending %s for experiment request to peer `%s' for experiment `%s'\n",
			"START", GNUNET_i2s(&n->id), e->name);

	e_ctx = GNUNET_new (struct NodeComCtx);
	e_ctx->n = n;
	e_ctx->e = e;
	e_ctx->size = sizeof (struct GED_start_message) + strlen (e->name) + 1;
	e_ctx->notify = &node_experiment_start_cb;
	e_ctx->notify_cls = e_ctx;

	GNUNET_CONTAINER_DLL_insert_tail (n->e_req_head, n->e_req_tail, e_ctx);
	schedule_transmisson (e_ctx);
	return GNUNET_OK;
}


/**
 * Start the nodes management
 */
void
GED_nodes_start ()
{
	/* Connecting to core service to find partners */
	ch = GNUNET_CORE_connect (GED_cfg, NULL,
														&core_startup_handler,
														&core_connect_handler,
													 	&core_disconnect_handler,
														&core_receive_handler,
														GNUNET_NO, NULL, GNUNET_NO, NULL);
	if (NULL == ch)
	{
			GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Failed to connect to CORE service!\n"));
			return;
	}

	nodes_requested = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
	nodes_active = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
	nodes_inactive = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
}


/**
 * Stop the nodes management
 */
void
GED_nodes_stop ()
{
  if (NULL != ch)
  {
  		GNUNET_CORE_disconnect (ch);
  		ch = NULL;
  }

  if (NULL != nodes_requested)
  {
  		GNUNET_CONTAINER_multipeermap_iterate (nodes_requested,
  																					 &cleanup_node,
  																					 nodes_requested);
  		update_stats (nodes_requested);
  		GNUNET_CONTAINER_multipeermap_destroy (nodes_requested);
  		nodes_requested = NULL;
  }

  if (NULL != nodes_active)
  {
  		GNUNET_CONTAINER_multipeermap_iterate (nodes_active,
  																					 &cleanup_node,
  																					 nodes_active);
  		update_stats (nodes_active);
  		GNUNET_CONTAINER_multipeermap_destroy (nodes_active);
  		nodes_active = NULL;
  }

  if (NULL != nodes_inactive)
  {
  		GNUNET_CONTAINER_multipeermap_iterate (nodes_inactive,
  																					 &cleanup_node,
  																					 nodes_inactive);
  		update_stats (nodes_inactive);
  		GNUNET_CONTAINER_multipeermap_destroy (nodes_inactive);
  		nodes_inactive = NULL;
  }
}

/* end of gnunet-daemon-experimentation_nodes.c */