i am a dumb dummy
[oweals/gnunet.git] / src / core / core_api.c
index 9500a1316957de50acceddd513b0007cc9a72a0d..21622852b441633e1d002b7ca34451d80a0e80a0 100644 (file)
@@ -23,9 +23,6 @@
  * @brief core service; this is the main API for encrypted P2P
  *        communications
  * @author Christian Grothoff
- *
- * TODO:
- * - implement atsi parsing and passing
  */
 #include "platform.h"
 #include "gnunet_constants.h"
@@ -133,7 +130,8 @@ struct ControlMessage
 
   /**
    * Function to run after successful transmission (or call with
-   * reason 'TIMEOUT' on error).
+   * reason 'TIMEOUT' on error); called with scheduler context 'NULL'
+   * on disconnect.
    */
   GNUNET_SCHEDULER_Task cont;
 
@@ -142,6 +140,10 @@ struct ControlMessage
    */
   void *cont_cls;
 
+  /**
+   * Transmit handle (if one is associated with this ControlMessage), or NULL.
+   */
+  struct GNUNET_CORE_TransmitHandle *th;
 };
 
 
@@ -235,11 +237,21 @@ struct GNUNET_CORE_Handle
    */
   struct GNUNET_CONTAINER_MultiHashMap *peers;
 
+  /**
+   * Identity of this peer.
+   */
+  struct GNUNET_PeerIdentity me;
+
   /**
    * ID of reconnect task (if any).
    */
   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
 
+  /**
+   * Current delay we use for re-trying to connect to core.
+   */
+  struct GNUNET_TIME_Relative retry_backoff;
+
   /**
    * Request information ID generator.
    */
@@ -362,18 +374,141 @@ reconnect_task (void *cls,
   struct GNUNET_CORE_Handle *h = cls;
 
   h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Connecting to CORE service after delay\n");
+#endif
   reconnect (h);
 }
 
 
+/**
+ * Notify clients about disconnect and free 
+ * the entry for connected peer.
+ *
+ * @param cls the 'struct GNUNET_CORE_Handle*'
+ * @param key the peer identity (not used)
+ * @param value the 'struct PeerRecord' to free.
+ * @return GNUNET_YES (continue)
+ */
+static int
+disconnect_and_free_peer_entry (void *cls,
+                               const GNUNET_HashCode *key,
+                               void *value)
+{
+  static struct GNUNET_BANDWIDTH_Value32NBO zero;
+  struct GNUNET_CORE_Handle *h = cls;
+  struct GNUNET_CORE_TransmitHandle *th;
+  struct PeerRecord *pr = value;
+  GNUNET_CORE_PeerConfigurationInfoCallback pcic;
+
+  while (NULL != (th = pr->pending_head))
+    {
+      GNUNET_CONTAINER_DLL_remove (pr->pending_head,
+                                  pr->pending_tail,
+                                  th);
+      pr->queue_size--;
+      GNUNET_assert (0 == 
+                    th->get_message (th->get_message_cls,
+                                     0, NULL));
+      GNUNET_free (th);
+    }
+  if (NULL != (pcic = pr->pcic))
+    {
+      pr->pcic = NULL;
+      pcic (pr->pcic_cls,
+           &pr->peer,
+           zero,
+           0, 0);
+    }
+  if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (pr->timeout_task);
+      pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    }
+  GNUNET_assert (pr->queue_size == 0);
+  if ( (pr->prev != NULL) ||
+       (pr->next != NULL) ||
+       (h->ready_peer_head == pr) )
+    GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
+                                h->ready_peer_tail,
+                                pr);
+  if (h->disconnects != NULL)
+    h->disconnects (h->cls,
+                   &pr->peer);    
+  GNUNET_assert (GNUNET_YES ==
+                GNUNET_CONTAINER_multihashmap_remove (h->peers,
+                                                      key,
+                                                      pr));
+  GNUNET_assert (pr->pending_head == NULL);
+  GNUNET_assert (pr->pending_tail == NULL);
+  GNUNET_assert (pr->ch = h);
+  GNUNET_assert (pr->queue_size == 0);
+  GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
+  GNUNET_free (pr);  
+  return GNUNET_YES;
+}
+
+
+/**
+ * Close down any existing connection to the CORE service and
+ * try re-establishing it later.
+ *
+ * @param h our handle
+ */
+static void
+reconnect_later (struct GNUNET_CORE_Handle *h)
+{
+  struct ControlMessage *cm;
+  struct PeerRecord *pr;
+
+  while (NULL != (cm = h->pending_head))
+    {
+      GNUNET_CONTAINER_DLL_remove (h->pending_head,
+                                  h->pending_tail,
+                                  cm);
+      if (cm->th != NULL)
+       cm->th->cm = NULL; 
+      if (cm->cont != NULL)
+       cm->cont (cm->cont_cls, NULL);
+      GNUNET_free (cm);
+    }
+  if (h->client != NULL)
+    {
+      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+      h->client = NULL;
+      h->cth = NULL;
+      GNUNET_CONTAINER_multihashmap_iterate (h->peers,
+                                            &disconnect_and_free_peer_entry,
+                                            h);
+    }
+  while (NULL != (pr = h->ready_peer_head))    
+    GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
+                                h->ready_peer_tail,
+                                pr);
+  
+  GNUNET_assert (h->pending_head == NULL);
+  h->currently_down = GNUNET_YES;
+  GNUNET_assert (h->reconnect_task == GNUNET_SCHEDULER_NO_TASK);
+  h->retry_backoff = GNUNET_TIME_relative_min (GNUNET_TIME_UNIT_SECONDS,
+                                              h->retry_backoff);
+  h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_backoff,
+                                                   &reconnect_task,
+                                                   h);
+  h->retry_backoff = GNUNET_TIME_relative_multiply (h->retry_backoff, 2);
+}
+
+
 /**
  * Check the list of pending requests, send the next
  * one to the core.
  *
  * @param h core handle
+ * @param ignore_currently_down transmit message even if not initialized?
  */
 static void
