make some functions static, ensure shutdown tasks could be run repeatedly if 1st...
[oweals/gnunet.git] / src / cadet / gnunet-cadet.c
index 3ce87a8825fca3179934c508872a863a6a688df0..a9b02714bfc5fa97a22ad34dee666ebfa8de242e 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (C) 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012, 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
 
      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.
 */
 
 /**
  * @file cadet/gnunet-cadet.c
  * @brief Print information about cadet tunnels and peers.
  * @author Bartlomiej Polot
+ * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "cadet.h"
 
 
-/**
- * Option -m.
- */
-static int monitor_mode;
-
 /**
  * Option -P.
  */
@@ -65,29 +61,29 @@ static char *conn_id;
 static char *channel_id;
 
 /**
- * Port to listen on (-p).
+ * Port to listen on (-o).
  */
-static uint32_t listen_port;
+static char *listen_port;
 
 /**
  * Request echo service
  */
-int echo;
+static int echo;
 
 /**
  * Request a debug dump
  */
-int dump;
+static int dump;
 
 /**
  * Time of last echo request.
  */
-struct GNUNET_TIME_Absolute echo_time;
+static struct GNUNET_TIME_Absolute echo_time;
 
 /**
  * Task for next echo request.
  */
-struct GNUNET_SCHEDULER_Task * echo_task;
+static struct GNUNET_SCHEDULER_Task *echo_task;
 
 /**
  * Peer to connect to.
@@ -97,13 +93,7 @@ static char *target_id;
 /**
  * Port to connect to
  */
-static uint32_t target_port;
-
-/**
- * Data pending in netcat mode.
- */
-size_t data_size;
-
+static char *target_port = "default";
 
 /**
  * Cadet handle.
@@ -116,29 +106,98 @@ static struct GNUNET_CADET_Handle *mh;
 static struct GNUNET_CADET_Channel *ch;
 
 /**
- * Shutdown task handle.
+ * HashCode of the given port string
  */
-struct GNUNET_SCHEDULER_Task * sd;
+static struct GNUNET_HashCode porthash;
 
+/**
+ * Data structure for ongoing reception of incoming virtual circuits.
+ */
+struct GNUNET_CADET_Port *lp;
 
+/**
+ * Task for reading from stdin.
+ */
+static struct GNUNET_SCHEDULER_Task *rd_task;
 
+/**
+ * Task for main job.
+ */
+static struct GNUNET_SCHEDULER_Task *job;
+
+
+/**
+ * Wait for input on STDIO and send it out over the #ch.
+ */
 static void
 listen_stdio (void);
 
 
+/**
+ * Convert encryption status to human readable string.
+ *
+ * @param status Encryption status.
+ *
+ * @return Human readable string.
+ */
+static const char *
+enc_2s (uint16_t status)
+{
+  switch (status)
+  {
+    case 0:
+      return "NULL ";
+    case 1:
+      return "KSENT";
+    case 2:
+      return "KRECV";
+    case 3:
+      return "READY";
+    default:
+      return "";
+  }
+}
+
+
+/**
+ * Convert connection status to human readable string.
+ *
+ * @param status Connection status.
+ *
+ * @return Human readable string.
+ */
+static const char *
+conn_2s (uint16_t status)
+{
+  switch (status)
+  {
+    case 0:
+      return "NEW  ";
+    case 1:
+      return "SRCH ";
+    case 2:
+      return "WAIT ";
+    case 3:
+      return "READY";
+    case 4:
+      return "SHUTD";
+    default:
+      return "";
+  }
+}
+
+
 
 /**
- * Task run in monitor mode when the user presses CTRL-C to abort.
- * Stops monitoring activity.
+ * Task to shut down this application.
  *
  * @param cls Closure (unused).
- * @param tc scheduler context
  */
 static void
