global reindent, now with uncrustify hook enabled
[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      SPDX-License-Identifier: AGPL3.0-or-later
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 "gns_api.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    * DLL
46    */
47   struct GNUNET_GNS_LookupRequest *next;
48
49   /**
50    * DLL
51    */
52   struct GNUNET_GNS_LookupRequest *prev;
53
54   /**
55    * handle to gns
56    */
57   struct GNUNET_GNS_Handle *gns_handle;
58
59   /**
60    * processor to call on lookup result
61    */
62   GNUNET_GNS_LookupResultProcessor lookup_proc;
63
64   /**
65    * @e lookup_proc closure
66    */
67   void *proc_cls;
68
69   /**
70    * Envelope with the message for this queue entry.
71    */
72   struct GNUNET_MQ_Envelope *env;
73
74   /**
75    * request id
76    */
77   uint32_t r_id;
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
206                                                         char*) &lookup_msg[1],
207                                                        rd_count,
208                                                        rd));
209   proc (proc_cls,
210         rd_count,
211         rd);
212   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
213                                handle->lookup_tail,
214                                lr);
215   if (NULL != lr->env)
216     GNUNET_MQ_discard (lr->env);
217   GNUNET_free (lr);
218 }
219
220
221 /**
222  * Reconnect to GNS service.
223  *
224  * @param handle the handle to the GNS service
225  */
226 static void
227 reconnect (struct GNUNET_GNS_Handle *handle)
228 {
229   struct GNUNET_MQ_MessageHandler handlers[] = {
230     GNUNET_MQ_hd_var_size (result,
231                            GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT,
232                            struct LookupResultMessage,
233                            handle),
234     GNUNET_MQ_handler_end ()
235   };
236
237   GNUNET_assert (NULL == handle->mq);
238   LOG (GNUNET_ERROR_TYPE_DEBUG,
239        "Trying to connect to GNS\n");
240   handle->mq = GNUNET_CLIENT_connect (handle->cfg,
241                                       "gns",
242                                       handlers,
243                                       &mq_error_handler,
244                                       handle);
245   if (NULL == handle->mq)
246     return;
247   for (struct GNUNET_GNS_LookupRequest *lh = handle->lookup_head;
248        NULL != lh;
249        lh = lh->next)
250     GNUNET_MQ_send_copy (handle->mq,
251                          lh->env);
252 }
253
254
255 /**
256  * Initialize the connection with the GNS service.
257  *
258  * @param cfg configuration to use
259  * @return handle to the GNS service, or NULL on error
260  */
261 struct GNUNET_GNS_Handle *
262 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
263 {
264   struct GNUNET_GNS_Handle *handle;
265
266   handle = GNUNET_new (struct GNUNET_GNS_Handle);
267   handle->cfg = cfg;
268   reconnect (handle);
269   if (NULL == handle->mq)
270   {
271     GNUNET_free (handle);
272     return NULL;
273   }
274   return handle;
275 }
276
277
278 /**
279  * Shutdown connection with the GNS service.
280  *
281  * @param handle handle of the GNS connection to stop
282  */
283 void
284 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
285 {
286   if (NULL != handle->mq)
287   {
288     GNUNET_MQ_destroy (handle->mq);
289     handle->mq = NULL;
290   }
291   if (NULL != handle->reconnect_task)
292   {
293     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
294     handle->reconnect_task = NULL;
295   }
296   GNUNET_assert (NULL == handle->lookup_head);
297   GNUNET_free (handle);
298 }
299
300
301 /**
302  * Cancel pending lookup request
303  *
304  * @param lr the lookup request to cancel
305  * @return closure from the lookup result processor
306  */
307 void *
308 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
309 {
310   struct GNUNET_GNS_Handle *handle = lr->gns_handle;
311   void *ret;
312
313   GNUNET_CONTAINER_DLL_remove (handle->lookup_head,
314                                handle->lookup_tail,
315                                lr);
316   GNUNET_MQ_discard (lr->env);
317   ret = lr->proc_cls;
318   GNUNET_free (lr);
319   return ret;
320 }
321
322
323 /**
324  * Perform an asynchronous lookup operation on the GNS.
325  *
326  * @param handle handle to the GNS service
327  * @param name the name to look up
328  * @param zone the zone to start the resolution in
329  * @param type the record type to look up
330  * @param options local options for the lookup
331  * @param proc processor to call on result
332  * @param proc_cls closure for @a proc
333  * @return handle to the get request
334  */
335 struct GNUNET_GNS_LookupRequest*
336 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
337                    const char *name,
338                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
339                    uint32_t type,
340                    enum GNUNET_GNS_LocalOptions options,
341                    GNUNET_GNS_LookupResultProcessor proc,
342                    void *proc_cls)
343 {
344   /* IPC to shorten gns names, return shorten_handle */
345   struct LookupMessage *lookup_msg;
346   struct GNUNET_GNS_LookupRequest *lr;
347   size_t nlen;
348
349   if (NULL == name)
350   {
351     GNUNET_break (0);
352     return NULL;
353   }
354   LOG (GNUNET_ERROR_TYPE_DEBUG,
355        "Trying to lookup `%s' in GNS\n",
356        name);
357   nlen = strlen (name) + 1;
358   if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof(*lr))
359   {
360     GNUNET_break (0);
361     return NULL;
362   }
363   lr = GNUNET_new (struct GNUNET_GNS_LookupRequest);
364   lr->gns_handle = handle;
365   lr->lookup_proc = proc;
366   lr->proc_cls = proc_cls;
367   lr->r_id = handle->r_id_gen++;
368   lr->env = GNUNET_MQ_msg_extra (lookup_msg,
369                                  nlen,
370                                  GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
371   lookup_msg->id = htonl (lr->r_id);
372   lookup_msg->options = htons ((uint16_t) options);
373   lookup_msg->zone = *zone;
374   lookup_msg->type = htonl (type);
375   GNUNET_memcpy (&lookup_msg[1],
376                  name,
377                  nlen);
378   GNUNET_CONTAINER_DLL_insert (handle->lookup_head,
379                                handle->lookup_tail,
380                                lr);
381   if (NULL != handle->mq)
382     GNUNET_MQ_send_copy (handle->mq,
383                          lr->env);
384   return lr;
385 }
386
387
388 /* end of gns_api.c */