gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r28438 - msh/src


From: gnunet
Subject: [GNUnet-SVN] r28438 - msh/src
Date: Wed, 7 Aug 2013 15:56:40 +0200

Author: harsha
Date: 2013-08-07 15:56:40 +0200 (Wed, 07 Aug 2013)
New Revision: 28438

Modified:
   msh/src/addressmap.c
   msh/src/addressmap.h
   msh/src/msh.c
   msh/src/mshd-server.c
   msh/src/mshd.c
   msh/src/mtypes.h
Log:
- altered protocol design; msh now connects to remote mshd directly.  For
    address lookup and authentication it relies on its parent mshd.


Modified: msh/src/addressmap.c
===================================================================
--- msh/src/addressmap.c        2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/addressmap.c        2013-08-07 13:56:40 UTC (rev 28438)
@@ -77,13 +77,19 @@
 #define instance_address_ip(iaddr) \
   (NULL == iaddr ? 0 : ((uint32_t) iaddr->ip))
 
+
 /**
- * Get the port number from an instance address
+ * Function to return the port number of an instance address
+ *
+ * @param iaddr the instance address
+ * @return the port number
  */
-#define instance_address_port(iaddr) \
-  (iaddr->port)
+uint16_t
+instance_address_port (struct InstanceAddr *iaddr)
+{
+  return iaddr->port;
+}
 
-
 /**
  * Create an instance address structure from its port number and ip address
  *
@@ -606,7 +612,7 @@
   for (iaddr = iainfo->addr_head; NULL != iaddr; iaddr = iaddr->next)
   {
     if (GNUNET_SYSERR == 
-        GNUNET_CONTAINER_multihashmap32_put (rmap->map, (uint32_t) iaddr->ip, 
iainfo,
+        GNUNET_CONTAINER_multihashmap32_put (rmap->map, (uint32_t) iaddr->ip, 
iaddr,
                                              
GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
     {
       GNUNET_break (0);
@@ -692,3 +698,19 @@
 {
   return GNUNET_CONTAINER_multihashmap32_contains (rmap->map, (uint32_t) ip);
 }
+
+
+/**
+ * Find the address through reverse IP mapping
+ *
+ * @param map the map
+ * @param ip the IP
+ * @return NULL if no value was found; note that this is indistinguishable from
+ *   values that just happen to be NULL; use "contains" to test for key-value
+ *   pairs with value NULL
+ */
+struct InstanceAddr *
+reversemap_lookup (struct ReverseAddressMap *rmap, in_addr_t ip)
+{
+  return GNUNET_CONTAINER_multihashmap32_get (rmap->map, (uint32_t) ip);
+}

Modified: msh/src/addressmap.h
===================================================================
--- msh/src/addressmap.h        2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/addressmap.h        2013-08-07 13:56:40 UTC (rev 28438)
@@ -34,6 +34,16 @@
 
 
 /**
+ * Function to return the port number of an instance address
+ *
+ * @param iaddr the instance address
+ * @return the port number
+ */
+uint16_t
+instance_address_port (struct InstanceAddr *iaddr);
+
+
+/**
  * Create an instance address information object
  *
  * @param rank the MPI rank of the instance
@@ -278,6 +288,31 @@
 reverse_map_destroy (struct ReverseAddressMap *rmap);
 
 
+/**
+ * Check if the reverse address map contains a given IP addresses
+ *
+ * @param rmap the map
+ * @param ip the IP addresses
+ * @return GNUNET_YES if the given IP exists,
+ *         GNUNET_NO if not
+ */
+int
+reversemap_check (struct ReverseAddressMap *rmap, in_addr_t ip);
+
+
+/**
+ * Find the address through reverse IP mapping
+ *
+ * @param map the map
+ * @param ip the IP
+ * @return NULL if no value was found; note that this is indistinguishable from
+ *   values that just happen to be NULL; use "contains" to test for key-value
+ *   pairs with value NULL
+ */
+struct InstanceAddr *
+reversemap_lookup (struct ReverseAddressMap *rmap, in_addr_t ip);
+
+
 #endif  /* ADDRESSMAP_H_ */
 
 /* End of addressmap.h */

Modified: msh/src/msh.c
===================================================================
--- msh/src/msh.c       2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/msh.c       2013-08-07 13:56:40 UTC (rev 28438)
@@ -8,7 +8,6 @@
 
 #include "common.h"
 #include <gnunet/gnunet_util_lib.h>
