2 This file is part of GNUnet.
3 (C) 2009-2013 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.
22 * @brief library to access the GNS service
23 * @author Martin Schanzenbach
24 * @author Christian Grothoff
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"
34 #include "gnunet_gns_service.h"
38 * Handle to a lookup request
40 struct GNUNET_GNS_LookupRequest
46 struct GNUNET_GNS_LookupRequest *next;
51 struct GNUNET_GNS_LookupRequest *prev;
56 struct GNUNET_GNS_Handle *gns_handle;
59 * processor to call on lookup result
61 GNUNET_GNS_LookupResultProcessor lookup_proc;
77 * Entry in our list of messages to be (re-)transmitted.
82 * This is a doubly-linked list.
84 struct PendingMessage *prev;
87 * This is a doubly-linked list.
89 struct PendingMessage *next;
92 * Size of the message.
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.
112 * Connection to the GNS service.
114 struct GNUNET_GNS_Handle
118 * Configuration to use.
120 const struct GNUNET_CONFIGURATION_Handle *cfg;
123 * Socket (if available).
125 struct GNUNET_CLIENT_Connection *client;
128 * Currently pending transmission request (or NULL).
130 struct GNUNET_CLIENT_TransmitHandle *th;
133 * Head of linked list of shorten messages we would like to transmit.
135 struct PendingMessage *pending_head;
138 * Tail of linked list of shorten messages we would like to transmit.
140 struct PendingMessage *pending_tail;
143 * Head of linked list of lookup messages we would like to transmit.
145 struct GNUNET_GNS_LookupRequest *lookup_head;
148 * Tail of linked list of lookup messages we would like to transmit.
150 struct GNUNET_GNS_LookupRequest *lookup_tail;
155 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
158 * How long do we wait until we try to reconnect?
160 struct GNUNET_TIME_Relative reconnect_backoff;
163 * Request Id generator. Incremented by one for each request.
168 * Did we start our receive loop yet?
176 * Try to send messages from list of messages to send
177 * @param handle GNS_Handle
180 process_pending_messages (struct GNUNET_GNS_Handle *handle);
184 * Reconnect to GNS service.
186 * @param handle the handle to the GNS service
189 reconnect (struct GNUNET_GNS_Handle *handle)
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);
203 * @param cls the handle
204 * @param tc task context
207 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
209 struct GNUNET_GNS_Handle *handle = cls;
211 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
217 * Disconnect from service and then reconnect.
219 * @param handle our handle
222 force_reconnect (struct GNUNET_GNS_Handle *handle)
224 struct GNUNET_GNS_LookupRequest *lh;
225 struct PendingMessage *p;
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)
232 p = (struct PendingMessage *) &lh[1];
233 if (GNUNET_NO == p->transmitted)
235 p->transmitted = GNUNET_NO;
236 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
237 handle->pending_tail,
240 handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
241 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
248 * Transmit the next pending message, called by notify_transmit_ready
250 * @param cls the closure
251 * @param size size of pending data
252 * @param buf buffer with pending data
253 * @return size data transmitted
256 transmit_pending (void *cls, size_t size, void *buf);
260 * Handler for messages received from the GNS service
262 * @param cls the 'struct GNUNET_GNS_Handle'
263 * @param msg the incoming message
266 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
270 * Try to send messages from list of messages to send
272 * @param handle the GNS handle
275 process_pending_messages (struct GNUNET_GNS_Handle *handle)
277 struct PendingMessage *p = handle->pending_head;
279 if (NULL == handle->client)
280 return; /* wait for reconnect */
281 if (NULL != handle->th)
282 return; /* transmission request already pending */
284 while ((NULL != p) && (p->transmitted == GNUNET_YES))
287 return; /* no messages pending */
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Trying to transmit %u bytes\n",
291 (unsigned int) p->size);
293 GNUNET_CLIENT_notify_transmit_ready (handle->client,
295 GNUNET_TIME_UNIT_FOREVER_REL,
296 GNUNET_NO, &transmit_pending,
298 GNUNET_break (NULL != handle->th);
303 * Transmit the next pending message, called by notify_transmit_ready
305 * @param cls the closure
306 * @param size size of pending data
307 * @param buf buffer with pending data
308 * @return size data transmitted
311 transmit_pending (void *cls, size_t size, void *buf)
313 struct GNUNET_GNS_Handle *handle = cls;
315 struct PendingMessage *p;
319 if ((0 == size) || (NULL == buf))
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Transmission to GNS service failed!\n");
323 force_reconnect (handle);
326 if (NULL == (p = handle->pending_head))
330 while ((NULL != (p = handle->pending_head)) && (p->size <= size))
332 memcpy (&cbuf[tsize], &p[1], p->size);
335 p->transmitted = GNUNET_YES;
336 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
337 handle->pending_tail,
339 if (GNUNET_YES != handle->in_receive)
341 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
342 GNUNET_TIME_UNIT_FOREVER_REL);
343 handle->in_receive = GNUNET_YES;
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
347 "Sending %u bytes\n",
348 (unsigned int) tsize);
349 process_pending_messages (handle);
355 * Process a given reply to the lookup request
357 * @param qe a queue entry
358 * @param msg the lookup message received
361 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
362 const struct GNUNET_GNS_ClientLookupResultMessage *msg)
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];
370 if (GNUNET_YES != p->transmitted)
372 /* service send reply to query we never managed to send!? */
374 force_reconnect (handle);
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],
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);
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);
395 GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
401 * Handler for messages received from the GNS service
403 * @param cls the 'struct GNUNET_GNS_Handle'
404 * @param msg the incoming message
407 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
409 struct GNUNET_GNS_Handle *handle = cls;
410 struct GNUNET_GNS_LookupRequest *lr;
411 const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
416 force_reconnect (handle);
419 switch (ntohs (msg->type))
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))
427 force_reconnect (handle);
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)
435 process_lookup_reply(lr, lookup_msg);
441 force_reconnect (handle);
444 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
445 GNUNET_TIME_UNIT_FOREVER_REL);
450 * Initialize the connection with the GNS service.
452 * @param cfg configuration to use
453 * @return handle to the GNS service, or NULL on error
455 struct GNUNET_GNS_Handle *
456 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
458 struct GNUNET_GNS_Handle *handle;
460 handle = GNUNET_new (struct GNUNET_GNS_Handle);
468 * Shutdown connection with the GNS service.
470 * @param handle handle of the GNS connection to stop
473 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
475 GNUNET_CLIENT_disconnect (handle->client);
476 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
478 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
479 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
481 GNUNET_assert (NULL == handle->lookup_head);
482 GNUNET_free (handle);
487 * Cancel pending lookup request
489 * @param lr the lookup request to cancel
492 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
494 struct PendingMessage *p = (struct PendingMessage*) &lr[1];
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,
501 GNUNET_CONTAINER_DLL_remove (lr->gns_handle->lookup_head,
502 lr->gns_handle->lookup_tail,
509 * Perform an asynchronous lookup operation on the GNS.
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
521 struct GNUNET_GNS_LookupRequest*
522 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
524 const struct GNUNET_CRYPTO_EccPublicSignKey *zone,
527 const struct GNUNET_CRYPTO_EccPrivateKey *shorten_zone_key,
528 GNUNET_GNS_LookupResultProcessor proc,
531 /* IPC to shorten gns names, return shorten_handle */
532 struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
533 struct GNUNET_GNS_LookupRequest *lr;
535 struct PendingMessage *pending;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Trying to lookup `%s' in GNS\n",
545 msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
547 if (msize > UINT16_MAX)
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);
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)
573 lookup_msg->have_key = htonl (GNUNET_YES);
574 lookup_msg->shorten_key = *shorten_zone_key;
576 memcpy (&lookup_msg[1], name, strlen (name) + 1);
577 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
578 handle->pending_tail,
580 process_pending_messages (handle);
585 /* end of gns_api.c */