-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index aeef547a5dac19030713ecdca8fa02ca4b332687..076bef13019dccf7899aa2f7360e825bf9e790cd 100644 (file)
@@ -48,7 +48,7 @@ struct IPv4HttpAddressWrapper
    */
   struct IPv4HttpAddressWrapper *prev;
 
-  struct sockaddr_in addr;
+  struct IPv4HttpAddress addr;
 };
 
 /**
@@ -66,10 +66,7 @@ struct IPv6HttpAddressWrapper
    */
   struct IPv6HttpAddressWrapper *prev;
 
-  /**
-   * IPv6 address.
-   */
-  struct sockaddr_in6 addr GNUNET_PACKED;
+  struct IPv6HttpAddress addr6;
 };
 
 
@@ -97,6 +94,10 @@ struct PrettyPrinterContext
    * Port to add after the IP address.
    */
   uint16_t port;
+
+  uint32_t addrlen;
+
+  int numeric;
 };
 
 
@@ -117,7 +118,7 @@ static void
 append_port (void *cls, const char *hostname)
 {
   struct PrettyPrinterContext *ppc = cls;
-  char *ret;
+  static char rbuf[INET6_ADDRSTRLEN + 13];
 
   if (hostname == NULL)
   {
@@ -125,13 +126,34 @@ append_port (void *cls, const char *hostname)
     GNUNET_free (ppc);
     return;
   }
-  GNUNET_asprintf (&ret, "%s://%s:%d", ppc->plugin->protocol, hostname,
-                   ppc->plugin->port);
-  ppc->asc (ppc->asc_cls, ret);
-  GNUNET_free (ret);
+
+#if !BUILD_HTTPS
+  const char *protocol = "http";
+#else
+  const char *protocol = "https";
+#endif
+  GNUNET_assert ((strlen (hostname) + 7) < (INET6_ADDRSTRLEN + 13));
+  if (ppc->addrlen == sizeof (struct IPv6HttpAddress))
+  {
+    if (ppc->numeric == GNUNET_YES)
+      GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
+    else
+    {
+      if (strchr(hostname, ':') != NULL)
+        GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, hostname, ppc->port);
+      else
+        GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
+    }
+  }
+  else if (ppc->addrlen == sizeof (struct IPv4HttpAddress))
+    GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, hostname, ppc->port);
+  ppc->asc (ppc->asc_cls, rbuf);
 }
 
 
