Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / core / gnunet-service-core_kx.c
index b2949a68883cd69bb0f163edaab7d535050f01d0..9068985123fd1723489f5b102400e5177ac82ee0 100644 (file)
@@ -26,7 +26,6 @@
 #include "platform.h"
 #include "gnunet-service-core_kx.h"
 #include "gnunet-service-core.h"
-#include "gnunet-service-core_clients.h"
 #include "gnunet-service-core_sessions.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_transport_core_service.h"
@@ -262,7 +261,7 @@ struct GSC_KeyExchangeInfo
    * Message queue for sending messages to @a peer.
    */
   struct GNUNET_MQ_Handle *mq;
-  
+
   /**
    * PING message we transmit to the other peer.
    */
@@ -320,7 +319,7 @@ struct GSC_KeyExchangeInfo
    * last were received (good for accepting out-of-order packets and
    * estimating reliability of the connection)
    */
-  unsigned int last_packets_bitmap;
+  uint32_t last_packets_bitmap;
 
   /**
    * last sequence number received on this connection (highest)
@@ -392,34 +391,9 @@ static struct GSC_KeyExchangeInfo *kx_tail;
 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
+ * Notification context for broadcasting to monitors.
  */
-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);
-}
+static struct GNUNET_NotificationContext *nc;
 
 
 /**
@@ -453,9 +427,9 @@ monitor_notify_all (struct GSC_KeyExchangeInfo *kx)
   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);
+  GNUNET_notification_context_broadcast (nc,
+                                        &msg.header,
+                                        GNUNET_NO);
   kx->last_notify_timeout = kx->timeout;
 }
 
@@ -803,7 +777,7 @@ handle_transport_notify_disconnect (void *cls,
                                    void *handler_cls)
 {
   struct GSC_KeyExchangeInfo *kx = handler_cls;
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer `%s' disconnected from us.\n",
               GNUNET_i2s (peer));
@@ -840,7 +814,7 @@ static void
 send_ping (struct GSC_KeyExchangeInfo *kx)
 {
   struct GNUNET_MQ_Envelope *env;
-  
+
   GNUNET_STATISTICS_update (GSC_stats,
                             gettext_noop ("# PING messages transmitted"),
                             1,
@@ -1358,7 +1332,7 @@ static void
 send_key (struct GSC_KeyExchangeInfo *kx)
 {
   struct GNUNET_MQ_Envelope *env;
-  
+
   GNUNET_assert (GNUNET_CORE_KX_STATE_DOWN != kx->status);
   if (NULL != kx->retry_set_key_task)
   {
@@ -1546,7 +1520,7 @@ handle_encrypted (void *cls,
                    sizeof (struct GNUNET_HashCode)))
   {
     /* checksum failed */
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                "Failed checksum validation for a message from `%s'\n",
                GNUNET_i2s (kx->peer));
     return;
@@ -1562,7 +1536,10 @@ handle_encrypted (void *cls,
                   &m->sequence_number,
                   &buf[ENCRYPTED_HEADER_SIZE],
                   size - ENCRYPTED_HEADER_SIZE))
