-some minor code cleanup
[oweals/gnunet.git] / src / transport / plugin_transport_http.c
index fb9376526f06d555596a6b8e165761f22c52a715..937f4f62f486a9e92e9b39b14b1e028cafcfacb5 100644 (file)
  */
 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
 
-/**
- * Wrapper to manage IPv4 addresses
- */
-struct IPv4HttpAddressWrapper
-{
-  /**
-   * Linked list next
-   */
-  struct IPv4HttpAddressWrapper *next;
-
-  /**
-   * Linked list previous
-   */
-  struct IPv4HttpAddressWrapper *prev;
-
-  struct IPv4HttpAddress addr;
-};
 
 /**
- * Wrapper for IPv4 addresses.
+ * Wrapper to manage addresses
  */
-struct IPv6HttpAddressWrapper
+struct HttpAddressWrapper
 {
   /**
    * Linked list next
    */
-  struct IPv6HttpAddressWrapper *next;
+  struct HttpAddressWrapper *next;
 
   /**
    * Linked list previous
    */
-  struct IPv6HttpAddressWrapper *prev;
+  struct HttpAddressWrapper *prev;
 
-  struct IPv6HttpAddress addr6;
+  struct HttpAddress *addr;
 };
 
-
 /**
  * Context for address to string conversion.
  */
@@ -106,50 +88,23 @@ struct PrettyPrinterContext
  */
 struct Plugin;
 
-
-
 /**
- * Append our port and forward the result.
- *
- * @param cls the 'struct PrettyPrinterContext*'
- * @param hostname hostname part of the address
+ * Start session timeout
  */
 static void
-append_port (void *cls, const char *hostname)
-{
-  struct PrettyPrinterContext *ppc = cls;
-  static char rbuf[INET6_ADDRSTRLEN + 13];
-
-  if (hostname == NULL)
-  {
-    ppc->asc (ppc->asc_cls, NULL);
-    GNUNET_free (ppc);
-    return;
-  }
+start_session_timeout (struct Session *s);
 
-#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);
-}
+/**
+ * Increment session timeout due to activity
+ */
+static void
+reschedule_session_timeout (struct Session *s);
 
+/**
+ * Cancel timeout
+ */
+static void
+stop_session_timeout (struct Session *s);
 
 /**
  * Convert the transports address to a nice, human-readable
@@ -174,56 +129,16 @@ http_plugin_address_pretty_printer (void *cls, const char *type,
                                     void *asc_cls)
 {
   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;
-
-  if ((addrlen == sizeof (struct IPv6HttpAddress))  && (addr != NULL))
-  {
-    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);
+  struct HttpAddress *haddr = (struct HttpAddress *) addr;
 
-  }
-  else if ((addrlen == sizeof (struct IPv4HttpAddress))  && (addr != NULL))
-  {
-    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 (a4->u4_port);
-  }
-  else
+  if (addrlen < (sizeof (struct HttpAddress)))
   {
     /* invalid address */
     GNUNET_break_op (0);
     asc (asc_cls, NULL);
     return;
   }
-  ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
-  ppc->asc = asc;
-  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);
+  asc (asc_cls, haddr->addr);
 }
 
 
