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 * - 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"
41 * After how long do we termiante idle connections?
43 #define IDLE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
47 * A message in the queue to be written to the mesh.
54 struct WriteQueueItem *next;
59 struct WriteQueueItem *prev;
62 * Number of bytes of payload, allocated at the end of this struct.
69 * Information we keep around for each active meshing client.
76 struct MeshClient *next;
81 struct MeshClient *prev;
84 * Tunnel for communication.
86 struct GNUNET_MESH_Tunnel *tunnel;
89 * Handle for active write operation, or NULL.
91 struct GNUNET_MESH_TransmitHandle *wh;
94 * Head of write queue.
96 struct WriteQueueItem *wqi_head;
99 * Tail of write queue.
101 struct WriteQueueItem *wqi_tail;
104 * Current active request to the datastore, if we have one pending.
106 struct GNUNET_DATASTORE_QueueEntry *qe;
109 * Task that is scheduled to asynchronously terminate the connection.
111 GNUNET_SCHEDULER_TaskIdentifier terminate_task;
114 * Task that is scheduled to terminate idle connections.
116 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
119 * Size of the last write that was initiated.
127 * Listen tunnel for incoming requests.
129 static struct GNUNET_MESH_Handle *listen_tunnel;
132 * Head of DLL of mesh clients.
134 static struct MeshClient *sc_head;
137 * Tail of DLL of mesh clients.
139 static struct MeshClient *sc_tail;
142 * Number of active mesh clients in the 'sc_*'-DLL.
144 static unsigned int sc_count;
147 * Maximum allowed number of mesh clients.
149 static unsigned long long sc_count_max;
154 * Task run to asynchronously terminate the mesh due to timeout.
156 * @param cls the 'struct MeshClient'
157 * @param tc scheduler context
160 timeout_mesh_task (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 struct MeshClient *sc = cls;
164 struct GNUNET_MESH_Tunnel *tun;
166 sc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
169 GNUNET_MESH_tunnel_destroy (tun);
174 * Reset the timeout for the mesh client (due to activity).
176 * @param sc client handle to reset timeout for
179 refresh_timeout_task (struct MeshClient *sc)
181 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
182 GNUNET_SCHEDULER_cancel (sc->timeout_task);
183 sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
190 * We're done handling a request from a client, read the next one.
192 * @param sc client to continue reading requests from
195 continue_reading (struct MeshClient *sc)
197 refresh_timeout_task (sc);
198 GNUNET_MESH_receive_done (sc->tunnel);
203 * Transmit the next entry from the write queue.
205 * @param sc where to process the write queue
208 continue_writing (struct MeshClient *sc);
212 * Send a reply now, mesh is ready.
214 * @param cls closure with the struct MeshClient which sent the query
215 * @param size number of bytes available in 'buf'
216 * @param buf where to write the message
217 * @return number of bytes written to 'buf'
220 write_continuation (void *cls,
224 struct MeshClient *sc = cls;
225 struct GNUNET_MESH_Tunnel *tun;
226 struct WriteQueueItem *wqi;
230 if (NULL == (wqi = sc->wqi_head))
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Write queue empty, reading more requests\n");
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Transmission of reply failed, terminating mesh\n");
242 GNUNET_MESH_tunnel_destroy (tun);
245 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Transmitted %u byte reply via mesh\n",
250 (unsigned int) size);
251 GNUNET_STATISTICS_update (GSF_stats,
252 gettext_noop ("# Blocks transferred via mesh"), 1,
254 memcpy (buf, &wqi[1], ret = wqi->msize);
256 continue_writing (sc);
262 * Transmit the next entry from the write queue.
264 * @param sc where to process the write queue
267 continue_writing (struct MeshClient *sc)
269 struct WriteQueueItem *wqi;
270 struct GNUNET_MESH_Tunnel *tun;
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Write pending, waiting for it to complete\n");
276 return; /* write already pending */
278 if (NULL == (wqi = sc->wqi_head))
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Write queue empty, reading more requests\n");
282 continue_reading (sc);
285 sc->wh = GNUNET_MESH_notify_transmit_ready (sc->tunnel, GNUNET_NO,
286 GNUNET_TIME_UNIT_FOREVER_REL,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Write failed; terminating mesh\n");
296 GNUNET_MESH_tunnel_destroy (tun);
303 * Process a datum that was stored in the datastore.
305 * @param cls closure with the struct MeshClient which sent the query
306 * @param key key for the content
307 * @param size number of bytes in data
308 * @param data content stored
309 * @param type type of the content
310 * @param priority priority of the content
311 * @param anonymity anonymity-level for the content
312 * @param expiration expiration time for the content
313 * @param uid unique identifier for the datum;
314 * maybe 0 if no unique identifier is available
317 handle_datastore_reply (void *cls,
318 const struct GNUNET_HashCode *key,
319 size_t size, const void *data,
320 enum GNUNET_BLOCK_Type type,
323 struct GNUNET_TIME_Absolute
324 expiration, uint64_t uid)
326 struct MeshClient *sc = cls;
327 size_t msize = size + sizeof (struct MeshReplyMessage);
328 struct WriteQueueItem *wqi;
329 struct MeshReplyMessage *srm;
332 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Performing on-demand encoding for query %s\n",
338 GNUNET_FS_handle_on_demand_block (key,
342 &handle_datastore_reply,
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "On-demand encoding request failed\n");
347 continue_writing (sc);
351 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
354 continue_writing (sc);
357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
358 "Starting transmission of %u byte reply for query `%s' via mesh\n",
361 wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
363 srm = (struct MeshReplyMessage *) &wqi[1];
364 srm->header.size = htons ((uint16_t) msize);
365 srm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MESH_REPLY);
366 srm->type = htonl (type);
367 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
368 memcpy (&srm[1], data, size);
369 sc->reply_size = msize;
370 GNUNET_CONTAINER_DLL_insert (sc->wqi_head,
373 continue_writing (sc);
378 * Functions with this signature are called whenever a
379 * complete query message is received.
381 * Do not call GNUNET_SERVER_mst_destroy in callback
383 * @param cls closure with the 'struct MeshClient'
384 * @param tunnel tunnel handle
385 * @param tunnel_ctx tunnel context
386 * @param message the actual message
387 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
390 request_cb (void *cls,
391 struct GNUNET_MESH_Tunnel *tunnel,
393 const struct GNUNET_MessageHeader *message)
395 struct MeshClient *sc = *tunnel_ctx;
396 const struct MeshQueryMessage *sqm;
398 sqm = (const struct MeshQueryMessage *) message;
399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
400 "Received query for `%s' via mesh from client %p\n",
401 GNUNET_h2s (&sqm->query),
403 GNUNET_STATISTICS_update (GSF_stats,
404 gettext_noop ("# queries received via mesh"), 1,
406 refresh_timeout_task (sc);
407 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
412 GSF_datastore_queue_size,
413 GNUNET_TIME_UNIT_FOREVER_REL,
414 &handle_datastore_reply, sc);
417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
418 "Queueing request with datastore failed (queue full?)\n");
419 continue_writing (sc);
426 * Functions of this type are called upon new mesh connection from other peers.
428 * @param cls the closure from GNUNET_MESH_connect
429 * @param tunnel the tunnel representing the mesh
430 * @param initiator the identity of the peer who wants to establish a mesh
431 * with us; NULL on binding error
432 * @param port mesh port used for the incoming connection
433 * @return initial tunnel context (our 'struct MeshClient')
436 accept_cb (void *cls,
437 struct GNUNET_MESH_Tunnel *tunnel,
438 const struct GNUNET_PeerIdentity *initiator,
441 struct MeshClient *sc;
443 GNUNET_assert (NULL != tunnel);
444 if (sc_count >= sc_count_max)
446 GNUNET_STATISTICS_update (GSF_stats,
447 gettext_noop ("# mesh client connections rejected"), 1,
449 GNUNET_MESH_tunnel_destroy (tunnel);
452 GNUNET_STATISTICS_update (GSF_stats,
453 gettext_noop ("# mesh connections active"), 1,
455 sc = GNUNET_new (struct MeshClient);
457 GNUNET_CONTAINER_DLL_insert (sc_head,
461 refresh_timeout_task (sc);
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "Accepting inbound mesh connection from `%s' as client %p\n",
464 GNUNET_i2s (initiator),
471 * Function called by mesh when a client disconnects.
472 * Cleans up our 'struct MeshClient' of that tunnel.
475 * @param tunnel tunnel of the disconnecting client
476 * @param tunnel_ctx our 'struct MeshClient'
479 cleaner_cb (void *cls,
480 const struct GNUNET_MESH_Tunnel *tunnel,
483 struct MeshClient *sc = tunnel_ctx;
484 struct WriteQueueItem *wqi;
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Terminating mesh connection with client %p\n",
492 GNUNET_STATISTICS_update (GSF_stats,
493 gettext_noop ("# mesh connections active"), -1,
495 if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
496 GNUNET_SCHEDULER_cancel (sc->terminate_task);
497 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
498 GNUNET_SCHEDULER_cancel (sc->timeout_task);
500 GNUNET_MESH_notify_transmit_ready_cancel (sc->wh);
502 GNUNET_DATASTORE_cancel (sc->qe);
503 while (NULL != (wqi = sc->wqi_head))
505 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
510 GNUNET_CONTAINER_DLL_remove (sc_head,
519 * Initialize subsystem for non-anonymous file-sharing.
522 GSF_mesh_start_server ()
524 static const struct GNUNET_MESH_MessageHandler handlers[] = {
525 { &request_cb, GNUNET_MESSAGE_TYPE_FS_MESH_QUERY, sizeof (struct MeshQueryMessage)},
528 static const uint32_t ports[] = {
529 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
534 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
539 listen_tunnel = GNUNET_MESH_connect (GSF_cfg,
549 * Shutdown subsystem for non-anonymous file-sharing.
552 GSF_mesh_stop_server ()
554 if (NULL != listen_tunnel)
556 GNUNET_MESH_disconnect (listen_tunnel);
557 listen_tunnel = NULL;
559 GNUNET_assert (NULL == sc_head);
560 GNUNET_assert (0 == sc_count);
563 /* end of gnunet-service-fs_mesh.c */