-shutdown_task (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Shutdown\n");
   if (NULL != ch)
   {
     GNUNET_CADET_channel_destroy (ch);
@@ -147,104 +206,88 @@ shutdown_task (void *cls,
   if (NULL != mh)
   {
     GNUNET_CADET_disconnect (mh);
-        mh = NULL;
+    mh = NULL;
   }
-}
-
-
-/**
- * Function called to notify a client about the connection
- * begin ready to queue more data.  "buf" will be
- * NULL and "size" zero if the connection was closed for
- * writing in the meantime.
- *
- * FIXME
- *
- * @param cls closure
- * @param size number of bytes available in buf
- * @param buf where the callee should write the message
- * @return number of bytes written to buf
- */
-size_t
-data_ready (void *cls, size_t size, void *buf)
-{
-  struct GNUNET_MessageHeader *msg;
-  size_t total_size;
-
-  if (NULL == buf || 0 == size)
+  if (NULL != rd_task)
   {
-    GNUNET_SCHEDULER_shutdown();
-    return 0;
+    GNUNET_SCHEDULER_cancel (rd_task);
+    rd_task = NULL;
   }
-
-  total_size = data_size + sizeof (struct GNUNET_MessageHeader);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending %u bytes\n", data_size);
-  GNUNET_assert (size >= total_size);
-
-  msg = buf;
-  msg->size = htons (total_size);
-  msg->type = htons (GNUNET_MESSAGE_TYPE_CADET_CLI);
-  memcpy (&msg[1], cls, data_size);
-  if (GNUNET_NO == echo)
+  if (NULL != echo_task)
   {
-    listen_stdio ();
+    GNUNET_SCHEDULER_cancel (echo_task);
+    echo_task = NULL;
   }
-  else
+  if (NULL != job)
   {
-    echo_time = GNUNET_TIME_absolute_get ();
+    GNUNET_SCHEDULER_cancel (job);
+    job = NULL;
   }
-
-  return total_size;
 }
 
 
 /**
- * Task run in monitor mode when the user presses CTRL-C to abort.
- * Stops monitoring activity.
+ * Task run in stdio mode, after some data is available at stdin.
  *
  * @param cls Closure (unused).
- * @param tc scheduler context
  */
 static void
-read_stdio (void *cls,
-            const struct GNUNET_SCHEDULER_TaskContext *tc)
+read_stdio (void *cls)
 {
-  static char buf[60000];
-
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    return;
-  }
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
+  char buf[60000];
+  ssize_t data_size;
 
-  data_size = read (0, buf, 60000);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
+  rd_task = NULL;
+  data_size = read (0,
+                    buf,
+                    60000);
   if (data_size < 1)
   {
     GNUNET_SCHEDULER_shutdown();
     return;
   }
-  GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
-                                     GNUNET_TIME_UNIT_FOREVER_REL,
-                                     data_size
-                                     + sizeof (struct GNUNET_MessageHeader),
-                                     &data_ready, buf);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Read %u bytes from stdio\n",
+              (unsigned int) data_size);
+  env = GNUNET_MQ_msg_extra (msg,
+                             data_size,
+                             GNUNET_MESSAGE_TYPE_CADET_CLI);
+  GNUNET_memcpy (&msg[1],
+                 buf,
+                 data_size);
+  GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
+                  env);
+  if (GNUNET_NO == echo)
+  {
+    listen_stdio ();
+  }
+  else
+  {
+    echo_time = GNUNET_TIME_absolute_get ();
+  }
 }
 
 
 /**
- * Start listening to stdin
+ * Wait for input on STDIO and send it out over the #ch.
  */
 static void
-listen_stdio (void)
+listen_stdio ()
 {
   struct GNUNET_NETWORK_FDSet *rs;
 
+  /* FIXME: why use 'rs' here, seems overly complicated... */
   rs = GNUNET_NETWORK_fdset_create ();
-  GNUNET_NETWORK_fdset_set_native (rs, 0);
-  GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                               GNUNET_TIME_UNIT_FOREVER_REL,
-                               rs, NULL,
-                               &read_stdio, NULL);
+  GNUNET_NETWORK_fdset_set_native (rs,
+                                   0); /* STDIN */
+  rd_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                                         GNUNET_TIME_UNIT_FOREVER_REL,
+                                         rs,
+                                         NULL,
+                                         &read_stdio,
+                                         NULL);
   GNUNET_NETWORK_fdset_destroy (rs);
 }
 
@@ -255,18 +298,16 @@ listen_stdio (void)
  *
  * It must NOT call #GNUNET_CADET_channel_destroy on the channel.
  *
- * @param cls closure (set from #GNUNET_CADET_connect)
+ * @param cls closure
  * @param channel connection to the other end (henceforth invalid)
- * @param channel_ctx place where local state associated
- *                   with the channel is stored
  */
 static void
 channel_ended (void *cls,
-               const struct GNUNET_CADET_Channel *channel,
-               void *channel_ctx)
+               const struct GNUNET_CADET_Channel *channel)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Channel ended!\n");
-  GNUNET_break (channel == ch);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Channel ended!\n");
+  GNUNET_assert (channel == ch);
   ch = NULL;
   GNUNET_SCHEDULER_shutdown ();
 }
