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", udpw->msg_size, GNUNET_NO);
1933 plugin->bytes_in_buffer += udpw->msg_size;
1935 GNUNET_STATISTICS_update (plugin->env->stats,
1936 "# UDP, total, msgs in buffers",
1938 if (udpw->session->address->address_length == sizeof (struct IPv4UdpAddress))
1939 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1940 plugin->ipv4_queue_tail,
1942 else if (udpw->session->address->address_length == sizeof (struct IPv6UdpAddress))
1943 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1944 plugin->ipv6_queue_tail,
1951 session->msgs_in_queue++;
1952 session->bytes_in_queue += udpw->msg_size;
1957 * Fragment message was transmitted via UDP, let fragmentation know
1958 * to send the next fragment now.
1960 * @param cls the `struct UDPMessageWrapper *` of the fragment
1961 * @param target destination peer (ignored)
1962 * @param result #GNUNET_OK on success (ignored)
1963 * @param payload bytes payload sent
1964 * @param physical bytes physical sent
1967 send_next_fragment (void *cls,
1968 const struct GNUNET_PeerIdentity *target,
1973 struct UDP_MessageWrapper *udpw = cls;
1975 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1980 * Function that is called with messages created by the fragmentation
1981 * module. In the case of the 'proc' callback of the
1982 * #GNUNET_FRAGMENT_context_create() function, this function must
1983 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1985 * @param cls closure, the 'struct FragmentationContext'
1986 * @param msg the message that was created
1989 enqueue_fragment (void *cls,
1990 const struct GNUNET_MessageHeader *msg)
1992 struct UDP_FragmentationContext *frag_ctx = cls;
1993 struct Plugin *plugin = frag_ctx->plugin;
1994 struct UDP_MessageWrapper * udpw;
1995 size_t msg_len = ntohs (msg->size);
1997 LOG (GNUNET_ERROR_TYPE_DEBUG,
1998 "Enqueuing fragment with %u bytes\n",
2000 frag_ctx->fragments_used++;
2001 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
2002 udpw->session = frag_ctx->session;
2003 udpw->msg_buf = (char *) &udpw[1];
2004 udpw->msg_size = msg_len;
2005 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
2006 udpw->cont = &send_next_fragment;
2007 udpw->cont_cls = udpw;
2008 udpw->timeout = frag_ctx->timeout;
2009 udpw->frag_ctx = frag_ctx;
2010 udpw->msg_type = UMT_MSG_FRAGMENTED;
2011 memcpy (udpw->msg_buf, msg, msg_len);
2012 enqueue (plugin, udpw);
2013 schedule_select (plugin);
2018 * Function that can be used by the transport service to transmit
2019 * a message using the plugin. Note that in the case of a
2020 * peer disconnecting, the continuation MUST be called
2021 * prior to the disconnect notification itself. This function
2022 * will be called with this peer's HELLO message to initiate
2023 * a fresh connection to another peer.
2025 * @param cls closure
2026 * @param s which session must be used
2027 * @param msgbuf the message to transmit
2028 * @param msgbuf_size number of bytes in 'msgbuf'
2029 * @param priority how important is the message (most plugins will
2030 * ignore message priority and just FIFO)
2031 * @param to how long to wait at most for the transmission (does not
2032 * require plugins to discard the message after the timeout,
2033 * just advisory for the desired delay; most plugins will ignore
2035 * @param cont continuation to call once the message has
2036 * been transmitted (or if the transport is ready
2037 * for the next transmission call; or if the
2038 * peer disconnected...); can be NULL
2039 * @param cont_cls closure for cont
2040 * @return number of bytes used (on the physical network, with overheads);
2041 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2042 * and does NOT mean that the message was not transmitted (DV)
2045 udp_plugin_send (void *cls,
2049 unsigned int priority,
2050 struct GNUNET_TIME_Relative to,
2051 GNUNET_TRANSPORT_TransmitContinuation cont,
2054 struct Plugin *plugin = cls;
2055 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2056 struct UDP_FragmentationContext * frag_ctx;
2057 struct UDP_MessageWrapper * udpw;
2058 struct UDPMessage *udp;
2061 if ( (s->address->address_length == sizeof(struct IPv6UdpAddress)) &&
2062 (plugin->sockv6 == NULL) )
2063 return GNUNET_SYSERR;
2064 if ( (s->address->address_length == sizeof(struct IPv4UdpAddress)) &&
2065 (plugin->sockv4 == NULL) )
2066 return GNUNET_SYSERR;
2067 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2070 return GNUNET_SYSERR;
2073 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2078 return GNUNET_SYSERR;
2080 LOG (GNUNET_ERROR_TYPE_DEBUG,
2081 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2083 GNUNET_i2s (&s->target),
2084 udp_address_to_string (plugin,
2085 s->address->address,
2086 s->address->address_length));
2089 udp = (struct UDPMessage *) mbuf;
2090 udp->header.size = htons (udpmlen);
2091 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2092 udp->reserved = htonl (0);
2093 udp->sender = *plugin->env->my_identity;
2095 /* We do not update the session time out here!
2096 * Otherwise this session will not timeout since we send keep alive before
2097 * session can timeout
2099 * For UDP we update session timeout only on receive, this will cover keep
2100 * alives, since remote peer will reply with keep alive response!
2102 if (udpmlen <= UDP_MTU)
2104 /* unfragmented message */
2105 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2107 udpw->msg_buf = (char *) &udpw[1];
2108 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2109 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2110 udpw->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to);
2112 udpw->cont_cls = cont_cls;
2113 udpw->frag_ctx = NULL;
2114 udpw->msg_type = UMT_MSG_UNFRAGMENTED;
2115 memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
2116 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)], msgbuf, msgbuf_size);
2117 enqueue (plugin, udpw);
2119 GNUNET_STATISTICS_update (plugin->env->stats,
2120 "# UDP, unfragmented msgs, messages, attempt", 1, GNUNET_NO);
2121 GNUNET_STATISTICS_update (plugin->env->stats,
2122 "# UDP, unfragmented msgs, bytes payload, attempt",
2128 /* fragmented message */
2129 if (s->frag_ctx != NULL)
2130 return GNUNET_SYSERR;
2131 memcpy (&udp[1], msgbuf, msgbuf_size);
2132 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2133 frag_ctx->plugin = plugin;
2134 frag_ctx->session = s;
2135 frag_ctx->cont = cont;
2136 frag_ctx->cont_cls = cont_cls;
2137 frag_ctx->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
2139 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2140 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2141 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2144 s->last_expected_msg_delay,
2145 s->last_expected_ack_delay,
2149 s->frag_ctx = frag_ctx;
2150 GNUNET_STATISTICS_update (plugin->env->stats,
2151 "# UDP, fragmented msgs, messages, pending",
2154 GNUNET_STATISTICS_update (plugin->env->stats,
2155 "# UDP, fragmented msgs, messages, attempt",
2158 GNUNET_STATISTICS_update (plugin->env->stats,
2159 "# UDP, fragmented msgs, bytes payload, attempt",
2160 frag_ctx->payload_size,
2163 notify_session_monitor (s->plugin,
2165 GNUNET_TRANSPORT_SS_UPDATE);
2166 schedule_select (plugin);
2172 * Our external IP address/port mapping has changed.
2174 * @param cls closure, the `struct LocalAddrList`
2175 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2176 * the previous (now invalid) one
2177 * @param addr either the previous or the new public IP address
2178 * @param addrlen actual lenght of the address
2181 udp_nat_port_map_callback (void *cls,
2183 const struct sockaddr *addr,
2186 struct Plugin *plugin = cls;
2187 struct GNUNET_HELLO_Address *address;
2188 struct IPv4UdpAddress u4;
2189 struct IPv6UdpAddress u6;
2193 LOG (GNUNET_ERROR_TYPE_INFO,
2194 "NAT notification to %s address `%s'\n",
2195 (GNUNET_YES == add_remove) ? "add" : "remove",
2196 GNUNET_a2s (addr, addrlen));
2198 /* convert 'address' to our internal format */
2199 switch (addr->sa_family)
2202 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
2203 memset (&u4, 0, sizeof(u4));
2204 u4.options = htonl (plugin->myoptions);
2205 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2206 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2207 if (0 == ((struct sockaddr_in *) addr)->sin_port)
2210 args = sizeof(struct IPv4UdpAddress);
2213 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
2214 memset (&u6, 0, sizeof(u6));
2215 u6.options = htonl (plugin->myoptions);
2216 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
2218 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2219 sizeof(struct in6_addr));
2220 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2222 args = sizeof(struct IPv6UdpAddress);
2228 /* modify our published address list */
2229 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2232 GNUNET_HELLO_ADDRESS_INFO_NONE);
2233 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2234 GNUNET_HELLO_address_free (address);
2239 * Message tokenizer has broken up an incomming message. Pass it on
2242 * @param cls the `struct Plugin *`
2243 * @param client the `struct SourceInformation *`
2244 * @param hdr the actual message
2245 * @return #GNUNET_OK (always)
2248 process_inbound_tokenized_messages (void *cls,
2250 const struct GNUNET_MessageHeader *hdr)
2252 struct Plugin *plugin = cls;
2253 struct SourceInformation *si = client;
2254 struct GNUNET_TIME_Relative delay;
2256 GNUNET_assert (NULL != si->session);
2257 if (GNUNET_YES == si->session->in_destroy)
2260 reschedule_session_timeout (si->session);
2261 delay = plugin->env->receive (plugin->env->cls,
2262 si->session->address,
2265 si->session->flow_delay_for_other_peer = delay;
2271 * We've received a UDP Message. Process it (pass contents to main service).
2273 * @param plugin plugin context
2274 * @param msg the message
2275 * @param udp_addr sender address
2276 * @param udp_addr_len number of bytes in @a udp_addr
2277 * @param network_type network type the address belongs to
2280 process_udp_message (struct Plugin *plugin,
2281 const struct UDPMessage *msg,
2282 const union UdpAddress *udp_addr,
2283 size_t udp_addr_len,
2284 enum GNUNET_ATS_Network_Type network_type)
2286 struct SourceInformation si;
2288 struct GNUNET_HELLO_Address *address;
2290 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
2291 if (0 != ntohl (msg->reserved))
2296 if (ntohs (msg->header.size)
2297 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2303 address = GNUNET_HELLO_address_allocate (&msg->sender,
2307 GNUNET_HELLO_ADDRESS_INFO_NONE);
2309 (s = udp_plugin_lookup_session (plugin, address)))
2311 s = udp_plugin_create_session (plugin,
2314 plugin->env->session_start (plugin->env->cls,
2318 notify_session_monitor (s->plugin,
2320 GNUNET_TRANSPORT_SS_INIT);
2321 notify_session_monitor (s->plugin,
2323 GNUNET_TRANSPORT_SS_UP);
2325 GNUNET_free (address);
2327 /* iterate over all embedded messages */
2329 si.sender = msg->sender;
2331 GNUNET_SERVER_mst_receive (plugin->mst,
2333 (const char *) &msg[1],
2334 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2338 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
2344 * Process a defragmented message.
2346 * @param cls the `struct DefragContext *`
2347 * @param msg the message
2350 fragment_msg_proc (void *cls,
2351 const struct GNUNET_MessageHeader *msg)
2353 struct DefragContext *rc = cls;
2354 const struct UDPMessage *um;
2356 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2361 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2366 um = (const struct UDPMessage *) msg;
2367 rc->sender = um->sender;
2368 rc->have_sender = GNUNET_YES;
2369 process_udp_message (rc->plugin,
2378 * Transmit an acknowledgement.
2380 * @param cls the `struct DefragContext *`
2381 * @param id message ID (unused)
2382 * @param msg ack to transmit
2385 ack_proc (void *cls,
2387 const struct GNUNET_MessageHeader *msg)
2389 struct DefragContext *rc = cls;
2390 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2391 struct UDP_ACK_Message *udp_ack;
2393 struct UDP_MessageWrapper *udpw;
2395 struct GNUNET_HELLO_Address *address;
2397 if (GNUNET_NO == rc->have_sender)
2399 /* tried to defragment but never succeeded, hence will not ACK */
2400 GNUNET_break_op (0);
2403 address = GNUNET_HELLO_address_allocate (&rc->sender,
2407 GNUNET_HELLO_ADDRESS_INFO_NONE);
2408 s = udp_plugin_lookup_session (rc->plugin,
2410 GNUNET_HELLO_address_free (address);
2413 LOG (GNUNET_ERROR_TYPE_ERROR,
2414 "Trying to transmit ACK to peer `%s' but no session found!\n",
2415 udp_address_to_string (rc->plugin,
2418 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2419 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2423 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2424 delay = s->flow_delay_for_other_peer.rel_value_us;
2426 LOG (GNUNET_ERROR_TYPE_DEBUG,
2427 "Sending ACK to `%s' including delay of %s\n",
2428 udp_address_to_string (rc->plugin,
2431 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2433 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2434 udpw->msg_size = msize;
2435 udpw->payload_size = 0;
2437 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2438 udpw->msg_buf = (char *) &udpw[1];
2439 udpw->msg_type = UMT_MSG_ACK;
2440 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2441 udp_ack->header.size = htons ((uint16_t) msize);
2442 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2443 udp_ack->delay = htonl (delay);
2444 udp_ack->sender = *rc->plugin->env->my_identity;
2445 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2446 enqueue (rc->plugin, udpw);
2447 notify_session_monitor (s->plugin,
2449 GNUNET_TRANSPORT_SS_UPDATE);
2450 schedule_select (rc->plugin);
2455 * Handle an ACK message.
2457 * @param plugin the UDP plugin
2458 * @param msg the (presumed) UDP ACK message
2459 * @param udp_addr sender address
2460 * @param udp_addr_len number of bytes in @a udp_addr
2463 read_process_ack (struct Plugin *plugin,
2464 const struct GNUNET_MessageHeader *msg,
2465 const union UdpAddress *udp_addr,
2466 socklen_t udp_addr_len)
2468 const struct GNUNET_MessageHeader *ack;
2469 const struct UDP_ACK_Message *udp_ack;
2470 struct GNUNET_HELLO_Address *address;
2472 struct GNUNET_TIME_Relative flow_delay;
2474 if (ntohs (msg->size)
2475 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2480 udp_ack = (const struct UDP_ACK_Message *) msg;
2481 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2485 GNUNET_HELLO_ADDRESS_INFO_NONE);
2486 s = udp_plugin_lookup_session (plugin,
2490 LOG (GNUNET_ERROR_TYPE_WARNING,
2491 "UDP session of address %s for ACK not found\n",
2492 udp_address_to_string (plugin,
2494 address->address_length));
2495 GNUNET_HELLO_address_free (address);
2498 if (NULL == s->frag_ctx)
2500 LOG (GNUNET_ERROR_TYPE_WARNING,
2501 "Fragmentation context of address %s for ACK not found\n",
2502 udp_address_to_string (plugin,
2504 address->address_length));
2505 GNUNET_HELLO_address_free (address);
2508 GNUNET_HELLO_address_free (address);
2510 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2511 LOG (GNUNET_ERROR_TYPE_DEBUG,
2512 "We received a sending delay of %s\n",
2513 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2515 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2517 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2518 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2525 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2528 LOG(GNUNET_ERROR_TYPE_DEBUG,
2529 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2530 (unsigned int ) ntohs (msg->size),
2531 GNUNET_i2s (&udp_ack->sender),
2532 udp_address_to_string (plugin,
2535 /* Expect more ACKs to arrive */
2539 LOG (GNUNET_ERROR_TYPE_DEBUG,
2540 "Message full ACK'ed\n",
2541 (unsigned int ) ntohs (msg->size),
2542 GNUNET_i2s (&udp_ack->sender),
2543 udp_address_to_string (plugin,
2547 /* Remove fragmented message after successful sending */
2548 fragmented_message_done (s->frag_ctx,
2554 * We received a fragment, process it.
2556 * @param plugin our plugin
2557 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2558 * @param udp_addr sender address
2559 * @param udp_addr_len number of bytes in @a udp_addr
2560 * @param network_type network type the address belongs to
2563 read_process_fragment (struct Plugin *plugin,
2564 const struct GNUNET_MessageHeader *msg,
2565 const union UdpAddress *udp_addr,
2566 size_t udp_addr_len,
2567 enum GNUNET_ATS_Network_Type network_type)
2569 struct DefragContext *d_ctx;
2570 struct GNUNET_TIME_Absolute now;
2571 struct FindReceiveContext frc;
2574 frc.udp_addr = udp_addr;
2575 frc.udp_addr_len = udp_addr_len;
2577 /* Lookup existing receive context for this address */
2578 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2579 &find_receive_context,
2581 now = GNUNET_TIME_absolute_get ();
2586 /* Create a new defragmentation context */
2587 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2591 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2592 d_ctx->udp_addr_len = udp_addr_len;
2593 d_ctx->network_type = network_type;
2594 d_ctx->plugin = plugin;
2595 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2597 UDP_MAX_MESSAGES_IN_DEFRAG,
2601 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2603 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2604 LOG (GNUNET_ERROR_TYPE_DEBUG,
2605 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2606 (unsigned int ) ntohs (msg->size),
2607 udp_address_to_string (plugin,
2613 LOG (GNUNET_ERROR_TYPE_DEBUG,
2614 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2615 (unsigned int ) ntohs (msg->size),
2616 udp_address_to_string (plugin,
2622 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2624 /* keep this 'rc' from expiring */
2625 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2627 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2629 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2630 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2632 /* remove 'rc' that was inactive the longest */
2633 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2634 GNUNET_assert (NULL != d_ctx);
2635 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2636 GNUNET_free (d_ctx);
2642 * Read and process a message from the given socket.
2644 * @param plugin the overall plugin
2645 * @param rsock socket to read from
2648 udp_select_read (struct Plugin *plugin,
2649 struct GNUNET_NETWORK_Handle *rsock)
2652 struct sockaddr_storage addr;
2653 char buf[65536] GNUNET_ALIGN;
2655 const struct GNUNET_MessageHeader *msg;
2656 struct IPv4UdpAddress v4;
2657 struct IPv6UdpAddress v6;
2658 const struct sockaddr *sa;
2659 const struct sockaddr_in *sa4;
2660 const struct sockaddr_in6 *sa6;
2661 const union UdpAddress *int_addr;
2662 size_t int_addr_len;
2663 enum GNUNET_ATS_Network_Type network_type;
2665 fromlen = sizeof(addr);
2666 memset (&addr, 0, sizeof(addr));
2667 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof(buf),
2668 (struct sockaddr *) &addr, &fromlen);
2669 sa = (const struct sockaddr *) &addr;
2671 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2672 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2673 * on this socket has failed.
2675 * WSAECONNRESET - The virtual circuit was reset by the remote side
2676 * executing a hard or abortive close. The application should close
2677 * the socket; it is no longer usable. On a UDP-datagram socket this
2678 * error indicates a previous send operation resulted in an ICMP Port
2679 * Unreachable message.
2681 if ( (-1 == size) && (ECONNRESET == errno) )
2686 LOG (GNUNET_ERROR_TYPE_DEBUG,
2687 "UDP failed to receive data: %s\n",
2689 /* Connection failure or something. Not a protocol violation. */
2692 if (size < sizeof(struct GNUNET_MessageHeader))
2694 LOG (GNUNET_ERROR_TYPE_WARNING,
2695 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2696 (unsigned int ) size,
2697 GNUNET_a2s (sa, fromlen));
2698 /* _MAY_ be a connection failure (got partial message) */
2699 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2703 msg = (const struct GNUNET_MessageHeader *) buf;
2704 LOG (GNUNET_ERROR_TYPE_DEBUG,
2705 "UDP received %u-byte message from `%s' type %u\n",
2706 (unsigned int) size,
2710 if (size != ntohs (msg->size))
2712 LOG (GNUNET_ERROR_TYPE_WARNING,
2713 "UDP malformed message header from %s\n",
2714 (unsigned int) size,
2717 GNUNET_break_op (0);
2720 GNUNET_STATISTICS_update (plugin->env->stats,
2721 "# UDP, total, bytes, received",
2724 network_type = plugin->env->get_address_type (plugin->env->cls,
2727 switch (sa->sa_family)
2730 sa4 = (const struct sockaddr_in *) &addr;
2732 v4.ipv4_addr = sa4->sin_addr.s_addr;
2733 v4.u4_port = sa4->sin_port;
2734 int_addr = (union UdpAddress *) &v4;
2735 int_addr_len = sizeof (v4);
2738 sa6 = (const struct sockaddr_in6 *) &addr;
2740 v6.ipv6_addr = sa6->sin6_addr;
2741 v6.u6_port = sa6->sin6_port;
2742 int_addr = (union UdpAddress *) &v6;
2743 int_addr_len = sizeof (v6);
2750 switch (ntohs (msg->type))
2752 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2753 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2754 udp_broadcast_receive (plugin,
2761 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2762 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2767 process_udp_message (plugin,
2768 (const struct UDPMessage *) msg,
2773 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2774 read_process_ack (plugin,
2779 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2780 read_process_fragment (plugin,
2794 * Removes messages from the transmission queue that have
2795 * timed out, and then selects a message that should be
2798 * @param plugin the UDP plugin
2799 * @param sock which socket should we process the queue for (v4 or v6)
2800 * @return message selected for transmission, or NULL for none
2802 static struct UDP_MessageWrapper *
2803 remove_timeout_messages_and_select (struct Plugin *plugin,
2804 struct GNUNET_NETWORK_Handle *sock)
2806 struct UDP_MessageWrapper *udpw = NULL;
2807 struct GNUNET_TIME_Relative remaining;
2808 struct Session *session;
2811 removed = GNUNET_NO;
2812 udpw = (sock == plugin->sockv4)
2813 ? plugin->ipv4_queue_head
2814 : plugin->ipv6_queue_head;
2815 while (NULL != udpw)
2817 session = udpw->session;
2818 /* Find messages with timeout */
2819 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2820 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2822 /* Message timed out */
2823 switch (udpw->msg_type)
2825 case UMT_MSG_UNFRAGMENTED:
2826 GNUNET_STATISTICS_update (plugin->env->stats,
2827 "# UDP, total, bytes, sent, timeout",
2830 GNUNET_STATISTICS_update (plugin->env->stats,
2831 "# UDP, total, messages, sent, timeout",
2834 GNUNET_STATISTICS_update (plugin->env->stats,
2835 "# UDP, unfragmented msgs, messages, sent, timeout",
2838 GNUNET_STATISTICS_update (plugin->env->stats,
2839 "# UDP, unfragmented msgs, bytes, sent, timeout",
2842 /* Not fragmented message */
2843 LOG (GNUNET_ERROR_TYPE_DEBUG,
2844 "Message for peer `%s' with size %u timed out\n",
2845 GNUNET_i2s (&udpw->session->target),
2846 udpw->payload_size);
2847 call_continuation (udpw, GNUNET_SYSERR);
2848 /* Remove message */
2849 removed = GNUNET_YES;
2850 dequeue (plugin, udpw);
2853 case UMT_MSG_FRAGMENTED:
2854 /* Fragmented message */
2855 GNUNET_STATISTICS_update (plugin->env->stats,
2856 "# UDP, total, bytes, sent, timeout",
2857 udpw->frag_ctx->on_wire_size,
2859 GNUNET_STATISTICS_update (plugin->env->stats,
2860 "# UDP, total, messages, sent, timeout",
2863 call_continuation (udpw,
2865 LOG (GNUNET_ERROR_TYPE_DEBUG,
2866 "Fragment for message for peer `%s' with size %u timed out\n",
2867 GNUNET_i2s (&udpw->session->target),
2868 udpw->frag_ctx->payload_size);
2870 GNUNET_STATISTICS_update (plugin->env->stats,
2871 "# UDP, fragmented msgs, messages, sent, timeout",
2874 GNUNET_STATISTICS_update (plugin->env->stats,
2875 "# UDP, fragmented msgs, bytes, sent, timeout",
2876 udpw->frag_ctx->payload_size,
2878 /* Remove fragmented message due to timeout */
2879 fragmented_message_done (udpw->frag_ctx,
2883 GNUNET_STATISTICS_update (plugin->env->stats,
2884 "# UDP, total, bytes, sent, timeout",
2887 GNUNET_STATISTICS_update (plugin->env->stats,
2888 "# UDP, total, messages, sent, timeout",
2891 LOG (GNUNET_ERROR_TYPE_DEBUG,
2892 "ACK Message for peer `%s' with size %u timed out\n",
2893 GNUNET_i2s (&udpw->session->target),
2894 udpw->payload_size);
2895 call_continuation (udpw,
2897 removed = GNUNET_YES;
2905 if (sock == plugin->sockv4)
2906 udpw = plugin->ipv4_queue_head;
2907 else if (sock == plugin->sockv6)
2908 udpw = plugin->ipv6_queue_head;
2911 GNUNET_break(0); /* should never happen */
2914 GNUNET_STATISTICS_update (plugin->env->stats,
2915 "# messages discarded due to timeout",
2921 /* Message did not time out, check flow delay */
2922 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2923 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2925 /* this message is not delayed */
2926 LOG (GNUNET_ERROR_TYPE_DEBUG,
2927 "Message for peer `%s' (%u bytes) is not delayed \n",
2928 GNUNET_i2s (&udpw->session->target),
2929 udpw->payload_size);
2930 break; /* Found message to send, break */
2934 /* Message is delayed, try next */
2935 LOG (GNUNET_ERROR_TYPE_DEBUG,
2936 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2937 GNUNET_i2s (&udpw->session->target),
2939 GNUNET_STRINGS_relative_time_to_string (remaining,
2945 if (GNUNET_YES == removed)
2946 notify_session_monitor (session->plugin,
2948 GNUNET_TRANSPORT_SS_UPDATE);
2957 analyze_send_error (struct Plugin *plugin,
2958 const struct sockaddr *sa,
2959 socklen_t slen, int error)
2961 enum GNUNET_ATS_Network_Type type;
2963 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
2964 if (((GNUNET_ATS_NET_LAN == type)
2965 || (GNUNET_ATS_NET_WAN == type))
2966 && ((ENETUNREACH == errno)|| (ENETDOWN == errno)))
2968 if (slen == sizeof (struct sockaddr_in))
2970 /* IPv4: "Network unreachable" or "Network down"
2972 * This indicates we do not have connectivity
2974 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2975 _("UDP could not transmit message to `%s': "
2976 "Network seems down, please check your network configuration\n"),
2977 GNUNET_a2s (sa, slen));
2979 if (slen == sizeof (struct sockaddr_in6))
2981 /* IPv6: "Network unreachable" or "Network down"
2983 * This indicates that this system is IPv6 enabled, but does not
2984 * have a valid global IPv6 address assigned or we do not have
2987 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2988 _("UDP could not transmit IPv6 message! "
2989 "Please check your network configuration and disable IPv6 if your "
2990 "connection does not have a global IPv6 address\n"));
2995 LOG (GNUNET_ERROR_TYPE_WARNING,
2996 "UDP could not transmit message to `%s': `%s'\n",
2997 GNUNET_a2s (sa, slen), STRERROR (error));
3003 * It is time to try to transmit a UDP message. Select one
3006 * @param plugin the plugin
3007 * @param sock which socket (v4/v6) to send on
3008 * @return number of bytes transmitted, #GNUNET_SYSERR on failure
3011 udp_select_send (struct Plugin *plugin,
3012 struct GNUNET_NETWORK_Handle *sock)
3017 const struct IPv4UdpAddress *u4;
3018 struct sockaddr_in a4;
3019 const struct IPv6UdpAddress *u6;
3020 struct sockaddr_in6 a6;
3021 struct UDP_MessageWrapper *udpw;
3023 /* Find message to send */
3024 udpw = remove_timeout_messages_and_select (plugin,
3027 return 0; /* No message to send */
3029 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
3031 u4 = udpw->session->address->address;
3032 memset (&a4, 0, sizeof(a4));
3033 a4.sin_family = AF_INET;
3034 #if HAVE_SOCKADDR_IN_SIN_LEN
3035 a4.sin_len = sizeof (a4);
3037 a4.sin_port = u4->u4_port;
3038 memcpy (&a4.sin_addr,
3040 sizeof(struct in_addr));
3041 a = (struct sockaddr *) &a4;
3044 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
3046 u6 = udpw->session->address->address;
3047 memset (&a6, 0, sizeof(a6));
3048 a6.sin6_family = AF_INET6;
3049 #if HAVE_SOCKADDR_IN_SIN_LEN
3050 a6.sin6_len = sizeof (a6);
3052 a6.sin6_port = u6->u6_port;
3053 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
3054 a = (struct sockaddr *) &a6;
3059 call_continuation (udpw,
3063 notify_session_monitor (plugin,
3065 GNUNET_TRANSPORT_SS_UPDATE);
3067 return GNUNET_SYSERR;
3069 sent = GNUNET_NETWORK_socket_sendto (sock,
3074 if (GNUNET_SYSERR == sent)
3077 analyze_send_error (plugin,
3081 call_continuation (udpw,
3083 GNUNET_STATISTICS_update (plugin->env->stats,
3084 "# UDP, total, bytes, sent, failure",
3087 GNUNET_STATISTICS_update (plugin->env->stats,
3088 "# UDP, total, messages, sent, failure",
3095 LOG (GNUNET_ERROR_TYPE_DEBUG,
3096 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3097 (unsigned int) (udpw->msg_size),
3098 GNUNET_i2s (&udpw->session->target),
3099 GNUNET_a2s (a, slen),
3101 (sent < 0) ? STRERROR (errno) : "ok");
3102 GNUNET_STATISTICS_update (plugin->env->stats,
3103 "# UDP, total, bytes, sent, success",
3106 GNUNET_STATISTICS_update (plugin->env->stats,
3107 "# UDP, total, messages, sent, success",
3110 if (NULL != udpw->frag_ctx)
3111 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3112 call_continuation (udpw, GNUNET_OK);
3114 dequeue (plugin, udpw);
3115 notify_session_monitor (plugin,
3117 GNUNET_TRANSPORT_SS_UPDATE);
3124 * We have been notified that our readset has something to read. We don't
3125 * know which socket needs to be read, so we have to check each one
3126 * Then reschedule this function to be called again once more is available.
3128 * @param cls the plugin handle
3129 * @param tc the scheduling context (for rescheduling this function again)
3132 udp_plugin_select (void *cls,
3133 const struct GNUNET_SCHEDULER_TaskContext *tc)
3135 struct Plugin *plugin = cls;
3137 plugin->select_task = NULL;
3138 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3140 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
3141 && (NULL != plugin->sockv4)
3142 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
3143 udp_select_read (plugin, plugin->sockv4);
3144 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3145 && (NULL != plugin->sockv4) && (NULL != plugin->ipv4_queue_head)
3146 && (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
3147 udp_select_send (plugin, plugin->sockv4);
3148 schedule_select (plugin);
3153 * We have been notified that our readset has something to read. We don't
3154 * know which socket needs to be read, so we have to check each one
3155 * Then reschedule this function to be called again once more is available.
3157 * @param cls the plugin handle
3158 * @param tc the scheduling context (for rescheduling this function again)
3161 udp_plugin_select_v6 (void *cls,
3162 const struct GNUNET_SCHEDULER_TaskContext *tc)
3164 struct Plugin *plugin = cls;
3166 plugin->select_task_v6 = NULL;
3167 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3169 if (((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
3170 && (NULL != plugin->sockv6)
3171 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
3172 udp_select_read (plugin, plugin->sockv6);
3173 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3174 && (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL )&&
3175 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )udp_select_send (plugin, plugin->sockv6);
3176 schedule_select (plugin);
3181 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3183 * @param plugin the plugin to initialize
3184 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3185 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3186 * @return number of sockets that were successfully bound
3189 setup_sockets (struct Plugin *plugin,
3190 const struct sockaddr_in6 *bind_v6,
3191 const struct sockaddr_in *bind_v4)
3194 int sockets_created = 0;
3195 struct sockaddr_in6 server_addrv6;
3196 struct sockaddr_in server_addrv4;
3197 struct sockaddr *server_addr;
3198 struct sockaddr *addrs[2];
3199 socklen_t addrlens[2];
3203 /* Create IPv6 socket */
3205 if (GNUNET_YES == plugin->enable_ipv6)
3207 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
3208 if (NULL == plugin->sockv6)
3210 LOG(GNUNET_ERROR_TYPE_WARNING,
3211 "Disabling IPv6 since it is not supported on this system!\n");
3212 plugin->enable_ipv6 = GNUNET_NO;
3216 memset (&server_addrv6, '\0', sizeof(struct sockaddr_in6));
3217 #if HAVE_SOCKADDR_IN_SIN_LEN
3218 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3220 server_addrv6.sin6_family = AF_INET6;
3221 if (NULL != bind_v6)
3222 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3224 server_addrv6.sin6_addr = in6addr_any;
3226 if (0 == plugin->port) /* autodetect */
3227 server_addrv6.sin6_port
3228 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3231 server_addrv6.sin6_port = htons (plugin->port);
3232 addrlen = sizeof(struct sockaddr_in6);
3233 server_addr = (struct sockaddr *) &server_addrv6;
3238 LOG(GNUNET_ERROR_TYPE_DEBUG,
3239 "Binding to IPv6 `%s'\n",
3240 GNUNET_a2s (server_addr, addrlen));
3243 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3248 if (0 != plugin->port)
3250 tries = 10; /* fail immediately */
3251 break; /* bind failed on specific port */
3254 server_addrv6.sin6_port
3255 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3261 GNUNET_NETWORK_socket_close (plugin->sockv6);
3262 plugin->enable_ipv6 = GNUNET_NO;
3263 plugin->sockv6 = NULL;
3267 plugin->port = ntohs (server_addrv6.sin6_port);
3269 if (NULL != plugin->sockv6)
3271 LOG (GNUNET_ERROR_TYPE_DEBUG,
3272 "IPv6 socket created on port %s\n",
3273 GNUNET_a2s (server_addr, addrlen));
3274 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
3275 addrlens[sockets_created] = sizeof(struct sockaddr_in6);
3280 LOG (GNUNET_ERROR_TYPE_ERROR,
3281 "Failed to bind UDP socket to %s: %s\n",
3282 GNUNET_a2s (server_addr, addrlen),
3288 /* Create IPv4 socket */
3290 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
3291 if (NULL == plugin->sockv4)
3293 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3295 LOG (GNUNET_ERROR_TYPE_WARNING,
3296 "Disabling IPv4 since it is not supported on this system!\n");
3297 plugin->enable_ipv4 = GNUNET_NO;
3301 memset (&server_addrv4, '\0', sizeof(struct sockaddr_in));
3302 #if HAVE_SOCKADDR_IN_SIN_LEN
3303 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3305 server_addrv4.sin_family = AF_INET;
3306 if (NULL != bind_v4)
3307 server_addrv4.sin_addr = bind_v4->sin_addr;
3309 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3311 if (0 == plugin->port)
3313 server_addrv4.sin_port
3314 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3318 server_addrv4.sin_port = htons (plugin->port);
3320 addrlen = sizeof(struct sockaddr_in);
3321 server_addr = (struct sockaddr *) &server_addrv4;
3326 LOG (GNUNET_ERROR_TYPE_DEBUG,
3327 "Binding to IPv4 `%s'\n",
3328 GNUNET_a2s (server_addr, addrlen));
3332 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3337 if (0 != plugin->port)
3339 tries = 10; /* fail */
3340 break; /* bind failed on specific port */
3344 server_addrv4.sin_port
3345 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3351 GNUNET_NETWORK_socket_close (plugin->sockv4);
3352 plugin->enable_ipv4 = GNUNET_NO;
3353 plugin->sockv4 = NULL;
3357 plugin->port = ntohs (server_addrv4.sin_port);
3360 if (NULL != plugin->sockv4)
3362 LOG (GNUNET_ERROR_TYPE_DEBUG,
3363 "IPv4 socket created on port %s\n",
3364 GNUNET_a2s (server_addr, addrlen));
3365 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
3366 addrlens[sockets_created] = sizeof(struct sockaddr_in);
3371 LOG (GNUNET_ERROR_TYPE_ERROR,
3372 _("Failed to bind UDP socket to %s: %s\n"),
3373 GNUNET_a2s (server_addr, addrlen),
3378 if (0 == sockets_created)
3380 LOG (GNUNET_ERROR_TYPE_WARNING,
3381 _("Failed to open UDP sockets\n"));
3382 return 0; /* No sockets created, return */
3385 /* Create file descriptors */
3386 if (plugin->enable_ipv4 == GNUNET_YES)
3388 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
3389 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
3390 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
3391 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
3392 if (NULL != plugin->sockv4)
3394 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
3395 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
3399 if (plugin->enable_ipv6 == GNUNET_YES)
3401 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
3402 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
3403 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
3404 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
3405 if (NULL != plugin->sockv6)
3407 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
3408 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
3412 schedule_select (plugin);
3413 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3417 (const struct sockaddr **) addrs,
3419 &udp_nat_port_map_callback,
3423 return sockets_created;
3428 * Return information about the given session to the
3431 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3432 * @param peer peer we send information about
3433 * @param value our `struct Session` to send information about
3434 * @return #GNUNET_OK (continue to iterate)
3437 send_session_info_iter (void *cls,
3438 const struct GNUNET_PeerIdentity *peer,
3441 struct Plugin *plugin = cls;
3442 struct Session *session = value;
3444 notify_session_monitor (plugin,
3446 GNUNET_TRANSPORT_SS_INIT);
3447 notify_session_monitor (plugin,
3449 GNUNET_TRANSPORT_SS_UP);
3455 * Begin monitoring sessions of a plugin. There can only
3456 * be one active monitor per plugin (i.e. if there are
3457 * multiple monitors, the transport service needs to
3458 * multiplex the generated events over all of them).
3460 * @param cls closure of the plugin
3461 * @param sic callback to invoke, NULL to disable monitor;
3462 * plugin will being by iterating over all active
3463 * sessions immediately and then enter monitor mode
3464 * @param sic_cls closure for @a sic
3467 udp_plugin_setup_monitor (void *cls,
3468 GNUNET_TRANSPORT_SessionInfoCallback sic,
3471 struct Plugin *plugin = cls;
3474 plugin->sic_cls = sic_cls;
3477 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3478 &send_session_info_iter,
3480 /* signal end of first iteration */
3481 sic (sic_cls, NULL, NULL);
3487 * The exported method. Makes the core api available via a global and
3488 * returns the udp transport API.
3490 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3491 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3494 libgnunet_plugin_transport_udp_init (void *cls)
3496 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3497 struct GNUNET_TRANSPORT_PluginFunctions *api;
3499 unsigned long long port;
3500 unsigned long long aport;
3501 unsigned long long udp_max_bps;
3502 unsigned long long enable_v6;
3503 unsigned long long enable_broadcasting;
3504 unsigned long long enable_broadcasting_recv;
3505 char *bind4_address;
3506 char *bind6_address;
3507 char *fancy_interval;
3508 struct GNUNET_TIME_Relative interval;
3509 struct sockaddr_in server_addrv4;
3510 struct sockaddr_in6 server_addrv6;
3515 if (NULL == env->receive)
3517 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3518 initialze the plugin or the API */
3519 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3521 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3522 api->address_to_string = &udp_address_to_string;
3523 api->string_to_address = &udp_string_to_address;
3527 /* Get port number: port == 0 : autodetect a port,
3528 * > 0 : use this port, not given : 2086 default */
3530 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3535 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3537 "ADVERTISED_PORT", &aport))
3541 LOG (GNUNET_ERROR_TYPE_WARNING,
3542 _("Given `%s' option is out of range: %llu > %u\n"),
3550 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6"))
3551 enable_v6 = GNUNET_NO;
3553 enable_v6 = GNUNET_YES;
3556 have_bind4 = GNUNET_NO;
3557 memset (&server_addrv4, 0, sizeof(server_addrv4));
3559 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3560 "BINDTO", &bind4_address))
3562 LOG (GNUNET_ERROR_TYPE_DEBUG,
3563 "Binding udp plugin to specific address: `%s'\n",
3565 if (1 != inet_pton (AF_INET,
3567 &server_addrv4.sin_addr))
3569 GNUNET_free (bind4_address);
3572 have_bind4 = GNUNET_YES;
3574 GNUNET_free_non_null(bind4_address);
3575 have_bind6 = GNUNET_NO;
3576 memset (&server_addrv6, 0, sizeof(server_addrv6));
3578 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3579 "BINDTO6", &bind6_address))
3581 LOG (GNUNET_ERROR_TYPE_DEBUG,
3582 "Binding udp plugin to specific address: `%s'\n",
3584 if (1 != inet_pton (AF_INET6,
3586 &server_addrv6.sin6_addr))
3588 LOG (GNUNET_ERROR_TYPE_ERROR,
3589 _("Invalid IPv6 address: `%s'\n"),
3591 GNUNET_free (bind6_address);
3594 have_bind6 = GNUNET_YES;
3596 GNUNET_free_non_null (bind6_address);
3598 /* Enable neighbour discovery */
3599 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3600 "transport-udp", "BROADCAST");
3601 if (enable_broadcasting == GNUNET_SYSERR)
3602 enable_broadcasting = GNUNET_NO;
3604 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3605 "transport-udp", "BROADCAST_RECEIVE");
3606 if (enable_broadcasting_recv == GNUNET_SYSERR)
3607 enable_broadcasting_recv = GNUNET_YES;
3609 if (GNUNET_SYSERR ==
3610 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3611 "BROADCAST_INTERVAL",
3614 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3618 if (GNUNET_SYSERR ==
3619 GNUNET_STRINGS_fancy_time_to_relative (fancy_interval, &interval))
3621 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3623 GNUNET_free(fancy_interval);
3626 /* Maximum datarate */
3628 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3629 "MAX_BPS", &udp_max_bps))
3631 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3634 p = GNUNET_new (struct Plugin);
3637 p->broadcast_interval = interval;
3638 p->enable_ipv6 = enable_v6;
3639 p->enable_ipv4 = GNUNET_YES; /* default */
3640 p->enable_broadcasting = enable_broadcasting;
3641 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3643 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3644 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (
3645 GNUNET_CONTAINER_HEAP_ORDER_MIN);
3646 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages,
3648 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3651 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps),
3653 LOG(GNUNET_ERROR_TYPE_DEBUG,
3654 "Setting up sockets\n");
3655 res = setup_sockets (p,
3656 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3657 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3658 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3660 LOG (GNUNET_ERROR_TYPE_ERROR,
3661 _("Failed to create network sockets, plugin failed\n"));
3662 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3663 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3664 GNUNET_SERVER_mst_destroy (p->mst);
3669 /* Setup broadcasting and receiving beacons */
3670 setup_broadcast (p, &server_addrv6, &server_addrv4);
3672 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3675 api->disconnect_session = &udp_disconnect_session;
3676 api->query_keepalive_factor = &udp_query_keepalive_factor;
3677 api->disconnect_peer = &udp_disconnect;
3678 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3679 api->address_to_string = &udp_address_to_string;
3680 api->string_to_address = &udp_string_to_address;
3681 api->check_address = &udp_plugin_check_address;
3682 api->get_session = &udp_plugin_get_session;
3683 api->send = &udp_plugin_send;
3684 api->get_network = &udp_get_network;
3685 api->update_session_timeout = &udp_plugin_update_session_timeout;
3686 api->setup_monitor = &udp_plugin_setup_monitor;
3692 * Function called on each entry in the defragmentation heap to
3696 * @param node node in the heap (to be removed)
3697 * @param element a `struct DefragContext` to be cleaned up
3698 * @param cost unused
3699 * @return #GNUNET_YES
3702 heap_cleanup_iterator (void *cls,
3703 struct GNUNET_CONTAINER_HeapNode *node,
3705 GNUNET_CONTAINER_HeapCostType cost)
3707 struct DefragContext *d_ctx = element;
3709 GNUNET_CONTAINER_heap_remove_node (node);
3710 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3711 GNUNET_free (d_ctx);
3717 * The exported method. Makes the core api available via a global and
3718 * returns the udp transport API.
3720 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3724 libgnunet_plugin_transport_udp_done (void *cls)
3726 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3727 struct Plugin *plugin = api->cls;
3728 struct PrettyPrinterContext *cur;
3729 struct PrettyPrinterContext *next;
3730 struct UDP_MessageWrapper *udpw;
3737 stop_broadcast (plugin);
3738 if (plugin->select_task != NULL)
3740 GNUNET_SCHEDULER_cancel (plugin->select_task);
3741 plugin->select_task = NULL;
3743 if (plugin->select_task_v6 != NULL)
3745 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3746 plugin->select_task_v6 = NULL;
3749 /* Closing sockets */
3750 if (GNUNET_YES == plugin->enable_ipv4)
3752 if (NULL != plugin->sockv4)
3754 GNUNET_break (GNUNET_OK ==
3755 GNUNET_NETWORK_socket_close (plugin->sockv4));
3756 plugin->sockv4 = NULL;
3758 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3759 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3761 if (GNUNET_YES == plugin->enable_ipv6)
3763 if (NULL != plugin->sockv6)
3765 GNUNET_break (GNUNET_OK ==
3766 GNUNET_NETWORK_socket_close (plugin->sockv6));
3767 plugin->sockv6 = NULL;
3769 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3770 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3773 if (NULL != plugin->nat)
3775 GNUNET_NAT_unregister (plugin->nat);
3778 if (NULL != plugin->defrag_ctxs)
3780 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3781 &heap_cleanup_iterator, NULL);
3782 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3783 plugin->defrag_ctxs = NULL;
3785 if (NULL != plugin->mst)
3787 GNUNET_SERVER_mst_destroy (plugin->mst);
3791 /* Clean up leftover messages */
3792 udpw = plugin->ipv4_queue_head;
3793 while (NULL != udpw)
3795 struct UDP_MessageWrapper *tmp = udpw->next;
3796 dequeue (plugin, udpw);
3797 call_continuation (udpw, GNUNET_SYSERR);
3801 udpw = plugin->ipv6_queue_head;
3802 while (NULL != udpw)
3804 struct UDP_MessageWrapper *tmp = udpw->next;
3805 dequeue (plugin, udpw);
3806 call_continuation (udpw, GNUNET_SYSERR);
3811 /* Clean up sessions */
3812 LOG (GNUNET_ERROR_TYPE_DEBUG,
3813 "Cleaning up sessions\n");
3814 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3815 &disconnect_and_free_it, plugin);
3816 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3818 next = plugin->ppc_dll_head;
3819 for (cur = next; NULL != cur; cur = next)
3823 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3824 plugin->ppc_dll_tail,
3826 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3829 GNUNET_free (plugin);
3834 /* end of plugin_transport_udp.c */