+
+
+
 /**
  * Convert the transports address to a nice, human-readable
  * format.
@@ -157,22 +179,38 @@ http_plugin_address_pretty_printer (void *cls, const char *type,
   GNUNET_assert (cls != NULL);
   struct PrettyPrinterContext *ppc;
   const void *sb;
+  struct sockaddr_in s4;
+  struct sockaddr_in6 s6;
   size_t sbs;
-  uint16_t port = 0 ;
+  uint16_t port = 0;
 
-  // BUG! Transport addrs over the network must NOT be 'struct sockaddr*'s!
-  if (addrlen == sizeof (struct sockaddr_in6))
+  if ((addrlen == sizeof (struct IPv6HttpAddress))  && (addr != NULL))
   {
-    sb = addr;
+    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
+    s6.sin6_family = AF_INET6;
+    s6.sin6_addr = a6->ipv6_addr;
+    s6.sin6_port = a6->u6_port;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s6.sin6_len = sizeof (struct sockaddr_in6);
+#endif
+    sb = &s6;
     sbs = sizeof (struct sockaddr_in6);
+    port = ntohs (a6->u6_port);
 
-    port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
   }
-  else if (addrlen == sizeof (struct sockaddr_in))
+  else if ((addrlen == sizeof (struct IPv4HttpAddress))  && (addr != NULL))
   {
-    sb = &addr;
+    struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
+
+    s4.sin_family = AF_INET;
+    s4.sin_addr.s_addr = a4->ipv4_addr;
+    s4.sin_port = a4->u4_port;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s4.sin_len = sizeof (struct sockaddr_in);
+#endif
+    sb = &s4;
     sbs = sizeof (struct sockaddr_in);
-    port = ntohs (((struct sockaddr_in *)addr)->sin_port);
+    port = ntohs (a4->u4_port);
   }
   else
   {
@@ -186,6 +224,8 @@ http_plugin_address_pretty_printer (void *cls, const char *type,
   ppc->asc_cls = asc_cls;
   ppc->port = port;
   ppc->plugin = cls;
+  ppc->addrlen = addrlen;
+  ppc->numeric = numeric;
   GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
 }
 
@@ -211,16 +251,23 @@ http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
   struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
   struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
 
+
+
   GNUNET_assert (cls != NULL);
   if ((addrlen != sizeof (struct sockaddr_in)) ||
       (addrlen != sizeof (struct sockaddr_in6)))
     return GNUNET_SYSERR;
 
-  if (addrlen == sizeof (struct sockaddr_in))
+  if (addrlen == sizeof (struct IPv4HttpAddress))
   {
+    struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
+
     while (w_tv4 != NULL)
     {
-      if (0 == memcmp (&w_tv4->addr, addr, sizeof (struct sockaddr_in)))
+      if ((0 ==
+           memcmp (&w_tv4->addr.ipv4_addr, &a4->ipv4_addr,
+                   sizeof (struct in_addr))) &&
+          (w_tv4->addr.u4_port == a4->u4_port))
         break;
       w_tv4 = w_tv4->next;
     }
@@ -231,11 +278,14 @@ http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
   }
   if (addrlen == sizeof (struct sockaddr_in6))
   {
+    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
+
     while (w_tv6 != NULL)
     {
-      if (0 ==
-          memcmp (&w_tv6->addr, addr,
-                  sizeof (struct sockaddr_in6)))
+      if ((0 ==
+           memcmp (&w_tv6->addr6.ipv6_addr, &a6->ipv6_addr,
+                   sizeof (struct in6_addr))) &&
+          (w_tv6->addr6.u6_port == a6->u6_port))
         break;
       w_tv6 = w_tv6->next;
     }
@@ -248,26 +298,114 @@ http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
 }
 
 struct GNUNET_TIME_Relative
-http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity * peer,
-    const struct  GNUNET_MessageHeader * message,
-    struct Session * session,
-    const char *sender_address,
-    uint16_t sender_address_len)
+http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
+                     const struct GNUNET_MessageHeader *message,
+                     struct Session *session, const char *sender_address,
+                     uint16_t sender_address_len)
 {
   struct Session *s = cls;
   struct Plugin *plugin = s->plugin;
-  struct GNUNET_TRANSPORT_ATS_Information distance[2];
   struct GNUNET_TIME_Relative delay;
+  struct GNUNET_ATS_Information atsi[2];
+
+  atsi[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
+  atsi[0].value = htonl (1);
+  atsi[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
+  atsi[1].value = session->ats_address_network_type;
+  GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
+
+  delay =
+      plugin->env->receive (plugin->env->cls, &s->target, message,
+                            (const struct GNUNET_ATS_Information *) &atsi,
+                            2, s, s->addr, s->addrlen);
+  return delay;
+}
 
-  distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
-  distance[0].value = htonl (1);
-  distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
-  distance[1].value = htonl (0);
 
-  delay = plugin->env->receive (plugin->env->cls, &s->target, message, (const struct GNUNET_TRANSPORT_ATS_Information*) &distance, 2, s, s->addr, s->addrlen);
-  return delay;
+/**
+ * Function called to convert a string address to
+ * a binary address.
+ *
+ * @param cls closure ('struct Plugin*')
+ * @param addr string address
+ * @param addrlen length of the address
+ * @param buf location to store the buffer
+ *        If the function returns GNUNET_SYSERR, its contents are undefined.
+ * @param added length of created address
+ * @return GNUNET_OK on success, GNUNET_SYSERR on failure
+ */
+int http_string_to_address (void *cls,
+                           const char *addr,
+                           uint16_t addrlen,
+                           void **buf,
+                           size_t *added)
+{
+#if !BUILD_HTTPS
+  char *protocol = "http";
+#else
+  char *protocol = "https";
+#endif
+  char *addr_str = NULL;
+  struct sockaddr_in addr_4;
+  struct sockaddr_in6 addr_6;
+  struct IPv4HttpAddress * http_4addr;
+  struct IPv6HttpAddress * http_6addr;
+  if ((addr == NULL) || (addrlen == 0) || (buf == NULL))
+    return GNUNET_SYSERR;
+
+  /* protocoll + "://" + ":" */
+  if (addrlen <= (strlen (protocol) + 4))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                     "Invalid address string `%s' to convert to address\n",
+                     addr);
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  if (NULL == (addr_str = strstr(addr, "://")))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               "Invalid address string `%s' to convert to address\n",
+               addr);
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  addr_str = &addr_str[3];
+
+  if (addr_str[strlen(addr_str)-1] == '/')
+    addr_str[strlen(addr_str)-1] = '\0';
+
+  if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv4(addr_str, strlen(addr_str), &addr_4))
+  {
+    http_4addr = GNUNET_malloc (sizeof (struct IPv4HttpAddress));
+    http_4addr->u4_port = addr_4.sin_port;
+    http_4addr->ipv4_addr = (uint32_t) addr_4.sin_addr.s_addr;
+    (*buf) = http_4addr;
+    (*added) = sizeof (struct IPv4HttpAddress);
+    return GNUNET_OK;
+  }
+  else if (GNUNET_OK == GNUNET_STRINGS_to_address_ipv6(addr_str, strlen(addr_str), &addr_6))
+  {
+    http_6addr = GNUNET_malloc (sizeof (struct IPv6HttpAddress));
+    http_6addr->u6_port = addr_6.sin6_port;
+    http_6addr->ipv6_addr = addr_6.sin6_addr;
+    (*buf) = http_6addr;
+    (*added) = sizeof (struct IPv6HttpAddress);
+    return GNUNET_OK;
+  }
+  else
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
+                     "Invalid address string `%s' to convert to address\n",
+                     addr);
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
 }
 
+
+
 /**
  * Function called for a quick conversion of the binary address to
  * a numeric address.  Note that the caller must not free the
@@ -283,43 +421,52 @@ const char *
 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
 {
 
-  struct sockaddr_in *a4;
-  struct sockaddr_in6 *a6;
+  struct IPv4HttpAddress *a4;
+  struct IPv6HttpAddress *a6;
   char *address;
   static char rbuf[INET6_ADDRSTRLEN + 13];
   uint16_t port;
   int res = 0;
 
-  if (addrlen == sizeof (struct sockaddr_in6))
+  if (addrlen == sizeof (struct IPv6HttpAddress))
   {
-    a6 = (struct sockaddr_in6 *) addr;
+    a6 = (struct IPv6HttpAddress *) addr;
     address = GNUNET_malloc (INET6_ADDRSTRLEN);
-    inet_ntop (AF_INET6, &(a6->sin6_addr), address, INET6_ADDRSTRLEN);
-    port = ntohs (a6->sin6_port);
+    GNUNET_assert (NULL !=
+                   inet_ntop (AF_INET6, &a6->ipv6_addr, address,
+                              INET6_ADDRSTRLEN));
+    port = ntohs (a6->u6_port);
   }
-  else if (addrlen == sizeof (struct sockaddr_in))
+  else if (addrlen == sizeof (struct IPv4HttpAddress))
   {
-    a4 = (struct sockaddr_in *) addr;
+    a4 = (struct IPv4HttpAddress *) addr;
     address = GNUNET_malloc (INET_ADDRSTRLEN);
-    inet_ntop (AF_INET, &(a4->sin_addr), address, INET_ADDRSTRLEN);
-    port = ntohs (a4->sin_port);
+    GNUNET_assert (NULL !=
+                   inet_ntop (AF_INET, &(a4->ipv4_addr), address,
+                              INET_ADDRSTRLEN));
+    port = ntohs (a4->u4_port);
   }
   else
   {
     /* invalid address */
