still not really working... sending and receiving of messages a bit hinky
[oweals/gnunet.git] / src / transport / plugin_transport_udp.c
index 5dca2a748880a90d83e29f236023dce4adf6f90f..3df492a913213adc196b96a62acf12b3bf343224 100644 (file)
 #include "plugin_transport.h"
 #include "transport.h"
 
-#define DEBUG_UDP GNUNET_NO
+#define DEBUG_UDP GNUNET_YES
 
-/**
- * The default maximum size of each outbound UDP message,
- * optimal value for Ethernet (10 or 100 MBit).
+/*
+ * Transport cost to peer, always 1 for UDP (direct connection)
  */
-#define MESSAGE_SIZE 1472
+#define UDP_DIRECT_DISTANCE 1
 
 /**
  * Handle for request of hostname resolution, non-NULL if pending.
  */
 static struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
 
+/**
+ * How long until we give up on transmitting the welcome message?
+ */
+#define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+
+
 /**
  * Message-Packet header.
  */
@@ -73,48 +78,11 @@ struct UDPMessage
 /* Forward definition */
 struct Plugin;
 
-/**
- * Session handle for UDP connections.
- */
-struct Session
+struct PrettyPrinterContext
 {
-
-  /**
-   * Stored in a linked list.
-   */
-  struct Session *next;
-
-  /**
-   * Pointer to the global plugin struct.
-   */
-  struct Plugin *plugin;
-
-  /**
-   * To whom are we talking to (set to our identity
-   */
-  struct GNUNET_PeerIdentity target;
-
-  /**
-   * Address of the other peer if WE initiated the connection
-   * (and hence can be sure what it is), otherwise NULL.
-   */
-  void *connect_addr;
-
-  /**
-   * Length of connect_addr, can be 0.
-   */
-  size_t connect_alen;
-
-  /*
-   * Random challenge number for validation
-   */
-  int challenge;
-
-  /*
-   * Have we received validation (performed ping/pong) from this peer?
-   */
-  unsigned int validated;
-
+  GNUNET_TRANSPORT_AddressStringCallback asc;
+  void *asc_cls;
+  uint16_t port;
 };
 
 /**
@@ -127,11 +95,6 @@ struct Plugin
    */
   struct GNUNET_TRANSPORT_PluginEnvironment *env;
 
-  /**
-   * List of open TCP sessions.
-   */
-  struct Session *sessions;
-
   /**
    * Handle for the statistics service.
    */
@@ -142,6 +105,11 @@ struct Plugin
    */
   struct GNUNET_SERVICE_Context *service;
 
+  /**
+   * Handle for request of hostname resolution, non-NULL if pending.
+   */
+  struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
+
   /**
    * ID of task used to update our addresses when one expires.
    */
@@ -170,58 +138,6 @@ struct Plugin
 
 };
 
-/**
- * Message used to ask a peer to validate receipt (to check an address
- * from a HELLO).  Followed by the address used.  Note that the
- * recipients response does not affirm that he has this address,
- * only that he got the challenge message.
- */
-struct UDPPingMessage
-{
-
-  /**
-   * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Random challenge number (in network byte order).
-   */
-  uint32_t challenge GNUNET_PACKED;
-
-
-
-};
-
-
-/**
- * Message used to validate a HELLO.  The challenge is included in the
- * confirmation to make matching of replies to requests possible.  The
- * signature signs the original challenge number, our public key, the
- * sender's address (so that the sender can check that the address we
- * saw is plausible for him and possibly detect a MiM attack) and a
- * timestamp (to limit replay).<p>
- *
- * This message is followed by the address of the
- * client that we are observing (which is part of what
- * is being signed).
- */
-struct UDPPongMessage
-{
-  /**
-   * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
-   */
-  struct GNUNET_MessageHeader header;
-
-  /**
-   * Random challenge number (in network byte order).
-   */
-  uint32_t challenge GNUNET_PACKED;
-
-  /* Length of addr, appended to end of message */
-  unsigned int addrlen;
-};
-
 /* *********** globals ************* */
 
 /**
@@ -229,24 +145,6 @@ struct UDPPongMessage
  */
 static struct GNUNET_NETWORK_Handle *udp_sock;
 
