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)
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.
132 MSG_FRAGMENTED_COMPLETE = 2,
133 MSG_UNFRAGMENTED = 3,
141 * Which peer is this session for?
143 struct GNUNET_PeerIdentity target;
146 * Plugin this session belongs to.
148 struct Plugin *plugin;
151 * Context for dealing with fragments.
153 struct UDP_FragmentationContext *frag_ctx;
156 * Desired delay for next sending we send to other peer
158 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
161 * Desired delay for next sending we received from other peer
163 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
166 * Session timeout task
168 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
171 * expected delay for ACKs
173 struct GNUNET_TIME_Relative last_expected_ack_delay;
176 * desired delay between UDP messages
178 struct GNUNET_TIME_Relative last_expected_msg_delay;
180 struct GNUNET_ATS_Information ats;
182 struct GNUNET_HELLO_Address *address;
185 * Reference counter to indicate that this session is
186 * currently being used and must not be destroyed;
187 * setting @e in_destroy will destroy it as soon as
193 * Is this session about to be destroyed (sometimes we cannot
194 * destroy a session immediately as below us on the stack
195 * there might be code that still uses it; in this case,
196 * @e rc is non-zero).
202 * Closure for #session_cmp_it().
204 struct SessionCompareContext
207 const struct GNUNET_HELLO_Address *address;
211 * Closure for #process_inbound_tokenized_messages().
213 struct SourceInformation
218 struct GNUNET_PeerIdentity sender;
226 * Associated session.
228 struct Session *session;
231 * Number of bytes in source address.
238 * Closure for #find_receive_context().
240 struct FindReceiveContext
243 * Where to store the result.
245 struct DefragContext *rc;
250 const struct sockaddr *addr;
252 struct Session *session;
255 * Number of bytes in @e addr.
262 * Data structure to track defragmentation contexts based
263 * on the source of the UDP traffic.
269 * Defragmentation context.
271 struct GNUNET_DEFRAGMENT_Context *defrag;
274 * Source address this receive context is for (allocated at the
275 * end of the struct).
277 const struct sockaddr *src_addr;
280 * Reference to master plugin struct.
282 struct Plugin *plugin;
285 * Node in the defrag heap.
287 struct GNUNET_CONTAINER_HeapNode *hnode;
290 * Length of 'src_addr'
296 * Context to send fragmented messages
298 struct UDP_FragmentationContext
301 * Next in linked list
303 struct UDP_FragmentationContext *next;
306 * Previous in linked list
308 struct UDP_FragmentationContext *prev;
313 struct Plugin *plugin;
316 * Handle for GNUNET_FRAGMENT context
318 struct GNUNET_FRAGMENT_Context *frag;
321 * The session this fragmentation context belongs to
323 struct Session *session;
326 * Function to call upon completion of the transmission.
328 GNUNET_TRANSPORT_TransmitContinuation cont;
331 * Closure for @e cont.
338 struct GNUNET_TIME_Absolute timeout;
341 * Payload size of original unfragmented message
346 * Bytes used to send all fragments on wire including UDP overhead
350 unsigned int fragments_used;
354 struct UDP_MessageWrapper
357 * Session this message belongs to
359 struct Session *session;
365 struct UDP_MessageWrapper *prev;
371 struct UDP_MessageWrapper *next;
375 * According to UDP_MessageType
380 * Message with size msg_size including UDP specific overhead
385 * Size of UDP message to send including UDP specific overhead
390 * Payload size of original message
397 struct GNUNET_TIME_Absolute timeout;
400 * Function to call upon completion of the transmission.
402 GNUNET_TRANSPORT_TransmitContinuation cont;
405 * Closure for 'cont'.
410 * Fragmentation context
411 * frag_ctx == NULL if transport <= MTU
412 * frag_ctx != NULL if transport > MTU
414 struct UDP_FragmentationContext *frag_ctx;
418 * UDP ACK Message-Packet header (after defragmentation).
420 struct UDP_ACK_Message
425 struct GNUNET_MessageHeader header;
428 * Desired delay for flow control
433 * What is the identity of the sender
435 struct GNUNET_PeerIdentity sender;
442 static uint32_t myoptions;
445 * Encapsulation of all of the state of the plugin.
447 struct Plugin * plugin;
450 * We have been notified that our readset has something to read. We don't
451 * know which socket needs to be read, so we have to check each one
452 * Then reschedule this function to be called again once more is available.
454 * @param cls the plugin handle
455 * @param tc the scheduling context (for rescheduling this function again)
458 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
461 * We have been notified that our readset has something to read. We don't
462 * know which socket needs to be read, so we have to check each one
463 * Then reschedule this function to be called again once more is available.
465 * @param cls the plugin handle
466 * @param tc the scheduling context (for rescheduling this function again)
469 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
472 * (re)schedule select tasks for this plugin.
474 * @param plugin plugin to reschedule
477 schedule_select (struct Plugin *plugin)
479 struct GNUNET_TIME_Relative min_delay;
480 struct UDP_MessageWrapper *udpw;
482 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
484 /* Find a message ready to send:
485 * Flow delay from other peer is expired or not set (0) */
486 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
487 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
488 min_delay = GNUNET_TIME_relative_min (min_delay,
489 GNUNET_TIME_absolute_get_remaining (
490 udpw->session->flow_delay_from_other_peer));
492 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK )
493 GNUNET_SCHEDULER_cancel (plugin->select_task);
496 * - write active set if message is ready
497 * - timeout minimum delay */
498 plugin->select_task = GNUNET_SCHEDULER_add_select (
499 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
500 (0 == min_delay.rel_value_us) ?
501 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v4,
502 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
503 &udp_plugin_select, plugin);
505 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
507 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
508 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
509 min_delay = GNUNET_TIME_relative_min (min_delay,
510 GNUNET_TIME_absolute_get_remaining (
511 udpw->session->flow_delay_from_other_peer));
513 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
514 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
515 plugin->select_task_v6 = GNUNET_SCHEDULER_add_select (
516 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
517 (0 == min_delay.rel_value_us) ?
518 GNUNET_TIME_UNIT_FOREVER_REL : min_delay, plugin->rs_v6,
519 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
520 &udp_plugin_select_v6, plugin);
525 * Function called for a quick conversion of the binary address to
526 * a numeric address. Note that the caller must not free the
527 * address and that the next call to this function is allowed
528 * to override the address again.
531 * @param addr binary address
532 * @param addrlen length of the address
533 * @return string representing the same address
536 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
538 static char rbuf[INET6_ADDRSTRLEN + 10];
539 char buf[INET6_ADDRSTRLEN];
543 const struct IPv4UdpAddress *t4;
544 const struct IPv6UdpAddress *t6;
549 if ((NULL != addr) && (addrlen == sizeof(struct IPv6UdpAddress)))
553 options = ntohl (t6->options);
554 port = ntohs (t6->u6_port);
555 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
558 else if ((NULL != addr) && (addrlen == sizeof(struct IPv4UdpAddress)))
562 options = ntohl (t4->options);
563 port = ntohs (t4->u4_port);
564 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
571 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
573 GNUNET_snprintf (rbuf, sizeof(rbuf),
574 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
580 * Function called to convert a string address to
583 * @param cls closure ('struct Plugin*')
584 * @param addr string address
585 * @param addrlen length of the address
586 * @param buf location to store the buffer
587 * @param added location to store the number of bytes in the buffer.
588 * If the function returns GNUNET_SYSERR, its contents are undefined.
589 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
592 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
593 void **buf, size_t *added)
595 struct sockaddr_storage socket_address;
601 /* Format tcp.options.address:port */
606 if ((NULL == addr) || (addrlen == 0))
609 return GNUNET_SYSERR;
611 if ('\0' != addr[addrlen - 1])
614 return GNUNET_SYSERR;
616 if (strlen (addr) != addrlen - 1)
619 return GNUNET_SYSERR;
621 plugin = GNUNET_strdup (addr);
622 optionstr = strchr (plugin, '.');
623 if (NULL == optionstr)
627 return GNUNET_SYSERR;
631 options = atol (optionstr);
632 address = strchr (optionstr, '.');
637 return GNUNET_SYSERR;
643 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
648 return GNUNET_SYSERR;
653 switch (socket_address.ss_family)
657 struct IPv4UdpAddress *u4;
658 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
659 u4 = GNUNET_new (struct IPv4UdpAddress);
660 u4->options = htonl (options);
661 u4->ipv4_addr = in4->sin_addr.s_addr;
662 u4->u4_port = in4->sin_port;
664 *added = sizeof(struct IPv4UdpAddress);
669 struct IPv6UdpAddress *u6;
670 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
671 u6 = GNUNET_new (struct IPv6UdpAddress);
672 u6->options = htonl (options);
673 u6->ipv6_addr = in6->sin6_addr;
674 u6->u6_port = in6->sin6_port;
676 *added = sizeof(struct IPv6UdpAddress);
681 return GNUNET_SYSERR;
686 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
688 struct PrettyPrinterContext *ppc = cls;
690 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
691 if (NULL != ppc->resolver_handle)
693 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
694 ppc->resolver_handle = NULL;
696 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
701 * Append our port and forward the result.
703 * @param cls a 'struct PrettyPrinterContext'
704 * @param hostname result from DNS resolver
707 append_port (void *cls, const char *hostname)
709 struct PrettyPrinterContext *ppc = cls;
710 struct PrettyPrinterContext *cur;
713 if (hostname == NULL )
715 ppc->asc (ppc->asc_cls, NULL );
716 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
717 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
718 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
719 ppc->resolver_handle = NULL;
723 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
730 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
734 if (GNUNET_YES == ppc->ipv6)
735 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname,
738 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname,
740 ppc->asc (ppc->asc_cls, ret);
745 * Convert the transports address to a nice, human-readable
749 * @param type name of the transport that generated the address
750 * @param addr one of the addresses of the host, NULL for the last address
751 * the specific address format depends on the transport
752 * @param addrlen length of the address
753 * @param numeric should (IP) addresses be displayed in numeric form?
754 * @param timeout after how long should we give up?
755 * @param asc function to call on each string
756 * @param asc_cls closure for @a asc
759 udp_plugin_address_pretty_printer (void *cls, const char *type,
760 const void *addr, size_t addrlen, int numeric,
761 struct GNUNET_TIME_Relative timeout,
762 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
764 struct PrettyPrinterContext *ppc;
767 struct sockaddr_in a4;
768 struct sockaddr_in6 a6;
769 const struct IPv4UdpAddress *u4;
770 const struct IPv6UdpAddress *u6;
774 if (addrlen == sizeof(struct IPv6UdpAddress))
777 memset (&a6, 0, sizeof(a6));
778 a6.sin6_family = AF_INET6;
779 #if HAVE_SOCKADDR_IN_SIN_LEN
780 a6.sin6_len = sizeof (a6);
782 a6.sin6_port = u6->u6_port;
783 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
784 port = ntohs (u6->u6_port);
785 options = ntohl (u6->options);
789 else if (addrlen == sizeof(struct IPv4UdpAddress))
792 memset (&a4, 0, sizeof(a4));
793 a4.sin_family = AF_INET;
794 #if HAVE_SOCKADDR_IN_SIN_LEN
795 a4.sin_len = sizeof (a4);
797 a4.sin_port = u4->u4_port;
798 a4.sin_addr.s_addr = u4->ipv4_addr;
799 port = ntohs (u4->u4_port);
800 options = ntohl (u4->options);
806 /* invalid address */
808 asc (asc_cls, NULL );
811 ppc = GNUNET_new (struct PrettyPrinterContext);
813 ppc->asc_cls = asc_cls;
815 ppc->options = options;
816 if (addrlen == sizeof(struct IPv6UdpAddress))
817 ppc->ipv6 = GNUNET_YES;
819 ppc->ipv6 = GNUNET_NO;
820 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (
821 GNUNET_TIME_relative_multiply (timeout, 2), &ppc_cancel_task, ppc);
822 GNUNET_CONTAINER_DLL_insert(ppc_dll_head, ppc_dll_tail, ppc);
823 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
824 timeout, &append_port, ppc);
828 call_continuation (struct UDP_MessageWrapper *udpw, int result)
832 LOG(GNUNET_ERROR_TYPE_DEBUG,
833 "Calling continuation for %u byte message to `%s' with result %s\n",
834 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
835 (GNUNET_OK == result) ? "OK" : "SYSERR");
837 if (udpw->msg_size >= udpw->payload_size)
838 overhead = udpw->msg_size - udpw->payload_size;
840 overhead = udpw->msg_size;
845 switch (udpw->msg_type)
847 case MSG_UNFRAGMENTED:
848 if (NULL != udpw->cont)
850 /* Transport continuation */
851 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
852 udpw->payload_size, udpw->msg_size);
854 GNUNET_STATISTICS_update (plugin->env->stats,
855 "# UDP, unfragmented msgs, messages, sent, success", 1, GNUNET_NO);
856 GNUNET_STATISTICS_update (plugin->env->stats,
857 "# UDP, unfragmented msgs, bytes payload, sent, success",
858 udpw->payload_size, GNUNET_NO);
859 GNUNET_STATISTICS_update (plugin->env->stats,
860 "# UDP, unfragmented msgs, bytes overhead, sent, success", overhead,
862 GNUNET_STATISTICS_update (plugin->env->stats,
863 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
864 GNUNET_STATISTICS_update (plugin->env->stats,
865 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
867 case MSG_FRAGMENTED_COMPLETE:
868 GNUNET_assert(NULL != udpw->frag_ctx);
869 if (udpw->frag_ctx->cont != NULL )
870 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
871 GNUNET_OK, udpw->frag_ctx->payload_size,
872 udpw->frag_ctx->on_wire_size);
873 GNUNET_STATISTICS_update (plugin->env->stats,
874 "# UDP, fragmented msgs, messages, sent, success", 1, GNUNET_NO);
875 GNUNET_STATISTICS_update (plugin->env->stats,
876 "# UDP, fragmented msgs, bytes payload, sent, success",
877 udpw->payload_size, GNUNET_NO);
878 GNUNET_STATISTICS_update (plugin->env->stats,
879 "# UDP, fragmented msgs, bytes overhead, sent, success", overhead,
881 GNUNET_STATISTICS_update (plugin->env->stats,
882 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
883 GNUNET_STATISTICS_update (plugin->env->stats,
884 "# UDP, total, bytes payload, sent", udpw->payload_size, GNUNET_NO);
885 GNUNET_STATISTICS_update (plugin->env->stats,
886 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
889 /* Fragmented message: enqueue next fragment */
890 if (NULL != udpw->cont)
891 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
892 udpw->payload_size, udpw->msg_size);
893 GNUNET_STATISTICS_update (plugin->env->stats,
894 "# UDP, fragmented msgs, fragments, sent, success", 1, GNUNET_NO);
895 GNUNET_STATISTICS_update (plugin->env->stats,
896 "# UDP, fragmented msgs, fragments bytes, sent, success",
897 udpw->msg_size, GNUNET_NO);
900 /* No continuation */
901 GNUNET_STATISTICS_update (plugin->env->stats,
902 "# UDP, ACK msgs, messages, sent, success", 1, GNUNET_NO);
903 GNUNET_STATISTICS_update (plugin->env->stats,
904 "# UDP, ACK msgs, bytes overhead, sent, success", overhead,
906 GNUNET_STATISTICS_update (plugin->env->stats,
907 "# UDP, total, bytes overhead, sent", overhead, GNUNET_NO);
913 LOG(GNUNET_ERROR_TYPE_ERROR, "ERROR: %u\n", udpw->msg_type);
919 switch (udpw->msg_type)
921 case MSG_UNFRAGMENTED:
922 /* Unfragmented message: failed to send */
923 if (NULL != udpw->cont)
924 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
925 udpw->payload_size, overhead);
926 GNUNET_STATISTICS_update (plugin->env->stats,
927 "# UDP, unfragmented msgs, messages, sent, failure", 1, GNUNET_NO);
928 GNUNET_STATISTICS_update (plugin->env->stats,
929 "# UDP, unfragmented msgs, bytes payload, sent, failure",
930 udpw->payload_size, GNUNET_NO);
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 "# UDP, unfragmented msgs, bytes overhead, sent, failure", overhead,
935 case MSG_FRAGMENTED_COMPLETE:
936 GNUNET_assert(NULL != udpw->frag_ctx);
937 if (udpw->frag_ctx->cont != NULL )
938 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target,
939 GNUNET_SYSERR, udpw->frag_ctx->payload_size,
940 udpw->frag_ctx->on_wire_size);
941 GNUNET_STATISTICS_update (plugin->env->stats,
942 "# UDP, fragmented msgs, messages, sent, failure", 1, GNUNET_NO);
943 GNUNET_STATISTICS_update (plugin->env->stats,
944 "# UDP, fragmented msgs, bytes payload, sent, failure",
945 udpw->payload_size, GNUNET_NO);
946 GNUNET_STATISTICS_update (plugin->env->stats,
947 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
949 GNUNET_STATISTICS_update (plugin->env->stats,
950 "# UDP, fragmented msgs, bytes payload, sent, failure", overhead,
952 GNUNET_STATISTICS_update (plugin->env->stats,
953 "# UDP, fragmented msgs, messages, pending", -1, GNUNET_NO);
956 GNUNET_assert(NULL != udpw->frag_ctx);
957 /* Fragmented message: failed to send */
958 GNUNET_STATISTICS_update (plugin->env->stats,
959 "# UDP, fragmented msgs, fragments, sent, failure", 1, GNUNET_NO);
960 GNUNET_STATISTICS_update (plugin->env->stats,
961 "# UDP, fragmented msgs, fragments bytes, sent, failure",
962 udpw->msg_size, GNUNET_NO);
965 /* ACK message: failed to send */
966 GNUNET_STATISTICS_update (plugin->env->stats,
967 "# UDP, ACK msgs, messages, sent, failure", 1, GNUNET_NO);
970 /* Beacon message: failed to send */
985 * Check if the given port is plausible (must be either our listen
986 * port or our advertised port). If it is neither, we return
989 * @param plugin global variables
990 * @param in_port port number to check
991 * @return #GNUNET_OK if port is either open_port or adv_port
994 check_port (struct Plugin *plugin, uint16_t in_port)
996 if ((in_port == plugin->port) || (in_port == plugin->aport))
998 return GNUNET_SYSERR;
1002 * Function that will be called to check if a binary address for this
1003 * plugin is well-formed and corresponds to an address for THIS peer
1004 * (as per our configuration). Naturally, if absolutely necessary,
1005 * plugins can be a bit conservative in their answer, but in general
1006 * plugins should make sure that the address does not redirect
1007 * traffic to a 3rd party that might try to man-in-the-middle our
1010 * @param cls closure, should be our handle to the Plugin
1011 * @param addr pointer to the address
1012 * @param addrlen length of @a addr
1013 * @return #GNUNET_OK if this is a plausible address for this peer
1014 * and transport, #GNUNET_SYSERR if not
1018 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1020 struct Plugin *plugin = cls;
1021 struct IPv4UdpAddress *v4;
1022 struct IPv6UdpAddress *v6;
1024 if ((addrlen != sizeof(struct IPv4UdpAddress))
1025 && (addrlen != sizeof(struct IPv6UdpAddress)))
1028 return GNUNET_SYSERR;
1030 if (addrlen == sizeof(struct IPv4UdpAddress))
1032 v4 = (struct IPv4UdpAddress *) addr;
1033 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1034 return GNUNET_SYSERR;
1036 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1037 sizeof(struct in_addr)))
1038 return GNUNET_SYSERR;
1042 v6 = (struct IPv6UdpAddress *) addr;
1043 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1046 return GNUNET_SYSERR;
1048 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1049 return GNUNET_SYSERR;
1051 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1052 sizeof(struct in6_addr)))
1053 return GNUNET_SYSERR;
1059 * Function to free last resources associated with a session.
1061 * @param s session to free
1064 free_session (struct Session *s)
1066 if (NULL != s->frag_ctx)
1068 GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag, NULL, NULL );
1069 GNUNET_free(s->frag_ctx);
1076 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1078 if (plugin->bytes_in_buffer < udpw->msg_size)
1082 GNUNET_STATISTICS_update (plugin->env->stats,
1083 "# UDP, total, bytes in buffers", -(long long) udpw->msg_size,
1085 plugin->bytes_in_buffer -= udpw->msg_size;
1087 GNUNET_STATISTICS_update (plugin->env->stats, "# UDP, total, msgs in buffers",
1089 if (udpw->session->address->address_length == sizeof(struct sockaddr_in))
1090 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head,
1091 plugin->ipv4_queue_tail, udpw);
1092 if (udpw->session->address->address_length == sizeof(struct sockaddr_in6))
1093 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head,
1094 plugin->ipv6_queue_tail, udpw);
1098 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1100 struct UDP_MessageWrapper *udpw;
1101 struct UDP_MessageWrapper *tmp;
1102 struct UDP_MessageWrapper dummy;
1103 struct Session *s = fc->session;
1105 LOG(GNUNET_ERROR_TYPE_DEBUG,
1106 "%p : Fragmented message removed with result %s\n", fc,
1107 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1109 /* Call continuation for fragmented message */
1110 memset (&dummy, 0, sizeof(dummy));
1111 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1112 dummy.msg_size = s->frag_ctx->on_wire_size;
1113 dummy.payload_size = s->frag_ctx->payload_size;
1114 dummy.frag_ctx = s->frag_ctx;
1116 dummy.cont_cls = NULL;
1119 call_continuation (&dummy, result);
1121 /* Remove leftover fragments from queue */
1122 if (s->address->address_length == sizeof(struct sockaddr_in6))
1124 udpw = plugin->ipv6_queue_head;
1125 while (NULL != udpw)
1128 if ((udpw->frag_ctx != NULL )&& (udpw->frag_ctx == s->frag_ctx)){
1129 dequeue (plugin, udpw);
1130 call_continuation (udpw, GNUNET_SYSERR);
1136 if (s->address->address_length == sizeof(struct sockaddr_in))
1138 udpw = plugin->ipv4_queue_head;
1139 while (udpw != NULL )
1142 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1144 dequeue (plugin, udpw);
1145 call_continuation (udpw, GNUNET_SYSERR);
1152 /* Destroy fragmentation context */
1153 GNUNET_FRAGMENT_context_destroy (fc->frag, &s->last_expected_msg_delay,
1154 &s->last_expected_ack_delay);
1160 * Functions with this signature are called whenever we need
1161 * to close a session due to a disconnect or failure to
1162 * establish a connection.
1164 * @param cls closure with the `struct Plugin`
1165 * @param s session to close down
1166 * @return #GNUNET_OK on success
1169 udp_disconnect_session (void *cls, struct Session *s)
1171 struct Plugin *plugin = cls;
1172 struct UDP_MessageWrapper *udpw;
1173 struct UDP_MessageWrapper *next;
1175 GNUNET_assert(GNUNET_YES != s->in_destroy);
1176 LOG(GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' address ended\n", s,
1177 GNUNET_i2s (&s->target),
1178 udp_address_to_string (NULL, s->address->address, s->address->address_length));
1179 /* stop timeout task */
1180 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1182 GNUNET_SCHEDULER_cancel (s->timeout_task);
1183 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1185 if (NULL != s->frag_ctx)
1187 /* Remove fragmented message due to disconnect */
1188 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1191 next = plugin->ipv4_queue_head;
1192 while (NULL != (udpw = next))
1195 if (udpw->session == s)
1197 dequeue (plugin, udpw);
1198 call_continuation (udpw, GNUNET_SYSERR);
1202 next = plugin->ipv6_queue_head;
1203 while (NULL != (udpw = next))
1206 if (udpw->session == s)
1208 dequeue (plugin, udpw);
1209 call_continuation (udpw, GNUNET_SYSERR);
1213 plugin->env->session_end (plugin->env->cls, &s->target, s);
1215 if (NULL != s->frag_ctx)
1217 if (NULL != s->frag_ctx->cont)
1219 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1220 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1221 LOG(GNUNET_ERROR_TYPE_DEBUG,
1222 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1223 GNUNET_i2s (&s->target));
1228 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->sessions, &s->target, s));
1229 GNUNET_STATISTICS_set (plugin->env->stats, "# UDP, sessions active",
1230 GNUNET_CONTAINER_multipeermap_size (plugin->sessions), GNUNET_NO);
1232 s->in_destroy = GNUNET_YES;
1235 GNUNET_HELLO_address_free (s->address);
1242 * Function that is called to get the keepalive factor.
1243 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1244 * calculate the interval between keepalive packets.
1246 * @param cls closure with the `struct Plugin`
1247 * @return keepalive factor
1250 udp_query_keepalive_factor (void *cls)
1256 * Destroy a session, plugin is being unloaded.
1258 * @param cls the `struct Plugin`
1259 * @param key hash of public key of target peer
1260 * @param value a `struct PeerSession *` to clean up
1261 * @return #GNUNET_OK (continue to iterate)
1264 disconnect_and_free_it (void *cls, const struct GNUNET_PeerIdentity *key,
1267 struct Plugin *plugin = cls;
1269 udp_disconnect_session (plugin, value);
1274 * Disconnect from a remote node. Clean up session if we have one for
1277 * @param cls closure for this call (should be handle to Plugin)
1278 * @param target the peeridentity of the peer to disconnect
1279 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1282 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1284 struct Plugin *plugin = cls;
1286 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting from peer `%s'\n",
1287 GNUNET_i2s (target));
1288 /* Clean up sessions */
1289 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, target,
1290 &disconnect_and_free_it, plugin);
1294 * Session was idle, so disconnect it
1296 * @param cls the `struct Session` to time out
1297 * @param tc scheduler context
1300 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1302 struct Session *s = cls;
1304 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1305 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1306 "Session %p was idle for %s, disconnecting\n", s,
1307 GNUNET_STRINGS_relative_time_to_string (UDP_SESSION_TIME_OUT, GNUNET_YES));
1308 /* call session destroy function */
1309 udp_disconnect_session (s->plugin, s);
1313 * Increment session timeout due to activity
1315 * @param s session to reschedule timeout activity for
1318 reschedule_session_timeout (struct Session *s)
1320 if (GNUNET_YES == s->in_destroy)
1322 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1323 GNUNET_SCHEDULER_cancel (s->timeout_task);
1324 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1325 &session_timeout, s);
1326 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Timeout restarted for session %p\n", s);
1329 static struct Session *
1330 create_session (struct Plugin *plugin,
1331 const struct GNUNET_HELLO_Address *address)
1335 s = GNUNET_new (struct Session);
1336 s->address = GNUNET_HELLO_address_copy (address);
1337 s->target = address->peer;
1338 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (
1339 GNUNET_TIME_UNIT_MILLISECONDS, 250);
1340 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1341 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1342 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1343 s->timeout_task = GNUNET_SCHEDULER_add_delayed (UDP_SESSION_TIME_OUT,
1344 &session_timeout, s);
1349 session_cmp_it (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1351 struct SessionCompareContext * cctx = cls;
1352 const struct GNUNET_HELLO_Address *address = cctx->address;
1353 struct Session *s = value;
1355 LOG(GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1356 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1357 udp_address_to_string (NULL, s->address->address, s->address->address_length));
1359 if (0 == GNUNET_HELLO_address_cmp(s->address, cctx->address))
1368 * Function obtain the network type for a session
1370 * @param cls closure ('struct Plugin*')
1371 * @param session the session
1372 * @return the network type
1374 static enum GNUNET_ATS_Network_Type
1375 udp_get_network (void *cls, struct Session *session)
1377 return ntohl (session->ats.value);
1381 * Creates a new outbound session the transport service will use to
1382 * send data to the peer
1384 * @param cls the plugin
1385 * @param address the address
1386 * @param inbound look for inbound session
1387 * @return the session or NULL of max connections exceeded
1389 static struct Session *
1390 udp_plugin_lookup_session (void *cls,
1391 const struct GNUNET_HELLO_Address *address)
1393 struct Plugin * plugin = cls;
1394 struct IPv6UdpAddress * udp_a6;
1395 struct IPv4UdpAddress * udp_a4;
1397 GNUNET_assert(plugin != NULL);
1398 GNUNET_assert(address != NULL);
1400 if ((address->address == NULL )||
1401 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1402 (address->address_length != sizeof (struct IPv6UdpAddress)))){
1403 LOG (GNUNET_ERROR_TYPE_WARNING,
1404 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1405 address->address_length,
1406 sizeof (struct IPv4UdpAddress),
1407 sizeof (struct IPv6UdpAddress));
1411 if (address->address_length == sizeof(struct IPv4UdpAddress))
1413 if (plugin->sockv4 == NULL )
1415 udp_a4 = (struct IPv4UdpAddress *) address->address;
1416 if (udp_a4->u4_port == 0)
1420 if (address->address_length == sizeof(struct IPv6UdpAddress))
1422 if (plugin->sockv6 == NULL )
1424 udp_a6 = (struct IPv6UdpAddress *) address->address;
1425 if (udp_a6->u6_port == 0)
1429 /* check if session already exists */
1430 struct SessionCompareContext cctx;
1431 cctx.address = address;
1433 LOG(GNUNET_ERROR_TYPE_ERROR,
1434 "Looking for existing session for peer `%s' `%s' \n",
1435 GNUNET_i2s (&address->peer),
1436 udp_address_to_string(NULL, address->address, address->address_length));
1437 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, &address->peer,
1438 session_cmp_it, &cctx);
1439 if (cctx.res != NULL )
1441 LOG(GNUNET_ERROR_TYPE_ERROR, "Found existing session %p\n", cctx.res);
1448 udp_address_is_inbound (const struct GNUNET_HELLO_Address *address)
1450 if (GNUNET_HELLO_ADDRESS_INFO_INBOUND == (address->local_info & GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1456 static struct Session *
1457 udp_plugin_create_session (void *cls,
1458 const struct GNUNET_HELLO_Address *address)
1461 struct IPv4UdpAddress *udp_v4;
1462 struct IPv6UdpAddress *udp_v6;
1464 s = create_session (plugin, address);
1465 if (sizeof (struct IPv4UdpAddress) == address->address_length)
1467 struct sockaddr_in v4;
1468 udp_v4 = (struct IPv4UdpAddress *) address->address;
1469 memset (&v4, '\0', sizeof (v4));
1470 v4.sin_family = AF_INET;
1471 #if HAVE_SOCKADDR_IN_SIN_LEN
1472 v4->sin_len = sizeof (struct sockaddr_in);
1474 v4.sin_port = udp_v4->u4_port;
1475 v4.sin_addr.s_addr = udp_v4->ipv4_addr;
1476 s->ats = plugin->env->get_address_type (plugin->env->cls,
1477 (const struct sockaddr *) &v4, sizeof (v4));
1479 else if (sizeof (struct IPv6UdpAddress) == address->address_length)
1481 struct sockaddr_in6 v6;
1482 udp_v6 = (struct IPv6UdpAddress *) address->address;
1483 memset (&v6, '\0', sizeof (v6));
1484 v6.sin6_family = AF_INET6;
1485 #if HAVE_SOCKADDR_IN_SIN_LEN
1486 v6->sin_len = sizeof (struct sockaddr_in6);
1488 v6.sin6_port = udp_v6->u6_port;
1489 v6.sin6_addr = udp_v6->ipv6_addr;
1490 s->ats = plugin->env->get_address_type (plugin->env->cls,
1491 (const struct sockaddr *) &v6, sizeof (v6));
1495 return NULL ; /* protocol not supported or address invalid */
1496 LOG(GNUNET_ERROR_TYPE_ERROR,
1497 "Creating new %s session %p for peer `%s' address `%s'\n",
1498 (GNUNET_YES == udp_address_is_inbound(address)) ? "inbound" : "outbound",
1499 s, GNUNET_i2s (&address->peer),
1500 udp_address_to_string(NULL,address->address,address->address_length));
1502 GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->sessions, &s->target, s, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1503 GNUNET_STATISTICS_set (plugin->env->stats, "# UDP, sessions active",
1504 GNUNET_CONTAINER_multipeermap_size (plugin->sessions), GNUNET_NO);
1509 udp_plugin_update_session_timeout (void *cls,
1510 const struct GNUNET_PeerIdentity *peer, struct Session *session)
1513 != GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions, peer,
1520 /* Reschedule session timeout */
1521 reschedule_session_timeout (session);
1525 * Creates a new outbound session the transport service will use to send data to the
1528 * @param cls the plugin
1529 * @param address the address
1530 * @return the session or NULL of max connections exceeded
1532 static struct Session *
1533 udp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
1537 if (NULL == address)
1542 if ((address->address_length != sizeof(struct IPv4UdpAddress))
1543 && (address->address_length != sizeof(struct IPv6UdpAddress)))
1546 /* otherwise create new */
1547 if (NULL != (s = udp_plugin_lookup_session (cls, address)))
1549 return udp_plugin_create_session (cls, address);
1553 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1555 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1559 GNUNET_STATISTICS_update (plugin->env->stats,
1560 "# UDP, total, bytes in buffers", udpw->msg_size, GNUNET_NO);
1561 plugin->bytes_in_buffer += udpw->msg_size;
1563 GNUNET_STATISTICS_update (plugin->env->stats, "# UDP, total, msgs in buffers",
1565 if (udpw->session->address->address_length == sizeof(struct sockaddr_in))
1566 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head,
1567 plugin->ipv4_queue_tail, udpw);
1568 if (udpw->session->address->address_length == sizeof(struct sockaddr_in6))
1569 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head,
1570 plugin->ipv6_queue_tail, udpw);
1574 * Fragment message was transmitted via UDP, let fragmentation know
1575 * to send the next fragment now.
1577 * @param cls the 'struct UDPMessageWrapper' of the fragment
1578 * @param target destination peer (ignored)
1579 * @param result GNUNET_OK on success (ignored)
1580 * @param payload bytes payload sent
1581 * @param physical bytes physical sent
1584 send_next_fragment (void *cls, const struct GNUNET_PeerIdentity *target,
1585 int result, size_t payload, size_t physical)
1587 struct UDP_MessageWrapper *udpw = cls;
1589 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1593 * Function that is called with messages created by the fragmentation
1594 * module. In the case of the 'proc' callback of the
1595 * GNUNET_FRAGMENT_context_create function, this function must
1596 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1598 * @param cls closure, the 'struct FragmentationContext'
1599 * @param msg the message that was created
1602 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1604 struct UDP_FragmentationContext *frag_ctx = cls;
1605 struct Plugin *plugin = frag_ctx->plugin;
1606 struct UDP_MessageWrapper * udpw;
1607 size_t msg_len = ntohs (msg->size);
1609 LOG(GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes\n", msg_len);
1610 frag_ctx->fragments_used++;
1611 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1612 udpw->session = frag_ctx->session;
1613 udpw->msg_buf = (char *) &udpw[1];
1614 udpw->msg_size = msg_len;
1615 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1616 udpw->cont = &send_next_fragment;
1617 udpw->cont_cls = udpw;
1618 udpw->timeout = frag_ctx->timeout;
1619 udpw->frag_ctx = frag_ctx;
1620 udpw->msg_type = MSG_FRAGMENTED;
1621 memcpy (udpw->msg_buf, msg, msg_len);
1622 enqueue (plugin, udpw);
1623 schedule_select (plugin);
1627 * Function that can be used by the transport service to transmit
1628 * a message using the plugin. Note that in the case of a
1629 * peer disconnecting, the continuation MUST be called
1630 * prior to the disconnect notification itself. This function
1631 * will be called with this peer's HELLO message to initiate
1632 * a fresh connection to another peer.
1634 * @param cls closure
1635 * @param s which session must be used
1636 * @param msgbuf the message to transmit
1637 * @param msgbuf_size number of bytes in 'msgbuf'
1638 * @param priority how important is the message (most plugins will
1639 * ignore message priority and just FIFO)
1640 * @param to how long to wait at most for the transmission (does not
1641 * require plugins to discard the message after the timeout,
1642 * just advisory for the desired delay; most plugins will ignore
1644 * @param cont continuation to call once the message has
1645 * been transmitted (or if the transport is ready
1646 * for the next transmission call; or if the
1647 * peer disconnected...); can be NULL
1648 * @param cont_cls closure for cont
1649 * @return number of bytes used (on the physical network, with overheads);
1650 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1651 * and does NOT mean that the message was not transmitted (DV)
1654 udp_plugin_send (void *cls, struct Session *s, const char *msgbuf,
1655 size_t msgbuf_size, unsigned int priority, struct GNUNET_TIME_Relative to,
1656 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1658 struct Plugin *plugin = cls;
1659 size_t udpmlen = msgbuf_size + sizeof(struct UDPMessage);
1660 struct UDP_FragmentationContext * frag_ctx;
1661 struct UDP_MessageWrapper * udpw;
1662 struct UDPMessage *udp;
1664 GNUNET_assert(plugin != NULL);
1665 GNUNET_assert(s != NULL);
1667 if ((s->address->address_length == sizeof(struct sockaddr_in6)) && (plugin->sockv6 == NULL ))
1668 return GNUNET_SYSERR;
1669 if ((s->address->address_length == sizeof(struct sockaddr_in)) && (plugin->sockv4 == NULL ))
1670 return GNUNET_SYSERR;
1671 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1674 return GNUNET_SYSERR;
1677 != GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1681 return GNUNET_SYSERR;
1683 LOG(GNUNET_ERROR_TYPE_DEBUG,
1684 "UDP transmits %u-byte message to `%s' using address `%s'\n", udpmlen,
1685 GNUNET_i2s (&s->target), udp_address_to_string (NULL, s->address->address, s->address->address_length));
1688 udp = (struct UDPMessage *) mbuf;
1689 udp->header.size = htons (udpmlen);
1690 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1691 udp->reserved = htonl (0);
1692 udp->sender = *plugin->env->my_identity;
1694 /* We do not update the session time out here!
1695 * Otherwise this session will not timeout since we send keep alive before
1696 * session can timeout
1698 * For UDP we update session timeout only on receive, this will cover keep
1699 * alives, since remote peer will reply with keep alive response!
1701 if (udpmlen <= UDP_MTU)
1703 /* unfragmented message */
1704 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1706 udpw->msg_buf = (char *) &udpw[1];
1707 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1708 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1709 udpw->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), to);
1711 udpw->cont_cls = cont_cls;
1712 udpw->frag_ctx = NULL;
1713 udpw->msg_type = MSG_UNFRAGMENTED;
1714 memcpy (udpw->msg_buf, udp, sizeof(struct UDPMessage));
1715 memcpy (&udpw->msg_buf[sizeof(struct UDPMessage)], msgbuf, msgbuf_size);
1716 enqueue (plugin, udpw);
1718 GNUNET_STATISTICS_update (plugin->env->stats,
1719 "# UDP, unfragmented msgs, messages, attempt", 1, GNUNET_NO);
1720 GNUNET_STATISTICS_update (plugin->env->stats,
1721 "# UDP, unfragmented msgs, bytes payload, attempt", udpw->payload_size,
1726 /* fragmented message */
1727 if (s->frag_ctx != NULL )
1728 return GNUNET_SYSERR;
1729 memcpy (&udp[1], msgbuf, msgbuf_size);
1730 frag_ctx = GNUNET_new (struct UDP_FragmentationContext);
1731 frag_ctx->plugin = plugin;
1732 frag_ctx->session = s;
1733 frag_ctx->cont = cont;
1734 frag_ctx->cont_cls = cont_cls;
1735 frag_ctx->timeout = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1737 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1738 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1739 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1740 UDP_MTU, &plugin->tracker, s->last_expected_msg_delay,
1741 s->last_expected_ack_delay, &udp->header, &enqueue_fragment, frag_ctx);
1742 s->frag_ctx = frag_ctx;
1743 GNUNET_STATISTICS_update (plugin->env->stats,
1744 "# UDP, fragmented msgs, messages, pending", 1, GNUNET_NO);
1745 GNUNET_STATISTICS_update (plugin->env->stats,
1746 "# UDP, fragmented msgs, messages, attempt", 1, GNUNET_NO);
1747 GNUNET_STATISTICS_update (plugin->env->stats,
1748 "# UDP, fragmented msgs, bytes payload, attempt",
1749 frag_ctx->payload_size, GNUNET_NO);
1751 schedule_select (plugin);
1756 * Our external IP address/port mapping has changed.
1758 * @param cls closure, the 'struct LocalAddrList'
1759 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1760 * the previous (now invalid) one
1761 * @param address either the previous or the new public IP address
1762 * @param addrlen actual lenght of the address
1765 udp_nat_port_map_callback (void *cls, int add_remove,
1766 const struct sockaddr *addr, socklen_t addrlen)
1768 struct Plugin *plugin = cls;
1769 struct GNUNET_HELLO_Address *address;
1770 struct IPv4UdpAddress u4;
1771 struct IPv6UdpAddress u6;
1775 LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
1776 (GNUNET_YES == add_remove) ? "add" : "remove",
1777 GNUNET_a2s (addr, addrlen));
1779 /* convert 'address' to our internal format */
1780 switch (addr->sa_family)
1783 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
1784 memset (&u4, 0, sizeof(u4));
1785 u4.options = htonl (myoptions);
1786 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1787 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1788 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1791 args = sizeof(struct IPv4UdpAddress);
1794 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
1795 memset (&u4, 0, sizeof(u4));
1796 u6.options = htonl (myoptions);
1797 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1799 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1800 sizeof(struct in6_addr));
1801 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1803 args = sizeof(struct IPv6UdpAddress);
1809 /* modify our published address list */
1810 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1811 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
1812 plugin->env->notify_address (plugin->env->cls, add_remove, address);
1813 GNUNET_HELLO_address_free (address);
1817 * Message tokenizer has broken up an incomming message. Pass it on
1820 * @param cls the 'struct Plugin'
1821 * @param client the `struct SourceInformation`
1822 * @param hdr the actual message
1823 * @return #GNUNET_OK (always)
1826 process_inbound_tokenized_messages (void *cls, void *client,
1827 const struct GNUNET_MessageHeader *hdr)
1829 struct Plugin *plugin = cls;
1830 struct SourceInformation *si = client;
1831 struct GNUNET_TIME_Relative delay;
1833 GNUNET_assert(si->session != NULL);
1834 if (GNUNET_YES == si->session->in_destroy)
1837 GNUNET_break(ntohl (si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1838 reschedule_session_timeout (si->session);
1839 delay = plugin->env->receive (plugin->env->cls, si->session->address, si->session, hdr);
1840 plugin->env->update_address_metrics (plugin->env->cls,
1841 si->session->address, si->session,
1842 &si->session->ats, 1);
1843 si->session->flow_delay_for_other_peer = delay;
1848 * We've received a UDP Message. Process it (pass contents to main service).
1850 * @param plugin plugin context
1851 * @param msg the message
1852 * @param sender_addr sender address
1853 * @param sender_addr_len number of bytes in sender_addr
1856 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1857 const struct sockaddr *sender_addr, socklen_t sender_addr_len)
1859 struct SourceInformation si;
1861 struct GNUNET_HELLO_Address *address;
1862 struct IPv4UdpAddress u4;
1863 struct IPv6UdpAddress u6;
1867 if (0 != ntohl (msg->reserved))
1872 if (ntohs (msg->header.size)
1873 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct UDPMessage))
1879 /* convert address */
1880 switch (sender_addr->sa_family)
1883 GNUNET_assert(sender_addr_len == sizeof(struct sockaddr_in));
1884 memset (&u4, 0, sizeof(u4));
1885 u6.options = htonl (0);
1886 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1887 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1892 GNUNET_assert(sender_addr_len == sizeof(struct sockaddr_in6));
1893 memset (&u6, 0, sizeof(u6));
1894 u6.options = htonl (0);
1895 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1896 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1904 LOG(GNUNET_ERROR_TYPE_DEBUG,
1905 "Received message with %u bytes from peer `%s' at `%s'\n",
1906 (unsigned int ) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1907 GNUNET_a2s (sender_addr, sender_addr_len));
1909 address = GNUNET_HELLO_address_allocate ( &msg->sender, PLUGIN_NAME,
1910 arg, args, GNUNET_HELLO_ADDRESS_INFO_INBOUND);
1911 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
1913 s = udp_plugin_create_session (plugin, address);
1914 plugin->env->session_start (NULL, address, s, NULL, 0);
1916 GNUNET_free(address);
1918 /* iterate over all embedded messages */
1920 si.sender = msg->sender;
1924 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1925 ntohs (msg->header.size) - sizeof(struct UDPMessage), GNUNET_YES,
1928 if ((0 == s->rc) && (GNUNET_YES == s->in_destroy))
1933 * Scan the heap for a receive context with the given address.
1935 * @param cls the `struct FindReceiveContext`
1936 * @param node internal node of the heap
1937 * @param element value stored at the node (a 'struct ReceiveContext')
1938 * @param cost cost associated with the node
1939 * @return #GNUNET_YES if we should continue to iterate,
1940 * #GNUNET_NO if not.
1943 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1944 void *element, GNUNET_CONTAINER_HeapCostType cost)
1946 struct FindReceiveContext *frc = cls;
1947 struct DefragContext *e = element;
1949 if ((frc->addr_len == e->addr_len)
1950 && (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1959 * Process a defragmented message.
1961 * @param cls the 'struct ReceiveContext'
1962 * @param msg the message
1965 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1967 struct DefragContext *rc = cls;
1969 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1974 if (ntohs (msg->size) < sizeof(struct UDPMessage))
1979 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1980 rc->src_addr, rc->addr_len);
1983 struct LookupContext
1985 struct Session *res;
1987 const struct GNUNET_HELLO_Address *address;
1989 int must_have_frag_ctx;
1993 lookup_session_by_addr_it (void *cls, const struct GNUNET_PeerIdentity *key,
1996 struct LookupContext *l_ctx = cls;
1997 struct Session * s = value;
1999 if ((GNUNET_YES == l_ctx->must_have_frag_ctx) && (NULL == s->frag_ctx))
2002 if (0 == GNUNET_HELLO_address_cmp (s->address, l_ctx->address))
2011 * Transmit an acknowledgement.
2013 * @param cls the 'struct ReceiveContext'
2014 * @param id message ID (unused)
2015 * @param msg ack to transmit
2018 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2020 struct DefragContext *rc = cls;
2021 size_t msize = sizeof(struct UDP_ACK_Message) + ntohs (msg->size);
2022 struct UDP_ACK_Message *udp_ack;
2024 struct UDP_MessageWrapper *udpw;
2026 struct LookupContext l_ctx;
2028 l_ctx.address = GNUNET_HELLO_address_allocate (NULL, PLUGIN_NAME, rc->src_addr,
2029 rc->addr_len, GNUNET_HELLO_ADDRESS_INFO_NONE);
2031 l_ctx.must_have_frag_ctx = GNUNET_NO;
2032 GNUNET_CONTAINER_multipeermap_iterate (rc->plugin->sessions,
2033 &lookup_session_by_addr_it, &l_ctx);
2039 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2040 delay = s->flow_delay_for_other_peer.rel_value_us;
2042 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sending ACK to `%s' including delay of %s\n",
2043 GNUNET_a2s (rc->src_addr, (rc->src_addr->sa_family == AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)),
2044 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer, GNUNET_YES));
2045 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2046 udpw->msg_size = msize;
2047 udpw->payload_size = 0;
2049 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2050 udpw->msg_buf = (char *) &udpw[1];
2051 udpw->msg_type = MSG_ACK;
2052 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2053 udp_ack->header.size = htons ((uint16_t) msize);
2054 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2055 udp_ack->delay = htonl (delay);
2056 udp_ack->sender = *rc->plugin->env->my_identity;
2057 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2058 enqueue (rc->plugin, udpw);
2062 read_process_msg (struct Plugin *plugin, const struct GNUNET_MessageHeader *msg,
2063 const struct sockaddr *addr, socklen_t fromlen)
2065 if (ntohs (msg->size) < sizeof(struct UDPMessage))
2070 process_udp_message (plugin, (const struct UDPMessage *) msg, addr, fromlen);
2074 read_process_ack (struct Plugin *plugin, const struct GNUNET_MessageHeader *msg,
2075 const struct sockaddr *addr, socklen_t fromlen)
2077 const struct GNUNET_MessageHeader *ack;
2078 const struct UDP_ACK_Message *udp_ack;
2079 struct LookupContext l_ctx;
2081 struct GNUNET_TIME_Relative flow_delay;
2083 if (ntohs (msg->size)
2084 < sizeof(struct UDP_ACK_Message) + sizeof(struct GNUNET_MessageHeader))
2089 udp_ack = (const struct UDP_ACK_Message *) msg;
2091 l_ctx.address = GNUNET_HELLO_address_allocate (NULL, PLUGIN_NAME,
2092 (const struct sockaddr *) addr, fromlen, GNUNET_HELLO_ADDRESS_INFO_NONE);
2094 l_ctx.must_have_frag_ctx = GNUNET_YES;
2095 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2096 &lookup_session_by_addr_it, &l_ctx);
2098 if ((NULL == s) || (NULL == s->frag_ctx))
2103 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2104 LOG(GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %s\n",
2105 GNUNET_STRINGS_relative_time_to_string (flow_delay, GNUNET_YES));
2106 s->flow_delay_from_other_peer = GNUNET_TIME_relative_to_absolute (flow_delay);
2108 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2109 if (ntohs (ack->size) != ntohs (msg->size) - sizeof(struct UDP_ACK_Message))
2116 != memcmp (&l_ctx.res->target, &udp_ack->sender,
2117 sizeof(struct GNUNET_PeerIdentity)))
2119 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2121 LOG(GNUNET_ERROR_TYPE_DEBUG,
2122 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2123 (unsigned int ) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2124 GNUNET_a2s (addr, fromlen));
2125 /* Expect more ACKs to arrive */
2129 LOG(GNUNET_ERROR_TYPE_DEBUG, "Message full ACK'ed\n",
2130 (unsigned int ) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2131 GNUNET_a2s (addr, fromlen));
2133 /* Remove fragmented message after successful sending */
2134 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2138 read_process_fragment (struct Plugin *plugin,
2139 const struct GNUNET_MessageHeader *msg, const struct sockaddr *addr,
2142 struct DefragContext *d_ctx;
2143 struct GNUNET_TIME_Absolute now;
2144 struct FindReceiveContext frc;
2148 frc.addr_len = fromlen;
2150 LOG(GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2151 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2152 /* Lookup existing receive context for this address */
2153 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs, &find_receive_context,
2155 now = GNUNET_TIME_absolute_get ();
2160 /* Create a new defragmentation context */
2161 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2162 memcpy (&d_ctx[1], addr, fromlen);
2163 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2164 d_ctx->addr_len = fromlen;
2165 d_ctx->plugin = plugin;
2166 d_ctx->defrag = GNUNET_DEFRAGMENT_context_create (plugin->env->stats,
2167 UDP_MTU, UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx, &fragment_msg_proc,
2169 d_ctx->hnode = GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2170 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2171 LOG(GNUNET_ERROR_TYPE_DEBUG,
2172 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2173 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2177 LOG(GNUNET_ERROR_TYPE_DEBUG,
2178 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2179 (unsigned int ) ntohs (msg->size), GNUNET_a2s (addr, fromlen));
2182 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2184 /* keep this 'rc' from expiring */
2185 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2186 (GNUNET_CONTAINER_HeapCostType) now.abs_value_us);
2188 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2189 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2191 /* remove 'rc' that was inactive the longest */
2192 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2193 GNUNET_assert(NULL != d_ctx);
2194 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2200 * Read and process a message from the given socket.
2202 * @param plugin the overall plugin
2203 * @param rsock socket to read from
2206 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2209 struct sockaddr_storage addr;
2210 char buf[65536] GNUNET_ALIGN;
2212 const struct GNUNET_MessageHeader *msg;
2214 fromlen = sizeof(addr);
2215 memset (&addr, 0, sizeof(addr));
2216 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof(buf),
2217 (struct sockaddr *) &addr, &fromlen);
2219 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2220 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2221 * on this socket has failed.
2223 * WSAECONNRESET - The virtual circuit was reset by the remote side
2224 * executing a hard or abortive close. The application should close
2225 * the socket; it is no longer usable. On a UDP-datagram socket this
2226 * error indicates a previous send operation resulted in an ICMP Port
2227 * Unreachable message.
2229 if ( (-1 == size) && (ECONNRESET == errno) )
2234 LOG(GNUNET_ERROR_TYPE_DEBUG, "UDP failed to receive data: %s\n",
2236 /* Connection failure or something. Not a protocol violation. */
2239 if (size < sizeof(struct GNUNET_MessageHeader))
2241 LOG(GNUNET_ERROR_TYPE_WARNING,
2242 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2243 (unsigned int ) size);
2244 /* _MAY_ be a connection failure (got partial message) */
2245 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2249 msg = (const struct GNUNET_MessageHeader *) buf;
2251 LOG(GNUNET_ERROR_TYPE_DEBUG,
2252 "UDP received %u-byte message from `%s' type %u\n", (unsigned int ) size,
2253 GNUNET_a2s ((const struct sockaddr * ) &addr, fromlen),
2256 if (size != ntohs (msg->size))
2262 GNUNET_STATISTICS_update (plugin->env->stats, "# UDP, total, bytes, received",
2265 switch (ntohs (msg->type))
2267 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2268 if (GNUNET_YES == plugin->enable_broadcasting_receiving)
2269 udp_broadcast_receive (plugin, buf, size, (const struct sockaddr *) &addr,
2272 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2273 read_process_msg (plugin, msg, (const struct sockaddr *) &addr, fromlen);
2275 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2276 read_process_ack (plugin, msg, (const struct sockaddr *) &addr, fromlen);
2278 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2279 read_process_fragment (plugin, msg, (const struct sockaddr *) &addr,
2288 static struct UDP_MessageWrapper *
2289 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2290 struct GNUNET_NETWORK_Handle *sock)
2292 struct UDP_MessageWrapper *udpw = NULL;
2293 struct GNUNET_TIME_Relative remaining;
2296 while (udpw != NULL )
2298 /* Find messages with timeout */
2299 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2300 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2302 /* Message timed out */
2303 switch (udpw->msg_type)
2305 case MSG_UNFRAGMENTED:
2306 GNUNET_STATISTICS_update (plugin->env->stats,
2307 "# UDP, total, bytes, sent, timeout", udpw->msg_size, GNUNET_NO);
2308 GNUNET_STATISTICS_update (plugin->env->stats,
2309 "# UDP, total, messages, sent, timeout", 1, GNUNET_NO);
2310 GNUNET_STATISTICS_update (plugin->env->stats,
2311 "# UDP, unfragmented msgs, messages, sent, timeout", 1, GNUNET_NO);
2312 GNUNET_STATISTICS_update (plugin->env->stats,
2313 "# UDP, unfragmented msgs, bytes, sent, timeout",
2314 udpw->payload_size, GNUNET_NO);
2315 /* Not fragmented message */
2316 LOG(GNUNET_ERROR_TYPE_DEBUG,
2317 "Message for peer `%s' with size %u timed out\n",
2318 GNUNET_i2s (&udpw->session->target), udpw->payload_size);
2319 call_continuation (udpw, GNUNET_SYSERR);
2320 /* Remove message */
2321 dequeue (plugin, udpw);
2324 case MSG_FRAGMENTED:
2325 /* Fragmented message */
2326 GNUNET_STATISTICS_update (plugin->env->stats,
2327 "# UDP, total, bytes, sent, timeout", udpw->frag_ctx->on_wire_size,
2329 GNUNET_STATISTICS_update (plugin->env->stats,
2330 "# UDP, total, messages, sent, timeout", 1, GNUNET_NO);
2331 call_continuation (udpw, GNUNET_SYSERR);
2332 LOG(GNUNET_ERROR_TYPE_DEBUG,
2333 "Fragment for message for peer `%s' with size %u timed out\n",
2334 GNUNET_i2s (&udpw->session->target), udpw->frag_ctx->payload_size);
2336 GNUNET_STATISTICS_update (plugin->env->stats,
2337 "# UDP, fragmented msgs, messages, sent, timeout", 1, GNUNET_NO);
2338 GNUNET_STATISTICS_update (plugin->env->stats,
2339 "# UDP, fragmented msgs, bytes, sent, timeout",
2340 udpw->frag_ctx->payload_size, GNUNET_NO);
2341 /* Remove fragmented message due to timeout */
2342 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2345 GNUNET_STATISTICS_update (plugin->env->stats,
2346 "# UDP, total, bytes, sent, timeout", udpw->msg_size, GNUNET_NO);
2347 GNUNET_STATISTICS_update (plugin->env->stats,
2348 "# UDP, total, messages, sent, timeout", 1, GNUNET_NO);
2349 LOG(GNUNET_ERROR_TYPE_DEBUG,
2350 "ACK Message for peer `%s' with size %u timed out\n",
2351 GNUNET_i2s (&udpw->session->target), udpw->payload_size);
2352 call_continuation (udpw, GNUNET_SYSERR);
2353 dequeue (plugin, udpw);
2359 if (sock == plugin->sockv4)
2360 udpw = plugin->ipv4_queue_head;
2361 else if (sock == plugin->sockv6)
2362 udpw = plugin->ipv6_queue_head;
2365 GNUNET_break(0); /* should never happen */
2368 GNUNET_STATISTICS_update (plugin->env->stats,
2369 "# messages dismissed due to timeout", 1, GNUNET_NO);
2373 /* Message did not time out, check flow delay */
2374 remaining = GNUNET_TIME_absolute_get_remaining (
2375 udpw->session->flow_delay_from_other_peer);
2376 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2378 /* this message is not delayed */
2379 LOG(GNUNET_ERROR_TYPE_DEBUG,
2380 "Message for peer `%s' (%u bytes) is not delayed \n",
2381 GNUNET_i2s (&udpw->session->target), udpw->payload_size);
2382 break; /* Found message to send, break */
2386 /* Message is delayed, try next */
2387 LOG(GNUNET_ERROR_TYPE_DEBUG,
2388 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2389 GNUNET_i2s (&udpw->session->target), udpw->payload_size,
2390 GNUNET_STRINGS_relative_time_to_string (remaining, GNUNET_YES));
2399 analyze_send_error (struct Plugin *plugin, const struct sockaddr * sa,
2400 socklen_t slen, int error)
2402 static int network_down_error;
2403 struct GNUNET_ATS_Information type;
2405 type = plugin->env->get_address_type (plugin->env->cls, sa, slen);
2406 if (((GNUNET_ATS_NET_LAN == ntohl (type.value))
2407 || (GNUNET_ATS_NET_WAN == ntohl (type.value)))
2408 && ((ENETUNREACH == errno)|| (ENETDOWN == errno)))
2410 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2412 /* IPv4: "Network unreachable" or "Network down"
2414 * This indicates we do not have connectivity
2416 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2417 _("UDP could not transmit message to `%s': "
2418 "Network seems down, please check your network configuration\n"),
2419 GNUNET_a2s (sa, slen));
2421 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2423 /* IPv6: "Network unreachable" or "Network down"
2425 * This indicates that this system is IPv6 enabled, but does not
2426 * have a valid global IPv6 address assigned or we do not have
2430 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2431 _("UDP could not transmit IPv6 message! "
2432 "Please check your network configuration and disable IPv6 if your "
2433 "connection does not have a global IPv6 address\n"));
2438 LOG (GNUNET_ERROR_TYPE_WARNING,
2439 "UDP could not transmit message to `%s': `%s'\n",
2440 GNUNET_a2s (sa, slen), STRERROR (error));
2445 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2447 const struct sockaddr * sa;
2451 struct UDP_MessageWrapper *udpw = NULL;
2453 /* Find message to send */
2454 udpw = remove_timeout_messages_and_select (
2455 (sock == plugin->sockv4) ?
2456 plugin->ipv4_queue_head : plugin->ipv6_queue_head, sock);
2458 return 0; /* No message to send */
2460 sa = udpw->session->address->address;
2461 slen = udpw->session->address->address_length;
2463 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa,
2466 if (GNUNET_SYSERR == sent)
2469 analyze_send_error (plugin, sa, slen, errno);
2470 call_continuation (udpw, GNUNET_SYSERR);
2471 GNUNET_STATISTICS_update (plugin->env->stats,
2472 "# UDP, total, bytes, sent, failure", sent, GNUNET_NO);
2473 GNUNET_STATISTICS_update (plugin->env->stats,
2474 "# UDP, total, messages, sent, failure", 1, GNUNET_NO);
2479 LOG(GNUNET_ERROR_TYPE_DEBUG,
2480 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2481 (unsigned int ) (udpw->msg_size), GNUNET_i2s (&udpw->session->target),
2482 GNUNET_a2s (sa, slen), (int ) sent,
2483 (sent < 0) ? STRERROR (errno) : "ok");
2484 GNUNET_STATISTICS_update (plugin->env->stats,
2485 "# UDP, total, bytes, sent, success", sent, GNUNET_NO);
2486 GNUNET_STATISTICS_update (plugin->env->stats,
2487 "# UDP, total, messages, sent, success", 1, GNUNET_NO);
2488 if (NULL != udpw->frag_ctx)
2489 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2490 call_continuation (udpw, GNUNET_OK);
2492 dequeue (plugin, udpw);
2500 * We have been notified that our readset has something to read. We don't
2501 * know which socket needs to be read, so we have to check each one
2502 * Then reschedule this function to be called again once more is available.
2504 * @param cls the plugin handle
2505 * @param tc the scheduling context (for rescheduling this function again)
2508 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2510 struct Plugin *plugin = cls;
2512 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2513 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2515 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
2516 && (NULL != plugin->sockv4)
2517 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
2518 udp_select_read (plugin, plugin->sockv4);
2519 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
2520 && (NULL != plugin->sockv4) && (NULL != plugin->ipv4_queue_head)
2521 && (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
2522 udp_select_send (plugin, plugin->sockv4);
2523 schedule_select (plugin);
2527 * We have been notified that our readset has something to read. We don't
2528 * know which socket needs to be read, so we have to check each one
2529 * Then reschedule this function to be called again once more is available.
2531 * @param cls the plugin handle
2532 * @param tc the scheduling context (for rescheduling this function again)
2535 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2537 struct Plugin *plugin = cls;
2539 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2540 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2542 if (((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
2543 && (NULL != plugin->sockv6)
2544 && (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
2545 udp_select_read (plugin, plugin->sockv6);
2546 if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY))
2547 && (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL )&&
2548 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )udp_select_send (plugin, plugin->sockv6);
2549 schedule_select (plugin);
2554 * @return number of sockets that were successfully bound
2557 setup_sockets (struct Plugin *plugin, const struct sockaddr_in6 *bind_v6,
2558 const struct sockaddr_in *bind_v4)
2561 int sockets_created = 0;
2562 struct sockaddr_in6 server_addrv6;
2563 struct sockaddr_in server_addrv4;
2564 struct sockaddr *server_addr;
2565 struct sockaddr *addrs[2];
2566 socklen_t addrlens[2];
2570 /* Create IPv6 socket */
2572 if (plugin->enable_ipv6 == GNUNET_YES)
2574 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2575 if (NULL == plugin->sockv6)
2577 LOG(GNUNET_ERROR_TYPE_WARNING,
2578 "Disabling IPv6 since it is not supported on this system!\n");
2579 plugin->enable_ipv6 = GNUNET_NO;
2583 memset (&server_addrv6, '\0', sizeof(struct sockaddr_in6));
2584 #if HAVE_SOCKADDR_IN_SIN_LEN
2585 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
2587 server_addrv6.sin6_family = AF_INET6;
2588 if (NULL != bind_v6)
2589 server_addrv6.sin6_addr = bind_v6->sin6_addr;
2591 server_addrv6.sin6_addr = in6addr_any;
2593 if (0 == plugin->port) /* autodetect */
2594 server_addrv6.sin6_port = htons (
2595 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
2598 server_addrv6.sin6_port = htons (plugin->port);
2599 addrlen = sizeof(struct sockaddr_in6);
2600 server_addr = (struct sockaddr *) &server_addrv6;
2605 LOG(GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2606 GNUNET_a2s (server_addr, addrlen));
2609 == GNUNET_NETWORK_socket_bind (plugin->sockv6, server_addr,
2613 if (0 != plugin->port)
2615 tries = 10; /* fail */
2616 break; /* bind failed on specific port */
2619 server_addrv6.sin6_port = htons (
2620 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
2626 GNUNET_NETWORK_socket_close (plugin->sockv6);
2627 plugin->enable_ipv6 = GNUNET_NO;
2628 plugin->sockv6 = NULL;
2631 if (plugin->sockv6 != NULL )
2633 LOG(GNUNET_ERROR_TYPE_DEBUG, "IPv6 socket created on port %s\n",
2634 GNUNET_a2s (server_addr, addrlen));
2635 addrs[sockets_created] = (struct sockaddr *) &server_addrv6;
2636 addrlens[sockets_created] = sizeof(struct sockaddr_in6);
2641 LOG(GNUNET_ERROR_TYPE_ERROR, "Failed to bind UDP socket to %s: %s\n",
2642 GNUNET_a2s (server_addr, addrlen), STRERROR (eno));
2647 /* Create IPv4 socket */
2649 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2650 if (NULL == plugin->sockv4)
2652 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "socket");
2653 LOG(GNUNET_ERROR_TYPE_WARNING,
2654 "Disabling IPv4 since it is not supported on this system!\n");
2655 plugin->enable_ipv4 = GNUNET_NO;
2659 memset (&server_addrv4, '\0', sizeof(struct sockaddr_in));
2660 #if HAVE_SOCKADDR_IN_SIN_LEN
2661 server_addrv4.sin_len = sizeof (struct sockaddr_in);
2663 server_addrv4.sin_family = AF_INET;
2664 if (NULL != bind_v4)
2665 server_addrv4.sin_addr = bind_v4->sin_addr;
2667 server_addrv4.sin_addr.s_addr = INADDR_ANY;
2669 if (0 == plugin->port)
2671 server_addrv4.sin_port = htons (
2672 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
2675 server_addrv4.sin_port = htons (plugin->port);
2677 addrlen = sizeof(struct sockaddr_in);
2678 server_addr = (struct sockaddr *) &server_addrv4;
2683 LOG(GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2684 GNUNET_a2s (server_addr, addrlen));
2688 == GNUNET_NETWORK_socket_bind (plugin->sockv4, server_addr, addrlen))
2691 if (0 != plugin->port)
2693 tries = 10; /* fail */
2694 break; /* bind failed on specific port */
2698 server_addrv4.sin_port = htons (
2699 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537)
2706 GNUNET_NETWORK_socket_close (plugin->sockv4);
2707 plugin->enable_ipv4 = GNUNET_NO;
2708 plugin->sockv4 = NULL;
2711 if (plugin->sockv4 != NULL )
2713 LOG(GNUNET_ERROR_TYPE_DEBUG, "IPv4 socket created on port %s\n",
2714 GNUNET_a2s (server_addr, addrlen));
2715 addrs[sockets_created] = (struct sockaddr *) &server_addrv4;
2716 addrlens[sockets_created] = sizeof(struct sockaddr_in);
2721 LOG(GNUNET_ERROR_TYPE_ERROR, "Failed to bind UDP socket to %s: %s\n",
2722 GNUNET_a2s (server_addr, addrlen), STRERROR (eno));
2726 if (0 == sockets_created)
2728 LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2729 return 0; /* No sockets created, return */
2732 /* Create file descriptors */
2733 if (plugin->enable_ipv4 == GNUNET_YES)
2735 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2736 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2737 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2738 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2739 if (NULL != plugin->sockv4)
2741 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2742 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2746 if (plugin->enable_ipv6 == GNUNET_YES)
2748 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2749 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2750 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2751 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2752 if (NULL != plugin->sockv6)
2754 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2755 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2759 schedule_select (plugin);
2760 plugin->nat = GNUNET_NAT_register (plugin->env->cfg, GNUNET_NO, plugin->port,
2761 sockets_created, (const struct sockaddr **) addrs, addrlens,
2762 &udp_nat_port_map_callback, NULL, plugin);
2764 return sockets_created;
2768 * The exported method. Makes the core api available via a global and
2769 * returns the udp transport API.
2771 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2772 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2775 libgnunet_plugin_transport_udp_init (void *cls)
2777 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2778 struct GNUNET_TRANSPORT_PluginFunctions *api;
2780 unsigned long long port;
2781 unsigned long long aport;
2782 unsigned long long udp_max_bps;
2783 unsigned long long enable_v6;
2784 unsigned long long enable_broadcasting;
2785 unsigned long long enable_broadcasting_recv;
2786 char * bind4_address;
2787 char * bind6_address;
2788 char * fancy_interval;
2789 struct GNUNET_TIME_Relative interval;
2790 struct sockaddr_in server_addrv4;
2791 struct sockaddr_in6 server_addrv6;
2796 if (NULL == env->receive)
2798 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2799 initialze the plugin or the API */
2800 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2802 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2803 api->address_to_string = &udp_address_to_string;
2804 api->string_to_address = &udp_string_to_address;
2808 GNUNET_assert(NULL != env->stats);
2810 /* Get port number: port == 0 : autodetect a port,
2811 * > 0 : use this port, not given : 2086 default */
2813 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2817 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2818 "ADVERTISED_PORT", &aport))
2822 LOG(GNUNET_ERROR_TYPE_WARNING,
2823 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2830 == GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat", "DISABLEV6")))
2831 enable_v6 = GNUNET_NO;
2833 enable_v6 = GNUNET_YES;
2836 have_bind4 = GNUNET_NO;
2837 memset (&server_addrv4, 0, sizeof(server_addrv4));
2839 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2840 "BINDTO", &bind4_address))
2842 LOG(GNUNET_ERROR_TYPE_DEBUG,
2843 "Binding udp plugin to specific address: `%s'\n", bind4_address);
2844 if (1 != inet_pton (AF_INET, bind4_address, &server_addrv4.sin_addr))
2846 GNUNET_free(bind4_address);
2849 have_bind4 = GNUNET_YES;
2851 GNUNET_free_non_null(bind4_address);
2852 have_bind6 = GNUNET_NO;
2853 memset (&server_addrv6, 0, sizeof(server_addrv6));
2855 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2856 "BINDTO6", &bind6_address))
2858 LOG(GNUNET_ERROR_TYPE_DEBUG,
2859 "Binding udp plugin to specific address: `%s'\n", bind6_address);
2860 if (1 != inet_pton (AF_INET6, bind6_address, &server_addrv6.sin6_addr))
2862 LOG(GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2864 GNUNET_free(bind6_address);
2867 have_bind6 = GNUNET_YES;
2869 GNUNET_free_non_null(bind6_address);
2871 /* Initialize my flags */
2874 /* Enable neighbour discovery */
2875 enable_broadcasting = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2876 "transport-udp", "BROADCAST");
2877 if (enable_broadcasting == GNUNET_SYSERR)
2878 enable_broadcasting = GNUNET_NO;
2880 enable_broadcasting_recv = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2881 "transport-udp", "BROADCAST_RECEIVE");
2882 if (enable_broadcasting_recv == GNUNET_SYSERR)
2883 enable_broadcasting_recv = GNUNET_YES;
2886 == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2887 "BROADCAST_INTERVAL", &fancy_interval))
2889 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2894 == GNUNET_STRINGS_fancy_time_to_relative (fancy_interval, &interval))
2896 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2898 GNUNET_free(fancy_interval);
2901 /* Maximum datarate */
2903 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2904 "MAX_BPS", &udp_max_bps))
2906 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2909 p = GNUNET_new (struct Plugin);
2912 p->broadcast_interval = interval;
2913 p->enable_ipv6 = enable_v6;
2914 p->enable_ipv4 = GNUNET_YES; /* default */
2915 p->enable_broadcasting = enable_broadcasting;
2916 p->enable_broadcasting_receiving = enable_broadcasting_recv;
2918 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
2919 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (
2920 GNUNET_CONTAINER_HEAP_ORDER_MIN);
2921 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2922 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2923 GNUNET_BANDWIDTH_value_init ((uint32_t) udp_max_bps), 30);
2926 LOG(GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2927 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
2928 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL );
2929 if ((res == 0) || ((p->sockv4 == NULL )&& (p->sockv6 == NULL)))
2931 LOG (GNUNET_ERROR_TYPE_ERROR,
2932 _("Failed to create network sockets, plugin failed\n"));
2933 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
2934 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
2935 GNUNET_SERVER_mst_destroy (p->mst);
2940 /* Setup broadcasting and receiving beacons */
2941 setup_broadcast (p, &server_addrv6, &server_addrv4);
2943 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2946 api->disconnect_session = &udp_disconnect_session;
2947 api->query_keepalive_factor = &udp_query_keepalive_factor;
2948 api->disconnect_peer = &udp_disconnect;
2949 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2950 api->address_to_string = &udp_address_to_string;
2951 api->string_to_address = &udp_string_to_address;
2952 api->check_address = &udp_plugin_check_address;
2953 api->get_session = &udp_plugin_get_session;
2954 api->send = &udp_plugin_send;
2955 api->get_network = &udp_get_network;
2956 api->update_session_timeout = &udp_plugin_update_session_timeout;
2961 heap_cleanup_iterator (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2962 void *element, GNUNET_CONTAINER_HeapCostType cost)
2964 struct DefragContext * d_ctx = element;
2966 GNUNET_CONTAINER_heap_remove_node (node);
2967 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2974 * The exported method. Makes the core api available via a global and
2975 * returns the udp transport API.
2977 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2981 libgnunet_plugin_transport_udp_done (void *cls)
2983 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2984 struct Plugin *plugin = api->cls;
2985 struct PrettyPrinterContext *cur;
2986 struct PrettyPrinterContext *next;
2994 stop_broadcast (plugin);
2995 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK )
2997 GNUNET_SCHEDULER_cancel (plugin->select_task);
2998 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3000 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK )
3002 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3003 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3006 /* Closing sockets */
3007 if (GNUNET_YES == plugin->enable_ipv4)
3009 if (NULL != plugin->sockv4)
3011 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3012 plugin->sockv4 = NULL;
3014 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3015 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3017 if (GNUNET_YES == plugin->enable_ipv6)
3019 if (NULL != plugin->sockv6)
3021 GNUNET_break(GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3022 plugin->sockv6 = NULL;
3024 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3025 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3028 if (NULL != plugin->nat)
3030 GNUNET_NAT_unregister (plugin->nat);
3033 if (NULL != plugin->defrag_ctxs)
3035 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs, heap_cleanup_iterator,
3037 GNUNET_CONTAINER_heap_destroy (plugin->defrag_ctxs);
3038 plugin->defrag_ctxs = NULL;
3040 if (plugin->mst != NULL )
3042 GNUNET_SERVER_mst_destroy (plugin->mst);
3046 /* Clean up leftover messages */
3047 struct UDP_MessageWrapper * udpw;
3048 udpw = plugin->ipv4_queue_head;
3049 while (udpw != NULL )
3051 struct UDP_MessageWrapper *tmp = udpw->next;
3052 dequeue (plugin, udpw);
3053 call_continuation (udpw, GNUNET_SYSERR);
3058 udpw = plugin->ipv6_queue_head;
3059 while (udpw != NULL )
3061 struct UDP_MessageWrapper *tmp = udpw->next;
3062 dequeue (plugin, udpw);
3063 call_continuation (udpw, GNUNET_SYSERR);
3069 /* Clean up sessions */
3070 LOG(GNUNET_ERROR_TYPE_DEBUG, "Cleaning up sessions\n");
3071 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
3072 &disconnect_and_free_it, plugin);
3073 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3075 next = ppc_dll_head;
3076 for (cur = next; NULL != cur; cur = next)
3079 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, cur);
3080 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3081 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3087 GNUNET_free(plugin);
3090 struct Allocation *allocation;
3091 while (NULL != ahead)
3094 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3095 GNUNET_free (allocation);
3097 struct Allocator *allocator;
3098 while (NULL != aehead)
3101 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3102 GNUNET_free (allocator);
3108 /* end of plugin_transport_udp.c */