2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file fs/gnunet-service-fs_cadet_client.c
23 * @brief non-anonymous file-transfer
24 * @author Christian Grothoff
27 * - PORT is set to old application type, unsure if we should keep
28 * it that way (fine for now)
31 #include "gnunet_constants.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_cadet_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_applications.h"
36 #include "gnunet-service-fs.h"
37 #include "gnunet-service-fs_indexing.h"
38 #include "gnunet-service-fs_cadet.h"
42 * After how long do we reset connections without replies?
44 #define CLIENT_RETRY_TIMEOUT \
45 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 * Handle for a cadet to another peer.
55 * Handle for a request that is going out via cadet API.
57 struct GSF_CadetRequest
62 struct GSF_CadetRequest *next;
67 struct GSF_CadetRequest *prev;
70 * Which cadet is this request associated with?
72 struct CadetHandle *mh;
75 * Function to call with the result.
77 GSF_CadetReplyProcessor proc;
85 * Query to transmit to the other peer.
87 struct GNUNET_HashCode query;
90 * Desired type for the reply.
92 enum GNUNET_BLOCK_Type type;
95 * Did we transmit this request already? #GNUNET_YES if we are
96 * in the 'waiting_map', #GNUNET_NO if we are in the 'pending' DLL.
103 * Handle for a cadet to another peer.
108 * Head of DLL of pending requests on this cadet.
110 struct GSF_CadetRequest *pending_head;
113 * Tail of DLL of pending requests on this cadet.
115 struct GSF_CadetRequest *pending_tail;
118 * Map from query to `struct GSF_CadetRequest`s waiting for
121 struct GNUNET_CONTAINER_MultiHashMap *waiting_map;
124 * Channel to the other peer.
126 struct GNUNET_CADET_Channel *channel;
129 * Which peer does this cadet go to?
131 struct GNUNET_PeerIdentity target;
134 * Task to kill inactive cadets (we keep them around for
135 * a few seconds to give the application a chance to give
138 struct GNUNET_SCHEDULER_Task *timeout_task;
141 * Task to reset cadets that had errors (asynchronously,
142 * as we may not be able to do it immediately during a
143 * callback from the cadet API).
145 struct GNUNET_SCHEDULER_Task *reset_task;
150 * Cadet channel for creating outbound channels.
152 struct GNUNET_CADET_Handle *cadet_handle;
155 * Map from peer identities to 'struct CadetHandles' with cadet
156 * channels to those peers.
158 struct GNUNET_CONTAINER_MultiPeerMap *cadet_map;
161 /* ********************* client-side code ************************* */
165 * Transmit pending requests via the cadet.
167 * @param cls `struct CadetHandle` to process
170 transmit_pending (void *cls);
174 * Iterator called on each entry in a waiting map to
175 * move it back to the pending list.
177 * @param cls the `struct CadetHandle`
178 * @param key the key of the entry in the map (the query)
179 * @param value the `struct GSF_CadetRequest` to move to pending
180 * @return #GNUNET_YES (continue to iterate)
183 move_to_pending (void *cls, const struct GNUNET_HashCode *key, void *value)
185 struct CadetHandle *mh = cls;
186 struct GSF_CadetRequest *sr = value;
190 GNUNET_CONTAINER_multihashmap_remove (mh->waiting_map, key, value));
191 GNUNET_CONTAINER_DLL_insert (mh->pending_head, mh->pending_tail, sr);
192 sr->was_transmitted = GNUNET_NO;
198 * Functions with this signature are called whenever a complete reply
201 * @param cls closure with the `struct CadetHandle`
202 * @param srm the actual message
203 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
206 check_reply (void *cls, const struct CadetReplyMessage *srm)
208 /* We check later... */
214 * Task called when it is time to reset an cadet.
216 * @param cls the `struct CadetHandle` to tear down
219 reset_cadet_task (void *cls);
223 * We had a serious error, tear down and re-create cadet from scratch,
224 * but do so asynchronously.
226 * @param mh cadet to reset
229 reset_cadet_async (struct CadetHandle *mh)
231 if (NULL != mh->reset_task)
232 GNUNET_SCHEDULER_cancel (mh->reset_task);
233 mh->reset_task = GNUNET_SCHEDULER_add_now (&reset_cadet_task, mh);
238 * Closure for handle_reply().
240 struct HandleReplyClosure
248 * Expiration time for the block.
250 struct GNUNET_TIME_Absolute expiration;
253 * Number of bytes in @e data.
260 enum GNUNET_BLOCK_Type type;
263 * Did we have a matching query?
270 * Iterator called on each entry in a waiting map to
273 * @param cls the `struct HandleReplyClosure`
274 * @param key the key of the entry in the map (the query)
275 * @param value the `struct GSF_CadetRequest` to handle result for
276 * @return #GNUNET_YES (continue to iterate)
279 process_reply (void *cls, const struct GNUNET_HashCode *key, void *value)
281 struct HandleReplyClosure *hrc = cls;
282 struct GSF_CadetRequest *sr = value;
284 sr->proc (sr->proc_cls,
290 GSF_cadet_query_cancel (sr);
291 hrc->found = GNUNET_YES;
297 * Iterator called on each entry in a waiting map to
298 * call the 'proc' continuation and release associated
301 * @param cls the `struct CadetHandle`
302 * @param key the key of the entry in the map (the query)
303 * @param value the `struct GSF_CadetRequest` to clean up
304 * @return #GNUNET_YES (continue to iterate)
307 free_waiting_entry (void *cls, const struct GNUNET_HashCode *key, void *value)
309 struct GSF_CadetRequest *sr = value;
311 GSF_cadet_query_cancel (sr);
317 * Functions with this signature are called whenever a complete reply
320 * @param cls closure with the `struct CadetHandle`
321 * @param srm the actual message
324 handle_reply (void *cls, const struct CadetReplyMessage *srm)
326 struct CadetHandle *mh = cls;
327 struct HandleReplyClosure hrc;
329 enum GNUNET_BLOCK_Type type;
330 struct GNUNET_HashCode query;
332 msize = ntohs (srm->header.size) - sizeof(struct CadetReplyMessage);
333 type = (enum GNUNET_BLOCK_Type) ntohl (srm->type);
335 GNUNET_BLOCK_get_key (GSF_block_ctx, type, &srm[1], msize, &query))
339 GNUNET_ERROR_TYPE_WARNING,
340 "Received bogus reply of type %u with %u bytes via cadet from peer %s\n",
343 GNUNET_i2s (&mh->target));
344 reset_cadet_async (mh);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Received reply `%s' via cadet from peer %s\n",
350 GNUNET_i2s (&mh->target));
351 GNUNET_CADET_receive_done (mh->channel);
352 GNUNET_STATISTICS_update (GSF_stats,
353 gettext_noop ("# replies received via cadet"),
357 hrc.data_size = msize;
358 hrc.expiration = GNUNET_TIME_absolute_ntoh (srm->expiration);
360 hrc.found = GNUNET_NO;
361 GNUNET_CONTAINER_multihashmap_get_multiple (mh->waiting_map,
365 if (GNUNET_NO == hrc.found)
367 GNUNET_STATISTICS_update (GSF_stats,
369 "# replies received via cadet dropped"),
377 * Function called by cadet when a client disconnects.
378 * Cleans up our `struct CadetClient` of that channel.
380 * @param cls our `struct CadetClient`
381 * @param channel channel of the disconnecting client
384 disconnect_cb (void *cls, const struct GNUNET_CADET_Channel *channel)
386 struct CadetHandle *mh = cls;
387 struct GSF_CadetRequest *sr;
389 if (NULL == mh->channel)
390 return; /* being destroyed elsewhere */
391 GNUNET_assert (channel == mh->channel);
393 while (NULL != (sr = mh->pending_head))
394 GSF_cadet_query_cancel (sr);
395 /* first remove `mh` from the `cadet_map`, so that if the
396 callback from `free_waiting_entry()` happens to re-issue
397 the request, we don't immediately have it back in the
399 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (cadet_map,
402 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map,
405 if (NULL != mh->timeout_task)
406 GNUNET_SCHEDULER_cancel (mh->timeout_task);
407 if (NULL != mh->reset_task)
408 GNUNET_SCHEDULER_cancel (mh->reset_task);
409 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap_size (mh->waiting_map));
410 GNUNET_CONTAINER_multihashmap_destroy (mh->waiting_map);
416 * Function called whenever an MQ-channel's transmission window size changes.
418 * The first callback in an outgoing channel will be with a non-zero value
419 * and will mean the channel is connected to the destination.
421 * For an incoming channel it will be called immediately after the
422 * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
424 * @param cls Channel closure.
425 * @param channel Connection to the other end (henceforth invalid).
426 * @param window_size New window size. If the is more messages than buffer size
427 * this value will be negative..
430 window_change_cb (void *cls,
431 const struct GNUNET_CADET_Channel *channel,
434 /* FIXME: for flow control, implement? */
436 /* Something like this instead of the GNUNET_MQ_notify_sent() in
437 transmit_pending() might be good (once the window change CB works...) */
438 if (0 < window_size) /* test needed? */
439 transmit_pending (mh);
445 * We had a serious error, tear down and re-create cadet from scratch.
447 * @param mh cadet to reset
450 reset_cadet (struct CadetHandle *mh)
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
453 "Resetting cadet channel to %s\n",
454 GNUNET_i2s (&mh->target));
455 if (NULL != mh->channel)
457 GNUNET_CADET_channel_destroy (mh->channel);
460 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map, &move_to_pending, mh);
462 struct GNUNET_MQ_MessageHandler handlers[] =
463 { GNUNET_MQ_hd_var_size (reply,
464 GNUNET_MESSAGE_TYPE_FS_CADET_REPLY,
465 struct CadetReplyMessage,
467 GNUNET_MQ_handler_end () };
468 struct GNUNET_HashCode port;
470 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
471 strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
473 mh->channel = GNUNET_CADET_channel_create (cadet_handle,
481 transmit_pending (mh);
486 * Task called when it is time to destroy an inactive cadet channel.
488 * @param cls the `struct CadetHandle` to tear down
491 cadet_timeout (void *cls)
493 struct CadetHandle *mh = cls;
494 struct GNUNET_CADET_Channel *tun;
496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 "Timeout on cadet channel to %s\n",
498 GNUNET_i2s (&mh->target));
499 mh->timeout_task = NULL;
503 GNUNET_CADET_channel_destroy (tun);
508 * Task called when it is time to reset an cadet.
510 * @param cls the `struct CadetHandle` to tear down
513 reset_cadet_task (void *cls)
515 struct CadetHandle *mh = cls;
517 mh->reset_task = NULL;
523 * Transmit pending requests via the cadet.
525 * @param cls `struct CadetHandle` to process
528 transmit_pending (void *cls)
530 struct CadetHandle *mh = cls;
531 struct GNUNET_MQ_Handle *mq = GNUNET_CADET_get_mq (mh->channel);
532 struct GSF_CadetRequest *sr;
533 struct GNUNET_MQ_Envelope *env;
534 struct CadetQueryMessage *sqm;
536 if ((0 != GNUNET_MQ_get_length (mq)) || (NULL == (sr = mh->pending_head)))
538 GNUNET_CONTAINER_DLL_remove (mh->pending_head, mh->pending_tail, sr);
539 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
544 sr->was_transmitted = GNUNET_YES;
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Sending query for %s via cadet to %s\n",
547 GNUNET_h2s (&sr->query),
548 GNUNET_i2s (&mh->target));
549 env = GNUNET_MQ_msg (sqm, GNUNET_MESSAGE_TYPE_FS_CADET_QUERY);
550 GNUNET_MQ_env_set_options (env,
551 GNUNET_MQ_PREF_GOODPUT
552 | GNUNET_MQ_PREF_CORK_ALLOWED
553 | GNUNET_MQ_PREF_OUT_OF_ORDER);
554 sqm->type = htonl (sr->type);
555 sqm->query = sr->query;
556 GNUNET_MQ_notify_sent (env, &transmit_pending, mh);
557 GNUNET_MQ_send (mq, env);
562 * Get (or create) a cadet to talk to the given peer.
564 * @param target peer we want to communicate with
566 static struct CadetHandle *
567 get_cadet (const struct GNUNET_PeerIdentity *target)
569 struct CadetHandle *mh;
571 mh = GNUNET_CONTAINER_multipeermap_get (cadet_map, target);
574 if (NULL != mh->timeout_task)
576 GNUNET_SCHEDULER_cancel (mh->timeout_task);
577 mh->timeout_task = NULL;
581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
582 "Creating cadet channel to %s\n",
583 GNUNET_i2s (target));
584 mh = GNUNET_new (struct CadetHandle);
586 GNUNET_SCHEDULER_add_delayed (CLIENT_RETRY_TIMEOUT, &reset_cadet_task, mh);
587 mh->waiting_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
588 mh->target = *target;
589 GNUNET_assert (GNUNET_OK ==
590 GNUNET_CONTAINER_multipeermap_put (
594 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
596 struct GNUNET_MQ_MessageHandler handlers[] =
597 { GNUNET_MQ_hd_var_size (reply,
598 GNUNET_MESSAGE_TYPE_FS_CADET_REPLY,
599 struct CadetReplyMessage,
601 GNUNET_MQ_handler_end () };
602 struct GNUNET_HashCode port;
604 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
605 strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
607 mh->channel = GNUNET_CADET_channel_create (cadet_handle,
620 * Look for a block by directly contacting a particular peer.
622 * @param target peer that should have the block
623 * @param query hash to query for the block
624 * @param type desired type for the block
625 * @param proc function to call with result
626 * @param proc_cls closure for @a proc
627 * @return handle to cancel the operation
629 struct GSF_CadetRequest *
630 GSF_cadet_query (const struct GNUNET_PeerIdentity *target,
631 const struct GNUNET_HashCode *query,
632 enum GNUNET_BLOCK_Type type,
633 GSF_CadetReplyProcessor proc,
636 struct CadetHandle *mh;
637 struct GSF_CadetRequest *sr;
639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
640 "Preparing to send query for %s via cadet to %s\n",
642 GNUNET_i2s (target));
643 mh = get_cadet (target);
644 sr = GNUNET_new (struct GSF_CadetRequest);
647 sr->proc_cls = proc_cls;
650 GNUNET_CONTAINER_DLL_insert (mh->pending_head, mh->pending_tail, sr);
651 transmit_pending (mh);
657 * Cancel an active request; must not be called after 'proc'
660 * @param sr request to cancel
663 GSF_cadet_query_cancel (struct GSF_CadetRequest *sr)
665 struct CadetHandle *mh = sr->mh;
666 GSF_CadetReplyProcessor p;
672 /* signal failure / cancellation to callback */
673 p (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY, GNUNET_TIME_UNIT_ZERO_ABS, 0, NULL);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
676 "Cancelled query for %s via cadet to %s\n",
677 GNUNET_h2s (&sr->query),
678 GNUNET_i2s (&sr->mh->target));
679 if (GNUNET_YES == sr->was_transmitted)
682 GNUNET_CONTAINER_multihashmap_remove (mh->waiting_map, &sr->query, sr));
684 GNUNET_CONTAINER_DLL_remove (mh->pending_head, mh->pending_tail, sr);
686 if ((0 == GNUNET_CONTAINER_multihashmap_size (mh->waiting_map)) &&
687 (NULL == mh->pending_head))
688 mh->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
695 * Function called on each active cadets to shut them down.
698 * @param key target peer, unused
699 * @param value the `struct CadetHandle` to destroy
700 * @return #GNUNET_YES (continue to iterate)
703 GSF_cadet_release_clients (void *cls,
704 const struct GNUNET_PeerIdentity *key,
707 struct CadetHandle *mh = value;
709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710 "Timeout on cadet channel to %s\n",
711 GNUNET_i2s (&mh->target));
712 if (NULL != mh->channel)
714 struct GNUNET_CADET_Channel *channel = mh->channel;
717 GNUNET_CADET_channel_destroy (channel);
719 if (NULL != mh->reset_task)
721 GNUNET_SCHEDULER_cancel (mh->reset_task);
722 mh->reset_task = NULL;
728 /* end of gnunet-service-fs_cadet_client.c */