add function conv param string
[oweals/gnunet.git] / src / vpn / gnunet-service-vpn.c
index 304ecb33c05ebbc948d31f86d4c7c9e2ac2fe1f7..8741aa719b3c5351b5865dc844ef79985cdbc6f5 100644 (file)
@@ -14,8 +14,8 @@
 
      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.
 */
 
 /**
@@ -83,11 +83,6 @@ struct DestinationChannel
    */
   struct DestinationEntry *destination;
 
-  /**
-   * Pre-allocated channel for this destination, or NULL for none.
-   */
-  struct ChannelState *ts;
-
   /**
    * Destination port this channel state is used for.
    */
@@ -246,12 +241,6 @@ struct ChannelState
    */
   struct ChannelMessageQueueEntry *tmq_tail;
 
-  /**
-   * Destination entry that has a pointer to this channel state;
-   * NULL if this channel state is in the channel map.
-   */
-  struct DestinationChannel *destination_container;
-
   /**
    * Destination to which this channel leads.  Note that
    * this struct is NOT in the destination_map (but a
@@ -265,6 +254,12 @@ struct ChannelState
    */
   int af;
 
+  /**
+   * Is this channel new (#GNUNET_NO), or did we exchange messages with the
+   * other side already (#GNUNET_YES)?
+   */
+  int is_established;
+
   /**
    * Length of the doubly linked 'tmq_head/tmq_tail' list.
    */
@@ -546,6 +541,17 @@ free_channel_state (struct ChannelState *ts)
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Cleaning up channel state\n");
+  if (NULL != ts->th)
+  {
+    GNUNET_CADET_notify_transmit_ready_cancel (ts->th);
+    ts->th = NULL;
+  }
+  if (NULL != (channel = ts->channel))
+  {
+    ts->channel = NULL;
+    GNUNET_CADET_channel_destroy (channel);
+    return;
+  }
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Active channels"),
                            -1, GNUNET_NO);
@@ -558,17 +564,7 @@ free_channel_state (struct ChannelState *ts)
     GNUNET_free (tnq);
   }
   GNUNET_assert (0 == ts->tmq_length);
-  if (NULL != ts->th)
-  {
-    GNUNET_CADET_notify_transmit_ready_cancel (ts->th);
-    ts->th = NULL;
-  }
   GNUNET_assert (NULL == ts->destination.heap_node);
-  if (NULL != (channel = ts->channel))
-  {
-    ts->channel = NULL;
-    GNUNET_CADET_channel_destroy (channel);
-  }
   if (NULL != ts->search)
   {
     GNUNET_REGEX_search_cancel (ts->search);
@@ -590,12 +586,6 @@ free_channel_state (struct ChannelState *ts)
                                                         &key,
                                                         ts));
   }
-  if (NULL != ts->destination_container)
-  {
-    GNUNET_assert (ts == ts->destination_container->ts);
-    ts->destination_container->ts = NULL;
-    ts->destination_container = NULL;
-  }
   GNUNET_free (ts);
 }
 
@@ -623,7 +613,7 @@ send_to_peer_notify_callback (void *cls, size_t size, void *buf)
   GNUNET_assert (size >= tnq->len);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Sending %u bytes via cadet channel\n",
-             tnq->len);
+             (unsigned int) tnq->len);
   GNUNET_CONTAINER_DLL_remove (ts->tmq_head,
                               ts->tmq_tail,
                               tnq);
@@ -658,7 +648,7 @@ send_to_channel (struct ChannelMessageQueueEntry *tnq,
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Queueing %u bytes for transmission via cadet channel\n",
-             tnq->len);
+             (unsigned int) tnq->len);
   GNUNET_assert (NULL != ts->channel);
   GNUNET_CONTAINER_DLL_insert_tail (ts->tmq_head,
                                    ts->tmq_tail,
@@ -707,7 +697,7 @@ print_channel_destination (const struct DestinationEntry *de)
   {
     GNUNET_snprintf (dest,
                      sizeof (dest),
-                     "HS: %s-%s\n",
+                     "HS: %s-%s",
                      GNUNET_i2s (&de->details.service_destination.target),
                      GNUNET_h2s (&de->details.service_destination.service_descriptor));
   }
@@ -790,7 +780,6 @@ create_channel_to_destination (struct DestinationChannel *dt,
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Cadet channels created"),
                            1, GNUNET_NO);
-  GNUNET_assert (NULL == dt->ts);
   switch (client_af)
   {
   case AF_INET:
@@ -807,8 +796,6 @@ create_channel_to_destination (struct DestinationChannel *dt,
   ts->af = client_af;
   ts->destination = *dt->destination;
   ts->destination.heap_node = NULL; /* copy is NOT in destination heap */
-  dt->ts = ts;
-  ts->destination_container = dt; /* we are referenced from dt */
   if (dt->destination->is_service)
   {
     ts->channel = GNUNET_CADET_channel_create (cadet_handle,
@@ -824,7 +811,8 @@ create_channel_to_destination (struct DestinationChannel *dt,
       return NULL;
     }
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Creating channel to peer %s offering service %s\n",
+               "Creating channel %p to peer %s offering service %s\n",
+                ts->channel,
                GNUNET_i2s (&dt->destination->details.service_destination.target),
                GNUNET_h2s (&dt->destination->details.service_destination.service_descriptor));
   }
@@ -892,6 +880,9 @@ expire_channel (struct ChannelState *except)
   GNUNET_assert (NULL != ts);
   if (except == ts)
     return; /* can't do this */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Tearing down expired channel to %s\n",
+              print_channel_destination (&except->destination));
   free_channel_state (ts);
 }
 
@@ -921,7 +912,6 @@ route_packet (struct DestinationEntry *destination,
   struct ChannelMessageQueueEntry *tnq;
   size_t alen;
   size_t mlen;
-  int is_new;
   const struct GNUNET_TUN_UdpHeader *udp;
   const struct GNUNET_TUN_TcpHeader *tcp;
   const struct GNUNET_TUN_IcmpHeader *icmp;
@@ -950,12 +940,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = ntohs (udp->source_port);
       destination_port = ntohs (udp->destination_port);
       get_channel_key_from_ips (af,
-                              IPPROTO_UDP,
-                              source_ip,
-                              source_port,
-                              destination_ip,
-                              destination_port,
-                              &key);
+                                IPPROTO_UDP,
+                                source_ip,
+                                source_port,
+                                destination_ip,
+                                destination_port,
+                                &key);
     }
     break;
   case IPPROTO_TCP:
