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;
84 * Connection to the GNS service.
86 struct GNUNET_GNS_Handle
90 * Configuration to use.
92 const struct GNUNET_CONFIGURATION_Handle *cfg;
95 * Connection to service (if available).
97 struct GNUNET_MQ_Handle *mq;
100 * Head of linked list of active lookup requests.
102 struct GNUNET_GNS_LookupRequest *lookup_head;
105 * Tail of linked list of active lookup requests.
107 struct GNUNET_GNS_LookupRequest *lookup_tail;
112 struct GNUNET_SCHEDULER_Task *reconnect_task;
115 * How long do we wait until we try to reconnect?
117 struct GNUNET_TIME_Relative reconnect_backoff;
120 * Request Id generator. Incremented by one for each request.
128 * Reconnect to GNS service.
130 * @param handle the handle to the GNS service
133 reconnect (struct GNUNET_GNS_Handle *handle);
139 * @param cls the handle
142 reconnect_task (void *cls)
144 struct GNUNET_GNS_Handle *handle = cls;
146 handle->reconnect_task = NULL;
152 * Disconnect from service and then reconnect.
154 * @param handle our handle
157 force_reconnect (struct GNUNET_GNS_Handle *handle)
159 GNUNET_MQ_destroy (handle->mq);
161 handle->reconnect_backoff
162 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
163 handle->reconnect_task
164 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
171 * Generic error handler, called with the appropriate error code and
172 * the same closure specified at the creation of the message queue.
173 * Not every message queue implementation supports an error handler.
175 * @param cls closure with the `struct GNUNET_GNS_Handle *`
176 * @param error error code
179 mq_error_handler (void *cls,
180 enum GNUNET_MQ_Error error)
182 struct GNUNET_GNS_Handle *handle = cls;
184 LOG (GNUNET_ERROR_TYPE_WARNING,
185 "Problem with message queue. error: %i\n",
187 force_reconnect (handle);
192 * Check validity of message received from the GNS service
194 * @param cls the `struct GNUNET_GNS_Handle *`
195 * @param loookup_msg the incoming message
198 check_result (void *cls,
199 const struct LookupResultMessage *lookup_msg)
201 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
202 uint32_t rd_count = ntohl (lookup_msg->rd_count);
203 struct GNUNET_GNSRECORD_Data rd[rd_count];
206 GNUNET_GNSRECORD_records_deserialize (mlen,
207 (const char*) &lookup_msg[1],
212 return GNUNET_SYSERR;
219 * Handler for messages received from the GNS service
221 * @param cls the `struct GNUNET_GNS_Handle *`
222 * @param loookup_msg the incoming message
225 handle_result (void *cls,
226 const struct LookupResultMessage *lookup_msg)
228 struct GNUNET_GNS_Handle *handle = cls;
229 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
230 uint32_t rd_count = ntohl (lookup_msg->rd_count);
231 struct GNUNET_GNSRECORD_Data rd[rd_count];
232 uint32_t r_id = ntohl (lookup_msg->id);
233 struct GNUNET_GNS_LookupRequest *lr;
234 GNUNET_GNS_LookupResultProcessor proc;
237 LOG (GNUNET_ERROR_TYPE_DEBUG,
238 "Received lookup reply from GNS service (%u records)\n",
239 (unsigned int) rd_count);
240 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
241 if (lr->r_id == r_id)
245 proc = lr->lookup_proc;
246 proc_cls = lr->proc_cls;
247 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
251 GNUNET_assert (GNUNET_OK ==
252 GNUNET_GNSRECORD_records_deserialize (mlen,
253 (const char*) &lookup_msg[1],
263 * Reconnect to GNS service.
265 * @param handle the handle to the GNS service
268 reconnect (struct GNUNET_GNS_Handle *handle)
270 struct GNUNET_MQ_MessageHandler handlers[] = {
271 GNUNET_MQ_hd_var_size (result,
272 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
273 struct LookupResultMessage,
275 GNUNET_MQ_handler_end ()
277 struct GNUNET_GNS_LookupRequest *lh;
279 GNUNET_assert (NULL == handle->mq);
280 LOG (GNUNET_ERROR_TYPE_DEBUG,
281 "Trying to connect to GNS\n");
282 handle->mq = GNUNET_CLIENT_connect (handle->cfg,
287 if (NULL == handle->mq)
289 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
290 GNUNET_MQ_send_copy (handle->mq,
296 * Initialize the connection with the GNS service.
298 * @param cfg configuration to use
299 * @return handle to the GNS service, or NULL on error
301 struct GNUNET_GNS_Handle *
302 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
304 struct GNUNET_GNS_Handle *handle;
306 handle = GNUNET_new (struct GNUNET_GNS_Handle);
309 if (NULL == handle->mq)
311 GNUNET_free (handle);
319 * Shutdown connection with the GNS service.
321 * @param handle handle of the GNS connection to stop
324 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
326 if (NULL != handle->mq)
328 GNUNET_MQ_destroy (handle->mq);
331 if (NULL != handle->reconnect_task)
333 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
334 handle->reconnect_task = NULL;
336 GNUNET_assert (NULL == handle->lookup_head);
337 GNUNET_free (handle);
342 * Cancel pending lookup request
344 * @param lr the lookup request to cancel
347 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
349 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
351 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
354 GNUNET_MQ_discard (lr->env);
360 * Perform an asynchronous lookup operation on the GNS.
362 * @param handle handle to the GNS service
363 * @param name the name to look up
364 * @param zone the zone to start the resolution in
365 * @param type the record type to look up
366 * @param options local options for the lookup
367 * @param proc processor to call on result
368 * @param proc_cls closure for @a proc
369 * @return handle to the get request
371 struct GNUNET_GNS_LookupRequest*
372 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
374 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
376 enum GNUNET_GNS_LocalOptions options,
377 GNUNET_GNS_LookupResultProcessor proc,
380 /* IPC to shorten gns names, return shorten_handle */
381 struct LookupMessage *lookup_msg;
382 struct GNUNET_GNS_LookupRequest *lr;
390 LOG (GNUNET_ERROR_TYPE_DEBUG,
391 "Trying to lookup `%s' in GNS\n",
393 nlen = strlen (name) + 1;
394 if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
399 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
400 lr->gns_handle = handle;
401 lr->lookup_proc = proc;
402 lr->proc_cls = proc_cls;
403 lr->r_id = handle->r_id_gen++;
404 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
406 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
407 lookup_msg->id = htonl (lr->r_id);
408 lookup_msg->options = htons ((uint16_t) options);
409 lookup_msg->zone = *zone;
410 lookup_msg->type = htonl (type);
411 GNUNET_memcpy (&lookup_msg[1],
414 GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
417 if (NULL != handle->mq)
418 GNUNET_MQ_send_copy (handle->mq,
423 /* end of gns_api.c */