fa177ec55863b8148866243ea03fa03434172d15
[oweals/gnunet.git] / src / multicast / multicast_api.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file multicast/multicast_api.c
23  * @brief multicast service; establish tunnels to distant peers
24  * @author Christian Grothoff
25  * @author Gabor X Toth
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.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_OriginMessageHandle
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 the origin of a multicast group.
54  */
55 struct GNUNET_MULTICAST_Origin
56 {
57   struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
58   struct GNUNET_MULTICAST_OriginMessageHandle msg_handle;
59
60   GNUNET_MULTICAST_JoinCallback join_cb;
61   GNUNET_MULTICAST_MembershipTestCallback mem_test_cb;
62   GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb;
63   GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb;
64   GNUNET_MULTICAST_RequestCallback request_cb;
65   GNUNET_MULTICAST_MessageCallback message_cb;
66   void *cls;
67
68   uint64_t next_fragment_id;
69 };
70
71
72 /**
73  * Handle for a message to be delivered from a member to the origin.
74  */
75 struct GNUNET_MULTICAST_MemberRequestHandle
76 {
77 };
78
79
80 /**
81  * Opaque handle for a multicast group member.
82  */
83 struct GNUNET_MULTICAST_Member
84 {
85 };
86
87
88 GNUNET_NETWORK_STRUCT_BEGIN
89
90 /**
91  * Header of a request from a member to the origin.
92  */
93 struct GNUNET_MULTICAST_RequestHeader
94 {
95   /**
96    * Header for all requests from a member to the origin.
97    */
98   struct GNUNET_MessageHeader header;
99
100   /**
101    * Public key of the sending member.
102    */
103   struct GNUNET_CRYPTO_EddsaPublicKey member_key;
104
105   /**
106    * ECC signature of the request fragment.
107    *
108    * Signature must match the public key of the multicast group.
109    */
110   struct GNUNET_CRYPTO_EddsaSignature signature;
111
112   /**
113    * Purpose for the signature and size of the signed data.
114    */
115   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
116
117   /**
118    * Number of the request fragment, monotonically increasing.
119    */
120   uint64_t fragment_id GNUNET_PACKED;
121
122   /**
123    * Byte offset of this @e fragment of the @e request.
124    */
125   uint64_t fragment_offset GNUNET_PACKED;
126
127   /**
128    * Number of the request this fragment belongs to.
129    *
130    * Set in GNUNET_MULTICAST_origin_to_all().
131    */
132   uint64_t request_id GNUNET_PACKED;
133
134   /**
135    * Flags for this request.
136    */
137   enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
138
139   /* Followed by request body. */
140 };
141
142 /**
143  * Header of a join request sent to the origin or another member.
144  */
145 struct GNUNET_MULTICAST_JoinRequest
146 {
147   /**
148    * Header for the join request.
149    */
150   struct GNUNET_MessageHeader header;
151
152   /**
153    * ECC signature of the rest of the fields of the join request.
154    *
155    * Signature must match the public key of the joining member.
156    */
157   struct GNUNET_CRYPTO_EddsaSignature signature;
158
159   /**
160    * Purpose for the signature and size of the signed data.
161    */
162   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
163
164   /**
165    * Public key of the target group.
166    */
167   struct GNUNET_CRYPTO_EddsaPublicKey group_key;
168
169   /**
170    * Public key of the joining member.
171    */
172   struct GNUNET_CRYPTO_EddsaPublicKey member_key;
173
174   /**
175    * Peer identity of the joining member.
176    */
177   struct GNUNET_PeerIdentity member_peer;
178
179   /* Followed by request body. */
180 };
181
182 GNUNET_NETWORK_STRUCT_END
183
184
185 /**
186  * Handle that identifies a join request.
187  *
188  * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
189  * corresponding calls to #GNUNET_MULTICAST_join_decision().
190  */
191 struct GNUNET_MULTICAST_JoinHandle
192 {
193 };
194
195
196 /**
197  * Function to call with the decision made for a join request.
198  *
199  * Must be called once and only once in response to an invocation of the
200  * #GNUNET_MULTICAST_JoinCallback.
201  *
202  * @param jh Join request handle.
203  * @param is_admitted #GNUNET_YES if joining is approved,
204  *        #GNUNET_NO if it is disapproved
205  * @param relay_count Number of relays given.
206  * @param relays Array of suggested peers that might be useful relays to use
207  *        when joining the multicast group (essentially a list of peers that
208  *        are already part of the multicast group and might thus be willing
209  *        to help with routing).  If empty, only this local peer (which must
210  *        be the multicast origin) is a good candidate for building the
211  *        multicast tree.  Note that it is unnecessary to specify our own
212  *        peer identity in this array.
213  * @param join_response Message to send in response to the joining peer;
214  *        can also be used to redirect the peer to a different group at the
215  *        application layer; this response is to be transmitted to the
216  *        peer that issued the request even if admission is denied.
217  */
218 struct GNUNET_MULTICAST_ReplayHandle *
219 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
220                                 int is_admitted,
221                                 unsigned int relay_count,
222                                 const struct GNUNET_PeerIdentity *relays,
223                                 const struct GNUNET_MessageHeader *join_response)
224 {
225   return NULL;
226 }
227
228
229 /**
230  * Handle to pass back for the answer of a membership test.
231  */
232 struct GNUNET_MULTICAST_MembershipTestHandle
233 {
234 };
235
236
237 /**
238  * Call informing multicast about the decision taken for a membership test.
239  *
240  * @param mth Handle that was given for the query.
241  * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
242  *        #GNUNET_SYSERR if we cannot answer the membership test.
243  */
244 void
245 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
246                                          int result)
247 {
248 }
249
250
251 /**
252  * Opaque handle to a replay request from the multicast service.
253  */
254 struct GNUNET_MULTICAST_ReplayHandle
255 {
256 };
257
258
259 /**
260  * Replay a message fragment for the multicast group.
261  *
262  * @param rh Replay handle identifying which replay operation was requested.
263  * @param msg Replayed message fragment, NULL if unknown/error.
264  * @param ec Error code.
265  */
266 void
267 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
268                                   const struct GNUNET_MessageHeader *msg,
269                                   enum GNUNET_MULTICAST_ReplayErrorCode ec)
270 {
271 }
272
273
274 /**
275  * Indicate the end of the replay session.
276  *
277  * Invalidates the replay handle.
278  *
279  * @param rh Replay session to end.
280  */
281 void
282 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
283 {
284 }
285
286
287 /**
288  * Replay a message for the multicast group.
289  *
290  * @param rh Replay handle identifying which replay operation was requested.
291  * @param notify Function to call to get the message.
292  * @param notify_cls Closure for @a notify.
293  */
294 void
295 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
296                                    GNUNET_MULTICAST_ReplayTransmitNotify notify,
297                                    void *notify_cls)
298 {
299 }
300
301
302 /**
303  * Start a multicast group.
304  *
305  * Will advertise the origin in the P2P overlay network under the respective
306  * public key so that other peer can find this peer to join it.  Peers that
307  * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
308  * either an existing group member or to the origin.  If the joining is
309  * approved, the member is cleared for @e replay and will begin to receive
310  * messages transmitted to the group.  If joining is disapproved, the failed
311  * candidate will be given a response.  Members in the group can send messages
312  * to the origin (one at a time).
313  *
314  * @param cfg Configuration to use.
315  * @param priv_key ECC key that will be used to sign messages for this
316  *        multicast session; public key is used to identify the multicast group;
317  * @param next_fragment_id Next fragment ID to continue counting fragments from
318  *        when restarting the origin.  0 for a new group.
319  * @param join_cb Function called to approve / disapprove joining of a peer.
320  * @param mem_test_cb Function multicast can use to test group membership.
321  * @param replay_frag_cb Function that can be called to replay a message fragment.
322  * @param replay_msg_cb Function that can be called to replay a message.
323  * @param request_cb Function called with message fragments from group members.
324  * @param message_cb Function called with the message fragments sent to the
325  *        network by GNUNET_MULTICAST_origin_to_all().  These message fragments
326  *        should be stored for answering replay requests later.
327  * @param cls Closure for the various callbacks that follow.
328  * @return Handle for the origin, NULL on error.
329  */
330 struct GNUNET_MULTICAST_Origin *
331 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
332                                const struct GNUNET_CRYPTO_EddsaPrivateKey *priv_key,
333                                uint64_t next_fragment_id,
334                                GNUNET_MULTICAST_JoinCallback join_cb,
335                                GNUNET_MULTICAST_MembershipTestCallback mem_test_cb,
336                                GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
337                                GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
338                                GNUNET_MULTICAST_RequestCallback request_cb,
339                                GNUNET_MULTICAST_MessageCallback message_cb,
340                                void *cls)
341 {
342   struct GNUNET_MULTICAST_Origin *orig = GNUNET_malloc (sizeof (*orig));
343   orig->priv_key = *priv_key;
344   orig->next_fragment_id = next_fragment_id;
345   orig->join_cb = join_cb;
346   orig->mem_test_cb = mem_test_cb;
347   orig->replay_frag_cb = replay_frag_cb;
348   orig->replay_msg_cb = replay_msg_cb;
349   orig->request_cb = request_cb;
350   orig->message_cb = message_cb;
351   orig->cls = cls;
352   return orig;
353 }
354
355
356 /* FIXME: for now just send back to the client what it sent. */
357 static void
358 schedule_origin_to_all (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
359 {
360   LOG (GNUNET_ERROR_TYPE_DEBUG, "schedule_origin_to_all()\n");
361   struct GNUNET_MULTICAST_Origin *orig = cls;
362   struct GNUNET_MULTICAST_OriginMessageHandle *mh = &orig->msg_handle;
363
364   size_t buf_size = GNUNET_MULTICAST_FRAGMENT_MAX_SIZE;
365   struct GNUNET_MULTICAST_MessageHeader *msg
366     = GNUNET_malloc (sizeof (*msg) + buf_size);
367   int ret = mh->notify (mh->notify_cls, &buf_size, &msg[1]);
368
369   if (! (GNUNET_YES == ret || GNUNET_NO == ret)
370       || buf_size > GNUNET_MULTICAST_FRAGMENT_MAX_SIZE)
371   {
372     LOG (GNUNET_ERROR_TYPE_ERROR,
373          "MasterTransmitNotify() returned error or invalid message size.\n");
374     /* FIXME: handle error */
375     return;
376   }
377
378   if (GNUNET_NO == ret && 0 == buf_size)
379     return; /* Transmission paused. */
380
381   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
382   msg->header.size = htons (buf_size);
383   msg->message_id = mh->message_id;
384   msg->group_generation = mh->group_generation;
385
386   /* FIXME: add fragment ID and signature in the service instead of here */
387   msg->fragment_id = orig->next_fragment_id++;
388   msg->fragment_offset = mh->fragment_offset;
389   mh->fragment_offset += buf_size;
390   msg->purpose.size = htonl (buf_size
391                              - sizeof (msg->header)
392                              - sizeof (msg->hop_counter)
393                              - sizeof (msg->signature));
394   msg->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE);
395
396   if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &msg->purpose,
397                                            &msg->signature))
398   {
399     /* FIXME: handle error */
400     return;
401   }
402
403   /* FIXME: send msg to the service and only then call message_cb with the
404    *        returned signed message.
405    * FIXME: Also send to local members in this group.
406    */
407   orig->message_cb (orig->cls, (const struct GNUNET_MessageHeader *) msg);
408
409   if (GNUNET_NO == ret)
410     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
411                                   (GNUNET_TIME_UNIT_SECONDS, 1),
412                                   schedule_origin_to_all, orig);
413
414 }
415
416 /**
417  * Send a message to the multicast group.
418  *
419  * @param origin Handle to the multicast group.
420  * @param message_id Application layer ID for the message.  Opaque to multicast.
421  * @param group_generation Group generation of the message.  Documented in
422  *             `struct GNUNET_MULTICAST_MessageHeader`.
423  * @param notify Function to call to get the message.
424  * @param notify_cls Closure for @a notify.
425  * @return NULL on error (i.e. request already pending).
426  */
427 struct GNUNET_MULTICAST_OriginMessageHandle *
428 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
429                                 uint64_t message_id,
430                                 uint64_t group_generation,
431                                 GNUNET_MULTICAST_OriginTransmitNotify notify,
432                                 void *notify_cls)
433 {
434   struct GNUNET_MULTICAST_OriginMessageHandle *mh = &origin->msg_handle;
435   mh->origin = origin;
436   mh->message_id = message_id;
437   mh->group_generation = group_generation;
438   mh->notify = notify;
439   mh->notify_cls = notify_cls;
440
441   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
442                                 (GNUNET_TIME_UNIT_SECONDS, 1),
443                                 schedule_origin_to_all, origin);
444   return &origin->msg_handle;
445 }
446
447
448 /**
449  * Resume message transmission to multicast group.
450  *
451  * @param mh Request to cancel.
452  */
453 void
454 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
455 {
456   GNUNET_SCHEDULER_add_now (schedule_origin_to_all, mh->origin);
457 }
458
459
460 /**
461  * Cancel request for message transmission to multicast group.
462  *
463  * @param mh Request to cancel.
464  */
465 void
466 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
467 {
468 }
469
470
471 /**
472  * Stop a multicast group.
473  *
474  * @param origin Multicast group to stop.
475  */
476 void
477 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
478 {
479   GNUNET_free (origin);
480 }
481
482
483 /**
484  * Join a multicast group.
485  *
486  * The entity joining is always the local peer.  Further information about the
487  * candidate can be provided in the @a join_request message.  If the join fails, the
488  * @a message_cb is invoked with a (failure) response and then with NULL.  If
489  * the join succeeds, outstanding (state) messages and ongoing multicast
490  * messages will be given to the @a message_cb until the member decides to part
491  * the group.  The @a test_cb and @a replay_cb functions may be called at
492  * anytime by the multicast service to support relaying messages to other
493  * members of the group.
494  *
495  * @param cfg Configuration to use.
496  * @param group_key ECC public key that identifies the group to join.
497  * @param member_key ECC key that identifies the member and used to sign
498  *        requests sent to the origin.
499  * @param origin Peer ID of the origin to send unicast requsets to.  If NULL,
500  *        unicast requests are sent back via multiple hops on the reverse path
501  *        of multicast messages.
502  * @param relay_count Number of peers in the @a relays array.
503  * @param relays Peer identities of members of the group, which serve as relays
504  *        and can be used to join the group at. and send the @a join_request to.
505  *        If empty, the @a join_request is sent directly to the @a origin.
506  * @param join_request  Application-dependent join request to be passed to the peer
507  *        @a relay (might, for example, contain a user, bind user
508  *        identity/pseudonym to peer identity, application-level message to
509  *        origin, etc.).
510  * @param join_cb Function called to approve / disapprove joining of a peer.
511  * @param mem_test_cb Function multicast can use to test group membership.
512  * @param replay_frag_cb Function that can be called to replay message fragments
513  *        this peer already knows from this group. NULL if this
514  *        client is unable to support replay.
515  * @param replay_msg_cb Function that can be called to replay message fragments
516  *        this peer already knows from this group. NULL if this
517  *        client is unable to support replay.
518  * @param message_cb Function to be called for all message fragments we
519  *        receive from the group, excluding those our @a replay_cb
520  *        already has.
521  * @param cls Closure for callbacks.
522  * @return Handle for the member, NULL on error.
523  */
524 struct GNUNET_MULTICAST_Member *
525 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
526                               const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
527                               const struct GNUNET_CRYPTO_EddsaPrivateKey *member_key,
528                               const struct GNUNET_PeerIdentity *origin,
529                               uint32_t relay_count,
530                               const struct GNUNET_PeerIdentity *relays,
531                               const struct GNUNET_MessageHeader *join_request,
532                               GNUNET_MULTICAST_JoinCallback join_cb,
533                               GNUNET_MULTICAST_MembershipTestCallback mem_test_cb,
534                               GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
535                               GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
536                               GNUNET_MULTICAST_MessageCallback message_cb,
537                               void *cls)
538 {
539   struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
540
541   return mem;
542 }
543
544
545 /**
546  * Handle for a replay request.
547  */
548 struct GNUNET_MULTICAST_MemberReplayHandle
549 {
550 };
551
552
553 /**
554  * Request a fragment to be replayed by fragment ID.
555  *
556  * Useful if messages below the @e max_known_fragment_id given when joining are
557  * needed and not known to the client.
558  *
559  * @param member Membership handle.
560  * @param fragment_id ID of a message fragment that this client would like to
561           see replayed.
562  * @param flags Additional flags for the replay request.  It is used and defined
563  *        by the replay callback.  FIXME: which replay callback? FIXME: use enum?
564  *        FIXME: why not pass reply cb here?
565  * @return Replay request handle, NULL on error.
566  */
567 struct GNUNET_MULTICAST_MemberReplayHandle *
568 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
569                                          uint64_t fragment_id,
570                                          uint64_t flags)
571 {
572   return NULL;
573 }
574
575
576 /**
577  * Request a message fragment to be replayed.
578  *
579  * Useful if messages below the @e max_known_fragment_id given when joining are
580  * needed and not known to the client.
581  *
582  * @param member Membership handle.
583  * @param message_id ID of the message this client would like to see replayed.
584  * @param fragment_offset Offset of the fragment within the message to replay.
585  * @param flags Additional flags for the replay request.  It is used & defined
586  *        by the replay callback.
587  * @param result_cb Function to be called for the replayed message.
588  * @param result_cb_cls Closure for @a result_cb.
589  * @return Replay request handle, NULL on error.
590  */
591 struct GNUNET_MULTICAST_MemberReplayHandle *
592 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
593                                         uint64_t message_id,
594                                         uint64_t fragment_offset,
595                                         uint64_t flags,
596                                         GNUNET_MULTICAST_ResultCallback result_cb,
597                                         void *result_cb_cls)
598 {
599   return NULL;
600 }
601
602
603 /**
604  * Cancel a replay request.
605  *
606  * @param rh Request to cancel.
607  */
608 void
609 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
610 {
611 }
612
613
614 /**
615  * Part a multicast group.
616  *
617  * Disconnects from all group members and invalidates the @a member handle.
618  *
619  * An application-dependent part message can be transmitted beforehand using
620  * #GNUNET_MULTICAST_member_to_origin())
621  *
622  * @param member Membership handle.
623  */
624 void
625 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
626 {
627   GNUNET_free (member);
628 }
629
630
631 /**
632  * Send a message to the origin of the multicast group.
633  *
634  * @param member Membership handle.
635  * @param message_id Application layer ID for the message.  Opaque to multicast.
636  * @param notify Callback to call to get the message.
637  * @param notify_cls Closure for @a notify.
638  * @return Handle to cancel request, NULL on error (i.e. request already pending).
639  */
640 struct GNUNET_MULTICAST_MemberRequestHandle *
641 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
642                                    uint64_t message_id,
643                                    GNUNET_MULTICAST_MemberTransmitNotify notify,
644                                    void *notify_cls)
645 {
646   return NULL;
647 }
648
649
650 /**
651  * Resume message transmission to origin.
652  *
653  * @param rh Request to cancel.
654  */
655 void
656 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
657 {
658
659 }
660
661
662 /**
663  * Cancel request for message transmission to origin.
664  *
665  * @param rh Request to cancel.
666  */
667 void
668 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
669 {
670 }
671
672
673 /* end of multicast_api.c */