- fix
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
index 18791c264274c7414888a9d70517a0120ed0d8d9..885a26b190e330cadb55b038bba753d4949565e2 100644 (file)
@@ -63,6 +63,7 @@
 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
 
 
+#define DEBUG_MALLOC GNUNET_NO
 
 /**
  * Closure for 'append_port'.
@@ -433,6 +434,175 @@ reschedule_session_timeout (struct Session *s);
 static void
 stop_session_timeout (struct Session *s);
 
+#if DEBUG_MALLOC
+
+struct Allocator
+{
+  struct Allocator *prev;
+  struct Allocator *next;
+
+  unsigned int bytes_alloced;
+  unsigned int max_alloced;
+  unsigned int diff;
+  unsigned int line;
+
+  struct GNUNET_TIME_Absolute max_alloced_when;
+  struct GNUNET_TIME_Absolute last_alloced_when;
+
+};
+
+struct Allocator *aehead;
+struct Allocator *aetail;
+
+struct Allocation
+{
+  struct Allocation *prev;
+  struct Allocation *next;
+
+  struct Allocator *alloc;
+  unsigned int bytes_alloced;
+  void *p;
+  unsigned int line;
+};
+
+struct Allocation *ahead;
+struct Allocation *atail;
+
+static int bytes_alloced;
+
+static struct Allocator *
+find_allocator (int line)
+{
+  struct Allocator *cur = aehead;
+  while (NULL != cur)
+  {
+      if (line == cur->line)
+        return cur;
+      cur = cur->next;
+  }
+  return cur;
+}
+
+static void
+print_allocators ()
+{
+  static int start = GNUNET_YES;
+  static struct GNUNET_TIME_Absolute next;
+  static struct GNUNET_TIME_Relative rem;
+  struct Allocator *cur = aehead;
+  if (start)
+  {
+      next = GNUNET_TIME_UNIT_ZERO_ABS;
+      start = GNUNET_NO;
+  }
+  if (0 == (rem = GNUNET_TIME_absolute_get_remaining(next)).rel_value)
+  {
+      fprintf (stderr, "Allocated in `%s' total: %5u bytes\n", __FILE__, bytes_alloced);
+      while (NULL != cur)
+      {
+          char *last_alloc = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string(cur->max_alloced_when));
+          fprintf (stderr, "Allocated from line %4u :%5u bytes (diff %5i bytes, max alloc: %5u @ %s, last alloc %s)\n",
+              cur->line, cur->bytes_alloced, cur->diff, cur->max_alloced,
+              last_alloc,
+              GNUNET_STRINGS_absolute_time_to_string(cur->last_alloced_when));
+          GNUNET_free (last_alloc);
+          cur->diff = 0;
+          cur = cur->next;
+      }
+      fprintf (stderr, "\n");
+    next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS);
+  }
+}
+
+#endif
+
+static void
+MEMDEBUG_add_alloc (void *p, size_t size, int line)
+{
+#if DEBUG_MALLOC
+  struct Allocation *alloc = GNUNET_malloc (sizeof (struct Allocation));
+  struct Allocator *allocator = find_allocator(line);
+  if (NULL == allocator)
+  {
+      allocator = GNUNET_malloc (sizeof (struct Allocator));
+      allocator->line = line;
+      GNUNET_CONTAINER_DLL_insert (aehead, aetail, allocator);
+  }
+  alloc->alloc = allocator;
+  alloc->p = p;
+  alloc->line = line;
+  alloc->bytes_alloced = size;
+  allocator->bytes_alloced += size;
+  allocator->last_alloced_when = GNUNET_TIME_absolute_get();
+  if (allocator->bytes_alloced >= allocator->max_alloced)
+  {
+      allocator->max_alloced = allocator->bytes_alloced;
+      allocator->max_alloced_when = allocator->last_alloced_when;
+  }
+  allocator->diff += size;
+  GNUNET_CONTAINER_DLL_insert (ahead, atail, alloc);
+  print_allocators ();
+  bytes_alloced += size;
+#endif
+}
+
+
+static void *
+MEMDEBUG_malloc (size_t size, int line)
+{
+  void * ret;
+
+  ret = GNUNET_malloc (size);
+#if DEBUG_MALLOC
+  if (NULL != ret)
+      MEMDEBUG_add_alloc (ret, size, line);
+#endif
+  return ret;
+
+}
+
+static void
+MEMDEBUG_free (void * alloc, int line)
+{
+#if DEBUG_MALLOC
+  struct Allocation *cur;
+  struct Allocator *allocator;
+  cur = ahead;
+  while (NULL != cur)
+  {
+      if (alloc == cur->p)
+        break;
+      cur = cur->next;
+  }
+  if (NULL == cur)
+  {
+    fprintf (stderr, "Unmonitored free from line %4u\n", line);
+    GNUNET_break (0);
+    return;
+  }
+  allocator = cur->alloc;
+  if (NULL == allocator)
+  {
+      GNUNET_break (0);
+  }
+  GNUNET_CONTAINER_DLL_remove (ahead, atail, cur);
+  allocator->bytes_alloced -= cur->bytes_alloced;
+  allocator->diff -= cur->bytes_alloced;
+  GNUNET_assert (allocator->bytes_alloced >= 0);
+  bytes_alloced -= cur->bytes_alloced;
+  GNUNET_assert (bytes_alloced >= 0);
+  GNUNET_free (cur);
+#endif
+  GNUNET_free (alloc);
+}
+
+static void
+MEMDEBUG_free_non_null (void * alloc, int line)
+{
+  if (alloc != NULL)
+    MEMDEBUG_free (alloc, line);
+}
+
 
 /**
  * (re)schedule select tasks for this plugin.
@@ -584,7 +754,7 @@ udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
     {
       struct IPv4UdpAddress *u4;
       struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
-      u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
+      u4 = MEMDEBUG_malloc (sizeof (struct IPv4UdpAddress), __LINE__ );
       u4->ipv4_addr = in4->sin_addr.s_addr;
       u4->u4_port = in4->sin_port;
       *buf = u4;
@@ -595,7 +765,7 @@ udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
     {
       struct IPv6UdpAddress *u6;
       struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
-      u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
+      u6 = MEMDEBUG_malloc (sizeof (struct IPv6UdpAddress),  __LINE__ );
       u6->ipv6_addr = in6->sin6_addr;
       u6->u6_port = in6->sin6_port;
       *buf = u6;
@@ -624,12 +794,13 @@ append_port (void *cls, const char *hostname)
   if (hostname == NULL)
   {
     ppc->asc (ppc->asc_cls, NULL);
-    GNUNET_free (ppc);
+    MEMDEBUG_free (ppc, __LINE__);
     return;
   }
   GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
+  MEMDEBUG_add_alloc (ret, strlen (ret)+ 1, __LINE__);
   ppc->asc (ppc->asc_cls, ret);
-  GNUNET_free (ret);
+  MEMDEBUG_free (ret, __LINE__);
 }
 
 
@@ -705,7 +876,7 @@ udp_plugin_address_pretty_printer (void *cls, const char *type,
     asc (asc_cls, NULL);
     return;
   }
-  ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
+  ppc = MEMDEBUG_malloc (sizeof (struct PrettyPrinterContext),  __LINE__ );
   ppc->asc = asc;
   ppc->asc_cls = asc_cls;
   ppc->port = port;
@@ -717,14 +888,13 @@ static void
 call_continuation (struct UDP_MessageWrapper *udpw, int result)
 {
   size_t overhead;
-  struct UDP_MessageWrapper dummy;
 
   LOG (GNUNET_ERROR_TYPE_DEBUG,
       "Calling continuation for %u byte message to `%s' with result %s\n",
       udpw->payload_size, GNUNET_i2s (&udpw->session->target),
       (GNUNET_OK == result) ? "OK" : "SYSERR");
 
-  if ((udpw->msg_size - udpw->payload_size) >= 0)
+  if (udpw->msg_size >= udpw->payload_size)
     overhead = udpw->msg_size - udpw->payload_size;
   else
     overhead = udpw->msg_size;
@@ -737,7 +907,7 @@ call_continuation (struct UDP_MessageWrapper *udpw, int result)
           {
             /* Transport continuation */
             udpw->cont (udpw->cont_cls, &udpw->session->target, result,
-                      udpw->payload_size, overhead);
+                      udpw->payload_size, udpw->msg_size);
           }
           GNUNET_STATISTICS_update (plugin->env->stats,
                                     "# UDP, unfragmented msgs, messages, sent, success",
@@ -860,15 +1030,6 @@ call_continuation (struct UDP_MessageWrapper *udpw, int result)
           GNUNET_STATISTICS_update (plugin->env->stats,
                                     "# UDP, fragmented msgs, fragments bytes, sent, failure",
                                     udpw->msg_size, GNUNET_NO);
-
-          dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
-          dummy.msg_buf = NULL;
-          dummy.msg_size = udpw->frag_ctx->on_wire_size;
-          dummy.payload_size = udpw->frag_ctx->payload_size;
-          dummy.frag_ctx = udpw->frag_ctx;
-          dummy.session = udpw->session;
-          call_continuation (&dummy, GNUNET_SYSERR);
-
           break;
         case MSG_ACK:
           /* ACK message: failed to send */
@@ -979,19 +1140,25 @@ free_session (struct Session *s)
   if (NULL != s->frag_ctx)
   {
     GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
-    GNUNET_free (s->frag_ctx);
+    MEMDEBUG_free (s->frag_ctx, __LINE__);
     s->frag_ctx = NULL;
   }
-  GNUNET_free (s);
+  MEMDEBUG_free (s, __LINE__);
 }
 
 
 static void
 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
 {
-  GNUNET_STATISTICS_update (plugin->env->stats,
-                            "# UDP, total, bytes in buffers",
-                            -udpw->msg_size, GNUNET_NO);
+  if (plugin->bytes_in_buffer < udpw->msg_size)
+      GNUNET_break (0);
+  else
+  {
+    GNUNET_STATISTICS_update (plugin->env->stats,
+                              "# UDP, total, bytes in buffers",
+                              - (long long) udpw->msg_size, GNUNET_NO);
+    plugin->bytes_in_buffer -= udpw->msg_size;
+  }
   GNUNET_STATISTICS_update (plugin->env->stats,
                             "# UDP, total, msgs in buffers",
                             -1, GNUNET_NO);
@@ -1003,6 +1170,68 @@ dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
                                  plugin->ipv6_queue_tail, udpw);
 }
 
