- fix compilation issue
[oweals/gnunet.git] / src / mesh / gnunet-mesh.c
index af34d4ed4f93b0b9928f4cca4d27e59c05c78b7f..669ab9a01e68f5f400934f65579ffacb9a6bb800 100644 (file)
@@ -91,7 +91,7 @@ GNUNET_SCHEDULER_TaskIdentifier sd;
 
 
 
-void
+static void
 listen_stdio (void);
 
 
@@ -150,8 +150,8 @@ data_ready (void *cls, size_t size, void *buf)
   GNUNET_assert (size >= total_size);
 
   msg = buf;
-  msg->size = ntohs (total_size);
-  msg->type = ntohs (GNUNET_MESSAGE_TYPE_MESH_CLI);
+  msg->size = htons (total_size);
+  msg->type = htons (GNUNET_MESSAGE_TYPE_MESH_CLI);
   memcpy (&msg[1], cls, data_size);
   listen_stdio ();
 
@@ -177,8 +177,19 @@ read_stdio (void *cls,
     return;
   }
 
-  data_size = read (2, buf, 60000);
+  data_size = read (0, buf, 60000);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "stdio read %u bytes\n", data_size);
+  {
+    struct GNUNET_HashCode hash;
+    GNUNET_CRYPTO_hash (buf, data_size, &hash);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  cli SEND %p hash %s (%u)\n",
+                buf, GNUNET_h2s_full (&hash), data_size);
+  }
+  if (data_size < 1)
+  {
+    GNUNET_SCHEDULER_shutdown();
+    return;
+  }
   GNUNET_MESH_notify_transmit_ready (ch, GNUNET_NO,
                                      GNUNET_TIME_UNIT_FOREVER_REL,
                                      data_size
@@ -186,17 +197,22 @@ read_stdio (void *cls,
                                      &data_ready, buf);
 }
 
-void
+
+/**
+ * Start listening to stdin
+ */
+static void
 listen_stdio (void)
 {
   struct GNUNET_NETWORK_FDSet *rs;
 
   rs = GNUNET_NETWORK_fdset_create ();
-  GNUNET_NETWORK_fdset_set_native (rs, 2);
+  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_destroy (rs);
 }
 
 
@@ -241,11 +257,11 @@ channel_ended (void *cls,
  * @return initial channel context for the channel
  *         (can be NULL -- that's not an error)
  */
-void *
+static void *
 channel_incoming (void *cls,
                   struct GNUNET_MESH_Channel * channel,
                   const struct GNUNET_PeerIdentity * initiator,
-                  uint32_t port, enum MeshOption options)
+                  uint32_t port, enum GNUNET_MESH_ChannelOption options)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Incoming channel %p on port %u\n",
@@ -277,6 +293,7 @@ static void
 create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_PeerIdentity pid;
+  enum GNUNET_MESH_ChannelOption opt;
 
   GNUNET_assert (NULL == ch);
 
@@ -292,8 +309,8 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     return;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id);
-  ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port,
-                                   GNUNET_MESH_OPTION_DEFAULT);
+  opt = GNUNET_MESH_OPTION_DEFAULT | GNUNET_MESH_OPTION_RELIABLE;
+  ch = GNUNET_MESH_channel_create (mh, NULL, &pid, target_port, opt);
   listen_stdio ();
 }
 
@@ -312,18 +329,28 @@ create_channel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return #GNUNET_OK to keep the channel open,
  *         #GNUNET_SYSERR to close it (signal serious error).
  */
-int
+static int
 data_callback (void *cls,
                struct GNUNET_MESH_Channel *channel,
                void **channel_ctx,
                const struct GNUNET_MessageHeader *message)
 {
-  int16_t len;
+  uint16_t len;
   GNUNET_break (ch == channel);
 
   len = ntohs (message->size) - sizeof (*message);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got %u bytes\n", len);
-  FPRINTF (stdout, "%.*s", len, (char *) &message[1]);
+  write (1, (char *) &message[1], len);
+  {
+    struct GNUNET_HashCode hash;
+
+    GNUNET_CRYPTO_hash (message, ntohs (message->size), &hash);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  api RECV %p hash %s (%u)\n",
+                message, GNUNET_h2s_full (&hash), ntohs (message->size));
+    GNUNET_CRYPTO_hash (&message[1], len, &hash);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "  cli RECV %p hash %s (%u)\n",
+                &message[1], GNUNET_h2s_full (&hash), len);
+  }
   return GNUNET_OK;
 }
 
@@ -543,6 +570,7 @@ int
 main (int argc, char *const *argv)
 {
   int res;
+  const char helpstr[] = "Create channels and retreive info about meshs status.";
   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'a', "channel", "TUNNEL_ID:CHANNEL_ID",
      gettext_noop ("provide information about a particular channel"),
@@ -569,9 +597,8 @@ main (int argc, char *const *argv)
     return 2;
 
   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-mesh (OPTIONS | TARGET PORT)",
-                      gettext_noop
-                      ("Create channels and retreive info about meshs status."),
-                      options, &run, NULL);
+                            gettext_noop (helpstr),
+                            options, &run, NULL);
 
   GNUNET_free ((void *) argv);