Smallers steps to keep plugin running
[oweals/gnunet.git] / src / dv / dv_api.c
index 8b92a703f47f35975cf376b8a00afda1e6bdc92f..d9aef03c86f85b00cc62c7f789f1e4ddb15ee4f2 100644 (file)
@@ -4,7 +4,7 @@
 
      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
@@ -178,7 +178,7 @@ try_connect (struct GNUNET_DV_Handle *ret)
   ret->client = GNUNET_CLIENT_connect (ret->sched, "dv", ret->cfg);
   if (ret->client != NULL)
     return GNUNET_YES;
-#if DEBUG_STATISTICS
+#if DEBUG_DV_MESSAGES
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               _("Failed to connect to the dv service!\n"));
 #endif
@@ -209,7 +209,7 @@ finish (struct GNUNET_DV_Handle *handle, int code)
  *
  * @param cls handle to the dv service (struct GNUNET_DV_Handle)
  * @param size how many bytes can we send
- * @buf where to copy the message to send
+ * @param buf where to copy the message to send
  *
  * @return how many bytes we copied to buf
  */
@@ -351,6 +351,9 @@ void handle_message_receipt (void *cls,
 
   if (msg == NULL)
   {
+#if DEBUG_DV_MESSAGES
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
+#endif
     return; /* Connection closed? */
   }
 
@@ -364,16 +367,15 @@ void handle_message_receipt (void *cls,
 
     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_len = ntohs(msg->size) - packed_msg_len - sizeof(struct GNUNET_DV_MessageReceived);
+    GNUNET_assert(sender_address_len > 0);
     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
+#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));
@@ -496,7 +498,9 @@ transmit_start (void *cls, size_t size, void *buf)
   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);
@@ -511,6 +515,11 @@ 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);
+
+
     return tsize;
   }
 
@@ -568,10 +577,6 @@ GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
 
   handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
 
-  GNUNET_CLIENT_receive (handle->client,
-                         &handle_message_receipt,
-                         handle, GNUNET_TIME_UNIT_FOREVER_REL);
-
   return handle;
 }