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;
298 * We have been notified that our readset has something to read. We don't
299 * know which socket needs to be read, so we have to check each one
300 * Then reschedule this function to be called again once more is available.
302 * @param cls the plugin handle
303 * @param tc the scheduling context (for rescheduling this function again)
306 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
309 * We have been notified that our readset has something to read. We don't
310 * know which socket needs to be read, so we have to check each one
311 * Then reschedule this function to be called again once more is available.
313 * @param cls the plugin handle
314 * @param tc the scheduling context (for rescheduling this function again)
317 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
320 * Function called for a quick conversion of the binary address to
321 * a numeric address. Note that the caller must not free the
322 * address and that the next call to this function is allowed
323 * to override the address again.
326 * @param addr binary address
327 * @param addrlen length of the address
328 * @return string representing the same address
331 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
333 static char rbuf[INET6_ADDRSTRLEN + 10];
334 char buf[INET6_ADDRSTRLEN];
338 const struct IPv4UdpAddress *t4;
339 const struct IPv6UdpAddress *t6;
343 if (addrlen == sizeof (struct IPv6UdpAddress))
347 port = ntohs (t6->u6_port);
348 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
351 else if (addrlen == sizeof (struct IPv4UdpAddress))
355 port = ntohs (t4->u4_port);
356 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
364 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
365 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
372 * Function called to convert a string address to
375 * @param cls closure ('struct Plugin*')
376 * @param addr string address
377 * @param addrlen length of the address
378 * @param buf location to store the buffer
379 * @param added location to store the number of bytes in the buffer.
380 * If the function returns GNUNET_SYSERR, its contents are undefined.
381 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
384 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
385 void **buf, size_t *added)
387 struct sockaddr_storage socket_address;
389 if ((NULL == addr) || (addrlen == 0))
392 return GNUNET_SYSERR;
395 if ('\0' != addr[addrlen - 1])
397 return GNUNET_SYSERR;
400 if (strlen (addr) != addrlen - 1)
402 return GNUNET_SYSERR;
405 int ret = GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
408 if (ret != GNUNET_OK)
410 return GNUNET_SYSERR;
413 if (socket_address.ss_family == AF_INET)
415 struct IPv4UdpAddress *u4;
416 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
417 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
418 u4->ipv4_addr = in4->sin_addr.s_addr;
419 u4->u4_port = in4->sin_port;
421 *added = sizeof (struct IPv4UdpAddress);
424 else if (socket_address.ss_family == AF_INET6)
426 struct IPv6UdpAddress *u6;
427 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
428 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
429 u6->ipv6_addr = in6->sin6_addr;
430 u6->u6_port = in6->sin6_port;
432 *added = sizeof (struct IPv6UdpAddress);
435 return GNUNET_SYSERR;
440 * Append our port and forward the result.
442 * @param cls a 'struct PrettyPrinterContext'
443 * @param hostname result from DNS resolver
446 append_port (void *cls, const char *hostname)
448 struct PrettyPrinterContext *ppc = cls;
451 if (hostname == NULL)
453 ppc->asc (ppc->asc_cls, NULL);
457 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
458 ppc->asc (ppc->asc_cls, ret);
464 * Convert the transports address to a nice, human-readable
468 * @param type name of the transport that generated the address
469 * @param addr one of the addresses of the host, NULL for the last address
470 * the specific address format depends on the transport
471 * @param addrlen length of the address
472 * @param numeric should (IP) addresses be displayed in numeric form?
473 * @param timeout after how long should we give up?
474 * @param asc function to call on each string
475 * @param asc_cls closure for asc
478 udp_plugin_address_pretty_printer (void *cls, const char *type,
479 const void *addr, size_t addrlen,
481 struct GNUNET_TIME_Relative timeout,
482 GNUNET_TRANSPORT_AddressStringCallback asc,
485 struct PrettyPrinterContext *ppc;
488 struct sockaddr_in a4;
489 struct sockaddr_in6 a6;
490 const struct IPv4UdpAddress *u4;
491 const struct IPv6UdpAddress *u6;
494 if (addrlen == sizeof (struct IPv6UdpAddress))
497 memset (&a6, 0, sizeof (a6));
498 a6.sin6_family = AF_INET6;
499 #if HAVE_SOCKADDR_IN_SIN_LEN
500 a6.sin6_len = sizeof (a6);
502 a6.sin6_port = u6->u6_port;
503 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
504 port = ntohs (u6->u6_port);
508 else if (addrlen == sizeof (struct IPv4UdpAddress))
511 memset (&a4, 0, sizeof (a4));
512 a4.sin_family = AF_INET;
513 #if HAVE_SOCKADDR_IN_SIN_LEN
514 a4.sin_len = sizeof (a4);
516 a4.sin_port = u4->u4_port;
517 a4.sin_addr.s_addr = u4->ipv4_addr;
518 port = ntohs (u4->u4_port);
522 else if (0 == addrlen)
524 asc (asc_cls, "<inbound connection>");
530 /* invalid address */
535 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
537 ppc->asc_cls = asc_cls;
539 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
543 call_continuation (struct UDPMessageWrapper *udpw, int result)
545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
546 "Calling continuation for %u byte message to `%s' with result %s\n",
547 udpw->msg_size, GNUNET_i2s (&udpw->session->target),
548 (GNUNET_OK == result) ? "OK" : "SYSERR");
549 if (NULL != udpw->cont)
551 udpw->cont (udpw->cont_cls, &udpw->session->target,result);
557 * Check if the given port is plausible (must be either our listen
558 * port or our advertised port). If it is neither, we return
561 * @param plugin global variables
562 * @param in_port port number to check
563 * @return GNUNET_OK if port is either open_port or adv_port
566 check_port (struct Plugin *plugin, uint16_t in_port)
568 if ((in_port == plugin->port) || (in_port == plugin->aport))
570 return GNUNET_SYSERR;
576 * Function that will be called to check if a binary address for this
577 * plugin is well-formed and corresponds to an address for THIS peer
578 * (as per our configuration). Naturally, if absolutely necessary,
579 * plugins can be a bit conservative in their answer, but in general
580 * plugins should make sure that the address does not redirect
581 * traffic to a 3rd party that might try to man-in-the-middle our
584 * @param cls closure, should be our handle to the Plugin
585 * @param addr pointer to the address
586 * @param addrlen length of addr
587 * @return GNUNET_OK if this is a plausible address for this peer
588 * and transport, GNUNET_SYSERR if not
592 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
594 struct Plugin *plugin = cls;
595 struct IPv4UdpAddress *v4;
596 struct IPv6UdpAddress *v6;
598 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
599 (addrlen != sizeof (struct IPv6UdpAddress)))
602 return GNUNET_SYSERR;
604 if (addrlen == sizeof (struct IPv4UdpAddress))
606 v4 = (struct IPv4UdpAddress *) addr;
607 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
608 return GNUNET_SYSERR;
610 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
611 sizeof (struct in_addr)))
612 return GNUNET_SYSERR;
616 v6 = (struct IPv6UdpAddress *) addr;
617 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
620 return GNUNET_SYSERR;
622 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
623 return GNUNET_SYSERR;
625 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
626 sizeof (struct in6_addr)))
627 return GNUNET_SYSERR;
634 * Task to free resources associated with a session.
636 * @param s session to free
639 free_session (struct Session *s)
641 if (s->frag_ctx != NULL)
643 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
644 GNUNET_free (s->frag_ctx);
652 * Destroy a session, plugin is being unloaded.
655 * @param key hash of public key of target peer
656 * @param value a 'struct PeerSession*' to clean up
657 * @return GNUNET_OK (continue to iterate)
660 disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
662 struct Plugin *plugin = cls;
663 struct Session *s = value;
664 struct UDPMessageWrapper *udpw;
665 struct UDPMessageWrapper *next;
667 GNUNET_assert (GNUNET_YES != s->in_destroy);
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Session %p to peer `%s' address ended \n",
671 GNUNET_i2s (&s->target),
672 GNUNET_a2s (s->sock_addr, s->addrlen));
673 next = plugin->ipv4_queue_head;
674 while (NULL != (udpw = next))
677 if (udpw->session == s)
679 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
680 call_continuation(udpw, GNUNET_SYSERR);
684 next = plugin->ipv6_queue_head;
685 while (NULL != (udpw = next))
688 if (udpw->session == s)
690 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
691 call_continuation(udpw, GNUNET_SYSERR);
696 plugin->env->session_end (plugin->env->cls, &s->target, s);
698 if (NULL != s->frag_ctx)
700 if (NULL != s->frag_ctx->cont)
702 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR);
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
705 GNUNET_i2s (&s->target));
709 GNUNET_assert (GNUNET_YES ==
710 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
711 &s->target.hashPubKey,
713 GNUNET_STATISTICS_set(plugin->env->stats,
714 "# UDP sessions active",
715 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
718 s->in_destroy = GNUNET_YES;
726 * Disconnect from a remote node. Clean up session if we have one for this peer
728 * @param cls closure for this call (should be handle to Plugin)
729 * @param target the peeridentity of the peer to disconnect
730 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
733 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
735 struct Plugin *plugin = cls;
736 GNUNET_assert (plugin != NULL);
738 GNUNET_assert (target != NULL);
739 LOG (GNUNET_ERROR_TYPE_DEBUG,
740 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
741 /* Clean up sessions */
742 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
745 static struct Session *
746 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
747 const void *addr, size_t addrlen,
748 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
751 const struct IPv4UdpAddress *t4;
752 const struct IPv6UdpAddress *t6;
753 struct sockaddr_in *v4;
754 struct sockaddr_in6 *v6;
759 case sizeof (struct IPv4UdpAddress):
760 if (NULL == plugin->sockv4)
765 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
766 len = sizeof (struct sockaddr_in);
767 v4 = (struct sockaddr_in *) &s[1];
768 v4->sin_family = AF_INET;
769 #if HAVE_SOCKADDR_IN_SIN_LEN
770 v4->sin_len = sizeof (struct sockaddr_in);
772 v4->sin_port = t4->u4_port;
773 v4->sin_addr.s_addr = t4->ipv4_addr;
774 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
776 case sizeof (struct IPv6UdpAddress):
777 if (NULL == plugin->sockv6)
783 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
784 len = sizeof (struct sockaddr_in6);
785 v6 = (struct sockaddr_in6 *) &s[1];
786 v6->sin6_family = AF_INET6;
787 #if HAVE_SOCKADDR_IN_SIN_LEN
788 v6->sin6_len = sizeof (struct sockaddr_in6);
790 v6->sin6_port = t6->u6_port;
791 v6->sin6_addr = t6->ipv6_addr;
792 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
795 /* Must have a valid address to send to */
802 s->sock_addr = (const struct sockaddr *) &s[1];
803 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
804 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
805 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
812 session_cmp_it (void *cls,
813 const GNUNET_HashCode * key,
816 struct SessionCompareContext * cctx = cls;
817 const struct GNUNET_HELLO_Address *address = cctx->addr;
818 struct Session *s = value;
820 socklen_t s_addrlen = s->addrlen;
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
824 udp_address_to_string (NULL, (void *) address->address, address->address_length),
825 GNUNET_a2s (s->sock_addr, s->addrlen));
828 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
829 (s_addrlen == sizeof (struct sockaddr_in)))
831 struct IPv4UdpAddress * u4 = NULL;
832 u4 = (struct IPv4UdpAddress *) address->address;
833 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
834 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
835 (u4->u4_port == s4->sin_port))
842 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
843 (s_addrlen == sizeof (struct sockaddr_in6)))
845 struct IPv6UdpAddress * u6 = NULL;
846 u6 = (struct IPv6UdpAddress *) address->address;
847 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
848 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
849 (u6->u6_port == s6->sin6_port))
862 * Creates a new outbound session the transport service will use to send data to the
865 * @param cls the plugin
866 * @param address the address
867 * @return the session or NULL of max connections exceeded
869 static struct Session *
870 udp_plugin_get_session (void *cls,
871 const struct GNUNET_HELLO_Address *address)
873 struct Session * s = NULL;
874 struct Plugin * plugin = cls;
875 struct IPv6UdpAddress * udp_a6;
876 struct IPv4UdpAddress * udp_a4;
878 GNUNET_assert (plugin != NULL);
879 GNUNET_assert (address != NULL);
882 if ((address->address == NULL) ||
883 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
884 (address->address_length != sizeof (struct IPv6UdpAddress))))
890 if (address->address_length == sizeof (struct IPv4UdpAddress))
892 if (plugin->sockv4 == NULL)
894 udp_a4 = (struct IPv4UdpAddress *) address->address;
895 if (udp_a4->u4_port == 0)
899 if (address->address_length == sizeof (struct IPv6UdpAddress))
901 if (plugin->sockv6 == NULL)
903 udp_a6 = (struct IPv6UdpAddress *) address->address;
904 if (udp_a6->u6_port == 0)
908 /* check if session already exists */
909 struct SessionCompareContext cctx;
913 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));
915 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
916 if (cctx.res != NULL)
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
924 /* otherwise create new */
925 s = create_session (plugin,
928 address->address_length,
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
932 "Creating new session %p for peer `%s' address `%s'\n",
934 GNUNET_i2s(&address->peer),
935 udp_address_to_string(NULL,address->address,address->address_length));
937 GNUNET_assert (GNUNET_OK ==
938 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
939 &s->target.hashPubKey,
941 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
943 GNUNET_STATISTICS_set(plugin->env->stats,
944 "# UDP sessions active",
945 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
953 enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
956 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
957 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
958 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
959 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
964 * Fragment message was transmitted via UDP, let fragmentation know
965 * to send the next fragment now.
967 * @param cls the 'struct UDPMessageWrapper' of the fragment
968 * @param target destination peer (ignored)
969 * @param result GNUNET_OK on success (ignored)
972 send_next_fragment (void *cls,
973 const struct GNUNET_PeerIdentity *target,
976 struct UDPMessageWrapper *udpw = cls;
978 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
983 * Function that is called with messages created by the fragmentation
984 * module. In the case of the 'proc' callback of the
985 * GNUNET_FRAGMENT_context_create function, this function must
986 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
988 * @param cls closure, the 'struct FragmentationContext'
989 * @param msg the message that was created
992 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
994 struct FragmentationContext *frag_ctx = cls;
995 struct Plugin *plugin = frag_ctx->plugin;
996 struct UDPMessageWrapper * udpw;
999 size_t msg_len = ntohs (msg->size);
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
1005 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
1006 udpw->session = frag_ctx->session;
1008 udpw->udp = (char *) &udpw[1];
1010 udpw->msg_size = msg_len;
1011 udpw->cont = &send_next_fragment;
1012 udpw->cont_cls = udpw;
1013 udpw->timeout = frag_ctx->timeout;
1014 udpw->frag_ctx = frag_ctx;
1015 memcpy (udpw->udp, msg, msg_len);
1017 enqueue (plugin, udpw);
1020 if (s->addrlen == sizeof (struct sockaddr_in))
1022 if (plugin->with_v4_ws == GNUNET_NO)
1024 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1025 GNUNET_SCHEDULER_cancel(plugin->select_task);
1027 plugin->select_task =
1028 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1029 GNUNET_TIME_UNIT_FOREVER_REL,
1032 &udp_plugin_select, plugin);
1033 plugin->with_v4_ws = GNUNET_YES;
1037 else if (s->addrlen == sizeof (struct sockaddr_in6))
1039 if (plugin->with_v6_ws == GNUNET_NO)
1041 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1042 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1044 plugin->select_task_v6 =
1045 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1046 GNUNET_TIME_UNIT_FOREVER_REL,
1049 &udp_plugin_select_v6, plugin);
1050 plugin->with_v6_ws = GNUNET_YES;
1057 * Function that can be used by the transport service to transmit
1058 * a message using the plugin. Note that in the case of a
1059 * peer disconnecting, the continuation MUST be called
1060 * prior to the disconnect notification itself. This function
1061 * will be called with this peer's HELLO message to initiate
1062 * a fresh connection to another peer.
1064 * @param cls closure
1065 * @param s which session must be used
1066 * @param msgbuf the message to transmit
1067 * @param msgbuf_size number of bytes in 'msgbuf'
1068 * @param priority how important is the message (most plugins will
1069 * ignore message priority and just FIFO)
1070 * @param to how long to wait at most for the transmission (does not
1071 * require plugins to discard the message after the timeout,
1072 * just advisory for the desired delay; most plugins will ignore
1074 * @param cont continuation to call once the message has
1075 * been transmitted (or if the transport is ready
1076 * for the next transmission call; or if the
1077 * peer disconnected...); can be NULL
1078 * @param cont_cls closure for cont
1079 * @return number of bytes used (on the physical network, with overheads);
1080 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1081 * and does NOT mean that the message was not transmitted (DV)
1084 udp_plugin_send (void *cls,
1086 const char *msgbuf, size_t msgbuf_size,
1087 unsigned int priority,
1088 struct GNUNET_TIME_Relative to,
1089 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1091 struct Plugin *plugin = cls;
1092 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
1094 struct UDPMessageWrapper * udpw;
1095 struct UDPMessage *udp;
1097 GNUNET_assert (plugin != NULL);
1098 GNUNET_assert (s != NULL);
1100 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1101 return GNUNET_SYSERR;
1103 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1104 return GNUNET_SYSERR;
1107 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1110 return GNUNET_SYSERR;
1113 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1116 return GNUNET_SYSERR;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1122 GNUNET_i2s (&s->target),
1123 GNUNET_a2s(s->sock_addr, s->addrlen));
1126 udp = (struct UDPMessage *) mbuf;
1127 udp->header.size = htons (mlen);
1128 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1129 udp->reserved = htonl (0);
1130 udp->sender = *plugin->env->my_identity;
1132 if (mlen <= UDP_MTU)
1134 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
1136 udpw->udp = (char *) &udpw[1];
1137 udpw->msg_size = mlen;
1138 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1140 udpw->cont_cls = cont_cls;
1141 udpw->frag_ctx = NULL;
1142 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
1143 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1145 enqueue (plugin, udpw);
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1150 "UDP has to fragment message \n");
1151 if (s->frag_ctx != NULL)
1152 return GNUNET_SYSERR;
1153 memcpy (&udp[1], msgbuf, msgbuf_size);
1154 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
1156 frag_ctx->plugin = plugin;
1157 frag_ctx->session = s;
1158 frag_ctx->cont = cont;
1159 frag_ctx->cont_cls = cont_cls;
1160 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1161 frag_ctx->bytes_to_send = mlen;
1162 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1165 s->last_expected_delay,
1170 s->frag_ctx = frag_ctx;
1173 if (s->addrlen == sizeof (struct sockaddr_in))
1175 if (plugin->with_v4_ws == GNUNET_NO)
1177 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1178 GNUNET_SCHEDULER_cancel(plugin->select_task);
1180 plugin->select_task =
1181 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1182 GNUNET_TIME_UNIT_FOREVER_REL,
1185 &udp_plugin_select, plugin);
1186 plugin->with_v4_ws = GNUNET_YES;
1190 else if (s->addrlen == sizeof (struct sockaddr_in6))
1192 if (plugin->with_v6_ws == GNUNET_NO)
1194 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1195 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1197 plugin->select_task_v6 =
1198 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1199 GNUNET_TIME_UNIT_FOREVER_REL,
1202 &udp_plugin_select_v6, plugin);
1203 plugin->with_v6_ws = GNUNET_YES;
1212 * Our external IP address/port mapping has changed.
1214 * @param cls closure, the 'struct LocalAddrList'
1215 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1216 * the previous (now invalid) one
1217 * @param addr either the previous or the new public IP address
1218 * @param addrlen actual lenght of the address
1221 udp_nat_port_map_callback (void *cls, int add_remove,
1222 const struct sockaddr *addr, socklen_t addrlen)
1224 struct Plugin *plugin = cls;
1225 struct IPv4UdpAddress u4;
1226 struct IPv6UdpAddress u6;
1230 /* convert 'addr' to our internal format */
1231 switch (addr->sa_family)
1234 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1235 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1236 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1241 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1242 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1243 sizeof (struct in6_addr));
1244 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1252 /* modify our published address list */
1253 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1259 * Message tokenizer has broken up an incomming message. Pass it on
1262 * @param cls the 'struct Plugin'
1263 * @param client the 'struct SourceInformation'
1264 * @param hdr the actual message
1267 process_inbound_tokenized_messages (void *cls, void *client,
1268 const struct GNUNET_MessageHeader *hdr)
1270 struct Plugin *plugin = cls;
1271 struct SourceInformation *si = client;
1272 struct GNUNET_ATS_Information ats[2];
1273 struct GNUNET_TIME_Relative delay;
1275 GNUNET_assert (si->session != NULL);
1276 if (GNUNET_YES == si->session->in_destroy)
1279 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1280 ats[0].value = htonl (1);
1281 ats[1] = si->session->ats;
1282 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1284 delay = plugin->env->receive (plugin->env->cls,
1287 (const struct GNUNET_ATS_Information *) &ats, 2,
1291 si->session->flow_delay_for_other_peer = delay;
1296 * We've received a UDP Message. Process it (pass contents to main service).
1298 * @param plugin plugin context
1299 * @param msg the message
1300 * @param sender_addr sender address
1301 * @param sender_addr_len number of bytes in sender_addr
1304 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1305 const struct sockaddr *sender_addr,
1306 socklen_t sender_addr_len)
1308 struct SourceInformation si;
1310 struct IPv4UdpAddress u4;
1311 struct IPv6UdpAddress u6;
1315 if (0 != ntohl (msg->reserved))
1317 GNUNET_break_op (0);
1320 if (ntohs (msg->header.size) <
1321 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1323 GNUNET_break_op (0);
1327 /* convert address */
1328 switch (sender_addr->sa_family)
1331 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1332 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1333 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1338 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1339 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1340 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1348 LOG (GNUNET_ERROR_TYPE_DEBUG,
1349 "Received message with %u bytes from peer `%s' at `%s'\n",
1350 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1351 GNUNET_a2s (sender_addr, sender_addr_len));
1353 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1354 s = udp_plugin_get_session(plugin, address);
1355 GNUNET_free (address);
1357 /* iterate over all embedded messages */
1359 si.sender = msg->sender;
1363 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1364 ntohs (msg->header.size) -
1365 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1367 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1373 * Scan the heap for a receive context with the given address.
1375 * @param cls the 'struct FindReceiveContext'
1376 * @param node internal node of the heap
1377 * @param element value stored at the node (a 'struct ReceiveContext')
1378 * @param cost cost associated with the node
1379 * @return GNUNET_YES if we should continue to iterate,
1383 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1384 void *element, GNUNET_CONTAINER_HeapCostType cost)
1386 struct FindReceiveContext *frc = cls;
1387 struct DefragContext *e = element;
1389 if ((frc->addr_len == e->addr_len) &&
1390 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1400 * Process a defragmented message.
1402 * @param cls the 'struct ReceiveContext'
1403 * @param msg the message
1406 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1408 struct DefragContext *rc = cls;
1410 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1415 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1420 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1421 rc->src_addr, rc->addr_len);
1424 struct LookupContext
1426 const struct sockaddr * addr;
1429 struct Session *res;
1433 lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1435 struct LookupContext *l_ctx = cls;
1436 struct Session * s = value;
1438 if ((s->addrlen == l_ctx->addrlen) &&
1439 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1448 * Transmit an acknowledgement.
1450 * @param cls the 'struct ReceiveContext'
1451 * @param id message ID (unused)
1452 * @param msg ack to transmit
1455 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1457 struct DefragContext *rc = cls;
1459 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1460 struct UDP_ACK_Message *udp_ack;
1462 struct UDPMessageWrapper *udpw;
1465 struct LookupContext l_ctx;
1466 l_ctx.addr = rc->src_addr;
1467 l_ctx.addrlen = rc->addr_len;
1469 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1470 &lookup_session_by_addr_it,
1477 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1478 delay = s->flow_delay_for_other_peer.rel_value;
1480 LOG (GNUNET_ERROR_TYPE_DEBUG,
1481 "Sending ACK to `%s' including delay of %u ms\n",
1482 GNUNET_a2s (rc->src_addr,
1483 (rc->src_addr->sa_family ==
1484 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1487 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1489 udpw->cont_cls = NULL;
1490 udpw->frag_ctx = NULL;
1491 udpw->msg_size = msize;
1493 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1494 udpw->udp = (char *)&udpw[1];
1496 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1497 udp_ack->header.size = htons ((uint16_t) msize);
1498 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1499 udp_ack->delay = htonl (delay);
1500 udp_ack->sender = *rc->plugin->env->my_identity;
1501 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1503 enqueue (rc->plugin, udpw);
1507 static void read_process_msg (struct Plugin *plugin,
1508 const struct GNUNET_MessageHeader *msg,
1512 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1514 GNUNET_break_op (0);
1517 process_udp_message (plugin, (const struct UDPMessage *) msg,
1518 (const struct sockaddr *) addr, fromlen);
1522 static void read_process_ack (struct Plugin *plugin,
1523 const struct GNUNET_MessageHeader *msg,
1527 const struct GNUNET_MessageHeader *ack;
1528 const struct UDP_ACK_Message *udp_ack;
1529 struct LookupContext l_ctx;
1530 struct Session *s = NULL;
1531 struct GNUNET_TIME_Relative flow_delay;
1533 if (ntohs (msg->size) <
1534 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1536 GNUNET_break_op (0);
1540 udp_ack = (const struct UDP_ACK_Message *) msg;
1542 l_ctx.addr = (const struct sockaddr *) addr;
1543 l_ctx.addrlen = fromlen;
1545 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1546 &lookup_session_by_addr_it,
1550 if ((s == NULL) || (s->frag_ctx == NULL))
1553 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1554 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1555 flow_delay.rel_value);
1556 s->flow_delay_from_other_peer =
1557 GNUNET_TIME_relative_to_absolute (flow_delay);
1559 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1560 if (ntohs (ack->size) !=
1561 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1563 GNUNET_break_op (0);
1567 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1569 LOG (GNUNET_ERROR_TYPE_DEBUG,
1570 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1571 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1572 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1576 LOG (GNUNET_ERROR_TYPE_DEBUG,
1577 "FULL MESSAGE ACKed\n",
1578 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1579 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1580 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1582 struct UDPMessageWrapper * udpw = NULL;
1583 struct UDPMessageWrapper * tmp = NULL;
1584 if (s->addrlen == sizeof (struct sockaddr_in6))
1586 udpw = plugin->ipv6_queue_head;
1590 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1592 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1598 if (s->addrlen == sizeof (struct sockaddr_in))
1600 udpw = plugin->ipv4_queue_head;
1604 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1606 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1613 if (s->frag_ctx->cont != NULL)
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "Calling continuation for fragmented message to `%s' with result %s\n",
1617 GNUNET_i2s (&s->target), "OK");
1618 s->frag_ctx->cont (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1621 GNUNET_free (s->frag_ctx);
1626 static void read_process_fragment (struct Plugin *plugin,
1627 const struct GNUNET_MessageHeader *msg,
1631 struct DefragContext *d_ctx;
1632 struct GNUNET_TIME_Absolute now;
1633 struct FindReceiveContext frc;
1637 frc.addr = (const struct sockaddr *) addr;
1638 frc.addr_len = fromlen;
1640 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1641 (unsigned int) ntohs (msg->size),
1642 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1643 /* Lookup existing receive context for this address */
1644 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1645 &find_receive_context,
1647 now = GNUNET_TIME_absolute_get ();
1652 /* Create a new defragmentation context */
1653 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1654 memcpy (&d_ctx[1], addr, fromlen);
1655 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1656 d_ctx->addr_len = fromlen;
1657 d_ctx->plugin = plugin;
1659 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1660 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1661 &fragment_msg_proc, &ack_proc);
1663 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1664 (GNUNET_CONTAINER_HeapCostType)
1666 LOG (GNUNET_ERROR_TYPE_DEBUG, "Created new defragmentation context for %u-byte fragment from `%s'\n",
1667 (unsigned int) ntohs (msg->size),
1668 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1672 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1673 (unsigned int) ntohs (msg->size),
1674 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1677 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1679 /* keep this 'rc' from expiring */
1680 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1681 (GNUNET_CONTAINER_HeapCostType)
1684 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1685 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1687 /* remove 'rc' that was inactive the longest */
1688 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1689 GNUNET_assert (NULL != d_ctx);
1690 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1691 GNUNET_free (d_ctx);
1696 * Read and process a message from the given socket.
1698 * @param plugin the overall plugin
1699 * @param rsock socket to read from
1702 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1706 char buf[65536] GNUNET_ALIGN;
1708 const struct GNUNET_MessageHeader *msg;
1710 fromlen = sizeof (addr);
1711 memset (&addr, 0, sizeof (addr));
1712 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1713 (struct sockaddr *) &addr, &fromlen);
1715 if (size < sizeof (struct GNUNET_MessageHeader))
1717 GNUNET_break_op (0);
1720 msg = (const struct GNUNET_MessageHeader *) buf;
1722 LOG (GNUNET_ERROR_TYPE_DEBUG,
1723 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1724 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1726 if (size != ntohs (msg->size))
1728 GNUNET_break_op (0);
1732 switch (ntohs (msg->type))
1734 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1735 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1738 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1739 read_process_msg (plugin, msg, addr, fromlen);
1742 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1743 read_process_ack (plugin, msg, addr, fromlen);
1746 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1747 read_process_fragment (plugin, msg, addr, fromlen);
1751 GNUNET_break_op (0);
1758 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
1762 struct GNUNET_TIME_Absolute max;
1763 struct UDPMessageWrapper *udpw = NULL;
1765 if (sock == plugin->sockv4)
1767 udpw = plugin->ipv4_queue_head;
1769 else if (sock == plugin->sockv6)
1771 udpw = plugin->ipv6_queue_head;
1779 const struct sockaddr * sa = udpw->session->sock_addr;
1780 slen = udpw->session->addrlen;
1782 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1784 while (udpw != NULL)
1786 if (max.abs_value != udpw->timeout.abs_value)
1788 /* Message timed out */
1789 call_continuation(udpw, GNUNET_SYSERR);
1790 if (udpw->frag_ctx != NULL)
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fragmented message for peer `%s' with size %u timed out\n",
1793 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1794 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1795 GNUNET_free (udpw->frag_ctx);
1796 udpw->session->frag_ctx = NULL;
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' with size %u timed out\n",
1801 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1804 if (sock == plugin->sockv4)
1806 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1808 udpw = plugin->ipv4_queue_head;
1810 else if (sock == plugin->sockv6)
1812 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1814 udpw = plugin->ipv6_queue_head;
1819 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1820 if (delta.rel_value == 0)
1822 /* this message is not delayed */
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is not delayed \n",
1824 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1829 /* this message is delayed, try next */
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1831 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1840 /* No message left */
1844 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
1846 if (GNUNET_SYSERR == sent)
1848 const struct GNUNET_ATS_Information type = plugin->env->get_address_type
1849 (plugin->env->cls,sa, slen);
1851 if ((GNUNET_ATS_NET_WAN == type.value) &&
1852 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
1854 /* "Network unreachable" or "Network down" */
1856 * This indicates that this system is IPv6 enabled, but does not
1857 * have a valid global IPv6 address assigned
1859 LOG (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1860 _("UDP could not message to `%s': `%s'. "
1861 "Please check your network configuration and disable IPv6 if your "
1862 "connection does not have a global IPv6 address\n"),
1863 GNUNET_a2s (sa, slen),
1868 LOG (GNUNET_ERROR_TYPE_ERROR,
1869 "UDP could not transmit %u-byte message to `%s': `%s'\n",
1870 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
1873 call_continuation(udpw, GNUNET_SYSERR);
1877 LOG (GNUNET_ERROR_TYPE_DEBUG,
1878 "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
1879 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1880 (sent < 0) ? STRERROR (errno) : "ok");
1881 call_continuation(udpw, GNUNET_OK);
1884 if (sock == plugin->sockv4)
1885 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1886 else if (sock == plugin->sockv6)
1887 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1895 * We have been notified that our readset has something to read. We don't
1896 * know which socket needs to be read, so we have to check each one
1897 * Then reschedule this function to be called again once more is available.
1899 * @param cls the plugin handle
1900 * @param tc the scheduling context (for rescheduling this function again)
1903 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1905 struct Plugin *plugin = cls;
1907 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1908 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1910 plugin->with_v4_ws = GNUNET_NO;
1912 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1914 if ((NULL != plugin->sockv4) &&
1915 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1916 udp_select_read (plugin, plugin->sockv4);
1920 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1922 if ((NULL != plugin->sockv4) && (plugin->ipv4_queue_head != NULL) &&
1923 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
1925 udp_select_send (plugin, plugin->sockv4);
1929 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1930 GNUNET_SCHEDULER_cancel (plugin->select_task);
1931 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1932 GNUNET_TIME_UNIT_FOREVER_REL,
1934 (plugin->ipv4_queue_head != NULL) ? plugin->ws_v4 : NULL,
1935 &udp_plugin_select, plugin);
1936 if (plugin->ipv4_queue_head != NULL)
1937 plugin->with_v4_ws = GNUNET_YES;
1939 plugin->with_v4_ws = GNUNET_NO;
1944 * We have been notified that our readset has something to read. We don't
1945 * know which socket needs to be read, so we have to check each one
1946 * Then reschedule this function to be called again once more is available.
1948 * @param cls the plugin handle
1949 * @param tc the scheduling context (for rescheduling this function again)
1952 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1954 struct Plugin *plugin = cls;
1956 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
1957 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1960 plugin->with_v6_ws = GNUNET_NO;
1961 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1963 if ((NULL != plugin->sockv6) &&
1964 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1965 udp_select_read (plugin, plugin->sockv6);
1968 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1970 if ((NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
1971 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)))
1973 udp_select_send (plugin, plugin->sockv6);
1976 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1977 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
1978 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1979 GNUNET_TIME_UNIT_FOREVER_REL,
1981 (plugin->ipv6_queue_head != NULL) ? plugin->ws_v6 : NULL,
1982 &udp_plugin_select_v6, plugin);
1983 if (plugin->ipv6_queue_head != NULL)
1984 plugin->with_v6_ws = GNUNET_YES;
1986 plugin->with_v6_ws = GNUNET_NO;
1991 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1994 int sockets_created = 0;
1995 struct sockaddr *serverAddr;
1996 struct sockaddr *addrs[2];
1997 socklen_t addrlens[2];
2000 /* Create IPv6 socket */
2001 if (plugin->enable_ipv6 == GNUNET_YES)
2003 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2004 if (NULL == plugin->sockv6)
2006 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2007 plugin->enable_ipv6 = GNUNET_NO;
2011 #if HAVE_SOCKADDR_IN_SIN_LEN
2012 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2014 serverAddrv6->sin6_family = AF_INET6;
2015 serverAddrv6->sin6_addr = in6addr_any;
2016 serverAddrv6->sin6_port = htons (plugin->port);
2017 addrlen = sizeof (struct sockaddr_in6);
2018 serverAddr = (struct sockaddr *) serverAddrv6;
2019 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2020 ntohs (serverAddrv6->sin6_port));
2022 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2025 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2026 LOG (GNUNET_ERROR_TYPE_DEBUG,
2027 "IPv6 Binding failed, trying new port %d\n",
2028 ntohs (serverAddrv6->sin6_port));
2032 GNUNET_NETWORK_socket_close (plugin->sockv6);
2033 plugin->sockv6 = NULL;
2037 if (plugin->sockv6 != NULL)
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "IPv6 socket created on port %d\n",
2041 ntohs (serverAddrv6->sin6_port));
2042 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2043 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2049 /* Create IPv4 socket */
2050 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2051 if (NULL == plugin->sockv4)
2053 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2057 #if HAVE_SOCKADDR_IN_SIN_LEN
2058 serverAddrv4->sin_len = sizeof (serverAddrv4);
2060 serverAddrv4->sin_family = AF_INET;
2061 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2062 serverAddrv4->sin_port = htons (plugin->port);
2063 addrlen = sizeof (struct sockaddr_in);
2064 serverAddr = (struct sockaddr *) serverAddrv4;
2066 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2067 ntohs (serverAddrv4->sin_port));
2069 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2072 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2073 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2074 ntohs (serverAddrv4->sin_port));
2078 GNUNET_NETWORK_socket_close (plugin->sockv4);
2079 plugin->sockv4 = NULL;
2083 if (plugin->sockv4 != NULL)
2085 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2086 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2091 /* Create file descriptors */
2092 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2093 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2094 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2095 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2096 if (NULL != plugin->sockv4)
2098 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2099 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2102 if (sockets_created == 0)
2103 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2105 plugin->select_task =
2106 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2107 GNUNET_TIME_UNIT_FOREVER_REL,
2110 &udp_plugin_select, plugin);
2111 plugin->with_v4_ws = GNUNET_NO;
2113 if (plugin->enable_ipv6 == GNUNET_YES)
2115 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2116 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2117 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2118 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2119 if (NULL != plugin->sockv6)
2121 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2122 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2125 plugin->select_task_v6 =
2126 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2127 GNUNET_TIME_UNIT_FOREVER_REL,
2130 &udp_plugin_select_v6, plugin);
2131 plugin->with_v6_ws = GNUNET_NO;
2134 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2135 GNUNET_NO, plugin->port,
2137 (const struct sockaddr **) addrs, addrlens,
2138 &udp_nat_port_map_callback, NULL, plugin);
2140 return sockets_created;
2145 * The exported method. Makes the core api available via a global and
2146 * returns the udp transport API.
2148 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2149 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2152 libgnunet_plugin_transport_udp_init (void *cls)
2154 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2155 struct GNUNET_TRANSPORT_PluginFunctions *api;
2156 struct Plugin *plugin;
2157 unsigned long long port;
2158 unsigned long long aport;
2159 unsigned long long broadcast;
2160 unsigned long long udp_max_bps;
2161 unsigned long long enable_v6;
2162 char * bind4_address;
2163 char * bind6_address;
2164 struct GNUNET_TIME_Relative interval;
2165 struct sockaddr_in serverAddrv4;
2166 struct sockaddr_in6 serverAddrv6;
2169 if (NULL == env->receive)
2171 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2172 initialze the plugin or the API */
2173 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2175 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2176 api->address_to_string = &udp_address_to_string;
2177 api->string_to_address = &udp_string_to_address;
2181 GNUNET_assert( NULL != env->stats);
2183 /* Get port number */
2185 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2189 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2190 "ADVERTISED_PORT", &aport))
2194 LOG (GNUNET_ERROR_TYPE_WARNING,
2195 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2202 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2205 enable_v6 = GNUNET_NO;
2208 enable_v6 = GNUNET_YES;
2211 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2212 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2215 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2216 "BINDTO", &bind4_address))
2218 LOG (GNUNET_ERROR_TYPE_DEBUG,
2219 "Binding udp plugin to specific address: `%s'\n",
2221 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2223 GNUNET_free (bind4_address);
2229 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2230 "BINDTO6", &bind6_address))
2232 LOG (GNUNET_ERROR_TYPE_DEBUG,
2233 "Binding udp plugin to specific address: `%s'\n",
2236 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2238 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2240 GNUNET_free_non_null (bind4_address);
2241 GNUNET_free (bind6_address);
2246 /* Enable neighbour discovery */
2247 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2249 if (broadcast == GNUNET_SYSERR)
2250 broadcast = GNUNET_NO;
2252 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2253 "BROADCAST_INTERVAL", &interval))
2255 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2258 /* Maximum datarate */
2259 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2260 "MAX_BPS", &udp_max_bps))
2262 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2265 plugin = GNUNET_malloc (sizeof (struct Plugin));
2266 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2268 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
2269 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2270 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2271 plugin->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2272 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
2273 plugin->port = port;
2274 plugin->aport = aport;
2275 plugin->broadcast_interval = interval;
2276 plugin->enable_ipv6 = enable_v6;
2281 api->disconnect = &udp_disconnect;
2282 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2283 api->address_to_string = &udp_address_to_string;
2284 api->string_to_address = &udp_string_to_address;
2285 api->check_address = &udp_plugin_check_address;
2286 api->get_session = &udp_plugin_get_session;
2287 api->send = &udp_plugin_send;
2289 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2290 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
2291 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
2293 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2294 GNUNET_free (plugin);
2299 if (broadcast == GNUNET_YES)
2301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2302 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
2305 GNUNET_free_non_null (bind4_address);
2306 GNUNET_free_non_null (bind6_address);
2312 heap_cleanup_iterator (void *cls,
2313 struct GNUNET_CONTAINER_HeapNode *
2314 node, void *element,
2315 GNUNET_CONTAINER_HeapCostType
2318 struct DefragContext * d_ctx = element;
2320 GNUNET_CONTAINER_heap_remove_node (node);
2321 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2322 GNUNET_free (d_ctx);
2329 * The exported method. Makes the core api available via a global and
2330 * returns the udp transport API.
2332 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2336 libgnunet_plugin_transport_udp_done (void *cls)
2338 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2339 struct Plugin *plugin = api->cls;
2347 stop_broadcast (plugin);
2349 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2351 GNUNET_SCHEDULER_cancel (plugin->select_task);
2352 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2354 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2356 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2357 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2360 /* Closing sockets */
2361 if (plugin->sockv4 != NULL)
2363 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2364 plugin->sockv4 = NULL;
2366 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2367 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2369 if (plugin->sockv6 != NULL)
2371 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2372 plugin->sockv6 = NULL;
2374 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2375 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2378 GNUNET_NAT_unregister (plugin->nat);
2380 if (plugin->defrag_ctxs != NULL)
2382 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2383 heap_cleanup_iterator, NULL);
2384 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2385 plugin->defrag_ctxs = NULL;
2387 if (plugin->mst != NULL)
2389 GNUNET_SERVER_mst_destroy(plugin->mst);
2393 /* Clean up leftover messages */
2394 struct UDPMessageWrapper * udpw;
2395 udpw = plugin->ipv4_queue_head;
2396 while (udpw != NULL)
2398 struct UDPMessageWrapper *tmp = udpw->next;
2399 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
2400 call_continuation(udpw, GNUNET_SYSERR);
2404 udpw = plugin->ipv6_queue_head;
2405 while (udpw != NULL)
2407 struct UDPMessageWrapper *tmp = udpw->next;
2408 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2409 call_continuation(udpw, GNUNET_SYSERR);
2414 /* Clean up sessions */
2415 LOG (GNUNET_ERROR_TYPE_DEBUG,
2416 "Cleaning up sessions\n");
2417 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2418 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2421 GNUNET_free (plugin);
2427 /* end of plugin_transport_udp.c */