2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file multicast/multicast_api.c
23 * @brief multicast service; establish tunnels to distant peers
24 * @author Christian Grothoff
25 * @author Gabor X Toth
28 #include "gnunet_util_lib.h"
29 #include "gnunet_signatures.h"
30 #include "gnunet_multicast_service.h"
31 #include "multicast.h"
33 #define LOG(kind,...) GNUNET_log_from (kind, "multicast-api",__VA_ARGS__)
37 * Handle for a request to send a message to all multicast group members
40 struct GNUNET_MULTICAST_OriginMessageHandle
42 GNUNET_MULTICAST_OriginTransmitNotify notify;
44 struct GNUNET_MULTICAST_Origin *origin;
47 uint64_t group_generation;
48 uint64_t fragment_offset;
53 * Handle for the origin of a multicast group.
55 struct GNUNET_MULTICAST_Origin
57 struct GNUNET_CRYPTO_EddsaPrivateKey priv_key;
58 struct GNUNET_MULTICAST_OriginMessageHandle msg_handle;
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;
68 uint64_t next_fragment_id;
73 * Handle for a message to be delivered from a member to the origin.
75 struct GNUNET_MULTICAST_MemberRequestHandle
81 * Opaque handle for a multicast group member.
83 struct GNUNET_MULTICAST_Member
88 GNUNET_NETWORK_STRUCT_BEGIN
91 * Header of a request from a member to the origin.
93 struct GNUNET_MULTICAST_RequestHeader
96 * Header for all requests from a member to the origin.
98 struct GNUNET_MessageHeader header;
101 * Public key of the sending member.
103 struct GNUNET_CRYPTO_EddsaPublicKey member_key;
106 * ECC signature of the request fragment.
108 * Signature must match the public key of the multicast group.
110 struct GNUNET_CRYPTO_EddsaSignature signature;
113 * Purpose for the signature and size of the signed data.
115 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
118 * Number of the request fragment, monotonically increasing.
120 uint64_t fragment_id GNUNET_PACKED;
123 * Byte offset of this @e fragment of the @e request.
125 uint64_t fragment_offset GNUNET_PACKED;
128 * Number of the request this fragment belongs to.
130 * Set in GNUNET_MULTICAST_origin_to_all().
132 uint64_t request_id GNUNET_PACKED;
135 * Flags for this request.
137 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
139 /* Followed by request body. */
143 * Header of a join request sent to the origin or another member.
145 struct GNUNET_MULTICAST_JoinRequest
148 * Header for the join request.
150 struct GNUNET_MessageHeader header;
153 * ECC signature of the rest of the fields of the join request.
155 * Signature must match the public key of the joining member.
157 struct GNUNET_CRYPTO_EddsaSignature signature;
160 * Purpose for the signature and size of the signed data.
162 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
165 * Public key of the target group.
167 struct GNUNET_CRYPTO_EddsaPublicKey group_key;
170 * Public key of the joining member.
172 struct GNUNET_CRYPTO_EddsaPublicKey member_key;
175 * Peer identity of the joining member.
177 struct GNUNET_PeerIdentity member_peer;
179 /* Followed by request body. */
182 GNUNET_NETWORK_STRUCT_END
186 * Handle that identifies a join request.
188 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
189 * corresponding calls to #GNUNET_MULTICAST_join_decision().
191 struct GNUNET_MULTICAST_JoinHandle
197 * Function to call with the decision made for a join request.
199 * Must be called once and only once in response to an invocation of the
200 * #GNUNET_MULTICAST_JoinCallback.
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.
218 struct GNUNET_MULTICAST_ReplayHandle *
219 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
221 unsigned int relay_count,
222 const struct GNUNET_PeerIdentity *relays,
223 const struct GNUNET_MessageHeader *join_response)
230 * Handle to pass back for the answer of a membership test.
232 struct GNUNET_MULTICAST_MembershipTestHandle
238 * Call informing multicast about the decision taken for a membership test.
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.
245 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
252 * Opaque handle to a replay request from the multicast service.
254 struct GNUNET_MULTICAST_ReplayHandle
260 * Replay a message fragment for the multicast group.
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.
267 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
268 const struct GNUNET_MessageHeader *msg,
269 enum GNUNET_MULTICAST_ReplayErrorCode ec)
275 * Indicate the end of the replay session.
277 * Invalidates the replay handle.
279 * @param rh Replay session to end.
282 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
288 * Replay a message for the multicast group.
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.
295 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
296 GNUNET_MULTICAST_ReplayTransmitNotify notify,
303 * Start a multicast group.
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).
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.
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,
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;
356 /* FIXME: for now just send back to the client what it sent. */
358 schedule_origin_to_all (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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;
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]);
369 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
370 || buf_size > GNUNET_MULTICAST_FRAGMENT_MAX_SIZE)
372 LOG (GNUNET_ERROR_TYPE_ERROR,
373 "MasterTransmitNotify() returned error or invalid message size.\n");
374 /* FIXME: handle error */
378 if (GNUNET_NO == ret && 0 == buf_size)
379 return; /* Transmission paused. */
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;
386 /* FIXME: add fragment ID and signature in the service */
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);
396 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &msg->purpose,
399 /* FIXME: handle error */
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.
407 orig->message_cb (orig->cls, (const struct GNUNET_MessageHeader *) msg);
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);
417 * Send a message to the multicast group.
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).
427 struct GNUNET_MULTICAST_OriginMessageHandle *
428 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
430 uint64_t group_generation,
431 GNUNET_MULTICAST_OriginTransmitNotify notify,
434 struct GNUNET_MULTICAST_OriginMessageHandle *mh = &origin->msg_handle;
436 mh->message_id = message_id;
437 mh->group_generation = group_generation;
439 mh->notify_cls = notify_cls;
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;
449 * Resume message transmission to multicast group.
451 * @param mh Request to cancel.
454 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
456 GNUNET_SCHEDULER_add_now (schedule_origin_to_all, mh->origin);
461 * Cancel request for message transmission to multicast group.
463 * @param mh Request to cancel.
466 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
472 * Stop a multicast group.
474 * @param origin Multicast group to stop.
477 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
479 GNUNET_free (origin);
484 * Join a multicast group.
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.
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
510 * @param join_cb Function called to approve / disapprove joining of a peer.
511 * @param 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
521 * @param cls Closure for callbacks.
522 * @return Handle for the member, NULL on error.
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 test_cb,
534 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
535 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
536 GNUNET_MULTICAST_MessageCallback message_cb,
539 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
546 * Handle for a replay request.
548 struct GNUNET_MULTICAST_MemberReplayHandle
554 * Request a fragment to be replayed by fragment ID.
556 * Useful if messages below the @e max_known_fragment_id given when joining are
557 * needed and not known to the client.
559 * @param member Membership handle.
560 * @param fragment_id ID of a message fragment that this client would like to
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.
567 struct GNUNET_MULTICAST_MemberReplayHandle *
568 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
569 uint64_t fragment_id,
577 * Request a message fragment to be replayed.
579 * Useful if messages below the @e max_known_fragment_id given when joining are
580 * needed and not known to the client.
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.
591 struct GNUNET_MULTICAST_MemberReplayHandle *
592 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
594 uint64_t fragment_offset,
596 GNUNET_MULTICAST_ResultCallback result_cb,
604 * Cancel a replay request.
606 * @param rh Request to cancel.
609 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
615 * Part a multicast group.
617 * Disconnects from all group members and invalidates the @a member handle.
619 * An application-dependent part message can be transmitted beforehand using
620 * #GNUNET_MULTICAST_member_to_origin())
622 * @param member Membership handle.
625 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
627 GNUNET_free (member);
632 * Send a message to the origin of the multicast group.
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).
640 struct GNUNET_MULTICAST_MemberRequestHandle *
641 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
643 GNUNET_MULTICAST_MemberTransmitNotify notify,
651 * Resume message transmission to origin.
653 * @param rh Request to cancel.
656 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
663 * Cancel request for message transmission to origin.
665 * @param rh Request to cancel.
668 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
673 /* end of multicast_api.c */