-
-/**
- * A (core) Session is to be associated with a transport session. The
- * transport service may want to know in order to call back on the
- * core if the connection is being closed.
- *
- * @param session the session handle passed along
- *   from the call to receive that was made by the transport
- *   layer
- * @return GNUNET_OK if the session could be associated,
- *         GNUNET_SYSERR if not.
- */
-int
-udp_associate (struct Session *session)
-{
-  return GNUNET_SYSERR;         /* UDP connections can never be associated */
-}
-
 /**
  * Disconnect from a remote node.
  *
@@ -279,192 +177,170 @@ udp_transport_server_stop (void *cls)
   return GNUNET_OK;
 }
 
-static struct Session *
-find_session (void *cls, struct Session *session_list,
-              const struct GNUNET_PeerIdentity *peer)
-{
-  struct Plugin *plugin = cls;
-  struct Session *pos;
-  pos = session_list;
-
-  while (pos != NULL)
-    {
-      if (memcmp (peer, &pos->target, sizeof (struct GNUNET_PeerIdentity)) ==
-          0)
-        return pos;
-      pos = pos->next;
-    }
-
-  return NULL;
-}
-
 /**
  * Function that can be used by the transport service to transmit
  * a message using the plugin.
  *
  * @param cls closure
- * @param service_context value passed to the transport-service
- *        to identify the neighbour
- * @param target who should receive this message
- * @param priority how important is the message
- * @param msg the message to transmit
+ * @param target who should receive this message (ignored by UDP)
+ * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
+ * @param msgbufsize the size of the msgbuf to send
+ * @param priority how important is the message (ignored by UDP)
  * @param timeout when should we time out (give up) if we can not transmit?
+ * @param addr the addr to send the message to, needs to be a sockaddr for us
+ * @param addrlen the len of addr
+ * @param force_address not used, we had better have an address to send to
+ *        because we are stateless!!
  * @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...)
  * @param cont_cls closure for cont
+ *
+ * @return the number of bytes written
  */
-static void
+
+static ssize_t
 udp_plugin_send (void *cls,
                  const struct GNUNET_PeerIdentity *target,
+                 char *msgbuf,
+                 size_t msgbuf_size,
                  unsigned int priority,
-                 const struct GNUNET_MessageHeader *msg,
                  struct GNUNET_TIME_Relative timeout,
+                 const void *addr,
+                 size_t addrlen,
+                 int force_address,
                  GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
 {
   struct Plugin *plugin = cls;
-  struct Session *session;
   struct UDPMessage *message;
   int ssize;
-  size_t sent;
+  ssize_t sent;
 
-  session = find_session (plugin, plugin->sessions, target);
+  GNUNET_assert(udp_sock != NULL);
 
-  if ((session == NULL) || (udp_sock == NULL))
-    return;
+  if ((addr == NULL) || (addrlen == 0))
+    {
+#if DEBUG_UDP
+  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
+                   ("udp_plugin_send called without address, returning!\n"));
+#endif
+      cont (cont_cls, target, GNUNET_OK);
+      return 0; /* Can never send if we don't have an address!! */
+    }
 
   /* Build the message to be sent */
-  message = GNUNET_malloc (sizeof (struct UDPMessage) + ntohs (msg->size));
-  ssize = sizeof (struct UDPMessage) + ntohs (msg->size);
+  message = GNUNET_malloc (sizeof (struct UDPMessage) + msgbuf_size);
+  ssize = sizeof (struct UDPMessage) + msgbuf_size;
 
 #if DEBUG_UDP
   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                   ("In udp_send, ssize is %d\n"), ssize);
+                   ("In udp_send, ssize is %d, sending message to %s\n"), ssize, GNUNET_a2s((const struct sockaddr *)addr, addrlen));
 #endif
   message->header.size = htons (ssize);
   message->header.type = htons (0);
   memcpy (&message->sender, plugin->env->my_identity,
           sizeof (struct GNUNET_PeerIdentity));
-  memcpy (&message[1], msg, ntohs (msg->size));
+  memcpy (&message[1], msgbuf, msgbuf_size);
 
   /* Actually send the message */
   sent =
     GNUNET_NETWORK_socket_sendto (udp_sock, message, ssize,
-                                  session->connect_addr,
-                                  session->connect_alen);
+                                  addr,
+                                  addrlen);
 
   if (cont != NULL)
     {
       if (sent == GNUNET_SYSERR)
         cont (cont_cls, target, GNUNET_SYSERR);
       else
-        cont (cont_cls, target, GNUNET_OK);
+        {
+#if DEBUG_UDP
+  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
+                   ("Sucessfully sent message, calling transmit continuation!\n"));
+#endif
+          cont (cont_cls, target, GNUNET_OK);
+        }
     }
