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_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_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"
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 cadet.
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 cadeting client.
76 struct CadetClient *next;
81 struct CadetClient *prev;
84 * Channel for communication.
86 struct GNUNET_CADET_Channel *channel;
89 * Handle for active write operation, or NULL.
91 struct GNUNET_CADET_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 struct GNUNET_SCHEDULER_Task * terminate_task;
114 * Task that is scheduled to terminate idle connections.
116 struct GNUNET_SCHEDULER_Task * timeout_task;
119 * Size of the last write that was initiated.
127 * Listen channel for incoming requests.
129 static struct GNUNET_CADET_Handle *listen_channel;
132 * Head of DLL of cadet clients.
134 static struct CadetClient *sc_head;
137 * Tail of DLL of cadet clients.
139 static struct CadetClient *sc_tail;
142 * Number of active cadet clients in the 'sc_*'-DLL.
144 static unsigned int sc_count;
147 * Maximum allowed number of cadet clients.
149 static unsigned long long sc_count_max;
154 * Task run to asynchronously terminate the cadet due to timeout.
156 * @param cls the 'struct CadetClient'
159 timeout_cadet_task (void *cls)
161 struct CadetClient *sc = cls;
162 struct GNUNET_CADET_Channel *tun;
164 sc->timeout_task = NULL;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Timeout for inactive cadet client %p\n",
170 GNUNET_CADET_channel_destroy (tun);
175 * Reset the timeout for the cadet client (due to activity).
177 * @param sc client handle to reset timeout for
180 refresh_timeout_task (struct CadetClient *sc)
182 if (NULL != sc->timeout_task)
183 GNUNET_SCHEDULER_cancel (sc->timeout_task);
184 sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
191 * We're done handling a request from a client, read the next one.
193 * @param sc client to continue reading requests from
196 continue_reading (struct CadetClient *sc)
198 refresh_timeout_task (sc);
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Finished processing cadet request from client %p, ready to receive the next one\n",
202 GNUNET_CADET_receive_done (sc->channel);
207 * Transmit the next entry from the write queue.
209 * @param sc where to process the write queue
212 continue_writing (struct CadetClient *sc);
216 * Send a reply now, cadet is ready.
218 * @param cls closure with the `struct CadetClient` which sent the query
219 * @param size number of bytes available in @a buf
220 * @param buf where to write the message
221 * @return number of bytes written to @a buf
224 write_continuation (void *cls,
228 struct CadetClient *sc = cls;
229 struct GNUNET_CADET_Channel *tun;
230 struct WriteQueueItem *wqi;
234 if (NULL == (wqi = sc->wqi_head))
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Write queue empty, reading more requests\n");
241 (size < wqi->msize) )
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Transmission of reply failed, terminating cadet\n");
247 GNUNET_CADET_channel_destroy (tun);
250 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Transmitted %u byte reply via cadet to %p\n",
257 GNUNET_STATISTICS_update (GSF_stats,
258 gettext_noop ("# Blocks transferred via cadet"), 1,
260 memcpy (buf, &wqi[1], ret = wqi->msize);
262 continue_writing (sc);
268 * Transmit the next entry from the write queue.
270 * @param sc where to process the write queue
273 continue_writing (struct CadetClient *sc)
275 struct WriteQueueItem *wqi;
276 struct GNUNET_CADET_Channel *tun;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Write pending, waiting for it to complete\n");
282 return; /* write already pending */
284 if (NULL == (wqi = sc->wqi_head))
286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
287 "Write queue empty, reading more requests\n");
288 continue_reading (sc);
291 sc->wh = GNUNET_CADET_notify_transmit_ready (sc->channel, GNUNET_NO,
292 GNUNET_TIME_UNIT_FOREVER_REL,
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Write failed; terminating cadet\n");
302 GNUNET_CADET_channel_destroy (tun);
309 * Process a datum that was stored in the datastore.
311 * @param cls closure with the `struct CadetClient` which sent the query
312 * @param key key for the content
313 * @param size number of bytes in @a data
314 * @param data content stored
315 * @param type type of the content
316 * @param priority priority of the content
317 * @param anonymity anonymity-level for the content
318 * @param expiration expiration time for the content
319 * @param uid unique identifier for the datum;
320 * maybe 0 if no unique identifier is available
323 handle_datastore_reply (void *cls,
324 const struct GNUNET_HashCode *key,
327 enum GNUNET_BLOCK_Type type,
330 struct GNUNET_TIME_Absolute expiration,
333 struct CadetClient *sc = cls;
334 size_t msize = size + sizeof (struct CadetReplyMessage);
335 struct WriteQueueItem *wqi;
336 struct CadetReplyMessage *srm;
341 /* no result, this should not really happen, as for
342 non-anonymous routing only peers that HAVE the
343 answers should be queried; OTOH, this is not a
344 hard error as we might have had the answer in the
345 past and the user might have unindexed it. Hence
346 we log at level "INFO" for now. */
349 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
350 "Have no answer and the query was NULL\n");
354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
355 "Have no answer for query `%s'\n",
358 GNUNET_STATISTICS_update (GSF_stats,
359 gettext_noop ("# queries received via CADET not answered"), 1,
361 continue_writing (sc);
364 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Performing on-demand encoding for query %s\n",
370 GNUNET_FS_handle_on_demand_block (key,
374 &handle_datastore_reply,
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "On-demand encoding request failed\n");
379 continue_writing (sc);
383 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
386 continue_writing (sc);
389 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Starting transmission of %u byte reply of type %d for query `%s' via cadet to %p\n",
396 wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
398 srm = (struct CadetReplyMessage *) &wqi[1];
399 srm->header.size = htons ((uint16_t) msize);
400 srm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_CADET_REPLY);
401 srm->type = htonl (type);
402 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
403 memcpy (&srm[1], data, size);
404 sc->reply_size = msize;
405 GNUNET_CONTAINER_DLL_insert (sc->wqi_head,
408 continue_writing (sc);
413 * Functions with this signature are called whenever a
414 * complete query message is received.
416 * Do not call #GNUNET_SERVER_mst_destroy() in callback
418 * @param cls closure with the `struct CadetClient`
419 * @param channel channel handle
420 * @param channel_ctx channel context
421 * @param message the actual message
422 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
425 request_cb (void *cls,
426 struct GNUNET_CADET_Channel *channel,
428 const struct GNUNET_MessageHeader *message)
430 struct CadetClient *sc = *channel_ctx;
431 const struct CadetQueryMessage *sqm;
433 sqm = (const struct CadetQueryMessage *) message;
434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
435 "Received query for `%s' via cadet from client %p\n",
436 GNUNET_h2s (&sqm->query),
438 GNUNET_STATISTICS_update (GSF_stats,
439 gettext_noop ("# queries received via cadet"), 1,
441 refresh_timeout_task (sc);
442 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
447 GSF_datastore_queue_size,
448 &handle_datastore_reply, sc);
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
452 "Queueing request with datastore failed (queue full?)\n");
453 continue_writing (sc);
460 * Functions of this type are called upon new cadet connection from other peers.
462 * @param cls the closure from GNUNET_CADET_connect
463 * @param channel the channel representing the cadet
464 * @param initiator the identity of the peer who wants to establish a cadet
465 * with us; NULL on binding error
466 * @param port cadet port used for the incoming connection
467 * @param options channel option flags
468 * @return initial channel context (our 'struct CadetClient')
471 accept_cb (void *cls,
472 struct GNUNET_CADET_Channel *channel,
473 const struct GNUNET_PeerIdentity *initiator,
474 uint32_t port, enum GNUNET_CADET_ChannelOption options)
476 struct CadetClient *sc;
478 GNUNET_assert (NULL != channel);
479 if (sc_count >= sc_count_max)
481 GNUNET_STATISTICS_update (GSF_stats,
482 gettext_noop ("# cadet client connections rejected"), 1,
484 GNUNET_CADET_channel_destroy (channel);
487 GNUNET_STATISTICS_update (GSF_stats,
488 gettext_noop ("# cadet connections active"), 1,
490 sc = GNUNET_new (struct CadetClient);
491 sc->channel = channel;
492 GNUNET_CONTAINER_DLL_insert (sc_head,
496 refresh_timeout_task (sc);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Accepting inbound cadet connection from `%s' as client %p\n",
499 GNUNET_i2s (initiator),
506 * Function called by cadet when a client disconnects.
507 * Cleans up our 'struct CadetClient' of that channel.
510 * @param channel channel of the disconnecting client
511 * @param channel_ctx our 'struct CadetClient'
514 cleaner_cb (void *cls,
515 const struct GNUNET_CADET_Channel *channel,
518 struct CadetClient *sc = channel_ctx;
519 struct WriteQueueItem *wqi;
524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
525 "Terminating cadet connection with client %p\n",
527 GNUNET_STATISTICS_update (GSF_stats,
528 gettext_noop ("# cadet connections active"), -1,
530 if (NULL != sc->terminate_task)
531 GNUNET_SCHEDULER_cancel (sc->terminate_task);
532 if (NULL != sc->timeout_task)
533 GNUNET_SCHEDULER_cancel (sc->timeout_task);
535 GNUNET_CADET_notify_transmit_ready_cancel (sc->wh);
537 GNUNET_DATASTORE_cancel (sc->qe);
538 while (NULL != (wqi = sc->wqi_head))
540 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
545 GNUNET_CONTAINER_DLL_remove (sc_head,
554 * Initialize subsystem for non-anonymous file-sharing.
557 GSF_cadet_start_server ()
559 static const struct GNUNET_CADET_MessageHandler handlers[] = {
560 { &request_cb, GNUNET_MESSAGE_TYPE_FS_CADET_QUERY, sizeof (struct CadetQueryMessage)},
563 static const uint32_t ports[] = {
564 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
569 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
575 "Initializing cadet FS server with a limit of %llu connections\n",
577 listen_channel = GNUNET_CADET_connect (GSF_cfg,
587 * Shutdown subsystem for non-anonymous file-sharing.
590 GSF_cadet_stop_server ()
592 if (NULL != listen_channel)
594 GNUNET_CADET_disconnect (listen_channel);
595 listen_channel = NULL;
597 GNUNET_assert (NULL == sc_head);
598 GNUNET_assert (0 == sc_count);
601 /* end of gnunet-service-fs_cadet.c */