- fix 2699
[oweals/gnunet.git] / src / dv / dv_api.c
index 7a250b5a6bbe1a0d23764435e0fca1758204299c..d3cdb0fb7ae01c352f896658a95701b6e04ad66f 100644 (file)
@@ -38,6 +38,8 @@
 #include "dv.h"
 #include "gnunet_transport_plugin.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "dv-api",__VA_ARGS__)
+
 /**
  * Store ready to send messages
  */
@@ -143,6 +145,16 @@ struct SendCallbackContext
    * Target of the message.
    */
   struct GNUNET_PeerIdentity target;
+
+  /**
+   * Payload size in bytes
+   */
+  size_t payload_size;
+
+  /**
+   * DV message size
+   */
+  size_t msg_size;
 };
 
 /**
@@ -152,9 +164,9 @@ struct SendCallbackContext
  * @param hash set to uid (extended with zeros)
  */
 static void
-hash_from_uid (uint32_t uid, GNUNET_HashCode * hash)
+hash_from_uid (uint32_t uid, struct GNUNET_HashCode * hash)
 {
-  memset (hash, 0, sizeof (GNUNET_HashCode));
+  memset (hash, 0, sizeof (struct GNUNET_HashCode));
   *((uint32_t *) hash) = uid;
 }
 
@@ -174,13 +186,13 @@ try_connect (struct GNUNET_DV_Handle *ret)
   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"));
+  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);
+static void
+process_pending_message (struct GNUNET_DV_Handle *handle);
 
 /**
  * Send complete, schedule next
@@ -218,16 +230,15 @@ transmit_pending (void *cls, size_t size, void *buf)
 
 #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));
+    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");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "DV API: Transmit pending FAILED!\n\n\n");
 #endif
     finish (handle, GNUNET_SYSERR);
     return 0;
@@ -243,8 +254,8 @@ transmit_pending (void *cls, size_t size, void *buf)
     {
       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);
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "DV API: Copied %d bytes into buffer!\n\n\n", tsize);
 #endif
       finish (handle, GNUNET_OK);
       return tsize;
@@ -282,19 +293,17 @@ process_pending_message (struct GNUNET_DV_Handle *handle)
   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)))
+      (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");
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Failed to transmit request to dv service.\n");
 #endif
     finish (handle, GNUNET_SYSERR);
   }
@@ -352,13 +361,13 @@ handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
   char *sender_address;
   char *packed_msg;
   char *packed_msg_start;
-  GNUNET_HashCode uidhash;
+  struct GNUNET_HashCode uidhash;
   struct SendCallbackContext *send_ctx;
 
   if (msg == NULL)
   {
 #if DEBUG_DV_MESSAGES
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
 #endif
     return;                     /* Connection closed? */
   }
@@ -386,23 +395,21 @@ handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
     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));
+    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);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "DV_API receive: message sender reported as %s\n",
+         GNUNET_i2s (&received_msg->sender));
+    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);
+    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);
     break;
@@ -419,11 +426,13 @@ handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
     {
       if (ntohl (send_result_msg->result) == 0)
       {
-        send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
+        send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_OK,
+                        send_ctx->payload_size, send_ctx->msg_size);
       }
       else
       {
-        send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR);
+        send_ctx->cont (send_ctx->cont_cls, &send_ctx->target, GNUNET_SYSERR,
+                        send_ctx->payload_size, 0);
       }
     }
     GNUNET_free_non_null (send_ctx);
@@ -431,9 +440,8 @@ handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
   default:
     break;
   }
-  GNUNET_CLIENT_receive (handle->client,
-                         &handle_message_receipt,
-                         handle, GNUNET_TIME_UNIT_FOREVER_REL);
+  GNUNET_CLIENT_receive (handle->client, &handle_message_receipt, handle,
+                         GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 /**
@@ -454,19 +462,16 @@ handle_message_receipt (void *cls, const struct GNUNET_MessageHeader *msg)
  */
 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)
+                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)
 {
   struct GNUNET_DV_SendMessage *msg;
   struct SendCallbackContext *send_ctx;
   char *end_of_message;
-  GNUNET_HashCode uidhash;
+  struct GNUNET_HashCode uidhash;
   int msize;
 
 #if DEBUG_DV_MESSAGES
@@ -491,6 +496,8 @@ GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
   memcpy (end_of_message, msgbuf, msgbuf_size);
   add_pending (dv_handle, msg);
   send_ctx = GNUNET_malloc (sizeof (struct SendCallbackContext));
+  send_ctx->payload_size = msgbuf_size;
+  send_ctx->msg_size = msize;
   send_ctx->cont = cont;
   send_ctx->cont_cls = cont_cls;
   memcpy (&send_ctx->target, target, sizeof (struct GNUNET_PeerIdentity));
@@ -520,8 +527,7 @@ transmit_start (void *cls, size_t size, void *buf)
   size_t tsize;
 
 #if DEBUG_DV
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "DV API: sending start request to service\n");
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "DV API: sending start request to service\n");
 #endif
   if (buf == NULL)
   {
@@ -537,9 +543,8 @@ transmit_start (void *cls, size_t size, void *buf)
     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);
+    GNUNET_CLIENT_receive (handle->client, &handle_message_receipt, handle,
+                           GNUNET_TIME_UNIT_FOREVER_REL);
 
 
     return tsize;
@@ -596,7 +601,7 @@ GNUNET_DV_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                        GNUNET_YES, &transmit_start,
                                        start_context);
 
-  handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create (100);
+  handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
 
   return handle;
 }
@@ -627,7 +632,7 @@ GNUNET_DV_disconnect (struct GNUNET_DV_Handle *handle)
   }
   if (handle->client != NULL)   /* Finally, disconnect from the service */
   {
-    GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (handle->client);
     handle->client = NULL;
   }