-  GNUNET_free(message);
-  return;
+  GNUNET_free (message);
+  return sent;
 }
 
+
 /**
- * We've received a PING from this peer via UDP.
- * Send back our PONG.
- *
- * @param cls closure
- * @param sender the Identity of the sender
- * @param message the actual message
+ * Add the IP of our network interface to the list of
+ * our external IP addresses.
  */
-static void
-handle_udp_ping (void *cls,
-                 struct GNUNET_PeerIdentity *sender,
-                 struct sockaddr_storage *addr, size_t addrlen,
-                 const struct GNUNET_MessageHeader *message)
+static int
+process_interfaces (void *cls,
+                    const char *name,
+                    int isDefault,
+                    const struct sockaddr *addr, socklen_t addrlen)
 {
   struct Plugin *plugin = cls;
-  struct Session *head = plugin->sessions;
-  const struct UDPPingMessage *ping = (const struct UDPPingMessage *) message;
-  struct UDPPongMessage *pong;
-  struct Session *found;
+  int af;
+  struct sockaddr_in *v4;
+  struct sockaddr_in6 *v6;
 
-#if DEBUG_UDP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                   ("handling ping, challenge is %d\n"),
-                   ntohs (ping->challenge));
-#endif
-  found = find_session (plugin, head, sender);
-  if (found != NULL)
+  af = addr->sa_family;
+  if (af == AF_INET)
     {
-      pong = GNUNET_malloc (sizeof (struct UDPPongMessage) + addrlen);
-      pong->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG);
-      pong->header.size = htons (sizeof (struct UDPPongMessage) + addrlen);
-      pong->challenge = ping->challenge;
-      memcpy (&pong[1], addr, addrlen);
-      pong->addrlen = htons (addrlen);
-
-      udp_plugin_send (plugin, sender, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                       &pong->header,
-                       GNUNET_TIME_relative_multiply
-                       (GNUNET_TIME_UNIT_SECONDS, 30), NULL, NULL);
-      GNUNET_free(pong);
+      v4 = (struct sockaddr_in *) addr;
+      v4->sin_port = htons (plugin->adv_port);
     }
+  else
+    {
+      GNUNET_assert (af == AF_INET6);
+      v6 = (struct sockaddr_in6 *) addr;
+      v6->sin6_port = htons (plugin->adv_port);
+    }
+  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
+                   GNUNET_ERROR_TYPE_BULK,
+                   "udp", _("Found address `%s' (%s)\n"),
+                   GNUNET_a2s (addr, addrlen), name);
+  plugin->env->notify_address (plugin->env->cls,
+                               "udp",
+                               addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
 
-  return;
-
+  return GNUNET_OK;
 }
 
+
 /**
- * We've received a PONG from this peer via UDP.
- * Great. Call validate func if we haven't already
- * received a PONG.
+ * Function called by the resolver for each address obtained from DNS
+ * for our own hostname.  Add the addresses to the list of our
+ * external IP addresses.
  *
  * @param cls closure
- * @param client identification of the client
- * @param message the actual message
+ * @param addr one of the addresses of the host, NULL for the last address
+ * @param addrlen length of the address
  */
 static void
-handle_udp_pong (void *cls,
-                 struct GNUNET_PeerIdentity *sender,
-                 const struct GNUNET_MessageHeader *message)
+process_hostname_ips (void *cls,
+                      const struct sockaddr *addr, socklen_t addrlen)
 {
   struct Plugin *plugin = cls;
-  const struct UDPPongMessage *pong = (struct UDPPongMessage *) message;
-  struct Session *found;
-  unsigned int addr_len;
-  struct sockaddr_storage addr;
 
-#if DEBUG_UDP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _("handling pong\n"));
-#endif
-  found = find_session (plugin, plugin->sessions, sender);
-#if DEBUG_UDP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                   ("found->challenge %d, pong->challenge %d\n"),
-                   found->challenge, ntohs (pong->challenge));
-#endif
-  if ((found != NULL) && (found->challenge == ntohs (pong->challenge)))
+  if (addr == NULL)
     {
-      found->validated = GNUNET_YES;
-      addr_len = ntohs (pong->addrlen);
-#if DEBUG_UDP
-      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("found associated ping, addr is %u bytes\n"),
-                       addr_len);
-#endif
-      memcpy (&addr, &pong[1], addr_len);
-      plugin->env->notify_validation (plugin->env->cls, "udp", sender,
-                                      ntohs (pong->challenge),
-                                      (char *) &addr);
+      plugin->hostname_dns = NULL;
+      return;
     }
