6696158c9c9deb74f1d2e5530d0541efd313eb04
[oweals/gnunet.git] / src / gns / gns_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013 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    * processor closure
67    */
68   void *proc_cls;
69
70   /**
71    * request id
72    */
73   uint32_t r_id;
74
75 };
76
77
78 /**
79  * Entry in our list of messages to be (re-)transmitted.
80  */
81 struct PendingMessage
82 {
83   /**
84    * This is a doubly-linked list.
85    */
86   struct PendingMessage *prev;
87
88   /**
89    * This is a doubly-linked list.
90    */
91   struct PendingMessage *next;
92
93   /**
94    * Size of the message.
95    */
96   size_t size;
97
98   /**
99    * request id
100    */
101   uint32_t r_id;
102
103   /**
104    * This message has been transmitted.  GNUNET_NO if the message is
105    * in the "pending" DLL, GNUNET_YES if it has been transmitted to
106    * the service via the current client connection.
107    */
108   int transmitted;
109
110 };
111
112
113 /**
114  * Connection to the GNS service.
115  */
116 struct GNUNET_GNS_Handle
117 {
118
119   /**
120    * Configuration to use.
121    */
122   const struct GNUNET_CONFIGURATION_Handle *cfg;
123
124   /**
125    * Socket (if available).
126    */
127   struct GNUNET_CLIENT_Connection *client;
128
129   /**
130    * Currently pending transmission request (or NULL).
131    */
132   struct GNUNET_CLIENT_TransmitHandle *th;
133
134   /**
135    * Head of linked list of shorten messages we would like to transmit.
136    */
137   struct PendingMessage *pending_head;
138
139   /**
140    * Tail of linked list of shorten messages we would like to transmit.
141    */
142   struct PendingMessage *pending_tail;
143
144   /**
145    * Head of linked list of lookup messages we would like to transmit.
146    */
147   struct GNUNET_GNS_LookupRequest *lookup_head;
148
149   /**
150    * Tail of linked list of lookup messages we would like to transmit.
151    */
152   struct GNUNET_GNS_LookupRequest *lookup_tail;
153
154   /**
155    * Reconnect task
156    */
157   struct GNUNET_SCHEDULER_Task * reconnect_task;
158
159   /**
160    * How long do we wait until we try to reconnect?
161    */
162   struct GNUNET_TIME_Relative reconnect_backoff;
163
164   /**
165    * Request Id generator.  Incremented by one for each request.
166    */
167   uint32_t r_id_gen;
168
169   /**
170    * Did we start our receive loop yet?
171    */
172   int in_receive;
173
174 };
175
176
177 /**
178  * Try to send messages from list of messages to send
179  * @param handle GNS_Handle
180  */
181 static void
182 process_pending_messages (struct GNUNET_GNS_Handle *handle);
183
184
185 /**
186  * Reconnect to GNS service.
187  *
188  * @param handle the handle to the GNS service
189  */
190 static void
191 reconnect (struct GNUNET_GNS_Handle *handle)
192 {
193   GNUNET_assert (NULL == handle->client);
194   LOG (GNUNET_ERROR_TYPE_DEBUG,
195        "Trying to connect to GNS\n");
196   handle->client = GNUNET_CLIENT_connect ("gns", handle->cfg);
197   GNUNET_assert (NULL != handle->client);
198   process_pending_messages (handle);
199 }
200
201
202 /**
203  * Reconnect to GNS
204  *
205  * @param cls the handle
206  */
207 static void
208 reconnect_task (void *cls)
209 {
210   struct GNUNET_GNS_Handle *handle = cls;
211
212   handle->reconnect_task = NULL;
213   reconnect (handle);
214 }
215
216
217 /**
218  * Disconnect from service and then reconnect.
219  *
220  * @param handle our handle
221  */
222 static void
223 force_reconnect (struct GNUNET_GNS_Handle *handle)
224 {
225   struct GNUNET_GNS_LookupRequest *lh;
226   struct PendingMessage *p;
227
228   GNUNET_CLIENT_disconnect (handle->client);
229   handle->client = NULL;
230   handle->in_receive = GNUNET_NO;
231   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
232   {
233     p = (struct PendingMessage *) &lh[1];
234     if (GNUNET_NO == p->transmitted)
235       continue;
236     p->transmitted = GNUNET_NO;
237     GNUNET_CONTAINER_DLL_insert (handle->pending_head,
238                                  handle->pending_tail,
239                                  p);
240   }
241   handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
242   handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
243                                                     &reconnect_task,
244                                                     handle);
245 }
246
247
248 /**
249  * Transmit the next pending message, called by notify_transmit_ready
250  *
251  * @param cls the closure
252  * @param size size of pending data
253  * @param buf buffer with pending data
254  * @return size data transmitted
255  */
256 static size_t
257 transmit_pending (void *cls, size_t size, void *buf);
258
259
260 /**
261  * Handler for messages received from the GNS service
262  *
263  * @param cls the 'struct GNUNET_GNS_Handle'
264  * @param msg the incoming message
265  */
266 static void
267 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
268
269
270 /**
271  * Try to send messages from list of messages to send
272  *
273  * @param handle the GNS handle
274  */
275 static void
276 process_pending_messages (struct GNUNET_GNS_Handle *handle)
277 {
278   struct PendingMessage *p = handle->pending_head;
279
280   if (NULL == handle->client)
281     return; /* wait for reconnect */
282   if (NULL != handle->th)
283     return; /* transmission request already pending */
284
285   while ((NULL != p) && (p->transmitted == GNUNET_YES))
286     p = p->next;
287   if (NULL == p)
288     return; /* no messages pending */
289
290   LOG (GNUNET_ERROR_TYPE_DEBUG,
291        "Trying to transmit %u bytes\n",
292        (unsigned int) p->size);
293   handle->th =
294     GNUNET_CLIENT_notify_transmit_ready (handle->client,
295                                          p->size,
296                                          GNUNET_TIME_UNIT_FOREVER_REL,
297                                          GNUNET_NO, &transmit_pending,
298                                          handle);
299   GNUNET_break (NULL != handle->th);
300 }
301
302
303 /**
304  * Transmit the next pending message, called by notify_transmit_ready
305  *
306  * @param cls the closure
307  * @param size size of pending data
308  * @param buf buffer with pending data
309  * @return size data transmitted
310  */
311 static size_t
312 transmit_pending (void *cls, size_t size, void *buf)
313 {
314   struct GNUNET_GNS_Handle *handle = cls;
315   char *cbuf = buf;
316   struct PendingMessage *p;
317   size_t tsize;
318
319   handle->th = NULL;
320   if ((0 == size) || (NULL == buf))
321   {
322     LOG (GNUNET_ERROR_TYPE_DEBUG,
323          "Transmission to GNS service failed!\n");
324     force_reconnect (handle);
325     return 0;
326   }
327   if (NULL == (p = handle->pending_head))
328     return 0;
329
330   tsize = 0;
331   while ((NULL != (p = handle->pending_head)) && (p->size <= size))
332   {
333     memcpy (&cbuf[tsize], &p[1], p->size);
334     tsize += p->size;
335     size -= p->size;
336     p->transmitted = GNUNET_YES;
337     GNUNET_CONTAINER_DLL_remove (handle->pending_head,
338                                  handle->pending_tail,
339                                  p);
340     if (GNUNET_YES != handle->in_receive)
341     {
342       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
343                              GNUNET_TIME_UNIT_FOREVER_REL);
344       handle->in_receive = GNUNET_YES;
345     }
346   }
347   LOG (GNUNET_ERROR_TYPE_DEBUG,
348        "Sending %u bytes\n",
349        (unsigned int) tsize);
350   process_pending_messages (handle);
351   return tsize;
352 }
353
354
355 /**
356  * Process a given reply to the lookup request
357  *
358  * @param qe a queue entry
359  * @param msg the lookup message received
360  */
361 static void
362 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
363                       const struct GNUNET_GNS_ClientLookupResultMessage *msg)
364 {
365   struct GNUNET_GNS_Handle *handle = qe->gns_handle;
366   struct PendingMessage *p = (struct PendingMessage *) &qe[1];
367   GNUNET_GNS_LookupResultProcessor proc;
368   void *proc_cls;
369   uint32_t rd_count = ntohl (msg->rd_count);
370   struct GNUNET_GNSRECORD_Data rd[rd_count];
371   size_t mlen;
372
373   if (GNUNET_YES != p->transmitted)
374   {
375     /* service send reply to query we never managed to send!? */
376     GNUNET_break (0);
377     force_reconnect (handle);
378     return;
379   }
380   mlen = ntohs (msg->header.size);
381   mlen -= sizeof (struct GNUNET_GNS_ClientLookupResultMessage);
382   proc = qe->lookup_proc;
383   proc_cls = qe->proc_cls;
384   GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
385   GNUNET_free (qe);
386   if (GNUNET_SYSERR == GNUNET_GNSRECORD_records_deserialize (mlen,
387                                                              (const char*) &msg[1],
388                                                              rd_count,
389                                                              rd))
390   {
391     LOG (GNUNET_ERROR_TYPE_ERROR,
392          _("Failed to deserialize lookup reply from GNS service!\n"));
393     proc (proc_cls, 0, NULL);
394   }
395   else
396   {
397     LOG (GNUNET_ERROR_TYPE_DEBUG,
398          "Received lookup reply from GNS service (%u records)\n",
399          (unsigned int) rd_count);
400     proc (proc_cls, rd_count, rd);
401   }
402 }
403
404
405 /**
406  * Handler for messages received from the GNS service
407  *
408  * @param cls the 'struct GNUNET_GNS_Handle'
409  * @param msg the incoming message
410  */
411 static void
412 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
413 {
414   struct GNUNET_GNS_Handle *handle = cls;
415   struct GNUNET_GNS_LookupRequest *lr;
416   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
417   uint32_t r_id;
418
419   if (NULL == msg)
420   {
421     force_reconnect (handle);
422     return;
423   }
424
425   GNUNET_CLIENT_receive (handle->client, &process_message, handle,
426                          GNUNET_TIME_UNIT_FOREVER_REL);
427   switch (ntohs (msg->type))
428   {
429   case GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT:
430     LOG (GNUNET_ERROR_TYPE_DEBUG,
431          "Got LOOKUP_RESULT msg\n");
432     if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
433     {
434       GNUNET_break (0);
435       force_reconnect (handle);
436       return;
437     }
438     lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
439     r_id = ntohl (lookup_msg->id);
440     for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
441       if (lr->r_id == r_id)
442       {
443         process_lookup_reply(lr, lookup_msg);
444         break;
445       }
446     break;
447   default:
448     GNUNET_break (0);
449     force_reconnect (handle);
450     return;
451   }
452 }
453
454
455 /**
456  * Initialize the connection with the GNS service.
457  *
458  * @param cfg configuration to use
459  * @return handle to the GNS service, or NULL on error
460  */
461 struct GNUNET_GNS_Handle *
462 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
463 {
464   struct GNUNET_GNS_Handle *handle;
465
466   handle = GNUNET_new (struct GNUNET_GNS_Handle);
467   handle->cfg = cfg;
468   reconnect (handle);
469   return handle;
470 }
471
472
473 /**
474  * Shutdown connection with the GNS service.
475  *
476  * @param handle handle of the GNS connection to stop
477  */
478 void
479 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
480 {
481   if (NULL != handle->client)
482   {
483     GNUNET_CLIENT_disconnect (handle->client);
484     handle->client = NULL;
485   }
486   if (NULL != handle->reconnect_task)
487   {
488     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
489     handle->reconnect_task = NULL;
490   }
491   GNUNET_assert (NULL == handle->lookup_head);
492   GNUNET_free (handle);
493 }
494
495
496 /**
497  * Cancel pending lookup request
498  *
499  * @param lr the lookup request to cancel
500  */
501 void
502 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
503 {
504   struct PendingMessage *p = (struct PendingMessage*) &lr[1];
505
506   GNUNET_assert (NULL != lr->gns_handle);
507   if (GNUNET_NO == p->transmitted)
508     GNUNET_CONTAINER_DLL_remove (lr->gns_handle->pending_head,
509                                  lr->gns_handle->pending_tail,
510                                  p);
511   GNUNET_CONTAINER_DLL_remove (lr->gns_handle->lookup_head,
512                                lr->gns_handle->lookup_tail,
513                                lr);
514   GNUNET_free (lr);
515 }
516
517
518 /**
519  * Perform an asynchronous lookup operation on the GNS.
520  *
521  * @param handle handle to the GNS service
522  * @param name the name to look up
523  * @param zone the zone to start the resolution in
524  * @param type the record type to look up
525  * @param options local options for the lookup
526  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
527  * @param proc processor to call on result
528  * @param proc_cls closure for @a proc
529  * @return handle to the get request
530  */
531 struct GNUNET_GNS_LookupRequest*
532 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
533                    const char *name,
534                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
535                    uint32_t type,
536                    enum GNUNET_GNS_LocalOptions options,
537                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
538                    GNUNET_GNS_LookupResultProcessor proc,
539                    void *proc_cls)
540 {
541   /* IPC to shorten gns names, return shorten_handle */
542   struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
543   struct GNUNET_GNS_LookupRequest *lr;
544   size_t msize;
545   struct PendingMessage *pending;
546
547   if (NULL == name)
548   {
549     GNUNET_break (0);
550     return NULL;
551   }
552   LOG (GNUNET_ERROR_TYPE_DEBUG,
553        "Trying to lookup `%s' in GNS\n",
554        name);
555   msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
556     + strlen (name) + 1;
557   if (msize > UINT16_MAX)
558   {
559     GNUNET_break (0);
560     return NULL;
561   }
562   lr = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupRequest) +
563                       sizeof (struct PendingMessage) + msize);
564   lr->gns_handle = handle;
565   lr->lookup_proc = proc;
566   lr->proc_cls = proc_cls;
567   lr->r_id = handle->r_id_gen++;
568   pending = (struct PendingMessage *)&lr[1];
569   pending->size = msize;
570   pending->r_id = lr->r_id;
571   GNUNET_CONTAINER_DLL_insert_tail (handle->lookup_head,
572                                     handle->lookup_tail, lr);
573
574   lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
575   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
576   lookup_msg->header.size = htons (msize);
577   lookup_msg->id = htonl (lr->r_id);
578   lookup_msg->options = htons ((uint16_t) options);
579   lookup_msg->zone = *zone;
580   lookup_msg->type = htonl (type);
581   if (NULL != shorten_zone_key)
582   {
583     lookup_msg->have_key = htons (GNUNET_YES);
584     lookup_msg->shorten_key = *shorten_zone_key;
585   }
586   memcpy (&lookup_msg[1], name, strlen (name) + 1);
587   GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
588                                     handle->pending_tail,
589                                     pending);
590   process_pending_messages (handle);
591   return lr;
592 }
593
594
595 /* end of gns_api.c */