Replace mesh with new version
[oweals/gnunet.git] / src / vpn / gnunet-helper-vpn.c
index 3250141fb205d14af1b854541f2aad657c7e0766..7c09827eedf9037c826163cd707fff9f9d1e1d01 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010 Christian Grothoff
+     (C) 2010, 2012 Christian Grothoff
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 */
 
 /**
- * @file vpn/gnunet-daemon-vpn.c
- * @brief the helper for various vpn-daemons. Opens a virtual network-interface,
+ * @file vpn/gnunet-helper-vpn.c
+ * @brief the helper for the VPN service. Opens a virtual network-interface,
  * sends data received on the if to stdout, sends data received on stdin to the
  * interface
  * @author Philipp Tölke
+ * @author Christian Grothoff
  *
  * The following list of people have reviewed this code and considered
  * it safe since the last modification (if you reviewed it, please
  */
 #include "gnunet_protocols.h"
 
+/**
+ * Should we print (interesting|debug) messages that can happen during
+ * normal operation?
+ */
+#define DEBUG GNUNET_NO
+
 /**
  * Maximum size of a GNUnet message (GNUNET_SERVER_MAX_MESSAGE_SIZE)
  */
@@ -91,6 +98,7 @@ init_tun (char *dev)
   if (fd >= FD_SETSIZE)
   {
     fprintf (stderr, "File descriptor to large: %d", fd);
+    (void) close (fd);
     return -1;
   }
 
@@ -419,7 +427,10 @@ run (int fd_tun)
 
         if (-1 == written)
         {
-          fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
+#if !DEBUG
+         if (errno != EPIPE)
+#endif
+           fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
           shutdown (fd_tun, SHUT_RD);
           shutdown (1, SHUT_WR);
           read_open = 0;
@@ -450,7 +461,9 @@ run (int fd_tun)
         }
         else if (0 == bufin_size)
         {
+#if DEBUG
           fprintf (stderr, "EOF on stdin\n");
+#endif
           shutdown (0, SHUT_RD);
           shutdown (fd_tun, SHUT_WR);
           write_open = 0;
@@ -518,10 +531,10 @@ PROCESS_BUFFER:
  * @param argc must be 6
  * @param argv 0: binary name (gnunet-helper-vpn)
  *             1: tunnel interface name (gnunet-vpn)
- *             2: IPv6 address (::1)
- *             3: IPv6 netmask length in bits (64)
- *             4: IPv4 address (1.2.3.4)
- *             5: IPv4 netmask (255.255.0.0)
+ *             2: IPv6 address (::1), "-" to disable
+ *             3: IPv6 netmask length in bits (64), ignored if #2 is "-"
+ *             4: IPv4 address (1.2.3.4), "-" to disable
+ *             5: IPv4 netmask (255.255.0.0), ignored if #4 is "-"
  */
 int
 main (int argc, char **argv)
@@ -541,10 +554,16 @@ main (int argc, char **argv)
 
   if (-1 == (fd_tun = init_tun (dev)))
   {
-    fprintf (stderr, "Fatal: could not initialize tun-interface\n");
+    fprintf (stderr, "Fatal: could not initialize tun-interface `%s'  with IPv6 %s/%s and IPv4 %s/%s\n",
+            dev,
+            argv[2],
+            argv[3],
+            argv[4],
+            argv[5]);
     return 1;
   }
 
+  if (0 != strcmp (argv[2], "-"))
   {
     const char *address = argv[2];
     long prefix_len = atol (argv[3]);
@@ -558,15 +577,16 @@ main (int argc, char **argv)
     set_address6 (dev, address, prefix_len);
   }
 
+  if (0 != strcmp (argv[4], "-"))
   {
     const char *address = argv[4];
     const char *mask = argv[5];
 
     set_address4 (dev, address, mask);
   }
-
-#ifdef HAVE_SETRESUID
+  
   uid_t uid = getuid ();
+#ifdef HAVE_SETRESUID
   if (0 != setresuid (uid, uid, uid))
   {
     fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));