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_server.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 * Channel for communication.
86 struct GNUNET_MESH_Channel *channel;
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 channel for incoming requests.
129 static struct GNUNET_MESH_Handle *listen_channel;
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_Channel *tun;
166 sc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Timeout for inactive mesh client %p\n",
172 GNUNET_MESH_channel_destroy (tun);
177 * Reset the timeout for the mesh client (due to activity).
179 * @param sc client handle to reset timeout for
182 refresh_timeout_task (struct MeshClient *sc)
184 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
185 GNUNET_SCHEDULER_cancel (sc->timeout_task);
186 sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
193 * We're done handling a request from a client, read the next one.
195 * @param sc client to continue reading requests from
198 continue_reading (struct MeshClient *sc)
200 refresh_timeout_task (sc);
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Finished processing mesh request from client %p, ready to receive the next one\n",
204 GNUNET_MESH_receive_done (sc->channel);
209 * Transmit the next entry from the write queue.
211 * @param sc where to process the write queue
214 continue_writing (struct MeshClient *sc);
218 * Send a reply now, mesh is ready.
220 * @param cls closure with the `struct MeshClient` which sent the query
221 * @param size number of bytes available in @a buf
222 * @param buf where to write the message
223 * @return number of bytes written to @a buf
226 write_continuation (void *cls,
230 struct MeshClient *sc = cls;
231 struct GNUNET_MESH_Channel *tun;
232 struct WriteQueueItem *wqi;
236 if (NULL == (wqi = sc->wqi_head))
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
239 "Write queue empty, reading more requests\n");
243 (size < wqi->msize) )
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Transmission of reply failed, terminating mesh\n");
249 GNUNET_MESH_channel_destroy (tun);
252 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Transmitted %u byte reply via mesh to %p\n",
259 GNUNET_STATISTICS_update (GSF_stats,
260 gettext_noop ("# Blocks transferred via mesh"), 1,
262 memcpy (buf, &wqi[1], ret = wqi->msize);
264 continue_writing (sc);
270 * Transmit the next entry from the write queue.
272 * @param sc where to process the write queue
275 continue_writing (struct MeshClient *sc)
277 struct WriteQueueItem *wqi;
278 struct GNUNET_MESH_Channel *tun;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Write pending, waiting for it to complete\n");
284 return; /* write already pending */
286 if (NULL == (wqi = sc->wqi_head))
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Write queue empty, reading more requests\n");
290 continue_reading (sc);
293 sc->wh = GNUNET_MESH_notify_transmit_ready (sc->channel, GNUNET_NO,
294 GNUNET_TIME_UNIT_FOREVER_REL,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Write failed; terminating mesh\n");
304 GNUNET_MESH_channel_destroy (tun);
311 * Process a datum that was stored in the datastore.
313 * @param cls closure with the `struct MeshClient` which sent the query
314 * @param key key for the content
315 * @param size number of bytes in @a data
316 * @param data content stored
317 * @param type type of the content
318 * @param priority priority of the content
319 * @param anonymity anonymity-level for the content
320 * @param expiration expiration time for the content
321 * @param uid unique identifier for the datum;
322 * maybe 0 if no unique identifier is available
325 handle_datastore_reply (void *cls,
326 const struct GNUNET_HashCode *key,
327 size_t size, const void *data,
328 enum GNUNET_BLOCK_Type type,
331 struct GNUNET_TIME_Absolute expiration,
334 struct MeshClient *sc = cls;
335 size_t msize = size + sizeof (struct MeshReplyMessage);
336 struct WriteQueueItem *wqi;
337 struct MeshReplyMessage *srm;
342 /* no result, this should not really happen, as for
343 non-anonymous routing only peers that HAVE the
344 answers should be queried; OTOH, this is not a
345 hard error as we might have had the answer in the
346 past and the user might have unindexed it. Hence
347 we log at level "INFO" for now. */
348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
349 "Have no answer for query `%s'\n",
351 GNUNET_STATISTICS_update (GSF_stats,
352 gettext_noop ("# queries received via mesh not answered"), 1,
354 continue_writing (sc);
357 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
360 "Performing on-demand encoding for query %s\n",
363 GNUNET_FS_handle_on_demand_block (key,
367 &handle_datastore_reply,
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "On-demand encoding request failed\n");
372 continue_writing (sc);
376 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
379 continue_writing (sc);
382 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Starting transmission of %u byte reply of type %d for query `%s' via mesh to %p\n",
389 wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
391 srm = (struct MeshReplyMessage *) &wqi[1];
392 srm->header.size = htons ((uint16_t) msize);
393 srm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_MESH_REPLY);
394 srm->type = htonl (type);
395 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
396 memcpy (&srm[1], data, size);
397 sc->reply_size = msize;
398 GNUNET_CONTAINER_DLL_insert (sc->wqi_head,
401 continue_writing (sc);
406 * Functions with this signature are called whenever a
407 * complete query message is received.
409 * Do not call #GNUNET_SERVER_mst_destroy in callback
411 * @param cls closure with the 'struct MeshClient'
412 * @param channel channel handle
413 * @param channel_ctx channel context
414 * @param message the actual message
415 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
418 request_cb (void *cls,
419 struct GNUNET_MESH_Channel *channel,
421 const struct GNUNET_MessageHeader *message)
423 struct MeshClient *sc = *channel_ctx;
424 const struct MeshQueryMessage *sqm;
426 sqm = (const struct MeshQueryMessage *) message;
427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
428 "Received query for `%s' via mesh from client %p\n",
429 GNUNET_h2s (&sqm->query),
431 GNUNET_STATISTICS_update (GSF_stats,
432 gettext_noop ("# queries received via mesh"), 1,
434 refresh_timeout_task (sc);
435 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
440 GSF_datastore_queue_size,
441 GNUNET_TIME_UNIT_FOREVER_REL,
442 &handle_datastore_reply, sc);
445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
446 "Queueing request with datastore failed (queue full?)\n");
447 continue_writing (sc);
454 * Functions of this type are called upon new mesh connection from other peers.
456 * @param cls the closure from GNUNET_MESH_connect
457 * @param channel the channel representing the mesh
458 * @param initiator the identity of the peer who wants to establish a mesh
459 * with us; NULL on binding error
460 * @param port mesh port used for the incoming connection
461 * @param options channel option flags
462 * @return initial channel context (our 'struct MeshClient')
465 accept_cb (void *cls,
466 struct GNUNET_MESH_Channel *channel,
467 const struct GNUNET_PeerIdentity *initiator,
468 uint32_t port, enum MeshOption options)
470 struct MeshClient *sc;
472 GNUNET_assert (NULL != channel);
473 if (sc_count >= sc_count_max)
475 GNUNET_STATISTICS_update (GSF_stats,
476 gettext_noop ("# mesh client connections rejected"), 1,
478 GNUNET_MESH_channel_destroy (channel);
481 GNUNET_STATISTICS_update (GSF_stats,
482 gettext_noop ("# mesh connections active"), 1,
484 sc = GNUNET_new (struct MeshClient);
485 sc->channel = channel;
486 GNUNET_CONTAINER_DLL_insert (sc_head,
490 refresh_timeout_task (sc);
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Accepting inbound mesh connection from `%s' as client %p\n",
493 GNUNET_i2s (initiator),
500 * Function called by mesh when a client disconnects.
501 * Cleans up our 'struct MeshClient' of that channel.
504 * @param channel channel of the disconnecting client
505 * @param channel_ctx our 'struct MeshClient'
508 cleaner_cb (void *cls,
509 const struct GNUNET_MESH_Channel *channel,
512 struct MeshClient *sc = channel_ctx;
513 struct WriteQueueItem *wqi;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Terminating mesh connection with client %p\n",
521 GNUNET_STATISTICS_update (GSF_stats,
522 gettext_noop ("# mesh connections active"), -1,
524 if (GNUNET_SCHEDULER_NO_TASK != sc->terminate_task)
525 GNUNET_SCHEDULER_cancel (sc->terminate_task);
526 if (GNUNET_SCHEDULER_NO_TASK != sc->timeout_task)
527 GNUNET_SCHEDULER_cancel (sc->timeout_task);
529 GNUNET_MESH_notify_transmit_ready_cancel (sc->wh);
531 GNUNET_DATASTORE_cancel (sc->qe);
532 while (NULL != (wqi = sc->wqi_head))
534 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
539 GNUNET_CONTAINER_DLL_remove (sc_head,
548 * Initialize subsystem for non-anonymous file-sharing.
551 GSF_mesh_start_server ()
553 static const struct GNUNET_MESH_MessageHandler handlers[] = {
554 { &request_cb, GNUNET_MESSAGE_TYPE_FS_MESH_QUERY, sizeof (struct MeshQueryMessage)},
557 static const uint32_t ports[] = {
558 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
563 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Initializing mesh FS server with a limit of %llu connections\n",
571 listen_channel = GNUNET_MESH_connect (GSF_cfg,
581 * Shutdown subsystem for non-anonymous file-sharing.
584 GSF_mesh_stop_server ()
586 if (NULL != listen_channel)
588 GNUNET_MESH_disconnect (listen_channel);
589 listen_channel = NULL;
591 GNUNET_assert (NULL == sc_head);
592 GNUNET_assert (0 == sc_count);
595 /* end of gnunet-service-fs_mesh.c */