bd77f378e2dcfe83d484ef7179924b3f1d310915
[oweals/gnunet.git] / src / gns / gns_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 /**
22  *
23  * @file gns/gns_api.c
24  * @brief library to access the GNS service
25  * @author Martin Schanzenbach
26  */
27
28 #include "platform.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_constants.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_dht_service.h"
35 #include "gns.h"
36 #include "gnunet_gns_service.h"
37
38 /* TODO into gnunet_protocols */
39 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP 23
40 #define GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT 24
41 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN 25
42 #define GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT 26
43 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH 27
44 #define GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT 28
45
46 /**
47  * A QueueEntry.
48  */
49 struct GNUNET_GNS_QueueEntry
50 {
51   /**
52    * DLL
53    */
54   struct GNUNET_GNS_QueueEntry *next;
55   
56   /**
57    * DLL
58    */
59   struct GNUNET_GNS_QueueEntry *prev;
60
61   /* request id */
62   uint32_t r_id;
63   
64   /* handle to gns */
65   struct GNUNET_GNS_Handle *gns_handle;
66   
67   /* processor to call on shorten result */
68   GNUNET_GNS_ShortenResultProcessor shorten_proc;
69   
70   /* processor to call on lookup result */
71   GNUNET_GNS_LookupResultProcessor lookup_proc;
72
73   /* processor to call on authority lookup result */
74   GNUNET_GNS_GetAuthResultProcessor auth_proc;
75   
76   /* processor closure */
77   void *proc_cls;
78   
79 };
80
81
82 /**
83  * Entry in our list of messages to be (re-)transmitted.
84  */
85 struct PendingMessage
86 {
87   /**
88    * This is a doubly-linked list.
89    */
90   struct PendingMessage *prev;
91
92   /**
93    * This is a doubly-linked list.
94    */
95   struct PendingMessage *next;
96
97   /**
98    * Size of the message.
99    */
100   size_t size;
101
102 };
103
104
105 /**
106  * Connection to the GNS service.
107  */
108 struct GNUNET_GNS_Handle
109 {
110
111   /**
112    * Configuration to use.
113    */
114   const struct GNUNET_CONFIGURATION_Handle *cfg;
115
116   /**
117    * Socket (if available).
118    */
119   struct GNUNET_CLIENT_Connection *client;
120
121   /**
122    * Currently pending transmission request (or NULL).
123    */
124   struct GNUNET_CLIENT_TransmitHandle *th;
125   
126   uint32_t r_id;
127   
128   /**
129    * Head of linked list of shorten messages we would like to transmit.
130    */
131   struct PendingMessage *pending_head;
132
133   /**
134    * Tail of linked list of shorten messages we would like to transmit.
135    */
136   struct PendingMessage *pending_tail;
137   
138   /**
139    * Head of linked list of shorten messages we would like to transmit.
140    */
141   struct GNUNET_GNS_QueueEntry *shorten_head;
142
143   /**
144    * Tail of linked list of shorten messages we would like to transmit.
145    */
146   struct GNUNET_GNS_QueueEntry *shorten_tail;
147   
148   /**
149    * Head of linked list of lookup messages we would like to transmit.
150    */
151   struct GNUNET_GNS_QueueEntry *lookup_head;
152
153   /**
154    * Tail of linked list of lookup messages we would like to transmit.
155    */
156   struct GNUNET_GNS_QueueEntry *lookup_tail;
157   
158   /**
159    * Head of linked list of authority lookup messages we would like to transmit.
160    */
161   struct GNUNET_GNS_QueueEntry *get_auth_head;
162
163   /**
164    * Tail of linked list of authority lookup messages we would like to transmit.
165    */
166   struct GNUNET_GNS_QueueEntry *get_auth_tail;
167
168   /**
169    * Reconnect task
170    */
171   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
172
173   /**
174    * Did we start our receive loop yet?
175    */
176   int in_receive;
177
178   /**
179    * Reconnect necessary
180    */
181   int reconnect;
182 };
183
184 /**
185  * Try to send messages from list of messages to send
186  * @param handle GNS_Handle
187  */
188 static void
189 process_pending_messages (struct GNUNET_GNS_Handle *handle);
190
191
192 /**
193  * Reconnect to GNS service.
194  *
195  * @param h the handle to the namestore service
196  */
197 static void
198 reconnect (struct GNUNET_GNS_Handle *h)
199 {
200   GNUNET_assert (NULL == h->client);
201   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
202              "Trying to connect to GNS...\n");
203   h->client = GNUNET_CLIENT_connect ("gns", h->cfg);
204   GNUNET_assert (NULL != h->client);
205 }
206
207 /**
208  * Reconnect to GNS
209  *
210  * @param cls the handle
211  * @param tc task context
212  */
213 static void
214 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 {
216   struct GNUNET_GNS_Handle *h = cls;
217
218   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
219   reconnect (h);
220 }
221
222
223 /**
224  * Disconnect from service and then reconnect.
225  *
226  * @param h our handle
227  */
228 static void
229 force_reconnect (struct GNUNET_GNS_Handle *h)
230 {
231   h->reconnect = GNUNET_NO;
232   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
233   h->client = NULL;
234   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
235                                                     &reconnect_task,
236                                                     h);
237 }
238
239 /**
240  * Transmit the next pending message, called by notify_transmit_ready
241  */
242 static size_t
243 transmit_pending (void *cls, size_t size, void *buf);
244
245 /**
246  * Handler for messages received from the GNS service
247  *
248  * @param cls the 'struct GNUNET_GNS_Handle'
249  * @param msg the incoming message
250  */
251 static void
252 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
253
254 /**
255  * Try to send messages from list of messages to send
256  */
257 static void
258 process_pending_messages (struct GNUNET_GNS_Handle *handle)
259 {
260   struct PendingMessage *p;
261
262   if (handle->client == NULL)
263   {
264     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265          "process_pending_messages called, but client is null\n");
266     return;
267   }
268   
269   if (handle->th != NULL)
270     return;
271   
272   if (NULL == (p = handle->pending_head))
273     return;
274   
275   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
276              "Trying to transmit %d bytes...\n", p->size);
277
278   handle->th =
279     GNUNET_CLIENT_notify_transmit_ready (handle->client,
280                                          p->size,
281                                          GNUNET_TIME_UNIT_FOREVER_REL,
282                                          GNUNET_NO, &transmit_pending,
283                                          handle);
284   if (NULL != handle->th)
285     return;
286
287   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288               "notify_transmit_ready returned NULL!\n");
289 }
290
291
292 /**
293  * Transmit the next pending message, called by notify_transmit_ready
294  */
295 static size_t
296 transmit_pending (void *cls, size_t size, void *buf)
297 {
298   struct GNUNET_GNS_Handle *handle = cls;
299   struct PendingMessage *p;
300   size_t tsize;
301   char *cbuf;
302
303   handle->th = NULL;
304   
305   if ((size == 0) || (buf == NULL))
306   {
307     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308          "Transmission to GNS service failed!\n");
309     force_reconnect(handle);
310     return 0;
311   }
312   
313   tsize = 0;
314   cbuf = buf;
315
316   if (NULL == (p = handle->pending_head))
317     return 0;
318
319   while ((NULL != (p = handle->pending_head)) && (p->size <= size))
320   {
321     memcpy (&cbuf[tsize], &p[1], p->size);
322     tsize += p->size;
323     size -= p->size;
324     GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail, p);
325     if (GNUNET_YES != handle->in_receive)
326     {
327       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
328                              GNUNET_TIME_UNIT_FOREVER_REL);
329       handle->in_receive = GNUNET_YES;
330     }
331     GNUNET_free(p);
332   }
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Sending %d bytes\n", tsize);
336
337   process_pending_messages(handle);
338   return tsize;
339 }
340
341 /**
342  * Process a given reply that might match the given
343  * request.
344  *
345  * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
346  * @param key query of the request
347  * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
348  */
349 static void
350 process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
351                        const struct GNUNET_GNS_ClientShortenResultMessage *msg)
352 {
353   struct GNUNET_GNS_Handle *h = qe->gns_handle;
354   const char *short_name;
355
356   GNUNET_CONTAINER_DLL_remove(h->shorten_head, h->shorten_tail, qe);
357
358   short_name = (char*)(&msg[1]);
359
360   if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
361       sizeof (struct GNUNET_GNS_ClientShortenResultMessage))
362   {
363     GNUNET_break (0);
364     force_reconnect (h);
365     return;
366   }
367   
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369               "Received shortened reply `%s' from GNS service\n",
370               short_name);
371   
372   GNUNET_CLIENT_receive (h->client, &process_message, h,
373                          GNUNET_TIME_UNIT_FOREVER_REL);
374   qe->shorten_proc(qe->proc_cls, short_name);
375
376 }
377
378
379 /**
380  * Process a given reply that might match the given
381  * request.
382  *
383  * @param qe the handle to the request
384  * @param msg the message to process
385  */
386 static void
387 process_get_auth_reply (struct GNUNET_GNS_QueueEntry *qe,
388                        const struct GNUNET_GNS_ClientGetAuthResultMessage *msg)
389 {
390   struct GNUNET_GNS_Handle *h = qe->gns_handle;
391   const char *auth_name;
392
393   GNUNET_CONTAINER_DLL_remove(h->get_auth_head, h->get_auth_tail, qe);
394
395   auth_name = (char*)(&msg[1]);
396
397   if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
398       sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
399   {
400     GNUNET_break (0);
401     force_reconnect (h);
402     return;
403   }
404   
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406               "Received GET_AUTH reply `%s' from GNS service\n",
407               auth_name);
408   
409   GNUNET_CLIENT_receive (h->client, &process_message, h,
410                          GNUNET_TIME_UNIT_FOREVER_REL);
411   qe->auth_proc(qe->proc_cls, auth_name);
412
413 }
414 /**
415  * Process a given reply to the lookup request
416  *
417  * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
418  * @param key query of the request
419  * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
420  * @return GNUNET_YES to continue to iterate over all results,
421  *         GNUNET_NO if the reply is malformed
422  */
423 static void
424 process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe,
425                       const struct GNUNET_GNS_ClientLookupResultMessage *msg)
426 {
427   struct GNUNET_GNS_Handle *h = qe->gns_handle;
428   int rd_count = ntohl(msg->rd_count);
429   size_t len = ntohs (((struct GNUNET_MessageHeader*)msg)->size);
430   struct GNUNET_NAMESTORE_RecordData rd[rd_count];
431
432   GNUNET_CONTAINER_DLL_remove(h->lookup_head, h->lookup_tail, qe);
433
434   if (len < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
435   {
436     GNUNET_break (0);
437     force_reconnect (h);
438     return;
439   }
440
441   len -= sizeof(struct GNUNET_GNS_ClientLookupResultMessage);
442
443   GNUNET_NAMESTORE_records_deserialize (len, (char*)&msg[1],
444                                         rd_count,
445                                         rd);
446   
447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
448               "Received lookup reply from GNS service (count=%d)\n",
449               ntohl(msg->rd_count));
450   
451   GNUNET_CLIENT_receive (h->client, &process_message, h,
452                          GNUNET_TIME_UNIT_FOREVER_REL);
453   qe->lookup_proc(qe->proc_cls, rd_count, rd);
454 }
455
456 /**
457  * Handler for messages received from the GNS service
458  *
459  * @param cls the 'struct GNUNET_GNS_Handle'
460  * @param msg the incoming message
461  */
462 static void
463 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
464 {
465   struct GNUNET_GNS_Handle *handle = cls;
466   struct GNUNET_GNS_QueueEntry *qe;
467   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
468   const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
469   const struct GNUNET_GNS_ClientGetAuthResultMessage *get_auth_msg;
470   uint16_t type;
471   uint32_t r_id;
472   
473   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
474               "Got message\n");
475   if (msg == NULL)
476   {
477     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
478          "Error receiving data from GNS service, reconnecting\n");
479     force_reconnect (handle);
480     return;
481   }
482
483   type = ntohs (msg->type);
484
485   if (type == GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT)
486   {
487     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488                 "Got lookup msg\n");
489     lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
490     r_id = ntohl (lookup_msg->id);
491     
492     if (r_id > handle->r_id)
493     {
494       /** no request found */
495       GNUNET_break_op (0);
496       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
497                              GNUNET_TIME_UNIT_FOREVER_REL);
498       return;
499     }
500
501     for (qe = handle->lookup_head; qe != NULL; qe = qe->next)
502     {
503       if (qe->r_id == r_id)
504         break;
505     }
506     if (qe)
507       process_lookup_reply(qe, lookup_msg);
508
509
510   }
511   else if (type == GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT)
512   {
513     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514                 "Got SHORTEN_RESULT msg\n");
515     shorten_msg = (struct GNUNET_GNS_ClientShortenResultMessage *) msg;
516     
517     r_id = ntohl (shorten_msg->id);
518     
519     if (r_id > handle->r_id)
520     {
521       /** no request found */
522       GNUNET_break_op (0);
523       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
524                              GNUNET_TIME_UNIT_FOREVER_REL);
525       return;
526     }
527
528     for (qe = handle->shorten_head; qe != NULL; qe = qe->next)
529     {
530       if (qe->r_id == r_id)
531         break;
532     }
533     if (qe)
534       process_shorten_reply(qe, shorten_msg);
535   }
536   else if (type == GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT)
537   {
538     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539                 "Got GET_AUTH_RESULT msg\n");
540     get_auth_msg = (struct GNUNET_GNS_ClientGetAuthResultMessage *) msg;
541
542     r_id = ntohl (get_auth_msg->id);
543
544     if (r_id > handle->r_id)
545     {
546       /** no request found */
547       GNUNET_break_op (0);
548       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
549                              GNUNET_TIME_UNIT_FOREVER_REL);
550       return;
551     }
552
553     for (qe = handle->get_auth_head; qe != NULL; qe = qe->next)
554     {
555       if (qe->r_id == r_id)
556         break;
557     }
558     if (qe)
559       process_get_auth_reply(qe, get_auth_msg);
560   }
561
562
563   if (GNUNET_YES == handle->reconnect)
564     force_reconnect (handle);
565   
566 }
567
568
569 /**
570  * Initialize the connection with the GNS service.
571  *
572  * @param cfg configuration to use
573  * @param ht_len size of the internal hash table to use for parallel requests
574  * @return handle to the GNS service, or NULL on error
575  */
576 struct GNUNET_GNS_Handle *
577 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
578 {
579   struct GNUNET_GNS_Handle *handle;
580
581   handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle));
582   handle->reconnect = GNUNET_NO;
583   handle->cfg = cfg;
584   reconnect (handle);
585   //handle->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, handle);
586   handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
587   handle->r_id = 0;
588   handle->in_receive = GNUNET_NO;
589   return handle;
590 }
591
592
593 /**
594  * Shutdown connection with the GNS service.
595  *
596  * @param handle handle of the GNS connection to stop
597  */
598 void
599 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *h)
600 {
601   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
602   if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
603   {
604     GNUNET_SCHEDULER_cancel (h->reconnect_task);
605     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
606   }
607   //GNUNET_free(h);
608   /* disco from GNS */
609 }
610
611 /*
612  * Helper function to generate request ids
613  * 
614  * @param h handle
615  * @return a new id
616  */
617 static uint32_t
618 get_request_id (struct GNUNET_GNS_Handle *h)
619 {
620   uint32_t r_id = h->r_id;
621   h->r_id++;
622   return r_id;
623 }
624
625 /**
626  * Perform an asynchronous Lookup operation on the GNS.
627  *
628  * @param handle handle to the GNS service
629  * @param name the name to look up
630  * @param iter function to call on each result
631  * @param iter_cls closure for iter
632  * @return handle to stop the async get
633  */
634 struct GNUNET_GNS_QueueEntry *
635 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
636                    const char * name,
637                    enum GNUNET_GNS_RecordType type,
638                    GNUNET_GNS_LookupResultProcessor proc,
639                    void *proc_cls)
640 {
641   /* IPC to shorten gns names, return shorten_handle */
642   struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
643   struct GNUNET_GNS_QueueEntry *qe;
644   size_t msize;
645   struct PendingMessage *pending;
646
647   if (NULL == name)
648   {
649     return NULL;
650   }
651
652   msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name) + 1;
653   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to lookup %s in GNS\n", name);
654
655   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
656   qe->gns_handle = handle;
657   qe->lookup_proc = proc;
658   qe->proc_cls = proc_cls;
659   qe->r_id = get_request_id(handle);
660   GNUNET_CONTAINER_DLL_insert_tail(handle->lookup_head,
661                                    handle->lookup_tail, qe);
662
663   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
664   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
665   
666   pending->size = msize;
667
668   lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
669   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
670   lookup_msg->header.size = htons (msize);
671   lookup_msg->id = htonl(qe->r_id);
672   lookup_msg->type = htonl(type);
673
674   memcpy(&lookup_msg[1], name, strlen(name));
675
676   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
677                                pending);
678   
679   process_pending_messages (handle);
680   return qe;
681 }
682
683
684 /**
685  * Perform a name shortening operation on the GNS.
686  *
687  * @param handle handle to the GNS service
688  * @param name the name to look up
689  * @param proc function to call on result
690  * @param proc_cls closure for processor
691  * @return handle to the operation
692  */
693 struct GNUNET_GNS_QueueEntry *
694 GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
695                     const char * name,
696                     GNUNET_GNS_ShortenResultProcessor proc,
697                     void *proc_cls)
698 {
699   /* IPC to shorten gns names, return shorten_handle */
700   struct GNUNET_GNS_ClientShortenMessage *shorten_msg;
701   struct GNUNET_GNS_QueueEntry *qe;
702   size_t msize;
703   struct PendingMessage *pending;
704
705   if (NULL == name)
706   {
707     return NULL;
708   }
709
710   msize = sizeof (struct GNUNET_GNS_ClientShortenMessage) + strlen(name) + 1;
711   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to shorten %s in GNS\n", name);
712
713   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
714   qe->gns_handle = handle;
715   qe->shorten_proc = proc;
716   qe->proc_cls = proc_cls;
717   qe->r_id = get_request_id(handle);
718   GNUNET_CONTAINER_DLL_insert_tail(handle->shorten_head,
719                                    handle->shorten_tail, qe);
720
721   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
722   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
723   
724   pending->size = msize;
725
726   shorten_msg = (struct GNUNET_GNS_ClientShortenMessage *) &pending[1];
727   shorten_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_SHORTEN);
728   shorten_msg->header.size = htons (msize);
729   shorten_msg->id = htonl(qe->r_id);
730
731   memcpy(&shorten_msg[1], name, strlen(name));
732
733   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
734                                pending);
735   
736   process_pending_messages (handle);
737   return qe;
738 }
739
740
741 /**
742  * Perform an authority lookup for a given name.
743  *
744  * @param handle handle to the GNS service
745  * @param name the name to look up authority for
746  * @param proc function to call on result
747  * @param proc_cls closure for processor
748  * @return handle to the operation
749  */
750 struct GNUNET_GNS_QueueEntry *
751 GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
752                     const char * name,
753                     GNUNET_GNS_GetAuthResultProcessor proc,
754                     void *proc_cls)
755 {
756   struct GNUNET_GNS_ClientGetAuthMessage *get_auth_msg;
757   struct GNUNET_GNS_QueueEntry *qe;
758   size_t msize;
759   struct PendingMessage *pending;
760
761   if (NULL == name)
762   {
763     return NULL;
764   }
765
766   msize = sizeof (struct GNUNET_GNS_ClientGetAuthMessage) + strlen(name) + 1;
767   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768               "Trying to look up authority for %s in GNS\n", name);
769
770   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
771   qe->gns_handle = handle;
772   qe->auth_proc = proc;
773   qe->proc_cls = proc_cls;
774   qe->r_id = get_request_id(handle);
775   GNUNET_CONTAINER_DLL_insert_tail(handle->get_auth_head,
776                                    handle->get_auth_tail, qe);
777
778   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
779   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
780   
781   pending->size = msize;
782
783   get_auth_msg = (struct GNUNET_GNS_ClientGetAuthMessage *) &pending[1];
784   get_auth_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_GET_AUTH);
785   get_auth_msg->header.size = htons (msize);
786   get_auth_msg->id = htonl(qe->r_id);
787
788   memcpy(&get_auth_msg[1], name, strlen(name));
789
790   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
791                                pending);
792   
793   process_pending_messages (handle);
794   return qe;
795 }
796
797
798 /* end of gns_api.c */