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/>.
20 * @file transport/plugin_transport_udp.c
21 * @brief Implementation of the UDP transport protocol
22 * @author Christian Grothoff
23 * @author Nathan Evans
24 * @author Matthias Wachs
27 #include "plugin_transport_udp.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fragmentation_lib.h"
31 #include "gnunet_nat_service.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_signatures.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
44 * After how much inactivity should a UDP session time out?
46 #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
49 * Number of messages we can defragment in parallel. We only really
50 * defragment 1 message at a time, but if messages get re-ordered, we
51 * may want to keep knowledge about the previous message to avoid
52 * discarding the current message in favor of a single fragment of a
53 * previous message. 3 should be good since we don't expect massive
54 * message reorderings with UDP.
56 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
59 * We keep a defragmentation queue per sender address. How many
60 * sender addresses do we support at the same time? Memory consumption
61 * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
62 * value. (So 128 corresponds to 12 MB and should suffice for
63 * connecting to roughly 128 peers via UDP).
65 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
69 * UDP Message-Packet header (after defragmentation).
76 struct GNUNET_MessageHeader header;
79 * Always zero for now.
84 * What is the identity of the sender
86 struct GNUNET_PeerIdentity sender;
92 * Closure for #append_port().
94 struct PrettyPrinterContext
99 struct PrettyPrinterContext *next;
104 struct PrettyPrinterContext *prev;
109 struct Plugin *plugin;
114 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
117 * Function to call with the result.
119 GNUNET_TRANSPORT_AddressStringCallback asc;
122 * Clsoure for @e asc.
129 struct GNUNET_SCHEDULER_Task *timeout_task;
132 * Is this an IPv6 address?
142 * Port to add after the IP address.
150 * Session with another peer.
152 struct GNUNET_ATS_Session
155 * Which peer is this session for?
157 struct GNUNET_PeerIdentity target;
160 * Tokenizer for inbound messages.
162 struct GNUNET_MessageStreamTokenizer *mst;
165 * Plugin this session belongs to.
167 struct Plugin *plugin;
170 * Context for dealing with fragments.
172 struct UDP_FragmentationContext *frag_ctx;
175 * Desired delay for next sending we send to other peer
177 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
180 * Desired delay for transmissions we received from other peer.
181 * This is for full messages, the value needs to be adjusted for
182 * fragmented messages.
184 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
187 * Session timeout task
189 struct GNUNET_SCHEDULER_Task *timeout_task;
192 * When does this session time out?
194 struct GNUNET_TIME_Absolute timeout;
197 * What time did we last transmit?
199 struct GNUNET_TIME_Absolute last_transmit_time;
202 * expected delay for ACKs
204 struct GNUNET_TIME_Relative last_expected_ack_delay;
207 * desired delay between UDP messages
209 struct GNUNET_TIME_Relative last_expected_msg_delay;
214 struct GNUNET_HELLO_Address *address;
217 * Number of bytes waiting for transmission to this peer.
219 unsigned long long bytes_in_queue;
222 * Number of messages waiting for transmission to this peer.
224 unsigned int msgs_in_queue;
227 * Reference counter to indicate that this session is
228 * currently being used and must not be destroyed;
229 * setting @e in_destroy will destroy it as soon as
235 * Network type of the address.
237 enum GNUNET_ATS_Network_Type scope;
240 * Is this session about to be destroyed (sometimes we cannot
241 * destroy a session immediately as below us on the stack
242 * there might be code that still uses it; in this case,
243 * @e rc is non-zero).
251 * Data structure to track defragmentation contexts based
252 * on the source of the UDP traffic.
258 * Defragmentation context.
260 struct GNUNET_DEFRAGMENT_Context *defrag;
263 * Reference to master plugin struct.
265 struct Plugin *plugin;
268 * Node in the defrag heap.
270 struct GNUNET_CONTAINER_HeapNode *hnode;
273 * Source address this receive context is for (allocated at the
274 * end of the struct).
276 const union UdpAddress *udp_addr;
279 * Who's message(s) are we defragmenting here?
280 * Only initialized once we succeeded and
281 * @e have_sender is set.
283 struct GNUNET_PeerIdentity sender;
286 * Length of @e udp_addr.
291 * Network type the address belongs to.
293 enum GNUNET_ATS_Network_Type network_type;
296 * Has the @e sender field been initialized yet?
303 * Context to send fragmented messages
305 struct UDP_FragmentationContext
308 * Next in linked list
310 struct UDP_FragmentationContext *next;
313 * Previous in linked list
315 struct UDP_FragmentationContext *prev;
320 struct Plugin *plugin;
323 * Handle for fragmentation.
325 struct GNUNET_FRAGMENT_Context *frag;
328 * The session this fragmentation context belongs to
330 struct GNUNET_ATS_Session *session;
333 * Function to call upon completion of the transmission.
335 GNUNET_TRANSPORT_TransmitContinuation cont;
338 * Closure for @e cont.
345 struct GNUNET_TIME_Absolute start_time;
348 * Transmission time for the next fragment. Incremented by
349 * the @e flow_delay_from_other_peer for each fragment when
350 * we setup the fragments.
352 struct GNUNET_TIME_Absolute next_frag_time;
355 * Desired delay for transmissions we received from other peer.
356 * Adjusted to be per fragment (UDP_MTU), even though on the
357 * wire it was for "full messages".
359 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
364 struct GNUNET_TIME_Absolute timeout;
367 * Payload size of original unfragmented message
372 * Bytes used to send all fragments on wire including UDP overhead
380 * Function called when a message is removed from the
381 * transmission queue.
384 * @param udpw message wrapper finished
385 * @param result #GNUNET_OK on success (message was sent)
386 * #GNUNET_SYSERR if the target disconnected
387 * or we had a timeout or other trouble sending
390 (*QueueContinuation) (void *cls,
391 struct UDP_MessageWrapper *udpw,
396 * Information we track for each message in the queue.
398 struct UDP_MessageWrapper
401 * Session this message belongs to
403 struct GNUNET_ATS_Session *session;
406 * DLL of messages, previous element
408 struct UDP_MessageWrapper *prev;
411 * DLL of messages, next element
413 struct UDP_MessageWrapper *next;
416 * Message with @e msg_size bytes including UDP-specific overhead.
421 * Function to call once the message wrapper is being removed
422 * from the queue (with success or failure).
424 QueueContinuation qc;
432 * External continuation to call upon completion of the
433 * transmission, NULL if this queue entry is not for a
434 * message from the application.
436 GNUNET_TRANSPORT_TransmitContinuation cont;
439 * Closure for @e cont.
444 * Fragmentation context.
445 * frag_ctx == NULL if transport <= MTU
446 * frag_ctx != NULL if transport > MTU
448 struct UDP_FragmentationContext *frag_ctx;
451 * Message enqueue time.
453 struct GNUNET_TIME_Absolute start_time;
456 * Desired transmission time for this message, based on the
457 * flow limiting information we got from the other peer.
459 struct GNUNET_TIME_Absolute transmission_time;
464 struct GNUNET_TIME_Absolute timeout;
467 * Size of UDP message to send, including UDP-specific overhead.
472 * Payload size of original message.
479 GNUNET_NETWORK_STRUCT_BEGIN
482 * UDP ACK Message-Packet header.
484 struct UDP_ACK_Message
489 struct GNUNET_MessageHeader header;
492 * Desired delay for flow control, in us (in NBO).
493 * A value of UINT32_MAX indicates that the other
494 * peer wants us to disconnect.
496 uint32_t delay GNUNET_PACKED;
499 * What is the identity of the sender
501 struct GNUNET_PeerIdentity sender;
505 GNUNET_NETWORK_STRUCT_END
508 /* ************************* Monitoring *********** */
512 * If a session monitor is attached, notify it about the new
515 * @param plugin our plugin
516 * @param session session that changed state
517 * @param state new state of the session
520 notify_session_monitor (struct Plugin *plugin,
521 struct GNUNET_ATS_Session *session,
522 enum GNUNET_TRANSPORT_SessionState state)
524 struct GNUNET_TRANSPORT_SessionInfo info;
526 if (NULL == plugin->sic)
528 if (GNUNET_YES == session->in_destroy)
529 return; /* already destroyed, just RC>0 left-over actions */
534 info.is_inbound = GNUNET_SYSERR; /* hard to say */
535 info.num_msg_pending = session->msgs_in_queue;
536 info.num_bytes_pending = session->bytes_in_queue;
537 /* info.receive_delay remains zero as this is not supported by UDP
538 (cannot selectively not receive from 'some' peer while continuing
539 to receive from others) */
540 info.session_timeout = session->timeout;
541 info.address = session->address;
542 plugin->sic (plugin->sic_cls,
549 * Return information about the given session to the monitor callback.
551 * @param cls the `struct Plugin` with the monitor callback (`sic`)
552 * @param peer peer we send information about
553 * @param value our `struct GNUNET_ATS_Session` to send information about
554 * @return #GNUNET_OK (continue to iterate)
557 send_session_info_iter (void *cls,
558 const struct GNUNET_PeerIdentity *peer,
561 struct Plugin *plugin = cls;
562 struct GNUNET_ATS_Session *session = value;
564 notify_session_monitor (plugin,
566 GNUNET_TRANSPORT_SS_INIT);
567 notify_session_monitor (plugin,
569 GNUNET_TRANSPORT_SS_UP);
575 * Begin monitoring sessions of a plugin. There can only
576 * be one active monitor per plugin (i.e. if there are
577 * multiple monitors, the transport service needs to
578 * multiplex the generated events over all of them).
580 * @param cls closure of the plugin
581 * @param sic callback to invoke, NULL to disable monitor;
582 * plugin will being by iterating over all active
583 * sessions immediately and then enter monitor mode
584 * @param sic_cls closure for @a sic
587 udp_plugin_setup_monitor (void *cls,
588 GNUNET_TRANSPORT_SessionInfoCallback sic,
591 struct Plugin *plugin = cls;
594 plugin->sic_cls = sic_cls;
597 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
598 &send_session_info_iter,
600 /* signal end of first iteration */
608 /* ****************** Little Helpers ****************** */
612 * Function to free last resources associated with a session.
614 * @param s session to free
617 free_session (struct GNUNET_ATS_Session *s)
619 if (NULL != s->address)
621 GNUNET_HELLO_address_free (s->address);
624 if (NULL != s->frag_ctx)
626 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
629 GNUNET_free (s->frag_ctx);
634 GNUNET_MST_destroy (s->mst);
642 * Function that is called to get the keepalive factor.
643 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
644 * calculate the interval between keepalive packets.
646 * @param cls closure with the `struct Plugin`
647 * @return keepalive factor
650 udp_query_keepalive_factor (void *cls)
657 * Function obtain the network type for a session
659 * @param cls closure (`struct Plugin *`)
660 * @param session the session
661 * @return the network type
663 static enum GNUNET_ATS_Network_Type
664 udp_plugin_get_network (void *cls,
665 struct GNUNET_ATS_Session *session)
667 return session->scope;
672 * Function obtain the network type for an address.
674 * @param cls closure (`struct Plugin *`)
675 * @param address the address
676 * @return the network type
678 static enum GNUNET_ATS_Network_Type
679 udp_plugin_get_network_for_address (void *cls,
680 const struct GNUNET_HELLO_Address *address)
682 struct Plugin *plugin = cls;
684 struct sockaddr_in a4;
685 struct sockaddr_in6 a6;
686 const struct IPv4UdpAddress *u4;
687 const struct IPv6UdpAddress *u6;
691 addrlen = address->address_length;
692 if (addrlen == sizeof(struct IPv6UdpAddress))
694 GNUNET_assert (NULL != address->address); /* make static analysis happy */
695 u6 = address->address;
696 memset (&a6, 0, sizeof(a6));
697 #if HAVE_SOCKADDR_IN_SIN_LEN
698 a6.sin6_len = sizeof (a6);
700 a6.sin6_family = AF_INET6;
701 a6.sin6_port = u6->u6_port;
702 GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
706 else if (addrlen == sizeof(struct IPv4UdpAddress))
708 GNUNET_assert (NULL != address->address); /* make static analysis happy */
709 u4 = address->address;
710 memset (&a4, 0, sizeof(a4));
711 #if HAVE_SOCKADDR_IN_SIN_LEN
712 a4.sin_len = sizeof (a4);
714 a4.sin_family = AF_INET;
715 a4.sin_port = u4->u4_port;
716 a4.sin_addr.s_addr = u4->ipv4_addr;
723 return GNUNET_ATS_NET_UNSPECIFIED;
725 return plugin->env->get_address_type (plugin->env->cls,
731 /* ******************* Event loop ******************** */
734 * We have been notified that our readset has something to read. We don't
735 * know which socket needs to be read, so we have to check each one
736 * Then reschedule this function to be called again once more is available.
738 * @param cls the plugin handle
741 udp_plugin_select_v4 (void *cls);
745 * We have been notified that our readset has something to read. We don't
746 * know which socket needs to be read, so we have to check each one
747 * Then reschedule this function to be called again once more is available.
749 * @param cls the plugin handle
752 udp_plugin_select_v6 (void *cls);
756 * (re)schedule IPv4-select tasks for this plugin.
758 * @param plugin plugin to reschedule
761 schedule_select_v4 (struct Plugin *plugin)
763 struct GNUNET_TIME_Relative min_delay;
764 struct GNUNET_TIME_Relative delay;
765 struct UDP_MessageWrapper *udpw;
766 struct UDP_MessageWrapper *min_udpw;
768 if ( (GNUNET_YES == plugin->enable_ipv4) &&
769 (NULL != plugin->sockv4) )
771 /* Find a message ready to send:
772 * Flow delay from other peer is expired or not set (0) */
773 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
775 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
777 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
778 if (delay.rel_value_us < min_delay.rel_value_us)
784 if (NULL != plugin->select_task_v4)
785 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
786 if (NULL != min_udpw)
788 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791 "Calculated flow delay for UDPv4 at %s for %s\n",
792 GNUNET_STRINGS_relative_time_to_string (min_delay,
794 GNUNET_i2s (&min_udpw->session->target));
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Calculated flow delay for UDPv4 at %s for %s\n",
800 GNUNET_STRINGS_relative_time_to_string (min_delay,
802 GNUNET_i2s (&min_udpw->session->target));
805 plugin->select_task_v4
806 = GNUNET_SCHEDULER_add_read_net (min_delay,
808 &udp_plugin_select_v4,
815 * (re)schedule IPv6-select tasks for this plugin.
817 * @param plugin plugin to reschedule
820 schedule_select_v6 (struct Plugin *plugin)
822 struct GNUNET_TIME_Relative min_delay;
823 struct GNUNET_TIME_Relative delay;
824 struct UDP_MessageWrapper *udpw;
825 struct UDP_MessageWrapper *min_udpw;
827 if ( (GNUNET_YES == plugin->enable_ipv6) &&
828 (NULL != plugin->sockv6) )
830 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
832 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
834 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
835 if (delay.rel_value_us < min_delay.rel_value_us)
841 if (NULL != plugin->select_task_v6)
842 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
843 if (NULL != min_udpw)
845 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
847 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
848 "Calculated flow delay for UDPv6 at %s for %s\n",
849 GNUNET_STRINGS_relative_time_to_string (min_delay,
851 GNUNET_i2s (&min_udpw->session->target));
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
856 "Calculated flow delay for UDPv6 at %s for %s\n",
857 GNUNET_STRINGS_relative_time_to_string (min_delay,
859 GNUNET_i2s (&min_udpw->session->target));
862 plugin->select_task_v6
863 = GNUNET_SCHEDULER_add_read_net (min_delay,
865 &udp_plugin_select_v6,
871 /* ******************* Address to string and back ***************** */
875 * Function called for a quick conversion of the binary address to
876 * a numeric address. Note that the caller must not free the
877 * address and that the next call to this function is allowed
878 * to override the address again.
881 * @param addr binary address (a `union UdpAddress`)
882 * @param addrlen length of the @a addr
883 * @return string representing the same address
886 udp_address_to_string (void *cls,
890 static char rbuf[INET6_ADDRSTRLEN + 10];
891 char buf[INET6_ADDRSTRLEN];
895 const struct IPv4UdpAddress *t4;
896 const struct IPv6UdpAddress *t6;
907 if (addrlen == sizeof(struct IPv6UdpAddress))
911 options = ntohl (t6->options);
912 port = ntohs (t6->u6_port);
916 else if (addrlen == sizeof(struct IPv4UdpAddress))
920 options = ntohl (t4->options);
921 port = ntohs (t4->u4_port);
922 a4.s_addr = t4->ipv4_addr;
934 GNUNET_snprintf (rbuf,
948 * Function called to convert a string address to a binary address.
950 * @param cls closure (`struct Plugin *`)
951 * @param addr string address
952 * @param addrlen length of the address
953 * @param buf location to store the buffer
954 * @param added location to store the number of bytes in the buffer.
955 * If the function returns #GNUNET_SYSERR, its contents are undefined.
956 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
959 udp_string_to_address (void *cls,
965 struct sockaddr_storage socket_address;
971 /* Format tcp.options.address:port */
976 if ((NULL == addr) || (0 == addrlen))
979 return GNUNET_SYSERR;
981 if ('\0' != addr[addrlen - 1])
984 return GNUNET_SYSERR;
986 if (strlen (addr) != addrlen - 1)
989 return GNUNET_SYSERR;
991 plugin = GNUNET_strdup (addr);
992 optionstr = strchr (plugin, '.');
993 if (NULL == optionstr)
996 GNUNET_free (plugin);
997 return GNUNET_SYSERR;
1001 options = atol (optionstr);
1002 address = strchr (optionstr, '.');
1003 if (NULL == address)
1006 GNUNET_free (plugin);
1007 return GNUNET_SYSERR;
1013 GNUNET_STRINGS_to_address_ip (address,
1018 GNUNET_free (plugin);
1019 return GNUNET_SYSERR;
1021 GNUNET_free(plugin);
1023 switch (socket_address.ss_family)
1027 struct IPv4UdpAddress *u4;
1028 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1030 u4 = GNUNET_new (struct IPv4UdpAddress);
1031 u4->options = htonl (options);
1032 u4->ipv4_addr = in4->sin_addr.s_addr;
1033 u4->u4_port = in4->sin_port;
1035 *added = sizeof (struct IPv4UdpAddress);
1040 struct IPv6UdpAddress *u6;
1041 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1043 u6 = GNUNET_new (struct IPv6UdpAddress);
1044 u6->options = htonl (options);
1045 u6->ipv6_addr = in6->sin6_addr;
1046 u6->u6_port = in6->sin6_port;
1048 *added = sizeof (struct IPv6UdpAddress);
1053 return GNUNET_SYSERR;
1059 * Append our port and forward the result.
1061 * @param cls a `struct PrettyPrinterContext *`
1062 * @param hostname result from DNS resolver
1065 append_port (void *cls,
1066 const char *hostname)
1068 struct PrettyPrinterContext *ppc = cls;
1069 struct Plugin *plugin = ppc->plugin;
1072 if (NULL == hostname)
1074 /* Final call, done */
1075 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1076 plugin->ppc_dll_tail,
1078 ppc->resolver_handle = NULL;
1079 ppc->asc (ppc->asc_cls,
1085 if (GNUNET_YES == ppc->ipv6)
1086 GNUNET_asprintf (&ret,
1093 GNUNET_asprintf (&ret,
1099 ppc->asc (ppc->asc_cls,
1107 * Convert the transports address to a nice, human-readable format.
1109 * @param cls closure with the `struct Plugin *`
1110 * @param type name of the transport that generated the address
1111 * @param addr one of the addresses of the host, NULL for the last address
1112 * the specific address format depends on the transport;
1113 * a `union UdpAddress`
1114 * @param addrlen length of the address
1115 * @param numeric should (IP) addresses be displayed in numeric form?
1116 * @param timeout after how long should we give up?
1117 * @param asc function to call on each string
1118 * @param asc_cls closure for @a asc
1121 udp_plugin_address_pretty_printer (void *cls,
1126 struct GNUNET_TIME_Relative timeout,
1127 GNUNET_TRANSPORT_AddressStringCallback asc,
1130 struct Plugin *plugin = cls;
1131 struct PrettyPrinterContext *ppc;
1132 const struct sockaddr *sb;
1134 struct sockaddr_in a4;
1135 struct sockaddr_in6 a6;
1136 const struct IPv4UdpAddress *u4;
1137 const struct IPv6UdpAddress *u6;
1141 if (addrlen == sizeof(struct IPv6UdpAddress))
1147 a6.sin6_family = AF_INET6;
1148 #if HAVE_SOCKADDR_IN_SIN_LEN
1149 a6.sin6_len = sizeof (a6);
1151 a6.sin6_port = u6->u6_port;
1152 a6.sin6_addr = u6->ipv6_addr;
1153 port = ntohs (u6->u6_port);
1154 options = ntohl (u6->options);
1155 sb = (const struct sockaddr *) &a6;
1158 else if (addrlen == sizeof (struct IPv4UdpAddress))
1164 a4.sin_family = AF_INET;
1165 #if HAVE_SOCKADDR_IN_SIN_LEN
1166 a4.sin_len = sizeof (a4);
1168 a4.sin_port = u4->u4_port;
1169 a4.sin_addr.s_addr = u4->ipv4_addr;
1170 port = ntohs (u4->u4_port);
1171 options = ntohl (u4->options);
1172 sb = (const struct sockaddr *) &a4;
1177 /* invalid address */
1178 GNUNET_break_op (0);
1187 ppc = GNUNET_new (struct PrettyPrinterContext);
1188 ppc->plugin = plugin;
1190 ppc->asc_cls = asc_cls;
1192 ppc->options = options;
1193 if (addrlen == sizeof (struct IPv6UdpAddress))
1194 ppc->ipv6 = GNUNET_YES;
1196 ppc->ipv6 = GNUNET_NO;
1197 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1198 plugin->ppc_dll_tail,
1200 ppc->resolver_handle
1201 = GNUNET_RESOLVER_hostname_get (sb,
1211 * Check if the given port is plausible (must be either our listen
1212 * port or our advertised port). If it is neither, we return
1215 * @param plugin global variables
1216 * @param in_port port number to check
1217 * @return #GNUNET_OK if port is either our open or advertised port
1220 check_port (const struct Plugin *plugin,
1223 if ( (plugin->port == in_port) ||
1224 (plugin->aport == in_port) )
1226 return GNUNET_SYSERR;
1231 * Function that will be called to check if a binary address for this
1232 * plugin is well-formed and corresponds to an address for THIS peer
1233 * (as per our configuration). Naturally, if absolutely necessary,
1234 * plugins can be a bit conservative in their answer, but in general
1235 * plugins should make sure that the address does not redirect
1236 * traffic to a 3rd party that might try to man-in-the-middle our
1239 * @param cls closure, should be our handle to the Plugin
1240 * @param addr pointer to a `union UdpAddress`
1241 * @param addrlen length of @a addr
1242 * @return #GNUNET_OK if this is a plausible address for this peer
1243 * and transport, #GNUNET_SYSERR if not
1246 udp_plugin_check_address (void *cls,
1250 struct Plugin *plugin = cls;
1251 const struct IPv4UdpAddress *v4;
1252 const struct IPv6UdpAddress *v6;
1254 if (sizeof(struct IPv4UdpAddress) == addrlen)
1256 struct sockaddr_in s4;
1258 v4 = (const struct IPv4UdpAddress *) addr;
1259 if (GNUNET_OK != check_port (plugin,
1260 ntohs (v4->u4_port)))
1261 return GNUNET_SYSERR;
1262 memset (&s4, 0, sizeof (s4));
1263 s4.sin_family = AF_INET;
1264 #if HAVE_SOCKADDR_IN_SIN_LEN
1265 s4.sin_len = sizeof (s4);
1267 s4.sin_port = v4->u4_port;
1268 s4.sin_addr.s_addr = v4->ipv4_addr;
1271 GNUNET_NAT_test_address (plugin->nat,
1273 sizeof (struct sockaddr_in)))
1274 return GNUNET_SYSERR;
1276 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1278 struct sockaddr_in6 s6;
1280 v6 = (const struct IPv6UdpAddress *) addr;
1281 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1282 return GNUNET_OK; /* plausible, if unlikely... */
1283 memset (&s6, 0, sizeof (s6));
1284 s6.sin6_family = AF_INET6;
1285 #if HAVE_SOCKADDR_IN_SIN_LEN
1286 s6.sin6_len = sizeof (s6);
1288 s6.sin6_port = v6->u6_port;
1289 s6.sin6_addr = v6->ipv6_addr;
1292 GNUNET_NAT_test_address (plugin->nat,
1294 sizeof(struct sockaddr_in6)))
1295 return GNUNET_SYSERR;
1299 GNUNET_break_op (0);
1300 return GNUNET_SYSERR;
1307 * Our external IP address/port mapping has changed.
1309 * @param cls closure, the `struct Plugin`
1310 * @param add_remove #GNUNET_YES to mean the new public IP address,
1311 * #GNUNET_NO to mean the previous (now invalid) one
1312 * @param ac address class the address belongs to
1313 * @param addr either the previous or the new public IP address
1314 * @param addrlen actual length of the @a addr
1317 udp_nat_port_map_callback (void *cls,
1319 enum GNUNET_NAT_AddressClass ac,
1320 const struct sockaddr *addr,
1323 struct Plugin *plugin = cls;
1324 struct GNUNET_HELLO_Address *address;
1325 struct IPv4UdpAddress u4;
1326 struct IPv6UdpAddress u6;
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 (GNUNET_YES == add_remove)
1332 ? "NAT notification to add address `%s'\n"
1333 : "NAT notification to remove address `%s'\n",
1336 /* convert 'address' to our internal format */
1337 switch (addr->sa_family)
1341 const struct sockaddr_in *i4;
1343 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1344 i4 = (const struct sockaddr_in *) addr;
1345 if (0 == ntohs (i4->sin_port))
1346 return; /* Port = 0 means unmapped, ignore these for UDP. */
1350 u4.options = htonl (plugin->myoptions);
1351 u4.ipv4_addr = i4->sin_addr.s_addr;
1352 u4.u4_port = i4->sin_port;
1354 args = sizeof (struct IPv4UdpAddress);
1359 const struct sockaddr_in6 *i6;
1361 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1362 i6 = (const struct sockaddr_in6 *) addr;
1363 if (0 == ntohs (i6->sin6_port))
1364 return; /* Port = 0 means unmapped, ignore these for UDP. */
1368 u6.options = htonl (plugin->myoptions);
1369 u6.ipv6_addr = i6->sin6_addr;
1370 u6.u6_port = i6->sin6_port;
1372 args = sizeof (struct IPv6UdpAddress);
1379 /* modify our published address list */
1380 /* TODO: use 'ac' here in the future... */
1381 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1385 GNUNET_HELLO_ADDRESS_INFO_NONE);
1386 plugin->env->notify_address (plugin->env->cls,
1389 GNUNET_HELLO_address_free (address);
1393 /* ********************* Finding sessions ******************* */
1397 * Closure for #session_cmp_it().
1399 struct GNUNET_ATS_SessionCompareContext
1402 * Set to session matching the address.
1404 struct GNUNET_ATS_Session *res;
1407 * Address we are looking for.
1409 const struct GNUNET_HELLO_Address *address;
1414 * Find a session with a matching address.
1416 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1417 * @param key peer identity (unused)
1418 * @param value the `struct GNUNET_ATS_Session *`
1419 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1422 session_cmp_it (void *cls,
1423 const struct GNUNET_PeerIdentity *key,
1426 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1427 struct GNUNET_ATS_Session *s = value;
1429 if (0 == GNUNET_HELLO_address_cmp (s->address,
1432 GNUNET_assert (GNUNET_NO == s->in_destroy);
1441 * Locate an existing session the transport service is using to
1442 * send data to another peer. Performs some basic sanity checks
1443 * on the address and then tries to locate a matching session.
1445 * @param cls the plugin
1446 * @param address the address we should locate the session by
1447 * @return the session if it exists, or NULL if it is not found
1449 static struct GNUNET_ATS_Session *
1450 udp_plugin_lookup_session (void *cls,
1451 const struct GNUNET_HELLO_Address *address)
1453 struct Plugin *plugin = cls;
1454 const struct IPv6UdpAddress *udp_a6;
1455 const struct IPv4UdpAddress *udp_a4;
1456 struct GNUNET_ATS_SessionCompareContext cctx;
1458 if (NULL == address->address)
1463 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1465 if (NULL == plugin->sockv4)
1467 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1468 if (0 == udp_a4->u4_port)
1474 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1476 if (NULL == plugin->sockv6)
1478 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1479 if (0 == udp_a6->u6_port)
1491 /* check if session already exists */
1492 cctx.address = address;
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Looking for existing session for peer `%s' with address `%s'\n",
1496 GNUNET_i2s (&address->peer),
1497 udp_address_to_string (plugin,
1499 address->address_length));
1500 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1504 if (NULL == cctx.res)
1506 LOG (GNUNET_ERROR_TYPE_DEBUG,
1507 "Found existing session %p\n",
1513 /* ********************** Timeout ****************** */
1517 * Increment session timeout due to activity.
1519 * @param s session to reschedule timeout activity for
1522 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1524 if (GNUNET_YES == s->in_destroy)
1526 GNUNET_assert (NULL != s->timeout_task);
1527 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1533 * Function that will be called whenever the transport service wants to
1534 * notify the plugin that a session is still active and in use and
1535 * therefore the session timeout for this session has to be updated
1537 * @param cls closure with the `struct Plugin`
1538 * @param peer which peer was the session for
1539 * @param session which session is being updated
1542 udp_plugin_update_session_timeout (void *cls,
1543 const struct GNUNET_PeerIdentity *peer,
1544 struct GNUNET_ATS_Session *session)
1546 struct Plugin *plugin = cls;
1549 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1556 /* Reschedule session timeout */
1557 reschedule_session_timeout (session);
1561 /* ************************* Sending ************************ */
1565 * Remove the given message from the transmission queue and
1566 * update all applicable statistics.
1568 * @param plugin the UDP plugin
1569 * @param udpw message wrapper to dequeue
1572 dequeue (struct Plugin *plugin,
1573 struct UDP_MessageWrapper *udpw)
1575 struct GNUNET_ATS_Session *session = udpw->session;
1577 if (plugin->bytes_in_buffer < udpw->msg_size)
1583 GNUNET_STATISTICS_update (plugin->env->stats,
1584 "# UDP, total bytes in send buffers",
1585 - (long long) udpw->msg_size,
1587 plugin->bytes_in_buffer -= udpw->msg_size;
1589 GNUNET_STATISTICS_update (plugin->env->stats,
1590 "# UDP, total messages in send buffers",
1593 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1595 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1596 plugin->ipv4_queue_tail,
1599 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1601 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1602 plugin->ipv6_queue_tail,
1610 GNUNET_assert (session->msgs_in_queue > 0);
1611 session->msgs_in_queue--;
1612 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1613 session->bytes_in_queue -= udpw->msg_size;
1618 * Enqueue a message for transmission and update statistics.
1620 * @param plugin the UDP plugin
1621 * @param udpw message wrapper to queue
1624 enqueue (struct Plugin *plugin,
1625 struct UDP_MessageWrapper *udpw)
1627 struct GNUNET_ATS_Session *session = udpw->session;
1629 if (GNUNET_YES == session->in_destroy)
1635 if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
1641 GNUNET_STATISTICS_update (plugin->env->stats,
1642 "# UDP, total bytes in send buffers",
1645 plugin->bytes_in_buffer += udpw->msg_size;
1647 GNUNET_STATISTICS_update (plugin->env->stats,
1648 "# UDP, total messages in send buffers",
1651 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1653 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1654 plugin->ipv4_queue_tail,
1657 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1659 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1660 plugin->ipv6_queue_tail,
1666 udpw->cont (udpw->cont_cls,
1674 session->msgs_in_queue++;
1675 session->bytes_in_queue += udpw->msg_size;
1680 * We have completed our (attempt) to transmit a message that had to
1681 * be fragmented -- either because we got an ACK saying that all
1682 * fragments were received, or because of timeout / disconnect. Clean
1685 * @param frag_ctx fragmentation context to clean up
1686 * @param result #GNUNET_OK if we succeeded (got ACK),
1687 * #GNUNET_SYSERR if the transmission failed
1690 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1693 struct Plugin *plugin = frag_ctx->plugin;
1694 struct GNUNET_ATS_Session *s = frag_ctx->session;
1695 struct UDP_MessageWrapper *udpw;
1696 struct UDP_MessageWrapper *tmp;
1698 struct GNUNET_TIME_Relative delay;
1700 LOG (GNUNET_ERROR_TYPE_DEBUG,
1701 "%p: Fragmented message removed with result %s\n",
1703 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1704 /* Call continuation for fragmented message */
1705 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1706 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1708 overhead = frag_ctx->on_wire_size;
1709 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1710 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1712 LOG (GNUNET_ERROR_TYPE_WARNING,
1713 "Fragmented message acknowledged after %s (expected at %s)\n",
1714 GNUNET_STRINGS_relative_time_to_string (delay,
1716 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1720 LOG (GNUNET_ERROR_TYPE_DEBUG,
1721 "Fragmented message acknowledged after %s (expected at %s)\n",
1722 GNUNET_STRINGS_relative_time_to_string (delay,
1724 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1727 if (NULL != frag_ctx->cont)
1728 frag_ctx->cont (frag_ctx->cont_cls,
1731 s->frag_ctx->payload_size,
1732 frag_ctx->on_wire_size);
1733 GNUNET_STATISTICS_update (plugin->env->stats,
1734 "# UDP, fragmented messages active",
1738 if (GNUNET_OK == result)
1740 GNUNET_STATISTICS_update (plugin->env->stats,
1741 "# UDP, fragmented msgs, messages, sent, success",
1744 GNUNET_STATISTICS_update (plugin->env->stats,
1745 "# UDP, fragmented msgs, bytes payload, sent, success",
1746 s->frag_ctx->payload_size,
1748 GNUNET_STATISTICS_update (plugin->env->stats,
1749 "# UDP, fragmented msgs, bytes overhead, sent, success",
1752 GNUNET_STATISTICS_update (plugin->env->stats,
1753 "# UDP, total, bytes overhead, sent",
1756 GNUNET_STATISTICS_update (plugin->env->stats,
1757 "# UDP, total, bytes payload, sent",
1758 s->frag_ctx->payload_size,
1763 GNUNET_STATISTICS_update (plugin->env->stats,
1764 "# UDP, fragmented msgs, messages, sent, failure",
1767 GNUNET_STATISTICS_update (plugin->env->stats,
1768 "# UDP, fragmented msgs, bytes payload, sent, failure",
1769 s->frag_ctx->payload_size,
1771 GNUNET_STATISTICS_update (plugin->env->stats,
1772 "# UDP, fragmented msgs, bytes payload, sent, failure",
1775 GNUNET_STATISTICS_update (plugin->env->stats,
1776 "# UDP, fragmented msgs, bytes payload, sent, failure",
1781 /* Remove remaining fragments from queue, no need to transmit those
1783 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1785 udpw = plugin->ipv6_queue_head;
1786 while (NULL != udpw)
1789 if ( (udpw->frag_ctx != NULL) &&
1790 (udpw->frag_ctx == frag_ctx) )
1799 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1801 udpw = plugin->ipv4_queue_head;
1802 while (NULL != udpw)
1805 if ( (NULL != udpw->frag_ctx) &&
1806 (udpw->frag_ctx == frag_ctx) )
1815 notify_session_monitor (s->plugin,
1817 GNUNET_TRANSPORT_SS_UPDATE);
1818 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1819 &s->last_expected_msg_delay,
1820 &s->last_expected_ack_delay);
1822 GNUNET_free (frag_ctx);
1827 * We are finished with a fragment in the message queue.
1828 * Notify the continuation and update statistics.
1830 * @param cls the `struct Plugin *`
1831 * @param udpw the queue entry
1832 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1835 qc_fragment_sent (void *cls,
1836 struct UDP_MessageWrapper *udpw,
1839 struct Plugin *plugin = cls;
1841 GNUNET_assert (NULL != udpw->frag_ctx);
1842 if (GNUNET_OK == result)
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "Fragment of message with %u bytes transmitted to %s\n",
1846 (unsigned int) udpw->payload_size,
1847 GNUNET_i2s (&udpw->session->target));
1848 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1849 GNUNET_STATISTICS_update (plugin->env->stats,
1850 "# UDP, fragmented msgs, fragments, sent, success",
1853 GNUNET_STATISTICS_update (plugin->env->stats,
1854 "# UDP, fragmented msgs, fragments bytes, sent, success",
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Failed to transmit fragment of message with %u bytes to %s\n",
1862 (unsigned int) udpw->payload_size,
1863 GNUNET_i2s (&udpw->session->target));
1864 fragmented_message_done (udpw->frag_ctx,
1866 GNUNET_STATISTICS_update (plugin->env->stats,
1867 "# UDP, fragmented msgs, fragments, sent, failure",
1870 GNUNET_STATISTICS_update (plugin->env->stats,
1871 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1879 * Function that is called with messages created by the fragmentation
1880 * module. In the case of the `proc` callback of the
1881 * #GNUNET_FRAGMENT_context_create() function, this function must
1882 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1884 * @param cls closure, the `struct UDP_FragmentationContext`
1885 * @param msg the message that was created
1888 enqueue_fragment (void *cls,
1889 const struct GNUNET_MessageHeader *msg)
1891 struct UDP_FragmentationContext *frag_ctx = cls;
1892 struct Plugin *plugin = frag_ctx->plugin;
1893 struct UDP_MessageWrapper *udpw;
1894 struct GNUNET_ATS_Session *session = frag_ctx->session;
1895 size_t msg_len = ntohs (msg->size);
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 "Enqueuing fragment with %u bytes\n",
1900 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1901 udpw->session = session;
1902 udpw->msg_buf = (char *) &udpw[1];
1903 udpw->msg_size = msg_len;
1904 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1905 udpw->timeout = frag_ctx->timeout;
1906 udpw->start_time = frag_ctx->start_time;
1907 udpw->transmission_time = frag_ctx->next_frag_time;
1908 frag_ctx->next_frag_time
1909 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1910 frag_ctx->flow_delay_from_other_peer);
1911 udpw->frag_ctx = frag_ctx;
1912 udpw->qc = &qc_fragment_sent;
1913 udpw->qc_cls = plugin;
1914 GNUNET_memcpy (udpw->msg_buf,
1919 if (session->address->address_length == sizeof (struct IPv4UdpAddress))
1920 schedule_select_v4 (plugin);
1922 schedule_select_v6 (plugin);
1927 * We are finished with a message from the message queue.
1928 * Notify the continuation and update statistics.
1930 * @param cls the `struct Plugin *`
1931 * @param udpw the queue entry
1932 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1935 qc_message_sent (void *cls,
1936 struct UDP_MessageWrapper *udpw,
1939 struct Plugin *plugin = cls;
1941 struct GNUNET_TIME_Relative delay;
1943 if (udpw->msg_size >= udpw->payload_size)
1944 overhead = udpw->msg_size - udpw->payload_size;
1946 overhead = udpw->msg_size;
1948 if (NULL != udpw->cont)
1950 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1951 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1953 LOG (GNUNET_ERROR_TYPE_WARNING,
1954 "Message sent via UDP with delay of %s\n",
1955 GNUNET_STRINGS_relative_time_to_string (delay,
1960 LOG (GNUNET_ERROR_TYPE_DEBUG,
1961 "Message sent via UDP with delay of %s\n",
1962 GNUNET_STRINGS_relative_time_to_string (delay,
1965 udpw->cont (udpw->cont_cls,
1966 &udpw->session->target,
1971 if (GNUNET_OK == result)
1973 GNUNET_STATISTICS_update (plugin->env->stats,
1974 "# UDP, unfragmented msgs, messages, sent, success",
1977 GNUNET_STATISTICS_update (plugin->env->stats,
1978 "# UDP, unfragmented msgs, bytes payload, sent, success",
1981 GNUNET_STATISTICS_update (plugin->env->stats,
1982 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1985 GNUNET_STATISTICS_update (plugin->env->stats,
1986 "# UDP, total, bytes overhead, sent",
1989 GNUNET_STATISTICS_update (plugin->env->stats,
1990 "# UDP, total, bytes payload, sent",
1996 GNUNET_STATISTICS_update (plugin->env->stats,
1997 "# UDP, unfragmented msgs, messages, sent, failure",
2000 GNUNET_STATISTICS_update (plugin->env->stats,
2001 "# UDP, unfragmented msgs, bytes payload, sent, failure",
2004 GNUNET_STATISTICS_update (plugin->env->stats,
2005 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
2013 * Function that can be used by the transport service to transmit a
2014 * message using the plugin. Note that in the case of a peer
2015 * disconnecting, the continuation MUST be called prior to the
2016 * disconnect notification itself. This function will be called with
2017 * this peer's HELLO message to initiate a fresh connection to another
2020 * @param cls closure
2021 * @param s which session must be used
2022 * @param msgbuf the message to transmit
2023 * @param msgbuf_size number of bytes in @a msgbuf
2024 * @param priority how important is the message (most plugins will
2025 * ignore message priority and just FIFO)
2026 * @param to how long to wait at most for the transmission (does not
2027 * require plugins to discard the message after the timeout,
2028 * just advisory for the desired delay; most plugins will ignore
2030 * @param cont continuation to call once the message has
2031 * been transmitted (or if the transport is ready
2032 * for the next transmission call; or if the
2033 * peer disconnected...); can be NULL
2034 * @param cont_cls closure for @a cont
2035 * @return number of bytes used (on the physical network, with overheads);
2036 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2037 * and does NOT mean that the message was not transmitted (DV)
2040 udp_plugin_send (void *cls,
2041 struct GNUNET_ATS_Session *s,
2044 unsigned int priority,
2045 struct GNUNET_TIME_Relative to,
2046 GNUNET_TRANSPORT_TransmitContinuation cont,
2049 struct Plugin *plugin = cls;
2050 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2051 struct UDP_FragmentationContext *frag_ctx;
2052 struct UDP_MessageWrapper *udpw;
2053 struct UDPMessage *udp;
2054 char mbuf[udpmlen] GNUNET_ALIGN;
2055 struct GNUNET_TIME_Relative latency;
2057 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2058 (NULL == plugin->sockv6) )
2059 return GNUNET_SYSERR;
2060 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2061 (NULL == plugin->sockv4) )
2062 return GNUNET_SYSERR;
2063 if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
2066 return GNUNET_SYSERR;
2069 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2074 return GNUNET_SYSERR;
2076 LOG (GNUNET_ERROR_TYPE_DEBUG,
2077 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2079 GNUNET_i2s (&s->target),
2080 udp_address_to_string (plugin,
2081 s->address->address,
2082 s->address->address_length));
2084 udp = (struct UDPMessage *) mbuf;
2085 udp->header.size = htons (udpmlen);
2086 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2087 udp->reserved = htonl (0);
2088 udp->sender = *plugin->env->my_identity;
2090 /* We do not update the session time out here! Otherwise this
2091 * session will not timeout since we send keep alive before session
2094 * For UDP we update session timeout only on receive, this will
2095 * cover keep alives, since remote peer will reply with keep alive
2098 if (udpmlen <= UDP_MTU)
2100 /* unfragmented message */
2101 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2103 udpw->msg_buf = (char *) &udpw[1];
2104 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2105 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2106 udpw->start_time = GNUNET_TIME_absolute_get ();
2107 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2108 udpw->transmission_time = s->last_transmit_time;
2109 s->last_transmit_time
2110 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2111 s->flow_delay_from_other_peer);
2113 udpw->cont_cls = cont_cls;
2114 udpw->frag_ctx = NULL;
2115 udpw->qc = &qc_message_sent;
2116 udpw->qc_cls = plugin;
2117 GNUNET_memcpy (udpw->msg_buf,
2119 sizeof (struct UDPMessage));
2120 GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2125 GNUNET_STATISTICS_update (plugin->env->stats,
2126 "# UDP, unfragmented messages queued total",
2129 GNUNET_STATISTICS_update (plugin->env->stats,
2130 "# UDP, unfragmented bytes payload queued total",
2133 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2134 schedule_select_v4 (plugin);
2136 schedule_select_v6 (plugin);
2140 /* fragmented message */
2141 if (NULL != s->frag_ctx)
2142 return GNUNET_SYSERR;
2143 GNUNET_memcpy (&udp[1],
2146 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2147 frag_ctx->plugin = plugin;
2148 frag_ctx->session = s;
2149 frag_ctx->cont = cont;
2150 frag_ctx->cont_cls = cont_cls;
2151 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2152 frag_ctx->next_frag_time = s->last_transmit_time;
2153 frag_ctx->flow_delay_from_other_peer
2154 = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
2157 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2158 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2159 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2160 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2163 s->last_expected_msg_delay,
2164 s->last_expected_ack_delay,
2168 s->frag_ctx = frag_ctx;
2169 s->last_transmit_time = frag_ctx->next_frag_time;
2170 latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
2171 if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2172 LOG (GNUNET_ERROR_TYPE_WARNING,
2173 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2174 GNUNET_STRINGS_relative_time_to_string (latency,
2176 GNUNET_i2s (&s->target),
2177 (unsigned int) s->msgs_in_queue);
2179 LOG (GNUNET_ERROR_TYPE_DEBUG,
2180 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2181 GNUNET_STRINGS_relative_time_to_string (latency,
2183 GNUNET_i2s (&s->target),
2184 (unsigned int) s->msgs_in_queue);
2186 GNUNET_STATISTICS_update (plugin->env->stats,
2187 "# UDP, fragmented messages active",
2190 GNUNET_STATISTICS_update (plugin->env->stats,
2191 "# UDP, fragmented messages, total",
2194 GNUNET_STATISTICS_update (plugin->env->stats,
2195 "# UDP, fragmented bytes (payload)",
2196 frag_ctx->payload_size,
2199 notify_session_monitor (s->plugin,
2201 GNUNET_TRANSPORT_SS_UPDATE);
2206 /* ********************** Receiving ********************** */
2210 * Closure for #find_receive_context().
2212 struct FindReceiveContext
2215 * Where to store the result.
2217 struct DefragContext *rc;
2220 * Session associated with this context.
2222 struct GNUNET_ATS_Session *session;
2227 const union UdpAddress *udp_addr;
2230 * Number of bytes in @e udp_addr.
2232 size_t udp_addr_len;
2238 * Scan the heap for a receive context with the given address.
2240 * @param cls the `struct FindReceiveContext`
2241 * @param node internal node of the heap
2242 * @param element value stored at the node (a `struct ReceiveContext`)
2243 * @param cost cost associated with the node
2244 * @return #GNUNET_YES if we should continue to iterate,
2245 * #GNUNET_NO if not.
2248 find_receive_context (void *cls,
2249 struct GNUNET_CONTAINER_HeapNode *node,
2251 GNUNET_CONTAINER_HeapCostType cost)
2253 struct FindReceiveContext *frc = cls;
2254 struct DefragContext *e = element;
2256 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2257 (0 == memcmp (frc->udp_addr,
2259 frc->udp_addr_len)) )
2269 * Functions with this signature are called whenever we need to close
2270 * a session due to a disconnect or failure to establish a connection.
2272 * @param cls closure with the `struct Plugin`
2273 * @param s session to close down
2274 * @return #GNUNET_OK on success
2277 udp_disconnect_session (void *cls,
2278 struct GNUNET_ATS_Session *s)
2280 struct Plugin *plugin = cls;
2281 struct UDP_MessageWrapper *udpw;
2282 struct UDP_MessageWrapper *next;
2283 struct FindReceiveContext frc;
2285 GNUNET_assert (GNUNET_YES != s->in_destroy);
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Session %p to peer `%s' at address %s ended\n",
2289 GNUNET_i2s (&s->target),
2290 udp_address_to_string (plugin,
2291 s->address->address,
2292 s->address->address_length));
2293 if (NULL != s->timeout_task)
2295 GNUNET_SCHEDULER_cancel (s->timeout_task);
2296 s->timeout_task = NULL;
2298 if (NULL != s->frag_ctx)
2300 /* Remove fragmented message due to disconnect */
2301 fragmented_message_done (s->frag_ctx,
2304 GNUNET_assert (GNUNET_YES ==
2305 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2309 frc.udp_addr = s->address->address;
2310 frc.udp_addr_len = s->address->address_length;
2311 /* Lookup existing receive context for this address */
2312 if (NULL != plugin->defrag_ctxs)
2314 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2315 &find_receive_context,
2319 struct DefragContext *d_ctx = frc.rc;
2321 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2322 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2323 GNUNET_free (d_ctx);
2326 s->in_destroy = GNUNET_YES;
2327 next = plugin->ipv4_queue_head;
2328 while (NULL != (udpw = next))
2331 if (udpw->session == s)
2335 udpw->qc (udpw->qc_cls,
2341 next = plugin->ipv6_queue_head;
2342 while (NULL != (udpw = next))
2345 if (udpw->session == s)
2349 udpw->qc (udpw->qc_cls,
2355 if ( (NULL != s->frag_ctx) &&
2356 (NULL != s->frag_ctx->cont) )
2358 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2359 later, as it might be in use right now */
2360 LOG (GNUNET_ERROR_TYPE_DEBUG,
2361 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2362 GNUNET_i2s (&s->target));
2363 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2366 s->frag_ctx->payload_size,
2367 s->frag_ctx->on_wire_size);
2369 notify_session_monitor (s->plugin,
2371 GNUNET_TRANSPORT_SS_DONE);
2372 plugin->env->session_end (plugin->env->cls,
2375 GNUNET_STATISTICS_set (plugin->env->stats,
2376 "# UDP sessions active",
2377 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2386 * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
2388 * @param plugin the UDP plugin
2389 * @param msg the (presumed) UDP ACK message
2390 * @param udp_addr sender address
2391 * @param udp_addr_len number of bytes in @a udp_addr
2394 read_process_ack (struct Plugin *plugin,
2395 const struct GNUNET_MessageHeader *msg,
2396 const union UdpAddress *udp_addr,
2397 socklen_t udp_addr_len)
2399 const struct GNUNET_MessageHeader *ack;
2400 const struct UDP_ACK_Message *udp_ack;
2401 struct GNUNET_HELLO_Address *address;
2402 struct GNUNET_ATS_Session *s;
2403 struct GNUNET_TIME_Relative flow_delay;
2405 /* check message format */
2406 if (ntohs (msg->size)
2407 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2409 GNUNET_break_op (0);
2412 udp_ack = (const struct UDP_ACK_Message *) msg;
2413 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2414 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2420 /* Locate session */
2421 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2425 GNUNET_HELLO_ADDRESS_INFO_NONE);
2426 s = udp_plugin_lookup_session (plugin,
2430 LOG (GNUNET_ERROR_TYPE_WARNING,
2431 "UDP session of address %s for ACK not found\n",
2432 udp_address_to_string (plugin,
2434 address->address_length));
2435 GNUNET_HELLO_address_free (address);
2438 if (NULL == s->frag_ctx)
2440 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2441 "Fragmentation context of address %s for ACK (%s) not found\n",
2442 udp_address_to_string (plugin,
2444 address->address_length),
2445 GNUNET_FRAGMENT_print_ack (ack));
2446 GNUNET_HELLO_address_free (address);
2449 GNUNET_HELLO_address_free (address);
2451 /* evaluate flow delay: how long should we wait between messages? */
2452 if (UINT32_MAX == ntohl (udp_ack->delay))
2454 /* Other peer asked for us to terminate the session */
2455 LOG (GNUNET_ERROR_TYPE_INFO,
2456 "Asked to disconnect UDP session of %s\n",
2457 GNUNET_i2s (&udp_ack->sender));
2458 udp_disconnect_session (plugin,
2462 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2463 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2464 LOG (GNUNET_ERROR_TYPE_WARNING,
2465 "We received a sending delay of %s for %s\n",
2466 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2468 GNUNET_i2s (&udp_ack->sender));
2470 LOG (GNUNET_ERROR_TYPE_DEBUG,
2471 "We received a sending delay of %s for %s\n",
2472 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2474 GNUNET_i2s (&udp_ack->sender));
2475 /* Flow delay is for the reassembled packet, however, our delay
2476 is per packet, so we need to adjust: */
2477 s->flow_delay_from_other_peer = flow_delay;
2481 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2484 LOG (GNUNET_ERROR_TYPE_DEBUG,
2485 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2486 (unsigned int) ntohs (msg->size),
2487 GNUNET_i2s (&udp_ack->sender),
2488 udp_address_to_string (plugin,
2491 /* Expect more ACKs to arrive */
2495 /* Remove fragmented message after successful sending */
2496 LOG (GNUNET_ERROR_TYPE_DEBUG,
2497 "Message from %s at %s full ACK'ed\n",
2498 GNUNET_i2s (&udp_ack->sender),
2499 udp_address_to_string (plugin,
2502 fragmented_message_done (s->frag_ctx,
2508 * Message tokenizer has broken up an incomming message. Pass it on
2511 * @param cls the `struct GNUNET_ATS_Session *`
2512 * @param hdr the actual message
2513 * @return #GNUNET_OK (always)
2516 process_inbound_tokenized_messages (void *cls,
2517 const struct GNUNET_MessageHeader *hdr)
2519 struct GNUNET_ATS_Session *session = cls;
2520 struct Plugin *plugin = session->plugin;
2522 if (GNUNET_YES == session->in_destroy)
2524 reschedule_session_timeout (session);
2525 session->flow_delay_for_other_peer
2526 = plugin->env->receive (plugin->env->cls,
2535 * Destroy a session, plugin is being unloaded.
2537 * @param cls the `struct Plugin`
2538 * @param key hash of public key of target peer
2539 * @param value a `struct PeerSession *` to clean up
2540 * @return #GNUNET_OK (continue to iterate)
2543 disconnect_and_free_it (void *cls,
2544 const struct GNUNET_PeerIdentity *key,
2547 struct Plugin *plugin = cls;
2549 udp_disconnect_session (plugin,
2556 * Disconnect from a remote node. Clean up session if we have one for
2559 * @param cls closure for this call (should be handle to Plugin)
2560 * @param target the peeridentity of the peer to disconnect
2561 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2564 udp_disconnect (void *cls,
2565 const struct GNUNET_PeerIdentity *target)
2567 struct Plugin *plugin = cls;
2569 LOG (GNUNET_ERROR_TYPE_DEBUG,
2570 "Disconnecting from peer `%s'\n",
2571 GNUNET_i2s (target));
2572 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2574 &disconnect_and_free_it,
2580 * Session was idle, so disconnect it.
2582 * @param cls the `struct GNUNET_ATS_Session` to time out
2585 session_timeout (void *cls)
2587 struct GNUNET_ATS_Session *s = cls;
2588 struct Plugin *plugin = s->plugin;
2589 struct GNUNET_TIME_Relative left;
2591 s->timeout_task = NULL;
2592 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2593 if (left.rel_value_us > 0)
2595 /* not actually our turn yet, but let's at least update
2596 the monitor, it may think we're about to die ... */
2597 notify_session_monitor (s->plugin,
2599 GNUNET_TRANSPORT_SS_UPDATE);
2600 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2605 LOG (GNUNET_ERROR_TYPE_DEBUG,
2606 "Session %p was idle for %s, disconnecting\n",
2608 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2610 /* call session destroy function */
2611 udp_disconnect_session (plugin,
2617 * Allocate a new session for the given endpoint address.
2618 * Note that this function does not inform the service
2619 * of the new session, this is the responsibility of the
2620 * caller (if needed).
2622 * @param cls the `struct Plugin`
2623 * @param address address of the other peer to use
2624 * @param network_type network type the address belongs to
2625 * @return NULL on error, otherwise session handle
2627 static struct GNUNET_ATS_Session *
2628 udp_plugin_create_session (void *cls,
2629 const struct GNUNET_HELLO_Address *address,
2630 enum GNUNET_ATS_Network_Type network_type)
2632 struct Plugin *plugin = cls;
2633 struct GNUNET_ATS_Session *s;
2635 s = GNUNET_new (struct GNUNET_ATS_Session);
2636 s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
2639 s->address = GNUNET_HELLO_address_copy (address);
2640 s->target = address->peer;
2641 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2642 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2644 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2645 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2646 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2647 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2648 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2651 s->scope = network_type;
2653 LOG (GNUNET_ERROR_TYPE_DEBUG,
2654 "Creating new session %p for peer `%s' address `%s'\n",
2656 GNUNET_i2s (&address->peer),
2657 udp_address_to_string (plugin,
2659 address->address_length));
2660 GNUNET_assert (GNUNET_OK ==
2661 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2664 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2665 GNUNET_STATISTICS_set (plugin->env->stats,
2666 "# UDP sessions active",
2667 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2669 notify_session_monitor (plugin,
2671 GNUNET_TRANSPORT_SS_INIT);
2677 * Creates a new outbound session the transport service will use to
2678 * send data to the peer.
2680 * @param cls the `struct Plugin *`
2681 * @param address the address
2682 * @return the session or NULL of max connections exceeded
2684 static struct GNUNET_ATS_Session *
2685 udp_plugin_get_session (void *cls,
2686 const struct GNUNET_HELLO_Address *address)
2688 struct Plugin *plugin = cls;
2689 struct GNUNET_ATS_Session *s;
2690 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2691 const struct IPv4UdpAddress *udp_v4;
2692 const struct IPv6UdpAddress *udp_v6;
2694 if (NULL == address)
2699 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2700 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2702 GNUNET_break_op (0);
2705 if (NULL != (s = udp_plugin_lookup_session (cls,
2709 /* need to create new session */
2710 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2712 struct sockaddr_in v4;
2714 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2715 memset (&v4, '\0', sizeof (v4));
2716 v4.sin_family = AF_INET;
2717 #if HAVE_SOCKADDR_IN_SIN_LEN
2718 v4.sin_len = sizeof (struct sockaddr_in);
2720 v4.sin_port = udp_v4->u4_port;
2721 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2722 network_type = plugin->env->get_address_type (plugin->env->cls,
2723 (const struct sockaddr *) &v4,
2726 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2728 struct sockaddr_in6 v6;
2730 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2731 memset (&v6, '\0', sizeof (v6));
2732 v6.sin6_family = AF_INET6;
2733 #if HAVE_SOCKADDR_IN_SIN_LEN
2734 v6.sin6_len = sizeof (struct sockaddr_in6);
2736 v6.sin6_port = udp_v6->u6_port;
2737 v6.sin6_addr = udp_v6->ipv6_addr;
2738 network_type = plugin->env->get_address_type (plugin->env->cls,
2739 (const struct sockaddr *) &v6,
2742 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2743 return udp_plugin_create_session (cls,
2750 * We've received a UDP Message. Process it (pass contents to main service).
2752 * @param plugin plugin context
2753 * @param msg the message
2754 * @param udp_addr sender address
2755 * @param udp_addr_len number of bytes in @a udp_addr
2756 * @param network_type network type the address belongs to
2759 process_udp_message (struct Plugin *plugin,
2760 const struct UDPMessage *msg,
2761 const union UdpAddress *udp_addr,
2762 size_t udp_addr_len,
2763 enum GNUNET_ATS_Network_Type network_type)
2765 struct GNUNET_ATS_Session *s;
2766 struct GNUNET_HELLO_Address *address;
2768 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2769 if (0 != ntohl (msg->reserved))
2774 if (ntohs (msg->header.size)
2775 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2781 address = GNUNET_HELLO_address_allocate (&msg->sender,
2785 GNUNET_HELLO_ADDRESS_INFO_NONE);
2787 (s = udp_plugin_lookup_session (plugin,
2790 s = udp_plugin_create_session (plugin,
2793 plugin->env->session_start (plugin->env->cls,
2797 notify_session_monitor (plugin,
2799 GNUNET_TRANSPORT_SS_UP);
2801 GNUNET_free (address);
2804 GNUNET_MST_from_buffer (s->mst,
2805 (const char *) &msg[1],
2806 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2810 if ( (0 == s->rc) &&
2811 (GNUNET_YES == s->in_destroy) )
2817 * Process a defragmented message.
2819 * @param cls the `struct DefragContext *`
2820 * @param msg the message
2823 fragment_msg_proc (void *cls,
2824 const struct GNUNET_MessageHeader *msg)
2826 struct DefragContext *dc = cls;
2827 const struct UDPMessage *um;
2829 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2831 GNUNET_break_op (0);
2834 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2836 GNUNET_break_op (0);
2839 um = (const struct UDPMessage *) msg;
2840 dc->sender = um->sender;
2841 dc->have_sender = GNUNET_YES;
2842 process_udp_message (dc->plugin,
2851 * We finished sending an acknowledgement. Update
2854 * @param cls the `struct Plugin`
2855 * @param udpw message queue entry of the ACK
2856 * @param result #GNUNET_OK if the transmission worked,
2857 * #GNUNET_SYSERR if we failed to send the ACK
2860 ack_message_sent (void *cls,
2861 struct UDP_MessageWrapper *udpw,
2864 struct Plugin *plugin = cls;
2866 if (GNUNET_OK == result)
2868 GNUNET_STATISTICS_update (plugin->env->stats,
2869 "# UDP, ACK messages sent",
2875 GNUNET_STATISTICS_update (plugin->env->stats,
2876 "# UDP, ACK transmissions failed",
2884 * Transmit an acknowledgement.
2886 * @param cls the `struct DefragContext *`
2887 * @param id message ID (unused)
2888 * @param msg ack to transmit
2891 ack_proc (void *cls,
2893 const struct GNUNET_MessageHeader *msg)
2895 struct DefragContext *rc = cls;
2896 struct Plugin *plugin = rc->plugin;
2897 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2898 struct UDP_ACK_Message *udp_ack;
2900 struct UDP_MessageWrapper *udpw;
2901 struct GNUNET_ATS_Session *s;
2902 struct GNUNET_HELLO_Address *address;
2904 if (GNUNET_NO == rc->have_sender)
2906 /* tried to defragment but never succeeded, hence will not ACK */
2907 /* This can happen if we just lost msgs */
2908 GNUNET_STATISTICS_update (plugin->env->stats,
2909 "# UDP, fragments discarded without ACK",
2914 address = GNUNET_HELLO_address_allocate (&rc->sender,
2918 GNUNET_HELLO_ADDRESS_INFO_NONE);
2919 s = udp_plugin_lookup_session (plugin,
2921 GNUNET_HELLO_address_free (address);
2924 LOG (GNUNET_ERROR_TYPE_ERROR,
2925 "Trying to transmit ACK to peer `%s' but no session found!\n",
2926 udp_address_to_string (plugin,
2929 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2930 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2932 GNUNET_STATISTICS_update (plugin->env->stats,
2933 "# UDP, ACK transmissions failed",
2938 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2939 s->flow_delay_for_other_peer.rel_value_us)
2941 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2942 delay = s->flow_delay_for_other_peer.rel_value_us;
2944 delay = UINT32_MAX - 1; /* largest value we can communicate */
2945 LOG (GNUNET_ERROR_TYPE_DEBUG,
2946 "Sending ACK to `%s' including delay of %s\n",
2947 udp_address_to_string (plugin,
2950 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2952 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2953 udpw->msg_size = msize;
2954 udpw->payload_size = 0;
2956 udpw->start_time = GNUNET_TIME_absolute_get ();
2957 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2958 udpw->msg_buf = (char *) &udpw[1];
2959 udpw->qc = &ack_message_sent;
2960 udpw->qc_cls = plugin;
2961 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2962 udp_ack->header.size = htons ((uint16_t) msize);
2963 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2964 udp_ack->delay = htonl (delay);
2965 udp_ack->sender = *plugin->env->my_identity;
2966 GNUNET_memcpy (&udp_ack[1],
2971 notify_session_monitor (plugin,
2973 GNUNET_TRANSPORT_SS_UPDATE);
2974 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2975 schedule_select_v4 (plugin);
2977 schedule_select_v6 (plugin);
2982 * We received a fragment, process it.
2984 * @param plugin our plugin
2985 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2986 * @param udp_addr sender address
2987 * @param udp_addr_len number of bytes in @a udp_addr
2988 * @param network_type network type the address belongs to
2991 read_process_fragment (struct Plugin *plugin,
2992 const struct GNUNET_MessageHeader *msg,
2993 const union UdpAddress *udp_addr,
2994 size_t udp_addr_len,
2995 enum GNUNET_ATS_Network_Type network_type)
2997 struct DefragContext *d_ctx;
2998 struct GNUNET_TIME_Absolute now;
2999 struct FindReceiveContext frc;
3002 frc.udp_addr = udp_addr;
3003 frc.udp_addr_len = udp_addr_len;
3005 /* Lookup existing receive context for this address */
3006 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3007 &find_receive_context,
3009 now = GNUNET_TIME_absolute_get ();
3014 /* Create a new defragmentation context */
3015 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
3016 GNUNET_memcpy (&d_ctx[1],
3019 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
3020 d_ctx->udp_addr_len = udp_addr_len;
3021 d_ctx->network_type = network_type;
3022 d_ctx->plugin = plugin;
3023 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
3025 UDP_MAX_MESSAGES_IN_DEFRAG,
3029 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
3031 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3032 LOG (GNUNET_ERROR_TYPE_DEBUG,
3033 "Created new defragmentation context for %u-byte fragment from `%s'\n",
3034 (unsigned int) ntohs (msg->size),
3035 udp_address_to_string (plugin,
3041 LOG (GNUNET_ERROR_TYPE_DEBUG,
3042 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
3043 (unsigned int) ntohs (msg->size),
3044 udp_address_to_string (plugin,
3050 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
3053 /* keep this 'rc' from expiring */
3054 GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
3055 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3057 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
3058 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
3060 /* remove 'rc' that was inactive the longest */
3061 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
3062 GNUNET_assert (NULL != d_ctx);
3063 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3064 GNUNET_free (d_ctx);
3065 GNUNET_STATISTICS_update (plugin->env->stats,
3066 "# UDP, Defragmentations aborted",
3074 * Read and process a message from the given socket.
3076 * @param plugin the overall plugin
3077 * @param rsock socket to read from
3080 udp_select_read (struct Plugin *plugin,
3081 struct GNUNET_NETWORK_Handle *rsock)
3084 struct sockaddr_storage addr;
3085 char buf[65536] GNUNET_ALIGN;
3087 const struct GNUNET_MessageHeader *msg;
3088 struct IPv4UdpAddress v4;
3089 struct IPv6UdpAddress v6;
3090 const struct sockaddr *sa;
3091 const struct sockaddr_in *sa4;
3092 const struct sockaddr_in6 *sa6;
3093 const union UdpAddress *int_addr;
3094 size_t int_addr_len;
3095 enum GNUNET_ATS_Network_Type network_type;
3097 fromlen = sizeof (addr);
3101 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3104 (struct sockaddr *) &addr,
3106 sa = (const struct sockaddr *) &addr;
3108 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3109 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3110 * on this socket has failed.
3112 * WSAECONNRESET - The virtual circuit was reset by the remote side
3113 * executing a hard or abortive close. The application should close
3114 * the socket; it is no longer usable. On a UDP-datagram socket this
3115 * error indicates a previous send operation resulted in an ICMP Port
3116 * Unreachable message.
3118 if ( (-1 == size) &&
3119 (ECONNRESET == errno) )
3124 LOG (GNUNET_ERROR_TYPE_DEBUG,
3125 "UDP failed to receive data: %s\n",
3127 /* Connection failure or something. Not a protocol violation. */
3131 /* Check if this is a STUN packet */
3133 GNUNET_NAT_stun_handle_packet (plugin->nat,
3134 (const struct sockaddr *) &addr,
3138 return; /* was STUN, do not process further */
3140 if (size < sizeof(struct GNUNET_MessageHeader))
3142 LOG (GNUNET_ERROR_TYPE_WARNING,
3143 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3144 (unsigned int ) size,
3147 /* _MAY_ be a connection failure (got partial message) */
3148 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3149 GNUNET_break_op (0);
3153 msg = (const struct GNUNET_MessageHeader *) buf;
3154 LOG (GNUNET_ERROR_TYPE_DEBUG,
3155 "UDP received %u-byte message from `%s' type %u\n",
3156 (unsigned int) size,
3160 if (size != ntohs (msg->size))
3162 LOG (GNUNET_ERROR_TYPE_WARNING,
3163 "UDP malformed message (size %u) header from %s\n",
3164 (unsigned int) size,
3167 GNUNET_break_op (0);
3170 GNUNET_STATISTICS_update (plugin->env->stats,
3171 "# UDP, total bytes received",
3174 network_type = plugin->env->get_address_type (plugin->env->cls,
3177 switch (sa->sa_family)
3180 sa4 = (const struct sockaddr_in *) &addr;
3182 v4.ipv4_addr = sa4->sin_addr.s_addr;
3183 v4.u4_port = sa4->sin_port;
3184 int_addr = (union UdpAddress *) &v4;
3185 int_addr_len = sizeof (v4);
3188 sa6 = (const struct sockaddr_in6 *) &addr;
3190 v6.ipv6_addr = sa6->sin6_addr;
3191 v6.u6_port = sa6->sin6_port;
3192 int_addr = (union UdpAddress *) &v6;
3193 int_addr_len = sizeof (v6);
3200 switch (ntohs (msg->type))
3202 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3203 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3204 udp_broadcast_receive (plugin,
3211 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3212 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3217 process_udp_message (plugin,
3218 (const struct UDPMessage *) msg,
3223 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3224 read_process_ack (plugin,
3229 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3230 read_process_fragment (plugin,
3244 * Removes messages from the transmission queue that have
3245 * timed out, and then selects a message that should be
3248 * @param plugin the UDP plugin
3249 * @param sock which socket should we process the queue for (v4 or v6)
3250 * @return message selected for transmission, or NULL for none
3252 static struct UDP_MessageWrapper *
3253 remove_timeout_messages_and_select (struct Plugin *plugin,
3254 struct GNUNET_NETWORK_Handle *sock)
3256 struct UDP_MessageWrapper *udpw;
3257 struct GNUNET_TIME_Relative remaining;
3258 struct GNUNET_ATS_Session *session;
3261 removed = GNUNET_NO;
3262 udpw = (sock == plugin->sockv4)
3263 ? plugin->ipv4_queue_head
3264 : plugin->ipv6_queue_head;
3265 while (NULL != udpw)
3267 session = udpw->session;
3268 /* Find messages with timeout */
3269 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3270 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3272 /* Message timed out */
3273 removed = GNUNET_YES;
3276 udpw->qc (udpw->qc_cls,
3281 if (sock == plugin->sockv4)
3283 udpw = plugin->ipv4_queue_head;
3285 else if (sock == plugin->sockv6)
3287 udpw = plugin->ipv6_queue_head;
3291 GNUNET_break (0); /* should never happen */
3294 GNUNET_STATISTICS_update (plugin->env->stats,
3295 "# messages discarded due to timeout",
3301 /* Message did not time out, check transmission time */
3302 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3303 if (0 == remaining.rel_value_us)
3305 /* this message is not delayed */
3306 LOG (GNUNET_ERROR_TYPE_DEBUG,
3307 "Message for peer `%s' (%u bytes) is not delayed \n",
3308 GNUNET_i2s (&udpw->session->target),
3309 udpw->payload_size);
3310 break; /* Found message to send, break */
3314 /* Message is delayed, try next */
3315 LOG (GNUNET_ERROR_TYPE_DEBUG,
3316 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3317 GNUNET_i2s (&udpw->session->target),
3319 GNUNET_STRINGS_relative_time_to_string (remaining,
3325 if (GNUNET_YES == removed)
3326 notify_session_monitor (session->plugin,
3328 GNUNET_TRANSPORT_SS_UPDATE);
3334 * We failed to transmit a message via UDP. Generate
3335 * a descriptive error message.
3337 * @param plugin our plugin
3338 * @param sa target address we were trying to reach
3339 * @param slen number of bytes in @a sa
3340 * @param error the errno value returned from the sendto() call
3343 analyze_send_error (struct Plugin *plugin,
3344 const struct sockaddr *sa,
3348 enum GNUNET_ATS_Network_Type type;
3350 type = plugin->env->get_address_type (plugin->env->cls,
3353 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3354 (GNUNET_ATS_NET_WAN == type) ) &&
3355 ( (ENETUNREACH == errno) ||
3356 (ENETDOWN == errno) ) )
3358 if (slen == sizeof (struct sockaddr_in))
3360 /* IPv4: "Network unreachable" or "Network down"
3362 * This indicates we do not have connectivity
3364 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3365 _("UDP could not transmit message to `%s': "
3366 "Network seems down, please check your network configuration\n"),
3370 if (slen == sizeof (struct sockaddr_in6))
3372 /* IPv6: "Network unreachable" or "Network down"
3374 * This indicates that this system is IPv6 enabled, but does not
3375 * have a valid global IPv6 address assigned or we do not have
3378 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3379 _("UDP could not transmit IPv6 message! "
3380 "Please check your network configuration and disable IPv6 if your "
3381 "connection does not have a global IPv6 address\n"));
3386 LOG (GNUNET_ERROR_TYPE_WARNING,
3387 "UDP could not transmit message to `%s': `%s'\n",
3396 * It is time to try to transmit a UDP message. Select one
3399 * @param plugin the plugin
3400 * @param sock which socket (v4/v6) to send on
3403 udp_select_send (struct Plugin *plugin,
3404 struct GNUNET_NETWORK_Handle *sock)
3408 const struct sockaddr *a;
3409 const struct IPv4UdpAddress *u4;
3410 struct sockaddr_in a4;
3411 const struct IPv6UdpAddress *u6;
3412 struct sockaddr_in6 a6;
3413 struct UDP_MessageWrapper *udpw;
3415 /* Find message(s) to send */
3416 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3419 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3421 u4 = udpw->session->address->address;
3425 a4.sin_family = AF_INET;
3426 #if HAVE_SOCKADDR_IN_SIN_LEN
3427 a4.sin_len = sizeof (a4);
3429 a4.sin_port = u4->u4_port;
3430 a4.sin_addr.s_addr = u4->ipv4_addr;
3431 a = (const struct sockaddr *) &a4;
3434 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3436 u6 = udpw->session->address->address;
3440 a6.sin6_family = AF_INET6;
3441 #if HAVE_SOCKADDR_IN_SIN_LEN
3442 a6.sin6_len = sizeof (a6);
3444 a6.sin6_port = u6->u6_port;
3445 a6.sin6_addr = u6->ipv6_addr;
3446 a = (const struct sockaddr *) &a6;
3454 udpw->qc (udpw->qc_cls,
3457 notify_session_monitor (plugin,
3459 GNUNET_TRANSPORT_SS_UPDATE);
3463 sent = GNUNET_NETWORK_socket_sendto (sock,
3468 udpw->session->last_transmit_time
3469 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3470 udpw->session->last_transmit_time);
3473 if (GNUNET_SYSERR == sent)
3476 analyze_send_error (plugin,
3480 udpw->qc (udpw->qc_cls,
3483 GNUNET_STATISTICS_update (plugin->env->stats,
3484 "# UDP, total, bytes, sent, failure",
3487 GNUNET_STATISTICS_update (plugin->env->stats,
3488 "# UDP, total, messages, sent, failure",
3495 LOG (GNUNET_ERROR_TYPE_DEBUG,
3496 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3497 (unsigned int) (udpw->msg_size),
3498 GNUNET_i2s (&udpw->session->target),
3502 (sent < 0) ? STRERROR (errno) : "ok");
3503 GNUNET_STATISTICS_update (plugin->env->stats,
3504 "# UDP, total, bytes, sent, success",
3507 GNUNET_STATISTICS_update (plugin->env->stats,
3508 "# UDP, total, messages, sent, success",
3511 if (NULL != udpw->frag_ctx)
3512 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3513 udpw->qc (udpw->qc_cls,
3517 notify_session_monitor (plugin,
3519 GNUNET_TRANSPORT_SS_UPDATE);
3525 /* ***************** Event loop (part 2) *************** */
3529 * We have been notified that our readset has something to read. We don't
3530 * know which socket needs to be read, so we have to check each one
3531 * Then reschedule this function to be called again once more is available.
3533 * @param cls the plugin handle
3536 udp_plugin_select_v4 (void *cls)
3538 struct Plugin *plugin = cls;
3539 const struct GNUNET_SCHEDULER_TaskContext *tc;
3541 plugin->select_task_v4 = NULL;
3542 if (NULL == plugin->sockv4)
3544 tc = GNUNET_SCHEDULER_get_task_context ();
3545 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3546 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3548 udp_select_read (plugin,
3550 udp_select_send (plugin,
3552 schedule_select_v4 (plugin);
3557 * We have been notified that our readset has something to read. We don't
3558 * know which socket needs to be read, so we have to check each one
3559 * Then reschedule this function to be called again once more is available.
3561 * @param cls the plugin handle
3564 udp_plugin_select_v6 (void *cls)
3566 struct Plugin *plugin = cls;
3567 const struct GNUNET_SCHEDULER_TaskContext *tc;
3569 plugin->select_task_v6 = NULL;
3570 if (NULL == plugin->sockv6)
3572 tc = GNUNET_SCHEDULER_get_task_context ();
3573 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3574 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3576 udp_select_read (plugin,
3579 udp_select_send (plugin,
3581 schedule_select_v6 (plugin);
3585 /* ******************* Initialization *************** */
3589 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3591 * @param plugin the plugin to initialize
3592 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3593 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3594 * @return number of sockets that were successfully bound
3597 setup_sockets (struct Plugin *plugin,
3598 const struct sockaddr_in6 *bind_v6,
3599 const struct sockaddr_in *bind_v4)
3602 unsigned int sockets_created = 0;
3603 struct sockaddr_in6 server_addrv6;
3604 struct sockaddr_in server_addrv4;
3605 const struct sockaddr *server_addr;
3606 const struct sockaddr *addrs[2];
3607 socklen_t addrlens[2];
3611 /* Create IPv6 socket */
3613 if (GNUNET_YES == plugin->enable_ipv6)
3615 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3618 if (NULL == plugin->sockv6)
3620 LOG (GNUNET_ERROR_TYPE_INFO,
3621 _("Disabling IPv6 since it is not supported on this system!\n"));
3622 plugin->enable_ipv6 = GNUNET_NO;
3626 memset (&server_addrv6,
3628 sizeof(struct sockaddr_in6));
3629 #if HAVE_SOCKADDR_IN_SIN_LEN
3630 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3632 server_addrv6.sin6_family = AF_INET6;
3633 if (NULL != bind_v6)
3634 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3636 server_addrv6.sin6_addr = in6addr_any;
3638 if (0 == plugin->port) /* autodetect */
3639 server_addrv6.sin6_port
3640 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3644 server_addrv6.sin6_port = htons (plugin->port);
3645 addrlen = sizeof (struct sockaddr_in6);
3646 server_addr = (const struct sockaddr *) &server_addrv6;
3651 LOG(GNUNET_ERROR_TYPE_DEBUG,
3652 "Binding to IPv6 `%s'\n",
3653 GNUNET_a2s (server_addr,
3657 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3662 if (0 != plugin->port)
3664 tries = 10; /* fail immediately */
3665 break; /* bind failed on specific port */
3668 server_addrv6.sin6_port
3669 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3676 GNUNET_NETWORK_socket_close (plugin->sockv6);
3677 plugin->enable_ipv6 = GNUNET_NO;
3678 plugin->sockv6 = NULL;
3682 plugin->port = ntohs (server_addrv6.sin6_port);
3684 if (NULL != plugin->sockv6)
3686 LOG (GNUNET_ERROR_TYPE_DEBUG,
3687 "IPv6 UDP socket created listinging at %s\n",
3688 GNUNET_a2s (server_addr,
3690 addrs[sockets_created] = server_addr;
3691 addrlens[sockets_created] = addrlen;
3696 LOG (GNUNET_ERROR_TYPE_WARNING,
3697 _("Failed to bind UDP socket to %s: %s\n"),
3698 GNUNET_a2s (server_addr,
3705 /* Create IPv4 socket */
3707 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3710 if (NULL == plugin->sockv4)
3712 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3714 LOG (GNUNET_ERROR_TYPE_INFO,
3715 _("Disabling IPv4 since it is not supported on this system!\n"));
3716 plugin->enable_ipv4 = GNUNET_NO;
3720 memset (&server_addrv4,
3722 sizeof(struct sockaddr_in));
3723 #if HAVE_SOCKADDR_IN_SIN_LEN
3724 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3726 server_addrv4.sin_family = AF_INET;
3727 if (NULL != bind_v4)
3728 server_addrv4.sin_addr = bind_v4->sin_addr;
3730 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3732 if (0 == plugin->port)
3734 server_addrv4.sin_port
3735 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3739 server_addrv4.sin_port = htons (plugin->port);
3741 addrlen = sizeof (struct sockaddr_in);
3742 server_addr = (const struct sockaddr *) &server_addrv4;
3747 LOG (GNUNET_ERROR_TYPE_DEBUG,
3748 "Binding to IPv4 `%s'\n",
3749 GNUNET_a2s (server_addr,
3754 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3759 if (0 != plugin->port)
3761 tries = 10; /* fail */
3762 break; /* bind failed on specific port */
3766 server_addrv4.sin_port
3767 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3774 GNUNET_NETWORK_socket_close (plugin->sockv4);
3775 plugin->enable_ipv4 = GNUNET_NO;
3776 plugin->sockv4 = NULL;
3780 plugin->port = ntohs (server_addrv4.sin_port);
3783 if (NULL != plugin->sockv4)
3785 LOG (GNUNET_ERROR_TYPE_DEBUG,
3786 "IPv4 socket created on port %s\n",
3787 GNUNET_a2s (server_addr,
3789 addrs[sockets_created] = server_addr;
3790 addrlens[sockets_created] = addrlen;
3795 LOG (GNUNET_ERROR_TYPE_ERROR,
3796 _("Failed to bind UDP socket to %s: %s\n"),
3797 GNUNET_a2s (server_addr,
3803 if (0 == sockets_created)
3805 LOG (GNUNET_ERROR_TYPE_WARNING,
3806 _("Failed to open UDP sockets\n"));
3807 return 0; /* No sockets created, return */
3809 schedule_select_v4 (plugin);
3810 schedule_select_v6 (plugin);
3811 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3817 &udp_nat_port_map_callback,
3820 return sockets_created;
3825 * The exported method. Makes the core api available via a global and
3826 * returns the udp transport API.
3828 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3829 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3832 libgnunet_plugin_transport_udp_init (void *cls)
3834 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3835 struct GNUNET_TRANSPORT_PluginFunctions *api;
3837 unsigned long long port;
3838 unsigned long long aport;
3839 unsigned long long udp_max_bps;
3841 int enable_broadcasting;
3842 int enable_broadcasting_recv;
3843 char *bind4_address;
3844 char *bind6_address;
3845 struct GNUNET_TIME_Relative interval;
3846 struct sockaddr_in server_addrv4;
3847 struct sockaddr_in6 server_addrv6;
3852 if (NULL == env->receive)
3854 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3855 initialze the plugin or the API */
3856 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3858 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3859 api->address_to_string = &udp_address_to_string;
3860 api->string_to_address = &udp_string_to_address;
3864 /* Get port number: port == 0 : autodetect a port,
3865 * > 0 : use this port, not given : 2086 default */
3867 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3874 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3877 _("must be in [0,65535]"));
3881 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3888 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3891 _("must be in [0,65535]"));
3896 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3899 enable_v6 = GNUNET_NO;
3901 enable_v6 = GNUNET_YES;
3903 have_bind4 = GNUNET_NO;
3904 memset (&server_addrv4,
3906 sizeof (server_addrv4));
3908 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3913 LOG (GNUNET_ERROR_TYPE_DEBUG,
3914 "Binding UDP plugin to specific address: `%s'\n",
3916 if (1 != inet_pton (AF_INET,
3918 &server_addrv4.sin_addr))
3920 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3923 _("must be valid IPv4 address"));
3924 GNUNET_free (bind4_address);
3927 have_bind4 = GNUNET_YES;
3929 GNUNET_free_non_null (bind4_address);
3930 have_bind6 = GNUNET_NO;
3931 memset (&server_addrv6,
3933 sizeof (server_addrv6));
3935 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3940 LOG (GNUNET_ERROR_TYPE_DEBUG,
3941 "Binding udp plugin to specific address: `%s'\n",
3943 if (1 != inet_pton (AF_INET6,
3945 &server_addrv6.sin6_addr))
3947 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3950 _("must be valid IPv6 address"));
3951 GNUNET_free (bind6_address);
3954 have_bind6 = GNUNET_YES;
3956 GNUNET_free_non_null (bind6_address);
3958 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3961 if (enable_broadcasting == GNUNET_SYSERR)
3962 enable_broadcasting = GNUNET_NO;
3964 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3966 "BROADCAST_RECEIVE");
3967 if (enable_broadcasting_recv == GNUNET_SYSERR)
3968 enable_broadcasting_recv = GNUNET_YES;
3970 if (GNUNET_SYSERR ==
3971 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3973 "BROADCAST_INTERVAL",
3976 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3980 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3985 /* 50 MB/s == infinity for practical purposes */
3986 udp_max_bps = 1024 * 1024 * 50;
3989 p = GNUNET_new (struct Plugin);
3992 p->broadcast_interval = interval;
3993 p->enable_ipv6 = enable_v6;
3994 p->enable_ipv4 = GNUNET_YES; /* default */
3995 p->enable_broadcasting = enable_broadcasting;
3996 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3998 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
4000 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4001 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
4004 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
4006 res = setup_sockets (p,
4007 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
4008 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
4010 ( (NULL == p->sockv4) &&
4011 (NULL == p->sockv6) ) )
4013 LOG (GNUNET_ERROR_TYPE_ERROR,
4014 _("Failed to create UDP network sockets\n"));
4015 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
4016 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
4018 GNUNET_NAT_unregister (p->nat);
4023 /* Setup broadcasting and receiving beacons */
4028 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
4030 api->disconnect_session = &udp_disconnect_session;
4031 api->query_keepalive_factor = &udp_query_keepalive_factor;
4032 api->disconnect_peer = &udp_disconnect;
4033 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
4034 api->address_to_string = &udp_address_to_string;
4035 api->string_to_address = &udp_string_to_address;
4036 api->check_address = &udp_plugin_check_address;
4037 api->get_session = &udp_plugin_get_session;
4038 api->send = &udp_plugin_send;
4039 api->get_network = &udp_plugin_get_network;
4040 api->get_network_for_address = &udp_plugin_get_network_for_address;
4041 api->update_session_timeout = &udp_plugin_update_session_timeout;
4042 api->setup_monitor = &udp_plugin_setup_monitor;
4048 * Function called on each entry in the defragmentation heap to
4052 * @param node node in the heap (to be removed)
4053 * @param element a `struct DefragContext` to be cleaned up
4054 * @param cost unused
4055 * @return #GNUNET_YES
4058 heap_cleanup_iterator (void *cls,
4059 struct GNUNET_CONTAINER_HeapNode *node,
4061 GNUNET_CONTAINER_HeapCostType cost)
4063 struct DefragContext *d_ctx = element;
4065 GNUNET_CONTAINER_heap_remove_node (node);
4066 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4067 GNUNET_free (d_ctx);
4073 * The exported method. Makes the core api available via a global and
4074 * returns the udp transport API.
4076 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4080 libgnunet_plugin_transport_udp_done (void *cls)
4082 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4083 struct Plugin *plugin = api->cls;
4084 struct PrettyPrinterContext *cur;
4085 struct UDP_MessageWrapper *udpw;
4092 stop_broadcast (plugin);
4093 if (NULL != plugin->select_task_v4)
4095 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4096 plugin->select_task_v4 = NULL;
4098 if (NULL != plugin->select_task_v6)
4100 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4101 plugin->select_task_v6 = NULL;
4103 if (NULL != plugin->sockv4)
4105 GNUNET_break (GNUNET_OK ==
4106 GNUNET_NETWORK_socket_close (plugin->sockv4));
4107 plugin->sockv4 = NULL;
4109 if (NULL != plugin->sockv6)
4111 GNUNET_break (GNUNET_OK ==
4112 GNUNET_NETWORK_socket_close (plugin->sockv6));
4113 plugin->sockv6 = NULL;
4115 if (NULL != plugin->nat)
4117 GNUNET_NAT_unregister (plugin->nat);
4120 if (NULL != plugin->defrag_ctxs)
4122 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4123 &heap_cleanup_iterator,
4125 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4126 plugin->defrag_ctxs = NULL;
4128 while (NULL != (udpw = plugin->ipv4_queue_head))
4132 udpw->qc (udpw->qc_cls,
4137 while (NULL != (udpw = plugin->ipv6_queue_head))
4141 udpw->qc (udpw->qc_cls,
4146 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4147 &disconnect_and_free_it,
4149 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4151 while (NULL != (cur = plugin->ppc_dll_head))
4154 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4155 plugin->ppc_dll_tail,
4157 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4158 if (NULL != cur->timeout_task)
4160 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4161 cur->timeout_task = NULL;
4165 GNUNET_free (plugin);
4170 /* end of plugin_transport_udp.c */