+static void
+fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
+{
+  struct UDP_MessageWrapper *udpw;
+  struct UDP_MessageWrapper *tmp;
+  struct UDP_MessageWrapper dummy;
+  struct Session *s = fc->session;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
+  
+  /* Call continuation for fragmented message */
+  memset (&dummy, 0, sizeof (dummy));
+  dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
+  dummy.msg_size = s->frag_ctx->on_wire_size;
+  dummy.payload_size = s->frag_ctx->payload_size;
+  dummy.frag_ctx = s->frag_ctx;
+  dummy.cont = NULL;
+  dummy.cont_cls = NULL;
+  dummy.session = s;
+
+  call_continuation (&dummy, result);
+
+  /* Remove leftover fragments from queue */
+  if (s->addrlen == sizeof (struct sockaddr_in6))
+  {
+    udpw = plugin->ipv6_queue_head;
+    while (NULL != udpw)
+    {
+      tmp = udpw->next;
+      if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
+      {
+        dequeue (plugin, udpw);
+        call_continuation (udpw, GNUNET_SYSERR);
+        MEMDEBUG_free (udpw, __LINE__);
+      }
+      udpw = tmp;
+    }
+  }
+  if (s->addrlen == sizeof (struct sockaddr_in))
+  {
+    udpw = plugin->ipv4_queue_head;
+    while (udpw!= NULL)
+    {
+      tmp = udpw->next;
+      if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
+      {
+        dequeue (plugin, udpw);
+        call_continuation (udpw, GNUNET_SYSERR);
+        MEMDEBUG_free (udpw, __LINE__);
+      }
+      udpw = tmp;
+    }
+  }
+
+  /* Destroy fragmentation context */
+  GNUNET_FRAGMENT_context_destroy (fc->frag,
+                                     &s->last_expected_msg_delay,
+                                     &s->last_expected_ack_delay);
+  s->frag_ctx = NULL;
+  MEMDEBUG_free (fc , __LINE__);
+}
+
 /**
  * Functions with this signature are called whenever we need
  * to close a session due to a disconnect or failure to
@@ -1023,6 +1252,13 @@ disconnect_session (struct Session *s)
          GNUNET_i2s (&s->target),
          GNUNET_a2s (s->sock_addr, s->addrlen));
   stop_session_timeout (s);
+
+  if (NULL != s->frag_ctx)
+  {
+    /* Remove fragmented message due to disconnect */
+    fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
+  }
+
   next = plugin->ipv4_queue_head;
   while (NULL != (udpw = next))
   {
@@ -1031,7 +1267,7 @@ disconnect_session (struct Session *s)
     {
       dequeue (plugin, udpw);
       call_continuation(udpw, GNUNET_SYSERR);
-      GNUNET_free (udpw);
+      MEMDEBUG_free (udpw, __LINE__);
     }
   }
   next = plugin->ipv6_queue_head;
