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.
154 struct GNUNET_ATS_Session
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 transmissions we received from other peer.
178 * This is for full messages, the value needs to be adjusted for
179 * fragmented messages.
181 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
184 * Session timeout task
186 struct GNUNET_SCHEDULER_Task *timeout_task;
189 * When does this session time out?
191 struct GNUNET_TIME_Absolute timeout;
194 * What time did we last transmit?
196 struct GNUNET_TIME_Absolute last_transmit_time;
199 * expected delay for ACKs
201 struct GNUNET_TIME_Relative last_expected_ack_delay;
204 * desired delay between UDP messages
206 struct GNUNET_TIME_Relative last_expected_msg_delay;
211 struct GNUNET_HELLO_Address *address;
214 * Number of bytes waiting for transmission to this peer.
216 unsigned long long bytes_in_queue;
219 * Number of messages waiting for transmission to this peer.
221 unsigned int msgs_in_queue;
224 * Reference counter to indicate that this session is
225 * currently being used and must not be destroyed;
226 * setting @e in_destroy will destroy it as soon as
232 * Network type of the address.
234 enum GNUNET_ATS_Network_Type scope;
237 * Is this session about to be destroyed (sometimes we cannot
238 * destroy a session immediately as below us on the stack
239 * there might be code that still uses it; in this case,
240 * @e rc is non-zero).
248 * Data structure to track defragmentation contexts based
249 * on the source of the UDP traffic.
255 * Defragmentation context.
257 struct GNUNET_DEFRAGMENT_Context *defrag;
260 * Reference to master plugin struct.
262 struct Plugin *plugin;
265 * Node in the defrag heap.
267 struct GNUNET_CONTAINER_HeapNode *hnode;
270 * Source address this receive context is for (allocated at the
271 * end of the struct).
273 const union UdpAddress *udp_addr;
276 * Who's message(s) are we defragmenting here?
277 * Only initialized once we succeeded and
278 * @e have_sender is set.
280 struct GNUNET_PeerIdentity sender;
283 * Length of @e udp_addr.
288 * Network type the address belongs to.
290 enum GNUNET_ATS_Network_Type network_type;
293 * Has the @e sender field been initialized yet?
300 * Context to send fragmented messages
302 struct UDP_FragmentationContext
305 * Next in linked list
307 struct UDP_FragmentationContext *next;
310 * Previous in linked list
312 struct UDP_FragmentationContext *prev;
317 struct Plugin *plugin;
320 * Handle for fragmentation.
322 struct GNUNET_FRAGMENT_Context *frag;
325 * The session this fragmentation context belongs to
327 struct GNUNET_ATS_Session *session;
330 * Function to call upon completion of the transmission.
332 GNUNET_TRANSPORT_TransmitContinuation cont;
335 * Closure for @e cont.
342 struct GNUNET_TIME_Absolute start_time;
345 * Transmission time for the next fragment. Incremented by
346 * the @e flow_delay_from_other_peer for each fragment when
347 * we setup the fragments.
349 struct GNUNET_TIME_Absolute next_frag_time;
352 * Desired delay for transmissions we received from other peer.
353 * Adjusted to be per fragment (UDP_MTU), even though on the
354 * wire it was for "full messages".
356 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
361 struct GNUNET_TIME_Absolute timeout;
364 * Payload size of original unfragmented message
369 * Bytes used to send all fragments on wire including UDP overhead
377 * Function called when a message is removed from the
378 * transmission queue.
381 * @param udpw message wrapper finished
382 * @param result #GNUNET_OK on success (message was sent)
383 * #GNUNET_SYSERR if the target disconnected
384 * or we had a timeout or other trouble sending
387 (*QueueContinuation) (void *cls,
388 struct UDP_MessageWrapper *udpw,
393 * Information we track for each message in the queue.
395 struct UDP_MessageWrapper
398 * Session this message belongs to
400 struct GNUNET_ATS_Session *session;
403 * DLL of messages, previous element
405 struct UDP_MessageWrapper *prev;
408 * DLL of messages, next element
410 struct UDP_MessageWrapper *next;
413 * Message with @e msg_size bytes including UDP-specific overhead.
418 * Function to call once the message wrapper is being removed
419 * from the queue (with success or failure).
421 QueueContinuation qc;
429 * External continuation to call upon completion of the
430 * transmission, NULL if this queue entry is not for a
431 * message from the application.
433 GNUNET_TRANSPORT_TransmitContinuation cont;
436 * Closure for @e cont.
441 * Fragmentation context.
442 * frag_ctx == NULL if transport <= MTU
443 * frag_ctx != NULL if transport > MTU
445 struct UDP_FragmentationContext *frag_ctx;
448 * Message enqueue time.
450 struct GNUNET_TIME_Absolute start_time;
453 * Desired transmission time for this message, based on the
454 * flow limiting information we got from the other peer.
456 struct GNUNET_TIME_Absolute transmission_time;
461 struct GNUNET_TIME_Absolute timeout;
464 * Size of UDP message to send, including UDP-specific overhead.
469 * Payload size of original message.
476 GNUNET_NETWORK_STRUCT_BEGIN
479 * UDP ACK Message-Packet header.
481 struct UDP_ACK_Message
486 struct GNUNET_MessageHeader header;
489 * Desired delay for flow control, in us (in NBO).
490 * A value of UINT32_MAX indicates that the other
491 * peer wants us to disconnect.
493 uint32_t delay GNUNET_PACKED;
496 * What is the identity of the sender
498 struct GNUNET_PeerIdentity sender;
502 GNUNET_NETWORK_STRUCT_END
505 /* ************************* Monitoring *********** */
509 * If a session monitor is attached, notify it about the new
512 * @param plugin our plugin
513 * @param session session that changed state
514 * @param state new state of the session
517 notify_session_monitor (struct Plugin *plugin,
518 struct GNUNET_ATS_Session *session,
519 enum GNUNET_TRANSPORT_SessionState state)
521 struct GNUNET_TRANSPORT_SessionInfo info;
523 if (NULL == plugin->sic)
525 if (GNUNET_YES == session->in_destroy)
526 return; /* already destroyed, just RC>0 left-over actions */
531 info.is_inbound = GNUNET_SYSERR; /* hard to say */
532 info.num_msg_pending = session->msgs_in_queue;
533 info.num_bytes_pending = session->bytes_in_queue;
534 /* info.receive_delay remains zero as this is not supported by UDP
535 (cannot selectively not receive from 'some' peer while continuing
536 to receive from others) */
537 info.session_timeout = session->timeout;
538 info.address = session->address;
539 plugin->sic (plugin->sic_cls,
546 * Return information about the given session to the monitor callback.
548 * @param cls the `struct Plugin` with the monitor callback (`sic`)
549 * @param peer peer we send information about
550 * @param value our `struct GNUNET_ATS_Session` to send information about
551 * @return #GNUNET_OK (continue to iterate)
554 send_session_info_iter (void *cls,
555 const struct GNUNET_PeerIdentity *peer,
558 struct Plugin *plugin = cls;
559 struct GNUNET_ATS_Session *session = value;
561 notify_session_monitor (plugin,
563 GNUNET_TRANSPORT_SS_INIT);
564 notify_session_monitor (plugin,
566 GNUNET_TRANSPORT_SS_UP);
572 * Begin monitoring sessions of a plugin. There can only
573 * be one active monitor per plugin (i.e. if there are
574 * multiple monitors, the transport service needs to
575 * multiplex the generated events over all of them).
577 * @param cls closure of the plugin
578 * @param sic callback to invoke, NULL to disable monitor;
579 * plugin will being by iterating over all active
580 * sessions immediately and then enter monitor mode
581 * @param sic_cls closure for @a sic
584 udp_plugin_setup_monitor (void *cls,
585 GNUNET_TRANSPORT_SessionInfoCallback sic,
588 struct Plugin *plugin = cls;
591 plugin->sic_cls = sic_cls;
594 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
595 &send_session_info_iter,
597 /* signal end of first iteration */
605 /* ****************** Little Helpers ****************** */
609 * Function to free last resources associated with a session.
611 * @param s session to free
614 free_session (struct GNUNET_ATS_Session *s)
616 if (NULL != s->address)
618 GNUNET_HELLO_address_free (s->address);
621 if (NULL != s->frag_ctx)
623 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
626 GNUNET_free (s->frag_ctx);
634 * Function that is called to get the keepalive factor.
635 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
636 * calculate the interval between keepalive packets.
638 * @param cls closure with the `struct Plugin`
639 * @return keepalive factor
642 udp_query_keepalive_factor (void *cls)
649 * Function obtain the network type for a session
651 * @param cls closure (`struct Plugin *`)
652 * @param session the session
653 * @return the network type
655 static enum GNUNET_ATS_Network_Type
656 udp_plugin_get_network (void *cls,
657 struct GNUNET_ATS_Session *session)
659 return session->scope;
664 * Function obtain the network type for an address.
666 * @param cls closure (`struct Plugin *`)
667 * @param address the address
668 * @return the network type
670 static enum GNUNET_ATS_Network_Type
671 udp_plugin_get_network_for_address (void *cls,
672 const struct GNUNET_HELLO_Address *address)
674 struct Plugin *plugin = cls;
676 struct sockaddr_in a4;
677 struct sockaddr_in6 a6;
678 const struct IPv4UdpAddress *u4;
679 const struct IPv6UdpAddress *u6;
683 addrlen = address->address_length;
684 if (addrlen == sizeof(struct IPv6UdpAddress))
686 GNUNET_assert (NULL != address->address); /* make static analysis happy */
687 u6 = address->address;
688 memset (&a6, 0, sizeof(a6));
689 #if HAVE_SOCKADDR_IN_SIN_LEN
690 a6.sin6_len = sizeof (a6);
692 a6.sin6_family = AF_INET6;
693 a6.sin6_port = u6->u6_port;
694 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
698 else if (addrlen == sizeof(struct IPv4UdpAddress))
700 GNUNET_assert (NULL != address->address); /* make static analysis happy */
701 u4 = address->address;
702 memset (&a4, 0, sizeof(a4));
703 #if HAVE_SOCKADDR_IN_SIN_LEN
704 a4.sin_len = sizeof (a4);
706 a4.sin_family = AF_INET;
707 a4.sin_port = u4->u4_port;
708 a4.sin_addr.s_addr = u4->ipv4_addr;
715 return GNUNET_ATS_NET_UNSPECIFIED;
717 return plugin->env->get_address_type (plugin->env->cls,
723 /* ******************* Event loop ******************** */
726 * We have been notified that our readset has something to read. We don't
727 * know which socket needs to be read, so we have to check each one
728 * Then reschedule this function to be called again once more is available.
730 * @param cls the plugin handle
731 * @param tc the scheduling context (for rescheduling this function again)
734 udp_plugin_select_v4 (void *cls,
735 const struct GNUNET_SCHEDULER_TaskContext *tc);
739 * We have been notified that our readset has something to read. We don't
740 * know which socket needs to be read, so we have to check each one
741 * Then reschedule this function to be called again once more is available.
743 * @param cls the plugin handle
744 * @param tc the scheduling context (for rescheduling this function again)
747 udp_plugin_select_v6 (void *cls,
748 const struct GNUNET_SCHEDULER_TaskContext *tc);
752 * (re)schedule IPv4-select tasks for this plugin.
754 * @param plugin plugin to reschedule
757 schedule_select_v4 (struct Plugin *plugin)
759 struct GNUNET_TIME_Relative min_delay;
760 struct GNUNET_TIME_Relative delay;
761 struct UDP_MessageWrapper *udpw;
762 struct UDP_MessageWrapper *min_udpw;
764 if ( (GNUNET_YES == plugin->enable_ipv4) &&
765 (NULL != plugin->sockv4) )
767 /* Find a message ready to send:
768 * Flow delay from other peer is expired or not set (0) */
769 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
771 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
773 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
774 if (delay.rel_value_us < min_delay.rel_value_us)
780 if (NULL != plugin->select_task_v4)
781 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
782 if (NULL != min_udpw)
784 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
786 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
787 "Calculated flow delay for UDPv4 at %s for %s\n",
788 GNUNET_STRINGS_relative_time_to_string (min_delay,
790 GNUNET_i2s (&min_udpw->session->target));
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
795 "Calculated flow delay for UDPv4 at %s for %s\n",
796 GNUNET_STRINGS_relative_time_to_string (min_delay,
798 GNUNET_i2s (&min_udpw->session->target));
801 plugin->select_task_v4
802 = GNUNET_SCHEDULER_add_read_net (min_delay,
804 &udp_plugin_select_v4,
811 * (re)schedule IPv6-select tasks for this plugin.
813 * @param plugin plugin to reschedule
816 schedule_select_v6 (struct Plugin *plugin)
818 struct GNUNET_TIME_Relative min_delay;
819 struct GNUNET_TIME_Relative delay;
820 struct UDP_MessageWrapper *udpw;
821 struct UDP_MessageWrapper *min_udpw;
823 if ( (GNUNET_YES == plugin->enable_ipv6) &&
824 (NULL != plugin->sockv6) )
826 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
828 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
830 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
831 if (delay.rel_value_us < min_delay.rel_value_us)
837 if (NULL != plugin->select_task_v6)
838 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
839 if (NULL != min_udpw)
841 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 "Calculated flow delay for UDPv6 at %s for %s\n",
845 GNUNET_STRINGS_relative_time_to_string (min_delay,
847 GNUNET_i2s (&min_udpw->session->target));
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Calculated flow delay for UDPv6 at %s for %s\n",
853 GNUNET_STRINGS_relative_time_to_string (min_delay,
855 GNUNET_i2s (&min_udpw->session->target));
858 plugin->select_task_v6
859 = GNUNET_SCHEDULER_add_read_net (min_delay,
861 &udp_plugin_select_v6,
867 /* ******************* Address to string and back ***************** */
871 * Function called for a quick conversion of the binary address to
872 * a numeric address. Note that the caller must not free the
873 * address and that the next call to this function is allowed
874 * to override the address again.
877 * @param addr binary address (a `union UdpAddress`)
878 * @param addrlen length of the @a addr
879 * @return string representing the same address
882 udp_address_to_string (void *cls,
886 static char rbuf[INET6_ADDRSTRLEN + 10];
887 char buf[INET6_ADDRSTRLEN];
891 const struct IPv4UdpAddress *t4;
892 const struct IPv6UdpAddress *t6;
903 if (addrlen == sizeof(struct IPv6UdpAddress))
907 options = ntohl (t6->options);
908 port = ntohs (t6->u6_port);
912 else if (addrlen == sizeof(struct IPv4UdpAddress))
916 options = ntohl (t4->options);
917 port = ntohs (t4->u4_port);
918 a4.s_addr = t4->ipv4_addr;
930 GNUNET_snprintf (rbuf,
944 * Function called to convert a string address to a binary address.
946 * @param cls closure (`struct Plugin *`)
947 * @param addr string address
948 * @param addrlen length of the address
949 * @param buf location to store the buffer
950 * @param added location to store the number of bytes in the buffer.
951 * If the function returns #GNUNET_SYSERR, its contents are undefined.
952 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
955 udp_string_to_address (void *cls,
961 struct sockaddr_storage socket_address;
967 /* Format tcp.options.address:port */
972 if ((NULL == addr) || (0 == addrlen))
975 return GNUNET_SYSERR;
977 if ('\0' != addr[addrlen - 1])
980 return GNUNET_SYSERR;
982 if (strlen (addr) != addrlen - 1)
985 return GNUNET_SYSERR;
987 plugin = GNUNET_strdup (addr);
988 optionstr = strchr (plugin, '.');
989 if (NULL == optionstr)
992 GNUNET_free (plugin);
993 return GNUNET_SYSERR;
997 options = atol (optionstr);
998 address = strchr (optionstr, '.');
1002 GNUNET_free (plugin);
1003 return GNUNET_SYSERR;
1009 GNUNET_STRINGS_to_address_ip (address,
1014 GNUNET_free (plugin);
1015 return GNUNET_SYSERR;
1017 GNUNET_free(plugin);
1019 switch (socket_address.ss_family)
1023 struct IPv4UdpAddress *u4;
1024 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1026 u4 = GNUNET_new (struct IPv4UdpAddress);
1027 u4->options = htonl (options);
1028 u4->ipv4_addr = in4->sin_addr.s_addr;
1029 u4->u4_port = in4->sin_port;
1031 *added = sizeof (struct IPv4UdpAddress);
1036 struct IPv6UdpAddress *u6;
1037 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1039 u6 = GNUNET_new (struct IPv6UdpAddress);
1040 u6->options = htonl (options);
1041 u6->ipv6_addr = in6->sin6_addr;
1042 u6->u6_port = in6->sin6_port;
1044 *added = sizeof (struct IPv6UdpAddress);
1049 return GNUNET_SYSERR;
1055 * Append our port and forward the result.
1057 * @param cls a `struct PrettyPrinterContext *`
1058 * @param hostname result from DNS resolver
1061 append_port (void *cls,
1062 const char *hostname)
1064 struct PrettyPrinterContext *ppc = cls;
1065 struct Plugin *plugin = ppc->plugin;
1068 if (NULL == hostname)
1070 /* Final call, done */
1071 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1072 plugin->ppc_dll_tail,
1074 ppc->resolver_handle = NULL;
1075 ppc->asc (ppc->asc_cls,
1081 if (GNUNET_YES == ppc->ipv6)
1082 GNUNET_asprintf (&ret,
1089 GNUNET_asprintf (&ret,
1095 ppc->asc (ppc->asc_cls,
1103 * Convert the transports address to a nice, human-readable format.
1105 * @param cls closure with the `struct Plugin *`
1106 * @param type name of the transport that generated the address
1107 * @param addr one of the addresses of the host, NULL for the last address
1108 * the specific address format depends on the transport;
1109 * a `union UdpAddress`
1110 * @param addrlen length of the address
1111 * @param numeric should (IP) addresses be displayed in numeric form?
1112 * @param timeout after how long should we give up?
1113 * @param asc function to call on each string
1114 * @param asc_cls closure for @a asc
1117 udp_plugin_address_pretty_printer (void *cls,
1122 struct GNUNET_TIME_Relative timeout,
1123 GNUNET_TRANSPORT_AddressStringCallback asc,
1126 struct Plugin *plugin = cls;
1127 struct PrettyPrinterContext *ppc;
1128 const struct sockaddr *sb;
1130 struct sockaddr_in a4;
1131 struct sockaddr_in6 a6;
1132 const struct IPv4UdpAddress *u4;
1133 const struct IPv6UdpAddress *u6;
1137 if (addrlen == sizeof(struct IPv6UdpAddress))
1143 a6.sin6_family = AF_INET6;
1144 #if HAVE_SOCKADDR_IN_SIN_LEN
1145 a6.sin6_len = sizeof (a6);
1147 a6.sin6_port = u6->u6_port;
1148 a6.sin6_addr = u6->ipv6_addr;
1149 port = ntohs (u6->u6_port);
1150 options = ntohl (u6->options);
1151 sb = (const struct sockaddr *) &a6;
1154 else if (addrlen == sizeof (struct IPv4UdpAddress))
1160 a4.sin_family = AF_INET;
1161 #if HAVE_SOCKADDR_IN_SIN_LEN
1162 a4.sin_len = sizeof (a4);
1164 a4.sin_port = u4->u4_port;
1165 a4.sin_addr.s_addr = u4->ipv4_addr;
1166 port = ntohs (u4->u4_port);
1167 options = ntohl (u4->options);
1168 sb = (const struct sockaddr *) &a4;
1173 /* invalid address */
1174 GNUNET_break_op (0);
1183 ppc = GNUNET_new (struct PrettyPrinterContext);
1184 ppc->plugin = plugin;
1186 ppc->asc_cls = asc_cls;
1188 ppc->options = options;
1189 if (addrlen == sizeof (struct IPv6UdpAddress))
1190 ppc->ipv6 = GNUNET_YES;
1192 ppc->ipv6 = GNUNET_NO;
1193 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1194 plugin->ppc_dll_tail,
1196 ppc->resolver_handle
1197 = GNUNET_RESOLVER_hostname_get (sb,
1207 * Check if the given port is plausible (must be either our listen
1208 * port or our advertised port). If it is neither, we return
1211 * @param plugin global variables
1212 * @param in_port port number to check
1213 * @return #GNUNET_OK if port is either our open or advertised port
1216 check_port (const struct Plugin *plugin,
1219 if ( (plugin->port == in_port) ||
1220 (plugin->aport == in_port) )
1222 return GNUNET_SYSERR;
1227 * Function that will be called to check if a binary address for this
1228 * plugin is well-formed and corresponds to an address for THIS peer
1229 * (as per our configuration). Naturally, if absolutely necessary,
1230 * plugins can be a bit conservative in their answer, but in general
1231 * plugins should make sure that the address does not redirect
1232 * traffic to a 3rd party that might try to man-in-the-middle our
1235 * @param cls closure, should be our handle to the Plugin
1236 * @param addr pointer to a `union UdpAddress`
1237 * @param addrlen length of @a addr
1238 * @return #GNUNET_OK if this is a plausible address for this peer
1239 * and transport, #GNUNET_SYSERR if not
1242 udp_plugin_check_address (void *cls,
1246 struct Plugin *plugin = cls;
1247 const struct IPv4UdpAddress *v4;
1248 const struct IPv6UdpAddress *v6;
1250 if (sizeof(struct IPv4UdpAddress) == addrlen)
1252 v4 = (const struct IPv4UdpAddress *) addr;
1253 if (GNUNET_OK != check_port (plugin,
1254 ntohs (v4->u4_port)))
1255 return GNUNET_SYSERR;
1257 GNUNET_NAT_test_address (plugin->nat,
1259 sizeof (struct in_addr)))
1260 return GNUNET_SYSERR;
1262 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1264 v6 = (const struct IPv6UdpAddress *) addr;
1265 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1267 GNUNET_break_op (0);
1268 return GNUNET_SYSERR;
1270 if (GNUNET_OK != check_port (plugin,
1271 ntohs (v6->u6_port)))
1272 return GNUNET_SYSERR;
1274 GNUNET_NAT_test_address (plugin->nat,
1276 sizeof (struct in6_addr)))
1277 return GNUNET_SYSERR;
1281 GNUNET_break_op (0);
1282 return GNUNET_SYSERR;
1289 * Our external IP address/port mapping has changed.
1291 * @param cls closure, the `struct Plugin`
1292 * @param add_remove #GNUNET_YES to mean the new public IP address,
1293 * #GNUNET_NO to mean the previous (now invalid) one
1294 * @param addr either the previous or the new public IP address
1295 * @param addrlen actual length of the @a addr
1298 udp_nat_port_map_callback (void *cls,
1300 const struct sockaddr *addr,
1303 struct Plugin *plugin = cls;
1304 struct GNUNET_HELLO_Address *address;
1305 struct IPv4UdpAddress u4;
1306 struct IPv6UdpAddress u6;
1310 LOG (GNUNET_ERROR_TYPE_DEBUG,
1311 (GNUNET_YES == add_remove)
1312 ? "NAT notification to add address `%s'\n"
1313 : "NAT notification to remove address `%s'\n",
1316 /* convert 'address' to our internal format */
1317 switch (addr->sa_family)
1321 const struct sockaddr_in *i4;
1323 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1324 i4 = (const struct sockaddr_in *) addr;
1325 if (0 == ntohs (i4->sin_port))
1333 u4.options = htonl (plugin->myoptions);
1334 u4.ipv4_addr = i4->sin_addr.s_addr;
1335 u4.u4_port = i4->sin_port;
1337 args = sizeof (struct IPv4UdpAddress);
1342 const struct sockaddr_in6 *i6;
1344 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1345 i6 = (const struct sockaddr_in6 *) addr;
1346 if (0 == ntohs (i6->sin6_port))
1354 u6.options = htonl (plugin->myoptions);
1355 u6.ipv6_addr = i6->sin6_addr;
1356 u6.u6_port = i6->sin6_port;
1358 args = sizeof (struct IPv6UdpAddress);
1365 /* modify our published address list */
1366 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1370 GNUNET_HELLO_ADDRESS_INFO_NONE);
1371 plugin->env->notify_address (plugin->env->cls,
1374 GNUNET_HELLO_address_free (address);
1378 /* ********************* Finding sessions ******************* */
1382 * Closure for #session_cmp_it().
1384 struct GNUNET_ATS_SessionCompareContext
1387 * Set to session matching the address.
1389 struct GNUNET_ATS_Session *res;
1392 * Address we are looking for.
1394 const struct GNUNET_HELLO_Address *address;
1399 * Find a session with a matching address.
1401 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1402 * @param key peer identity (unused)
1403 * @param value the `struct GNUNET_ATS_Session *`
1404 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1407 session_cmp_it (void *cls,
1408 const struct GNUNET_PeerIdentity *key,
1411 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1412 struct GNUNET_ATS_Session *s = value;
1414 if (0 == GNUNET_HELLO_address_cmp (s->address,
1417 GNUNET_assert (GNUNET_NO == s->in_destroy);
1426 * Locate an existing session the transport service is using to
1427 * send data to another peer. Performs some basic sanity checks
1428 * on the address and then tries to locate a matching session.
1430 * @param cls the plugin
1431 * @param address the address we should locate the session by
1432 * @return the session if it exists, or NULL if it is not found
1434 static struct GNUNET_ATS_Session *
1435 udp_plugin_lookup_session (void *cls,
1436 const struct GNUNET_HELLO_Address *address)
1438 struct Plugin *plugin = cls;
1439 const struct IPv6UdpAddress *udp_a6;
1440 const struct IPv4UdpAddress *udp_a4;
1441 struct GNUNET_ATS_SessionCompareContext cctx;
1443 if (NULL == address->address)
1448 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1450 if (NULL == plugin->sockv4)
1452 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1453 if (0 == udp_a4->u4_port)
1459 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1461 if (NULL == plugin->sockv6)
1463 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1464 if (0 == udp_a6->u6_port)
1476 /* check if session already exists */
1477 cctx.address = address;
1479 LOG (GNUNET_ERROR_TYPE_DEBUG,
1480 "Looking for existing session for peer `%s' with address `%s'\n",
1481 GNUNET_i2s (&address->peer),
1482 udp_address_to_string (plugin,
1484 address->address_length));
1485 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1489 if (NULL == cctx.res)
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Found existing session %p\n",
1498 /* ********************** Timeout ****************** */
1502 * Increment session timeout due to activity.
1504 * @param s session to reschedule timeout activity for
1507 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1509 if (GNUNET_YES == s->in_destroy)
1511 GNUNET_assert (NULL != s->timeout_task);
1512 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1518 * Function that will be called whenever the transport service wants to
1519 * notify the plugin that a session is still active and in use and
1520 * therefore the session timeout for this session has to be updated
1522 * @param cls closure with the `struct Plugin`
1523 * @param peer which peer was the session for
1524 * @param session which session is being updated
1527 udp_plugin_update_session_timeout (void *cls,
1528 const struct GNUNET_PeerIdentity *peer,
1529 struct GNUNET_ATS_Session *session)
1531 struct Plugin *plugin = cls;
1534 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1541 /* Reschedule session timeout */
1542 reschedule_session_timeout (session);
1546 /* ************************* Sending ************************ */
1550 * Remove the given message from the transmission queue and
1551 * update all applicable statistics.
1553 * @param plugin the UDP plugin
1554 * @param udpw message wrapper to dequeue
1557 dequeue (struct Plugin *plugin,
1558 struct UDP_MessageWrapper *udpw)
1560 struct GNUNET_ATS_Session *session = udpw->session;
1562 if (plugin->bytes_in_buffer < udpw->msg_size)
1568 GNUNET_STATISTICS_update (plugin->env->stats,
1569 "# UDP, total bytes in send buffers",
1570 - (long long) udpw->msg_size,
1572 plugin->bytes_in_buffer -= udpw->msg_size;
1574 GNUNET_STATISTICS_update (plugin->env->stats,
1575 "# UDP, total messages in send buffers",
1578 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1580 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1581 plugin->ipv4_queue_tail,
1584 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1586 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1587 plugin->ipv6_queue_tail,
1595 GNUNET_assert (session->msgs_in_queue > 0);
1596 session->msgs_in_queue--;
1597 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1598 session->bytes_in_queue -= udpw->msg_size;
1603 * Enqueue a message for transmission and update statistics.
1605 * @param plugin the UDP plugin
1606 * @param udpw message wrapper to queue
1609 enqueue (struct Plugin *plugin,
1610 struct UDP_MessageWrapper *udpw)
1612 struct GNUNET_ATS_Session *session = udpw->session;
1614 if (GNUNET_YES == session->in_destroy)
1619 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1625 GNUNET_STATISTICS_update (plugin->env->stats,
1626 "# UDP, total bytes in send buffers",
1629 plugin->bytes_in_buffer += udpw->msg_size;
1631 GNUNET_STATISTICS_update (plugin->env->stats,
1632 "# UDP, total messages in send buffers",
1635 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1637 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1638 plugin->ipv4_queue_tail,
1641 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1643 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1644 plugin->ipv6_queue_tail,
1650 udpw->cont (udpw->cont_cls,
1658 session->msgs_in_queue++;
1659 session->bytes_in_queue += udpw->msg_size;
1664 * We have completed our (attempt) to transmit a message that had to
1665 * be fragmented -- either because we got an ACK saying that all
1666 * fragments were received, or because of timeout / disconnect. Clean
1669 * @param frag_ctx fragmentation context to clean up
1670 * @param result #GNUNET_OK if we succeeded (got ACK),
1671 * #GNUNET_SYSERR if the transmission failed
1674 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1677 struct Plugin *plugin = frag_ctx->plugin;
1678 struct GNUNET_ATS_Session *s = frag_ctx->session;
1679 struct UDP_MessageWrapper *udpw;
1680 struct UDP_MessageWrapper *tmp;
1682 struct GNUNET_TIME_Relative delay;
1684 LOG (GNUNET_ERROR_TYPE_DEBUG,
1685 "%p: Fragmented message removed with result %s\n",
1687 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1688 /* Call continuation for fragmented message */
1689 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1690 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1692 overhead = frag_ctx->on_wire_size;
1693 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1694 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1696 LOG (GNUNET_ERROR_TYPE_WARNING,
1697 "Fragmented message acknowledged after %s (expected at %s)\n",
1698 GNUNET_STRINGS_relative_time_to_string (delay,
1700 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1705 "Fragmented message acknowledged after %s (expected at %s)\n",
1706 GNUNET_STRINGS_relative_time_to_string (delay,
1708 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1711 if (NULL != frag_ctx->cont)
1712 frag_ctx->cont (frag_ctx->cont_cls,
1715 s->frag_ctx->payload_size,
1716 frag_ctx->on_wire_size);
1717 GNUNET_STATISTICS_update (plugin->env->stats,
1718 "# UDP, fragmented messages active",
1722 if (GNUNET_OK == result)
1724 GNUNET_STATISTICS_update (plugin->env->stats,
1725 "# UDP, fragmented msgs, messages, sent, success",
1728 GNUNET_STATISTICS_update (plugin->env->stats,
1729 "# UDP, fragmented msgs, bytes payload, sent, success",
1730 s->frag_ctx->payload_size,
1732 GNUNET_STATISTICS_update (plugin->env->stats,
1733 "# UDP, fragmented msgs, bytes overhead, sent, success",
1736 GNUNET_STATISTICS_update (plugin->env->stats,
1737 "# UDP, total, bytes overhead, sent",
1740 GNUNET_STATISTICS_update (plugin->env->stats,
1741 "# UDP, total, bytes payload, sent",
1742 s->frag_ctx->payload_size,
1747 GNUNET_STATISTICS_update (plugin->env->stats,
1748 "# UDP, fragmented msgs, messages, sent, failure",
1751 GNUNET_STATISTICS_update (plugin->env->stats,
1752 "# UDP, fragmented msgs, bytes payload, sent, failure",
1753 s->frag_ctx->payload_size,
1755 GNUNET_STATISTICS_update (plugin->env->stats,
1756 "# UDP, fragmented msgs, bytes payload, sent, failure",
1759 GNUNET_STATISTICS_update (plugin->env->stats,
1760 "# UDP, fragmented msgs, bytes payload, sent, failure",
1765 /* Remove remaining fragments from queue, no need to transmit those
1767 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1769 udpw = plugin->ipv6_queue_head;
1770 while (NULL != udpw)
1773 if ( (udpw->frag_ctx != NULL) &&
1774 (udpw->frag_ctx == frag_ctx) )
1783 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1785 udpw = plugin->ipv4_queue_head;
1786 while (NULL != udpw)
1789 if ( (NULL != udpw->frag_ctx) &&
1790 (udpw->frag_ctx == frag_ctx) )
1799 notify_session_monitor (s->plugin,
1801 GNUNET_TRANSPORT_SS_UPDATE);
1802 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1803 &s->last_expected_msg_delay,
1804 &s->last_expected_ack_delay);
1806 GNUNET_free (frag_ctx);
1811 * We are finished with a fragment in the message queue.
1812 * Notify the continuation and update statistics.
1814 * @param cls the `struct Plugin *`
1815 * @param udpw the queue entry
1816 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1819 qc_fragment_sent (void *cls,
1820 struct UDP_MessageWrapper *udpw,
1823 struct Plugin *plugin = cls;
1825 GNUNET_assert (NULL != udpw->frag_ctx);
1826 if (GNUNET_OK == result)
1828 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1829 GNUNET_STATISTICS_update (plugin->env->stats,
1830 "# UDP, fragmented msgs, fragments, sent, success",
1833 GNUNET_STATISTICS_update (plugin->env->stats,
1834 "# UDP, fragmented msgs, fragments bytes, sent, success",
1840 fragmented_message_done (udpw->frag_ctx,
1842 GNUNET_STATISTICS_update (plugin->env->stats,
1843 "# UDP, fragmented msgs, fragments, sent, failure",
1846 GNUNET_STATISTICS_update (plugin->env->stats,
1847 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1855 * Function that is called with messages created by the fragmentation
1856 * module. In the case of the `proc` callback of the
1857 * #GNUNET_FRAGMENT_context_create() function, this function must
1858 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1860 * @param cls closure, the `struct UDP_FragmentationContext`
1861 * @param msg the message that was created
1864 enqueue_fragment (void *cls,
1865 const struct GNUNET_MessageHeader *msg)
1867 struct UDP_FragmentationContext *frag_ctx = cls;
1868 struct Plugin *plugin = frag_ctx->plugin;
1869 struct UDP_MessageWrapper *udpw;
1870 struct GNUNET_ATS_Session *session = frag_ctx->session;
1871 size_t msg_len = ntohs (msg->size);
1873 LOG (GNUNET_ERROR_TYPE_DEBUG,
1874 "Enqueuing fragment with %u bytes\n",
1876 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1877 udpw->session = session;
1878 udpw->msg_buf = (char *) &udpw[1];
1879 udpw->msg_size = msg_len;
1880 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1881 udpw->timeout = frag_ctx->timeout;
1882 udpw->start_time = frag_ctx->start_time;
1883 udpw->transmission_time = frag_ctx->next_frag_time;
1884 frag_ctx->next_frag_time
1885 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1886 frag_ctx->flow_delay_from_other_peer);
1887 udpw->frag_ctx = frag_ctx;
1888 udpw->qc = &qc_fragment_sent;
1889 udpw->qc_cls = plugin;
1890 memcpy (udpw->msg_buf,
1899 * We are finished with a message from the message queue.
1900 * Notify the continuation and update statistics.
1902 * @param cls the `struct Plugin *`
1903 * @param udpw the queue entry
1904 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1907 qc_message_sent (void *cls,
1908 struct UDP_MessageWrapper *udpw,
1911 struct Plugin *plugin = cls;
1913 struct GNUNET_TIME_Relative delay;
1915 if (udpw->msg_size >= udpw->payload_size)
1916 overhead = udpw->msg_size - udpw->payload_size;
1918 overhead = udpw->msg_size;
1920 if (NULL != udpw->cont)
1922 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1923 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1925 LOG (GNUNET_ERROR_TYPE_WARNING,
1926 "Message sent via UDP with delay of %s\n",
1927 GNUNET_STRINGS_relative_time_to_string (delay,
1932 LOG (GNUNET_ERROR_TYPE_DEBUG,
1933 "Message sent via UDP with delay of %s\n",
1934 GNUNET_STRINGS_relative_time_to_string (delay,
1937 udpw->cont (udpw->cont_cls,
1938 &udpw->session->target,
1943 if (GNUNET_OK == result)
1945 GNUNET_STATISTICS_update (plugin->env->stats,
1946 "# UDP, unfragmented msgs, messages, sent, success",
1949 GNUNET_STATISTICS_update (plugin->env->stats,
1950 "# UDP, unfragmented msgs, bytes payload, sent, success",
1953 GNUNET_STATISTICS_update (plugin->env->stats,
1954 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1957 GNUNET_STATISTICS_update (plugin->env->stats,
1958 "# UDP, total, bytes overhead, sent",
1961 GNUNET_STATISTICS_update (plugin->env->stats,
1962 "# UDP, total, bytes payload, sent",
1968 GNUNET_STATISTICS_update (plugin->env->stats,
1969 "# UDP, unfragmented msgs, messages, sent, failure",
1972 GNUNET_STATISTICS_update (plugin->env->stats,
1973 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1976 GNUNET_STATISTICS_update (plugin->env->stats,
1977 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1985 * Function that can be used by the transport service to transmit a
1986 * message using the plugin. Note that in the case of a peer
1987 * disconnecting, the continuation MUST be called prior to the
1988 * disconnect notification itself. This function will be called with
1989 * this peer's HELLO message to initiate a fresh connection to another
1992 * @param cls closure
1993 * @param s which session must be used
1994 * @param msgbuf the message to transmit
1995 * @param msgbuf_size number of bytes in @a msgbuf
1996 * @param priority how important is the message (most plugins will
1997 * ignore message priority and just FIFO)
1998 * @param to how long to wait at most for the transmission (does not
1999 * require plugins to discard the message after the timeout,
2000 * just advisory for the desired delay; most plugins will ignore
2002 * @param cont continuation to call once the message has
2003 * been transmitted (or if the transport is ready
2004 * for the next transmission call; or if the
2005 * peer disconnected...); can be NULL
2006 * @param cont_cls closure for @a cont
2007 * @return number of bytes used (on the physical network, with overheads);
2008 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2009 * and does NOT mean that the message was not transmitted (DV)
2012 udp_plugin_send (void *cls,
2013 struct GNUNET_ATS_Session *s,
2016 unsigned int priority,
2017 struct GNUNET_TIME_Relative to,
2018 GNUNET_TRANSPORT_TransmitContinuation cont,
2021 struct Plugin *plugin = cls;
2022 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2023 struct UDP_FragmentationContext *frag_ctx;
2024 struct UDP_MessageWrapper *udpw;
2025 struct UDPMessage *udp;
2026 char mbuf[udpmlen] GNUNET_ALIGN;
2027 struct GNUNET_TIME_Relative latency;
2029 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2030 (NULL == plugin->sockv6) )
2031 return GNUNET_SYSERR;
2032 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2033 (NULL == plugin->sockv4) )
2034 return GNUNET_SYSERR;
2035 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2038 return GNUNET_SYSERR;
2041 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2046 return GNUNET_SYSERR;
2048 LOG (GNUNET_ERROR_TYPE_DEBUG,
2049 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2051 GNUNET_i2s (&s->target),
2052 udp_address_to_string (plugin,
2053 s->address->address,
2054 s->address->address_length));
2056 udp = (struct UDPMessage *) mbuf;
2057 udp->header.size = htons (udpmlen);
2058 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2059 udp->reserved = htonl (0);
2060 udp->sender = *plugin->env->my_identity;
2062 /* We do not update the session time out here! Otherwise this
2063 * session will not timeout since we send keep alive before session
2066 * For UDP we update session timeout only on receive, this will
2067 * cover keep alives, since remote peer will reply with keep alive
2070 if (udpmlen <= UDP_MTU)
2072 /* unfragmented message */
2073 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2075 udpw->msg_buf = (char *) &udpw[1];
2076 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2077 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2078 udpw->start_time = GNUNET_TIME_absolute_get ();
2079 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2080 udpw->transmission_time = s->last_transmit_time;
2081 s->last_transmit_time
2082 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2083 s->flow_delay_from_other_peer);
2085 udpw->cont_cls = cont_cls;
2086 udpw->frag_ctx = NULL;
2087 udpw->qc = &qc_message_sent;
2088 udpw->qc_cls = plugin;
2089 memcpy (udpw->msg_buf,
2091 sizeof (struct UDPMessage));
2092 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2097 GNUNET_STATISTICS_update (plugin->env->stats,
2098 "# UDP, unfragmented messages queued total",
2101 GNUNET_STATISTICS_update (plugin->env->stats,
2102 "# UDP, unfragmented bytes payload queued total",
2108 /* fragmented message */
2109 if (NULL != s->frag_ctx)
2110 return GNUNET_SYSERR;
2114 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2115 frag_ctx->plugin = plugin;
2116 frag_ctx->session = s;
2117 frag_ctx->cont = cont;
2118 frag_ctx->cont_cls = cont_cls;
2119 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2120 frag_ctx->next_frag_time = s->last_transmit_time;
2121 frag_ctx->flow_delay_from_other_peer
2122 = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
2125 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2126 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2127 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2128 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2131 s->last_expected_msg_delay,
2132 s->last_expected_ack_delay,
2136 s->frag_ctx = frag_ctx;
2137 s->last_transmit_time = frag_ctx->next_frag_time;
2138 latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
2139 if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2140 LOG (GNUNET_ERROR_TYPE_WARNING,
2141 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2142 GNUNET_STRINGS_relative_time_to_string (latency,
2144 GNUNET_i2s (&s->target),
2145 (unsigned int) s->msgs_in_queue);
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2149 GNUNET_STRINGS_relative_time_to_string (latency,
2151 GNUNET_i2s (&s->target),
2152 (unsigned int) s->msgs_in_queue);
2154 GNUNET_STATISTICS_update (plugin->env->stats,
2155 "# UDP, fragmented messages active",
2158 GNUNET_STATISTICS_update (plugin->env->stats,
2159 "# UDP, fragmented messages, total",
2162 GNUNET_STATISTICS_update (plugin->env->stats,
2163 "# UDP, fragmented bytes (payload)",
2164 frag_ctx->payload_size,
2167 notify_session_monitor (s->plugin,
2169 GNUNET_TRANSPORT_SS_UPDATE);
2170 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2171 schedule_select_v4 (plugin);
2173 schedule_select_v6 (plugin);
2178 /* ********************** Receiving ********************** */
2182 * Closure for #find_receive_context().
2184 struct FindReceiveContext
2187 * Where to store the result.
2189 struct DefragContext *rc;
2192 * Session associated with this context.
2194 struct GNUNET_ATS_Session *session;
2199 const union UdpAddress *udp_addr;
2202 * Number of bytes in @e udp_addr.
2204 size_t udp_addr_len;
2210 * Scan the heap for a receive context with the given address.
2212 * @param cls the `struct FindReceiveContext`
2213 * @param node internal node of the heap
2214 * @param element value stored at the node (a `struct ReceiveContext`)
2215 * @param cost cost associated with the node
2216 * @return #GNUNET_YES if we should continue to iterate,
2217 * #GNUNET_NO if not.
2220 find_receive_context (void *cls,
2221 struct GNUNET_CONTAINER_HeapNode *node,
2223 GNUNET_CONTAINER_HeapCostType cost)
2225 struct FindReceiveContext *frc = cls;
2226 struct DefragContext *e = element;
2228 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2229 (0 == memcmp (frc->udp_addr,
2231 frc->udp_addr_len)) )
2241 * Functions with this signature are called whenever we need to close
2242 * a session due to a disconnect or failure to establish a connection.
2244 * @param cls closure with the `struct Plugin`
2245 * @param s session to close down
2246 * @return #GNUNET_OK on success
2249 udp_disconnect_session (void *cls,
2250 struct GNUNET_ATS_Session *s)
2252 struct Plugin *plugin = cls;
2253 struct UDP_MessageWrapper *udpw;
2254 struct UDP_MessageWrapper *next;
2255 struct FindReceiveContext frc;
2257 GNUNET_assert (GNUNET_YES != s->in_destroy);
2258 LOG (GNUNET_ERROR_TYPE_DEBUG,
2259 "Session %p to peer `%s' at address %s ended\n",
2261 GNUNET_i2s (&s->target),
2262 udp_address_to_string (plugin,
2263 s->address->address,
2264 s->address->address_length));
2265 if (NULL != s->timeout_task)
2267 GNUNET_SCHEDULER_cancel (s->timeout_task);
2268 s->timeout_task = NULL;
2270 if (NULL != s->frag_ctx)
2272 /* Remove fragmented message due to disconnect */
2273 fragmented_message_done (s->frag_ctx,
2276 GNUNET_assert (GNUNET_YES ==
2277 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2281 frc.udp_addr = s->address->address;
2282 frc.udp_addr_len = s->address->address_length;
2283 /* Lookup existing receive context for this address */
2284 if (NULL != plugin->defrag_ctxs)
2286 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2287 &find_receive_context,
2291 struct DefragContext *d_ctx = frc.rc;
2293 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2294 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2295 GNUNET_free (d_ctx);
2298 s->in_destroy = GNUNET_YES;
2299 next = plugin->ipv4_queue_head;
2300 while (NULL != (udpw = next))
2303 if (udpw->session == s)
2307 udpw->qc (udpw->qc_cls,
2313 next = plugin->ipv6_queue_head;
2314 while (NULL != (udpw = next))
2317 if (udpw->session == s)
2321 udpw->qc (udpw->qc_cls,
2327 if ( (NULL != s->frag_ctx) &&
2328 (NULL != s->frag_ctx->cont) )
2330 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2331 later, as it might be in use right now */
2332 LOG (GNUNET_ERROR_TYPE_DEBUG,
2333 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2334 GNUNET_i2s (&s->target));
2335 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2338 s->frag_ctx->payload_size,
2339 s->frag_ctx->on_wire_size);
2341 notify_session_monitor (s->plugin,
2343 GNUNET_TRANSPORT_SS_DONE);
2344 plugin->env->session_end (plugin->env->cls,
2347 GNUNET_STATISTICS_set (plugin->env->stats,
2348 "# UDP sessions active",
2349 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2358 * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
2360 * @param plugin the UDP plugin
2361 * @param msg the (presumed) UDP ACK message
2362 * @param udp_addr sender address
2363 * @param udp_addr_len number of bytes in @a udp_addr
2366 read_process_ack (struct Plugin *plugin,
2367 const struct GNUNET_MessageHeader *msg,
2368 const union UdpAddress *udp_addr,
2369 socklen_t udp_addr_len)
2371 const struct GNUNET_MessageHeader *ack;
2372 const struct UDP_ACK_Message *udp_ack;
2373 struct GNUNET_HELLO_Address *address;
2374 struct GNUNET_ATS_Session *s;
2375 struct GNUNET_TIME_Relative flow_delay;
2377 /* check message format */
2378 if (ntohs (msg->size)
2379 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2381 GNUNET_break_op (0);
2384 udp_ack = (const struct UDP_ACK_Message *) msg;
2385 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2386 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2392 /* Locate session */
2393 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2397 GNUNET_HELLO_ADDRESS_INFO_NONE);
2398 s = udp_plugin_lookup_session (plugin,
2402 LOG (GNUNET_ERROR_TYPE_WARNING,
2403 "UDP session of address %s for ACK not found\n",
2404 udp_address_to_string (plugin,
2406 address->address_length));
2407 GNUNET_HELLO_address_free (address);
2410 if (NULL == s->frag_ctx)
2412 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2413 "Fragmentation context of address %s for ACK (%s) not found\n",
2414 udp_address_to_string (plugin,
2416 address->address_length),
2417 GNUNET_FRAGMENT_print_ack (ack));
2418 GNUNET_HELLO_address_free (address);
2421 GNUNET_HELLO_address_free (address);
2423 /* evaluate flow delay: how long should we wait between messages? */
2424 if (UINT32_MAX == ntohl (udp_ack->delay))
2426 /* Other peer asked for us to terminate the session */
2427 LOG (GNUNET_ERROR_TYPE_INFO,
2428 "Asked to disconnect UDP session of %s\n",
2429 GNUNET_i2s (&udp_ack->sender));
2430 udp_disconnect_session (plugin,
2434 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2435 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2436 LOG (GNUNET_ERROR_TYPE_WARNING,
2437 "We received a sending delay of %s for %s\n",
2438 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2440 GNUNET_i2s (&udp_ack->sender));
2442 LOG (GNUNET_ERROR_TYPE_DEBUG,
2443 "We received a sending delay of %s for %s\n",
2444 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2446 GNUNET_i2s (&udp_ack->sender));
2447 /* Flow delay is for the reassembled packet, however, our delay
2448 is per packet, so we need to adjust: */
2449 s->flow_delay_from_other_peer = flow_delay;
2453 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2456 LOG (GNUNET_ERROR_TYPE_DEBUG,
2457 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2458 (unsigned int) ntohs (msg->size),
2459 GNUNET_i2s (&udp_ack->sender),
2460 udp_address_to_string (plugin,
2463 /* Expect more ACKs to arrive */
2467 /* Remove fragmented message after successful sending */
2468 LOG (GNUNET_ERROR_TYPE_DEBUG,
2469 "Message from %s at %s full ACK'ed\n",
2470 GNUNET_i2s (&udp_ack->sender),
2471 udp_address_to_string (plugin,
2474 fragmented_message_done (s->frag_ctx,
2480 * Message tokenizer has broken up an incomming message. Pass it on
2483 * @param cls the `struct Plugin *`
2484 * @param client the `struct GNUNET_ATS_Session *`
2485 * @param hdr the actual message
2486 * @return #GNUNET_OK (always)
2489 process_inbound_tokenized_messages (void *cls,
2491 const struct GNUNET_MessageHeader *hdr)
2493 struct Plugin *plugin = cls;
2494 struct GNUNET_ATS_Session *session = client;
2496 if (GNUNET_YES == session->in_destroy)
2498 reschedule_session_timeout (session);
2499 session->flow_delay_for_other_peer
2500 = plugin->env->receive (plugin->env->cls,
2509 * Destroy a session, plugin is being unloaded.
2511 * @param cls the `struct Plugin`
2512 * @param key hash of public key of target peer
2513 * @param value a `struct PeerSession *` to clean up
2514 * @return #GNUNET_OK (continue to iterate)
2517 disconnect_and_free_it (void *cls,
2518 const struct GNUNET_PeerIdentity *key,
2521 struct Plugin *plugin = cls;
2523 udp_disconnect_session (plugin,
2530 * Disconnect from a remote node. Clean up session if we have one for
2533 * @param cls closure for this call (should be handle to Plugin)
2534 * @param target the peeridentity of the peer to disconnect
2535 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2538 udp_disconnect (void *cls,
2539 const struct GNUNET_PeerIdentity *target)
2541 struct Plugin *plugin = cls;
2543 LOG (GNUNET_ERROR_TYPE_DEBUG,
2544 "Disconnecting from peer `%s'\n",
2545 GNUNET_i2s (target));
2546 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2548 &disconnect_and_free_it,
2554 * Session was idle, so disconnect it.
2556 * @param cls the `struct GNUNET_ATS_Session` to time out
2557 * @param tc scheduler context
2560 session_timeout (void *cls,
2561 const struct GNUNET_SCHEDULER_TaskContext *tc)
2563 struct GNUNET_ATS_Session *s = cls;
2564 struct Plugin *plugin = s->plugin;
2565 struct GNUNET_TIME_Relative left;
2567 s->timeout_task = NULL;
2568 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2569 if (left.rel_value_us > 0)
2571 /* not actually our turn yet, but let's at least update
2572 the monitor, it may think we're about to die ... */
2573 notify_session_monitor (s->plugin,
2575 GNUNET_TRANSPORT_SS_UPDATE);
2576 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2581 LOG (GNUNET_ERROR_TYPE_DEBUG,
2582 "Session %p was idle for %s, disconnecting\n",
2584 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2586 /* call session destroy function */
2587 udp_disconnect_session (plugin,
2593 * Allocate a new session for the given endpoint address.
2594 * Note that this function does not inform the service
2595 * of the new session, this is the responsibility of the
2596 * caller (if needed).
2598 * @param cls the `struct Plugin`
2599 * @param address address of the other peer to use
2600 * @param network_type network type the address belongs to
2601 * @return NULL on error, otherwise session handle
2603 static struct GNUNET_ATS_Session *
2604 udp_plugin_create_session (void *cls,
2605 const struct GNUNET_HELLO_Address *address,
2606 enum GNUNET_ATS_Network_Type network_type)
2608 struct Plugin *plugin = cls;
2609 struct GNUNET_ATS_Session *s;
2611 s = GNUNET_new (struct GNUNET_ATS_Session);
2613 s->address = GNUNET_HELLO_address_copy (address);
2614 s->target = address->peer;
2615 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2616 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2618 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2619 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2620 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2621 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2622 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2625 s->scope = network_type;
2627 LOG (GNUNET_ERROR_TYPE_DEBUG,
2628 "Creating new session %p for peer `%s' address `%s'\n",
2630 GNUNET_i2s (&address->peer),
2631 udp_address_to_string (plugin,
2633 address->address_length));
2634 GNUNET_assert (GNUNET_OK ==
2635 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2638 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2639 GNUNET_STATISTICS_set (plugin->env->stats,
2640 "# UDP sessions active",
2641 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2643 notify_session_monitor (plugin,
2645 GNUNET_TRANSPORT_SS_INIT);
2651 * Creates a new outbound session the transport service will use to
2652 * send data to the peer.
2654 * @param cls the `struct Plugin *`
2655 * @param address the address
2656 * @return the session or NULL of max connections exceeded
2658 static struct GNUNET_ATS_Session *
2659 udp_plugin_get_session (void *cls,
2660 const struct GNUNET_HELLO_Address *address)
2662 struct Plugin *plugin = cls;
2663 struct GNUNET_ATS_Session *s;
2664 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2665 const struct IPv4UdpAddress *udp_v4;
2666 const struct IPv6UdpAddress *udp_v6;
2668 if (NULL == address)
2673 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2674 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2676 GNUNET_break_op (0);
2679 if (NULL != (s = udp_plugin_lookup_session (cls,
2683 /* need to create new session */
2684 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2686 struct sockaddr_in v4;
2688 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2689 memset (&v4, '\0', sizeof (v4));
2690 v4.sin_family = AF_INET;
2691 #if HAVE_SOCKADDR_IN_SIN_LEN
2692 v4.sin_len = sizeof (struct sockaddr_in);
2694 v4.sin_port = udp_v4->u4_port;
2695 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2696 network_type = plugin->env->get_address_type (plugin->env->cls,
2697 (const struct sockaddr *) &v4,
2700 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2702 struct sockaddr_in6 v6;
2704 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2705 memset (&v6, '\0', sizeof (v6));
2706 v6.sin6_family = AF_INET6;
2707 #if HAVE_SOCKADDR_IN_SIN_LEN
2708 v6.sin6_len = sizeof (struct sockaddr_in6);
2710 v6.sin6_port = udp_v6->u6_port;
2711 v6.sin6_addr = udp_v6->ipv6_addr;
2712 network_type = plugin->env->get_address_type (plugin->env->cls,
2713 (const struct sockaddr *) &v6,
2716 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2717 return udp_plugin_create_session (cls,
2724 * We've received a UDP Message. Process it (pass contents to main service).
2726 * @param plugin plugin context
2727 * @param msg the message
2728 * @param udp_addr sender address
2729 * @param udp_addr_len number of bytes in @a udp_addr
2730 * @param network_type network type the address belongs to
2733 process_udp_message (struct Plugin *plugin,
2734 const struct UDPMessage *msg,
2735 const union UdpAddress *udp_addr,
2736 size_t udp_addr_len,
2737 enum GNUNET_ATS_Network_Type network_type)
2739 struct GNUNET_ATS_Session *s;
2740 struct GNUNET_HELLO_Address *address;
2742 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2743 if (0 != ntohl (msg->reserved))
2748 if (ntohs (msg->header.size)
2749 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2755 address = GNUNET_HELLO_address_allocate (&msg->sender,
2759 GNUNET_HELLO_ADDRESS_INFO_NONE);
2761 (s = udp_plugin_lookup_session (plugin,
2764 s = udp_plugin_create_session (plugin,
2767 plugin->env->session_start (plugin->env->cls,
2771 notify_session_monitor (plugin,
2773 GNUNET_TRANSPORT_SS_UP);
2775 GNUNET_free (address);
2778 GNUNET_SERVER_mst_receive (plugin->mst,
2780 (const char *) &msg[1],
2781 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2785 if ( (0 == s->rc) &&
2786 (GNUNET_YES == s->in_destroy) )
2792 * Process a defragmented message.
2794 * @param cls the `struct DefragContext *`
2795 * @param msg the message
2798 fragment_msg_proc (void *cls,
2799 const struct GNUNET_MessageHeader *msg)
2801 struct DefragContext *dc = cls;
2802 const struct UDPMessage *um;
2804 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2806 GNUNET_break_op (0);
2809 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2811 GNUNET_break_op (0);
2814 um = (const struct UDPMessage *) msg;
2815 dc->sender = um->sender;
2816 dc->have_sender = GNUNET_YES;
2817 process_udp_message (dc->plugin,
2826 * We finished sending an acknowledgement. Update
2829 * @param cls the `struct Plugin`
2830 * @param udpw message queue entry of the ACK
2831 * @param result #GNUNET_OK if the transmission worked,
2832 * #GNUNET_SYSERR if we failed to send the ACK
2835 ack_message_sent (void *cls,
2836 struct UDP_MessageWrapper *udpw,
2839 struct Plugin *plugin = cls;
2841 if (GNUNET_OK == result)
2843 GNUNET_STATISTICS_update (plugin->env->stats,
2844 "# UDP, ACK messages sent",
2850 GNUNET_STATISTICS_update (plugin->env->stats,
2851 "# UDP, ACK transmissions failed",
2859 * Transmit an acknowledgement.
2861 * @param cls the `struct DefragContext *`
2862 * @param id message ID (unused)
2863 * @param msg ack to transmit
2866 ack_proc (void *cls,
2868 const struct GNUNET_MessageHeader *msg)
2870 struct DefragContext *rc = cls;
2871 struct Plugin *plugin = rc->plugin;
2872 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2873 struct UDP_ACK_Message *udp_ack;
2875 struct UDP_MessageWrapper *udpw;
2876 struct GNUNET_ATS_Session *s;
2877 struct GNUNET_HELLO_Address *address;
2879 if (GNUNET_NO == rc->have_sender)
2881 /* tried to defragment but never succeeded, hence will not ACK */
2882 /* This can happen if we just lost msgs */
2883 GNUNET_STATISTICS_update (plugin->env->stats,
2884 "# UDP, fragments discarded without ACK",
2889 address = GNUNET_HELLO_address_allocate (&rc->sender,
2893 GNUNET_HELLO_ADDRESS_INFO_NONE);
2894 s = udp_plugin_lookup_session (plugin,
2896 GNUNET_HELLO_address_free (address);
2899 LOG (GNUNET_ERROR_TYPE_ERROR,
2900 "Trying to transmit ACK to peer `%s' but no session found!\n",
2901 udp_address_to_string (plugin,
2904 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2905 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2907 GNUNET_STATISTICS_update (plugin->env->stats,
2908 "# UDP, ACK transmissions failed",
2913 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2914 s->flow_delay_for_other_peer.rel_value_us)
2916 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2917 delay = s->flow_delay_for_other_peer.rel_value_us;
2919 delay = UINT32_MAX - 1; /* largest value we can communicate */
2920 LOG (GNUNET_ERROR_TYPE_DEBUG,
2921 "Sending ACK to `%s' including delay of %s\n",
2922 udp_address_to_string (plugin,
2925 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2927 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2928 udpw->msg_size = msize;
2929 udpw->payload_size = 0;
2931 udpw->start_time = GNUNET_TIME_absolute_get ();
2932 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2933 udpw->msg_buf = (char *) &udpw[1];
2934 udpw->qc = &ack_message_sent;
2935 udpw->qc_cls = plugin;
2936 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2937 udp_ack->header.size = htons ((uint16_t) msize);
2938 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2939 udp_ack->delay = htonl (delay);
2940 udp_ack->sender = *plugin->env->my_identity;
2941 memcpy (&udp_ack[1],
2946 notify_session_monitor (plugin,
2948 GNUNET_TRANSPORT_SS_UPDATE);
2949 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2950 schedule_select_v4 (plugin);
2952 schedule_select_v6 (plugin);
2957 * We received a fragment, process it.
2959 * @param plugin our plugin
2960 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2961 * @param udp_addr sender address
2962 * @param udp_addr_len number of bytes in @a udp_addr
2963 * @param network_type network type the address belongs to
2966 read_process_fragment (struct Plugin *plugin,
2967 const struct GNUNET_MessageHeader *msg,
2968 const union UdpAddress *udp_addr,
2969 size_t udp_addr_len,
2970 enum GNUNET_ATS_Network_Type network_type)
2972 struct DefragContext *d_ctx;
2973 struct GNUNET_TIME_Absolute now;
2974 struct FindReceiveContext frc;
2977 frc.udp_addr = udp_addr;
2978 frc.udp_addr_len = udp_addr_len;
2980 /* Lookup existing receive context for this address */
2981 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2982 &find_receive_context,
2984 now = GNUNET_TIME_absolute_get ();
2989 /* Create a new defragmentation context */
2990 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2994 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2995 d_ctx->udp_addr_len = udp_addr_len;
2996 d_ctx->network_type = network_type;
2997 d_ctx->plugin = plugin;
2998 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
3000 UDP_MAX_MESSAGES_IN_DEFRAG,
3004 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
3006 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3007 LOG (GNUNET_ERROR_TYPE_DEBUG,
3008 "Created new defragmentation context for %u-byte fragment from `%s'\n",
3009 (unsigned int) ntohs (msg->size),
3010 udp_address_to_string (plugin,
3016 LOG (GNUNET_ERROR_TYPE_DEBUG,
3017 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
3018 (unsigned int) ntohs (msg->size),
3019 udp_address_to_string (plugin,
3025 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
3028 /* keep this 'rc' from expiring */
3029 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
3031 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3033 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
3034 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
3036 /* remove 'rc' that was inactive the longest */
3037 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
3038 GNUNET_assert (NULL != d_ctx);
3039 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3040 GNUNET_free (d_ctx);
3041 GNUNET_STATISTICS_update (plugin->env->stats,
3042 "# UDP, Defragmentations aborted",
3050 * Read and process a message from the given socket.
3052 * @param plugin the overall plugin
3053 * @param rsock socket to read from
3056 udp_select_read (struct Plugin *plugin,
3057 struct GNUNET_NETWORK_Handle *rsock)
3060 struct sockaddr_storage addr;
3061 char buf[65536] GNUNET_ALIGN;
3063 const struct GNUNET_MessageHeader *msg;
3064 struct IPv4UdpAddress v4;
3065 struct IPv6UdpAddress v6;
3066 const struct sockaddr *sa;
3067 const struct sockaddr_in *sa4;
3068 const struct sockaddr_in6 *sa6;
3069 const union UdpAddress *int_addr;
3070 size_t int_addr_len;
3071 enum GNUNET_ATS_Network_Type network_type;
3073 fromlen = sizeof (addr);
3077 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3080 (struct sockaddr *) &addr,
3082 sa = (const struct sockaddr *) &addr;
3084 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3085 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3086 * on this socket has failed.
3088 * WSAECONNRESET - The virtual circuit was reset by the remote side
3089 * executing a hard or abortive close. The application should close
3090 * the socket; it is no longer usable. On a UDP-datagram socket this
3091 * error indicates a previous send operation resulted in an ICMP Port
3092 * Unreachable message.
3094 if ( (-1 == size) &&
3095 (ECONNRESET == errno) )
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3101 "UDP failed to receive data: %s\n",
3103 /* Connection failure or something. Not a protocol violation. */
3107 /* Check if this is a STUN packet */
3108 if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
3111 return; /* was STUN, do not process further */
3113 if (size < sizeof(struct GNUNET_MessageHeader))
3115 LOG (GNUNET_ERROR_TYPE_WARNING,
3116 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3117 (unsigned int ) size,
3120 /* _MAY_ be a connection failure (got partial message) */
3121 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3122 GNUNET_break_op (0);
3126 msg = (const struct GNUNET_MessageHeader *) buf;
3127 LOG (GNUNET_ERROR_TYPE_DEBUG,
3128 "UDP received %u-byte message from `%s' type %u\n",
3129 (unsigned int) size,
3133 if (size != ntohs (msg->size))
3135 LOG (GNUNET_ERROR_TYPE_WARNING,
3136 "UDP malformed message header from %s\n",
3137 (unsigned int) size,
3140 GNUNET_break_op (0);
3143 GNUNET_STATISTICS_update (plugin->env->stats,
3144 "# UDP, total bytes received",
3147 network_type = plugin->env->get_address_type (plugin->env->cls,
3150 switch (sa->sa_family)
3153 sa4 = (const struct sockaddr_in *) &addr;
3155 v4.ipv4_addr = sa4->sin_addr.s_addr;
3156 v4.u4_port = sa4->sin_port;
3157 int_addr = (union UdpAddress *) &v4;
3158 int_addr_len = sizeof (v4);
3161 sa6 = (const struct sockaddr_in6 *) &addr;
3163 v6.ipv6_addr = sa6->sin6_addr;
3164 v6.u6_port = sa6->sin6_port;
3165 int_addr = (union UdpAddress *) &v6;
3166 int_addr_len = sizeof (v6);
3173 switch (ntohs (msg->type))
3175 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3176 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3177 udp_broadcast_receive (plugin,
3184 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3185 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3190 process_udp_message (plugin,
3191 (const struct UDPMessage *) msg,
3196 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3197 read_process_ack (plugin,
3202 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3203 read_process_fragment (plugin,
3217 * Removes messages from the transmission queue that have
3218 * timed out, and then selects a message that should be
3221 * @param plugin the UDP plugin
3222 * @param sock which socket should we process the queue for (v4 or v6)
3223 * @return message selected for transmission, or NULL for none
3225 static struct UDP_MessageWrapper *
3226 remove_timeout_messages_and_select (struct Plugin *plugin,
3227 struct GNUNET_NETWORK_Handle *sock)
3229 struct UDP_MessageWrapper *udpw;
3230 struct GNUNET_TIME_Relative remaining;
3231 struct GNUNET_ATS_Session *session;
3234 removed = GNUNET_NO;
3235 udpw = (sock == plugin->sockv4)
3236 ? plugin->ipv4_queue_head
3237 : plugin->ipv6_queue_head;
3238 while (NULL != udpw)
3240 session = udpw->session;
3241 /* Find messages with timeout */
3242 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3243 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3245 /* Message timed out */
3246 removed = GNUNET_YES;
3249 udpw->qc (udpw->qc_cls,
3254 if (sock == plugin->sockv4)
3256 udpw = plugin->ipv4_queue_head;
3258 else if (sock == plugin->sockv6)
3260 udpw = plugin->ipv6_queue_head;
3264 GNUNET_break (0); /* should never happen */
3267 GNUNET_STATISTICS_update (plugin->env->stats,
3268 "# messages discarded due to timeout",
3274 /* Message did not time out, check transmission time */
3275 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3276 if (0 == remaining.rel_value_us)
3278 /* this message is not delayed */
3279 LOG (GNUNET_ERROR_TYPE_DEBUG,
3280 "Message for peer `%s' (%u bytes) is not delayed \n",
3281 GNUNET_i2s (&udpw->session->target),
3282 udpw->payload_size);
3283 break; /* Found message to send, break */
3287 /* Message is delayed, try next */
3288 LOG (GNUNET_ERROR_TYPE_DEBUG,
3289 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3290 GNUNET_i2s (&udpw->session->target),
3292 GNUNET_STRINGS_relative_time_to_string (remaining,
3298 if (GNUNET_YES == removed)
3299 notify_session_monitor (session->plugin,
3301 GNUNET_TRANSPORT_SS_UPDATE);
3307 * We failed to transmit a message via UDP. Generate
3308 * a descriptive error message.
3310 * @param plugin our plugin
3311 * @param sa target address we were trying to reach
3312 * @param slen number of bytes in @a sa
3313 * @param error the errno value returned from the sendto() call
3316 analyze_send_error (struct Plugin *plugin,
3317 const struct sockaddr *sa,
3321 enum GNUNET_ATS_Network_Type type;
3323 type = plugin->env->get_address_type (plugin->env->cls,
3326 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3327 (GNUNET_ATS_NET_WAN == type) ) &&
3328 ( (ENETUNREACH == errno) ||
3329 (ENETDOWN == errno) ) )
3331 if (slen == sizeof (struct sockaddr_in))
3333 /* IPv4: "Network unreachable" or "Network down"
3335 * This indicates we do not have connectivity
3337 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3338 _("UDP could not transmit message to `%s': "
3339 "Network seems down, please check your network configuration\n"),
3343 if (slen == sizeof (struct sockaddr_in6))
3345 /* IPv6: "Network unreachable" or "Network down"
3347 * This indicates that this system is IPv6 enabled, but does not
3348 * have a valid global IPv6 address assigned or we do not have
3351 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3352 _("UDP could not transmit IPv6 message! "
3353 "Please check your network configuration and disable IPv6 if your "
3354 "connection does not have a global IPv6 address\n"));
3359 LOG (GNUNET_ERROR_TYPE_WARNING,
3360 "UDP could not transmit message to `%s': `%s'\n",
3369 * It is time to try to transmit a UDP message. Select one
3372 * @param plugin the plugin
3373 * @param sock which socket (v4/v6) to send on
3376 udp_select_send (struct Plugin *plugin,
3377 struct GNUNET_NETWORK_Handle *sock)
3381 const struct sockaddr *a;
3382 const struct IPv4UdpAddress *u4;
3383 struct sockaddr_in a4;
3384 const struct IPv6UdpAddress *u6;
3385 struct sockaddr_in6 a6;
3386 struct UDP_MessageWrapper *udpw;
3388 /* Find message(s) to send */
3389 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3392 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3394 u4 = udpw->session->address->address;
3398 a4.sin_family = AF_INET;
3399 #if HAVE_SOCKADDR_IN_SIN_LEN
3400 a4.sin_len = sizeof (a4);
3402 a4.sin_port = u4->u4_port;
3403 a4.sin_addr.s_addr = u4->ipv4_addr;
3404 a = (const struct sockaddr *) &a4;
3407 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3409 u6 = udpw->session->address->address;
3413 a6.sin6_family = AF_INET6;
3414 #if HAVE_SOCKADDR_IN_SIN_LEN
3415 a6.sin6_len = sizeof (a6);
3417 a6.sin6_port = u6->u6_port;
3418 a6.sin6_addr = u6->ipv6_addr;
3419 a = (const struct sockaddr *) &a6;
3427 udpw->qc (udpw->qc_cls,
3430 notify_session_monitor (plugin,
3432 GNUNET_TRANSPORT_SS_UPDATE);
3436 sent = GNUNET_NETWORK_socket_sendto (sock,
3441 udpw->session->last_transmit_time
3442 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3443 udpw->session->last_transmit_time);
3446 if (GNUNET_SYSERR == sent)
3449 analyze_send_error (plugin,
3453 udpw->qc (udpw->qc_cls,
3456 GNUNET_STATISTICS_update (plugin->env->stats,
3457 "# UDP, total, bytes, sent, failure",
3460 GNUNET_STATISTICS_update (plugin->env->stats,
3461 "# UDP, total, messages, sent, failure",
3468 LOG (GNUNET_ERROR_TYPE_DEBUG,
3469 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3470 (unsigned int) (udpw->msg_size),
3471 GNUNET_i2s (&udpw->session->target),
3475 (sent < 0) ? STRERROR (errno) : "ok");
3476 GNUNET_STATISTICS_update (plugin->env->stats,
3477 "# UDP, total, bytes, sent, success",
3480 GNUNET_STATISTICS_update (plugin->env->stats,
3481 "# UDP, total, messages, sent, success",
3484 if (NULL != udpw->frag_ctx)
3485 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3486 udpw->qc (udpw->qc_cls,
3490 notify_session_monitor (plugin,
3492 GNUNET_TRANSPORT_SS_UPDATE);
3498 /* ***************** Event loop (part 2) *************** */
3502 * We have been notified that our readset has something to read. We don't
3503 * know which socket needs to be read, so we have to check each one
3504 * Then reschedule this function to be called again once more is available.
3506 * @param cls the plugin handle
3507 * @param tc the scheduling context
3510 udp_plugin_select_v4 (void *cls,
3511 const struct GNUNET_SCHEDULER_TaskContext *tc)
3513 struct Plugin *plugin = cls;
3515 plugin->select_task_v4 = NULL;
3516 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3518 if (NULL == plugin->sockv4)
3520 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3521 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3523 udp_select_read (plugin,
3525 udp_select_send (plugin,
3527 schedule_select_v4 (plugin);
3532 * We have been notified that our readset has something to read. We don't
3533 * know which socket needs to be read, so we have to check each one
3534 * Then reschedule this function to be called again once more is available.
3536 * @param cls the plugin handle
3537 * @param tc the scheduling context
3540 udp_plugin_select_v6 (void *cls,
3541 const struct GNUNET_SCHEDULER_TaskContext *tc)
3543 struct Plugin *plugin = cls;
3545 plugin->select_task_v6 = NULL;
3546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3548 if (NULL == plugin->sockv6)
3550 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3551 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3553 udp_select_read (plugin,
3556 udp_select_send (plugin,
3558 schedule_select_v6 (plugin);
3562 /* ******************* Initialization *************** */
3566 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3568 * @param plugin the plugin to initialize
3569 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3570 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3571 * @return number of sockets that were successfully bound
3574 setup_sockets (struct Plugin *plugin,
3575 const struct sockaddr_in6 *bind_v6,
3576 const struct sockaddr_in *bind_v4)
3579 int sockets_created = 0;
3580 struct sockaddr_in6 server_addrv6;
3581 struct sockaddr_in server_addrv4;
3582 const struct sockaddr *server_addr;
3583 const struct sockaddr *addrs[2];
3584 socklen_t addrlens[2];
3588 /* Create IPv6 socket */
3590 if (GNUNET_YES == plugin->enable_ipv6)
3592 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3595 if (NULL == plugin->sockv6)
3597 LOG (GNUNET_ERROR_TYPE_INFO,
3598 _("Disabling IPv6 since it is not supported on this system!\n"));
3599 plugin->enable_ipv6 = GNUNET_NO;
3603 memset (&server_addrv6,
3605 sizeof(struct sockaddr_in6));
3606 #if HAVE_SOCKADDR_IN_SIN_LEN
3607 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3609 server_addrv6.sin6_family = AF_INET6;
3610 if (NULL != bind_v6)
3611 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3613 server_addrv6.sin6_addr = in6addr_any;
3615 if (0 == plugin->port) /* autodetect */
3616 server_addrv6.sin6_port
3617 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3621 server_addrv6.sin6_port = htons (plugin->port);
3622 addrlen = sizeof (struct sockaddr_in6);
3623 server_addr = (const struct sockaddr *) &server_addrv6;
3628 LOG(GNUNET_ERROR_TYPE_DEBUG,
3629 "Binding to IPv6 `%s'\n",
3630 GNUNET_a2s (server_addr,
3634 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3639 if (0 != plugin->port)
3641 tries = 10; /* fail immediately */
3642 break; /* bind failed on specific port */
3645 server_addrv6.sin6_port
3646 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3653 GNUNET_NETWORK_socket_close (plugin->sockv6);
3654 plugin->enable_ipv6 = GNUNET_NO;
3655 plugin->sockv6 = NULL;
3659 plugin->port = ntohs (server_addrv6.sin6_port);
3661 if (NULL != plugin->sockv6)
3663 LOG (GNUNET_ERROR_TYPE_DEBUG,
3664 "IPv6 UDP socket created listinging at %s\n",
3665 GNUNET_a2s (server_addr,
3667 addrs[sockets_created] = server_addr;
3668 addrlens[sockets_created] = addrlen;
3673 LOG (GNUNET_ERROR_TYPE_WARNING,
3674 _("Failed to bind UDP socket to %s: %s\n"),
3675 GNUNET_a2s (server_addr,
3682 /* Create IPv4 socket */
3684 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3687 if (NULL == plugin->sockv4)
3689 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3691 LOG (GNUNET_ERROR_TYPE_INFO,
3692 _("Disabling IPv4 since it is not supported on this system!\n"));
3693 plugin->enable_ipv4 = GNUNET_NO;
3697 memset (&server_addrv4,
3699 sizeof(struct sockaddr_in));
3700 #if HAVE_SOCKADDR_IN_SIN_LEN
3701 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3703 server_addrv4.sin_family = AF_INET;
3704 if (NULL != bind_v4)
3705 server_addrv4.sin_addr = bind_v4->sin_addr;
3707 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3709 if (0 == plugin->port)
3711 server_addrv4.sin_port
3712 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3716 server_addrv4.sin_port = htons (plugin->port);
3718 addrlen = sizeof (struct sockaddr_in);
3719 server_addr = (const struct sockaddr *) &server_addrv4;
3724 LOG (GNUNET_ERROR_TYPE_DEBUG,
3725 "Binding to IPv4 `%s'\n",
3726 GNUNET_a2s (server_addr,
3731 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3736 if (0 != plugin->port)
3738 tries = 10; /* fail */
3739 break; /* bind failed on specific port */
3743 server_addrv4.sin_port
3744 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3751 GNUNET_NETWORK_socket_close (plugin->sockv4);
3752 plugin->enable_ipv4 = GNUNET_NO;
3753 plugin->sockv4 = NULL;
3757 plugin->port = ntohs (server_addrv4.sin_port);
3760 if (NULL != plugin->sockv4)
3762 LOG (GNUNET_ERROR_TYPE_DEBUG,
3763 "IPv4 socket created on port %s\n",
3764 GNUNET_a2s (server_addr,
3766 addrs[sockets_created] = server_addr;
3767 addrlens[sockets_created] = addrlen;
3772 LOG (GNUNET_ERROR_TYPE_ERROR,
3773 _("Failed to bind UDP socket to %s: %s\n"),
3774 GNUNET_a2s (server_addr,
3780 if (0 == sockets_created)
3782 LOG (GNUNET_ERROR_TYPE_WARNING,
3783 _("Failed to open UDP sockets\n"));
3784 return 0; /* No sockets created, return */
3786 schedule_select_v4 (plugin);
3787 schedule_select_v6 (plugin);
3788 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3794 &udp_nat_port_map_callback,
3798 return sockets_created;
3803 * The exported method. Makes the core api available via a global and
3804 * returns the udp transport API.
3806 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3807 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3810 libgnunet_plugin_transport_udp_init (void *cls)
3812 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3813 struct GNUNET_TRANSPORT_PluginFunctions *api;
3815 unsigned long long port;
3816 unsigned long long aport;
3817 unsigned long long udp_max_bps;
3818 unsigned long long enable_v6;
3819 unsigned long long enable_broadcasting;
3820 unsigned long long enable_broadcasting_recv;
3821 char *bind4_address;
3822 char *bind6_address;
3823 struct GNUNET_TIME_Relative interval;
3824 struct sockaddr_in server_addrv4;
3825 struct sockaddr_in6 server_addrv6;
3830 if (NULL == env->receive)
3832 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3833 initialze the plugin or the API */
3834 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3836 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3837 api->address_to_string = &udp_address_to_string;
3838 api->string_to_address = &udp_string_to_address;
3842 /* Get port number: port == 0 : autodetect a port,
3843 * > 0 : use this port, not given : 2086 default */
3845 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3852 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3855 _("must be in [0,65535]"));
3859 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3866 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3869 _("must be in [0,65535]"));
3874 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3877 enable_v6 = GNUNET_NO;
3879 enable_v6 = GNUNET_YES;
3881 have_bind4 = GNUNET_NO;
3882 memset (&server_addrv4,
3884 sizeof (server_addrv4));
3886 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3891 LOG (GNUNET_ERROR_TYPE_DEBUG,
3892 "Binding UDP plugin to specific address: `%s'\n",
3894 if (1 != inet_pton (AF_INET,
3896 &server_addrv4.sin_addr))
3898 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3901 _("must be valid IPv4 address"));
3902 GNUNET_free (bind4_address);
3905 have_bind4 = GNUNET_YES;
3907 GNUNET_free_non_null (bind4_address);
3908 have_bind6 = GNUNET_NO;
3909 memset (&server_addrv6,
3911 sizeof (server_addrv6));
3913 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3918 LOG (GNUNET_ERROR_TYPE_DEBUG,
3919 "Binding udp plugin to specific address: `%s'\n",
3921 if (1 != inet_pton (AF_INET6,
3923 &server_addrv6.sin6_addr))
3925 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3928 _("must be valid IPv6 address"));
3929 GNUNET_free (bind6_address);
3932 have_bind6 = GNUNET_YES;
3934 GNUNET_free_non_null (bind6_address);
3936 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3939 if (enable_broadcasting == GNUNET_SYSERR)
3940 enable_broadcasting = GNUNET_NO;
3942 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3944 "BROADCAST_RECEIVE");
3945 if (enable_broadcasting_recv == GNUNET_SYSERR)
3946 enable_broadcasting_recv = GNUNET_YES;
3948 if (GNUNET_SYSERR ==
3949 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3951 "BROADCAST_INTERVAL",
3954 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3958 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3963 /* 50 MB/s == infinity for practical purposes */
3964 udp_max_bps = 1024 * 1024 * 50;
3967 p = GNUNET_new (struct Plugin);
3970 p->broadcast_interval = interval;
3971 p->enable_ipv6 = enable_v6;
3972 p->enable_ipv4 = GNUNET_YES; /* default */
3973 p->enable_broadcasting = enable_broadcasting;
3974 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3976 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3978 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3979 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3981 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3984 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3986 res = setup_sockets (p,
3987 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3988 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3990 ( (NULL == p->sockv4) &&
3991 (NULL == p->sockv6) ) )
3993 LOG (GNUNET_ERROR_TYPE_ERROR,
3994 _("Failed to create UDP network sockets\n"));
3995 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3996 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3997 GNUNET_SERVER_mst_destroy (p->mst);
4002 /* Setup broadcasting and receiving beacons */
4007 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
4009 api->disconnect_session = &udp_disconnect_session;
4010 api->query_keepalive_factor = &udp_query_keepalive_factor;
4011 api->disconnect_peer = &udp_disconnect;
4012 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
4013 api->address_to_string = &udp_address_to_string;
4014 api->string_to_address = &udp_string_to_address;
4015 api->check_address = &udp_plugin_check_address;
4016 api->get_session = &udp_plugin_get_session;
4017 api->send = &udp_plugin_send;
4018 api->get_network = &udp_plugin_get_network;
4019 api->get_network_for_address = &udp_plugin_get_network_for_address;
4020 api->update_session_timeout = &udp_plugin_update_session_timeout;
4021 api->setup_monitor = &udp_plugin_setup_monitor;
4027 * Function called on each entry in the defragmentation heap to
4031 * @param node node in the heap (to be removed)
4032 * @param element a `struct DefragContext` to be cleaned up
4033 * @param cost unused
4034 * @return #GNUNET_YES
4037 heap_cleanup_iterator (void *cls,
4038 struct GNUNET_CONTAINER_HeapNode *node,
4040 GNUNET_CONTAINER_HeapCostType cost)
4042 struct DefragContext *d_ctx = element;
4044 GNUNET_CONTAINER_heap_remove_node (node);
4045 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4046 GNUNET_free (d_ctx);
4052 * The exported method. Makes the core api available via a global and
4053 * returns the udp transport API.
4055 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4059 libgnunet_plugin_transport_udp_done (void *cls)
4061 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4062 struct Plugin *plugin = api->cls;
4063 struct PrettyPrinterContext *cur;
4064 struct UDP_MessageWrapper *udpw;
4071 stop_broadcast (plugin);
4072 if (NULL != plugin->select_task_v4)
4074 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4075 plugin->select_task_v4 = NULL;
4077 if (NULL != plugin->select_task_v6)
4079 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4080 plugin->select_task_v6 = NULL;
4082 if (NULL != plugin->sockv4)
4084 GNUNET_break (GNUNET_OK ==
4085 GNUNET_NETWORK_socket_close (plugin->sockv4));
4086 plugin->sockv4 = NULL;
4088 if (NULL != plugin->sockv6)
4090 GNUNET_break (GNUNET_OK ==
4091 GNUNET_NETWORK_socket_close (plugin->sockv6));
4092 plugin->sockv6 = NULL;
4094 if (NULL != plugin->nat)
4096 GNUNET_NAT_unregister (plugin->nat);
4099 if (NULL != plugin->defrag_ctxs)
4101 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4102 &heap_cleanup_iterator,
4104 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4105 plugin->defrag_ctxs = NULL;
4107 if (NULL != plugin->mst)
4109 GNUNET_SERVER_mst_destroy (plugin->mst);
4112 while (NULL != (udpw = plugin->ipv4_queue_head))
4116 udpw->qc (udpw->qc_cls,
4121 while (NULL != (udpw = plugin->ipv6_queue_head))
4125 udpw->qc (udpw->qc_cls,
4130 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4131 &disconnect_and_free_it,
4133 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4135 while (NULL != (cur = plugin->ppc_dll_head))
4138 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4139 plugin->ppc_dll_tail,
4141 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4142 if (NULL != cur->timeout_task)
4144 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4145 cur->timeout_task = NULL;
4149 GNUNET_free (plugin);
4154 /* end of plugin_transport_udp.c */