2 This file is part of GNUnet
3 Copyright (C) 2010-2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
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_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
46 * After how much inactivity should a UDP session time out?
48 #define UDP_SESSION_TIME_OUT 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 * Tokenizer for inbound messages.
164 struct GNUNET_MessageStreamTokenizer *mst;
167 * Plugin this session belongs to.
169 struct Plugin *plugin;
172 * Context for dealing with fragments.
174 struct UDP_FragmentationContext *frag_ctx;
177 * Desired delay for next sending we send to other peer
179 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
182 * Desired delay for transmissions we received from other peer.
183 * This is for full messages, the value needs to be adjusted for
184 * fragmented messages.
186 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
189 * Session timeout task
191 struct GNUNET_SCHEDULER_Task *timeout_task;
194 * When does this session time out?
196 struct GNUNET_TIME_Absolute timeout;
199 * What time did we last transmit?
201 struct GNUNET_TIME_Absolute last_transmit_time;
204 * expected delay for ACKs
206 struct GNUNET_TIME_Relative last_expected_ack_delay;
209 * desired delay between UDP messages
211 struct GNUNET_TIME_Relative last_expected_msg_delay;
216 struct GNUNET_HELLO_Address *address;
219 * Number of bytes waiting for transmission to this peer.
221 unsigned long long bytes_in_queue;
224 * Number of messages waiting for transmission to this peer.
226 unsigned int msgs_in_queue;
229 * Reference counter to indicate that this session is
230 * currently being used and must not be destroyed;
231 * setting @e in_destroy will destroy it as soon as
237 * Network type of the address.
239 enum GNUNET_ATS_Network_Type scope;
242 * Is this session about to be destroyed (sometimes we cannot
243 * destroy a session immediately as below us on the stack
244 * there might be code that still uses it; in this case,
245 * @e rc is non-zero).
253 * Data structure to track defragmentation contexts based
254 * on the source of the UDP traffic.
260 * Defragmentation context.
262 struct GNUNET_DEFRAGMENT_Context *defrag;
265 * Reference to master plugin struct.
267 struct Plugin *plugin;
270 * Node in the defrag heap.
272 struct GNUNET_CONTAINER_HeapNode *hnode;
275 * Source address this receive context is for (allocated at the
276 * end of the struct).
278 const union UdpAddress *udp_addr;
281 * Who's message(s) are we defragmenting here?
282 * Only initialized once we succeeded and
283 * @e have_sender is set.
285 struct GNUNET_PeerIdentity sender;
288 * Length of @e udp_addr.
293 * Network type the address belongs to.
295 enum GNUNET_ATS_Network_Type network_type;
298 * Has the @e sender field been initialized yet?
305 * Context to send fragmented messages
307 struct UDP_FragmentationContext
310 * Next in linked list
312 struct UDP_FragmentationContext *next;
315 * Previous in linked list
317 struct UDP_FragmentationContext *prev;
322 struct Plugin *plugin;
325 * Handle for fragmentation.
327 struct GNUNET_FRAGMENT_Context *frag;
330 * The session this fragmentation context belongs to
332 struct GNUNET_ATS_Session *session;
335 * Function to call upon completion of the transmission.
337 GNUNET_TRANSPORT_TransmitContinuation cont;
340 * Closure for @e cont.
347 struct GNUNET_TIME_Absolute start_time;
350 * Transmission time for the next fragment. Incremented by
351 * the @e flow_delay_from_other_peer for each fragment when
352 * we setup the fragments.
354 struct GNUNET_TIME_Absolute next_frag_time;
357 * Desired delay for transmissions we received from other peer.
358 * Adjusted to be per fragment (UDP_MTU), even though on the
359 * wire it was for "full messages".
361 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
366 struct GNUNET_TIME_Absolute timeout;
369 * Payload size of original unfragmented message
374 * Bytes used to send all fragments on wire including UDP overhead
382 * Function called when a message is removed from the
383 * transmission queue.
386 * @param udpw message wrapper finished
387 * @param result #GNUNET_OK on success (message was sent)
388 * #GNUNET_SYSERR if the target disconnected
389 * or we had a timeout or other trouble sending
392 (*QueueContinuation) (void *cls,
393 struct UDP_MessageWrapper *udpw,
398 * Information we track for each message in the queue.
400 struct UDP_MessageWrapper
403 * Session this message belongs to
405 struct GNUNET_ATS_Session *session;
408 * DLL of messages, previous element
410 struct UDP_MessageWrapper *prev;
413 * DLL of messages, next element
415 struct UDP_MessageWrapper *next;
418 * Message with @e msg_size bytes including UDP-specific overhead.
423 * Function to call once the message wrapper is being removed
424 * from the queue (with success or failure).
426 QueueContinuation qc;
434 * External continuation to call upon completion of the
435 * transmission, NULL if this queue entry is not for a
436 * message from the application.
438 GNUNET_TRANSPORT_TransmitContinuation cont;
441 * Closure for @e cont.
446 * Fragmentation context.
447 * frag_ctx == NULL if transport <= MTU
448 * frag_ctx != NULL if transport > MTU
450 struct UDP_FragmentationContext *frag_ctx;
453 * Message enqueue time.
455 struct GNUNET_TIME_Absolute start_time;
458 * Desired transmission time for this message, based on the
459 * flow limiting information we got from the other peer.
461 struct GNUNET_TIME_Absolute transmission_time;
466 struct GNUNET_TIME_Absolute timeout;
469 * Size of UDP message to send, including UDP-specific overhead.
474 * Payload size of original message.
481 GNUNET_NETWORK_STRUCT_BEGIN
484 * UDP ACK Message-Packet header.
486 struct UDP_ACK_Message
491 struct GNUNET_MessageHeader header;
494 * Desired delay for flow control, in us (in NBO).
495 * A value of UINT32_MAX indicates that the other
496 * peer wants us to disconnect.
498 uint32_t delay GNUNET_PACKED;
501 * What is the identity of the sender
503 struct GNUNET_PeerIdentity sender;
507 GNUNET_NETWORK_STRUCT_END
510 /* ************************* Monitoring *********** */
514 * If a session monitor is attached, notify it about the new
517 * @param plugin our plugin
518 * @param session session that changed state
519 * @param state new state of the session
522 notify_session_monitor (struct Plugin *plugin,
523 struct GNUNET_ATS_Session *session,
524 enum GNUNET_TRANSPORT_SessionState state)
526 struct GNUNET_TRANSPORT_SessionInfo info;
528 if (NULL == plugin->sic)
530 if (GNUNET_YES == session->in_destroy)
531 return; /* already destroyed, just RC>0 left-over actions */
536 info.is_inbound = GNUNET_SYSERR; /* hard to say */
537 info.num_msg_pending = session->msgs_in_queue;
538 info.num_bytes_pending = session->bytes_in_queue;
539 /* info.receive_delay remains zero as this is not supported by UDP
540 (cannot selectively not receive from 'some' peer while continuing
541 to receive from others) */
542 info.session_timeout = session->timeout;
543 info.address = session->address;
544 plugin->sic (plugin->sic_cls,
551 * Return information about the given session to the monitor callback.
553 * @param cls the `struct Plugin` with the monitor callback (`sic`)
554 * @param peer peer we send information about
555 * @param value our `struct GNUNET_ATS_Session` to send information about
556 * @return #GNUNET_OK (continue to iterate)
559 send_session_info_iter (void *cls,
560 const struct GNUNET_PeerIdentity *peer,
563 struct Plugin *plugin = cls;
564 struct GNUNET_ATS_Session *session = value;
566 notify_session_monitor (plugin,
568 GNUNET_TRANSPORT_SS_INIT);
569 notify_session_monitor (plugin,
571 GNUNET_TRANSPORT_SS_UP);
577 * Begin monitoring sessions of a plugin. There can only
578 * be one active monitor per plugin (i.e. if there are
579 * multiple monitors, the transport service needs to
580 * multiplex the generated events over all of them).
582 * @param cls closure of the plugin
583 * @param sic callback to invoke, NULL to disable monitor;
584 * plugin will being by iterating over all active
585 * sessions immediately and then enter monitor mode
586 * @param sic_cls closure for @a sic
589 udp_plugin_setup_monitor (void *cls,
590 GNUNET_TRANSPORT_SessionInfoCallback sic,
593 struct Plugin *plugin = cls;
596 plugin->sic_cls = sic_cls;
599 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
600 &send_session_info_iter,
602 /* signal end of first iteration */
610 /* ****************** Little Helpers ****************** */
614 * Function to free last resources associated with a session.
616 * @param s session to free
619 free_session (struct GNUNET_ATS_Session *s)
621 if (NULL != s->address)
623 GNUNET_HELLO_address_free (s->address);
626 if (NULL != s->frag_ctx)
628 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
631 GNUNET_free (s->frag_ctx);
636 GNUNET_MST_destroy (s->mst);
644 * Function that is called to get the keepalive factor.
645 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
646 * calculate the interval between keepalive packets.
648 * @param cls closure with the `struct Plugin`
649 * @return keepalive factor
652 udp_query_keepalive_factor (void *cls)
659 * Function obtain the network type for a session
661 * @param cls closure (`struct Plugin *`)
662 * @param session the session
663 * @return the network type
665 static enum GNUNET_ATS_Network_Type
666 udp_plugin_get_network (void *cls,
667 struct GNUNET_ATS_Session *session)
669 return session->scope;
674 * Function obtain the network type for an address.
676 * @param cls closure (`struct Plugin *`)
677 * @param address the address
678 * @return the network type
680 static enum GNUNET_ATS_Network_Type
681 udp_plugin_get_network_for_address (void *cls,
682 const struct GNUNET_HELLO_Address *address)
684 struct Plugin *plugin = cls;
686 struct sockaddr_in a4;
687 struct sockaddr_in6 a6;
688 const struct IPv4UdpAddress *u4;
689 const struct IPv6UdpAddress *u6;
693 addrlen = address->address_length;
694 if (addrlen == sizeof(struct IPv6UdpAddress))
696 GNUNET_assert (NULL != address->address); /* make static analysis happy */
697 u6 = address->address;
698 memset (&a6, 0, sizeof(a6));
699 #if HAVE_SOCKADDR_IN_SIN_LEN
700 a6.sin6_len = sizeof (a6);
702 a6.sin6_family = AF_INET6;
703 a6.sin6_port = u6->u6_port;
704 GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
708 else if (addrlen == sizeof(struct IPv4UdpAddress))
710 GNUNET_assert (NULL != address->address); /* make static analysis happy */
711 u4 = address->address;
712 memset (&a4, 0, sizeof(a4));
713 #if HAVE_SOCKADDR_IN_SIN_LEN
714 a4.sin_len = sizeof (a4);
716 a4.sin_family = AF_INET;
717 a4.sin_port = u4->u4_port;
718 a4.sin_addr.s_addr = u4->ipv4_addr;
725 return GNUNET_ATS_NET_UNSPECIFIED;
727 return plugin->env->get_address_type (plugin->env->cls,
733 /* ******************* Event loop ******************** */
736 * We have been notified that our readset has something to read. We don't
737 * know which socket needs to be read, so we have to check each one
738 * Then reschedule this function to be called again once more is available.
740 * @param cls the plugin handle
743 udp_plugin_select_v4 (void *cls);
747 * We have been notified that our readset has something to read. We don't
748 * know which socket needs to be read, so we have to check each one
749 * Then reschedule this function to be called again once more is available.
751 * @param cls the plugin handle
754 udp_plugin_select_v6 (void *cls);
758 * (re)schedule IPv4-select tasks for this plugin.
760 * @param plugin plugin to reschedule
763 schedule_select_v4 (struct Plugin *plugin)
765 struct GNUNET_TIME_Relative min_delay;
766 struct GNUNET_TIME_Relative delay;
767 struct UDP_MessageWrapper *udpw;
768 struct UDP_MessageWrapper *min_udpw;
770 if ( (GNUNET_YES == plugin->enable_ipv4) &&
771 (NULL != plugin->sockv4) )
773 /* Find a message ready to send:
774 * Flow delay from other peer is expired or not set (0) */
775 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
777 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
779 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
780 if (delay.rel_value_us < min_delay.rel_value_us)
786 if (NULL != plugin->select_task_v4)
787 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
788 if (NULL != min_udpw)
790 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
793 "Calculated flow delay for UDPv4 at %s for %s\n",
794 GNUNET_STRINGS_relative_time_to_string (min_delay,
796 GNUNET_i2s (&min_udpw->session->target));
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Calculated flow delay for UDPv4 at %s for %s\n",
802 GNUNET_STRINGS_relative_time_to_string (min_delay,
804 GNUNET_i2s (&min_udpw->session->target));
807 plugin->select_task_v4
808 = GNUNET_SCHEDULER_add_read_net (min_delay,
810 &udp_plugin_select_v4,
817 * (re)schedule IPv6-select tasks for this plugin.
819 * @param plugin plugin to reschedule
822 schedule_select_v6 (struct Plugin *plugin)
824 struct GNUNET_TIME_Relative min_delay;
825 struct GNUNET_TIME_Relative delay;
826 struct UDP_MessageWrapper *udpw;
827 struct UDP_MessageWrapper *min_udpw;
829 if ( (GNUNET_YES == plugin->enable_ipv6) &&
830 (NULL != plugin->sockv6) )
832 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
834 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
836 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
837 if (delay.rel_value_us < min_delay.rel_value_us)
843 if (NULL != plugin->select_task_v6)
844 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
845 if (NULL != min_udpw)
847 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
850 "Calculated flow delay for UDPv6 at %s for %s\n",
851 GNUNET_STRINGS_relative_time_to_string (min_delay,
853 GNUNET_i2s (&min_udpw->session->target));
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "Calculated flow delay for UDPv6 at %s for %s\n",
859 GNUNET_STRINGS_relative_time_to_string (min_delay,
861 GNUNET_i2s (&min_udpw->session->target));
864 plugin->select_task_v6
865 = GNUNET_SCHEDULER_add_read_net (min_delay,
867 &udp_plugin_select_v6,
873 /* ******************* Address to string and back ***************** */
877 * Function called for a quick conversion of the binary address to
878 * a numeric address. Note that the caller must not free the
879 * address and that the next call to this function is allowed
880 * to override the address again.
883 * @param addr binary address (a `union UdpAddress`)
884 * @param addrlen length of the @a addr
885 * @return string representing the same address
888 udp_address_to_string (void *cls,
892 static char rbuf[INET6_ADDRSTRLEN + 10];
893 char buf[INET6_ADDRSTRLEN];
897 const struct IPv4UdpAddress *t4;
898 const struct IPv6UdpAddress *t6;
909 if (addrlen == sizeof(struct IPv6UdpAddress))
913 options = ntohl (t6->options);
914 port = ntohs (t6->u6_port);
918 else if (addrlen == sizeof(struct IPv4UdpAddress))
922 options = ntohl (t4->options);
923 port = ntohs (t4->u4_port);
924 a4.s_addr = t4->ipv4_addr;
936 GNUNET_snprintf (rbuf,
950 * Function called to convert a string address to a binary address.
952 * @param cls closure (`struct Plugin *`)
953 * @param addr string address
954 * @param addrlen length of the address
955 * @param buf location to store the buffer
956 * @param added location to store the number of bytes in the buffer.
957 * If the function returns #GNUNET_SYSERR, its contents are undefined.
958 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
961 udp_string_to_address (void *cls,
967 struct sockaddr_storage socket_address;
973 /* Format tcp.options.address:port */
978 if ((NULL == addr) || (0 == addrlen))
981 return GNUNET_SYSERR;
983 if ('\0' != addr[addrlen - 1])
986 return GNUNET_SYSERR;
988 if (strlen (addr) != addrlen - 1)
991 return GNUNET_SYSERR;
993 plugin = GNUNET_strdup (addr);
994 optionstr = strchr (plugin, '.');
995 if (NULL == optionstr)
998 GNUNET_free (plugin);
999 return GNUNET_SYSERR;
1001 optionstr[0] = '\0';
1003 options = atol (optionstr);
1004 address = strchr (optionstr, '.');
1005 if (NULL == address)
1008 GNUNET_free (plugin);
1009 return GNUNET_SYSERR;
1015 GNUNET_STRINGS_to_address_ip (address,
1020 GNUNET_free (plugin);
1021 return GNUNET_SYSERR;
1023 GNUNET_free(plugin);
1025 switch (socket_address.ss_family)
1029 struct IPv4UdpAddress *u4;
1030 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1032 u4 = GNUNET_new (struct IPv4UdpAddress);
1033 u4->options = htonl (options);
1034 u4->ipv4_addr = in4->sin_addr.s_addr;
1035 u4->u4_port = in4->sin_port;
1037 *added = sizeof (struct IPv4UdpAddress);
1042 struct IPv6UdpAddress *u6;
1043 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1045 u6 = GNUNET_new (struct IPv6UdpAddress);
1046 u6->options = htonl (options);
1047 u6->ipv6_addr = in6->sin6_addr;
1048 u6->u6_port = in6->sin6_port;
1050 *added = sizeof (struct IPv6UdpAddress);
1055 return GNUNET_SYSERR;
1061 * Append our port and forward the result.
1063 * @param cls a `struct PrettyPrinterContext *`
1064 * @param hostname result from DNS resolver
1067 append_port (void *cls,
1068 const char *hostname)
1070 struct PrettyPrinterContext *ppc = cls;
1071 struct Plugin *plugin = ppc->plugin;
1074 if (NULL == hostname)
1076 /* Final call, done */
1077 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1078 plugin->ppc_dll_tail,
1080 ppc->resolver_handle = NULL;
1081 ppc->asc (ppc->asc_cls,
1087 if (GNUNET_YES == ppc->ipv6)
1088 GNUNET_asprintf (&ret,
1095 GNUNET_asprintf (&ret,
1101 ppc->asc (ppc->asc_cls,
1109 * Convert the transports address to a nice, human-readable format.
1111 * @param cls closure with the `struct Plugin *`
1112 * @param type name of the transport that generated the address
1113 * @param addr one of the addresses of the host, NULL for the last address
1114 * the specific address format depends on the transport;
1115 * a `union UdpAddress`
1116 * @param addrlen length of the address
1117 * @param numeric should (IP) addresses be displayed in numeric form?
1118 * @param timeout after how long should we give up?
1119 * @param asc function to call on each string
1120 * @param asc_cls closure for @a asc
1123 udp_plugin_address_pretty_printer (void *cls,
1128 struct GNUNET_TIME_Relative timeout,
1129 GNUNET_TRANSPORT_AddressStringCallback asc,
1132 struct Plugin *plugin = cls;
1133 struct PrettyPrinterContext *ppc;
1134 const struct sockaddr *sb;
1136 struct sockaddr_in a4;
1137 struct sockaddr_in6 a6;
1138 const struct IPv4UdpAddress *u4;
1139 const struct IPv6UdpAddress *u6;
1143 if (addrlen == sizeof(struct IPv6UdpAddress))
1149 a6.sin6_family = AF_INET6;
1150 #if HAVE_SOCKADDR_IN_SIN_LEN
1151 a6.sin6_len = sizeof (a6);
1153 a6.sin6_port = u6->u6_port;
1154 a6.sin6_addr = u6->ipv6_addr;
1155 port = ntohs (u6->u6_port);
1156 options = ntohl (u6->options);
1157 sb = (const struct sockaddr *) &a6;
1160 else if (addrlen == sizeof (struct IPv4UdpAddress))
1166 a4.sin_family = AF_INET;
1167 #if HAVE_SOCKADDR_IN_SIN_LEN
1168 a4.sin_len = sizeof (a4);
1170 a4.sin_port = u4->u4_port;
1171 a4.sin_addr.s_addr = u4->ipv4_addr;
1172 port = ntohs (u4->u4_port);
1173 options = ntohl (u4->options);
1174 sb = (const struct sockaddr *) &a4;
1179 /* invalid address */
1180 GNUNET_break_op (0);
1189 ppc = GNUNET_new (struct PrettyPrinterContext);
1190 ppc->plugin = plugin;
1192 ppc->asc_cls = asc_cls;
1194 ppc->options = options;
1195 if (addrlen == sizeof (struct IPv6UdpAddress))
1196 ppc->ipv6 = GNUNET_YES;
1198 ppc->ipv6 = GNUNET_NO;
1199 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1200 plugin->ppc_dll_tail,
1202 ppc->resolver_handle
1203 = GNUNET_RESOLVER_hostname_get (sb,
1213 * Check if the given port is plausible (must be either our listen
1214 * port or our advertised port). If it is neither, we return
1217 * @param plugin global variables
1218 * @param in_port port number to check
1219 * @return #GNUNET_OK if port is either our open or advertised port
1222 check_port (const struct Plugin *plugin,
1225 if ( (plugin->port == in_port) ||
1226 (plugin->aport == in_port) )
1228 return GNUNET_SYSERR;
1233 * Function that will be called to check if a binary address for this
1234 * plugin is well-formed and corresponds to an address for THIS peer
1235 * (as per our configuration). Naturally, if absolutely necessary,
1236 * plugins can be a bit conservative in their answer, but in general
1237 * plugins should make sure that the address does not redirect
1238 * traffic to a 3rd party that might try to man-in-the-middle our
1241 * @param cls closure, should be our handle to the Plugin
1242 * @param addr pointer to a `union UdpAddress`
1243 * @param addrlen length of @a addr
1244 * @return #GNUNET_OK if this is a plausible address for this peer
1245 * and transport, #GNUNET_SYSERR if not
1248 udp_plugin_check_address (void *cls,
1252 struct Plugin *plugin = cls;
1253 const struct IPv4UdpAddress *v4;
1254 const struct IPv6UdpAddress *v6;
1256 if (sizeof(struct IPv4UdpAddress) == addrlen)
1258 struct sockaddr_in s4;
1260 v4 = (const struct IPv4UdpAddress *) addr;
1261 if (GNUNET_OK != check_port (plugin,
1262 ntohs (v4->u4_port)))
1263 return GNUNET_SYSERR;
1264 memset (&s4, 0, sizeof (s4));
1265 s4.sin_family = AF_INET;
1266 #if HAVE_SOCKADDR_IN_SIN_LEN
1267 s4.sin_len = sizeof (s4);
1269 s4.sin_port = v4->u4_port;
1270 s4.sin_addr.s_addr = v4->ipv4_addr;
1273 GNUNET_NAT_test_address (plugin->nat,
1275 sizeof (struct sockaddr_in)))
1276 return GNUNET_SYSERR;
1278 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1280 struct sockaddr_in6 s6;
1282 v6 = (const struct IPv6UdpAddress *) addr;
1283 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1284 return GNUNET_OK; /* plausible, if unlikely... */
1285 memset (&s6, 0, sizeof (s6));
1286 s6.sin6_family = AF_INET6;
1287 #if HAVE_SOCKADDR_IN_SIN_LEN
1288 s6.sin6_len = sizeof (s6);
1290 s6.sin6_port = v6->u6_port;
1291 s6.sin6_addr = v6->ipv6_addr;
1294 GNUNET_NAT_test_address (plugin->nat,
1296 sizeof(struct sockaddr_in6)))
1297 return GNUNET_SYSERR;
1301 GNUNET_break_op (0);
1302 return GNUNET_SYSERR;
1309 * Our external IP address/port mapping has changed.
1311 * @param cls closure, the `struct Plugin`
1312 * @param add_remove #GNUNET_YES to mean the new public IP address,
1313 * #GNUNET_NO to mean the previous (now invalid) one
1314 * @param ac address class the address belongs to
1315 * @param addr either the previous or the new public IP address
1316 * @param addrlen actual length of the @a addr
1319 udp_nat_port_map_callback (void *cls,
1321 enum GNUNET_NAT_AddressClass ac,
1322 const struct sockaddr *addr,
1325 struct Plugin *plugin = cls;
1326 struct GNUNET_HELLO_Address *address;
1327 struct IPv4UdpAddress u4;
1328 struct IPv6UdpAddress u6;
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 (GNUNET_YES == add_remove)
1334 ? "NAT notification to add address `%s'\n"
1335 : "NAT notification to remove address `%s'\n",
1338 /* convert 'address' to our internal format */
1339 switch (addr->sa_family)
1343 const struct sockaddr_in *i4;
1345 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1346 i4 = (const struct sockaddr_in *) addr;
1347 if (0 == ntohs (i4->sin_port))
1348 return; /* Port = 0 means unmapped, ignore these for UDP. */
1352 u4.options = htonl (plugin->myoptions);
1353 u4.ipv4_addr = i4->sin_addr.s_addr;
1354 u4.u4_port = i4->sin_port;
1356 args = sizeof (struct IPv4UdpAddress);
1361 const struct sockaddr_in6 *i6;
1363 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1364 i6 = (const struct sockaddr_in6 *) addr;
1365 if (0 == ntohs (i6->sin6_port))
1366 return; /* Port = 0 means unmapped, ignore these for UDP. */
1370 u6.options = htonl (plugin->myoptions);
1371 u6.ipv6_addr = i6->sin6_addr;
1372 u6.u6_port = i6->sin6_port;
1374 args = sizeof (struct IPv6UdpAddress);
1381 /* modify our published address list */
1382 /* TODO: use 'ac' here in the future... */
1383 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1387 GNUNET_HELLO_ADDRESS_INFO_NONE);
1388 plugin->env->notify_address (plugin->env->cls,
1391 GNUNET_HELLO_address_free (address);
1395 /* ********************* Finding sessions ******************* */
1399 * Closure for #session_cmp_it().
1401 struct GNUNET_ATS_SessionCompareContext
1404 * Set to session matching the address.
1406 struct GNUNET_ATS_Session *res;
1409 * Address we are looking for.
1411 const struct GNUNET_HELLO_Address *address;
1416 * Find a session with a matching address.
1418 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1419 * @param key peer identity (unused)
1420 * @param value the `struct GNUNET_ATS_Session *`
1421 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1424 session_cmp_it (void *cls,
1425 const struct GNUNET_PeerIdentity *key,
1428 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1429 struct GNUNET_ATS_Session *s = value;
1431 if (0 == GNUNET_HELLO_address_cmp (s->address,
1434 GNUNET_assert (GNUNET_NO == s->in_destroy);
1443 * Locate an existing session the transport service is using to
1444 * send data to another peer. Performs some basic sanity checks
1445 * on the address and then tries to locate a matching session.
1447 * @param cls the plugin
1448 * @param address the address we should locate the session by
1449 * @return the session if it exists, or NULL if it is not found
1451 static struct GNUNET_ATS_Session *
1452 udp_plugin_lookup_session (void *cls,
1453 const struct GNUNET_HELLO_Address *address)
1455 struct Plugin *plugin = cls;
1456 const struct IPv6UdpAddress *udp_a6;
1457 const struct IPv4UdpAddress *udp_a4;
1458 struct GNUNET_ATS_SessionCompareContext cctx;
1460 if (NULL == address->address)
1465 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1467 if (NULL == plugin->sockv4)
1469 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1470 if (0 == udp_a4->u4_port)
1476 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1478 if (NULL == plugin->sockv6)
1480 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1481 if (0 == udp_a6->u6_port)
1493 /* check if session already exists */
1494 cctx.address = address;
1496 LOG (GNUNET_ERROR_TYPE_DEBUG,
1497 "Looking for existing session for peer `%s' with address `%s'\n",
1498 GNUNET_i2s (&address->peer),
1499 udp_address_to_string (plugin,
1501 address->address_length));
1502 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1506 if (NULL == cctx.res)
1508 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Found existing session %p\n",
1515 /* ********************** Timeout ****************** */
1519 * Increment session timeout due to activity.
1521 * @param s session to reschedule timeout activity for
1524 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1526 if (GNUNET_YES == s->in_destroy)
1528 GNUNET_assert (NULL != s->timeout_task);
1529 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1535 * Function that will be called whenever the transport service wants to
1536 * notify the plugin that a session is still active and in use and
1537 * therefore the session timeout for this session has to be updated
1539 * @param cls closure with the `struct Plugin`
1540 * @param peer which peer was the session for
1541 * @param session which session is being updated
1544 udp_plugin_update_session_timeout (void *cls,
1545 const struct GNUNET_PeerIdentity *peer,
1546 struct GNUNET_ATS_Session *session)
1548 struct Plugin *plugin = cls;
1551 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1558 /* Reschedule session timeout */
1559 reschedule_session_timeout (session);
1563 /* ************************* Sending ************************ */
1567 * Remove the given message from the transmission queue and
1568 * update all applicable statistics.
1570 * @param plugin the UDP plugin
1571 * @param udpw message wrapper to dequeue
1574 dequeue (struct Plugin *plugin,
1575 struct UDP_MessageWrapper *udpw)
1577 struct GNUNET_ATS_Session *session = udpw->session;
1579 if (plugin->bytes_in_buffer < udpw->msg_size)
1585 GNUNET_STATISTICS_update (plugin->env->stats,
1586 "# UDP, total bytes in send buffers",
1587 - (long long) udpw->msg_size,
1589 plugin->bytes_in_buffer -= udpw->msg_size;
1591 GNUNET_STATISTICS_update (plugin->env->stats,
1592 "# UDP, total messages in send buffers",
1595 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1597 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1598 plugin->ipv4_queue_tail,
1601 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1603 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1604 plugin->ipv6_queue_tail,
1612 GNUNET_assert (session->msgs_in_queue > 0);
1613 session->msgs_in_queue--;
1614 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1615 session->bytes_in_queue -= udpw->msg_size;
1620 * Enqueue a message for transmission and update statistics.
1622 * @param plugin the UDP plugin
1623 * @param udpw message wrapper to queue
1626 enqueue (struct Plugin *plugin,
1627 struct UDP_MessageWrapper *udpw)
1629 struct GNUNET_ATS_Session *session = udpw->session;
1631 if (GNUNET_YES == session->in_destroy)
1637 if (plugin->bytes_in_buffer > INT64_MAX - udpw->msg_size)
1643 GNUNET_STATISTICS_update (plugin->env->stats,
1644 "# UDP, total bytes in send buffers",
1647 plugin->bytes_in_buffer += udpw->msg_size;
1649 GNUNET_STATISTICS_update (plugin->env->stats,
1650 "# UDP, total messages in send buffers",
1653 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1655 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1656 plugin->ipv4_queue_tail,
1659 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1661 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1662 plugin->ipv6_queue_tail,
1668 udpw->cont (udpw->cont_cls,
1676 session->msgs_in_queue++;
1677 session->bytes_in_queue += udpw->msg_size;
1682 * We have completed our (attempt) to transmit a message that had to
1683 * be fragmented -- either because we got an ACK saying that all
1684 * fragments were received, or because of timeout / disconnect. Clean
1687 * @param frag_ctx fragmentation context to clean up
1688 * @param result #GNUNET_OK if we succeeded (got ACK),
1689 * #GNUNET_SYSERR if the transmission failed
1692 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1695 struct Plugin *plugin = frag_ctx->plugin;
1696 struct GNUNET_ATS_Session *s = frag_ctx->session;
1697 struct UDP_MessageWrapper *udpw;
1698 struct UDP_MessageWrapper *tmp;
1700 struct GNUNET_TIME_Relative delay;
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "%p: Fragmented message removed with result %s\n",
1705 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1706 /* Call continuation for fragmented message */
1707 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1708 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1710 overhead = frag_ctx->on_wire_size;
1711 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1712 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1714 LOG (GNUNET_ERROR_TYPE_WARNING,
1715 "Fragmented message acknowledged after %s (expected at %s)\n",
1716 GNUNET_STRINGS_relative_time_to_string (delay,
1718 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1722 LOG (GNUNET_ERROR_TYPE_DEBUG,
1723 "Fragmented message acknowledged after %s (expected at %s)\n",
1724 GNUNET_STRINGS_relative_time_to_string (delay,
1726 GNUNET_STRINGS_absolute_time_to_string (frag_ctx->next_frag_time));
1729 if (NULL != frag_ctx->cont)
1730 frag_ctx->cont (frag_ctx->cont_cls,
1733 s->frag_ctx->payload_size,
1734 frag_ctx->on_wire_size);
1735 GNUNET_STATISTICS_update (plugin->env->stats,
1736 "# UDP, fragmented messages active",
1740 if (GNUNET_OK == result)
1742 GNUNET_STATISTICS_update (plugin->env->stats,
1743 "# UDP, fragmented msgs, messages, sent, success",
1746 GNUNET_STATISTICS_update (plugin->env->stats,
1747 "# UDP, fragmented msgs, bytes payload, sent, success",
1748 s->frag_ctx->payload_size,
1750 GNUNET_STATISTICS_update (plugin->env->stats,
1751 "# UDP, fragmented msgs, bytes overhead, sent, success",
1754 GNUNET_STATISTICS_update (plugin->env->stats,
1755 "# UDP, total, bytes overhead, sent",
1758 GNUNET_STATISTICS_update (plugin->env->stats,
1759 "# UDP, total, bytes payload, sent",
1760 s->frag_ctx->payload_size,
1765 GNUNET_STATISTICS_update (plugin->env->stats,
1766 "# UDP, fragmented msgs, messages, sent, failure",
1769 GNUNET_STATISTICS_update (plugin->env->stats,
1770 "# UDP, fragmented msgs, bytes payload, sent, failure",
1771 s->frag_ctx->payload_size,
1773 GNUNET_STATISTICS_update (plugin->env->stats,
1774 "# UDP, fragmented msgs, bytes payload, sent, failure",
1777 GNUNET_STATISTICS_update (plugin->env->stats,
1778 "# UDP, fragmented msgs, bytes payload, sent, failure",
1783 /* Remove remaining fragments from queue, no need to transmit those
1785 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1787 udpw = plugin->ipv6_queue_head;
1788 while (NULL != udpw)
1791 if ( (udpw->frag_ctx != NULL) &&
1792 (udpw->frag_ctx == frag_ctx) )
1801 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1803 udpw = plugin->ipv4_queue_head;
1804 while (NULL != udpw)
1807 if ( (NULL != udpw->frag_ctx) &&
1808 (udpw->frag_ctx == frag_ctx) )
1817 notify_session_monitor (s->plugin,
1819 GNUNET_TRANSPORT_SS_UPDATE);
1820 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1821 &s->last_expected_msg_delay,
1822 &s->last_expected_ack_delay);
1824 GNUNET_free (frag_ctx);
1829 * We are finished with a fragment in the message queue.
1830 * Notify the continuation and update statistics.
1832 * @param cls the `struct Plugin *`
1833 * @param udpw the queue entry
1834 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1837 qc_fragment_sent (void *cls,
1838 struct UDP_MessageWrapper *udpw,
1841 struct Plugin *plugin = cls;
1843 GNUNET_assert (NULL != udpw->frag_ctx);
1844 if (GNUNET_OK == result)
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1847 "Fragment of message with %u bytes transmitted to %s\n",
1848 (unsigned int) udpw->payload_size,
1849 GNUNET_i2s (&udpw->session->target));
1850 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1851 GNUNET_STATISTICS_update (plugin->env->stats,
1852 "# UDP, fragmented msgs, fragments, sent, success",
1855 GNUNET_STATISTICS_update (plugin->env->stats,
1856 "# UDP, fragmented msgs, fragments bytes, sent, success",
1862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1863 "Failed to transmit fragment of message with %u bytes to %s\n",
1864 (unsigned int) udpw->payload_size,
1865 GNUNET_i2s (&udpw->session->target));
1866 fragmented_message_done (udpw->frag_ctx,
1868 GNUNET_STATISTICS_update (plugin->env->stats,
1869 "# UDP, fragmented msgs, fragments, sent, failure",
1872 GNUNET_STATISTICS_update (plugin->env->stats,
1873 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1881 * Function that is called with messages created by the fragmentation
1882 * module. In the case of the `proc` callback of the
1883 * #GNUNET_FRAGMENT_context_create() function, this function must
1884 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1886 * @param cls closure, the `struct UDP_FragmentationContext`
1887 * @param msg the message that was created
1890 enqueue_fragment (void *cls,
1891 const struct GNUNET_MessageHeader *msg)
1893 struct UDP_FragmentationContext *frag_ctx = cls;
1894 struct Plugin *plugin = frag_ctx->plugin;
1895 struct UDP_MessageWrapper *udpw;
1896 struct GNUNET_ATS_Session *session = frag_ctx->session;
1897 size_t msg_len = ntohs (msg->size);
1899 LOG (GNUNET_ERROR_TYPE_DEBUG,
1900 "Enqueuing fragment with %u bytes\n",
1902 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1903 udpw->session = session;
1904 udpw->msg_buf = (char *) &udpw[1];
1905 udpw->msg_size = msg_len;
1906 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1907 udpw->timeout = frag_ctx->timeout;
1908 udpw->start_time = frag_ctx->start_time;
1909 udpw->transmission_time = frag_ctx->next_frag_time;
1910 frag_ctx->next_frag_time
1911 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1912 frag_ctx->flow_delay_from_other_peer);
1913 udpw->frag_ctx = frag_ctx;
1914 udpw->qc = &qc_fragment_sent;
1915 udpw->qc_cls = plugin;
1916 GNUNET_memcpy (udpw->msg_buf,
1921 if (session->address->address_length == sizeof (struct IPv4UdpAddress))
1922 schedule_select_v4 (plugin);
1924 schedule_select_v6 (plugin);
1929 * We are finished with a message from the message queue.
1930 * Notify the continuation and update statistics.
1932 * @param cls the `struct Plugin *`
1933 * @param udpw the queue entry
1934 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1937 qc_message_sent (void *cls,
1938 struct UDP_MessageWrapper *udpw,
1941 struct Plugin *plugin = cls;
1943 struct GNUNET_TIME_Relative delay;
1945 if (udpw->msg_size >= udpw->payload_size)
1946 overhead = udpw->msg_size - udpw->payload_size;
1948 overhead = udpw->msg_size;
1950 if (NULL != udpw->cont)
1952 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1953 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1955 LOG (GNUNET_ERROR_TYPE_WARNING,
1956 "Message sent via UDP with delay of %s\n",
1957 GNUNET_STRINGS_relative_time_to_string (delay,
1962 LOG (GNUNET_ERROR_TYPE_DEBUG,
1963 "Message sent via UDP with delay of %s\n",
1964 GNUNET_STRINGS_relative_time_to_string (delay,
1967 udpw->cont (udpw->cont_cls,
1968 &udpw->session->target,
1973 if (GNUNET_OK == result)
1975 GNUNET_STATISTICS_update (plugin->env->stats,
1976 "# UDP, unfragmented msgs, messages, sent, success",
1979 GNUNET_STATISTICS_update (plugin->env->stats,
1980 "# UDP, unfragmented msgs, bytes payload, sent, success",
1983 GNUNET_STATISTICS_update (plugin->env->stats,
1984 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1987 GNUNET_STATISTICS_update (plugin->env->stats,
1988 "# UDP, total, bytes overhead, sent",
1991 GNUNET_STATISTICS_update (plugin->env->stats,
1992 "# UDP, total, bytes payload, sent",
1998 GNUNET_STATISTICS_update (plugin->env->stats,
1999 "# UDP, unfragmented msgs, messages, sent, failure",
2002 GNUNET_STATISTICS_update (plugin->env->stats,
2003 "# UDP, unfragmented msgs, bytes payload, sent, failure",
2006 GNUNET_STATISTICS_update (plugin->env->stats,
2007 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
2015 * Function that can be used by the transport service to transmit a
2016 * message using the plugin. Note that in the case of a peer
2017 * disconnecting, the continuation MUST be called prior to the
2018 * disconnect notification itself. This function will be called with
2019 * this peer's HELLO message to initiate a fresh connection to another
2022 * @param cls closure
2023 * @param s which session must be used
2024 * @param msgbuf the message to transmit
2025 * @param msgbuf_size number of bytes in @a msgbuf
2026 * @param priority how important is the message (most plugins will
2027 * ignore message priority and just FIFO)
2028 * @param to how long to wait at most for the transmission (does not
2029 * require plugins to discard the message after the timeout,
2030 * just advisory for the desired delay; most plugins will ignore
2032 * @param cont continuation to call once the message has
2033 * been transmitted (or if the transport is ready
2034 * for the next transmission call; or if the
2035 * peer disconnected...); can be NULL
2036 * @param cont_cls closure for @a cont
2037 * @return number of bytes used (on the physical network, with overheads);
2038 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2039 * and does NOT mean that the message was not transmitted (DV)
2042 udp_plugin_send (void *cls,
2043 struct GNUNET_ATS_Session *s,
2046 unsigned int priority,
2047 struct GNUNET_TIME_Relative to,
2048 GNUNET_TRANSPORT_TransmitContinuation cont,
2051 struct Plugin *plugin = cls;
2052 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2053 struct UDP_FragmentationContext *frag_ctx;
2054 struct UDP_MessageWrapper *udpw;
2055 struct UDPMessage *udp;
2056 char mbuf[udpmlen] GNUNET_ALIGN;
2057 struct GNUNET_TIME_Relative latency;
2059 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2060 (NULL == plugin->sockv6) )
2061 return GNUNET_SYSERR;
2062 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2063 (NULL == plugin->sockv4) )
2064 return GNUNET_SYSERR;
2065 if (udpmlen >= GNUNET_MAX_MESSAGE_SIZE)
2068 return GNUNET_SYSERR;
2071 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2076 return GNUNET_SYSERR;
2078 LOG (GNUNET_ERROR_TYPE_DEBUG,
2079 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2081 GNUNET_i2s (&s->target),
2082 udp_address_to_string (plugin,
2083 s->address->address,
2084 s->address->address_length));
2086 udp = (struct UDPMessage *) mbuf;
2087 udp->header.size = htons (udpmlen);
2088 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2089 udp->reserved = htonl (0);
2090 udp->sender = *plugin->env->my_identity;
2092 /* We do not update the session time out here! Otherwise this
2093 * session will not timeout since we send keep alive before session
2096 * For UDP we update session timeout only on receive, this will
2097 * cover keep alives, since remote peer will reply with keep alive
2100 if (udpmlen <= UDP_MTU)
2102 /* unfragmented message */
2103 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2105 udpw->msg_buf = (char *) &udpw[1];
2106 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2107 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2108 udpw->start_time = GNUNET_TIME_absolute_get ();
2109 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2110 udpw->transmission_time = s->last_transmit_time;
2111 s->last_transmit_time
2112 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2113 s->flow_delay_from_other_peer);
2115 udpw->cont_cls = cont_cls;
2116 udpw->frag_ctx = NULL;
2117 udpw->qc = &qc_message_sent;
2118 udpw->qc_cls = plugin;
2119 GNUNET_memcpy (udpw->msg_buf,
2121 sizeof (struct UDPMessage));
2122 GNUNET_memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2127 GNUNET_STATISTICS_update (plugin->env->stats,
2128 "# UDP, unfragmented messages queued total",
2131 GNUNET_STATISTICS_update (plugin->env->stats,
2132 "# UDP, unfragmented bytes payload queued total",
2135 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2136 schedule_select_v4 (plugin);
2138 schedule_select_v6 (plugin);
2142 /* fragmented message */
2143 if (NULL != s->frag_ctx)
2144 return GNUNET_SYSERR;
2145 GNUNET_memcpy (&udp[1],
2148 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2149 frag_ctx->plugin = plugin;
2150 frag_ctx->session = s;
2151 frag_ctx->cont = cont;
2152 frag_ctx->cont_cls = cont_cls;
2153 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2154 frag_ctx->next_frag_time = s->last_transmit_time;
2155 frag_ctx->flow_delay_from_other_peer
2156 = GNUNET_TIME_relative_divide (s->flow_delay_from_other_peer,
2159 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2160 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2161 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2162 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2165 s->last_expected_msg_delay,
2166 s->last_expected_ack_delay,
2170 s->frag_ctx = frag_ctx;
2171 s->last_transmit_time = frag_ctx->next_frag_time;
2172 latency = GNUNET_TIME_absolute_get_remaining (s->last_transmit_time);
2173 if (latency.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2174 LOG (GNUNET_ERROR_TYPE_WARNING,
2175 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2176 GNUNET_STRINGS_relative_time_to_string (latency,
2178 GNUNET_i2s (&s->target),
2179 (unsigned int) s->msgs_in_queue);
2181 LOG (GNUNET_ERROR_TYPE_DEBUG,
2182 "Enqueued fragments will take %s for transmission to %s (queue size: %u)\n",
2183 GNUNET_STRINGS_relative_time_to_string (latency,
2185 GNUNET_i2s (&s->target),
2186 (unsigned int) s->msgs_in_queue);
2188 GNUNET_STATISTICS_update (plugin->env->stats,
2189 "# UDP, fragmented messages active",
2192 GNUNET_STATISTICS_update (plugin->env->stats,
2193 "# UDP, fragmented messages, total",
2196 GNUNET_STATISTICS_update (plugin->env->stats,
2197 "# UDP, fragmented bytes (payload)",
2198 frag_ctx->payload_size,
2201 notify_session_monitor (s->plugin,
2203 GNUNET_TRANSPORT_SS_UPDATE);
2208 /* ********************** Receiving ********************** */
2212 * Closure for #find_receive_context().
2214 struct FindReceiveContext
2217 * Where to store the result.
2219 struct DefragContext *rc;
2222 * Session associated with this context.
2224 struct GNUNET_ATS_Session *session;
2229 const union UdpAddress *udp_addr;
2232 * Number of bytes in @e udp_addr.
2234 size_t udp_addr_len;
2240 * Scan the heap for a receive context with the given address.
2242 * @param cls the `struct FindReceiveContext`
2243 * @param node internal node of the heap
2244 * @param element value stored at the node (a `struct ReceiveContext`)
2245 * @param cost cost associated with the node
2246 * @return #GNUNET_YES if we should continue to iterate,
2247 * #GNUNET_NO if not.
2250 find_receive_context (void *cls,
2251 struct GNUNET_CONTAINER_HeapNode *node,
2253 GNUNET_CONTAINER_HeapCostType cost)
2255 struct FindReceiveContext *frc = cls;
2256 struct DefragContext *e = element;
2258 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2259 (0 == memcmp (frc->udp_addr,
2261 frc->udp_addr_len)) )
2271 * Functions with this signature are called whenever we need to close
2272 * a session due to a disconnect or failure to establish a connection.
2274 * @param cls closure with the `struct Plugin`
2275 * @param s session to close down
2276 * @return #GNUNET_OK on success
2279 udp_disconnect_session (void *cls,
2280 struct GNUNET_ATS_Session *s)
2282 struct Plugin *plugin = cls;
2283 struct UDP_MessageWrapper *udpw;
2284 struct UDP_MessageWrapper *next;
2285 struct FindReceiveContext frc;
2287 GNUNET_assert (GNUNET_YES != s->in_destroy);
2288 LOG (GNUNET_ERROR_TYPE_DEBUG,
2289 "Session %p to peer `%s' at address %s ended\n",
2291 GNUNET_i2s (&s->target),
2292 udp_address_to_string (plugin,
2293 s->address->address,
2294 s->address->address_length));
2295 if (NULL != s->timeout_task)
2297 GNUNET_SCHEDULER_cancel (s->timeout_task);
2298 s->timeout_task = NULL;
2300 if (NULL != s->frag_ctx)
2302 /* Remove fragmented message due to disconnect */
2303 fragmented_message_done (s->frag_ctx,
2306 GNUNET_assert (GNUNET_YES ==
2307 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2311 frc.udp_addr = s->address->address;
2312 frc.udp_addr_len = s->address->address_length;
2313 /* Lookup existing receive context for this address */
2314 if (NULL != plugin->defrag_ctxs)
2316 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2317 &find_receive_context,
2321 struct DefragContext *d_ctx = frc.rc;
2323 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2324 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2325 GNUNET_free (d_ctx);
2328 s->in_destroy = GNUNET_YES;
2329 next = plugin->ipv4_queue_head;
2330 while (NULL != (udpw = next))
2333 if (udpw->session == s)
2337 udpw->qc (udpw->qc_cls,
2343 next = plugin->ipv6_queue_head;
2344 while (NULL != (udpw = next))
2347 if (udpw->session == s)
2351 udpw->qc (udpw->qc_cls,
2357 if ( (NULL != s->frag_ctx) &&
2358 (NULL != s->frag_ctx->cont) )
2360 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2361 later, as it might be in use right now */
2362 LOG (GNUNET_ERROR_TYPE_DEBUG,
2363 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2364 GNUNET_i2s (&s->target));
2365 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2368 s->frag_ctx->payload_size,
2369 s->frag_ctx->on_wire_size);
2371 notify_session_monitor (s->plugin,
2373 GNUNET_TRANSPORT_SS_DONE);
2374 plugin->env->session_end (plugin->env->cls,
2377 GNUNET_STATISTICS_set (plugin->env->stats,
2378 "# UDP sessions active",
2379 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2388 * Handle a #GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK message.
2390 * @param plugin the UDP plugin
2391 * @param msg the (presumed) UDP ACK message
2392 * @param udp_addr sender address
2393 * @param udp_addr_len number of bytes in @a udp_addr
2396 read_process_ack (struct Plugin *plugin,
2397 const struct GNUNET_MessageHeader *msg,
2398 const union UdpAddress *udp_addr,
2399 socklen_t udp_addr_len)
2401 const struct GNUNET_MessageHeader *ack;
2402 const struct UDP_ACK_Message *udp_ack;
2403 struct GNUNET_HELLO_Address *address;
2404 struct GNUNET_ATS_Session *s;
2405 struct GNUNET_TIME_Relative flow_delay;
2407 /* check message format */
2408 if (ntohs (msg->size)
2409 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2411 GNUNET_break_op (0);
2414 udp_ack = (const struct UDP_ACK_Message *) msg;
2415 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2416 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2422 /* Locate session */
2423 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2427 GNUNET_HELLO_ADDRESS_INFO_NONE);
2428 s = udp_plugin_lookup_session (plugin,
2432 LOG (GNUNET_ERROR_TYPE_WARNING,
2433 "UDP session of address %s for ACK not found\n",
2434 udp_address_to_string (plugin,
2436 address->address_length));
2437 GNUNET_HELLO_address_free (address);
2440 if (NULL == s->frag_ctx)
2442 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2443 "Fragmentation context of address %s for ACK (%s) not found\n",
2444 udp_address_to_string (plugin,
2446 address->address_length),
2447 GNUNET_FRAGMENT_print_ack (ack));
2448 GNUNET_HELLO_address_free (address);
2451 GNUNET_HELLO_address_free (address);
2453 /* evaluate flow delay: how long should we wait between messages? */
2454 if (UINT32_MAX == ntohl (udp_ack->delay))
2456 /* Other peer asked for us to terminate the session */
2457 LOG (GNUNET_ERROR_TYPE_INFO,
2458 "Asked to disconnect UDP session of %s\n",
2459 GNUNET_i2s (&udp_ack->sender));
2460 udp_disconnect_session (plugin,
2464 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2465 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2466 LOG (GNUNET_ERROR_TYPE_WARNING,
2467 "We received a sending delay of %s for %s\n",
2468 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2470 GNUNET_i2s (&udp_ack->sender));
2472 LOG (GNUNET_ERROR_TYPE_DEBUG,
2473 "We received a sending delay of %s for %s\n",
2474 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2476 GNUNET_i2s (&udp_ack->sender));
2477 /* Flow delay is for the reassembled packet, however, our delay
2478 is per packet, so we need to adjust: */
2479 s->flow_delay_from_other_peer = flow_delay;
2483 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2486 LOG (GNUNET_ERROR_TYPE_DEBUG,
2487 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2488 (unsigned int) ntohs (msg->size),
2489 GNUNET_i2s (&udp_ack->sender),
2490 udp_address_to_string (plugin,
2493 /* Expect more ACKs to arrive */
2497 /* Remove fragmented message after successful sending */
2498 LOG (GNUNET_ERROR_TYPE_DEBUG,
2499 "Message from %s at %s full ACK'ed\n",
2500 GNUNET_i2s (&udp_ack->sender),
2501 udp_address_to_string (plugin,
2504 fragmented_message_done (s->frag_ctx,
2510 * Message tokenizer has broken up an incomming message. Pass it on
2513 * @param cls the `struct GNUNET_ATS_Session *`
2514 * @param hdr the actual message
2515 * @return #GNUNET_OK (always)
2518 process_inbound_tokenized_messages (void *cls,
2519 const struct GNUNET_MessageHeader *hdr)
2521 struct GNUNET_ATS_Session *session = cls;
2522 struct Plugin *plugin = session->plugin;
2524 if (GNUNET_YES == session->in_destroy)
2526 reschedule_session_timeout (session);
2527 session->flow_delay_for_other_peer
2528 = plugin->env->receive (plugin->env->cls,
2537 * Destroy a session, plugin is being unloaded.
2539 * @param cls the `struct Plugin`
2540 * @param key hash of public key of target peer
2541 * @param value a `struct PeerSession *` to clean up
2542 * @return #GNUNET_OK (continue to iterate)
2545 disconnect_and_free_it (void *cls,
2546 const struct GNUNET_PeerIdentity *key,
2549 struct Plugin *plugin = cls;
2551 udp_disconnect_session (plugin,
2558 * Disconnect from a remote node. Clean up session if we have one for
2561 * @param cls closure for this call (should be handle to Plugin)
2562 * @param target the peeridentity of the peer to disconnect
2563 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2566 udp_disconnect (void *cls,
2567 const struct GNUNET_PeerIdentity *target)
2569 struct Plugin *plugin = cls;
2571 LOG (GNUNET_ERROR_TYPE_DEBUG,
2572 "Disconnecting from peer `%s'\n",
2573 GNUNET_i2s (target));
2574 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2576 &disconnect_and_free_it,
2582 * Session was idle, so disconnect it.
2584 * @param cls the `struct GNUNET_ATS_Session` to time out
2587 session_timeout (void *cls)
2589 struct GNUNET_ATS_Session *s = cls;
2590 struct Plugin *plugin = s->plugin;
2591 struct GNUNET_TIME_Relative left;
2593 s->timeout_task = NULL;
2594 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2595 if (left.rel_value_us > 0)
2597 /* not actually our turn yet, but let's at least update
2598 the monitor, it may think we're about to die ... */
2599 notify_session_monitor (s->plugin,
2601 GNUNET_TRANSPORT_SS_UPDATE);
2602 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2607 LOG (GNUNET_ERROR_TYPE_DEBUG,
2608 "Session %p was idle for %s, disconnecting\n",
2610 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2612 /* call session destroy function */
2613 udp_disconnect_session (plugin,
2619 * Allocate a new session for the given endpoint address.
2620 * Note that this function does not inform the service
2621 * of the new session, this is the responsibility of the
2622 * caller (if needed).
2624 * @param cls the `struct Plugin`
2625 * @param address address of the other peer to use
2626 * @param network_type network type the address belongs to
2627 * @return NULL on error, otherwise session handle
2629 static struct GNUNET_ATS_Session *
2630 udp_plugin_create_session (void *cls,
2631 const struct GNUNET_HELLO_Address *address,
2632 enum GNUNET_ATS_Network_Type network_type)
2634 struct Plugin *plugin = cls;
2635 struct GNUNET_ATS_Session *s;
2637 s = GNUNET_new (struct GNUNET_ATS_Session);
2638 s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
2641 s->address = GNUNET_HELLO_address_copy (address);
2642 s->target = address->peer;
2643 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2644 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2646 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2647 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2648 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2649 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2650 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2653 s->scope = network_type;
2655 LOG (GNUNET_ERROR_TYPE_DEBUG,
2656 "Creating new session %p for peer `%s' address `%s'\n",
2658 GNUNET_i2s (&address->peer),
2659 udp_address_to_string (plugin,
2661 address->address_length));
2662 GNUNET_assert (GNUNET_OK ==
2663 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2667 GNUNET_STATISTICS_set (plugin->env->stats,
2668 "# UDP sessions active",
2669 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2671 notify_session_monitor (plugin,
2673 GNUNET_TRANSPORT_SS_INIT);
2679 * Creates a new outbound session the transport service will use to
2680 * send data to the peer.
2682 * @param cls the `struct Plugin *`
2683 * @param address the address
2684 * @return the session or NULL of max connections exceeded
2686 static struct GNUNET_ATS_Session *
2687 udp_plugin_get_session (void *cls,
2688 const struct GNUNET_HELLO_Address *address)
2690 struct Plugin *plugin = cls;
2691 struct GNUNET_ATS_Session *s;
2692 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2693 const struct IPv4UdpAddress *udp_v4;
2694 const struct IPv6UdpAddress *udp_v6;
2696 if (NULL == address)
2701 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2702 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2704 GNUNET_break_op (0);
2707 if (NULL != (s = udp_plugin_lookup_session (cls,
2711 /* need to create new session */
2712 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2714 struct sockaddr_in v4;
2716 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2717 memset (&v4, '\0', sizeof (v4));
2718 v4.sin_family = AF_INET;
2719 #if HAVE_SOCKADDR_IN_SIN_LEN
2720 v4.sin_len = sizeof (struct sockaddr_in);
2722 v4.sin_port = udp_v4->u4_port;
2723 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2724 network_type = plugin->env->get_address_type (plugin->env->cls,
2725 (const struct sockaddr *) &v4,
2728 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2730 struct sockaddr_in6 v6;
2732 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2733 memset (&v6, '\0', sizeof (v6));
2734 v6.sin6_family = AF_INET6;
2735 #if HAVE_SOCKADDR_IN_SIN_LEN
2736 v6.sin6_len = sizeof (struct sockaddr_in6);
2738 v6.sin6_port = udp_v6->u6_port;
2739 v6.sin6_addr = udp_v6->ipv6_addr;
2740 network_type = plugin->env->get_address_type (plugin->env->cls,
2741 (const struct sockaddr *) &v6,
2744 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2745 return udp_plugin_create_session (cls,
2752 * We've received a UDP Message. Process it (pass contents to main service).
2754 * @param plugin plugin context
2755 * @param msg the message
2756 * @param udp_addr sender address
2757 * @param udp_addr_len number of bytes in @a udp_addr
2758 * @param network_type network type the address belongs to
2761 process_udp_message (struct Plugin *plugin,
2762 const struct UDPMessage *msg,
2763 const union UdpAddress *udp_addr,
2764 size_t udp_addr_len,
2765 enum GNUNET_ATS_Network_Type network_type)
2767 struct GNUNET_ATS_Session *s;
2768 struct GNUNET_HELLO_Address *address;
2770 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2771 if (0 != ntohl (msg->reserved))
2776 if (ntohs (msg->header.size)
2777 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2783 address = GNUNET_HELLO_address_allocate (&msg->sender,
2787 GNUNET_HELLO_ADDRESS_INFO_NONE);
2789 (s = udp_plugin_lookup_session (plugin,
2792 s = udp_plugin_create_session (plugin,
2795 plugin->env->session_start (plugin->env->cls,
2799 notify_session_monitor (plugin,
2801 GNUNET_TRANSPORT_SS_UP);
2803 GNUNET_free (address);
2806 GNUNET_MST_from_buffer (s->mst,
2807 (const char *) &msg[1],
2808 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2812 if ( (0 == s->rc) &&
2813 (GNUNET_YES == s->in_destroy) )
2819 * Process a defragmented message.
2821 * @param cls the `struct DefragContext *`
2822 * @param msg the message
2825 fragment_msg_proc (void *cls,
2826 const struct GNUNET_MessageHeader *msg)
2828 struct DefragContext *dc = cls;
2829 const struct UDPMessage *um;
2831 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2833 GNUNET_break_op (0);
2836 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2838 GNUNET_break_op (0);
2841 um = (const struct UDPMessage *) msg;
2842 dc->sender = um->sender;
2843 dc->have_sender = GNUNET_YES;
2844 process_udp_message (dc->plugin,
2853 * We finished sending an acknowledgement. Update
2856 * @param cls the `struct Plugin`
2857 * @param udpw message queue entry of the ACK
2858 * @param result #GNUNET_OK if the transmission worked,
2859 * #GNUNET_SYSERR if we failed to send the ACK
2862 ack_message_sent (void *cls,
2863 struct UDP_MessageWrapper *udpw,
2866 struct Plugin *plugin = cls;
2868 if (GNUNET_OK == result)
2870 GNUNET_STATISTICS_update (plugin->env->stats,
2871 "# UDP, ACK messages sent",
2877 GNUNET_STATISTICS_update (plugin->env->stats,
2878 "# UDP, ACK transmissions failed",
2886 * Transmit an acknowledgement.
2888 * @param cls the `struct DefragContext *`
2889 * @param id message ID (unused)
2890 * @param msg ack to transmit
2893 ack_proc (void *cls,
2895 const struct GNUNET_MessageHeader *msg)
2897 struct DefragContext *rc = cls;
2898 struct Plugin *plugin = rc->plugin;
2899 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2900 struct UDP_ACK_Message *udp_ack;
2902 struct UDP_MessageWrapper *udpw;
2903 struct GNUNET_ATS_Session *s;
2904 struct GNUNET_HELLO_Address *address;
2906 if (GNUNET_NO == rc->have_sender)
2908 /* tried to defragment but never succeeded, hence will not ACK */
2909 /* This can happen if we just lost msgs */
2910 GNUNET_STATISTICS_update (plugin->env->stats,
2911 "# UDP, fragments discarded without ACK",
2916 address = GNUNET_HELLO_address_allocate (&rc->sender,
2920 GNUNET_HELLO_ADDRESS_INFO_NONE);
2921 s = udp_plugin_lookup_session (plugin,
2923 GNUNET_HELLO_address_free (address);
2926 LOG (GNUNET_ERROR_TYPE_ERROR,
2927 "Trying to transmit ACK to peer `%s' but no session found!\n",
2928 udp_address_to_string (plugin,
2931 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2932 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2934 GNUNET_STATISTICS_update (plugin->env->stats,
2935 "# UDP, ACK transmissions failed",
2940 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2941 s->flow_delay_for_other_peer.rel_value_us)
2943 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2944 delay = s->flow_delay_for_other_peer.rel_value_us;
2946 delay = UINT32_MAX - 1; /* largest value we can communicate */
2947 LOG (GNUNET_ERROR_TYPE_DEBUG,
2948 "Sending ACK to `%s' including delay of %s\n",
2949 udp_address_to_string (plugin,
2952 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2954 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2955 udpw->msg_size = msize;
2956 udpw->payload_size = 0;
2958 udpw->start_time = GNUNET_TIME_absolute_get ();
2959 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2960 udpw->msg_buf = (char *) &udpw[1];
2961 udpw->qc = &ack_message_sent;
2962 udpw->qc_cls = plugin;
2963 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2964 udp_ack->header.size = htons ((uint16_t) msize);
2965 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2966 udp_ack->delay = htonl (delay);
2967 udp_ack->sender = *plugin->env->my_identity;
2968 GNUNET_memcpy (&udp_ack[1],
2973 notify_session_monitor (plugin,
2975 GNUNET_TRANSPORT_SS_UPDATE);
2976 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2977 schedule_select_v4 (plugin);
2979 schedule_select_v6 (plugin);
2984 * We received a fragment, process it.
2986 * @param plugin our plugin
2987 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2988 * @param udp_addr sender address
2989 * @param udp_addr_len number of bytes in @a udp_addr
2990 * @param network_type network type the address belongs to
2993 read_process_fragment (struct Plugin *plugin,
2994 const struct GNUNET_MessageHeader *msg,
2995 const union UdpAddress *udp_addr,
2996 size_t udp_addr_len,
2997 enum GNUNET_ATS_Network_Type network_type)
2999 struct DefragContext *d_ctx;
3000 struct GNUNET_TIME_Absolute now;
3001 struct FindReceiveContext frc;
3004 frc.udp_addr = udp_addr;
3005 frc.udp_addr_len = udp_addr_len;
3007 /* Lookup existing receive context for this address */
3008 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3009 &find_receive_context,
3011 now = GNUNET_TIME_absolute_get ();
3016 /* Create a new defragmentation context */
3017 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
3018 GNUNET_memcpy (&d_ctx[1],
3021 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
3022 d_ctx->udp_addr_len = udp_addr_len;
3023 d_ctx->network_type = network_type;
3024 d_ctx->plugin = plugin;
3025 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
3027 UDP_MAX_MESSAGES_IN_DEFRAG,
3031 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
3033 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3034 LOG (GNUNET_ERROR_TYPE_DEBUG,
3035 "Created new defragmentation context for %u-byte fragment from `%s'\n",
3036 (unsigned int) ntohs (msg->size),
3037 udp_address_to_string (plugin,
3043 LOG (GNUNET_ERROR_TYPE_DEBUG,
3044 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
3045 (unsigned int) ntohs (msg->size),
3046 udp_address_to_string (plugin,
3052 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
3055 /* keep this 'rc' from expiring */
3056 GNUNET_CONTAINER_heap_update_cost (d_ctx->hnode,
3057 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3059 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
3060 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
3062 /* remove 'rc' that was inactive the longest */
3063 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
3064 GNUNET_assert (NULL != d_ctx);
3065 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3066 GNUNET_free (d_ctx);
3067 GNUNET_STATISTICS_update (plugin->env->stats,
3068 "# UDP, Defragmentations aborted",
3076 * Read and process a message from the given socket.
3078 * @param plugin the overall plugin
3079 * @param rsock socket to read from
3082 udp_select_read (struct Plugin *plugin,
3083 struct GNUNET_NETWORK_Handle *rsock)
3086 struct sockaddr_storage addr;
3087 char buf[65536] GNUNET_ALIGN;
3089 const struct GNUNET_MessageHeader *msg;
3090 struct IPv4UdpAddress v4;
3091 struct IPv6UdpAddress v6;
3092 const struct sockaddr *sa;
3093 const struct sockaddr_in *sa4;
3094 const struct sockaddr_in6 *sa6;
3095 const union UdpAddress *int_addr;
3096 size_t int_addr_len;
3097 enum GNUNET_ATS_Network_Type network_type;
3099 fromlen = sizeof (addr);
3103 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3106 (struct sockaddr *) &addr,
3108 sa = (const struct sockaddr *) &addr;
3110 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3111 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3112 * on this socket has failed.
3114 * WSAECONNRESET - The virtual circuit was reset by the remote side
3115 * executing a hard or abortive close. The application should close
3116 * the socket; it is no longer usable. On a UDP-datagram socket this
3117 * error indicates a previous send operation resulted in an ICMP Port
3118 * Unreachable message.
3120 if ( (-1 == size) &&
3121 (ECONNRESET == errno) )
3126 LOG (GNUNET_ERROR_TYPE_DEBUG,
3127 "UDP failed to receive data: %s\n",
3129 /* Connection failure or something. Not a protocol violation. */
3133 /* Check if this is a STUN packet */
3135 GNUNET_NAT_stun_handle_packet (plugin->nat,
3136 (const struct sockaddr *) &addr,
3140 return; /* was STUN, do not process further */
3142 if (size < sizeof(struct GNUNET_MessageHeader))
3144 LOG (GNUNET_ERROR_TYPE_WARNING,
3145 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3146 (unsigned int ) size,
3149 /* _MAY_ be a connection failure (got partial message) */
3150 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3151 GNUNET_break_op (0);
3155 msg = (const struct GNUNET_MessageHeader *) buf;
3156 LOG (GNUNET_ERROR_TYPE_DEBUG,
3157 "UDP received %u-byte message from `%s' type %u\n",
3158 (unsigned int) size,
3162 if (size != ntohs (msg->size))
3164 LOG (GNUNET_ERROR_TYPE_WARNING,
3165 "UDP malformed message (size %u) header from %s\n",
3166 (unsigned int) size,
3169 GNUNET_break_op (0);
3172 GNUNET_STATISTICS_update (plugin->env->stats,
3173 "# UDP, total bytes received",
3176 network_type = plugin->env->get_address_type (plugin->env->cls,
3179 switch (sa->sa_family)
3182 sa4 = (const struct sockaddr_in *) &addr;
3184 v4.ipv4_addr = sa4->sin_addr.s_addr;
3185 v4.u4_port = sa4->sin_port;
3186 int_addr = (union UdpAddress *) &v4;
3187 int_addr_len = sizeof (v4);
3190 sa6 = (const struct sockaddr_in6 *) &addr;
3192 v6.ipv6_addr = sa6->sin6_addr;
3193 v6.u6_port = sa6->sin6_port;
3194 int_addr = (union UdpAddress *) &v6;
3195 int_addr_len = sizeof (v6);
3202 switch (ntohs (msg->type))
3204 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3205 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3206 udp_broadcast_receive (plugin,
3213 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3214 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3219 process_udp_message (plugin,
3220 (const struct UDPMessage *) msg,
3225 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3226 read_process_ack (plugin,
3231 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3232 read_process_fragment (plugin,
3246 * Removes messages from the transmission queue that have
3247 * timed out, and then selects a message that should be
3250 * @param plugin the UDP plugin
3251 * @param sock which socket should we process the queue for (v4 or v6)
3252 * @return message selected for transmission, or NULL for none
3254 static struct UDP_MessageWrapper *
3255 remove_timeout_messages_and_select (struct Plugin *plugin,
3256 struct GNUNET_NETWORK_Handle *sock)
3258 struct UDP_MessageWrapper *udpw;
3259 struct GNUNET_TIME_Relative remaining;
3260 struct GNUNET_ATS_Session *session;
3263 removed = GNUNET_NO;
3264 udpw = (sock == plugin->sockv4)
3265 ? plugin->ipv4_queue_head
3266 : plugin->ipv6_queue_head;
3267 while (NULL != udpw)
3269 session = udpw->session;
3270 /* Find messages with timeout */
3271 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3272 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3274 /* Message timed out */
3275 removed = GNUNET_YES;
3278 udpw->qc (udpw->qc_cls,
3283 if (sock == plugin->sockv4)
3285 udpw = plugin->ipv4_queue_head;
3287 else if (sock == plugin->sockv6)
3289 udpw = plugin->ipv6_queue_head;
3293 GNUNET_break (0); /* should never happen */
3296 GNUNET_STATISTICS_update (plugin->env->stats,
3297 "# messages discarded due to timeout",
3303 /* Message did not time out, check transmission time */
3304 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3305 if (0 == remaining.rel_value_us)
3307 /* this message is not delayed */
3308 LOG (GNUNET_ERROR_TYPE_DEBUG,
3309 "Message for peer `%s' (%u bytes) is not delayed \n",
3310 GNUNET_i2s (&udpw->session->target),
3311 udpw->payload_size);
3312 break; /* Found message to send, break */
3316 /* Message is delayed, try next */
3317 LOG (GNUNET_ERROR_TYPE_DEBUG,
3318 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3319 GNUNET_i2s (&udpw->session->target),
3321 GNUNET_STRINGS_relative_time_to_string (remaining,
3327 if (GNUNET_YES == removed)
3328 notify_session_monitor (session->plugin,
3330 GNUNET_TRANSPORT_SS_UPDATE);
3336 * We failed to transmit a message via UDP. Generate
3337 * a descriptive error message.
3339 * @param plugin our plugin
3340 * @param sa target address we were trying to reach
3341 * @param slen number of bytes in @a sa
3342 * @param error the errno value returned from the sendto() call
3345 analyze_send_error (struct Plugin *plugin,
3346 const struct sockaddr *sa,
3350 enum GNUNET_ATS_Network_Type type;
3352 type = plugin->env->get_address_type (plugin->env->cls,
3355 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3356 (GNUNET_ATS_NET_WAN == type) ) &&
3357 ( (ENETUNREACH == errno) ||
3358 (ENETDOWN == errno) ) )
3360 if (slen == sizeof (struct sockaddr_in))
3362 /* IPv4: "Network unreachable" or "Network down"
3364 * This indicates we do not have connectivity
3366 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3367 _("UDP could not transmit message to `%s': "
3368 "Network seems down, please check your network configuration\n"),
3372 if (slen == sizeof (struct sockaddr_in6))
3374 /* IPv6: "Network unreachable" or "Network down"
3376 * This indicates that this system is IPv6 enabled, but does not
3377 * have a valid global IPv6 address assigned or we do not have
3380 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3381 _("UDP could not transmit IPv6 message! "
3382 "Please check your network configuration and disable IPv6 if your "
3383 "connection does not have a global IPv6 address\n"));
3388 LOG (GNUNET_ERROR_TYPE_WARNING,
3389 "UDP could not transmit message to `%s': `%s'\n",
3398 * It is time to try to transmit a UDP message. Select one
3401 * @param plugin the plugin
3402 * @param sock which socket (v4/v6) to send on
3405 udp_select_send (struct Plugin *plugin,
3406 struct GNUNET_NETWORK_Handle *sock)
3410 const struct sockaddr *a;
3411 const struct IPv4UdpAddress *u4;
3412 struct sockaddr_in a4;
3413 const struct IPv6UdpAddress *u6;
3414 struct sockaddr_in6 a6;
3415 struct UDP_MessageWrapper *udpw;
3417 /* Find message(s) to send */
3418 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3421 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3423 u4 = udpw->session->address->address;
3427 a4.sin_family = AF_INET;
3428 #if HAVE_SOCKADDR_IN_SIN_LEN
3429 a4.sin_len = sizeof (a4);
3431 a4.sin_port = u4->u4_port;
3432 a4.sin_addr.s_addr = u4->ipv4_addr;
3433 a = (const struct sockaddr *) &a4;
3436 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3438 u6 = udpw->session->address->address;
3442 a6.sin6_family = AF_INET6;
3443 #if HAVE_SOCKADDR_IN_SIN_LEN
3444 a6.sin6_len = sizeof (a6);
3446 a6.sin6_port = u6->u6_port;
3447 a6.sin6_addr = u6->ipv6_addr;
3448 a = (const struct sockaddr *) &a6;
3456 udpw->qc (udpw->qc_cls,
3459 notify_session_monitor (plugin,
3461 GNUNET_TRANSPORT_SS_UPDATE);
3465 sent = GNUNET_NETWORK_socket_sendto (sock,
3470 udpw->session->last_transmit_time
3471 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3472 udpw->session->last_transmit_time);
3475 if (GNUNET_SYSERR == sent)
3478 analyze_send_error (plugin,
3482 udpw->qc (udpw->qc_cls,
3485 GNUNET_STATISTICS_update (plugin->env->stats,
3486 "# UDP, total, bytes, sent, failure",
3489 GNUNET_STATISTICS_update (plugin->env->stats,
3490 "# UDP, total, messages, sent, failure",
3497 LOG (GNUNET_ERROR_TYPE_DEBUG,
3498 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3499 (unsigned int) (udpw->msg_size),
3500 GNUNET_i2s (&udpw->session->target),
3504 (sent < 0) ? STRERROR (errno) : "ok");
3505 GNUNET_STATISTICS_update (plugin->env->stats,
3506 "# UDP, total, bytes, sent, success",
3509 GNUNET_STATISTICS_update (plugin->env->stats,
3510 "# UDP, total, messages, sent, success",
3513 if (NULL != udpw->frag_ctx)
3514 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3515 udpw->qc (udpw->qc_cls,
3519 notify_session_monitor (plugin,
3521 GNUNET_TRANSPORT_SS_UPDATE);
3527 /* ***************** Event loop (part 2) *************** */
3531 * We have been notified that our readset has something to read. We don't
3532 * know which socket needs to be read, so we have to check each one
3533 * Then reschedule this function to be called again once more is available.
3535 * @param cls the plugin handle
3538 udp_plugin_select_v4 (void *cls)
3540 struct Plugin *plugin = cls;
3541 const struct GNUNET_SCHEDULER_TaskContext *tc;
3543 plugin->select_task_v4 = NULL;
3544 if (NULL == plugin->sockv4)
3546 tc = GNUNET_SCHEDULER_get_task_context ();
3547 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3548 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3550 udp_select_read (plugin,
3552 udp_select_send (plugin,
3554 schedule_select_v4 (plugin);
3559 * We have been notified that our readset has something to read. We don't
3560 * know which socket needs to be read, so we have to check each one
3561 * Then reschedule this function to be called again once more is available.
3563 * @param cls the plugin handle
3566 udp_plugin_select_v6 (void *cls)
3568 struct Plugin *plugin = cls;
3569 const struct GNUNET_SCHEDULER_TaskContext *tc;
3571 plugin->select_task_v6 = NULL;
3572 if (NULL == plugin->sockv6)
3574 tc = GNUNET_SCHEDULER_get_task_context ();
3575 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3576 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3578 udp_select_read (plugin,
3581 udp_select_send (plugin,
3583 schedule_select_v6 (plugin);
3587 /* ******************* Initialization *************** */
3591 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3593 * @param plugin the plugin to initialize
3594 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3595 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3596 * @return number of sockets that were successfully bound
3599 setup_sockets (struct Plugin *plugin,
3600 const struct sockaddr_in6 *bind_v6,
3601 const struct sockaddr_in *bind_v4)
3604 unsigned int sockets_created = 0;
3605 struct sockaddr_in6 server_addrv6;
3606 struct sockaddr_in server_addrv4;
3607 const struct sockaddr *server_addr;
3608 const struct sockaddr *addrs[2];
3609 socklen_t addrlens[2];
3613 /* Create IPv6 socket */
3615 if (GNUNET_YES == plugin->enable_ipv6)
3617 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3620 if (NULL == plugin->sockv6)
3622 LOG (GNUNET_ERROR_TYPE_INFO,
3623 _("Disabling IPv6 since it is not supported on this system!\n"));
3624 plugin->enable_ipv6 = GNUNET_NO;
3628 memset (&server_addrv6,
3630 sizeof(struct sockaddr_in6));
3631 #if HAVE_SOCKADDR_IN_SIN_LEN
3632 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3634 server_addrv6.sin6_family = AF_INET6;
3635 if (NULL != bind_v6)
3636 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3638 server_addrv6.sin6_addr = in6addr_any;
3640 if (0 == plugin->port) /* autodetect */
3641 server_addrv6.sin6_port
3642 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3646 server_addrv6.sin6_port = htons (plugin->port);
3647 addrlen = sizeof (struct sockaddr_in6);
3648 server_addr = (const struct sockaddr *) &server_addrv6;
3653 LOG(GNUNET_ERROR_TYPE_DEBUG,
3654 "Binding to IPv6 `%s'\n",
3655 GNUNET_a2s (server_addr,
3659 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3664 if (0 != plugin->port)
3666 tries = 10; /* fail immediately */
3667 break; /* bind failed on specific port */
3670 server_addrv6.sin6_port
3671 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3678 GNUNET_NETWORK_socket_close (plugin->sockv6);
3679 plugin->enable_ipv6 = GNUNET_NO;
3680 plugin->sockv6 = NULL;
3684 plugin->port = ntohs (server_addrv6.sin6_port);
3686 if (NULL != plugin->sockv6)
3688 LOG (GNUNET_ERROR_TYPE_DEBUG,
3689 "IPv6 UDP socket created listinging at %s\n",
3690 GNUNET_a2s (server_addr,
3692 addrs[sockets_created] = server_addr;
3693 addrlens[sockets_created] = addrlen;
3698 LOG (GNUNET_ERROR_TYPE_WARNING,
3699 _("Failed to bind UDP socket to %s: %s\n"),
3700 GNUNET_a2s (server_addr,
3707 /* Create IPv4 socket */
3709 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3712 if (NULL == plugin->sockv4)
3714 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3716 LOG (GNUNET_ERROR_TYPE_INFO,
3717 _("Disabling IPv4 since it is not supported on this system!\n"));
3718 plugin->enable_ipv4 = GNUNET_NO;
3722 memset (&server_addrv4,
3724 sizeof(struct sockaddr_in));
3725 #if HAVE_SOCKADDR_IN_SIN_LEN
3726 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3728 server_addrv4.sin_family = AF_INET;
3729 if (NULL != bind_v4)
3730 server_addrv4.sin_addr = bind_v4->sin_addr;
3732 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3734 if (0 == plugin->port)
3736 server_addrv4.sin_port
3737 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3741 server_addrv4.sin_port = htons (plugin->port);
3743 addrlen = sizeof (struct sockaddr_in);
3744 server_addr = (const struct sockaddr *) &server_addrv4;
3749 LOG (GNUNET_ERROR_TYPE_DEBUG,
3750 "Binding to IPv4 `%s'\n",
3751 GNUNET_a2s (server_addr,
3756 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3761 if (0 != plugin->port)
3763 tries = 10; /* fail */
3764 break; /* bind failed on specific port */
3768 server_addrv4.sin_port
3769 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3776 GNUNET_NETWORK_socket_close (plugin->sockv4);
3777 plugin->enable_ipv4 = GNUNET_NO;
3778 plugin->sockv4 = NULL;
3782 plugin->port = ntohs (server_addrv4.sin_port);
3785 if (NULL != plugin->sockv4)
3787 LOG (GNUNET_ERROR_TYPE_DEBUG,
3788 "IPv4 socket created on port %s\n",
3789 GNUNET_a2s (server_addr,
3791 addrs[sockets_created] = server_addr;
3792 addrlens[sockets_created] = addrlen;
3797 LOG (GNUNET_ERROR_TYPE_ERROR,
3798 _("Failed to bind UDP socket to %s: %s\n"),
3799 GNUNET_a2s (server_addr,
3805 if (0 == sockets_created)
3807 LOG (GNUNET_ERROR_TYPE_WARNING,
3808 _("Failed to open UDP sockets\n"));
3809 return 0; /* No sockets created, return */
3811 schedule_select_v4 (plugin);
3812 schedule_select_v6 (plugin);
3813 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3819 &udp_nat_port_map_callback,
3822 return sockets_created;
3827 * The exported method. Makes the core api available via a global and
3828 * returns the udp transport API.
3830 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3831 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3834 libgnunet_plugin_transport_udp_init (void *cls)
3836 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3837 struct GNUNET_TRANSPORT_PluginFunctions *api;
3839 unsigned long long port;
3840 unsigned long long aport;
3841 unsigned long long udp_max_bps;
3842 unsigned long long enable_v6;
3843 unsigned long long enable_broadcasting;
3844 unsigned long long enable_broadcasting_recv;
3845 char *bind4_address;
3846 char *bind6_address;
3847 struct GNUNET_TIME_Relative interval;
3848 struct sockaddr_in server_addrv4;
3849 struct sockaddr_in6 server_addrv6;
3854 if (NULL == env->receive)
3856 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3857 initialze the plugin or the API */
3858 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3860 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3861 api->address_to_string = &udp_address_to_string;
3862 api->string_to_address = &udp_string_to_address;
3866 /* Get port number: port == 0 : autodetect a port,
3867 * > 0 : use this port, not given : 2086 default */
3869 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3876 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3879 _("must be in [0,65535]"));
3883 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3890 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3893 _("must be in [0,65535]"));
3898 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3901 enable_v6 = GNUNET_NO;
3903 enable_v6 = GNUNET_YES;
3905 have_bind4 = GNUNET_NO;
3906 memset (&server_addrv4,
3908 sizeof (server_addrv4));
3910 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3915 LOG (GNUNET_ERROR_TYPE_DEBUG,
3916 "Binding UDP plugin to specific address: `%s'\n",
3918 if (1 != inet_pton (AF_INET,
3920 &server_addrv4.sin_addr))
3922 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3925 _("must be valid IPv4 address"));
3926 GNUNET_free (bind4_address);
3929 have_bind4 = GNUNET_YES;
3931 GNUNET_free_non_null (bind4_address);
3932 have_bind6 = GNUNET_NO;
3933 memset (&server_addrv6,
3935 sizeof (server_addrv6));
3937 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3942 LOG (GNUNET_ERROR_TYPE_DEBUG,
3943 "Binding udp plugin to specific address: `%s'\n",
3945 if (1 != inet_pton (AF_INET6,
3947 &server_addrv6.sin6_addr))
3949 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3952 _("must be valid IPv6 address"));
3953 GNUNET_free (bind6_address);
3956 have_bind6 = GNUNET_YES;
3958 GNUNET_free_non_null (bind6_address);
3960 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3963 if (enable_broadcasting == GNUNET_SYSERR)
3964 enable_broadcasting = GNUNET_NO;
3966 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3968 "BROADCAST_RECEIVE");
3969 if (enable_broadcasting_recv == GNUNET_SYSERR)
3970 enable_broadcasting_recv = GNUNET_YES;
3972 if (GNUNET_SYSERR ==
3973 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3975 "BROADCAST_INTERVAL",
3978 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3982 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3987 /* 50 MB/s == infinity for practical purposes */
3988 udp_max_bps = 1024 * 1024 * 50;
3991 p = GNUNET_new (struct Plugin);
3994 p->broadcast_interval = interval;
3995 p->enable_ipv6 = enable_v6;
3996 p->enable_ipv4 = GNUNET_YES; /* default */
3997 p->enable_broadcasting = enable_broadcasting;
3998 p->enable_broadcasting_receiving = enable_broadcasting_recv;
4000 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
4002 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
4003 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
4006 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
4008 res = setup_sockets (p,
4009 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
4010 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
4012 ( (NULL == p->sockv4) &&
4013 (NULL == p->sockv6) ) )
4015 LOG (GNUNET_ERROR_TYPE_ERROR,
4016 _("Failed to create UDP network sockets\n"));
4017 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
4018 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
4020 GNUNET_NAT_unregister (p->nat);
4025 /* Setup broadcasting and receiving beacons */
4030 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
4032 api->disconnect_session = &udp_disconnect_session;
4033 api->query_keepalive_factor = &udp_query_keepalive_factor;
4034 api->disconnect_peer = &udp_disconnect;
4035 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
4036 api->address_to_string = &udp_address_to_string;
4037 api->string_to_address = &udp_string_to_address;
4038 api->check_address = &udp_plugin_check_address;
4039 api->get_session = &udp_plugin_get_session;
4040 api->send = &udp_plugin_send;
4041 api->get_network = &udp_plugin_get_network;
4042 api->get_network_for_address = &udp_plugin_get_network_for_address;
4043 api->update_session_timeout = &udp_plugin_update_session_timeout;
4044 api->setup_monitor = &udp_plugin_setup_monitor;
4050 * Function called on each entry in the defragmentation heap to
4054 * @param node node in the heap (to be removed)
4055 * @param element a `struct DefragContext` to be cleaned up
4056 * @param cost unused
4057 * @return #GNUNET_YES
4060 heap_cleanup_iterator (void *cls,
4061 struct GNUNET_CONTAINER_HeapNode *node,
4063 GNUNET_CONTAINER_HeapCostType cost)
4065 struct DefragContext *d_ctx = element;
4067 GNUNET_CONTAINER_heap_remove_node (node);
4068 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4069 GNUNET_free (d_ctx);
4075 * The exported method. Makes the core api available via a global and
4076 * returns the udp transport API.
4078 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4082 libgnunet_plugin_transport_udp_done (void *cls)
4084 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4085 struct Plugin *plugin = api->cls;
4086 struct PrettyPrinterContext *cur;
4087 struct UDP_MessageWrapper *udpw;
4094 stop_broadcast (plugin);
4095 if (NULL != plugin->select_task_v4)
4097 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4098 plugin->select_task_v4 = NULL;
4100 if (NULL != plugin->select_task_v6)
4102 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4103 plugin->select_task_v6 = NULL;
4105 if (NULL != plugin->sockv4)
4107 GNUNET_break (GNUNET_OK ==
4108 GNUNET_NETWORK_socket_close (plugin->sockv4));
4109 plugin->sockv4 = NULL;
4111 if (NULL != plugin->sockv6)
4113 GNUNET_break (GNUNET_OK ==
4114 GNUNET_NETWORK_socket_close (plugin->sockv6));
4115 plugin->sockv6 = NULL;
4117 if (NULL != plugin->nat)
4119 GNUNET_NAT_unregister (plugin->nat);
4122 if (NULL != plugin->defrag_ctxs)
4124 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4125 &heap_cleanup_iterator,
4127 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4128 plugin->defrag_ctxs = NULL;
4130 while (NULL != (udpw = plugin->ipv4_queue_head))
4134 udpw->qc (udpw->qc_cls,
4139 while (NULL != (udpw = plugin->ipv6_queue_head))
4143 udpw->qc (udpw->qc_cls,
4148 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4149 &disconnect_and_free_it,
4151 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4153 while (NULL != (cur = plugin->ppc_dll_head))
4156 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4157 plugin->ppc_dll_tail,
4159 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4160 if (NULL != cur->timeout_task)
4162 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4163 cur->timeout_task = NULL;
4167 GNUNET_free (plugin);
4172 /* end of plugin_transport_udp.c */