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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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__)
45 #define UDP_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
48 * Number of messages we can defragment in parallel. We only really
49 * defragment 1 message at a time, but if messages get re-ordered, we
50 * may want to keep knowledge about the previous message to avoid
51 * discarding the current message in favor of a single fragment of a
52 * previous message. 3 should be good since we don't expect massive
53 * message reorderings with UDP.
55 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
58 * We keep a defragmentation queue per sender address. How many
59 * sender addresses do we support at the same time? Memory consumption
60 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
61 * value. (So 128 corresponds to 12 MB and should suffice for
62 * connecting to roughly 128 peers via UDP).
64 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
68 * Closure for #append_port().
70 struct PrettyPrinterContext
75 struct PrettyPrinterContext *next;
80 struct PrettyPrinterContext *prev;
85 struct Plugin *plugin;
90 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
93 * Function to call with the result.
95 GNUNET_TRANSPORT_AddressStringCallback asc;
105 struct GNUNET_SCHEDULER_Task *timeout_task;
108 * Is this an IPv6 address?
118 * Port to add after the IP address.
126 * Session with another peer.
131 * Which peer is this session for?
133 struct GNUNET_PeerIdentity target;
136 * Plugin this session belongs to.
138 struct Plugin *plugin;
141 * Context for dealing with fragments.
143 struct UDP_FragmentationContext *frag_ctx;
146 * Desired delay for next sending we send to other peer
148 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
151 * Desired delay for next sending we received from other peer
153 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
156 * Session timeout task
158 struct GNUNET_SCHEDULER_Task *timeout_task;
161 * When does this session time out?
163 struct GNUNET_TIME_Absolute timeout;
166 * expected delay for ACKs
168 struct GNUNET_TIME_Relative last_expected_ack_delay;
171 * desired delay between UDP messages
173 struct GNUNET_TIME_Relative last_expected_msg_delay;
178 struct GNUNET_HELLO_Address *address;
181 * Number of bytes waiting for transmission to this peer.
183 unsigned long long bytes_in_queue;
186 * Number of messages waiting for transmission to this peer.
188 unsigned int msgs_in_queue;
191 * Reference counter to indicate that this session is
192 * currently being used and must not be destroyed;
193 * setting @e in_destroy will destroy it as soon as
199 * Network type of the address.
201 enum GNUNET_ATS_Network_Type scope;
204 * Is this session about to be destroyed (sometimes we cannot
205 * destroy a session immediately as below us on the stack
206 * there might be code that still uses it; in this case,
207 * @e rc is non-zero).
214 * Closure for #process_inbound_tokenized_messages().
216 struct SourceInformation
221 struct GNUNET_PeerIdentity sender;
224 * Associated session.
226 struct Session *session;
231 * Closure for #find_receive_context().
233 struct FindReceiveContext
236 * Where to store the result.
238 struct DefragContext *rc;
241 * Session associated with this context.
243 struct Session *session;
248 const union UdpAddress *udp_addr;
251 * Number of bytes in @e udp_addr.
258 * Data structure to track defragmentation contexts based
259 * on the source of the UDP traffic.
265 * Defragmentation context.
267 struct GNUNET_DEFRAGMENT_Context *defrag;
270 * Reference to master plugin struct.
272 struct Plugin *plugin;
275 * Node in the defrag heap.
277 struct GNUNET_CONTAINER_HeapNode *hnode;
280 * Who's message(s) are we defragmenting here?
281 * Only initialized once we succeeded and
282 * @e have_sender is set.
284 struct GNUNET_PeerIdentity sender;
287 * Source address this receive context is for (allocated at the
288 * end of the struct).
290 const union UdpAddress *udp_addr;
293 * Length of @e udp_addr.
298 * Network type the address belongs to.
300 enum GNUNET_ATS_Network_Type network_type;
303 * Has the @e sender field been initialized yet?
310 * Context to send fragmented messages
312 struct UDP_FragmentationContext
315 * Next in linked list
317 struct UDP_FragmentationContext *next;
320 * Previous in linked list
322 struct UDP_FragmentationContext *prev;
327 struct Plugin *plugin;
330 * Handle for GNUNET_FRAGMENT context
332 struct GNUNET_FRAGMENT_Context *frag;
335 * The session this fragmentation context belongs to
337 struct Session *session;
340 * Function to call upon completion of the transmission.
342 GNUNET_TRANSPORT_TransmitContinuation cont;
345 * Closure for @e cont.
352 struct GNUNET_TIME_Absolute timeout;
355 * Payload size of original unfragmented message
360 * Bytes used to send all fragments on wire including UDP overhead
367 unsigned int fragments_used;
373 * Message types included in a `struct UDP_MessageWrapper`
378 * Uninitialized (error)
383 * Fragment of a message.
385 UMT_MSG_FRAGMENTED = 1,
390 UMT_MSG_FRAGMENTED_COMPLETE = 2,
393 * Unfragmented message.
395 UMT_MSG_UNFRAGMENTED = 3,
398 * Receipt confirmation.
406 * Information we track for each message in the queue.
408 struct UDP_MessageWrapper
411 * Session this message belongs to
413 struct Session *session;
419 struct UDP_MessageWrapper *prev;
425 struct UDP_MessageWrapper *next;
428 * Message with size msg_size including UDP specific overhead
433 * Function to call upon completion of the transmission.
435 GNUNET_TRANSPORT_TransmitContinuation cont;
438 * Closure for @e cont.
443 * Fragmentation context
444 * frag_ctx == NULL if transport <= MTU
445 * frag_ctx != NULL if transport > MTU
447 struct UDP_FragmentationContext *frag_ctx;
452 struct GNUNET_TIME_Absolute timeout;
455 * Size of UDP message to send including UDP specific overhead
460 * Payload size of original message
467 enum UDP_MessageType msg_type;
473 * UDP ACK Message-Packet header.
475 struct UDP_ACK_Message
480 struct GNUNET_MessageHeader header;
483 * Desired delay for flow control
488 * What is the identity of the sender
490 struct GNUNET_PeerIdentity sender;
496 * If a session monitor is attached, notify it about the new
499 * @param plugin our plugin
500 * @param session session that changed state
501 * @param state new state of the session
504 notify_session_monitor (struct Plugin *plugin,
505 struct Session *session,
506 enum GNUNET_TRANSPORT_SessionState state)
508 struct GNUNET_TRANSPORT_SessionInfo info;
510 if (NULL == plugin->sic)
512 if (GNUNET_YES == session->in_destroy)
513 return; /* already destroyed, just RC>0 left-over actions */
514 memset (&info, 0, sizeof (info));
516 info.is_inbound = GNUNET_SYSERR; /* hard to say */
517 info.num_msg_pending = session->msgs_in_queue;
518 info.num_bytes_pending = session->bytes_in_queue;
519 /* info.receive_delay remains zero as this is not supported by UDP
520 (cannot selectively not receive from 'some' peer while continuing
521 to receive from others) */
522 info.session_timeout = session->timeout;
523 info.address = session->address;
524 plugin->sic (plugin->sic_cls,
531 * We have been notified that our readset has something to read. We don't
532 * know which socket needs to be read, so we have to check each one
533 * Then reschedule this function to be called again once more is available.
535 * @param cls the plugin handle
536 * @param tc the scheduling context (for rescheduling this function again)
539 udp_plugin_select (void *cls,
540 const struct GNUNET_SCHEDULER_TaskContext *tc);
544 * We have been notified that our readset has something to read. We don't
545 * know which socket needs to be read, so we have to check each one
546 * Then reschedule this function to be called again once more is available.
548 * @param cls the plugin handle
549 * @param tc the scheduling context (for rescheduling this function again)
552 udp_plugin_select_v6 (void *cls,
553 const struct GNUNET_SCHEDULER_TaskContext *tc);
557 * (re)schedule select tasks for this plugin.
559 * @param plugin plugin to reschedule
562 schedule_select (struct Plugin *plugin)
564 struct GNUNET_TIME_Relative min_delay;
565 struct UDP_MessageWrapper *udpw;
567 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
569 /* Find a message ready to send:
570 * Flow delay from other peer is expired or not set (0) */
571 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
572 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
573 min_delay = GNUNET_TIME_relative_min (min_delay,
574 GNUNET_TIME_absolute_get_remaining (
575 udpw->session->flow_delay_from_other_peer));
577 if (plugin->select_task != NULL )
578 GNUNET_SCHEDULER_cancel (plugin->select_task);
581 * - write active set if message is ready
582 * - timeout minimum delay */
583 plugin->select_task = GNUNET_SCHEDULER_add_select (
584 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
585 (0 == min_delay.rel_value_us) ?
586 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v4,
587 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
588 &udp_plugin_select, plugin);
590 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
592 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
593 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
594 min_delay = GNUNET_TIME_relative_min (min_delay,
595 GNUNET_TIME_absolute_get_remaining (
596 udpw->session->flow_delay_from_other_peer));
598 if (NULL != plugin->select_task_v6)
599 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
600 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (
601 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
602 (0 == min_delay.rel_value_us) ?
603 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v6,
604 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
605 &udp_plugin_select_v6, plugin);
611 * Function called for a quick conversion of the binary address to
612 * a numeric address. Note that the caller must not free the
613 * address and that the next call to this function is allowed
614 * to override the address again.
617 * @param addr binary address (a `union UdpAddress`)
618 * @param addrlen length of the @a addr
619 * @return string representing the same address
622 udp_address_to_string (void *cls,
626 static char rbuf[INET6_ADDRSTRLEN + 10];
627 char buf[INET6_ADDRSTRLEN];
631 const struct IPv4UdpAddress *t4;
632 const struct IPv6UdpAddress *t6;
637 if ((NULL != addr) && (addrlen == sizeof(struct IPv6UdpAddress)))
641 options = ntohl (t6->options);
642 port = ntohs (t6->u6_port);
643 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
646 else if ((NULL != addr) && (addrlen == sizeof(struct IPv4UdpAddress)))
650 options = ntohl (t4->options);
651 port = ntohs (t4->u4_port);
652 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
659 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
661 GNUNET_snprintf (rbuf, sizeof(rbuf),
674 * Function called to convert a string address to
677 * @param cls closure (`struct Plugin *`)
678 * @param addr string address
679 * @param addrlen length of the address
680 * @param buf location to store the buffer
681 * @param added location to store the number of bytes in the buffer.
682 * If the function returns #GNUNET_SYSERR, its contents are undefined.
683 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
686 udp_string_to_address (void *cls,
692 struct sockaddr_storage socket_address;
698 /* Format tcp.options.address:port */
703 if ((NULL == addr) || (addrlen == 0))
706 return GNUNET_SYSERR;
708 if ('\0' != addr[addrlen - 1])
711 return GNUNET_SYSERR;
713 if (strlen (addr) != addrlen - 1)
716 return GNUNET_SYSERR;
718 plugin = GNUNET_strdup (addr);
719 optionstr = strchr (plugin, '.');
720 if (NULL == optionstr)
724 return GNUNET_SYSERR;
728 options = atol (optionstr);
729 address = strchr (optionstr, '.');
734 return GNUNET_SYSERR;
740 GNUNET_STRINGS_to_address_ip (address, strlen (address),
745 return GNUNET_SYSERR;
750 switch (socket_address.ss_family)
754 struct IPv4UdpAddress *u4;
755 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
756 u4 = GNUNET_new (struct IPv4UdpAddress);
757 u4->options = htonl (options);
758 u4->ipv4_addr = in4->sin_addr.s_addr;
759 u4->u4_port = in4->sin_port;
761 *added = sizeof(struct IPv4UdpAddress);
766 struct IPv6UdpAddress *u6;
767 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
768 u6 = GNUNET_new (struct IPv6UdpAddress);
769 u6->options = htonl (options);
770 u6->ipv6_addr = in6->sin6_addr;
771 u6->u6_port = in6->sin6_port;
773 *added = sizeof(struct IPv6UdpAddress);
778 return GNUNET_SYSERR;
784 * Append our port and forward the result.
786 * @param cls a `struct PrettyPrinterContext *`
787 * @param hostname result from DNS resolver
790 append_port (void *cls,
791 const char *hostname)
793 struct PrettyPrinterContext *ppc = cls;
794 struct Plugin *plugin = ppc->plugin;
797 if (NULL == hostname)
799 /* Final call, done */
800 ppc->asc (ppc->asc_cls,
803 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
804 plugin->ppc_dll_tail,
806 ppc->resolver_handle = NULL;
810 if (GNUNET_YES == ppc->ipv6)
811 GNUNET_asprintf (&ret,
818 GNUNET_asprintf (&ret,
824 ppc->asc (ppc->asc_cls,
832 * Convert the transports address to a nice, human-readable
835 * @param cls closure with the `struct Plugin *`
836 * @param type name of the transport that generated the address
837 * @param addr one of the addresses of the host, NULL for the last address
838 * the specific address format depends on the transport;
839 * a `union UdpAddress`
840 * @param addrlen length of the address
841 * @param numeric should (IP) addresses be displayed in numeric form?
842 * @param timeout after how long should we give up?
843 * @param asc function to call on each string
844 * @param asc_cls closure for @a asc
847 udp_plugin_address_pretty_printer (void *cls,
852 struct GNUNET_TIME_Relative timeout,
853 GNUNET_TRANSPORT_AddressStringCallback asc,
856 struct Plugin *plugin = cls;
857 struct PrettyPrinterContext *ppc;
860 struct sockaddr_in a4;
861 struct sockaddr_in6 a6;
862 const struct IPv4UdpAddress *u4;
863 const struct IPv6UdpAddress *u6;
867 if (addrlen == sizeof(struct IPv6UdpAddress))
870 memset (&a6, 0, sizeof(a6));
871 a6.sin6_family = AF_INET6;
872 #if HAVE_SOCKADDR_IN_SIN_LEN
873 a6.sin6_len = sizeof (a6);
875 a6.sin6_port = u6->u6_port;
876 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
877 port = ntohs (u6->u6_port);
878 options = ntohl (u6->options);
882 else if (addrlen == sizeof(struct IPv4UdpAddress))
885 memset (&a4, 0, sizeof(a4));
886 a4.sin_family = AF_INET;
887 #if HAVE_SOCKADDR_IN_SIN_LEN
888 a4.sin_len = sizeof (a4);
890 a4.sin_port = u4->u4_port;
891 a4.sin_addr.s_addr = u4->ipv4_addr;
892 port = ntohs (u4->u4_port);
893 options = ntohl (u4->options);
899 /* invalid address */
901 asc (asc_cls, NULL , GNUNET_SYSERR);
902 asc (asc_cls, NULL, GNUNET_OK);
905 ppc = GNUNET_new (struct PrettyPrinterContext);
906 ppc->plugin = plugin;
908 ppc->asc_cls = asc_cls;
910 ppc->options = options;
911 if (addrlen == sizeof(struct IPv6UdpAddress))
912 ppc->ipv6 = GNUNET_YES;
914 ppc->ipv6 = GNUNET_NO;
915 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
916 plugin->ppc_dll_tail,
919 = GNUNET_RESOLVER_hostname_get (sb,
931 call_continuation (struct UDP_MessageWrapper *udpw,
934 struct Session *session = udpw->session;
935 struct Plugin *plugin = session->plugin;
938 LOG (GNUNET_ERROR_TYPE_DEBUG,
939 "Calling continuation for %u byte message to `%s' with result %s\n",
941 GNUNET_i2s (&udpw->session->target),
942 (GNUNET_OK == result) ? "OK" : "SYSERR");
944 if (udpw->msg_size >= udpw->payload_size)
945 overhead = udpw->msg_size - udpw->payload_size;
947 overhead = udpw->msg_size;
952 switch (udpw->msg_type)
954 case UMT_MSG_UNFRAGMENTED:
955 if (NULL != udpw->cont)
957 /* Transport continuation */
958 udpw->cont (udpw->cont_cls,
959 &udpw->session->target,
964 GNUNET_STATISTICS_update (plugin->env->stats,
965 "# UDP, unfragmented msgs, messages, sent, success",
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 "# UDP, unfragmented msgs, bytes payload, sent, success",
972 GNUNET_STATISTICS_update (plugin->env->stats,
973 "# UDP, unfragmented msgs, bytes overhead, sent, success",
976 GNUNET_STATISTICS_update (plugin->env->stats,
977 "# UDP, total, bytes overhead, sent",
980 GNUNET_STATISTICS_update (plugin->env->stats,
981 "# UDP, total, bytes payload, sent",
985 case UMT_MSG_FRAGMENTED_COMPLETE:
986 GNUNET_assert(NULL != udpw->frag_ctx);
987 if (udpw->frag_ctx->cont != NULL )
988 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls,
989 &udpw->session->target,
991 udpw->frag_ctx->payload_size,
992 udpw->frag_ctx->on_wire_size);
993 GNUNET_STATISTICS_update (plugin->env->stats,
994 "# UDP, fragmented msgs, messages, sent, success",
997 GNUNET_STATISTICS_update (plugin->env->stats,
998 "# UDP, fragmented msgs, bytes payload, sent, success",
1001 GNUNET_STATISTICS_update (plugin->env->stats,
1002 "# UDP, fragmented msgs, bytes overhead, sent, success",
1005 GNUNET_STATISTICS_update (plugin->env->stats,
1006 "# UDP, total, bytes overhead, sent",
1009 GNUNET_STATISTICS_update (plugin->env->stats,
1010 "# UDP, total, bytes payload, sent",
1013 GNUNET_STATISTICS_update (plugin->env->stats,
1014 "# UDP, fragmented msgs, messages, pending",
1018 case UMT_MSG_FRAGMENTED:
1019 /* Fragmented message: enqueue next fragment */
1020 if (NULL != udpw->cont)
1021 udpw->cont (udpw->cont_cls,
1022 &udpw->session->target,
1026 GNUNET_STATISTICS_update (plugin->env->stats,
1027 "# UDP, fragmented msgs, fragments, sent, success",
1030 GNUNET_STATISTICS_update (plugin->env->stats,
1031 "# UDP, fragmented msgs, fragments bytes, sent, success",
1036 /* No continuation */
1037 GNUNET_STATISTICS_update (plugin->env->stats,
1038 "# UDP, ACK msgs, messages, sent, success",
1041 GNUNET_STATISTICS_update (plugin->env->stats,
1042 "# UDP, ACK msgs, bytes overhead, sent, success",
1045 GNUNET_STATISTICS_update (plugin->env->stats,
1046 "# UDP, total, bytes overhead, sent",
1056 switch (udpw->msg_type)
1058 case UMT_MSG_UNFRAGMENTED:
1059 /* Unfragmented message: failed to send */
1060 if (NULL != udpw->cont)
1061 udpw->cont (udpw->cont_cls,
1062 &udpw->session->target,
1066 GNUNET_STATISTICS_update (plugin->env->stats,
1067 "# UDP, unfragmented msgs, messages, sent, failure",
1070 GNUNET_STATISTICS_update (plugin->env->stats,
1071 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1074 GNUNET_STATISTICS_update (plugin->env->stats,
1075 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
1079 case UMT_MSG_FRAGMENTED_COMPLETE:
1080 GNUNET_assert (NULL != udpw->frag_ctx);
1081 if (udpw->frag_ctx->cont != NULL)
1082 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls,
1083 &udpw->session->target,
1085 udpw->frag_ctx->payload_size,
1086 udpw->frag_ctx->on_wire_size);
1087 GNUNET_STATISTICS_update (plugin->env->stats,
1088 "# UDP, fragmented msgs, messages, sent, failure",
1091 GNUNET_STATISTICS_update (plugin->env->stats,
1092 "# UDP, fragmented msgs, bytes payload, sent, failure",
1095 GNUNET_STATISTICS_update (plugin->env->stats,
1096 "# UDP, fragmented msgs, bytes payload, sent, failure",
1099 GNUNET_STATISTICS_update (plugin->env->stats,
1100 "# UDP, fragmented msgs, bytes payload, sent, failure",
1103 GNUNET_STATISTICS_update (plugin->env->stats,
1104 "# UDP, fragmented msgs, messages, pending",
1108 case UMT_MSG_FRAGMENTED:
1109 GNUNET_assert (NULL != udpw->frag_ctx);
1110 /* Fragmented message: failed to send */
1111 GNUNET_STATISTICS_update (plugin->env->stats,
1112 "# UDP, fragmented msgs, fragments, sent, failure",
1115 GNUNET_STATISTICS_update (plugin->env->stats,
1116 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1121 /* ACK message: failed to send */
1122 GNUNET_STATISTICS_update (plugin->env->stats,
1123 "# UDP, ACK msgs, messages, sent, failure",
1140 * Check if the given port is plausible (must be either our listen
1141 * port or our advertised port). If it is neither, we return
1144 * @param plugin global variables
1145 * @param in_port port number to check
1146 * @return #GNUNET_OK if port is either open_port or adv_port
1149 check_port (struct Plugin *plugin,
1152 if ((in_port == plugin->port) || (in_port == plugin->aport))
1154 return GNUNET_SYSERR;
1159 * Function that will be called to check if a binary address for this
1160 * plugin is well-formed and corresponds to an address for THIS peer
1161 * (as per our configuration). Naturally, if absolutely necessary,
1162 * plugins can be a bit conservative in their answer, but in general
1163 * plugins should make sure that the address does not redirect
1164 * traffic to a 3rd party that might try to man-in-the-middle our
1167 * @param cls closure, should be our handle to the Plugin
1168 * @param addr pointer to a `union UdpAddress`
1169 * @param addrlen length of @a addr
1170 * @return #GNUNET_OK if this is a plausible address for this peer
1171 * and transport, #GNUNET_SYSERR if not
1174 udp_plugin_check_address (void *cls,
1178 struct Plugin *plugin = cls;
1179 struct IPv4UdpAddress *v4;
1180 struct IPv6UdpAddress *v6;
1182 if ( (addrlen != sizeof(struct IPv4UdpAddress)) &&
1183 (addrlen != sizeof(struct IPv6UdpAddress)) )
1186 return GNUNET_SYSERR;
1188 if (addrlen == sizeof(struct IPv4UdpAddress))
1190 v4 = (struct IPv4UdpAddress *) addr;
1191 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1192 return GNUNET_SYSERR;
1194 GNUNET_NAT_test_address (plugin->nat,
1196 sizeof (struct in_addr)))
1197 return GNUNET_SYSERR;
1201 v6 = (struct IPv6UdpAddress *) addr;
1202 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1205 return GNUNET_SYSERR;
1207 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1208 return GNUNET_SYSERR;
1210 GNUNET_NAT_test_address (plugin->nat,
1212 sizeof(struct in6_addr)))
1213 return GNUNET_SYSERR;
1220 * Function to free last resources associated with a session.
1222 * @param s session to free
1225 free_session (struct Session *s)
1227 if (NULL != s->frag_ctx)
1229 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL );
1230 GNUNET_free(s->frag_ctx);
1238 * Remove a message from the transmission queue.
1240 * @param plugin the UDP plugin
1241 * @param udpw message wrapper to queue
1244 dequeue (struct Plugin *plugin,
1245 struct UDP_MessageWrapper *udpw)
1247 struct Session *session = udpw->session;
1249 if (plugin->bytes_in_buffer < udpw->msg_size)
1255 GNUNET_STATISTICS_update (plugin->env->stats,
1256 "# UDP, total, bytes in buffers",
1257 -(long long) udpw->msg_size,
1259 plugin->bytes_in_buffer -= udpw->msg_size;
1261 GNUNET_STATISTICS_update (plugin->env->stats,
1262 "# UDP, total, msgs in buffers",
1264 if (udpw->session->address->address_length == sizeof(struct IPv4UdpAddress))
1265 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1266 plugin->ipv4_queue_tail,
1268 else if (udpw->session->address->address_length == sizeof(struct IPv6UdpAddress))
1269 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1270 plugin->ipv6_queue_tail,
1277 GNUNET_assert (session->msgs_in_queue > 0);
1278 session->msgs_in_queue--;
1279 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1280 session->bytes_in_queue -= udpw->msg_size;
1285 * We have completed our (attempt) to transmit a message
1286 * that had to be fragmented -- either because we got an
1287 * ACK saying that all fragments were received, or because
1288 * of timeout / disconnect. Clean up our state.
1290 * @param fc fragmentation context to clean up
1291 * @param result #GNUNET_OK if we succeeded (got ACK),
1292 * #GNUNET_SYSERR if the transmission failed
1295 fragmented_message_done (struct UDP_FragmentationContext *fc,
1298 struct Plugin *plugin = fc->plugin;
1299 struct Session *s = fc->session;
1300 struct UDP_MessageWrapper *udpw;
1301 struct UDP_MessageWrapper *tmp;
1302 struct UDP_MessageWrapper dummy;
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "%p : Fragmented message removed with result %s\n",
1307 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1309 /* Call continuation for fragmented message */
1310 memset (&dummy, 0, sizeof(dummy));
1311 dummy.msg_type = UMT_MSG_FRAGMENTED_COMPLETE;
1312 dummy.msg_size = s->frag_ctx->on_wire_size;
1313 dummy.payload_size = s->frag_ctx->payload_size;
1314 dummy.frag_ctx = s->frag_ctx;
1316 dummy.cont_cls = NULL;
1318 call_continuation (&dummy, result);
1319 /* Remove leftover fragments from queue */
1320 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1322 udpw = plugin->ipv6_queue_head;
1323 while (NULL != udpw)
1326 if ( (udpw->frag_ctx != NULL) &&
1327 (udpw->frag_ctx == s->frag_ctx) )
1329 dequeue (plugin, udpw);
1330 call_continuation (udpw, GNUNET_SYSERR);
1336 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1338 udpw = plugin->ipv4_queue_head;
1339 while (udpw != NULL )
1342 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1344 dequeue (plugin, udpw);
1345 call_continuation (udpw, GNUNET_SYSERR);
1351 notify_session_monitor (s->plugin,
1353 GNUNET_TRANSPORT_SS_UPDATE);
1354 /* Destroy fragmentation context */
1355 GNUNET_FRAGMENT_context_destroy (fc->frag,
1356 &s->last_expected_msg_delay,
1357 &s->last_expected_ack_delay);
1364 * Scan the heap for a receive context with the given address.
1366 * @param cls the `struct FindReceiveContext`
1367 * @param node internal node of the heap
1368 * @param element value stored at the node (a `struct ReceiveContext`)
1369 * @param cost cost associated with the node
1370 * @return #GNUNET_YES if we should continue to iterate,
1371 * #GNUNET_NO if not.
1374 find_receive_context (void *cls,
1375 struct GNUNET_CONTAINER_HeapNode *node,
1377 GNUNET_CONTAINER_HeapCostType cost)
1379 struct FindReceiveContext *frc = cls;
1380 struct DefragContext *e = element;
1382 if ( (frc->udp_addr_len == e->udp_addr_len) &&
1383 (0 == memcmp (frc->udp_addr,
1385 frc->udp_addr_len)) )
1395 * Functions with this signature are called whenever we need
1396 * to close a session due to a disconnect or failure to
1397 * establish a connection.
1399 * @param cls closure with the `struct Plugin`
1400 * @param s session to close down
1401 * @return #GNUNET_OK on success
1404 udp_disconnect_session (void *cls,
1407 struct Plugin *plugin = cls;
1408 struct UDP_MessageWrapper *udpw;
1409 struct UDP_MessageWrapper *next;
1410 struct FindReceiveContext frc;
1412 GNUNET_assert (GNUNET_YES != s->in_destroy);
1413 LOG (GNUNET_ERROR_TYPE_DEBUG,
1414 "Session %p to peer `%s' address ended\n",
1416 GNUNET_i2s (&s->target),
1417 udp_address_to_string (plugin,
1418 s->address->address,
1419 s->address->address_length));
1420 /* stop timeout task */
1421 if (NULL != s->timeout_task)
1423 GNUNET_SCHEDULER_cancel (s->timeout_task);
1424 s->timeout_task = NULL;
1426 if (NULL != s->frag_ctx)
1428 /* Remove fragmented message due to disconnect */
1429 fragmented_message_done (s->frag_ctx,
1434 frc.udp_addr = s->address->address;
1435 frc.udp_addr_len = s->address->address_length;
1436 /* Lookup existing receive context for this address */
1437 if (NULL != plugin->defrag_ctxs)
1439 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1440 &find_receive_context,
1444 struct DefragContext *d_ctx = frc.rc;
1446 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
1447 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1448 GNUNET_free (d_ctx);
1451 next = plugin->ipv4_queue_head;
1452 while (NULL != (udpw = next))
1455 if (udpw->session == s)
1457 dequeue (plugin, udpw);
1458 call_continuation (udpw, GNUNET_SYSERR);
1462 next = plugin->ipv6_queue_head;
1463 while (NULL != (udpw = next))
1466 if (udpw->session == s)
1468 dequeue (plugin, udpw);
1469 call_continuation (udpw, GNUNET_SYSERR);
1473 notify_session_monitor (s->plugin,
1475 GNUNET_TRANSPORT_SS_DONE);
1476 plugin->env->session_end (plugin->env->cls,
1480 if (NULL != s->frag_ctx)
1482 if (NULL != s->frag_ctx->cont)
1484 s->frag_ctx->cont (s->frag_ctx->cont_cls,
1487 s->frag_ctx->payload_size,
1488 s->frag_ctx->on_wire_size);
1489 LOG (GNUNET_ERROR_TYPE_DEBUG,
1490 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1491 GNUNET_i2s (&s->target));
1495 GNUNET_assert (GNUNET_YES ==
1496 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1499 GNUNET_STATISTICS_set (plugin->env->stats,
1500 "# UDP sessions active",
1501 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1505 s->in_destroy = GNUNET_YES;
1509 GNUNET_HELLO_address_free (s->address);
1517 * Function that is called to get the keepalive factor.
1518 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1519 * calculate the interval between keepalive packets.
1521 * @param cls closure with the `struct Plugin`
1522 * @return keepalive factor
1525 udp_query_keepalive_factor (void *cls)
1532 * Destroy a session, plugin is being unloaded.
1534 * @param cls the `struct Plugin`
1535 * @param key hash of public key of target peer
1536 * @param value a `struct PeerSession *` to clean up
1537 * @return #GNUNET_OK (continue to iterate)
1540 disconnect_and_free_it (void *cls,
1541 const struct GNUNET_PeerIdentity *key,
1544 struct Plugin *plugin = cls;
1546 udp_disconnect_session (plugin, value);
1552 * Disconnect from a remote node. Clean up session if we have one for
1555 * @param cls closure for this call (should be handle to Plugin)
1556 * @param target the peeridentity of the peer to disconnect
1557 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1560 udp_disconnect (void *cls,
1561 const struct GNUNET_PeerIdentity *target)
1563 struct Plugin *plugin = cls;
1565 LOG (GNUNET_ERROR_TYPE_DEBUG,
1566 "Disconnecting from peer `%s'\n",
1567 GNUNET_i2s (target));
1568 /* Clean up sessions */
1569 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1571 &disconnect_and_free_it,
1577 * Session was idle, so disconnect it
1579 * @param cls the `struct Session` to time out
1580 * @param tc scheduler context
1583 session_timeout (void *cls,
1584 const struct GNUNET_SCHEDULER_TaskContext *tc)
1586 struct Session *s = cls;
1587 struct Plugin *plugin = s->plugin;
1588 struct GNUNET_TIME_Relative left;
1590 s->timeout_task = NULL;
1591 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1592 if (left.rel_value_us > 0)
1594 /* not actually our turn yet, but let's at least update
1595 the monitor, it may think we're about to die ... */
1596 notify_session_monitor (s->plugin,
1598 GNUNET_TRANSPORT_SS_UPDATE);
1599 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Session %p was idle for %s, disconnecting\n",
1607 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
1609 /* call session destroy function */
1610 udp_disconnect_session (plugin, s);
1615 * Increment session timeout due to activity
1617 * @param s session to reschedule timeout activity for
1620 reschedule_session_timeout (struct Session *s)
1622 if (GNUNET_YES == s->in_destroy)
1624 GNUNET_assert(NULL != s->timeout_task);
1625 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1630 * Function obtain the network type for a session
1632 * @param cls closure (`struct Plugin *`)
1633 * @param session the session
1634 * @return the network type
1636 static enum GNUNET_ATS_Network_Type
1637 udp_get_network (void *cls,
1638 struct Session *session)
1640 return session->scope;
1645 * Closure for #session_cmp_it().
1647 struct SessionCompareContext
1650 * Set to session matching the address.
1652 struct Session *res;
1655 * Address we are looking for.
1657 const struct GNUNET_HELLO_Address *address;
1662 * Find a session with a matching address.
1664 * @param cls the `struct SessionCompareContext *`
1665 * @param key peer identity (unused)
1666 * @param value the `struct Session *`
1667 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1670 session_cmp_it (void *cls,
1671 const struct GNUNET_PeerIdentity *key,
1674 struct SessionCompareContext *cctx = cls;
1675 struct Session *s = value;
1677 if (0 == GNUNET_HELLO_address_cmp (s->address,
1688 * Locate an existing session the transport service is using to
1689 * send data to another peer. Performs some basic sanity checks
1690 * on the address and then tries to locate a matching session.
1692 * @param cls the plugin
1693 * @param address the address we should locate the session by
1694 * @return the session if it exists, or NULL if it is not found
1696 static struct Session *
1697 udp_plugin_lookup_session (void *cls,
1698 const struct GNUNET_HELLO_Address *address)
1700 struct Plugin *plugin = cls;
1701 const struct IPv6UdpAddress *udp_a6;
1702 const struct IPv4UdpAddress *udp_a4;
1703 struct SessionCompareContext cctx;
1705 if ( (NULL == address->address) ||
1706 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1707 (address->address_length != sizeof (struct IPv6UdpAddress))))
1709 LOG (GNUNET_ERROR_TYPE_WARNING,
1710 "Trying to locate session for address of unexpected length %u (should be %u or %u)\n",
1711 address->address_length,
1712 sizeof (struct IPv4UdpAddress),
1713 sizeof (struct IPv6UdpAddress));
1717 if (address->address_length == sizeof(struct IPv4UdpAddress))
1719 if (NULL == plugin->sockv4)
1721 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1722 if (0 == udp_a4->u4_port)
1726 if (address->address_length == sizeof(struct IPv6UdpAddress))
1728 if (NULL == plugin->sockv6)
1730 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1731 if (0 == udp_a6->u6_port)
1735 /* check if session already exists */
1736 cctx.address = address;
1738 LOG (GNUNET_ERROR_TYPE_DEBUG,
1739 "Looking for existing session for peer `%s' `%s' \n",
1740 GNUNET_i2s (&address->peer),
1741 udp_address_to_string (plugin,
1743 address->address_length));
1744 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1748 if (NULL != cctx.res)
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "Found existing session %p\n",
1760 * Allocate a new session for the given endpoint address.
1761 * Note that this function does not inform the service
1762 * of the new session, this is the responsibility of the
1763 * caller (if needed).
1765 * @param cls the `struct Plugin`
1766 * @param address address of the other peer to use
1767 * @param network_type network type the address belongs to
1768 * @return NULL on error, otherwise session handle
1770 static struct Session *
1771 udp_plugin_create_session (void *cls,
1772 const struct GNUNET_HELLO_Address *address,
1773 enum GNUNET_ATS_Network_Type network_type)
1775 struct Plugin *plugin = cls;
1778 s = GNUNET_new (struct Session);
1780 s->address = GNUNET_HELLO_address_copy (address);
1781 s->target = address->peer;
1782 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1784 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1785 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1786 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1787 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1788 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1789 &session_timeout, s);
1790 s->scope = network_type;
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 "Creating new session %p for peer `%s' address `%s'\n",
1795 GNUNET_i2s (&address->peer),
1796 udp_address_to_string (plugin,
1798 address->address_length));
1799 GNUNET_assert(GNUNET_OK ==
1800 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1804 GNUNET_STATISTICS_set (plugin->env->stats,
1805 "# UDP sessions active",
1806 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1813 * Function that will be called whenever the transport service wants to
1814 * notify the plugin that a session is still active and in use and
1815 * therefore the session timeout for this session has to be updated
1817 * @param cls closure with the `struct Plugin`
1818 * @param peer which peer was the session for
1819 * @param session which session is being updated
1822 udp_plugin_update_session_timeout (void *cls,
1823 const struct GNUNET_PeerIdentity *peer,
1824 struct Session *session)
1826 struct Plugin *plugin = cls;
1829 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1836 /* Reschedule session timeout */
1837 reschedule_session_timeout (session);
1842 * Creates a new outbound session the transport service will use to
1843 * send data to the peer.
1845 * @param cls the `struct Plugin *`
1846 * @param address the address
1847 * @return the session or NULL of max connections exceeded
1849 static struct Session *
1850 udp_plugin_get_session (void *cls,
1851 const struct GNUNET_HELLO_Address *address)
1853 struct Plugin *plugin = cls;
1855 enum GNUNET_ATS_Network_Type network_type;
1856 struct IPv4UdpAddress *udp_v4;
1857 struct IPv6UdpAddress *udp_v6;
1859 if (NULL == address)
1864 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
1865 (address->address_length != sizeof(struct IPv6UdpAddress)) )
1867 GNUNET_break_op (0);
1870 if (NULL != (s = udp_plugin_lookup_session (cls,
1874 /* need to create new session */
1875 if (sizeof (struct IPv4UdpAddress) == address->address_length)
1877 struct sockaddr_in v4;
1879 udp_v4 = (struct IPv4UdpAddress *) address->address;
1880 memset (&v4, '\0', sizeof (v4));
1881 v4.sin_family = AF_INET;
1882 #if HAVE_SOCKADDR_IN_SIN_LEN
1883 v4.sin_len = sizeof (struct sockaddr_in);
1885 v4.sin_port = udp_v4->u4_port;
1886 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
1887 network_type = plugin->env->get_address_type (plugin->env->cls,
1888 (const struct sockaddr *) &v4,
1891 if (sizeof (struct IPv6UdpAddress) == address->address_length)
1893 struct sockaddr_in6 v6;
1895 udp_v6 = (struct IPv6UdpAddress *) address->address;
1896 memset (&v6, '\0', sizeof (v6));
1897 v6.sin6_family = AF_INET6;
1898 #if HAVE_SOCKADDR_IN_SIN_LEN
1899 v6.sin6_len = sizeof (struct sockaddr_in6);
1901 v6.sin6_port = udp_v6->u6_port;
1902 v6.sin6_addr = udp_v6->ipv6_addr;
1903 network_type = plugin->env->get_address_type (plugin->env->cls,
1904 (const struct sockaddr *) &v6,
1907 return udp_plugin_create_session (cls,
1914 * Enqueue a message for transmission.
1916 * @param plugin the UDP plugin
1917 * @param udpw message wrapper to queue
1920 enqueue (struct Plugin *plugin,
1921 struct UDP_MessageWrapper *udpw)
1923 struct Session *session = udpw->session;
1925 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1931 GNUNET_STATISTICS_update (plugin->env->stats,
1932 "# UDP, total, bytes in buffers",
1935 plugin->bytes_in_buffer += udpw->msg_size;
1937 GNUNET_STATISTICS_update (plugin->env->stats,
1938 "# UDP, total, msgs in buffers",
1940 if (udpw->session->address->address_length == sizeof (struct IPv4UdpAddress))
1941 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1942 plugin->ipv4_queue_tail,
1944 else if (udpw->session->address->address_length == sizeof (struct IPv6UdpAddress))
1945 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1946 plugin->ipv6_queue_tail,
1953 session->msgs_in_queue++;
1954 session->bytes_in_queue += udpw->msg_size;
1959 * Fragment message was transmitted via UDP, let fragmentation know
1960 * to send the next fragment now.
1962 * @param cls the `struct UDPMessageWrapper *` of the fragment
1963 * @param target destination peer (ignored)
1964 * @param result #GNUNET_OK on success (ignored)
1965 * @param payload bytes payload sent
1966 * @param physical bytes physical sent
1969 send_next_fragment (void *cls,
1970 const struct GNUNET_PeerIdentity *target,
1975 struct UDP_MessageWrapper *udpw = cls;
1977 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1982 * Function that is called with messages created by the fragmentation
1983 * module. In the case of the 'proc' callback of the
1984 * #GNUNET_FRAGMENT_context_create() function, this function must
1985 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1987 * @param cls closure, the 'struct FragmentationContext'
1988 * @param msg the message that was created
1991 enqueue_fragment (void *cls,
1992 const struct GNUNET_MessageHeader *msg)
1994 struct UDP_FragmentationContext *frag_ctx = cls;
1995 struct Plugin *plugin = frag_ctx->plugin;
1996 struct UDP_MessageWrapper * udpw;
1997 size_t msg_len = ntohs (msg->size);
1999 LOG (GNUNET_ERROR_TYPE_DEBUG,
2000 "Enqueuing fragment with %u bytes\n",
2002 frag_ctx->fragments_used++;
2003 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
2004 udpw->session = frag_ctx->session;
2005 udpw->msg_buf = (char *) &udpw[1];
2006 udpw->msg_size = msg_len;
2007 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
2008 udpw->cont = &send_next_fragment;
2009 udpw->cont_cls = udpw;
2010 udpw->timeout = frag_ctx->timeout;
2011 udpw->frag_ctx = frag_ctx;
2012 udpw->msg_type = UMT_MSG_FRAGMENTED;
2013 memcpy (udpw->msg_buf, msg, msg_len);
2014 enqueue (plugin, udpw);
2015 schedule_select (plugin);
2020 * Function that can be used by the transport service to transmit
2021 * a message using the plugin. Note that in the case of a
2022 * peer disconnecting, the continuation MUST be called
2023 * prior to the disconnect notification itself. This function
2024 * will be called with this peer's HELLO message to initiate
2025 * a fresh connection to another peer.
2027 * @param cls closure
2028 * @param s which session must be used
2029 * @param msgbuf the message to transmit
2030 * @param msgbuf_size number of bytes in 'msgbuf'
2031 * @param priority how important is the message (most plugins will
2032 * ignore message priority and just FIFO)
2033 * @param to how long to wait at most for the transmission (does not
2034 * require plugins to discard the message after the timeout,
2035 * just advisory for the desired delay; most plugins will ignore
2037 * @param cont continuation to call once the message has
2038 * been transmitted (or if the transport is ready
2039 * for the next transmission call; or if the
2040 * peer disconnected...); can be NULL
2041 * @param cont_cls closure for cont
2042 * @return number of bytes used (on the physical network, with overheads);
2043 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2044 * and does NOT mean that the message was not transmitted (DV)
2047 udp_plugin_send (void *cls,
2051 unsigned int priority,
2052 struct GNUNET_TIME_Relative to,
2053 GNUNET_TRANSPORT_TransmitContinuation cont,
2056 struct Plugin *plugin = cls;
2057 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2058 struct UDP_FragmentationContext * frag_ctx;
2059 struct UDP_MessageWrapper * udpw;
2060 struct UDPMessage *udp;
2063 if ( (s->address->address_length == sizeof(struct IPv6UdpAddress)) &&
2064 (plugin->sockv6 == NULL) )
2065 return GNUNET_SYSERR;
2066 if ( (s->address->address_length == sizeof(struct IPv4UdpAddress)) &&
2067 (plugin->sockv4 == NULL) )
2068 return GNUNET_SYSERR;
2069 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2072 return GNUNET_SYSERR;
2075 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2080 return GNUNET_SYSERR;
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2085 GNUNET_i2s (&s->target),
2086 udp_address_to_string (plugin,
2087 s->address->address,
2088 s->address->address_length));
2091 udp = (struct UDPMessage *) mbuf;
2092 udp->header.size = htons (udpmlen);
2093 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2094 udp->reserved = htonl (0);
2095 udp->sender = *plugin->env->my_identity;
2097 /* We do not update the session time out here!
2098 * Otherwise this session will not timeout since we send keep alive before
2099 * session can timeout
2101 * For UDP we update session timeout only on receive, this will cover keep
2102 * alives, since remote peer will reply with keep alive response!
2104 if (udpmlen <= UDP_MTU)
2106 /* unfragmented message */
2107 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2109 udpw->msg_buf = (char *) &udpw[1];
2110 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2111 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2112 udpw->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to);
2114 udpw->cont_cls = cont_cls;
2115 udpw->frag_ctx = NULL;
2116 udpw->msg_type = UMT_MSG_UNFRAGMENTED;
2117 memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
2118 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)], msgbuf, msgbuf_size);
2119 enqueue (plugin, udpw);
2121 GNUNET_STATISTICS_update (plugin->env->stats,
2122 "# UDP, unfragmented msgs, messages, attempt", 1, GNUNET_NO);
2123 GNUNET_STATISTICS_update (plugin->env->stats,
2124 "# UDP, unfragmented msgs, bytes payload, attempt",
2130 /* fragmented message */
2131 if (s->frag_ctx != NULL)
2132 return GNUNET_SYSERR;
2133 memcpy (&udp[1], msgbuf, msgbuf_size);
2134 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2135 frag_ctx->plugin = plugin;
2136 frag_ctx->session = s;
2137 frag_ctx->cont = cont;
2138 frag_ctx->cont_cls = cont_cls;
2139 frag_ctx->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
2141 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2142 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2143 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2146 s->last_expected_msg_delay,
2147 s->last_expected_ack_delay,
2151 s->frag_ctx = frag_ctx;
2152 GNUNET_STATISTICS_update (plugin->env->stats,
2153 "# UDP, fragmented msgs, messages, pending",
2156 GNUNET_STATISTICS_update (plugin->env->stats,
2157 "# UDP, fragmented msgs, messages, attempt",
2160 GNUNET_STATISTICS_update (plugin->env->stats,
2161 "# UDP, fragmented msgs, bytes payload, attempt",
2162 frag_ctx->payload_size,
2165 notify_session_monitor (s->plugin,
2167 GNUNET_TRANSPORT_SS_UPDATE);
2168 schedule_select (plugin);
2174 * Our external IP address/port mapping has changed.
2176 * @param cls closure, the `struct LocalAddrList`
2177 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2178 * the previous (now invalid) one
2179 * @param addr either the previous or the new public IP address
2180 * @param addrlen actual lenght of the address
2183 udp_nat_port_map_callback (void *cls,
2185 const struct sockaddr *addr,
2188 struct Plugin *plugin = cls;
2189 struct GNUNET_HELLO_Address *address;
2190 struct IPv4UdpAddress u4;
2191 struct IPv6UdpAddress u6;
2195 LOG (GNUNET_ERROR_TYPE_INFO,
2196 "NAT notification to %s address `%s'\n",
2197 (GNUNET_YES == add_remove) ? "add" : "remove",
2198 GNUNET_a2s (addr, addrlen));
2200 /* convert 'address' to our internal format */
2201 switch (addr->sa_family)
2204 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
2205 memset (&u4, 0, sizeof(u4));
2206 u4.options = htonl (plugin->myoptions);
2207 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2208 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2209 if (0 == ((struct sockaddr_in *) addr)->sin_port)
2212 args = sizeof(struct IPv4UdpAddress);
2215 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
2216 memset (&u6, 0, sizeof(u6));
2217 u6.options = htonl (plugin->myoptions);
2218 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
2220 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2221 sizeof(struct in6_addr));
2222 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2224 args = sizeof(struct IPv6UdpAddress);
2230 /* modify our published address list */
2231 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2234 GNUNET_HELLO_ADDRESS_INFO_NONE);
2235 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2236 GNUNET_HELLO_address_free (address);
2241 * Message tokenizer has broken up an incomming message. Pass it on
2244 * @param cls the `struct Plugin *`
2245 * @param client the `struct SourceInformation *`
2246 * @param hdr the actual message
2247 * @return #GNUNET_OK (always)
2250 process_inbound_tokenized_messages (void *cls,
2252 const struct GNUNET_MessageHeader *hdr)
2254 struct Plugin *plugin = cls;
2255 struct SourceInformation *si = client;
2256 struct GNUNET_TIME_Relative delay;
2258 GNUNET_assert (NULL != si->session);
2259 if (GNUNET_YES == si->session->in_destroy)
2262 reschedule_session_timeout (si->session);
2263 delay = plugin->env->receive (plugin->env->cls,
2264 si->session->address,
2267 si->session->flow_delay_for_other_peer = delay;
2273 * We've received a UDP Message. Process it (pass contents to main service).
2275 * @param plugin plugin context
2276 * @param msg the message
2277 * @param udp_addr sender address
2278 * @param udp_addr_len number of bytes in @a udp_addr
2279 * @param network_type network type the address belongs to
2282 process_udp_message (struct Plugin *plugin,
2283 const struct UDPMessage *msg,
2284 const union UdpAddress *udp_addr,
2285 size_t udp_addr_len,
2286 enum GNUNET_ATS_Network_Type network_type)
2288 struct SourceInformation si;
2290 struct GNUNET_HELLO_Address *address;
2292 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2293 if (0 != ntohl (msg->reserved))
2298 if (ntohs (msg->header.size)
2299 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2305 address = GNUNET_HELLO_address_allocate (&msg->sender,
2309 GNUNET_HELLO_ADDRESS_INFO_NONE);
2311 (s = udp_plugin_lookup_session (plugin, address)))
2313 s = udp_plugin_create_session (plugin,
2316 plugin->env->session_start (plugin->env->cls,
2320 notify_session_monitor (s->plugin,
2322 GNUNET_TRANSPORT_SS_INIT);
2323 notify_session_monitor (s->plugin,
2325 GNUNET_TRANSPORT_SS_UP);
2327 GNUNET_free (address);
2329 /* iterate over all embedded messages */
2331 si.sender = msg->sender;
2333 GNUNET_SERVER_mst_receive (plugin->mst,
2335 (const char *) &msg[1],
2336 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2340 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
2346 * Process a defragmented message.
2348 * @param cls the `struct DefragContext *`
2349 * @param msg the message
2352 fragment_msg_proc (void *cls,
2353 const struct GNUNET_MessageHeader *msg)
2355 struct DefragContext *rc = cls;
2356 const struct UDPMessage *um;
2358 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2363 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2368 um = (const struct UDPMessage *) msg;
2369 rc->sender = um->sender;
2370 rc->have_sender = GNUNET_YES;
2371 process_udp_message (rc->plugin,
2380 * Transmit an acknowledgement.
2382 * @param cls the `struct DefragContext *`
2383 * @param id message ID (unused)
2384 * @param msg ack to transmit
2387 ack_proc (void *cls,
2389 const struct GNUNET_MessageHeader *msg)
2391 struct DefragContext *rc = cls;
2392 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2393 struct UDP_ACK_Message *udp_ack;
2395 struct UDP_MessageWrapper *udpw;
2397 struct GNUNET_HELLO_Address *address;
2399 if (GNUNET_NO == rc->have_sender)
2401 /* tried to defragment but never succeeded, hence will not ACK */
2402 GNUNET_break_op (0);
2405 address = GNUNET_HELLO_address_allocate (&rc->sender,
2409 GNUNET_HELLO_ADDRESS_INFO_NONE);
2410 s = udp_plugin_lookup_session (rc->plugin,
2412 GNUNET_HELLO_address_free (address);
2415 LOG (GNUNET_ERROR_TYPE_ERROR,
2416 "Trying to transmit ACK to peer `%s' but no session found!\n",
2417 udp_address_to_string (rc->plugin,
2420 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2421 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2425 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2426 delay = s->flow_delay_for_other_peer.rel_value_us;
2428 LOG (GNUNET_ERROR_TYPE_DEBUG,
2429 "Sending ACK to `%s' including delay of %s\n",
2430 udp_address_to_string (rc->plugin,
2433 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2435 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2436 udpw->msg_size = msize;
2437 udpw->payload_size = 0;
2439 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2440 udpw->msg_buf = (char *) &udpw[1];
2441 udpw->msg_type = UMT_MSG_ACK;
2442 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2443 udp_ack->header.size = htons ((uint16_t) msize);
2444 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2445 udp_ack->delay = htonl (delay);
2446 udp_ack->sender = *rc->plugin->env->my_identity;
2447 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2448 enqueue (rc->plugin, udpw);
2449 notify_session_monitor (s->plugin,
2451 GNUNET_TRANSPORT_SS_UPDATE);
2452 schedule_select (rc->plugin);
2457 * Handle an ACK message.
2459 * @param plugin the UDP plugin
2460 * @param msg the (presumed) UDP ACK message
2461 * @param udp_addr sender address
2462 * @param udp_addr_len number of bytes in @a udp_addr
2465 read_process_ack (struct Plugin *plugin,
2466 const struct GNUNET_MessageHeader *msg,
2467 const union UdpAddress *udp_addr,
2468 socklen_t udp_addr_len)
2470 const struct GNUNET_MessageHeader *ack;
2471 const struct UDP_ACK_Message *udp_ack;
2472 struct GNUNET_HELLO_Address *address;
2474 struct GNUNET_TIME_Relative flow_delay;
2476 if (ntohs (msg->size)
2477 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2482 udp_ack = (const struct UDP_ACK_Message *) msg;
2483 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2487 GNUNET_HELLO_ADDRESS_INFO_NONE);
2488 s = udp_plugin_lookup_session (plugin,
2492 LOG (GNUNET_ERROR_TYPE_WARNING,
2493 "UDP session of address %s for ACK not found\n",
2494 udp_address_to_string (plugin,
2496 address->address_length));
2497 GNUNET_HELLO_address_free (address);
2500 if (NULL == s->frag_ctx)
2502 LOG (GNUNET_ERROR_TYPE_WARNING,
2503 "Fragmentation context of address %s for ACK not found\n",
2504 udp_address_to_string (plugin,
2506 address->address_length));
2507 GNUNET_HELLO_address_free (address);
2510 GNUNET_HELLO_address_free (address);
2512 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2513 LOG (GNUNET_ERROR_TYPE_DEBUG,
2514 "We received a sending delay of %s for %s\n",
2515 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2517 GNUNET_i2s (&udp_ack->sender));
2518 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2520 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2521 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2528 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2531 LOG(GNUNET_ERROR_TYPE_DEBUG,
2532 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2533 (unsigned int ) ntohs (msg->size),
2534 GNUNET_i2s (&udp_ack->sender),
2535 udp_address_to_string (plugin,
2538 /* Expect more ACKs to arrive */
2542 LOG (GNUNET_ERROR_TYPE_DEBUG,
2543 "Message from %s at %s full ACK'ed\n",
2544 GNUNET_i2s (&udp_ack->sender),
2545 udp_address_to_string (plugin,
2549 /* Remove fragmented message after successful sending */
2550 fragmented_message_done (s->frag_ctx,
2556 * We received a fragment, process it.
2558 * @param plugin our plugin
2559 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2560 * @param udp_addr sender address
2561 * @param udp_addr_len number of bytes in @a udp_addr
2562 * @param network_type network type the address belongs to
2565 read_process_fragment (struct Plugin *plugin,
2566 const struct GNUNET_MessageHeader *msg,
2567 const union UdpAddress *udp_addr,
2568 size_t udp_addr_len,
2569 enum GNUNET_ATS_Network_Type network_type)
2571 struct DefragContext *d_ctx;
2572 struct GNUNET_TIME_Absolute now;
2573 struct FindReceiveContext frc;
2576 frc.udp_addr = udp_addr;
2577 frc.udp_addr_len = udp_addr_len;
2579 /* Lookup existing receive context for this address */
2580 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2581 &find_receive_context,
2583 now = GNUNET_TIME_absolute_get ();
2588 /* Create a new defragmentation context */
2589 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2593 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2594 d_ctx->udp_addr_len = udp_addr_len;
2595 d_ctx->network_type = network_type;
2596 d_ctx->plugin = plugin;
2597 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2599 UDP_MAX_MESSAGES_IN_DEFRAG,
2603 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2605 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2606 LOG (GNUNET_ERROR_TYPE_DEBUG,
2607 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2608 (unsigned int ) ntohs (msg->size),
2609 udp_address_to_string (plugin,
2615 LOG (GNUNET_ERROR_TYPE_DEBUG,
2616 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2617 (unsigned int ) ntohs (msg->size),
2618 udp_address_to_string (plugin,
2624 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2626 /* keep this 'rc' from expiring */
2627 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2629 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2631 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2632 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2634 /* remove 'rc' that was inactive the longest */
2635 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2636 GNUNET_assert (NULL != d_ctx);
2637 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2638 GNUNET_free (d_ctx);
2644 * Read and process a message from the given socket.
2646 * @param plugin the overall plugin
2647 * @param rsock socket to read from
2650 udp_select_read (struct Plugin *plugin,
2651 struct GNUNET_NETWORK_Handle *rsock)
2654 struct sockaddr_storage addr;
2655 char buf[65536] GNUNET_ALIGN;
2657 const struct GNUNET_MessageHeader *msg;
2658 struct IPv4UdpAddress v4;
2659 struct IPv6UdpAddress v6;
2660 const struct sockaddr *sa;
2661 const struct sockaddr_in *sa4;
2662 const struct sockaddr_in6 *sa6;
2663 const union UdpAddress *int_addr;
2664 size_t int_addr_len;
2665 enum GNUNET_ATS_Network_Type network_type;
2667 fromlen = sizeof(addr);
2668 memset (&addr, 0, sizeof(addr));
2669 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof(buf),
2670 (struct sockaddr *) &addr, &fromlen);
2671 sa = (const struct sockaddr *) &addr;
2673 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2674 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2675 * on this socket has failed.
2677 * WSAECONNRESET - The virtual circuit was reset by the remote side
2678 * executing a hard or abortive close. The application should close
2679 * the socket; it is no longer usable. On a UDP-datagram socket this
2680 * error indicates a previous send operation resulted in an ICMP Port
2681 * Unreachable message.
2683 if ( (-1 == size) && (ECONNRESET == errno) )
2688 LOG (GNUNET_ERROR_TYPE_DEBUG,
2689 "UDP failed to receive data: %s\n",
2691 /* Connection failure or something. Not a protocol violation. */
2694 if (size < sizeof(struct GNUNET_MessageHeader))
2696 LOG (GNUNET_ERROR_TYPE_WARNING,
2697 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2698 (unsigned int ) size,
2699 GNUNET_a2s (sa, fromlen));
2700 /* _MAY_ be a connection failure (got partial message) */
2701 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2705 msg = (const struct GNUNET_MessageHeader *) buf;
2706 LOG (GNUNET_ERROR_TYPE_DEBUG,
2707 "UDP received %u-byte message from `%s' type %u\n",
2708 (unsigned int) size,
2712 if (size != ntohs (msg->size))
2714 LOG (GNUNET_ERROR_TYPE_WARNING,
2715 "UDP malformed message header from %s\n",
2716 (unsigned int) size,
2719 GNUNET_break_op (0);
2722 GNUNET_STATISTICS_update (plugin->env->stats,
2723 "# UDP, total, bytes, received",
2726 network_type = plugin->env->get_address_type (plugin->env->cls,
2729 switch (sa->sa_family)
2732 sa4 = (const struct sockaddr_in *) &addr;
2734 v4.ipv4_addr = sa4->sin_addr.s_addr;
2735 v4.u4_port = sa4->sin_port;
2736 int_addr = (union UdpAddress *) &v4;
2737 int_addr_len = sizeof (v4);
2740 sa6 = (const struct sockaddr_in6 *) &addr;
2742 v6.ipv6_addr = sa6->sin6_addr;
2743 v6.u6_port = sa6->sin6_port;
2744 int_addr = (union UdpAddress *) &v6;
2745 int_addr_len = sizeof (v6);
2752 switch (ntohs (msg->type))
2754 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2755 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2756 udp_broadcast_receive (plugin,
2763 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2764 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2769 process_udp_message (plugin,
2770 (const struct UDPMessage *) msg,
2775 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2776 read_process_ack (plugin,
2781 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2782 read_process_fragment (plugin,
2796 * Removes messages from the transmission queue that have
2797 * timed out, and then selects a message that should be
2800 * @param plugin the UDP plugin
2801 * @param sock which socket should we process the queue for (v4 or v6)
2802 * @return message selected for transmission, or NULL for none
2804 static struct UDP_MessageWrapper *
2805 remove_timeout_messages_and_select (struct Plugin *plugin,
2806 struct GNUNET_NETWORK_Handle *sock)
2808 struct UDP_MessageWrapper *udpw = NULL;
2809 struct GNUNET_TIME_Relative remaining;
2810 struct Session *session;
2813 removed = GNUNET_NO;
2814 udpw = (sock == plugin->sockv4)
2815 ? plugin->ipv4_queue_head
2816 : plugin->ipv6_queue_head;
2817 while (NULL != udpw)
2819 session = udpw->session;
2820 /* Find messages with timeout */
2821 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2822 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2824 /* Message timed out */
2825 switch (udpw->msg_type)
2827 case UMT_MSG_UNFRAGMENTED:
2828 GNUNET_STATISTICS_update (plugin->env->stats,
2829 "# UDP, total, bytes, sent, timeout",
2832 GNUNET_STATISTICS_update (plugin->env->stats,
2833 "# UDP, total, messages, sent, timeout",
2836 GNUNET_STATISTICS_update (plugin->env->stats,
2837 "# UDP, unfragmented msgs, messages, sent, timeout",
2840 GNUNET_STATISTICS_update (plugin->env->stats,
2841 "# UDP, unfragmented msgs, bytes, sent, timeout",
2844 /* Not fragmented message */
2845 LOG (GNUNET_ERROR_TYPE_DEBUG,
2846 "Message for peer `%s' with size %u timed out\n",
2847 GNUNET_i2s (&udpw->session->target),
2848 udpw->payload_size);
2849 call_continuation (udpw, GNUNET_SYSERR);
2850 /* Remove message */
2851 removed = GNUNET_YES;
2852 dequeue (plugin, udpw);
2855 case UMT_MSG_FRAGMENTED:
2856 /* Fragmented message */
2857 GNUNET_STATISTICS_update (plugin->env->stats,
2858 "# UDP, total, bytes, sent, timeout",
2859 udpw->frag_ctx->on_wire_size,
2861 GNUNET_STATISTICS_update (plugin->env->stats,
2862 "# UDP, total, messages, sent, timeout",
2865 call_continuation (udpw,
2867 LOG (GNUNET_ERROR_TYPE_DEBUG,
2868 "Fragment for message for peer `%s' with size %u timed out\n",
2869 GNUNET_i2s (&udpw->session->target),
2870 udpw->frag_ctx->payload_size);
2872 GNUNET_STATISTICS_update (plugin->env->stats,
2873 "# UDP, fragmented msgs, messages, sent, timeout",
2876 GNUNET_STATISTICS_update (plugin->env->stats,
2877 "# UDP, fragmented msgs, bytes, sent, timeout",
2878 udpw->frag_ctx->payload_size,
2880 /* Remove fragmented message due to timeout */
2881 fragmented_message_done (udpw->frag_ctx,
2885 GNUNET_STATISTICS_update (plugin->env->stats,
2886 "# UDP, total, bytes, sent, timeout",
2889 GNUNET_STATISTICS_update (plugin->env->stats,
2890 "# UDP, total, messages, sent, timeout",
2893 LOG (GNUNET_ERROR_TYPE_DEBUG,
2894 "ACK Message for peer `%s' with size %u timed out\n",
2895 GNUNET_i2s (&udpw->session->target),
2896 udpw->payload_size);
2897 call_continuation (udpw,
2899 removed = GNUNET_YES;
2907 if (sock == plugin->sockv4)
2908 udpw = plugin->ipv4_queue_head;
2909 else if (sock == plugin->sockv6)
2910 udpw = plugin->ipv6_queue_head;
2913 GNUNET_break(0); /* should never happen */
2916 GNUNET_STATISTICS_update (plugin->env->stats,
2917 "# messages discarded due to timeout",
2923 /* Message did not time out, check flow delay */
2924 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2925 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2927 /* this message is not delayed */
2928 LOG (GNUNET_ERROR_TYPE_DEBUG,
2929 "Message for peer `%s' (%u bytes) is not delayed \n",
2930 GNUNET_i2s (&udpw->session->target),
2931 udpw->payload_size);
2932 break; /* Found message to send, break */
2936 /* Message is delayed, try next */
2937 LOG (GNUNET_ERROR_TYPE_DEBUG,
2938 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2939 GNUNET_i2s (&udpw->session->target),
2941 GNUNET_STRINGS_relative_time_to_string (remaining,
2947 if (GNUNET_YES == removed)
2948 notify_session_monitor (session->plugin,
2950 GNUNET_TRANSPORT_SS_UPDATE);
2959 analyze_send_error (struct Plugin *plugin,
2960 const struct sockaddr *sa,
2961 socklen_t slen, int error)
2963 enum GNUNET_ATS_Network_Type type;
2965 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
2966 if (((GNUNET_ATS_NET_LAN == type)
2967 || (GNUNET_ATS_NET_WAN == type))
2968 && ((ENETUNREACH == errno)|| (ENETDOWN == errno)))
2970 if (slen == sizeof (struct sockaddr_in))
2972 /* IPv4: "Network unreachable" or "Network down"
2974 * This indicates we do not have connectivity
2976 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2977 _("UDP could not transmit message to `%s': "
2978 "Network seems down, please check your network configuration\n"),
2979 GNUNET_a2s (sa, slen));
2981 if (slen == sizeof (struct sockaddr_in6))
2983 /* IPv6: "Network unreachable" or "Network down"
2985 * This indicates that this system is IPv6 enabled, but does not
2986 * have a valid global IPv6 address assigned or we do not have
2989 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2990 _("UDP could not transmit IPv6 message! "
2991 "Please check your network configuration and disable IPv6 if your "
2992 "connection does not have a global IPv6 address\n"));
2997 LOG (GNUNET_ERROR_TYPE_WARNING,
2998 "UDP could not transmit message to `%s': `%s'\n",
2999 GNUNET_a2s (sa, slen), STRERROR (error));
3005 * It is time to try to transmit a UDP message. Select one
3008 * @param plugin the plugin
3009 * @param sock which socket (v4/v6) to send on
3010 * @return number of bytes transmitted, #GNUNET_SYSERR on failure
3013 udp_select_send (struct Plugin *plugin,
3014 struct GNUNET_NETWORK_Handle *sock)
3019 const struct IPv4UdpAddress *u4;
3020 struct sockaddr_in a4;
3021 const struct IPv6UdpAddress *u6;
3022 struct sockaddr_in6 a6;
3023 struct UDP_MessageWrapper *udpw;
3025 /* Find message to send */
3026 udpw = remove_timeout_messages_and_select (plugin,
3029 return 0; /* No message to send */
3031 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3033 u4 = udpw->session->address->address;
3034 memset (&a4, 0, sizeof(a4));
3035 a4.sin_family = AF_INET;
3036 #if HAVE_SOCKADDR_IN_SIN_LEN
3037 a4.sin_len = sizeof (a4);
3039 a4.sin_port = u4->u4_port;
3040 memcpy (&a4.sin_addr,
3042 sizeof(struct in_addr));
3043 a = (struct sockaddr *) &a4;
3046 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3048 u6 = udpw->session->address->address;
3049 memset (&a6, 0, sizeof(a6));
3050 a6.sin6_family = AF_INET6;
3051 #if HAVE_SOCKADDR_IN_SIN_LEN
3052 a6.sin6_len = sizeof (a6);
3054 a6.sin6_port = u6->u6_port;
3055 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
3056 a = (struct sockaddr *) &a6;
3061 call_continuation (udpw,
3065 notify_session_monitor (plugin,
3067 GNUNET_TRANSPORT_SS_UPDATE);
3069 return GNUNET_SYSERR;
3071 sent = GNUNET_NETWORK_socket_sendto (sock,
3076 if (GNUNET_SYSERR == sent)
3079 analyze_send_error (plugin,
3083 call_continuation (udpw,
3085 GNUNET_STATISTICS_update (plugin->env->stats,
3086 "# UDP, total, bytes, sent, failure",
3089 GNUNET_STATISTICS_update (plugin->env->stats,
3090 "# UDP, total, messages, sent, failure",
3097 LOG (GNUNET_ERROR_TYPE_DEBUG,
3098 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3099 (unsigned int) (udpw->msg_size),
3100 GNUNET_i2s (&udpw->session->target),
3101 GNUNET_a2s (a, slen),
3103 (sent < 0) ? STRERROR (errno) : "ok");
3104 GNUNET_STATISTICS_update (plugin->env->stats,
3105 "# UDP, total, bytes, sent, success",
3108 GNUNET_STATISTICS_update (plugin->env->stats,
3109 "# UDP, total, messages, sent, success",
3112 if (NULL != udpw->frag_ctx)
3113 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3114 call_continuation (udpw, GNUNET_OK);
3116 dequeue (plugin, udpw);
3117 notify_session_monitor (plugin,
3119 GNUNET_TRANSPORT_SS_UPDATE);
3126 * We have been notified that our readset has something to read. We don't
3127 * know which socket needs to be read, so we have to check each one
3128 * Then reschedule this function to be called again once more is available.
3130 * @param cls the plugin handle
3131 * @param tc the scheduling context (for rescheduling this function again)
3134 udp_plugin_select (void *cls,
3135 const struct GNUNET_SCHEDULER_TaskContext *tc)
3137 struct Plugin *plugin = cls;
3139 plugin->select_task = NULL;
3140 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3142 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
3143 && (NULL != plugin->sockv4)
3144 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
3145 udp_select_read (plugin, plugin->sockv4);
3146 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3147 && (NULL != plugin->sockv4) && (NULL != plugin->ipv4_queue_head)
3148 && (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
3149 udp_select_send (plugin, plugin->sockv4);
3150 schedule_select (plugin);
3155 * We have been notified that our readset has something to read. We don't
3156 * know which socket needs to be read, so we have to check each one
3157 * Then reschedule this function to be called again once more is available.
3159 * @param cls the plugin handle
3160 * @param tc the scheduling context (for rescheduling this function again)
3163 udp_plugin_select_v6 (void *cls,
3164 const struct GNUNET_SCHEDULER_TaskContext *tc)
3166 struct Plugin *plugin = cls;
3168 plugin->select_task_v6 = NULL;
3169 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3171 if (((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
3172 && (NULL != plugin->sockv6)
3173 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
3174 udp_select_read (plugin, plugin->sockv6);
3175 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3176 && (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL )&&
3177 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )udp_select_send (plugin, plugin->sockv6);
3178 schedule_select (plugin);
3183 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3185 * @param plugin the plugin to initialize
3186 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3187 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3188 * @return number of sockets that were successfully bound
3191 setup_sockets (struct Plugin *plugin,
3192 const struct sockaddr_in6 *bind_v6,
3193 const struct sockaddr_in *bind_v4)
3196 int sockets_created = 0;
3197 struct sockaddr_in6 server_addrv6;
3198 struct sockaddr_in server_addrv4;
3199 struct sockaddr *server_addr;
3200 struct sockaddr *addrs[2];
3201 socklen_t addrlens[2];
3205 /* Create IPv6 socket */
3207 if (GNUNET_YES == plugin->enable_ipv6)
3209 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
3210 if (NULL == plugin->sockv6)
3212 LOG(GNUNET_ERROR_TYPE_WARNING,
3213 "Disabling IPv6 since it is not supported on this system!\n");
3214 plugin->enable_ipv6 = GNUNET_NO;
3218 memset (&server_addrv6, '\0', sizeof(struct sockaddr_in6));
3219 #if HAVE_SOCKADDR_IN_SIN_LEN
3220 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3222 server_addrv6.sin6_family = AF_INET6;
3223 if (NULL != bind_v6)
3224 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3226 server_addrv6.sin6_addr = in6addr_any;
3228 if (0 == plugin->port) /* autodetect */
3229 server_addrv6.sin6_port
3230 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3233 server_addrv6.sin6_port = htons (plugin->port);
3234 addrlen = sizeof(struct sockaddr_in6);
3235 server_addr = (struct sockaddr *) &server_addrv6;
3240 LOG(GNUNET_ERROR_TYPE_DEBUG,
3241 "Binding to IPv6 `%s'\n",
3242 GNUNET_a2s (server_addr, addrlen));
3245 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3250 if (0 != plugin->port)
3252 tries = 10; /* fail immediately */
3253 break; /* bind failed on specific port */
3256 server_addrv6.sin6_port
3257 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3263 GNUNET_NETWORK_socket_close (plugin->sockv6);
3264 plugin->enable_ipv6 = GNUNET_NO;
3265 plugin->sockv6 = NULL;
3269 plugin->port = ntohs (server_addrv6.sin6_port);
3271 if (NULL != plugin->sockv6)
3273 LOG (GNUNET_ERROR_TYPE_DEBUG,
3274 "IPv6 socket created on port %s\n",
3275 GNUNET_a2s (server_addr, addrlen));
3276 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
3277 addrlens[sockets_created] = sizeof(struct sockaddr_in6);
3282 LOG (GNUNET_ERROR_TYPE_ERROR,
3283 "Failed to bind UDP socket to %s: %s\n",
3284 GNUNET_a2s (server_addr, addrlen),
3290 /* Create IPv4 socket */
3292 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
3293 if (NULL == plugin->sockv4)
3295 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3297 LOG (GNUNET_ERROR_TYPE_WARNING,
3298 "Disabling IPv4 since it is not supported on this system!\n");
3299 plugin->enable_ipv4 = GNUNET_NO;
3303 memset (&server_addrv4, '\0', sizeof(struct sockaddr_in));
3304 #if HAVE_SOCKADDR_IN_SIN_LEN
3305 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3307 server_addrv4.sin_family = AF_INET;
3308 if (NULL != bind_v4)
3309 server_addrv4.sin_addr = bind_v4->sin_addr;
3311 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3313 if (0 == plugin->port)
3315 server_addrv4.sin_port
3316 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3320 server_addrv4.sin_port = htons (plugin->port);
3322 addrlen = sizeof(struct sockaddr_in);
3323 server_addr = (struct sockaddr *) &server_addrv4;
3328 LOG (GNUNET_ERROR_TYPE_DEBUG,
3329 "Binding to IPv4 `%s'\n",
3330 GNUNET_a2s (server_addr, addrlen));
3334 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3339 if (0 != plugin->port)
3341 tries = 10; /* fail */
3342 break; /* bind failed on specific port */
3346 server_addrv4.sin_port
3347 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3353 GNUNET_NETWORK_socket_close (plugin->sockv4);
3354 plugin->enable_ipv4 = GNUNET_NO;
3355 plugin->sockv4 = NULL;
3359 plugin->port = ntohs (server_addrv4.sin_port);
3362 if (NULL != plugin->sockv4)
3364 LOG (GNUNET_ERROR_TYPE_DEBUG,
3365 "IPv4 socket created on port %s\n",
3366 GNUNET_a2s (server_addr, addrlen));
3367 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
3368 addrlens[sockets_created] = sizeof(struct sockaddr_in);
3373 LOG (GNUNET_ERROR_TYPE_ERROR,
3374 _("Failed to bind UDP socket to %s: %s\n"),
3375 GNUNET_a2s (server_addr, addrlen),
3380 if (0 == sockets_created)
3382 LOG (GNUNET_ERROR_TYPE_WARNING,
3383 _("Failed to open UDP sockets\n"));
3384 return 0; /* No sockets created, return */
3387 /* Create file descriptors */
3388 if (plugin->enable_ipv4 == GNUNET_YES)
3390 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
3391 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
3392 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
3393 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
3394 if (NULL != plugin->sockv4)
3396 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
3397 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
3401 if (plugin->enable_ipv6 == GNUNET_YES)
3403 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
3404 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
3405 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
3406 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
3407 if (NULL != plugin->sockv6)
3409 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
3410 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
3414 schedule_select (plugin);
3415 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3419 (const struct sockaddr **) addrs,
3421 &udp_nat_port_map_callback,
3425 return sockets_created;
3430 * Return information about the given session to the
3433 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3434 * @param peer peer we send information about
3435 * @param value our `struct Session` to send information about
3436 * @return #GNUNET_OK (continue to iterate)
3439 send_session_info_iter (void *cls,
3440 const struct GNUNET_PeerIdentity *peer,
3443 struct Plugin *plugin = cls;
3444 struct Session *session = value;
3446 notify_session_monitor (plugin,
3448 GNUNET_TRANSPORT_SS_INIT);
3449 notify_session_monitor (plugin,
3451 GNUNET_TRANSPORT_SS_UP);
3457 * Begin monitoring sessions of a plugin. There can only
3458 * be one active monitor per plugin (i.e. if there are
3459 * multiple monitors, the transport service needs to
3460 * multiplex the generated events over all of them).
3462 * @param cls closure of the plugin
3463 * @param sic callback to invoke, NULL to disable monitor;
3464 * plugin will being by iterating over all active
3465 * sessions immediately and then enter monitor mode
3466 * @param sic_cls closure for @a sic
3469 udp_plugin_setup_monitor (void *cls,
3470 GNUNET_TRANSPORT_SessionInfoCallback sic,
3473 struct Plugin *plugin = cls;
3476 plugin->sic_cls = sic_cls;
3479 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3480 &send_session_info_iter,
3482 /* signal end of first iteration */
3483 sic (sic_cls, NULL, NULL);
3489 * The exported method. Makes the core api available via a global and
3490 * returns the udp transport API.
3492 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3493 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3496 libgnunet_plugin_transport_udp_init (void *cls)
3498 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3499 struct GNUNET_TRANSPORT_PluginFunctions *api;
3501 unsigned long long port;
3502 unsigned long long aport;
3503 unsigned long long udp_max_bps;
3504 unsigned long long enable_v6;
3505 unsigned long long enable_broadcasting;
3506 unsigned long long enable_broadcasting_recv;
3507 char *bind4_address;
3508 char *bind6_address;
3509 char *fancy_interval;
3510 struct GNUNET_TIME_Relative interval;
3511 struct sockaddr_in server_addrv4;
3512 struct sockaddr_in6 server_addrv6;
3517 if (NULL == env->receive)
3519 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3520 initialze the plugin or the API */
3521 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3523 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3524 api->address_to_string = &udp_address_to_string;
3525 api->string_to_address = &udp_string_to_address;
3529 /* Get port number: port == 0 : autodetect a port,
3530 * > 0 : use this port, not given : 2086 default */
3532 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3537 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3539 "ADVERTISED_PORT", &aport))
3543 LOG (GNUNET_ERROR_TYPE_WARNING,
3544 _("Given `%s' option is out of range: %llu > %u\n"),
3552 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6"))
3553 enable_v6 = GNUNET_NO;
3555 enable_v6 = GNUNET_YES;
3558 have_bind4 = GNUNET_NO;
3559 memset (&server_addrv4, 0, sizeof(server_addrv4));
3561 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3562 "BINDTO", &bind4_address))
3564 LOG (GNUNET_ERROR_TYPE_DEBUG,
3565 "Binding udp plugin to specific address: `%s'\n",
3567 if (1 != inet_pton (AF_INET,
3569 &server_addrv4.sin_addr))
3571 GNUNET_free (bind4_address);
3574 have_bind4 = GNUNET_YES;
3576 GNUNET_free_non_null(bind4_address);
3577 have_bind6 = GNUNET_NO;
3578 memset (&server_addrv6, 0, sizeof(server_addrv6));
3580 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3581 "BINDTO6", &bind6_address))
3583 LOG (GNUNET_ERROR_TYPE_DEBUG,
3584 "Binding udp plugin to specific address: `%s'\n",
3586 if (1 != inet_pton (AF_INET6,
3588 &server_addrv6.sin6_addr))
3590 LOG (GNUNET_ERROR_TYPE_ERROR,
3591 _("Invalid IPv6 address: `%s'\n"),
3593 GNUNET_free (bind6_address);
3596 have_bind6 = GNUNET_YES;
3598 GNUNET_free_non_null (bind6_address);
3600 /* Enable neighbour discovery */
3601 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3602 "transport-udp", "BROADCAST");
3603 if (enable_broadcasting == GNUNET_SYSERR)
3604 enable_broadcasting = GNUNET_NO;
3606 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3607 "transport-udp", "BROADCAST_RECEIVE");
3608 if (enable_broadcasting_recv == GNUNET_SYSERR)
3609 enable_broadcasting_recv = GNUNET_YES;
3611 if (GNUNET_SYSERR ==
3612 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3613 "BROADCAST_INTERVAL",
3616 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3620 if (GNUNET_SYSERR ==
3621 GNUNET_STRINGS_fancy_time_to_relative (fancy_interval, &interval))
3623 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3625 GNUNET_free(fancy_interval);
3628 /* Maximum datarate */
3630 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3631 "MAX_BPS", &udp_max_bps))
3633 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3636 p = GNUNET_new (struct Plugin);
3639 p->broadcast_interval = interval;
3640 p->enable_ipv6 = enable_v6;
3641 p->enable_ipv4 = GNUNET_YES; /* default */
3642 p->enable_broadcasting = enable_broadcasting;
3643 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3645 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3646 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (
3647 GNUNET_CONTAINER_HEAP_ORDER_MIN);
3648 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3650 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3653 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3655 LOG(GNUNET_ERROR_TYPE_DEBUG,
3656 "Setting up sockets\n");
3657 res = setup_sockets (p,
3658 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3659 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3660 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3662 LOG (GNUNET_ERROR_TYPE_ERROR,
3663 _("Failed to create network sockets, plugin failed\n"));
3664 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3665 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3666 GNUNET_SERVER_mst_destroy (p->mst);
3671 /* Setup broadcasting and receiving beacons */
3672 setup_broadcast (p, &server_addrv6, &server_addrv4);
3674 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3677 api->disconnect_session = &udp_disconnect_session;
3678 api->query_keepalive_factor = &udp_query_keepalive_factor;
3679 api->disconnect_peer = &udp_disconnect;
3680 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3681 api->address_to_string = &udp_address_to_string;
3682 api->string_to_address = &udp_string_to_address;
3683 api->check_address = &udp_plugin_check_address;
3684 api->get_session = &udp_plugin_get_session;
3685 api->send = &udp_plugin_send;
3686 api->get_network = &udp_get_network;
3687 api->update_session_timeout = &udp_plugin_update_session_timeout;
3688 api->setup_monitor = &udp_plugin_setup_monitor;
3694 * Function called on each entry in the defragmentation heap to
3698 * @param node node in the heap (to be removed)
3699 * @param element a `struct DefragContext` to be cleaned up
3700 * @param cost unused
3701 * @return #GNUNET_YES
3704 heap_cleanup_iterator (void *cls,
3705 struct GNUNET_CONTAINER_HeapNode *node,
3707 GNUNET_CONTAINER_HeapCostType cost)
3709 struct DefragContext *d_ctx = element;
3711 GNUNET_CONTAINER_heap_remove_node (node);
3712 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3713 GNUNET_free (d_ctx);
3719 * The exported method. Makes the core api available via a global and
3720 * returns the udp transport API.
3722 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3726 libgnunet_plugin_transport_udp_done (void *cls)
3728 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3729 struct Plugin *plugin = api->cls;
3730 struct PrettyPrinterContext *cur;
3731 struct PrettyPrinterContext *next;
3732 struct UDP_MessageWrapper *udpw;
3739 stop_broadcast (plugin);
3740 if (plugin->select_task != NULL)
3742 GNUNET_SCHEDULER_cancel (plugin->select_task);
3743 plugin->select_task = NULL;
3745 if (plugin->select_task_v6 != NULL)
3747 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3748 plugin->select_task_v6 = NULL;
3751 /* Closing sockets */
3752 if (GNUNET_YES == plugin->enable_ipv4)
3754 if (NULL != plugin->sockv4)
3756 GNUNET_break (GNUNET_OK ==
3757 GNUNET_NETWORK_socket_close (plugin->sockv4));
3758 plugin->sockv4 = NULL;
3760 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3761 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3763 if (GNUNET_YES == plugin->enable_ipv6)
3765 if (NULL != plugin->sockv6)
3767 GNUNET_break (GNUNET_OK ==
3768 GNUNET_NETWORK_socket_close (plugin->sockv6));
3769 plugin->sockv6 = NULL;
3771 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3772 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3775 if (NULL != plugin->nat)
3777 GNUNET_NAT_unregister (plugin->nat);
3780 if (NULL != plugin->defrag_ctxs)
3782 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3783 &heap_cleanup_iterator, NULL);
3784 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3785 plugin->defrag_ctxs = NULL;
3787 if (NULL != plugin->mst)
3789 GNUNET_SERVER_mst_destroy (plugin->mst);
3793 /* Clean up leftover messages */
3794 udpw = plugin->ipv4_queue_head;
3795 while (NULL != udpw)
3797 struct UDP_MessageWrapper *tmp = udpw->next;
3798 dequeue (plugin, udpw);
3799 call_continuation (udpw, GNUNET_SYSERR);
3803 udpw = plugin->ipv6_queue_head;
3804 while (NULL != udpw)
3806 struct UDP_MessageWrapper *tmp = udpw->next;
3807 dequeue (plugin, udpw);
3808 call_continuation (udpw, GNUNET_SYSERR);
3813 /* Clean up sessions */
3814 LOG (GNUNET_ERROR_TYPE_DEBUG,
3815 "Cleaning up sessions\n");
3816 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3817 &disconnect_and_free_it, plugin);
3818 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3820 next = plugin->ppc_dll_head;
3821 for (cur = next; NULL != cur; cur = next)
3825 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3826 plugin->ppc_dll_tail,
3828 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3831 GNUNET_free (plugin);
3836 /* end of plugin_transport_udp.c */