+    GNUNET_break (0);
     return NULL;
   }
-#if !BUILD_HTTPS  
-  char * protocol = "http";
+#if !BUILD_HTTPS
+  char *protocol = "http";
 #else
-  char * protocol = "https";
+  char *protocol = "https";
 #endif
 
   GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
-  if (addrlen == sizeof (struct sockaddr_in6))
-    res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol, address, port);
-  else if (addrlen == sizeof (struct sockaddr_in))
-    res = GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address, port);
+  if (addrlen == sizeof (struct IPv6HttpAddress))
+    res =
+        GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://[%s]:%u/", protocol,
+                         address, port);
+  else if (addrlen == sizeof (struct IPv4HttpAddress))
+    res =
+        GNUNET_snprintf (rbuf, sizeof (rbuf), "%s://%s:%u/", protocol, address,
+                         port);
 
   GNUNET_free (address);
   GNUNET_assert (res != 0);
@@ -327,75 +474,65 @@ http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
 }
 
 struct Session *
-lookup_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
-                struct Session * session,
-                const void *addr, size_t addrlen, int force_address)
+lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
+                struct Session *session, const void *addr, size_t addrlen,
+                int force_address)
 {
-  struct Session *s = NULL;
-  struct Session *t = NULL;
+  struct Session *t;
   int e_peer;
   int e_addr;
 
-  t = plugin->head;
-  if (t == NULL)
-    return NULL;
-  while (t != NULL)
+  for (t = plugin->head; NULL != t; t = t->next)
   {
 #if 0
     GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                     "Comparing peer `%s' address `%s' len %i session %X to \n", GNUNET_i2s(target), GNUNET_a2s(addr,addrlen), addrlen, session);
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"peer `%s' address `%s' len %i session %X \n\n", GNUNET_i2s(&t->target), GNUNET_a2s(t->addr,t->addrlen), t->addrlen, t);
-    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,"memcmp %i \n", memcmp (addr, t->addr, addrlen));
+                     "Comparing peer `%s' address `%s' len %i session %X to \n",
+                     GNUNET_i2s (target), GNUNET_a2s (addr, addrlen), addrlen,
+                     session);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+                     "peer `%s' address `%s' len %i session %X \n\n",
+                     GNUNET_i2s (&t->target), GNUNET_a2s (t->addr, t->addrlen),
+                     t->addrlen, t);
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, "memcmp %i \n",
+                     memcmp (addr, t->addr, addrlen));
 #endif
     e_peer = GNUNET_NO;
     e_addr = GNUNET_NO;
-
     if (0 == memcmp (target, &t->target, sizeof (struct GNUNET_PeerIdentity)))
     {
       e_peer = GNUNET_YES;
-      if (addrlen == t->addrlen)
-      {
-        if (0 == memcmp (addr, t->addr, addrlen))
-        {
-          e_addr = GNUNET_YES;
-        }
-      }
-      if ((t == session))
-      {
-       if(t->addrlen == session->addrlen)
-       {
-        if (0 == memcmp (session->addr, t->addr, t->addrlen))
-        {
-          e_addr = GNUNET_YES;
-        }
-       }
-      }
+      if ( (addrlen == t->addrlen) &&
+          (0 == memcmp (addr, t->addr, addrlen)) )
+       e_addr = GNUNET_YES;    
+      if ( (t == session) &&
+          (t->addrlen == session->addrlen) &&
+          (0 == memcmp (session->addr, t->addr, t->addrlen)) )
+       e_addr = GNUNET_YES;
     }
 
-    if ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO))
-    {
-      s = t;
-      break;
-    }
-    if ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES))
-    {
-      s = t;
-      break;
-    }
-    if ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR))
-    {
-      s = t;
-      break;
-    }
-    if (s != NULL)
-      break;
-    t = t->next;
+    if ( ((e_peer == GNUNET_YES) && (force_address == GNUNET_NO)) ||
+        ((e_peer == GNUNET_YES) && (force_address == GNUNET_YES) && (e_addr == GNUNET_YES)) ||
+        ((e_peer == GNUNET_YES) && (force_address == GNUNET_SYSERR)) )
+      return t;
   }
+  return NULL;
+}
 
+struct Session *
+lookup_session (struct Plugin *plugin,
+                const struct GNUNET_HELLO_Address *address)
+{
+  struct Session *pos;
 
-  return s;
+  for (pos = plugin->head; NULL != pos; pos = pos->next)
+    if ( (0 == memcmp (&address->peer, &pos->target, sizeof (struct GNUNET_PeerIdentity))) &&
+        (address->address_length == pos->addrlen) &&
+        (0 == memcmp (address->address, pos->addr, pos->addrlen)) )
+      return pos;
+  return NULL;
 }
 
