-ignore
[oweals/gnunet.git] / src / multicast / gnunet-service-multicast.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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_statistics_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_multicast_service.h"
32 #include "multicast.h"
33
34 /**
35  * Handle to our current configuration.
36  */
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 /**
40  * Server handle.
41  */
42 static struct GNUNET_SERVER_Handle *server;
43
44 /**
45  * Core handle.
46  * Only used during initialization.
47  */
48 static struct GNUNET_CORE_Handle *core;
49
50 /**
51  * Identity of this peer.
52  */
53 static struct GNUNET_PeerIdentity this_peer;
54
55 /**
56  * Handle to the statistics service.
57  */
58 static struct GNUNET_STATISTICS_Handle *stats;
59 /**
60  * Notification context, simplifies client broadcasts.
61  */
62 static struct GNUNET_SERVER_NotificationContext *nc;
63
64 /**
65  * All connected origins.
66  * Group's pub_key_hash -> struct Origin
67  */
68 static struct GNUNET_CONTAINER_MultiHashMap *origins;
69
70 /**
71  * All connected members.
72  * Group's pub_key_hash -> struct Member
73  */
74 static struct GNUNET_CONTAINER_MultiHashMap *members;
75
76 /**
77  * Connected members per group.
78  * Group's pub_key_hash -> Member's pub_key -> struct Member
79  */
80 static struct GNUNET_CONTAINER_MultiHashMap *group_members;
81
82 /**
83  * List of connected clients.
84  */
85 struct ClientList
86 {
87   struct ClientList *prev;
88   struct ClientList *next;
89   struct GNUNET_SERVER_Client *client;
90 };
91
92 /**
93  * Common part of the client context for both an origin and member.
94  */
95 struct Group
96 {
97   struct ClientList *clients_head;
98   struct ClientList *clients_tail;
99
100   /**
101    * Public key of the group.
102    */
103   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
104
105   /**
106    * Hash of @a pub_key.
107    */
108   struct GNUNET_HashCode pub_key_hash;
109
110   /**
111    * Is this an origin (#GNUNET_YES), or member (#GNUNET_NO)?
112    */
113   uint8_t is_origin;
114
115   /**
116    * Is the client disconnected? #GNUNET_YES or #GNUNET_NO
117    */
118   uint8_t disconnected;
119 };
120
121
122 /**
123  * Client context for a group's origin.
124  */
125 struct Origin
126 {
127   struct Group grp;
128
129   /**
130    * Private key of the group.
131    */
132   struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
133
134   /**
135    * Last message fragment ID sent to the group.
136    */
137   uint64_t max_fragment_id;
138 };
139
140
141 /**
142  * Client context for a group member.
143  */
144 struct Member
145 {
146   struct Group grp;
147
148   /**
149    * Private key of the member.
150    */
151   struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
152
153   /**
154    * Public key of the member.
155    */
156   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
157
158   /**
159    * Hash of @a pub_key.
160    */
161   struct GNUNET_HashCode pub_key_hash;
162
163   /**
164    * Join request sent to the origin / members.
165    */
166   struct MulticastJoinRequestMessage *join_req;
167
168   /**
169    * Join decision sent in reply to our request.
170    *
171    * Only a positive decision is stored here, in case of a negative decision the
172    * client is disconnected.
173    */
174   struct MulticastJoinDecisionMessageHeader *join_dcsn;
175
176   /**
177    * Last request fragment ID sent to the origin.
178    */
179   uint64_t max_fragment_id;
180 };
181
182
183 /**
184  * Task run during shutdown.
185  *
186  * @param cls unused
187  * @param tc unused
188  */
189 static void
190 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
191 {
192   if (NULL != core)
193   {
194     GNUNET_CORE_disconnect (core);
195     core = NULL;
196   }
197   if (NULL != stats)
198   {
199     GNUNET_STATISTICS_destroy (stats, GNUNET_YES);
200     stats = NULL;
201   }
202   /* FIXME: do more clean up here */
203 }
204
205 /**
206  * Clean up origin data structures after a client disconnected.
207  */
208 static void
209 cleanup_origin (struct Origin *orig)
210 {
211   struct Group *grp = &orig->grp;
212   GNUNET_CONTAINER_multihashmap_remove (origins, &grp->pub_key_hash, orig);
213 }
214
215
216 /**
217  * Clean up member data structures after a client disconnected.
218  */
219 static void
220 cleanup_member (struct Member *mem)
221 {
222   struct Group *grp = &mem->grp;
223   struct GNUNET_CONTAINER_MultiHashMap *
224     grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members,
225                                                  &grp->pub_key_hash);
226   GNUNET_assert (NULL != grp_mem);
227   GNUNET_CONTAINER_multihashmap_remove (grp_mem, &mem->pub_key_hash, mem);
228
229   if (0 == GNUNET_CONTAINER_multihashmap_size (grp_mem))
230   {
231     GNUNET_CONTAINER_multihashmap_remove (group_members, &grp->pub_key_hash,
232                                           grp_mem);
233     GNUNET_CONTAINER_multihashmap_destroy (grp_mem);
234   }
235   if (NULL != mem->join_dcsn)
236   {
237     GNUNET_free (mem->join_dcsn);
238     mem->join_dcsn = NULL;
239   }
240   GNUNET_CONTAINER_multihashmap_remove (members, &grp->pub_key_hash, mem);
241 }
242
243
244 /**
245  * Clean up group data structures after a client disconnected.
246  */
247 static void
248 cleanup_group (struct Group *grp)
249 {
250   (GNUNET_YES == grp->is_origin)
251     ? cleanup_origin ((struct Origin *) grp)
252     : cleanup_member ((struct Member *) grp);
253
254   GNUNET_free (grp);
255 }
256
257
258 /**
259  * Called whenever a client is disconnected.
260  *
261  * Frees our resources associated with that client.
262  *
263  * @param cls  Closure.
264  * @param client  Client handle.
265  */
266 static void
267 client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
268 {
269   if (NULL == client)
270     return;
271
272   struct Group *grp
273     = GNUNET_SERVER_client_get_user_context (client, struct Group);
274
275   if (NULL == grp)
276   {
277     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
278                 "%p User context is NULL in client_disconnect()\n", grp);
279     GNUNET_assert (0);
280     return;
281   }
282
283   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284               "%p Client (%s) disconnected from group %s\n",
285               grp, (GNUNET_YES == grp->is_origin) ? "origin" : "member",
286               GNUNET_h2s (&grp->pub_key_hash));
287
288   struct ClientList *cl = grp->clients_head;
289   while (NULL != cl)
290   {
291     if (cl->client == client)
292     {
293       GNUNET_CONTAINER_DLL_remove (grp->clients_head, grp->clients_tail, cl);
294       GNUNET_free (cl);
295       break;
296     }
297     cl = cl->next;
298   }
299
300   if (NULL == grp->clients_head)
301   { /* Last client disconnected. */
302 #if FIXME
303     if (NULL != grp->tmit_head)
304     { /* Send pending messages via CADET before cleanup. */
305       transmit_message (grp);
306     }
307     else
308 #endif
309     {
310       cleanup_group (grp);
311     }
312   }
313 }
314
315
316 /**
317  * Send message to all clients connected to the group.
318  */
319 static void
320 message_to_clients (const struct Group *grp,
321                     const struct GNUNET_MessageHeader *msg)
322 {
323   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
324               "%p Sending message to clients.\n", grp);
325
326   struct ClientList *cl = grp->clients_head;
327   while (NULL != cl)
328   {
329     GNUNET_SERVER_notification_context_add (nc, cl->client);
330     GNUNET_SERVER_notification_context_unicast (nc, cl->client, msg, GNUNET_NO);
331     cl = cl->next;
332   }
333 }
334
335
336 /**
337  * Iterator callback for sending a message to origin clients.
338  */
339 static int
340 origin_message_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
341                    void *origin)
342 {
343   const struct GNUNET_MessageHeader *msg = cls;
344   struct Member *orig = origin;
345
346   message_to_clients (&orig->grp, msg);
347   return GNUNET_YES;
348 }
349
350
351 /**
352  * Iterator callback for sending a message to member clients.
353  */
354 static int
355 member_message_cb (void *cls, const struct GNUNET_HashCode *pub_key_hash,
356                    void *member)
357 {
358   const struct GNUNET_MessageHeader *msg = cls;
359   struct Member *mem = member;
360
361   if (NULL != mem->join_dcsn)
362   { /* Only send message to admitted members */
363     message_to_clients (&mem->grp, msg);
364   }
365   return GNUNET_YES;
366 }
367
368
369 /**
370  * Send message to all origin and member clients connected to the group.
371  *
372  * @param grp  The group to send @a msg to.
373  * @param msg  Message to send.
374  */
375 static void
376 message_to_group (struct Group *grp, const struct GNUNET_MessageHeader *msg)
377 {
378   if (origins != NULL)
379     GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
380                                                 origin_message_cb, (void *) msg);
381   if (members != NULL)
382     GNUNET_CONTAINER_multihashmap_get_multiple (members, &grp->pub_key_hash,
383                                                 member_message_cb, (void *) msg);
384 }
385
386
387 /**
388  * Send message to all origin clients connected to the group.
389  *
390  * @param grp  The group to send @a msg to.
391  * @param msg  Message to send.
392  */
393 static void
394 message_to_origin (struct Group *grp, const struct GNUNET_MessageHeader *msg)
395 {
396   if (origins != NULL)
397     GNUNET_CONTAINER_multihashmap_get_multiple (origins, &grp->pub_key_hash,
398                                                 origin_message_cb, (void *) msg);
399 }
400
401
402 /**
403  * Handle a connecting client starting an origin.
404  */
405 static void
406 client_origin_start (void *cls, struct GNUNET_SERVER_Client *client,
407                      const struct GNUNET_MessageHeader *m)
408 {
409   const struct MulticastOriginStartMessage *
410     msg = (const struct MulticastOriginStartMessage *) m;
411
412   struct GNUNET_CRYPTO_EddsaPublicKey pub_key;
413   struct GNUNET_HashCode pub_key_hash;
414
415   GNUNET_CRYPTO_eddsa_key_get_public (&msg->group_key, &pub_key);
416   GNUNET_CRYPTO_hash (&pub_key, sizeof (pub_key), &pub_key_hash);
417
418   struct Origin *
419     orig = GNUNET_CONTAINER_multihashmap_get (origins, &pub_key_hash);
420   struct Group *grp;
421
422   if (NULL == orig)
423   {
424     orig = GNUNET_new (struct Origin);
425     orig->priv_key = msg->group_key;
426     grp = &orig->grp;
427     grp->is_origin = GNUNET_YES;
428     grp->pub_key = pub_key;
429     grp->pub_key_hash = pub_key_hash;
430
431     GNUNET_CONTAINER_multihashmap_put (origins, &grp->pub_key_hash, orig,
432                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
433   }
434   else
435   {
436     grp = &orig->grp;
437   }
438
439   struct ClientList *cl = GNUNET_new (struct ClientList);
440   cl->client = client;
441   GNUNET_CONTAINER_DLL_insert (grp->clients_head, grp->clients_tail, cl);
442
443   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
444               "%p Client connected as origin to group %s.\n",
445               orig, GNUNET_h2s (&grp->pub_key_hash));
446
447   GNUNET_SERVER_client_set_user_context (client, grp);
448   GNUNET_SERVER_receive_done (client, GNUNET_OK);
449 }
450
451
452 /**
453  * Handle a connecting client joining a group.
454  */
455 static void
456 client_member_join (void *cls, struct GNUNET_SERVER_Client *client,
457                     const struct GNUNET_MessageHeader *m)
458 {
459   const struct MulticastMemberJoinMessage *
460     msg = (const struct MulticastMemberJoinMessage *) m;
461
462   struct GNUNET_CRYPTO_EddsaPublicKey mem_pub_key;
463   struct GNUNET_HashCode pub_key_hash, mem_pub_key_hash;
464
465   GNUNET_CRYPTO_eddsa_key_get_public (&msg->member_key, &mem_pub_key);
466   GNUNET_CRYPTO_hash (&mem_pub_key, sizeof (mem_pub_key), &mem_pub_key_hash);
467   GNUNET_CRYPTO_hash (&msg->group_key, sizeof (msg->group_key), &pub_key_hash);
468
469   struct GNUNET_CONTAINER_MultiHashMap *
470     grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members, &pub_key_hash);
471   struct Member *mem = NULL;
472   struct Group *grp;
473
474   if (NULL != grp_mem)
475   {
476     mem = GNUNET_CONTAINER_multihashmap_get (grp_mem, &mem_pub_key_hash);
477   }
478   if (NULL == mem)
479   {
480     mem = GNUNET_new (struct Member);
481     mem->priv_key = msg->member_key;
482     mem->pub_key = mem_pub_key;
483     mem->pub_key_hash = mem_pub_key_hash;
484
485     grp = &mem->grp;
486     grp->is_origin = GNUNET_NO;
487     grp->pub_key = msg->group_key;
488     grp->pub_key_hash = pub_key_hash;
489
490     if (NULL == grp_mem)
491     {
492       grp_mem = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
493       GNUNET_CONTAINER_multihashmap_put (group_members, &grp->pub_key_hash, grp_mem,
494                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
495     }
496     GNUNET_CONTAINER_multihashmap_put (grp_mem, &mem->pub_key_hash, mem,
497                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
498     GNUNET_CONTAINER_multihashmap_put (members, &grp->pub_key_hash, mem,
499                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
500   }
501   else
502   {
503     grp = &mem->grp;
504   }
505
506   struct ClientList *cl = GNUNET_new (struct ClientList);
507   cl->client = client;
508   GNUNET_CONTAINER_DLL_insert (grp->clients_head, grp->clients_tail, cl);
509
510   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511               "%p Client connected to group %s..\n",
512               mem, GNUNET_h2s (&grp->pub_key_hash));
513   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514               "%p ..as member %s.\n",
515               mem, GNUNET_h2s (&mem_pub_key_hash));
516
517   GNUNET_SERVER_client_set_user_context (client, grp);
518
519   if (NULL != mem->join_dcsn)
520   { /* Already got a join decision, send it to client. */
521     GNUNET_SERVER_notification_context_add (nc, client);
522     GNUNET_SERVER_notification_context_unicast (nc, client,
523                                                 (struct GNUNET_MessageHeader *)
524                                                 mem->join_dcsn,
525                                                 GNUNET_NO);
526   }
527   else if (grp->clients_head == grp->clients_tail)
528   { /* First client of the group, send join request. */
529     struct GNUNET_PeerIdentity *relays = (struct GNUNET_PeerIdentity *) &msg[1];
530     uint32_t relay_count = ntohs (msg->relay_count);
531     uint16_t relay_size = relay_count * sizeof (*relays);
532     struct GNUNET_MessageHeader *join_msg = NULL;
533     uint16_t join_msg_size = 0;
534     if (sizeof (*msg) + relay_size + sizeof (struct GNUNET_MessageHeader)
535         <= ntohs (msg->header.size))
536     {
537       join_msg = (struct GNUNET_MessageHeader *)
538         (((char *) &msg[1]) + relay_size);
539       join_msg_size = ntohs (join_msg->size);
540     }
541
542     struct MulticastJoinRequestMessage *
543       req = GNUNET_malloc (sizeof (*req) + join_msg_size);
544     req->header.size = htons (sizeof (*req) + join_msg_size);
545     req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST);
546     req->group_key = grp->pub_key;
547     req->member_peer = this_peer;
548     GNUNET_CRYPTO_eddsa_key_get_public (&mem->priv_key, &req->member_key);
549     if (0 < join_msg_size)
550       memcpy (&req[1], join_msg, join_msg_size);
551
552     req->purpose.size = htonl (sizeof (*req) + join_msg_size
553                                - sizeof (req->header)
554                                - sizeof (req->signature));
555     req->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST);
556
557     if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&mem->priv_key, &req->purpose,
558                                                &req->signature))
559     {
560       /* FIXME: handle error */
561       GNUNET_assert (0);
562     }
563
564     if (NULL != mem->join_req)
565       GNUNET_free (mem->join_req);
566     mem->join_req = req;
567
568     if (GNUNET_YES
569         == GNUNET_CONTAINER_multihashmap_contains (origins, &grp->pub_key_hash))
570     { /* Local origin */
571       message_to_origin (grp, (struct GNUNET_MessageHeader *) mem->join_req);
572     }
573     else
574     {
575       /* FIXME: send join request to remote peers */
576     }
577   }
578   GNUNET_SERVER_receive_done (client, GNUNET_OK);
579 }
580
581
582 /**
583  * Join decision from client.
584  */
585 static void
586 client_join_decision (void *cls, struct GNUNET_SERVER_Client *client,
587                       const struct GNUNET_MessageHeader *m)
588 {
589   struct Group *
590     grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
591   const struct MulticastJoinDecisionMessageHeader *
592     hdcsn = (const struct MulticastJoinDecisionMessageHeader *) m;
593   const struct MulticastJoinDecisionMessage *
594     dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
595
596   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
597               "%p Got join decision from client for group %s..\n",
598               grp, GNUNET_h2s (&grp->pub_key_hash));
599
600   if (GNUNET_YES
601       == GNUNET_CONTAINER_multihashmap_contains (origins, &grp->pub_key_hash))
602   { /* Local origin */
603     struct GNUNET_CONTAINER_MultiHashMap *
604       grp_mem = GNUNET_CONTAINER_multihashmap_get (group_members,
605                                                    &grp->pub_key_hash);
606     if (NULL != grp_mem)
607     {
608       struct GNUNET_HashCode member_key_hash;
609       GNUNET_CRYPTO_hash (&hdcsn->member_key, sizeof (hdcsn->member_key),
610                           &member_key_hash);
611       struct Member *
612         mem = GNUNET_CONTAINER_multihashmap_get (grp_mem, &member_key_hash);
613       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
614                   "%p ..and member %s: %p\n",
615                   grp, GNUNET_h2s (&member_key_hash), mem);
616       if (NULL != mem)
617       {
618         message_to_clients (&mem->grp, (struct GNUNET_MessageHeader *) hdcsn);
619         if (GNUNET_YES == ntohl (dcsn->is_admitted))
620         { /* Member admitted, store join_decision. */
621           uint16_t dcsn_size = ntohs (dcsn->header.size);
622           mem->join_dcsn = GNUNET_malloc (dcsn_size);
623           memcpy (mem->join_dcsn, dcsn, dcsn_size);
624         }
625         else
626         { /* Refused entry, disconnect clients. */
627           struct ClientList *cl = mem->grp.clients_head;
628           while (NULL != cl)
629           {
630             struct GNUNET_SERVER_Client *client = cl->client;
631             cl = cl->next;
632             GNUNET_SERVER_client_disconnect (client);
633           }
634         }
635       }
636     }
637   }
638   else
639   {
640     /* FIXME: send join decision to hdcsn->peer */
641   }
642   GNUNET_SERVER_receive_done (client, GNUNET_OK);
643 }
644
645 /**
646  * Incoming message from a client.
647  */
648 static void
649 client_multicast_message (void *cls, struct GNUNET_SERVER_Client *client,
650                           const struct GNUNET_MessageHeader *m)
651 {
652   struct Group *
653     grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
654   GNUNET_assert (GNUNET_YES == grp->is_origin);
655   struct Origin *orig = (struct Origin *) grp;
656   struct GNUNET_MULTICAST_MessageHeader *
657     msg = (struct GNUNET_MULTICAST_MessageHeader *) m;
658
659   msg->fragment_id = GNUNET_htonll (orig->max_fragment_id++);
660   msg->purpose.size = htonl (sizeof (*msg) + ntohs (m->size)
661                              - sizeof (msg->header)
662                              - sizeof (msg->hop_counter)
663                              - sizeof (msg->signature));
664   msg->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE);
665
666   if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &msg->purpose,
667                                              &msg->signature))
668   {
669     /* FIXME: handle error */
670     GNUNET_assert (0);
671   }
672
673   /* FIXME: send to remote members */
674
675   message_to_group (grp, m);
676   GNUNET_SERVER_receive_done (client, GNUNET_OK);
677 }
678
679
680 /**
681  * Incoming request from a client.
682  */
683 static void
684 client_multicast_request (void *cls, struct GNUNET_SERVER_Client *client,
685                           const struct GNUNET_MessageHeader *m)
686 {
687   struct Group *
688     grp = GNUNET_SERVER_client_get_user_context (client, struct Group);
689   GNUNET_assert (GNUNET_NO == grp->is_origin);
690   struct Member *mem = (struct Member *) grp;
691
692   struct GNUNET_MULTICAST_RequestHeader *
693     req = (struct GNUNET_MULTICAST_RequestHeader *) m;
694
695   req->fragment_id = GNUNET_ntohll (mem->max_fragment_id++);
696
697   req->purpose.size = htonl (sizeof (*req) + ntohs (m->size)
698                              - sizeof (req->header)
699                              - sizeof (req->member_key)
700                              - sizeof (req->signature));
701   req->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_REQUEST);
702
703   if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&mem->priv_key, &req->purpose,
704                                              &req->signature))
705   {
706     /* FIXME: handle error */
707     GNUNET_assert (0);
708   }
709
710   if (GNUNET_YES
711       == GNUNET_CONTAINER_multihashmap_contains (origins, &grp->pub_key_hash))
712   { /* Local origin */
713     message_to_origin (grp, m);
714   }
715   else
716   {
717     /* FIXME: send to remote origin */
718   }
719   GNUNET_SERVER_receive_done (client, GNUNET_OK);
720 }
721
722
723 /**
724  * Core connected.
725  */
726 static void
727 core_connected_cb  (void *cls, const struct GNUNET_PeerIdentity *my_identity)
728 {
729   this_peer = *my_identity;
730
731   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
732     { &client_origin_start, NULL,
733       GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START, 0 },
734
735     { &client_member_join, NULL,
736       GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN, 0 },
737
738     { &client_join_decision, NULL,
739       GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION, 0 },
740
741     { &client_multicast_message, NULL,
742       GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE, 0 },
743
744     { &client_multicast_request, NULL,
745       GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST, 0 },
746
747     {NULL, NULL, 0, 0}
748   };
749
750   stats = GNUNET_STATISTICS_create ("multicast", cfg);
751   origins = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
752   members = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_YES);
753   group_members = GNUNET_CONTAINER_multihashmap_create (1, GNUNET_NO);
754   nc = GNUNET_SERVER_notification_context_create (server, 1);
755
756   GNUNET_SERVER_add_handlers (server, handlers);
757   GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);
758   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
759                                 NULL);
760 }
761
762
763 /**
764  * Service started.
765  *
766  * @param cls closure
767  * @param server the initialized server
768  * @param cfg configuration to use
769  */
770 static void
771 run (void *cls, struct GNUNET_SERVER_Handle *srv,
772      const struct GNUNET_CONFIGURATION_Handle *c)
773 {
774   cfg = c;
775   server = srv;
776   core = GNUNET_CORE_connect (cfg, NULL, core_connected_cb, NULL, NULL,
777                               NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
778 }
779
780
781 /**
782  * The main function for the multicast service.
783  *
784  * @param argc number of arguments from the command line
785  * @param argv command line arguments
786  * @return 0 ok, 1 on error
787  */
788 int
789 main (int argc, char *const *argv)
790 {
791   return (GNUNET_OK ==
792           GNUNET_SERVICE_run (argc, argv, "multicast",
793                               GNUNET_SERVICE_OPTION_NONE, &run, NULL)) ? 0 : 1;
794 }
795
796 /* end of gnunet-service-multicast.c */