2 This file is part of GNUnet
3 (C) 2010-2013 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)
47 #define PLUGIN_NAME "udp"
50 * Number of messages we can defragment in parallel. We only really
51 * defragment 1 message at a time, but if messages get re-ordered, we
52 * may want to keep knowledge about the previous message to avoid
53 * discarding the current message in favor of a single fragment of a
54 * previous message. 3 should be good since we don't expect massive
55 * message reorderings with UDP.
57 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
60 * We keep a defragmentation queue per sender address. How many
61 * sender addresses do we support at the same time? Memory consumption
62 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
63 * value. (So 128 corresponds to 12 MB and should suffice for
64 * connecting to roughly 128 peers via UDP).
66 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
69 * Running pretty printers: head
71 static struct PrettyPrinterContext *ppc_dll_head;
74 * Running pretty printers: tail
76 static struct PrettyPrinterContext *ppc_dll_tail;
79 * Closure for 'append_port'.
81 struct PrettyPrinterContext
86 struct PrettyPrinterContext *next;
91 struct PrettyPrinterContext *prev;
96 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
101 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
104 * Function to call with the result.
106 GNUNET_TRANSPORT_AddressStringCallback asc;
114 * Port to add after the IP address.
135 MSG_FRAGMENTED_COMPLETE = 2,
136 MSG_UNFRAGMENTED = 3,
145 * Which peer is this session for?
147 struct GNUNET_PeerIdentity target;
150 * Plugin this session belongs to.
152 struct Plugin *plugin;
155 * Context for dealing with fragments.
157 struct UDP_FragmentationContext *frag_ctx;
160 * Address of the other peer
162 const struct sockaddr *sock_addr;
165 * Desired delay for next sending we send to other peer
167 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
170 * Desired delay for next sending we received from other peer
172 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
175 * Session timeout task
177 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
180 * expected delay for ACKs
182 struct GNUNET_TIME_Relative last_expected_ack_delay;
185 * desired delay between UDP messages
187 struct GNUNET_TIME_Relative last_expected_msg_delay;
189 struct GNUNET_ATS_Information ats;
192 * Number of bytes in @e sock_addr.
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).
217 * Closure for #session_cmp_it().
219 struct SessionCompareContext
222 const struct GNUNET_HELLO_Address *addr;
228 * Closure for #process_inbound_tokenized_messages().
230 struct SourceInformation
235 struct GNUNET_PeerIdentity sender;
243 * Associated session.
245 struct Session *session;
248 * Number of bytes in source address.
256 * Closure for #find_receive_context().
258 struct FindReceiveContext
261 * Where to store the result.
263 struct DefragContext *rc;
268 const struct sockaddr *addr;
270 struct Session *session;
273 * Number of bytes in @e addr.
282 * Data structure to track defragmentation contexts based
283 * on the source of the UDP traffic.
289 * Defragmentation context.
291 struct GNUNET_DEFRAGMENT_Context *defrag;
294 * Source address this receive context is for (allocated at the
295 * end of the struct).
297 const struct sockaddr *src_addr;
300 * Reference to master plugin struct.
302 struct Plugin *plugin;
305 * Node in the defrag heap.
307 struct GNUNET_CONTAINER_HeapNode *hnode;
310 * Length of 'src_addr'
318 * Context to send fragmented messages
320 struct UDP_FragmentationContext
323 * Next in linked list
325 struct UDP_FragmentationContext *next;
328 * Previous in linked list
330 struct UDP_FragmentationContext *prev;
335 struct Plugin *plugin;
338 * Handle for GNUNET_FRAGMENT context
340 struct GNUNET_FRAGMENT_Context *frag;
343 * The session this fragmentation context belongs to
345 struct Session *session;
348 * Function to call upon completion of the transmission.
350 GNUNET_TRANSPORT_TransmitContinuation cont;
353 * Closure for @e cont.
360 struct GNUNET_TIME_Absolute timeout;
363 * Payload size of original unfragmented message
368 * Bytes used to send all fragments on wire including UDP overhead
372 unsigned int fragments_used;
377 struct UDP_MessageWrapper
380 * Session this message belongs to
382 struct Session *session;
388 struct UDP_MessageWrapper *prev;
394 struct UDP_MessageWrapper *next;
398 * According to UDP_MessageType
403 * Message with size msg_size including UDP specific overhead
408 * Size of UDP message to send including UDP specific overhead
413 * Payload size of original message
420 struct GNUNET_TIME_Absolute timeout;
423 * Function to call upon completion of the transmission.
425 GNUNET_TRANSPORT_TransmitContinuation cont;
428 * Closure for 'cont'.
433 * Fragmentation context
434 * frag_ctx == NULL if transport <= MTU
435 * frag_ctx != NULL if transport > MTU
437 struct UDP_FragmentationContext *frag_ctx;
442 * UDP ACK Message-Packet header (after defragmentation).
444 struct UDP_ACK_Message
449 struct GNUNET_MessageHeader header;
452 * Desired delay for flow control
457 * What is the identity of the sender
459 struct GNUNET_PeerIdentity sender;
466 static uint32_t myoptions;
470 * Encapsulation of all of the state of the plugin.
472 struct Plugin * plugin;
476 * We have been notified that our readset has something to read. We don't
477 * know which socket needs to be read, so we have to check each one
478 * Then reschedule this function to be called again once more is available.
480 * @param cls the plugin handle
481 * @param tc the scheduling context (for rescheduling this function again)
484 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
488 * We have been notified that our readset has something to read. We don't
489 * know which socket needs to be read, so we have to check each one
490 * Then reschedule this function to be called again once more is available.
492 * @param cls the plugin handle
493 * @param tc the scheduling context (for rescheduling this function again)
496 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
500 * (re)schedule select tasks for this plugin.
502 * @param plugin plugin to reschedule
505 schedule_select (struct Plugin *plugin)
507 struct GNUNET_TIME_Relative min_delay;
508 struct UDP_MessageWrapper *udpw;
510 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
512 /* Find a message ready to send:
513 * Flow delay from other peer is expired or not set (0) */
514 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
515 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
516 min_delay = GNUNET_TIME_relative_min (min_delay,
517 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
519 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
520 GNUNET_SCHEDULER_cancel(plugin->select_task);
523 * - write active set if message is ready
524 * - timeout minimum delay */
525 plugin->select_task =
526 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
527 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
529 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
530 &udp_plugin_select, plugin);
532 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
534 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
535 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
536 min_delay = GNUNET_TIME_relative_min (min_delay,
537 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
539 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
540 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
541 plugin->select_task_v6 =
542 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
543 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
545 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
546 &udp_plugin_select_v6, plugin);
552 * Function called for a quick conversion of the binary address to
553 * a numeric address. Note that the caller must not free the
554 * address and that the next call to this function is allowed
555 * to override the address again.
558 * @param addr binary address
559 * @param addrlen length of the address
560 * @return string representing the same address
563 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
565 static char rbuf[INET6_ADDRSTRLEN + 10];
566 char buf[INET6_ADDRSTRLEN];
570 const struct IPv4UdpAddress *t4;
571 const struct IPv6UdpAddress *t6;
576 if ((NULL != addr) && (addrlen == sizeof (struct IPv6UdpAddress)))
580 options = ntohl (t6->options);
581 port = ntohs (t6->u6_port);
582 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
585 else if ((NULL != addr) && (addrlen == sizeof (struct IPv4UdpAddress)))
589 options = ntohl (t4->options);
590 port = ntohs (t4->u4_port);
591 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
598 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
600 GNUNET_snprintf (rbuf, sizeof (rbuf),
601 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
602 PLUGIN_NAME, options, buf, port);
608 * Function called to convert a string address to
611 * @param cls closure ('struct Plugin*')
612 * @param addr string address
613 * @param addrlen length of the address
614 * @param buf location to store the buffer
615 * @param added location to store the number of bytes in the buffer.
616 * If the function returns GNUNET_SYSERR, its contents are undefined.
617 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
620 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
621 void **buf, size_t *added)
623 struct sockaddr_storage socket_address;
629 /* Format tcp.options.address:port */
634 if ((NULL == addr) || (addrlen == 0))
637 return GNUNET_SYSERR;
639 if ('\0' != addr[addrlen - 1])
642 return GNUNET_SYSERR;
644 if (strlen (addr) != addrlen - 1)
647 return GNUNET_SYSERR;
649 plugin = GNUNET_strdup (addr);
650 optionstr = strchr (plugin, '.');
651 if (NULL == optionstr)
654 GNUNET_free (plugin);
655 return GNUNET_SYSERR;
659 options = atol (optionstr);
660 address = strchr (optionstr, '.');
664 GNUNET_free (plugin);
665 return GNUNET_SYSERR;
671 GNUNET_STRINGS_to_address_ip (address, strlen (address),
675 GNUNET_free (plugin);
676 return GNUNET_SYSERR;
679 GNUNET_free (plugin);
681 switch (socket_address.ss_family)
685 struct IPv4UdpAddress *u4;
686 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
687 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
688 u4->options = htonl (options);
689 u4->ipv4_addr = in4->sin_addr.s_addr;
690 u4->u4_port = in4->sin_port;
692 *added = sizeof (struct IPv4UdpAddress);
697 struct IPv6UdpAddress *u6;
698 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
699 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
700 u6->options = htonl (options);
701 u6->ipv6_addr = in6->sin6_addr;
702 u6->u6_port = in6->sin6_port;
704 *added = sizeof (struct IPv6UdpAddress);
709 return GNUNET_SYSERR;
715 ppc_cancel_task (void *cls,
716 const struct GNUNET_SCHEDULER_TaskContext *tc)
718 struct PrettyPrinterContext *ppc = cls;
720 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
721 if (NULL != ppc->resolver_handle)
723 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
724 ppc->resolver_handle = NULL;
726 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
732 * Append our port and forward the result.
734 * @param cls a 'struct PrettyPrinterContext'
735 * @param hostname result from DNS resolver
738 append_port (void *cls, const char *hostname)
740 struct PrettyPrinterContext *ppc = cls;
741 struct PrettyPrinterContext *cur;
744 if (hostname == NULL)
746 ppc->asc (ppc->asc_cls, NULL);
747 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
748 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
749 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
750 ppc->resolver_handle = NULL;
754 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
762 "Invalid callback for PPC %p \n", ppc);
766 if (GNUNET_YES == ppc->ipv6)
767 GNUNET_asprintf (&ret,
774 GNUNET_asprintf (&ret,
780 ppc->asc (ppc->asc_cls, ret);
786 * Convert the transports address to a nice, human-readable
790 * @param type name of the transport that generated the address
791 * @param addr one of the addresses of the host, NULL for the last address
792 * the specific address format depends on the transport
793 * @param addrlen length of the address
794 * @param numeric should (IP) addresses be displayed in numeric form?
795 * @param timeout after how long should we give up?
796 * @param asc function to call on each string
797 * @param asc_cls closure for @a asc
800 udp_plugin_address_pretty_printer (void *cls, const char *type,
801 const void *addr, size_t addrlen,
803 struct GNUNET_TIME_Relative timeout,
804 GNUNET_TRANSPORT_AddressStringCallback asc,
807 struct PrettyPrinterContext *ppc;
810 struct sockaddr_in a4;
811 struct sockaddr_in6 a6;
812 const struct IPv4UdpAddress *u4;
813 const struct IPv6UdpAddress *u6;
817 if (addrlen == sizeof (struct IPv6UdpAddress))
820 memset (&a6, 0, sizeof (a6));
821 a6.sin6_family = AF_INET6;
822 #if HAVE_SOCKADDR_IN_SIN_LEN
823 a6.sin6_len = sizeof (a6);
825 a6.sin6_port = u6->u6_port;
826 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
827 port = ntohs (u6->u6_port);
828 options = ntohl (u6->options);
832 else if (addrlen == sizeof (struct IPv4UdpAddress))
835 memset (&a4, 0, sizeof (a4));
836 a4.sin_family = AF_INET;
837 #if HAVE_SOCKADDR_IN_SIN_LEN
838 a4.sin_len = sizeof (a4);
840 a4.sin_port = u4->u4_port;
841 a4.sin_addr.s_addr = u4->ipv4_addr;
842 port = ntohs (u4->u4_port);
843 options = ntohl (u4->options);
849 /* invalid address */
854 ppc = GNUNET_new (struct PrettyPrinterContext);
856 ppc->asc_cls = asc_cls;
858 ppc->options = options;
859 if (addrlen == sizeof (struct IPv6UdpAddress))
860 ppc->ipv6 = GNUNET_YES;
862 ppc->ipv6 = GNUNET_NO;
863 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
864 &ppc_cancel_task, ppc);
865 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
866 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs,
874 call_continuation (struct UDP_MessageWrapper *udpw, int result)
878 LOG (GNUNET_ERROR_TYPE_DEBUG,
879 "Calling continuation for %u byte message to `%s' with result %s\n",
880 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
881 (GNUNET_OK == result) ? "OK" : "SYSERR");
883 if (udpw->msg_size >= udpw->payload_size)
884 overhead = udpw->msg_size - udpw->payload_size;
886 overhead = udpw->msg_size;
890 switch (udpw->msg_type) {
891 case MSG_UNFRAGMENTED:
892 if (NULL != udpw->cont)
894 /* Transport continuation */
895 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
896 udpw->payload_size, udpw->msg_size);
898 GNUNET_STATISTICS_update (plugin->env->stats,
899 "# UDP, unfragmented msgs, messages, sent, success",
901 GNUNET_STATISTICS_update (plugin->env->stats,
902 "# UDP, unfragmented msgs, bytes payload, sent, success",
903 udpw->payload_size, GNUNET_NO);
904 GNUNET_STATISTICS_update (plugin->env->stats,
905 "# UDP, unfragmented msgs, bytes overhead, sent, success",
906 overhead, GNUNET_NO);
907 GNUNET_STATISTICS_update (plugin->env->stats,
908 "# UDP, total, bytes overhead, sent",
909 overhead, GNUNET_NO);
910 GNUNET_STATISTICS_update (plugin->env->stats,
911 "# UDP, total, bytes payload, sent",
912 udpw->payload_size, GNUNET_NO);
914 case MSG_FRAGMENTED_COMPLETE:
915 GNUNET_assert (NULL != udpw->frag_ctx);
916 if (udpw->frag_ctx->cont != NULL)
917 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
918 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
919 GNUNET_STATISTICS_update (plugin->env->stats,
920 "# UDP, fragmented msgs, messages, sent, success",
922 GNUNET_STATISTICS_update (plugin->env->stats,
923 "# UDP, fragmented msgs, bytes payload, sent, success",
924 udpw->payload_size, GNUNET_NO);
925 GNUNET_STATISTICS_update (plugin->env->stats,
926 "# UDP, fragmented msgs, bytes overhead, sent, success",
927 overhead, GNUNET_NO);
928 GNUNET_STATISTICS_update (plugin->env->stats,
929 "# UDP, total, bytes overhead, sent",
930 overhead, GNUNET_NO);
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 "# UDP, total, bytes payload, sent",
933 udpw->payload_size, GNUNET_NO);
934 GNUNET_STATISTICS_update (plugin->env->stats,
935 "# UDP, fragmented msgs, messages, pending",
939 /* Fragmented message: enqueue next fragment */
940 if (NULL != udpw->cont)
941 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
942 udpw->payload_size, udpw->msg_size);
943 GNUNET_STATISTICS_update (plugin->env->stats,
944 "# UDP, fragmented msgs, fragments, sent, success",
946 GNUNET_STATISTICS_update (plugin->env->stats,
947 "# UDP, fragmented msgs, fragments bytes, sent, success",
948 udpw->msg_size, GNUNET_NO);
951 /* No continuation */
952 GNUNET_STATISTICS_update (plugin->env->stats,
953 "# UDP, ACK msgs, messages, sent, success",
955 GNUNET_STATISTICS_update (plugin->env->stats,
956 "# UDP, ACK msgs, bytes overhead, sent, success",
957 overhead, GNUNET_NO);
958 GNUNET_STATISTICS_update (plugin->env->stats,
959 "# UDP, total, bytes overhead, sent",
960 overhead, GNUNET_NO);
966 LOG (GNUNET_ERROR_TYPE_ERROR,
967 "ERROR: %u\n", udpw->msg_type);
973 switch (udpw->msg_type) {
974 case MSG_UNFRAGMENTED:
975 /* Unfragmented message: failed to send */
976 if (NULL != udpw->cont)
977 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
978 udpw->payload_size, overhead);
979 GNUNET_STATISTICS_update (plugin->env->stats,
980 "# UDP, unfragmented msgs, messages, sent, failure",
982 GNUNET_STATISTICS_update (plugin->env->stats,
983 "# UDP, unfragmented msgs, bytes payload, sent, failure",
984 udpw->payload_size, GNUNET_NO);
985 GNUNET_STATISTICS_update (plugin->env->stats,
986 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
987 overhead, GNUNET_NO);
989 case MSG_FRAGMENTED_COMPLETE:
990 GNUNET_assert (NULL != udpw->frag_ctx);
991 if (udpw->frag_ctx->cont != NULL)
992 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
993 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
994 GNUNET_STATISTICS_update (plugin->env->stats,
995 "# UDP, fragmented msgs, messages, sent, failure",
997 GNUNET_STATISTICS_update (plugin->env->stats,
998 "# UDP, fragmented msgs, bytes payload, sent, failure",
999 udpw->payload_size, GNUNET_NO);
1000 GNUNET_STATISTICS_update (plugin->env->stats,
1001 "# UDP, fragmented msgs, bytes payload, sent, failure",
1002 overhead, GNUNET_NO);
1003 GNUNET_STATISTICS_update (plugin->env->stats,
1004 "# UDP, fragmented msgs, bytes payload, sent, failure",
1005 overhead, GNUNET_NO);
1006 GNUNET_STATISTICS_update (plugin->env->stats,
1007 "# UDP, fragmented msgs, messages, pending",
1010 case MSG_FRAGMENTED:
1011 GNUNET_assert (NULL != udpw->frag_ctx);
1012 /* Fragmented message: failed to send */
1013 GNUNET_STATISTICS_update (plugin->env->stats,
1014 "# UDP, fragmented msgs, fragments, sent, failure",
1016 GNUNET_STATISTICS_update (plugin->env->stats,
1017 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1018 udpw->msg_size, GNUNET_NO);
1021 /* ACK message: failed to send */
1022 GNUNET_STATISTICS_update (plugin->env->stats,
1023 "# UDP, ACK msgs, messages, sent, failure",
1027 /* Beacon message: failed to send */
1043 * Check if the given port is plausible (must be either our listen
1044 * port or our advertised port). If it is neither, we return
1047 * @param plugin global variables
1048 * @param in_port port number to check
1049 * @return #GNUNET_OK if port is either open_port or adv_port
1052 check_port (struct Plugin *plugin, uint16_t in_port)
1054 if ((in_port == plugin->port) || (in_port == plugin->aport))
1056 return GNUNET_SYSERR;
1061 * Function that will be called to check if a binary address for this
1062 * plugin is well-formed and corresponds to an address for THIS peer
1063 * (as per our configuration). Naturally, if absolutely necessary,
1064 * plugins can be a bit conservative in their answer, but in general
1065 * plugins should make sure that the address does not redirect
1066 * traffic to a 3rd party that might try to man-in-the-middle our
1069 * @param cls closure, should be our handle to the Plugin
1070 * @param addr pointer to the address
1071 * @param addrlen length of @a addr
1072 * @return #GNUNET_OK if this is a plausible address for this peer
1073 * and transport, #GNUNET_SYSERR if not
1077 udp_plugin_check_address (void *cls,
1081 struct Plugin *plugin = cls;
1082 struct IPv4UdpAddress *v4;
1083 struct IPv6UdpAddress *v6;
1085 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1086 (addrlen != sizeof (struct IPv6UdpAddress)))
1088 GNUNET_break_op (0);
1089 return GNUNET_SYSERR;
1091 if (addrlen == sizeof (struct IPv4UdpAddress))
1093 v4 = (struct IPv4UdpAddress *) addr;
1094 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1095 return GNUNET_SYSERR;
1097 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1098 sizeof (struct in_addr)))
1099 return GNUNET_SYSERR;
1103 v6 = (struct IPv6UdpAddress *) addr;
1104 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1106 GNUNET_break_op (0);
1107 return GNUNET_SYSERR;
1109 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1110 return GNUNET_SYSERR;
1112 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1113 sizeof (struct in6_addr)))
1114 return GNUNET_SYSERR;
1121 * Function to free last resources associated with a session.
1123 * @param s session to free
1126 free_session (struct Session *s)
1128 if (NULL != s->frag_ctx)
1130 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL);
1131 GNUNET_free (s->frag_ctx);
1139 dequeue (struct Plugin *plugin,
1140 struct UDP_MessageWrapper * udpw)
1142 if (plugin->bytes_in_buffer < udpw->msg_size)
1146 GNUNET_STATISTICS_update (plugin->env->stats,
1147 "# UDP, total, bytes in buffers",
1148 - (long long) udpw->msg_size, GNUNET_NO);
1149 plugin->bytes_in_buffer -= udpw->msg_size;
1151 GNUNET_STATISTICS_update (plugin->env->stats,
1152 "# UDP, total, msgs in buffers",
1154 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1155 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1156 plugin->ipv4_queue_tail, udpw);
1157 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1158 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1159 plugin->ipv6_queue_tail, udpw);
1164 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1166 struct UDP_MessageWrapper *udpw;
1167 struct UDP_MessageWrapper *tmp;
1168 struct UDP_MessageWrapper dummy;
1169 struct Session *s = fc->session;
1171 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 "%p : Fragmented message removed with result %s\n",
1174 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1176 /* Call continuation for fragmented message */
1177 memset (&dummy, 0, sizeof (dummy));
1178 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1179 dummy.msg_size = s->frag_ctx->on_wire_size;
1180 dummy.payload_size = s->frag_ctx->payload_size;
1181 dummy.frag_ctx = s->frag_ctx;
1183 dummy.cont_cls = NULL;
1186 call_continuation (&dummy, result);
1188 /* Remove leftover fragments from queue */
1189 if (s->addrlen == sizeof (struct sockaddr_in6))
1191 udpw = plugin->ipv6_queue_head;
1192 while (NULL != udpw)
1195 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1197 dequeue (plugin, udpw);
1198 call_continuation (udpw, GNUNET_SYSERR);
1204 if (s->addrlen == sizeof (struct sockaddr_in))
1206 udpw = plugin->ipv4_queue_head;
1210 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1212 dequeue (plugin, udpw);
1213 call_continuation (udpw, GNUNET_SYSERR);
1220 /* Destroy fragmentation context */
1221 GNUNET_FRAGMENT_context_destroy (fc->frag,
1222 &s->last_expected_msg_delay,
1223 &s->last_expected_ack_delay);
1230 * Functions with this signature are called whenever we need
1231 * to close a session due to a disconnect or failure to
1232 * establish a connection.
1234 * @param cls closure with the `struct Plugin`
1235 * @param s session to close down
1236 * @return #GNUNET_OK on success
1239 udp_disconnect_session (void *cls,
1242 struct Plugin *plugin = cls;
1243 struct UDP_MessageWrapper *udpw;
1244 struct UDP_MessageWrapper *next;
1246 GNUNET_assert (GNUNET_YES != s->in_destroy);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "Session %p to peer `%s' address ended\n",
1250 GNUNET_i2s (&s->target),
1251 GNUNET_a2s (s->sock_addr, s->addrlen));
1252 /* stop timeout task */
1253 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1255 GNUNET_SCHEDULER_cancel (s->timeout_task);
1256 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1258 if (NULL != s->frag_ctx)
1260 /* Remove fragmented message due to disconnect */
1261 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1264 next = plugin->ipv4_queue_head;
1265 while (NULL != (udpw = next))
1268 if (udpw->session == s)
1270 dequeue (plugin, udpw);
1271 call_continuation (udpw, GNUNET_SYSERR);
1275 next = plugin->ipv6_queue_head;
1276 while (NULL != (udpw = next))
1279 if (udpw->session == s)
1281 dequeue (plugin, udpw);
1282 call_continuation (udpw, GNUNET_SYSERR);
1286 plugin->env->session_end (plugin->env->cls, &s->target, s);
1288 if (NULL != s->frag_ctx)
1290 if (NULL != s->frag_ctx->cont)
1292 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1293 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1294 LOG (GNUNET_ERROR_TYPE_DEBUG,
1295 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1296 GNUNET_i2s (&s->target));
1300 GNUNET_assert (GNUNET_YES ==
1301 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1304 GNUNET_STATISTICS_set (plugin->env->stats,
1305 "# UDP, sessions active",
1306 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1309 s->in_destroy = GNUNET_YES;
1317 * Function that is called to get the keepalive factor.
1318 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1319 * calculate the interval between keepalive packets.
1321 * @param cls closure with the `struct Plugin`
1322 * @return keepalive factor
1325 udp_query_keepalive_factor (void *cls)
1332 * Destroy a session, plugin is being unloaded.
1334 * @param cls the `struct Plugin`
1335 * @param key hash of public key of target peer
1336 * @param value a `struct PeerSession *` to clean up
1337 * @return #GNUNET_OK (continue to iterate)
1340 disconnect_and_free_it (void *cls,
1341 const struct GNUNET_PeerIdentity *key,
1344 struct Plugin *plugin = cls;
1346 udp_disconnect_session (plugin, value);
1352 * Disconnect from a remote node. Clean up session if we have one for
1355 * @param cls closure for this call (should be handle to Plugin)
1356 * @param target the peeridentity of the peer to disconnect
1357 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1360 udp_disconnect (void *cls,
1361 const struct GNUNET_PeerIdentity *target)
1363 struct Plugin *plugin = cls;
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Disconnecting from peer `%s'\n",
1367 GNUNET_i2s (target));
1368 /* Clean up sessions */
1369 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, target,
1370 &disconnect_and_free_it, plugin);
1375 * Session was idle, so disconnect it
1377 * @param cls the `struct Session` to time out
1378 * @param tc scheduler context
1381 session_timeout (void *cls,
1382 const struct GNUNET_SCHEDULER_TaskContext *tc)
1384 struct Session *s = cls;
1386 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Session %p was idle for %s, disconnecting\n",
1390 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT,
1392 /* call session destroy function */
1393 udp_disconnect_session (s->plugin,
1399 * Increment session timeout due to activity
1401 * @param s session to reschedule timeout activity for
1404 reschedule_session_timeout (struct Session *s)
1406 if (GNUNET_YES == s->in_destroy)
1408 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1409 GNUNET_SCHEDULER_cancel (s->timeout_task);
1410 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Timeout restarted for session %p\n",
1419 static struct Session *
1420 create_session (struct Plugin *plugin,
1421 const struct GNUNET_PeerIdentity *target,
1422 const void *addr, size_t addrlen,
1423 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1426 const struct IPv4UdpAddress *t4;
1427 const struct IPv6UdpAddress *t6;
1428 struct sockaddr_in *v4;
1429 struct sockaddr_in6 *v6;
1440 case sizeof (struct IPv4UdpAddress):
1441 if (NULL == plugin->sockv4)
1443 LOG (GNUNET_ERROR_TYPE_DEBUG,
1444 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1446 udp_address_to_string (NULL, addr, addrlen));
1450 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1452 len = sizeof (struct sockaddr_in);
1453 v4 = (struct sockaddr_in *) &s[1];
1454 v4->sin_family = AF_INET;
1455 #if HAVE_SOCKADDR_IN_SIN_LEN
1456 v4->sin_len = sizeof (struct sockaddr_in);
1458 v4->sin_port = t4->u4_port;
1459 v4->sin_addr.s_addr = t4->ipv4_addr;
1460 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1462 case sizeof (struct IPv6UdpAddress):
1463 if (NULL == plugin->sockv6)
1465 LOG (GNUNET_ERROR_TYPE_INFO,
1466 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1468 udp_address_to_string(NULL, addr, addrlen));
1472 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1474 len = sizeof (struct sockaddr_in6);
1475 v6 = (struct sockaddr_in6 *) &s[1];
1476 v6->sin6_family = AF_INET6;
1477 #if HAVE_SOCKADDR_IN_SIN_LEN
1478 v6->sin6_len = sizeof (struct sockaddr_in6);
1480 v6->sin6_port = t6->u6_port;
1481 v6->sin6_addr = t6->ipv6_addr;
1482 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1485 /* Must have a valid address to send to */
1486 GNUNET_STATISTICS_update (plugin->env->stats,
1488 ("# requests to create session with invalid address"),
1493 s->target = *target;
1494 s->sock_addr = (const struct sockaddr *) &s[1];
1495 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1496 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1497 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1498 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1499 s->inbound = GNUNET_NO;
1500 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1508 session_cmp_it (void *cls,
1509 const struct GNUNET_PeerIdentity * key,
1512 struct SessionCompareContext * cctx = cls;
1513 const struct GNUNET_HELLO_Address *address = cctx->addr;
1514 struct Session *s = value;
1515 socklen_t s_addrlen = s->addrlen;
1517 LOG (GNUNET_ERROR_TYPE_DEBUG,
1518 "Comparing address %s <-> %s\n",
1519 udp_address_to_string (NULL, (void *) address->address,
1520 address->address_length),
1521 GNUNET_a2s (s->sock_addr, s->addrlen));
1522 if (s->inbound != cctx->inbound)
1524 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1525 (s_addrlen == sizeof (struct sockaddr_in)))
1527 struct IPv4UdpAddress * u4 = NULL;
1528 u4 = (struct IPv4UdpAddress *) address->address;
1529 GNUNET_assert (NULL != u4);
1530 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1531 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1532 (u4->u4_port == s4->sin_port))
1539 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1540 (s_addrlen == sizeof (struct sockaddr_in6)))
1542 struct IPv6UdpAddress * u6 = NULL;
1543 u6 = (struct IPv6UdpAddress *) address->address;
1544 GNUNET_assert (NULL != u6);
1545 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1546 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1547 (u6->u6_port == s6->sin6_port))
1558 * Function obtain the network type for a session
1560 * @param cls closure ('struct Plugin*')
1561 * @param session the session
1562 * @return the network type
1564 static enum GNUNET_ATS_Network_Type
1565 udp_get_network (void *cls,
1566 struct Session *session)
1568 return ntohl (session->ats.value);
1573 * Creates a new outbound session the transport service will use to
1574 * send data to the peer
1576 * @param cls the plugin
1577 * @param address the address
1578 * @param inbound look for inbound session
1579 * @return the session or NULL of max connections exceeded
1581 static struct Session *
1582 udp_plugin_lookup_session (void *cls,
1583 const struct GNUNET_HELLO_Address *address,
1586 struct Plugin * plugin = cls;
1587 struct IPv6UdpAddress * udp_a6;
1588 struct IPv4UdpAddress * udp_a4;
1590 GNUNET_assert (plugin != NULL);
1591 GNUNET_assert (address != NULL);
1594 if ((address->address == NULL) ||
1595 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1596 (address->address_length != sizeof (struct IPv6UdpAddress))))
1598 LOG (GNUNET_ERROR_TYPE_WARNING,
1599 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1600 address->address_length,
1601 sizeof (struct IPv4UdpAddress),
1602 sizeof (struct IPv6UdpAddress));
1606 if (address->address_length == sizeof (struct IPv4UdpAddress))
1608 if (plugin->sockv4 == NULL)
1610 udp_a4 = (struct IPv4UdpAddress *) address->address;
1611 if (udp_a4->u4_port == 0)
1615 if (address->address_length == sizeof (struct IPv6UdpAddress))
1617 if (plugin->sockv6 == NULL)
1619 udp_a6 = (struct IPv6UdpAddress *) address->address;
1620 if (udp_a6->u6_port == 0)
1624 /* check if session already exists */
1625 struct SessionCompareContext cctx;
1626 cctx.addr = address;
1628 cctx.inbound = inbound;
1629 LOG (GNUNET_ERROR_TYPE_DEBUG,
1630 "Looking for existing session for peer `%s' `%s' \n",
1631 GNUNET_i2s (&address->peer),
1632 udp_address_to_string(NULL, address->address, address->address_length));
1633 GNUNET_CONTAINER_multipeermap_get_multiple(plugin->sessions, &address->peer, session_cmp_it, &cctx);
1634 if (cctx.res != NULL)
1636 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1643 static struct Session *
1644 udp_plugin_create_session (void *cls,
1645 const struct GNUNET_HELLO_Address *address,
1650 s = create_session (plugin,
1653 address->address_length,
1656 return NULL; /* protocol not supported or address invalid */
1657 s->inbound = inbound;
1658 LOG (GNUNET_ERROR_TYPE_DEBUG,
1659 "Creating new %s session %p for peer `%s' address `%s'\n",
1660 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
1662 GNUNET_i2s(&address->peer),
1663 udp_address_to_string(NULL,address->address,address->address_length));
1664 GNUNET_assert (GNUNET_OK ==
1665 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1669 GNUNET_STATISTICS_set(plugin->env->stats,
1670 "# UDP, sessions active",
1671 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1677 udp_plugin_update_session_timeout (void *cls,
1678 const struct GNUNET_PeerIdentity *peer,
1679 struct Session *session)
1685 * Creates a new outbound session the transport service will use to send data to the
1688 * @param cls the plugin
1689 * @param address the address
1690 * @return the session or NULL of max connections exceeded
1692 static struct Session *
1693 udp_plugin_get_session (void *cls,
1694 const struct GNUNET_HELLO_Address *address)
1698 if (NULL == address)
1703 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1704 (address->address_length != sizeof (struct IPv6UdpAddress)))
1707 /* otherwise create new */
1708 if (NULL != (s = udp_plugin_lookup_session (cls, address, GNUNET_NO)))
1710 return udp_plugin_create_session (cls, address, GNUNET_NO);
1715 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1717 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1721 GNUNET_STATISTICS_update (plugin->env->stats,
1722 "# UDP, total, bytes in buffers",
1723 udpw->msg_size, GNUNET_NO);
1724 plugin->bytes_in_buffer += udpw->msg_size;
1726 GNUNET_STATISTICS_update (plugin->env->stats,
1727 "# UDP, total, msgs in buffers",
1729 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1730 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1731 plugin->ipv4_queue_tail, udpw);
1732 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1733 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1734 plugin->ipv6_queue_tail, udpw);
1740 * Fragment message was transmitted via UDP, let fragmentation know
1741 * to send the next fragment now.
1743 * @param cls the 'struct UDPMessageWrapper' of the fragment
1744 * @param target destination peer (ignored)
1745 * @param result GNUNET_OK on success (ignored)
1746 * @param payload bytes payload sent
1747 * @param physical bytes physical sent
1750 send_next_fragment (void *cls,
1751 const struct GNUNET_PeerIdentity *target,
1752 int result, size_t payload, size_t physical)
1754 struct UDP_MessageWrapper *udpw = cls;
1756 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1761 * Function that is called with messages created by the fragmentation
1762 * module. In the case of the 'proc' callback of the
1763 * GNUNET_FRAGMENT_context_create function, this function must
1764 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1766 * @param cls closure, the 'struct FragmentationContext'
1767 * @param msg the message that was created
1770 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1772 struct UDP_FragmentationContext *frag_ctx = cls;
1773 struct Plugin *plugin = frag_ctx->plugin;
1774 struct UDP_MessageWrapper * udpw;
1775 size_t msg_len = ntohs (msg->size);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG,
1778 "Enqueuing fragment with %u bytes\n", msg_len);
1779 frag_ctx->fragments_used ++;
1780 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1781 udpw->session = frag_ctx->session;
1782 udpw->msg_buf = (char *) &udpw[1];
1783 udpw->msg_size = msg_len;
1784 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1785 udpw->cont = &send_next_fragment;
1786 udpw->cont_cls = udpw;
1787 udpw->timeout = frag_ctx->timeout;
1788 udpw->frag_ctx = frag_ctx;
1789 udpw->msg_type = MSG_FRAGMENTED;
1790 memcpy (udpw->msg_buf, msg, msg_len);
1791 enqueue (plugin, udpw);
1792 schedule_select (plugin);
1797 * Function that can be used by the transport service to transmit
1798 * a message using the plugin. Note that in the case of a
1799 * peer disconnecting, the continuation MUST be called
1800 * prior to the disconnect notification itself. This function
1801 * will be called with this peer's HELLO message to initiate
1802 * a fresh connection to another peer.
1804 * @param cls closure
1805 * @param s which session must be used
1806 * @param msgbuf the message to transmit
1807 * @param msgbuf_size number of bytes in 'msgbuf'
1808 * @param priority how important is the message (most plugins will
1809 * ignore message priority and just FIFO)
1810 * @param to how long to wait at most for the transmission (does not
1811 * require plugins to discard the message after the timeout,
1812 * just advisory for the desired delay; most plugins will ignore
1814 * @param cont continuation to call once the message has
1815 * been transmitted (or if the transport is ready
1816 * for the next transmission call; or if the
1817 * peer disconnected...); can be NULL
1818 * @param cont_cls closure for cont
1819 * @return number of bytes used (on the physical network, with overheads);
1820 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1821 * and does NOT mean that the message was not transmitted (DV)
1824 udp_plugin_send (void *cls,
1826 const char *msgbuf, size_t msgbuf_size,
1827 unsigned int priority,
1828 struct GNUNET_TIME_Relative to,
1829 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1831 struct Plugin *plugin = cls;
1832 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1833 struct UDP_FragmentationContext * frag_ctx;
1834 struct UDP_MessageWrapper * udpw;
1835 struct UDPMessage *udp;
1837 GNUNET_assert (plugin != NULL);
1838 GNUNET_assert (s != NULL);
1840 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1841 return GNUNET_SYSERR;
1842 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1843 return GNUNET_SYSERR;
1844 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1847 return GNUNET_SYSERR;
1849 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains_value(plugin->sessions, &s->target, s))
1852 return GNUNET_SYSERR;
1854 LOG (GNUNET_ERROR_TYPE_DEBUG,
1855 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1857 GNUNET_i2s (&s->target),
1858 GNUNET_a2s(s->sock_addr, s->addrlen));
1862 udp = (struct UDPMessage *) mbuf;
1863 udp->header.size = htons (udpmlen);
1864 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1865 udp->reserved = htonl (0);
1866 udp->sender = *plugin->env->my_identity;
1868 /* We do not update the session time out here!
1869 * Otherwise this session will not timeout since we send keep alive before
1870 * session can timeout
1872 * For UDP we update session timeout only on receive, this will cover keep
1873 * alives, since remote peer will reply with keep alive response!
1875 if (udpmlen <= UDP_MTU)
1877 /* unfragmented message */
1878 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1880 udpw->msg_buf = (char *) &udpw[1];
1881 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1882 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1883 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1885 udpw->cont_cls = cont_cls;
1886 udpw->frag_ctx = NULL;
1887 udpw->msg_type = MSG_UNFRAGMENTED;
1888 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1889 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1890 enqueue (plugin, udpw);
1892 GNUNET_STATISTICS_update (plugin->env->stats,
1893 "# UDP, unfragmented msgs, messages, attempt",
1895 GNUNET_STATISTICS_update (plugin->env->stats,
1896 "# UDP, unfragmented msgs, bytes payload, attempt",
1897 udpw->payload_size, GNUNET_NO);
1901 /* fragmented message */
1902 if (s->frag_ctx != NULL)
1903 return GNUNET_SYSERR;
1904 memcpy (&udp[1], msgbuf, msgbuf_size);
1905 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1906 frag_ctx->plugin = plugin;
1907 frag_ctx->session = s;
1908 frag_ctx->cont = cont;
1909 frag_ctx->cont_cls = cont_cls;
1910 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1911 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1912 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1913 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1916 s->last_expected_msg_delay,
1917 s->last_expected_ack_delay,
1921 s->frag_ctx = frag_ctx;
1922 GNUNET_STATISTICS_update (plugin->env->stats,
1923 "# UDP, fragmented msgs, messages, pending",
1925 GNUNET_STATISTICS_update (plugin->env->stats,
1926 "# UDP, fragmented msgs, messages, attempt",
1928 GNUNET_STATISTICS_update (plugin->env->stats,
1929 "# UDP, fragmented msgs, bytes payload, attempt",
1930 frag_ctx->payload_size, GNUNET_NO);
1932 schedule_select (plugin);
1938 * Our external IP address/port mapping has changed.
1940 * @param cls closure, the 'struct LocalAddrList'
1941 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1942 * the previous (now invalid) one
1943 * @param addr either the previous or the new public IP address
1944 * @param addrlen actual lenght of the address
1947 udp_nat_port_map_callback (void *cls, int add_remove,
1948 const struct sockaddr *addr, socklen_t addrlen)
1950 struct Plugin *plugin = cls;
1951 struct IPv4UdpAddress u4;
1952 struct IPv6UdpAddress u6;
1956 LOG (GNUNET_ERROR_TYPE_INFO,
1957 "NAT notification to %s address `%s'\n",
1958 (GNUNET_YES == add_remove) ? "add" : "remove",
1959 GNUNET_a2s (addr, addrlen));
1961 /* convert 'addr' to our internal format */
1962 switch (addr->sa_family)
1965 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1966 memset (&u4, 0, sizeof (u4));
1967 u4.options = htonl(myoptions);
1968 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1969 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1970 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1973 args = sizeof (struct IPv4UdpAddress);
1976 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1977 memset (&u4, 0, sizeof (u4));
1978 u6.options = htonl(myoptions);
1979 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1981 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1982 sizeof (struct in6_addr));
1983 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1985 args = sizeof (struct IPv6UdpAddress);
1991 /* modify our published address list */
1992 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1998 * Message tokenizer has broken up an incomming message. Pass it on
2001 * @param cls the 'struct Plugin'
2002 * @param client the `struct SourceInformation`
2003 * @param hdr the actual message
2004 * @return #GNUNET_OK (always)
2007 process_inbound_tokenized_messages (void *cls, void *client,
2008 const struct GNUNET_MessageHeader *hdr)
2010 struct Plugin *plugin = cls;
2011 struct SourceInformation *si = client;
2012 struct GNUNET_TIME_Relative delay;
2014 GNUNET_assert (si->session != NULL);
2015 if (GNUNET_YES == si->session->in_destroy)
2018 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
2019 reschedule_session_timeout (si->session);
2020 delay = plugin->env->receive (plugin->env->cls,
2024 (GNUNET_YES == si->session->inbound)
2026 (GNUNET_YES == si->session->inbound)
2028 plugin->env->update_address_metrics (plugin->env->cls,
2030 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
2031 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
2033 &si->session->ats, 1);
2034 si->session->flow_delay_for_other_peer = delay;
2040 * We've received a UDP Message. Process it (pass contents to main service).
2042 * @param plugin plugin context
2043 * @param msg the message
2044 * @param sender_addr sender address
2045 * @param sender_addr_len number of bytes in sender_addr
2048 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
2049 const struct sockaddr *sender_addr,
2050 socklen_t sender_addr_len)
2052 struct SourceInformation si;
2054 struct IPv4UdpAddress u4;
2055 struct IPv6UdpAddress u6;
2059 if (0 != ntohl (msg->reserved))
2061 GNUNET_break_op (0);
2064 if (ntohs (msg->header.size) <
2065 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2067 GNUNET_break_op (0);
2071 /* convert address */
2072 switch (sender_addr->sa_family)
2075 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2076 memset (&u4, 0, sizeof (u4));
2077 u6.options = htonl (0);
2078 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2079 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2084 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2085 memset (&u6, 0, sizeof (u6));
2086 u6.options = htonl (0);
2087 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2088 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2096 LOG (GNUNET_ERROR_TYPE_DEBUG,
2097 "Received message with %u bytes from peer `%s' at `%s'\n",
2098 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2099 GNUNET_a2s (sender_addr, sender_addr_len));
2101 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2102 if (NULL == (s = udp_plugin_lookup_session (plugin, address, GNUNET_YES)))
2104 s = udp_plugin_create_session (plugin, address, GNUNET_YES);
2105 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2106 address->address, address->address_length, s, NULL, 0);
2108 GNUNET_free (address);
2110 /* iterate over all embedded messages */
2112 si.sender = msg->sender;
2116 GNUNET_SERVER_mst_receive (plugin->mst,
2118 (const char *) &msg[1],
2119 ntohs (msg->header.size) - sizeof (struct UDPMessage),
2123 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2129 * Scan the heap for a receive context with the given address.
2131 * @param cls the `struct FindReceiveContext`
2132 * @param node internal node of the heap
2133 * @param element value stored at the node (a 'struct ReceiveContext')
2134 * @param cost cost associated with the node
2135 * @return #GNUNET_YES if we should continue to iterate,
2136 * #GNUNET_NO if not.
2139 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2140 void *element, GNUNET_CONTAINER_HeapCostType cost)
2142 struct FindReceiveContext *frc = cls;
2143 struct DefragContext *e = element;
2145 if ((frc->addr_len == e->addr_len) &&
2146 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2156 * Process a defragmented message.
2158 * @param cls the 'struct ReceiveContext'
2159 * @param msg the message
2162 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2164 struct DefragContext *rc = cls;
2166 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2171 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2176 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2177 rc->src_addr, rc->addr_len);
2181 struct LookupContext
2183 struct Session *res;
2185 const struct sockaddr * addr;
2189 int must_have_frag_ctx;
2194 lookup_session_by_addr_it (void *cls,
2195 const struct GNUNET_PeerIdentity *key,
2198 struct LookupContext *l_ctx = cls;
2199 struct Session * s = value;
2201 if ((GNUNET_YES == l_ctx->must_have_frag_ctx) && (NULL == s->frag_ctx))
2205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Comparing session: have %s %s %p<-> want %s\n",
2206 GNUNET_a2s(s->sock_addr, s->addrlen),
2207 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
2209 GNUNET_a2s(l_ctx->addr, l_ctx->addrlen));
2211 if ((s->addrlen == l_ctx->addrlen) &&
2212 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2222 * Transmit an acknowledgement.
2224 * @param cls the 'struct ReceiveContext'
2225 * @param id message ID (unused)
2226 * @param msg ack to transmit
2229 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2231 struct DefragContext *rc = cls;
2232 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2233 struct UDP_ACK_Message *udp_ack;
2235 struct UDP_MessageWrapper *udpw;
2237 struct LookupContext l_ctx;
2239 l_ctx.addr = rc->src_addr;
2240 l_ctx.addrlen = rc->addr_len;
2242 l_ctx.must_have_frag_ctx = GNUNET_NO;
2243 GNUNET_CONTAINER_multipeermap_iterate (rc->plugin->sessions,
2244 &lookup_session_by_addr_it,
2251 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2252 delay = s->flow_delay_for_other_peer.rel_value_us;
2254 LOG (GNUNET_ERROR_TYPE_DEBUG,
2255 "Sending ACK to `%s' including delay of %s\n",
2256 GNUNET_a2s (rc->src_addr,
2257 (rc->src_addr->sa_family ==
2258 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2260 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2262 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2263 udpw->msg_size = msize;
2264 udpw->payload_size = 0;
2266 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2267 udpw->msg_buf = (char *)&udpw[1];
2268 udpw->msg_type = MSG_ACK;
2269 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2270 udp_ack->header.size = htons ((uint16_t) msize);
2271 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2272 udp_ack->delay = htonl (delay);
2273 udp_ack->sender = *rc->plugin->env->my_identity;
2274 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2275 enqueue (rc->plugin, udpw);
2280 read_process_msg (struct Plugin *plugin,
2281 const struct GNUNET_MessageHeader *msg,
2282 const struct sockaddr *addr,
2285 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2287 GNUNET_break_op (0);
2290 process_udp_message (plugin, (const struct UDPMessage *) msg,
2296 read_process_ack (struct Plugin *plugin,
2297 const struct GNUNET_MessageHeader *msg,
2298 const struct sockaddr *addr,
2301 const struct GNUNET_MessageHeader *ack;
2302 const struct UDP_ACK_Message *udp_ack;
2303 struct LookupContext l_ctx;
2305 struct GNUNET_TIME_Relative flow_delay;
2307 if (ntohs (msg->size) <
2308 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2310 GNUNET_break_op (0);
2313 udp_ack = (const struct UDP_ACK_Message *) msg;
2314 l_ctx.addr = (const struct sockaddr *) addr;
2315 l_ctx.addrlen = fromlen;
2317 l_ctx.must_have_frag_ctx = GNUNET_YES;
2318 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2319 &lookup_session_by_addr_it,
2322 if ((NULL == s) || (NULL == s->frag_ctx))
2328 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2329 LOG (GNUNET_ERROR_TYPE_DEBUG,
2330 "We received a sending delay of %s\n",
2331 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2333 s->flow_delay_from_other_peer =
2334 GNUNET_TIME_relative_to_absolute (flow_delay);
2336 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2337 if (ntohs (ack->size) !=
2338 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2340 GNUNET_break_op (0);
2344 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2346 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2348 LOG (GNUNET_ERROR_TYPE_DEBUG,
2349 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2350 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2351 GNUNET_a2s (addr, fromlen));
2352 /* Expect more ACKs to arrive */
2356 LOG (GNUNET_ERROR_TYPE_DEBUG,
2357 "Message full ACK'ed\n",
2358 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2359 GNUNET_a2s (addr, fromlen));
2361 /* Remove fragmented message after successful sending */
2362 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2367 read_process_fragment (struct Plugin *plugin,
2368 const struct GNUNET_MessageHeader *msg,
2369 const struct sockaddr *addr,
2372 struct DefragContext *d_ctx;
2373 struct GNUNET_TIME_Absolute now;
2374 struct FindReceiveContext frc;
2378 frc.addr_len = fromlen;
2380 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2381 (unsigned int) ntohs (msg->size),
2382 GNUNET_a2s (addr, fromlen));
2383 /* Lookup existing receive context for this address */
2384 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2385 &find_receive_context,
2387 now = GNUNET_TIME_absolute_get ();
2392 /* Create a new defragmentation context */
2393 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2394 memcpy (&d_ctx[1], addr, fromlen);
2395 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2396 d_ctx->addr_len = fromlen;
2397 d_ctx->plugin = plugin;
2399 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2400 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2401 &fragment_msg_proc, &ack_proc);
2403 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2404 (GNUNET_CONTAINER_HeapCostType)
2406 LOG (GNUNET_ERROR_TYPE_DEBUG,
2407 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2408 (unsigned int) ntohs (msg->size),
2409 GNUNET_a2s (addr, fromlen));
2413 LOG (GNUNET_ERROR_TYPE_DEBUG,
2414 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2415 (unsigned int) ntohs (msg->size),
2416 GNUNET_a2s (addr, fromlen));
2419 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2421 /* keep this 'rc' from expiring */
2422 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2423 (GNUNET_CONTAINER_HeapCostType)
2426 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2427 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2429 /* remove 'rc' that was inactive the longest */
2430 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2431 GNUNET_assert (NULL != d_ctx);
2432 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2433 GNUNET_free (d_ctx);
2439 * Read and process a message from the given socket.
2441 * @param plugin the overall plugin
2442 * @param rsock socket to read from
2445 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2448 struct sockaddr_storage addr;
2449 char buf[65536] GNUNET_ALIGN;
2451 const struct GNUNET_MessageHeader *msg;
2453 fromlen = sizeof (addr);
2454 memset (&addr, 0, sizeof (addr));
2455 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2456 (struct sockaddr *) &addr, &fromlen);
2458 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2459 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2460 * on this socket has failed.
2462 * WSAECONNRESET - The virtual circuit was reset by the remote side
2463 * executing a hard or abortive close. The application should close
2464 * the socket; it is no longer usable. On a UDP-datagram socket this
2465 * error indicates a previous send operation resulted in an ICMP Port
2466 * Unreachable message.
2468 if ( (-1 == size) && (ECONNRESET == errno) )
2473 LOG (GNUNET_ERROR_TYPE_DEBUG,
2474 "UDP failed to receive data: %s\n", STRERROR (errno));
2475 /* Connection failure or something. Not a protocol violation. */
2478 if (size < sizeof (struct GNUNET_MessageHeader))
2480 LOG (GNUNET_ERROR_TYPE_WARNING,
2481 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2482 (unsigned int) size);
2483 /* _MAY_ be a connection failure (got partial message) */
2484 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2485 GNUNET_break_op (0);
2488 msg = (const struct GNUNET_MessageHeader *) buf;
2490 LOG (GNUNET_ERROR_TYPE_DEBUG,
2491 "UDP received %u-byte message from `%s' type %u\n",
2492 (unsigned int) size,
2493 GNUNET_a2s ((const struct sockaddr *) &addr, fromlen),
2496 if (size != ntohs (msg->size))
2498 GNUNET_break_op (0);
2502 GNUNET_STATISTICS_update (plugin->env->stats,
2503 "# UDP, total, bytes, received",
2506 switch (ntohs (msg->type))
2508 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2509 udp_broadcast_receive (plugin, buf, size,
2510 (const struct sockaddr *) &addr, fromlen);
2512 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2513 read_process_msg (plugin, msg,
2514 (const struct sockaddr *) &addr, fromlen);
2516 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2517 read_process_ack (plugin, msg,
2518 (const struct sockaddr *) &addr, fromlen);
2520 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2521 read_process_fragment (plugin, msg,
2522 (const struct sockaddr *) &addr, fromlen);
2525 GNUNET_break_op (0);
2531 static struct UDP_MessageWrapper *
2532 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2533 struct GNUNET_NETWORK_Handle *sock)
2535 struct UDP_MessageWrapper *udpw = NULL;
2536 struct GNUNET_TIME_Relative remaining;
2539 while (udpw != NULL)
2541 /* Find messages with timeout */
2542 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2543 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2545 /* Message timed out */
2546 switch (udpw->msg_type) {
2547 case MSG_UNFRAGMENTED:
2548 GNUNET_STATISTICS_update (plugin->env->stats,
2549 "# UDP, total, bytes, sent, timeout",
2550 udpw->msg_size, GNUNET_NO);
2551 GNUNET_STATISTICS_update (plugin->env->stats,
2552 "# UDP, total, messages, sent, timeout",
2554 GNUNET_STATISTICS_update (plugin->env->stats,
2555 "# UDP, unfragmented msgs, messages, sent, timeout",
2557 GNUNET_STATISTICS_update (plugin->env->stats,
2558 "# UDP, unfragmented msgs, bytes, sent, timeout",
2559 udpw->payload_size, GNUNET_NO);
2560 /* Not fragmented message */
2561 LOG (GNUNET_ERROR_TYPE_DEBUG,
2562 "Message for peer `%s' with size %u timed out\n",
2563 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2564 call_continuation (udpw, GNUNET_SYSERR);
2565 /* Remove message */
2566 dequeue (plugin, udpw);
2569 case MSG_FRAGMENTED:
2570 /* Fragmented message */
2571 GNUNET_STATISTICS_update (plugin->env->stats,
2572 "# UDP, total, bytes, sent, timeout",
2573 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2574 GNUNET_STATISTICS_update (plugin->env->stats,
2575 "# UDP, total, messages, sent, timeout",
2577 call_continuation (udpw, GNUNET_SYSERR);
2578 LOG (GNUNET_ERROR_TYPE_DEBUG,
2579 "Fragment for message for peer `%s' with size %u timed out\n",
2580 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2583 GNUNET_STATISTICS_update (plugin->env->stats,
2584 "# UDP, fragmented msgs, messages, sent, timeout",
2586 GNUNET_STATISTICS_update (plugin->env->stats,
2587 "# UDP, fragmented msgs, bytes, sent, timeout",
2588 udpw->frag_ctx->payload_size, GNUNET_NO);
2589 /* Remove fragmented message due to timeout */
2590 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2593 GNUNET_STATISTICS_update (plugin->env->stats,
2594 "# UDP, total, bytes, sent, timeout",
2595 udpw->msg_size, GNUNET_NO);
2596 GNUNET_STATISTICS_update (plugin->env->stats,
2597 "# UDP, total, messages, sent, timeout",
2599 LOG (GNUNET_ERROR_TYPE_DEBUG,
2600 "ACK Message for peer `%s' with size %u timed out\n",
2601 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2602 call_continuation (udpw, GNUNET_SYSERR);
2603 dequeue (plugin, udpw);
2609 if (sock == plugin->sockv4)
2610 udpw = plugin->ipv4_queue_head;
2611 else if (sock == plugin->sockv6)
2612 udpw = plugin->ipv6_queue_head;
2615 GNUNET_break (0); /* should never happen */
2618 GNUNET_STATISTICS_update (plugin->env->stats,
2619 "# messages dismissed due to timeout",
2624 /* Message did not time out, check flow delay */
2625 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2626 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2628 /* this message is not delayed */
2629 LOG (GNUNET_ERROR_TYPE_DEBUG,
2630 "Message for peer `%s' (%u bytes) is not delayed \n",
2631 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2632 break; /* Found message to send, break */
2636 /* Message is delayed, try next */
2637 LOG (GNUNET_ERROR_TYPE_DEBUG,
2638 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2639 GNUNET_i2s(&udpw->session->target), udpw->payload_size,
2640 GNUNET_STRINGS_relative_time_to_string (remaining,
2651 analyze_send_error (struct Plugin *plugin,
2652 const struct sockaddr * sa,
2656 static int network_down_error;
2657 struct GNUNET_ATS_Information type;
2659 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2660 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2661 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2663 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2665 /* IPv4: "Network unreachable" or "Network down"
2667 * This indicates we do not have connectivity
2669 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2670 _("UDP could not transmit message to `%s': "
2671 "Network seems down, please check your network configuration\n"),
2672 GNUNET_a2s (sa, slen));
2674 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2676 /* IPv6: "Network unreachable" or "Network down"
2678 * This indicates that this system is IPv6 enabled, but does not
2679 * have a valid global IPv6 address assigned or we do not have
2683 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2684 _("UDP could not transmit IPv6 message! "
2685 "Please check your network configuration and disable IPv6 if your "
2686 "connection does not have a global IPv6 address\n"));
2691 LOG (GNUNET_ERROR_TYPE_WARNING,
2692 "UDP could not transmit message to `%s': `%s'\n",
2693 GNUNET_a2s (sa, slen), STRERROR (error));
2699 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2701 const struct sockaddr * sa;
2705 struct UDP_MessageWrapper *udpw = NULL;
2707 /* Find message to send */
2708 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2711 return 0; /* No message to send */
2713 sa = udpw->session->sock_addr;
2714 slen = udpw->session->addrlen;
2716 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2718 if (GNUNET_SYSERR == sent)
2721 analyze_send_error (plugin, sa, slen, errno);
2722 call_continuation(udpw, GNUNET_SYSERR);
2723 GNUNET_STATISTICS_update (plugin->env->stats,
2724 "# UDP, total, bytes, sent, failure",
2726 GNUNET_STATISTICS_update (plugin->env->stats,
2727 "# UDP, total, messages, sent, failure",
2733 LOG (GNUNET_ERROR_TYPE_DEBUG,
2734 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2735 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2736 (sent < 0) ? STRERROR (errno) : "ok");
2737 GNUNET_STATISTICS_update (plugin->env->stats,
2738 "# UDP, total, bytes, sent, success",
2740 GNUNET_STATISTICS_update (plugin->env->stats,
2741 "# UDP, total, messages, sent, success",
2743 if (NULL != udpw->frag_ctx)
2744 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2745 call_continuation (udpw, GNUNET_OK);
2747 dequeue (plugin, udpw);
2756 * We have been notified that our readset has something to read. We don't
2757 * know which socket needs to be read, so we have to check each one
2758 * Then reschedule this function to be called again once more is available.
2760 * @param cls the plugin handle
2761 * @param tc the scheduling context (for rescheduling this function again)
2764 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2766 struct Plugin *plugin = cls;
2768 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2769 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2771 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2772 (NULL != plugin->sockv4) &&
2773 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2774 udp_select_read (plugin, plugin->sockv4);
2775 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2776 (NULL != plugin->sockv4) &&
2777 (NULL != plugin->ipv4_queue_head) &&
2778 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2779 udp_select_send (plugin, plugin->sockv4);
2780 schedule_select (plugin);
2785 * We have been notified that our readset has something to read. We don't
2786 * know which socket needs to be read, so we have to check each one
2787 * Then reschedule this function to be called again once more is available.
2789 * @param cls the plugin handle
2790 * @param tc the scheduling context (for rescheduling this function again)
2793 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2795 struct Plugin *plugin = cls;
2797 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2798 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2800 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2801 (NULL != plugin->sockv6) &&
2802 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2803 udp_select_read (plugin, plugin->sockv6);
2804 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2805 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2806 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2807 udp_select_send (plugin, plugin->sockv6);
2808 schedule_select (plugin);
2814 * @return number of sockets that were successfully bound
2817 setup_sockets (struct Plugin *plugin,
2818 const struct sockaddr_in6 *bind_v6,
2819 const struct sockaddr_in *bind_v4)
2822 int sockets_created = 0;
2823 struct sockaddr_in6 server_addrv6;
2824 struct sockaddr_in server_addrv4;
2825 struct sockaddr *server_addr;
2826 struct sockaddr *addrs[2];
2827 socklen_t addrlens[2];
2831 /* Create IPv6 socket */
2833 if (plugin->enable_ipv6 == GNUNET_YES)
2835 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2836 if (NULL == plugin->sockv6)
2838 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2839 plugin->enable_ipv6 = GNUNET_NO;
2843 memset (&server_addrv6, '\0', sizeof (struct sockaddr_in6));
2844 #if HAVE_SOCKADDR_IN_SIN_LEN
2845 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
2847 server_addrv6.sin6_family = AF_INET6;
2848 if (NULL != bind_v6)
2849 server_addrv6.sin6_addr = bind_v6->sin6_addr;
2851 server_addrv6.sin6_addr = in6addr_any;
2853 if (0 == plugin->port) /* autodetect */
2854 server_addrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2856 server_addrv6.sin6_port = htons (plugin->port);
2857 addrlen = sizeof (struct sockaddr_in6);
2858 server_addr = (struct sockaddr *) &server_addrv6;
2863 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2864 GNUNET_a2s (server_addr, addrlen));
2866 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2867 server_addr, addrlen))
2870 if (0 != plugin->port)
2872 tries = 10; /* fail */
2873 break; /* bind failed on specific port */
2876 server_addrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2881 GNUNET_NETWORK_socket_close (plugin->sockv6);
2882 plugin->enable_ipv6 = GNUNET_NO;
2883 plugin->sockv6 = NULL;
2886 if (plugin->sockv6 != NULL)
2888 LOG (GNUNET_ERROR_TYPE_DEBUG,
2889 "IPv6 socket created on port %s\n",
2890 GNUNET_a2s (server_addr, addrlen));
2891 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
2892 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2897 LOG (GNUNET_ERROR_TYPE_ERROR,
2898 "Failed to bind UDP socket to %s: %s\n",
2899 GNUNET_a2s (server_addr, addrlen),
2905 /* Create IPv4 socket */
2907 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2908 if (NULL == plugin->sockv4)
2910 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2911 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2912 plugin->enable_ipv4 = GNUNET_NO;
2916 memset (&server_addrv4, '\0', sizeof (struct sockaddr_in));
2917 #if HAVE_SOCKADDR_IN_SIN_LEN
2918 server_addrv4.sin_len = sizeof (struct sockaddr_in);
2920 server_addrv4.sin_family = AF_INET;
2921 if (NULL != bind_v4)
2922 server_addrv4.sin_addr = bind_v4->sin_addr;
2924 server_addrv4.sin_addr.s_addr = INADDR_ANY;
2926 if (0 == plugin->port)
2928 server_addrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2930 server_addrv4.sin_port = htons (plugin->port);
2933 addrlen = sizeof (struct sockaddr_in);
2934 server_addr = (struct sockaddr *) &server_addrv4;
2939 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2940 GNUNET_a2s (server_addr, addrlen));
2943 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2944 server_addr, addrlen))
2947 if (0 != plugin->port)
2949 tries = 10; /* fail */
2950 break; /* bind failed on specific port */
2954 server_addrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2960 GNUNET_NETWORK_socket_close (plugin->sockv4);
2961 plugin->enable_ipv4 = GNUNET_NO;
2962 plugin->sockv4 = NULL;
2965 if (plugin->sockv4 != NULL)
2967 LOG (GNUNET_ERROR_TYPE_DEBUG,
2968 "IPv4 socket created on port %s\n", GNUNET_a2s (server_addr, addrlen));
2969 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
2970 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2975 LOG (GNUNET_ERROR_TYPE_ERROR,
2976 "Failed to bind UDP socket to %s: %s\n",
2977 GNUNET_a2s (server_addr, addrlen), STRERROR (eno));
2981 if (0 == sockets_created)
2983 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2984 return 0; /* No sockets created, return */
2987 /* Create file descriptors */
2988 if (plugin->enable_ipv4 == GNUNET_YES)
2990 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2991 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2992 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2993 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2994 if (NULL != plugin->sockv4)
2996 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2997 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
3001 if (plugin->enable_ipv6 == GNUNET_YES)
3003 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
3004 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
3005 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
3006 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
3007 if (NULL != plugin->sockv6)
3009 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
3010 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
3014 schedule_select (plugin);
3015 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3016 GNUNET_NO, plugin->port,
3018 (const struct sockaddr **) addrs, addrlens,
3019 &udp_nat_port_map_callback, NULL, plugin);
3021 return sockets_created;
3026 * The exported method. Makes the core api available via a global and
3027 * returns the udp transport API.
3029 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3030 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
3033 libgnunet_plugin_transport_udp_init (void *cls)
3035 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3036 struct GNUNET_TRANSPORT_PluginFunctions *api;
3038 unsigned long long port;
3039 unsigned long long aport;
3040 unsigned long long udp_max_bps;
3041 unsigned long long enable_v6;
3042 unsigned long long enable_broadcasting;
3043 char * bind4_address;
3044 char * bind6_address;
3045 char * fancy_interval;
3046 struct GNUNET_TIME_Relative interval;
3047 struct sockaddr_in server_addrv4;
3048 struct sockaddr_in6 server_addrv6;
3053 if (NULL == env->receive)
3055 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3056 initialze the plugin or the API */
3057 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3059 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3060 api->address_to_string = &udp_address_to_string;
3061 api->string_to_address = &udp_string_to_address;
3065 GNUNET_assert (NULL != env->stats);
3067 /* Get port number: port == 0 : autodetect a port,
3068 * > 0 : use this port, not given : 2086 default */
3070 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
3074 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3075 "ADVERTISED_PORT", &aport))
3079 LOG (GNUNET_ERROR_TYPE_WARNING,
3080 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3087 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3089 enable_v6 = GNUNET_NO;
3091 enable_v6 = GNUNET_YES;
3094 have_bind4 = GNUNET_NO;
3095 memset (&server_addrv4, 0, sizeof (server_addrv4));
3097 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3098 "BINDTO", &bind4_address))
3100 LOG (GNUNET_ERROR_TYPE_DEBUG,
3101 "Binding udp plugin to specific address: `%s'\n",
3103 if (1 != inet_pton (AF_INET, bind4_address, &server_addrv4.sin_addr))
3105 GNUNET_free (bind4_address);
3108 have_bind4 = GNUNET_YES;
3110 GNUNET_free_non_null (bind4_address);
3111 have_bind6 = GNUNET_NO;
3112 memset (&server_addrv6, 0, sizeof (server_addrv6));
3114 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3115 "BINDTO6", &bind6_address))
3117 LOG (GNUNET_ERROR_TYPE_DEBUG,
3118 "Binding udp plugin to specific address: `%s'\n",
3121 inet_pton (AF_INET6, bind6_address, &server_addrv6.sin6_addr))
3123 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3125 GNUNET_free (bind6_address);
3128 have_bind6 = GNUNET_YES;
3130 GNUNET_free_non_null (bind6_address);
3132 /* Initialize my flags */
3135 /* Enable neighbour discovery */
3136 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3138 if (enable_broadcasting == GNUNET_SYSERR)
3139 enable_broadcasting = GNUNET_NO;
3141 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3142 "BROADCAST_INTERVAL", &fancy_interval))
3144 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3148 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3150 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3152 GNUNET_free (fancy_interval);
3155 /* Maximum datarate */
3156 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3157 "MAX_BPS", &udp_max_bps))
3159 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3162 p = GNUNET_new (struct Plugin);
3165 p->broadcast_interval = interval;
3166 p->enable_ipv6 = enable_v6;
3167 p->enable_ipv4 = GNUNET_YES; /* default */
3168 p->enable_broadcasting = enable_broadcasting;
3170 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3171 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3172 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3173 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3174 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps), 30);
3177 LOG(GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3178 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3179 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL );
3180 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3182 LOG (GNUNET_ERROR_TYPE_ERROR,
3183 _("Failed to create network sockets, plugin failed\n"));
3184 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3185 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3186 GNUNET_SERVER_mst_destroy (p->mst);
3191 /* Setup broadcasting and receiving beacons */
3192 setup_broadcast (p, &server_addrv6, &server_addrv4);
3194 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3197 api->disconnect_session = &udp_disconnect_session;
3198 api->query_keepalive_factor = &udp_query_keepalive_factor;
3199 api->disconnect_peer = &udp_disconnect;
3200 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3201 api->address_to_string = &udp_address_to_string;
3202 api->string_to_address = &udp_string_to_address;
3203 api->check_address = &udp_plugin_check_address;
3204 api->get_session = &udp_plugin_get_session;
3205 api->send = &udp_plugin_send;
3206 api->get_network = &udp_get_network;
3207 api->update_session_timeout = &udp_plugin_update_session_timeout;
3213 heap_cleanup_iterator (void *cls,
3214 struct GNUNET_CONTAINER_HeapNode *node,
3216 GNUNET_CONTAINER_HeapCostType cost)
3218 struct DefragContext * d_ctx = element;
3220 GNUNET_CONTAINER_heap_remove_node (node);
3221 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3222 GNUNET_free (d_ctx);
3229 * The exported method. Makes the core api available via a global and
3230 * returns the udp transport API.
3232 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3236 libgnunet_plugin_transport_udp_done (void *cls)
3238 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3239 struct Plugin *plugin = api->cls;
3240 struct PrettyPrinterContext *cur;
3241 struct PrettyPrinterContext *next;
3249 stop_broadcast (plugin);
3250 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3252 GNUNET_SCHEDULER_cancel (plugin->select_task);
3253 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3255 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3257 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3258 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3261 /* Closing sockets */
3262 if (GNUNET_YES == plugin->enable_ipv4)
3264 if (NULL != plugin->sockv4)
3266 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3267 plugin->sockv4 = NULL;
3269 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3270 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3272 if (GNUNET_YES == plugin->enable_ipv6)
3274 if (NULL != plugin->sockv6)
3276 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3277 plugin->sockv6 = NULL;
3279 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3280 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3283 if (NULL != plugin->nat)
3285 GNUNET_NAT_unregister (plugin->nat);
3288 if (NULL != plugin->defrag_ctxs)
3290 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
3291 heap_cleanup_iterator, NULL);
3292 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3293 plugin->defrag_ctxs = NULL;
3295 if (plugin->mst != NULL)
3297 GNUNET_SERVER_mst_destroy(plugin->mst);
3301 /* Clean up leftover messages */
3302 struct UDP_MessageWrapper * udpw;
3303 udpw = plugin->ipv4_queue_head;
3304 while (udpw != NULL)
3306 struct UDP_MessageWrapper *tmp = udpw->next;
3307 dequeue (plugin, udpw);
3308 call_continuation(udpw, GNUNET_SYSERR);
3313 udpw = plugin->ipv6_queue_head;
3314 while (udpw != NULL)
3316 struct UDP_MessageWrapper *tmp = udpw->next;
3317 dequeue (plugin, udpw);
3318 call_continuation(udpw, GNUNET_SYSERR);
3324 /* Clean up sessions */
3325 LOG (GNUNET_ERROR_TYPE_DEBUG,
3326 "Cleaning up sessions\n");
3327 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3328 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3330 next = ppc_dll_head;
3331 for (cur = next; NULL != cur; cur = next)
3334 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3335 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3336 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3342 GNUNET_free (plugin);
3345 struct Allocation *allocation;
3346 while (NULL != ahead)
3349 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3350 GNUNET_free (allocation);
3352 struct Allocator *allocator;
3353 while (NULL != aehead)
3356 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3357 GNUNET_free (allocator);
3364 /* end of plugin_transport_udp.c */