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 GNUNET_TIME_Relative delay;
752 struct UDP_MessageWrapper *udpw;
753 struct UDP_MessageWrapper *min_udpw;
755 if ( (GNUNET_YES == plugin->enable_ipv4) &&
756 (NULL != plugin->sockv4) )
758 /* Find a message ready to send:
759 * Flow delay from other peer is expired or not set (0) */
760 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
762 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
764 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
765 if (delay.rel_value_us < min_delay.rel_value_us)
771 if (NULL != plugin->select_task_v4)
772 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
773 if (NULL != min_udpw)
775 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
778 "Calculated flow delay for UDPv4 at %s for %s\n",
779 GNUNET_STRINGS_relative_time_to_string (min_delay,
781 GNUNET_i2s (&udpw->session->target));
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
786 "Calculated flow delay for UDPv4 at %s for %s\n",
787 GNUNET_STRINGS_relative_time_to_string (min_delay,
789 GNUNET_i2s (&udpw->session->target));
792 plugin->select_task_v4
793 = GNUNET_SCHEDULER_add_read_net (min_delay,
795 &udp_plugin_select_v4,
802 * (re)schedule IPv6-select tasks for this plugin.
804 * @param plugin plugin to reschedule
807 schedule_select_v6 (struct Plugin *plugin)
809 struct GNUNET_TIME_Relative min_delay;
810 struct GNUNET_TIME_Relative delay;
811 struct UDP_MessageWrapper *udpw;
812 struct UDP_MessageWrapper *min_udpw;
814 if ( (GNUNET_YES == plugin->enable_ipv6) &&
815 (NULL != plugin->sockv6) )
817 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
819 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
821 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
822 if (delay.rel_value_us < min_delay.rel_value_us)
828 if (NULL != plugin->select_task_v6)
829 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
830 if (NULL != min_udpw)
832 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
834 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
835 "Calculated flow delay for UDPv6 at %s for %s\n",
836 GNUNET_STRINGS_relative_time_to_string (min_delay,
838 GNUNET_i2s (&udpw->session->target));
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Calculated flow delay for UDPv6 at %s for %s\n",
844 GNUNET_STRINGS_relative_time_to_string (min_delay,
846 GNUNET_i2s (&udpw->session->target));
849 plugin->select_task_v6
850 = GNUNET_SCHEDULER_add_read_net (min_delay,
852 &udp_plugin_select_v6,
858 /* ******************* Address to string and back ***************** */
862 * Function called for a quick conversion of the binary address to
863 * a numeric address. Note that the caller must not free the
864 * address and that the next call to this function is allowed
865 * to override the address again.
868 * @param addr binary address (a `union UdpAddress`)
869 * @param addrlen length of the @a addr
870 * @return string representing the same address
873 udp_address_to_string (void *cls,
877 static char rbuf[INET6_ADDRSTRLEN + 10];
878 char buf[INET6_ADDRSTRLEN];
882 const struct IPv4UdpAddress *t4;
883 const struct IPv6UdpAddress *t6;
894 if (addrlen == sizeof(struct IPv6UdpAddress))
898 options = ntohl (t6->options);
899 port = ntohs (t6->u6_port);
903 else if (addrlen == sizeof(struct IPv4UdpAddress))
907 options = ntohl (t4->options);
908 port = ntohs (t4->u4_port);
909 a4.s_addr = t4->ipv4_addr;
921 GNUNET_snprintf (rbuf,
935 * Function called to convert a string address to a binary address.
937 * @param cls closure (`struct Plugin *`)
938 * @param addr string address
939 * @param addrlen length of the address
940 * @param buf location to store the buffer
941 * @param added location to store the number of bytes in the buffer.
942 * If the function returns #GNUNET_SYSERR, its contents are undefined.
943 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
946 udp_string_to_address (void *cls,
952 struct sockaddr_storage socket_address;
958 /* Format tcp.options.address:port */
963 if ((NULL == addr) || (0 == addrlen))
966 return GNUNET_SYSERR;
968 if ('\0' != addr[addrlen - 1])
971 return GNUNET_SYSERR;
973 if (strlen (addr) != addrlen - 1)
976 return GNUNET_SYSERR;
978 plugin = GNUNET_strdup (addr);
979 optionstr = strchr (plugin, '.');
980 if (NULL == optionstr)
983 GNUNET_free (plugin);
984 return GNUNET_SYSERR;
988 options = atol (optionstr);
989 address = strchr (optionstr, '.');
993 GNUNET_free (plugin);
994 return GNUNET_SYSERR;
1000 GNUNET_STRINGS_to_address_ip (address,
1005 GNUNET_free (plugin);
1006 return GNUNET_SYSERR;
1008 GNUNET_free(plugin);
1010 switch (socket_address.ss_family)
1014 struct IPv4UdpAddress *u4;
1015 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1017 u4 = GNUNET_new (struct IPv4UdpAddress);
1018 u4->options = htonl (options);
1019 u4->ipv4_addr = in4->sin_addr.s_addr;
1020 u4->u4_port = in4->sin_port;
1022 *added = sizeof (struct IPv4UdpAddress);
1027 struct IPv6UdpAddress *u6;
1028 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1030 u6 = GNUNET_new (struct IPv6UdpAddress);
1031 u6->options = htonl (options);
1032 u6->ipv6_addr = in6->sin6_addr;
1033 u6->u6_port = in6->sin6_port;
1035 *added = sizeof (struct IPv6UdpAddress);
1040 return GNUNET_SYSERR;
1046 * Append our port and forward the result.
1048 * @param cls a `struct PrettyPrinterContext *`
1049 * @param hostname result from DNS resolver
1052 append_port (void *cls,
1053 const char *hostname)
1055 struct PrettyPrinterContext *ppc = cls;
1056 struct Plugin *plugin = ppc->plugin;
1059 if (NULL == hostname)
1061 /* Final call, done */
1062 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1063 plugin->ppc_dll_tail,
1065 ppc->resolver_handle = NULL;
1066 ppc->asc (ppc->asc_cls,
1072 if (GNUNET_YES == ppc->ipv6)
1073 GNUNET_asprintf (&ret,
1080 GNUNET_asprintf (&ret,
1086 ppc->asc (ppc->asc_cls,
1094 * Convert the transports address to a nice, human-readable format.
1096 * @param cls closure with the `struct Plugin *`
1097 * @param type name of the transport that generated the address
1098 * @param addr one of the addresses of the host, NULL for the last address
1099 * the specific address format depends on the transport;
1100 * a `union UdpAddress`
1101 * @param addrlen length of the address
1102 * @param numeric should (IP) addresses be displayed in numeric form?
1103 * @param timeout after how long should we give up?
1104 * @param asc function to call on each string
1105 * @param asc_cls closure for @a asc
1108 udp_plugin_address_pretty_printer (void *cls,
1113 struct GNUNET_TIME_Relative timeout,
1114 GNUNET_TRANSPORT_AddressStringCallback asc,
1117 struct Plugin *plugin = cls;
1118 struct PrettyPrinterContext *ppc;
1119 const struct sockaddr *sb;
1121 struct sockaddr_in a4;
1122 struct sockaddr_in6 a6;
1123 const struct IPv4UdpAddress *u4;
1124 const struct IPv6UdpAddress *u6;
1128 if (addrlen == sizeof(struct IPv6UdpAddress))
1134 a6.sin6_family = AF_INET6;
1135 #if HAVE_SOCKADDR_IN_SIN_LEN
1136 a6.sin6_len = sizeof (a6);
1138 a6.sin6_port = u6->u6_port;
1139 a6.sin6_addr = u6->ipv6_addr;
1140 port = ntohs (u6->u6_port);
1141 options = ntohl (u6->options);
1142 sb = (const struct sockaddr *) &a6;
1145 else if (addrlen == sizeof (struct IPv4UdpAddress))
1151 a4.sin_family = AF_INET;
1152 #if HAVE_SOCKADDR_IN_SIN_LEN
1153 a4.sin_len = sizeof (a4);
1155 a4.sin_port = u4->u4_port;
1156 a4.sin_addr.s_addr = u4->ipv4_addr;
1157 port = ntohs (u4->u4_port);
1158 options = ntohl (u4->options);
1159 sb = (const struct sockaddr *) &a4;
1164 /* invalid address */
1165 GNUNET_break_op (0);
1174 ppc = GNUNET_new (struct PrettyPrinterContext);
1175 ppc->plugin = plugin;
1177 ppc->asc_cls = asc_cls;
1179 ppc->options = options;
1180 if (addrlen == sizeof (struct IPv6UdpAddress))
1181 ppc->ipv6 = GNUNET_YES;
1183 ppc->ipv6 = GNUNET_NO;
1184 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1185 plugin->ppc_dll_tail,
1187 ppc->resolver_handle
1188 = GNUNET_RESOLVER_hostname_get (sb,
1198 * Check if the given port is plausible (must be either our listen
1199 * port or our advertised port). If it is neither, we return
1202 * @param plugin global variables
1203 * @param in_port port number to check
1204 * @return #GNUNET_OK if port is either our open or advertised port
1207 check_port (const struct Plugin *plugin,
1210 if ( (plugin->port == in_port) ||
1211 (plugin->aport == in_port) )
1213 return GNUNET_SYSERR;
1218 * Function that will be called to check if a binary address for this
1219 * plugin is well-formed and corresponds to an address for THIS peer
1220 * (as per our configuration). Naturally, if absolutely necessary,
1221 * plugins can be a bit conservative in their answer, but in general
1222 * plugins should make sure that the address does not redirect
1223 * traffic to a 3rd party that might try to man-in-the-middle our
1226 * @param cls closure, should be our handle to the Plugin
1227 * @param addr pointer to a `union UdpAddress`
1228 * @param addrlen length of @a addr
1229 * @return #GNUNET_OK if this is a plausible address for this peer
1230 * and transport, #GNUNET_SYSERR if not
1233 udp_plugin_check_address (void *cls,
1237 struct Plugin *plugin = cls;
1238 const struct IPv4UdpAddress *v4;
1239 const struct IPv6UdpAddress *v6;
1241 if (sizeof(struct IPv4UdpAddress) == addrlen)
1243 v4 = (const struct IPv4UdpAddress *) addr;
1244 if (GNUNET_OK != check_port (plugin,
1245 ntohs (v4->u4_port)))
1246 return GNUNET_SYSERR;
1248 GNUNET_NAT_test_address (plugin->nat,
1250 sizeof (struct in_addr)))
1251 return GNUNET_SYSERR;
1253 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1255 v6 = (const struct IPv6UdpAddress *) addr;
1256 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1258 GNUNET_break_op (0);
1259 return GNUNET_SYSERR;
1261 if (GNUNET_OK != check_port (plugin,
1262 ntohs (v6->u6_port)))
1263 return GNUNET_SYSERR;
1265 GNUNET_NAT_test_address (plugin->nat,
1267 sizeof (struct in6_addr)))
1268 return GNUNET_SYSERR;
1272 GNUNET_break_op (0);
1273 return GNUNET_SYSERR;
1280 * Our external IP address/port mapping has changed.
1282 * @param cls closure, the `struct Plugin`
1283 * @param add_remove #GNUNET_YES to mean the new public IP address,
1284 * #GNUNET_NO to mean the previous (now invalid) one
1285 * @param addr either the previous or the new public IP address
1286 * @param addrlen actual length of the @a addr
1289 udp_nat_port_map_callback (void *cls,
1291 const struct sockaddr *addr,
1294 struct Plugin *plugin = cls;
1295 struct GNUNET_HELLO_Address *address;
1296 struct IPv4UdpAddress u4;
1297 struct IPv6UdpAddress u6;
1301 LOG (GNUNET_ERROR_TYPE_DEBUG,
1302 (GNUNET_YES == add_remove)
1303 ? "NAT notification to add address `%s'\n"
1304 : "NAT notification to remove address `%s'\n",
1307 /* convert 'address' to our internal format */
1308 switch (addr->sa_family)
1312 const struct sockaddr_in *i4;
1314 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1315 i4 = (const struct sockaddr_in *) addr;
1316 if (0 == ntohs (i4->sin_port))
1324 u4.options = htonl (plugin->myoptions);
1325 u4.ipv4_addr = i4->sin_addr.s_addr;
1326 u4.u4_port = i4->sin_port;
1328 args = sizeof (struct IPv4UdpAddress);
1333 const struct sockaddr_in6 *i6;
1335 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1336 i6 = (const struct sockaddr_in6 *) addr;
1337 if (0 == ntohs (i6->sin6_port))
1345 u6.options = htonl (plugin->myoptions);
1346 u6.ipv6_addr = i6->sin6_addr;
1347 u6.u6_port = i6->sin6_port;
1349 args = sizeof (struct IPv6UdpAddress);
1356 /* modify our published address list */
1357 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1361 GNUNET_HELLO_ADDRESS_INFO_NONE);
1362 plugin->env->notify_address (plugin->env->cls,
1365 GNUNET_HELLO_address_free (address);
1369 /* ********************* Finding sessions ******************* */
1373 * Closure for #session_cmp_it().
1375 struct GNUNET_ATS_SessionCompareContext
1378 * Set to session matching the address.
1380 struct GNUNET_ATS_Session *res;
1383 * Address we are looking for.
1385 const struct GNUNET_HELLO_Address *address;
1390 * Find a session with a matching address.
1392 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1393 * @param key peer identity (unused)
1394 * @param value the `struct GNUNET_ATS_Session *`
1395 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1398 session_cmp_it (void *cls,
1399 const struct GNUNET_PeerIdentity *key,
1402 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1403 struct GNUNET_ATS_Session *s = value;
1405 if (0 == GNUNET_HELLO_address_cmp (s->address,
1408 GNUNET_assert (GNUNET_NO == s->in_destroy);
1417 * Locate an existing session the transport service is using to
1418 * send data to another peer. Performs some basic sanity checks
1419 * on the address and then tries to locate a matching session.
1421 * @param cls the plugin
1422 * @param address the address we should locate the session by
1423 * @return the session if it exists, or NULL if it is not found
1425 static struct GNUNET_ATS_Session *
1426 udp_plugin_lookup_session (void *cls,
1427 const struct GNUNET_HELLO_Address *address)
1429 struct Plugin *plugin = cls;
1430 const struct IPv6UdpAddress *udp_a6;
1431 const struct IPv4UdpAddress *udp_a4;
1432 struct GNUNET_ATS_SessionCompareContext cctx;
1434 if (NULL == address->address)
1439 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1441 if (NULL == plugin->sockv4)
1443 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1444 if (0 == udp_a4->u4_port)
1450 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1452 if (NULL == plugin->sockv6)
1454 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1455 if (0 == udp_a6->u6_port)
1467 /* check if session already exists */
1468 cctx.address = address;
1470 LOG (GNUNET_ERROR_TYPE_DEBUG,
1471 "Looking for existing session for peer `%s' with address `%s'\n",
1472 GNUNET_i2s (&address->peer),
1473 udp_address_to_string (plugin,
1475 address->address_length));
1476 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1480 if (NULL == cctx.res)
1482 LOG (GNUNET_ERROR_TYPE_DEBUG,
1483 "Found existing session %p\n",
1489 /* ********************** Timeout ****************** */
1493 * Increment session timeout due to activity.
1495 * @param s session to reschedule timeout activity for
1498 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1500 if (GNUNET_YES == s->in_destroy)
1502 GNUNET_assert (NULL != s->timeout_task);
1503 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1509 * Function that will be called whenever the transport service wants to
1510 * notify the plugin that a session is still active and in use and
1511 * therefore the session timeout for this session has to be updated
1513 * @param cls closure with the `struct Plugin`
1514 * @param peer which peer was the session for
1515 * @param session which session is being updated
1518 udp_plugin_update_session_timeout (void *cls,
1519 const struct GNUNET_PeerIdentity *peer,
1520 struct GNUNET_ATS_Session *session)
1522 struct Plugin *plugin = cls;
1525 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1532 /* Reschedule session timeout */
1533 reschedule_session_timeout (session);
1537 /* ************************* Sending ************************ */
1541 * Remove the given message from the transmission queue and
1542 * update all applicable statistics.
1544 * @param plugin the UDP plugin
1545 * @param udpw message wrapper to dequeue
1548 dequeue (struct Plugin *plugin,
1549 struct UDP_MessageWrapper *udpw)
1551 struct GNUNET_ATS_Session *session = udpw->session;
1553 if (plugin->bytes_in_buffer < udpw->msg_size)
1559 GNUNET_STATISTICS_update (plugin->env->stats,
1560 "# UDP, total bytes in send buffers",
1561 - (long long) udpw->msg_size,
1563 plugin->bytes_in_buffer -= udpw->msg_size;
1565 GNUNET_STATISTICS_update (plugin->env->stats,
1566 "# UDP, total messages in send buffers",
1569 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1571 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1572 plugin->ipv4_queue_tail,
1575 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1577 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1578 plugin->ipv6_queue_tail,
1586 GNUNET_assert (session->msgs_in_queue > 0);
1587 session->msgs_in_queue--;
1588 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1589 session->bytes_in_queue -= udpw->msg_size;
1594 * Enqueue a message for transmission and update statistics.
1596 * @param plugin the UDP plugin
1597 * @param udpw message wrapper to queue
1600 enqueue (struct Plugin *plugin,
1601 struct UDP_MessageWrapper *udpw)
1603 struct GNUNET_ATS_Session *session = udpw->session;
1605 if (GNUNET_YES == session->in_destroy)
1610 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1616 GNUNET_STATISTICS_update (plugin->env->stats,
1617 "# UDP, total bytes in send buffers",
1620 plugin->bytes_in_buffer += udpw->msg_size;
1622 GNUNET_STATISTICS_update (plugin->env->stats,
1623 "# UDP, total messages in send buffers",
1626 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1628 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1629 plugin->ipv4_queue_tail,
1632 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1634 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1635 plugin->ipv6_queue_tail,
1641 udpw->cont (udpw->cont_cls,
1649 session->msgs_in_queue++;
1650 session->bytes_in_queue += udpw->msg_size;
1655 * We have completed our (attempt) to transmit a message that had to
1656 * be fragmented -- either because we got an ACK saying that all
1657 * fragments were received, or because of timeout / disconnect. Clean
1660 * @param frag_ctx fragmentation context to clean up
1661 * @param result #GNUNET_OK if we succeeded (got ACK),
1662 * #GNUNET_SYSERR if the transmission failed
1665 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1668 struct Plugin *plugin = frag_ctx->plugin;
1669 struct GNUNET_ATS_Session *s = frag_ctx->session;
1670 struct UDP_MessageWrapper *udpw;
1671 struct UDP_MessageWrapper *tmp;
1673 struct GNUNET_TIME_Relative delay;
1675 LOG (GNUNET_ERROR_TYPE_DEBUG,
1676 "%p: Fragmented message removed with result %s\n",
1678 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1679 /* Call continuation for fragmented message */
1680 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1681 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1683 overhead = frag_ctx->on_wire_size;
1684 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1685 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1687 LOG (GNUNET_ERROR_TYPE_WARNING,
1688 "Fragmented message acknowledged after %s\n",
1689 GNUNET_STRINGS_relative_time_to_string (delay,
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "Fragmented message acknowledged after %s\n",
1696 GNUNET_STRINGS_relative_time_to_string (delay,
1700 if (NULL != frag_ctx->cont)
1701 frag_ctx->cont (frag_ctx->cont_cls,
1704 s->frag_ctx->payload_size,
1705 frag_ctx->on_wire_size);
1706 GNUNET_STATISTICS_update (plugin->env->stats,
1707 "# UDP, fragmented messages active",
1711 if (GNUNET_OK == result)
1713 GNUNET_STATISTICS_update (plugin->env->stats,
1714 "# UDP, fragmented msgs, messages, sent, success",
1717 GNUNET_STATISTICS_update (plugin->env->stats,
1718 "# UDP, fragmented msgs, bytes payload, sent, success",
1719 s->frag_ctx->payload_size,
1721 GNUNET_STATISTICS_update (plugin->env->stats,
1722 "# UDP, fragmented msgs, bytes overhead, sent, success",
1725 GNUNET_STATISTICS_update (plugin->env->stats,
1726 "# UDP, total, bytes overhead, sent",
1729 GNUNET_STATISTICS_update (plugin->env->stats,
1730 "# UDP, total, bytes payload, sent",
1731 s->frag_ctx->payload_size,
1736 GNUNET_STATISTICS_update (plugin->env->stats,
1737 "# UDP, fragmented msgs, messages, sent, failure",
1740 GNUNET_STATISTICS_update (plugin->env->stats,
1741 "# UDP, fragmented msgs, bytes payload, sent, failure",
1742 s->frag_ctx->payload_size,
1744 GNUNET_STATISTICS_update (plugin->env->stats,
1745 "# UDP, fragmented msgs, bytes payload, sent, failure",
1748 GNUNET_STATISTICS_update (plugin->env->stats,
1749 "# UDP, fragmented msgs, bytes payload, sent, failure",
1754 /* Remove remaining fragments from queue, no need to transmit those
1756 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1758 udpw = plugin->ipv6_queue_head;
1759 while (NULL != udpw)
1762 if ( (udpw->frag_ctx != NULL) &&
1763 (udpw->frag_ctx == frag_ctx) )
1772 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1774 udpw = plugin->ipv4_queue_head;
1775 while (NULL != udpw)
1778 if ( (NULL != udpw->frag_ctx) &&
1779 (udpw->frag_ctx == frag_ctx) )
1788 notify_session_monitor (s->plugin,
1790 GNUNET_TRANSPORT_SS_UPDATE);
1791 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1792 &s->last_expected_msg_delay,
1793 &s->last_expected_ack_delay);
1795 GNUNET_free (frag_ctx);
1800 * We are finished with a fragment in the message queue.
1801 * Notify the continuation and update statistics.
1803 * @param cls the `struct Plugin *`
1804 * @param udpw the queue entry
1805 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1808 qc_fragment_sent (void *cls,
1809 struct UDP_MessageWrapper *udpw,
1812 struct Plugin *plugin = cls;
1814 GNUNET_assert (NULL != udpw->frag_ctx);
1815 if (GNUNET_OK == result)
1817 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1818 GNUNET_STATISTICS_update (plugin->env->stats,
1819 "# UDP, fragmented msgs, fragments, sent, success",
1822 GNUNET_STATISTICS_update (plugin->env->stats,
1823 "# UDP, fragmented msgs, fragments bytes, sent, success",
1829 fragmented_message_done (udpw->frag_ctx,
1831 GNUNET_STATISTICS_update (plugin->env->stats,
1832 "# UDP, fragmented msgs, fragments, sent, failure",
1835 GNUNET_STATISTICS_update (plugin->env->stats,
1836 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1844 * Function that is called with messages created by the fragmentation
1845 * module. In the case of the `proc` callback of the
1846 * #GNUNET_FRAGMENT_context_create() function, this function must
1847 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1849 * @param cls closure, the `struct UDP_FragmentationContext`
1850 * @param msg the message that was created
1853 enqueue_fragment (void *cls,
1854 const struct GNUNET_MessageHeader *msg)
1856 struct UDP_FragmentationContext *frag_ctx = cls;
1857 struct Plugin *plugin = frag_ctx->plugin;
1858 struct UDP_MessageWrapper *udpw;
1859 struct GNUNET_ATS_Session *session = frag_ctx->session;
1860 size_t msg_len = ntohs (msg->size);
1862 LOG (GNUNET_ERROR_TYPE_DEBUG,
1863 "Enqueuing fragment with %u bytes\n",
1865 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1866 udpw->session = session;
1867 udpw->msg_buf = (char *) &udpw[1];
1868 udpw->msg_size = msg_len;
1869 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1870 udpw->timeout = frag_ctx->timeout;
1871 udpw->start_time = frag_ctx->start_time;
1872 udpw->transmission_time = frag_ctx->next_frag_time;
1873 frag_ctx->next_frag_time
1874 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1875 session->flow_delay_from_other_peer);
1876 udpw->frag_ctx = frag_ctx;
1877 udpw->qc = &qc_fragment_sent;
1878 udpw->qc_cls = plugin;
1879 memcpy (udpw->msg_buf,
1888 * We are finished with a message from the message queue.
1889 * Notify the continuation and update statistics.
1891 * @param cls the `struct Plugin *`
1892 * @param udpw the queue entry
1893 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1896 qc_message_sent (void *cls,
1897 struct UDP_MessageWrapper *udpw,
1900 struct Plugin *plugin = cls;
1902 struct GNUNET_TIME_Relative delay;
1904 if (udpw->msg_size >= udpw->payload_size)
1905 overhead = udpw->msg_size - udpw->payload_size;
1907 overhead = udpw->msg_size;
1909 if (NULL != udpw->cont)
1911 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1912 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1914 LOG (GNUNET_ERROR_TYPE_WARNING,
1915 "Message sent via UDP with delay of %s\n",
1916 GNUNET_STRINGS_relative_time_to_string (delay,
1921 LOG (GNUNET_ERROR_TYPE_DEBUG,
1922 "Message sent via UDP with delay of %s\n",
1923 GNUNET_STRINGS_relative_time_to_string (delay,
1926 udpw->cont (udpw->cont_cls,
1927 &udpw->session->target,
1932 if (GNUNET_OK == result)
1934 GNUNET_STATISTICS_update (plugin->env->stats,
1935 "# UDP, unfragmented msgs, messages, sent, success",
1938 GNUNET_STATISTICS_update (plugin->env->stats,
1939 "# UDP, unfragmented msgs, bytes payload, sent, success",
1942 GNUNET_STATISTICS_update (plugin->env->stats,
1943 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1946 GNUNET_STATISTICS_update (plugin->env->stats,
1947 "# UDP, total, bytes overhead, sent",
1950 GNUNET_STATISTICS_update (plugin->env->stats,
1951 "# UDP, total, bytes payload, sent",
1957 GNUNET_STATISTICS_update (plugin->env->stats,
1958 "# UDP, unfragmented msgs, messages, sent, failure",
1961 GNUNET_STATISTICS_update (plugin->env->stats,
1962 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1965 GNUNET_STATISTICS_update (plugin->env->stats,
1966 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1974 * Function that can be used by the transport service to transmit a
1975 * message using the plugin. Note that in the case of a peer
1976 * disconnecting, the continuation MUST be called prior to the
1977 * disconnect notification itself. This function will be called with
1978 * this peer's HELLO message to initiate a fresh connection to another
1981 * @param cls closure
1982 * @param s which session must be used
1983 * @param msgbuf the message to transmit
1984 * @param msgbuf_size number of bytes in @a msgbuf
1985 * @param priority how important is the message (most plugins will
1986 * ignore message priority and just FIFO)
1987 * @param to how long to wait at most for the transmission (does not
1988 * require plugins to discard the message after the timeout,
1989 * just advisory for the desired delay; most plugins will ignore
1991 * @param cont continuation to call once the message has
1992 * been transmitted (or if the transport is ready
1993 * for the next transmission call; or if the
1994 * peer disconnected...); can be NULL
1995 * @param cont_cls closure for @a cont
1996 * @return number of bytes used (on the physical network, with overheads);
1997 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1998 * and does NOT mean that the message was not transmitted (DV)
2001 udp_plugin_send (void *cls,
2002 struct GNUNET_ATS_Session *s,
2005 unsigned int priority,
2006 struct GNUNET_TIME_Relative to,
2007 GNUNET_TRANSPORT_TransmitContinuation cont,
2010 struct Plugin *plugin = cls;
2011 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2012 struct UDP_FragmentationContext *frag_ctx;
2013 struct UDP_MessageWrapper *udpw;
2014 struct UDPMessage *udp;
2015 char mbuf[udpmlen] GNUNET_ALIGN;
2017 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2018 (NULL == plugin->sockv6) )
2019 return GNUNET_SYSERR;
2020 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2021 (NULL == plugin->sockv4) )
2022 return GNUNET_SYSERR;
2023 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2026 return GNUNET_SYSERR;
2029 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2034 return GNUNET_SYSERR;
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2039 GNUNET_i2s (&s->target),
2040 udp_address_to_string (plugin,
2041 s->address->address,
2042 s->address->address_length));
2044 udp = (struct UDPMessage *) mbuf;
2045 udp->header.size = htons (udpmlen);
2046 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2047 udp->reserved = htonl (0);
2048 udp->sender = *plugin->env->my_identity;
2050 /* We do not update the session time out here! Otherwise this
2051 * session will not timeout since we send keep alive before session
2054 * For UDP we update session timeout only on receive, this will
2055 * cover keep alives, since remote peer will reply with keep alive
2058 if (udpmlen <= UDP_MTU)
2060 /* unfragmented message */
2061 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2063 udpw->msg_buf = (char *) &udpw[1];
2064 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2065 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2066 udpw->start_time = GNUNET_TIME_absolute_get ();
2067 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2068 udpw->transmission_time = s->last_transmit_time;
2069 s->last_transmit_time
2070 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2071 s->flow_delay_from_other_peer);
2073 udpw->cont_cls = cont_cls;
2074 udpw->frag_ctx = NULL;
2075 udpw->qc = &qc_message_sent;
2076 udpw->qc_cls = plugin;
2077 memcpy (udpw->msg_buf,
2079 sizeof (struct UDPMessage));
2080 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2085 GNUNET_STATISTICS_update (plugin->env->stats,
2086 "# UDP, unfragmented messages queued total",
2089 GNUNET_STATISTICS_update (plugin->env->stats,
2090 "# UDP, unfragmented bytes payload queued total",
2096 /* fragmented message */
2097 if (NULL != s->frag_ctx)
2098 return GNUNET_SYSERR;
2102 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2103 frag_ctx->plugin = plugin;
2104 frag_ctx->session = s;
2105 frag_ctx->cont = cont;
2106 frag_ctx->cont_cls = cont_cls;
2107 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2108 frag_ctx->next_frag_time = s->last_transmit_time;
2109 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2110 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2111 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2112 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2115 s->last_expected_msg_delay,
2116 s->last_expected_ack_delay,
2120 s->frag_ctx = frag_ctx;
2121 s->last_transmit_time = frag_ctx->next_frag_time;
2122 GNUNET_STATISTICS_update (plugin->env->stats,
2123 "# UDP, fragmented messages active",
2126 GNUNET_STATISTICS_update (plugin->env->stats,
2127 "# UDP, fragmented messages, total",
2130 GNUNET_STATISTICS_update (plugin->env->stats,
2131 "# UDP, fragmented bytes (payload)",
2132 frag_ctx->payload_size,
2135 notify_session_monitor (s->plugin,
2137 GNUNET_TRANSPORT_SS_UPDATE);
2138 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2139 schedule_select_v4 (plugin);
2141 schedule_select_v6 (plugin);
2147 * Handle an ACK message.
2149 * @param plugin the UDP plugin
2150 * @param msg the (presumed) UDP ACK message
2151 * @param udp_addr sender address
2152 * @param udp_addr_len number of bytes in @a udp_addr
2155 read_process_ack (struct Plugin *plugin,
2156 const struct GNUNET_MessageHeader *msg,
2157 const union UdpAddress *udp_addr,
2158 socklen_t udp_addr_len)
2160 const struct GNUNET_MessageHeader *ack;
2161 const struct UDP_ACK_Message *udp_ack;
2162 struct GNUNET_HELLO_Address *address;
2163 struct GNUNET_ATS_Session *s;
2164 struct GNUNET_TIME_Relative flow_delay;
2166 if (ntohs (msg->size)
2167 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2169 GNUNET_break_op (0);
2172 udp_ack = (const struct UDP_ACK_Message *) msg;
2173 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2174 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2179 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2183 GNUNET_HELLO_ADDRESS_INFO_NONE);
2184 s = udp_plugin_lookup_session (plugin,
2188 LOG (GNUNET_ERROR_TYPE_WARNING,
2189 "UDP session of address %s for ACK not found\n",
2190 udp_address_to_string (plugin,
2192 address->address_length));
2193 GNUNET_HELLO_address_free (address);
2196 if (NULL == s->frag_ctx)
2198 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2199 "Fragmentation context of address %s for ACK (%s) not found\n",
2200 udp_address_to_string (plugin,
2202 address->address_length),
2203 GNUNET_FRAGMENT_print_ack (ack));
2204 GNUNET_HELLO_address_free (address);
2207 GNUNET_HELLO_address_free (address);
2209 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2210 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2211 LOG (GNUNET_ERROR_TYPE_WARNING,
2212 "We received a sending delay of %s for %s\n",
2213 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2215 GNUNET_i2s (&udp_ack->sender));
2217 LOG (GNUNET_ERROR_TYPE_DEBUG,
2218 "We received a sending delay of %s for %s\n",
2219 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2221 GNUNET_i2s (&udp_ack->sender));
2222 /* Flow delay is for the reassembled packet, however, our delay
2223 is per packet, so we need to adjust: */
2224 flow_delay = GNUNET_TIME_relative_divide (flow_delay,
2225 1 + (s->frag_ctx->payload_size /
2227 s->flow_delay_from_other_peer = flow_delay;
2231 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2234 LOG (GNUNET_ERROR_TYPE_DEBUG,
2235 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2236 (unsigned int) ntohs (msg->size),
2237 GNUNET_i2s (&udp_ack->sender),
2238 udp_address_to_string (plugin,
2241 /* Expect more ACKs to arrive */
2245 LOG (GNUNET_ERROR_TYPE_DEBUG,
2246 "Message from %s at %s full ACK'ed\n",
2247 GNUNET_i2s (&udp_ack->sender),
2248 udp_address_to_string (plugin,
2252 /* Remove fragmented message after successful sending */
2253 fragmented_message_done (s->frag_ctx,
2258 /* ********************** Receiving ********************** */
2262 * Closure for #find_receive_context().
2264 struct FindReceiveContext
2267 * Where to store the result.
2269 struct DefragContext *rc;
2272 * Session associated with this context.
2274 struct GNUNET_ATS_Session *session;
2279 const union UdpAddress *udp_addr;
2282 * Number of bytes in @e udp_addr.
2284 size_t udp_addr_len;
2290 * Scan the heap for a receive context with the given address.
2292 * @param cls the `struct FindReceiveContext`
2293 * @param node internal node of the heap
2294 * @param element value stored at the node (a `struct ReceiveContext`)
2295 * @param cost cost associated with the node
2296 * @return #GNUNET_YES if we should continue to iterate,
2297 * #GNUNET_NO if not.
2300 find_receive_context (void *cls,
2301 struct GNUNET_CONTAINER_HeapNode *node,
2303 GNUNET_CONTAINER_HeapCostType cost)
2305 struct FindReceiveContext *frc = cls;
2306 struct DefragContext *e = element;
2308 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2309 (0 == memcmp (frc->udp_addr,
2311 frc->udp_addr_len)) )
2321 * Message tokenizer has broken up an incomming message. Pass it on
2324 * @param cls the `struct Plugin *`
2325 * @param client the `struct GNUNET_ATS_Session *`
2326 * @param hdr the actual message
2327 * @return #GNUNET_OK (always)
2330 process_inbound_tokenized_messages (void *cls,
2332 const struct GNUNET_MessageHeader *hdr)
2334 struct Plugin *plugin = cls;
2335 struct GNUNET_ATS_Session *session = client;
2337 if (GNUNET_YES == session->in_destroy)
2339 reschedule_session_timeout (session);
2340 session->flow_delay_for_other_peer
2341 = plugin->env->receive (plugin->env->cls,
2350 * Functions with this signature are called whenever we need to close
2351 * a session due to a disconnect or failure to establish a connection.
2353 * @param cls closure with the `struct Plugin`
2354 * @param s session to close down
2355 * @return #GNUNET_OK on success
2358 udp_disconnect_session (void *cls,
2359 struct GNUNET_ATS_Session *s)
2361 struct Plugin *plugin = cls;
2362 struct UDP_MessageWrapper *udpw;
2363 struct UDP_MessageWrapper *next;
2364 struct FindReceiveContext frc;
2366 GNUNET_assert (GNUNET_YES != s->in_destroy);
2367 LOG (GNUNET_ERROR_TYPE_DEBUG,
2368 "Session %p to peer `%s' at address %s ended\n",
2370 GNUNET_i2s (&s->target),
2371 udp_address_to_string (plugin,
2372 s->address->address,
2373 s->address->address_length));
2374 if (NULL != s->timeout_task)
2376 GNUNET_SCHEDULER_cancel (s->timeout_task);
2377 s->timeout_task = NULL;
2379 if (NULL != s->frag_ctx)
2381 /* Remove fragmented message due to disconnect */
2382 fragmented_message_done (s->frag_ctx,
2385 GNUNET_assert (GNUNET_YES ==
2386 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2390 frc.udp_addr = s->address->address;
2391 frc.udp_addr_len = s->address->address_length;
2392 /* Lookup existing receive context for this address */
2393 if (NULL != plugin->defrag_ctxs)
2395 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2396 &find_receive_context,
2400 struct DefragContext *d_ctx = frc.rc;
2402 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2403 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2404 GNUNET_free (d_ctx);
2407 s->in_destroy = GNUNET_YES;
2408 next = plugin->ipv4_queue_head;
2409 while (NULL != (udpw = next))
2412 if (udpw->session == s)
2416 udpw->qc (udpw->qc_cls,
2422 next = plugin->ipv6_queue_head;
2423 while (NULL != (udpw = next))
2426 if (udpw->session == s)
2430 udpw->qc (udpw->qc_cls,
2436 if ( (NULL != s->frag_ctx) &&
2437 (NULL != s->frag_ctx->cont) )
2439 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2440 later, as it might be in use right now */
2441 LOG (GNUNET_ERROR_TYPE_DEBUG,
2442 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2443 GNUNET_i2s (&s->target));
2444 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2447 s->frag_ctx->payload_size,
2448 s->frag_ctx->on_wire_size);
2450 notify_session_monitor (s->plugin,
2452 GNUNET_TRANSPORT_SS_DONE);
2453 plugin->env->session_end (plugin->env->cls,
2456 GNUNET_STATISTICS_set (plugin->env->stats,
2457 "# UDP sessions active",
2458 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2467 * Destroy a session, plugin is being unloaded.
2469 * @param cls the `struct Plugin`
2470 * @param key hash of public key of target peer
2471 * @param value a `struct PeerSession *` to clean up
2472 * @return #GNUNET_OK (continue to iterate)
2475 disconnect_and_free_it (void *cls,
2476 const struct GNUNET_PeerIdentity *key,
2479 struct Plugin *plugin = cls;
2481 udp_disconnect_session (plugin,
2488 * Disconnect from a remote node. Clean up session if we have one for
2491 * @param cls closure for this call (should be handle to Plugin)
2492 * @param target the peeridentity of the peer to disconnect
2493 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2496 udp_disconnect (void *cls,
2497 const struct GNUNET_PeerIdentity *target)
2499 struct Plugin *plugin = cls;
2501 LOG (GNUNET_ERROR_TYPE_DEBUG,
2502 "Disconnecting from peer `%s'\n",
2503 GNUNET_i2s (target));
2504 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2506 &disconnect_and_free_it,
2512 * Session was idle, so disconnect it.
2514 * @param cls the `struct GNUNET_ATS_Session` to time out
2515 * @param tc scheduler context
2518 session_timeout (void *cls,
2519 const struct GNUNET_SCHEDULER_TaskContext *tc)
2521 struct GNUNET_ATS_Session *s = cls;
2522 struct Plugin *plugin = s->plugin;
2523 struct GNUNET_TIME_Relative left;
2525 s->timeout_task = NULL;
2526 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2527 if (left.rel_value_us > 0)
2529 /* not actually our turn yet, but let's at least update
2530 the monitor, it may think we're about to die ... */
2531 notify_session_monitor (s->plugin,
2533 GNUNET_TRANSPORT_SS_UPDATE);
2534 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2539 LOG (GNUNET_ERROR_TYPE_DEBUG,
2540 "Session %p was idle for %s, disconnecting\n",
2542 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2544 /* call session destroy function */
2545 udp_disconnect_session (plugin,
2551 * Allocate a new session for the given endpoint address.
2552 * Note that this function does not inform the service
2553 * of the new session, this is the responsibility of the
2554 * caller (if needed).
2556 * @param cls the `struct Plugin`
2557 * @param address address of the other peer to use
2558 * @param network_type network type the address belongs to
2559 * @return NULL on error, otherwise session handle
2561 static struct GNUNET_ATS_Session *
2562 udp_plugin_create_session (void *cls,
2563 const struct GNUNET_HELLO_Address *address,
2564 enum GNUNET_ATS_Network_Type network_type)
2566 struct Plugin *plugin = cls;
2567 struct GNUNET_ATS_Session *s;
2569 s = GNUNET_new (struct GNUNET_ATS_Session);
2571 s->address = GNUNET_HELLO_address_copy (address);
2572 s->target = address->peer;
2573 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2574 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2576 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2577 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2578 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2579 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2580 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2583 s->scope = network_type;
2585 LOG (GNUNET_ERROR_TYPE_DEBUG,
2586 "Creating new session %p for peer `%s' address `%s'\n",
2588 GNUNET_i2s (&address->peer),
2589 udp_address_to_string (plugin,
2591 address->address_length));
2592 GNUNET_assert (GNUNET_OK ==
2593 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2597 GNUNET_STATISTICS_set (plugin->env->stats,
2598 "# UDP sessions active",
2599 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2601 notify_session_monitor (plugin,
2603 GNUNET_TRANSPORT_SS_INIT);
2609 * Creates a new outbound session the transport service will use to
2610 * send data to the peer.
2612 * @param cls the `struct Plugin *`
2613 * @param address the address
2614 * @return the session or NULL of max connections exceeded
2616 static struct GNUNET_ATS_Session *
2617 udp_plugin_get_session (void *cls,
2618 const struct GNUNET_HELLO_Address *address)
2620 struct Plugin *plugin = cls;
2621 struct GNUNET_ATS_Session *s;
2622 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2623 const struct IPv4UdpAddress *udp_v4;
2624 const struct IPv6UdpAddress *udp_v6;
2626 if (NULL == address)
2631 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2632 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2634 GNUNET_break_op (0);
2637 if (NULL != (s = udp_plugin_lookup_session (cls,
2641 /* need to create new session */
2642 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2644 struct sockaddr_in v4;
2646 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2647 memset (&v4, '\0', sizeof (v4));
2648 v4.sin_family = AF_INET;
2649 #if HAVE_SOCKADDR_IN_SIN_LEN
2650 v4.sin_len = sizeof (struct sockaddr_in);
2652 v4.sin_port = udp_v4->u4_port;
2653 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2654 network_type = plugin->env->get_address_type (plugin->env->cls,
2655 (const struct sockaddr *) &v4,
2658 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2660 struct sockaddr_in6 v6;
2662 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2663 memset (&v6, '\0', sizeof (v6));
2664 v6.sin6_family = AF_INET6;
2665 #if HAVE_SOCKADDR_IN_SIN_LEN
2666 v6.sin6_len = sizeof (struct sockaddr_in6);
2668 v6.sin6_port = udp_v6->u6_port;
2669 v6.sin6_addr = udp_v6->ipv6_addr;
2670 network_type = plugin->env->get_address_type (plugin->env->cls,
2671 (const struct sockaddr *) &v6,
2674 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2675 return udp_plugin_create_session (cls,
2682 * We've received a UDP Message. Process it (pass contents to main service).
2684 * @param plugin plugin context
2685 * @param msg the message
2686 * @param udp_addr sender address
2687 * @param udp_addr_len number of bytes in @a udp_addr
2688 * @param network_type network type the address belongs to
2691 process_udp_message (struct Plugin *plugin,
2692 const struct UDPMessage *msg,
2693 const union UdpAddress *udp_addr,
2694 size_t udp_addr_len,
2695 enum GNUNET_ATS_Network_Type network_type)
2697 struct GNUNET_ATS_Session *s;
2698 struct GNUNET_HELLO_Address *address;
2700 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2701 if (0 != ntohl (msg->reserved))
2706 if (ntohs (msg->header.size)
2707 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2713 address = GNUNET_HELLO_address_allocate (&msg->sender,
2717 GNUNET_HELLO_ADDRESS_INFO_NONE);
2719 (s = udp_plugin_lookup_session (plugin,
2722 s = udp_plugin_create_session (plugin,
2725 plugin->env->session_start (plugin->env->cls,
2729 notify_session_monitor (plugin,
2731 GNUNET_TRANSPORT_SS_UP);
2733 GNUNET_free (address);
2736 GNUNET_SERVER_mst_receive (plugin->mst,
2738 (const char *) &msg[1],
2739 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2743 if ( (0 == s->rc) &&
2744 (GNUNET_YES == s->in_destroy) )
2750 * Process a defragmented message.
2752 * @param cls the `struct DefragContext *`
2753 * @param msg the message
2756 fragment_msg_proc (void *cls,
2757 const struct GNUNET_MessageHeader *msg)
2759 struct DefragContext *dc = cls;
2760 const struct UDPMessage *um;
2762 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2764 GNUNET_break_op (0);
2767 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2769 GNUNET_break_op (0);
2772 um = (const struct UDPMessage *) msg;
2773 dc->sender = um->sender;
2774 dc->have_sender = GNUNET_YES;
2775 process_udp_message (dc->plugin,
2784 * We finished sending an acknowledgement. Update
2787 * @param cls the `struct Plugin`
2788 * @param udpw message queue entry of the ACK
2789 * @param result #GNUNET_OK if the transmission worked,
2790 * #GNUNET_SYSERR if we failed to send the ACK
2793 ack_message_sent (void *cls,
2794 struct UDP_MessageWrapper *udpw,
2797 struct Plugin *plugin = cls;
2799 if (GNUNET_OK == result)
2801 GNUNET_STATISTICS_update (plugin->env->stats,
2802 "# UDP, ACK messages sent",
2808 GNUNET_STATISTICS_update (plugin->env->stats,
2809 "# UDP, ACK transmissions failed",
2817 * Transmit an acknowledgement.
2819 * @param cls the `struct DefragContext *`
2820 * @param id message ID (unused)
2821 * @param msg ack to transmit
2824 ack_proc (void *cls,
2826 const struct GNUNET_MessageHeader *msg)
2828 struct DefragContext *rc = cls;
2829 struct Plugin *plugin = rc->plugin;
2830 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2831 struct UDP_ACK_Message *udp_ack;
2833 struct UDP_MessageWrapper *udpw;
2834 struct GNUNET_ATS_Session *s;
2835 struct GNUNET_HELLO_Address *address;
2837 if (GNUNET_NO == rc->have_sender)
2839 /* tried to defragment but never succeeded, hence will not ACK */
2840 /* This can happen if we just lost msgs */
2841 GNUNET_STATISTICS_update (plugin->env->stats,
2842 "# UDP, fragments discarded without ACK",
2847 address = GNUNET_HELLO_address_allocate (&rc->sender,
2851 GNUNET_HELLO_ADDRESS_INFO_NONE);
2852 s = udp_plugin_lookup_session (plugin,
2854 GNUNET_HELLO_address_free (address);
2857 LOG (GNUNET_ERROR_TYPE_ERROR,
2858 "Trying to transmit ACK to peer `%s' but no session found!\n",
2859 udp_address_to_string (plugin,
2862 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2863 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2865 GNUNET_STATISTICS_update (plugin->env->stats,
2866 "# UDP, ACK transmissions failed",
2871 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2872 delay = s->flow_delay_for_other_peer.rel_value_us;
2875 LOG (GNUNET_ERROR_TYPE_DEBUG,
2876 "Sending ACK to `%s' including delay of %s\n",
2877 udp_address_to_string (plugin,
2880 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2882 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2883 udpw->msg_size = msize;
2884 udpw->payload_size = 0;
2886 udpw->start_time = GNUNET_TIME_absolute_get ();
2887 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2888 udpw->msg_buf = (char *) &udpw[1];
2889 udpw->qc = &ack_message_sent;
2890 udpw->qc_cls = plugin;
2891 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2892 udp_ack->header.size = htons ((uint16_t) msize);
2893 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2894 udp_ack->delay = htonl (delay);
2895 udp_ack->sender = *plugin->env->my_identity;
2896 memcpy (&udp_ack[1],
2901 notify_session_monitor (plugin,
2903 GNUNET_TRANSPORT_SS_UPDATE);
2904 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2905 schedule_select_v4 (plugin);
2907 schedule_select_v6 (plugin);
2912 * We received a fragment, process it.
2914 * @param plugin our plugin
2915 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2916 * @param udp_addr sender address
2917 * @param udp_addr_len number of bytes in @a udp_addr
2918 * @param network_type network type the address belongs to
2921 read_process_fragment (struct Plugin *plugin,
2922 const struct GNUNET_MessageHeader *msg,
2923 const union UdpAddress *udp_addr,
2924 size_t udp_addr_len,
2925 enum GNUNET_ATS_Network_Type network_type)
2927 struct DefragContext *d_ctx;
2928 struct GNUNET_TIME_Absolute now;
2929 struct FindReceiveContext frc;
2932 frc.udp_addr = udp_addr;
2933 frc.udp_addr_len = udp_addr_len;
2935 /* Lookup existing receive context for this address */
2936 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2937 &find_receive_context,
2939 now = GNUNET_TIME_absolute_get ();
2944 /* Create a new defragmentation context */
2945 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2949 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2950 d_ctx->udp_addr_len = udp_addr_len;
2951 d_ctx->network_type = network_type;
2952 d_ctx->plugin = plugin;
2953 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2955 UDP_MAX_MESSAGES_IN_DEFRAG,
2959 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2961 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2962 LOG (GNUNET_ERROR_TYPE_DEBUG,
2963 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2964 (unsigned int) ntohs (msg->size),
2965 udp_address_to_string (plugin,
2971 LOG (GNUNET_ERROR_TYPE_DEBUG,
2972 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2973 (unsigned int) ntohs (msg->size),
2974 udp_address_to_string (plugin,
2980 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
2983 /* keep this 'rc' from expiring */
2984 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2986 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2988 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2989 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2991 /* remove 'rc' that was inactive the longest */
2992 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2993 GNUNET_assert (NULL != d_ctx);
2994 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2995 GNUNET_free (d_ctx);
2996 GNUNET_STATISTICS_update (plugin->env->stats,
2997 "# UDP, Defragmentations aborted",
3005 * Read and process a message from the given socket.
3007 * @param plugin the overall plugin
3008 * @param rsock socket to read from
3011 udp_select_read (struct Plugin *plugin,
3012 struct GNUNET_NETWORK_Handle *rsock)
3015 struct sockaddr_storage addr;
3016 char buf[65536] GNUNET_ALIGN;
3018 const struct GNUNET_MessageHeader *msg;
3019 struct IPv4UdpAddress v4;
3020 struct IPv6UdpAddress v6;
3021 const struct sockaddr *sa;
3022 const struct sockaddr_in *sa4;
3023 const struct sockaddr_in6 *sa6;
3024 const union UdpAddress *int_addr;
3025 size_t int_addr_len;
3026 enum GNUNET_ATS_Network_Type network_type;
3028 fromlen = sizeof (addr);
3032 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3035 (struct sockaddr *) &addr,
3037 sa = (const struct sockaddr *) &addr;
3039 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3040 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3041 * on this socket has failed.
3043 * WSAECONNRESET - The virtual circuit was reset by the remote side
3044 * executing a hard or abortive close. The application should close
3045 * the socket; it is no longer usable. On a UDP-datagram socket this
3046 * error indicates a previous send operation resulted in an ICMP Port
3047 * Unreachable message.
3049 if ( (-1 == size) &&
3050 (ECONNRESET == errno) )
3055 LOG (GNUNET_ERROR_TYPE_DEBUG,
3056 "UDP failed to receive data: %s\n",
3058 /* Connection failure or something. Not a protocol violation. */
3062 /* Check if this is a STUN packet */
3063 if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
3066 return; /* was STUN, do not process further */
3068 if (size < sizeof(struct GNUNET_MessageHeader))
3070 LOG (GNUNET_ERROR_TYPE_WARNING,
3071 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3072 (unsigned int ) size,
3075 /* _MAY_ be a connection failure (got partial message) */
3076 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3077 GNUNET_break_op (0);
3084 msg = (const struct GNUNET_MessageHeader *) buf;
3085 LOG (GNUNET_ERROR_TYPE_DEBUG,
3086 "UDP received %u-byte message from `%s' type %u\n",
3087 (unsigned int) size,
3091 if (size != ntohs (msg->size))
3093 LOG (GNUNET_ERROR_TYPE_WARNING,
3094 "UDP malformed message header from %s\n",
3095 (unsigned int) size,
3098 GNUNET_break_op (0);
3101 GNUNET_STATISTICS_update (plugin->env->stats,
3102 "# UDP, total bytes received",
3105 network_type = plugin->env->get_address_type (plugin->env->cls,
3108 switch (sa->sa_family)
3111 sa4 = (const struct sockaddr_in *) &addr;
3113 v4.ipv4_addr = sa4->sin_addr.s_addr;
3114 v4.u4_port = sa4->sin_port;
3115 int_addr = (union UdpAddress *) &v4;
3116 int_addr_len = sizeof (v4);
3119 sa6 = (const struct sockaddr_in6 *) &addr;
3121 v6.ipv6_addr = sa6->sin6_addr;
3122 v6.u6_port = sa6->sin6_port;
3123 int_addr = (union UdpAddress *) &v6;
3124 int_addr_len = sizeof (v6);
3131 switch (ntohs (msg->type))
3133 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3134 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3135 udp_broadcast_receive (plugin,
3142 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3143 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3148 process_udp_message (plugin,
3149 (const struct UDPMessage *) msg,
3154 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3155 read_process_ack (plugin,
3160 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3161 read_process_fragment (plugin,
3175 * Removes messages from the transmission queue that have
3176 * timed out, and then selects a message that should be
3179 * @param plugin the UDP plugin
3180 * @param sock which socket should we process the queue for (v4 or v6)
3181 * @return message selected for transmission, or NULL for none
3183 static struct UDP_MessageWrapper *
3184 remove_timeout_messages_and_select (struct Plugin *plugin,
3185 struct GNUNET_NETWORK_Handle *sock)
3187 struct UDP_MessageWrapper *udpw;
3188 struct GNUNET_TIME_Relative remaining;
3189 struct GNUNET_ATS_Session *session;
3192 removed = GNUNET_NO;
3193 udpw = (sock == plugin->sockv4)
3194 ? plugin->ipv4_queue_head
3195 : plugin->ipv6_queue_head;
3196 while (NULL != udpw)
3198 session = udpw->session;
3199 /* Find messages with timeout */
3200 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3201 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3203 /* Message timed out */
3204 removed = GNUNET_YES;
3207 udpw->qc (udpw->qc_cls,
3212 if (sock == plugin->sockv4)
3214 udpw = plugin->ipv4_queue_head;
3216 else if (sock == plugin->sockv6)
3218 udpw = plugin->ipv6_queue_head;
3222 GNUNET_break (0); /* should never happen */
3225 GNUNET_STATISTICS_update (plugin->env->stats,
3226 "# messages discarded due to timeout",
3232 /* Message did not time out, check transmission time */
3233 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3234 if (0 == remaining.rel_value_us)
3236 /* this message is not delayed */
3237 LOG (GNUNET_ERROR_TYPE_DEBUG,
3238 "Message for peer `%s' (%u bytes) is not delayed \n",
3239 GNUNET_i2s (&udpw->session->target),
3240 udpw->payload_size);
3241 break; /* Found message to send, break */
3245 /* Message is delayed, try next */
3246 LOG (GNUNET_ERROR_TYPE_DEBUG,
3247 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3248 GNUNET_i2s (&udpw->session->target),
3250 GNUNET_STRINGS_relative_time_to_string (remaining,
3256 if (GNUNET_YES == removed)
3257 notify_session_monitor (session->plugin,
3259 GNUNET_TRANSPORT_SS_UPDATE);
3265 * We failed to transmit a message via UDP. Generate
3266 * a descriptive error message.
3268 * @param plugin our plugin
3269 * @param sa target address we were trying to reach
3270 * @param slen number of bytes in @a sa
3271 * @param error the errno value returned from the sendto() call
3274 analyze_send_error (struct Plugin *plugin,
3275 const struct sockaddr *sa,
3279 enum GNUNET_ATS_Network_Type type;
3281 type = plugin->env->get_address_type (plugin->env->cls,
3284 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3285 (GNUNET_ATS_NET_WAN == type) ) &&
3286 ( (ENETUNREACH == errno) ||
3287 (ENETDOWN == errno) ) )
3289 if (slen == sizeof (struct sockaddr_in))
3291 /* IPv4: "Network unreachable" or "Network down"
3293 * This indicates we do not have connectivity
3295 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3296 _("UDP could not transmit message to `%s': "
3297 "Network seems down, please check your network configuration\n"),
3301 if (slen == sizeof (struct sockaddr_in6))
3303 /* IPv6: "Network unreachable" or "Network down"
3305 * This indicates that this system is IPv6 enabled, but does not
3306 * have a valid global IPv6 address assigned or we do not have
3309 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3310 _("UDP could not transmit IPv6 message! "
3311 "Please check your network configuration and disable IPv6 if your "
3312 "connection does not have a global IPv6 address\n"));
3317 LOG (GNUNET_ERROR_TYPE_WARNING,
3318 "UDP could not transmit message to `%s': `%s'\n",
3327 * It is time to try to transmit a UDP message. Select one
3330 * @param plugin the plugin
3331 * @param sock which socket (v4/v6) to send on
3334 udp_select_send (struct Plugin *plugin,
3335 struct GNUNET_NETWORK_Handle *sock)
3339 const struct sockaddr *a;
3340 const struct IPv4UdpAddress *u4;
3341 struct sockaddr_in a4;
3342 const struct IPv6UdpAddress *u6;
3343 struct sockaddr_in6 a6;
3344 struct UDP_MessageWrapper *udpw;
3346 /* Find message(s) to send */
3347 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3350 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3352 u4 = udpw->session->address->address;
3356 a4.sin_family = AF_INET;
3357 #if HAVE_SOCKADDR_IN_SIN_LEN
3358 a4.sin_len = sizeof (a4);
3360 a4.sin_port = u4->u4_port;
3361 a4.sin_addr.s_addr = u4->ipv4_addr;
3362 a = (const struct sockaddr *) &a4;
3365 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3367 u6 = udpw->session->address->address;
3371 a6.sin6_family = AF_INET6;
3372 #if HAVE_SOCKADDR_IN_SIN_LEN
3373 a6.sin6_len = sizeof (a6);
3375 a6.sin6_port = u6->u6_port;
3376 a6.sin6_addr = u6->ipv6_addr;
3377 a = (const struct sockaddr *) &a6;
3385 udpw->qc (udpw->qc_cls,
3388 notify_session_monitor (plugin,
3390 GNUNET_TRANSPORT_SS_UPDATE);
3394 sent = GNUNET_NETWORK_socket_sendto (sock,
3399 udpw->session->last_transmit_time
3400 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3401 udpw->session->last_transmit_time);
3404 if (GNUNET_SYSERR == sent)
3407 analyze_send_error (plugin,
3411 udpw->qc (udpw->qc_cls,
3414 GNUNET_STATISTICS_update (plugin->env->stats,
3415 "# UDP, total, bytes, sent, failure",
3418 GNUNET_STATISTICS_update (plugin->env->stats,
3419 "# UDP, total, messages, sent, failure",
3426 LOG (GNUNET_ERROR_TYPE_DEBUG,
3427 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3428 (unsigned int) (udpw->msg_size),
3429 GNUNET_i2s (&udpw->session->target),
3433 (sent < 0) ? STRERROR (errno) : "ok");
3434 GNUNET_STATISTICS_update (plugin->env->stats,
3435 "# UDP, total, bytes, sent, success",
3438 GNUNET_STATISTICS_update (plugin->env->stats,
3439 "# UDP, total, messages, sent, success",
3442 if (NULL != udpw->frag_ctx)
3443 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3444 udpw->qc (udpw->qc_cls,
3448 notify_session_monitor (plugin,
3450 GNUNET_TRANSPORT_SS_UPDATE);
3456 /* ***************** Event loop (part 2) *************** */
3460 * We have been notified that our readset has something to read. We don't
3461 * know which socket needs to be read, so we have to check each one
3462 * Then reschedule this function to be called again once more is available.
3464 * @param cls the plugin handle
3465 * @param tc the scheduling context
3468 udp_plugin_select_v4 (void *cls,
3469 const struct GNUNET_SCHEDULER_TaskContext *tc)
3471 struct Plugin *plugin = cls;
3473 plugin->select_task_v4 = NULL;
3474 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3476 if (NULL == plugin->sockv4)
3478 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3479 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3481 udp_select_read (plugin,
3483 udp_select_send (plugin,
3485 schedule_select_v4 (plugin);
3490 * We have been notified that our readset has something to read. We don't
3491 * know which socket needs to be read, so we have to check each one
3492 * Then reschedule this function to be called again once more is available.
3494 * @param cls the plugin handle
3495 * @param tc the scheduling context
3498 udp_plugin_select_v6 (void *cls,
3499 const struct GNUNET_SCHEDULER_TaskContext *tc)
3501 struct Plugin *plugin = cls;
3503 plugin->select_task_v6 = NULL;
3504 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3506 if (NULL == plugin->sockv6)
3508 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3509 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3511 udp_select_read (plugin,
3514 udp_select_send (plugin,
3516 schedule_select_v6 (plugin);
3520 /* ******************* Initialization *************** */
3524 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3526 * @param plugin the plugin to initialize
3527 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3528 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3529 * @return number of sockets that were successfully bound
3532 setup_sockets (struct Plugin *plugin,
3533 const struct sockaddr_in6 *bind_v6,
3534 const struct sockaddr_in *bind_v4)
3537 int sockets_created = 0;
3538 struct sockaddr_in6 server_addrv6;
3539 struct sockaddr_in server_addrv4;
3540 const struct sockaddr *server_addr;
3541 const struct sockaddr *addrs[2];
3542 socklen_t addrlens[2];
3546 /* Create IPv6 socket */
3548 if (GNUNET_YES == plugin->enable_ipv6)
3550 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3553 if (NULL == plugin->sockv6)
3555 LOG (GNUNET_ERROR_TYPE_INFO,
3556 _("Disabling IPv6 since it is not supported on this system!\n"));
3557 plugin->enable_ipv6 = GNUNET_NO;
3561 memset (&server_addrv6,
3563 sizeof(struct sockaddr_in6));
3564 #if HAVE_SOCKADDR_IN_SIN_LEN
3565 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3567 server_addrv6.sin6_family = AF_INET6;
3568 if (NULL != bind_v6)
3569 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3571 server_addrv6.sin6_addr = in6addr_any;
3573 if (0 == plugin->port) /* autodetect */
3574 server_addrv6.sin6_port
3575 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3579 server_addrv6.sin6_port = htons (plugin->port);
3580 addrlen = sizeof (struct sockaddr_in6);
3581 server_addr = (const struct sockaddr *) &server_addrv6;
3586 LOG(GNUNET_ERROR_TYPE_DEBUG,
3587 "Binding to IPv6 `%s'\n",
3588 GNUNET_a2s (server_addr,
3592 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3597 if (0 != plugin->port)
3599 tries = 10; /* fail immediately */
3600 break; /* bind failed on specific port */
3603 server_addrv6.sin6_port
3604 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3611 GNUNET_NETWORK_socket_close (plugin->sockv6);
3612 plugin->enable_ipv6 = GNUNET_NO;
3613 plugin->sockv6 = NULL;
3617 plugin->port = ntohs (server_addrv6.sin6_port);
3619 if (NULL != plugin->sockv6)
3621 LOG (GNUNET_ERROR_TYPE_DEBUG,
3622 "IPv6 UDP socket created listinging at %s\n",
3623 GNUNET_a2s (server_addr,
3625 addrs[sockets_created] = server_addr;
3626 addrlens[sockets_created] = addrlen;
3631 LOG (GNUNET_ERROR_TYPE_WARNING,
3632 _("Failed to bind UDP socket to %s: %s\n"),
3633 GNUNET_a2s (server_addr,
3640 /* Create IPv4 socket */
3642 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3645 if (NULL == plugin->sockv4)
3647 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3649 LOG (GNUNET_ERROR_TYPE_INFO,
3650 _("Disabling IPv4 since it is not supported on this system!\n"));
3651 plugin->enable_ipv4 = GNUNET_NO;
3655 memset (&server_addrv4,
3657 sizeof(struct sockaddr_in));
3658 #if HAVE_SOCKADDR_IN_SIN_LEN
3659 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3661 server_addrv4.sin_family = AF_INET;
3662 if (NULL != bind_v4)
3663 server_addrv4.sin_addr = bind_v4->sin_addr;
3665 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3667 if (0 == plugin->port)
3669 server_addrv4.sin_port
3670 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3674 server_addrv4.sin_port = htons (plugin->port);
3676 addrlen = sizeof (struct sockaddr_in);
3677 server_addr = (const struct sockaddr *) &server_addrv4;
3682 LOG (GNUNET_ERROR_TYPE_DEBUG,
3683 "Binding to IPv4 `%s'\n",
3684 GNUNET_a2s (server_addr,
3689 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3694 if (0 != plugin->port)
3696 tries = 10; /* fail */
3697 break; /* bind failed on specific port */
3701 server_addrv4.sin_port
3702 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3709 GNUNET_NETWORK_socket_close (plugin->sockv4);
3710 plugin->enable_ipv4 = GNUNET_NO;
3711 plugin->sockv4 = NULL;
3715 plugin->port = ntohs (server_addrv4.sin_port);
3718 if (NULL != plugin->sockv4)
3720 LOG (GNUNET_ERROR_TYPE_DEBUG,
3721 "IPv4 socket created on port %s\n",
3722 GNUNET_a2s (server_addr,
3724 addrs[sockets_created] = server_addr;
3725 addrlens[sockets_created] = addrlen;
3730 LOG (GNUNET_ERROR_TYPE_ERROR,
3731 _("Failed to bind UDP socket to %s: %s\n"),
3732 GNUNET_a2s (server_addr,
3738 if (0 == sockets_created)
3740 LOG (GNUNET_ERROR_TYPE_WARNING,
3741 _("Failed to open UDP sockets\n"));
3742 return 0; /* No sockets created, return */
3744 schedule_select_v4 (plugin);
3745 schedule_select_v6 (plugin);
3746 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3752 &udp_nat_port_map_callback,
3756 return sockets_created;
3761 * The exported method. Makes the core api available via a global and
3762 * returns the udp transport API.
3764 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3765 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3768 libgnunet_plugin_transport_udp_init (void *cls)
3770 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3771 struct GNUNET_TRANSPORT_PluginFunctions *api;
3773 unsigned long long port;
3774 unsigned long long aport;
3775 unsigned long long udp_max_bps;
3776 unsigned long long enable_v6;
3777 unsigned long long enable_broadcasting;
3778 unsigned long long enable_broadcasting_recv;
3779 char *bind4_address;
3780 char *bind6_address;
3781 struct GNUNET_TIME_Relative interval;
3782 struct sockaddr_in server_addrv4;
3783 struct sockaddr_in6 server_addrv6;
3788 if (NULL == env->receive)
3790 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3791 initialze the plugin or the API */
3792 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3794 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3795 api->address_to_string = &udp_address_to_string;
3796 api->string_to_address = &udp_string_to_address;
3800 /* Get port number: port == 0 : autodetect a port,
3801 * > 0 : use this port, not given : 2086 default */
3803 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3810 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3813 _("must be in [0,65535]"));
3817 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3824 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3827 _("must be in [0,65535]"));
3832 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3835 enable_v6 = GNUNET_NO;
3837 enable_v6 = GNUNET_YES;
3839 have_bind4 = GNUNET_NO;
3840 memset (&server_addrv4,
3842 sizeof (server_addrv4));
3844 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3849 LOG (GNUNET_ERROR_TYPE_DEBUG,
3850 "Binding UDP plugin to specific address: `%s'\n",
3852 if (1 != inet_pton (AF_INET,
3854 &server_addrv4.sin_addr))
3856 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3859 _("must be valid IPv4 address"));
3860 GNUNET_free (bind4_address);
3863 have_bind4 = GNUNET_YES;
3865 GNUNET_free_non_null (bind4_address);
3866 have_bind6 = GNUNET_NO;
3867 memset (&server_addrv6,
3869 sizeof (server_addrv6));
3871 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3876 LOG (GNUNET_ERROR_TYPE_DEBUG,
3877 "Binding udp plugin to specific address: `%s'\n",
3879 if (1 != inet_pton (AF_INET6,
3881 &server_addrv6.sin6_addr))
3883 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3886 _("must be valid IPv6 address"));
3887 GNUNET_free (bind6_address);
3890 have_bind6 = GNUNET_YES;
3892 GNUNET_free_non_null (bind6_address);
3894 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3897 if (enable_broadcasting == GNUNET_SYSERR)
3898 enable_broadcasting = GNUNET_NO;
3900 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3902 "BROADCAST_RECEIVE");
3903 if (enable_broadcasting_recv == GNUNET_SYSERR)
3904 enable_broadcasting_recv = GNUNET_YES;
3906 if (GNUNET_SYSERR ==
3907 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3909 "BROADCAST_INTERVAL",
3912 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3916 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3921 /* 50 MB/s == infinity for practical purposes */
3922 udp_max_bps = 1024 * 1024 * 50;
3925 p = GNUNET_new (struct Plugin);
3928 p->broadcast_interval = interval;
3929 p->enable_ipv6 = enable_v6;
3930 p->enable_ipv4 = GNUNET_YES; /* default */
3931 p->enable_broadcasting = enable_broadcasting;
3932 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3934 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3936 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3937 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3939 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3942 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3944 res = setup_sockets (p,
3945 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3946 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3948 ( (NULL == p->sockv4) &&
3949 (NULL == p->sockv6) ) )
3951 LOG (GNUNET_ERROR_TYPE_ERROR,
3952 _("Failed to create UDP network sockets\n"));
3953 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3954 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3955 GNUNET_SERVER_mst_destroy (p->mst);
3960 /* Setup broadcasting and receiving beacons */
3965 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3967 api->disconnect_session = &udp_disconnect_session;
3968 api->query_keepalive_factor = &udp_query_keepalive_factor;
3969 api->disconnect_peer = &udp_disconnect;
3970 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3971 api->address_to_string = &udp_address_to_string;
3972 api->string_to_address = &udp_string_to_address;
3973 api->check_address = &udp_plugin_check_address;
3974 api->get_session = &udp_plugin_get_session;
3975 api->send = &udp_plugin_send;
3976 api->get_network = &udp_plugin_get_network;
3977 api->get_network_for_address = &udp_plugin_get_network_for_address;
3978 api->update_session_timeout = &udp_plugin_update_session_timeout;
3979 api->setup_monitor = &udp_plugin_setup_monitor;
3985 * Function called on each entry in the defragmentation heap to
3989 * @param node node in the heap (to be removed)
3990 * @param element a `struct DefragContext` to be cleaned up
3991 * @param cost unused
3992 * @return #GNUNET_YES
3995 heap_cleanup_iterator (void *cls,
3996 struct GNUNET_CONTAINER_HeapNode *node,
3998 GNUNET_CONTAINER_HeapCostType cost)
4000 struct DefragContext *d_ctx = element;
4002 GNUNET_CONTAINER_heap_remove_node (node);
4003 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4004 GNUNET_free (d_ctx);
4010 * The exported method. Makes the core api available via a global and
4011 * returns the udp transport API.
4013 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4017 libgnunet_plugin_transport_udp_done (void *cls)
4019 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4020 struct Plugin *plugin = api->cls;
4021 struct PrettyPrinterContext *cur;
4022 struct UDP_MessageWrapper *udpw;
4029 stop_broadcast (plugin);
4030 if (NULL != plugin->select_task_v4)
4032 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4033 plugin->select_task_v4 = NULL;
4035 if (NULL != plugin->select_task_v6)
4037 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4038 plugin->select_task_v6 = NULL;
4040 if (NULL != plugin->sockv4)
4042 GNUNET_break (GNUNET_OK ==
4043 GNUNET_NETWORK_socket_close (plugin->sockv4));
4044 plugin->sockv4 = NULL;
4046 if (NULL != plugin->sockv6)
4048 GNUNET_break (GNUNET_OK ==
4049 GNUNET_NETWORK_socket_close (plugin->sockv6));
4050 plugin->sockv6 = NULL;
4052 if (NULL != plugin->nat)
4054 GNUNET_NAT_unregister (plugin->nat);
4057 if (NULL != plugin->defrag_ctxs)
4059 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4060 &heap_cleanup_iterator,
4062 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4063 plugin->defrag_ctxs = NULL;
4065 if (NULL != plugin->mst)
4067 GNUNET_SERVER_mst_destroy (plugin->mst);
4070 while (NULL != (udpw = plugin->ipv4_queue_head))
4074 udpw->qc (udpw->qc_cls,
4079 while (NULL != (udpw = plugin->ipv6_queue_head))
4083 udpw->qc (udpw->qc_cls,
4088 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4089 &disconnect_and_free_it,
4091 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4093 while (NULL != (cur = plugin->ppc_dll_head))
4096 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4097 plugin->ppc_dll_tail,
4099 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4100 if (NULL != cur->timeout_task)
4102 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4103 cur->timeout_task = NULL;
4107 GNUNET_free (plugin);
4112 /* end of plugin_transport_udp.c */