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_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'
157 * @param tc scheduler context
160 timeout_cadet_task (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 struct CadetClient *sc = cls;
164 struct GNUNET_CADET_Channel *tun;
166 sc->timeout_task = NULL;
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Timeout for inactive cadet client %p\n",
172 GNUNET_CADET_channel_destroy (tun);
177 * Reset the timeout for the cadet client (due to activity).
179 * @param sc client handle to reset timeout for
182 refresh_timeout_task (struct CadetClient *sc)
184 if (NULL != 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 CadetClient *sc)
200 refresh_timeout_task (sc);
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Finished processing cadet request from client %p, ready to receive the next one\n",
204 GNUNET_CADET_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 CadetClient *sc);
218 * Send a reply now, cadet is ready.
220 * @param cls closure with the `struct CadetClient` 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 CadetClient *sc = cls;
231 struct GNUNET_CADET_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 cadet\n");
249 GNUNET_CADET_channel_destroy (tun);
252 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Transmitted %u byte reply via cadet to %p\n",
259 GNUNET_STATISTICS_update (GSF_stats,
260 gettext_noop ("# Blocks transferred via cadet"), 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 CadetClient *sc)
277 struct WriteQueueItem *wqi;
278 struct GNUNET_CADET_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_CADET_notify_transmit_ready (sc->channel, GNUNET_NO,
294 GNUNET_TIME_UNIT_FOREVER_REL,
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Write failed; terminating cadet\n");
304 GNUNET_CADET_channel_destroy (tun);
311 * Process a datum that was stored in the datastore.
313 * @param cls closure with the `struct CadetClient` 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,
329 enum GNUNET_BLOCK_Type type,
332 struct GNUNET_TIME_Absolute expiration,
335 struct CadetClient *sc = cls;
336 size_t msize = size + sizeof (struct CadetReplyMessage);
337 struct WriteQueueItem *wqi;
338 struct CadetReplyMessage *srm;
343 /* no result, this should not really happen, as for
344 non-anonymous routing only peers that HAVE the
345 answers should be queried; OTOH, this is not a
346 hard error as we might have had the answer in the
347 past and the user might have unindexed it. Hence
348 we log at level "INFO" for now. */
351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
352 "Have no answer and the query was NULL\n");
356 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
357 "Have no answer for query `%s'\n",
360 GNUNET_STATISTICS_update (GSF_stats,
361 gettext_noop ("# queries received via CADET not answered"), 1,
363 continue_writing (sc);
366 if (GNUNET_BLOCK_TYPE_FS_ONDEMAND == type)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Performing on-demand encoding for query %s\n",
372 GNUNET_FS_handle_on_demand_block (key,
376 &handle_datastore_reply,
379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380 "On-demand encoding request failed\n");
381 continue_writing (sc);
385 if (msize > GNUNET_SERVER_MAX_MESSAGE_SIZE)
388 continue_writing (sc);
391 GNUNET_break (GNUNET_BLOCK_TYPE_ANY != type);
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Starting transmission of %u byte reply of type %d for query `%s' via cadet to %p\n",
398 wqi = GNUNET_malloc (sizeof (struct WriteQueueItem) + msize);
400 srm = (struct CadetReplyMessage *) &wqi[1];
401 srm->header.size = htons ((uint16_t) msize);
402 srm->header.type = htons (GNUNET_MESSAGE_TYPE_FS_CADET_REPLY);
403 srm->type = htonl (type);
404 srm->expiration = GNUNET_TIME_absolute_hton (expiration);
405 memcpy (&srm[1], data, size);
406 sc->reply_size = msize;
407 GNUNET_CONTAINER_DLL_insert (sc->wqi_head,
410 continue_writing (sc);
415 * Functions with this signature are called whenever a
416 * complete query message is received.
418 * Do not call #GNUNET_SERVER_mst_destroy() in callback
420 * @param cls closure with the `struct CadetClient`
421 * @param channel channel handle
422 * @param channel_ctx channel context
423 * @param message the actual message
424 * @return #GNUNET_OK on success, #GNUNET_SYSERR to stop further processing
427 request_cb (void *cls,
428 struct GNUNET_CADET_Channel *channel,
430 const struct GNUNET_MessageHeader *message)
432 struct CadetClient *sc = *channel_ctx;
433 const struct CadetQueryMessage *sqm;
435 sqm = (const struct CadetQueryMessage *) message;
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437 "Received query for `%s' via cadet from client %p\n",
438 GNUNET_h2s (&sqm->query),
440 GNUNET_STATISTICS_update (GSF_stats,
441 gettext_noop ("# queries received via cadet"), 1,
443 refresh_timeout_task (sc);
444 sc->qe = GNUNET_DATASTORE_get_key (GSF_dsh,
449 GSF_datastore_queue_size,
450 GNUNET_TIME_UNIT_FOREVER_REL,
451 &handle_datastore_reply, sc);
454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
455 "Queueing request with datastore failed (queue full?)\n");
456 continue_writing (sc);
463 * Functions of this type are called upon new cadet connection from other peers.
465 * @param cls the closure from GNUNET_CADET_connect
466 * @param channel the channel representing the cadet
467 * @param initiator the identity of the peer who wants to establish a cadet
468 * with us; NULL on binding error
469 * @param port cadet port used for the incoming connection
470 * @param options channel option flags
471 * @return initial channel context (our 'struct CadetClient')
474 accept_cb (void *cls,
475 struct GNUNET_CADET_Channel *channel,
476 const struct GNUNET_PeerIdentity *initiator,
477 uint32_t port, enum GNUNET_CADET_ChannelOption options)
479 struct CadetClient *sc;
481 GNUNET_assert (NULL != channel);
482 if (sc_count >= sc_count_max)
484 GNUNET_STATISTICS_update (GSF_stats,
485 gettext_noop ("# cadet client connections rejected"), 1,
487 GNUNET_CADET_channel_destroy (channel);
490 GNUNET_STATISTICS_update (GSF_stats,
491 gettext_noop ("# cadet connections active"), 1,
493 sc = GNUNET_new (struct CadetClient);
494 sc->channel = channel;
495 GNUNET_CONTAINER_DLL_insert (sc_head,
499 refresh_timeout_task (sc);
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Accepting inbound cadet connection from `%s' as client %p\n",
502 GNUNET_i2s (initiator),
509 * Function called by cadet when a client disconnects.
510 * Cleans up our 'struct CadetClient' of that channel.
513 * @param channel channel of the disconnecting client
514 * @param channel_ctx our 'struct CadetClient'
517 cleaner_cb (void *cls,
518 const struct GNUNET_CADET_Channel *channel,
521 struct CadetClient *sc = channel_ctx;
522 struct WriteQueueItem *wqi;
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Terminating cadet connection with client %p\n",
530 GNUNET_STATISTICS_update (GSF_stats,
531 gettext_noop ("# cadet connections active"), -1,
533 if (NULL != sc->terminate_task)
534 GNUNET_SCHEDULER_cancel (sc->terminate_task);
535 if (NULL != sc->timeout_task)
536 GNUNET_SCHEDULER_cancel (sc->timeout_task);
538 GNUNET_CADET_notify_transmit_ready_cancel (sc->wh);
540 GNUNET_DATASTORE_cancel (sc->qe);
541 while (NULL != (wqi = sc->wqi_head))
543 GNUNET_CONTAINER_DLL_remove (sc->wqi_head,
548 GNUNET_CONTAINER_DLL_remove (sc_head,
557 * Initialize subsystem for non-anonymous file-sharing.
560 GSF_cadet_start_server ()
562 static const struct GNUNET_CADET_MessageHandler handlers[] = {
563 { &request_cb, GNUNET_MESSAGE_TYPE_FS_CADET_QUERY, sizeof (struct CadetQueryMessage)},
566 static const uint32_t ports[] = {
567 GNUNET_APPLICATION_TYPE_FS_BLOCK_TRANSFER,
572 GNUNET_CONFIGURATION_get_value_number (GSF_cfg,
577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
578 "Initializing cadet FS server with a limit of %llu connections\n",
580 listen_channel = GNUNET_CADET_connect (GSF_cfg,
590 * Shutdown subsystem for non-anonymous file-sharing.
593 GSF_cadet_stop_server ()
595 if (NULL != listen_channel)
597 GNUNET_CADET_disconnect (listen_channel);
598 listen_channel = NULL;
600 GNUNET_assert (NULL == sc_head);
601 GNUNET_assert (0 == sc_count);
604 /* end of gnunet-service-fs_cadet.c */