@@ -278,63 +319,51 @@ channel_ended (void *cls,
  * Only called (once) upon reception of data with a message type which was
  * subscribed to in #GNUNET_CADET_connect.
  *
- * A call to #GNUNET_CADET_channel_destroy causes te channel to be ignored. In
- * this case the handler MUST return NULL.
+ * A call to #GNUNET_CADET_channel_destroy causes the channel to be ignored.
+ * In this case the handler MUST return NULL.
  *
  * @param cls closure
  * @param channel new handle to the channel
  * @param initiator peer that started the channel
- * @param port Port this channel is for.
- * @param options CadetOption flag field, with all active option bits set to 1.
- *
- * @return initial channel context for the channel
- *         (can be NULL -- that's not an error)
+ * @return initial channel context for the channel, we use @a channel
  */
 static void *
 channel_incoming (void *cls,
-                  struct GNUNET_CADET_Channel * channel,
-                  const struct GNUNET_PeerIdentity * initiator,
-                  uint32_t port, enum GNUNET_CADET_ChannelOption options)
+                  struct GNUNET_CADET_Channel *channel,
+                  const struct GNUNET_PeerIdentity *initiator)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Incoming channel %p on port %u\n",
-              channel, port);
-  if (NULL != ch)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "A channel already exists\n");
-    return NULL;
-  }
-  if (0 == listen_port)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not listening to channels\n");
-    return NULL;
-  }
+  GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+              "Incoming connection from %s\n",
+              GNUNET_i2s_full (initiator));
+  GNUNET_assert (NULL == ch);
+  GNUNET_assert (NULL != lp);
+  GNUNET_CADET_close_port (lp);
+  lp = NULL;
   ch = channel;
   if (GNUNET_NO == echo)
-  {
     listen_stdio ();
-    return NULL;
-  }
-  data_size = 0;
-  return NULL;
+  return channel;
 }
 
+
 /**
  * @brief Send an echo request to the remote peer.
  *
  * @param cls Closure (NULL).
- * @param tc Task context.
  */
 static void
-send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_echo (void *cls)
 {
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) || NULL == ch)
-    return;
+  struct GNUNET_MQ_Envelope *env;
+  struct GNUNET_MessageHeader *msg;
 
-  GNUNET_CADET_notify_transmit_ready (ch, GNUNET_NO,
-                                     GNUNET_TIME_UNIT_FOREVER_REL,
-                                     sizeof (struct GNUNET_MessageHeader),
-                                     &data_ready, NULL);
+  echo_task = NULL;
+  if (NULL == ch)
+    return;
+  env = GNUNET_MQ_msg (msg,
+                       GNUNET_MESSAGE_TYPE_CADET_CLI);
+  GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
+                  env);
 }
 
 
