pass only CadetTunnelAxolotl if it suffices, preparation for having ambiguous KX...
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.c
index 5e0f5bf4f7f6c7284c018fcfcc09cec9f8e5bc31..3b21f4107fb0d0e2c84f5a87202487a39026c38b 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2013 GNUnet e.V.
+     Copyright (C) 2013, 2017 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
      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
      Boston, MA 02110-1301, USA.
 */
-
+/**
+ * @file cadet/gnunet-service-cadet_tunnel.c
+ * @brief logical links between CADET clients
+ * @author Bartlomiej Polot
+ */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-
 #include "gnunet_signatures.h"
 #include "gnunet_statistics_service.h"
-
 #include "cadet_protocol.h"
 #include "cadet_path.h"
-
 #include "gnunet-service-cadet_tunnel.h"
 #include "gnunet-service-cadet_connection.h"
 #include "gnunet-service-cadet_channel.h"
@@ -310,7 +311,7 @@ struct CadetTunnel
   /**
    * Channel ID for the next created channel.
    */
-  struct GNUNET_CADET_ChannelNumber next_chid;
+  struct GNUNET_CADET_ChannelTunnelNumber next_ctn;
 
   /**
    * Destroy flag: if true, destroy on last message.
@@ -843,7 +844,7 @@ t_ax_decrypt (struct CadetTunnel *t, void *dst, const void *src, size_t size)
  * @param msg Message whose header to encrypt.
  */
 static void
-t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_ConnectionEncryptedMessage *msg)
+t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_TunnelEncryptedMessage *msg)
 {
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct CadetTunnelAxolotl *ax;
@@ -874,8 +875,8 @@ t_h_encrypt (struct CadetTunnel *t, struct GNUNET_CADET_ConnectionEncryptedMessa
  * @param dst Where to decrypt header to.
  */
 static void
-t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_ConnectionEncryptedMessage *src,
-             struct GNUNET_CADET_ConnectionEncryptedMessage *dst)
+t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_TunnelEncryptedMessage *src,
+             struct GNUNET_CADET_TunnelEncryptedMessage *dst)
 {
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
   struct CadetTunnelAxolotl *ax;
@@ -913,12 +914,12 @@ t_h_decrypt (struct CadetTunnel *t, const struct GNUNET_CADET_ConnectionEncrypte
  */
 static int
 try_old_ax_keys (struct CadetTunnel *t, void *dst,
-                 const struct GNUNET_CADET_ConnectionEncryptedMessage *src, size_t size)
+                 const struct GNUNET_CADET_TunnelEncryptedMessage *src, size_t size)
 {
   struct CadetTunnelSkippedKey *key;
   struct GNUNET_ShortHashCode *hmac;
   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
-  struct GNUNET_CADET_ConnectionEncryptedMessage plaintext_header;
+  struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
   struct GNUNET_CRYPTO_SymmetricSessionKey *valid_HK;
   size_t esize;
   size_t res;
@@ -927,7 +928,7 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
 
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Trying old keys\n");
   hmac = &plaintext_header.hmac;
-  esize = size - sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage);
+  esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
 
   /* Find a correct Header Key */
   for (key = t->ax->skipped_head; NULL != key; key = key->next)
@@ -948,8 +949,8 @@ try_old_ax_keys (struct CadetTunnel *t, void *dst,
     return -1;
 
   /* Should've been checked in -cadet_connection.c handle_cadet_encrypted. */
-  GNUNET_assert (size > sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage));
-  len = size - sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage);
+  GNUNET_assert (size > sizeof (struct GNUNET_CADET_TunnelEncryptedMessage));
+  len = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
   GNUNET_assert (len >= sizeof (struct GNUNET_MessageHeader));
 
   /* Decrypt header */
