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_multicast_service.h"
31 * Opaque handle for a multicast group member.
33 struct GNUNET_MULTICAST_Member
39 * Handle for the origin of a multicast group.
41 struct GNUNET_MULTICAST_Origin
46 GNUNET_NETWORK_STRUCT_BEGIN
49 * Header of a request from a member to the origin.
51 struct GNUNET_MULTICAST_RequestHeader
54 * Header for all requests from a member to the origin.
56 struct GNUNET_MessageHeader header;
59 * Public key of the sending member.
61 struct GNUNET_CRYPTO_EccPublicKey member_key;
64 * ECC signature of the request fragment.
66 * Signature must match the public key of the multicast group.
68 struct GNUNET_CRYPTO_EccSignature signature;
71 * Purpose for the signature and size of the signed data.
73 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
76 * Number of the request fragment, monotonically increasing.
78 uint64_t fragment_id GNUNET_PACKED;
81 * Byte offset of this @e fragment of the @e request.
83 uint64_t fragment_offset GNUNET_PACKED;
86 * Number of the request this fragment belongs to.
88 * Set in GNUNET_MULTICAST_origin_to_all().
90 uint64_t request_id GNUNET_PACKED;
93 * Flags for this request.
95 enum GNUNET_MULTICAST_MessageFlags flags GNUNET_PACKED;
97 /* Followed by request body. */
101 * Header of a join request sent to the origin or another member.
103 struct GNUNET_MULTICAST_JoinRequest
106 * Header for the join request.
108 struct GNUNET_MessageHeader header;
111 * ECC signature of the rest of the fields of the join request.
113 * Signature must match the public key of the joining member.
115 struct GNUNET_CRYPTO_EccSignature signature;
118 * Purpose for the signature and size of the signed data.
120 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
123 * Public key of the target group.
125 struct GNUNET_CRYPTO_EccPublicKey group_key;
128 * Public key of the joining member.
130 struct GNUNET_CRYPTO_EccPublicKey member_key;
133 * Peer identity of the joining member.
135 struct GNUNET_PeerIdentity member_peer;
137 /* Followed by request body. */
140 GNUNET_NETWORK_STRUCT_END
144 * Handle that identifies a join request.
146 * Used to match calls to #GNUNET_MULTICAST_JoinCallback to the
147 * corresponding calls to #GNUNET_MULTICAST_join_decision().
149 struct GNUNET_MULTICAST_JoinHandle
155 * Function to call with the decision made for a join request.
157 * Must be called once and only once in response to an invocation of the
158 * #GNUNET_MULTICAST_JoinCallback.
160 * @param jh Join request handle.
161 * @param is_admitted #GNUNET_YES if joining is approved,
162 * #GNUNET_NO if it is disapproved
163 * @param relay_count Number of relays given.
164 * @param relays Array of suggested peers that might be useful relays to use
165 * when joining the multicast group (essentially a list of peers that
166 * are already part of the multicast group and might thus be willing
167 * to help with routing). If empty, only this local peer (which must
168 * be the multicast origin) is a good candidate for building the
169 * multicast tree. Note that it is unnecessary to specify our own
170 * peer identity in this array.
171 * @param join_response Message to send in response to the joining peer;
172 * can also be used to redirect the peer to a different group at the
173 * application layer; this response is to be transmitted to the
174 * peer that issued the request even if admission is denied.
176 struct GNUNET_MULTICAST_ReplayHandle *
177 GNUNET_MULTICAST_join_decision (struct GNUNET_MULTICAST_JoinHandle *jh,
179 unsigned int relay_count,
180 const struct GNUNET_PeerIdentity *relays,
181 const struct GNUNET_MessageHeader *join_response)
188 * Handle to pass back for the answer of a membership test.
190 struct GNUNET_MULTICAST_MembershipTestHandle
196 * Call informing multicast about the decision taken for a membership test.
198 * @param mth Handle that was given for the query.
199 * @param result #GNUNET_YES if peer was a member, #GNUNET_NO if peer was not a member,
200 * #GNUNET_SYSERR if we cannot answer the membership test.
203 GNUNET_MULTICAST_membership_test_result (struct GNUNET_MULTICAST_MembershipTestHandle *mth,
210 * Opaque handle to a replay request from the multicast service.
212 struct GNUNET_MULTICAST_ReplayHandle
218 * Replay a message fragment for the multicast group.
220 * @param rh Replay handle identifying which replay operation was requested.
221 * @param msg Replayed message fragment, NULL if unknown/error.
222 * @param ec Error code.
225 GNUNET_MULTICAST_replay_response (struct GNUNET_MULTICAST_ReplayHandle *rh,
226 const struct GNUNET_MessageHeader *msg,
227 enum GNUNET_MULTICAST_ReplayErrorCode ec)
233 * Indicate the end of the replay session.
235 * Invalidates the replay handle.
237 * @param rh Replay session to end.
240 GNUNET_MULTICAST_replay_response_end (struct GNUNET_MULTICAST_ReplayHandle *rh)
246 * Replay a message for the multicast group.
248 * @param rh Replay handle identifying which replay operation was requested.
249 * @param notify Function to call to get the message.
250 * @param notify_cls Closure for @a notify.
253 GNUNET_MULTICAST_replay_response2 (struct GNUNET_MULTICAST_ReplayHandle *rh,
254 GNUNET_MULTICAST_ReplayTransmitNotify notify,
261 * Start a multicast group.
263 * Will advertise the origin in the P2P overlay network under the respective
264 * public key so that other peer can find this peer to join it. Peers that
265 * issue GNUNET_MULTICAST_member_join() can then transmit a join request to
266 * either an existing group member or to the origin. If the joining is
267 * approved, the member is cleared for @e replay and will begin to receive
268 * messages transmitted to the group. If joining is disapproved, the failed
269 * candidate will be given a response. Members in the group can send messages
270 * to the origin (one at a time).
272 * @param cfg Configuration to use.
273 * @param priv_key ECC key that will be used to sign messages for this
274 * multicast session; public key is used to identify the multicast group;
275 * FIXME: we'll likely want to use NOT the p521 curve here, but a cheaper
277 * @param last_fragment_id Last fragment ID to continue counting fragments from
278 * when restarting the origin. 0 for a new group.
279 * @param join_cb Function called to approve / disapprove joining of a peer.
280 * @param test_cb Function multicast can use to test group membership.
281 * @param replay_frag_cb Function that can be called to replay a message fragment.
282 * @param replay_msg_cb Function that can be called to replay a message.
283 * @param request_cb Function called with message fragments from group members.
284 * @param message_cb Function called with the message fragments sent to the
285 * network by GNUNET_MULTICAST_origin_to_all(). These message fragments
286 * should be stored for answering replay requests later.
287 * @param cls Closure for the various callbacks that follow.
288 * @return Handle for the origin, NULL on error.
290 struct GNUNET_MULTICAST_Origin *
291 GNUNET_MULTICAST_origin_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
292 const struct GNUNET_CRYPTO_EccPrivateKey *priv_key,
293 uint64_t last_fragment_id,
294 GNUNET_MULTICAST_JoinCallback join_cb,
295 GNUNET_MULTICAST_MembershipTestCallback test_cb,
296 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
297 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
298 GNUNET_MULTICAST_RequestCallback request_cb,
299 GNUNET_MULTICAST_MessageCallback message_cb,
307 * Handle for a request to send a message to all multicast group members
310 struct GNUNET_MULTICAST_OriginMessageHandle
316 * Send a message to the multicast group.
318 * @param origin Handle to the multicast group.
319 * @param message_id Application layer ID for the message. Opaque to multicast.
320 * @param group_generation Group generation of the message. Documented in
321 * GNUNET_MULTICAST_MessageHeader.
322 * @param size Number of bytes to transmit.
323 * FIXME: Needed? The end of the message can be flagged with a last fragment flag.
324 * @param notify Function to call to get the message.
325 * @param notify_cls Closure for @a notify.
326 * @return NULL on error (i.e. request already pending).
328 struct GNUNET_MULTICAST_OriginMessageHandle *
329 GNUNET_MULTICAST_origin_to_all (struct GNUNET_MULTICAST_Origin *origin,
331 uint64_t group_generation,
333 GNUNET_MULTICAST_OriginTransmitNotify notify,
341 * Cancel request for message transmission to multicast group.
343 * @param mh Request to cancel.
346 GNUNET_MULTICAST_origin_to_all_cancel (struct GNUNET_MULTICAST_OriginMessageHandle *mh)
352 * Stop a multicast group.
354 * @param origin Multicast group to stop.
357 GNUNET_MULTICAST_origin_stop (struct GNUNET_MULTICAST_Origin *origin)
363 * Join a multicast group.
365 * The entity joining is always the local peer. Further information about the
366 * candidate can be provided in the @a join_request message. If the join fails, the
367 * @a message_cb is invoked with a (failure) response and then with NULL. If
368 * the join succeeds, outstanding (state) messages and ongoing multicast
369 * messages will be given to the @a message_cb until the member decides to part
370 * the group. The @a test_cb and @a replay_cb functions may be called at
371 * anytime by the multicast service to support relaying messages to other
372 * members of the group.
374 * @param cfg Configuration to use.
375 * @param group_key ECC public key that identifies the group to join.
376 * @param member_key ECC key that identifies the member and used to sign
377 * requests sent to the origin.
378 * @param origin Peer ID of the origin to send unicast requsets to. If NULL,
379 * unicast requests are sent back via multiple hops on the reverse path
380 * of multicast messages.
381 * @param relay_count Number of peers in the @a relays array.
382 * @param relays Peer identities of members of the group, which serve as relays
383 * and can be used to join the group at. and send the @a join_request to.
384 * If empty, the @a join_request is sent directly to the @a origin.
385 * @param join_request Application-dependent join request to be passed to the peer
386 * @a relay (might, for example, contain a user, bind user
387 * identity/pseudonym to peer identity, application-level message to
389 * @param join_cb Function called to approve / disapprove joining of a peer.
390 * @param test_cb Function multicast can use to test group membership.
391 * @param replay_frag_cb Function that can be called to replay message fragments
392 * this peer already knows from this group. NULL if this
393 * client is unable to support replay.
394 * @param replay_msg_cb Function that can be called to replay message fragments
395 * this peer already knows from this group. NULL if this
396 * client is unable to support replay.
397 * @param message_cb Function to be called for all message fragments we
398 * receive from the group, excluding those our @a replay_cb
400 * @param cls Closure for callbacks.
401 * @return Handle for the member, NULL on error.
403 struct GNUNET_MULTICAST_Member *
404 GNUNET_MULTICAST_member_join (const struct GNUNET_CONFIGURATION_Handle *cfg,
405 const struct GNUNET_CRYPTO_EccPublicKey *group_key,
406 const struct GNUNET_CRYPTO_EccPrivateKey *member_key,
407 const struct GNUNET_PeerIdentity *origin,
409 const struct GNUNET_PeerIdentity *relays,
410 const struct GNUNET_MessageHeader *join_request,
411 GNUNET_MULTICAST_JoinCallback join_cb,
412 GNUNET_MULTICAST_MembershipTestCallback test_cb,
413 GNUNET_MULTICAST_ReplayFragmentCallback replay_frag_cb,
414 GNUNET_MULTICAST_ReplayMessageCallback replay_msg_cb,
415 GNUNET_MULTICAST_MessageCallback message_cb,
423 * Handle for a replay request.
425 struct GNUNET_MULTICAST_MemberReplayHandle
431 * Request a fragment to be replayed by fragment ID.
433 * Useful if messages below the @e max_known_fragment_id given when joining are
434 * needed and not known to the client.
436 * @param member Membership handle.
437 * @param fragment_id ID of a message fragment that this client would like to
439 * @param flags Additional flags for the replay request. It is used and defined
440 * by the replay callback. FIXME: which replay callback? FIXME: use enum?
441 * FIXME: why not pass reply cb here?
442 * @return Replay request handle, NULL on error.
444 struct GNUNET_MULTICAST_MemberReplayHandle *
445 GNUNET_MULTICAST_member_replay_fragment (struct GNUNET_MULTICAST_Member *member,
446 uint64_t fragment_id,
454 * Request a message fr to be replayed.
456 * Useful if messages below the @e max_known_fragment_id given when joining are
457 * needed and not known to the client.
459 * @param member Membership handle.
460 * @param message_id ID of the message this client would like to see replayed.
461 * @param fragment_offset Offset of the fragment within the message to replay.
462 * @param flags Additional flags for the replay request. It is used & defined
463 * by the replay callback.
464 * @param result_cb Function to be called for the replayed message.
465 * @param result_cb_cls Closure for @a result_cb.
466 * @return Replay request handle, NULL on error.
468 struct GNUNET_MULTICAST_MemberReplayHandle *
469 GNUNET_MULTICAST_member_replay_message (struct GNUNET_MULTICAST_Member *member,
471 uint64_t fragment_offset,
473 GNUNET_MULTICAST_ResultCallback result_cb,
481 * Cancel a replay request.
483 * @param rh Request to cancel.
486 GNUNET_MULTICAST_member_replay_cancel (struct GNUNET_MULTICAST_MemberReplayHandle *rh)
492 * Part a multicast group.
494 * Disconnects from all group members and invalidates the @a member handle.
496 * An application-dependent part message can be transmitted beforehand using
497 * #GNUNET_MULTICAST_member_to_origin())
499 * @param member Membership handle.
502 GNUNET_MULTICAST_member_part (struct GNUNET_MULTICAST_Member *member)
508 * Handle for a message to be delivered from a member to the origin.
510 struct GNUNET_MULTICAST_MemberRequestHandle
516 * Send a message to the origin of the multicast group.
518 * @param member Membership handle.
519 * @param message_id Application layer ID for the message. Opaque to multicast.
520 * @param size Number of bytes we want to send to origin.
521 * @param notify Callback to call to get the message.
522 * @param notify_cls Closure for @a notify.
523 * @return Handle to cancel request, NULL on error (i.e. request already pending).
525 struct GNUNET_MULTICAST_MemberRequestHandle *
526 GNUNET_MULTICAST_member_to_origin (struct GNUNET_MULTICAST_Member *member,
529 GNUNET_MULTICAST_MemberTransmitNotify notify,
537 * Cancel request for message transmission to origin.
539 * @param rh Request to cancel.
542 GNUNET_MULTICAST_member_to_origin_cancel (struct GNUNET_MULTICAST_MemberRequestHandle *rh)
547 /* end of multicast_api.c */