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., 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__)
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 (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1465 GNUNET_STATISTICS_update (plugin->env->stats,
1466 "# UDP, total bytes in send buffers",
1469 plugin->bytes_in_buffer += udpw->msg_size;
1471 GNUNET_STATISTICS_update (plugin->env->stats,
1472 "# UDP, total messages in send buffers",
1475 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1477 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1478 plugin->ipv4_queue_tail,
1481 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1483 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1484 plugin->ipv6_queue_tail,
1490 udpw->cont (udpw->cont_cls,
1498 session->msgs_in_queue++;
1499 session->bytes_in_queue += udpw->msg_size;
1504 * We have completed our (attempt) to transmit a message that had to
1505 * be fragmented -- either because we got an ACK saying that all
1506 * fragments were received, or because of timeout / disconnect. Clean
1509 * @param frag_ctx fragmentation context to clean up
1510 * @param result #GNUNET_OK if we succeeded (got ACK),
1511 * #GNUNET_SYSERR if the transmission failed
1514 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1517 struct Plugin *plugin = frag_ctx->plugin;
1518 struct Session *s = frag_ctx->session;
1519 struct UDP_MessageWrapper *udpw;
1520 struct UDP_MessageWrapper *tmp;
1523 LOG (GNUNET_ERROR_TYPE_DEBUG,
1524 "%p: Fragmented message removed with result %s\n",
1526 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1527 /* Call continuation for fragmented message */
1528 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1529 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1531 overhead = frag_ctx->on_wire_size;
1532 if (NULL != frag_ctx->cont)
1533 frag_ctx->cont (frag_ctx->cont_cls,
1536 s->frag_ctx->payload_size,
1537 frag_ctx->on_wire_size);
1538 GNUNET_STATISTICS_update (plugin->env->stats,
1539 "# UDP, fragmented messages active",
1543 if (GNUNET_OK == result)
1545 GNUNET_STATISTICS_update (plugin->env->stats,
1546 "# UDP, fragmented msgs, messages, sent, success",
1549 GNUNET_STATISTICS_update (plugin->env->stats,
1550 "# UDP, fragmented msgs, bytes payload, sent, success",
1551 s->frag_ctx->payload_size,
1553 GNUNET_STATISTICS_update (plugin->env->stats,
1554 "# UDP, fragmented msgs, bytes overhead, sent, success",
1557 GNUNET_STATISTICS_update (plugin->env->stats,
1558 "# UDP, total, bytes overhead, sent",
1561 GNUNET_STATISTICS_update (plugin->env->stats,
1562 "# UDP, total, bytes payload, sent",
1563 s->frag_ctx->payload_size,
1568 GNUNET_STATISTICS_update (plugin->env->stats,
1569 "# UDP, fragmented msgs, messages, sent, failure",
1572 GNUNET_STATISTICS_update (plugin->env->stats,
1573 "# UDP, fragmented msgs, bytes payload, sent, failure",
1574 s->frag_ctx->payload_size,
1576 GNUNET_STATISTICS_update (plugin->env->stats,
1577 "# UDP, fragmented msgs, bytes payload, sent, failure",
1580 GNUNET_STATISTICS_update (plugin->env->stats,
1581 "# UDP, fragmented msgs, bytes payload, sent, failure",
1586 /* Remove remaining fragments from queue, no need to transmit those
1588 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1590 udpw = plugin->ipv6_queue_head;
1591 while (NULL != udpw)
1594 if ( (udpw->frag_ctx != NULL) &&
1595 (udpw->frag_ctx == frag_ctx) )
1604 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1606 udpw = plugin->ipv4_queue_head;
1607 while (NULL != udpw)
1610 if ( (NULL != udpw->frag_ctx) &&
1611 (udpw->frag_ctx == frag_ctx) )
1620 notify_session_monitor (s->plugin,
1622 GNUNET_TRANSPORT_SS_UPDATE);
1623 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1624 &s->last_expected_msg_delay,
1625 &s->last_expected_ack_delay);
1627 GNUNET_free (frag_ctx);
1632 * We are finished with a fragment in the message queue.
1633 * Notify the continuation and update statistics.
1635 * @param cls the `struct Plugin *`
1636 * @param udpw the queue entry
1637 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1640 qc_fragment_sent (void *cls,
1641 struct UDP_MessageWrapper *udpw,
1644 struct Plugin *plugin = cls;
1646 GNUNET_assert (NULL != udpw->frag_ctx);
1647 if (GNUNET_OK == result)
1649 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1650 GNUNET_STATISTICS_update (plugin->env->stats,
1651 "# UDP, fragmented msgs, fragments, sent, success",
1654 GNUNET_STATISTICS_update (plugin->env->stats,
1655 "# UDP, fragmented msgs, fragments bytes, sent, success",
1661 fragmented_message_done (udpw->frag_ctx,
1663 GNUNET_STATISTICS_update (plugin->env->stats,
1664 "# UDP, fragmented msgs, fragments, sent, failure",
1667 GNUNET_STATISTICS_update (plugin->env->stats,
1668 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1676 * Function that is called with messages created by the fragmentation
1677 * module. In the case of the `proc` callback of the
1678 * #GNUNET_FRAGMENT_context_create() function, this function must
1679 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1681 * @param cls closure, the `struct UDP_FragmentationContext`
1682 * @param msg the message that was created
1685 enqueue_fragment (void *cls,
1686 const struct GNUNET_MessageHeader *msg)
1688 struct UDP_FragmentationContext *frag_ctx = cls;
1689 struct Plugin *plugin = frag_ctx->plugin;
1690 struct UDP_MessageWrapper *udpw;
1691 struct Session *session = frag_ctx->session;
1692 size_t msg_len = ntohs (msg->size);
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "Enqueuing fragment with %u bytes\n",
1697 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1698 udpw->session = session;
1699 udpw->msg_buf = (char *) &udpw[1];
1700 udpw->msg_size = msg_len;
1701 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1702 udpw->timeout = frag_ctx->timeout;
1703 udpw->frag_ctx = frag_ctx;
1704 udpw->qc = &qc_fragment_sent;
1705 udpw->qc_cls = plugin;
1706 memcpy (udpw->msg_buf,
1711 if (sizeof (struct IPv4UdpAddress) == session->address->address_length)
1712 schedule_select_v4 (plugin);
1714 schedule_select_v6 (plugin);
1719 * We are finished with a message from the message queue.
1720 * Notify the continuation and update statistics.
1722 * @param cls the `struct Plugin *`
1723 * @param udpw the queue entry
1724 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1727 qc_message_sent (void *cls,
1728 struct UDP_MessageWrapper *udpw,
1731 struct Plugin *plugin = cls;
1734 if (udpw->msg_size >= udpw->payload_size)
1735 overhead = udpw->msg_size - udpw->payload_size;
1737 overhead = udpw->msg_size;
1739 if (NULL != udpw->cont)
1740 udpw->cont (udpw->cont_cls,
1741 &udpw->session->target,
1745 if (GNUNET_OK == result)
1747 GNUNET_STATISTICS_update (plugin->env->stats,
1748 "# UDP, unfragmented msgs, messages, sent, success",
1751 GNUNET_STATISTICS_update (plugin->env->stats,
1752 "# UDP, unfragmented msgs, bytes payload, sent, success",
1755 GNUNET_STATISTICS_update (plugin->env->stats,
1756 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1759 GNUNET_STATISTICS_update (plugin->env->stats,
1760 "# UDP, total, bytes overhead, sent",
1763 GNUNET_STATISTICS_update (plugin->env->stats,
1764 "# UDP, total, bytes payload, sent",
1770 GNUNET_STATISTICS_update (plugin->env->stats,
1771 "# UDP, unfragmented msgs, messages, sent, failure",
1774 GNUNET_STATISTICS_update (plugin->env->stats,
1775 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1778 GNUNET_STATISTICS_update (plugin->env->stats,
1779 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1787 * Function that can be used by the transport service to transmit a
1788 * message using the plugin. Note that in the case of a peer
1789 * disconnecting, the continuation MUST be called prior to the
1790 * disconnect notification itself. This function will be called with
1791 * this peer's HELLO message to initiate a fresh connection to another
1794 * @param cls closure
1795 * @param s which session must be used
1796 * @param msgbuf the message to transmit
1797 * @param msgbuf_size number of bytes in @a msgbuf
1798 * @param priority how important is the message (most plugins will
1799 * ignore message priority and just FIFO)
1800 * @param to how long to wait at most for the transmission (does not
1801 * require plugins to discard the message after the timeout,
1802 * just advisory for the desired delay; most plugins will ignore
1804 * @param cont continuation to call once the message has
1805 * been transmitted (or if the transport is ready
1806 * for the next transmission call; or if the
1807 * peer disconnected...); can be NULL
1808 * @param cont_cls closure for @a cont
1809 * @return number of bytes used (on the physical network, with overheads);
1810 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1811 * and does NOT mean that the message was not transmitted (DV)
1814 udp_plugin_send (void *cls,
1818 unsigned int priority,
1819 struct GNUNET_TIME_Relative to,
1820 GNUNET_TRANSPORT_TransmitContinuation cont,
1823 struct Plugin *plugin = cls;
1824 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
1825 struct UDP_FragmentationContext *frag_ctx;
1826 struct UDP_MessageWrapper *udpw;
1827 struct UDPMessage *udp;
1828 char mbuf[udpmlen] GNUNET_ALIGN;
1830 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
1831 (NULL == plugin->sockv6) )
1832 return GNUNET_SYSERR;
1833 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
1834 (NULL == plugin->sockv4) )
1835 return GNUNET_SYSERR;
1836 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1839 return GNUNET_SYSERR;
1842 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1847 return GNUNET_SYSERR;
1849 LOG (GNUNET_ERROR_TYPE_DEBUG,
1850 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1852 GNUNET_i2s (&s->target),
1853 udp_address_to_string (plugin,
1854 s->address->address,
1855 s->address->address_length));
1857 udp = (struct UDPMessage *) mbuf;
1858 udp->header.size = htons (udpmlen);
1859 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1860 udp->reserved = htonl (0);
1861 udp->sender = *plugin->env->my_identity;
1863 /* We do not update the session time out here! Otherwise this
1864 * session will not timeout since we send keep alive before session
1867 * For UDP we update session timeout only on receive, this will
1868 * cover keep alives, since remote peer will reply with keep alive
1871 if (udpmlen <= UDP_MTU)
1873 /* unfragmented message */
1874 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1876 udpw->msg_buf = (char *) &udpw[1];
1877 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1878 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1879 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
1881 udpw->cont_cls = cont_cls;
1882 udpw->frag_ctx = NULL;
1883 udpw->qc = &qc_message_sent;
1884 udpw->qc_cls = plugin;
1885 memcpy (udpw->msg_buf,
1887 sizeof (struct UDPMessage));
1888 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
1893 GNUNET_STATISTICS_update (plugin->env->stats,
1894 "# UDP, unfragmented messages queued total",
1897 GNUNET_STATISTICS_update (plugin->env->stats,
1898 "# UDP, unfragmented bytes payload queued total",
1904 /* fragmented message */
1905 if (NULL != s->frag_ctx)
1906 return GNUNET_SYSERR;
1910 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
1911 frag_ctx->plugin = plugin;
1912 frag_ctx->session = s;
1913 frag_ctx->cont = cont;
1914 frag_ctx->cont_cls = cont_cls;
1915 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
1916 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1917 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1918 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1921 s->last_expected_msg_delay,
1922 s->last_expected_ack_delay,
1926 s->frag_ctx = frag_ctx;
1927 GNUNET_STATISTICS_update (plugin->env->stats,
1928 "# UDP, fragmented messages active",
1931 GNUNET_STATISTICS_update (plugin->env->stats,
1932 "# UDP, fragmented messages, total",
1935 GNUNET_STATISTICS_update (plugin->env->stats,
1936 "# UDP, fragmented bytes (payload)",
1937 frag_ctx->payload_size,
1940 notify_session_monitor (s->plugin,
1942 GNUNET_TRANSPORT_SS_UPDATE);
1943 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
1944 schedule_select_v4 (plugin);
1946 schedule_select_v6 (plugin);
1952 * Handle an ACK message.
1954 * @param plugin the UDP plugin
1955 * @param msg the (presumed) UDP ACK message
1956 * @param udp_addr sender address
1957 * @param udp_addr_len number of bytes in @a udp_addr
1960 read_process_ack (struct Plugin *plugin,
1961 const struct GNUNET_MessageHeader *msg,
1962 const union UdpAddress *udp_addr,
1963 socklen_t udp_addr_len)
1965 const struct GNUNET_MessageHeader *ack;
1966 const struct UDP_ACK_Message *udp_ack;
1967 struct GNUNET_HELLO_Address *address;
1969 struct GNUNET_TIME_Relative flow_delay;
1971 if (ntohs (msg->size)
1972 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
1974 GNUNET_break_op (0);
1977 udp_ack = (const struct UDP_ACK_Message *) msg;
1978 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
1982 GNUNET_HELLO_ADDRESS_INFO_NONE);
1983 s = udp_plugin_lookup_session (plugin,
1987 LOG (GNUNET_ERROR_TYPE_WARNING,
1988 "UDP session of address %s for ACK not found\n",
1989 udp_address_to_string (plugin,
1991 address->address_length));
1992 GNUNET_HELLO_address_free (address);
1995 if (NULL == s->frag_ctx)
1997 LOG (GNUNET_ERROR_TYPE_WARNING,
1998 "Fragmentation context of address %s for ACK not found\n",
1999 udp_address_to_string (plugin,
2001 address->address_length));
2002 GNUNET_HELLO_address_free (address);
2005 GNUNET_HELLO_address_free (address);
2007 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2008 LOG (GNUNET_ERROR_TYPE_DEBUG,
2009 "We received a sending delay of %s for %s\n",
2010 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2012 GNUNET_i2s (&udp_ack->sender));
2013 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2015 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2016 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2023 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2026 LOG (GNUNET_ERROR_TYPE_DEBUG,
2027 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2028 (unsigned int) ntohs (msg->size),
2029 GNUNET_i2s (&udp_ack->sender),
2030 udp_address_to_string (plugin,
2033 /* Expect more ACKs to arrive */
2037 LOG (GNUNET_ERROR_TYPE_DEBUG,
2038 "Message from %s at %s full ACK'ed\n",
2039 GNUNET_i2s (&udp_ack->sender),
2040 udp_address_to_string (plugin,
2044 /* Remove fragmented message after successful sending */
2045 fragmented_message_done (s->frag_ctx,
2050 /* ********************** Receiving ********************** */
2054 * Closure for #find_receive_context().
2056 struct FindReceiveContext
2059 * Where to store the result.
2061 struct DefragContext *rc;
2064 * Session associated with this context.
2066 struct Session *session;
2071 const union UdpAddress *udp_addr;
2074 * Number of bytes in @e udp_addr.
2076 size_t udp_addr_len;
2082 * Scan the heap for a receive context with the given address.
2084 * @param cls the `struct FindReceiveContext`
2085 * @param node internal node of the heap
2086 * @param element value stored at the node (a `struct ReceiveContext`)
2087 * @param cost cost associated with the node
2088 * @return #GNUNET_YES if we should continue to iterate,
2089 * #GNUNET_NO if not.
2092 find_receive_context (void *cls,
2093 struct GNUNET_CONTAINER_HeapNode *node,
2095 GNUNET_CONTAINER_HeapCostType cost)
2097 struct FindReceiveContext *frc = cls;
2098 struct DefragContext *e = element;
2100 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2101 (0 == memcmp (frc->udp_addr,
2103 frc->udp_addr_len)) )
2113 * Message tokenizer has broken up an incomming message. Pass it on
2116 * @param cls the `struct Plugin *`
2117 * @param client the `struct Session *`
2118 * @param hdr the actual message
2119 * @return #GNUNET_OK (always)
2122 process_inbound_tokenized_messages (void *cls,
2124 const struct GNUNET_MessageHeader *hdr)
2126 struct Plugin *plugin = cls;
2127 struct Session *session = client;
2129 if (GNUNET_YES == session->in_destroy)
2131 reschedule_session_timeout (session);
2132 session->flow_delay_for_other_peer
2133 = plugin->env->receive (plugin->env->cls,
2142 * Functions with this signature are called whenever we need to close
2143 * a session due to a disconnect or failure to establish a connection.
2145 * @param cls closure with the `struct Plugin`
2146 * @param s session to close down
2147 * @return #GNUNET_OK on success
2150 udp_disconnect_session (void *cls,
2153 struct Plugin *plugin = cls;
2154 struct UDP_MessageWrapper *udpw;
2155 struct UDP_MessageWrapper *next;
2156 struct FindReceiveContext frc;
2158 GNUNET_assert (GNUNET_YES != s->in_destroy);
2159 LOG (GNUNET_ERROR_TYPE_DEBUG,
2160 "Session %p to peer `%s' at address %s ended\n",
2162 GNUNET_i2s (&s->target),
2163 udp_address_to_string (plugin,
2164 s->address->address,
2165 s->address->address_length));
2166 if (NULL != s->timeout_task)
2168 GNUNET_SCHEDULER_cancel (s->timeout_task);
2169 s->timeout_task = NULL;
2171 if (NULL != s->frag_ctx)
2173 /* Remove fragmented message due to disconnect */
2174 fragmented_message_done (s->frag_ctx,
2179 frc.udp_addr = s->address->address;
2180 frc.udp_addr_len = s->address->address_length;
2181 /* Lookup existing receive context for this address */
2182 if (NULL != plugin->defrag_ctxs)
2184 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2185 &find_receive_context,
2189 struct DefragContext *d_ctx = frc.rc;
2191 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2192 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2193 GNUNET_free (d_ctx);
2196 next = plugin->ipv4_queue_head;
2197 while (NULL != (udpw = next))
2200 if (udpw->session == s)
2204 udpw->qc (udpw->qc_cls,
2210 next = plugin->ipv6_queue_head;
2211 while (NULL != (udpw = next))
2214 if (udpw->session == s)
2218 udpw->qc (udpw->qc_cls,
2224 notify_session_monitor (s->plugin,
2226 GNUNET_TRANSPORT_SS_DONE);
2227 plugin->env->session_end (plugin->env->cls,
2231 if ( (NULL != s->frag_ctx) &&
2232 (NULL != s->frag_ctx->cont) )
2234 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2235 later, as it might be in use right now */
2236 LOG (GNUNET_ERROR_TYPE_DEBUG,
2237 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2238 GNUNET_i2s (&s->target));
2239 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2242 s->frag_ctx->payload_size,
2243 s->frag_ctx->on_wire_size);
2246 GNUNET_assert (GNUNET_YES ==
2247 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2250 GNUNET_STATISTICS_set (plugin->env->stats,
2251 "# UDP sessions active",
2252 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2256 s->in_destroy = GNUNET_YES;
2267 * Destroy a session, plugin is being unloaded.
2269 * @param cls the `struct Plugin`
2270 * @param key hash of public key of target peer
2271 * @param value a `struct PeerSession *` to clean up
2272 * @return #GNUNET_OK (continue to iterate)
2275 disconnect_and_free_it (void *cls,
2276 const struct GNUNET_PeerIdentity *key,
2279 struct Plugin *plugin = cls;
2281 udp_disconnect_session (plugin,
2288 * Disconnect from a remote node. Clean up session if we have one for
2291 * @param cls closure for this call (should be handle to Plugin)
2292 * @param target the peeridentity of the peer to disconnect
2293 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2296 udp_disconnect (void *cls,
2297 const struct GNUNET_PeerIdentity *target)
2299 struct Plugin *plugin = cls;
2301 LOG (GNUNET_ERROR_TYPE_DEBUG,
2302 "Disconnecting from peer `%s'\n",
2303 GNUNET_i2s (target));
2304 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2306 &disconnect_and_free_it,
2312 * Session was idle, so disconnect it.
2314 * @param cls the `struct Session` to time out
2315 * @param tc scheduler context
2318 session_timeout (void *cls,
2319 const struct GNUNET_SCHEDULER_TaskContext *tc)
2321 struct Session *s = cls;
2322 struct Plugin *plugin = s->plugin;
2323 struct GNUNET_TIME_Relative left;
2325 s->timeout_task = NULL;
2326 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2327 if (left.rel_value_us > 0)
2329 /* not actually our turn yet, but let's at least update
2330 the monitor, it may think we're about to die ... */
2331 notify_session_monitor (s->plugin,
2333 GNUNET_TRANSPORT_SS_UPDATE);
2334 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2339 LOG (GNUNET_ERROR_TYPE_DEBUG,
2340 "Session %p was idle for %s, disconnecting\n",
2342 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2344 /* call session destroy function */
2345 udp_disconnect_session (plugin,
2351 * Allocate a new session for the given endpoint address.
2352 * Note that this function does not inform the service
2353 * of the new session, this is the responsibility of the
2354 * caller (if needed).
2356 * @param cls the `struct Plugin`
2357 * @param address address of the other peer to use
2358 * @param network_type network type the address belongs to
2359 * @return NULL on error, otherwise session handle
2361 static struct Session *
2362 udp_plugin_create_session (void *cls,
2363 const struct GNUNET_HELLO_Address *address,
2364 enum GNUNET_ATS_Network_Type network_type)
2366 struct Plugin *plugin = cls;
2369 s = GNUNET_new (struct Session);
2371 s->address = GNUNET_HELLO_address_copy (address);
2372 s->target = address->peer;
2373 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2375 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2376 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
2377 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2378 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2379 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2382 s->scope = network_type;
2384 LOG (GNUNET_ERROR_TYPE_DEBUG,
2385 "Creating new session %p for peer `%s' address `%s'\n",
2387 GNUNET_i2s (&address->peer),
2388 udp_address_to_string (plugin,
2390 address->address_length));
2391 GNUNET_assert (GNUNET_OK ==
2392 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2395 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2396 GNUNET_STATISTICS_set (plugin->env->stats,
2397 "# UDP sessions active",
2398 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2405 * Creates a new outbound session the transport service will use to
2406 * send data to the peer.
2408 * @param cls the `struct Plugin *`
2409 * @param address the address
2410 * @return the session or NULL of max connections exceeded
2412 static struct Session *
2413 udp_plugin_get_session (void *cls,
2414 const struct GNUNET_HELLO_Address *address)
2416 struct Plugin *plugin = cls;
2418 enum GNUNET_ATS_Network_Type network_type;
2419 const struct IPv4UdpAddress *udp_v4;
2420 const struct IPv6UdpAddress *udp_v6;
2422 if (NULL == address)
2427 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2428 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2430 GNUNET_break_op (0);
2433 if (NULL != (s = udp_plugin_lookup_session (cls,
2437 /* need to create new session */
2438 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2440 struct sockaddr_in v4;
2442 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2443 memset (&v4, '\0', sizeof (v4));
2444 v4.sin_family = AF_INET;
2445 #if HAVE_SOCKADDR_IN_SIN_LEN
2446 v4.sin_len = sizeof (struct sockaddr_in);
2448 v4.sin_port = udp_v4->u4_port;
2449 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2450 network_type = plugin->env->get_address_type (plugin->env->cls,
2451 (const struct sockaddr *) &v4,
2454 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2456 struct sockaddr_in6 v6;
2458 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2459 memset (&v6, '\0', sizeof (v6));
2460 v6.sin6_family = AF_INET6;
2461 #if HAVE_SOCKADDR_IN_SIN_LEN
2462 v6.sin6_len = sizeof (struct sockaddr_in6);
2464 v6.sin6_port = udp_v6->u6_port;
2465 v6.sin6_addr = udp_v6->ipv6_addr;
2466 network_type = plugin->env->get_address_type (plugin->env->cls,
2467 (const struct sockaddr *) &v6,
2470 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2471 return udp_plugin_create_session (cls,
2478 * We've received a UDP Message. Process it (pass contents to main service).
2480 * @param plugin plugin context
2481 * @param msg the message
2482 * @param udp_addr sender address
2483 * @param udp_addr_len number of bytes in @a udp_addr
2484 * @param network_type network type the address belongs to
2487 process_udp_message (struct Plugin *plugin,
2488 const struct UDPMessage *msg,
2489 const union UdpAddress *udp_addr,
2490 size_t udp_addr_len,
2491 enum GNUNET_ATS_Network_Type network_type)
2494 struct GNUNET_HELLO_Address *address;
2496 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2497 if (0 != ntohl (msg->reserved))
2502 if (ntohs (msg->header.size)
2503 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2509 address = GNUNET_HELLO_address_allocate (&msg->sender,
2513 GNUNET_HELLO_ADDRESS_INFO_NONE);
2515 (s = udp_plugin_lookup_session (plugin,
2518 s = udp_plugin_create_session (plugin,
2521 plugin->env->session_start (plugin->env->cls,
2525 notify_session_monitor (s->plugin,
2527 GNUNET_TRANSPORT_SS_INIT);
2528 notify_session_monitor (s->plugin,
2530 GNUNET_TRANSPORT_SS_UP);
2532 GNUNET_free (address);
2535 GNUNET_SERVER_mst_receive (plugin->mst,
2537 (const char *) &msg[1],
2538 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2542 if ( (0 == s->rc) &&
2543 (GNUNET_YES == s->in_destroy) )
2549 * Process a defragmented message.
2551 * @param cls the `struct DefragContext *`
2552 * @param msg the message
2555 fragment_msg_proc (void *cls,
2556 const struct GNUNET_MessageHeader *msg)
2558 struct DefragContext *dc = cls;
2559 const struct UDPMessage *um;
2561 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2563 GNUNET_break_op (0);
2566 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2568 GNUNET_break_op (0);
2571 um = (const struct UDPMessage *) msg;
2572 dc->sender = um->sender;
2573 dc->have_sender = GNUNET_YES;
2574 process_udp_message (dc->plugin,
2583 * We finished sending an acknowledgement. Update
2586 * @param cls the `struct Plugin`
2587 * @param udpw message queue entry of the ACK
2588 * @param result #GNUNET_OK if the transmission worked,
2589 * #GNUNET_SYSERR if we failed to send the ACK
2592 ack_message_sent (void *cls,
2593 struct UDP_MessageWrapper *udpw,
2596 struct Plugin *plugin = cls;
2598 if (GNUNET_OK == result)
2600 GNUNET_STATISTICS_update (plugin->env->stats,
2601 "# UDP, ACK messages sent",
2607 GNUNET_STATISTICS_update (plugin->env->stats,
2608 "# UDP, ACK transmissions failed",
2616 * Transmit an acknowledgement.
2618 * @param cls the `struct DefragContext *`
2619 * @param id message ID (unused)
2620 * @param msg ack to transmit
2623 ack_proc (void *cls,
2625 const struct GNUNET_MessageHeader *msg)
2627 struct DefragContext *rc = cls;
2628 struct Plugin *plugin = rc->plugin;
2629 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2630 struct UDP_ACK_Message *udp_ack;
2632 struct UDP_MessageWrapper *udpw;
2634 struct GNUNET_HELLO_Address *address;
2636 if (GNUNET_NO == rc->have_sender)
2638 /* tried to defragment but never succeeded, hence will not ACK */
2639 GNUNET_break_op (0);
2642 address = GNUNET_HELLO_address_allocate (&rc->sender,
2646 GNUNET_HELLO_ADDRESS_INFO_NONE);
2647 s = udp_plugin_lookup_session (plugin,
2649 GNUNET_HELLO_address_free (address);
2652 LOG (GNUNET_ERROR_TYPE_ERROR,
2653 "Trying to transmit ACK to peer `%s' but no session found!\n",
2654 udp_address_to_string (plugin,
2657 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2658 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2660 GNUNET_STATISTICS_update (plugin->env->stats,
2661 "# UDP, ACK transmissions failed",
2666 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2667 delay = s->flow_delay_for_other_peer.rel_value_us;
2671 LOG (GNUNET_ERROR_TYPE_DEBUG,
2672 "Sending ACK to `%s' including delay of %s\n",
2673 udp_address_to_string (plugin,
2676 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2678 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2679 udpw->msg_size = msize;
2680 udpw->payload_size = 0;
2682 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2683 udpw->msg_buf = (char *) &udpw[1];
2684 udpw->qc = &ack_message_sent;
2685 udpw->qc_cls = plugin;
2686 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2687 udp_ack->header.size = htons ((uint16_t) msize);
2688 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2689 udp_ack->delay = htonl (delay);
2690 udp_ack->sender = *plugin->env->my_identity;
2691 memcpy (&udp_ack[1],
2696 notify_session_monitor (plugin,
2698 GNUNET_TRANSPORT_SS_UPDATE);
2699 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2700 schedule_select_v4 (plugin);
2702 schedule_select_v6 (plugin);
2707 * We received a fragment, process it.
2709 * @param plugin our plugin
2710 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2711 * @param udp_addr sender address
2712 * @param udp_addr_len number of bytes in @a udp_addr
2713 * @param network_type network type the address belongs to
2716 read_process_fragment (struct Plugin *plugin,
2717 const struct GNUNET_MessageHeader *msg,
2718 const union UdpAddress *udp_addr,
2719 size_t udp_addr_len,
2720 enum GNUNET_ATS_Network_Type network_type)
2722 struct DefragContext *d_ctx;
2723 struct GNUNET_TIME_Absolute now;
2724 struct FindReceiveContext frc;
2727 frc.udp_addr = udp_addr;
2728 frc.udp_addr_len = udp_addr_len;
2730 /* Lookup existing receive context for this address */
2731 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2732 &find_receive_context,
2734 now = GNUNET_TIME_absolute_get ();
2739 /* Create a new defragmentation context */
2740 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2744 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2745 d_ctx->udp_addr_len = udp_addr_len;
2746 d_ctx->network_type = network_type;
2747 d_ctx->plugin = plugin;
2748 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2750 UDP_MAX_MESSAGES_IN_DEFRAG,
2754 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2756 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2757 LOG (GNUNET_ERROR_TYPE_DEBUG,
2758 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2759 (unsigned int) ntohs (msg->size),
2760 udp_address_to_string (plugin,
2766 LOG (GNUNET_ERROR_TYPE_DEBUG,
2767 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2768 (unsigned int) ntohs (msg->size),
2769 udp_address_to_string (plugin,
2775 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
2778 /* keep this 'rc' from expiring */
2779 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2781 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2783 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2784 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2786 /* remove 'rc' that was inactive the longest */
2787 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2788 GNUNET_assert (NULL != d_ctx);
2789 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2790 GNUNET_free (d_ctx);
2791 GNUNET_STATISTICS_update (plugin->env->stats,
2792 "# UDP, Defragmentations aborted",
2800 * Read and process a message from the given socket.
2802 * @param plugin the overall plugin
2803 * @param rsock socket to read from
2806 udp_select_read (struct Plugin *plugin,
2807 struct GNUNET_NETWORK_Handle *rsock)
2810 struct sockaddr_storage addr;
2811 char buf[65536] GNUNET_ALIGN;
2813 const struct GNUNET_MessageHeader *msg;
2814 struct IPv4UdpAddress v4;
2815 struct IPv6UdpAddress v6;
2816 const struct sockaddr *sa;
2817 const struct sockaddr_in *sa4;
2818 const struct sockaddr_in6 *sa6;
2819 const union UdpAddress *int_addr;
2820 size_t int_addr_len;
2821 enum GNUNET_ATS_Network_Type network_type;
2823 fromlen = sizeof (addr);
2827 size = GNUNET_NETWORK_socket_recvfrom (rsock,
2830 (struct sockaddr *) &addr,
2832 sa = (const struct sockaddr *) &addr;
2834 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2835 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2836 * on this socket has failed.
2838 * WSAECONNRESET - The virtual circuit was reset by the remote side
2839 * executing a hard or abortive close. The application should close
2840 * the socket; it is no longer usable. On a UDP-datagram socket this
2841 * error indicates a previous send operation resulted in an ICMP Port
2842 * Unreachable message.
2844 if ( (-1 == size) &&
2845 (ECONNRESET == errno) )
2850 LOG (GNUNET_ERROR_TYPE_DEBUG,
2851 "UDP failed to receive data: %s\n",
2853 /* Connection failure or something. Not a protocol violation. */
2856 if (size < sizeof(struct GNUNET_MessageHeader))
2858 LOG (GNUNET_ERROR_TYPE_WARNING,
2859 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2860 (unsigned int ) size,
2863 /* _MAY_ be a connection failure (got partial message) */
2864 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2865 GNUNET_break_op (0);
2868 msg = (const struct GNUNET_MessageHeader *) buf;
2869 LOG (GNUNET_ERROR_TYPE_DEBUG,
2870 "UDP received %u-byte message from `%s' type %u\n",
2871 (unsigned int) size,
2875 if (size != ntohs (msg->size))
2877 LOG (GNUNET_ERROR_TYPE_WARNING,
2878 "UDP malformed message header from %s\n",
2879 (unsigned int) size,
2882 GNUNET_break_op (0);
2885 GNUNET_STATISTICS_update (plugin->env->stats,
2886 "# UDP, total bytes received",
2889 network_type = plugin->env->get_address_type (plugin->env->cls,
2892 switch (sa->sa_family)
2895 sa4 = (const struct sockaddr_in *) &addr;
2897 v4.ipv4_addr = sa4->sin_addr.s_addr;
2898 v4.u4_port = sa4->sin_port;
2899 int_addr = (union UdpAddress *) &v4;
2900 int_addr_len = sizeof (v4);
2903 sa6 = (const struct sockaddr_in6 *) &addr;
2905 v6.ipv6_addr = sa6->sin6_addr;
2906 v6.u6_port = sa6->sin6_port;
2907 int_addr = (union UdpAddress *) &v6;
2908 int_addr_len = sizeof (v6);
2915 switch (ntohs (msg->type))
2917 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2918 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2919 udp_broadcast_receive (plugin,
2926 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2927 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2932 process_udp_message (plugin,
2933 (const struct UDPMessage *) msg,
2938 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2939 read_process_ack (plugin,
2944 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2945 read_process_fragment (plugin,
2959 * Removes messages from the transmission queue that have
2960 * timed out, and then selects a message that should be
2963 * @param plugin the UDP plugin
2964 * @param sock which socket should we process the queue for (v4 or v6)
2965 * @return message selected for transmission, or NULL for none
2967 static struct UDP_MessageWrapper *
2968 remove_timeout_messages_and_select (struct Plugin *plugin,
2969 struct GNUNET_NETWORK_Handle *sock)
2971 struct UDP_MessageWrapper *udpw;
2972 struct GNUNET_TIME_Relative remaining;
2973 struct Session *session;
2976 removed = GNUNET_NO;
2977 udpw = (sock == plugin->sockv4)
2978 ? plugin->ipv4_queue_head
2979 : plugin->ipv6_queue_head;
2980 while (NULL != udpw)
2982 session = udpw->session;
2983 /* Find messages with timeout */
2984 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2985 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2987 /* Message timed out */
2988 udpw->qc (udpw->qc_cls,
2991 /* Remove message */
2992 removed = GNUNET_YES;
2997 if (sock == plugin->sockv4)
2999 udpw = plugin->ipv4_queue_head;
3001 else if (sock == plugin->sockv6)
3003 udpw = plugin->ipv6_queue_head;
3007 GNUNET_break (0); /* should never happen */
3010 GNUNET_STATISTICS_update (plugin->env->stats,
3011 "# messages discarded due to timeout",
3017 /* Message did not time out, check flow delay */
3018 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
3019 if (0 == remaining.rel_value_us)
3021 /* this message is not delayed */
3022 LOG (GNUNET_ERROR_TYPE_DEBUG,
3023 "Message for peer `%s' (%u bytes) is not delayed \n",
3024 GNUNET_i2s (&udpw->session->target),
3025 udpw->payload_size);
3026 break; /* Found message to send, break */
3030 /* Message is delayed, try next */
3031 LOG (GNUNET_ERROR_TYPE_DEBUG,
3032 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3033 GNUNET_i2s (&udpw->session->target),
3035 GNUNET_STRINGS_relative_time_to_string (remaining,
3041 if (GNUNET_YES == removed)
3042 notify_session_monitor (session->plugin,
3044 GNUNET_TRANSPORT_SS_UPDATE);
3050 * We failed to transmit a message via UDP. Generate
3051 * a descriptive error message.
3053 * @param plugin our plugin
3054 * @param sa target address we were trying to reach
3055 * @param slen number of bytes in @a sa
3056 * @param error the errno value returned from the sendto() call
3059 analyze_send_error (struct Plugin *plugin,
3060 const struct sockaddr *sa,
3064 enum GNUNET_ATS_Network_Type type;
3066 type = plugin->env->get_address_type (plugin->env->cls,
3069 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3070 (GNUNET_ATS_NET_WAN == type) ) &&
3071 ( (ENETUNREACH == errno) ||
3072 (ENETDOWN == errno) ) )
3074 if (slen == sizeof (struct sockaddr_in))
3076 /* IPv4: "Network unreachable" or "Network down"
3078 * This indicates we do not have connectivity
3080 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3081 _("UDP could not transmit message to `%s': "
3082 "Network seems down, please check your network configuration\n"),
3086 if (slen == sizeof (struct sockaddr_in6))
3088 /* IPv6: "Network unreachable" or "Network down"
3090 * This indicates that this system is IPv6 enabled, but does not
3091 * have a valid global IPv6 address assigned or we do not have
3094 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3095 _("UDP could not transmit IPv6 message! "
3096 "Please check your network configuration and disable IPv6 if your "
3097 "connection does not have a global IPv6 address\n"));
3102 LOG (GNUNET_ERROR_TYPE_WARNING,
3103 "UDP could not transmit message to `%s': `%s'\n",
3112 * It is time to try to transmit a UDP message. Select one
3115 * @param plugin the plugin
3116 * @param sock which socket (v4/v6) to send on
3119 udp_select_send (struct Plugin *plugin,
3120 struct GNUNET_NETWORK_Handle *sock)
3124 const struct sockaddr *a;
3125 const struct IPv4UdpAddress *u4;
3126 struct sockaddr_in a4;
3127 const struct IPv6UdpAddress *u6;
3128 struct sockaddr_in6 a6;
3129 struct UDP_MessageWrapper *udpw;
3131 /* Find message(s) to send */
3132 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3135 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3137 u4 = udpw->session->address->address;
3141 a4.sin_family = AF_INET;
3142 #if HAVE_SOCKADDR_IN_SIN_LEN
3143 a4.sin_len = sizeof (a4);
3145 a4.sin_port = u4->u4_port;
3146 a4.sin_addr.s_addr = u4->ipv4_addr;
3147 a = (const struct sockaddr *) &a4;
3150 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3152 u6 = udpw->session->address->address;
3156 a6.sin6_family = AF_INET6;
3157 #if HAVE_SOCKADDR_IN_SIN_LEN
3158 a6.sin6_len = sizeof (a6);
3160 a6.sin6_port = u6->u6_port;
3161 a6.sin6_addr = u6->ipv6_addr;
3162 a = (const struct sockaddr *) &a6;
3168 udpw->qc (udpw->qc_cls,
3173 notify_session_monitor (plugin,
3175 GNUNET_TRANSPORT_SS_UPDATE);
3179 sent = GNUNET_NETWORK_socket_sendto (sock,
3184 if (GNUNET_SYSERR == sent)
3187 analyze_send_error (plugin,
3191 udpw->qc (udpw->qc_cls,
3194 GNUNET_STATISTICS_update (plugin->env->stats,
3195 "# UDP, total, bytes, sent, failure",
3198 GNUNET_STATISTICS_update (plugin->env->stats,
3199 "# UDP, total, messages, sent, failure",
3206 LOG (GNUNET_ERROR_TYPE_DEBUG,
3207 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3208 (unsigned int) (udpw->msg_size),
3209 GNUNET_i2s (&udpw->session->target),
3213 (sent < 0) ? STRERROR (errno) : "ok");
3214 GNUNET_STATISTICS_update (plugin->env->stats,
3215 "# UDP, total, bytes, sent, success",
3218 GNUNET_STATISTICS_update (plugin->env->stats,
3219 "# UDP, total, messages, sent, success",
3222 if (NULL != udpw->frag_ctx)
3223 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3224 udpw->qc (udpw->qc_cls,
3230 notify_session_monitor (plugin,
3232 GNUNET_TRANSPORT_SS_UPDATE);
3238 /* ***************** Event loop (part 2) *************** */
3242 * We have been notified that our readset has something to read. We don't
3243 * know which socket needs to be read, so we have to check each one
3244 * Then reschedule this function to be called again once more is available.
3246 * @param cls the plugin handle
3247 * @param tc the scheduling context
3250 udp_plugin_select_v4 (void *cls,
3251 const struct GNUNET_SCHEDULER_TaskContext *tc)
3253 struct Plugin *plugin = cls;
3255 plugin->select_task_v4 = NULL;
3256 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3258 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3259 (NULL != plugin->sockv4) &&
3260 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3262 udp_select_read (plugin,
3264 udp_select_send (plugin,
3266 schedule_select_v4 (plugin);
3271 * We have been notified that our readset has something to read. We don't
3272 * know which socket needs to be read, so we have to check each one
3273 * Then reschedule this function to be called again once more is available.
3275 * @param cls the plugin handle
3276 * @param tc the scheduling context
3279 udp_plugin_select_v6 (void *cls,
3280 const struct GNUNET_SCHEDULER_TaskContext *tc)
3282 struct Plugin *plugin = cls;
3284 plugin->select_task_v6 = NULL;
3285 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3287 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3288 (NULL != plugin->sockv6) &&
3289 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3291 udp_select_read (plugin,
3293 udp_select_send (plugin,
3295 schedule_select_v6 (plugin);
3299 /* ******************* Initialization *************** */
3303 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3305 * @param plugin the plugin to initialize
3306 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3307 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3308 * @return number of sockets that were successfully bound
3311 setup_sockets (struct Plugin *plugin,
3312 const struct sockaddr_in6 *bind_v6,
3313 const struct sockaddr_in *bind_v4)
3316 int sockets_created = 0;
3317 struct sockaddr_in6 server_addrv6;
3318 struct sockaddr_in server_addrv4;
3319 const struct sockaddr *server_addr;
3320 const struct sockaddr *addrs[2];
3321 socklen_t addrlens[2];
3325 /* Create IPv6 socket */
3327 if (GNUNET_YES == plugin->enable_ipv6)
3329 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3332 if (NULL == plugin->sockv6)
3334 LOG (GNUNET_ERROR_TYPE_INFO,
3335 _("Disabling IPv6 since it is not supported on this system!\n"));
3336 plugin->enable_ipv6 = GNUNET_NO;
3340 memset (&server_addrv6,
3342 sizeof(struct sockaddr_in6));
3343 #if HAVE_SOCKADDR_IN_SIN_LEN
3344 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3346 server_addrv6.sin6_family = AF_INET6;
3347 if (NULL != bind_v6)
3348 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3350 server_addrv6.sin6_addr = in6addr_any;
3352 if (0 == plugin->port) /* autodetect */
3353 server_addrv6.sin6_port
3354 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3358 server_addrv6.sin6_port = htons (plugin->port);
3359 addrlen = sizeof (struct sockaddr_in6);
3360 server_addr = (const struct sockaddr *) &server_addrv6;
3365 LOG(GNUNET_ERROR_TYPE_DEBUG,
3366 "Binding to IPv6 `%s'\n",
3367 GNUNET_a2s (server_addr,
3371 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3376 if (0 != plugin->port)
3378 tries = 10; /* fail immediately */
3379 break; /* bind failed on specific port */
3382 server_addrv6.sin6_port
3383 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3390 GNUNET_NETWORK_socket_close (plugin->sockv6);
3391 plugin->enable_ipv6 = GNUNET_NO;
3392 plugin->sockv6 = NULL;
3396 plugin->port = ntohs (server_addrv6.sin6_port);
3398 if (NULL != plugin->sockv6)
3400 LOG (GNUNET_ERROR_TYPE_DEBUG,
3401 "IPv6 UDP socket created listinging at %s\n",
3402 GNUNET_a2s (server_addr,
3404 addrs[sockets_created] = server_addr;
3405 addrlens[sockets_created] = addrlen;
3410 LOG (GNUNET_ERROR_TYPE_WARNING,
3411 _("Failed to bind UDP socket to %s: %s\n"),
3412 GNUNET_a2s (server_addr,
3419 /* Create IPv4 socket */
3421 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3424 if (NULL == plugin->sockv4)
3426 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3428 LOG (GNUNET_ERROR_TYPE_INFO,
3429 _("Disabling IPv4 since it is not supported on this system!\n"));
3430 plugin->enable_ipv4 = GNUNET_NO;
3434 memset (&server_addrv4,
3436 sizeof(struct sockaddr_in));
3437 #if HAVE_SOCKADDR_IN_SIN_LEN
3438 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3440 server_addrv4.sin_family = AF_INET;
3441 if (NULL != bind_v4)
3442 server_addrv4.sin_addr = bind_v4->sin_addr;
3444 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3446 if (0 == plugin->port)
3448 server_addrv4.sin_port
3449 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3453 server_addrv4.sin_port = htons (plugin->port);
3455 addrlen = sizeof (struct sockaddr_in);
3456 server_addr = (const struct sockaddr *) &server_addrv4;
3461 LOG (GNUNET_ERROR_TYPE_DEBUG,
3462 "Binding to IPv4 `%s'\n",
3463 GNUNET_a2s (server_addr,
3468 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3473 if (0 != plugin->port)
3475 tries = 10; /* fail */
3476 break; /* bind failed on specific port */
3480 server_addrv4.sin_port
3481 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3488 GNUNET_NETWORK_socket_close (plugin->sockv4);
3489 plugin->enable_ipv4 = GNUNET_NO;
3490 plugin->sockv4 = NULL;
3494 plugin->port = ntohs (server_addrv4.sin_port);
3497 if (NULL != plugin->sockv4)
3499 LOG (GNUNET_ERROR_TYPE_DEBUG,
3500 "IPv4 socket created on port %s\n",
3501 GNUNET_a2s (server_addr,
3503 addrs[sockets_created] = server_addr;
3504 addrlens[sockets_created] = addrlen;
3509 LOG (GNUNET_ERROR_TYPE_ERROR,
3510 _("Failed to bind UDP socket to %s: %s\n"),
3511 GNUNET_a2s (server_addr,
3517 if (0 == sockets_created)
3519 LOG (GNUNET_ERROR_TYPE_WARNING,
3520 _("Failed to open UDP sockets\n"));
3521 return 0; /* No sockets created, return */
3523 schedule_select_v4 (plugin);
3524 schedule_select_v6 (plugin);
3525 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3531 &udp_nat_port_map_callback,
3534 return sockets_created;
3539 * The exported method. Makes the core api available via a global and
3540 * returns the udp transport API.
3542 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3543 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3546 libgnunet_plugin_transport_udp_init (void *cls)
3548 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3549 struct GNUNET_TRANSPORT_PluginFunctions *api;
3551 unsigned long long port;
3552 unsigned long long aport;
3553 unsigned long long udp_max_bps;
3554 unsigned long long enable_v6;
3555 unsigned long long enable_broadcasting;
3556 unsigned long long enable_broadcasting_recv;
3557 char *bind4_address;
3558 char *bind6_address;
3559 struct GNUNET_TIME_Relative interval;
3560 struct sockaddr_in server_addrv4;
3561 struct sockaddr_in6 server_addrv6;
3566 if (NULL == env->receive)
3568 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3569 initialze the plugin or the API */
3570 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3572 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3573 api->address_to_string = &udp_address_to_string;
3574 api->string_to_address = &udp_string_to_address;
3578 /* Get port number: port == 0 : autodetect a port,
3579 * > 0 : use this port, not given : 2086 default */
3581 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3588 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3591 _("must be in [0,65535]"));
3595 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3602 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3605 _("must be in [0,65535]"));
3610 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3613 enable_v6 = GNUNET_NO;
3615 enable_v6 = GNUNET_YES;
3617 have_bind4 = GNUNET_NO;
3618 memset (&server_addrv4,
3620 sizeof (server_addrv4));
3622 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3627 LOG (GNUNET_ERROR_TYPE_DEBUG,
3628 "Binding UDP plugin to specific address: `%s'\n",
3630 if (1 != inet_pton (AF_INET,
3632 &server_addrv4.sin_addr))
3634 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3637 _("must be valid IPv4 address"));
3638 GNUNET_free (bind4_address);
3641 have_bind4 = GNUNET_YES;
3643 GNUNET_free_non_null (bind4_address);
3644 have_bind6 = GNUNET_NO;
3645 memset (&server_addrv6,
3647 sizeof (server_addrv6));
3649 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3654 LOG (GNUNET_ERROR_TYPE_DEBUG,
3655 "Binding udp plugin to specific address: `%s'\n",
3657 if (1 != inet_pton (AF_INET6,
3659 &server_addrv6.sin6_addr))
3661 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3664 _("must be valid IPv6 address"));
3665 GNUNET_free (bind6_address);
3668 have_bind6 = GNUNET_YES;
3670 GNUNET_free_non_null (bind6_address);
3672 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3675 if (enable_broadcasting == GNUNET_SYSERR)
3676 enable_broadcasting = GNUNET_NO;
3678 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3680 "BROADCAST_RECEIVE");
3681 if (enable_broadcasting_recv == GNUNET_SYSERR)
3682 enable_broadcasting_recv = GNUNET_YES;
3684 if (GNUNET_SYSERR ==
3685 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3687 "BROADCAST_INTERVAL",
3690 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3694 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3699 /* 50 MB/s == infinity for practical purposes */
3700 udp_max_bps = 1024 * 1024 * 50;
3703 p = GNUNET_new (struct Plugin);
3706 p->broadcast_interval = interval;
3707 p->enable_ipv6 = enable_v6;
3708 p->enable_ipv4 = GNUNET_YES; /* default */
3709 p->enable_broadcasting = enable_broadcasting;
3710 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3712 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3714 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3715 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3717 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3720 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3722 res = setup_sockets (p,
3723 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3724 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3726 ( (NULL == p->sockv4) &&
3727 (NULL == p->sockv6) ) )
3729 LOG (GNUNET_ERROR_TYPE_ERROR,
3730 _("Failed to create UDP network sockets\n"));
3731 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3732 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3733 GNUNET_SERVER_mst_destroy (p->mst);
3738 /* Setup broadcasting and receiving beacons */
3743 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3745 api->disconnect_session = &udp_disconnect_session;
3746 api->query_keepalive_factor = &udp_query_keepalive_factor;
3747 api->disconnect_peer = &udp_disconnect;
3748 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3749 api->address_to_string = &udp_address_to_string;
3750 api->string_to_address = &udp_string_to_address;
3751 api->check_address = &udp_plugin_check_address;
3752 api->get_session = &udp_plugin_get_session;
3753 api->send = &udp_plugin_send;
3754 api->get_network = &udp_get_network;
3755 api->update_session_timeout = &udp_plugin_update_session_timeout;
3756 api->setup_monitor = &udp_plugin_setup_monitor;
3762 * Function called on each entry in the defragmentation heap to
3766 * @param node node in the heap (to be removed)
3767 * @param element a `struct DefragContext` to be cleaned up
3768 * @param cost unused
3769 * @return #GNUNET_YES
3772 heap_cleanup_iterator (void *cls,
3773 struct GNUNET_CONTAINER_HeapNode *node,
3775 GNUNET_CONTAINER_HeapCostType cost)
3777 struct DefragContext *d_ctx = element;
3779 GNUNET_CONTAINER_heap_remove_node (node);
3780 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3781 GNUNET_free (d_ctx);
3787 * The exported method. Makes the core api available via a global and
3788 * returns the udp transport API.
3790 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3794 libgnunet_plugin_transport_udp_done (void *cls)
3796 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3797 struct Plugin *plugin = api->cls;
3798 struct PrettyPrinterContext *cur;
3799 struct UDP_MessageWrapper *udpw;
3806 stop_broadcast (plugin);
3807 if (NULL != plugin->select_task_v4)
3809 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
3810 plugin->select_task_v4 = NULL;
3812 if (NULL != plugin->select_task_v6)
3814 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3815 plugin->select_task_v6 = NULL;
3817 if (NULL != plugin->sockv4)
3819 GNUNET_break (GNUNET_OK ==
3820 GNUNET_NETWORK_socket_close (plugin->sockv4));
3821 plugin->sockv4 = NULL;
3823 if (NULL != plugin->sockv6)
3825 GNUNET_break (GNUNET_OK ==
3826 GNUNET_NETWORK_socket_close (plugin->sockv6));
3827 plugin->sockv6 = NULL;
3829 if (NULL != plugin->nat)
3831 GNUNET_NAT_unregister (plugin->nat);
3834 if (NULL != plugin->defrag_ctxs)
3836 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3837 &heap_cleanup_iterator,
3839 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3840 plugin->defrag_ctxs = NULL;
3842 if (NULL != plugin->mst)
3844 GNUNET_SERVER_mst_destroy (plugin->mst);
3847 while (NULL != (udpw = plugin->ipv4_queue_head))
3851 udpw->qc (udpw->qc_cls,
3856 while (NULL != (udpw = plugin->ipv6_queue_head))
3860 udpw->qc (udpw->qc_cls,
3865 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3866 &disconnect_and_free_it,
3868 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3870 while (NULL != (cur = plugin->ppc_dll_head))
3873 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3874 plugin->ppc_dll_tail,
3876 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3879 GNUNET_free (plugin);
3884 /* end of plugin_transport_udp.c */