Merge branch 'master' of gnunet.org:gnunet
[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      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/>.
17 */
18 /**
19  * @file gns/gns_api.c
20  * @brief library to access the GNS service
21  * @author Martin Schanzenbach
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_constants.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_dht_service.h"
31 #include "gns.h"
32 #include "gns_api.h"
33
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
36
37 /**
38  * Handle to a lookup request
39  */
40 struct GNUNET_GNS_LookupRequest
41 {
42
43   /**
44    * DLL
45    */
46   struct GNUNET_GNS_LookupRequest *next;
47
48   /**
49    * DLL
50    */
51   struct GNUNET_GNS_LookupRequest *prev;
52
53   /**
54    * handle to gns
55    */
56   struct GNUNET_GNS_Handle *gns_handle;
57
58   /**
59    * processor to call on lookup result
60    */
61   GNUNET_GNS_LookupResultProcessor lookup_proc;
62
63   /**
64    * @e lookup_proc closure
65    */
66   void *proc_cls;
67
68   /**
69    * Envelope with the message for this queue entry.
70    */
71   struct GNUNET_MQ_Envelope *env;
72
73   /**
74    * request id
75    */
76   uint32_t r_id;
77
78 };
79
80
81 /**
82  * Reconnect to GNS service.
83  *
84  * @param handle the handle to the GNS service
85  */
86 static void
87 reconnect (struct GNUNET_GNS_Handle *handle);
88
89
90 /**
91  * Reconnect to GNS
92  *
93  * @param cls the handle
94  */
95 static void
96 reconnect_task (void *cls)
97 {
98   struct GNUNET_GNS_Handle *handle = cls;
99
100   handle->reconnect_task = NULL;
101   reconnect (handle);
102 }
103
104
105 /**
106  * Disconnect from service and then reconnect.
107  *
108  * @param handle our handle
109  */
110 static void
111 force_reconnect (struct GNUNET_GNS_Handle *handle)
112 {
113   GNUNET_MQ_destroy (handle->mq);
114   handle->mq = NULL;
115   handle->reconnect_backoff
116     = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
117   handle->reconnect_task
118     = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
119                                     &reconnect_task,
120                                     handle);
121 }
122
123
124 /**
125  * Generic error handler, called with the appropriate error code and
126  * the same closure specified at the creation of the message queue.
127  * Not every message queue implementation supports an error handler.
128  *
129  * @param cls closure with the `struct GNUNET_GNS_Handle *`
130  * @param error error code
131  */
132 static void
133 mq_error_handler (void *cls,
134                   enum GNUNET_MQ_Error error)
135 {
136   struct GNUNET_GNS_Handle *handle = cls;
137
138   LOG (GNUNET_ERROR_TYPE_WARNING,
139        "Problem with message queue. error: %i\n",
140        error);
141   force_reconnect (handle);
142 }
143
144
145 /**
146  * Check validity of message received from the GNS service
147  *
148  * @param cls the `struct GNUNET_GNS_Handle *`
149  * @param loookup_msg the incoming message
150  */
151 static int
152 check_result (void *cls,
153               const struct LookupResultMessage *lookup_msg)
154 {
155   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
156   uint32_t rd_count = ntohl (lookup_msg->rd_count);
157   struct GNUNET_GNSRECORD_Data rd[rd_count];
158
159   (void) cls;
160   if (GNUNET_SYSERR ==
161       GNUNET_GNSRECORD_records_deserialize (mlen,
162                                             (const char*) &lookup_msg[1],
163                                             rd_count,
164                                             rd))
165   {
166     GNUNET_break (0);
167     return GNUNET_SYSERR;
168   }
169   return GNUNET_OK;
170 }
171
172
173 /**
174  * Handler for messages received from the GNS service
175  *
176  * @param cls the `struct GNUNET_GNS_Handle *`
177  * @param loookup_msg the incoming message
178  */
179 static void
180 handle_result (void *cls,
181                const struct LookupResultMessage *lookup_msg)
182 {
183   struct GNUNET_GNS_Handle *handle = cls;
184   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
185   uint32_t rd_count = ntohl (lookup_msg->rd_count);
186   struct GNUNET_GNSRECORD_Data rd[rd_count];
187   uint32_t r_id = ntohl (lookup_msg->id);
188   struct GNUNET_GNS_LookupRequest *lr;
189   GNUNET_GNS_LookupResultProcessor proc;
190   void *proc_cls;
191
192   LOG (GNUNET_ERROR_TYPE_DEBUG,
193        "Received lookup reply from GNS service (%u records)\n",
194        (unsigned int) rd_count);
195   for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
196     if (lr->r_id == r_id)
197       break;
198   if (NULL == lr)
199     return;
200   proc = lr->lookup_proc;
201   proc_cls = lr->proc_cls;
202
203   GNUNET_assert (GNUNET_OK ==
204                  GNUNET_GNSRECORD_records_deserialize (mlen,
205                                                        (const char*) &lookup_msg[1],
206                                                        rd_count,
207                                                        rd));
208   proc (proc_cls,
209         rd_count,
210         rd);
211   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
212                                handle->lookup_tail,
213                                lr);
214   if (NULL != lr->env)
215     GNUNET_MQ_discard (lr->env);
216   GNUNET_free (lr);
217 }
218
219
220 /**
221  * Reconnect to GNS service.
222  *
223  * @param handle the handle to the GNS service
224  */
225 static void
226 reconnect (struct GNUNET_GNS_Handle *handle)
227 {
228   struct GNUNET_MQ_MessageHandler handlers[] = {
229     GNUNET_MQ_hd_var_size (result,
230                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
231                            struct LookupResultMessage,
232                            handle),
233     GNUNET_MQ_handler_end ()
234   };
235
236   GNUNET_assert (NULL == handle->mq);
237   LOG (GNUNET_ERROR_TYPE_DEBUG,
238        "Trying to connect to GNS\n");
239   handle->mq = GNUNET_CLIENT_connect (handle->cfg,
240                                       "gns",
241                                       handlers,
242                                       &mq_error_handler,
243                                       handle);
244   if (NULL == handle->mq)
245     return;
246   for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
247        NULL != lh;
248        lh = lh->next)
249     GNUNET_MQ_send_copy (handle->mq,
250                          lh->env);
251 }
252
253
254 /**
255  * Initialize the connection with the GNS service.
256  *
257  * @param cfg configuration to use
258  * @return handle to the GNS service, or NULL on error
259  */
260 struct GNUNET_GNS_Handle *
261 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
262 {
263   struct GNUNET_GNS_Handle *handle;
264
265   handle = GNUNET_new (struct GNUNET_GNS_Handle);
266   handle->cfg = cfg;
267   reconnect (handle);
268   if (NULL == handle->mq)
269   {
270     GNUNET_free (handle);
271     return NULL;
272   }
273   return handle;
274 }
275
276
277 /**
278  * Shutdown connection with the GNS service.
279  *
280  * @param handle handle of the GNS connection to stop
281  */
282 void
283 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
284 {
285   if (NULL != handle->mq)
286   {
287     GNUNET_MQ_destroy (handle->mq);
288     handle->mq = NULL;
289   }
290   if (NULL != handle->reconnect_task)
291   {
292     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
293     handle->reconnect_task = NULL;
294   }
295   GNUNET_assert (NULL == handle->lookup_head);
296   GNUNET_free (handle);
297 }
298
299
300 /**
301  * Cancel pending lookup request
302  *
303  * @param lr the lookup request to cancel
304  * @return closure from the lookup result processor
305  */
306 void *
307 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
308 {
309   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
310   void *ret;
311
312   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
313                                handle->lookup_tail,
314                                lr);
315   GNUNET_MQ_discard (lr->env);
316   ret = lr->proc_cls;
317   GNUNET_free (lr);
318   return ret;
319 }
320
321
322 /**
323  * Perform an asynchronous lookup operation on the GNS.
324  *
325  * @param handle handle to the GNS service
326  * @param name the name to look up
327  * @param zone the zone to start the resolution in
328  * @param type the record type to look up
329  * @param options local options for the lookup
330  * @param proc processor to call on result
331  * @param proc_cls closure for @a proc
332  * @return handle to the get request
333  */
334 struct GNUNET_GNS_LookupRequest*
335 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
336                    const char *name,
337                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
338                    uint32_t type,
339                    enum GNUNET_GNS_LocalOptions options,
340                    GNUNET_GNS_LookupResultProcessor proc,
341                    void *proc_cls)
342 {
343   /* IPC to shorten gns names, return shorten_handle */
344   struct LookupMessage *lookup_msg;
345   struct GNUNET_GNS_LookupRequest *lr;
346   size_t nlen;
347
348   if (NULL == name)
349   {
350     GNUNET_break (0);
351     return NULL;
352   }
353   LOG (GNUNET_ERROR_TYPE_DEBUG,
354        "Trying to lookup `%s' in GNS\n",
355        name);
356   nlen = strlen (name) + 1;
357   if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr))
358   {
359     GNUNET_break (0);
360     return NULL;
361   }
362   lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
363   lr->gns_handle = handle;
364   lr->lookup_proc = proc;
365   lr->proc_cls = proc_cls;
366   lr->r_id = handle->r_id_gen++;
367   lr->env = GNUNET_MQ_msg_extra (lookup_msg,
368                                  nlen,
369                                  GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
370   lookup_msg->id = htonl (lr->r_id);
371   lookup_msg->options = htons ((uint16_t) options);
372   lookup_msg->zone = *zone;
373   lookup_msg->type = htonl (type);
374   GNUNET_memcpy (&lookup_msg[1],
375                  name,
376                  nlen);
377   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
378                                handle->lookup_tail,
379                                lr);
380   if (NULL != handle->mq)
381     GNUNET_MQ_send_copy (handle->mq,
382                          lr->env);
383   return lr;
384 }
385
386
387 /* end of gns_api.c */