changing time measurement from milliseconds to microseconds
[oweals/gnunet.git] / src / vpn / gnunet-service-vpn.c
index 2ab53047be6fff1aabe2b3d8855ba84329d109ef..e2cf3e5c9a162b8a03b8e4cee634ef454d421db8 100644 (file)
  */
 #define MAX_MESSAGE_QUEUE_SIZE 4
 
-/**
- * Maximum regex string length for use with GNUNET_REGEX_ipv4toregex
- */
-#define GNUNET_REGEX_IPV4_REGEXLEN 32 + 6
-
-/**
- * Maximum regex string length for use with GNUNET_REGEX_ipv6toregex
- */
-#define GNUNET_REGEX_IPV6_REGEXLEN 128 + 6
 
+#define PORT_VPN 42
 
 /**
  * State we keep for each of our tunnels.
@@ -611,10 +603,12 @@ destroy_tunnel_task (void *cls,
 /**
  * Method called whenever a peer has disconnected from the tunnel.
  *
+ * FIXME merge with inbound_cleaner
+ * 
  * @param cls closure
  * @param peer peer identity the tunnel stopped working with
  */
-static void
+void
 tunnel_peer_disconnect_handler (void *cls,
                                const struct
                                GNUNET_PeerIdentity * peer)
@@ -645,11 +639,13 @@ tunnel_peer_disconnect_handler (void *cls,
  * Method called whenever a peer has connected to the tunnel.  Notifies
  * the waiting client that the tunnel is now up.
  *
+ * FIXME merge with tunnel_create
+ * 
  * @param cls closure
  * @param peer peer identity the tunnel was created to, NULL on timeout
  * @param atsi performance data for the connection
  */
-static void
+void
 tunnel_peer_connect_handler (void *cls,
                             const struct GNUNET_PeerIdentity
                             * peer,
@@ -709,7 +705,6 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
     ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel, 
                                                GNUNET_NO /* cork */, 
                                                GNUNET_TIME_UNIT_FOREVER_REL,
-                                               NULL, 
                                                tnq->len,
                                                &send_to_peer_notify_callback,
                                                ts);
@@ -761,125 +756,12 @@ send_to_tunnel (struct TunnelMessageQueueEntry *tnq,
     ts->th = GNUNET_MESH_notify_transmit_ready (ts->tunnel, 
                                                GNUNET_NO /* cork */,
                                                GNUNET_TIME_UNIT_FOREVER_REL,
-                                               NULL, 
                                                tnq->len,
                                                &send_to_peer_notify_callback,
                                                ts);
 }
 
 
-/**
- * Create a string with binary IP notation for the given 'addr' in 'str'.
- *
- * @param af address family of the given 'addr'.
- * @param addr address that should be converted to a string.
- *             struct in_addr * for IPv4 and struct in6_addr * for IPv6.
- * @param str string that will contain binary notation of 'addr'. Expected
- *            to be at least 33 bytes long for IPv4 and 129 bytes long for IPv6.
- */
-static void
-iptobinstr (const int af, const void *addr, char *str)
-{
-  int i;
-  
-  switch (af)
-  {
-    case AF_INET:
-    {
-      uint32_t b = htonl (((struct in_addr *) addr)->s_addr);
-      
-      str[32] = '\0';
-          str += 31;
-          for (i = 31; i >= 0; i--)
-          {
-            *str = (b & 1) + '0';
-            str--;
-            b >>= 1;
-          }
-              break;
-    }
-    case AF_INET6:
-    {
-      struct in6_addr b = *(const struct in6_addr *) addr;
-      
-      str[128] = '\0';
-            str += 127;
-            for (i = 127; i >= 0; i--)
-            {
-              *str = (b.s6_addr[i / 8] & 1) + '0';
-            str--;
-            b.s6_addr[i / 8] >>= 1;
-            }
-                break;
-    }
-  }
-}
-
-
-/**
- * Get the ipv4 network prefix from the given 'netmask'.
- *
- * @param netmask netmask for which to get the prefix len.
- *
- * @return length of ipv4 prefix for 'netmask'.
- */
-static unsigned int
-ipv4netmasktoprefixlen (const char *netmask)
-{
-  struct in_addr a;
-  unsigned int len;
-  uint32_t t;
-  
-  if (1 != inet_pton (AF_INET, netmask, &a))
-    return 0;
-  len = 32;
-  for (t = htonl (~a.s_addr); 0 != t; t >>= 1)
-    len--;
-  return len;
-}
-
-
-/**
- * Create a regex in 'rxstr' from the given 'ip' and 'netmask'.
- *
- * @param ip IPv4 representation.
- * @param netmask netmask for the ip.
- * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV4_REGEXLEN
- *              bytes long.
- */
-static void
-ipv4toregex (const struct in_addr *ip, const char *netmask,
-            char *rxstr)
-{
-  unsigned int pfxlen;
-  
-  pfxlen = ipv4netmasktoprefixlen (netmask);
-  iptobinstr (AF_INET, ip, rxstr);
-  rxstr[pfxlen] = '\0';
-            if (pfxlen < 32)
-              strcat (rxstr, "(0|1)+");
-}
-
-
-/**
- * Create a regex in 'rxstr' from the given 'ipv6' and 'prefixlen'.
- *
- * @param ipv6 IPv6 representation.
- * @param prefixlen length of the ipv6 prefix.
- * @param rxstr generated regex, must be at least GNUNET_REGEX_IPV6_REGEXLEN
- *              bytes long.
- */
-static void
-ipv6toregex (const struct in6_addr *ipv6, unsigned int prefixlen,
-                          char *rxstr)
-{
-  iptobinstr (AF_INET6, ipv6, rxstr);
-  rxstr[prefixlen] = '\0';
-    if (prefixlen < 128)
-      strcat (rxstr, "(0|1)+");
-}
-
-
 /**
  * Regex has found a potential exit peer for us; consider using it.
  *
@@ -903,12 +785,11 @@ handle_regex_result (void *cls,
   GNUNET_REGEX_search_cancel (ts->search);
   ts->search = NULL;
   ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
-                                         ts,
-                                         &tunnel_peer_connect_handler,
-                                         &tunnel_peer_disconnect_handler,
-                                         ts);
-  GNUNET_MESH_peer_request_connect_add (ts->tunnel,
-                                       id);
+                                          ts,
+                                          id,
+                                          PORT_VPN,
+                                          GNUNET_YES,
+                                          GNUNET_NO);
 }
 
 
@@ -948,9 +829,10 @@ create_tunnel_to_destination (struct DestinationEntry *de,
   {
     ts->tunnel = GNUNET_MESH_tunnel_create (mesh_handle,
                                            ts,
-                                           &tunnel_peer_connect_handler,
-                                           &tunnel_peer_disconnect_handler,
-                                           ts);
+                        &de->details.service_destination.target,
+                                           PORT_VPN,
+                        GNUNET_YES,
+                        GNUNET_NO);
     if (NULL == ts->tunnel)
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -962,8 +844,6 @@ create_tunnel_to_destination (struct DestinationEntry *de,
                "Creating tunnel to peer %s offering service %s\n",
                GNUNET_i2s (&de->details.service_destination.target),
                GNUNET_h2s (&de->details.service_destination.service_descriptor));
-    GNUNET_MESH_peer_request_connect_add (ts->tunnel,
-                                         &de->details.service_destination.target);  
   }
   else
   {
@@ -973,9 +853,9 @@ create_tunnel_to_destination (struct DestinationEntry *de,
     {
     case AF_INET:
     {
-      char address[GNUNET_REGEX_IPV4_REGEXLEN];
+      char address[GNUNET_TUN_IPV4_REGEXLEN];
 
-      ipv4toregex (&de->details.exit_destination.ip.v4,
+      GNUNET_TUN_ipv4toregexsearch (&de->details.exit_destination.ip.v4,
                   "255.255.255.255", address);
       GNUNET_asprintf (&policy, "%s%s%s",
                        GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
@@ -985,9 +865,9 @@ create_tunnel_to_destination (struct DestinationEntry *de,
     }
     case AF_INET6:
     {
-      char address[GNUNET_REGEX_IPV6_REGEXLEN];
+      char address[GNUNET_TUN_IPV6_REGEXLEN];
       
-      ipv6toregex (&de->details.exit_destination.ip.v6,
+      GNUNET_TUN_ipv6toregexsearch (&de->details.exit_destination.ip.v6,
                   128, address);
       GNUNET_asprintf (&policy, "%s%s%s",
                        GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
@@ -1240,7 +1120,7 @@ route_packet (struct DestinationEntry *destination,
     ts->destination_port = destination_port;
     ts->heap_node = GNUNET_CONTAINER_heap_insert (tunnel_heap,
                                                  ts,
-                                                 GNUNET_TIME_absolute_get ().abs_value);
+                                                 GNUNET_TIME_absolute_get ().abs_value_us);
     GNUNET_assert (GNUNET_YES ==
                   GNUNET_CONTAINER_multihashmap_put (tunnel_map,
                                                      &key,
@@ -1257,7 +1137,7 @@ route_packet (struct DestinationEntry *destination,
     is_new = GNUNET_NO;
     GNUNET_CONTAINER_heap_update_cost (tunnel_heap, 
                                       ts->heap_node,
-                                      GNUNET_TIME_absolute_get ().abs_value);
+                                      GNUNET_TIME_absolute_get ().abs_value_us);
   }
   GNUNET_assert (NULL != ts->tunnel);
   
@@ -1863,17 +1743,15 @@ make_up_icmpv6_payload (struct TunnelState *ts,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ * 
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */ 
 static int
 receive_icmp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
-                  const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                  void **tunnel_ctx,
+                  const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *ts = *tunnel_ctx;
   const struct GNUNET_EXIT_IcmpToVPNMessage *i2v;
@@ -2192,7 +2070,7 @@ receive_icmp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   }
   GNUNET_CONTAINER_heap_update_cost (tunnel_heap, 
                                     ts->heap_node,
-                                    GNUNET_TIME_absolute_get ().abs_value);
+                                    GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
 }
 