@@ -243,55 +158,25 @@ http_plugin_address_pretty_printer (void *cls, const char *type,
 static int
 http_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
 {
-
   struct Plugin *plugin = cls;
-  struct IPv4HttpAddressWrapper *w_tv4 = plugin->ipv4_addr_head;
-  struct IPv6HttpAddressWrapper *w_tv6 = plugin->ipv6_addr_head;
-
-
+  struct HttpAddressWrapper *w = plugin->addr_head;
+  struct HttpAddress *haddr = (struct HttpAddress *) addr;
 
   GNUNET_assert (cls != NULL);
-  if ((addrlen != sizeof (struct sockaddr_in)) ||
-      (addrlen != sizeof (struct sockaddr_in6)))
-    return GNUNET_SYSERR;
 
-  if (addrlen == sizeof (struct IPv4HttpAddress))
-  {
-    struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
+  if (addrlen <= sizeof (struct HttpAddress))
+    return GNUNET_SYSERR;
 
-    while (w_tv4 != NULL)
-    {
-      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;
-    }
-    if (w_tv4 != NULL)
+  if (0 == (strcmp (plugin->ext_addr->addr, haddr->addr)))
       return GNUNET_OK;
-    else
-      return GNUNET_SYSERR;
-  }
-  if (addrlen == sizeof (struct sockaddr_in6))
-  {
-    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
 
-    while (w_tv6 != NULL)
-    {
-      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;
-    }
-    if (w_tv6 != NULL)
-      return GNUNET_OK;
-    else
-      return GNUNET_SYSERR;
+  while (NULL != w)
+  {
+      if (0 == (strcmp (w->addr->addr, haddr->addr)))
+          return GNUNET_OK;
+      w = w->next;
   }
-  return GNUNET_OK;
+  return GNUNET_SYSERR;
 }
 
 struct GNUNET_TIME_Relative
@@ -303,21 +188,121 @@ http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct Session *s = cls;
   struct Plugin *plugin = s->plugin;
   struct GNUNET_TIME_Relative delay;
-  struct GNUNET_ATS_Information atsi[2];
+  struct GNUNET_ATS_Information atsi;
 
-  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;
+  atsi.type = htonl (GNUNET_ATS_NETWORK_TYPE);
+  atsi.value = session->ats_address_network_type;
   GNUNET_break (session->ats_address_network_type != ntohl (GNUNET_ATS_NET_UNSPECIFIED));
 
+  reschedule_session_timeout (session);
+
   delay =
       plugin->env->receive (plugin->env->cls, &s->target, message,
-                            (const struct GNUNET_ATS_Information *) &atsi,
-                            2, s, s->addr, s->addrlen);
+                            &atsi,
+                            1, 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 0
+#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 ((NULL == addr) || (addrlen == 0))
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  if ('\0' != addr[addrlen - 1])
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+
+  if (strlen (addr) != addrlen - 1)
+  {
+    GNUNET_break (0);
+    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;
+  }
+  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;
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+             "Invalid address string `%s' to convert to address\n",
+             addr_str);
+  GNUNET_break (0);
+  return GNUNET_SYSERR;
+#endif
+}
+
+
 /**
  * Function called for a quick conversion of the binary address to
  * a numeric address.  Note that the caller must not free the
@@ -332,81 +317,40 @@ http_plugin_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
 const char *
 http_plugin_address_to_string (void *cls, const void *addr, size_t addrlen)
 {
-
-  struct IPv4HttpAddress *a4;
-  struct IPv6HttpAddress *a6;
-  char *address;
-  static char rbuf[INET6_ADDRSTRLEN + 13];
-  uint16_t port;
-  int res = 0;
-
-  if (addrlen == sizeof (struct IPv6HttpAddress))
-  {
-    a6 = (struct IPv6HttpAddress *) addr;
-    address = GNUNET_malloc (INET6_ADDRSTRLEN);
-    GNUNET_assert (NULL !=
-                   inet_ntop (AF_INET6, &a6->ipv6_addr, address,
-                              INET6_ADDRSTRLEN));
-    port = ntohs (a6->u6_port);
-  }
-  else if (addrlen == sizeof (struct IPv4HttpAddress))
+  struct HttpAddress *haddr;
+  if (addrlen < sizeof (struct HttpAddress))
   {
-    a4 = (struct IPv4HttpAddress *) addr;
-    address = GNUNET_malloc (INET_ADDRSTRLEN);
-    GNUNET_assert (NULL !=
-                   inet_ntop (AF_INET, &(a4->ipv4_addr), address,
-                              INET_ADDRSTRLEN));
-    port = ntohs (a4->u4_port);
+      /* invalid address */
+      GNUNET_break (0);
+      return NULL;
   }
   else
   {
-    /* invalid address */
-    GNUNET_break (0);
-    return NULL;
+      haddr = (struct HttpAddress *) addr;
+      GNUNET_assert (NULL != haddr->addr);
+      return (const char *) haddr->addr;
   }
-#if !BUILD_HTTPS
-  char *protocol = "http";
-#else
-  char *protocol = "https";
-#endif
-
-  GNUNET_assert (strlen (address) + 7 < (INET6_ADDRSTRLEN + 13));
-  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);
-  return rbuf;
 }
 
+
 struct Session *
 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",
+                     "Comparing peer `%s' address `%s' len %i session %p 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",
+                     "peer `%s' address `%s' len %i session %p \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",
@@ -414,80 +358,86 @@ lookup_session_old (struct Plugin *plugin, const struct GNUNET_PeerIdentity *tar
 #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 s;
+  return NULL;
 }
 