+
 void
 delete_session (struct Session *s)
 {
@@ -405,8 +542,8 @@ delete_session (struct Session *s)
     s->msg_tk = NULL;
   }
   GNUNET_free (s->addr);
-  GNUNET_free_non_null(s->server_recv);
-  GNUNET_free_non_null(s->server_send);
+  GNUNET_free_non_null (s->server_recv);
+  GNUNET_free_non_null (s->server_send);
   GNUNET_free (s);
 }
 
@@ -417,6 +554,8 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
 {
   struct Session *s = NULL;
 
+  GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
+                 (addrlen == sizeof (struct IPv4HttpAddress)));
   s = GNUNET_malloc (sizeof (struct Session));
   memcpy (&s->target, target, sizeof (struct GNUNET_PeerIdentity));
   s->plugin = plugin;
@@ -424,22 +563,122 @@ create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
   memcpy (s->addr, addr, addrlen);
   s->addrlen = addrlen;
   s->next = NULL;
-  s->next_receive = GNUNET_TIME_absolute_get_zero();
+  s->next_receive = GNUNET_TIME_absolute_get_zero ();
+  s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
   return s;
 }
 
 void
-notify_session_end (void *cls,
-                    const struct GNUNET_PeerIdentity *
-                    peer, struct Session * s)
+notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
+                    struct Session *s)
 {
   struct Plugin *plugin = cls;
 
-  plugin->env->session_end (NULL, peer, s);
+  plugin->env->session_end (plugin->env->cls, peer, s);
   GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
   delete_session (s);
 }
 
+/**
+ * Creates a new outbound session the transport service will use to send data to the
+ * peer
+ *
+ * @param cls the plugin
+ * @param address the address
+ * @return the session or NULL of max connections exceeded
+ */
+
+static struct Session *
+http_get_session (void *cls,
+                  const struct GNUNET_HELLO_Address *address)
+{
+  struct Plugin *plugin = cls;
+  struct Session * s = NULL;
+  struct GNUNET_ATS_Information ats;
+  size_t addrlen;
+
+  GNUNET_assert (plugin != NULL);
+  GNUNET_assert (address != NULL);
+  GNUNET_assert (address->address != NULL);
+
+  ats.type = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
+  ats.value = htonl (GNUNET_ATS_ARRAY_TERMINATOR);
+
+  /* find existing session */
+  s = lookup_session (plugin, address);
+  if (s != NULL)
+    return s;
+
+  if (plugin->max_connections <= plugin->cur_connections)
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
+                     "Maximum number of connections reached, "
+                     "cannot connect to peer `%s'\n", GNUNET_i2s (&address->peer));
+    return NULL;
+  }
+
+  /* create new session */
+  addrlen = address->address_length;
+
+  GNUNET_assert ((addrlen == sizeof (struct IPv6HttpAddress)) ||
+                 (addrlen == sizeof (struct IPv4HttpAddress)));
+
+  s = GNUNET_malloc (sizeof (struct Session));
+  memcpy (&s->target, &address->peer, sizeof (struct GNUNET_PeerIdentity));
+  s->plugin = plugin;
+  s->addr = GNUNET_malloc (address->address_length);
+  memcpy (s->addr, address->address, address->address_length);
+  s->addrlen = addrlen;
+  s->next = NULL;
+  s->next_receive = GNUNET_TIME_absolute_get_zero ();
+  s->inbound = GNUNET_NO;
+  s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
+
+  /* Get ATS type */
+  if (addrlen == sizeof (struct IPv4HttpAddress))
+  {
+    struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) address->address;
+    struct sockaddr_in s4;
+
+    s4.sin_family = AF_INET;
+    s4.sin_addr.s_addr = a4->ipv4_addr;
+    s4.sin_port = a4->u4_port;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s4.sin_len = sizeof (struct sockaddr_in);
+#endif
+    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s4, sizeof (struct sockaddr_in));
+  }
+  if (addrlen == sizeof (struct IPv6HttpAddress))
+  {
+    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
+    struct sockaddr_in6 s6;
+
+    s6.sin6_family = AF_INET6;
+    s6.sin6_addr = a6->ipv6_addr;
+    s6.sin6_port = a6->u6_port;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+    s6.sin6_len = sizeof (struct sockaddr_in6);
+#endif
+    ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
+  }
+  s->ats_address_network_type = ats.value;
+
+  /* add new session */
+  GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
+  /* initiate new connection */
+  if (GNUNET_SYSERR == client_connect (s))
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+                     "Cannot connect to peer `%s' address `%s''\n",
+                     http_plugin_address_to_string(NULL, s->addr, s->addrlen),
+                     GNUNET_i2s (&s->target));
+    GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
+    delete_session (s);
+    return NULL;
+  }
+
+  return s;
+}
 
 /**
  * Function that can be used by the transport service to transmit
@@ -450,7 +689,7 @@ notify_session_end (void *cls,
  * a fresh connection to another peer.
  *
  * @param cls closure
- * @param target who should receive this message
+ * @param session which session must be used
  * @param msgbuf the message to transmit
  * @param msgbuf_size number of bytes in 'msgbuf'
  * @param priority how important is the message (most plugins will
@@ -459,15 +698,6 @@ notify_session_end (void *cls,
  *                require plugins to discard the message after the timeout,
  *                just advisory for the desired delay; most plugins will ignore
  *                this as well)
- * @param session which session must be used (or NULL for "any")
- * @param addr the address to use (can be NULL if the plugin
- *                is "on its own" (i.e. re-use existing TCP connection))
- * @param addrlen length of the address in bytes
- * @param force_address GNUNET_YES if the plugin MUST use the given address,
- *                GNUNET_NO means the plugin may use any other address and
- *                GNUNET_SYSERR means that only reliable existing
- *                bi-directional connections should be used (regardless
- *                of address)
  * @param cont continuation to call once the message has
  *        been transmitted (or if the transport is ready
  *        for the next transmission call; or if the
@@ -478,61 +708,40 @@ notify_session_end (void *cls,
  *         and does NOT mean that the message was not transmitted (DV)
  */
 static ssize_t
