2 This file is part of GNUnet
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP transport protocol
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Matthias Wachs
29 #include "plugin_transport_udp.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
47 * Number of messages we can defragment in parallel. We only really
48 * defragment 1 message at a time, but if messages get re-ordered, we
49 * may want to keep knowledge about the previous message to avoid
50 * discarding the current message in favor of a single fragment of a
51 * previous message. 3 should be good since we don't expect massive
52 * message reorderings with UDP.
54 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
57 * We keep a defragmentation queue per sender address. How many
58 * sender addresses do we support at the same time? Memory consumption
59 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
60 * value. (So 128 corresponds to 12 MB and should suffice for
61 * connecting to roughly 128 peers via UDP).
63 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
68 * Closure for 'append_port'.
70 struct PrettyPrinterContext
73 * Function to call with the result.
75 GNUNET_TRANSPORT_AddressStringCallback asc;
83 * Port to add after the IP address.
92 * Which peer is this session for?
94 struct GNUNET_PeerIdentity target;
97 * Address of the other peer
99 const struct sockaddr *sock_addr;
104 * Desired delay for next sending we send to other peer
106 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
109 * Desired delay for next sending we received from other peer
111 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
114 * expected delay for ACKs
116 struct GNUNET_TIME_Relative last_expected_delay;
118 struct GNUNET_ATS_Information ats;
120 struct FragmentationContext * frag_ctx;
128 struct SessionCompareContext
131 const struct GNUNET_HELLO_Address *addr;
136 * Closure for 'process_inbound_tokenized_messages'
138 struct SourceInformation
143 struct GNUNET_PeerIdentity sender;
151 * Number of bytes in source address.
155 struct Session *session;
160 * Closure for 'find_receive_context'.
162 struct FindReceiveContext
165 * Where to store the result.
167 struct DefragContext *rc;
172 const struct sockaddr *addr;
175 * Number of bytes in 'addr'.
179 struct Session *session;
185 * Data structure to track defragmentation contexts based
186 * on the source of the UDP traffic.
192 * Defragmentation context.
194 struct GNUNET_DEFRAGMENT_Context *defrag;
197 * Source address this receive context is for (allocated at the
198 * end of the struct).
200 const struct sockaddr *src_addr;
203 * Reference to master plugin struct.
205 struct Plugin *plugin;
208 * Node in the defrag heap.
210 struct GNUNET_CONTAINER_HeapNode *hnode;
213 * Length of 'src_addr'
221 * Closure for 'process_inbound_tokenized_messages'
223 struct FragmentationContext
225 struct FragmentationContext * next;
226 struct FragmentationContext * prev;
228 struct Plugin * plugin;
229 struct GNUNET_FRAGMENT_Context * frag;
230 struct Session * session;
232 struct GNUNET_TIME_Absolute timeout;
236 * Function to call upon completion of the transmission.
238 GNUNET_TRANSPORT_TransmitContinuation cont;
241 * Closure for 'cont'.
245 size_t bytes_to_send;
249 struct UDPMessageWrapper
251 struct Session *session;
252 struct UDPMessageWrapper *prev;
253 struct UDPMessageWrapper *next;
257 struct GNUNET_TIME_Absolute timeout;
260 * Function to call upon completion of the transmission.
262 GNUNET_TRANSPORT_TransmitContinuation cont;
265 * Closure for 'cont'.
269 struct FragmentationContext *frag_ctx;
275 * UDP ACK Message-Packet header (after defragmentation).
277 struct UDP_ACK_Message
282 struct GNUNET_MessageHeader header;
285 * Desired delay for flow control
290 * What is the identity of the sender
292 struct GNUNET_PeerIdentity sender;
296 static int cont_calls;
299 * We have been notified that our readset has something to read. We don't
300 * know which socket needs to be read, so we have to check each one
301 * Then reschedule this function to be called again once more is available.
303 * @param cls the plugin handle
304 * @param tc the scheduling context (for rescheduling this function again)
307 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
310 * We have been notified that our readset has something to read. We don't
311 * know which socket needs to be read, so we have to check each one
312 * Then reschedule this function to be called again once more is available.
314 * @param cls the plugin handle
315 * @param tc the scheduling context (for rescheduling this function again)
318 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
321 * Function called for a quick conversion of the binary address to
322 * a numeric address. Note that the caller must not free the
323 * address and that the next call to this function is allowed
324 * to override the address again.
327 * @param addr binary address
328 * @param addrlen length of the address
329 * @return string representing the same address
332 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
334 static char rbuf[INET6_ADDRSTRLEN + 10];
335 char buf[INET6_ADDRSTRLEN];
339 const struct IPv4UdpAddress *t4;
340 const struct IPv6UdpAddress *t6;
344 if (addrlen == sizeof (struct IPv6UdpAddress))
348 port = ntohs (t6->u6_port);
349 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
352 else if (addrlen == sizeof (struct IPv4UdpAddress))
356 port = ntohs (t4->u4_port);
357 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
365 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
366 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
373 * Function called to convert a string address to
376 * @param cls closure ('struct Plugin*')
377 * @param addr string address
378 * @param addrlen length of the address
379 * @param buf location to store the buffer
380 * @param added location to store the number of bytes in the buffer.
381 * If the function returns GNUNET_SYSERR, its contents are undefined.
382 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
385 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
386 void **buf, size_t *added)
388 struct sockaddr_storage socket_address;
390 if ((NULL == addr) || (addrlen == 0))
393 return GNUNET_SYSERR;
396 if ('\0' != addr[addrlen - 1])
398 return GNUNET_SYSERR;
401 if (strlen (addr) != addrlen - 1)
403 return GNUNET_SYSERR;
406 int ret = GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
409 if (ret != GNUNET_OK)
411 return GNUNET_SYSERR;
414 if (socket_address.ss_family == AF_INET)
416 struct IPv4UdpAddress *u4;
417 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
418 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
419 u4->ipv4_addr = in4->sin_addr.s_addr;
420 u4->u4_port = in4->sin_port;
422 *added = sizeof (struct IPv4UdpAddress);
425 else if (socket_address.ss_family == AF_INET6)
427 struct IPv6UdpAddress *u6;
428 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
429 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
430 u6->ipv6_addr = in6->sin6_addr;
431 u6->u6_port = in6->sin6_port;
433 *added = sizeof (struct IPv6UdpAddress);
436 return GNUNET_SYSERR;
441 * Append our port and forward the result.
443 * @param cls a 'struct PrettyPrinterContext'
444 * @param hostname result from DNS resolver
447 append_port (void *cls, const char *hostname)
449 struct PrettyPrinterContext *ppc = cls;
452 if (hostname == NULL)
454 ppc->asc (ppc->asc_cls, NULL);
458 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
459 ppc->asc (ppc->asc_cls, ret);
465 * Convert the transports address to a nice, human-readable
469 * @param type name of the transport that generated the address
470 * @param addr one of the addresses of the host, NULL for the last address
471 * the specific address format depends on the transport
472 * @param addrlen length of the address
473 * @param numeric should (IP) addresses be displayed in numeric form?
474 * @param timeout after how long should we give up?
475 * @param asc function to call on each string
476 * @param asc_cls closure for asc
479 udp_plugin_address_pretty_printer (void *cls, const char *type,
480 const void *addr, size_t addrlen,
482 struct GNUNET_TIME_Relative timeout,
483 GNUNET_TRANSPORT_AddressStringCallback asc,
486 struct PrettyPrinterContext *ppc;
489 struct sockaddr_in a4;
490 struct sockaddr_in6 a6;
491 const struct IPv4UdpAddress *u4;
492 const struct IPv6UdpAddress *u6;
495 if (addrlen == sizeof (struct IPv6UdpAddress))
498 memset (&a6, 0, sizeof (a6));
499 a6.sin6_family = AF_INET6;
500 #if HAVE_SOCKADDR_IN_SIN_LEN
501 a6.sin6_len = sizeof (a6);
503 a6.sin6_port = u6->u6_port;
504 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
505 port = ntohs (u6->u6_port);
509 else if (addrlen == sizeof (struct IPv4UdpAddress))
512 memset (&a4, 0, sizeof (a4));
513 a4.sin_family = AF_INET;
514 #if HAVE_SOCKADDR_IN_SIN_LEN
515 a4.sin_len = sizeof (a4);
517 a4.sin_port = u4->u4_port;
518 a4.sin_addr.s_addr = u4->ipv4_addr;
519 port = ntohs (u4->u4_port);
523 else if (0 == addrlen)
525 asc (asc_cls, "<inbound connection>");
531 /* invalid address */
536 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
538 ppc->asc_cls = asc_cls;
540 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
544 call_continuation (struct UDPMessageWrapper *udpw, int result)
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Calling continuation for %u byte message to `%s' with result %s\n",
548 udpw->msg_size, GNUNET_i2s (&udpw->session->target),
549 (GNUNET_OK == result) ? "OK" : "SYSERR");
550 if (NULL != udpw->cont)
552 udpw->cont (udpw->cont_cls, &udpw->session->target,result);
553 GNUNET_assert (cont_calls > 0);
560 * Check if the given port is plausible (must be either our listen
561 * port or our advertised port). If it is neither, we return
564 * @param plugin global variables
565 * @param in_port port number to check
566 * @return GNUNET_OK if port is either open_port or adv_port
569 check_port (struct Plugin *plugin, uint16_t in_port)
571 if ((in_port == plugin->port) || (in_port == plugin->aport))
573 return GNUNET_SYSERR;
579 * Function that will be called to check if a binary address for this
580 * plugin is well-formed and corresponds to an address for THIS peer
581 * (as per our configuration). Naturally, if absolutely necessary,
582 * plugins can be a bit conservative in their answer, but in general
583 * plugins should make sure that the address does not redirect
584 * traffic to a 3rd party that might try to man-in-the-middle our
587 * @param cls closure, should be our handle to the Plugin
588 * @param addr pointer to the address
589 * @param addrlen length of addr
590 * @return GNUNET_OK if this is a plausible address for this peer
591 * and transport, GNUNET_SYSERR if not
595 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
597 struct Plugin *plugin = cls;
598 struct IPv4UdpAddress *v4;
599 struct IPv6UdpAddress *v6;
601 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
602 (addrlen != sizeof (struct IPv6UdpAddress)))
605 return GNUNET_SYSERR;
607 if (addrlen == sizeof (struct IPv4UdpAddress))
609 v4 = (struct IPv4UdpAddress *) addr;
610 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
611 return GNUNET_SYSERR;
613 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
614 sizeof (struct in_addr)))
615 return GNUNET_SYSERR;
619 v6 = (struct IPv6UdpAddress *) addr;
620 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
623 return GNUNET_SYSERR;
625 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
626 return GNUNET_SYSERR;
628 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
629 sizeof (struct in6_addr)))
630 return GNUNET_SYSERR;
637 * Task to free resources associated with a session.
639 * @param s session to free
642 free_session (struct Session *s)
644 if (s->frag_ctx != NULL)
646 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
647 GNUNET_free (s->frag_ctx);
655 * Destroy a session, plugin is being unloaded.
658 * @param key hash of public key of target peer
659 * @param value a 'struct PeerSession*' to clean up
660 * @return GNUNET_OK (continue to iterate)
663 disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
665 struct Plugin *plugin = cls;
666 struct Session *s = value;
667 struct UDPMessageWrapper *udpw;
668 struct UDPMessageWrapper *next;
670 GNUNET_assert (GNUNET_YES != s->in_destroy);
671 LOG (GNUNET_ERROR_TYPE_DEBUG,
672 "Session %p to peer `%s' address ended \n",
674 GNUNET_i2s (&s->target),
675 GNUNET_a2s (s->sock_addr, s->addrlen));
676 next = plugin->ipv4_queue_head;
677 while (NULL != (udpw = next))
680 if (udpw->session == s)
682 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
683 call_continuation(udpw, GNUNET_SYSERR);
687 next = plugin->ipv6_queue_head;
688 while (NULL != (udpw = next))
691 if (udpw->session == s)
693 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
694 call_continuation(udpw, GNUNET_SYSERR);
699 plugin->env->session_end (plugin->env->cls, &s->target, s);
701 if (NULL != s->frag_ctx)
703 if (NULL != s->frag_ctx->cont)
705 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR);
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
709 GNUNET_i2s (&s->target));
713 GNUNET_assert (GNUNET_YES ==
714 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
715 &s->target.hashPubKey,
717 GNUNET_STATISTICS_set(plugin->env->stats,
718 "# UDP sessions active",
719 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
722 s->in_destroy = GNUNET_YES;
730 * Disconnect from a remote node. Clean up session if we have one for this peer
732 * @param cls closure for this call (should be handle to Plugin)
733 * @param target the peeridentity of the peer to disconnect
734 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
737 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
739 struct Plugin *plugin = cls;
740 GNUNET_assert (plugin != NULL);
742 GNUNET_assert (target != NULL);
743 LOG (GNUNET_ERROR_TYPE_DEBUG,
744 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
745 /* Clean up sessions */
746 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
749 static struct Session *
750 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
751 const void *addr, size_t addrlen,
752 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
755 const struct IPv4UdpAddress *t4;
756 const struct IPv6UdpAddress *t6;
757 struct sockaddr_in *v4;
758 struct sockaddr_in6 *v6;
763 case sizeof (struct IPv4UdpAddress):
764 if (NULL == plugin->sockv4)
769 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
770 len = sizeof (struct sockaddr_in);
771 v4 = (struct sockaddr_in *) &s[1];
772 v4->sin_family = AF_INET;
773 #if HAVE_SOCKADDR_IN_SIN_LEN
774 v4->sin_len = sizeof (struct sockaddr_in);
776 v4->sin_port = t4->u4_port;
777 v4->sin_addr.s_addr = t4->ipv4_addr;
778 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
780 case sizeof (struct IPv6UdpAddress):
781 if (NULL == plugin->sockv6)
787 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
788 len = sizeof (struct sockaddr_in6);
789 v6 = (struct sockaddr_in6 *) &s[1];
790 v6->sin6_family = AF_INET6;
791 #if HAVE_SOCKADDR_IN_SIN_LEN
792 v6->sin6_len = sizeof (struct sockaddr_in6);
794 v6->sin6_port = t6->u6_port;
795 v6->sin6_addr = t6->ipv6_addr;
796 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
799 /* Must have a valid address to send to */
806 s->sock_addr = (const struct sockaddr *) &s[1];
807 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
808 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
809 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
816 session_cmp_it (void *cls,
817 const GNUNET_HashCode * key,
820 struct SessionCompareContext * cctx = cls;
821 const struct GNUNET_HELLO_Address *address = cctx->addr;
822 struct Session *s = value;
824 socklen_t s_addrlen = s->addrlen;
827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
828 udp_address_to_string (NULL, (void *) address->address, address->address_length),
829 GNUNET_a2s (s->sock_addr, s->addrlen));
832 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
833 (s_addrlen == sizeof (struct sockaddr_in)))
835 struct IPv4UdpAddress * u4 = NULL;
836 u4 = (struct IPv4UdpAddress *) address->address;
837 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
838 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
839 (u4->u4_port == s4->sin_port))
846 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
847 (s_addrlen == sizeof (struct sockaddr_in6)))
849 struct IPv6UdpAddress * u6 = NULL;
850 u6 = (struct IPv6UdpAddress *) address->address;
851 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
852 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
853 (u6->u6_port == s6->sin6_port))
866 * Creates a new outbound session the transport service will use to send data to the
869 * @param cls the plugin
870 * @param address the address
871 * @return the session or NULL of max connections exceeded
873 static struct Session *
874 udp_plugin_get_session (void *cls,
875 const struct GNUNET_HELLO_Address *address)
877 struct Session * s = NULL;
878 struct Plugin * plugin = cls;
879 struct IPv6UdpAddress * udp_a6;
880 struct IPv4UdpAddress * udp_a4;
882 GNUNET_assert (plugin != NULL);
883 GNUNET_assert (address != NULL);
886 if ((address->address == NULL) ||
887 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
888 (address->address_length != sizeof (struct IPv6UdpAddress))))
894 if (address->address_length == sizeof (struct IPv4UdpAddress))
896 if (plugin->sockv4 == NULL)
898 udp_a4 = (struct IPv4UdpAddress *) address->address;
899 if (udp_a4->u4_port == 0)
903 if (address->address_length == sizeof (struct IPv6UdpAddress))
905 if (plugin->sockv6 == NULL)
907 udp_a6 = (struct IPv6UdpAddress *) address->address;
908 if (udp_a6->u6_port == 0)
912 /* check if session already exists */
913 struct SessionCompareContext cctx;
917 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));
919 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
920 if (cctx.res != NULL)
923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
928 /* otherwise create new */
929 s = create_session (plugin,
932 address->address_length,
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "Creating new session %p for peer `%s' address `%s'\n",
938 GNUNET_i2s(&address->peer),
939 udp_address_to_string(NULL,address->address,address->address_length));
941 GNUNET_assert (GNUNET_OK ==
942 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
943 &s->target.hashPubKey,
945 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
947 GNUNET_STATISTICS_set(plugin->env->stats,
948 "# UDP sessions active",
949 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
957 enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
960 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
961 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
962 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
963 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
967 * Function that is called with messages created by the fragmentation
968 * module. In the case of the 'proc' callback of the
969 * GNUNET_FRAGMENT_context_create function, this function must
970 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
972 * @param cls closure, the 'struct FragmentationContext'
973 * @param msg the message that was created
976 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
978 struct FragmentationContext *frag_ctx = cls;
979 struct Plugin *plugin = frag_ctx->plugin;
980 struct UDPMessageWrapper * udpw;
983 size_t msg_len = ntohs (msg->size);
986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
989 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
990 udpw->session = frag_ctx->session;
992 udpw->udp = (char *) &udpw[1];
994 udpw->msg_size = msg_len;
995 udpw->cont = frag_ctx->cont;
996 udpw->cont_cls = frag_ctx->cont_cls;
997 udpw->timeout = frag_ctx->timeout;
998 udpw->frag_ctx = frag_ctx;
999 memcpy (udpw->udp, msg, msg_len);
1001 enqueue (plugin, udpw);
1004 if (s->addrlen == sizeof (struct sockaddr_in))
1006 if (plugin->with_v4_ws == GNUNET_NO)
1008 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1009 GNUNET_SCHEDULER_cancel(plugin->select_task);
1011 plugin->select_task =
1012 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1013 GNUNET_TIME_UNIT_FOREVER_REL,
1016 &udp_plugin_select, plugin);
1017 plugin->with_v4_ws = GNUNET_YES;
1021 else if (s->addrlen == sizeof (struct sockaddr_in6))
1023 if (plugin->with_v6_ws == GNUNET_NO)
1025 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1026 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1028 plugin->select_task_v6 =
1029 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1030 GNUNET_TIME_UNIT_FOREVER_REL,
1033 &udp_plugin_select_v6, plugin);
1034 plugin->with_v6_ws = GNUNET_YES;
1041 * Function that can be used by the transport service to transmit
1042 * a message using the plugin. Note that in the case of a
1043 * peer disconnecting, the continuation MUST be called
1044 * prior to the disconnect notification itself. This function
1045 * will be called with this peer's HELLO message to initiate
1046 * a fresh connection to another peer.
1048 * @param cls closure
1049 * @param s which session must be used
1050 * @param msgbuf the message to transmit
1051 * @param msgbuf_size number of bytes in 'msgbuf'
1052 * @param priority how important is the message (most plugins will
1053 * ignore message priority and just FIFO)
1054 * @param to how long to wait at most for the transmission (does not
1055 * require plugins to discard the message after the timeout,
1056 * just advisory for the desired delay; most plugins will ignore
1058 * @param cont continuation to call once the message has
1059 * been transmitted (or if the transport is ready
1060 * for the next transmission call; or if the
1061 * peer disconnected...); can be NULL
1062 * @param cont_cls closure for cont
1063 * @return number of bytes used (on the physical network, with overheads);
1064 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1065 * and does NOT mean that the message was not transmitted (DV)
1068 udp_plugin_send (void *cls,
1070 const char *msgbuf, size_t msgbuf_size,
1071 unsigned int priority,
1072 struct GNUNET_TIME_Relative to,
1073 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1075 struct Plugin *plugin = cls;
1076 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
1078 struct UDPMessageWrapper * udpw;
1079 struct UDPMessage *udp;
1081 GNUNET_assert (plugin != NULL);
1082 GNUNET_assert (s != NULL);
1084 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1085 return GNUNET_SYSERR;
1087 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1088 return GNUNET_SYSERR;
1091 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1094 return GNUNET_SYSERR;
1097 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1100 return GNUNET_SYSERR;
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1104 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1106 GNUNET_i2s (&s->target),
1107 GNUNET_a2s(s->sock_addr, s->addrlen));
1110 udp = (struct UDPMessage *) mbuf;
1111 udp->header.size = htons (mlen);
1112 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1113 udp->reserved = htonl (0);
1114 udp->sender = *plugin->env->my_identity;
1116 if (mlen <= UDP_MTU)
1118 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
1120 udpw->udp = (char *) &udpw[1];
1121 udpw->msg_size = mlen;
1122 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1124 udpw->cont_cls = cont_cls;
1125 udpw->frag_ctx = NULL;
1126 if (NULL != udpw->cont)
1130 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
1131 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1133 enqueue (plugin, udpw);
1137 LOG (GNUNET_ERROR_TYPE_DEBUG,
1138 "UDP has to fragment message \n");
1139 if (s->frag_ctx != NULL)
1140 return GNUNET_SYSERR;
1141 memcpy (&udp[1], msgbuf, msgbuf_size);
1142 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
1144 frag_ctx->plugin = plugin;
1145 frag_ctx->session = s;
1146 frag_ctx->cont = cont;
1147 frag_ctx->cont_cls = cont_cls;
1148 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1149 frag_ctx->bytes_to_send = mlen;
1150 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1153 s->last_expected_delay,
1158 if (NULL != frag_ctx->cont)
1160 s->frag_ctx = frag_ctx;
1163 if (s->addrlen == sizeof (struct sockaddr_in))
1165 if (plugin->with_v4_ws == GNUNET_NO)
1167 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1168 GNUNET_SCHEDULER_cancel(plugin->select_task);
1170 plugin->select_task =
1171 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1172 GNUNET_TIME_UNIT_FOREVER_REL,
1175 &udp_plugin_select, plugin);
1176 plugin->with_v4_ws = GNUNET_YES;
1180 else if (s->addrlen == sizeof (struct sockaddr_in6))
1182 if (plugin->with_v6_ws == GNUNET_NO)
1184 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1185 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1187 plugin->select_task_v6 =
1188 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1189 GNUNET_TIME_UNIT_FOREVER_REL,
1192 &udp_plugin_select_v6, plugin);
1193 plugin->with_v6_ws = GNUNET_YES;
1202 * Our external IP address/port mapping has changed.
1204 * @param cls closure, the 'struct LocalAddrList'
1205 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1206 * the previous (now invalid) one
1207 * @param addr either the previous or the new public IP address
1208 * @param addrlen actual lenght of the address
1211 udp_nat_port_map_callback (void *cls, int add_remove,
1212 const struct sockaddr *addr, socklen_t addrlen)
1214 struct Plugin *plugin = cls;
1215 struct IPv4UdpAddress u4;
1216 struct IPv6UdpAddress u6;
1220 /* convert 'addr' to our internal format */
1221 switch (addr->sa_family)
1224 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1225 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1226 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1231 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1232 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1233 sizeof (struct in6_addr));
1234 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1242 /* modify our published address list */
1243 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1249 * Message tokenizer has broken up an incomming message. Pass it on
1252 * @param cls the 'struct Plugin'
1253 * @param client the 'struct SourceInformation'
1254 * @param hdr the actual message
1257 process_inbound_tokenized_messages (void *cls, void *client,
1258 const struct GNUNET_MessageHeader *hdr)
1260 struct Plugin *plugin = cls;
1261 struct SourceInformation *si = client;
1262 struct GNUNET_ATS_Information ats[2];
1263 struct GNUNET_TIME_Relative delay;
1265 GNUNET_assert (si->session != NULL);
1266 if (GNUNET_YES == si->session->in_destroy)
1269 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1270 ats[0].value = htonl (1);
1271 ats[1] = si->session->ats;
1272 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1274 delay = plugin->env->receive (plugin->env->cls,
1277 (const struct GNUNET_ATS_Information *) &ats, 2,
1281 si->session->flow_delay_for_other_peer = delay;
1286 * We've received a UDP Message. Process it (pass contents to main service).
1288 * @param plugin plugin context
1289 * @param msg the message
1290 * @param sender_addr sender address
1291 * @param sender_addr_len number of bytes in sender_addr
1294 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1295 const struct sockaddr *sender_addr,
1296 socklen_t sender_addr_len)
1298 struct SourceInformation si;
1300 struct IPv4UdpAddress u4;
1301 struct IPv6UdpAddress u6;
1305 if (0 != ntohl (msg->reserved))
1307 GNUNET_break_op (0);
1310 if (ntohs (msg->header.size) <
1311 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1313 GNUNET_break_op (0);
1317 /* convert address */
1318 switch (sender_addr->sa_family)
1321 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1322 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1323 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1328 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1329 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1330 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1338 LOG (GNUNET_ERROR_TYPE_DEBUG,
1339 "Received message with %u bytes from peer `%s' at `%s'\n",
1340 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1341 GNUNET_a2s (sender_addr, sender_addr_len));
1343 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1344 s = udp_plugin_get_session(plugin, address);
1345 GNUNET_free (address);
1347 /* iterate over all embedded messages */
1349 si.sender = msg->sender;
1353 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1354 ntohs (msg->header.size) -
1355 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1357 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1363 * Scan the heap for a receive context with the given address.
1365 * @param cls the 'struct FindReceiveContext'
1366 * @param node internal node of the heap
1367 * @param element value stored at the node (a 'struct ReceiveContext')
1368 * @param cost cost associated with the node
1369 * @return GNUNET_YES if we should continue to iterate,
1373 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1374 void *element, GNUNET_CONTAINER_HeapCostType cost)
1376 struct FindReceiveContext *frc = cls;
1377 struct DefragContext *e = element;
1379 if ((frc->addr_len == e->addr_len) &&
1380 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1390 * Process a defragmented message.
1392 * @param cls the 'struct ReceiveContext'
1393 * @param msg the message
1396 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1398 struct DefragContext *rc = cls;
1400 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1405 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1410 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1411 rc->src_addr, rc->addr_len);
1414 struct LookupContext
1416 const struct sockaddr * addr;
1419 struct Session *res;
1423 lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1425 struct LookupContext *l_ctx = cls;
1426 struct Session * s = value;
1428 if ((s->addrlen == l_ctx->addrlen) &&
1429 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1438 * Transmit an acknowledgement.
1440 * @param cls the 'struct ReceiveContext'
1441 * @param id message ID (unused)
1442 * @param msg ack to transmit
1445 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1447 struct DefragContext *rc = cls;
1449 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1450 struct UDP_ACK_Message *udp_ack;
1452 struct UDPMessageWrapper *udpw;
1455 struct LookupContext l_ctx;
1456 l_ctx.addr = rc->src_addr;
1457 l_ctx.addrlen = rc->addr_len;
1459 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1460 &lookup_session_by_addr_it,
1467 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1468 delay = s->flow_delay_for_other_peer.rel_value;
1470 LOG (GNUNET_ERROR_TYPE_DEBUG,
1471 "Sending ACK to `%s' including delay of %u ms\n",
1472 GNUNET_a2s (rc->src_addr,
1473 (rc->src_addr->sa_family ==
1474 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1477 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1479 udpw->cont_cls = NULL;
1480 udpw->frag_ctx = NULL;
1481 udpw->msg_size = msize;
1483 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1484 udpw->udp = (char *)&udpw[1];
1486 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1487 udp_ack->header.size = htons ((uint16_t) msize);
1488 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1489 udp_ack->delay = htonl (delay);
1490 udp_ack->sender = *rc->plugin->env->my_identity;
1491 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1493 enqueue (rc->plugin, udpw);
1497 static void read_process_msg (struct Plugin *plugin,
1498 const struct GNUNET_MessageHeader *msg,
1502 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1504 GNUNET_break_op (0);
1507 process_udp_message (plugin, (const struct UDPMessage *) msg,
1508 (const struct sockaddr *) addr, fromlen);
1512 static void read_process_ack (struct Plugin *plugin,
1513 const struct GNUNET_MessageHeader *msg,
1517 const struct GNUNET_MessageHeader *ack;
1518 const struct UDP_ACK_Message *udp_ack;
1519 struct LookupContext l_ctx;
1520 struct Session *s = NULL;
1521 struct GNUNET_TIME_Relative flow_delay;
1523 if (ntohs (msg->size) <
1524 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1526 GNUNET_break_op (0);
1530 udp_ack = (const struct UDP_ACK_Message *) msg;
1532 l_ctx.addr = (const struct sockaddr *) addr;
1533 l_ctx.addrlen = fromlen;
1535 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1536 &lookup_session_by_addr_it,
1540 if ((s == NULL) || (s->frag_ctx == NULL))
1543 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1544 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1545 flow_delay.rel_value);
1546 s->flow_delay_from_other_peer =
1547 GNUNET_TIME_relative_to_absolute (flow_delay);
1549 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1550 if (ntohs (ack->size) !=
1551 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1553 GNUNET_break_op (0);
1557 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1559 LOG (GNUNET_ERROR_TYPE_DEBUG,
1560 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1561 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1562 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1566 LOG (GNUNET_ERROR_TYPE_DEBUG,
1567 "FULL MESSAGE ACKed\n",
1568 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1569 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1570 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1572 struct UDPMessageWrapper * udpw = NULL;
1573 struct UDPMessageWrapper * tmp = NULL;
1574 if (s->addrlen == sizeof (struct sockaddr_in6))
1576 udpw = plugin->ipv6_queue_head;
1580 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1582 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1588 if (s->addrlen == sizeof (struct sockaddr_in))
1590 udpw = plugin->ipv4_queue_head;
1594 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1596 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1603 if (s->frag_ctx->cont != NULL)
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1606 "Calling continuation for fragmented message to `%s' with result %s\n",
1607 GNUNET_i2s (&s->target), "OK");
1608 s->frag_ctx->cont (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1612 GNUNET_free (s->frag_ctx);
1617 static void read_process_fragment (struct Plugin *plugin,
1618 const struct GNUNET_MessageHeader *msg,
1622 struct DefragContext *d_ctx;
1623 struct GNUNET_TIME_Absolute now;
1624 struct FindReceiveContext frc;
1628 frc.addr = (const struct sockaddr *) addr;
1629 frc.addr_len = fromlen;
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1632 (unsigned int) ntohs (msg->size),
1633 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1634 /* Lookup existing receive context for this address */
1635 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1636 &find_receive_context,
1638 now = GNUNET_TIME_absolute_get ();
1643 /* Create a new defragmentation context */
1644 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1645 memcpy (&d_ctx[1], addr, fromlen);
1646 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1647 d_ctx->addr_len = fromlen;
1648 d_ctx->plugin = plugin;
1650 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1651 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1652 &fragment_msg_proc, &ack_proc);
1654 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1655 (GNUNET_CONTAINER_HeapCostType)
1657 LOG (GNUNET_ERROR_TYPE_DEBUG, "Created new defragmentation context for %u-byte fragment from `%s'\n",
1658 (unsigned int) ntohs (msg->size),
1659 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1663 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1664 (unsigned int) ntohs (msg->size),
1665 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1668 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1670 /* keep this 'rc' from expiring */
1671 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1672 (GNUNET_CONTAINER_HeapCostType)
1675 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1676 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1678 /* remove 'rc' that was inactive the longest */
1679 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1680 GNUNET_assert (NULL != d_ctx);
1681 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1682 GNUNET_free (d_ctx);
1687 * Read and process a message from the given socket.
1689 * @param plugin the overall plugin
1690 * @param rsock socket to read from
1693 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1697 char buf[65536] GNUNET_ALIGN;
1699 const struct GNUNET_MessageHeader *msg;
1701 fromlen = sizeof (addr);
1702 memset (&addr, 0, sizeof (addr));
1703 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1704 (struct sockaddr *) &addr, &fromlen);
1706 if (size < sizeof (struct GNUNET_MessageHeader))
1708 GNUNET_break_op (0);
1711 msg = (const struct GNUNET_MessageHeader *) buf;
1713 LOG (GNUNET_ERROR_TYPE_DEBUG,
1714 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1715 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1717 if (size != ntohs (msg->size))
1719 GNUNET_break_op (0);
1723 switch (ntohs (msg->type))
1725 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1726 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1729 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1730 read_process_msg (plugin, msg, addr, fromlen);
1733 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1734 read_process_ack (plugin, msg, addr, fromlen);
1737 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1738 read_process_fragment (plugin, msg, addr, fromlen);
1742 GNUNET_break_op (0);
1749 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
1753 struct GNUNET_TIME_Absolute max;
1754 struct GNUNET_TIME_Absolute ;
1756 struct UDPMessageWrapper *udpw = NULL;
1758 if (sock == plugin->sockv4)
1760 udpw = plugin->ipv4_queue_head;
1762 else if (sock == plugin->sockv6)
1764 udpw = plugin->ipv6_queue_head;
1772 const struct sockaddr * sa = udpw->session->sock_addr;
1773 slen = udpw->session->addrlen;
1775 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1777 while (udpw != NULL)
1779 if (max.abs_value != udpw->timeout.abs_value)
1781 /* Message timed out */
1782 call_continuation(udpw, GNUNET_SYSERR);
1783 if (udpw->frag_ctx != NULL)
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fragmented message for peer `%s' with size %u timed out\n",
1786 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1787 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1788 GNUNET_free (udpw->frag_ctx);
1789 udpw->session->frag_ctx = NULL;
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' with size %u timed out\n",
1794 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1797 if (sock == plugin->sockv4)
1799 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1801 udpw = plugin->ipv4_queue_head;
1803 else if (sock == plugin->sockv6)
1805 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1807 udpw = plugin->ipv6_queue_head;
1812 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1813 if (delta.rel_value == 0)
1815 /* this message is not delayed */
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is not delayed \n",
1817 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1822 /* this message is delayed, try next */
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1824 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1833 /* No message left */
1837 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
1839 if (GNUNET_SYSERR == sent)
1841 LOG (GNUNET_ERROR_TYPE_ERROR,
1842 "UDP could not transmit %u-byte message to `%s': `%s'\n",
1843 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
1845 call_continuation(udpw, GNUNET_SYSERR);
1849 LOG (GNUNET_ERROR_TYPE_DEBUG,
1850 "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
1851 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1852 (sent < 0) ? STRERROR (errno) : "ok");
1854 /* This was just a message fragment */
1855 if (udpw->frag_ctx != NULL)
1857 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1859 /* This was a complete message*/
1861 call_continuation(udpw, GNUNET_OK);
1863 if (sock == plugin->sockv4)
1864 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1865 else if (sock == plugin->sockv6)
1866 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1874 * We have been notified that our readset has something to read. We don't
1875 * know which socket needs to be read, so we have to check each one
1876 * Then reschedule this function to be called again once more is available.
1878 * @param cls the plugin handle
1879 * @param tc the scheduling context (for rescheduling this function again)
1882 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1884 struct Plugin *plugin = cls;
1886 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1887 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1889 plugin->with_v4_ws = GNUNET_NO;
1891 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1893 if ((NULL != plugin->sockv4) &&
1894 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1895 udp_select_read (plugin, plugin->sockv4);
1899 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1901 if ((NULL != plugin->sockv4) && (plugin->ipv4_queue_head != NULL) &&
1902 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
1904 udp_select_send (plugin, plugin->sockv4);
1908 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1909 GNUNET_SCHEDULER_cancel (plugin->select_task);
1910 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1911 GNUNET_TIME_UNIT_FOREVER_REL,
1913 (plugin->ipv4_queue_head != NULL) ? plugin->ws_v4 : NULL,
1914 &udp_plugin_select, plugin);
1915 if (plugin->ipv4_queue_head != NULL)
1916 plugin->with_v4_ws = GNUNET_YES;
1918 plugin->with_v4_ws = GNUNET_NO;
1923 * We have been notified that our readset has something to read. We don't
1924 * know which socket needs to be read, so we have to check each one
1925 * Then reschedule this function to be called again once more is available.
1927 * @param cls the plugin handle
1928 * @param tc the scheduling context (for rescheduling this function again)
1931 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1933 struct Plugin *plugin = cls;
1935 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1936 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1939 plugin->with_v6_ws = GNUNET_NO;
1940 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1942 if ((NULL != plugin->sockv6) &&
1943 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1944 udp_select_read (plugin, plugin->sockv6);
1947 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1949 if ((NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
1950 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)))
1952 udp_select_send (plugin, plugin->sockv6);
1955 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1956 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
1957 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1958 GNUNET_TIME_UNIT_FOREVER_REL,
1960 (plugin->ipv6_queue_head != NULL) ? plugin->ws_v6 : NULL,
1961 &udp_plugin_select_v6, plugin);
1962 if (plugin->ipv6_queue_head != NULL)
1963 plugin->with_v6_ws = GNUNET_YES;
1965 plugin->with_v6_ws = GNUNET_NO;
1970 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1973 int sockets_created = 0;
1974 struct sockaddr *serverAddr;
1975 struct sockaddr *addrs[2];
1976 socklen_t addrlens[2];
1979 /* Create IPv6 socket */
1980 if (plugin->enable_ipv6 == GNUNET_YES)
1982 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1983 if (NULL == plugin->sockv6)
1985 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
1986 plugin->enable_ipv6 = GNUNET_NO;
1990 #if HAVE_SOCKADDR_IN_SIN_LEN
1991 serverAddrv6->sin6_len = sizeof (serverAddrv6);
1993 serverAddrv6->sin6_family = AF_INET6;
1994 serverAddrv6->sin6_addr = in6addr_any;
1995 serverAddrv6->sin6_port = htons (plugin->port);
1996 addrlen = sizeof (struct sockaddr_in6);
1997 serverAddr = (struct sockaddr *) serverAddrv6;
1998 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1999 ntohs (serverAddrv6->sin6_port));
2001 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2004 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2005 LOG (GNUNET_ERROR_TYPE_DEBUG,
2006 "IPv6 Binding failed, trying new port %d\n",
2007 ntohs (serverAddrv6->sin6_port));
2011 GNUNET_NETWORK_socket_close (plugin->sockv6);
2012 plugin->sockv6 = NULL;
2016 if (plugin->sockv6 != NULL)
2018 LOG (GNUNET_ERROR_TYPE_DEBUG,
2019 "IPv6 socket created on port %d\n",
2020 ntohs (serverAddrv6->sin6_port));
2021 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2022 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2028 /* Create IPv4 socket */
2029 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2030 if (NULL == plugin->sockv4)
2032 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2036 #if HAVE_SOCKADDR_IN_SIN_LEN
2037 serverAddrv4->sin_len = sizeof (serverAddrv4);
2039 serverAddrv4->sin_family = AF_INET;
2040 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2041 serverAddrv4->sin_port = htons (plugin->port);
2042 addrlen = sizeof (struct sockaddr_in);
2043 serverAddr = (struct sockaddr *) serverAddrv4;
2045 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2046 ntohs (serverAddrv4->sin_port));
2048 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2051 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2052 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2053 ntohs (serverAddrv4->sin_port));
2057 GNUNET_NETWORK_socket_close (plugin->sockv4);
2058 plugin->sockv4 = NULL;
2062 if (plugin->sockv4 != NULL)
2064 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2065 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2070 /* Create file descriptors */
2071 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2072 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2073 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2074 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2075 if (NULL != plugin->sockv4)
2077 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2078 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2081 if (sockets_created == 0)
2082 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2084 plugin->select_task =
2085 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2086 GNUNET_TIME_UNIT_FOREVER_REL,
2089 &udp_plugin_select, plugin);
2090 plugin->with_v4_ws = GNUNET_NO;
2092 if (plugin->enable_ipv6 == GNUNET_YES)
2094 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2095 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2096 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2097 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2098 if (NULL != plugin->sockv6)
2100 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2101 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2104 plugin->select_task_v6 =
2105 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2106 GNUNET_TIME_UNIT_FOREVER_REL,
2109 &udp_plugin_select_v6, plugin);
2110 plugin->with_v6_ws = GNUNET_NO;
2113 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2114 GNUNET_NO, plugin->port,
2116 (const struct sockaddr **) addrs, addrlens,
2117 &udp_nat_port_map_callback, NULL, plugin);
2119 return sockets_created;
2124 * The exported method. Makes the core api available via a global and
2125 * returns the udp transport API.
2127 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2128 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2131 libgnunet_plugin_transport_udp_init (void *cls)
2133 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2134 struct GNUNET_TRANSPORT_PluginFunctions *api;
2135 struct Plugin *plugin;
2137 unsigned long long port;
2138 unsigned long long aport;
2139 unsigned long long broadcast;
2140 unsigned long long udp_max_bps;
2141 unsigned long long enable_v6;
2142 char * bind4_address;
2143 char * bind6_address;
2144 struct GNUNET_TIME_Relative interval;
2146 struct sockaddr_in serverAddrv4;
2147 struct sockaddr_in6 serverAddrv6;
2151 if (NULL == env->receive)
2153 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2154 initialze the plugin or the API */
2155 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2157 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2158 api->address_to_string = &udp_address_to_string;
2159 api->string_to_address = &udp_string_to_address;
2163 GNUNET_assert( NULL != env->stats);
2165 /* Get port number */
2167 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2171 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2172 "ADVERTISED_PORT", &aport))
2176 LOG (GNUNET_ERROR_TYPE_WARNING,
2177 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2184 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2187 enable_v6 = GNUNET_NO;
2190 enable_v6 = GNUNET_YES;
2194 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2195 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2198 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2199 "BINDTO", &bind4_address))
2201 LOG (GNUNET_ERROR_TYPE_DEBUG,
2202 "Binding udp plugin to specific address: `%s'\n",
2204 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2206 GNUNET_free (bind4_address);
2212 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2213 "BINDTO6", &bind6_address))
2215 LOG (GNUNET_ERROR_TYPE_DEBUG,
2216 "Binding udp plugin to specific address: `%s'\n",
2219 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2221 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2223 GNUNET_free_non_null (bind4_address);
2224 GNUNET_free (bind6_address);
2230 /* Enable neighbour discovery */
2231 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2233 if (broadcast == GNUNET_SYSERR)
2234 broadcast = GNUNET_NO;
2236 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2237 "BROADCAST_INTERVAL", &interval))
2239 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2242 /* Maximum datarate */
2243 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2244 "MAX_BPS", &udp_max_bps))
2246 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2249 plugin = GNUNET_malloc (sizeof (struct Plugin));
2250 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2252 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2253 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2256 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2257 plugin->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2258 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2259 plugin->port = port;
2260 plugin->aport = aport;
2261 plugin->broadcast_interval = interval;
2262 plugin->enable_ipv6 = enable_v6;
2267 api->disconnect = &udp_disconnect;
2268 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2269 api->address_to_string = &udp_address_to_string;
2270 api->string_to_address = &udp_string_to_address;
2271 api->check_address = &udp_plugin_check_address;
2272 api->get_session = &udp_plugin_get_session;
2273 api->send = &udp_plugin_send;
2275 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2276 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
2277 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
2279 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2280 GNUNET_free (plugin);
2285 if (broadcast == GNUNET_YES)
2287 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2288 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
2291 GNUNET_free_non_null (bind4_address);
2292 GNUNET_free_non_null (bind6_address);
2298 heap_cleanup_iterator (void *cls,
2299 struct GNUNET_CONTAINER_HeapNode *
2300 node, void *element,
2301 GNUNET_CONTAINER_HeapCostType
2304 struct DefragContext * d_ctx = element;
2306 GNUNET_CONTAINER_heap_remove_node (node);
2307 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2308 GNUNET_free (d_ctx);
2315 * The exported method. Makes the core api available via a global and
2316 * returns the udp transport API.
2318 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2322 libgnunet_plugin_transport_udp_done (void *cls)
2324 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2325 struct Plugin *plugin = api->cls;
2333 stop_broadcast (plugin);
2335 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2337 GNUNET_SCHEDULER_cancel (plugin->select_task);
2338 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2340 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2342 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2343 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2346 /* Closing sockets */
2347 if (plugin->sockv4 != NULL)
2349 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2350 plugin->sockv4 = NULL;
2352 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2353 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2355 if (plugin->sockv6 != NULL)
2357 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2358 plugin->sockv6 = NULL;
2360 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2361 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2364 GNUNET_NAT_unregister (plugin->nat);
2366 if (plugin->defrag_ctxs != NULL)
2368 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2369 heap_cleanup_iterator, NULL);
2370 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2371 plugin->defrag_ctxs = NULL;
2373 if (plugin->mst != NULL)
2375 GNUNET_SERVER_mst_destroy(plugin->mst);
2379 /* Clean up leftover messages */
2380 struct UDPMessageWrapper * udpw;
2381 udpw = plugin->ipv4_queue_head;
2382 while (udpw != NULL)
2384 struct UDPMessageWrapper *tmp = udpw->next;
2385 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
2386 call_continuation(udpw, GNUNET_SYSERR);
2390 udpw = plugin->ipv6_queue_head;
2391 while (udpw != NULL)
2393 struct UDPMessageWrapper *tmp = udpw->next;
2394 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2395 call_continuation(udpw, GNUNET_SYSERR);
2400 /* Clean up sessions */
2401 LOG (GNUNET_ERROR_TYPE_DEBUG,
2402 "Cleaning up sessions\n");
2403 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2404 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2407 GNUNET_free (plugin);
2409 GNUNET_assert (0 == cont_calls);
2414 /* end of plugin_transport_udp.c */