+
 struct Session *
 lookup_session (struct Plugin *plugin,
                 const struct GNUNET_HELLO_Address *address)
 {
-  struct Session *tmp = NULL;
+  struct Session *pos;
 
-  tmp = plugin->head;
-  if (tmp == NULL)
-    return NULL;
-  while (tmp != NULL)
-  {
-    if (0 != memcmp (&address->peer, &tmp->target, sizeof (struct GNUNET_PeerIdentity)))
-      continue;
-    if ((address->address_length != tmp->addrlen) &&
-        (0 != memcmp (address->address, tmp->addr, tmp->addrlen)))
-      continue;
+  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;
+}
 
-    return tmp;
+
+int
+exist_session (struct Plugin *plugin, struct Session *s)
+{
+  struct Session * head;
+
+  GNUNET_assert (NULL != plugin);
+  GNUNET_assert (NULL != s);
+
+  for (head = plugin->head; head != NULL; head = head->next)
+  {
+    if (head == s)
+      return GNUNET_YES;
   }
-  return NULL;
+  return GNUNET_NO;
 }
 
+/**
+ * Deleting the session
+ * Must not be used afterwards
+ */
 
 void
 delete_session (struct Session *s)
 {
+  struct Plugin *plugin = s->plugin;
+  stop_session_timeout(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, &s->target, GNUNET_SYSERR);
+    }
+    GNUNET_free (msg);
+    msg = tmp;
+  }
+
   if (s->msg_tk != NULL)
   {
     GNUNET_SERVER_mst_destroy (s->msg_tk);
@@ -499,38 +449,55 @@ delete_session (struct Session *s)
   GNUNET_free (s);
 }
 
+
 struct Session *
 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
-                const void *addr, size_t addrlen,
-                GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
+                const void *addr, size_t addrlen)
 {
   struct Session *s = NULL;
+  struct GNUNET_ATS_Information ats;
+
+  /*
+   * ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) &s6, sizeof (struct sockaddr_in6));
+   */
+  if (addrlen < sizeof (struct HttpAddress))
+  {
+      GNUNET_break (0);
+      return 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;
   s->addr = GNUNET_malloc (addrlen);
   memcpy (s->addr, addr, addrlen);
   s->addrlen = addrlen;
-  s->next = NULL;
-  s->next_receive = GNUNET_TIME_absolute_get_zero ();
-  s->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
+  s->ats_address_network_type = ats.value;
+
+
+  start_session_timeout(s);
   return s;
 }
 
+
 void
 notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
                     struct Session *s)
 {
   struct Plugin *plugin = cls;
 
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   "Notifying transport about ending session %p (`%s')\n",
+                   s,
+                   http_plugin_address_to_string(NULL, s->addr,s->addrlen));
+
   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
@@ -539,18 +506,17 @@ notify_session_end (void *cls, const struct GNUNET_PeerIdentity *peer,
  * @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);
 
   /* find existing session */
   s = lookup_session (plugin, address);
@@ -568,48 +534,9 @@ http_get_session (void *cls,
   /* 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)) && (address->address != NULL))
-  {
-    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)) && (address->address != NULL))
-  {
-    struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) address->address;
-    struct sockaddr_in6 s6;
+  GNUNET_assert (addrlen > sizeof (struct HttpAddress));
 
-    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;
+  s = create_session (plugin, &address->peer, address->address, address->address_length);
 
   /* add new session */
   GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
@@ -628,6 +555,7 @@ http_get_session (void *cls,
   return s;
 }
 
+
 /**
  * Function that can be used by the transport service to transmit
  * a message using the plugin.   Note that in the case of a
@@ -689,7 +617,6 @@ http_plugin_send (void *cls,
   }
 
   /* create new message and schedule */
-
   msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
   msg->next = NULL;
   msg->size = msgbuf_size;
@@ -699,25 +626,21 @@ http_plugin_send (void *cls,
   msg->transmit_cont_cls = cont_cls;
   memcpy (msg->buf, msgbuf, msgbuf_size);
 
+  reschedule_session_timeout (session);
+
   if (session->inbound == GNUNET_NO)
   {
-#if DEBUG_HTTP
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using outbound client session %p to send to `%session'\n", session,
+                     "Using outbound client session %p to send to `%s'\n", session,
                      GNUNET_i2s (&session->target));
-#endif
-
     client_send (session, msg);
     res = msgbuf_size;
   }
   if (session->inbound == GNUNET_YES)
   {
-#if DEBUG_HTTP
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using inbound server %p session to send to `%session'\n", session,
+                     "Using inbound server %p session to send to `%s'\n", session,
                      GNUNET_i2s (&session->target));
-#endif
-
     server_send (session, msg);
     res = msgbuf_size;
   }
