2 This file is part of GNUnet.
3 Copyright (C) 2009-2013, 2016, 2018 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.
17 * @brief library to access the GNS service
18 * @author Martin Schanzenbach
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_constants.h"
24 #include "gnunet_arm_service.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_dht_service.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
35 * Handle to a lookup request
37 struct GNUNET_GNS_LookupRequest
43 struct GNUNET_GNS_LookupRequest *next;
48 struct GNUNET_GNS_LookupRequest *prev;
53 struct GNUNET_GNS_Handle *gns_handle;
56 * processor to call on lookup result
58 GNUNET_GNS_LookupResultProcessor lookup_proc;
61 * @e lookup_proc closure
66 * Envelope with the message for this queue entry.
68 struct GNUNET_MQ_Envelope *env;
79 * Reconnect to GNS service.
81 * @param handle the handle to the GNS service
84 reconnect (struct GNUNET_GNS_Handle *handle);
90 * @param cls the handle
93 reconnect_task (void *cls)
95 struct GNUNET_GNS_Handle *handle = cls;
97 handle->reconnect_task = NULL;
103 * Disconnect from service and then reconnect.
105 * @param handle our handle
108 force_reconnect (struct GNUNET_GNS_Handle *handle)
110 GNUNET_MQ_destroy (handle->mq);
112 handle->reconnect_backoff
113 = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
114 handle->reconnect_task
115 = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
122 * Generic error handler, called with the appropriate error code and
123 * the same closure specified at the creation of the message queue.
124 * Not every message queue implementation supports an error handler.
126 * @param cls closure with the `struct GNUNET_GNS_Handle *`
127 * @param error error code
130 mq_error_handler (void *cls,
131 enum GNUNET_MQ_Error error)
133 struct GNUNET_GNS_Handle *handle = cls;
135 LOG (GNUNET_ERROR_TYPE_WARNING,
136 "Problem with message queue. error: %i\n",
138 force_reconnect (handle);
143 * Check validity of message received from the GNS service
145 * @param cls the `struct GNUNET_GNS_Handle *`
146 * @param loookup_msg the incoming message
149 check_result (void *cls,
150 const struct LookupResultMessage *lookup_msg)
152 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
153 uint32_t rd_count = ntohl (lookup_msg->rd_count);
154 struct GNUNET_GNSRECORD_Data rd[rd_count];
158 GNUNET_GNSRECORD_records_deserialize (mlen,
159 (const char*) &lookup_msg[1],
164 return GNUNET_SYSERR;
171 * Handler for messages received from the GNS service
173 * @param cls the `struct GNUNET_GNS_Handle *`
174 * @param loookup_msg the incoming message
177 handle_result (void *cls,
178 const struct LookupResultMessage *lookup_msg)
180 struct GNUNET_GNS_Handle *handle = cls;
181 size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
182 uint32_t rd_count = ntohl (lookup_msg->rd_count);
183 struct GNUNET_GNSRECORD_Data rd[rd_count];
184 uint32_t r_id = ntohl (lookup_msg->id);
185 struct GNUNET_GNS_LookupRequest *lr;
186 GNUNET_GNS_LookupResultProcessor proc;
189 LOG (GNUNET_ERROR_TYPE_DEBUG,
190 "Received lookup reply from GNS service (%u records)\n",
191 (unsigned int) rd_count);
192 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
193 if (lr->r_id == r_id)
197 proc = lr->lookup_proc;
198 proc_cls = lr->proc_cls;
200 GNUNET_assert (GNUNET_OK ==
201 GNUNET_GNSRECORD_records_deserialize (mlen,
202 (const char*) &lookup_msg[1],
208 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
212 GNUNET_MQ_discard (lr->env);
218 * Reconnect to GNS service.
220 * @param handle the handle to the GNS service
223 reconnect (struct GNUNET_GNS_Handle *handle)
225 struct GNUNET_MQ_MessageHandler handlers[] = {
226 GNUNET_MQ_hd_var_size (result,
227 GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
228 struct LookupResultMessage,
230 GNUNET_MQ_handler_end ()
232 struct GNUNET_GNS_LookupRequest *lh;
234 GNUNET_assert (NULL == handle->mq);
235 LOG (GNUNET_ERROR_TYPE_DEBUG,
236 "Trying to connect to GNS\n");
237 handle->mq = GNUNET_CLIENT_connect (handle->cfg,
242 if (NULL == handle->mq)
244 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
245 GNUNET_MQ_send_copy (handle->mq,
251 * Initialize the connection with the GNS service.
253 * @param cfg configuration to use
254 * @return handle to the GNS service, or NULL on error
256 struct GNUNET_GNS_Handle *
257 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
259 struct GNUNET_GNS_Handle *handle;
261 handle = GNUNET_new (struct GNUNET_GNS_Handle);
264 if (NULL == handle->mq)
266 GNUNET_free (handle);
274 * Shutdown connection with the GNS service.
276 * @param handle handle of the GNS connection to stop
279 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
281 if (NULL != handle->mq)
283 GNUNET_MQ_destroy (handle->mq);
286 if (NULL != handle->reconnect_task)
288 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
289 handle->reconnect_task = NULL;
291 GNUNET_assert (NULL == handle->lookup_head);
292 GNUNET_free (handle);
297 * Cancel pending lookup request
299 * @param lr the lookup request to cancel
302 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
304 struct GNUNET_GNS_Handle *handle = lr->gns_handle;
306 GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
309 GNUNET_MQ_discard (lr->env);
315 * Perform an asynchronous lookup operation on the GNS.
317 * @param handle handle to the GNS service
318 * @param name the name to look up
319 * @param zone the zone to start the resolution in
320 * @param type the record type to look up
321 * @param options local options for the lookup
322 * @param proc processor to call on result
323 * @param proc_cls closure for @a proc
324 * @return handle to the get request
326 struct GNUNET_GNS_LookupRequest*
327 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
329 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
331 enum GNUNET_GNS_LocalOptions options,
332 GNUNET_GNS_LookupResultProcessor proc,
335 /* IPC to shorten gns names, return shorten_handle */
336 struct LookupMessage *lookup_msg;
337 struct GNUNET_GNS_LookupRequest *lr;
345 LOG (GNUNET_ERROR_TYPE_DEBUG,
346 "Trying to lookup `%s' in GNS\n",
348 nlen = strlen (name) + 1;
349 if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr))
354 lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
355 lr->gns_handle = handle;
356 lr->lookup_proc = proc;
357 lr->proc_cls = proc_cls;
358 lr->r_id = handle->r_id_gen++;
359 lr->env = GNUNET_MQ_msg_extra (lookup_msg,
361 GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
362 lookup_msg->id = htonl (lr->r_id);
363 lookup_msg->options = htons ((uint16_t) options);
364 lookup_msg->zone = *zone;
365 lookup_msg->type = htonl (type);
366 GNUNET_memcpy (&lookup_msg[1],
369 GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
372 if (NULL != handle->mq)
373 GNUNET_MQ_send_copy (handle->mq,
379 /* end of gns_api.c */