GNUnet  0.10.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 
52 
57 
62 
67 
71  void *cont_cls;
72 
77 
82 
86  uint32_t op_id;
87 };
88 
89 
98 
103 
107  struct GNUNET_CLIENT_TransmitHandle *th;
108 
113 
118 
123 
127  struct GNUNET_TIME_Relative reconnect_delay;
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  {
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;
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 + sizeof(struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN;
227  struct GNUNET_GNSRECORD_Block *block;
228 
229  block = (struct GNUNET_GNSRECORD_Block *)buf;
230  block->signature = msg->signature;
231  block->derived_key = msg->derived_key;
233  block->purpose.size = htonl(size +
234  sizeof(struct GNUNET_TIME_AbsoluteNBO) +
235  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
236  block->expiration_time = msg->expire;
237  GNUNET_memcpy(&block[1],
238  &msg[1],
239  size);
240  if (GNUNET_OK !=
242  {
243  GNUNET_break(0);
244  if (NULL != qe->block_proc)
245  qe->block_proc(qe->block_proc_cls,
246  NULL);
247  force_reconnect(h);
248  }
249  else
250  {
251  if (NULL != qe->block_proc)
252  qe->block_proc(qe->block_proc_cls,
253  block);
254  }
255  }
256  GNUNET_free(qe);
257 }
258 
259 
269 static void
271  const struct BlockCacheResponseMessage *msg)
272 {
273  struct GNUNET_NAMECACHE_Handle *h = cls;
275  int res;
276 
278  "Received BLOCK_CACHE_RESPONSE\n");
279  qe = find_qe(h,
280  ntohl(msg->gns_header.r_id));
281  if (NULL == qe)
282  return;
283  res = ntohl(msg->op_result);
284  /* TODO: add actual error message from namecache to response... */
285  if (NULL != qe->cont)
286  qe->cont(qe->cont_cls,
287  res,
288  (GNUNET_OK == res)
289  ? NULL
290  : _("Namecache failed to cache block"));
291  GNUNET_free(qe);
292 }
293 
294 
303 static void
305  enum GNUNET_MQ_Error error)
306 {
307  struct GNUNET_NAMECACHE_Handle *h = cls;
308 
309  force_reconnect(h);
310 }
311 
312 
318 static void
320 {
321  struct GNUNET_MQ_MessageHandler handlers[] = {
322  GNUNET_MQ_hd_var_size(lookup_block_response,
325  h),
326  GNUNET_MQ_hd_fixed_size(block_cache_response,
329  h),
331  };
332 
333  GNUNET_assert(NULL == h->mq);
334  h->mq = GNUNET_CLIENT_connect(h->cfg,
335  "namecache",
336  handlers,
338  h);
339 }
340 
341 
347 static void
349 {
350  struct GNUNET_NAMECACHE_Handle *h = cls;
351 
352  h->reconnect_task = NULL;
353  reconnect(h);
354 }
355 
356 
362 static void
364 {
366 
367  h->reconnect = GNUNET_NO;
368  GNUNET_MQ_destroy(h->mq);
369  h->mq = NULL;
370  while (NULL != (qe = h->op_head))
371  {
373  h->op_tail,
374  qe);
375  if (NULL != qe->cont)
376  qe->cont(qe->cont_cls,
378  _("Error communicating with namecache service"));
379  GNUNET_free(qe);
380  }
382  "Reconnecting to namecache\n");
386  h);
387 }
388 
389 
396 static uint32_t
398 {
399  return h->last_op_id_used++;
400 }
401 
402 
411 {
412  struct GNUNET_NAMECACHE_Handle *h;
413 
415  h->cfg = cfg;
416  reconnect(h);
417  if (NULL == h->mq)
418  {
419  GNUNET_free(h);
420  return NULL;
421  }
422  return h;
423 }
424 
425 
432 void
434 {
436 
437  GNUNET_break(NULL == h->op_head);
438  while (NULL != (q = h->op_head))
439  {
441  h->op_tail,
442  q);
443  GNUNET_free(q);
444  }
445  if (NULL != h->mq)
446  {
447  GNUNET_MQ_destroy(h->mq);
448  h->mq = NULL;
449  }
450  if (NULL != h->reconnect_task)
451  {
453  h->reconnect_task = NULL;
454  }
455  GNUNET_free(h);
456 }
457 
458 
471  const struct GNUNET_GNSRECORD_Block *block,
473  void *cont_cls)
474 {
476  struct BlockCacheMessage *msg;
477  struct GNUNET_MQ_Envelope *env;
478  uint32_t rid;
479  size_t blen;
480 
481  if (NULL == h->mq)
482  return NULL;
483  blen = ntohl(block->purpose.size);
484  GNUNET_assert(blen > (sizeof(struct GNUNET_TIME_AbsoluteNBO) +
485  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)));
486  blen -= (sizeof(struct GNUNET_TIME_AbsoluteNBO) +
487  sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
488  rid = get_op_id(h);
490  qe->nsh = h;
491  qe->cont = cont;
492  qe->cont_cls = cont_cls;
493  qe->op_id = rid;
495  h->op_tail,
496  qe);
497  /* send msg */
498  env = GNUNET_MQ_msg_extra(msg,
499  blen,
501  msg->gns_header.r_id = htonl(rid);
502  msg->expire = block->expiration_time;
503  msg->signature = block->signature;
504  msg->derived_key = block->derived_key;
505  GNUNET_memcpy(&msg[1],
506  &block[1],
507  blen);
508  GNUNET_MQ_send(h->mq,
509  env);
510  return qe;
511 }
512 
513 
527  const struct GNUNET_HashCode *derived_hash,
529  void *proc_cls)
530 {
532  struct LookupBlockMessage *msg;
533  struct GNUNET_MQ_Envelope *env;
534  uint32_t rid;
535 
536  if (NULL == h->mq)
537  return NULL;
539  "Looking for block under %s\n",
540  GNUNET_h2s(derived_hash));
541  rid = get_op_id(h);
543  qe->nsh = h;
544  qe->block_proc = proc;
545  qe->block_proc_cls = proc_cls;
546  qe->op_id = rid;
548  h->op_tail,
549  qe);
550  env = GNUNET_MQ_msg(msg,
552  msg->gns_header.r_id = htonl(rid);
553  msg->query = *derived_hash;
554  GNUNET_MQ_send(h->mq,
555  env);
556  return qe;
557 }
558 
559 
566 void
568 {
569  struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
570 
572  h->op_tail,
573  qe);
574  GNUNET_free(qe);
575 }
576 
577 
578 /* 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:57
struct GNUNET_NAMECACHE_Handle * GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the NAMECACHE service.
struct GNUNET_TIME_AbsoluteNBO expiration_time
Expiration time of the block.
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
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature.
Definition: namecache.h:87
Lookup response.
Definition: namecache.h:73
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:671
void(* GNUNET_NAMECACHE_BlockProcessor)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Process a record that was stored in the namecache.
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived public key.
Definition: namecache.h:120
uint32_t op_id
The operation id this zone iteration operation has.
Definition: namecache_api.c:86
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:900
uint32_t purpose
What does this signature vouch for? This must contain a GNUNET_SIGNATURE_PURPOSE_XXX constant (from g...
GNUNET_MQ_Error
Error codes for the queue.
Response to a request to cache a block.
Definition: namecache.h:129
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_* header.size will be message size ...
Definition: namecache.h:45
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK.
Definition: namecache.h:61
struct GNUNET_NAMECACHE_QueueEntry * next
Kept in a DLL.
Definition: namecache_api.c:51
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
#define GNUNET_NO
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_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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...
Information we have in an encrypted block with record data (i.e.
static struct GNUNET_NAMECACHE_QueueEntry * find_qe(struct GNUNET_NAMECACHE_Handle *h, uint32_t rid)
Find queue entry for the given rid.
Time for absolute time used by GNUnet, in microseconds and in network byte order. ...
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:94
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:181
header of what an ECC signature signs this must be followed by "size - 8" bytes of the actual signed ...
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:1237
struct GNUNET_NAMECACHE_Handle * nsh
Main handle to access the namecache.
Definition: namecache_api.c:61
void * cls
Closure for mv and cb.
Cache a record in the namecache.
Definition: namecache.h:101
void * cont_cls
Closure for cont.
Definition: namecache_api.c:71
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: namecache_api.c:97
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:81
GNUNET_NAMECACHE_BlockProcessor block_proc
Function to call with the blocks we get back; or NULL.
Definition: namecache_api.c:76
static void reconnect(struct GNUNET_NAMECACHE_Handle *h)
Reconnect to namecache service.
uint32_t size
How many bytes does this signature sign? (including this purpose header); in network byte order (!)...
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:133
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_NAMECACHE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
Definition: namecache.h:77
int 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.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time.
Definition: namecache.h:110
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:84
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature of the block.
struct GNUNET_HashCode query
The query.
Definition: namecache.h:66
struct GNUNET_NAMECACHE_QueueEntry * op_head
Head of pending namecache queue entries.
configuration data
Definition: configuration.c:83
struct GNUNET_NAMECACHE_QueueEntry * prev
Kept in a DLL.
Definition: namecache_api.c:56
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived public key.
Definition: namecache.h:92
uint32_t r_id
Request ID in NBO.
Definition: namecache.h:50
Connection to the NAMECACHE service.
Definition: namecache_api.c:93
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
#define GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN
Signature of a gnunet naming system record block.
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:821
struct GNUNET_TIME_AbsoluteNBO expire
Expiration time.
Definition: namecache.h:82
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:351
struct GNUNET_CRYPTO_EcdsaSignature signature
Signature.
Definition: namecache.h:115
int32_t op_result
GNUNET_OK on success, GNUNET_SYSERR error
Definition: namecache.h:138
static void force_reconnect(struct GNUNET_NAMECACHE_Handle *h)
Disconnect from service and then reconnect.
struct GNUNET_CRYPTO_EccSignaturePurpose purpose
Number of bytes signed; also specifies the number of bytes of encrypted data that follow...
common internal definitions for namecache service
struct GNUNET_CRYPTO_EcdsaPublicKey derived_key
Derived key used for signing; hash of this is the query.
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:105
GNUNET_NAMECACHE_ContinuationWithStatus cont
Continuation to call.
Definition: namecache_api.c:66
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:956