2 This file is part of GNUnet
3 Copyright (C) 2010-2015 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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__)
46 * After how much inactivity should a UDP session time out?
48 #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51 * Number of messages we can defragment in parallel. We only really
52 * defragment 1 message at a time, but if messages get re-ordered, we
53 * may want to keep knowledge about the previous message to avoid
54 * discarding the current message in favor of a single fragment of a
55 * previous message. 3 should be good since we don't expect massive
56 * message reorderings with UDP.
58 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
61 * We keep a defragmentation queue per sender address. How many
62 * sender addresses do we support at the same time? Memory consumption
63 * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
64 * value. (So 128 corresponds to 12 MB and should suffice for
65 * connecting to roughly 128 peers via UDP).
67 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
71 * UDP Message-Packet header (after defragmentation).
78 struct GNUNET_MessageHeader header;
81 * Always zero for now.
86 * What is the identity of the sender
88 struct GNUNET_PeerIdentity sender;
94 * Closure for #append_port().
96 struct PrettyPrinterContext
101 struct PrettyPrinterContext *next;
106 struct PrettyPrinterContext *prev;
111 struct Plugin *plugin;
116 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
119 * Function to call with the result.
121 GNUNET_TRANSPORT_AddressStringCallback asc;
124 * Clsoure for @e asc.
131 struct GNUNET_SCHEDULER_Task *timeout_task;
134 * Is this an IPv6 address?
144 * Port to add after the IP address.
152 * Session with another peer.
157 * Which peer is this session for?
159 struct GNUNET_PeerIdentity target;
162 * Plugin this session belongs to.
164 struct Plugin *plugin;
167 * Context for dealing with fragments.
169 struct UDP_FragmentationContext *frag_ctx;
172 * Desired delay for next sending we send to other peer
174 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
177 * Desired delay for next sending we received from other peer
179 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
182 * Session timeout task
184 struct GNUNET_SCHEDULER_Task *timeout_task;
187 * When does this session time out?
189 struct GNUNET_TIME_Absolute timeout;
192 * expected delay for ACKs
194 struct GNUNET_TIME_Relative last_expected_ack_delay;
197 * desired delay between UDP messages
199 struct GNUNET_TIME_Relative last_expected_msg_delay;
204 struct GNUNET_HELLO_Address *address;
207 * Number of bytes waiting for transmission to this peer.
209 unsigned long long bytes_in_queue;
212 * Number of messages waiting for transmission to this peer.
214 unsigned int msgs_in_queue;
217 * Reference counter to indicate that this session is
218 * currently being used and must not be destroyed;
219 * setting @e in_destroy will destroy it as soon as
225 * Network type of the address.
227 enum GNUNET_ATS_Network_Type scope;
230 * Is this session about to be destroyed (sometimes we cannot
231 * destroy a session immediately as below us on the stack
232 * there might be code that still uses it; in this case,
233 * @e rc is non-zero).
241 * Data structure to track defragmentation contexts based
242 * on the source of the UDP traffic.
248 * Defragmentation context.
250 struct GNUNET_DEFRAGMENT_Context *defrag;
253 * Reference to master plugin struct.
255 struct Plugin *plugin;
258 * Node in the defrag heap.
260 struct GNUNET_CONTAINER_HeapNode *hnode;
263 * Source address this receive context is for (allocated at the
264 * end of the struct).
266 const union UdpAddress *udp_addr;
269 * Who's message(s) are we defragmenting here?
270 * Only initialized once we succeeded and
271 * @e have_sender is set.
273 struct GNUNET_PeerIdentity sender;
276 * Length of @e udp_addr.
281 * Network type the address belongs to.
283 enum GNUNET_ATS_Network_Type network_type;
286 * Has the @e sender field been initialized yet?
293 * Context to send fragmented messages
295 struct UDP_FragmentationContext
298 * Next in linked list
300 struct UDP_FragmentationContext *next;
303 * Previous in linked list
305 struct UDP_FragmentationContext *prev;
310 struct Plugin *plugin;
313 * Handle for fragmentation.
315 struct GNUNET_FRAGMENT_Context *frag;
318 * The session this fragmentation context belongs to
320 struct Session *session;
323 * Function to call upon completion of the transmission.
325 GNUNET_TRANSPORT_TransmitContinuation cont;
328 * Closure for @e cont.
335 struct GNUNET_TIME_Absolute timeout;
338 * Payload size of original unfragmented message
343 * Bytes used to send all fragments on wire including UDP overhead
351 * Function called when a message is removed from the
352 * transmission queue.
355 * @param udpw message wrapper finished
356 * @param result #GNUNET_OK on success (message was sent)
357 * #GNUNET_SYSERR if the target disconnected
358 * or we had a timeout or other trouble sending
361 (*QueueContinuation) (void *cls,
362 struct UDP_MessageWrapper *udpw,
367 * Information we track for each message in the queue.
369 struct UDP_MessageWrapper
372 * Session this message belongs to
374 struct Session *session;
377 * DLL of messages, previous element
379 struct UDP_MessageWrapper *prev;
382 * DLL of messages, next element
384 struct UDP_MessageWrapper *next;
387 * Message with @e msg_size bytes including UDP-specific overhead.
392 * Function to call once the message wrapper is being removed
393 * from the queue (with success or failure).
395 QueueContinuation qc;
403 * External continuation to call upon completion of the
404 * transmission, NULL if this queue entry is not for a
405 * message from the application.
407 GNUNET_TRANSPORT_TransmitContinuation cont;
410 * Closure for @e cont.
415 * Fragmentation context.
416 * frag_ctx == NULL if transport <= MTU
417 * frag_ctx != NULL if transport > MTU
419 struct UDP_FragmentationContext *frag_ctx;
424 struct GNUNET_TIME_Absolute timeout;
427 * Size of UDP message to send, including UDP-specific overhead.
432 * Payload size of original message.
439 GNUNET_NETWORK_STRUCT_BEGIN
442 * UDP ACK Message-Packet header.
444 struct UDP_ACK_Message
449 struct GNUNET_MessageHeader header;
452 * Desired delay for flow control, in us (in NBO).
454 uint32_t delay GNUNET_PACKED;
457 * What is the identity of the sender
459 struct GNUNET_PeerIdentity sender;
463 GNUNET_NETWORK_STRUCT_END
466 /* ************************* Monitoring *********** */
470 * If a session monitor is attached, notify it about the new
473 * @param plugin our plugin
474 * @param session session that changed state
475 * @param state new state of the session
478 notify_session_monitor (struct Plugin *plugin,
479 struct Session *session,
480 enum GNUNET_TRANSPORT_SessionState state)
482 struct GNUNET_TRANSPORT_SessionInfo info;
484 if (NULL == plugin->sic)
486 if (GNUNET_YES == session->in_destroy)
487 return; /* already destroyed, just RC>0 left-over actions */
492 info.is_inbound = GNUNET_SYSERR; /* hard to say */
493 info.num_msg_pending = session->msgs_in_queue;
494 info.num_bytes_pending = session->bytes_in_queue;
495 /* info.receive_delay remains zero as this is not supported by UDP
496 (cannot selectively not receive from 'some' peer while continuing
497 to receive from others) */
498 info.session_timeout = session->timeout;
499 info.address = session->address;
500 plugin->sic (plugin->sic_cls,
507 * Return information about the given session to the monitor callback.
509 * @param cls the `struct Plugin` with the monitor callback (`sic`)
510 * @param peer peer we send information about
511 * @param value our `struct Session` to send information about
512 * @return #GNUNET_OK (continue to iterate)
515 send_session_info_iter (void *cls,
516 const struct GNUNET_PeerIdentity *peer,
519 struct Plugin *plugin = cls;
520 struct Session *session = value;
522 notify_session_monitor (plugin,
524 GNUNET_TRANSPORT_SS_INIT);
525 notify_session_monitor (plugin,
527 GNUNET_TRANSPORT_SS_UP);
533 * Begin monitoring sessions of a plugin. There can only
534 * be one active monitor per plugin (i.e. if there are
535 * multiple monitors, the transport service needs to
536 * multiplex the generated events over all of them).
538 * @param cls closure of the plugin
539 * @param sic callback to invoke, NULL to disable monitor;
540 * plugin will being by iterating over all active
541 * sessions immediately and then enter monitor mode
542 * @param sic_cls closure for @a sic
545 udp_plugin_setup_monitor (void *cls,
546 GNUNET_TRANSPORT_SessionInfoCallback sic,
549 struct Plugin *plugin = cls;
552 plugin->sic_cls = sic_cls;
555 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
556 &send_session_info_iter,
558 /* signal end of first iteration */
566 /* ****************** Little Helpers ****************** */
570 * Function to free last resources associated with a session.
572 * @param s session to free
575 free_session (struct Session *s)
577 if (NULL != s->address)
579 GNUNET_HELLO_address_free (s->address);
582 if (NULL != s->frag_ctx)
584 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
587 GNUNET_free (s->frag_ctx);
595 * Function that is called to get the keepalive factor.
596 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
597 * calculate the interval between keepalive packets.
599 * @param cls closure with the `struct Plugin`
600 * @return keepalive factor
603 udp_query_keepalive_factor (void *cls)
610 * Function obtain the network type for a session
612 * @param cls closure (`struct Plugin *`)
613 * @param session the session
614 * @return the network type
616 static enum GNUNET_ATS_Network_Type
617 udp_get_network (void *cls,
618 struct Session *session)
620 return session->scope;
624 /* ******************* Event loop ******************** */
627 * We have been notified that our readset has something to read. We don't
628 * know which socket needs to be read, so we have to check each one
629 * Then reschedule this function to be called again once more is available.
631 * @param cls the plugin handle
632 * @param tc the scheduling context (for rescheduling this function again)
635 udp_plugin_select_v4 (void *cls,
636 const struct GNUNET_SCHEDULER_TaskContext *tc);
640 * We have been notified that our readset has something to read. We don't
641 * know which socket needs to be read, so we have to check each one
642 * Then reschedule this function to be called again once more is available.
644 * @param cls the plugin handle
645 * @param tc the scheduling context (for rescheduling this function again)
648 udp_plugin_select_v6 (void *cls,
649 const struct GNUNET_SCHEDULER_TaskContext *tc);
653 * (re)schedule IPv4-select tasks for this plugin.
655 * @param plugin plugin to reschedule
658 schedule_select_v4 (struct Plugin *plugin)
660 struct GNUNET_TIME_Relative min_delay;
661 struct UDP_MessageWrapper *udpw;
663 if ( (GNUNET_YES == plugin->enable_ipv4) &&
664 (NULL != plugin->sockv4) )
666 /* Find a message ready to send:
667 * Flow delay from other peer is expired or not set (0) */
668 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
669 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
670 min_delay = GNUNET_TIME_relative_min (min_delay,
671 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
672 if (NULL != plugin->select_task_v4)
673 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
674 plugin->select_task_v4
675 = GNUNET_SCHEDULER_add_read_net (min_delay,
677 &udp_plugin_select_v4,
684 * (re)schedule IPv6-select tasks for this plugin.
686 * @param plugin plugin to reschedule
689 schedule_select_v6 (struct Plugin *plugin)
691 struct GNUNET_TIME_Relative min_delay;
692 struct UDP_MessageWrapper *udpw;
694 if ( (GNUNET_YES == plugin->enable_ipv6) &&
695 (NULL != plugin->sockv6) )
697 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
698 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
699 min_delay = GNUNET_TIME_relative_min (min_delay,
700 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
701 if (NULL != plugin->select_task_v6)
702 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
703 plugin->select_task_v6
704 = GNUNET_SCHEDULER_add_read_net (min_delay,
706 &udp_plugin_select_v6,
712 /* ******************* Address to string and back ***************** */
716 * Function called for a quick conversion of the binary address to
717 * a numeric address. Note that the caller must not free the
718 * address and that the next call to this function is allowed
719 * to override the address again.
722 * @param addr binary address (a `union UdpAddress`)
723 * @param addrlen length of the @a addr
724 * @return string representing the same address
727 udp_address_to_string (void *cls,
731 static char rbuf[INET6_ADDRSTRLEN + 10];
732 char buf[INET6_ADDRSTRLEN];
736 const struct IPv4UdpAddress *t4;
737 const struct IPv6UdpAddress *t6;
748 if (addrlen == sizeof(struct IPv6UdpAddress))
752 options = ntohl (t6->options);
753 port = ntohs (t6->u6_port);
757 else if (addrlen == sizeof(struct IPv4UdpAddress))
761 options = ntohl (t4->options);
762 port = ntohs (t4->u4_port);
763 a4.s_addr = t4->ipv4_addr;
775 GNUNET_snprintf (rbuf,
789 * Function called to convert a string address to a binary address.
791 * @param cls closure (`struct Plugin *`)
792 * @param addr string address
793 * @param addrlen length of the address
794 * @param buf location to store the buffer
795 * @param added location to store the number of bytes in the buffer.
796 * If the function returns #GNUNET_SYSERR, its contents are undefined.
797 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
800 udp_string_to_address (void *cls,
806 struct sockaddr_storage socket_address;
812 /* Format tcp.options.address:port */
817 if ((NULL == addr) || (0 == addrlen))
820 return GNUNET_SYSERR;
822 if ('\0' != addr[addrlen - 1])
825 return GNUNET_SYSERR;
827 if (strlen (addr) != addrlen - 1)
830 return GNUNET_SYSERR;
832 plugin = GNUNET_strdup (addr);
833 optionstr = strchr (plugin, '.');
834 if (NULL == optionstr)
837 GNUNET_free (plugin);
838 return GNUNET_SYSERR;
842 options = atol (optionstr);
843 address = strchr (optionstr, '.');
847 GNUNET_free (plugin);
848 return GNUNET_SYSERR;
854 GNUNET_STRINGS_to_address_ip (address,
859 GNUNET_free (plugin);
860 return GNUNET_SYSERR;
864 switch (socket_address.ss_family)
868 struct IPv4UdpAddress *u4;
869 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
871 u4 = GNUNET_new (struct IPv4UdpAddress);
872 u4->options = htonl (options);
873 u4->ipv4_addr = in4->sin_addr.s_addr;
874 u4->u4_port = in4->sin_port;
876 *added = sizeof (struct IPv4UdpAddress);
881 struct IPv6UdpAddress *u6;
882 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
884 u6 = GNUNET_new (struct IPv6UdpAddress);
885 u6->options = htonl (options);
886 u6->ipv6_addr = in6->sin6_addr;
887 u6->u6_port = in6->sin6_port;
889 *added = sizeof (struct IPv6UdpAddress);
894 return GNUNET_SYSERR;
900 * Append our port and forward the result.
902 * @param cls a `struct PrettyPrinterContext *`
903 * @param hostname result from DNS resolver
906 append_port (void *cls,
907 const char *hostname)
909 struct PrettyPrinterContext *ppc = cls;
910 struct Plugin *plugin = ppc->plugin;
913 if (NULL == hostname)
915 /* Final call, done */
916 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
917 plugin->ppc_dll_tail,
919 ppc->resolver_handle = NULL;
920 ppc->asc (ppc->asc_cls,
926 if (GNUNET_YES == ppc->ipv6)
927 GNUNET_asprintf (&ret,
934 GNUNET_asprintf (&ret,
940 ppc->asc (ppc->asc_cls,
948 * Convert the transports address to a nice, human-readable format.
950 * @param cls closure with the `struct Plugin *`
951 * @param type name of the transport that generated the address
952 * @param addr one of the addresses of the host, NULL for the last address
953 * the specific address format depends on the transport;
954 * a `union UdpAddress`
955 * @param addrlen length of the address
956 * @param numeric should (IP) addresses be displayed in numeric form?
957 * @param timeout after how long should we give up?
958 * @param asc function to call on each string
959 * @param asc_cls closure for @a asc
962 udp_plugin_address_pretty_printer (void *cls,
967 struct GNUNET_TIME_Relative timeout,
968 GNUNET_TRANSPORT_AddressStringCallback asc,
971 struct Plugin *plugin = cls;
972 struct PrettyPrinterContext *ppc;
973 const struct sockaddr *sb;
975 struct sockaddr_in a4;
976 struct sockaddr_in6 a6;
977 const struct IPv4UdpAddress *u4;
978 const struct IPv6UdpAddress *u6;
982 if (addrlen == sizeof(struct IPv6UdpAddress))
988 a6.sin6_family = AF_INET6;
989 #if HAVE_SOCKADDR_IN_SIN_LEN
990 a6.sin6_len = sizeof (a6);
992 a6.sin6_port = u6->u6_port;
993 a6.sin6_addr = u6->ipv6_addr;
994 port = ntohs (u6->u6_port);
995 options = ntohl (u6->options);
996 sb = (const struct sockaddr *) &a6;
999 else if (addrlen == sizeof (struct IPv4UdpAddress))
1005 a4.sin_family = AF_INET;
1006 #if HAVE_SOCKADDR_IN_SIN_LEN
1007 a4.sin_len = sizeof (a4);
1009 a4.sin_port = u4->u4_port;
1010 a4.sin_addr.s_addr = u4->ipv4_addr;
1011 port = ntohs (u4->u4_port);
1012 options = ntohl (u4->options);
1013 sb = (const struct sockaddr *) &a4;
1018 /* invalid address */
1019 GNUNET_break_op (0);
1028 ppc = GNUNET_new (struct PrettyPrinterContext);
1029 ppc->plugin = plugin;
1031 ppc->asc_cls = asc_cls;
1033 ppc->options = options;
1034 if (addrlen == sizeof (struct IPv6UdpAddress))
1035 ppc->ipv6 = GNUNET_YES;
1037 ppc->ipv6 = GNUNET_NO;
1038 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1039 plugin->ppc_dll_tail,
1041 ppc->resolver_handle
1042 = GNUNET_RESOLVER_hostname_get (sb,
1052 * Check if the given port is plausible (must be either our listen
1053 * port or our advertised port). If it is neither, we return
1056 * @param plugin global variables
1057 * @param in_port port number to check
1058 * @return #GNUNET_OK if port is either our open or advertised port
1061 check_port (const struct Plugin *plugin,
1064 if ( (plugin->port == in_port) ||
1065 (plugin->aport == in_port) )
1067 return GNUNET_SYSERR;
1072 * Function that will be called to check if a binary address for this
1073 * plugin is well-formed and corresponds to an address for THIS peer
1074 * (as per our configuration). Naturally, if absolutely necessary,
1075 * plugins can be a bit conservative in their answer, but in general
1076 * plugins should make sure that the address does not redirect
1077 * traffic to a 3rd party that might try to man-in-the-middle our
1080 * @param cls closure, should be our handle to the Plugin
1081 * @param addr pointer to a `union UdpAddress`
1082 * @param addrlen length of @a addr
1083 * @return #GNUNET_OK if this is a plausible address for this peer
1084 * and transport, #GNUNET_SYSERR if not
1087 udp_plugin_check_address (void *cls,
1091 struct Plugin *plugin = cls;
1092 const struct IPv4UdpAddress *v4;
1093 const struct IPv6UdpAddress *v6;
1095 if (sizeof(struct IPv4UdpAddress) == addrlen)
1097 v4 = (const struct IPv4UdpAddress *) addr;
1098 if (GNUNET_OK != check_port (plugin,
1099 ntohs (v4->u4_port)))
1100 return GNUNET_SYSERR;
1102 GNUNET_NAT_test_address (plugin->nat,
1104 sizeof (struct in_addr)))
1105 return GNUNET_SYSERR;
1107 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1109 v6 = (const struct IPv6UdpAddress *) addr;
1110 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1112 GNUNET_break_op (0);
1113 return GNUNET_SYSERR;
1115 if (GNUNET_OK != check_port (plugin,
1116 ntohs (v6->u6_port)))
1117 return GNUNET_SYSERR;
1119 GNUNET_NAT_test_address (plugin->nat,
1121 sizeof (struct in6_addr)))
1122 return GNUNET_SYSERR;
1126 GNUNET_break_op (0);
1127 return GNUNET_SYSERR;
1134 * Our external IP address/port mapping has changed.
1136 * @param cls closure, the `struct Plugin`
1137 * @param add_remove #GNUNET_YES to mean the new public IP address,
1138 * #GNUNET_NO to mean the previous (now invalid) one
1139 * @param addr either the previous or the new public IP address
1140 * @param addrlen actual length of the @a addr
1143 udp_nat_port_map_callback (void *cls,
1145 const struct sockaddr *addr,
1148 struct Plugin *plugin = cls;
1149 struct GNUNET_HELLO_Address *address;
1150 struct IPv4UdpAddress u4;
1151 struct IPv6UdpAddress u6;
1155 LOG (GNUNET_ERROR_TYPE_DEBUG,
1156 (GNUNET_YES == add_remove)
1157 ? "NAT notification to add address `%s'\n"
1158 : "NAT notification to remove address `%s'\n",
1161 /* convert 'address' to our internal format */
1162 switch (addr->sa_family)
1166 const struct sockaddr_in *i4;
1168 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1169 i4 = (const struct sockaddr_in *) addr;
1170 if (0 == ntohs (i4->sin_port))
1178 u4.options = htonl (plugin->myoptions);
1179 u4.ipv4_addr = i4->sin_addr.s_addr;
1180 u4.u4_port = i4->sin_port;
1182 args = sizeof (struct IPv4UdpAddress);
1187 const struct sockaddr_in6 *i6;
1189 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1190 i6 = (const struct sockaddr_in6 *) addr;
1191 if (0 == ntohs (i6->sin6_port))
1199 u6.options = htonl (plugin->myoptions);
1200 u6.ipv6_addr = i6->sin6_addr;
1201 u6.u6_port = i6->sin6_port;
1203 args = sizeof (struct IPv6UdpAddress);
1210 /* modify our published address list */
1211 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1215 GNUNET_HELLO_ADDRESS_INFO_NONE);
1216 plugin->env->notify_address (plugin->env->cls,
1219 GNUNET_HELLO_address_free (address);
1223 /* ********************* Finding sessions ******************* */
1227 * Closure for #session_cmp_it().
1229 struct SessionCompareContext
1232 * Set to session matching the address.
1234 struct Session *res;
1237 * Address we are looking for.
1239 const struct GNUNET_HELLO_Address *address;
1244 * Find a session with a matching address.
1246 * @param cls the `struct SessionCompareContext *`
1247 * @param key peer identity (unused)
1248 * @param value the `struct Session *`
1249 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1252 session_cmp_it (void *cls,
1253 const struct GNUNET_PeerIdentity *key,
1256 struct SessionCompareContext *cctx = cls;
1257 struct Session *s = value;
1259 if (0 == GNUNET_HELLO_address_cmp (s->address,
1262 GNUNET_assert (GNUNET_NO == s->in_destroy);
1271 * Locate an existing session the transport service is using to
1272 * send data to another peer. Performs some basic sanity checks
1273 * on the address and then tries to locate a matching session.
1275 * @param cls the plugin
1276 * @param address the address we should locate the session by
1277 * @return the session if it exists, or NULL if it is not found
1279 static struct Session *
1280 udp_plugin_lookup_session (void *cls,
1281 const struct GNUNET_HELLO_Address *address)
1283 struct Plugin *plugin = cls;
1284 const struct IPv6UdpAddress *udp_a6;
1285 const struct IPv4UdpAddress *udp_a4;
1286 struct SessionCompareContext cctx;
1288 if (NULL == address->address)
1293 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1295 if (NULL == plugin->sockv4)
1297 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1298 if (0 == udp_a4->u4_port)
1304 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1306 if (NULL == plugin->sockv6)
1308 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1309 if (0 == udp_a6->u6_port)
1321 /* check if session already exists */
1322 cctx.address = address;
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "Looking for existing session for peer `%s' with address `%s'\n",
1326 GNUNET_i2s (&address->peer),
1327 udp_address_to_string (plugin,
1329 address->address_length));
1330 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1334 if (NULL == cctx.res)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG,
1337 "Found existing session %p\n",
1343 /* ********************** Timeout ****************** */
1347 * Increment session timeout due to activity.
1349 * @param s session to reschedule timeout activity for
1352 reschedule_session_timeout (struct Session *s)
1354 if (GNUNET_YES == s->in_destroy)
1356 GNUNET_assert (NULL != s->timeout_task);
1357 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1363 * Function that will be called whenever the transport service wants to
1364 * notify the plugin that a session is still active and in use and
1365 * therefore the session timeout for this session has to be updated
1367 * @param cls closure with the `struct Plugin`
1368 * @param peer which peer was the session for
1369 * @param session which session is being updated
1372 udp_plugin_update_session_timeout (void *cls,
1373 const struct GNUNET_PeerIdentity *peer,
1374 struct Session *session)
1376 struct Plugin *plugin = cls;
1379 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1386 /* Reschedule session timeout */
1387 reschedule_session_timeout (session);
1391 /* ************************* Sending ************************ */
1395 * Remove the given message from the transmission queue and
1396 * update all applicable statistics.
1398 * @param plugin the UDP plugin
1399 * @param udpw message wrapper to dequeue
1402 dequeue (struct Plugin *plugin,
1403 struct UDP_MessageWrapper *udpw)
1405 struct Session *session = udpw->session;
1407 if (plugin->bytes_in_buffer < udpw->msg_size)
1413 GNUNET_STATISTICS_update (plugin->env->stats,
1414 "# UDP, total bytes in send buffers",
1415 - (long long) udpw->msg_size,
1417 plugin->bytes_in_buffer -= udpw->msg_size;
1419 GNUNET_STATISTICS_update (plugin->env->stats,
1420 "# UDP, total messages in send buffers",
1423 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1425 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1426 plugin->ipv4_queue_tail,
1429 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1431 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1432 plugin->ipv6_queue_tail,
1440 GNUNET_assert (session->msgs_in_queue > 0);
1441 session->msgs_in_queue--;
1442 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1443 session->bytes_in_queue -= udpw->msg_size;
1448 * Enqueue a message for transmission and update statistics.
1450 * @param plugin the UDP plugin
1451 * @param udpw message wrapper to queue
1454 enqueue (struct Plugin *plugin,
1455 struct UDP_MessageWrapper *udpw)
1457 struct Session *session = udpw->session;
1459 if (GNUNET_YES == session->in_destroy)
1464 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1470 GNUNET_STATISTICS_update (plugin->env->stats,
1471 "# UDP, total bytes in send buffers",
1474 plugin->bytes_in_buffer += udpw->msg_size;
1476 GNUNET_STATISTICS_update (plugin->env->stats,
1477 "# UDP, total messages in send buffers",
1480 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1482 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1483 plugin->ipv4_queue_tail,
1486 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1488 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1489 plugin->ipv6_queue_tail,
1495 udpw->cont (udpw->cont_cls,
1503 session->msgs_in_queue++;
1504 session->bytes_in_queue += udpw->msg_size;
1509 * We have completed our (attempt) to transmit a message that had to
1510 * be fragmented -- either because we got an ACK saying that all
1511 * fragments were received, or because of timeout / disconnect. Clean
1514 * @param frag_ctx fragmentation context to clean up
1515 * @param result #GNUNET_OK if we succeeded (got ACK),
1516 * #GNUNET_SYSERR if the transmission failed
1519 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1522 struct Plugin *plugin = frag_ctx->plugin;
1523 struct Session *s = frag_ctx->session;
1524 struct UDP_MessageWrapper *udpw;
1525 struct UDP_MessageWrapper *tmp;
1528 LOG (GNUNET_ERROR_TYPE_DEBUG,
1529 "%p: Fragmented message removed with result %s\n",
1531 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1532 /* Call continuation for fragmented message */
1533 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1534 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1536 overhead = frag_ctx->on_wire_size;
1537 if (NULL != frag_ctx->cont)
1538 frag_ctx->cont (frag_ctx->cont_cls,
1541 s->frag_ctx->payload_size,
1542 frag_ctx->on_wire_size);
1543 GNUNET_STATISTICS_update (plugin->env->stats,
1544 "# UDP, fragmented messages active",
1548 if (GNUNET_OK == result)
1550 GNUNET_STATISTICS_update (plugin->env->stats,
1551 "# UDP, fragmented msgs, messages, sent, success",
1554 GNUNET_STATISTICS_update (plugin->env->stats,
1555 "# UDP, fragmented msgs, bytes payload, sent, success",
1556 s->frag_ctx->payload_size,
1558 GNUNET_STATISTICS_update (plugin->env->stats,
1559 "# UDP, fragmented msgs, bytes overhead, sent, success",
1562 GNUNET_STATISTICS_update (plugin->env->stats,
1563 "# UDP, total, bytes overhead, sent",
1566 GNUNET_STATISTICS_update (plugin->env->stats,
1567 "# UDP, total, bytes payload, sent",
1568 s->frag_ctx->payload_size,
1573 GNUNET_STATISTICS_update (plugin->env->stats,
1574 "# UDP, fragmented msgs, messages, sent, failure",
1577 GNUNET_STATISTICS_update (plugin->env->stats,
1578 "# UDP, fragmented msgs, bytes payload, sent, failure",
1579 s->frag_ctx->payload_size,
1581 GNUNET_STATISTICS_update (plugin->env->stats,
1582 "# UDP, fragmented msgs, bytes payload, sent, failure",
1585 GNUNET_STATISTICS_update (plugin->env->stats,
1586 "# UDP, fragmented msgs, bytes payload, sent, failure",
1591 /* Remove remaining fragments from queue, no need to transmit those
1593 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1595 udpw = plugin->ipv6_queue_head;
1596 while (NULL != udpw)
1599 if ( (udpw->frag_ctx != NULL) &&
1600 (udpw->frag_ctx == frag_ctx) )
1609 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1611 udpw = plugin->ipv4_queue_head;
1612 while (NULL != udpw)
1615 if ( (NULL != udpw->frag_ctx) &&
1616 (udpw->frag_ctx == frag_ctx) )
1625 notify_session_monitor (s->plugin,
1627 GNUNET_TRANSPORT_SS_UPDATE);
1628 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1629 &s->last_expected_msg_delay,
1630 &s->last_expected_ack_delay);
1632 GNUNET_free (frag_ctx);
1637 * We are finished with a fragment in the message queue.
1638 * Notify the continuation and update statistics.
1640 * @param cls the `struct Plugin *`
1641 * @param udpw the queue entry
1642 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1645 qc_fragment_sent (void *cls,
1646 struct UDP_MessageWrapper *udpw,
1649 struct Plugin *plugin = cls;
1651 GNUNET_assert (NULL != udpw->frag_ctx);
1652 if (GNUNET_OK == result)
1654 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1655 GNUNET_STATISTICS_update (plugin->env->stats,
1656 "# UDP, fragmented msgs, fragments, sent, success",
1659 GNUNET_STATISTICS_update (plugin->env->stats,
1660 "# UDP, fragmented msgs, fragments bytes, sent, success",
1666 fragmented_message_done (udpw->frag_ctx,
1668 GNUNET_STATISTICS_update (plugin->env->stats,
1669 "# UDP, fragmented msgs, fragments, sent, failure",
1672 GNUNET_STATISTICS_update (plugin->env->stats,
1673 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1681 * Function that is called with messages created by the fragmentation
1682 * module. In the case of the `proc` callback of the
1683 * #GNUNET_FRAGMENT_context_create() function, this function must
1684 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1686 * @param cls closure, the `struct UDP_FragmentationContext`
1687 * @param msg the message that was created
1690 enqueue_fragment (void *cls,
1691 const struct GNUNET_MessageHeader *msg)
1693 struct UDP_FragmentationContext *frag_ctx = cls;
1694 struct Plugin *plugin = frag_ctx->plugin;
1695 struct UDP_MessageWrapper *udpw;
1696 struct Session *session = frag_ctx->session;
1697 size_t msg_len = ntohs (msg->size);
1699 LOG (GNUNET_ERROR_TYPE_DEBUG,
1700 "Enqueuing fragment with %u bytes\n",
1702 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1703 udpw->session = session;
1704 udpw->msg_buf = (char *) &udpw[1];
1705 udpw->msg_size = msg_len;
1706 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1707 udpw->timeout = frag_ctx->timeout;
1708 udpw->frag_ctx = frag_ctx;
1709 udpw->qc = &qc_fragment_sent;
1710 udpw->qc_cls = plugin;
1711 memcpy (udpw->msg_buf,
1716 if (sizeof (struct IPv4UdpAddress) == session->address->address_length)
1717 schedule_select_v4 (plugin);
1719 schedule_select_v6 (plugin);
1724 * We are finished with a message from the message queue.
1725 * Notify the continuation and update statistics.
1727 * @param cls the `struct Plugin *`
1728 * @param udpw the queue entry
1729 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1732 qc_message_sent (void *cls,
1733 struct UDP_MessageWrapper *udpw,
1736 struct Plugin *plugin = cls;
1739 if (udpw->msg_size >= udpw->payload_size)
1740 overhead = udpw->msg_size - udpw->payload_size;
1742 overhead = udpw->msg_size;
1744 if (NULL != udpw->cont)
1745 udpw->cont (udpw->cont_cls,
1746 &udpw->session->target,
1750 if (GNUNET_OK == result)
1752 GNUNET_STATISTICS_update (plugin->env->stats,
1753 "# UDP, unfragmented msgs, messages, sent, success",
1756 GNUNET_STATISTICS_update (plugin->env->stats,
1757 "# UDP, unfragmented msgs, bytes payload, sent, success",
1760 GNUNET_STATISTICS_update (plugin->env->stats,
1761 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1764 GNUNET_STATISTICS_update (plugin->env->stats,
1765 "# UDP, total, bytes overhead, sent",
1768 GNUNET_STATISTICS_update (plugin->env->stats,
1769 "# UDP, total, bytes payload, sent",
1775 GNUNET_STATISTICS_update (plugin->env->stats,
1776 "# UDP, unfragmented msgs, messages, sent, failure",
1779 GNUNET_STATISTICS_update (plugin->env->stats,
1780 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1783 GNUNET_STATISTICS_update (plugin->env->stats,
1784 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1792 * Function that can be used by the transport service to transmit a
1793 * message using the plugin. Note that in the case of a peer
1794 * disconnecting, the continuation MUST be called prior to the
1795 * disconnect notification itself. This function will be called with
1796 * this peer's HELLO message to initiate a fresh connection to another
1799 * @param cls closure
1800 * @param s which session must be used
1801 * @param msgbuf the message to transmit
1802 * @param msgbuf_size number of bytes in @a msgbuf
1803 * @param priority how important is the message (most plugins will
1804 * ignore message priority and just FIFO)
1805 * @param to how long to wait at most for the transmission (does not
1806 * require plugins to discard the message after the timeout,
1807 * just advisory for the desired delay; most plugins will ignore
1809 * @param cont continuation to call once the message has
1810 * been transmitted (or if the transport is ready
1811 * for the next transmission call; or if the
1812 * peer disconnected...); can be NULL
1813 * @param cont_cls closure for @a cont
1814 * @return number of bytes used (on the physical network, with overheads);
1815 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1816 * and does NOT mean that the message was not transmitted (DV)
1819 udp_plugin_send (void *cls,
1823 unsigned int priority,
1824 struct GNUNET_TIME_Relative to,
1825 GNUNET_TRANSPORT_TransmitContinuation cont,
1828 struct Plugin *plugin = cls;
1829 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
1830 struct UDP_FragmentationContext *frag_ctx;
1831 struct UDP_MessageWrapper *udpw;
1832 struct UDPMessage *udp;
1833 char mbuf[udpmlen] GNUNET_ALIGN;
1835 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
1836 (NULL == plugin->sockv6) )
1837 return GNUNET_SYSERR;
1838 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
1839 (NULL == plugin->sockv4) )
1840 return GNUNET_SYSERR;
1841 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1844 return GNUNET_SYSERR;
1847 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1852 return GNUNET_SYSERR;
1854 LOG (GNUNET_ERROR_TYPE_DEBUG,
1855 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1857 GNUNET_i2s (&s->target),
1858 udp_address_to_string (plugin,
1859 s->address->address,
1860 s->address->address_length));
1862 udp = (struct UDPMessage *) mbuf;
1863 udp->header.size = htons (udpmlen);
1864 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1865 udp->reserved = htonl (0);
1866 udp->sender = *plugin->env->my_identity;
1868 /* We do not update the session time out here! Otherwise this
1869 * session will not timeout since we send keep alive before session
1872 * For UDP we update session timeout only on receive, this will
1873 * cover keep alives, since remote peer will reply with keep alive
1876 if (udpmlen <= UDP_MTU)
1878 /* unfragmented message */
1879 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1881 udpw->msg_buf = (char *) &udpw[1];
1882 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1883 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1884 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
1886 udpw->cont_cls = cont_cls;
1887 udpw->frag_ctx = NULL;
1888 udpw->qc = &qc_message_sent;
1889 udpw->qc_cls = plugin;
1890 memcpy (udpw->msg_buf,
1892 sizeof (struct UDPMessage));
1893 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
1898 GNUNET_STATISTICS_update (plugin->env->stats,
1899 "# UDP, unfragmented messages queued total",
1902 GNUNET_STATISTICS_update (plugin->env->stats,
1903 "# UDP, unfragmented bytes payload queued total",
1909 /* fragmented message */
1910 if (NULL != s->frag_ctx)
1911 return GNUNET_SYSERR;
1915 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
1916 frag_ctx->plugin = plugin;
1917 frag_ctx->session = s;
1918 frag_ctx->cont = cont;
1919 frag_ctx->cont_cls = cont_cls;
1920 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
1921 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1922 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1923 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1926 s->last_expected_msg_delay,
1927 s->last_expected_ack_delay,
1931 s->frag_ctx = frag_ctx;
1932 GNUNET_STATISTICS_update (plugin->env->stats,
1933 "# UDP, fragmented messages active",
1936 GNUNET_STATISTICS_update (plugin->env->stats,
1937 "# UDP, fragmented messages, total",
1940 GNUNET_STATISTICS_update (plugin->env->stats,
1941 "# UDP, fragmented bytes (payload)",
1942 frag_ctx->payload_size,
1945 notify_session_monitor (s->plugin,
1947 GNUNET_TRANSPORT_SS_UPDATE);
1948 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
1949 schedule_select_v4 (plugin);
1951 schedule_select_v6 (plugin);
1957 * Handle an ACK message.
1959 * @param plugin the UDP plugin
1960 * @param msg the (presumed) UDP ACK message
1961 * @param udp_addr sender address
1962 * @param udp_addr_len number of bytes in @a udp_addr
1965 read_process_ack (struct Plugin *plugin,
1966 const struct GNUNET_MessageHeader *msg,
1967 const union UdpAddress *udp_addr,
1968 socklen_t udp_addr_len)
1970 const struct GNUNET_MessageHeader *ack;
1971 const struct UDP_ACK_Message *udp_ack;
1972 struct GNUNET_HELLO_Address *address;
1974 struct GNUNET_TIME_Relative flow_delay;
1976 if (ntohs (msg->size)
1977 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
1979 GNUNET_break_op (0);
1982 udp_ack = (const struct UDP_ACK_Message *) msg;
1983 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1984 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
1989 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
1993 GNUNET_HELLO_ADDRESS_INFO_NONE);
1994 s = udp_plugin_lookup_session (plugin,
1998 LOG (GNUNET_ERROR_TYPE_WARNING,
1999 "UDP session of address %s for ACK not found\n",
2000 udp_address_to_string (plugin,
2002 address->address_length));
2003 GNUNET_HELLO_address_free (address);
2006 if (NULL == s->frag_ctx)
2008 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2009 "Fragmentation context of address %s for ACK (%s) not found\n",
2010 udp_address_to_string (plugin,
2012 address->address_length),
2013 GNUNET_FRAGMENT_print_ack (ack));
2014 GNUNET_HELLO_address_free (address);
2017 GNUNET_HELLO_address_free (address);
2019 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2020 LOG (GNUNET_ERROR_TYPE_DEBUG,
2021 "We received a sending delay of %s for %s\n",
2022 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2024 GNUNET_i2s (&udp_ack->sender));
2025 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2029 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2032 LOG (GNUNET_ERROR_TYPE_DEBUG,
2033 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2034 (unsigned int) ntohs (msg->size),
2035 GNUNET_i2s (&udp_ack->sender),
2036 udp_address_to_string (plugin,
2039 /* Expect more ACKs to arrive */
2043 LOG (GNUNET_ERROR_TYPE_DEBUG,
2044 "Message from %s at %s full ACK'ed\n",
2045 GNUNET_i2s (&udp_ack->sender),
2046 udp_address_to_string (plugin,
2050 /* Remove fragmented message after successful sending */
2051 fragmented_message_done (s->frag_ctx,
2056 /* ********************** Receiving ********************** */
2060 * Closure for #find_receive_context().
2062 struct FindReceiveContext
2065 * Where to store the result.
2067 struct DefragContext *rc;
2070 * Session associated with this context.
2072 struct Session *session;
2077 const union UdpAddress *udp_addr;
2080 * Number of bytes in @e udp_addr.
2082 size_t udp_addr_len;
2088 * Scan the heap for a receive context with the given address.
2090 * @param cls the `struct FindReceiveContext`
2091 * @param node internal node of the heap
2092 * @param element value stored at the node (a `struct ReceiveContext`)
2093 * @param cost cost associated with the node
2094 * @return #GNUNET_YES if we should continue to iterate,
2095 * #GNUNET_NO if not.
2098 find_receive_context (void *cls,
2099 struct GNUNET_CONTAINER_HeapNode *node,
2101 GNUNET_CONTAINER_HeapCostType cost)
2103 struct FindReceiveContext *frc = cls;
2104 struct DefragContext *e = element;
2106 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2107 (0 == memcmp (frc->udp_addr,
2109 frc->udp_addr_len)) )
2119 * Message tokenizer has broken up an incomming message. Pass it on
2122 * @param cls the `struct Plugin *`
2123 * @param client the `struct Session *`
2124 * @param hdr the actual message
2125 * @return #GNUNET_OK (always)
2128 process_inbound_tokenized_messages (void *cls,
2130 const struct GNUNET_MessageHeader *hdr)
2132 struct Plugin *plugin = cls;
2133 struct Session *session = client;
2135 if (GNUNET_YES == session->in_destroy)
2137 reschedule_session_timeout (session);
2138 session->flow_delay_for_other_peer
2139 = plugin->env->receive (plugin->env->cls,
2148 * Functions with this signature are called whenever we need to close
2149 * a session due to a disconnect or failure to establish a connection.
2151 * @param cls closure with the `struct Plugin`
2152 * @param s session to close down
2153 * @return #GNUNET_OK on success
2156 udp_disconnect_session (void *cls,
2159 struct Plugin *plugin = cls;
2160 struct UDP_MessageWrapper *udpw;
2161 struct UDP_MessageWrapper *next;
2162 struct FindReceiveContext frc;
2164 GNUNET_assert (GNUNET_YES != s->in_destroy);
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 "Session %p to peer `%s' at address %s ended\n",
2168 GNUNET_i2s (&s->target),
2169 udp_address_to_string (plugin,
2170 s->address->address,
2171 s->address->address_length));
2172 if (NULL != s->timeout_task)
2174 GNUNET_SCHEDULER_cancel (s->timeout_task);
2175 s->timeout_task = NULL;
2177 if (NULL != s->frag_ctx)
2179 /* Remove fragmented message due to disconnect */
2180 fragmented_message_done (s->frag_ctx,
2183 GNUNET_assert (GNUNET_YES ==
2184 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2188 frc.udp_addr = s->address->address;
2189 frc.udp_addr_len = s->address->address_length;
2190 /* Lookup existing receive context for this address */
2191 if (NULL != plugin->defrag_ctxs)
2193 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2194 &find_receive_context,
2198 struct DefragContext *d_ctx = frc.rc;
2200 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2201 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2202 GNUNET_free (d_ctx);
2205 s->in_destroy = GNUNET_YES;
2206 next = plugin->ipv4_queue_head;
2207 while (NULL != (udpw = next))
2210 if (udpw->session == s)
2214 udpw->qc (udpw->qc_cls,
2220 next = plugin->ipv6_queue_head;
2221 while (NULL != (udpw = next))
2224 if (udpw->session == s)
2228 udpw->qc (udpw->qc_cls,
2234 if ( (NULL != s->frag_ctx) &&
2235 (NULL != s->frag_ctx->cont) )
2237 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2238 later, as it might be in use right now */
2239 LOG (GNUNET_ERROR_TYPE_DEBUG,
2240 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2241 GNUNET_i2s (&s->target));
2242 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2245 s->frag_ctx->payload_size,
2246 s->frag_ctx->on_wire_size);
2248 notify_session_monitor (s->plugin,
2250 GNUNET_TRANSPORT_SS_DONE);
2251 plugin->env->session_end (plugin->env->cls,
2254 GNUNET_STATISTICS_set (plugin->env->stats,
2255 "# UDP sessions active",
2256 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2265 * Destroy a session, plugin is being unloaded.
2267 * @param cls the `struct Plugin`
2268 * @param key hash of public key of target peer
2269 * @param value a `struct PeerSession *` to clean up
2270 * @return #GNUNET_OK (continue to iterate)
2273 disconnect_and_free_it (void *cls,
2274 const struct GNUNET_PeerIdentity *key,
2277 struct Plugin *plugin = cls;
2279 udp_disconnect_session (plugin,
2286 * Disconnect from a remote node. Clean up session if we have one for
2289 * @param cls closure for this call (should be handle to Plugin)
2290 * @param target the peeridentity of the peer to disconnect
2291 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2294 udp_disconnect (void *cls,
2295 const struct GNUNET_PeerIdentity *target)
2297 struct Plugin *plugin = cls;
2299 LOG (GNUNET_ERROR_TYPE_DEBUG,
2300 "Disconnecting from peer `%s'\n",
2301 GNUNET_i2s (target));
2302 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2304 &disconnect_and_free_it,
2310 * Session was idle, so disconnect it.
2312 * @param cls the `struct Session` to time out
2313 * @param tc scheduler context
2316 session_timeout (void *cls,
2317 const struct GNUNET_SCHEDULER_TaskContext *tc)
2319 struct Session *s = cls;
2320 struct Plugin *plugin = s->plugin;
2321 struct GNUNET_TIME_Relative left;
2323 s->timeout_task = NULL;
2324 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2325 if (left.rel_value_us > 0)
2327 /* not actually our turn yet, but let's at least update
2328 the monitor, it may think we're about to die ... */
2329 notify_session_monitor (s->plugin,
2331 GNUNET_TRANSPORT_SS_UPDATE);
2332 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2337 LOG (GNUNET_ERROR_TYPE_DEBUG,
2338 "Session %p was idle for %s, disconnecting\n",
2340 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2342 /* call session destroy function */
2343 udp_disconnect_session (plugin,
2349 * Allocate a new session for the given endpoint address.
2350 * Note that this function does not inform the service
2351 * of the new session, this is the responsibility of the
2352 * caller (if needed).
2354 * @param cls the `struct Plugin`
2355 * @param address address of the other peer to use
2356 * @param network_type network type the address belongs to
2357 * @return NULL on error, otherwise session handle
2359 static struct Session *
2360 udp_plugin_create_session (void *cls,
2361 const struct GNUNET_HELLO_Address *address,
2362 enum GNUNET_ATS_Network_Type network_type)
2364 struct Plugin *plugin = cls;
2367 s = GNUNET_new (struct Session);
2369 s->address = GNUNET_HELLO_address_copy (address);
2370 s->target = address->peer;
2371 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2373 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2374 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
2375 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2376 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2377 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2380 s->scope = network_type;
2382 LOG (GNUNET_ERROR_TYPE_DEBUG,
2383 "Creating new session %p for peer `%s' address `%s'\n",
2385 GNUNET_i2s (&address->peer),
2386 udp_address_to_string (plugin,
2388 address->address_length));
2389 GNUNET_assert (GNUNET_OK ==
2390 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2393 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2394 GNUNET_STATISTICS_set (plugin->env->stats,
2395 "# UDP sessions active",
2396 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2398 notify_session_monitor (plugin,
2400 GNUNET_TRANSPORT_SS_INIT);
2406 * Creates a new outbound session the transport service will use to
2407 * send data to the peer.
2409 * @param cls the `struct Plugin *`
2410 * @param address the address
2411 * @return the session or NULL of max connections exceeded
2413 static struct Session *
2414 udp_plugin_get_session (void *cls,
2415 const struct GNUNET_HELLO_Address *address)
2417 struct Plugin *plugin = cls;
2419 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2420 const struct IPv4UdpAddress *udp_v4;
2421 const struct IPv6UdpAddress *udp_v6;
2423 if (NULL == address)
2428 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2429 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2431 GNUNET_break_op (0);
2434 if (NULL != (s = udp_plugin_lookup_session (cls,
2438 /* need to create new session */
2439 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2441 struct sockaddr_in v4;
2443 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2444 memset (&v4, '\0', sizeof (v4));
2445 v4.sin_family = AF_INET;
2446 #if HAVE_SOCKADDR_IN_SIN_LEN
2447 v4.sin_len = sizeof (struct sockaddr_in);
2449 v4.sin_port = udp_v4->u4_port;
2450 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2451 network_type = plugin->env->get_address_type (plugin->env->cls,
2452 (const struct sockaddr *) &v4,
2455 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2457 struct sockaddr_in6 v6;
2459 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2460 memset (&v6, '\0', sizeof (v6));
2461 v6.sin6_family = AF_INET6;
2462 #if HAVE_SOCKADDR_IN_SIN_LEN
2463 v6.sin6_len = sizeof (struct sockaddr_in6);
2465 v6.sin6_port = udp_v6->u6_port;
2466 v6.sin6_addr = udp_v6->ipv6_addr;
2467 network_type = plugin->env->get_address_type (plugin->env->cls,
2468 (const struct sockaddr *) &v6,
2471 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2472 return udp_plugin_create_session (cls,
2479 * We've received a UDP Message. Process it (pass contents to main service).
2481 * @param plugin plugin context
2482 * @param msg the message
2483 * @param udp_addr sender address
2484 * @param udp_addr_len number of bytes in @a udp_addr
2485 * @param network_type network type the address belongs to
2488 process_udp_message (struct Plugin *plugin,
2489 const struct UDPMessage *msg,
2490 const union UdpAddress *udp_addr,
2491 size_t udp_addr_len,
2492 enum GNUNET_ATS_Network_Type network_type)
2495 struct GNUNET_HELLO_Address *address;
2497 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2498 if (0 != ntohl (msg->reserved))
2503 if (ntohs (msg->header.size)
2504 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2510 address = GNUNET_HELLO_address_allocate (&msg->sender,
2514 GNUNET_HELLO_ADDRESS_INFO_NONE);
2516 (s = udp_plugin_lookup_session (plugin,
2519 s = udp_plugin_create_session (plugin,
2522 plugin->env->session_start (plugin->env->cls,
2526 notify_session_monitor (plugin,
2528 GNUNET_TRANSPORT_SS_UP);
2530 GNUNET_free (address);
2533 GNUNET_SERVER_mst_receive (plugin->mst,
2535 (const char *) &msg[1],
2536 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2540 if ( (0 == s->rc) &&
2541 (GNUNET_YES == s->in_destroy) )
2547 * Process a defragmented message.
2549 * @param cls the `struct DefragContext *`
2550 * @param msg the message
2553 fragment_msg_proc (void *cls,
2554 const struct GNUNET_MessageHeader *msg)
2556 struct DefragContext *dc = cls;
2557 const struct UDPMessage *um;
2559 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2561 GNUNET_break_op (0);
2564 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2566 GNUNET_break_op (0);
2569 um = (const struct UDPMessage *) msg;
2570 dc->sender = um->sender;
2571 dc->have_sender = GNUNET_YES;
2572 process_udp_message (dc->plugin,
2581 * We finished sending an acknowledgement. Update
2584 * @param cls the `struct Plugin`
2585 * @param udpw message queue entry of the ACK
2586 * @param result #GNUNET_OK if the transmission worked,
2587 * #GNUNET_SYSERR if we failed to send the ACK
2590 ack_message_sent (void *cls,
2591 struct UDP_MessageWrapper *udpw,
2594 struct Plugin *plugin = cls;
2596 if (GNUNET_OK == result)
2598 GNUNET_STATISTICS_update (plugin->env->stats,
2599 "# UDP, ACK messages sent",
2605 GNUNET_STATISTICS_update (plugin->env->stats,
2606 "# UDP, ACK transmissions failed",
2614 * Transmit an acknowledgement.
2616 * @param cls the `struct DefragContext *`
2617 * @param id message ID (unused)
2618 * @param msg ack to transmit
2621 ack_proc (void *cls,
2623 const struct GNUNET_MessageHeader *msg)
2625 struct DefragContext *rc = cls;
2626 struct Plugin *plugin = rc->plugin;
2627 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2628 struct UDP_ACK_Message *udp_ack;
2630 struct UDP_MessageWrapper *udpw;
2632 struct GNUNET_HELLO_Address *address;
2634 if (GNUNET_NO == rc->have_sender)
2636 /* tried to defragment but never succeeded, hence will not ACK */
2637 /* This can happen if we just lost msgs */
2638 GNUNET_STATISTICS_update (plugin->env->stats,
2639 "# UDP, fragments discarded without ACK",
2644 address = GNUNET_HELLO_address_allocate (&rc->sender,
2648 GNUNET_HELLO_ADDRESS_INFO_NONE);
2649 s = udp_plugin_lookup_session (plugin,
2651 GNUNET_HELLO_address_free (address);
2654 LOG (GNUNET_ERROR_TYPE_ERROR,
2655 "Trying to transmit ACK to peer `%s' but no session found!\n",
2656 udp_address_to_string (plugin,
2659 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2660 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2662 GNUNET_STATISTICS_update (plugin->env->stats,
2663 "# UDP, ACK transmissions failed",
2668 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2669 delay = s->flow_delay_for_other_peer.rel_value_us;
2672 LOG (GNUNET_ERROR_TYPE_DEBUG,
2673 "Sending ACK to `%s' including delay of %s\n",
2674 udp_address_to_string (plugin,
2677 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2679 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2680 udpw->msg_size = msize;
2681 udpw->payload_size = 0;
2683 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2684 udpw->msg_buf = (char *) &udpw[1];
2685 udpw->qc = &ack_message_sent;
2686 udpw->qc_cls = plugin;
2687 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2688 udp_ack->header.size = htons ((uint16_t) msize);
2689 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2690 udp_ack->delay = htonl (delay);
2691 udp_ack->sender = *plugin->env->my_identity;
2692 memcpy (&udp_ack[1],
2697 notify_session_monitor (plugin,
2699 GNUNET_TRANSPORT_SS_UPDATE);
2700 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2701 schedule_select_v4 (plugin);
2703 schedule_select_v6 (plugin);
2708 * We received a fragment, process it.
2710 * @param plugin our plugin
2711 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2712 * @param udp_addr sender address
2713 * @param udp_addr_len number of bytes in @a udp_addr
2714 * @param network_type network type the address belongs to
2717 read_process_fragment (struct Plugin *plugin,
2718 const struct GNUNET_MessageHeader *msg,
2719 const union UdpAddress *udp_addr,
2720 size_t udp_addr_len,
2721 enum GNUNET_ATS_Network_Type network_type)
2723 struct DefragContext *d_ctx;
2724 struct GNUNET_TIME_Absolute now;
2725 struct FindReceiveContext frc;
2728 frc.udp_addr = udp_addr;
2729 frc.udp_addr_len = udp_addr_len;
2731 /* Lookup existing receive context for this address */
2732 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2733 &find_receive_context,
2735 now = GNUNET_TIME_absolute_get ();
2740 /* Create a new defragmentation context */
2741 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2745 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2746 d_ctx->udp_addr_len = udp_addr_len;
2747 d_ctx->network_type = network_type;
2748 d_ctx->plugin = plugin;
2749 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2751 UDP_MAX_MESSAGES_IN_DEFRAG,
2755 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2757 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2758 LOG (GNUNET_ERROR_TYPE_DEBUG,
2759 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2760 (unsigned int) ntohs (msg->size),
2761 udp_address_to_string (plugin,
2767 LOG (GNUNET_ERROR_TYPE_DEBUG,
2768 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2769 (unsigned int) ntohs (msg->size),
2770 udp_address_to_string (plugin,
2776 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
2779 /* keep this 'rc' from expiring */
2780 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2782 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2784 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2785 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2787 /* remove 'rc' that was inactive the longest */
2788 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2789 GNUNET_assert (NULL != d_ctx);
2790 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2791 GNUNET_free (d_ctx);
2792 GNUNET_STATISTICS_update (plugin->env->stats,
2793 "# UDP, Defragmentations aborted",
2801 * Read and process a message from the given socket.
2803 * @param plugin the overall plugin
2804 * @param rsock socket to read from
2807 udp_select_read (struct Plugin *plugin,
2808 struct GNUNET_NETWORK_Handle *rsock)
2811 struct sockaddr_storage addr;
2812 char buf[65536] GNUNET_ALIGN;
2814 const struct GNUNET_MessageHeader *msg;
2815 struct IPv4UdpAddress v4;
2816 struct IPv6UdpAddress v6;
2817 const struct sockaddr *sa;
2818 const struct sockaddr_in *sa4;
2819 const struct sockaddr_in6 *sa6;
2820 const union UdpAddress *int_addr;
2821 size_t int_addr_len;
2822 enum GNUNET_ATS_Network_Type network_type;
2824 fromlen = sizeof (addr);
2828 size = GNUNET_NETWORK_socket_recvfrom (rsock,
2831 (struct sockaddr *) &addr,
2833 sa = (const struct sockaddr *) &addr;
2835 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2836 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2837 * on this socket has failed.
2839 * WSAECONNRESET - The virtual circuit was reset by the remote side
2840 * executing a hard or abortive close. The application should close
2841 * the socket; it is no longer usable. On a UDP-datagram socket this
2842 * error indicates a previous send operation resulted in an ICMP Port
2843 * Unreachable message.
2845 if ( (-1 == size) &&
2846 (ECONNRESET == errno) )
2851 LOG (GNUNET_ERROR_TYPE_DEBUG,
2852 "UDP failed to receive data: %s\n",
2854 /* Connection failure or something. Not a protocol violation. */
2858 /* Check if this is a STUN packet */
2859 if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
2862 return; /* was STUN, do not process further */
2864 if (size < sizeof(struct GNUNET_MessageHeader))
2866 LOG (GNUNET_ERROR_TYPE_WARNING,
2867 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2868 (unsigned int ) size,
2871 /* _MAY_ be a connection failure (got partial message) */
2872 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2873 GNUNET_break_op (0);
2880 msg = (const struct GNUNET_MessageHeader *) buf;
2881 LOG (GNUNET_ERROR_TYPE_DEBUG,
2882 "UDP received %u-byte message from `%s' type %u\n",
2883 (unsigned int) size,
2887 if (size != ntohs (msg->size))
2889 LOG (GNUNET_ERROR_TYPE_WARNING,
2890 "UDP malformed message header from %s\n",
2891 (unsigned int) size,
2894 GNUNET_break_op (0);
2897 GNUNET_STATISTICS_update (plugin->env->stats,
2898 "# UDP, total bytes received",
2901 network_type = plugin->env->get_address_type (plugin->env->cls,
2904 switch (sa->sa_family)
2907 sa4 = (const struct sockaddr_in *) &addr;
2909 v4.ipv4_addr = sa4->sin_addr.s_addr;
2910 v4.u4_port = sa4->sin_port;
2911 int_addr = (union UdpAddress *) &v4;
2912 int_addr_len = sizeof (v4);
2915 sa6 = (const struct sockaddr_in6 *) &addr;
2917 v6.ipv6_addr = sa6->sin6_addr;
2918 v6.u6_port = sa6->sin6_port;
2919 int_addr = (union UdpAddress *) &v6;
2920 int_addr_len = sizeof (v6);
2927 switch (ntohs (msg->type))
2929 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2930 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2931 udp_broadcast_receive (plugin,
2938 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2939 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2944 process_udp_message (plugin,
2945 (const struct UDPMessage *) msg,
2950 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2951 read_process_ack (plugin,
2956 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2957 read_process_fragment (plugin,
2971 * Removes messages from the transmission queue that have
2972 * timed out, and then selects a message that should be
2975 * @param plugin the UDP plugin
2976 * @param sock which socket should we process the queue for (v4 or v6)
2977 * @return message selected for transmission, or NULL for none
2979 static struct UDP_MessageWrapper *
2980 remove_timeout_messages_and_select (struct Plugin *plugin,
2981 struct GNUNET_NETWORK_Handle *sock)
2983 struct UDP_MessageWrapper *udpw;
2984 struct GNUNET_TIME_Relative remaining;
2985 struct Session *session;
2988 removed = GNUNET_NO;
2989 udpw = (sock == plugin->sockv4)
2990 ? plugin->ipv4_queue_head
2991 : plugin->ipv6_queue_head;
2992 while (NULL != udpw)
2994 session = udpw->session;
2995 /* Find messages with timeout */
2996 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2997 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2999 /* Message timed out */
3000 removed = GNUNET_YES;
3003 udpw->qc (udpw->qc_cls,
3008 if (sock == plugin->sockv4)
3010 udpw = plugin->ipv4_queue_head;
3012 else if (sock == plugin->sockv6)
3014 udpw = plugin->ipv6_queue_head;
3018 GNUNET_break (0); /* should never happen */
3021 GNUNET_STATISTICS_update (plugin->env->stats,
3022 "# messages discarded due to timeout",
3028 /* Message did not time out, check flow delay */
3029 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
3030 if (0 == remaining.rel_value_us)
3032 /* this message is not delayed */
3033 LOG (GNUNET_ERROR_TYPE_DEBUG,
3034 "Message for peer `%s' (%u bytes) is not delayed \n",
3035 GNUNET_i2s (&udpw->session->target),
3036 udpw->payload_size);
3037 break; /* Found message to send, break */
3041 /* Message is delayed, try next */
3042 LOG (GNUNET_ERROR_TYPE_DEBUG,
3043 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3044 GNUNET_i2s (&udpw->session->target),
3046 GNUNET_STRINGS_relative_time_to_string (remaining,
3052 if (GNUNET_YES == removed)
3053 notify_session_monitor (session->plugin,
3055 GNUNET_TRANSPORT_SS_UPDATE);
3061 * We failed to transmit a message via UDP. Generate
3062 * a descriptive error message.
3064 * @param plugin our plugin
3065 * @param sa target address we were trying to reach
3066 * @param slen number of bytes in @a sa
3067 * @param error the errno value returned from the sendto() call
3070 analyze_send_error (struct Plugin *plugin,
3071 const struct sockaddr *sa,
3075 enum GNUNET_ATS_Network_Type type;
3077 type = plugin->env->get_address_type (plugin->env->cls,
3080 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3081 (GNUNET_ATS_NET_WAN == type) ) &&
3082 ( (ENETUNREACH == errno) ||
3083 (ENETDOWN == errno) ) )
3085 if (slen == sizeof (struct sockaddr_in))
3087 /* IPv4: "Network unreachable" or "Network down"
3089 * This indicates we do not have connectivity
3091 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3092 _("UDP could not transmit message to `%s': "
3093 "Network seems down, please check your network configuration\n"),
3097 if (slen == sizeof (struct sockaddr_in6))
3099 /* IPv6: "Network unreachable" or "Network down"
3101 * This indicates that this system is IPv6 enabled, but does not
3102 * have a valid global IPv6 address assigned or we do not have
3105 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3106 _("UDP could not transmit IPv6 message! "
3107 "Please check your network configuration and disable IPv6 if your "
3108 "connection does not have a global IPv6 address\n"));
3113 LOG (GNUNET_ERROR_TYPE_WARNING,
3114 "UDP could not transmit message to `%s': `%s'\n",
3123 * It is time to try to transmit a UDP message. Select one
3126 * @param plugin the plugin
3127 * @param sock which socket (v4/v6) to send on
3130 udp_select_send (struct Plugin *plugin,
3131 struct GNUNET_NETWORK_Handle *sock)
3135 const struct sockaddr *a;
3136 const struct IPv4UdpAddress *u4;
3137 struct sockaddr_in a4;
3138 const struct IPv6UdpAddress *u6;
3139 struct sockaddr_in6 a6;
3140 struct UDP_MessageWrapper *udpw;
3142 /* Find message(s) to send */
3143 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3146 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3148 u4 = udpw->session->address->address;
3152 a4.sin_family = AF_INET;
3153 #if HAVE_SOCKADDR_IN_SIN_LEN
3154 a4.sin_len = sizeof (a4);
3156 a4.sin_port = u4->u4_port;
3157 a4.sin_addr.s_addr = u4->ipv4_addr;
3158 a = (const struct sockaddr *) &a4;
3161 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3163 u6 = udpw->session->address->address;
3167 a6.sin6_family = AF_INET6;
3168 #if HAVE_SOCKADDR_IN_SIN_LEN
3169 a6.sin6_len = sizeof (a6);
3171 a6.sin6_port = u6->u6_port;
3172 a6.sin6_addr = u6->ipv6_addr;
3173 a = (const struct sockaddr *) &a6;
3181 udpw->qc (udpw->qc_cls,
3184 notify_session_monitor (plugin,
3186 GNUNET_TRANSPORT_SS_UPDATE);
3190 sent = GNUNET_NETWORK_socket_sendto (sock,
3197 if (GNUNET_SYSERR == sent)
3200 analyze_send_error (plugin,
3204 udpw->qc (udpw->qc_cls,
3207 GNUNET_STATISTICS_update (plugin->env->stats,
3208 "# UDP, total, bytes, sent, failure",
3211 GNUNET_STATISTICS_update (plugin->env->stats,
3212 "# UDP, total, messages, sent, failure",
3219 LOG (GNUNET_ERROR_TYPE_DEBUG,
3220 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3221 (unsigned int) (udpw->msg_size),
3222 GNUNET_i2s (&udpw->session->target),
3226 (sent < 0) ? STRERROR (errno) : "ok");
3227 GNUNET_STATISTICS_update (plugin->env->stats,
3228 "# UDP, total, bytes, sent, success",
3231 GNUNET_STATISTICS_update (plugin->env->stats,
3232 "# UDP, total, messages, sent, success",
3235 if (NULL != udpw->frag_ctx)
3236 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3237 udpw->qc (udpw->qc_cls,
3241 notify_session_monitor (plugin,
3243 GNUNET_TRANSPORT_SS_UPDATE);
3249 /* ***************** Event loop (part 2) *************** */
3253 * We have been notified that our readset has something to read. We don't
3254 * know which socket needs to be read, so we have to check each one
3255 * Then reschedule this function to be called again once more is available.
3257 * @param cls the plugin handle
3258 * @param tc the scheduling context
3261 udp_plugin_select_v4 (void *cls,
3262 const struct GNUNET_SCHEDULER_TaskContext *tc)
3264 struct Plugin *plugin = cls;
3266 plugin->select_task_v4 = NULL;
3267 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3269 if (NULL == plugin->sockv4)
3271 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3272 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3274 udp_select_read (plugin,
3276 udp_select_send (plugin,
3278 schedule_select_v4 (plugin);
3283 * We have been notified that our readset has something to read. We don't
3284 * know which socket needs to be read, so we have to check each one
3285 * Then reschedule this function to be called again once more is available.
3287 * @param cls the plugin handle
3288 * @param tc the scheduling context
3291 udp_plugin_select_v6 (void *cls,
3292 const struct GNUNET_SCHEDULER_TaskContext *tc)
3294 struct Plugin *plugin = cls;
3296 plugin->select_task_v6 = NULL;
3297 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3299 if (NULL == plugin->sockv6)
3301 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3302 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3304 udp_select_read (plugin,
3307 udp_select_send (plugin,
3309 schedule_select_v6 (plugin);
3313 /* ******************* Initialization *************** */
3317 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3319 * @param plugin the plugin to initialize
3320 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3321 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3322 * @return number of sockets that were successfully bound
3325 setup_sockets (struct Plugin *plugin,
3326 const struct sockaddr_in6 *bind_v6,
3327 const struct sockaddr_in *bind_v4)
3330 int sockets_created = 0;
3331 struct sockaddr_in6 server_addrv6;
3332 struct sockaddr_in server_addrv4;
3333 const struct sockaddr *server_addr;
3334 const struct sockaddr *addrs[2];
3335 socklen_t addrlens[2];
3339 /* Create IPv6 socket */
3341 if (GNUNET_YES == plugin->enable_ipv6)
3343 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3346 if (NULL == plugin->sockv6)
3348 LOG (GNUNET_ERROR_TYPE_INFO,
3349 _("Disabling IPv6 since it is not supported on this system!\n"));
3350 plugin->enable_ipv6 = GNUNET_NO;
3354 memset (&server_addrv6,
3356 sizeof(struct sockaddr_in6));
3357 #if HAVE_SOCKADDR_IN_SIN_LEN
3358 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3360 server_addrv6.sin6_family = AF_INET6;
3361 if (NULL != bind_v6)
3362 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3364 server_addrv6.sin6_addr = in6addr_any;
3366 if (0 == plugin->port) /* autodetect */
3367 server_addrv6.sin6_port
3368 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3372 server_addrv6.sin6_port = htons (plugin->port);
3373 addrlen = sizeof (struct sockaddr_in6);
3374 server_addr = (const struct sockaddr *) &server_addrv6;
3379 LOG(GNUNET_ERROR_TYPE_DEBUG,
3380 "Binding to IPv6 `%s'\n",
3381 GNUNET_a2s (server_addr,
3385 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3390 if (0 != plugin->port)
3392 tries = 10; /* fail immediately */
3393 break; /* bind failed on specific port */
3396 server_addrv6.sin6_port
3397 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3404 GNUNET_NETWORK_socket_close (plugin->sockv6);
3405 plugin->enable_ipv6 = GNUNET_NO;
3406 plugin->sockv6 = NULL;
3410 plugin->port = ntohs (server_addrv6.sin6_port);
3412 if (NULL != plugin->sockv6)
3414 LOG (GNUNET_ERROR_TYPE_DEBUG,
3415 "IPv6 UDP socket created listinging at %s\n",
3416 GNUNET_a2s (server_addr,
3418 addrs[sockets_created] = server_addr;
3419 addrlens[sockets_created] = addrlen;
3424 LOG (GNUNET_ERROR_TYPE_WARNING,
3425 _("Failed to bind UDP socket to %s: %s\n"),
3426 GNUNET_a2s (server_addr,
3433 /* Create IPv4 socket */
3435 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3438 if (NULL == plugin->sockv4)
3440 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3442 LOG (GNUNET_ERROR_TYPE_INFO,
3443 _("Disabling IPv4 since it is not supported on this system!\n"));
3444 plugin->enable_ipv4 = GNUNET_NO;
3448 memset (&server_addrv4,
3450 sizeof(struct sockaddr_in));
3451 #if HAVE_SOCKADDR_IN_SIN_LEN
3452 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3454 server_addrv4.sin_family = AF_INET;
3455 if (NULL != bind_v4)
3456 server_addrv4.sin_addr = bind_v4->sin_addr;
3458 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3460 if (0 == plugin->port)
3462 server_addrv4.sin_port
3463 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3467 server_addrv4.sin_port = htons (plugin->port);
3469 addrlen = sizeof (struct sockaddr_in);
3470 server_addr = (const struct sockaddr *) &server_addrv4;
3475 LOG (GNUNET_ERROR_TYPE_DEBUG,
3476 "Binding to IPv4 `%s'\n",
3477 GNUNET_a2s (server_addr,
3482 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3487 if (0 != plugin->port)
3489 tries = 10; /* fail */
3490 break; /* bind failed on specific port */
3494 server_addrv4.sin_port
3495 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3502 GNUNET_NETWORK_socket_close (plugin->sockv4);
3503 plugin->enable_ipv4 = GNUNET_NO;
3504 plugin->sockv4 = NULL;
3508 plugin->port = ntohs (server_addrv4.sin_port);
3511 if (NULL != plugin->sockv4)
3513 LOG (GNUNET_ERROR_TYPE_DEBUG,
3514 "IPv4 socket created on port %s\n",
3515 GNUNET_a2s (server_addr,
3517 addrs[sockets_created] = server_addr;
3518 addrlens[sockets_created] = addrlen;
3523 LOG (GNUNET_ERROR_TYPE_ERROR,
3524 _("Failed to bind UDP socket to %s: %s\n"),
3525 GNUNET_a2s (server_addr,
3531 if (0 == sockets_created)
3533 LOG (GNUNET_ERROR_TYPE_WARNING,
3534 _("Failed to open UDP sockets\n"));
3535 return 0; /* No sockets created, return */
3537 schedule_select_v4 (plugin);
3538 schedule_select_v6 (plugin);
3539 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3545 &udp_nat_port_map_callback,
3549 return sockets_created;
3554 * The exported method. Makes the core api available via a global and
3555 * returns the udp transport API.
3557 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3558 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3561 libgnunet_plugin_transport_udp_init (void *cls)
3563 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3564 struct GNUNET_TRANSPORT_PluginFunctions *api;
3566 unsigned long long port;
3567 unsigned long long aport;
3568 unsigned long long udp_max_bps;
3569 unsigned long long enable_v6;
3570 unsigned long long enable_broadcasting;
3571 unsigned long long enable_broadcasting_recv;
3572 char *bind4_address;
3573 char *bind6_address;
3574 struct GNUNET_TIME_Relative interval;
3575 struct sockaddr_in server_addrv4;
3576 struct sockaddr_in6 server_addrv6;
3581 if (NULL == env->receive)
3583 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3584 initialze the plugin or the API */
3585 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3587 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3588 api->address_to_string = &udp_address_to_string;
3589 api->string_to_address = &udp_string_to_address;
3593 /* Get port number: port == 0 : autodetect a port,
3594 * > 0 : use this port, not given : 2086 default */
3596 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3603 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3606 _("must be in [0,65535]"));
3610 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3617 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3620 _("must be in [0,65535]"));
3625 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3628 enable_v6 = GNUNET_NO;
3630 enable_v6 = GNUNET_YES;
3632 have_bind4 = GNUNET_NO;
3633 memset (&server_addrv4,
3635 sizeof (server_addrv4));
3637 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3642 LOG (GNUNET_ERROR_TYPE_DEBUG,
3643 "Binding UDP plugin to specific address: `%s'\n",
3645 if (1 != inet_pton (AF_INET,
3647 &server_addrv4.sin_addr))
3649 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3652 _("must be valid IPv4 address"));
3653 GNUNET_free (bind4_address);
3656 have_bind4 = GNUNET_YES;
3658 GNUNET_free_non_null (bind4_address);
3659 have_bind6 = GNUNET_NO;
3660 memset (&server_addrv6,
3662 sizeof (server_addrv6));
3664 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3669 LOG (GNUNET_ERROR_TYPE_DEBUG,
3670 "Binding udp plugin to specific address: `%s'\n",
3672 if (1 != inet_pton (AF_INET6,
3674 &server_addrv6.sin6_addr))
3676 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3679 _("must be valid IPv6 address"));
3680 GNUNET_free (bind6_address);
3683 have_bind6 = GNUNET_YES;
3685 GNUNET_free_non_null (bind6_address);
3687 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3690 if (enable_broadcasting == GNUNET_SYSERR)
3691 enable_broadcasting = GNUNET_NO;
3693 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3695 "BROADCAST_RECEIVE");
3696 if (enable_broadcasting_recv == GNUNET_SYSERR)
3697 enable_broadcasting_recv = GNUNET_YES;
3699 if (GNUNET_SYSERR ==
3700 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3702 "BROADCAST_INTERVAL",
3705 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3709 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3714 /* 50 MB/s == infinity for practical purposes */
3715 udp_max_bps = 1024 * 1024 * 50;
3718 p = GNUNET_new (struct Plugin);
3721 p->broadcast_interval = interval;
3722 p->enable_ipv6 = enable_v6;
3723 p->enable_ipv4 = GNUNET_YES; /* default */
3724 p->enable_broadcasting = enable_broadcasting;
3725 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3727 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3729 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3730 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3732 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3735 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3737 res = setup_sockets (p,
3738 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3739 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3741 ( (NULL == p->sockv4) &&
3742 (NULL == p->sockv6) ) )
3744 LOG (GNUNET_ERROR_TYPE_ERROR,
3745 _("Failed to create UDP network sockets\n"));
3746 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3747 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3748 GNUNET_SERVER_mst_destroy (p->mst);
3753 /* Setup broadcasting and receiving beacons */
3758 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3760 api->disconnect_session = &udp_disconnect_session;
3761 api->query_keepalive_factor = &udp_query_keepalive_factor;
3762 api->disconnect_peer = &udp_disconnect;
3763 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3764 api->address_to_string = &udp_address_to_string;
3765 api->string_to_address = &udp_string_to_address;
3766 api->check_address = &udp_plugin_check_address;
3767 api->get_session = &udp_plugin_get_session;
3768 api->send = &udp_plugin_send;
3769 api->get_network = &udp_get_network;
3770 api->update_session_timeout = &udp_plugin_update_session_timeout;
3771 api->setup_monitor = &udp_plugin_setup_monitor;
3777 * Function called on each entry in the defragmentation heap to
3781 * @param node node in the heap (to be removed)
3782 * @param element a `struct DefragContext` to be cleaned up
3783 * @param cost unused
3784 * @return #GNUNET_YES
3787 heap_cleanup_iterator (void *cls,
3788 struct GNUNET_CONTAINER_HeapNode *node,
3790 GNUNET_CONTAINER_HeapCostType cost)
3792 struct DefragContext *d_ctx = element;
3794 GNUNET_CONTAINER_heap_remove_node (node);
3795 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3796 GNUNET_free (d_ctx);
3802 * The exported method. Makes the core api available via a global and
3803 * returns the udp transport API.
3805 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3809 libgnunet_plugin_transport_udp_done (void *cls)
3811 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3812 struct Plugin *plugin = api->cls;
3813 struct PrettyPrinterContext *cur;
3814 struct UDP_MessageWrapper *udpw;
3821 stop_broadcast (plugin);
3822 if (NULL != plugin->select_task_v4)
3824 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
3825 plugin->select_task_v4 = NULL;
3827 if (NULL != plugin->select_task_v6)
3829 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3830 plugin->select_task_v6 = NULL;
3832 if (NULL != plugin->sockv4)
3834 GNUNET_break (GNUNET_OK ==
3835 GNUNET_NETWORK_socket_close (plugin->sockv4));
3836 plugin->sockv4 = NULL;
3838 if (NULL != plugin->sockv6)
3840 GNUNET_break (GNUNET_OK ==
3841 GNUNET_NETWORK_socket_close (plugin->sockv6));
3842 plugin->sockv6 = NULL;
3844 if (NULL != plugin->nat)
3846 GNUNET_NAT_unregister (plugin->nat);
3849 if (NULL != plugin->defrag_ctxs)
3851 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3852 &heap_cleanup_iterator,
3854 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3855 plugin->defrag_ctxs = NULL;
3857 if (NULL != plugin->mst)
3859 GNUNET_SERVER_mst_destroy (plugin->mst);
3862 while (NULL != (udpw = plugin->ipv4_queue_head))
3866 udpw->qc (udpw->qc_cls,
3871 while (NULL != (udpw = plugin->ipv6_queue_head))
3875 udpw->qc (udpw->qc_cls,
3880 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3881 &disconnect_and_free_it,
3883 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3885 while (NULL != (cur = plugin->ppc_dll_head))
3888 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3889 plugin->ppc_dll_tail,
3891 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3892 if (NULL != cur->timeout_task)
3894 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3895 cur->timeout_task = NULL;
3899 GNUNET_free (plugin);
3904 /* end of plugin_transport_udp.c */