@@ -725,189 +648,6 @@ http_plugin_send (void *cls,
 
 }
 
-/**
- * Function that can be used by the transport service to transmit
- * a message using the plugin.   Note that in the case of a
- * peer disconnecting, the continuation MUST be called
- * prior to the disconnect notification itself.  This function
- * will be called with this peer's HELLO message to initiate
- * a fresh connection to another peer.
- *
- * @param cls closure
- * @param target who should receive this message
- * @param msgbuf the message to transmit
- * @param msgbuf_size number of bytes in 'msgbuf'
- * @param priority how important is the message (most plugins will
- *                 ignore message priority and just FIFO)
- * @param to how long to wait at most for the transmission (does not
- *                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
- *        peer disconnected...); can be NULL
- * @param cont_cls closure for cont
- * @return number of bytes used (on the physical network, with overheads);
- *         -1 on hard errors (i.e. address invalid); 0 is a legal value
- *         and does NOT mean that the message was not transmitted (DV)
- */
-static ssize_t
-http_plugin_send_old (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,
-                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
-{
-  struct Plugin *plugin = cls;
-  struct HTTP_Message *msg;
-  struct Session *s;
-
-  GNUNET_assert (plugin != NULL);
-
-  int res = GNUNET_SYSERR;
-
-#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
-
-
-
-  if (addrlen != 0)
-    GNUNET_assert ((addrlen == sizeof (struct IPv4HttpAddress)) ||
-                   (addrlen == sizeof (struct IPv6HttpAddress)));
-  /* look for existing connection */
-  s = lookup_session_old (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
-  /* create new outbound connection */
-  if (s == 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
-/* AAAAAAAAAAAAAAAAAAA */
-    int res = GNUNET_OK;
-    struct GNUNET_ATS_Information ats;
-    if ((addrlen == sizeof (struct IPv4HttpAddress)) && (addr != NULL))
-    {
-      struct IPv4HttpAddress *a4 = (struct IPv4HttpAddress *) addr;
-      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 ((ntohs (a4->u4_port) == 0) || (plugin->ipv4 == GNUNET_NO))
-        res = GNUNET_SYSERR;
-    }
-    if ((addrlen == sizeof (struct IPv6HttpAddress)) && (addr != NULL))
-    {
-      struct IPv6HttpAddress *a6 = (struct IPv6HttpAddress *) addr;
-      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));
-
-      if ((ntohs (a6->u6_port) == 0) || (plugin->ipv6 == GNUNET_NO))
-        res = GNUNET_SYSERR;
-    }
-    if (res == GNUNET_OK)
-    {
-      s = create_session (plugin, target, addr, addrlen, cont, cont_cls);
-      s->ats_address_network_type = ats.value;
-      GNUNET_CONTAINER_DLL_insert (plugin->head, plugin->tail, s);
-      // initiate new connection
-      res = client_connect (s);
-    }
-    if (res == GNUNET_SYSERR)
-    {
-      if (s != NULL)
-      {
-        GNUNET_CONTAINER_DLL_remove (plugin->head, plugin->tail, s);
-        delete_session (s);
-      }
-      return GNUNET_SYSERR;
-    }
-  }
-
-  /* real sending */
-
-  msg = GNUNET_malloc (sizeof (struct HTTP_Message) + msgbuf_size);
-  msg->next = NULL;
-  msg->size = msgbuf_size;
-  msg->pos = 0;
-  msg->buf = (char *) &msg[1];
-  msg->transmit_cont = cont;
-  msg->transmit_cont_cls = cont_cls;
-  memcpy (msg->buf, msgbuf, msgbuf_size);
-
-  if (s->inbound == GNUNET_NO)
-  {
-#if DEBUG_HTTP
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using outbound client session %p to send to `%s'\n", s,
-                     GNUNET_i2s (target));
-#endif
-
-    client_send (s, msg);
-    res = msgbuf_size;
-  }
-  if (s->inbound == GNUNET_YES)
-  {
-#if DEBUG_HTTP
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
-                     "Using inbound server %p session to send to `%s'\n", s,
-                     GNUNET_i2s (target));
-#endif
-
-    server_send (s, msg);
-    res = msgbuf_size;
-  }
-  return res;
-}
-
 
 /**
  * Function that can be used to force the plugin to disconnect
@@ -932,219 +672,95 @@ http_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
     next = s->next;
     if (0 == memcmp (target, &s->target, sizeof (struct GNUNET_PeerIdentity)))
     {
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                       "Disconnecting %s session %p to `%s'\n",
+                       (s->inbound == GNUNET_NO) ? "outbound" : "inbound",
+                       s, GNUNET_i2s (target));
+
       if (s->inbound == GNUNET_NO)
         GNUNET_assert (GNUNET_OK == client_disconnect (s));
       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;
-  struct IPv4HttpAddressWrapper *w_t4 = NULL;
-  struct IPv6HttpAddressWrapper *w_t6 = NULL;
-  int af;
+  struct HttpAddressWrapper *w = NULL;
+  char *saddr;
 
-  af = addr->sa_family;
-  switch (af)
+  GNUNET_asprintf(&saddr, "%s://%s", plugin->protocol, GNUNET_a2s (addr, addrlen));
+  w = plugin->addr_head;
+  while (NULL != w)
   {
-  case AF_INET:
-    w_t4 = plugin->ipv4_addr_head;
-    struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
+      if (0 == strcmp (saddr, w->addr->addr))
+        break;
+      w = w->next;
+  }
 
-    while (w_t4 != NULL)
-    {
-      int res = memcmp (&w_t4->addr.ipv4_addr,
-                        &a4->sin_addr,
-                        sizeof (struct in_addr));
+  GNUNET_free (saddr);
+  return w;
+}
 
-      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.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);
-    }
-#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
-                                                            IPv4HttpAddress)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
-                                 sizeof (struct IPv4HttpAddress));
 
-    break;
-  case AF_INET6:
-    w_t6 = plugin->ipv6_addr_head;
-    struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
+static void
+nat_add_address (void *cls, int add_remove, const struct sockaddr *addr,
+                 socklen_t addrlen)
+{
+  struct Plugin *plugin = cls;
+  struct HttpAddressWrapper *w = NULL;
+  char *saddr;
+  size_t haddrlen;
 
-    while (w_t6)
-    {
-      int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
-                        sizeof (struct in6_addr));
+  GNUNET_asprintf(&saddr, "%s://%s", plugin->protocol, GNUNET_a2s (addr, addrlen));
 
-      if (res == 0)
-      {
-        if (a6->sin6_port != w_t6->addr6.u6_port)
-          res = -1;
-      }
-      if (0 == res)
-        break;
-      w_t6 = w_t6->next;
-    }
-    if (w_t6 == NULL)
-    {
-      w_t6 = GNUNET_malloc (sizeof (struct IPv6HttpAddressWrapper));
-
-      memcpy (&w_t6->addr6.ipv6_addr, &a6->sin6_addr, sizeof (struct in6_addr));
-      w_t6->addr6.u6_port = a6->sin6_port;
+  haddrlen = sizeof (struct HttpAddress) + strlen(saddr) + 1;
+  w = GNUNET_malloc (sizeof (struct HttpAddressWrapper));
+  w->addr = GNUNET_malloc (haddrlen);
+  w->addr->addr = &w->addr[1];
+  w->addr->addr_len = htonl (strlen(saddr) + 1);
+  memcpy (w->addr->addr, saddr, strlen(saddr) + 1);
+  GNUNET_free (saddr);
 
-      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,
-                     "Notifying transport to add IPv6 address `%s'\n",
-                     http_plugin_address_to_string (NULL, &w_t6->addr6,
-                                                    sizeof (struct
-                                                            IPv6HttpAddress)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
-                                 sizeof (struct IPv6HttpAddress));
-    break;
-  default:
-    return;
-  }
+  GNUNET_CONTAINER_DLL_insert(plugin->addr_head, plugin->addr_tail, w);
+  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+                   "Notifying transport to add address `%s'\n", w->addr->addr);
 
+  plugin->env->notify_address (plugin->env->cls, add_remove, w->addr, haddrlen, "http");
 }
 
+
 static void
 nat_remove_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 = plugin->ipv4_addr_head;
-    struct sockaddr_in *a4 = (struct sockaddr_in *) addr;
-
-    while (w_t4 != NULL)
-    {
-      int res = memcmp (&w_t4->addr.ipv4_addr,
-                        &a4->sin_addr,
-                        sizeof (struct in_addr));
+  struct HttpAddressWrapper *w = NULL;
+  size_t haddrlen;
 
-      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)
-      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
-                                                            IPv4HttpAddress)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t4->addr,
-                                 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;
-    struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) addr;
-
-    while (w_t6)
-    {
-      int res = memcmp (&w_t6->addr6.ipv6_addr, &a6->sin6_addr,
-                        sizeof (struct in6_addr));
+  w = find_address (plugin, addr, addrlen);
+  if (NULL == w)
+    return;
 
-      if (res == 0)
-      {
-        if (a6->sin6_port != w_t6->addr6.u6_port)
-          res = -1;
-      }
-      if (0 == res)
-        break;
-      w_t6 = w_t6->next;
-    }
-    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->addr6,
-                                                    sizeof (struct
-                                                            IPv6HttpAddress)));
-#endif
-    plugin->env->notify_address (plugin->env->cls, add_remove, &w_t6->addr6,
-                                 sizeof (struct IPv6HttpAddress));
+  haddrlen = sizeof (struct HttpAddress) + ntohl (w->addr->addr_len);
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                   "Notifying transport to remove address `%s'\n", http_plugin_address_to_string(NULL, w->addr, haddrlen));
 
-    GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
-                                 w_t6);
-    GNUNET_free (w_t6);
-    break;
-  default:
-    return;
-  }
 
+  GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
+  plugin->env->notify_address (plugin->env->cls, add_remove, w->addr,
+       sizeof (struct HttpAddress) + ntohl (w->addr->addr_len), "http");
+  GNUNET_free (w->addr);
+  GNUNET_free (w);
 }
 
+
 /**
  * Our external IP address/port mapping has changed.
  *
@@ -1159,15 +775,13 @@ 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:
@@ -1179,6 +793,7 @@ nat_port_map_callback (void *cls, int add_remove, const struct sockaddr *addr,
   }
 }
 
+
 void
 http_check_ipv6 (struct Plugin *plugin)
 {
@@ -1212,6 +827,7 @@ http_check_ipv6 (struct Plugin *plugin)
   }
 }
 
+
 int
 http_get_addresses (struct Plugin *plugin, const char *serviceName,
                     const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -1249,6 +865,13 @@ http_get_addresses (struct Plugin *plugin, const char *serviceName,
       return GNUNET_SYSERR;
     }
   }
+  if (0 == port)
+  {
+    GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, plugin->name,
+                     "Starting in listen only mode\n");
+    return -1; /* listen only */
+  }
+
 
   if (GNUNET_CONFIGURATION_have_value (cfg, serviceName, "BINDTO"))
   {
@@ -1394,61 +1017,86 @@ start_report_addresses (struct Plugin *plugin)
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    _("Found %u addresses to report to NAT service\n"), res);
 
