2 This file is part of GNUnet.
3 Copyright (C) 2009-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * 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
208 reconnect_task (void *cls)
210 struct GNUNET_GNS_Handle *handle = cls;
212 handle->reconnect_task = NULL;
218 * Disconnect from service and then reconnect.
220 * @param handle our handle
223 force_reconnect (struct GNUNET_GNS_Handle *handle)
225 struct GNUNET_GNS_LookupRequest *lh;
226 struct PendingMessage *p;
228 GNUNET_CLIENT_disconnect (handle->client);
229 handle->client = NULL;
230 handle->in_receive = GNUNET_NO;
231 for (lh = handle->lookup_head; NULL != lh; lh = lh->next)
233 p = (struct PendingMessage *) &lh[1];
234 if (GNUNET_NO == p->transmitted)
236 p->transmitted = GNUNET_NO;
237 GNUNET_CONTAINER_DLL_insert (handle->pending_head,
238 handle->pending_tail,
241 handle->reconnect_backoff = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff);
242 handle->reconnect_task = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff,
249 * Transmit the next pending message, called by notify_transmit_ready
251 * @param cls the closure
252 * @param size size of pending data
253 * @param buf buffer with pending data
254 * @return size data transmitted
257 transmit_pending (void *cls, size_t size, void *buf);
261 * Handler for messages received from the GNS service
263 * @param cls the 'struct GNUNET_GNS_Handle'
264 * @param msg the incoming message
267 process_message (void *cls, const struct GNUNET_MessageHeader *msg);
271 * Try to send messages from list of messages to send
273 * @param handle the GNS handle
276 process_pending_messages (struct GNUNET_GNS_Handle *handle)
278 struct PendingMessage *p = handle->pending_head;
280 if (NULL == handle->client)
281 return; /* wait for reconnect */
282 if (NULL != handle->th)
283 return; /* transmission request already pending */
285 while ((NULL != p) && (p->transmitted == GNUNET_YES))
288 return; /* no messages pending */
290 LOG (GNUNET_ERROR_TYPE_DEBUG,
291 "Trying to transmit %u bytes\n",
292 (unsigned int) p->size);
294 GNUNET_CLIENT_notify_transmit_ready (handle->client,
296 GNUNET_TIME_UNIT_FOREVER_REL,
297 GNUNET_NO, &transmit_pending,
299 GNUNET_break (NULL != handle->th);
304 * Transmit the next pending message, called by notify_transmit_ready
306 * @param cls the closure
307 * @param size size of pending data
308 * @param buf buffer with pending data
309 * @return size data transmitted
312 transmit_pending (void *cls, size_t size, void *buf)
314 struct GNUNET_GNS_Handle *handle = cls;
316 struct PendingMessage *p;
320 if ((0 == size) || (NULL == buf))
322 LOG (GNUNET_ERROR_TYPE_DEBUG,
323 "Transmission to GNS service failed!\n");
324 force_reconnect (handle);
327 if (NULL == (p = handle->pending_head))
331 while ((NULL != (p = handle->pending_head)) && (p->size <= size))
333 memcpy (&cbuf[tsize], &p[1], p->size);
336 p->transmitted = GNUNET_YES;
337 GNUNET_CONTAINER_DLL_remove (handle->pending_head,
338 handle->pending_tail,
340 if (GNUNET_YES != handle->in_receive)
342 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
343 GNUNET_TIME_UNIT_FOREVER_REL);
344 handle->in_receive = GNUNET_YES;
347 LOG (GNUNET_ERROR_TYPE_DEBUG,
348 "Sending %u bytes\n",
349 (unsigned int) tsize);
350 process_pending_messages (handle);
356 * Process a given reply to the lookup request
358 * @param qe a queue entry
359 * @param msg the lookup message received
362 process_lookup_reply (struct GNUNET_GNS_LookupRequest *qe,
363 const struct GNUNET_GNS_ClientLookupResultMessage *msg)
365 struct GNUNET_GNS_Handle *handle = qe->gns_handle;
366 struct PendingMessage *p = (struct PendingMessage *) &qe[1];
367 GNUNET_GNS_LookupResultProcessor proc;
369 uint32_t rd_count = ntohl (msg->rd_count);
370 struct GNUNET_GNSRECORD_Data rd[rd_count];
373 if (GNUNET_YES != p->transmitted)
375 /* service send reply to query we never managed to send!? */
377 force_reconnect (handle);
380 mlen = ntohs (msg->header.size);
381 mlen -= sizeof (struct GNUNET_GNS_ClientLookupResultMessage);
382 proc = qe->lookup_proc;
383 proc_cls = qe->proc_cls;
384 GNUNET_CONTAINER_DLL_remove (handle->lookup_head, handle->lookup_tail, qe);
386 if (GNUNET_SYSERR == GNUNET_GNSRECORD_records_deserialize (mlen,
387 (const char*) &msg[1],
391 LOG (GNUNET_ERROR_TYPE_ERROR,
392 _("Failed to deserialize lookup reply from GNS service!\n"));
393 proc (proc_cls, 0, NULL);
397 LOG (GNUNET_ERROR_TYPE_DEBUG,
398 "Received lookup reply from GNS service (%u records)\n",
399 (unsigned int) rd_count);
400 proc (proc_cls, rd_count, rd);
406 * Handler for messages received from the GNS service
408 * @param cls the 'struct GNUNET_GNS_Handle'
409 * @param msg the incoming message
412 process_message (void *cls, const struct GNUNET_MessageHeader *msg)
414 struct GNUNET_GNS_Handle *handle = cls;
415 struct GNUNET_GNS_LookupRequest *lr;
416 const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
421 force_reconnect (handle);
425 GNUNET_CLIENT_receive (handle->client, &process_message, handle,
426 GNUNET_TIME_UNIT_FOREVER_REL);
427 switch (ntohs (msg->type))
429 case GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT:
430 LOG (GNUNET_ERROR_TYPE_DEBUG,
431 "Got LOOKUP_RESULT msg\n");
432 if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
435 force_reconnect (handle);
438 lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
439 r_id = ntohl (lookup_msg->id);
440 for (lr = handle->lookup_head; NULL != lr; lr = lr->next)
441 if (lr->r_id == r_id)
443 process_lookup_reply(lr, lookup_msg);
449 force_reconnect (handle);
456 * Initialize the connection with the GNS service.
458 * @param cfg configuration to use
459 * @return handle to the GNS service, or NULL on error
461 struct GNUNET_GNS_Handle *
462 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
464 struct GNUNET_GNS_Handle *handle;
466 handle = GNUNET_new (struct GNUNET_GNS_Handle);
474 * Shutdown connection with the GNS service.
476 * @param handle handle of the GNS connection to stop
479 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
481 if (NULL != handle->client)
483 GNUNET_CLIENT_disconnect (handle->client);
484 handle->client = NULL;
486 if (NULL != handle->reconnect_task)
488 GNUNET_SCHEDULER_cancel (handle->reconnect_task);
489 handle->reconnect_task = NULL;
491 GNUNET_assert (NULL == handle->lookup_head);
492 GNUNET_free (handle);
497 * Cancel pending lookup request
499 * @param lr the lookup request to cancel
502 GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr)
504 struct PendingMessage *p = (struct PendingMessage*) &lr[1];
506 GNUNET_assert (NULL != lr->gns_handle);
507 if (GNUNET_NO == p->transmitted)
508 GNUNET_CONTAINER_DLL_remove (lr->gns_handle->pending_head,
509 lr->gns_handle->pending_tail,
511 GNUNET_CONTAINER_DLL_remove (lr->gns_handle->lookup_head,
512 lr->gns_handle->lookup_tail,
519 * Perform an asynchronous lookup operation on the GNS.
521 * @param handle handle to the GNS service
522 * @param name the name to look up
523 * @param zone the zone to start the resolution in
524 * @param type the record type to look up
525 * @param options local options for the lookup
526 * @param shorten_zone_key the private key of the shorten zone (can be NULL)
527 * @param proc processor to call on result
528 * @param proc_cls closure for @a proc
529 * @return handle to the get request
531 struct GNUNET_GNS_LookupRequest*
532 GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle,
534 const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
536 enum GNUNET_GNS_LocalOptions options,
537 const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_zone_key,
538 GNUNET_GNS_LookupResultProcessor proc,
541 /* IPC to shorten gns names, return shorten_handle */
542 struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
543 struct GNUNET_GNS_LookupRequest *lr;
545 struct PendingMessage *pending;
552 LOG (GNUNET_ERROR_TYPE_DEBUG,
553 "Trying to lookup `%s' in GNS\n",
555 msize = sizeof (struct GNUNET_GNS_ClientLookupMessage)
557 if (msize > UINT16_MAX)
562 lr = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupRequest) +
563 sizeof (struct PendingMessage) + msize);
564 lr->gns_handle = handle;
565 lr->lookup_proc = proc;
566 lr->proc_cls = proc_cls;
567 lr->r_id = handle->r_id_gen++;
568 pending = (struct PendingMessage *)&lr[1];
569 pending->size = msize;
570 pending->r_id = lr->r_id;
571 GNUNET_CONTAINER_DLL_insert_tail (handle->lookup_head,
572 handle->lookup_tail, lr);
574 lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
575 lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_LOOKUP);
576 lookup_msg->header.size = htons (msize);
577 lookup_msg->id = htonl (lr->r_id);
578 lookup_msg->options = htons ((uint16_t) options);
579 lookup_msg->zone = *zone;
580 lookup_msg->type = htonl (type);
581 if (NULL != shorten_zone_key)
583 lookup_msg->have_key = htons (GNUNET_YES);
584 lookup_msg->shorten_key = *shorten_zone_key;
586 memcpy (&lookup_msg[1], name, strlen (name) + 1);
587 GNUNET_CONTAINER_DLL_insert_tail (handle->pending_head,
588 handle->pending_tail,
590 process_pending_messages (handle);
595 /* end of gns_api.c */