2 This file is part of GNUnet
3 (C) 2010, 2011 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 * Closure for 'append_port'.
69 struct PrettyPrinterContext
72 * Function to call with the result.
74 GNUNET_TRANSPORT_AddressStringCallback asc;
82 * Port to add after the IP address.
103 MSG_FRAGMENTED_COMPLETE = 2,
104 MSG_UNFRAGMENTED = 3,
112 * Which peer is this session for?
114 struct GNUNET_PeerIdentity target;
116 struct UDP_FragmentationContext * frag_ctx;
119 * Address of the other peer
121 const struct sockaddr *sock_addr;
124 * Desired delay for next sending we send to other peer
126 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
129 * Desired delay for next sending we received from other peer
131 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
134 * Session timeout task
136 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
139 * expected delay for ACKs
141 struct GNUNET_TIME_Relative last_expected_ack_delay;
144 * desired delay between UDP messages
146 struct GNUNET_TIME_Relative last_expected_msg_delay;
148 struct GNUNET_ATS_Information ats;
161 struct SessionCompareContext
164 const struct GNUNET_HELLO_Address *addr;
169 * Closure for 'process_inbound_tokenized_messages'
171 struct SourceInformation
176 struct GNUNET_PeerIdentity sender;
183 struct Session *session;
185 * Number of bytes in source address.
193 * Closure for 'find_receive_context'.
195 struct FindReceiveContext
198 * Where to store the result.
200 struct DefragContext *rc;
205 const struct sockaddr *addr;
207 struct Session *session;
210 * Number of bytes in 'addr'.
219 * Data structure to track defragmentation contexts based
220 * on the source of the UDP traffic.
226 * Defragmentation context.
228 struct GNUNET_DEFRAGMENT_Context *defrag;
231 * Source address this receive context is for (allocated at the
232 * end of the struct).
234 const struct sockaddr *src_addr;
237 * Reference to master plugin struct.
239 struct Plugin *plugin;
242 * Node in the defrag heap.
244 struct GNUNET_CONTAINER_HeapNode *hnode;
247 * Length of 'src_addr'
255 * Context to send fragmented messages
257 struct UDP_FragmentationContext
260 * Next in linked list
262 struct UDP_FragmentationContext * next;
265 * Previous in linked list
267 struct UDP_FragmentationContext * prev;
272 struct Plugin * plugin;
275 * Handle for GNUNET_FRAGMENT context
277 struct GNUNET_FRAGMENT_Context * frag;
280 * The session this fragmentation context belongs to
282 struct Session * session;
285 * Function to call upon completion of the transmission.
287 GNUNET_TRANSPORT_TransmitContinuation cont;
290 * Closure for 'cont'.
297 struct GNUNET_TIME_Absolute timeout;
300 * Payload size of original unfragmented message
305 * Bytes used to send all fragments on wire including UDP overhead
309 unsigned int fragments_used;
314 struct UDP_MessageWrapper
317 * Session this message belongs to
319 struct Session *session;
325 struct UDP_MessageWrapper *prev;
331 struct UDP_MessageWrapper *next;
335 * According to UDP_MessageType
340 * Message with size msg_size including UDP specific overhead
345 * Size of UDP message to send including UDP specific overhead
350 * Payload size of original message
357 struct GNUNET_TIME_Absolute timeout;
360 * Function to call upon completion of the transmission.
362 GNUNET_TRANSPORT_TransmitContinuation cont;
365 * Closure for 'cont'.
370 * Fragmentation context
371 * frag_ctx == NULL if transport <= MTU
372 * frag_ctx != NULL if transport > MTU
374 struct UDP_FragmentationContext *frag_ctx;
379 * UDP ACK Message-Packet header (after defragmentation).
381 struct UDP_ACK_Message
386 struct GNUNET_MessageHeader header;
389 * Desired delay for flow control
394 * What is the identity of the sender
396 struct GNUNET_PeerIdentity sender;
403 static uint32_t myoptions;
407 * Encapsulation of all of the state of the plugin.
409 struct Plugin * plugin;
413 * We have been notified that our readset has something to read. We don't
414 * know which socket needs to be read, so we have to check each one
415 * Then reschedule this function to be called again once more is available.
417 * @param cls the plugin handle
418 * @param tc the scheduling context (for rescheduling this function again)
421 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
425 * We have been notified that our readset has something to read. We don't
426 * know which socket needs to be read, so we have to check each one
427 * Then reschedule this function to be called again once more is available.
429 * @param cls the plugin handle
430 * @param tc the scheduling context (for rescheduling this function again)
433 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
437 * Start session timeout
440 start_session_timeout (struct Session *s);
443 * Increment session timeout due to activity
446 reschedule_session_timeout (struct Session *s);
452 stop_session_timeout (struct Session *s);
455 * (re)schedule select tasks for this plugin.
457 * @param plugin plugin to reschedule
460 schedule_select (struct Plugin *plugin)
462 struct GNUNET_TIME_Relative min_delay;
463 struct UDP_MessageWrapper *udpw;
465 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
467 /* Find a message ready to send:
468 * Flow delay from other peer is expired or not set (0) */
469 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
470 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
471 min_delay = GNUNET_TIME_relative_min (min_delay,
472 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
474 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
475 GNUNET_SCHEDULER_cancel(plugin->select_task);
478 * - write active set if message is ready
479 * - timeout minimum delay */
480 plugin->select_task =
481 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
482 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
484 (0 == min_delay.rel_value) ? plugin->ws_v4 : NULL,
485 &udp_plugin_select, plugin);
487 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
489 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
490 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
491 min_delay = GNUNET_TIME_relative_min (min_delay,
492 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
494 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
495 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
496 plugin->select_task_v6 =
497 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
498 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
500 (0 == min_delay.rel_value) ? plugin->ws_v6 : NULL,
501 &udp_plugin_select_v6, plugin);
507 * Function called for a quick conversion of the binary address to
508 * a numeric address. Note that the caller must not free the
509 * address and that the next call to this function is allowed
510 * to override the address again.
513 * @param addr binary address
514 * @param addrlen length of the address
515 * @return string representing the same address
518 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
520 static char rbuf[INET6_ADDRSTRLEN + 10];
521 char buf[INET6_ADDRSTRLEN];
525 const struct IPv4UdpAddress *t4;
526 const struct IPv6UdpAddress *t6;
532 if (addrlen == sizeof (struct IPv6UdpAddress))
536 options = ntohl (t6->options);
537 port = ntohs (t6->u6_port);
538 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
541 else if (addrlen == sizeof (struct IPv4UdpAddress))
545 options = ntohl (t4->options);
546 port = ntohs (t4->u4_port);
547 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
550 else if (addrlen == 0)
552 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s", TRANSPORT_SESSION_INBOUND_STRING);
560 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
562 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
563 PLUGIN_NAME, options, buf, port);
569 * Function called to convert a string address to
572 * @param cls closure ('struct Plugin*')
573 * @param addr string address
574 * @param addrlen length of the address
575 * @param buf location to store the buffer
576 * @param added location to store the number of bytes in the buffer.
577 * If the function returns GNUNET_SYSERR, its contents are undefined.
578 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
581 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
582 void **buf, size_t *added)
584 struct sockaddr_storage socket_address;
590 /* Format tcp.options.address:port */
595 if ((NULL == addr) || (addrlen == 0))
598 return GNUNET_SYSERR;
600 if ('\0' != addr[addrlen - 1])
603 return GNUNET_SYSERR;
605 if (strlen (addr) != addrlen - 1)
608 return GNUNET_SYSERR;
610 plugin = GNUNET_strdup (addr);
611 optionstr = strchr (plugin, '.');
612 if (NULL == optionstr)
615 GNUNET_free (plugin);
616 return GNUNET_SYSERR;
620 options = atol (optionstr);
621 address = strchr (optionstr, '.');
625 GNUNET_free (plugin);
626 return GNUNET_SYSERR;
632 GNUNET_STRINGS_to_address_ip (address, strlen (address),
636 GNUNET_free (plugin);
637 return GNUNET_SYSERR;
640 GNUNET_free (plugin);
642 switch (socket_address.ss_family)
646 struct IPv4UdpAddress *u4;
647 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
648 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
649 u4->options = htonl (options);
650 u4->ipv4_addr = in4->sin_addr.s_addr;
651 u4->u4_port = in4->sin_port;
653 *added = sizeof (struct IPv4UdpAddress);
658 struct IPv6UdpAddress *u6;
659 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
660 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
661 u6->options = htonl (options);
662 u6->ipv6_addr = in6->sin6_addr;
663 u6->u6_port = in6->sin6_port;
665 *added = sizeof (struct IPv6UdpAddress);
670 return GNUNET_SYSERR;
676 * Append our port and forward the result.
678 * @param cls a 'struct PrettyPrinterContext'
679 * @param hostname result from DNS resolver
682 append_port (void *cls, const char *hostname)
684 struct PrettyPrinterContext *ppc = cls;
687 if (hostname == NULL)
689 ppc->asc (ppc->asc_cls, NULL);
693 if (GNUNET_YES == ppc->ipv6)
694 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
696 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
697 ppc->asc (ppc->asc_cls, ret);
703 * Convert the transports address to a nice, human-readable
707 * @param type name of the transport that generated the address
708 * @param addr one of the addresses of the host, NULL for the last address
709 * the specific address format depends on the transport
710 * @param addrlen length of the address
711 * @param numeric should (IP) addresses be displayed in numeric form?
712 * @param timeout after how long should we give up?
713 * @param asc function to call on each string
714 * @param asc_cls closure for asc
717 udp_plugin_address_pretty_printer (void *cls, const char *type,
718 const void *addr, size_t addrlen,
720 struct GNUNET_TIME_Relative timeout,
721 GNUNET_TRANSPORT_AddressStringCallback asc,
724 struct PrettyPrinterContext *ppc;
727 struct sockaddr_in a4;
728 struct sockaddr_in6 a6;
729 const struct IPv4UdpAddress *u4;
730 const struct IPv6UdpAddress *u6;
735 if (addrlen == sizeof (struct IPv6UdpAddress))
738 memset (&a6, 0, sizeof (a6));
739 a6.sin6_family = AF_INET6;
740 #if HAVE_SOCKADDR_IN_SIN_LEN
741 a6.sin6_len = sizeof (a6);
743 a6.sin6_port = u6->u6_port;
744 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
745 port = ntohs (u6->u6_port);
746 options = ntohl (u6->options);
750 else if (addrlen == sizeof (struct IPv4UdpAddress))
753 memset (&a4, 0, sizeof (a4));
754 a4.sin_family = AF_INET;
755 #if HAVE_SOCKADDR_IN_SIN_LEN
756 a4.sin_len = sizeof (a4);
758 a4.sin_port = u4->u4_port;
759 a4.sin_addr.s_addr = u4->ipv4_addr;
760 port = ntohs (u4->u4_port);
761 options = ntohl (u4->options);
765 else if (0 == addrlen)
767 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
773 /* invalid address */
778 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
780 ppc->asc_cls = asc_cls;
782 ppc->options = options;
783 if (addrlen == sizeof (struct IPv6UdpAddress))
784 ppc->ipv6 = GNUNET_YES;
786 ppc->ipv6 = GNUNET_NO;
787 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
792 call_continuation (struct UDP_MessageWrapper *udpw, int result)
796 LOG (GNUNET_ERROR_TYPE_DEBUG,
797 "Calling continuation for %u byte message to `%s' with result %s\n",
798 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
799 (GNUNET_OK == result) ? "OK" : "SYSERR");
801 if (udpw->msg_size >= udpw->payload_size)
802 overhead = udpw->msg_size - udpw->payload_size;
804 overhead = udpw->msg_size;
808 switch (udpw->msg_type) {
809 case MSG_UNFRAGMENTED:
810 if (NULL != udpw->cont)
812 /* Transport continuation */
813 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
814 udpw->payload_size, udpw->msg_size);
816 GNUNET_STATISTICS_update (plugin->env->stats,
817 "# UDP, unfragmented msgs, messages, sent, success",
819 GNUNET_STATISTICS_update (plugin->env->stats,
820 "# UDP, unfragmented msgs, bytes payload, sent, success",
821 udpw->payload_size, GNUNET_NO);
822 GNUNET_STATISTICS_update (plugin->env->stats,
823 "# UDP, unfragmented msgs, bytes overhead, sent, success",
824 overhead, GNUNET_NO);
825 GNUNET_STATISTICS_update (plugin->env->stats,
826 "# UDP, total, bytes overhead, sent",
827 overhead, GNUNET_NO);
828 GNUNET_STATISTICS_update (plugin->env->stats,
829 "# UDP, total, bytes payload, sent",
830 udpw->payload_size, GNUNET_NO);
832 case MSG_FRAGMENTED_COMPLETE:
833 GNUNET_assert (NULL != udpw->frag_ctx);
834 if (udpw->frag_ctx->cont != NULL)
835 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
836 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
837 GNUNET_STATISTICS_update (plugin->env->stats,
838 "# UDP, fragmented msgs, messages, sent, success",
840 GNUNET_STATISTICS_update (plugin->env->stats,
841 "# UDP, fragmented msgs, bytes payload, sent, success",
842 udpw->payload_size, GNUNET_NO);
843 GNUNET_STATISTICS_update (plugin->env->stats,
844 "# UDP, fragmented msgs, bytes overhead, sent, success",
845 overhead, GNUNET_NO);
846 GNUNET_STATISTICS_update (plugin->env->stats,
847 "# UDP, total, bytes overhead, sent",
848 overhead, GNUNET_NO);
849 GNUNET_STATISTICS_update (plugin->env->stats,
850 "# UDP, total, bytes payload, sent",
851 udpw->payload_size, GNUNET_NO);
852 GNUNET_STATISTICS_update (plugin->env->stats,
853 "# UDP, fragmented msgs, messages, pending",
857 /* Fragmented message: enqueue next fragment */
858 if (NULL != udpw->cont)
859 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
860 udpw->payload_size, udpw->msg_size);
861 GNUNET_STATISTICS_update (plugin->env->stats,
862 "# UDP, fragmented msgs, fragments, sent, success",
864 GNUNET_STATISTICS_update (plugin->env->stats,
865 "# UDP, fragmented msgs, fragments bytes, sent, success",
866 udpw->msg_size, GNUNET_NO);
869 /* No continuation */
870 GNUNET_STATISTICS_update (plugin->env->stats,
871 "# UDP, ACK msgs, messages, sent, success",
873 GNUNET_STATISTICS_update (plugin->env->stats,
874 "# UDP, ACK msgs, bytes overhead, sent, success",
875 overhead, GNUNET_NO);
876 GNUNET_STATISTICS_update (plugin->env->stats,
877 "# UDP, total, bytes overhead, sent",
878 overhead, GNUNET_NO);
884 LOG (GNUNET_ERROR_TYPE_ERROR,
885 "ERROR: %u\n", udpw->msg_type);
891 switch (udpw->msg_type) {
892 case MSG_UNFRAGMENTED:
893 /* Unfragmented message: failed to send */
894 if (NULL != udpw->cont)
895 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
896 udpw->payload_size, overhead);
897 GNUNET_STATISTICS_update (plugin->env->stats,
898 "# UDP, unfragmented msgs, messages, sent, failure",
900 GNUNET_STATISTICS_update (plugin->env->stats,
901 "# UDP, unfragmented msgs, bytes payload, sent, failure",
902 udpw->payload_size, GNUNET_NO);
903 GNUNET_STATISTICS_update (plugin->env->stats,
904 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
905 overhead, GNUNET_NO);
907 case MSG_FRAGMENTED_COMPLETE:
908 GNUNET_assert (NULL != udpw->frag_ctx);
909 if (udpw->frag_ctx->cont != NULL)
910 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
911 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
912 GNUNET_STATISTICS_update (plugin->env->stats,
913 "# UDP, fragmented msgs, messages, sent, failure",
915 GNUNET_STATISTICS_update (plugin->env->stats,
916 "# UDP, fragmented msgs, bytes payload, sent, failure",
917 udpw->payload_size, GNUNET_NO);
918 GNUNET_STATISTICS_update (plugin->env->stats,
919 "# UDP, fragmented msgs, bytes payload, sent, failure",
920 overhead, GNUNET_NO);
921 GNUNET_STATISTICS_update (plugin->env->stats,
922 "# UDP, fragmented msgs, bytes payload, sent, failure",
923 overhead, GNUNET_NO);
924 GNUNET_STATISTICS_update (plugin->env->stats,
925 "# UDP, fragmented msgs, messages, pending",
929 GNUNET_assert (NULL != udpw->frag_ctx);
930 /* Fragmented message: failed to send */
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 "# UDP, fragmented msgs, fragments, sent, failure",
934 GNUNET_STATISTICS_update (plugin->env->stats,
935 "# UDP, fragmented msgs, fragments bytes, sent, failure",
936 udpw->msg_size, GNUNET_NO);
939 /* ACK message: failed to send */
940 GNUNET_STATISTICS_update (plugin->env->stats,
941 "# UDP, ACK msgs, messages, sent, failure",
945 /* Beacon message: failed to send */
961 * Check if the given port is plausible (must be either our listen
962 * port or our advertised port). If it is neither, we return
965 * @param plugin global variables
966 * @param in_port port number to check
967 * @return GNUNET_OK if port is either open_port or adv_port
970 check_port (struct Plugin *plugin, uint16_t in_port)
972 if ((in_port == plugin->port) || (in_port == plugin->aport))
974 return GNUNET_SYSERR;
979 * Function that will be called to check if a binary address for this
980 * plugin is well-formed and corresponds to an address for THIS peer
981 * (as per our configuration). Naturally, if absolutely necessary,
982 * plugins can be a bit conservative in their answer, but in general
983 * plugins should make sure that the address does not redirect
984 * traffic to a 3rd party that might try to man-in-the-middle our
987 * @param cls closure, should be our handle to the Plugin
988 * @param addr pointer to the address
989 * @param addrlen length of addr
990 * @return GNUNET_OK if this is a plausible address for this peer
991 * and transport, GNUNET_SYSERR if not
995 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
997 struct Plugin *plugin = cls;
998 struct IPv4UdpAddress *v4;
999 struct IPv6UdpAddress *v6;
1001 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1002 (addrlen != sizeof (struct IPv6UdpAddress)))
1004 GNUNET_break_op (0);
1005 return GNUNET_SYSERR;
1007 if (addrlen == sizeof (struct IPv4UdpAddress))
1009 v4 = (struct IPv4UdpAddress *) addr;
1010 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1011 return GNUNET_SYSERR;
1013 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1014 sizeof (struct in_addr)))
1015 return GNUNET_SYSERR;
1019 v6 = (struct IPv6UdpAddress *) addr;
1020 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1022 GNUNET_break_op (0);
1023 return GNUNET_SYSERR;
1025 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1026 return GNUNET_SYSERR;
1028 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1029 sizeof (struct in6_addr)))
1030 return GNUNET_SYSERR;
1037 * Task to free resources associated with a session.
1039 * @param s session to free
1042 free_session (struct Session *s)
1044 if (NULL != s->frag_ctx)
1046 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
1047 GNUNET_free (s->frag_ctx);
1055 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1057 if (plugin->bytes_in_buffer < udpw->msg_size)
1061 GNUNET_STATISTICS_update (plugin->env->stats,
1062 "# UDP, total, bytes in buffers",
1063 - (long long) udpw->msg_size, GNUNET_NO);
1064 plugin->bytes_in_buffer -= udpw->msg_size;
1066 GNUNET_STATISTICS_update (plugin->env->stats,
1067 "# UDP, total, msgs in buffers",
1069 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1070 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1071 plugin->ipv4_queue_tail, udpw);
1072 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1073 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1074 plugin->ipv6_queue_tail, udpw);
1078 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1080 struct UDP_MessageWrapper *udpw;
1081 struct UDP_MessageWrapper *tmp;
1082 struct UDP_MessageWrapper dummy;
1083 struct Session *s = fc->session;
1085 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1087 /* Call continuation for fragmented message */
1088 memset (&dummy, 0, sizeof (dummy));
1089 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1090 dummy.msg_size = s->frag_ctx->on_wire_size;
1091 dummy.payload_size = s->frag_ctx->payload_size;
1092 dummy.frag_ctx = s->frag_ctx;
1094 dummy.cont_cls = NULL;
1097 call_continuation (&dummy, result);
1099 /* Remove leftover fragments from queue */
1100 if (s->addrlen == sizeof (struct sockaddr_in6))
1102 udpw = plugin->ipv6_queue_head;
1103 while (NULL != udpw)
1106 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1108 dequeue (plugin, udpw);
1109 call_continuation (udpw, GNUNET_SYSERR);
1115 if (s->addrlen == sizeof (struct sockaddr_in))
1117 udpw = plugin->ipv4_queue_head;
1121 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1123 dequeue (plugin, udpw);
1124 call_continuation (udpw, GNUNET_SYSERR);
1131 /* Destroy fragmentation context */
1132 GNUNET_FRAGMENT_context_destroy (fc->frag,
1133 &s->last_expected_msg_delay,
1134 &s->last_expected_ack_delay);
1140 * Functions with this signature are called whenever we need
1141 * to close a session due to a disconnect or failure to
1142 * establish a connection.
1144 * @param s session to close down
1147 disconnect_session (struct Session *s)
1149 struct UDP_MessageWrapper *udpw;
1150 struct UDP_MessageWrapper *next;
1152 GNUNET_assert (GNUNET_YES != s->in_destroy);
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "Session %p to peer `%s' address ended \n",
1156 GNUNET_i2s (&s->target),
1157 GNUNET_a2s (s->sock_addr, s->addrlen));
1158 stop_session_timeout (s);
1160 if (NULL != s->frag_ctx)
1162 /* Remove fragmented message due to disconnect */
1163 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1166 next = plugin->ipv4_queue_head;
1167 while (NULL != (udpw = next))
1170 if (udpw->session == s)
1172 dequeue (plugin, udpw);
1173 call_continuation(udpw, GNUNET_SYSERR);
1177 next = plugin->ipv6_queue_head;
1178 while (NULL != (udpw = next))
1181 if (udpw->session == s)
1183 dequeue (plugin, udpw);
1184 call_continuation(udpw, GNUNET_SYSERR);
1189 plugin->env->session_end (plugin->env->cls, &s->target, s);
1191 if (NULL != s->frag_ctx)
1193 if (NULL != s->frag_ctx->cont)
1195 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1196 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1199 GNUNET_i2s (&s->target));
1203 GNUNET_assert (GNUNET_YES ==
1204 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1205 &s->target.hashPubKey,
1207 GNUNET_STATISTICS_set(plugin->env->stats,
1208 "# UDP, sessions active",
1209 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1212 s->in_destroy = GNUNET_YES;
1218 * Destroy a session, plugin is being unloaded.
1221 * @param key hash of public key of target peer
1222 * @param value a 'struct PeerSession*' to clean up
1223 * @return GNUNET_OK (continue to iterate)
1226 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1228 disconnect_session(value);
1234 * Disconnect from a remote node. Clean up session if we have one for this peer
1236 * @param cls closure for this call (should be handle to Plugin)
1237 * @param target the peeridentity of the peer to disconnect
1238 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1241 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1243 struct Plugin *plugin = cls;
1244 GNUNET_assert (plugin != NULL);
1246 GNUNET_assert (target != NULL);
1247 LOG (GNUNET_ERROR_TYPE_DEBUG,
1248 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1249 /* Clean up sessions */
1250 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1255 * Session was idle, so disconnect it
1258 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1260 GNUNET_assert (NULL != cls);
1261 struct Session *s = cls;
1263 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Session %p was idle for %llu ms, disconnecting\n",
1266 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1267 /* call session destroy function */
1268 disconnect_session (s);
1273 * Start session timeout
1276 start_session_timeout (struct Session *s)
1278 GNUNET_assert (NULL != s);
1279 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1280 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "Timeout for session %p set to %llu ms\n",
1285 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1290 * Increment session timeout due to activity
1293 reschedule_session_timeout (struct Session *s)
1295 GNUNET_assert (NULL != s);
1296 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1298 GNUNET_SCHEDULER_cancel (s->timeout_task);
1299 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Timeout rescheduled for session %p set to %llu ms\n",
1304 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1312 stop_session_timeout (struct Session *s)
1314 GNUNET_assert (NULL != s);
1316 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1318 GNUNET_SCHEDULER_cancel (s->timeout_task);
1319 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Timeout stopped for session %p canceled\n",
1322 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1327 static struct Session *
1328 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1329 const void *addr, size_t addrlen,
1330 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1333 const struct IPv4UdpAddress *t4;
1334 const struct IPv6UdpAddress *t6;
1335 struct sockaddr_in *v4;
1336 struct sockaddr_in6 *v6;
1341 case sizeof (struct IPv4UdpAddress):
1342 if (NULL == plugin->sockv4)
1344 LOG (GNUNET_ERROR_TYPE_DEBUG,
1345 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1347 udp_address_to_string(NULL, addr, addrlen));
1351 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1352 len = sizeof (struct sockaddr_in);
1353 v4 = (struct sockaddr_in *) &s[1];
1354 v4->sin_family = AF_INET;
1355 #if HAVE_SOCKADDR_IN_SIN_LEN
1356 v4->sin_len = sizeof (struct sockaddr_in);
1358 v4->sin_port = t4->u4_port;
1359 v4->sin_addr.s_addr = t4->ipv4_addr;
1360 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1362 case sizeof (struct IPv6UdpAddress):
1363 if (NULL == plugin->sockv6)
1365 LOG (GNUNET_ERROR_TYPE_INFO,
1366 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1368 udp_address_to_string(NULL, addr, addrlen));
1373 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1374 len = sizeof (struct sockaddr_in6);
1375 v6 = (struct sockaddr_in6 *) &s[1];
1376 v6->sin6_family = AF_INET6;
1377 #if HAVE_SOCKADDR_IN_SIN_LEN
1378 v6->sin6_len = sizeof (struct sockaddr_in6);
1380 v6->sin6_port = t6->u6_port;
1381 v6->sin6_addr = t6->ipv6_addr;
1382 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1385 /* Must have a valid address to send to */
1386 GNUNET_break_op (0);
1390 s->target = *target;
1391 s->sock_addr = (const struct sockaddr *) &s[1];
1392 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1393 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1394 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1395 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1396 s->inbound = GNUNET_NO;
1397 start_session_timeout (s);
1403 session_cmp_it (void *cls,
1404 const struct GNUNET_HashCode * key,
1407 struct SessionCompareContext * cctx = cls;
1408 const struct GNUNET_HELLO_Address *address = cctx->addr;
1409 struct Session *s = value;
1411 socklen_t s_addrlen = s->addrlen;
1413 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1414 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1415 GNUNET_a2s (s->sock_addr, s->addrlen));
1416 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1417 (s_addrlen == sizeof (struct sockaddr_in)))
1419 struct IPv4UdpAddress * u4 = NULL;
1420 u4 = (struct IPv4UdpAddress *) address->address;
1421 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1422 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1423 (u4->u4_port == s4->sin_port))
1430 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1431 (s_addrlen == sizeof (struct sockaddr_in6)))
1433 struct IPv6UdpAddress * u6 = NULL;
1434 u6 = (struct IPv6UdpAddress *) address->address;
1435 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1436 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1437 (u6->u6_port == s6->sin6_port))
1447 * Function obtain the network type for a session
1449 * @param cls closure ('struct Plugin*')
1450 * @param session the session
1451 * @return the network type in HBO or GNUNET_SYSERR
1453 static enum GNUNET_ATS_Network_Type
1454 udp_get_network (void *cls, void *session)
1456 struct Session *s = (struct Session *) session;
1458 return ntohl(s->ats.value);
1462 * Creates a new outbound session the transport service will use to send data to the
1465 * @param cls the plugin
1466 * @param address the address
1467 * @return the session or NULL of max connections exceeded
1469 static struct Session *
1470 udp_plugin_lookup_session (void *cls,
1471 const struct GNUNET_HELLO_Address *address)
1473 struct Plugin * plugin = cls;
1474 struct IPv6UdpAddress * udp_a6;
1475 struct IPv4UdpAddress * udp_a4;
1477 GNUNET_assert (plugin != NULL);
1478 GNUNET_assert (address != NULL);
1481 if ((address->address == NULL) ||
1482 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1483 (address->address_length != sizeof (struct IPv6UdpAddress))))
1485 LOG (GNUNET_ERROR_TYPE_WARNING,
1486 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1487 address->address_length,
1488 sizeof (struct IPv4UdpAddress),
1489 sizeof (struct IPv6UdpAddress));
1493 if (address->address_length == sizeof (struct IPv4UdpAddress))
1495 if (plugin->sockv4 == NULL)
1497 udp_a4 = (struct IPv4UdpAddress *) address->address;
1498 if (udp_a4->u4_port == 0)
1502 if (address->address_length == sizeof (struct IPv6UdpAddress))
1504 if (plugin->sockv6 == NULL)
1506 udp_a6 = (struct IPv6UdpAddress *) address->address;
1507 if (udp_a6->u6_port == 0)
1511 /* check if session already exists */
1512 struct SessionCompareContext cctx;
1513 cctx.addr = address;
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "Looking for existing session for peer `%s' `%s' \n",
1517 GNUNET_i2s (&address->peer),
1518 udp_address_to_string(NULL, address->address, address->address_length));
1519 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1520 if (cctx.res != NULL)
1522 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1528 static struct Session *
1529 udp_plugin_create_session (void *cls,
1530 const struct GNUNET_HELLO_Address *address)
1532 struct Session * s = NULL;
1534 /* otherwise create new */
1535 s = create_session (plugin,
1538 address->address_length,
1541 return NULL; /* protocol not supported or address invalid */
1542 LOG (GNUNET_ERROR_TYPE_DEBUG,
1543 "Creating new session %p for peer `%s' address `%s'\n",
1545 GNUNET_i2s(&address->peer),
1546 udp_address_to_string(NULL,address->address,address->address_length));
1547 GNUNET_assert (GNUNET_OK ==
1548 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1549 &s->target.hashPubKey,
1551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1552 GNUNET_STATISTICS_set(plugin->env->stats,
1553 "# UDP, sessions active",
1554 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1562 * Creates a new outbound session the transport service will use to send data to the
1565 * @param cls the plugin
1566 * @param address the address
1567 * @return the session or NULL of max connections exceeded
1569 static struct Session *
1570 udp_plugin_get_session (void *cls,
1571 const struct GNUNET_HELLO_Address *address)
1573 struct Session * s = NULL;
1575 if (NULL == address)
1580 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1581 (address->address_length != sizeof (struct IPv6UdpAddress)))
1584 /* otherwise create new */
1585 if (NULL != (s = udp_plugin_lookup_session(cls, address)))
1588 return udp_plugin_create_session (cls, address);
1593 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1595 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1599 GNUNET_STATISTICS_update (plugin->env->stats,
1600 "# UDP, total, bytes in buffers",
1601 udpw->msg_size, GNUNET_NO);
1602 plugin->bytes_in_buffer += udpw->msg_size;
1604 GNUNET_STATISTICS_update (plugin->env->stats,
1605 "# UDP, total, msgs in buffers",
1607 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1608 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1609 plugin->ipv4_queue_tail, udpw);
1610 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1611 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1612 plugin->ipv6_queue_tail, udpw);
1618 * Fragment message was transmitted via UDP, let fragmentation know
1619 * to send the next fragment now.
1621 * @param cls the 'struct UDPMessageWrapper' of the fragment
1622 * @param target destination peer (ignored)
1623 * @param result GNUNET_OK on success (ignored)
1624 * @param payload bytes payload sent
1625 * @param physical bytes physical sent
1628 send_next_fragment (void *cls,
1629 const struct GNUNET_PeerIdentity *target,
1630 int result, size_t payload, size_t physical)
1632 struct UDP_MessageWrapper *udpw = cls;
1633 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1638 * Function that is called with messages created by the fragmentation
1639 * module. In the case of the 'proc' callback of the
1640 * GNUNET_FRAGMENT_context_create function, this function must
1641 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1643 * @param cls closure, the 'struct FragmentationContext'
1644 * @param msg the message that was created
1647 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1649 struct UDP_FragmentationContext *frag_ctx = cls;
1650 struct Plugin *plugin = frag_ctx->plugin;
1651 struct UDP_MessageWrapper * udpw;
1652 size_t msg_len = ntohs (msg->size);
1654 LOG (GNUNET_ERROR_TYPE_DEBUG,
1655 "Enqueuing fragment with %u bytes\n", msg_len);
1656 frag_ctx->fragments_used ++;
1657 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1658 udpw->session = frag_ctx->session;
1659 udpw->msg_buf = (char *) &udpw[1];
1660 udpw->msg_size = msg_len;
1661 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1662 udpw->cont = &send_next_fragment;
1663 udpw->cont_cls = udpw;
1664 udpw->timeout = frag_ctx->timeout;
1665 udpw->frag_ctx = frag_ctx;
1666 udpw->msg_type = MSG_FRAGMENTED;
1667 memcpy (udpw->msg_buf, msg, msg_len);
1668 enqueue (plugin, udpw);
1669 schedule_select (plugin);
1674 * Function that can be used by the transport service to transmit
1675 * a message using the plugin. Note that in the case of a
1676 * peer disconnecting, the continuation MUST be called
1677 * prior to the disconnect notification itself. This function
1678 * will be called with this peer's HELLO message to initiate
1679 * a fresh connection to another peer.
1681 * @param cls closure
1682 * @param s which session must be used
1683 * @param msgbuf the message to transmit
1684 * @param msgbuf_size number of bytes in 'msgbuf'
1685 * @param priority how important is the message (most plugins will
1686 * ignore message priority and just FIFO)
1687 * @param to how long to wait at most for the transmission (does not
1688 * require plugins to discard the message after the timeout,
1689 * just advisory for the desired delay; most plugins will ignore
1691 * @param cont continuation to call once the message has
1692 * been transmitted (or if the transport is ready
1693 * for the next transmission call; or if the
1694 * peer disconnected...); can be NULL
1695 * @param cont_cls closure for cont
1696 * @return number of bytes used (on the physical network, with overheads);
1697 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1698 * and does NOT mean that the message was not transmitted (DV)
1701 udp_plugin_send (void *cls,
1703 const char *msgbuf, size_t msgbuf_size,
1704 unsigned int priority,
1705 struct GNUNET_TIME_Relative to,
1706 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1708 struct Plugin *plugin = cls;
1709 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1710 struct UDP_FragmentationContext * frag_ctx;
1711 struct UDP_MessageWrapper * udpw;
1712 struct UDPMessage *udp;
1714 GNUNET_assert (plugin != NULL);
1715 GNUNET_assert (s != NULL);
1717 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1718 return GNUNET_SYSERR;
1719 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1720 return GNUNET_SYSERR;
1721 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1724 return GNUNET_SYSERR;
1726 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1729 return GNUNET_SYSERR;
1731 LOG (GNUNET_ERROR_TYPE_DEBUG,
1732 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1734 GNUNET_i2s (&s->target),
1735 GNUNET_a2s(s->sock_addr, s->addrlen));
1739 udp = (struct UDPMessage *) mbuf;
1740 udp->header.size = htons (udpmlen);
1741 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1742 udp->reserved = htonl (0);
1743 udp->sender = *plugin->env->my_identity;
1745 reschedule_session_timeout(s);
1746 if (udpmlen <= UDP_MTU)
1748 /* unfragmented message */
1749 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1751 udpw->msg_buf = (char *) &udpw[1];
1752 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1753 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1754 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1756 udpw->cont_cls = cont_cls;
1757 udpw->frag_ctx = NULL;
1758 udpw->msg_type = MSG_UNFRAGMENTED;
1759 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1760 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1761 enqueue (plugin, udpw);
1763 GNUNET_STATISTICS_update (plugin->env->stats,
1764 "# UDP, unfragmented msgs, messages, attempt",
1766 GNUNET_STATISTICS_update (plugin->env->stats,
1767 "# UDP, unfragmented msgs, bytes payload, attempt",
1768 udpw->payload_size, GNUNET_NO);
1772 /* fragmented message */
1773 if (s->frag_ctx != NULL)
1774 return GNUNET_SYSERR;
1775 memcpy (&udp[1], msgbuf, msgbuf_size);
1776 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1777 frag_ctx->plugin = plugin;
1778 frag_ctx->session = s;
1779 frag_ctx->cont = cont;
1780 frag_ctx->cont_cls = cont_cls;
1781 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1782 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1783 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1784 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1787 s->last_expected_msg_delay,
1788 s->last_expected_ack_delay,
1792 s->frag_ctx = frag_ctx;
1793 GNUNET_STATISTICS_update (plugin->env->stats,
1794 "# UDP, fragmented msgs, messages, pending",
1796 GNUNET_STATISTICS_update (plugin->env->stats,
1797 "# UDP, fragmented msgs, messages, attempt",
1799 GNUNET_STATISTICS_update (plugin->env->stats,
1800 "# UDP, fragmented msgs, bytes payload, attempt",
1801 frag_ctx->payload_size, GNUNET_NO);
1803 schedule_select (plugin);
1809 * Our external IP address/port mapping has changed.
1811 * @param cls closure, the 'struct LocalAddrList'
1812 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1813 * the previous (now invalid) one
1814 * @param addr either the previous or the new public IP address
1815 * @param addrlen actual lenght of the address
1818 udp_nat_port_map_callback (void *cls, int add_remove,
1819 const struct sockaddr *addr, socklen_t addrlen)
1821 struct Plugin *plugin = cls;
1822 struct IPv4UdpAddress u4;
1823 struct IPv6UdpAddress u6;
1827 LOG (GNUNET_ERROR_TYPE_INFO,
1828 "NAT notification to %s address `%s'\n",
1829 (GNUNET_YES == add_remove) ? "add" : "remove",
1830 GNUNET_a2s (addr, addrlen));
1832 /* convert 'addr' to our internal format */
1833 switch (addr->sa_family)
1836 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1837 memset (&u4, 0, sizeof (u4));
1838 u4.options = htonl(myoptions);
1839 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1840 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1842 args = sizeof (struct IPv4UdpAddress);
1845 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1846 memset (&u4, 0, sizeof (u4));
1847 u6.options = htonl(myoptions);
1848 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1849 sizeof (struct in6_addr));
1850 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1852 args = sizeof (struct IPv6UdpAddress);
1858 /* modify our published address list */
1859 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1865 * Message tokenizer has broken up an incomming message. Pass it on
1868 * @param cls the 'struct Plugin'
1869 * @param client the 'struct SourceInformation'
1870 * @param hdr the actual message
1873 process_inbound_tokenized_messages (void *cls, void *client,
1874 const struct GNUNET_MessageHeader *hdr)
1876 struct Plugin *plugin = cls;
1877 struct SourceInformation *si = client;
1878 struct GNUNET_TIME_Relative delay;
1880 GNUNET_assert (si->session != NULL);
1881 if (GNUNET_YES == si->session->in_destroy)
1884 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1885 delay = plugin->env->receive (plugin->env->cls,
1889 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1890 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1892 plugin->env->update_address_metrics (plugin->env->cls,
1894 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1895 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1897 &si->session->ats, 1);
1899 si->session->flow_delay_for_other_peer = delay;
1900 reschedule_session_timeout(si->session);
1906 * We've received a UDP Message. Process it (pass contents to main service).
1908 * @param plugin plugin context
1909 * @param msg the message
1910 * @param sender_addr sender address
1911 * @param sender_addr_len number of bytes in sender_addr
1914 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1915 const struct sockaddr *sender_addr,
1916 socklen_t sender_addr_len)
1918 struct SourceInformation si;
1920 struct IPv4UdpAddress u4;
1921 struct IPv6UdpAddress u6;
1925 if (0 != ntohl (msg->reserved))
1927 GNUNET_break_op (0);
1930 if (ntohs (msg->header.size) <
1931 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1933 GNUNET_break_op (0);
1937 /* convert address */
1938 switch (sender_addr->sa_family)
1941 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1942 memset (&u4, 0, sizeof (u4));
1943 u6.options = htonl (0);
1944 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1945 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1950 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1951 memset (&u6, 0, sizeof (u6));
1952 u6.options = htonl (0);
1953 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1954 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1962 LOG (GNUNET_ERROR_TYPE_DEBUG,
1963 "Received message with %u bytes from peer `%s' at `%s'\n",
1964 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1965 GNUNET_a2s (sender_addr, sender_addr_len));
1967 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1968 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
1970 s = udp_plugin_create_session(plugin, address);
1971 s->inbound = GNUNET_YES;
1972 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
1973 (GNUNET_YES == s->inbound) ? NULL : address->address,
1974 (GNUNET_YES == s->inbound) ? 0 : address->address_length,
1977 GNUNET_free (address);
1979 /* iterate over all embedded messages */
1981 si.sender = msg->sender;
1985 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1986 ntohs (msg->header.size) -
1987 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1989 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1995 * Scan the heap for a receive context with the given address.
1997 * @param cls the 'struct FindReceiveContext'
1998 * @param node internal node of the heap
1999 * @param element value stored at the node (a 'struct ReceiveContext')
2000 * @param cost cost associated with the node
2001 * @return GNUNET_YES if we should continue to iterate,
2005 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2006 void *element, GNUNET_CONTAINER_HeapCostType cost)
2008 struct FindReceiveContext *frc = cls;
2009 struct DefragContext *e = element;
2011 if ((frc->addr_len == e->addr_len) &&
2012 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2022 * Process a defragmented message.
2024 * @param cls the 'struct ReceiveContext'
2025 * @param msg the message
2028 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2030 struct DefragContext *rc = cls;
2032 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2037 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2042 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2043 rc->src_addr, rc->addr_len);
2047 struct LookupContext
2049 const struct sockaddr * addr;
2051 struct Session *res;
2058 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
2060 struct LookupContext *l_ctx = cls;
2061 struct Session * s = value;
2063 if ((s->addrlen == l_ctx->addrlen) &&
2064 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2074 * Transmit an acknowledgement.
2076 * @param cls the 'struct ReceiveContext'
2077 * @param id message ID (unused)
2078 * @param msg ack to transmit
2081 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2083 struct DefragContext *rc = cls;
2084 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2085 struct UDP_ACK_Message *udp_ack;
2087 struct UDP_MessageWrapper *udpw;
2089 struct LookupContext l_ctx;
2091 l_ctx.addr = rc->src_addr;
2092 l_ctx.addrlen = rc->addr_len;
2094 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
2095 &lookup_session_by_addr_it,
2102 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
2103 delay = s->flow_delay_for_other_peer.rel_value;
2105 LOG (GNUNET_ERROR_TYPE_DEBUG,
2106 "Sending ACK to `%s' including delay of %u ms\n",
2107 GNUNET_a2s (rc->src_addr,
2108 (rc->src_addr->sa_family ==
2109 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2112 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2113 udpw->msg_size = msize;
2114 udpw->payload_size = 0;
2116 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2117 udpw->msg_buf = (char *)&udpw[1];
2118 udpw->msg_type = MSG_ACK;
2119 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2120 udp_ack->header.size = htons ((uint16_t) msize);
2121 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2122 udp_ack->delay = htonl (delay);
2123 udp_ack->sender = *rc->plugin->env->my_identity;
2124 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2125 enqueue (rc->plugin, udpw);
2130 read_process_msg (struct Plugin *plugin,
2131 const struct GNUNET_MessageHeader *msg,
2135 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2137 GNUNET_break_op (0);
2140 process_udp_message (plugin, (const struct UDPMessage *) msg,
2141 (const struct sockaddr *) addr, fromlen);
2146 read_process_ack (struct Plugin *plugin,
2147 const struct GNUNET_MessageHeader *msg,
2151 const struct GNUNET_MessageHeader *ack;
2152 const struct UDP_ACK_Message *udp_ack;
2153 struct LookupContext l_ctx;
2155 struct GNUNET_TIME_Relative flow_delay;
2157 if (ntohs (msg->size) <
2158 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2160 GNUNET_break_op (0);
2163 udp_ack = (const struct UDP_ACK_Message *) msg;
2164 l_ctx.addr = (const struct sockaddr *) addr;
2165 l_ctx.addrlen = fromlen;
2167 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2168 &lookup_session_by_addr_it,
2172 if ((NULL == s) || (NULL == s->frag_ctx))
2177 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
2178 LOG (GNUNET_ERROR_TYPE_DEBUG,
2179 "We received a sending delay of %llu\n",
2180 flow_delay.rel_value);
2181 s->flow_delay_from_other_peer =
2182 GNUNET_TIME_relative_to_absolute (flow_delay);
2184 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2185 if (ntohs (ack->size) !=
2186 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2188 GNUNET_break_op (0);
2192 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2194 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2196 LOG (GNUNET_ERROR_TYPE_DEBUG,
2197 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2198 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2199 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2200 /* Expect more ACKs to arrive */
2204 LOG (GNUNET_ERROR_TYPE_DEBUG,
2205 "Message full ACK'ed\n",
2206 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2207 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2209 /* Remove fragmented message after successful sending */
2210 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2215 read_process_fragment (struct Plugin *plugin,
2216 const struct GNUNET_MessageHeader *msg,
2220 struct DefragContext *d_ctx;
2221 struct GNUNET_TIME_Absolute now;
2222 struct FindReceiveContext frc;
2225 frc.addr = (const struct sockaddr *) addr;
2226 frc.addr_len = fromlen;
2228 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2229 (unsigned int) ntohs (msg->size),
2230 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2231 /* Lookup existing receive context for this address */
2232 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2233 &find_receive_context,
2235 now = GNUNET_TIME_absolute_get ();
2240 /* Create a new defragmentation context */
2241 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2242 memcpy (&d_ctx[1], addr, fromlen);
2243 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2244 d_ctx->addr_len = fromlen;
2245 d_ctx->plugin = plugin;
2247 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2248 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2249 &fragment_msg_proc, &ack_proc);
2251 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2252 (GNUNET_CONTAINER_HeapCostType)
2254 LOG (GNUNET_ERROR_TYPE_DEBUG,
2255 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2256 (unsigned int) ntohs (msg->size),
2257 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2261 LOG (GNUNET_ERROR_TYPE_DEBUG,
2262 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2263 (unsigned int) ntohs (msg->size),
2264 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2267 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2269 /* keep this 'rc' from expiring */
2270 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2271 (GNUNET_CONTAINER_HeapCostType)
2274 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2275 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2277 /* remove 'rc' that was inactive the longest */
2278 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2279 GNUNET_assert (NULL != d_ctx);
2280 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2281 GNUNET_free (d_ctx);
2287 * Read and process a message from the given socket.
2289 * @param plugin the overall plugin
2290 * @param rsock socket to read from
2293 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2297 char buf[65536] GNUNET_ALIGN;
2299 const struct GNUNET_MessageHeader *msg;
2301 fromlen = sizeof (addr);
2302 memset (&addr, 0, sizeof (addr));
2303 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2304 (struct sockaddr *) &addr, &fromlen);
2306 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2307 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2308 * on this socket has failed.
2310 * WSAECONNRESET - The virtual circuit was reset by the remote side
2311 * executing a hard or abortive close. The application should close
2312 * the socket; it is no longer usable. On a UDP-datagram socket this
2313 * error indicates a previous send operation resulted in an ICMP Port
2314 * Unreachable message.
2316 if ( (-1 == size) && (ECONNRESET == errno) )
2321 LOG (GNUNET_ERROR_TYPE_DEBUG,
2322 "UDP failed to receive data: %s\n", STRERROR (errno));
2323 /* Connection failure or something. Not a protocol violation. */
2326 if (size < sizeof (struct GNUNET_MessageHeader))
2328 LOG (GNUNET_ERROR_TYPE_WARNING,
2329 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2330 (unsigned int) size);
2331 /* _MAY_ be a connection failure (got partial message) */
2332 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2333 GNUNET_break_op (0);
2336 msg = (const struct GNUNET_MessageHeader *) buf;
2338 LOG (GNUNET_ERROR_TYPE_DEBUG,
2339 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2340 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2342 if (size != ntohs (msg->size))
2344 GNUNET_break_op (0);
2348 GNUNET_STATISTICS_update (plugin->env->stats,
2349 "# UDP, total, bytes, received",
2352 switch (ntohs (msg->type))
2354 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2355 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2358 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2359 read_process_msg (plugin, msg, addr, fromlen);
2362 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2363 read_process_ack (plugin, msg, addr, fromlen);
2366 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2367 read_process_fragment (plugin, msg, addr, fromlen);
2371 GNUNET_break_op (0);
2376 static struct UDP_MessageWrapper *
2377 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2378 struct GNUNET_NETWORK_Handle *sock)
2380 struct UDP_MessageWrapper *udpw = NULL;
2381 struct GNUNET_TIME_Relative remaining;
2384 while (udpw != NULL)
2386 /* Find messages with timeout */
2387 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2388 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2390 /* Message timed out */
2391 switch (udpw->msg_type) {
2392 case MSG_UNFRAGMENTED:
2393 GNUNET_STATISTICS_update (plugin->env->stats,
2394 "# UDP, total, bytes, sent, timeout",
2395 udpw->msg_size, GNUNET_NO);
2396 GNUNET_STATISTICS_update (plugin->env->stats,
2397 "# UDP, total, messages, sent, timeout",
2399 GNUNET_STATISTICS_update (plugin->env->stats,
2400 "# UDP, unfragmented msgs, messages, sent, timeout",
2402 GNUNET_STATISTICS_update (plugin->env->stats,
2403 "# UDP, unfragmented msgs, bytes, sent, timeout",
2404 udpw->payload_size, GNUNET_NO);
2405 /* Not fragmented message */
2406 LOG (GNUNET_ERROR_TYPE_DEBUG,
2407 "Message for peer `%s' with size %u timed out\n",
2408 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2409 call_continuation (udpw, GNUNET_SYSERR);
2410 /* Remove message */
2411 dequeue (plugin, udpw);
2414 case MSG_FRAGMENTED:
2415 /* Fragmented message */
2416 GNUNET_STATISTICS_update (plugin->env->stats,
2417 "# UDP, total, bytes, sent, timeout",
2418 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2419 GNUNET_STATISTICS_update (plugin->env->stats,
2420 "# UDP, total, messages, sent, timeout",
2422 call_continuation (udpw, GNUNET_SYSERR);
2423 LOG (GNUNET_ERROR_TYPE_DEBUG,
2424 "Fragment for message for peer `%s' with size %u timed out\n",
2425 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2428 GNUNET_STATISTICS_update (plugin->env->stats,
2429 "# UDP, fragmented msgs, messages, sent, timeout",
2431 GNUNET_STATISTICS_update (plugin->env->stats,
2432 "# UDP, fragmented msgs, bytes, sent, timeout",
2433 udpw->frag_ctx->payload_size, GNUNET_NO);
2434 /* Remove fragmented message due to timeout */
2435 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2438 GNUNET_STATISTICS_update (plugin->env->stats,
2439 "# UDP, total, bytes, sent, timeout",
2440 udpw->msg_size, GNUNET_NO);
2441 GNUNET_STATISTICS_update (plugin->env->stats,
2442 "# UDP, total, messages, sent, timeout",
2444 LOG (GNUNET_ERROR_TYPE_DEBUG,
2445 "ACK Message for peer `%s' with size %u timed out\n",
2446 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2447 call_continuation (udpw, GNUNET_SYSERR);
2448 dequeue (plugin, udpw);
2454 if (sock == plugin->sockv4)
2455 udpw = plugin->ipv4_queue_head;
2456 else if (sock == plugin->sockv6)
2457 udpw = plugin->ipv6_queue_head;
2460 GNUNET_break (0); /* should never happen */
2463 GNUNET_STATISTICS_update (plugin->env->stats,
2464 "# messages dismissed due to timeout",
2469 /* Message did not time out, check flow delay */
2470 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2471 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2473 /* this message is not delayed */
2474 LOG (GNUNET_ERROR_TYPE_DEBUG,
2475 "Message for peer `%s' (%u bytes) is not delayed \n",
2476 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2477 break; /* Found message to send, break */
2481 /* Message is delayed, try next */
2482 LOG (GNUNET_ERROR_TYPE_DEBUG,
2483 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
2484 GNUNET_i2s(&udpw->session->target), udpw->payload_size, remaining.rel_value);
2494 analyze_send_error (struct Plugin *plugin,
2495 const struct sockaddr * sa,
2499 static int network_down_error;
2500 struct GNUNET_ATS_Information type;
2502 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2503 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2504 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2506 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2508 /* IPv4: "Network unreachable" or "Network down"
2510 * This indicates we do not have connectivity
2512 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2513 _("UDP could not transmit message to `%s': "
2514 "Network seems down, please check your network configuration\n"),
2515 GNUNET_a2s (sa, slen));
2517 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2519 /* IPv6: "Network unreachable" or "Network down"
2521 * This indicates that this system is IPv6 enabled, but does not
2522 * have a valid global IPv6 address assigned or we do not have
2526 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2527 _("UDP could not transmit message to `%s': "
2528 "Please check your network configuration and disable IPv6 if your "
2529 "connection does not have a global IPv6 address\n"),
2530 GNUNET_a2s (sa, slen));
2535 LOG (GNUNET_ERROR_TYPE_WARNING,
2536 "UDP could not transmit message to `%s': `%s'\n",
2537 GNUNET_a2s (sa, slen), STRERROR (error));
2542 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2544 const struct sockaddr * sa;
2548 struct UDP_MessageWrapper *udpw = NULL;
2550 /* Find message to send */
2551 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2554 return 0; /* No message to send */
2556 sa = udpw->session->sock_addr;
2557 slen = udpw->session->addrlen;
2559 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2561 if (GNUNET_SYSERR == sent)
2564 analyze_send_error (plugin, sa, slen, errno);
2565 call_continuation(udpw, GNUNET_SYSERR);
2566 GNUNET_STATISTICS_update (plugin->env->stats,
2567 "# UDP, total, bytes, sent, failure",
2569 GNUNET_STATISTICS_update (plugin->env->stats,
2570 "# UDP, total, messages, sent, failure",
2576 LOG (GNUNET_ERROR_TYPE_DEBUG,
2577 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2578 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2579 (sent < 0) ? STRERROR (errno) : "ok");
2580 GNUNET_STATISTICS_update (plugin->env->stats,
2581 "# UDP, total, bytes, sent, success",
2583 GNUNET_STATISTICS_update (plugin->env->stats,
2584 "# UDP, total, messages, sent, success",
2586 if (NULL != udpw->frag_ctx)
2587 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2588 call_continuation (udpw, GNUNET_OK);
2590 dequeue (plugin, udpw);
2599 * We have been notified that our readset has something to read. We don't
2600 * know which socket needs to be read, so we have to check each one
2601 * Then reschedule this function to be called again once more is available.
2603 * @param cls the plugin handle
2604 * @param tc the scheduling context (for rescheduling this function again)
2607 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2609 struct Plugin *plugin = cls;
2611 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2612 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2614 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2615 (NULL != plugin->sockv4) &&
2616 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2617 udp_select_read (plugin, plugin->sockv4);
2618 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2619 (NULL != plugin->sockv4) &&
2620 (NULL != plugin->ipv4_queue_head) &&
2621 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2622 udp_select_send (plugin, plugin->sockv4);
2623 schedule_select (plugin);
2628 * We have been notified that our readset has something to read. We don't
2629 * know which socket needs to be read, so we have to check each one
2630 * Then reschedule this function to be called again once more is available.
2632 * @param cls the plugin handle
2633 * @param tc the scheduling context (for rescheduling this function again)
2636 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2638 struct Plugin *plugin = cls;
2640 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2641 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2643 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2644 (NULL != plugin->sockv6) &&
2645 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2646 udp_select_read (plugin, plugin->sockv6);
2647 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2648 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2649 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2650 udp_select_send (plugin, plugin->sockv6);
2651 schedule_select (plugin);
2657 * @return number of sockets that were successfully bound
2660 setup_sockets (struct Plugin *plugin,
2661 const struct sockaddr_in6 *bind_v6,
2662 const struct sockaddr_in *bind_v4)
2665 int sockets_created = 0;
2666 struct sockaddr_in6 serverAddrv6;
2667 struct sockaddr_in serverAddrv4;
2668 struct sockaddr *serverAddr;
2669 struct sockaddr *addrs[2];
2670 socklen_t addrlens[2];
2674 /* Create IPv6 socket */
2676 if (plugin->enable_ipv6 == GNUNET_YES)
2678 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2679 if (NULL == plugin->sockv6)
2681 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2682 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2683 plugin->enable_ipv6 = GNUNET_NO;
2687 memset (&serverAddrv6, '\0', sizeof (struct sockaddr_in6));
2688 #if HAVE_SOCKADDR_IN_SIN_LEN
2689 serverAddrv6.sin6_len = sizeof (struct sockaddr_in6);
2691 serverAddrv6.sin6_family = AF_INET6;
2692 if (NULL != bind_v6)
2693 serverAddrv6.sin6_addr = bind_v6->sin6_addr;
2695 serverAddrv6.sin6_addr = in6addr_any;
2697 if (0 == plugin->port) /* autodetect */
2698 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2700 serverAddrv6.sin6_port = htons (plugin->port);
2701 addrlen = sizeof (struct sockaddr_in6);
2702 serverAddr = (struct sockaddr *) &serverAddrv6;
2707 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2708 GNUNET_a2s (serverAddr, addrlen));
2710 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2711 serverAddr, addrlen, 0))
2714 if (0 != plugin->port)
2716 tries = 10; /* fail */
2717 break; /* bind failed on specific port */
2720 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2725 GNUNET_NETWORK_socket_close (plugin->sockv6);
2726 plugin->enable_ipv6 = GNUNET_NO;
2727 plugin->sockv6 = NULL;
2730 if (plugin->sockv6 != NULL)
2732 LOG (GNUNET_ERROR_TYPE_DEBUG,
2733 "IPv6 socket created on port %s\n",
2734 GNUNET_a2s (serverAddr, addrlen));
2735 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2736 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2741 LOG (GNUNET_ERROR_TYPE_ERROR,
2742 "Failed to bind UDP socket to %s: %s\n",
2743 GNUNET_a2s (serverAddr, addrlen),
2749 /* Create IPv4 socket */
2751 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2752 if (NULL == plugin->sockv4)
2754 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2755 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2756 plugin->enable_ipv4 = GNUNET_NO;
2760 memset (&serverAddrv4, '\0', sizeof (struct sockaddr_in));
2761 #if HAVE_SOCKADDR_IN_SIN_LEN
2762 serverAddrv4.sin_len = sizeof (struct sockaddr_in);
2764 serverAddrv4.sin_family = AF_INET;
2765 if (NULL != bind_v4)
2766 serverAddrv4.sin_addr = bind_v4->sin_addr;
2768 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2770 if (0 == plugin->port)
2772 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2774 serverAddrv4.sin_port = htons (plugin->port);
2777 addrlen = sizeof (struct sockaddr_in);
2778 serverAddr = (struct sockaddr *) &serverAddrv4;
2783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2784 GNUNET_a2s (serverAddr, addrlen));
2787 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2788 serverAddr, addrlen, 0))
2791 if (0 != plugin->port)
2793 tries = 10; /* fail */
2794 break; /* bind failed on specific port */
2798 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2804 GNUNET_NETWORK_socket_close (plugin->sockv4);
2805 plugin->enable_ipv4 = GNUNET_NO;
2806 plugin->sockv4 = NULL;
2809 if (plugin->sockv4 != NULL)
2811 LOG (GNUNET_ERROR_TYPE_DEBUG,
2812 "IPv4 socket created on port %s\n", GNUNET_a2s (serverAddr, addrlen));
2813 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2814 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2819 LOG (GNUNET_ERROR_TYPE_ERROR,
2820 "Failed to bind UDP socket to %s: %s\n",
2821 GNUNET_a2s (serverAddr, addrlen), STRERROR (eno));
2825 if (0 == sockets_created)
2827 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2828 return 0; /* No sockets created, return */
2831 /* Create file descriptors */
2832 if (plugin->enable_ipv4 == GNUNET_YES)
2834 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2835 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2836 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2837 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2838 if (NULL != plugin->sockv4)
2840 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2841 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2845 if (plugin->enable_ipv6 == GNUNET_YES)
2847 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2848 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2849 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2850 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2851 if (NULL != plugin->sockv6)
2853 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2854 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2858 schedule_select (plugin);
2859 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2860 GNUNET_NO, plugin->port,
2862 (const struct sockaddr **) addrs, addrlens,
2863 &udp_nat_port_map_callback, NULL, plugin);
2865 return sockets_created;
2870 * The exported method. Makes the core api available via a global and
2871 * returns the udp transport API.
2873 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2874 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2877 libgnunet_plugin_transport_udp_init (void *cls)
2879 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2880 struct GNUNET_TRANSPORT_PluginFunctions *api;
2882 unsigned long long port;
2883 unsigned long long aport;
2884 unsigned long long broadcast;
2885 unsigned long long udp_max_bps;
2886 unsigned long long enable_v6;
2887 char * bind4_address;
2888 char * bind6_address;
2889 char * fancy_interval;
2890 struct GNUNET_TIME_Relative interval;
2891 struct sockaddr_in serverAddrv4;
2892 struct sockaddr_in6 serverAddrv6;
2897 if (NULL == env->receive)
2899 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2900 initialze the plugin or the API */
2901 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2903 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2904 api->address_to_string = &udp_address_to_string;
2905 api->string_to_address = &udp_string_to_address;
2909 GNUNET_assert (NULL != env->stats);
2911 /* Get port number: port == 0 : autodetect a port,
2912 * > 0 : use this port,
2913 * not given : 2086 default */
2915 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2919 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2920 "ADVERTISED_PORT", &aport))
2924 LOG (GNUNET_ERROR_TYPE_WARNING,
2925 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2932 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2934 enable_v6 = GNUNET_NO;
2936 enable_v6 = GNUNET_YES;
2939 have_bind4 = GNUNET_NO;
2940 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2942 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2943 "BINDTO", &bind4_address))
2945 LOG (GNUNET_ERROR_TYPE_DEBUG,
2946 "Binding udp plugin to specific address: `%s'\n",
2948 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2950 GNUNET_free (bind4_address);
2953 have_bind4 = GNUNET_YES;
2955 GNUNET_free_non_null (bind4_address);
2956 have_bind6 = GNUNET_NO;
2957 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2959 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2960 "BINDTO6", &bind6_address))
2962 LOG (GNUNET_ERROR_TYPE_DEBUG,
2963 "Binding udp plugin to specific address: `%s'\n",
2966 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2968 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2970 GNUNET_free (bind6_address);
2973 have_bind6 = GNUNET_YES;
2975 GNUNET_free_non_null (bind6_address);
2977 /* Initialize my flags */
2980 /* Enable neighbour discovery */
2981 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2983 if (broadcast == GNUNET_SYSERR)
2984 broadcast = GNUNET_NO;
2986 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2987 "BROADCAST_INTERVAL", &fancy_interval))
2989 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2993 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
2995 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2997 GNUNET_free (fancy_interval);
3000 /* Maximum datarate */
3001 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3002 "MAX_BPS", &udp_max_bps))
3004 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3007 p = GNUNET_malloc (sizeof (struct Plugin));
3010 p->broadcast_interval = interval;
3011 p->enable_ipv6 = enable_v6;
3012 p->enable_ipv4 = GNUNET_YES; /* default */
3014 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
3015 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3016 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3017 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3018 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
3021 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3022 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &serverAddrv6 : NULL,
3023 (GNUNET_YES == have_bind4) ? &serverAddrv4 : NULL);
3024 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
3026 LOG (GNUNET_ERROR_TYPE_ERROR,
3027 _("Failed to create network sockets, plugin failed\n"));
3028 GNUNET_CONTAINER_multihashmap_destroy (p->sessions);
3029 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3030 GNUNET_SERVER_mst_destroy (p->mst);
3034 else if (broadcast == GNUNET_YES)
3036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3037 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
3040 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3043 api->disconnect = &udp_disconnect;
3044 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3045 api->address_to_string = &udp_address_to_string;
3046 api->string_to_address = &udp_string_to_address;
3047 api->check_address = &udp_plugin_check_address;
3048 api->get_session = &udp_plugin_get_session;
3049 api->send = &udp_plugin_send;
3050 api->get_network = &udp_get_network;
3057 heap_cleanup_iterator (void *cls,
3058 struct GNUNET_CONTAINER_HeapNode *
3059 node, void *element,
3060 GNUNET_CONTAINER_HeapCostType
3063 struct DefragContext * d_ctx = element;
3065 GNUNET_CONTAINER_heap_remove_node (node);
3066 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3067 GNUNET_free (d_ctx);
3074 * The exported method. Makes the core api available via a global and
3075 * returns the udp transport API.
3077 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3081 libgnunet_plugin_transport_udp_done (void *cls)
3083 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3084 struct Plugin *plugin = api->cls;
3092 stop_broadcast (plugin);
3093 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3095 GNUNET_SCHEDULER_cancel (plugin->select_task);
3096 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3098 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3100 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3101 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3104 /* Closing sockets */
3105 if (GNUNET_YES ==plugin->enable_ipv4)
3107 if (plugin->sockv4 != NULL)
3109 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3110 plugin->sockv4 = NULL;
3112 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3113 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3115 if (GNUNET_YES ==plugin->enable_ipv6)
3117 if (plugin->sockv6 != NULL)
3119 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3120 plugin->sockv6 = NULL;
3122 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3123 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3126 if (NULL != plugin->nat)
3127 GNUNET_NAT_unregister (plugin->nat);
3129 if (plugin->defrag_ctxs != NULL)
3131 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3132 heap_cleanup_iterator, NULL);
3133 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3134 plugin->defrag_ctxs = NULL;
3136 if (plugin->mst != NULL)
3138 GNUNET_SERVER_mst_destroy(plugin->mst);
3142 /* Clean up leftover messages */
3143 struct UDP_MessageWrapper * udpw;
3144 udpw = plugin->ipv4_queue_head;
3145 while (udpw != NULL)
3147 struct UDP_MessageWrapper *tmp = udpw->next;
3148 dequeue (plugin, udpw);
3149 call_continuation(udpw, GNUNET_SYSERR);
3154 udpw = plugin->ipv6_queue_head;
3155 while (udpw != NULL)
3157 struct UDP_MessageWrapper *tmp = udpw->next;
3158 dequeue (plugin, udpw);
3159 call_continuation(udpw, GNUNET_SYSERR);
3165 /* Clean up sessions */
3166 LOG (GNUNET_ERROR_TYPE_DEBUG,
3167 "Cleaning up sessions\n");
3168 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3169 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
3172 GNUNET_free (plugin);
3175 struct Allocation *allocation;
3176 while (NULL != ahead)
3179 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3180 GNUNET_free (allocation);
3182 struct Allocator *allocator;
3183 while (NULL != aehead)
3186 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3187 GNUNET_free (allocator);
3194 /* end of plugin_transport_udp.c */