-  if (res != GNUNET_SYSERR)
+  if (GNUNET_SYSERR == res)
   {
-    plugin->nat =
-        GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
-                             (unsigned int) res,
-                             (const struct sockaddr **) addrs, addrlens,
-                             &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_a2s (addrs[res], addrlens[res]));
-#endif
-      GNUNET_assert (addrs[res] != NULL);
-      GNUNET_free (addrs[res]);
-    }
-    GNUNET_free_non_null (addrs);
-    GNUNET_free_non_null (addrlens);
+    plugin->nat = NULL;
+    return;
   }
-  else
+
+  plugin->nat =
+      GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, plugin->port,
+                           (unsigned int) res,
+                           (const struct sockaddr **) addrs, addrlens,
+                           &nat_port_map_callback, NULL, plugin);
+  while (res > 0)
   {
-    plugin->nat =
-        GNUNET_NAT_register (plugin->env->cfg, GNUNET_YES, 0, 0, NULL, NULL,
-                             NULL, NULL, plugin);
+    res--;
+    GNUNET_assert (addrs[res] != NULL);
+    GNUNET_free (addrs[res]);
   }
+  GNUNET_free_non_null (addrs);
+  GNUNET_free_non_null (addrlens);
 }
 
+
 static void
 stop_report_addresses (struct Plugin *plugin)
 {
+
   /* Stop NAT handle */
-  GNUNET_NAT_unregister (plugin->nat);
+  if (NULL != plugin->nat)
+    GNUNET_NAT_unregister (plugin->nat);
 
   /* Clean up addresses */
-  struct IPv4HttpAddressWrapper *w_t4;
-  struct IPv6HttpAddressWrapper *w_t6;
+  struct HttpAddressWrapper *w;
 
-  while (plugin->ipv4_addr_head != NULL)
+  while (plugin->addr_head != NULL)
   {
-    w_t4 = plugin->ipv4_addr_head;
-    GNUNET_CONTAINER_DLL_remove (plugin->ipv4_addr_head, plugin->ipv4_addr_tail,
-                                 w_t4);
-    GNUNET_free (w_t4);
+    w = plugin->addr_head;
+    GNUNET_CONTAINER_DLL_remove (plugin->addr_head, plugin->addr_tail, w);
+    GNUNET_free (w->addr);
+    GNUNET_free (w);
   }
+}
 