@@ -342,49 +371,28 @@ send_echo (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Call CADET's monitor API, request debug dump on the service.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-request_dump (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+request_dump (void *cls)
 {
   GNUNET_CADET_request_dump (mh);
-  GNUNET_SCHEDULER_cancel (sd);
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &shutdown_task, NULL);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
 /**
- * Call CADET's monitor API, get info of one connection.
+ * Check data message sanity. Does nothing so far (all messages are OK).
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
+ * @param message The message to check.
+ * @return #GNUNET_OK to keep the channel open,
+ *         #GNUNET_SYSERR to close it (signal serious error).
  */
-static void
-create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static int
+check_data (void *cls,
+            const struct GNUNET_MessageHeader *message)
 {
-  struct GNUNET_PeerIdentity pid;
-  enum GNUNET_CADET_ChannelOption opt;
-
-  GNUNET_assert (NULL == ch);
-
-  if (GNUNET_OK !=
-      GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
-                                                  strlen (target_id),
-                                                  &pid.public_key))
-  {
-    FPRINTF (stderr,
-             _("Invalid target `%s'\n"),
-             target_id);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
-  opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
-  ch = GNUNET_CADET_channel_create (mh, NULL, &pid, target_port, opt);
-  if (GNUNET_NO == echo)
-    listen_stdio ();
-  else
-    GNUNET_SCHEDULER_add_now (send_echo, NULL);
+  return GNUNET_OK; /* all is well-formed */
 }
 
 
@@ -395,35 +403,36 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * in order to receive the next message. This doesn't need to be immediate:
  * can be delayed if some processing is done on the message.
  *
- * @param cls Closure (set from #GNUNET_CADET_connect).
- * @param channel Connection to the other end.
- * @param channel_ctx Place to store local state associated with the channel.
+ * @param cls NULL
  * @param message The actual message.
- * @return #GNUNET_OK to keep the channel open,
- *         #GNUNET_SYSERR to close it (signal serious error).
  */
-static int
-data_callback (void *cls,
-               struct GNUNET_CADET_Channel *channel,
-               void **channel_ctx,
-               const struct GNUNET_MessageHeader *message)
+static void
+handle_data (void *cls,
+             const struct GNUNET_MessageHeader *message)
 {
+  size_t payload_size = ntohs (message->size) - sizeof (*message);
   uint16_t len;
   ssize_t done;
   uint16_t off;
   const char *buf;
-  GNUNET_break (ch == channel);
 
+  GNUNET_CADET_receive_done (ch);
   if (GNUNET_YES == echo)
   {
-    if (0 != listen_port)
+    if (NULL != listen_port)
     {
-      /* Just listening to echo incoming messages*/
-      GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
-                                        GNUNET_TIME_UNIT_FOREVER_REL,
-                                        sizeof (struct GNUNET_MessageHeader),
-                                        &data_ready, NULL);
-      return GNUNET_OK;
+      struct GNUNET_MQ_Envelope *env;
+      struct GNUNET_MessageHeader *msg;
+
+      env = GNUNET_MQ_msg_extra (msg,
+                                 payload_size,
+                                 GNUNET_MESSAGE_TYPE_CADET_CLI);
+      GNUNET_memcpy (&msg[1],
+                     &message[1],
+                     payload_size);
+      GNUNET_MQ_send (GNUNET_CADET_get_mq (ch),
+                      env);
+      return;
     }
     else
     {
@@ -431,30 +440,37 @@ data_callback (void *cls,
 
       latency = GNUNET_TIME_absolute_get_duration (echo_time);
       echo_time = GNUNET_TIME_UNIT_FOREVER_ABS;
-      FPRINTF (stdout, "time: %s\n",
-               GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_NO));
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  "time: %s\n",
+                  GNUNET_STRINGS_relative_time_to_string (latency,
+                                                          GNUNET_NO));
       echo_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
-                                                &send_echo, NULL);
+                                                &send_echo,
+                                                NULL);
     }
   }
 
   len = ntohs (message->size) - sizeof (*message);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Got %u bytes\n",
+              len);
   buf = (const char *) &message[1];
   off = 0;
   while (off < len)
   {
-    done = write (1, &buf[off], len - off);
+    done = write (1,
+                  &buf[off],
+                  len - off);
     if (done <= 0)
     {
       if (-1 == done)
         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                              "write");
-      return GNUNET_SYSERR;
+      GNUNET_SCHEDULER_shutdown ();
+      return;
     }
     off += done;
   }
-  return GNUNET_OK;
 }
 
 
@@ -472,21 +488,25 @@ data_callback (void *cls,
  *                  (0 = unknown, 1 = ourselves, 2 = neighbor)
  */
 static void
-peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
-                int tunnel, unsigned int n_paths, unsigned int best_path)
+peers_callback (void *cls,
+               const struct GNUNET_PeerIdentity *peer,
+                int tunnel,
+               unsigned int n_paths,
+               unsigned int best_path)
 {
   if (NULL == peer)
   {
-    if (GNUNET_YES != monitor_mode)
-    {
-      GNUNET_SCHEDULER_shutdown();
-    }
+    GNUNET_SCHEDULER_shutdown();
     return;
   }
-  FPRINTF (stdout, "%s tunnel: %c, paths: %u\n",
-           GNUNET_i2s_full (peer), tunnel ? 'Y' : 'N', n_paths);
+  FPRINTF (stdout,
+           "%s tunnel: %c, paths: %u\n",
+           GNUNET_i2s_full (peer),
+           tunnel ? 'Y' : 'N',
+           n_paths);
 }
 
+
 /**
  * Method called to retrieve information about a specific peer
  * known to the service.
@@ -500,14 +520,40 @@ peers_callback (void *cls, const struct GNUNET_PeerIdentity *peer,
  *              Each path starts with the local peer.
  *              Each path ends with the destination peer (given in @c peer).
  */
