multicast, psyc, psycstore, client_manager fixes
[oweals/gnunet.git] / src / multicast / gnunet-service-multicast.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file multicast/gnunet-service-multicast.c
23  * @brief program that does multicast
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_applications.h"
30 #include "gnunet_statistics_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_cadet_service.h"
33 #include "gnunet_multicast_service.h"
34 #include "multicast.h"
35
36 /**
37  * Handle to our current configuration.
38  */
39 static const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41 /**
42  * Server handle.
43  */
44 static struct GNUNET_SERVER_Handle *server;
45
46 /**
47  * Core handle.
48  * Only used during initialization.
49  */
50 static struct GNUNET_CORE_Handle *core;
51
52 /**
53  * CADET handle.
54  */
55 static struct GNUNET_CADET_Handle *cadet;
56
57 /**
58  * Identity of this peer.
59  */
60 static struct GNUNET_PeerIdentity this_peer;
61
62 /**
63  * Handle to the statistics service.
64  */
65 static struct GNUNET_STATISTICS_Handle *stats;
66
67 /**
68  * Notification context, simplifies client broadcasts.
69  */
70 static struct GNUNET_SERVER_NotificationContext *nc;
71
72 /**
73  * All connected origin clients.
74  * Group's pub_key_hash -> struct Origin * (uniq)
75  */
76 static struct GNUNET_CONTAINER_MultiHashMap *origins;
77
78 /**
79  * All connected member clients.
80  * Group's pub_key_hash -> struct Member * (multi)
81  */
82 static struct GNUNET_CONTAINER_MultiHashMap *members;
83
84 /**
85  * Connected member clients per group.
86  * Group's pub_key_hash -> Member's pub_key_hash (uniq) -> struct Member * (uniq)
87  */
88 static struct GNUNET_CONTAINER_MultiHashMap *group_members;
89
90 /**
91  * Incoming CADET channels.
92  * Group's pub_key_hash -> struct Channel * (multi)
93  */
94 static struct GNUNET_CONTAINER_MultiHashMap *channels_in;
95
96 /**
97  * Outgoing CADET channels.
98  * Group's pub_key_hash -> struct Channel * (multi)
99  */
100 static struct GNUNET_CONTAINER_MultiHashMap *channels_out;
101
102 /**
103  * Join status of a remote peer.
104  */
105 enum JoinStatus
106 {
107   JOIN_REFUSED  = -1,
108   JOIN_NOT_ASKED = 0,
109   JOIN_WAITING   = 1,
110   JOIN_ADMITTED  = 2,
111 };
112
113 enum ChannelDirection
114 {
115   DIR_INCOMING = 0,
116   DIR_OUTGOING = 1,
117 };
118
119
120 /**
121  * Context for a CADET channel.
122  */
123 struct Channel
124 {
125   /**
126    * Group the channel belongs to.
127    *
128    * Only set for outgoing channels.
129    */
130   struct Group *grp;
131
132   /**
133    * CADET channel.
134    */
135   struct GNUNET_CADET_Channel *channel;
136
137   /**
138    * CADET transmission handle.
139    */
140   struct GNUNET_CADET_TransmitHandle *tmit_handle;
141
142   /**
143    * Public key of the target group.
144    */
145   struct GNUNET_CRYPTO_EddsaPublicKey group_key;
146
147   /**
148    * Hash of @a group_key.
149    */
150   struct GNUNET_HashCode group_key_hash;
151
152   /**
153    * Public key of the joining member.
154    */
155   struct GNUNET_CRYPTO_EcdsaPublicKey member_key;
156
157   /**
158    * Remote peer identity.
159    */
160   struct GNUNET_PeerIdentity peer;
161
162   /**
163    * Is the remote peer admitted to the group?
164    * @see enum JoinStatus
165    */
166   int8_t join_status;
167
168   /**
169    * Channel direction.
170    * @see enum ChannelDirection
171    */
172   uint8_t direction;
173 };
174
175
176 /**
177  * List of connected clients.
178  */
179 struct ClientList
180 {
181   struct ClientList *prev;
182   struct ClientList *next;
183   struct GNUNET_SERVER_Client *client;
184 };
185
186 /**
187  * Common part of the client context for both an origin and member.
188  */
189 struct Group
190 {
191   struct ClientList *clients_head;
192   struct ClientList *clients_tail;
193
194   /**
195    * Public key of the group.
196    */
197   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
198
199   /**
200    * Hash of @a pub_key.
201    */
202   struct GNUNET_HashCode pub_key_hash;
203
204   /**
205    * Is this an origin (#GNUNET_YES), or member (#GNUNET_NO)?
206    */
207   uint8_t is_origin;
208
209   /**
210    * Is the client disconnected? #GNUNET_YES or #GNUNET_NO
211    */
212   uint8_t disconnected;
213 };
214
215
216 /**
217  * Client context for a group's origin.
218  */
219 struct Origin
220 {
221   struct Group grp;
222
223   /**
224    * Private key of the group.
225    */
226   struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
227
228   /**
229    * Last message fragment ID sent to the group.
230    */
231   uint64_t max_fragment_id;
232 };
233
234
235 /**
236  * Client context for a group member.
237  */
238 struct Member
239 {
240   struct Group grp;
241
242   /**
243    * Private key of the member.
244    */
245   struct GNUNET_CRYPTO_EcdsaPrivateKey priv_key;
246
247   /**
248    * Public key of the member.
249    */
250   struct GNUNET_CRYPTO_EcdsaPublicKey pub_key;
251
252   /**
253    * Hash of @a pub_key.
254    */
255   struct GNUNET_HashCode pub_key_hash;
256
257   /**
258    * Join request sent to the origin / members.
259    */
260   struct MulticastJoinRequestMessage *join_req;
261
262   /**
263    * Join decision sent in reply to our request.
264    *
265    * Only a positive decision is stored here, in case of a negative decision the
266    * client is disconnected.
267    */
268   struct MulticastJoinDecisionMessageHeader *join_dcsn;
269
270   /**
271    * CADET channel to the origin.
272    */
273   struct Channel *origin_channel;
274
275   /**
276    * Peer identity of origin.
277    */
278   struct GNUNET_PeerIdentity origin;
279
280   /**
281    * Peer identity of relays (other members to connect).
282    */
283   struct GNUNET_PeerIdentity *relays;
284
285   /**
286    * Last request fragment ID sent to the origin.
287    */
288   uint64_t max_fragment_id;
289
290   /**
291    * Number of @a relays.
292    */
293   uint32_t relay_count;
294 };
295
296
297 /**
298  * Task run during shutdown.
299  *
300  * @param cls unused
301  * @param tc unused
302  */
303 static void
304 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
305 {
306   if (NULL != core)
307   {
308     GNUNET_CORE_disconnect (core);
309     core = NULL;
310   }
311   if (NULL != cadet)
312   {
313     GNUNET_CADET_disconnect (cadet);
314     cadet = NULL;
315   }
316   if (NULL != stats)
317   {
318     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
319     stats = NULL;
320   }
321   /* FIXME: do more clean up here */
322 }
323
324
325 /**
326  * Clean up origin data structures after a client disconnected.
327  */
328 static void
329 cleanup_origin (struct Origin *orig)
330 {
331   struct Group *grp = &orig->grp;
332   GNUNET_CONTAINER_multihashmap_remove (origins, &grp->pub_key_hash, orig);
333 }
334
335
336 /**
337  * Clean up member data structures after a client disconnected.
338  */
339 static void
340 cleanup_member (struct Member *mem)
341 {
342   struct Group *grp = &mem->grp;
343   struct GNUNET_CONTAINER_MultiHashMap *
344     grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members,
345                                                  &grp->pub_key_hash);
346   GNUNET_assert (NULL != grp_mem);
347   GNUNET_CONTAINER_multihashmap_remove (grp_mem, &mem->pub_key_hash, mem);
348
349   if (0 == GNUNET_CONTAINER_multihashmap_size (grp_mem))
350   {
351     GNUNET_CONTAINER_multihashmap_remove (group_members, &grp->pub_key_hash,
352                                           grp_mem);
353     GNUNET_CONTAINER_multihashmap_destroy (grp_mem);
354   }
355   if (NULL != mem->join_dcsn)
356   {
357     GNUNET_free (mem->join_dcsn);
358     mem->join_dcsn = NULL;
359   }
360   GNUNET_CONTAINER_multihashmap_remove (members, &grp->pub_key_hash, mem);
361 }
362
363
364 /**
365  * Clean up group data structures after a client disconnected.
366  */
367 static void
368 cleanup_group (struct Group *grp)
369 {
370   (GNUNET_YES == grp->is_origin)
371     ? cleanup_origin ((struct Origin *) grp)
372     : cleanup_member ((struct Member *) grp);
373
374   GNUNET_free (grp);
375 }
376
377
378 /**
379  * Called whenever a client is disconnected.
380  *
381  * Frees our resources associated with that client.
382  *
383  * @param cls  Closure.
384  * @param client  Client handle.
385  */
386 static void
387 client_notify_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
388 {
389   if (NULL == client)
390     return;
391
392   struct Group *grp
393     = GNUNET_SERVER_client_get_user_context (client, struct Group);
394
395   if (NULL == grp)
396   {
397     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
398                 "%p User context is NULL in client_disconnect()\n", grp);
399     GNUNET_assert (0);
400     return;
401   }
402
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404               "%p Client (%s) disconnected from group %s\n",
405               grp, (GNUNET_YES == grp->is_origin) ? "origin" : "member",
406               GNUNET_h2s (&grp->pub_key_hash));
407
408   struct ClientList *cl = grp->clients_head;
409   while (NULL != cl)
410   {
411     if (cl->client == client)
412     {
413       GNUNET_CONTAINER_DLL_remove (grp->clients_head, grp->clients_tail, cl);
414       GNUNET_free (cl);
415       break;
416     }
417     cl = cl->next;
418   }
419
420   if (NULL == grp->clients_head)
421   { /* Last client disconnected. */
422 #if FIXME
423     if (NULL != grp->tmit_head)
424     { /* Send pending messages via CADET before cleanup. */
425       transmit_message (grp);
426     }
427     else
428 #endif
429     {
430       cleanup_group (grp);
431     }
432   }
433 }
434
435
436 /**
437  * Send message to all clients connected to the group.
438  */
439 static void
440 client_send_msg (const struct Group *grp,
441                  const struct GNUNET_MessageHeader *msg)
442 {
443   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
444               "%p Sending message to clients.\n", grp);
445
446   struct ClientList *cl = grp->clients_head;
447   while (NULL != cl)
448   {
449     GNUNET_SERVER_notification_context_add (nc, cl->client);
450     GNUNET_SERVER_notification_context_unicast (nc, cl->client, msg, GNUNET_NO);
451     cl = cl->next;
452   }
453 }
454
455
456 /**
457  * Iterator callback for sending a message to origin clients.
458  */
459 static int
460 client_send_origin_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
461                        void *origin)
462 {
463   const struct GNUNET_MessageHeader *msg = cls;
464   struct Member *orig = origin;
465
466   client_send_msg (&orig->grp, msg);
467   return GNUNET_YES;
468 }
469
470
471 /**
472  * Iterator callback for sending a message to member clients.
473  */
474 static int
475 client_send_member_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
476                        void *member)
477 {
478   const struct GNUNET_MessageHeader *msg = cls;
479   struct Member *mem = member;
480
481   if (NULL != mem->join_dcsn)
482   { /* Only send message to admitted members */
483     client_send_msg (&mem->grp, msg);
484   }
485   return GNUNET_YES;
486 }
487
488
489 /**
490  * Send message to all origin and member clients connected to the group.
491  *
492  * @param grp  The group to send @a msg to.
493  * @param msg  Message to send.
494  */
495 static int
496 client_send_all (struct GNUNET_HashCode *pub_key_hash,
497                  const struct GNUNET_MessageHeader *msg)
498 {
499   int n = 0;
500   if (origins != NULL)
501     n += GNUNET_CONTAINER_multihashmap_get_multiple (origins, pub_key_hash,
502                                                      client_send_origin_cb,
503                                                      (void *) msg);
504   if (members != NULL)
505     n += GNUNET_CONTAINER_multihashmap_get_multiple (members, pub_key_hash,
506                                                      client_send_member_cb,
507                                                      (void *) msg);
508   return n;
509 }
510
511
512 /**
513  * Send message to all origin clients connected to the group.
514  *
515  * @param grp  The group to send @a msg to.
516  * @param msg  Message to send.
517  */
518 static int
519 client_send_origin (struct GNUNET_HashCode *pub_key_hash,
520                     const struct GNUNET_MessageHeader *msg)
521 {
522   int n = 0;
523   if (origins != NULL)
524     n += GNUNET_CONTAINER_multihashmap_get_multiple (origins, pub_key_hash,
525                                                      client_send_origin_cb,
526                                                      (void *) msg);
527   return n;
528 }
529
530
531 /**
532  * CADET is ready to transmit a message.
533  */
534 size_t
535 cadet_notify_transmit_ready (void *cls, size_t buf_size, void *buf)
536 {
537   if (0 == buf_size)
538   {
539     /* FIXME: connection closed */
540     return 0;
541   }
542   const struct GNUNET_MessageHeader *msg = cls;
543   uint16_t msg_size = ntohs (msg->size);
544   GNUNET_assert (msg_size <= buf_size);
545   memcpy (buf, msg, msg_size);
546   return msg_size;
547 }
548
549
550 /**
551  * Send a message to a CADET channel.
552  *
553  * @param chn  Channel.
554  * @param msg  Message.
555  */
556 static void
557 cadet_send_msg (struct Channel *chn, const struct GNUNET_MessageHeader *msg)
558 {
559   chn->tmit_handle
560     = GNUNET_CADET_notify_transmit_ready (chn->channel, GNUNET_NO,
561                                           GNUNET_TIME_UNIT_FOREVER_REL,
562                                           ntohs (msg->size),
563                                           &cadet_notify_transmit_ready,
564                                           (void *) msg);
565   GNUNET_assert (NULL != chn->tmit_handle);
566 }
567
568
569 /**
570  * Create new outgoing CADET channel.
571  *
572  * @param peer
573  *        Peer to connect to.
574  * @param group_key
575  *        Public key of group the channel belongs to.
576  * @param group_key_hash
577  *        Hash of @a group_key.
578  *
579  * @return Channel.
580  */
581 static struct Channel *
582 cadet_channel_create (struct Group *grp, struct GNUNET_PeerIdentity *peer)
583 {
584   struct Channel *chn = GNUNET_malloc (sizeof (*chn));
585   chn->grp = grp;
586   chn->group_key = grp->pub_key;
587   chn->group_key_hash = grp->pub_key_hash;
588   chn->peer = *peer;
589   chn->direction = DIR_OUTGOING;
590   chn->join_status = JOIN_WAITING;
591   chn->channel = GNUNET_CADET_channel_create (cadet, chn, &chn->peer,
592                                               GNUNET_APPLICATION_TYPE_MULTICAST,
593                                               GNUNET_CADET_OPTION_RELIABLE);
594   GNUNET_CONTAINER_multihashmap_put (channels_out, &chn->group_key_hash, chn,
595                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
596   return chn;
597 }
598
599
600 /**
601  * Create CADET channel and send a join request.
602  */
603 static void
604 cadet_send_join_request (struct Member *mem)
605 {
606   mem->origin_channel = cadet_channel_create (&mem->grp, &mem->origin);
607   cadet_send_msg (mem->origin_channel, &mem->join_req->header);
608
609   uint32_t i;
610   for (i = 0; i < mem->relay_count; i++)
611   {
612     struct Channel *
613       chn = cadet_channel_create (&mem->grp, &mem->relays[i]);
614     cadet_send_msg (chn, &mem->join_req->header);
615   }
616 }
617
618
619 static int
620 cadet_send_join_decision_cb (void *cls,
621                              const struct GNUNET_HashCode *group_key_hash,
622                              void *channel)
623 {
624   const struct MulticastJoinDecisionMessageHeader *hdcsn = cls;
625   struct Channel *chn = channel;
626
627   if (0 == memcmp (&hdcsn->member_key, &chn->member_key, sizeof (chn->member_key))
628       && 0 == memcmp (&hdcsn->peer, &chn->peer, sizeof (chn->peer)))
629   {
630     cadet_send_msg (chn, &hdcsn->header);
631     return GNUNET_NO;
632   }
633   return GNUNET_YES;
634 }
635
636
637 /**
638  * Send join decision to a remote peer.
639  */
640 static void
641 cadet_send_join_decision (struct Group *grp,
642                           const struct MulticastJoinDecisionMessageHeader *hdcsn)
643 {
644   GNUNET_CONTAINER_multihashmap_get_multiple (channels_in, &grp->pub_key_hash,
645                                               &cadet_send_join_decision_cb,
646                                               (void *) hdcsn);
647 }
648
649
650 /**
651  * Iterator callback for sending a message to origin clients.
652  */
653 static int
654 cadet_send_members_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
655                        void *channel)
656 {
657   const struct GNUNET_MessageHeader *msg = cls;
658   struct Channel *chn = channel;
659   if (JOIN_ADMITTED == chn->join_status)
660     cadet_send_msg (chn, msg);
661   return GNUNET_YES;
662 }
663
664
665 static int
666 cadet_send_members (struct GNUNET_HashCode *pub_key_hash,
667                     const struct GNUNET_MessageHeader *msg)
668 {
669   int n = 0;
670   if (channels_in != NULL)
671     n += GNUNET_CONTAINER_multihashmap_get_multiple (channels_in, pub_key_hash,
672                                                      cadet_send_members_cb,
673                                                      (void *) msg);
674   return n;
675 }
676
677 /**
678  * Handle a connecting client starting an origin.
679  */
680 static void
681 client_recv_origin_start (void *cls, struct GNUNET_SERVER_Client *client,
682                           const struct GNUNET_MessageHeader *m)
683 {
684   const struct MulticastOriginStartMessage *
685     msg = (const struct MulticastOriginStartMessage *) m;
686
687   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
688   struct GNUNET_HashCode pub_key_hash;
689
690   GNUNET_CRYPTO_eddsa_key_get_public (&msg->group_key, &pub_key);
691   GNUNET_CRYPTO_hash (&pub_key, sizeof (pub_key), &pub_key_hash);
692
693   struct Origin *
694     orig = GNUNET_CONTAINER_multihashmap_get (origins, &pub_key_hash);
695   struct Group *grp;
696
697   if (NULL == orig)
698   {
699     orig = GNUNET_new (struct Origin);
700     orig->priv_key = msg->group_key;
701     orig->max_fragment_id = GNUNET_ntohll (msg->max_fragment_id);
702     grp = &orig->grp;
703     grp->is_origin = GNUNET_YES;
704     grp->pub_key = pub_key;
705     grp->pub_key_hash = pub_key_hash;
706
707     GNUNET_CONTAINER_multihashmap_put (origins, &grp->pub_key_hash, orig,
708                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
709   }
710   else
711   {
712     grp = &orig->grp;
713   }
714
715   struct ClientList *cl = GNUNET_new (struct ClientList);
716   cl->client = client;
717   GNUNET_CONTAINER_DLL_insert (grp->clients_head, grp->clients_tail, cl);
718
719   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
720               "%p Client connected as origin to group %s.\n",
721               orig, GNUNET_h2s (&grp->pub_key_hash));
722
723   GNUNET_SERVER_client_set_user_context (client, grp);
724   GNUNET_SERVER_receive_done (client, GNUNET_OK);
725 }
726
727
728 /**
729  * Handle a connecting client joining a group.
730  */
731 static void
732 client_recv_member_join (void *cls, struct GNUNET_SERVER_Client *client,
733                          const struct GNUNET_MessageHeader *m)
734 {
735   const struct MulticastMemberJoinMessage *
736     msg = (const struct MulticastMemberJoinMessage *) m;
737   uint16_t msg_size = ntohs (msg->header.size);
738
739   struct GNUNET_CRYPTO_EcdsaPublicKey mem_pub_key;
740   struct GNUNET_HashCode pub_key_hash, mem_pub_key_hash;
741
742   GNUNET_CRYPTO_ecdsa_key_get_public (&msg->member_key, &mem_pub_key);
743   GNUNET_CRYPTO_hash (&mem_pub_key, sizeof (mem_pub_key), &mem_pub_key_hash);
744   GNUNET_CRYPTO_hash (&msg->group_key, sizeof (msg->group_key), &pub_key_hash);
745
746   struct GNUNET_CONTAINER_MultiHashMap *
747     grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members, &pub_key_hash);
748   struct Member *mem = NULL;
749   struct Group *grp;
750
751   if (NULL != grp_mem)
752   {
753     mem = GNUNET_CONTAINER_multihashmap_get (grp_mem, &mem_pub_key_hash);
754   }
755   if (NULL == mem)
756   {
757     mem = GNUNET_new (struct Member);
758     mem->priv_key = msg->member_key;
759     mem->pub_key = mem_pub_key;
760     mem->pub_key_hash = mem_pub_key_hash;
761     mem->max_fragment_id = 0; // FIXME
762
763     grp = &mem->grp;
764     grp->is_origin = GNUNET_NO;
765     grp->pub_key = msg->group_key;
766     grp->pub_key_hash = pub_key_hash;
767
768     if (NULL == grp_mem)
769     {
770       grp_mem = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
771       GNUNET_CONTAINER_multihashmap_put (group_members, &grp->pub_key_hash, grp_mem,
772                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
773     }
774     GNUNET_CONTAINER_multihashmap_put (grp_mem, &mem->pub_key_hash, mem,
775                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
776     GNUNET_CONTAINER_multihashmap_put (members, &grp->pub_key_hash, mem,
777                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
778   }
779   else
780   {
781     grp = &mem->grp;
782   }
783
784   struct ClientList *cl = GNUNET_new (struct ClientList);
785   cl->client = client;
786   GNUNET_CONTAINER_DLL_insert (grp->clients_head, grp->clients_tail, cl);
787
788   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789               "%p Client connected to group %s..\n",
790               mem, GNUNET_h2s (&grp->pub_key_hash));
791   char *str = GNUNET_CRYPTO_ecdsa_public_key_to_string (&mem->pub_key);
792   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793               "%p ..as member %s (%s).\n",
794               mem, GNUNET_h2s (&mem->pub_key_hash), str);
795   GNUNET_free (str);
796
797   GNUNET_SERVER_client_set_user_context (client, grp);
798
799   if (NULL != mem->join_dcsn)
800   { /* Already got a join decision, send it to client. */
801     GNUNET_SERVER_notification_context_add (nc, client);
802     GNUNET_SERVER_notification_context_unicast (nc, client,
803                                                 (struct GNUNET_MessageHeader *)
804                                                 mem->join_dcsn,
805                                                 GNUNET_NO);
806   }
807   else if (grp->clients_head == grp->clients_tail)
808   { /* First client of the group, send join request. */
809     struct GNUNET_PeerIdentity *relays = (struct GNUNET_PeerIdentity *) &msg[1];
810     uint32_t relay_count = ntohs (msg->relay_count);
811     uint16_t relay_size = relay_count * sizeof (*relays);
812     struct GNUNET_MessageHeader *join_msg = NULL;
813     uint16_t join_msg_size = 0;
814     if (sizeof (*msg) + relay_size + sizeof (struct GNUNET_MessageHeader)
815         <= msg_size)
816     {
817       join_msg = (struct GNUNET_MessageHeader *)
818         (((char *) &msg[1]) + relay_size);
819       join_msg_size = ntohs (join_msg->size);
820     }
821     if (sizeof (*msg) + relay_size + join_msg_size != msg_size)
822     {
823       GNUNET_break (0);
824       GNUNET_SERVER_client_disconnect (client);
825       return;
826     }
827
828     struct MulticastJoinRequestMessage *
829       req = GNUNET_malloc (sizeof (*req) + join_msg_size);
830     req->header.size = htons (sizeof (*req) + join_msg_size);
831     req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST);
832     req->group_key = grp->pub_key;
833     req->peer = this_peer;
834     GNUNET_CRYPTO_ecdsa_key_get_public (&mem->priv_key, &req->member_key);
835     if (0 < join_msg_size)
836       memcpy (&req[1], join_msg, join_msg_size);
837
838     req->member_key = mem->pub_key;
839     req->purpose.size = htonl (msg_size
840                                - sizeof (req->header)
841                                - sizeof (req->reserved)
842                                - sizeof (req->signature));
843     req->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST);
844
845     if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (&mem->priv_key, &req->purpose,
846                                                &req->signature))
847     {
848       /* FIXME: handle error */
849       GNUNET_assert (0);
850     }
851
852     if (NULL != mem->join_req)
853       GNUNET_free (mem->join_req);
854     mem->join_req = req;
855
856     if (0 == client_send_origin (&grp->pub_key_hash, &mem->join_req->header))
857     { /* No local origins, send to remote origin */
858       cadet_send_join_request (mem);
859     }
860   }
861   GNUNET_SERVER_receive_done (client, GNUNET_OK);
862 }
863
864
865 static void
866 client_send_join_decision (struct Member *mem,
867                            const struct MulticastJoinDecisionMessageHeader *hdcsn)
868 {
869   client_send_msg (&mem->grp, &hdcsn->header);
870
871   const struct MulticastJoinDecisionMessage *
872     dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
873   if (GNUNET_YES == ntohl (dcsn->is_admitted))
874   { /* Member admitted, store join_decision. */
875     uint16_t dcsn_size = ntohs (dcsn->header.size);
876     mem->join_dcsn = GNUNET_malloc (dcsn_size);
877     memcpy (mem->join_dcsn, dcsn, dcsn_size);
878   }
879   else
880   { /* Refused entry, disconnect clients. */
881     struct ClientList *cl = mem->grp.clients_head;
882     while (NULL != cl)
883     {
884       struct GNUNET_SERVER_Client *client = cl->client;
885       cl = cl->next;
886       GNUNET_SERVER_client_disconnect (client);
887     }
888   }
889 }
890
891
892 /**
893  * Join decision from client.
894  */
895 static void
896 client_recv_join_decision (void *cls, struct GNUNET_SERVER_Client *client,
897                            const struct GNUNET_MessageHeader *m)
898 {
899   struct Group *
900     grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
901   const struct MulticastJoinDecisionMessageHeader *
902     hdcsn = (const struct MulticastJoinDecisionMessageHeader *) m;
903
904   if (NULL == grp)
905   {
906     GNUNET_break (0);
907     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
908     return;
909   }
910   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911               "%p Got join decision from client for group %s..\n",
912               grp, GNUNET_h2s (&grp->pub_key_hash));
913
914   struct GNUNET_CONTAINER_MultiHashMap *
915     grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members,
916                                                  &grp->pub_key_hash);
917   struct Member *mem = NULL;
918   if (NULL != grp_mem)
919   {
920     struct GNUNET_HashCode member_key_hash;
921     GNUNET_CRYPTO_hash (&hdcsn->member_key, sizeof (hdcsn->member_key),
922                         &member_key_hash);
923     mem = GNUNET_CONTAINER_multihashmap_get (grp_mem, &member_key_hash);
924     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925                 "%p ..and member %s: %p\n",
926                 grp, GNUNET_h2s (&member_key_hash), mem);
927   }
928   if (NULL != mem)
929   { /* Found local member */
930     client_send_join_decision (mem, hdcsn);
931   }
932   else
933   { /* Look for remote member */
934     cadet_send_join_decision (grp, hdcsn);
935   }
936   GNUNET_SERVER_receive_done (client, GNUNET_OK);
937 }
938
939
940 /**
941  * Incoming message from a client.
942  */
943 static void
944 client_recv_multicast_message (void *cls, struct GNUNET_SERVER_Client *client,
945                                const struct GNUNET_MessageHeader *m)
946 {
947   struct Group *
948     grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
949   struct GNUNET_MULTICAST_MessageHeader *out;
950   struct Origin *orig;
951
952   if (NULL == grp)
953   {
954     GNUNET_break (0);
955     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
956     return;
957   }
958   GNUNET_assert (GNUNET_YES == grp->is_origin);
959   orig = (struct Origin *) grp;
960   /* FIXME: yucky, should use separate message structs for P2P and CS! */
961   out = (struct GNUNET_MULTICAST_MessageHeader *) GNUNET_copy_message (m);
962
963   out->fragment_id = GNUNET_htonll (++orig->max_fragment_id);
964   out->purpose.size = htonl (ntohs (out->header.size)
965                              - sizeof (out->header)
966                              - sizeof (out->hop_counter)
967                              - sizeof (out->signature));
968   out->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE);
969
970   if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &out->purpose,
971                                              &out->signature))
972   {
973     GNUNET_assert (0);
974   }
975
976   client_send_all (&grp->pub_key_hash, &out->header);
977   cadet_send_members (&grp->pub_key_hash, &out->header);
978   GNUNET_free (out);
979
980   GNUNET_SERVER_receive_done (client, GNUNET_OK);
981 }
982
983
984 /**
985  * Incoming request from a client.
986  */
987 static void
988 client_recv_multicast_request (void *cls, struct GNUNET_SERVER_Client *client,
989                                const struct GNUNET_MessageHeader *m)
990 {
991   struct Group *grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
992   struct Member *mem;
993   struct GNUNET_MULTICAST_RequestHeader *out;
994
995   if (NULL == grp)
996   {
997     GNUNET_break (0);
998     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
999     return;
1000   }
1001   GNUNET_assert (GNUNET_NO == grp->is_origin);
1002   mem = (struct Member *) grp;
1003   /* FIXME: yucky, should use separate message structs for P2P and CS! */
1004   out = (struct GNUNET_MULTICAST_RequestHeader *) GNUNET_copy_message (m);
1005
1006   out->member_key = mem->pub_key;
1007   out->fragment_id = GNUNET_ntohll (++mem->max_fragment_id);
1008   out->purpose.size = htonl (ntohs (out->header.size)
1009                              - sizeof (out->header)
1010                              - sizeof (out->member_key)
1011                              - sizeof (out->signature));
1012   out->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST);
1013
1014   if (GNUNET_OK != GNUNET_CRYPTO_ecdsa_sign (&mem->priv_key, &out->purpose,
1015                                              &out->signature))
1016   {
1017     GNUNET_assert (0);
1018   }
1019
1020   if (0 == client_send_origin (&grp->pub_key_hash, &out->header))
1021   { /* No local origins, send to remote origin */
1022     if (NULL != mem->origin_channel)
1023     {
1024       cadet_send_msg (mem->origin_channel, &out->header);
1025     }
1026     else
1027     {
1028       /* FIXME: not yet connected to origin */
1029       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1030       GNUNET_free (out);
1031       return;
1032     }
1033   }
1034   GNUNET_free (out);
1035   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1036 }
1037
1038
1039 /**
1040  * A new client connected.
1041  */
1042 static void
1043 client_notify_connect (void *cls, struct GNUNET_SERVER_Client *client)
1044 {
1045   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client connected: %p\n", client);
1046   /* FIXME: send connect ACK */
1047 }
1048
1049
1050 /**
1051  * Message handlers for the server.
1052  */
1053 static const struct GNUNET_SERVER_MessageHandler server_handlers[] = {
1054   { &client_recv_origin_start, NULL,
1055     GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START, 0 },
1056
1057   { &client_recv_member_join, NULL,
1058     GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN, 0 },
1059
1060   { &client_recv_join_decision, NULL,
1061     GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION, 0 },
1062
1063   { &client_recv_multicast_message, NULL,
1064     GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE, 0 },
1065
1066   { &client_recv_multicast_request, NULL,
1067     GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST, 0 },
1068
1069   {NULL, NULL, 0, 0}
1070 };
1071
1072
1073 /**
1074  * New incoming CADET channel.
1075  */
1076 static void *
1077 cadet_notify_channel_new (void *cls,
1078                           struct GNUNET_CADET_Channel *channel,
1079                           const struct GNUNET_PeerIdentity *initiator,
1080                           uint32_t port,
1081                           enum GNUNET_CADET_ChannelOption options)
1082 {
1083   return NULL;
1084 }
1085
1086
1087 /**
1088  * CADET channel is being destroyed.
1089  */
1090 static void
1091 cadet_notify_channel_end (void *cls,
1092                           const struct GNUNET_CADET_Channel *channel,
1093                           void *ctx)
1094 {
1095   if (NULL == ctx)
1096     return;
1097
1098   struct Channel *chn = ctx;
1099   if (NULL != chn->grp)
1100   {
1101     if (GNUNET_NO == chn->grp->is_origin)
1102     {
1103       struct Member *mem = (struct Member *) chn->grp;
1104       if (chn == mem->origin_channel)
1105         mem->origin_channel = NULL;
1106     }
1107   }
1108   GNUNET_free (chn);
1109 }
1110
1111
1112 /**
1113  * Incoming join request message from CADET.
1114  */
1115 int
1116 cadet_recv_join_request (void *cls,
1117                          struct GNUNET_CADET_Channel *channel,
1118                          void **ctx,
1119                          const struct GNUNET_MessageHeader *m)
1120 {
1121   const struct MulticastJoinRequestMessage *
1122     req = (const struct MulticastJoinRequestMessage *) m;
1123   uint16_t size = ntohs (m->size);
1124   if (size < sizeof (*req))
1125   {
1126     GNUNET_break_op (0);
1127     return GNUNET_SYSERR;
1128   }
1129   if (NULL != *ctx)
1130   {
1131     GNUNET_break_op (0);
1132     return GNUNET_SYSERR;
1133   }
1134   if (ntohl (req->purpose.size) != (size
1135                                     - sizeof (req->header)
1136                                     - sizeof (req->reserved)
1137                                     - sizeof (req->signature)))
1138   {
1139     GNUNET_break_op (0);
1140     return GNUNET_SYSERR;
1141   }
1142   if (GNUNET_OK !=
1143       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST,
1144                                   &req->purpose, &req->signature,
1145                                   &req->member_key))
1146   {
1147     GNUNET_break_op (0);
1148     return GNUNET_SYSERR;
1149   }
1150
1151   struct GNUNET_HashCode group_key_hash;
1152   GNUNET_CRYPTO_hash (&req->group_key, sizeof (req->group_key), &group_key_hash);
1153
1154   struct Channel *chn = GNUNET_malloc (sizeof *chn);
1155   chn->channel = channel;
1156   chn->group_key = req->group_key;
1157   chn->group_key_hash = group_key_hash;
1158   chn->member_key = req->member_key;
1159   chn->peer = req->peer;
1160   chn->join_status = JOIN_WAITING;
1161   GNUNET_CONTAINER_multihashmap_put (channels_in, &chn->group_key_hash, chn,
1162                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1163
1164   client_send_all (&group_key_hash, m);
1165   return GNUNET_OK;
1166 }
1167
1168
1169 /**
1170  * Incoming join decision message from CADET.
1171  */
1172 int
1173 cadet_recv_join_decision (void *cls,
1174                           struct GNUNET_CADET_Channel *channel,
1175                           void **ctx,
1176                           const struct GNUNET_MessageHeader *m)
1177 {
1178   const struct MulticastJoinDecisionMessage *
1179     dcsn = (const struct MulticastJoinDecisionMessage *) m;
1180   uint16_t size = ntohs (m->size);
1181   if (size < sizeof (*dcsn))
1182   {
1183     GNUNET_break_op (0);
1184     return GNUNET_SYSERR;
1185   }
1186   struct Channel *chn = *ctx;
1187   if (NULL == chn)
1188   {
1189     GNUNET_break_op (0);
1190     return GNUNET_SYSERR;
1191   }
1192   if (NULL == chn->grp || GNUNET_NO != chn->grp->is_origin)
1193   {
1194     GNUNET_break_op (0);
1195     return GNUNET_SYSERR;
1196   }
1197   switch (chn->join_status)
1198   {
1199   case JOIN_REFUSED:
1200     return GNUNET_SYSERR;
1201
1202   case JOIN_ADMITTED:
1203     return GNUNET_OK;
1204
1205   case JOIN_NOT_ASKED:
1206   case JOIN_WAITING:
1207     break;
1208   }
1209
1210   struct MulticastJoinDecisionMessageHeader *
1211     hdcsn = GNUNET_malloc (sizeof (*hdcsn) + size);
1212   hdcsn->peer = chn->peer;
1213   memcpy (&hdcsn[1], dcsn, sizeof (*hdcsn) + size);
1214
1215   struct Member *mem = (struct Member *) chn->grp;
1216   client_send_join_decision (mem, hdcsn);
1217   GNUNET_free (hdcsn);
1218   if (GNUNET_YES == ntohs (dcsn->is_admitted))
1219   {
1220     chn->join_status = JOIN_ADMITTED;
1221     return GNUNET_OK;
1222   }
1223   else
1224   {
1225     chn->join_status = JOIN_REFUSED;
1226     return GNUNET_SYSERR;
1227   }
1228 }
1229
1230 /**
1231  * Incoming multicast message from CADET.
1232  */
1233 int
1234 cadet_recv_message (void *cls,
1235                     struct GNUNET_CADET_Channel *channel,
1236                     void **ctx,
1237                     const struct GNUNET_MessageHeader *m)
1238 {
1239   const struct GNUNET_MULTICAST_MessageHeader *
1240     msg = (const struct GNUNET_MULTICAST_MessageHeader *) m;
1241   uint16_t size = ntohs (m->size);
1242   if (size < sizeof (*msg))
1243   {
1244     GNUNET_break_op (0);
1245     return GNUNET_SYSERR;
1246   }
1247   struct Channel *chn = *ctx;
1248   if (NULL == chn)
1249   {
1250     GNUNET_break_op (0);
1251     return GNUNET_SYSERR;
1252   }
1253   if (ntohl (msg->purpose.size) != (size
1254                                     - sizeof (msg->header)
1255                                     - sizeof (msg->hop_counter)
1256                                     - sizeof (msg->signature)))
1257   {
1258     GNUNET_break_op (0);
1259     return GNUNET_SYSERR;
1260   }
1261   if (GNUNET_OK !=
1262       GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE,
1263                                   &msg->purpose, &msg->signature,
1264                                   &chn->group_key))
1265   {
1266     GNUNET_break_op (0);
1267     return GNUNET_SYSERR;
1268   }
1269
1270   client_send_all (&chn->group_key_hash, m);
1271   return GNUNET_OK;
1272 }
1273
1274
1275 /**
1276  * Incoming multicast request message from CADET.
1277  */
1278 int
1279 cadet_recv_request (void *cls,
1280                     struct GNUNET_CADET_Channel *channel,
1281                     void **ctx,
1282                     const struct GNUNET_MessageHeader *m)
1283 {
1284   const struct GNUNET_MULTICAST_RequestHeader *
1285     req = (const struct GNUNET_MULTICAST_RequestHeader *) m;
1286   uint16_t size = ntohs (m->size);
1287   if (size < sizeof (*req))
1288   {
1289     GNUNET_break_op (0);
1290     return GNUNET_SYSERR;
1291   }
1292   struct Channel *chn = *ctx;
1293   if (NULL == chn)
1294   {
1295     GNUNET_break_op (0);
1296     return GNUNET_SYSERR;
1297   }
1298   if (ntohl (req->purpose.size) != (size
1299                                     - sizeof (req->header)
1300                                     - sizeof (req->member_key)
1301                                     - sizeof (req->signature)))
1302   {
1303     GNUNET_break_op (0);
1304     return GNUNET_SYSERR;
1305   }
1306   if (GNUNET_OK !=
1307       GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST,
1308                                   &req->purpose, &req->signature,
1309                                   &req->member_key))
1310   {
1311     GNUNET_break_op (0);
1312     return GNUNET_SYSERR;
1313   }
1314
1315   client_send_origin (&chn->group_key_hash, m);
1316   return GNUNET_OK;
1317 }
1318
1319
1320 /**
1321  * Message handlers for CADET.
1322  */
1323 static const struct GNUNET_CADET_MessageHandler cadet_handlers[] = {
1324   { &cadet_recv_join_request, GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST, 0 },
1325   { &cadet_recv_message, GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE, 0 },
1326   { &cadet_recv_request, GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST, 0 },
1327   { NULL, 0, 0 }
1328 };
1329
1330
1331 /**
1332  * Listening ports for CADET.
1333  */
1334 static const uint32_t cadet_ports[] = { GNUNET_APPLICATION_TYPE_MULTICAST, 0 };
1335
1336
1337 /**
1338  * Connected to core service.
1339  */
1340 static void
1341 core_connected_cb  (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1342 {
1343   this_peer = *my_identity;
1344
1345   stats = GNUNET_STATISTICS_create ("multicast", cfg);
1346   origins = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1347   members = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1348   group_members = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
1349   channels_in = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1350   channels_out = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
1351
1352   cadet = GNUNET_CADET_connect (cfg, NULL,
1353                                 &cadet_notify_channel_new,
1354                                 &cadet_notify_channel_end,
1355                                 cadet_handlers, cadet_ports);
1356
1357   nc = GNUNET_SERVER_notification_context_create (server, 1);
1358   GNUNET_SERVER_add_handlers (server, server_handlers);
1359   GNUNET_SERVER_disconnect_notify (server, &client_notify_disconnect, NULL);
1360
1361   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
1362                                 NULL);
1363 }
1364
1365
1366 /**
1367  * Service started.
1368  *
1369  * @param cls closure
1370  * @param server the initialized server
1371  * @param cfg configuration to use
1372  */
1373 static void
1374 run (void *cls, struct GNUNET_SERVER_Handle *srv,
1375      const struct GNUNET_CONFIGURATION_Handle *c)
1376 {
1377   cfg = c;
1378   server = srv;
1379   GNUNET_SERVER_connect_notify (server, &client_notify_connect, NULL);
1380   core = GNUNET_CORE_connect (cfg, NULL, &core_connected_cb, NULL, NULL,
1381                               NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
1382 }
1383
1384
1385 /**
1386  * The main function for the multicast service.
1387  *
1388  * @param argc number of arguments from the command line
1389  * @param argv command line arguments
1390  * @return 0 ok, 1 on error
1391  */
1392 int
1393 main (int argc, char *const *argv)
1394 {
1395   return (GNUNET_OK ==
1396           GNUNET_SERVICE_run (argc, argv, "multicast",
1397                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
1398 }
1399
1400 /* end of gnunet-service-multicast.c */