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.
22 * TODO: Do we really need a client API?
25 * @brief library to access the GNS service
26 * @author Martin Schanzenbach
30 #include "gnunet_util_lib.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_arm_service.h"
33 #include "gnunet_hello_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_dht_service.h"
37 #include "gnunet_gns_service.h"
39 #define DEBUG_GNS_API GNUNET_EXTRA_LOGGING
41 #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__)
43 /* TODO into gnunet_protocols */
44 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
45 #define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
48 * Entry in our list of messages to be (re-)transmitted.
53 * This is a doubly-linked list.
55 struct PendingMessage *prev;
58 * This is a doubly-linked list.
60 struct PendingMessage *next;
63 * Message that is pending, allocated at the end
66 const struct GNUNET_MessageHeader *msg;
69 * Handle to the GNS API context.
71 struct GNUNET_GNS_Handle *handle;
74 * Continuation to call when the request has been
75 * transmitted (for the first time) to the service; can be NULL.
77 GNUNET_SCHEDULER_Task cont;
85 * Timeout task for this message
87 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
90 * Unique ID for this request
95 * Free the saved message once sent, set to GNUNET_YES for messages
96 * that do not receive responses; GNUNET_NO if this pending message
97 * is aliased from a 'struct GNUNET_DHT_RouteHandle' and will be freed
103 * GNUNET_YES if this message is in our pending queue right now.
105 int in_pending_queue;
110 * Handle to a Lookup request
112 struct GNUNET_GNS_LookupHandle
116 * Iterator to call on data receipt
118 GNUNET_GNS_LookupIterator iter;
121 * Closure for the iterator callback
126 * Main handle to this GNS api
128 struct GNUNET_GNS_Handle *gns_handle;
131 * Key that this get request is for
136 * Unique identifier for this request (for key collisions).
140 struct PendingMessage *message;
145 * Connection to the GNS service.
147 struct GNUNET_GNS_Handle
151 * Configuration to use.
153 const struct GNUNET_CONFIGURATION_Handle *cfg;
156 * Socket (if available).
158 struct GNUNET_CLIENT_Connection *client;
161 * Currently pending transmission request (or NULL).
163 struct GNUNET_CLIENT_TransmitHandle *th;
166 * Head of linked list of messages we would like to transmit.
168 struct PendingMessage *pending_head;
171 * Tail of linked list of messages we would like to transmit.
173 struct PendingMessage *pending_tail;
176 * Hash map containing the current outstanding unique requests.
178 struct GNUNET_CONTAINER_MultiHashMap *active_requests;
180 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
183 * How quickly should we retry? Used for exponential back-off on
186 struct GNUNET_TIME_Relative retry_time;
189 * Generator for unique ids.
194 * Did we start our receive loop yet?
200 * Try to send messages from list of messages to send
201 * @param handle GNS_Handle
204 process_pending_messages (struct GNUNET_GNS_Handle *handle);
207 * Try to (re)connect to the GNS service.
209 * @return GNUNET_YES on success, GNUNET_NO on failure.
212 try_connect (struct GNUNET_GNS_Handle *handle)
214 if (handle->client != NULL)
216 handle->in_receive = GNUNET_NO;
217 handle->client = GNUNET_CLIENT_connect ("gns", handle->cfg);
218 if (handle->client == NULL)
220 LOG (GNUNET_ERROR_TYPE_WARNING,
221 _("Failed to connect to the GNS service!\n"));
228 * Add the request corresponding to the given handle
229 * to the pending queue (if it is not already in there).
231 * @param cls the 'struct GNUNET_GNS_Handle*'
232 * @param key key for the request (not used)
233 * @param value the 'struct GNUNET_GNS_LookupHandle*'
234 * @return GNUNET_YES (always)
237 add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
239 struct GNUNET_GNS_Handle *handle = cls;
240 struct GNUNET_GNS_LookupHandle *rh = value;
242 if (GNUNET_NO == rh->message->in_pending_queue)
245 LOG (GNUNET_ERROR_TYPE_DEBUG,
246 "Retransmitting request related to %s to GNS %p\n", GNUNET_h2s(key),
249 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
251 rh->message->in_pending_queue = GNUNET_YES;
257 * Try reconnecting to the GNS service.
259 * @param cls GNUNET_GNS_Handle
260 * @param tc scheduler context
263 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
265 struct GNUNET_GNS_Handle *handle = cls;
268 LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with GNS %p\n", handle);
270 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
271 if (handle->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
272 handle->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
274 handle->retry_time = GNUNET_TIME_relative_multiply (handle->retry_time, 2);
275 if (handle->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
276 handle->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
277 handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
278 if (GNUNET_YES != try_connect (handle))
281 LOG (GNUNET_ERROR_TYPE_DEBUG, "GNS reconnect failed(!)\n");
285 GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
286 &add_request_to_pending, handle);
287 process_pending_messages (handle);
292 * Try reconnecting to the GNS service.
294 * @param handle handle to gns to (possibly) disconnect and reconnect
297 do_disconnect (struct GNUNET_GNS_Handle *handle)
299 if (handle->client == NULL)
301 GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
302 if (NULL != handle->th)
303 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
306 "Disconnecting from GNS service, will try to reconnect in %llu ms\n",
307 (unsigned long long) handle->retry_time.rel_value);
308 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
309 handle->client = NULL;
310 handle->reconnect_task =
311 GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle);
315 * Transmit the next pending message, called by notify_transmit_ready
318 transmit_pending (void *cls, size_t size, void *buf);
321 * Handler for messages received from the GNS service
323 * @param cls the 'struct GNUNET_GNS_Handle'
324 * @param msg the incoming message
327 message_handler (void *cls, const struct GNUNET_MessageHeader *msg);
330 * Try to send messages from list of messages to send
333 process_pending_messages (struct GNUNET_GNS_Handle *handle)
335 struct PendingMessage *head;
337 if (handle->client == NULL)
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "process_pending_messages called, but client is null, reconnecting\n");
341 do_disconnect (handle);
344 if (handle->th != NULL)
346 if (NULL == (head = handle->pending_head))
349 GNUNET_CLIENT_notify_transmit_ready (handle->client,
350 ntohs (head->msg->size),
351 GNUNET_TIME_UNIT_FOREVER_REL,
352 GNUNET_YES, &transmit_pending,
354 if (NULL != handle->th)
356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357 "notify_transmit_ready returned NULL, reconnecting\n");
358 do_disconnect (handle);
363 * Transmit the next pending message, called by notify_transmit_ready
366 transmit_pending (void *cls, size_t size, void *buf)
368 struct GNUNET_GNS_Handle *handle = cls;
369 struct PendingMessage *head;
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Transmission to GNS service failed! Reconnecting!\n");
377 do_disconnect (handle);
380 if (NULL == (head = handle->pending_head))
383 tsize = ntohs (head->msg->size);
386 process_pending_messages (handle);
389 memcpy (buf, head->msg, tsize);
390 GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
392 head->in_pending_queue = GNUNET_NO;
393 if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
395 GNUNET_SCHEDULER_cancel (head->timeout_task);
396 head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
398 if (GNUNET_YES == head->free_on_send)
400 process_pending_messages (handle);
402 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 "Forwarded request of %u bytes to GNS service\n", (unsigned int) tsize);
405 if (GNUNET_NO == handle->in_receive)
408 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from GNS\n");
410 handle->in_receive = GNUNET_YES;
411 GNUNET_CLIENT_receive (handle->client, &message_handler, handle,
412 GNUNET_TIME_UNIT_FOREVER_REL);
418 * Process a given reply that might match the given
421 * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
422 * @param key query of the request
423 * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
424 * @return GNUNET_YES to continue to iterate over all results,
425 * GNUNET_NO if the reply is malformed
428 process_reply (void *cls, const GNUNET_HashCode * key, void *value)
430 const struct GNUNET_GNS_ClientResultMessage *gns_msg = cls;
431 struct GNUNET_GNS_LookupHandle *lookup_handle = value;
432 const char *name = (const char*) &lookup_handle[1];
433 const struct GNUNET_NAMESTORE_RecordData *records;
434 uint32_t num_records;
438 if (gns_msg->unique_id != lookup_handle->unique_id)
442 LOG (GNUNET_ERROR_TYPE_DEBUG,
443 "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
444 gns_msg->unique_id, lookup_handle->unique_id);
448 msize = ntohs (gns_msg->header.size);
449 num_records = ntohl (gns_msg->num_records);
451 sizeof (struct GNUNET_GNS_ClientResultMessage) +
452 sizeof (struct GNUNET_NAMESTORE_RecordData) * (num_records);
453 if ((msize < meta_length) ||
455 GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_NAMESTORE_RecordData)))
461 LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
462 (unsigned int) (msize - meta_length), GNUNET_h2s (key));
464 records = (const struct GNUNET_NAMESTORE_RecordData *) &gns_msg[1];
465 lookup_handle->iter (lookup_handle->iter_cls, name, records, num_records);
471 * Handler for messages received from the GNS service
473 * @param cls the 'struct GNUNET_GNS_Handle'
474 * @param msg the incoming message
477 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
479 struct GNUNET_GNS_Handle *handle = cls;
480 const struct GNUNET_GNS_ClientResultMessage *gns_msg;
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Error receiving data from GNS service, reconnecting\n");
488 do_disconnect (handle);
491 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT)
494 do_disconnect (handle);
497 if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientResultMessage))
500 do_disconnect (handle);
503 gns_msg = (const struct GNUNET_GNS_ClientResultMessage *) msg;
505 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from GNS service %p\n",
506 &gns_msg->name, handle);
508 /* TODO uniquely identify requests... maybe hash(name) or uid */
509 GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
510 &gns_msg->key, &process_reply,
512 GNUNET_CLIENT_receive (handle->client, &message_handler, handle,
513 GNUNET_TIME_UNIT_FOREVER_REL);
518 * Initialize the connection with the GNS service.
520 * @param cfg configuration to use
521 * @param ht_len size of the internal hash table to use for parallel requests
522 * @return handle to the GNS service, or NULL on error
524 struct GNUNET_GNS_Handle *
525 GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
528 struct GNUNET_GNS_Handle *handle;
530 handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle));
533 GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
534 handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len);
535 if (GNUNET_NO == try_connect (handle))
537 GNUNET_GNS_disconnect (handle);
545 * Shutdown connection with the GNS service.
547 * @param handle handle of the GNS connection to stop
550 GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle)
557 * Perform an asynchronous Lookup operation on the GNS.
559 * - Still not sure what we query for... "names" it is for now
560 * - Do we need such sophisticated message queueing like dht? simplify?
562 * @param handle handle to the GNS service
563 * @param timeout how long to wait for transmission of this request to the service
564 * @param name the name to look up
565 * @param iter function to call on each result
566 * @param iter_cls closure for iter
567 * @return handle to stop the async get
569 struct GNUNET_GNS_LookupHandle *
570 GNUNET_GNS_lookup_start (struct GNUNET_GNS_Handle *handle,
571 struct GNUNET_TIME_Relative timeout,
573 enum GNUNET_GNS_RecordType type,
574 GNUNET_GNS_LookupIterator iter,
577 /* IPC to look for local entries, start dht lookup, return lookup_handle */
578 struct GNUNET_GNS_ClientLookupMessage *lookup_msg;
579 struct GNUNET_GNS_LookupHandle *lookup_handle;
582 struct PendingMessage *pending;
589 GNUNET_CRYPTO_hash (name, strlen(name), &key);
591 msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name);
593 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting lookup for %s in GNS %p\n",
596 pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
597 lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
598 pending->msg = &lookup_msg->header;
599 pending->handle = handle;
600 pending->free_on_send = GNUNET_NO;
601 lookup_msg->header.size = htons (msize);
602 lookup_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP);
603 lookup_msg->key = key;
604 memcpy(&lookup_msg[1], name, strlen(name));
606 lookup_msg->unique_id = handle->uid_gen;
607 GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
609 pending->in_pending_queue = GNUNET_YES;
610 lookup_handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupHandle));
611 lookup_handle->iter = iter;
612 lookup_handle->iter_cls = iter_cls;
613 lookup_handle->message = pending;
614 lookup_handle->unique_id = lookup_msg->unique_id;
615 GNUNET_CONTAINER_multihashmap_put (handle->active_requests, &lookup_msg->key,
617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
618 process_pending_messages (handle);
619 return lookup_handle;
624 * Stop async GNS lookup.
626 * @param lookup_handle handle to the GNS lookup operation to stop
629 GNUNET_GNS_lookup_stop (struct GNUNET_GNS_LookupHandle *lookup_handle)
631 /* TODO Stop dht lookups */
635 /* end of gns_api.c */