@@ -1092,19 +1093,19 @@ store_ax_keys (struct CadetTunnel *t,
  */
 static int
 t_ax_decrypt_and_validate (struct CadetTunnel *t, void *dst,
-                           const struct GNUNET_CADET_ConnectionEncryptedMessage *src,
+                           const struct GNUNET_CADET_TunnelEncryptedMessage *src,
                            size_t size)
 {
   struct CadetTunnelAxolotl *ax;
   struct GNUNET_ShortHashCode msg_hmac;
   struct GNUNET_HashCode hmac;
-  struct GNUNET_CADET_ConnectionEncryptedMessage plaintext_header;
+  struct GNUNET_CADET_TunnelEncryptedMessage plaintext_header;
   uint32_t Np;
   uint32_t PNp;
   size_t esize; /* Size of encryped payload */
   size_t osize; /* Size of output (decrypted payload) */
 
-  esize = size - sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage);
+  esize = size - sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
   ax = t->ax;
   if (NULL == ax)
     return -1;
@@ -1322,10 +1323,10 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                        struct CadetTunnelQueue *existing_q)
 {
   struct GNUNET_MessageHeader *msg;
-  struct GNUNET_CADET_ConnectionEncryptedMessage *ax_msg;
+  struct GNUNET_CADET_TunnelEncryptedMessage *ax_msg;
   struct CadetTunnelQueue *tq;
   size_t size = ntohs (message->size);
-  char cbuf[sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage) + size] GNUNET_ALIGN;
+  char cbuf[sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size] GNUNET_ALIGN;
   size_t esize;
   uint16_t type;
   int fwd;
@@ -1352,10 +1353,10 @@ send_prebuilt_message (const struct GNUNET_MessageHeader *message,
 
   GNUNET_assert (GNUNET_NO == GCT_is_loopback (t));
 
-  ax_msg = (struct GNUNET_CADET_ConnectionEncryptedMessage *) cbuf;
+  ax_msg = (struct GNUNET_CADET_TunnelEncryptedMessage *) cbuf;
   msg = &ax_msg->header;
-  msg->size = htons (sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage) + size);
-  msg->type = htons (GNUNET_MESSAGE_TYPE_CONNECTION_ENCRYPTED);
+  msg->size = htons (sizeof (struct GNUNET_CADET_TunnelEncryptedMessage) + size);
+  msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED);
   esize = t_ax_encrypt (t, &ax_msg[1], message, size);
   ax_msg->Ns = htonl (t->ax->Ns++);
   ax_msg->PNs = htonl (t->ax->PNs);
@@ -1556,13 +1557,13 @@ destroy_iterator (void *cls,
  */
 static void
 send_channel_destroy (struct CadetTunnel *t,
-                      struct GNUNET_CADET_ChannelNumber gid)
+                      struct GNUNET_CADET_ChannelTunnelNumber gid)
 {
   struct GNUNET_CADET_ChannelManageMessage msg;
 
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY);
   msg.header.size = htons (sizeof (msg));
