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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
48 * Handle for a cadet to another peer.
54 * Handle for a request that is going out via cadet API.
56 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 * Handle for active write operation, or NULL.
131 struct GNUNET_CADET_TransmitHandle *wh;
134 * Which peer does this cadet go to?
136 struct GNUNET_PeerIdentity target;
139 * Task to kill inactive cadets (we keep them around for
140 * a few seconds to give the application a chance to give
143 struct GNUNET_SCHEDULER_Task * timeout_task;
146 * Task to reset cadets that had errors (asynchronously,
147 * as we may not be able to do it immediately during a
148 * callback from the cadet API).
150 struct GNUNET_SCHEDULER_Task * reset_task;
156 * Cadet channel for creating outbound channels.
158 struct GNUNET_CADET_Handle *cadet_handle;
161 * Map from peer identities to 'struct CadetHandles' with cadet
162 * channels to those peers.
164 struct GNUNET_CONTAINER_MultiPeerMap *cadet_map;
167 /* ********************* client-side code ************************* */
171 * Transmit pending requests via the cadet.
173 * @param mh cadet to process
176 transmit_pending (struct CadetHandle *mh);
180 * Iterator called on each entry in a waiting map to
181 * move it back to the pending list.
183 * @param cls the `struct CadetHandle`
184 * @param key the key of the entry in the map (the query)
185 * @param value the `struct GSF_CadetRequest` to move to pending
186 * @return #GNUNET_YES (continue to iterate)
189 move_to_pending (void *cls,
190 const struct GNUNET_HashCode *key,
193 struct CadetHandle *mh = cls;
194 struct GSF_CadetRequest *sr = value;
196 GNUNET_assert (GNUNET_YES ==
197 GNUNET_CONTAINER_multihashmap_remove (mh->waiting_map,
200 GNUNET_CONTAINER_DLL_insert (mh->pending_head,
203 sr->was_transmitted = GNUNET_NO;
209 * We had a serious error, tear down and re-create cadet from scratch.
211 * @param mh cadet to reset
214 reset_cadet (struct CadetHandle *mh)
216 struct GNUNET_CADET_Channel *channel = mh->channel;
217 struct GNUNET_HashCode port;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Resetting cadet channel to %s\n",
221 GNUNET_i2s (&mh->target));
229 GNUNET_CADET_notify_transmit_ready_cancel (mh->wh);
232 GNUNET_CADET_channel_destroy (channel);
234 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map,
237 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
238 strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
240 mh->channel = GNUNET_CADET_channel_create (cadet_handle,
244 GNUNET_CADET_OPTION_RELIABLE);
245 transmit_pending (mh);
250 * Task called when it is time to destroy an inactive cadet channel.
252 * @param cls the `struct CadetHandle` to tear down
255 cadet_timeout (void *cls)
257 struct CadetHandle *mh = cls;
258 struct GNUNET_CADET_Channel *tun;
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Timeout on cadet channel to %s\n",
262 GNUNET_i2s (&mh->target));
263 mh->timeout_task = NULL;
267 GNUNET_CADET_channel_destroy (tun);
272 * Task called when it is time to reset an cadet.
274 * @param cls the `struct CadetHandle` to tear down
277 reset_cadet_task (void *cls)
279 struct CadetHandle *mh = cls;
281 mh->reset_task = NULL;
287 * We had a serious error, tear down and re-create cadet from scratch,
288 * but do so asynchronously.
290 * @param mh cadet to reset
293 reset_cadet_async (struct CadetHandle *mh)
295 if (NULL != mh->reset_task)
296 GNUNET_SCHEDULER_cancel (mh->reset_task);
297 mh->reset_task = GNUNET_SCHEDULER_add_now (&reset_cadet_task,
303 * Functions of this signature are called whenever we are ready to transmit
306 * @param cls the struct CadetHandle for which we did the write call
307 * @param size the number of bytes that can be written to @a buf
308 * @param buf where to write the message
309 * @return number of bytes written to @a buf
312 transmit_sqm (void *cls,
316 struct CadetHandle *mh = cls;
317 struct CadetQueryMessage sqm;
318 struct GSF_CadetRequest *sr;
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Cadet channel to %s failed during transmission attempt, rebuilding\n",
325 GNUNET_i2s (&mh->target));
326 reset_cadet_async (mh);
329 sr = mh->pending_head;
332 GNUNET_assert (size >= sizeof (struct CadetQueryMessage));
333 GNUNET_CONTAINER_DLL_remove (mh->pending_head,
336 GNUNET_assert (GNUNET_OK ==
337 GNUNET_CONTAINER_multihashmap_put (mh->waiting_map,
340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
341 sr->was_transmitted = GNUNET_YES;
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Sending query for %s via cadet to %s\n",
344 GNUNET_h2s (&sr->query),
345 GNUNET_i2s (&mh->target));
346 sqm.header.size = htons (sizeof (sqm));
347 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_CADET_QUERY);
348 sqm.type = htonl (sr->type);
349 sqm.query = sr->query;
350 GNUNET_memcpy (buf, &sqm, sizeof (sqm));
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352 "Successfully transmitted %u bytes via cadet to %s\n",
354 GNUNET_i2s (&mh->target));
355 transmit_pending (mh);
361 * Transmit pending requests via the cadet.
363 * @param mh cadet to process
366 transmit_pending (struct CadetHandle *mh)
368 if (NULL == mh->channel)
372 mh->wh = GNUNET_CADET_notify_transmit_ready (mh->channel, GNUNET_YES /* allow cork */,
373 GNUNET_TIME_UNIT_FOREVER_REL,
374 sizeof (struct CadetQueryMessage),
380 * Closure for handle_reply().
382 struct HandleReplyClosure
391 * Expiration time for the block.
393 struct GNUNET_TIME_Absolute expiration;
396 * Number of bytes in 'data'.
403 enum GNUNET_BLOCK_Type type;
406 * Did we have a matching query?
413 * Iterator called on each entry in a waiting map to
416 * @param cls the `struct HandleReplyClosure`
417 * @param key the key of the entry in the map (the query)
418 * @param value the `struct GSF_CadetRequest` to handle result for
419 * @return #GNUNET_YES (continue to iterate)
422 process_reply (void *cls,
423 const struct GNUNET_HashCode *key,
426 struct HandleReplyClosure *hrc = cls;
427 struct GSF_CadetRequest *sr = value;
429 sr->proc (sr->proc_cls,
435 GSF_cadet_query_cancel (sr);
436 hrc->found = GNUNET_YES;
442 * Functions with this signature are called whenever a complete reply
445 * @param cls closure with the `struct CadetHandle`
446 * @param srm the actual message
447 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
450 check_reply (void *cls,
451 const struct CadetReplyMessage *srm)
453 /* We check later... */
459 * Functions with this signature are called whenever a complete reply
462 * @param cls closure with the `struct CadetHandle`
463 * @param srm the actual message
466 handle_reply (void *cls,
467 const struct CadetReplyMessage *srm)
469 struct CadetHandle *mh = cls;
470 struct HandleReplyClosure hrc;
472 enum GNUNET_BLOCK_Type type;
473 struct GNUNET_HashCode query;
475 msize = ntohs (srm->header.size) - sizeof (struct CadetReplyMessage);
476 type = (enum GNUNET_BLOCK_Type) ntohl (srm->type);
478 GNUNET_BLOCK_get_key (GSF_block_ctx,
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 "Received bogus reply of type %u with %u bytes via cadet from peer %s\n",
489 GNUNET_i2s (&mh->target));
490 reset_cadet_async (mh);
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Received reply `%s' via cadet from peer %s\n",
496 GNUNET_i2s (&mh->target));
497 GNUNET_CADET_receive_done (mh->channel);
498 GNUNET_STATISTICS_update (GSF_stats,
499 gettext_noop ("# replies received via cadet"), 1,
502 hrc.data_size = msize;
503 hrc.expiration = GNUNET_TIME_absolute_ntoh (srm->expiration);
505 hrc.found = GNUNET_NO;
506 GNUNET_CONTAINER_multihashmap_get_multiple (mh->waiting_map,
510 if (GNUNET_NO == hrc.found)
512 GNUNET_STATISTICS_update (GSF_stats,
513 gettext_noop ("# replies received via cadet dropped"), 1,
520 * Iterator called on each entry in a waiting map to
521 * call the 'proc' continuation and release associated
524 * @param cls the `struct CadetHandle`
525 * @param key the key of the entry in the map (the query)
526 * @param value the `struct GSF_CadetRequest` to clean up
527 * @return #GNUNET_YES (continue to iterate)
530 free_waiting_entry (void *cls,
531 const struct GNUNET_HashCode *key,
534 struct GSF_CadetRequest *sr = value;
536 GSF_cadet_query_cancel (sr);
542 * Function called by cadet when a client disconnects.
543 * Cleans up our `struct CadetClient` of that channel.
545 * @param cls our `struct CadetClient`
546 * @param channel channel of the disconnecting client
549 disconnect_cb (void *cls,
550 const struct GNUNET_CADET_Channel *channel)
552 struct CadetHandle *mh = cls;
553 struct GSF_CadetRequest *sr;
555 if (NULL == mh->channel)
556 return; /* being destroyed elsewhere */
557 GNUNET_assert (channel == mh->channel);
559 while (NULL != (sr = mh->pending_head))
560 GSF_cadet_query_cancel (sr);
561 /* first remove `mh` from the `cadet_map`, so that if the
562 callback from `free_waiting_entry()` happens to re-issue
563 the request, we don't immediately have it back in the
565 GNUNET_assert (GNUNET_OK ==
566 GNUNET_CONTAINER_multipeermap_remove (cadet_map,
569 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map,
573 GNUNET_CADET_notify_transmit_ready_cancel (mh->wh);
574 if (NULL != mh->timeout_task)
575 GNUNET_SCHEDULER_cancel (mh->timeout_task);
576 if (NULL != mh->reset_task)
577 GNUNET_SCHEDULER_cancel (mh->reset_task);
579 GNUNET_CONTAINER_multihashmap_size (mh->waiting_map));
580 GNUNET_CONTAINER_multihashmap_destroy (mh->waiting_map);
586 * Function called whenever an MQ-channel's transmission window size changes.
588 * The first callback in an outgoing channel will be with a non-zero value
589 * and will mean the channel is connected to the destination.
591 * For an incoming channel it will be called immediately after the
592 * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
594 * @param cls Channel closure.
595 * @param channel Connection to the other end (henceforth invalid).
596 * @param window_size New window size. If the is more messages than buffer size
597 * this value will be negative..
600 window_change_cb (void *cls,
601 const struct GNUNET_CADET_Channel *channel,
604 /* FIXME: for flow control, implement? */
609 * Get (or create) a cadet to talk to the given peer.
611 * @param target peer we want to communicate with
613 static struct CadetHandle *
614 get_cadet (const struct GNUNET_PeerIdentity *target)
616 struct CadetHandle *mh;
617 struct GNUNET_HashCode port;
619 mh = GNUNET_CONTAINER_multipeermap_get (cadet_map,
623 if (NULL != mh->timeout_task)
625 GNUNET_SCHEDULER_cancel (mh->timeout_task);
626 mh->timeout_task = NULL;
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Creating cadet channel to %s\n",
632 GNUNET_i2s (target));
633 mh = GNUNET_new (struct CadetHandle);
634 mh->reset_task = GNUNET_SCHEDULER_add_delayed (CLIENT_RETRY_TIMEOUT,
637 mh->waiting_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
638 mh->target = *target;
639 GNUNET_assert (GNUNET_OK ==
640 GNUNET_CONTAINER_multipeermap_put (cadet_map,
643 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
644 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
645 strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
649 struct GNUNET_MQ_MessageHandler handlers[] = {
650 GNUNET_MQ_hd_var_size (reply,
651 GNUNET_MESSAGE_TYPE_FS_CADET_REPLY,
652 struct CadetReplyMessage,
654 GNUNET_MQ_handler_end ()
657 mh->channel = GNUNET_CADET_channel_creatE (cadet_handle,
661 GNUNET_CADET_OPTION_RELIABLE,
671 * Look for a block by directly contacting a particular peer.
673 * @param target peer that should have the block
674 * @param query hash to query for the block
675 * @param type desired type for the block
676 * @param proc function to call with result
677 * @param proc_cls closure for @a proc
678 * @return handle to cancel the operation
680 struct GSF_CadetRequest *
681 GSF_cadet_query (const struct GNUNET_PeerIdentity *target,
682 const struct GNUNET_HashCode *query,
683 enum GNUNET_BLOCK_Type type,
684 GSF_CadetReplyProcessor proc, void *proc_cls)
686 struct CadetHandle *mh;
687 struct GSF_CadetRequest *sr;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Preparing to send query for %s via cadet to %s\n",
692 GNUNET_i2s (target));
693 mh = get_cadet (target);
694 sr = GNUNET_new (struct GSF_CadetRequest);
697 sr->proc_cls = proc_cls;
700 GNUNET_CONTAINER_DLL_insert (mh->pending_head,
703 transmit_pending (mh);
709 * Cancel an active request; must not be called after 'proc'
712 * @param sr request to cancel
715 GSF_cadet_query_cancel (struct GSF_CadetRequest *sr)
717 struct CadetHandle *mh = sr->mh;
718 GSF_CadetReplyProcessor p;
724 /* signal failure / cancellation to callback */
725 p (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY,
726 GNUNET_TIME_UNIT_ZERO_ABS,
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730 "Cancelled query for %s via cadet to %s\n",
731 GNUNET_h2s (&sr->query),
732 GNUNET_i2s (&sr->mh->target));
733 if (GNUNET_YES == sr->was_transmitted)
734 GNUNET_assert (GNUNET_OK ==
735 GNUNET_CONTAINER_multihashmap_remove (mh->waiting_map,
739 GNUNET_CONTAINER_DLL_remove (mh->pending_head,
743 if ( (0 == GNUNET_CONTAINER_multihashmap_size (mh->waiting_map)) &&
744 (NULL == mh->pending_head) )
745 mh->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
752 * Function called on each active cadets to shut them down.
755 * @param key target peer, unused
756 * @param value the `struct CadetHandle` to destroy
757 * @return #GNUNET_YES (continue to iterate)
760 GSF_cadet_release_clients (void *cls,
761 const struct GNUNET_PeerIdentity *key,
764 struct CadetHandle *mh = value;
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Timeout on cadet channel to %s\n",
768 GNUNET_i2s (&mh->target));
769 if (NULL != mh->channel)
770 GNUNET_CADET_channel_destroy (mh->channel);
776 /* end of gnunet-service-fs_cadet_client.c */