-http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
-                  const char *msgbuf, size_t msgbuf_size, unsigned int priority,
-                  struct GNUNET_TIME_Relative to, struct Session *session,
-                  const void *addr, size_t addrlen, int force_address,
+http_plugin_send (void *cls,
+                  struct Session *session,
+                  const char *msgbuf, size_t msgbuf_size,
+                  unsigned int priority,
+                  struct GNUNET_TIME_Relative to,
                   GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
 {
   struct Plugin *plugin = cls;
   struct HTTP_Message *msg;
-  GNUNET_assert (plugin != NULL);
-
-  int res = GNUNET_SYSERR;
+  struct Session *tmp;
+  size_t res = -1;
 
-#if DEBUG_HTTP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Sending %u bytes to peer `%s' on address `%s' %X %i\n", msgbuf_size,
-                   GNUNET_i2s (target), ((addr != NULL) && (addrlen != 0)) ? http_plugin_address_to_string(plugin, addr, addrlen) : "<inbound>", session, force_address);
-#endif
-
-  struct Session *s = NULL;
-
-  /* look for existing connection */
-  s = lookup_session (plugin, target, session, addr, addrlen, 1);
-#if DEBUG_HTTP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "%s existing session: %s\n", (s!=NULL) ? "Found" : "NOT Found", ((s != NULL) && (s->inbound == GNUNET_YES)) ? "inbound" : "outbound");
-#endif
+  GNUNET_assert (plugin != NULL);
+  GNUNET_assert (session != NULL);
 
-  /* create new outbound connection */
-  if (s == NULL)
+  /* lookup if session is really existing */
+  tmp = plugin->head;
+  while (tmp != NULL)
   {
-    if (plugin->max_connections <= plugin->cur_connections)
-    {
-      GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
-                       "Maximum number of connections reached, "
-                       "cannot connect to peer `%s'\n",
-                       GNUNET_i2s (target));
-      return res;
-    }
-
-#if DEBUG_HTTP
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Initiiating new connection to peer `%s'\n",
-                     GNUNET_i2s (target));
-#endif
-    s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
-    GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-    // initiate new connection
-    if (GNUNET_SYSERR == (res = client_connect (s)))
-    {
-      GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
-      delete_session (s);
-      return GNUNET_SYSERR;
-    }
+    if ((tmp == session) &&
+       (0 == memcmp (&session->target, &tmp->target, sizeof (struct GNUNET_PeerIdentity))) &&
+       (session->addrlen == tmp->addrlen) &&
+       (0 == memcmp (session->addr, tmp->addr, tmp->addrlen)))
+      break;
+    tmp = tmp->next;
+  }
+  if (tmp == NULL)
+  {
+    GNUNET_break_op (0);
+    return res;
   }
 
+  /* create new message and schedule */
+
   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
   msg->next = NULL;
   msg->size = msgbuf_size;
@@ -542,28 +751,30 @@ http_plugin_send (void *cls, const struct GNUNET_PeerIdentity *target,
   msg->transmit_cont_cls = cont_cls;
   memcpy (msg->buf, msgbuf, msgbuf_size);
 
-  if (s->inbound == GNUNET_NO)
+  if (session->inbound == GNUNET_NO)
   {
 #if DEBUG_HTTP
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using outbound client session to send to `%s'\n",
-                     GNUNET_i2s (target));
+                     "Using outbound client session %p to send to `%session'\n", session,
+                     GNUNET_i2s (&session->target));
 #endif
-     client_send (s, msg);
-     res = msgbuf_size;
+
+    client_send (session, msg);
+    res = msgbuf_size;
   }
-  if (s->inbound == GNUNET_YES)
+  if (session->inbound == GNUNET_YES)
   {
-    server_send (s, msg);
-    res = msgbuf_size;
 #if DEBUG_HTTP
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using inbound server session to send to `%s'\n",
-                     GNUNET_i2s (target));
+                     "Using inbound server %p session to send to `%session'\n", session,
+                     GNUNET_i2s (&session->target));
 #endif
 
+    server_send (session, msg);
+    res = msgbuf_size;
   }
   return res;
+
 }
 
 
@@ -595,80 +806,140 @@ http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
       else
         GNUNET_assert (GNUNET_OK == server_disconnect (s));
       GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
+
+      struct HTTP_Message *msg = s->msg_head;
+      struct HTTP_Message *tmp = NULL;
+
+      while (msg != NULL)
+      {
+        tmp = msg->next;
+
+        GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
+        if (msg->transmit_cont != NULL)
+        {
+          msg->transmit_cont (msg->transmit_cont_cls, target, GNUNET_SYSERR);
+        }
+        GNUNET_free (msg);
+        msg = tmp;
+      }
+
       delete_session (s);
     }
     s = next;
   }
 }
 
