convert fs publish to MQ
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index 94f3f5c8785fccfc74211c7853713af69a1c12f6..6a1924e5409b327b3deb842d6360cd050a3c9405 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.
 */
 
 /**
@@ -144,7 +144,7 @@ struct PingMessage
   struct GNUNET_PeerIdentity target;
 
   /**
-   * Random number chosen to make reply harder.
+   * Random number chosen to make replay harder.
    */
   uint32_t challenge GNUNET_PACKED;
 };
@@ -166,8 +166,7 @@ struct PongMessage
   uint32_t iv_seed GNUNET_PACKED;
 
   /**
-   * Random number to make faking the reply harder.  Must be
-   * first field after header (this is where we start to encrypt!).
+   * Random number to make replay attacks harder.
    */
   uint32_t challenge GNUNET_PACKED;
 
@@ -191,7 +190,7 @@ struct PongMessage
 struct EncryptedMessage
 {
   /**
-   * Message type is either #GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
+   * Message type is #GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
    */
   struct GNUNET_MessageHeader header;
 
@@ -201,10 +200,10 @@ struct EncryptedMessage
   uint32_t iv_seed GNUNET_PACKED;
 
   /**
-   * MAC of the encrypted message (starting at 'sequence_number'),
+   * MAC of the encrypted message (starting at @e sequence_number),
    * used to verify message integrity. Everything after this value
    * (excluding this value itself) will be encrypted and authenticated.
-   * ENCRYPTED_HEADER_SIZE must be set to the offset of the *next* field.
+   * #ENCRYPTED_HEADER_SIZE must be set to the offset of the *next* field.
    */
   struct GNUNET_HashCode hmac;
 
@@ -220,7 +219,7 @@ struct EncryptedMessage
   uint32_t reserved;
 
   /**
-   * Timestamp.  Used to prevent reply of ancient messages
+   * Timestamp.  Used to prevent replay of ancient messages
    * (recent messages are caught with the sequence number).
    */
   struct GNUNET_TIME_AbsoluteNBO timestamp;
@@ -302,12 +301,12 @@ struct GSC_KeyExchangeInfo
   /**
    * ID of task used for re-trying SET_KEY and PING message.
    */
-  struct GNUNET_SCHEDULER_Task * retry_set_key_task;
+  struct GNUNET_SCHEDULER_Task *retry_set_key_task;
 
   /**
    * ID of task used for sending keep-alive pings.
    */
-  struct GNUNET_SCHEDULER_Task * keep_alive_task;
+  struct GNUNET_SCHEDULER_Task *keep_alive_task;
 
   /**
    * Bit map indicating which of the 32 sequence numbers before the last
@@ -373,7 +372,7 @@ static struct GSC_KeyExchangeInfo *kx_tail;
  * Task scheduled for periodic re-generation (and thus rekeying) of our
  * ephemeral key.
  */
-static struct GNUNET_SCHEDULER_Task * rekey_task;
+static struct GNUNET_SCHEDULER_Task *rekey_task;
 
 /**
  * Notification context for all monitors.
@@ -476,12 +475,14 @@ derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
  */
 static void
 derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
-           const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
+           const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
+           uint32_t seed,
            const struct GNUNET_PeerIdentity *identity)
 {
   static const char ctx[] = "initialization vector";
 
-  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
+                                     &seed, sizeof (seed),
                                     identity,
                                     sizeof (struct GNUNET_PeerIdentity), ctx,
                                     sizeof (ctx), NULL);
