trying to make KX logic slightly more readable
[oweals/gnunet.git] / src / cadet / cadet_common.c
index 8ac85fe17f73c1b8354e9c01f4839184b2ff1493..95a3144e4dda0dc2fd2c431cf7a667ccd180fdf6 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012 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
@@ -47,15 +47,24 @@ GC_f2s (int fwd)
   else
   {
     /* Not an error, can happen with CONNECTION_BROKEN messages. */
-    return "";
+    return "\???";
   }
 }
 
+
+/**
+ * Test if @a bigger is larger than @a smaller.
+ * Considers the case that @a bigger just overflowed
+ * and is thus tiny while @a smaller is still below
+ * `UINT32_MAX`.
+ */
 int
-GC_is_pid_bigger (uint32_t bigger, uint32_t smaller)
+GC_is_pid_bigger (uint32_t bigger,
+                 uint32_t smaller)
 {
-    return (GNUNET_YES == PID_OVERFLOW (smaller, bigger) ||
-            (bigger > smaller && GNUNET_NO == PID_OVERFLOW (bigger, smaller)));
+    return (PID_OVERFLOW (smaller, bigger) ||
+            ( (bigger > smaller) &&
+             (! PID_OVERFLOW (bigger, smaller))) );
 }
 
 
@@ -77,25 +86,35 @@ GC_min_pid (uint32_t a, uint32_t b)
 }
 
 
-const struct GNUNET_HashCode *
-GC_h2hc (const struct GNUNET_CADET_Hash *id)
+/**
+ * Allocate a string with a hexdump of any binary data.
+ *
+ * @param bin Arbitrary binary data.
+ * @param len Length of @a bin in bytes.
+ * @param output Where to write the output (if *output be NULL it's allocated).
+ *
+ * @return The size of the output.
+ */
+size_t
+GC_bin2s (void *bin, unsigned int len, char **output)
 {
-  static struct GNUNET_HashCode hc;
-  memcpy (&hc, id, sizeof (*id));
-
-  return &hc;
-}
+  char *data = bin;
+  char *buf;
+  unsigned int s_len;
+  unsigned int i;
 
+  s_len = 2 * len + 1;
+  if (NULL == *output)
+    *output = GNUNET_malloc (s_len);
+  buf = *output;
 
-const char *
-GC_h2s (const struct GNUNET_CADET_Hash *id)
-{
-  static char s[53];
-
-  memcpy (s, GNUNET_h2s_full (GC_h2hc (id)), 52);
-  s[52] = '\0';
+  for (i = 0; i < len; i++)
+  {
+    SPRINTF (&buf[2 * i], "%2X", data[i]);
+  }
+  buf[s_len - 1] = '\0';
 
-  return s;
+  return s_len;
 }
 
 
@@ -114,7 +133,8 @@ GC_m2s (uint16_t m)
      * Used to mark the "payload" of a non-payload message.
      */
     case 0:
-      return "";
+      s = "retransmit";
+      break;
 
       /**
        * Request the creation of a path
@@ -126,7 +146,7 @@ GC_m2s (uint16_t m)
       /**
        * Request the modification of an existing path
        */
-    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_CREATE_ACK:
       s = "CONN_ACK";
       break;
 
@@ -140,50 +160,36 @@ GC_m2s (uint16_t m)
       /**
        * At some point, the route will spontaneously change
        */
-    case GNUNET_MESSAGE_TYPE_CADET_PATH_CHANGED:
+    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_PATH_CHANGED_UNIMPLEMENTED:
       s = "PATH_CHNGD";
       break;
 
       /**
        * Transport payload data.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_DATA:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA:
       s = "DATA";
       break;
 
     /**
      * Confirm receipt of payload data.
      */
-    case GNUNET_MESSAGE_TYPE_CADET_DATA_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_APP_DATA_ACK:
       s = "DATA_ACK";
       break;
 
       /**
-       * Key exchange encapsulation.
+       * Key exchange message.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_KX:
+    case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_KX:
       s = "KX";
       break;
 
       /**
-       * Axolotl key exchange message.
-       */
-    case GNUNET_MESSAGE_TYPE_CADET_AX_KX:
-      s = "AX_KX";
-      break;
-
-      /**
-       * New ephemeral key.
-       */
-    case GNUNET_MESSAGE_TYPE_CADET_KX_EPHEMERAL:
-      s = "KX_EPHM";
-      break;
-
-      /**
-       * Answer to session key challenge.
+       * Encrypted.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_KX_PONG:
-      s = "KX_PONG";
+    case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED:
+      s = "ENCRYPTED";
       break;
 
       /**
@@ -196,35 +202,42 @@ GC_m2s (uint16_t m)
       /**
        * ACK for a data packet.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CONNECTION_HOP_BY_HOP_ENCRYPTED_ACK:
       s = "ACK";
       break;
 
       /**
        * POLL for ACK.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_POLL:
+    case GNUNET_MESSAGE_TYPE_CADET_TUNNEL_ENCRYPTED_POLL:
       s = "POLL";
       break;
 
       /**
        * Announce origin is still alive.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_KEEPALIVE:
       s = "KEEPALIVE";
       break;
 
-    /**
-       * Connect to the cadet service, specifying subscriptions
+      /**
+       * Open port
        */
-    case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT:
-      s = "LOC_CONNCT";
+    case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN:
+      s = "OPEN_PORT";
+      break;
+
+      /**
+       * Close port
+       */
+    case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE:
+      s = "CLOSE_PORT";
       break;
 
       /**
        * Ask the cadet service to create a new tunnel
        */
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN:
       s = "CHAN_CREAT";
       break;
 
@@ -238,31 +251,17 @@ GC_m2s (uint16_t m)
       /**
        * Confirm the creation of a channel.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_ACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_ACK:
       s = "CHAN_ACK";
       break;
 
       /**
        * Confirm the creation of a channel.
        */
-    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK:
+    case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_OPEN_NACK_DEPRECATED:
       s = "CHAN_NACK";
       break;
 
-      /**
-       * OTR encrypted payload.
-       */
-    case GNUNET_MESSAGE_TYPE_CADET_ENCRYPTED:
-      s = "OTR";
-      break;
-
-      /**
-       * Axolotl encrypted payload.
-       */
-    case GNUNET_MESSAGE_TYPE_CADET_AX:
-      s = "AX";
-      break;
-
       /**
        * Local payload traffic
        */
@@ -347,8 +346,16 @@ GC_m2s (uint16_t m)
       s = "INFO_DUMP";
       break;
 
+      /**
+       * Used to mark the "payload" of a non-payload message.
+       */
+    case UINT16_MAX:
+      s = "      N/A";
+      break;
+
+
     default:
-      SPRINTF (buf[idx], "%u (UNKNOWN)", m);
+      SPRINTF (buf[idx], "{UNK: %5u}", m);
       return buf[idx];
   }
   SPRINTF (buf[idx], "{%10s}", s);