2 This file is part of GNUnet
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP transport protocol
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Matthias Wachs
29 #include "plugin_transport_udp.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45 #define PLUGIN_NAME "udp"
48 * Number of messages we can defragment in parallel. We only really
49 * defragment 1 message at a time, but if messages get re-ordered, we
50 * may want to keep knowledge about the previous message to avoid
51 * discarding the current message in favor of a single fragment of a
52 * previous message. 3 should be good since we don't expect massive
53 * message reorderings with UDP.
55 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
58 * We keep a defragmentation queue per sender address. How many
59 * sender addresses do we support at the same time? Memory consumption
60 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
61 * value. (So 128 corresponds to 12 MB and should suffice for
62 * connecting to roughly 128 peers via UDP).
64 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
67 * Running pretty printers: head
69 static struct PrettyPrinterContext *ppc_dll_head;
72 * Running pretty printers: tail
74 static struct PrettyPrinterContext *ppc_dll_tail;
77 * Closure for 'append_port'.
79 struct PrettyPrinterContext
84 struct PrettyPrinterContext *next;
89 struct PrettyPrinterContext *prev;
94 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
99 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
102 * Function to call with the result.
104 GNUNET_TRANSPORT_AddressStringCallback asc;
112 * Port to add after the IP address.
133 MSG_FRAGMENTED_COMPLETE = 2,
134 MSG_UNFRAGMENTED = 3,
142 * Which peer is this session for?
144 struct GNUNET_PeerIdentity target;
146 struct UDP_FragmentationContext * frag_ctx;
149 * Address of the other peer
151 const struct sockaddr *sock_addr;
154 * Desired delay for next sending we send to other peer
156 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
159 * Desired delay for next sending we received from other peer
161 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
164 * Session timeout task
166 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
169 * expected delay for ACKs
171 struct GNUNET_TIME_Relative last_expected_ack_delay;
174 * desired delay between UDP messages
176 struct GNUNET_TIME_Relative last_expected_msg_delay;
178 struct GNUNET_ATS_Information ats;
191 struct SessionCompareContext
194 const struct GNUNET_HELLO_Address *addr;
200 * Closure for 'process_inbound_tokenized_messages'
202 struct SourceInformation
207 struct GNUNET_PeerIdentity sender;
214 struct Session *session;
216 * Number of bytes in source address.
224 * Closure for 'find_receive_context'.
226 struct FindReceiveContext
229 * Where to store the result.
231 struct DefragContext *rc;
236 const struct sockaddr *addr;
238 struct Session *session;
241 * Number of bytes in 'addr'.
250 * Data structure to track defragmentation contexts based
251 * on the source of the UDP traffic.
257 * Defragmentation context.
259 struct GNUNET_DEFRAGMENT_Context *defrag;
262 * Source address this receive context is for (allocated at the
263 * end of the struct).
265 const struct sockaddr *src_addr;
268 * Reference to master plugin struct.
270 struct Plugin *plugin;
273 * Node in the defrag heap.
275 struct GNUNET_CONTAINER_HeapNode *hnode;
278 * Length of 'src_addr'
286 * Context to send fragmented messages
288 struct UDP_FragmentationContext
291 * Next in linked list
293 struct UDP_FragmentationContext * next;
296 * Previous in linked list
298 struct UDP_FragmentationContext * prev;
303 struct Plugin * plugin;
306 * Handle for GNUNET_FRAGMENT context
308 struct GNUNET_FRAGMENT_Context * frag;
311 * The session this fragmentation context belongs to
313 struct Session * session;
316 * Function to call upon completion of the transmission.
318 GNUNET_TRANSPORT_TransmitContinuation cont;
321 * Closure for 'cont'.
328 struct GNUNET_TIME_Absolute timeout;
331 * Payload size of original unfragmented message
336 * Bytes used to send all fragments on wire including UDP overhead
340 unsigned int fragments_used;
345 struct UDP_MessageWrapper
348 * Session this message belongs to
350 struct Session *session;
356 struct UDP_MessageWrapper *prev;
362 struct UDP_MessageWrapper *next;
366 * According to UDP_MessageType
371 * Message with size msg_size including UDP specific overhead
376 * Size of UDP message to send including UDP specific overhead
381 * Payload size of original message
388 struct GNUNET_TIME_Absolute timeout;
391 * Function to call upon completion of the transmission.
393 GNUNET_TRANSPORT_TransmitContinuation cont;
396 * Closure for 'cont'.
401 * Fragmentation context
402 * frag_ctx == NULL if transport <= MTU
403 * frag_ctx != NULL if transport > MTU
405 struct UDP_FragmentationContext *frag_ctx;
410 * UDP ACK Message-Packet header (after defragmentation).
412 struct UDP_ACK_Message
417 struct GNUNET_MessageHeader header;
420 * Desired delay for flow control
425 * What is the identity of the sender
427 struct GNUNET_PeerIdentity sender;
434 static uint32_t myoptions;
438 * Encapsulation of all of the state of the plugin.
440 struct Plugin * plugin;
444 * We have been notified that our readset has something to read. We don't
445 * know which socket needs to be read, so we have to check each one
446 * Then reschedule this function to be called again once more is available.
448 * @param cls the plugin handle
449 * @param tc the scheduling context (for rescheduling this function again)
452 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
456 * We have been notified that our readset has something to read. We don't
457 * know which socket needs to be read, so we have to check each one
458 * Then reschedule this function to be called again once more is available.
460 * @param cls the plugin handle
461 * @param tc the scheduling context (for rescheduling this function again)
464 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
471 stop_session_timeout (struct Session *s)
473 GNUNET_assert (NULL != s);
475 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
477 GNUNET_SCHEDULER_cancel (s->timeout_task);
478 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
480 "Timeout stopped for session %p canceled\n",
487 * (re)schedule select tasks for this plugin.
489 * @param plugin plugin to reschedule
492 schedule_select (struct Plugin *plugin)
494 struct GNUNET_TIME_Relative min_delay;
495 struct UDP_MessageWrapper *udpw;
497 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
499 /* Find a message ready to send:
500 * Flow delay from other peer is expired or not set (0) */
501 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
502 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
503 min_delay = GNUNET_TIME_relative_min (min_delay,
504 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
506 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
507 GNUNET_SCHEDULER_cancel(plugin->select_task);
510 * - write active set if message is ready
511 * - timeout minimum delay */
512 plugin->select_task =
513 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
514 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
516 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
517 &udp_plugin_select, plugin);
519 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
521 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
522 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
523 min_delay = GNUNET_TIME_relative_min (min_delay,
524 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
526 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
527 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
528 plugin->select_task_v6 =
529 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
530 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
532 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
533 &udp_plugin_select_v6, plugin);
539 * Function called for a quick conversion of the binary address to
540 * a numeric address. Note that the caller must not free the
541 * address and that the next call to this function is allowed
542 * to override the address again.
545 * @param addr binary address
546 * @param addrlen length of the address
547 * @return string representing the same address
550 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
552 static char rbuf[INET6_ADDRSTRLEN + 10];
553 char buf[INET6_ADDRSTRLEN];
557 const struct IPv4UdpAddress *t4;
558 const struct IPv6UdpAddress *t6;
563 if ((NULL != addr) && (addrlen == sizeof (struct IPv6UdpAddress)))
567 options = ntohl (t6->options);
568 port = ntohs (t6->u6_port);
569 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
572 else if ((NULL != addr) && (addrlen == sizeof (struct IPv4UdpAddress)))
576 options = ntohl (t4->options);
577 port = ntohs (t4->u4_port);
578 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
581 else if (addrlen == 0)
583 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s", TRANSPORT_SESSION_INBOUND_STRING);
590 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
592 GNUNET_snprintf (rbuf, sizeof (rbuf),
593 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
594 PLUGIN_NAME, options, buf, port);
600 * Function called to convert a string address to
603 * @param cls closure ('struct Plugin*')
604 * @param addr string address
605 * @param addrlen length of the address
606 * @param buf location to store the buffer
607 * @param added location to store the number of bytes in the buffer.
608 * If the function returns GNUNET_SYSERR, its contents are undefined.
609 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
612 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
613 void **buf, size_t *added)
615 struct sockaddr_storage socket_address;
621 /* Format tcp.options.address:port */
626 if ((NULL == addr) || (addrlen == 0))
629 return GNUNET_SYSERR;
631 if ('\0' != addr[addrlen - 1])
634 return GNUNET_SYSERR;
636 if (strlen (addr) != addrlen - 1)
639 return GNUNET_SYSERR;
641 plugin = GNUNET_strdup (addr);
642 optionstr = strchr (plugin, '.');
643 if (NULL == optionstr)
646 GNUNET_free (plugin);
647 return GNUNET_SYSERR;
651 options = atol (optionstr);
652 address = strchr (optionstr, '.');
656 GNUNET_free (plugin);
657 return GNUNET_SYSERR;
663 GNUNET_STRINGS_to_address_ip (address, strlen (address),
667 GNUNET_free (plugin);
668 return GNUNET_SYSERR;
671 GNUNET_free (plugin);
673 switch (socket_address.ss_family)
677 struct IPv4UdpAddress *u4;
678 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
679 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
680 u4->options = htonl (options);
681 u4->ipv4_addr = in4->sin_addr.s_addr;
682 u4->u4_port = in4->sin_port;
684 *added = sizeof (struct IPv4UdpAddress);
689 struct IPv6UdpAddress *u6;
690 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
691 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
692 u6->options = htonl (options);
693 u6->ipv6_addr = in6->sin6_addr;
694 u6->u6_port = in6->sin6_port;
696 *added = sizeof (struct IPv6UdpAddress);
701 return GNUNET_SYSERR;
707 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
709 struct PrettyPrinterContext *ppc = cls;
710 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PPC %p was not removed!\n", ppc); */
711 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
712 if (NULL != ppc->resolver_handle)
714 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
715 ppc->resolver_handle = NULL;
718 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
724 * Append our port and forward the result.
726 * @param cls a 'struct PrettyPrinterContext'
727 * @param hostname result from DNS resolver
730 append_port (void *cls, const char *hostname)
732 struct PrettyPrinterContext *ppc = cls;
733 struct PrettyPrinterContext *cur;
735 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC callback: %p `%s'\n",ppc, hostname); */
736 if (hostname == NULL)
738 ppc->asc (ppc->asc_cls, NULL);
739 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
740 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
741 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
742 ppc->resolver_handle = NULL;
743 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was removed!\n", ppc); */
747 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
754 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
758 if (GNUNET_YES == ppc->ipv6)
759 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
761 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
762 ppc->asc (ppc->asc_cls, ret);
767 * Convert the transports address to a nice, human-readable
771 * @param type name of the transport that generated the address
772 * @param addr one of the addresses of the host, NULL for the last address
773 * the specific address format depends on the transport
774 * @param addrlen length of the address
775 * @param numeric should (IP) addresses be displayed in numeric form?
776 * @param timeout after how long should we give up?
777 * @param asc function to call on each string
778 * @param asc_cls closure for asc
781 udp_plugin_address_pretty_printer (void *cls, const char *type,
782 const void *addr, size_t addrlen,
784 struct GNUNET_TIME_Relative timeout,
785 GNUNET_TRANSPORT_AddressStringCallback asc,
788 struct PrettyPrinterContext *ppc;
791 struct sockaddr_in a4;
792 struct sockaddr_in6 a6;
793 const struct IPv4UdpAddress *u4;
794 const struct IPv6UdpAddress *u6;
798 if (addrlen == sizeof (struct IPv6UdpAddress))
801 memset (&a6, 0, sizeof (a6));
802 a6.sin6_family = AF_INET6;
803 #if HAVE_SOCKADDR_IN_SIN_LEN
804 a6.sin6_len = sizeof (a6);
806 a6.sin6_port = u6->u6_port;
807 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
808 port = ntohs (u6->u6_port);
809 options = ntohl (u6->options);
813 else if (addrlen == sizeof (struct IPv4UdpAddress))
816 memset (&a4, 0, sizeof (a4));
817 a4.sin_family = AF_INET;
818 #if HAVE_SOCKADDR_IN_SIN_LEN
819 a4.sin_len = sizeof (a4);
821 a4.sin_port = u4->u4_port;
822 a4.sin_addr.s_addr = u4->ipv4_addr;
823 port = ntohs (u4->u4_port);
824 options = ntohl (u4->options);
828 else if (0 == addrlen)
830 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
836 /* invalid address */
841 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
843 ppc->asc_cls = asc_cls;
845 ppc->options = options;
846 if (addrlen == sizeof (struct IPv6UdpAddress))
847 ppc->ipv6 = GNUNET_YES;
849 ppc->ipv6 = GNUNET_NO;
850 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
851 &ppc_cancel_task, ppc);
852 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
853 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was created!\n", ppc); */
854 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
860 call_continuation (struct UDP_MessageWrapper *udpw, int result)
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 "Calling continuation for %u byte message to `%s' with result %s\n",
866 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
867 (GNUNET_OK == result) ? "OK" : "SYSERR");
869 if (udpw->msg_size >= udpw->payload_size)
870 overhead = udpw->msg_size - udpw->payload_size;
872 overhead = udpw->msg_size;
876 switch (udpw->msg_type) {
877 case MSG_UNFRAGMENTED:
878 if (NULL != udpw->cont)
880 /* Transport continuation */
881 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
882 udpw->payload_size, udpw->msg_size);
884 GNUNET_STATISTICS_update (plugin->env->stats,
885 "# UDP, unfragmented msgs, messages, sent, success",
887 GNUNET_STATISTICS_update (plugin->env->stats,
888 "# UDP, unfragmented msgs, bytes payload, sent, success",
889 udpw->payload_size, GNUNET_NO);
890 GNUNET_STATISTICS_update (plugin->env->stats,
891 "# UDP, unfragmented msgs, bytes overhead, sent, success",
892 overhead, GNUNET_NO);
893 GNUNET_STATISTICS_update (plugin->env->stats,
894 "# UDP, total, bytes overhead, sent",
895 overhead, GNUNET_NO);
896 GNUNET_STATISTICS_update (plugin->env->stats,
897 "# UDP, total, bytes payload, sent",
898 udpw->payload_size, GNUNET_NO);
900 case MSG_FRAGMENTED_COMPLETE:
901 GNUNET_assert (NULL != udpw->frag_ctx);
902 if (udpw->frag_ctx->cont != NULL)
903 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
904 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
905 GNUNET_STATISTICS_update (plugin->env->stats,
906 "# UDP, fragmented msgs, messages, sent, success",
908 GNUNET_STATISTICS_update (plugin->env->stats,
909 "# UDP, fragmented msgs, bytes payload, sent, success",
910 udpw->payload_size, GNUNET_NO);
911 GNUNET_STATISTICS_update (plugin->env->stats,
912 "# UDP, fragmented msgs, bytes overhead, sent, success",
913 overhead, GNUNET_NO);
914 GNUNET_STATISTICS_update (plugin->env->stats,
915 "# UDP, total, bytes overhead, sent",
916 overhead, GNUNET_NO);
917 GNUNET_STATISTICS_update (plugin->env->stats,
918 "# UDP, total, bytes payload, sent",
919 udpw->payload_size, GNUNET_NO);
920 GNUNET_STATISTICS_update (plugin->env->stats,
921 "# UDP, fragmented msgs, messages, pending",
925 /* Fragmented message: enqueue next fragment */
926 if (NULL != udpw->cont)
927 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
928 udpw->payload_size, udpw->msg_size);
929 GNUNET_STATISTICS_update (plugin->env->stats,
930 "# UDP, fragmented msgs, fragments, sent, success",
932 GNUNET_STATISTICS_update (plugin->env->stats,
933 "# UDP, fragmented msgs, fragments bytes, sent, success",
934 udpw->msg_size, GNUNET_NO);
937 /* No continuation */
938 GNUNET_STATISTICS_update (plugin->env->stats,
939 "# UDP, ACK msgs, messages, sent, success",
941 GNUNET_STATISTICS_update (plugin->env->stats,
942 "# UDP, ACK msgs, bytes overhead, sent, success",
943 overhead, GNUNET_NO);
944 GNUNET_STATISTICS_update (plugin->env->stats,
945 "# UDP, total, bytes overhead, sent",
946 overhead, GNUNET_NO);
952 LOG (GNUNET_ERROR_TYPE_ERROR,
953 "ERROR: %u\n", udpw->msg_type);
959 switch (udpw->msg_type) {
960 case MSG_UNFRAGMENTED:
961 /* Unfragmented message: failed to send */
962 if (NULL != udpw->cont)
963 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
964 udpw->payload_size, overhead);
965 GNUNET_STATISTICS_update (plugin->env->stats,
966 "# UDP, unfragmented msgs, messages, sent, failure",
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 "# UDP, unfragmented msgs, bytes payload, sent, failure",
970 udpw->payload_size, GNUNET_NO);
971 GNUNET_STATISTICS_update (plugin->env->stats,
972 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
973 overhead, GNUNET_NO);
975 case MSG_FRAGMENTED_COMPLETE:
976 GNUNET_assert (NULL != udpw->frag_ctx);
977 if (udpw->frag_ctx->cont != NULL)
978 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
979 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
980 GNUNET_STATISTICS_update (plugin->env->stats,
981 "# UDP, fragmented msgs, messages, sent, failure",
983 GNUNET_STATISTICS_update (plugin->env->stats,
984 "# UDP, fragmented msgs, bytes payload, sent, failure",
985 udpw->payload_size, GNUNET_NO);
986 GNUNET_STATISTICS_update (plugin->env->stats,
987 "# UDP, fragmented msgs, bytes payload, sent, failure",
988 overhead, GNUNET_NO);
989 GNUNET_STATISTICS_update (plugin->env->stats,
990 "# UDP, fragmented msgs, bytes payload, sent, failure",
991 overhead, GNUNET_NO);
992 GNUNET_STATISTICS_update (plugin->env->stats,
993 "# UDP, fragmented msgs, messages, pending",
997 GNUNET_assert (NULL != udpw->frag_ctx);
998 /* Fragmented message: failed to send */
999 GNUNET_STATISTICS_update (plugin->env->stats,
1000 "# UDP, fragmented msgs, fragments, sent, failure",
1002 GNUNET_STATISTICS_update (plugin->env->stats,
1003 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1004 udpw->msg_size, GNUNET_NO);
1007 /* ACK message: failed to send */
1008 GNUNET_STATISTICS_update (plugin->env->stats,
1009 "# UDP, ACK msgs, messages, sent, failure",
1013 /* Beacon message: failed to send */
1029 * Check if the given port is plausible (must be either our listen
1030 * port or our advertised port). If it is neither, we return
1033 * @param plugin global variables
1034 * @param in_port port number to check
1035 * @return GNUNET_OK if port is either open_port or adv_port
1038 check_port (struct Plugin *plugin, uint16_t in_port)
1040 if ((in_port == plugin->port) || (in_port == plugin->aport))
1042 return GNUNET_SYSERR;
1047 * Function that will be called to check if a binary address for this
1048 * plugin is well-formed and corresponds to an address for THIS peer
1049 * (as per our configuration). Naturally, if absolutely necessary,
1050 * plugins can be a bit conservative in their answer, but in general
1051 * plugins should make sure that the address does not redirect
1052 * traffic to a 3rd party that might try to man-in-the-middle our
1055 * @param cls closure, should be our handle to the Plugin
1056 * @param addr pointer to the address
1057 * @param addrlen length of addr
1058 * @return GNUNET_OK if this is a plausible address for this peer
1059 * and transport, GNUNET_SYSERR if not
1063 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1065 struct Plugin *plugin = cls;
1066 struct IPv4UdpAddress *v4;
1067 struct IPv6UdpAddress *v6;
1069 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1070 (addrlen != sizeof (struct IPv6UdpAddress)))
1072 return GNUNET_SYSERR;
1074 if (addrlen == sizeof (struct IPv4UdpAddress))
1076 v4 = (struct IPv4UdpAddress *) addr;
1077 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1078 return GNUNET_SYSERR;
1080 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1081 sizeof (struct in_addr)))
1082 return GNUNET_SYSERR;
1086 v6 = (struct IPv6UdpAddress *) addr;
1087 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1089 GNUNET_break_op (0);
1090 return GNUNET_SYSERR;
1092 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1093 return GNUNET_SYSERR;
1095 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1096 sizeof (struct in6_addr)))
1097 return GNUNET_SYSERR;
1104 * Task to free resources associated with a session.
1106 * @param s session to free
1109 free_session (struct Session *s)
1111 if (NULL != s->frag_ctx)
1113 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
1114 GNUNET_free (s->frag_ctx);
1122 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1124 if (plugin->bytes_in_buffer < udpw->msg_size)
1128 GNUNET_STATISTICS_update (plugin->env->stats,
1129 "# UDP, total, bytes in buffers",
1130 - (long long) udpw->msg_size, GNUNET_NO);
1131 plugin->bytes_in_buffer -= udpw->msg_size;
1133 GNUNET_STATISTICS_update (plugin->env->stats,
1134 "# UDP, total, msgs in buffers",
1136 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1137 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1138 plugin->ipv4_queue_tail, udpw);
1139 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1140 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1141 plugin->ipv6_queue_tail, udpw);
1146 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1148 struct UDP_MessageWrapper *udpw;
1149 struct UDP_MessageWrapper *tmp;
1150 struct UDP_MessageWrapper dummy;
1151 struct Session *s = fc->session;
1153 LOG (GNUNET_ERROR_TYPE_DEBUG,
1154 "%p : Fragmented message removed with result %s\n",
1156 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1158 /* Call continuation for fragmented message */
1159 memset (&dummy, 0, sizeof (dummy));
1160 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1161 dummy.msg_size = s->frag_ctx->on_wire_size;
1162 dummy.payload_size = s->frag_ctx->payload_size;
1163 dummy.frag_ctx = s->frag_ctx;
1165 dummy.cont_cls = NULL;
1168 call_continuation (&dummy, result);
1170 /* Remove leftover fragments from queue */
1171 if (s->addrlen == sizeof (struct sockaddr_in6))
1173 udpw = plugin->ipv6_queue_head;
1174 while (NULL != udpw)
1177 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1179 dequeue (plugin, udpw);
1180 call_continuation (udpw, GNUNET_SYSERR);
1186 if (s->addrlen == sizeof (struct sockaddr_in))
1188 udpw = plugin->ipv4_queue_head;
1192 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1194 dequeue (plugin, udpw);
1195 call_continuation (udpw, GNUNET_SYSERR);
1202 /* Destroy fragmentation context */
1203 GNUNET_FRAGMENT_context_destroy (fc->frag,
1204 &s->last_expected_msg_delay,
1205 &s->last_expected_ack_delay);
1211 * Functions with this signature are called whenever we need
1212 * to close a session due to a disconnect or failure to
1213 * establish a connection.
1215 * @param s session to close down
1218 disconnect_session (struct Session *s)
1220 struct UDP_MessageWrapper *udpw;
1221 struct UDP_MessageWrapper *next;
1223 GNUNET_assert (GNUNET_YES != s->in_destroy);
1224 LOG (GNUNET_ERROR_TYPE_DEBUG,
1225 "Session %p to peer `%s' address ended \n",
1227 GNUNET_i2s (&s->target),
1228 GNUNET_a2s (s->sock_addr, s->addrlen));
1229 stop_session_timeout (s);
1231 if (NULL != s->frag_ctx)
1233 /* Remove fragmented message due to disconnect */
1234 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1237 next = plugin->ipv4_queue_head;
1238 while (NULL != (udpw = next))
1241 if (udpw->session == s)
1243 dequeue (plugin, udpw);
1244 call_continuation(udpw, GNUNET_SYSERR);
1248 next = plugin->ipv6_queue_head;
1249 while (NULL != (udpw = next))
1252 if (udpw->session == s)
1254 dequeue (plugin, udpw);
1255 call_continuation(udpw, GNUNET_SYSERR);
1259 plugin->env->session_end (plugin->env->cls, &s->target, s);
1261 if (NULL != s->frag_ctx)
1263 if (NULL != s->frag_ctx->cont)
1265 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1266 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1267 LOG (GNUNET_ERROR_TYPE_DEBUG,
1268 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1269 GNUNET_i2s (&s->target));
1273 GNUNET_assert (GNUNET_YES ==
1274 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1277 GNUNET_STATISTICS_set(plugin->env->stats,
1278 "# UDP, sessions active",
1279 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1282 s->in_destroy = GNUNET_YES;
1288 * Destroy a session, plugin is being unloaded.
1291 * @param key hash of public key of target peer
1292 * @param value a 'struct PeerSession*' to clean up
1293 * @return GNUNET_OK (continue to iterate)
1296 disconnect_and_free_it (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
1298 disconnect_session(value);
1304 * Disconnect from a remote node. Clean up session if we have one for this peer
1306 * @param cls closure for this call (should be handle to Plugin)
1307 * @param target the peeridentity of the peer to disconnect
1308 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1311 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1313 struct Plugin *plugin = cls;
1314 GNUNET_assert (plugin != NULL);
1316 GNUNET_assert (target != NULL);
1317 LOG (GNUNET_ERROR_TYPE_DEBUG,
1318 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1319 /* Clean up sessions */
1320 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions, target,
1321 &disconnect_and_free_it, plugin);
1326 * Session was idle, so disconnect it
1329 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1331 GNUNET_assert (NULL != cls);
1332 struct Session *s = cls;
1334 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Session %p was idle for %s, disconnecting\n",
1338 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1340 /* call session destroy function */
1341 disconnect_session (s);
1346 * Start session timeout
1349 start_session_timeout (struct Session *s)
1351 GNUNET_assert (NULL != s);
1352 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1353 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 "Timeout for session %p set to %s\n",
1359 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1365 * Increment session timeout due to activity
1368 reschedule_session_timeout (struct Session *s)
1370 GNUNET_assert (NULL != s);
1371 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1373 GNUNET_SCHEDULER_cancel (s->timeout_task);
1374 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 "Timeout rescheduled for session %p set to %s\n",
1380 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1385 static struct Session *
1386 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1387 const void *addr, size_t addrlen,
1388 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1391 const struct IPv4UdpAddress *t4;
1392 const struct IPv6UdpAddress *t6;
1393 struct sockaddr_in *v4;
1394 struct sockaddr_in6 *v6;
1405 case sizeof (struct IPv4UdpAddress):
1406 if (NULL == plugin->sockv4)
1408 LOG (GNUNET_ERROR_TYPE_DEBUG,
1409 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1411 udp_address_to_string(NULL, addr, addrlen));
1415 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1416 len = sizeof (struct sockaddr_in);
1417 v4 = (struct sockaddr_in *) &s[1];
1418 v4->sin_family = AF_INET;
1419 #if HAVE_SOCKADDR_IN_SIN_LEN
1420 v4->sin_len = sizeof (struct sockaddr_in);
1422 v4->sin_port = t4->u4_port;
1423 v4->sin_addr.s_addr = t4->ipv4_addr;
1424 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1426 case sizeof (struct IPv6UdpAddress):
1427 if (NULL == plugin->sockv6)
1429 LOG (GNUNET_ERROR_TYPE_INFO,
1430 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1432 udp_address_to_string(NULL, addr, addrlen));
1436 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1437 len = sizeof (struct sockaddr_in6);
1438 v6 = (struct sockaddr_in6 *) &s[1];
1439 v6->sin6_family = AF_INET6;
1440 #if HAVE_SOCKADDR_IN_SIN_LEN
1441 v6->sin6_len = sizeof (struct sockaddr_in6);
1443 v6->sin6_port = t6->u6_port;
1444 v6->sin6_addr = t6->ipv6_addr;
1445 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1448 /* Must have a valid address to send to */
1449 GNUNET_STATISTICS_update (plugin->env->stats,
1451 ("# requests to create session with invalid address"),
1456 s->target = *target;
1457 s->sock_addr = (const struct sockaddr *) &s[1];
1458 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1459 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1460 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1461 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1462 s->inbound = GNUNET_NO;
1463 start_session_timeout (s);
1469 session_cmp_it (void *cls,
1470 const struct GNUNET_PeerIdentity * key,
1473 struct SessionCompareContext * cctx = cls;
1474 const struct GNUNET_HELLO_Address *address = cctx->addr;
1475 struct Session *s = value;
1477 socklen_t s_addrlen = s->addrlen;
1479 LOG (GNUNET_ERROR_TYPE_DEBUG,
1480 "Comparing address %s <-> %s\n",
1481 udp_address_to_string (NULL, (void *) address->address,
1482 address->address_length),
1483 GNUNET_a2s (s->sock_addr, s->addrlen));
1484 if (s->inbound != cctx->inbound)
1486 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1487 (s_addrlen == sizeof (struct sockaddr_in)))
1489 struct IPv4UdpAddress * u4 = NULL;
1490 u4 = (struct IPv4UdpAddress *) address->address;
1491 GNUNET_assert (NULL != u4);
1492 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1493 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1494 (u4->u4_port == s4->sin_port))
1501 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1502 (s_addrlen == sizeof (struct sockaddr_in6)))
1504 struct IPv6UdpAddress * u6 = NULL;
1505 u6 = (struct IPv6UdpAddress *) address->address;
1506 GNUNET_assert (NULL != u6);
1507 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1508 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1509 (u6->u6_port == s6->sin6_port))
1520 * Function obtain the network type for a session
1522 * @param cls closure ('struct Plugin*')
1523 * @param session the session
1524 * @return the network type in HBO or GNUNET_SYSERR
1526 static enum GNUNET_ATS_Network_Type
1527 udp_get_network (void *cls,
1528 struct Session *session)
1530 return ntohl (session->ats.value);
1535 * Creates a new outbound session the transport service will use to send data to the
1538 * @param cls the plugin
1539 * @param address the address
1540 * @param inbound look for inbound session
1541 * @return the session or NULL of max connections exceeded
1543 static struct Session *
1544 udp_plugin_lookup_session (void *cls,
1545 const struct GNUNET_HELLO_Address *address,
1548 struct Plugin * plugin = cls;
1549 struct IPv6UdpAddress * udp_a6;
1550 struct IPv4UdpAddress * udp_a4;
1552 GNUNET_assert (plugin != NULL);
1553 GNUNET_assert (address != NULL);
1556 if ((address->address == NULL) ||
1557 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1558 (address->address_length != sizeof (struct IPv6UdpAddress))))
1560 LOG (GNUNET_ERROR_TYPE_WARNING,
1561 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1562 address->address_length,
1563 sizeof (struct IPv4UdpAddress),
1564 sizeof (struct IPv6UdpAddress));
1568 if (address->address_length == sizeof (struct IPv4UdpAddress))
1570 if (plugin->sockv4 == NULL)
1572 udp_a4 = (struct IPv4UdpAddress *) address->address;
1573 if (udp_a4->u4_port == 0)
1577 if (address->address_length == sizeof (struct IPv6UdpAddress))
1579 if (plugin->sockv6 == NULL)
1581 udp_a6 = (struct IPv6UdpAddress *) address->address;
1582 if (udp_a6->u6_port == 0)
1586 /* check if session already exists */
1587 struct SessionCompareContext cctx;
1588 cctx.addr = address;
1590 cctx.inbound = inbound;
1591 LOG (GNUNET_ERROR_TYPE_DEBUG,
1592 "Looking for existing session for peer `%s' `%s' \n",
1593 GNUNET_i2s (&address->peer),
1594 udp_address_to_string(NULL, address->address, address->address_length));
1595 GNUNET_CONTAINER_multipeermap_get_multiple(plugin->sessions, &address->peer, session_cmp_it, &cctx);
1596 if (cctx.res != NULL)
1598 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1605 static struct Session *
1606 udp_plugin_create_session (void *cls,
1607 const struct GNUNET_HELLO_Address *address,
1612 s = create_session (plugin,
1615 address->address_length,
1618 return NULL; /* protocol not supported or address invalid */
1619 s->inbound = inbound;
1620 LOG (GNUNET_ERROR_TYPE_DEBUG,
1621 "Creating new %s session %p for peer `%s' address `%s'\n",
1622 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
1624 GNUNET_i2s(&address->peer),
1625 udp_address_to_string(NULL,address->address,address->address_length));
1626 GNUNET_assert (GNUNET_OK ==
1627 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1631 GNUNET_STATISTICS_set(plugin->env->stats,
1632 "# UDP, sessions active",
1633 GNUNET_CONTAINER_multipeermap_size(plugin->sessions),
1640 * Creates a new outbound session the transport service will use to send data to the
1643 * @param cls the plugin
1644 * @param address the address
1645 * @return the session or NULL of max connections exceeded
1647 static struct Session *
1648 udp_plugin_get_session (void *cls,
1649 const struct GNUNET_HELLO_Address *address)
1653 if (NULL == address)
1658 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1659 (address->address_length != sizeof (struct IPv6UdpAddress)))
1662 /* otherwise create new */
1663 if (NULL != (s = udp_plugin_lookup_session (cls, address, GNUNET_NO)))
1665 return udp_plugin_create_session (cls, address, GNUNET_NO);
1670 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1672 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1676 GNUNET_STATISTICS_update (plugin->env->stats,
1677 "# UDP, total, bytes in buffers",
1678 udpw->msg_size, GNUNET_NO);
1679 plugin->bytes_in_buffer += udpw->msg_size;
1681 GNUNET_STATISTICS_update (plugin->env->stats,
1682 "# UDP, total, msgs in buffers",
1684 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1685 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1686 plugin->ipv4_queue_tail, udpw);
1687 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1688 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1689 plugin->ipv6_queue_tail, udpw);
1695 * Fragment message was transmitted via UDP, let fragmentation know
1696 * to send the next fragment now.
1698 * @param cls the 'struct UDPMessageWrapper' of the fragment
1699 * @param target destination peer (ignored)
1700 * @param result GNUNET_OK on success (ignored)
1701 * @param payload bytes payload sent
1702 * @param physical bytes physical sent
1705 send_next_fragment (void *cls,
1706 const struct GNUNET_PeerIdentity *target,
1707 int result, size_t payload, size_t physical)
1709 struct UDP_MessageWrapper *udpw = cls;
1711 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1716 * Function that is called with messages created by the fragmentation
1717 * module. In the case of the 'proc' callback of the
1718 * GNUNET_FRAGMENT_context_create function, this function must
1719 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1721 * @param cls closure, the 'struct FragmentationContext'
1722 * @param msg the message that was created
1725 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1727 struct UDP_FragmentationContext *frag_ctx = cls;
1728 struct Plugin *plugin = frag_ctx->plugin;
1729 struct UDP_MessageWrapper * udpw;
1730 size_t msg_len = ntohs (msg->size);
1732 LOG (GNUNET_ERROR_TYPE_DEBUG,
1733 "Enqueuing fragment with %u bytes\n", msg_len);
1734 frag_ctx->fragments_used ++;
1735 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1736 udpw->session = frag_ctx->session;
1737 udpw->msg_buf = (char *) &udpw[1];
1738 udpw->msg_size = msg_len;
1739 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1740 udpw->cont = &send_next_fragment;
1741 udpw->cont_cls = udpw;
1742 udpw->timeout = frag_ctx->timeout;
1743 udpw->frag_ctx = frag_ctx;
1744 udpw->msg_type = MSG_FRAGMENTED;
1745 memcpy (udpw->msg_buf, msg, msg_len);
1746 enqueue (plugin, udpw);
1747 schedule_select (plugin);
1752 * Function that can be used by the transport service to transmit
1753 * a message using the plugin. Note that in the case of a
1754 * peer disconnecting, the continuation MUST be called
1755 * prior to the disconnect notification itself. This function
1756 * will be called with this peer's HELLO message to initiate
1757 * a fresh connection to another peer.
1759 * @param cls closure
1760 * @param s which session must be used
1761 * @param msgbuf the message to transmit
1762 * @param msgbuf_size number of bytes in 'msgbuf'
1763 * @param priority how important is the message (most plugins will
1764 * ignore message priority and just FIFO)
1765 * @param to how long to wait at most for the transmission (does not
1766 * require plugins to discard the message after the timeout,
1767 * just advisory for the desired delay; most plugins will ignore
1769 * @param cont continuation to call once the message has
1770 * been transmitted (or if the transport is ready
1771 * for the next transmission call; or if the
1772 * peer disconnected...); can be NULL
1773 * @param cont_cls closure for cont
1774 * @return number of bytes used (on the physical network, with overheads);
1775 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1776 * and does NOT mean that the message was not transmitted (DV)
1779 udp_plugin_send (void *cls,
1781 const char *msgbuf, size_t msgbuf_size,
1782 unsigned int priority,
1783 struct GNUNET_TIME_Relative to,
1784 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1786 struct Plugin *plugin = cls;
1787 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1788 struct UDP_FragmentationContext * frag_ctx;
1789 struct UDP_MessageWrapper * udpw;
1790 struct UDPMessage *udp;
1792 GNUNET_assert (plugin != NULL);
1793 GNUNET_assert (s != NULL);
1795 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1796 return GNUNET_SYSERR;
1797 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1798 return GNUNET_SYSERR;
1799 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1802 return GNUNET_SYSERR;
1804 if (GNUNET_YES != GNUNET_CONTAINER_multipeermap_contains_value(plugin->sessions, &s->target, s))
1807 return GNUNET_SYSERR;
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1812 GNUNET_i2s (&s->target),
1813 GNUNET_a2s(s->sock_addr, s->addrlen));
1817 udp = (struct UDPMessage *) mbuf;
1818 udp->header.size = htons (udpmlen);
1819 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1820 udp->reserved = htonl (0);
1821 udp->sender = *plugin->env->my_identity;
1823 reschedule_session_timeout(s);
1824 if (udpmlen <= UDP_MTU)
1826 /* unfragmented message */
1827 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1829 udpw->msg_buf = (char *) &udpw[1];
1830 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1831 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1832 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1834 udpw->cont_cls = cont_cls;
1835 udpw->frag_ctx = NULL;
1836 udpw->msg_type = MSG_UNFRAGMENTED;
1837 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1838 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1839 enqueue (plugin, udpw);
1841 GNUNET_STATISTICS_update (plugin->env->stats,
1842 "# UDP, unfragmented msgs, messages, attempt",
1844 GNUNET_STATISTICS_update (plugin->env->stats,
1845 "# UDP, unfragmented msgs, bytes payload, attempt",
1846 udpw->payload_size, GNUNET_NO);
1850 /* fragmented message */
1851 if (s->frag_ctx != NULL)
1852 return GNUNET_SYSERR;
1853 memcpy (&udp[1], msgbuf, msgbuf_size);
1854 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1855 frag_ctx->plugin = plugin;
1856 frag_ctx->session = s;
1857 frag_ctx->cont = cont;
1858 frag_ctx->cont_cls = cont_cls;
1859 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1860 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1861 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1862 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1865 s->last_expected_msg_delay,
1866 s->last_expected_ack_delay,
1870 s->frag_ctx = frag_ctx;
1871 GNUNET_STATISTICS_update (plugin->env->stats,
1872 "# UDP, fragmented msgs, messages, pending",
1874 GNUNET_STATISTICS_update (plugin->env->stats,
1875 "# UDP, fragmented msgs, messages, attempt",
1877 GNUNET_STATISTICS_update (plugin->env->stats,
1878 "# UDP, fragmented msgs, bytes payload, attempt",
1879 frag_ctx->payload_size, GNUNET_NO);
1881 schedule_select (plugin);
1887 * Our external IP address/port mapping has changed.
1889 * @param cls closure, the 'struct LocalAddrList'
1890 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1891 * the previous (now invalid) one
1892 * @param addr either the previous or the new public IP address
1893 * @param addrlen actual lenght of the address
1896 udp_nat_port_map_callback (void *cls, int add_remove,
1897 const struct sockaddr *addr, socklen_t addrlen)
1899 struct Plugin *plugin = cls;
1900 struct IPv4UdpAddress u4;
1901 struct IPv6UdpAddress u6;
1905 LOG (GNUNET_ERROR_TYPE_INFO,
1906 "NAT notification to %s address `%s'\n",
1907 (GNUNET_YES == add_remove) ? "add" : "remove",
1908 GNUNET_a2s (addr, addrlen));
1910 /* convert 'addr' to our internal format */
1911 switch (addr->sa_family)
1914 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1915 memset (&u4, 0, sizeof (u4));
1916 u4.options = htonl(myoptions);
1917 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1918 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1919 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1922 args = sizeof (struct IPv4UdpAddress);
1925 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1926 memset (&u4, 0, sizeof (u4));
1927 u6.options = htonl(myoptions);
1928 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1930 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1931 sizeof (struct in6_addr));
1932 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1934 args = sizeof (struct IPv6UdpAddress);
1940 /* modify our published address list */
1941 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1947 * Message tokenizer has broken up an incomming message. Pass it on
1950 * @param cls the 'struct Plugin'
1951 * @param client the 'struct SourceInformation'
1952 * @param hdr the actual message
1955 process_inbound_tokenized_messages (void *cls, void *client,
1956 const struct GNUNET_MessageHeader *hdr)
1958 struct Plugin *plugin = cls;
1959 struct SourceInformation *si = client;
1960 struct GNUNET_TIME_Relative delay;
1962 GNUNET_assert (si->session != NULL);
1963 if (GNUNET_YES == si->session->in_destroy)
1966 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1967 delay = plugin->env->receive (plugin->env->cls,
1971 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1972 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1974 plugin->env->update_address_metrics (plugin->env->cls,
1976 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1977 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1979 &si->session->ats, 1);
1981 si->session->flow_delay_for_other_peer = delay;
1982 reschedule_session_timeout(si->session);
1988 * We've received a UDP Message. Process it (pass contents to main service).
1990 * @param plugin plugin context
1991 * @param msg the message
1992 * @param sender_addr sender address
1993 * @param sender_addr_len number of bytes in sender_addr
1996 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1997 const struct sockaddr *sender_addr,
1998 socklen_t sender_addr_len)
2000 struct SourceInformation si;
2002 struct IPv4UdpAddress u4;
2003 struct IPv6UdpAddress u6;
2007 if (0 != ntohl (msg->reserved))
2009 GNUNET_break_op (0);
2012 if (ntohs (msg->header.size) <
2013 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2015 GNUNET_break_op (0);
2019 /* convert address */
2020 switch (sender_addr->sa_family)
2023 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2024 memset (&u4, 0, sizeof (u4));
2025 u6.options = htonl (0);
2026 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2027 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2032 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2033 memset (&u6, 0, sizeof (u6));
2034 u6.options = htonl (0);
2035 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2036 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2044 LOG (GNUNET_ERROR_TYPE_DEBUG,
2045 "Received message with %u bytes from peer `%s' at `%s'\n",
2046 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2047 GNUNET_a2s (sender_addr, sender_addr_len));
2049 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2050 if (NULL == (s = udp_plugin_lookup_session (plugin, address, GNUNET_YES)))
2052 s = udp_plugin_create_session (plugin, address, GNUNET_YES);
2053 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2054 (GNUNET_YES == s->inbound) ? NULL : address->address,
2055 (GNUNET_YES == s->inbound) ? 0 : address->address_length,
2058 GNUNET_free (address);
2060 /* iterate over all embedded messages */
2062 si.sender = msg->sender;
2066 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2067 ntohs (msg->header.size) -
2068 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
2070 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2076 * Scan the heap for a receive context with the given address.
2078 * @param cls the 'struct FindReceiveContext'
2079 * @param node internal node of the heap
2080 * @param element value stored at the node (a 'struct ReceiveContext')
2081 * @param cost cost associated with the node
2082 * @return GNUNET_YES if we should continue to iterate,
2086 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2087 void *element, GNUNET_CONTAINER_HeapCostType cost)
2089 struct FindReceiveContext *frc = cls;
2090 struct DefragContext *e = element;
2092 if ((frc->addr_len == e->addr_len) &&
2093 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2103 * Process a defragmented message.
2105 * @param cls the 'struct ReceiveContext'
2106 * @param msg the message
2109 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2111 struct DefragContext *rc = cls;
2113 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2118 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2123 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2124 rc->src_addr, rc->addr_len);
2128 struct LookupContext
2130 struct Session *res;
2132 const struct sockaddr * addr;
2136 int must_have_frag_ctx;
2141 lookup_session_by_addr_it (void *cls,
2142 const struct GNUNET_PeerIdentity *key,
2145 struct LookupContext *l_ctx = cls;
2146 struct Session * s = value;
2148 if ((GNUNET_YES == l_ctx->must_have_frag_ctx) && (NULL == s->frag_ctx))
2152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Comparing session: have %s %s %p<-> want %s\n",
2153 GNUNET_a2s(s->sock_addr, s->addrlen),
2154 (GNUNET_YES == s->inbound) ? "inbound" : "outbound",
2156 GNUNET_a2s(l_ctx->addr, l_ctx->addrlen));
2158 if ((s->addrlen == l_ctx->addrlen) &&
2159 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2169 * Transmit an acknowledgement.
2171 * @param cls the 'struct ReceiveContext'
2172 * @param id message ID (unused)
2173 * @param msg ack to transmit
2176 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2178 struct DefragContext *rc = cls;
2179 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2180 struct UDP_ACK_Message *udp_ack;
2182 struct UDP_MessageWrapper *udpw;
2184 struct LookupContext l_ctx;
2186 l_ctx.addr = rc->src_addr;
2187 l_ctx.addrlen = rc->addr_len;
2189 l_ctx.must_have_frag_ctx = GNUNET_NO;
2190 GNUNET_CONTAINER_multipeermap_iterate (rc->plugin->sessions,
2191 &lookup_session_by_addr_it,
2198 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2199 delay = s->flow_delay_for_other_peer.rel_value_us;
2201 LOG (GNUNET_ERROR_TYPE_DEBUG,
2202 "Sending ACK to `%s' including delay of %s\n",
2203 GNUNET_a2s (rc->src_addr,
2204 (rc->src_addr->sa_family ==
2205 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2207 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2209 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2210 udpw->msg_size = msize;
2211 udpw->payload_size = 0;
2213 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2214 udpw->msg_buf = (char *)&udpw[1];
2215 udpw->msg_type = MSG_ACK;
2216 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2217 udp_ack->header.size = htons ((uint16_t) msize);
2218 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2219 udp_ack->delay = htonl (delay);
2220 udp_ack->sender = *rc->plugin->env->my_identity;
2221 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2222 enqueue (rc->plugin, udpw);
2227 read_process_msg (struct Plugin *plugin,
2228 const struct GNUNET_MessageHeader *msg,
2232 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2234 GNUNET_break_op (0);
2237 process_udp_message (plugin, (const struct UDPMessage *) msg,
2238 (const struct sockaddr *) addr, fromlen);
2243 read_process_ack (struct Plugin *plugin,
2244 const struct GNUNET_MessageHeader *msg,
2248 const struct GNUNET_MessageHeader *ack;
2249 const struct UDP_ACK_Message *udp_ack;
2250 struct LookupContext l_ctx;
2252 struct GNUNET_TIME_Relative flow_delay;
2254 if (ntohs (msg->size) <
2255 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2257 GNUNET_break_op (0);
2260 udp_ack = (const struct UDP_ACK_Message *) msg;
2261 l_ctx.addr = (const struct sockaddr *) addr;
2262 l_ctx.addrlen = fromlen;
2264 l_ctx.must_have_frag_ctx = GNUNET_YES;
2265 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
2266 &lookup_session_by_addr_it,
2269 if ((NULL == s) || (NULL == s->frag_ctx))
2275 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2276 LOG (GNUNET_ERROR_TYPE_DEBUG,
2277 "We received a sending delay of %s\n",
2278 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2280 s->flow_delay_from_other_peer =
2281 GNUNET_TIME_relative_to_absolute (flow_delay);
2283 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2284 if (ntohs (ack->size) !=
2285 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2287 GNUNET_break_op (0);
2291 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2293 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2295 LOG (GNUNET_ERROR_TYPE_DEBUG,
2296 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2297 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2298 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2299 /* Expect more ACKs to arrive */
2303 LOG (GNUNET_ERROR_TYPE_DEBUG,
2304 "Message full ACK'ed\n",
2305 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2306 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2308 /* Remove fragmented message after successful sending */
2309 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2314 read_process_fragment (struct Plugin *plugin,
2315 const struct GNUNET_MessageHeader *msg,
2319 struct DefragContext *d_ctx;
2320 struct GNUNET_TIME_Absolute now;
2321 struct FindReceiveContext frc;
2324 frc.addr = (const struct sockaddr *) addr;
2325 frc.addr_len = fromlen;
2327 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2328 (unsigned int) ntohs (msg->size),
2329 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2330 /* Lookup existing receive context for this address */
2331 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2332 &find_receive_context,
2334 now = GNUNET_TIME_absolute_get ();
2339 /* Create a new defragmentation context */
2340 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2341 memcpy (&d_ctx[1], addr, fromlen);
2342 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2343 d_ctx->addr_len = fromlen;
2344 d_ctx->plugin = plugin;
2346 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2347 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2348 &fragment_msg_proc, &ack_proc);
2350 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2351 (GNUNET_CONTAINER_HeapCostType)
2353 LOG (GNUNET_ERROR_TYPE_DEBUG,
2354 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2355 (unsigned int) ntohs (msg->size),
2356 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2360 LOG (GNUNET_ERROR_TYPE_DEBUG,
2361 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2362 (unsigned int) ntohs (msg->size),
2363 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2366 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2368 /* keep this 'rc' from expiring */
2369 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2370 (GNUNET_CONTAINER_HeapCostType)
2373 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2374 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2376 /* remove 'rc' that was inactive the longest */
2377 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2378 GNUNET_assert (NULL != d_ctx);
2379 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2380 GNUNET_free (d_ctx);
2386 * Read and process a message from the given socket.
2388 * @param plugin the overall plugin
2389 * @param rsock socket to read from
2392 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2396 char buf[65536] GNUNET_ALIGN;
2398 const struct GNUNET_MessageHeader *msg;
2400 fromlen = sizeof (addr);
2401 memset (&addr, 0, sizeof (addr));
2402 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2403 (struct sockaddr *) &addr, &fromlen);
2405 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2406 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2407 * on this socket has failed.
2409 * WSAECONNRESET - The virtual circuit was reset by the remote side
2410 * executing a hard or abortive close. The application should close
2411 * the socket; it is no longer usable. On a UDP-datagram socket this
2412 * error indicates a previous send operation resulted in an ICMP Port
2413 * Unreachable message.
2415 if ( (-1 == size) && (ECONNRESET == errno) )
2420 LOG (GNUNET_ERROR_TYPE_DEBUG,
2421 "UDP failed to receive data: %s\n", STRERROR (errno));
2422 /* Connection failure or something. Not a protocol violation. */
2425 if (size < sizeof (struct GNUNET_MessageHeader))
2427 LOG (GNUNET_ERROR_TYPE_WARNING,
2428 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2429 (unsigned int) size);
2430 /* _MAY_ be a connection failure (got partial message) */
2431 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2432 GNUNET_break_op (0);
2435 msg = (const struct GNUNET_MessageHeader *) buf;
2437 LOG (GNUNET_ERROR_TYPE_DEBUG,
2438 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2439 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2441 if (size != ntohs (msg->size))
2443 GNUNET_break_op (0);
2447 GNUNET_STATISTICS_update (plugin->env->stats,
2448 "# UDP, total, bytes, received",
2451 switch (ntohs (msg->type))
2453 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2454 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2457 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2458 read_process_msg (plugin, msg, addr, fromlen);
2461 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2462 read_process_ack (plugin, msg, addr, fromlen);
2465 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2466 read_process_fragment (plugin, msg, addr, fromlen);
2470 GNUNET_break_op (0);
2476 static struct UDP_MessageWrapper *
2477 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2478 struct GNUNET_NETWORK_Handle *sock)
2480 struct UDP_MessageWrapper *udpw = NULL;
2481 struct GNUNET_TIME_Relative remaining;
2484 while (udpw != NULL)
2486 /* Find messages with timeout */
2487 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2488 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2490 /* Message timed out */
2491 switch (udpw->msg_type) {
2492 case MSG_UNFRAGMENTED:
2493 GNUNET_STATISTICS_update (plugin->env->stats,
2494 "# UDP, total, bytes, sent, timeout",
2495 udpw->msg_size, GNUNET_NO);
2496 GNUNET_STATISTICS_update (plugin->env->stats,
2497 "# UDP, total, messages, sent, timeout",
2499 GNUNET_STATISTICS_update (plugin->env->stats,
2500 "# UDP, unfragmented msgs, messages, sent, timeout",
2502 GNUNET_STATISTICS_update (plugin->env->stats,
2503 "# UDP, unfragmented msgs, bytes, sent, timeout",
2504 udpw->payload_size, GNUNET_NO);
2505 /* Not fragmented message */
2506 LOG (GNUNET_ERROR_TYPE_DEBUG,
2507 "Message for peer `%s' with size %u timed out\n",
2508 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2509 call_continuation (udpw, GNUNET_SYSERR);
2510 /* Remove message */
2511 dequeue (plugin, udpw);
2514 case MSG_FRAGMENTED:
2515 /* Fragmented message */
2516 GNUNET_STATISTICS_update (plugin->env->stats,
2517 "# UDP, total, bytes, sent, timeout",
2518 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2519 GNUNET_STATISTICS_update (plugin->env->stats,
2520 "# UDP, total, messages, sent, timeout",
2522 call_continuation (udpw, GNUNET_SYSERR);
2523 LOG (GNUNET_ERROR_TYPE_DEBUG,
2524 "Fragment for message for peer `%s' with size %u timed out\n",
2525 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2528 GNUNET_STATISTICS_update (plugin->env->stats,
2529 "# UDP, fragmented msgs, messages, sent, timeout",
2531 GNUNET_STATISTICS_update (plugin->env->stats,
2532 "# UDP, fragmented msgs, bytes, sent, timeout",
2533 udpw->frag_ctx->payload_size, GNUNET_NO);
2534 /* Remove fragmented message due to timeout */
2535 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2538 GNUNET_STATISTICS_update (plugin->env->stats,
2539 "# UDP, total, bytes, sent, timeout",
2540 udpw->msg_size, GNUNET_NO);
2541 GNUNET_STATISTICS_update (plugin->env->stats,
2542 "# UDP, total, messages, sent, timeout",
2544 LOG (GNUNET_ERROR_TYPE_DEBUG,
2545 "ACK Message for peer `%s' with size %u timed out\n",
2546 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2547 call_continuation (udpw, GNUNET_SYSERR);
2548 dequeue (plugin, udpw);
2554 if (sock == plugin->sockv4)
2555 udpw = plugin->ipv4_queue_head;
2556 else if (sock == plugin->sockv6)
2557 udpw = plugin->ipv6_queue_head;
2560 GNUNET_break (0); /* should never happen */
2563 GNUNET_STATISTICS_update (plugin->env->stats,
2564 "# messages dismissed due to timeout",
2569 /* Message did not time out, check flow delay */
2570 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2571 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2573 /* this message is not delayed */
2574 LOG (GNUNET_ERROR_TYPE_DEBUG,
2575 "Message for peer `%s' (%u bytes) is not delayed \n",
2576 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2577 break; /* Found message to send, break */
2581 /* Message is delayed, try next */
2582 LOG (GNUNET_ERROR_TYPE_DEBUG,
2583 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2584 GNUNET_i2s(&udpw->session->target), udpw->payload_size,
2585 GNUNET_STRINGS_relative_time_to_string (remaining,
2596 analyze_send_error (struct Plugin *plugin,
2597 const struct sockaddr * sa,
2601 static int network_down_error;
2602 struct GNUNET_ATS_Information type;
2604 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2605 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2606 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2608 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2610 /* IPv4: "Network unreachable" or "Network down"
2612 * This indicates we do not have connectivity
2614 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2615 _("UDP could not transmit message to `%s': "
2616 "Network seems down, please check your network configuration\n"),
2617 GNUNET_a2s (sa, slen));
2619 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2621 /* IPv6: "Network unreachable" or "Network down"
2623 * This indicates that this system is IPv6 enabled, but does not
2624 * have a valid global IPv6 address assigned or we do not have
2628 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2629 _("UDP could not transmit message to `%s': "
2630 "Please check your network configuration and disable IPv6 if your "
2631 "connection does not have a global IPv6 address\n"),
2632 GNUNET_a2s (sa, slen));
2637 LOG (GNUNET_ERROR_TYPE_WARNING,
2638 "UDP could not transmit message to `%s': `%s'\n",
2639 GNUNET_a2s (sa, slen), STRERROR (error));
2645 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2647 const struct sockaddr * sa;
2651 struct UDP_MessageWrapper *udpw = NULL;
2653 /* Find message to send */
2654 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2657 return 0; /* No message to send */
2659 sa = udpw->session->sock_addr;
2660 slen = udpw->session->addrlen;
2662 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2664 if (GNUNET_SYSERR == sent)
2667 analyze_send_error (plugin, sa, slen, errno);
2668 call_continuation(udpw, GNUNET_SYSERR);
2669 GNUNET_STATISTICS_update (plugin->env->stats,
2670 "# UDP, total, bytes, sent, failure",
2672 GNUNET_STATISTICS_update (plugin->env->stats,
2673 "# UDP, total, messages, sent, failure",
2679 LOG (GNUNET_ERROR_TYPE_DEBUG,
2680 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2681 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2682 (sent < 0) ? STRERROR (errno) : "ok");
2683 GNUNET_STATISTICS_update (plugin->env->stats,
2684 "# UDP, total, bytes, sent, success",
2686 GNUNET_STATISTICS_update (plugin->env->stats,
2687 "# UDP, total, messages, sent, success",
2689 if (NULL != udpw->frag_ctx)
2690 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2691 call_continuation (udpw, GNUNET_OK);
2693 dequeue (plugin, udpw);
2702 * We have been notified that our readset has something to read. We don't
2703 * know which socket needs to be read, so we have to check each one
2704 * Then reschedule this function to be called again once more is available.
2706 * @param cls the plugin handle
2707 * @param tc the scheduling context (for rescheduling this function again)
2710 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2712 struct Plugin *plugin = cls;
2714 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2717 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2718 (NULL != plugin->sockv4) &&
2719 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2720 udp_select_read (plugin, plugin->sockv4);
2721 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2722 (NULL != plugin->sockv4) &&
2723 (NULL != plugin->ipv4_queue_head) &&
2724 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2725 udp_select_send (plugin, plugin->sockv4);
2726 schedule_select (plugin);
2731 * We have been notified that our readset has something to read. We don't
2732 * know which socket needs to be read, so we have to check each one
2733 * Then reschedule this function to be called again once more is available.
2735 * @param cls the plugin handle
2736 * @param tc the scheduling context (for rescheduling this function again)
2739 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2741 struct Plugin *plugin = cls;
2743 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2744 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2746 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2747 (NULL != plugin->sockv6) &&
2748 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2749 udp_select_read (plugin, plugin->sockv6);
2750 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2751 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2752 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2753 udp_select_send (plugin, plugin->sockv6);
2754 schedule_select (plugin);
2760 * @return number of sockets that were successfully bound
2763 setup_sockets (struct Plugin *plugin,
2764 const struct sockaddr_in6 *bind_v6,
2765 const struct sockaddr_in *bind_v4)
2768 int sockets_created = 0;
2769 struct sockaddr_in6 serverAddrv6;
2770 struct sockaddr_in serverAddrv4;
2771 struct sockaddr *serverAddr;
2772 struct sockaddr *addrs[2];
2773 socklen_t addrlens[2];
2777 /* Create IPv6 socket */
2779 if (plugin->enable_ipv6 == GNUNET_YES)
2781 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2782 if (NULL == plugin->sockv6)
2784 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2785 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2786 plugin->enable_ipv6 = GNUNET_NO;
2790 memset (&serverAddrv6, '\0', sizeof (struct sockaddr_in6));
2791 #if HAVE_SOCKADDR_IN_SIN_LEN
2792 serverAddrv6.sin6_len = sizeof (struct sockaddr_in6);
2794 serverAddrv6.sin6_family = AF_INET6;
2795 if (NULL != bind_v6)
2796 serverAddrv6.sin6_addr = bind_v6->sin6_addr;
2798 serverAddrv6.sin6_addr = in6addr_any;
2800 if (0 == plugin->port) /* autodetect */
2801 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2803 serverAddrv6.sin6_port = htons (plugin->port);
2804 addrlen = sizeof (struct sockaddr_in6);
2805 serverAddr = (struct sockaddr *) &serverAddrv6;
2810 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2811 GNUNET_a2s (serverAddr, addrlen));
2813 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2814 serverAddr, addrlen, 0))
2817 if (0 != plugin->port)
2819 tries = 10; /* fail */
2820 break; /* bind failed on specific port */
2823 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2828 GNUNET_NETWORK_socket_close (plugin->sockv6);
2829 plugin->enable_ipv6 = GNUNET_NO;
2830 plugin->sockv6 = NULL;
2833 if (plugin->sockv6 != NULL)
2835 LOG (GNUNET_ERROR_TYPE_DEBUG,
2836 "IPv6 socket created on port %s\n",
2837 GNUNET_a2s (serverAddr, addrlen));
2838 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2839 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2844 LOG (GNUNET_ERROR_TYPE_ERROR,
2845 "Failed to bind UDP socket to %s: %s\n",
2846 GNUNET_a2s (serverAddr, addrlen),
2852 /* Create IPv4 socket */
2854 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2855 if (NULL == plugin->sockv4)
2857 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2858 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2859 plugin->enable_ipv4 = GNUNET_NO;
2863 memset (&serverAddrv4, '\0', sizeof (struct sockaddr_in));
2864 #if HAVE_SOCKADDR_IN_SIN_LEN
2865 serverAddrv4.sin_len = sizeof (struct sockaddr_in);
2867 serverAddrv4.sin_family = AF_INET;
2868 if (NULL != bind_v4)
2869 serverAddrv4.sin_addr = bind_v4->sin_addr;
2871 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2873 if (0 == plugin->port)
2875 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2877 serverAddrv4.sin_port = htons (plugin->port);
2880 addrlen = sizeof (struct sockaddr_in);
2881 serverAddr = (struct sockaddr *) &serverAddrv4;
2886 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2887 GNUNET_a2s (serverAddr, addrlen));
2890 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2891 serverAddr, addrlen, 0))
2894 if (0 != plugin->port)
2896 tries = 10; /* fail */
2897 break; /* bind failed on specific port */
2901 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2907 GNUNET_NETWORK_socket_close (plugin->sockv4);
2908 plugin->enable_ipv4 = GNUNET_NO;
2909 plugin->sockv4 = NULL;
2912 if (plugin->sockv4 != NULL)
2914 LOG (GNUNET_ERROR_TYPE_DEBUG,
2915 "IPv4 socket created on port %s\n", GNUNET_a2s (serverAddr, addrlen));
2916 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2917 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2922 LOG (GNUNET_ERROR_TYPE_ERROR,
2923 "Failed to bind UDP socket to %s: %s\n",
2924 GNUNET_a2s (serverAddr, addrlen), STRERROR (eno));
2928 if (0 == sockets_created)
2930 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2931 return 0; /* No sockets created, return */
2934 /* Create file descriptors */
2935 if (plugin->enable_ipv4 == GNUNET_YES)
2937 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2938 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2939 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2940 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2941 if (NULL != plugin->sockv4)
2943 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2944 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2948 if (plugin->enable_ipv6 == GNUNET_YES)
2950 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2951 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2952 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2953 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2954 if (NULL != plugin->sockv6)
2956 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2957 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2961 schedule_select (plugin);
2962 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2963 GNUNET_NO, plugin->port,
2965 (const struct sockaddr **) addrs, addrlens,
2966 &udp_nat_port_map_callback, NULL, plugin);
2968 return sockets_created;
2973 * The exported method. Makes the core api available via a global and
2974 * returns the udp transport API.
2976 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2977 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2980 libgnunet_plugin_transport_udp_init (void *cls)
2982 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2983 struct GNUNET_TRANSPORT_PluginFunctions *api;
2985 unsigned long long port;
2986 unsigned long long aport;
2987 unsigned long long broadcast;
2988 unsigned long long udp_max_bps;
2989 unsigned long long enable_v6;
2990 char * bind4_address;
2991 char * bind6_address;
2992 char * fancy_interval;
2993 struct GNUNET_TIME_Relative interval;
2994 struct sockaddr_in serverAddrv4;
2995 struct sockaddr_in6 serverAddrv6;
3000 if (NULL == env->receive)
3002 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3003 initialze the plugin or the API */
3004 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3006 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3007 api->address_to_string = &udp_address_to_string;
3008 api->string_to_address = &udp_string_to_address;
3012 GNUNET_assert (NULL != env->stats);
3014 /* Get port number: port == 0 : autodetect a port,
3015 * > 0 : use this port,
3016 * not given : 2086 default */
3018 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
3022 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3023 "ADVERTISED_PORT", &aport))
3027 LOG (GNUNET_ERROR_TYPE_WARNING,
3028 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3035 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3037 enable_v6 = GNUNET_NO;
3039 enable_v6 = GNUNET_YES;
3042 have_bind4 = GNUNET_NO;
3043 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
3045 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3046 "BINDTO", &bind4_address))
3048 LOG (GNUNET_ERROR_TYPE_DEBUG,
3049 "Binding udp plugin to specific address: `%s'\n",
3051 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
3053 GNUNET_free (bind4_address);
3056 have_bind4 = GNUNET_YES;
3058 GNUNET_free_non_null (bind4_address);
3059 have_bind6 = GNUNET_NO;
3060 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
3062 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3063 "BINDTO6", &bind6_address))
3065 LOG (GNUNET_ERROR_TYPE_DEBUG,
3066 "Binding udp plugin to specific address: `%s'\n",
3069 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
3071 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3073 GNUNET_free (bind6_address);
3076 have_bind6 = GNUNET_YES;
3078 GNUNET_free_non_null (bind6_address);
3080 /* Initialize my flags */
3083 /* Enable neighbour discovery */
3084 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3086 if (broadcast == GNUNET_SYSERR)
3087 broadcast = GNUNET_NO;
3089 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3090 "BROADCAST_INTERVAL", &fancy_interval))
3092 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3096 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3098 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3100 GNUNET_free (fancy_interval);
3103 /* Maximum datarate */
3104 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3105 "MAX_BPS", &udp_max_bps))
3107 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3110 p = GNUNET_malloc (sizeof (struct Plugin));
3113 p->broadcast_interval = interval;
3114 p->enable_ipv6 = enable_v6;
3115 p->enable_ipv4 = GNUNET_YES; /* default */
3117 p->sessions = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
3118 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3119 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3120 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3121 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
3124 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3125 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &serverAddrv6 : NULL,
3126 (GNUNET_YES == have_bind4) ? &serverAddrv4 : NULL);
3127 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
3129 LOG (GNUNET_ERROR_TYPE_ERROR,
3130 _("Failed to create network sockets, plugin failed\n"));
3131 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
3132 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3133 GNUNET_SERVER_mst_destroy (p->mst);
3137 else if (broadcast == GNUNET_YES)
3139 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3140 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
3143 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3146 api->disconnect = &udp_disconnect;
3147 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3148 api->address_to_string = &udp_address_to_string;
3149 api->string_to_address = &udp_string_to_address;
3150 api->check_address = &udp_plugin_check_address;
3151 api->get_session = &udp_plugin_get_session;
3152 api->send = &udp_plugin_send;
3153 api->get_network = &udp_get_network;
3160 heap_cleanup_iterator (void *cls,
3161 struct GNUNET_CONTAINER_HeapNode *
3162 node, void *element,
3163 GNUNET_CONTAINER_HeapCostType
3166 struct DefragContext * d_ctx = element;
3168 GNUNET_CONTAINER_heap_remove_node (node);
3169 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3170 GNUNET_free (d_ctx);
3177 * The exported method. Makes the core api available via a global and
3178 * returns the udp transport API.
3180 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3184 libgnunet_plugin_transport_udp_done (void *cls)
3186 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3187 struct Plugin *plugin = api->cls;
3188 struct PrettyPrinterContext *cur;
3189 struct PrettyPrinterContext *next;
3197 stop_broadcast (plugin);
3198 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3200 GNUNET_SCHEDULER_cancel (plugin->select_task);
3201 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3203 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3205 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3206 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3209 /* Closing sockets */
3210 if (GNUNET_YES ==plugin->enable_ipv4)
3212 if (plugin->sockv4 != NULL)
3214 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3215 plugin->sockv4 = NULL;
3217 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3218 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3220 if (GNUNET_YES ==plugin->enable_ipv6)
3222 if (plugin->sockv6 != NULL)
3224 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3225 plugin->sockv6 = NULL;
3227 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3228 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3231 if (NULL != plugin->nat)
3232 GNUNET_NAT_unregister (plugin->nat);
3234 if (plugin->defrag_ctxs != NULL)
3236 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3237 heap_cleanup_iterator, NULL);
3238 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3239 plugin->defrag_ctxs = NULL;
3241 if (plugin->mst != NULL)
3243 GNUNET_SERVER_mst_destroy(plugin->mst);
3247 /* Clean up leftover messages */
3248 struct UDP_MessageWrapper * udpw;
3249 udpw = plugin->ipv4_queue_head;
3250 while (udpw != NULL)
3252 struct UDP_MessageWrapper *tmp = udpw->next;
3253 dequeue (plugin, udpw);
3254 call_continuation(udpw, GNUNET_SYSERR);
3259 udpw = plugin->ipv6_queue_head;
3260 while (udpw != NULL)
3262 struct UDP_MessageWrapper *tmp = udpw->next;
3263 dequeue (plugin, udpw);
3264 call_continuation(udpw, GNUNET_SYSERR);
3270 /* Clean up sessions */
3271 LOG (GNUNET_ERROR_TYPE_DEBUG,
3272 "Cleaning up sessions\n");
3273 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3274 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
3276 next = ppc_dll_head;
3277 for (cur = next; NULL != cur; cur = next)
3280 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3281 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3282 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3288 GNUNET_free (plugin);
3291 struct Allocation *allocation;
3292 while (NULL != ahead)
3295 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3296 GNUNET_free (allocation);
3298 struct Allocator *allocator;
3299 while (NULL != aehead)
3302 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3303 GNUNET_free (allocator);
3310 /* end of plugin_transport_udp.c */