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.c
23 * @brief non-anonymous file-transfer
24 * @author Christian Grothoff
27 * - update comments on functions (still matches 'mesh')
28 * - MESH2 API doesn't allow flow control for server yet (needed!)
29 * - likely need to register clean up handler with mesh to handle
30 * client disconnect (likely leaky right now)
31 * - server is optional, currently client code will NPE if we have
32 * no server, again MESH2 API requirement forcing this for now
33 * - message handlers are symmetric for client/server, should be
34 * separated (currently clients can get requests and servers can
35 * handle answers, not good)
36 * - code is entirely untested
37 * - might have overlooked a few possible simplifications
38 * - PORT is set to old application type, unsure if we should keep
39 * it that way (fine for now)
42 #include "gnunet_constants.h"
43 #include "gnunet_util_lib.h"
44 #include "gnunet_mesh_service.h"
45 #include "gnunet_protocols.h"
46 #include "gnunet_applications.h"
47 #include "gnunet-service-fs.h"
48 #include "gnunet-service-fs_indexing.h"
49 #include "gnunet-service-fs_mesh.h"
52 * After how long do we termiante idle connections?
54 #define IDLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
57 * After how long do we reset connections without replies?
59 #define CLIENT_RETRY_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
63 * A message in the queue to be written to the mesh.
70 struct WriteQueueItem *next;
75 struct WriteQueueItem *prev;
78 * Number of bytes of payload, allocated at the end of this struct.
85 * Information we keep around for each active meshing client.
92 struct StreamClient *next;
97 struct StreamClient *prev;
100 * Socket for communication.
102 struct GNUNET_MESH_Tunnel *socket;
105 * Handle for active write operation, or NULL.
107 struct GNUNET_MESH_TransmitHandle *wh;
110 * Head of write queue.
112 struct WriteQueueItem *wqi_head;
115 * Tail of write queue.
117 struct WriteQueueItem *wqi_tail;
120 * Current active request to the datastore, if we have one pending.
122 struct GNUNET_DATASTORE_QueueEntry *qe;
125 * Task that is scheduled to asynchronously terminate the connection.
127 GNUNET_SCHEDULER_TaskIdentifier terminate_task;
130 * Task that is scheduled to terminate idle connections.
132 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
135 * Size of the last write that was initiated.
143 * Query from one peer, asking the other for CHK-data.
145 struct StreamQueryMessage
149 * Type is GNUNET_MESSAGE_TYPE_FS_STREAM_QUERY.
151 struct GNUNET_MessageHeader header;
154 * Block type must be DBLOCK or IBLOCK.
159 * Query hash from CHK (hash of encrypted block).
161 struct GNUNET_HashCode query;
167 * Reply to a StreamQueryMessage.
169 struct StreamReplyMessage
173 * Type is GNUNET_MESSAGE_TYPE_FS_STREAM_REPLY.
175 struct GNUNET_MessageHeader header;
178 * Block type must be DBLOCK or IBLOCK.
183 * Expiration time for the block.
185 struct GNUNET_TIME_AbsoluteNBO expiration;
187 /* followed by the encrypted block */
193 * Handle for a mesh to another peer.
199 * Handle for a request that is going out via mesh API.
201 struct GSF_StreamRequest
207 struct GSF_StreamRequest *next;
212 struct GSF_StreamRequest *prev;
215 * Which mesh is this request associated with?
217 struct StreamHandle *sh;
220 * Function to call with the result.
222 GSF_StreamReplyProcessor proc;
230 * Query to transmit to the other peer.
232 struct GNUNET_HashCode query;
235 * Desired type for the reply.
237 enum GNUNET_BLOCK_Type type;
240 * Did we transmit this request already? YES if we are
241 * in the 'waiting' DLL, NO if we are in the 'pending' DLL.
248 * Handle for a mesh to another peer.
253 * Head of DLL of pending requests on this mesh.
255 struct GSF_StreamRequest *pending_head;
258 * Tail of DLL of pending requests on this mesh.
260 struct GSF_StreamRequest *pending_tail;
263 * Map from query to 'struct GSF_StreamRequest's waiting for
266 struct GNUNET_CONTAINER_MultiHashMap *waiting_map;
269 * Connection to the other peer.
271 struct GNUNET_MESH_Tunnel *mesh;
274 * Handle for active write operation, or NULL.
276 struct GNUNET_MESH_TransmitHandle *wh;
279 * Which peer does this mesh go to?
281 struct GNUNET_PeerIdentity target;
284 * Task to kill inactive meshs (we keep them around for
285 * a few seconds to give the application a chance to give
288 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
291 * Task to reset meshs that had errors (asynchronously,
292 * as we may not be able to do it immediately during a
293 * callback from the mesh API).
295 GNUNET_SCHEDULER_TaskIdentifier reset_task;
298 * Is this mesh ready for transmission?
306 * Listen socket for incoming requests.
308 static struct GNUNET_MESH_Handle *listen_socket;
311 * Head of DLL of mesh clients.
313 static struct StreamClient *sc_head;
316 * Tail of DLL of mesh clients.
318 static struct StreamClient *sc_tail;
321 * Number of active mesh clients in the 'sc_*'-DLL.
323 static unsigned int sc_count;
326 * Maximum allowed number of mesh clients.
328 static unsigned long long sc_count_max;
331 * Map from peer identities to 'struct StreamHandles' with meshs to
334 static struct GNUNET_CONTAINER_MultiHashMap *mesh_map;
337 /* ********************* client-side code ************************* */
340 * Iterator called on each entry in a waiting map to
341 * call the 'proc' continuation and release associated
344 * @param cls the 'struct StreamHandle'
345 * @param key the key of the entry in the map (the query)
346 * @param value the 'struct GSF_StreamRequest' to clean up
347 * @return GNUNET_YES (continue to iterate)
350 free_waiting_entry (void *cls,
351 const struct GNUNET_HashCode *key,
354 struct GSF_StreamRequest *sr = value;
356 sr->proc (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY,
357 GNUNET_TIME_UNIT_FOREVER_ABS,
359 GSF_mesh_query_cancel (sr);
365 * Destroy a mesh handle.
367 * @param sh mesh to process
370 destroy_mesh_handle (struct StreamHandle *sh)
372 struct GSF_StreamRequest *sr;
374 while (NULL != (sr = sh->pending_head))
376 sr->proc (sr->proc_cls, GNUNET_BLOCK_TYPE_ANY,
377 GNUNET_TIME_UNIT_FOREVER_ABS,
379 GSF_mesh_query_cancel (sr);
381 GNUNET_CONTAINER_multihashmap_iterate (sh->waiting_map,
385 GNUNET_MESH_notify_transmit_ready_cancel (sh->wh);
386 if (GNUNET_SCHEDULER_NO_TASK != sh->timeout_task)
387 GNUNET_SCHEDULER_cancel (sh->timeout_task);
388 if (GNUNET_SCHEDULER_NO_TASK != sh->reset_task)
389 GNUNET_SCHEDULER_cancel (sh->reset_task);
390 GNUNET_MESH_tunnel_destroy (sh->mesh);
391 GNUNET_assert (GNUNET_OK ==
392 GNUNET_CONTAINER_multihashmap_remove (mesh_map,
393 &sh->target.hashPubKey,
395 GNUNET_CONTAINER_multihashmap_destroy (sh->waiting_map);
401 * Transmit pending requests via the mesh.
403 * @param sh mesh to process
406 transmit_pending (struct StreamHandle *sh);
410 * Iterator called on each entry in a waiting map to
411 * move it back to the pending list.
413 * @param cls the 'struct StreamHandle'
414 * @param key the key of the entry in the map (the query)
415 * @param value the 'struct GSF_StreamRequest' to move to pending
416 * @return GNUNET_YES (continue to iterate)
419 move_to_pending (void *cls,
420 const struct GNUNET_HashCode *key,
423 struct StreamHandle *sh = cls;
424 struct GSF_StreamRequest *sr = value;
426 GNUNET_assert (GNUNET_YES ==
427 GNUNET_CONTAINER_multihashmap_remove (sh->waiting_map,
430 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
433 sr->was_transmitted = GNUNET_NO;
439 * We had a serious error, tear down and re-create mesh from scratch.
441 * @param sh mesh to reset
444 reset_mesh (struct StreamHandle *sh)
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Resetting mesh to %s\n",
448 GNUNET_i2s (&sh->target));
449 GNUNET_MESH_tunnel_destroy (sh->mesh);
450 sh->is_ready = GNUNET_NO;
451 GNUNET_CONTAINER_multihashmap_iterate (sh->waiting_map,
454 sh->mesh = GNUNET_MESH_tunnel_create (listen_socket,
457 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
464 * Task called when it is time to destroy an inactive mesh.
466 * @param cls the 'struct StreamHandle' to tear down
467 * @param tc scheduler context, unused
470 mesh_timeout (void *cls,
471 const struct GNUNET_SCHEDULER_TaskContext *tc)
473 struct StreamHandle *sh = cls;
475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
476 "Timeout on mesh to %s\n",
477 GNUNET_i2s (&sh->target));
478 sh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
479 destroy_mesh_handle (sh);
484 * Task called when it is time to reset an mesh.
486 * @param cls the 'struct StreamHandle' to tear down
487 * @param tc scheduler context, unused
490 reset_mesh_task (void *cls,
491 const struct GNUNET_SCHEDULER_TaskContext *tc)
493 struct StreamHandle *sh = cls;
495 sh->reset_task = GNUNET_SCHEDULER_NO_TASK;
501 * We had a serious error, tear down and re-create mesh from scratch,
502 * but do so asynchronously.
504 * @param sh mesh to reset
507 reset_mesh_async (struct StreamHandle *sh)
509 if (GNUNET_SCHEDULER_NO_TASK != sh->reset_task)
510 GNUNET_SCHEDULER_cancel (sh->reset_task);
511 sh->reset_task = GNUNET_SCHEDULER_add_now (&reset_mesh_task,
517 * Functions of this signature are called whenever we are ready to transmit
520 * @param cls the struct StreamHandle for which we did the write call
521 * @param size the number of bytes that can be written to 'buf'
522 * @param buf where to write the message
523 * @return number of bytes written to 'buf'
526 transmit_sqm (void *cls,
530 struct StreamHandle *sh = cls;
531 struct StreamQueryMessage sqm;
532 struct GSF_StreamRequest *sr;
540 sr = sh->pending_head;
543 GNUNET_assert (size >= sizeof (struct StreamQueryMessage));
544 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
547 GNUNET_CONTAINER_multihashmap_put (sh->waiting_map,
550 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "Sending query via mesh to %s\n",
553 GNUNET_i2s (&sh->target));
554 sr->was_transmitted = GNUNET_YES;
555 sqm.header.size = htons (sizeof (sqm));
556 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_FS_STREAM_QUERY);
557 sqm.type = htonl (sr->type);
558 sqm.query = sr->query;
559 memcpy (buf, &sqm, sizeof (sqm));
560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
561 "Successfully transmitted %u bytes via mesh to %s\n",
563 GNUNET_i2s (&sh->target));
564 transmit_pending (sh);
570 * Transmit pending requests via the mesh.
572 * @param sh mesh to process
575 transmit_pending (struct StreamHandle *sh)
579 sh->wh = GNUNET_MESH_notify_transmit_ready (sh->mesh, GNUNET_YES /* allow cork */,
580 GNUNET_TIME_UNIT_FOREVER_REL,
581 sizeof (struct StreamQueryMessage),
587 * Closure for 'handle_reply'.
589 struct HandleReplyClosure
598 * Expiration time for the block.
600 struct GNUNET_TIME_Absolute expiration;
603 * Number of bytes in 'data'.
610 enum GNUNET_BLOCK_Type type;
613 * Did we have a matching query?
620 * Iterator called on each entry in a waiting map to
623 * @param cls the 'struct HandleReplyClosure'
624 * @param key the key of the entry in the map (the query)
625 * @param value the 'struct GSF_StreamRequest' to handle result for
626 * @return GNUNET_YES (continue to iterate)
629 handle_reply (void *cls,
630 const struct GNUNET_HashCode *key,
633 struct HandleReplyClosure *hrc = cls;
634 struct GSF_StreamRequest *sr = value;
636 sr->proc (sr->proc_cls,
641 GSF_mesh_query_cancel (sr);
642 hrc->found = GNUNET_YES;
648 * Functions with this signature are called whenever a
649 * complete reply is received.
651 * @param cls closure with the 'struct StreamHandle'
652 * @param tunnel tunnel handle
653 * @param tunnel_ctx tunnel context
654 * @param message the actual message
655 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
659 struct GNUNET_MESH_Tunnel *tunnel,
661 const struct GNUNET_MessageHeader *message)
663 struct StreamHandle *sh = *tunnel_ctx;
664 const struct StreamReplyMessage *srm;
665 struct HandleReplyClosure hrc;
667 enum GNUNET_BLOCK_Type type;
668 struct GNUNET_HashCode query;
670 msize = ntohs (message->size);
671 if (sizeof (struct StreamReplyMessage) > msize)
674 reset_mesh_async (sh);
675 return GNUNET_SYSERR;
677 srm = (const struct StreamReplyMessage *) message;
678 msize -= sizeof (struct StreamReplyMessage);
679 type = (enum GNUNET_BLOCK_Type) ntohl (srm->type);
681 GNUNET_BLOCK_get_key (GSF_block_ctx,
683 &srm[1], msize, &query))
686 reset_mesh_async (sh);
687 return GNUNET_SYSERR;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Received reply `%s' via mesh\n",
691 GNUNET_h2s (&query));
692 GNUNET_STATISTICS_update (GSF_stats,
693 gettext_noop ("# replies received via mesh"), 1,
696 hrc.data_size = msize;
697 hrc.expiration = GNUNET_TIME_absolute_ntoh (srm->expiration);
699 hrc.found = GNUNET_NO;
700 GNUNET_CONTAINER_multihashmap_get_multiple (sh->waiting_map,
704 if (GNUNET_NO == hrc.found)
706 GNUNET_STATISTICS_update (GSF_stats,
707 gettext_noop ("# replies received via mesh dropped"), 1,
716 * Get (or create) a mesh to talk to the given peer.
718 * @param target peer we want to communicate with
720 static struct StreamHandle *
721 get_mesh (const struct GNUNET_PeerIdentity *target)
723 struct StreamHandle *sh;
725 sh = GNUNET_CONTAINER_multihashmap_get (mesh_map,
726 &target->hashPubKey);
729 if (GNUNET_SCHEDULER_NO_TASK != sh->timeout_task)
731 GNUNET_SCHEDULER_cancel (sh->timeout_task);
732 sh->timeout_task = GNUNET_SCHEDULER_NO_TASK;
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "Creating mesh to %s\n",
738 GNUNET_i2s (target));
739 sh = GNUNET_malloc (sizeof (struct StreamHandle));
740 sh->reset_task = GNUNET_SCHEDULER_add_delayed (CLIENT_RETRY_TIMEOUT,
743 sh->waiting_map = GNUNET_CONTAINER_multihashmap_create (512, GNUNET_YES);
744 sh->target = *target;
745 sh->mesh = GNUNET_MESH_tunnel_create (listen_socket,
748 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
751 GNUNET_assert (GNUNET_OK ==
752 GNUNET_CONTAINER_multihashmap_put (mesh_map,
753 &sh->target.hashPubKey,
755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
761 * Look for a block by directly contacting a particular peer.
763 * @param target peer that should have the block
764 * @param query hash to query for the block
765 * @param type desired type for the block
766 * @param proc function to call with result
767 * @param proc_cls closure for 'proc'
768 * @return handle to cancel the operation
770 struct GSF_StreamRequest *
771 GSF_mesh_query (const struct GNUNET_PeerIdentity *target,
772 const struct GNUNET_HashCode *query,
773 enum GNUNET_BLOCK_Type type,
774 GSF_StreamReplyProcessor proc, void *proc_cls)
776 struct StreamHandle *sh;
777 struct GSF_StreamRequest *sr;
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Preparing to send query for %s via mesh to %s\n",
782 GNUNET_i2s (target));
783 sh = get_mesh (target);
784 sr = GNUNET_malloc (sizeof (struct GSF_StreamRequest));
787 sr->proc_cls = proc_cls;
790 GNUNET_CONTAINER_DLL_insert (sh->pending_head,
793 if (GNUNET_YES == sh->is_ready)
794 transmit_pending (sh);
800 * Cancel an active request; must not be called after 'proc'
803 * @param sr request to cancel
806 GSF_mesh_query_cancel (struct GSF_StreamRequest *sr)
808 struct StreamHandle *sh = sr->sh;
810 if (GNUNET_YES == sr->was_transmitted)
811 GNUNET_assert (GNUNET_OK ==
812 GNUNET_CONTAINER_multihashmap_remove (sh->waiting_map,
816 GNUNET_CONTAINER_DLL_remove (sh->pending_head,
820 if ( (0 == GNUNET_CONTAINER_multihashmap_size (sh->waiting_map)) &&
821 (NULL == sh->pending_head) )
822 sh->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
828 /* ********************* server-side code ************************* */
832 * We're done with a particular client, clean up.
834 * @param sc client to clean up
837 terminate_mesh (struct StreamClient *sc)
839 GNUNET_STATISTICS_update (GSF_stats,
840 gettext_noop ("# mesh connections active"), -1,
842 if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
843 GNUNET_SCHEDULER_cancel (sc->terminate_task);
844 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
845 GNUNET_SCHEDULER_cancel (sc->timeout_task);
847 GNUNET_MESH_notify_transmit_ready_cancel (sc->wh);
849 GNUNET_DATASTORE_cancel (sc->qe);
850 GNUNET_MESH_tunnel_destroy (sc->socket);
851 struct WriteQueueItem *wqi;
852 while (NULL != (wqi = sc->wqi_head))
854 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
859 GNUNET_CONTAINER_DLL_remove (sc_head,
868 * Task run to asynchronously terminate the mesh due to timeout.
870 * @param cls the 'struct StreamClient'
871 * @param tc scheduler context
874 timeout_mesh_task (void *cls,
875 const struct GNUNET_SCHEDULER_TaskContext *tc)
877 struct StreamClient *sc = cls;
879 sc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
885 * Reset the timeout for the mesh client (due to activity).
887 * @param sc client handle to reset timeout for
890 refresh_timeout_task (struct StreamClient *sc)
892 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
893 GNUNET_SCHEDULER_cancel (sc->timeout_task);
894 sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
901 * We're done handling a request from a client, read the next one.
903 * @param sc client to continue reading requests from
906 continue_reading (struct StreamClient *sc)
908 refresh_timeout_task (sc);
913 * Transmit the next entry from the write queue.
915 * @param sc where to process the write queue
918 continue_writing (struct StreamClient *sc);
922 * Send a reply now, mesh is ready.
924 * @param cls closure with the struct StreamClient which sent the query
925 * @param size number of bytes available in 'buf'
926 * @param buf where to write the message
927 * @return number of bytes written to 'buf'
930 write_continuation (void *cls,
934 struct StreamClient *sc = cls;
935 struct WriteQueueItem *wqi;
939 if (NULL == (wqi = sc->wqi_head))
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Write queue empty, reading more requests\n");
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Transmission of reply failed, terminating mesh\n");
952 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Transmitted %u byte reply via mesh\n",
957 (unsigned int) size);
958 GNUNET_STATISTICS_update (GSF_stats,
959 gettext_noop ("# Blocks transferred via mesh"), 1,
961 memcpy (buf, &wqi[1], ret = wqi->msize);
963 continue_writing (sc);
969 * Transmit the next entry from the write queue.
971 * @param sc where to process the write queue
974 continue_writing (struct StreamClient *sc)
976 struct WriteQueueItem *wqi;
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
981 "Write pending, waiting for it to complete\n");
982 return; /* write already pending */
984 if (NULL == (wqi = sc->wqi_head))
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987 "Write queue empty, reading more requests\n");
988 continue_reading (sc);
991 sc->wh = GNUNET_MESH_notify_transmit_ready (sc->socket, GNUNET_NO,
992 GNUNET_TIME_UNIT_FOREVER_REL,
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Write failed; terminating mesh\n");
1000 terminate_mesh (sc);
1007 * Process a datum that was stored in the datastore.
1009 * @param cls closure with the struct StreamClient which sent the query
1010 * @param key key for the content
1011 * @param size number of bytes in data
1012 * @param data content stored
1013 * @param type type of the content
1014 * @param priority priority of the content
1015 * @param anonymity anonymity-level for the content
1016 * @param expiration expiration time for the content
1017 * @param uid unique identifier for the datum;
1018 * maybe 0 if no unique identifier is available
1021 handle_datastore_reply (void *cls,
1022 const struct GNUNET_HashCode * key,
1023 size_t size, const void *data,
1024 enum GNUNET_BLOCK_Type type,
1027 struct GNUNET_TIME_Absolute
1028 expiration, uint64_t uid)
1030 struct StreamClient *sc = cls;
1031 size_t msize = size + sizeof (struct StreamReplyMessage);
1032 struct WriteQueueItem *wqi;
1033 struct StreamReplyMessage *srm;
1036 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Performing on-demand encoding\n");
1041 GNUNET_FS_handle_on_demand_block (key,
1043 priority, anonymity,
1045 &handle_datastore_reply,
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "On-demand encoding request failed\n");
1050 continue_writing (sc);
1054 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
1057 continue_writing (sc);
1060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1061 "Starting transmission of %u byte reply for query `%s' via mesh\n",
1062 (unsigned int) size,
1064 wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
1066 srm = (struct StreamReplyMessage *) &wqi[1];
1067 srm->header.size = htons ((uint16_t) msize);
1068 srm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_STREAM_REPLY);
1069 srm->type = htonl (type);
1070 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
1071 memcpy (&srm[1], data, size);
1072 sc->reply_size = msize;
1073 GNUNET_CONTAINER_DLL_insert (sc->wqi_head,
1076 continue_writing (sc);
1081 * Functions with this signature are called whenever a
1082 * complete query message is received.
1084 * Do not call GNUNET_SERVER_mst_destroy in callback
1086 * @param cls closure with the 'struct StreamClient'
1087 * @param tunnel tunnel handle
1088 * @param tunnel_ctx tunnel context
1089 * @param message the actual message
1090 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1093 request_cb (void *cls,
1094 struct GNUNET_MESH_Tunnel *tunnel,
1096 const struct GNUNET_MessageHeader *message)
1098 struct StreamClient *sc = *tunnel_ctx;
1099 const struct StreamQueryMessage *sqm;
1101 sqm = (const struct StreamQueryMessage *) message;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1103 "Received query for `%s' via mesh\n",
1104 GNUNET_h2s (&sqm->query));
1105 GNUNET_STATISTICS_update (GSF_stats,
1106 gettext_noop ("# queries received via mesh"), 1,
1108 refresh_timeout_task (sc);
1109 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
1114 GSF_datastore_queue_size,
1115 GNUNET_TIME_UNIT_FOREVER_REL,
1116 &handle_datastore_reply, sc);
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Queueing request with datastore failed (queue full?)\n");
1121 continue_writing (sc);
1128 * Functions of this type are called upon new mesh connection from other peers.
1130 * @param cls the closure from GNUNET_MESH_connect
1131 * @param socket the socket representing the mesh
1132 * @param initiator the identity of the peer who wants to establish a mesh
1133 * with us; NULL on binding error
1134 * @param port mesh port used for the incoming connection
1135 * @return initial tunnel context (our 'struct StreamClient')
1138 accept_cb (void *cls,
1139 struct GNUNET_MESH_Tunnel *socket,
1140 const struct GNUNET_PeerIdentity *initiator,
1143 struct StreamClient *sc;
1145 GNUNET_assert (NULL != socket);
1146 if (sc_count >= sc_count_max)
1148 GNUNET_STATISTICS_update (GSF_stats,
1149 gettext_noop ("# mesh client connections rejected"), 1,
1151 GNUNET_MESH_tunnel_destroy (socket);
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Accepting inbound mesh connection from `%s'\n",
1156 GNUNET_i2s (initiator));
1157 GNUNET_STATISTICS_update (GSF_stats,
1158 gettext_noop ("# mesh connections active"), 1,
1160 sc = GNUNET_malloc (sizeof (struct StreamClient));
1161 sc->socket = socket;
1162 GNUNET_CONTAINER_DLL_insert (sc_head,
1166 refresh_timeout_task (sc);
1172 * Initialize subsystem for non-anonymous file-sharing.
1177 static const struct GNUNET_MESH_MessageHandler handlers[] = {
1178 { &request_cb, GNUNET_MESSAGE_TYPE_FS_STREAM_QUERY, sizeof (struct StreamQueryMessage)},
1179 { &reply_cb, GNUNET_MESSAGE_TYPE_FS_STREAM_REPLY, 0 },
1182 static const uint32_t ports[] = {
1183 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
1187 mesh_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
1189 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
1191 "MAX_STREAM_CLIENTS",
1194 listen_socket = GNUNET_MESH_connect (GSF_cfg,
1197 NULL /* FIXME: have a cleanup callback? */,
1205 * Function called on each active meshs to shut them down.
1208 * @param key target peer, unused
1209 * @param value the 'struct StreamHandle' to destroy
1210 * @return GNUNET_YES (continue to iterate)
1213 release_meshs (void *cls,
1214 const struct GNUNET_HashCode *key,
1217 struct StreamHandle *sh = value;
1219 destroy_mesh_handle (sh);
1225 * Shutdown subsystem for non-anonymous file-sharing.
1230 struct StreamClient *sc;
1232 while (NULL != (sc = sc_head))
1233 terminate_mesh (sc);
1234 if (NULL != listen_socket)
1236 GNUNET_MESH_disconnect (listen_socket);
1237 listen_socket = NULL;
1239 GNUNET_CONTAINER_multihashmap_iterate (mesh_map,
1242 GNUNET_CONTAINER_multihashmap_destroy (mesh_map);
1246 /* end of gnunet-service-fs_mesh.c */