-static void
-nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
-                 socklen_t addrlen)
+static void *
+find_address (struct Plugin *plugin, const struct sockaddr *addr, socklen_t addrlen)
 {
-  struct Plugin *plugin = cls;
+  int af;
   struct IPv4HttpAddressWrapper *w_t4 = NULL;
   struct IPv6HttpAddressWrapper *w_t6 = NULL;
-  int af;
 
   af = addr->sa_family;
   switch (af)
   {
   case AF_INET:
     w_t4 = plugin->ipv4_addr_head;
+    struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
+
     while (w_t4 != NULL)
     {
-      int res = memcmp (&w_t4->addr,
-                        (struct sockaddr_in *) addr,
-                        sizeof (struct sockaddr_in));
+      int res = memcmp (&w_t4->addr.ipv4_addr,
+                        &a4->sin_addr,
+                        sizeof (struct in_addr));
+
+      if (res == 0)
+      {
+        if (a4->sin_port != w_t4->addr.u4_port)
+          res = -1;
+      }
 
       if (0 == res)
         break;
       w_t4 = w_t4->next;
     }
-    if (w_t4 == NULL)
-    {
-      w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
-      memcpy (&w_t4->addr, (struct sockaddr_in *) addr,
-              sizeof (struct sockaddr_in));
-
-      GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
-                                   plugin->ipv4_addr_tail, w_t4);
-    }
-#if DEBUG_HTTP
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Notifying transport to add IPv4 address `%s'\n",
-                     http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr, sizeof (struct sockaddr_in));
-
+    return w_t4;
     break;
   case AF_INET6:
     w_t6 = plugin->ipv6_addr_head;
+    struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
+
     while (w_t6)
     {
-      int res = memcmp (&w_t6->addr,
-                        (struct sockaddr_in6 *) addr,
-                        sizeof (struct sockaddr_in6));
+      int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
+                        sizeof (struct in6_addr));
 
+      if (res == 0)
+      {
+        if (a6->sin6_port != w_t6->addr6.u6_port)
+          res = -1;
+      }
       if (0 == res)
         break;
       w_t6 = w_t6->next;
     }
+    return w_t6;
+    break;
+  default:
+    return NULL;
+  }
+  return NULL;
+}
+
+static void
+nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
+                 socklen_t addrlen)
+{
+  struct Plugin *plugin = cls;
+  struct IPv4HttpAddressWrapper *w_t4 = NULL;
+  struct IPv6HttpAddressWrapper *w_t6 = NULL;
+  int af;
+
+  af = addr->sa_family;
+  switch (af)
+  {
+  case AF_INET:
+    w_t4 = find_address (plugin, addr, addrlen);
+    if (w_t4 == NULL)
+    {
+      struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
+      w_t4 = GNUNET_malloc (sizeof (struct IPv4HttpAddressWrapper));
+      memcpy (&w_t4->addr.ipv4_addr, &a4->sin_addr, sizeof (struct in_addr));
+      w_t4->addr.u4_port = a4->sin_port;
+
+      GNUNET_CONTAINER_DLL_insert (plugin->ipv4_addr_head,
+                                   plugin->ipv4_addr_tail, w_t4);
+
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     "Notifying transport to add IPv4 address `%s'\n",
+                     http_plugin_address_to_string (NULL, &w_t4->addr,
+                                                    sizeof (struct
+                                                            IPv4HttpAddress)));
+    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
+                                 sizeof (struct IPv4HttpAddress));
+    }
+    break;
+  case AF_INET6:
+    w_t6 = find_address (plugin, addr, addrlen);
     if (w_t6 == NULL)
     {
       w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
-      memcpy (&w_t6->addr, (struct sockaddr_in6 *) addr,
-              sizeof (struct sockaddr_in6));
+      struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
+      memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
+      w_t6->addr6.u6_port = a6->sin6_port;
 
       GNUNET_CONTAINER_DLL_insert (plugin->ipv6_addr_head,
                                    plugin->ipv6_addr_tail, w_t6);
-    }
-#if DEBUG_HTTP
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Notifying transport to add IPv6 address `%s'\n",
-                     http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr, sizeof (struct sockaddr_in6));
+                     http_plugin_address_to_string (NULL, &w_t6->addr6,
+                                                    sizeof (struct
+                                                            IPv6HttpAddress)));
+      plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
+                                 sizeof (struct IPv6HttpAddress));
+    }
     break;
   default:
     return;
@@ -689,53 +960,36 @@ nat_remove_address (void *cls, int add_remove, const struct sockaddr *addr,
   switch (af)
   {
   case AF_INET:
-    w_t4 = plugin->ipv4_addr_head;
-    while (w_t4 != NULL)
-    {
-      int res = memcmp (&(w_t4->addr.sin_addr),
-                        &((struct sockaddr_in *) addr)->sin_addr,
-                        sizeof (struct in_addr));
-
-      if (0 == res)
-        break;
-      w_t4 = w_t4->next;
-    }
+    w_t4 = find_address (plugin, addr, addrlen);
     if (w_t4 == NULL)
       return;
 
-#if DEBUG_HTTP
+
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Notifying transport to remove IPv4 address `%s'\n",
-                     http_plugin_address_to_string(NULL, &w_t4->addr, sizeof (struct sockaddr_in)));
-#endif
+                     http_plugin_address_to_string (NULL, &w_t4->addr,
+                                                    sizeof (struct
+                                                            IPv4HttpAddress)));
     plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
-                                 sizeof (struct sockaddr_in));
+                                 sizeof (struct IPv4HttpAddress));
 
     GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
                                  w_t4);
     GNUNET_free (w_t4);
     break;
   case AF_INET6:
