2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
24 * @brief library to access the GNS service
25 * @author Martin Schanzenbach
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"
36 #include "gnunet_gns_service.h"
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
49 struct GNUNET_GNS_QueueEntry
54 struct GNUNET_GNS_QueueEntry *next;
59 struct GNUNET_GNS_QueueEntry *prev;
65 struct GNUNET_GNS_Handle *gns_handle;
67 /* processor to call on shorten result */
68 GNUNET_GNS_ShortenResultProcessor shorten_proc;
70 /* processor to call on lookup result */
71 GNUNET_GNS_LookupResultProcessor lookup_proc;
73 /* processor to call on authority lookup result */
74 GNUNET_GNS_GetAuthResultProcessor auth_proc;
76 /* processor closure */
83 * Entry in our list of messages to be (re-)transmitted.
88 * This is a doubly-linked list.
90 struct PendingMessage *prev;
93 * This is a doubly-linked list.
95 struct PendingMessage *next;
98 * Size of the message.
106 * Connection to the GNS service.
108 struct GNUNET_GNS_Handle
112 * Configuration to use.
114 const struct GNUNET_CONFIGURATION_Handle *cfg;
117 * Socket (if available).
119 struct GNUNET_CLIENT_Connection *client;
122 * Currently pending transmission request (or NULL).
124 struct GNUNET_CLIENT_TransmitHandle *th;
129 * Head of linked list of shorten messages we would like to transmit.
131 struct PendingMessage *pending_head;
134 * Tail of linked list of shorten messages we would like to transmit.
136 struct PendingMessage *pending_tail;
139 * Head of linked list of shorten messages we would like to transmit.
141 struct GNUNET_GNS_QueueEntry *shorten_head;
144 * Tail of linked list of shorten messages we would like to transmit.
146 struct GNUNET_GNS_QueueEntry *shorten_tail;
149 * Head of linked list of lookup messages we would like to transmit.
151 struct GNUNET_GNS_QueueEntry *lookup_head;
154 * Tail of linked list of lookup messages we would like to transmit.
156 struct GNUNET_GNS_QueueEntry *lookup_tail;
159 * Head of linked list of authority lookup messages we would like to transmit.
161 struct GNUNET_GNS_QueueEntry *get_auth_head;
164 * Tail of linked list of authority lookup messages we would like to transmit.
166 struct GNUNET_GNS_QueueEntry *get_auth_tail;
171 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
174 * Did we start our receive loop yet?
179 * Reconnect necessary
185 * Try to send messages from list of messages to send
186 * @param handle GNS_Handle
189 process_pending_messages (struct GNUNET_GNS_Handle *handle);
193 * Reconnect to GNS service.
195 * @param h the handle to the namestore service
198 reconnect (struct GNUNET_GNS_Handle *h)
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);
210 * @param cls the handle
211 * @param tc task context
214 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 struct GNUNET_GNS_Handle *h = cls;
218 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
224 * Disconnect from service and then reconnect.
226 * @param h our handle
229 force_reconnect (struct GNUNET_GNS_Handle *h)
231 h->reconnect = GNUNET_NO;
232 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
234 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
240 * Transmit the next pending message, called by notify_transmit_ready
243 transmit_pending (void *cls, size_t size, void *buf);
246 * Handler for messages received from the GNS service
248 * @param cls the 'struct GNUNET_GNS_Handle'
249 * @param msg the incoming message
252 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
255 * Try to send messages from list of messages to send
258 process_pending_messages (struct GNUNET_GNS_Handle *handle)
260 struct PendingMessage *p;
262 if (handle->client == NULL)
264 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
265 "process_pending_messages called, but client is null\n");
269 if (handle->th != NULL)
272 if (NULL == (p = handle->pending_head))
275 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
276 "Trying to transmit %d bytes...\n", p->size);
279 GNUNET_CLIENT_notify_transmit_ready (handle->client,
281 GNUNET_TIME_UNIT_FOREVER_REL,
282 GNUNET_NO, &transmit_pending,
284 if (NULL != handle->th)
287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
288 "notify_transmit_ready returned NULL!\n");
293 * Transmit the next pending message, called by notify_transmit_ready
296 transmit_pending (void *cls, size_t size, void *buf)
298 struct GNUNET_GNS_Handle *handle = cls;
299 struct PendingMessage *p;
305 if ((size == 0) || (buf == NULL))
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Transmission to GNS service failed!\n");
309 force_reconnect(handle);
316 if (NULL == (p = handle->pending_head))
319 while ((NULL != (p = handle->pending_head)) && (p->size <= size))
321 memcpy (&cbuf[tsize], &p[1], p->size);
324 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail, p);
325 if (GNUNET_YES != handle->in_receive)
327 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
328 GNUNET_TIME_UNIT_FOREVER_REL);
329 handle->in_receive = GNUNET_YES;
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Sending %d bytes\n", tsize);
337 process_pending_messages(handle);
342 * Process a given reply that might match the given
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
350 process_shorten_reply (struct GNUNET_GNS_QueueEntry *qe,
351 const struct GNUNET_GNS_ClientShortenResultMessage *msg)
353 struct GNUNET_GNS_Handle *h = qe->gns_handle;
354 const char *short_name;
356 GNUNET_CONTAINER_DLL_remove(h->shorten_head, h->shorten_tail, qe);
358 short_name = (char*)(&msg[1]);
360 if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
361 sizeof (struct GNUNET_GNS_ClientShortenResultMessage))
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Received shortened reply `%s' from GNS service\n",
372 GNUNET_CLIENT_receive (h->client, &process_message, h,
373 GNUNET_TIME_UNIT_FOREVER_REL);
374 qe->shorten_proc(qe->proc_cls, short_name);
380 * Process a given reply that might match the given
383 * @param qe the handle to the request
384 * @param msg the message to process
387 process_get_auth_reply (struct GNUNET_GNS_QueueEntry *qe,
388 const struct GNUNET_GNS_ClientGetAuthResultMessage *msg)
390 struct GNUNET_GNS_Handle *h = qe->gns_handle;
391 const char *auth_name;
393 GNUNET_CONTAINER_DLL_remove(h->get_auth_head, h->get_auth_tail, qe);
395 auth_name = (char*)(&msg[1]);
397 if (ntohs (((struct GNUNET_MessageHeader*)msg)->size) <
398 sizeof (struct GNUNET_GNS_ClientGetAuthResultMessage))
405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
406 "Received GET_AUTH reply `%s' from GNS service\n",
409 GNUNET_CLIENT_receive (h->client, &process_message, h,
410 GNUNET_TIME_UNIT_FOREVER_REL);
411 qe->auth_proc(qe->proc_cls, auth_name);
415 * Process a given reply to the lookup request
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
424 process_lookup_reply (struct GNUNET_GNS_QueueEntry *qe,
425 const struct GNUNET_GNS_ClientLookupResultMessage *msg)
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];
432 GNUNET_CONTAINER_DLL_remove(h->lookup_head, h->lookup_tail, qe);
434 if (len < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
441 len -= sizeof(struct GNUNET_GNS_ClientLookupResultMessage);
443 GNUNET_CLIENT_receive (h->client, &process_message, h,
444 GNUNET_TIME_UNIT_FOREVER_REL);
445 if (GNUNET_SYSERR == GNUNET_NAMESTORE_records_deserialize (len,
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);
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);
465 * Handler for messages received from the GNS service
467 * @param cls the 'struct GNUNET_GNS_Handle'
468 * @param msg the incoming message
471 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
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;
481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 "Error receiving data from GNS service, reconnecting\n");
487 force_reconnect (handle);
491 type = ntohs (msg->type);
493 if (type == GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT)
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
497 lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
498 r_id = ntohl (lookup_msg->id);
500 if (r_id > handle->r_id)
502 /** no request found */
504 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
505 GNUNET_TIME_UNIT_FOREVER_REL);
509 for (qe = handle->lookup_head; qe != NULL; qe = qe->next)
511 if (qe->r_id == r_id)
515 process_lookup_reply(qe, lookup_msg);
520 else if (type == GNUNET_MESSAGE_TYPE_GNS_SHORTEN_RESULT)
522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
523 "Got SHORTEN_RESULT msg\n");
524 shorten_msg = (struct GNUNET_GNS_ClientShortenResultMessage *) msg;
526 r_id = ntohl (shorten_msg->id);
528 if (r_id > handle->r_id)
530 /** no request found */
532 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
533 GNUNET_TIME_UNIT_FOREVER_REL);
537 for (qe = handle->shorten_head; qe != NULL; qe = qe->next)
539 if (qe->r_id == r_id)
543 process_shorten_reply(qe, shorten_msg);
546 else if (type == GNUNET_MESSAGE_TYPE_GNS_GET_AUTH_RESULT)
548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
549 "Got GET_AUTH_RESULT msg\n");
550 get_auth_msg = (struct GNUNET_GNS_ClientGetAuthResultMessage *) msg;
552 r_id = ntohl (get_auth_msg->id);
554 if (r_id > handle->r_id)
556 /** no request found */
558 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
559 GNUNET_TIME_UNIT_FOREVER_REL);
563 for (qe = handle->get_auth_head; qe != NULL; qe = qe->next)
565 if (qe->r_id == r_id)
569 process_get_auth_reply(qe, get_auth_msg);
574 if (GNUNET_YES == handle->reconnect)
575 force_reconnect (handle);
581 * Initialize the connection with the GNS service.
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
587 struct GNUNET_GNS_Handle *
588 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
590 struct GNUNET_GNS_Handle *handle;
592 handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle));
593 handle->reconnect = GNUNET_NO;
596 //handle->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect_task, handle);
597 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
599 handle->in_receive = GNUNET_NO;
605 * Shutdown connection with the GNS service.
607 * @param handle handle of the GNS connection to stop
610 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *h)
612 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
613 if (GNUNET_SCHEDULER_NO_TASK != h->reconnect_task)
615 GNUNET_SCHEDULER_cancel (h->reconnect_task);
616 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
623 * Helper function to generate request ids
629 get_request_id (struct GNUNET_GNS_Handle *h)
631 uint32_t r_id = h->r_id;
637 * Perform an asynchronous Lookup operation on the GNS.
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
645 struct GNUNET_GNS_QueueEntry *
646 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
648 enum GNUNET_GNS_RecordType type,
649 GNUNET_GNS_LookupResultProcessor proc,
652 /* IPC to shorten gns names, return shorten_handle */
653 struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
654 struct GNUNET_GNS_QueueEntry *qe;
656 struct PendingMessage *pending;
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);
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);
674 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
675 memset(pending, 0, (sizeof (struct PendingMessage) + msize));
677 pending->size = msize;
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);
685 memcpy(&lookup_msg[1], name, strlen(name));
687 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
690 process_pending_messages (handle);
696 * Perform a name shortening operation on the GNS.
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
704 struct GNUNET_GNS_QueueEntry *
705 GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
707 GNUNET_GNS_ShortenResultProcessor proc,
710 /* IPC to shorten gns names, return shorten_handle */
711 struct GNUNET_GNS_ClientShortenMessage *shorten_msg;
712 struct GNUNET_GNS_QueueEntry *qe;
714 struct PendingMessage *pending;
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);
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);
732 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
733 memset(pending, 0, (sizeof (struct PendingMessage) + msize));
735 pending->size = msize;
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);
742 memcpy(&shorten_msg[1], name, strlen(name));
744 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
747 process_pending_messages (handle);
753 * Perform an authority lookup for a given name.
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
761 struct GNUNET_GNS_QueueEntry *
762 GNUNET_GNS_get_authority (struct GNUNET_GNS_Handle *handle,
764 GNUNET_GNS_GetAuthResultProcessor proc,
767 struct GNUNET_GNS_ClientGetAuthMessage *get_auth_msg;
768 struct GNUNET_GNS_QueueEntry *qe;
770 struct PendingMessage *pending;
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);
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);
789 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
790 memset(pending, 0, (sizeof (struct PendingMessage) + msize));
792 pending->size = msize;
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);
799 memcpy(&get_auth_msg[1], name, strlen(name));
801 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
804 process_pending_messages (handle);
809 /* end of gns_api.c */