gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r27680 - in msh: . src


From: gnunet
Subject: [GNUnet-SVN] r27680 - in msh: . src
Date: Fri, 28 Jun 2013 16:21:08 +0200

Author: harsha
Date: 2013-06-28 16:21:08 +0200 (Fri, 28 Jun 2013)
New Revision: 27680

Added:
   msh/src/common.h
   msh/src/mshd.c
   msh/src/util.c
   msh/src/util.h
Modified:
   msh/
   msh/configure.ac
   msh/src/
   msh/src/Makefile.am
Log:
- scan interfaces


Index: msh
===================================================================
--- msh 2013-06-28 14:16:32 UTC (rev 27679)
+++ msh 2013-06-28 14:21:08 UTC (rev 27680)

Property changes on: msh
___________________________________________________________________
Added: svn:ignore
## -0,0 +1,14 ##
+configure
+Makefile.in
+config.log
+config.cache
+config.status
+depcomp
+stamp-h1
+config.h
+config.h.in
+autom4te*
+missing
+aclocal.m4
+Makefile
+install-sh
Modified: msh/configure.ac
===================================================================
--- msh/configure.ac    2013-06-28 14:16:32 UTC (rev 27679)
+++ msh/configure.ac    2013-06-28 14:21:08 UTC (rev 27680)
@@ -1,3 +1,4 @@
+
 #                                               -*- Autoconf -*-
 # Process this file with autoconf to produce a configure script.
 
@@ -13,12 +14,17 @@
 # Checks for libraries.
 AC_CHECK_LIB([mpi],[MPI_Init],,[AC_MSG_ERROR([cannot find MPI libraries])])
 # Checks for header files.
-AC_CHECK_HEADERS([stdlib.h string.h unistd.h mpi.h])
+AC_CHECK_HEADERS([stdlib.h string.h unistd.h mpi.h netinet/in.h net/if.h 
ifaddrs.h \
+sys/socket.h sys/types.h error.h errno.h limits.h],,
+ [AC_MSG_ERROR([a required standard UNIX header is missing])])
 
 # Checks for typedefs, structures, and compiler characteristics.
 
 # Checks for library functions.
 AC_FUNC_MALLOC
+AC_FUNC_REALLOC
+AC_CHECK_FUNCS([strdup strerror getnameinfo getifaddrs freeifaddrs],,
+               [AC_MSG_ERROR([a required C library function is missing])])
 
 AC_CONFIG_FILES([Makefile
                src/Makefile])

Index: msh/src
===================================================================
--- msh/src     2013-06-28 14:16:32 UTC (rev 27679)
+++ msh/src     2013-06-28 14:21:08 UTC (rev 27680)

Property changes on: msh/src
___________________________________________________________________
Added: svn:ignore
## -0,0 +1,6 ##
+Makefile.in
+Makefile
+.deps
+mshd
+mping
+
Modified: msh/src/Makefile.am
===================================================================
--- msh/src/Makefile.am 2013-06-28 14:16:32 UTC (rev 27679)
+++ msh/src/Makefile.am 2013-06-28 14:21:08 UTC (rev 27680)
@@ -1,3 +1,5 @@
-bin_PROGRAMS = mping
+bin_PROGRAMS = mping mshd
 
 mping_SOURCES = mping.c
+
+mshd_SOURCES = mshd.c util.c util.h

Added: msh/src/common.h
===================================================================
--- msh/src/common.h                            (rev 0)
+++ msh/src/common.h    2013-06-28 14:21:08 UTC (rev 27680)
@@ -0,0 +1,55 @@
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
+#endif
+
+#include "../config.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <limits.h>
+
+#include <errno.h>
+#include <string.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <net/if.h>
+#include <ifaddrs.h>
+#include <netdb.h>
+
+#define MSH_OK 1
+
+#define MSH_SYSERR -1
+
+#define MSH_YES 1
+
+#define MSH_NO 0
+
+#define LOG_DEBUG(...)                          \
+  fprintf (stderr, __VA_ARGS__)
+
+#define LOG_ERROR(...)                          \
+  fprintf (stderr, __VA_ARGS__)
+
+#define LOG_STRERROR(fname)                     \
+  LOG_ERROR ("%s:%d: %s() failed -- %s\n", __FILE__, __LINE__, fname, 
strerror(errno))
+
+
+/**
+ * Use this for internal assertion violations that are
+ * not fatal (can be handled) but should not occur.
+ */
+#define MSH_break(cond)  do { if (! (cond)) { LOG_ERROR ("Assertion failed at 
%s:%d.\n", __FILE__, __LINE__); } } while(0)
+
+
+/**
+ * Use this for fatal errors that cannot be handled
+ */
+#define MSH_assert_at(cond, file, line) do { if (! (cond)) { LOG_ERROR 
("Assertion failed at %s:%d.\n", file, line); abort(); } } while(0)
+
+/**
+ * Use this for fatal errors that cannot be handled
+ */
+#define MSH_assert(cond) MSH_assert_at (cond, __FILE__, __LINE__)

