X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fgns%2Fgns_api.c;h=84c4ae189f3ee760450062b39309285fbfc67be4;hb=ab281595eeb270120f89ec954a572f4fcf78fc53;hp=46b140ac7e7c7c798d5d96a1554d4a22d9be9ff1;hpb=7f572300440a8d059a6a35a54a8d5fd6c049c4c3;p=oweals%2Fgnunet.git diff --git a/src/gns/gns_api.c b/src/gns/gns_api.c index 46b140ac7..84c4ae189 100644 --- a/src/gns/gns_api.c +++ b/src/gns/gns_api.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2009, 2010 Christian Grothoff (and other contributing authors) + Copyright (C) 2009-2013, 2016 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,16 +14,15 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ - /** * @file gns/gns_api.c * @brief library to access the GNS service * @author Martin Schanzenbach + * @author Christian Grothoff */ - #include "platform.h" #include "gnunet_util_lib.h" #include "gnunet_constants.h" @@ -31,41 +30,52 @@ #include "gnunet_hello_lib.h" #include "gnunet_protocols.h" #include "gnunet_dht_service.h" +#include "gns.h" +#include "gnunet_gns_service.h" -#define DEBUG_GNS_API GNUNET_EXTRA_LOGGING #define LOG(kind,...) GNUNET_log_from (kind, "gns-api",__VA_ARGS__) /** - * Handle to a Lookup request + * Handle to a lookup request */ -struct GNUNET_GNS_LookupHandle +struct GNUNET_GNS_LookupRequest { /** - * Iterator to call on data receipt + * DLL */ - GNUNET_GNS_LookupIterator iter; + struct GNUNET_GNS_LookupRequest *next; /** - * Closure for the iterator callback + * DLL */ - void *iter_cls; + struct GNUNET_GNS_LookupRequest *prev; /** - * Main handle to this GNS api + * handle to gns */ struct GNUNET_GNS_Handle *gns_handle; /** - * Key that this get request is for + * processor to call on lookup result + */ + GNUNET_GNS_LookupResultProcessor lookup_proc; + + /** + * @e lookup_proc closure */ - GNUNET_HashCode key; + void *proc_cls; /** - * Unique identifier for this request (for key collisions). + * Envelope with the message for this queue entry. */ - uint64_t unique_id; + struct GNUNET_MQ_Envelope *env; + + /** + * request id + */ + uint32_t r_id; }; @@ -82,112 +92,206 @@ struct GNUNET_GNS_Handle const struct GNUNET_CONFIGURATION_Handle *cfg; /** - * Socket (if available). + * Connection to service (if available). */ - struct GNUNET_CLIENT_Connection *client; + struct GNUNET_MQ_Handle *mq; /** - * Currently pending transmission request (or NULL). + * Head of linked list of active lookup requests. */ - struct GNUNET_CLIENT_TransmitHandle *th; + struct GNUNET_GNS_LookupRequest *lookup_head; - GNUNET_SCHEDULER_TaskIdentifier reconnect_task; + /** + * Tail of linked list of active lookup requests. + */ + struct GNUNET_GNS_LookupRequest *lookup_tail; /** - * How quickly should we retry? Used for exponential back-off on - * connect-errors. + * Reconnect task */ - struct GNUNET_TIME_Relative retry_time; + struct GNUNET_SCHEDULER_Task *reconnect_task; /** - * Generator for unique ids. + * How long do we wait until we try to reconnect? */ - uint64_t uid_gen; + struct GNUNET_TIME_Relative reconnect_backoff; /** - * Did we start our receive loop yet? + * Request Id generator. Incremented by one for each request. */ - int in_receive; + uint32_t r_id_gen; + }; /** - * Try to (re)connect to the GNS service. + * Reconnect to GNS service. + * + * @param handle the handle to the GNS service + */ +static void +reconnect (struct GNUNET_GNS_Handle *handle); + + +/** + * Reconnect to GNS + * + * @param cls the handle + */ +static void +reconnect_task (void *cls) +{ + struct GNUNET_GNS_Handle *handle = cls; + + handle->reconnect_task = NULL; + reconnect (handle); +} + + +/** + * Disconnect from service and then reconnect. + * + * @param handle our handle + */ +static void +force_reconnect (struct GNUNET_GNS_Handle *handle) +{ + GNUNET_MQ_destroy (handle->mq); + handle->mq = NULL; + handle->reconnect_backoff + = GNUNET_TIME_STD_BACKOFF (handle->reconnect_backoff); + handle->reconnect_task + = GNUNET_SCHEDULER_add_delayed (handle->reconnect_backoff, + &reconnect_task, + handle); +} + + +/** + * Generic error handler, called with the appropriate error code and + * the same closure specified at the creation of the message queue. + * Not every message queue implementation supports an error handler. * - * @return GNUNET_YES on success, GNUNET_NO on failure. + * @param cls closure with the `struct GNUNET_GNS_Handle *` + * @param error error code + */ +static void +mq_error_handler (void *cls, + enum GNUNET_MQ_Error error) +{ + struct GNUNET_GNS_Handle *handle = cls; + + LOG (GNUNET_ERROR_TYPE_WARNING, + "Problem with message queue. error: %i\n", + error); + force_reconnect (handle); +} + + +/** + * Check validity of message received from the GNS service + * + * @param cls the `struct GNUNET_GNS_Handle *` + * @param loookup_msg the incoming message */ static int -try_connect (struct GNUNET_GNS_Handle *handle) +check_result (void *cls, + const struct LookupResultMessage *lookup_msg) { - if (handle->client != NULL) - return GNUNET_OK; - handle->in_receive = GNUNET_NO; - handle->client = GNUNET_CLIENT_connect ("gns", handle->cfg); - if (handle->client == NULL) + size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); + uint32_t rd_count = ntohl (lookup_msg->rd_count); + struct GNUNET_GNSRECORD_Data rd[rd_count]; + + if (GNUNET_SYSERR == + GNUNET_GNSRECORD_records_deserialize (mlen, + (const char*) &lookup_msg[1], + rd_count, + rd)) { - LOG (GNUNET_ERROR_TYPE_WARNING, - _("Failed to connect to the GNS service!\n")); - return GNUNET_NO; + GNUNET_break (0); + return GNUNET_SYSERR; } - return GNUNET_YES; + return GNUNET_OK; } + /** - * Try reconnecting to the GNS service. + * Handler for messages received from the GNS service * - * @param cls GNUNET_GNS_Handle - * @param tc scheduler context + * @param cls the `struct GNUNET_GNS_Handle *` + * @param loookup_msg the incoming message */ static void -try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +handle_result (void *cls, + const struct LookupResultMessage *lookup_msg) { struct GNUNET_GNS_Handle *handle = cls; - -#if DEBUG_DHT - LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with GNS %p\n", handle); -#endif - handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - if (handle->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value) - handle->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY; - else - handle->retry_time = GNUNET_TIME_relative_multiply (handle->retry_time, 2); - if (handle->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value) - handle->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT; - handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK; - if (GNUNET_YES != try_connect (handle)) - { -#if DEBUG_DHT - LOG (GNUNET_ERROR_TYPE_DEBUG, "GNS reconnect failed(!)\n"); -#endif + size_t mlen = ntohs (lookup_msg->header.size) - sizeof (*lookup_msg); + uint32_t rd_count = ntohl (lookup_msg->rd_count); + struct GNUNET_GNSRECORD_Data rd[rd_count]; + uint32_t r_id = ntohl (lookup_msg->id); + struct GNUNET_GNS_LookupRequest *lr; + GNUNET_GNS_LookupResultProcessor proc; + void *proc_cls; + + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Received lookup reply from GNS service (%u records)\n", + (unsigned int) rd_count); + for (lr = handle->lookup_head; NULL != lr; lr = lr->next) + if (lr->r_id == r_id) + break; + if (NULL == lr) return; - } - GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests, - &add_request_to_pending, handle); - process_pending_messages (handle); + proc = lr->lookup_proc; + proc_cls = lr->proc_cls; + + GNUNET_assert (GNUNET_OK == + GNUNET_GNSRECORD_records_deserialize (mlen, + (const char*) &lookup_msg[1], + rd_count, + rd)); + proc (proc_cls, + rd_count, + rd); + GNUNET_CONTAINER_DLL_remove (handle->lookup_head, + handle->lookup_tail, + lr); + if (NULL != lr->env) + GNUNET_MQ_discard (lr->env); + GNUNET_free (lr); } /** - * Try reconnecting to the GNS service. + * Reconnect to GNS service. * - * @param handle handle to gns to (possibly) disconnect and reconnect + * @param handle the handle to the GNS service */ static void -do_disconnect (struct GNUNET_GNS_Handle *handle) +reconnect (struct GNUNET_GNS_Handle *handle) { - if (handle->client == NULL) + struct GNUNET_MQ_MessageHandler handlers[] = { + GNUNET_MQ_hd_var_size (result, + GNUNET_MESSAGE_TYPE_GNS_LOOKUP_RESULT, + struct LookupResultMessage, + handle), + GNUNET_MQ_handler_end () + }; + struct GNUNET_GNS_LookupRequest *lh; + + GNUNET_assert (NULL == handle->mq); + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trying to connect to GNS\n"); + handle->mq = GNUNET_CLIENT_connect (handle->cfg, + "gns", + handlers, + &mq_error_handler, + handle); + if (NULL == handle->mq) return; - GNUNET_assert (handle->reconnect_task == GNUNET_SCHEDULER_NO_TASK); - if (NULL != handle->th) - GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th); - handle->th = NULL; - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Disconnecting from GNS service, will try to reconnect in %llu ms\n", - (unsigned long long) handle->retry_time.rel_value); - GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO); - handle->client = NULL; - handle->reconnect_task = - GNUNET_SCHEDULER_add_delayed (handle->retry_time, &try_reconnect, handle); + for (lh = handle->lookup_head; NULL != lh; lh = lh->next) + GNUNET_MQ_send_copy (handle->mq, + lh->env); } @@ -198,19 +302,16 @@ do_disconnect (struct GNUNET_GNS_Handle *handle) * @return handle to the GNS service, or NULL on error */ struct GNUNET_GNS_Handle * -GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, - unsigned int ht_len) +GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_GNS_Handle *handle; - handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_Handle)); + handle = GNUNET_new (struct GNUNET_GNS_Handle); handle->cfg = cfg; - handle->uid_gen = - GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); - handle->active_requests = GNUNET_CONTAINER_multihashmap_create (ht_len); - if (GNUNET_NO == try_connect (handle)) + reconnect (handle); + if (NULL == handle->mq) { - GNUNET_GNS_disconnect (handle); + GNUNET_free (handle); return NULL; } return handle; @@ -225,79 +326,101 @@ GNUNET_GNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void GNUNET_GNS_disconnect (struct GNUNET_GNS_Handle *handle) { - /* disco from GNS */ + if (NULL != handle->mq) + { + GNUNET_MQ_destroy (handle->mq); + handle->mq = NULL; + } + if (NULL != handle->reconnect_task) + { + GNUNET_SCHEDULER_cancel (handle->reconnect_task); + handle->reconnect_task = NULL; + } + GNUNET_assert (NULL == handle->lookup_head); + GNUNET_free (handle); } /** - * Add a new record to the GNS. + * Cancel pending lookup request * - * @param handle handle to GNS service - * @param key the key to store under - * @param desired_replication_level estimate of how many - * nearest peers this request should reach - * @param options routing options for this message - * @param type type of the value - * @param size number of bytes in data; must be less than 64k - * @param data the data to store - * @param exp desired expiration time for the value - * @param timeout how long to wait for transmission of this request - * @param cont continuation to call when done (transmitting request to service) - * @param cont_cls closure for cont + * @param lr the lookup request to cancel */ void -GNUNET_GNS_add_record (struct GNUNET_GNS_Handle *handle, const GNUNET_HashCode * key, - uint32_t desired_replication_level, - enum GNUNET_DHT_RouteOption options, - enum GNUNET_BLOCK_Type type, size_t size, const char *data, - struct GNUNET_TIME_Absolute exp, - struct GNUNET_TIME_Relative timeout, GNUNET_SCHEDULER_Task cont, - void *cont_cls) +GNUNET_GNS_lookup_cancel (struct GNUNET_GNS_LookupRequest *lr) { - /* add record to local db, dht; sign etc */ + struct GNUNET_GNS_Handle *handle = lr->gns_handle; + + GNUNET_CONTAINER_DLL_remove (handle->lookup_head, + handle->lookup_tail, + lr); + GNUNET_MQ_discard (lr->env); + GNUNET_free (lr); } /** - * Perform an asynchronous Lookup operation on the GNS. + * Perform an asynchronous lookup operation on the GNS. * * @param handle handle to the GNS service - * @param timeout how long to wait for transmission of this request to the service - * @param type expected type of the response object - * @param key the key to look up - * @param desired_replication_level estimate of how many - nearest peers this request should reach - * @param options routing options for this message - * @param xquery extended query data (can be NULL, depending on type) - * @param xquery_size number of bytes in xquery - * @param iter function to call on each result - * @param iter_cls closure for iter - * @return handle to stop the async get + * @param name the name to look up + * @param zone the zone to start the resolution in + * @param type the record type to look up + * @param options local options for the lookup + * @param proc processor to call on result + * @param proc_cls closure for @a proc + * @return handle to the get request */ -struct GNUNET_GNS_LookupHandle * -GNUNET_GNS_lookup_start (struct GNUNET_GNS_Handle *handle, - struct GNUNET_TIME_Relative timeout, - enum GNUNET_BLOCK_Type type, const GNUNET_HashCode * key, - uint32_t desired_replication_level, - enum GNUNET_DHT_RouteOption options, const void *xquery, - size_t xquery_size, GNUNET_GNS_LookupIterator iter, - void *iter_cls) +struct GNUNET_GNS_LookupRequest* +GNUNET_GNS_lookup (struct GNUNET_GNS_Handle *handle, + const char *name, + const struct GNUNET_CRYPTO_EcdsaPublicKey *zone, + uint32_t type, + enum GNUNET_GNS_LocalOptions options, + GNUNET_GNS_LookupResultProcessor proc, + void *proc_cls) { - /* look for local entries, start dht lookup, return lookup_handle */ -} - + /* IPC to shorten gns names, return shorten_handle */ + struct LookupMessage *lookup_msg; + struct GNUNET_GNS_LookupRequest *lr; + size_t nlen; -/** - * Stop async GNS lookup. - * - * @param lookup_handle handle to the GNS lookup operation to stop - */ -void -GNUNET_GNS_lookup_stop (struct GNUNET_GNS_LookupHandle *lookup_handle) -{ - struct GNUNET_DHT_Handle *handle; - /* TODO Stop dht lookups */ + if (NULL == name) + { + GNUNET_break (0); + return NULL; + } + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Trying to lookup `%s' in GNS\n", + name); + nlen = strlen (name) + 1; + if (nlen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (*lr)) + { + GNUNET_break (0); + return NULL; + } + lr = GNUNET_new (struct GNUNET_GNS_LookupRequest); + lr->gns_handle = handle; + lr->lookup_proc = proc; + lr->proc_cls = proc_cls; + lr->r_id = handle->r_id_gen++; + lr->env = GNUNET_MQ_msg_extra (lookup_msg, + nlen, + GNUNET_MESSAGE_TYPE_GNS_LOOKUP); + lookup_msg->id = htonl (lr->r_id); + lookup_msg->options = htons ((uint16_t) options); + lookup_msg->zone = *zone; + lookup_msg->type = htonl (type); + GNUNET_memcpy (&lookup_msg[1], + name, + nlen); + GNUNET_CONTAINER_DLL_insert (handle->lookup_head, + handle->lookup_tail, + lr); + if (NULL != handle->mq) + GNUNET_MQ_send_copy (handle->mq, + lr->env); + return lr; } - /* end of gns_api.c */