@@ -977,12 +967,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = ntohs (tcp->source_port);
       destination_port = ntohs (tcp->destination_port);
       get_channel_key_from_ips (af,
-                              IPPROTO_TCP,
-                              source_ip,
-                              source_port,
-                              destination_ip,
-                              destination_port,
-                              &key);
+                                IPPROTO_TCP,
+                                source_ip,
+                                source_port,
+                                destination_ip,
+                                destination_port,
+                                &key);
     }
     break;
   case IPPROTO_ICMP:
@@ -1005,12 +995,12 @@ route_packet (struct DestinationEntry *destination,
       source_port = 0;
       destination_port = 0;
       get_channel_key_from_ips (af,
-                              protocol,
-                              source_ip,
-                              0,
-                              destination_ip,
-                              0,
-                              &key);
+                                protocol,
+                                source_ip,
+                                0,
+                                destination_ip,
+                                0,
+                                &key);
     }
     break;
   default:
@@ -1040,7 +1030,7 @@ route_packet (struct DestinationEntry *destination,
       char xbuf[INET6_ADDRSTRLEN];
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Routing %s packet from %s:%u -> %s:%u to destination %s:%u\n",
+                 "Routing %s packet from [%s]:%u -> [%s]:%u to destination [%s]:%u\n",
                  (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
                  inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
                  source_port,
@@ -1062,7 +1052,7 @@ route_packet (struct DestinationEntry *destination,
       char dbuf[INET6_ADDRSTRLEN];
 
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Routing %s packet from %s:%u -> %s:%u to service %s at peer %s\n",
+                 "Routing %s packet from [%s]:%u -> [%s]:%u to service %s at peer %s\n",
                  (protocol == IPPROTO_TCP) ? "TCP" : "UDP",
                  inet_ntop (af, source_ip, sbuf, sizeof (sbuf)),
                  source_port,
@@ -1088,17 +1078,14 @@ route_packet (struct DestinationEntry *destination,
                                          &key);
   if (NULL == ts)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Creating new channel for key %s\n",
+                GNUNET_h2s (&key));
     /* need to either use the existing channel from the destination (if still
        available) or create a fresh one */
-    is_new = GNUNET_YES;
-    if (NULL == dt->ts)
-      ts = create_channel_to_destination (dt, af);
-    else
-      ts = dt->ts;
+    ts = create_channel_to_destination (dt, af);
     if (NULL == ts)
       return;
-    dt->ts = NULL;
-    ts->destination_container = NULL; /* no longer 'contained' */
     /* now bind existing "unbound" channel to our IP/port tuple */
     ts->protocol = protocol;
     ts->af = af;
@@ -1130,7 +1117,6 @@ route_packet (struct DestinationEntry *destination,
   }
   else
   {
-    is_new = GNUNET_NO;
     GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                       ts->heap_node,
                                       GNUNET_TIME_absolute_get ().abs_value_us);
@@ -1224,7 +1210,7 @@ route_packet (struct DestinationEntry *destination,
     }
     break;
   case IPPROTO_TCP:
-    if (is_new)
+    if (GNUNET_NO == ts->is_established)
     {
       if (destination->is_service)
       {
@@ -1556,6 +1542,7 @@ route_packet (struct DestinationEntry *destination,
     GNUNET_assert (0);
     break;
   }
+  ts->is_established = GNUNET_YES;
   send_to_channel (tnq, ts);
 }
 
@@ -2069,6 +2056,7 @@ receive_icmp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -2120,7 +2108,7 @@ receive_udp_back (void *cls,
     char dbuf[INET6_ADDRSTRLEN];
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received UDP reply from cadet, sending %u bytes from %s:%u -> %s:%u via TUN\n",
+               "Received UDP reply from cadet, sending %u bytes from [%s]:%u -> [%s]:%u via TUN\n",
                (unsigned int) mlen,
                inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
                ts->destination_port,
@@ -2224,6 +2212,7 @@ receive_udp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -2270,7 +2259,7 @@ receive_tcp_back (void *cls,
     char dbuf[INET6_ADDRSTRLEN];
 
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-               "Received TCP reply from cadet, sending %u bytes from %s:%u -> %s:%u via TUN\n",
+               "Received TCP reply from cadet, sending %u bytes from [%s]:%u -> [%s]:%u via TUN\n",
                (unsigned int) mlen,
                inet_ntop (ts->af, &ts->destination_ip, sbuf, sizeof (sbuf)),
                ts->destination_port,
@@ -2366,6 +2355,7 @@ receive_tcp_back (void *cls,
   GNUNET_CONTAINER_heap_update_cost (channel_heap,
                                     ts->heap_node,
                                     GNUNET_TIME_absolute_get ().abs_value_us);
+  GNUNET_CADET_receive_done (channel);
   return GNUNET_OK;
 }
 
@@ -2495,7 +2485,8 @@ free_destination_entry (struct DestinationEntry *de)
   struct DestinationChannel *dt;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Cleaning up destination entry\n");
+             "Cleaning up destination entry `%s'\n",
+              print_channel_destination (de));
   GNUNET_STATISTICS_update (stats,
                            gettext_noop ("# Active destinations"),
                            -1, GNUNET_NO);
@@ -2504,11 +2495,6 @@ free_destination_entry (struct DestinationEntry *de)
     GNUNET_CONTAINER_DLL_remove (de->dt_head,
                                 de->dt_tail,
                                 dt);
-    if (NULL != dt->ts)
-    {
-      free_channel_state (dt->ts);
-      GNUNET_assert (NULL == dt->ts);
-    }
     GNUNET_free (dt);
   }
   if (NULL != de->heap_node)
@@ -2742,7 +2728,6 @@ service_redirect_to_service (void *cls,
   void *addr;
   struct DestinationEntry *de;
   struct GNUNET_HashCode key;
-  struct ChannelState *ts;
   struct DestinationChannel *dt;
 
   /*  parse request */
@@ -2805,19 +2790,6 @@ service_redirect_to_service (void *cls,
   GNUNET_CONTAINER_DLL_insert (de->dt_head,
                               de->dt_tail,
                               dt);
-  ts = create_channel_to_destination (dt,
-                                      result_af);
-  switch (result_af)
-  {
-  case AF_INET:
-    ts->destination_ip.v4 = v4;
-    break;
-  case AF_INET6:
-    ts->destination_ip.v6 = v6;
-    break;
-  default:
-    GNUNET_assert (0);
-  }
   /* we're done */
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
@@ -2840,6 +2812,9 @@ channel_cleaner (void *cls,
   struct ChannelState *ts = channel_ctx;
 
   ts->channel = NULL; /* we must not call GNUNET_CADET_channel_destroy() anymore */
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "CADET notified us about death of channel to `%s'\n",
+              print_channel_destination (&ts->destination));
   free_channel_state (ts);
 }
 
@@ -2874,11 +2849,14 @@ cleanup_destination (void *cls,
  */
 static int
 cleanup_channel (void *cls,
-               const struct GNUNET_HashCode *key,
-               void *value)
+                 const struct GNUNET_HashCode *key,
+                 void *value)
 {
   struct ChannelState *ts = value;
 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Tearing down channel to `%s' during cleanup\n",
+              print_channel_destination (&ts->destination));
   free_channel_state (ts);
   return GNUNET_OK;
 }
@@ -2888,11 +2866,9 @@ cleanup_channel (void *cls,
  * Function scheduled as very last function, cleans up after us
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-cleanup (void *cls,
-         const struct GNUNET_SCHEDULER_TaskContext *tc)
+cleanup (void *cls)
 {
   unsigned int i;
 
@@ -2931,7 +2907,8 @@ cleanup (void *cls,
   }
   if (NULL != helper_handle)
   {
-    GNUNET_HELPER_stop (helper_handle, GNUNET_NO);
+    GNUNET_HELPER_kill (helper_handle, GNUNET_NO);
+    GNUNET_HELPER_wait (helper_handle);
     helper_handle = NULL;
   }
   if (NULL != nc)
@@ -3113,16 +3090,17 @@ run (void *cls,
 
   cadet_handle =
     GNUNET_CADET_connect (cfg_, NULL,
-                        NULL,
-                        &channel_cleaner,
-                        cadet_handlers,
-                        NULL);
+                          NULL,
+                          &channel_cleaner,
+                          cadet_handlers,
+                          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_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleanup, cls);
+  GNUNET_SCHEDULER_add_shutdown (&cleanup,
+                                NULL);
 }