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