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 PLUGIN_NAME "udp"
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
67 * Running pretty printers: head
69 static struct PrettyPrinterContext *ppc_dll_head;
72 * Running pretty printers: tail
74 static struct PrettyPrinterContext *ppc_dll_tail;
77 * Closure for 'append_port'.
79 struct PrettyPrinterContext
84 struct PrettyPrinterContext *next;
89 struct PrettyPrinterContext *prev;
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
99 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
102 * Function to call with the result.
104 GNUNET_TRANSPORT_AddressStringCallback asc;
112 * Port to add after the IP address.
133 MSG_FRAGMENTED_COMPLETE = 2,
134 MSG_UNFRAGMENTED = 3,
142 * Which peer is this session for?
144 struct GNUNET_PeerIdentity target;
146 struct UDP_FragmentationContext * frag_ctx;
149 * Address of the other peer
151 const struct sockaddr *sock_addr;
154 * Desired delay for next sending we send to other peer
156 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
159 * Desired delay for next sending we received from other peer
161 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
164 * Session timeout task
166 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
169 * expected delay for ACKs
171 struct GNUNET_TIME_Relative last_expected_ack_delay;
174 * desired delay between UDP messages
176 struct GNUNET_TIME_Relative last_expected_msg_delay;
178 struct GNUNET_ATS_Information ats;
191 struct SessionCompareContext
194 const struct GNUNET_HELLO_Address *addr;
200 * Closure for 'process_inbound_tokenized_messages'
202 struct SourceInformation
207 struct GNUNET_PeerIdentity sender;
214 struct Session *session;
216 * Number of bytes in source address.
224 * Closure for 'find_receive_context'.
226 struct FindReceiveContext
229 * Where to store the result.
231 struct DefragContext *rc;
236 const struct sockaddr *addr;
238 struct Session *session;
241 * Number of bytes in 'addr'.
250 * Data structure to track defragmentation contexts based
251 * on the source of the UDP traffic.
257 * Defragmentation context.
259 struct GNUNET_DEFRAGMENT_Context *defrag;
262 * Source address this receive context is for (allocated at the
263 * end of the struct).
265 const struct sockaddr *src_addr;
268 * Reference to master plugin struct.
270 struct Plugin *plugin;
273 * Node in the defrag heap.
275 struct GNUNET_CONTAINER_HeapNode *hnode;
278 * Length of 'src_addr'
286 * Context to send fragmented messages
288 struct UDP_FragmentationContext
291 * Next in linked list
293 struct UDP_FragmentationContext * next;
296 * Previous in linked list
298 struct UDP_FragmentationContext * prev;
303 struct Plugin * plugin;
306 * Handle for GNUNET_FRAGMENT context
308 struct GNUNET_FRAGMENT_Context * frag;
311 * The session this fragmentation context belongs to
313 struct Session * session;
316 * Function to call upon completion of the transmission.
318 GNUNET_TRANSPORT_TransmitContinuation cont;
321 * Closure for 'cont'.
328 struct GNUNET_TIME_Absolute timeout;
331 * Payload size of original unfragmented message
336 * Bytes used to send all fragments on wire including UDP overhead
340 unsigned int fragments_used;
345 struct UDP_MessageWrapper
348 * Session this message belongs to
350 struct Session *session;
356 struct UDP_MessageWrapper *prev;
362 struct UDP_MessageWrapper *next;
366 * According to UDP_MessageType
371 * Message with size msg_size including UDP specific overhead
376 * Size of UDP message to send including UDP specific overhead
381 * Payload size of original message
388 struct GNUNET_TIME_Absolute timeout;
391 * Function to call upon completion of the transmission.
393 GNUNET_TRANSPORT_TransmitContinuation cont;
396 * Closure for 'cont'.
401 * Fragmentation context
402 * frag_ctx == NULL if transport <= MTU
403 * frag_ctx != NULL if transport > MTU
405 struct UDP_FragmentationContext *frag_ctx;
410 * UDP ACK Message-Packet header (after defragmentation).
412 struct UDP_ACK_Message
417 struct GNUNET_MessageHeader header;
420 * Desired delay for flow control
425 * What is the identity of the sender
427 struct GNUNET_PeerIdentity sender;
434 static uint32_t myoptions;
438 * Encapsulation of all of the state of the plugin.
440 struct Plugin * plugin;
444 * We have been notified that our readset has something to read. We don't
445 * know which socket needs to be read, so we have to check each one
446 * Then reschedule this function to be called again once more is available.
448 * @param cls the plugin handle
449 * @param tc the scheduling context (for rescheduling this function again)
452 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
456 * We have been notified that our readset has something to read. We don't
457 * know which socket needs to be read, so we have to check each one
458 * Then reschedule this function to be called again once more is available.
460 * @param cls the plugin handle
461 * @param tc the scheduling context (for rescheduling this function again)
464 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
471 stop_session_timeout (struct Session *s)
473 GNUNET_assert (NULL != s);
475 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
477 GNUNET_SCHEDULER_cancel (s->timeout_task);
478 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Timeout stopped for session %p canceled\n",
487 * (re)schedule select tasks for this plugin.
489 * @param plugin plugin to reschedule
492 schedule_select (struct Plugin *plugin)
494 struct GNUNET_TIME_Relative min_delay;
495 struct UDP_MessageWrapper *udpw;
497 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
499 /* Find a message ready to send:
500 * Flow delay from other peer is expired or not set (0) */
501 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
502 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
503 min_delay = GNUNET_TIME_relative_min (min_delay,
504 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
506 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
507 GNUNET_SCHEDULER_cancel(plugin->select_task);
510 * - write active set if message is ready
511 * - timeout minimum delay */
512 plugin->select_task =
513 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
514 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
516 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
517 &udp_plugin_select, plugin);
519 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
521 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
522 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
523 min_delay = GNUNET_TIME_relative_min (min_delay,
524 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
526 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
527 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
528 plugin->select_task_v6 =
529 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
530 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
532 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
533 &udp_plugin_select_v6, plugin);
539 * Function called for a quick conversion of the binary address to
540 * a numeric address. Note that the caller must not free the
541 * address and that the next call to this function is allowed
542 * to override the address again.
545 * @param addr binary address
546 * @param addrlen length of the address
547 * @return string representing the same address
550 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
552 static char rbuf[INET6_ADDRSTRLEN + 10];
553 char buf[INET6_ADDRSTRLEN];
557 const struct IPv4UdpAddress *t4;
558 const struct IPv6UdpAddress *t6;
563 if ((NULL != addr) && (addrlen == sizeof (struct IPv6UdpAddress)))
567 options = ntohl (t6->options);
568 port = ntohs (t6->u6_port);
569 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
572 else if ((NULL != addr) && (addrlen == sizeof (struct IPv4UdpAddress)))
576 options = ntohl (t4->options);
577 port = ntohs (t4->u4_port);
578 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
585 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
587 GNUNET_snprintf (rbuf, sizeof (rbuf),
588 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
589 PLUGIN_NAME, options, buf, port);
595 * Function called to convert a string address to
598 * @param cls closure ('struct Plugin*')
599 * @param addr string address
600 * @param addrlen length of the address
601 * @param buf location to store the buffer
602 * @param added location to store the number of bytes in the buffer.
603 * If the function returns GNUNET_SYSERR, its contents are undefined.
604 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
607 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
608 void **buf, size_t *added)
610 struct sockaddr_storage socket_address;
616 /* Format tcp.options.address:port */
621 if ((NULL == addr) || (addrlen == 0))
624 return GNUNET_SYSERR;
626 if ('\0' != addr[addrlen - 1])
629 return GNUNET_SYSERR;
631 if (strlen (addr) != addrlen - 1)
634 return GNUNET_SYSERR;
636 plugin = GNUNET_strdup (addr);
637 optionstr = strchr (plugin, '.');
638 if (NULL == optionstr)
641 GNUNET_free (plugin);
642 return GNUNET_SYSERR;
646 options = atol (optionstr);
647 address = strchr (optionstr, '.');
651 GNUNET_free (plugin);
652 return GNUNET_SYSERR;
658 GNUNET_STRINGS_to_address_ip (address, strlen (address),
662 GNUNET_free (plugin);
663 return GNUNET_SYSERR;
666 GNUNET_free (plugin);
668 switch (socket_address.ss_family)
672 struct IPv4UdpAddress *u4;
673 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
674 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
675 u4->options = htonl (options);
676 u4->ipv4_addr = in4->sin_addr.s_addr;
677 u4->u4_port = in4->sin_port;
679 *added = sizeof (struct IPv4UdpAddress);
684 struct IPv6UdpAddress *u6;
685 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
686 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
687 u6->options = htonl (options);
688 u6->ipv6_addr = in6->sin6_addr;
689 u6->u6_port = in6->sin6_port;
691 *added = sizeof (struct IPv6UdpAddress);
696 return GNUNET_SYSERR;
702 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
704 struct PrettyPrinterContext *ppc = cls;
705 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PPC %p was not removed!\n", ppc); */
706 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
707 if (NULL != ppc->resolver_handle)
709 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
710 ppc->resolver_handle = NULL;
713 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
719 * Append our port and forward the result.
721 * @param cls a 'struct PrettyPrinterContext'
722 * @param hostname result from DNS resolver
725 append_port (void *cls, const char *hostname)
727 struct PrettyPrinterContext *ppc = cls;
728 struct PrettyPrinterContext *cur;
730 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC callback: %p `%s'\n",ppc, hostname); */
731 if (hostname == NULL)
733 ppc->asc (ppc->asc_cls, NULL);
734 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
735 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
736 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
737 ppc->resolver_handle = NULL;
738 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was removed!\n", ppc); */
742 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
753 if (GNUNET_YES == ppc->ipv6)
754 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
756 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
757 ppc->asc (ppc->asc_cls, ret);
762 * Convert the transports address to a nice, human-readable
766 * @param type name of the transport that generated the address
767 * @param addr one of the addresses of the host, NULL for the last address
768 * the specific address format depends on the transport
769 * @param addrlen length of the address
770 * @param numeric should (IP) addresses be displayed in numeric form?
771 * @param timeout after how long should we give up?
772 * @param asc function to call on each string
773 * @param asc_cls closure for asc
776 udp_plugin_address_pretty_printer (void *cls, const char *type,
777 const void *addr, size_t addrlen,
779 struct GNUNET_TIME_Relative timeout,
780 GNUNET_TRANSPORT_AddressStringCallback asc,
783 struct PrettyPrinterContext *ppc;
786 struct sockaddr_in a4;
787 struct sockaddr_in6 a6;
788 const struct IPv4UdpAddress *u4;
789 const struct IPv6UdpAddress *u6;
793 if (addrlen == sizeof (struct IPv6UdpAddress))
796 memset (&a6, 0, sizeof (a6));
797 a6.sin6_family = AF_INET6;
798 #if HAVE_SOCKADDR_IN_SIN_LEN
799 a6.sin6_len = sizeof (a6);
801 a6.sin6_port = u6->u6_port;
802 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
803 port = ntohs (u6->u6_port);
804 options = ntohl (u6->options);
808 else if (addrlen == sizeof (struct IPv4UdpAddress))
811 memset (&a4, 0, sizeof (a4));
812 a4.sin_family = AF_INET;
813 #if HAVE_SOCKADDR_IN_SIN_LEN
814 a4.sin_len = sizeof (a4);
816 a4.sin_port = u4->u4_port;
817 a4.sin_addr.s_addr = u4->ipv4_addr;
818 port = ntohs (u4->u4_port);
819 options = ntohl (u4->options);
825 /* invalid address */
830 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
832 ppc->asc_cls = asc_cls;
834 ppc->options = options;
835 if (addrlen == sizeof (struct IPv6UdpAddress))
836 ppc->ipv6 = GNUNET_YES;
838 ppc->ipv6 = GNUNET_NO;
839 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
840 &ppc_cancel_task, ppc);
841 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
842 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was created!\n", ppc); */
843 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
849 call_continuation (struct UDP_MessageWrapper *udpw, int result)
853 LOG (GNUNET_ERROR_TYPE_DEBUG,
854 "Calling continuation for %u byte message to `%s' with result %s\n",
855 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
856 (GNUNET_OK == result) ? "OK" : "SYSERR");
858 if (udpw->msg_size >= udpw->payload_size)
859 overhead = udpw->msg_size - udpw->payload_size;
861 overhead = udpw->msg_size;
865 switch (udpw->msg_type) {
866 case MSG_UNFRAGMENTED:
867 if (NULL != udpw->cont)
869 /* Transport continuation */
870 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
871 udpw->payload_size, udpw->msg_size);
873 GNUNET_STATISTICS_update (plugin->env->stats,
874 "# UDP, unfragmented msgs, messages, sent, success",
876 GNUNET_STATISTICS_update (plugin->env->stats,
877 "# UDP, unfragmented msgs, bytes payload, sent, success",
878 udpw->payload_size, GNUNET_NO);
879 GNUNET_STATISTICS_update (plugin->env->stats,
880 "# UDP, unfragmented msgs, bytes overhead, sent, success",
881 overhead, GNUNET_NO);
882 GNUNET_STATISTICS_update (plugin->env->stats,
883 "# UDP, total, bytes overhead, sent",
884 overhead, GNUNET_NO);
885 GNUNET_STATISTICS_update (plugin->env->stats,
886 "# UDP, total, bytes payload, sent",
887 udpw->payload_size, GNUNET_NO);
889 case MSG_FRAGMENTED_COMPLETE:
890 GNUNET_assert (NULL != udpw->frag_ctx);
891 if (udpw->frag_ctx->cont != NULL)
892 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
893 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
894 GNUNET_STATISTICS_update (plugin->env->stats,
895 "# UDP, fragmented msgs, messages, sent, success",
897 GNUNET_STATISTICS_update (plugin->env->stats,
898 "# UDP, fragmented msgs, bytes payload, sent, success",
899 udpw->payload_size, GNUNET_NO);
900 GNUNET_STATISTICS_update (plugin->env->stats,
901 "# UDP, fragmented msgs, bytes overhead, sent, success",
902 overhead, GNUNET_NO);
903 GNUNET_STATISTICS_update (plugin->env->stats,
904 "# UDP, total, bytes overhead, sent",
905 overhead, GNUNET_NO);
906 GNUNET_STATISTICS_update (plugin->env->stats,
907 "# UDP, total, bytes payload, sent",
908 udpw->payload_size, GNUNET_NO);
909 GNUNET_STATISTICS_update (plugin->env->stats,
910 "# UDP, fragmented msgs, messages, pending",
914 /* Fragmented message: enqueue next fragment */
915 if (NULL != udpw->cont)
916 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
917 udpw->payload_size, udpw->msg_size);
918 GNUNET_STATISTICS_update (plugin->env->stats,
919 "# UDP, fragmented msgs, fragments, sent, success",
921 GNUNET_STATISTICS_update (plugin->env->stats,
922 "# UDP, fragmented msgs, fragments bytes, sent, success",
923 udpw->msg_size, GNUNET_NO);
926 /* No continuation */
927 GNUNET_STATISTICS_update (plugin->env->stats,
928 "# UDP, ACK msgs, messages, sent, success",
930 GNUNET_STATISTICS_update (plugin->env->stats,
931 "# UDP, ACK msgs, bytes overhead, sent, success",
932 overhead, GNUNET_NO);
933 GNUNET_STATISTICS_update (plugin->env->stats,
934 "# UDP, total, bytes overhead, sent",
935 overhead, GNUNET_NO);
941 LOG (GNUNET_ERROR_TYPE_ERROR,
942 "ERROR: %u\n", udpw->msg_type);
948 switch (udpw->msg_type) {
949 case MSG_UNFRAGMENTED:
950 /* Unfragmented message: failed to send */
951 if (NULL != udpw->cont)
952 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
953 udpw->payload_size, overhead);
954 GNUNET_STATISTICS_update (plugin->env->stats,
955 "# UDP, unfragmented msgs, messages, sent, failure",
957 GNUNET_STATISTICS_update (plugin->env->stats,
958 "# UDP, unfragmented msgs, bytes payload, sent, failure",
959 udpw->payload_size, GNUNET_NO);
960 GNUNET_STATISTICS_update (plugin->env->stats,
961 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
962 overhead, GNUNET_NO);
964 case MSG_FRAGMENTED_COMPLETE:
965 GNUNET_assert (NULL != udpw->frag_ctx);
966 if (udpw->frag_ctx->cont != NULL)
967 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
968 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
969 GNUNET_STATISTICS_update (plugin->env->stats,
970 "# UDP, fragmented msgs, messages, sent, failure",
972 GNUNET_STATISTICS_update (plugin->env->stats,
973 "# UDP, fragmented msgs, bytes payload, sent, failure",
974 udpw->payload_size, GNUNET_NO);
975 GNUNET_STATISTICS_update (plugin->env->stats,
976 "# UDP, fragmented msgs, bytes payload, sent, failure",
977 overhead, GNUNET_NO);
978 GNUNET_STATISTICS_update (plugin->env->stats,
979 "# UDP, fragmented msgs, bytes payload, sent, failure",
980 overhead, GNUNET_NO);
981 GNUNET_STATISTICS_update (plugin->env->stats,
982 "# UDP, fragmented msgs, messages, pending",
986 GNUNET_assert (NULL != udpw->frag_ctx);
987 /* Fragmented message: failed to send */
988 GNUNET_STATISTICS_update (plugin->env->stats,
989 "# UDP, fragmented msgs, fragments, sent, failure",
991 GNUNET_STATISTICS_update (plugin->env->stats,
992 "# UDP, fragmented msgs, fragments bytes, sent, failure",
993 udpw->msg_size, GNUNET_NO);
996 /* ACK message: failed to send */
997 GNUNET_STATISTICS_update (plugin->env->stats,
998 "# UDP, ACK msgs, messages, sent, failure",
1002 /* Beacon message: failed to send */
1018 * Check if the given port is plausible (must be either our listen
1019 * port or our advertised port). If it is neither, we return
1022 * @param plugin global variables
1023 * @param in_port port number to check
1024 * @return GNUNET_OK if port is either open_port or adv_port
1027 check_port (struct Plugin *plugin, uint16_t in_port)
1029 if ((in_port == plugin->port) || (in_port == plugin->aport))
1031 return GNUNET_SYSERR;
1036 * Function that will be called to check if a binary address for this
1037 * plugin is well-formed and corresponds to an address for THIS peer
1038 * (as per our configuration). Naturally, if absolutely necessary,
1039 * plugins can be a bit conservative in their answer, but in general
1040 * plugins should make sure that the address does not redirect
1041 * traffic to a 3rd party that might try to man-in-the-middle our
1044 * @param cls closure, should be our handle to the Plugin
1045 * @param addr pointer to the address
1046 * @param addrlen length of addr
1047 * @return GNUNET_OK if this is a plausible address for this peer
1048 * and transport, GNUNET_SYSERR if not
1052 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1054 struct Plugin *plugin = cls;
1055 struct IPv4UdpAddress *v4;
1056 struct IPv6UdpAddress *v6;
1058 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1059 (addrlen != sizeof (struct IPv6UdpAddress)))
1061 return GNUNET_SYSERR;
1063 if (addrlen == sizeof (struct IPv4UdpAddress))
1065 v4 = (struct IPv4UdpAddress *) addr;
1066 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1067 return GNUNET_SYSERR;
1069 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1070 sizeof (struct in_addr)))
1071 return GNUNET_SYSERR;
1075 v6 = (struct IPv6UdpAddress *) addr;
1076 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1078 GNUNET_break_op (0);
1079 return GNUNET_SYSERR;
1081 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1082 return GNUNET_SYSERR;
1084 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1085 sizeof (struct in6_addr)))
1086 return GNUNET_SYSERR;
1093 * Task to free resources associated with a session.
1095 * @param s session to free
1098 free_session (struct Session *s)
1100 if (NULL != s->frag_ctx)
1102 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
1103 GNUNET_free (s->frag_ctx);
1111 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1113 if (plugin->bytes_in_buffer < udpw->msg_size)
1117 GNUNET_STATISTICS_update (plugin->env->stats,
1118 "# UDP, total, bytes in buffers",
1119 - (long long) udpw->msg_size, GNUNET_NO);
1120 plugin->bytes_in_buffer -= udpw->msg_size;
1122 GNUNET_STATISTICS_update (plugin->env->stats,
1123 "# UDP, total, msgs in buffers",
1125 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1126 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1127 plugin->ipv4_queue_tail, udpw);
1128 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1129 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1130 plugin->ipv6_queue_tail, udpw);
1135 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1137 struct UDP_MessageWrapper *udpw;
1138 struct UDP_MessageWrapper *tmp;
1139 struct UDP_MessageWrapper dummy;
1140 struct Session *s = fc->session;
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "%p : Fragmented message removed with result %s\n",
1145 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1147 /* Call continuation for fragmented message */
1148 memset (&dummy, 0, sizeof (dummy));
1149 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1150 dummy.msg_size = s->frag_ctx->on_wire_size;
1151 dummy.payload_size = s->frag_ctx->payload_size;
1152 dummy.frag_ctx = s->frag_ctx;
1154 dummy.cont_cls = NULL;
1157 call_continuation (&dummy, result);
1159 /* Remove leftover fragments from queue */
1160 if (s->addrlen == sizeof (struct sockaddr_in6))
1162 udpw = plugin->ipv6_queue_head;
1163 while (NULL != udpw)
1166 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1168 dequeue (plugin, udpw);
1169 call_continuation (udpw, GNUNET_SYSERR);
1175 if (s->addrlen == sizeof (struct sockaddr_in))
1177 udpw = plugin->ipv4_queue_head;
1181 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1183 dequeue (plugin, udpw);
1184 call_continuation (udpw, GNUNET_SYSERR);
1191 /* Destroy fragmentation context */
1192 GNUNET_FRAGMENT_context_destroy (fc->frag,
1193 &s->last_expected_msg_delay,
1194 &s->last_expected_ack_delay);
1200 * Functions with this signature are called whenever we need
1201 * to close a session due to a disconnect or failure to
1202 * establish a connection.
1204 * @param s session to close down
1207 disconnect_session (struct Session *s)
1209 struct UDP_MessageWrapper *udpw;
1210 struct UDP_MessageWrapper *next;
1212 GNUNET_assert (GNUNET_YES != s->in_destroy);
1213 LOG (GNUNET_ERROR_TYPE_DEBUG,
1214 "Session %p to peer `%s' address ended \n",
1216 GNUNET_i2s (&s->target),
1217 GNUNET_a2s (s->sock_addr, s->addrlen));
1218 stop_session_timeout (s);
1220 if (NULL != s->frag_ctx)
1222 /* Remove fragmented message due to disconnect */
1223 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1226 next = plugin->ipv4_queue_head;
1227 while (NULL != (udpw = next))
1230 if (udpw->session == s)
1232 dequeue (plugin, udpw);
1233 call_continuation(udpw, GNUNET_SYSERR);
1237 next = plugin->ipv6_queue_head;
1238 while (NULL != (udpw = next))
1241 if (udpw->session == s)
1243 dequeue (plugin, udpw);
1244 call_continuation(udpw, GNUNET_SYSERR);
1248 plugin->env->session_end (plugin->env->cls, &s->target, s);
1250 if (NULL != s->frag_ctx)
1252 if (NULL != s->frag_ctx->cont)
1254 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1255 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1256 LOG (GNUNET_ERROR_TYPE_DEBUG,
1257 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1258 GNUNET_i2s (&s->target));
1262 GNUNET_assert (GNUNET_YES ==
1263 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1266 GNUNET_STATISTICS_set(plugin->env->stats,
1267 "# UDP, sessions active",
1268 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1271 s->in_destroy = GNUNET_YES;
1277 * Destroy a session, plugin is being unloaded.
1280 * @param key hash of public key of target peer
1281 * @param value a 'struct PeerSession*' to clean up
1282 * @return GNUNET_OK (continue to iterate)
1285 disconnect_and_free_it (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1287 disconnect_session(value);
1293 * Disconnect from a remote node. Clean up session if we have one for this peer
1295 * @param cls closure for this call (should be handle to Plugin)
1296 * @param target the peeridentity of the peer to disconnect
1297 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1300 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1302 struct Plugin *plugin = cls;
1303 GNUNET_assert (plugin != NULL);
1305 GNUNET_assert (target != NULL);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1308 /* Clean up sessions */
1309 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, target,
1310 &disconnect_and_free_it, plugin);
1315 * Session was idle, so disconnect it
1318 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 GNUNET_assert (NULL != cls);
1321 struct Session *s = cls;
1323 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Session %p was idle for %s, disconnecting\n",
1327 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1329 /* call session destroy function */
1330 disconnect_session (s);
1335 * Start session timeout
1338 start_session_timeout (struct Session *s)
1340 GNUNET_assert (NULL != s);
1341 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1342 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 "Timeout for session %p set to %s\n",
1348 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1354 * Increment session timeout due to activity
1357 reschedule_session_timeout (struct Session *s)
1359 GNUNET_assert (NULL != s);
1360 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1362 GNUNET_SCHEDULER_cancel (s->timeout_task);
1363 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "Timeout rescheduled for session %p set to %s\n",
1369 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1374 static struct Session *
1375 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1376 const void *addr, size_t addrlen,
1377 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1380 const struct IPv4UdpAddress *t4;
1381 const struct IPv6UdpAddress *t6;
1382 struct sockaddr_in *v4;
1383 struct sockaddr_in6 *v6;
1394 case sizeof (struct IPv4UdpAddress):
1395 if (NULL == plugin->sockv4)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG,
1398 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1400 udp_address_to_string(NULL, addr, addrlen));
1404 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1405 len = sizeof (struct sockaddr_in);
1406 v4 = (struct sockaddr_in *) &s[1];
1407 v4->sin_family = AF_INET;
1408 #if HAVE_SOCKADDR_IN_SIN_LEN
1409 v4->sin_len = sizeof (struct sockaddr_in);
1411 v4->sin_port = t4->u4_port;
1412 v4->sin_addr.s_addr = t4->ipv4_addr;
1413 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1415 case sizeof (struct IPv6UdpAddress):
1416 if (NULL == plugin->sockv6)
1418 LOG (GNUNET_ERROR_TYPE_INFO,
1419 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1421 udp_address_to_string(NULL, addr, addrlen));
1425 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1426 len = sizeof (struct sockaddr_in6);
1427 v6 = (struct sockaddr_in6 *) &s[1];
1428 v6->sin6_family = AF_INET6;
1429 #if HAVE_SOCKADDR_IN_SIN_LEN
1430 v6->sin6_len = sizeof (struct sockaddr_in6);
1432 v6->sin6_port = t6->u6_port;
1433 v6->sin6_addr = t6->ipv6_addr;
1434 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1437 /* Must have a valid address to send to */
1438 GNUNET_STATISTICS_update (plugin->env->stats,
1440 ("# requests to create session with invalid address"),
1445 s->target = *target;
1446 s->sock_addr = (const struct sockaddr *) &s[1];
1447 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1448 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1449 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1450 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1451 s->inbound = GNUNET_NO;
1452 start_session_timeout (s);
1458 session_cmp_it (void *cls,
1459 const struct GNUNET_PeerIdentity * key,
1462 struct SessionCompareContext * cctx = cls;
1463 const struct GNUNET_HELLO_Address *address = cctx->addr;
1464 struct Session *s = value;
1466 socklen_t s_addrlen = s->addrlen;
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "Comparing address %s <-> %s\n",
1470 udp_address_to_string (NULL, (void *) address->address,
1471 address->address_length),
1472 GNUNET_a2s (s->sock_addr, s->addrlen));
1473 if (s->inbound != cctx->inbound)
1475 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1476 (s_addrlen == sizeof (struct sockaddr_in)))
1478 struct IPv4UdpAddress * u4 = NULL;
1479 u4 = (struct IPv4UdpAddress *) address->address;
1480 GNUNET_assert (NULL != u4);
1481 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1482 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1483 (u4->u4_port == s4->sin_port))
1490 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1491 (s_addrlen == sizeof (struct sockaddr_in6)))
1493 struct IPv6UdpAddress * u6 = NULL;
1494 u6 = (struct IPv6UdpAddress *) address->address;
1495 GNUNET_assert (NULL != u6);
1496 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1497 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1498 (u6->u6_port == s6->sin6_port))
1509 * Function obtain the network type for a session
1511 * @param cls closure ('struct Plugin*')
1512 * @param session the session
1513 * @return the network type in HBO or GNUNET_SYSERR
1515 static enum GNUNET_ATS_Network_Type
1516 udp_get_network (void *cls,
1517 struct Session *session)
1519 return ntohl (session->ats.value);
1524 * Creates a new outbound session the transport service will use to send data to the
1527 * @param cls the plugin
1528 * @param address the address
1529 * @param inbound look for inbound session
1530 * @return the session or NULL of max connections exceeded
1532 static struct Session *
1533 udp_plugin_lookup_session (void *cls,
1534 const struct GNUNET_HELLO_Address *address,
1537 struct Plugin * plugin = cls;
1538 struct IPv6UdpAddress * udp_a6;
1539 struct IPv4UdpAddress * udp_a4;
1541 GNUNET_assert (plugin != NULL);
1542 GNUNET_assert (address != NULL);
1545 if ((address->address == NULL) ||
1546 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1547 (address->address_length != sizeof (struct IPv6UdpAddress))))
1549 LOG (GNUNET_ERROR_TYPE_WARNING,
1550 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1551 address->address_length,
1552 sizeof (struct IPv4UdpAddress),
1553 sizeof (struct IPv6UdpAddress));
1557 if (address->address_length == sizeof (struct IPv4UdpAddress))
1559 if (plugin->sockv4 == NULL)
1561 udp_a4 = (struct IPv4UdpAddress *) address->address;
1562 if (udp_a4->u4_port == 0)
1566 if (address->address_length == sizeof (struct IPv6UdpAddress))
1568 if (plugin->sockv6 == NULL)
1570 udp_a6 = (struct IPv6UdpAddress *) address->address;
1571 if (udp_a6->u6_port == 0)
1575 /* check if session already exists */
1576 struct SessionCompareContext cctx;
1577 cctx.addr = address;
1579 cctx.inbound = inbound;
1580 LOG (GNUNET_ERROR_TYPE_DEBUG,
1581 "Looking for existing session for peer `%s' `%s' \n",
1582 GNUNET_i2s (&address->peer),
1583 udp_address_to_string(NULL, address->address, address->address_length));
1584 GNUNET_CONTAINER_multipeermap_get_multiple(plugin->sessions, &address->peer, session_cmp_it, &cctx);
1585 if (cctx.res != NULL)
1587 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1594 static struct Session *
1595 udp_plugin_create_session (void *cls,
1596 const struct GNUNET_HELLO_Address *address,
1601 s = create_session (plugin,
1604 address->address_length,
1607 return NULL; /* protocol not supported or address invalid */
1608 s->inbound = inbound;
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Creating new %s session %p for peer `%s' address `%s'\n",
1611 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
1613 GNUNET_i2s(&address->peer),
1614 udp_address_to_string(NULL,address->address,address->address_length));
1615 GNUNET_assert (GNUNET_OK ==
1616 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1620 GNUNET_STATISTICS_set(plugin->env->stats,
1621 "# UDP, sessions active",
1622 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1629 * Creates a new outbound session the transport service will use to send data to the
1632 * @param cls the plugin
1633 * @param address the address
1634 * @return the session or NULL of max connections exceeded
1636 static struct Session *
1637 udp_plugin_get_session (void *cls,
1638 const struct GNUNET_HELLO_Address *address)
1642 if (NULL == address)
1647 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1648 (address->address_length != sizeof (struct IPv6UdpAddress)))
1651 /* otherwise create new */
1652 if (NULL != (s = udp_plugin_lookup_session (cls, address, GNUNET_NO)))
1654 return udp_plugin_create_session (cls, address, GNUNET_NO);
1659 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1661 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1665 GNUNET_STATISTICS_update (plugin->env->stats,
1666 "# UDP, total, bytes in buffers",
1667 udpw->msg_size, GNUNET_NO);
1668 plugin->bytes_in_buffer += udpw->msg_size;
1670 GNUNET_STATISTICS_update (plugin->env->stats,
1671 "# UDP, total, msgs in buffers",
1673 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1674 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1675 plugin->ipv4_queue_tail, udpw);
1676 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1677 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1678 plugin->ipv6_queue_tail, udpw);
1684 * Fragment message was transmitted via UDP, let fragmentation know
1685 * to send the next fragment now.
1687 * @param cls the 'struct UDPMessageWrapper' of the fragment
1688 * @param target destination peer (ignored)
1689 * @param result GNUNET_OK on success (ignored)
1690 * @param payload bytes payload sent
1691 * @param physical bytes physical sent
1694 send_next_fragment (void *cls,
1695 const struct GNUNET_PeerIdentity *target,
1696 int result, size_t payload, size_t physical)
1698 struct UDP_MessageWrapper *udpw = cls;
1700 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1705 * Function that is called with messages created by the fragmentation
1706 * module. In the case of the 'proc' callback of the
1707 * GNUNET_FRAGMENT_context_create function, this function must
1708 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1710 * @param cls closure, the 'struct FragmentationContext'
1711 * @param msg the message that was created
1714 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1716 struct UDP_FragmentationContext *frag_ctx = cls;
1717 struct Plugin *plugin = frag_ctx->plugin;
1718 struct UDP_MessageWrapper * udpw;
1719 size_t msg_len = ntohs (msg->size);
1721 LOG (GNUNET_ERROR_TYPE_DEBUG,
1722 "Enqueuing fragment with %u bytes\n", msg_len);
1723 frag_ctx->fragments_used ++;
1724 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1725 udpw->session = frag_ctx->session;
1726 udpw->msg_buf = (char *) &udpw[1];
1727 udpw->msg_size = msg_len;
1728 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1729 udpw->cont = &send_next_fragment;
1730 udpw->cont_cls = udpw;
1731 udpw->timeout = frag_ctx->timeout;
1732 udpw->frag_ctx = frag_ctx;
1733 udpw->msg_type = MSG_FRAGMENTED;
1734 memcpy (udpw->msg_buf, msg, msg_len);
1735 enqueue (plugin, udpw);
1736 schedule_select (plugin);
1741 * Function that can be used by the transport service to transmit
1742 * a message using the plugin. Note that in the case of a
1743 * peer disconnecting, the continuation MUST be called
1744 * prior to the disconnect notification itself. This function
1745 * will be called with this peer's HELLO message to initiate
1746 * a fresh connection to another peer.
1748 * @param cls closure
1749 * @param s which session must be used
1750 * @param msgbuf the message to transmit
1751 * @param msgbuf_size number of bytes in 'msgbuf'
1752 * @param priority how important is the message (most plugins will
1753 * ignore message priority and just FIFO)
1754 * @param to how long to wait at most for the transmission (does not
1755 * require plugins to discard the message after the timeout,
1756 * just advisory for the desired delay; most plugins will ignore
1758 * @param cont continuation to call once the message has
1759 * been transmitted (or if the transport is ready
1760 * for the next transmission call; or if the
1761 * peer disconnected...); can be NULL
1762 * @param cont_cls closure for cont
1763 * @return number of bytes used (on the physical network, with overheads);
1764 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1765 * and does NOT mean that the message was not transmitted (DV)
1768 udp_plugin_send (void *cls,
1770 const char *msgbuf, size_t msgbuf_size,
1771 unsigned int priority,
1772 struct GNUNET_TIME_Relative to,
1773 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1775 struct Plugin *plugin = cls;
1776 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1777 struct UDP_FragmentationContext * frag_ctx;
1778 struct UDP_MessageWrapper * udpw;
1779 struct UDPMessage *udp;
1781 GNUNET_assert (plugin != NULL);
1782 GNUNET_assert (s != NULL);
1784 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1785 return GNUNET_SYSERR;
1786 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1787 return GNUNET_SYSERR;
1788 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1791 return GNUNET_SYSERR;
1793 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains_value(plugin->sessions, &s->target, s))
1796 return GNUNET_SYSERR;
1798 LOG (GNUNET_ERROR_TYPE_DEBUG,
1799 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1801 GNUNET_i2s (&s->target),
1802 GNUNET_a2s(s->sock_addr, s->addrlen));
1806 udp = (struct UDPMessage *) mbuf;
1807 udp->header.size = htons (udpmlen);
1808 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1809 udp->reserved = htonl (0);
1810 udp->sender = *plugin->env->my_identity;
1812 reschedule_session_timeout(s);
1813 if (udpmlen <= UDP_MTU)
1815 /* unfragmented message */
1816 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1818 udpw->msg_buf = (char *) &udpw[1];
1819 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1820 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1821 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1823 udpw->cont_cls = cont_cls;
1824 udpw->frag_ctx = NULL;
1825 udpw->msg_type = MSG_UNFRAGMENTED;
1826 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1827 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1828 enqueue (plugin, udpw);
1830 GNUNET_STATISTICS_update (plugin->env->stats,
1831 "# UDP, unfragmented msgs, messages, attempt",
1833 GNUNET_STATISTICS_update (plugin->env->stats,
1834 "# UDP, unfragmented msgs, bytes payload, attempt",
1835 udpw->payload_size, GNUNET_NO);
1839 /* fragmented message */
1840 if (s->frag_ctx != NULL)
1841 return GNUNET_SYSERR;
1842 memcpy (&udp[1], msgbuf, msgbuf_size);
1843 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1844 frag_ctx->plugin = plugin;
1845 frag_ctx->session = s;
1846 frag_ctx->cont = cont;
1847 frag_ctx->cont_cls = cont_cls;
1848 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1849 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1850 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1851 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1854 s->last_expected_msg_delay,
1855 s->last_expected_ack_delay,
1859 s->frag_ctx = frag_ctx;
1860 GNUNET_STATISTICS_update (plugin->env->stats,
1861 "# UDP, fragmented msgs, messages, pending",
1863 GNUNET_STATISTICS_update (plugin->env->stats,
1864 "# UDP, fragmented msgs, messages, attempt",
1866 GNUNET_STATISTICS_update (plugin->env->stats,
1867 "# UDP, fragmented msgs, bytes payload, attempt",
1868 frag_ctx->payload_size, GNUNET_NO);
1870 schedule_select (plugin);
1876 * Our external IP address/port mapping has changed.
1878 * @param cls closure, the 'struct LocalAddrList'
1879 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1880 * the previous (now invalid) one
1881 * @param addr either the previous or the new public IP address
1882 * @param addrlen actual lenght of the address
1885 udp_nat_port_map_callback (void *cls, int add_remove,
1886 const struct sockaddr *addr, socklen_t addrlen)
1888 struct Plugin *plugin = cls;
1889 struct IPv4UdpAddress u4;
1890 struct IPv6UdpAddress u6;
1894 LOG (GNUNET_ERROR_TYPE_INFO,
1895 "NAT notification to %s address `%s'\n",
1896 (GNUNET_YES == add_remove) ? "add" : "remove",
1897 GNUNET_a2s (addr, addrlen));
1899 /* convert 'addr' to our internal format */
1900 switch (addr->sa_family)
1903 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1904 memset (&u4, 0, sizeof (u4));
1905 u4.options = htonl(myoptions);
1906 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1907 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1908 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1911 args = sizeof (struct IPv4UdpAddress);
1914 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1915 memset (&u4, 0, sizeof (u4));
1916 u6.options = htonl(myoptions);
1917 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1919 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1920 sizeof (struct in6_addr));
1921 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1923 args = sizeof (struct IPv6UdpAddress);
1929 /* modify our published address list */
1930 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1936 * Message tokenizer has broken up an incomming message. Pass it on
1939 * @param cls the 'struct Plugin'
1940 * @param client the 'struct SourceInformation'
1941 * @param hdr the actual message
1944 process_inbound_tokenized_messages (void *cls, void *client,
1945 const struct GNUNET_MessageHeader *hdr)
1947 struct Plugin *plugin = cls;
1948 struct SourceInformation *si = client;
1949 struct GNUNET_TIME_Relative delay;
1951 GNUNET_assert (si->session != NULL);
1952 if (GNUNET_YES == si->session->in_destroy)
1955 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1956 delay = plugin->env->receive (plugin->env->cls,
1960 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1961 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1963 plugin->env->update_address_metrics (plugin->env->cls,
1965 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1966 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1968 &si->session->ats, 1);
1970 si->session->flow_delay_for_other_peer = delay;
1971 reschedule_session_timeout(si->session);
1977 * We've received a UDP Message. Process it (pass contents to main service).
1979 * @param plugin plugin context
1980 * @param msg the message
1981 * @param sender_addr sender address
1982 * @param sender_addr_len number of bytes in sender_addr
1985 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1986 const struct sockaddr *sender_addr,
1987 socklen_t sender_addr_len)
1989 struct SourceInformation si;
1991 struct IPv4UdpAddress u4;
1992 struct IPv6UdpAddress u6;
1996 if (0 != ntohl (msg->reserved))
1998 GNUNET_break_op (0);
2001 if (ntohs (msg->header.size) <
2002 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2004 GNUNET_break_op (0);
2008 /* convert address */
2009 switch (sender_addr->sa_family)
2012 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2013 memset (&u4, 0, sizeof (u4));
2014 u6.options = htonl (0);
2015 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2016 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2021 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2022 memset (&u6, 0, sizeof (u6));
2023 u6.options = htonl (0);
2024 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2025 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2033 LOG (GNUNET_ERROR_TYPE_DEBUG,
2034 "Received message with %u bytes from peer `%s' at `%s'\n",
2035 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2036 GNUNET_a2s (sender_addr, sender_addr_len));
2038 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2039 if (NULL == (s = udp_plugin_lookup_session (plugin, address, GNUNET_YES)))
2041 s = udp_plugin_create_session (plugin, address, GNUNET_YES);
2042 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2043 address->address, address->address_length, s, NULL, 0);
2045 GNUNET_free (address);
2047 /* iterate over all embedded messages */
2049 si.sender = msg->sender;
2053 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2054 ntohs (msg->header.size) -
2055 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
2057 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2063 * Scan the heap for a receive context with the given address.
2065 * @param cls the 'struct FindReceiveContext'
2066 * @param node internal node of the heap
2067 * @param element value stored at the node (a 'struct ReceiveContext')
2068 * @param cost cost associated with the node
2069 * @return GNUNET_YES if we should continue to iterate,
2073 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2074 void *element, GNUNET_CONTAINER_HeapCostType cost)
2076 struct FindReceiveContext *frc = cls;
2077 struct DefragContext *e = element;
2079 if ((frc->addr_len == e->addr_len) &&
2080 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2090 * Process a defragmented message.
2092 * @param cls the 'struct ReceiveContext'
2093 * @param msg the message
2096 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2098 struct DefragContext *rc = cls;
2100 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2105 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2110 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2111 rc->src_addr, rc->addr_len);
2115 struct LookupContext
2117 struct Session *res;
2119 const struct sockaddr * addr;
2123 int must_have_frag_ctx;
2128 lookup_session_by_addr_it (void *cls,
2129 const struct GNUNET_PeerIdentity *key,
2132 struct LookupContext *l_ctx = cls;
2133 struct Session * s = value;
2135 if ((GNUNET_YES == l_ctx->must_have_frag_ctx) && (NULL == s->frag_ctx))
2139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Comparing session: have %s %s %p<-> want %s\n",
2140 GNUNET_a2s(s->sock_addr, s->addrlen),
2141 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
2143 GNUNET_a2s(l_ctx->addr, l_ctx->addrlen));
2145 if ((s->addrlen == l_ctx->addrlen) &&
2146 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2156 * Transmit an acknowledgement.
2158 * @param cls the 'struct ReceiveContext'
2159 * @param id message ID (unused)
2160 * @param msg ack to transmit
2163 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2165 struct DefragContext *rc = cls;
2166 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2167 struct UDP_ACK_Message *udp_ack;
2169 struct UDP_MessageWrapper *udpw;
2171 struct LookupContext l_ctx;
2173 l_ctx.addr = rc->src_addr;
2174 l_ctx.addrlen = rc->addr_len;
2176 l_ctx.must_have_frag_ctx = GNUNET_NO;
2177 GNUNET_CONTAINER_multipeermap_iterate (rc->plugin->sessions,
2178 &lookup_session_by_addr_it,
2185 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2186 delay = s->flow_delay_for_other_peer.rel_value_us;
2188 LOG (GNUNET_ERROR_TYPE_DEBUG,
2189 "Sending ACK to `%s' including delay of %s\n",
2190 GNUNET_a2s (rc->src_addr,
2191 (rc->src_addr->sa_family ==
2192 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2194 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2196 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2197 udpw->msg_size = msize;
2198 udpw->payload_size = 0;
2200 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2201 udpw->msg_buf = (char *)&udpw[1];
2202 udpw->msg_type = MSG_ACK;
2203 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2204 udp_ack->header.size = htons ((uint16_t) msize);
2205 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2206 udp_ack->delay = htonl (delay);
2207 udp_ack->sender = *rc->plugin->env->my_identity;
2208 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2209 enqueue (rc->plugin, udpw);
2214 read_process_msg (struct Plugin *plugin,
2215 const struct GNUNET_MessageHeader *msg,
2216 const struct sockaddr *addr,
2219 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2221 GNUNET_break_op (0);
2224 process_udp_message (plugin, (const struct UDPMessage *) msg,
2230 read_process_ack (struct Plugin *plugin,
2231 const struct GNUNET_MessageHeader *msg,
2232 const struct sockaddr *addr,
2235 const struct GNUNET_MessageHeader *ack;
2236 const struct UDP_ACK_Message *udp_ack;
2237 struct LookupContext l_ctx;
2239 struct GNUNET_TIME_Relative flow_delay;
2241 if (ntohs (msg->size) <
2242 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2244 GNUNET_break_op (0);
2247 udp_ack = (const struct UDP_ACK_Message *) msg;
2248 l_ctx.addr = (const struct sockaddr *) addr;
2249 l_ctx.addrlen = fromlen;
2251 l_ctx.must_have_frag_ctx = GNUNET_YES;
2252 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2253 &lookup_session_by_addr_it,
2256 if ((NULL == s) || (NULL == s->frag_ctx))
2262 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2263 LOG (GNUNET_ERROR_TYPE_DEBUG,
2264 "We received a sending delay of %s\n",
2265 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2267 s->flow_delay_from_other_peer =
2268 GNUNET_TIME_relative_to_absolute (flow_delay);
2270 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2271 if (ntohs (ack->size) !=
2272 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2274 GNUNET_break_op (0);
2278 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2280 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2282 LOG (GNUNET_ERROR_TYPE_DEBUG,
2283 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2284 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2285 GNUNET_a2s (addr, fromlen));
2286 /* Expect more ACKs to arrive */
2290 LOG (GNUNET_ERROR_TYPE_DEBUG,
2291 "Message full ACK'ed\n",
2292 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2293 GNUNET_a2s (addr, fromlen));
2295 /* Remove fragmented message after successful sending */
2296 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2301 read_process_fragment (struct Plugin *plugin,
2302 const struct GNUNET_MessageHeader *msg,
2303 const struct sockaddr *addr,
2306 struct DefragContext *d_ctx;
2307 struct GNUNET_TIME_Absolute now;
2308 struct FindReceiveContext frc;
2312 frc.addr_len = fromlen;
2314 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2315 (unsigned int) ntohs (msg->size),
2316 GNUNET_a2s (addr, fromlen));
2317 /* Lookup existing receive context for this address */
2318 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2319 &find_receive_context,
2321 now = GNUNET_TIME_absolute_get ();
2326 /* Create a new defragmentation context */
2327 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2328 memcpy (&d_ctx[1], addr, fromlen);
2329 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2330 d_ctx->addr_len = fromlen;
2331 d_ctx->plugin = plugin;
2333 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2334 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2335 &fragment_msg_proc, &ack_proc);
2337 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2338 (GNUNET_CONTAINER_HeapCostType)
2340 LOG (GNUNET_ERROR_TYPE_DEBUG,
2341 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2342 (unsigned int) ntohs (msg->size),
2343 GNUNET_a2s (addr, fromlen));
2347 LOG (GNUNET_ERROR_TYPE_DEBUG,
2348 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2349 (unsigned int) ntohs (msg->size),
2350 GNUNET_a2s (addr, fromlen));
2353 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2355 /* keep this 'rc' from expiring */
2356 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2357 (GNUNET_CONTAINER_HeapCostType)
2360 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2361 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2363 /* remove 'rc' that was inactive the longest */
2364 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2365 GNUNET_assert (NULL != d_ctx);
2366 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2367 GNUNET_free (d_ctx);
2373 * Read and process a message from the given socket.
2375 * @param plugin the overall plugin
2376 * @param rsock socket to read from
2379 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2382 struct sockaddr_storage addr;
2383 char buf[65536] GNUNET_ALIGN;
2385 const struct GNUNET_MessageHeader *msg;
2387 fromlen = sizeof (addr);
2388 memset (&addr, 0, sizeof (addr));
2389 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2390 (struct sockaddr *) &addr, &fromlen);
2392 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2393 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2394 * on this socket has failed.
2396 * WSAECONNRESET - The virtual circuit was reset by the remote side
2397 * executing a hard or abortive close. The application should close
2398 * the socket; it is no longer usable. On a UDP-datagram socket this
2399 * error indicates a previous send operation resulted in an ICMP Port
2400 * Unreachable message.
2402 if ( (-1 == size) && (ECONNRESET == errno) )
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "UDP failed to receive data: %s\n", STRERROR (errno));
2409 /* Connection failure or something. Not a protocol violation. */
2412 if (size < sizeof (struct GNUNET_MessageHeader))
2414 LOG (GNUNET_ERROR_TYPE_WARNING,
2415 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2416 (unsigned int) size);
2417 /* _MAY_ be a connection failure (got partial message) */
2418 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2419 GNUNET_break_op (0);
2422 msg = (const struct GNUNET_MessageHeader *) buf;
2424 LOG (GNUNET_ERROR_TYPE_DEBUG,
2425 "UDP received %u-byte message from `%s' type %u\n",
2426 (unsigned int) size,
2427 GNUNET_a2s ((const struct sockaddr *) &addr, fromlen),
2430 if (size != ntohs (msg->size))
2432 GNUNET_break_op (0);
2436 GNUNET_STATISTICS_update (plugin->env->stats,
2437 "# UDP, total, bytes, received",
2440 switch (ntohs (msg->type))
2442 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2443 udp_broadcast_receive (plugin, buf, size,
2444 (const struct sockaddr *) &addr, fromlen);
2446 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2447 read_process_msg (plugin, msg,
2448 (const struct sockaddr *) &addr, fromlen);
2450 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2451 read_process_ack (plugin, msg,
2452 (const struct sockaddr *) &addr, fromlen);
2454 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2455 read_process_fragment (plugin, msg,
2456 (const struct sockaddr *) &addr, fromlen);
2459 GNUNET_break_op (0);
2465 static struct UDP_MessageWrapper *
2466 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2467 struct GNUNET_NETWORK_Handle *sock)
2469 struct UDP_MessageWrapper *udpw = NULL;
2470 struct GNUNET_TIME_Relative remaining;
2473 while (udpw != NULL)
2475 /* Find messages with timeout */
2476 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2477 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2479 /* Message timed out */
2480 switch (udpw->msg_type) {
2481 case MSG_UNFRAGMENTED:
2482 GNUNET_STATISTICS_update (plugin->env->stats,
2483 "# UDP, total, bytes, sent, timeout",
2484 udpw->msg_size, GNUNET_NO);
2485 GNUNET_STATISTICS_update (plugin->env->stats,
2486 "# UDP, total, messages, sent, timeout",
2488 GNUNET_STATISTICS_update (plugin->env->stats,
2489 "# UDP, unfragmented msgs, messages, sent, timeout",
2491 GNUNET_STATISTICS_update (plugin->env->stats,
2492 "# UDP, unfragmented msgs, bytes, sent, timeout",
2493 udpw->payload_size, GNUNET_NO);
2494 /* Not fragmented message */
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "Message for peer `%s' with size %u timed out\n",
2497 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2498 call_continuation (udpw, GNUNET_SYSERR);
2499 /* Remove message */
2500 dequeue (plugin, udpw);
2503 case MSG_FRAGMENTED:
2504 /* Fragmented message */
2505 GNUNET_STATISTICS_update (plugin->env->stats,
2506 "# UDP, total, bytes, sent, timeout",
2507 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2508 GNUNET_STATISTICS_update (plugin->env->stats,
2509 "# UDP, total, messages, sent, timeout",
2511 call_continuation (udpw, GNUNET_SYSERR);
2512 LOG (GNUNET_ERROR_TYPE_DEBUG,
2513 "Fragment for message for peer `%s' with size %u timed out\n",
2514 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2517 GNUNET_STATISTICS_update (plugin->env->stats,
2518 "# UDP, fragmented msgs, messages, sent, timeout",
2520 GNUNET_STATISTICS_update (plugin->env->stats,
2521 "# UDP, fragmented msgs, bytes, sent, timeout",
2522 udpw->frag_ctx->payload_size, GNUNET_NO);
2523 /* Remove fragmented message due to timeout */
2524 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2527 GNUNET_STATISTICS_update (plugin->env->stats,
2528 "# UDP, total, bytes, sent, timeout",
2529 udpw->msg_size, GNUNET_NO);
2530 GNUNET_STATISTICS_update (plugin->env->stats,
2531 "# UDP, total, messages, sent, timeout",
2533 LOG (GNUNET_ERROR_TYPE_DEBUG,
2534 "ACK Message for peer `%s' with size %u timed out\n",
2535 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2536 call_continuation (udpw, GNUNET_SYSERR);
2537 dequeue (plugin, udpw);
2543 if (sock == plugin->sockv4)
2544 udpw = plugin->ipv4_queue_head;
2545 else if (sock == plugin->sockv6)
2546 udpw = plugin->ipv6_queue_head;
2549 GNUNET_break (0); /* should never happen */
2552 GNUNET_STATISTICS_update (plugin->env->stats,
2553 "# messages dismissed due to timeout",
2558 /* Message did not time out, check flow delay */
2559 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2560 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2562 /* this message is not delayed */
2563 LOG (GNUNET_ERROR_TYPE_DEBUG,
2564 "Message for peer `%s' (%u bytes) is not delayed \n",
2565 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2566 break; /* Found message to send, break */
2570 /* Message is delayed, try next */
2571 LOG (GNUNET_ERROR_TYPE_DEBUG,
2572 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2573 GNUNET_i2s(&udpw->session->target), udpw->payload_size,
2574 GNUNET_STRINGS_relative_time_to_string (remaining,
2585 analyze_send_error (struct Plugin *plugin,
2586 const struct sockaddr * sa,
2590 static int network_down_error;
2591 struct GNUNET_ATS_Information type;
2593 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2594 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2595 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2597 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2599 /* IPv4: "Network unreachable" or "Network down"
2601 * This indicates we do not have connectivity
2603 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2604 _("UDP could not transmit message to `%s': "
2605 "Network seems down, please check your network configuration\n"),
2606 GNUNET_a2s (sa, slen));
2608 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2610 /* IPv6: "Network unreachable" or "Network down"
2612 * This indicates that this system is IPv6 enabled, but does not
2613 * have a valid global IPv6 address assigned or we do not have
2617 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2618 _("UDP could not transmit IPv6 message! "
2619 "Please check your network configuration and disable IPv6 if your "
2620 "connection does not have a global IPv6 address\n"));
2625 LOG (GNUNET_ERROR_TYPE_WARNING,
2626 "UDP could not transmit message to `%s': `%s'\n",
2627 GNUNET_a2s (sa, slen), STRERROR (error));
2633 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2635 const struct sockaddr * sa;
2639 struct UDP_MessageWrapper *udpw = NULL;
2641 /* Find message to send */
2642 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2645 return 0; /* No message to send */
2647 sa = udpw->session->sock_addr;
2648 slen = udpw->session->addrlen;
2650 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2652 if (GNUNET_SYSERR == sent)
2655 analyze_send_error (plugin, sa, slen, errno);
2656 call_continuation(udpw, GNUNET_SYSERR);
2657 GNUNET_STATISTICS_update (plugin->env->stats,
2658 "# UDP, total, bytes, sent, failure",
2660 GNUNET_STATISTICS_update (plugin->env->stats,
2661 "# UDP, total, messages, sent, failure",
2667 LOG (GNUNET_ERROR_TYPE_DEBUG,
2668 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2669 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2670 (sent < 0) ? STRERROR (errno) : "ok");
2671 GNUNET_STATISTICS_update (plugin->env->stats,
2672 "# UDP, total, bytes, sent, success",
2674 GNUNET_STATISTICS_update (plugin->env->stats,
2675 "# UDP, total, messages, sent, success",
2677 if (NULL != udpw->frag_ctx)
2678 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2679 call_continuation (udpw, GNUNET_OK);
2681 dequeue (plugin, udpw);
2690 * We have been notified that our readset has something to read. We don't
2691 * know which socket needs to be read, so we have to check each one
2692 * Then reschedule this function to be called again once more is available.
2694 * @param cls the plugin handle
2695 * @param tc the scheduling context (for rescheduling this function again)
2698 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2700 struct Plugin *plugin = cls;
2702 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2705 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2706 (NULL != plugin->sockv4) &&
2707 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2708 udp_select_read (plugin, plugin->sockv4);
2709 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2710 (NULL != plugin->sockv4) &&
2711 (NULL != plugin->ipv4_queue_head) &&
2712 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2713 udp_select_send (plugin, plugin->sockv4);
2714 schedule_select (plugin);
2719 * We have been notified that our readset has something to read. We don't
2720 * know which socket needs to be read, so we have to check each one
2721 * Then reschedule this function to be called again once more is available.
2723 * @param cls the plugin handle
2724 * @param tc the scheduling context (for rescheduling this function again)
2727 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2729 struct Plugin *plugin = cls;
2731 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2732 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2734 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2735 (NULL != plugin->sockv6) &&
2736 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2737 udp_select_read (plugin, plugin->sockv6);
2738 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2739 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2740 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2741 udp_select_send (plugin, plugin->sockv6);
2742 schedule_select (plugin);
2748 * @return number of sockets that were successfully bound
2751 setup_sockets (struct Plugin *plugin,
2752 const struct sockaddr_in6 *bind_v6,
2753 const struct sockaddr_in *bind_v4)
2756 int sockets_created = 0;
2757 struct sockaddr_in6 server_addrv6;
2758 struct sockaddr_in server_addrv4;
2759 struct sockaddr *server_addr;
2760 struct sockaddr *addrs[2];
2761 socklen_t addrlens[2];
2765 /* Create IPv6 socket */
2767 if (plugin->enable_ipv6 == GNUNET_YES)
2769 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2770 if (NULL == plugin->sockv6)
2772 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2773 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2774 plugin->enable_ipv6 = GNUNET_NO;
2778 memset (&server_addrv6, '\0', sizeof (struct sockaddr_in6));
2779 #if HAVE_SOCKADDR_IN_SIN_LEN
2780 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
2782 server_addrv6.sin6_family = AF_INET6;
2783 if (NULL != bind_v6)
2784 server_addrv6.sin6_addr = bind_v6->sin6_addr;
2786 server_addrv6.sin6_addr = in6addr_any;
2788 if (0 == plugin->port) /* autodetect */
2789 server_addrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2791 server_addrv6.sin6_port = htons (plugin->port);
2792 addrlen = sizeof (struct sockaddr_in6);
2793 server_addr = (struct sockaddr *) &server_addrv6;
2798 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2799 GNUNET_a2s (server_addr, addrlen));
2801 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2802 server_addr, addrlen))
2805 if (0 != plugin->port)
2807 tries = 10; /* fail */
2808 break; /* bind failed on specific port */
2811 server_addrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2816 GNUNET_NETWORK_socket_close (plugin->sockv6);
2817 plugin->enable_ipv6 = GNUNET_NO;
2818 plugin->sockv6 = NULL;
2821 if (plugin->sockv6 != NULL)
2823 LOG (GNUNET_ERROR_TYPE_DEBUG,
2824 "IPv6 socket created on port %s\n",
2825 GNUNET_a2s (server_addr, addrlen));
2826 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
2827 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2832 LOG (GNUNET_ERROR_TYPE_ERROR,
2833 "Failed to bind UDP socket to %s: %s\n",
2834 GNUNET_a2s (server_addr, addrlen),
2840 /* Create IPv4 socket */
2842 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2843 if (NULL == plugin->sockv4)
2845 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2846 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2847 plugin->enable_ipv4 = GNUNET_NO;
2851 memset (&server_addrv4, '\0', sizeof (struct sockaddr_in));
2852 #if HAVE_SOCKADDR_IN_SIN_LEN
2853 server_addrv4.sin_len = sizeof (struct sockaddr_in);
2855 server_addrv4.sin_family = AF_INET;
2856 if (NULL != bind_v4)
2857 server_addrv4.sin_addr = bind_v4->sin_addr;
2859 server_addrv4.sin_addr.s_addr = INADDR_ANY;
2861 if (0 == plugin->port)
2863 server_addrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2865 server_addrv4.sin_port = htons (plugin->port);
2868 addrlen = sizeof (struct sockaddr_in);
2869 server_addr = (struct sockaddr *) &server_addrv4;
2874 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2875 GNUNET_a2s (server_addr, addrlen));
2878 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2879 server_addr, addrlen))
2882 if (0 != plugin->port)
2884 tries = 10; /* fail */
2885 break; /* bind failed on specific port */
2889 server_addrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2895 GNUNET_NETWORK_socket_close (plugin->sockv4);
2896 plugin->enable_ipv4 = GNUNET_NO;
2897 plugin->sockv4 = NULL;
2900 if (plugin->sockv4 != NULL)
2902 LOG (GNUNET_ERROR_TYPE_DEBUG,
2903 "IPv4 socket created on port %s\n", GNUNET_a2s (server_addr, addrlen));
2904 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
2905 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2910 LOG (GNUNET_ERROR_TYPE_ERROR,
2911 "Failed to bind UDP socket to %s: %s\n",
2912 GNUNET_a2s (server_addr, addrlen), STRERROR (eno));
2916 if (0 == sockets_created)
2918 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2919 return 0; /* No sockets created, return */
2922 /* Create file descriptors */
2923 if (plugin->enable_ipv4 == GNUNET_YES)
2925 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2926 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2927 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2928 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2929 if (NULL != plugin->sockv4)
2931 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2932 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2936 if (plugin->enable_ipv6 == GNUNET_YES)
2938 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2939 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2940 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2941 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2942 if (NULL != plugin->sockv6)
2944 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2945 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2949 schedule_select (plugin);
2950 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2951 GNUNET_NO, plugin->port,
2953 (const struct sockaddr **) addrs, addrlens,
2954 &udp_nat_port_map_callback, NULL, plugin);
2956 return sockets_created;
2961 * The exported method. Makes the core api available via a global and
2962 * returns the udp transport API.
2964 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2965 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2968 libgnunet_plugin_transport_udp_init (void *cls)
2970 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2971 struct GNUNET_TRANSPORT_PluginFunctions *api;
2973 unsigned long long port;
2974 unsigned long long aport;
2975 unsigned long long broadcast;
2976 unsigned long long udp_max_bps;
2977 unsigned long long enable_v6;
2978 char * bind4_address;
2979 char * bind6_address;
2980 char * fancy_interval;
2981 struct GNUNET_TIME_Relative interval;
2982 struct sockaddr_in server_addrv4;
2983 struct sockaddr_in6 server_addrv6;
2988 if (NULL == env->receive)
2990 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2991 initialze the plugin or the API */
2992 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2994 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2995 api->address_to_string = &udp_address_to_string;
2996 api->string_to_address = &udp_string_to_address;
3000 GNUNET_assert (NULL != env->stats);
3002 /* Get port number: port == 0 : autodetect a port,
3003 * > 0 : use this port,
3004 * not given : 2086 default */
3006 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
3010 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3011 "ADVERTISED_PORT", &aport))
3015 LOG (GNUNET_ERROR_TYPE_WARNING,
3016 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3023 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3025 enable_v6 = GNUNET_NO;
3027 enable_v6 = GNUNET_YES;
3030 have_bind4 = GNUNET_NO;
3031 memset (&server_addrv4, 0, sizeof (server_addrv4));
3033 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3034 "BINDTO", &bind4_address))
3036 LOG (GNUNET_ERROR_TYPE_DEBUG,
3037 "Binding udp plugin to specific address: `%s'\n",
3039 if (1 != inet_pton (AF_INET, bind4_address, &server_addrv4.sin_addr))
3041 GNUNET_free (bind4_address);
3044 have_bind4 = GNUNET_YES;
3046 GNUNET_free_non_null (bind4_address);
3047 have_bind6 = GNUNET_NO;
3048 memset (&server_addrv6, 0, sizeof (server_addrv6));
3050 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3051 "BINDTO6", &bind6_address))
3053 LOG (GNUNET_ERROR_TYPE_DEBUG,
3054 "Binding udp plugin to specific address: `%s'\n",
3057 inet_pton (AF_INET6, bind6_address, &server_addrv6.sin6_addr))
3059 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3061 GNUNET_free (bind6_address);
3064 have_bind6 = GNUNET_YES;
3066 GNUNET_free_non_null (bind6_address);
3068 /* Initialize my flags */
3071 /* Enable neighbour discovery */
3072 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3074 if (broadcast == GNUNET_SYSERR)
3075 broadcast = GNUNET_NO;
3077 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3078 "BROADCAST_INTERVAL", &fancy_interval))
3080 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3084 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3086 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3088 GNUNET_free (fancy_interval);
3091 /* Maximum datarate */
3092 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3093 "MAX_BPS", &udp_max_bps))
3095 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3098 p = GNUNET_malloc (sizeof (struct Plugin));
3101 p->broadcast_interval = interval;
3102 p->enable_ipv6 = enable_v6;
3103 p->enable_ipv4 = GNUNET_YES; /* default */
3105 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3106 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3107 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3108 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3109 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps), 30);
3112 LOG(GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3113 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
3114 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL );
3115 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
3117 LOG (GNUNET_ERROR_TYPE_ERROR,
3118 _("Failed to create network sockets, plugin failed\n"));
3119 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3120 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3121 GNUNET_SERVER_mst_destroy (p->mst);
3125 else if (broadcast == GNUNET_YES)
3127 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3128 setup_broadcast (p, &server_addrv6, &server_addrv4);
3131 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3134 api->disconnect = &udp_disconnect;
3135 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3136 api->address_to_string = &udp_address_to_string;
3137 api->string_to_address = &udp_string_to_address;
3138 api->check_address = &udp_plugin_check_address;
3139 api->get_session = &udp_plugin_get_session;
3140 api->send = &udp_plugin_send;
3141 api->get_network = &udp_get_network;
3148 heap_cleanup_iterator (void *cls,
3149 struct GNUNET_CONTAINER_HeapNode *
3150 node, void *element,
3151 GNUNET_CONTAINER_HeapCostType
3154 struct DefragContext * d_ctx = element;
3156 GNUNET_CONTAINER_heap_remove_node (node);
3157 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3158 GNUNET_free (d_ctx);
3165 * The exported method. Makes the core api available via a global and
3166 * returns the udp transport API.
3168 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3172 libgnunet_plugin_transport_udp_done (void *cls)
3174 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3175 struct Plugin *plugin = api->cls;
3176 struct PrettyPrinterContext *cur;
3177 struct PrettyPrinterContext *next;
3185 stop_broadcast (plugin);
3186 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3188 GNUNET_SCHEDULER_cancel (plugin->select_task);
3189 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3191 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3193 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3194 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3197 /* Closing sockets */
3198 if (GNUNET_YES ==plugin->enable_ipv4)
3200 if (plugin->sockv4 != NULL)
3202 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3203 plugin->sockv4 = NULL;
3205 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3206 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3208 if (GNUNET_YES ==plugin->enable_ipv6)
3210 if (plugin->sockv6 != NULL)
3212 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3213 plugin->sockv6 = NULL;
3215 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3216 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3219 if (NULL != plugin->nat)
3220 GNUNET_NAT_unregister (plugin->nat);
3222 if (plugin->defrag_ctxs != NULL)
3224 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3225 heap_cleanup_iterator, NULL);
3226 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3227 plugin->defrag_ctxs = NULL;
3229 if (plugin->mst != NULL)
3231 GNUNET_SERVER_mst_destroy(plugin->mst);
3235 /* Clean up leftover messages */
3236 struct UDP_MessageWrapper * udpw;
3237 udpw = plugin->ipv4_queue_head;
3238 while (udpw != NULL)
3240 struct UDP_MessageWrapper *tmp = udpw->next;
3241 dequeue (plugin, udpw);
3242 call_continuation(udpw, GNUNET_SYSERR);
3247 udpw = plugin->ipv6_queue_head;
3248 while (udpw != NULL)
3250 struct UDP_MessageWrapper *tmp = udpw->next;
3251 dequeue (plugin, udpw);
3252 call_continuation(udpw, GNUNET_SYSERR);
3258 /* Clean up sessions */
3259 LOG (GNUNET_ERROR_TYPE_DEBUG,
3260 "Cleaning up sessions\n");
3261 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3262 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3264 next = ppc_dll_head;
3265 for (cur = next; NULL != cur; cur = next)
3268 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3269 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3270 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3276 GNUNET_free (plugin);
3279 struct Allocation *allocation;
3280 while (NULL != ahead)
3283 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3284 GNUNET_free (allocation);
3286 struct Allocator *allocator;
3287 while (NULL != aehead)
3290 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3291 GNUNET_free (allocator);
3298 /* end of plugin_transport_udp.c */