-  else
-    {
-
-#if DEBUG_UDP
-      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("Session not found!\n"));
-#endif
-    }
-  return;
+  process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
 }
 
+
+/*
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ *
+ * We have been notified that our writeset has something to read.  Presumably
+ * select has been called already, so we can go ahead and start reading from
+ * the socket immediately.  Then we check if there is more to be read by
+ * calling select ourselves while there is stuff on the wire.  Then reschedule
+ * this function to be called again once more is available.
+ *
+ */
 static void
 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
@@ -479,6 +355,11 @@ udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   socklen_t fromlen;
   struct sockaddr_storage addr;
   ssize_t ret;
+  int offset;
+  int count;
+  int tsize;
+  char *msgbuf;
+  const struct GNUNET_MessageHeader *currhdr;
 
   do
     {
@@ -495,11 +376,6 @@ udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       buf = GNUNET_malloc (buflen);
       fromlen = sizeof (addr);
 
-#if DEBUG_UDP
-      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("src_addr_len is %u\n"), fromlen);
-#endif
-
       memset (&addr, 0, fromlen);
       ret =
         GNUNET_NETWORK_socket_recvfrom (udp_sock, buf, buflen,
@@ -516,7 +392,6 @@ udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           GNUNET_free (buf);
           return;
         }
-
       msg = (struct UDPMessage *) buf;
 
 #if DEBUG_UDP
@@ -528,37 +403,41 @@ udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                        ("header reports message type of %d\n"),
                        ntohs (msg->header.type));
 #endif
-      /*if (ntohs(hdr->size) < sizeof(struct UDPMessage))
-         {
-         GNUNET_free(buf);
-         GNUNET_NETWORK_fdset_zero(plugin->rs);
-         GNUNET_NETWORK_fdset_set(plugin->rs, udp_sock);
-         break;
-         } */
+      if (ntohs (msg->header.size) < sizeof (struct UDPMessage))
+        {
+          GNUNET_free (buf);
+          GNUNET_NETWORK_fdset_zero (plugin->rs);
+          GNUNET_NETWORK_fdset_set (plugin->rs, udp_sock);
+          break;
+        }
       hdr = (const struct GNUNET_MessageHeader *) &msg[1];
+      msgbuf = (char *)&msg[1];
       sender = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
       memcpy (sender, &msg->sender, sizeof (struct GNUNET_PeerIdentity));
 
+      offset = 0;
+      count = 0;
+      tsize = ntohs (msg->header.size) - sizeof(struct UDPMessage);
+      while (offset < tsize)
+        {
+          currhdr = (struct GNUNET_MessageHeader *)&msgbuf[offset];
 #if DEBUG_UDP
       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("msg reports message size of %d\n"),
-                       ntohs (hdr->size));
-
+                       ("processing msg %d: type %d, size %d at offset %d\n"),
+                       count, ntohs(currhdr->type), ntohs(currhdr->size), offset);
+#endif
+          plugin->env->receive (plugin->env->cls,
+              sender, currhdr, UDP_DIRECT_DISTANCE, (char *)&addr, fromlen);
+          offset += ntohs(currhdr->size);
+#if DEBUG_UDP
       GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("msg reports message type of %d\n"),
-                       ntohs (hdr->type));
+                       ("offset now %d, tsize %d\n"),
+                       offset, tsize);
 #endif
-
-      if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING)
-        {
-          handle_udp_ping (plugin, sender, &addr, fromlen, hdr);
+          count++;
         }
 
-      if (ntohs (hdr->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PONG)
-        {
-          handle_udp_pong (plugin, sender, hdr);
-        }
-      GNUNET_free(sender);
+      GNUNET_free (sender);
       GNUNET_free (buf);
 
     }
@@ -653,70 +532,98 @@ udp_transport_server_start (void *cls)
   return desc;
 }
 
