converting conversation to use new CADET API
[oweals/gnunet.git] / src / cadet / gnunet-service-cadet_tunnel.h
index 382fcdf339f8f177d42081a12929b817e67d42c9..1b56a06326ad3c9b0e43fde17784b74b2d1a9b60 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 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.
 */
 
 /**
@@ -40,29 +40,31 @@ extern "C"
 #include "platform.h"
 #include "gnunet_util_lib.h"
 
+#define CONNECTIONS_PER_TUNNEL 3
+
 /**
  * All the connectivity states a tunnel can be in.
  */
 enum CadetTunnelCState
 {
-    /**
-     * Uninitialized status, should never appear in operation.
-     */
+  /**
+   * Uninitialized status, should never appear in operation.
+   */
   CADET_TUNNEL_NEW,
 
-    /**
-     * No path to the peer known yet.
-     */
+  /**
+   * No path to the peer known yet.
+   */
   CADET_TUNNEL_SEARCHING,
 
-    /**
-     * Request sent, not yet answered.
-     */
+  /**
+   * Request sent, not yet answered.
+   */
   CADET_TUNNEL_WAITING,
 
-    /**
-     * Peer connected and ready to accept data.
-     */
+  /**
+   * Peer connected and ready to accept data.
+   */
   CADET_TUNNEL_READY,
 
   /**
@@ -85,15 +87,24 @@ enum CadetTunnelEState
   /**
    * Ephemeral key sent, waiting for peer's key.
    */
-  CADET_TUNNEL_KEY_SENT,
+  CADET_TUNNEL_KEY_AX_SENT,
 
   /**
-   * New ephemeral key and ping sent, waiting for pong.
+   * In OTR: New ephemeral key and ping sent, waiting for pong.
+   *
    * This means that we DO have the peer's ephemeral key, otherwise the
    * state would be KEY_SENT. We DO NOT have a valid session key (either no
    * previous key or previous key expired).
+   *
+   *
+   * In Axolotl: Key sent and received but no deciphered traffic yet.
+   *
+   * This means that we can send traffic (otherwise we would never complete
+   * the handshake), but we don't have complete confirmation. Since the first
+   * traffic MUST be a complete channel creation 3-way handshake, no payload
+   * will be sent before confirmation.
    */
-  CADET_TUNNEL_KEY_PING,
+  CADET_TUNNEL_KEY_AX_AUTH_SENT,
 
   /**
    * Handshake completed: session key available.
@@ -101,11 +112,11 @@ enum CadetTunnelEState
   CADET_TUNNEL_KEY_OK,
 
   /**
-   * New ephemeral key and ping sent, waiting for pong. Opposite to KEY_PING,
+   * New ephemeral key and ping sent, waiting for pong. Unlike KEY_PING,
    * we still have a valid session key and therefore we *can* still send
    * traffic on the tunnel.
    */
-  CADET_TUNNEL_KEY_REKEY,
+  CADET_TUNNEL_KEY_REKEY
 };
 
 /**
@@ -131,13 +142,18 @@ struct CadetTunnelQueue;
  * @param type Type of message sent.
  * @param size Size of the message.
  */
-typedef void (*GCT_sent) (void *cls,
-                          struct CadetTunnel *t,
-                          struct CadetTunnelQueue *q,
-                          uint16_t type, size_t size);
+typedef void
+(*GCT_sent) (void *cls,
+            struct CadetTunnel *t,
+            struct CadetTunnelQueue *q,
+            uint16_t type, size_t size);
+
+typedef void
+(*GCT_conn_iter) (void *cls, struct CadetConnection *c);
+
 