-    w_t6 = plugin->ipv6_addr_head;
-    while (w_t6 != NULL)
-    {
-      int res = memcmp (&(w_t6->addr.sin6_addr),
-                        &((struct sockaddr_in6 *) addr)->sin6_addr,
-                        sizeof (struct in6_addr));
-
-      if (0 == res)
-        break;
-      w_t6 = w_t6->next;
-    }
+    w_t6 = find_address (plugin, addr, addrlen);
     if (w_t6 == NULL)
       return;
-#if DEBUG_HTTP
+
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                      "Notifying transport to remove IPv6 address `%s'\n",
-                     http_plugin_address_to_string(NULL, &w_t6->addr, sizeof (struct sockaddr_in6)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr,
-                                 sizeof (struct sockaddr_in6 ));
+                     http_plugin_address_to_string (NULL, &w_t6->addr6,
+                                                    sizeof (struct
+                                                            IPv6HttpAddress)));
+
+    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
+                                 sizeof (struct IPv6HttpAddress));
 
     GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
                                  w_t6);
@@ -761,19 +1015,17 @@ nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
                        socklen_t addrlen)
 {
   GNUNET_assert (cls != NULL);
-#if DEBUG_HTTP
   struct Plugin *plugin = cls;
-#endif
-#if DEBUG_HTTP
+
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "NPMC called %s to address `%s'\n",
                    (add_remove == GNUNET_NO) ? "remove" : "add",
                    GNUNET_a2s (addr, addrlen));
-#endif
+
   switch (add_remove)
   {
   case GNUNET_YES:
-      nat_add_address (cls, add_remove, addr, addrlen);
+    nat_add_address (cls, add_remove, addr, addrlen);
     break;
   case GNUNET_NO:
     nat_remove_address (cls, add_remove, addr, addrlen);
@@ -785,6 +1037,7 @@ void
 http_check_ipv6 (struct Plugin *plugin)
 {
   struct GNUNET_NETWORK_Handle *desc = NULL;
+
   if (plugin->ipv6 == GNUNET_YES)
   {
     /* probe IPv6 support */
@@ -797,8 +1050,8 @@ http_check_ipv6 (struct Plugin *plugin)
         GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
       }
       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, plugin->name,
-                  _
-                  ("Disabling IPv6 since it is not supported on this system!\n"));
+                       _
+                       ("Disabling IPv6 since it is not supported on this system!\n"));
       plugin->ipv6 = GNUNET_NO;
     }
     else
@@ -807,17 +1060,16 @@ http_check_ipv6 (struct Plugin *plugin)
       desc = NULL;
     }
 
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-              "Testing IPv6 on this system: %s\n", (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     "Testing IPv6 on this system: %s\n",
+                     (plugin->ipv6 == GNUNET_YES) ? "successful" : "failed");
   }
 }
 
 int
-http_get_addresses (struct Plugin *plugin,
-                    const char *serviceName,
-                    const struct GNUNET_CONFIGURATION_Handle
-                    *cfg, struct sockaddr ***addrs,
-                    socklen_t ** addr_lens)
+http_get_addresses (struct Plugin *plugin, const char *serviceName,
+                    const struct GNUNET_CONFIGURATION_Handle *cfg,
+                    struct sockaddr ***addrs, socklen_t ** addr_lens)
 {
   int disablev6;
   unsigned long long port;
@@ -864,8 +1116,8 @@ http_get_addresses (struct Plugin *plugin,
   if (hostname != NULL)
   {
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                "Resolving `%s' since that is where `%s' will bind to.\n",
-                hostname, serviceName);
+                     "Resolving `%s' since that is where `%s' will bind to.\n",
+                     hostname, serviceName);
     memset (&hints, 0, sizeof (struct addrinfo));
     if (disablev6)
       hints.ai_family = AF_INET;
@@ -910,8 +1162,8 @@ http_get_addresses (struct Plugin *plugin,
       if ((pos->ai_socktype != SOCK_STREAM) && (pos->ai_socktype != 0))
         continue;               /* huh? */
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                  "Service will bind to `%s'\n",
-                  GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
+                       "Service will bind to `%s'\n", GNUNET_a2s (pos->ai_addr,
+                                                                  pos->ai_addrlen));
       if (pos->ai_family == AF_INET)
       {
         GNUNET_assert (pos->ai_addrlen == sizeof (struct sockaddr_in));
@@ -991,10 +1243,10 @@ start_report_addresses (struct Plugin *plugin)
   socklen_t *addrlens;
 
   res =
-      http_get_addresses (plugin, plugin->name, plugin->env->cfg,
-                                           &addrs, &addrlens);
+      http_get_addresses (plugin, plugin->name, plugin->env->cfg, &addrs,
+                          &addrlens);
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   _("Found %u addresses to report to NAT service\n"),res);
+                   _("Found %u addresses to report to NAT service\n"), res);
 
   if (res != GNUNET_SYSERR)
   {
@@ -1002,14 +1254,12 @@ start_report_addresses (struct Plugin *plugin)
         GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
                              (unsigned int) res,
                              (const struct sockaddr **) addrs, addrlens,
-                             &nat_port_map_callback, NULL,
-                             plugin);
+                             &nat_port_map_callback, NULL, plugin);
     while (res > 0)
     {
       res--;
 #if 0
-      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                       _("FREEING %s\n"),
+      GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name, _("FREEING %s\n"),
                        GNUNET_a2s (addrs[res], addrlens[res]));
 #endif
       GNUNET_assert (addrs[res] != NULL);
@@ -1108,24 +1358,28 @@ configure_plugin (struct Plugin *plugin)
   if (plugin->port == 0)
   {
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   _("Port 0, client only mode\n"));
+                     _("Port 0, client only mode\n"));
     plugin->client_only = GNUNET_YES;
   }
 
