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