@@ -499,12 +500,15 @@ derive_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
  */
 static void
 derive_pong_iv (struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
-                const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed,
-                uint32_t challenge, const struct GNUNET_PeerIdentity *identity)
+                const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
+                uint32_t seed,
+                uint32_t challenge,
+                const struct GNUNET_PeerIdentity *identity)
 {
   static const char ctx[] = "pong initialization vector";
 
-  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey, &seed, sizeof (seed),
+  GNUNET_CRYPTO_symmetric_derive_iv (iv, skey,
+                                     &seed, sizeof (seed),
                                     identity,
                                     sizeof (struct GNUNET_PeerIdentity),
                                     &challenge, sizeof (challenge),
@@ -552,7 +556,9 @@ derive_aes_key (const struct GNUNET_PeerIdentity *sender,
 static int
 do_encrypt (struct GSC_KeyExchangeInfo *kx,
             const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
-            const void *in, void *out, size_t size)
+            const void *in,
+            void *out,
+            size_t size)
 {
   if (size != (uint16_t) size)
   {
@@ -568,8 +574,9 @@ do_encrypt (struct GSC_KeyExchangeInfo *kx,
      so we require manual intervention to get this one... */
 #if 0
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Encrypted %u bytes for `%4s' using key %u, IV %u\n",
-              (unsigned int) size, GNUNET_i2s (&kx->peer),
+              "Encrypted %u bytes for `%s' using key %u, IV %u\n",
+              (unsigned int) size,
+              GNUNET_i2s (&kx->peer),
               (unsigned int) kx->encrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
                                                                            sizeof
                                                                            (iv)));
@@ -593,7 +600,9 @@ do_encrypt (struct GSC_KeyExchangeInfo *kx,
 static int
 do_decrypt (struct GSC_KeyExchangeInfo *kx,
             const struct GNUNET_CRYPTO_SymmetricInitializationVector *iv,
-            const void *in, void *out, size_t size)
+            const void *in,
+            void *out,
+            size_t size)
 {
   if (size != (uint16_t) size)
   {
@@ -608,23 +617,30 @@ do_decrypt (struct GSC_KeyExchangeInfo *kx,
     return GNUNET_SYSERR;
   }
   if (size !=
-      GNUNET_CRYPTO_symmetric_decrypt (in, (uint16_t) size, &kx->decrypt_key, iv,
-                                 out))
+      GNUNET_CRYPTO_symmetric_decrypt (in,
+                                       (uint16_t) size,
+                                       &kx->decrypt_key,
+                                       iv,
+                                       out))
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# bytes decrypted"), size,
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# bytes decrypted"),
+                            size,
                             GNUNET_NO);
   /* the following is too sensitive to write to log files by accident,
      so we require manual intervention to get this one... */
 #if 0
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Decrypted %u bytes from `%4s' using key %u, IV %u\n",
-              (unsigned int) size, GNUNET_i2s (&kx->peer),
-              (unsigned int) kx->decrypt_key.crc32, GNUNET_CRYPTO_crc32_n (iv,
-                                                                           sizeof
-                                                                           (*iv)));
+              "Decrypted %u bytes from `%s' using key %u, IV %u\n",
+              (unsigned int) size,
+              GNUNET_i2s (&kx->peer),
+              (unsigned int) kx->decrypt_key.crc32,
+              GNUNET_CRYPTO_crc32_n (iv,
+                                     sizeof
+                                     (*iv)));
 #endif
   return GNUNET_OK;
 }
@@ -643,11 +659,9 @@ send_key (struct GSC_KeyExchangeInfo *kx);
  * Task that will retry #send_key() if our previous attempt failed.
  *
  * @param cls our `struct GSC_KeyExchangeInfo`
- * @param tc scheduler context
  */
 static void
-set_key_retry_task (void *cls,
-                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+set_key_retry_task (void *cls)
 {
   struct GSC_KeyExchangeInfo *kx = cls;
 
@@ -671,6 +685,8 @@ setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
 
   pm = &kx->ping;
+  kx->ping_challenge = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                                 UINT32_MAX);
   pm->header.size = htons (sizeof (struct PingMessage));
   pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
   pm->iv_seed = calculate_seed (kx);
@@ -700,7 +716,8 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
              "Initiating key exchange with `%s'\n",
               GNUNET_i2s (pid));
   GNUNET_STATISTICS_update (GSC_stats,
-                            gettext_noop ("# key exchanges initiated"), 1,
+                            gettext_noop ("# key exchanges initiated"),
+                            1,
                             GNUNET_NO);
   kx = GNUNET_new (struct GSC_KeyExchangeInfo);
   kx->peer = *pid;
@@ -728,8 +745,10 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
   {
     /* peer with "higher" identity starts a delayed  KX, if the "lower" peer
      * does not start a KX since he sees no reasons to do so  */
-    kx->retry_set_key_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                                           &set_key_retry_task, kx);
+    kx->retry_set_key_task
+      = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                     &set_key_retry_task,
+                                     kx);
   }
   return kx;
 }