-#include "util.h"
 #include "mtypes.h"
 
 #define LOG(kind,...)                           \
@@ -19,6 +18,16 @@
 #define LOG_ERROR(...) LOG(GNUNET_ERROR_TYPE_ERROR, __VA_ARGS__)
 
 /**
+ * Configuration section for connection to local MSHD
+ */
+#define MSHD_LOCAL "MSHD-LOCAL"
+
+/**
+ * Configuration section for connection to remote MSHD
+ */
+#define MSHD_REMOTE "MSHD-REMOTE"
+
+/**
  * The message queue for sending messages to the controller service
  */
 struct MessageQueue
@@ -54,7 +63,13 @@
  */
 static struct GNUNET_CLIENT_Connection *client;
 
+
 /**
+ * Our connection to the parent MSHD
+ */
+static struct GNUNET_CLIENT_Connection *client_remote;
+
+/**
  * Handle for transmissions on conn
  */
 static struct GNUNET_CLIENT_TransmitHandle *th;
@@ -92,11 +107,16 @@
 struct GNUNET_CLIENT_TestHandle *test;
 
 /**
- * our configuration for accessing GNUnet's client API functions
+ * configuration for connecting to local MSHD through client API
  */
 struct GNUNET_CONFIGURATION_Handle *cfg;
 
 /**
+ * configuration for connecting to remote MSHD through client API
+ */
+struct GNUNET_CONFIGURATION_Handle *remote_cfg;
+
+/**
  * length of the cmdstr
  */
 static size_t cmdstr_len;
@@ -147,8 +167,33 @@
  */
 static in_addr_t target;
 
+/**
+ * The port number where the MSHD is listening on target host
+ */
+static uint16_t target_port;
 
+
 /**
+ * Function to convert an 32-bit IP address into numbers-and-dots notation
+ *
+ * @param ip the IP address
+ * @return the string represeting the IP address.  The string is statically 
allocated.
+ */
+char *
+ip2str (const in_addr_t ip)
+{
+  static char hostip[NI_MAXHOST];
+  
+  GNUNET_break (0 < snprintf (hostip, NI_MAXHOST, "%u.%u.%u.%u",
+                           ip >> 24, 
+                           (ip >> 16) % 256,
+                           (ip >> 8) % 256,
+                           ip % 256));
+  return hostip;
+}
+
+
+/**
  * Function to cleanup the message queue
  *
  * @param 
@@ -320,7 +365,6 @@
 forward_output (void *cls, const struct GNUNET_MessageHeader *msg_)
 {
   const struct MSH_MSG_CmdIO *msg;
-  int fd_stdout;
   uint16_t size;
 
   msg = (const struct MSH_MSG_CmdIO *) msg_;
@@ -331,11 +375,10 @@
     return;
   }
   size = ntohs (msg->header.size);  
-  fd_stdout = fileno (stdout);
-  GNUNET_assert (-1 < fd_stdout);
-  GNUNET_break (size == write (fd_stdout, msg->data,
-                               size - sizeof (struct MSH_MSG_CmdIO)));
-  GNUNET_CLIENT_receive (client, &forward_output, NULL,
+  GNUNET_break (size == 
+                GNUNET_DISK_file_write (fh_stdout, msg->data,
+                                        size - sizeof (struct MSH_MSG_CmdIO)));
+  GNUNET_CLIENT_receive (client_remote, &forward_output, NULL,
                          GNUNET_TIME_UNIT_FOREVER_REL);
 }
 
@@ -348,26 +391,144 @@
  * @param msg message received, NULL on timeout or fatal error
  */
 static void