@@ -1042,7 +1278,7 @@ disconnect_session (struct Session *s)
     {
       dequeue (plugin, udpw);
       call_continuation(udpw, GNUNET_SYSERR);
-      GNUNET_free (udpw);
+      MEMDEBUG_free (udpw, __LINE__);
     }
     udpw = next;
   }
@@ -1204,7 +1440,7 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
       return NULL;
     }
     t4 = addr;
-    s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
+    s = MEMDEBUG_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in),  __LINE__ );
     len = sizeof (struct sockaddr_in);
     v4 = (struct sockaddr_in *) &s[1];
     v4->sin_family = AF_INET;
@@ -1222,7 +1458,7 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
     }
     t6 = addr;
     s =
-        GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
+        MEMDEBUG_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6),  __LINE__ );
     len = sizeof (struct sockaddr_in6);
     v6 = (struct sockaddr_in6 *) &s[1];
     v6->sin6_family = AF_INET6;
@@ -1241,7 +1477,7 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
   s->addrlen = len;
   s->target = *target;
   s->sock_addr = (const struct sockaddr *) &s[1];
-  s->last_expected_ack_delay = GNUNET_TIME_UNIT_SECONDS;
+  s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
   s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
   s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
   s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
@@ -1380,13 +1616,18 @@ udp_plugin_get_session (void *cls,
   return s;
 }
 