@@ -746,12 +765,12 @@ GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
   GSC_SESSIONS_end (&kx->peer);
   GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# key exchanges stopped"),
                             1, GNUNET_NO);
-  if (kx->retry_set_key_task != NULL)
+  if (NULL != kx->retry_set_key_task)
   {
     GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
     kx->retry_set_key_task = NULL;
   }
-  if (kx->keep_alive_task != NULL)
+  if (NULL != kx->keep_alive_task)
   {
     GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
     kx->keep_alive_task = NULL;
@@ -773,9 +792,13 @@ GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
 static void
 send_ping (struct GSC_KeyExchangeInfo *kx)
 {
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# PING messages transmitted"),
+                            1,
+                            GNUNET_NO);
   GSC_NEIGHBOURS_transmit (&kx->peer,
                            &kx->ping.header,
-                           MIN_PING_FREQUENCY);
+                           kx->set_key_retry_frequency);
 }
 
 
@@ -814,8 +837,8 @@ derive_session_keys (struct GSC_KeyExchangeInfo *kx)
 
 
 /**
- * We received a SET_KEY message.  Validate and update
- * our key material and status.
+ * We received a #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY message.
+ * Validate and update our key material and status.
  *
  * @param kx key exchange status for the corresponding peer
  * @param msg the set key message we received
@@ -842,7 +865,7 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   if ( ( (GNUNET_CORE_KX_STATE_KEY_RECEIVED == kx->status) ||
         (GNUNET_CORE_KX_STATE_UP == kx->status) ||
         (GNUNET_CORE_KX_STATE_REKEY_SENT == kx->status) ) &&
-       (end_t.abs_value_us <= kx->foreign_key_expires.abs_value_us) )
+       (end_t.abs_value_us < kx->foreign_key_expires.abs_value_us) )
   {
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop ("# old ephemeral keys ignored"),
@@ -855,17 +878,21 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
                             gettext_noop ("# ephemeral keys received"),
                             1, GNUNET_NO);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' request from `%4s'.\n", "EPHEMERAL_KEY",
-              GNUNET_i2s (&kx->peer));
   if (0 !=
       memcmp (&m->origin_identity,
-             &kx->peer.public_key,
+             &kx->peer,
               sizeof (struct GNUNET_PeerIdentity)))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Received EPHEMERAL_KEY from %s, but expected %s\n",
+                GNUNET_i2s (&m->origin_identity),
+                GNUNET_i2s_full (&kx->peer));
     GNUNET_break_op (0);
     return;
   }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Core service receives EPHEMERAL_KEY request from `%s'.\n",
+              GNUNET_i2s (&kx->peer));
   if ((ntohl (m->purpose.size) !=
        sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
        sizeof (struct GNUNET_TIME_AbsoluteNBO) +
@@ -874,8 +901,9 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
        sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)) ||
       (GNUNET_OK !=
        GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_SET_ECC_KEY,
-                                &m->purpose,
-                                 &m->signature, &m->origin_identity.public_key)))
+                                   &m->purpose,
+                                   &m->signature,
+                                   &m->origin_identity.public_key)))
   {
     /* invalid signature */
     GNUNET_break_op (0);
