- fixes, intendation
[oweals/gnunet.git] / src / gns / gns_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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  * @param tc task context
207  */
208 static void
209 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
210 {
211   struct GNUNET_GNS_Handle *handle = cls;
212
213   handle->reconnect_task = NULL;
214   reconnect (handle);
215 }
216
217
218 /**
219  * Disconnect from service and then reconnect.
220  *
221  * @param handle our handle
222  */
223 static void
224 force_reconnect (struct GNUNET_GNS_Handle *handle)
225 {
226   struct GNUNET_GNS_LookupRequest *lh;
227   struct PendingMessage *p;
228
229   GNUNET_CLIENT_disconnect (handle->client);
230   handle->client = NULL;
231   handle->in_receive = GNUNET_NO;
232   for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
233   {
234     p = (struct PendingMessage *) &lh[1];
235     if (GNUNET_NO == p->transmitted)
236       continue;
237     p->transmitted = GNUNET_NO;
238     GNUNET_CONTAINER_DLL_insert (handle->pending_head,
239                                  handle->pending_tail,
240                                  p);
241   }
242   handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
243   handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
244                                                     &reconnect_task,
245                                                     handle);
246 }
247
248
249 /**
250  * Transmit the next pending message, called by notify_transmit_ready
251  *
252  * @param cls the closure
253  * @param size size of pending data
254  * @param buf buffer with pending data
255  * @return size data transmitted
256  */
257 static size_t
258 transmit_pending (void *cls, size_t size, void *buf);
259
260
261 /**
262  * Handler for messages received from the GNS service
263  *
264  * @param cls the 'struct GNUNET_GNS_Handle'
265  * @param msg the incoming message
266  */
267 static void
268 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
269
270
271 /**
272  * Try to send messages from list of messages to send
273  *
274  * @param handle the GNS handle
275  */
276 static void
277 process_pending_messages (struct GNUNET_GNS_Handle *handle)
278 {
279   struct PendingMessage *p = handle->pending_head;
280
281   if (NULL == handle->client)
282     return; /* wait for reconnect */
283   if (NULL != handle->th)
284     return; /* transmission request already pending */
285
286   while ((NULL != p) && (p->transmitted == GNUNET_YES))
287     p = p->next;
288   if (NULL == p)
289     return; /* no messages pending */
290
291   LOG (GNUNET_ERROR_TYPE_DEBUG,
292        "Trying to transmit %u bytes\n",
293        (unsigned int) p->size);
294   handle->th =
295     GNUNET_CLIENT_notify_transmit_ready (handle->client,
296                                          p->size,
297                                          GNUNET_TIME_UNIT_FOREVER_REL,
298                                          GNUNET_NO, &transmit_pending,
299                                          handle);
300   GNUNET_break (NULL != handle->th);
301 }
302
303
304 /**
305  * Transmit the next pending message, called by notify_transmit_ready
306  *
307  * @param cls the closure
308  * @param size size of pending data
309  * @param buf buffer with pending data
310  * @return size data transmitted
311  */
312 static size_t
313 transmit_pending (void *cls, size_t size, void *buf)
314 {
315   struct GNUNET_GNS_Handle *handle = cls;
316   char *cbuf = buf;
317   struct PendingMessage *p;
318   size_t tsize;
319
320   handle->th = NULL;
321   if ((0 == size) || (NULL == buf))
322   {
323     LOG (GNUNET_ERROR_TYPE_DEBUG,
324          "Transmission to GNS service failed!\n");
325     force_reconnect (handle);
326     return 0;
327   }
328   if (NULL == (p = handle->pending_head))
329     return 0;
330
331   tsize = 0;
332   while ((NULL != (p = handle->pending_head)) && (p->size <= size))
333   {
334     memcpy (&cbuf[tsize], &p[1], p->size);
335     tsize += p->size;
336     size -= p->size;
337     p->transmitted = GNUNET_YES;
338     GNUNET_CONTAINER_DLL_remove (handle->pending_head,
339                                  handle->pending_tail,
340                                  p);
341     if (GNUNET_YES != handle->in_receive)
342     {
343       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
344                              GNUNET_TIME_UNIT_FOREVER_REL);
345       handle->in_receive = GNUNET_YES;
346     }
347   }
348   LOG (GNUNET_ERROR_TYPE_DEBUG,
349        "Sending %u bytes\n",
350        (unsigned int) tsize);
351   process_pending_messages (handle);
352   return tsize;
353 }
354
355
356 /**
357  * Process a given reply to the lookup request
358  *
359  * @param qe a queue entry
360  * @param msg the lookup message received
361  */
362 static void
363 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
364                       const struct GNUNET_GNS_ClientLookupResultMessage *msg)
365 {
366   struct GNUNET_GNS_Handle *handle = qe->gns_handle;
367   struct PendingMessage *p = (struct PendingMessage *) &qe[1];
368   GNUNET_GNS_LookupResultProcessor proc;
369   void *proc_cls;
370   uint32_t rd_count = ntohl (msg->rd_count);
371   struct GNUNET_GNSRECORD_Data rd[rd_count];
372   size_t mlen;
373
374   if (GNUNET_YES != p->transmitted)
375   {
376     /* service send reply to query we never managed to send!? */
377     GNUNET_break (0);
378     force_reconnect (handle);
379     return;
380   }
381   mlen = ntohs (msg->header.size);
382   mlen -= sizeof (struct GNUNET_GNS_ClientLookupResultMessage);
383   proc = qe->lookup_proc;
384   proc_cls = qe->proc_cls;
385   GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
386   GNUNET_free (qe);
387   if (GNUNET_SYSERR == GNUNET_GNSRECORD_records_deserialize (mlen,
388                                                              (const char*) &msg[1],
389                                                              rd_count,
390                                                              rd))
391   {
392     LOG (GNUNET_ERROR_TYPE_ERROR,
393          _("Failed to deserialize lookup reply from GNS service!\n"));
394     proc (proc_cls, 0, NULL);
395   }
396   else
397   {
398     LOG (GNUNET_ERROR_TYPE_DEBUG,
399          "Received lookup reply from GNS service (%u records)\n",
400          (unsigned int) rd_count);
401     proc (proc_cls, rd_count, rd);
402   }
403 }
404
405
406 /**
407  * Handler for messages received from the GNS service
408  *
409  * @param cls the 'struct GNUNET_GNS_Handle'
410  * @param msg the incoming message
411  */
412 static void
413 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
414 {
415   struct GNUNET_GNS_Handle *handle = cls;
416   struct GNUNET_GNS_LookupRequest *lr;
417   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
418   uint32_t r_id;
419
420   if (NULL == msg)
421   {
422     force_reconnect (handle);
423     return;
424   }
425
426   GNUNET_CLIENT_receive (handle->client, &process_message, handle,
427                          GNUNET_TIME_UNIT_FOREVER_REL);
428   switch (ntohs (msg->type))
429   {
430   case GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT:
431     LOG (GNUNET_ERROR_TYPE_DEBUG,
432          "Got LOOKUP_RESULT msg\n");
433     if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
434     {
435       GNUNET_break (0);
436       force_reconnect (handle);
437       return;
438     }
439     lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
440     r_id = ntohl (lookup_msg->id);
441     for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
442       if (lr->r_id == r_id)
443       {
444         process_lookup_reply(lr, lookup_msg);
445         break;
446       }
447     break;
448   default:
449     GNUNET_break (0);
450     force_reconnect (handle);
451     return;
452   }
453 }
454
455
456 /**
457  * Initialize the connection with the GNS service.
458  *
459  * @param cfg configuration to use
460  * @return handle to the GNS service, or NULL on error
461  */
462 struct GNUNET_GNS_Handle *
463 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
464 {
465   struct GNUNET_GNS_Handle *handle;
466
467   handle = GNUNET_new (struct GNUNET_GNS_Handle);
468   handle->cfg = cfg;
469   reconnect (handle);
470   return handle;
471 }
472
473
474 /**
475  * Shutdown connection with the GNS service.
476  *
477  * @param handle handle of the GNS connection to stop
478  */
479 void
480 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
481 {
482   if (NULL != handle->client)
483   {
484     GNUNET_CLIENT_disconnect (handle->client);
485     handle->client = NULL;
486   }
487   if (NULL != handle->reconnect_task)
488   {
489     GNUNET_SCHEDULER_cancel (handle->reconnect_task);
490     handle->reconnect_task = NULL;
491   }
492   GNUNET_assert (NULL == handle->lookup_head);
493   GNUNET_free (handle);
494 }
495
496
497 /**
498  * Cancel pending lookup request
499  *
500  * @param lr the lookup request to cancel
501  */
502 void
503 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
504 {
505   struct PendingMessage *p = (struct PendingMessage*) &lr[1];
506
507   GNUNET_assert (NULL != lr->gns_handle);
508   if (GNUNET_NO == p->transmitted)
509     GNUNET_CONTAINER_DLL_remove (lr->gns_handle->pending_head,
510                                  lr->gns_handle->pending_tail,
511                                  p);
512   GNUNET_CONTAINER_DLL_remove (lr->gns_handle->lookup_head,
513                                lr->gns_handle->lookup_tail,
514                                lr);
515   GNUNET_free (lr);
516 }
517
518
519 /**
520  * Perform an asynchronous lookup operation on the GNS.
521  *
522  * @param handle handle to the GNS service
523  * @param name the name to look up
524  * @param zone the zone to start the resolution in
525  * @param type the record type to look up
526  * @param options local options for the lookup
527  * @param shorten_zone_key the private key of the shorten zone (can be NULL)
528  * @param proc processor to call on result
529  * @param proc_cls closure for @a proc
530  * @return handle to the get request
531  */
532 struct GNUNET_GNS_LookupRequest*
533 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
534                    const char *name,
535                    const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
536                    uint32_t type,
537                    enum GNUNET_GNS_LocalOptions options,
538                    const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
539                    GNUNET_GNS_LookupResultProcessor proc,
540                    void *proc_cls)
541 {
542   /* IPC to shorten gns names, return shorten_handle */
543   struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
544   struct GNUNET_GNS_LookupRequest *lr;
545   size_t msize;
546   struct PendingMessage *pending;
547
548   if (NULL == name)
549   {
550     GNUNET_break (0);
551     return NULL;
552   }
553   LOG (GNUNET_ERROR_TYPE_DEBUG,
554        "Trying to lookup `%s' in GNS\n",
555        name);
556   msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
557     + strlen (name) + 1;
558   if (msize > UINT16_MAX)
559   {
560     GNUNET_break (0);
561     return NULL;
562   }
563   lr = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupRequest) +
564                       sizeof (struct PendingMessage) + msize);
565   lr->gns_handle = handle;
566   lr->lookup_proc = proc;
567   lr->proc_cls = proc_cls;
568   lr->r_id = handle->r_id_gen++;
569   pending = (struct PendingMessage *)&lr[1];
570   pending->size = msize;
571   pending->r_id = lr->r_id;
572   GNUNET_CONTAINER_DLL_insert_tail (handle->lookup_head,
573                                     handle->lookup_tail, lr);
574
575   lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
576   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
577   lookup_msg->header.size = htons (msize);
578   lookup_msg->id = htonl (lr->r_id);
579   lookup_msg->options = htons ((uint16_t) options);
580   lookup_msg->zone = *zone;
581   lookup_msg->type = htonl (type);
582   if (NULL != shorten_zone_key)
583   {
584     lookup_msg->have_key = htons (GNUNET_YES);
585     lookup_msg->shorten_key = *shorten_zone_key;
586   }
587   memcpy (&lookup_msg[1], name, strlen (name) + 1);
588   GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
589                                     handle->pending_tail,
590                                     pending);
591   process_pending_messages (handle);
592   return lr;
593 }
594
595
596 /* end of gns_api.c */