In Windows, pipes, files and the console have to be accessed
[oweals/gnunet.git] / src / vpn / gnunet-service-vpn.c
index 219949bd31791115d9d6a46259d8836de8500ee0..1a46f0b5eb833c624b8dbb4263a4af73e7dc1864 100644 (file)
@@ -35,6 +35,7 @@
 #include "gnunet_statistics_service.h"
 #include "gnunet_constants.h"
 #include "gnunet_tun_lib.h"
+#include "gnunet_regex_lib.h"
 #include "vpn.h"
 #include "exit.h"
 
@@ -689,7 +690,6 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   if (NULL != (tnq = ts->tmq_head))
     ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel, 
                                                GNUNET_NO /* cork */, 
-                                               42 /* priority */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                NULL, 
                                                tnq->len,
@@ -742,7 +742,6 @@ send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
   if (NULL == ts->th)
     ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel, 
                                                GNUNET_NO /* cork */,
-                                               42 /* priority */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
                                                NULL, 
                                                tnq->len,
@@ -809,27 +808,45 @@ create_tunnel_to_destination (struct DestinationEntry *de,
   }
   else
   {
+    char *policy;
+
     switch (de->details.exit_destination.af)
     {
     case AF_INET:
-      GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
-                                               GNUNET_APPLICATION_TYPE_IPV4_GATEWAY);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Creating tunnel to exit peer for %s\n",
-                 "IPv4");
-     break;
+    {
+      char address[GNUNET_REGEX_IPV4_REGEXLEN];
+      GNUNET_REGEX_ipv4toregex (&de->details.exit_destination.ip.v4,
+                                "255.255.255.255", address);
+      GNUNET_asprintf (&policy, "%s%s%s",
+                       GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
+                       "4",
+                       address);
+      break;
+    }
     case AF_INET6:
-      GNUNET_MESH_peer_request_connect_by_type (ts->tunnel,
-                                               GNUNET_APPLICATION_TYPE_IPV6_GATEWAY);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Creating tunnel to exit peer for %s\n",
-                 "IPv6");
+    {
+      char address[GNUNET_REGEX_IPV6_REGEXLEN];
+      GNUNET_REGEX_ipv6toregex (&de->details.exit_destination.ip.v6,
+                                128, address);
+      GNUNET_asprintf (&policy, "%s%s%s",
+                       GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
+                       "6",
+                       address);
       break;
+    }
     default:
       GNUNET_assert (0);
       break;
     }
-  }  
+
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Requesting connect by string: %s\n", policy);
+
+    GNUNET_MESH_peer_request_connect_by_string (ts->tunnel, policy);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Creating tunnel to exit peer for policy `%s'\n",
+                policy);
+    GNUNET_free (policy);
+  }
   return ts;
 }
 
@@ -3004,31 +3021,6 @@ client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
 }
 
 
-/**
- * Test if the given AF is supported by this system.
- * 
- * @param af to test
- * @return GNUNET_OK if the AF is supported
- */
-static int
-test_af (int af)
-{
-  int s;
-
-  s = socket (af, SOCK_STREAM, 0);
-  if (-1 == s)
-  {
-    if (EAFNOSUPPORT == errno)
-      return GNUNET_NO;
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
-                        "socket");
-    return GNUNET_SYSERR;
-  }
-  close (s);
-  return GNUNET_OK;
-}
-
-
 /**
  * Main function that will be run by the scheduler.
  *
@@ -3065,16 +3057,21 @@ run (void *cls,
   char *ipv4mask;
   struct in_addr v4;
   struct in6_addr v6;
+  char *binary;
+
+  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
 
   if (GNUNET_YES !=
-      GNUNET_OS_check_helper_binary ("gnunet-helper-vpn"))
+      GNUNET_OS_check_helper_binary (binary))
   {
     fprintf (stderr,
             "`%s' is not SUID, refusing to run.\n",
             "gnunet-helper-vpn");
+    GNUNET_free (binary);
     global_ret = 1;
     return;
   }
+  GNUNET_free (binary);
   cfg = cfg_;
   stats = GNUNET_STATISTICS_create ("vpn", cfg);
   if (GNUNET_OK !=
@@ -3086,9 +3083,9 @@ run (void *cls,
                                             &max_tunnel_mappings))
     max_tunnel_mappings = 200;
 
-  destination_map = GNUNET_CONTAINER_multihashmap_create (max_destination_mappings * 2);
+  destination_map = GNUNET_CONTAINER_multihashmap_create (max_destination_mappings * 2, GNUNET_NO);
   destination_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2);
+  tunnel_map = GNUNET_CONTAINER_multihashmap_create (max_tunnel_mappings * 2, GNUNET_NO);
   tunnel_heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
 
 
@@ -3102,7 +3099,7 @@ run (void *cls,
     return;
   }
   vpn_argv[1] = ifname;
-  if (GNUNET_OK == test_af (AF_INET6))
+  if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET6))
   {
     if ( (GNUNET_SYSERR ==
          GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV6ADDR",
@@ -3142,7 +3139,7 @@ run (void *cls,
     vpn_argv[2] = GNUNET_strdup ("-");
     vpn_argv[3] = GNUNET_strdup ("-");
   }
-  if (GNUNET_OK == test_af (AF_INET))
+  if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET))
   {
     if ( (GNUNET_SYSERR ==
          GNUNET_CONFIGURATION_get_value_string (cfg, "vpn", "IPV4ADDR",
@@ -3177,13 +3174,14 @@ run (void *cls,
   vpn_argv[6] = NULL;
 
   mesh_handle =
-    GNUNET_MESH_connect (cfg_, 42 /* queue length */, NULL, 
+    GNUNET_MESH_connect (cfg_, NULL, 
                         &inbound_tunnel_cb, 
                         &tunnel_cleaner, 
                         mesh_handlers,
                         types);
-  helper_handle = GNUNET_HELPER_start ("gnunet-helper-vpn", vpn_argv,
-                                      &message_token, NULL);
+  helper_handle = GNUNET_HELPER_start (GNUNET_NO,
+                                      "gnunet-helper-vpn", vpn_argv,
+                                      &message_token, NULL, NULL);
   nc = GNUNET_SERVER_notification_context_create (server, 1);
   GNUNET_SERVER_add_handlers (server, service_handlers);
   GNUNET_SERVER_disconnect_notify (server, &client_disconnect, NULL);