@@ -888,9 +916,9 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Ephemeral key message from peer `%s' rejected as its validity range does not match our system time (%llu not in [%llu,%llu]).\n"),
                GNUNET_i2s (&kx->peer),
-               now.abs_value_us,
-               start_t.abs_value_us,
-               end_t.abs_value_us);
+               (unsigned long long) now.abs_value_us,
+                (unsigned long long) start_t.abs_value_us,
+                (unsigned long long) end_t.abs_value_us);
     return;
   }
   kx->other_ephemeral_key = m->ephemeral_key;
@@ -934,7 +962,8 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
     monitor_notify_all (kx);
     if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
-    send_ping (kx);
+    else
+      send_ping (kx);
     break;
   case GNUNET_CORE_KX_STATE_KEY_SENT:
     GNUNET_assert (NULL == kx->keep_alive_task);
@@ -942,27 +971,29 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
     monitor_notify_all (kx);
     if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
-    send_ping (kx);
+    else
+      send_ping (kx);
     break;
   case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
     GNUNET_assert (NULL == kx->keep_alive_task);
     if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
-    send_ping (kx);
+    else
+      send_ping (kx);
     break;
   case GNUNET_CORE_KX_STATE_UP:
     kx->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
     monitor_notify_all (kx);
     if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
-    /* we got a new key, need to reconfirm! */
-    send_ping (kx);
+    else
+      send_ping (kx);
     break;
   case GNUNET_CORE_KX_STATE_REKEY_SENT:
     if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
-    /* we got a new key, need to reconfirm! */
-    send_ping (kx);
+    else
+      send_ping (kx);
     break;
   default:
     GNUNET_break (0);
@@ -996,7 +1027,8 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
     return;
   }
   GNUNET_STATISTICS_update (GSC_stats,
-                            gettext_noop ("# PING messages received"), 1,
+                            gettext_noop ("# PING messages received"),
+                            1,
                             GNUNET_NO);
   if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
        (kx->status != GNUNET_CORE_KX_STATE_UP) &&
@@ -1004,13 +1036,14 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   {
     /* ignore */
     GNUNET_STATISTICS_update (GSC_stats,
-                             gettext_noop ("# PING messages dropped (out of order)"), 1,
+                             gettext_noop ("# PING messages dropped (out of order)"),
+                              1,
                              GNUNET_NO);
     return;
   }
   m = (const struct PingMessage *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' request from `%4s'.\n", "PING",
+              "Core service receives PING request from `%s'.\n",
               GNUNET_i2s (&kx->peer));
   derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
   if (GNUNET_OK !=
@@ -1022,17 +1055,18 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
     return;
   }
   if (0 !=
-      memcmp (&t.target, &GSC_my_identity, sizeof (struct GNUNET_PeerIdentity)))
+      memcmp (&t.target,
+              &GSC_my_identity,
+              sizeof (struct GNUNET_PeerIdentity)))
   {
-    char sender[9];
-    char peer[9];
-
-    GNUNET_snprintf (sender, sizeof (sender), "%8s", GNUNET_i2s (&kx->peer));
-    GNUNET_snprintf (peer, sizeof (peer), "%8s", GNUNET_i2s (&t.target));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _
-                ("Received PING from `%s' for different identity: I am `%s', PONG identity: `%s'\n"),
-                sender, GNUNET_i2s (&GSC_my_identity), peer);
+    if (GNUNET_CORE_KX_STATE_REKEY_SENT != kx->status)
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Decryption of PING from peer `%s' failed\n",
+                  GNUNET_i2s (&kx->peer));
+    else
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Decryption of PING from peer `%s' failed after rekey (harmless)\n",
+                  GNUNET_i2s (&kx->peer));
     GNUNET_break_op (0);
     return;
   }
@@ -1043,15 +1077,24 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   tp.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
   tp.header.size = htons (sizeof (struct PongMessage));
   tp.iv_seed = calculate_seed (kx);
-  derive_pong_iv (&iv, &kx->encrypt_key, tp.iv_seed, t.challenge, &kx->peer);
-  do_encrypt (kx, &iv, &tx.challenge, &tp.challenge,
+  derive_pong_iv (&iv,
+                  &kx->encrypt_key,
+                  tp.iv_seed,
+                  t.challenge,
+                  &kx->peer);
+  do_encrypt (kx,
+              &iv,
+              &tx.challenge,
+              &tp.challenge,
               sizeof (struct PongMessage) - ((void *) &tp.challenge -
                                              (void *) &tp));
-  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# PONG messages created"),
-                            1, GNUNET_NO);
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# PONG messages created"),
+                            1,
+                            GNUNET_NO);
   GSC_NEIGHBOURS_transmit (&kx->peer,
                            &tp.header,
-                           GNUNET_TIME_UNIT_FOREVER_REL /* FIXME: timeout */ );
+                           kx->set_key_retry_frequency);
 }
 
 
