add function conv param string
[oweals/gnunet.git] / src / vpn / gnunet-helper-vpn.c
index 77512832d3653f6a5b3b7798735683f7049d6ed1..0519680ebee988e978c66fe0256f2460fbd133f1 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010 Christian Grothoff
+     Copyright (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
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
- * @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
@@ -37,6 +38,7 @@
 /**
  * Need 'struct GNUNET_MessageHeader'.
  */
+#include "gnunet_crypto_lib.h"
 #include "gnunet_common.h"
 
 /**
@@ -109,7 +111,9 @@ init_tun (char *dev)
 
   if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr))
   {
-    fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
+    fprintf (stderr,
+             "Error with ioctl on `%s': %s\n",
+             "/dev/net/tun",
              strerror (errno));
     (void) close (fd);
     return -1;
@@ -141,14 +145,17 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
   sa6.sin6_family = AF_INET6;
   if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr))
   {
-    fprintf (stderr, "Failed to parse address `%s': %s\n", address,
-             strerror (errno));
+    fprintf (stderr,
+             "Failed to parse IPv6 address `%s'\n",
+             address);
     exit (1);
   }
 
   if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0)))
   {
-    fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
+    fprintf (stderr,
+             "Error creating socket: %s\n",
+             strerror (errno));
     exit (1);
   }
 
@@ -234,14 +241,17 @@ set_address4 (const char *dev, const char *address, const char *mask)
    */
   if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr))
   {
-    fprintf (stderr, "Failed to parse address `%s': %s\n", address,
-             strerror (errno));
+    fprintf (stderr,
+             "Failed to parse IPv4 address `%s'\n",
+             address);
     exit (1);
   }
 
   if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0)))
   {
-    fprintf (stderr, "Error creating socket: %s\n", strerror (errno));
+    fprintf (stderr,
+             "Error creating socket: %s\n",
+             strerror (errno));
     exit (1);
   }
 
@@ -252,7 +262,10 @@ set_address4 (const char *dev, const char *address, const char *mask)
    */
   if (-1 == ioctl (fd, SIOCSIFADDR, &ifr))
   {
-    fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno));
+    fprintf (stderr,
+             "ioctl failed at %d: %s\n",
+             __LINE__,
+             strerror (errno));
     (void) close (fd);
     exit (1);
   }
@@ -263,8 +276,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
   addr = (struct sockaddr_in *) &(ifr.ifr_netmask);
   if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr))
   {
-    fprintf (stderr, "Failed to parse address `%s': %s\n", mask,
-             strerror (errno));
+    fprintf (stderr,
+             "Failed to parse IPv4 address mask `%s'\n",
+             mask);
     (void) close (fd);
     exit (1);
   }
@@ -274,7 +288,9 @@ set_address4 (const char *dev, const char *address, const char *mask)
    */
   if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr))
   {
-    fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__,
+    fprintf (stderr,
+             "ioctl failed at line %d: %s\n",
+             __LINE__,
              strerror (errno));
     (void) close (fd);
     exit (1);
@@ -344,7 +360,7 @@ run (int fd_tun)
   /* write refers to reading from stdin, writing to fd_tun */
   int write_open = 1;
 
-  while ((1 == read_open) || (1 == write_open))
+  while ((1 == read_open) && (1 == write_open))
   {
     FD_ZERO (&fds_w);
     FD_ZERO (&fds_r);
@@ -396,7 +412,9 @@ run (int fd_tun)
                   MAX_SIZE - sizeof (struct GNUNET_MessageHeader));
         if (-1 == buftun_size)
         {
-          fprintf (stderr, "read-error: %s\n", strerror (errno));
+          fprintf (stderr,
+                   "read-error: %s\n",
+                   strerror (errno));
           shutdown (fd_tun, SHUT_RD);
           shutdown (1, SHUT_WR);
           read_open = 0;
@@ -429,7 +447,9 @@ run (int fd_tun)
 #if !DEBUG
          if (errno != EPIPE)
 #endif
-           fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
+           fprintf (stderr,
+                     "write-error to stdout: %s\n",
+                     strerror (errno));
           shutdown (fd_tun, SHUT_RD);
           shutdown (1, SHUT_WR);
           read_open = 0;
@@ -452,7 +472,9 @@ run (int fd_tun)
         bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
         if (-1 == bufin_size)
         {
-          fprintf (stderr, "read-error: %s\n", strerror (errno));
+          fprintf (stderr,
+                   "read-error: %s\n",
+                   strerror (errno));
           shutdown (0, SHUT_RD);
           shutdown (fd_tun, SHUT_WR);
           write_open = 0;
@@ -530,10 +552,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)
@@ -553,10 +575,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]);
@@ -570,13 +598,14 @@ 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);
   }
-  
+
   uid_t uid = getuid ();
 #ifdef HAVE_SETRESUID
   if (0 != setresuid (uid, uid, uid))