-fix
[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_CLIENT_receive (h->client, &process_message, h,
444                          GNUNET_TIME_UNIT_FOREVER_REL);
445   if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (len,
446                                                              (char*)&msg[1],
447                                                              rd_count,
448                                                              rd))
449   {
450     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
451                 "Failed to serialize lookup reply from GNS service!\n");
452     qe->lookup_proc(qe->proc_cls, 0, NULL);
453   }
454   else
455   {
456   
457     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
458                 "Received lookup reply from GNS service (count=%d)\n",
459                 ntohl(msg->rd_count));
460     qe->lookup_proc(qe->proc_cls, rd_count, rd);
461   }
462 }
463
464 /**
465  * Handler for messages received from the GNS service
466  *
467  * @param cls the 'struct GNUNET_GNS_Handle'
468  * @param msg the incoming message
469  */
470 static void
471 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
472 {
473   struct GNUNET_GNS_Handle *handle = cls;
474   struct GNUNET_GNS_QueueEntry *qe;
475   const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
476   const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
477   const struct GNUNET_GNS_ClientGetAuthResultMessage *get_auth_msg;
478   uint16_t type;
479   uint32_t r_id;
480   
481   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
482               "Got message\n");
483   if (msg == NULL)
484   {
485     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486          "Error receiving data from GNS service, reconnecting\n");
487     force_reconnect (handle);
488     return;
489   }
490
491   type = ntohs (msg->type);
492
493   if (type == GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT)
494   {
495     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496                 "Got lookup msg\n");
497     lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
498     r_id = ntohl (lookup_msg->id);
499     
500     if (r_id > handle->r_id)
501     {
502       /** no request found */
503       GNUNET_break_op (0);
504       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
505                              GNUNET_TIME_UNIT_FOREVER_REL);
506       return;
507     }
508
509     for (qe = handle->lookup_head; qe != NULL; qe = qe->next)
510     {
511       if (qe->r_id == r_id)
512         break;
513     }
514     if (qe)
515       process_lookup_reply(qe, lookup_msg);
516     
517     return;
518
519   }
520   else if (type == GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT)
521   {
522     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523                 "Got SHORTEN_RESULT msg\n");
524     shorten_msg = (struct GNUNET_GNS_ClientShortenResultMessage *) msg;
525     
526     r_id = ntohl (shorten_msg->id);
527     
528     if (r_id > handle->r_id)
529     {
530       /** no request found */
531       GNUNET_break_op (0);
532       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
533                              GNUNET_TIME_UNIT_FOREVER_REL);
534       return;
535     }
536
537     for (qe = handle->shorten_head; qe != NULL; qe = qe->next)
538     {
539       if (qe->r_id == r_id)
540         break;
541     }
542     if (qe)
543       process_shorten_reply(qe, shorten_msg);
544     return;
545   }
546   else if (type == GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT)
547   {
548     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549                 "Got GET_AUTH_RESULT msg\n");
550     get_auth_msg = (struct GNUNET_GNS_ClientGetAuthResultMessage *) msg;
551
552     r_id = ntohl (get_auth_msg->id);
553
554     if (r_id > handle->r_id)
555     {
556       /** no request found */
557       GNUNET_break_op (0);
558       GNUNET_CLIENT_receive (handle->client, &process_message, handle,
559                              GNUNET_TIME_UNIT_FOREVER_REL);
560       return;
561     }
562
563     for (qe = handle->get_auth_head; qe != NULL; qe = qe->next)
564     {
565       if (qe->r_id == r_id)
566         break;
567     }
568     if (qe)
569       process_get_auth_reply(qe, get_auth_msg);
570     return;
571   }
572
573
574   if (GNUNET_YES == handle->reconnect)
575     force_reconnect (handle);
576   
577 }
578
579
580 /**
581  * Initialize the connection with the GNS service.
582  *
583  * @param cfg configuration to use
584  * @param ht_len size of the internal hash table to use for parallel requests
585  * @return handle to the GNS service, or NULL on error
586  */
587 struct GNUNET_GNS_Handle *
588 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
589 {
590   struct GNUNET_GNS_Handle *handle;
591
592   handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle));
593   handle->reconnect = GNUNET_NO;
594   handle->cfg = cfg;
595   reconnect (handle);
596   //handle->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, handle);
597   handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
598   handle->r_id = 0;
599   handle->in_receive = GNUNET_NO;
600   return handle;
601 }
602
603
604 /**
605  * Shutdown connection with the GNS service.
606  *
607  * @param handle handle of the GNS connection to stop
608  */
609 void
610 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *h)
611 {
612   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
613   if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
614   {
615     GNUNET_SCHEDULER_cancel (h->reconnect_task);
616     h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
617   }
618   GNUNET_free(h);
619   /* disco from GNS */
620 }
621
622 /*
623  * Helper function to generate request ids
624  * 
625  * @param h handle
626  * @return a new id
627  */
628 static uint32_t
629 get_request_id (struct GNUNET_GNS_Handle *h)
630 {
631   uint32_t r_id = h->r_id;
632   h->r_id++;
633   return r_id;
634 }
635
636 /**
637  * Perform an asynchronous Lookup operation on the GNS.
638  *
639  * @param handle handle to the GNS service
640  * @param name the name to look up
641  * @param iter function to call on each result
642  * @param iter_cls closure for iter
643  * @return handle to stop the async get
644  */
645 struct GNUNET_GNS_QueueEntry *
646 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
647                    const char * name,
648                    enum GNUNET_GNS_RecordType type,
649                    GNUNET_GNS_LookupResultProcessor proc,
650                    void *proc_cls)
651 {
652   /* IPC to shorten gns names, return shorten_handle */
653   struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
654   struct GNUNET_GNS_QueueEntry *qe;
655   size_t msize;
656   struct PendingMessage *pending;
657
658   if (NULL == name)
659   {
660     return NULL;
661   }
662
663   msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name) + 1;
664   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to lookup %s in GNS\n", name);
665
666   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
667   qe->gns_handle = handle;
668   qe->lookup_proc = proc;
669   qe->proc_cls = proc_cls;
670   qe->r_id = get_request_id(handle);
671   GNUNET_CONTAINER_DLL_insert_tail(handle->lookup_head,
672                                    handle->lookup_tail, qe);
673
674   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
675   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
676   
677   pending->size = msize;
678
679   lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
680   lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
681   lookup_msg->header.size = htons (msize);
682   lookup_msg->id = htonl(qe->r_id);
683   lookup_msg->type = htonl(type);
684
685   memcpy(&lookup_msg[1], name, strlen(name));
686
687   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
688                                pending);
689   
690   process_pending_messages (handle);
691   return qe;
692 }
693
694
695 /**
696  * Perform a name shortening operation on the GNS.
697  *
698  * @param handle handle to the GNS service
699  * @param name the name to look up
700  * @param proc function to call on result
701  * @param proc_cls closure for processor
702  * @return handle to the operation
703  */
704 struct GNUNET_GNS_QueueEntry *
705 GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
706                     const char * name,
707                     GNUNET_GNS_ShortenResultProcessor proc,
708                     void *proc_cls)
709 {
710   /* IPC to shorten gns names, return shorten_handle */
711   struct GNUNET_GNS_ClientShortenMessage *shorten_msg;
712   struct GNUNET_GNS_QueueEntry *qe;
713   size_t msize;
714   struct PendingMessage *pending;
715
716   if (NULL == name)
717   {
718     return NULL;
719   }
720
721   msize = sizeof (struct GNUNET_GNS_ClientShortenMessage) + strlen(name) + 1;
722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to shorten %s in GNS\n", name);
723
724   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
725   qe->gns_handle = handle;
726   qe->shorten_proc = proc;
727   qe->proc_cls = proc_cls;
728   qe->r_id = get_request_id(handle);
729   GNUNET_CONTAINER_DLL_insert_tail(handle->shorten_head,
730                                    handle->shorten_tail, qe);
731
732   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
733   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
734   
735   pending->size = msize;
736
737   shorten_msg = (struct GNUNET_GNS_ClientShortenMessage *) &pending[1];
738   shorten_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_SHORTEN);
739   shorten_msg->header.size = htons (msize);
740   shorten_msg->id = htonl(qe->r_id);
741
742   memcpy(&shorten_msg[1], name, strlen(name));
743
744   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
745                                pending);
746   
747   process_pending_messages (handle);
748   return qe;
749 }
750
751
752 /**
753  * Perform an authority lookup for a given name.
754  *
755  * @param handle handle to the GNS service
756  * @param name the name to look up authority for
757  * @param proc function to call on result
758  * @param proc_cls closure for processor
759  * @return handle to the operation
760  */
761 struct GNUNET_GNS_QueueEntry *
762 GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
763                     const char * name,
764                     GNUNET_GNS_GetAuthResultProcessor proc,
765                     void *proc_cls)
766 {
767   struct GNUNET_GNS_ClientGetAuthMessage *get_auth_msg;
768   struct GNUNET_GNS_QueueEntry *qe;
769   size_t msize;
770   struct PendingMessage *pending;
771
772   if (NULL == name)
773   {
774     return NULL;
775   }
776
777   msize = sizeof (struct GNUNET_GNS_ClientGetAuthMessage) + strlen(name) + 1;
778   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
779               "Trying to look up authority for %s in GNS\n", name);
780
781   qe = GNUNET_malloc(sizeof (struct GNUNET_GNS_QueueEntry));
782   qe->gns_handle = handle;
783   qe->auth_proc = proc;
784   qe->proc_cls = proc_cls;
785   qe->r_id = get_request_id(handle);
786   GNUNET_CONTAINER_DLL_insert_tail(handle->get_auth_head,
787                                    handle->get_auth_tail, qe);
788
789   pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
790   memset(pending, 0, (sizeof (struct PendingMessage) + msize));
791   
792   pending->size = msize;
793
794   get_auth_msg = (struct GNUNET_GNS_ClientGetAuthMessage *) &pending[1];
795   get_auth_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_GET_AUTH);
796   get_auth_msg->header.size = htons (msize);
797   get_auth_msg->id = htonl(qe->r_id);
798
799   memcpy(&get_auth_msg[1], name, strlen(name));
800
801   GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
802                                pending);
803   
804   process_pending_messages (handle);
805   return qe;
806 }
807
808
809 /* end of gns_api.c */