-
 static void 
 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
 {
-  GNUNET_STATISTICS_update (plugin->env->stats,
-                            "# UDP, total, bytes in buffers",
-                            udpw->msg_size, GNUNET_NO);
+  if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
+      GNUNET_break (0);
+  else
+  {
+    GNUNET_STATISTICS_update (plugin->env->stats,
+                              "# UDP, total, bytes in buffers",
+                              udpw->msg_size, GNUNET_NO);
+    plugin->bytes_in_buffer += udpw->msg_size;
+  }
   GNUNET_STATISTICS_update (plugin->env->stats,
                             "# UDP, total, msgs in buffers",
                             1, GNUNET_NO);
@@ -1440,7 +1681,7 @@ enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
   LOG (GNUNET_ERROR_TYPE_DEBUG, 
        "Enqueuing fragment with %u bytes\n", msg_len);
   frag_ctx->fragments_used ++;
-  udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
+  udpw = MEMDEBUG_malloc (sizeof (struct UDP_MessageWrapper) + msg_len,  __LINE__ );
   udpw->session = frag_ctx->session;
   udpw->msg_buf = (char *) &udpw[1];
   udpw->msg_size = msg_len;
@@ -1532,7 +1773,7 @@ udp_plugin_send (void *cls,
   if (udpmlen <= UDP_MTU)
   {
     /* unfragmented message */
-    udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
+    udpw = MEMDEBUG_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen,  __LINE__ );
     udpw->session = s;
     udpw->msg_buf = (char *) &udpw[1];
     udpw->msg_size = udpmlen; /* message size with UDP overhead */
@@ -1559,7 +1800,7 @@ udp_plugin_send (void *cls,
     if  (s->frag_ctx != NULL)
       return GNUNET_SYSERR;
     memcpy (&udp[1], msgbuf, msgbuf_size);
-    frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
+    frag_ctx = MEMDEBUG_malloc (sizeof (struct UDP_FragmentationContext), __LINE__ );
     frag_ctx->plugin = plugin;
     frag_ctx->session = s;
     frag_ctx->cont = cont;
@@ -1567,9 +1808,6 @@ udp_plugin_send (void *cls,
     frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), 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 */
-    fprintf (stderr, "New fc with msg delay: %llu and ack delay %llu\n",
-            (unsigned long long )s->last_expected_msg_delay.rel_value,
-            (unsigned long long )s->last_expected_ack_delay.rel_value);
     frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
                                                     UDP_MTU,
                                                     &plugin->tracker,
@@ -1578,6 +1816,7 @@ udp_plugin_send (void *cls,
                                                     &udp->header,
                                                     &enqueue_fragment,
                                                     frag_ctx);    
+    s->frag_ctx = frag_ctx;
     GNUNET_STATISTICS_update (plugin->env->stats,
                               "# UDP, fragmented msgs, messages, pending",
                               1, GNUNET_NO);
@@ -1668,10 +1907,17 @@ process_inbound_tokenized_messages (void *cls, void *client,
   delay = plugin->env->receive (plugin->env->cls,
                                &si->sender,
                                hdr,
-                               (const struct GNUNET_ATS_Information *) &ats, 2,
                                si->session,
                                si->arg,
                                si->args);
+
+  plugin->env->update_address_metrics (plugin->env->cls,
+               &si->sender,
+               si->arg,
+               si->args,
+      si->session,
+                       (struct GNUNET_ATS_Information *) &ats, 2);
+
   si->session->flow_delay_for_other_peer = delay;
   reschedule_session_timeout(si->session);
   return GNUNET_OK;
@@ -1737,8 +1983,9 @@ process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
        GNUNET_a2s (sender_addr, sender_addr_len));
 
   struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
+  MEMDEBUG_add_alloc (address, GNUNET_HELLO_address_get_size(address), __LINE__);
   s = udp_plugin_get_session(plugin, address);
-  GNUNET_free (address);
+  MEMDEBUG_free (address, __LINE__);
 
   /* iterate over all embedded messages */
   si.session = s;
@@ -1850,8 +2097,8 @@ ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
   uint32_t delay = 0;
   struct UDP_MessageWrapper *udpw;
   struct Session *s;
-
   struct LookupContext l_ctx;
+
   l_ctx.addr = rc->src_addr;
   l_ctx.addrlen = rc->addr_len;
   l_ctx.res = NULL;
@@ -1873,7 +2120,7 @@ ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
                     AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
                                                                      sockaddr_in6)),
        delay);
