2 This file is part of GNUnet
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP transport protocol
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Matthias Wachs
29 #include "plugin_transport_udp.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
46 * After how much inactivity should a UDP session time out?
48 #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51 * Number of messages we can defragment in parallel. We only really
52 * defragment 1 message at a time, but if messages get re-ordered, we
53 * may want to keep knowledge about the previous message to avoid
54 * discarding the current message in favor of a single fragment of a
55 * previous message. 3 should be good since we don't expect massive
56 * message reorderings with UDP.
58 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
61 * We keep a defragmentation queue per sender address. How many
62 * sender addresses do we support at the same time? Memory consumption
63 * is roughly a factor of 32k * #UDP_MAX_MESSAGES_IN_DEFRAG times this
64 * value. (So 128 corresponds to 12 MB and should suffice for
65 * connecting to roughly 128 peers via UDP).
67 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
71 * UDP Message-Packet header (after defragmentation).
78 struct GNUNET_MessageHeader header;
81 * Always zero for now.
86 * What is the identity of the sender
88 struct GNUNET_PeerIdentity sender;
94 * Closure for #append_port().
96 struct PrettyPrinterContext
101 struct PrettyPrinterContext *next;
106 struct PrettyPrinterContext *prev;
111 struct Plugin *plugin;
116 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
119 * Function to call with the result.
121 GNUNET_TRANSPORT_AddressStringCallback asc;
124 * Clsoure for @e asc.
131 struct GNUNET_SCHEDULER_Task *timeout_task;
134 * Is this an IPv6 address?
144 * Port to add after the IP address.
152 * Session with another peer.
154 struct GNUNET_ATS_Session
157 * Which peer is this session for?
159 struct GNUNET_PeerIdentity target;
162 * Plugin this session belongs to.
164 struct Plugin *plugin;
167 * Context for dealing with fragments.
169 struct UDP_FragmentationContext *frag_ctx;
172 * Desired delay for next sending we send to other peer
174 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
177 * Desired delay for transmissions we received from other peer.
178 * Adjusted to be per fragment (UDP_MTU), even though on the
179 * wire it was for "full messages".
181 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
184 * Session timeout task
186 struct GNUNET_SCHEDULER_Task *timeout_task;
189 * When does this session time out?
191 struct GNUNET_TIME_Absolute timeout;
194 * What time did we last transmit?
196 struct GNUNET_TIME_Absolute last_transmit_time;
199 * expected delay for ACKs
201 struct GNUNET_TIME_Relative last_expected_ack_delay;
204 * desired delay between UDP messages
206 struct GNUNET_TIME_Relative last_expected_msg_delay;
211 struct GNUNET_HELLO_Address *address;
214 * Number of bytes waiting for transmission to this peer.
216 unsigned long long bytes_in_queue;
219 * Number of messages waiting for transmission to this peer.
221 unsigned int msgs_in_queue;
224 * Reference counter to indicate that this session is
225 * currently being used and must not be destroyed;
226 * setting @e in_destroy will destroy it as soon as
232 * Network type of the address.
234 enum GNUNET_ATS_Network_Type scope;
237 * Is this session about to be destroyed (sometimes we cannot
238 * destroy a session immediately as below us on the stack
239 * there might be code that still uses it; in this case,
240 * @e rc is non-zero).
248 * Data structure to track defragmentation contexts based
249 * on the source of the UDP traffic.
255 * Defragmentation context.
257 struct GNUNET_DEFRAGMENT_Context *defrag;
260 * Reference to master plugin struct.
262 struct Plugin *plugin;
265 * Node in the defrag heap.
267 struct GNUNET_CONTAINER_HeapNode *hnode;
270 * Source address this receive context is for (allocated at the
271 * end of the struct).
273 const union UdpAddress *udp_addr;
276 * Who's message(s) are we defragmenting here?
277 * Only initialized once we succeeded and
278 * @e have_sender is set.
280 struct GNUNET_PeerIdentity sender;
283 * Length of @e udp_addr.
288 * Network type the address belongs to.
290 enum GNUNET_ATS_Network_Type network_type;
293 * Has the @e sender field been initialized yet?
300 * Context to send fragmented messages
302 struct UDP_FragmentationContext
305 * Next in linked list
307 struct UDP_FragmentationContext *next;
310 * Previous in linked list
312 struct UDP_FragmentationContext *prev;
317 struct Plugin *plugin;
320 * Handle for fragmentation.
322 struct GNUNET_FRAGMENT_Context *frag;
325 * The session this fragmentation context belongs to
327 struct GNUNET_ATS_Session *session;
330 * Function to call upon completion of the transmission.
332 GNUNET_TRANSPORT_TransmitContinuation cont;
335 * Closure for @e cont.
342 struct GNUNET_TIME_Absolute start_time;
345 * Transmission time for the next fragment. Incremented by
346 * the "flow_delay_from_other_peer" for each fragment when
347 * we setup the fragments.
349 struct GNUNET_TIME_Absolute next_frag_time;
354 struct GNUNET_TIME_Absolute timeout;
357 * Payload size of original unfragmented message
362 * Bytes used to send all fragments on wire including UDP overhead
370 * Function called when a message is removed from the
371 * transmission queue.
374 * @param udpw message wrapper finished
375 * @param result #GNUNET_OK on success (message was sent)
376 * #GNUNET_SYSERR if the target disconnected
377 * or we had a timeout or other trouble sending
380 (*QueueContinuation) (void *cls,
381 struct UDP_MessageWrapper *udpw,
386 * Information we track for each message in the queue.
388 struct UDP_MessageWrapper
391 * Session this message belongs to
393 struct GNUNET_ATS_Session *session;
396 * DLL of messages, previous element
398 struct UDP_MessageWrapper *prev;
401 * DLL of messages, next element
403 struct UDP_MessageWrapper *next;
406 * Message with @e msg_size bytes including UDP-specific overhead.
411 * Function to call once the message wrapper is being removed
412 * from the queue (with success or failure).
414 QueueContinuation qc;
422 * External continuation to call upon completion of the
423 * transmission, NULL if this queue entry is not for a
424 * message from the application.
426 GNUNET_TRANSPORT_TransmitContinuation cont;
429 * Closure for @e cont.
434 * Fragmentation context.
435 * frag_ctx == NULL if transport <= MTU
436 * frag_ctx != NULL if transport > MTU
438 struct UDP_FragmentationContext *frag_ctx;
441 * Message enqueue time.
443 struct GNUNET_TIME_Absolute start_time;
446 * Desired transmission time for this message, based on the
447 * flow limiting information we got from the other peer.
449 struct GNUNET_TIME_Absolute transmission_time;
454 struct GNUNET_TIME_Absolute timeout;
457 * Size of UDP message to send, including UDP-specific overhead.
462 * Payload size of original message.
469 GNUNET_NETWORK_STRUCT_BEGIN
472 * UDP ACK Message-Packet header.
474 struct UDP_ACK_Message
479 struct GNUNET_MessageHeader header;
482 * Desired delay for flow control, in us (in NBO).
484 uint32_t delay GNUNET_PACKED;
487 * What is the identity of the sender
489 struct GNUNET_PeerIdentity sender;
493 GNUNET_NETWORK_STRUCT_END
496 /* ************************* Monitoring *********** */
500 * If a session monitor is attached, notify it about the new
503 * @param plugin our plugin
504 * @param session session that changed state
505 * @param state new state of the session
508 notify_session_monitor (struct Plugin *plugin,
509 struct GNUNET_ATS_Session *session,
510 enum GNUNET_TRANSPORT_SessionState state)
512 struct GNUNET_TRANSPORT_SessionInfo info;
514 if (NULL == plugin->sic)
516 if (GNUNET_YES == session->in_destroy)
517 return; /* already destroyed, just RC>0 left-over actions */
522 info.is_inbound = GNUNET_SYSERR; /* hard to say */
523 info.num_msg_pending = session->msgs_in_queue;
524 info.num_bytes_pending = session->bytes_in_queue;
525 /* info.receive_delay remains zero as this is not supported by UDP
526 (cannot selectively not receive from 'some' peer while continuing
527 to receive from others) */
528 info.session_timeout = session->timeout;
529 info.address = session->address;
530 plugin->sic (plugin->sic_cls,
537 * Return information about the given session to the monitor callback.
539 * @param cls the `struct Plugin` with the monitor callback (`sic`)
540 * @param peer peer we send information about
541 * @param value our `struct GNUNET_ATS_Session` to send information about
542 * @return #GNUNET_OK (continue to iterate)
545 send_session_info_iter (void *cls,
546 const struct GNUNET_PeerIdentity *peer,
549 struct Plugin *plugin = cls;
550 struct GNUNET_ATS_Session *session = value;
552 notify_session_monitor (plugin,
554 GNUNET_TRANSPORT_SS_INIT);
555 notify_session_monitor (plugin,
557 GNUNET_TRANSPORT_SS_UP);
563 * Begin monitoring sessions of a plugin. There can only
564 * be one active monitor per plugin (i.e. if there are
565 * multiple monitors, the transport service needs to
566 * multiplex the generated events over all of them).
568 * @param cls closure of the plugin
569 * @param sic callback to invoke, NULL to disable monitor;
570 * plugin will being by iterating over all active
571 * sessions immediately and then enter monitor mode
572 * @param sic_cls closure for @a sic
575 udp_plugin_setup_monitor (void *cls,
576 GNUNET_TRANSPORT_SessionInfoCallback sic,
579 struct Plugin *plugin = cls;
582 plugin->sic_cls = sic_cls;
585 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
586 &send_session_info_iter,
588 /* signal end of first iteration */
596 /* ****************** Little Helpers ****************** */
600 * Function to free last resources associated with a session.
602 * @param s session to free
605 free_session (struct GNUNET_ATS_Session *s)
607 if (NULL != s->address)
609 GNUNET_HELLO_address_free (s->address);
612 if (NULL != s->frag_ctx)
614 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
617 GNUNET_free (s->frag_ctx);
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_ATS_Network_Type
647 udp_plugin_get_network (void *cls,
648 struct GNUNET_ATS_Session *session)
650 return session->scope;
655 * Function obtain the network type for an address.
657 * @param cls closure (`struct Plugin *`)
658 * @param address the address
659 * @return the network type
661 static enum GNUNET_ATS_Network_Type
662 udp_plugin_get_network_for_address (void *cls,
663 const struct GNUNET_HELLO_Address *address)
665 struct Plugin *plugin = cls;
667 struct sockaddr_in a4;
668 struct sockaddr_in6 a6;
669 const struct IPv4UdpAddress *u4;
670 const struct IPv6UdpAddress *u6;
674 addrlen = address->address_length;
675 if (addrlen == sizeof(struct IPv6UdpAddress))
677 GNUNET_assert (NULL != address->address); /* make static analysis happy */
678 u6 = address->address;
679 memset (&a6, 0, sizeof(a6));
680 #if HAVE_SOCKADDR_IN_SIN_LEN
681 a6.sin6_len = sizeof (a6);
683 a6.sin6_family = AF_INET6;
684 a6.sin6_port = u6->u6_port;
685 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
689 else if (addrlen == sizeof(struct IPv4UdpAddress))
691 GNUNET_assert (NULL != address->address); /* make static analysis happy */
692 u4 = address->address;
693 memset (&a4, 0, sizeof(a4));
694 #if HAVE_SOCKADDR_IN_SIN_LEN
695 a4.sin_len = sizeof (a4);
697 a4.sin_family = AF_INET;
698 a4.sin_port = u4->u4_port;
699 a4.sin_addr.s_addr = u4->ipv4_addr;
706 return GNUNET_ATS_NET_UNSPECIFIED;
708 return plugin->env->get_address_type (plugin->env->cls,
714 /* ******************* Event loop ******************** */
717 * We have been notified that our readset has something to read. We don't
718 * know which socket needs to be read, so we have to check each one
719 * Then reschedule this function to be called again once more is available.
721 * @param cls the plugin handle
722 * @param tc the scheduling context (for rescheduling this function again)
725 udp_plugin_select_v4 (void *cls,
726 const struct GNUNET_SCHEDULER_TaskContext *tc);
730 * We have been notified that our readset has something to read. We don't
731 * know which socket needs to be read, so we have to check each one
732 * Then reschedule this function to be called again once more is available.
734 * @param cls the plugin handle
735 * @param tc the scheduling context (for rescheduling this function again)
738 udp_plugin_select_v6 (void *cls,
739 const struct GNUNET_SCHEDULER_TaskContext *tc);
743 * (re)schedule IPv4-select tasks for this plugin.
745 * @param plugin plugin to reschedule
748 schedule_select_v4 (struct Plugin *plugin)
750 struct GNUNET_TIME_Relative min_delay;
751 struct UDP_MessageWrapper *udpw;
753 if ( (GNUNET_YES == plugin->enable_ipv4) &&
754 (NULL != plugin->sockv4) )
756 /* Find a message ready to send:
757 * Flow delay from other peer is expired or not set (0) */
758 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
759 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
760 min_delay = GNUNET_TIME_relative_min (min_delay,
761 GNUNET_TIME_absolute_get_remaining (udpw->transmission_time));
762 if (NULL != plugin->select_task_v4)
763 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
764 if (NULL != plugin->ipv4_queue_head)
766 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
769 "Calculated flow delay for UDPv4 at %s\n",
770 GNUNET_STRINGS_relative_time_to_string (min_delay,
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
776 "Calculated flow delay for UDPv4 at %s\n",
777 GNUNET_STRINGS_relative_time_to_string (min_delay,
781 plugin->select_task_v4
782 = GNUNET_SCHEDULER_add_read_net (min_delay,
784 &udp_plugin_select_v4,
791 * (re)schedule IPv6-select tasks for this plugin.
793 * @param plugin plugin to reschedule
796 schedule_select_v6 (struct Plugin *plugin)
798 struct GNUNET_TIME_Relative min_delay;
799 struct UDP_MessageWrapper *udpw;
801 if ( (GNUNET_YES == plugin->enable_ipv6) &&
802 (NULL != plugin->sockv6) )
804 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
805 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
806 min_delay = GNUNET_TIME_relative_min (min_delay,
807 GNUNET_TIME_absolute_get_remaining (udpw->transmission_time));
808 if (NULL != plugin->select_task_v6)
809 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
810 if (NULL != plugin->ipv6_queue_head)
812 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
815 "Calculated flow delay for UDPv6 at %s\n",
816 GNUNET_STRINGS_relative_time_to_string (min_delay,
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Calculated flow delay for UDPv6 at %s\n",
823 GNUNET_STRINGS_relative_time_to_string (min_delay,
827 plugin->select_task_v6
828 = GNUNET_SCHEDULER_add_read_net (min_delay,
830 &udp_plugin_select_v6,
836 /* ******************* Address to string and back ***************** */
840 * Function called for a quick conversion of the binary address to
841 * a numeric address. Note that the caller must not free the
842 * address and that the next call to this function is allowed
843 * to override the address again.
846 * @param addr binary address (a `union UdpAddress`)
847 * @param addrlen length of the @a addr
848 * @return string representing the same address
851 udp_address_to_string (void *cls,
855 static char rbuf[INET6_ADDRSTRLEN + 10];
856 char buf[INET6_ADDRSTRLEN];
860 const struct IPv4UdpAddress *t4;
861 const struct IPv6UdpAddress *t6;
872 if (addrlen == sizeof(struct IPv6UdpAddress))
876 options = ntohl (t6->options);
877 port = ntohs (t6->u6_port);
881 else if (addrlen == sizeof(struct IPv4UdpAddress))
885 options = ntohl (t4->options);
886 port = ntohs (t4->u4_port);
887 a4.s_addr = t4->ipv4_addr;
899 GNUNET_snprintf (rbuf,
913 * Function called to convert a string address to a binary address.
915 * @param cls closure (`struct Plugin *`)
916 * @param addr string address
917 * @param addrlen length of the address
918 * @param buf location to store the buffer
919 * @param added location to store the number of bytes in the buffer.
920 * If the function returns #GNUNET_SYSERR, its contents are undefined.
921 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
924 udp_string_to_address (void *cls,
930 struct sockaddr_storage socket_address;
936 /* Format tcp.options.address:port */
941 if ((NULL == addr) || (0 == addrlen))
944 return GNUNET_SYSERR;
946 if ('\0' != addr[addrlen - 1])
949 return GNUNET_SYSERR;
951 if (strlen (addr) != addrlen - 1)
954 return GNUNET_SYSERR;
956 plugin = GNUNET_strdup (addr);
957 optionstr = strchr (plugin, '.');
958 if (NULL == optionstr)
961 GNUNET_free (plugin);
962 return GNUNET_SYSERR;
966 options = atol (optionstr);
967 address = strchr (optionstr, '.');
971 GNUNET_free (plugin);
972 return GNUNET_SYSERR;
978 GNUNET_STRINGS_to_address_ip (address,
983 GNUNET_free (plugin);
984 return GNUNET_SYSERR;
988 switch (socket_address.ss_family)
992 struct IPv4UdpAddress *u4;
993 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
995 u4 = GNUNET_new (struct IPv4UdpAddress);
996 u4->options = htonl (options);
997 u4->ipv4_addr = in4->sin_addr.s_addr;
998 u4->u4_port = in4->sin_port;
1000 *added = sizeof (struct IPv4UdpAddress);
1005 struct IPv6UdpAddress *u6;
1006 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1008 u6 = GNUNET_new (struct IPv6UdpAddress);
1009 u6->options = htonl (options);
1010 u6->ipv6_addr = in6->sin6_addr;
1011 u6->u6_port = in6->sin6_port;
1013 *added = sizeof (struct IPv6UdpAddress);
1018 return GNUNET_SYSERR;
1024 * Append our port and forward the result.
1026 * @param cls a `struct PrettyPrinterContext *`
1027 * @param hostname result from DNS resolver
1030 append_port (void *cls,
1031 const char *hostname)
1033 struct PrettyPrinterContext *ppc = cls;
1034 struct Plugin *plugin = ppc->plugin;
1037 if (NULL == hostname)
1039 /* Final call, done */
1040 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1041 plugin->ppc_dll_tail,
1043 ppc->resolver_handle = NULL;
1044 ppc->asc (ppc->asc_cls,
1050 if (GNUNET_YES == ppc->ipv6)
1051 GNUNET_asprintf (&ret,
1058 GNUNET_asprintf (&ret,
1064 ppc->asc (ppc->asc_cls,
1072 * Convert the transports address to a nice, human-readable format.
1074 * @param cls closure with the `struct Plugin *`
1075 * @param type name of the transport that generated the address
1076 * @param addr one of the addresses of the host, NULL for the last address
1077 * the specific address format depends on the transport;
1078 * a `union UdpAddress`
1079 * @param addrlen length of the address
1080 * @param numeric should (IP) addresses be displayed in numeric form?
1081 * @param timeout after how long should we give up?
1082 * @param asc function to call on each string
1083 * @param asc_cls closure for @a asc
1086 udp_plugin_address_pretty_printer (void *cls,
1091 struct GNUNET_TIME_Relative timeout,
1092 GNUNET_TRANSPORT_AddressStringCallback asc,
1095 struct Plugin *plugin = cls;
1096 struct PrettyPrinterContext *ppc;
1097 const struct sockaddr *sb;
1099 struct sockaddr_in a4;
1100 struct sockaddr_in6 a6;
1101 const struct IPv4UdpAddress *u4;
1102 const struct IPv6UdpAddress *u6;
1106 if (addrlen == sizeof(struct IPv6UdpAddress))
1112 a6.sin6_family = AF_INET6;
1113 #if HAVE_SOCKADDR_IN_SIN_LEN
1114 a6.sin6_len = sizeof (a6);
1116 a6.sin6_port = u6->u6_port;
1117 a6.sin6_addr = u6->ipv6_addr;
1118 port = ntohs (u6->u6_port);
1119 options = ntohl (u6->options);
1120 sb = (const struct sockaddr *) &a6;
1123 else if (addrlen == sizeof (struct IPv4UdpAddress))
1129 a4.sin_family = AF_INET;
1130 #if HAVE_SOCKADDR_IN_SIN_LEN
1131 a4.sin_len = sizeof (a4);
1133 a4.sin_port = u4->u4_port;
1134 a4.sin_addr.s_addr = u4->ipv4_addr;
1135 port = ntohs (u4->u4_port);
1136 options = ntohl (u4->options);
1137 sb = (const struct sockaddr *) &a4;
1142 /* invalid address */
1143 GNUNET_break_op (0);
1152 ppc = GNUNET_new (struct PrettyPrinterContext);
1153 ppc->plugin = plugin;
1155 ppc->asc_cls = asc_cls;
1157 ppc->options = options;
1158 if (addrlen == sizeof (struct IPv6UdpAddress))
1159 ppc->ipv6 = GNUNET_YES;
1161 ppc->ipv6 = GNUNET_NO;
1162 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1163 plugin->ppc_dll_tail,
1165 ppc->resolver_handle
1166 = GNUNET_RESOLVER_hostname_get (sb,
1176 * Check if the given port is plausible (must be either our listen
1177 * port or our advertised port). If it is neither, we return
1180 * @param plugin global variables
1181 * @param in_port port number to check
1182 * @return #GNUNET_OK if port is either our open or advertised port
1185 check_port (const struct Plugin *plugin,
1188 if ( (plugin->port == in_port) ||
1189 (plugin->aport == in_port) )
1191 return GNUNET_SYSERR;
1196 * Function that will be called to check if a binary address for this
1197 * plugin is well-formed and corresponds to an address for THIS peer
1198 * (as per our configuration). Naturally, if absolutely necessary,
1199 * plugins can be a bit conservative in their answer, but in general
1200 * plugins should make sure that the address does not redirect
1201 * traffic to a 3rd party that might try to man-in-the-middle our
1204 * @param cls closure, should be our handle to the Plugin
1205 * @param addr pointer to a `union UdpAddress`
1206 * @param addrlen length of @a addr
1207 * @return #GNUNET_OK if this is a plausible address for this peer
1208 * and transport, #GNUNET_SYSERR if not
1211 udp_plugin_check_address (void *cls,
1215 struct Plugin *plugin = cls;
1216 const struct IPv4UdpAddress *v4;
1217 const struct IPv6UdpAddress *v6;
1219 if (sizeof(struct IPv4UdpAddress) == addrlen)
1221 v4 = (const struct IPv4UdpAddress *) addr;
1222 if (GNUNET_OK != check_port (plugin,
1223 ntohs (v4->u4_port)))
1224 return GNUNET_SYSERR;
1226 GNUNET_NAT_test_address (plugin->nat,
1228 sizeof (struct in_addr)))
1229 return GNUNET_SYSERR;
1231 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1233 v6 = (const struct IPv6UdpAddress *) addr;
1234 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1236 GNUNET_break_op (0);
1237 return GNUNET_SYSERR;
1239 if (GNUNET_OK != check_port (plugin,
1240 ntohs (v6->u6_port)))
1241 return GNUNET_SYSERR;
1243 GNUNET_NAT_test_address (plugin->nat,
1245 sizeof (struct in6_addr)))
1246 return GNUNET_SYSERR;
1250 GNUNET_break_op (0);
1251 return GNUNET_SYSERR;
1258 * Our external IP address/port mapping has changed.
1260 * @param cls closure, the `struct Plugin`
1261 * @param add_remove #GNUNET_YES to mean the new public IP address,
1262 * #GNUNET_NO to mean the previous (now invalid) one
1263 * @param addr either the previous or the new public IP address
1264 * @param addrlen actual length of the @a addr
1267 udp_nat_port_map_callback (void *cls,
1269 const struct sockaddr *addr,
1272 struct Plugin *plugin = cls;
1273 struct GNUNET_HELLO_Address *address;
1274 struct IPv4UdpAddress u4;
1275 struct IPv6UdpAddress u6;
1279 LOG (GNUNET_ERROR_TYPE_DEBUG,
1280 (GNUNET_YES == add_remove)
1281 ? "NAT notification to add address `%s'\n"
1282 : "NAT notification to remove address `%s'\n",
1285 /* convert 'address' to our internal format */
1286 switch (addr->sa_family)
1290 const struct sockaddr_in *i4;
1292 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1293 i4 = (const struct sockaddr_in *) addr;
1294 if (0 == ntohs (i4->sin_port))
1302 u4.options = htonl (plugin->myoptions);
1303 u4.ipv4_addr = i4->sin_addr.s_addr;
1304 u4.u4_port = i4->sin_port;
1306 args = sizeof (struct IPv4UdpAddress);
1311 const struct sockaddr_in6 *i6;
1313 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1314 i6 = (const struct sockaddr_in6 *) addr;
1315 if (0 == ntohs (i6->sin6_port))
1323 u6.options = htonl (plugin->myoptions);
1324 u6.ipv6_addr = i6->sin6_addr;
1325 u6.u6_port = i6->sin6_port;
1327 args = sizeof (struct IPv6UdpAddress);
1334 /* modify our published address list */
1335 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1339 GNUNET_HELLO_ADDRESS_INFO_NONE);
1340 plugin->env->notify_address (plugin->env->cls,
1343 GNUNET_HELLO_address_free (address);
1347 /* ********************* Finding sessions ******************* */
1351 * Closure for #session_cmp_it().
1353 struct GNUNET_ATS_SessionCompareContext
1356 * Set to session matching the address.
1358 struct GNUNET_ATS_Session *res;
1361 * Address we are looking for.
1363 const struct GNUNET_HELLO_Address *address;
1368 * Find a session with a matching address.
1370 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1371 * @param key peer identity (unused)
1372 * @param value the `struct GNUNET_ATS_Session *`
1373 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1376 session_cmp_it (void *cls,
1377 const struct GNUNET_PeerIdentity *key,
1380 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1381 struct GNUNET_ATS_Session *s = value;
1383 if (0 == GNUNET_HELLO_address_cmp (s->address,
1386 GNUNET_assert (GNUNET_NO == s->in_destroy);
1395 * Locate an existing session the transport service is using to
1396 * send data to another peer. Performs some basic sanity checks
1397 * on the address and then tries to locate a matching session.
1399 * @param cls the plugin
1400 * @param address the address we should locate the session by
1401 * @return the session if it exists, or NULL if it is not found
1403 static struct GNUNET_ATS_Session *
1404 udp_plugin_lookup_session (void *cls,
1405 const struct GNUNET_HELLO_Address *address)
1407 struct Plugin *plugin = cls;
1408 const struct IPv6UdpAddress *udp_a6;
1409 const struct IPv4UdpAddress *udp_a4;
1410 struct GNUNET_ATS_SessionCompareContext cctx;
1412 if (NULL == address->address)
1417 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1419 if (NULL == plugin->sockv4)
1421 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1422 if (0 == udp_a4->u4_port)
1428 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1430 if (NULL == plugin->sockv6)
1432 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1433 if (0 == udp_a6->u6_port)
1445 /* check if session already exists */
1446 cctx.address = address;
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "Looking for existing session for peer `%s' with address `%s'\n",
1450 GNUNET_i2s (&address->peer),
1451 udp_address_to_string (plugin,
1453 address->address_length));
1454 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1458 if (NULL == cctx.res)
1460 LOG (GNUNET_ERROR_TYPE_DEBUG,
1461 "Found existing session %p\n",
1467 /* ********************** Timeout ****************** */
1471 * Increment session timeout due to activity.
1473 * @param s session to reschedule timeout activity for
1476 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1478 if (GNUNET_YES == s->in_destroy)
1480 GNUNET_assert (NULL != s->timeout_task);
1481 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1487 * Function that will be called whenever the transport service wants to
1488 * notify the plugin that a session is still active and in use and
1489 * therefore the session timeout for this session has to be updated
1491 * @param cls closure with the `struct Plugin`
1492 * @param peer which peer was the session for
1493 * @param session which session is being updated
1496 udp_plugin_update_session_timeout (void *cls,
1497 const struct GNUNET_PeerIdentity *peer,
1498 struct GNUNET_ATS_Session *session)
1500 struct Plugin *plugin = cls;
1503 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1510 /* Reschedule session timeout */
1511 reschedule_session_timeout (session);
1515 /* ************************* Sending ************************ */
1519 * Remove the given message from the transmission queue and
1520 * update all applicable statistics.
1522 * @param plugin the UDP plugin
1523 * @param udpw message wrapper to dequeue
1526 dequeue (struct Plugin *plugin,
1527 struct UDP_MessageWrapper *udpw)
1529 struct GNUNET_ATS_Session *session = udpw->session;
1531 if (plugin->bytes_in_buffer < udpw->msg_size)
1537 GNUNET_STATISTICS_update (plugin->env->stats,
1538 "# UDP, total bytes in send buffers",
1539 - (long long) udpw->msg_size,
1541 plugin->bytes_in_buffer -= udpw->msg_size;
1543 GNUNET_STATISTICS_update (plugin->env->stats,
1544 "# UDP, total messages in send buffers",
1547 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1549 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1550 plugin->ipv4_queue_tail,
1553 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1555 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1556 plugin->ipv6_queue_tail,
1564 GNUNET_assert (session->msgs_in_queue > 0);
1565 session->msgs_in_queue--;
1566 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1567 session->bytes_in_queue -= udpw->msg_size;
1572 * Enqueue a message for transmission and update statistics.
1574 * @param plugin the UDP plugin
1575 * @param udpw message wrapper to queue
1578 enqueue (struct Plugin *plugin,
1579 struct UDP_MessageWrapper *udpw)
1581 struct GNUNET_ATS_Session *session = udpw->session;
1583 if (GNUNET_YES == session->in_destroy)
1588 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1594 GNUNET_STATISTICS_update (plugin->env->stats,
1595 "# UDP, total bytes in send buffers",
1598 plugin->bytes_in_buffer += udpw->msg_size;
1600 GNUNET_STATISTICS_update (plugin->env->stats,
1601 "# UDP, total messages in send buffers",
1604 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1606 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1607 plugin->ipv4_queue_tail,
1610 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1612 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1613 plugin->ipv6_queue_tail,
1619 udpw->cont (udpw->cont_cls,
1627 session->msgs_in_queue++;
1628 session->bytes_in_queue += udpw->msg_size;
1633 * We have completed our (attempt) to transmit a message that had to
1634 * be fragmented -- either because we got an ACK saying that all
1635 * fragments were received, or because of timeout / disconnect. Clean
1638 * @param frag_ctx fragmentation context to clean up
1639 * @param result #GNUNET_OK if we succeeded (got ACK),
1640 * #GNUNET_SYSERR if the transmission failed
1643 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1646 struct Plugin *plugin = frag_ctx->plugin;
1647 struct GNUNET_ATS_Session *s = frag_ctx->session;
1648 struct UDP_MessageWrapper *udpw;
1649 struct UDP_MessageWrapper *tmp;
1651 struct GNUNET_TIME_Relative delay;
1653 LOG (GNUNET_ERROR_TYPE_DEBUG,
1654 "%p: Fragmented message removed with result %s\n",
1656 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1657 /* Call continuation for fragmented message */
1658 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1659 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1661 overhead = frag_ctx->on_wire_size;
1662 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1663 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1666 "Fragmented message acknowledged after %s\n",
1667 GNUNET_STRINGS_relative_time_to_string (delay,
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "Fragmented message acknowledged after %s\n",
1674 GNUNET_STRINGS_relative_time_to_string (delay,
1678 if (NULL != frag_ctx->cont)
1679 frag_ctx->cont (frag_ctx->cont_cls,
1682 s->frag_ctx->payload_size,
1683 frag_ctx->on_wire_size);
1684 GNUNET_STATISTICS_update (plugin->env->stats,
1685 "# UDP, fragmented messages active",
1689 if (GNUNET_OK == result)
1691 GNUNET_STATISTICS_update (plugin->env->stats,
1692 "# UDP, fragmented msgs, messages, sent, success",
1695 GNUNET_STATISTICS_update (plugin->env->stats,
1696 "# UDP, fragmented msgs, bytes payload, sent, success",
1697 s->frag_ctx->payload_size,
1699 GNUNET_STATISTICS_update (plugin->env->stats,
1700 "# UDP, fragmented msgs, bytes overhead, sent, success",
1703 GNUNET_STATISTICS_update (plugin->env->stats,
1704 "# UDP, total, bytes overhead, sent",
1707 GNUNET_STATISTICS_update (plugin->env->stats,
1708 "# UDP, total, bytes payload, sent",
1709 s->frag_ctx->payload_size,
1714 GNUNET_STATISTICS_update (plugin->env->stats,
1715 "# UDP, fragmented msgs, messages, sent, failure",
1718 GNUNET_STATISTICS_update (plugin->env->stats,
1719 "# UDP, fragmented msgs, bytes payload, sent, failure",
1720 s->frag_ctx->payload_size,
1722 GNUNET_STATISTICS_update (plugin->env->stats,
1723 "# UDP, fragmented msgs, bytes payload, sent, failure",
1726 GNUNET_STATISTICS_update (plugin->env->stats,
1727 "# UDP, fragmented msgs, bytes payload, sent, failure",
1732 /* Remove remaining fragments from queue, no need to transmit those
1734 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1736 udpw = plugin->ipv6_queue_head;
1737 while (NULL != udpw)
1740 if ( (udpw->frag_ctx != NULL) &&
1741 (udpw->frag_ctx == frag_ctx) )
1750 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1752 udpw = plugin->ipv4_queue_head;
1753 while (NULL != udpw)
1756 if ( (NULL != udpw->frag_ctx) &&
1757 (udpw->frag_ctx == frag_ctx) )
1766 notify_session_monitor (s->plugin,
1768 GNUNET_TRANSPORT_SS_UPDATE);
1769 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1770 &s->last_expected_msg_delay,
1771 &s->last_expected_ack_delay);
1773 GNUNET_free (frag_ctx);
1778 * We are finished with a fragment in the message queue.
1779 * Notify the continuation and update statistics.
1781 * @param cls the `struct Plugin *`
1782 * @param udpw the queue entry
1783 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1786 qc_fragment_sent (void *cls,
1787 struct UDP_MessageWrapper *udpw,
1790 struct Plugin *plugin = cls;
1792 GNUNET_assert (NULL != udpw->frag_ctx);
1793 if (GNUNET_OK == result)
1795 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1796 GNUNET_STATISTICS_update (plugin->env->stats,
1797 "# UDP, fragmented msgs, fragments, sent, success",
1800 GNUNET_STATISTICS_update (plugin->env->stats,
1801 "# UDP, fragmented msgs, fragments bytes, sent, success",
1807 fragmented_message_done (udpw->frag_ctx,
1809 GNUNET_STATISTICS_update (plugin->env->stats,
1810 "# UDP, fragmented msgs, fragments, sent, failure",
1813 GNUNET_STATISTICS_update (plugin->env->stats,
1814 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1822 * Function that is called with messages created by the fragmentation
1823 * module. In the case of the `proc` callback of the
1824 * #GNUNET_FRAGMENT_context_create() function, this function must
1825 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1827 * @param cls closure, the `struct UDP_FragmentationContext`
1828 * @param msg the message that was created
1831 enqueue_fragment (void *cls,
1832 const struct GNUNET_MessageHeader *msg)
1834 struct UDP_FragmentationContext *frag_ctx = cls;
1835 struct Plugin *plugin = frag_ctx->plugin;
1836 struct UDP_MessageWrapper *udpw;
1837 struct GNUNET_ATS_Session *session = frag_ctx->session;
1838 size_t msg_len = ntohs (msg->size);
1840 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 "Enqueuing fragment with %u bytes\n",
1843 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1844 udpw->session = session;
1845 udpw->msg_buf = (char *) &udpw[1];
1846 udpw->msg_size = msg_len;
1847 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1848 udpw->timeout = frag_ctx->timeout;
1849 udpw->start_time = frag_ctx->start_time;
1850 udpw->transmission_time = frag_ctx->next_frag_time;
1851 frag_ctx->next_frag_time
1852 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1853 session->flow_delay_from_other_peer);
1854 udpw->frag_ctx = frag_ctx;
1855 udpw->qc = &qc_fragment_sent;
1856 udpw->qc_cls = plugin;
1857 memcpy (udpw->msg_buf,
1862 if (sizeof (struct IPv4UdpAddress) == session->address->address_length)
1863 schedule_select_v4 (plugin);
1865 schedule_select_v6 (plugin);
1870 * We are finished with a message from the message queue.
1871 * Notify the continuation and update statistics.
1873 * @param cls the `struct Plugin *`
1874 * @param udpw the queue entry
1875 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1878 qc_message_sent (void *cls,
1879 struct UDP_MessageWrapper *udpw,
1882 struct Plugin *plugin = cls;
1884 struct GNUNET_TIME_Relative delay;
1886 if (udpw->msg_size >= udpw->payload_size)
1887 overhead = udpw->msg_size - udpw->payload_size;
1889 overhead = udpw->msg_size;
1891 if (NULL != udpw->cont)
1893 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1894 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1896 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1897 "Message sent via UDP with delay of %s\n",
1898 GNUNET_STRINGS_relative_time_to_string (delay,
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 "Message sent via UDP with delay of %s\n",
1905 GNUNET_STRINGS_relative_time_to_string (delay,
1908 udpw->cont (udpw->cont_cls,
1909 &udpw->session->target,
1914 if (GNUNET_OK == result)
1916 GNUNET_STATISTICS_update (plugin->env->stats,
1917 "# UDP, unfragmented msgs, messages, sent, success",
1920 GNUNET_STATISTICS_update (plugin->env->stats,
1921 "# UDP, unfragmented msgs, bytes payload, sent, success",
1924 GNUNET_STATISTICS_update (plugin->env->stats,
1925 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1928 GNUNET_STATISTICS_update (plugin->env->stats,
1929 "# UDP, total, bytes overhead, sent",
1932 GNUNET_STATISTICS_update (plugin->env->stats,
1933 "# UDP, total, bytes payload, sent",
1939 GNUNET_STATISTICS_update (plugin->env->stats,
1940 "# UDP, unfragmented msgs, messages, sent, failure",
1943 GNUNET_STATISTICS_update (plugin->env->stats,
1944 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1947 GNUNET_STATISTICS_update (plugin->env->stats,
1948 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1956 * Function that can be used by the transport service to transmit a
1957 * message using the plugin. Note that in the case of a peer
1958 * disconnecting, the continuation MUST be called prior to the
1959 * disconnect notification itself. This function will be called with
1960 * this peer's HELLO message to initiate a fresh connection to another
1963 * @param cls closure
1964 * @param s which session must be used
1965 * @param msgbuf the message to transmit
1966 * @param msgbuf_size number of bytes in @a msgbuf
1967 * @param priority how important is the message (most plugins will
1968 * ignore message priority and just FIFO)
1969 * @param to how long to wait at most for the transmission (does not
1970 * require plugins to discard the message after the timeout,
1971 * just advisory for the desired delay; most plugins will ignore
1973 * @param cont continuation to call once the message has
1974 * been transmitted (or if the transport is ready
1975 * for the next transmission call; or if the
1976 * peer disconnected...); can be NULL
1977 * @param cont_cls closure for @a cont
1978 * @return number of bytes used (on the physical network, with overheads);
1979 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1980 * and does NOT mean that the message was not transmitted (DV)
1983 udp_plugin_send (void *cls,
1984 struct GNUNET_ATS_Session *s,
1987 unsigned int priority,
1988 struct GNUNET_TIME_Relative to,
1989 GNUNET_TRANSPORT_TransmitContinuation cont,
1992 struct Plugin *plugin = cls;
1993 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
1994 struct UDP_FragmentationContext *frag_ctx;
1995 struct UDP_MessageWrapper *udpw;
1996 struct UDPMessage *udp;
1997 char mbuf[udpmlen] GNUNET_ALIGN;
1999 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2000 (NULL == plugin->sockv6) )
2001 return GNUNET_SYSERR;
2002 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2003 (NULL == plugin->sockv4) )
2004 return GNUNET_SYSERR;
2005 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2008 return GNUNET_SYSERR;
2011 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2016 return GNUNET_SYSERR;
2018 LOG (GNUNET_ERROR_TYPE_DEBUG,
2019 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2021 GNUNET_i2s (&s->target),
2022 udp_address_to_string (plugin,
2023 s->address->address,
2024 s->address->address_length));
2026 udp = (struct UDPMessage *) mbuf;
2027 udp->header.size = htons (udpmlen);
2028 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2029 udp->reserved = htonl (0);
2030 udp->sender = *plugin->env->my_identity;
2032 /* We do not update the session time out here! Otherwise this
2033 * session will not timeout since we send keep alive before session
2036 * For UDP we update session timeout only on receive, this will
2037 * cover keep alives, since remote peer will reply with keep alive
2040 if (udpmlen <= UDP_MTU)
2042 /* unfragmented message */
2043 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2045 udpw->msg_buf = (char *) &udpw[1];
2046 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2047 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2048 udpw->start_time = GNUNET_TIME_absolute_get ();
2049 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2050 udpw->transmission_time = s->last_transmit_time;
2051 s->last_transmit_time
2052 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2053 s->flow_delay_from_other_peer);
2055 udpw->cont_cls = cont_cls;
2056 udpw->frag_ctx = NULL;
2057 udpw->qc = &qc_message_sent;
2058 udpw->qc_cls = plugin;
2059 memcpy (udpw->msg_buf,
2061 sizeof (struct UDPMessage));
2062 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2067 GNUNET_STATISTICS_update (plugin->env->stats,
2068 "# UDP, unfragmented messages queued total",
2071 GNUNET_STATISTICS_update (plugin->env->stats,
2072 "# UDP, unfragmented bytes payload queued total",
2078 /* fragmented message */
2079 if (NULL != s->frag_ctx)
2080 return GNUNET_SYSERR;
2084 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2085 frag_ctx->plugin = plugin;
2086 frag_ctx->session = s;
2087 frag_ctx->cont = cont;
2088 frag_ctx->cont_cls = cont_cls;
2089 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2090 frag_ctx->next_frag_time = s->last_transmit_time;
2091 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2092 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2093 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2094 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2097 s->last_expected_msg_delay,
2098 s->last_expected_ack_delay,
2102 s->frag_ctx = frag_ctx;
2103 s->last_transmit_time = frag_ctx->next_frag_time;
2104 GNUNET_STATISTICS_update (plugin->env->stats,
2105 "# UDP, fragmented messages active",
2108 GNUNET_STATISTICS_update (plugin->env->stats,
2109 "# UDP, fragmented messages, total",
2112 GNUNET_STATISTICS_update (plugin->env->stats,
2113 "# UDP, fragmented bytes (payload)",
2114 frag_ctx->payload_size,
2117 notify_session_monitor (s->plugin,
2119 GNUNET_TRANSPORT_SS_UPDATE);
2120 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2121 schedule_select_v4 (plugin);
2123 schedule_select_v6 (plugin);
2129 * Handle an ACK message.
2131 * @param plugin the UDP plugin
2132 * @param msg the (presumed) UDP ACK message
2133 * @param udp_addr sender address
2134 * @param udp_addr_len number of bytes in @a udp_addr
2137 read_process_ack (struct Plugin *plugin,
2138 const struct GNUNET_MessageHeader *msg,
2139 const union UdpAddress *udp_addr,
2140 socklen_t udp_addr_len)
2142 const struct GNUNET_MessageHeader *ack;
2143 const struct UDP_ACK_Message *udp_ack;
2144 struct GNUNET_HELLO_Address *address;
2145 struct GNUNET_ATS_Session *s;
2146 struct GNUNET_TIME_Relative flow_delay;
2148 if (ntohs (msg->size)
2149 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2151 GNUNET_break_op (0);
2154 udp_ack = (const struct UDP_ACK_Message *) msg;
2155 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2156 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2161 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2165 GNUNET_HELLO_ADDRESS_INFO_NONE);
2166 s = udp_plugin_lookup_session (plugin,
2170 LOG (GNUNET_ERROR_TYPE_WARNING,
2171 "UDP session of address %s for ACK not found\n",
2172 udp_address_to_string (plugin,
2174 address->address_length));
2175 GNUNET_HELLO_address_free (address);
2178 if (NULL == s->frag_ctx)
2180 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2181 "Fragmentation context of address %s for ACK (%s) not found\n",
2182 udp_address_to_string (plugin,
2184 address->address_length),
2185 GNUNET_FRAGMENT_print_ack (ack));
2186 GNUNET_HELLO_address_free (address);
2189 GNUNET_HELLO_address_free (address);
2191 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2192 LOG (GNUNET_ERROR_TYPE_DEBUG,
2193 "We received a sending delay of %s for %s\n",
2194 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2196 GNUNET_i2s (&udp_ack->sender));
2197 /* Flow delay is for the reassembled packet, however, our delay
2198 is per packet, so we need to adjust: */
2199 flow_delay = GNUNET_TIME_relative_divide (flow_delay,
2200 1 + (s->frag_ctx->payload_size /
2202 s->flow_delay_from_other_peer = flow_delay;
2206 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2209 LOG (GNUNET_ERROR_TYPE_DEBUG,
2210 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2211 (unsigned int) ntohs (msg->size),
2212 GNUNET_i2s (&udp_ack->sender),
2213 udp_address_to_string (plugin,
2216 /* Expect more ACKs to arrive */
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "Message from %s at %s full ACK'ed\n",
2222 GNUNET_i2s (&udp_ack->sender),
2223 udp_address_to_string (plugin,
2227 /* Remove fragmented message after successful sending */
2228 fragmented_message_done (s->frag_ctx,
2233 /* ********************** Receiving ********************** */
2237 * Closure for #find_receive_context().
2239 struct FindReceiveContext
2242 * Where to store the result.
2244 struct DefragContext *rc;
2247 * Session associated with this context.
2249 struct GNUNET_ATS_Session *session;
2254 const union UdpAddress *udp_addr;
2257 * Number of bytes in @e udp_addr.
2259 size_t udp_addr_len;
2265 * Scan the heap for a receive context with the given address.
2267 * @param cls the `struct FindReceiveContext`
2268 * @param node internal node of the heap
2269 * @param element value stored at the node (a `struct ReceiveContext`)
2270 * @param cost cost associated with the node
2271 * @return #GNUNET_YES if we should continue to iterate,
2272 * #GNUNET_NO if not.
2275 find_receive_context (void *cls,
2276 struct GNUNET_CONTAINER_HeapNode *node,
2278 GNUNET_CONTAINER_HeapCostType cost)
2280 struct FindReceiveContext *frc = cls;
2281 struct DefragContext *e = element;
2283 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2284 (0 == memcmp (frc->udp_addr,
2286 frc->udp_addr_len)) )
2296 * Message tokenizer has broken up an incomming message. Pass it on
2299 * @param cls the `struct Plugin *`
2300 * @param client the `struct GNUNET_ATS_Session *`
2301 * @param hdr the actual message
2302 * @return #GNUNET_OK (always)
2305 process_inbound_tokenized_messages (void *cls,
2307 const struct GNUNET_MessageHeader *hdr)
2309 struct Plugin *plugin = cls;
2310 struct GNUNET_ATS_Session *session = client;
2312 if (GNUNET_YES == session->in_destroy)
2314 reschedule_session_timeout (session);
2315 session->flow_delay_for_other_peer
2316 = plugin->env->receive (plugin->env->cls,
2325 * Functions with this signature are called whenever we need to close
2326 * a session due to a disconnect or failure to establish a connection.
2328 * @param cls closure with the `struct Plugin`
2329 * @param s session to close down
2330 * @return #GNUNET_OK on success
2333 udp_disconnect_session (void *cls,
2334 struct GNUNET_ATS_Session *s)
2336 struct Plugin *plugin = cls;
2337 struct UDP_MessageWrapper *udpw;
2338 struct UDP_MessageWrapper *next;
2339 struct FindReceiveContext frc;
2341 GNUNET_assert (GNUNET_YES != s->in_destroy);
2342 LOG (GNUNET_ERROR_TYPE_DEBUG,
2343 "Session %p to peer `%s' at address %s ended\n",
2345 GNUNET_i2s (&s->target),
2346 udp_address_to_string (plugin,
2347 s->address->address,
2348 s->address->address_length));
2349 if (NULL != s->timeout_task)
2351 GNUNET_SCHEDULER_cancel (s->timeout_task);
2352 s->timeout_task = NULL;
2354 if (NULL != s->frag_ctx)
2356 /* Remove fragmented message due to disconnect */
2357 fragmented_message_done (s->frag_ctx,
2360 GNUNET_assert (GNUNET_YES ==
2361 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2365 frc.udp_addr = s->address->address;
2366 frc.udp_addr_len = s->address->address_length;
2367 /* Lookup existing receive context for this address */
2368 if (NULL != plugin->defrag_ctxs)
2370 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2371 &find_receive_context,
2375 struct DefragContext *d_ctx = frc.rc;
2377 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2378 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2379 GNUNET_free (d_ctx);
2382 s->in_destroy = GNUNET_YES;
2383 next = plugin->ipv4_queue_head;
2384 while (NULL != (udpw = next))
2387 if (udpw->session == s)
2391 udpw->qc (udpw->qc_cls,
2397 next = plugin->ipv6_queue_head;
2398 while (NULL != (udpw = next))
2401 if (udpw->session == s)
2405 udpw->qc (udpw->qc_cls,
2411 if ( (NULL != s->frag_ctx) &&
2412 (NULL != s->frag_ctx->cont) )
2414 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2415 later, as it might be in use right now */
2416 LOG (GNUNET_ERROR_TYPE_DEBUG,
2417 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2418 GNUNET_i2s (&s->target));
2419 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2422 s->frag_ctx->payload_size,
2423 s->frag_ctx->on_wire_size);
2425 notify_session_monitor (s->plugin,
2427 GNUNET_TRANSPORT_SS_DONE);
2428 plugin->env->session_end (plugin->env->cls,
2431 GNUNET_STATISTICS_set (plugin->env->stats,
2432 "# UDP sessions active",
2433 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2442 * Destroy a session, plugin is being unloaded.
2444 * @param cls the `struct Plugin`
2445 * @param key hash of public key of target peer
2446 * @param value a `struct PeerSession *` to clean up
2447 * @return #GNUNET_OK (continue to iterate)
2450 disconnect_and_free_it (void *cls,
2451 const struct GNUNET_PeerIdentity *key,
2454 struct Plugin *plugin = cls;
2456 udp_disconnect_session (plugin,
2463 * Disconnect from a remote node. Clean up session if we have one for
2466 * @param cls closure for this call (should be handle to Plugin)
2467 * @param target the peeridentity of the peer to disconnect
2468 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2471 udp_disconnect (void *cls,
2472 const struct GNUNET_PeerIdentity *target)
2474 struct Plugin *plugin = cls;
2476 LOG (GNUNET_ERROR_TYPE_DEBUG,
2477 "Disconnecting from peer `%s'\n",
2478 GNUNET_i2s (target));
2479 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2481 &disconnect_and_free_it,
2487 * Session was idle, so disconnect it.
2489 * @param cls the `struct GNUNET_ATS_Session` to time out
2490 * @param tc scheduler context
2493 session_timeout (void *cls,
2494 const struct GNUNET_SCHEDULER_TaskContext *tc)
2496 struct GNUNET_ATS_Session *s = cls;
2497 struct Plugin *plugin = s->plugin;
2498 struct GNUNET_TIME_Relative left;
2500 s->timeout_task = NULL;
2501 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2502 if (left.rel_value_us > 0)
2504 /* not actually our turn yet, but let's at least update
2505 the monitor, it may think we're about to die ... */
2506 notify_session_monitor (s->plugin,
2508 GNUNET_TRANSPORT_SS_UPDATE);
2509 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2514 LOG (GNUNET_ERROR_TYPE_DEBUG,
2515 "Session %p was idle for %s, disconnecting\n",
2517 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2519 /* call session destroy function */
2520 udp_disconnect_session (plugin,
2526 * Allocate a new session for the given endpoint address.
2527 * Note that this function does not inform the service
2528 * of the new session, this is the responsibility of the
2529 * caller (if needed).
2531 * @param cls the `struct Plugin`
2532 * @param address address of the other peer to use
2533 * @param network_type network type the address belongs to
2534 * @return NULL on error, otherwise session handle
2536 static struct GNUNET_ATS_Session *
2537 udp_plugin_create_session (void *cls,
2538 const struct GNUNET_HELLO_Address *address,
2539 enum GNUNET_ATS_Network_Type network_type)
2541 struct Plugin *plugin = cls;
2542 struct GNUNET_ATS_Session *s;
2544 s = GNUNET_new (struct GNUNET_ATS_Session);
2546 s->address = GNUNET_HELLO_address_copy (address);
2547 s->target = address->peer;
2548 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2549 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2551 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2552 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2553 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2554 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2555 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2558 s->scope = network_type;
2560 LOG (GNUNET_ERROR_TYPE_DEBUG,
2561 "Creating new session %p for peer `%s' address `%s'\n",
2563 GNUNET_i2s (&address->peer),
2564 udp_address_to_string (plugin,
2566 address->address_length));
2567 GNUNET_assert (GNUNET_OK ==
2568 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2571 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2572 GNUNET_STATISTICS_set (plugin->env->stats,
2573 "# UDP sessions active",
2574 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2576 notify_session_monitor (plugin,
2578 GNUNET_TRANSPORT_SS_INIT);
2584 * Creates a new outbound session the transport service will use to
2585 * send data to the peer.
2587 * @param cls the `struct Plugin *`
2588 * @param address the address
2589 * @return the session or NULL of max connections exceeded
2591 static struct GNUNET_ATS_Session *
2592 udp_plugin_get_session (void *cls,
2593 const struct GNUNET_HELLO_Address *address)
2595 struct Plugin *plugin = cls;
2596 struct GNUNET_ATS_Session *s;
2597 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2598 const struct IPv4UdpAddress *udp_v4;
2599 const struct IPv6UdpAddress *udp_v6;
2601 if (NULL == address)
2606 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2607 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2609 GNUNET_break_op (0);
2612 if (NULL != (s = udp_plugin_lookup_session (cls,
2616 /* need to create new session */
2617 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2619 struct sockaddr_in v4;
2621 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2622 memset (&v4, '\0', sizeof (v4));
2623 v4.sin_family = AF_INET;
2624 #if HAVE_SOCKADDR_IN_SIN_LEN
2625 v4.sin_len = sizeof (struct sockaddr_in);
2627 v4.sin_port = udp_v4->u4_port;
2628 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2629 network_type = plugin->env->get_address_type (plugin->env->cls,
2630 (const struct sockaddr *) &v4,
2633 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2635 struct sockaddr_in6 v6;
2637 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2638 memset (&v6, '\0', sizeof (v6));
2639 v6.sin6_family = AF_INET6;
2640 #if HAVE_SOCKADDR_IN_SIN_LEN
2641 v6.sin6_len = sizeof (struct sockaddr_in6);
2643 v6.sin6_port = udp_v6->u6_port;
2644 v6.sin6_addr = udp_v6->ipv6_addr;
2645 network_type = plugin->env->get_address_type (plugin->env->cls,
2646 (const struct sockaddr *) &v6,
2649 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2650 return udp_plugin_create_session (cls,
2657 * We've received a UDP Message. Process it (pass contents to main service).
2659 * @param plugin plugin context
2660 * @param msg the message
2661 * @param udp_addr sender address
2662 * @param udp_addr_len number of bytes in @a udp_addr
2663 * @param network_type network type the address belongs to
2666 process_udp_message (struct Plugin *plugin,
2667 const struct UDPMessage *msg,
2668 const union UdpAddress *udp_addr,
2669 size_t udp_addr_len,
2670 enum GNUNET_ATS_Network_Type network_type)
2672 struct GNUNET_ATS_Session *s;
2673 struct GNUNET_HELLO_Address *address;
2675 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2676 if (0 != ntohl (msg->reserved))
2681 if (ntohs (msg->header.size)
2682 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2688 address = GNUNET_HELLO_address_allocate (&msg->sender,
2692 GNUNET_HELLO_ADDRESS_INFO_NONE);
2694 (s = udp_plugin_lookup_session (plugin,
2697 s = udp_plugin_create_session (plugin,
2700 plugin->env->session_start (plugin->env->cls,
2704 notify_session_monitor (plugin,
2706 GNUNET_TRANSPORT_SS_UP);
2708 GNUNET_free (address);
2711 GNUNET_SERVER_mst_receive (plugin->mst,
2713 (const char *) &msg[1],
2714 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2718 if ( (0 == s->rc) &&
2719 (GNUNET_YES == s->in_destroy) )
2725 * Process a defragmented message.
2727 * @param cls the `struct DefragContext *`
2728 * @param msg the message
2731 fragment_msg_proc (void *cls,
2732 const struct GNUNET_MessageHeader *msg)
2734 struct DefragContext *dc = cls;
2735 const struct UDPMessage *um;
2737 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2739 GNUNET_break_op (0);
2742 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2744 GNUNET_break_op (0);
2747 um = (const struct UDPMessage *) msg;
2748 dc->sender = um->sender;
2749 dc->have_sender = GNUNET_YES;
2750 process_udp_message (dc->plugin,
2759 * We finished sending an acknowledgement. Update
2762 * @param cls the `struct Plugin`
2763 * @param udpw message queue entry of the ACK
2764 * @param result #GNUNET_OK if the transmission worked,
2765 * #GNUNET_SYSERR if we failed to send the ACK
2768 ack_message_sent (void *cls,
2769 struct UDP_MessageWrapper *udpw,
2772 struct Plugin *plugin = cls;
2774 if (GNUNET_OK == result)
2776 GNUNET_STATISTICS_update (plugin->env->stats,
2777 "# UDP, ACK messages sent",
2783 GNUNET_STATISTICS_update (plugin->env->stats,
2784 "# UDP, ACK transmissions failed",
2792 * Transmit an acknowledgement.
2794 * @param cls the `struct DefragContext *`
2795 * @param id message ID (unused)
2796 * @param msg ack to transmit
2799 ack_proc (void *cls,
2801 const struct GNUNET_MessageHeader *msg)
2803 struct DefragContext *rc = cls;
2804 struct Plugin *plugin = rc->plugin;
2805 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2806 struct UDP_ACK_Message *udp_ack;
2808 struct UDP_MessageWrapper *udpw;
2809 struct GNUNET_ATS_Session *s;
2810 struct GNUNET_HELLO_Address *address;
2812 if (GNUNET_NO == rc->have_sender)
2814 /* tried to defragment but never succeeded, hence will not ACK */
2815 /* This can happen if we just lost msgs */
2816 GNUNET_STATISTICS_update (plugin->env->stats,
2817 "# UDP, fragments discarded without ACK",
2822 address = GNUNET_HELLO_address_allocate (&rc->sender,
2826 GNUNET_HELLO_ADDRESS_INFO_NONE);
2827 s = udp_plugin_lookup_session (plugin,
2829 GNUNET_HELLO_address_free (address);
2832 LOG (GNUNET_ERROR_TYPE_ERROR,
2833 "Trying to transmit ACK to peer `%s' but no session found!\n",
2834 udp_address_to_string (plugin,
2837 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2838 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2840 GNUNET_STATISTICS_update (plugin->env->stats,
2841 "# UDP, ACK transmissions failed",
2846 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2847 delay = s->flow_delay_for_other_peer.rel_value_us;
2850 LOG (GNUNET_ERROR_TYPE_DEBUG,
2851 "Sending ACK to `%s' including delay of %s\n",
2852 udp_address_to_string (plugin,
2855 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2857 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2858 udpw->msg_size = msize;
2859 udpw->payload_size = 0;
2861 udpw->start_time = GNUNET_TIME_absolute_get ();
2862 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2863 udpw->msg_buf = (char *) &udpw[1];
2864 udpw->qc = &ack_message_sent;
2865 udpw->qc_cls = plugin;
2866 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2867 udp_ack->header.size = htons ((uint16_t) msize);
2868 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2869 udp_ack->delay = htonl (delay);
2870 udp_ack->sender = *plugin->env->my_identity;
2871 memcpy (&udp_ack[1],
2876 notify_session_monitor (plugin,
2878 GNUNET_TRANSPORT_SS_UPDATE);
2879 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2880 schedule_select_v4 (plugin);
2882 schedule_select_v6 (plugin);
2887 * We received a fragment, process it.
2889 * @param plugin our plugin
2890 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2891 * @param udp_addr sender address
2892 * @param udp_addr_len number of bytes in @a udp_addr
2893 * @param network_type network type the address belongs to
2896 read_process_fragment (struct Plugin *plugin,
2897 const struct GNUNET_MessageHeader *msg,
2898 const union UdpAddress *udp_addr,
2899 size_t udp_addr_len,
2900 enum GNUNET_ATS_Network_Type network_type)
2902 struct DefragContext *d_ctx;
2903 struct GNUNET_TIME_Absolute now;
2904 struct FindReceiveContext frc;
2907 frc.udp_addr = udp_addr;
2908 frc.udp_addr_len = udp_addr_len;
2910 /* Lookup existing receive context for this address */
2911 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2912 &find_receive_context,
2914 now = GNUNET_TIME_absolute_get ();
2919 /* Create a new defragmentation context */
2920 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2924 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2925 d_ctx->udp_addr_len = udp_addr_len;
2926 d_ctx->network_type = network_type;
2927 d_ctx->plugin = plugin;
2928 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2930 UDP_MAX_MESSAGES_IN_DEFRAG,
2934 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2936 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2937 LOG (GNUNET_ERROR_TYPE_DEBUG,
2938 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2939 (unsigned int) ntohs (msg->size),
2940 udp_address_to_string (plugin,
2946 LOG (GNUNET_ERROR_TYPE_DEBUG,
2947 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2948 (unsigned int) ntohs (msg->size),
2949 udp_address_to_string (plugin,
2955 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
2958 /* keep this 'rc' from expiring */
2959 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2961 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2963 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2964 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2966 /* remove 'rc' that was inactive the longest */
2967 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2968 GNUNET_assert (NULL != d_ctx);
2969 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2970 GNUNET_free (d_ctx);
2971 GNUNET_STATISTICS_update (plugin->env->stats,
2972 "# UDP, Defragmentations aborted",
2980 * Read and process a message from the given socket.
2982 * @param plugin the overall plugin
2983 * @param rsock socket to read from
2986 udp_select_read (struct Plugin *plugin,
2987 struct GNUNET_NETWORK_Handle *rsock)
2990 struct sockaddr_storage addr;
2991 char buf[65536] GNUNET_ALIGN;
2993 const struct GNUNET_MessageHeader *msg;
2994 struct IPv4UdpAddress v4;
2995 struct IPv6UdpAddress v6;
2996 const struct sockaddr *sa;
2997 const struct sockaddr_in *sa4;
2998 const struct sockaddr_in6 *sa6;
2999 const union UdpAddress *int_addr;
3000 size_t int_addr_len;
3001 enum GNUNET_ATS_Network_Type network_type;
3003 fromlen = sizeof (addr);
3007 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3010 (struct sockaddr *) &addr,
3012 sa = (const struct sockaddr *) &addr;
3014 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3015 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3016 * on this socket has failed.
3018 * WSAECONNRESET - The virtual circuit was reset by the remote side
3019 * executing a hard or abortive close. The application should close
3020 * the socket; it is no longer usable. On a UDP-datagram socket this
3021 * error indicates a previous send operation resulted in an ICMP Port
3022 * Unreachable message.
3024 if ( (-1 == size) &&
3025 (ECONNRESET == errno) )
3030 LOG (GNUNET_ERROR_TYPE_DEBUG,
3031 "UDP failed to receive data: %s\n",
3033 /* Connection failure or something. Not a protocol violation. */
3037 /* Check if this is a STUN packet */
3038 if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
3041 return; /* was STUN, do not process further */
3043 if (size < sizeof(struct GNUNET_MessageHeader))
3045 LOG (GNUNET_ERROR_TYPE_WARNING,
3046 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3047 (unsigned int ) size,
3050 /* _MAY_ be a connection failure (got partial message) */
3051 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3052 GNUNET_break_op (0);
3059 msg = (const struct GNUNET_MessageHeader *) buf;
3060 LOG (GNUNET_ERROR_TYPE_DEBUG,
3061 "UDP received %u-byte message from `%s' type %u\n",
3062 (unsigned int) size,
3066 if (size != ntohs (msg->size))
3068 LOG (GNUNET_ERROR_TYPE_WARNING,
3069 "UDP malformed message header from %s\n",
3070 (unsigned int) size,
3073 GNUNET_break_op (0);
3076 GNUNET_STATISTICS_update (plugin->env->stats,
3077 "# UDP, total bytes received",
3080 network_type = plugin->env->get_address_type (plugin->env->cls,
3083 switch (sa->sa_family)
3086 sa4 = (const struct sockaddr_in *) &addr;
3088 v4.ipv4_addr = sa4->sin_addr.s_addr;
3089 v4.u4_port = sa4->sin_port;
3090 int_addr = (union UdpAddress *) &v4;
3091 int_addr_len = sizeof (v4);
3094 sa6 = (const struct sockaddr_in6 *) &addr;
3096 v6.ipv6_addr = sa6->sin6_addr;
3097 v6.u6_port = sa6->sin6_port;
3098 int_addr = (union UdpAddress *) &v6;
3099 int_addr_len = sizeof (v6);
3106 switch (ntohs (msg->type))
3108 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3109 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3110 udp_broadcast_receive (plugin,
3117 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3118 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3123 process_udp_message (plugin,
3124 (const struct UDPMessage *) msg,
3129 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3130 read_process_ack (plugin,
3135 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3136 read_process_fragment (plugin,
3150 * Removes messages from the transmission queue that have
3151 * timed out, and then selects a message that should be
3154 * @param plugin the UDP plugin
3155 * @param sock which socket should we process the queue for (v4 or v6)
3156 * @return message selected for transmission, or NULL for none
3158 static struct UDP_MessageWrapper *
3159 remove_timeout_messages_and_select (struct Plugin *plugin,
3160 struct GNUNET_NETWORK_Handle *sock)
3162 struct UDP_MessageWrapper *udpw;
3163 struct GNUNET_TIME_Relative remaining;
3164 struct GNUNET_ATS_Session *session;
3167 removed = GNUNET_NO;
3168 udpw = (sock == plugin->sockv4)
3169 ? plugin->ipv4_queue_head
3170 : plugin->ipv6_queue_head;
3171 while (NULL != udpw)
3173 session = udpw->session;
3174 /* Find messages with timeout */
3175 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3176 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3178 /* Message timed out */
3179 removed = GNUNET_YES;
3182 udpw->qc (udpw->qc_cls,
3187 if (sock == plugin->sockv4)
3189 udpw = plugin->ipv4_queue_head;
3191 else if (sock == plugin->sockv6)
3193 udpw = plugin->ipv6_queue_head;
3197 GNUNET_break (0); /* should never happen */
3200 GNUNET_STATISTICS_update (plugin->env->stats,
3201 "# messages discarded due to timeout",
3207 /* Message did not time out, check transmission time */
3208 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3209 if (0 == remaining.rel_value_us)
3211 /* this message is not delayed */
3212 LOG (GNUNET_ERROR_TYPE_DEBUG,
3213 "Message for peer `%s' (%u bytes) is not delayed \n",
3214 GNUNET_i2s (&udpw->session->target),
3215 udpw->payload_size);
3216 break; /* Found message to send, break */
3220 /* Message is delayed, try next */
3221 LOG (GNUNET_ERROR_TYPE_DEBUG,
3222 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3223 GNUNET_i2s (&udpw->session->target),
3225 GNUNET_STRINGS_relative_time_to_string (remaining,
3231 if (GNUNET_YES == removed)
3232 notify_session_monitor (session->plugin,
3234 GNUNET_TRANSPORT_SS_UPDATE);
3240 * We failed to transmit a message via UDP. Generate
3241 * a descriptive error message.
3243 * @param plugin our plugin
3244 * @param sa target address we were trying to reach
3245 * @param slen number of bytes in @a sa
3246 * @param error the errno value returned from the sendto() call
3249 analyze_send_error (struct Plugin *plugin,
3250 const struct sockaddr *sa,
3254 enum GNUNET_ATS_Network_Type type;
3256 type = plugin->env->get_address_type (plugin->env->cls,
3259 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3260 (GNUNET_ATS_NET_WAN == type) ) &&
3261 ( (ENETUNREACH == errno) ||
3262 (ENETDOWN == errno) ) )
3264 if (slen == sizeof (struct sockaddr_in))
3266 /* IPv4: "Network unreachable" or "Network down"
3268 * This indicates we do not have connectivity
3270 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3271 _("UDP could not transmit message to `%s': "
3272 "Network seems down, please check your network configuration\n"),
3276 if (slen == sizeof (struct sockaddr_in6))
3278 /* IPv6: "Network unreachable" or "Network down"
3280 * This indicates that this system is IPv6 enabled, but does not
3281 * have a valid global IPv6 address assigned or we do not have
3284 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3285 _("UDP could not transmit IPv6 message! "
3286 "Please check your network configuration and disable IPv6 if your "
3287 "connection does not have a global IPv6 address\n"));
3292 LOG (GNUNET_ERROR_TYPE_WARNING,
3293 "UDP could not transmit message to `%s': `%s'\n",
3302 * It is time to try to transmit a UDP message. Select one
3305 * @param plugin the plugin
3306 * @param sock which socket (v4/v6) to send on
3309 udp_select_send (struct Plugin *plugin,
3310 struct GNUNET_NETWORK_Handle *sock)
3314 const struct sockaddr *a;
3315 const struct IPv4UdpAddress *u4;
3316 struct sockaddr_in a4;
3317 const struct IPv6UdpAddress *u6;
3318 struct sockaddr_in6 a6;
3319 struct UDP_MessageWrapper *udpw;
3321 /* Find message(s) to send */
3322 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3325 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3327 u4 = udpw->session->address->address;
3331 a4.sin_family = AF_INET;
3332 #if HAVE_SOCKADDR_IN_SIN_LEN
3333 a4.sin_len = sizeof (a4);
3335 a4.sin_port = u4->u4_port;
3336 a4.sin_addr.s_addr = u4->ipv4_addr;
3337 a = (const struct sockaddr *) &a4;
3340 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3342 u6 = udpw->session->address->address;
3346 a6.sin6_family = AF_INET6;
3347 #if HAVE_SOCKADDR_IN_SIN_LEN
3348 a6.sin6_len = sizeof (a6);
3350 a6.sin6_port = u6->u6_port;
3351 a6.sin6_addr = u6->ipv6_addr;
3352 a = (const struct sockaddr *) &a6;
3360 udpw->qc (udpw->qc_cls,
3363 notify_session_monitor (plugin,
3365 GNUNET_TRANSPORT_SS_UPDATE);
3369 sent = GNUNET_NETWORK_socket_sendto (sock,
3374 udpw->session->last_transmit_time
3375 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3376 udpw->session->last_transmit_time);
3379 if (GNUNET_SYSERR == sent)
3382 analyze_send_error (plugin,
3386 udpw->qc (udpw->qc_cls,
3389 GNUNET_STATISTICS_update (plugin->env->stats,
3390 "# UDP, total, bytes, sent, failure",
3393 GNUNET_STATISTICS_update (plugin->env->stats,
3394 "# UDP, total, messages, sent, failure",
3401 LOG (GNUNET_ERROR_TYPE_DEBUG,
3402 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3403 (unsigned int) (udpw->msg_size),
3404 GNUNET_i2s (&udpw->session->target),
3408 (sent < 0) ? STRERROR (errno) : "ok");
3409 GNUNET_STATISTICS_update (plugin->env->stats,
3410 "# UDP, total, bytes, sent, success",
3413 GNUNET_STATISTICS_update (plugin->env->stats,
3414 "# UDP, total, messages, sent, success",
3417 if (NULL != udpw->frag_ctx)
3418 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3419 udpw->qc (udpw->qc_cls,
3423 notify_session_monitor (plugin,
3425 GNUNET_TRANSPORT_SS_UPDATE);
3431 /* ***************** Event loop (part 2) *************** */
3435 * We have been notified that our readset has something to read. We don't
3436 * know which socket needs to be read, so we have to check each one
3437 * Then reschedule this function to be called again once more is available.
3439 * @param cls the plugin handle
3440 * @param tc the scheduling context
3443 udp_plugin_select_v4 (void *cls,
3444 const struct GNUNET_SCHEDULER_TaskContext *tc)
3446 struct Plugin *plugin = cls;
3448 plugin->select_task_v4 = NULL;
3449 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3451 if (NULL == plugin->sockv4)
3453 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3454 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3456 udp_select_read (plugin,
3458 udp_select_send (plugin,
3460 schedule_select_v4 (plugin);
3465 * We have been notified that our readset has something to read. We don't
3466 * know which socket needs to be read, so we have to check each one
3467 * Then reschedule this function to be called again once more is available.
3469 * @param cls the plugin handle
3470 * @param tc the scheduling context
3473 udp_plugin_select_v6 (void *cls,
3474 const struct GNUNET_SCHEDULER_TaskContext *tc)
3476 struct Plugin *plugin = cls;
3478 plugin->select_task_v6 = NULL;
3479 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3481 if (NULL == plugin->sockv6)
3483 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3484 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3486 udp_select_read (plugin,
3489 udp_select_send (plugin,
3491 schedule_select_v6 (plugin);
3495 /* ******************* Initialization *************** */
3499 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3501 * @param plugin the plugin to initialize
3502 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3503 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3504 * @return number of sockets that were successfully bound
3507 setup_sockets (struct Plugin *plugin,
3508 const struct sockaddr_in6 *bind_v6,
3509 const struct sockaddr_in *bind_v4)
3512 int sockets_created = 0;
3513 struct sockaddr_in6 server_addrv6;
3514 struct sockaddr_in server_addrv4;
3515 const struct sockaddr *server_addr;
3516 const struct sockaddr *addrs[2];
3517 socklen_t addrlens[2];
3521 /* Create IPv6 socket */
3523 if (GNUNET_YES == plugin->enable_ipv6)
3525 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3528 if (NULL == plugin->sockv6)
3530 LOG (GNUNET_ERROR_TYPE_INFO,
3531 _("Disabling IPv6 since it is not supported on this system!\n"));
3532 plugin->enable_ipv6 = GNUNET_NO;
3536 memset (&server_addrv6,
3538 sizeof(struct sockaddr_in6));
3539 #if HAVE_SOCKADDR_IN_SIN_LEN
3540 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3542 server_addrv6.sin6_family = AF_INET6;
3543 if (NULL != bind_v6)
3544 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3546 server_addrv6.sin6_addr = in6addr_any;
3548 if (0 == plugin->port) /* autodetect */
3549 server_addrv6.sin6_port
3550 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3554 server_addrv6.sin6_port = htons (plugin->port);
3555 addrlen = sizeof (struct sockaddr_in6);
3556 server_addr = (const struct sockaddr *) &server_addrv6;
3561 LOG(GNUNET_ERROR_TYPE_DEBUG,
3562 "Binding to IPv6 `%s'\n",
3563 GNUNET_a2s (server_addr,
3567 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3572 if (0 != plugin->port)
3574 tries = 10; /* fail immediately */
3575 break; /* bind failed on specific port */
3578 server_addrv6.sin6_port
3579 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3586 GNUNET_NETWORK_socket_close (plugin->sockv6);
3587 plugin->enable_ipv6 = GNUNET_NO;
3588 plugin->sockv6 = NULL;
3592 plugin->port = ntohs (server_addrv6.sin6_port);
3594 if (NULL != plugin->sockv6)
3596 LOG (GNUNET_ERROR_TYPE_DEBUG,
3597 "IPv6 UDP socket created listinging at %s\n",
3598 GNUNET_a2s (server_addr,
3600 addrs[sockets_created] = server_addr;
3601 addrlens[sockets_created] = addrlen;
3606 LOG (GNUNET_ERROR_TYPE_WARNING,
3607 _("Failed to bind UDP socket to %s: %s\n"),
3608 GNUNET_a2s (server_addr,
3615 /* Create IPv4 socket */
3617 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3620 if (NULL == plugin->sockv4)
3622 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3624 LOG (GNUNET_ERROR_TYPE_INFO,
3625 _("Disabling IPv4 since it is not supported on this system!\n"));
3626 plugin->enable_ipv4 = GNUNET_NO;
3630 memset (&server_addrv4,
3632 sizeof(struct sockaddr_in));
3633 #if HAVE_SOCKADDR_IN_SIN_LEN
3634 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3636 server_addrv4.sin_family = AF_INET;
3637 if (NULL != bind_v4)
3638 server_addrv4.sin_addr = bind_v4->sin_addr;
3640 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3642 if (0 == plugin->port)
3644 server_addrv4.sin_port
3645 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3649 server_addrv4.sin_port = htons (plugin->port);
3651 addrlen = sizeof (struct sockaddr_in);
3652 server_addr = (const struct sockaddr *) &server_addrv4;
3657 LOG (GNUNET_ERROR_TYPE_DEBUG,
3658 "Binding to IPv4 `%s'\n",
3659 GNUNET_a2s (server_addr,
3664 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3669 if (0 != plugin->port)
3671 tries = 10; /* fail */
3672 break; /* bind failed on specific port */
3676 server_addrv4.sin_port
3677 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3684 GNUNET_NETWORK_socket_close (plugin->sockv4);
3685 plugin->enable_ipv4 = GNUNET_NO;
3686 plugin->sockv4 = NULL;
3690 plugin->port = ntohs (server_addrv4.sin_port);
3693 if (NULL != plugin->sockv4)
3695 LOG (GNUNET_ERROR_TYPE_DEBUG,
3696 "IPv4 socket created on port %s\n",
3697 GNUNET_a2s (server_addr,
3699 addrs[sockets_created] = server_addr;
3700 addrlens[sockets_created] = addrlen;
3705 LOG (GNUNET_ERROR_TYPE_ERROR,
3706 _("Failed to bind UDP socket to %s: %s\n"),
3707 GNUNET_a2s (server_addr,
3713 if (0 == sockets_created)
3715 LOG (GNUNET_ERROR_TYPE_WARNING,
3716 _("Failed to open UDP sockets\n"));
3717 return 0; /* No sockets created, return */
3719 schedule_select_v4 (plugin);
3720 schedule_select_v6 (plugin);
3721 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3727 &udp_nat_port_map_callback,
3731 return sockets_created;
3736 * The exported method. Makes the core api available via a global and
3737 * returns the udp transport API.
3739 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3740 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3743 libgnunet_plugin_transport_udp_init (void *cls)
3745 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3746 struct GNUNET_TRANSPORT_PluginFunctions *api;
3748 unsigned long long port;
3749 unsigned long long aport;
3750 unsigned long long udp_max_bps;
3751 unsigned long long enable_v6;
3752 unsigned long long enable_broadcasting;
3753 unsigned long long enable_broadcasting_recv;
3754 char *bind4_address;
3755 char *bind6_address;
3756 struct GNUNET_TIME_Relative interval;
3757 struct sockaddr_in server_addrv4;
3758 struct sockaddr_in6 server_addrv6;
3763 if (NULL == env->receive)
3765 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3766 initialze the plugin or the API */
3767 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3769 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3770 api->address_to_string = &udp_address_to_string;
3771 api->string_to_address = &udp_string_to_address;
3775 /* Get port number: port == 0 : autodetect a port,
3776 * > 0 : use this port, not given : 2086 default */
3778 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3785 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3788 _("must be in [0,65535]"));
3792 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3799 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3802 _("must be in [0,65535]"));
3807 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3810 enable_v6 = GNUNET_NO;
3812 enable_v6 = GNUNET_YES;
3814 have_bind4 = GNUNET_NO;
3815 memset (&server_addrv4,
3817 sizeof (server_addrv4));
3819 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3824 LOG (GNUNET_ERROR_TYPE_DEBUG,
3825 "Binding UDP plugin to specific address: `%s'\n",
3827 if (1 != inet_pton (AF_INET,
3829 &server_addrv4.sin_addr))
3831 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3834 _("must be valid IPv4 address"));
3835 GNUNET_free (bind4_address);
3838 have_bind4 = GNUNET_YES;
3840 GNUNET_free_non_null (bind4_address);
3841 have_bind6 = GNUNET_NO;
3842 memset (&server_addrv6,
3844 sizeof (server_addrv6));
3846 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3851 LOG (GNUNET_ERROR_TYPE_DEBUG,
3852 "Binding udp plugin to specific address: `%s'\n",
3854 if (1 != inet_pton (AF_INET6,
3856 &server_addrv6.sin6_addr))
3858 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3861 _("must be valid IPv6 address"));
3862 GNUNET_free (bind6_address);
3865 have_bind6 = GNUNET_YES;
3867 GNUNET_free_non_null (bind6_address);
3869 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3872 if (enable_broadcasting == GNUNET_SYSERR)
3873 enable_broadcasting = GNUNET_NO;
3875 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3877 "BROADCAST_RECEIVE");
3878 if (enable_broadcasting_recv == GNUNET_SYSERR)
3879 enable_broadcasting_recv = GNUNET_YES;
3881 if (GNUNET_SYSERR ==
3882 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3884 "BROADCAST_INTERVAL",
3887 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3891 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3896 /* 50 MB/s == infinity for practical purposes */
3897 udp_max_bps = 1024 * 1024 * 50;
3900 p = GNUNET_new (struct Plugin);
3903 p->broadcast_interval = interval;
3904 p->enable_ipv6 = enable_v6;
3905 p->enable_ipv4 = GNUNET_YES; /* default */
3906 p->enable_broadcasting = enable_broadcasting;
3907 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3909 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3911 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3912 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3914 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3917 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3919 res = setup_sockets (p,
3920 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3921 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3923 ( (NULL == p->sockv4) &&
3924 (NULL == p->sockv6) ) )
3926 LOG (GNUNET_ERROR_TYPE_ERROR,
3927 _("Failed to create UDP network sockets\n"));
3928 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3929 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3930 GNUNET_SERVER_mst_destroy (p->mst);
3935 /* Setup broadcasting and receiving beacons */
3940 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3942 api->disconnect_session = &udp_disconnect_session;
3943 api->query_keepalive_factor = &udp_query_keepalive_factor;
3944 api->disconnect_peer = &udp_disconnect;
3945 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3946 api->address_to_string = &udp_address_to_string;
3947 api->string_to_address = &udp_string_to_address;
3948 api->check_address = &udp_plugin_check_address;
3949 api->get_session = &udp_plugin_get_session;
3950 api->send = &udp_plugin_send;
3951 api->get_network = &udp_plugin_get_network;
3952 api->get_network_for_address = &udp_plugin_get_network_for_address;
3953 api->update_session_timeout = &udp_plugin_update_session_timeout;
3954 api->setup_monitor = &udp_plugin_setup_monitor;
3960 * Function called on each entry in the defragmentation heap to
3964 * @param node node in the heap (to be removed)
3965 * @param element a `struct DefragContext` to be cleaned up
3966 * @param cost unused
3967 * @return #GNUNET_YES
3970 heap_cleanup_iterator (void *cls,
3971 struct GNUNET_CONTAINER_HeapNode *node,
3973 GNUNET_CONTAINER_HeapCostType cost)
3975 struct DefragContext *d_ctx = element;
3977 GNUNET_CONTAINER_heap_remove_node (node);
3978 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3979 GNUNET_free (d_ctx);
3985 * The exported method. Makes the core api available via a global and
3986 * returns the udp transport API.
3988 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3992 libgnunet_plugin_transport_udp_done (void *cls)
3994 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3995 struct Plugin *plugin = api->cls;
3996 struct PrettyPrinterContext *cur;
3997 struct UDP_MessageWrapper *udpw;
4004 stop_broadcast (plugin);
4005 if (NULL != plugin->select_task_v4)
4007 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4008 plugin->select_task_v4 = NULL;
4010 if (NULL != plugin->select_task_v6)
4012 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4013 plugin->select_task_v6 = NULL;
4015 if (NULL != plugin->sockv4)
4017 GNUNET_break (GNUNET_OK ==
4018 GNUNET_NETWORK_socket_close (plugin->sockv4));
4019 plugin->sockv4 = NULL;
4021 if (NULL != plugin->sockv6)
4023 GNUNET_break (GNUNET_OK ==
4024 GNUNET_NETWORK_socket_close (plugin->sockv6));
4025 plugin->sockv6 = NULL;
4027 if (NULL != plugin->nat)
4029 GNUNET_NAT_unregister (plugin->nat);
4032 if (NULL != plugin->defrag_ctxs)
4034 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4035 &heap_cleanup_iterator,
4037 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4038 plugin->defrag_ctxs = NULL;
4040 if (NULL != plugin->mst)
4042 GNUNET_SERVER_mst_destroy (plugin->mst);
4045 while (NULL != (udpw = plugin->ipv4_queue_head))
4049 udpw->qc (udpw->qc_cls,
4054 while (NULL != (udpw = plugin->ipv6_queue_head))
4058 udpw->qc (udpw->qc_cls,
4063 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4064 &disconnect_and_free_it,
4066 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4068 while (NULL != (cur = plugin->ppc_dll_head))
4071 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4072 plugin->ppc_dll_tail,
4074 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4075 if (NULL != cur->timeout_task)
4077 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4078 cur->timeout_task = NULL;
4082 GNUNET_free (plugin);
4087 /* end of plugin_transport_udp.c */