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"
34 * Handle for a request to send a message to all multicast group members
37 struct GNUNET_MULTICAST_OriginMessageHandle
39 GNUNET_MULTICAST_OriginTransmitNotify notify;
43 uint64_t group_generation;
44 uint64_t fragment_offset;
49 * Handle for the origin of a multicast group.
51 struct GNUNET_MULTICAST_Origin
53 struct GNUNET_CRYPTO_EccPrivateKey priv_key;
54 struct GNUNET_MULTICAST_OriginMessageHandle msg_handle;
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;
64 uint64_t next_fragment_id;
69 * Handle for a message to be delivered from a member to the origin.
71 struct GNUNET_MULTICAST_MemberRequestHandle
77 * Opaque handle for a multicast group member.
79 struct GNUNET_MULTICAST_Member
84 GNUNET_NETWORK_STRUCT_BEGIN
87 * Header of a request from a member to the origin.
89 struct GNUNET_MULTICAST_RequestHeader
92 * Header for all requests from a member to the origin.
94 struct GNUNET_MessageHeader header;
97 * Public key of the sending member.
99 struct GNUNET_CRYPTO_EccPublicSignKey member_key;
102 * ECC signature of the request fragment.
104 * Signature must match the public key of the multicast group.
106 struct GNUNET_CRYPTO_EccSignature signature;
109 * Purpose for the signature and size of the signed data.
111 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
114 * Number of the request fragment, monotonically increasing.
116 uint64_t fragment_id GNUNET_PACKED;
119 * Byte offset of this @e fragment of the @e request.
121 uint64_t fragment_offset GNUNET_PACKED;
124 * Number of the request this fragment belongs to.
126 * Set in GNUNET_MULTICAST_origin_to_all().
128 uint64_t request_id GNUNET_PACKED;
131 * Flags for this request.
133 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
135 /* Followed by request body. */
139 * Header of a join request sent to the origin or another member.
141 struct GNUNET_MULTICAST_JoinRequest
144 * Header for the join request.
146 struct GNUNET_MessageHeader header;
149 * ECC signature of the rest of the fields of the join request.
151 * Signature must match the public key of the joining member.
153 struct GNUNET_CRYPTO_EccSignature signature;
156 * Purpose for the signature and size of the signed data.
158 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
161 * Public key of the target group.
163 struct GNUNET_CRYPTO_EccPublicSignKey group_key;
166 * Public key of the joining member.
168 struct GNUNET_CRYPTO_EccPublicSignKey member_key;
171 * Peer identity of the joining member.
173 struct GNUNET_PeerIdentity member_peer;
175 /* Followed by request body. */
178 GNUNET_NETWORK_STRUCT_END
182 * Handle that identifies a join request.
184 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
185 * corresponding calls to #GNUNET_MULTICAST_join_decision().
187 struct GNUNET_MULTICAST_JoinHandle
193 * Function to call with the decision made for a join request.
195 * Must be called once and only once in response to an invocation of the
196 * #GNUNET_MULTICAST_JoinCallback.
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.
214 struct GNUNET_MULTICAST_ReplayHandle *
215 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
217 unsigned int relay_count,
218 const struct GNUNET_PeerIdentity *relays,
219 const struct GNUNET_MessageHeader *join_response)
226 * Handle to pass back for the answer of a membership test.
228 struct GNUNET_MULTICAST_MembershipTestHandle
234 * Call informing multicast about the decision taken for a membership test.
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.
241 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
248 * Opaque handle to a replay request from the multicast service.
250 struct GNUNET_MULTICAST_ReplayHandle
256 * Replay a message fragment for the multicast group.
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.
263 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
264 const struct GNUNET_MessageHeader *msg,
265 enum GNUNET_MULTICAST_ReplayErrorCode ec)
271 * Indicate the end of the replay session.
273 * Invalidates the replay handle.
275 * @param rh Replay session to end.
278 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
284 * Replay a message for the multicast group.
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.
291 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
292 GNUNET_MULTICAST_ReplayTransmitNotify notify,
299 * Start a multicast group.
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).
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.
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,
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;
352 /* FIXME: for now just send back to the client what it sent. */
354 schedule_origin_to_all (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
356 struct GNUNET_MULTICAST_Origin *orig = cls;
357 struct GNUNET_MULTICAST_OriginMessageHandle *mh = &orig->msg_handle;
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]);
364 if (ret != GNUNET_YES || ret != GNUNET_NO)
366 /* FIXME: handle error */
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;
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);
385 if (GNUNET_OK != GNUNET_CRYPTO_ecc_sign (&orig->priv_key, &msg->purpose,
388 /* FIXME: handle error */
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.
396 orig->message_cb (orig->cls, msg);
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);
406 * Send a message to the multicast group.
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).
416 struct GNUNET_MULTICAST_OriginMessageHandle *
417 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
419 uint64_t group_generation,
420 GNUNET_MULTICAST_OriginTransmitNotify notify,
423 struct GNUNET_MULTICAST_OriginMessageHandle *mh = &origin->msg_handle;
424 mh->message_id = message_id;
425 mh->group_generation = group_generation;
427 mh->notify_cls = notify_cls;
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;
437 * Resume message transmission to multicast group.
439 * @param mh Request to cancel.
442 GNUNET_MULTICAST_origin_to_all_resume (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
449 * Cancel request for message transmission to multicast group.
451 * @param mh Request to cancel.
454 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
460 * Stop a multicast group.
462 * @param origin Multicast group to stop.
465 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
467 GNUNET_free (origin);
472 * Join a multicast group.
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.
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
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
509 * @param cls Closure for callbacks.
510 * @return Handle for the member, NULL on error.
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,
527 struct GNUNET_MULTICAST_Member *mem = GNUNET_malloc (sizeof (*mem));
534 * Handle for a replay request.
536 struct GNUNET_MULTICAST_MemberReplayHandle
542 * Request a fragment to be replayed by fragment ID.
544 * Useful if messages below the @e max_known_fragment_id given when joining are
545 * needed and not known to the client.
547 * @param member Membership handle.
548 * @param fragment_id ID of a message fragment that this client would like to
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.
555 struct GNUNET_MULTICAST_MemberReplayHandle *
556 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
557 uint64_t fragment_id,
565 * Request a message fragment to be replayed.
567 * Useful if messages below the @e max_known_fragment_id given when joining are
568 * needed and not known to the client.
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.
579 struct GNUNET_MULTICAST_MemberReplayHandle *
580 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
582 uint64_t fragment_offset,
584 GNUNET_MULTICAST_ResultCallback result_cb,
592 * Cancel a replay request.
594 * @param rh Request to cancel.
597 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
603 * Part a multicast group.
605 * Disconnects from all group members and invalidates the @a member handle.
607 * An application-dependent part message can be transmitted beforehand using
608 * #GNUNET_MULTICAST_member_to_origin())
610 * @param member Membership handle.
613 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
615 GNUNET_free (member);
620 * Send a message to the origin of the multicast group.
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).
628 struct GNUNET_MULTICAST_MemberRequestHandle *
629 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
631 GNUNET_MULTICAST_MemberTransmitNotify notify,
639 * Resume message transmission to origin.
641 * @param rh Request to cancel.
644 GNUNET_MULTICAST_member_to_origin_resume (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
651 * Cancel request for message transmission to origin.
653 * @param rh Request to cancel.
656 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
661 /* end of multicast_api.c */