-delivery_status (void *cls, const struct GNUNET_MessageHeader *msg)
+challenge_status (void *cls, const struct GNUNET_MessageHeader *msg_)
 {
-  if (MSH_MTYPE_SERVER_RUNCMDSTATUS != ntohs (msg->type))
-  {
-    GNUNET_break (0);
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
+  LOG (GNUNET_ERROR_TYPE_INFO, "Executing remote command\n");
   fh_stdin = GNUNET_DISK_get_handle_from_native (stdin);
   fh_stdout = GNUNET_DISK_get_handle_from_native (stdout);
+  GNUNET_assert (NULL != fh_stdin);
+  GNUNET_assert (NULL != fh_stdout);
   state = STATE_FORWARD_STREAMS;
-  GNUNET_CLIENT_receive (client, &forward_output, NULL,
+  GNUNET_CLIENT_receive (client_remote, &forward_output, NULL,
                          GNUNET_TIME_UNIT_FOREVER_REL);
   task_fwd_stdin = 
       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
-                                      fh_stdin, &fwd_stdin, NULL);
+                                      fh_stdin, &fwd_stdin, NULL);  
 }
 
 
 /**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+auth_challenge_response (void *cls, const struct GNUNET_MessageHeader *msg_)
+{
+  const struct MSH_MSG_ChallengeResponse *msg;
+
+  //msg = (const struct MSH_MSG_ChallengeResponse *) msg_;
+  GNUNET_CLIENT_transmit_and_get_response (client_remote,
+                                           msg_,
+                                           GNUNET_TIME_UNIT_MINUTES,
+                                           GNUNET_NO,
+                                           &challenge_status,
+                                           NULL);
+}
+
+
+/**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+auth_challenge (void *cls, const struct GNUNET_MessageHeader *msg_)
+{
+  if (MSH_MTYPE_CHALLENGE != ntohs (msg_->type))
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  if (sizeof (struct MSH_MSG_Challenge) >= ntohs (msg_->size))
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  GNUNET_assert (GNUNET_OK ==
+                 GNUNET_CLIENT_transmit_and_get_response (client,
+                                                          msg_,
+                                                          
GNUNET_TIME_UNIT_SECONDS,
+                                                          GNUNET_NO,
+                                                          
&auth_challenge_response,
+                                                          NULL));
+}
+
+
+static void
+target_connect ()
+{
+  struct MSH_MSG_RunCmd *msg;
+  uint16_t size;
+
+  remote_cfg = GNUNET_CONFIGURATION_create ();
+  GNUNET_CONFIGURATION_set_value_string (remote_cfg, MSHD_REMOTE, "LISTEN",
+                                         ip2str (target));
+  GNUNET_CONFIGURATION_set_value_number (remote_cfg, MSHD_REMOTE, "PORT",
+                                         target_port);
+  client_remote = GNUNET_CLIENT_connect (MSHD_REMOTE, remote_cfg);
+  if (NULL == client_remote)
+    return;
+  size = sizeof (struct MSH_MSG_RunCmd) + cmdstr_len;
+  msg = GNUNET_malloc (size);
+  msg->header.size = htons (size);
+  msg->header.type = htons (MSH_MTYPE_RUNCMD);
+  (void) memcpy (msg->cmd, cmdstr, cmdstr_len);
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_CLIENT_transmit_and_get_response (client_remote,
+                                                          &msg->header,
+                                                          
GNUNET_TIME_UNIT_SECONDS,
+                                                          GNUNET_NO,
+                                                          &auth_challenge,
+                                                          NULL));
+  GNUNET_free (msg);
+}
+
+
+/**
+ * Type of a function to call when we receive a message
+ * from the service.
+ *
+ * @param cls closure
+ * @param msg message received, NULL on timeout or fatal error
+ */
+static void
+lookup_status (void *cls, const struct GNUNET_MessageHeader *msg_)
+{
+  const struct MSH_MSG_AddressLookupReply *msg;
+  uint16_t size;
+  if (MSH_MTYPE_ADDRESS_LOOKUP_REPLY != ntohs (msg_->type))
+  {
+    GNUNET_break (0);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  size = ntohs (msg_->size);
+  msg = (const struct MSH_MSG_AddressLookupReply *) msg_;
+  if (0 == (target_port = ntohs (msg->port)))
+  {
+    if ('\0' != msg->emsg[(size - sizeof (struct MSH_MSG_AddressLookup)) - 1])
+    {
+      GNUNET_break (0);
+      GNUNET_SCHEDULER_shutdown ();
+      return;
+    }
+    LOG_ERROR ("Address lookup for IP:%s failed with error: %s\n",
+               ip2str (target), msg->emsg);
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  target_connect ();
+}
+
+
+/**
  * Function called with the result on the service test.
  *
  * @param cls closure
@@ -378,7 +539,7 @@
 static void
 result_cb (void *cls, int result)
 {
-  struct MSH_MSG_RunCmd *msg;
+  struct MSH_MSG_AddressLookup *msg;
   uint16_t size;
 
   test = NULL;
@@ -387,22 +548,21 @@
     LOG_ERROR ("Service not running\n");
     return;
   }
-  client = GNUNET_CLIENT_connect ("mshd", cfg);
+  client = GNUNET_CLIENT_connect (MSHD_LOCAL, cfg);
   GNUNET_assert (NULL != client);
   GNUNET_assert (0 != cmdstr_len);
-  size = sizeof (struct MSH_MSG_RunCmd) + cmdstr_len;
+  size = sizeof (struct MSH_MSG_AddressLookup);
   msg = GNUNET_malloc (size);
   msg->header.size = htons (size);
-  msg->header.type = htons (MSH_MTYPE_CLIENT_RUNCMD);
+  msg->header.type = htons (MSH_MTYPE_ADDRESS_LOOKUP);
   msg->ip = htonl ((uint32_t) target);
-  (void) memcpy (msg->cmd, cmdstr, cmdstr_len);
   GNUNET_assert (GNUNET_OK == 
                  GNUNET_CLIENT_transmit_and_get_response (client, &msg->header,
                                                           
GNUNET_TIME_UNIT_SECONDS,
                                                           GNUNET_NO,
-                                                          &delivery_status,
+                                                          &lookup_status,
                                                           NULL));
-
+  GNUNET_free (msg);
 }
 
 
@@ -455,13 +615,13 @@
     return;
   }
   cfg = GNUNET_CONFIGURATION_dup (cfg_);
-  GNUNET_CONFIGURATION_set_value_string (cfg, "MSHD", "UNIXPATH", psock_path);
+  GNUNET_CONFIGURATION_set_value_string (cfg, MSHD_LOCAL, "UNIXPATH", 
psock_path);
   if (NULL == client)
   {
     LOG_ERROR ("Cannot open connection to MSHD\n");
     return;
   }
-  test = GNUNET_CLIENT_service_test ("mshd", cfg, GNUNET_TIME_UNIT_SECONDS,
+  test = GNUNET_CLIENT_service_test (MSHD_LOCAL, cfg, GNUNET_TIME_UNIT_SECONDS,
                                      &result_cb, NULL);
 }
 

Modified: msh/src/mshd-server.c
===================================================================
--- msh/src/mshd-server.c       2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/mshd-server.c       2013-08-07 13:56:40 UTC (rev 28438)
@@ -11,6 +11,9 @@
 #include "mshd.h"
 #include "addressmap.h"
 
+#define MESSAGE_SEND_TIMEOUT(s) \
+  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, s)
+
 /**
  * server handle for accepting requests from local MSH instances
  */
@@ -33,6 +36,161 @@
 
 
 /**
+ * Tranmission context
+ */
+struct TransmitCtx
+{
+  struct GNUNET_SERVER_Client *client;
+  
+  struct GNUNET_MessageHeader *msg;
+  
+};
+
+/**
+ * Context for connections requiring to execute commands
+ */
+struct ExecConnCtx
+{
+  /**
+   * the client handle
+   */
+  struct GNUNET_SERVER_Client *client;
+
+  /**
+   * The command strings to execute
+   */
+  char **args;
+
+  /**
+   * salt hash used for authentication
+   */
+  struct GNUNET_HashCode salt;
+
+  /**
+   * timeout task
+   */
+  GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+
+  /**
+   * the file descriptor associated with the client connection
+   */
+  int conn_fd;
+};
+
+
+/**
+ * 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.
+ *
+ * @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
+ */
+static size_t 
+transmit_ready_cb (void *cls, size_t size, void *buf)
+{
+  struct TransmitCtx *ctx = cls;
+  struct GNUNET_SERVER_Client *client = ctx->client;
+  struct GNUNET_MessageHeader *msg = ctx->msg;
+  size_t wrote;
+
+  wrote = 0;
+  if ((0 == size) || (NULL == buf))
+    goto cleanup;
+  wrote = ntohs (msg->size);
+  GNUNET_assert (size >= wrote);
+  (void) memcpy (buf, msg, wrote);
+  
+ cleanup:
+  GNUNET_free (msg);
+  GNUNET_SERVER_client_drop (client);
+  GNUNET_SERVER_receive_done (client, GNUNET_YES);
+  return wrote;
+}
+
+
+static void
+send_message (struct GNUNET_SERVER_Client *client,
+              struct GNUNET_MessageHeader *msg)
+{
+  struct TransmitCtx *ctx;
+
+  GNUNET_SERVER_client_keep (client);
+  ctx = GNUNET_malloc (sizeof (struct TransmitCtx));
+  ctx->client = client;
+  ctx->msg = msg;
+  (void) GNUNET_SERVER_notify_transmit_ready (client, ntohs (msg->size),
+                                              MESSAGE_SEND_TIMEOUT (30),
+                                              &transmit_ready_cb, ctx);
+
+}
+
+
+static int
+parse_strings (const char *buf, size_t size, char ***s)
+{
+  size_t off;
+  unsigned int cnt;
+  const char *p;
+
+  cnt = 0;
+  p = NULL;
+  for (off = 0; off < size; off++)
+  {
+    if ('\0' == buf[off])
+    {
+      if (NULL == p)
+        continue;
+      GNUNET_array_append (*s, cnt, GNUNET_strdup (p));
+      p = NULL;
+      continue;
+    }
+    p = &buf[off];      
+  }
+  if (0 < cnt)
+    GNUNET_array_append (*s, cnt, NULL);
+  return cnt;
+}
+
+static struct GNUNET_MessageHeader *
+create_challenge_message (struct ExecConnCtx *ctx)
+{
+  struct MSH_MSG_Challenge *msg;
+  uint16_t size;
+
+  GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &ctx->salt);
+  size = sizeof (struct MSH_MSG_Challenge);
+  msg = GNUNET_malloc (size);
+  msg->header.type = htons (MSH_MTYPE_CHALLENGE);
+  msg->header.size = htons (size);
+  GNUNET_CRYPTO_hash_to_enc (&ctx->salt, &msg->salt);
+  return &msg->header;
+}
+
+
+static void
+destroy_ctx (struct ExecConnCtx *ctx)
+{
+  unsigned int cnt;
+
+  if (NULL != ctx->client)
+    GNUNET_SERVER_client_drop (ctx->client);
+  if (NULL != ctx->args)
+  {
+    for (cnt = 0; NULL != ctx->args[cnt]; cnt++)
+      GNUNET_free (ctx->args[cnt]);
+    GNUNET_free (ctx->args);
+  }
+  if (GNUNET_SCHEDULER_NO_TASK != ctx->timeout_task)
+    GNUNET_SCHEDULER_cancel (ctx->timeout_task);
+  GNUNET_free (ctx);
+}
+
+
+/**
  * Functions with this signature are called whenever a message is
  * received.
  *
@@ -41,38 +199,80 @@
  * @param message the actual message
  */
 static void
-handle_client_runcmd (void *cls,
-                      struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader* message)
+handle_runcmd (void *cls,
+               struct GNUNET_SERVER_Client *client,
+               const struct GNUNET_MessageHeader* message)
 {
-  struct MSH_MSG_RunCmdStatus *reply;
+  struct ExecConnCtx *ctx;
   const struct MSH_MSG_RunCmd *msg;
-  const struct MSH_MSG_RunCmd *forward;
-  const char *emsg;
-  in_addr_t ip;
+  struct GNUNET_MessageHeader *reply;
   uint16_t size;
-  uint16_t emsg_len;
   
+  ctx = GNUNET_SERVER_client_get_user_context (client, struct ExecConnCtx);
+  size = ntohs (message->size);
+  if ( size <= sizeof (struct MSH_MSG_RunCmd))
+    goto close_conn;
+  msg = (const struct MSH_MSG_RunCmd *) message;
+  if (0 == parse_strings (msg->cmd,
+                          size - sizeof (struct MSH_MSG_RunCmd),
+                          &ctx->args))
+    goto close_conn;
+  reply = create_challenge_message (ctx);
+  send_message (client, reply);
+  return;
+
+ close_conn:
+  GNUNET_break_op (0);
+  destroy_ctx (ctx);
+  GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+}
+
+
+/**
+ * Functions with this signature are called whenever a message is
+ * received.
+ *
+ * @param cls closure
+ * @param client identification of the client
+ * @param message the actual message
+ */
+static void
+handle_addresslookup (void *cls, struct GNUNET_SERVER_Client *client,
+                      const struct GNUNET_MessageHeader* message)
+{
+  const struct MSH_MSG_AddressLookup *msg;
+  struct MSH_MSG_AddressLookupReply *reply;
+  struct TransmitCtx *ctx;
+  struct InstanceAddr *iaddr;
+  char *emsg;
+  in_addr_t ip;
+  uint16_t port;
+  uint16_t reply_size;
+
   emsg = NULL;
-  msg = (const struct MSH_MSG_RunCmd *) message;
-  ip = (in_addr_t) ntohl (msg->ip);
+  port = 0;
+  msg = (const struct MSH_MSG_AddressLookup *) message;
+  ip = ntohl (msg->ip);
   if (GNUNET_NO == reversemap_check (rmap, ip))
   {
     emsg = "Given IP address not present in the current mapping\n";
     goto respond;
   }
-  forward = (struct MSH_MSG_RunCmd *) GNUNET_copy_message (message);
-  /* FIXME: forward message to the responsible MSHD */
-  
-  
+  iaddr = reversemap_lookup (rmap, ip);
+  GNUNET_assert (NULL != iaddr);
+  port = instance_address_port (iaddr);
+
  respond:
-  emsg_len = (NULL == emsg) ? 0 : strlen (emsg);
-  size = sizeof (struct MSH_MSG_RunCmdStatus) + emsg_len;
-  reply = GNUNET_malloc (size);
-  reply->header.type = htons (MSH_MTYPE_SERVER_RUNCMDSTATUS);
-  reply->header.size = htons (size);
+  reply_size = sizeof (struct MSH_MSG_AddressLookupReply);
   if (NULL != emsg)
-    (void) memcpy (reply->emsg, emsg, emsg_len);
+    reply_size += strlen (emsg) + 1;
+  reply = GNUNET_malloc (reply_size);
+  reply->header.size = htons (reply_size);
+  reply->header.type = htons (MSH_MTYPE_ADDRESS_LOOKUP_REPLY);
+  reply->port = htons (port);
+  if (NULL != emsg)
+    (void) strcpy (reply->emsg, emsg);
+  send_message (client, &reply->header);
 }
 
 
@@ -85,15 +285,26 @@
  * @param message the actual message
  */
 static void
-handle_client_cmd_stream_stdin (void *cls,
-                                struct GNUNET_SERVER_Client *client,
-                                const struct GNUNET_MessageHeader* message)
+handle_auth_challenge (void *cls, struct GNUNET_SERVER_Client *client,
+                       const struct GNUNET_MessageHeader* message)
 {
-  const struct MSH_MSG_CmdIO *msg;
+  const struct MSH_MSG_Challenge *msg;
+  struct MSH_MSG_ChallengeResponse *reply;
+  struct GNUNET_HashCode salt;
+  struct GNUNET_HashCode xor;
 
-  msg = (const struct MSH_MSG_CmdIO *) message;
-  /* FIXME: forward message to the responsible MSHD */
-  GNUNET_break (0);
+  msg = (const struct MSH_MSG_Challenge *) message;
+  GNUNET_assert (GNUNET_OK == 
+                 GNUNET_CRYPTO_hash_from_string2 (msg->salt.encoding,
+                                                  sizeof (msg->salt),
+                                                  &salt));
+  GNUNET_CRYPTO_hash_xor (&salt, &shash, &xor);
+  GNUNET_CRYPTO_hash (&xor, sizeof (struct GNUNET_HashCode), &salt);
+  reply = GNUNET_malloc (sizeof (struct MSH_MSG_ChallengeResponse));
+  reply->header.size = htons (sizeof (struct MSH_MSG_ChallengeResponse));
+  reply->header.type = htons (MSH_MTYPE_CHALLENGE_RESPONSE);
+  GNUNET_CRYPTO_hash_to_enc (&salt, &reply->auth);
+  send_message (client, &reply->header);
 }
 
 
@@ -123,8 +334,9 @@
 {
   struct sockaddr_un saddr;
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    {&handle_client_runcmd, NULL, MSH_MTYPE_CLIENT_RUNCMD, 0},
-    {&handle_client_cmd_stream_stdin, NULL, MSH_MTYPE_CMD_STREAM_STDIN, 0},
+    {&handle_addresslookup, NULL, MSH_MTYPE_ADDRESS_LOOKUP, sizeof (struct
+    MSH_MSG_AddressLookup)},
+    {&handle_auth_challenge, NULL, MSH_MTYPE_CHALLENGE, sizeof (struct 
MSH_MSG_Challenge)},
     {NULL, NULL, 0, 0}
   };
   struct sockaddr *saddrs[] = {
@@ -187,24 +399,51 @@
  * @param h the network handle of the socket to listen for incoming connections
  */
 int
-init_daemon_server (struct GNUNET_NETWORK_Handle *h)
+init_daemon_server ()
 {
-  static struct GNUNET_NETWORK_Handle *lsocks[] = {
-    NULL,
-    NULL
+  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
+    {&handle_runcmd, NULL, MSH_MTYPE_RUNCMD, 0},
+    {NULL, NULL, 0, 0}
   };
   
-  lsocks[0] = h;
-  daemon_serv = GNUNET_SERVER_create_with_sockets (&check_remote_access,
+  daemon_serv = GNUNET_SERVER_create_with_sockets (NULL,
                                                    NULL,
-                                                   lsocks,
+                                                   NULL,
                                                    
GNUNET_TIME_UNIT_FOREVER_REL,
                                                    GNUNET_YES);
-  return (NULL == daemon_serv) ? GNUNET_SYSERR: GNUNET_OK;
+  if (NULL == daemon_serv) 
+    return GNUNET_SYSERR;
+  GNUNET_SERVER_add_handlers (daemon_serv, handlers);
+  return GNUNET_OK;
 }
 
 
+static void
+timeout_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct ExecConnCtx *ctx = cls;
+
+  ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  destroy_ctx (ctx);
+}
+
+
 void
+daemon_server_add_connection (struct GNUNET_CONNECTION_Handle *conn, 
+                              int conn_fd)
+{
+  struct ExecConnCtx *ctx;
+
+  ctx = GNUNET_malloc (sizeof (struct ExecConnCtx));
+  ctx->conn_fd = conn_fd;
+  ctx->client = GNUNET_SERVER_connect_socket (daemon_serv, conn);
+  GNUNET_SERVER_client_set_user_context (ctx->client, ctx);
+  ctx->timeout_task = 
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &timeout_cb, 
ctx);
+}
+
+
+void
 shutdown_daemon_server ()
 {
   GNUNET_SERVER_destroy (daemon_serv);

Modified: msh/src/mshd.c
===================================================================
--- msh/src/mshd.c      2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/mshd.c      2013-08-07 13:56:40 UTC (rev 28438)
@@ -123,7 +123,7 @@
    * In this mode the listen socket accepts requests for starting remote 
processes
    */
   LISTEN_MODE_SERV
-};
+} listen_mode;;
 
 
 /**
@@ -339,22 +339,43 @@
     GNUNET_break (0);
     goto clo_ret;
   }
-  LOG_DEBUG ("%d: Got a connect\n", rank);
-  conn = GNUNET_CONNECTION_create_from_accept (NULL, NULL, listen_socket);
-  if (NULL == conn)
+  switch (listen_mode)
   {
-    GNUNET_break (0);
-    goto clo_ret;
+  case LISTEN_MODE_PROBE:
+    LOG_DEBUG ("%d: Got a probe connect\n", rank);
+    conn = GNUNET_CONNECTION_create_from_accept (NULL, NULL, listen_socket);
+    if (NULL == conn)
+    {
+      GNUNET_break (0);
+      goto clo_ret;
+    }
+    rctx = GNUNET_malloc (sizeof (struct ReadContext));
+    rctx->conn = conn;
+    rctx->in_receive = GNUNET_YES;
+    GNUNET_CONNECTION_receive (rctx->conn, sizeof (unsigned int),
+                               GNUNET_TIME_UNIT_FOREVER_REL, conn_reader, 
rctx);
+    GNUNET_CONTAINER_DLL_insert_tail (rhead, rtail, rctx);
+    /* resume accepting connections on the listen sock */
+    atask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                           listen_socket, &accept_task, NULL);
+    break;
+  case LISTEN_MODE_SERV:
+    {
+      struct GNUNET_NETWORK_Handle *client_sock;
+      struct GNUNET_CONNECTION_Handle *client_conn;
+      int client_sock_fd;
+      
+      LOG_DEBUG ("Got a command execution connection\n");
+      client_sock = GNUNET_NETWORK_socket_accept (listen_socket, NULL, NULL);
+      client_sock_fd = GNUNET_NETWORK_get_fd (client_sock);
+      client_conn = GNUNET_CONNECTION_create_from_existing (client_sock);
+      daemon_server_add_connection (client_conn);
+      GNUNET_break (0);
+    }
+    break;
+  default:
+    GNUNET_assert (0);
   }