+
 /**
- * Function that can be used by the transport service to validate that
- * another peer is reachable at a particular address (even if we
- * already have a connection to this peer, this function is required
- * to establish a new one).
+ * Check if the given port is plausible (must be either
+ * our listen port or our advertised port).  If it is
+ * neither, we return one of these two ports at random.
  *
- * @param cls closure
- * @param target who should receive this message
- * @param challenge challenge code to use
- * @param addrlen length of the address
- * @param addr the address
- * @param timeout how long should we try to transmit these?
- * @return GNUNET_OK if the transmission has been scheduled
+ * @return either in_port or a more plausible port
+ */
+static uint16_t
+check_port (struct Plugin *plugin, uint16_t in_port)
+{
+  if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
+    return in_port;
+  return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                    2) == 0)
+    ? plugin->open_port : plugin->adv_port;
+}
+
+
+/**
+ * Another peer has suggested an address for this peer and transport
+ * plugin.  Check that this could be a valid address.  This function
+ * is not expected to 'validate' the address in the sense of trying to
+ * connect to it but simply to see if the binary format is technically
+ * legal for establishing a connection.
+ *
+ * @param addr pointer to the address, may be modified (slightly)
+ * @param addrlen length of addr
+ * @return GNUNET_OK if this is a plausible address for this peer
+ *         and transport, GNUNET_SYSERR if not
+ *
+ * TODO: perhaps make everything work with sockaddr_storage, it may
+ *       be a cleaner way to handle addresses in UDP
  */
 static int
-udp_plugin_validate (void *cls,
-                     const struct GNUNET_PeerIdentity *target,
-                     uint32_t challenge,
-                     struct GNUNET_TIME_Relative timeout,
-                     const void *addr, size_t addrlen)
+udp_check_address (void *cls, void *addr, size_t addrlen)
 {
   struct Plugin *plugin = cls;
-  struct Session *new_session;
-  struct UDPPongMessage *msg;
+  char buf[sizeof (struct sockaddr_in6)];
 
-  if (addrlen <= 0)
-    return GNUNET_SYSERR;
+  struct sockaddr_in *v4;
+  struct sockaddr_in6 *v6;
 
-  new_session = GNUNET_malloc (sizeof (struct Session));
-  new_session->connect_addr = GNUNET_malloc (addrlen);
-  memcpy (new_session->connect_addr, addr, addrlen);
-  new_session->connect_alen = addrlen;
-#if DEBUG_UDP
-  if (memcmp
-      (target, plugin->env->my_identity,
-       sizeof (struct GNUNET_PeerIdentity)) == 0)
+  if ((addrlen != sizeof (struct sockaddr_in)) &&
+      (addrlen != sizeof (struct sockaddr_in6)))
     {
-      GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                       ("definitely adding self to session list... hmmm\n"));
+      GNUNET_break_op (0);
+      return GNUNET_SYSERR;
+    }
+  memcpy (buf, addr, sizeof (struct sockaddr_in6));
+  if (addrlen == sizeof (struct sockaddr_in))
+    {
+      v4 = (struct sockaddr_in *) buf;
+      v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
+    }
+  else
+    {
+      v6 = (struct sockaddr_in6 *) buf;
+      v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
     }
-#endif
-  memcpy (&new_session->target, target, sizeof (struct GNUNET_PeerIdentity));
-  new_session->challenge = challenge;
-  new_session->validated = GNUNET_NO;
-  new_session->next = plugin->sessions;
-  plugin->sessions = new_session;
-
-  msg = GNUNET_malloc (sizeof (struct UDPPongMessage));
-  msg->header.size = htons (sizeof (struct UDPPongMessage));
-  msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_PING);
-  msg->challenge = htons (challenge);
 #if DEBUG_UDP
-  GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "udp", _
-                   ("In validate, header size is %d, type %d, challenge %u\n"),
-                   ntohs (msg->header.size), ntohs (msg->header.type),
-                   ntohl (msg->challenge));
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                   "tcp",
+                   "Informing transport service about my address `%s'.\n",
+                   GNUNET_a2s (addr, addrlen));
 #endif
-  udp_plugin_send (plugin, target, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                   &msg->header, timeout, NULL, NULL);
-
-  GNUNET_free(msg);
+  return GNUNET_OK;
   return GNUNET_OK;
 }
 