-  udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
+  udpw = MEMDEBUG_malloc (sizeof (struct UDP_MessageWrapper) + msize,  __LINE__ );
   udpw->msg_size = msize;
   udpw->payload_size = 0;
   udpw->session = s;
@@ -1912,9 +2159,6 @@ read_process_ack (struct Plugin *plugin,
                  char *addr,
                  socklen_t fromlen)
 {
-  struct UDP_MessageWrapper dummy;
-  struct UDP_MessageWrapper * udpw;
-  struct UDP_MessageWrapper * tmp;
   const struct GNUNET_MessageHeader *ack;
   const struct UDP_ACK_Message *udp_ack;
   struct LookupContext l_ctx;
@@ -1932,15 +2176,18 @@ read_process_ack (struct Plugin *plugin,
   l_ctx.addrlen = fromlen;
   l_ctx.res = NULL;
   GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
-      &lookup_session_by_addr_it,
-      &l_ctx);
+                                        &lookup_session_by_addr_it,
+                                        &l_ctx);
   s = l_ctx.res;
 
-  if ((s == NULL) || (s->frag_ctx == NULL))
+  if ((NULL == s) || (NULL == s->frag_ctx))
+  {
     return;
+  }
 
   flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, 
+       "We received a sending delay of %llu\n",
        flow_delay.rel_value);
   s->flow_delay_from_other_peer =
       GNUNET_TIME_relative_to_absolute (flow_delay);