-void
+static void
 peer_callback (void *cls,
                const struct GNUNET_PeerIdentity *peer,
                int tunnel,
                int neighbor,
                unsigned int n_paths,
-               struct GNUNET_PeerIdentity *paths)
+               const struct GNUNET_PeerIdentity *paths)
 {
+  unsigned int i;
+  const struct GNUNET_PeerIdentity *p;
+
+  FPRINTF (stdout,
+           "%s [TUNNEL: %s, NEIGHBOR: %s, PATHS: %u]\n",
+           GNUNET_i2s_full (peer),
+           tunnel ? "Y" : "N",
+           neighbor ? "Y" : "N",
+           n_paths);
+  p = paths;
+  for (i = 0; i < n_paths && NULL != p;)
+  {
+    FPRINTF (stdout,
+             "%s ",
+             GNUNET_i2s (p));
+    if (0 == memcmp (p,
+                     peer,
+                     sizeof (*p)))
+    {
+      FPRINTF (stdout, "\n");
+      i++;
+    }
+    p++;
+  }
+
+  GNUNET_SCHEDULER_shutdown();
 }
 
 
@@ -521,7 +567,7 @@ peer_callback (void *cls,
  * @param estate Encryption state.
  * @param cstate Connectivity state.
  */
-void
+static void
 tunnels_callback (void *cls,
                   const struct GNUNET_PeerIdentity *peer,
                   unsigned int channels,
@@ -531,14 +577,16 @@ tunnels_callback (void *cls,
 {
   if (NULL == peer)
   {
-    if (GNUNET_YES != monitor_mode)
-    {
-      GNUNET_SCHEDULER_shutdown();
-    }
+    GNUNET_SCHEDULER_shutdown();
     return;
   }
-  FPRINTF (stdout, "%s [ENC: %u, CON: %u] CHs: %u, CONNs: %u\n",
-           GNUNET_i2s_full (peer), estate, cstate, channels, connections);
+  FPRINTF (stdout,
+           "%s [ENC: %s, CON: %s] CHs: %u, CONNs: %u\n",
+           GNUNET_i2s_full (peer),
+           enc_2s (estate),
+           conn_2s (cstate),
+           channels,
+           connections);
 }
 
 
@@ -555,13 +603,13 @@ tunnels_callback (void *cls,
  * @param estate Encryption status.
  * @param cstate Connectivity status.
  */
-void
+static void
 tunnel_callback (void *cls,
                  const struct GNUNET_PeerIdentity *peer,
                  unsigned int n_channels,
                  unsigned int n_connections,
-                 uint32_t *channels,
-                 struct GNUNET_CADET_Hash *connections,
+                 const struct GNUNET_CADET_ChannelTunnelNumber *channels,
+                 const struct GNUNET_CADET_ConnectionTunnelIdentifier *connections,
                  unsigned int estate,
                  unsigned int cstate)
 {
@@ -570,21 +618,16 @@ tunnel_callback (void *cls,
   if (NULL != peer)
   {
     FPRINTF (stdout, "Tunnel %s\n", GNUNET_i2s_full (peer));
-    FPRINTF (stdout, "%u channels\n", n_channels);
+    FPRINTF (stdout, "\t%u channels\n", n_channels);
     for (i = 0; i < n_channels; i++)
-      FPRINTF (stdout, "   %u\n", channels[i]);
-    FPRINTF (stdout, "%u connections\n", n_connections);
+      FPRINTF (stdout, "\t\t%X\n", ntohl (channels[i].cn));
+    FPRINTF (stdout, "\t%u connections\n", n_connections);
     for (i = 0; i < n_connections; i++)
-      FPRINTF (stdout, "   %s\n", GC_h2s (&connections[i]));
-    FPRINTF (stdout, "- enc state: %u\n", estate);
-    FPRINTF (stdout, "- con state: %u\n", cstate);
+      FPRINTF (stdout, "\t\t%s\n", GNUNET_sh2s (&connections[i].connection_of_tunnel));
+    FPRINTF (stdout, "\tencryption state: %s\n", enc_2s (estate));
+    FPRINTF (stdout, "\tconnection state: %s\n", conn_2s (cstate));
   }
-  if (GNUNET_YES != monitor_mode)
-  {
-    GNUNET_SCHEDULER_shutdown();
-  }
-  return;
-
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -592,16 +635,11 @@ tunnel_callback (void *cls,
  * Call CADET's meta API, get all peers known to a peer.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+get_peers (void *cls)
 {
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
-    return;
-  }
+  job = NULL;
   GNUNET_CADET_get_peers (mh, &peers_callback, NULL);
 }
 
@@ -610,18 +648,18 @@ get_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Call CADET's monitor API, get info of one peer.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+show_peer (void *cls)
 {
   struct GNUNET_PeerIdentity pid;
 
+  job = NULL;
   if (GNUNET_OK !=
-    GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
-                                                strlen (peer_id),
-                                                &pid.public_key))
-  {
+      GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id,
+                                                  strlen (peer_id),
+                                                  &pid.public_key))
+    {
     fprintf (stderr,
              _("Invalid peer ID `%s'\n"),
              peer_id);
@@ -631,20 +669,16 @@ show_peer (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_CADET_get_peer (mh, &pid, peer_callback, NULL);
 }
 
+
 /**
  * Call CADET's meta API, get all tunnels known to a peer.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+get_tunnels (void *cls)
 {
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown\n");
-    return;
-  }
+  job = NULL;
   GNUNET_CADET_get_tunnels (mh, &tunnels_callback, NULL);
 }
 
@@ -653,13 +687,13 @@ get_tunnels (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Call CADET's monitor API, get info of one tunnel.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+show_tunnel (void *cls)
 {
   struct GNUNET_PeerIdentity pid;
 
+  job = NULL;
   if (GNUNET_OK !=
       GNUNET_CRYPTO_eddsa_public_key_from_string (tunnel_id,
                                                   strlen (tunnel_id),
@@ -668,10 +702,13 @@ show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     fprintf (stderr,
              _("Invalid tunnel owner `%s'\n"),
              tunnel_id);
-    GNUNET_SCHEDULER_shutdown();
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  GNUNET_CADET_get_tunnel (mh, &pid, tunnel_callback, NULL);
+  GNUNET_CADET_get_tunnel (mh,
+                           &pid,
+                           &tunnel_callback,
+                           NULL);
 }
 
 
@@ -679,12 +716,12 @@ show_tunnel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Call CADET's monitor API, get info of one channel.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+show_channel (void *cls)
 {
-
+  job = NULL;
+  GNUNET_break (0);
 }
 
 
@@ -692,12 +729,12 @@ show_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Call CADET's monitor API, get info of one connection.
  *
  * @param cls Closure (unused).
- * @param tc TaskContext
  */
 static void
-show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+show_connection (void *cls)
 {
-
+  job = NULL;
+  GNUNET_break (0);
 }
 
 
@@ -710,105 +747,164 @@ show_connection (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cfg configuration
  */
 static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL;
-  GNUNET_CADET_ChannelEndHandler *endch = NULL;
-  static const struct GNUNET_CADET_MessageHandler handlers[] = {
-    {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0},
-    {NULL, 0, 0} /* FIXME add option to monitor msg types */
+  struct GNUNET_MQ_MessageHandler handlers[] = {
+    GNUNET_MQ_hd_var_size (data,
+                           GNUNET_MESSAGE_TYPE_CADET_CLI,
+                           struct GNUNET_MessageHeader,
+                           NULL),
+    GNUNET_MQ_handler_end ()
   };
-  static uint32_t *ports = NULL;
+
   /* FIXME add option to monitor apps */
 
   target_id = args[0];
-  target_port = args[0] && args[1] ? atoi(args[1]) : 0;
+  if (target_id && args[1])
+    target_port = args[1];
+
   if ( (0 != (request_peers | request_tunnels)
-        || 0 != monitor_mode
         || NULL != tunnel_id
         || NULL != conn_id
         || NULL != channel_id)
        && target_id != NULL)
   {
     FPRINTF (stderr,
-             _("You must NOT give a TARGET"
-               "when using 'request all' options\n"));
+             _("Extra arguments are not applicable "
+               "in combination with this option.\n"));
     return;
   }
 
   if (GNUNET_YES == dump)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "requesting debug dump\n");
-    GNUNET_SCHEDULER_add_now (&request_dump, NULL);
-  }
-  else if (NULL != target_id)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Creating channel to %s\n",
-                target_id);
-    GNUNET_SCHEDULER_add_now (&create_channel, NULL);
-    endch = &channel_ended;
-  }
-  else if (0 != listen_port)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n");
-    newch = &channel_incoming;
-    endch = &channel_ended;
-    ports = GNUNET_malloc (sizeof (uint32_t) * 2);
-    ports[0] = listen_port;
+                "Requesting debug dump\n");
+    job = GNUNET_SCHEDULER_add_now (&request_dump,
+                                    NULL);
   }
   else if (NULL != peer_id)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show peer\n");
-    GNUNET_SCHEDULER_add_now (&show_peer, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show peer\n");
+    job = GNUNET_SCHEDULER_add_now (&show_peer,
+                                    NULL);
   }
   else if (NULL != tunnel_id)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show tunnel\n");
-    GNUNET_SCHEDULER_add_now (&show_tunnel, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show tunnel\n");
+    job = GNUNET_SCHEDULER_add_now (&show_tunnel,
+                                    NULL);
   }
   else if (NULL != channel_id)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show channel\n");
-    GNUNET_SCHEDULER_add_now (&show_channel, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show channel\n");
+    job = GNUNET_SCHEDULER_add_now (&show_channel,
+                                    NULL);
   }
   else if (NULL != conn_id)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show connection\n");
-    GNUNET_SCHEDULER_add_now (&show_connection, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show connection\n");
+    job = GNUNET_SCHEDULER_add_now (&show_connection,
+                                    NULL);
   }
   else if (GNUNET_YES == request_peers)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all peers\n");
-    GNUNET_SCHEDULER_add_now (&get_peers, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show all peers\n");
+    job = GNUNET_SCHEDULER_add_now (&get_peers,
+                                    NULL);
   }
   else if (GNUNET_YES == request_tunnels)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Show all tunnels\n");
-    GNUNET_SCHEDULER_add_now (&get_tunnels, NULL);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Show all tunnels\n");
+    job = GNUNET_SCHEDULER_add_now (&get_tunnels,
+                                    NULL);
   }
