GNUnet  0.10.x
Go to the documentation of this file.
1 /*
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
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 <>.
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
30 #include "platform.h"
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"
50 {
64  size_t msize;
65 };
71 struct CadetClient
72 {
76  struct CadetClient *next;
81  struct CadetClient *prev;
116  size_t reply_size;
118 };
129 static struct CadetClient *sc_head;
134 static struct CadetClient *sc_tail;
139 static unsigned int sc_count;
144 static unsigned long long sc_count_max;
153 static void
155 {
156  struct CadetClient *sc = cls;
157  struct GNUNET_CADET_Channel *tun;
159  sc->timeout_task = NULL;
160  tun = sc->channel;
161  sc->channel = NULL;
163  "Timeout for inactive cadet client %p\n",
164  sc);
166 }
174 static void
176 {
177  if (NULL != sc->timeout_task)
181  sc);
182 }
191 static void
192 continue_writing (void *cls)
193 {
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))
199  {
201  "Write pending, waiting for it to complete\n");
202  return;
203  }
206  "Finished processing cadet request from client %p, ready to receive the next one\n",
207  sc);
209 }
227 static void
229  const struct GNUNET_HashCode *key,
230  size_t size,
231  const void *data,
232  enum GNUNET_BLOCK_Type type,
233  uint32_t priority,
234  uint32_t anonymity,
235  uint32_t replication,
237  uint64_t uid)
238 {
239  struct CadetClient *sc = cls;
240  size_t msize = size + sizeof (struct CadetReplyMessage);
241  struct GNUNET_MQ_Envelope *env;
242  struct CadetReplyMessage *srm;
244  sc->qe = NULL;
245  if (NULL == data)
246  {
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. */
253  if (NULL == key)
254  {
256  "Have no answer and the query was NULL\n");
257  }
258  else
259  {
261  "Have no answer for query `%s'\n",
262  GNUNET_h2s (key));
263  }
265  gettext_noop ("# queries received via CADET not answered"),
266  1,
267  GNUNET_NO);
268  continue_writing (sc);
269  return;
270  }
272  {
274  "Performing on-demand encoding for query %s\n",
275  GNUNET_h2s (key));
276  if (GNUNET_OK !=
278  size,
279  data,
280  type,
281  priority,
282  anonymity,
283  replication,
284  expiration,
285  uid,
287  sc))
288  {
290  "On-demand encoding request failed\n");
291  continue_writing (sc);
292  }
293  return;
294  }
295  if (msize > GNUNET_MAX_MESSAGE_SIZE)
296  {
297  GNUNET_break (0);
298  continue_writing (sc);
299  return;
300  }
303  "Starting transmission of %u byte reply of type %d for query `%s' via cadet to %p\n",
304  (unsigned int) size,
305  (unsigned int) type,
306  GNUNET_h2s (key),
307  sc);
308  env = GNUNET_MQ_msg_extra (srm,
309  size,
311  srm->type = htonl (type);
312  srm->expiration = GNUNET_TIME_absolute_hton (expiration);
313  GNUNET_memcpy (&srm[1],
314  data,
315  size);
318  sc);
320  gettext_noop ("# Blocks transferred via cadet"),
321  1,
322  GNUNET_NO);
324  env);
325 }
335 static void
336 handle_request (void *cls,
337  const struct CadetQueryMessage *sqm)
338 {
339  struct CadetClient *sc = cls;
342  "Received query for `%s' via cadet from client %p\n",
343  GNUNET_h2s (&sqm->query),
344  sc);
346  gettext_noop ("# queries received via cadet"),
347  1,
348  GNUNET_NO);
351  0 /* next_uid */,
352  false /* random */,
353  &sqm->query,
354  ntohl (sqm->type),
355  0 /* priority */,
358  sc);
359  if (NULL == sc->qe)
360  {
362  "Queueing request with datastore failed (queue full?)\n");
363  continue_writing (sc);
364  }
365 }
377 static void *
378 connect_cb (void *cls,
380  const struct GNUNET_PeerIdentity *initiator)
381 {
382  struct CadetClient *sc;
384  GNUNET_assert (NULL != channel);
385  if (sc_count >= sc_count_max)
386  {
388  gettext_noop ("# cadet client connections rejected"),
389  1,
390  GNUNET_NO);
392  return NULL;
393  }
395  gettext_noop ("# cadet connections active"),
396  1,
397  GNUNET_NO);
398  sc = GNUNET_new (struct CadetClient);
399  sc->channel = channel;
401  sc_tail,
402  sc);
403  sc_count++;
406  "Accepting inbound cadet connection from `%s' as client %p\n",
407  GNUNET_i2s (initiator),
408  sc);
409  return sc;
410 }
421 static void
422 disconnect_cb (void *cls,
423  const struct GNUNET_CADET_Channel *channel)
424 {
425  struct CadetClient *sc = cls;
426  struct WriteQueueItem *wqi;
428  if (NULL == sc)
429  return;
430  sc->channel = NULL;
432  "Terminating cadet connection with client %p\n",
433  sc);
435  gettext_noop ("# cadet connections active"), -1,
436  GNUNET_NO);
437  if (NULL != sc->terminate_task)
439  if (NULL != sc->timeout_task)
441  if (NULL != sc->qe)
443  while (NULL != (wqi = sc->wqi_head))
444  {
446  sc->wqi_tail,
447  wqi);
448  GNUNET_free (wqi);
449  }
451  sc_tail,
452  sc);
453  sc_count--;
454  GNUNET_free (sc);
455 }
472 static void
473 window_change_cb (void *cls,
474  const struct GNUNET_CADET_Channel *channel,
475  int window_size)
476 {
477  /* FIXME: could do flow control here... */
478 }
484 void
486 {
487  struct GNUNET_MQ_MessageHandler handlers[] = {
490  struct CadetQueryMessage,
491  NULL),
493  };
494  struct GNUNET_HashCode port;
496  if (GNUNET_YES !=
498  "fs",
500  &sc_count_max))
501  return;
503  "Initializing cadet FS server with a limit of %llu connections\n",
504  sc_count_max);
507  GNUNET_assert (NULL != cadet_handle);
510  &port);
511  cadet_port = GNUNET_CADET_open_port (cadet_handle,
512  &port,
513  &connect_cb,
514  NULL,
516  &disconnect_cb,
517  handlers);
518 }
524 void
526 {
529  NULL);
531  cadet_map = NULL;
532  if (NULL != cadet_port)
533  {
534  GNUNET_CADET_close_port (cadet_port);
535  cadet_port = NULL;
536  }
537  if (NULL != cadet_handle)
538  {
540  cadet_handle = NULL;
541  }
542  GNUNET_assert (NULL == sc_head);
543  GNUNET_assert (0 == sc_count);
544 }
546 /* end of gnunet-service-fs_cadet.c */
static void * connect_cb(void *cls, struct GNUNET_CADET_Channel *channel, const struct GNUNET_PeerIdentity *initiator)
Functions of this type are called upon new cadet connection from other peers.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
P2P answer for content (one FS to another via a cadet).
size_t msize
Number of bytes of payload, allocated at the end of this struct.
static struct GNUNET_CADET_Handle * cadet_handle
The handle to cadet.
void GNUNET_CADET_disconnect(struct GNUNET_CADET_Handle *handle)
Disconnect from the cadet service.
Definition: cadet_api.c:849
Query from one peer, asking the other for CHK-data.
static char * expiration
Credential TTL.
int GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
static void handle_datastore_reply(void *cls, const struct GNUNET_HashCode *key, size_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
Process a datum that was stored in the datastore.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
Any type of block, used as a wildcard when searching.
struct GNUNET_SCHEDULER_Task * timeout_task
Task that is scheduled to terminate idle connections.
struct WriteQueueItem * next
Kept in a DLL.
struct GNUNET_SCHEDULER_Task * terminate_task
Task that is scheduled to asynchronously terminate the connection.
Blocks in the datastore and the datacache must have a unique type.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct CadetClient * sc_head
Head of DLL of cadet clients.
static unsigned int replication
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
static unsigned long long sc_count_max
Maximum allowed number of cadet clients.
uint32_t type
Block type must be DBLOCK or IBLOCK.
struct GNUNET_STATISTICS_Handle * GSF_stats
Handle for reporting statistics.
#define GNUNET_NO
Definition: gnunet_common.h:81
shared data structures of gnunet-service-fs.c
Opaque handle to a channel.
Definition: cadet_api.c:80
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
const struct GNUNET_CONFIGURATION_Handle * GSF_cfg
Our configuration.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
Type of a block representing a block to be encoded on demand from disk.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
struct GNUNET_CADET_Handle * GNUNET_CADET_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Connect to the MQ-based cadet service.
Definition: cadet_api.c:995
static void window_change_cb(void *cls, const struct GNUNET_CADET_Channel *channel, int window_size)
Function called whenever an MQ-channel&#39;s transmission window size changes.
After how long do we termiante idle connections?
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
static void refresh_timeout_task(struct CadetClient *sc)
Reset the timeout for the cadet client (due to activity).
Entry in our priority queue.
struct GNUNET_CADET_Port * GNUNET_CADET_open_port(struct GNUNET_CADET_Handle *h, const struct GNUNET_HashCode *port, GNUNET_CADET_ConnectEventHandler connects, void *connects_cls, GNUNET_CADET_WindowSizeEventHandler window_changes, GNUNET_CADET_DisconnectEventHandler disconnects, const struct GNUNET_MQ_MessageHandler *handlers)
Open a port to receive incomming MQ-based channels.
Definition: cadet_api.c:1029
#define GNUNET_memcpy(dst, src, n)
struct GNUNET_HashCode query
Query hash from CHK (hash of encrypted block).
void GNUNET_MQ_notify_sent(struct GNUNET_MQ_Envelope *ev, GNUNET_SCHEDULER_TaskCallback cb, void *cb_cls)
Call a callback once the envelope has been sent, that is, sending it can not be canceled anymore...
Definition: mq.c:774
int GSF_cadet_release_clients(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Function called on each active cadets to shut them down.
static unsigned int sc_count
Number of active cadet clients in the &#39;sc_*&#39;-DLL.
static void continue_writing(void *cls)
Check if we are done with the write queue, and if so tell CADET that we are ready to read more...
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
unsigned int GNUNET_MQ_get_length(struct GNUNET_MQ_Handle *mq)
Obtain the current length of the message queue.
Definition: mq.c:335
unsigned int GSF_datastore_queue_size
Size of the datastore queue we assume for common requests.
non-anonymous file-transfer
static void timeout_cadet_task(void *cls)
Task run to asynchronously terminate the cadet due to timeout.
struct GNUNET_CONTAINER_MultiPeerMap * cadet_map
Map from peer identities to &#39;struct CadetHandles&#39; with cadet channels to those peers.
struct WriteQueueItem * prev
Kept in a DLL.
struct WriteQueueItem * wqi_head
Head of write queue.
enum GNUNET_MQ_PriorityPreferences priority
Flags that were set for this queue by GNUNET_MQ_set_options().
Definition: mq.c:164
A 512-bit hashcode.
Message handler for a specific message type.
struct GNUNET_DATASTORE_Handle * GSF_dsh
Our connection to the datastore.
void GSF_cadet_start_server()
Initialize subsystem for non-anonymous file-sharing.
Struct containing information about a client of the service.
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:67
struct GNUNET_CADET_Channel * channel
Channel for communication.
void GSF_cadet_stop_server()
Shutdown subsystem for non-anonymous file-sharing.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
struct GNUNET_TIME_AbsoluteNBO expiration
Expiration time for the block.
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:37
indexing for the file-sharing service
size_t reply_size
Size of the last write that was initiated.
Largest supported message (to be precise, one byte more than the largest possible message...
struct WriteQueueItem * wqi_tail
Tail of write queue.
Handle to a message queue.
Definition: mq.c:85
static void disconnect_cb(void *cls, const struct GNUNET_CADET_Channel *channel)
Function called by cadet when a client disconnects.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
void GNUNET_DATASTORE_cancel(struct GNUNET_DATASTORE_QueueEntry *qe)
Cancel a datastore operation.
The identity of the host (wraps the signing key of the peer).
void GNUNET_CADET_receive_done(struct GNUNET_CADET_Channel *channel)
Send an ack on the channel to confirm the processing of a message.
Definition: cadet_api.c:973
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
P2P request for content (one FS to another via a cadet).
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
static void handle_request(void *cls, const struct CadetQueryMessage *sqm)
Functions with this signature are called whenever a complete query message is received.
Opaque handle to a port.
Definition: cadet_api.c:151
static struct GNUNET_CADET_Port * cadet_port
Listen port for incoming requests.
static struct CadetClient * sc_tail
Tail of DLL of cadet clients.
Transfer of blocks for non-anonymmous file-sharing.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
void GNUNET_CADET_close_port(struct GNUNET_CADET_Port *p)
Close a port opened with GNUNET_CADET_open_port().
Definition: cadet_api.c:882
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
static unsigned int anonymity
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
uint32_t type
Block type must be DBLOCK or IBLOCK.
struct GNUNET_DATASTORE_QueueEntry * GNUNET_DATASTORE_get_key(struct GNUNET_DATASTORE_Handle *h, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int queue_priority, unsigned int max_queue_size, GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
Get a result for a particular key from the datastore.
int GNUNET_FS_handle_on_demand_block(const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid, GNUNET_DATASTORE_DatumProcessor cont, void *cont_cls)
We&#39;ve received an on-demand encoded block from the datastore.
uint32_t data
The data value.
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:654
Reply to a CadetQueryMessage.
void GNUNET_CADET_channel_destroy(struct GNUNET_CADET_Channel *channel)
Destroy an existing channel.
Definition: cadet_api.c:911
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_DATASTORE_QueueEntry * qe
Current active request to the datastore, if we have one pending.
A message in the queue to be written to the cadet.
#define GNUNET_free(ptr)
Wrapper around free.
#define gettext_noop(String)
Definition: gettext.h:69
struct GNUNET_MQ_Handle * GNUNET_CADET_get_mq(const struct GNUNET_CADET_Channel *channel)
Obtain the message queue for a connected peer.
Definition: cadet_api.c:1142
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965