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).
483 * A value of UINT32_MAX indicates that the other
484 * peer wants us to disconnect.
486 uint32_t delay GNUNET_PACKED;
489 * What is the identity of the sender
491 struct GNUNET_PeerIdentity sender;
495 GNUNET_NETWORK_STRUCT_END
498 /* ************************* Monitoring *********** */
502 * If a session monitor is attached, notify it about the new
505 * @param plugin our plugin
506 * @param session session that changed state
507 * @param state new state of the session
510 notify_session_monitor (struct Plugin *plugin,
511 struct GNUNET_ATS_Session *session,
512 enum GNUNET_TRANSPORT_SessionState state)
514 struct GNUNET_TRANSPORT_SessionInfo info;
516 if (NULL == plugin->sic)
518 if (GNUNET_YES == session->in_destroy)
519 return; /* already destroyed, just RC>0 left-over actions */
524 info.is_inbound = GNUNET_SYSERR; /* hard to say */
525 info.num_msg_pending = session->msgs_in_queue;
526 info.num_bytes_pending = session->bytes_in_queue;
527 /* info.receive_delay remains zero as this is not supported by UDP
528 (cannot selectively not receive from 'some' peer while continuing
529 to receive from others) */
530 info.session_timeout = session->timeout;
531 info.address = session->address;
532 plugin->sic (plugin->sic_cls,
539 * Return information about the given session to the monitor callback.
541 * @param cls the `struct Plugin` with the monitor callback (`sic`)
542 * @param peer peer we send information about
543 * @param value our `struct GNUNET_ATS_Session` to send information about
544 * @return #GNUNET_OK (continue to iterate)
547 send_session_info_iter (void *cls,
548 const struct GNUNET_PeerIdentity *peer,
551 struct Plugin *plugin = cls;
552 struct GNUNET_ATS_Session *session = value;
554 notify_session_monitor (plugin,
556 GNUNET_TRANSPORT_SS_INIT);
557 notify_session_monitor (plugin,
559 GNUNET_TRANSPORT_SS_UP);
565 * Begin monitoring sessions of a plugin. There can only
566 * be one active monitor per plugin (i.e. if there are
567 * multiple monitors, the transport service needs to
568 * multiplex the generated events over all of them).
570 * @param cls closure of the plugin
571 * @param sic callback to invoke, NULL to disable monitor;
572 * plugin will being by iterating over all active
573 * sessions immediately and then enter monitor mode
574 * @param sic_cls closure for @a sic
577 udp_plugin_setup_monitor (void *cls,
578 GNUNET_TRANSPORT_SessionInfoCallback sic,
581 struct Plugin *plugin = cls;
584 plugin->sic_cls = sic_cls;
587 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
588 &send_session_info_iter,
590 /* signal end of first iteration */
598 /* ****************** Little Helpers ****************** */
602 * Function to free last resources associated with a session.
604 * @param s session to free
607 free_session (struct GNUNET_ATS_Session *s)
609 if (NULL != s->address)
611 GNUNET_HELLO_address_free (s->address);
614 if (NULL != s->frag_ctx)
616 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag,
619 GNUNET_free (s->frag_ctx);
627 * Function that is called to get the keepalive factor.
628 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
629 * calculate the interval between keepalive packets.
631 * @param cls closure with the `struct Plugin`
632 * @return keepalive factor
635 udp_query_keepalive_factor (void *cls)
642 * Function obtain the network type for a session
644 * @param cls closure (`struct Plugin *`)
645 * @param session the session
646 * @return the network type
648 static enum GNUNET_ATS_Network_Type
649 udp_plugin_get_network (void *cls,
650 struct GNUNET_ATS_Session *session)
652 return session->scope;
657 * Function obtain the network type for an address.
659 * @param cls closure (`struct Plugin *`)
660 * @param address the address
661 * @return the network type
663 static enum GNUNET_ATS_Network_Type
664 udp_plugin_get_network_for_address (void *cls,
665 const struct GNUNET_HELLO_Address *address)
667 struct Plugin *plugin = cls;
669 struct sockaddr_in a4;
670 struct sockaddr_in6 a6;
671 const struct IPv4UdpAddress *u4;
672 const struct IPv6UdpAddress *u6;
676 addrlen = address->address_length;
677 if (addrlen == sizeof(struct IPv6UdpAddress))
679 GNUNET_assert (NULL != address->address); /* make static analysis happy */
680 u6 = address->address;
681 memset (&a6, 0, sizeof(a6));
682 #if HAVE_SOCKADDR_IN_SIN_LEN
683 a6.sin6_len = sizeof (a6);
685 a6.sin6_family = AF_INET6;
686 a6.sin6_port = u6->u6_port;
687 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
691 else if (addrlen == sizeof(struct IPv4UdpAddress))
693 GNUNET_assert (NULL != address->address); /* make static analysis happy */
694 u4 = address->address;
695 memset (&a4, 0, sizeof(a4));
696 #if HAVE_SOCKADDR_IN_SIN_LEN
697 a4.sin_len = sizeof (a4);
699 a4.sin_family = AF_INET;
700 a4.sin_port = u4->u4_port;
701 a4.sin_addr.s_addr = u4->ipv4_addr;
708 return GNUNET_ATS_NET_UNSPECIFIED;
710 return plugin->env->get_address_type (plugin->env->cls,
716 /* ******************* Event loop ******************** */
719 * We have been notified that our readset has something to read. We don't
720 * know which socket needs to be read, so we have to check each one
721 * Then reschedule this function to be called again once more is available.
723 * @param cls the plugin handle
724 * @param tc the scheduling context (for rescheduling this function again)
727 udp_plugin_select_v4 (void *cls,
728 const struct GNUNET_SCHEDULER_TaskContext *tc);
732 * We have been notified that our readset has something to read. We don't
733 * know which socket needs to be read, so we have to check each one
734 * Then reschedule this function to be called again once more is available.
736 * @param cls the plugin handle
737 * @param tc the scheduling context (for rescheduling this function again)
740 udp_plugin_select_v6 (void *cls,
741 const struct GNUNET_SCHEDULER_TaskContext *tc);
745 * (re)schedule IPv4-select tasks for this plugin.
747 * @param plugin plugin to reschedule
750 schedule_select_v4 (struct Plugin *plugin)
752 struct GNUNET_TIME_Relative min_delay;
753 struct GNUNET_TIME_Relative delay;
754 struct UDP_MessageWrapper *udpw;
755 struct UDP_MessageWrapper *min_udpw;
757 if ( (GNUNET_YES == plugin->enable_ipv4) &&
758 (NULL != plugin->sockv4) )
760 /* Find a message ready to send:
761 * Flow delay from other peer is expired or not set (0) */
762 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
764 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
766 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
767 if (delay.rel_value_us < min_delay.rel_value_us)
773 if (NULL != plugin->select_task_v4)
774 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
775 if (NULL != min_udpw)
777 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
779 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
780 "Calculated flow delay for UDPv4 at %s for %s\n",
781 GNUNET_STRINGS_relative_time_to_string (min_delay,
783 GNUNET_i2s (&udpw->session->target));
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Calculated flow delay for UDPv4 at %s for %s\n",
789 GNUNET_STRINGS_relative_time_to_string (min_delay,
791 GNUNET_i2s (&udpw->session->target));
794 plugin->select_task_v4
795 = GNUNET_SCHEDULER_add_read_net (min_delay,
797 &udp_plugin_select_v4,
804 * (re)schedule IPv6-select tasks for this plugin.
806 * @param plugin plugin to reschedule
809 schedule_select_v6 (struct Plugin *plugin)
811 struct GNUNET_TIME_Relative min_delay;
812 struct GNUNET_TIME_Relative delay;
813 struct UDP_MessageWrapper *udpw;
814 struct UDP_MessageWrapper *min_udpw;
816 if ( (GNUNET_YES == plugin->enable_ipv6) &&
817 (NULL != plugin->sockv6) )
819 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
821 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
823 delay = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
824 if (delay.rel_value_us < min_delay.rel_value_us)
830 if (NULL != plugin->select_task_v6)
831 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
832 if (NULL != min_udpw)
834 if (min_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
837 "Calculated flow delay for UDPv6 at %s for %s\n",
838 GNUNET_STRINGS_relative_time_to_string (min_delay,
840 GNUNET_i2s (&udpw->session->target));
844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
845 "Calculated flow delay for UDPv6 at %s for %s\n",
846 GNUNET_STRINGS_relative_time_to_string (min_delay,
848 GNUNET_i2s (&udpw->session->target));
851 plugin->select_task_v6
852 = GNUNET_SCHEDULER_add_read_net (min_delay,
854 &udp_plugin_select_v6,
860 /* ******************* Address to string and back ***************** */
864 * Function called for a quick conversion of the binary address to
865 * a numeric address. Note that the caller must not free the
866 * address and that the next call to this function is allowed
867 * to override the address again.
870 * @param addr binary address (a `union UdpAddress`)
871 * @param addrlen length of the @a addr
872 * @return string representing the same address
875 udp_address_to_string (void *cls,
879 static char rbuf[INET6_ADDRSTRLEN + 10];
880 char buf[INET6_ADDRSTRLEN];
884 const struct IPv4UdpAddress *t4;
885 const struct IPv6UdpAddress *t6;
896 if (addrlen == sizeof(struct IPv6UdpAddress))
900 options = ntohl (t6->options);
901 port = ntohs (t6->u6_port);
905 else if (addrlen == sizeof(struct IPv4UdpAddress))
909 options = ntohl (t4->options);
910 port = ntohs (t4->u4_port);
911 a4.s_addr = t4->ipv4_addr;
923 GNUNET_snprintf (rbuf,
937 * Function called to convert a string address to a binary address.
939 * @param cls closure (`struct Plugin *`)
940 * @param addr string address
941 * @param addrlen length of the address
942 * @param buf location to store the buffer
943 * @param added location to store the number of bytes in the buffer.
944 * If the function returns #GNUNET_SYSERR, its contents are undefined.
945 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
948 udp_string_to_address (void *cls,
954 struct sockaddr_storage socket_address;
960 /* Format tcp.options.address:port */
965 if ((NULL == addr) || (0 == addrlen))
968 return GNUNET_SYSERR;
970 if ('\0' != addr[addrlen - 1])
973 return GNUNET_SYSERR;
975 if (strlen (addr) != addrlen - 1)
978 return GNUNET_SYSERR;
980 plugin = GNUNET_strdup (addr);
981 optionstr = strchr (plugin, '.');
982 if (NULL == optionstr)
985 GNUNET_free (plugin);
986 return GNUNET_SYSERR;
990 options = atol (optionstr);
991 address = strchr (optionstr, '.');
995 GNUNET_free (plugin);
996 return GNUNET_SYSERR;
1002 GNUNET_STRINGS_to_address_ip (address,
1007 GNUNET_free (plugin);
1008 return GNUNET_SYSERR;
1010 GNUNET_free(plugin);
1012 switch (socket_address.ss_family)
1016 struct IPv4UdpAddress *u4;
1017 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
1019 u4 = GNUNET_new (struct IPv4UdpAddress);
1020 u4->options = htonl (options);
1021 u4->ipv4_addr = in4->sin_addr.s_addr;
1022 u4->u4_port = in4->sin_port;
1024 *added = sizeof (struct IPv4UdpAddress);
1029 struct IPv6UdpAddress *u6;
1030 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
1032 u6 = GNUNET_new (struct IPv6UdpAddress);
1033 u6->options = htonl (options);
1034 u6->ipv6_addr = in6->sin6_addr;
1035 u6->u6_port = in6->sin6_port;
1037 *added = sizeof (struct IPv6UdpAddress);
1042 return GNUNET_SYSERR;
1048 * Append our port and forward the result.
1050 * @param cls a `struct PrettyPrinterContext *`
1051 * @param hostname result from DNS resolver
1054 append_port (void *cls,
1055 const char *hostname)
1057 struct PrettyPrinterContext *ppc = cls;
1058 struct Plugin *plugin = ppc->plugin;
1061 if (NULL == hostname)
1063 /* Final call, done */
1064 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1065 plugin->ppc_dll_tail,
1067 ppc->resolver_handle = NULL;
1068 ppc->asc (ppc->asc_cls,
1074 if (GNUNET_YES == ppc->ipv6)
1075 GNUNET_asprintf (&ret,
1082 GNUNET_asprintf (&ret,
1088 ppc->asc (ppc->asc_cls,
1096 * Convert the transports address to a nice, human-readable format.
1098 * @param cls closure with the `struct Plugin *`
1099 * @param type name of the transport that generated the address
1100 * @param addr one of the addresses of the host, NULL for the last address
1101 * the specific address format depends on the transport;
1102 * a `union UdpAddress`
1103 * @param addrlen length of the address
1104 * @param numeric should (IP) addresses be displayed in numeric form?
1105 * @param timeout after how long should we give up?
1106 * @param asc function to call on each string
1107 * @param asc_cls closure for @a asc
1110 udp_plugin_address_pretty_printer (void *cls,
1115 struct GNUNET_TIME_Relative timeout,
1116 GNUNET_TRANSPORT_AddressStringCallback asc,
1119 struct Plugin *plugin = cls;
1120 struct PrettyPrinterContext *ppc;
1121 const struct sockaddr *sb;
1123 struct sockaddr_in a4;
1124 struct sockaddr_in6 a6;
1125 const struct IPv4UdpAddress *u4;
1126 const struct IPv6UdpAddress *u6;
1130 if (addrlen == sizeof(struct IPv6UdpAddress))
1136 a6.sin6_family = AF_INET6;
1137 #if HAVE_SOCKADDR_IN_SIN_LEN
1138 a6.sin6_len = sizeof (a6);
1140 a6.sin6_port = u6->u6_port;
1141 a6.sin6_addr = u6->ipv6_addr;
1142 port = ntohs (u6->u6_port);
1143 options = ntohl (u6->options);
1144 sb = (const struct sockaddr *) &a6;
1147 else if (addrlen == sizeof (struct IPv4UdpAddress))
1153 a4.sin_family = AF_INET;
1154 #if HAVE_SOCKADDR_IN_SIN_LEN
1155 a4.sin_len = sizeof (a4);
1157 a4.sin_port = u4->u4_port;
1158 a4.sin_addr.s_addr = u4->ipv4_addr;
1159 port = ntohs (u4->u4_port);
1160 options = ntohl (u4->options);
1161 sb = (const struct sockaddr *) &a4;
1166 /* invalid address */
1167 GNUNET_break_op (0);
1176 ppc = GNUNET_new (struct PrettyPrinterContext);
1177 ppc->plugin = plugin;
1179 ppc->asc_cls = asc_cls;
1181 ppc->options = options;
1182 if (addrlen == sizeof (struct IPv6UdpAddress))
1183 ppc->ipv6 = GNUNET_YES;
1185 ppc->ipv6 = GNUNET_NO;
1186 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
1187 plugin->ppc_dll_tail,
1189 ppc->resolver_handle
1190 = GNUNET_RESOLVER_hostname_get (sb,
1200 * Check if the given port is plausible (must be either our listen
1201 * port or our advertised port). If it is neither, we return
1204 * @param plugin global variables
1205 * @param in_port port number to check
1206 * @return #GNUNET_OK if port is either our open or advertised port
1209 check_port (const struct Plugin *plugin,
1212 if ( (plugin->port == in_port) ||
1213 (plugin->aport == in_port) )
1215 return GNUNET_SYSERR;
1220 * Function that will be called to check if a binary address for this
1221 * plugin is well-formed and corresponds to an address for THIS peer
1222 * (as per our configuration). Naturally, if absolutely necessary,
1223 * plugins can be a bit conservative in their answer, but in general
1224 * plugins should make sure that the address does not redirect
1225 * traffic to a 3rd party that might try to man-in-the-middle our
1228 * @param cls closure, should be our handle to the Plugin
1229 * @param addr pointer to a `union UdpAddress`
1230 * @param addrlen length of @a addr
1231 * @return #GNUNET_OK if this is a plausible address for this peer
1232 * and transport, #GNUNET_SYSERR if not
1235 udp_plugin_check_address (void *cls,
1239 struct Plugin *plugin = cls;
1240 const struct IPv4UdpAddress *v4;
1241 const struct IPv6UdpAddress *v6;
1243 if (sizeof(struct IPv4UdpAddress) == addrlen)
1245 v4 = (const struct IPv4UdpAddress *) addr;
1246 if (GNUNET_OK != check_port (plugin,
1247 ntohs (v4->u4_port)))
1248 return GNUNET_SYSERR;
1250 GNUNET_NAT_test_address (plugin->nat,
1252 sizeof (struct in_addr)))
1253 return GNUNET_SYSERR;
1255 else if (sizeof(struct IPv6UdpAddress) == addrlen)
1257 v6 = (const struct IPv6UdpAddress *) addr;
1258 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1260 GNUNET_break_op (0);
1261 return GNUNET_SYSERR;
1263 if (GNUNET_OK != check_port (plugin,
1264 ntohs (v6->u6_port)))
1265 return GNUNET_SYSERR;
1267 GNUNET_NAT_test_address (plugin->nat,
1269 sizeof (struct in6_addr)))
1270 return GNUNET_SYSERR;
1274 GNUNET_break_op (0);
1275 return GNUNET_SYSERR;
1282 * Our external IP address/port mapping has changed.
1284 * @param cls closure, the `struct Plugin`
1285 * @param add_remove #GNUNET_YES to mean the new public IP address,
1286 * #GNUNET_NO to mean the previous (now invalid) one
1287 * @param addr either the previous or the new public IP address
1288 * @param addrlen actual length of the @a addr
1291 udp_nat_port_map_callback (void *cls,
1293 const struct sockaddr *addr,
1296 struct Plugin *plugin = cls;
1297 struct GNUNET_HELLO_Address *address;
1298 struct IPv4UdpAddress u4;
1299 struct IPv6UdpAddress u6;
1303 LOG (GNUNET_ERROR_TYPE_DEBUG,
1304 (GNUNET_YES == add_remove)
1305 ? "NAT notification to add address `%s'\n"
1306 : "NAT notification to remove address `%s'\n",
1309 /* convert 'address' to our internal format */
1310 switch (addr->sa_family)
1314 const struct sockaddr_in *i4;
1316 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
1317 i4 = (const struct sockaddr_in *) addr;
1318 if (0 == ntohs (i4->sin_port))
1326 u4.options = htonl (plugin->myoptions);
1327 u4.ipv4_addr = i4->sin_addr.s_addr;
1328 u4.u4_port = i4->sin_port;
1330 args = sizeof (struct IPv4UdpAddress);
1335 const struct sockaddr_in6 *i6;
1337 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
1338 i6 = (const struct sockaddr_in6 *) addr;
1339 if (0 == ntohs (i6->sin6_port))
1347 u6.options = htonl (plugin->myoptions);
1348 u6.ipv6_addr = i6->sin6_addr;
1349 u6.u6_port = i6->sin6_port;
1351 args = sizeof (struct IPv6UdpAddress);
1358 /* modify our published address list */
1359 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1363 GNUNET_HELLO_ADDRESS_INFO_NONE);
1364 plugin->env->notify_address (plugin->env->cls,
1367 GNUNET_HELLO_address_free (address);
1371 /* ********************* Finding sessions ******************* */
1375 * Closure for #session_cmp_it().
1377 struct GNUNET_ATS_SessionCompareContext
1380 * Set to session matching the address.
1382 struct GNUNET_ATS_Session *res;
1385 * Address we are looking for.
1387 const struct GNUNET_HELLO_Address *address;
1392 * Find a session with a matching address.
1394 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1395 * @param key peer identity (unused)
1396 * @param value the `struct GNUNET_ATS_Session *`
1397 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1400 session_cmp_it (void *cls,
1401 const struct GNUNET_PeerIdentity *key,
1404 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1405 struct GNUNET_ATS_Session *s = value;
1407 if (0 == GNUNET_HELLO_address_cmp (s->address,
1410 GNUNET_assert (GNUNET_NO == s->in_destroy);
1419 * Locate an existing session the transport service is using to
1420 * send data to another peer. Performs some basic sanity checks
1421 * on the address and then tries to locate a matching session.
1423 * @param cls the plugin
1424 * @param address the address we should locate the session by
1425 * @return the session if it exists, or NULL if it is not found
1427 static struct GNUNET_ATS_Session *
1428 udp_plugin_lookup_session (void *cls,
1429 const struct GNUNET_HELLO_Address *address)
1431 struct Plugin *plugin = cls;
1432 const struct IPv6UdpAddress *udp_a6;
1433 const struct IPv4UdpAddress *udp_a4;
1434 struct GNUNET_ATS_SessionCompareContext cctx;
1436 if (NULL == address->address)
1441 if (sizeof(struct IPv4UdpAddress) == address->address_length)
1443 if (NULL == plugin->sockv4)
1445 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1446 if (0 == udp_a4->u4_port)
1452 else if (sizeof(struct IPv6UdpAddress) == address->address_length)
1454 if (NULL == plugin->sockv6)
1456 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1457 if (0 == udp_a6->u6_port)
1469 /* check if session already exists */
1470 cctx.address = address;
1472 LOG (GNUNET_ERROR_TYPE_DEBUG,
1473 "Looking for existing session for peer `%s' with address `%s'\n",
1474 GNUNET_i2s (&address->peer),
1475 udp_address_to_string (plugin,
1477 address->address_length));
1478 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1482 if (NULL == cctx.res)
1484 LOG (GNUNET_ERROR_TYPE_DEBUG,
1485 "Found existing session %p\n",
1491 /* ********************** Timeout ****************** */
1495 * Increment session timeout due to activity.
1497 * @param s session to reschedule timeout activity for
1500 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1502 if (GNUNET_YES == s->in_destroy)
1504 GNUNET_assert (NULL != s->timeout_task);
1505 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1511 * Function that will be called whenever the transport service wants to
1512 * notify the plugin that a session is still active and in use and
1513 * therefore the session timeout for this session has to be updated
1515 * @param cls closure with the `struct Plugin`
1516 * @param peer which peer was the session for
1517 * @param session which session is being updated
1520 udp_plugin_update_session_timeout (void *cls,
1521 const struct GNUNET_PeerIdentity *peer,
1522 struct GNUNET_ATS_Session *session)
1524 struct Plugin *plugin = cls;
1527 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1534 /* Reschedule session timeout */
1535 reschedule_session_timeout (session);
1539 /* ************************* Sending ************************ */
1543 * Remove the given message from the transmission queue and
1544 * update all applicable statistics.
1546 * @param plugin the UDP plugin
1547 * @param udpw message wrapper to dequeue
1550 dequeue (struct Plugin *plugin,
1551 struct UDP_MessageWrapper *udpw)
1553 struct GNUNET_ATS_Session *session = udpw->session;
1555 if (plugin->bytes_in_buffer < udpw->msg_size)
1561 GNUNET_STATISTICS_update (plugin->env->stats,
1562 "# UDP, total bytes in send buffers",
1563 - (long long) udpw->msg_size,
1565 plugin->bytes_in_buffer -= udpw->msg_size;
1567 GNUNET_STATISTICS_update (plugin->env->stats,
1568 "# UDP, total messages in send buffers",
1571 if (sizeof(struct IPv4UdpAddress) == udpw->session->address->address_length)
1573 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1574 plugin->ipv4_queue_tail,
1577 else if (sizeof(struct IPv6UdpAddress) == udpw->session->address->address_length)
1579 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1580 plugin->ipv6_queue_tail,
1588 GNUNET_assert (session->msgs_in_queue > 0);
1589 session->msgs_in_queue--;
1590 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1591 session->bytes_in_queue -= udpw->msg_size;
1596 * Enqueue a message for transmission and update statistics.
1598 * @param plugin the UDP plugin
1599 * @param udpw message wrapper to queue
1602 enqueue (struct Plugin *plugin,
1603 struct UDP_MessageWrapper *udpw)
1605 struct GNUNET_ATS_Session *session = udpw->session;
1607 if (GNUNET_YES == session->in_destroy)
1612 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1618 GNUNET_STATISTICS_update (plugin->env->stats,
1619 "# UDP, total bytes in send buffers",
1622 plugin->bytes_in_buffer += udpw->msg_size;
1624 GNUNET_STATISTICS_update (plugin->env->stats,
1625 "# UDP, total messages in send buffers",
1628 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
1630 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1631 plugin->ipv4_queue_tail,
1634 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
1636 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1637 plugin->ipv6_queue_tail,
1643 udpw->cont (udpw->cont_cls,
1651 session->msgs_in_queue++;
1652 session->bytes_in_queue += udpw->msg_size;
1657 * We have completed our (attempt) to transmit a message that had to
1658 * be fragmented -- either because we got an ACK saying that all
1659 * fragments were received, or because of timeout / disconnect. Clean
1662 * @param frag_ctx fragmentation context to clean up
1663 * @param result #GNUNET_OK if we succeeded (got ACK),
1664 * #GNUNET_SYSERR if the transmission failed
1667 fragmented_message_done (struct UDP_FragmentationContext *frag_ctx,
1670 struct Plugin *plugin = frag_ctx->plugin;
1671 struct GNUNET_ATS_Session *s = frag_ctx->session;
1672 struct UDP_MessageWrapper *udpw;
1673 struct UDP_MessageWrapper *tmp;
1675 struct GNUNET_TIME_Relative delay;
1677 LOG (GNUNET_ERROR_TYPE_DEBUG,
1678 "%p: Fragmented message removed with result %s\n",
1680 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1681 /* Call continuation for fragmented message */
1682 if (frag_ctx->on_wire_size >= frag_ctx->payload_size)
1683 overhead = frag_ctx->on_wire_size - frag_ctx->payload_size;
1685 overhead = frag_ctx->on_wire_size;
1686 delay = GNUNET_TIME_absolute_get_duration (frag_ctx->start_time);
1687 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1689 LOG (GNUNET_ERROR_TYPE_WARNING,
1690 "Fragmented message acknowledged after %s\n",
1691 GNUNET_STRINGS_relative_time_to_string (delay,
1696 LOG (GNUNET_ERROR_TYPE_DEBUG,
1697 "Fragmented message acknowledged after %s\n",
1698 GNUNET_STRINGS_relative_time_to_string (delay,
1702 if (NULL != frag_ctx->cont)
1703 frag_ctx->cont (frag_ctx->cont_cls,
1706 s->frag_ctx->payload_size,
1707 frag_ctx->on_wire_size);
1708 GNUNET_STATISTICS_update (plugin->env->stats,
1709 "# UDP, fragmented messages active",
1713 if (GNUNET_OK == result)
1715 GNUNET_STATISTICS_update (plugin->env->stats,
1716 "# UDP, fragmented msgs, messages, sent, success",
1719 GNUNET_STATISTICS_update (plugin->env->stats,
1720 "# UDP, fragmented msgs, bytes payload, sent, success",
1721 s->frag_ctx->payload_size,
1723 GNUNET_STATISTICS_update (plugin->env->stats,
1724 "# UDP, fragmented msgs, bytes overhead, sent, success",
1727 GNUNET_STATISTICS_update (plugin->env->stats,
1728 "# UDP, total, bytes overhead, sent",
1731 GNUNET_STATISTICS_update (plugin->env->stats,
1732 "# UDP, total, bytes payload, sent",
1733 s->frag_ctx->payload_size,
1738 GNUNET_STATISTICS_update (plugin->env->stats,
1739 "# UDP, fragmented msgs, messages, sent, failure",
1742 GNUNET_STATISTICS_update (plugin->env->stats,
1743 "# UDP, fragmented msgs, bytes payload, sent, failure",
1744 s->frag_ctx->payload_size,
1746 GNUNET_STATISTICS_update (plugin->env->stats,
1747 "# UDP, fragmented msgs, bytes payload, sent, failure",
1750 GNUNET_STATISTICS_update (plugin->env->stats,
1751 "# UDP, fragmented msgs, bytes payload, sent, failure",
1756 /* Remove remaining fragments from queue, no need to transmit those
1758 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1760 udpw = plugin->ipv6_queue_head;
1761 while (NULL != udpw)
1764 if ( (udpw->frag_ctx != NULL) &&
1765 (udpw->frag_ctx == frag_ctx) )
1774 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1776 udpw = plugin->ipv4_queue_head;
1777 while (NULL != udpw)
1780 if ( (NULL != udpw->frag_ctx) &&
1781 (udpw->frag_ctx == frag_ctx) )
1790 notify_session_monitor (s->plugin,
1792 GNUNET_TRANSPORT_SS_UPDATE);
1793 GNUNET_FRAGMENT_context_destroy (frag_ctx->frag,
1794 &s->last_expected_msg_delay,
1795 &s->last_expected_ack_delay);
1797 GNUNET_free (frag_ctx);
1802 * We are finished with a fragment in the message queue.
1803 * Notify the continuation and update statistics.
1805 * @param cls the `struct Plugin *`
1806 * @param udpw the queue entry
1807 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1810 qc_fragment_sent (void *cls,
1811 struct UDP_MessageWrapper *udpw,
1814 struct Plugin *plugin = cls;
1816 GNUNET_assert (NULL != udpw->frag_ctx);
1817 if (GNUNET_OK == result)
1819 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1820 GNUNET_STATISTICS_update (plugin->env->stats,
1821 "# UDP, fragmented msgs, fragments, sent, success",
1824 GNUNET_STATISTICS_update (plugin->env->stats,
1825 "# UDP, fragmented msgs, fragments bytes, sent, success",
1831 fragmented_message_done (udpw->frag_ctx,
1833 GNUNET_STATISTICS_update (plugin->env->stats,
1834 "# UDP, fragmented msgs, fragments, sent, failure",
1837 GNUNET_STATISTICS_update (plugin->env->stats,
1838 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1846 * Function that is called with messages created by the fragmentation
1847 * module. In the case of the `proc` callback of the
1848 * #GNUNET_FRAGMENT_context_create() function, this function must
1849 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1851 * @param cls closure, the `struct UDP_FragmentationContext`
1852 * @param msg the message that was created
1855 enqueue_fragment (void *cls,
1856 const struct GNUNET_MessageHeader *msg)
1858 struct UDP_FragmentationContext *frag_ctx = cls;
1859 struct Plugin *plugin = frag_ctx->plugin;
1860 struct UDP_MessageWrapper *udpw;
1861 struct GNUNET_ATS_Session *session = frag_ctx->session;
1862 size_t msg_len = ntohs (msg->size);
1864 LOG (GNUNET_ERROR_TYPE_DEBUG,
1865 "Enqueuing fragment with %u bytes\n",
1867 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1868 udpw->session = session;
1869 udpw->msg_buf = (char *) &udpw[1];
1870 udpw->msg_size = msg_len;
1871 udpw->payload_size = msg_len; /* FIXME: minus fragment overhead */
1872 udpw->timeout = frag_ctx->timeout;
1873 udpw->start_time = frag_ctx->start_time;
1874 udpw->transmission_time = frag_ctx->next_frag_time;
1875 frag_ctx->next_frag_time
1876 = GNUNET_TIME_absolute_add (frag_ctx->next_frag_time,
1877 session->flow_delay_from_other_peer);
1878 udpw->frag_ctx = frag_ctx;
1879 udpw->qc = &qc_fragment_sent;
1880 udpw->qc_cls = plugin;
1881 memcpy (udpw->msg_buf,
1890 * We are finished with a message from the message queue.
1891 * Notify the continuation and update statistics.
1893 * @param cls the `struct Plugin *`
1894 * @param udpw the queue entry
1895 * @param result #GNUNET_OK on success, #GNUNET_SYSERR on failure
1898 qc_message_sent (void *cls,
1899 struct UDP_MessageWrapper *udpw,
1902 struct Plugin *plugin = cls;
1904 struct GNUNET_TIME_Relative delay;
1906 if (udpw->msg_size >= udpw->payload_size)
1907 overhead = udpw->msg_size - udpw->payload_size;
1909 overhead = udpw->msg_size;
1911 if (NULL != udpw->cont)
1913 delay = GNUNET_TIME_absolute_get_duration (udpw->start_time);
1914 if (delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
1916 LOG (GNUNET_ERROR_TYPE_WARNING,
1917 "Message sent via UDP with delay of %s\n",
1918 GNUNET_STRINGS_relative_time_to_string (delay,
1923 LOG (GNUNET_ERROR_TYPE_DEBUG,
1924 "Message sent via UDP with delay of %s\n",
1925 GNUNET_STRINGS_relative_time_to_string (delay,
1928 udpw->cont (udpw->cont_cls,
1929 &udpw->session->target,
1934 if (GNUNET_OK == result)
1936 GNUNET_STATISTICS_update (plugin->env->stats,
1937 "# UDP, unfragmented msgs, messages, sent, success",
1940 GNUNET_STATISTICS_update (plugin->env->stats,
1941 "# UDP, unfragmented msgs, bytes payload, sent, success",
1944 GNUNET_STATISTICS_update (plugin->env->stats,
1945 "# UDP, unfragmented msgs, bytes overhead, sent, success",
1948 GNUNET_STATISTICS_update (plugin->env->stats,
1949 "# UDP, total, bytes overhead, sent",
1952 GNUNET_STATISTICS_update (plugin->env->stats,
1953 "# UDP, total, bytes payload, sent",
1959 GNUNET_STATISTICS_update (plugin->env->stats,
1960 "# UDP, unfragmented msgs, messages, sent, failure",
1963 GNUNET_STATISTICS_update (plugin->env->stats,
1964 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1967 GNUNET_STATISTICS_update (plugin->env->stats,
1968 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1976 * Function that can be used by the transport service to transmit a
1977 * message using the plugin. Note that in the case of a peer
1978 * disconnecting, the continuation MUST be called prior to the
1979 * disconnect notification itself. This function will be called with
1980 * this peer's HELLO message to initiate a fresh connection to another
1983 * @param cls closure
1984 * @param s which session must be used
1985 * @param msgbuf the message to transmit
1986 * @param msgbuf_size number of bytes in @a msgbuf
1987 * @param priority how important is the message (most plugins will
1988 * ignore message priority and just FIFO)
1989 * @param to how long to wait at most for the transmission (does not
1990 * require plugins to discard the message after the timeout,
1991 * just advisory for the desired delay; most plugins will ignore
1993 * @param cont continuation to call once the message has
1994 * been transmitted (or if the transport is ready
1995 * for the next transmission call; or if the
1996 * peer disconnected...); can be NULL
1997 * @param cont_cls closure for @a cont
1998 * @return number of bytes used (on the physical network, with overheads);
1999 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2000 * and does NOT mean that the message was not transmitted (DV)
2003 udp_plugin_send (void *cls,
2004 struct GNUNET_ATS_Session *s,
2007 unsigned int priority,
2008 struct GNUNET_TIME_Relative to,
2009 GNUNET_TRANSPORT_TransmitContinuation cont,
2012 struct Plugin *plugin = cls;
2013 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2014 struct UDP_FragmentationContext *frag_ctx;
2015 struct UDP_MessageWrapper *udpw;
2016 struct UDPMessage *udp;
2017 char mbuf[udpmlen] GNUNET_ALIGN;
2019 if ( (sizeof(struct IPv6UdpAddress) == s->address->address_length) &&
2020 (NULL == plugin->sockv6) )
2021 return GNUNET_SYSERR;
2022 if ( (sizeof(struct IPv4UdpAddress) == s->address->address_length) &&
2023 (NULL == plugin->sockv4) )
2024 return GNUNET_SYSERR;
2025 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2028 return GNUNET_SYSERR;
2031 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2036 return GNUNET_SYSERR;
2038 LOG (GNUNET_ERROR_TYPE_DEBUG,
2039 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2041 GNUNET_i2s (&s->target),
2042 udp_address_to_string (plugin,
2043 s->address->address,
2044 s->address->address_length));
2046 udp = (struct UDPMessage *) mbuf;
2047 udp->header.size = htons (udpmlen);
2048 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2049 udp->reserved = htonl (0);
2050 udp->sender = *plugin->env->my_identity;
2052 /* We do not update the session time out here! Otherwise this
2053 * session will not timeout since we send keep alive before session
2056 * For UDP we update session timeout only on receive, this will
2057 * cover keep alives, since remote peer will reply with keep alive
2060 if (udpmlen <= UDP_MTU)
2062 /* unfragmented message */
2063 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2065 udpw->msg_buf = (char *) &udpw[1];
2066 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2067 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2068 udpw->start_time = GNUNET_TIME_absolute_get ();
2069 udpw->timeout = GNUNET_TIME_relative_to_absolute (to);
2070 udpw->transmission_time = s->last_transmit_time;
2071 s->last_transmit_time
2072 = GNUNET_TIME_absolute_add (s->last_transmit_time,
2073 s->flow_delay_from_other_peer);
2075 udpw->cont_cls = cont_cls;
2076 udpw->frag_ctx = NULL;
2077 udpw->qc = &qc_message_sent;
2078 udpw->qc_cls = plugin;
2079 memcpy (udpw->msg_buf,
2081 sizeof (struct UDPMessage));
2082 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)],
2087 GNUNET_STATISTICS_update (plugin->env->stats,
2088 "# UDP, unfragmented messages queued total",
2091 GNUNET_STATISTICS_update (plugin->env->stats,
2092 "# UDP, unfragmented bytes payload queued total",
2098 /* fragmented message */
2099 if (NULL != s->frag_ctx)
2100 return GNUNET_SYSERR;
2104 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2105 frag_ctx->plugin = plugin;
2106 frag_ctx->session = s;
2107 frag_ctx->cont = cont;
2108 frag_ctx->cont_cls = cont_cls;
2109 frag_ctx->start_time = GNUNET_TIME_absolute_get ();
2110 frag_ctx->next_frag_time = s->last_transmit_time;
2111 frag_ctx->timeout = GNUNET_TIME_relative_to_absolute (to);
2112 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2113 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2114 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2117 s->last_expected_msg_delay,
2118 s->last_expected_ack_delay,
2122 s->frag_ctx = frag_ctx;
2123 s->last_transmit_time = frag_ctx->next_frag_time;
2124 GNUNET_STATISTICS_update (plugin->env->stats,
2125 "# UDP, fragmented messages active",
2128 GNUNET_STATISTICS_update (plugin->env->stats,
2129 "# UDP, fragmented messages, total",
2132 GNUNET_STATISTICS_update (plugin->env->stats,
2133 "# UDP, fragmented bytes (payload)",
2134 frag_ctx->payload_size,
2137 notify_session_monitor (s->plugin,
2139 GNUNET_TRANSPORT_SS_UPDATE);
2140 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2141 schedule_select_v4 (plugin);
2143 schedule_select_v6 (plugin);
2148 /* ********************** Receiving ********************** */
2152 * Closure for #find_receive_context().
2154 struct FindReceiveContext
2157 * Where to store the result.
2159 struct DefragContext *rc;
2162 * Session associated with this context.
2164 struct GNUNET_ATS_Session *session;
2169 const union UdpAddress *udp_addr;
2172 * Number of bytes in @e udp_addr.
2174 size_t udp_addr_len;
2180 * Scan the heap for a receive context with the given address.
2182 * @param cls the `struct FindReceiveContext`
2183 * @param node internal node of the heap
2184 * @param element value stored at the node (a `struct ReceiveContext`)
2185 * @param cost cost associated with the node
2186 * @return #GNUNET_YES if we should continue to iterate,
2187 * #GNUNET_NO if not.
2190 find_receive_context (void *cls,
2191 struct GNUNET_CONTAINER_HeapNode *node,
2193 GNUNET_CONTAINER_HeapCostType cost)
2195 struct FindReceiveContext *frc = cls;
2196 struct DefragContext *e = element;
2198 if ( (frc->udp_addr_len == e->udp_addr_len) &&
2199 (0 == memcmp (frc->udp_addr,
2201 frc->udp_addr_len)) )
2211 * Functions with this signature are called whenever we need to close
2212 * a session due to a disconnect or failure to establish a connection.
2214 * @param cls closure with the `struct Plugin`
2215 * @param s session to close down
2216 * @return #GNUNET_OK on success
2219 udp_disconnect_session (void *cls,
2220 struct GNUNET_ATS_Session *s)
2222 struct Plugin *plugin = cls;
2223 struct UDP_MessageWrapper *udpw;
2224 struct UDP_MessageWrapper *next;
2225 struct FindReceiveContext frc;
2227 GNUNET_assert (GNUNET_YES != s->in_destroy);
2228 LOG (GNUNET_ERROR_TYPE_DEBUG,
2229 "Session %p to peer `%s' at address %s ended\n",
2231 GNUNET_i2s (&s->target),
2232 udp_address_to_string (plugin,
2233 s->address->address,
2234 s->address->address_length));
2235 if (NULL != s->timeout_task)
2237 GNUNET_SCHEDULER_cancel (s->timeout_task);
2238 s->timeout_task = NULL;
2240 if (NULL != s->frag_ctx)
2242 /* Remove fragmented message due to disconnect */
2243 fragmented_message_done (s->frag_ctx,
2246 GNUNET_assert (GNUNET_YES ==
2247 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
2251 frc.udp_addr = s->address->address;
2252 frc.udp_addr_len = s->address->address_length;
2253 /* Lookup existing receive context for this address */
2254 if (NULL != plugin->defrag_ctxs)
2256 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2257 &find_receive_context,
2261 struct DefragContext *d_ctx = frc.rc;
2263 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
2264 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2265 GNUNET_free (d_ctx);
2268 s->in_destroy = GNUNET_YES;
2269 next = plugin->ipv4_queue_head;
2270 while (NULL != (udpw = next))
2273 if (udpw->session == s)
2277 udpw->qc (udpw->qc_cls,
2283 next = plugin->ipv6_queue_head;
2284 while (NULL != (udpw = next))
2287 if (udpw->session == s)
2291 udpw->qc (udpw->qc_cls,
2297 if ( (NULL != s->frag_ctx) &&
2298 (NULL != s->frag_ctx->cont) )
2300 /* The 'frag_ctx' itself will be freed in #free_session() a bit
2301 later, as it might be in use right now */
2302 LOG (GNUNET_ERROR_TYPE_DEBUG,
2303 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
2304 GNUNET_i2s (&s->target));
2305 s->frag_ctx->cont (s->frag_ctx->cont_cls,
2308 s->frag_ctx->payload_size,
2309 s->frag_ctx->on_wire_size);
2311 notify_session_monitor (s->plugin,
2313 GNUNET_TRANSPORT_SS_DONE);
2314 plugin->env->session_end (plugin->env->cls,
2317 GNUNET_STATISTICS_set (plugin->env->stats,
2318 "# UDP sessions active",
2319 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2328 * Handle an ACK message.
2330 * @param plugin the UDP plugin
2331 * @param msg the (presumed) UDP ACK message
2332 * @param udp_addr sender address
2333 * @param udp_addr_len number of bytes in @a udp_addr
2336 read_process_ack (struct Plugin *plugin,
2337 const struct GNUNET_MessageHeader *msg,
2338 const union UdpAddress *udp_addr,
2339 socklen_t udp_addr_len)
2341 const struct GNUNET_MessageHeader *ack;
2342 const struct UDP_ACK_Message *udp_ack;
2343 struct GNUNET_HELLO_Address *address;
2344 struct GNUNET_ATS_Session *s;
2345 struct GNUNET_TIME_Relative flow_delay;
2347 if (ntohs (msg->size)
2348 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2350 GNUNET_break_op (0);
2353 udp_ack = (const struct UDP_ACK_Message *) msg;
2354 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2355 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2360 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2364 GNUNET_HELLO_ADDRESS_INFO_NONE);
2365 s = udp_plugin_lookup_session (plugin,
2369 LOG (GNUNET_ERROR_TYPE_WARNING,
2370 "UDP session of address %s for ACK not found\n",
2371 udp_address_to_string (plugin,
2373 address->address_length));
2374 GNUNET_HELLO_address_free (address);
2377 if (NULL == s->frag_ctx)
2379 LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2380 "Fragmentation context of address %s for ACK (%s) not found\n",
2381 udp_address_to_string (plugin,
2383 address->address_length),
2384 GNUNET_FRAGMENT_print_ack (ack));
2385 GNUNET_HELLO_address_free (address);
2388 GNUNET_HELLO_address_free (address);
2390 if (UINT32_MAX == ntohl (udp_ack->delay))
2392 /* Other peer asked for us to terminate the session */
2393 udp_disconnect_session (plugin,
2397 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2398 if (flow_delay.rel_value_us > GNUNET_CONSTANTS_LATENCY_WARN.rel_value_us)
2399 LOG (GNUNET_ERROR_TYPE_WARNING,
2400 "We received a sending delay of %s for %s\n",
2401 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2403 GNUNET_i2s (&udp_ack->sender));
2405 LOG (GNUNET_ERROR_TYPE_DEBUG,
2406 "We received a sending delay of %s for %s\n",
2407 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2409 GNUNET_i2s (&udp_ack->sender));
2410 /* Flow delay is for the reassembled packet, however, our delay
2411 is per packet, so we need to adjust: */
2412 flow_delay = GNUNET_TIME_relative_divide (flow_delay,
2413 1 + (s->frag_ctx->payload_size /
2415 s->flow_delay_from_other_peer = flow_delay;
2419 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2422 LOG (GNUNET_ERROR_TYPE_DEBUG,
2423 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2424 (unsigned int) ntohs (msg->size),
2425 GNUNET_i2s (&udp_ack->sender),
2426 udp_address_to_string (plugin,
2429 /* Expect more ACKs to arrive */
2433 LOG (GNUNET_ERROR_TYPE_DEBUG,
2434 "Message from %s at %s full ACK'ed\n",
2435 GNUNET_i2s (&udp_ack->sender),
2436 udp_address_to_string (plugin,
2440 /* Remove fragmented message after successful sending */
2441 fragmented_message_done (s->frag_ctx,
2447 * Message tokenizer has broken up an incomming message. Pass it on
2450 * @param cls the `struct Plugin *`
2451 * @param client the `struct GNUNET_ATS_Session *`
2452 * @param hdr the actual message
2453 * @return #GNUNET_OK (always)
2456 process_inbound_tokenized_messages (void *cls,
2458 const struct GNUNET_MessageHeader *hdr)
2460 struct Plugin *plugin = cls;
2461 struct GNUNET_ATS_Session *session = client;
2463 if (GNUNET_YES == session->in_destroy)
2465 reschedule_session_timeout (session);
2466 session->flow_delay_for_other_peer
2467 = plugin->env->receive (plugin->env->cls,
2476 * Destroy a session, plugin is being unloaded.
2478 * @param cls the `struct Plugin`
2479 * @param key hash of public key of target peer
2480 * @param value a `struct PeerSession *` to clean up
2481 * @return #GNUNET_OK (continue to iterate)
2484 disconnect_and_free_it (void *cls,
2485 const struct GNUNET_PeerIdentity *key,
2488 struct Plugin *plugin = cls;
2490 udp_disconnect_session (plugin,
2497 * Disconnect from a remote node. Clean up session if we have one for
2500 * @param cls closure for this call (should be handle to Plugin)
2501 * @param target the peeridentity of the peer to disconnect
2502 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
2505 udp_disconnect (void *cls,
2506 const struct GNUNET_PeerIdentity *target)
2508 struct Plugin *plugin = cls;
2510 LOG (GNUNET_ERROR_TYPE_DEBUG,
2511 "Disconnecting from peer `%s'\n",
2512 GNUNET_i2s (target));
2513 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
2515 &disconnect_and_free_it,
2521 * Session was idle, so disconnect it.
2523 * @param cls the `struct GNUNET_ATS_Session` to time out
2524 * @param tc scheduler context
2527 session_timeout (void *cls,
2528 const struct GNUNET_SCHEDULER_TaskContext *tc)
2530 struct GNUNET_ATS_Session *s = cls;
2531 struct Plugin *plugin = s->plugin;
2532 struct GNUNET_TIME_Relative left;
2534 s->timeout_task = NULL;
2535 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
2536 if (left.rel_value_us > 0)
2538 /* not actually our turn yet, but let's at least update
2539 the monitor, it may think we're about to die ... */
2540 notify_session_monitor (s->plugin,
2542 GNUNET_TRANSPORT_SS_UPDATE);
2543 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
2548 LOG (GNUNET_ERROR_TYPE_DEBUG,
2549 "Session %p was idle for %s, disconnecting\n",
2551 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
2553 /* call session destroy function */
2554 udp_disconnect_session (plugin,
2560 * Allocate a new session for the given endpoint address.
2561 * Note that this function does not inform the service
2562 * of the new session, this is the responsibility of the
2563 * caller (if needed).
2565 * @param cls the `struct Plugin`
2566 * @param address address of the other peer to use
2567 * @param network_type network type the address belongs to
2568 * @return NULL on error, otherwise session handle
2570 static struct GNUNET_ATS_Session *
2571 udp_plugin_create_session (void *cls,
2572 const struct GNUNET_HELLO_Address *address,
2573 enum GNUNET_ATS_Network_Type network_type)
2575 struct Plugin *plugin = cls;
2576 struct GNUNET_ATS_Session *s;
2578 s = GNUNET_new (struct GNUNET_ATS_Session);
2580 s->address = GNUNET_HELLO_address_copy (address);
2581 s->target = address->peer;
2582 s->last_transmit_time = GNUNET_TIME_absolute_get ();
2583 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2585 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
2586 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
2587 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
2588 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
2589 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
2592 s->scope = network_type;
2594 LOG (GNUNET_ERROR_TYPE_DEBUG,
2595 "Creating new session %p for peer `%s' address `%s'\n",
2597 GNUNET_i2s (&address->peer),
2598 udp_address_to_string (plugin,
2600 address->address_length));
2601 GNUNET_assert (GNUNET_OK ==
2602 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
2605 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
2606 GNUNET_STATISTICS_set (plugin->env->stats,
2607 "# UDP sessions active",
2608 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
2610 notify_session_monitor (plugin,
2612 GNUNET_TRANSPORT_SS_INIT);
2618 * Creates a new outbound session the transport service will use to
2619 * send data to the peer.
2621 * @param cls the `struct Plugin *`
2622 * @param address the address
2623 * @return the session or NULL of max connections exceeded
2625 static struct GNUNET_ATS_Session *
2626 udp_plugin_get_session (void *cls,
2627 const struct GNUNET_HELLO_Address *address)
2629 struct Plugin *plugin = cls;
2630 struct GNUNET_ATS_Session *s;
2631 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
2632 const struct IPv4UdpAddress *udp_v4;
2633 const struct IPv6UdpAddress *udp_v6;
2635 if (NULL == address)
2640 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
2641 (address->address_length != sizeof(struct IPv6UdpAddress)) )
2643 GNUNET_break_op (0);
2646 if (NULL != (s = udp_plugin_lookup_session (cls,
2650 /* need to create new session */
2651 if (sizeof (struct IPv4UdpAddress) == address->address_length)
2653 struct sockaddr_in v4;
2655 udp_v4 = (const struct IPv4UdpAddress *) address->address;
2656 memset (&v4, '\0', sizeof (v4));
2657 v4.sin_family = AF_INET;
2658 #if HAVE_SOCKADDR_IN_SIN_LEN
2659 v4.sin_len = sizeof (struct sockaddr_in);
2661 v4.sin_port = udp_v4->u4_port;
2662 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
2663 network_type = plugin->env->get_address_type (plugin->env->cls,
2664 (const struct sockaddr *) &v4,
2667 if (sizeof (struct IPv6UdpAddress) == address->address_length)
2669 struct sockaddr_in6 v6;
2671 udp_v6 = (const struct IPv6UdpAddress *) address->address;
2672 memset (&v6, '\0', sizeof (v6));
2673 v6.sin6_family = AF_INET6;
2674 #if HAVE_SOCKADDR_IN_SIN_LEN
2675 v6.sin6_len = sizeof (struct sockaddr_in6);
2677 v6.sin6_port = udp_v6->u6_port;
2678 v6.sin6_addr = udp_v6->ipv6_addr;
2679 network_type = plugin->env->get_address_type (plugin->env->cls,
2680 (const struct sockaddr *) &v6,
2683 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2684 return udp_plugin_create_session (cls,
2691 * We've received a UDP Message. Process it (pass contents to main service).
2693 * @param plugin plugin context
2694 * @param msg the message
2695 * @param udp_addr sender address
2696 * @param udp_addr_len number of bytes in @a udp_addr
2697 * @param network_type network type the address belongs to
2700 process_udp_message (struct Plugin *plugin,
2701 const struct UDPMessage *msg,
2702 const union UdpAddress *udp_addr,
2703 size_t udp_addr_len,
2704 enum GNUNET_ATS_Network_Type network_type)
2706 struct GNUNET_ATS_Session *s;
2707 struct GNUNET_HELLO_Address *address;
2709 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2710 if (0 != ntohl (msg->reserved))
2715 if (ntohs (msg->header.size)
2716 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2722 address = GNUNET_HELLO_address_allocate (&msg->sender,
2726 GNUNET_HELLO_ADDRESS_INFO_NONE);
2728 (s = udp_plugin_lookup_session (plugin,
2731 s = udp_plugin_create_session (plugin,
2734 plugin->env->session_start (plugin->env->cls,
2738 notify_session_monitor (plugin,
2740 GNUNET_TRANSPORT_SS_UP);
2742 GNUNET_free (address);
2745 GNUNET_SERVER_mst_receive (plugin->mst,
2747 (const char *) &msg[1],
2748 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2752 if ( (0 == s->rc) &&
2753 (GNUNET_YES == s->in_destroy) )
2759 * Process a defragmented message.
2761 * @param cls the `struct DefragContext *`
2762 * @param msg the message
2765 fragment_msg_proc (void *cls,
2766 const struct GNUNET_MessageHeader *msg)
2768 struct DefragContext *dc = cls;
2769 const struct UDPMessage *um;
2771 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2773 GNUNET_break_op (0);
2776 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2778 GNUNET_break_op (0);
2781 um = (const struct UDPMessage *) msg;
2782 dc->sender = um->sender;
2783 dc->have_sender = GNUNET_YES;
2784 process_udp_message (dc->plugin,
2793 * We finished sending an acknowledgement. Update
2796 * @param cls the `struct Plugin`
2797 * @param udpw message queue entry of the ACK
2798 * @param result #GNUNET_OK if the transmission worked,
2799 * #GNUNET_SYSERR if we failed to send the ACK
2802 ack_message_sent (void *cls,
2803 struct UDP_MessageWrapper *udpw,
2806 struct Plugin *plugin = cls;
2808 if (GNUNET_OK == result)
2810 GNUNET_STATISTICS_update (plugin->env->stats,
2811 "# UDP, ACK messages sent",
2817 GNUNET_STATISTICS_update (plugin->env->stats,
2818 "# UDP, ACK transmissions failed",
2826 * Transmit an acknowledgement.
2828 * @param cls the `struct DefragContext *`
2829 * @param id message ID (unused)
2830 * @param msg ack to transmit
2833 ack_proc (void *cls,
2835 const struct GNUNET_MessageHeader *msg)
2837 struct DefragContext *rc = cls;
2838 struct Plugin *plugin = rc->plugin;
2839 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2840 struct UDP_ACK_Message *udp_ack;
2842 struct UDP_MessageWrapper *udpw;
2843 struct GNUNET_ATS_Session *s;
2844 struct GNUNET_HELLO_Address *address;
2846 if (GNUNET_NO == rc->have_sender)
2848 /* tried to defragment but never succeeded, hence will not ACK */
2849 /* This can happen if we just lost msgs */
2850 GNUNET_STATISTICS_update (plugin->env->stats,
2851 "# UDP, fragments discarded without ACK",
2856 address = GNUNET_HELLO_address_allocate (&rc->sender,
2860 GNUNET_HELLO_ADDRESS_INFO_NONE);
2861 s = udp_plugin_lookup_session (plugin,
2863 GNUNET_HELLO_address_free (address);
2866 LOG (GNUNET_ERROR_TYPE_ERROR,
2867 "Trying to transmit ACK to peer `%s' but no session found!\n",
2868 udp_address_to_string (plugin,
2871 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2872 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2874 GNUNET_STATISTICS_update (plugin->env->stats,
2875 "# UDP, ACK transmissions failed",
2880 if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us ==
2881 s->flow_delay_for_other_peer.rel_value_us)
2883 else if (s->flow_delay_for_other_peer.rel_value_us < UINT32_MAX)
2884 delay = s->flow_delay_for_other_peer.rel_value_us;
2886 delay = UINT32_MAX - 1; /* largest value we can communicate */
2887 LOG (GNUNET_ERROR_TYPE_DEBUG,
2888 "Sending ACK to `%s' including delay of %s\n",
2889 udp_address_to_string (plugin,
2892 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2894 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2895 udpw->msg_size = msize;
2896 udpw->payload_size = 0;
2898 udpw->start_time = GNUNET_TIME_absolute_get ();
2899 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2900 udpw->msg_buf = (char *) &udpw[1];
2901 udpw->qc = &ack_message_sent;
2902 udpw->qc_cls = plugin;
2903 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2904 udp_ack->header.size = htons ((uint16_t) msize);
2905 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2906 udp_ack->delay = htonl (delay);
2907 udp_ack->sender = *plugin->env->my_identity;
2908 memcpy (&udp_ack[1],
2913 notify_session_monitor (plugin,
2915 GNUNET_TRANSPORT_SS_UPDATE);
2916 if (s->address->address_length == sizeof (struct IPv4UdpAddress))
2917 schedule_select_v4 (plugin);
2919 schedule_select_v6 (plugin);
2924 * We received a fragment, process it.
2926 * @param plugin our plugin
2927 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2928 * @param udp_addr sender address
2929 * @param udp_addr_len number of bytes in @a udp_addr
2930 * @param network_type network type the address belongs to
2933 read_process_fragment (struct Plugin *plugin,
2934 const struct GNUNET_MessageHeader *msg,
2935 const union UdpAddress *udp_addr,
2936 size_t udp_addr_len,
2937 enum GNUNET_ATS_Network_Type network_type)
2939 struct DefragContext *d_ctx;
2940 struct GNUNET_TIME_Absolute now;
2941 struct FindReceiveContext frc;
2944 frc.udp_addr = udp_addr;
2945 frc.udp_addr_len = udp_addr_len;
2947 /* Lookup existing receive context for this address */
2948 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2949 &find_receive_context,
2951 now = GNUNET_TIME_absolute_get ();
2956 /* Create a new defragmentation context */
2957 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2961 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2962 d_ctx->udp_addr_len = udp_addr_len;
2963 d_ctx->network_type = network_type;
2964 d_ctx->plugin = plugin;
2965 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2967 UDP_MAX_MESSAGES_IN_DEFRAG,
2971 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2973 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2974 LOG (GNUNET_ERROR_TYPE_DEBUG,
2975 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2976 (unsigned int) ntohs (msg->size),
2977 udp_address_to_string (plugin,
2983 LOG (GNUNET_ERROR_TYPE_DEBUG,
2984 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2985 (unsigned int) ntohs (msg->size),
2986 udp_address_to_string (plugin,
2992 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag,
2995 /* keep this 'rc' from expiring */
2996 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2998 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
3000 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
3001 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
3003 /* remove 'rc' that was inactive the longest */
3004 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
3005 GNUNET_assert (NULL != d_ctx);
3006 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3007 GNUNET_free (d_ctx);
3008 GNUNET_STATISTICS_update (plugin->env->stats,
3009 "# UDP, Defragmentations aborted",
3017 * Read and process a message from the given socket.
3019 * @param plugin the overall plugin
3020 * @param rsock socket to read from
3023 udp_select_read (struct Plugin *plugin,
3024 struct GNUNET_NETWORK_Handle *rsock)
3027 struct sockaddr_storage addr;
3028 char buf[65536] GNUNET_ALIGN;
3030 const struct GNUNET_MessageHeader *msg;
3031 struct IPv4UdpAddress v4;
3032 struct IPv6UdpAddress v6;
3033 const struct sockaddr *sa;
3034 const struct sockaddr_in *sa4;
3035 const struct sockaddr_in6 *sa6;
3036 const union UdpAddress *int_addr;
3037 size_t int_addr_len;
3038 enum GNUNET_ATS_Network_Type network_type;
3040 fromlen = sizeof (addr);
3044 size = GNUNET_NETWORK_socket_recvfrom (rsock,
3047 (struct sockaddr *) &addr,
3049 sa = (const struct sockaddr *) &addr;
3051 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
3052 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
3053 * on this socket has failed.
3055 * WSAECONNRESET - The virtual circuit was reset by the remote side
3056 * executing a hard or abortive close. The application should close
3057 * the socket; it is no longer usable. On a UDP-datagram socket this
3058 * error indicates a previous send operation resulted in an ICMP Port
3059 * Unreachable message.
3061 if ( (-1 == size) &&
3062 (ECONNRESET == errno) )
3067 LOG (GNUNET_ERROR_TYPE_DEBUG,
3068 "UDP failed to receive data: %s\n",
3070 /* Connection failure or something. Not a protocol violation. */
3074 /* Check if this is a STUN packet */
3075 if (GNUNET_NAT_is_valid_stun_packet (plugin->nat,
3078 return; /* was STUN, do not process further */
3080 if (size < sizeof(struct GNUNET_MessageHeader))
3082 LOG (GNUNET_ERROR_TYPE_WARNING,
3083 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
3084 (unsigned int ) size,
3087 /* _MAY_ be a connection failure (got partial message) */
3088 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
3089 GNUNET_break_op (0);
3093 msg = (const struct GNUNET_MessageHeader *) buf;
3094 LOG (GNUNET_ERROR_TYPE_DEBUG,
3095 "UDP received %u-byte message from `%s' type %u\n",
3096 (unsigned int) size,
3100 if (size != ntohs (msg->size))
3102 LOG (GNUNET_ERROR_TYPE_WARNING,
3103 "UDP malformed message header from %s\n",
3104 (unsigned int) size,
3107 GNUNET_break_op (0);
3110 GNUNET_STATISTICS_update (plugin->env->stats,
3111 "# UDP, total bytes received",
3114 network_type = plugin->env->get_address_type (plugin->env->cls,
3117 switch (sa->sa_family)
3120 sa4 = (const struct sockaddr_in *) &addr;
3122 v4.ipv4_addr = sa4->sin_addr.s_addr;
3123 v4.u4_port = sa4->sin_port;
3124 int_addr = (union UdpAddress *) &v4;
3125 int_addr_len = sizeof (v4);
3128 sa6 = (const struct sockaddr_in6 *) &addr;
3130 v6.ipv6_addr = sa6->sin6_addr;
3131 v6.u6_port = sa6->sin6_port;
3132 int_addr = (union UdpAddress *) &v6;
3133 int_addr_len = sizeof (v6);
3140 switch (ntohs (msg->type))
3142 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
3143 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
3144 udp_broadcast_receive (plugin,
3151 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
3152 if (ntohs (msg->size) < sizeof(struct UDPMessage))
3157 process_udp_message (plugin,
3158 (const struct UDPMessage *) msg,
3163 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
3164 read_process_ack (plugin,
3169 case GNUNET_MESSAGE_TYPE_FRAGMENT:
3170 read_process_fragment (plugin,
3184 * Removes messages from the transmission queue that have
3185 * timed out, and then selects a message that should be
3188 * @param plugin the UDP plugin
3189 * @param sock which socket should we process the queue for (v4 or v6)
3190 * @return message selected for transmission, or NULL for none
3192 static struct UDP_MessageWrapper *
3193 remove_timeout_messages_and_select (struct Plugin *plugin,
3194 struct GNUNET_NETWORK_Handle *sock)
3196 struct UDP_MessageWrapper *udpw;
3197 struct GNUNET_TIME_Relative remaining;
3198 struct GNUNET_ATS_Session *session;
3201 removed = GNUNET_NO;
3202 udpw = (sock == plugin->sockv4)
3203 ? plugin->ipv4_queue_head
3204 : plugin->ipv6_queue_head;
3205 while (NULL != udpw)
3207 session = udpw->session;
3208 /* Find messages with timeout */
3209 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
3210 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
3212 /* Message timed out */
3213 removed = GNUNET_YES;
3216 udpw->qc (udpw->qc_cls,
3221 if (sock == plugin->sockv4)
3223 udpw = plugin->ipv4_queue_head;
3225 else if (sock == plugin->sockv6)
3227 udpw = plugin->ipv6_queue_head;
3231 GNUNET_break (0); /* should never happen */
3234 GNUNET_STATISTICS_update (plugin->env->stats,
3235 "# messages discarded due to timeout",
3241 /* Message did not time out, check transmission time */
3242 remaining = GNUNET_TIME_absolute_get_remaining (udpw->transmission_time);
3243 if (0 == remaining.rel_value_us)
3245 /* this message is not delayed */
3246 LOG (GNUNET_ERROR_TYPE_DEBUG,
3247 "Message for peer `%s' (%u bytes) is not delayed \n",
3248 GNUNET_i2s (&udpw->session->target),
3249 udpw->payload_size);
3250 break; /* Found message to send, break */
3254 /* Message is delayed, try next */
3255 LOG (GNUNET_ERROR_TYPE_DEBUG,
3256 "Message for peer `%s' (%u bytes) is delayed for %s\n",
3257 GNUNET_i2s (&udpw->session->target),
3259 GNUNET_STRINGS_relative_time_to_string (remaining,
3265 if (GNUNET_YES == removed)
3266 notify_session_monitor (session->plugin,
3268 GNUNET_TRANSPORT_SS_UPDATE);
3274 * We failed to transmit a message via UDP. Generate
3275 * a descriptive error message.
3277 * @param plugin our plugin
3278 * @param sa target address we were trying to reach
3279 * @param slen number of bytes in @a sa
3280 * @param error the errno value returned from the sendto() call
3283 analyze_send_error (struct Plugin *plugin,
3284 const struct sockaddr *sa,
3288 enum GNUNET_ATS_Network_Type type;
3290 type = plugin->env->get_address_type (plugin->env->cls,
3293 if ( ( (GNUNET_ATS_NET_LAN == type) ||
3294 (GNUNET_ATS_NET_WAN == type) ) &&
3295 ( (ENETUNREACH == errno) ||
3296 (ENETDOWN == errno) ) )
3298 if (slen == sizeof (struct sockaddr_in))
3300 /* IPv4: "Network unreachable" or "Network down"
3302 * This indicates we do not have connectivity
3304 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3305 _("UDP could not transmit message to `%s': "
3306 "Network seems down, please check your network configuration\n"),
3310 if (slen == sizeof (struct sockaddr_in6))
3312 /* IPv6: "Network unreachable" or "Network down"
3314 * This indicates that this system is IPv6 enabled, but does not
3315 * have a valid global IPv6 address assigned or we do not have
3318 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
3319 _("UDP could not transmit IPv6 message! "
3320 "Please check your network configuration and disable IPv6 if your "
3321 "connection does not have a global IPv6 address\n"));
3326 LOG (GNUNET_ERROR_TYPE_WARNING,
3327 "UDP could not transmit message to `%s': `%s'\n",
3336 * It is time to try to transmit a UDP message. Select one
3339 * @param plugin the plugin
3340 * @param sock which socket (v4/v6) to send on
3343 udp_select_send (struct Plugin *plugin,
3344 struct GNUNET_NETWORK_Handle *sock)
3348 const struct sockaddr *a;
3349 const struct IPv4UdpAddress *u4;
3350 struct sockaddr_in a4;
3351 const struct IPv6UdpAddress *u6;
3352 struct sockaddr_in6 a6;
3353 struct UDP_MessageWrapper *udpw;
3355 /* Find message(s) to send */
3356 while (NULL != (udpw = remove_timeout_messages_and_select (plugin,
3359 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3361 u4 = udpw->session->address->address;
3365 a4.sin_family = AF_INET;
3366 #if HAVE_SOCKADDR_IN_SIN_LEN
3367 a4.sin_len = sizeof (a4);
3369 a4.sin_port = u4->u4_port;
3370 a4.sin_addr.s_addr = u4->ipv4_addr;
3371 a = (const struct sockaddr *) &a4;
3374 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3376 u6 = udpw->session->address->address;
3380 a6.sin6_family = AF_INET6;
3381 #if HAVE_SOCKADDR_IN_SIN_LEN
3382 a6.sin6_len = sizeof (a6);
3384 a6.sin6_port = u6->u6_port;
3385 a6.sin6_addr = u6->ipv6_addr;
3386 a = (const struct sockaddr *) &a6;
3394 udpw->qc (udpw->qc_cls,
3397 notify_session_monitor (plugin,
3399 GNUNET_TRANSPORT_SS_UPDATE);
3403 sent = GNUNET_NETWORK_socket_sendto (sock,
3408 udpw->session->last_transmit_time
3409 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
3410 udpw->session->last_transmit_time);
3413 if (GNUNET_SYSERR == sent)
3416 analyze_send_error (plugin,
3420 udpw->qc (udpw->qc_cls,
3423 GNUNET_STATISTICS_update (plugin->env->stats,
3424 "# UDP, total, bytes, sent, failure",
3427 GNUNET_STATISTICS_update (plugin->env->stats,
3428 "# UDP, total, messages, sent, failure",
3435 LOG (GNUNET_ERROR_TYPE_DEBUG,
3436 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3437 (unsigned int) (udpw->msg_size),
3438 GNUNET_i2s (&udpw->session->target),
3442 (sent < 0) ? STRERROR (errno) : "ok");
3443 GNUNET_STATISTICS_update (plugin->env->stats,
3444 "# UDP, total, bytes, sent, success",
3447 GNUNET_STATISTICS_update (plugin->env->stats,
3448 "# UDP, total, messages, sent, success",
3451 if (NULL != udpw->frag_ctx)
3452 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3453 udpw->qc (udpw->qc_cls,
3457 notify_session_monitor (plugin,
3459 GNUNET_TRANSPORT_SS_UPDATE);
3465 /* ***************** Event loop (part 2) *************** */
3469 * We have been notified that our readset has something to read. We don't
3470 * know which socket needs to be read, so we have to check each one
3471 * Then reschedule this function to be called again once more is available.
3473 * @param cls the plugin handle
3474 * @param tc the scheduling context
3477 udp_plugin_select_v4 (void *cls,
3478 const struct GNUNET_SCHEDULER_TaskContext *tc)
3480 struct Plugin *plugin = cls;
3482 plugin->select_task_v4 = NULL;
3483 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3485 if (NULL == plugin->sockv4)
3487 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3488 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3490 udp_select_read (plugin,
3492 udp_select_send (plugin,
3494 schedule_select_v4 (plugin);
3499 * We have been notified that our readset has something to read. We don't
3500 * know which socket needs to be read, so we have to check each one
3501 * Then reschedule this function to be called again once more is available.
3503 * @param cls the plugin handle
3504 * @param tc the scheduling context
3507 udp_plugin_select_v6 (void *cls,
3508 const struct GNUNET_SCHEDULER_TaskContext *tc)
3510 struct Plugin *plugin = cls;
3512 plugin->select_task_v6 = NULL;
3513 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3515 if (NULL == plugin->sockv6)
3517 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
3518 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
3520 udp_select_read (plugin,
3523 udp_select_send (plugin,
3525 schedule_select_v6 (plugin);
3529 /* ******************* Initialization *************** */
3533 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3535 * @param plugin the plugin to initialize
3536 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3537 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3538 * @return number of sockets that were successfully bound
3541 setup_sockets (struct Plugin *plugin,
3542 const struct sockaddr_in6 *bind_v6,
3543 const struct sockaddr_in *bind_v4)
3546 int sockets_created = 0;
3547 struct sockaddr_in6 server_addrv6;
3548 struct sockaddr_in server_addrv4;
3549 const struct sockaddr *server_addr;
3550 const struct sockaddr *addrs[2];
3551 socklen_t addrlens[2];
3555 /* Create IPv6 socket */
3557 if (GNUNET_YES == plugin->enable_ipv6)
3559 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
3562 if (NULL == plugin->sockv6)
3564 LOG (GNUNET_ERROR_TYPE_INFO,
3565 _("Disabling IPv6 since it is not supported on this system!\n"));
3566 plugin->enable_ipv6 = GNUNET_NO;
3570 memset (&server_addrv6,
3572 sizeof(struct sockaddr_in6));
3573 #if HAVE_SOCKADDR_IN_SIN_LEN
3574 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3576 server_addrv6.sin6_family = AF_INET6;
3577 if (NULL != bind_v6)
3578 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3580 server_addrv6.sin6_addr = in6addr_any;
3582 if (0 == plugin->port) /* autodetect */
3583 server_addrv6.sin6_port
3584 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3588 server_addrv6.sin6_port = htons (plugin->port);
3589 addrlen = sizeof (struct sockaddr_in6);
3590 server_addr = (const struct sockaddr *) &server_addrv6;
3595 LOG(GNUNET_ERROR_TYPE_DEBUG,
3596 "Binding to IPv6 `%s'\n",
3597 GNUNET_a2s (server_addr,
3601 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3606 if (0 != plugin->port)
3608 tries = 10; /* fail immediately */
3609 break; /* bind failed on specific port */
3612 server_addrv6.sin6_port
3613 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3620 GNUNET_NETWORK_socket_close (plugin->sockv6);
3621 plugin->enable_ipv6 = GNUNET_NO;
3622 plugin->sockv6 = NULL;
3626 plugin->port = ntohs (server_addrv6.sin6_port);
3628 if (NULL != plugin->sockv6)
3630 LOG (GNUNET_ERROR_TYPE_DEBUG,
3631 "IPv6 UDP socket created listinging at %s\n",
3632 GNUNET_a2s (server_addr,
3634 addrs[sockets_created] = server_addr;
3635 addrlens[sockets_created] = addrlen;
3640 LOG (GNUNET_ERROR_TYPE_WARNING,
3641 _("Failed to bind UDP socket to %s: %s\n"),
3642 GNUNET_a2s (server_addr,
3649 /* Create IPv4 socket */
3651 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
3654 if (NULL == plugin->sockv4)
3656 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3658 LOG (GNUNET_ERROR_TYPE_INFO,
3659 _("Disabling IPv4 since it is not supported on this system!\n"));
3660 plugin->enable_ipv4 = GNUNET_NO;
3664 memset (&server_addrv4,
3666 sizeof(struct sockaddr_in));
3667 #if HAVE_SOCKADDR_IN_SIN_LEN
3668 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3670 server_addrv4.sin_family = AF_INET;
3671 if (NULL != bind_v4)
3672 server_addrv4.sin_addr = bind_v4->sin_addr;
3674 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3676 if (0 == plugin->port)
3678 server_addrv4.sin_port
3679 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3683 server_addrv4.sin_port = htons (plugin->port);
3685 addrlen = sizeof (struct sockaddr_in);
3686 server_addr = (const struct sockaddr *) &server_addrv4;
3691 LOG (GNUNET_ERROR_TYPE_DEBUG,
3692 "Binding to IPv4 `%s'\n",
3693 GNUNET_a2s (server_addr,
3698 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3703 if (0 != plugin->port)
3705 tries = 10; /* fail */
3706 break; /* bind failed on specific port */
3710 server_addrv4.sin_port
3711 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3718 GNUNET_NETWORK_socket_close (plugin->sockv4);
3719 plugin->enable_ipv4 = GNUNET_NO;
3720 plugin->sockv4 = NULL;
3724 plugin->port = ntohs (server_addrv4.sin_port);
3727 if (NULL != plugin->sockv4)
3729 LOG (GNUNET_ERROR_TYPE_DEBUG,
3730 "IPv4 socket created on port %s\n",
3731 GNUNET_a2s (server_addr,
3733 addrs[sockets_created] = server_addr;
3734 addrlens[sockets_created] = addrlen;
3739 LOG (GNUNET_ERROR_TYPE_ERROR,
3740 _("Failed to bind UDP socket to %s: %s\n"),
3741 GNUNET_a2s (server_addr,
3747 if (0 == sockets_created)
3749 LOG (GNUNET_ERROR_TYPE_WARNING,
3750 _("Failed to open UDP sockets\n"));
3751 return 0; /* No sockets created, return */
3753 schedule_select_v4 (plugin);
3754 schedule_select_v6 (plugin);
3755 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3761 &udp_nat_port_map_callback,
3765 return sockets_created;
3770 * The exported method. Makes the core api available via a global and
3771 * returns the udp transport API.
3773 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3774 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3777 libgnunet_plugin_transport_udp_init (void *cls)
3779 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3780 struct GNUNET_TRANSPORT_PluginFunctions *api;
3782 unsigned long long port;
3783 unsigned long long aport;
3784 unsigned long long udp_max_bps;
3785 unsigned long long enable_v6;
3786 unsigned long long enable_broadcasting;
3787 unsigned long long enable_broadcasting_recv;
3788 char *bind4_address;
3789 char *bind6_address;
3790 struct GNUNET_TIME_Relative interval;
3791 struct sockaddr_in server_addrv4;
3792 struct sockaddr_in6 server_addrv6;
3797 if (NULL == env->receive)
3799 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3800 initialze the plugin or the API */
3801 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3803 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3804 api->address_to_string = &udp_address_to_string;
3805 api->string_to_address = &udp_string_to_address;
3809 /* Get port number: port == 0 : autodetect a port,
3810 * > 0 : use this port, not given : 2086 default */
3812 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3819 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3822 _("must be in [0,65535]"));
3826 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3833 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3836 _("must be in [0,65535]"));
3841 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3844 enable_v6 = GNUNET_NO;
3846 enable_v6 = GNUNET_YES;
3848 have_bind4 = GNUNET_NO;
3849 memset (&server_addrv4,
3851 sizeof (server_addrv4));
3853 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3858 LOG (GNUNET_ERROR_TYPE_DEBUG,
3859 "Binding UDP plugin to specific address: `%s'\n",
3861 if (1 != inet_pton (AF_INET,
3863 &server_addrv4.sin_addr))
3865 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3868 _("must be valid IPv4 address"));
3869 GNUNET_free (bind4_address);
3872 have_bind4 = GNUNET_YES;
3874 GNUNET_free_non_null (bind4_address);
3875 have_bind6 = GNUNET_NO;
3876 memset (&server_addrv6,
3878 sizeof (server_addrv6));
3880 GNUNET_CONFIGURATION_get_value_string (env->cfg,
3885 LOG (GNUNET_ERROR_TYPE_DEBUG,
3886 "Binding udp plugin to specific address: `%s'\n",
3888 if (1 != inet_pton (AF_INET6,
3890 &server_addrv6.sin6_addr))
3892 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
3895 _("must be valid IPv6 address"));
3896 GNUNET_free (bind6_address);
3899 have_bind6 = GNUNET_YES;
3901 GNUNET_free_non_null (bind6_address);
3903 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3906 if (enable_broadcasting == GNUNET_SYSERR)
3907 enable_broadcasting = GNUNET_NO;
3909 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3911 "BROADCAST_RECEIVE");
3912 if (enable_broadcasting_recv == GNUNET_SYSERR)
3913 enable_broadcasting_recv = GNUNET_YES;
3915 if (GNUNET_SYSERR ==
3916 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3918 "BROADCAST_INTERVAL",
3921 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
3925 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3930 /* 50 MB/s == infinity for practical purposes */
3931 udp_max_bps = 1024 * 1024 * 50;
3934 p = GNUNET_new (struct Plugin);
3937 p->broadcast_interval = interval;
3938 p->enable_ipv6 = enable_v6;
3939 p->enable_ipv4 = GNUNET_YES; /* default */
3940 p->enable_broadcasting = enable_broadcasting;
3941 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3943 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
3945 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3946 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3948 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3951 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3953 res = setup_sockets (p,
3954 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3955 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3957 ( (NULL == p->sockv4) &&
3958 (NULL == p->sockv6) ) )
3960 LOG (GNUNET_ERROR_TYPE_ERROR,
3961 _("Failed to create UDP network sockets\n"));
3962 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3963 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3964 GNUNET_SERVER_mst_destroy (p->mst);
3969 /* Setup broadcasting and receiving beacons */
3974 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3976 api->disconnect_session = &udp_disconnect_session;
3977 api->query_keepalive_factor = &udp_query_keepalive_factor;
3978 api->disconnect_peer = &udp_disconnect;
3979 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3980 api->address_to_string = &udp_address_to_string;
3981 api->string_to_address = &udp_string_to_address;
3982 api->check_address = &udp_plugin_check_address;
3983 api->get_session = &udp_plugin_get_session;
3984 api->send = &udp_plugin_send;
3985 api->get_network = &udp_plugin_get_network;
3986 api->get_network_for_address = &udp_plugin_get_network_for_address;
3987 api->update_session_timeout = &udp_plugin_update_session_timeout;
3988 api->setup_monitor = &udp_plugin_setup_monitor;
3994 * Function called on each entry in the defragmentation heap to
3998 * @param node node in the heap (to be removed)
3999 * @param element a `struct DefragContext` to be cleaned up
4000 * @param cost unused
4001 * @return #GNUNET_YES
4004 heap_cleanup_iterator (void *cls,
4005 struct GNUNET_CONTAINER_HeapNode *node,
4007 GNUNET_CONTAINER_HeapCostType cost)
4009 struct DefragContext *d_ctx = element;
4011 GNUNET_CONTAINER_heap_remove_node (node);
4012 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
4013 GNUNET_free (d_ctx);
4019 * The exported method. Makes the core api available via a global and
4020 * returns the udp transport API.
4022 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
4026 libgnunet_plugin_transport_udp_done (void *cls)
4028 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
4029 struct Plugin *plugin = api->cls;
4030 struct PrettyPrinterContext *cur;
4031 struct UDP_MessageWrapper *udpw;
4038 stop_broadcast (plugin);
4039 if (NULL != plugin->select_task_v4)
4041 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
4042 plugin->select_task_v4 = NULL;
4044 if (NULL != plugin->select_task_v6)
4046 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
4047 plugin->select_task_v6 = NULL;
4049 if (NULL != plugin->sockv4)
4051 GNUNET_break (GNUNET_OK ==
4052 GNUNET_NETWORK_socket_close (plugin->sockv4));
4053 plugin->sockv4 = NULL;
4055 if (NULL != plugin->sockv6)
4057 GNUNET_break (GNUNET_OK ==
4058 GNUNET_NETWORK_socket_close (plugin->sockv6));
4059 plugin->sockv6 = NULL;
4061 if (NULL != plugin->nat)
4063 GNUNET_NAT_unregister (plugin->nat);
4066 if (NULL != plugin->defrag_ctxs)
4068 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
4069 &heap_cleanup_iterator,
4071 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
4072 plugin->defrag_ctxs = NULL;
4074 if (NULL != plugin->mst)
4076 GNUNET_SERVER_mst_destroy (plugin->mst);
4079 while (NULL != (udpw = plugin->ipv4_queue_head))
4083 udpw->qc (udpw->qc_cls,
4088 while (NULL != (udpw = plugin->ipv6_queue_head))
4092 udpw->qc (udpw->qc_cls,
4097 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
4098 &disconnect_and_free_it,
4100 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
4102 while (NULL != (cur = plugin->ppc_dll_head))
4105 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
4106 plugin->ppc_dll_tail,
4108 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
4109 if (NULL != cur->timeout_task)
4111 GNUNET_SCHEDULER_cancel (cur->timeout_task);
4112 cur->timeout_task = NULL;
4116 GNUNET_free (plugin);
4121 /* end of plugin_transport_udp.c */