-typedef void (*GCT_conn_iter) (void *cls, struct CadetConnection *c);
-typedef void (*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
+typedef void
+(*GCT_chan_iter) (void *cls, struct CadetChannel *ch);
 
 
 /******************************************************************************/
@@ -154,12 +170,14 @@ void
 GCT_init (const struct GNUNET_CONFIGURATION_Handle *c,
           const struct GNUNET_CRYPTO_EddsaPrivateKey *key);
 
+
 /**
  * Shut down the tunnel subsystem.
  */
 void
 GCT_shutdown (void);
 
+
 /**
  * Create a tunnel.
  *
@@ -168,6 +186,7 @@ GCT_shutdown (void);
 struct CadetTunnel *
 GCT_new (struct CadetPeer *destination);
 
+
 /**
  * Tunnel is empty: destroy it.
  *
@@ -178,6 +197,7 @@ GCT_new (struct CadetPeer *destination);
 void
 GCT_destroy_empty (struct CadetTunnel *t);
 
+
 /**
  * Destroy tunnel if empty (no more channels).
  *
@@ -186,6 +206,7 @@ GCT_destroy_empty (struct CadetTunnel *t);
 void
 GCT_destroy_if_empty (struct CadetTunnel *t);
 
+
 /**
  * Destroy the tunnel.
  *
@@ -220,6 +241,7 @@ GCT_change_cstate (struct CadetTunnel* t, enum CadetTunnelCState cstate);
 void
 GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
 
+
 /**
  * Add a connection to a tunnel.
  *
@@ -229,14 +251,6 @@ GCT_change_estate (struct CadetTunnel* t, enum CadetTunnelEState state);
 void
 GCT_add_connection (struct CadetTunnel *t, struct CadetConnection *c);
 
-/**
- * Mark a path as no longer valid for this tunnel: has been tried and failed.
- *
- * @param t Tunnel to update.
- * @param path Invalid path to remove. Is destroyed after removal.
- */
-void
-GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path);
 
 /**
  * Remove a connection from a tunnel.
@@ -247,6 +261,7 @@ GCT_remove_path (struct CadetTunnel *t, struct CadetPeerPath *path);
 void
 GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
 
+
 /**
  * Add a channel to a tunnel.
  *
@@ -256,6 +271,7 @@ GCT_remove_connection (struct CadetTunnel *t, struct CadetConnection *c);
 void
 GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
 
+
 /**
  * Remove a channel from a tunnel.
  *
@@ -265,38 +281,42 @@ GCT_add_channel (struct CadetTunnel *t, struct CadetChannel *ch);
 void
 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, CADET_ChannelNumber chid);
+GCT_get_channel (struct CadetTunnel *t, struct GNUNET_CADET_ChannelTunnelNumber ctn);
+
 
 /**
- * Decrypt and demultiplex by message type. Call appropriate handler
- * for a message
- * towards a channel of a local tunnel.
+ * Decrypt and process an encrypted message.
+ *
+ * Calls the appropriate handler for a message in a channel of a local tunnel.
  *
  * @param t Tunnel this message came on.
  * @param msg Message header.
  */
 void
 GCT_handle_encrypted (struct CadetTunnel *t,
-                      const struct GNUNET_CADET_Encrypted *msg);
+                      const struct GNUNET_CADET_TunnelEncryptedMessage *msg);
+
 
 /**
- * Demultiplex an encapsulated KX message by message type.
+ * Handle a Key eXchange message.
  *
  * @param t Tunnel on which the message came.
- * @param message KX message itself.
+ * @param msg KX message itself.
  */
 void
 GCT_handle_kx (struct CadetTunnel *t,
-               const struct GNUNET_MessageHeader *message);
+               const struct GNUNET_CADET_TunnelKeyExchangeMessage *msg);
+
 
 /**
  * @brief Use the given path for the tunnel.
@@ -311,6 +331,7 @@ GCT_handle_kx (struct CadetTunnel *t,
 struct CadetConnection *
 GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
 
+
 /**
  * Count all created connections of a tunnel. Not necessarily ready connections!
  *
@@ -321,6 +342,7 @@ GCT_use_path (struct CadetTunnel *t, struct CadetPeerPath *p);
 unsigned int
 GCT_count_any_connections (struct CadetTunnel *t);
 
+
 /**
  * Count established (ready) connections of a tunnel.
  *
@@ -331,6 +353,7 @@ GCT_count_any_connections (struct CadetTunnel *t);
 unsigned int
 GCT_count_connections (struct CadetTunnel *t);
 
+
 /**
  * Count channels of a tunnel.
  *
@@ -341,6 +364,7 @@ GCT_count_connections (struct CadetTunnel *t);
 unsigned int
 GCT_count_channels (struct CadetTunnel *t);
 
+
 /**
  * Get the connectivity state of a tunnel.
  *
@@ -351,6 +375,7 @@ GCT_count_channels (struct CadetTunnel *t);
 enum CadetTunnelCState
 GCT_get_cstate (struct CadetTunnel *t);
 
+
 /**
  * Get the encryption state of a tunnel.
  *
@@ -361,6 +386,7 @@ GCT_get_cstate (struct CadetTunnel *t);
 enum CadetTunnelEState
 GCT_get_estate (struct CadetTunnel *t);
 
+
 /**
  * Get the maximum buffer space for a tunnel towards a local client.
  *
@@ -371,6 +397,7 @@ GCT_get_estate (struct CadetTunnel *t);
 unsigned int
 GCT_get_channels_buffer (struct CadetTunnel *t);
 
+
 /**
  * Get the total buffer space for a tunnel for P2P traffic.
  *
@@ -381,6 +408,7 @@ GCT_get_channels_buffer (struct CadetTunnel *t);
 unsigned int
 GCT_get_connections_buffer (struct CadetTunnel *t);
 
+
 /**
  * Get the tunnel's destination.
  *
@@ -391,6 +419,7 @@ GCT_get_connections_buffer (struct CadetTunnel *t);
 const struct GNUNET_PeerIdentity *
 GCT_get_destination (struct CadetTunnel *t);
 
+
 /**
  * Get the tunnel's next free Channel ID.
  *
@@ -398,8 +427,9 @@ GCT_get_destination (struct CadetTunnel *t);
  *
  * @return ID of a channel free to use.
  */
