5ad7b4faeda74b0be71116df753fa218bb394ee3
[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   
358   GNUNET_assert (GNUNET_OK ==
359                  GNUNET_GNSRECORD_records_deserialize (mlen,
360                                                        (const char*) &lookup_msg[1],
361                                                        rd_count,
362                                                        rd));
363   proc (proc_cls,
364         rd_count,
365         rd);
366   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
367                                handle->lookup_tail,
368                                lr);
369   if (NULL != lr->env)
370     GNUNET_MQ_discard (lr->env);
371   GNUNET_free (lr);
372 }
373
374
375 /**
376  * Reconnect to GNS service.
377  *
378  * @param handle the handle to the GNS service
379  */
380 static void
381 reconnect (struct GNUNET_GNS_Handle *handle)
382 {
383   struct GNUNET_MQ_MessageHandler handlers[] = {
384     GNUNET_MQ_hd_var_size (result,
385                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
386                            struct LookupResultMessage,
387                            handle),
388     GNUNET_MQ_hd_var_size (rev_result,
389                            GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP_RESULT,
390                            struct ReverseLookupResultMessage,
391                            handle),
392     GNUNET_MQ_handler_end ()
393   };
394   struct GNUNET_GNS_LookupRequest *lh;
395   struct GNUNET_GNS_ReverseLookupRequest *rlh;
396
397   GNUNET_assert (NULL == handle->mq);
398   LOG (GNUNET_ERROR_TYPE_DEBUG,
399        "Trying to connect to GNS\n");
400   handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
401                                       "gns",
402                                       handlers,
403                                       &mq_error_handler,
404                                       handle);
405   if (NULL == handle->mq)
406     return;
407   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
408     GNUNET_MQ_send_copy (handle->mq,
409                          lh->env);
410   for (rlh = handle->rev_lookup_head; NULL != rlh; rlh = rlh->next)
411     GNUNET_MQ_send_copy (handle->mq,
412                          rlh->env);
413 }
414
415
416 /**
417  * Initialize the connection with the GNS service.
418  *
419  * @param cfg configuration to use
420  * @return handle to the GNS service, or NULL on error
421  */
422 struct GNUNET_GNS_Handle *
423 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
424 {
425   struct GNUNET_GNS_Handle *handle;
426
427   handle = GNUNET_new (struct GNUNET_GNS_Handle);
428   handle->cfg = cfg;
429   reconnect (handle);
430   if (NULL == handle->mq)
431   {
432     GNUNET_free (handle);
433     return NULL;
434   }
435   return handle;
436 }
437
438
439 /**
440  * Shutdown connection with the GNS service.
441  *
442  * @param handle handle of the GNS connection to stop
443  */
444 void
445 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
446 {
447   if (NULL != handle->mq)
448   {
449     GNUNET_MQ_destroy (handle->mq);
450     handle->mq = NULL;
451   }
452   if (NULL != handle->reconnect_task)
453   {
454     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
455     handle->reconnect_task = NULL;
456   }
457   GNUNET_assert (NULL == handle->lookup_head);
458   GNUNET_assert (NULL == handle->rev_lookup_head);
459   GNUNET_free (handle);
460 }
461
462
463 /**
464  * Cancel pending lookup request
465  *
466  * @param lr the lookup request to cancel
467  */
468 void
469 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
470 {
471   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
472
473   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
474                                handle->lookup_tail,
475                                lr);
476   GNUNET_MQ_discard (lr->env);
477   GNUNET_free (lr);
478 }
479
480 /**
481  * Cancel pending reverse lookup request
482  *
483  * @param lr the lookup request to cancel
484  */
485 void
486 GNUNET_GNS_reverse_lookup_cancel (struct GNUNET_GNS_ReverseLookupRequest *lr)
487 {
488   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
489
490   GNUNET_CONTAINER_DLL_remove (handle->rev_lookup_head,
491                                handle->rev_lookup_tail,
492                                lr);
493   GNUNET_MQ_discard (lr->env);
494   GNUNET_free (lr);
495 }
496
497 /**
498  * Perform an asynchronous lookup operation on the GNS.
499  *
500  * @param handle handle to the GNS service
501  * @param name the name to look up
502  * @param zone the zone to start the resolution in
503  * @param type the record type to look up
504  * @param options local options for the lookup
505  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
506  * @param proc processor to call on result
507  * @param proc_cls closure for @a proc
508  * @return handle to the get request
509  */
510 struct GNUNET_GNS_LookupRequest*
511 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
512                    const char *name,
513                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
514                    uint32_t type,
515                    enum GNUNET_GNS_LocalOptions options,
516                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
517                    GNUNET_GNS_LookupResultProcessor proc,
518                    void *proc_cls)
519 {
520   /* IPC to shorten gns names, return shorten_handle */
521   struct LookupMessage *lookup_msg;
522   struct GNUNET_GNS_LookupRequest *lr;
523   size_t nlen;
524
525   if (NULL == name)
526   {
527     GNUNET_break (0);
528     return NULL;
529   }
530   LOG (GNUNET_ERROR_TYPE_DEBUG,
531        "Trying to lookup `%s' in GNS\n",
532        name);
533   nlen = strlen (name) + 1;
534   if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
535   {
536     GNUNET_break (0);
537     return NULL;
538   }
539   lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
540   lr->gns_handle = handle;
541   lr->lookup_proc = proc;
542   lr->proc_cls = proc_cls;
543   lr->r_id = handle->r_id_gen++;
544   lr->env = GNUNET_MQ_msg_extra (lookup_msg,
545                                  nlen,
546                                  GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
547   lookup_msg->id = htonl (lr->r_id);
548   lookup_msg->options = htons ((uint16_t) options);
549   lookup_msg->zone = *zone;
550   lookup_msg->type = htonl (type);
551   if (NULL != shorten_zone_key)
552   {
553     lookup_msg->have_key = htons (GNUNET_YES);
554     lookup_msg->shorten_key = *shorten_zone_key;
555   }
556   GNUNET_memcpy (&lookup_msg[1],
557                  name,
558                  nlen);
559   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
560                                handle->lookup_tail,
561                                lr);
562   if (NULL != handle->mq)
563     GNUNET_MQ_send_copy (handle->mq,
564                          lr->env);
565   return lr;
566 }
567
568 /**
569  * Perform an asynchronous reverse lookup operation on the GNS.
570  *
571  * @param handle handle to the GNS service
572  * @param zone_key zone to find a name for
573  * @param root_key our zone
574  * @param proc processor to call on result
575  * @param proc_cls closure for @a proc
576  * @return handle to the request
577  */
578 struct GNUNET_GNS_ReverseLookupRequest*
579 GNUNET_GNS_reverse_lookup (struct GNUNET_GNS_Handle *handle,
580                            const struct GNUNET_CRYPTO_EcdsaPublicKey *zone_key,
581                            const struct GNUNET_CRYPTO_EcdsaPublicKey *root_key,
582                            GNUNET_GNS_ReverseLookupResultProcessor proc,
583                            void *proc_cls)
584 {
585   /* IPC to shorten gns names, return shorten_handle */
586   struct ReverseLookupMessage *rev_lookup_msg;
587   struct GNUNET_GNS_ReverseLookupRequest *lr;
588
589   if ((NULL == zone_key) || (NULL == root_key))
590   {
591     GNUNET_break (0);
592     return NULL;
593   }
594   LOG (GNUNET_ERROR_TYPE_DEBUG,
595        "Trying to reverse lookup in GNS\n");
596   lr = GNUNET_new (struct GNUNET_GNS_ReverseLookupRequest);
597   lr->gns_handle = handle;
598   lr->lookup_proc = proc;
599   lr->proc_cls = proc_cls;
600   lr->r_id = handle->r_id_gen++;
601   lr->env = GNUNET_MQ_msg (rev_lookup_msg,
602                            GNUNET_MESSAGE_TYPE_GNS_REVERSE_LOOKUP);
603   rev_lookup_msg->id = htonl (lr->r_id);
604   rev_lookup_msg->zone_pkey = *zone_key;
605   rev_lookup_msg->root_pkey = *root_key;
606   GNUNET_CONTAINER_DLL_insert (handle->rev_lookup_head,
607                                handle->rev_lookup_tail,
608                                lr);
609   if (NULL != handle->mq)
610     GNUNET_MQ_send_copy (handle->mq,
611                          lr->env);
612   return lr;
613 }
614 /* end of gns_api.c */