@@ -1955,7 +2202,6 @@ read_process_ack (struct Plugin *plugin,
 
   if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
     GNUNET_break (0);
-
   if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1970,49 +2216,9 @@ read_process_ack (struct Plugin *plugin,
        "Message full ACK'ed\n",
        (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
        GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
-  GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
-                                  &s->last_expected_msg_delay,
-                                  &s->last_expected_ack_delay);
 
-  if (s->addrlen == sizeof (struct sockaddr_in6))
-  {
-    udpw = plugin->ipv6_queue_head;
-    while (NULL != udpw)
-    {
-      tmp = udpw->next;
-      if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
-      {
-        dequeue (plugin, udpw);
-        GNUNET_free (udpw);
-      }
-      udpw = tmp;
-    }
-  }
-  if (s->addrlen == sizeof (struct sockaddr_in))
-  {
-    udpw = plugin->ipv4_queue_head;
-    while (udpw!= NULL)
-    {
-      tmp = udpw->next;
-      if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
-      {
-        dequeue (plugin, udpw);
-        GNUNET_free (udpw);
-      }
-      udpw = tmp;
-    }
-  }
-  dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
-  dummy.msg_buf = NULL;
-  dummy.msg_size = s->frag_ctx->on_wire_size;
-  dummy.payload_size = s->frag_ctx->payload_size;
-  dummy.frag_ctx = s->frag_ctx;
-  dummy.session = s;
-
-  call_continuation (&dummy, GNUNET_OK);
-
-  GNUNET_free (s->frag_ctx);
-  s->frag_ctx = NULL;
+  /* Remove fragmented message after successful sending */
+  fragmented_message_done (s->frag_ctx, GNUNET_OK);
 }
 
 
@@ -2043,7 +2249,7 @@ read_process_fragment (struct Plugin *plugin,
   if (d_ctx == NULL)
   {
     /* Create a new defragmentation context */
-    d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
+    d_ctx = MEMDEBUG_malloc (sizeof (struct DefragContext) + fromlen,  __LINE__ );
     memcpy (&d_ctx[1], addr, fromlen);
     d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
     d_ctx->addr_len = fromlen;
@@ -2083,7 +2289,7 @@ read_process_fragment (struct Plugin *plugin,
     d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
     GNUNET_assert (NULL != d_ctx);
     GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
-    GNUNET_free (d_ctx);
+    MEMDEBUG_free (d_ctx, __LINE__);
   }
 }
 
@@ -2109,14 +2315,32 @@ udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
                                       (struct sockaddr *) &addr, &fromlen);
 #if MINGW
   /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
-   * WSAECONNRESET error to indicate that previous sendto() (???)
+   * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
    * on this socket has failed.
+   * Quote from MSDN:
+   *   WSAECONNRESET - The virtual circuit was reset by the remote side
+   *   executing a hard or abortive close. The application should close
+   *   the socket; it is no longer usable. On a UDP-datagram socket this
+   *   error indicates a previous send operation resulted in an ICMP Port
+   *   Unreachable message.
    */
   if ( (-1 == size) && (ECONNRESET == errno) )
     return;
 #endif
-  if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
+  if (-1 == size)
   {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "UDP failed to receive data: %s\n", STRERROR (errno));
+    /* Connection failure or something. Not a protocol violation. */
+    return;
+  }
+  if (size < sizeof (struct GNUNET_MessageHeader))
+  {
+    LOG (GNUNET_ERROR_TYPE_WARNING,
+        "UDP got %u bytes, which is not enough for a GNUnet message header\n",
+        (unsigned int) size);
+    /* _MAY_ be a connection failure (got partial message) */
+    /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
     GNUNET_break_op (0);
     return;
   }
