*/
/**
- * TODO: Do we really need a client API?
*
* @file gns/gns_api.c
* @brief library to access the GNS service
/* TODO into gnunet_protocols */
#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP 23
-#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT 24
+#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP_RESULT 24
+#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_SHORTEN 25
+#define GNUNET_MESSAGE_TYPE_GNS_CLIENT_SHORTEN_RESULT 26
/**
* Entry in our list of messages to be (re-)transmitted.
*/
void *cont_cls;
- /**
- * Timeout task for this message
- */
- GNUNET_SCHEDULER_TaskIdentifier timeout_task;
-
/**
* Unique ID for this request
*/
};
+
+/**
+ * Handle to a shorten request
+ */
+struct GNUNET_GNS_ShortenHandle
+{
+
+ /**
+ * Processor to call on data receipt
+ */
+ GNUNET_GNS_ShortenResultProcessor proc;
+
+ /**
+ * Closure for the processor
+ */
+ void *proc_cls;
+
+ /**
+ * Main handle to this GNS api
+ */
+ struct GNUNET_GNS_Handle *gns_handle;
+
+ /**
+ * Key that this get request is for
+ */
+ GNUNET_HashCode key;
+
+ /**
+ * Unique identifier for this request (for key collisions).
+ */
+ uint64_t unique_id;
+
+ struct PendingMessage *message;
+
+};
+
+
/**
* Connection to the GNS service.
*/
struct PendingMessage *pending_tail;
/**
- * Hash map containing the current outstanding unique requests.
+ * Hash maps containing the current outstanding unique requests.
*/
- struct GNUNET_CONTAINER_MultiHashMap *active_requests;
+ struct GNUNET_CONTAINER_MultiHashMap *active_lookup_requests;
+ struct GNUNET_CONTAINER_MultiHashMap *active_shorten_requests;
GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
* @return GNUNET_YES (always)
*/
static int
-add_request_to_pending (void *cls, const GNUNET_HashCode * key, void *value)
+add_lookup_request_to_pending (void *cls, const GNUNET_HashCode * key,
+ void *value)
{
struct GNUNET_GNS_Handle *handle = cls;
- struct GNUNET_GNS_LookupHandle *rh = value;
+ struct GNUNET_GNS_LookupHandle *lh = value;
- if (GNUNET_NO == rh->message->in_pending_queue)
+ if (GNUNET_NO == lh->message->in_pending_queue)
{
-#if DEBUG_DHT
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Retransmitting request related to %s to GNS %p\n", GNUNET_h2s(key),
handle);
-#endif
GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
- rh->message);
- rh->message->in_pending_queue = GNUNET_YES;
+ lh->message);
+ lh->message->in_pending_queue = GNUNET_YES;
}
return GNUNET_YES;
}
+
+/**
+ * Add the request corresponding to the given handle
+ * to the pending queue (if it is not already in there).
+ *
+ * @param cls the 'struct GNUNET_GNS_Handle*'
+ * @param key key for the request (not used)
+ * @param value the 'struct GNUNET_GNS_ShortenHandle*'
+ * @return GNUNET_YES (always)
+ */
+static int
+add_shorten_request_to_pending (void *cls, const GNUNET_HashCode * key,
+ void *value)
+{
+ struct GNUNET_GNS_Handle *handle = cls;
+ struct GNUNET_GNS_ShortenHandle *sh = value;
+
+ if (GNUNET_NO == sh->message->in_pending_queue)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Retransmitting shorten request related to %s to GNS %p\n",
+ GNUNET_h2s(key),
+ handle);
+ GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
+ sh->message);
+ sh->message->in_pending_queue = GNUNET_YES;
+ }
+ return GNUNET_YES;
+}
+
+
/**
* Try reconnecting to the GNS service.
*
{
struct GNUNET_GNS_Handle *handle = cls;
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with GNS %p\n", handle);
-#endif
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting with GNS %p\n", handle);
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;
handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
if (GNUNET_YES != try_connect (handle))
{
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "GNS reconnect failed(!)\n");
-#endif
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "GNS reconnect failed(!)\n");
return;
}
- GNUNET_CONTAINER_multihashmap_iterate (handle->active_requests,
- &add_request_to_pending, handle);
+ GNUNET_CONTAINER_multihashmap_iterate (handle->active_lookup_requests,
+ &add_lookup_request_to_pending, handle);
+ GNUNET_CONTAINER_multihashmap_iterate (handle->active_shorten_requests,
+ &add_shorten_request_to_pending, handle);
process_pending_messages (handle);
}
if (handle->client == NULL)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "process_pending_messages called, but client is null, reconnecting\n");
+ "process_pending_messages called, but client is null, reconnecting\n");
do_disconnect (handle);
return;
}
+
if (handle->th != NULL)
return;
+
if (NULL == (head = handle->pending_head))
return;
+
handle->th =
GNUNET_CLIENT_notify_transmit_ready (handle->client,
ntohs (head->msg->size),
size_t tsize;
handle->th = NULL;
+
if (buf == NULL)
{
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
do_disconnect (handle);
return 0;
}
+
if (NULL == (head = handle->pending_head))
return 0;
tsize = ntohs (head->msg->size);
+
if (size < tsize)
{
process_pending_messages (handle);
return 0;
}
+
memcpy (buf, head->msg, tsize);
+
GNUNET_CONTAINER_DLL_remove (handle->pending_head, handle->pending_tail,
head);
+
head->in_pending_queue = GNUNET_NO;
- if (head->timeout_task != GNUNET_SCHEDULER_NO_TASK)
- {
- GNUNET_SCHEDULER_cancel (head->timeout_task);
- head->timeout_task = GNUNET_SCHEDULER_NO_TASK;
- }
+
if (GNUNET_YES == head->free_on_send)
GNUNET_free (head);
+
process_pending_messages (handle);
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Forwarded request of %u bytes to GNS service\n", (unsigned int) tsize);
-#endif
+
if (GNUNET_NO == handle->in_receive)
{
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting to process replies from GNS\n");
-#endif
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Starting to process replies from GNS\n");
handle->in_receive = GNUNET_YES;
GNUNET_CLIENT_receive (handle->client, &message_handler, handle,
GNUNET_TIME_UNIT_FOREVER_REL);
* GNUNET_NO if the reply is malformed
*/
static int
-process_reply (void *cls, const GNUNET_HashCode * key, void *value)
+process_shorten_reply (void *cls, const GNUNET_HashCode * key, void *value)
{
- const struct GNUNET_GNS_ClientResultMessage *gns_msg = cls;
- struct GNUNET_GNS_LookupHandle *lookup_handle = value;
- const char *name = (const char*) &lookup_handle[1];
- const struct GNUNET_NAMESTORE_RecordData *records;
- uint32_t num_records;
- size_t meta_length;
- size_t msize;
+ const struct GNUNET_GNS_ClientShortenResultMessage *gns_msg = cls;
+ struct GNUNET_GNS_ShortenHandle *shorten_handle = value;
+ const char *name = (const char*) &shorten_handle[1];
+ const char *short_name;
- if (gns_msg->unique_id != lookup_handle->unique_id)
+ if (ntohs (((struct GNUNET_MessageHeader*)gns_msg)->size) <
+ sizeof (struct GNUNET_GNS_ClientShortenResultMessage))
+ {
+ GNUNET_break (0);
+ do_disconnect (shorten_handle->gns_handle);
+ return GNUNET_NO;
+ }
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received reply for `%s' from GNS service %p\n",
+ name, shorten_handle->gns_handle);
+
+ if (gns_msg->unique_id != shorten_handle->unique_id)
{
/* UID mismatch */
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
- gns_msg->unique_id, lookup_handle->unique_id);
-#endif
+ gns_msg->unique_id, shorten_handle->unique_id);
return GNUNET_YES;
}
- msize = ntohs (gns_msg->header.size);
- num_records = ntohl (gns_msg->num_records);
- meta_length =
- sizeof (struct GNUNET_GNS_ClientResultMessage) +
- sizeof (struct GNUNET_NAMESTORE_RecordData) * (num_records);
- if ((msize < meta_length) ||
- (num_records >
- GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_NAMESTORE_RecordData)))
+ short_name = (char*)&gns_msg[1];
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Giving shorten reply %s for %s to application\n",
+ short_name, name);
+
+ shorten_handle->proc (shorten_handle->proc_cls, name, short_name);
+
+ GNUNET_CLIENT_receive (shorten_handle->gns_handle->client,
+ &message_handler, shorten_handle->gns_handle,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ return GNUNET_YES;
+}
+
+
+/**
+ * Process a given reply to the lookup request
+ *
+ * @param cls the 'struct GNUNET_GNS_ClientResultMessage'
+ * @param key query of the request
+ * @param value the 'struct GNUNET_GNS_LookupHandle' of a request matching the same key
+ * @return GNUNET_YES to continue to iterate over all results,
+ * GNUNET_NO if the reply is malformed
+ */
+static int
+process_lookup_reply (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ const struct GNUNET_GNS_ClientLookupResultMessage *gns_msg = cls;
+ struct GNUNET_GNS_LookupHandle *lookup_handle = value;
+ const char *name = (const char*) &lookup_handle[1];
+ struct GNUNET_NAMESTORE_RecordData *rd;
+ uint32_t rd_count;
+
+ if (ntohs (((struct GNUNET_MessageHeader*)gns_msg)->size) <
+ sizeof (struct GNUNET_GNS_ClientLookupResultMessage))
{
GNUNET_break (0);
+ do_disconnect (lookup_handle->gns_handle);
return GNUNET_NO;
}
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Giving %u byte reply for %s to application\n",
- (unsigned int) (msize - meta_length), GNUNET_h2s (key));
-#endif
- records = (const struct GNUNET_NAMESTORE_RecordData *) &gns_msg[1];
- lookup_handle->iter (lookup_handle->iter_cls, name, records, num_records);
+
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Received lookup reply for `%s' from GNS service %p\n",
+ name, lookup_handle->gns_handle);
+
+ if (gns_msg->unique_id != lookup_handle->unique_id)
+ {
+ /* UID mismatch */
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Ignoring reply for %s: UID mismatch: %llu/%llu\n", GNUNET_h2s (key),
+ gns_msg->unique_id, lookup_handle->unique_id);
+ return GNUNET_YES;
+ }
+
+ rd_count = ntohl(gns_msg->rd_count);
+ rd = (struct GNUNET_NAMESTORE_RecordData*)&gns_msg[1];
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Giving lookup reply for %s to application\n",
+ name);
+
+ lookup_handle->iter (lookup_handle->iter_cls, name, rd_count, rd);
+
+ GNUNET_CLIENT_receive (lookup_handle->gns_handle->client, &message_handler,
+ lookup_handle->gns_handle,
+ GNUNET_TIME_UNIT_FOREVER_REL);
return GNUNET_YES;
}
-
/**
* Handler for messages received from the GNS service
*
message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_GNS_Handle *handle = cls;
- const struct GNUNET_GNS_ClientResultMessage *gns_msg;
+ const struct GNUNET_GNS_ClientLookupResultMessage *lookup_msg;
+ const struct GNUNET_GNS_ClientShortenResultMessage *shorten_msg;
if (msg == NULL)
{
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG,
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Error receiving data from GNS service, reconnecting\n");
-#endif
do_disconnect (handle);
return;
}
- if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_GNS_CLIENT_RESULT)
+
+ if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_GNS_CLIENT_LOOKUP_RESULT)
{
- GNUNET_break (0);
- do_disconnect (handle);
- return;
+ lookup_msg = (const struct GNUNET_GNS_ClientLookupResultMessage *) msg;
+ GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_lookup_requests,
+ &lookup_msg->key,
+ &process_lookup_reply,
+ (void *) lookup_msg);
}
- if (ntohs (msg->size) < sizeof (struct GNUNET_GNS_ClientResultMessage))
+ else if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_GNS_CLIENT_SHORTEN_RESULT)
+ {
+ shorten_msg = (const struct GNUNET_GNS_ClientShortenResultMessage *) msg;
+ GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_shorten_requests,
+ &shorten_msg->key,
+ &process_shorten_reply,
+ (void *) shorten_msg);
+ }
+ else
{
GNUNET_break (0);
do_disconnect (handle);
return;
}
- gns_msg = (const struct GNUNET_GNS_ClientResultMessage *) msg;
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Received reply for `%s' from GNS service %p\n",
- &gns_msg->name, handle);
-#endif
- /* TODO uniquely identify requests... maybe hash(name) or uid */
- GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
- &gns_msg->key, &process_reply,
- (void *) gns_msg);
- GNUNET_CLIENT_receive (handle->client, &message_handler, handle,
- GNUNET_TIME_UNIT_FOREVER_REL);
+
}
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);
+ handle->active_lookup_requests =
+ GNUNET_CONTAINER_multihashmap_create (ht_len);
+ handle->active_shorten_requests =
+ GNUNET_CONTAINER_multihashmap_create (ht_len);
if (GNUNET_NO == try_connect (handle))
{
GNUNET_GNS_disconnect (handle);
/**
* Perform an asynchronous Lookup operation on the GNS.
- * TODO:
- * - Still not sure what we query for... "names" it is for now
- * - Do we need such sophisticated message queueing like dht? simplify?
*
* @param handle handle to the GNS service
- * @param timeout how long to wait for transmission of this request to the service
* @param name the name to look up
* @param iter function to call on each result
* @param iter_cls closure for iter
*/
struct GNUNET_GNS_LookupHandle *
GNUNET_GNS_lookup_start (struct GNUNET_GNS_Handle *handle,
- struct GNUNET_TIME_Relative timeout,
const char * name,
enum GNUNET_GNS_RecordType type,
GNUNET_GNS_LookupIterator iter,
GNUNET_CRYPTO_hash (name, strlen(name), &key);
msize = sizeof (struct GNUNET_GNS_ClientLookupMessage) + strlen(name);
-#if DEBUG_GNS
- LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting lookup for %s in GNS %p\n",
- name, handle);
-#endif
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting lookup for %s in GNS %p\n",
+ name, handle);
pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
lookup_msg = (struct GNUNET_GNS_ClientLookupMessage *) &pending[1];
pending->msg = &lookup_msg->header;
lookup_handle->iter_cls = iter_cls;
lookup_handle->message = pending;
lookup_handle->unique_id = lookup_msg->unique_id;
- GNUNET_CONTAINER_multihashmap_put (handle->active_requests, &lookup_msg->key,
- lookup_handle,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GNUNET_CONTAINER_multihashmap_put (handle->active_lookup_requests,
+ &lookup_msg->key,
+ lookup_handle,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
process_pending_messages (handle);
return lookup_handle;
}
void
GNUNET_GNS_lookup_stop (struct GNUNET_GNS_LookupHandle *lookup_handle)
{
- /* TODO Stop dht lookups */
+ /* TODO Stop gns lookups */
+}
+
+
+/**
+ * Perform a name shortening operation on the GNS.
+ *
+ * @param handle handle to the GNS service
+ * @param name the name to look up
+ * @param proc function to call on result
+ * @param proc_cls closure for processor
+ * @return handle to the operation
+ */
+struct GNUNET_GNS_ShortenHandle *
+GNUNET_GNS_shorten (struct GNUNET_GNS_Handle *handle,
+ const char * name,
+ GNUNET_GNS_ShortenResultProcessor proc,
+ void *proc_cls)
+{
+ /* IPC to shorten gns names, return shorten_handle */
+ struct GNUNET_GNS_ClientShortenMessage *shorten_msg;
+ struct GNUNET_GNS_ShortenHandle *shorten_handle;
+ size_t msize;
+ struct PendingMessage *pending;
+
+ if (NULL == name)
+ {
+ return NULL;
+ }
+
+ msize = sizeof (struct GNUNET_GNS_ClientShortenMessage) + strlen(name);
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to shorten %s in GNS\n", name);
+ pending = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
+ shorten_msg = (struct GNUNET_GNS_ClientShortenMessage *) &pending[1];
+ pending->msg = &shorten_msg->header;
+ pending->handle = handle;
+ pending->free_on_send = GNUNET_NO;
+ shorten_msg->header.size = htons (msize);
+ shorten_msg->header.type = htons (GNUNET_MESSAGE_TYPE_GNS_CLIENT_SHORTEN);
+ memcpy(&shorten_msg[1], name, strlen(name));
+ handle->uid_gen++;
+ shorten_msg->unique_id = handle->uid_gen;
+ GNUNET_CONTAINER_DLL_insert (handle->pending_head, handle->pending_tail,
+ pending);
+ pending->in_pending_queue = GNUNET_YES;
+ shorten_handle = GNUNET_malloc (sizeof (struct GNUNET_GNS_LookupHandle));
+ shorten_handle->proc = proc;
+ shorten_handle->proc_cls = proc_cls;
+ shorten_handle->message = pending;
+ shorten_handle->unique_id = shorten_msg->unique_id;
+ GNUNET_CONTAINER_multihashmap_put (handle->active_shorten_requests,
+ &shorten_msg->key,
+ shorten_handle,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ process_pending_messages (handle);
+ return shorten_handle;
}
+
/* end of gns_api.c */