REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
index 29ade08f04ebbe843061d2ce64b568aa6c8220e9..b05192e063a52324f78585174d2bd7c357bda3f1 100644 (file)
@@ -1,21 +1,21 @@
 /*
  This file is part of GNUnet
- Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2010-2017 GNUnet e.V.
 
- GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
- by the Free Software Foundation; either version 3, or (at your
- option) any later version.
+ GNUnet is free software: you can redistribute it and/or modify it
under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
 
  GNUnet is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- General Public License for more details.
Affero General Public License for more details.
 
- 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., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
+ You should have received a copy of the GNU Affero General Public License
+ along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
  */
 
 /**
@@ -30,7 +30,7 @@
 #include "gnunet_hello_lib.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_fragmentation_lib.h"
-#include "gnunet_nat_lib.h"
+#include "gnunet_nat_service.h"
 #include "gnunet_protocols.h"
 #include "gnunet_resolver_service.h"
 #include "gnunet_signatures.h"
@@ -158,6 +158,11 @@ struct GNUNET_ATS_Session
    */
   struct GNUNET_PeerIdentity target;
 
+  /**
+   * Tokenizer for inbound messages.
+   */
+  struct GNUNET_MessageStreamTokenizer *mst;
+
   /**
    * Plugin this session belongs to.
    */
@@ -175,8 +180,8 @@ struct GNUNET_ATS_Session
 
   /**
    * Desired delay for transmissions we received from other peer.
-   * Adjusted to be per fragment (UDP_MTU), even though on the
-   * wire it was for "full messages".
+   * This is for full messages, the value needs to be adjusted for
+   * fragmented messages.
    */
   struct GNUNET_TIME_Relative flow_delay_from_other_peer;
 
@@ -231,7 +236,7 @@ struct GNUNET_ATS_Session
   /**
    * Network type of the address.
    */
-  enum GNUNET_ATS_Network_Type scope;
+  enum GNUNET_NetworkType scope;
 
   /**
    * Is this session about to be destroyed (sometimes we cannot
@@ -287,7 +292,7 @@ struct DefragContext
   /**
    * Network type the address belongs to.
    */
-  enum GNUNET_ATS_Network_Type network_type;
+  enum GNUNET_NetworkType network_type;
 
   /**
    * Has the @e sender field been initialized yet?
@@ -343,11 +348,18 @@ struct UDP_FragmentationContext
 
   /**
    * Transmission time for the next fragment.  Incremented by
-   * the "flow_delay_from_other_peer" for each fragment when
+   * the @e flow_delay_from_other_peer for each fragment when
    * we setup the fragments.
    */
   struct GNUNET_TIME_Absolute next_frag_time;
 
+  /**
+   * Desired delay for transmissions we received from other peer.
+   * Adjusted to be per fragment (UDP_MTU), even though on the
+   * wire it was for "full messages".
+   */
+  struct GNUNET_TIME_Relative flow_delay_from_other_peer;
+
   /**
    * Message timeout
    */
@@ -619,6 +631,11 @@ free_session (struct GNUNET_ATS_Session *s)
     GNUNET_free (s->frag_ctx);
     s->frag_ctx = NULL;
   }
+  if (NULL != s->mst)
+  {
+    GNUNET_MST_destroy (s->mst);
+    s->mst = NULL;
+  }
   GNUNET_free (s);
 }
 
@@ -645,7 +662,7 @@ udp_query_keepalive_factor (void *cls)
  * @param session the session
  * @return the network type
  */
