glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / gns / gns_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016, 2018 GNUnet e.V.
4
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.
9
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.
14 */
15 /**
16  * @file gns/gns_api.c
17  * @brief library to access the GNS service
18  * @author Martin Schanzenbach
19  * @author Christian Grothoff
20  */
21 #include "platform.h"
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"
28 #include "gns.h"
29 #include "gns_api.h"
30
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
33
34 /**
35  * Handle to a lookup request
36  */
37 struct GNUNET_GNS_LookupRequest
38 {
39
40   /**
41    * DLL
42    */
43   struct GNUNET_GNS_LookupRequest *next;
44
45   /**
46    * DLL
47    */
48   struct GNUNET_GNS_LookupRequest *prev;
49
50   /**
51    * handle to gns
52    */
53   struct GNUNET_GNS_Handle *gns_handle;
54
55   /**
56    * processor to call on lookup result
57    */
58   GNUNET_GNS_LookupResultProcessor lookup_proc;
59
60   /**
61    * @e lookup_proc closure
62    */
63   void *proc_cls;
64
65   /**
66    * Envelope with the message for this queue entry.
67    */
68   struct GNUNET_MQ_Envelope *env;
69
70   /**
71    * request id
72    */
73   uint32_t r_id;
74
75 };
76
77
78 /**
79  * Reconnect to GNS service.
80  *
81  * @param handle the handle to the GNS service
82  */
83 static void
84 reconnect (struct GNUNET_GNS_Handle *handle);
85
86
87 /**
88  * Reconnect to GNS
89  *
90  * @param cls the handle
91  */
92 static void
93 reconnect_task (void *cls)
94 {
95   struct GNUNET_GNS_Handle *handle = cls;
96
97   handle->reconnect_task = NULL;
98   reconnect (handle);
99 }
100
101
102 /**
103  * Disconnect from service and then reconnect.
104  *
105  * @param handle our handle
106  */
107 static void
108 force_reconnect (struct GNUNET_GNS_Handle *handle)
109 {
110   GNUNET_MQ_destroy (handle->mq);
111   handle->mq = NULL;
112   handle->reconnect_backoff
113     = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
114   handle->reconnect_task
115     = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
116                                     &reconnect_task,
117                                     handle);
118 }
119
120
121 /**
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.
125  *
126  * @param cls closure with the `struct GNUNET_GNS_Handle *`
127  * @param error error code
128  */
129 static void
130 mq_error_handler (void *cls,
131                   enum GNUNET_MQ_Error error)
132 {
133   struct GNUNET_GNS_Handle *handle = cls;
134
135   LOG (GNUNET_ERROR_TYPE_WARNING,
136        "Problem with message queue. error: %i\n",
137        error);
138   force_reconnect (handle);
139 }
140
141
142 /**
143  * Check validity of message received from the GNS service
144  *
145  * @param cls the `struct GNUNET_GNS_Handle *`
146  * @param loookup_msg the incoming message
147  */
148 static int
149 check_result (void *cls,
150               const struct LookupResultMessage *lookup_msg)
151 {
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];
155
156   (void) cls;
157   if (GNUNET_SYSERR ==
158       GNUNET_GNSRECORD_records_deserialize (mlen,
159                                             (const char*) &lookup_msg[1],
160                                             rd_count,
161                                             rd))
162   {
163     GNUNET_break (0);
164     return GNUNET_SYSERR;
165   }
166   return GNUNET_OK;
167 }
168
169
170 /**
171  * Handler for messages received from the GNS service
172  *
173  * @param cls the `struct GNUNET_GNS_Handle *`
174  * @param loookup_msg the incoming message
175  */
176 static void
177 handle_result (void *cls,
178                const struct LookupResultMessage *lookup_msg)
179 {
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;
187   void *proc_cls;
188
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)
194       break;
195   if (NULL == lr)
196     return;
197   proc = lr->lookup_proc;
198   proc_cls = lr->proc_cls;
199
200   GNUNET_assert (GNUNET_OK ==
201                  GNUNET_GNSRECORD_records_deserialize (mlen,
202                                                        (const char*) &lookup_msg[1],
203                                                        rd_count,
204                                                        rd));
205   proc (proc_cls,
206         rd_count,
207         rd);
208   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
209                                handle->lookup_tail,
210                                lr);
211   if (NULL != lr->env)
212     GNUNET_MQ_discard (lr->env);
213   GNUNET_free (lr);
214 }
215
216
217 /**
218  * Reconnect to GNS service.
219  *
220  * @param handle the handle to the GNS service
221  */
222 static void
223 reconnect (struct GNUNET_GNS_Handle *handle)
224 {
225   struct GNUNET_MQ_MessageHandler handlers[] = {
226     GNUNET_MQ_hd_var_size (result,
227                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
228                            struct LookupResultMessage,
229                            handle),
230     GNUNET_MQ_handler_end ()
231   };
232   struct GNUNET_GNS_LookupRequest *lh;
233
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,
238                                       "gns",
239                                       handlers,
240                                       &mq_error_handler,
241                                       handle);
242   if (NULL == handle->mq)
243     return;
244   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
245     GNUNET_MQ_send_copy (handle->mq,
246                          lh->env);
247 }
248
249
250 /**
251  * Initialize the connection with the GNS service.
252  *
253  * @param cfg configuration to use
254  * @return handle to the GNS service, or NULL on error
255  */
256 struct GNUNET_GNS_Handle *
257 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
258 {
259   struct GNUNET_GNS_Handle *handle;
260
261   handle = GNUNET_new (struct GNUNET_GNS_Handle);
262   handle->cfg = cfg;
263   reconnect (handle);
264   if (NULL == handle->mq)
265   {
266     GNUNET_free (handle);
267     return NULL;
268   }
269   return handle;
270 }
271
272
273 /**
274  * Shutdown connection with the GNS service.
275  *
276  * @param handle handle of the GNS connection to stop
277  */
278 void
279 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
280 {
281   if (NULL != handle->mq)
282   {
283     GNUNET_MQ_destroy (handle->mq);
284     handle->mq = NULL;
285   }
286   if (NULL != handle->reconnect_task)
287   {
288     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
289     handle->reconnect_task = NULL;
290   }
291   GNUNET_assert (NULL == handle->lookup_head);
292   GNUNET_free (handle);
293 }
294
295
296 /**
297  * Cancel pending lookup request
298  *
299  * @param lr the lookup request to cancel
300  */
301 void
302 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
303 {
304   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
305
306   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
307                                handle->lookup_tail,
308                                lr);
309   GNUNET_MQ_discard (lr->env);
310   GNUNET_free (lr);
311 }
312
313
314 /**
315  * Perform an asynchronous lookup operation on the GNS.
316  *
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
325  */
326 struct GNUNET_GNS_LookupRequest*
327 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
328                    const char *name,
329                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
330                    uint32_t type,
331                    enum GNUNET_GNS_LocalOptions options,
332                    GNUNET_GNS_LookupResultProcessor proc,
333                    void *proc_cls)
334 {
335   /* IPC to shorten gns names, return shorten_handle */
336   struct LookupMessage *lookup_msg;
337   struct GNUNET_GNS_LookupRequest *lr;
338   size_t nlen;
339
340   if (NULL == name)
341   {
342     GNUNET_break (0);
343     return NULL;
344   }
345   LOG (GNUNET_ERROR_TYPE_DEBUG,
346        "Trying to lookup `%s' in GNS\n",
347        name);
348   nlen = strlen (name) + 1;
349   if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr))
350   {
351     GNUNET_break (0);
352     return NULL;
353   }
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,
360                                  nlen,
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],
367                  name,
368                  nlen);
369   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
370                                handle->lookup_tail,
371                                lr);
372   if (NULL != handle->mq)
373     GNUNET_MQ_send_copy (handle->mq,
374                          lr->env);
375   return lr;
376 }
377
378
379 /* end of gns_api.c */