2 This file is part of GNUnet
3 Copyright (C) 2010-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file transport/plugin_transport_udp.c
18 * @brief Implementation of the UDP transport protocol
19 * @author Christian Grothoff
20 * @author Nathan Evans
21 * @author Matthias Wachs
24 #include "plugin_transport_udp.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fragmentation_lib.h"
28 #include "gnunet_nat_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
41 * After how much inactivity should a UDP session time out?
43 #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
46 * Number of messages we can defragment in parallel. We only really
47 * defragment 1 message at a time, but if messages get re-ordered, we
48 * may want to keep knowledge about the previous message to avoid
49 * discarding the current message in favor of a single fragment of a
50 * previous message. 3 should be good since we don't expect massive
51 * message reorderings with UDP.
53 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
56 * We keep a defragmentation queue per sender address. How many
57 * sender addresses do we support at the same time? Memory consumption
58 * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
59 * value. (So 128 corresponds to 12 MB and should suffice for
60 * connecting to roughly 128 peers via UDP).
62 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
66 * UDP Message-Packet header (after defragmentation).
73 struct GNUNET_MessageHeader header;
76 * Always zero for now.
81 * What is the identity of the sender
83 struct GNUNET_PeerIdentity sender;
89 * Closure for #append_port().
91 struct PrettyPrinterContext
96 struct PrettyPrinterContext *next;
101 struct PrettyPrinterContext *prev;
106 struct Plugin *plugin;
111 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
114 * Function to call with the result.
116 GNUNET_TRANSPORT_AddressStringCallback asc;
119 * Clsoure for @e asc.
126 struct GNUNET_SCHEDULER_Task *timeout_task;
129 * Is this an IPv6 address?
139 * Port to add after the IP address.
147 * Session with another peer.
149 struct GNUNET_ATS_Session
152 * Which peer is this session for?
154 struct GNUNET_PeerIdentity target;
157 * Tokenizer for inbound messages.
159 struct GNUNET_MessageStreamTokenizer *mst;
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);
631 GNUNET_MST_destroy (s->mst);
639 * Function that is called to get the keepalive factor.
640 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
641 * calculate the interval between keepalive packets.
643 * @param cls closure with the `struct Plugin`
644 * @return keepalive factor
647 udp_query_keepalive_factor (void *cls)
654 * Function obtain the network type for a session
656 * @param cls closure (`struct Plugin *`)
657 * @param session the session
658 * @return the network type
660 static enum GNUNET_ATS_Network_Type
661 udp_plugin_get_network (void *cls,
662 struct GNUNET_ATS_Session *session)
664 return session->scope;
669 * Function obtain the network type for an address.
671 * @param cls closure (`struct Plugin *`)
672 * @param address the address
673 * @return the network type
675 static enum GNUNET_ATS_Network_Type
676 udp_plugin_get_network_for_address (void *cls,
677 const struct GNUNET_HELLO_Address *address)
679 struct Plugin *plugin = cls;
681 struct sockaddr_in a4;
682 struct sockaddr_in6 a6;
683 const struct IPv4UdpAddress *u4;
684 const struct IPv6UdpAddress *u6;
688 addrlen = address->address_length;
689 if (addrlen == sizeof(struct IPv6UdpAddress))
691 GNUNET_assert (NULL != address->address); /* make static analysis happy */
692 u6 = address->address;
693 memset (&a6, 0, sizeof(a6));
694 #if HAVE_SOCKADDR_IN_SIN_LEN
695 a6.sin6_len = sizeof (a6);
697 a6.sin6_family = AF_INET6;
698 a6.sin6_port = u6->u6_port;
699 GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
703 else if (addrlen == sizeof(struct IPv4UdpAddress))
705 GNUNET_assert (NULL != address->address); /* make static analysis happy */
706 u4 = address->address;
707 memset (&a4, 0, sizeof(a4));
708 #if HAVE_SOCKADDR_IN_SIN_LEN
709 a4.sin_len = sizeof (a4);
711 a4.sin_family = AF_INET;
712 a4.sin_port = u4->u4_port;
713 a4.sin_addr.s_addr = u4->ipv4_addr;
720 return GNUNET_ATS_NET_UNSPECIFIED;
722 return plugin->env->get_address_type (plugin->env->cls,
728 /* ******************* Event loop ******************** */
731 * We have been notified that our readset has something to read. We don't
732 * know which socket needs to be read, so we have to check each one
733 * Then reschedule this function to be called again once more is available.
735 * @param cls the plugin handle
738 udp_plugin_select_v4 (void *cls);
742 * We have been notified that our readset has something to read. We don't
743 * know which socket needs to be read, so we have to check each one
744 * Then reschedule this function to be called again once more is available.
746 * @param cls the plugin handle
749 udp_plugin_select_v6 (void *cls);
753 * (re)schedule IPv4-select tasks for this plugin.
755 * @param plugin plugin to reschedule
758 schedule_select_v4 (struct Plugin *plugin)
760 struct GNUNET_TIME_Relative min_delay;
761 struct GNUNET_TIME_Relative delay;
762 struct UDP_MessageWrapper *udpw;
763 struct UDP_MessageWrapper *min_udpw;
765 if ( (GNUNET_YES == plugin->enable_ipv4) &&
766 (NULL != plugin->sockv4) )
768 /* Find a message ready to send:
769 * Flow delay from other peer is expired or not set (0) */
770 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
772 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
774 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
775 if (delay.rel_value_us < min_delay.rel_value_us)
781 if (NULL != plugin->select_task_v4)
782 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
783 if (NULL != min_udpw)
785 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788 "Calculated flow delay for UDPv4 at %s for %s\n",
789 GNUNET_STRINGS_relative_time_to_string (min_delay,
791 GNUNET_i2s (&min_udpw->session->target));
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
796 "Calculated flow delay for UDPv4 at %s for %s\n",
797 GNUNET_STRINGS_relative_time_to_string (min_delay,
799 GNUNET_i2s (&min_udpw->session->target));
802 plugin->select_task_v4
803 = GNUNET_SCHEDULER_add_read_net (min_delay,
805 &udp_plugin_select_v4,
812 * (re)schedule IPv6-select tasks for this plugin.
814 * @param plugin plugin to reschedule
817 schedule_select_v6 (struct Plugin *plugin)
819 struct GNUNET_TIME_Relative min_delay;
820 struct GNUNET_TIME_Relative delay;
821 struct UDP_MessageWrapper *udpw;
822 struct UDP_MessageWrapper *min_udpw;
824 if ( (GNUNET_YES == plugin->enable_ipv6) &&
825 (NULL != plugin->sockv6) )
827 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
829 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
831 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
832 if (delay.rel_value_us < min_delay.rel_value_us)
838 if (NULL != plugin->select_task_v6)
839 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
840 if (NULL != min_udpw)
842 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
844 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
845 "Calculated flow delay for UDPv6 at %s for %s\n",
846 GNUNET_STRINGS_relative_time_to_string (min_delay,
848 GNUNET_i2s (&min_udpw->session->target));
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Calculated flow delay for UDPv6 at %s for %s\n",
854 GNUNET_STRINGS_relative_time_to_string (min_delay,
856 GNUNET_i2s (&min_udpw->session->target));
859 plugin->select_task_v6
860 = GNUNET_SCHEDULER_add_read_net (min_delay,
862 &udp_plugin_select_v6,
868 /* ******************* Address to string and back ***************** */
872 * Function called for a quick conversion of the binary address to
873 * a numeric address. Note that the caller must not free the
874 * address and that the next call to this function is allowed
875 * to override the address again.
878 * @param addr binary address (a `union UdpAddress`)
879 * @param addrlen length of the @a addr
880 * @return string representing the same address
883 udp_address_to_string (void *cls,
887 static char rbuf[INET6_ADDRSTRLEN + 10];
888 char buf[INET6_ADDRSTRLEN];
892 const struct IPv4UdpAddress *t4;
893 const struct IPv6UdpAddress *t6;
904 if (addrlen == sizeof(struct IPv6UdpAddress))
908 options = ntohl (t6->options);
909 port = ntohs (t6->u6_port);
913 else if (addrlen == sizeof(struct IPv4UdpAddress))
917 options = ntohl (t4->options);
918 port = ntohs (t4->u4_port);
919 a4.s_addr = t4->ipv4_addr;
931 GNUNET_snprintf (rbuf,
945 * Function called to convert a string address to a binary address.
947 * @param cls closure (`struct Plugin *`)
948 * @param addr string address
949 * @param addrlen length of the address
950 * @param buf location to store the buffer
951 * @param added location to store the number of bytes in the buffer.
952 * If the function returns #GNUNET_SYSERR, its contents are undefined.
953 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
956 udp_string_to_address (void *cls,
962 struct sockaddr_storage socket_address;
968 /* Format tcp.options.address:port */
973 if ((NULL == addr) || (0 == addrlen))
976 return GNUNET_SYSERR;
978 if ('\0' != addr[addrlen - 1])
981 return GNUNET_SYSERR;
983 if (strlen (addr) != addrlen - 1)
986 return GNUNET_SYSERR;
988 plugin = GNUNET_strdup (addr);
989 optionstr = strchr (plugin, '.');
990 if (NULL == optionstr)
993 GNUNET_free (plugin);
994 return GNUNET_SYSERR;
998 options = atol (optionstr);
999 address = strchr (optionstr, '.');
1000 if (NULL == address)
1003 GNUNET_free (plugin);
1004 return GNUNET_SYSERR;
1010 GNUNET_STRINGS_to_address_ip (address,
1015 GNUNET_free (plugin);
1016 return GNUNET_SYSERR;
1018 GNUNET_free(plugin);
1020 switch (socket_address.ss_family)
1024 struct IPv4UdpAddress *u4;
1025 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1027 u4 = GNUNET_new (struct IPv4UdpAddress);
1028 u4->options = htonl (options);
1029 u4->ipv4_addr = in4->sin_addr.s_addr;
1030 u4->u4_port = in4->sin_port;
1032 *added = sizeof (struct IPv4UdpAddress);
1037 struct IPv6UdpAddress *u6;
1038 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1040 u6 = GNUNET_new (struct IPv6UdpAddress);
1041 u6->options = htonl (options);
1042 u6->ipv6_addr = in6->sin6_addr;
1043 u6->u6_port = in6->sin6_port;
1045 *added = sizeof (struct IPv6UdpAddress);
1050 return GNUNET_SYSERR;
1056 * Append our port and forward the result.
1058 * @param cls a `struct PrettyPrinterContext *`
1059 * @param hostname result from DNS resolver
1062 append_port (void *cls,
1063 const char *hostname)
1065 struct PrettyPrinterContext *ppc = cls;
1066 struct Plugin *plugin = ppc->plugin;
1069 if (NULL == hostname)
1071 /* Final call, done */
1072 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1073 plugin->ppc_dll_tail,
1075 ppc->resolver_handle = NULL;
1076 ppc->asc (ppc->asc_cls,
1082 if (GNUNET_YES == ppc->ipv6)
1083 GNUNET_asprintf (&ret,
1090 GNUNET_asprintf (&ret,
1096 ppc->asc (ppc->asc_cls,
1104 * Convert the transports address to a nice, human-readable format.
1106 * @param cls closure with the `struct Plugin *`
1107 * @param type name of the transport that generated the address
1108 * @param addr one of the addresses of the host, NULL for the last address
1109 * the specific address format depends on the transport;
1110 * a `union UdpAddress`
1111 * @param addrlen length of the address
1112 * @param numeric should (IP) addresses be displayed in numeric form?
1113 * @param timeout after how long should we give up?
1114 * @param asc function to call on each string
1115 * @param asc_cls closure for @a asc
1118 udp_plugin_address_pretty_printer (void *cls,
1123 struct GNUNET_TIME_Relative timeout,
1124 GNUNET_TRANSPORT_AddressStringCallback asc,
1127 struct Plugin *plugin = cls;
1128 struct PrettyPrinterContext *ppc;
1129 const struct sockaddr *sb;
1131 struct sockaddr_in a4;
1132 struct sockaddr_in6 a6;
1133 const struct IPv4UdpAddress *u4;
1134 const struct IPv6UdpAddress *u6;
1138 if (addrlen == sizeof(struct IPv6UdpAddress))
1144 a6.sin6_family = AF_INET6;
1145 #if HAVE_SOCKADDR_IN_SIN_LEN
1146 a6.sin6_len = sizeof (a6);
1148 a6.sin6_port = u6->u6_port;
1149 a6.sin6_addr = u6->ipv6_addr;
1150 port = ntohs (u6->u6_port);
1151 options = ntohl (u6->options);
1152 sb = (const struct sockaddr *) &a6;
1155 else if (addrlen == sizeof (struct IPv4UdpAddress))
1161 a4.sin_family = AF_INET;
1162 #if HAVE_SOCKADDR_IN_SIN_LEN
1163 a4.sin_len = sizeof (a4);
1165 a4.sin_port = u4->u4_port;
1166 a4.sin_addr.s_addr = u4->ipv4_addr;
1167 port = ntohs (u4->u4_port);
1168 options = ntohl (u4->options);
1169 sb = (const struct sockaddr *) &a4;
1174 /* invalid address */
1175 GNUNET_break_op (0);
1184 ppc = GNUNET_new (struct PrettyPrinterContext);
1185 ppc->plugin = plugin;
1187 ppc->asc_cls = asc_cls;
1189 ppc->options = options;
1190 if (addrlen == sizeof (struct IPv6UdpAddress))
1191 ppc->ipv6 = GNUNET_YES;
1193 ppc->ipv6 = GNUNET_NO;
1194 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1195 plugin->ppc_dll_tail,
1197 ppc->resolver_handle
1198 = GNUNET_RESOLVER_hostname_get (sb,
1208 * Check if the given port is plausible (must be either our listen
1209 * port or our advertised port). If it is neither, we return
1212 * @param plugin global variables
1213 * @param in_port port number to check
1214 * @return #GNUNET_OK if port is either our open or advertised port
1217 check_port (const struct Plugin *plugin,
1220 if ( (plugin->port == in_port) ||
1221 (plugin->aport == in_port) )
1223 return GNUNET_SYSERR;
1228 * Function that will be called to check if a binary address for this
1229 * plugin is well-formed and corresponds to an address for THIS peer
1230 * (as per our configuration). Naturally, if absolutely necessary,
1231 * plugins can be a bit conservative in their answer, but in general
1232 * plugins should make sure that the address does not redirect
1233 * traffic to a 3rd party that might try to man-in-the-middle our
1236 * @param cls closure, should be our handle to the Plugin
1237 * @param addr pointer to a `union UdpAddress`
1238 * @param addrlen length of @a addr
1239 * @return #GNUNET_OK if this is a plausible address for this peer
1240 * and transport, #GNUNET_SYSERR if not
1243 udp_plugin_check_address (void *cls,
1247 struct Plugin *plugin = cls;
1248 const struct IPv4UdpAddress *v4;
1249 const struct IPv6UdpAddress *v6;
1251 if (sizeof(struct IPv4UdpAddress) == addrlen)
1253 struct sockaddr_in s4;
1255 v4 = (const struct IPv4UdpAddress *) addr;
1256 if (GNUNET_OK != check_port (plugin,
1257 ntohs (v4->u4_port)))
1258 return GNUNET_SYSERR;
1259 memset (&s4, 0, sizeof (s4));
1260 s4.sin_family = AF_INET;
1261 #if HAVE_SOCKADDR_IN_SIN_LEN
1262 s4.sin_len = sizeof (s4);
1264 s4.sin_port = v4->u4_port;
1265 s4.sin_addr.s_addr = v4->ipv4_addr;
1268 GNUNET_NAT_test_address (plugin->nat,
1270 sizeof (struct sockaddr_in)))
1271 return GNUNET_SYSERR;
1273 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1275 struct sockaddr_in6 s6;
1277 v6 = (const struct IPv6UdpAddress *) addr;
1278 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1279 return GNUNET_OK; /* plausible, if unlikely... */
1280 memset (&s6, 0, sizeof (s6));
1281 s6.sin6_family = AF_INET6;
1282 #if HAVE_SOCKADDR_IN_SIN_LEN
1283 s6.sin6_len = sizeof (s6);
1285 s6.sin6_port = v6->u6_port;
1286 s6.sin6_addr = v6->ipv6_addr;
1289 GNUNET_NAT_test_address (plugin->nat,
1291 sizeof(struct sockaddr_in6)))
1292 return GNUNET_SYSERR;
1296 GNUNET_break_op (0);
1297 return GNUNET_SYSERR;
1304 * Our external IP address/port mapping has changed.
1306 * @param cls closure, the `struct Plugin`
1307 * @param add_remove #GNUNET_YES to mean the new public IP address,
1308 * #GNUNET_NO to mean the previous (now invalid) one
1309 * @param ac address class the address belongs to
1310 * @param addr either the previous or the new public IP address
1311 * @param addrlen actual length of the @a addr
1314 udp_nat_port_map_callback (void *cls,
1316 enum GNUNET_NAT_AddressClass ac,
1317 const struct sockaddr *addr,
1320 struct Plugin *plugin = cls;
1321 struct GNUNET_HELLO_Address *address;
1322 struct IPv4UdpAddress u4;
1323 struct IPv6UdpAddress u6;
1327 LOG (GNUNET_ERROR_TYPE_DEBUG,
1328 (GNUNET_YES == add_remove)
1329 ? "NAT notification to add address `%s'\n"
1330 : "NAT notification to remove address `%s'\n",
1333 /* convert 'address' to our internal format */
1334 switch (addr->sa_family)
1338 const struct sockaddr_in *i4;
1340 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1341 i4 = (const struct sockaddr_in *) addr;
1342 if (0 == ntohs (i4->sin_port))
1343 return; /* Port = 0 means unmapped, ignore these for UDP. */
1347 u4.options = htonl (plugin->myoptions);
1348 u4.ipv4_addr = i4->sin_addr.s_addr;
1349 u4.u4_port = i4->sin_port;
1351 args = sizeof (struct IPv4UdpAddress);
1356 const struct sockaddr_in6 *i6;
1358 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1359 i6 = (const struct sockaddr_in6 *) addr;
1360 if (0 == ntohs (i6->sin6_port))
1361 return; /* Port = 0 means unmapped, ignore these for UDP. */
1365 u6.options = htonl (plugin->myoptions);
1366 u6.ipv6_addr = i6->sin6_addr;
1367 u6.u6_port = i6->sin6_port;
1369 args = sizeof (struct IPv6UdpAddress);
1376 /* modify our published address list */
1377 /* TODO: use 'ac' here in the future... */
1378 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1382 GNUNET_HELLO_ADDRESS_INFO_NONE);
1383 plugin->env->notify_address (plugin->env->cls,
1386 GNUNET_HELLO_address_free (address);
1390 /* ********************* Finding sessions ******************* */
1394 * Closure for #session_cmp_it().
1396 struct GNUNET_ATS_SessionCompareContext
1399 * Set to session matching the address.
1401 struct GNUNET_ATS_Session *res;
1404 * Address we are looking for.
1406 const struct GNUNET_HELLO_Address *address;
1411 * Find a session with a matching address.
1413 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1414 * @param key peer identity (unused)
1415 * @param value the `struct GNUNET_ATS_Session *`
1416 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1419 session_cmp_it (void *cls,
1420 const struct GNUNET_PeerIdentity *key,
1423 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1424 struct GNUNET_ATS_Session *s = value;
1426 if (0 == GNUNET_HELLO_address_cmp (s->address,
1429 GNUNET_assert (GNUNET_NO == s->in_destroy);
1438 * Locate an existing session the transport service is using to
1439 * send data to another peer. Performs some basic sanity checks
1440 * on the address and then tries to locate a matching session.
1442 * @param cls the plugin
1443 * @param address the address we should locate the session by
1444 * @return the session if it exists, or NULL if it is not found
1446 static struct GNUNET_ATS_Session *
1447 udp_plugin_lookup_session (void *cls,
1448 const struct GNUNET_HELLO_Address *address)
1450 struct Plugin *plugin = cls;
1451 const struct IPv6UdpAddress *udp_a6;
1452 const struct IPv4UdpAddress *udp_a4;
1453 struct GNUNET_ATS_SessionCompareContext cctx;
1455 if (NULL == address->address)
1460 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1462 if (NULL == plugin->sockv4)
1464 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1465 if (0 == udp_a4->u4_port)
1471 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1473 if (NULL == plugin->sockv6)
1475 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1476 if (0 == udp_a6->u6_port)
1488 /* check if session already exists */
1489 cctx.address = address;
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Looking for existing session for peer `%s' with address `%s'\n",
1493 GNUNET_i2s (&address->peer),
1494 udp_address_to_string (plugin,
1496 address->address_length));
1497 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1501 if (NULL == cctx.res)
1503 LOG (GNUNET_ERROR_TYPE_DEBUG,
1504 "Found existing session %p\n",
1510 /* ********************** Timeout ****************** */
1514 * Increment session timeout due to activity.
1516 * @param s session to reschedule timeout activity for
1519 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1521 if (GNUNET_YES == s->in_destroy)
1523 GNUNET_assert (NULL != s->timeout_task);
1524 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1530 * Function that will be called whenever the transport service wants to
1531 * notify the plugin that a session is still active and in use and
1532 * therefore the session timeout for this session has to be updated
1534 * @param cls closure with the `struct Plugin`
1535 * @param peer which peer was the session for
1536 * @param session which session is being updated
1539 udp_plugin_update_session_timeout (void *cls,
1540 const struct GNUNET_PeerIdentity *peer,
1541 struct GNUNET_ATS_Session *session)
1543 struct Plugin *plugin = cls;
1546 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1553 /* Reschedule session timeout */
1554 reschedule_session_timeout (session);
1558 /* ************************* Sending ************************ */
1562 * Remove the given message from the transmission queue and
1563 * update all applicable statistics.
1565 * @param plugin the UDP plugin
1566 * @param udpw message wrapper to dequeue
1569 dequeue (struct Plugin *plugin,
1570 struct UDP_MessageWrapper *udpw)
1572 struct GNUNET_ATS_Session *session = udpw->session;
1574 if (plugin->bytes_in_buffer < udpw->msg_size)
1580 GNUNET_STATISTICS_update (plugin->env->stats,
1581 "# UDP, total bytes in send buffers",
1582 - (long long) udpw->msg_size,
1584 plugin->bytes_in_buffer -= udpw->msg_size;
1586 GNUNET_STATISTICS_update (plugin->env->stats,
1587 "# UDP, total messages in send buffers",
1590 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1592 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1593 plugin->ipv4_queue_tail,
1596 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1598 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1599 plugin->ipv6_queue_tail,
1607 GNUNET_assert (session->msgs_in_queue > 0);
1608 session->msgs_in_queue--;
1609 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1610 session->bytes_in_queue -= udpw->msg_size;
1615 * Enqueue a message for transmission and update statistics.
1617 * @param plugin the UDP plugin
1618 * @param udpw message wrapper to queue
1621 enqueue (struct Plugin *plugin,
1622 struct UDP_MessageWrapper *udpw)
1624 struct GNUNET_ATS_Session *session = udpw->session;
1626 if (GNUNET_YES == session->in_destroy)
1632 if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
1638 GNUNET_STATISTICS_update (plugin->env->stats,
1639 "# UDP, total bytes in send buffers",
1642 plugin->bytes_in_buffer += udpw->msg_size;
1644 GNUNET_STATISTICS_update (plugin->env->stats,
1645 "# UDP, total messages in send buffers",
1648 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1650 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1651 plugin->ipv4_queue_tail,
1654 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1656 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1657 plugin->ipv6_queue_tail,
1663 udpw->cont (udpw->cont_cls,
1671 session->msgs_in_queue++;
1672 session->bytes_in_queue += udpw->msg_size;
1677 * We have completed our (attempt) to transmit a message that had to
1678 * be fragmented -- either because we got an ACK saying that all
1679 * fragments were received, or because of timeout / disconnect. Clean
1682 * @param frag_ctx fragmentation context to clean up
1683 * @param result #GNUNET_OK if we succeeded (got ACK),
1684 * #GNUNET_SYSERR if the transmission failed
1687 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1690 struct Plugin *plugin = frag_ctx->plugin;
1691 struct GNUNET_ATS_Session *s = frag_ctx->session;
1692 struct UDP_MessageWrapper *udpw;
1693 struct UDP_MessageWrapper *tmp;
1695 struct GNUNET_TIME_Relative delay;
1697 LOG (GNUNET_ERROR_TYPE_DEBUG,
1698 "%p: Fragmented message removed with result %s\n",
1700 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1701 /* Call continuation for fragmented message */
1702 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1703 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1705 overhead = frag_ctx->on_wire_size;
1706 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1707 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1709 LOG (GNUNET_ERROR_TYPE_WARNING,
1710 "Fragmented message acknowledged after %s (expected at %s)\n",
1711 GNUNET_STRINGS_relative_time_to_string (delay,
1713 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1717 LOG (GNUNET_ERROR_TYPE_DEBUG,
1718 "Fragmented message acknowledged after %s (expected at %s)\n",
1719 GNUNET_STRINGS_relative_time_to_string (delay,
1721 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1724 if (NULL != frag_ctx->cont)
1725 frag_ctx->cont (frag_ctx->cont_cls,
1728 s->frag_ctx->payload_size,
1729 frag_ctx->on_wire_size);
1730 GNUNET_STATISTICS_update (plugin->env->stats,
1731 "# UDP, fragmented messages active",
1735 if (GNUNET_OK == result)
1737 GNUNET_STATISTICS_update (plugin->env->stats,
1738 "# UDP, fragmented msgs, messages, sent, success",
1741 GNUNET_STATISTICS_update (plugin->env->stats,
1742 "# UDP, fragmented msgs, bytes payload, sent, success",
1743 s->frag_ctx->payload_size,
1745 GNUNET_STATISTICS_update (plugin->env->stats,
1746 "# UDP, fragmented msgs, bytes overhead, sent, success",
1749 GNUNET_STATISTICS_update (plugin->env->stats,
1750 "# UDP, total, bytes overhead, sent",
1753 GNUNET_STATISTICS_update (plugin->env->stats,
1754 "# UDP, total, bytes payload, sent",
1755 s->frag_ctx->payload_size,
1760 GNUNET_STATISTICS_update (plugin->env->stats,
1761 "# UDP, fragmented msgs, messages, sent, failure",
1764 GNUNET_STATISTICS_update (plugin->env->stats,
1765 "# UDP, fragmented msgs, bytes payload, sent, failure",
1766 s->frag_ctx->payload_size,
1768 GNUNET_STATISTICS_update (plugin->env->stats,
1769 "# UDP, fragmented msgs, bytes payload, sent, failure",
1772 GNUNET_STATISTICS_update (plugin->env->stats,
1773 "# UDP, fragmented msgs, bytes payload, sent, failure",
1778 /* Remove remaining fragments from queue, no need to transmit those
1780 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1782 udpw = plugin->ipv6_queue_head;
1783 while (NULL != udpw)
1786 if ( (udpw->frag_ctx != NULL) &&
1787 (udpw->frag_ctx == frag_ctx) )
1796 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1798 udpw = plugin->ipv4_queue_head;
1799 while (NULL != udpw)
1802 if ( (NULL != udpw->frag_ctx) &&
1803 (udpw->frag_ctx == frag_ctx) )
1812 notify_session_monitor (s->plugin,
1814 GNUNET_TRANSPORT_SS_UPDATE);
1815 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1816 &s->last_expected_msg_delay,
1817 &s->last_expected_ack_delay);
1819 GNUNET_free (frag_ctx);
1824 * We are finished with a fragment in the message queue.
1825 * Notify the continuation and update statistics.
1827 * @param cls the `struct Plugin *`
1828 * @param udpw the queue entry
1829 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1832 qc_fragment_sent (void *cls,
1833 struct UDP_MessageWrapper *udpw,
1836 struct Plugin *plugin = cls;
1838 GNUNET_assert (NULL != udpw->frag_ctx);
1839 if (GNUNET_OK == result)
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Fragment of message with %u bytes transmitted to %s\n",
1843 (unsigned int) udpw->payload_size,
1844 GNUNET_i2s (&udpw->session->target));
1845 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1846 GNUNET_STATISTICS_update (plugin->env->stats,
1847 "# UDP, fragmented msgs, fragments, sent, success",
1850 GNUNET_STATISTICS_update (plugin->env->stats,
1851 "# UDP, fragmented msgs, fragments bytes, sent, success",
1857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1858 "Failed to transmit fragment of message with %u bytes to %s\n",
1859 (unsigned int) udpw->payload_size,
1860 GNUNET_i2s (&udpw->session->target));
1861 fragmented_message_done (udpw->frag_ctx,
1863 GNUNET_STATISTICS_update (plugin->env->stats,
1864 "# UDP, fragmented msgs, fragments, sent, failure",
1867 GNUNET_STATISTICS_update (plugin->env->stats,
1868 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1876 * Function that is called with messages created by the fragmentation
1877 * module. In the case of the `proc` callback of the
1878 * #GNUNET_FRAGMENT_context_create() function, this function must
1879 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1881 * @param cls closure, the `struct UDP_FragmentationContext`
1882 * @param msg the message that was created
1885 enqueue_fragment (void *cls,
1886 const struct GNUNET_MessageHeader *msg)
1888 struct UDP_FragmentationContext *frag_ctx = cls;
1889 struct Plugin *plugin = frag_ctx->plugin;
1890 struct UDP_MessageWrapper *udpw;
1891 struct GNUNET_ATS_Session *session = frag_ctx->session;
1892 size_t msg_len = ntohs (msg->size);
1894 LOG (GNUNET_ERROR_TYPE_DEBUG,
1895 "Enqueuing fragment with %u bytes\n",
1897 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1898 udpw->session = session;
1899 udpw->msg_buf = (char *) &udpw[1];
1900 udpw->msg_size = msg_len;
1901 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1902 udpw->timeout = frag_ctx->timeout;
1903 udpw->start_time = frag_ctx->start_time;
1904 udpw->transmission_time = frag_ctx->next_frag_time;
1905 frag_ctx->next_frag_time
1906 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1907 frag_ctx->flow_delay_from_other_peer);
1908 udpw->frag_ctx = frag_ctx;
1909 udpw->qc = &qc_fragment_sent;
1910 udpw->qc_cls = plugin;
1911 GNUNET_memcpy (udpw->msg_buf,
1916 if (session->address->address_length == sizeof (struct IPv4UdpAddress))
1917 schedule_select_v4 (plugin);
1919 schedule_select_v6 (plugin);
1924 * We are finished with a message from the message queue.
1925 * Notify the continuation and update statistics.
1927 * @param cls the `struct Plugin *`
1928 * @param udpw the queue entry
1929 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1932 qc_message_sent (void *cls,
1933 struct UDP_MessageWrapper *udpw,
1936 struct Plugin *plugin = cls;
1938 struct GNUNET_TIME_Relative delay;
1940 if (udpw->msg_size >= udpw->payload_size)
1941 overhead = udpw->msg_size - udpw->payload_size;
1943 overhead = udpw->msg_size;
1945 if (NULL != udpw->cont)
1947 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1948 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1950 LOG (GNUNET_ERROR_TYPE_WARNING,
1951 "Message sent via UDP with delay of %s\n",
1952 GNUNET_STRINGS_relative_time_to_string (delay,
1957 LOG (GNUNET_ERROR_TYPE_DEBUG,
1958 "Message sent via UDP with delay of %s\n",
1959 GNUNET_STRINGS_relative_time_to_string (delay,
1962 udpw->cont (udpw->cont_cls,
1963 &udpw->session->target,
1968 if (GNUNET_OK == result)
1970 GNUNET_STATISTICS_update (plugin->env->stats,
1971 "# UDP, unfragmented msgs, messages, sent, success",
1974 GNUNET_STATISTICS_update (plugin->env->stats,
1975 "# UDP, unfragmented msgs, bytes payload, sent, success",
1978 GNUNET_STATISTICS_update (plugin->env->stats,
1979 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1982 GNUNET_STATISTICS_update (plugin->env->stats,
1983 "# UDP, total, bytes overhead, sent",
1986 GNUNET_STATISTICS_update (plugin->env->stats,
1987 "# UDP, total, bytes payload, sent",
1993 GNUNET_STATISTICS_update (plugin->env->stats,
1994 "# UDP, unfragmented msgs, messages, sent, failure",
1997 GNUNET_STATISTICS_update (plugin->env->stats,
1998 "# UDP, unfragmented msgs, bytes payload, sent, failure",
2001 GNUNET_STATISTICS_update (plugin->env->stats,
2002 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
2010 * Function that can be used by the transport service to transmit a
2011 * message using the plugin. Note that in the case of a peer
2012 * disconnecting, the continuation MUST be called prior to the
2013 * disconnect notification itself. This function will be called with
2014 * this peer's HELLO message to initiate a fresh connection to another
2017 * @param cls closure
2018 * @param s which session must be used
2019 * @param msgbuf the message to transmit
2020 * @param msgbuf_size number of bytes in @a msgbuf
2021 * @param priority how important is the message (most plugins will
2022 * ignore message priority and just FIFO)
2023 * @param to how long to wait at most for the transmission (does not
2024 * require plugins to discard the message after the timeout,
2025 * just advisory for the desired delay; most plugins will ignore
2027 * @param cont continuation to call once the message has
2028 * been transmitted (or if the transport is ready
2029 * for the next transmission call; or if the
2030 * peer disconnected...); can be NULL
2031 * @param cont_cls closure for @a cont
2032 * @return number of bytes used (on the physical network, with overheads);
2033 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2034 * and does NOT mean that the message was not transmitted (DV)
2037 udp_plugin_send (void *cls,
2038 struct GNUNET_ATS_Session *s,
2041 unsigned int priority,
2042 struct GNUNET_TIME_Relative to,
2043 GNUNET_TRANSPORT_TransmitContinuation cont,
2046 struct Plugin *plugin = cls;
2047 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2048 struct UDP_FragmentationContext *frag_ctx;
2049 struct UDP_MessageWrapper *udpw;
2050 struct UDPMessage *udp;
2051 char mbuf[udpmlen] GNUNET_ALIGN;
2052 struct GNUNET_TIME_Relative latency;
2054 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2055 (NULL == plugin->sockv6) )
2056 return GNUNET_SYSERR;
2057 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2058 (NULL == plugin->sockv4) )
2059 return GNUNET_SYSERR;
2060 if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
2063 return GNUNET_SYSERR;
2066 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2071 return GNUNET_SYSERR;
2073 LOG (GNUNET_ERROR_TYPE_DEBUG,
2074 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2076 GNUNET_i2s (&s->target),
2077 udp_address_to_string (plugin,
2078 s->address->address,
2079 s->address->address_length));
2081 udp = (struct UDPMessage *) mbuf;
2082 udp->header.size = htons (udpmlen);
2083 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2084 udp->reserved = htonl (0);
2085 udp->sender = *plugin->env->my_identity;
2087 /* We do not update the session time out here! Otherwise this
2088 * session will not timeout since we send keep alive before session
2091 * For UDP we update session timeout only on receive, this will
2092 * cover keep alives, since remote peer will reply with keep alive
2095 if (udpmlen <= UDP_MTU)
2097 /* unfragmented message */
2098 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2100 udpw->msg_buf = (char *) &udpw[1];
2101 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2102 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2103 udpw->start_time = GNUNET_TIME_absolute_get ();
2104 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2105 udpw->transmission_time = s->last_transmit_time;
2106 s->last_transmit_time
2107 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2108 s->flow_delay_from_other_peer);
2110 udpw->cont_cls = cont_cls;
2111 udpw->frag_ctx = NULL;
2112 udpw->qc = &qc_message_sent;
2113 udpw->qc_cls = plugin;
2114 GNUNET_memcpy (udpw->msg_buf,
2116 sizeof (struct UDPMessage));
2117 GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2122 GNUNET_STATISTICS_update (plugin->env->stats,
2123 "# UDP, unfragmented messages queued total",
2126 GNUNET_STATISTICS_update (plugin->env->stats,
2127 "# UDP, unfragmented bytes payload queued total",
2130 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2131 schedule_select_v4 (plugin);
2133 schedule_select_v6 (plugin);
2137 /* fragmented message */
2138 if (NULL != s->frag_ctx)
2139 return GNUNET_SYSERR;
2140 GNUNET_memcpy (&udp[1],
2143 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2144 frag_ctx->plugin = plugin;
2145 frag_ctx->session = s;
2146 frag_ctx->cont = cont;
2147 frag_ctx->cont_cls = cont_cls;
2148 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2149 frag_ctx->next_frag_time = s->last_transmit_time;
2150 frag_ctx->flow_delay_from_other_peer
2151 = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
2154 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2155 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2156 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2157 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2160 s->last_expected_msg_delay,
2161 s->last_expected_ack_delay,
2165 s->frag_ctx = frag_ctx;
2166 s->last_transmit_time = frag_ctx->next_frag_time;
2167 latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
2168 if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2169 LOG (GNUNET_ERROR_TYPE_WARNING,
2170 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2171 GNUNET_STRINGS_relative_time_to_string (latency,
2173 GNUNET_i2s (&s->target),
2174 (unsigned int) s->msgs_in_queue);
2176 LOG (GNUNET_ERROR_TYPE_DEBUG,
2177 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2178 GNUNET_STRINGS_relative_time_to_string (latency,
2180 GNUNET_i2s (&s->target),
2181 (unsigned int) s->msgs_in_queue);
2183 GNUNET_STATISTICS_update (plugin->env->stats,
2184 "# UDP, fragmented messages active",
2187 GNUNET_STATISTICS_update (plugin->env->stats,
2188 "# UDP, fragmented messages, total",
2191 GNUNET_STATISTICS_update (plugin->env->stats,
2192 "# UDP, fragmented bytes (payload)",
2193 frag_ctx->payload_size,
2196 notify_session_monitor (s->plugin,
2198 GNUNET_TRANSPORT_SS_UPDATE);
2203 /* ********************** Receiving ********************** */
2207 * Closure for #find_receive_context().
2209 struct FindReceiveContext
2212 * Where to store the result.
2214 struct DefragContext *rc;
2217 * Session associated with this context.
2219 struct GNUNET_ATS_Session *session;
2224 const union UdpAddress *udp_addr;
2227 * Number of bytes in @e udp_addr.
2229 size_t udp_addr_len;
2235 * Scan the heap for a receive context with the given address.
2237 * @param cls the `struct FindReceiveContext`
2238 * @param node internal node of the heap
2239 * @param element value stored at the node (a `struct ReceiveContext`)
2240 * @param cost cost associated with the node
2241 * @return #GNUNET_YES if we should continue to iterate,
2242 * #GNUNET_NO if not.
2245 find_receive_context (void *cls,
2246 struct GNUNET_CONTAINER_HeapNode *node,
2248 GNUNET_CONTAINER_HeapCostType cost)
2250 struct FindReceiveContext *frc = cls;
2251 struct DefragContext *e = element;
2253 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2254 (0 == memcmp (frc->udp_addr,
2256 frc->udp_addr_len)) )
2266 * Functions with this signature are called whenever we need to close
2267 * a session due to a disconnect or failure to establish a connection.
2269 * @param cls closure with the `struct Plugin`
2270 * @param s session to close down
2271 * @return #GNUNET_OK on success
2274 udp_disconnect_session (void *cls,
2275 struct GNUNET_ATS_Session *s)
2277 struct Plugin *plugin = cls;
2278 struct UDP_MessageWrapper *udpw;
2279 struct UDP_MessageWrapper *next;
2280 struct FindReceiveContext frc;
2282 GNUNET_assert (GNUNET_YES != s->in_destroy);
2283 LOG (GNUNET_ERROR_TYPE_DEBUG,
2284 "Session %p to peer `%s' at address %s ended\n",
2286 GNUNET_i2s (&s->target),
2287 udp_address_to_string (plugin,
2288 s->address->address,
2289 s->address->address_length));
2290 if (NULL != s->timeout_task)
2292 GNUNET_SCHEDULER_cancel (s->timeout_task);
2293 s->timeout_task = NULL;
2295 if (NULL != s->frag_ctx)
2297 /* Remove fragmented message due to disconnect */
2298 fragmented_message_done (s->frag_ctx,
2301 GNUNET_assert (GNUNET_YES ==
2302 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2306 frc.udp_addr = s->address->address;
2307 frc.udp_addr_len = s->address->address_length;
2308 /* Lookup existing receive context for this address */
2309 if (NULL != plugin->defrag_ctxs)
2311 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2312 &find_receive_context,
2316 struct DefragContext *d_ctx = frc.rc;
2318 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2319 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2320 GNUNET_free (d_ctx);
2323 s->in_destroy = GNUNET_YES;
2324 next = plugin->ipv4_queue_head;
2325 while (NULL != (udpw = next))
2328 if (udpw->session == s)
2332 udpw->qc (udpw->qc_cls,
2338 next = plugin->ipv6_queue_head;
2339 while (NULL != (udpw = next))
2342 if (udpw->session == s)
2346 udpw->qc (udpw->qc_cls,
2352 if ( (NULL != s->frag_ctx) &&
2353 (NULL != s->frag_ctx->cont) )
2355 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2356 later, as it might be in use right now */
2357 LOG (GNUNET_ERROR_TYPE_DEBUG,
2358 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2359 GNUNET_i2s (&s->target));
2360 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2363 s->frag_ctx->payload_size,
2364 s->frag_ctx->on_wire_size);
2366 notify_session_monitor (s->plugin,
2368 GNUNET_TRANSPORT_SS_DONE);
2369 plugin->env->session_end (plugin->env->cls,
2372 GNUNET_STATISTICS_set (plugin->env->stats,
2373 "# UDP sessions active",
2374 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2383 * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
2385 * @param plugin the UDP plugin
2386 * @param msg the (presumed) UDP ACK message
2387 * @param udp_addr sender address
2388 * @param udp_addr_len number of bytes in @a udp_addr
2391 read_process_ack (struct Plugin *plugin,
2392 const struct GNUNET_MessageHeader *msg,
2393 const union UdpAddress *udp_addr,
2394 socklen_t udp_addr_len)
2396 const struct GNUNET_MessageHeader *ack;
2397 const struct UDP_ACK_Message *udp_ack;
2398 struct GNUNET_HELLO_Address *address;
2399 struct GNUNET_ATS_Session *s;
2400 struct GNUNET_TIME_Relative flow_delay;
2402 /* check message format */
2403 if (ntohs (msg->size)
2404 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2406 GNUNET_break_op (0);
2409 udp_ack = (const struct UDP_ACK_Message *) msg;
2410 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2411 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2417 /* Locate session */
2418 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2422 GNUNET_HELLO_ADDRESS_INFO_NONE);
2423 s = udp_plugin_lookup_session (plugin,
2427 LOG (GNUNET_ERROR_TYPE_WARNING,
2428 "UDP session of address %s for ACK not found\n",
2429 udp_address_to_string (plugin,
2431 address->address_length));
2432 GNUNET_HELLO_address_free (address);
2435 if (NULL == s->frag_ctx)
2437 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2438 "Fragmentation context of address %s for ACK (%s) not found\n",
2439 udp_address_to_string (plugin,
2441 address->address_length),
2442 GNUNET_FRAGMENT_print_ack (ack));
2443 GNUNET_HELLO_address_free (address);
2446 GNUNET_HELLO_address_free (address);
2448 /* evaluate flow delay: how long should we wait between messages? */
2449 if (UINT32_MAX == ntohl (udp_ack->delay))
2451 /* Other peer asked for us to terminate the session */
2452 LOG (GNUNET_ERROR_TYPE_INFO,
2453 "Asked to disconnect UDP session of %s\n",
2454 GNUNET_i2s (&udp_ack->sender));
2455 udp_disconnect_session (plugin,
2459 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2460 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2461 LOG (GNUNET_ERROR_TYPE_WARNING,
2462 "We received a sending delay of %s for %s\n",
2463 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2465 GNUNET_i2s (&udp_ack->sender));
2467 LOG (GNUNET_ERROR_TYPE_DEBUG,
2468 "We received a sending delay of %s for %s\n",
2469 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2471 GNUNET_i2s (&udp_ack->sender));
2472 /* Flow delay is for the reassembled packet, however, our delay
2473 is per packet, so we need to adjust: */
2474 s->flow_delay_from_other_peer = flow_delay;
2478 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2481 LOG (GNUNET_ERROR_TYPE_DEBUG,
2482 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2483 (unsigned int) ntohs (msg->size),
2484 GNUNET_i2s (&udp_ack->sender),
2485 udp_address_to_string (plugin,
2488 /* Expect more ACKs to arrive */
2492 /* Remove fragmented message after successful sending */
2493 LOG (GNUNET_ERROR_TYPE_DEBUG,
2494 "Message from %s at %s full ACK'ed\n",
2495 GNUNET_i2s (&udp_ack->sender),
2496 udp_address_to_string (plugin,
2499 fragmented_message_done (s->frag_ctx,
2505 * Message tokenizer has broken up an incomming message. Pass it on
2508 * @param cls the `struct GNUNET_ATS_Session *`
2509 * @param hdr the actual message
2510 * @return #GNUNET_OK (always)
2513 process_inbound_tokenized_messages (void *cls,
2514 const struct GNUNET_MessageHeader *hdr)
2516 struct GNUNET_ATS_Session *session = cls;
2517 struct Plugin *plugin = session->plugin;
2519 if (GNUNET_YES == session->in_destroy)
2521 reschedule_session_timeout (session);
2522 session->flow_delay_for_other_peer
2523 = plugin->env->receive (plugin->env->cls,
2532 * Destroy a session, plugin is being unloaded.
2534 * @param cls the `struct Plugin`
2535 * @param key hash of public key of target peer
2536 * @param value a `struct PeerSession *` to clean up
2537 * @return #GNUNET_OK (continue to iterate)
2540 disconnect_and_free_it (void *cls,
2541 const struct GNUNET_PeerIdentity *key,
2544 struct Plugin *plugin = cls;
2546 udp_disconnect_session (plugin,
2553 * Disconnect from a remote node. Clean up session if we have one for
2556 * @param cls closure for this call (should be handle to Plugin)
2557 * @param target the peeridentity of the peer to disconnect
2558 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2561 udp_disconnect (void *cls,
2562 const struct GNUNET_PeerIdentity *target)
2564 struct Plugin *plugin = cls;
2566 LOG (GNUNET_ERROR_TYPE_DEBUG,
2567 "Disconnecting from peer `%s'\n",
2568 GNUNET_i2s (target));
2569 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2571 &disconnect_and_free_it,
2577 * Session was idle, so disconnect it.
2579 * @param cls the `struct GNUNET_ATS_Session` to time out
2582 session_timeout (void *cls)
2584 struct GNUNET_ATS_Session *s = cls;
2585 struct Plugin *plugin = s->plugin;
2586 struct GNUNET_TIME_Relative left;
2588 s->timeout_task = NULL;
2589 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2590 if (left.rel_value_us > 0)
2592 /* not actually our turn yet, but let's at least update
2593 the monitor, it may think we're about to die ... */
2594 notify_session_monitor (s->plugin,
2596 GNUNET_TRANSPORT_SS_UPDATE);
2597 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2602 LOG (GNUNET_ERROR_TYPE_DEBUG,
2603 "Session %p was idle for %s, disconnecting\n",
2605 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2607 /* call session destroy function */
2608 udp_disconnect_session (plugin,
2614 * Allocate a new session for the given endpoint address.
2615 * Note that this function does not inform the service
2616 * of the new session, this is the responsibility of the
2617 * caller (if needed).
2619 * @param cls the `struct Plugin`
2620 * @param address address of the other peer to use
2621 * @param network_type network type the address belongs to
2622 * @return NULL on error, otherwise session handle
2624 static struct GNUNET_ATS_Session *
2625 udp_plugin_create_session (void *cls,
2626 const struct GNUNET_HELLO_Address *address,
2627 enum GNUNET_ATS_Network_Type network_type)
2629 struct Plugin *plugin = cls;
2630 struct GNUNET_ATS_Session *s;
2632 s = GNUNET_new (struct GNUNET_ATS_Session);
2633 s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
2636 s->address = GNUNET_HELLO_address_copy (address);
2637 s->target = address->peer;
2638 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2639 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2641 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2642 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2643 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2644 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2645 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2648 s->scope = network_type;
2650 LOG (GNUNET_ERROR_TYPE_DEBUG,
2651 "Creating new session %p for peer `%s' address `%s'\n",
2653 GNUNET_i2s (&address->peer),
2654 udp_address_to_string (plugin,
2656 address->address_length));
2657 GNUNET_assert (GNUNET_OK ==
2658 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2662 GNUNET_STATISTICS_set (plugin->env->stats,
2663 "# UDP sessions active",
2664 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2666 notify_session_monitor (plugin,
2668 GNUNET_TRANSPORT_SS_INIT);
2674 * Creates a new outbound session the transport service will use to
2675 * send data to the peer.
2677 * @param cls the `struct Plugin *`
2678 * @param address the address
2679 * @return the session or NULL of max connections exceeded
2681 static struct GNUNET_ATS_Session *
2682 udp_plugin_get_session (void *cls,
2683 const struct GNUNET_HELLO_Address *address)
2685 struct Plugin *plugin = cls;
2686 struct GNUNET_ATS_Session *s;
2687 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2688 const struct IPv4UdpAddress *udp_v4;
2689 const struct IPv6UdpAddress *udp_v6;
2691 if (NULL == address)
2696 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2697 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2699 GNUNET_break_op (0);
2702 if (NULL != (s = udp_plugin_lookup_session (cls,
2706 /* need to create new session */
2707 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2709 struct sockaddr_in v4;
2711 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2712 memset (&v4, '\0', sizeof (v4));
2713 v4.sin_family = AF_INET;
2714 #if HAVE_SOCKADDR_IN_SIN_LEN
2715 v4.sin_len = sizeof (struct sockaddr_in);
2717 v4.sin_port = udp_v4->u4_port;
2718 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2719 network_type = plugin->env->get_address_type (plugin->env->cls,
2720 (const struct sockaddr *) &v4,
2723 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2725 struct sockaddr_in6 v6;
2727 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2728 memset (&v6, '\0', sizeof (v6));
2729 v6.sin6_family = AF_INET6;
2730 #if HAVE_SOCKADDR_IN_SIN_LEN
2731 v6.sin6_len = sizeof (struct sockaddr_in6);
2733 v6.sin6_port = udp_v6->u6_port;
2734 v6.sin6_addr = udp_v6->ipv6_addr;
2735 network_type = plugin->env->get_address_type (plugin->env->cls,
2736 (const struct sockaddr *) &v6,
2739 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2740 return udp_plugin_create_session (cls,
2747 * We've received a UDP Message. Process it (pass contents to main service).
2749 * @param plugin plugin context
2750 * @param msg the message
2751 * @param udp_addr sender address
2752 * @param udp_addr_len number of bytes in @a udp_addr
2753 * @param network_type network type the address belongs to
2756 process_udp_message (struct Plugin *plugin,
2757 const struct UDPMessage *msg,
2758 const union UdpAddress *udp_addr,
2759 size_t udp_addr_len,
2760 enum GNUNET_ATS_Network_Type network_type)
2762 struct GNUNET_ATS_Session *s;
2763 struct GNUNET_HELLO_Address *address;
2765 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2766 if (0 != ntohl (msg->reserved))
2771 if (ntohs (msg->header.size)
2772 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2778 address = GNUNET_HELLO_address_allocate (&msg->sender,
2782 GNUNET_HELLO_ADDRESS_INFO_NONE);
2784 (s = udp_plugin_lookup_session (plugin,
2787 s = udp_plugin_create_session (plugin,
2790 plugin->env->session_start (plugin->env->cls,
2794 notify_session_monitor (plugin,
2796 GNUNET_TRANSPORT_SS_UP);
2798 GNUNET_free (address);
2801 GNUNET_MST_from_buffer (s->mst,
2802 (const char *) &msg[1],
2803 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2807 if ( (0 == s->rc) &&
2808 (GNUNET_YES == s->in_destroy) )
2814 * Process a defragmented message.
2816 * @param cls the `struct DefragContext *`
2817 * @param msg the message
2820 fragment_msg_proc (void *cls,
2821 const struct GNUNET_MessageHeader *msg)
2823 struct DefragContext *dc = cls;
2824 const struct UDPMessage *um;
2826 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2828 GNUNET_break_op (0);
2831 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2833 GNUNET_break_op (0);
2836 um = (const struct UDPMessage *) msg;
2837 dc->sender = um->sender;
2838 dc->have_sender = GNUNET_YES;
2839 process_udp_message (dc->plugin,
2848 * We finished sending an acknowledgement. Update
2851 * @param cls the `struct Plugin`
2852 * @param udpw message queue entry of the ACK
2853 * @param result #GNUNET_OK if the transmission worked,
2854 * #GNUNET_SYSERR if we failed to send the ACK
2857 ack_message_sent (void *cls,
2858 struct UDP_MessageWrapper *udpw,
2861 struct Plugin *plugin = cls;
2863 if (GNUNET_OK == result)
2865 GNUNET_STATISTICS_update (plugin->env->stats,
2866 "# UDP, ACK messages sent",
2872 GNUNET_STATISTICS_update (plugin->env->stats,
2873 "# UDP, ACK transmissions failed",
2881 * Transmit an acknowledgement.
2883 * @param cls the `struct DefragContext *`
2884 * @param id message ID (unused)
2885 * @param msg ack to transmit
2888 ack_proc (void *cls,
2890 const struct GNUNET_MessageHeader *msg)
2892 struct DefragContext *rc = cls;
2893 struct Plugin *plugin = rc->plugin;
2894 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2895 struct UDP_ACK_Message *udp_ack;
2897 struct UDP_MessageWrapper *udpw;
2898 struct GNUNET_ATS_Session *s;
2899 struct GNUNET_HELLO_Address *address;
2901 if (GNUNET_NO == rc->have_sender)
2903 /* tried to defragment but never succeeded, hence will not ACK */
2904 /* This can happen if we just lost msgs */
2905 GNUNET_STATISTICS_update (plugin->env->stats,
2906 "# UDP, fragments discarded without ACK",
2911 address = GNUNET_HELLO_address_allocate (&rc->sender,
2915 GNUNET_HELLO_ADDRESS_INFO_NONE);
2916 s = udp_plugin_lookup_session (plugin,
2918 GNUNET_HELLO_address_free (address);
2921 LOG (GNUNET_ERROR_TYPE_ERROR,
2922 "Trying to transmit ACK to peer `%s' but no session found!\n",
2923 udp_address_to_string (plugin,
2926 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2927 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2929 GNUNET_STATISTICS_update (plugin->env->stats,
2930 "# UDP, ACK transmissions failed",
2935 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2936 s->flow_delay_for_other_peer.rel_value_us)
2938 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2939 delay = s->flow_delay_for_other_peer.rel_value_us;
2941 delay = UINT32_MAX - 1; /* largest value we can communicate */
2942 LOG (GNUNET_ERROR_TYPE_DEBUG,
2943 "Sending ACK to `%s' including delay of %s\n",
2944 udp_address_to_string (plugin,
2947 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2949 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2950 udpw->msg_size = msize;
2951 udpw->payload_size = 0;
2953 udpw->start_time = GNUNET_TIME_absolute_get ();
2954 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2955 udpw->msg_buf = (char *) &udpw[1];
2956 udpw->qc = &ack_message_sent;
2957 udpw->qc_cls = plugin;
2958 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2959 udp_ack->header.size = htons ((uint16_t) msize);
2960 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2961 udp_ack->delay = htonl (delay);
2962 udp_ack->sender = *plugin->env->my_identity;
2963 GNUNET_memcpy (&udp_ack[1],
2968 notify_session_monitor (plugin,
2970 GNUNET_TRANSPORT_SS_UPDATE);
2971 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2972 schedule_select_v4 (plugin);
2974 schedule_select_v6 (plugin);
2979 * We received a fragment, process it.
2981 * @param plugin our plugin
2982 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2983 * @param udp_addr sender address
2984 * @param udp_addr_len number of bytes in @a udp_addr
2985 * @param network_type network type the address belongs to
2988 read_process_fragment (struct Plugin *plugin,
2989 const struct GNUNET_MessageHeader *msg,
2990 const union UdpAddress *udp_addr,
2991 size_t udp_addr_len,
2992 enum GNUNET_ATS_Network_Type network_type)
2994 struct DefragContext *d_ctx;
2995 struct GNUNET_TIME_Absolute now;
2996 struct FindReceiveContext frc;
2999 frc.udp_addr = udp_addr;
3000 frc.udp_addr_len = udp_addr_len;
3002 /* Lookup existing receive context for this address */
3003 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3004 &find_receive_context,
3006 now = GNUNET_TIME_absolute_get ();
3011 /* Create a new defragmentation context */
3012 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
3013 GNUNET_memcpy (&d_ctx[1],
3016 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
3017 d_ctx->udp_addr_len = udp_addr_len;
3018 d_ctx->network_type = network_type;
3019 d_ctx->plugin = plugin;
3020 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
3022 UDP_MAX_MESSAGES_IN_DEFRAG,
3026 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
3028 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3029 LOG (GNUNET_ERROR_TYPE_DEBUG,
3030 "Created new defragmentation context for %u-byte fragment from `%s'\n",
3031 (unsigned int) ntohs (msg->size),
3032 udp_address_to_string (plugin,
3038 LOG (GNUNET_ERROR_TYPE_DEBUG,
3039 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
3040 (unsigned int) ntohs (msg->size),
3041 udp_address_to_string (plugin,
3047 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
3050 /* keep this 'rc' from expiring */
3051 GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
3052 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3054 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
3055 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
3057 /* remove 'rc' that was inactive the longest */
3058 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
3059 GNUNET_assert (NULL != d_ctx);
3060 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3061 GNUNET_free (d_ctx);
3062 GNUNET_STATISTICS_update (plugin->env->stats,
3063 "# UDP, Defragmentations aborted",
3071 * Read and process a message from the given socket.
3073 * @param plugin the overall plugin
3074 * @param rsock socket to read from
3077 udp_select_read (struct Plugin *plugin,
3078 struct GNUNET_NETWORK_Handle *rsock)
3081 struct sockaddr_storage addr;
3082 char buf[65536] GNUNET_ALIGN;
3084 const struct GNUNET_MessageHeader *msg;
3085 struct IPv4UdpAddress v4;
3086 struct IPv6UdpAddress v6;
3087 const struct sockaddr *sa;
3088 const struct sockaddr_in *sa4;
3089 const struct sockaddr_in6 *sa6;
3090 const union UdpAddress *int_addr;
3091 size_t int_addr_len;
3092 enum GNUNET_ATS_Network_Type network_type;
3094 fromlen = sizeof (addr);
3098 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3101 (struct sockaddr *) &addr,
3103 sa = (const struct sockaddr *) &addr;
3105 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3106 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3107 * on this socket has failed.
3109 * WSAECONNRESET - The virtual circuit was reset by the remote side
3110 * executing a hard or abortive close. The application should close
3111 * the socket; it is no longer usable. On a UDP-datagram socket this
3112 * error indicates a previous send operation resulted in an ICMP Port
3113 * Unreachable message.
3115 if ( (-1 == size) &&
3116 (ECONNRESET == errno) )
3121 LOG (GNUNET_ERROR_TYPE_DEBUG,
3122 "UDP failed to receive data: %s\n",
3124 /* Connection failure or something. Not a protocol violation. */
3128 /* Check if this is a STUN packet */
3130 GNUNET_NAT_stun_handle_packet (plugin->nat,
3131 (const struct sockaddr *) &addr,
3135 return; /* was STUN, do not process further */
3137 if (size < sizeof(struct GNUNET_MessageHeader))
3139 LOG (GNUNET_ERROR_TYPE_WARNING,
3140 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3141 (unsigned int ) size,
3144 /* _MAY_ be a connection failure (got partial message) */
3145 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3146 GNUNET_break_op (0);
3150 msg = (const struct GNUNET_MessageHeader *) buf;
3151 LOG (GNUNET_ERROR_TYPE_DEBUG,
3152 "UDP received %u-byte message from `%s' type %u\n",
3153 (unsigned int) size,
3157 if (size != ntohs (msg->size))
3159 LOG (GNUNET_ERROR_TYPE_WARNING,
3160 "UDP malformed message (size %u) header from %s\n",
3161 (unsigned int) size,
3164 GNUNET_break_op (0);
3167 GNUNET_STATISTICS_update (plugin->env->stats,
3168 "# UDP, total bytes received",
3171 network_type = plugin->env->get_address_type (plugin->env->cls,
3174 switch (sa->sa_family)
3177 sa4 = (const struct sockaddr_in *) &addr;
3179 v4.ipv4_addr = sa4->sin_addr.s_addr;
3180 v4.u4_port = sa4->sin_port;
3181 int_addr = (union UdpAddress *) &v4;
3182 int_addr_len = sizeof (v4);
3185 sa6 = (const struct sockaddr_in6 *) &addr;
3187 v6.ipv6_addr = sa6->sin6_addr;
3188 v6.u6_port = sa6->sin6_port;
3189 int_addr = (union UdpAddress *) &v6;
3190 int_addr_len = sizeof (v6);
3197 switch (ntohs (msg->type))
3199 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3200 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3201 udp_broadcast_receive (plugin,
3208 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3209 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3214 process_udp_message (plugin,
3215 (const struct UDPMessage *) msg,
3220 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3221 read_process_ack (plugin,
3226 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3227 read_process_fragment (plugin,
3241 * Removes messages from the transmission queue that have
3242 * timed out, and then selects a message that should be
3245 * @param plugin the UDP plugin
3246 * @param sock which socket should we process the queue for (v4 or v6)
3247 * @return message selected for transmission, or NULL for none
3249 static struct UDP_MessageWrapper *
3250 remove_timeout_messages_and_select (struct Plugin *plugin,
3251 struct GNUNET_NETWORK_Handle *sock)
3253 struct UDP_MessageWrapper *udpw;
3254 struct GNUNET_TIME_Relative remaining;
3255 struct GNUNET_ATS_Session *session;
3258 removed = GNUNET_NO;
3259 udpw = (sock == plugin->sockv4)
3260 ? plugin->ipv4_queue_head
3261 : plugin->ipv6_queue_head;
3262 while (NULL != udpw)
3264 session = udpw->session;
3265 /* Find messages with timeout */
3266 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3267 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3269 /* Message timed out */
3270 removed = GNUNET_YES;
3273 udpw->qc (udpw->qc_cls,
3278 if (sock == plugin->sockv4)
3280 udpw = plugin->ipv4_queue_head;
3282 else if (sock == plugin->sockv6)
3284 udpw = plugin->ipv6_queue_head;
3288 GNUNET_break (0); /* should never happen */
3291 GNUNET_STATISTICS_update (plugin->env->stats,
3292 "# messages discarded due to timeout",
3298 /* Message did not time out, check transmission time */
3299 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3300 if (0 == remaining.rel_value_us)
3302 /* this message is not delayed */
3303 LOG (GNUNET_ERROR_TYPE_DEBUG,
3304 "Message for peer `%s' (%u bytes) is not delayed \n",
3305 GNUNET_i2s (&udpw->session->target),
3306 udpw->payload_size);
3307 break; /* Found message to send, break */
3311 /* Message is delayed, try next */
3312 LOG (GNUNET_ERROR_TYPE_DEBUG,
3313 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3314 GNUNET_i2s (&udpw->session->target),
3316 GNUNET_STRINGS_relative_time_to_string (remaining,
3322 if (GNUNET_YES == removed)
3323 notify_session_monitor (session->plugin,
3325 GNUNET_TRANSPORT_SS_UPDATE);
3331 * We failed to transmit a message via UDP. Generate
3332 * a descriptive error message.
3334 * @param plugin our plugin
3335 * @param sa target address we were trying to reach
3336 * @param slen number of bytes in @a sa
3337 * @param error the errno value returned from the sendto() call
3340 analyze_send_error (struct Plugin *plugin,
3341 const struct sockaddr *sa,
3345 enum GNUNET_ATS_Network_Type type;
3347 type = plugin->env->get_address_type (plugin->env->cls,
3350 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3351 (GNUNET_ATS_NET_WAN == type) ) &&
3352 ( (ENETUNREACH == errno) ||
3353 (ENETDOWN == errno) ) )
3355 if (slen == sizeof (struct sockaddr_in))
3357 /* IPv4: "Network unreachable" or "Network down"
3359 * This indicates we do not have connectivity
3361 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3362 _("UDP could not transmit message to `%s': "
3363 "Network seems down, please check your network configuration\n"),
3367 if (slen == sizeof (struct sockaddr_in6))
3369 /* IPv6: "Network unreachable" or "Network down"
3371 * This indicates that this system is IPv6 enabled, but does not
3372 * have a valid global IPv6 address assigned or we do not have
3375 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3376 _("UDP could not transmit IPv6 message! "
3377 "Please check your network configuration and disable IPv6 if your "
3378 "connection does not have a global IPv6 address\n"));
3383 LOG (GNUNET_ERROR_TYPE_WARNING,
3384 "UDP could not transmit message to `%s': `%s'\n",
3393 * It is time to try to transmit a UDP message. Select one
3396 * @param plugin the plugin
3397 * @param sock which socket (v4/v6) to send on
3400 udp_select_send (struct Plugin *plugin,
3401 struct GNUNET_NETWORK_Handle *sock)
3405 const struct sockaddr *a;
3406 const struct IPv4UdpAddress *u4;
3407 struct sockaddr_in a4;
3408 const struct IPv6UdpAddress *u6;
3409 struct sockaddr_in6 a6;
3410 struct UDP_MessageWrapper *udpw;
3412 /* Find message(s) to send */
3413 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3416 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3418 u4 = udpw->session->address->address;
3422 a4.sin_family = AF_INET;
3423 #if HAVE_SOCKADDR_IN_SIN_LEN
3424 a4.sin_len = sizeof (a4);
3426 a4.sin_port = u4->u4_port;
3427 a4.sin_addr.s_addr = u4->ipv4_addr;
3428 a = (const struct sockaddr *) &a4;
3431 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3433 u6 = udpw->session->address->address;
3437 a6.sin6_family = AF_INET6;
3438 #if HAVE_SOCKADDR_IN_SIN_LEN
3439 a6.sin6_len = sizeof (a6);
3441 a6.sin6_port = u6->u6_port;
3442 a6.sin6_addr = u6->ipv6_addr;
3443 a = (const struct sockaddr *) &a6;
3451 udpw->qc (udpw->qc_cls,
3454 notify_session_monitor (plugin,
3456 GNUNET_TRANSPORT_SS_UPDATE);
3460 sent = GNUNET_NETWORK_socket_sendto (sock,
3465 udpw->session->last_transmit_time
3466 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3467 udpw->session->last_transmit_time);
3470 if (GNUNET_SYSERR == sent)
3473 analyze_send_error (plugin,
3477 udpw->qc (udpw->qc_cls,
3480 GNUNET_STATISTICS_update (plugin->env->stats,
3481 "# UDP, total, bytes, sent, failure",
3484 GNUNET_STATISTICS_update (plugin->env->stats,
3485 "# UDP, total, messages, sent, failure",
3492 LOG (GNUNET_ERROR_TYPE_DEBUG,
3493 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3494 (unsigned int) (udpw->msg_size),
3495 GNUNET_i2s (&udpw->session->target),
3499 (sent < 0) ? STRERROR (errno) : "ok");
3500 GNUNET_STATISTICS_update (plugin->env->stats,
3501 "# UDP, total, bytes, sent, success",
3504 GNUNET_STATISTICS_update (plugin->env->stats,
3505 "# UDP, total, messages, sent, success",
3508 if (NULL != udpw->frag_ctx)
3509 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3510 udpw->qc (udpw->qc_cls,
3514 notify_session_monitor (plugin,
3516 GNUNET_TRANSPORT_SS_UPDATE);
3522 /* ***************** Event loop (part 2) *************** */
3526 * We have been notified that our readset has something to read. We don't
3527 * know which socket needs to be read, so we have to check each one
3528 * Then reschedule this function to be called again once more is available.
3530 * @param cls the plugin handle
3533 udp_plugin_select_v4 (void *cls)
3535 struct Plugin *plugin = cls;
3536 const struct GNUNET_SCHEDULER_TaskContext *tc;
3538 plugin->select_task_v4 = NULL;
3539 if (NULL == plugin->sockv4)
3541 tc = GNUNET_SCHEDULER_get_task_context ();
3542 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3543 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3545 udp_select_read (plugin,
3547 udp_select_send (plugin,
3549 schedule_select_v4 (plugin);
3554 * We have been notified that our readset has something to read. We don't
3555 * know which socket needs to be read, so we have to check each one
3556 * Then reschedule this function to be called again once more is available.
3558 * @param cls the plugin handle
3561 udp_plugin_select_v6 (void *cls)
3563 struct Plugin *plugin = cls;
3564 const struct GNUNET_SCHEDULER_TaskContext *tc;
3566 plugin->select_task_v6 = NULL;
3567 if (NULL == plugin->sockv6)
3569 tc = GNUNET_SCHEDULER_get_task_context ();
3570 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3571 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3573 udp_select_read (plugin,
3576 udp_select_send (plugin,
3578 schedule_select_v6 (plugin);
3582 /* ******************* Initialization *************** */
3586 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3588 * @param plugin the plugin to initialize
3589 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3590 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3591 * @return number of sockets that were successfully bound
3594 setup_sockets (struct Plugin *plugin,
3595 const struct sockaddr_in6 *bind_v6,
3596 const struct sockaddr_in *bind_v4)
3599 unsigned int sockets_created = 0;
3600 struct sockaddr_in6 server_addrv6;
3601 struct sockaddr_in server_addrv4;
3602 const struct sockaddr *server_addr;
3603 const struct sockaddr *addrs[2];
3604 socklen_t addrlens[2];
3608 /* Create IPv6 socket */
3610 if (GNUNET_YES == plugin->enable_ipv6)
3612 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3615 if (NULL == plugin->sockv6)
3617 LOG (GNUNET_ERROR_TYPE_INFO,
3618 _("Disabling IPv6 since it is not supported on this system!\n"));
3619 plugin->enable_ipv6 = GNUNET_NO;
3623 memset (&server_addrv6,
3625 sizeof(struct sockaddr_in6));
3626 #if HAVE_SOCKADDR_IN_SIN_LEN
3627 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3629 server_addrv6.sin6_family = AF_INET6;
3630 if (NULL != bind_v6)
3631 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3633 server_addrv6.sin6_addr = in6addr_any;
3635 if (0 == plugin->port) /* autodetect */
3636 server_addrv6.sin6_port
3637 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3641 server_addrv6.sin6_port = htons (plugin->port);
3642 addrlen = sizeof (struct sockaddr_in6);
3643 server_addr = (const struct sockaddr *) &server_addrv6;
3648 LOG(GNUNET_ERROR_TYPE_DEBUG,
3649 "Binding to IPv6 `%s'\n",
3650 GNUNET_a2s (server_addr,
3654 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3659 if (0 != plugin->port)
3661 tries = 10; /* fail immediately */
3662 break; /* bind failed on specific port */
3665 server_addrv6.sin6_port
3666 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3673 GNUNET_NETWORK_socket_close (plugin->sockv6);
3674 plugin->enable_ipv6 = GNUNET_NO;
3675 plugin->sockv6 = NULL;
3679 plugin->port = ntohs (server_addrv6.sin6_port);
3681 if (NULL != plugin->sockv6)
3683 LOG (GNUNET_ERROR_TYPE_DEBUG,
3684 "IPv6 UDP socket created listinging at %s\n",
3685 GNUNET_a2s (server_addr,
3687 addrs[sockets_created] = server_addr;
3688 addrlens[sockets_created] = addrlen;
3693 LOG (GNUNET_ERROR_TYPE_WARNING,
3694 _("Failed to bind UDP socket to %s: %s\n"),
3695 GNUNET_a2s (server_addr,
3702 /* Create IPv4 socket */
3704 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3707 if (NULL == plugin->sockv4)
3709 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3711 LOG (GNUNET_ERROR_TYPE_INFO,
3712 _("Disabling IPv4 since it is not supported on this system!\n"));
3713 plugin->enable_ipv4 = GNUNET_NO;
3717 memset (&server_addrv4,
3719 sizeof(struct sockaddr_in));
3720 #if HAVE_SOCKADDR_IN_SIN_LEN
3721 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3723 server_addrv4.sin_family = AF_INET;
3724 if (NULL != bind_v4)
3725 server_addrv4.sin_addr = bind_v4->sin_addr;
3727 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3729 if (0 == plugin->port)
3731 server_addrv4.sin_port
3732 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3736 server_addrv4.sin_port = htons (plugin->port);
3738 addrlen = sizeof (struct sockaddr_in);
3739 server_addr = (const struct sockaddr *) &server_addrv4;
3744 LOG (GNUNET_ERROR_TYPE_DEBUG,
3745 "Binding to IPv4 `%s'\n",
3746 GNUNET_a2s (server_addr,
3751 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3756 if (0 != plugin->port)
3758 tries = 10; /* fail */
3759 break; /* bind failed on specific port */
3763 server_addrv4.sin_port
3764 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3771 GNUNET_NETWORK_socket_close (plugin->sockv4);
3772 plugin->enable_ipv4 = GNUNET_NO;
3773 plugin->sockv4 = NULL;
3777 plugin->port = ntohs (server_addrv4.sin_port);
3780 if (NULL != plugin->sockv4)
3782 LOG (GNUNET_ERROR_TYPE_DEBUG,
3783 "IPv4 socket created on port %s\n",
3784 GNUNET_a2s (server_addr,
3786 addrs[sockets_created] = server_addr;
3787 addrlens[sockets_created] = addrlen;
3792 LOG (GNUNET_ERROR_TYPE_ERROR,
3793 _("Failed to bind UDP socket to %s: %s\n"),
3794 GNUNET_a2s (server_addr,
3800 if (0 == sockets_created)
3802 LOG (GNUNET_ERROR_TYPE_WARNING,
3803 _("Failed to open UDP sockets\n"));
3804 return 0; /* No sockets created, return */
3806 schedule_select_v4 (plugin);
3807 schedule_select_v6 (plugin);
3808 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3814 &udp_nat_port_map_callback,
3817 return sockets_created;
3822 * The exported method. Makes the core api available via a global and
3823 * returns the udp transport API.
3825 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3826 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3829 libgnunet_plugin_transport_udp_init (void *cls)
3831 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3832 struct GNUNET_TRANSPORT_PluginFunctions *api;
3834 unsigned long long port;
3835 unsigned long long aport;
3836 unsigned long long udp_max_bps;
3838 int enable_broadcasting;
3839 int enable_broadcasting_recv;
3840 char *bind4_address;
3841 char *bind6_address;
3842 struct GNUNET_TIME_Relative interval;
3843 struct sockaddr_in server_addrv4;
3844 struct sockaddr_in6 server_addrv6;
3849 if (NULL == env->receive)
3851 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3852 initialze the plugin or the API */
3853 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3855 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3856 api->address_to_string = &udp_address_to_string;
3857 api->string_to_address = &udp_string_to_address;
3861 /* Get port number: port == 0 : autodetect a port,
3862 * > 0 : use this port, not given : 2086 default */
3864 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3871 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3874 _("must be in [0,65535]"));
3878 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3885 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3888 _("must be in [0,65535]"));
3893 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3896 enable_v6 = GNUNET_NO;
3898 enable_v6 = GNUNET_YES;
3900 have_bind4 = GNUNET_NO;
3901 memset (&server_addrv4,
3903 sizeof (server_addrv4));
3905 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3910 LOG (GNUNET_ERROR_TYPE_DEBUG,
3911 "Binding UDP plugin to specific address: `%s'\n",
3913 if (1 != inet_pton (AF_INET,
3915 &server_addrv4.sin_addr))
3917 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3920 _("must be valid IPv4 address"));
3921 GNUNET_free (bind4_address);
3924 have_bind4 = GNUNET_YES;
3926 GNUNET_free_non_null (bind4_address);
3927 have_bind6 = GNUNET_NO;
3928 memset (&server_addrv6,
3930 sizeof (server_addrv6));
3932 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3937 LOG (GNUNET_ERROR_TYPE_DEBUG,
3938 "Binding udp plugin to specific address: `%s'\n",
3940 if (1 != inet_pton (AF_INET6,
3942 &server_addrv6.sin6_addr))
3944 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3947 _("must be valid IPv6 address"));
3948 GNUNET_free (bind6_address);
3951 have_bind6 = GNUNET_YES;
3953 GNUNET_free_non_null (bind6_address);
3955 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3958 if (enable_broadcasting == GNUNET_SYSERR)
3959 enable_broadcasting = GNUNET_NO;
3961 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3963 "BROADCAST_RECEIVE");
3964 if (enable_broadcasting_recv == GNUNET_SYSERR)
3965 enable_broadcasting_recv = GNUNET_YES;
3967 if (GNUNET_SYSERR ==
3968 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3970 "BROADCAST_INTERVAL",
3973 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3977 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3982 /* 50 MB/s == infinity for practical purposes */
3983 udp_max_bps = 1024 * 1024 * 50;
3986 p = GNUNET_new (struct Plugin);
3989 p->broadcast_interval = interval;
3990 p->enable_ipv6 = enable_v6;
3991 p->enable_ipv4 = GNUNET_YES; /* default */
3992 p->enable_broadcasting = enable_broadcasting;
3993 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3995 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3997 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3998 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
4001 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
4003 res = setup_sockets (p,
4004 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
4005 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
4007 ( (NULL == p->sockv4) &&
4008 (NULL == p->sockv6) ) )
4010 LOG (GNUNET_ERROR_TYPE_ERROR,
4011 _("Failed to create UDP network sockets\n"));
4012 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
4013 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
4015 GNUNET_NAT_unregister (p->nat);
4020 /* Setup broadcasting and receiving beacons */
4025 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
4027 api->disconnect_session = &udp_disconnect_session;
4028 api->query_keepalive_factor = &udp_query_keepalive_factor;
4029 api->disconnect_peer = &udp_disconnect;
4030 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
4031 api->address_to_string = &udp_address_to_string;
4032 api->string_to_address = &udp_string_to_address;
4033 api->check_address = &udp_plugin_check_address;
4034 api->get_session = &udp_plugin_get_session;
4035 api->send = &udp_plugin_send;
4036 api->get_network = &udp_plugin_get_network;
4037 api->get_network_for_address = &udp_plugin_get_network_for_address;
4038 api->update_session_timeout = &udp_plugin_update_session_timeout;
4039 api->setup_monitor = &udp_plugin_setup_monitor;
4045 * Function called on each entry in the defragmentation heap to
4049 * @param node node in the heap (to be removed)
4050 * @param element a `struct DefragContext` to be cleaned up
4051 * @param cost unused
4052 * @return #GNUNET_YES
4055 heap_cleanup_iterator (void *cls,
4056 struct GNUNET_CONTAINER_HeapNode *node,
4058 GNUNET_CONTAINER_HeapCostType cost)
4060 struct DefragContext *d_ctx = element;
4062 GNUNET_CONTAINER_heap_remove_node (node);
4063 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4064 GNUNET_free (d_ctx);
4070 * The exported method. Makes the core api available via a global and
4071 * returns the udp transport API.
4073 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4077 libgnunet_plugin_transport_udp_done (void *cls)
4079 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4080 struct Plugin *plugin = api->cls;
4081 struct PrettyPrinterContext *cur;
4082 struct UDP_MessageWrapper *udpw;
4089 stop_broadcast (plugin);
4090 if (NULL != plugin->select_task_v4)
4092 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4093 plugin->select_task_v4 = NULL;
4095 if (NULL != plugin->select_task_v6)
4097 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4098 plugin->select_task_v6 = NULL;
4100 if (NULL != plugin->sockv4)
4102 GNUNET_break (GNUNET_OK ==
4103 GNUNET_NETWORK_socket_close (plugin->sockv4));
4104 plugin->sockv4 = NULL;
4106 if (NULL != plugin->sockv6)
4108 GNUNET_break (GNUNET_OK ==
4109 GNUNET_NETWORK_socket_close (plugin->sockv6));
4110 plugin->sockv6 = NULL;
4112 if (NULL != plugin->nat)
4114 GNUNET_NAT_unregister (plugin->nat);
4117 if (NULL != plugin->defrag_ctxs)
4119 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4120 &heap_cleanup_iterator,
4122 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4123 plugin->defrag_ctxs = NULL;
4125 while (NULL != (udpw = plugin->ipv4_queue_head))
4129 udpw->qc (udpw->qc_cls,
4134 while (NULL != (udpw = plugin->ipv6_queue_head))
4138 udpw->qc (udpw->qc_cls,
4143 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4144 &disconnect_and_free_it,
4146 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4148 while (NULL != (cur = plugin->ppc_dll_head))
4151 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4152 plugin->ppc_dll_tail,
4154 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4155 if (NULL != cur->timeout_task)
4157 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4158 cur->timeout_task = NULL;
4162 GNUNET_free (plugin);
4167 /* end of plugin_transport_udp.c */