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;
96 struct FragmentationContext * frag_ctx;
99 * Address of the other peer
101 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 * Session timeout task
116 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
119 * expected delay for ACKs
121 struct GNUNET_TIME_Relative last_expected_delay;
123 struct GNUNET_ATS_Information ats;
134 struct SessionCompareContext
137 const struct GNUNET_HELLO_Address *addr;
142 * Closure for 'process_inbound_tokenized_messages'
144 struct SourceInformation
149 struct GNUNET_PeerIdentity sender;
156 struct Session *session;
158 * Number of bytes in source address.
166 * Closure for 'find_receive_context'.
168 struct FindReceiveContext
171 * Where to store the result.
173 struct DefragContext *rc;
178 const struct sockaddr *addr;
180 struct Session *session;
183 * Number of bytes in 'addr'.
192 * Data structure to track defragmentation contexts based
193 * on the source of the UDP traffic.
199 * Defragmentation context.
201 struct GNUNET_DEFRAGMENT_Context *defrag;
204 * Source address this receive context is for (allocated at the
205 * end of the struct).
207 const struct sockaddr *src_addr;
210 * Reference to master plugin struct.
212 struct Plugin *plugin;
215 * Node in the defrag heap.
217 struct GNUNET_CONTAINER_HeapNode *hnode;
220 * Length of 'src_addr'
228 * Closure for 'process_inbound_tokenized_messages'
230 struct FragmentationContext
232 struct FragmentationContext * next;
233 struct FragmentationContext * prev;
235 struct Plugin * plugin;
236 struct GNUNET_FRAGMENT_Context * frag;
237 struct Session * session;
240 * Function to call upon completion of the transmission.
242 GNUNET_TRANSPORT_TransmitContinuation cont;
245 * Closure for 'cont'.
249 struct GNUNET_TIME_Absolute timeout;
251 size_t bytes_to_send;
255 struct UDPMessageWrapper
257 struct Session *session;
258 struct UDPMessageWrapper *prev;
259 struct UDPMessageWrapper *next;
263 * Function to call upon completion of the transmission.
265 GNUNET_TRANSPORT_TransmitContinuation cont;
268 * Closure for 'cont'.
272 struct FragmentationContext *frag_ctx;
276 struct GNUNET_TIME_Absolute timeout;
281 * UDP ACK Message-Packet header (after defragmentation).
283 struct UDP_ACK_Message
288 struct GNUNET_MessageHeader header;
291 * Desired delay for flow control
296 * What is the identity of the sender
298 struct GNUNET_PeerIdentity sender;
303 * Encapsulation of all of the state of the plugin.
305 struct Plugin * plugin;
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 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
321 * We have been notified that our readset has something to read. We don't
322 * know which socket needs to be read, so we have to check each one
323 * Then reschedule this function to be called again once more is available.
325 * @param cls the plugin handle
326 * @param tc the scheduling context (for rescheduling this function again)
329 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
333 * Start session timeout
336 start_session_timeout (struct Session *s);
339 * Increment session timeout due to activity
342 reschedule_session_timeout (struct Session *s);
348 stop_session_timeout (struct Session *s);
353 * Function called for a quick conversion of the binary address to
354 * a numeric address. Note that the caller must not free the
355 * address and that the next call to this function is allowed
356 * to override the address again.
359 * @param addr binary address
360 * @param addrlen length of the address
361 * @return string representing the same address
364 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
366 static char rbuf[INET6_ADDRSTRLEN + 10];
367 char buf[INET6_ADDRSTRLEN];
371 const struct IPv4UdpAddress *t4;
372 const struct IPv6UdpAddress *t6;
376 if (addrlen == sizeof (struct IPv6UdpAddress))
380 port = ntohs (t6->u6_port);
381 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
384 else if (addrlen == sizeof (struct IPv4UdpAddress))
388 port = ntohs (t4->u4_port);
389 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
397 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
398 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
405 * Function called to convert a string address to
408 * @param cls closure ('struct Plugin*')
409 * @param addr string address
410 * @param addrlen length of the address
411 * @param buf location to store the buffer
412 * @param added location to store the number of bytes in the buffer.
413 * If the function returns GNUNET_SYSERR, its contents are undefined.
414 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
417 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
418 void **buf, size_t *added)
420 struct sockaddr_storage socket_address;
422 if ((NULL == addr) || (0 == addrlen))
425 return GNUNET_SYSERR;
428 if ('\0' != addr[addrlen - 1])
430 return GNUNET_SYSERR;
433 if (strlen (addr) != addrlen - 1)
435 return GNUNET_SYSERR;
438 if (GNUNET_OK != GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
441 return GNUNET_SYSERR;
444 switch (socket_address.ss_family)
448 struct IPv4UdpAddress *u4;
449 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
450 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
451 u4->ipv4_addr = in4->sin_addr.s_addr;
452 u4->u4_port = in4->sin_port;
454 *added = sizeof (struct IPv4UdpAddress);
459 struct IPv6UdpAddress *u6;
460 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
461 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
462 u6->ipv6_addr = in6->sin6_addr;
463 u6->u6_port = in6->sin6_port;
465 *added = sizeof (struct IPv6UdpAddress);
470 return GNUNET_SYSERR;
476 * Append our port and forward the result.
478 * @param cls a 'struct PrettyPrinterContext'
479 * @param hostname result from DNS resolver
482 append_port (void *cls, const char *hostname)
484 struct PrettyPrinterContext *ppc = cls;
487 if (hostname == NULL)
489 ppc->asc (ppc->asc_cls, NULL);
493 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
494 ppc->asc (ppc->asc_cls, ret);
500 * Convert the transports address to a nice, human-readable
504 * @param type name of the transport that generated the address
505 * @param addr one of the addresses of the host, NULL for the last address
506 * the specific address format depends on the transport
507 * @param addrlen length of the address
508 * @param numeric should (IP) addresses be displayed in numeric form?
509 * @param timeout after how long should we give up?
510 * @param asc function to call on each string
511 * @param asc_cls closure for asc
514 udp_plugin_address_pretty_printer (void *cls, const char *type,
515 const void *addr, size_t addrlen,
517 struct GNUNET_TIME_Relative timeout,
518 GNUNET_TRANSPORT_AddressStringCallback asc,
521 struct PrettyPrinterContext *ppc;
524 struct sockaddr_in a4;
525 struct sockaddr_in6 a6;
526 const struct IPv4UdpAddress *u4;
527 const struct IPv6UdpAddress *u6;
530 if (addrlen == sizeof (struct IPv6UdpAddress))
533 memset (&a6, 0, sizeof (a6));
534 a6.sin6_family = AF_INET6;
535 #if HAVE_SOCKADDR_IN_SIN_LEN
536 a6.sin6_len = sizeof (a6);
538 a6.sin6_port = u6->u6_port;
539 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
540 port = ntohs (u6->u6_port);
544 else if (addrlen == sizeof (struct IPv4UdpAddress))
547 memset (&a4, 0, sizeof (a4));
548 a4.sin_family = AF_INET;
549 #if HAVE_SOCKADDR_IN_SIN_LEN
550 a4.sin_len = sizeof (a4);
552 a4.sin_port = u4->u4_port;
553 a4.sin_addr.s_addr = u4->ipv4_addr;
554 port = ntohs (u4->u4_port);
558 else if (0 == addrlen)
560 asc (asc_cls, "<inbound connection>");
566 /* invalid address */
571 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
573 ppc->asc_cls = asc_cls;
575 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
580 call_continuation (struct UDPMessageWrapper *udpw, int result)
582 LOG (GNUNET_ERROR_TYPE_DEBUG,
583 "Calling continuation for %u byte message to `%s' with result %s\n",
584 udpw->msg_size, GNUNET_i2s (&udpw->session->target),
585 (GNUNET_OK == result) ? "OK" : "SYSERR");
586 if (NULL != udpw->cont)
588 udpw->cont (udpw->cont_cls, &udpw->session->target,result);
595 * Check if the given port is plausible (must be either our listen
596 * port or our advertised port). If it is neither, we return
599 * @param plugin global variables
600 * @param in_port port number to check
601 * @return GNUNET_OK if port is either open_port or adv_port
604 check_port (struct Plugin *plugin, uint16_t in_port)
606 if ((in_port == plugin->port) || (in_port == plugin->aport))
608 return GNUNET_SYSERR;
613 * Function that will be called to check if a binary address for this
614 * plugin is well-formed and corresponds to an address for THIS peer
615 * (as per our configuration). Naturally, if absolutely necessary,
616 * plugins can be a bit conservative in their answer, but in general
617 * plugins should make sure that the address does not redirect
618 * traffic to a 3rd party that might try to man-in-the-middle our
621 * @param cls closure, should be our handle to the Plugin
622 * @param addr pointer to the address
623 * @param addrlen length of addr
624 * @return GNUNET_OK if this is a plausible address for this peer
625 * and transport, GNUNET_SYSERR if not
629 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
631 struct Plugin *plugin = cls;
632 struct IPv4UdpAddress *v4;
633 struct IPv6UdpAddress *v6;
635 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
636 (addrlen != sizeof (struct IPv6UdpAddress)))
639 return GNUNET_SYSERR;
641 if (addrlen == sizeof (struct IPv4UdpAddress))
643 v4 = (struct IPv4UdpAddress *) addr;
644 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
645 return GNUNET_SYSERR;
647 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
648 sizeof (struct in_addr)))
649 return GNUNET_SYSERR;
653 v6 = (struct IPv6UdpAddress *) addr;
654 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
657 return GNUNET_SYSERR;
659 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
660 return GNUNET_SYSERR;
662 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
663 sizeof (struct in6_addr)))
664 return GNUNET_SYSERR;
671 * Task to free resources associated with a session.
673 * @param s session to free
676 free_session (struct Session *s)
678 if (s->frag_ctx != NULL)
680 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
681 GNUNET_free (s->frag_ctx);
689 * Functions with this signature are called whenever we need
690 * to close a session due to a disconnect or failure to
691 * establish a connection.
693 * @param session session to close down
696 disconnect_session (struct Session *s)
698 struct UDPMessageWrapper *udpw;
699 struct UDPMessageWrapper *next;
701 GNUNET_assert (GNUNET_YES != s->in_destroy);
702 LOG (GNUNET_ERROR_TYPE_DEBUG,
703 "Session %p to peer `%s' address ended \n",
705 GNUNET_i2s (&s->target),
706 GNUNET_a2s (s->sock_addr, s->addrlen));
707 stop_session_timeout(s);
708 next = plugin->ipv4_queue_head;
709 while (NULL != (udpw = next))
712 if (udpw->session == s)
714 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
715 call_continuation(udpw, GNUNET_SYSERR);
719 next = plugin->ipv6_queue_head;
720 while (NULL != (udpw = next))
723 if (udpw->session == s)
725 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
726 call_continuation(udpw, GNUNET_SYSERR);
731 plugin->env->session_end (plugin->env->cls, &s->target, s);
733 if (NULL != s->frag_ctx)
735 if (NULL != s->frag_ctx->cont)
737 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR);
738 LOG (GNUNET_ERROR_TYPE_DEBUG,
739 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
740 GNUNET_i2s (&s->target));
744 GNUNET_assert (GNUNET_YES ==
745 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
746 &s->target.hashPubKey,
748 GNUNET_STATISTICS_set(plugin->env->stats,
749 "# UDP sessions active",
750 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
753 s->in_destroy = GNUNET_YES;
759 * Destroy a session, plugin is being unloaded.
762 * @param key hash of public key of target peer
763 * @param value a 'struct PeerSession*' to clean up
764 * @return GNUNET_OK (continue to iterate)
767 disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
769 disconnect_session(value);
775 * Disconnect from a remote node. Clean up session if we have one for this peer
777 * @param cls closure for this call (should be handle to Plugin)
778 * @param target the peeridentity of the peer to disconnect
779 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
782 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
784 struct Plugin *plugin = cls;
785 GNUNET_assert (plugin != NULL);
787 GNUNET_assert (target != NULL);
788 LOG (GNUNET_ERROR_TYPE_DEBUG,
789 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
790 /* Clean up sessions */
791 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
795 static struct Session *
796 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
797 const void *addr, size_t addrlen,
798 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
801 const struct IPv4UdpAddress *t4;
802 const struct IPv6UdpAddress *t6;
803 struct sockaddr_in *v4;
804 struct sockaddr_in6 *v6;
809 case sizeof (struct IPv4UdpAddress):
810 if (NULL == plugin->sockv4)
815 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
816 len = sizeof (struct sockaddr_in);
817 v4 = (struct sockaddr_in *) &s[1];
818 v4->sin_family = AF_INET;
819 #if HAVE_SOCKADDR_IN_SIN_LEN
820 v4->sin_len = sizeof (struct sockaddr_in);
822 v4->sin_port = t4->u4_port;
823 v4->sin_addr.s_addr = t4->ipv4_addr;
824 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
826 case sizeof (struct IPv6UdpAddress):
827 if (NULL == plugin->sockv6)
833 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
834 len = sizeof (struct sockaddr_in6);
835 v6 = (struct sockaddr_in6 *) &s[1];
836 v6->sin6_family = AF_INET6;
837 #if HAVE_SOCKADDR_IN_SIN_LEN
838 v6->sin6_len = sizeof (struct sockaddr_in6);
840 v6->sin6_port = t6->u6_port;
841 v6->sin6_addr = t6->ipv6_addr;
842 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
845 /* Must have a valid address to send to */
852 s->sock_addr = (const struct sockaddr *) &s[1];
853 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
854 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
855 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
857 start_session_timeout(s);
864 session_cmp_it (void *cls,
865 const GNUNET_HashCode * key,
868 struct SessionCompareContext * cctx = cls;
869 const struct GNUNET_HELLO_Address *address = cctx->addr;
870 struct Session *s = value;
872 socklen_t s_addrlen = s->addrlen;
874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
875 udp_address_to_string (NULL, (void *) address->address, address->address_length),
876 GNUNET_a2s (s->sock_addr, s->addrlen));
877 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
878 (s_addrlen == sizeof (struct sockaddr_in)))
880 struct IPv4UdpAddress * u4 = NULL;
881 u4 = (struct IPv4UdpAddress *) address->address;
882 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
883 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
884 (u4->u4_port == s4->sin_port))
891 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
892 (s_addrlen == sizeof (struct sockaddr_in6)))
894 struct IPv6UdpAddress * u6 = NULL;
895 u6 = (struct IPv6UdpAddress *) address->address;
896 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
897 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
898 (u6->u6_port == s6->sin6_port))
909 * Creates a new outbound session the transport service will use to send data to the
912 * @param cls the plugin
913 * @param address the address
914 * @return the session or NULL of max connections exceeded
916 static struct Session *
917 udp_plugin_get_session (void *cls,
918 const struct GNUNET_HELLO_Address *address)
920 struct Session * s = NULL;
921 struct Plugin * plugin = cls;
922 struct IPv6UdpAddress * udp_a6;
923 struct IPv4UdpAddress * udp_a4;
925 GNUNET_assert (plugin != NULL);
926 GNUNET_assert (address != NULL);
929 if ((address->address == NULL) ||
930 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
931 (address->address_length != sizeof (struct IPv6UdpAddress))))
937 if (address->address_length == sizeof (struct IPv4UdpAddress))
939 if (plugin->sockv4 == NULL)
941 udp_a4 = (struct IPv4UdpAddress *) address->address;
942 if (udp_a4->u4_port == 0)
946 if (address->address_length == sizeof (struct IPv6UdpAddress))
948 if (plugin->sockv6 == NULL)
950 udp_a6 = (struct IPv6UdpAddress *) address->address;
951 if (udp_a6->u6_port == 0)
955 /* check if session already exists */
956 struct SessionCompareContext cctx;
959 LOG (GNUNET_ERROR_TYPE_DEBUG,
960 "Looking for existing session for peer `%s' `%s' \n",
961 GNUNET_i2s (&address->peer),
962 udp_address_to_string(NULL, address->address, address->address_length));
963 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
964 if (cctx.res != NULL)
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
970 /* otherwise create new */
971 s = create_session (plugin,
974 address->address_length,
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "Creating new session %p for peer `%s' address `%s'\n",
979 GNUNET_i2s(&address->peer),
980 udp_address_to_string(NULL,address->address,address->address_length));
981 GNUNET_assert (GNUNET_OK ==
982 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
983 &s->target.hashPubKey,
985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
987 GNUNET_STATISTICS_set(plugin->env->stats,
988 "# UDP sessions active",
989 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
997 enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
1000 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1001 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1002 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1003 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1008 * Fragment message was transmitted via UDP, let fragmentation know
1009 * to send the next fragment now.
1011 * @param cls the 'struct UDPMessageWrapper' of the fragment
1012 * @param target destination peer (ignored)
1013 * @param result GNUNET_OK on success (ignored)
1016 send_next_fragment (void *cls,
1017 const struct GNUNET_PeerIdentity *target,
1020 struct UDPMessageWrapper *udpw = cls;
1022 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1027 * Function that is called with messages created by the fragmentation
1028 * module. In the case of the 'proc' callback of the
1029 * GNUNET_FRAGMENT_context_create function, this function must
1030 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1032 * @param cls closure, the 'struct FragmentationContext'
1033 * @param msg the message that was created
1036 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1038 struct FragmentationContext *frag_ctx = cls;
1039 struct Plugin *plugin = frag_ctx->plugin;
1040 struct UDPMessageWrapper * udpw;
1042 size_t msg_len = ntohs (msg->size);
1044 LOG (GNUNET_ERROR_TYPE_DEBUG,
1045 "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
1046 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
1047 udpw->session = frag_ctx->session;
1049 udpw->udp = (char *) &udpw[1];
1051 udpw->msg_size = msg_len;
1052 udpw->cont = &send_next_fragment;
1053 udpw->cont_cls = udpw;
1054 udpw->timeout = frag_ctx->timeout;
1055 udpw->frag_ctx = frag_ctx;
1056 memcpy (udpw->udp, msg, msg_len);
1057 enqueue (plugin, udpw);
1059 if (s->addrlen == sizeof (struct sockaddr_in))
1061 if (plugin->with_v4_ws == GNUNET_NO)
1063 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1064 GNUNET_SCHEDULER_cancel(plugin->select_task);
1066 plugin->select_task =
1067 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1068 GNUNET_TIME_UNIT_FOREVER_REL,
1071 &udp_plugin_select, plugin);
1072 plugin->with_v4_ws = GNUNET_YES;
1075 else if (s->addrlen == sizeof (struct sockaddr_in6))
1077 if (plugin->with_v6_ws == GNUNET_NO)
1079 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1080 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1082 plugin->select_task_v6 =
1083 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1084 GNUNET_TIME_UNIT_FOREVER_REL,
1087 &udp_plugin_select_v6, plugin);
1088 plugin->with_v6_ws = GNUNET_YES;
1095 * Function that can be used by the transport service to transmit
1096 * a message using the plugin. Note that in the case of a
1097 * peer disconnecting, the continuation MUST be called
1098 * prior to the disconnect notification itself. This function
1099 * will be called with this peer's HELLO message to initiate
1100 * a fresh connection to another peer.
1102 * @param cls closure
1103 * @param s which session must be used
1104 * @param msgbuf the message to transmit
1105 * @param msgbuf_size number of bytes in 'msgbuf'
1106 * @param priority how important is the message (most plugins will
1107 * ignore message priority and just FIFO)
1108 * @param to how long to wait at most for the transmission (does not
1109 * require plugins to discard the message after the timeout,
1110 * just advisory for the desired delay; most plugins will ignore
1112 * @param cont continuation to call once the message has
1113 * been transmitted (or if the transport is ready
1114 * for the next transmission call; or if the
1115 * peer disconnected...); can be NULL
1116 * @param cont_cls closure for cont
1117 * @return number of bytes used (on the physical network, with overheads);
1118 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1119 * and does NOT mean that the message was not transmitted (DV)
1122 udp_plugin_send (void *cls,
1124 const char *msgbuf, size_t msgbuf_size,
1125 unsigned int priority,
1126 struct GNUNET_TIME_Relative to,
1127 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1129 struct Plugin *plugin = cls;
1130 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
1131 struct UDPMessageWrapper * udpw;
1132 struct UDPMessage *udp;
1134 GNUNET_assert (plugin != NULL);
1135 GNUNET_assert (s != NULL);
1137 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1138 return GNUNET_SYSERR;
1139 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1140 return GNUNET_SYSERR;
1141 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1144 return GNUNET_SYSERR;
1146 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1149 return GNUNET_SYSERR;
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1154 GNUNET_i2s (&s->target),
1155 GNUNET_a2s(s->sock_addr, s->addrlen));
1158 udp = (struct UDPMessage *) mbuf;
1159 udp->header.size = htons (mlen);
1160 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1161 udp->reserved = htonl (0);
1162 udp->sender = *plugin->env->my_identity;
1164 reschedule_session_timeout(s);
1165 if (mlen <= UDP_MTU)
1167 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
1169 udpw->udp = (char *) &udpw[1];
1170 udpw->msg_size = mlen;
1171 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1173 udpw->cont_cls = cont_cls;
1174 udpw->frag_ctx = NULL;
1175 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
1176 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1178 enqueue (plugin, udpw);
1182 LOG (GNUNET_ERROR_TYPE_DEBUG,
1183 "UDP has to fragment message \n");
1184 if (s->frag_ctx != NULL)
1185 return GNUNET_SYSERR;
1186 memcpy (&udp[1], msgbuf, msgbuf_size);
1187 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
1189 frag_ctx->plugin = plugin;
1190 frag_ctx->session = s;
1191 frag_ctx->cont = cont;
1192 frag_ctx->cont_cls = cont_cls;
1193 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1194 frag_ctx->bytes_to_send = mlen;
1195 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1198 s->last_expected_delay,
1203 s->frag_ctx = frag_ctx;
1206 if (s->addrlen == sizeof (struct sockaddr_in))
1208 if (plugin->with_v4_ws == GNUNET_NO)
1210 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1211 GNUNET_SCHEDULER_cancel(plugin->select_task);
1213 plugin->select_task =
1214 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1215 GNUNET_TIME_UNIT_FOREVER_REL,
1218 &udp_plugin_select, plugin);
1219 plugin->with_v4_ws = GNUNET_YES;
1222 else if (s->addrlen == sizeof (struct sockaddr_in6))
1224 if (plugin->with_v6_ws == GNUNET_NO)
1226 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
1227 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
1229 plugin->select_task_v6 =
1230 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1231 GNUNET_TIME_UNIT_FOREVER_REL,
1234 &udp_plugin_select_v6, plugin);
1235 plugin->with_v6_ws = GNUNET_YES;
1244 * Our external IP address/port mapping has changed.
1246 * @param cls closure, the 'struct LocalAddrList'
1247 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1248 * the previous (now invalid) one
1249 * @param addr either the previous or the new public IP address
1250 * @param addrlen actual lenght of the address
1253 udp_nat_port_map_callback (void *cls, int add_remove,
1254 const struct sockaddr *addr, socklen_t addrlen)
1256 struct Plugin *plugin = cls;
1257 struct IPv4UdpAddress u4;
1258 struct IPv6UdpAddress u6;
1262 /* convert 'addr' to our internal format */
1263 switch (addr->sa_family)
1266 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1267 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1268 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1273 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1274 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1275 sizeof (struct in6_addr));
1276 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1284 /* modify our published address list */
1285 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1291 * Message tokenizer has broken up an incomming message. Pass it on
1294 * @param cls the 'struct Plugin'
1295 * @param client the 'struct SourceInformation'
1296 * @param hdr the actual message
1299 process_inbound_tokenized_messages (void *cls, void *client,
1300 const struct GNUNET_MessageHeader *hdr)
1302 struct Plugin *plugin = cls;
1303 struct SourceInformation *si = client;
1304 struct GNUNET_ATS_Information ats[2];
1305 struct GNUNET_TIME_Relative delay;
1307 GNUNET_assert (si->session != NULL);
1308 if (GNUNET_YES == si->session->in_destroy)
1311 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1312 ats[0].value = htonl (1);
1313 ats[1] = si->session->ats;
1314 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1315 delay = plugin->env->receive (plugin->env->cls,
1318 (const struct GNUNET_ATS_Information *) &ats, 2,
1322 si->session->flow_delay_for_other_peer = delay;
1323 reschedule_session_timeout(si->session);
1329 * We've received a UDP Message. Process it (pass contents to main service).
1331 * @param plugin plugin context
1332 * @param msg the message
1333 * @param sender_addr sender address
1334 * @param sender_addr_len number of bytes in sender_addr
1337 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1338 const struct sockaddr *sender_addr,
1339 socklen_t sender_addr_len)
1341 struct SourceInformation si;
1343 struct IPv4UdpAddress u4;
1344 struct IPv6UdpAddress u6;
1348 if (0 != ntohl (msg->reserved))
1350 GNUNET_break_op (0);
1353 if (ntohs (msg->header.size) <
1354 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1356 GNUNET_break_op (0);
1360 /* convert address */
1361 switch (sender_addr->sa_family)
1364 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1365 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1366 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1371 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1372 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1373 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1381 LOG (GNUNET_ERROR_TYPE_DEBUG,
1382 "Received message with %u bytes from peer `%s' at `%s'\n",
1383 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1384 GNUNET_a2s (sender_addr, sender_addr_len));
1386 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1387 s = udp_plugin_get_session(plugin, address);
1388 GNUNET_free (address);
1390 /* iterate over all embedded messages */
1392 si.sender = msg->sender;
1396 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1397 ntohs (msg->header.size) -
1398 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1400 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1406 * Scan the heap for a receive context with the given address.
1408 * @param cls the 'struct FindReceiveContext'
1409 * @param node internal node of the heap
1410 * @param element value stored at the node (a 'struct ReceiveContext')
1411 * @param cost cost associated with the node
1412 * @return GNUNET_YES if we should continue to iterate,
1416 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1417 void *element, GNUNET_CONTAINER_HeapCostType cost)
1419 struct FindReceiveContext *frc = cls;
1420 struct DefragContext *e = element;
1422 if ((frc->addr_len == e->addr_len) &&
1423 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1433 * Process a defragmented message.
1435 * @param cls the 'struct ReceiveContext'
1436 * @param msg the message
1439 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1441 struct DefragContext *rc = cls;
1443 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1448 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1453 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1454 rc->src_addr, rc->addr_len);
1458 struct LookupContext
1460 const struct sockaddr * addr;
1462 struct Session *res;
1469 lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1471 struct LookupContext *l_ctx = cls;
1472 struct Session * s = value;
1474 if ((s->addrlen == l_ctx->addrlen) &&
1475 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1485 * Transmit an acknowledgement.
1487 * @param cls the 'struct ReceiveContext'
1488 * @param id message ID (unused)
1489 * @param msg ack to transmit
1492 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1494 struct DefragContext *rc = cls;
1495 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1496 struct UDP_ACK_Message *udp_ack;
1498 struct UDPMessageWrapper *udpw;
1501 struct LookupContext l_ctx;
1502 l_ctx.addr = rc->src_addr;
1503 l_ctx.addrlen = rc->addr_len;
1505 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1506 &lookup_session_by_addr_it,
1513 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1514 delay = s->flow_delay_for_other_peer.rel_value;
1516 LOG (GNUNET_ERROR_TYPE_DEBUG,
1517 "Sending ACK to `%s' including delay of %u ms\n",
1518 GNUNET_a2s (rc->src_addr,
1519 (rc->src_addr->sa_family ==
1520 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1523 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1525 udpw->cont_cls = NULL;
1526 udpw->frag_ctx = NULL;
1527 udpw->msg_size = msize;
1529 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1530 udpw->udp = (char *)&udpw[1];
1532 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1533 udp_ack->header.size = htons ((uint16_t) msize);
1534 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1535 udp_ack->delay = htonl (delay);
1536 udp_ack->sender = *rc->plugin->env->my_identity;
1537 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1539 enqueue (rc->plugin, udpw);
1544 read_process_msg (struct Plugin *plugin,
1545 const struct GNUNET_MessageHeader *msg,
1549 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1551 GNUNET_break_op (0);
1554 process_udp_message (plugin, (const struct UDPMessage *) msg,
1555 (const struct sockaddr *) addr, fromlen);
1560 read_process_ack (struct Plugin *plugin,
1561 const struct GNUNET_MessageHeader *msg,
1565 const struct GNUNET_MessageHeader *ack;
1566 const struct UDP_ACK_Message *udp_ack;
1567 struct LookupContext l_ctx;
1569 struct GNUNET_TIME_Relative flow_delay;
1571 if (ntohs (msg->size) <
1572 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1574 GNUNET_break_op (0);
1577 udp_ack = (const struct UDP_ACK_Message *) msg;
1578 l_ctx.addr = (const struct sockaddr *) addr;
1579 l_ctx.addrlen = fromlen;
1581 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1582 &lookup_session_by_addr_it,
1586 if ((s == NULL) || (s->frag_ctx == NULL))
1589 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1590 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1591 flow_delay.rel_value);
1592 s->flow_delay_from_other_peer =
1593 GNUNET_TIME_relative_to_absolute (flow_delay);
1595 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1596 if (ntohs (ack->size) !=
1597 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1599 GNUNET_break_op (0);
1603 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1605 LOG (GNUNET_ERROR_TYPE_DEBUG,
1606 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1607 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1608 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1612 LOG (GNUNET_ERROR_TYPE_DEBUG,
1613 "FULL MESSAGE ACKed\n",
1614 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1615 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1616 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1618 struct UDPMessageWrapper * udpw;
1619 struct UDPMessageWrapper * tmp;
1620 if (s->addrlen == sizeof (struct sockaddr_in6))
1622 udpw = plugin->ipv6_queue_head;
1623 while (NULL != udpw)
1626 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1628 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1634 if (s->addrlen == sizeof (struct sockaddr_in))
1636 udpw = plugin->ipv4_queue_head;
1640 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1642 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1649 if (s->frag_ctx->cont != NULL)
1651 LOG (GNUNET_ERROR_TYPE_DEBUG,
1652 "Calling continuation for fragmented message to `%s' with result %s\n",
1653 GNUNET_i2s (&s->target), "OK");
1654 s->frag_ctx->cont (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1657 GNUNET_free (s->frag_ctx);
1663 read_process_fragment (struct Plugin *plugin,
1664 const struct GNUNET_MessageHeader *msg,
1668 struct DefragContext *d_ctx;
1669 struct GNUNET_TIME_Absolute now;
1670 struct FindReceiveContext frc;
1673 frc.addr = (const struct sockaddr *) addr;
1674 frc.addr_len = fromlen;
1676 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1677 (unsigned int) ntohs (msg->size),
1678 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1679 /* Lookup existing receive context for this address */
1680 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1681 &find_receive_context,
1683 now = GNUNET_TIME_absolute_get ();
1688 /* Create a new defragmentation context */
1689 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1690 memcpy (&d_ctx[1], addr, fromlen);
1691 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1692 d_ctx->addr_len = fromlen;
1693 d_ctx->plugin = plugin;
1695 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1696 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1697 &fragment_msg_proc, &ack_proc);
1699 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1700 (GNUNET_CONTAINER_HeapCostType)
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "Created new defragmentation context for %u-byte fragment from `%s'\n",
1704 (unsigned int) ntohs (msg->size),
1705 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1709 LOG (GNUNET_ERROR_TYPE_DEBUG,
1710 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1711 (unsigned int) ntohs (msg->size),
1712 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1715 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1717 /* keep this 'rc' from expiring */
1718 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1719 (GNUNET_CONTAINER_HeapCostType)
1722 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1723 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1725 /* remove 'rc' that was inactive the longest */
1726 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1727 GNUNET_assert (NULL != d_ctx);
1728 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1729 GNUNET_free (d_ctx);
1735 * Read and process a message from the given socket.
1737 * @param plugin the overall plugin
1738 * @param rsock socket to read from
1741 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1745 char buf[65536] GNUNET_ALIGN;
1747 const struct GNUNET_MessageHeader *msg;
1749 fromlen = sizeof (addr);
1750 memset (&addr, 0, sizeof (addr));
1751 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1752 (struct sockaddr *) &addr, &fromlen);
1754 if (size < sizeof (struct GNUNET_MessageHeader))
1756 GNUNET_break_op (0);
1759 msg = (const struct GNUNET_MessageHeader *) buf;
1761 LOG (GNUNET_ERROR_TYPE_DEBUG,
1762 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1763 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1765 if (size != ntohs (msg->size))
1767 GNUNET_break_op (0);
1771 switch (ntohs (msg->type))
1773 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1774 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1777 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1778 read_process_msg (plugin, msg, addr, fromlen);
1781 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1782 read_process_ack (plugin, msg, addr, fromlen);
1785 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1786 read_process_fragment (plugin, msg, addr, fromlen);
1790 GNUNET_break_op (0);
1797 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
1801 struct GNUNET_TIME_Absolute max;
1802 struct UDPMessageWrapper *udpw = NULL;
1804 if (sock == plugin->sockv4)
1806 udpw = plugin->ipv4_queue_head;
1808 else if (sock == plugin->sockv6)
1810 udpw = plugin->ipv6_queue_head;
1818 const struct sockaddr * sa = udpw->session->sock_addr;
1819 slen = udpw->session->addrlen;
1821 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1823 while (udpw != NULL)
1825 if (max.abs_value != udpw->timeout.abs_value)
1827 /* Message timed out */
1828 call_continuation(udpw, GNUNET_SYSERR);
1829 if (udpw->frag_ctx != NULL)
1831 LOG (GNUNET_ERROR_TYPE_DEBUG,
1832 "Fragmented message for peer `%s' with size %u timed out\n",
1833 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1834 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1835 GNUNET_free (udpw->frag_ctx);
1836 udpw->session->frag_ctx = NULL;
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "Message for peer `%s' with size %u timed out\n",
1842 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1845 if (sock == plugin->sockv4)
1847 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1849 udpw = plugin->ipv4_queue_head;
1851 else if (sock == plugin->sockv6)
1853 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1855 udpw = plugin->ipv6_queue_head;
1860 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1861 if (delta.rel_value == 0)
1863 /* this message is not delayed */
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "Message for peer `%s' (%u bytes) is not delayed \n",
1866 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1871 /* this message is delayed, try next */
1872 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1874 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1883 /* No message left */
1887 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
1889 if (GNUNET_SYSERR == sent)
1891 const struct GNUNET_ATS_Information type = plugin->env->get_address_type
1892 (plugin->env->cls,sa, slen);
1894 if ((GNUNET_ATS_NET_WAN == type.value) &&
1895 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
1897 /* "Network unreachable" or "Network down" */
1899 * This indicates that this system is IPv6 enabled, but does not
1900 * have a valid global IPv6 address assigned
1902 LOG (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1903 _("UDP could not message to `%s': `%s'. "
1904 "Please check your network configuration and disable IPv6 if your "
1905 "connection does not have a global IPv6 address\n"),
1906 GNUNET_a2s (sa, slen),
1911 LOG (GNUNET_ERROR_TYPE_ERROR,
1912 "UDP could not transmit %u-byte message to `%s': `%s'\n",
1913 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
1916 call_continuation(udpw, GNUNET_SYSERR);
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
1922 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1923 (sent < 0) ? STRERROR (errno) : "ok");
1924 call_continuation(udpw, GNUNET_OK);
1927 if (sock == plugin->sockv4)
1928 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1929 else if (sock == plugin->sockv6)
1930 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1939 * We have been notified that our readset has something to read. We don't
1940 * know which socket needs to be read, so we have to check each one
1941 * Then reschedule this function to be called again once more is available.
1943 * @param cls the plugin handle
1944 * @param tc the scheduling context (for rescheduling this function again)
1947 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1949 struct Plugin *plugin = cls;
1951 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1952 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1954 plugin->with_v4_ws = GNUNET_NO;
1956 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1958 if ((NULL != plugin->sockv4) &&
1959 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1960 udp_select_read (plugin, plugin->sockv4);
1964 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1966 if ((NULL != plugin->sockv4) && (plugin->ipv4_queue_head != NULL) &&
1967 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
1969 udp_select_send (plugin, plugin->sockv4);
1973 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1974 GNUNET_SCHEDULER_cancel (plugin->select_task);
1975 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1976 GNUNET_TIME_UNIT_FOREVER_REL,
1978 (plugin->ipv4_queue_head != NULL) ? plugin->ws_v4 : NULL,
1979 &udp_plugin_select, plugin);
1980 if (plugin->ipv4_queue_head != NULL)
1981 plugin->with_v4_ws = GNUNET_YES;
1983 plugin->with_v4_ws = GNUNET_NO;
1988 * We have been notified that our readset has something to read. We don't
1989 * know which socket needs to be read, so we have to check each one
1990 * Then reschedule this function to be called again once more is available.
1992 * @param cls the plugin handle
1993 * @param tc the scheduling context (for rescheduling this function again)
1996 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1998 struct Plugin *plugin = cls;
2000 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2001 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2004 plugin->with_v6_ws = GNUNET_NO;
2005 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
2007 if ((NULL != plugin->sockv6) &&
2008 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
2009 udp_select_read (plugin, plugin->sockv6);
2012 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
2014 if ((NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2015 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)))
2017 udp_select_send (plugin, plugin->sockv6);
2020 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2021 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2022 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2023 GNUNET_TIME_UNIT_FOREVER_REL,
2025 (plugin->ipv6_queue_head != NULL) ? plugin->ws_v6 : NULL,
2026 &udp_plugin_select_v6, plugin);
2027 if (plugin->ipv6_queue_head != NULL)
2028 plugin->with_v6_ws = GNUNET_YES;
2030 plugin->with_v6_ws = GNUNET_NO;
2035 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
2038 int sockets_created = 0;
2039 struct sockaddr *serverAddr;
2040 struct sockaddr *addrs[2];
2041 socklen_t addrlens[2];
2044 /* Create IPv6 socket */
2045 if (plugin->enable_ipv6 == GNUNET_YES)
2047 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2048 if (NULL == plugin->sockv6)
2050 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2051 plugin->enable_ipv6 = GNUNET_NO;
2055 #if HAVE_SOCKADDR_IN_SIN_LEN
2056 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2058 serverAddrv6->sin6_family = AF_INET6;
2059 serverAddrv6->sin6_addr = in6addr_any;
2060 serverAddrv6->sin6_port = htons (plugin->port);
2061 addrlen = sizeof (struct sockaddr_in6);
2062 serverAddr = (struct sockaddr *) serverAddrv6;
2063 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2064 ntohs (serverAddrv6->sin6_port));
2066 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2069 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2070 LOG (GNUNET_ERROR_TYPE_DEBUG,
2071 "IPv6 Binding failed, trying new port %d\n",
2072 ntohs (serverAddrv6->sin6_port));
2076 GNUNET_NETWORK_socket_close (plugin->sockv6);
2077 plugin->sockv6 = NULL;
2081 if (plugin->sockv6 != NULL)
2083 LOG (GNUNET_ERROR_TYPE_DEBUG,
2084 "IPv6 socket created on port %d\n",
2085 ntohs (serverAddrv6->sin6_port));
2086 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2087 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2093 /* Create IPv4 socket */
2094 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2095 if (NULL == plugin->sockv4)
2097 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2101 #if HAVE_SOCKADDR_IN_SIN_LEN
2102 serverAddrv4->sin_len = sizeof (serverAddrv4);
2104 serverAddrv4->sin_family = AF_INET;
2105 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2106 serverAddrv4->sin_port = htons (plugin->port);
2107 addrlen = sizeof (struct sockaddr_in);
2108 serverAddr = (struct sockaddr *) serverAddrv4;
2110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2111 ntohs (serverAddrv4->sin_port));
2113 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2116 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2117 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2118 ntohs (serverAddrv4->sin_port));
2122 GNUNET_NETWORK_socket_close (plugin->sockv4);
2123 plugin->sockv4 = NULL;
2127 if (plugin->sockv4 != NULL)
2129 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2130 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2135 /* Create file descriptors */
2136 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2137 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2138 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2139 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2140 if (NULL != plugin->sockv4)
2142 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2143 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2146 if (sockets_created == 0)
2147 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2149 plugin->select_task =
2150 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2151 GNUNET_TIME_UNIT_FOREVER_REL,
2154 &udp_plugin_select, plugin);
2155 plugin->with_v4_ws = GNUNET_NO;
2157 if (plugin->enable_ipv6 == GNUNET_YES)
2159 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2160 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2161 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2162 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2163 if (NULL != plugin->sockv6)
2165 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2166 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2169 plugin->select_task_v6 =
2170 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
2171 GNUNET_TIME_UNIT_FOREVER_REL,
2174 &udp_plugin_select_v6, plugin);
2175 plugin->with_v6_ws = GNUNET_NO;
2178 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2179 GNUNET_NO, plugin->port,
2181 (const struct sockaddr **) addrs, addrlens,
2182 &udp_nat_port_map_callback, NULL, plugin);
2184 return sockets_created;
2188 * Session was idle, so disconnect it
2191 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2193 GNUNET_assert (NULL != cls);
2194 struct Session *s = cls;
2196 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p was idle for %llu, disconnecting\n",
2199 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2201 /* call session destroy function */
2202 disconnect_session(s);
2207 * Start session timeout
2210 start_session_timeout (struct Session *s)
2212 GNUNET_assert (NULL != s);
2213 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2215 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout for session %p set to %llu\n",
2220 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2224 * Increment session timeout due to activity
2227 reschedule_session_timeout (struct Session *s)
2229 GNUNET_assert (NULL != s);
2230 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2232 GNUNET_SCHEDULER_cancel (s->timeout_task);
2233 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout rescheduled for session %p set to %llu\n",
2238 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2245 stop_session_timeout (struct Session *s)
2247 GNUNET_assert (NULL != s);
2249 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2251 GNUNET_SCHEDULER_cancel (s->timeout_task);
2252 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout rescheduled for session %p canceled\n",
2255 s, GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Timeout for session %p was not active\n",
2265 * The exported method. Makes the core api available via a global and
2266 * returns the udp transport API.
2268 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2269 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2272 libgnunet_plugin_transport_udp_init (void *cls)
2274 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2275 struct GNUNET_TRANSPORT_PluginFunctions *api;
2277 unsigned long long port;
2278 unsigned long long aport;
2279 unsigned long long broadcast;
2280 unsigned long long udp_max_bps;
2281 unsigned long long enable_v6;
2282 char * bind4_address;
2283 char * bind6_address;
2284 struct GNUNET_TIME_Relative interval;
2285 struct sockaddr_in serverAddrv4;
2286 struct sockaddr_in6 serverAddrv6;
2289 if (NULL == env->receive)
2291 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2292 initialze the plugin or the API */
2293 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2295 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2296 api->address_to_string = &udp_address_to_string;
2297 api->string_to_address = &udp_string_to_address;
2301 GNUNET_assert( NULL != env->stats);
2303 /* Get port number */
2305 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2309 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2310 "ADVERTISED_PORT", &aport))
2314 LOG (GNUNET_ERROR_TYPE_WARNING,
2315 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2322 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2325 enable_v6 = GNUNET_NO;
2328 enable_v6 = GNUNET_YES;
2331 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2332 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2335 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2336 "BINDTO", &bind4_address))
2338 LOG (GNUNET_ERROR_TYPE_DEBUG,
2339 "Binding udp plugin to specific address: `%s'\n",
2341 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2343 GNUNET_free (bind4_address);
2349 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2350 "BINDTO6", &bind6_address))
2352 LOG (GNUNET_ERROR_TYPE_DEBUG,
2353 "Binding udp plugin to specific address: `%s'\n",
2356 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2358 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2360 GNUNET_free_non_null (bind4_address);
2361 GNUNET_free (bind6_address);
2366 /* Enable neighbour discovery */
2367 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2369 if (broadcast == GNUNET_SYSERR)
2370 broadcast = GNUNET_NO;
2372 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
2373 "BROADCAST_INTERVAL", &interval))
2375 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2378 /* Maximum datarate */
2379 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2380 "MAX_BPS", &udp_max_bps))
2382 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2385 p = GNUNET_malloc (sizeof (struct Plugin));
2386 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2388 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2389 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2390 p->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2391 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2392 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2395 p->broadcast_interval = interval;
2396 p->enable_ipv6 = enable_v6;
2403 api->disconnect = &udp_disconnect;
2404 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2405 api->address_to_string = &udp_address_to_string;
2406 api->string_to_address = &udp_string_to_address;
2407 api->check_address = &udp_plugin_check_address;
2408 api->get_session = &udp_plugin_get_session;
2409 api->send = &udp_plugin_send;
2411 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2412 res = setup_sockets (p, &serverAddrv6, &serverAddrv4);
2413 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
2415 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2421 if (broadcast == GNUNET_YES)
2423 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2424 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
2427 GNUNET_free_non_null (bind4_address);
2428 GNUNET_free_non_null (bind6_address);
2434 heap_cleanup_iterator (void *cls,
2435 struct GNUNET_CONTAINER_HeapNode *
2436 node, void *element,
2437 GNUNET_CONTAINER_HeapCostType
2440 struct DefragContext * d_ctx = element;
2442 GNUNET_CONTAINER_heap_remove_node (node);
2443 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2444 GNUNET_free (d_ctx);
2451 * The exported method. Makes the core api available via a global and
2452 * returns the udp transport API.
2454 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2458 libgnunet_plugin_transport_udp_done (void *cls)
2460 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2461 struct Plugin *plugin = api->cls;
2469 stop_broadcast (plugin);
2471 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2473 GNUNET_SCHEDULER_cancel (plugin->select_task);
2474 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2476 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2478 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2479 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2482 /* Closing sockets */
2483 if (plugin->sockv4 != NULL)
2485 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2486 plugin->sockv4 = NULL;
2488 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2489 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2491 if (plugin->sockv6 != NULL)
2493 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2494 plugin->sockv6 = NULL;
2496 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2497 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2500 GNUNET_NAT_unregister (plugin->nat);
2502 if (plugin->defrag_ctxs != NULL)
2504 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2505 heap_cleanup_iterator, NULL);
2506 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2507 plugin->defrag_ctxs = NULL;
2509 if (plugin->mst != NULL)
2511 GNUNET_SERVER_mst_destroy(plugin->mst);
2515 /* Clean up leftover messages */
2516 struct UDPMessageWrapper * udpw;
2517 udpw = plugin->ipv4_queue_head;
2518 while (udpw != NULL)
2520 struct UDPMessageWrapper *tmp = udpw->next;
2521 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
2522 call_continuation(udpw, GNUNET_SYSERR);
2526 udpw = plugin->ipv6_queue_head;
2527 while (udpw != NULL)
2529 struct UDPMessageWrapper *tmp = udpw->next;
2530 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2531 call_continuation(udpw, GNUNET_SYSERR);
2536 /* Clean up sessions */
2537 LOG (GNUNET_ERROR_TYPE_DEBUG,
2538 "Cleaning up sessions\n");
2539 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2540 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2543 GNUNET_free (plugin);
2549 /* end of plugin_transport_udp.c */