server mst change
[oweals/gnunet.git] / src / dv / dv_api.c
index 28bd0dcac9a5875b3707e8e72e04a47b44ec1b50..e5713cdda697be6ed9a7e9743e91f8f0c408c007 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
 
      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
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -38,7 +38,9 @@
 #include "dv.h"
 #include "../transport/plugin_transport.h"
 
 #include "dv.h"
 #include "../transport/plugin_transport.h"
 
-
+/**
+ * Store ready to send messages
+ */
 struct PendingMessages
 {
   /**
 struct PendingMessages
 {
   /**
@@ -58,8 +60,6 @@ struct PendingMessages
 
 };
 
 
 };
 
-
-
 /**
  * Handle for the service.
  */
 /**
  * Handle for the service.
  */
@@ -95,11 +95,6 @@ struct GNUNET_DV_Handle
    */
   struct PendingMessages *current;
 
    */
   struct PendingMessages *current;
 
-  /**
-   * Kill off the connection and any pending messages.
-   */
-  int do_destroy;
-
   /**
    * Handler for messages we receive from the DV service
    */
   /**
    * Handler for messages we receive from the DV service
    */
@@ -171,6 +166,8 @@ hash_from_uid (uint32_t uid,
 /**
  * Try to (re)connect to the dv service.
  *
 /**
  * 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
  * @return GNUNET_YES on success, GNUNET_NO on failure.
  */
 static int
@@ -181,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;
   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
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               _("Failed to connect to the dv service!\n"));
 #endif
@@ -192,6 +189,9 @@ static void process_pending_message(struct GNUNET_DV_Handle *handle);
 
 /**
  * Send complete, schedule next
 
 /**
  * Send complete, schedule next
+ *
+ * @param handle handle to the dv service
+ * @param code return code for send (unused)
  */
 static void
 finish (struct GNUNET_DV_Handle *handle, int code)
  */
 static void
 finish (struct GNUNET_DV_Handle *handle, int code)
@@ -200,10 +200,19 @@ finish (struct GNUNET_DV_Handle *handle, int code)
   handle->current = NULL;
   process_pending_message (handle);
 
   handle->current = NULL;
   process_pending_message (handle);
 
+  GNUNET_free(pos->msg);
   GNUNET_free (pos);
 }
 
   GNUNET_free (pos);
 }
 
-
+/**
+ * Notification that we can send data
+ *
+ * @param cls handle to the dv service (struct GNUNET_DV_Handle)
+ * @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)
 {
 static size_t
 transmit_pending (void *cls, size_t size, void *buf)
 {
@@ -248,6 +257,8 @@ transmit_pending (void *cls, size_t size, void *buf)
 
 /**
  * Try to send messages from list of messages to send
 
 /**
  * Try to send messages from list of messages to send
+ *
+ * @param handle handle to the distance vector service
  */
 static void process_pending_message(struct GNUNET_DV_Handle *handle)
 {
  */
 static void process_pending_message(struct GNUNET_DV_Handle *handle)
 {
@@ -264,11 +275,6 @@ static void process_pending_message(struct GNUNET_DV_Handle *handle)
   handle->current = handle->pending_list;
   if (NULL == handle->current)
     {
   handle->current = handle->pending_list;
   if (NULL == handle->current)
     {
-      if (handle->do_destroy)
-        {
-          handle->do_destroy = GNUNET_NO;
-          //GNUNET_DV_disconnect (handle); /* FIXME: replace with proper disconnect stuffs */
-        }
       return;
     }
   handle->pending_list = handle->pending_list->next;
       return;
     }
   handle->pending_list = handle->pending_list->next;
@@ -276,10 +282,10 @@ static void process_pending_message(struct GNUNET_DV_Handle *handle)
 
   if (NULL ==
       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
 
   if (NULL ==
       (handle->th = GNUNET_CLIENT_notify_transmit_ready (handle->client,
-                                                    ntohl(handle->current->msg->msgbuf_size),
-                                                    handle->current->msg->timeout,
-                                                    GNUNET_YES,
-                                                    &transmit_pending, handle)))
+                                                         ntohs(handle->current->msg->header.size),
+                                                         handle->current->msg->timeout,
+                                                         GNUNET_YES,
+                                                         &transmit_pending, handle)))
     {
 #if DEBUG_DV
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
     {
 #if DEBUG_DV
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -312,12 +318,10 @@ static void add_pending(struct GNUNET_DV_Handle *handle, struct GNUNET_DV_SendMe
           last = pos;
           pos = pos->next;
         }
           last = pos;
           pos = pos->next;
         }
-      new_message->next = last->next; /* Should always be null */
       last->next = new_message;
     }
   else
     {
       last->next = new_message;
     }
   else
     {
-      new_message->next = handle->pending_list; /* Will always be null */
       handle->pending_list = new_message;
     }
 
       handle->pending_list = new_message;
     }
 
@@ -347,6 +351,9 @@ void handle_message_receipt (void *cls,
 
   if (msg == NULL)
   {
 
   if (msg == NULL)
   {
+#if DEBUG_DV_MESSAGES
+    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV_API receive: connection closed\n");
+#endif
     return; /* Connection closed? */
   }
 
     return; /* Connection closed? */
   }
 
@@ -363,16 +370,13 @@ void handle_message_receipt (void *cls,
     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 = ntohl(received_msg->sender_address_len);
 
     GNUNET_assert(ntohs(msg->size) == (sizeof(struct GNUNET_DV_MessageReceived) + packed_msg_len + sender_address_len));
-#if DEBUG_DV
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api receives message, size checks out!\n");
-#endif
     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);
 
     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));
     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));