@@ -2204,17 +2082,15 @@ receive_icmp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ * 
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */ 
 static int
 receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
-                  void **tunnel_ctx, const struct GNUNET_PeerIdentity *sender,
-                  const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                  void **tunnel_ctx,
+                  const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *ts = *tunnel_ctx;
   const struct GNUNET_EXIT_UdpReplyMessage *reply;
@@ -2349,7 +2225,7 @@ receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   }
   GNUNET_CONTAINER_heap_update_cost (tunnel_heap, 
                                     ts->heap_node,
-                                    GNUNET_TIME_absolute_get ().abs_value);
+                                    GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
 }
 
@@ -2361,18 +2237,15 @@ receive_udp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
  * @param cls closure, NULL
  * @param tunnel connection to the other end
  * @param tunnel_ctx pointer to our 'struct TunnelState *'
- * @param sender who sent the message
  * @param message the actual message
- * @param atsi performance data for the connection
+ * 
  * @return GNUNET_OK to keep the connection open,
  *         GNUNET_SYSERR to close it (signal serious error)
  */ 
 static int
 receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
                   void **tunnel_ctx,
-                  const struct GNUNET_PeerIdentity *sender GNUNET_UNUSED,
-                  const struct GNUNET_MessageHeader *message,
-                  const struct GNUNET_ATS_Information *atsi GNUNET_UNUSED)
+                  const struct GNUNET_MessageHeader *message)
 {
   struct TunnelState *ts = *tunnel_ctx;
   const struct GNUNET_EXIT_TcpDataMessage *data;
@@ -2494,7 +2367,7 @@ receive_tcp_back (void *cls GNUNET_UNUSED, struct GNUNET_MESH_Tunnel *tunnel,
   }
   GNUNET_CONTAINER_heap_update_cost (tunnel_heap, 
                                     ts->heap_node,
-                                    GNUNET_TIME_absolute_get ().abs_value);
+                                    GNUNET_TIME_absolute_get ().abs_value_us);
   return GNUNET_OK;
 }
 