-  while (plugin->ipv6_addr_head != NULL)
-  {
-    w_t6 = plugin->ipv6_addr_head;
-    GNUNET_CONTAINER_DLL_remove (plugin->ipv6_addr_head, plugin->ipv6_addr_tail,
-                                 w_t6);
-    GNUNET_free (w_t6);
-  }
+/**
+ * Function called when the service shuts down.  Unloads our plugins
+ * and cancels pending validations.
+ *
+ * @param cls closure, unused
+ * @param tc task context (unused)
+ */
+static void
+notify_external_hostname (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct Plugin *plugin = cls;
+  struct HttpAddress *eaddr;
+  char *addr;
+  size_t eaddr_len;
+  size_t uri_len;
+
+  plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
+
+  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
+    return;
+
+  GNUNET_asprintf(&addr, "%s://%s", plugin->protocol, plugin->external_hostname);
+  uri_len = strlen (addr) + 1;
+  eaddr_len = sizeof (struct HttpAddress) + uri_len;
+  eaddr = GNUNET_malloc (eaddr_len);
+  eaddr->addr_len = htonl (uri_len);
+  eaddr->addr = (void *) &eaddr[1];
+  memcpy (&eaddr->addr, addr, uri_len);
+  GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, plugin->name,
+                   "Notifying transport about external hostname address `%s'\n", addr);
+
+  GNUNET_free (addr);
+  plugin->env->notify_address (plugin->env->cls, GNUNET_YES, eaddr, eaddr_len, "http");
+  plugin->ext_addr = eaddr;
+  plugin->ext_addr_len = eaddr_len;
 }
 
