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;
358 GNUNET_assert (GNUNET_OK ==
359 GNUNET_GNSRECORD_records_deserialize (mlen,
360 (const char*) &lookup_msg[1],
366 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
370 GNUNET_MQ_discard (lr->env);
376 * Reconnect to GNS service.
378 * @param handle the handle to the GNS service
381 reconnect (struct GNUNET_GNS_Handle *handle)
383 struct GNUNET_MQ_MessageHandler handlers[] = {
384 GNUNET_MQ_hd_var_size (result,
385 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
386 struct LookupResultMessage,
388 GNUNET_MQ_hd_var_size (rev_result,
389 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT,
390 struct ReverseLookupResultMessage,
392 GNUNET_MQ_handler_end ()
394 struct GNUNET_GNS_LookupRequest *lh;
395 struct GNUNET_GNS_ReverseLookupRequest *rlh;
397 GNUNET_assert (NULL == handle->mq);
398 LOG (GNUNET_ERROR_TYPE_DEBUG,
399 "Trying to connect to GNS\n");
400 handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
405 if (NULL == handle->mq)
407 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
408 GNUNET_MQ_send_copy (handle->mq,
410 for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next)
411 GNUNET_MQ_send_copy (handle->mq,
417 * Initialize the connection with the GNS service.
419 * @param cfg configuration to use
420 * @return handle to the GNS service, or NULL on error
422 struct GNUNET_GNS_Handle *
423 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
425 struct GNUNET_GNS_Handle *handle;
427 handle = GNUNET_new (struct GNUNET_GNS_Handle);
430 if (NULL == handle->mq)
432 GNUNET_free (handle);
440 * Shutdown connection with the GNS service.
442 * @param handle handle of the GNS connection to stop
445 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
447 if (NULL != handle->mq)
449 GNUNET_MQ_destroy (handle->mq);
452 if (NULL != handle->reconnect_task)
454 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
455 handle->reconnect_task = NULL;
457 GNUNET_assert (NULL == handle->lookup_head);
458 GNUNET_assert (NULL == handle->rev_lookup_head);
459 GNUNET_free (handle);
464 * Cancel pending lookup request
466 * @param lr the lookup request to cancel
469 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
471 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
473 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
476 GNUNET_MQ_discard (lr->env);
481 * Cancel pending reverse lookup request
483 * @param lr the lookup request to cancel
486 GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr)
488 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
490 GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
491 handle->rev_lookup_tail,
493 GNUNET_MQ_discard (lr->env);
498 * Perform an asynchronous lookup operation on the GNS.
500 * @param handle handle to the GNS service
501 * @param name the name to look up
502 * @param zone the zone to start the resolution in
503 * @param type the record type to look up
504 * @param options local options for the lookup
505 * @param shorten_zone_key the private key of the shorten zone (can be NULL)
506 * @param proc processor to call on result
507 * @param proc_cls closure for @a proc
508 * @return handle to the get request
510 struct GNUNET_GNS_LookupRequest*
511 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
513 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
515 enum GNUNET_GNS_LocalOptions options,
516 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
517 GNUNET_GNS_LookupResultProcessor proc,
520 /* IPC to shorten gns names, return shorten_handle */
521 struct LookupMessage *lookup_msg;
522 struct GNUNET_GNS_LookupRequest *lr;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Trying to lookup `%s' in GNS\n",
533 nlen = strlen (name) + 1;
534 if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
539 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
540 lr->gns_handle = handle;
541 lr->lookup_proc = proc;
542 lr->proc_cls = proc_cls;
543 lr->r_id = handle->r_id_gen++;
544 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
546 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
547 lookup_msg->id = htonl (lr->r_id);
548 lookup_msg->options = htons ((uint16_t) options);
549 lookup_msg->zone = *zone;
550 lookup_msg->type = htonl (type);
551 if (NULL != shorten_zone_key)
553 lookup_msg->have_key = htons (GNUNET_YES);
554 lookup_msg->shorten_key = *shorten_zone_key;
556 GNUNET_memcpy (&lookup_msg[1],
559 GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
562 if (NULL != handle->mq)
563 GNUNET_MQ_send_copy (handle->mq,
569 * Perform an asynchronous reverse lookup operation on the GNS.
571 * @param handle handle to the GNS service
572 * @param zone_key zone to find a name for
573 * @param root_key our zone
574 * @param proc processor to call on result
575 * @param proc_cls closure for @a proc
576 * @return handle to the request
578 struct GNUNET_GNS_ReverseLookupRequest*
579 GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle,
580 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
581 const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key,
582 GNUNET_GNS_ReverseLookupResultProcessor proc,
585 /* IPC to shorten gns names, return shorten_handle */
586 struct ReverseLookupMessage *rev_lookup_msg;
587 struct GNUNET_GNS_ReverseLookupRequest *lr;
589 if ((NULL == zone_key) || (NULL == root_key))
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Trying to reverse lookup in GNS\n");
596 lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest);
597 lr->gns_handle = handle;
598 lr->lookup_proc = proc;
599 lr->proc_cls = proc_cls;
600 lr->r_id = handle->r_id_gen++;
601 lr->env = GNUNET_MQ_msg (rev_lookup_msg,
602 GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP);
603 rev_lookup_msg->id = htonl (lr->r_id);
604 rev_lookup_msg->zone_pkey = *zone_key;
605 rev_lookup_msg->root_pkey = *root_key;
606 GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head,
607 handle->rev_lookup_tail,
609 if (NULL != handle->mq)
610 GNUNET_MQ_send_copy (handle->mq,
614 /* end of gns_api.c */