-static enum GNUNET_ATS_Network_Type
+static enum GNUNET_NetworkType
 udp_plugin_get_network (void *cls,
                         struct GNUNET_ATS_Session *session)
 {
@@ -660,7 +677,7 @@ udp_plugin_get_network (void *cls,
  * @param address the address
  * @return the network type
  */
-static enum GNUNET_ATS_Network_Type
+static enum GNUNET_NetworkType
 udp_plugin_get_network_for_address (void *cls,
                                     const struct GNUNET_HELLO_Address *address)
 {
@@ -684,7 +701,7 @@ udp_plugin_get_network_for_address (void *cls,
 #endif
     a6.sin6_family = AF_INET6;
     a6.sin6_port = u6->u6_port;
-    memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
+    GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
     sb = &a6;
     sbs = sizeof(a6);
   }
@@ -705,7 +722,7 @@ udp_plugin_get_network_for_address (void *cls,
   else
   {
     GNUNET_break (0);
-    return GNUNET_ATS_NET_UNSPECIFIED;
+    return GNUNET_NT_UNSPECIFIED;
   }
   return plugin->env->get_address_type (plugin->env->cls,
                                         sb,
@@ -721,11 +738,9 @@ udp_plugin_get_network_for_address (void *cls,
  * Then reschedule this function to be called again once more is available.
  *
  * @param cls the plugin handle
- * @param tc the scheduling context (for rescheduling this function again)
  */
 static void
-udp_plugin_select_v4 (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc);
+udp_plugin_select_v4 (void *cls);
 
 
 /**
@@ -734,11 +749,9 @@ udp_plugin_select_v4 (void *cls,
  * Then reschedule this function to be called again once more is available.
  *
  * @param cls the plugin handle
- * @param tc the scheduling context (for rescheduling this function again)
  */
 static void
-udp_plugin_select_v6 (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc);
+udp_plugin_select_v6 (void *cls);
 
 
 /**
@@ -780,7 +793,7 @@ schedule_select_v4 (struct Plugin *plugin)
                     "Calculated flow delay for UDPv4 at %s for %s\n",
                     GNUNET_STRINGS_relative_time_to_string (min_delay,
                                                             GNUNET_YES),
-                    GNUNET_i2s (&udpw->session->target));
+                    GNUNET_i2s (&min_udpw->session->target));
       }
       else
       {
@@ -788,7 +801,7 @@ schedule_select_v4 (struct Plugin *plugin)
                     "Calculated flow delay for UDPv4 at %s for %s\n",
                     GNUNET_STRINGS_relative_time_to_string (min_delay,
                                                             GNUNET_YES),
-                    GNUNET_i2s (&udpw->session->target));
+                    GNUNET_i2s (&min_udpw->session->target));
       }
     }
     plugin->select_task_v4
@@ -837,7 +850,7 @@ schedule_select_v6 (struct Plugin *plugin)
                     "Calculated flow delay for UDPv6 at %s for %s\n",
                     GNUNET_STRINGS_relative_time_to_string (min_delay,
                                                             GNUNET_YES),
-                    GNUNET_i2s (&udpw->session->target));
+                    GNUNET_i2s (&min_udpw->session->target));
       }
       else
       {
@@ -845,7 +858,7 @@ schedule_select_v6 (struct Plugin *plugin)
                     "Calculated flow delay for UDPv6 at %s for %s\n",
                     GNUNET_STRINGS_relative_time_to_string (min_delay,
                                                             GNUNET_YES),
-                    GNUNET_i2s (&udpw->session->target));
+                    GNUNET_i2s (&min_udpw->session->target));
       }
     }
     plugin->select_task_v6
@@ -1242,31 +1255,45 @@ udp_plugin_check_address (void *cls,
 
   if (sizeof(struct IPv4UdpAddress) == addrlen)
   {
+    struct sockaddr_in s4;
+
     v4 = (const struct IPv4UdpAddress *) addr;
     if (GNUNET_OK != check_port (plugin,
                                  ntohs (v4->u4_port)))
       return GNUNET_SYSERR;
+    memset (&s4, 0, sizeof (s4));
+    s4.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s4.sin_len = sizeof (s4);
+#endif
+    s4.sin_port = v4->u4_port;
+    s4.sin_addr.s_addr = v4->ipv4_addr;
+
     if (GNUNET_OK !=
-        GNUNET_NAT_test_address (plugin->nat,
-                                 &v4->ipv4_addr,
-                                 sizeof (struct in_addr)))
+       GNUNET_NAT_test_address (plugin->nat,
+                                &s4,
+                                sizeof (struct sockaddr_in)))
       return GNUNET_SYSERR;
   }
   else if (sizeof(struct IPv6UdpAddress) == addrlen)
   {
+    struct sockaddr_in6 s6;
+
     v6 = (const struct IPv6UdpAddress *) addr;
     if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
-    {
-      GNUNET_break_op (0);
-      return GNUNET_SYSERR;
-    }
-    if (GNUNET_OK != check_port (plugin,
-                                 ntohs (v6->u6_port)))
-      return GNUNET_SYSERR;
+      return GNUNET_OK; /* plausible, if unlikely... */
+    memset (&s6, 0, sizeof (s6));
+    s6.sin6_family = AF_INET6;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s6.sin6_len = sizeof (s6);
+#endif
+    s6.sin6_port = v6->u6_port;
+    s6.sin6_addr = v6->ipv6_addr;
+
     if (GNUNET_OK !=
-        GNUNET_NAT_test_address (plugin->nat,
-                                 &v6->ipv6_addr,
-                                 sizeof (struct in6_addr)))
+       GNUNET_NAT_test_address (plugin->nat,
+                                &s6,
+                                sizeof(struct sockaddr_in6)))
       return GNUNET_SYSERR;
   }
   else
@@ -1282,14 +1309,19 @@ udp_plugin_check_address (void *cls,
  * Our external IP address/port mapping has changed.
  *
  * @param cls closure, the `struct Plugin`
+ * @param app_ctx[in,out] location where the app can store stuff
+ *                  on add and retrieve it on remove
  * @param add_remove #GNUNET_YES to mean the new public IP address,
  *                   #GNUNET_NO to mean the previous (now invalid) one
+ * @param ac address class the address belongs to
  * @param addr either the previous or the new public IP address
  * @param addrlen actual length of the @a addr
  */
 static void
 udp_nat_port_map_callback (void *cls,
+                          void **app_ctx,
                            int add_remove,
+                          enum GNUNET_NAT_AddressClass ac,
                            const struct sockaddr *addr,
                            socklen_t addrlen)
 {
@@ -1300,6 +1332,7 @@ udp_nat_port_map_callback (void *cls,
   void *arg;
   size_t args;
 
+  (void) app_ctx;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        (GNUNET_YES == add_remove)
        ? "NAT notification to add address `%s'\n"
@@ -1316,10 +1349,7 @@ udp_nat_port_map_callback (void *cls,
       GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
       i4 = (const struct sockaddr_in *) addr;
       if (0 == ntohs (i4->sin_port))
-      {
-        GNUNET_break (0);
-        return;
-      }
+        return; /* Port = 0 means unmapped, ignore these for UDP. */
       memset (&u4,
               0,
               sizeof(u4));
@@ -1337,10 +1367,7 @@ udp_nat_port_map_callback (void *cls,
       GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
       i6 = (const struct sockaddr_in6 *) addr;
       if (0 == ntohs (i6->sin6_port))
-      {
-        GNUNET_break (0);
-        return;
-      }
+        return; /* Port = 0 means unmapped, ignore these for UDP. */
       memset (&u6,
               0,
               sizeof(u6));
@@ -1356,6 +1383,7 @@ udp_nat_port_map_callback (void *cls,
     return;
   }
   /* modify our published address list */
+  /* TODO: use 'ac' here in the future... */
   address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
                                            PLUGIN_NAME,
                                            arg,
@@ -1607,9 +1635,10 @@ enqueue (struct Plugin *plugin,
   if (GNUNET_YES == session->in_destroy)
   {
     GNUNET_break (0);
+    GNUNET_free (udpw);
     return;
   }
-  if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
+  if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
   {
     GNUNET_break (0);
   }
@@ -1687,16 +1716,18 @@ fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
   if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
-         "Fragmented message acknowledged after %s\n",
+         "Fragmented message acknowledged after %s (expected at %s)\n",
          GNUNET_STRINGS_relative_time_to_string (delay,
-                                                 GNUNET_YES));
+                                                 GNUNET_YES),
+         GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
   }
   else
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "Fragmented message acknowledged after %s\n",
+         "Fragmented message acknowledged after %s (expected at %s)\n",
          GNUNET_STRINGS_relative_time_to_string (delay,
-                                                 GNUNET_YES));
+                                                 GNUNET_YES),
+         GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
   }
 
   if (NULL != frag_ctx->cont)
@@ -1816,6 +1847,10 @@ qc_fragment_sent (void *cls,
   GNUNET_assert (NULL != udpw->frag_ctx);
   if (GNUNET_OK == result)
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Fragment of message with %u bytes transmitted to %s\n",
+                (unsigned int) udpw->payload_size,
+                GNUNET_i2s (&udpw->session->target));
     GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
     GNUNET_STATISTICS_update (plugin->env->stats,
                               "# UDP, fragmented msgs, fragments, sent, success",
@@ -1828,6 +1863,10 @@ qc_fragment_sent (void *cls,
   }
   else
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Failed to transmit fragment of message with %u bytes to %s\n",
+                (unsigned int) udpw->payload_size,
+                GNUNET_i2s (&udpw->session->target));
     fragmented_message_done (udpw->frag_ctx,
                              GNUNET_SYSERR);
     GNUNET_STATISTICS_update (plugin->env->stats,
@@ -1874,15 +1913,19 @@ enqueue_fragment (void *cls,
   udpw->transmission_time = frag_ctx->next_frag_time;
   frag_ctx->next_frag_time
     = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
-                                session->flow_delay_from_other_peer);
+                                frag_ctx->flow_delay_from_other_peer);
   udpw->frag_ctx = frag_ctx;
   udpw->qc = &qc_fragment_sent;
   udpw->qc_cls = plugin;
-  memcpy (udpw->msg_buf,
-          msg,
-          msg_len);
+  GNUNET_memcpy (udpw->msg_buf,
+                msg,
+                msg_len);
   enqueue (plugin,
            udpw);
+  if (session->address->address_length == sizeof (struct IPv4UdpAddress))
+    schedule_select_v4 (plugin);
+  else
+    schedule_select_v6 (plugin);
 }
 
 
@@ -2015,6 +2058,7 @@ udp_plugin_send (void *cls,
   struct UDP_MessageWrapper *udpw;
   struct UDPMessage *udp;
   char mbuf[udpmlen] GNUNET_ALIGN;
+  struct GNUNET_TIME_Relative latency;
 
   if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
        (NULL == plugin->sockv6) )
@@ -2022,7 +2066,7 @@ udp_plugin_send (void *cls,
   if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
        (NULL == plugin->sockv4) )
     return GNUNET_SYSERR;
-  if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+  if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
@@ -2076,10 +2120,10 @@ udp_plugin_send (void *cls,
     udpw->frag_ctx = NULL;
     udpw->qc = &qc_message_sent;
     udpw->qc_cls = plugin;
-    memcpy (udpw->msg_buf,
+    GNUNET_memcpy (udpw->msg_buf,
             udp,
             sizeof (struct UDPMessage));
-    memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
+    GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
             msgbuf,
             msgbuf_size);
     enqueue (plugin,
@@ -2092,13 +2136,17 @@ udp_plugin_send (void *cls,
                               "# UDP, unfragmented bytes payload queued total",
                               msgbuf_size,
                               GNUNET_NO);
+    if (s->address->address_length == sizeof (struct IPv4UdpAddress))
+      schedule_select_v4 (plugin);
+    else
+      schedule_select_v6 (plugin);
   }
   else
   {
     /* fragmented message */
     if (NULL != s->frag_ctx)
       return GNUNET_SYSERR;
-    memcpy (&udp[1],
+    GNUNET_memcpy (&udp[1],
             msgbuf,
             msgbuf_size);
     frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
@@ -2108,6 +2156,10 @@ udp_plugin_send (void *cls,
     frag_ctx->cont_cls = cont_cls;
     frag_ctx->start_time = GNUNET_TIME_absolute_get ();
     frag_ctx->next_frag_time = s->last_transmit_time;
+    frag_ctx->flow_delay_from_other_peer
+      = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
+                                     1 + (msgbuf_size /
+                                          UDP_MTU));
     frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
     frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
     frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
@@ -2121,6 +2173,22 @@ udp_plugin_send (void *cls,
                                                      frag_ctx);
     s->frag_ctx = frag_ctx;
     s->last_transmit_time = frag_ctx->next_frag_time;
+    latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
+    if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
+      LOG (GNUNET_ERROR_TYPE_WARNING,
+           "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
+           GNUNET_STRINGS_relative_time_to_string (latency,
+                                                   GNUNET_YES),
+           GNUNET_i2s (&s->target),
+           (unsigned int) s->msgs_in_queue);
+    else
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
+           GNUNET_STRINGS_relative_time_to_string (latency,
+                                                   GNUNET_YES),
+           GNUNET_i2s (&s->target),
+           (unsigned int) s->msgs_in_queue);
+
     GNUNET_STATISTICS_update (plugin->env->stats,
                               "# UDP, fragmented messages active",
                               1,
@@ -2137,10 +2205,6 @@ udp_plugin_send (void *cls,
   notify_session_monitor (s->plugin,
                           s,
                           GNUNET_TRANSPORT_SS_UPDATE);
-  if (s->address->address_length == sizeof (struct IPv4UdpAddress))
-    schedule_select_v4 (plugin);
-  else
-    schedule_select_v6 (plugin);
   return udpmlen;
 }
 
@@ -2325,7 +2389,7 @@ udp_disconnect_session (void *cls,
 
 
 /**
- * Handle aACK message.
+ * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
  *
  * @param plugin the UDP plugin
  * @param msg the (presumed) UDP ACK message
@@ -2344,6 +2408,7 @@ read_process_ack (struct Plugin *plugin,
   struct GNUNET_ATS_Session *s;
   struct GNUNET_TIME_Relative flow_delay;
 
+  /* check message format */
   if (ntohs (msg->size)
       < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
   {
@@ -2357,6 +2422,8 @@ read_process_ack (struct Plugin *plugin,
     GNUNET_break_op(0);
     return;
   }
+
+  /* Locate session */
   address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
                                            PLUGIN_NAME,
                                            udp_addr,
@@ -2387,9 +2454,13 @@ read_process_ack (struct Plugin *plugin,
   }
   GNUNET_HELLO_address_free (address);
 
+  /* evaluate flow delay: how long should we wait between messages? */
   if (UINT32_MAX == ntohl (udp_ack->delay))
   {
     /* Other peer asked for us to terminate the session */
+    LOG (GNUNET_ERROR_TYPE_INFO,
+         "Asked to disconnect UDP session of %s\n",
+         GNUNET_i2s (&udp_ack->sender));
     udp_disconnect_session (plugin,
                             s);
     return;
@@ -2409,12 +2480,9 @@ read_process_ack (struct Plugin *plugin,
          GNUNET_i2s (&udp_ack->sender));
   /* Flow delay is for the reassembled packet, however, our delay
      is per packet, so we need to adjust: */
-  flow_delay = GNUNET_TIME_relative_divide (flow_delay,
-                                            1 + (s->frag_ctx->payload_size /
-                                                 UDP_MTU));
   s->flow_delay_from_other_peer = flow_delay;
 
-
+  /* Handle ACK */
   if (GNUNET_OK !=
       GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
                                    ack))
@@ -2430,14 +2498,13 @@ read_process_ack (struct Plugin *plugin,
     return;
   }
 
+  /* Remove fragmented message after successful sending */
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Message from %s at %s full ACK'ed\n",
        GNUNET_i2s (&udp_ack->sender),
        udp_address_to_string (plugin,
                               udp_addr,
                               udp_addr_len));
-
-  /* Remove fragmented message after successful sending */
   fragmented_message_done (s->frag_ctx,
                            GNUNET_OK);
 }
@@ -2447,18 +2514,16 @@ read_process_ack (struct Plugin *plugin,
  * Message tokenizer has broken up an incomming message. Pass it on
  * to the service.
  *
- * @param cls the `struct Plugin *`
- * @param client the `struct GNUNET_ATS_Session *`
+ * @param cls the `struct GNUNET_ATS_Session *`
  * @param hdr the actual message
  * @return #GNUNET_OK (always)
  */
 static int
 process_inbound_tokenized_messages (void *cls,
-                                    void *client,
                                     const struct GNUNET_MessageHeader *hdr)
 {
-  struct Plugin *plugin = cls;
-  struct GNUNET_ATS_Session *session = client;
+  struct GNUNET_ATS_Session *session = cls;
+  struct Plugin *plugin = session->plugin;
 
   if (GNUNET_YES == session->in_destroy)
     return GNUNET_OK;
@@ -2521,11 +2586,9 @@ udp_disconnect (void *cls,
  * Session was idle, so disconnect it.
  *
  * @param cls the `struct GNUNET_ATS_Session` to time out
- * @param tc scheduler context
  */
 static void
-session_timeout (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+session_timeout (void *cls)
 {
   struct GNUNET_ATS_Session *s = cls;
   struct Plugin *plugin = s->plugin;
@@ -2570,12 +2633,14 @@ session_timeout (void *cls,
 static struct GNUNET_ATS_Session *
 udp_plugin_create_session (void *cls,
                            const struct GNUNET_HELLO_Address *address,
-                           enum GNUNET_ATS_Network_Type network_type)
+                           enum GNUNET_NetworkType network_type)
 {
   struct Plugin *plugin = cls;
   struct GNUNET_ATS_Session *s;
 
   s = GNUNET_new (struct GNUNET_ATS_Session);
+  s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
+                              s);
   s->plugin = plugin;
   s->address = GNUNET_HELLO_address_copy (address);
   s->target = address->peer;
@@ -2628,7 +2693,7 @@ udp_plugin_get_session (void *cls,
 {
   struct Plugin *plugin = cls;
   struct GNUNET_ATS_Session *s;
-  enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
+  enum GNUNET_NetworkType network_type = GNUNET_NT_UNSPECIFIED;
   const struct IPv4UdpAddress *udp_v4;
   const struct IPv6UdpAddress *udp_v6;
 
@@ -2680,7 +2745,7 @@ udp_plugin_get_session (void *cls,
                                                   (const struct sockaddr *) &v6,
                                                   sizeof (v6));
   }
-  GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
+  GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
   return udp_plugin_create_session (cls,
                                    address,
                                    network_type);
@@ -2701,12 +2766,12 @@ process_udp_message (struct Plugin *plugin,
                      const struct UDPMessage *msg,
                      const union UdpAddress *udp_addr,
                      size_t udp_addr_len,
-                     enum GNUNET_ATS_Network_Type network_type)
+                     enum GNUNET_NetworkType network_type)
 {
   struct GNUNET_ATS_Session *s;
   struct GNUNET_HELLO_Address *address;
 
-  GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
+  GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
   if (0 != ntohl (msg->reserved))
   {
     GNUNET_break_op(0);
@@ -2742,12 +2807,11 @@ process_udp_message (struct Plugin *plugin,
   GNUNET_free (address);
 
   s->rc++;
-  GNUNET_SERVER_mst_receive (plugin->mst,
-                             s,
-                             (const char *) &msg[1],
-                             ntohs (msg->header.size) - sizeof(struct UDPMessage),
-                             GNUNET_YES,
-                             GNUNET_NO);
+  GNUNET_MST_from_buffer (s->mst,
+                          (const char *) &msg[1],
+                          ntohs (msg->header.size) - sizeof(struct UDPMessage),
+                          GNUNET_YES,
+                          GNUNET_NO);
   s->rc--;
   if ( (0 == s->rc) &&
        (GNUNET_YES == s->in_destroy) )
@@ -2905,7 +2969,7 @@ ack_proc (void *cls,
   udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
   udp_ack->delay = htonl (delay);
   udp_ack->sender = *plugin->env->my_identity;
-  memcpy (&udp_ack[1],
+  GNUNET_memcpy (&udp_ack[1],
           msg,
           ntohs (msg->size));
   enqueue (plugin,
@@ -2934,7 +2998,7 @@ read_process_fragment (struct Plugin *plugin,
                        const struct GNUNET_MessageHeader *msg,
                        const union UdpAddress *udp_addr,
                        size_t udp_addr_len,
-                       enum GNUNET_ATS_Network_Type network_type)
+                       enum GNUNET_NetworkType network_type)
 {
   struct DefragContext *d_ctx;
   struct GNUNET_TIME_Absolute now;
@@ -2955,7 +3019,7 @@ read_process_fragment (struct Plugin *plugin,
   {
     /* Create a new defragmentation context */
     d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
-    memcpy (&d_ctx[1],
+    GNUNET_memcpy (&d_ctx[1],
             udp_addr,
             udp_addr_len);
     d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
@@ -2993,8 +3057,7 @@ read_process_fragment (struct Plugin *plugin,
                                           msg))
   {
     /* keep this 'rc' from expiring */
-    GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
-                                       d_ctx->hnode,
+    GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
                                        (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
   }
   if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
@@ -3035,7 +3098,7 @@ udp_select_read (struct Plugin *plugin,
   const struct sockaddr_in6 *sa6;
   const union UdpAddress *int_addr;
   size_t int_addr_len;
-  enum GNUNET_ATS_Network_Type network_type;
+  enum GNUNET_NetworkType network_type;
 
   fromlen = sizeof (addr);
   memset (&addr,
@@ -3043,7 +3106,7 @@ udp_select_read (struct Plugin *plugin,
           sizeof(addr));
   size = GNUNET_NETWORK_socket_recvfrom (rsock,
                                          buf,
-                                         sizeof(buf),
+                                         sizeof (buf),
                                          (struct sockaddr *) &addr,
                                          &fromlen);
   sa = (const struct sockaddr *) &addr;
@@ -3072,9 +3135,12 @@ udp_select_read (struct Plugin *plugin,
   }
 
   /* Check if this is a STUN packet */
-  if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
-                                       (uint8_t *)buf,
-                                       size))
+  if (GNUNET_NO !=
+      GNUNET_NAT_stun_handle_packet (plugin->nat,
+                                    (const struct sockaddr *) &addr,
+                                    fromlen,
+                                    buf,
+                                    size))
     return; /* was STUN, do not process further */
 
   if (size < sizeof(struct GNUNET_MessageHeader))
@@ -3100,7 +3166,7 @@ udp_select_read (struct Plugin *plugin,
   if (size != ntohs (msg->size))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
-         "UDP malformed message header from %s\n",
+         "UDP malformed message (size %u) header from %s\n",
          (unsigned int) size,
          GNUNET_a2s (sa,
                      fromlen));
@@ -3285,13 +3351,13 @@ analyze_send_error (struct Plugin *plugin,
                     socklen_t slen,
                     int error)
 {
-  enum GNUNET_ATS_Network_Type type;
+  enum GNUNET_NetworkType type;
 
   type = plugin->env->get_address_type (plugin->env->cls,
                                         sa,
                                         slen);
-  if ( ( (GNUNET_ATS_NET_LAN == type) ||
-         (GNUNET_ATS_NET_WAN == type) ) &&
+  if ( ( (GNUNET_NT_LAN == type) ||
+         (GNUNET_NT_WAN == type) ) &&
        ( (ENETUNREACH == errno) ||
          (ENETDOWN == errno) ) )
   {
@@ -3471,19 +3537,17 @@ udp_select_send (struct Plugin *plugin,
  * Then reschedule this function to be called again once more is available.
  *
  * @param cls the plugin handle
- * @param tc the scheduling context
  */
 static void
-udp_plugin_select_v4 (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+udp_plugin_select_v4 (void *cls)
 {
   struct Plugin *plugin = cls;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
   plugin->select_task_v4 = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   if (NULL == plugin->sockv4)
     return;
+  tc = GNUNET_SCHEDULER_get_task_context ();
   if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
       (GNUNET_NETWORK_fdset_isset (tc->read_ready,
                                    plugin->sockv4)))
@@ -3501,19 +3565,17 @@ udp_plugin_select_v4 (void *cls,
  * Then reschedule this function to be called again once more is available.
  *
  * @param cls the plugin handle
- * @param tc the scheduling context
  */
 static void
-udp_plugin_select_v6 (void *cls,
-                      const struct GNUNET_SCHEDULER_TaskContext *tc)
+udp_plugin_select_v6 (void *cls)
 {
   struct Plugin *plugin = cls;
+  const struct GNUNET_SCHEDULER_TaskContext *tc;
 
   plugin->select_task_v6 = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   if (NULL == plugin->sockv6)
     return;
+  tc = GNUNET_SCHEDULER_get_task_context ();
   if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
        (GNUNET_NETWORK_fdset_isset (tc->read_ready,
                                     plugin->sockv6)) )
@@ -3537,13 +3599,13 @@ udp_plugin_select_v6 (void *cls,
  * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
  * @return number of sockets that were successfully bound
  */
-static int
+static unsigned int
 setup_sockets (struct Plugin *plugin,
                const struct sockaddr_in6 *bind_v6,
                const struct sockaddr_in *bind_v4)
 {
   int tries;
-  int sockets_created = 0;
+  unsigned int sockets_created = 0;
   struct sockaddr_in6 server_addrv6;
   struct sockaddr_in server_addrv4;
   const struct sockaddr *server_addr;
@@ -3753,15 +3815,14 @@ setup_sockets (struct Plugin *plugin,
   schedule_select_v4 (plugin);
   schedule_select_v6 (plugin);
   plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
-                                     GNUNET_NO,
-                                     plugin->port,
+                                    "transport-udp",
+                                    IPPROTO_UDP,
                                      sockets_created,
                                      addrs,
                                      addrlens,
                                      &udp_nat_port_map_callback,
                                      NULL,
-                                     plugin,
-                                     plugin->sockv4);
+                                     plugin);
   return sockets_created;
 }
 
@@ -3782,15 +3843,15 @@ libgnunet_plugin_transport_udp_init (void *cls)
   unsigned long long port;
   unsigned long long aport;
   unsigned long long udp_max_bps;
-  unsigned long long enable_v6;
-  unsigned long long enable_broadcasting;
-  unsigned long long enable_broadcasting_recv;
+  int enable_v6;
+  int enable_broadcasting;
+  int enable_broadcasting_recv;
   char *bind4_address;
   char *bind6_address;
   struct GNUNET_TIME_Relative interval;
   struct sockaddr_in server_addrv4;
   struct sockaddr_in6 server_addrv6;
-  int res;
+  unsigned int res;
   int have_bind4;
   int have_bind6;
 
@@ -3943,8 +4004,6 @@ libgnunet_plugin_transport_udp_init (void *cls)
   p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
                                                       GNUNET_NO);
   p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
-  p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
-                                     p);
   GNUNET_BANDWIDTH_tracker_init (&p->tracker,
                                  NULL,
                                  NULL,
@@ -3961,7 +4020,8 @@ libgnunet_plugin_transport_udp_init (void *cls)
         _("Failed to create UDP network sockets\n"));
     GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
     GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
-    GNUNET_SERVER_mst_destroy (p->mst);
+    if (NULL != p->nat)
+      GNUNET_NAT_unregister (p->nat);
     GNUNET_free (p);
     return NULL;
   }
@@ -4071,11 +4131,6 @@ libgnunet_plugin_transport_udp_done (void *cls)
     GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
     plugin->defrag_ctxs = NULL;
   }
-  if (NULL != plugin->mst)
-  {
-    GNUNET_SERVER_mst_destroy (plugin->mst);
-    plugin->mst = NULL;
-  }
   while (NULL != (udpw = plugin->ipv4_queue_head))
   {
     dequeue (plugin,