2 This file is part of GNUnet.
3 Copyright (C) 2010-2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file namecache/namecache_api.c
23 * @brief API to access the NAMECACHE service
24 * @author Martin Schanzenbach
25 * @author Matthias Wachs
26 * @author Christian Grothoff
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"
36 #include "gnunet_namecache_service.h"
37 #include "namecache.h"
40 #define LOG(kind,...) GNUNET_log_from (kind, "namecache-api",__VA_ARGS__)
44 * An QueueEntry used to store information for a pending
45 * NAMECACHE record operation
47 struct GNUNET_NAMECACHE_QueueEntry
53 struct GNUNET_NAMECACHE_QueueEntry *next;
58 struct GNUNET_NAMECACHE_QueueEntry *prev;
61 * Main handle to access the namecache.
63 struct GNUNET_NAMECACHE_Handle *nsh;
66 * Continuation to call
68 GNUNET_NAMECACHE_ContinuationWithStatus cont;
71 * Closure for @e cont.
76 * Function to call with the blocks we get back; or NULL.
78 GNUNET_NAMECACHE_BlockProcessor block_proc;
81 * Closure for @e block_proc.
86 * The operation id this zone iteration operation has
94 * Connection to the NAMECACHE service.
96 struct GNUNET_NAMECACHE_Handle
100 * Configuration to use.
102 const struct GNUNET_CONFIGURATION_Handle *cfg;
105 * Message queue to service.
107 struct GNUNET_MQ_Handle *mq;
110 * Currently pending transmission request (or NULL).
112 struct GNUNET_CLIENT_TransmitHandle *th;
115 * Head of pending namecache queue entries
117 struct GNUNET_NAMECACHE_QueueEntry *op_head;
120 * Tail of pending namecache queue entries
122 struct GNUNET_NAMECACHE_QueueEntry *op_tail;
127 struct GNUNET_SCHEDULER_Task *reconnect_task;
130 * Delay introduced before we reconnect.
132 struct GNUNET_TIME_Relative reconnect_delay;
135 * Should we reconnect to service due to some serious error?
140 * The last operation id used for a NAMECACHE operation
142 uint32_t last_op_id_used;
148 * Disconnect from service and then reconnect.
150 * @param h our handle
153 force_reconnect (struct GNUNET_NAMECACHE_Handle *h);
157 * Find queue entry for the given @a rid.
159 * @param h handle to search
160 * @param rid request ID to look for
161 * @return NULL if not found, otherwise the queue entry (removed from the queue)
163 static struct GNUNET_NAMECACHE_QueueEntry *
164 find_qe (struct GNUNET_NAMECACHE_Handle *h,
167 struct GNUNET_NAMECACHE_QueueEntry *qe;
169 for (qe = h->op_head; qe != NULL; qe = qe->next)
171 if (qe->op_id == rid)
173 GNUNET_CONTAINER_DLL_remove (h->op_head,
184 * Handle an incoming message of type
185 * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
187 * @param cls the `struct GNUNET_NAMECACHE_Handle`
188 * @param msg the message we received
191 check_lookup_block_response (void *cls,
192 const struct LookupBlockResponseMessage *msg)
194 /* any length will do, format validation is in handler */
200 * Handle an incoming message of type
201 * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
203 * @param cls the `struct GNUNET_NAMECACHE_Handle`
204 * @param msg the message we received
207 handle_lookup_block_response (void *cls,
208 const struct LookupBlockResponseMessage *msg)
210 struct GNUNET_NAMECACHE_Handle *h = cls;
212 struct GNUNET_NAMECACHE_QueueEntry *qe;
214 LOG (GNUNET_ERROR_TYPE_DEBUG,
215 "Received LOOKUP_BLOCK_RESPONSE\n");
217 ntohl (msg->gns_header.r_id));
220 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
223 if (NULL != qe->block_proc)
224 qe->block_proc (qe->block_proc_cls,
229 size = ntohs (msg->gns_header.header.size)
230 - sizeof (struct LookupBlockResponseMessage);
232 char buf[size + sizeof (struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN;
233 struct GNUNET_GNSRECORD_Block *block;
235 block = (struct GNUNET_GNSRECORD_Block *) buf;
236 block->signature = msg->signature;
237 block->derived_key = msg->derived_key;
238 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
239 block->purpose.size = htonl (size +
240 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
241 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
242 block->expiration_time = msg->expire;
243 GNUNET_memcpy (&block[1],
247 GNUNET_GNSRECORD_block_verify (block))
250 if (NULL != qe->block_proc)
251 qe->block_proc (qe->block_proc_cls,
257 if (NULL != qe->block_proc)
258 qe->block_proc (qe->block_proc_cls,
267 * Handle an incoming message of type
268 * #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
270 * @param cls the `struct GNUNET_NAMECACHE_Handle`
271 * @param msg the message we received
272 * @param size the message size
273 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
276 handle_block_cache_response (void *cls,
277 const struct BlockCacheResponseMessage *msg)
279 struct GNUNET_NAMECACHE_Handle *h = cls;
280 struct GNUNET_NAMECACHE_QueueEntry *qe;
283 LOG (GNUNET_ERROR_TYPE_DEBUG,
284 "Received BLOCK_CACHE_RESPONSE\n");
286 ntohl (msg->gns_header.r_id));
289 res = ntohl (msg->op_result);
290 /* TODO: add actual error message from namecache to response... */
291 if (NULL != qe->cont)
292 qe->cont (qe->cont_cls,
296 : _("Namecache failed to cache block"));
302 * Generic error handler, called with the appropriate error code and
303 * the same closure specified at the creation of the message queue.
304 * Not every message queue implementation supports an error handler.
306 * @param cls closure with the `struct GNUNET_NAMECACHE_Handle *`
307 * @param error error code
310 mq_error_handler (void *cls,
311 enum GNUNET_MQ_Error error)
313 struct GNUNET_NAMECACHE_Handle *h = cls;
320 * Reconnect to namecache service.
322 * @param h the handle to the NAMECACHE service
325 reconnect (struct GNUNET_NAMECACHE_Handle *h)
327 struct GNUNET_MQ_MessageHandler handlers[] = {
328 GNUNET_MQ_hd_var_size (lookup_block_response,
329 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE,
330 struct LookupBlockResponseMessage,
332 GNUNET_MQ_hd_fixed_size (block_cache_response,
333 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE,
334 struct BlockCacheResponseMessage,
336 GNUNET_MQ_handler_end ()
338 GNUNET_assert (NULL == h->mq);
339 h->mq = GNUNET_CLIENT_connect (h->cfg,
348 * Re-establish the connection to the service.
350 * @param cls handle to use to re-connect.
353 reconnect_task (void *cls)
355 struct GNUNET_NAMECACHE_Handle *h = cls;
357 h->reconnect_task = NULL;
363 * Disconnect from service and then reconnect.
365 * @param h our handle
368 force_reconnect (struct GNUNET_NAMECACHE_Handle *h)
370 struct GNUNET_NAMECACHE_QueueEntry *qe;
372 h->reconnect = GNUNET_NO;
373 GNUNET_MQ_destroy (h->mq);
375 while (NULL != (qe = h->op_head))
377 GNUNET_CONTAINER_DLL_remove (h->op_head,
380 if (NULL != qe->cont)
381 qe->cont (qe->cont_cls,
383 _("Error communicating with namecache service"));
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Reconnecting to namecache\n");
388 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
389 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
396 * Get a fresh operation id to distinguish between namecache requests
398 * @param h the namecache handle
399 * @return next operation id to use
402 get_op_id (struct GNUNET_NAMECACHE_Handle *h)
404 return h->last_op_id_used++;
409 * Initialize the connection with the NAMECACHE service.
411 * @param cfg configuration to use
412 * @return handle to the GNS service, or NULL on error
414 struct GNUNET_NAMECACHE_Handle *
415 GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
417 struct GNUNET_NAMECACHE_Handle *h;
419 h = GNUNET_new (struct GNUNET_NAMECACHE_Handle);
432 * Disconnect from the namecache service (and free associated
435 * @param h handle to the namecache
438 GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h)
440 struct GNUNET_NAMECACHE_QueueEntry *q;
442 GNUNET_break (NULL == h->op_head);
443 while (NULL != (q = h->op_head))
445 GNUNET_CONTAINER_DLL_remove (h->op_head,
452 GNUNET_MQ_destroy (h->mq);
455 if (NULL != h->reconnect_task)
457 GNUNET_SCHEDULER_cancel (h->reconnect_task);
458 h->reconnect_task = NULL;
465 * Store an item in the namecache. If the item is already present,
466 * it is replaced with the new record.
468 * @param h handle to the namecache
469 * @param block block to store
470 * @param cont continuation to call when done
471 * @param cont_cls closure for @a cont
472 * @return handle to abort the request, NULL on error
474 struct GNUNET_NAMECACHE_QueueEntry *
475 GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h,
476 const struct GNUNET_GNSRECORD_Block *block,
477 GNUNET_NAMECACHE_ContinuationWithStatus cont,
480 struct GNUNET_NAMECACHE_QueueEntry *qe;
481 struct BlockCacheMessage *msg;
482 struct GNUNET_MQ_Envelope *env;
488 blen = ntohl (block->purpose.size)
489 - sizeof (struct GNUNET_TIME_AbsoluteNBO)
490 - sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose);
492 qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
495 qe->cont_cls = cont_cls;
497 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
501 env = GNUNET_MQ_msg_extra (msg,
503 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE);
504 msg->gns_header.r_id = htonl (rid);
505 msg->expire = block->expiration_time;
506 msg->signature = block->signature;
507 msg->derived_key = block->derived_key;
508 GNUNET_memcpy (&msg[1],
511 GNUNET_MQ_send (h->mq,
518 * Get a result for a particular key from the namecache. The processor
519 * will only be called once.
521 * @param h handle to the namecache
522 * @param derived_hash hash of zone key combined with name to lookup
523 * @param proc function to call on the matching block, or with
524 * NULL if there is no matching block
525 * @param proc_cls closure for @a proc
526 * @return a handle that can be used to cancel, NULL on error
528 struct GNUNET_NAMECACHE_QueueEntry *
529 GNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h,
530 const struct GNUNET_HashCode *derived_hash,
531 GNUNET_NAMECACHE_BlockProcessor proc,
534 struct GNUNET_NAMECACHE_QueueEntry *qe;
535 struct LookupBlockMessage *msg;
536 struct GNUNET_MQ_Envelope *env;
541 LOG (GNUNET_ERROR_TYPE_DEBUG,
542 "Looking for block under %s\n",
543 GNUNET_h2s (derived_hash));
545 qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
547 qe->block_proc = proc;
548 qe->block_proc_cls = proc_cls;
550 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
553 env = GNUNET_MQ_msg (msg,
554 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK);
555 msg->gns_header.r_id = htonl (rid);
556 msg->query = *derived_hash;
557 GNUNET_MQ_send (h->mq,
564 * Cancel a namecache operation. The final callback from the
565 * operation must not have been done yet.
567 * @param qe operation to cancel
570 GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe)
572 struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
574 GNUNET_CONTAINER_DLL_remove (h->op_head,
581 /* end of namecache_api.c */