-  char * bind4_address = NULL;
-  if ((plugin->ipv4 == GNUNET_YES) && (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
-                                             "BINDTO", &bind4_address)))
+  char *bind4_address = NULL;
+
+  if ((plugin->ipv4 == GNUNET_YES) &&
+      (GNUNET_YES ==
+       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
+                                              "BINDTO", &bind4_address)))
   {
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                "Binding %s plugin to specific IPv4 address: `%s'\n",
-                plugin->protocol, bind4_address);
+                     "Binding %s plugin to specific IPv4 address: `%s'\n",
+                     plugin->protocol, bind4_address);
     plugin->server_addr_v4 = GNUNET_malloc (sizeof (struct sockaddr_in));
-    if (1 != inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
+    if (1 !=
+        inet_pton (AF_INET, bind4_address, &plugin->server_addr_v4->sin_addr))
     {
       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                  _("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
-                  bind4_address, plugin->protocol);
+                       _
+                       ("Specific IPv4 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
+                       bind4_address, plugin->protocol);
       GNUNET_free (plugin->server_addr_v4);
       plugin->server_addr_v4 = NULL;
     }
@@ -1138,20 +1392,24 @@ configure_plugin (struct Plugin *plugin)
   }
 
 
-  char * bind6_address = NULL;
-  if ((plugin->ipv6 == GNUNET_YES) && (GNUNET_YES ==
-      GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
-                                             "BINDTO6", &bind6_address)))
+  char *bind6_address = NULL;
+
+  if ((plugin->ipv6 == GNUNET_YES) &&
+      (GNUNET_YES ==
+       GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
+                                              "BINDTO6", &bind6_address)))
   {
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                "Binding %s plugin to specific IPv6 address: `%s'\n",
-                plugin->protocol, bind6_address);
+                     "Binding %s plugin to specific IPv6 address: `%s'\n",
+                     plugin->protocol, bind6_address);
     plugin->server_addr_v6 = GNUNET_malloc (sizeof (struct sockaddr_in6));
-    if (1 != inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
+    if (1 !=
+        inet_pton (AF_INET6, bind6_address, &plugin->server_addr_v6->sin6_addr))
     {
       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
-                  _("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
-                  bind6_address, plugin->protocol);
+                       _
+                       ("Specific IPv6 address `%s' for plugin %s in configuration file is invalid! Binding to all addresses!\n"),
+                       bind6_address, plugin->protocol);
       GNUNET_free (plugin->server_addr_v6);
       plugin->server_addr_v6 = NULL;
     }
@@ -1166,6 +1424,7 @@ configure_plugin (struct Plugin *plugin)
 
   /* Optional parameters */
   unsigned long long maxneigh;
+
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg, plugin->name,
                                              "MAX_CONNECTIONS", &maxneigh))
@@ -1187,15 +1446,29 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
   struct Plugin *plugin;
   int res;
 
+  if (NULL == env->receive)
+  {
+    /* 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->cls = NULL;
+    api->address_pretty_printer = &http_plugin_address_pretty_printer;
+    api->address_to_string = &http_plugin_address_to_string;
+    api->string_to_address = &http_string_to_address;
+    return api;
+  }
+
   plugin = GNUNET_malloc (sizeof (struct Plugin));
   plugin->env = env;
   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
   api->cls = plugin;
-  api->send = &http_plugin_send;
   api->disconnect = &http_plugin_disconnect;
   api->address_pretty_printer = &http_plugin_address_pretty_printer;
   api->check_address = &http_plugin_address_suggested;
   api->address_to_string = &http_plugin_address_to_string;
+  api->string_to_address = &http_string_to_address;
+  api->get_session = &http_get_session;
+  api->send = &http_plugin_send;
 
 #if BUILD_HTTPS
   plugin->name = "transport-https";
@@ -1265,7 +1538,13 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
 {
   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
-  struct Session *s = NULL;
+  struct Session *s;
+
+  if (NULL == plugin)
+  {
+    GNUNET_free (api);
+    return NULL;
+  }
 
   /* Stop reporting addresses to transport service */
   stop_report_addresses (plugin);
@@ -1275,8 +1554,8 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
   while (s != NULL)
   {
 #if DEBUG_HTTP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     "Disconnecting `%s' \n", GNUNET_i2s (&s->target));
 #endif
     if (s->inbound == GNUNET_NO)
       GNUNET_assert (GNUNET_OK == client_disconnect (s));
@@ -1286,15 +1565,13 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
   }
 
 #if DEBUG_HTTP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Stopping server\n");
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping server\n");
 #endif
   /* Stop server */
   server_stop (plugin);
 
 #if DEBUG_HTTP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                   "Stopping client\n");
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name, "Stopping client\n");
 #endif
   /* Stop client */
   client_stop (plugin);
@@ -1304,7 +1581,25 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
   while (s != NULL)
   {
     struct Session *t = s->next;
+
     GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
+
+    struct HTTP_Message *msg = s->msg_head;
+    struct HTTP_Message *tmp = NULL;
+
+    while (msg != NULL)
+    {
+      tmp = msg->next;
+
+      GNUNET_CONTAINER_DLL_remove (s->msg_head, s->msg_tail, msg);
+      if (msg->transmit_cont != NULL)
+      {
+        msg->transmit_cont (msg->transmit_cont_cls, &s->target, GNUNET_SYSERR);
+      }
+      GNUNET_free (msg);
+      msg = tmp;
+    }
+
     delete_session (s);
     s = t;
   }