@@ -1059,11 +1102,10 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
  * Task triggered when a neighbour entry is about to time out
  * (and we should prevent this by sending a PING).
  *
- * @param cls the 'struct GSC_KeyExchangeInfo'
- * @param tc scheduler context (not used)
+ * @param cls the `struct GSC_KeyExchangeInfo`
  */
 static void
-send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_keep_alive (void *cls)
 {
   struct GSC_KeyExchangeInfo *kx = cls;
   struct GNUNET_TIME_Relative retry;
@@ -1075,27 +1117,30 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   {
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop ("# sessions terminated by timeout"),
-                              1, GNUNET_NO);
+                              1,
+                              GNUNET_NO);
     GSC_SESSIONS_end (&kx->peer);
     kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
     monitor_notify_all (kx);
     send_key (kx);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending KEEPALIVE to `%s'\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Sending KEEPALIVE to `%s'\n",
               GNUNET_i2s (&kx->peer));
   GNUNET_STATISTICS_update (GSC_stats,
-                            gettext_noop ("# keepalive messages sent"), 1,
+                            gettext_noop ("# keepalive messages sent"),
+                            1,
                             GNUNET_NO);
   setup_fresh_ping (kx);
-  GSC_NEIGHBOURS_transmit (&kx->peer,
-                           &kx->ping.header,
-                           kx->set_key_retry_frequency);
+  send_ping (kx);
   retry =
       GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
                                 MIN_PING_FREQUENCY);
   kx->keep_alive_task =
-      GNUNET_SCHEDULER_add_delayed (retry, &send_keep_alive, kx);
+      GNUNET_SCHEDULER_add_delayed (retry,
+                                    &send_keep_alive,
+                                    kx);
 }
 
 
@@ -1122,12 +1167,14 @@ update_timeout (struct GSC_KeyExchangeInfo *kx)
        are bigger than the threshold (5s) */
     monitor_notify_all (kx);
   }
-  if (kx->keep_alive_task != NULL)
+  if (NULL != kx->keep_alive_task)
     GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
   kx->keep_alive_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
                                     (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
-                                     2), &send_keep_alive, kx);
+                                     2),
+                                    &send_keep_alive,
+                                    kx);
 }
 
 
@@ -1153,7 +1200,8 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
     return;
   }
   GNUNET_STATISTICS_update (GSC_stats,
-                            gettext_noop ("# PONG messages received"), 1,
+                            gettext_noop ("# PONG messages received"),
+                            1,
                             GNUNET_NO);
   switch (kx->status)
   {
@@ -1179,14 +1227,20 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
   }
   m = (const struct PongMessage *) msg;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Core service receives `%s' response from `%4s'.\n", "PONG",
+              "Core service receives PONG response from `%s'.\n",
               GNUNET_i2s (&kx->peer));
   /* mark as garbage, just to be sure */
   memset (&t, 255, sizeof (t));
