More API function tests...
[oweals/gnunet.git] / src / gns / gns_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013, 2016 GNUnet e.V.
4
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.
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      General Public License for more details.
14
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.
19 */
20 /**
21  * @file gns/gns_api.c
22  * @brief library to access the GNS service
23  * @author Martin Schanzenbach
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
33 #include "gns.h"
34 #include "gnunet_gns_service.h"
35
36
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
38
39 /**
40  * Handle to a lookup request
41  */
42 struct GNUNET_GNS_LookupRequest
43 {
44
45   /**
46    * DLL
47    */
48   struct GNUNET_GNS_LookupRequest *next;
49
50   /**
51    * DLL
52    */
53   struct GNUNET_GNS_LookupRequest *prev;
54
55   /**
56    * handle to gns
57    */
58   struct GNUNET_GNS_Handle *gns_handle;
59
60   /**
61    * processor to call on lookup result
62    */
63   GNUNET_GNS_LookupResultProcessor lookup_proc;
64
65   /**
66    * @e lookup_proc closure
67    */
68   void *proc_cls;
69
70   /**
71    * Envelope with the message for this queue entry.
72    */
73   struct GNUNET_MQ_Envelope *env;
74
75   /**
76    * request id
77    */
78   uint32_t r_id;
79
80 };
81
82 /**
83  * Handle to a lookup request
84  */
85 struct GNUNET_GNS_ReverseLookupRequest
86 {
87
88   /**
89    * DLL
90    */
91   struct GNUNET_GNS_ReverseLookupRequest *next;
92
93   /**
94    * DLL
95    */
96   struct GNUNET_GNS_ReverseLookupRequest *prev;
97
98   /**
99    * handle to gns
100    */
101   struct GNUNET_GNS_Handle *gns_handle;
102
103   /**
104    * processor to call on lookup result
105    */
106   GNUNET_GNS_ReverseLookupResultProcessor lookup_proc;
107
108   /**
109    * @e lookup_proc closure
110    */
111   void *proc_cls;
112
113   /**
114    * Envelope with the message for this queue entry.
115    */
116   struct GNUNET_MQ_Envelope *env;
117
118   /**
119    * request id
120    */
121   uint32_t r_id;
122
123 };
124
125
126 /**
127  * Connection to the GNS service.
128  */
129 struct GNUNET_GNS_Handle
130 {
131
132   /**
133    * Configuration to use.
134    */
135   const struct GNUNET_CONFIGURATION_Handle *cfg;
136
137   /**
138    * Connection to service (if available).
139    */
140   struct GNUNET_MQ_Handle *mq;
141
142   /**
143    * Head of linked list of active lookup requests.
144    */
145   struct GNUNET_GNS_LookupRequest *lookup_head;
146
147   /**
148    * Tail of linked list of active lookup requests.
149    */
150   struct GNUNET_GNS_LookupRequest *lookup_tail;
151
152   /**
153    * Head of linked list of active reverse lookup requests.
154    */
155   struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_head;
156
157   /**
158    * Tail of linked list of active reverse lookup requests.
159    */
160   struct GNUNET_GNS_ReverseLookupRequest *rev_lookup_tail;
161   /**
162    * Reconnect task
163    */
164   struct GNUNET_SCHEDULER_Task *reconnect_task;
165
166   /**
167    * How long do we wait until we try to reconnect?
168    */
169   struct GNUNET_TIME_Relative reconnect_backoff;
170
171   /**
172    * Request Id generator.  Incremented by one for each request.
173    */
174   uint32_t r_id_gen;
175
176 };
177
178
179 /**
180  * Reconnect to GNS service.
181  *
182  * @param handle the handle to the GNS service
183  */
184 static void
185 reconnect (struct GNUNET_GNS_Handle *handle);
186
187
188 /**
189  * Reconnect to GNS
190  *
191  * @param cls the handle
192  */
193 static void
194 reconnect_task (void *cls)
195 {
196   struct GNUNET_GNS_Handle *handle = cls;
197
198   handle->reconnect_task = NULL;
199   reconnect (handle);
200 }
201
202
203 /**
204  * Disconnect from service and then reconnect.
205  *
206  * @param handle our handle
207  */
208 static void
209 force_reconnect (struct GNUNET_GNS_Handle *handle)
210 {
211   GNUNET_MQ_destroy (handle->mq);
212   handle->mq = NULL;
213   handle->reconnect_backoff
214     = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
215   handle->reconnect_task
216     = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
217                                     &reconnect_task,
218                                     handle);
219 }
220
221
222 /**
223  * Generic error handler, called with the appropriate error code and
224  * the same closure specified at the creation of the message queue.
225  * Not every message queue implementation supports an error handler.
226  *
227  * @param cls closure with the `struct GNUNET_GNS_Handle *`
228  * @param error error code
229  */
230 static void
231 mq_error_handler (void *cls,
232                   enum GNUNET_MQ_Error error)
233 {
234   struct GNUNET_GNS_Handle *handle = cls;
235   LOG (GNUNET_ERROR_TYPE_WARNING, "Problem with message queue. error: %i\n",
236        error);
237   force_reconnect (handle);
238 }
239
240 /**
241  * Check validity of message received from the GNS service
242  *
243  * @param cls the `struct GNUNET_GNS_Handle *`
244  * @param loookup_msg the incoming message
245  */
246 static int
247 check_rev_result (void *cls,
248               const struct ReverseLookupResultMessage *lookup_msg)
249 {
250   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
251   char *name;
252   
253   name = (char*) &lookup_msg[1];
254   if ('\0' != name[mlen-1])
255   {
256     GNUNET_break (0);
257     return GNUNET_SYSERR;
258   }
259   return GNUNET_OK;
260 }
261
262
263 /**
264  * Handler for messages received from the GNS service
265  *
266  * @param cls the `struct GNUNET_GNS_Handle *`
267  * @param loookup_msg the incoming message
268  */
269 static void
270 handle_rev_result (void *cls,
271                    const struct ReverseLookupResultMessage *lookup_msg)
272 {
273   struct GNUNET_GNS_Handle *handle = cls;
274   char *name;
275   uint32_t r_id = ntohl (lookup_msg->id);
276   struct GNUNET_GNS_ReverseLookupRequest *rlr;
277   GNUNET_GNS_ReverseLookupResultProcessor proc;
278   void *proc_cls;
279
280   name = (char*)&lookup_msg[1];
281   LOG (GNUNET_ERROR_TYPE_DEBUG,
282        "Received reverse lookup reply from GNS service (%s)\n",
283        name);
284   for (rlr = handle->rev_lookup_head; NULL != rlr; rlr = rlr->next)
285     if (rlr->r_id == r_id)
286       break;
287   if (NULL == rlr)
288     return;
289   proc = rlr->lookup_proc;
290   proc_cls = rlr->proc_cls;
291   GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
292                                handle->rev_lookup_tail,
293                                rlr);
294   GNUNET_free (rlr);
295   proc (proc_cls,
296         name);
297 }
298
299
300
301 /**
302  * Check validity of message received from the GNS service
303  *
304  * @param cls the `struct GNUNET_GNS_Handle *`
305  * @param loookup_msg the incoming message
306  */
307 static int
308 check_result (void *cls,
309               const struct LookupResultMessage *lookup_msg)
310 {
311   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
312   uint32_t rd_count = ntohl (lookup_msg->rd_count);
313   struct GNUNET_GNSRECORD_Data rd[rd_count];
314
315   if (GNUNET_SYSERR ==
316       GNUNET_GNSRECORD_records_deserialize (mlen,
317                                             (const char*) &lookup_msg[1],
318                                             rd_count,
319                                             rd))
320   {
321     GNUNET_break (0);
322     return GNUNET_SYSERR;
323   }
324   return GNUNET_OK;
325 }
326
327
328 /**
329  * Handler for messages received from the GNS service
330  *
331  * @param cls the `struct GNUNET_GNS_Handle *`
332  * @param loookup_msg the incoming message
333  */
334 static void
335 handle_result (void *cls,
336                const struct LookupResultMessage *lookup_msg)
337 {
338   struct GNUNET_GNS_Handle *handle = cls;
339   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
340   uint32_t rd_count = ntohl (lookup_msg->rd_count);
341   struct GNUNET_GNSRECORD_Data rd[rd_count];
342   uint32_t r_id = ntohl (lookup_msg->id);
343   struct GNUNET_GNS_LookupRequest *lr;
344   GNUNET_GNS_LookupResultProcessor proc;
345   void *proc_cls;
346
347   LOG (GNUNET_ERROR_TYPE_DEBUG,
348        "Received lookup reply from GNS service (%u records)\n",
349        (unsigned int) rd_count);
350   for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
351     if (lr->r_id == r_id)
352       break;
353   if (NULL == lr)
354     return;
355   proc = lr->lookup_proc;
356   proc_cls = lr->proc_cls;
357   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
358                                handle->lookup_tail,
359                                lr);
360   GNUNET_free (lr);
361   GNUNET_assert (GNUNET_OK ==
362                  GNUNET_GNSRECORD_records_deserialize (mlen,
363                                                        (const char*) &lookup_msg[1],
364                                                        rd_count,
365                                                        rd));
366   proc (proc_cls,
367         rd_count,
368         rd);
369 }
370
371
372 /**
373  * Reconnect to GNS service.
374  *
375  * @param handle the handle to the GNS service
376  */
377 static void
378 reconnect (struct GNUNET_GNS_Handle *handle)
379 {
380   struct GNUNET_MQ_MessageHandler handlers[] = {
381     GNUNET_MQ_hd_var_size (result,
382                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
383                            struct LookupResultMessage,
384                            handle),
385     GNUNET_MQ_hd_var_size (rev_result,
386                            GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT,
387                            struct ReverseLookupResultMessage,
388                            handle),
389     GNUNET_MQ_handler_end ()
390   };
391   struct GNUNET_GNS_LookupRequest *lh;
392   struct GNUNET_GNS_ReverseLookupRequest *rlh;
393
394   GNUNET_assert (NULL == handle->mq);
395   LOG (GNUNET_ERROR_TYPE_DEBUG,
396        "Trying to connect to GNS\n");
397   handle->mq = GNUNET_CLIENT_connect (handle->cfg,
398                                       "gns",
399                                       handlers,
400                                       &mq_error_handler,
401                                       handle);
402   if (NULL == handle->mq)
403     return;
404   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
405     GNUNET_MQ_send_copy (handle->mq,
406                          lh->env);
407   for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next)
408     GNUNET_MQ_send_copy (handle->mq,
409                          rlh->env);
410 }
411
412
413 /**
414  * Initialize the connection with the GNS service.
415  *
416  * @param cfg configuration to use
417  * @return handle to the GNS service, or NULL on error
418  */
419 struct GNUNET_GNS_Handle *
420 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
421 {
422   struct GNUNET_GNS_Handle *handle;
423
424   handle = GNUNET_new (struct GNUNET_GNS_Handle);
425   handle->cfg = cfg;
426   reconnect (handle);
427   if (NULL == handle->mq)
428   {
429     GNUNET_free (handle);
430     return NULL;
431   }
432   return handle;
433 }
434
435
436 /**
437  * Shutdown connection with the GNS service.
438  *
439  * @param handle handle of the GNS connection to stop
440  */
441 void
442 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
443 {
444   if (NULL != handle->mq)
445   {
446     GNUNET_MQ_destroy (handle->mq);
447     handle->mq = NULL;
448   }
449   if (NULL != handle->reconnect_task)
450   {
451     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
452     handle->reconnect_task = NULL;
453   }
454   GNUNET_assert (NULL == handle->lookup_head);
455   GNUNET_assert (NULL == handle->rev_lookup_head);
456   GNUNET_free (handle);
457 }
458
459
460 /**
461  * Cancel pending lookup request
462  *
463  * @param lr the lookup request to cancel
464  */
465 void
466 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
467 {
468   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
469
470   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
471                                handle->lookup_tail,
472                                lr);
473   GNUNET_MQ_discard (lr->env);
474   GNUNET_free (lr);
475 }
476
477 /**
478  * Cancel pending reverse lookup request
479  *
480  * @param lr the lookup request to cancel
481  */
482 void
483 GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr)
484 {
485   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
486
487   GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
488                                handle->rev_lookup_tail,
489                                lr);
490   GNUNET_MQ_discard (lr->env);
491   GNUNET_free (lr);
492 }
493
494 /**
495  * Perform an asynchronous lookup operation on the GNS.
496  *
497  * @param handle handle to the GNS service
498  * @param name the name to look up
499  * @param zone the zone to start the resolution in
500  * @param type the record type to look up
501  * @param options local options for the lookup
502  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
503  * @param proc processor to call on result
504  * @param proc_cls closure for @a proc
505  * @return handle to the get request
506  */
507 struct GNUNET_GNS_LookupRequest*
508 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
509                    const char *name,
510                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
511                    uint32_t type,
512                    enum GNUNET_GNS_LocalOptions options,
513                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
514                    GNUNET_GNS_LookupResultProcessor proc,
515                    void *proc_cls)
516 {
517   /* IPC to shorten gns names, return shorten_handle */
518   struct LookupMessage *lookup_msg;
519   struct GNUNET_GNS_LookupRequest *lr;
520   size_t nlen;
521
522   if (NULL == name)
523   {
524     GNUNET_break (0);
525     return NULL;
526   }
527   LOG (GNUNET_ERROR_TYPE_DEBUG,
528        "Trying to lookup `%s' in GNS\n",
529        name);
530   nlen = strlen (name) + 1;
531   if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
532   {
533     GNUNET_break (0);
534     return NULL;
535   }
536   lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
537   lr->gns_handle = handle;
538   lr->lookup_proc = proc;
539   lr->proc_cls = proc_cls;
540   lr->r_id = handle->r_id_gen++;
541   lr->env = GNUNET_MQ_msg_extra (lookup_msg,
542                                  nlen,
543                                  GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
544   lookup_msg->id = htonl (lr->r_id);
545   lookup_msg->options = htons ((uint16_t) options);
546   lookup_msg->zone = *zone;
547   lookup_msg->type = htonl (type);
548   if (NULL != shorten_zone_key)
549   {
550     lookup_msg->have_key = htons (GNUNET_YES);
551     lookup_msg->shorten_key = *shorten_zone_key;
552   }
553   GNUNET_memcpy (&lookup_msg[1],
554                  name,
555                  nlen);
556   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
557                                handle->lookup_tail,
558                                lr);
559   if (NULL != handle->mq)
560     GNUNET_MQ_send_copy (handle->mq,
561                          lr->env);
562   return lr;
563 }
564
565 /**
566  * Perform an asynchronous reverse lookup operation on the GNS.
567  *
568  * @param handle handle to the GNS service
569  * @param zone_key zone to find a name for
570  * @param root_key our zone
571  * @param proc processor to call on result
572  * @param proc_cls closure for @a proc
573  * @return handle to the request
574  */
575 struct GNUNET_GNS_ReverseLookupRequest*
576 GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle,
577                            const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
578                            const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key,
579                            GNUNET_GNS_ReverseLookupResultProcessor proc,
580                            void *proc_cls)
581 {
582   /* IPC to shorten gns names, return shorten_handle */
583   struct ReverseLookupMessage *rev_lookup_msg;
584   struct GNUNET_GNS_ReverseLookupRequest *lr;
585
586   if ((NULL == zone_key) || (NULL == root_key))
587   {
588     GNUNET_break (0);
589     return NULL;
590   }
591   LOG (GNUNET_ERROR_TYPE_DEBUG,
592        "Trying to reverse lookup in GNS\n");
593   lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest);
594   lr->gns_handle = handle;
595   lr->lookup_proc = proc;
596   lr->proc_cls = proc_cls;
597   lr->r_id = handle->r_id_gen++;
598   lr->env = GNUNET_MQ_msg (rev_lookup_msg,
599                            GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP);
600   rev_lookup_msg->id = htonl (lr->r_id);
601   rev_lookup_msg->zone_pkey = *zone_key;
602   rev_lookup_msg->root_pkey = *root_key;
603   GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head,
604                                handle->rev_lookup_tail,
605                                lr);
606   if (NULL != handle->mq)
607     GNUNET_MQ_send_copy (handle->mq,
608                          lr->env);
609   return lr;
610 }
611 /* end of gns_api.c */