GNUnet  0.11.x
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_crypto_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_gnsrecord_lib.h"
35 #include "gnunet_signatures.h"
37 #include "namecache.h"
38 
39 
40 #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)
41 
42 
48 {
53 
58 
63 
68 
72  void *cont_cls;
73 
78 
83 
87  uint32_t op_id;
88 };
89 
90 
95 {
100 
105 
109  struct GNUNET_CLIENT_TransmitHandle *th;
110 
115 
120 
125 
129  struct GNUNET_TIME_Relative reconnect_delay;
130 
135 
139  uint32_t last_op_id_used;
140 };
141 
142 
148 static void
150 
151 
159 static struct GNUNET_NAMECACHE_QueueEntry *
161  uint32_t rid)
162 {
164 
165  for (qe = h->op_head; qe != NULL; qe = qe->next)
166  {
167  if (qe->op_id == rid)
168  {
170  h->op_tail,
171  qe);
172  return qe;
173  }
174  }
175  return NULL;
176 }
177 
178 
186 static int
188  const struct LookupBlockResponseMessage *msg)
189 {
190  /* any length will do, format validation is in handler */
191  return GNUNET_OK;
192 }
193 
194 
202 static void
204  const struct LookupBlockResponseMessage *msg)
205 {
206  struct GNUNET_NAMECACHE_Handle *h = cls;
207  size_t size;
209 
211  "Received LOOKUP_BLOCK_RESPONSE\n");
212  qe = find_qe (h,
213  ntohl (msg->gns_header.r_id));
214  if (NULL == qe)
215  return;
217  {
218  /* no match found */
219  if (NULL != qe->block_proc)
220  qe->block_proc (qe->block_proc_cls,
221  NULL);
222  GNUNET_free (qe);
223  return;
224  }
225  size = ntohs (msg->gns_header.header.size)
226  - sizeof(struct LookupBlockResponseMessage);
227  {
228  char buf[size] GNUNET_ALIGN;
229  struct GNUNET_GNSRECORD_Block *block;
230 
231  block = (struct GNUNET_GNSRECORD_Block *) buf;
232  GNUNET_memcpy (block,
233  &msg[1],
234  size);
235  if (GNUNET_OK !=
237  {
238  GNUNET_break (0);
239  if (NULL != qe->block_proc)
240  qe->block_proc (qe->block_proc_cls,
241  NULL);
242  force_reconnect (h);
243  }
244  else
245  {
246  if (NULL != qe->block_proc)
247  qe->block_proc (qe->block_proc_cls,
248  block);
249  }
250  }
251  GNUNET_free (qe);
252 }
253 
254 
264 static void
266  const struct BlockCacheResponseMessage *msg)
267 {
268  struct GNUNET_NAMECACHE_Handle *h = cls;
270  int res;
271 
273  "Received BLOCK_CACHE_RESPONSE\n");
274  qe = find_qe (h,
275  ntohl (msg->gns_header.r_id));
276  if (NULL == qe)
277  return;
278  res = ntohl (msg->op_result);
279  /* TODO: add actual error message from namecache to response... */
280  if (NULL != qe->cont)
281  qe->cont (qe->cont_cls,
282  res,
283  (GNUNET_OK == res)
284  ? NULL
285  : _ ("Namecache failed to cache block"));
286  GNUNET_free (qe);
287 }
288 
289 
298 static void
299 mq_error_handler (void *cls,
300  enum GNUNET_MQ_Error error)
301 {
302  struct GNUNET_NAMECACHE_Handle *h = cls;
303 
304  force_reconnect (h);
305 }
306 
307 
313 static void
315 {
316  struct GNUNET_MQ_MessageHandler handlers[] = {
317  GNUNET_MQ_hd_var_size (lookup_block_response,
320  h),
321  GNUNET_MQ_hd_fixed_size (block_cache_response,
324  h),
326  };
327 
328  GNUNET_assert (NULL == h->mq);
329  h->mq = GNUNET_CLIENT_connect (h->cfg,
330  "namecache",
331  handlers,
333  h);
334 }
335 
336 
342 static void
344 {
345  struct GNUNET_NAMECACHE_Handle *h = cls;
346 
347  h->reconnect_task = NULL;
348  reconnect (h);
349 }
350 
351 
357 static void
359 {
361 
362  h->reconnect = GNUNET_NO;
363  GNUNET_MQ_destroy (h->mq);
364  h->mq = NULL;
365  while (NULL != (qe = h->op_head))
366  {
368  h->op_tail,
369  qe);
370  if (NULL != qe->cont)
371  qe->cont (qe->cont_cls,
373  _ ("Error communicating with namecache service"));
374  GNUNET_free (qe);
375  }
377  "Reconnecting to namecache\n");
381  h);
382 }
383 
384 
391 static uint32_t
393 {
394  return h->last_op_id_used++;
395 }
396 
397 
406 {
407  struct GNUNET_NAMECACHE_Handle *h;
408 
409  h = GNUNET_new (struct GNUNET_NAMECACHE_Handle);
410  h->cfg = cfg;
411  reconnect (h);
412  if (NULL == h->mq)
413  {
414  GNUNET_free (h);
415  return NULL;
416  }
417  return h;
418 }
419 
420 
427 void
429 {
431 
432  GNUNET_break (NULL == h->op_head);
433  while (NULL != (q = h->op_head))
434  {
436  h->op_tail,
437  q);
438  GNUNET_free (q);
439  }
440  if (NULL != h->mq)
441  {
442  GNUNET_MQ_destroy (h->mq);
443  h->mq = NULL;
444  }
445  if (NULL != h->reconnect_task)
446  {
448  h->reconnect_task = NULL;
449  }
450  GNUNET_free (h);
451 }
452 
453 
466  const struct GNUNET_GNSRECORD_Block *block,
468  void *cont_cls)
469 {
471  struct BlockCacheMessage *msg;
472  struct GNUNET_MQ_Envelope *env;
473  uint32_t rid;
474  size_t blen;
475 
476  if (NULL == h->mq)
477  return NULL;
478  blen = GNUNET_GNSRECORD_block_get_size (block);
479  rid = get_op_id (h);
481  qe->nsh = h;
482  qe->cont = cont;
483  qe->cont_cls = cont_cls;
484  qe->op_id = rid;
486  h->op_tail,
487  qe);
488  /* send msg */
489  env = GNUNET_MQ_msg_extra (msg,
490  blen,
492  msg->gns_header.r_id = htonl (rid);
493  GNUNET_memcpy (&msg[1],
494  block,
495  blen);
496  GNUNET_MQ_send (h->mq,
497  env);
498  return qe;
499 }
500 
501 
515  const struct GNUNET_HashCode *derived_hash,
517  void *proc_cls)
518 {
520  struct LookupBlockMessage *msg;
521  struct GNUNET_MQ_Envelope *env;
522  uint32_t rid;
523 
524  if (NULL == h->mq)
525  return NULL;
527  "Looking for block under %s\n",
528  GNUNET_h2s (derived_hash));
529  rid = get_op_id (h);
531  qe->nsh = h;
532  qe->block_proc = proc;
533  qe->block_proc_cls = proc_cls;
534  qe->op_id = rid;
536  h->op_tail,
537  qe);
538  env = GNUNET_MQ_msg (msg,
540  msg->gns_header.r_id = htonl (rid);
541  msg->query = *derived_hash;
542  GNUNET_MQ_send (h->mq,
543  env);
544  return qe;
545 }
546 
547 
554 void
556 {
557  struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
558 
560  h->op_tail,
561  qe);
562  GNUNET_free (qe);
563 }
564 
565 
566 /* end of namecache_api.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE
Service to client: result of block lookup.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
Service to client: result of block cache request.
struct GNUNET_TIME_Relative reconnect_delay
Delay introduced before we reconnect.
Lookup a block in the namecache.
Definition: namecache.h:58
struct GNUNET_NAMECACHE_Handle * GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the NAMECACHE service.
static struct GNUNET_DATASTORE_QueueEntry * qe
Current operation.
#define GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE
Client to service: cache a block.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
Lookup response.
Definition: namecache.h:75
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:474
void(* GNUNET_NAMECACHE_BlockProcessor)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Process a record that was stored in the namecache.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
uint32_t op_id
The operation id this zone iteration operation has.
Definition: namecache_api.c:87
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:1063
GNUNET_MQ_Error
Error codes for the queue.
Response to a request to cache a block.
Definition: namecache.h:133
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_* header.size will be message size ...
Definition: namecache.h:46
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK.
Definition: namecache.h:63
struct GNUNET_NAMECACHE_QueueEntry * next
Kept in a DLL.
Definition: namecache_api.c:52
struct GNUNET_NAMECACHE_QueueEntry * op_tail
Tail of pending namecache queue entries.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
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.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
static struct GNUNET_NAMECACHE_QueueEntry * find_qe(struct GNUNET_NAMECACHE_Handle *h, uint32_t rid)
Find queue entry for the given rid.
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.
uint64_t abs_value_us
The actual value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK
Client to service: lookup block.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:99
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 handle_block_cache_response(void *cls, const struct BlockCacheResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
cryptographic primitives for GNUnet
#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:1269
struct GNUNET_NAMECACHE_Handle * nsh
Main handle to access the namecache.
Definition: namecache_api.c:62
void * cls
Closure for mv and cb.
Cache a record in the namecache.
Definition: namecache.h:104
void * cont_cls
Closure for cont.
Definition: namecache_api.c:72
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: namecache_api.c:99
static char buf[2048]
void GNUNET_NAMECACHE_cancel(struct GNUNET_NAMECACHE_QueueEntry *qe)
Cancel a namecache operation.
void * block_proc_cls
Closure for block_proc.
Definition: namecache_api.c:82
GNUNET_NAMECACHE_BlockProcessor block_proc
Function to call with the blocks we get back; or NULL.
Definition: namecache_api.c:77
static void reconnect(struct GNUNET_NAMECACHE_Handle *h)
Reconnect to namecache service.
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.
A 512-bit hashcode.
static uint32_t get_op_id(struct GNUNET_NAMECACHE_Handle *h)
Get a fresh operation id to distinguish between namecache requests.
Message handler for a specific message type.
static int res
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE.
Definition: namecache.h:138
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
Definition: namecache.h:80
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_block_verify(const struct GNUNET_GNSRECORD_Block *block)
Check if a signature is valid.
struct GNUNET_MQ_Handle * mq
Message queue to service.
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 unsigned int size
Size of the "table".
Definition: peer.c:67
int reconnect
Should we reconnect to service due to some serious error?
void(* GNUNET_NAMECACHE_ContinuationWithStatus)(void *cls, int32_t success, const char *emsg)
Continuation called to notify client about result of the operation.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
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.
Handle to a message queue.
Definition: mq.c:85
struct GNUNET_HashCode query
The query.
Definition: namecache.h:68
struct GNUNET_NAMECACHE_QueueEntry * op_head
Head of pending namecache queue entries.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to &#39;struct&#39;s...
configuration data
Definition: configuration.c:84
struct GNUNET_NAMECACHE_QueueEntry * prev
Kept in a DLL.
Definition: namecache_api.c:57
uint32_t r_id
Request ID in NBO.
Definition: namecache.h:51
Connection to the NAMECACHE service.
Definition: namecache_api.c:94
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
void GNUNET_NAMECACHE_disconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from the namecache service (and free associated resources).
#define LOG(kind,...)
Definition: namecache_api.c:40
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:837
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time.
Definition: namecache.h:85
uint32_t last_op_id_used
The last operation id used for a NAMECACHE operation.
An QueueEntry used to store information for a pending NAMECACHE record operation. ...
Definition: namecache_api.c:47
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:355
int32_t op_result
GNUNET_OK on success, GNUNET_SYSERR error
Definition: namecache.h:143
static void force_reconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from service and then reconnect.
common internal definitions for namecache service
struct GNUNET_CLIENT_TransmitHandle * th
Currently pending transmission request (or NULL).
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE.
Definition: namecache.h:109
GNUNET_NAMECACHE_ContinuationWithStatus cont
Continuation to call.
Definition: namecache_api.c:67
static void reconnect_task(void *cls)
Re-establish the connection to the service.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972