ef4cc73e793147f95ed3ec425682a69b3e726cd9
[oweals/gnunet.git] / src / multicast / multicast_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012, 2013 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/multicast_api.c
23  * @brief Multicast service; implements multicast groups using CADET connections.
24  * @author Christian Grothoff
25  * @author Gabor X Toth
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_multicast_service.h"
31 #include "multicast.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "multicast-api",__VA_ARGS__)
34
35
36 /**
37  * Handle for a request to send a message to all multicast group members
38  * (from the origin).
39  */
40 struct GNUNET_MULTICAST_OriginTransmitHandle
41 {
42   GNUNET_MULTICAST_OriginTransmitNotify notify;
43   void *notify_cls;
44   struct GNUNET_MULTICAST_Origin *origin;
45
46   uint64_t message_id;
47   uint64_t group_generation;
48   uint64_t fragment_offset;
49 };
50
51
52 /**
53  * Handle for a message to be delivered from a member to the origin.
54  */
55 struct GNUNET_MULTICAST_MemberTransmitHandle
56 {
57   GNUNET_MULTICAST_MemberTransmitNotify notify;
58   void *notify_cls;
59   struct GNUNET_MULTICAST_Member *member;
60
61   uint64_t request_id;
62   uint64_t fragment_offset;
63 };
64
65
66 struct GNUNET_MULTICAST_Group
67 {
68   /**
69    * Configuration to use.
70    */
71   const struct GNUNET_CONFIGURATION_Handle *cfg;
72
73   /**
74    * Client connection to the service.
75    */
76   struct GNUNET_CLIENT_MANAGER_Connection *client;
77
78   /**
79    * Message to send on reconnect.
80    */
81   struct GNUNET_MessageHeader *connect_msg;
82
83   GNUNET_MULTICAST_JoinRequestCallback join_req_cb;
84   GNUNET_MULTICAST_MembershipTestCallback member_test_cb;
85   GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
86   GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
87   GNUNET_MULTICAST_MessageCallback message_cb;
88   void *cb_cls;
89
90   /**
91    * Function called after disconnected from the service.
92    */
93   GNUNET_ContinuationCallback disconnect_cb;
94
95   /**
96    * Closure for @a disconnect_cb.
97    */
98   void *disconnect_cls;
99
100   /**
101    * Are we currently transmitting a message?
102    */
103   uint8_t in_transmit;
104
105   /**
106    * Is this the origin or a member?
107    */
108   uint8_t is_origin;
109
110   /**
111    * Is this channel in the process of disconnecting from the service?
112    * #GNUNET_YES or #GNUNET_NO
113    */
114   uint8_t is_disconnecting;
115 };
116
117
118 /**
119  * Handle for the origin of a multicast group.
120  */
121 struct GNUNET_MULTICAST_Origin
122 {
123   struct GNUNET_MULTICAST_Group grp;
124   struct GNUNET_MULTICAST_OriginTransmitHandle tmit;
125
126   GNUNET_MULTICAST_RequestCallback request_cb;
127 };
128
129
130 /**
131  * Handle for a multicast group member.
132  */
133 struct GNUNET_MULTICAST_Member
134 {
135   struct GNUNET_MULTICAST_Group grp;
136   struct GNUNET_MULTICAST_MemberTransmitHandle tmit;
137
138   GNUNET_MULTICAST_JoinDecisionCallback join_dcsn_cb;
139
140   uint64_t next_fragment_id;
141 };
142
143
144 /**
145  * Handle that identifies a join request.
146  *
147  * Used to match calls to #GNUNET_MULTICAST_JoinRequestCallback to the
148  * corresponding calls to #GNUNET_MULTICAST_join_decision().
149  */
150 struct GNUNET_MULTICAST_JoinHandle
151 {
152   struct GNUNET_MULTICAST_Group *group;
153
154   /**
155    * Public key of the member requesting join.
156    */
157   struct GNUNET_CRYPTO_EcdsaPublicKey member_key;
158
159   /**
160    * Peer identity of the member requesting join.
161    */
162   struct GNUNET_PeerIdentity peer;
163 };
164
165
166 /**
167  * Handle to pass back for the answer of a membership test.
168  */
169 struct GNUNET_MULTICAST_MembershipTestHandle
170 {
171 };
172
173
174 /**
175  * Opaque handle to a replay request from the multicast service.
176  */
177 struct GNUNET_MULTICAST_ReplayHandle
178 {
179 };
180
181
182 /**
183  * Handle for a replay request.
184  */
185 struct GNUNET_MULTICAST_MemberReplayHandle
186 {
187 };
188
189
190 /**
191  * Send first message to the service after connecting.
192  */
193 static void
194 group_send_connect_msg (struct GNUNET_MULTICAST_Group *grp)
195 {
196   uint16_t cmsg_size = ntohs (grp->connect_msg->size);
197   struct GNUNET_MessageHeader * cmsg = GNUNET_malloc (cmsg_size);
198   memcpy (cmsg, grp->connect_msg, cmsg_size);
199   GNUNET_CLIENT_MANAGER_transmit_now (grp->client, cmsg);
200 }
201
202
203 /**
204  * Got disconnected from service.  Reconnect.
205  */
206 static void
207 group_recv_disconnect (void *cls,
208                         struct GNUNET_CLIENT_MANAGER_Connection *client,
209                         const struct GNUNET_MessageHeader *msg)
210 {
211   struct GNUNET_MULTICAST_Group *
212     grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
213   GNUNET_CLIENT_MANAGER_reconnect (client);
214   group_send_connect_msg (grp);
215 }
216
217
218 /**
219  * Receive join request from service.
220  */
221 static void
222 group_recv_join_request (void *cls,
223                           struct GNUNET_CLIENT_MANAGER_Connection *client,
224                           const struct GNUNET_MessageHeader *msg)
225 {
226   struct GNUNET_MULTICAST_Group *grp;
227   const struct MulticastJoinRequestMessage *jreq;
228   struct GNUNET_MULTICAST_JoinHandle *jh;
229   const struct GNUNET_MessageHeader *jmsg;
230
231   grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
232   if (NULL == grp)
233   {
234     GNUNET_break (0);
235     return;
236   }
237   if (NULL == grp->join_req_cb)
238     return;
239   /* FIXME: this fails to check that 'msg' is well-formed! */
240   jreq = (const struct MulticastJoinRequestMessage *) msg;
241   if (sizeof (*jreq) + sizeof (*jmsg) <= ntohs (jreq->header.size))
242     jmsg = (const struct GNUNET_MessageHeader *) &jreq[1];
243   else
244     jmsg = NULL;
245   jh = GNUNET_malloc (sizeof (*jh));
246   jh->group = grp;
247   jh->member_key = jreq->member_key;
248   jh->peer = jreq->peer;
249   grp->join_req_cb (grp->cb_cls, &jreq->member_key, jmsg, jh);
250 }
251
252
253 /**
254  * Receive multicast message from service.
255  */
256 static void
257 group_recv_message (void *cls,
258                     struct GNUNET_CLIENT_MANAGER_Connection *client,
259                     const struct GNUNET_MessageHeader *msg)
260 {
261   struct GNUNET_MULTICAST_Group *
262     grp = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
263   struct GNUNET_MULTICAST_MessageHeader *
264     mmsg = (struct GNUNET_MULTICAST_MessageHeader *) msg;
265
266   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267               "Calling message callback with a message of size %u.\n",
268               ntohs (mmsg->header.size));
269
270   if (GNUNET_YES != grp->is_disconnecting && NULL != grp->message_cb)
271     grp->message_cb (grp->cb_cls, mmsg);
272 }
273
274
275 /**
276  * Origin receives uniquest request from a member.
277  */
278 static void
279 origin_recv_request (void *cls,
280                      struct GNUNET_CLIENT_MANAGER_Connection *client,
281                      const struct GNUNET_MessageHeader *msg)
282 {
283   struct GNUNET_MULTICAST_Group *grp;
284   struct GNUNET_MULTICAST_Origin *
285     orig = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
286   grp = &orig->grp;
287   struct GNUNET_MULTICAST_RequestHeader *
288     req = (struct GNUNET_MULTICAST_RequestHeader *) msg;
289
290   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291               "Calling request callback with a request of size %u.\n",
292               ntohs (req->header.size));
293
294   if (NULL != orig->request_cb)
295     orig->request_cb (grp->cb_cls, req);
296 }
297
298
299 /**
300  * Member receives join decision.
301  */
302 static void
303 member_recv_join_decision (void *cls,
304                            struct GNUNET_CLIENT_MANAGER_Connection *client,
305                            const struct GNUNET_MessageHeader *msg)
306 {
307   struct GNUNET_MULTICAST_Group *grp;
308   struct GNUNET_MULTICAST_Member *
309     mem = GNUNET_CLIENT_MANAGER_get_user_context_ (client, sizeof (*grp));
310   grp = &mem->grp;
311
312   const struct MulticastJoinDecisionMessageHeader *
313     hdcsn = (const struct MulticastJoinDecisionMessageHeader *) msg;
314   const struct MulticastJoinDecisionMessage *
315     dcsn = (const struct MulticastJoinDecisionMessage *) &hdcsn[1];
316
317   uint16_t dcsn_size = ntohs (dcsn->header.size);
318   int is_admitted = ntohl (dcsn->is_admitted);
319
320   LOG (GNUNET_ERROR_TYPE_DEBUG,
321        "%p Member got join decision from multicast: %d\n",
322        mem, is_admitted);
323
324   const struct GNUNET_MessageHeader *join_resp = NULL;
325   uint16_t join_resp_size = 0;
326
327   uint16_t relay_count = ntohl (dcsn->relay_count);
328   const struct GNUNET_PeerIdentity *relays = NULL;
329   uint16_t relay_size = relay_count * sizeof (*relays);
330   if (0 < relay_count)
331   {
332     if (dcsn_size < sizeof (*dcsn) + relay_size)
333     {
334       GNUNET_break_op (0);
335       is_admitted = GNUNET_SYSERR;
336     }
337     else
338     {
339       relays = (struct GNUNET_PeerIdentity *) &dcsn[1];
340     }
341   }
342
343   if (sizeof (*dcsn) + relay_size + sizeof (*join_resp) <= dcsn_size)
344   {
345     join_resp = (const struct GNUNET_MessageHeader *) ((char *) &dcsn[1] + relay_size);
346     join_resp_size = ntohs (join_resp->size);
347   }
348   if (dcsn_size < sizeof (*dcsn) + relay_size + join_resp_size)
349   {
350     LOG (GNUNET_ERROR_TYPE_DEBUG,
351          "Received invalid join decision message from multicast: %u < %u + %u + %u\n",
352          dcsn_size , sizeof (*dcsn), relay_size, join_resp_size);
353     GNUNET_break_op (0);
354     is_admitted = GNUNET_SYSERR;
355   }
356
357   if (NULL != mem->join_dcsn_cb)
358     mem->join_dcsn_cb (grp->cb_cls, is_admitted, &hdcsn->peer,
359                        relay_count, relays, join_resp);
360
361   // FIXME:
362   //if (GNUNET_YES != is_admitted)
363   //  GNUNET_MULTICAST_member_part (mem);
364 }
365
366
367 /**
368  * Message handlers for an origin.
369  */
370 static struct GNUNET_CLIENT_MANAGER_MessageHandler origin_handlers[] =
371 {
372   { &group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
373
374   { &group_recv_message, NULL,
375     GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
376     sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
377
378   { &origin_recv_request, NULL,
379     GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST,
380     sizeof (struct GNUNET_MULTICAST_RequestHeader), GNUNET_YES },
381
382   { &group_recv_join_request, NULL,
383     GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
384     sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
385
386   { NULL, NULL, 0, 0, GNUNET_NO }
387 };
388
389
390 /**
391  * Message handlers for a member.
392  */
393 static struct GNUNET_CLIENT_MANAGER_MessageHandler member_handlers[] =
394 {
395   { &group_recv_disconnect, NULL, 0, 0, GNUNET_NO },
396
397   { &group_recv_message, NULL,
398     GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE,
399     sizeof (struct GNUNET_MULTICAST_MessageHeader), GNUNET_YES },
400
401   { &group_recv_join_request, NULL,
402     GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_REQUEST,
403     sizeof (struct MulticastJoinRequestMessage), GNUNET_YES },
404
405   { &member_recv_join_decision, NULL,
406     GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION,
407     sizeof (struct MulticastJoinDecisionMessage), GNUNET_YES },
408
409   { NULL, NULL, 0, 0, GNUNET_NO }
410 };
411
412
413 static void
414 group_cleanup (struct GNUNET_MULTICAST_Group *grp)
415 {
416   GNUNET_free (grp->connect_msg);
417   if (NULL != grp->disconnect_cb)
418     grp->disconnect_cb (grp->disconnect_cls);
419 }
420
421
422 static void
423 origin_cleanup (void *cls)
424 {
425   struct GNUNET_MULTICAST_Origin *orig = cls;
426   group_cleanup (&orig->grp);
427   GNUNET_free (orig);
428 }
429
430
431 static void
432 member_cleanup (void *cls)
433 {
434   struct GNUNET_MULTICAST_Member *mem = cls;
435   group_cleanup (&mem->grp);
436   GNUNET_free (mem);
437 }
438
439
440 /**
441  * Function to call with the decision made for a join request.
442  *
443  * Must be called once and only once in response to an invocation of the
444  * #GNUNET_MULTICAST_JoinRequestCallback.
445  *
446  * @param join  Join request handle.
447  * @param is_admitted  #GNUNET_YES    if the join is approved,
448  *                     #GNUNET_NO     if it is disapproved,
449  *                     #GNUNET_SYSERR if we cannot answer the request.
450  * @param relay_count Number of relays given.
451  * @param relays Array of suggested peers that might be useful relays to use
452  *        when joining the multicast group (essentially a list of peers that
453  *        are already part of the multicast group and might thus be willing
454  *        to help with routing).  If empty, only this local peer (which must
455  *        be the multicast origin) is a good candidate for building the
456  *        multicast tree.  Note that it is unnecessary to specify our own
457  *        peer identity in this array.
458  * @param join_resp  Message to send in response to the joining peer;
459  *        can also be used to redirect the peer to a different group at the
460  *        application layer; this response is to be transmitted to the
461  *        peer that issued the request even if admission is denied.
462  */
463 struct GNUNET_MULTICAST_ReplayHandle *
464 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *join,
465                                 int is_admitted,
466                                 uint16_t relay_count,
467                                 const struct GNUNET_PeerIdentity *relays,
468                                 const struct GNUNET_MessageHeader *join_resp)
469 {
470   struct GNUNET_MULTICAST_Group *grp = join->group;
471   uint16_t join_resp_size = (NULL != join_resp) ? ntohs (join_resp->size) : 0;
472   uint16_t relay_size = relay_count * sizeof (*relays);
473
474   struct MulticastJoinDecisionMessageHeader * hdcsn;
475   struct MulticastJoinDecisionMessage *dcsn;
476   hdcsn = GNUNET_malloc (sizeof (*hdcsn) + sizeof (*dcsn)
477                          + relay_size + join_resp_size);
478   hdcsn->header.size = htons (sizeof (*hdcsn) + sizeof (*dcsn)
479                               + relay_size + join_resp_size);
480   hdcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
481   hdcsn->member_key = join->member_key;
482   hdcsn->peer = join->peer;
483
484   dcsn = (struct MulticastJoinDecisionMessage *) &hdcsn[1];
485   dcsn->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_JOIN_DECISION);
486   dcsn->header.size = htons (sizeof (*dcsn) + relay_size + join_resp_size);
487   dcsn->is_admitted = htonl (is_admitted);
488   dcsn->relay_count = htonl (relay_count);
489   if (0 < relay_size)
490     memcpy (&dcsn[1], relays, relay_size);
491   if (0 < join_resp_size)
492     memcpy (((char *) &dcsn[1]) + relay_size, join_resp, join_resp_size);
493
494   GNUNET_CLIENT_MANAGER_transmit (grp->client, &hdcsn->header);
495   GNUNET_free (join);
496   return NULL;
497 }
498
499
500 /**
501  * Call informing multicast about the decision taken for a membership test.
502  *
503  * @param mth Handle that was given for the query.
504  * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
505  *        #GNUNET_SYSERR if we cannot answer the membership test.
506  */
507 void
508 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
509                                          int result)
510 {
511 }
512
513
514 /**
515  * Replay a message fragment for the multicast group.
516  *
517  * @param rh Replay handle identifying which replay operation was requested.
518  * @param msg Replayed message fragment, NULL if unknown/error.
519  * @param ec Error code.
520  */
521 void
522 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
523                                   const struct GNUNET_MessageHeader *msg,
524                                   enum GNUNET_MULTICAST_ReplayErrorCode ec)
525 {
526 }
527
528
529 /**
530  * Indicate the end of the replay session.
531  *
532  * Invalidates the replay handle.
533  *
534  * @param rh Replay session to end.
535  */
536 void
537 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
538 {
539 }
540
541
542 /**
543  * Replay a message for the multicast group.
544  *
545  * @param rh Replay handle identifying which replay operation was requested.
546  * @param notify Function to call to get the message.
547  * @param notify_cls Closure for @a notify.
548  */
549 void
550 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
551                                    GNUNET_MULTICAST_ReplayTransmitNotify notify,
552                                    void *notify_cls)
553 {
554 }
555
556
557 /**
558  * Start a multicast group.
559  *
560  * Will advertise the origin in the P2P overlay network under the respective
561  * public key so that other peer can find this peer to join it.  Peers that
562  * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
563  * either an existing group member or to the origin.  If the joining is
564  * approved, the member is cleared for @e replay and will begin to receive
565  * messages transmitted to the group.  If joining is disapproved, the failed
566  * candidate will be given a response.  Members in the group can send messages
567  * to the origin (one at a time).
568  *
569  * @param cfg  Configuration to use.
570  * @param priv_key  ECC key that will be used to sign messages for this
571  *        multicast session; public key is used to identify the multicast group;
572  * @param max_fragment_id  Maximum fragment ID already sent to the group.
573  *        0 for a new group.
574  * @param join_request_cb Function called to approve / disapprove joining of a peer.
575  * @param member_test_cb  Function multicast can use to test group membership.
576  * @param replay_frag_cb  Function that can be called to replay a message fragment.
577  * @param replay_msg_cb  Function that can be called to replay a message.
578  * @param request_cb  Function called with message fragments from group members.
579  * @param message_cb  Function called with the message fragments sent to the
580  *        network by GNUNET_MULTICAST_origin_to_all().  These message fragments
581  *        should be stored for answering replay requests later.
582  * @param cls  Closure for the various callbacks that follow.
583  *
584  * @return Handle for the origin, NULL on error.
585  */
586 struct GNUNET_MULTICAST_Origin *
587 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
588                                const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
589                                uint64_t max_fragment_id,
590                                GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
591                                GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
592                                GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
593                                GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
594                                GNUNET_MULTICAST_RequestCallback request_cb,
595                                GNUNET_MULTICAST_MessageCallback message_cb,
596                                void *cls)
597 {
598   struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
599   struct GNUNET_MULTICAST_Group *grp = &orig->grp;
600   struct MulticastOriginStartMessage *start = GNUNET_malloc (sizeof (*start));
601
602   start->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_ORIGIN_START);
603   start->header.size = htons (sizeof (*start));
604   start->max_fragment_id = max_fragment_id;
605   memcpy (&start->group_key, priv_key, sizeof (*priv_key));
606
607   grp->connect_msg = (struct GNUNET_MessageHeader *) start;
608   grp->is_origin = GNUNET_YES;
609   grp->cfg = cfg;
610
611   grp->cb_cls = cls;
612   grp->join_req_cb = join_request_cb;
613   grp->member_test_cb = member_test_cb;
614   grp->replay_frag_cb = replay_frag_cb;
615   grp->replay_msg_cb = replay_msg_cb;
616   grp->message_cb = message_cb;
617
618   orig->request_cb = request_cb;
619
620   grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", origin_handlers);
621   GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, orig, sizeof (*grp));
622   group_send_connect_msg (grp);
623
624   return orig;
625 }
626
627
628 /**
629  * Stop a multicast group.
630  *
631  * @param origin Multicast group to stop.
632  */
633 void
634 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *orig,
635                               GNUNET_ContinuationCallback stop_cb,
636                               void *stop_cls)
637 {
638   struct GNUNET_MULTICAST_Group *grp = &orig->grp;
639
640   grp->is_disconnecting = GNUNET_YES;
641   grp->disconnect_cb = stop_cb;
642   grp->disconnect_cls = stop_cls;
643
644   GNUNET_CLIENT_MANAGER_disconnect (orig->grp.client, GNUNET_YES,
645                                     &origin_cleanup, orig);
646 }
647
648
649 static void
650 origin_to_all (struct GNUNET_MULTICAST_Origin *orig)
651 {
652   LOG (GNUNET_ERROR_TYPE_DEBUG, "origin_to_all()\n");
653   struct GNUNET_MULTICAST_Group *grp = &orig->grp;
654   struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
655
656   size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
657   struct GNUNET_MULTICAST_MessageHeader *msg = GNUNET_malloc (buf_size);
658   int ret = tmit->notify (tmit->notify_cls, &buf_size, &msg[1]);
659
660   if (! (GNUNET_YES == ret || GNUNET_NO == ret)
661       || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
662   {
663     LOG (GNUNET_ERROR_TYPE_ERROR,
664          "OriginTransmitNotify() returned error or invalid message size.\n");
665     /* FIXME: handle error */
666     GNUNET_free (msg);
667     return;
668   }
669
670   if (GNUNET_NO == ret && 0 == buf_size)
671   {
672     GNUNET_free (msg);
673     return; /* Transmission paused. */
674   }
675
676   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
677   msg->header.size = htons (sizeof (*msg) + buf_size);
678   msg->message_id = GNUNET_htonll (tmit->message_id);
679   msg->group_generation = tmit->group_generation;
680   msg->fragment_offset = GNUNET_htonll (tmit->fragment_offset);
681   tmit->fragment_offset += sizeof (*msg) + buf_size;
682
683   GNUNET_CLIENT_MANAGER_transmit (grp->client, &msg->header);
684 }
685
686
687 /**
688  * Send a message to the multicast group.
689  *
690  * @param orig  Handle to the multicast group.
691  * @param message_id  Application layer ID for the message.  Opaque to multicast.
692  * @param group_generation  Group generation of the message.
693  *                          Documented in struct GNUNET_MULTICAST_MessageHeader.
694  * @param notify  Function to call to get the message.
695  * @param notify_cls  Closure for @a notify.
696  *
697  * @return Message handle on success,
698  *         NULL on error (i.e. another request is already pending).
699  */
700 struct GNUNET_MULTICAST_OriginTransmitHandle *
701 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *orig,
702                                 uint64_t message_id,
703                                 uint64_t group_generation,
704                                 GNUNET_MULTICAST_OriginTransmitNotify notify,
705                                 void *notify_cls)
706 {
707 /* FIXME
708   if (GNUNET_YES == orig->grp.in_transmit)
709     return NULL;
710   orig->grp.in_transmit = GNUNET_YES;
711 */
712
713   struct GNUNET_MULTICAST_OriginTransmitHandle *tmit = &orig->tmit;
714   tmit->origin = orig;
715   tmit->message_id = message_id;
716   tmit->group_generation = group_generation;
717   tmit->notify = notify;
718   tmit->notify_cls = notify_cls;
719
720   origin_to_all (orig);
721   return tmit;
722 }
723
724
725 /**
726  * Resume message transmission to multicast group.
727  *
728  * @param th  Transmission to cancel.
729  */
730 void
731 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
732 {
733   origin_to_all (th->origin);
734 }
735
736
737 /**
738  * Cancel request for message transmission to multicast group.
739  *
740  * @param th  Transmission to cancel.
741  */
742 void
743 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginTransmitHandle *th)
744 {
745 }
746
747
748 /**
749  * Join a multicast group.
750  *
751  * The entity joining is always the local peer.  Further information about the
752  * candidate can be provided in the @a join_request message.  If the join fails, the
753  * @a message_cb is invoked with a (failure) response and then with NULL.  If
754  * the join succeeds, outstanding (state) messages and ongoing multicast
755  * messages will be given to the @a message_cb until the member decides to part
756  * the group.  The @a test_cb and @a replay_cb functions may be called at
757  * anytime by the multicast service to support relaying messages to other
758  * members of the group.
759  *
760  * @param cfg Configuration to use.
761  * @param group_key ECC public key that identifies the group to join.
762  * @param member_key ECC key that identifies the member and used to sign
763  *        requests sent to the origin.
764  * @param origin Peer ID of the origin to send unicast requsets to.  If NULL,
765  *        unicast requests are sent back via multiple hops on the reverse path
766  *        of multicast messages.
767  * @param relay_count Number of peers in the @a relays array.
768  * @param relays Peer identities of members of the group, which serve as relays
769  *        and can be used to join the group at. and send the @a join_request to.
770  *        If empty, the @a join_request is sent directly to the @a origin.
771  * @param join_msg  Application-dependent join message to be passed to the peer
772  *        @a origin.
773  * @param join_request_cb Function called to approve / disapprove joining of a peer.
774  * @param join_decision_cb Function called to inform about the join decision.
775  * @param member_test_cb Function multicast can use to test group membership.
776  * @param replay_frag_cb Function that can be called to replay message fragments
777  *        this peer already knows from this group. NULL if this
778  *        client is unable to support replay.
779  * @param replay_msg_cb Function that can be called to replay message fragments
780  *        this peer already knows from this group. NULL if this
781  *        client is unable to support replay.
782  * @param message_cb Function to be called for all message fragments we
783  *        receive from the group, excluding those our @a replay_cb
784  *        already has.
785  * @param cls Closure for callbacks.
786  * @return Handle for the member, NULL on error.
787  */
788 struct GNUNET_MULTICAST_Member *
789 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
790                               const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
791                               const struct GNUNET_CRYPTO_EcdsaPrivateKey *member_key,
792                               const struct GNUNET_PeerIdentity *origin,
793                               uint16_t relay_count,
794                               const struct GNUNET_PeerIdentity *relays,
795                               const struct GNUNET_MessageHeader *join_msg,
796                               GNUNET_MULTICAST_JoinRequestCallback join_request_cb,
797                               GNUNET_MULTICAST_JoinDecisionCallback join_decision_cb,
798                               GNUNET_MULTICAST_MembershipTestCallback member_test_cb,
799                               GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
800                               GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
801                               GNUNET_MULTICAST_MessageCallback message_cb,
802                               void *cls)
803 {
804   struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
805   struct GNUNET_MULTICAST_Group *grp = &mem->grp;
806
807   uint16_t relay_size = relay_count * sizeof (*relays);
808   uint16_t join_msg_size = (NULL != join_msg) ? ntohs (join_msg->size) : 0;
809   struct MulticastMemberJoinMessage *
810     join = GNUNET_malloc (sizeof (*join) + relay_size + join_msg_size);
811   join->header.size = htons (sizeof (*join) + relay_size + join_msg_size);
812   join->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MEMBER_JOIN);
813   join->group_key = *group_key;
814   join->member_key = *member_key;
815   join->origin = *origin;
816   join->relay_count = ntohl (relay_count);
817   if (0 < relay_size)
818     memcpy (&join[1], relays, relay_size);
819   if (0 < join_msg_size)
820     memcpy (((char *) &join[1]) + relay_size, join_msg, join_msg_size);
821
822   grp->connect_msg = (struct GNUNET_MessageHeader *) join;
823   grp->is_origin = GNUNET_NO;
824   grp->cfg = cfg;
825
826   mem->join_dcsn_cb = join_decision_cb;
827   grp->join_req_cb = join_request_cb;
828   grp->member_test_cb = member_test_cb;
829   grp->replay_frag_cb = replay_frag_cb;
830   grp->message_cb = message_cb;
831   grp->cb_cls = cls;
832
833   grp->client = GNUNET_CLIENT_MANAGER_connect (cfg, "multicast", member_handlers);
834   GNUNET_CLIENT_MANAGER_set_user_context_ (grp->client, mem, sizeof (*grp));
835   group_send_connect_msg (grp);
836
837   return mem;
838 }
839
840
841 /**
842  * Part a multicast group.
843  *
844  * Disconnects from all group members and invalidates the @a member handle.
845  *
846  * An application-dependent part message can be transmitted beforehand using
847  * #GNUNET_MULTICAST_member_to_origin())
848  *
849  * @param member Membership handle.
850  */
851 void
852 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *mem,
853                               GNUNET_ContinuationCallback part_cb,
854                               void *part_cls)
855 {
856   struct GNUNET_MULTICAST_Group *grp = &mem->grp;
857
858   grp->is_disconnecting = GNUNET_YES;
859   grp->disconnect_cb = part_cb;
860   grp->disconnect_cls = part_cls;
861
862   GNUNET_CLIENT_MANAGER_disconnect (mem->grp.client, GNUNET_YES,
863                                     member_cleanup, mem);
864 }
865
866
867 /**
868  * Request a fragment to be replayed by fragment ID.
869  *
870  * Useful if messages below the @e max_known_fragment_id given when joining are
871  * needed and not known to the client.
872  *
873  * @param member Membership handle.
874  * @param fragment_id ID of a message fragment that this client would like to
875           see replayed.
876  * @param flags Additional flags for the replay request.  It is used and defined
877  *        by the replay callback.  FIXME: which replay callback? FIXME: use enum?
878  *        FIXME: why not pass reply cb here?
879  * @return Replay request handle, NULL on error.
880  */
881 struct GNUNET_MULTICAST_MemberReplayHandle *
882 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
883                                          uint64_t fragment_id,
884                                          uint64_t flags)
885 {
886   return NULL;
887 }
888
889
890 /**
891  * Request a message fragment to be replayed.
892  *
893  * Useful if messages below the @e max_known_fragment_id given when joining are
894  * needed and not known to the client.
895  *
896  * @param member Membership handle.
897  * @param message_id ID of the message this client would like to see replayed.
898  * @param fragment_offset Offset of the fragment within the message to replay.
899  * @param flags Additional flags for the replay request.  It is used & defined
900  *        by the replay callback.
901  * @param result_cb Function to be called for the replayed message.
902  * @param result_cb_cls Closure for @a result_cb.
903  * @return Replay request handle, NULL on error.
904  */
905 struct GNUNET_MULTICAST_MemberReplayHandle *
906 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
907                                         uint64_t message_id,
908                                         uint64_t fragment_offset,
909                                         uint64_t flags,
910                                         GNUNET_MULTICAST_ResultCallback result_cb,
911                                         void *result_cb_cls)
912 {
913   return NULL;
914 }
915
916
917 /**
918  * Cancel a replay request.
919  *
920  * @param rh Request to cancel.
921  */
922 void
923 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
924 {
925 }
926
927
928 static void
929 member_to_origin (struct GNUNET_MULTICAST_Member *mem)
930 {
931   LOG (GNUNET_ERROR_TYPE_DEBUG, "member_to_origin()\n");
932   struct GNUNET_MULTICAST_Group *grp = &mem->grp;
933   struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
934
935   size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
936   struct GNUNET_MULTICAST_RequestHeader *req = GNUNET_malloc (buf_size);
937   int ret = tmit->notify (tmit->notify_cls, &buf_size, &req[1]);
938
939   if (! (GNUNET_YES == ret || GNUNET_NO == ret)
940       || GNUNET_MULTICAST_FRAGMENT_MAX_SIZE < buf_size)
941   {
942     LOG (GNUNET_ERROR_TYPE_ERROR,
943          "MemberTransmitNotify() returned error or invalid message size.\n");
944     /* FIXME: handle error */
945     GNUNET_free (req);
946     return;
947   }
948
949   if (GNUNET_NO == ret && 0 == buf_size)
950   {
951     /* Transmission paused. */
952     GNUNET_free (req);
953     return;
954   }
955
956   req->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_REQUEST);
957   req->header.size = htons (sizeof (*req) + buf_size);
958   req->request_id = GNUNET_htonll (tmit->request_id);
959   req->fragment_offset = GNUNET_ntohll (tmit->fragment_offset);
960   tmit->fragment_offset += sizeof (*req) + buf_size;
961
962   GNUNET_CLIENT_MANAGER_transmit (grp->client, &req->header);
963 }
964
965
966 /**
967  * Send a message to the origin of the multicast group.
968  *
969  * @param mem Membership handle.
970  * @param request_id Application layer ID for the request.  Opaque to multicast.
971  * @param notify Callback to call to get the message.
972  * @param notify_cls Closure for @a notify.
973  * @return Handle to cancel request, NULL on error (i.e. request already pending).
974  */
975 struct GNUNET_MULTICAST_MemberTransmitHandle *
976 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *mem,
977                                    uint64_t request_id,
978                                    GNUNET_MULTICAST_MemberTransmitNotify notify,
979                                    void *notify_cls)
980 {
981 /* FIXME
982   if (GNUNET_YES == mem->grp.in_transmit)
983     return NULL;
984   mem->grp.in_transmit = GNUNET_YES;
985 */
986
987   struct GNUNET_MULTICAST_MemberTransmitHandle *tmit = &mem->tmit;
988   tmit->member = mem;
989   tmit->request_id = request_id;
990   tmit->notify = notify;
991   tmit->notify_cls = notify_cls;
992
993   member_to_origin (mem);
994   return tmit;
995 }
996
997
998 /**
999  * Resume message transmission to origin.
1000  *
1001  * @param th  Transmission to cancel.
1002  */
1003 void
1004 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1005 {
1006   member_to_origin (th->member);
1007 }
1008
1009
1010 /**
1011  * Cancel request for message transmission to origin.
1012  *
1013  * @param th  Transmission to cancel.
1014  */
1015 void
1016 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberTransmitHandle *th)
1017 {
1018 }
1019
1020
1021 /* end of multicast_api.c */