@@ -2175,52 +2399,81 @@ remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
     if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
     {
       /* Message timed out */
-      call_continuation (udpw, GNUNET_SYSERR);
       switch (udpw->msg_type) {
         case MSG_UNFRAGMENTED:
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, bytes, sent, timeout",
+                                    udpw->msg_size, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, messages, sent, timeout",
+                                    1, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, unfragmented msgs, messages, sent, timeout",
+                                    1, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, unfragmented msgs, bytes, sent, timeout",
+                                    udpw->payload_size, GNUNET_NO);
           /* Not fragmented message */
           LOG (GNUNET_ERROR_TYPE_DEBUG,
                "Message for peer `%s' with size %u timed out\n",
                GNUNET_i2s(&udpw->session->target), udpw->payload_size);
+          call_continuation (udpw, GNUNET_SYSERR);
+          /* Remove message */
+          dequeue (plugin, udpw);
+          MEMDEBUG_free (udpw, __LINE__);
           break;
         case MSG_FRAGMENTED:
           /* Fragmented message */
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, bytes, sent, timeout",
+                                    udpw->frag_ctx->on_wire_size, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, messages, sent, timeout",
+                                    1, GNUNET_NO);
           call_continuation (udpw, GNUNET_SYSERR);
           LOG (GNUNET_ERROR_TYPE_DEBUG,
                "Fragment for message for peer `%s' with size %u timed out\n",
                GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
-         GNUNET_FRAGMENT_context_destroy (udpw->frag_ctx->frag,
-                                          &udpw->session->last_expected_msg_delay,
-                                          &udpw->session->last_expected_ack_delay);
-          GNUNET_free (udpw->frag_ctx);
-          udpw->session->frag_ctx = NULL;
 
+
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, fragmented msgs, messages, sent, timeout",
+                                    1, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, fragmented msgs, bytes, sent, timeout",
+                                    udpw->frag_ctx->payload_size, GNUNET_NO);
+          /* Remove fragmented message due to timeout */
+          fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
           break;
         case MSG_ACK:
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, bytes, sent, timeout",
+                                    udpw->msg_size, GNUNET_NO);
+          GNUNET_STATISTICS_update (plugin->env->stats,
+                                    "# UDP, total, messages, sent, timeout",
+                                    1, GNUNET_NO);
           LOG (GNUNET_ERROR_TYPE_DEBUG,
                "ACK Message for peer `%s' with size %u timed out\n",
                GNUNET_i2s(&udpw->session->target), udpw->payload_size);
+          call_continuation (udpw, GNUNET_SYSERR);
+          dequeue (plugin, udpw);
+          MEMDEBUG_free (udpw, __LINE__);
           break;
         default:
           break;
       }
-
-      GNUNET_STATISTICS_update (plugin->env->stats,
-                                "# messages dismissed due to timeout",
-                                1, GNUNET_NO);
-      /* Remove message */
       if (sock == plugin->sockv4)
-      {
-        dequeue (plugin, udpw);
-        GNUNET_free (udpw);
         udpw = plugin->ipv4_queue_head;
-      }
-      if (sock == plugin->sockv6)
-      {
-        dequeue (plugin, udpw);
-        GNUNET_free (udpw);
+      else if (sock == plugin->sockv6)
         udpw = plugin->ipv6_queue_head;
+      else
+      {
+        GNUNET_break (0); /* should never happen */
+        udpw = NULL;
       }
+      GNUNET_STATISTICS_update (plugin->env->stats,
+                                "# messages dismissed due to timeout",
+                                1, GNUNET_NO);
     }
     else
     {
@@ -2346,7 +2599,7 @@ udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
     call_continuation (udpw, GNUNET_OK);
   }
   dequeue (plugin, udpw);
-  GNUNET_free (udpw);
+  MEMDEBUG_free (udpw, __LINE__);
   udpw = NULL;
 
   return sent;
@@ -2577,7 +2830,7 @@ libgnunet_plugin_transport_udp_init (void *cls)
   {
     /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
        initialze the plugin or the API */
-    api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+    api = MEMDEBUG_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions),  __LINE__ );
     api->cls = NULL;
     api->address_pretty_printer = &udp_plugin_address_pretty_printer;
     api->address_to_string = &udp_address_to_string;
@@ -2627,7 +2880,7 @@ libgnunet_plugin_transport_udp_init (void *cls)
          bind4_address);
     if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
     {
-      GNUNET_free (bind4_address);
+      MEMDEBUG_free (bind4_address, __LINE__);
       return NULL;
     }
   }
@@ -2644,8 +2897,8 @@ libgnunet_plugin_transport_udp_init (void *cls)
     {
       LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
            bind6_address);