-CADET_ChannelNumber
-GCT_get_next_chid (struct CadetTunnel *t);
+struct GNUNET_CADET_ChannelTunnelNumber
+GCT_get_next_ctn (struct CadetTunnel *t);
+
 
 /**
  * Send ACK on one or more channels due to buffer in connections.
@@ -409,6 +439,7 @@ GCT_get_next_chid (struct CadetTunnel *t);
 void
 GCT_unchoke_channels (struct CadetTunnel *t);
 
+
 /**
  * Send ACK on one or more connections due to buffer space to the client.
  *
@@ -419,6 +450,7 @@ GCT_unchoke_channels (struct CadetTunnel *t);
 void
 GCT_send_connection_acks (struct CadetTunnel *t);
 
+
 /**
  * Cancel a previously sent message while it's in the queue.
  *
@@ -431,6 +463,18 @@ GCT_send_connection_acks (struct CadetTunnel *t);
 void
 GCT_cancel (struct CadetTunnelQueue *q);
 
+
+/**
+ * Check if the tunnel has queued traffic.
+ *
+ * @param t Tunnel to check.
+ *
+ * @return #GNUNET_YES if there is queued traffic
+ *         #GNUNET_NO otherwise
+ */
+int
+GCT_has_queued_traffic (struct CadetTunnel *t);
+
 /**
  * Sends an already built message on a tunnel, encrypting it and
  * choosing the best connection.
@@ -449,16 +493,16 @@ GCT_send_prebuilt_message (const struct GNUNET_MessageHeader *message,
                            struct CadetTunnel *t, struct CadetConnection *c,
                            int force, GCT_sent cont, void *cont_cls);
 
+
 /**
- * Sends an already built and encrypted message on a tunnel, choosing the best
- * connection. Useful for re-queueing messages queued on a destroyed connection.
+ * Send a KX message.
  *
- * @param message Message to send. Function modifies it.
- * @param t Tunnel on which this message is transmitted.
+ * @param t Tunnel on which to send it.
+ * @param force_reply Force the other peer to reply with a KX message.
  */
 void
-GCT_resend_message (const struct GNUNET_MessageHeader *message,
-                    struct CadetTunnel *t);
+GCT_send_kx (struct CadetTunnel *t, int force_reply);
+
 
 /**
  * Is the tunnel directed towards the local peer?
@@ -470,6 +514,7 @@ GCT_resend_message (const struct GNUNET_MessageHeader *message,
 int
 GCT_is_loopback (const struct CadetTunnel *t);
 
+
 /**
  * Is the tunnel using this path already?
  *
@@ -481,6 +526,7 @@ GCT_is_loopback (const struct CadetTunnel *t);
 int
 GCT_is_path_used (const struct CadetTunnel *t, const struct CadetPeerPath *p);
 
+
 /**
  * Get a cost of a path for a tunnel considering existing connections.
  *
@@ -493,6 +539,7 @@ unsigned int
 GCT_get_path_cost (const struct CadetTunnel *t,
                    const struct CadetPeerPath *path);
 
+
 /**
  * Get the static string for the peer this tunnel is directed.
  *
@@ -503,6 +550,7 @@ GCT_get_path_cost (const struct CadetTunnel *t,
 const char *
 GCT_2s (const struct CadetTunnel *t);
 
+
 /**
  * Log all possible info about the tunnel state.
  *
@@ -512,6 +560,7 @@ GCT_2s (const struct CadetTunnel *t);
 void
 GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
 
+
 /**
  * Iterate all tunnels.
  *
@@ -521,6 +570,7 @@ GCT_debug (const struct CadetTunnel *t, enum GNUNET_ErrorType level);
 void
 GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
 
+
 /**
  * Count all tunnels.
  *
@@ -529,6 +579,7 @@ GCT_iterate_all (GNUNET_CONTAINER_PeerMapIterator iter, void *cls);
 unsigned int
 GCT_count_all (void);
 
+
 /**
  * Iterate all connections of a tunnel.
  *
@@ -539,6 +590,7 @@ GCT_count_all (void);
 void
 GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
 
+
 /**
  * Iterate all channels of a tunnel.
  *
@@ -547,7 +599,10 @@ GCT_iterate_connections (struct CadetTunnel *t, GCT_conn_iter iter, void *cls);
  * @param cls Closure for @c iter.
  */
 void
-GCT_iterate_channels (struct CadetTunnel *t, GCT_chan_iter iter, void *cls);
+GCT_iterate_channels (struct CadetTunnel *t,
+                      GCT_chan_iter iter,
+                      void *cls);
+
 
 #if 0                           /* keep Emacsens' auto-indent happy */
 {