2 This file is part of GNUnet.
3 Copyright (C) 2009-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 * @brief library to access the GNS service
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_dht_service.h"
34 #include "gnunet_gns_service.h"
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
40 * Handle to a lookup request
42 struct GNUNET_GNS_LookupRequest
48 struct GNUNET_GNS_LookupRequest *next;
53 struct GNUNET_GNS_LookupRequest *prev;
58 struct GNUNET_GNS_Handle *gns_handle;
61 * processor to call on lookup result
63 GNUNET_GNS_LookupResultProcessor lookup_proc;
66 * @e lookup_proc closure
71 * Envelope with the message for this queue entry.
73 struct GNUNET_MQ_Envelope *env;
83 * Handle to a lookup request
85 struct GNUNET_GNS_ReverseLookupRequest
91 struct GNUNET_GNS_ReverseLookupRequest *next;
96 struct GNUNET_GNS_ReverseLookupRequest *prev;
101 struct GNUNET_GNS_Handle *gns_handle;
104 * processor to call on lookup result
106 GNUNET_GNS_ReverseLookupResultProcessor lookup_proc;
109 * @e lookup_proc closure
114 * Envelope with the message for this queue entry.
116 struct GNUNET_MQ_Envelope *env;
127 * Connection to the GNS service.
129 struct GNUNET_GNS_Handle
133 * Configuration to use.
135 const struct GNUNET_CONFIGURATION_Handle *cfg;
138 * Connection to service (if available).
140 struct GNUNET_MQ_Handle *mq;
143 * Head of linked list of active lookup requests.
145 struct GNUNET_GNS_LookupRequest *lookup_head;
148 * Tail of linked list of active lookup requests.
150 struct GNUNET_GNS_LookupRequest *lookup_tail;
153 * Head of linked list of active reverse lookup requests.
155 struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_head;
158 * Tail of linked list of active reverse lookup requests.
160 struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_tail;
164 struct GNUNET_SCHEDULER_Task *reconnect_task;
167 * How long do we wait until we try to reconnect?
169 struct GNUNET_TIME_Relative reconnect_backoff;
172 * Request Id generator. Incremented by one for each request.
180 * Reconnect to GNS service.
182 * @param handle the handle to the GNS service
185 reconnect (struct GNUNET_GNS_Handle *handle);
191 * @param cls the handle
194 reconnect_task (void *cls)
196 struct GNUNET_GNS_Handle *handle = cls;
198 handle->reconnect_task = NULL;
204 * Disconnect from service and then reconnect.
206 * @param handle our handle
209 force_reconnect (struct GNUNET_GNS_Handle *handle)
211 GNUNET_MQ_destroy (handle->mq);
213 handle->reconnect_backoff
214 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
215 handle->reconnect_task
216 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
223 * Generic error handler, called with the appropriate error code and
224 * the same closure specified at the creation of the message queue.
225 * Not every message queue implementation supports an error handler.
227 * @param cls closure with the `struct GNUNET_GNS_Handle *`
228 * @param error error code
231 mq_error_handler (void *cls,
232 enum GNUNET_MQ_Error error)
234 struct GNUNET_GNS_Handle *handle = cls;
235 LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n",
237 force_reconnect (handle);
241 * Check validity of message received from the GNS service
243 * @param cls the `struct GNUNET_GNS_Handle *`
244 * @param loookup_msg the incoming message
247 check_rev_result (void *cls,
248 const struct ReverseLookupResultMessage *lookup_msg)
250 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
253 name = (char*) &lookup_msg[1];
254 if ('\0' != name[mlen-1])
257 return GNUNET_SYSERR;
264 * Handler for messages received from the GNS service
266 * @param cls the `struct GNUNET_GNS_Handle *`
267 * @param loookup_msg the incoming message
270 handle_rev_result (void *cls,
271 const struct ReverseLookupResultMessage *lookup_msg)
273 struct GNUNET_GNS_Handle *handle = cls;
275 uint32_t r_id = ntohl (lookup_msg->id);
276 struct GNUNET_GNS_ReverseLookupRequest *rlr;
277 GNUNET_GNS_ReverseLookupResultProcessor proc;
280 name = (char*)&lookup_msg[1];
281 LOG (GNUNET_ERROR_TYPE_DEBUG,
282 "Received reverse lookup reply from GNS service (%s)\n",
284 for (rlr = handle->rev_lookup_head; NULL != rlr; rlr = rlr->next)
285 if (rlr->r_id == r_id)
289 proc = rlr->lookup_proc;
290 proc_cls = rlr->proc_cls;
291 GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
292 handle->rev_lookup_tail,
302 * Check validity of message received from the GNS service
304 * @param cls the `struct GNUNET_GNS_Handle *`
305 * @param loookup_msg the incoming message
308 check_result (void *cls,
309 const struct LookupResultMessage *lookup_msg)
311 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
312 uint32_t rd_count = ntohl (lookup_msg->rd_count);
313 struct GNUNET_GNSRECORD_Data rd[rd_count];
316 GNUNET_GNSRECORD_records_deserialize (mlen,
317 (const char*) &lookup_msg[1],
322 return GNUNET_SYSERR;
329 * Handler for messages received from the GNS service
331 * @param cls the `struct GNUNET_GNS_Handle *`
332 * @param loookup_msg the incoming message
335 handle_result (void *cls,
336 const struct LookupResultMessage *lookup_msg)
338 struct GNUNET_GNS_Handle *handle = cls;
339 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
340 uint32_t rd_count = ntohl (lookup_msg->rd_count);
341 struct GNUNET_GNSRECORD_Data rd[rd_count];
342 uint32_t r_id = ntohl (lookup_msg->id);
343 struct GNUNET_GNS_LookupRequest *lr;
344 GNUNET_GNS_LookupResultProcessor proc;
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Received lookup reply from GNS service (%u records)\n",
349 (unsigned int) rd_count);
350 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
351 if (lr->r_id == r_id)
355 proc = lr->lookup_proc;
356 proc_cls = lr->proc_cls;
357 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
361 GNUNET_assert (GNUNET_OK ==
362 GNUNET_GNSRECORD_records_deserialize (mlen,
363 (const char*) &lookup_msg[1],
373 * Reconnect to GNS service.
375 * @param handle the handle to the GNS service
378 reconnect (struct GNUNET_GNS_Handle *handle)
380 struct GNUNET_MQ_MessageHandler handlers[] = {
381 GNUNET_MQ_hd_var_size (result,
382 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
383 struct LookupResultMessage,
385 GNUNET_MQ_hd_var_size (rev_result,
386 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT,
387 struct ReverseLookupResultMessage,
389 GNUNET_MQ_handler_end ()
391 struct GNUNET_GNS_LookupRequest *lh;
392 struct GNUNET_GNS_ReverseLookupRequest *rlh;
394 GNUNET_assert (NULL == handle->mq);
395 LOG (GNUNET_ERROR_TYPE_DEBUG,
396 "Trying to connect to GNS\n");
397 handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
402 if (NULL == handle->mq)
404 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
405 GNUNET_MQ_send_copy (handle->mq,
407 for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next)
408 GNUNET_MQ_send_copy (handle->mq,
414 * Initialize the connection with the GNS service.
416 * @param cfg configuration to use
417 * @return handle to the GNS service, or NULL on error
419 struct GNUNET_GNS_Handle *
420 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
422 struct GNUNET_GNS_Handle *handle;
424 handle = GNUNET_new (struct GNUNET_GNS_Handle);
427 if (NULL == handle->mq)
429 GNUNET_free (handle);
437 * Shutdown connection with the GNS service.
439 * @param handle handle of the GNS connection to stop
442 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
444 if (NULL != handle->mq)
446 GNUNET_MQ_destroy (handle->mq);
449 if (NULL != handle->reconnect_task)
451 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
452 handle->reconnect_task = NULL;
454 GNUNET_assert (NULL == handle->lookup_head);
455 GNUNET_assert (NULL == handle->rev_lookup_head);
456 GNUNET_free (handle);
461 * Cancel pending lookup request
463 * @param lr the lookup request to cancel
466 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
468 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
470 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
473 GNUNET_MQ_discard (lr->env);
478 * Cancel pending reverse lookup request
480 * @param lr the lookup request to cancel
483 GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr)
485 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
487 GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
488 handle->rev_lookup_tail,
490 GNUNET_MQ_discard (lr->env);
495 * Perform an asynchronous lookup operation on the GNS.
497 * @param handle handle to the GNS service
498 * @param name the name to look up
499 * @param zone the zone to start the resolution in
500 * @param type the record type to look up
501 * @param options local options for the lookup
502 * @param shorten_zone_key the private key of the shorten zone (can be NULL)
503 * @param proc processor to call on result
504 * @param proc_cls closure for @a proc
505 * @return handle to the get request
507 struct GNUNET_GNS_LookupRequest*
508 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
510 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
512 enum GNUNET_GNS_LocalOptions options,
513 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
514 GNUNET_GNS_LookupResultProcessor proc,
517 /* IPC to shorten gns names, return shorten_handle */
518 struct LookupMessage *lookup_msg;
519 struct GNUNET_GNS_LookupRequest *lr;
527 LOG (GNUNET_ERROR_TYPE_DEBUG,
528 "Trying to lookup `%s' in GNS\n",
530 nlen = strlen (name) + 1;
531 if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
536 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
537 lr->gns_handle = handle;
538 lr->lookup_proc = proc;
539 lr->proc_cls = proc_cls;
540 lr->r_id = handle->r_id_gen++;
541 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
543 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
544 lookup_msg->id = htonl (lr->r_id);
545 lookup_msg->options = htons ((uint16_t) options);
546 lookup_msg->zone = *zone;
547 lookup_msg->type = htonl (type);
548 if (NULL != shorten_zone_key)
550 lookup_msg->have_key = htons (GNUNET_YES);
551 lookup_msg->shorten_key = *shorten_zone_key;
553 GNUNET_memcpy (&lookup_msg[1],
556 GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
559 if (NULL != handle->mq)
560 GNUNET_MQ_send_copy (handle->mq,
566 * Perform an asynchronous reverse lookup operation on the GNS.
568 * @param handle handle to the GNS service
569 * @param zone_key zone to find a name for
570 * @param root_key our zone
571 * @param proc processor to call on result
572 * @param proc_cls closure for @a proc
573 * @return handle to the request
575 struct GNUNET_GNS_ReverseLookupRequest*
576 GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle,
577 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
578 const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key,
579 GNUNET_GNS_ReverseLookupResultProcessor proc,
582 /* IPC to shorten gns names, return shorten_handle */
583 struct ReverseLookupMessage *rev_lookup_msg;
584 struct GNUNET_GNS_ReverseLookupRequest *lr;
586 if ((NULL == zone_key) || (NULL == root_key))
591 LOG (GNUNET_ERROR_TYPE_DEBUG,
592 "Trying to reverse lookup in GNS\n");
593 lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest);
594 lr->gns_handle = handle;
595 lr->lookup_proc = proc;
596 lr->proc_cls = proc_cls;
597 lr->r_id = handle->r_id_gen++;
598 lr->env = GNUNET_MQ_msg (rev_lookup_msg,
599 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP);
600 rev_lookup_msg->id = htonl (lr->r_id);
601 rev_lookup_msg->zone_pkey = *zone_key;
602 rev_lookup_msg->root_pkey = *root_key;
603 GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head,
604 handle->rev_lookup_tail,
606 if (NULL != handle->mq)
607 GNUNET_MQ_send_copy (handle->mq,
611 /* end of gns_api.c */