continue to fix extract result
[oweals/gnunet.git] / src / dv / dv_api.c
index 4cd072eb7f223bf985bac1157cc6b401ac5c67ce..e3ba995c3bfce277420bd54f9fb519bbe46490eb 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009--2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009--2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -197,10 +197,19 @@ static void
 reconnect (struct GNUNET_DV_ServiceHandle *sh);
 
 
+/**
+ * Start sending messages from our queue to the service.
+ *
+ * @param sh service handle
+ */
+static void
+start_transmit (struct GNUNET_DV_ServiceHandle *sh);
+
+
 /**
  * Gives a message from our queue to the DV service.
  *
- * @param cls handle to the dv service (struct GNUNET_DV_ServiceHandle)
+ * @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 @a buf
@@ -228,19 +237,25 @@ transmit_pending (void *cls, size_t size, void *buf)
                                 sh->th_tail,
                                 th);
     memcpy (&cbuf[ret], th->msg, tsize);
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Passing %u bytes of type %u to DV service\n",
+         tsize,
+         ntohs (th->msg->type));
     th->msg = NULL;
     ret += tsize;
     if (NULL != th->cb)
     {
-      GNUNET_CONTAINER_DLL_insert (th->target->head,
-                                   th->target->tail,
-                                   th);
+      GNUNET_CONTAINER_DLL_insert_tail (th->target->head,
+                                        th->target->tail,
+                                        th);
     }
     else
     {
       GNUNET_free (th);
     }
   }
+  if (NULL != sh->th_head)
+    start_transmit (sh);
   return ret;
 }
 
@@ -422,6 +437,14 @@ handle_message_receipt (void *cls,
       reconnect (sh);
       return;
     }
+    if (NULL ==
+        GNUNET_CONTAINER_multipeermap_get (sh->peers,
+                                           &rm->sender))
+    {
+      GNUNET_break (0);
+      reconnect (sh);
+      return;
+    }
     sh->message_cb (sh->cls,
                    &rm->sender,
                    ntohl (rm->distance),
@@ -439,7 +462,7 @@ handle_message_receipt (void *cls,
     peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
                                               &ack->target);
     if (NULL == peer)
-      return; /* this happens, just ignore */
+      break; /* this happens, just ignore */
     for (th = peer->head; NULL != th; th = th->next)
     {
       if (th->uid != ntohl (ack->uid))
@@ -462,6 +485,9 @@ handle_message_receipt (void *cls,
     reconnect (sh);
     break;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Received message, continuing receive loop for %p\n",
+       sh->client);
   GNUNET_CLIENT_receive (sh->client,
                         &handle_message_receipt, sh,
                          GNUNET_TIME_UNIT_FOREVER_REL);
@@ -495,6 +521,9 @@ transmit_start (void *cls,
   start_message.size = htons (sizeof (struct GNUNET_MessageHeader));
   start_message.type = htons (GNUNET_MESSAGE_TYPE_DV_START);
   memcpy (buf, &start_message, sizeof (start_message));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Transmitting START request, starting receive loop for %p\n",
+       sh->client);
   GNUNET_CLIENT_receive (sh->client,
                         &handle_message_receipt, sh,
                          GNUNET_TIME_UNIT_FOREVER_REL);
@@ -516,6 +545,9 @@ reconnect (struct GNUNET_DV_ServiceHandle *sh)
     GNUNET_CLIENT_notify_transmit_ready_cancel (sh->th);
     sh->th = NULL;
   }
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Disconnecting from DV service at %p\n",
+       sh->client);
   if (NULL != sh->client)
   {
     GNUNET_CLIENT_disconnect (sh->client);
@@ -639,10 +671,11 @@ GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
     return NULL;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Asked to send %u bytes of type %u to %s\n",
+       "Asked to send %u bytes of type %u to %s via %p\n",
        (unsigned int) ntohs (msg->size),
        (unsigned int) ntohs (msg->type),
-       GNUNET_i2s (target));
+       GNUNET_i2s (target),
+       sh->client);
   peer = GNUNET_CONTAINER_multipeermap_get (sh->peers,
                                             target);
   if (NULL == peer)
@@ -669,9 +702,9 @@ GNUNET_DV_send (struct GNUNET_DV_ServiceHandle *sh,
   /* 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);
+  GNUNET_CONTAINER_DLL_insert_tail (sh->th_head,
+                                    sh->th_tail,
+                                    th);
   start_transmit (sh);
   return th;
 }