- decrease timeout
[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 (buf_size);
367   buf_size -= sizeof (*msg);
368   int ret = mh->notify (mh->notify_cls, &buf_size, &msg[1]);
369
370   if (! (GNUNET_YES == ret || GNUNET_NO == ret)
371       || sizeof (*msg) + buf_size > GNUNET_MULTICAST_FRAGMENT_MAX_SIZE)
372   {
373     LOG (GNUNET_ERROR_TYPE_ERROR,
374          "MasterTransmitNotify() returned error or invalid message size.\n");
375     /* FIXME: handle error */
376     return;
377   }
378
379   if (GNUNET_NO == ret && 0 == buf_size)
380     return; /* Transmission paused. */
381
382   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
383   msg->header.size = htons (sizeof (*msg) + buf_size);
384   msg->message_id = mh->message_id;
385   msg->group_generation = mh->group_generation;
386
387   /* FIXME: add fragment ID and signature in the service instead of here */
388   msg->fragment_id = orig->next_fragment_id++;
389   msg->fragment_offset = mh->fragment_offset;
390   mh->fragment_offset += sizeof (*msg) + buf_size;
391   msg->purpose.size = htonl (sizeof (*msg) + buf_size
392                              - sizeof (msg->header)
393                              - sizeof (msg->hop_counter)
394                              - sizeof (msg->signature));
395   msg->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_MULTICAST_MESSAGE);
396
397   if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &msg->purpose,
398                                            &msg->signature))
399   {
400     /* FIXME: handle error */
401     return;
402   }
403
404   /* FIXME: send msg to the service and only then call message_cb with the
405    *        returned signed message.
406    * FIXME: Also send to local members in this group.
407    */
408   orig->message_cb (orig->cls, (const struct GNUNET_MessageHeader *) msg);
409
410   if (GNUNET_NO == ret)
411     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
412                                   (GNUNET_TIME_UNIT_SECONDS, 1),
413                                   schedule_origin_to_all, orig);
414
415 }
416
417 /**
418  * Send a message to the multicast group.
419  *
420  * @param origin Handle to the multicast group.
421  * @param message_id Application layer ID for the message.  Opaque to multicast.
422  * @param group_generation Group generation of the message.  Documented in
423  *             `struct GNUNET_MULTICAST_MessageHeader`.
424  * @param notify Function to call to get the message.
425  * @param notify_cls Closure for @a notify.
426  * @return NULL on error (i.e. request already pending).
427  */
428 struct GNUNET_MULTICAST_OriginMessageHandle *
429 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
430                                 uint64_t message_id,
431                                 uint64_t group_generation,
432                                 GNUNET_MULTICAST_OriginTransmitNotify notify,
433                                 void *notify_cls)
434 {
435   struct GNUNET_MULTICAST_OriginMessageHandle *mh = &origin->msg_handle;
436   mh->origin = origin;
437   mh->message_id = message_id;
438   mh->group_generation = group_generation;
439   mh->notify = notify;
440   mh->notify_cls = notify_cls;
441
442   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
443                                 (GNUNET_TIME_UNIT_SECONDS, 1),
444                                 schedule_origin_to_all, origin);
445   return &origin->msg_handle;
446 }
447
448
449 /**
450  * Resume message transmission to multicast group.
451  *
452  * @param mh Request to cancel.
453  */
454 void
455 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
456 {
457   GNUNET_SCHEDULER_add_now (schedule_origin_to_all, mh->origin);
458 }
459
460
461 /**
462  * Cancel request for message transmission to multicast group.
463  *
464  * @param mh Request to cancel.
465  */
466 void
467 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
468 {
469 }
470
471
472 /**
473  * Stop a multicast group.
474  *
475  * @param origin Multicast group to stop.
476  */
477 void
478 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
479 {
480   GNUNET_free (origin);
481 }
482
483
484 /**
485  * Join a multicast group.
486  *
487  * The entity joining is always the local peer.  Further information about the
488  * candidate can be provided in the @a join_request message.  If the join fails, the
489  * @a message_cb is invoked with a (failure) response and then with NULL.  If
490  * the join succeeds, outstanding (state) messages and ongoing multicast
491  * messages will be given to the @a message_cb until the member decides to part
492  * the group.  The @a test_cb and @a replay_cb functions may be called at
493  * anytime by the multicast service to support relaying messages to other
494  * members of the group.
495  *
496  * @param cfg Configuration to use.
497  * @param group_key ECC public key that identifies the group to join.
498  * @param member_key ECC key that identifies the member and used to sign
499  *        requests sent to the origin.
500  * @param origin Peer ID of the origin to send unicast requsets to.  If NULL,
501  *        unicast requests are sent back via multiple hops on the reverse path
502  *        of multicast messages.
503  * @param relay_count Number of peers in the @a relays array.
504  * @param relays Peer identities of members of the group, which serve as relays
505  *        and can be used to join the group at. and send the @a join_request to.
506  *        If empty, the @a join_request is sent directly to the @a origin.
507  * @param join_request  Application-dependent join request to be passed to the peer
508  *        @a relay (might, for example, contain a user, bind user
509  *        identity/pseudonym to peer identity, application-level message to
510  *        origin, etc.).
511  * @param join_cb Function called to approve / disapprove joining of a peer.
512  * @param mem_test_cb Function multicast can use to test group membership.
513  * @param replay_frag_cb Function that can be called to replay message fragments
514  *        this peer already knows from this group. NULL if this
515  *        client is unable to support replay.
516  * @param replay_msg_cb Function that can be called to replay message fragments
517  *        this peer already knows from this group. NULL if this
518  *        client is unable to support replay.
519  * @param message_cb Function to be called for all message fragments we
520  *        receive from the group, excluding those our @a replay_cb
521  *        already has.
522  * @param cls Closure for callbacks.
523  * @return Handle for the member, NULL on error.
524  */
525 struct GNUNET_MULTICAST_Member *
526 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
527                               const struct GNUNET_CRYPTO_EddsaPublicKey *group_key,
528                               const struct GNUNET_CRYPTO_EddsaPrivateKey *member_key,
529                               const struct GNUNET_PeerIdentity *origin,
530                               uint32_t relay_count,
531                               const struct GNUNET_PeerIdentity *relays,
532                               const struct GNUNET_MessageHeader *join_request,
533                               GNUNET_MULTICAST_JoinCallback join_cb,
534                               GNUNET_MULTICAST_MembershipTestCallback mem_test_cb,
535                               GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
536                               GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
537                               GNUNET_MULTICAST_MessageCallback message_cb,
538                               void *cls)
539 {
540   struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
541
542   return mem;
543 }
544
545
546 /**
547  * Handle for a replay request.
548  */
549 struct GNUNET_MULTICAST_MemberReplayHandle
550 {
551 };
552
553
554 /**
555  * Request a fragment to be replayed by fragment ID.
556  *
557  * Useful if messages below the @e max_known_fragment_id given when joining are
558  * needed and not known to the client.
559  *
560  * @param member Membership handle.
561  * @param fragment_id ID of a message fragment that this client would like to
562           see replayed.
563  * @param flags Additional flags for the replay request.  It is used and defined
564  *        by the replay callback.  FIXME: which replay callback? FIXME: use enum?
565  *        FIXME: why not pass reply cb here?
566  * @return Replay request handle, NULL on error.
567  */
568 struct GNUNET_MULTICAST_MemberReplayHandle *
569 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
570                                          uint64_t fragment_id,
571                                          uint64_t flags)
572 {
573   return NULL;
574 }
575
576
577 /**
578  * Request a message fragment to be replayed.
579  *
580  * Useful if messages below the @e max_known_fragment_id given when joining are
581  * needed and not known to the client.
582  *
583  * @param member Membership handle.
584  * @param message_id ID of the message this client would like to see replayed.
585  * @param fragment_offset Offset of the fragment within the message to replay.
586  * @param flags Additional flags for the replay request.  It is used & defined
587  *        by the replay callback.
588  * @param result_cb Function to be called for the replayed message.
589  * @param result_cb_cls Closure for @a result_cb.
590  * @return Replay request handle, NULL on error.
591  */
592 struct GNUNET_MULTICAST_MemberReplayHandle *
593 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
594                                         uint64_t message_id,
595                                         uint64_t fragment_offset,
596                                         uint64_t flags,
597                                         GNUNET_MULTICAST_ResultCallback result_cb,
598                                         void *result_cb_cls)
599 {
600   return NULL;
601 }
602
603
604 /**
605  * Cancel a replay request.
606  *
607  * @param rh Request to cancel.
608  */
609 void
610 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
611 {
612 }
613
614
615 /**
616  * Part a multicast group.
617  *
618  * Disconnects from all group members and invalidates the @a member handle.
619  *
620  * An application-dependent part message can be transmitted beforehand using
621  * #GNUNET_MULTICAST_member_to_origin())
622  *
623  * @param member Membership handle.
624  */
625 void
626 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
627 {
628   GNUNET_free (member);
629 }
630
631
632 /**
633  * Send a message to the origin of the multicast group.
634  *
635  * @param member Membership handle.
636  * @param message_id Application layer ID for the message.  Opaque to multicast.
637  * @param notify Callback to call to get the message.
638  * @param notify_cls Closure for @a notify.
639  * @return Handle to cancel request, NULL on error (i.e. request already pending).
640  */
641 struct GNUNET_MULTICAST_MemberRequestHandle *
642 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
643                                    uint64_t message_id,
644                                    GNUNET_MULTICAST_MemberTransmitNotify notify,
645                                    void *notify_cls)
646 {
647   return NULL;
648 }
649
650
651 /**
652  * Resume message transmission to origin.
653  *
654  * @param rh Request to cancel.
655  */
656 void
657 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
658 {
659
660 }
661
662
663 /**
664  * Cancel request for message transmission to origin.
665  *
666  * @param rh Request to cancel.
667  */
668 void
669 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
670 {
671 }
672
673
674 /* end of multicast_api.c */