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;
176 * Address metrics (as set by the "update_address_metrics" by
179 struct GNUNET_ATS_Information ats;
184 struct GNUNET_HELLO_Address *address;
187 * Number of bytes waiting for transmission to this peer.
189 unsigned long long bytes_in_queue;
192 * Number of messages waiting for transmission to this peer.
194 unsigned int msgs_in_queue;
197 * Reference counter to indicate that this session is
198 * currently being used and must not be destroyed;
199 * setting @e in_destroy will destroy it as soon as
205 * Is this session about to be destroyed (sometimes we cannot
206 * destroy a session immediately as below us on the stack
207 * there might be code that still uses it; in this case,
208 * @e rc is non-zero).
215 * Closure for #process_inbound_tokenized_messages().
217 struct SourceInformation
222 struct GNUNET_PeerIdentity sender;
225 * Associated session.
227 struct Session *session;
232 * Closure for #find_receive_context().
234 struct FindReceiveContext
237 * Where to store the result.
239 struct DefragContext *rc;
242 * Session associated with this context.
244 struct Session *session;
249 const union UdpAddress *udp_addr;
252 * Number of bytes in @e udp_addr.
259 * Data structure to track defragmentation contexts based
260 * on the source of the UDP traffic.
266 * Defragmentation context.
268 struct GNUNET_DEFRAGMENT_Context *defrag;
271 * Reference to master plugin struct.
273 struct Plugin *plugin;
276 * Node in the defrag heap.
278 struct GNUNET_CONTAINER_HeapNode *hnode;
281 * Who's message(s) are we defragmenting here?
282 * Only initialized once we succeeded and
283 * @e have_sender is set.
285 struct GNUNET_PeerIdentity sender;
288 * Source address this receive context is for (allocated at the
289 * end of the struct).
291 const union UdpAddress *udp_addr;
294 * Length of @e udp_addr.
299 * Network type the address belongs to.
301 enum GNUNET_ATS_Network_Type network_type;
304 * Has the @e sender field been initialized yet?
311 * Context to send fragmented messages
313 struct UDP_FragmentationContext
316 * Next in linked list
318 struct UDP_FragmentationContext *next;
321 * Previous in linked list
323 struct UDP_FragmentationContext *prev;
328 struct Plugin *plugin;
331 * Handle for GNUNET_FRAGMENT context
333 struct GNUNET_FRAGMENT_Context *frag;
336 * The session this fragmentation context belongs to
338 struct Session *session;
341 * Function to call upon completion of the transmission.
343 GNUNET_TRANSPORT_TransmitContinuation cont;
346 * Closure for @e cont.
353 struct GNUNET_TIME_Absolute timeout;
356 * Payload size of original unfragmented message
361 * Bytes used to send all fragments on wire including UDP overhead
368 unsigned int fragments_used;
374 * Message types included in a `struct UDP_MessageWrapper`
379 * Uninitialized (error)
384 * Fragment of a message.
386 UMT_MSG_FRAGMENTED = 1,
391 UMT_MSG_FRAGMENTED_COMPLETE = 2,
394 * Unfragmented message.
396 UMT_MSG_UNFRAGMENTED = 3,
399 * Receipt confirmation.
407 * Information we track for each message in the queue.
409 struct UDP_MessageWrapper
412 * Session this message belongs to
414 struct Session *session;
420 struct UDP_MessageWrapper *prev;
426 struct UDP_MessageWrapper *next;
429 * Message with size msg_size including UDP specific overhead
434 * Function to call upon completion of the transmission.
436 GNUNET_TRANSPORT_TransmitContinuation cont;
439 * Closure for @e cont.
444 * Fragmentation context
445 * frag_ctx == NULL if transport <= MTU
446 * frag_ctx != NULL if transport > MTU
448 struct UDP_FragmentationContext *frag_ctx;
453 struct GNUNET_TIME_Absolute timeout;
456 * Size of UDP message to send including UDP specific overhead
461 * Payload size of original message
468 enum UDP_MessageType msg_type;
474 * UDP ACK Message-Packet header.
476 struct UDP_ACK_Message
481 struct GNUNET_MessageHeader header;
484 * Desired delay for flow control
489 * What is the identity of the sender
491 struct GNUNET_PeerIdentity sender;
497 * If a session monitor is attached, notify it about the new
500 * @param plugin our plugin
501 * @param session session that changed state
502 * @param state new state of the session
505 notify_session_monitor (struct Plugin *plugin,
506 struct Session *session,
507 enum GNUNET_TRANSPORT_SessionState state)
509 struct GNUNET_TRANSPORT_SessionInfo info;
511 if (NULL == plugin->sic)
513 memset (&info, 0, sizeof (info));
515 info.is_inbound = GNUNET_SYSERR; /* hard to say */
516 info.num_msg_pending = session->msgs_in_queue;
517 info.num_bytes_pending = session->bytes_in_queue;
518 /* info.receive_delay remains zero as this is not supported by UDP
519 (cannot selectively not receive from 'some' peer while continuing
520 to receive from others) */
521 info.session_timeout = session->timeout;
522 info.address = session->address;
523 plugin->sic (plugin->sic_cls,
530 * We have been notified that our readset has something to read. We don't
531 * know which socket needs to be read, so we have to check each one
532 * Then reschedule this function to be called again once more is available.
534 * @param cls the plugin handle
535 * @param tc the scheduling context (for rescheduling this function again)
538 udp_plugin_select (void *cls,
539 const struct GNUNET_SCHEDULER_TaskContext *tc);
543 * We have been notified that our readset has something to read. We don't
544 * know which socket needs to be read, so we have to check each one
545 * Then reschedule this function to be called again once more is available.
547 * @param cls the plugin handle
548 * @param tc the scheduling context (for rescheduling this function again)
551 udp_plugin_select_v6 (void *cls,
552 const struct GNUNET_SCHEDULER_TaskContext *tc);
556 * (re)schedule select tasks for this plugin.
558 * @param plugin plugin to reschedule
561 schedule_select (struct Plugin *plugin)
563 struct GNUNET_TIME_Relative min_delay;
564 struct UDP_MessageWrapper *udpw;
566 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
568 /* Find a message ready to send:
569 * Flow delay from other peer is expired or not set (0) */
570 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
571 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
572 min_delay = GNUNET_TIME_relative_min (min_delay,
573 GNUNET_TIME_absolute_get_remaining (
574 udpw->session->flow_delay_from_other_peer));
576 if (plugin->select_task != NULL )
577 GNUNET_SCHEDULER_cancel (plugin->select_task);
580 * - write active set if message is ready
581 * - timeout minimum delay */
582 plugin->select_task = GNUNET_SCHEDULER_add_select (
583 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
584 (0 == min_delay.rel_value_us) ?
585 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v4,
586 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
587 &udp_plugin_select, plugin);
589 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
591 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
592 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
593 min_delay = GNUNET_TIME_relative_min (min_delay,
594 GNUNET_TIME_absolute_get_remaining (
595 udpw->session->flow_delay_from_other_peer));
597 if (NULL != plugin->select_task_v6)
598 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
599 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (
600 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
601 (0 == min_delay.rel_value_us) ?
602 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v6,
603 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
604 &udp_plugin_select_v6, plugin);
610 * Function called for a quick conversion of the binary address to
611 * a numeric address. Note that the caller must not free the
612 * address and that the next call to this function is allowed
613 * to override the address again.
616 * @param addr binary address (a `union UdpAddress`)
617 * @param addrlen length of the @a addr
618 * @return string representing the same address
621 udp_address_to_string (void *cls,
625 static char rbuf[INET6_ADDRSTRLEN + 10];
626 char buf[INET6_ADDRSTRLEN];
630 const struct IPv4UdpAddress *t4;
631 const struct IPv6UdpAddress *t6;
636 if ((NULL != addr) && (addrlen == sizeof(struct IPv6UdpAddress)))
640 options = ntohl (t6->options);
641 port = ntohs (t6->u6_port);
642 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
645 else if ((NULL != addr) && (addrlen == sizeof(struct IPv4UdpAddress)))
649 options = ntohl (t4->options);
650 port = ntohs (t4->u4_port);
651 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
658 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
660 GNUNET_snprintf (rbuf, sizeof(rbuf),
673 * Function called to convert a string address to
676 * @param cls closure (`struct Plugin *`)
677 * @param addr string address
678 * @param addrlen length of the address
679 * @param buf location to store the buffer
680 * @param added location to store the number of bytes in the buffer.
681 * If the function returns #GNUNET_SYSERR, its contents are undefined.
682 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
685 udp_string_to_address (void *cls,
691 struct sockaddr_storage socket_address;
697 /* Format tcp.options.address:port */
702 if ((NULL == addr) || (addrlen == 0))
705 return GNUNET_SYSERR;
707 if ('\0' != addr[addrlen - 1])
710 return GNUNET_SYSERR;
712 if (strlen (addr) != addrlen - 1)
715 return GNUNET_SYSERR;
717 plugin = GNUNET_strdup (addr);
718 optionstr = strchr (plugin, '.');
719 if (NULL == optionstr)
723 return GNUNET_SYSERR;
727 options = atol (optionstr);
728 address = strchr (optionstr, '.');
733 return GNUNET_SYSERR;
739 GNUNET_STRINGS_to_address_ip (address, strlen (address),
744 return GNUNET_SYSERR;
749 switch (socket_address.ss_family)
753 struct IPv4UdpAddress *u4;
754 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
755 u4 = GNUNET_new (struct IPv4UdpAddress);
756 u4->options = htonl (options);
757 u4->ipv4_addr = in4->sin_addr.s_addr;
758 u4->u4_port = in4->sin_port;
760 *added = sizeof(struct IPv4UdpAddress);
765 struct IPv6UdpAddress *u6;
766 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
767 u6 = GNUNET_new (struct IPv6UdpAddress);
768 u6->options = htonl (options);
769 u6->ipv6_addr = in6->sin6_addr;
770 u6->u6_port = in6->sin6_port;
772 *added = sizeof(struct IPv6UdpAddress);
777 return GNUNET_SYSERR;
783 * Append our port and forward the result.
785 * @param cls a `struct PrettyPrinterContext *`
786 * @param hostname result from DNS resolver
789 append_port (void *cls,
790 const char *hostname)
792 struct PrettyPrinterContext *ppc = cls;
793 struct Plugin *plugin = ppc->plugin;
796 if (NULL == hostname)
798 /* Final call, done */
799 ppc->asc (ppc->asc_cls,
802 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
803 plugin->ppc_dll_tail,
805 ppc->resolver_handle = NULL;
809 if (GNUNET_YES == ppc->ipv6)
810 GNUNET_asprintf (&ret,
817 GNUNET_asprintf (&ret,
823 ppc->asc (ppc->asc_cls,
831 * Convert the transports address to a nice, human-readable
834 * @param cls closure with the `struct Plugin *`
835 * @param type name of the transport that generated the address
836 * @param addr one of the addresses of the host, NULL for the last address
837 * the specific address format depends on the transport;
838 * a `union UdpAddress`
839 * @param addrlen length of the address
840 * @param numeric should (IP) addresses be displayed in numeric form?
841 * @param timeout after how long should we give up?
842 * @param asc function to call on each string
843 * @param asc_cls closure for @a asc
846 udp_plugin_address_pretty_printer (void *cls,
851 struct GNUNET_TIME_Relative timeout,
852 GNUNET_TRANSPORT_AddressStringCallback asc,
855 struct Plugin *plugin = cls;
856 struct PrettyPrinterContext *ppc;
859 struct sockaddr_in a4;
860 struct sockaddr_in6 a6;
861 const struct IPv4UdpAddress *u4;
862 const struct IPv6UdpAddress *u6;
866 if (addrlen == sizeof(struct IPv6UdpAddress))
869 memset (&a6, 0, sizeof(a6));
870 a6.sin6_family = AF_INET6;
871 #if HAVE_SOCKADDR_IN_SIN_LEN
872 a6.sin6_len = sizeof (a6);
874 a6.sin6_port = u6->u6_port;
875 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
876 port = ntohs (u6->u6_port);
877 options = ntohl (u6->options);
881 else if (addrlen == sizeof(struct IPv4UdpAddress))
884 memset (&a4, 0, sizeof(a4));
885 a4.sin_family = AF_INET;
886 #if HAVE_SOCKADDR_IN_SIN_LEN
887 a4.sin_len = sizeof (a4);
889 a4.sin_port = u4->u4_port;
890 a4.sin_addr.s_addr = u4->ipv4_addr;
891 port = ntohs (u4->u4_port);
892 options = ntohl (u4->options);
898 /* invalid address */
900 asc (asc_cls, NULL , GNUNET_SYSERR);
901 asc (asc_cls, NULL, GNUNET_OK);
904 ppc = GNUNET_new (struct PrettyPrinterContext);
905 ppc->plugin = plugin;
907 ppc->asc_cls = asc_cls;
909 ppc->options = options;
910 if (addrlen == sizeof(struct IPv6UdpAddress))
911 ppc->ipv6 = GNUNET_YES;
913 ppc->ipv6 = GNUNET_NO;
914 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
915 plugin->ppc_dll_tail,
918 = GNUNET_RESOLVER_hostname_get (sb,
930 call_continuation (struct UDP_MessageWrapper *udpw,
933 struct Session *session = udpw->session;
934 struct Plugin *plugin = session->plugin;
937 LOG (GNUNET_ERROR_TYPE_DEBUG,
938 "Calling continuation for %u byte message to `%s' with result %s\n",
939 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
940 (GNUNET_OK == result) ? "OK" : "SYSERR");
942 if (udpw->msg_size >= udpw->payload_size)
943 overhead = udpw->msg_size - udpw->payload_size;
945 overhead = udpw->msg_size;
950 switch (udpw->msg_type)
952 case UMT_MSG_UNFRAGMENTED:
953 if (NULL != udpw->cont)
955 /* Transport continuation */
956 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
957 udpw->payload_size, udpw->msg_size);
959 GNUNET_STATISTICS_update (plugin->env->stats,
960 "# UDP, unfragmented msgs, messages, sent, success", 1, GNUNET_NO);
961 GNUNET_STATISTICS_update (plugin->env->stats,
962 "# UDP, unfragmented msgs, bytes payload, sent, success",
963 udpw->payload_size, GNUNET_NO);
964 GNUNET_STATISTICS_update (plugin->env->stats,
965 "# UDP, unfragmented msgs, bytes overhead, sent, success", overhead,
967 GNUNET_STATISTICS_update (plugin->env->stats,
968 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
969 GNUNET_STATISTICS_update (plugin->env->stats,
970 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
972 case UMT_MSG_FRAGMENTED_COMPLETE:
973 GNUNET_assert(NULL != udpw->frag_ctx);
974 if (udpw->frag_ctx->cont != NULL )
975 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
976 GNUNET_OK, udpw->frag_ctx->payload_size,
977 udpw->frag_ctx->on_wire_size);
978 GNUNET_STATISTICS_update (plugin->env->stats,
979 "# UDP, fragmented msgs, messages, sent, success", 1, GNUNET_NO);
980 GNUNET_STATISTICS_update (plugin->env->stats,
981 "# UDP, fragmented msgs, bytes payload, sent, success",
982 udpw->payload_size, GNUNET_NO);
983 GNUNET_STATISTICS_update (plugin->env->stats,
984 "# UDP, fragmented msgs, bytes overhead, sent, success", overhead,
986 GNUNET_STATISTICS_update (plugin->env->stats,
987 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
988 GNUNET_STATISTICS_update (plugin->env->stats,
989 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
990 GNUNET_STATISTICS_update (plugin->env->stats,
991 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
993 case UMT_MSG_FRAGMENTED:
994 /* Fragmented message: enqueue next fragment */
995 if (NULL != udpw->cont)
996 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
997 udpw->payload_size, udpw->msg_size);
998 GNUNET_STATISTICS_update (plugin->env->stats,
999 "# UDP, fragmented msgs, fragments, sent, success", 1, GNUNET_NO);
1000 GNUNET_STATISTICS_update (plugin->env->stats,
1001 "# UDP, fragmented msgs, fragments bytes, sent, success",
1002 udpw->msg_size, GNUNET_NO);
1005 /* No continuation */
1006 GNUNET_STATISTICS_update (plugin->env->stats,
1007 "# UDP, ACK msgs, messages, sent, success", 1, GNUNET_NO);
1008 GNUNET_STATISTICS_update (plugin->env->stats,
1009 "# UDP, ACK msgs, bytes overhead, sent, success", overhead,
1011 GNUNET_STATISTICS_update (plugin->env->stats,
1012 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
1020 switch (udpw->msg_type)
1022 case UMT_MSG_UNFRAGMENTED:
1023 /* Unfragmented message: failed to send */
1024 if (NULL != udpw->cont)
1025 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
1026 udpw->payload_size, overhead);
1027 GNUNET_STATISTICS_update (plugin->env->stats,
1028 "# UDP, unfragmented msgs, messages, sent, failure", 1, GNUNET_NO);
1029 GNUNET_STATISTICS_update (plugin->env->stats,
1030 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1031 udpw->payload_size, GNUNET_NO);
1032 GNUNET_STATISTICS_update (plugin->env->stats,
1033 "# UDP, unfragmented msgs, bytes overhead, sent, failure", overhead,
1036 case UMT_MSG_FRAGMENTED_COMPLETE:
1037 GNUNET_assert(NULL != udpw->frag_ctx);
1038 if (udpw->frag_ctx->cont != NULL )
1039 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
1040 GNUNET_SYSERR, udpw->frag_ctx->payload_size,
1041 udpw->frag_ctx->on_wire_size);
1042 GNUNET_STATISTICS_update (plugin->env->stats,
1043 "# UDP, fragmented msgs, messages, sent, failure", 1, GNUNET_NO);
1044 GNUNET_STATISTICS_update (plugin->env->stats,
1045 "# UDP, fragmented msgs, bytes payload, sent, failure",
1046 udpw->payload_size, GNUNET_NO);
1047 GNUNET_STATISTICS_update (plugin->env->stats,
1048 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
1050 GNUNET_STATISTICS_update (plugin->env->stats,
1051 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
1053 GNUNET_STATISTICS_update (plugin->env->stats,
1054 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
1056 case UMT_MSG_FRAGMENTED:
1057 GNUNET_assert(NULL != udpw->frag_ctx);
1058 /* Fragmented message: failed to send */
1059 GNUNET_STATISTICS_update (plugin->env->stats,
1060 "# UDP, fragmented msgs, fragments, sent, failure", 1, GNUNET_NO);
1061 GNUNET_STATISTICS_update (plugin->env->stats,
1062 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1063 udpw->msg_size, GNUNET_NO);
1066 /* ACK message: failed to send */
1067 GNUNET_STATISTICS_update (plugin->env->stats,
1068 "# UDP, ACK msgs, messages, sent, failure", 1, GNUNET_NO);
1083 * Check if the given port is plausible (must be either our listen
1084 * port or our advertised port). If it is neither, we return
1087 * @param plugin global variables
1088 * @param in_port port number to check
1089 * @return #GNUNET_OK if port is either open_port or adv_port
1092 check_port (struct Plugin *plugin,
1095 if ((in_port == plugin->port) || (in_port == plugin->aport))
1097 return GNUNET_SYSERR;
1102 * Function that will be called to check if a binary address for this
1103 * plugin is well-formed and corresponds to an address for THIS peer
1104 * (as per our configuration). Naturally, if absolutely necessary,
1105 * plugins can be a bit conservative in their answer, but in general
1106 * plugins should make sure that the address does not redirect
1107 * traffic to a 3rd party that might try to man-in-the-middle our
1110 * @param cls closure, should be our handle to the Plugin
1111 * @param addr pointer to a `union UdpAddress`
1112 * @param addrlen length of @a addr
1113 * @return #GNUNET_OK if this is a plausible address for this peer
1114 * and transport, #GNUNET_SYSERR if not
1117 udp_plugin_check_address (void *cls,
1121 struct Plugin *plugin = cls;
1122 struct IPv4UdpAddress *v4;
1123 struct IPv6UdpAddress *v6;
1125 if ( (addrlen != sizeof(struct IPv4UdpAddress)) &&
1126 (addrlen != sizeof(struct IPv6UdpAddress)) )
1129 return GNUNET_SYSERR;
1131 if (addrlen == sizeof(struct IPv4UdpAddress))
1133 v4 = (struct IPv4UdpAddress *) addr;
1134 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1135 return GNUNET_SYSERR;
1137 GNUNET_NAT_test_address (plugin->nat,
1139 sizeof (struct in_addr)))
1140 return GNUNET_SYSERR;
1144 v6 = (struct IPv6UdpAddress *) addr;
1145 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1148 return GNUNET_SYSERR;
1150 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1151 return GNUNET_SYSERR;
1153 GNUNET_NAT_test_address (plugin->nat,
1155 sizeof(struct in6_addr)))
1156 return GNUNET_SYSERR;
1163 * Function to free last resources associated with a session.
1165 * @param s session to free
1168 free_session (struct Session *s)
1170 if (NULL != s->frag_ctx)
1172 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL );
1173 GNUNET_free(s->frag_ctx);
1181 * Remove a message from the transmission queue.
1183 * @param plugin the UDP plugin
1184 * @param udpw message wrapper to queue
1187 dequeue (struct Plugin *plugin,
1188 struct UDP_MessageWrapper *udpw)
1190 struct Session *session = udpw->session;
1192 if (plugin->bytes_in_buffer < udpw->msg_size)
1198 GNUNET_STATISTICS_update (plugin->env->stats,
1199 "# UDP, total, bytes in buffers",
1200 -(long long) udpw->msg_size,
1202 plugin->bytes_in_buffer -= udpw->msg_size;
1204 GNUNET_STATISTICS_update (plugin->env->stats,
1205 "# UDP, total, msgs in buffers",
1207 if (udpw->session->address->address_length == sizeof(struct IPv4UdpAddress))
1208 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1209 plugin->ipv4_queue_tail,
1211 else if (udpw->session->address->address_length == sizeof(struct IPv6UdpAddress))
1212 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1213 plugin->ipv6_queue_tail,
1220 GNUNET_assert (session->msgs_in_queue > 0);
1221 session->msgs_in_queue--;
1222 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1223 session->bytes_in_queue -= udpw->msg_size;
1231 fragmented_message_done (struct UDP_FragmentationContext *fc,
1234 struct Plugin *plugin = fc->plugin;
1235 struct Session *s = fc->session;
1236 struct UDP_MessageWrapper *udpw;
1237 struct UDP_MessageWrapper *tmp;
1238 struct UDP_MessageWrapper dummy;
1240 LOG (GNUNET_ERROR_TYPE_DEBUG,
1241 "%p : Fragmented message removed with result %s\n",
1243 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1245 /* Call continuation for fragmented message */
1246 memset (&dummy, 0, sizeof(dummy));
1247 dummy.msg_type = UMT_MSG_FRAGMENTED_COMPLETE;
1248 dummy.msg_size = s->frag_ctx->on_wire_size;
1249 dummy.payload_size = s->frag_ctx->payload_size;
1250 dummy.frag_ctx = s->frag_ctx;
1252 dummy.cont_cls = NULL;
1254 call_continuation (&dummy, result);
1255 /* Remove leftover fragments from queue */
1256 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1258 udpw = plugin->ipv6_queue_head;
1259 while (NULL != udpw)
1262 if ((udpw->frag_ctx != NULL )&& (udpw->frag_ctx == s->frag_ctx)){
1263 dequeue (plugin, udpw);
1264 call_continuation (udpw, GNUNET_SYSERR);
1270 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1272 udpw = plugin->ipv4_queue_head;
1273 while (udpw != NULL )
1276 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1278 dequeue (plugin, udpw);
1279 call_continuation (udpw, GNUNET_SYSERR);
1285 notify_session_monitor (s->plugin,
1287 GNUNET_TRANSPORT_SS_UPDATE);
1288 /* Destroy fragmentation context */
1289 GNUNET_FRAGMENT_context_destroy (fc->frag,
1290 &s->last_expected_msg_delay,
1291 &s->last_expected_ack_delay);
1298 * Scan the heap for a receive context with the given address.
1300 * @param cls the `struct FindReceiveContext`
1301 * @param node internal node of the heap
1302 * @param element value stored at the node (a `struct ReceiveContext`)
1303 * @param cost cost associated with the node
1304 * @return #GNUNET_YES if we should continue to iterate,
1305 * #GNUNET_NO if not.
1308 find_receive_context (void *cls,
1309 struct GNUNET_CONTAINER_HeapNode *node,
1311 GNUNET_CONTAINER_HeapCostType cost)
1313 struct FindReceiveContext *frc = cls;
1314 struct DefragContext *e = element;
1316 if ( (frc->udp_addr_len == e->udp_addr_len) &&
1317 (0 == memcmp (frc->udp_addr,
1319 frc->udp_addr_len)) )
1329 * Functions with this signature are called whenever we need
1330 * to close a session due to a disconnect or failure to
1331 * establish a connection.
1333 * @param cls closure with the `struct Plugin`
1334 * @param s session to close down
1335 * @return #GNUNET_OK on success
1338 udp_disconnect_session (void *cls,
1341 struct Plugin *plugin = cls;
1342 struct UDP_MessageWrapper *udpw;
1343 struct UDP_MessageWrapper *next;
1344 struct FindReceiveContext frc;
1346 GNUNET_assert (GNUNET_YES != s->in_destroy);
1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1348 "Session %p to peer `%s' address ended\n", s,
1349 GNUNET_i2s (&s->target),
1350 udp_address_to_string (plugin,
1351 s->address->address,
1352 s->address->address_length));
1353 /* stop timeout task */
1354 if (NULL != s->timeout_task)
1356 GNUNET_SCHEDULER_cancel (s->timeout_task);
1357 s->timeout_task = NULL;
1359 if (NULL != s->frag_ctx)
1361 /* Remove fragmented message due to disconnect */
1362 fragmented_message_done (s->frag_ctx,
1367 frc.udp_addr = s->address->address;
1368 frc.udp_addr_len = s->address->address_length;
1369 /* Lookup existing receive context for this address */
1370 if (NULL != plugin->defrag_ctxs)
1372 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1373 &find_receive_context,
1377 struct DefragContext *d_ctx = frc.rc;
1379 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
1380 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1381 GNUNET_free (d_ctx);
1384 next = plugin->ipv4_queue_head;
1385 while (NULL != (udpw = next))
1388 if (udpw->session == s)
1390 dequeue (plugin, udpw);
1391 call_continuation (udpw, GNUNET_SYSERR);
1395 next = plugin->ipv6_queue_head;
1396 while (NULL != (udpw = next))
1399 if (udpw->session == s)
1401 dequeue (plugin, udpw);
1402 call_continuation (udpw, GNUNET_SYSERR);
1406 notify_session_monitor (s->plugin,
1408 GNUNET_TRANSPORT_SS_DONE);
1409 plugin->env->session_end (plugin->env->cls,
1413 if (NULL != s->frag_ctx)
1415 if (NULL != s->frag_ctx->cont)
1417 s->frag_ctx->cont (s->frag_ctx->cont_cls,
1420 s->frag_ctx->payload_size,
1421 s->frag_ctx->on_wire_size);
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1424 GNUNET_i2s (&s->target));
1428 GNUNET_assert (GNUNET_YES ==
1429 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1432 GNUNET_STATISTICS_set (plugin->env->stats,
1433 "# UDP sessions active",
1434 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1438 s->in_destroy = GNUNET_YES;
1442 GNUNET_HELLO_address_free (s->address);
1450 * Function that is called to get the keepalive factor.
1451 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1452 * calculate the interval between keepalive packets.
1454 * @param cls closure with the `struct Plugin`
1455 * @return keepalive factor
1458 udp_query_keepalive_factor (void *cls)
1465 * Destroy a session, plugin is being unloaded.
1467 * @param cls the `struct Plugin`
1468 * @param key hash of public key of target peer
1469 * @param value a `struct PeerSession *` to clean up
1470 * @return #GNUNET_OK (continue to iterate)
1473 disconnect_and_free_it (void *cls,
1474 const struct GNUNET_PeerIdentity *key,
1477 struct Plugin *plugin = cls;
1479 udp_disconnect_session (plugin, value);
1485 * Disconnect from a remote node. Clean up session if we have one for
1488 * @param cls closure for this call (should be handle to Plugin)
1489 * @param target the peeridentity of the peer to disconnect
1490 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1493 udp_disconnect (void *cls,
1494 const struct GNUNET_PeerIdentity *target)
1496 struct Plugin *plugin = cls;
1498 LOG (GNUNET_ERROR_TYPE_DEBUG,
1499 "Disconnecting from peer `%s'\n",
1500 GNUNET_i2s (target));
1501 /* Clean up sessions */
1502 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1504 &disconnect_and_free_it,
1510 * Session was idle, so disconnect it
1512 * @param cls the `struct Session` to time out
1513 * @param tc scheduler context
1516 session_timeout (void *cls,
1517 const struct GNUNET_SCHEDULER_TaskContext *tc)
1519 struct Session *s = cls;
1520 struct Plugin *plugin = s->plugin;
1521 struct GNUNET_TIME_Relative left;
1523 s->timeout_task = NULL;
1524 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1525 if (left.rel_value_us > 0)
1527 /* not actually our turn yet, but let's at least update
1528 the monitor, it may think we're about to die ... */
1529 notify_session_monitor (s->plugin,
1531 GNUNET_TRANSPORT_SS_UPDATE);
1532 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1537 LOG (GNUNET_ERROR_TYPE_DEBUG,
1538 "Session %p was idle for %s, disconnecting\n",
1540 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
1542 /* call session destroy function */
1543 udp_disconnect_session (plugin, s);
1548 * Increment session timeout due to activity
1550 * @param s session to reschedule timeout activity for
1553 reschedule_session_timeout (struct Session *s)
1555 if (GNUNET_YES == s->in_destroy)
1557 GNUNET_assert(NULL != s->timeout_task);
1558 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1565 static struct Session *
1566 create_session (struct Plugin *plugin,
1567 const struct GNUNET_HELLO_Address *address)
1571 s = GNUNET_new (struct Session);
1573 s->address = GNUNET_HELLO_address_copy (address);
1574 s->target = address->peer;
1575 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1577 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1578 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1579 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1580 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1581 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1582 &session_timeout, s);
1588 * Function obtain the network type for a session
1590 * @param cls closure ('struct Plugin*')
1591 * @param session the session
1592 * @return the network type
1594 static enum GNUNET_ATS_Network_Type
1595 udp_get_network (void *cls,
1596 struct Session *session)
1598 return ntohl (session->ats.value);
1603 * Closure for #session_cmp_it().
1605 struct SessionCompareContext
1608 * Set to session matching the address.
1610 struct Session *res;
1613 * Address we are looking for.
1615 const struct GNUNET_HELLO_Address *address;
1620 * Find a session with a matching address.
1622 * @param cls the `struct SessionCompareContext *`
1623 * @param key peer identity (unused)
1624 * @param value the `struct Session *`
1625 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1628 session_cmp_it (void *cls,
1629 const struct GNUNET_PeerIdentity *key,
1632 struct SessionCompareContext *cctx = cls;
1633 const struct GNUNET_HELLO_Address *address = cctx->address;
1634 struct Session *s = value;
1636 LOG (GNUNET_ERROR_TYPE_DEBUG,
1637 "Comparing address %s <-> %s\n",
1638 udp_address_to_string (s->plugin,
1640 address->address_length),
1641 udp_address_to_string (s->plugin,
1642 s->address->address,
1643 s->address->address_length));
1644 if (0 == GNUNET_HELLO_address_cmp(s->address, cctx->address))
1654 * Locate an existing session the transport service is using to
1655 * send data to another peer. Performs some basic sanity checks
1656 * on the address and then tries to locate a matching session.
1658 * @param cls the plugin
1659 * @param address the address we should locate the session by
1660 * @return the session if it exists, or NULL if it is not found
1662 static struct Session *
1663 udp_plugin_lookup_session (void *cls,
1664 const struct GNUNET_HELLO_Address *address)
1666 struct Plugin *plugin = cls;
1667 const struct IPv6UdpAddress *udp_a6;
1668 const struct IPv4UdpAddress *udp_a4;
1669 struct SessionCompareContext cctx;
1671 if ( (NULL == address->address) ||
1672 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1673 (address->address_length != sizeof (struct IPv6UdpAddress))))
1675 LOG (GNUNET_ERROR_TYPE_WARNING,
1676 _("Trying to locate session for address of unexpected length %u (should be %u or %u)\n"),
1677 address->address_length,
1678 sizeof (struct IPv4UdpAddress),
1679 sizeof (struct IPv6UdpAddress));
1683 if (address->address_length == sizeof(struct IPv4UdpAddress))
1685 if (NULL == plugin->sockv4)
1687 udp_a4 = (const struct IPv4UdpAddress *) address->address;
1688 if (0 == udp_a4->u4_port)
1692 if (address->address_length == sizeof(struct IPv6UdpAddress))
1694 if (NULL == plugin->sockv6)
1696 udp_a6 = (const struct IPv6UdpAddress *) address->address;
1697 if (0 == udp_a6->u6_port)
1701 /* check if session already exists */
1702 cctx.address = address;
1704 LOG (GNUNET_ERROR_TYPE_DEBUG,
1705 "Looking for existing session for peer `%s' `%s' \n",
1706 GNUNET_i2s (&address->peer),
1707 udp_address_to_string (plugin,
1709 address->address_length));
1710 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1714 if (NULL != cctx.res)
1716 LOG (GNUNET_ERROR_TYPE_DEBUG,
1717 "Found existing session %p\n",
1726 * Allocate a new session for the given endpoint address.
1727 * Note that this function does not inform the service
1728 * of the new session, this is the responsibility of the
1729 * caller (if needed).
1731 * @param cls the `struct Plugin`
1732 * @param address address of the other peer to use
1733 * @param network_type network type the address belongs to
1734 * @return NULL on error, otherwise session handle
1736 static struct Session *
1737 udp_plugin_create_session (void *cls,
1738 const struct GNUNET_HELLO_Address *address,
1739 enum GNUNET_ATS_Network_Type network_type)
1741 struct Plugin *plugin = cls;
1744 s = create_session (plugin, address);
1745 s->ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
1746 s->ats.value = htonl (network_type);
1749 return NULL; /* protocol not supported or address invalid */
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "Creating new session %p for peer `%s' address `%s'\n",
1753 GNUNET_i2s (&address->peer),
1754 udp_address_to_string (plugin,
1756 address->address_length));
1757 GNUNET_assert(GNUNET_OK ==
1758 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1761 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1762 GNUNET_STATISTICS_set (plugin->env->stats,
1763 "# UDP sessions active",
1764 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1771 * Function that will be called whenever the transport service wants to
1772 * notify the plugin that a session is still active and in use and
1773 * therefore the session timeout for this session has to be updated
1775 * @param cls closure
1776 * @param peer which peer was the session for
1777 * @param session which session is being updated
1780 udp_plugin_update_session_timeout (void *cls,
1781 const struct GNUNET_PeerIdentity *peer,
1782 struct Session *session)
1784 struct Plugin *plugin = cls;
1787 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1794 /* Reschedule session timeout */
1795 reschedule_session_timeout (session);
1800 * Creates a new outbound session the transport service will use to
1801 * send data to the peer.
1803 * @param cls the plugin
1804 * @param address the address
1805 * @return the session or NULL of max connections exceeded
1807 static struct Session *
1808 udp_plugin_get_session (void *cls,
1809 const struct GNUNET_HELLO_Address *address)
1811 struct Plugin *plugin = cls;
1813 enum GNUNET_ATS_Network_Type network_type;
1814 struct IPv4UdpAddress *udp_v4;
1815 struct IPv6UdpAddress *udp_v6;
1817 if (NULL == address)
1822 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
1823 (address->address_length != sizeof(struct IPv6UdpAddress)) )
1825 if (NULL != (s = udp_plugin_lookup_session (cls,
1829 if (sizeof (struct IPv4UdpAddress) == address->address_length)
1831 struct sockaddr_in v4;
1833 udp_v4 = (struct IPv4UdpAddress *) address->address;
1834 memset (&v4, '\0', sizeof (v4));
1835 v4.sin_family = AF_INET;
1836 #if HAVE_SOCKADDR_IN_SIN_LEN
1837 v4.sin_len = sizeof (struct sockaddr_in);
1839 v4.sin_port = udp_v4->u4_port;
1840 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
1841 network_type = plugin->env->get_address_type (plugin->env->cls,
1842 (const struct sockaddr *) &v4,
1845 else if (sizeof (struct IPv6UdpAddress) == address->address_length)
1847 struct sockaddr_in6 v6;
1849 udp_v6 = (struct IPv6UdpAddress *) address->address;
1850 memset (&v6, '\0', sizeof (v6));
1851 v6.sin6_family = AF_INET6;
1852 #if HAVE_SOCKADDR_IN_SIN_LEN
1853 v6.sin6_len = sizeof (struct sockaddr_in6);
1855 v6.sin6_port = udp_v6->u6_port;
1856 v6.sin6_addr = udp_v6->ipv6_addr;
1857 network_type = plugin->env->get_address_type (plugin->env->cls,
1858 (const struct sockaddr *) &v6,
1862 /* otherwise create new */
1863 return udp_plugin_create_session (cls, address, network_type);
1868 * Enqueue a message for transmission.
1870 * @param plugin the UDP plugin
1871 * @param udpw message wrapper to queue
1874 enqueue (struct Plugin *plugin,
1875 struct UDP_MessageWrapper *udpw)
1877 struct Session *session = udpw->session;
1879 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1885 GNUNET_STATISTICS_update (plugin->env->stats,
1886 "# UDP, total, bytes in buffers", udpw->msg_size, GNUNET_NO);
1887 plugin->bytes_in_buffer += udpw->msg_size;
1889 GNUNET_STATISTICS_update (plugin->env->stats,
1890 "# UDP, total, msgs in buffers",
1892 if (udpw->session->address->address_length == sizeof (struct IPv4UdpAddress))
1893 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1894 plugin->ipv4_queue_tail,
1896 else if (udpw->session->address->address_length == sizeof (struct IPv6UdpAddress))
1897 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1898 plugin->ipv6_queue_tail,
1905 session->msgs_in_queue++;
1906 session->bytes_in_queue += udpw->msg_size;
1911 * Fragment message was transmitted via UDP, let fragmentation know
1912 * to send the next fragment now.
1914 * @param cls the `struct UDPMessageWrapper *` of the fragment
1915 * @param target destination peer (ignored)
1916 * @param result #GNUNET_OK on success (ignored)
1917 * @param payload bytes payload sent
1918 * @param physical bytes physical sent
1921 send_next_fragment (void *cls,
1922 const struct GNUNET_PeerIdentity *target,
1927 struct UDP_MessageWrapper *udpw = cls;
1929 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1934 * Function that is called with messages created by the fragmentation
1935 * module. In the case of the 'proc' callback of the
1936 * #GNUNET_FRAGMENT_context_create() function, this function must
1937 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1939 * @param cls closure, the 'struct FragmentationContext'
1940 * @param msg the message that was created
1943 enqueue_fragment (void *cls,
1944 const struct GNUNET_MessageHeader *msg)
1946 struct UDP_FragmentationContext *frag_ctx = cls;
1947 struct Plugin *plugin = frag_ctx->plugin;
1948 struct UDP_MessageWrapper * udpw;
1949 size_t msg_len = ntohs (msg->size);
1951 LOG (GNUNET_ERROR_TYPE_DEBUG,
1952 "Enqueuing fragment with %u bytes\n",
1954 frag_ctx->fragments_used++;
1955 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1956 udpw->session = frag_ctx->session;
1957 udpw->msg_buf = (char *) &udpw[1];
1958 udpw->msg_size = msg_len;
1959 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1960 udpw->cont = &send_next_fragment;
1961 udpw->cont_cls = udpw;
1962 udpw->timeout = frag_ctx->timeout;
1963 udpw->frag_ctx = frag_ctx;
1964 udpw->msg_type = UMT_MSG_FRAGMENTED;
1965 memcpy (udpw->msg_buf, msg, msg_len);
1966 enqueue (plugin, udpw);
1967 schedule_select (plugin);
1972 * Function that can be used by the transport service to transmit
1973 * a message using the plugin. Note that in the case of a
1974 * peer disconnecting, the continuation MUST be called
1975 * prior to the disconnect notification itself. This function
1976 * will be called with this peer's HELLO message to initiate
1977 * a fresh connection to another peer.
1979 * @param cls closure
1980 * @param s which session must be used
1981 * @param msgbuf the message to transmit
1982 * @param msgbuf_size number of bytes in 'msgbuf'
1983 * @param priority how important is the message (most plugins will
1984 * ignore message priority and just FIFO)
1985 * @param to how long to wait at most for the transmission (does not
1986 * require plugins to discard the message after the timeout,
1987 * just advisory for the desired delay; most plugins will ignore
1989 * @param cont continuation to call once the message has
1990 * been transmitted (or if the transport is ready
1991 * for the next transmission call; or if the
1992 * peer disconnected...); can be NULL
1993 * @param cont_cls closure for cont
1994 * @return number of bytes used (on the physical network, with overheads);
1995 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1996 * and does NOT mean that the message was not transmitted (DV)
1999 udp_plugin_send (void *cls,
2003 unsigned int priority,
2004 struct GNUNET_TIME_Relative to,
2005 GNUNET_TRANSPORT_TransmitContinuation cont,
2008 struct Plugin *plugin = cls;
2009 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2010 struct UDP_FragmentationContext * frag_ctx;
2011 struct UDP_MessageWrapper * udpw;
2012 struct UDPMessage *udp;
2014 GNUNET_assert(plugin != NULL);
2015 GNUNET_assert(s != NULL);
2017 if ( (s->address->address_length == sizeof(struct IPv6UdpAddress)) &&
2018 (plugin->sockv6 == NULL) )
2019 return GNUNET_SYSERR;
2020 if ( (s->address->address_length == sizeof(struct IPv4UdpAddress)) &&
2021 (plugin->sockv4 == NULL) )
2022 return GNUNET_SYSERR;
2023 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2026 return GNUNET_SYSERR;
2029 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2034 return GNUNET_SYSERR;
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2039 GNUNET_i2s (&s->target),
2040 udp_address_to_string (plugin,
2041 s->address->address,
2042 s->address->address_length));
2045 udp = (struct UDPMessage *) mbuf;
2046 udp->header.size = htons (udpmlen);
2047 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2048 udp->reserved = htonl (0);
2049 udp->sender = *plugin->env->my_identity;
2051 /* We do not update the session time out here!
2052 * Otherwise this session will not timeout since we send keep alive before
2053 * session can timeout
2055 * For UDP we update session timeout only on receive, this will cover keep
2056 * alives, since remote peer will reply with keep alive response!
2058 if (udpmlen <= UDP_MTU)
2060 /* unfragmented message */
2061 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2063 udpw->msg_buf = (char *) &udpw[1];
2064 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2065 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2066 udpw->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to);
2068 udpw->cont_cls = cont_cls;
2069 udpw->frag_ctx = NULL;
2070 udpw->msg_type = UMT_MSG_UNFRAGMENTED;
2071 memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
2072 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)], msgbuf, msgbuf_size);
2073 enqueue (plugin, udpw);
2075 GNUNET_STATISTICS_update (plugin->env->stats,
2076 "# UDP, unfragmented msgs, messages, attempt", 1, GNUNET_NO);
2077 GNUNET_STATISTICS_update (plugin->env->stats,
2078 "# UDP, unfragmented msgs, bytes payload, attempt",
2084 /* fragmented message */
2085 if (s->frag_ctx != NULL)
2086 return GNUNET_SYSERR;
2087 memcpy (&udp[1], msgbuf, msgbuf_size);
2088 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2089 frag_ctx->plugin = plugin;
2090 frag_ctx->session = s;
2091 frag_ctx->cont = cont;
2092 frag_ctx->cont_cls = cont_cls;
2093 frag_ctx->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
2095 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2096 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2097 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2100 s->last_expected_msg_delay,
2101 s->last_expected_ack_delay,
2105 s->frag_ctx = frag_ctx;
2106 GNUNET_STATISTICS_update (plugin->env->stats,
2107 "# UDP, fragmented msgs, messages, pending",
2110 GNUNET_STATISTICS_update (plugin->env->stats,
2111 "# UDP, fragmented msgs, messages, attempt",
2114 GNUNET_STATISTICS_update (plugin->env->stats,
2115 "# UDP, fragmented msgs, bytes payload, attempt",
2116 frag_ctx->payload_size,
2119 notify_session_monitor (s->plugin,
2121 GNUNET_TRANSPORT_SS_UPDATE);
2122 schedule_select (plugin);
2128 * Our external IP address/port mapping has changed.
2130 * @param cls closure, the `struct LocalAddrList`
2131 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2132 * the previous (now invalid) one
2133 * @param addr either the previous or the new public IP address
2134 * @param addrlen actual lenght of the address
2137 udp_nat_port_map_callback (void *cls,
2139 const struct sockaddr *addr,
2142 struct Plugin *plugin = cls;
2143 struct GNUNET_HELLO_Address *address;
2144 struct IPv4UdpAddress u4;
2145 struct IPv6UdpAddress u6;
2149 LOG (GNUNET_ERROR_TYPE_INFO,
2150 "NAT notification to %s address `%s'\n",
2151 (GNUNET_YES == add_remove) ? "add" : "remove",
2152 GNUNET_a2s (addr, addrlen));
2154 /* convert 'address' to our internal format */
2155 switch (addr->sa_family)
2158 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
2159 memset (&u4, 0, sizeof(u4));
2160 u4.options = htonl (plugin->myoptions);
2161 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2162 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2163 if (0 == ((struct sockaddr_in *) addr)->sin_port)
2166 args = sizeof(struct IPv4UdpAddress);
2169 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
2170 memset (&u6, 0, sizeof(u6));
2171 u6.options = htonl (plugin->myoptions);
2172 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
2174 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2175 sizeof(struct in6_addr));
2176 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2178 args = sizeof(struct IPv6UdpAddress);
2184 /* modify our published address list */
2185 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2188 GNUNET_HELLO_ADDRESS_INFO_NONE);
2189 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2190 GNUNET_HELLO_address_free (address);
2195 * Message tokenizer has broken up an incomming message. Pass it on
2198 * @param cls the `struct Plugin *`
2199 * @param client the `struct SourceInformation *`
2200 * @param hdr the actual message
2201 * @return #GNUNET_OK (always)
2204 process_inbound_tokenized_messages (void *cls,
2206 const struct GNUNET_MessageHeader *hdr)
2208 struct Plugin *plugin = cls;
2209 struct SourceInformation *si = client;
2210 struct GNUNET_TIME_Relative delay;
2212 GNUNET_assert (NULL != si->session);
2213 if (GNUNET_YES == si->session->in_destroy)
2216 GNUNET_break (ntohl (si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
2217 reschedule_session_timeout (si->session);
2218 delay = plugin->env->receive (plugin->env->cls,
2219 si->session->address,
2222 plugin->env->update_address_metrics (plugin->env->cls,
2223 si->session->address,
2225 &si->session->ats, 1);
2226 si->session->flow_delay_for_other_peer = delay;
2232 * We've received a UDP Message. Process it (pass contents to main service).
2234 * @param plugin plugin context
2235 * @param msg the message
2236 * @param udp_addr sender address
2237 * @param udp_addr_len number of bytes in @a udp_addr
2238 * @param network_type network type the address belongs to
2241 process_udp_message (struct Plugin *plugin,
2242 const struct UDPMessage *msg,
2243 const union UdpAddress *udp_addr,
2244 size_t udp_addr_len,
2245 enum GNUNET_ATS_Network_Type network_type)
2247 struct SourceInformation si;
2249 struct GNUNET_HELLO_Address *address;
2251 if (0 != ntohl (msg->reserved))
2256 if (ntohs (msg->header.size)
2257 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2263 address = GNUNET_HELLO_address_allocate (&msg->sender,
2267 GNUNET_HELLO_ADDRESS_INFO_NONE);
2268 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2270 s = udp_plugin_create_session (plugin,
2273 plugin->env->session_start (plugin->env->cls,
2274 address, s, NULL, 0);
2275 notify_session_monitor (s->plugin,
2277 GNUNET_TRANSPORT_SS_INIT);
2278 notify_session_monitor (s->plugin,
2280 GNUNET_TRANSPORT_SS_UP);
2282 GNUNET_free (address);
2284 /* iterate over all embedded messages */
2286 si.sender = msg->sender;
2288 GNUNET_SERVER_mst_receive (plugin->mst,
2290 (const char *) &msg[1],
2291 ntohs (msg->header.size) - sizeof(struct UDPMessage),
2295 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
2301 * Process a defragmented message.
2303 * @param cls the `struct DefragContext *`
2304 * @param msg the message
2307 fragment_msg_proc (void *cls,
2308 const struct GNUNET_MessageHeader *msg)
2310 struct DefragContext *rc = cls;
2311 const struct UDPMessage *um;
2313 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2318 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2323 um = (const struct UDPMessage *) msg;
2324 rc->sender = um->sender;
2325 rc->have_sender = GNUNET_YES;
2326 process_udp_message (rc->plugin,
2335 * Transmit an acknowledgement.
2337 * @param cls the `struct DefragContext *`
2338 * @param id message ID (unused)
2339 * @param msg ack to transmit
2342 ack_proc (void *cls,
2344 const struct GNUNET_MessageHeader *msg)
2346 struct DefragContext *rc = cls;
2347 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2348 struct UDP_ACK_Message *udp_ack;
2350 struct UDP_MessageWrapper *udpw;
2352 struct GNUNET_HELLO_Address *address;
2354 if (GNUNET_NO == rc->have_sender)
2356 /* tried to defragment but never succeeded, hence will not ACK */
2357 GNUNET_break_op (0);
2360 address = GNUNET_HELLO_address_allocate (&rc->sender,
2364 GNUNET_HELLO_ADDRESS_INFO_NONE);
2365 s = udp_plugin_lookup_session (rc->plugin,
2367 GNUNET_HELLO_address_free (address);
2370 LOG (GNUNET_ERROR_TYPE_ERROR,
2371 "Trying to transmit ACK to peer `%s' but no session found!\n",
2372 udp_address_to_string (rc->plugin,
2375 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2376 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2380 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2381 delay = s->flow_delay_for_other_peer.rel_value_us;
2383 LOG (GNUNET_ERROR_TYPE_DEBUG,
2384 "Sending ACK to `%s' including delay of %s\n",
2385 udp_address_to_string (rc->plugin,
2388 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2390 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2391 udpw->msg_size = msize;
2392 udpw->payload_size = 0;
2394 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2395 udpw->msg_buf = (char *) &udpw[1];
2396 udpw->msg_type = UMT_MSG_ACK;
2397 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2398 udp_ack->header.size = htons ((uint16_t) msize);
2399 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2400 udp_ack->delay = htonl (delay);
2401 udp_ack->sender = *rc->plugin->env->my_identity;
2402 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2403 enqueue (rc->plugin, udpw);
2404 notify_session_monitor (s->plugin,
2406 GNUNET_TRANSPORT_SS_UPDATE);
2407 schedule_select (rc->plugin);
2412 * Handle an ACK message.
2414 * @param plugin the UDP plugin
2415 * @param msg the (presumed) UDP ACK message
2416 * @param udp_addr sender address
2417 * @param udp_addr_len number of bytes in @a udp_addr
2420 read_process_ack (struct Plugin *plugin,
2421 const struct GNUNET_MessageHeader *msg,
2422 const union UdpAddress *udp_addr,
2423 socklen_t udp_addr_len)
2425 const struct GNUNET_MessageHeader *ack;
2426 const struct UDP_ACK_Message *udp_ack;
2427 struct GNUNET_HELLO_Address *address;
2429 struct GNUNET_TIME_Relative flow_delay;
2431 if (ntohs (msg->size)
2432 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2437 udp_ack = (const struct UDP_ACK_Message *) msg;
2438 address = GNUNET_HELLO_address_allocate (&udp_ack->sender,
2442 GNUNET_HELLO_ADDRESS_INFO_NONE);
2443 s = udp_plugin_lookup_session (plugin,
2446 (NULL == s->frag_ctx) )
2448 LOG (GNUNET_ERROR_TYPE_WARNING,
2449 "UDP session of address %s for ACK not found\n",
2450 udp_address_to_string (plugin,
2452 address->address_length));
2453 GNUNET_HELLO_address_free (address);
2456 GNUNET_HELLO_address_free (address);
2458 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2459 LOG (GNUNET_ERROR_TYPE_DEBUG,
2460 "We received a sending delay of %s\n",
2461 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2463 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2465 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2466 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2473 GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag,
2476 LOG(GNUNET_ERROR_TYPE_DEBUG,
2477 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2478 (unsigned int ) ntohs (msg->size),
2479 GNUNET_i2s (&udp_ack->sender),
2480 udp_address_to_string (plugin,
2483 /* Expect more ACKs to arrive */
2487 LOG (GNUNET_ERROR_TYPE_DEBUG,
2488 "Message full ACK'ed\n",
2489 (unsigned int ) ntohs (msg->size),
2490 GNUNET_i2s (&udp_ack->sender),
2491 udp_address_to_string (plugin,
2496 /* Remove fragmented message after successful sending */
2497 fragmented_message_done (s->frag_ctx,
2503 * We received a fragment, process it.
2505 * @param plugin our plugin
2506 * @param msg a message of type #GNUNET_MESSAGE_TYPE_FRAGMENT
2507 * @param udp_addr sender address
2508 * @param udp_addr_len number of bytes in @a udp_addr
2509 * @param network_type network type the address belongs to
2512 read_process_fragment (struct Plugin *plugin,
2513 const struct GNUNET_MessageHeader *msg,
2514 const union UdpAddress *udp_addr,
2515 size_t udp_addr_len,
2516 enum GNUNET_ATS_Network_Type network_type)
2518 struct DefragContext *d_ctx;
2519 struct GNUNET_TIME_Absolute now;
2520 struct FindReceiveContext frc;
2523 frc.udp_addr = udp_addr;
2524 frc.udp_addr_len = udp_addr_len;
2526 /* Lookup existing receive context for this address */
2527 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2528 &find_receive_context,
2530 now = GNUNET_TIME_absolute_get ();
2535 /* Create a new defragmentation context */
2536 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + udp_addr_len);
2540 d_ctx->udp_addr = (const union UdpAddress *) &d_ctx[1];
2541 d_ctx->udp_addr_len = udp_addr_len;
2542 d_ctx->network_type = network_type;
2543 d_ctx->plugin = plugin;
2544 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2546 UDP_MAX_MESSAGES_IN_DEFRAG,
2550 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs,
2552 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2553 LOG (GNUNET_ERROR_TYPE_DEBUG,
2554 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2555 (unsigned int ) ntohs (msg->size),
2556 udp_address_to_string (plugin,
2562 LOG (GNUNET_ERROR_TYPE_DEBUG,
2563 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2564 (unsigned int ) ntohs (msg->size),
2565 udp_address_to_string (plugin,
2571 GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2573 /* keep this 'rc' from expiring */
2574 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs,
2576 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2578 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2579 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2581 /* remove 'rc' that was inactive the longest */
2582 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2583 GNUNET_assert (NULL != d_ctx);
2584 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2585 GNUNET_free (d_ctx);
2591 * Read and process a message from the given socket.
2593 * @param plugin the overall plugin
2594 * @param rsock socket to read from
2597 udp_select_read (struct Plugin *plugin,
2598 struct GNUNET_NETWORK_Handle *rsock)
2601 struct sockaddr_storage addr;
2602 char buf[65536] GNUNET_ALIGN;
2604 const struct GNUNET_MessageHeader *msg;
2605 struct IPv4UdpAddress v4;
2606 struct IPv6UdpAddress v6;
2607 const struct sockaddr *sa;
2608 const struct sockaddr_in *sa4;
2609 const struct sockaddr_in6 *sa6;
2610 const union UdpAddress *int_addr;
2611 size_t int_addr_len;
2612 enum GNUNET_ATS_Network_Type network_type;
2614 fromlen = sizeof(addr);
2615 memset (&addr, 0, sizeof(addr));
2616 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof(buf),
2617 (struct sockaddr *) &addr, &fromlen);
2618 sa = (const struct sockaddr *) &addr;
2620 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2621 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2622 * on this socket has failed.
2624 * WSAECONNRESET - The virtual circuit was reset by the remote side
2625 * executing a hard or abortive close. The application should close
2626 * the socket; it is no longer usable. On a UDP-datagram socket this
2627 * error indicates a previous send operation resulted in an ICMP Port
2628 * Unreachable message.
2630 if ( (-1 == size) && (ECONNRESET == errno) )
2635 LOG (GNUNET_ERROR_TYPE_DEBUG,
2636 "UDP failed to receive data: %s\n",
2638 /* Connection failure or something. Not a protocol violation. */
2641 if (size < sizeof(struct GNUNET_MessageHeader))
2643 LOG (GNUNET_ERROR_TYPE_WARNING,
2644 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2645 (unsigned int ) size,
2646 GNUNET_a2s (sa, fromlen));
2647 /* _MAY_ be a connection failure (got partial message) */
2648 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2652 msg = (const struct GNUNET_MessageHeader *) buf;
2653 LOG (GNUNET_ERROR_TYPE_DEBUG,
2654 "UDP received %u-byte message from `%s' type %u\n",
2655 (unsigned int) size,
2659 if (size != ntohs (msg->size))
2661 LOG (GNUNET_ERROR_TYPE_WARNING,
2662 "UDP malformed message header from %s\n",
2663 (unsigned int) size,
2666 GNUNET_break_op (0);
2669 GNUNET_STATISTICS_update (plugin->env->stats,
2670 "# UDP, total, bytes, received",
2673 network_type = plugin->env->get_address_type (plugin->env->cls,
2676 switch (sa->sa_family)
2679 sa4 = (const struct sockaddr_in *) &addr;
2681 v4.ipv4_addr = sa4->sin_addr.s_addr;
2682 v4.u4_port = sa4->sin_port;
2683 int_addr = (union UdpAddress *) &v4;
2684 int_addr_len = sizeof (v4);
2687 sa6 = (const struct sockaddr_in6 *) &addr;
2689 v6.ipv6_addr = sa6->sin6_addr;
2690 v6.u6_port = sa6->sin6_port;
2691 int_addr = (union UdpAddress *) &v6;
2692 int_addr_len = sizeof (v6);
2699 switch (ntohs (msg->type))
2701 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2702 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2703 udp_broadcast_receive (plugin,
2710 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2711 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2716 process_udp_message (plugin,
2717 (const struct UDPMessage *) msg,
2722 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2723 read_process_ack (plugin,
2728 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2729 read_process_fragment (plugin,
2745 static struct UDP_MessageWrapper *
2746 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2747 struct GNUNET_NETWORK_Handle *sock)
2749 struct UDP_MessageWrapper *udpw = NULL;
2750 struct GNUNET_TIME_Relative remaining;
2751 struct Session *session;
2752 struct Plugin *plugin;
2755 removed = GNUNET_NO;
2757 while (NULL != udpw)
2759 session = udpw->session;
2760 plugin = session->plugin;
2761 /* Find messages with timeout */
2762 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2763 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2765 /* Message timed out */
2766 switch (udpw->msg_type)
2768 case UMT_MSG_UNFRAGMENTED:
2769 GNUNET_STATISTICS_update (plugin->env->stats,
2770 "# UDP, total, bytes, sent, timeout",
2773 GNUNET_STATISTICS_update (plugin->env->stats,
2774 "# UDP, total, messages, sent, timeout",
2777 GNUNET_STATISTICS_update (plugin->env->stats,
2778 "# UDP, unfragmented msgs, messages, sent, timeout",
2781 GNUNET_STATISTICS_update (plugin->env->stats,
2782 "# UDP, unfragmented msgs, bytes, sent, timeout",
2785 /* Not fragmented message */
2786 LOG (GNUNET_ERROR_TYPE_DEBUG,
2787 "Message for peer `%s' with size %u timed out\n",
2788 GNUNET_i2s (&udpw->session->target),
2789 udpw->payload_size);
2790 call_continuation (udpw, GNUNET_SYSERR);
2791 /* Remove message */
2792 removed = GNUNET_YES;
2793 dequeue (plugin, udpw);
2796 case UMT_MSG_FRAGMENTED:
2797 /* Fragmented message */
2798 GNUNET_STATISTICS_update (plugin->env->stats,
2799 "# UDP, total, bytes, sent, timeout",
2800 udpw->frag_ctx->on_wire_size,
2802 GNUNET_STATISTICS_update (plugin->env->stats,
2803 "# UDP, total, messages, sent, timeout",
2806 call_continuation (udpw, GNUNET_SYSERR);
2807 LOG (GNUNET_ERROR_TYPE_DEBUG,
2808 "Fragment for message for peer `%s' with size %u timed out\n",
2809 GNUNET_i2s (&udpw->session->target),
2810 udpw->frag_ctx->payload_size);
2812 GNUNET_STATISTICS_update (plugin->env->stats,
2813 "# UDP, fragmented msgs, messages, sent, timeout",
2816 GNUNET_STATISTICS_update (plugin->env->stats,
2817 "# UDP, fragmented msgs, bytes, sent, timeout",
2818 udpw->frag_ctx->payload_size,
2820 /* Remove fragmented message due to timeout */
2821 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2824 GNUNET_STATISTICS_update (plugin->env->stats,
2825 "# UDP, total, bytes, sent, timeout",
2828 GNUNET_STATISTICS_update (plugin->env->stats,
2829 "# UDP, total, messages, sent, timeout",
2832 LOG (GNUNET_ERROR_TYPE_DEBUG,
2833 "ACK Message for peer `%s' with size %u timed out\n",
2834 GNUNET_i2s (&udpw->session->target),
2835 udpw->payload_size);
2836 call_continuation (udpw, GNUNET_SYSERR);
2837 removed = GNUNET_YES;
2838 dequeue (plugin, udpw);
2844 if (sock == plugin->sockv4)
2845 udpw = plugin->ipv4_queue_head;
2846 else if (sock == plugin->sockv6)
2847 udpw = plugin->ipv6_queue_head;
2850 GNUNET_break(0); /* should never happen */
2853 GNUNET_STATISTICS_update (plugin->env->stats,
2854 "# messages discarded due to timeout",
2860 /* Message did not time out, check flow delay */
2861 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2862 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2864 /* this message is not delayed */
2865 LOG (GNUNET_ERROR_TYPE_DEBUG,
2866 "Message for peer `%s' (%u bytes) is not delayed \n",
2867 GNUNET_i2s (&udpw->session->target),
2868 udpw->payload_size);
2869 break; /* Found message to send, break */
2873 /* Message is delayed, try next */
2874 LOG (GNUNET_ERROR_TYPE_DEBUG,
2875 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2876 GNUNET_i2s (&udpw->session->target), udpw->payload_size,
2877 GNUNET_STRINGS_relative_time_to_string (remaining, GNUNET_YES));
2882 if (GNUNET_YES == removed)
2883 notify_session_monitor (session->plugin,
2885 GNUNET_TRANSPORT_SS_UPDATE);
2894 analyze_send_error (struct Plugin *plugin,
2895 const struct sockaddr *sa,
2896 socklen_t slen, int error)
2898 enum GNUNET_ATS_Network_Type type;
2900 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
2901 if (((GNUNET_ATS_NET_LAN == type)
2902 || (GNUNET_ATS_NET_WAN == type))
2903 && ((ENETUNREACH == errno)|| (ENETDOWN == errno)))
2905 if (slen == sizeof (struct sockaddr_in))
2907 /* IPv4: "Network unreachable" or "Network down"
2909 * This indicates we do not have connectivity
2911 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2912 _("UDP could not transmit message to `%s': "
2913 "Network seems down, please check your network configuration\n"),
2914 GNUNET_a2s (sa, slen));
2916 if (slen == sizeof (struct sockaddr_in6))
2918 /* IPv6: "Network unreachable" or "Network down"
2920 * This indicates that this system is IPv6 enabled, but does not
2921 * have a valid global IPv6 address assigned or we do not have
2924 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2925 _("UDP could not transmit IPv6 message! "
2926 "Please check your network configuration and disable IPv6 if your "
2927 "connection does not have a global IPv6 address\n"));
2932 LOG (GNUNET_ERROR_TYPE_WARNING,
2933 "UDP could not transmit message to `%s': `%s'\n",
2934 GNUNET_a2s (sa, slen), STRERROR (error));
2943 udp_select_send (struct Plugin *plugin,
2944 struct GNUNET_NETWORK_Handle *sock)
2949 const struct IPv4UdpAddress *u4;
2950 struct sockaddr_in a4;
2951 const struct IPv6UdpAddress *u6;
2952 struct sockaddr_in6 a6;
2953 struct UDP_MessageWrapper *udpw;
2955 /* Find message to send */
2956 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4)
2957 ? plugin->ipv4_queue_head
2958 : plugin->ipv6_queue_head,
2961 return 0; /* No message to send */
2963 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
2965 u4 = udpw->session->address->address;
2966 memset (&a4, 0, sizeof(a4));
2967 a4.sin_family = AF_INET;
2968 #if HAVE_SOCKADDR_IN_SIN_LEN
2969 a4.sin_len = sizeof (a4);
2971 a4.sin_port = u4->u4_port;
2972 memcpy (&a4.sin_addr, &u4->ipv4_addr, sizeof(struct in_addr));
2973 a = (struct sockaddr *) &a4;
2976 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
2978 u6 = udpw->session->address->address;
2979 memset (&a6, 0, sizeof(a6));
2980 a6.sin6_family = AF_INET6;
2981 #if HAVE_SOCKADDR_IN_SIN_LEN
2982 a6.sin6_len = sizeof (a6);
2984 a6.sin6_port = u6->u6_port;
2985 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
2986 a = (struct sockaddr *) &a6;
2991 call_continuation (udpw, GNUNET_OK);
2992 dequeue (plugin, udpw);
2993 notify_session_monitor (plugin,
2995 GNUNET_TRANSPORT_SS_UPDATE);
2997 return GNUNET_SYSERR;
3000 sent = GNUNET_NETWORK_socket_sendto (sock,
3005 if (GNUNET_SYSERR == sent)
3008 analyze_send_error (plugin, a, slen, errno);
3009 call_continuation (udpw, GNUNET_SYSERR);
3010 GNUNET_STATISTICS_update (plugin->env->stats,
3011 "# UDP, total, bytes, sent, failure", sent, GNUNET_NO);
3012 GNUNET_STATISTICS_update (plugin->env->stats,
3013 "# UDP, total, messages, sent, failure", 1, GNUNET_NO);
3018 LOG (GNUNET_ERROR_TYPE_DEBUG,
3019 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
3020 (unsigned int) (udpw->msg_size),
3021 GNUNET_i2s (&udpw->session->target),
3022 GNUNET_a2s (a, slen),
3024 (sent < 0) ? STRERROR (errno) : "ok");
3025 GNUNET_STATISTICS_update (plugin->env->stats,
3026 "# UDP, total, bytes, sent, success",
3029 GNUNET_STATISTICS_update (plugin->env->stats,
3030 "# UDP, total, messages, sent, success",
3033 if (NULL != udpw->frag_ctx)
3034 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3035 call_continuation (udpw, GNUNET_OK);
3037 dequeue (plugin, udpw);
3038 notify_session_monitor (plugin,
3040 GNUNET_TRANSPORT_SS_UPDATE);
3047 * We have been notified that our readset has something to read. We don't
3048 * know which socket needs to be read, so we have to check each one
3049 * Then reschedule this function to be called again once more is available.
3051 * @param cls the plugin handle
3052 * @param tc the scheduling context (for rescheduling this function again)
3055 udp_plugin_select (void *cls,
3056 const struct GNUNET_SCHEDULER_TaskContext *tc)
3058 struct Plugin *plugin = cls;
3060 plugin->select_task = NULL;
3061 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3063 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
3064 && (NULL != plugin->sockv4)
3065 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
3066 udp_select_read (plugin, plugin->sockv4);
3067 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3068 && (NULL != plugin->sockv4) && (NULL != plugin->ipv4_queue_head)
3069 && (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
3070 udp_select_send (plugin, plugin->sockv4);
3071 schedule_select (plugin);
3076 * We have been notified that our readset has something to read. We don't
3077 * know which socket needs to be read, so we have to check each one
3078 * Then reschedule this function to be called again once more is available.
3080 * @param cls the plugin handle
3081 * @param tc the scheduling context (for rescheduling this function again)
3084 udp_plugin_select_v6 (void *cls,
3085 const struct GNUNET_SCHEDULER_TaskContext *tc)
3087 struct Plugin *plugin = cls;
3089 plugin->select_task_v6 = NULL;
3090 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3092 if (((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
3093 && (NULL != plugin->sockv6)
3094 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
3095 udp_select_read (plugin, plugin->sockv6);
3096 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3097 && (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL )&&
3098 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )udp_select_send (plugin, plugin->sockv6);
3099 schedule_select (plugin);
3104 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3106 * @param plugin the plugin to initialize
3107 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3108 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3109 * @return number of sockets that were successfully bound
3112 setup_sockets (struct Plugin *plugin,
3113 const struct sockaddr_in6 *bind_v6,
3114 const struct sockaddr_in *bind_v4)
3117 int sockets_created = 0;
3118 struct sockaddr_in6 server_addrv6;
3119 struct sockaddr_in server_addrv4;
3120 struct sockaddr *server_addr;
3121 struct sockaddr *addrs[2];
3122 socklen_t addrlens[2];
3126 /* Create IPv6 socket */
3128 if (GNUNET_YES == plugin->enable_ipv6)
3130 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
3131 if (NULL == plugin->sockv6)
3133 LOG(GNUNET_ERROR_TYPE_WARNING,
3134 "Disabling IPv6 since it is not supported on this system!\n");
3135 plugin->enable_ipv6 = GNUNET_NO;
3139 memset (&server_addrv6, '\0', sizeof(struct sockaddr_in6));
3140 #if HAVE_SOCKADDR_IN_SIN_LEN
3141 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3143 server_addrv6.sin6_family = AF_INET6;
3144 if (NULL != bind_v6)
3145 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3147 server_addrv6.sin6_addr = in6addr_any;
3149 if (0 == plugin->port) /* autodetect */
3150 server_addrv6.sin6_port
3151 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3154 server_addrv6.sin6_port = htons (plugin->port);
3155 addrlen = sizeof(struct sockaddr_in6);
3156 server_addr = (struct sockaddr *) &server_addrv6;
3161 LOG(GNUNET_ERROR_TYPE_DEBUG,
3162 "Binding to IPv6 `%s'\n",
3163 GNUNET_a2s (server_addr, addrlen));
3166 GNUNET_NETWORK_socket_bind (plugin->sockv6,
3171 if (0 != plugin->port)
3173 tries = 10; /* fail immediately */
3174 break; /* bind failed on specific port */
3177 server_addrv6.sin6_port
3178 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3184 GNUNET_NETWORK_socket_close (plugin->sockv6);
3185 plugin->enable_ipv6 = GNUNET_NO;
3186 plugin->sockv6 = NULL;
3190 plugin->port = ntohs (server_addrv6.sin6_port);
3192 if (NULL != plugin->sockv6)
3194 LOG (GNUNET_ERROR_TYPE_DEBUG,
3195 "IPv6 socket created on port %s\n",
3196 GNUNET_a2s (server_addr, addrlen));
3197 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
3198 addrlens[sockets_created] = sizeof(struct sockaddr_in6);
3203 LOG (GNUNET_ERROR_TYPE_ERROR,
3204 "Failed to bind UDP socket to %s: %s\n",
3205 GNUNET_a2s (server_addr, addrlen),
3211 /* Create IPv4 socket */
3213 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
3214 if (NULL == plugin->sockv4)
3216 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3218 LOG (GNUNET_ERROR_TYPE_WARNING,
3219 "Disabling IPv4 since it is not supported on this system!\n");
3220 plugin->enable_ipv4 = GNUNET_NO;
3224 memset (&server_addrv4, '\0', sizeof(struct sockaddr_in));
3225 #if HAVE_SOCKADDR_IN_SIN_LEN
3226 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3228 server_addrv4.sin_family = AF_INET;
3229 if (NULL != bind_v4)
3230 server_addrv4.sin_addr = bind_v4->sin_addr;
3232 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3234 if (0 == plugin->port)
3236 server_addrv4.sin_port
3237 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3241 server_addrv4.sin_port = htons (plugin->port);
3243 addrlen = sizeof(struct sockaddr_in);
3244 server_addr = (struct sockaddr *) &server_addrv4;
3249 LOG (GNUNET_ERROR_TYPE_DEBUG,
3250 "Binding to IPv4 `%s'\n",
3251 GNUNET_a2s (server_addr, addrlen));
3255 GNUNET_NETWORK_socket_bind (plugin->sockv4,
3260 if (0 != plugin->port)
3262 tries = 10; /* fail */
3263 break; /* bind failed on specific port */
3267 server_addrv4.sin_port
3268 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3274 GNUNET_NETWORK_socket_close (plugin->sockv4);
3275 plugin->enable_ipv4 = GNUNET_NO;
3276 plugin->sockv4 = NULL;
3280 plugin->port = ntohs (server_addrv4.sin_port);
3283 if (NULL != plugin->sockv4)
3285 LOG (GNUNET_ERROR_TYPE_DEBUG,
3286 "IPv4 socket created on port %s\n",
3287 GNUNET_a2s (server_addr, addrlen));
3288 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
3289 addrlens[sockets_created] = sizeof(struct sockaddr_in);
3294 LOG (GNUNET_ERROR_TYPE_ERROR,
3295 _("Failed to bind UDP socket to %s: %s\n"),
3296 GNUNET_a2s (server_addr, addrlen),
3301 if (0 == sockets_created)
3303 LOG (GNUNET_ERROR_TYPE_WARNING,
3304 _("Failed to open UDP sockets\n"));
3305 return 0; /* No sockets created, return */
3308 /* Create file descriptors */
3309 if (plugin->enable_ipv4 == GNUNET_YES)
3311 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
3312 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
3313 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
3314 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
3315 if (NULL != plugin->sockv4)
3317 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
3318 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
3322 if (plugin->enable_ipv6 == GNUNET_YES)
3324 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
3325 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
3326 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
3327 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
3328 if (NULL != plugin->sockv6)
3330 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
3331 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
3335 schedule_select (plugin);
3336 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3340 (const struct sockaddr **) addrs,
3342 &udp_nat_port_map_callback,
3346 return sockets_created;
3351 * Return information about the given session to the
3354 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3355 * @param peer peer we send information about
3356 * @param value our `struct Session` to send information about
3357 * @return #GNUNET_OK (continue to iterate)
3360 send_session_info_iter (void *cls,
3361 const struct GNUNET_PeerIdentity *peer,
3364 struct Plugin *plugin = cls;
3365 struct Session *session = value;
3367 notify_session_monitor (plugin,
3369 GNUNET_TRANSPORT_SS_INIT);
3370 notify_session_monitor (plugin,
3372 GNUNET_TRANSPORT_SS_UP);
3378 * Begin monitoring sessions of a plugin. There can only
3379 * be one active monitor per plugin (i.e. if there are
3380 * multiple monitors, the transport service needs to
3381 * multiplex the generated events over all of them).
3383 * @param cls closure of the plugin
3384 * @param sic callback to invoke, NULL to disable monitor;
3385 * plugin will being by iterating over all active
3386 * sessions immediately and then enter monitor mode
3387 * @param sic_cls closure for @a sic
3390 udp_plugin_setup_monitor (void *cls,
3391 GNUNET_TRANSPORT_SessionInfoCallback sic,
3394 struct Plugin *plugin = cls;
3397 plugin->sic_cls = sic_cls;
3400 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3401 &send_session_info_iter,
3403 /* signal end of first iteration */
3404 sic (sic_cls, NULL, NULL);
3410 * The exported method. Makes the core api available via a global and
3411 * returns the udp transport API.
3413 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3414 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3417 libgnunet_plugin_transport_udp_init (void *cls)
3419 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3420 struct GNUNET_TRANSPORT_PluginFunctions *api;
3422 unsigned long long port;
3423 unsigned long long aport;
3424 unsigned long long udp_max_bps;
3425 unsigned long long enable_v6;
3426 unsigned long long enable_broadcasting;
3427 unsigned long long enable_broadcasting_recv;
3428 char *bind4_address;
3429 char *bind6_address;
3430 char *fancy_interval;
3431 struct GNUNET_TIME_Relative interval;
3432 struct sockaddr_in server_addrv4;
3433 struct sockaddr_in6 server_addrv6;
3438 if (NULL == env->receive)
3440 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3441 initialze the plugin or the API */
3442 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3444 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3445 api->address_to_string = &udp_address_to_string;
3446 api->string_to_address = &udp_string_to_address;
3450 /* Get port number: port == 0 : autodetect a port,
3451 * > 0 : use this port, not given : 2086 default */
3453 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3458 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3460 "ADVERTISED_PORT", &aport))
3464 LOG (GNUNET_ERROR_TYPE_WARNING,
3465 _("Given `%s' option is out of range: %llu > %u\n"),
3473 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6"))
3474 enable_v6 = GNUNET_NO;
3476 enable_v6 = GNUNET_YES;
3479 have_bind4 = GNUNET_NO;
3480 memset (&server_addrv4, 0, sizeof(server_addrv4));
3482 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3483 "BINDTO", &bind4_address))
3485 LOG (GNUNET_ERROR_TYPE_DEBUG,
3486 "Binding udp plugin to specific address: `%s'\n",
3488 if (1 != inet_pton (AF_INET,
3490 &server_addrv4.sin_addr))
3492 GNUNET_free (bind4_address);
3495 have_bind4 = GNUNET_YES;
3497 GNUNET_free_non_null(bind4_address);
3498 have_bind6 = GNUNET_NO;
3499 memset (&server_addrv6, 0, sizeof(server_addrv6));
3501 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3502 "BINDTO6", &bind6_address))
3504 LOG (GNUNET_ERROR_TYPE_DEBUG,
3505 "Binding udp plugin to specific address: `%s'\n",
3507 if (1 != inet_pton (AF_INET6,
3509 &server_addrv6.sin6_addr))
3511 LOG (GNUNET_ERROR_TYPE_ERROR,
3512 _("Invalid IPv6 address: `%s'\n"),
3514 GNUNET_free (bind6_address);
3517 have_bind6 = GNUNET_YES;
3519 GNUNET_free_non_null (bind6_address);
3521 /* Enable neighbour discovery */
3522 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3523 "transport-udp", "BROADCAST");
3524 if (enable_broadcasting == GNUNET_SYSERR)
3525 enable_broadcasting = GNUNET_NO;
3527 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3528 "transport-udp", "BROADCAST_RECEIVE");
3529 if (enable_broadcasting_recv == GNUNET_SYSERR)
3530 enable_broadcasting_recv = GNUNET_YES;
3532 if (GNUNET_SYSERR ==
3533 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3534 "BROADCAST_INTERVAL",
3537 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3541 if (GNUNET_SYSERR ==
3542 GNUNET_STRINGS_fancy_time_to_relative (fancy_interval, &interval))
3544 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3546 GNUNET_free(fancy_interval);
3549 /* Maximum datarate */
3551 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3552 "MAX_BPS", &udp_max_bps))
3554 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3557 p = GNUNET_new (struct Plugin);
3560 p->broadcast_interval = interval;
3561 p->enable_ipv6 = enable_v6;
3562 p->enable_ipv4 = GNUNET_YES; /* default */
3563 p->enable_broadcasting = enable_broadcasting;
3564 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3566 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3567 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (
3568 GNUNET_CONTAINER_HEAP_ORDER_MIN);
3569 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3570 GNUNET_BANDWIDTH_tracker_init (&p->tracker, NULL, NULL,
3571 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps), 30);
3572 LOG(GNUNET_ERROR_TYPE_DEBUG,
3573 "Setting up sockets\n");
3574 res = setup_sockets (p,
3575 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3576 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3577 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3579 LOG (GNUNET_ERROR_TYPE_ERROR,
3580 _("Failed to create network sockets, plugin failed\n"));
3581 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3582 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3583 GNUNET_SERVER_mst_destroy (p->mst);
3588 /* Setup broadcasting and receiving beacons */
3589 setup_broadcast (p, &server_addrv6, &server_addrv4);
3591 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3594 api->disconnect_session = &udp_disconnect_session;
3595 api->query_keepalive_factor = &udp_query_keepalive_factor;
3596 api->disconnect_peer = &udp_disconnect;
3597 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3598 api->address_to_string = &udp_address_to_string;
3599 api->string_to_address = &udp_string_to_address;
3600 api->check_address = &udp_plugin_check_address;
3601 api->get_session = &udp_plugin_get_session;
3602 api->send = &udp_plugin_send;
3603 api->get_network = &udp_get_network;
3604 api->update_session_timeout = &udp_plugin_update_session_timeout;
3605 api->setup_monitor = &udp_plugin_setup_monitor;
3611 * Function called on each entry in the defragmentation heap to
3615 * @param node node in the heap (to be removed)
3616 * @param element a `struct DefragContext` to be cleaned up
3617 * @param cost unused
3618 * @return #GNUNET_YES
3621 heap_cleanup_iterator (void *cls,
3622 struct GNUNET_CONTAINER_HeapNode *node,
3624 GNUNET_CONTAINER_HeapCostType cost)
3626 struct DefragContext *d_ctx = element;
3628 GNUNET_CONTAINER_heap_remove_node (node);
3629 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3630 GNUNET_free (d_ctx);
3636 * The exported method. Makes the core api available via a global and
3637 * returns the udp transport API.
3639 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3643 libgnunet_plugin_transport_udp_done (void *cls)
3645 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3646 struct Plugin *plugin = api->cls;
3647 struct PrettyPrinterContext *cur;
3648 struct PrettyPrinterContext *next;
3649 struct UDP_MessageWrapper *udpw;
3656 stop_broadcast (plugin);
3657 if (plugin->select_task != NULL)
3659 GNUNET_SCHEDULER_cancel (plugin->select_task);
3660 plugin->select_task = NULL;
3662 if (plugin->select_task_v6 != NULL)
3664 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3665 plugin->select_task_v6 = NULL;
3668 /* Closing sockets */
3669 if (GNUNET_YES == plugin->enable_ipv4)
3671 if (NULL != plugin->sockv4)
3673 GNUNET_break (GNUNET_OK ==
3674 GNUNET_NETWORK_socket_close (plugin->sockv4));
3675 plugin->sockv4 = NULL;
3677 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3678 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3680 if (GNUNET_YES == plugin->enable_ipv6)
3682 if (NULL != plugin->sockv6)
3684 GNUNET_break (GNUNET_OK ==
3685 GNUNET_NETWORK_socket_close (plugin->sockv6));
3686 plugin->sockv6 = NULL;
3688 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3689 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3692 if (NULL != plugin->nat)
3694 GNUNET_NAT_unregister (plugin->nat);
3697 if (NULL != plugin->defrag_ctxs)
3699 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3700 &heap_cleanup_iterator, NULL);
3701 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3702 plugin->defrag_ctxs = NULL;
3704 if (NULL != plugin->mst)
3706 GNUNET_SERVER_mst_destroy (plugin->mst);
3710 /* Clean up leftover messages */
3711 udpw = plugin->ipv4_queue_head;
3712 while (NULL != udpw)
3714 struct UDP_MessageWrapper *tmp = udpw->next;
3715 dequeue (plugin, udpw);
3716 call_continuation (udpw, GNUNET_SYSERR);
3720 udpw = plugin->ipv6_queue_head;
3721 while (NULL != udpw)
3723 struct UDP_MessageWrapper *tmp = udpw->next;
3724 dequeue (plugin, udpw);
3725 call_continuation (udpw, GNUNET_SYSERR);
3730 /* Clean up sessions */
3731 LOG (GNUNET_ERROR_TYPE_DEBUG,
3732 "Cleaning up sessions\n");
3733 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3734 &disconnect_and_free_it, plugin);
3735 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3737 next = plugin->ppc_dll_head;
3738 for (cur = next; NULL != cur; cur = next)
3742 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3743 plugin->ppc_dll_tail,
3745 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3748 GNUNET_free (plugin);
3753 /* end of plugin_transport_udp.c */