@@ -395,13 +399,9 @@ void handle_message_receipt (void *cls,
     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);
     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);
-    GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "got uid of %u or %u, hash of %s !!!!\n", ntohl(send_result_msg->uid), send_result_msg->uid, GNUNET_h2s(&uidhash));
 
     if ((send_ctx != NULL) && (send_ctx->cont != NULL))
       {
 
     if ((send_ctx != NULL) && (send_ctx->cont != NULL))
       {
-#if DEBUG_DV
-        GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "dv api notifies transport of send result (%u)!\n", ntohl(send_result_msg->result));
-#endif
         if (ntohl(send_result_msg->result) == 0)
           {
             send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
         if (ntohl(send_result_msg->result) == 0)
           {
             send_ctx->cont(send_ctx->cont_cls, &send_ctx->target, GNUNET_OK);
@@ -452,15 +452,18 @@ int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
   struct SendCallbackContext *send_ctx;
   char *end_of_message;
   GNUNET_HashCode uidhash;
   struct SendCallbackContext *send_ctx;
   char *end_of_message;
   GNUNET_HashCode uidhash;
-#if DEBUG_DV
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "DV SEND called with message of size %d, address size %d, total size to send is %d\n", msgbuf_size, addrlen, sizeof(struct GNUNET_DV_SendMessage) + msgbuf_size + addrlen);
-#endif
+  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++;
   dv_handle->uid_gen++;
-  msg = GNUNET_malloc(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
-  msg->header.size = htons(sizeof(struct GNUNET_DV_SendMessage) + addrlen + msgbuf_size);
+#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->header.type = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_DV_SEND);
   memcpy(&msg->target, target, sizeof(struct GNUNET_PeerIdentity));
-  msg->msgbuf_size = htonl(msgbuf_size);
   msg->priority = htonl(priority);
   msg->timeout = timeout;
   msg->addrlen = htonl(addrlen);
   msg->priority = htonl(priority);
   msg->timeout = timeout;
   msg->addrlen = htonl(addrlen);
@@ -470,33 +473,35 @@ int GNUNET_DV_send (struct GNUNET_DV_Handle *dv_handle,
   end_of_message = &end_of_message[addrlen];
   memcpy(end_of_message, msgbuf, msgbuf_size);
   add_pending(dv_handle, msg);
   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 = GNUNET_malloc(sizeof(struct SendCallbackContext));
-
   send_ctx->cont = cont;
   send_ctx->cont = cont;
-  if (cont == NULL)
-    fprintf(stderr, "DV_SEND called with null continuation!\n");
   send_ctx->cont_cls = cont_cls;
   memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
   send_ctx->cont_cls = cont_cls;
   memcpy(&send_ctx->target, target, sizeof(struct GNUNET_PeerIdentity));
-
   hash_from_uid(dv_handle->uid_gen, &uidhash);
   hash_from_uid(dv_handle->uid_gen, &uidhash);
-
-  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "set uid of %u or %u, hash of %s !!!!\n", dv_handle->uid_gen, htonl(dv_handle->uid_gen), GNUNET_h2s(&uidhash));
   GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
 
   return GNUNET_OK;
 }
 
   GNUNET_CONTAINER_multihashmap_put(dv_handle->send_callbacks, &uidhash, send_ctx, GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
 
   return GNUNET_OK;
 }
 
-/* Forward declaration */
-void GNUNET_DV_disconnect(struct GNUNET_DV_Handle *handle);
-
+/**
+ * Callback to transmit a start message to
+ * the DV service, once we can send
+ *
+ * @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
+ */
 static size_t
 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;
 static size_t
 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);
   if (buf == NULL)
     {
       GNUNET_free(start_context->message);
@@ -509,6 +514,13 @@ transmit_start (void *cls, size_t size, void *buf)
   if (size >= tsize)
   {
     memcpy(buf, start_context->message, tsize);
   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);
+
+
     return tsize;
   }
 
     return tsize;
   }
 
@@ -540,7 +552,6 @@ GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
   handle->sched = sched;
   handle->pending_list = NULL;
   handle->current = NULL;
   handle->sched = sched;
   handle->pending_list = NULL;
   handle->current = NULL;
-  handle->do_destroy = GNUNET_NO;
   handle->th = NULL;
   handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
   handle->receive_handler = receive_handler;
   handle->th = NULL;
   handle->client = GNUNET_CLIENT_connect(sched, "dv", cfg);
   handle->receive_handler = receive_handler;
@@ -567,10 +578,6 @@ GNUNET_DV_connect (struct GNUNET_SCHEDULER_Handle *sched,
 
   handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
 
 
   handle->send_callbacks = GNUNET_CONTAINER_multihashmap_create(100);
 
-  GNUNET_CLIENT_receive (handle->client,
-                         &handle_message_receipt,
-                         handle, GNUNET_TIME_UNIT_FOREVER_REL);
-
   return handle;
 }
 
   return handle;
 }