+
 static int
 configure_plugin (struct Plugin *plugin)
 {
@@ -1537,7 +1185,6 @@ configure_plugin (struct Plugin *plugin)
     GNUNET_free (bind4_address);
   }
 
-
   char *bind6_address = NULL;
 
   if ((plugin->ipv6 == GNUNET_YES) &&
@@ -1567,6 +1214,17 @@ configure_plugin (struct Plugin *plugin)
     GNUNET_free (bind6_address);
   }
 
+  if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg, plugin->name,
+                                              "EXTERNAL_HOSTNAME", &plugin->external_hostname))
+  {
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                       _("Using external hostname `%s'\n"), plugin->external_hostname);
+      plugin->notify_ext_task = GNUNET_SCHEDULER_add_now (&notify_external_hostname, plugin);
+  }
+  else
+    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
+                     _("No external hostname configured\n"));
+
 
   /* Optional parameters */
   unsigned long long maxneigh;
@@ -1581,6 +1239,99 @@ fail:
   return res;
 }
 
+#define TESTING GNUNET_NO
+
+#if TESTING
+#define TIMEOUT_LOG GNUNET_ERROR_TYPE_ERROR
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+#else
+#define TIMEOUT_LOG GNUNET_ERROR_TYPE_DEBUG
+#define TIMEOUT GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT
+#endif
+
+
+/**
+ * Session was idle, so disconnect it
+ */
+static void
+session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_assert (NULL != cls);
+  struct Session *s = cls;
+
+  s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  GNUNET_log (TIMEOUT_LOG,
+              "Session %p was idle for %s, disconnecting\n",
+              s,
+             GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
+                                                     GNUNET_YES));
+
+  /* call session destroy function */
+  if (s->inbound == GNUNET_NO)
+    GNUNET_assert (GNUNET_OK == client_disconnect (s));
+  else
+    GNUNET_assert (GNUNET_OK == server_disconnect (s));
+}
+
+
+/**
+* Start session timeout
+*/
+static void
+start_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
+ s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                                  &session_timeout,
+                                                  s);
+ GNUNET_log (TIMEOUT_LOG,
+             "Timeout for session %p set to %s\n",
+             s,
+            GNUNET_STRINGS_relative_time_to_string (TIMEOUT,
+                                                    GNUNET_YES));
+}
+
+
+/**
+* Increment session timeout due to activity
+*/
+static void
+reschedule_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+ GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
+
+ GNUNET_SCHEDULER_cancel (s->timeout_task);
+ s->timeout_task =  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                                  &session_timeout,
+                                                  s);
+ GNUNET_log (TIMEOUT_LOG,
+             "Timeout rescheduled for session %p set to %s\n",
+             s,
+            GNUNET_STRINGS_relative_time_to_String (TIMEOUT,
+                                                    GNUNET_YES));
+}
+
+
+/**
+* Cancel timeout
+*/
+static void
+stop_session_timeout (struct Session *s)
+{
+ GNUNET_assert (NULL != s);
+
+ if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
+ {
+   GNUNET_SCHEDULER_cancel (s->timeout_task);
+   s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
+   GNUNET_log (TIMEOUT_LOG,
+               "Timeout stopped for session %p\n",
+               s);
+ }
+}
+
 /**
  * Entry point for the plugin.
  */
