2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 * Head of write queue.
91 struct WriteQueueItem *wqi_head;
94 * Tail of write queue.
96 struct WriteQueueItem *wqi_tail;
99 * Current active request to the datastore, if we have one pending.
101 struct GNUNET_DATASTORE_QueueEntry *qe;
104 * Task that is scheduled to asynchronously terminate the connection.
106 struct GNUNET_SCHEDULER_Task * terminate_task;
109 * Task that is scheduled to terminate idle connections.
111 struct GNUNET_SCHEDULER_Task * timeout_task;
114 * Size of the last write that was initiated.
122 * Listen port for incoming requests.
124 static struct GNUNET_CADET_Port *cadet_port;
127 * Head of DLL of cadet clients.
129 static struct CadetClient *sc_head;
132 * Tail of DLL of cadet clients.
134 static struct CadetClient *sc_tail;
137 * Number of active cadet clients in the 'sc_*'-DLL.
139 static unsigned int sc_count;
142 * Maximum allowed number of cadet clients.
144 static unsigned long long sc_count_max;
149 * Task run to asynchronously terminate the cadet due to timeout.
151 * @param cls the 'struct CadetClient'
154 timeout_cadet_task (void *cls)
156 struct CadetClient *sc = cls;
157 struct GNUNET_CADET_Channel *tun;
159 sc->timeout_task = NULL;
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "Timeout for inactive cadet client %p\n",
165 GNUNET_CADET_channel_destroy (tun);
170 * Reset the timeout for the cadet client (due to activity).
172 * @param sc client handle to reset timeout for
175 refresh_timeout_task (struct CadetClient *sc)
177 if (NULL != sc->timeout_task)
178 GNUNET_SCHEDULER_cancel (sc->timeout_task);
179 sc->timeout_task = GNUNET_SCHEDULER_add_delayed (IDLE_TIMEOUT,
186 * Check if we are done with the write queue, and if so tell CADET
187 * that we are ready to read more.
189 * @param cls where to process the write queue
192 continue_writing (void *cls)
194 struct CadetClient *sc = cls;
195 struct GNUNET_MQ_Handle *mq;
197 mq = GNUNET_CADET_get_mq (sc->channel);
198 if (0 != GNUNET_MQ_get_length (mq))
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Write pending, waiting for it to complete\n");
204 refresh_timeout_task (sc);
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Finished processing cadet request from client %p, ready to receive the next one\n",
208 GNUNET_CADET_receive_done (sc->channel);
213 * Process a datum that was stored in the datastore.
215 * @param cls closure with the `struct CadetClient` which sent the query
216 * @param key key for the content
217 * @param size number of bytes in @a data
218 * @param data content stored
219 * @param type type of the content
220 * @param priority priority of the content
221 * @param anonymity anonymity-level for the content
222 * @param replication replication-level for the content
223 * @param expiration expiration time for the content
224 * @param uid unique identifier for the datum;
225 * maybe 0 if no unique identifier is available
228 handle_datastore_reply (void *cls,
229 const struct GNUNET_HashCode *key,
232 enum GNUNET_BLOCK_Type type,
235 uint32_t replication,
236 struct GNUNET_TIME_Absolute expiration,
239 struct CadetClient *sc = cls;
240 size_t msize = size + sizeof (struct CadetReplyMessage);
241 struct GNUNET_MQ_Envelope *env;
242 struct CadetReplyMessage *srm;
247 /* no result, this should not really happen, as for
248 non-anonymous routing only peers that HAVE the
249 answers should be queried; OTOH, this is not a
250 hard error as we might have had the answer in the
251 past and the user might have unindexed it. Hence
252 we log at level "INFO" for now. */
255 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
256 "Have no answer and the query was NULL\n");
260 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
261 "Have no answer for query `%s'\n",
264 GNUNET_STATISTICS_update (GSF_stats,
265 gettext_noop ("# queries received via CADET not answered"),
268 continue_writing (sc);
271 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
274 "Performing on-demand encoding for query %s\n",
277 GNUNET_FS_handle_on_demand_block (key,
286 &handle_datastore_reply,
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "On-demand encoding request failed\n");
291 continue_writing (sc);
295 if (msize > GNUNET_MAX_MESSAGE_SIZE)
298 continue_writing (sc);
301 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Starting transmission of %u byte reply of type %d for query `%s' via cadet to %p\n",
308 env = GNUNET_MQ_msg_extra (srm,
310 GNUNET_MESSAGE_TYPE_FS_CADET_REPLY);
311 srm->type = htonl (type);
312 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
313 GNUNET_memcpy (&srm[1],
316 GNUNET_MQ_notify_sent (env,
319 GNUNET_STATISTICS_update (GSF_stats,
320 gettext_noop ("# Blocks transferred via cadet"),
323 GNUNET_MQ_send (GNUNET_CADET_get_mq (sc->channel),
329 * Functions with this signature are called whenever a
330 * complete query message is received.
332 * @param cls closure with the `struct CadetClient`
333 * @param sqm the actual message
336 handle_request (void *cls,
337 const struct CadetQueryMessage *sqm)
339 struct CadetClient *sc = cls;
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Received query for `%s' via cadet from client %p\n",
343 GNUNET_h2s (&sqm->query),
345 GNUNET_STATISTICS_update (GSF_stats,
346 gettext_noop ("# queries received via cadet"),
349 refresh_timeout_task (sc);
350 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
356 GSF_datastore_queue_size,
357 &handle_datastore_reply,
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Queueing request with datastore failed (queue full?)\n");
363 continue_writing (sc);
369 * Functions of this type are called upon new cadet connection from other peers.
371 * @param cls the closure from GNUNET_CADET_connect
372 * @param channel the channel representing the cadet
373 * @param initiator the identity of the peer who wants to establish a cadet
374 * with us; NULL on binding error
375 * @return initial channel context (our `struct CadetClient`)
378 connect_cb (void *cls,
379 struct GNUNET_CADET_Channel *channel,
380 const struct GNUNET_PeerIdentity *initiator)
382 struct CadetClient *sc;
384 GNUNET_assert (NULL != channel);
385 if (sc_count >= sc_count_max)
387 GNUNET_STATISTICS_update (GSF_stats,
388 gettext_noop ("# cadet client connections rejected"),
391 GNUNET_CADET_channel_destroy (channel);
394 GNUNET_STATISTICS_update (GSF_stats,
395 gettext_noop ("# cadet connections active"),
398 sc = GNUNET_new (struct CadetClient);
399 sc->channel = channel;
400 GNUNET_CONTAINER_DLL_insert (sc_head,
404 refresh_timeout_task (sc);
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Accepting inbound cadet connection from `%s' as client %p\n",
407 GNUNET_i2s (initiator),
414 * Function called by cadet when a client disconnects.
415 * Cleans up our `struct CadetClient` of that channel.
417 * @param cls our `struct CadetClient`
418 * @param channel channel of the disconnecting client
422 disconnect_cb (void *cls,
423 const struct GNUNET_CADET_Channel *channel)
425 struct CadetClient *sc = cls;
426 struct WriteQueueItem *wqi;
431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
432 "Terminating cadet connection with client %p\n",
434 GNUNET_STATISTICS_update (GSF_stats,
435 gettext_noop ("# cadet connections active"), -1,
437 if (NULL != sc->terminate_task)
438 GNUNET_SCHEDULER_cancel (sc->terminate_task);
439 if (NULL != sc->timeout_task)
440 GNUNET_SCHEDULER_cancel (sc->timeout_task);
442 GNUNET_DATASTORE_cancel (sc->qe);
443 while (NULL != (wqi = sc->wqi_head))
445 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
450 GNUNET_CONTAINER_DLL_remove (sc_head,
459 * Function called whenever an MQ-channel's transmission window size changes.
461 * The first callback in an outgoing channel will be with a non-zero value
462 * and will mean the channel is connected to the destination.
464 * For an incoming channel it will be called immediately after the
465 * #GNUNET_CADET_ConnectEventHandler, also with a non-zero value.
467 * @param cls Channel closure.
468 * @param channel Connection to the other end (henceforth invalid).
469 * @param window_size New window size. If the is more messages than buffer size
470 * this value will be negative..
473 window_change_cb (void *cls,
474 const struct GNUNET_CADET_Channel *channel,
477 /* FIXME: could do flow control here... */
482 * Initialize subsystem for non-anonymous file-sharing.
485 GSF_cadet_start_server ()
487 struct GNUNET_MQ_MessageHandler handlers[] = {
488 GNUNET_MQ_hd_fixed_size (request,
489 GNUNET_MESSAGE_TYPE_FS_CADET_QUERY,
490 struct CadetQueryMessage,
492 GNUNET_MQ_handler_end ()
494 struct GNUNET_HashCode port;
497 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Initializing cadet FS server with a limit of %llu connections\n",
505 cadet_map = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES);
506 cadet_handle = GNUNET_CADET_connect (GSF_cfg);
507 GNUNET_assert (NULL != cadet_handle);
508 GNUNET_CRYPTO_hash (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER,
509 strlen (GNUNET_APPLICATION_PORT_FS_BLOCK_TRANSFER),
511 cadet_port = GNUNET_CADET_open_port (cadet_handle,
522 * Shutdown subsystem for non-anonymous file-sharing.
525 GSF_cadet_stop_server ()
527 GNUNET_CONTAINER_multipeermap_iterate (cadet_map,
528 &GSF_cadet_release_clients,
530 GNUNET_CONTAINER_multipeermap_destroy (cadet_map);
532 if (NULL != cadet_port)
534 GNUNET_CADET_close_port (cadet_port);
537 if (NULL != cadet_handle)
539 GNUNET_CADET_disconnect (cadet_handle);
542 GNUNET_assert (NULL == sc_head);
543 GNUNET_assert (0 == sc_count);
546 /* end of gnunet-service-fs_cadet.c */