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 (buf_size);
367 buf_size -= sizeof (*msg);
368 int ret = mh->notify (mh->notify_cls, &buf_size, &msg[1]);
370 if (! (GNUNET_YES == ret || GNUNET_NO == ret)
371 || sizeof (*msg) + buf_size > GNUNET_MULTICAST_FRAGMENT_MAX_SIZE)
373 LOG (GNUNET_ERROR_TYPE_ERROR,
374 "MasterTransmitNotify() returned error or invalid message size.\n");
375 /* FIXME: handle error */
379 if (GNUNET_NO == ret && 0 == buf_size)
380 return; /* Transmission paused. */
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;
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);
397 if (GNUNET_OK != GNUNET_CRYPTO_eddsa_sign (&orig->priv_key, &msg->purpose,
400 /* FIXME: handle error */
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.
408 orig->message_cb (orig->cls, (const struct GNUNET_MessageHeader *) msg);
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);
418 * Send a message to the multicast group.
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).
428 struct GNUNET_MULTICAST_OriginMessageHandle *
429 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
431 uint64_t group_generation,
432 GNUNET_MULTICAST_OriginTransmitNotify notify,
435 struct GNUNET_MULTICAST_OriginMessageHandle *mh = &origin->msg_handle;
437 mh->message_id = message_id;
438 mh->group_generation = group_generation;
440 mh->notify_cls = notify_cls;
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;
450 * Resume message transmission to multicast group.
452 * @param mh Request to cancel.
455 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
457 GNUNET_SCHEDULER_add_now (schedule_origin_to_all, mh->origin);
462 * Cancel request for message transmission to multicast group.
464 * @param mh Request to cancel.
467 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
473 * Stop a multicast group.
475 * @param origin Multicast group to stop.
478 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
480 GNUNET_free (origin);
485 * Join a multicast group.
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.
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
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
522 * @param cls Closure for callbacks.
523 * @return Handle for the member, NULL on error.
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,
540 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
547 * Handle for a replay request.
549 struct GNUNET_MULTICAST_MemberReplayHandle
555 * Request a fragment to be replayed by fragment ID.
557 * Useful if messages below the @e max_known_fragment_id given when joining are
558 * needed and not known to the client.
560 * @param member Membership handle.
561 * @param fragment_id ID of a message fragment that this client would like to
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.
568 struct GNUNET_MULTICAST_MemberReplayHandle *
569 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
570 uint64_t fragment_id,
578 * Request a message fragment to be replayed.
580 * Useful if messages below the @e max_known_fragment_id given when joining are
581 * needed and not known to the client.
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.
592 struct GNUNET_MULTICAST_MemberReplayHandle *
593 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
595 uint64_t fragment_offset,
597 GNUNET_MULTICAST_ResultCallback result_cb,
605 * Cancel a replay request.
607 * @param rh Request to cancel.
610 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
616 * Part a multicast group.
618 * Disconnects from all group members and invalidates the @a member handle.
620 * An application-dependent part message can be transmitted beforehand using
621 * #GNUNET_MULTICAST_member_to_origin())
623 * @param member Membership handle.
626 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
628 GNUNET_free (member);
633 * Send a message to the origin of the multicast group.
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).
641 struct GNUNET_MULTICAST_MemberRequestHandle *
642 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
644 GNUNET_MULTICAST_MemberTransmitNotify notify,
652 * Resume message transmission to origin.
654 * @param rh Request to cancel.
657 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
664 * Cancel request for message transmission to origin.
666 * @param rh Request to cancel.
669 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
674 /* end of multicast_api.c */