-improve UDP logging
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index e899b6dedbe0bb1a3a99f946446120a2768f254a..fc242b171dc6862385771011542ea13efd271654 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 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -77,17 +77,17 @@ struct EphemeralKeyMessage
 {
 
   /**
-   * Message type is CORE_EPHEMERAL_KEY.
+   * Message type is #GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY.
    */
   struct GNUNET_MessageHeader header;
 
   /**
-   * Status of the sender (should be in "enum PeerStateMachine"), nbo.
+   * Status of the sender (should be in `enum PeerStateMachine`), nbo.
    */
   int32_t sender_status GNUNET_PACKED;
 
   /**
-   * An ECC signature of the 'origin' asserting the validity of
+   * An ECC signature of the @e origin_identity asserting the validity of
    * the given ephemeral key.
    */
   struct GNUNET_CRYPTO_EddsaSignature signature;
@@ -128,7 +128,7 @@ struct EphemeralKeyMessage
 struct PingMessage
 {
   /**
-   * Message type is CORE_PING.
+   * Message type is #GNUNET_MESSAGE_TYPE_CORE_PING.
    */
   struct GNUNET_MessageHeader header;
 
@@ -156,7 +156,7 @@ struct PingMessage
 struct PongMessage
 {
   /**
-   * Message type is CORE_PONG.
+   * Message type is #GNUNET_MESSAGE_TYPE_CORE_PONG.
    */
   struct GNUNET_MessageHeader header;
 
@@ -191,7 +191,7 @@ struct PongMessage
 struct EncryptedMessage
 {
   /**
-   * Message type is either CORE_ENCRYPTED_MESSAGE.
+   * Message type is either #GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE.
    */
   struct GNUNET_MessageHeader header;
 
@@ -230,56 +230,12 @@ GNUNET_NETWORK_STRUCT_END
 
 
 /**
- * Number of bytes (at the beginning) of "struct EncryptedMessage"
+ * Number of bytes (at the beginning) of `struct EncryptedMessage`
  * that are NOT encrypted.
  */
 #define ENCRYPTED_HEADER_SIZE (offsetof(struct EncryptedMessage, sequence_number))
 
 
-/**
- * State machine for our P2P encryption handshake.  Everyone starts in
- * "DOWN", if we receive the other peer's key (other peer initiated)
- * we start in state RECEIVED (since we will immediately send our
- * own); otherwise we start in SENT.  If we get back a PONG from
- * within either state, we move up to CONFIRMED (the PONG will always
- * be sent back encrypted with the key we sent to the other peer).
- */
-enum KxStateMachine
-{
-  /**
-   * No handshake yet.
-   */
-  KX_STATE_DOWN,
-
-  /**
-   * We've sent our session key.
-   */
-  KX_STATE_KEY_SENT,
-
-  /**
-   * We've received the other peers session key.
-   */
-  KX_STATE_KEY_RECEIVED,
-
-  /**
-   * The other peer has confirmed our session key + PING with a PONG
-   * message encrypted with his session key (which we got).  Key
-   * exchange is done.
-   */
-  KX_STATE_UP,
-
-  /**
-   * We're rekeying (or had a timeout), so we have sent the other peer
-   * our new ephemeral key, but we did not get a matching PONG yet.
-   * This is equivalent to being 'KX_STATE_KEY_RECEIVED', except that
-   * the session is marked as 'up' with sessions (as we don't want to
-   * drop and re-establish P2P connections simply due to rekeying).
-   */
-  KX_STATE_REKEY_SENT
-
-};
-
-
 /**
  * Information about the status of a key exchange with another peer.
  */
@@ -333,6 +289,11 @@ struct GSC_KeyExchangeInfo
    */
   struct GNUNET_TIME_Absolute timeout;
 
+  /**
+   * What was the last timeout we informed our monitors about?
+   */
+  struct GNUNET_TIME_Absolute last_notify_timeout;
+
   /**
    * At what frequency are we currently re-trying SET_KEY messages?
    */
@@ -341,12 +302,12 @@ struct GSC_KeyExchangeInfo
   /**
    * ID of task used for re-trying SET_KEY and PING message.
    */
-  GNUNET_SCHEDULER_TaskIdentifier retry_set_key_task;
+  struct GNUNET_SCHEDULER_Task * retry_set_key_task;
 
   /**
    * ID of task used for sending keep-alive pings.
    */
-  GNUNET_SCHEDULER_TaskIdentifier keep_alive_task;
+  struct GNUNET_SCHEDULER_Task * keep_alive_task;
 
   /**
    * Bit map indicating which of the 32 sequence numbers before the last
@@ -373,7 +334,7 @@ struct GSC_KeyExchangeInfo
   /**
    * What is our connection status?
    */
-  enum KxStateMachine status;
+  enum GNUNET_CORE_KxState status;
 
 };
 
@@ -412,7 +373,75 @@ static struct GSC_KeyExchangeInfo *kx_tail;
  * Task scheduled for periodic re-generation (and thus rekeying) of our
  * ephemeral key.
  */
-static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
+static struct GNUNET_SCHEDULER_Task * rekey_task;
+
+/**
+ * Notification context for all monitors.
+ */
+static struct GNUNET_SERVER_NotificationContext *nc;
+
+
+/**
+ * Inform the given monitor about the KX state of
+ * the given peer.
+ *
+ * @param client client to inform
+ * @param kx key exchange state to inform about
+ */
+static void
+monitor_notify (struct GNUNET_SERVER_Client *client,
+                struct GSC_KeyExchangeInfo *kx)
+{
+  struct MonitorNotifyMessage msg;
+
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
+  msg.header.size = htons (sizeof (msg));
+  msg.state = htonl ((uint32_t) kx->status);
+  msg.peer = kx->peer;
+  msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
+  GNUNET_SERVER_notification_context_unicast (nc,
+                                              client,
+                                              &msg.header,
+                                              GNUNET_NO);
+}
+
+
+/**
+ * Calculate seed value we should use for a message.
+ *
+ * @param kx key exchange context
+ */
+static uint32_t
+calculate_seed (struct GSC_KeyExchangeInfo *kx)
+{
+  /* Note: may want to make this non-random and instead
+     derive from key material to avoid having an undetectable
+     side-channel */
+  return htonl (GNUNET_CRYPTO_random_u32
+               (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
+}
+
+
+/**
+ * Inform all monitors about the KX state of the given peer.
+ *
+ * @param kx key exchange state to inform about
+ */
+static void
+monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
+{
+  struct MonitorNotifyMessage msg;
+
+  msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
+  msg.header.size = htons (sizeof (msg));
+  msg.state = htonl ((uint32_t) kx->status);
+  msg.peer = kx->peer;
+  msg.timeout = GNUNET_TIME_absolute_hton (kx->timeout);
+  GNUNET_SERVER_notification_context_broadcast (nc,
+                                                &msg.header,
+                                                GNUNET_NO);
+  kx->last_notify_timeout = kx->timeout;
+}
 
 
 /**
@@ -424,7 +453,8 @@ static GNUNET_SCHEDULER_TaskIdentifier rekey_task;
  */
 static void
 derive_auth_key (struct GNUNET_CRYPTO_AuthKey *akey,
-                 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey, uint32_t seed)
+                 const struct GNUNET_CRYPTO_SymmetricSessionKey *skey,
+                 uint32_t seed)
 {
   static const char ctx[] = "authentication key";
 
@@ -509,15 +539,15 @@ derive_aes_key (const struct GNUNET_PeerIdentity *sender,
 
 
 /**
- * Encrypt size bytes from in and write the result to out.  Use the
- * key for outbound traffic of the given neighbour.
+ * Encrypt size bytes from @a in and write the result to @a out.  Use the
+ * @a kx key for outbound traffic of the given neighbour.
  *
  * @param kx key information context
  * @param iv initialization vector to use
  * @param in ciphertext
  * @param out plaintext
- * @param size size of in/out
- * @return GNUNET_OK on success
+ * @param size size of @a in/@a out
+ * @return #GNUNET_OK on success
  */
 static int
 do_encrypt (struct GSC_KeyExchangeInfo *kx,
@@ -549,8 +579,8 @@ do_encrypt (struct GSC_KeyExchangeInfo *kx,
 
 
 /**
- * Decrypt size bytes from in and write the result to out.  Use the
- * key for inbound traffic of the given neighbour.  This function does
+ * Decrypt size bytes from @a in and write the result to @a out.  Use the
+ * @a kx key for inbound traffic of the given neighbour.  This function does
  * NOT do any integrity-checks on the result.
  *
  * @param kx key information context
@@ -570,8 +600,9 @@ do_decrypt (struct GSC_KeyExchangeInfo *kx,
     GNUNET_break (0);
     return GNUNET_NO;
   }
-  if ( (kx->status != KX_STATE_KEY_RECEIVED) && (kx->status != KX_STATE_UP) &&
-       (kx->status != KX_STATE_REKEY_SENT) )
+  if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
+       (kx->status != GNUNET_CORE_KX_STATE_UP) &&
+       (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT) )
   {
     GNUNET_break_op (0);
     return GNUNET_SYSERR;
@@ -609,19 +640,20 @@ send_key (struct GSC_KeyExchangeInfo *kx);
 
 
 /**
- * Task that will retry "send_key" if our previous attempt failed.
+ * Task that will retry #send_key() if our previous attempt failed.
  *
- * @param cls our 'struct GSC_KeyExchangeInfo'
+ * @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,
+                    const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GSC_KeyExchangeInfo *kx = cls;
 
-  kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+  kx->retry_set_key_task = NULL;
   kx->set_key_retry_frequency = GNUNET_TIME_STD_BACKOFF (kx->set_key_retry_frequency);
-  GNUNET_assert (KX_STATE_DOWN != kx->status);
+  GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
   send_key (kx);
 }
 
@@ -641,8 +673,7 @@ setup_fresh_ping (struct GSC_KeyExchangeInfo *kx)
   pm = &kx->ping;
   pm->header.size = htons (sizeof (struct PingMessage));
   pm->header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PING);
-  pm->iv_seed =
-      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+  pm->iv_seed = calculate_seed (kx);
   derive_iv (&iv, &kx->encrypt_key, pm->iv_seed, &kx->peer);
   pp.challenge = kx->ping_challenge;
   pp.target = kx->peer;
@@ -677,10 +708,14 @@ GSC_KX_start (const struct GNUNET_PeerIdentity *pid)
   GNUNET_CONTAINER_DLL_insert (kx_head,
                               kx_tail,
                               kx);
-  GNUNET_CRYPTO_hash (pid, sizeof (struct GNUNET_PeerIdentity), &h1);
-  GNUNET_CRYPTO_hash (&GSC_my_identity, sizeof (struct GNUNET_PeerIdentity), &h2);
-
-  kx->status = KX_STATE_KEY_SENT;
+  kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
+  monitor_notify_all (kx);
+  GNUNET_CRYPTO_hash (pid,
+                      sizeof (struct GNUNET_PeerIdentity),
+                      &h1);
+  GNUNET_CRYPTO_hash (&GSC_my_identity,
+                      sizeof (struct GNUNET_PeerIdentity),
+                      &h2);
   if (0 < GNUNET_CRYPTO_hash_cmp (&h1,
                                  &h2))
   {
@@ -694,7 +729,7 @@ 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);
+                                                           &set_key_retry_task, kx);
   }
   return kx;
 }
@@ -711,16 +746,18 @@ 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 != GNUNET_SCHEDULER_NO_TASK)
+  if (kx->retry_set_key_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
-    kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+    kx->retry_set_key_task = NULL;
   }
-  if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
+  if (kx->keep_alive_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
-    kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+    kx->keep_alive_task = NULL;
   }
+  kx->status = GNUNET_CORE_KX_PEER_DISCONNECT;
+  monitor_notify_all (kx);
   GNUNET_CONTAINER_DLL_remove (kx_head,
                               kx_tail,
                               kx);
@@ -736,7 +773,8 @@ GSC_KX_stop (struct GSC_KeyExchangeInfo *kx)
 static void
 send_ping (struct GSC_KeyExchangeInfo *kx)
 {
-  GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
+  GSC_NEIGHBOURS_transmit (&kx->peer,
+                           &kx->ping.header,
                            MIN_PING_FREQUENCY);
 }
 
@@ -790,7 +828,7 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   struct GNUNET_TIME_Absolute start_t;
   struct GNUNET_TIME_Absolute end_t;
   struct GNUNET_TIME_Absolute now;
-  enum KxStateMachine sender_status;
+  enum GNUNET_CORE_KxState sender_status;
   uint16_t size;
 
   size = ntohs (msg->size);
@@ -801,18 +839,20 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   }
   m = (const struct EphemeralKeyMessage *) msg;
   end_t = GNUNET_TIME_absolute_ntoh (m->expiration_time);
-  if ( ( (KX_STATE_KEY_RECEIVED == kx->status) ||
-        (KX_STATE_UP == kx->status) ||
-        (KX_STATE_REKEY_SENT == kx->status) ) &&
+  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) )
   {
-    GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# old ephemeral keys ignored"),
+    GNUNET_STATISTICS_update (GSC_stats,
+                              gettext_noop ("# old ephemeral keys ignored"),
                              1, GNUNET_NO);
     return;
   }
   start_t = GNUNET_TIME_absolute_ntoh (m->creation_time);
 
-  GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# ephemeral keys received"),
+  GNUNET_STATISTICS_update (GSC_stats,
+                            gettext_noop ("# ephemeral keys received"),
                             1, GNUNET_NO);
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -861,19 +901,25 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
                             GNUNET_NO);
 
   /* check if we still need to send the sender our key */
-  sender_status = (enum KxStateMachine) ntohl (m->sender_status);
+  sender_status = (enum GNUNET_CORE_KxState) ntohl (m->sender_status);
   switch (sender_status)
   {
-  case KX_STATE_DOWN:
+  case GNUNET_CORE_KX_STATE_DOWN:
     GNUNET_break_op (0);
     break;
-  case KX_STATE_KEY_SENT:
+  case GNUNET_CORE_KX_STATE_KEY_SENT:
     /* fine, need to send our key after updating our status, see below */
+    GSC_SESSIONS_reinit (&kx->peer);
+    break;
+  case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
+    /* other peer already got our key, but typemap did go down */
+    GSC_SESSIONS_reinit (&kx->peer);
+    break;
+  case GNUNET_CORE_KX_STATE_UP:
+    /* other peer already got our key, typemap NOT down */
     break;
-  case KX_STATE_KEY_RECEIVED:
-  case KX_STATE_UP:
-  case KX_STATE_REKEY_SENT:
-    /* other peer already got our key */
+  case GNUNET_CORE_KX_STATE_REKEY_SENT:
+    /* other peer already got our key, typemap NOT down */
     break;
   default:
     GNUNET_break (0);
@@ -882,35 +928,38 @@ GSC_KX_handle_ephemeral_key (struct GSC_KeyExchangeInfo *kx,
   /* check if we need to confirm everything is fine via PING + PONG */
   switch (kx->status)
   {
-  case KX_STATE_DOWN:
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
-    kx->status = KX_STATE_KEY_RECEIVED;
-    if (KX_STATE_KEY_SENT == sender_status)
+  case GNUNET_CORE_KX_STATE_DOWN:
+    GNUNET_assert (NULL == kx->keep_alive_task);
+    kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
+    monitor_notify_all (kx);
+    if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
     send_ping (kx);
     break;
-  case KX_STATE_KEY_SENT:
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
-    kx->status = KX_STATE_KEY_RECEIVED;
-    if (KX_STATE_KEY_SENT == sender_status)
+  case GNUNET_CORE_KX_STATE_KEY_SENT:
+    GNUNET_assert (NULL == kx->keep_alive_task);
+    kx->status = GNUNET_CORE_KX_STATE_KEY_RECEIVED;
+    monitor_notify_all (kx);
+    if (GNUNET_CORE_KX_STATE_KEY_SENT == sender_status)
       send_key (kx);
     send_ping (kx);
     break;
-  case KX_STATE_KEY_RECEIVED:
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
-    if (KX_STATE_KEY_SENT == sender_status)
+  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);
     break;
-  case KX_STATE_UP:
-    kx->status = KX_STATE_REKEY_SENT;
-    if (KX_STATE_KEY_SENT == sender_status)
+  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);
     break;
-  case KX_STATE_REKEY_SENT:
-    if (KX_STATE_KEY_SENT == sender_status)
+  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);
@@ -949,9 +998,9 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop ("# PING messages received"), 1,
                             GNUNET_NO);
-  if ( (kx->status != KX_STATE_KEY_RECEIVED) &&
-       (kx->status != KX_STATE_UP) &&
-       (kx->status != KX_STATE_REKEY_SENT))
+  if ( (kx->status != GNUNET_CORE_KX_STATE_KEY_RECEIVED) &&
+       (kx->status != GNUNET_CORE_KX_STATE_UP) &&
+       (kx->status != GNUNET_CORE_KX_STATE_REKEY_SENT))
   {
     /* ignore */
     GNUNET_STATISTICS_update (GSC_stats,
@@ -993,15 +1042,15 @@ GSC_KX_handle_ping (struct GSC_KeyExchangeInfo *kx,
   tx.target = t.target;
   tp.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_PONG);
   tp.header.size = htons (sizeof (struct PongMessage));
-  tp.iv_seed =
-      GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
+  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,
               sizeof (struct PongMessage) - ((void *) &tp.challenge -
                                              (void *) &tp));
   GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# PONG messages created"),
                             1, GNUNET_NO);
-  GSC_NEIGHBOURS_transmit (&kx->peer, &tp.header,
+  GSC_NEIGHBOURS_transmit (&kx->peer,
+                           &tp.header,
                            GNUNET_TIME_UNIT_FOREVER_REL /* FIXME: timeout */ );
 }
 
@@ -1020,7 +1069,7 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   struct GNUNET_TIME_Relative retry;
   struct GNUNET_TIME_Relative left;
 
-  kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+  kx->keep_alive_task = NULL;
   left = GNUNET_TIME_absolute_get_remaining (kx->timeout);
   if (0 == left.rel_value_us)
   {
@@ -1028,7 +1077,8 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                               gettext_noop ("# sessions terminated by timeout"),
                               1, GNUNET_NO);
     GSC_SESSIONS_end (&kx->peer);
-    kx->status = KX_STATE_KEY_SENT;
+    kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
+    monitor_notify_all (kx);
     send_key (kx);
     return;
   }
@@ -1038,7 +1088,8 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                             gettext_noop ("# keepalive messages sent"), 1,
                             GNUNET_NO);
   setup_fresh_ping (kx);
-  GSC_NEIGHBOURS_transmit (&kx->peer, &kx->ping.header,
+  GSC_NEIGHBOURS_transmit (&kx->peer,
+                           &kx->ping.header,
                            kx->set_key_retry_frequency);
   retry =
       GNUNET_TIME_relative_max (GNUNET_TIME_relative_divide (left, 2),
@@ -1058,10 +1109,20 @@ send_keep_alive (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 update_timeout (struct GSC_KeyExchangeInfo *kx)
 {
+  struct GNUNET_TIME_Relative delta;
+
   kx->timeout =
       GNUNET_TIME_relative_to_absolute
       (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
-  if (kx->keep_alive_task != GNUNET_SCHEDULER_NO_TASK)
+  delta = GNUNET_TIME_absolute_get_difference (kx->last_notify_timeout,
+                                               kx->timeout);
+  if (delta.rel_value_us > 5LL * 1000LL * 1000LL)
+  {
+    /* we only notify monitors about timeout changes if those
+       are bigger than the threshold (5s) */
+    monitor_notify_all (kx);
+  }
+  if (kx->keep_alive_task != NULL)
     GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
   kx->keep_alive_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
@@ -1096,21 +1157,21 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
                             GNUNET_NO);
   switch (kx->status)
   {
-  case KX_STATE_DOWN:
+  case GNUNET_CORE_KX_STATE_DOWN:
     GNUNET_STATISTICS_update (GSC_stats,
                              gettext_noop ("# PONG messages dropped (connection down)"), 1,
                              GNUNET_NO);
     return;
-  case KX_STATE_KEY_SENT:
+  case GNUNET_CORE_KX_STATE_KEY_SENT:
     GNUNET_STATISTICS_update (GSC_stats,
                              gettext_noop ("# PONG messages dropped (out of order)"), 1,
                              GNUNET_NO);
     return;
-  case KX_STATE_KEY_RECEIVED:
+  case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
     break;
-  case KX_STATE_UP:
+  case GNUNET_CORE_KX_STATE_UP:
     break;
-  case KX_STATE_REKEY_SENT:
+  case GNUNET_CORE_KX_STATE_REKEY_SENT:
     break;
   default:
     GNUNET_break (0);
@@ -1148,45 +1209,48 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
                 "PONG", GNUNET_i2s (&t.target), (unsigned int) t.challenge);
     return;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received PONG from `%s'\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Received PONG from `%s'\n",
               GNUNET_i2s (&kx->peer));
   /* no need to resend key any longer */
-  if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
+  if (NULL != kx->retry_set_key_task)
   {
     GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
-    kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+    kx->retry_set_key_task = NULL;
   }
   switch (kx->status)
   {
-  case KX_STATE_DOWN:
+  case GNUNET_CORE_KX_STATE_DOWN:
     GNUNET_assert (0);           /* should be impossible */
     return;
-  case KX_STATE_KEY_SENT:
+  case GNUNET_CORE_KX_STATE_KEY_SENT:
     GNUNET_assert (0);           /* should be impossible */
     return;
-  case KX_STATE_KEY_RECEIVED:
+  case GNUNET_CORE_KX_STATE_KEY_RECEIVED:
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop
                               ("# session keys confirmed via PONG"), 1,
                               GNUNET_NO);
-    kx->status = KX_STATE_UP;
+    kx->status = GNUNET_CORE_KX_STATE_UP;
+    monitor_notify_all (kx);
     GSC_SESSIONS_create (&kx->peer, kx);
-    GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == kx->keep_alive_task);
+    GNUNET_assert (NULL == kx->keep_alive_task);
     update_timeout (kx);
     break;
-  case KX_STATE_UP:
+  case GNUNET_CORE_KX_STATE_UP:
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop
                               ("# timeouts prevented via PONG"), 1,
                               GNUNET_NO);
     update_timeout (kx);
     break;
-  case KX_STATE_REKEY_SENT:
+  case GNUNET_CORE_KX_STATE_REKEY_SENT:
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop
                               ("# rekey operations confirmed via PONG"), 1,
                               GNUNET_NO);
-    kx->status = KX_STATE_UP;
+    kx->status = GNUNET_CORE_KX_STATE_UP;
+    monitor_notify_all (kx);
     update_timeout (kx);
     break;
   default:
@@ -1204,11 +1268,11 @@ GSC_KX_handle_pong (struct GSC_KeyExchangeInfo *kx,
 static void
 send_key (struct GSC_KeyExchangeInfo *kx)
 {
-  GNUNET_assert (KX_STATE_DOWN != kx->status);
-  if (GNUNET_SCHEDULER_NO_TASK != kx->retry_set_key_task)
+  GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
+  if (NULL != kx->retry_set_key_task)
   {
      GNUNET_SCHEDULER_cancel (kx->retry_set_key_task);
-     kx->retry_set_key_task = GNUNET_SCHEDULER_NO_TASK;
+     kx->retry_set_key_task = NULL;
   }
   /* always update sender status in SET KEY message */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
@@ -1216,7 +1280,8 @@ send_key (struct GSC_KeyExchangeInfo *kx)
               GNUNET_i2s (&kx->peer),
              kx->status);
   current_ekm.sender_status = htonl ((int32_t) (kx->status));
-  GSC_NEIGHBOURS_transmit (&kx->peer, &current_ekm.header,
+  GSC_NEIGHBOURS_transmit (&kx->peer,
+                           &current_ekm.header,
                            kx->set_key_retry_frequency);
   kx->retry_set_key_task =
       GNUNET_SCHEDULER_add_delayed (kx->set_key_retry_frequency,
@@ -1229,11 +1294,12 @@ send_key (struct GSC_KeyExchangeInfo *kx)
  *
  * @param kx key exchange context
  * @param payload payload of the message
- * @param payload_size number of bytes in 'payload'
+ * @param payload_size number of bytes in @a payload
  */
 void
 GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
-                             const void *payload, size_t payload_size)
+                             const void *payload,
+                             size_t payload_size)
 {
   size_t used = payload_size + sizeof (struct EncryptedMessage);
   char pbuf[used];              /* plaintext */
@@ -1244,10 +1310,8 @@ GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
   struct GNUNET_CRYPTO_AuthKey auth_key;
 
   ph = (struct EncryptedMessage *) pbuf;
-  ph->iv_seed =
-      htonl (GNUNET_CRYPTO_random_u32
-             (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX));
   ph->sequence_number = htonl (++kx->last_sequence_number_sent);
+  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);
@@ -1268,7 +1332,8 @@ GSC_KX_encrypt_and_transmit (struct GSC_KeyExchangeInfo *kx,
                   ph->iv_seed);
   GNUNET_CRYPTO_hmac (&auth_key, &em->sequence_number,
                       used - ENCRYPTED_HEADER_SIZE, &em->hmac);
-  GSC_NEIGHBOURS_transmit (&kx->peer, &em->header,
+  GSC_NEIGHBOURS_transmit (&kx->peer,
+                           &em->header,
                            GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
@@ -1320,7 +1385,7 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
     return;
   }
   m = (const struct EncryptedMessage *) msg;
-  if (KX_STATE_UP != kx->status)
+  if (GNUNET_CORE_KX_STATE_UP != kx->status)
   {
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop
@@ -1337,12 +1402,13 @@ GSC_KX_handle_encrypted_message (struct GSC_KeyExchangeInfo *kx,
                               gettext_noop ("# sessions terminated by key expiration"),
                               1, GNUNET_NO);
     GSC_SESSIONS_end (&kx->peer);
-    if (GNUNET_SCHEDULER_NO_TASK != kx->keep_alive_task)
+    if (NULL != kx->keep_alive_task)
     {
       GNUNET_SCHEDULER_cancel (kx->keep_alive_task);
-      kx->keep_alive_task = GNUNET_SCHEDULER_NO_TASK;
+      kx->keep_alive_task = NULL;
     }
-    kx->status = KX_STATE_KEY_SENT;
+    kx->status = GNUNET_CORE_KX_STATE_KEY_SENT;
+    monitor_notify_all (kx);
     send_key (kx);
     return;
   }
@@ -1472,7 +1538,7 @@ deliver_message (void *cls,
 {
   struct DeliverMessageContext *dmc = client;
 
-  if (KX_STATE_UP != dmc->kx->status)
+  if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
   {
     GNUNET_STATISTICS_update (GSC_stats,
                               gettext_noop
@@ -1486,6 +1552,9 @@ deliver_message (void *cls,
   case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP:
     GSC_SESSIONS_set_typemap (dmc->peer, m);
     return GNUNET_OK;
+  case GNUNET_MESSAGE_TYPE_CORE_CONFIRM_TYPE_MAP:
+    GSC_SESSIONS_confirm_typemap (dmc->peer, m);
+    return GNUNET_OK;
   default:
     GSC_CLIENTS_deliver_message (dmc->peer, m,
                                  ntohs (m->size),
@@ -1559,15 +1628,18 @@ do_rekey (void *cls,
   sign_ephemeral_key ();
   for (pos = kx_head; NULL != pos; pos = pos->next)
   {
-    if (KX_STATE_UP == pos->status)
+    if (GNUNET_CORE_KX_STATE_UP == pos->status)
     {
-      pos->status = KX_STATE_REKEY_SENT;
+      pos->status = GNUNET_CORE_KX_STATE_REKEY_SENT;
+      monitor_notify_all (pos);
       derive_session_keys (pos);
     }
-    if (KX_STATE_DOWN == pos->status)
+    if (GNUNET_CORE_KX_STATE_DOWN == pos->status)
     {
-      pos->status = KX_STATE_KEY_SENT;
+      pos->status = GNUNET_CORE_KX_STATE_KEY_SENT;
+      monitor_notify_all (pos);
     }
+    monitor_notify_all (pos);
     send_key (pos);
   }
 }
@@ -1577,11 +1649,15 @@ do_rekey (void *cls,
  * Initialize KX subsystem.
  *
  * @param pk private key to use for the peer
+ * @param server the server of the CORE service
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  */
 int
-GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
+GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk,
+             struct GNUNET_SERVER_Handle *server)
 {
+  nc = GNUNET_SERVER_notification_context_create (server,
+                                                  1);
   my_private_key = pk;
   GNUNET_CRYPTO_eddsa_key_get_public (my_private_key,
                                                  &GSC_my_identity.public_key);
@@ -1608,10 +1684,10 @@ GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
 void
 GSC_KX_done ()
 {
-  if (GNUNET_SCHEDULER_NO_TASK != rekey_task)
+  if (NULL != rekey_task)
   {
     GNUNET_SCHEDULER_cancel (rekey_task);
-    rekey_task = GNUNET_SCHEDULER_NO_TASK;
+    rekey_task = NULL;
   }
   if (NULL != my_ephemeral_key)
   {
@@ -1628,6 +1704,47 @@ GSC_KX_done ()
     GNUNET_SERVER_mst_destroy (mst);
     mst = NULL;
   }
+  if (NULL != nc)
+  {
+    GNUNET_SERVER_notification_context_destroy (nc);
+    nc = NULL;
+  }
+}
+
+
+/**
+ * Handle #GNUNET_MESSAGE_TYPE_CORE_MONITOR_PEERS request.  For this
+ * request type, the client does not have to have transmitted an INIT
+ * request.  All current peers are returned, regardless of which
+ * message types they accept.
+ *
+ * @param cls unused
+ * @param client client sending the iteration request
+ * @param message iteration request message
+ */
+void
+GSC_KX_handle_client_monitor_peers (void *cls,
+                                    struct GNUNET_SERVER_Client *client,
+                                    const struct GNUNET_MessageHeader *message)
+{
+  struct MonitorNotifyMessage done_msg;
+  struct GSC_KeyExchangeInfo *kx;
+
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+  GNUNET_SERVER_notification_context_add (nc,
+                                          client);
+  for (kx = kx_head; NULL != kx; kx = kx->next)
+    monitor_notify (client, kx);
+  done_msg.header.size = htons (sizeof (struct MonitorNotifyMessage));
+  done_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
+  done_msg.state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
+  memset (&done_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity));
+  done_msg.timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
+  GNUNET_SERVER_notification_context_unicast (nc,
+                                              client,
+                                              &done_msg.header,
+                                              GNUNET_NO);
 }
 
+
 /* end of gnunet-service-core_kx.c */