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 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
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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 <http://www.gnu.org/licenses/>.
20 * @file namecache/namecache_api.c
21 * @brief API to access the NAMECACHE service
22 * @author Martin Schanzenbach
23 * @author Matthias Wachs
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_crypto_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_gnsrecord_lib.h"
33 #include "gnunet_signatures.h"
34 #include "gnunet_namecache_service.h"
35 #include "namecache.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "namecache-api",__VA_ARGS__)
42 * An QueueEntry used to store information for a pending
43 * NAMECACHE record operation
45 struct GNUNET_NAMECACHE_QueueEntry
51 struct GNUNET_NAMECACHE_QueueEntry *next;
56 struct GNUNET_NAMECACHE_QueueEntry *prev;
59 * Main handle to access the namecache.
61 struct GNUNET_NAMECACHE_Handle *nsh;
64 * Continuation to call
66 GNUNET_NAMECACHE_ContinuationWithStatus cont;
69 * Closure for @e cont.
74 * Function to call with the blocks we get back; or NULL.
76 GNUNET_NAMECACHE_BlockProcessor block_proc;
79 * Closure for @e block_proc.
84 * The operation id this zone iteration operation has
92 * Connection to the NAMECACHE service.
94 struct GNUNET_NAMECACHE_Handle
98 * Configuration to use.
100 const struct GNUNET_CONFIGURATION_Handle *cfg;
103 * Message queue to service.
105 struct GNUNET_MQ_Handle *mq;
108 * Currently pending transmission request (or NULL).
110 struct GNUNET_CLIENT_TransmitHandle *th;
113 * Head of pending namecache queue entries
115 struct GNUNET_NAMECACHE_QueueEntry *op_head;
118 * Tail of pending namecache queue entries
120 struct GNUNET_NAMECACHE_QueueEntry *op_tail;
125 struct GNUNET_SCHEDULER_Task *reconnect_task;
128 * Delay introduced before we reconnect.
130 struct GNUNET_TIME_Relative reconnect_delay;
133 * Should we reconnect to service due to some serious error?
138 * The last operation id used for a NAMECACHE operation
140 uint32_t last_op_id_used;
146 * Disconnect from service and then reconnect.
148 * @param h our handle
151 force_reconnect (struct GNUNET_NAMECACHE_Handle *h);
155 * Find queue entry for the given @a rid.
157 * @param h handle to search
158 * @param rid request ID to look for
159 * @return NULL if not found, otherwise the queue entry (removed from the queue)
161 static struct GNUNET_NAMECACHE_QueueEntry *
162 find_qe (struct GNUNET_NAMECACHE_Handle *h,
165 struct GNUNET_NAMECACHE_QueueEntry *qe;
167 for (qe = h->op_head; qe != NULL; qe = qe->next)
169 if (qe->op_id == rid)
171 GNUNET_CONTAINER_DLL_remove (h->op_head,
182 * Handle an incoming message of type
183 * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
185 * @param cls the `struct GNUNET_NAMECACHE_Handle`
186 * @param msg the message we received
189 check_lookup_block_response (void *cls,
190 const struct LookupBlockResponseMessage *msg)
192 /* any length will do, format validation is in handler */
198 * Handle an incoming message of type
199 * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
201 * @param cls the `struct GNUNET_NAMECACHE_Handle`
202 * @param msg the message we received
205 handle_lookup_block_response (void *cls,
206 const struct LookupBlockResponseMessage *msg)
208 struct GNUNET_NAMECACHE_Handle *h = cls;
210 struct GNUNET_NAMECACHE_QueueEntry *qe;
212 LOG (GNUNET_ERROR_TYPE_DEBUG,
213 "Received LOOKUP_BLOCK_RESPONSE\n");
215 ntohl (msg->gns_header.r_id));
218 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
221 if (NULL != qe->block_proc)
222 qe->block_proc (qe->block_proc_cls,
227 size = ntohs (msg->gns_header.header.size)
228 - sizeof (struct LookupBlockResponseMessage);
230 char buf[size + sizeof (struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN;
231 struct GNUNET_GNSRECORD_Block *block;
233 block = (struct GNUNET_GNSRECORD_Block *) buf;
234 block->signature = msg->signature;
235 block->derived_key = msg->derived_key;
236 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
237 block->purpose.size = htonl (size +
238 sizeof (struct GNUNET_TIME_AbsoluteNBO) +
239 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose));
240 block->expiration_time = msg->expire;
241 GNUNET_memcpy (&block[1],
245 GNUNET_GNSRECORD_block_verify (block))
248 if (NULL != qe->block_proc)
249 qe->block_proc (qe->block_proc_cls,
255 if (NULL != qe->block_proc)
256 qe->block_proc (qe->block_proc_cls,
265 * Handle an incoming message of type
266 * #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
268 * @param cls the `struct GNUNET_NAMECACHE_Handle`
269 * @param msg the message we received
270 * @param size the message size
271 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
274 handle_block_cache_response (void *cls,
275 const struct BlockCacheResponseMessage *msg)
277 struct GNUNET_NAMECACHE_Handle *h = cls;
278 struct GNUNET_NAMECACHE_QueueEntry *qe;
281 LOG (GNUNET_ERROR_TYPE_DEBUG,
282 "Received BLOCK_CACHE_RESPONSE\n");
284 ntohl (msg->gns_header.r_id));
287 res = ntohl (msg->op_result);
288 /* TODO: add actual error message from namecache to response... */
289 if (NULL != qe->cont)
290 qe->cont (qe->cont_cls,
294 : _("Namecache failed to cache block"));
300 * Generic error handler, called with the appropriate error code and
301 * the same closure specified at the creation of the message queue.
302 * Not every message queue implementation supports an error handler.
304 * @param cls closure with the `struct GNUNET_NAMECACHE_Handle *`
305 * @param error error code
308 mq_error_handler (void *cls,
309 enum GNUNET_MQ_Error error)
311 struct GNUNET_NAMECACHE_Handle *h = cls;
318 * Reconnect to namecache service.
320 * @param h the handle to the NAMECACHE service
323 reconnect (struct GNUNET_NAMECACHE_Handle *h)
325 struct GNUNET_MQ_MessageHandler handlers[] = {
326 GNUNET_MQ_hd_var_size (lookup_block_response,
327 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE,
328 struct LookupBlockResponseMessage,
330 GNUNET_MQ_hd_fixed_size (block_cache_response,
331 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE,
332 struct BlockCacheResponseMessage,
334 GNUNET_MQ_handler_end ()
336 GNUNET_assert (NULL == h->mq);
337 h->mq = GNUNET_CLIENT_connect (h->cfg,
346 * Re-establish the connection to the service.
348 * @param cls handle to use to re-connect.
351 reconnect_task (void *cls)
353 struct GNUNET_NAMECACHE_Handle *h = cls;
355 h->reconnect_task = NULL;
361 * Disconnect from service and then reconnect.
363 * @param h our handle
366 force_reconnect (struct GNUNET_NAMECACHE_Handle *h)
368 struct GNUNET_NAMECACHE_QueueEntry *qe;
370 h->reconnect = GNUNET_NO;
371 GNUNET_MQ_destroy (h->mq);
373 while (NULL != (qe = h->op_head))
375 GNUNET_CONTAINER_DLL_remove (h->op_head,
378 if (NULL != qe->cont)
379 qe->cont (qe->cont_cls,
381 _("Error communicating with namecache service"));
384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385 "Reconnecting to namecache\n");
386 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
387 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
394 * Get a fresh operation id to distinguish between namecache requests
396 * @param h the namecache handle
397 * @return next operation id to use
400 get_op_id (struct GNUNET_NAMECACHE_Handle *h)
402 return h->last_op_id_used++;
407 * Initialize the connection with the NAMECACHE service.
409 * @param cfg configuration to use
410 * @return handle to the GNS service, or NULL on error
412 struct GNUNET_NAMECACHE_Handle *
413 GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
415 struct GNUNET_NAMECACHE_Handle *h;
417 h = GNUNET_new (struct GNUNET_NAMECACHE_Handle);
430 * Disconnect from the namecache service (and free associated
433 * @param h handle to the namecache
436 GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h)
438 struct GNUNET_NAMECACHE_QueueEntry *q;
440 GNUNET_break (NULL == h->op_head);
441 while (NULL != (q = h->op_head))
443 GNUNET_CONTAINER_DLL_remove (h->op_head,
450 GNUNET_MQ_destroy (h->mq);
453 if (NULL != h->reconnect_task)
455 GNUNET_SCHEDULER_cancel (h->reconnect_task);
456 h->reconnect_task = NULL;
463 * Store an item in the namecache. If the item is already present,
464 * it is replaced with the new record.
466 * @param h handle to the namecache
467 * @param block block to store
468 * @param cont continuation to call when done
469 * @param cont_cls closure for @a cont
470 * @return handle to abort the request, NULL on error
472 struct GNUNET_NAMECACHE_QueueEntry *
473 GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h,
474 const struct GNUNET_GNSRECORD_Block *block,
475 GNUNET_NAMECACHE_ContinuationWithStatus cont,
478 struct GNUNET_NAMECACHE_QueueEntry *qe;
479 struct BlockCacheMessage *msg;
480 struct GNUNET_MQ_Envelope *env;
486 blen = ntohl (block->purpose.size);
487 GNUNET_assert (blen > (sizeof (struct GNUNET_TIME_AbsoluteNBO) +
488 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)));
489 blen -= (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 */