-ensure stats queues do not grow too big
[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 /**
84  * Connection to the GNS service.
85  */
86 struct GNUNET_GNS_Handle
87 {
88
89   /**
90    * Configuration to use.
91    */
92   const struct GNUNET_CONFIGURATION_Handle *cfg;
93
94   /**
95    * Connection to service (if available).
96    */
97   struct GNUNET_MQ_Handle *mq;
98
99   /**
100    * Head of linked list of active lookup requests.
101    */
102   struct GNUNET_GNS_LookupRequest *lookup_head;
103
104   /**
105    * Tail of linked list of active lookup requests.
106    */
107   struct GNUNET_GNS_LookupRequest *lookup_tail;
108
109   /**
110    * Reconnect task
111    */
112   struct GNUNET_SCHEDULER_Task *reconnect_task;
113
114   /**
115    * How long do we wait until we try to reconnect?
116    */
117   struct GNUNET_TIME_Relative reconnect_backoff;
118
119   /**
120    * Request Id generator.  Incremented by one for each request.
121    */
122   uint32_t r_id_gen;
123
124 };
125
126
127 /**
128  * Reconnect to GNS service.
129  *
130  * @param handle the handle to the GNS service
131  */
132 static void
133 reconnect (struct GNUNET_GNS_Handle *handle);
134
135
136 /**
137  * Reconnect to GNS
138  *
139  * @param cls the handle
140  */
141 static void
142 reconnect_task (void *cls)
143 {
144   struct GNUNET_GNS_Handle *handle = cls;
145
146   handle->reconnect_task = NULL;
147   reconnect (handle);
148 }
149
150
151 /**
152  * Disconnect from service and then reconnect.
153  *
154  * @param handle our handle
155  */
156 static void
157 force_reconnect (struct GNUNET_GNS_Handle *handle)
158 {
159   GNUNET_MQ_destroy (handle->mq);
160   handle->mq = NULL;
161   handle->reconnect_backoff
162     = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
163   handle->reconnect_task
164     = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
165                                     &reconnect_task,
166                                     handle);
167 }
168
169
170 /**
171  * Generic error handler, called with the appropriate error code and
172  * the same closure specified at the creation of the message queue.
173  * Not every message queue implementation supports an error handler.
174  *
175  * @param cls closure with the `struct GNUNET_GNS_Handle *`
176  * @param error error code
177  */
178 static void
179 mq_error_handler (void *cls,
180                   enum GNUNET_MQ_Error error)
181 {
182   struct GNUNET_GNS_Handle *handle = cls;
183
184   force_reconnect (handle);
185 }
186
187
188 /**
189  * Check validity of message received from the GNS service
190  *
191  * @param cls the `struct GNUNET_GNS_Handle *`
192  * @param loookup_msg the incoming message
193  */
194 static int
195 check_result (void *cls,
196               const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg)
197 {
198   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
199   uint32_t rd_count = ntohl (lookup_msg->rd_count);
200   struct GNUNET_GNSRECORD_Data rd[rd_count];
201
202   if (GNUNET_SYSERR ==
203       GNUNET_GNSRECORD_records_deserialize (mlen,
204                                             (const char*) &lookup_msg[1],
205                                             rd_count,
206                                             rd))
207   {
208     GNUNET_break (0);
209     return GNUNET_SYSERR;
210   }
211   return GNUNET_OK;
212 }
213
214
215 /**
216  * Handler for messages received from the GNS service
217  *
218  * @param cls the `struct GNUNET_GNS_Handle *`
219  * @param loookup_msg the incoming message
220  */
221 static void
222 handle_result (void *cls,
223                const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg)
224 {
225   struct GNUNET_GNS_Handle *handle = cls;
226   size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg);
227   uint32_t rd_count = ntohl (lookup_msg->rd_count);
228   struct GNUNET_GNSRECORD_Data rd[rd_count];
229   uint32_t r_id = ntohl (lookup_msg->id);
230   struct GNUNET_GNS_LookupRequest *lr;
231   GNUNET_GNS_LookupResultProcessor proc;
232   void *proc_cls;
233
234   LOG (GNUNET_ERROR_TYPE_DEBUG,
235        "Received lookup reply from GNS service (%u records)\n",
236        (unsigned int) rd_count);
237   for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
238     if (lr->r_id == r_id)
239       break;
240   if (NULL == lr)
241     return;
242   proc = lr->lookup_proc;
243   proc_cls = lr->proc_cls;
244   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
245                                handle->lookup_tail,
246                                lr);
247   GNUNET_free (lr);
248   GNUNET_assert (GNUNET_OK ==
249                  GNUNET_GNSRECORD_records_deserialize (mlen,
250                                                        (const char*) &lookup_msg[1],
251                                                        rd_count,
252                                                        rd));
253   proc (proc_cls,
254         rd_count,
255         rd);
256 }
257
258
259 /**
260  * Reconnect to GNS service.
261  *
262  * @param handle the handle to the GNS service
263  */
264 static void
265 reconnect (struct GNUNET_GNS_Handle *handle)
266 {
267   GNUNET_MQ_hd_var_size (result,
268                          GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
269                          struct GNUNET_GNS_ClientLookupResultMessage);
270   struct GNUNET_MQ_MessageHandler handlers[] = {
271     make_result_handler (handle),
272     GNUNET_MQ_handler_end ()
273   };
274   struct GNUNET_GNS_LookupRequest *lh;
275
276   GNUNET_assert (NULL == handle->mq);
277   LOG (GNUNET_ERROR_TYPE_DEBUG,
278        "Trying to connect to GNS\n");
279   handle->mq = GNUNET_CLIENT_connecT (handle->cfg,
280                                       "gns",
281                                       handlers,
282                                       &mq_error_handler,
283                                       handle);
284   if (NULL == handle->mq)
285     return;
286   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
287     GNUNET_MQ_send_copy (handle->mq,
288                          lh->env);
289 }
290
291
292 /**
293  * Initialize the connection with the GNS service.
294  *
295  * @param cfg configuration to use
296  * @return handle to the GNS service, or NULL on error
297  */
298 struct GNUNET_GNS_Handle *
299 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
300 {
301   struct GNUNET_GNS_Handle *handle;
302
303   handle = GNUNET_new (struct GNUNET_GNS_Handle);
304   handle->cfg = cfg;
305   reconnect (handle);
306   if (NULL == handle->mq)
307   {
308     GNUNET_free (handle);
309     return NULL;
310   }
311   return handle;
312 }
313
314
315 /**
316  * Shutdown connection with the GNS service.
317  *
318  * @param handle handle of the GNS connection to stop
319  */
320 void
321 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
322 {
323   if (NULL != handle->mq)
324   {
325     GNUNET_MQ_destroy (handle->mq);
326     handle->mq = NULL;
327   }
328   if (NULL != handle->reconnect_task)
329   {
330     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
331     handle->reconnect_task = NULL;
332   }
333   GNUNET_assert (NULL == handle->lookup_head);
334   GNUNET_free (handle);
335 }
336
337
338 /**
339  * Cancel pending lookup request
340  *
341  * @param lr the lookup request to cancel
342  */
343 void
344 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
345 {
346   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
347
348   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
349                                handle->lookup_tail,
350                                lr);
351   GNUNET_MQ_discard (lr->env);
352   GNUNET_free (lr);
353 }
354
355
356 /**
357  * Perform an asynchronous lookup operation on the GNS.
358  *
359  * @param handle handle to the GNS service
360  * @param name the name to look up
361  * @param zone the zone to start the resolution in
362  * @param type the record type to look up
363  * @param options local options for the lookup
364  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
365  * @param proc processor to call on result
366  * @param proc_cls closure for @a proc
367  * @return handle to the get request
368  */
369 struct GNUNET_GNS_LookupRequest*
370 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
371                    const char *name,
372                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
373                    uint32_t type,
374                    enum GNUNET_GNS_LocalOptions options,
375                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
376                    GNUNET_GNS_LookupResultProcessor proc,
377                    void *proc_cls)
378 {
379   /* IPC to shorten gns names, return shorten_handle */
380   struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
381   struct GNUNET_GNS_LookupRequest *lr;
382   size_t nlen;
383
384   if (NULL == name)
385   {
386     GNUNET_break (0);
387     return NULL;
388   }
389   LOG (GNUNET_ERROR_TYPE_DEBUG,
390        "Trying to lookup `%s' in GNS\n",
391        name);
392   nlen = strlen (name) + 1;
393   if (nlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*lr))
394   {
395     GNUNET_break (0);
396     return NULL;
397   }
398   lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
399   lr->gns_handle = handle;
400   lr->lookup_proc = proc;
401   lr->proc_cls = proc_cls;
402   lr->r_id = handle->r_id_gen++;
403   lr->env = GNUNET_MQ_msg_extra (lookup_msg,
404                                  nlen,
405                                  GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
406   lookup_msg->id = htonl (lr->r_id);
407   lookup_msg->options = htons ((uint16_t) options);
408   lookup_msg->zone = *zone;
409   lookup_msg->type = htonl (type);
410   if (NULL != shorten_zone_key)
411   {
412     lookup_msg->have_key = htons (GNUNET_YES);
413     lookup_msg->shorten_key = *shorten_zone_key;
414   }
415   memcpy (&lookup_msg[1],
416           name,
417           nlen);
418   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
419                                handle->lookup_tail,
420                                lr);
421   if (NULL != handle->mq)
422     GNUNET_MQ_send_copy (handle->mq,
423                          lr->env);
424   return lr;
425 }
426
427
428 /* end of gns_api.c */