/*
This file is part of GNUnet.
- (C) 2009, 2010 Christian Grothoff (and other contributing authors)
+ (C) 2009--2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 2, or (at your
+ by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
* @author Nathan Evans
*/
#include "platform.h"
-#include "gnunet_bandwidth_lib.h"
-#include "gnunet_client_lib.h"
-#include "gnunet_constants.h"
-#include "gnunet_container_lib.h"
-#include "gnunet_arm_service.h"
-#include "gnunet_hello_lib.h"
-#include "gnunet_protocols.h"
-#include "gnunet_server_lib.h"
-#include "gnunet_time_lib.h"
+#include "gnunet_util_lib.h"
#include "gnunet_dv_service.h"
+#include "gnunet_protocols.h"
#include "dv.h"
-#include "../transport/plugin_transport.h"
+#include "gnunet_transport_plugin.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "dv-api",__VA_ARGS__)
+
/**
- * Store ready to send messages
+ * Handle for a send operation.
*/
-struct PendingMessages
+struct GNUNET_DV_TransmitHandle
{
/**
- * Linked list of pending messages
+ * Kept in a DLL.
*/
- struct PendingMessages *next;
+ struct GNUNET_DV_TransmitHandle *next;
/**
- * Message that is pending
+ * Kept in a DLL.
*/
- struct GNUNET_DV_SendMessage *msg;
+ struct GNUNET_DV_TransmitHandle *prev;
/**
- * Timeout for this message
+ * Handle to the service.
*/
- struct GNUNET_TIME_Absolute timeout;
-
-};
+ struct GNUNET_DV_ServiceHandle *sh;
-/**
- * Handle for the service.
- */
-struct GNUNET_DV_Handle
-{
/**
- * Our scheduler.
+ * Function to call upon completion.
*/
- struct GNUNET_SCHEDULER_Handle *sched;
+ GNUNET_DV_MessageSentCallback cb;
/**
- * Configuration to use.
+ * Closure for 'cb'.
*/
- const struct GNUNET_CONFIGURATION_Handle *cfg;
-
+ void *cb_cls;
+
/**
- * Socket (if available).
+ * The actual message (allocated at the end of this struct).
*/
- struct GNUNET_CLIENT_Connection *client;
+ const struct GNUNET_MessageHeader *msg;
/**
- * Currently pending transmission request.
+ * Destination for the message.
*/
- struct GNUNET_CLIENT_TransmitHandle *th;
+ struct GNUNET_PeerIdentity target;
/**
- * List of the currently pending messages for the DV service.
+ * UID of our message, if any.
*/
- struct PendingMessages *pending_list;
+ uint32_t uid;
+
+};
+
+
+/**
+ * Handle to the DV service.
+ */
+struct GNUNET_DV_ServiceHandle
+{
/**
- * Message we are currently sending.
+ * Connection to DV service.
*/
- struct PendingMessages *current;
+ struct GNUNET_CLIENT_Connection *client;
/**
- * Handler for messages we receive from the DV service
+ * Active request for transmission to DV service.
*/
- GNUNET_DV_MessageReceivedHandler receive_handler;
+ struct GNUNET_CLIENT_TransmitHandle *th;
/**
- * Closure for the receive handler
+ * Our configuration.
*/
- void *receive_cls;
+ const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
- * Current unique ID
+ * Closure for the callbacks.
*/
- uint32_t uid_gen;
-
+ void *cls;
+
/**
- * Hashmap containing outstanding send requests awaiting confirmation.
+ * Function to call on connect events.
*/
- struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
-
-};
+ GNUNET_DV_ConnectCallback connect_cb;
+ /**
+ * Function to call on distance change events.
+ */
+ GNUNET_DV_DistanceChangedCallback distance_cb;
-struct StartContext
-{
/**
- * Start message
+ * Function to call on disconnect events.
*/
- struct GNUNET_MessageHeader *message;
+ GNUNET_DV_DisconnectCallback disconnect_cb;
/**
- * Handle to service, in case of timeout
+ * Function to call on receiving messages events.
*/
- struct GNUNET_DV_Handle *handle;
-};
+ GNUNET_DV_MessageReceivedCallback message_cb;
-struct SendCallbackContext
-{
/**
- * The continuation to call once a message is confirmed sent (or failed)
+ * Head of messages to transmit.
*/
- GNUNET_TRANSPORT_TransmitContinuation cont;
+ struct GNUNET_DV_TransmitHandle *th_head;
/**
- * Closure to call with send continuation.
+ * Tail of messages to transmit.
*/
- void *cont_cls;
+ struct GNUNET_DV_TransmitHandle *th_tail;
/**
- * Target of the message.
+ * Mapping of peer identities to TransmitHandles to invoke
+ * upon successful transmission. The respective
+ * transmissions have already been done.
*/
- struct GNUNET_PeerIdentity target;
-};
+ struct GNUNET_CONTAINER_MultiHashMap *send_callbacks;
-/**
- * Convert unique ID to hash code.
- *
- * @param uid unique ID to convert
- * @param hash set to uid (extended with zeros)
- */
-static void
-hash_from_uid (uint32_t uid,
- GNUNET_HashCode *hash)
-{
- memset (hash, 0, sizeof(GNUNET_HashCode));
- *((uint32_t*)hash) = uid;
-}
+ /**
+ * Current unique ID
+ */
+ uint32_t uid_gen;
-/**
- * Try to (re)connect to the dv service.
- *
- * @param ret handle to the (disconnected) dv service
- *
- * @return GNUNET_YES on success, GNUNET_NO on failure.
- */
-static int
-try_connect (struct GNUNET_DV_Handle *ret)
-{
- if (ret->client != NULL)
- return GNUNET_OK;
- ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
- if (ret->client != NULL)
- return GNUNET_YES;
-#if DEBUG_DV_MESSAGES
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- _("Failed to connect to the dv service!\n"));
-#endif
- return GNUNET_NO;
-}
+};
-static void process_pending_message(struct GNUNET_DV_Handle *handle);
/**
- * Send complete, schedule next
+ * Disconnect and then reconnect to the DV service.
*
- * @param handle handle to the dv service
- * @param code return code for send (unused)
+ * @param sh service handle
*/
static void
-finish (struct GNUNET_DV_Handle *handle, int code)
-{
- struct PendingMessages *pos = handle->current;
- handle->current = NULL;
- process_pending_message (handle);
+reconnect (struct GNUNET_DV_ServiceHandle *sh);
- GNUNET_free(pos->msg);
- GNUNET_free (pos);
-}
/**
- * Notification that we can send data
+ * Gives a message from our queue to the DV service.
*
- * @param cls handle to the dv service (struct GNUNET_DV_Handle)
+ * @param cls handle to the dv service (struct GNUNET_DV_ServiceHandle)
* @param size how many bytes can we send
* @param buf where to copy the message to send
- *
* @return how many bytes we copied to buf
*/
static size_t
transmit_pending (void *cls, size_t size, void *buf)
{
- struct GNUNET_DV_Handle *handle = cls;
+ struct GNUNET_DV_ServiceHandle *sh = cls;
+ char *cbuf = buf;
+ struct GNUNET_DV_TransmitHandle *th;
size_t ret;
size_t tsize;
-#if DEBUG_DV
- if (handle->current != NULL)
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending called with message type %d\n", ntohs(handle->current->msg->header.type));
-#endif
-
- if (buf == NULL)
- {
-#if DEBUG_DV
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
-#endif
- finish(handle, GNUNET_SYSERR);
- return 0;
- }
- handle->th = NULL;
-
+ sh->th = NULL;
+ if (NULL == buf)
+ {
+ reconnect (sh);
+ return 0;
+ }
ret = 0;
-
- if (handle->current != NULL)
+ while ( (NULL != (th = sh->th_head)) &&
+ (size - ret >= (tsize = ntohs (th->msg->size)) ))
{
- tsize = ntohs(handle->current->msg->header.size);
- if (size >= tsize)
+ GNUNET_CONTAINER_DLL_remove (sh->th_head,
+ sh->th_tail,
+ th);
+ memcpy (&cbuf[ret], th->msg, tsize);
+ ret += tsize;
+ if (NULL != th->cb)
{
- memcpy(buf, handle->current->msg, tsize);
-#if DEBUG_DV
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
-#endif
- finish(handle, GNUNET_OK);
- return tsize;
+ (void) GNUNET_CONTAINER_multihashmap_put (sh->send_callbacks,
+ &th->target.hashPubKey,
+ th,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ }
+ else
+ {
+ GNUNET_free (th);
}
-
}
-
return ret;
}
+
/**
- * Try to send messages from list of messages to send
+ * Start sending messages from our queue to the service.
*
- * @param handle handle to the distance vector service
+ * @param sh service handle
*/
-static void process_pending_message(struct GNUNET_DV_Handle *handle)
+static void
+start_transmit (struct GNUNET_DV_ServiceHandle *sh)
{
-
- if (handle->current != NULL)
- return; /* action already pending */
- if (GNUNET_YES != try_connect (handle))
- {
- finish (handle, GNUNET_SYSERR);
- return;
- }
-
- /* schedule next action */
- handle->current = handle->pending_list;
- if (NULL == handle->current)
- {
- return;
- }
- handle->pending_list = handle->pending_list->next;
- handle->current->next = NULL;
-
- if (NULL ==
- (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
- ntohs(handle->current->msg->header.size),
- handle->current->msg->timeout,
- GNUNET_YES,
- &transmit_pending, handle)))
- {
-#if DEBUG_DV
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "Failed to transmit request to dv service.\n");
-#endif
- finish (handle, GNUNET_SYSERR);
- }
+ if (NULL != sh->th)
+ return;
+ if (NULL == sh->th_head)
+ return;
+ sh->th =
+ GNUNET_CLIENT_notify_transmit_ready (sh->client,
+ ntohs (sh->th_head->msg->size),
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_NO,
+ &transmit_pending, sh);
}
+
/**
- * Add a pending message to the linked list
- *
- * @param handle handle to the specified DV api
- * @param msg the message to add to the list
+ * Closure for 'process_ack'.
*/
-static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMessage *msg)
+struct AckContext
{
- struct PendingMessages *new_message;
- struct PendingMessages *pos;
- struct PendingMessages *last;
+ /**
+ * The ACK message.
+ */
+ const struct GNUNET_DV_AckMessage *ack;
- new_message = GNUNET_malloc(sizeof(struct PendingMessages));
- new_message->msg = msg;
+ /**
+ * Our service handle.
+ */
+ struct GNUNET_DV_ServiceHandle *sh;
+};
- if (handle->pending_list != NULL)
- {
- pos = handle->pending_list;
- while(pos != NULL)
- {
- last = pos;
- pos = pos->next;
- }
- last->next = new_message;
- }
- else
- {
- handle->pending_list = new_message;
- }
- process_pending_message(handle);
+/**
+ * We got an ACK. Check if it matches the given transmit handle, and if
+ * so call the continuation.
+ *
+ * @param cls the 'struct AckContext'
+ * @param key peer identity
+ * @param value the 'struct GNUNET_DV_TransmitHandle'
+ * @return GNUNET_OK if the ACK did not match (continue to iterate)
+ */
+static int
+process_ack (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ struct AckContext *ctx = cls;
+ struct GNUNET_DV_TransmitHandle *th = value;
+
+ if (th->uid != ntohl (ctx->ack->uid))
+ return GNUNET_OK;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Matchedk ACK for message to peer %s\n",
+ GNUNET_h2s (key));
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (ctx->sh->send_callbacks,
+ key,
+ th));
+ th->cb (th->cb_cls,
+ (ntohs (ctx->ack->header.type) == GNUNET_MESSAGE_TYPE_DV_SEND_ACK)
+ ? GNUNET_OK
+ : GNUNET_SYSERR);
+ GNUNET_free (th);
+ return GNUNET_NO;
}
+
/**
* Handles a message sent from the DV service to us.
* Parse it out and give it to the plugin.
* @param cls the handle to the DV API
* @param msg the message that was received
*/
-void handle_message_receipt (void *cls,
- const struct GNUNET_MessageHeader * msg)
+static void
+handle_message_receipt (void *cls,
+ const struct GNUNET_MessageHeader *msg)
{
- struct GNUNET_DV_Handle *handle = cls;
- struct GNUNET_DV_MessageReceived *received_msg;
- struct GNUNET_DV_SendResultMessage *send_result_msg;
- size_t packed_msg_len;
- size_t sender_address_len;
- char *sender_address;
- char *packed_msg;
- char *packed_msg_start;
- GNUNET_HashCode uidhash;
- struct SendCallbackContext *send_ctx;
-
- if (msg == NULL)
+ struct GNUNET_DV_ServiceHandle *sh = cls;
+ const struct GNUNET_DV_ConnectMessage *cm;
+ const struct GNUNET_DV_DistanceUpdateMessage *dum;
+ const struct GNUNET_DV_DisconnectMessage *dm;
+ const struct GNUNET_DV_ReceivedMessage *rm;
+ const struct GNUNET_MessageHeader *payload;
+ const struct GNUNET_DV_AckMessage *ack;
+ struct AckContext ctx;
+
+ if (NULL == msg)
{
-#if DEBUG_DV_MESSAGES
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
-#endif
- return; /* Connection closed? */
+ /* Connection closed */
+ reconnect (sh);
+ return;
}
-
- GNUNET_assert((ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE) || (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT));
-
- switch (ntohs(msg->type))
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Received message of type %u from DV service\n",
+ (unsigned int) msg->type);
+ switch (ntohs (msg->type))
{
- case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_RECEIVE:
- if (ntohs(msg->size) < sizeof(struct GNUNET_DV_MessageReceived))
+ case GNUNET_MESSAGE_TYPE_DV_CONNECT:
+ if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ConnectMessage))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
return;
-
- received_msg = (struct GNUNET_DV_MessageReceived *)msg;
- packed_msg_len = ntohl(received_msg->msg_len);
- sender_address_len = ntohl(received_msg->sender_address_len);
-
- GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
- sender_address = GNUNET_malloc(sender_address_len);
- memcpy(sender_address, &received_msg[1], sender_address_len);
- packed_msg_start = (char *)&received_msg[1];
- packed_msg = GNUNET_malloc(packed_msg_len);
- memcpy(packed_msg, &packed_msg_start[sender_address_len], packed_msg_len);
-
-#if DEBUG_DV_MESSAGES
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: packed message type: %d or %d\n", ntohs(((struct GNUNET_MessageHeader *)packed_msg)->type), ((struct GNUNET_MessageHeader *)packed_msg)->type);
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: message sender reported as %s\n", GNUNET_i2s(&received_msg->sender));
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: distance is %u\n", ntohl(received_msg->distance));
-#endif
-
- handle->receive_handler(handle->receive_cls,
- &received_msg->sender,
- packed_msg,
- packed_msg_len,
- ntohl(received_msg->distance),
- sender_address,
- sender_address_len);
-
- GNUNET_free(sender_address);
+ }
+ cm = (const struct GNUNET_DV_ConnectMessage *) msg;
+ sh->connect_cb (sh->cls,
+ &cm->peer,
+ ntohl (cm->distance));
break;
- case GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND_RESULT:
- if (ntohs(msg->size) < sizeof(struct GNUNET_DV_SendResultMessage))
+ case GNUNET_MESSAGE_TYPE_DV_DISTANCE_CHANGED:
+ if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DistanceUpdateMessage))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
return;
-
- send_result_msg = (struct GNUNET_DV_SendResultMessage *)msg;
- hash_from_uid(ntohl(send_result_msg->uid), &uidhash);
- send_ctx = GNUNET_CONTAINER_multihashmap_get(handle->send_callbacks, &uidhash);
-
- if ((send_ctx != NULL) && (send_ctx->cont != NULL))
- {
- if (ntohl(send_result_msg->result) == 0)
- {
- send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
- }
- else
- {
- send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
- }
- }
- GNUNET_free_non_null(send_ctx);
+ }
+ dum = (const struct GNUNET_DV_DistanceUpdateMessage *) msg;
+ sh->distance_cb (sh->cls,
+ &dum->peer,
+ ntohl (dum->distance));
+ break;
+ case GNUNET_MESSAGE_TYPE_DV_DISCONNECT:
+ if (ntohs (msg->size) != sizeof (struct GNUNET_DV_DisconnectMessage))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
+ return;
+ }
+ dm = (const struct GNUNET_DV_DisconnectMessage *) msg;
+ sh->disconnect_cb (sh->cls,
+ &dm->peer);
+ break;
+ case GNUNET_MESSAGE_TYPE_DV_RECV:
+ if (ntohs (msg->size) < sizeof (struct GNUNET_DV_ReceivedMessage) + sizeof (struct GNUNET_MessageHeader))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
+ return;
+ }
+ rm = (const struct GNUNET_DV_ReceivedMessage *) msg;
+ payload = (const struct GNUNET_MessageHeader *) &rm[1];
+ if (ntohs (msg->size) != sizeof (struct GNUNET_DV_ReceivedMessage) + ntohs (payload->size))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
+ return;
+ }
+ sh->message_cb (sh->cls,
+ &rm->sender,
+ ntohl (rm->distance),
+ payload);
+ break;
+ case GNUNET_MESSAGE_TYPE_DV_SEND_ACK:
+ case GNUNET_MESSAGE_TYPE_DV_SEND_NACK:
+ if (ntohs (msg->size) != sizeof (struct GNUNET_DV_AckMessage))
+ {
+ GNUNET_break (0);
+ reconnect (sh);
+ return;
+ }
+ ack = (const struct GNUNET_DV_AckMessage *) msg;
+ ctx.ack = ack;
+ ctx.sh = sh;
+ GNUNET_CONTAINER_multihashmap_get_multiple (sh->send_callbacks,
+ &ack->target.hashPubKey,
+ &process_ack,
+ &ctx);
break;
default:
+ reconnect (sh);
break;
}
- GNUNET_CLIENT_receive (handle->client,
- &handle_message_receipt,
- handle, GNUNET_TIME_UNIT_FOREVER_REL);
+ GNUNET_CLIENT_receive (sh->client,
+ &handle_message_receipt, sh,
+ GNUNET_TIME_UNIT_FOREVER_REL);
}
+
/**
- * Send a message from the plugin to the DV service indicating that
- * a message should be sent via DV to some peer.
+ * Transmit the start message to the DV service.
*
- * @param dv_handle the handle to the DV api
- * @param target the final target of the message
- * @param msgbuf the msg(s) to send
- * @param msgbuf_size the size of msgbuf
- * @param priority priority to pass on to core when sending the message
- * @param timeout how long can this message be delayed (pass through to core)
- * @param addr the address of this peer (internally known to DV)
- * @param addrlen the length of the peer address
- * @param cont continuation to call once the message has been sent (or failed)
- * @param cont_cls closure for continuation
- *
- */
-int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
- const struct GNUNET_PeerIdentity *target,
- const char *msgbuf,
- size_t msgbuf_size,
- unsigned int priority,
- struct GNUNET_TIME_Relative timeout,
- const void *addr,
- size_t addrlen,
- GNUNET_TRANSPORT_TransmitContinuation
- cont, void *cont_cls)
+ * @param cls the 'struct GNUNET_DV_ServiceHandle'
+ * @param size number of bytes available in buf
+ * @param buf where to copy the message
+ * @return number of bytes written to buf
+ */
+static size_t
+transmit_start (void *cls,
+ size_t size,
+ void *buf)
{
- struct GNUNET_DV_SendMessage *msg;
- struct SendCallbackContext *send_ctx;
- char *end_of_message;
- GNUNET_HashCode uidhash;
- int msize;
-#if DEBUG_DV_MESSAGES
- dv_handle->uid_gen = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, UINT32_MAX);
-#else
- dv_handle->uid_gen++;
-#endif
-
- msize = sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size;
- msg = GNUNET_malloc(msize);
- msg->header.size = htons(msize);
- msg->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
- memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
- msg->priority = htonl(priority);
- msg->timeout = timeout;
- msg->addrlen = htonl(addrlen);
- msg->uid = htonl(dv_handle->uid_gen);
- memcpy(&msg[1], addr, addrlen);
- end_of_message = (char *)&msg[1];
- end_of_message = &end_of_message[addrlen];
- memcpy(end_of_message, msgbuf, msgbuf_size);
- add_pending(dv_handle, msg);
- send_ctx = GNUNET_malloc(sizeof(struct SendCallbackContext));
- send_ctx->cont = cont;
- send_ctx->cont_cls = cont_cls;
- memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
- hash_from_uid(dv_handle->uid_gen, &uidhash);
- GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
+ struct GNUNET_DV_ServiceHandle *sh = cls;
+ struct GNUNET_MessageHeader start_message;
- return GNUNET_OK;
+ sh->th = NULL;
+ if (NULL == buf)
+ {
+ GNUNET_break (0);
+ reconnect (sh);
+ return 0;
+ }
+ GNUNET_assert (size >= sizeof (start_message));
+ start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
+ start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
+ memcpy (buf, &start_message, sizeof (start_message));
+ GNUNET_CLIENT_receive (sh->client,
+ &handle_message_receipt, sh,
+ GNUNET_TIME_UNIT_FOREVER_REL);
+ start_transmit (sh);
+ return sizeof (start_message);
}
+
/**
- * Callback to transmit a start message to
- * the DV service, once we can send
+ * We got disconnected from the service and thus all of the
+ * pending send callbacks will never be confirmed. Clean up.
*
- * @param cls struct StartContext
- * @param size how much can we send
- * @param buf where to copy the message
- *
- * @return number of bytes copied to buf
+ * @param cls the 'struct GNUNET_DV_ServiceHandle'
+ * @param key a peer identity
+ * @param value a 'struct GNUNET_DV_TransmitHandle' to clean up
+ * @return GNUNET_OK (continue to iterate)
*/
-static size_t
-transmit_start (void *cls, size_t size, void *buf)
+static int
+cleanup_send_cb (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
{
- struct StartContext *start_context = cls;
- struct GNUNET_DV_Handle *handle = start_context->handle;
- size_t tsize;
-#if DEBUG_DV
- GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV API: sending start request to service\n");
-#endif
- if (buf == NULL)
- {
- GNUNET_free(start_context->message);
- GNUNET_free(start_context);
- GNUNET_DV_disconnect(handle);
- return 0;
- }
-
- tsize = ntohs(start_context->message->size);
- if (size >= tsize)
- {
- memcpy(buf, start_context->message, tsize);
- GNUNET_free(start_context->message);
- GNUNET_free(start_context);
- GNUNET_CLIENT_receive (handle->client,
- &handle_message_receipt,
- handle, GNUNET_TIME_UNIT_FOREVER_REL);
+ struct GNUNET_DV_ServiceHandle *sh = cls;
+ struct GNUNET_DV_TransmitHandle *th = value;
+
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
+ key,
+ th));
+ th->cb (th->cb_cls, GNUNET_SYSERR);
+ GNUNET_free (th);
+ return GNUNET_OK;
+}
- return tsize;
+/**
+ * Disconnect and then reconnect to the DV service.
+ *
+ * @param sh service handle
+ */
+static void
+reconnect (struct GNUNET_DV_ServiceHandle *sh)
+{
+ if (NULL != sh->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
+ sh->th = NULL;
}
-
- return 0;
+ if (NULL != sh->client)
+ {
+ GNUNET_CLIENT_disconnect (sh->client);
+ sh->client = NULL;
+ }
+ GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
+ &cleanup_send_cb,
+ sh);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Connecting to DV service\n");
+ sh->client = GNUNET_CLIENT_connect ("dv", sh->cfg);
+ if (NULL == sh->client)
+ {
+ GNUNET_break (0);
+ return;
+ }
+ sh->th = GNUNET_CLIENT_notify_transmit_ready (sh->client,
+ sizeof (struct GNUNET_MessageHeader),
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES,
+ &transmit_start,
+ sh);
}
+
/**
- * Connect to the DV service
- *
- * @param sched the scheduler to use
- * @param cfg the configuration to use
- * @param receive_handler method call when on receipt from the service
- * @param receive_handler_cls closure for receive_handler
+ * Connect to the DV service.
*
- * @return handle to the DV service
+ * @param cfg configuration
+ * @param cls closure for callbacks
+ * @param connect_cb function to call on connects
+ * @param distance_cb function to call if distances change
+ * @param disconnect_cb function to call on disconnects
+ * @param message_cb function to call if we receive messages
+ * @return handle to access the service
*/
-struct GNUNET_DV_Handle *
-GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
- const struct GNUNET_CONFIGURATION_Handle *cfg,
- GNUNET_DV_MessageReceivedHandler receive_handler,
- void *receive_handler_cls)
+struct GNUNET_DV_ServiceHandle *
+GNUNET_DV_service_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
+ void *cls,
+ GNUNET_DV_ConnectCallback connect_cb,
+ GNUNET_DV_DistanceChangedCallback distance_cb,
+ GNUNET_DV_DisconnectCallback disconnect_cb,
+ GNUNET_DV_MessageReceivedCallback message_cb)
{
- struct GNUNET_DV_Handle *handle;
- struct GNUNET_MessageHeader *start_message;
- struct StartContext *start_context;
- handle = GNUNET_malloc(sizeof(struct GNUNET_DV_Handle));
-
- handle->cfg = cfg;
- handle->sched = sched;
- handle->pending_list = NULL;
- handle->current = NULL;
- handle->th = NULL;
- handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
- handle->receive_handler = receive_handler;
- handle->receive_cls = receive_handler_cls;
-
- if (handle->client == NULL)
- {
- GNUNET_free(handle);
- return NULL;
- }
-
- start_message = GNUNET_malloc(sizeof(struct GNUNET_MessageHeader));
- start_message->size = htons(sizeof(struct GNUNET_MessageHeader));
- start_message->type = htons(GNUNET_MESSAGE_TYPE_DV_START);
-
- start_context = GNUNET_malloc(sizeof(struct StartContext));
- start_context->handle = handle;
- start_context->message = start_message;
- GNUNET_CLIENT_notify_transmit_ready (handle->client,
- sizeof(struct GNUNET_MessageHeader),
- GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
- GNUNET_YES,
- &transmit_start, start_context);
+ struct GNUNET_DV_ServiceHandle *sh;
+
+ sh = GNUNET_new (struct GNUNET_DV_ServiceHandle);
+ sh->cfg = cfg;
+ sh->cls = cls;
+ sh->connect_cb = connect_cb;
+ sh->distance_cb = distance_cb;
+ sh->disconnect_cb = disconnect_cb;
+ sh->message_cb = message_cb;
+ sh->send_callbacks = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_YES);
+ reconnect (sh);
+ return sh;
+}
- handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
- return handle;
+/**
+ * Disconnect from DV service.
+ *
+ * @param sh service handle
+ */
+void
+GNUNET_DV_service_disconnect (struct GNUNET_DV_ServiceHandle *sh)
+{
+ struct GNUNET_DV_TransmitHandle *pos;
+
+ if (NULL == sh)
+ return;
+ if (NULL != sh->th)
+ {
+ GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
+ sh->th = NULL;
+ }
+ while (NULL != (pos = sh->th_head))
+ {
+ GNUNET_CONTAINER_DLL_remove (sh->th_head,
+ sh->th_tail,
+ pos);
+ GNUNET_free (pos);
+ }
+ if (NULL != sh->client)
+ {
+ GNUNET_CLIENT_disconnect (sh->client);
+ sh->client = NULL;
+ }
+ GNUNET_CONTAINER_multihashmap_iterate (sh->send_callbacks,
+ &cleanup_send_cb,
+ sh);
+ GNUNET_CONTAINER_multihashmap_destroy (sh->send_callbacks);
+ GNUNET_free (sh);
}
+
/**
- * Disconnect from the DV service
+ * Send a message via DV service.
*
- * @param handle the current handle to the service to disconnect
+ * @param sh service handle
+ * @param target intended recpient
+ * @param msg message payload
+ * @param cb function to invoke when done
+ * @param cb_cls closure for 'cb'
+ * @return handle to cancel the operation
*/
-void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle)
+struct GNUNET_DV_TransmitHandle *
+GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
+ const struct GNUNET_PeerIdentity *target,
+ const struct GNUNET_MessageHeader *msg,
+ GNUNET_DV_MessageSentCallback cb,
+ void *cb_cls)
{
- struct PendingMessages *pos;
+ struct GNUNET_DV_TransmitHandle *th;
+ struct GNUNET_DV_SendMessage *sm;
- GNUNET_assert(handle != NULL);
+ if (ntohs (msg->size) + sizeof (struct GNUNET_DV_SendMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Asked to send %u bytes of type %u to %s\n",
+ (unsigned int) msg->size,
+ (unsigned int) msg->type,
+ GNUNET_i2s (target));
+
+ th = GNUNET_malloc (sizeof (struct GNUNET_DV_TransmitHandle) +
+ sizeof (struct GNUNET_DV_SendMessage) +
+ ntohs (msg->size));
+ th->sh = sh;
+ th->target = *target;
+ th->cb = cb;
+ th->cb_cls = cb_cls;
+ th->msg = (const struct GNUNET_MessageHeader *) &th[1];
+ sm = (struct GNUNET_DV_SendMessage *) &th[1];
+ sm->header.type = htons (GNUNET_MESSAGE_TYPE_DV_SEND);
+ sm->header.size = htons (sizeof (struct GNUNET_DV_SendMessage) +
+ ntohs (msg->size));
+ if (0 == sh->uid_gen)
+ sh->uid_gen = 1;
+ th->uid = sh->uid_gen;
+ sm->uid = htonl (sh->uid_gen++);
+ /* use memcpy here as 'target' may not be sufficiently aligned */
+ memcpy (&sm->target, target, sizeof (struct GNUNET_PeerIdentity));
+ memcpy (&sm[1], msg, ntohs (msg->size));
+ GNUNET_CONTAINER_DLL_insert (sh->th_head,
+ sh->th_tail,
+ th);
+ start_transmit (sh);
+ return th;
+}
- if (handle->th != NULL) /* We have a live transmit request in the Aether */
- {
- GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
- handle->th = NULL;
- }
- if (handle->current != NULL) /* We are trying to send something now, clean it up */
- GNUNET_free(handle->current);
- while (NULL != (pos = handle->pending_list)) /* Remove all pending sends from the list */
- {
- handle->pending_list = pos->next;
- GNUNET_free(pos);
- }
- if (handle->client != NULL) /* Finally, disconnect from the service */
- {
- GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
- handle->client = NULL;
- }
- GNUNET_free (handle);
+/**
+ * Abort send operation (naturally, the message may have
+ * already been transmitted; this only stops the 'cb'
+ * from being called again).
+ *
+ * @param th send operation to cancel
+ */
+void
+GNUNET_DV_send_cancel (struct GNUNET_DV_TransmitHandle *th)
+{
+ struct GNUNET_DV_ServiceHandle *sh = th->sh;
+ int ret;
+
+ ret = GNUNET_CONTAINER_multihashmap_remove (sh->send_callbacks,
+ &th->target.hashPubKey,
+ th);
+ if (GNUNET_YES != ret)
+ GNUNET_CONTAINER_DLL_remove (sh->th_head,
+ sh->th_tail,
+ th);
+ GNUNET_free (th);
}
+
/* end of dv_api.c */