2 This file is part of GNUnet.
3 Copyright (C) 2009-2020 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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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"
37 #define LOG(kind, ...) GNUNET_log_from (kind, "gns-api", __VA_ARGS__)
40 * Default recursion depth limit to apply if
41 * the application does not specify any.
43 #define DEFAULT_LIMIT 128
46 * Handle to a lookup request
48 struct GNUNET_GNS_LookupRequest
53 struct GNUNET_GNS_LookupRequest *next;
58 struct GNUNET_GNS_LookupRequest *prev;
63 struct GNUNET_GNS_Handle *gns_handle;
66 * processor to call on lookup result
68 GNUNET_GNS_LookupResultProcessor lookup_proc;
71 * @e lookup_proc closure
76 * Envelope with the message for this queue entry.
78 struct GNUNET_MQ_Envelope *env;
88 * Reconnect to GNS service.
90 * @param handle the handle to the GNS service
93 reconnect (struct GNUNET_GNS_Handle *handle);
99 * @param cls the handle
102 reconnect_task (void *cls)
104 struct GNUNET_GNS_Handle *handle = cls;
106 handle->reconnect_task = NULL;
112 * Disconnect from service and then reconnect.
114 * @param handle our handle
117 force_reconnect (struct GNUNET_GNS_Handle *handle)
119 GNUNET_MQ_destroy (handle->mq);
121 handle->reconnect_backoff
122 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
123 handle->reconnect_task
124 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
131 * Generic error handler, called with the appropriate error code and
132 * the same closure specified at the creation of the message queue.
133 * Not every message queue implementation supports an error handler.
135 * @param cls closure with the `struct GNUNET_GNS_Handle *`
136 * @param error error code
139 mq_error_handler (void *cls,
140 enum GNUNET_MQ_Error error)
142 struct GNUNET_GNS_Handle *handle = cls;
144 LOG (GNUNET_ERROR_TYPE_WARNING,
145 "Problem with message queue. error: %i\n",
147 force_reconnect (handle);
152 * Check validity of message received from the GNS service
154 * @param cls the `struct GNUNET_GNS_Handle *`
155 * @param loookup_msg the incoming message
158 check_result (void *cls,
159 const struct LookupResultMessage *lookup_msg)
161 size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
162 uint32_t rd_count = ntohl (lookup_msg->rd_count);
163 struct GNUNET_GNSRECORD_Data rd[rd_count];
167 GNUNET_GNSRECORD_records_deserialize (mlen,
168 (const char *) &lookup_msg[1],
173 return GNUNET_SYSERR;
180 * Handler for messages received from the GNS service
182 * @param cls the `struct GNUNET_GNS_Handle *`
183 * @param loookup_msg the incoming message
186 handle_result (void *cls,
187 const struct LookupResultMessage *lookup_msg)
189 struct GNUNET_GNS_Handle *handle = cls;
190 size_t mlen = ntohs (lookup_msg->header.size) - sizeof(*lookup_msg);
191 uint32_t rd_count = ntohl (lookup_msg->rd_count);
192 struct GNUNET_GNSRECORD_Data rd[rd_count];
193 uint32_t r_id = ntohl (lookup_msg->id);
194 struct GNUNET_GNS_LookupRequest *lr;
195 GNUNET_GNS_LookupResultProcessor proc;
198 LOG (GNUNET_ERROR_TYPE_DEBUG,
199 "Received lookup reply from GNS service (%u records)\n",
200 (unsigned int) rd_count);
201 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
202 if (lr->r_id == r_id)
206 proc = lr->lookup_proc;
207 proc_cls = lr->proc_cls;
209 GNUNET_assert (GNUNET_OK ==
210 GNUNET_GNSRECORD_records_deserialize (mlen,
212 char *) &lookup_msg[1],
218 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
222 GNUNET_MQ_discard (lr->env);
228 * Reconnect to GNS service.
230 * @param handle the handle to the GNS service
233 reconnect (struct GNUNET_GNS_Handle *handle)
235 struct GNUNET_MQ_MessageHandler handlers[] = {
236 GNUNET_MQ_hd_var_size (result,
237 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
238 struct LookupResultMessage,
240 GNUNET_MQ_handler_end ()
243 GNUNET_assert (NULL == handle->mq);
244 LOG (GNUNET_ERROR_TYPE_DEBUG,
245 "Trying to connect to GNS\n");
246 handle->mq = GNUNET_CLIENT_connect (handle->cfg,
251 if (NULL == handle->mq)
253 for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
256 GNUNET_MQ_send_copy (handle->mq,
262 * Initialize the connection with the GNS service.
264 * @param cfg configuration to use
265 * @return handle to the GNS service, or NULL on error
267 struct GNUNET_GNS_Handle *
268 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
270 struct GNUNET_GNS_Handle *handle;
272 handle = GNUNET_new (struct GNUNET_GNS_Handle);
275 if (NULL == handle->mq)
277 GNUNET_free (handle);
285 * Shutdown connection with the GNS service.
287 * @param handle handle of the GNS connection to stop
290 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
292 if (NULL != handle->mq)
294 GNUNET_MQ_destroy (handle->mq);
297 if (NULL != handle->reconnect_task)
299 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
300 handle->reconnect_task = NULL;
302 GNUNET_assert (NULL == handle->lookup_head);
303 GNUNET_free (handle);
308 * Cancel pending lookup request
310 * @param lr the lookup request to cancel
311 * @return closure from the lookup result processor
314 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
316 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
319 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
322 GNUNET_MQ_discard (lr->env);
330 * Perform an asynchronous lookup operation on the GNS.
332 * @param handle handle to the GNS service
333 * @param name the name to look up (in UTF-8 encoding)
334 * @param zone zone to look in
335 * @param type the GNS record type to look for
336 * @param options local options for the lookup
337 * @param recursion_depth_limit maximum number of zones
338 * that the lookup may (still) traverse
339 * @param proc function to call on result
340 * @param proc_cls closure for @a proc
341 * @return handle to the queued request
343 struct GNUNET_GNS_LookupRequest *
344 GNUNET_GNS_lookup_limited (struct GNUNET_GNS_Handle *handle,
346 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
348 enum GNUNET_GNS_LocalOptions options,
349 uint16_t recursion_depth_limit,
350 GNUNET_GNS_LookupResultProcessor proc,
353 /* IPC to shorten gns names, return shorten_handle */
354 struct LookupMessage *lookup_msg;
355 struct GNUNET_GNS_LookupRequest *lr;
363 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "Trying to lookup `%s' in GNS\n",
366 nlen = strlen (name) + 1;
367 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
372 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
373 lr->gns_handle = handle;
374 lr->lookup_proc = proc;
375 lr->proc_cls = proc_cls;
376 lr->r_id = handle->r_id_gen++;
377 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
379 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
380 lookup_msg->id = htonl (lr->r_id);
381 lookup_msg->options = htons ((uint16_t) options);
382 lookup_msg->recursion_depth_limit
383 = htons (recursion_depth_limit);
384 lookup_msg->zone = *zone;
385 lookup_msg->type = htonl (type);
386 GNUNET_memcpy (&lookup_msg[1],
389 GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
392 if (NULL != handle->mq)
393 GNUNET_MQ_send_copy (handle->mq,
400 * Perform an asynchronous lookup operation on the GNS.
402 * @param handle handle to the GNS service
403 * @param name the name to look up (in UTF-8 encoding)
404 * @param zone the zone to start the resolution in
405 * @param type the record type to look up
406 * @param options local options for the lookup
407 * @param proc processor to call on result
408 * @param proc_cls closure for @a proc
409 * @return handle to the get request
411 struct GNUNET_GNS_LookupRequest*
412 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
414 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
416 enum GNUNET_GNS_LocalOptions options,
417 GNUNET_GNS_LookupResultProcessor proc,
420 return GNUNET_GNS_lookup_limited (handle,
430 /* end of gns_api.c */