Added: msh/src/mshd.c
===================================================================
--- msh/src/mshd.c                              (rev 0)
+++ msh/src/mshd.c      2013-06-28 14:21:08 UTC (rev 27680)
@@ -0,0 +1,82 @@
+#include "common.h"
+#include <mpi.h>
+#include "util.h"
+
+/**
+ * The number of total mshd processes 
+ */
+static int nproc;
+
+/**
+ * Rank of this process
+ */
+static int rank;
+
+/**
+ * Array of the string representation of our IP addresses
+ */
+static char **ip_addr_str;
+
+/**
+ * Number of IP addresses
+ */
+static unsigned int nips;
+
+/**
+ * Callback function invoked for each interface found.
+ *
+ * @param cls closure
+ * @param name name of the interface (can be NULL for unknown)
+ * @param isDefault is this presumably the default interface
+ * @param addr address of this interface (can be NULL for unknown or 
unassigned)
+ * @param broadcast_addr the broadcast address (can be NULL for unknown or 
unassigned)
+ * @param netmask the network mask (can be NULL for unknown or unassigned))
+ * @param addrlen length of the address
+ * @return MSH_OK to continue iteration, MSH_SYSERR to abort
+ */
+static int net_if_processor (void *cls, const char *name,
+                               int isDefault,
+                             const struct sockaddr *addr,
+                             const struct sockaddr *broadcast_addr,
+                             const struct sockaddr *netmask, 
+                             socklen_t addrlen)
+{
+  char hostip[NI_MAXHOST];
+
+  if (sizeof (struct sockaddr_in) != addrlen)
+    return MSH_OK;           /* Only consider IPv4 for now */
+  if (0 !=
+      getnameinfo (addr, addrlen, hostip, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
+    LOG_STRERROR ("getnameinfo");
+  MSH_array_append (ip_addr_str, nips, strdup (hostip));
+  LOG_DEBUG ("%d: Found IP: %s\n", rank, hostip);
+  return MSH_OK;
+}
+
+
+int 
+main (int argc, char **argv)
+{
+  if (MPI_SUCCESS != MPI_Init(&argc, &argv))
+  {
+    LOG_ERROR ("Failed to initialise MPI\n");
+    return 1;
+  }
+  if (MPI_SUCCESS != MPI_Comm_size (MPI_COMM_WORLD, &nproc))
+  {
+    LOG_ERROR ("Cannot determine the number of mshd processes\n");
+    goto fail;
+  }
+  if (MPI_SUCCESS != MPI_Comm_rank (MPI_COMM_WORLD, &rank))
+  {
+    LOG_ERROR ("Cannot determine our MPI rank\n");
+    goto fail;
+  }
+  GNUNET_OS_network_interfaces_list (&net_if_processor, NULL);
+  if (0 == nips)
+    LOG_ERROR ("No IP addresses found\n");
+
+ fail:
+  MSH_break (MPI_SUCCESS == MPI_Finalize());
+  return 1;
+}

Added: msh/src/util.c
===================================================================
--- msh/src/util.c                              (rev 0)
+++ msh/src/util.c      2013-06-28 14:21:08 UTC (rev 27680)
@@ -0,0 +1,87 @@
+#include "common.h"
+#include "util.h"
+
+#define DEFAULT_INTERFACE "eth0"
+
+/**
+ * @brief Enumerate all network interfaces
+ *
+ * @param proc the callback function
+ * @param proc_cls closure for proc
+ */
+void
+GNUNET_OS_network_interfaces_list (NetworkInterfaceProcessor proc,
+                                   void *proc_cls)
+{
+  struct ifaddrs *ifa_first;
+  struct ifaddrs *ifa_ptr;
+  socklen_t alen;
+  
+  if (getifaddrs (&ifa_first) == 0)
+  {
+    for (ifa_ptr = ifa_first; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next)
+    {
+      if (ifa_ptr->ifa_name != NULL && ifa_ptr->ifa_addr != NULL &&
+          (ifa_ptr->ifa_flags & IFF_UP) != 0)
+      {
+        if ((ifa_ptr->ifa_addr->sa_family != AF_INET) &&
+            (ifa_ptr->ifa_addr->sa_family != AF_INET6))
+          continue;
+        if (ifa_ptr->ifa_addr->sa_family == AF_INET)
+          alen = sizeof (struct sockaddr_in);
+        else
+          alen = sizeof (struct sockaddr_in6);
+        if (MSH_OK !=
+            proc (proc_cls, ifa_ptr->ifa_name,
+                  0 == strcmp (ifa_ptr->ifa_name, DEFAULT_INTERFACE),
+                  ifa_ptr->ifa_addr, ifa_ptr->ifa_broadaddr,
+                  ifa_ptr->ifa_netmask, alen))
+          break;
+      }
+    }
+    freeifaddrs (ifa_first);
+  }
+}
+
+
+/**
+ * Grow an array.  Grows old by (*oldCount-newCount)*elementSize bytes
+ * and sets *oldCount to newCount.
+ *
+ * @param old address of the pointer to the array
+ *        *old may be NULL
+ * @param elementSize the size of the elements of the array
+ * @param oldCount address of the number of elements in the *old array
+ * @param newCount number of elements in the new array, may be 0
+ * @param filename where in the code was the call to GNUNET_array_grow
+ * @param linenumber where in the code was the call to GNUNET_array_grow
+ */
+void
+MSH_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
+            unsigned int newCount, const char *filename, int linenumber)
+{
+  void *tmp;
+  size_t size;
+
+  MSH_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
+  size = newCount * elementSize;
+  if (size == 0)
+  {
+    tmp = NULL;
+  }
+  else
+  {
+    tmp = malloc (size);
+    memset (tmp, 0, size);      /* client code should not rely on this, 
though... */
+    if (*oldCount > newCount)
+      *oldCount = newCount;     /* shrink is also allowed! */
+    memcpy (tmp, *old, elementSize * (*oldCount));
+  }
+
+  if (*old != NULL)
+  {
+    free (*old);
+  }
+  *old = tmp;
+  *oldCount = newCount;
+}

Added: msh/src/util.h
===================================================================
--- msh/src/util.h                              (rev 0)
+++ msh/src/util.h      2013-06-28 14:21:08 UTC (rev 27680)
@@ -0,0 +1,102 @@
+#ifndef MSH_UTIL_H
+#define MSH_UTIL_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+
+/**
+ * Callback function invoked for each interface found.
+ *
+ * @param cls closure
+ * @param name name of the interface (can be NULL for unknown)
+ * @param isDefault is this presumably the default interface
+ * @param addr address of this interface (can be NULL for unknown or 
unassigned)
+ * @param broadcast_addr the broadcast address (can be NULL for unknown or 
unassigned)
+ * @param netmask the network mask (can be NULL for unknown or unassigned))
+ * @param addrlen length of the address
+ * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
+ */
+typedef int (*NetworkInterfaceProcessor) (void *cls, const char *name,
+                                          int isDefault,
+                                          const struct sockaddr *addr,
+                                          const struct sockaddr 
*broadcast_addr,
+                                          const struct sockaddr *netmask, 
+                                          socklen_t addrlen);
+
+
+/**
+ * @brief Enumerate all network interfaces
+ *
+ * @param proc the callback function
+ * @param proc_cls closure for proc
+ */
+void
+GNUNET_OS_network_interfaces_list (NetworkInterfaceProcessor proc,
+                                   void *proc_cls);
+
+
+/**
+ * Grow an array, the new elements are zeroed out.
+ * Grows old by (*oldCount-newCount)*elementSize
+ * bytes and sets *oldCount to newCount.
+ *
+ * Don't call GNUNET_xgrow_ directly. Use the GNUNET_array_grow macro.
+ *
+ * @param old address of the pointer to the array
+ *        *old may be NULL
+ * @param elementSize the size of the elements of the array
+ * @param oldCount address of the number of elements in the *old array
+ * @param newCount number of elements in the new array, may be 0 (then *old 
will be NULL afterwards)
+ * @param filename where is this call being made (for debugging)
+ * @param linenumber line where this call is being made (for debugging)
+ */
+void
+MSH_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount,
+            unsigned int newCount, const char *filename, int linenumber);
+
+
+/**
+ * Grow a well-typed (!) array.  This is a convenience
+ * method to grow a vector <tt>arr</tt> of size <tt>size</tt>
+ * to the new (target) size <tt>tsize</tt>.
+ * <p>
+ *
+ * Example (simple, well-typed stack):
+ *
+ * <pre>
+ * static struct foo * myVector = NULL;
+ * static int myVecLen = 0;
+ *
+ * static void push(struct foo * elem) {
+ *   GNUNET_array_grow(myVector, myVecLen, myVecLen+1);
+ *   memcpy(&myVector[myVecLen-1], elem, sizeof(struct foo));
+ * }
+ *
+ * static void pop(struct foo * elem) {
+ *   if (myVecLen == 0) die();
+ *   memcpy(elem, myVector[myVecLen-1], sizeof(struct foo));
+ *   GNUNET_array_grow(myVector, myVecLen, myVecLen-1);
+ * }
+ * </pre>
+ *
+ * @param arr base-pointer of the vector, may be NULL if size is 0;
+ *        will be updated to reflect the new address. The TYPE of
+ *        arr is important since size is the number of elements and
+ *        not the size in bytes
+ * @param size the number of elements in the existing vector (number
+ *        of elements to copy over)
+ * @param tsize the target size for the resulting vector, use 0 to
+ *        free the vector (then, arr will be NULL afterwards).
+ */
+#define MSH_array_grow(arr,size,tsize) MSH_xgrow_((void**)&arr, 
sizeof(arr[0]), &size, tsize, __FILE__, __LINE__)
+
+
+/**
+ * Append an element to a list (growing the
+ * list by one).
+ */
+#define MSH_array_append(arr,size,element) do { 
MSH_array_grow(arr,size,size+1); arr[size-1] = element; } while(0)
+
+
+#endif /* #ifndef MSH_UTIL_H */
+/* end of MSH_UTIL_H */




reply via email to

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