-trigger_next_request (struct GNUNET_CORE_Handle *h);
+trigger_next_request (struct GNUNET_CORE_Handle *h,
+                     int ignore_currently_down);
 
 
 /**
@@ -388,22 +523,6 @@ transmission_timeout (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
-/**
- * Control message was sent, mark it as such.
- *
- * @param cls the 'struct GNUNET_CORE_TransmitHandle*'
- * @param tc scheduler context
- */
-static void
-mark_control_message_sent (void *cls,
-                          const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  struct GNUNET_CORE_TransmitHandle *th = cls;
-
-  th->cm = NULL;
-}
-
-
 /**
  * Send a control message to the peer asking for transmission
  * of the message in the given peer record.
@@ -425,7 +544,7 @@ request_next_transmission (struct PeerRecord *pr)
     }
   if (NULL == (th = pr->pending_head))
     {
-      trigger_next_request (h);
+      trigger_next_request (h, GNUNET_NO);
       return;
     }
   GNUNET_assert (pr->prev == NULL);
@@ -435,9 +554,8 @@ request_next_transmission (struct PeerRecord *pr)
                                                   pr);
   cm = GNUNET_malloc (sizeof (struct ControlMessage) + 
                      sizeof (struct SendMessageRequest));
-  cm->cont = &mark_control_message_sent;
-  cm->cont_cls = th;
   th->cm = cm;
+  cm->th = th;
   smr = (struct SendMessageRequest*) &cm[1];
   smr->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND_REQUEST);
   smr->header.size = htons (sizeof (struct SendMessageRequest));
@@ -451,7 +569,12 @@ request_next_transmission (struct PeerRecord *pr)
                                     h->pending_tail,
                                     h->pending_tail,
                                     cm);
-  trigger_next_request (h);
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Adding SEND REQUEST for peer `%s' to message queue\n",
+             GNUNET_i2s (&pr->peer));
+#endif
+  trigger_next_request (h, GNUNET_NO);
 }
 
 
@@ -467,14 +590,26 @@ transmission_timeout (void *cls,
                      const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct PeerRecord *pr = cls;
+  struct GNUNET_CORE_Handle *h = pr->ch;
   struct GNUNET_CORE_TransmitHandle *th;
-
+  
   pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   th = pr->pending_head;
   GNUNET_CONTAINER_DLL_remove (pr->pending_head,
                                pr->pending_tail,
                                th);
   pr->queue_size--;
+  if ( (pr->prev != NULL) ||
+       (pr->next != NULL) ||
+       (pr == h->ready_peer_head) )
+    {
+      /* the request that was 'approved' by core was
+        canceled before it could be transmitted; remove
+        us from the 'ready' list */
+      GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
+                                  h->ready_peer_tail,
+                                  pr);
+    }
 #if DEBUG_CORE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Signalling timeout of request for transmission to CORE service\n");
