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"
37 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
40 * Handle to a lookup request
42 struct GNUNET_GNS_LookupRequest
48 struct GNUNET_GNS_LookupRequest *next;
53 struct GNUNET_GNS_LookupRequest *prev;
58 struct GNUNET_GNS_Handle *gns_handle;
61 * processor to call on lookup result
63 GNUNET_GNS_LookupResultProcessor lookup_proc;
79 * Entry in our list of messages to be (re-)transmitted.
84 * This is a doubly-linked list.
86 struct PendingMessage *prev;
89 * This is a doubly-linked list.
91 struct PendingMessage *next;
94 * Size of the message.
104 * This message has been transmitted. GNUNET_NO if the message is
105 * in the "pending" DLL, GNUNET_YES if it has been transmitted to
106 * the service via the current client connection.
114 * Connection to the GNS service.
116 struct GNUNET_GNS_Handle
120 * Configuration to use.
122 const struct GNUNET_CONFIGURATION_Handle *cfg;
125 * Socket (if available).
127 struct GNUNET_CLIENT_Connection *client;
130 * Currently pending transmission request (or NULL).
132 struct GNUNET_CLIENT_TransmitHandle *th;
135 * Head of linked list of shorten messages we would like to transmit.
137 struct PendingMessage *pending_head;
140 * Tail of linked list of shorten messages we would like to transmit.
142 struct PendingMessage *pending_tail;
145 * Head of linked list of lookup messages we would like to transmit.
147 struct GNUNET_GNS_LookupRequest *lookup_head;
150 * Tail of linked list of lookup messages we would like to transmit.
152 struct GNUNET_GNS_LookupRequest *lookup_tail;
157 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
160 * How long do we wait until we try to reconnect?
162 struct GNUNET_TIME_Relative reconnect_backoff;
165 * Request Id generator. Incremented by one for each request.
170 * Did we start our receive loop yet?
178 * Try to send messages from list of messages to send
179 * @param handle GNS_Handle
182 process_pending_messages (struct GNUNET_GNS_Handle *handle);
186 * Reconnect to GNS service.
188 * @param handle the handle to the GNS service
191 reconnect (struct GNUNET_GNS_Handle *handle)
193 GNUNET_assert (NULL == handle->client);
194 LOG (GNUNET_ERROR_TYPE_DEBUG,
195 "Trying to connect to GNS\n");
196 handle->client = GNUNET_CLIENT_connect ("gns", handle->cfg);
197 GNUNET_assert (NULL != handle->client);
198 process_pending_messages (handle);
205 * @param cls the handle
206 * @param tc task context
209 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
211 struct GNUNET_GNS_Handle *handle = cls;
213 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
219 * Disconnect from service and then reconnect.
221 * @param handle our handle
224 force_reconnect (struct GNUNET_GNS_Handle *handle)
226 struct GNUNET_GNS_LookupRequest *lh;
227 struct PendingMessage *p;
229 GNUNET_CLIENT_disconnect (handle->client);
230 handle->client = NULL;
231 handle->in_receive = GNUNET_NO;
232 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
234 p = (struct PendingMessage *) &lh[1];
235 if (GNUNET_NO == p->transmitted)
237 p->transmitted = GNUNET_NO;
238 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
239 handle->pending_tail,
242 handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
243 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
250 * Transmit the next pending message, called by notify_transmit_ready
252 * @param cls the closure
253 * @param size size of pending data
254 * @param buf buffer with pending data
255 * @return size data transmitted
258 transmit_pending (void *cls, size_t size, void *buf);
262 * Handler for messages received from the GNS service
264 * @param cls the 'struct GNUNET_GNS_Handle'
265 * @param msg the incoming message
268 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
272 * Try to send messages from list of messages to send
274 * @param handle the GNS handle
277 process_pending_messages (struct GNUNET_GNS_Handle *handle)
279 struct PendingMessage *p = handle->pending_head;
281 if (NULL == handle->client)
282 return; /* wait for reconnect */
283 if (NULL != handle->th)
284 return; /* transmission request already pending */
286 while ((NULL != p) && (p->transmitted == GNUNET_YES))
289 return; /* no messages pending */
291 LOG (GNUNET_ERROR_TYPE_DEBUG,
292 "Trying to transmit %u bytes\n",
293 (unsigned int) p->size);
295 GNUNET_CLIENT_notify_transmit_ready (handle->client,
297 GNUNET_TIME_UNIT_FOREVER_REL,
298 GNUNET_NO, &transmit_pending,
300 GNUNET_break (NULL != handle->th);
305 * Transmit the next pending message, called by notify_transmit_ready
307 * @param cls the closure
308 * @param size size of pending data
309 * @param buf buffer with pending data
310 * @return size data transmitted
313 transmit_pending (void *cls, size_t size, void *buf)
315 struct GNUNET_GNS_Handle *handle = cls;
317 struct PendingMessage *p;
321 if ((0 == size) || (NULL == buf))
323 LOG (GNUNET_ERROR_TYPE_DEBUG,
324 "Transmission to GNS service failed!\n");
325 force_reconnect (handle);
328 if (NULL == (p = handle->pending_head))
332 while ((NULL != (p = handle->pending_head)) && (p->size <= size))
334 memcpy (&cbuf[tsize], &p[1], p->size);
337 p->transmitted = GNUNET_YES;
338 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
339 handle->pending_tail,
341 if (GNUNET_YES != handle->in_receive)
343 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
344 GNUNET_TIME_UNIT_FOREVER_REL);
345 handle->in_receive = GNUNET_YES;
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
349 "Sending %u bytes\n",
350 (unsigned int) tsize);
351 process_pending_messages (handle);
357 * Process a given reply to the lookup request
359 * @param qe a queue entry
360 * @param msg the lookup message received
363 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
364 const struct GNUNET_GNS_ClientLookupResultMessage *msg)
366 struct GNUNET_GNS_Handle *handle = qe->gns_handle;
367 struct PendingMessage *p = (struct PendingMessage *) &qe[1];
368 uint32_t rd_count = ntohl (msg->rd_count);
369 struct GNUNET_GNSRECORD_Data rd[rd_count];
372 if (GNUNET_YES != p->transmitted)
374 /* service send reply to query we never managed to send!? */
376 force_reconnect (handle);
379 mlen = ntohs (msg->header.size);
380 mlen -= sizeof (struct GNUNET_GNS_ClientLookupResultMessage);
381 if (GNUNET_SYSERR == GNUNET_GNSRECORD_records_deserialize (mlen,
382 (const char*) &msg[1],
386 LOG (GNUNET_ERROR_TYPE_ERROR,
387 _("Failed to serialize lookup reply from GNS service!\n"));
388 qe->lookup_proc (qe->proc_cls, 0, NULL);
392 LOG (GNUNET_ERROR_TYPE_DEBUG,
393 "Received lookup reply from GNS service (%u records)\n",
394 (unsigned int) rd_count);
395 qe->lookup_proc (qe->proc_cls, rd_count, rd);
397 GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
403 * Handler for messages received from the GNS service
405 * @param cls the 'struct GNUNET_GNS_Handle'
406 * @param msg the incoming message
409 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
411 struct GNUNET_GNS_Handle *handle = cls;
412 struct GNUNET_GNS_LookupRequest *lr;
413 const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
418 force_reconnect (handle);
421 switch (ntohs (msg->type))
423 case GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT:
424 LOG (GNUNET_ERROR_TYPE_DEBUG,
425 "Got LOOKUP_RESULT msg\n");
426 if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
429 force_reconnect (handle);
432 lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
433 r_id = ntohl (lookup_msg->id);
434 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
435 if (lr->r_id == r_id)
437 process_lookup_reply(lr, lookup_msg);
443 force_reconnect (handle);
446 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
447 GNUNET_TIME_UNIT_FOREVER_REL);
452 * Initialize the connection with the GNS service.
454 * @param cfg configuration to use
455 * @return handle to the GNS service, or NULL on error
457 struct GNUNET_GNS_Handle *
458 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
460 struct GNUNET_GNS_Handle *handle;
462 handle = GNUNET_new (struct GNUNET_GNS_Handle);
470 * Shutdown connection with the GNS service.
472 * @param handle handle of the GNS connection to stop
475 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
477 if (NULL != handle->client)
479 GNUNET_CLIENT_disconnect (handle->client);
480 handle->client = NULL;
482 if (GNUNET_SCHEDULER_NO_TASK != handle->reconnect_task)
484 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
485 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
487 GNUNET_assert (NULL == handle->lookup_head);
488 GNUNET_free (handle);
493 * Cancel pending lookup request
495 * @param lr the lookup request to cancel
498 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
500 struct PendingMessage *p = (struct PendingMessage*) &lr[1];
502 GNUNET_assert (NULL != lr->gns_handle);
503 if (GNUNET_NO == p->transmitted)
504 GNUNET_CONTAINER_DLL_remove (lr->gns_handle->pending_head,
505 lr->gns_handle->pending_tail,
507 GNUNET_CONTAINER_DLL_remove (lr->gns_handle->lookup_head,
508 lr->gns_handle->lookup_tail,
515 * Perform an asynchronous lookup operation on the GNS.
517 * @param handle handle to the GNS service
518 * @param name the name to look up
519 * @param zone the zone to start the resolution in
520 * @param type the record type to look up
521 * @param only_cached #GNUNET_YES to only check locally (not in the DHT)
522 * @param shorten_zone_key the private key of the shorten zone (can be NULL)
523 * @param proc processor to call on result
524 * @param proc_cls closure for processor
525 * @return handle to the get request
527 struct GNUNET_GNS_LookupRequest*
528 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
530 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
533 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
534 GNUNET_GNS_LookupResultProcessor proc,
537 /* IPC to shorten gns names, return shorten_handle */
538 struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
539 struct GNUNET_GNS_LookupRequest *lr;
541 struct PendingMessage *pending;
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 "Trying to lookup `%s' in GNS\n",
551 msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
553 if (msize > UINT16_MAX)
558 lr = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupRequest) +
559 sizeof (struct PendingMessage) + msize);
560 lr->gns_handle = handle;
561 lr->lookup_proc = proc;
562 lr->proc_cls = proc_cls;
563 lr->r_id = handle->r_id_gen++;
564 pending = (struct PendingMessage *)&lr[1];
565 pending->size = msize;
566 pending->r_id = lr->r_id;
567 GNUNET_CONTAINER_DLL_insert_tail (handle->lookup_head,
568 handle->lookup_tail, lr);
570 lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
571 lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
572 lookup_msg->header.size = htons (msize);
573 lookup_msg->id = htonl (lr->r_id);
574 lookup_msg->only_cached = htonl (only_cached);
575 lookup_msg->zone = *zone;
576 lookup_msg->type = htonl (type);
577 if (NULL != shorten_zone_key)
579 lookup_msg->have_key = htons (GNUNET_YES);
580 lookup_msg->shorten_key = *shorten_zone_key;
582 memcpy (&lookup_msg[1], name, strlen (name) + 1);
583 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
584 handle->pending_tail,
586 process_pending_messages (handle);
591 /* end of gns_api.c */