-  msg.chid = gid;
+  msg.ctn = gid;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "WARNING destroying unknown channel %u on tunnel %s\n",
@@ -1584,7 +1585,7 @@ send_channel_destroy (struct CadetTunnel *t,
  */
 static void
 handle_data (struct CadetTunnel *t,
-             const struct GNUNET_CADET_ChannelDataMessage *msg,
+             const struct GNUNET_CADET_ChannelAppDataMessage *msg,
              int fwd)
 {
   struct CadetChannel *ch;
@@ -1595,7 +1596,7 @@ handle_data (struct CadetTunnel *t,
   /* Check size */
   size = ntohs (msg->header.size);
   if (size <
-      sizeof (struct GNUNET_CADET_ChannelDataMessage) +
+      sizeof (struct GNUNET_CADET_ChannelAppDataMessage) +
       sizeof (struct GNUNET_MessageHeader))
   {
     GNUNET_break (0);
@@ -1608,7 +1609,7 @@ handle_data (struct CadetTunnel *t,
 
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats,
@@ -1617,8 +1618,8 @@ handle_data (struct CadetTunnel *t,
                               GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "channel 0x%X unknown\n",
-         ntohl (msg->chid.cn));
-    send_channel_destroy (t, msg->chid);
+         ntohl (msg->ctn.cn));
+    send_channel_destroy (t, msg->ctn);
     return;
   }
 
@@ -1653,13 +1654,13 @@ handle_data_ack (struct CadetTunnel *t,
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats, "# data ack on unknown channel",
                               1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG, "WARNING channel %u unknown\n",
-         ntohl (msg->chid.cn));
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1675,21 +1676,21 @@ handle_data_ack (struct CadetTunnel *t,
  */
 static void
 handle_ch_create (struct CadetTunnel *t,
-                  const struct GNUNET_CADET_ChannelCreateMessage *msg)
+                  const struct GNUNET_CADET_ChannelOpenMessage *msg)
 {
   struct CadetChannel *ch;
   size_t size;
 
   /* Check size */
   size = ntohs (msg->header.size);
-  if (size != sizeof (struct GNUNET_CADET_ChannelCreateMessage))
+  if (size != sizeof (struct GNUNET_CADET_ChannelOpenMessage))
   {
     GNUNET_break_op (0);
     return;
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL != ch && ! GCT_is_loopback (t))
   {
     /* Probably a retransmission, safe to ignore */
@@ -1724,14 +1725,14 @@ handle_ch_nack (struct CadetTunnel *t,
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats, "# channel NACK on unknown channel",
                               1, GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "WARNING channel %u unknown\n",
-         ntohl (msg->chid.cn));
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1766,7 +1767,7 @@ handle_ch_ack (struct CadetTunnel *t,
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     GNUNET_STATISTICS_update (stats,
@@ -1775,7 +1776,7 @@ handle_ch_ack (struct CadetTunnel *t,
                               GNUNET_NO);
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "WARNING channel %u unknown\n",
-         ntohl (msg->chid.cn));
+         ntohl (msg->ctn.cn));
     return;
   }
 
@@ -1810,7 +1811,7 @@ handle_ch_destroy (struct CadetTunnel *t,
   }
 
   /* Check channel */
-  ch = GCT_get_channel (t, msg->chid);
+  ch = GCT_get_channel (t, msg->ctn);
   if (NULL == ch)
   {
     /* Probably a retransmission, safe to ignore */
@@ -1885,24 +1886,24 @@ handle_decrypted (struct CadetTunnel *t,
       GNUNET_STATISTICS_update (stats, "# keepalives received", 1, GNUNET_NO);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
       /* Don't send hop ACK, wait for client to ACK */
-      handle_data (t, (struct GNUNET_CADET_ChannelDataMessage *) msgh, fwd);
+      handle_data (t, (struct GNUNET_CADET_ChannelAppDataMessage *) msgh, fwd);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DATA_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
       handle_data_ack (t, (struct GNUNET_CADET_ChannelDataAckMessage *) msgh, fwd);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
-      handle_ch_create (t, (struct GNUNET_CADET_ChannelCreateMessage *) msgh);
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
+      handle_ch_create (t, (struct GNUNET_CADET_ChannelOpenMessage *) msgh);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_NACK_DEPRECATED:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
       handle_ch_nack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh);
       break;
 
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
       handle_ch_ack (t, (struct GNUNET_CADET_ChannelManageMessage *) msgh, fwd);
       break;
 
@@ -1934,7 +1935,7 @@ handle_decrypted (struct CadetTunnel *t,
  */
 void
 GCT_handle_encrypted (struct CadetTunnel *t,
-                      const struct GNUNET_CADET_ConnectionEncryptedMessage *msg)
+                      const struct GNUNET_CADET_TunnelEncryptedMessage *msg)
 {
   uint16_t size = ntohs (msg->header.size);
   char cbuf [size];
@@ -2146,8 +2147,8 @@ GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n");
 
   expected_overhead = 0;
-  expected_overhead += sizeof (struct GNUNET_CADET_ConnectionEncryptedMessage);
-  expected_overhead += sizeof (struct GNUNET_CADET_ChannelDataMessage);
+  expected_overhead += sizeof (struct GNUNET_CADET_TunnelEncryptedMessage);
+  expected_overhead += sizeof (struct GNUNET_CADET_ChannelAppDataMessage);
   expected_overhead += sizeof (struct GNUNET_CADET_ConnectionEncryptedAckMessage);
   GNUNET_assert (GNUNET_CONSTANTS_CADET_P2P_OVERHEAD == expected_overhead);
 
@@ -2203,7 +2204,7 @@ GCT_new (struct CadetPeer *destination)
   struct CadetTunnel *t;
 
   t = GNUNET_new (struct CadetTunnel);
-  t->next_chid.cn = 0;
+  t->next_ctn.cn = 0;
   t->peer = destination;
 
   if (GNUNET_OK !=
@@ -2509,13 +2510,13 @@ GCT_remove_channel (struct CadetTunnel *t, struct CadetChannel *ch)
  * Search for a channel by global ID.
  *
  * @param t Tunnel containing the channel.
- * @param chid Public channel number.
+ * @param ctn Public channel number.
  *
  * @return channel handler, NULL if doesn't exist
  */
 struct CadetChannel *
 GCT_get_channel (struct CadetTunnel *t,
-                 struct GNUNET_CADET_ChannelNumber chid)
+                 struct GNUNET_CADET_ChannelTunnelNumber ctn)
 {
   struct CadetTChannel *iter;
 
@@ -2524,7 +2525,7 @@ GCT_get_channel (struct CadetTunnel *t,
 
   for (iter = t->channel_head; NULL != iter; iter = iter->next)
   {
-    if (GCCH_get_id (iter->ch).cn == chid.cn)
+    if (GCCH_get_id (iter->ch).cn == ctn.cn)
       break;
   }
 
@@ -2971,11 +2972,11 @@ GCT_get_destination (struct CadetTunnel *t)
  *
  * @return GID of a channel free to use.
  */
-struct GNUNET_CADET_ChannelNumber
-GCT_get_next_chid (struct CadetTunnel *t)
+struct GNUNET_CADET_ChannelTunnelNumber
+GCT_get_next_ctn (struct CadetTunnel *t)
 {
-  struct GNUNET_CADET_ChannelNumber chid;
-  struct GNUNET_CADET_ChannelNumber mask;
+  struct GNUNET_CADET_ChannelTunnelNumber ctn;
+  struct GNUNET_CADET_ChannelTunnelNumber mask;
   int result;
 
   /* Set bit 30 depending on the ID relationship. Bit 31 is always 0 for GID.
@@ -2987,21 +2988,21 @@ GCT_get_next_chid (struct CadetTunnel *t)
     mask.cn = htonl (0x40000000);
   else
     mask.cn = 0x0;
-  t->next_chid.cn |= mask.cn;
+  t->next_ctn.cn |= mask.cn;
 
-  while (NULL != GCT_get_channel (t, t->next_chid))
+  while (NULL != GCT_get_channel (t, t->next_ctn))
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
          "Channel %u exists...\n",
-         t->next_chid.cn);
-    t->next_chid.cn = htonl ((ntohl (t->next_chid.cn) + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
-    t->next_chid.cn |= mask.cn;
+         t->next_ctn.cn);
+    t->next_ctn.cn = htonl ((ntohl (t->next_ctn.cn) + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI);
+    t->next_ctn.cn |= mask.cn;
   }
-  chid = t->next_chid;
-  t->next_chid.cn = (t->next_chid.cn + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
-  t->next_chid.cn |= mask.cn;
+  ctn = t->next_ctn;
+  t->next_ctn.cn = (t->next_ctn.cn + 1) & ~GNUNET_CADET_LOCAL_CHANNEL_ID_CLI;
+  t->next_ctn.cn |= mask.cn;
 
-  return chid;
+  return ctn;
 }