-  derive_pong_iv (&iv, &kx->decrypt_key, m->iv_seed, kx->ping_challenge,
+  derive_pong_iv (&iv,
+                  &kx->decrypt_key,
+                  m->iv_seed,
+                  kx->ping_challenge,
                   &GSC_my_identity);
   if (GNUNET_OK !=
-      do_decrypt (kx, &iv, &m->challenge, &t.challenge,
+      do_decrypt (kx,
+                  &iv,
+                  &m->challenge,
+                  &t.challenge,
                   sizeof (struct PongMessage) - ((void *) &m->challenge -
                                                  (void *) m)))
   {
@@ -1194,19 +1248,23 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
     return;
   }
   GNUNET_STATISTICS_update (GSC_stats,
-                            gettext_noop ("# PONG messages decrypted"), 1,
+                            gettext_noop ("# PONG messages decrypted"),
+                            1,
                             GNUNET_NO);
-  if ((0 != memcmp (&t.target, &kx->peer, sizeof (struct GNUNET_PeerIdentity)))
-      || (kx->ping_challenge != t.challenge))
+  if ((0 != memcmp (&t.target,
+                    &kx->peer,
+                    sizeof (struct GNUNET_PeerIdentity))) ||
+      (kx->ping_challenge != t.challenge))
   {
     /* PONG malformed */
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received malformed `%s' wanted sender `%4s' with challenge %u\n",
-                "PONG", GNUNET_i2s (&kx->peer),
+                "Received malformed PONG wanted sender `%s' with challenge %u\n",
+                GNUNET_i2s (&kx->peer),
                 (unsigned int) kx->ping_challenge);
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received malformed `%s' received from `%4s' with challenge %u\n",
-                "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
+                "Received malformed PONG received from `%s' with challenge %u\n",
+                GNUNET_i2s (&t.target),
+                (unsigned int) t.challenge);
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1228,8 +1286,8 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
     return;
   case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
     GNUNET_STATISTICS_update (GSC_stats,
-                              gettext_noop
-                              ("# session keys confirmed via PONG"), 1,
+                              gettext_noop ("# session keys confirmed via PONG"),
+                              1,
                               GNUNET_NO);
     kx->status = GNUNET_CORE_KX_STATE_UP;
     monitor_notify_all (kx);
@@ -1239,15 +1297,15 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
     break;
   case GNUNET_CORE_KX_STATE_UP:
     GNUNET_STATISTICS_update (GSC_stats,
-                              gettext_noop
-                              ("# timeouts prevented via PONG"), 1,
+                              gettext_noop ("# timeouts prevented via PONG"),
+                              1,
                               GNUNET_NO);
     update_timeout (kx);
     break;
   case GNUNET_CORE_KX_STATE_REKEY_SENT:
     GNUNET_STATISTICS_update (GSC_stats,
-                              gettext_noop
-                              ("# rekey operations confirmed via PONG"), 1,
+                              gettext_noop ("# rekey operations confirmed via PONG"),
+                              1,
                               GNUNET_NO);
     kx->status = GNUNET_CORE_KX_STATE_UP;
     monitor_notify_all (kx);
@@ -1283,9 +1341,12 @@ send_key (struct GSC_KeyExchangeInfo *kx)
   GSC_NEIGHBOURS_transmit (&kx->peer,
                            &current_ekm.header,
                            kx->set_key_retry_frequency);
+  if (GNUNET_CORE_KX_STATE_KEY_SENT != kx->status)
+    send_ping (kx);
   kx->retry_set_key_task =
       GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
-                                    &set_key_retry_task, kx);
+                                    &set_key_retry_task,
+                                    kx);
 }
 
 
@@ -1314,24 +1375,35 @@ GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
   ph->iv_seed = calculate_seed (kx);
   ph->reserved = 0;
   ph->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
-  memcpy (&ph[1], payload, payload_size);
+  memcpy (&ph[1],
+          payload,
+          payload_size);
 
   em = (struct EncryptedMessage *) cbuf;
   em->header.size = htons (used);
   em->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE);
   em->iv_seed = ph->iv_seed;
-  derive_iv (&iv, &kx->encrypt_key, ph->iv_seed, &kx->peer);
+  derive_iv (&iv,
+             &kx->encrypt_key,
+             ph->iv_seed,
+             &kx->peer);
   GNUNET_assert (GNUNET_OK ==
-                 do_encrypt (kx, &iv, &ph->sequence_number,
+                 do_encrypt (kx,
+                             &iv,
+                             &ph->sequence_number,
                              &em->sequence_number,
                              used - ENCRYPTED_HEADER_SIZE));
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted %u bytes for %s\n",
-              used - ENCRYPTED_HEADER_SIZE, GNUNET_i2s (&kx->peer));
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Encrypted %u bytes for %s\n",
+              (unsigned int) (used - ENCRYPTED_HEADER_SIZE),
+              GNUNET_i2s (&kx->peer));
   derive_auth_key (&auth_key,
                   &kx->encrypt_key,
                   ph->iv_seed);