@@ -2835,7 +2708,7 @@ service_redirect_to_ip (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Client *cl
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
   de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
                                                de,
-                                               GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
+                                               GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value_us);
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Active destinations"),
                            1, GNUNET_NO);
@@ -2941,7 +2814,7 @@ service_redirect_to_service (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Clien
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
   de->heap_node = GNUNET_CONTAINER_heap_insert (destination_heap,
                                                de,
-                                               GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value);
+                                               GNUNET_TIME_absolute_ntoh (msg->expiration_time).abs_value_us);
   while (GNUNET_CONTAINER_multihashmap_size (destination_map) > max_destination_mappings)
     expire_destination (de);
   ts = create_tunnel_to_destination (de,
@@ -2964,32 +2837,11 @@ service_redirect_to_service (void *cls GNUNET_UNUSED, struct GNUNET_SERVER_Clien
 }
 
 
-
-/**
- * Function called for inbound tunnels.  As we don't offer
- * any mesh services, this function should never be called.
- *
- * @param cls closure
- * @param tunnel new handle to the tunnel
- * @param initiator peer that started the tunnel
- * @param atsi performance information for the tunnel
- * @return initial tunnel context for the tunnel
- *         (can be NULL -- that's not an error)
- */ 
-static void *
-inbound_tunnel_cb (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
-                  const struct GNUNET_PeerIdentity *initiator,
-                  const struct GNUNET_ATS_Information *atsi)
-{
-  /* How can and why should anyone open an inbound tunnel to vpn? */
-  GNUNET_break (0);
-  return NULL;
-}
-
-
 /**
  * Function called whenever an inbound tunnel is destroyed.  Should clean up
  * any associated state.
+ * 
+ * FIXME now its also user for disconnections
  *
  * @param cls closure (set from GNUNET_MESH_connect)
  * @param tunnel connection to the other end (henceforth invalid)
@@ -3203,9 +3055,6 @@ run (void *cls,
     { &receive_icmp_back, GNUNET_MESSAGE_TYPE_VPN_ICMP_TO_VPN, 0},
     {NULL, 0, 0}
   };
-  static const GNUNET_MESH_ApplicationType types[] = {
-    GNUNET_APPLICATION_TYPE_END
-  };
   char *ifname;
   char *ipv6addr;
   char *ipv6prefix_s;
@@ -3331,10 +3180,10 @@ run (void *cls,
 
   mesh_handle =
     GNUNET_MESH_connect (cfg_, NULL, 
-                        &inbound_tunnel_cb
+                        NULL
                         &tunnel_cleaner, 
                         mesh_handlers,
-                        types);
+                        NULL);
   helper_handle = GNUNET_HELPER_start (GNUNET_NO,
                                       "gnunet-helper-vpn", vpn_argv,
                                       &message_token, NULL, NULL);