aboutsummaryrefslogtreecommitdiff
path: root/src/transport/plugin_transport_udp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/plugin_transport_udp.c')
-rw-r--r--src/transport/plugin_transport_udp.c2297
1 files changed, 2297 insertions, 0 deletions
diff --git a/src/transport/plugin_transport_udp.c b/src/transport/plugin_transport_udp.c
new file mode 100644
index 0000000..7141563
--- /dev/null
+++ b/src/transport/plugin_transport_udp.c
@@ -0,0 +1,2297 @@
+/*
+ This file is part of GNUnet
+ (C) 2010, 2011 Christian Grothoff (and other contributing authors)
+
+ GNUnet is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ GNUnet is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GNUnet; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file transport/plugin_transport_udp.c
+ * @brief Implementation of the UDP transport protocol
+ * @author Christian Grothoff
+ * @author Nathan Evans
+ * @author Matthias Wachs
+ */
+#include "platform.h"
+#include "plugin_transport_udp.h"
+#include "gnunet_hello_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_fragmentation_lib.h"
+#include "gnunet_nat_lib.h"
+#include "gnunet_protocols.h"
+#include "gnunet_resolver_service.h"
+#include "gnunet_signatures.h"
+#include "gnunet_constants.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_transport_service.h"
+#include "gnunet_transport_plugin.h"
+#include "transport.h"
+
+#define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
+
+
+/**
+ * Number of messages we can defragment in parallel. We only really
+ * defragment 1 message at a time, but if messages get re-ordered, we
+ * may want to keep knowledge about the previous message to avoid
+ * discarding the current message in favor of a single fragment of a
+ * previous message. 3 should be good since we don't expect massive
+ * message reorderings with UDP.
+ */
+#define UDP_MAX_MESSAGES_IN_DEFRAG 3
+
+/**
+ * We keep a defragmentation queue per sender address. How many
+ * sender addresses do we support at the same time? Memory consumption
+ * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
+ * value. (So 128 corresponds to 12 MB and should suffice for
+ * connecting to roughly 128 peers via UDP).
+ */
+#define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
+
+
+
+/**
+ * Closure for 'append_port'.
+ */
+struct PrettyPrinterContext
+{
+ /**
+ * Function to call with the result.
+ */
+ GNUNET_TRANSPORT_AddressStringCallback asc;
+
+ /**
+ * Clsoure for 'asc'.
+ */
+ void *asc_cls;
+
+ /**
+ * Port to add after the IP address.
+ */
+ uint16_t port;
+};
+
+struct Session
+{
+ /**
+ * Which peer is this session for?
+ */
+ struct GNUNET_PeerIdentity target;
+
+ /**
+ * Address of the other peer
+ */
+ const struct sockaddr *sock_addr;
+
+ size_t addrlen;
+
+ /**
+ * Desired delay for next sending we send to other peer
+ */
+ struct GNUNET_TIME_Relative flow_delay_for_other_peer;
+
+ /**
+ * Desired delay for next sending we received from other peer
+ */
+ struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
+
+ /**
+ * expected delay for ACKs
+ */
+ struct GNUNET_TIME_Relative last_expected_delay;
+
+
+ struct GNUNET_ATS_Information ats;
+
+ struct FragmentationContext * frag_ctx;
+};
+
+
+struct SessionCompareContext
+{
+ struct Session *res;
+ const struct GNUNET_HELLO_Address *addr;
+};
+
+
+/**
+ * Closure for 'process_inbound_tokenized_messages'
+ */
+struct SourceInformation
+{
+ /**
+ * Sender identity.
+ */
+ struct GNUNET_PeerIdentity sender;
+
+ /**
+ * Source address.
+ */
+ const void *arg;
+
+ /**
+ * Number of bytes in source address.
+ */
+ size_t args;
+
+ struct Session *session;
+};
+
+
+/**
+ * Closure for 'find_receive_context'.
+ */
+struct FindReceiveContext
+{
+ /**
+ * Where to store the result.
+ */
+ struct DefragContext *rc;
+
+ /**
+ * Address to find.
+ */
+ const struct sockaddr *addr;
+
+ /**
+ * Number of bytes in 'addr'.
+ */
+ socklen_t addr_len;
+
+ struct Session *session;
+};
+
+
+
+/**
+ * Data structure to track defragmentation contexts based
+ * on the source of the UDP traffic.
+ */
+struct DefragContext
+{
+
+ /**
+ * Defragmentation context.
+ */
+ struct GNUNET_DEFRAGMENT_Context *defrag;
+
+ /**
+ * Source address this receive context is for (allocated at the
+ * end of the struct).
+ */
+ const struct sockaddr *src_addr;
+
+ /**
+ * Reference to master plugin struct.
+ */
+ struct Plugin *plugin;
+
+ /**
+ * Node in the defrag heap.
+ */
+ struct GNUNET_CONTAINER_HeapNode *hnode;
+
+ /**
+ * Length of 'src_addr'
+ */
+ size_t addr_len;
+};
+
+
+
+/**
+ * Closure for 'process_inbound_tokenized_messages'
+ */
+struct FragmentationContext
+{
+ struct FragmentationContext * next;
+ struct FragmentationContext * prev;
+
+ struct Plugin * plugin;
+ struct GNUNET_FRAGMENT_Context * frag;
+ struct Session * session;
+
+ struct GNUNET_TIME_Absolute timeout;
+
+
+ /**
+ * Function to call upon completion of the transmission.
+ */
+ GNUNET_TRANSPORT_TransmitContinuation cont;
+
+ /**
+ * Closure for 'cont'.
+ */
+ void *cont_cls;
+
+ size_t bytes_to_send;
+};
+
+
+struct UDPMessageWrapper
+{
+ struct Session *session;
+ struct UDPMessageWrapper *prev;
+ struct UDPMessageWrapper *next;
+ char *udp;
+ size_t msg_size;
+
+ struct GNUNET_TIME_Absolute timeout;
+
+ /**
+ * Function to call upon completion of the transmission.
+ */
+ GNUNET_TRANSPORT_TransmitContinuation cont;
+
+ /**
+ * Closure for 'cont'.
+ */
+ void *cont_cls;
+
+ struct FragmentationContext *frag_ctx;
+
+};
+
+
+/**
+ * UDP ACK Message-Packet header (after defragmentation).
+ */
+struct UDP_ACK_Message
+{
+ /**
+ * Message header.
+ */
+ struct GNUNET_MessageHeader header;
+
+ /**
+ * Desired delay for flow control
+ */
+ uint32_t delay;
+
+ /**
+ * What is the identity of the sender
+ */
+ struct GNUNET_PeerIdentity sender;
+
+};
+
+/**
+ * We have been notified that our readset has something to read. We don't
+ * know which socket needs to be read, so we have to check each one
+ * Then reschedule this function to be called again once more is available.
+ *
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ */
+static void
+udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+/**
+ * We have been notified that our readset has something to read. We don't
+ * know which socket needs to be read, so we have to check each one
+ * Then reschedule this function to be called again once more is available.
+ *
+ * @param cls the plugin handle
+ * @param tc the scheduling context (for rescheduling this function again)
+ */
+static void
+udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+/**
+ * Function called for a quick conversion of the binary address to
+ * a numeric address. Note that the caller must not free the
+ * address and that the next call to this function is allowed
+ * to override the address again.
+ *
+ * @param cls closure
+ * @param addr binary address
+ * @param addrlen length of the address
+ * @return string representing the same address
+ */
+const char *
+udp_address_to_string (void *cls, const void *addr, size_t addrlen)
+{
+ static char rbuf[INET6_ADDRSTRLEN + 10];
+ char buf[INET6_ADDRSTRLEN];
+ const void *sb;
+ struct in_addr a4;
+ struct in6_addr a6;
+ const struct IPv4UdpAddress *t4;
+ const struct IPv6UdpAddress *t6;
+ int af;
+ uint16_t port;
+
+ if (addrlen == sizeof (struct IPv6UdpAddress))
+ {
+ t6 = addr;
+ af = AF_INET6;
+ port = ntohs (t6->u6_port);
+ memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
+ sb = &a6;
+ }
+ else if (addrlen == sizeof (struct IPv4UdpAddress))
+ {
+ t4 = addr;
+ af = AF_INET;
+ port = ntohs (t4->u4_port);
+ memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
+ sb = &a4;
+ }
+ else
+ {
+ GNUNET_break_op (0);
+ return NULL;
+ }
+ inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
+ GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
+ buf, port);
+ return rbuf;
+}
+
+
+/**
+ * Append our port and forward the result.
+ *
+ * @param cls a 'struct PrettyPrinterContext'
+ * @param hostname result from DNS resolver
+ */
+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.
+ *
+ * @param cls closure
+ * @param type name of the transport that generated the address
+ * @param addr one of the addresses of the host, NULL for the last address
+ * the specific address format depends on the transport
+ * @param addrlen length of the address
+ * @param numeric should (IP) addresses be displayed in numeric form?
+ * @param timeout after how long should we give up?
+ * @param asc function to call on each string
+ * @param asc_cls closure for asc
+ */
+static void
+udp_plugin_address_pretty_printer (void *cls, const char *type,
+ const void *addr, size_t addrlen,
+ int numeric,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_TRANSPORT_AddressStringCallback asc,
+ void *asc_cls)
+{
+ struct PrettyPrinterContext *ppc;
+ const void *sb;
+ size_t sbs;
+ struct sockaddr_in a4;
+ struct sockaddr_in6 a6;
+ const struct IPv4UdpAddress *u4;
+ const struct IPv6UdpAddress *u6;
+ uint16_t port;
+
+ if (addrlen == sizeof (struct IPv6UdpAddress))
+ {
+ u6 = addr;
+ memset (&a6, 0, sizeof (a6));
+ a6.sin6_family = AF_INET6;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ a6.sin6_len = sizeof (a6);
+#endif
+ a6.sin6_port = u6->u6_port;
+ memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
+ port = ntohs (u6->u6_port);
+ sb = &a6;
+ sbs = sizeof (a6);
+ }
+ else if (addrlen == sizeof (struct IPv4UdpAddress))
+ {
+ u4 = addr;
+ memset (&a4, 0, sizeof (a4));
+ a4.sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ a4.sin_len = sizeof (a4);
+#endif
+ a4.sin_port = u4->u4_port;
+ a4.sin_addr.s_addr = u4->ipv4_addr;
+ port = ntohs (u4->u4_port);
+ sb = &a4;
+ sbs = sizeof (a4);
+ }
+ else
+ {
+ /* 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;
+ GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
+}
+
+
+/**
+ * Check if the given port is plausible (must be either our listen
+ * port or our advertised port). If it is neither, we return
+ * GNUNET_SYSERR.
+ *
+ * @param plugin global variables
+ * @param in_port port number to check
+ * @return GNUNET_OK if port is either open_port or adv_port
+ */
+static int
+check_port (struct Plugin *plugin, uint16_t in_port)
+{
+ if ((in_port == plugin->port) || (in_port == plugin->aport))
+ return GNUNET_OK;
+ return GNUNET_SYSERR;
+}
+
+
+
+/**
+ * Function that will be called to check if a binary address for this
+ * plugin is well-formed and corresponds to an address for THIS peer
+ * (as per our configuration). Naturally, if absolutely necessary,
+ * plugins can be a bit conservative in their answer, but in general
+ * plugins should make sure that the address does not redirect
+ * traffic to a 3rd party that might try to man-in-the-middle our
+ * traffic.
+ *
+ * @param cls closure, should be our handle to the Plugin
+ * @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, GNUNET_SYSERR if not
+ *
+ */
+static int
+udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
+{
+ struct Plugin *plugin = cls;
+ struct IPv4UdpAddress *v4;
+ struct IPv6UdpAddress *v6;
+
+ if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
+ (addrlen != sizeof (struct IPv6UdpAddress)))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (addrlen == sizeof (struct IPv4UdpAddress))
+ {
+ v4 = (struct IPv4UdpAddress *) addr;
+ if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
+ return GNUNET_SYSERR;
+ if (GNUNET_OK !=
+ GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
+ sizeof (struct in_addr)))
+ return GNUNET_SYSERR;
+ }
+ else
+ {
+ v6 = (struct IPv6UdpAddress *) addr;
+ if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
+ {
+ GNUNET_break_op (0);
+ return GNUNET_SYSERR;
+ }
+ if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
+ return GNUNET_SYSERR;
+ if (GNUNET_OK !=
+ GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
+ sizeof (struct in6_addr)))
+ return GNUNET_SYSERR;
+ }
+ return GNUNET_OK;
+}
+
+
+/**
+ * Destroy a session, plugin is being unloaded.
+ *
+ * @param cls unused
+ * @param key hash of public key of target peer
+ * @param value a 'struct PeerSession*' to clean up
+ * @return GNUNET_OK (continue to iterate)
+ */
+static int
+disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct Plugin *plugin = cls;
+ struct Session *s = value;
+ struct UDPMessageWrapper *udpw;
+ struct UDPMessageWrapper *next;
+
+#if DEBUG_UDP
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Session %p to peer `%s' address ended \n",
+ s,
+ GNUNET_i2s (&s->target),
+ GNUNET_a2s (s->sock_addr, s->addrlen));
+#endif
+
+ if (s->frag_ctx != NULL)
+ {
+ GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
+ GNUNET_free (s->frag_ctx);
+ s->frag_ctx = NULL;
+ }
+
+ udpw = plugin->ipv4_queue_head;
+ while (udpw != NULL)
+ {
+ next = udpw->next;
+ if (udpw->session == s)
+ {
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
+
+ if (udpw->cont != NULL)
+ udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
+ GNUNET_free (udpw);
+ }
+ udpw = next;
+ }
+
+ udpw = plugin->ipv6_queue_head;
+ while (udpw != NULL)
+ {
+ next = udpw->next;
+ if (udpw->session == s)
+ {
+ GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
+
+ if (udpw->cont != NULL)
+ udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
+ GNUNET_free (udpw);
+ }
+ udpw = next;
+ }
+
+ plugin->env->session_end (plugin->env->cls, &s->target, s);
+
+ GNUNET_assert (GNUNET_YES ==
+ GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
+ &s->target.hashPubKey,
+ s));
+
+
+ GNUNET_free (s);
+ return GNUNET_OK;
+}
+
+
+/**
+ * Disconnect from a remote node. Clean up session if we have one for this peer
+ *
+ * @param cls closure for this call (should be handle to Plugin)
+ * @param target the peeridentity of the peer to disconnect
+ * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
+ */
+static void
+udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
+{
+ struct Plugin *plugin = cls;
+ GNUNET_assert (plugin != NULL);
+
+ GNUNET_assert (target != NULL);
+#if DEBUG_UDP
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
+#endif
+ /* Clean up sessions */
+ GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
+}
+
+static 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)
+{
+ struct Session *s;
+ const struct IPv4UdpAddress *t4;
+ const struct IPv6UdpAddress *t6;
+ struct sockaddr_in *v4;
+ struct sockaddr_in6 *v6;
+ size_t len;
+
+ switch (addrlen)
+ {
+ case sizeof (struct IPv4UdpAddress):
+ if (NULL == plugin->sockv4)
+ {
+ return NULL;
+ }
+ t4 = addr;
+ s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
+ len = sizeof (struct sockaddr_in);
+ v4 = (struct sockaddr_in *) &s[1];
+ v4->sin_family = AF_INET;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ v4->sin_len = sizeof (struct sockaddr_in);
+#endif
+ v4->sin_port = t4->u4_port;
+ v4->sin_addr.s_addr = t4->ipv4_addr;
+ s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
+ break;
+ case sizeof (struct IPv6UdpAddress):
+ if (NULL == plugin->sockv6)
+ {
+ return NULL;
+ }
+ t6 = addr;
+ s =
+ GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
+ len = sizeof (struct sockaddr_in6);
+ v6 = (struct sockaddr_in6 *) &s[1];
+ v6->sin6_family = AF_INET6;
+#if HAVE_SOCKADDR_IN_SIN_LEN
+ v6->sin6_len = sizeof (struct sockaddr_in6);
+#endif
+ v6->sin6_port = t6->u6_port;
+ v6->sin6_addr = t6->ipv6_addr;
+ s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
+ break;
+ default:
+ /* Must have a valid address to send to */
+ GNUNET_break_op (0);
+ return NULL;
+ }
+
+ s->addrlen = len;
+ s->target = *target;
+ s->sock_addr = (const struct sockaddr *) &s[1];
+ s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
+ s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
+ s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
+
+ return s;
+}
+
+static int session_cmp_it (void *cls,
+ const GNUNET_HashCode * key,
+ void *value)
+{
+ struct SessionCompareContext * cctx = cls;
+ const struct GNUNET_HELLO_Address *address = cctx->addr;
+ struct Session *s = value;
+
+ socklen_t s_addrlen = s->addrlen;
+
+#if VERBOSE_UDP
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
+ udp_address_to_string (NULL, (void *) address->address, address->address_length),
+ GNUNET_a2s (s->sock_addr, s->addrlen));
+#endif
+
+ if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
+ (s_addrlen == sizeof (struct sockaddr_in)))
+ {
+ struct IPv4UdpAddress * u4 = NULL;
+ u4 = (struct IPv4UdpAddress *) address->address;
+ const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
+ if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
+ (u4->u4_port == s4->sin_port))
+ {
+ cctx->res = s;
+ return GNUNET_NO;
+ }
+
+ }
+ if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
+ (s_addrlen == sizeof (struct sockaddr_in6)))
+ {
+ struct IPv6UdpAddress * u6 = NULL;
+ u6 = (struct IPv6UdpAddress *) address->address;
+ const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
+ if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
+ (u6->u6_port == s6->sin6_port))
+ {
+ cctx->res = s;
+ return GNUNET_NO;
+ }
+ }
+
+
+ return GNUNET_YES;
+}
+
+
+/**
+ * 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 *
+udp_plugin_get_session (void *cls,
+ const struct GNUNET_HELLO_Address *address)
+{
+ struct Session * s = NULL;
+ struct Plugin * plugin = cls;
+ struct IPv6UdpAddress * udp_a6;
+ struct IPv4UdpAddress * udp_a4;
+
+ GNUNET_assert (plugin != NULL);
+ GNUNET_assert (address != NULL);
+
+
+ if ((address->address == NULL) ||
+ ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
+ (address->address_length != sizeof (struct IPv6UdpAddress))))
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+
+ if (address->address_length == sizeof (struct IPv4UdpAddress))
+ {
+ if (plugin->sockv4 == NULL)
+ return NULL;
+ udp_a4 = (struct IPv4UdpAddress *) address->address;
+ if (udp_a4->u4_port == 0)
+ return NULL;
+ }
+
+ if (address->address_length == sizeof (struct IPv6UdpAddress))
+ {
+ if (plugin->sockv6 == NULL)
+ return NULL;
+ udp_a6 = (struct IPv6UdpAddress *) address->address;
+ if (udp_a6->u6_port == 0)
+ return NULL;
+ }
+
+ /* check if session already exists */
+ struct SessionCompareContext cctx;
+ cctx.addr = address;
+ cctx.res = NULL;
+#if VERBOSE_UDP
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for existing session for peer `%s' `%s' \n", GNUNET_i2s (&address->peer), udp_address_to_string(NULL, address->address, address->address_length));
+#endif
+ GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
+ if (cctx.res != NULL)
+ {
+#if VERBOSE_UDP
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
+#endif
+ return cctx.res;
+ }
+
+ /* otherwise create new */
+ s = create_session (plugin,
+ &address->peer,
+ address->address,
+ address->address_length,
+ NULL, NULL);
+#if VERBOSE
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ "Creating new session %p for peer `%s' address `%s'\n",
+ s,
+ GNUNET_i2s(&address->peer),
+ udp_address_to_string(NULL,address->address,address->address_length));
+#endif
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
+ &s->target.hashPubKey,
+ s,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+
+ return s;
+}
+
+static void enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
+{
+
+ if (udpw->session->addrlen == sizeof (struct sockaddr_in))
+ GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
+ if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
+ GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
+}
+
+/**
+ * Function that is called with messages created by the fragmentation
+ * module. In the case of the 'proc' callback of the
+ * GNUNET_FRAGMENT_context_create function, this function must
+ * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
+ *
+ * @param cls closure, the 'struct FragmentationContext'
+ * @param msg the message that was created
+ */
+static void
+enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
+{
+ struct FragmentationContext *frag_ctx = cls;
+ struct Plugin *plugin = frag_ctx->plugin;
+ struct UDPMessageWrapper * udpw;
+ struct Session *s;
+
+ size_t msg_len = ntohs (msg->size);
+
+#if VERBOSE_UDP
+ GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
+#endif
+
+ udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
+ udpw->session = frag_ctx->session;
+ s = udpw->session;
+ udpw->udp = (char *) &udpw[1];
+
+ udpw->msg_size = msg_len;
+ udpw->cont = frag_ctx->cont;
+ udpw->cont_cls = frag_ctx->cont_cls;
+ udpw->timeout = frag_ctx->timeout;
+ udpw->frag_ctx = frag_ctx;
+ memcpy (udpw->udp, msg, msg_len);
+
+ enqueue (plugin, udpw);
+
+
+ if (s->addrlen == sizeof (struct sockaddr_in))
+ {
+ if (plugin->with_v4_ws == GNUNET_NO)
+ {
+ if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel(plugin->select_task);
+
+ plugin->select_task =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_SCHEDULER_NO_TASK,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v4,
+ plugin->ws_v4,
+ &udp_plugin_select, plugin);
+ plugin->with_v4_ws = GNUNET_YES;
+ }
+ }
+
+ else if (s->addrlen == sizeof (struct sockaddr_in6))
+ {
+ if (plugin->with_v6_ws == GNUNET_NO)
+ {
+ if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
+
+ plugin->select_task_v6 =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_SCHEDULER_NO_TASK,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v6,
+ plugin->ws_v6,
+ &udp_plugin_select_v6, plugin);
+ plugin->with_v6_ws = GNUNET_YES;
+ }
+ }
+
+}
+
+
+
+
+/**
+ * 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 s 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
+ * 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 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
+udp_plugin_send (void *cls,
+ struct Session *s,
+ 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;
+ size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
+
+ struct UDPMessageWrapper * udpw;
+ struct UDPMessage *udp;
+ char mbuf[mlen];
+ GNUNET_assert (plugin != NULL);
+ GNUNET_assert (s != NULL);
+
+ if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
+ return GNUNET_SYSERR;
+
+ if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
+ return GNUNET_SYSERR;
+
+
+ if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+
+ if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
+ {
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP transmits %u-byte message to `%s' using address `%s'\n",
+ msgbuf_size,
+ GNUNET_i2s (&s->target),
+ GNUNET_a2s(s->sock_addr, s->addrlen));
+
+ /* Message */
+ udp = (struct UDPMessage *) mbuf;
+ udp->header.size = htons (mlen);
+ udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
+ udp->reserved = htonl (0);
+ udp->sender = *plugin->env->my_identity;
+
+ if (mlen <= UDP_MTU)
+ {
+ udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
+ udpw->session = s;
+ udpw->udp = (char *) &udpw[1];
+ udpw->msg_size = mlen;
+ udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
+ udpw->cont = cont;
+ udpw->cont_cls = cont_cls;
+ udpw->frag_ctx = NULL;
+
+ memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
+ memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
+
+ enqueue (plugin, udpw);
+ }
+ else
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP has to fragment message \n");
+ if (s->frag_ctx != NULL)
+ return GNUNET_SYSERR;
+ memcpy (&udp[1], msgbuf, msgbuf_size);
+ struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
+
+ frag_ctx->plugin = plugin;
+ frag_ctx->session = s;
+ frag_ctx->cont = cont;
+ frag_ctx->cont_cls = cont_cls;
+ frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
+ frag_ctx->bytes_to_send = mlen;
+ frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
+ UDP_MTU,
+ &plugin->tracker,
+ s->last_expected_delay,
+ &udp->header,
+ &enqueue_fragment,
+ frag_ctx);
+
+ s->frag_ctx = frag_ctx;
+
+ }
+
+ if (s->addrlen == sizeof (struct sockaddr_in))
+ {
+ if (plugin->with_v4_ws == GNUNET_NO)
+ {
+ if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel(plugin->select_task);
+
+ plugin->select_task =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_SCHEDULER_NO_TASK,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v4,
+ plugin->ws_v4,
+ &udp_plugin_select, plugin);
+ plugin->with_v4_ws = GNUNET_YES;
+ }
+ }
+
+ else if (s->addrlen == sizeof (struct sockaddr_in6))
+ {
+ if (plugin->with_v6_ws == GNUNET_NO)
+ {
+ if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
+
+ plugin->select_task_v6 =
+ GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+ GNUNET_SCHEDULER_NO_TASK,
+ GNUNET_TIME_UNIT_FOREVER_REL,
+ plugin->rs_v6,
+ plugin->ws_v6,
+ &udp_plugin_select_v6, plugin);
+ plugin->with_v6_ws = GNUNET_YES;
+ }
+ }
+
+ return mlen;
+}
+
+
+/**
+ * Our external IP address/port mapping has changed.
+ *
+ * @param cls closure, the 'struct LocalAddrList'
+ * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
+ * the previous (now invalid) one
+ * @param addr either the previous or the new public IP address
+ * @param addrlen actual lenght of the address
+ */
+static void
+udp_nat_port_map_callback (void *cls, int add_remove,
+ const struct sockaddr *addr, socklen_t addrlen)
+{
+ struct Plugin *plugin = cls;
+ struct IPv4UdpAddress u4;
+ struct IPv6UdpAddress u6;
+ void *arg;
+ size_t args;
+
+ /* convert 'addr' to our internal format */
+ switch (addr->sa_family)
+ {
+ case AF_INET:
+ GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
+ u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
+ u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
+ arg = &u4;
+ args = sizeof (u4);
+ break;
+ case AF_INET6:
+ GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
+ memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
+ sizeof (struct in6_addr));
+ u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
+ arg = &u6;
+ args = sizeof (u6);
+ break;
+ default:
+ GNUNET_break (0);
+ return;
+ }
+ /* modify our published address list */
+ plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
+}
+
+
+
+/**
+ * Message tokenizer has broken up an incomming message. Pass it on
+ * to the service.
+ *
+ * @param cls the 'struct Plugin'
+ * @param client the 'struct SourceInformation'
+ * @param hdr the actual message
+ */
+static void
+process_inbound_tokenized_messages (void *cls, void *client,
+ const struct GNUNET_MessageHeader *hdr)
+{
+ struct Plugin *plugin = cls;
+ struct SourceInformation *si = client;
+ struct GNUNET_ATS_Information ats[2];
+ struct GNUNET_TIME_Relative delay;
+
+ GNUNET_assert (si->session != NULL);
+ /* setup A