-  else
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Connecting to CADET service\n");
+  mh = GNUNET_CADET_connect (cfg);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                 NULL);
+  if (NULL == mh)
   {
-    FPRINTF (stderr, "No action requested\n");
+    GNUNET_SCHEDULER_shutdown ();
     return;
   }
+  if (NULL != listen_port)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Opening CADET listen port\n");
+    GNUNET_CRYPTO_hash (listen_port,
+                        strlen (listen_port),
+                        &porthash);
+    lp = GNUNET_CADET_open_port (mh,
+                                 &porthash,
+                                 &channel_incoming,
+                                 NULL,
+                                 NULL /* window changes */,
+                                 &channel_ended,
+                                 handlers);
+  }
+  if (NULL != target_id)
+  {
+    struct GNUNET_PeerIdentity pid;
+    enum GNUNET_CADET_ChannelOption opt;
+
+    if (GNUNET_OK !=
+        GNUNET_CRYPTO_eddsa_public_key_from_string (target_id,
+                                                    strlen (target_id),
+                                                    &pid.public_key))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                  _("Invalid target `%s'\n"),
+                  target_id);
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Connecting to `%s:%s'\n",
+                target_id,
+                target_port);
+    opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE;
+    GNUNET_CRYPTO_hash (target_port,
+                        strlen(target_port),
+                        &porthash);
+    ch = GNUNET_CADET_channel_create (mh,
+                                      NULL,
+                                      &pid,
+                                      &porthash,
+                                      opt,
+                                      NULL /* window changes */,
+                                      &channel_ended,
+                                      handlers);
+    if (GNUNET_YES == echo)
+    {
+      echo_task = GNUNET_SCHEDULER_add_now (&send_echo,
+                                            NULL);
+    }
+    else
+    {
+      listen_stdio ();
+    }
+  }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to cadet\n");
-  mh = GNUNET_CADET_connect (cfg,
-                            NULL, /* cls */
-                            newch, /* new channel */
-                            endch, /* cleaner */
-                            handlers,
-                            ports);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n");
-  if (NULL == mh)
-    GNUNET_SCHEDULER_add_now (shutdown_task, NULL);
-  else
-    sd = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                       shutdown_task, NULL);
-
+  if ( (NULL == lp) &&
+       (NULL == job) &&
+       (NULL == ch) )
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE,
+                _("No action requested\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
 }
 
 
@@ -820,51 +916,70 @@ run (void *cls, char *const *args, const char *cfgfile,
  * @return 0 ok, 1 on error
  */
 int
-main (int argc, char *const *argv)
+main (int argc,
+      char *const *argv)
 {
   int res;
-  const char helpstr[] = "Create channels and retreive info about cadets status.";
-  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
-//     {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
-//      gettext_noop ("provide information about a particular channel"),
-//      GNUNET_YES, &GNUNET_GETOPT_set_string, &channel_id},
-    {'C', "connection", "CONNECTION_ID",
-     gettext_noop ("provide information about a particular connection"),
-     GNUNET_YES, &GNUNET_GETOPT_set_string, &conn_id},
-    {'e', "echo", NULL,
-     gettext_noop ("activate echo mode"),
-     GNUNET_NO, &GNUNET_GETOPT_set_one, &echo},
-    {'d', "dump", NULL,
-     gettext_noop ("dump debug information to STDERR"),
-     GNUNET_NO, &GNUNET_GETOPT_set_one, &dump},
-//     {'m', "monitor", NULL,
-//      gettext_noop ("provide information about all events (continuously)"),
-//      GNUNET_NO, &GNUNET_GETOPT_set_one, &monitor_mode},
-    {'o', "open-port", NULL,
-     gettext_noop ("port to listen to (default; 0)"),
-     GNUNET_YES, &GNUNET_GETOPT_set_uint, &listen_port},
-    {'p', "peer", "PEER_ID",
-     gettext_noop ("provide information about a patricular peer"),
-     GNUNET_YES, &GNUNET_GETOPT_set_string, &peer_id},
-    {'P', "peers", NULL,
-      gettext_noop ("provide information about all peers"),
-      GNUNET_NO, &GNUNET_GETOPT_set_one, &request_peers},
-    {'t', "tunnel", "TUNNEL_ID",
-     gettext_noop ("provide information about a particular tunnel"),
-     GNUNET_YES, &GNUNET_GETOPT_set_string, &tunnel_id},
-    {'T', "tunnels", NULL,
-     gettext_noop ("provide information about all tunnels"),
-     GNUNET_NO, &GNUNET_GETOPT_set_one, &request_tunnels},
+  const char helpstr[] = "Create tunnels and retrieve info about CADET's status.";
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+    /* I would use the terminology 'circuit' here...  --lynX */
+    GNUNET_GETOPT_option_string ('C',
+                                 "connection",
+                                 "CONNECTION_ID",
+                                 gettext_noop ("Provide information about a particular connection"),
+                                 &conn_id),
+
+    GNUNET_GETOPT_option_flag ('e',
+                                  "echo",
+                                  gettext_noop ("Activate echo mode"),
+                                  &echo), 
+
+    GNUNET_GETOPT_option_flag ('d',
+                                  "dump",
+                                  gettext_noop ("Dump debug information to STDERR"),
+                                  &dump),
+
+    GNUNET_GETOPT_option_string ('o',
+                                 "open-port",
+                                 "SHARED_SECRET",
+                                 gettext_noop ("Listen for connections using a shared secret among sender and recipient"),
+                                 &listen_port),
+
+
+    GNUNET_GETOPT_option_string ('p',
+                                 "peer",
+                                 "PEER_ID",
+                                 gettext_noop ("Provide information about a patricular peer"),
+                                 &peer_id),
+
+
+    GNUNET_GETOPT_option_flag ('P',
+                                  "peers",
+                                  gettext_noop ("Provide information about all peers"),
+                                  &request_peers),
+
+    GNUNET_GETOPT_option_string ('t',
+                                 "tunnel",
+                                 "TUNNEL_ID",
+                                 gettext_noop ("Provide information about a particular tunnel"),
+                                 &tunnel_id),
+
+
+    GNUNET_GETOPT_option_flag ('T',
+                                  "tunnels",
+                                  gettext_noop ("Provide information about all tunnels"),
+                                  &request_tunnels),
 
     GNUNET_GETOPT_OPTION_END
   };
 
-  monitor_mode = GNUNET_NO;
-
-  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
+  if (GNUNET_OK !=
+      GNUNET_STRINGS_get_utf8_args (argc, argv,
+                                    &argc, &argv))
     return 2;
 
-  res = GNUNET_PROGRAM_run (argc, argv, "gnunet-cadet (OPTIONS | TARGET PORT)",
+  res = GNUNET_PROGRAM_run (argc, argv,
+                            "gnunet-cadet (OPTIONS | PEER_ID SHARED_SECRET)",
                             gettext_noop (helpstr),
                             options, &run, NULL);
 
@@ -872,8 +987,7 @@ main (int argc, char *const *argv)
 
   if (GNUNET_OK == res)
     return 0;
-  else
-    return 1;
+  return 1;
 }
 
 /* end of gnunet-cadet.c */