-  rctx = GNUNET_malloc (sizeof (struct ReadContext));
-  rctx->conn = conn;
-  rctx->in_receive = GNUNET_YES;
-  GNUNET_CONNECTION_receive (rctx->conn, sizeof (unsigned int),
-                             GNUNET_TIME_UNIT_FOREVER_REL, conn_reader, rctx);
-  GNUNET_CONTAINER_DLL_insert_tail (rhead, rtail, rctx);
-  /* resume accepting connections on the listen sock */
-  atask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                         listen_socket, &accept_task, NULL);
   return;
 
  clo_ret:
@@ -393,7 +414,11 @@
   LOG_DEBUG ("Verification phase complete; commencing reduction phase\n");
   GNUNET_break (GNUNET_OK == reduce_ntree ());
   addressmap_print (addrmap);
+  listen_mode = LISTEN_MODE_SERV;
   rmap = addressmap_create_reverse_mapping (addrmap);
+  init_local_server ();
+  init_daemon_server (listen_socket);
+  listen_socket = NULL;
   if (0 == rank)
   {
     const char *unixpath = "/tmp/mshd.sock";

Modified: msh/src/mtypes.h
===================================================================
--- msh/src/mtypes.h    2013-08-07 12:10:06 UTC (rev 28437)
+++ msh/src/mtypes.h    2013-08-07 13:56:40 UTC (rev 28438)
@@ -106,31 +106,33 @@
 /* MSH Daemon and MSH command line tool communication messsages */
 /****************************************************************/
 
-#define MSH_MTYPE_CLIENT_RUNCMD 200
+#define MSH_MTYPE_RUNCMD 200
 
-#define MSH_MTYPE_SERVER_RUNCMDSTATUS 201
+#define MSH_MTYPE_RUNCMDSTATUS 201
 
 #define MSH_MTYPE_CMD_STREAM_STDIN 202
 
 #define MSH_MTYPE_CMD_STREAM_STDOUT 203
 
+#define MSH_MTYPE_ADDRESS_LOOKUP 204
 
+#define MSH_MTYPE_ADDRESS_LOOKUP_REPLY 205
+
+#define MSH_MTYPE_CHALLENGE 206
+
+#define MSH_MTYPE_CHALLENGE_RESPONSE 207
+
 /**
  * Message for sending a remote command and its arguments to MSHD
  */
 struct MSH_MSG_RunCmd
 {
   /**
-   * header
+   * header; set type to MSH_MTYPE_RUNCMD
    */
   struct GNUNET_MessageHeader header;
 
   /**
-   * The IP of the remote host
-   */
-  uint32_t ip;
-
-  /**
    * The command and all its arguments to be followed.  Each string is to be
    * NULL-terminated.
    */
@@ -144,7 +146,7 @@
 struct MSH_MSG_RunCmdStatus
 {
   /**
-   * header
+   * header; set type to MSH_MTYPE_RUNCMDSTATUS
    */
   struct GNUNET_MessageHeader header;
   
@@ -174,4 +176,67 @@
 };
 
 
+struct MSH_MSG_AddressLookup
+{
+  /**
+   * header; set type to MSH_MTYPE_ADDRESS_LOOKUP
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * The IP address to lookup
+   */
+  uint32_t ip;
+
+};
+
+
+struct MSH_MSG_AddressLookupReply
+{
+  /**
+   * header; set type to MSH_MTYPE_ADDRESS_LOOKUP_REPLY
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * the port number.  Will be set to 0 upon lookup failure
+   */
+  uint16_t port;
+
+  /**
+   * If the lookup failed, it will contain the related NULL-terminated error
+   * message
+   */
+  char emsg[0];  
+};
+
+
+struct MSH_MSG_Challenge
+{
+  /**
+   * header; set type to MSH_MTYPE_CHALLENGE
+   */
+  struct GNUNET_MessageHeader header;
+  
+  /**
+   * ascii encoded salt value to follow (0-terminated)
+   */
+  struct GNUNET_CRYPTO_HashAsciiEncoded salt;
+};
+
+
+struct MSH_MSG_ChallengeResponse
+{
+  /**
+   * header; set type to MSH_MTYPE_CHALLENGE_RESPONSE
+   */
+  struct GNUNET_MessageHeader header;
+
+  /**
+   * ascii encoded auth hash
+   */
+  struct GNUNET_CRYPTO_HashAsciiEncoded auth;
+};
+
+
 #endif  /* MTYPES_H_ */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]