remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / namecache / namecache_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2013, 2016 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      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @file namecache/namecache_api.c
23  * @brief API to access the NAMECACHE service
24  * @author Martin Schanzenbach
25  * @author Matthias Wachs
26  * @author Christian Grothoff
27  */
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_gnsrecord_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_namecache_service.h"
37 #include "namecache.h"
38
39
40 #define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)
41
42
43 /**
44  * An QueueEntry used to store information for a pending
45  * NAMECACHE record operation
46  */
47 struct GNUNET_NAMECACHE_QueueEntry
48 {
49   /**
50    * Kept in a DLL.
51    */
52   struct GNUNET_NAMECACHE_QueueEntry *next;
53
54   /**
55    * Kept in a DLL.
56    */
57   struct GNUNET_NAMECACHE_QueueEntry *prev;
58
59   /**
60    * Main handle to access the namecache.
61    */
62   struct GNUNET_NAMECACHE_Handle *nsh;
63
64   /**
65    * Continuation to call
66    */
67   GNUNET_NAMECACHE_ContinuationWithStatus cont;
68
69   /**
70    * Closure for @e cont.
71    */
72   void *cont_cls;
73
74   /**
75    * Function to call with the blocks we get back; or NULL.
76    */
77   GNUNET_NAMECACHE_BlockProcessor block_proc;
78
79   /**
80    * Closure for @e block_proc.
81    */
82   void *block_proc_cls;
83
84   /**
85    * The operation id this zone iteration operation has
86    */
87   uint32_t op_id;
88 };
89
90
91 /**
92  * Connection to the NAMECACHE service.
93  */
94 struct GNUNET_NAMECACHE_Handle
95 {
96   /**
97    * Configuration to use.
98    */
99   const struct GNUNET_CONFIGURATION_Handle *cfg;
100
101   /**
102    * Message queue to service.
103    */
104   struct GNUNET_MQ_Handle *mq;
105
106   /**
107    * Currently pending transmission request (or NULL).
108    */
109   struct GNUNET_CLIENT_TransmitHandle *th;
110
111   /**
112    * Head of pending namecache queue entries
113    */
114   struct GNUNET_NAMECACHE_QueueEntry *op_head;
115
116   /**
117    * Tail of pending namecache queue entries
118    */
119   struct GNUNET_NAMECACHE_QueueEntry *op_tail;
120
121   /**
122    * Reconnect task
123    */
124   struct GNUNET_SCHEDULER_Task *reconnect_task;
125
126   /**
127    * Delay introduced before we reconnect.
128    */
129   struct GNUNET_TIME_Relative reconnect_delay;
130
131   /**
132    * Should we reconnect to service due to some serious error?
133    */
134   int reconnect;
135
136   /**
137    * The last operation id used for a NAMECACHE operation
138    */
139   uint32_t last_op_id_used;
140 };
141
142
143 /**
144  * Disconnect from service and then reconnect.
145  *
146  * @param h our handle
147  */
148 static void
149 force_reconnect (struct GNUNET_NAMECACHE_Handle *h);
150
151
152 /**
153  * Find queue entry for the given @a rid.
154  *
155  * @param h handle to search
156  * @param rid request ID to look for
157  * @return NULL if not found, otherwise the queue entry (removed from the queue)
158  */
159 static struct GNUNET_NAMECACHE_QueueEntry *
160 find_qe (struct GNUNET_NAMECACHE_Handle *h,
161          uint32_t rid)
162 {
163   struct GNUNET_NAMECACHE_QueueEntry *qe;
164
165   for (qe = h->op_head; qe != NULL; qe = qe->next)
166   {
167     if (qe->op_id == rid)
168     {
169       GNUNET_CONTAINER_DLL_remove (h->op_head,
170                                    h->op_tail,
171                                    qe);
172       return qe;
173     }
174   }
175   return NULL;
176 }
177
178
179 /**
180  * Handle an incoming message of type
181  * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
182  *
183  * @param cls the `struct GNUNET_NAMECACHE_Handle`
184  * @param msg the message we received
185  */
186 static int
187 check_lookup_block_response (void *cls,
188                              const struct LookupBlockResponseMessage *msg)
189 {
190   /* any length will do, format validation is in handler */
191   return GNUNET_OK;
192 }
193
194
195 /**
196  * Handle an incoming message of type
197  * #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE.
198  *
199  * @param cls the `struct GNUNET_NAMECACHE_Handle`
200  * @param msg the message we received
201  */
202 static void
203 handle_lookup_block_response (void *cls,
204                               const struct LookupBlockResponseMessage *msg)
205 {
206   struct GNUNET_NAMECACHE_Handle *h = cls;
207   size_t size;
208   struct GNUNET_NAMECACHE_QueueEntry *qe;
209
210   LOG (GNUNET_ERROR_TYPE_DEBUG,
211        "Received LOOKUP_BLOCK_RESPONSE\n");
212   qe = find_qe (h,
213                 ntohl (msg->gns_header.r_id));
214   if (NULL == qe)
215     return;
216   if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
217   {
218     /* no match found */
219     if (NULL != qe->block_proc)
220       qe->block_proc (qe->block_proc_cls,
221                       NULL);
222     GNUNET_free (qe);
223     return;
224   }
225   size = ntohs (msg->gns_header.header.size)
226          - sizeof(struct LookupBlockResponseMessage);
227   {
228     char buf[size + sizeof(struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN;
229     struct GNUNET_GNSRECORD_Block *block;
230
231     block = (struct GNUNET_GNSRECORD_Block *) buf;
232     block->signature = msg->signature;
233     block->derived_key = msg->derived_key;
234     block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
235     block->purpose.size = htonl (size
236                                  + sizeof(struct GNUNET_TIME_AbsoluteNBO)
237                                  + sizeof(struct
238                                           GNUNET_CRYPTO_EccSignaturePurpose));
239     block->expiration_time = msg->expire;
240     GNUNET_memcpy (&block[1],
241                    &msg[1],
242                    size);
243     if (GNUNET_OK !=
244         GNUNET_GNSRECORD_block_verify (block))
245     {
246       GNUNET_break (0);
247       if (NULL != qe->block_proc)
248         qe->block_proc (qe->block_proc_cls,
249                         NULL);
250       force_reconnect (h);
251     }
252     else
253     {
254       if (NULL != qe->block_proc)
255         qe->block_proc (qe->block_proc_cls,
256                         block);
257     }
258   }
259   GNUNET_free (qe);
260 }
261
262
263 /**
264  * Handle an incoming message of type
265  * #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
266  *
267  * @param cls the `struct GNUNET_NAMECACHE_Handle`
268  * @param msg the message we received
269  * @param size the message size
270  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
271  */
272 static void
273 handle_block_cache_response (void *cls,
274                              const struct BlockCacheResponseMessage *msg)
275 {
276   struct GNUNET_NAMECACHE_Handle *h = cls;
277   struct GNUNET_NAMECACHE_QueueEntry *qe;
278   int res;
279
280   LOG (GNUNET_ERROR_TYPE_DEBUG,
281        "Received BLOCK_CACHE_RESPONSE\n");
282   qe = find_qe (h,
283                 ntohl (msg->gns_header.r_id));
284   if (NULL == qe)
285     return;
286   res = ntohl (msg->op_result);
287   /* TODO: add actual error message from namecache to response... */
288   if (NULL != qe->cont)
289     qe->cont (qe->cont_cls,
290               res,
291               (GNUNET_OK == res)
292               ? NULL
293               : _ ("Namecache failed to cache block"));
294   GNUNET_free (qe);
295 }
296
297
298 /**
299  * Generic error handler, called with the appropriate error code and
300  * the same closure specified at the creation of the message queue.
301  * Not every message queue implementation supports an error handler.
302  *
303  * @param cls closure with the `struct GNUNET_NAMECACHE_Handle *`
304  * @param error error code
305  */
306 static void
307 mq_error_handler (void *cls,
308                   enum GNUNET_MQ_Error error)
309 {
310   struct GNUNET_NAMECACHE_Handle *h = cls;
311
312   force_reconnect (h);
313 }
314
315
316 /**
317  * Reconnect to namecache service.
318  *
319  * @param h the handle to the NAMECACHE service
320  */
321 static void
322 reconnect (struct GNUNET_NAMECACHE_Handle *h)
323 {
324   struct GNUNET_MQ_MessageHandler handlers[] = {
325     GNUNET_MQ_hd_var_size (lookup_block_response,
326                            GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE,
327                            struct LookupBlockResponseMessage,
328                            h),
329     GNUNET_MQ_hd_fixed_size (block_cache_response,
330                              GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE,
331                              struct BlockCacheResponseMessage,
332                              h),
333     GNUNET_MQ_handler_end ()
334   };
335
336   GNUNET_assert (NULL == h->mq);
337   h->mq = GNUNET_CLIENT_connect (h->cfg,
338                                  "namecache",
339                                  handlers,
340                                  &mq_error_handler,
341                                  h);
342 }
343
344
345 /**
346  * Re-establish the connection to the service.
347  *
348  * @param cls handle to use to re-connect.
349  */
350 static void
351 reconnect_task (void *cls)
352 {
353   struct GNUNET_NAMECACHE_Handle *h = cls;
354
355   h->reconnect_task = NULL;
356   reconnect (h);
357 }
358
359
360 /**
361  * Disconnect from service and then reconnect.
362  *
363  * @param h our handle
364  */
365 static void
366 force_reconnect (struct GNUNET_NAMECACHE_Handle *h)
367 {
368   struct GNUNET_NAMECACHE_QueueEntry *qe;
369
370   h->reconnect = GNUNET_NO;
371   GNUNET_MQ_destroy (h->mq);
372   h->mq = NULL;
373   while (NULL != (qe = h->op_head))
374   {
375     GNUNET_CONTAINER_DLL_remove (h->op_head,
376                                  h->op_tail,
377                                  qe);
378     if (NULL != qe->cont)
379       qe->cont (qe->cont_cls,
380                 GNUNET_SYSERR,
381                 _ ("Error communicating with namecache service"));
382     GNUNET_free (qe);
383   }
384   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385               "Reconnecting to namecache\n");
386   h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
387   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
388                                                     &reconnect_task,
389                                                     h);
390 }
391
392
393 /**
394  * Get a fresh operation id to distinguish between namecache requests
395  *
396  * @param h the namecache handle
397  * @return next operation id to use
398  */
399 static uint32_t
400 get_op_id (struct GNUNET_NAMECACHE_Handle *h)
401 {
402   return h->last_op_id_used++;
403 }
404
405
406 /**
407  * Initialize the connection with the NAMECACHE service.
408  *
409  * @param cfg configuration to use
410  * @return handle to the GNS service, or NULL on error
411  */
412 struct GNUNET_NAMECACHE_Handle *
413 GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
414 {
415   struct GNUNET_NAMECACHE_Handle *h;
416
417   h = GNUNET_new (struct GNUNET_NAMECACHE_Handle);
418   h->cfg = cfg;
419   reconnect (h);
420   if (NULL == h->mq)
421   {
422     GNUNET_free (h);
423     return NULL;
424   }
425   return h;
426 }
427
428
429 /**
430  * Disconnect from the namecache service (and free associated
431  * resources).
432  *
433  * @param h handle to the namecache
434  */
435 void
436 GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h)
437 {
438   struct GNUNET_NAMECACHE_QueueEntry *q;
439
440   GNUNET_break (NULL == h->op_head);
441   while (NULL != (q = h->op_head))
442   {
443     GNUNET_CONTAINER_DLL_remove (h->op_head,
444                                  h->op_tail,
445                                  q);
446     GNUNET_free (q);
447   }
448   if (NULL != h->mq)
449   {
450     GNUNET_MQ_destroy (h->mq);
451     h->mq = NULL;
452   }
453   if (NULL != h->reconnect_task)
454   {
455     GNUNET_SCHEDULER_cancel (h->reconnect_task);
456     h->reconnect_task = NULL;
457   }
458   GNUNET_free (h);
459 }
460
461
462 /**
463  * Store an item in the namecache.  If the item is already present,
464  * it is replaced with the new record.
465  *
466  * @param h handle to the namecache
467  * @param block block to store
468  * @param cont continuation to call when done
469  * @param cont_cls closure for @a cont
470  * @return handle to abort the request, NULL on error
471  */
472 struct GNUNET_NAMECACHE_QueueEntry *
473 GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h,
474                               const struct GNUNET_GNSRECORD_Block *block,
475                               GNUNET_NAMECACHE_ContinuationWithStatus cont,
476                               void *cont_cls)
477 {
478   struct GNUNET_NAMECACHE_QueueEntry *qe;
479   struct BlockCacheMessage *msg;
480   struct GNUNET_MQ_Envelope *env;
481   uint32_t rid;
482   size_t blen;
483
484   if (NULL == h->mq)
485     return NULL;
486   blen = ntohl (block->purpose.size);
487   GNUNET_assert (blen > (sizeof(struct GNUNET_TIME_AbsoluteNBO)
488                          + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)));
489   blen -= (sizeof(struct GNUNET_TIME_AbsoluteNBO)
490            + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
491   rid = get_op_id (h);
492   qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
493   qe->nsh = h;
494   qe->cont = cont;
495   qe->cont_cls = cont_cls;
496   qe->op_id = rid;
497   GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
498                                     h->op_tail,
499                                     qe);
500   /* send msg */
501   env = GNUNET_MQ_msg_extra (msg,
502                              blen,
503                              GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE);
504   msg->gns_header.r_id = htonl (rid);
505   msg->expire = block->expiration_time;
506   msg->signature = block->signature;
507   msg->derived_key = block->derived_key;
508   GNUNET_memcpy (&msg[1],
509                  &block[1],
510                  blen);
511   GNUNET_MQ_send (h->mq,
512                   env);
513   return qe;
514 }
515
516
517 /**
518  * Get a result for a particular key from the namecache.  The processor
519  * will only be called once.
520  *
521  * @param h handle to the namecache
522  * @param derived_hash hash of zone key combined with name to lookup
523  * @param proc function to call on the matching block, or with
524  *        NULL if there is no matching block
525  * @param proc_cls closure for @a proc
526  * @return a handle that can be used to cancel, NULL on error
527  */
528 struct GNUNET_NAMECACHE_QueueEntry *
529 GNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h,
530                                const struct GNUNET_HashCode *derived_hash,
531                                GNUNET_NAMECACHE_BlockProcessor proc,
532                                void *proc_cls)
533 {
534   struct GNUNET_NAMECACHE_QueueEntry *qe;
535   struct LookupBlockMessage *msg;
536   struct GNUNET_MQ_Envelope *env;
537   uint32_t rid;
538
539   if (NULL == h->mq)
540     return NULL;
541   LOG (GNUNET_ERROR_TYPE_DEBUG,
542        "Looking for block under %s\n",
543        GNUNET_h2s (derived_hash));
544   rid = get_op_id (h);
545   qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
546   qe->nsh = h;
547   qe->block_proc = proc;
548   qe->block_proc_cls = proc_cls;
549   qe->op_id = rid;
550   GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
551                                     h->op_tail,
552                                     qe);
553   env = GNUNET_MQ_msg (msg,
554                        GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK);
555   msg->gns_header.r_id = htonl (rid);
556   msg->query = *derived_hash;
557   GNUNET_MQ_send (h->mq,
558                   env);
559   return qe;
560 }
561
562
563 /**
564  * Cancel a namecache operation.  The final callback from the
565  * operation must not have been done yet.
566  *
567  * @param qe operation to cancel
568  */
569 void
570 GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe)
571 {
572   struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
573
574   GNUNET_CONTAINER_DLL_remove (h->op_head,
575                                h->op_tail,
576                                qe);
577   GNUNET_free (qe);
578 }
579
580
581 /* end of namecache_api.c */