@@ -1592,17 +1343,31 @@ 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;
+  plugin->outbound_sessions = 0;
+  plugin->inbound_sessions = 0;
   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
   api->cls = plugin;
-  api->send = &http_plugin_send_old;
   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_with_session =   &http_plugin_send;
+  api->send = &http_plugin_send;
 
 #if BUILD_HTTPS
   plugin->name = "transport-https";
@@ -1655,11 +1420,8 @@ LIBGNUNET_PLUGIN_TRANSPORT_INIT (void *cls)
   /* Report addresses to transport service */
   start_report_addresses (plugin);
 
-#if DEBUG_HTTP
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Plugin `%s' loaded\n", plugin->name);
-#endif
-
   return api;
 }
 
@@ -1672,7 +1434,26 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
 {
   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
-  struct Session *s = NULL;
+  struct Session *s;
+  struct Session *next;
+
+  if (NULL == plugin)
+  {
+    GNUNET_free (api);
+    return NULL;
+  }
+
+  if (GNUNET_SCHEDULER_NO_TASK != plugin->notify_ext_task)
+  {
+      GNUNET_SCHEDULER_cancel (plugin->notify_ext_task);
+       plugin->notify_ext_task = GNUNET_SCHEDULER_NO_TASK;
+  }
+
+  if (NULL != plugin->ext_addr)
+  {
+      plugin->env->notify_address (plugin->env->cls, GNUNET_NO, plugin->ext_addr, plugin->ext_addr_len, "http");
+      GNUNET_free (plugin->ext_addr);
+  }
 
   /* Stop reporting addresses to transport service */
   stop_report_addresses (plugin);
@@ -1681,68 +1462,31 @@ LIBGNUNET_PLUGIN_TRANSPORT_DONE (void *cls)
   s = plugin->head;
   while (s != NULL)
   {
-#if DEBUG_HTTP
+    next = s->next;
     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));
     else
       GNUNET_assert (GNUNET_OK == server_disconnect (s));
-    s = s->next;
+    s = next;
   }
 
-#if DEBUG_HTTP
   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");
-#endif
   /* Stop client */
   client_stop (plugin);
 
-  /* deleting up sessions */
-  s = plugin->head;
-  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;
-  }
-
-
-#if DEBUG_HTTP
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, plugin->name,
                    "Plugin `%s' unloaded\n", plugin->name);
-#endif
-
   GNUNET_free_non_null (plugin->server_addr_v4);
   GNUNET_free_non_null (plugin->server_addr_v6);
+  GNUNET_free_non_null (plugin->external_hostname);
   GNUNET_free (plugin);
   GNUNET_free (api);
-
   return NULL;
 }