#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 DEBUG_UDP GNUNET_NO
+#define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
+
+
+#define DEBUG_UDP GNUNET_EXTRA_LOGGING
/**
* MTU for fragmentation subsystem. Should be conservative since
};
+/**
+ * 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;
+};
+
+
/**
* Network format for IPv4 addresses.
*/
* Session with another peer. FIXME: why not make this into
* a regular 'struct Session' and pass it around!?
*/
-struct PeerSession
+struct Session
{
/**
*/
const struct sockaddr *sock_addr;
+ size_t addrlen;
+
/**
* Function to call upon completion of the transmission.
*/
*/
struct GNUNET_FRAGMENT_Context *frag;
+ struct GNUNET_TIME_Absolute valid_until;
+
+ GNUNET_SCHEDULER_TaskIdentifier invalidation_task;
+
+ GNUNET_SCHEDULER_TaskIdentifier delayed_cont_task;
+
+ /**
+ * 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;
};
/**
* Data structure to track defragmentation contexts based
- * on the source of the UDP traffic.
+ * on the source of the UDP traffic.
*/
struct ReceiveContext
{
*/
size_t addr_len;
+ struct GNUNET_PeerIdentity id;
+
};
*/
struct GNUNET_CONTAINER_MultiHashMap *sessions;
+ /**
+ * Session of peers with whom we are currently connected,
+ * map of peer identity to 'struct PeerSession'.
+ */
+ struct GNUNET_CONTAINER_MultiHashMap *inbound_sessions;
+
/**
* Heap with all of our defragmentation activities.
*/
struct GNUNET_NETWORK_Handle *sockv6;
/**
- * expected delay for ACKs
+ * expected delay for ACKs
*/
struct GNUNET_TIME_Relative last_expected_delay;
};
+struct PeerSessionIteratorContext
+{
+ struct Session * result;
+ const void * addr;
+ size_t addrlen;
+};
+
/**
* Lookup the session for the given peer.
* @param peer peer's identity
* @return NULL if we have no session
*/
-struct PeerSession *
+static struct Session *
find_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *peer)
{
return GNUNET_CONTAINER_multihashmap_get (plugin->sessions,
}
+static int
+inbound_session_iterator (void *cls,
+ const GNUNET_HashCode * key,
+ void *value)
+{
+ struct PeerSessionIteratorContext *psc = cls;
+ struct Session *s = value;
+ if (s->addrlen == psc->addrlen)
+ {
+ if (0 == memcmp (&s[1], psc->addr, s->addrlen))
+ psc->result = s;
+ }
+ if (psc->result != NULL)
+ return GNUNET_NO;
+ return GNUNET_YES;
+}
+
+
+/**
+ * Lookup the session for the given peer.
+ *
+ * @param plugin the plugin
+ * @param peer peer's identity
+ * @return NULL if we have no session
+ */
+static struct Session *
+find_inbound_session (struct Plugin *plugin,
+ const struct GNUNET_PeerIdentity *peer,
+ const void * addr, size_t addrlen)
+{
+ struct PeerSessionIteratorContext psc;
+ psc.result = NULL;
+ psc.addrlen = addrlen;
+ psc.addr = addr;
+
+ GNUNET_CONTAINER_multihashmap_get_multiple(plugin->inbound_sessions, &peer->hashPubKey, &inbound_session_iterator, &psc);
+ return psc.result;
+}
+
+
+static int
+inbound_session_by_addr_iterator (void *cls,
+ const GNUNET_HashCode * key,
+ void *value)
+{
+ struct PeerSessionIteratorContext *psc = cls;
+ struct Session *s = value;
+ if (s->addrlen == psc->addrlen)
+ {
+ if (0 == memcmp (&s[1], psc->addr, s->addrlen))
+ psc->result = s;
+ }
+ if (psc->result != NULL)
+ return GNUNET_NO;
+ else
+ return GNUNET_YES;
+};
+
+/**
+ * Lookup the session for the given peer just by address.
+ *
+ * @param plugin the plugin
+ * @param addr address
+ * @param addrlen address length
+ * @return NULL if we have no session
+ */
+static struct Session *
+find_inbound_session_by_addr (struct Plugin *plugin, const void * addr, size_t addrlen)
+{
+ struct PeerSessionIteratorContext psc;
+ psc.result = NULL;
+ psc.addrlen = addrlen;
+ psc.addr = addr;
+
+ GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &inbound_session_by_addr_iterator, &psc);
+ return psc.result;
+}
+
+
+/**
+ * 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
+destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct Session *peer_session = value;
+
+ if (peer_session->frag != NULL)
+ GNUNET_FRAGMENT_context_destroy (peer_session->frag);
+ if (GNUNET_SCHEDULER_NO_TASK != peer_session->delayed_cont_task)
+ GNUNET_SCHEDULER_cancel (peer_session->delayed_cont_task);
+ GNUNET_free (peer_session);
+ 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
+destroy_inbound_session (void *cls, const GNUNET_HashCode * key, void *value)
+{
+ struct Session *s = value;
+
+ if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
+ GNUNET_SCHEDULER_cancel(s->invalidation_task);
+ if (GNUNET_SCHEDULER_NO_TASK != s->delayed_cont_task)
+ GNUNET_SCHEDULER_cancel (s->delayed_cont_task);
+ GNUNET_free (s);
+ return GNUNET_OK;
+}
+
/**
* Disconnect from a remote node. Clean up session if we have one for this peer
*
udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
{
struct Plugin *plugin = cls;
- struct PeerSession *session;
+ struct Session *session;
session = find_session (plugin, target);
if (NULL == session)
GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
&target->hashPubKey,
session));
+
+ GNUNET_CONTAINER_multihashmap_get_multiple (plugin->inbound_sessions,
+ &target->hashPubKey,
+ &destroy_inbound_session, NULL);
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "UDP DISCONNECT\n");
+
plugin->last_expected_delay = GNUNET_FRAGMENT_context_destroy (session->frag);
- session->cont (session->cont_cls, target, GNUNET_SYSERR);
+ if (GNUNET_SCHEDULER_NO_TASK != session->delayed_cont_task)
+ GNUNET_SCHEDULER_cancel (session->delayed_cont_task);
+ if (session->cont != NULL)
+ session->cont (session->cont_cls, target, GNUNET_SYSERR);
GNUNET_free (session);
}
* Actually send out the message.
*
* @param plugin the plugin
- * @param send_handle which handle to send message on
- * @param target who should receive this message (ignored by UDP)
- * @param msgbuf one or more GNUNET_MessageHeader(s) strung together
- * @param msgbuf_size 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 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
+ * @param sa the address to send the message to
+ * @param msg message to transmit
* @return the number of bytes written
*/
static ssize_t
return 0;
}
if (GNUNET_SYSERR == sent)
- GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "sendto");
-#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "UDP transmited %u-byte message to %s (%d: %s)\n",
+ (unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
+ (int) sent, (sent < 0) ? STRERROR (errno) : "ok");
+
+ }
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"UDP transmited %u-byte message to %s (%d: %s)\n",
(unsigned int) ntohs (msg->size), GNUNET_a2s (sa, slen),
(int) sent, (sent < 0) ? STRERROR (errno) : "ok");
-#endif
return sent;
}
static void
send_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
{
- struct PeerSession *session = cls;
+ struct Session *session = cls;
udp_send (session->plugin, session->sock_addr, msg);
GNUNET_FRAGMENT_context_transmission_done (session->frag);
}
+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 * peer_session;
+ 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;
+ peer_session =
+ GNUNET_malloc (sizeof (struct Session) +
+ sizeof (struct sockaddr_in));
+ len = sizeof (struct sockaddr_in);
+ v4 = (struct sockaddr_in *) &peer_session[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;
+ break;
+ case sizeof (struct IPv6UdpAddress):
+ if (NULL == plugin->sockv6)
+ {
+ return NULL;
+ }
+ t6 = addr;
+ peer_session =
+ GNUNET_malloc (sizeof (struct Session) +
+ sizeof (struct sockaddr_in6));
+ len = sizeof (struct sockaddr_in6);
+ v6 = (struct sockaddr_in6 *) &peer_session[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;
+ break;
+ default:
+ /* Must have a valid address to send to */
+ GNUNET_break_op (0);
+ return NULL;
+ }
+
+ peer_session->valid_until = GNUNET_TIME_absolute_get_zero ();
+ peer_session->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
+ peer_session->addrlen = len;
+ peer_session->target = *target;
+ peer_session->plugin = plugin;
+ peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
+ peer_session->cont = cont;
+ peer_session->cont_cls = cont_cls;
+
+ return peer_session;
+}
+
+static const char *
+udp_address_to_string (void *cls, const void *addr, size_t addrlen);
+
+
+static void
+udp_call_continuation (void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct Session *s = cls;
+ GNUNET_TRANSPORT_TransmitContinuation cont = s->cont;
+
+ s->delayed_cont_task = GNUNET_SCHEDULER_NO_TASK;
+ s->cont = NULL;
+ cont (s->cont_cls, &s->target, GNUNET_OK);
+}
+
+
/**
* Function that can be used by the transport service to transmit
* a message using the plugin.
GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
{
struct Plugin *plugin = cls;
- struct PeerSession *peer_session;
+ struct Session *peer_session;
+ struct Session *s;
const struct IPv4UdpAddress *t4;
const struct IPv6UdpAddress *t6;
- struct sockaddr_in *v4;
- struct sockaddr_in6 *v6;
size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
char mbuf[mlen];
struct UDPMessage *udp;
+ struct GNUNET_TIME_Relative delta;
- if (force_address == GNUNET_SYSERR)
- return GNUNET_SYSERR;
- GNUNET_assert (NULL == session);
if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
{
GNUNET_break (0);
return GNUNET_SYSERR;
}
- switch (addrlen)
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP transmits %u-byte message to `%s' using address `%s' session 0x%X mode %i\n",
+ msgbuf_size, GNUNET_i2s (target),
+ udp_address_to_string (NULL, addr, addrlen),
+ session, force_address);
+
+ if ((force_address == GNUNET_SYSERR) && (session == NULL))
+ return GNUNET_SYSERR;
+
+ s = NULL;
+ /* safety check: comparing address to address stored in session */
+ if ((session != NULL) && (addr != NULL) && (addrlen != 0))
{
- case sizeof (struct IPv4UdpAddress):
- if (NULL == plugin->sockv4)
- {
- cont (cont_cls, target, GNUNET_SYSERR);
- return 0;
- }
- t4 = addr;
- peer_session =
- GNUNET_malloc (sizeof (struct PeerSession) +
- sizeof (struct sockaddr_in));
- v4 = (struct sockaddr_in *) &peer_session[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;
- break;
- case sizeof (struct IPv6UdpAddress):
- if (NULL == plugin->sockv6)
+ s = session;
+ GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (
+ plugin->inbound_sessions, &target->hashPubKey, s));
+
+ if (0 != memcmp (&s->target, target, sizeof (struct GNUNET_PeerIdentity)))
+ return GNUNET_SYSERR;
+ switch (addrlen)
{
- cont (cont_cls, target, GNUNET_SYSERR);
- return 0;
+ case sizeof (struct IPv4UdpAddress):
+ if (NULL == plugin->sockv4)
+ {
+ if (cont != NULL)
+ cont (cont_cls, target, GNUNET_SYSERR);
+ return GNUNET_SYSERR;
+ }
+ t4 = addr;
+ if (s->addrlen != (sizeof (struct sockaddr_in)))
+ return GNUNET_SYSERR;
+ struct sockaddr_in *a4 = (struct sockaddr_in *) s->sock_addr;
+ GNUNET_assert (a4->sin_port == t4->u4_port);
+ GNUNET_assert (0 == memcmp(&a4->sin_addr, &t4->ipv4_addr, sizeof (struct in_addr)));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Session 0x%X successfully checked!\n", session);
+ break;
+ case sizeof (struct IPv6UdpAddress):
+ if (NULL == plugin->sockv6)
+ {
+ if (cont != NULL)
+ cont (cont_cls, target, GNUNET_SYSERR);
+ return GNUNET_SYSERR;
+ }
+ t6 = addr;
+ GNUNET_assert (s->addrlen == sizeof (struct sockaddr_in6));
+ struct sockaddr_in6 *a6 = (struct sockaddr_in6 *) s->sock_addr;
+ GNUNET_assert (a6->sin6_port == t6->u6_port);
+ GNUNET_assert (0 == memcmp(&a6->sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr)));
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Session 0x%X successfully checked!\n", session);
+ break;
+ default:
+ /* Must have a valid address to send to */
+ GNUNET_break_op (0);
}
- t6 = addr;
- peer_session =
- GNUNET_malloc (sizeof (struct PeerSession) +
- sizeof (struct sockaddr_in6));
- v6 = (struct sockaddr_in6 *) &peer_session[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;
- break;
- default:
- /* Must have a valid address to send to */
- GNUNET_break_op (0);
+ }
+//session_invalid:
+ if ((addr == NULL) || (addrlen == 0))
return GNUNET_SYSERR;
+ peer_session = create_session (plugin, target, addr, addrlen, cont, cont_cls);
+ if (peer_session == NULL)
+ {
+ if (cont != NULL)
+ cont (cont_cls, target, GNUNET_SYSERR);
+ return GNUNET_SYSERR;;
}
+
+ /* 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;
memcpy (&udp[1], msgbuf, msgbuf_size);
- peer_session->target = *target;
- peer_session->plugin = plugin;
- peer_session->sock_addr = (const struct sockaddr *) &peer_session[1];
- peer_session->cont = cont;
- peer_session->cont_cls = cont_cls;
+
+ if (s != NULL)
+ delta = GNUNET_TIME_absolute_get_remaining (s->flow_delay_from_other_peer);
+ else
+ delta = GNUNET_TIME_UNIT_ZERO;
if (mlen <= UDP_MTU)
{
mlen = udp_send (plugin, peer_session->sock_addr, &udp->header);
- cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
- GNUNET_free (peer_session);
+ if (cont != NULL)
+ {
+ if ( (delta.rel_value > 0) &&
+ (mlen > 0) )
+ {
+ s->cont = cont;
+ s->cont_cls = cont_cls;
+ s->delayed_cont_task = GNUNET_SCHEDULER_add_delayed (delta,
+ &udp_call_continuation,
+ s);
+ }
+ else
+ cont (cont_cls, target, (mlen > 0) ? GNUNET_OK : GNUNET_SYSERR);
+ }
+ GNUNET_free_non_null (peer_session);
}
else
{
* Number of bytes in source address.
*/
size_t args;
+
+ struct Session * session;
};
/**
- * Message tokenizer has broken up an incomming message. Pass it on
+ * Message tokenizer has broken up an incomming message. Pass it on
* to the service.
*
* @param cls the 'struct Plugin'
{
struct Plugin *plugin = cls;
struct SourceInformation *si = client;
- struct GNUNET_TRANSPORT_ATS_Information distance[2];
+ struct GNUNET_ATS_Information distance;
+ struct GNUNET_TIME_Relative delay;
/* setup ATS */
- distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
- distance[0].value = htonl (1);
- distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
- distance[1].value = htonl (0);
+ distance.type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
+ distance.value = htonl (1);
- plugin->env->receive (plugin->env->cls, &si->sender, hdr, distance, 2, NULL,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Giving Session %X %s to transport\n", si->session, GNUNET_i2s(&si->session->target));
+ delay = plugin->env->receive (plugin->env->cls, &si->sender, hdr, &distance, 1, si->session,
si->arg, si->args);
+ si->session->flow_delay_for_other_peer = delay;
+}
+
+static void
+invalidation_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ struct Session * s = cls;
+ s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
+ LOG (GNUNET_ERROR_TYPE_ERROR,
+ "Session %X (`%s') is now invalid\n", s, GNUNET_a2s (s->sock_addr,s->addrlen));
+
+ s->plugin->env->session_end(s->plugin->env->cls, &s->target, s);
+ GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(s->plugin->inbound_sessions, &s->target.hashPubKey, s));
+ GNUNET_free (s);
}
*
* @param plugin plugin context
* @param msg the message
- * @param sender_addr sender address
+ * @param sender_addr sender address
* @param sender_addr_len number of bytes in sender_addr
*/
static void
return;
}
#if DEBUG_UDP
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp",
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Received message with %u bytes from peer `%s' at `%s'\n",
(unsigned int) ntohs (msg->header.size),
GNUNET_i2s (&msg->sender), GNUNET_a2s (sender_addr,
sender_addr_len));
#endif
+ /* create a session for inbound connections */
+ const struct UDPMessage * udp_msg = (const struct UDPMessage *) msg;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Lookup inbound UDP sessions for peer `%s' address `%s'\n",
+ GNUNET_i2s (&udp_msg->sender),
+ udp_address_to_string(NULL, arg, args));
+
+ struct Session * s = NULL;
+ s = find_inbound_session (plugin, &udp_msg->sender, sender_addr, sender_addr_len);
+
+ if (s != NULL)
+ {
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Found existing inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
+ s,
+ GNUNET_i2s (&s->target),
+ udp_address_to_string(NULL, arg, args));
+ }
+ else
+ {
+ s = create_session (plugin, &udp_msg->sender, arg, args, NULL, NULL);
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Creating inbound UDP sessions 0x%X for peer `%s' address `%s'\n",
+ s,
+ GNUNET_i2s (&s->target),
+ udp_address_to_string(NULL, arg, args));
+
+ GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (plugin->inbound_sessions,
+ &s->target.hashPubKey,
+ s,
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
+ }
+ s->valid_until = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
+ if (s->invalidation_task != GNUNET_SCHEDULER_NO_TASK)
+ {
+ GNUNET_SCHEDULER_cancel(s->invalidation_task);
+ s->invalidation_task = GNUNET_SCHEDULER_NO_TASK;
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "Rescheduling %X' `%s'\n",
+ s, udp_address_to_string(NULL, arg, args));
+ }
+ s->invalidation_task = GNUNET_SCHEDULER_add_delayed(GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &invalidation_task, s);
/* iterate over all embedded messages */
si.sender = msg->sender;
si.arg = arg;
si.args = args;
+ si.session = s;
GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
ntohs (msg->header.size) -
sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
{
struct ReceiveContext *rc = cls;
- size_t msize = sizeof (struct UDPMessage) + ntohs (msg->size);
+
+ size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
char buf[msize];
- struct UDPMessage *udp;
+ struct UDP_ACK_Message *udp_ack;
+ uint32_t delay = 0;
+
+ struct Session *s;
+ s = find_inbound_session_by_addr (rc->plugin, rc->src_addr, rc->addr_len);
+ if (s != NULL)
+ {
+ if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
+ delay = s->flow_delay_for_other_peer.rel_value;
+ else
+ delay = UINT32_MAX;
+ }
+
#if DEBUG_UDP
- GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "udp", "Sending ACK to `%s'\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ACK to `%s' including delay of %u ms\n",
GNUNET_a2s (rc->src_addr,
(rc->src_addr->sa_family ==
AF_INET) ? sizeof (struct sockaddr_in) :
- sizeof (struct sockaddr_in6)));
+ sizeof (struct sockaddr_in6)),
+ delay);
#endif
- udp = (struct UDPMessage *) buf;
- udp->header.size = htons ((uint16_t) msize);
- udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
- udp->reserved = htonl (0);
- udp->sender = *rc->plugin->env->my_identity;
- memcpy (&udp[1], msg, ntohs (msg->size));
- (void) udp_send (rc->plugin, rc->src_addr, &udp->header);
+ udp_ack = (struct UDP_ACK_Message *) buf;
+ udp_ack->header.size = htons ((uint16_t) msize);
+ udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
+ udp_ack->delay = htonl (delay);
+ udp_ack->sender = *rc->plugin->env->my_identity;
+ memcpy (&udp_ack[1], msg, ntohs (msg->size));
+ (void) udp_send (rc->plugin, rc->src_addr, &udp_ack->header);
}
* Number of bytes in 'addr'.
*/
socklen_t addr_len;
+
+ struct Session * session;
};
ssize_t ret;
const struct GNUNET_MessageHeader *msg;
const struct GNUNET_MessageHeader *ack;
- struct PeerSession *peer_session;
- const struct UDPMessage *udp;
+ struct Session *peer_session;
+ const struct UDP_ACK_Message *udp_ack;
struct ReceiveContext *rc;
struct GNUNET_TIME_Absolute now;
struct FindReceiveContext frc;
+ struct Session * s = NULL;
+ struct GNUNET_TIME_Relative flow_delay;
fromlen = sizeof (addr);
memset (&addr, 0, sizeof (addr));
GNUNET_break_op (0);
return;
}
-#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
- "UDP received %u-byte message from `%s'\n", (unsigned int) ret,
- GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
-#endif
msg = (const struct GNUNET_MessageHeader *) buf;
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "UDP received %u-byte message from `%s' type %i\n", (unsigned int) ret,
+ GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs(msg->type));
+
if (ret != ntohs (msg->size))
{
GNUNET_break_op (0);
(const struct sockaddr *) addr, fromlen);
return;
case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
+
if (ntohs (msg->size) <
- sizeof (struct UDPMessage) + sizeof (struct GNUNET_MessageHeader))
+ sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
{
GNUNET_break_op (0);
return;
}
- udp = (const struct UDPMessage *) msg;
- if (ntohl (udp->reserved) != 0)
+ udp_ack = (const struct UDP_ACK_Message *) msg;
+ s = find_inbound_session(plugin, &udp_ack->sender, addr, fromlen);
+ if (s != NULL)
{
- GNUNET_break_op (0);
- return;
+ flow_delay.rel_value = (uint64_t) ntohl(udp_ack->delay);
+
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
+ "We received a sending delay of %llu\n", flow_delay.rel_value);
+
+ s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
}
- ack = (const struct GNUNET_MessageHeader *) &udp[1];
- if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDPMessage))
+ ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
+ if (ntohs (ack->size) != ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
{
GNUNET_break_op (0);
return;
}
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
- (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp->sender),
+ (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
#endif
- peer_session = find_session (plugin, &udp->sender);
+ peer_session = find_session (plugin, &udp_ack->sender);
if (NULL == peer_session)
{
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Session for ACK not found, dropping ACK!\n");
#endif
return;
return;
GNUNET_assert (GNUNET_OK ==
GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
- &udp->
+ &udp_ack->
sender.hashPubKey,
peer_session));
plugin->last_expected_delay =
GNUNET_FRAGMENT_context_destroy (peer_session->frag);
- peer_session->cont (peer_session->cont_cls, &udp->sender, GNUNET_OK);
+ if (peer_session->cont != NULL)
+ peer_session->cont (peer_session->cont_cls, &udp_ack->sender, GNUNET_OK);
GNUNET_free (peer_session);
return;
case GNUNET_MESSAGE_TYPE_FRAGMENT:
now.abs_value);
}
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"UDP processes %u-byte fragment from `%s'\n",
(unsigned int) ntohs (msg->size),
GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
return NULL;
}
inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
- GNUNET_snprintf (rbuf, sizeof (rbuf), "%s:%u", buf, port);
+ GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
+ buf, port);
return rbuf;
}
aport = port;
if (port > 65535)
{
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ LOG (GNUNET_ERROR_TYPE_WARNING,
_("Given `%s' option is out of range: %llu > %u\n"), "PORT",
port, 65535);
return NULL;
GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
"BINDTO", &plugin->bind4_address))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Binding udp plugin to specific address: `%s'\n",
plugin->bind4_address);
if (1 != inet_pton (AF_INET, plugin->bind4_address, &serverAddrv4.sin_addr))
GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
"BINDTO6", &plugin->bind6_address))
{
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"Binding udp plugin to specific address: `%s'\n",
plugin->bind6_address);
if (1 !=
inet_pton (AF_INET6, plugin->bind6_address, &serverAddrv6.sin6_addr))
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
+ LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
plugin->bind6_address);
GNUNET_free_non_null (plugin->bind4_address);
GNUNET_free (plugin->bind6_address);
plugin->sessions =
GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
* 2);
+ plugin->inbound_sessions =
+ GNUNET_CONTAINER_multihashmap_create (UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG
+ * 2);
sockets_created = 0;
if ((GNUNET_YES !=
GNUNET_CONFIGURATION_get_value_yesno (plugin->env->cfg, "nat",
addrlen = sizeof (serverAddrv6);
serverAddr = (struct sockaddr *) &serverAddrv6;
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
ntohs (serverAddrv6.sin6_port));
#endif
tries = 0;
{
serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"IPv6 Binding failed, trying new port %d\n",
ntohs (serverAddrv6.sin6_port));
#endif
addrlen = sizeof (serverAddrv4);
serverAddr = (struct sockaddr *) &serverAddrv4;
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
ntohs (serverAddrv4.sin_port));
#endif
tries = 0;
{
serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
#if DEBUG_UDP
- GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+ LOG (GNUNET_ERROR_TYPE_DEBUG,
"IPv4 Binding failed, trying new port %d\n",
ntohs (serverAddrv4.sin_port));
#endif
return api;
}
-
-/**
- * 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
-destroy_session (void *cls, const GNUNET_HashCode * key, void *value)
-{
- struct PeerSession *peer_session = value;
-
- GNUNET_FRAGMENT_context_destroy (peer_session->frag);
- GNUNET_free (peer_session);
- return GNUNET_OK;
-}
-
-
/**
* Shutdown the plugin.
*
NULL);
GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
plugin->sessions = NULL;
+ GNUNET_CONTAINER_multihashmap_iterate (plugin->inbound_sessions, &destroy_inbound_session,
+ NULL);
+ GNUNET_CONTAINER_multihashmap_destroy (plugin->inbound_sessions);
+ plugin->inbound_sessions = NULL;
while (NULL != (rc = GNUNET_CONTAINER_heap_remove_root (plugin->defrags)))
{
GNUNET_DEFRAGMENT_context_destroy (rc->defrag);