+
+/**
+ * Append our port and forward the result.
+ */
+static void
+append_port (void *cls, const char *hostname)
+{
+  struct PrettyPrinterContext *ppc = cls;
+  char *ret;
+
+  if (hostname == NULL)
+    {
+      ppc->asc (ppc->asc_cls, NULL);
+      GNUNET_free (ppc);
+      return;
+    }
+  GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
+  ppc->asc (ppc->asc_cls, ret);
+  GNUNET_free (ret);
+}
+
+
 /**
  * Convert the transports address to a nice, human-readable
  * format.
@@ -741,13 +648,45 @@ udp_plugin_address_pretty_printer (void *cls,
                                    GNUNET_TRANSPORT_AddressStringCallback asc,
                                    void *asc_cls)
 {
+  struct Plugin *plugin = cls;
+  const struct sockaddr_in *v4;
+  const struct sockaddr_in6 *v6;
+  struct PrettyPrinterContext *ppc;
 
+  if ((addrlen != sizeof (struct sockaddr_in)) &&
+      (addrlen != sizeof (struct sockaddr_in6)))
+    {
+      /* 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;
+  if (addrlen == sizeof (struct sockaddr_in))
+    {
+      v4 = (const struct sockaddr_in *) addr;
+      ppc->port = ntohs (v4->sin_port);
+    }
+  else
+    {
+      v6 = (const struct sockaddr_in6 *) addr;
+      ppc->port = ntohs (v6->sin6_port);
+
+    }
+  GNUNET_RESOLVER_hostname_get (plugin->env->sched,
+                                plugin->env->cfg,
+                                addr,
+                                addrlen,
+                                !numeric, timeout, &append_port, ppc);
 }
 
 /**
  * Set a quota for receiving data from the given peer; this is a
- * per-transport limit.  The transport should limit its read/select
- * calls to stay below the quota (in terms of incoming data).
+ * per-transport limit.  This call has no meaning for UDP, as if
+ * we don't receive data it still comes in.  UDP has no friendliness
+ * guarantees, and our buffers will fill at some level.
  *
  * @param cls closure
  * @param target the peer for whom the quota is given
@@ -758,29 +697,9 @@ udp_plugin_set_receive_quota (void *cls,
                               const struct GNUNET_PeerIdentity *target,
                               uint32_t quota_in)
 {
-
-}
-
-/**
- * Another peer has suggested an address for this
- * peer and transport plugin.  Check that this could be a valid
- * address.  If so, consider adding it to the list
- * of addresses.
- *
- * @param cls closure
- * @param addr pointer to the address
- * @param addrlen length of addr
- * @return GNUNET_OK if this is a plausible address for this peer
- *         and transport
- */
-static int
-udp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
-{
-
-  return GNUNET_SYSERR;
+  return; /* Do nothing */
 }
 
-
 /**
  * The exported method. Makes the core api available via a global and
  * returns the udp transport API.
@@ -842,18 +761,26 @@ libgnunet_plugin_transport_udp_init (void *cls)
   plugin->env = env;
   plugin->statistics = NULL;
   api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
-  plugin->sessions = NULL;
   api->cls = plugin;
 
-  api->validate = &udp_plugin_validate;
   api->send = &udp_plugin_send;
   api->disconnect = &udp_disconnect;
   api->address_pretty_printer = &udp_plugin_address_pretty_printer;
   api->set_receive_quota = &udp_plugin_set_receive_quota;
-  api->address_suggested = &udp_plugin_address_suggested;
-  api->cost_estimate = 17;      /* TODO: ATS */
+  api->check_address = &udp_check_address;
+
   plugin->service = service;
 
+  /* FIXME: do the two calls below periodically again and
+     not just once (since the info we get might change...) */
+  GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
+  plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
+                                                           env->cfg,
+                                                           AF_UNSPEC,
+                                                           HOSTNAME_RESOLVE_TIMEOUT,
+                                                           &process_hostname_ips,
+                                                           plugin);
+
   udp_sock = udp_transport_server_start (plugin);
 
   GNUNET_assert (udp_sock != NULL);
@@ -866,8 +793,6 @@ libgnunet_plugin_transport_udp_done (void *cls)
 {
   struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
-  struct Session *pos;
-  struct Session *oldpos;
 
   udp_transport_server_stop (plugin);
   if (NULL != hostname_dns)
@@ -877,16 +802,7 @@ libgnunet_plugin_transport_udp_done (void *cls)
     }
   GNUNET_SERVICE_stop (plugin->service);
 
-  pos = plugin->sessions;
-  while (pos != NULL)
-    {
-      GNUNET_free(pos->connect_addr);
-      oldpos = pos;
-      pos = pos->next;
-      GNUNET_free(oldpos);
-    }
-
-  GNUNET_NETWORK_fdset_destroy(plugin->rs);
+  GNUNET_NETWORK_fdset_destroy (plugin->rs);
   GNUNET_free (plugin);
   GNUNET_free (api);
   return NULL;