-      GNUNET_free_non_null (bind4_address);
-      GNUNET_free (bind6_address);
+      MEMDEBUG_free_non_null (bind4_address, __LINE__);
+      MEMDEBUG_free (bind6_address, __LINE__);
       return NULL;
     }
   }
@@ -2663,11 +2916,12 @@ libgnunet_plugin_transport_udp_init (void *cls)
   }
   else
   {
+     MEMDEBUG_add_alloc (fancy_interval, strlen (fancy_interval)+ 1, __LINE__);
      if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
      {
        interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
      }
-     GNUNET_free (fancy_interval);
+     MEMDEBUG_free (fancy_interval, __LINE__);
   }
 
   /* Maximum datarate */
@@ -2677,8 +2931,8 @@ libgnunet_plugin_transport_udp_init (void *cls)
     udp_max_bps = 1024 * 1024 * 50;     /* 50 MB/s == infinity for practical purposes */
   }
 
-  p = GNUNET_malloc (sizeof (struct Plugin));
-  api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
+  p = MEMDEBUG_malloc (sizeof (struct Plugin),  __LINE__ );
+  api = MEMDEBUG_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions), __LINE__ );
 
   GNUNET_BANDWIDTH_tracker_init (&p->tracker,
                                  GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
@@ -2708,8 +2962,8 @@ libgnunet_plugin_transport_udp_init (void *cls)
   if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
-    GNUNET_free (p);
-    GNUNET_free (api);
+    MEMDEBUG_free (p, __LINE__);
+    MEMDEBUG_free (api, __LINE__);
     return NULL;
   }
 
@@ -2719,8 +2973,8 @@ libgnunet_plugin_transport_udp_init (void *cls)
     setup_broadcast (p, &serverAddrv6, &serverAddrv4);
   }
 
-  GNUNET_free_non_null (bind4_address);
-  GNUNET_free_non_null (bind6_address);
+  MEMDEBUG_free_non_null (bind4_address, __LINE__);
+  MEMDEBUG_free_non_null (bind6_address, __LINE__);
   return api;
 }
 
@@ -2736,7 +2990,7 @@ heap_cleanup_iterator (void *cls,
 
   GNUNET_CONTAINER_heap_remove_node (node);
   GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
-  GNUNET_free (d_ctx);
+  MEMDEBUG_free (d_ctx, __LINE__);
 
   return GNUNET_YES;
 }
@@ -2757,7 +3011,7 @@ libgnunet_plugin_transport_udp_done (void *cls)
 
   if (NULL == plugin)
   {
-    GNUNET_free (api);
+    MEMDEBUG_free (api, __LINE__);
     return NULL;
   }
 
@@ -2814,7 +3068,7 @@ libgnunet_plugin_transport_udp_done (void *cls)
     struct UDP_MessageWrapper *tmp = udpw->next;
     dequeue (plugin, udpw);
     call_continuation(udpw, GNUNET_SYSERR);
-    GNUNET_free (udpw);
+    MEMDEBUG_free (udpw, __LINE__);
 
     udpw = tmp;
   }
@@ -2824,7 +3078,7 @@ libgnunet_plugin_transport_udp_done (void *cls)
     struct UDP_MessageWrapper *tmp = udpw->next;
     dequeue (plugin, udpw);
     call_continuation(udpw, GNUNET_SYSERR);
-    GNUNET_free (udpw);
+    MEMDEBUG_free (udpw, __LINE__);
 
     udpw = tmp;
   }
@@ -2836,8 +3090,24 @@ libgnunet_plugin_transport_udp_done (void *cls)
   GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
 
   plugin->nat = NULL;
-  GNUNET_free (plugin);
-  GNUNET_free (api);
+  MEMDEBUG_free (plugin, __LINE__);
+  MEMDEBUG_free (api, __LINE__);
+#if DEBUG_MALLOC
+  struct Allocation *allocation;
+  while (NULL != ahead)
+  {
+      allocation = ahead;
+      GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
+      GNUNET_free (allocation);
+  }
+  struct Allocator *allocator;
+  while (NULL != aehead)
+  {
+      allocator = aehead;
+      GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
+      GNUNET_free (allocator);
+  }
+#endif
   return NULL;
 }