2 This file is part of GNUnet
3 (C) 2010-2014 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;
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;
230 * Associated session.
232 struct Session *session;
235 * Number of bytes in source address.
242 * Closure for #find_receive_context().
244 struct FindReceiveContext
247 * Where to store the result.
249 struct DefragContext *rc;
254 const struct sockaddr *addr;
259 struct Session *session;
262 * Number of bytes in @e addr.
269 * Data structure to track defragmentation contexts based
270 * on the source of the UDP traffic.
276 * Defragmentation context.
278 struct GNUNET_DEFRAGMENT_Context *defrag;
281 * Source address this receive context is for (allocated at the
282 * end of the struct).
284 const struct sockaddr *src_addr;
287 * Reference to master plugin struct.
289 struct Plugin *plugin;
292 * Node in the defrag heap.
294 struct GNUNET_CONTAINER_HeapNode *hnode;
297 * Length of @e src_addr.
304 * Context to send fragmented messages
306 struct UDP_FragmentationContext
309 * Next in linked list
311 struct UDP_FragmentationContext *next;
314 * Previous in linked list
316 struct UDP_FragmentationContext *prev;
321 struct Plugin *plugin;
324 * Handle for GNUNET_FRAGMENT context
326 struct GNUNET_FRAGMENT_Context *frag;
329 * The session this fragmentation context belongs to
331 struct Session *session;
334 * Function to call upon completion of the transmission.
336 GNUNET_TRANSPORT_TransmitContinuation cont;
339 * Closure for @e cont.
346 struct GNUNET_TIME_Absolute timeout;
349 * Payload size of original unfragmented message
354 * Bytes used to send all fragments on wire including UDP overhead
361 unsigned int fragments_used;
367 * Message types included in a `struct UDP_MessageWrapper`
372 * Uninitialized (error)
377 * Fragment of a message.
379 UMT_MSG_FRAGMENTED = 1,
384 UMT_MSG_FRAGMENTED_COMPLETE = 2,
387 * Unfragmented message.
389 UMT_MSG_UNFRAGMENTED = 3,
392 * Receipt confirmation.
400 * Information we track for each message in the queue.
402 struct UDP_MessageWrapper
405 * Session this message belongs to
407 struct Session *session;
413 struct UDP_MessageWrapper *prev;
419 struct UDP_MessageWrapper *next;
422 * Message with size msg_size including UDP specific overhead
427 * Function to call upon completion of the transmission.
429 GNUNET_TRANSPORT_TransmitContinuation cont;
432 * Closure for @e cont.
437 * Fragmentation context
438 * frag_ctx == NULL if transport <= MTU
439 * frag_ctx != NULL if transport > MTU
441 struct UDP_FragmentationContext *frag_ctx;
446 struct GNUNET_TIME_Absolute timeout;
449 * Size of UDP message to send including UDP specific overhead
454 * Payload size of original message
461 enum UDP_MessageType msg_type;
467 * UDP ACK Message-Packet header (after defragmentation).
469 struct UDP_ACK_Message
474 struct GNUNET_MessageHeader header;
477 * Desired delay for flow control
482 * What is the identity of the sender
484 struct GNUNET_PeerIdentity sender;
490 * If a session monitor is attached, notify it about the new
493 * @param plugin our plugin
494 * @param session session that changed state
495 * @param state new state of the session
498 notify_session_monitor (struct Plugin *plugin,
499 struct Session *session,
500 enum GNUNET_TRANSPORT_SessionState state)
502 struct GNUNET_TRANSPORT_SessionInfo info;
504 if (NULL == plugin->sic)
506 memset (&info, 0, sizeof (info));
508 info.is_inbound = GNUNET_SYSERR; /* hard to say */
509 info.num_msg_pending = session->msgs_in_queue;
510 info.num_bytes_pending = session->bytes_in_queue;
511 /* info.receive_delay remains zero as this is not supported by UDP
512 (cannot selectively not receive from 'some' peer while continuing
513 to receive from others) */
514 info.session_timeout = session->timeout;
515 info.address = session->address;
516 plugin->sic (plugin->sic_cls,
523 * We have been notified that our readset has something to read. We don't
524 * know which socket needs to be read, so we have to check each one
525 * Then reschedule this function to be called again once more is available.
527 * @param cls the plugin handle
528 * @param tc the scheduling context (for rescheduling this function again)
531 udp_plugin_select (void *cls,
532 const struct GNUNET_SCHEDULER_TaskContext *tc);
536 * We have been notified that our readset has something to read. We don't
537 * know which socket needs to be read, so we have to check each one
538 * Then reschedule this function to be called again once more is available.
540 * @param cls the plugin handle
541 * @param tc the scheduling context (for rescheduling this function again)
544 udp_plugin_select_v6 (void *cls,
545 const struct GNUNET_SCHEDULER_TaskContext *tc);
549 * (re)schedule select tasks for this plugin.
551 * @param plugin plugin to reschedule
554 schedule_select (struct Plugin *plugin)
556 struct GNUNET_TIME_Relative min_delay;
557 struct UDP_MessageWrapper *udpw;
559 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
561 /* Find a message ready to send:
562 * Flow delay from other peer is expired or not set (0) */
563 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
564 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
565 min_delay = GNUNET_TIME_relative_min (min_delay,
566 GNUNET_TIME_absolute_get_remaining (
567 udpw->session->flow_delay_from_other_peer));
569 if (plugin->select_task != NULL )
570 GNUNET_SCHEDULER_cancel (plugin->select_task);
573 * - write active set if message is ready
574 * - timeout minimum delay */
575 plugin->select_task = GNUNET_SCHEDULER_add_select (
576 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
577 (0 == min_delay.rel_value_us) ?
578 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v4,
579 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
580 &udp_plugin_select, plugin);
582 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
584 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
585 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
586 min_delay = GNUNET_TIME_relative_min (min_delay,
587 GNUNET_TIME_absolute_get_remaining (
588 udpw->session->flow_delay_from_other_peer));
590 if (NULL != plugin->select_task_v6)
591 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
592 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (
593 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
594 (0 == min_delay.rel_value_us) ?
595 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v6,
596 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
597 &udp_plugin_select_v6, plugin);
603 * Function called for a quick conversion of the binary address to
604 * a numeric address. Note that the caller must not free the
605 * address and that the next call to this function is allowed
606 * to override the address again.
609 * @param addr binary address
610 * @param addrlen length of the address
611 * @return string representing the same address
614 udp_address_to_string (void *cls,
618 static char rbuf[INET6_ADDRSTRLEN + 10];
619 char buf[INET6_ADDRSTRLEN];
623 const struct IPv4UdpAddress *t4;
624 const struct IPv6UdpAddress *t6;
629 if ((NULL != addr) && (addrlen == sizeof(struct IPv6UdpAddress)))
633 options = ntohl (t6->options);
634 port = ntohs (t6->u6_port);
635 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
638 else if ((NULL != addr) && (addrlen == sizeof(struct IPv4UdpAddress)))
642 options = ntohl (t4->options);
643 port = ntohs (t4->u4_port);
644 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
651 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
653 GNUNET_snprintf (rbuf, sizeof(rbuf),
654 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
661 * Function called to convert a string address to
664 * @param cls closure ('struct Plugin*')
665 * @param addr string address
666 * @param addrlen length of the address
667 * @param buf location to store the buffer
668 * @param added location to store the number of bytes in the buffer.
669 * If the function returns #GNUNET_SYSERR, its contents are undefined.
670 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
673 udp_string_to_address (void *cls,
679 struct sockaddr_storage socket_address;
685 /* Format tcp.options.address:port */
690 if ((NULL == addr) || (addrlen == 0))
693 return GNUNET_SYSERR;
695 if ('\0' != addr[addrlen - 1])
698 return GNUNET_SYSERR;
700 if (strlen (addr) != addrlen - 1)
703 return GNUNET_SYSERR;
705 plugin = GNUNET_strdup (addr);
706 optionstr = strchr (plugin, '.');
707 if (NULL == optionstr)
711 return GNUNET_SYSERR;
715 options = atol (optionstr);
716 address = strchr (optionstr, '.');
721 return GNUNET_SYSERR;
727 GNUNET_STRINGS_to_address_ip (address, strlen (address),
732 return GNUNET_SYSERR;
737 switch (socket_address.ss_family)
741 struct IPv4UdpAddress *u4;
742 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
743 u4 = GNUNET_new (struct IPv4UdpAddress);
744 u4->options = htonl (options);
745 u4->ipv4_addr = in4->sin_addr.s_addr;
746 u4->u4_port = in4->sin_port;
748 *added = sizeof(struct IPv4UdpAddress);
753 struct IPv6UdpAddress *u6;
754 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
755 u6 = GNUNET_new (struct IPv6UdpAddress);
756 u6->options = htonl (options);
757 u6->ipv6_addr = in6->sin6_addr;
758 u6->u6_port = in6->sin6_port;
760 *added = sizeof(struct IPv6UdpAddress);
765 return GNUNET_SYSERR;
771 * Append our port and forward the result.
773 * @param cls a `struct PrettyPrinterContext *`
774 * @param hostname result from DNS resolver
777 append_port (void *cls,
778 const char *hostname)
780 struct PrettyPrinterContext *ppc = cls;
781 struct Plugin *plugin = ppc->plugin;
784 if (NULL == hostname)
786 /* Final call, done */
787 ppc->asc (ppc->asc_cls,
790 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
791 plugin->ppc_dll_tail,
793 ppc->resolver_handle = NULL;
797 if (GNUNET_YES == ppc->ipv6)
798 GNUNET_asprintf (&ret,
805 GNUNET_asprintf (&ret,
811 ppc->asc (ppc->asc_cls,
819 * Convert the transports address to a nice, human-readable
822 * @param cls closure with the `struct Plugin *`
823 * @param type name of the transport that generated the address
824 * @param addr one of the addresses of the host, NULL for the last address
825 * the specific address format depends on the transport
826 * @param addrlen length of the address
827 * @param numeric should (IP) addresses be displayed in numeric form?
828 * @param timeout after how long should we give up?
829 * @param asc function to call on each string
830 * @param asc_cls closure for @a asc
833 udp_plugin_address_pretty_printer (void *cls,
838 struct GNUNET_TIME_Relative timeout,
839 GNUNET_TRANSPORT_AddressStringCallback asc,
842 struct Plugin *plugin = cls;
843 struct PrettyPrinterContext *ppc;
846 struct sockaddr_in a4;
847 struct sockaddr_in6 a6;
848 const struct IPv4UdpAddress *u4;
849 const struct IPv6UdpAddress *u6;
853 if (addrlen == sizeof(struct IPv6UdpAddress))
856 memset (&a6, 0, sizeof(a6));
857 a6.sin6_family = AF_INET6;
858 #if HAVE_SOCKADDR_IN_SIN_LEN
859 a6.sin6_len = sizeof (a6);
861 a6.sin6_port = u6->u6_port;
862 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
863 port = ntohs (u6->u6_port);
864 options = ntohl (u6->options);
868 else if (addrlen == sizeof(struct IPv4UdpAddress))
871 memset (&a4, 0, sizeof(a4));
872 a4.sin_family = AF_INET;
873 #if HAVE_SOCKADDR_IN_SIN_LEN
874 a4.sin_len = sizeof (a4);
876 a4.sin_port = u4->u4_port;
877 a4.sin_addr.s_addr = u4->ipv4_addr;
878 port = ntohs (u4->u4_port);
879 options = ntohl (u4->options);
885 /* invalid address */
887 asc (asc_cls, NULL , GNUNET_SYSERR);
888 asc (asc_cls, NULL, GNUNET_OK);
891 ppc = GNUNET_new (struct PrettyPrinterContext);
892 ppc->plugin = plugin;
894 ppc->asc_cls = asc_cls;
896 ppc->options = options;
897 if (addrlen == sizeof(struct IPv6UdpAddress))
898 ppc->ipv6 = GNUNET_YES;
900 ppc->ipv6 = GNUNET_NO;
901 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
902 plugin->ppc_dll_tail,
905 = GNUNET_RESOLVER_hostname_get (sb,
917 call_continuation (struct UDP_MessageWrapper *udpw,
920 struct Session *session = udpw->session;
921 struct Plugin *plugin = session->plugin;
924 LOG (GNUNET_ERROR_TYPE_DEBUG,
925 "Calling continuation for %u byte message to `%s' with result %s\n",
926 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
927 (GNUNET_OK == result) ? "OK" : "SYSERR");
929 if (udpw->msg_size >= udpw->payload_size)
930 overhead = udpw->msg_size - udpw->payload_size;
932 overhead = udpw->msg_size;
937 switch (udpw->msg_type)
939 case UMT_MSG_UNFRAGMENTED:
940 if (NULL != udpw->cont)
942 /* Transport continuation */
943 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
944 udpw->payload_size, udpw->msg_size);
946 GNUNET_STATISTICS_update (plugin->env->stats,
947 "# UDP, unfragmented msgs, messages, sent, success", 1, GNUNET_NO);
948 GNUNET_STATISTICS_update (plugin->env->stats,
949 "# UDP, unfragmented msgs, bytes payload, sent, success",
950 udpw->payload_size, GNUNET_NO);
951 GNUNET_STATISTICS_update (plugin->env->stats,
952 "# UDP, unfragmented msgs, bytes overhead, sent, success", overhead,
954 GNUNET_STATISTICS_update (plugin->env->stats,
955 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
956 GNUNET_STATISTICS_update (plugin->env->stats,
957 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
959 case UMT_MSG_FRAGMENTED_COMPLETE:
960 GNUNET_assert(NULL != udpw->frag_ctx);
961 if (udpw->frag_ctx->cont != NULL )
962 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
963 GNUNET_OK, udpw->frag_ctx->payload_size,
964 udpw->frag_ctx->on_wire_size);
965 GNUNET_STATISTICS_update (plugin->env->stats,
966 "# UDP, fragmented msgs, messages, sent, success", 1, GNUNET_NO);
967 GNUNET_STATISTICS_update (plugin->env->stats,
968 "# UDP, fragmented msgs, bytes payload, sent, success",
969 udpw->payload_size, GNUNET_NO);
970 GNUNET_STATISTICS_update (plugin->env->stats,
971 "# UDP, fragmented msgs, bytes overhead, sent, success", overhead,
973 GNUNET_STATISTICS_update (plugin->env->stats,
974 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
975 GNUNET_STATISTICS_update (plugin->env->stats,
976 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
977 GNUNET_STATISTICS_update (plugin->env->stats,
978 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
980 case UMT_MSG_FRAGMENTED:
981 /* Fragmented message: enqueue next fragment */
982 if (NULL != udpw->cont)
983 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
984 udpw->payload_size, udpw->msg_size);
985 GNUNET_STATISTICS_update (plugin->env->stats,
986 "# UDP, fragmented msgs, fragments, sent, success", 1, GNUNET_NO);
987 GNUNET_STATISTICS_update (plugin->env->stats,
988 "# UDP, fragmented msgs, fragments bytes, sent, success",
989 udpw->msg_size, GNUNET_NO);
992 /* No continuation */
993 GNUNET_STATISTICS_update (plugin->env->stats,
994 "# UDP, ACK msgs, messages, sent, success", 1, GNUNET_NO);
995 GNUNET_STATISTICS_update (plugin->env->stats,
996 "# UDP, ACK msgs, bytes overhead, sent, success", overhead,
998 GNUNET_STATISTICS_update (plugin->env->stats,
999 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
1007 switch (udpw->msg_type)
1009 case UMT_MSG_UNFRAGMENTED:
1010 /* Unfragmented message: failed to send */
1011 if (NULL != udpw->cont)
1012 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
1013 udpw->payload_size, overhead);
1014 GNUNET_STATISTICS_update (plugin->env->stats,
1015 "# UDP, unfragmented msgs, messages, sent, failure", 1, GNUNET_NO);
1016 GNUNET_STATISTICS_update (plugin->env->stats,
1017 "# UDP, unfragmented msgs, bytes payload, sent, failure",
1018 udpw->payload_size, GNUNET_NO);
1019 GNUNET_STATISTICS_update (plugin->env->stats,
1020 "# UDP, unfragmented msgs, bytes overhead, sent, failure", overhead,
1023 case UMT_MSG_FRAGMENTED_COMPLETE:
1024 GNUNET_assert(NULL != udpw->frag_ctx);
1025 if (udpw->frag_ctx->cont != NULL )
1026 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
1027 GNUNET_SYSERR, udpw->frag_ctx->payload_size,
1028 udpw->frag_ctx->on_wire_size);
1029 GNUNET_STATISTICS_update (plugin->env->stats,
1030 "# UDP, fragmented msgs, messages, sent, failure", 1, GNUNET_NO);
1031 GNUNET_STATISTICS_update (plugin->env->stats,
1032 "# UDP, fragmented msgs, bytes payload, sent, failure",
1033 udpw->payload_size, GNUNET_NO);
1034 GNUNET_STATISTICS_update (plugin->env->stats,
1035 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
1037 GNUNET_STATISTICS_update (plugin->env->stats,
1038 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
1040 GNUNET_STATISTICS_update (plugin->env->stats,
1041 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
1043 case UMT_MSG_FRAGMENTED:
1044 GNUNET_assert(NULL != udpw->frag_ctx);
1045 /* Fragmented message: failed to send */
1046 GNUNET_STATISTICS_update (plugin->env->stats,
1047 "# UDP, fragmented msgs, fragments, sent, failure", 1, GNUNET_NO);
1048 GNUNET_STATISTICS_update (plugin->env->stats,
1049 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1050 udpw->msg_size, GNUNET_NO);
1053 /* ACK message: failed to send */
1054 GNUNET_STATISTICS_update (plugin->env->stats,
1055 "# UDP, ACK msgs, messages, sent, failure", 1, GNUNET_NO);
1070 * Check if the given port is plausible (must be either our listen
1071 * port or our advertised port). If it is neither, we return
1074 * @param plugin global variables
1075 * @param in_port port number to check
1076 * @return #GNUNET_OK if port is either open_port or adv_port
1079 check_port (struct Plugin *plugin,
1082 if ((in_port == plugin->port) || (in_port == plugin->aport))
1084 return GNUNET_SYSERR;
1089 * Function that will be called to check if a binary address for this
1090 * plugin is well-formed and corresponds to an address for THIS peer
1091 * (as per our configuration). Naturally, if absolutely necessary,
1092 * plugins can be a bit conservative in their answer, but in general
1093 * plugins should make sure that the address does not redirect
1094 * traffic to a 3rd party that might try to man-in-the-middle our
1097 * @param cls closure, should be our handle to the Plugin
1098 * @param addr pointer to the address
1099 * @param addrlen length of @a addr
1100 * @return #GNUNET_OK if this is a plausible address for this peer
1101 * and transport, #GNUNET_SYSERR if not
1104 udp_plugin_check_address (void *cls,
1108 struct Plugin *plugin = cls;
1109 struct IPv4UdpAddress *v4;
1110 struct IPv6UdpAddress *v6;
1112 if ((addrlen != sizeof(struct IPv4UdpAddress))
1113 && (addrlen != sizeof(struct IPv6UdpAddress)))
1116 return GNUNET_SYSERR;
1118 if (addrlen == sizeof(struct IPv4UdpAddress))
1120 v4 = (struct IPv4UdpAddress *) addr;
1121 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1122 return GNUNET_SYSERR;
1124 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1125 sizeof(struct in_addr)))
1126 return GNUNET_SYSERR;
1130 v6 = (struct IPv6UdpAddress *) addr;
1131 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1134 return GNUNET_SYSERR;
1136 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1137 return GNUNET_SYSERR;
1139 GNUNET_NAT_test_address (plugin->nat,
1141 sizeof(struct in6_addr)))
1142 return GNUNET_SYSERR;
1149 * Function to free last resources associated with a session.
1151 * @param s session to free
1154 free_session (struct Session *s)
1156 if (NULL != s->frag_ctx)
1158 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL );
1159 GNUNET_free(s->frag_ctx);
1167 * Remove a message from the transmission queue.
1169 * @param plugin the UDP plugin
1170 * @param udpw message wrapper to queue
1173 dequeue (struct Plugin *plugin,
1174 struct UDP_MessageWrapper *udpw)
1176 struct Session *session = udpw->session;
1178 if (plugin->bytes_in_buffer < udpw->msg_size)
1184 GNUNET_STATISTICS_update (plugin->env->stats,
1185 "# UDP, total, bytes in buffers",
1186 -(long long) udpw->msg_size,
1188 plugin->bytes_in_buffer -= udpw->msg_size;
1190 GNUNET_STATISTICS_update (plugin->env->stats,
1191 "# UDP, total, msgs in buffers",
1193 if (udpw->session->address->address_length == sizeof(struct IPv4UdpAddress))
1194 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1195 plugin->ipv4_queue_tail,
1197 else if (udpw->session->address->address_length == sizeof(struct IPv6UdpAddress))
1198 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1199 plugin->ipv6_queue_tail,
1206 GNUNET_assert (session->msgs_in_queue > 0);
1207 session->msgs_in_queue--;
1208 GNUNET_assert (session->bytes_in_queue >= udpw->msg_size);
1209 session->bytes_in_queue -= udpw->msg_size;
1217 fragmented_message_done (struct UDP_FragmentationContext *fc,
1220 struct Plugin *plugin = fc->plugin;
1221 struct Session *s = fc->session;
1222 struct UDP_MessageWrapper *udpw;
1223 struct UDP_MessageWrapper *tmp;
1224 struct UDP_MessageWrapper dummy;
1226 LOG (GNUNET_ERROR_TYPE_DEBUG,
1227 "%p : Fragmented message removed with result %s\n",
1229 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1231 /* Call continuation for fragmented message */
1232 memset (&dummy, 0, sizeof(dummy));
1233 dummy.msg_type = UMT_MSG_FRAGMENTED_COMPLETE;
1234 dummy.msg_size = s->frag_ctx->on_wire_size;
1235 dummy.payload_size = s->frag_ctx->payload_size;
1236 dummy.frag_ctx = s->frag_ctx;
1238 dummy.cont_cls = NULL;
1240 call_continuation (&dummy, result);
1241 /* Remove leftover fragments from queue */
1242 if (s->address->address_length == sizeof(struct IPv6UdpAddress))
1244 udpw = plugin->ipv6_queue_head;
1245 while (NULL != udpw)
1248 if ((udpw->frag_ctx != NULL )&& (udpw->frag_ctx == s->frag_ctx)){
1249 dequeue (plugin, udpw);
1250 call_continuation (udpw, GNUNET_SYSERR);
1256 if (s->address->address_length == sizeof(struct IPv4UdpAddress))
1258 udpw = plugin->ipv4_queue_head;
1259 while (udpw != NULL )
1262 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1264 dequeue (plugin, udpw);
1265 call_continuation (udpw, GNUNET_SYSERR);
1271 notify_session_monitor (s->plugin,
1273 GNUNET_TRANSPORT_SS_UPDATE);
1274 /* Destroy fragmentation context */
1275 GNUNET_FRAGMENT_context_destroy (fc->frag,
1276 &s->last_expected_msg_delay,
1277 &s->last_expected_ack_delay);
1284 * Scan the heap for a receive context with the given address.
1286 * @param cls the `struct FindReceiveContext`
1287 * @param node internal node of the heap
1288 * @param element value stored at the node (a 'struct ReceiveContext')
1289 * @param cost cost associated with the node
1290 * @return #GNUNET_YES if we should continue to iterate,
1291 * #GNUNET_NO if not.
1294 find_receive_context (void *cls,
1295 struct GNUNET_CONTAINER_HeapNode *node,
1297 GNUNET_CONTAINER_HeapCostType cost)
1299 struct FindReceiveContext *frc = cls;
1300 struct DefragContext *e = element;
1302 if ((frc->addr_len == e->addr_len)
1303 && (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1313 * Functions with this signature are called whenever we need
1314 * to close a session due to a disconnect or failure to
1315 * establish a connection.
1317 * @param cls closure with the `struct Plugin`
1318 * @param s session to close down
1319 * @return #GNUNET_OK on success
1322 udp_disconnect_session (void *cls,
1325 struct Plugin *plugin = cls;
1326 struct UDP_MessageWrapper *udpw;
1327 struct UDP_MessageWrapper *next;
1328 struct FindReceiveContext frc;
1330 GNUNET_assert(GNUNET_YES != s->in_destroy);
1331 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' address ended\n", s,
1332 GNUNET_i2s (&s->target),
1333 udp_address_to_string (NULL, s->address->address, s->address->address_length));
1334 /* stop timeout task */
1335 if (NULL != s->timeout_task)
1337 GNUNET_SCHEDULER_cancel (s->timeout_task);
1338 s->timeout_task = NULL;
1340 if (NULL != s->frag_ctx)
1342 /* Remove fragmented message due to disconnect */
1343 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1347 frc.addr = s->address->address;
1348 frc.addr_len = s->address->address_length;
1349 /* Lookup existing receive context for this address */
1350 if (NULL != plugin->defrag_ctxs)
1352 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1353 &find_receive_context,
1357 struct DefragContext *d_ctx = frc.rc;
1359 GNUNET_CONTAINER_heap_remove_node (d_ctx->hnode);
1360 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1361 GNUNET_free (d_ctx);
1364 next = plugin->ipv4_queue_head;
1365 while (NULL != (udpw = next))
1368 if (udpw->session == s)
1370 dequeue (plugin, udpw);
1371 call_continuation (udpw, GNUNET_SYSERR);
1375 next = plugin->ipv6_queue_head;
1376 while (NULL != (udpw = next))
1379 if (udpw->session == s)
1381 dequeue (plugin, udpw);
1382 call_continuation (udpw, GNUNET_SYSERR);
1386 notify_session_monitor (s->plugin,
1388 GNUNET_TRANSPORT_SS_DONE);
1389 plugin->env->session_end (plugin->env->cls,
1393 if (NULL != s->frag_ctx)
1395 if (NULL != s->frag_ctx->cont)
1397 s->frag_ctx->cont (s->frag_ctx->cont_cls,
1400 s->frag_ctx->payload_size,
1401 s->frag_ctx->on_wire_size);
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1404 GNUNET_i2s (&s->target));
1408 GNUNET_assert(GNUNET_YES ==
1409 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1412 GNUNET_STATISTICS_set (plugin->env->stats,
1413 "# UDP sessions active",
1414 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1418 s->in_destroy = GNUNET_YES;
1422 GNUNET_HELLO_address_free (s->address);
1430 * Function that is called to get the keepalive factor.
1431 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1432 * calculate the interval between keepalive packets.
1434 * @param cls closure with the `struct Plugin`
1435 * @return keepalive factor
1438 udp_query_keepalive_factor (void *cls)
1445 * Destroy a session, plugin is being unloaded.
1447 * @param cls the `struct Plugin`
1448 * @param key hash of public key of target peer
1449 * @param value a `struct PeerSession *` to clean up
1450 * @return #GNUNET_OK (continue to iterate)
1453 disconnect_and_free_it (void *cls,
1454 const struct GNUNET_PeerIdentity *key,
1457 struct Plugin *plugin = cls;
1459 udp_disconnect_session (plugin, value);
1465 * Disconnect from a remote node. Clean up session if we have one for
1468 * @param cls closure for this call (should be handle to Plugin)
1469 * @param target the peeridentity of the peer to disconnect
1470 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1473 udp_disconnect (void *cls,
1474 const struct GNUNET_PeerIdentity *target)
1476 struct Plugin *plugin = cls;
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 "Disconnecting from peer `%s'\n",
1480 GNUNET_i2s (target));
1481 /* Clean up sessions */
1482 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1484 &disconnect_and_free_it,
1490 * Session was idle, so disconnect it
1492 * @param cls the `struct Session` to time out
1493 * @param tc scheduler context
1496 session_timeout (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 struct Session *s = cls;
1500 struct Plugin *plugin = s->plugin;
1501 struct GNUNET_TIME_Relative left;
1503 s->timeout_task = NULL;
1504 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1505 if (left.rel_value_us > 0)
1507 /* not actually our turn yet, but let's at least update
1508 the monitor, it may think we're about to die ... */
1509 notify_session_monitor (s->plugin,
1511 GNUNET_TRANSPORT_SS_UPDATE);
1512 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Session %p was idle for %s, disconnecting\n",
1520 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
1522 /* call session destroy function */
1523 udp_disconnect_session (plugin, s);
1528 * Increment session timeout due to activity
1530 * @param s session to reschedule timeout activity for
1533 reschedule_session_timeout (struct Session *s)
1535 if (GNUNET_YES == s->in_destroy)
1537 GNUNET_assert(NULL != s->timeout_task);
1538 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1545 static struct Session *
1546 create_session (struct Plugin *plugin,
1547 const struct GNUNET_HELLO_Address *address)
1551 s = GNUNET_new (struct Session);
1553 s->address = GNUNET_HELLO_address_copy (address);
1554 s->target = address->peer;
1555 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (
1556 GNUNET_TIME_UNIT_MILLISECONDS, 250);
1557 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1558 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1559 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1560 s->timeout = GNUNET_TIME_relative_to_absolute (UDP_SESSION_TIME_OUT);
1561 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1562 &session_timeout, s);
1568 * Function obtain the network type for a session
1570 * @param cls closure ('struct Plugin*')
1571 * @param session the session
1572 * @return the network type
1574 static enum GNUNET_ATS_Network_Type
1575 udp_get_network (void *cls,
1576 struct Session *session)
1578 return ntohl (session->ats.value);
1583 * Closure for #session_cmp_it().
1585 struct SessionCompareContext
1588 * Set to session matching the address.
1590 struct Session *res;
1593 * Address we are looking for.
1595 const struct GNUNET_HELLO_Address *address;
1600 * Find a session with a matching address.
1602 * @param cls the `struct SessionCompareContext *`
1603 * @param key peer identity (unused)
1604 * @param value the `struct Session *`
1605 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1608 session_cmp_it (void *cls,
1609 const struct GNUNET_PeerIdentity *key,
1612 struct SessionCompareContext *cctx = cls;
1613 const struct GNUNET_HELLO_Address *address = cctx->address;
1614 struct Session *s = value;
1616 LOG (GNUNET_ERROR_TYPE_DEBUG,
1617 "Comparing address %s <-> %s\n",
1618 udp_address_to_string (NULL,
1620 address->address_length),
1621 udp_address_to_string (NULL,
1622 s->address->address,
1623 s->address->address_length));
1624 if (0 == GNUNET_HELLO_address_cmp(s->address, cctx->address))
1634 * Creates a new outbound session the transport service will use to
1635 * send data to the peer
1637 * @param cls the plugin
1638 * @param address the address
1639 * @return the session or NULL of max connections exceeded
1641 static struct Session *
1642 udp_plugin_lookup_session (void *cls,
1643 const struct GNUNET_HELLO_Address *address)
1645 struct Plugin * plugin = cls;
1646 struct IPv6UdpAddress *udp_a6;
1647 struct IPv4UdpAddress *udp_a4;
1648 struct SessionCompareContext cctx;
1650 if ( (NULL == address->address) ||
1651 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1652 (address->address_length != sizeof (struct IPv6UdpAddress))))
1654 LOG (GNUNET_ERROR_TYPE_WARNING,
1655 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1656 address->address_length,
1657 sizeof (struct IPv4UdpAddress),
1658 sizeof (struct IPv6UdpAddress));
1662 if (address->address_length == sizeof(struct IPv4UdpAddress))
1664 if (plugin->sockv4 == NULL)
1666 udp_a4 = (struct IPv4UdpAddress *) address->address;
1667 if (udp_a4->u4_port == 0)
1671 if (address->address_length == sizeof(struct IPv6UdpAddress))
1673 if (plugin->sockv6 == NULL)
1675 udp_a6 = (struct IPv6UdpAddress *) address->address;
1676 if (udp_a6->u6_port == 0)
1680 /* check if session already exists */
1681 cctx.address = address;
1683 LOG (GNUNET_ERROR_TYPE_DEBUG,
1684 "Looking for existing session for peer `%s' `%s' \n",
1685 GNUNET_i2s (&address->peer),
1686 udp_address_to_string(NULL, address->address, address->address_length));
1687 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, &address->peer,
1688 session_cmp_it, &cctx);
1689 if (cctx.res != NULL )
1691 LOG (GNUNET_ERROR_TYPE_DEBUG,
1692 "Found existing session %p\n",
1701 * Context to lookup a session based on a IP address
1703 struct LookupContext
1708 struct Session *res;
1711 * The socket address
1713 const struct sockaddr *address;
1716 * The socket address length
1721 * Is a fragmentation context required for the session
1723 int must_have_frag_ctx;
1728 * Find a session with a matching address.
1729 * FIXME: very similar code to #udp_plugin_lookup_session() above.
1732 * @param cls the `struct LookupContext *`
1733 * @param key peer identity (unused)
1734 * @param value the `struct Session *`
1735 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1738 lookup_session_by_sockaddr_it (void *cls,
1739 const struct GNUNET_PeerIdentity *key,
1742 struct LookupContext *l_ctx = cls;
1743 struct Session *s = value;
1744 struct IPv4UdpAddress u4;
1745 struct IPv6UdpAddress u6;
1749 /* convert address */
1750 switch (l_ctx->address->sa_family)
1753 GNUNET_assert(l_ctx->addr_len == sizeof(struct sockaddr_in));
1754 memset (&u4, 0, sizeof(u4));
1755 u6.options = htonl (0);
1756 u4.ipv4_addr = ((struct sockaddr_in *) l_ctx->address)->sin_addr.s_addr;
1757 u4.u4_port = ((struct sockaddr_in *) l_ctx->address)->sin_port;
1762 GNUNET_assert(l_ctx->addr_len == sizeof(struct sockaddr_in6));
1763 memset (&u6, 0, sizeof(u6));
1764 u6.options = htonl (0);
1765 u6.ipv6_addr = ((struct sockaddr_in6 *) l_ctx->address)->sin6_addr;
1766 u6.u6_port = ((struct sockaddr_in6 *) l_ctx->address)->sin6_port;
1774 if ( (GNUNET_YES == l_ctx->must_have_frag_ctx) &&
1775 (NULL == s->frag_ctx))
1778 /* Does not compare peer identities but addresses */
1779 if ((args == s->address->address_length) &&
1780 (0 == memcmp (arg, s->address->address, args)))
1789 static struct Session *
1790 udp_plugin_create_session (void *cls,
1791 const struct GNUNET_HELLO_Address *address)
1793 struct Plugin *plugin = cls;
1795 struct IPv4UdpAddress *udp_v4;
1796 struct IPv6UdpAddress *udp_v6;
1798 s = create_session (plugin, address);
1799 if (sizeof (struct IPv4UdpAddress) == address->address_length)
1801 struct sockaddr_in v4;
1802 udp_v4 = (struct IPv4UdpAddress *) address->address;
1803 memset (&v4, '\0', sizeof (v4));
1804 v4.sin_family = AF_INET;
1805 #if HAVE_SOCKADDR_IN_SIN_LEN
1806 v4.sin_len = sizeof (struct sockaddr_in);
1808 v4.sin_port = udp_v4->u4_port;
1809 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
1810 s->ats = plugin->env->get_address_type (plugin->env->cls,
1811 (const struct sockaddr *) &v4,
1814 else if (sizeof (struct IPv6UdpAddress) == address->address_length)
1816 struct sockaddr_in6 v6;
1817 udp_v6 = (struct IPv6UdpAddress *) address->address;
1818 memset (&v6, '\0', sizeof (v6));
1819 v6.sin6_family = AF_INET6;
1820 #if HAVE_SOCKADDR_IN_SIN_LEN
1821 v6.sin6_len = sizeof (struct sockaddr_in6);
1823 v6.sin6_port = udp_v6->u6_port;
1824 v6.sin6_addr = udp_v6->ipv6_addr;
1825 s->ats = plugin->env->get_address_type (plugin->env->cls,
1826 (const struct sockaddr *) &v6, sizeof (v6));
1830 return NULL; /* protocol not supported or address invalid */
1831 LOG(GNUNET_ERROR_TYPE_DEBUG,
1832 "Creating new %s session %p for peer `%s' address `%s'\n",
1833 GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
1834 s, GNUNET_i2s (&address->peer),
1835 udp_address_to_string( NULL,address->address,address->address_length));
1837 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->sessions, &s->target, s, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1838 GNUNET_STATISTICS_set (plugin->env->stats, "# UDP sessions active",
1839 GNUNET_CONTAINER_multipeermap_size (plugin->sessions), GNUNET_NO);
1845 * Function that will be called whenever the transport service wants to
1846 * notify the plugin that a session is still active and in use and
1847 * therefore the session timeout for this session has to be updated
1849 * @param cls closure
1850 * @param peer which peer was the session for
1851 * @param session which session is being updated
1854 udp_plugin_update_session_timeout (void *cls,
1855 const struct GNUNET_PeerIdentity *peer,
1856 struct Session *session)
1858 struct Plugin *plugin = cls;
1861 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1868 /* Reschedule session timeout */
1869 reschedule_session_timeout (session);
1874 * Creates a new outbound session the transport service will use to send data to the
1877 * @param cls the plugin
1878 * @param address the address
1879 * @return the session or NULL of max connections exceeded
1881 static struct Session *
1882 udp_plugin_get_session (void *cls,
1883 const struct GNUNET_HELLO_Address *address)
1887 if (NULL == address)
1892 if ( (address->address_length != sizeof(struct IPv4UdpAddress)) &&
1893 (address->address_length != sizeof(struct IPv6UdpAddress)) )
1896 /* otherwise create new */
1897 if (NULL != (s = udp_plugin_lookup_session (cls, address)))
1899 return udp_plugin_create_session (cls, address);
1904 * Enqueue a message for transmission.
1906 * @param plugin the UDP plugin
1907 * @param udpw message wrapper to queue
1910 enqueue (struct Plugin *plugin,
1911 struct UDP_MessageWrapper *udpw)
1913 struct Session *session = udpw->session;
1915 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1921 GNUNET_STATISTICS_update (plugin->env->stats,
1922 "# UDP, total, bytes in buffers", udpw->msg_size, GNUNET_NO);
1923 plugin->bytes_in_buffer += udpw->msg_size;
1925 GNUNET_STATISTICS_update (plugin->env->stats,
1926 "# UDP, total, msgs in buffers",
1928 if (udpw->session->address->address_length == sizeof (struct IPv4UdpAddress))
1929 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1930 plugin->ipv4_queue_tail,
1932 else if (udpw->session->address->address_length == sizeof (struct IPv6UdpAddress))
1933 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1934 plugin->ipv6_queue_tail,
1941 session->msgs_in_queue++;
1942 session->bytes_in_queue += udpw->msg_size;
1947 * Fragment message was transmitted via UDP, let fragmentation know
1948 * to send the next fragment now.
1950 * @param cls the `struct UDPMessageWrapper *` of the fragment
1951 * @param target destination peer (ignored)
1952 * @param result #GNUNET_OK on success (ignored)
1953 * @param payload bytes payload sent
1954 * @param physical bytes physical sent
1957 send_next_fragment (void *cls,
1958 const struct GNUNET_PeerIdentity *target,
1963 struct UDP_MessageWrapper *udpw = cls;
1965 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1970 * Function that is called with messages created by the fragmentation
1971 * module. In the case of the 'proc' callback of the
1972 * #GNUNET_FRAGMENT_context_create() function, this function must
1973 * eventually call #GNUNET_FRAGMENT_context_transmission_done().
1975 * @param cls closure, the 'struct FragmentationContext'
1976 * @param msg the message that was created
1979 enqueue_fragment (void *cls,
1980 const struct GNUNET_MessageHeader *msg)
1982 struct UDP_FragmentationContext *frag_ctx = cls;
1983 struct Plugin *plugin = frag_ctx->plugin;
1984 struct UDP_MessageWrapper * udpw;
1985 size_t msg_len = ntohs (msg->size);
1987 LOG (GNUNET_ERROR_TYPE_DEBUG,
1988 "Enqueuing fragment with %u bytes\n",
1990 frag_ctx->fragments_used++;
1991 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1992 udpw->session = frag_ctx->session;
1993 udpw->msg_buf = (char *) &udpw[1];
1994 udpw->msg_size = msg_len;
1995 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1996 udpw->cont = &send_next_fragment;
1997 udpw->cont_cls = udpw;
1998 udpw->timeout = frag_ctx->timeout;
1999 udpw->frag_ctx = frag_ctx;
2000 udpw->msg_type = UMT_MSG_FRAGMENTED;
2001 memcpy (udpw->msg_buf, msg, msg_len);
2002 enqueue (plugin, udpw);
2003 schedule_select (plugin);
2008 * Function that can be used by the transport service to transmit
2009 * a message using the plugin. Note that in the case of a
2010 * peer disconnecting, the continuation MUST be called
2011 * prior to the disconnect notification itself. This function
2012 * will be called with this peer's HELLO message to initiate
2013 * a fresh connection to another peer.
2015 * @param cls closure
2016 * @param s which session must be used
2017 * @param msgbuf the message to transmit
2018 * @param msgbuf_size number of bytes in 'msgbuf'
2019 * @param priority how important is the message (most plugins will
2020 * ignore message priority and just FIFO)
2021 * @param to how long to wait at most for the transmission (does not
2022 * require plugins to discard the message after the timeout,
2023 * just advisory for the desired delay; most plugins will ignore
2025 * @param cont continuation to call once the message has
2026 * been transmitted (or if the transport is ready
2027 * for the next transmission call; or if the
2028 * peer disconnected...); can be NULL
2029 * @param cont_cls closure for cont
2030 * @return number of bytes used (on the physical network, with overheads);
2031 * -1 on hard errors (i.e. address invalid); 0 is a legal value
2032 * and does NOT mean that the message was not transmitted (DV)
2035 udp_plugin_send (void *cls,
2039 unsigned int priority,
2040 struct GNUNET_TIME_Relative to,
2041 GNUNET_TRANSPORT_TransmitContinuation cont,
2044 struct Plugin *plugin = cls;
2045 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
2046 struct UDP_FragmentationContext * frag_ctx;
2047 struct UDP_MessageWrapper * udpw;
2048 struct UDPMessage *udp;
2050 GNUNET_assert(plugin != NULL);
2051 GNUNET_assert(s != NULL);
2053 if ( (s->address->address_length == sizeof(struct IPv6UdpAddress)) &&
2054 (plugin->sockv6 == NULL) )
2055 return GNUNET_SYSERR;
2056 if ( (s->address->address_length == sizeof(struct IPv4UdpAddress)) &&
2057 (plugin->sockv4 == NULL) )
2058 return GNUNET_SYSERR;
2059 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
2062 return GNUNET_SYSERR;
2065 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
2070 return GNUNET_SYSERR;
2072 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "UDP transmits %u-byte message to `%s' using address `%s'\n",
2075 GNUNET_i2s (&s->target),
2076 udp_address_to_string (NULL,
2077 s->address->address,
2078 s->address->address_length));
2081 udp = (struct UDPMessage *) mbuf;
2082 udp->header.size = htons (udpmlen);
2083 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
2084 udp->reserved = htonl (0);
2085 udp->sender = *plugin->env->my_identity;
2087 /* We do not update the session time out here!
2088 * Otherwise this session will not timeout since we send keep alive before
2089 * session can timeout
2091 * For UDP we update session timeout only on receive, this will cover keep
2092 * alives, since remote peer will reply with keep alive response!
2094 if (udpmlen <= UDP_MTU)
2096 /* unfragmented message */
2097 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
2099 udpw->msg_buf = (char *) &udpw[1];
2100 udpw->msg_size = udpmlen; /* message size with UDP overhead */
2101 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
2102 udpw->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to);
2104 udpw->cont_cls = cont_cls;
2105 udpw->frag_ctx = NULL;
2106 udpw->msg_type = UMT_MSG_UNFRAGMENTED;
2107 memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
2108 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)], msgbuf, msgbuf_size);
2109 enqueue (plugin, udpw);
2111 GNUNET_STATISTICS_update (plugin->env->stats,
2112 "# UDP, unfragmented msgs, messages, attempt", 1, GNUNET_NO);
2113 GNUNET_STATISTICS_update (plugin->env->stats,
2114 "# UDP, unfragmented msgs, bytes payload, attempt",
2120 /* fragmented message */
2121 if (s->frag_ctx != NULL)
2122 return GNUNET_SYSERR;
2123 memcpy (&udp[1], msgbuf, msgbuf_size);
2124 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
2125 frag_ctx->plugin = plugin;
2126 frag_ctx->session = s;
2127 frag_ctx->cont = cont;
2128 frag_ctx->cont_cls = cont_cls;
2129 frag_ctx->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
2131 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
2132 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
2133 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
2136 s->last_expected_msg_delay,
2137 s->last_expected_ack_delay,
2141 s->frag_ctx = frag_ctx;
2142 GNUNET_STATISTICS_update (plugin->env->stats,
2143 "# UDP, fragmented msgs, messages, pending",
2146 GNUNET_STATISTICS_update (plugin->env->stats,
2147 "# UDP, fragmented msgs, messages, attempt",
2150 GNUNET_STATISTICS_update (plugin->env->stats,
2151 "# UDP, fragmented msgs, bytes payload, attempt",
2152 frag_ctx->payload_size,
2155 notify_session_monitor (s->plugin,
2157 GNUNET_TRANSPORT_SS_UPDATE);
2158 schedule_select (plugin);
2164 * Our external IP address/port mapping has changed.
2166 * @param cls closure, the `struct LocalAddrList`
2167 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
2168 * the previous (now invalid) one
2169 * @param addr either the previous or the new public IP address
2170 * @param addrlen actual lenght of the address
2173 udp_nat_port_map_callback (void *cls,
2175 const struct sockaddr *addr,
2178 struct Plugin *plugin = cls;
2179 struct GNUNET_HELLO_Address *address;
2180 struct IPv4UdpAddress u4;
2181 struct IPv6UdpAddress u6;
2185 LOG (GNUNET_ERROR_TYPE_INFO,
2186 "NAT notification to %s address `%s'\n",
2187 (GNUNET_YES == add_remove) ? "add" : "remove",
2188 GNUNET_a2s (addr, addrlen));
2190 /* convert 'address' to our internal format */
2191 switch (addr->sa_family)
2194 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
2195 memset (&u4, 0, sizeof(u4));
2196 u4.options = htonl (plugin->myoptions);
2197 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2198 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
2199 if (0 == ((struct sockaddr_in *) addr)->sin_port)
2202 args = sizeof(struct IPv4UdpAddress);
2205 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
2206 memset (&u6, 0, sizeof(u6));
2207 u6.options = htonl (plugin->myoptions);
2208 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
2210 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
2211 sizeof(struct in6_addr));
2212 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
2214 args = sizeof(struct IPv6UdpAddress);
2220 /* modify our published address list */
2221 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
2224 GNUNET_HELLO_ADDRESS_INFO_NONE);
2225 plugin->env->notify_address (plugin->env->cls, add_remove, address);
2226 GNUNET_HELLO_address_free (address);
2231 * Message tokenizer has broken up an incomming message. Pass it on
2234 * @param cls the `struct Plugin *`
2235 * @param client the `struct SourceInformation *`
2236 * @param hdr the actual message
2237 * @return #GNUNET_OK (always)
2240 process_inbound_tokenized_messages (void *cls,
2242 const struct GNUNET_MessageHeader *hdr)
2244 struct Plugin *plugin = cls;
2245 struct SourceInformation *si = client;
2246 struct GNUNET_TIME_Relative delay;
2248 GNUNET_assert(si->session != NULL);
2249 if (GNUNET_YES == si->session->in_destroy)
2252 GNUNET_break (ntohl (si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
2253 reschedule_session_timeout (si->session);
2254 delay = plugin->env->receive (plugin->env->cls,
2255 si->session->address,
2258 plugin->env->update_address_metrics (plugin->env->cls,
2259 si->session->address,
2261 &si->session->ats, 1);
2262 si->session->flow_delay_for_other_peer = delay;
2268 * We've received a UDP Message. Process it (pass contents to main service).
2270 * @param plugin plugin context
2271 * @param msg the message
2272 * @param sender_addr sender address
2273 * @param sender_addr_len number of bytes in @a sender_addr
2276 process_udp_message (struct Plugin *plugin,
2277 const struct UDPMessage *msg,
2278 const struct sockaddr *sender_addr,
2279 socklen_t sender_addr_len)
2281 struct SourceInformation si;
2283 struct GNUNET_HELLO_Address *address;
2284 struct IPv4UdpAddress u4;
2285 struct IPv6UdpAddress u6;
2289 if (0 != ntohl (msg->reserved))
2294 if (ntohs (msg->header.size)
2295 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
2301 /* convert address */
2302 switch (sender_addr->sa_family)
2305 GNUNET_assert(sender_addr_len == sizeof(struct sockaddr_in));
2306 memset (&u4, 0, sizeof(u4));
2307 u6.options = htonl (0);
2308 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2309 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2314 GNUNET_assert(sender_addr_len == sizeof(struct sockaddr_in6));
2315 memset (&u6, 0, sizeof(u6));
2316 u6.options = htonl (0);
2317 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2318 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2326 LOG(GNUNET_ERROR_TYPE_DEBUG,
2327 "Received message with %u bytes from peer `%s' at `%s'\n",
2328 (unsigned int ) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2329 GNUNET_a2s (sender_addr, sender_addr_len));
2331 address = GNUNET_HELLO_address_allocate ( &msg->sender, PLUGIN_NAME,
2333 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2334 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2336 s = udp_plugin_create_session (plugin, address);
2337 plugin->env->session_start (NULL, address, s, NULL, 0);
2338 notify_session_monitor (s->plugin,
2340 GNUNET_TRANSPORT_SS_INIT);
2341 notify_session_monitor (s->plugin,
2343 GNUNET_TRANSPORT_SS_UP);
2345 GNUNET_free (address);
2347 /* iterate over all embedded messages */
2349 si.sender = msg->sender;
2353 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2354 ntohs (msg->header.size) - sizeof(struct UDPMessage), GNUNET_YES,
2357 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
2363 * Process a defragmented message.
2365 * @param cls the `struct DefragContext *`
2366 * @param msg the message
2369 fragment_msg_proc (void *cls,
2370 const struct GNUNET_MessageHeader *msg)
2372 struct DefragContext *rc = cls;
2374 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2379 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2384 process_udp_message (rc->plugin,
2385 (const struct UDPMessage *) msg,
2392 * Transmit an acknowledgement.
2394 * @param cls the `struct DefragContext *`
2395 * @param id message ID (unused)
2396 * @param msg ack to transmit
2399 ack_proc (void *cls,
2401 const struct GNUNET_MessageHeader *msg)
2403 struct DefragContext *rc = cls;
2404 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2405 struct UDP_ACK_Message *udp_ack;
2407 struct UDP_MessageWrapper *udpw;
2409 struct LookupContext l_ctx;
2411 l_ctx.address = rc->src_addr;
2412 l_ctx.addr_len = rc->addr_len;
2413 l_ctx.must_have_frag_ctx = GNUNET_NO;
2415 GNUNET_CONTAINER_multipeermap_iterate (rc->plugin->sessions,
2416 &lookup_session_by_sockaddr_it,
2421 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2422 "Trying to transmit ACK to peer `%s' but not session found!\n",
2423 GNUNET_a2s(rc->src_addr, rc->addr_len));
2425 GNUNET_CONTAINER_heap_remove_node (rc->hnode);
2426 GNUNET_DEFRAGMENT_context_destroy (rc->defrag);
2431 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2432 delay = s->flow_delay_for_other_peer.rel_value_us;
2434 LOG (GNUNET_ERROR_TYPE_DEBUG,
2435 "Sending ACK to `%s' including delay of %s\n",
2436 GNUNET_a2s (rc->src_addr, (rc->src_addr->sa_family == AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)),
2437 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2439 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2440 udpw->msg_size = msize;
2441 udpw->payload_size = 0;
2443 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2444 udpw->msg_buf = (char *) &udpw[1];
2445 udpw->msg_type = UMT_MSG_ACK;
2446 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2447 udp_ack->header.size = htons ((uint16_t) msize);
2448 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2449 udp_ack->delay = htonl (delay);
2450 udp_ack->sender = *rc->plugin->env->my_identity;
2451 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2452 enqueue (rc->plugin, udpw);
2453 notify_session_monitor (s->plugin,
2455 GNUNET_TRANSPORT_SS_UPDATE);
2456 schedule_select (rc->plugin);
2464 read_process_msg (struct Plugin *plugin,
2465 const struct GNUNET_MessageHeader *msg,
2466 const struct sockaddr *addr,
2469 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2474 process_udp_message (plugin,
2475 (const struct UDPMessage *) msg,
2485 read_process_ack (struct Plugin *plugin,
2486 const struct GNUNET_MessageHeader *msg,
2487 const struct sockaddr *addr,
2490 const struct GNUNET_MessageHeader *ack;
2491 const struct UDP_ACK_Message *udp_ack;
2492 struct LookupContext l_ctx;
2494 struct GNUNET_TIME_Relative flow_delay;
2496 if (ntohs (msg->size)
2497 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2502 udp_ack = (const struct UDP_ACK_Message *) msg;
2504 /* Lookup session based on sockaddr */
2505 l_ctx.address = addr;
2506 l_ctx.addr_len = fromlen;
2508 l_ctx.must_have_frag_ctx = GNUNET_YES;
2509 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2510 &lookup_session_by_sockaddr_it, &l_ctx);
2512 if ((NULL == s) || (NULL == s->frag_ctx))
2517 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2518 LOG(GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %s\n",
2519 GNUNET_STRINGS_relative_time_to_string (flow_delay, GNUNET_YES));
2520 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2522 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2523 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2530 != memcmp (&l_ctx.res->target, &udp_ack->sender,
2531 sizeof(struct GNUNET_PeerIdentity)))
2533 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2535 LOG(GNUNET_ERROR_TYPE_DEBUG,
2536 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2537 (unsigned int ) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2538 GNUNET_a2s (addr, fromlen));
2539 /* Expect more ACKs to arrive */
2543 LOG(GNUNET_ERROR_TYPE_DEBUG, "Message full ACK'ed\n",
2544 (unsigned int ) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2545 GNUNET_a2s (addr, fromlen));
2547 /* Remove fragmented message after successful sending */
2548 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2556 read_process_fragment (struct Plugin *plugin,
2557 const struct GNUNET_MessageHeader *msg,
2558 const struct sockaddr *addr,
2561 struct DefragContext *d_ctx;
2562 struct GNUNET_TIME_Absolute now;
2563 struct FindReceiveContext frc;
2567 frc.addr_len = fromlen;
2569 LOG(GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2570 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2571 /* Lookup existing receive context for this address */
2572 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2573 &find_receive_context, &frc);
2574 now = GNUNET_TIME_absolute_get ();
2579 /* Create a new defragmentation context */
2580 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2581 memcpy (&d_ctx[1], addr, fromlen);
2582 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2583 d_ctx->addr_len = fromlen;
2584 d_ctx->plugin = plugin;
2585 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2586 UDP_MTU, UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx, &fragment_msg_proc,
2588 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2589 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2590 LOG(GNUNET_ERROR_TYPE_DEBUG,
2591 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2592 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2596 LOG(GNUNET_ERROR_TYPE_DEBUG,
2597 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2598 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2601 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2603 /* keep this 'rc' from expiring */
2604 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2605 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2607 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2608 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2610 /* remove 'rc' that was inactive the longest */
2611 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2612 GNUNET_assert(NULL != d_ctx);
2613 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2620 * Read and process a message from the given socket.
2622 * @param plugin the overall plugin
2623 * @param rsock socket to read from
2626 udp_select_read (struct Plugin *plugin,
2627 struct GNUNET_NETWORK_Handle *rsock)
2630 struct sockaddr_storage addr;
2631 char buf[65536] GNUNET_ALIGN;
2633 const struct GNUNET_MessageHeader *msg;
2635 fromlen = sizeof(addr);
2636 memset (&addr, 0, sizeof(addr));
2637 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof(buf),
2638 (struct sockaddr *) &addr, &fromlen);
2640 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2641 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2642 * on this socket has failed.
2644 * WSAECONNRESET - The virtual circuit was reset by the remote side
2645 * executing a hard or abortive close. The application should close
2646 * the socket; it is no longer usable. On a UDP-datagram socket this
2647 * error indicates a previous send operation resulted in an ICMP Port
2648 * Unreachable message.
2650 if ( (-1 == size) && (ECONNRESET == errno) )
2655 LOG (GNUNET_ERROR_TYPE_DEBUG,
2656 "UDP failed to receive data: %s\n",
2658 /* Connection failure or something. Not a protocol violation. */
2661 if (size < sizeof(struct GNUNET_MessageHeader))
2663 LOG (GNUNET_ERROR_TYPE_WARNING,
2664 "UDP got %u bytes from %s, which is not enough for a GNUnet message header\n",
2665 (unsigned int ) size,
2666 GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
2667 /* _MAY_ be a connection failure (got partial message) */
2668 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2672 msg = (const struct GNUNET_MessageHeader *) buf;
2674 LOG (GNUNET_ERROR_TYPE_DEBUG,
2675 "UDP received %u-byte message from `%s' type %u\n",
2676 (unsigned int) size,
2677 GNUNET_a2s ((const struct sockaddr *) &addr, fromlen),
2680 if (size != ntohs (msg->size))
2682 LOG (GNUNET_ERROR_TYPE_WARNING,
2683 "UDP malformed message header from %s\n",
2684 (unsigned int ) size,
2685 GNUNET_a2s ((const struct sockaddr *) &addr, fromlen));
2689 GNUNET_STATISTICS_update (plugin->env->stats,
2690 "# UDP, total, bytes, received",
2694 switch (ntohs (msg->type))
2696 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2697 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2698 udp_broadcast_receive (plugin, buf, size, (const struct sockaddr *) &addr,
2701 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2702 read_process_msg (plugin, msg, (const struct sockaddr *) &addr, fromlen);
2704 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2705 read_process_ack (plugin, msg, (const struct sockaddr *) &addr, fromlen);
2707 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2708 read_process_fragment (plugin, msg, (const struct sockaddr *) &addr,
2721 static struct UDP_MessageWrapper *
2722 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2723 struct GNUNET_NETWORK_Handle *sock)
2725 struct UDP_MessageWrapper *udpw = NULL;
2726 struct GNUNET_TIME_Relative remaining;
2727 struct Session *session;
2728 struct Plugin *plugin;
2731 removed = GNUNET_NO;
2733 while (NULL != udpw)
2735 session = udpw->session;
2736 plugin = session->plugin;
2737 /* Find messages with timeout */
2738 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2739 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2741 /* Message timed out */
2742 switch (udpw->msg_type)
2744 case UMT_MSG_UNFRAGMENTED:
2745 GNUNET_STATISTICS_update (plugin->env->stats,
2746 "# UDP, total, bytes, sent, timeout",
2749 GNUNET_STATISTICS_update (plugin->env->stats,
2750 "# UDP, total, messages, sent, timeout",
2753 GNUNET_STATISTICS_update (plugin->env->stats,
2754 "# UDP, unfragmented msgs, messages, sent, timeout",
2757 GNUNET_STATISTICS_update (plugin->env->stats,
2758 "# UDP, unfragmented msgs, bytes, sent, timeout",
2761 /* Not fragmented message */
2762 LOG (GNUNET_ERROR_TYPE_DEBUG,
2763 "Message for peer `%s' with size %u timed out\n",
2764 GNUNET_i2s (&udpw->session->target),
2765 udpw->payload_size);
2766 call_continuation (udpw, GNUNET_SYSERR);
2767 /* Remove message */
2768 removed = GNUNET_YES;
2769 dequeue (plugin, udpw);
2772 case UMT_MSG_FRAGMENTED:
2773 /* Fragmented message */
2774 GNUNET_STATISTICS_update (plugin->env->stats,
2775 "# UDP, total, bytes, sent, timeout",
2776 udpw->frag_ctx->on_wire_size,
2778 GNUNET_STATISTICS_update (plugin->env->stats,
2779 "# UDP, total, messages, sent, timeout",
2782 call_continuation (udpw, GNUNET_SYSERR);
2783 LOG (GNUNET_ERROR_TYPE_DEBUG,
2784 "Fragment for message for peer `%s' with size %u timed out\n",
2785 GNUNET_i2s (&udpw->session->target),
2786 udpw->frag_ctx->payload_size);
2788 GNUNET_STATISTICS_update (plugin->env->stats,
2789 "# UDP, fragmented msgs, messages, sent, timeout",
2792 GNUNET_STATISTICS_update (plugin->env->stats,
2793 "# UDP, fragmented msgs, bytes, sent, timeout",
2794 udpw->frag_ctx->payload_size,
2796 /* Remove fragmented message due to timeout */
2797 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2800 GNUNET_STATISTICS_update (plugin->env->stats,
2801 "# UDP, total, bytes, sent, timeout",
2804 GNUNET_STATISTICS_update (plugin->env->stats,
2805 "# UDP, total, messages, sent, timeout",
2808 LOG (GNUNET_ERROR_TYPE_DEBUG,
2809 "ACK Message for peer `%s' with size %u timed out\n",
2810 GNUNET_i2s (&udpw->session->target),
2811 udpw->payload_size);
2812 call_continuation (udpw, GNUNET_SYSERR);
2813 removed = GNUNET_YES;
2814 dequeue (plugin, udpw);
2820 if (sock == plugin->sockv4)
2821 udpw = plugin->ipv4_queue_head;
2822 else if (sock == plugin->sockv6)
2823 udpw = plugin->ipv6_queue_head;
2826 GNUNET_break(0); /* should never happen */
2829 GNUNET_STATISTICS_update (plugin->env->stats,
2830 "# messages discarded due to timeout",
2836 /* Message did not time out, check flow delay */
2837 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2838 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2840 /* this message is not delayed */
2841 LOG (GNUNET_ERROR_TYPE_DEBUG,
2842 "Message for peer `%s' (%u bytes) is not delayed \n",
2843 GNUNET_i2s (&udpw->session->target),
2844 udpw->payload_size);
2845 break; /* Found message to send, break */
2849 /* Message is delayed, try next */
2850 LOG (GNUNET_ERROR_TYPE_DEBUG,
2851 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2852 GNUNET_i2s (&udpw->session->target), udpw->payload_size,
2853 GNUNET_STRINGS_relative_time_to_string (remaining, GNUNET_YES));
2858 if (GNUNET_YES == removed)
2859 notify_session_monitor (session->plugin,
2861 GNUNET_TRANSPORT_SS_UPDATE);
2870 analyze_send_error (struct Plugin *plugin,
2871 const struct sockaddr *sa,
2872 socklen_t slen, int error)
2874 struct GNUNET_ATS_Information type;
2876 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
2877 if (((GNUNET_ATS_NET_LAN == ntohl (type.value))
2878 || (GNUNET_ATS_NET_WAN == ntohl (type.value)))
2879 && ((ENETUNREACH == errno)|| (ENETDOWN == errno)))
2881 if (slen == sizeof (struct sockaddr_in))
2883 /* IPv4: "Network unreachable" or "Network down"
2885 * This indicates we do not have connectivity
2887 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2888 _("UDP could not transmit message to `%s': "
2889 "Network seems down, please check your network configuration\n"),
2890 GNUNET_a2s (sa, slen));
2892 if (slen == sizeof (struct sockaddr_in6))
2894 /* IPv6: "Network unreachable" or "Network down"
2896 * This indicates that this system is IPv6 enabled, but does not
2897 * have a valid global IPv6 address assigned or we do not have
2900 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2901 _("UDP could not transmit IPv6 message! "
2902 "Please check your network configuration and disable IPv6 if your "
2903 "connection does not have a global IPv6 address\n"));
2908 LOG (GNUNET_ERROR_TYPE_WARNING,
2909 "UDP could not transmit message to `%s': `%s'\n",
2910 GNUNET_a2s (sa, slen), STRERROR (error));
2919 udp_select_send (struct Plugin *plugin,
2920 struct GNUNET_NETWORK_Handle *sock)
2925 const struct IPv4UdpAddress *u4;
2926 struct sockaddr_in a4;
2927 const struct IPv6UdpAddress *u6;
2928 struct sockaddr_in6 a6;
2929 struct UDP_MessageWrapper *udpw;
2931 /* Find message to send */
2932 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4)
2933 ? plugin->ipv4_queue_head
2934 : plugin->ipv6_queue_head,
2937 return 0; /* No message to send */
2939 if (sizeof (struct IPv4UdpAddress) == udpw->session->address->address_length)
2941 u4 = udpw->session->address->address;
2942 memset (&a4, 0, sizeof(a4));
2943 a4.sin_family = AF_INET;
2944 #if HAVE_SOCKADDR_IN_SIN_LEN
2945 a4.sin_len = sizeof (a4);
2947 a4.sin_port = u4->u4_port;
2948 memcpy (&a4.sin_addr, &u4->ipv4_addr, sizeof(struct in_addr));
2949 a = (struct sockaddr *) &a4;
2952 else if (sizeof (struct IPv6UdpAddress) == udpw->session->address->address_length)
2954 u6 = udpw->session->address->address;
2955 memset (&a6, 0, sizeof(a6));
2956 a6.sin6_family = AF_INET6;
2957 #if HAVE_SOCKADDR_IN_SIN_LEN
2958 a6.sin6_len = sizeof (a6);
2960 a6.sin6_port = u6->u6_port;
2961 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
2962 a = (struct sockaddr *) &a6;
2967 call_continuation (udpw, GNUNET_OK);
2968 dequeue (plugin, udpw);
2969 notify_session_monitor (plugin,
2971 GNUNET_TRANSPORT_SS_UPDATE);
2973 return GNUNET_SYSERR;
2976 sent = GNUNET_NETWORK_socket_sendto (sock,
2981 if (GNUNET_SYSERR == sent)
2984 analyze_send_error (plugin, a, slen, errno);
2985 call_continuation (udpw, GNUNET_SYSERR);
2986 GNUNET_STATISTICS_update (plugin->env->stats,
2987 "# UDP, total, bytes, sent, failure", sent, GNUNET_NO);
2988 GNUNET_STATISTICS_update (plugin->env->stats,
2989 "# UDP, total, messages, sent, failure", 1, GNUNET_NO);
2994 LOG(GNUNET_ERROR_TYPE_DEBUG,
2995 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2996 (unsigned int ) (udpw->msg_size), GNUNET_i2s (&udpw->session->target),
2997 GNUNET_a2s (a, slen), (int ) sent,
2998 (sent < 0) ? STRERROR (errno) : "ok");
2999 GNUNET_STATISTICS_update (plugin->env->stats,
3000 "# UDP, total, bytes, sent, success", sent, GNUNET_NO);
3001 GNUNET_STATISTICS_update (plugin->env->stats,
3002 "# UDP, total, messages, sent, success", 1, GNUNET_NO);
3003 if (NULL != udpw->frag_ctx)
3004 udpw->frag_ctx->on_wire_size += udpw->msg_size;
3005 call_continuation (udpw, GNUNET_OK);
3007 dequeue (plugin, udpw);
3008 notify_session_monitor (plugin,
3010 GNUNET_TRANSPORT_SS_UPDATE);
3017 * We have been notified that our readset has something to read. We don't
3018 * know which socket needs to be read, so we have to check each one
3019 * Then reschedule this function to be called again once more is available.
3021 * @param cls the plugin handle
3022 * @param tc the scheduling context (for rescheduling this function again)
3025 udp_plugin_select (void *cls,
3026 const struct GNUNET_SCHEDULER_TaskContext *tc)
3028 struct Plugin *plugin = cls;
3030 plugin->select_task = NULL;
3031 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3033 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
3034 && (NULL != plugin->sockv4)
3035 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
3036 udp_select_read (plugin, plugin->sockv4);
3037 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3038 && (NULL != plugin->sockv4) && (NULL != plugin->ipv4_queue_head)
3039 && (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
3040 udp_select_send (plugin, plugin->sockv4);
3041 schedule_select (plugin);
3046 * We have been notified that our readset has something to read. We don't
3047 * know which socket needs to be read, so we have to check each one
3048 * Then reschedule this function to be called again once more is available.
3050 * @param cls the plugin handle
3051 * @param tc the scheduling context (for rescheduling this function again)
3054 udp_plugin_select_v6 (void *cls,
3055 const struct GNUNET_SCHEDULER_TaskContext *tc)
3057 struct Plugin *plugin = cls;
3059 plugin->select_task_v6 = NULL;
3060 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3062 if (((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
3063 && (NULL != plugin->sockv6)
3064 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
3065 udp_select_read (plugin, plugin->sockv6);
3066 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
3067 && (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL )&&
3068 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )udp_select_send (plugin, plugin->sockv6);
3069 schedule_select (plugin);
3074 * Setup the UDP sockets (for IPv4 and IPv6) for the plugin.
3076 * @param plugin the plugin to initialize
3077 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
3078 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
3079 * @return number of sockets that were successfully bound
3082 setup_sockets (struct Plugin *plugin,
3083 const struct sockaddr_in6 *bind_v6,
3084 const struct sockaddr_in *bind_v4)
3087 int sockets_created = 0;
3088 struct sockaddr_in6 server_addrv6;
3089 struct sockaddr_in server_addrv4;
3090 struct sockaddr *server_addr;
3091 struct sockaddr *addrs[2];
3092 socklen_t addrlens[2];
3096 /* Create IPv6 socket */
3098 if (GNUNET_YES == plugin->enable_ipv6)
3100 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
3101 if (NULL == plugin->sockv6)
3103 LOG(GNUNET_ERROR_TYPE_WARNING,
3104 "Disabling IPv6 since it is not supported on this system!\n");
3105 plugin->enable_ipv6 = GNUNET_NO;
3109 memset (&server_addrv6, '\0', sizeof(struct sockaddr_in6));
3110 #if HAVE_SOCKADDR_IN_SIN_LEN
3111 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
3113 server_addrv6.sin6_family = AF_INET6;
3114 if (NULL != bind_v6)
3115 server_addrv6.sin6_addr = bind_v6->sin6_addr;
3117 server_addrv6.sin6_addr = in6addr_any;
3119 if (0 == plugin->port) /* autodetect */
3120 server_addrv6.sin6_port = htons (
3121 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3124 server_addrv6.sin6_port = htons (plugin->port);
3125 addrlen = sizeof(struct sockaddr_in6);
3126 server_addr = (struct sockaddr *) &server_addrv6;
3131 LOG(GNUNET_ERROR_TYPE_DEBUG,
3132 "Binding to IPv6 `%s'\n",
3133 GNUNET_a2s (server_addr, addrlen));
3136 == GNUNET_NETWORK_socket_bind (plugin->sockv6, server_addr,
3140 if (0 != plugin->port)
3142 tries = 10; /* fail */
3143 break; /* bind failed on specific port */
3146 server_addrv6.sin6_port = htons (
3147 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3153 GNUNET_NETWORK_socket_close (plugin->sockv6);
3154 plugin->enable_ipv6 = GNUNET_NO;
3155 plugin->sockv6 = NULL;
3158 if (plugin->sockv6 != NULL )
3160 LOG (GNUNET_ERROR_TYPE_DEBUG,
3161 "IPv6 socket created on port %s\n",
3162 GNUNET_a2s (server_addr, addrlen));
3163 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
3164 addrlens[sockets_created] = sizeof(struct sockaddr_in6);
3169 LOG (GNUNET_ERROR_TYPE_ERROR,
3170 "Failed to bind UDP socket to %s: %s\n",
3171 GNUNET_a2s (server_addr, addrlen),
3177 /* Create IPv4 socket */
3179 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
3180 if (NULL == plugin->sockv4)
3182 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
3184 LOG(GNUNET_ERROR_TYPE_WARNING,
3185 "Disabling IPv4 since it is not supported on this system!\n");
3186 plugin->enable_ipv4 = GNUNET_NO;
3190 memset (&server_addrv4, '\0', sizeof(struct sockaddr_in));
3191 #if HAVE_SOCKADDR_IN_SIN_LEN
3192 server_addrv4.sin_len = sizeof (struct sockaddr_in);
3194 server_addrv4.sin_family = AF_INET;
3195 if (NULL != bind_v4)
3196 server_addrv4.sin_addr = bind_v4->sin_addr;
3198 server_addrv4.sin_addr.s_addr = INADDR_ANY;
3200 if (0 == plugin->port)
3202 server_addrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
3206 server_addrv4.sin_port = htons (plugin->port);
3208 addrlen = sizeof(struct sockaddr_in);
3209 server_addr = (struct sockaddr *) &server_addrv4;
3214 LOG (GNUNET_ERROR_TYPE_DEBUG,
3215 "Binding to IPv4 `%s'\n",
3216 GNUNET_a2s (server_addr, addrlen));
3220 == GNUNET_NETWORK_socket_bind (plugin->sockv4, server_addr, addrlen))
3223 if (0 != plugin->port)
3225 tries = 10; /* fail */
3226 break; /* bind failed on specific port */
3230 server_addrv4.sin_port = htons (
3231 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
3238 GNUNET_NETWORK_socket_close (plugin->sockv4);
3239 plugin->enable_ipv4 = GNUNET_NO;
3240 plugin->sockv4 = NULL;
3243 if (NULL != plugin->sockv4)
3245 LOG(GNUNET_ERROR_TYPE_DEBUG, "IPv4 socket created on port %s\n",
3246 GNUNET_a2s (server_addr, addrlen));
3247 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
3248 addrlens[sockets_created] = sizeof(struct sockaddr_in);
3253 LOG (GNUNET_ERROR_TYPE_ERROR,
3254 _("Failed to bind UDP socket to %s: %s\n"),
3255 GNUNET_a2s (server_addr, addrlen),
3260 if (0 == sockets_created)
3262 LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
3263 return 0; /* No sockets created, return */
3266 /* Create file descriptors */
3267 if (plugin->enable_ipv4 == GNUNET_YES)
3269 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
3270 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
3271 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
3272 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
3273 if (NULL != plugin->sockv4)
3275 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
3276 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
3280 if (plugin->enable_ipv6 == GNUNET_YES)
3282 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
3283 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
3284 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
3285 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
3286 if (NULL != plugin->sockv6)
3288 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
3289 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
3293 schedule_select (plugin);
3294 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3298 (const struct sockaddr **) addrs,
3300 &udp_nat_port_map_callback,
3304 return sockets_created;
3309 * Return information about the given session to the
3312 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3313 * @param peer peer we send information about
3314 * @param value our `struct Session` to send information about
3315 * @return #GNUNET_OK (continue to iterate)
3318 send_session_info_iter (void *cls,
3319 const struct GNUNET_PeerIdentity *peer,
3322 struct Plugin *plugin = cls;
3323 struct Session *session = value;
3325 notify_session_monitor (plugin,
3327 GNUNET_TRANSPORT_SS_INIT);
3328 notify_session_monitor (plugin,
3330 GNUNET_TRANSPORT_SS_UP);
3336 * Begin monitoring sessions of a plugin. There can only
3337 * be one active monitor per plugin (i.e. if there are
3338 * multiple monitors, the transport service needs to
3339 * multiplex the generated events over all of them).
3341 * @param cls closure of the plugin
3342 * @param sic callback to invoke, NULL to disable monitor;
3343 * plugin will being by iterating over all active
3344 * sessions immediately and then enter monitor mode
3345 * @param sic_cls closure for @a sic
3348 udp_plugin_setup_monitor (void *cls,
3349 GNUNET_TRANSPORT_SessionInfoCallback sic,
3352 struct Plugin *plugin = cls;
3355 plugin->sic_cls = sic_cls;
3358 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3359 &send_session_info_iter,
3361 /* signal end of first iteration */
3362 sic (sic_cls, NULL, NULL);
3368 * The exported method. Makes the core api available via a global and
3369 * returns the udp transport API.
3371 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3372 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
3375 libgnunet_plugin_transport_udp_init (void *cls)
3377 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3378 struct GNUNET_TRANSPORT_PluginFunctions *api;
3380 unsigned long long port;
3381 unsigned long long aport;
3382 unsigned long long udp_max_bps;
3383 unsigned long long enable_v6;
3384 unsigned long long enable_broadcasting;
3385 unsigned long long enable_broadcasting_recv;
3386 char *bind4_address;
3387 char *bind6_address;
3388 char *fancy_interval;
3389 struct GNUNET_TIME_Relative interval;
3390 struct sockaddr_in server_addrv4;
3391 struct sockaddr_in6 server_addrv6;
3396 if (NULL == env->receive)
3398 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3399 initialze the plugin or the API */
3400 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3402 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3403 api->address_to_string = &udp_address_to_string;
3404 api->string_to_address = &udp_string_to_address;
3408 /* Get port number: port == 0 : autodetect a port,
3409 * > 0 : use this port, not given : 2086 default */
3411 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3415 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3416 "ADVERTISED_PORT", &aport))
3420 LOG (GNUNET_ERROR_TYPE_WARNING,
3421 _("Given `%s' option is out of range: %llu > %u\n"),
3429 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6"))
3430 enable_v6 = GNUNET_NO;
3432 enable_v6 = GNUNET_YES;
3435 have_bind4 = GNUNET_NO;
3436 memset (&server_addrv4, 0, sizeof(server_addrv4));
3438 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3439 "BINDTO", &bind4_address))
3441 LOG (GNUNET_ERROR_TYPE_DEBUG,
3442 "Binding udp plugin to specific address: `%s'\n",
3444 if (1 != inet_pton (AF_INET,
3446 &server_addrv4.sin_addr))
3448 GNUNET_free (bind4_address);
3451 have_bind4 = GNUNET_YES;
3453 GNUNET_free_non_null(bind4_address);
3454 have_bind6 = GNUNET_NO;
3455 memset (&server_addrv6, 0, sizeof(server_addrv6));
3457 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3458 "BINDTO6", &bind6_address))
3460 LOG (GNUNET_ERROR_TYPE_DEBUG,
3461 "Binding udp plugin to specific address: `%s'\n",
3463 if (1 != inet_pton (AF_INET6,
3465 &server_addrv6.sin6_addr))
3467 LOG (GNUNET_ERROR_TYPE_ERROR,
3468 _("Invalid IPv6 address: `%s'\n"),
3470 GNUNET_free (bind6_address);
3473 have_bind6 = GNUNET_YES;
3475 GNUNET_free_non_null (bind6_address);
3477 /* Enable neighbour discovery */
3478 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3479 "transport-udp", "BROADCAST");
3480 if (enable_broadcasting == GNUNET_SYSERR)
3481 enable_broadcasting = GNUNET_NO;
3483 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3484 "transport-udp", "BROADCAST_RECEIVE");
3485 if (enable_broadcasting_recv == GNUNET_SYSERR)
3486 enable_broadcasting_recv = GNUNET_YES;
3488 if (GNUNET_SYSERR ==
3489 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3490 "BROADCAST_INTERVAL",
3493 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3497 if (GNUNET_SYSERR ==
3498 GNUNET_STRINGS_fancy_time_to_relative (fancy_interval, &interval))
3500 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3502 GNUNET_free(fancy_interval);
3505 /* Maximum datarate */
3507 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3508 "MAX_BPS", &udp_max_bps))
3510 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3513 p = GNUNET_new (struct Plugin);
3516 p->broadcast_interval = interval;
3517 p->enable_ipv6 = enable_v6;
3518 p->enable_ipv4 = GNUNET_YES; /* default */
3519 p->enable_broadcasting = enable_broadcasting;
3520 p->enable_broadcasting_receiving = enable_broadcasting_recv;
3522 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3523 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (
3524 GNUNET_CONTAINER_HEAP_ORDER_MIN);
3525 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3526 GNUNET_BANDWIDTH_tracker_init (&p->tracker, NULL, NULL,
3527 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps), 30);
3528 LOG(GNUNET_ERROR_TYPE_DEBUG,
3529 "Setting up sockets\n");
3530 res = setup_sockets (p,
3531 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3532 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
3533 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3535 LOG (GNUNET_ERROR_TYPE_ERROR,
3536 _("Failed to create network sockets, plugin failed\n"));
3537 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3538 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3539 GNUNET_SERVER_mst_destroy (p->mst);
3544 /* Setup broadcasting and receiving beacons */
3545 setup_broadcast (p, &server_addrv6, &server_addrv4);
3547 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3550 api->disconnect_session = &udp_disconnect_session;
3551 api->query_keepalive_factor = &udp_query_keepalive_factor;
3552 api->disconnect_peer = &udp_disconnect;
3553 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3554 api->address_to_string = &udp_address_to_string;
3555 api->string_to_address = &udp_string_to_address;
3556 api->check_address = &udp_plugin_check_address;
3557 api->get_session = &udp_plugin_get_session;
3558 api->send = &udp_plugin_send;
3559 api->get_network = &udp_get_network;
3560 api->update_session_timeout = &udp_plugin_update_session_timeout;
3561 api->setup_monitor = &udp_plugin_setup_monitor;
3567 * Function called on each entry in the defragmentation heap to
3571 * @param node node in the heap (to be removed)
3572 * @param element a `struct DefragContext` to be cleaned up
3573 * @param cost unused
3574 * @return #GNUNET_YES
3577 heap_cleanup_iterator (void *cls,
3578 struct GNUNET_CONTAINER_HeapNode *node,
3580 GNUNET_CONTAINER_HeapCostType cost)
3582 struct DefragContext *d_ctx = element;
3584 GNUNET_CONTAINER_heap_remove_node (node);
3585 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
3586 GNUNET_free (d_ctx);
3592 * The exported method. Makes the core api available via a global and
3593 * returns the udp transport API.
3595 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
3599 libgnunet_plugin_transport_udp_done (void *cls)
3601 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3602 struct Plugin *plugin = api->cls;
3603 struct PrettyPrinterContext *cur;
3604 struct PrettyPrinterContext *next;
3605 struct UDP_MessageWrapper *udpw;
3612 stop_broadcast (plugin);
3613 if (plugin->select_task != NULL )
3615 GNUNET_SCHEDULER_cancel (plugin->select_task);
3616 plugin->select_task = NULL;
3618 if (plugin->select_task_v6 != NULL )
3620 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3621 plugin->select_task_v6 = NULL;
3624 /* Closing sockets */
3625 if (GNUNET_YES == plugin->enable_ipv4)
3627 if (NULL != plugin->sockv4)
3629 GNUNET_break (GNUNET_OK ==
3630 GNUNET_NETWORK_socket_close (plugin->sockv4));
3631 plugin->sockv4 = NULL;
3633 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3634 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3636 if (GNUNET_YES == plugin->enable_ipv6)
3638 if (NULL != plugin->sockv6)
3640 GNUNET_break (GNUNET_OK ==
3641 GNUNET_NETWORK_socket_close (plugin->sockv6));
3642 plugin->sockv6 = NULL;
3644 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3645 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3648 if (NULL != plugin->nat)
3650 GNUNET_NAT_unregister (plugin->nat);
3653 if (NULL != plugin->defrag_ctxs)
3655 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3656 &heap_cleanup_iterator, NULL);
3657 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3658 plugin->defrag_ctxs = NULL;
3660 if (NULL != plugin->mst)
3662 GNUNET_SERVER_mst_destroy (plugin->mst);
3666 /* Clean up leftover messages */
3667 udpw = plugin->ipv4_queue_head;
3668 while (NULL != udpw)
3670 struct UDP_MessageWrapper *tmp = udpw->next;
3671 dequeue (plugin, udpw);
3672 call_continuation (udpw, GNUNET_SYSERR);
3676 udpw = plugin->ipv6_queue_head;
3677 while (NULL != udpw)
3679 struct UDP_MessageWrapper *tmp = udpw->next;
3680 dequeue (plugin, udpw);
3681 call_continuation (udpw, GNUNET_SYSERR);
3686 /* Clean up sessions */
3687 LOG (GNUNET_ERROR_TYPE_DEBUG,
3688 "Cleaning up sessions\n");
3689 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3690 &disconnect_and_free_it, plugin);
3691 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3693 next = plugin->ppc_dll_head;
3694 for (cur = next; NULL != cur; cur = next)
3698 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3699 plugin->ppc_dll_tail,
3701 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3704 GNUNET_free (plugin);
3709 /* end of plugin_transport_udp.c */