[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r28406 - msh/src
From: |
gnunet |
Subject: |
[GNUnet-SVN] r28406 - msh/src |
Date: |
Mon, 5 Aug 2013 17:46:18 +0200 |
Author: harsha
Date: 2013-08-05 17:46:18 +0200 (Mon, 05 Aug 2013)
New Revision: 28406
Added:
msh/src/mshd-server.c
Modified:
msh/src/Makefile.am
msh/src/addressmap.c
msh/src/addressmap.h
msh/src/common.h
msh/src/mshd.c
msh/src/mshd.h
msh/src/mtypes.h
msh/src/reduce.c
Log:
- local server without forwarding
Modified: msh/src/Makefile.am
===================================================================
--- msh/src/Makefile.am 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/Makefile.am 2013-08-05 15:46:18 UTC (rev 28406)
@@ -3,7 +3,8 @@
mping_SOURCES = mping.c
mshd_SOURCES = mshd.c mshd.h util.c util.h mtypes.h \
- common.h bitmap.c bitmap.h addressmap.c addressmap.h reduce.h reduce.c
+ common.h bitmap.c bitmap.h addressmap.c addressmap.h reduce.h reduce.c \
+ mshd-server.c
mshd_LDADD = -lgnunetutil -lm
msh_SOURCES = msh.c mtypes.h
@@ -25,3 +26,4 @@
test-bitmap \
test-addressmap
+
Modified: msh/src/addressmap.c
===================================================================
--- msh/src/addressmap.c 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/addressmap.c 2013-08-05 15:46:18 UTC (rev 28406)
@@ -575,3 +575,120 @@
}
}
}
+
+
+/**
+ * Handle for reverse mapping from IP addresses to instance addresses
+ */
+struct ReverseAddressMap
+{
+ /**
+ * Hash map
+ */
+ struct GNUNET_CONTAINER_MultiHashMap32 *map;
+};
+
+
+/**
+ * Callback for iterating all the instance address info objects present in an
+ * address map
+ *
+ * @param cls the closure passed to addressmap_iterate_instance_address_infos()
+ * @param iainfo the instance address info object
+ * @return MSH_OK to continue iteration; MSH_SYSERR to terminate
+ */
+static int
+reversemap_address_iterator (void *cls, struct InstanceAddrInfo *iainfo)
+{
+ struct ReverseAddressMap *rmap = cls;
+ struct InstanceAddr *iaddr;
+
+ 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_MULTIHASHMAPOPTION_UNIQUE_ONLY))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Iterator over hash map entries.
+ *
+ * @param cls closure
+ * @param key current key code
+ * @param value value in the hash map
+ * @return GNUNET_YES if we should continue to
+ * iterate,
+ * GNUNET_NO if not.
+ */
+static int
+reversemap_cleanup_iterator (void *cls, uint32_t key, void *value)
+{
+ struct ReverseAddressMap *rmap = cls;
+
+ GNUNET_break (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap32_remove (rmap->map, key,
value));
+ return GNUNET_YES;
+}
+
+
+/**
+ * Create a reverse mapping from IP addresses to instance addresses
+ *
+ * @param m the address map
+ * @return reverse address map; NULL upon error
+ */
+struct ReverseAddressMap *
+addressmap_create_reverse_mapping (AddressMap *m)
+{
+ struct ReverseAddressMap *rmap;
+ unsigned int cnt;
+
+ rmap = GNUNET_malloc (sizeof (struct ReverseAddressMap));
+ rmap->map = GNUNET_CONTAINER_multihashmap32_create (m->size * 3);
+ if (GNUNET_SYSERR == addressmap_iterate_instance_address_infos
+ (m, &reversemap_address_iterator, rmap))
+ {
+ reverse_map_destroy (rmap);
+ return NULL;
+ }
+ return rmap;
+}
+
+
+/**
+ * Destroy a reverse address map
+ *
+ * @param rmap the reverse address map
+ */
+void
+reverse_map_destroy (struct ReverseAddressMap *rmap)
+{
+ GNUNET_break (GNUNET_SYSERR !=
+ GNUNET_CONTAINER_multihashmap32_iterate (rmap->map,
+
&reversemap_cleanup_iterator,
+ rmap));
+ GNUNET_CONTAINER_multihashmap32_destroy (rmap->map);
+ GNUNET_free (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)
+{
+ return GNUNET_CONTAINER_multihashmap32_contains (rmap->map, (uint32_t) ip);
+}
Modified: msh/src/addressmap.h
===================================================================
--- msh/src/addressmap.h 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/addressmap.h 2013-08-05 15:46:18 UTC (rev 28406)
@@ -253,6 +253,31 @@
addressmap_print (AddressMap *m);
+/**
+ * Handle for reverse mapping from IP addresses to instance addresses
+ */
+struct ReverseAddressMap;
+
+
+/**
+ * Create a reverse mapping from IP addresses to instance addresses
+ *
+ * @param m the address map
+ * @return reverse address map; NULL upon error
+ */
+struct ReverseAddressMap *
+addressmap_create_reverse_mapping (AddressMap *m);
+
+
+/**
+ * Destroy a reverse address map
+ *
+ * @param rmap the reverse address map
+ */
+void
+reverse_map_destroy (struct ReverseAddressMap *rmap);
+
+
#endif /* ADDRESSMAP_H_ */
/* End of addressmap.h */
Modified: msh/src/common.h
===================================================================
--- msh/src/common.h 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/common.h 2013-08-05 15:46:18 UTC (rev 28406)
@@ -23,6 +23,9 @@
GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "close"); \
} while (0)
+
+#define MSHD_SOCK_NAME "MSHD_SOCK"
+
#endif /* COMMON_H_ */
/* End of common.h */
Added: msh/src/mshd-server.c
===================================================================
--- msh/src/mshd-server.c (rev 0)
+++ msh/src/mshd-server.c 2013-08-05 15:46:18 UTC (rev 28406)
@@ -0,0 +1,211 @@
+/**
+ * @file mshd-server.c
+ * @brief functions implementing server functionalities of MSH daemon. We
+ * listen for commands from locally started MSH instances and also for
+ * executing commands given by remote MSH daemons.
+ * @author Sree Harsha Totakura <address@hidden>
+ */
+
+#include "common.h"
+#include <gnunet/gnunet_util_lib.h>
+#include "mshd.h"
+#include "addressmap.h"
+
+/**
+ * server handle for accepting requests from local MSH instances
+ */
+static struct GNUNET_SERVER_Handle *local_serv;
+
+/**
+ * server handle for acceptiong requests from remote MSHD instances
+ */
+static struct GNUNET_SERVER_Handle *daemon_serv;
+
+/**
+ * Our UID. We use this for access checks
+ */
+uid_t our_uid;
+
+/**
+ * Our GID. We use this for access checks
+ */
+uid_t our_gid;
+
+
+/**
+ * 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_client_runcmd (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader* message)
+{
+ struct MSH_MSG_RunCmdStatus *reply;
+ const struct MSH_MSG_RunCmd *msg;
+ const struct MSH_MSG_RunCmd *forward;
+ const char *emsg;
+ in_addr_t ip;
+ uint16_t size;
+ uint16_t emsg_len;
+
+ emsg = NULL;
+ msg = (const struct MSH_MSG_RunCmd *) message;
+ ip = (in_addr_t) 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 */
+
+
+ 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);
+ if (NULL != emsg)
+ (void) memcpy (reply->emsg, emsg, emsg_len);
+}
+
+
+/**
+ * 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_client_cmd_stream_stdin (void *cls,
+ struct GNUNET_SERVER_Client *client,
+ const struct GNUNET_MessageHeader* message)
+{
+ const struct MSH_MSG_CmdIO *msg;
+
+ msg = (const struct MSH_MSG_CmdIO *) message;
+ /* FIXME: forward message to the responsible MSHD */
+ GNUNET_break (0);
+}
+
+
+/**
+ * Function to call for access control checks.
+ *
+ * @param cls closure
+ * @param ucred credentials, if available, otherwise NULL
+ * @param addr address
+ * @param addrlen length of address
+ * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
+ * for unknown address family (will be denied).
+ */
+static int
+check_local_access (void *cls,
+ const struct GNUNET_CONNECTION_Credentials *cred,
+ const struct sockaddr * addr, socklen_t addrlen)
+{
+ if ((our_uid != cred->uid) && (our_gid != cred->gid))
+ return GNUNET_NO;
+ return GNUNET_YES;
+}
+
+
+int
+init_local_server (const char *unixpath)
+{
+ 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},
+ {NULL, NULL, 0, 0}
+ };
+ struct sockaddr *saddrs[] = {
+ ((struct sockaddr *) &saddr),
+ NULL
+ };
+ socklen_t saddr_lens[] = {
+ sizeof (struct sockaddr_un),
+ 0
+ };
+
+ our_uid = getuid ();
+ our_gid = getgid ();
+ saddr.sun_family = AF_UNIX;
+ (void) strcpy (saddr.sun_path, unixpath);
+ local_serv = GNUNET_SERVER_create (&check_local_access, NULL,
+ saddrs,
+ saddr_lens,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES);
+ if (NULL == local_serv)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+ GNUNET_SERVER_add_handlers (local_serv, handlers);
+ return GNUNET_OK;
+}
+
+
+void
+shutdown_local_server ()
+{
+ GNUNET_SERVER_destroy (local_serv);
+}
+
+
+/**
+ * Function to call for access control checks.
+ *
+ * @param cls closure
+ * @param ucred credentials, if available, otherwise NULL
+ * @param addr address
+ * @param addrlen length of address
+ * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
+ * for unknown address family (will be denied).
+ */
+static int
+check_remote_access (void *cls,
+ const struct GNUNET_CONNECTION_Credentials *cred,
+ const struct sockaddr * addr, socklen_t addrlen)
+{
+ return GNUNET_YES;
+}
+
+
+/**
+ * Initialises the server which spawns processes and forwards it stdin and
stdout
+ *
+ * @param h the network handle of the socket to listen for incoming connections
+ */
+int
+init_daemon_server (struct GNUNET_NETWORK_Handle *h)
+{
+ static struct GNUNET_NETWORK_Handle *lsocks[] = {
+ NULL,
+ NULL
+ };
+
+ lsocks[0] = h;
+ daemon_serv = GNUNET_SERVER_create_with_sockets (&check_remote_access,
+ NULL,
+ lsocks,
+
GNUNET_TIME_UNIT_FOREVER_REL,
+ GNUNET_YES);
+ return (NULL == daemon_serv) ? GNUNET_SYSERR: GNUNET_OK;
+}
+
+
+void
+shutdown_daemon_server ()
+{
+ GNUNET_SERVER_destroy (daemon_serv);
+}
Modified: msh/src/mshd.c
===================================================================
--- msh/src/mshd.c 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/mshd.c 2013-08-05 15:46:18 UTC (rev 28406)
@@ -132,6 +132,11 @@
AddressMap *addrmap;
/**
+ * Reverse mapping of the address map
+ */
+struct ReverseAddressMap *rmap;
+
+/**
* Rank of this process
*/
int rank;
@@ -168,21 +173,6 @@
static in_addr_t *s_addrs;
/**
- * Signal handler for SIGINT
- */
-static struct GNUNET_SIGNAL_Context *shc_int;
-
-/**
- * Signal handler for SIGTERM
- */
-static struct GNUNET_SIGNAL_Context *shc_term;
-
-/**
- * Pipe used to communicate shutdown via signal.
- */
-static struct GNUNET_DISK_PipeHandle *sigpipe;
-
-/**
* network handle for the listen socket
*/
static struct GNUNET_NETWORK_Handle *listen_socket;
@@ -229,6 +219,21 @@
static struct ReadContext *rtail;
/**
+ * arguments representing the command to run and its arguments
+ */
+static char **run_args;
+
+/**
+ * the process handle for the command to run
+ */
+static struct GNUNET_OS_Process *process;
+
+/**
+ * Random hashcode for authentication
+ */
+struct GNUNET_HashCode shash;
+
+/**
* Number of IP addresses
*/
static unsigned int nips;
@@ -387,7 +392,17 @@
}
LOG_DEBUG ("Verification phase complete; commencing reduction phase\n");
GNUNET_break (GNUNET_OK == reduce_ntree ());
- addressmap_print (addrmap);
+ addressmap_print (addrmap);
+ rmap = addressmap_create_reverse_mapping (addrmap);
+ if (0 == rank)
+ {
+ const char *unixpath = "/tmp/mshd.sock";
+
+ (void) unlink (unixpath);
+ setenv (MSHD_SOCK_NAME, unixpath, 1);
+ GNUNET_break (0); /* FIXME: start process */
+ /* process = GNUNET_OS_start_process_vap () */
+ }
}
@@ -812,28 +827,6 @@
/**
- * Event callback for handling shutdown signals
- *
- * @param signal the signal
- * @param flags EV_* flags
- * @param cls pointer to the corresponding Task
- */
-static void
-sighandler_shutdown ()
-{
- static char c;
- int old_errno; /* back-up errno */
-
- old_errno = errno;
- GNUNET_break (1 ==
- GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
- (sigpipe, GNUNET_DISK_PIPE_END_WRITE),
- &c, sizeof (c)));
- errno = old_errno;
-}
-
-
-/**
* Task for running a round
*
* @param cls NULL
@@ -849,24 +842,39 @@
/**
- * Function called whenever a signal is written to the signal pipe
+ * Function to copy NULL terminated list of arguments
*
- * @param cls NULL
- * @param tc scheduler task context
+ * @param argv the NULL terminated list of arguments. Cannot be NULL.
+ * @return the copied NULL terminated arguments
*/
+static char **
+copy_argv (char *const *argv)
+{
+ char **argv_dup;
+ unsigned int argp;
+
+ GNUNET_assert (NULL != argv);
+ for (argp = 0; NULL != argv[argp]; argp++) ;
+ argv_dup = GNUNET_malloc (sizeof (char *) * (argp + 1));
+ for (argp = 0; NULL != argv[argp]; argp++)
+ argv_dup[argp] = strdup (argv[argp]);
+ return argv_dup;
+}
+
+
+/**
+ * Frees the given NULL terminated arguments
+ *
+ * @param argv the NULL terminated list of arguments
+ */
static void
-sigpipe_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+free_argv (char **argv)
{
- const struct GNUNET_DISK_FileHandle *pr;
- char c[16];
+ unsigned int argp;
- sigread_task = GNUNET_SCHEDULER_NO_TASK;
- if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
- return;
- pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
- GNUNET_break (0 < GNUNET_DISK_file_read (pr, &c, sizeof (c)));
- LOG_DEBUG ("Caught signal. Exiting.\n");
- GNUNET_SCHEDULER_shutdown ();
+ for (argp = 0; NULL != argv[argp]; argp++)
+ GNUNET_free (argv[argp]);
+ GNUNET_free (argv);
}
@@ -898,12 +906,15 @@
LOG_ERROR ("Round width should be less than the number of processes\n");
return;
}
+ for (cnt = 0; NULL != args[cnt]; cnt++);
+ if (0 == cnt)
+ {
+ LOG_ERROR ("Need a command to execute\n");
+ return;
+ }
+ run_args = copy_argv (args);
bitmap = bitmap_create (rwidth);
addrmap = addressmap_create (nproc);
- fh = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
- sigread_task =
- GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, fh,
- &sigpipe_read, NULL);
addrlen = sizeof (struct sockaddr_in);
(void) memset (&addr, 0, addrlen);
addr.sin_addr.s_addr = INADDR_ANY; /* bind to all available addresses */
@@ -952,26 +963,11 @@
int c;
ret = 1;
- rwidth = 1;
- if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv,
- &argc, (char *const **)
&argv))
- {
- GNUNET_break (0);
- return 2;
- }
- if (NULL == (sigpipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO,
- GNUNET_NO, GNUNET_NO)))
- {
- GNUNET_break (0);
- ret = GNUNET_SYSERR;
- return 1;
- }
- shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
- shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
+ rwidth = 1;
if (MPI_SUCCESS != MPI_Init(&argc, &argv))
{
LOG_ERROR ("Failed to initialise MPI\n");
- goto uninstall_sighandlers;
+ return 1;
}
if (MPI_SUCCESS != MPI_Comm_size (MPI_COMM_WORLD, &nproc))
{
@@ -1003,19 +999,12 @@
bitmap = NULL;
}
if (NULL != addrmap)
- {
addressmap_destroy (addrmap);
- addrmap = NULL;
- }
+ if (NULL != rmap)
+ reverse_map_destroy (rmap);
GNUNET_break (MPI_SUCCESS == MPI_Finalize());
GNUNET_free_non_null (s_addrs);
+ if (NULL != run_args)
+ free_argv (run_args);
LOG_DEBUG ("Returning\n");
-
- uninstall_sighandlers:
- if (NULL != listen_socket)
- GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (listen_socket));
- GNUNET_DISK_pipe_close (sigpipe);
- GNUNET_SIGNAL_handler_uninstall (shc_int);
- GNUNET_SIGNAL_handler_uninstall (shc_term);
- return ret;
}
Modified: msh/src/mshd.h
===================================================================
--- msh/src/mshd.h 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/mshd.h 2013-08-05 15:46:18 UTC (rev 28406)
@@ -16,6 +16,11 @@
extern AddressMap *addrmap;
/**
+ * Reverse mapping of the address map
+ */
+extern struct ReverseAddressMap *rmap;
+
+/**
* Our instance rank
*/
extern int rank;
@@ -30,5 +35,10 @@
*/
extern unsigned int nproc;
+/**
+ * Random hashcode for authentication
+ */
+extern struct GNUNET_HashCode shash;
+
#endif /* MSHD_H_ */
/* End of mshd.h */
Modified: msh/src/mtypes.h
===================================================================
--- msh/src/mtypes.h 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/mtypes.h 2013-08-05 15:46:18 UTC (rev 28406)
@@ -139,6 +139,24 @@
/**
+ * Message indicating the status of the RunCmd message
+ */
+struct MSH_MSG_RunCmdStatus
+{
+ /**
+ * header
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * The error message, if any. Absence of error message signifies that the
+ * RunCmd message is accepted and will be forwarded accordingly
+ */
+ char emsg[0];
+};
+
+
+/**
* Message for sending STDIN for the remote command to MSHD
*/
struct MSH_MSG_CmdIO
Modified: msh/src/reduce.c
===================================================================
--- msh/src/reduce.c 2013-08-05 15:02:19 UTC (rev 28405)
+++ msh/src/reduce.c 2013-08-05 15:46:18 UTC (rev 28406)
@@ -107,5 +107,12 @@
GNUNET_free (buf);
GNUNET_free (iaddr_msgs);
}
+ GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE, &shash);
+ if (MPI_SUCCESS != MPI_Bcast (&shash, sizeof (struct GNUNET_HashCode),
+ MPI_BYTE, 0, MPI_COMM_WORLD))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
return GNUNET_OK;
}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r28406 - msh/src,
gnunet <=