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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_service.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 \
49 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
52 * Number of messages we can defragment in parallel. We only really
53 * defragment 1 message at a time, but if messages get re-ordered, we
54 * may want to keep knowledge about the previous message to avoid
55 * discarding the current message in favor of a single fragment of a
56 * previous message. 3 should be good since we don't expect massive
57 * message reorderings with UDP.
59 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
62 * We keep a defragmentation queue per sender address. How many
63 * sender addresses do we support at the same time? Memory consumption
64 * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
65 * value. (So 128 corresponds to 12 MB and should suffice for
66 * connecting to roughly 128 peers via UDP).
68 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
72 * UDP Message-Packet header (after defragmentation).
79 struct GNUNET_MessageHeader header;
82 * Always zero for now.
87 * What is the identity of the sender
89 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.
151 * Session with another peer.
153 struct GNUNET_ATS_Session
156 * Which peer is this session for?
158 struct GNUNET_PeerIdentity target;
161 * Tokenizer for inbound messages.
163 struct GNUNET_MessageStreamTokenizer *mst;
166 * Plugin this session belongs to.
168 struct Plugin *plugin;
171 * Context for dealing with fragments.
173 struct UDP_FragmentationContext *frag_ctx;
176 * Desired delay for next sending we send to other peer
178 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
181 * Desired delay for transmissions we received from other peer.
182 * This is for full messages, the value needs to be adjusted for
183 * fragmented messages.
185 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
188 * Session timeout task
190 struct GNUNET_SCHEDULER_Task *timeout_task;
193 * When does this session time out?
195 struct GNUNET_TIME_Absolute timeout;
198 * What time did we last transmit?
200 struct GNUNET_TIME_Absolute last_transmit_time;
203 * expected delay for ACKs
205 struct GNUNET_TIME_Relative last_expected_ack_delay;
208 * desired delay between UDP messages
210 struct GNUNET_TIME_Relative last_expected_msg_delay;
215 struct GNUNET_HELLO_Address *address;
218 * Number of bytes waiting for transmission to this peer.
220 unsigned long long bytes_in_queue;
223 * Number of messages waiting for transmission to this peer.
225 unsigned int msgs_in_queue;
228 * Reference counter to indicate that this session is
229 * currently being used and must not be destroyed;
230 * setting @e in_destroy will destroy it as soon as
236 * Network type of the address.
238 enum GNUNET_NetworkType scope;
241 * Is this session about to be destroyed (sometimes we cannot
242 * destroy a session immediately as below us on the stack
243 * there might be code that still uses it; in this case,
244 * @e rc is non-zero).
251 * Data structure to track defragmentation contexts based
252 * on the source of the UDP traffic.
257 * Defragmentation context.
259 struct GNUNET_DEFRAGMENT_Context *defrag;
262 * Reference to master plugin struct.
264 struct Plugin *plugin;
267 * Node in the defrag heap.
269 struct GNUNET_CONTAINER_HeapNode *hnode;
272 * Source address this receive context is for (allocated at the
273 * end of the struct).
275 const union UdpAddress *udp_addr;
278 * Who's message(s) are we defragmenting here?
279 * Only initialized once we succeeded and
280 * @e have_sender is set.
282 struct GNUNET_PeerIdentity sender;
285 * Length of @e udp_addr.
290 * Network type the address belongs to.
292 enum GNUNET_NetworkType network_type;
295 * Has the @e sender field been initialized yet?
302 * Context to send fragmented messages
304 struct UDP_FragmentationContext
307 * Next in linked list
309 struct UDP_FragmentationContext *next;
312 * Previous in linked list
314 struct UDP_FragmentationContext *prev;
319 struct Plugin *plugin;
322 * Handle for fragmentation.
324 struct GNUNET_FRAGMENT_Context *frag;
327 * The session this fragmentation context belongs to
329 struct GNUNET_ATS_Session *session;
332 * Function to call upon completion of the transmission.
334 GNUNET_TRANSPORT_TransmitContinuation cont;
337 * Closure for @e cont.
344 struct GNUNET_TIME_Absolute start_time;
347 * Transmission time for the next fragment. Incremented by
348 * the @e flow_delay_from_other_peer for each fragment when
349 * we setup the fragments.
351 struct GNUNET_TIME_Absolute next_frag_time;
354 * Desired delay for transmissions we received from other peer.
355 * Adjusted to be per fragment (UDP_MTU), even though on the
356 * wire it was for "full messages".
358 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
363 struct GNUNET_TIME_Absolute timeout;
366 * Payload size of original unfragmented message
371 * Bytes used to send all fragments on wire including UDP overhead
378 * Function called when a message is removed from the
379 * transmission queue.
382 * @param udpw message wrapper finished
383 * @param result #GNUNET_OK on success (message was sent)
384 * #GNUNET_SYSERR if the target disconnected
385 * or we had a timeout or other trouble sending
387 typedef void (*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.
475 GNUNET_NETWORK_STRUCT_BEGIN
478 * UDP ACK Message-Packet header.
480 struct UDP_ACK_Message
485 struct GNUNET_MessageHeader header;
488 * Desired delay for flow control, in us (in NBO).
489 * A value of UINT32_MAX indicates that the other
490 * peer wants us to disconnect.
492 uint32_t delay GNUNET_PACKED;
495 * What is the identity of the sender
497 struct GNUNET_PeerIdentity sender;
500 GNUNET_NETWORK_STRUCT_END
503 /* ************************* Monitoring *********** */
507 * If a session monitor is attached, notify it about the new
510 * @param plugin our plugin
511 * @param session session that changed state
512 * @param state new state of the session
515 notify_session_monitor (struct Plugin *plugin,
516 struct GNUNET_ATS_Session *session,
517 enum GNUNET_TRANSPORT_SessionState state)
519 struct GNUNET_TRANSPORT_SessionInfo info;
521 if (NULL == plugin->sic)
523 if (GNUNET_YES == session->in_destroy)
524 return; /* already destroyed, just RC>0 left-over actions */
525 memset (&info, 0, sizeof(info));
527 info.is_inbound = GNUNET_SYSERR; /* hard to say */
528 info.num_msg_pending = session->msgs_in_queue;
529 info.num_bytes_pending = session->bytes_in_queue;
530 /* info.receive_delay remains zero as this is not supported by UDP
531 (cannot selectively not receive from 'some' peer while continuing
532 to receive from others) */
533 info.session_timeout = session->timeout;
534 info.address = session->address;
535 plugin->sic (plugin->sic_cls, session, &info);
540 * Return information about the given session to the monitor callback.
542 * @param cls the `struct Plugin` with the monitor callback (`sic`)
543 * @param peer peer we send information about
544 * @param value our `struct GNUNET_ATS_Session` to send information about
545 * @return #GNUNET_OK (continue to iterate)
548 send_session_info_iter (void *cls,
549 const struct GNUNET_PeerIdentity *peer,
552 struct Plugin *plugin = cls;
553 struct GNUNET_ATS_Session *session = value;
555 notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_INIT);
556 notify_session_monitor (plugin, session, GNUNET_TRANSPORT_SS_UP);
562 * Begin monitoring sessions of a plugin. There can only
563 * be one active monitor per plugin (i.e. if there are
564 * multiple monitors, the transport service needs to
565 * multiplex the generated events over all of them).
567 * @param cls closure of the plugin
568 * @param sic callback to invoke, NULL to disable monitor;
569 * plugin will being by iterating over all active
570 * sessions immediately and then enter monitor mode
571 * @param sic_cls closure for @a sic
574 udp_plugin_setup_monitor (void *cls,
575 GNUNET_TRANSPORT_SessionInfoCallback sic,
578 struct Plugin *plugin = cls;
581 plugin->sic_cls = sic_cls;
584 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
585 &send_session_info_iter,
587 /* signal end of first iteration */
588 sic (sic_cls, NULL, NULL);
593 /* ****************** Little Helpers ****************** */
597 * Function to free last resources associated with a session.
599 * @param s session to free
602 free_session (struct GNUNET_ATS_Session *s)
604 if (NULL != s->address)
606 GNUNET_HELLO_address_free (s->address);
609 if (NULL != s->frag_ctx)
611 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL);
612 GNUNET_free (s->frag_ctx);
617 GNUNET_MST_destroy (s->mst);
625 * Function that is called to get the keepalive factor.
626 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
627 * calculate the interval between keepalive packets.
629 * @param cls closure with the `struct Plugin`
630 * @return keepalive factor
633 udp_query_keepalive_factor (void *cls)
640 * Function obtain the network type for a session
642 * @param cls closure (`struct Plugin *`)
643 * @param session the session
644 * @return the network type
646 static enum GNUNET_NetworkType
647 udp_plugin_get_network (void *cls, struct GNUNET_ATS_Session *session)
649 return session->scope;
654 * Function obtain the network type for an address.
656 * @param cls closure (`struct Plugin *`)
657 * @param address the address
658 * @return the network type
660 static enum GNUNET_NetworkType
661 udp_plugin_get_network_for_address (void *cls,
662 const struct GNUNET_HELLO_Address *address)
664 struct Plugin *plugin = cls;
666 struct sockaddr_in a4;
667 struct sockaddr_in6 a6;
668 const struct IPv4UdpAddress *u4;
669 const struct IPv6UdpAddress *u6;
673 addrlen = address->address_length;
674 if (addrlen == sizeof(struct IPv6UdpAddress))
676 GNUNET_assert (NULL != address->address); /* make static analysis happy */
677 u6 = address->address;
678 memset (&a6, 0, sizeof(a6));
679 #if HAVE_SOCKADDR_IN_SIN_LEN
680 a6.sin6_len = sizeof(a6);
682 a6.sin6_family = AF_INET6;
683 a6.sin6_port = u6->u6_port;
684 GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
688 else if (addrlen == sizeof(struct IPv4UdpAddress))
690 GNUNET_assert (NULL != address->address); /* make static analysis happy */
691 u4 = address->address;
692 memset (&a4, 0, sizeof(a4));
693 #if HAVE_SOCKADDR_IN_SIN_LEN
694 a4.sin_len = sizeof(a4);
696 a4.sin_family = AF_INET;
697 a4.sin_port = u4->u4_port;
698 a4.sin_addr.s_addr = u4->ipv4_addr;
705 return GNUNET_NT_UNSPECIFIED;
707 return plugin->env->get_address_type (plugin->env->cls, sb, sbs);
711 /* ******************* Event loop ******************** */
714 * We have been notified that our readset has something to read. We don't
715 * know which socket needs to be read, so we have to check each one
716 * Then reschedule this function to be called again once more is available.
718 * @param cls the plugin handle
721 udp_plugin_select_v4 (void *cls);
725 * We have been notified that our readset has something to read. We don't
726 * know which socket needs to be read, so we have to check each one
727 * Then reschedule this function to be called again once more is available.
729 * @param cls the plugin handle
732 udp_plugin_select_v6 (void *cls);
736 * (re)schedule IPv4-select tasks for this plugin.
738 * @param plugin plugin to reschedule
741 schedule_select_v4 (struct Plugin *plugin)
743 struct GNUNET_TIME_Relative min_delay;
744 struct GNUNET_TIME_Relative delay;
745 struct UDP_MessageWrapper *udpw;
746 struct UDP_MessageWrapper *min_udpw;
748 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
750 /* Find a message ready to send:
751 * Flow delay from other peer is expired or not set (0) */
752 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
754 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
756 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
757 if (delay.rel_value_us < min_delay.rel_value_us)
763 if (NULL != plugin->select_task_v4)
764 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
765 if (NULL != min_udpw)
767 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
770 "Calculated flow delay for UDPv4 at %s for %s\n",
771 GNUNET_STRINGS_relative_time_to_string (min_delay,
773 GNUNET_i2s (&min_udpw->session->target));
777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
778 "Calculated flow delay for UDPv4 at %s for %s\n",
779 GNUNET_STRINGS_relative_time_to_string (min_delay,
781 GNUNET_i2s (&min_udpw->session->target));
784 plugin->select_task_v4 =
785 GNUNET_SCHEDULER_add_read_net (min_delay,
787 &udp_plugin_select_v4,
794 * (re)schedule IPv6-select tasks for this plugin.
796 * @param plugin plugin to reschedule
799 schedule_select_v6 (struct Plugin *plugin)
801 struct GNUNET_TIME_Relative min_delay;
802 struct GNUNET_TIME_Relative delay;
803 struct UDP_MessageWrapper *udpw;
804 struct UDP_MessageWrapper *min_udpw;
806 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
808 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
810 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
812 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
813 if (delay.rel_value_us < min_delay.rel_value_us)
819 if (NULL != plugin->select_task_v6)
820 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
821 if (NULL != min_udpw)
823 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826 "Calculated flow delay for UDPv6 at %s for %s\n",
827 GNUNET_STRINGS_relative_time_to_string (min_delay,
829 GNUNET_i2s (&min_udpw->session->target));
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Calculated flow delay for UDPv6 at %s for %s\n",
835 GNUNET_STRINGS_relative_time_to_string (min_delay,
837 GNUNET_i2s (&min_udpw->session->target));
840 plugin->select_task_v6 =
841 GNUNET_SCHEDULER_add_read_net (min_delay,
843 &udp_plugin_select_v6,
849 /* ******************* Address to string and back ***************** */
853 * Function called for a quick conversion of the binary address to
854 * a numeric address. Note that the caller must not free the
855 * address and that the next call to this function is allowed
856 * to override the address again.
859 * @param addr binary address (a `union UdpAddress`)
860 * @param addrlen length of the @a addr
861 * @return string representing the same address
864 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
866 static char rbuf[INET6_ADDRSTRLEN + 10];
867 char buf[INET6_ADDRSTRLEN];
871 const struct IPv4UdpAddress *t4;
872 const struct IPv6UdpAddress *t6;
883 if (addrlen == sizeof(struct IPv6UdpAddress))
887 options = ntohl (t6->options);
888 port = ntohs (t6->u6_port);
892 else if (addrlen == sizeof(struct IPv4UdpAddress))
896 options = ntohl (t4->options);
897 port = ntohs (t4->u4_port);
898 a4.s_addr = t4->ipv4_addr;
906 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
907 GNUNET_snprintf (rbuf,
909 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
919 * Function called to convert a string address to a binary address.
921 * @param cls closure (`struct Plugin *`)
922 * @param addr string address
923 * @param addrlen length of the address
924 * @param buf location to store the buffer
925 * @param added location to store the number of bytes in the buffer.
926 * If the function returns #GNUNET_SYSERR, its contents are undefined.
927 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
930 udp_string_to_address (void *cls,
936 struct sockaddr_storage socket_address;
942 /* Format tcp.options.address:port */
947 if ((NULL == addr) || (0 == addrlen))
950 return GNUNET_SYSERR;
952 if ('\0' != addr[addrlen - 1])
955 return GNUNET_SYSERR;
957 if (strlen (addr) != addrlen - 1)
960 return GNUNET_SYSERR;
962 plugin = GNUNET_strdup (addr);
963 optionstr = strchr (plugin, '.');
964 if (NULL == optionstr)
967 GNUNET_free (plugin);
968 return GNUNET_SYSERR;
972 options = atol (optionstr);
973 address = strchr (optionstr, '.');
977 GNUNET_free (plugin);
978 return GNUNET_SYSERR;
984 GNUNET_STRINGS_to_address_ip (address, strlen (address), &socket_address))
987 GNUNET_free (plugin);
988 return GNUNET_SYSERR;
990 GNUNET_free (plugin);
992 switch (socket_address.ss_family)
995 struct IPv4UdpAddress *u4;
996 const struct sockaddr_in *in4 =
997 (const struct sockaddr_in *) &socket_address;
999 u4 = GNUNET_new (struct IPv4UdpAddress);
1000 u4->options = htonl (options);
1001 u4->ipv4_addr = in4->sin_addr.s_addr;
1002 u4->u4_port = in4->sin_port;
1004 *added = sizeof(struct IPv4UdpAddress);
1009 struct IPv6UdpAddress *u6;
1010 const struct sockaddr_in6 *in6 =
1011 (const struct sockaddr_in6 *) &socket_address;
1013 u6 = GNUNET_new (struct IPv6UdpAddress);
1014 u6->options = htonl (options);
1015 u6->ipv6_addr = in6->sin6_addr;
1016 u6->u6_port = in6->sin6_port;
1018 *added = sizeof(struct IPv6UdpAddress);
1024 return GNUNET_SYSERR;
1030 * Append our port and forward the result.
1032 * @param cls a `struct PrettyPrinterContext *`
1033 * @param hostname result from DNS resolver
1036 append_port (void *cls, const char *hostname)
1038 struct PrettyPrinterContext *ppc = cls;
1039 struct Plugin *plugin = ppc->plugin;
1042 if (NULL == hostname)
1044 /* Final call, done */
1045 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1046 plugin->ppc_dll_tail,
1048 ppc->resolver_handle = NULL;
1049 ppc->asc (ppc->asc_cls, NULL, GNUNET_OK);
1053 if (GNUNET_YES == ppc->ipv6)
1054 GNUNET_asprintf (&ret,
1061 GNUNET_asprintf (&ret,
1067 ppc->asc (ppc->asc_cls, ret, GNUNET_OK);
1073 * Convert the transports address to a nice, human-readable format.
1075 * @param cls closure with the `struct Plugin *`
1076 * @param type name of the transport that generated the address
1077 * @param addr one of the addresses of the host, NULL for the last address
1078 * the specific address format depends on the transport;
1079 * a `union UdpAddress`
1080 * @param addrlen length of the address
1081 * @param numeric should (IP) addresses be displayed in numeric form?
1082 * @param timeout after how long should we give up?
1083 * @param asc function to call on each string
1084 * @param asc_cls closure for @a asc
1087 udp_plugin_address_pretty_printer (void *cls,
1092 struct GNUNET_TIME_Relative timeout,
1093 GNUNET_TRANSPORT_AddressStringCallback asc,
1096 struct Plugin *plugin = cls;
1097 struct PrettyPrinterContext *ppc;
1098 const struct sockaddr *sb;
1100 struct sockaddr_in a4;
1101 struct sockaddr_in6 a6;
1102 const struct IPv4UdpAddress *u4;
1103 const struct IPv6UdpAddress *u6;
1107 if (addrlen == sizeof(struct IPv6UdpAddress))
1110 memset (&a6, 0, sizeof(a6));
1111 a6.sin6_family = AF_INET6;
1112 #if HAVE_SOCKADDR_IN_SIN_LEN
1113 a6.sin6_len = sizeof(a6);
1115 a6.sin6_port = u6->u6_port;
1116 a6.sin6_addr = u6->ipv6_addr;
1117 port = ntohs (u6->u6_port);
1118 options = ntohl (u6->options);
1119 sb = (const struct sockaddr *) &a6;
1122 else if (addrlen == sizeof(struct IPv4UdpAddress))
1125 memset (&a4, 0, sizeof(a4));
1126 a4.sin_family = AF_INET;
1127 #if HAVE_SOCKADDR_IN_SIN_LEN
1128 a4.sin_len = sizeof(a4);
1130 a4.sin_port = u4->u4_port;
1131 a4.sin_addr.s_addr = u4->ipv4_addr;
1132 port = ntohs (u4->u4_port);
1133 options = ntohl (u4->options);
1134 sb = (const struct sockaddr *) &a4;
1139 /* invalid address */
1140 GNUNET_break_op (0);
1141 asc (asc_cls, NULL, GNUNET_SYSERR);
1142 asc (asc_cls, NULL, GNUNET_OK);
1145 ppc = GNUNET_new (struct PrettyPrinterContext);
1146 ppc->plugin = plugin;
1148 ppc->asc_cls = asc_cls;
1150 ppc->options = options;
1151 if (addrlen == sizeof(struct IPv6UdpAddress))
1152 ppc->ipv6 = GNUNET_YES;
1154 ppc->ipv6 = GNUNET_NO;
1155 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head, plugin->ppc_dll_tail, ppc);
1156 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
1166 * Check if the given port is plausible (must be either our listen
1167 * port or our advertised port). If it is neither, we return
1170 * @param plugin global variables
1171 * @param in_port port number to check
1172 * @return #GNUNET_OK if port is either our open or advertised port
1175 check_port (const struct Plugin *plugin, uint16_t in_port)
1177 if ((plugin->port == in_port) || (plugin->aport == in_port))
1179 return GNUNET_SYSERR;
1184 * Function that will be called to check if a binary address for this
1185 * plugin is well-formed and corresponds to an address for THIS peer
1186 * (as per our configuration). Naturally, if absolutely necessary,
1187 * plugins can be a bit conservative in their answer, but in general
1188 * plugins should make sure that the address does not redirect
1189 * traffic to a 3rd party that might try to man-in-the-middle our
1192 * @param cls closure, should be our handle to the Plugin
1193 * @param addr pointer to a `union UdpAddress`
1194 * @param addrlen length of @a addr
1195 * @return #GNUNET_OK if this is a plausible address for this peer
1196 * and transport, #GNUNET_SYSERR if not
1199 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1201 struct Plugin *plugin = cls;
1202 const struct IPv4UdpAddress *v4;
1203 const struct IPv6UdpAddress *v6;
1205 if (sizeof(struct IPv4UdpAddress) == addrlen)
1207 struct sockaddr_in s4;
1209 v4 = (const struct IPv4UdpAddress *) addr;
1210 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1211 return GNUNET_SYSERR;
1212 memset (&s4, 0, sizeof(s4));
1213 s4.sin_family = AF_INET;
1214 #if HAVE_SOCKADDR_IN_SIN_LEN
1215 s4.sin_len = sizeof(s4);
1217 s4.sin_port = v4->u4_port;
1218 s4.sin_addr.s_addr = v4->ipv4_addr;
1221 GNUNET_NAT_test_address (plugin->nat, &s4, sizeof(struct sockaddr_in)))
1222 return GNUNET_SYSERR;
1224 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1226 struct sockaddr_in6 s6;
1228 v6 = (const struct IPv6UdpAddress *) addr;
1229 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1230 return GNUNET_OK; /* plausible, if unlikely... */
1231 memset (&s6, 0, sizeof(s6));
1232 s6.sin6_family = AF_INET6;
1233 #if HAVE_SOCKADDR_IN_SIN_LEN
1234 s6.sin6_len = sizeof(s6);
1236 s6.sin6_port = v6->u6_port;
1237 s6.sin6_addr = v6->ipv6_addr;
1239 if (GNUNET_OK != GNUNET_NAT_test_address (plugin->nat,
1241 sizeof(struct sockaddr_in6)))
1242 return GNUNET_SYSERR;
1246 GNUNET_break_op (0);
1247 return GNUNET_SYSERR;
1254 * Our external IP address/port mapping has changed.
1256 * @param cls closure, the `struct Plugin`
1257 * @param app_ctx[in,out] location where the app can store stuff
1258 * on add and retrieve it on remove
1259 * @param add_remove #GNUNET_YES to mean the new public IP address,
1260 * #GNUNET_NO to mean the previous (now invalid) one
1261 * @param ac address class the address belongs to
1262 * @param addr either the previous or the new public IP address
1263 * @param addrlen actual length of the @a addr
1266 udp_nat_port_map_callback (void *cls,
1269 enum GNUNET_NAT_AddressClass ac,
1270 const struct sockaddr *addr,
1273 struct Plugin *plugin = cls;
1274 struct GNUNET_HELLO_Address *address;
1275 struct IPv4UdpAddress u4;
1276 struct IPv6UdpAddress u6;
1281 LOG (GNUNET_ERROR_TYPE_DEBUG,
1282 (GNUNET_YES == add_remove) ? "NAT notification to add address `%s'\n"
1283 : "NAT notification to remove address `%s'\n",
1284 GNUNET_a2s (addr, addrlen));
1285 /* convert 'address' to our internal format */
1286 switch (addr->sa_family)
1289 const struct sockaddr_in *i4;
1291 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1292 i4 = (const struct sockaddr_in *) addr;
1293 if (0 == ntohs (i4->sin_port))
1294 return; /* Port = 0 means unmapped, ignore these for UDP. */
1295 memset (&u4, 0, sizeof(u4));
1296 u4.options = htonl (plugin->myoptions);
1297 u4.ipv4_addr = i4->sin_addr.s_addr;
1298 u4.u4_port = i4->sin_port;
1300 args = sizeof(struct IPv4UdpAddress);
1305 const struct sockaddr_in6 *i6;
1307 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1308 i6 = (const struct sockaddr_in6 *) addr;
1309 if (0 == ntohs (i6->sin6_port))
1310 return; /* Port = 0 means unmapped, ignore these for UDP. */
1311 memset (&u6, 0, sizeof(u6));
1312 u6.options = htonl (plugin->myoptions);
1313 u6.ipv6_addr = i6->sin6_addr;
1314 u6.u6_port = i6->sin6_port;
1316 args = sizeof(struct IPv6UdpAddress);
1324 /* modify our published address list */
1325 /* TODO: use 'ac' here in the future... */
1326 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1330 GNUNET_HELLO_ADDRESS_INFO_NONE);
1331 plugin->env->notify_address (plugin->env->cls, add_remove, address);
1332 GNUNET_HELLO_address_free (address);
1336 /* ********************* Finding sessions ******************* */
1340 * Closure for #session_cmp_it().
1342 struct GNUNET_ATS_SessionCompareContext
1345 * Set to session matching the address.
1347 struct GNUNET_ATS_Session *res;
1350 * Address we are looking for.
1352 const struct GNUNET_HELLO_Address *address;
1357 * Find a session with a matching address.
1359 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1360 * @param key peer identity (unused)
1361 * @param value the `struct GNUNET_ATS_Session *`
1362 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1365 session_cmp_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1367 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1368 struct GNUNET_ATS_Session *s = value;
1370 if (0 == GNUNET_HELLO_address_cmp (s->address, cctx->address))
1372 GNUNET_assert (GNUNET_NO == s->in_destroy);
1381 * Locate an existing session the transport service is using to
1382 * send data to another peer. Performs some basic sanity checks
1383 * on the address and then tries to locate a matching session.
1385 * @param cls the plugin
1386 * @param address the address we should locate the session by
1387 * @return the session if it exists, or NULL if it is not found
1389 static struct GNUNET_ATS_Session *
1390 udp_plugin_lookup_session (void *cls,
1391 const struct GNUNET_HELLO_Address *address)
1393 struct Plugin *plugin = cls;
1394 const struct IPv6UdpAddress *udp_a6;
1395 const struct IPv4UdpAddress *udp_a4;
1396 struct GNUNET_ATS_SessionCompareContext cctx;
1398 if (NULL == address->address)
1403 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1405 if (NULL == plugin->sockv4)
1407 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1408 if (0 == udp_a4->u4_port)
1414 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1416 if (NULL == plugin->sockv6)
1418 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1419 if (0 == udp_a6->u6_port)
1431 /* check if session already exists */
1432 cctx.address = address;
1434 LOG (GNUNET_ERROR_TYPE_DEBUG,
1435 "Looking for existing session for peer `%s' with address `%s'\n",
1436 GNUNET_i2s (&address->peer),
1437 udp_address_to_string (plugin,
1439 address->address_length));
1440 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1444 if (NULL == cctx.res)
1446 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1451 /* ********************** Timeout ****************** */
1455 * Increment session timeout due to activity.
1457 * @param s session to reschedule timeout activity for
1460 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1462 if (GNUNET_YES == s->in_destroy)
1464 GNUNET_assert (NULL != s->timeout_task);
1465 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1470 * Function that will be called whenever the transport service wants to
1471 * notify the plugin that a session is still active and in use and
1472 * therefore the session timeout for this session has to be updated
1474 * @param cls closure with the `struct Plugin`
1475 * @param peer which peer was the session for
1476 * @param session which session is being updated
1479 udp_plugin_update_session_timeout (void *cls,
1480 const struct GNUNET_PeerIdentity *peer,
1481 struct GNUNET_ATS_Session *session)
1483 struct Plugin *plugin = cls;
1486 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1493 /* Reschedule session timeout */
1494 reschedule_session_timeout (session);
1498 /* ************************* Sending ************************ */
1502 * Remove the given message from the transmission queue and
1503 * update all applicable statistics.
1505 * @param plugin the UDP plugin
1506 * @param udpw message wrapper to dequeue
1509 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper *udpw)
1511 struct GNUNET_ATS_Session *session = udpw->session;
1513 if (plugin->bytes_in_buffer < udpw->msg_size)
1519 GNUNET_STATISTICS_update (plugin->env->stats,
1520 "# UDP, total bytes in send buffers",
1521 -(long long) udpw->msg_size,
1523 plugin->bytes_in_buffer -= udpw->msg_size;
1525 GNUNET_STATISTICS_update (plugin->env->stats,
1526 "# UDP, total messages in send buffers",
1529 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1531 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1532 plugin->ipv4_queue_tail,
1535 else if (sizeof(struct IPv6UdpAddress) ==
1536 udpw->session->address->address_length)
1538 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1539 plugin->ipv6_queue_tail,
1547 GNUNET_assert (session->msgs_in_queue > 0);
1548 session->msgs_in_queue--;
1549 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1550 session->bytes_in_queue -= udpw->msg_size;
1555 * Enqueue a message for transmission and update statistics.
1557 * @param plugin the UDP plugin
1558 * @param udpw message wrapper to queue
1561 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper *udpw)
1563 struct GNUNET_ATS_Session *session = udpw->session;
1565 if (GNUNET_YES == session->in_destroy)
1571 if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
1577 GNUNET_STATISTICS_update (plugin->env->stats,
1578 "# UDP, total bytes in send buffers",
1581 plugin->bytes_in_buffer += udpw->msg_size;
1583 GNUNET_STATISTICS_update (plugin->env->stats,
1584 "# UDP, total messages in send buffers",
1587 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1589 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1590 plugin->ipv4_queue_tail,
1593 else if (sizeof(struct IPv6UdpAddress) ==
1594 udpw->session->address->address_length)
1596 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1597 plugin->ipv6_queue_tail,
1603 udpw->cont (udpw->cont_cls,
1611 session->msgs_in_queue++;
1612 session->bytes_in_queue += udpw->msg_size;
1617 * We have completed our (attempt) to transmit a message that had to
1618 * be fragmented -- either because we got an ACK saying that all
1619 * fragments were received, or because of timeout / disconnect. Clean
1622 * @param frag_ctx fragmentation context to clean up
1623 * @param result #GNUNET_OK if we succeeded (got ACK),
1624 * #GNUNET_SYSERR if the transmission failed
1627 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx, int result)
1629 struct Plugin *plugin = frag_ctx->plugin;
1630 struct GNUNET_ATS_Session *s = frag_ctx->session;
1631 struct UDP_MessageWrapper *udpw;
1632 struct UDP_MessageWrapper *tmp;
1634 struct GNUNET_TIME_Relative delay;
1636 LOG (GNUNET_ERROR_TYPE_DEBUG,
1637 "%p: Fragmented message removed with result %s\n",
1639 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1640 /* Call continuation for fragmented message */
1641 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1642 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1644 overhead = frag_ctx->on_wire_size;
1645 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1646 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1648 LOG (GNUNET_ERROR_TYPE_WARNING,
1649 "Fragmented message acknowledged after %s (expected at %s)\n",
1650 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
1651 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1655 LOG (GNUNET_ERROR_TYPE_DEBUG,
1656 "Fragmented message acknowledged after %s (expected at %s)\n",
1657 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES),
1658 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1661 if (NULL != frag_ctx->cont)
1662 frag_ctx->cont (frag_ctx->cont_cls,
1665 s->frag_ctx->payload_size,
1666 frag_ctx->on_wire_size);
1667 GNUNET_STATISTICS_update (plugin->env->stats,
1668 "# UDP, fragmented messages active",
1672 if (GNUNET_OK == result)
1674 GNUNET_STATISTICS_update (plugin->env->stats,
1675 "# UDP, fragmented msgs, messages, sent, success",
1678 GNUNET_STATISTICS_update (plugin->env->stats,
1679 "# UDP, fragmented msgs, bytes payload, sent, success",
1680 s->frag_ctx->payload_size,
1682 GNUNET_STATISTICS_update (
1684 "# UDP, fragmented msgs, bytes overhead, sent, success",
1687 GNUNET_STATISTICS_update (plugin->env->stats,
1688 "# UDP, total, bytes overhead, sent",
1691 GNUNET_STATISTICS_update (plugin->env->stats,
1692 "# UDP, total, bytes payload, sent",
1693 s->frag_ctx->payload_size,
1698 GNUNET_STATISTICS_update (plugin->env->stats,
1699 "# UDP, fragmented msgs, messages, sent, failure",
1702 GNUNET_STATISTICS_update (plugin->env->stats,
1703 "# UDP, fragmented msgs, bytes payload, sent, failure",
1704 s->frag_ctx->payload_size,
1706 GNUNET_STATISTICS_update (plugin->env->stats,
1707 "# UDP, fragmented msgs, bytes payload, sent, failure",
1710 GNUNET_STATISTICS_update (plugin->env->stats,
1711 "# UDP, fragmented msgs, bytes payload, sent, failure",
1716 /* Remove remaining fragments from queue, no need to transmit those
1718 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1720 udpw = plugin->ipv6_queue_head;
1721 while (NULL != udpw)
1724 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == frag_ctx))
1726 dequeue (plugin, udpw);
1732 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1734 udpw = plugin->ipv4_queue_head;
1735 while (NULL != udpw)
1738 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == frag_ctx))
1740 dequeue (plugin, udpw);
1746 notify_session_monitor (s->plugin, s, GNUNET_TRANSPORT_SS_UPDATE);
1747 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1748 &s->last_expected_msg_delay,
1749 &s->last_expected_ack_delay);
1751 GNUNET_free (frag_ctx);
1756 * We are finished with a fragment in the message queue.
1757 * Notify the continuation and update statistics.
1759 * @param cls the `struct Plugin *`
1760 * @param udpw the queue entry
1761 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1764 qc_fragment_sent (void *cls, struct UDP_MessageWrapper *udpw, int result)
1766 struct Plugin *plugin = cls;
1768 GNUNET_assert (NULL != udpw->frag_ctx);
1769 if (GNUNET_OK == result)
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 "Fragment of message with %u bytes transmitted to %s\n",
1773 (unsigned int) udpw->payload_size,
1774 GNUNET_i2s (&udpw->session->target));
1775 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1776 GNUNET_STATISTICS_update (plugin->env->stats,
1777 "# UDP, fragmented msgs, fragments, sent, success",
1780 GNUNET_STATISTICS_update (
1782 "# UDP, fragmented msgs, fragments bytes, sent, success",
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "Failed to transmit fragment of message with %u bytes to %s\n",
1790 (unsigned int) udpw->payload_size,
1791 GNUNET_i2s (&udpw->session->target));
1792 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
1793 GNUNET_STATISTICS_update (plugin->env->stats,
1794 "# UDP, fragmented msgs, fragments, sent, failure",
1797 GNUNET_STATISTICS_update (
1799 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1807 * Function that is called with messages created by the fragmentation
1808 * module. In the case of the `proc` callback of the
1809 * #GNUNET_FRAGMENT_context_create() function, this function must
1810 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1812 * @param cls closure, the `struct UDP_FragmentationContext`
1813 * @param msg the message that was created
1816 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1818 struct UDP_FragmentationContext *frag_ctx = cls;
1819 struct Plugin *plugin = frag_ctx->plugin;
1820 struct UDP_MessageWrapper *udpw;
1821 struct GNUNET_ATS_Session *session = frag_ctx->session;
1822 size_t msg_len = ntohs (msg->size);
1824 LOG (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes\n", msg_len);
1825 udpw = GNUNET_malloc (sizeof(struct UDP_MessageWrapper) + msg_len);
1826 udpw->session = session;
1827 udpw->msg_buf = (char *) &udpw[1];
1828 udpw->msg_size = msg_len;
1829 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1830 udpw->timeout = frag_ctx->timeout;
1831 udpw->start_time = frag_ctx->start_time;
1832 udpw->transmission_time = frag_ctx->next_frag_time;
1833 frag_ctx->next_frag_time =
1834 GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1835 frag_ctx->flow_delay_from_other_peer);
1836 udpw->frag_ctx = frag_ctx;
1837 udpw->qc = &qc_fragment_sent;
1838 udpw->qc_cls = plugin;
1839 GNUNET_memcpy (udpw->msg_buf, msg, msg_len);
1840 enqueue (plugin, udpw);
1841 if (session->address->address_length == sizeof(struct IPv4UdpAddress))
1842 schedule_select_v4 (plugin);
1844 schedule_select_v6 (plugin);
1849 * We are finished with a message from the message queue.
1850 * Notify the continuation and update statistics.
1852 * @param cls the `struct Plugin *`
1853 * @param udpw the queue entry
1854 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1857 qc_message_sent (void *cls, struct UDP_MessageWrapper *udpw, int result)
1859 struct Plugin *plugin = cls;
1861 struct GNUNET_TIME_Relative delay;
1863 if (udpw->msg_size >= udpw->payload_size)
1864 overhead = udpw->msg_size - udpw->payload_size;
1866 overhead = udpw->msg_size;
1868 if (NULL != udpw->cont)
1870 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1871 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1873 LOG (GNUNET_ERROR_TYPE_WARNING,
1874 "Message sent via UDP with delay of %s\n",
1875 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1879 LOG (GNUNET_ERROR_TYPE_DEBUG,
1880 "Message sent via UDP with delay of %s\n",
1881 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
1883 udpw->cont (udpw->cont_cls,
1884 &udpw->session->target,
1889 if (GNUNET_OK == result)
1891 GNUNET_STATISTICS_update (plugin->env->stats,
1892 "# UDP, unfragmented msgs, messages, sent, success",
1895 GNUNET_STATISTICS_update (
1897 "# UDP, unfragmented msgs, bytes payload, sent, success",
1900 GNUNET_STATISTICS_update (
1902 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1905 GNUNET_STATISTICS_update (plugin->env->stats,
1906 "# UDP, total, bytes overhead, sent",
1909 GNUNET_STATISTICS_update (plugin->env->stats,
1910 "# UDP, total, bytes payload, sent",
1916 GNUNET_STATISTICS_update (plugin->env->stats,
1917 "# UDP, unfragmented msgs, messages, sent, failure",
1920 GNUNET_STATISTICS_update (
1922 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1925 GNUNET_STATISTICS_update (
1927 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1935 * Function that can be used by the transport service to transmit a
1936 * message using the plugin. Note that in the case of a peer
1937 * disconnecting, the continuation MUST be called prior to the
1938 * disconnect notification itself. This function will be called with
1939 * this peer's HELLO message to initiate a fresh connection to another
1942 * @param cls closure
1943 * @param s which session must be used
1944 * @param msgbuf the message to transmit
1945 * @param msgbuf_size number of bytes in @a msgbuf
1946 * @param priority how important is the message (most plugins will
1947 * ignore message priority and just FIFO)
1948 * @param to how long to wait at most for the transmission (does not
1949 * require plugins to discard the message after the timeout,
1950 * just advisory for the desired delay; most plugins will ignore
1952 * @param cont continuation to call once the message has
1953 * been transmitted (or if the transport is ready
1954 * for the next transmission call; or if the
1955 * peer disconnected...); can be NULL
1956 * @param cont_cls closure for @a cont
1957 * @return number of bytes used (on the physical network, with overheads);
1958 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1959 * and does NOT mean that the message was not transmitted (DV)
1962 udp_plugin_send (void *cls,
1963 struct GNUNET_ATS_Session *s,
1966 unsigned int priority,
1967 struct GNUNET_TIME_Relative to,
1968 GNUNET_TRANSPORT_TransmitContinuation cont,
1971 struct Plugin *plugin = cls;
1972 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
1973 struct UDP_FragmentationContext *frag_ctx;
1974 struct UDP_MessageWrapper *udpw;
1975 struct UDPMessage *udp;
1976 char mbuf[udpmlen] GNUNET_ALIGN;
1977 struct GNUNET_TIME_Relative latency;
1979 if ((sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
1980 (NULL == plugin->sockv6))
1981 return GNUNET_SYSERR;
1982 if ((sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
1983 (NULL == plugin->sockv4))
1984 return GNUNET_SYSERR;
1985 if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
1988 return GNUNET_SYSERR;
1991 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1996 return GNUNET_SYSERR;
1998 LOG (GNUNET_ERROR_TYPE_DEBUG,
1999 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2001 GNUNET_i2s (&s->target),
2002 udp_address_to_string (plugin,
2003 s->address->address,
2004 s->address->address_length));
2006 udp = (struct UDPMessage *) mbuf;
2007 udp->header.size = htons (udpmlen);
2008 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2009 udp->reserved = htonl (0);
2010 udp->sender = *plugin->env->my_identity;
2012 /* We do not update the session time out here! Otherwise this
2013 * session will not timeout since we send keep alive before session
2016 * For UDP we update session timeout only on receive, this will
2017 * cover keep alives, since remote peer will reply with keep alive
2019 */if (udpmlen <= UDP_MTU)
2021 /* unfragmented message */
2022 udpw = GNUNET_malloc (sizeof(struct UDP_MessageWrapper) + udpmlen);
2024 udpw->msg_buf = (char *) &udpw[1];
2025 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2026 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2027 udpw->start_time = GNUNET_TIME_absolute_get ();
2028 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2029 udpw->transmission_time = s->last_transmit_time;
2030 s->last_transmit_time =
2031 GNUNET_TIME_absolute_add (s->last_transmit_time,
2032 s->flow_delay_from_other_peer);
2034 udpw->cont_cls = cont_cls;
2035 udpw->frag_ctx = NULL;
2036 udpw->qc = &qc_message_sent;
2037 udpw->qc_cls = plugin;
2038 GNUNET_memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
2039 GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2042 enqueue (plugin, udpw);
2043 GNUNET_STATISTICS_update (plugin->env->stats,
2044 "# UDP, unfragmented messages queued total",
2047 GNUNET_STATISTICS_update (plugin->env->stats,
2048 "# UDP, unfragmented bytes payload queued total",
2051 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
2052 schedule_select_v4 (plugin);
2054 schedule_select_v6 (plugin);
2058 /* fragmented message */
2059 if (NULL != s->frag_ctx)
2060 return GNUNET_SYSERR;
2061 GNUNET_memcpy (&udp[1], msgbuf, msgbuf_size);
2062 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2063 frag_ctx->plugin = plugin;
2064 frag_ctx->session = s;
2065 frag_ctx->cont = cont;
2066 frag_ctx->cont_cls = cont_cls;
2067 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2068 frag_ctx->next_frag_time = s->last_transmit_time;
2069 frag_ctx->flow_delay_from_other_peer =
2070 GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
2071 1 + (msgbuf_size / UDP_MTU));
2072 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2073 frag_ctx->payload_size =
2074 msgbuf_size; /* unfragmented message size without UDP overhead */
2075 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2076 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2079 s->last_expected_msg_delay,
2080 s->last_expected_ack_delay,
2084 s->frag_ctx = frag_ctx;
2085 s->last_transmit_time = frag_ctx->next_frag_time;
2086 latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
2087 if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2088 LOG (GNUNET_ERROR_TYPE_WARNING,
2089 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2090 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_YES),
2091 GNUNET_i2s (&s->target),
2092 (unsigned int) s->msgs_in_queue);
2094 LOG (GNUNET_ERROR_TYPE_DEBUG,
2095 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2096 GNUNET_STRINGS_relative_time_to_string (latency, GNUNET_YES),
2097 GNUNET_i2s (&s->target),
2098 (unsigned int) s->msgs_in_queue);
2100 GNUNET_STATISTICS_update (plugin->env->stats,
2101 "# UDP, fragmented messages active",
2104 GNUNET_STATISTICS_update (plugin->env->stats,
2105 "# UDP, fragmented messages, total",
2108 GNUNET_STATISTICS_update (plugin->env->stats,
2109 "# UDP, fragmented bytes (payload)",
2110 frag_ctx->payload_size,
2113 notify_session_monitor (s->plugin, s, GNUNET_TRANSPORT_SS_UPDATE);
2118 /* ********************** Receiving ********************** */
2122 * Closure for #find_receive_context().
2124 struct FindReceiveContext
2127 * Where to store the result.
2129 struct DefragContext *rc;
2132 * Session associated with this context.
2134 struct GNUNET_ATS_Session *session;
2139 const union UdpAddress *udp_addr;
2142 * Number of bytes in @e udp_addr.
2144 size_t udp_addr_len;
2149 * Scan the heap for a receive context with the given address.
2151 * @param cls the `struct FindReceiveContext`
2152 * @param node internal node of the heap
2153 * @param element value stored at the node (a `struct ReceiveContext`)
2154 * @param cost cost associated with the node
2155 * @return #GNUNET_YES if we should continue to iterate,
2156 * #GNUNET_NO if not.
2159 find_receive_context (void *cls,
2160 struct GNUNET_CONTAINER_HeapNode *node,
2162 GNUNET_CONTAINER_HeapCostType cost)
2164 struct FindReceiveContext *frc = cls;
2165 struct DefragContext *e = element;
2167 if ((frc->udp_addr_len == e->udp_addr_len) &&
2168 (0 == memcmp (frc->udp_addr, e->udp_addr, frc->udp_addr_len)))
2178 * Functions with this signature are called whenever we need to close
2179 * a session due to a disconnect or failure to establish a connection.
2181 * @param cls closure with the `struct Plugin`
2182 * @param s session to close down
2183 * @return #GNUNET_OK on success
2186 udp_disconnect_session (void *cls, struct GNUNET_ATS_Session *s)
2188 struct Plugin *plugin = cls;
2189 struct UDP_MessageWrapper *udpw;
2190 struct UDP_MessageWrapper *next;
2191 struct FindReceiveContext frc;
2193 GNUNET_assert (GNUNET_YES != s->in_destroy);
2194 LOG (GNUNET_ERROR_TYPE_DEBUG,
2195 "Session %p to peer `%s' at address %s ended\n",
2197 GNUNET_i2s (&s->target),
2198 udp_address_to_string (plugin,
2199 s->address->address,
2200 s->address->address_length));
2201 if (NULL != s->timeout_task)
2203 GNUNET_SCHEDULER_cancel (s->timeout_task);
2204 s->timeout_task = NULL;
2206 if (NULL != s->frag_ctx)
2208 /* Remove fragmented message due to disconnect */
2209 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
2213 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions, &s->target, s));
2215 frc.udp_addr = s->address->address;
2216 frc.udp_addr_len = s->address->address_length;
2217 /* Lookup existing receive context for this address */
2218 if (NULL != plugin->defrag_ctxs)
2220 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2221 &find_receive_context,
2225 struct DefragContext *d_ctx = frc.rc;
2227 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2228 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2229 GNUNET_free (d_ctx);
2232 s->in_destroy = GNUNET_YES;
2233 next = plugin->ipv4_queue_head;
2234 while (NULL != (udpw = next))
2237 if (udpw->session == s)
2239 dequeue (plugin, udpw);
2240 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
2244 next = plugin->ipv6_queue_head;
2245 while (NULL != (udpw = next))
2248 if (udpw->session == s)
2250 dequeue (plugin, udpw);
2251 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
2255 if ((NULL != s->frag_ctx) && (NULL != s->frag_ctx->cont))
2257 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2258 later, as it might be in use right now */
2259 LOG (GNUNET_ERROR_TYPE_DEBUG,
2260 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2261 GNUNET_i2s (&s->target));
2262 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2265 s->frag_ctx->payload_size,
2266 s->frag_ctx->on_wire_size);
2268 notify_session_monitor (s->plugin, s, GNUNET_TRANSPORT_SS_DONE);
2269 plugin->env->session_end (plugin->env->cls, s->address, s);
2270 GNUNET_STATISTICS_set (plugin->env->stats,
2271 "# UDP sessions active",
2272 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2281 * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
2283 * @param plugin the UDP plugin
2284 * @param msg the (presumed) UDP ACK message
2285 * @param udp_addr sender address
2286 * @param udp_addr_len number of bytes in @a udp_addr
2289 read_process_ack (struct Plugin *plugin,
2290 const struct GNUNET_MessageHeader *msg,
2291 const union UdpAddress *udp_addr,
2292 socklen_t udp_addr_len)
2294 const struct GNUNET_MessageHeader *ack;
2295 const struct UDP_ACK_Message *udp_ack;
2296 struct GNUNET_HELLO_Address *address;
2297 struct GNUNET_ATS_Session *s;
2298 struct GNUNET_TIME_Relative flow_delay;
2300 /* check message format */
2301 if (ntohs (msg->size) <
2302 sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2304 GNUNET_break_op (0);
2307 udp_ack = (const struct UDP_ACK_Message *) msg;
2308 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2309 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2311 GNUNET_break_op (0);
2315 /* Locate session */
2316 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2320 GNUNET_HELLO_ADDRESS_INFO_NONE);
2321 s = udp_plugin_lookup_session (plugin, address);
2324 LOG (GNUNET_ERROR_TYPE_WARNING,
2325 "UDP session of address %s for ACK not found\n",
2326 udp_address_to_string (plugin,
2328 address->address_length));
2329 GNUNET_HELLO_address_free (address);
2332 if (NULL == s->frag_ctx)
2334 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2335 "Fragmentation context of address %s for ACK (%s) not found\n",
2336 udp_address_to_string (plugin,
2338 address->address_length),
2339 GNUNET_FRAGMENT_print_ack (ack));
2340 GNUNET_HELLO_address_free (address);
2343 GNUNET_HELLO_address_free (address);
2345 /* evaluate flow delay: how long should we wait between messages? */
2346 if (UINT32_MAX == ntohl (udp_ack->delay))
2348 /* Other peer asked for us to terminate the session */
2349 LOG (GNUNET_ERROR_TYPE_INFO,
2350 "Asked to disconnect UDP session of %s\n",
2351 GNUNET_i2s (&udp_ack->sender));
2352 udp_disconnect_session (plugin, s);
2355 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2356 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2357 LOG (GNUNET_ERROR_TYPE_WARNING,
2358 "We received a sending delay of %s for %s\n",
2359 GNUNET_STRINGS_relative_time_to_string (flow_delay, GNUNET_YES),
2360 GNUNET_i2s (&udp_ack->sender));
2362 LOG (GNUNET_ERROR_TYPE_DEBUG,
2363 "We received a sending delay of %s for %s\n",
2364 GNUNET_STRINGS_relative_time_to_string (flow_delay, GNUNET_YES),
2365 GNUNET_i2s (&udp_ack->sender));
2366 /* Flow delay is for the reassembled packet, however, our delay
2367 is per packet, so we need to adjust: */
2368 s->flow_delay_from_other_peer = flow_delay;
2371 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2373 LOG (GNUNET_ERROR_TYPE_DEBUG,
2374 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2375 (unsigned int) ntohs (msg->size),
2376 GNUNET_i2s (&udp_ack->sender),
2377 udp_address_to_string (plugin, udp_addr, udp_addr_len));
2378 /* Expect more ACKs to arrive */
2382 /* Remove fragmented message after successful sending */
2383 LOG (GNUNET_ERROR_TYPE_DEBUG,
2384 "Message from %s at %s full ACK'ed\n",
2385 GNUNET_i2s (&udp_ack->sender),
2386 udp_address_to_string (plugin, udp_addr, udp_addr_len));
2387 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2392 * Message tokenizer has broken up an incomming message. Pass it on
2395 * @param cls the `struct GNUNET_ATS_Session *`
2396 * @param hdr the actual message
2397 * @return #GNUNET_OK (always)
2400 process_inbound_tokenized_messages (void *cls,
2401 const struct GNUNET_MessageHeader *hdr)
2403 struct GNUNET_ATS_Session *session = cls;
2404 struct Plugin *plugin = session->plugin;
2406 if (GNUNET_YES == session->in_destroy)
2408 reschedule_session_timeout (session);
2409 session->flow_delay_for_other_peer =
2410 plugin->env->receive (plugin->env->cls, session->address, session, hdr);
2416 * Destroy a session, plugin is being unloaded.
2418 * @param cls the `struct Plugin`
2419 * @param key hash of public key of target peer
2420 * @param value a `struct PeerSession *` to clean up
2421 * @return #GNUNET_OK (continue to iterate)
2424 disconnect_and_free_it (void *cls,
2425 const struct GNUNET_PeerIdentity *key,
2428 struct Plugin *plugin = cls;
2430 udp_disconnect_session (plugin, value);
2436 * Disconnect from a remote node. Clean up session if we have one for
2439 * @param cls closure for this call (should be handle to Plugin)
2440 * @param target the peeridentity of the peer to disconnect
2441 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2444 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
2446 struct Plugin *plugin = cls;
2448 LOG (GNUNET_ERROR_TYPE_DEBUG,
2449 "Disconnecting from peer `%s'\n",
2450 GNUNET_i2s (target));
2451 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2453 &disconnect_and_free_it,
2459 * Session was idle, so disconnect it.
2461 * @param cls the `struct GNUNET_ATS_Session` to time out
2464 session_timeout (void *cls)
2466 struct GNUNET_ATS_Session *s = cls;
2467 struct Plugin *plugin = s->plugin;
2468 struct GNUNET_TIME_Relative left;
2470 s->timeout_task = NULL;
2471 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2472 if (left.rel_value_us > 0)
2474 /* not actually our turn yet, but let's at least update
2475 the monitor, it may think we're about to die ... */
2476 notify_session_monitor (s->plugin, s, GNUNET_TRANSPORT_SS_UPDATE);
2477 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left, &session_timeout, s);
2480 LOG (GNUNET_ERROR_TYPE_DEBUG,
2481 "Session %p was idle for %s, disconnecting\n",
2483 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2485 /* call session destroy function */
2486 udp_disconnect_session (plugin, s);
2491 * Allocate a new session for the given endpoint address.
2492 * Note that this function does not inform the service
2493 * of the new session, this is the responsibility of the
2494 * caller (if needed).
2496 * @param cls the `struct Plugin`
2497 * @param address address of the other peer to use
2498 * @param network_type network type the address belongs to
2499 * @return NULL on error, otherwise session handle
2501 static struct GNUNET_ATS_Session *
2502 udp_plugin_create_session (void *cls,
2503 const struct GNUNET_HELLO_Address *address,
2504 enum GNUNET_NetworkType network_type)
2506 struct Plugin *plugin = cls;
2507 struct GNUNET_ATS_Session *s;
2509 s = GNUNET_new (struct GNUNET_ATS_Session);
2510 s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages, s);
2512 s->address = GNUNET_HELLO_address_copy (address);
2513 s->target = address->peer;
2514 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2515 s->last_expected_ack_delay =
2516 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
2517 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2518 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2519 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2520 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2522 GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT, &session_timeout, s);
2523 s->scope = network_type;
2525 LOG (GNUNET_ERROR_TYPE_DEBUG,
2526 "Creating new session %p for peer `%s' address `%s'\n",
2528 GNUNET_i2s (&address->peer),
2529 udp_address_to_string (plugin,
2531 address->address_length));
2532 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (
2536 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2537 GNUNET_STATISTICS_set (plugin->env->stats,
2538 "# UDP sessions active",
2539 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2541 notify_session_monitor (plugin, s, GNUNET_TRANSPORT_SS_INIT);
2547 * Creates a new outbound session the transport service will use to
2548 * send data to the peer.
2550 * @param cls the `struct Plugin *`
2551 * @param address the address
2552 * @return the session or NULL of max connections exceeded
2554 static struct GNUNET_ATS_Session *
2555 udp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
2557 struct Plugin *plugin = cls;
2558 struct GNUNET_ATS_Session *s;
2559 enum GNUNET_NetworkType network_type = GNUNET_NT_UNSPECIFIED;
2560 const struct IPv4UdpAddress *udp_v4;
2561 const struct IPv6UdpAddress *udp_v6;
2563 if (NULL == address)
2568 if ((address->address_length != sizeof(struct IPv4UdpAddress)) &&
2569 (address->address_length != sizeof(struct IPv6UdpAddress)))
2571 GNUNET_break_op (0);
2574 if (NULL != (s = udp_plugin_lookup_session (cls, address)))
2577 /* need to create new session */
2578 if (sizeof(struct IPv4UdpAddress) == address->address_length)
2580 struct sockaddr_in v4;
2582 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2583 memset (&v4, '\0', sizeof(v4));
2584 v4.sin_family = AF_INET;
2585 #if HAVE_SOCKADDR_IN_SIN_LEN
2586 v4.sin_len = sizeof(struct sockaddr_in);
2588 v4.sin_port = udp_v4->u4_port;
2589 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2590 network_type = plugin->env->get_address_type (plugin->env->cls,
2591 (const struct sockaddr *) &v4,
2594 if (sizeof(struct IPv6UdpAddress) == address->address_length)
2596 struct sockaddr_in6 v6;
2598 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2599 memset (&v6, '\0', sizeof(v6));
2600 v6.sin6_family = AF_INET6;
2601 #if HAVE_SOCKADDR_IN_SIN_LEN
2602 v6.sin6_len = sizeof(struct sockaddr_in6);
2604 v6.sin6_port = udp_v6->u6_port;
2605 v6.sin6_addr = udp_v6->ipv6_addr;
2606 network_type = plugin->env->get_address_type (plugin->env->cls,
2607 (const struct sockaddr *) &v6,
2610 GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
2611 return udp_plugin_create_session (cls, address, network_type);
2616 * We've received a UDP Message. Process it (pass contents to main service).
2618 * @param plugin plugin context
2619 * @param msg the message
2620 * @param udp_addr sender address
2621 * @param udp_addr_len number of bytes in @a udp_addr
2622 * @param network_type network type the address belongs to
2625 process_udp_message (struct Plugin *plugin,
2626 const struct UDPMessage *msg,
2627 const union UdpAddress *udp_addr,
2628 size_t udp_addr_len,
2629 enum GNUNET_NetworkType network_type)
2631 struct GNUNET_ATS_Session *s;
2632 struct GNUNET_HELLO_Address *address;
2634 GNUNET_break (GNUNET_NT_UNSPECIFIED != network_type);
2635 if (0 != ntohl (msg->reserved))
2637 GNUNET_break_op (0);
2640 if (ntohs (msg->header.size) <
2641 sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2643 GNUNET_break_op (0);
2647 address = GNUNET_HELLO_address_allocate (&msg->sender,
2651 GNUNET_HELLO_ADDRESS_INFO_NONE);
2652 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2654 s = udp_plugin_create_session (plugin, address, network_type);
2655 plugin->env->session_start (plugin->env->cls, address, s, s->scope);
2656 notify_session_monitor (plugin, s, GNUNET_TRANSPORT_SS_UP);
2658 GNUNET_free (address);
2661 GNUNET_MST_from_buffer (s->mst,
2662 (const char *) &msg[1],
2663 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2667 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
2673 * Process a defragmented message.
2675 * @param cls the `struct DefragContext *`
2676 * @param msg the message
2679 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2681 struct DefragContext *dc = cls;
2682 const struct UDPMessage *um;
2684 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2686 GNUNET_break_op (0);
2689 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2691 GNUNET_break_op (0);
2694 um = (const struct UDPMessage *) msg;
2695 dc->sender = um->sender;
2696 dc->have_sender = GNUNET_YES;
2697 process_udp_message (dc->plugin,
2706 * We finished sending an acknowledgement. Update
2709 * @param cls the `struct Plugin`
2710 * @param udpw message queue entry of the ACK
2711 * @param result #GNUNET_OK if the transmission worked,
2712 * #GNUNET_SYSERR if we failed to send the ACK
2715 ack_message_sent (void *cls, struct UDP_MessageWrapper *udpw, int result)
2717 struct Plugin *plugin = cls;
2719 if (GNUNET_OK == result)
2721 GNUNET_STATISTICS_update (plugin->env->stats,
2722 "# UDP, ACK messages sent",
2728 GNUNET_STATISTICS_update (plugin->env->stats,
2729 "# UDP, ACK transmissions failed",
2737 * Transmit an acknowledgement.
2739 * @param cls the `struct DefragContext *`
2740 * @param id message ID (unused)
2741 * @param msg ack to transmit
2744 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2746 struct DefragContext *rc = cls;
2747 struct Plugin *plugin = rc->plugin;
2748 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2749 struct UDP_ACK_Message *udp_ack;
2751 struct UDP_MessageWrapper *udpw;
2752 struct GNUNET_ATS_Session *s;
2753 struct GNUNET_HELLO_Address *address;
2755 if (GNUNET_NO == rc->have_sender)
2757 /* tried to defragment but never succeeded, hence will not ACK */
2758 /* This can happen if we just lost msgs */
2759 GNUNET_STATISTICS_update (plugin->env->stats,
2760 "# UDP, fragments discarded without ACK",
2765 address = GNUNET_HELLO_address_allocate (&rc->sender,
2769 GNUNET_HELLO_ADDRESS_INFO_NONE);
2770 s = udp_plugin_lookup_session (plugin, address);
2771 GNUNET_HELLO_address_free (address);
2774 LOG (GNUNET_ERROR_TYPE_ERROR,
2775 "Trying to transmit ACK to peer `%s' but no session found!\n",
2776 udp_address_to_string (plugin, rc->udp_addr, rc->udp_addr_len));
2777 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2778 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2780 GNUNET_STATISTICS_update (plugin->env->stats,
2781 "# UDP, ACK transmissions failed",
2786 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2787 s->flow_delay_for_other_peer.rel_value_us)
2789 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2790 delay = s->flow_delay_for_other_peer.rel_value_us;
2792 delay = UINT32_MAX - 1; /* largest value we can communicate */
2793 LOG (GNUNET_ERROR_TYPE_DEBUG,
2794 "Sending ACK to `%s' including delay of %s\n",
2795 udp_address_to_string (plugin, rc->udp_addr, rc->udp_addr_len),
2796 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2798 udpw = GNUNET_malloc (sizeof(struct UDP_MessageWrapper) + msize);
2799 udpw->msg_size = msize;
2800 udpw->payload_size = 0;
2802 udpw->start_time = GNUNET_TIME_absolute_get ();
2803 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2804 udpw->msg_buf = (char *) &udpw[1];
2805 udpw->qc = &ack_message_sent;
2806 udpw->qc_cls = plugin;
2807 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2808 udp_ack->header.size = htons ((uint16_t) msize);
2809 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2810 udp_ack->delay = htonl (delay);
2811 udp_ack->sender = *plugin->env->my_identity;
2812 GNUNET_memcpy (&udp_ack[1], msg, ntohs (msg->size));
2813 enqueue (plugin, udpw);
2814 notify_session_monitor (plugin, s, GNUNET_TRANSPORT_SS_UPDATE);
2815 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
2816 schedule_select_v4 (plugin);
2818 schedule_select_v6 (plugin);
2823 * We received a fragment, process it.
2825 * @param plugin our plugin
2826 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2827 * @param udp_addr sender address
2828 * @param udp_addr_len number of bytes in @a udp_addr
2829 * @param network_type network type the address belongs to
2832 read_process_fragment (struct Plugin *plugin,
2833 const struct GNUNET_MessageHeader *msg,
2834 const union UdpAddress *udp_addr,
2835 size_t udp_addr_len,
2836 enum GNUNET_NetworkType network_type)
2838 struct DefragContext *d_ctx;
2839 struct GNUNET_TIME_Absolute now;
2840 struct FindReceiveContext frc;
2843 frc.udp_addr = udp_addr;
2844 frc.udp_addr_len = udp_addr_len;
2846 /* Lookup existing receive context for this address */
2847 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2848 &find_receive_context,
2850 now = GNUNET_TIME_absolute_get ();
2855 /* Create a new defragmentation context */
2856 d_ctx = GNUNET_malloc (sizeof(struct DefragContext) + udp_addr_len);
2857 GNUNET_memcpy (&d_ctx[1], udp_addr, udp_addr_len);
2858 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2859 d_ctx->udp_addr_len = udp_addr_len;
2860 d_ctx->network_type = network_type;
2861 d_ctx->plugin = plugin;
2863 GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2865 UDP_MAX_MESSAGES_IN_DEFRAG,
2869 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2871 (GNUNET_CONTAINER_HeapCostType)
2873 LOG (GNUNET_ERROR_TYPE_DEBUG,
2874 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2875 (unsigned int) ntohs (msg->size),
2876 udp_address_to_string (plugin, udp_addr, udp_addr_len));
2880 LOG (GNUNET_ERROR_TYPE_DEBUG,
2881 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2882 (unsigned int) ntohs (msg->size),
2883 udp_address_to_string (plugin, udp_addr, udp_addr_len));
2886 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2888 /* keep this 'rc' from expiring */
2889 GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
2890 (GNUNET_CONTAINER_HeapCostType)
2893 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2894 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2896 /* remove 'rc' that was inactive the longest */
2897 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2898 GNUNET_assert (NULL != d_ctx);
2899 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2900 GNUNET_free (d_ctx);
2901 GNUNET_STATISTICS_update (plugin->env->stats,
2902 "# UDP, Defragmentations aborted",
2910 * Read and process a message from the given socket.
2912 * @param plugin the overall plugin
2913 * @param rsock socket to read from
2916 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2919 struct sockaddr_storage addr;
2920 char buf[65536] GNUNET_ALIGN;
2922 const struct GNUNET_MessageHeader *msg;
2923 struct IPv4UdpAddress v4;
2924 struct IPv6UdpAddress v6;
2925 const struct sockaddr *sa;
2926 const struct sockaddr_in *sa4;
2927 const struct sockaddr_in6 *sa6;
2928 const union UdpAddress *int_addr;
2929 size_t int_addr_len;
2930 enum GNUNET_NetworkType network_type;
2932 fromlen = sizeof(addr);
2933 memset (&addr, 0, sizeof(addr));
2934 size = GNUNET_NETWORK_socket_recvfrom (rsock,
2937 (struct sockaddr *) &addr,
2939 sa = (const struct sockaddr *) &addr;
2943 LOG (GNUNET_ERROR_TYPE_DEBUG,
2944 "UDP failed to receive data: %s\n",
2946 /* Connection failure or something. Not a protocol violation. */
2950 /* Check if this is a STUN packet */
2952 GNUNET_NAT_stun_handle_packet (plugin->nat,
2953 (const struct sockaddr *) &addr,
2957 return; /* was STUN, do not process further */
2959 if (size < sizeof(struct GNUNET_MessageHeader))
2961 LOG (GNUNET_ERROR_TYPE_WARNING,
2962 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2963 (unsigned int) size,
2964 GNUNET_a2s (sa, fromlen));
2965 /* _MAY_ be a connection failure (got partial message) */
2966 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2967 GNUNET_break_op (0);
2971 msg = (const struct GNUNET_MessageHeader *) buf;
2972 LOG (GNUNET_ERROR_TYPE_DEBUG,
2973 "UDP received %u-byte message from `%s' type %u\n",
2974 (unsigned int) size,
2975 GNUNET_a2s (sa, fromlen),
2977 if (size != ntohs (msg->size))
2979 LOG (GNUNET_ERROR_TYPE_WARNING,
2980 "UDP malformed message (size %u) header from %s\n",
2981 (unsigned int) size,
2982 GNUNET_a2s (sa, fromlen));
2983 GNUNET_break_op (0);
2986 GNUNET_STATISTICS_update (plugin->env->stats,
2987 "# UDP, total bytes received",
2990 network_type = plugin->env->get_address_type (plugin->env->cls, sa, fromlen);
2991 switch (sa->sa_family)
2994 sa4 = (const struct sockaddr_in *) &addr;
2996 v4.ipv4_addr = sa4->sin_addr.s_addr;
2997 v4.u4_port = sa4->sin_port;
2998 int_addr = (union UdpAddress *) &v4;
2999 int_addr_len = sizeof(v4);
3003 sa6 = (const struct sockaddr_in6 *) &addr;
3005 v6.ipv6_addr = sa6->sin6_addr;
3006 v6.u6_port = sa6->sin6_port;
3007 int_addr = (union UdpAddress *) &v6;
3008 int_addr_len = sizeof(v6);
3016 switch (ntohs (msg->type))
3018 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3019 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3020 udp_broadcast_receive (plugin,
3028 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3029 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3031 GNUNET_break_op (0);
3034 process_udp_message (plugin,
3035 (const struct UDPMessage *) msg,
3041 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3042 read_process_ack (plugin, msg, int_addr, int_addr_len);
3045 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3046 read_process_fragment (plugin, msg, int_addr, int_addr_len, network_type);
3050 GNUNET_break_op (0);
3057 * Removes messages from the transmission queue that have
3058 * timed out, and then selects a message that should be
3061 * @param plugin the UDP plugin
3062 * @param sock which socket should we process the queue for (v4 or v6)
3063 * @return message selected for transmission, or NULL for none
3065 static struct UDP_MessageWrapper *
3066 remove_timeout_messages_and_select (struct Plugin *plugin,
3067 struct GNUNET_NETWORK_Handle *sock)
3069 struct UDP_MessageWrapper *udpw;
3070 struct GNUNET_TIME_Relative remaining;
3071 struct GNUNET_ATS_Session *session;
3074 removed = GNUNET_NO;
3075 udpw = (sock == plugin->sockv4) ? plugin->ipv4_queue_head
3076 : plugin->ipv6_queue_head;
3077 while (NULL != udpw)
3079 session = udpw->session;
3080 /* Find messages with timeout */
3081 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3082 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3084 /* Message timed out */
3085 removed = GNUNET_YES;
3086 dequeue (plugin, udpw);
3087 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
3090 if (sock == plugin->sockv4)
3092 udpw = plugin->ipv4_queue_head;
3094 else if (sock == plugin->sockv6)
3096 udpw = plugin->ipv6_queue_head;
3100 GNUNET_break (0); /* should never happen */
3103 GNUNET_STATISTICS_update (plugin->env->stats,
3104 "# messages discarded due to timeout",
3110 /* Message did not time out, check transmission time */
3111 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3112 if (0 == remaining.rel_value_us)
3114 /* this message is not delayed */
3115 LOG (GNUNET_ERROR_TYPE_DEBUG,
3116 "Message for peer `%s' (%u bytes) is not delayed \n",
3117 GNUNET_i2s (&udpw->session->target),
3118 udpw->payload_size);
3119 break; /* Found message to send, break */
3123 /* Message is delayed, try next */
3124 LOG (GNUNET_ERROR_TYPE_DEBUG,
3125 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3126 GNUNET_i2s (&udpw->session->target),
3128 GNUNET_STRINGS_relative_time_to_string (remaining, GNUNET_YES));
3133 if (GNUNET_YES == removed)
3134 notify_session_monitor (session->plugin,
3136 GNUNET_TRANSPORT_SS_UPDATE);
3142 * We failed to transmit a message via UDP. Generate
3143 * a descriptive error message.
3145 * @param plugin our plugin
3146 * @param sa target address we were trying to reach
3147 * @param slen number of bytes in @a sa
3148 * @param error the errno value returned from the sendto() call
3151 analyze_send_error (struct Plugin *plugin,
3152 const struct sockaddr *sa,
3156 enum GNUNET_NetworkType type;
3158 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
3159 if (((GNUNET_NT_LAN == type) || (GNUNET_NT_WAN == type)) &&
3160 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
3162 if (slen == sizeof(struct sockaddr_in))
3164 /* IPv4: "Network unreachable" or "Network down"
3166 * This indicates we do not have connectivity
3168 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3169 _ ("UDP could not transmit message to `%s': "
3170 "Network seems down, please check your network configuration\n"),
3171 GNUNET_a2s (sa, slen));
3173 if (slen == sizeof(struct sockaddr_in6))
3175 /* IPv6: "Network unreachable" or "Network down"
3177 * This indicates that this system is IPv6 enabled, but does not
3178 * have a valid global IPv6 address assigned or we do not have
3180 */LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3182 "UDP could not transmit IPv6 message! "
3183 "Please check your network configuration and disable IPv6 if your "
3184 "connection does not have a global IPv6 address\n"));
3189 LOG (GNUNET_ERROR_TYPE_WARNING,
3190 "UDP could not transmit message to `%s': `%s'\n",
3191 GNUNET_a2s (sa, slen),
3198 * It is time to try to transmit a UDP message. Select one
3201 * @param plugin the plugin
3202 * @param sock which socket (v4/v6) to send on
3205 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
3209 const struct sockaddr *a;
3210 const struct IPv4UdpAddress *u4;
3211 struct sockaddr_in a4;
3212 const struct IPv6UdpAddress *u6;
3213 struct sockaddr_in6 a6;
3214 struct UDP_MessageWrapper *udpw;
3216 /* Find message(s) to send */
3217 while (NULL != (udpw = remove_timeout_messages_and_select (plugin, sock)))
3219 if (sizeof(struct IPv4UdpAddress) ==
3220 udpw->session->address->address_length)
3222 u4 = udpw->session->address->address;
3223 memset (&a4, 0, sizeof(a4));
3224 a4.sin_family = AF_INET;
3225 #if HAVE_SOCKADDR_IN_SIN_LEN
3226 a4.sin_len = sizeof(a4);
3228 a4.sin_port = u4->u4_port;
3229 a4.sin_addr.s_addr = u4->ipv4_addr;
3230 a = (const struct sockaddr *) &a4;
3233 else if (sizeof(struct IPv6UdpAddress) ==
3234 udpw->session->address->address_length)
3236 u6 = udpw->session->address->address;
3237 memset (&a6, 0, sizeof(a6));
3238 a6.sin6_family = AF_INET6;
3239 #if HAVE_SOCKADDR_IN_SIN_LEN
3240 a6.sin6_len = sizeof(a6);
3242 a6.sin6_port = u6->u6_port;
3243 a6.sin6_addr = u6->ipv6_addr;
3244 a = (const struct sockaddr *) &a6;
3250 dequeue (plugin, udpw);
3251 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
3252 notify_session_monitor (plugin,
3254 GNUNET_TRANSPORT_SS_UPDATE);
3258 sent = GNUNET_NETWORK_socket_sendto (sock,
3263 udpw->session->last_transmit_time =
3264 GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3265 udpw->session->last_transmit_time);
3266 dequeue (plugin, udpw);
3267 if (GNUNET_SYSERR == sent)
3270 analyze_send_error (plugin, a, slen, errno);
3271 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
3272 GNUNET_STATISTICS_update (plugin->env->stats,
3273 "# UDP, total, bytes, sent, failure",
3276 GNUNET_STATISTICS_update (plugin->env->stats,
3277 "# UDP, total, messages, sent, failure",
3284 LOG (GNUNET_ERROR_TYPE_DEBUG,
3285 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3286 (unsigned int) (udpw->msg_size),
3287 GNUNET_i2s (&udpw->session->target),
3288 GNUNET_a2s (a, slen),
3290 (sent < 0) ? strerror (errno) : "ok");
3291 GNUNET_STATISTICS_update (plugin->env->stats,
3292 "# UDP, total, bytes, sent, success",
3295 GNUNET_STATISTICS_update (plugin->env->stats,
3296 "# UDP, total, messages, sent, success",
3299 if (NULL != udpw->frag_ctx)
3300 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3301 udpw->qc (udpw->qc_cls, udpw, GNUNET_OK);
3303 notify_session_monitor (plugin, udpw->session, GNUNET_TRANSPORT_SS_UPDATE);
3309 /* ***************** Event loop (part 2) *************** */
3313 * We have been notified that our readset has something to read. We don't
3314 * know which socket needs to be read, so we have to check each one
3315 * Then reschedule this function to be called again once more is available.
3317 * @param cls the plugin handle
3320 udp_plugin_select_v4 (void *cls)
3322 struct Plugin *plugin = cls;
3323 const struct GNUNET_SCHEDULER_TaskContext *tc;
3325 plugin->select_task_v4 = NULL;
3326 if (NULL == plugin->sockv4)
3328 tc = GNUNET_SCHEDULER_get_task_context ();
3329 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3330 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
3331 udp_select_read (plugin, plugin->sockv4);
3332 udp_select_send (plugin, plugin->sockv4);
3333 schedule_select_v4 (plugin);
3338 * We have been notified that our readset has something to read. We don't
3339 * know which socket needs to be read, so we have to check each one
3340 * Then reschedule this function to be called again once more is available.
3342 * @param cls the plugin handle
3345 udp_plugin_select_v6 (void *cls)
3347 struct Plugin *plugin = cls;
3348 const struct GNUNET_SCHEDULER_TaskContext *tc;
3350 plugin->select_task_v6 = NULL;
3351 if (NULL == plugin->sockv6)
3353 tc = GNUNET_SCHEDULER_get_task_context ();
3354 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3355 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
3356 udp_select_read (plugin, plugin->sockv6);
3358 udp_select_send (plugin, plugin->sockv6);
3359 schedule_select_v6 (plugin);
3363 /* ******************* Initialization *************** */
3367 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3369 * @param plugin the plugin to initialize
3370 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3371 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3372 * @return number of sockets that were successfully bound
3375 setup_sockets (struct Plugin *plugin,
3376 const struct sockaddr_in6 *bind_v6,
3377 const struct sockaddr_in *bind_v4)
3380 unsigned int sockets_created = 0;
3381 struct sockaddr_in6 server_addrv6;
3382 struct sockaddr_in server_addrv4;
3383 const struct sockaddr *server_addr;
3384 const struct sockaddr *addrs[2];
3385 socklen_t addrlens[2];
3389 /* Create IPv6 socket */
3391 if (GNUNET_YES == plugin->enable_ipv6)
3393 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
3394 if (NULL == plugin->sockv6)
3396 LOG (GNUNET_ERROR_TYPE_INFO,
3397 _ ("Disabling IPv6 since it is not supported on this system!\n"));
3398 plugin->enable_ipv6 = GNUNET_NO;
3402 memset (&server_addrv6, 0, sizeof(struct sockaddr_in6));
3403 #if HAVE_SOCKADDR_IN_SIN_LEN
3404 server_addrv6.sin6_len = sizeof(struct sockaddr_in6);
3406 server_addrv6.sin6_family = AF_INET6;
3407 if (NULL != bind_v6)
3408 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3410 server_addrv6.sin6_addr = in6addr_any;
3412 if (0 == plugin->port) /* autodetect */
3413 server_addrv6.sin6_port = htons (
3414 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3417 server_addrv6.sin6_port = htons (plugin->port);
3418 addrlen = sizeof(struct sockaddr_in6);
3419 server_addr = (const struct sockaddr *) &server_addrv6;
3424 LOG (GNUNET_ERROR_TYPE_DEBUG,
3425 "Binding to IPv6 `%s'\n",
3426 GNUNET_a2s (server_addr, addrlen));
3429 GNUNET_NETWORK_socket_bind (plugin->sockv6, server_addr, addrlen))
3432 if (0 != plugin->port)
3434 tries = 10; /* fail immediately */
3435 break; /* bind failed on specific port */
3438 server_addrv6.sin6_port = htons (
3439 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3445 GNUNET_NETWORK_socket_close (plugin->sockv6);
3446 plugin->enable_ipv6 = GNUNET_NO;
3447 plugin->sockv6 = NULL;
3451 plugin->port = ntohs (server_addrv6.sin6_port);
3453 if (NULL != plugin->sockv6)
3455 LOG (GNUNET_ERROR_TYPE_DEBUG,
3456 "IPv6 UDP socket created listinging at %s\n",
3457 GNUNET_a2s (server_addr, addrlen));
3458 addrs[sockets_created] = server_addr;
3459 addrlens[sockets_created] = addrlen;
3464 LOG (GNUNET_ERROR_TYPE_WARNING,
3465 _ ("Failed to bind UDP socket to %s: %s\n"),
3466 GNUNET_a2s (server_addr, addrlen),
3472 /* Create IPv4 socket */
3474 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
3475 if (NULL == plugin->sockv4)
3477 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
3478 LOG (GNUNET_ERROR_TYPE_INFO,
3479 _ ("Disabling IPv4 since it is not supported on this system!\n"));
3480 plugin->enable_ipv4 = GNUNET_NO;
3484 memset (&server_addrv4, 0, sizeof(struct sockaddr_in));
3485 #if HAVE_SOCKADDR_IN_SIN_LEN
3486 server_addrv4.sin_len = sizeof(struct sockaddr_in);
3488 server_addrv4.sin_family = AF_INET;
3489 if (NULL != bind_v4)
3490 server_addrv4.sin_addr = bind_v4->sin_addr;
3492 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3494 if (0 == plugin->port)
3496 server_addrv4.sin_port = htons (
3497 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
3499 server_addrv4.sin_port = htons (plugin->port);
3501 addrlen = sizeof(struct sockaddr_in);
3502 server_addr = (const struct sockaddr *) &server_addrv4;
3507 LOG (GNUNET_ERROR_TYPE_DEBUG,
3508 "Binding to IPv4 `%s'\n",
3509 GNUNET_a2s (server_addr, addrlen));
3513 GNUNET_NETWORK_socket_bind (plugin->sockv4, server_addr, addrlen))
3516 if (0 != plugin->port)
3518 tries = 10; /* fail */
3519 break; /* bind failed on specific port */
3523 server_addrv4.sin_port = htons (
3524 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
3529 GNUNET_NETWORK_socket_close (plugin->sockv4);
3530 plugin->enable_ipv4 = GNUNET_NO;
3531 plugin->sockv4 = NULL;
3535 plugin->port = ntohs (server_addrv4.sin_port);
3538 if (NULL != plugin->sockv4)
3540 LOG (GNUNET_ERROR_TYPE_DEBUG,
3541 "IPv4 socket created on port %s\n",
3542 GNUNET_a2s (server_addr, addrlen));
3543 addrs[sockets_created] = server_addr;
3544 addrlens[sockets_created] = addrlen;
3549 LOG (GNUNET_ERROR_TYPE_ERROR,
3550 _ ("Failed to bind UDP socket to %s: %s\n"),
3551 GNUNET_a2s (server_addr, addrlen),
3556 if (0 == sockets_created)
3558 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Failed to open UDP sockets\n"));
3559 return 0; /* No sockets created, return */
3561 schedule_select_v4 (plugin);
3562 schedule_select_v6 (plugin);
3563 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3569 &udp_nat_port_map_callback,
3572 return sockets_created;
3577 * The exported method. Makes the core api available via a global and
3578 * returns the udp transport API.
3580 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3581 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3584 libgnunet_plugin_transport_udp_init (void *cls)
3586 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3587 struct GNUNET_TRANSPORT_PluginFunctions *api;
3589 unsigned long long port;
3590 unsigned long long aport;
3591 unsigned long long udp_max_bps;
3593 int enable_broadcasting;
3594 int enable_broadcasting_recv;
3595 char *bind4_address;
3596 char *bind6_address;
3597 struct GNUNET_TIME_Relative interval;
3598 struct sockaddr_in server_addrv4;
3599 struct sockaddr_in6 server_addrv6;
3604 if (NULL == env->receive)
3606 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3607 initialze the plugin or the API */
3608 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3610 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3611 api->address_to_string = &udp_address_to_string;
3612 api->string_to_address = &udp_string_to_address;
3616 /* Get port number: port == 0 : autodetect a port,
3617 * > 0 : use this port, not given : 2086 default */
3618 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg,
3625 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3628 _ ("must be in [0,65535]"));
3631 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg,
3638 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3641 _ ("must be in [0,65535]"));
3646 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6"))
3647 enable_v6 = GNUNET_NO;
3649 enable_v6 = GNUNET_YES;
3651 have_bind4 = GNUNET_NO;
3652 memset (&server_addrv4, 0, sizeof(server_addrv4));
3653 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3658 LOG (GNUNET_ERROR_TYPE_DEBUG,
3659 "Binding UDP plugin to specific address: `%s'\n",
3661 if (1 != inet_pton (AF_INET, bind4_address, &server_addrv4.sin_addr))
3663 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3666 _ ("must be valid IPv4 address"));
3667 GNUNET_free (bind4_address);
3670 have_bind4 = GNUNET_YES;
3672 GNUNET_free_non_null (bind4_address);
3673 have_bind6 = GNUNET_NO;
3674 memset (&server_addrv6, 0, sizeof(server_addrv6));
3675 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
3680 LOG (GNUNET_ERROR_TYPE_DEBUG,
3681 "Binding udp plugin to specific address: `%s'\n",
3683 if (1 != inet_pton (AF_INET6, bind6_address, &server_addrv6.sin6_addr))
3685 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3688 _ ("must be valid IPv6 address"));
3689 GNUNET_free (bind6_address);
3692 have_bind6 = GNUNET_YES;
3694 GNUNET_free_non_null (bind6_address);
3696 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3699 if (enable_broadcasting == GNUNET_SYSERR)
3700 enable_broadcasting = GNUNET_NO;
3702 enable_broadcasting_recv =
3703 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3705 "BROADCAST_RECEIVE");
3706 if (enable_broadcasting_recv == GNUNET_SYSERR)
3707 enable_broadcasting_recv = GNUNET_YES;
3709 if (GNUNET_SYSERR ==
3710 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3712 "BROADCAST_INTERVAL",
3715 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3717 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg,
3722 /* 50 MB/s == infinity for practical purposes */
3723 udp_max_bps = 1024 * 1024 * 50;
3726 p = GNUNET_new (struct Plugin);
3729 p->broadcast_interval = interval;
3730 p->enable_ipv6 = enable_v6;
3731 p->enable_ipv4 = GNUNET_YES; /* default */
3732 p->enable_broadcasting = enable_broadcasting;
3733 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3735 p->sessions = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_NO);
3737 GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3738 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3741 GNUNET_BANDWIDTH_value_init (
3742 (uint32_t) udp_max_bps),
3744 res = setup_sockets (p,
3745 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3746 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3747 if ((0 == res) || ((NULL == p->sockv4) && (NULL == p->sockv6)))
3749 LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Failed to create UDP network sockets\n"));
3750 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3751 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3753 GNUNET_NAT_unregister (p->nat);
3758 /* Setup broadcasting and receiving beacons */
3759 setup_broadcast (p, &server_addrv6, &server_addrv4);
3761 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3763 api->disconnect_session = &udp_disconnect_session;
3764 api->query_keepalive_factor = &udp_query_keepalive_factor;
3765 api->disconnect_peer = &udp_disconnect;
3766 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3767 api->address_to_string = &udp_address_to_string;
3768 api->string_to_address = &udp_string_to_address;
3769 api->check_address = &udp_plugin_check_address;
3770 api->get_session = &udp_plugin_get_session;
3771 api->send = &udp_plugin_send;
3772 api->get_network = &udp_plugin_get_network;
3773 api->get_network_for_address = &udp_plugin_get_network_for_address;
3774 api->update_session_timeout = &udp_plugin_update_session_timeout;
3775 api->setup_monitor = &udp_plugin_setup_monitor;
3781 * Function called on each entry in the defragmentation heap to
3785 * @param node node in the heap (to be removed)
3786 * @param element a `struct DefragContext` to be cleaned up
3787 * @param cost unused
3788 * @return #GNUNET_YES
3791 heap_cleanup_iterator (void *cls,
3792 struct GNUNET_CONTAINER_HeapNode *node,
3794 GNUNET_CONTAINER_HeapCostType cost)
3796 struct DefragContext *d_ctx = element;
3798 GNUNET_CONTAINER_heap_remove_node (node);
3799 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3800 GNUNET_free (d_ctx);
3806 * The exported method. Makes the core api available via a global and
3807 * returns the udp transport API.
3809 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3813 libgnunet_plugin_transport_udp_done (void *cls)
3815 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3816 struct Plugin *plugin = api->cls;
3817 struct PrettyPrinterContext *cur;
3818 struct UDP_MessageWrapper *udpw;
3825 stop_broadcast (plugin);
3826 if (NULL != plugin->select_task_v4)
3828 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
3829 plugin->select_task_v4 = NULL;
3831 if (NULL != plugin->select_task_v6)
3833 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3834 plugin->select_task_v6 = NULL;
3836 if (NULL != plugin->sockv4)
3838 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3839 plugin->sockv4 = NULL;
3841 if (NULL != plugin->sockv6)
3843 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3844 plugin->sockv6 = NULL;
3846 if (NULL != plugin->nat)
3848 GNUNET_NAT_unregister (plugin->nat);
3851 if (NULL != plugin->defrag_ctxs)
3853 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3854 &heap_cleanup_iterator,
3856 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3857 plugin->defrag_ctxs = NULL;
3859 while (NULL != (udpw = plugin->ipv4_queue_head))
3861 dequeue (plugin, udpw);
3862 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
3865 while (NULL != (udpw = plugin->ipv6_queue_head))
3867 dequeue (plugin, udpw);
3868 udpw->qc (udpw->qc_cls, udpw, GNUNET_SYSERR);
3871 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3872 &disconnect_and_free_it,
3874 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3876 while (NULL != (cur = plugin->ppc_dll_head))
3879 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3880 plugin->ppc_dll_tail,
3882 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3883 if (NULL != cur->timeout_task)
3885 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3886 cur->timeout_task = NULL;
3890 GNUNET_free (plugin);
3896 /* end of plugin_transport_udp.c */