GNUnet  0.20.0
namecache_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2013, 2016 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_signatures.h"
35 #include "namecache.h"
36 
37 
38 #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)
39 
40 
46 {
51 
56 
61 
66 
70  void *cont_cls;
71 
76 
81 
85  uint32_t op_id;
86 };
87 
88 
93 {
98 
103 
107  struct GNUNET_CLIENT_TransmitHandle *th;
108 
113 
118 
123 
128 
133 
137  uint32_t last_op_id_used;
138 };
139 
140 
146 static void
148 
149 
157 static struct GNUNET_NAMECACHE_QueueEntry *
159  uint32_t rid)
160 {
162 
163  for (qe = h->op_head; qe != NULL; qe = qe->next)
164  {
165  if (qe->op_id == rid)
166  {
167  GNUNET_CONTAINER_DLL_remove (h->op_head,
168  h->op_tail,
169  qe);
170  return qe;
171  }
172  }
173  return NULL;
174 }
175 
176 
184 static int
186  const struct LookupBlockResponseMessage *msg)
187 {
188  /* any length will do, format validation is in handler */
189  return GNUNET_OK;
190 }
191 
192 
200 static void
202  const struct LookupBlockResponseMessage *msg)
203 {
204  struct GNUNET_NAMECACHE_Handle *h = cls;
205  size_t size;
207 
209  "Received LOOKUP_BLOCK_RESPONSE\n");
210  qe = find_qe (h,
211  ntohl (msg->gns_header.r_id));
212  if (NULL == qe)
213  return;
214  if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
215  {
216  /* no match found */
217  if (NULL != qe->block_proc)
218  qe->block_proc (qe->block_proc_cls,
219  NULL);
220  GNUNET_free (qe);
221  return;
222  }
223  size = ntohs (msg->gns_header.header.size)
224  - sizeof(struct LookupBlockResponseMessage);
225  {
226  char buf[size] GNUNET_ALIGN;
227  struct GNUNET_GNSRECORD_Block *block;
228 
229  memset (buf, 0, size);
230  block = (struct GNUNET_GNSRECORD_Block *) buf;
231  GNUNET_memcpy (block,
232  &msg[1],
233  size);
234  if (GNUNET_OK !=
236  {
237  GNUNET_break (0);
238  if (NULL != qe->block_proc)
239  qe->block_proc (qe->block_proc_cls,
240  NULL);
241  force_reconnect (h);
242  }
243  else
244  {
245  if (NULL != qe->block_proc)
246  qe->block_proc (qe->block_proc_cls,
247  block);
248  }
249  }
250  GNUNET_free (qe);
251 }
252 
253 
262 static void
264  const struct BlockCacheResponseMessage *msg)
265 {
266  struct GNUNET_NAMECACHE_Handle *h = cls;
268  int res;
269 
271  "Received BLOCK_CACHE_RESPONSE\n");
272  qe = find_qe (h,
273  ntohl (msg->gns_header.r_id));
274  if (NULL == qe)
275  return;
276  res = ntohl (msg->op_result);
277  /* TODO: add actual error message from namecache to response... */
278  if (NULL != qe->cont)
279  qe->cont (qe->cont_cls,
280  res,
281  (GNUNET_OK == res)
282  ? NULL
283  : _ ("Namecache failed to cache block"));
284  GNUNET_free (qe);
285 }
286 
287 
296 static void
297 mq_error_handler (void *cls,
298  enum GNUNET_MQ_Error error)
299 {
300  struct GNUNET_NAMECACHE_Handle *h = cls;
301 
302  force_reconnect (h);
303 }
304 
305 
311 static void
313 {
315  GNUNET_MQ_hd_var_size (lookup_block_response,
318  h),
319  GNUNET_MQ_hd_fixed_size (block_cache_response,
322  h),
324  };
325 
326  GNUNET_assert (NULL == h->mq);
328  "namecache",
329  handlers,
331  h);
332 }
333 
334 
340 static void
342 {
343  struct GNUNET_NAMECACHE_Handle *h = cls;
344 
345  h->reconnect_task = NULL;
346  reconnect (h);
347 }
348 
349 
355 static void
357 {
359 
360  h->reconnect = GNUNET_NO;
362  h->mq = NULL;
363  while (NULL != (qe = h->op_head))
364  {
365  GNUNET_CONTAINER_DLL_remove (h->op_head,
366  h->op_tail,
367  qe);
368  if (NULL != qe->cont)
369  qe->cont (qe->cont_cls,
371  _ ("Error communicating with namecache service"));
372  GNUNET_free (qe);
373  }
375  "Reconnecting to namecache\n");
376  h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
377  h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
379  h);
380 }
381 
382 
389 static uint32_t
391 {
392  return h->last_op_id_used++;
393 }
394 
395 
404 {
405  struct GNUNET_NAMECACHE_Handle *h;
406 
408  h->cfg = cfg;
409  reconnect (h);
410  if (NULL == h->mq)
411  {
412  GNUNET_free (h);
413  return NULL;
414  }
415  return h;
416 }
417 
418 
425 void
427 {
429 
430  GNUNET_break (NULL == h->op_head);
431  while (NULL != (q = h->op_head))
432  {
433  GNUNET_CONTAINER_DLL_remove (h->op_head,
434  h->op_tail,
435  q);
436  GNUNET_free (q);
437  }
438  if (NULL != h->mq)
439  {
441  h->mq = NULL;
442  }
443  if (NULL != h->reconnect_task)
444  {
446  h->reconnect_task = NULL;
447  }
448  GNUNET_free (h);
449 }
450 
451 
464  const struct GNUNET_GNSRECORD_Block *block,
466  void *cont_cls)
467 {
469  struct BlockCacheMessage *msg;
470  struct GNUNET_MQ_Envelope *env;
471  uint32_t rid;
472  size_t blen;
473 
474  if (NULL == h->mq)
475  return NULL;
476  blen = GNUNET_GNSRECORD_block_get_size (block);
477  rid = get_op_id (h);
479  qe->nsh = h;
480  qe->cont = cont;
481  qe->cont_cls = cont_cls;
482  qe->op_id = rid;
484  h->op_tail,
485  qe);
486  /* send msg */
488  blen,
490  msg->gns_header.r_id = htonl (rid);
491  GNUNET_memcpy (&msg[1],
492  block,
493  blen);
494  GNUNET_MQ_send (h->mq,
495  env);
496  return qe;
497 }
498 
499 
513  const struct GNUNET_HashCode *derived_hash,
515  void *proc_cls)
516 {
518  struct LookupBlockMessage *msg;
519  struct GNUNET_MQ_Envelope *env;
520  uint32_t rid;
521 
522  if (NULL == h->mq)
523  return NULL;
525  "Looking for block under %s\n",
526  GNUNET_h2s (derived_hash));
527  rid = get_op_id (h);
529  qe->nsh = h;
530  qe->block_proc = proc;
531  qe->block_proc_cls = proc_cls;
532  qe->op_id = rid;
534  h->op_tail,
535  qe);
536  env = GNUNET_MQ_msg (msg,
538  msg->gns_header.r_id = htonl (rid);
539  msg->query = *derived_hash;
540  GNUNET_MQ_send (h->mq,
541  env);
542  return qe;
543 }
544 
545 
552 void
554 {
555  struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
556 
557  GNUNET_CONTAINER_DLL_remove (h->op_head,
558  h->op_tail,
559  qe);
560  GNUNET_free (qe);
561 }
562 
563 
564 /* end of namecache_api.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
static int res
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_DATASTORE_QueueEntry * qe
Current operation.
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
static char buf[2048]
API that can be used to manipulate GNS record data.
API that can be used to store naming information on a GNUnet node.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1057
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_verify(const struct GNUNET_GNSRECORD_Block *block)
Check if a signature is valid.
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
#define GNUNET_log(kind,...)
void * cls
Closure for mv and cb.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#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:63
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:78
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
void(* GNUNET_NAMECACHE_ContinuationWithStatus)(void *cls, int32_t success, const char *emsg)
Continuation called to notify client about result of the operation.
void GNUNET_NAMECACHE_disconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from the namecache service (and free associated resources).
void GNUNET_NAMECACHE_cancel(struct GNUNET_NAMECACHE_QueueEntry *qe)
Cancel a namecache operation.
void(* GNUNET_NAMECACHE_BlockProcessor)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Process a record that was stored in the namecache.
struct GNUNET_NAMECACHE_Handle * GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the NAMECACHE service.
struct GNUNET_NAMECACHE_QueueEntry * GNUNET_NAMECACHE_lookup_block(struct GNUNET_NAMECACHE_Handle *h, const struct GNUNET_HashCode *derived_hash, GNUNET_NAMECACHE_BlockProcessor proc, void *proc_cls)
Get a result for a particular key from the namecache.
struct GNUNET_NAMECACHE_QueueEntry * GNUNET_NAMECACHE_block_cache(struct GNUNET_NAMECACHE_Handle *h, const struct GNUNET_GNSRECORD_Block *block, GNUNET_NAMECACHE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the namecache.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK
Client to service: lookup block.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
Service to client: result of block cache request.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE
Client to service: cache a block.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE
Service to client: result of block lookup.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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:1272
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:737
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
common internal definitions for namecache service
static uint32_t get_op_id(struct GNUNET_NAMECACHE_Handle *h)
Get a fresh operation id to distinguish between namecache requests.
static void handle_lookup_block_response(void *cls, const struct LookupBlockResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
static void reconnect(struct GNUNET_NAMECACHE_Handle *h)
Reconnect to namecache service.
static int check_lookup_block_response(void *cls, const struct LookupBlockResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
static struct GNUNET_NAMECACHE_QueueEntry * find_qe(struct GNUNET_NAMECACHE_Handle *h, uint32_t rid)
Find queue entry for the given rid.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
static void force_reconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from service and then reconnect.
#define LOG(kind,...)
Definition: namecache_api.c:38
static void handle_block_cache_response(void *cls, const struct BlockCacheResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE.
static void reconnect_task(void *cls)
Re-establish the connection to the service.
static unsigned int size
Size of the "table".
Definition: peer.c:68
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Cache a record in the namecache.
Definition: namecache.h:95
Response to a request to cache a block.
Definition: namecache.h:114
struct GNUNET_MQ_Handle * mq
Our connection to the ARM service.
Definition: arm_api.c:107
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the reconnect task (if any).
Definition: arm_api.c:147
struct GNUNET_DATASTORE_QueueEntry * next
This is a linked list.
GNUNET_DATASTORE_ContinuationWithStatus cont
Function to call after transmission of the request.
void * cont_cls
Closure for cont.
A 512-bit hashcode.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Connection to the NAMECACHE service.
Definition: namecache_api.c:93
struct GNUNET_CLIENT_TransmitHandle * th
Currently pending transmission request (or NULL).
struct GNUNET_TIME_Relative reconnect_delay
Delay introduced before we reconnect.
struct GNUNET_MQ_Handle * mq
Message queue to service.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: namecache_api.c:97
uint32_t last_op_id_used
The last operation id used for a NAMECACHE operation.
struct GNUNET_NAMECACHE_QueueEntry * op_head
Head of pending namecache queue entries.
int reconnect
Should we reconnect to service due to some serious error?
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
struct GNUNET_NAMECACHE_QueueEntry * op_tail
Tail of pending namecache queue entries.
An QueueEntry used to store information for a pending NAMECACHE record operation.
Definition: namecache_api.c:46
struct GNUNET_NAMECACHE_QueueEntry * prev
Kept in a DLL.
Definition: namecache_api.c:55
void * cont_cls
Closure for cont.
Definition: namecache_api.c:70
struct GNUNET_NAMECACHE_QueueEntry * next
Kept in a DLL.
Definition: namecache_api.c:50
GNUNET_NAMECACHE_ContinuationWithStatus cont
Continuation to call.
Definition: namecache_api.c:65
GNUNET_NAMECACHE_BlockProcessor block_proc
Function to call with the blocks we get back; or NULL.
Definition: namecache_api.c:75
struct GNUNET_NAMECACHE_Handle * nsh
Main handle to access the namecache.
Definition: namecache_api.c:60
uint32_t op_id
The operation id this zone iteration operation has.
Definition: namecache_api.c:85
void * block_proc_cls
Closure for block_proc.
Definition: namecache_api.c:80
Entry in list of pending tasks.
Definition: scheduler.c:136
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
Lookup a block in the namecache.
Definition: namecache.h:59
Lookup response.
Definition: namecache.h:76