-  GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
-                      used - ENCRYPTED_HEADER_SIZE, &em->hmac);
+  GNUNET_CRYPTO_hmac (&auth_key,
+                      &em->sequence_number,
+                      used - ENCRYPTED_HEADER_SIZE,
+                      &em->hmac);
   GSC_NEIGHBOURS_transmit (&kx->peer,
                            &em->header,
                            GNUNET_TIME_UNIT_FOREVER_REL);
@@ -1388,9 +1460,9 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
   if (GNUNET_CORE_KX_STATE_UP != kx->status)
   {
     GNUNET_STATISTICS_update (GSC_stats,
-                              gettext_noop
-                              ("# DATA message dropped (out of order)"),
-                              1, GNUNET_NO);
+                              gettext_noop ("# DATA message dropped (out of order)"),
+                              1,
+                              GNUNET_NO);
     return;
   }
   if (0 == GNUNET_TIME_absolute_get_remaining (kx->foreign_key_expires).rel_value_us)
@@ -1414,10 +1486,16 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
   }
 
   /* validate hash */
-  derive_auth_key (&auth_key, &kx->decrypt_key, m->iv_seed);
-  GNUNET_CRYPTO_hmac (&auth_key, &m->sequence_number,
-                      size - ENCRYPTED_HEADER_SIZE, &ph);
-  if (0 != memcmp (&ph, &m->hmac, sizeof (struct GNUNET_HashCode)))
+  derive_auth_key (&auth_key,
+                   &kx->decrypt_key,
+                   m->iv_seed);
+  GNUNET_CRYPTO_hmac (&auth_key,
+                      &m->sequence_number,
+                      size - ENCRYPTED_HEADER_SIZE,
+                      &ph);
+  if (0 != memcmp (&ph,
+                   &m->hmac,
+                   sizeof (struct GNUNET_HashCode)))
   {
     /* checksum failed */
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1425,15 +1503,21 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
                GNUNET_i2s (&kx->peer));
     return;
   }
-  derive_iv (&iv, &kx->decrypt_key, m->iv_seed, &GSC_my_identity);
+  derive_iv (&iv,
+             &kx->decrypt_key,
+             m->iv_seed,
+             &GSC_my_identity);
   /* decrypt */
   if (GNUNET_OK !=
-      do_decrypt (kx, &iv, &m->sequence_number, &buf[ENCRYPTED_HEADER_SIZE],
+      do_decrypt (kx,
+                  &iv,
+                  &m->sequence_number,
+                  &buf[ENCRYPTED_HEADER_SIZE],
                   size - ENCRYPTED_HEADER_SIZE))
     return;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Decrypted %u bytes from %s\n",
-              size - ENCRYPTED_HEADER_SIZE,
+              (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
               GNUNET_i2s (&kx->peer));
   pt = (struct EncryptedMessage *) buf;
 
@@ -1446,7 +1530,8 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
     /* duplicate, ignore */
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop ("# bytes dropped (duplicates)"),
-                              size, GNUNET_NO);
+                              size,
+                              GNUNET_NO);
     return;
   }
   if ((kx->last_sequence_number_received > snum) &&
@@ -1541,9 +1626,9 @@ deliver_message (void *cls,
   if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
   {
     GNUNET_STATISTICS_update (GSC_stats,
-                              gettext_noop
-                              ("# PAYLOAD dropped (out of order)"),
-                              1, GNUNET_NO);
+                              gettext_noop ("# PAYLOAD dropped (out of order)"),
+                              1,
+                              GNUNET_NO);
     return GNUNET_OK;
   }
   switch (ntohs (m->type))
@@ -1610,11 +1695,9 @@ sign_ephemeral_key ()
  * Task run to trigger rekeying.
  *
  * @param cls closure, NULL
- * @param tc scheduler context
  */
 static void
-do_rekey (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_rekey (void *cls)
 {
   struct GSC_KeyExchangeInfo *pos;
 
@@ -1623,6 +1706,8 @@ do_rekey (void *cls,
                                             NULL);
   if (NULL != my_ephemeral_key)
     GNUNET_free (my_ephemeral_key);
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Rekeying\n");
   my_ephemeral_key = GNUNET_CRYPTO_ecdhe_key_create ();
   GNUNET_assert (NULL != my_ephemeral_key);
   sign_ephemeral_key ();