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 fs/gnunet-service-fs_mesh_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_mesh_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_mesh.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 mesh to another peer.
54 * Handle for a request that is going out via mesh API.
56 struct GSF_MeshRequest
62 struct GSF_MeshRequest *next;
67 struct GSF_MeshRequest *prev;
70 * Which mesh is this request associated with?
72 struct MeshHandle *mh;
75 * Function to call with the result.
77 GSF_MeshReplyProcessor 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? YES if we are
96 * in the 'waiting_map', NO if we are in the 'pending' DLL.
103 * Handle for a mesh to another peer.
108 * Head of DLL of pending requests on this mesh.
110 struct GSF_MeshRequest *pending_head;
113 * Tail of DLL of pending requests on this mesh.
115 struct GSF_MeshRequest *pending_tail;
118 * Map from query to 'struct GSF_MeshRequest's waiting for
121 struct GNUNET_CONTAINER_MultiHashMap *waiting_map;
124 * Tunnel to the other peer.
126 struct GNUNET_MESH_Tunnel *tunnel;
129 * Handle for active write operation, or NULL.
131 struct GNUNET_MESH_TransmitHandle *wh;
134 * Which peer does this mesh go to?
136 struct GNUNET_PeerIdentity target;
139 * Task to kill inactive meshs (we keep them around for
140 * a few seconds to give the application a chance to give
143 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
146 * Task to reset meshs that had errors (asynchronously,
147 * as we may not be able to do it immediately during a
148 * callback from the mesh API).
150 GNUNET_SCHEDULER_TaskIdentifier reset_task;
156 * Mesh tunnel for creating outbound tunnels.
158 static struct GNUNET_MESH_Handle *mesh_handle;
161 * Map from peer identities to 'struct MeshHandles' with mesh
162 * tunnels to those peers.
164 static struct GNUNET_CONTAINER_MultiHashMap *mesh_map;
167 /* ********************* client-side code ************************* */
171 * Transmit pending requests via the mesh.
173 * @param mh mesh to process
176 transmit_pending (struct MeshHandle *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 MeshHandle'
184 * @param key the key of the entry in the map (the query)
185 * @param value the 'struct GSF_MeshRequest' 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 MeshHandle *mh = cls;
194 struct GSF_MeshRequest *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 mesh from scratch.
211 * @param mh mesh to reset
214 reset_mesh (struct MeshHandle *mh)
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "Resetting mesh tunnel to %s\n",
218 GNUNET_i2s (&mh->target));
219 GNUNET_MESH_tunnel_destroy (mh->tunnel);
220 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map,
223 mh->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
226 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
229 transmit_pending (mh);
234 * Task called when it is time to destroy an inactive mesh tunnel.
236 * @param cls the 'struct MeshHandle' to tear down
237 * @param tc scheduler context, unused
240 mesh_timeout (void *cls,
241 const struct GNUNET_SCHEDULER_TaskContext *tc)
243 struct MeshHandle *mh = cls;
244 struct GNUNET_MESH_Tunnel *tun;
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Timeout on mesh tunnel to %s\n",
248 GNUNET_i2s (&mh->target));
249 mh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
252 GNUNET_MESH_tunnel_destroy (tun);
257 * Task called when it is time to reset an mesh.
259 * @param cls the 'struct MeshHandle' to tear down
260 * @param tc scheduler context, unused
263 reset_mesh_task (void *cls,
264 const struct GNUNET_SCHEDULER_TaskContext *tc)
266 struct MeshHandle *mh = cls;
268 mh->reset_task = GNUNET_SCHEDULER_NO_TASK;
274 * We had a serious error, tear down and re-create mesh from scratch,
275 * but do so asynchronously.
277 * @param mh mesh to reset
280 reset_mesh_async (struct MeshHandle *mh)
282 if (GNUNET_SCHEDULER_NO_TASK != mh->reset_task)
283 GNUNET_SCHEDULER_cancel (mh->reset_task);
284 mh->reset_task = GNUNET_SCHEDULER_add_now (&reset_mesh_task,
290 * Functions of this signature are called whenever we are ready to transmit
293 * @param cls the struct MeshHandle for which we did the write call
294 * @param size the number of bytes that can be written to 'buf'
295 * @param buf where to write the message
296 * @return number of bytes written to 'buf'
299 transmit_sqm (void *cls,
303 struct MeshHandle *mh = cls;
304 struct MeshQueryMessage sqm;
305 struct GSF_MeshRequest *sr;
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
311 "Mesh tunnel to %s failed during transmission attempt, rebuilding\n",
312 GNUNET_i2s (&mh->target));
313 reset_mesh_async (mh);
316 sr = mh->pending_head;
319 GNUNET_assert (size >= sizeof (struct MeshQueryMessage));
320 GNUNET_CONTAINER_DLL_remove (mh->pending_head,
323 GNUNET_assert (GNUNET_OK ==
324 GNUNET_CONTAINER_multihashmap_put (mh->waiting_map,
327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
328 sr->was_transmitted = GNUNET_YES;
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Sending query for %s via mesh to %s\n",
331 GNUNET_h2s (&sr->query),
332 GNUNET_i2s (&mh->target));
333 sqm.header.size = htons (sizeof (sqm));
334 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_MESH_QUERY);
335 sqm.type = htonl (sr->type);
336 sqm.query = sr->query;
337 memcpy (buf, &sqm, sizeof (sqm));
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Successfully transmitted %u bytes via mesh to %s\n",
341 GNUNET_i2s (&mh->target));
342 transmit_pending (mh);
348 * Transmit pending requests via the mesh.
350 * @param mh mesh to process
353 transmit_pending (struct MeshHandle *mh)
357 mh->wh = GNUNET_MESH_notify_transmit_ready (mh->tunnel, GNUNET_YES /* allow cork */,
358 GNUNET_TIME_UNIT_FOREVER_REL,
359 sizeof (struct MeshQueryMessage),
365 * Closure for 'handle_reply'.
367 struct HandleReplyClosure
376 * Expiration time for the block.
378 struct GNUNET_TIME_Absolute expiration;
381 * Number of bytes in 'data'.
388 enum GNUNET_BLOCK_Type type;
391 * Did we have a matching query?
398 * Iterator called on each entry in a waiting map to
401 * @param cls the 'struct HandleReplyClosure'
402 * @param key the key of the entry in the map (the query)
403 * @param value the 'struct GSF_MeshRequest' to handle result for
404 * @return GNUNET_YES (continue to iterate)
407 handle_reply (void *cls,
408 const struct GNUNET_HashCode *key,
411 struct HandleReplyClosure *hrc = cls;
412 struct GSF_MeshRequest *sr = value;
414 sr->proc (sr->proc_cls,
419 GSF_mesh_query_cancel (sr);
420 hrc->found = GNUNET_YES;
426 * Functions with this signature are called whenever a complete reply
429 * @param cls closure with the 'struct MeshHandle'
430 * @param tunnel tunnel handle
431 * @param tunnel_ctx tunnel context
432 * @param message the actual message
433 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
437 struct GNUNET_MESH_Tunnel *tunnel,
439 const struct GNUNET_MessageHeader *message)
441 struct MeshHandle *mh = *tunnel_ctx;
442 const struct MeshReplyMessage *srm;
443 struct HandleReplyClosure hrc;
445 enum GNUNET_BLOCK_Type type;
446 struct GNUNET_HashCode query;
448 msize = ntohs (message->size);
449 if (sizeof (struct MeshReplyMessage) > msize)
452 reset_mesh_async (mh);
453 return GNUNET_SYSERR;
455 srm = (const struct MeshReplyMessage *) message;
456 msize -= sizeof (struct MeshReplyMessage);
457 type = (enum GNUNET_BLOCK_Type) ntohl (srm->type);
459 GNUNET_BLOCK_get_key (GSF_block_ctx,
461 &srm[1], msize, &query))
464 reset_mesh_async (mh);
465 return GNUNET_SYSERR;
467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
468 "Received reply `%s' via mesh from peer %s\n",
470 GNUNET_i2s (&mh->target));
471 GNUNET_MESH_receive_done (tunnel);
472 GNUNET_STATISTICS_update (GSF_stats,
473 gettext_noop ("# replies received via mesh"), 1,
476 hrc.data_size = msize;
477 hrc.expiration = GNUNET_TIME_absolute_ntoh (srm->expiration);
479 hrc.found = GNUNET_NO;
480 GNUNET_CONTAINER_multihashmap_get_multiple (mh->waiting_map,
484 if (GNUNET_NO == hrc.found)
486 GNUNET_STATISTICS_update (GSF_stats,
487 gettext_noop ("# replies received via mesh dropped"), 1,
496 * Get (or create) a mesh to talk to the given peer.
498 * @param target peer we want to communicate with
500 static struct MeshHandle *
501 get_mesh (const struct GNUNET_PeerIdentity *target)
503 struct MeshHandle *mh;
505 mh = GNUNET_CONTAINER_multihashmap_get (mesh_map,
506 &target->hashPubKey);
509 if (GNUNET_SCHEDULER_NO_TASK != mh->timeout_task)
511 GNUNET_SCHEDULER_cancel (mh->timeout_task);
512 mh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "Creating mesh tunnel to %s\n",
518 GNUNET_i2s (target));
519 mh = GNUNET_new (struct MeshHandle);
520 mh->reset_task = GNUNET_SCHEDULER_add_delayed (CLIENT_RETRY_TIMEOUT,
523 mh->waiting_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
524 mh->target = *target;
525 mh->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
528 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
531 GNUNET_assert (GNUNET_OK ==
532 GNUNET_CONTAINER_multihashmap_put (mesh_map,
533 &mh->target.hashPubKey,
535 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
541 * Look for a block by directly contacting a particular peer.
543 * @param target peer that should have the block
544 * @param query hash to query for the block
545 * @param type desired type for the block
546 * @param proc function to call with result
547 * @param proc_cls closure for 'proc'
548 * @return handle to cancel the operation
550 struct GSF_MeshRequest *
551 GSF_mesh_query (const struct GNUNET_PeerIdentity *target,
552 const struct GNUNET_HashCode *query,
553 enum GNUNET_BLOCK_Type type,
554 GSF_MeshReplyProcessor proc, void *proc_cls)
556 struct MeshHandle *mh;
557 struct GSF_MeshRequest *sr;
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Preparing to send query for %s via mesh to %s\n",
562 GNUNET_i2s (target));
563 mh = get_mesh (target);
564 sr = GNUNET_new (struct GSF_MeshRequest);
567 sr->proc_cls = proc_cls;
570 GNUNET_CONTAINER_DLL_insert (mh->pending_head,
573 transmit_pending (mh);
579 * Cancel an active request; must not be called after 'proc'
582 * @param sr request to cancel
585 GSF_mesh_query_cancel (struct GSF_MeshRequest *sr)
587 struct MeshHandle *mh = sr->mh;
589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
590 "Cancelled query for %s via mesh to %s\n",
591 GNUNET_h2s (&sr->query),
592 GNUNET_i2s (&sr->mh->target));
593 if (GNUNET_YES == sr->was_transmitted)
594 GNUNET_assert (GNUNET_OK ==
595 GNUNET_CONTAINER_multihashmap_remove (mh->waiting_map,
599 GNUNET_CONTAINER_DLL_remove (mh->pending_head,
603 if ( (0 == GNUNET_CONTAINER_multihashmap_size (mh->waiting_map)) &&
604 (NULL == mh->pending_head) )
605 mh->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
612 * Iterator called on each entry in a waiting map to
613 * call the 'proc' continuation and release associated
616 * @param cls the 'struct MeshHandle'
617 * @param key the key of the entry in the map (the query)
618 * @param value the 'struct GSF_MeshRequest' to clean up
619 * @return GNUNET_YES (continue to iterate)
622 free_waiting_entry (void *cls,
623 const struct GNUNET_HashCode *key,
626 struct GSF_MeshRequest *sr = value;
628 sr->proc (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY,
629 GNUNET_TIME_UNIT_FOREVER_ABS,
631 GSF_mesh_query_cancel (sr);
637 * Function called by mesh when a client disconnects.
638 * Cleans up our 'struct MeshClient' of that tunnel.
641 * @param tunnel tunnel of the disconnecting client
642 * @param tunnel_ctx our 'struct MeshClient'
645 cleaner_cb (void *cls,
646 const struct GNUNET_MESH_Tunnel *tunnel,
649 struct MeshHandle *mh = tunnel_ctx;
650 struct GSF_MeshRequest *sr;
653 while (NULL != (sr = mh->pending_head))
655 sr->proc (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY,
656 GNUNET_TIME_UNIT_FOREVER_ABS,
658 GSF_mesh_query_cancel (sr);
660 GNUNET_CONTAINER_multihashmap_iterate (mh->waiting_map,
664 GNUNET_MESH_notify_transmit_ready_cancel (mh->wh);
665 if (GNUNET_SCHEDULER_NO_TASK != mh->timeout_task)
666 GNUNET_SCHEDULER_cancel (mh->timeout_task);
667 if (GNUNET_SCHEDULER_NO_TASK != mh->reset_task)
668 GNUNET_SCHEDULER_cancel (mh->reset_task);
669 GNUNET_assert (GNUNET_OK ==
670 GNUNET_CONTAINER_multihashmap_remove (mesh_map,
671 &mh->target.hashPubKey,
673 GNUNET_CONTAINER_multihashmap_destroy (mh->waiting_map);
679 * Initialize subsystem for non-anonymous file-sharing.
682 GSF_mesh_start_client ()
684 static const struct GNUNET_MESH_MessageHandler handlers[] = {
685 { &reply_cb, GNUNET_MESSAGE_TYPE_FS_MESH_REPLY, 0 },
689 mesh_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
690 mesh_handle = GNUNET_MESH_connect (GSF_cfg,
700 * Function called on each active meshs to shut them down.
703 * @param key target peer, unused
704 * @param value the 'struct MeshHandle' to destroy
705 * @return GNUNET_YES (continue to iterate)
708 release_meshs (void *cls,
709 const struct GNUNET_HashCode *key,
712 struct MeshHandle *mh = value;
713 struct GNUNET_MESH_Tunnel *tun;
715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
716 "Timeout on mesh tunnel to %s\n",
717 GNUNET_i2s (&mh->target));
721 GNUNET_MESH_tunnel_destroy (tun);
727 * Shutdown subsystem for non-anonymous file-sharing.
730 GSF_mesh_stop_client ()
732 GNUNET_CONTAINER_multihashmap_iterate (mesh_map,
735 GNUNET_CONTAINER_multihashmap_destroy (mesh_map);
737 if (NULL != mesh_handle)
739 GNUNET_MESH_disconnect (mesh_handle);
745 /* end of gnunet-service-fs_mesh_client.c */