@@ -504,7 +639,11 @@ transmit_message (void *cls,
   h->cth = NULL;
   if (buf == NULL)
     {
-      reconnect (h);
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Transmission failed, initiating reconnect\n");
+#endif
+      reconnect_later (h);
       return 0;
     }
   /* first check for control messages */
@@ -514,26 +653,37 @@ transmit_message (void *cls,
       msize = ntohs (hdr->size);
       if (size < msize)
        {
-         trigger_next_request (h);
+         trigger_next_request (h, GNUNET_NO);
          return 0;
        }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Transmitting control message with %u bytes of type %u to core.\n",
+                 (unsigned int) msize,
+                 (unsigned int) ntohs (hdr->type));
+#endif
       memcpy (buf, hdr, msize);
       GNUNET_CONTAINER_DLL_remove (h->pending_head,
                                   h->pending_tail,
-                                  cm);
+                                  cm);     
+      if (cm->th != NULL)
+       cm->th->cm = NULL;
       if (NULL != cm->cont)
-       GNUNET_SCHEDULER_add_now (cm->cont, cm->cont_cls);
+       GNUNET_SCHEDULER_add_continuation (cm->cont, 
+                                          cm->cont_cls,
+                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       GNUNET_free (cm);
-      trigger_next_request (h);
+      trigger_next_request (h, GNUNET_NO);
       return msize;
     }
   /* now check for 'ready' P2P messages */
   if (NULL != (pr = h->ready_peer_head))
     {
+      GNUNET_assert (pr->pending_head != NULL);
       th = pr->pending_head;
       if (size < th->msize + sizeof (struct SendMessage))
        {
-         trigger_next_request (h);
+         trigger_next_request (h, GNUNET_NO);
          return 0;
        }
       GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
@@ -548,7 +698,12 @@ transmit_message (void *cls,
          GNUNET_SCHEDULER_cancel (pr->timeout_task);
          pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
        }
-
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Transmitting SEND request to `%s' with %u bytes.\n",
+                 GNUNET_i2s (&pr->peer),
+                 (unsigned int) th->msize);
+#endif
       sm = (struct SendMessage *) buf;
       sm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_SEND);
       sm->priority = htonl (th->priority);
@@ -597,22 +752,43 @@ transmit_message (void *cls,
  * one to the core.
  *
  * @param h core handle
+ * @param ignore_currently_down transmit message even if not initialized?
  */
 static void
-trigger_next_request (struct GNUNET_CORE_Handle *h)
+trigger_next_request (struct GNUNET_CORE_Handle *h,
+                     int ignore_currently_down)
 {
   uint16_t msize;
 
-  if (GNUNET_YES == h->currently_down)
-    return;
+  if ( (GNUNET_YES == h->currently_down) &&
+       (ignore_currently_down == GNUNET_NO) )
+    {
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Core connection down, not processing queue\n");
+#endif
+      return;
+    }
   if (NULL != h->cth)
-    return;
+    {
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Request pending, not processing queue\n");
+#endif
+      return;
+    }
   if (h->pending_head != NULL)
     msize = ntohs (((struct GNUNET_MessageHeader*) &h->pending_head[1])->size);    
-  else if (h->ready_peer_head != NULL)
+  else if (h->ready_peer_head != NULL) 
     msize = h->ready_peer_head->pending_head->msize + sizeof (struct SendMessage);    
   else
-    return; /* no pending message */
+    {
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Request queue empty, not processing queue\n");
+#endif
+      return; /* no pending message */
+    }
   h->cth = GNUNET_CLIENT_notify_transmit_ready (h->client,
                                                msize,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
@@ -621,70 +797,6 @@ trigger_next_request (struct GNUNET_CORE_Handle *h)
 }
 
 
-
-
-/**
- * Notify clients about disconnect and free 
- * the entry for connected peer.
- *
- * @param cls the 'struct GNUNET_CORE_Handle*'
- * @param key the peer identity (not used)
- * @param value the 'struct PeerRecord' to free.
- * @return GNUNET_YES (continue)
- */
-static int
-disconnect_and_free_peer_entry (void *cls,
-                               const GNUNET_HashCode *key,
-                               void *value)
-{
-  struct GNUNET_CORE_Handle *h = cls;
-  struct GNUNET_CORE_TransmitHandle *th;
-  struct PeerRecord *pr = value;
-
-  while (NULL != (th = pr->pending_head))
-    {
-      GNUNET_CONTAINER_DLL_remove (pr->pending_head,
-                                  pr->pending_tail,
-                                  th);
-      pr->queue_size--;
-      GNUNET_assert (0 == 
-                    th->get_message (th->get_message_cls,
-                                     0, NULL));
-      GNUNET_free (th);
-    }
-  if (pr->pcic != NULL)
-    {
-      // FIXME: call pcic callback!
-    }
-  if (pr->timeout_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_cancel (pr->timeout_task);
-      pr->timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    }
-  GNUNET_assert (pr->queue_size == 0);
-  if ( (pr->prev != NULL) ||
-       (pr->next != NULL) ||
-       (h->ready_peer_head == pr) )
-    GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
-                                h->ready_peer_tail,
-                                pr);
-  if (h->disconnects != NULL)
-    h->disconnects (h->cls,
-                   &pr->peer);    
-  GNUNET_assert (GNUNET_YES ==
-                GNUNET_CONTAINER_multihashmap_remove (h->peers,
-                                                      key,
-                                                      pr));
-  GNUNET_assert (pr->pending_head == NULL);
-  GNUNET_assert (pr->pending_tail == NULL);
-  GNUNET_assert (pr->ch = h);
-  GNUNET_assert (pr->queue_size == 0);
-  GNUNET_assert (pr->timeout_task == GNUNET_SCHEDULER_NO_TASK);
-  GNUNET_free (pr);  
-  return GNUNET_YES;
-}
-
-
 /**
  * Handler for notification messages received from the core.
  *
@@ -707,20 +819,20 @@ main_notify_handler (void *cls,
   const struct GNUNET_CORE_MessageHandler *mh;
   GNUNET_CORE_StartupCallback init;
   GNUNET_CORE_PeerConfigurationInfoCallback pcic;
-  struct GNUNET_PeerIdentity my_identity;
   struct PeerRecord *pr;
   struct GNUNET_CORE_TransmitHandle *th;
   unsigned int hpos;
   int trigger;
   uint16_t msize;
   uint16_t et;
+  uint32_t ats_count;
 
   if (msg == NULL)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                   _
                   ("Client was disconnected from core service, trying to reconnect.\n"));
-      reconnect (h);
+      reconnect_later (h);
       return;
     }
   msize = ntohs (msg->size);
@@ -735,21 +847,18 @@ main_notify_handler (void *cls,
       if (ntohs (msg->size) != sizeof (struct InitReplyMessage))
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       m = (const struct InitReplyMessage *) msg;
       GNUNET_break (0 == ntohl (m->reserved));
       /* start our message processing loop */
-#if DEBUG_CORE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Successfully connected to core service, starting processing loop.\n");
-#endif
       if (GNUNET_YES == h->currently_down)
        {
          h->currently_down = GNUNET_NO;
-         trigger_next_request (h);
+         trigger_next_request (h, GNUNET_NO);
        }
+      h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
       if (NULL != (init = h->init))
        {
          /* mark so we don't call init on reconnect */
@@ -757,23 +866,57 @@ main_notify_handler (void *cls,
          GNUNET_CRYPTO_hash (&m->publicKey,
                              sizeof (struct
                                      GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
-                             &my_identity.hashPubKey);
-         init (h->cls, h, &my_identity, &m->publicKey);
+                             &h->me.hashPubKey);
+#if DEBUG_CORE
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Connected to core service of peer `%s'.\n",
+                     GNUNET_i2s (&h->me));
+#endif
+         init (h->cls, h, &h->me, &m->publicKey);
+       }
+      else
+       {
+#if DEBUG_CORE
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Successfully reconnected to core service.\n");
+#endif
        }
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_CONNECT:
-      if (msize != sizeof (struct ConnectNotifyMessage))
+      if (msize < sizeof (struct ConnectNotifyMessage))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       cnm = (const struct ConnectNotifyMessage *) msg;
+      ats_count = ntohl (cnm->ats_count);
+      if ( (msize != sizeof (struct ConnectNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
+          (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&cnm->ats)[ats_count].type)) )
+        {
+          GNUNET_break (0);
+         reconnect_later (h);
+         return;
+        }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about connection from `%s'.\n",
+                 GNUNET_i2s (&cnm->peer));
+#endif
+      if (0 == memcmp (&h->me,
+                      &cnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* disconnect from self!? */
+         GNUNET_break (0);
+         return;
+       }
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &cnm->peer.hashPubKey);
       if (pr != NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       pr = GNUNET_malloc (sizeof (struct PeerRecord));
@@ -787,21 +930,35 @@ main_notify_handler (void *cls,
       if (NULL != h->connects)
        h->connects (h->cls,
                     &cnm->peer,
-                    NULL /* FIXME: atsi! */);
+                    &cnm->ats);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_DISCONNECT:
       if (msize != sizeof (struct DisconnectNotifyMessage))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       dnm = (const struct DisconnectNotifyMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &dnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* connection to self!? */
+         GNUNET_break (0);
+         return;
+       }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about disconnect from `%s'.\n",
+                 GNUNET_i2s (&dnm->peer));
+#endif
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &dnm->peer.hashPubKey);
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       trigger = ( (pr->prev != NULL) ||
@@ -809,7 +966,7 @@ main_notify_handler (void *cls,
                  (h->ready_peer_head == pr) );
       disconnect_and_free_peer_entry (h, &dnm->peer.hashPubKey, pr);
       if (trigger)
-       trigger_next_request (h);
+       trigger_next_request (h, GNUNET_NO);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_STATUS_CHANGE:
       if (NULL == h->status_events)
@@ -817,18 +974,40 @@ main_notify_handler (void *cls,
           GNUNET_break (0);
           break;
         }
-      if (msize != sizeof (struct PeerStatusNotifyMessage))
+      if (msize < sizeof (struct PeerStatusNotifyMessage))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       psnm = (const struct PeerStatusNotifyMessage *) msg;
+      if (0 == memcmp (&h->me,
+                      &psnm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
+      ats_count = ntohl (psnm->ats_count);
+      if ( (msize != sizeof (struct PeerStatusNotifyMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) ||
+          (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&psnm->ats)[ats_count].type)) )
+        {
+          GNUNET_break (0);
+         reconnect_later (h);
+         return;
+        }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about status change by `%s'.\n",
+                 GNUNET_i2s (&psnm->peer));
+#endif
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &psnm->peer.hashPubKey);
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       h->status_events (h->cls,
@@ -836,18 +1015,34 @@ main_notify_handler (void *cls,
                        psnm->bandwidth_in,
                        psnm->bandwidth_out,
                        GNUNET_TIME_absolute_ntoh (psnm->timeout),
-                       NULL /* FIXME: atsi */);
+                       &psnm->ats);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_INBOUND:
-      if (msize <
-          sizeof (struct NotifyTrafficMessage) +
-          sizeof (struct GNUNET_MessageHeader))
+      if (msize < sizeof (struct NotifyTrafficMessage))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       ntm = (const struct NotifyTrafficMessage *) msg;
-      em = (const struct GNUNET_MessageHeader *) &ntm[1];
+      if (0 == memcmp (&h->me,
+                      &ntm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
+      ats_count = ntohl (ntm->ats_count);
+      if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
+           + sizeof (struct GNUNET_MessageHeader)) ||
+          (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
+        {
+          GNUNET_break (0);
+         reconnect_later (h);
+         return;
+        }
+      em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
 #if DEBUG_CORE
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Received message of type %u and size %u from peer `%4s'\n",
@@ -860,14 +1055,16 @@ main_notify_handler (void *cls,
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       if ((GNUNET_NO == h->inbound_hdr_only) &&
-          (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
+          (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) + 
+          + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       et = ntohs (em->type);
       for (hpos = 0; hpos < h->hcnt; hpos++)
@@ -883,7 +1080,7 @@ main_notify_handler (void *cls,
             }
           if (GNUNET_OK !=
               h->handlers[hpos].callback (h->cls, &ntm->peer, em,
-                                         NULL /* FIXME: atsi */))
+                                         &ntm->ats))
             {
               /* error in processing, do not process other messages! */
               break;
@@ -891,31 +1088,54 @@ main_notify_handler (void *cls,
         }
       if (NULL != h->inbound_notify)
         h->inbound_notify (h->cls, &ntm->peer, em,
-                          NULL /* FIXME: atsi */);
+                          &ntm->ats);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_NOTIFY_OUTBOUND:
-      if (msize <
-          sizeof (struct NotifyTrafficMessage) +
-          sizeof (struct GNUNET_MessageHeader))
+      if (msize < sizeof (struct NotifyTrafficMessage))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       ntm = (const struct NotifyTrafficMessage *) msg;
-      em = (const struct GNUNET_MessageHeader *) &ntm[1];
+      if (0 == memcmp (&h->me,
+                      &ntm->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
+      ats_count = ntohl (ntm->ats_count);
+      if ( (msize < sizeof (struct NotifyTrafficMessage) + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)
+           + sizeof (struct GNUNET_MessageHeader)) ||
+          (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR != ntohl ((&ntm->ats)[ats_count].type)) )
+        {
+          GNUNET_break (0);
+         reconnect_later (h);
+         return;
+        }
+      em = (const struct GNUNET_MessageHeader *) &(&ntm->ats)[ats_count+1];
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &ntm->peer.hashPubKey);
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about transmission to `%s'.\n",
+                 GNUNET_i2s (&ntm->peer));
+#endif
       if ((GNUNET_NO == h->outbound_hdr_only) &&
-          (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage)))
+          (msize != ntohs (em->size) + sizeof (struct NotifyTrafficMessage) + 
+          + ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information)) )
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       if (NULL == h->outbound_notify)
         {
@@ -923,23 +1143,44 @@ main_notify_handler (void *cls,
           break;
         }
       h->outbound_notify (h->cls, &ntm->peer, em,
-                         NULL /* FIXME: atsi? */);
+                         &ntm->ats);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_SEND_READY:
       if (msize != sizeof (struct SendMessageReady))
         {
           GNUNET_break (0);
-          break;
+         reconnect_later (h);
+         return;
         }
       smr = (const struct SendMessageReady *) msg;
+      if (0 == memcmp (&h->me,
+                      &smr->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &smr->peer.hashPubKey);
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about transmission readiness to `%s'.\n",
+                 GNUNET_i2s (&smr->peer));
+#endif
+      if (pr->pending_head == NULL)
+        {
+         /* request must have been cancelled between the original request
+            and the response from core, ignore core's readiness */
+          return;
+        }
+
       th = pr->pending_head;
       if (ntohs (smr->smr_id) != th->smr_id)
        {
@@ -953,27 +1194,41 @@ main_notify_handler (void *cls,
        {
          /* we should not already be on the ready list... */
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       GNUNET_CONTAINER_DLL_insert (h->ready_peer_head,
                                   h->ready_peer_tail,
                                   pr);
-      trigger_next_request (h);
+      trigger_next_request (h, GNUNET_NO);
       break;
     case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO:
       if (ntohs (msg->size) != sizeof (struct ConfigurationInfoMessage))
        {
          GNUNET_break (0);
-         break;
+         reconnect_later (h);
+         return;
        }
       cim = (const struct ConfigurationInfoMessage*) msg;
+      if (0 == memcmp (&h->me,
+                      &cim->peer,
+                      sizeof (struct GNUNET_PeerIdentity)))
+       {
+         /* self-change!? */
+         GNUNET_break (0);
+         return;
+       }
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Received notification about configuration update for `%s'.\n",
+                 GNUNET_i2s (&cim->peer));
+#endif
       pr = GNUNET_CONTAINER_multihashmap_get (h->peers,
                                              &cim->peer.hashPubKey);
       if (pr == NULL)
        {
          GNUNET_break (0);
-         reconnect (h);
+         reconnect_later (h);
          return;
        }
       if (pr->rim_id != ntohl (cim->rim_id))
@@ -988,11 +1243,12 @@ main_notify_handler (void *cls,
              GNUNET_ntohll (cim->preference));
       break;
     default:
-      GNUNET_break (0);
-      break;
+      reconnect_later (h);
+      return;
     }
   GNUNET_CLIENT_receive (h->client,
-                         &main_notify_handler, h, GNUNET_TIME_UNIT_FOREVER_REL);
+                        &main_notify_handler, h, 
+                        GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
 
@@ -1009,16 +1265,15 @@ init_done_task (void *cls,
 {
   struct GNUNET_CORE_Handle *h = cls;
 
+  if (tc == NULL)
+    return; /* error */
   if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
     {
-      if (h->client != NULL)
-       {
-         GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
-         h->client = NULL;
-       }
-      h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                                       &reconnect_task,
-                                                       h);
+#if DEBUG_CORE
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Failed to exchange INIT with core, retrying\n");
+#endif
+      reconnect_later (h);
       return;
     }
   GNUNET_CLIENT_receive (h->client,
@@ -1048,21 +1303,12 @@ reconnect (struct GNUNET_CORE_Handle *h)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Reconnecting to CORE service\n");
 #endif
-  if (h->client != NULL)
-    {
-      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
-      h->client = NULL;
-      GNUNET_CONTAINER_multihashmap_iterate (h->peers,
-                                            &disconnect_and_free_peer_entry,
-                                            h);
-    }
-  h->currently_down = GNUNET_YES;
+  GNUNET_assert (h->client == NULL);
+  GNUNET_assert (h->currently_down == GNUNET_YES);
   h->client = GNUNET_CLIENT_connect ("core", h->cfg);
   if (h->client == NULL)
     {
-      h->reconnect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                                       &reconnect_task,
-                                                       h);
+      reconnect_later (h);
       return;
     }
   msize = h->hcnt * sizeof (uint16_t) + sizeof (struct InitMessage);
@@ -1097,7 +1343,7 @@ reconnect (struct GNUNET_CORE_Handle *h)
   GNUNET_CONTAINER_DLL_insert (h->pending_head,
                               h->pending_tail,
                               cm);
-  trigger_next_request (h);
+  trigger_next_request (h, GNUNET_YES);
 }
 
 
@@ -1156,11 +1402,18 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
   h->outbound_hdr_only = outbound_hdr_only;
   h->handlers = handlers;
   h->hcnt = 0;
+  h->currently_down = GNUNET_YES;
+  h->peers = GNUNET_CONTAINER_multihashmap_create (128);
+  h->retry_backoff = GNUNET_TIME_UNIT_MILLISECONDS;
   while (handlers[h->hcnt].callback != NULL)
     h->hcnt++;
   GNUNET_assert (h->hcnt <
                  (GNUNET_SERVER_MAX_MESSAGE_SIZE -
                   sizeof (struct InitMessage)) / sizeof (uint16_t));
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Connecting to CORE service\n");
+#endif
   reconnect (h);
   return h;
 }
@@ -1169,7 +1422,7 @@ GNUNET_CORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
 /**
  * Disconnect from the core service.  This function can only 
  * be called *after* all pending 'GNUNET_CORE_notify_transmit_ready'
- * requests have been explicitly cancelled.
+ * requests have been explicitly canceled.
  *
  * @param handle connection to core to disconnect
  */
@@ -1178,11 +1431,10 @@ GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
 {
   struct ControlMessage *cm;
   
-  if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
-    {
-      GNUNET_SCHEDULER_cancel (handle->reconnect_task);
-      handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
-    }
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Disconnecting from CORE service\n");
+#endif
   if (handle->cth != NULL)
     {
       GNUNET_CLIENT_notify_transmit_ready_cancel (handle->cth);
@@ -1193,11 +1445,20 @@ GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle)
       GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
       handle->client = NULL;
     }
+  if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (handle->reconnect_task);
+      handle->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    }
   while (NULL != (cm = handle->pending_head))
     {
       GNUNET_CONTAINER_DLL_remove (handle->pending_head,
                                   handle->pending_tail,
                                   cm);
+      if (cm->th != NULL)
+       cm->th->cm = NULL;
+      if (cm->cont != NULL)
+       cm->cont (cm->cont_cls, NULL);
       GNUNET_free (cm);
     }
   GNUNET_CONTAINER_multihashmap_iterate (handle->peers,
@@ -1247,6 +1508,9 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
   if (NULL == pr)
     {
       /* attempt to send to peer that is not connected */
+      GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+                 "Attempting to send to peer `%s' from peer `%s', but not connected!\n",
+                 GNUNET_i2s(target), GNUNET_h2s(&handle->me.hashPubKey));
       GNUNET_break (0);
       return NULL;
     }
@@ -1262,9 +1526,9 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
   /* bound queue size */
   if (pr->queue_size == handle->queue_size)
     {
-      /* find lowest-priority entry */
-      minp = pr->pending_head;
-      prev = minp->next;
+      /* find lowest-priority entry, but skip the head of the list */
+      minp = pr->pending_head->next;
+      prev = minp;
       while (prev != NULL)
        {
          if (prev->priority < minp->priority)
@@ -1278,7 +1542,13 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
          return NULL;
        }
       if (priority <= minp->priority)
-       return NULL; /* priority too low */
+       {
+#if DEBUG_CORE
+         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                     "Dropping transmission request: priority too low\n");
+#endif
+         return NULL; /* priority too low */
+       }
       GNUNET_CONTAINER_DLL_remove (pr->pending_head,
                                   pr->pending_tail,
                                   minp);
@@ -1314,6 +1584,10 @@ GNUNET_CORE_notify_transmit_ready (struct GNUNET_CORE_Handle *handle,
                                     th);
   pr->queue_size++;
   /* was the request queue previously empty? */
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Transmission request added to queue\n");
+#endif
   if (pr->pending_head == th) 
     request_next_transmission (pr);
   return th;
@@ -1337,6 +1611,7 @@ GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
   GNUNET_CONTAINER_DLL_remove (pr->pending_head,
                               pr->pending_tail,
                               th);    
+  pr->queue_size--;
   if (th->cm != NULL)
     {
       /* we're currently in the control queue, remove */
@@ -1353,7 +1628,7 @@ GNUNET_CORE_notify_transmit_ready_cancel (struct GNUNET_CORE_TransmitHandle
           (pr == h->ready_peer_head) )
        {
          /* the request that was 'approved' by core was
-            cancelled before it could be transmitted; remove
+            canceled before it could be transmitted; remove
             us from the 'ready' list */
          GNUNET_CONTAINER_DLL_remove (h->ready_peer_head,
                                       h->ready_peer_tail,
@@ -1397,7 +1672,6 @@ struct GNUNET_CORE_PeerRequestHandle
 };
 
 
-
 /**
  * Continuation called when the control message was transmitted.
  * Calls the original continuation and frees the remaining
@@ -1413,7 +1687,13 @@ peer_request_connect_cont (void *cls,
   struct GNUNET_CORE_PeerRequestHandle *ret = cls;
   
   if (ret->cont != NULL)
-    ret->cont (ret->cont_cls, tc);
+    {
+      if (tc == NULL)
+       GNUNET_SCHEDULER_add_now (ret->cont,
+                                 ret->cont_cls);
+      else
+       ret->cont (ret->cont_cls, tc);
+    }
   GNUNET_free (ret);
 }
 
@@ -1465,8 +1745,12 @@ GNUNET_CORE_peer_request_connect (struct GNUNET_CORE_Handle *h,
   ret->cont_cls = cont_cls;
   cm->cont = &peer_request_connect_cont;
   cm->cont_cls = ret;
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Queueing REQUEST_CONNECT request\n");
+#endif
   if (h->pending_head == cm)
-    trigger_next_request (h);
+    trigger_next_request (h, GNUNET_NO);
   return ret;
 }
 
@@ -1595,6 +1879,7 @@ GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
     }
   irc = GNUNET_malloc (sizeof (struct GNUNET_CORE_InformationRequestContext));
   irc->h = h;
+  irc->pr = pr;
   irc->info = info;
   irc->info_cls = info_cls;
   cm = GNUNET_malloc (sizeof (struct ControlMessage) +
@@ -1610,11 +1895,17 @@ GNUNET_CORE_peer_change_preference (struct GNUNET_CORE_Handle *h,
   rim->reserve_inbound = htonl (amount);
   rim->preference_change = GNUNET_htonll(preference);
   rim->peer = *peer;
+#if DEBUG_CORE
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Queueing CHANGE PREFERENCE request\n");
+#endif
   GNUNET_CONTAINER_DLL_insert (h->pending_head,
                               h->pending_tail,
                               cm); 
   pr->pcic = info;
   pr->pcic_cls = info_cls;
+  if (h->pending_head == cm)
+    trigger_next_request (h, GNUNET_NO);
   return irc;
 }
 
@@ -1649,70 +1940,4 @@ GNUNET_CORE_peer_change_preference_cancel (struct GNUNET_CORE_InformationRequest
 }
 
 
-/* ********************* GNUNET_CORE_iterate_peers *********************** */
-
-/**
- * Context for 'iterate_peers' helper function.
- */
-struct IterationContext
-{
-  /**
-   * Callback to call.
-   */
-  GNUNET_CORE_ConnectEventHandler peer_cb;
-
-  /**
-   * Closure for 'peer_cb'.
-   */
-  void *cb_cls;
-};
-
-
-/**
- * Call callback for each peer.
- *
- * @param cls the 'struct IterationContext'
- * @param hc peer identity, not used
- * @param value the 'struct PeerRecord'
- * @return GNUNET_YES (continue iteration)
- */
-static int
-iterate_peers (void *cls,
-              const GNUNET_HashCode *hc,
-              void *value)
-{
-  struct IterationContext *ic = cls;
-  struct PeerRecord *pr = value;
-
-  ic->peer_cb (ic->cb_cls,
-              &pr->peer,
-              NULL /* FIXME: pass atsi? */);
-  return GNUNET_YES;
-}
-
-
-/**
- * Obtain statistics and/or change preferences for the given peer.
- *
- * @param h handle to core
- * @param peer_cb function to call with the peer information
- * @param cb_cls closure for peer_cb
- * @return GNUNET_OK if iterating, GNUNET_SYSERR on error
- */
-int
-GNUNET_CORE_iterate_peers (struct GNUNET_CORE_Handle *h,
-                           GNUNET_CORE_ConnectEventHandler peer_cb,
-                           void *cb_cls)
-{
-  struct IterationContext ic;
-
-  ic.peer_cb = peer_cb;
-  ic.cb_cls = cb_cls;
-  GNUNET_CONTAINER_multihashmap_iterate (h->peers,
-                                        &iterate_peers,
-                                        &ic);
-  return GNUNET_OK;
-}
-
-
 /* end of core_api.c */