+  {
+    GNUNET_break_op (0);
     return;
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Decrypted %u bytes from %s\n",
               (unsigned int) (size - ENCRYPTED_HEADER_SIZE),
@@ -1596,7 +1573,7 @@ handle_encrypted (void *cls,
   }
   if (kx->last_sequence_number_received > snum)
   {
-    unsigned int rotbit = 1 << (kx->last_sequence_number_received - snum - 1);
+    uint32_t rotbit = 1U << (kx->last_sequence_number_received - snum - 1);
 
     if ((kx->last_packets_bitmap & rotbit) != 0)
     {
@@ -1668,7 +1645,7 @@ handle_transport_notify_excess_bw (void *cls,
                                    const struct GNUNET_PeerIdentity *pid,
                                   void *connect_cls)
 {
-  struct GSC_KeyExchangeInfo *kx = connect_cls;  
+  struct GSC_KeyExchangeInfo *kx = connect_cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %s has excess bandwidth available\n",
@@ -1694,6 +1671,10 @@ deliver_message (void *cls,
 {
   struct DeliverMessageContext *dmc = client;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Decrypted message of type %d from %s\n",
+              ntohs (m->type),
+              GNUNET_i2s (dmc->peer));
   if (GNUNET_CORE_KX_STATE_UP != dmc->kx->status)
   {
     GNUNET_STATISTICS_update (GSC_stats,
@@ -1807,35 +1788,31 @@ 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,
-             struct GNUNET_SERVER_Handle *server)
+GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
 {
-  GNUNET_MQ_hd_fixed_size (ephemeral_key,
-                          GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY,
-                          struct EphemeralKeyMessage);
-  GNUNET_MQ_hd_fixed_size (ping,
-                          GNUNET_MESSAGE_TYPE_CORE_PING,
-                          struct PingMessage);
-  GNUNET_MQ_hd_fixed_size (pong,
-                          GNUNET_MESSAGE_TYPE_CORE_PONG,
-                          struct PongMessage);
-  GNUNET_MQ_hd_var_size (encrypted,
-                        GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE,
-                        struct EncryptedMessage);
   struct GNUNET_MQ_MessageHandler handlers[] = {
-    make_ephemeral_key_handler (NULL),
-    make_ping_handler (NULL),
-    make_pong_handler (NULL),
-    make_encrypted_handler (NULL),
+    GNUNET_MQ_hd_fixed_size (ephemeral_key,
+                             GNUNET_MESSAGE_TYPE_CORE_EPHEMERAL_KEY,
+                             struct EphemeralKeyMessage,
+                             NULL),
+    GNUNET_MQ_hd_fixed_size (ping,
+                             GNUNET_MESSAGE_TYPE_CORE_PING,
+                             struct PingMessage,
+                             NULL),
+    GNUNET_MQ_hd_fixed_size (pong,
+                             GNUNET_MESSAGE_TYPE_CORE_PONG,
+                             struct PongMessage,
+                             NULL),
+    GNUNET_MQ_hd_var_size (encrypted,
+                           GNUNET_MESSAGE_TYPE_CORE_ENCRYPTED_MESSAGE,
+                           struct EncryptedMessage,
+                           NULL),
     GNUNET_MQ_handler_end()
   };
 
-  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);
@@ -1848,11 +1825,13 @@ GSC_KX_init (struct GNUNET_CRYPTO_EddsaPrivateKey *pk,
     return GNUNET_SYSERR;
   }
   sign_ephemeral_key ();
+  nc = GNUNET_notification_context_create (1);
   rekey_task = GNUNET_SCHEDULER_add_delayed (REKEY_FREQUENCY,
                                              &do_rekey,
                                              NULL);
-  mst = GNUNET_SERVER_mst_create (&deliver_message, NULL);
-  transport 
+  mst = GNUNET_SERVER_mst_create (&deliver_message,
+                                 NULL);
+  transport
     = GNUNET_TRANSPORT_core_connect (GSC_cfg,
                                     &GSC_my_identity,
                                     handlers,
@@ -1902,7 +1881,7 @@ GSC_KX_done ()
   }
   if (NULL != nc)
   {
-    GNUNET_SERVER_notification_context_destroy (nc);
+    GNUNET_notification_context_destroy (nc);
     nc = NULL;
   }
 }
@@ -1940,34 +1919,36 @@ GSC_NEIGHBOURS_check_excess_bandwidth (const struct GSC_KeyExchangeInfo *kxinfo)
  * 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
+ * @param mq message queue to add for monitoring
  */
 void
-GSC_KX_handle_client_monitor_peers (void *cls,
-                                    struct GNUNET_SERVER_Client *client,
-                                    const struct GNUNET_MessageHeader *message)
+GSC_KX_handle_client_monitor_peers (struct GNUNET_MQ_Handle *mq)
 {
-  struct MonitorNotifyMessage done_msg;
+  struct GNUNET_MQ_Envelope *env;
+  struct MonitorNotifyMessage *done_msg;
   struct GSC_KeyExchangeInfo *kx;
 
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  GNUNET_SERVER_notification_context_add (nc,
-                                          client);
+  GNUNET_notification_context_add (nc,
+                                  mq);
   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);
+  {
+    struct GNUNET_MQ_Envelope *env;
+    struct MonitorNotifyMessage *msg;
+
+    env = GNUNET_MQ_msg (msg,
+                        GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
+    msg->state = htonl ((uint32_t) kx->status);
+    msg->peer = *kx->peer;
+    msg->timeout = GNUNET_TIME_absolute_hton (kx->timeout);
+    GNUNET_MQ_send (mq,
+                   env);
+  }
+  env = GNUNET_MQ_msg (done_msg,
+                      GNUNET_MESSAGE_TYPE_CORE_MONITOR_NOTIFY);
+  done_msg->state = htonl ((uint32_t) GNUNET_CORE_KX_ITERATION_FINISHED);
+  done_msg->timeout = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
+  GNUNET_MQ_send (mq,
+                 env);
 }