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;
199 * Closure for 'process_inbound_tokenized_messages'
201 struct SourceInformation
206 struct GNUNET_PeerIdentity sender;
213 struct Session *session;
215 * Number of bytes in source address.
223 * Closure for 'find_receive_context'.
225 struct FindReceiveContext
228 * Where to store the result.
230 struct DefragContext *rc;
235 const struct sockaddr *addr;
237 struct Session *session;
240 * Number of bytes in 'addr'.
249 * Data structure to track defragmentation contexts based
250 * on the source of the UDP traffic.
256 * Defragmentation context.
258 struct GNUNET_DEFRAGMENT_Context *defrag;
261 * Source address this receive context is for (allocated at the
262 * end of the struct).
264 const struct sockaddr *src_addr;
267 * Reference to master plugin struct.
269 struct Plugin *plugin;
272 * Node in the defrag heap.
274 struct GNUNET_CONTAINER_HeapNode *hnode;
277 * Length of 'src_addr'
285 * Context to send fragmented messages
287 struct UDP_FragmentationContext
290 * Next in linked list
292 struct UDP_FragmentationContext * next;
295 * Previous in linked list
297 struct UDP_FragmentationContext * prev;
302 struct Plugin * plugin;
305 * Handle for GNUNET_FRAGMENT context
307 struct GNUNET_FRAGMENT_Context * frag;
310 * The session this fragmentation context belongs to
312 struct Session * session;
315 * Function to call upon completion of the transmission.
317 GNUNET_TRANSPORT_TransmitContinuation cont;
320 * Closure for 'cont'.
327 struct GNUNET_TIME_Absolute timeout;
330 * Payload size of original unfragmented message
335 * Bytes used to send all fragments on wire including UDP overhead
339 unsigned int fragments_used;
344 struct UDP_MessageWrapper
347 * Session this message belongs to
349 struct Session *session;
355 struct UDP_MessageWrapper *prev;
361 struct UDP_MessageWrapper *next;
365 * According to UDP_MessageType
370 * Message with size msg_size including UDP specific overhead
375 * Size of UDP message to send including UDP specific overhead
380 * Payload size of original message
387 struct GNUNET_TIME_Absolute timeout;
390 * Function to call upon completion of the transmission.
392 GNUNET_TRANSPORT_TransmitContinuation cont;
395 * Closure for 'cont'.
400 * Fragmentation context
401 * frag_ctx == NULL if transport <= MTU
402 * frag_ctx != NULL if transport > MTU
404 struct UDP_FragmentationContext *frag_ctx;
409 * UDP ACK Message-Packet header (after defragmentation).
411 struct UDP_ACK_Message
416 struct GNUNET_MessageHeader header;
419 * Desired delay for flow control
424 * What is the identity of the sender
426 struct GNUNET_PeerIdentity sender;
433 static uint32_t myoptions;
437 * Encapsulation of all of the state of the plugin.
439 struct Plugin * plugin;
443 * We have been notified that our readset has something to read. We don't
444 * know which socket needs to be read, so we have to check each one
445 * Then reschedule this function to be called again once more is available.
447 * @param cls the plugin handle
448 * @param tc the scheduling context (for rescheduling this function again)
451 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
455 * We have been notified that our readset has something to read. We don't
456 * know which socket needs to be read, so we have to check each one
457 * Then reschedule this function to be called again once more is available.
459 * @param cls the plugin handle
460 * @param tc the scheduling context (for rescheduling this function again)
463 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
467 * Start session timeout
470 start_session_timeout (struct Session *s);
473 * Increment session timeout due to activity
476 reschedule_session_timeout (struct Session *s);
482 stop_session_timeout (struct Session *s);
485 * (re)schedule select tasks for this plugin.
487 * @param plugin plugin to reschedule
490 schedule_select (struct Plugin *plugin)
492 struct GNUNET_TIME_Relative min_delay;
493 struct UDP_MessageWrapper *udpw;
495 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
497 /* Find a message ready to send:
498 * Flow delay from other peer is expired or not set (0) */
499 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
500 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
501 min_delay = GNUNET_TIME_relative_min (min_delay,
502 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
504 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
505 GNUNET_SCHEDULER_cancel(plugin->select_task);
508 * - write active set if message is ready
509 * - timeout minimum delay */
510 plugin->select_task =
511 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
512 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
514 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
515 &udp_plugin_select, plugin);
517 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
519 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
520 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
521 min_delay = GNUNET_TIME_relative_min (min_delay,
522 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
524 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
525 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
526 plugin->select_task_v6 =
527 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
528 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
530 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
531 &udp_plugin_select_v6, plugin);
537 * Function called for a quick conversion of the binary address to
538 * a numeric address. Note that the caller must not free the
539 * address and that the next call to this function is allowed
540 * to override the address again.
543 * @param addr binary address
544 * @param addrlen length of the address
545 * @return string representing the same address
548 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
550 static char rbuf[INET6_ADDRSTRLEN + 10];
551 char buf[INET6_ADDRSTRLEN];
555 const struct IPv4UdpAddress *t4;
556 const struct IPv6UdpAddress *t6;
562 if (addrlen == sizeof (struct IPv6UdpAddress))
566 options = ntohl (t6->options);
567 port = ntohs (t6->u6_port);
568 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
571 else if (addrlen == sizeof (struct IPv4UdpAddress))
575 options = ntohl (t4->options);
576 port = ntohs (t4->u4_port);
577 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
580 else if (addrlen == 0)
582 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s", TRANSPORT_SESSION_INBOUND_STRING);
589 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
591 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
592 PLUGIN_NAME, options, buf, port);
598 * Function called to convert a string address to
601 * @param cls closure ('struct Plugin*')
602 * @param addr string address
603 * @param addrlen length of the address
604 * @param buf location to store the buffer
605 * @param added location to store the number of bytes in the buffer.
606 * If the function returns GNUNET_SYSERR, its contents are undefined.
607 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
610 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
611 void **buf, size_t *added)
613 struct sockaddr_storage socket_address;
619 /* Format tcp.options.address:port */
624 if ((NULL == addr) || (addrlen == 0))
627 return GNUNET_SYSERR;
629 if ('\0' != addr[addrlen - 1])
632 return GNUNET_SYSERR;
634 if (strlen (addr) != addrlen - 1)
637 return GNUNET_SYSERR;
639 plugin = GNUNET_strdup (addr);
640 optionstr = strchr (plugin, '.');
641 if (NULL == optionstr)
644 GNUNET_free (plugin);
645 return GNUNET_SYSERR;
649 options = atol (optionstr);
650 address = strchr (optionstr, '.');
654 GNUNET_free (plugin);
655 return GNUNET_SYSERR;
661 GNUNET_STRINGS_to_address_ip (address, strlen (address),
665 GNUNET_free (plugin);
666 return GNUNET_SYSERR;
669 GNUNET_free (plugin);
671 switch (socket_address.ss_family)
675 struct IPv4UdpAddress *u4;
676 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
677 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
678 u4->options = htonl (options);
679 u4->ipv4_addr = in4->sin_addr.s_addr;
680 u4->u4_port = in4->sin_port;
682 *added = sizeof (struct IPv4UdpAddress);
687 struct IPv6UdpAddress *u6;
688 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
689 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
690 u6->options = htonl (options);
691 u6->ipv6_addr = in6->sin6_addr;
692 u6->u6_port = in6->sin6_port;
694 *added = sizeof (struct IPv6UdpAddress);
699 return GNUNET_SYSERR;
705 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
707 struct PrettyPrinterContext *ppc = cls;
708 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PPC %p was not removed!\n", ppc); */
709 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
710 if (NULL != ppc->resolver_handle)
712 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
713 ppc->resolver_handle = NULL;
716 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
722 * Append our port and forward the result.
724 * @param cls a 'struct PrettyPrinterContext'
725 * @param hostname result from DNS resolver
728 append_port (void *cls, const char *hostname)
730 struct PrettyPrinterContext *ppc = cls;
731 struct PrettyPrinterContext *cur;
733 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC callback: %p `%s'\n",ppc, hostname); */
734 if (hostname == NULL)
736 ppc->asc (ppc->asc_cls, NULL);
737 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
738 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
739 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
740 ppc->resolver_handle = NULL;
741 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was removed!\n", ppc); */
745 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
752 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
756 if (GNUNET_YES == ppc->ipv6)
757 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
759 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
760 ppc->asc (ppc->asc_cls, ret);
765 * Convert the transports address to a nice, human-readable
769 * @param type name of the transport that generated the address
770 * @param addr one of the addresses of the host, NULL for the last address
771 * the specific address format depends on the transport
772 * @param addrlen length of the address
773 * @param numeric should (IP) addresses be displayed in numeric form?
774 * @param timeout after how long should we give up?
775 * @param asc function to call on each string
776 * @param asc_cls closure for asc
779 udp_plugin_address_pretty_printer (void *cls, const char *type,
780 const void *addr, size_t addrlen,
782 struct GNUNET_TIME_Relative timeout,
783 GNUNET_TRANSPORT_AddressStringCallback asc,
786 struct PrettyPrinterContext *ppc;
789 struct sockaddr_in a4;
790 struct sockaddr_in6 a6;
791 const struct IPv4UdpAddress *u4;
792 const struct IPv6UdpAddress *u6;
797 if (addrlen == sizeof (struct IPv6UdpAddress))
800 memset (&a6, 0, sizeof (a6));
801 a6.sin6_family = AF_INET6;
802 #if HAVE_SOCKADDR_IN_SIN_LEN
803 a6.sin6_len = sizeof (a6);
805 a6.sin6_port = u6->u6_port;
806 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
807 port = ntohs (u6->u6_port);
808 options = ntohl (u6->options);
812 else if (addrlen == sizeof (struct IPv4UdpAddress))
815 memset (&a4, 0, sizeof (a4));
816 a4.sin_family = AF_INET;
817 #if HAVE_SOCKADDR_IN_SIN_LEN
818 a4.sin_len = sizeof (a4);
820 a4.sin_port = u4->u4_port;
821 a4.sin_addr.s_addr = u4->ipv4_addr;
822 port = ntohs (u4->u4_port);
823 options = ntohl (u4->options);
827 else if (0 == addrlen)
829 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
835 /* invalid address */
840 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
842 ppc->asc_cls = asc_cls;
844 ppc->options = options;
845 if (addrlen == sizeof (struct IPv6UdpAddress))
846 ppc->ipv6 = GNUNET_YES;
848 ppc->ipv6 = GNUNET_NO;
849 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
850 &ppc_cancel_task, ppc);
851 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
852 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was created!\n", ppc); */
853 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
859 call_continuation (struct UDP_MessageWrapper *udpw, int result)
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 "Calling continuation for %u byte message to `%s' with result %s\n",
865 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
866 (GNUNET_OK == result) ? "OK" : "SYSERR");
868 if (udpw->msg_size >= udpw->payload_size)
869 overhead = udpw->msg_size - udpw->payload_size;
871 overhead = udpw->msg_size;
875 switch (udpw->msg_type) {
876 case MSG_UNFRAGMENTED:
877 if (NULL != udpw->cont)
879 /* Transport continuation */
880 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
881 udpw->payload_size, udpw->msg_size);
883 GNUNET_STATISTICS_update (plugin->env->stats,
884 "# UDP, unfragmented msgs, messages, sent, success",
886 GNUNET_STATISTICS_update (plugin->env->stats,
887 "# UDP, unfragmented msgs, bytes payload, sent, success",
888 udpw->payload_size, GNUNET_NO);
889 GNUNET_STATISTICS_update (plugin->env->stats,
890 "# UDP, unfragmented msgs, bytes overhead, sent, success",
891 overhead, GNUNET_NO);
892 GNUNET_STATISTICS_update (plugin->env->stats,
893 "# UDP, total, bytes overhead, sent",
894 overhead, GNUNET_NO);
895 GNUNET_STATISTICS_update (plugin->env->stats,
896 "# UDP, total, bytes payload, sent",
897 udpw->payload_size, GNUNET_NO);
899 case MSG_FRAGMENTED_COMPLETE:
900 GNUNET_assert (NULL != udpw->frag_ctx);
901 if (udpw->frag_ctx->cont != NULL)
902 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
903 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
904 GNUNET_STATISTICS_update (plugin->env->stats,
905 "# UDP, fragmented msgs, messages, sent, success",
907 GNUNET_STATISTICS_update (plugin->env->stats,
908 "# UDP, fragmented msgs, bytes payload, sent, success",
909 udpw->payload_size, GNUNET_NO);
910 GNUNET_STATISTICS_update (plugin->env->stats,
911 "# UDP, fragmented msgs, bytes overhead, sent, success",
912 overhead, GNUNET_NO);
913 GNUNET_STATISTICS_update (plugin->env->stats,
914 "# UDP, total, bytes overhead, sent",
915 overhead, GNUNET_NO);
916 GNUNET_STATISTICS_update (plugin->env->stats,
917 "# UDP, total, bytes payload, sent",
918 udpw->payload_size, GNUNET_NO);
919 GNUNET_STATISTICS_update (plugin->env->stats,
920 "# UDP, fragmented msgs, messages, pending",
924 /* Fragmented message: enqueue next fragment */
925 if (NULL != udpw->cont)
926 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
927 udpw->payload_size, udpw->msg_size);
928 GNUNET_STATISTICS_update (plugin->env->stats,
929 "# UDP, fragmented msgs, fragments, sent, success",
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 "# UDP, fragmented msgs, fragments bytes, sent, success",
933 udpw->msg_size, GNUNET_NO);
936 /* No continuation */
937 GNUNET_STATISTICS_update (plugin->env->stats,
938 "# UDP, ACK msgs, messages, sent, success",
940 GNUNET_STATISTICS_update (plugin->env->stats,
941 "# UDP, ACK msgs, bytes overhead, sent, success",
942 overhead, GNUNET_NO);
943 GNUNET_STATISTICS_update (plugin->env->stats,
944 "# UDP, total, bytes overhead, sent",
945 overhead, GNUNET_NO);
951 LOG (GNUNET_ERROR_TYPE_ERROR,
952 "ERROR: %u\n", udpw->msg_type);
958 switch (udpw->msg_type) {
959 case MSG_UNFRAGMENTED:
960 /* Unfragmented message: failed to send */
961 if (NULL != udpw->cont)
962 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
963 udpw->payload_size, overhead);
964 GNUNET_STATISTICS_update (plugin->env->stats,
965 "# UDP, unfragmented msgs, messages, sent, failure",
967 GNUNET_STATISTICS_update (plugin->env->stats,
968 "# UDP, unfragmented msgs, bytes payload, sent, failure",
969 udpw->payload_size, GNUNET_NO);
970 GNUNET_STATISTICS_update (plugin->env->stats,
971 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
972 overhead, GNUNET_NO);
974 case MSG_FRAGMENTED_COMPLETE:
975 GNUNET_assert (NULL != udpw->frag_ctx);
976 if (udpw->frag_ctx->cont != NULL)
977 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
978 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
979 GNUNET_STATISTICS_update (plugin->env->stats,
980 "# UDP, fragmented msgs, messages, sent, failure",
982 GNUNET_STATISTICS_update (plugin->env->stats,
983 "# UDP, fragmented msgs, bytes payload, sent, failure",
984 udpw->payload_size, GNUNET_NO);
985 GNUNET_STATISTICS_update (plugin->env->stats,
986 "# UDP, fragmented msgs, bytes payload, sent, failure",
987 overhead, GNUNET_NO);
988 GNUNET_STATISTICS_update (plugin->env->stats,
989 "# UDP, fragmented msgs, bytes payload, sent, failure",
990 overhead, GNUNET_NO);
991 GNUNET_STATISTICS_update (plugin->env->stats,
992 "# UDP, fragmented msgs, messages, pending",
996 GNUNET_assert (NULL != udpw->frag_ctx);
997 /* Fragmented message: failed to send */
998 GNUNET_STATISTICS_update (plugin->env->stats,
999 "# UDP, fragmented msgs, fragments, sent, failure",
1001 GNUNET_STATISTICS_update (plugin->env->stats,
1002 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1003 udpw->msg_size, GNUNET_NO);
1006 /* ACK message: failed to send */
1007 GNUNET_STATISTICS_update (plugin->env->stats,
1008 "# UDP, ACK msgs, messages, sent, failure",
1012 /* Beacon message: failed to send */
1028 * Check if the given port is plausible (must be either our listen
1029 * port or our advertised port). If it is neither, we return
1032 * @param plugin global variables
1033 * @param in_port port number to check
1034 * @return GNUNET_OK if port is either open_port or adv_port
1037 check_port (struct Plugin *plugin, uint16_t in_port)
1039 if ((in_port == plugin->port) || (in_port == plugin->aport))
1041 return GNUNET_SYSERR;
1046 * Function that will be called to check if a binary address for this
1047 * plugin is well-formed and corresponds to an address for THIS peer
1048 * (as per our configuration). Naturally, if absolutely necessary,
1049 * plugins can be a bit conservative in their answer, but in general
1050 * plugins should make sure that the address does not redirect
1051 * traffic to a 3rd party that might try to man-in-the-middle our
1054 * @param cls closure, should be our handle to the Plugin
1055 * @param addr pointer to the address
1056 * @param addrlen length of addr
1057 * @return GNUNET_OK if this is a plausible address for this peer
1058 * and transport, GNUNET_SYSERR if not
1062 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1064 struct Plugin *plugin = cls;
1065 struct IPv4UdpAddress *v4;
1066 struct IPv6UdpAddress *v6;
1068 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1069 (addrlen != sizeof (struct IPv6UdpAddress)))
1071 return GNUNET_SYSERR;
1073 if (addrlen == sizeof (struct IPv4UdpAddress))
1075 v4 = (struct IPv4UdpAddress *) addr;
1076 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1077 return GNUNET_SYSERR;
1079 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1080 sizeof (struct in_addr)))
1081 return GNUNET_SYSERR;
1085 v6 = (struct IPv6UdpAddress *) addr;
1086 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1088 GNUNET_break_op (0);
1089 return GNUNET_SYSERR;
1091 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1092 return GNUNET_SYSERR;
1094 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1095 sizeof (struct in6_addr)))
1096 return GNUNET_SYSERR;
1103 * Task to free resources associated with a session.
1105 * @param s session to free
1108 free_session (struct Session *s)
1110 if (NULL != s->frag_ctx)
1112 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
1113 GNUNET_free (s->frag_ctx);
1121 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1123 if (plugin->bytes_in_buffer < udpw->msg_size)
1127 GNUNET_STATISTICS_update (plugin->env->stats,
1128 "# UDP, total, bytes in buffers",
1129 - (long long) udpw->msg_size, GNUNET_NO);
1130 plugin->bytes_in_buffer -= udpw->msg_size;
1132 GNUNET_STATISTICS_update (plugin->env->stats,
1133 "# UDP, total, msgs in buffers",
1135 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1136 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1137 plugin->ipv4_queue_tail, udpw);
1138 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1139 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1140 plugin->ipv6_queue_tail, udpw);
1144 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1146 struct UDP_MessageWrapper *udpw;
1147 struct UDP_MessageWrapper *tmp;
1148 struct UDP_MessageWrapper dummy;
1149 struct Session *s = fc->session;
1151 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1153 /* Call continuation for fragmented message */
1154 memset (&dummy, 0, sizeof (dummy));
1155 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1156 dummy.msg_size = s->frag_ctx->on_wire_size;
1157 dummy.payload_size = s->frag_ctx->payload_size;
1158 dummy.frag_ctx = s->frag_ctx;
1160 dummy.cont_cls = NULL;
1163 call_continuation (&dummy, result);
1165 /* Remove leftover fragments from queue */
1166 if (s->addrlen == sizeof (struct sockaddr_in6))
1168 udpw = plugin->ipv6_queue_head;
1169 while (NULL != udpw)
1172 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1174 dequeue (plugin, udpw);
1175 call_continuation (udpw, GNUNET_SYSERR);
1181 if (s->addrlen == sizeof (struct sockaddr_in))
1183 udpw = plugin->ipv4_queue_head;
1187 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1189 dequeue (plugin, udpw);
1190 call_continuation (udpw, GNUNET_SYSERR);
1197 /* Destroy fragmentation context */
1198 GNUNET_FRAGMENT_context_destroy (fc->frag,
1199 &s->last_expected_msg_delay,
1200 &s->last_expected_ack_delay);
1206 * Functions with this signature are called whenever we need
1207 * to close a session due to a disconnect or failure to
1208 * establish a connection.
1210 * @param s session to close down
1213 disconnect_session (struct Session *s)
1215 struct UDP_MessageWrapper *udpw;
1216 struct UDP_MessageWrapper *next;
1218 GNUNET_assert (GNUNET_YES != s->in_destroy);
1219 LOG (GNUNET_ERROR_TYPE_DEBUG,
1220 "Session %p to peer `%s' address ended \n",
1222 GNUNET_i2s (&s->target),
1223 GNUNET_a2s (s->sock_addr, s->addrlen));
1224 stop_session_timeout (s);
1226 if (NULL != s->frag_ctx)
1228 /* Remove fragmented message due to disconnect */
1229 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1232 next = plugin->ipv4_queue_head;
1233 while (NULL != (udpw = next))
1236 if (udpw->session == s)
1238 dequeue (plugin, udpw);
1239 call_continuation(udpw, GNUNET_SYSERR);
1243 next = plugin->ipv6_queue_head;
1244 while (NULL != (udpw = next))
1247 if (udpw->session == s)
1249 dequeue (plugin, udpw);
1250 call_continuation(udpw, GNUNET_SYSERR);
1255 plugin->env->session_end (plugin->env->cls, &s->target, s);
1257 if (NULL != s->frag_ctx)
1259 if (NULL != s->frag_ctx->cont)
1261 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1262 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1263 LOG (GNUNET_ERROR_TYPE_DEBUG,
1264 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1265 GNUNET_i2s (&s->target));
1269 GNUNET_assert (GNUNET_YES ==
1270 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1271 &s->target.hashPubKey,
1273 GNUNET_STATISTICS_set(plugin->env->stats,
1274 "# UDP, sessions active",
1275 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1278 s->in_destroy = GNUNET_YES;
1284 * Destroy a session, plugin is being unloaded.
1287 * @param key hash of public key of target peer
1288 * @param value a 'struct PeerSession*' to clean up
1289 * @return GNUNET_OK (continue to iterate)
1292 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1294 disconnect_session(value);
1300 * Disconnect from a remote node. Clean up session if we have one for this peer
1302 * @param cls closure for this call (should be handle to Plugin)
1303 * @param target the peeridentity of the peer to disconnect
1304 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1307 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1309 struct Plugin *plugin = cls;
1310 GNUNET_assert (plugin != NULL);
1312 GNUNET_assert (target != NULL);
1313 LOG (GNUNET_ERROR_TYPE_DEBUG,
1314 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1315 /* Clean up sessions */
1316 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1321 * Session was idle, so disconnect it
1324 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1326 GNUNET_assert (NULL != cls);
1327 struct Session *s = cls;
1329 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Session %p was idle for %s, disconnecting\n",
1333 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1335 /* call session destroy function */
1336 disconnect_session (s);
1341 * Start session timeout
1344 start_session_timeout (struct Session *s)
1346 GNUNET_assert (NULL != s);
1347 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1348 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "Timeout for session %p set to %s\n",
1354 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1360 * Increment session timeout due to activity
1363 reschedule_session_timeout (struct Session *s)
1365 GNUNET_assert (NULL != s);
1366 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1368 GNUNET_SCHEDULER_cancel (s->timeout_task);
1369 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373 "Timeout rescheduled for session %p set to %s\n",
1375 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1384 stop_session_timeout (struct Session *s)
1386 GNUNET_assert (NULL != s);
1388 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1390 GNUNET_SCHEDULER_cancel (s->timeout_task);
1391 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Timeout stopped for session %p canceled\n",
1399 static struct Session *
1400 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1401 const void *addr, size_t addrlen,
1402 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1405 const struct IPv4UdpAddress *t4;
1406 const struct IPv6UdpAddress *t6;
1407 struct sockaddr_in *v4;
1408 struct sockaddr_in6 *v6;
1413 case sizeof (struct IPv4UdpAddress):
1414 if (NULL == plugin->sockv4)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1417 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1419 udp_address_to_string(NULL, addr, addrlen));
1423 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1424 len = sizeof (struct sockaddr_in);
1425 v4 = (struct sockaddr_in *) &s[1];
1426 v4->sin_family = AF_INET;
1427 #if HAVE_SOCKADDR_IN_SIN_LEN
1428 v4->sin_len = sizeof (struct sockaddr_in);
1430 v4->sin_port = t4->u4_port;
1431 v4->sin_addr.s_addr = t4->ipv4_addr;
1432 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1434 case sizeof (struct IPv6UdpAddress):
1435 if (NULL == plugin->sockv6)
1437 LOG (GNUNET_ERROR_TYPE_INFO,
1438 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1440 udp_address_to_string(NULL, addr, addrlen));
1445 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1446 len = sizeof (struct sockaddr_in6);
1447 v6 = (struct sockaddr_in6 *) &s[1];
1448 v6->sin6_family = AF_INET6;
1449 #if HAVE_SOCKADDR_IN_SIN_LEN
1450 v6->sin6_len = sizeof (struct sockaddr_in6);
1452 v6->sin6_port = t6->u6_port;
1453 v6->sin6_addr = t6->ipv6_addr;
1454 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1457 /* Must have a valid address to send to */
1458 GNUNET_STATISTICS_update (plugin->env->stats,
1460 ("# requests to create session with invalid address"),
1465 s->target = *target;
1466 s->sock_addr = (const struct sockaddr *) &s[1];
1467 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1468 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1469 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1470 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1471 s->inbound = GNUNET_NO;
1472 start_session_timeout (s);
1478 session_cmp_it (void *cls,
1479 const struct GNUNET_HashCode * key,
1482 struct SessionCompareContext * cctx = cls;
1483 const struct GNUNET_HELLO_Address *address = cctx->addr;
1484 struct Session *s = value;
1486 socklen_t s_addrlen = s->addrlen;
1488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1489 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1490 GNUNET_a2s (s->sock_addr, s->addrlen));
1491 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1492 (s_addrlen == sizeof (struct sockaddr_in)))
1494 struct IPv4UdpAddress * u4 = NULL;
1495 u4 = (struct IPv4UdpAddress *) address->address;
1496 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1497 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1498 (u4->u4_port == s4->sin_port))
1505 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1506 (s_addrlen == sizeof (struct sockaddr_in6)))
1508 struct IPv6UdpAddress * u6 = NULL;
1509 u6 = (struct IPv6UdpAddress *) address->address;
1510 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1511 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1512 (u6->u6_port == s6->sin6_port))
1523 * Function obtain the network type for a session
1525 * @param cls closure ('struct Plugin*')
1526 * @param session the session
1527 * @return the network type in HBO or GNUNET_SYSERR
1529 static enum GNUNET_ATS_Network_Type
1530 udp_get_network (void *cls,
1531 struct Session *session)
1533 return ntohl (session->ats.value);
1538 * Creates a new outbound session the transport service will use to send data to the
1541 * @param cls the plugin
1542 * @param address the address
1543 * @return the session or NULL of max connections exceeded
1545 static struct Session *
1546 udp_plugin_lookup_session (void *cls,
1547 const struct GNUNET_HELLO_Address *address)
1549 struct Plugin * plugin = cls;
1550 struct IPv6UdpAddress * udp_a6;
1551 struct IPv4UdpAddress * udp_a4;
1553 GNUNET_assert (plugin != NULL);
1554 GNUNET_assert (address != NULL);
1557 if ((address->address == NULL) ||
1558 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1559 (address->address_length != sizeof (struct IPv6UdpAddress))))
1561 LOG (GNUNET_ERROR_TYPE_WARNING,
1562 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1563 address->address_length,
1564 sizeof (struct IPv4UdpAddress),
1565 sizeof (struct IPv6UdpAddress));
1569 if (address->address_length == sizeof (struct IPv4UdpAddress))
1571 if (plugin->sockv4 == NULL)
1573 udp_a4 = (struct IPv4UdpAddress *) address->address;
1574 if (udp_a4->u4_port == 0)
1578 if (address->address_length == sizeof (struct IPv6UdpAddress))
1580 if (plugin->sockv6 == NULL)
1582 udp_a6 = (struct IPv6UdpAddress *) address->address;
1583 if (udp_a6->u6_port == 0)
1587 /* check if session already exists */
1588 struct SessionCompareContext cctx;
1589 cctx.addr = address;
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_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1596 if (cctx.res != NULL)
1598 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1604 static struct Session *
1605 udp_plugin_create_session (void *cls,
1606 const struct GNUNET_HELLO_Address *address)
1608 struct Session * s = NULL;
1610 /* otherwise create new */
1611 s = create_session (plugin,
1614 address->address_length,
1617 return NULL; /* protocol not supported or address invalid */
1618 LOG (GNUNET_ERROR_TYPE_DEBUG,
1619 "Creating new session %p for peer `%s' address `%s'\n",
1621 GNUNET_i2s(&address->peer),
1622 udp_address_to_string(NULL,address->address,address->address_length));
1623 GNUNET_assert (GNUNET_OK ==
1624 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1625 &s->target.hashPubKey,
1627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1628 GNUNET_STATISTICS_set(plugin->env->stats,
1629 "# UDP, sessions active",
1630 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1638 * Creates a new outbound session the transport service will use to send data to the
1641 * @param cls the plugin
1642 * @param address the address
1643 * @return the session or NULL of max connections exceeded
1645 static struct Session *
1646 udp_plugin_get_session (void *cls,
1647 const struct GNUNET_HELLO_Address *address)
1649 struct Session * s = NULL;
1651 if (NULL == address)
1656 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1657 (address->address_length != sizeof (struct IPv6UdpAddress)))
1660 /* otherwise create new */
1661 if (NULL != (s = udp_plugin_lookup_session(cls, address)))
1664 return udp_plugin_create_session (cls, address);
1669 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1671 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1675 GNUNET_STATISTICS_update (plugin->env->stats,
1676 "# UDP, total, bytes in buffers",
1677 udpw->msg_size, GNUNET_NO);
1678 plugin->bytes_in_buffer += udpw->msg_size;
1680 GNUNET_STATISTICS_update (plugin->env->stats,
1681 "# UDP, total, msgs in buffers",
1683 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1684 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1685 plugin->ipv4_queue_tail, udpw);
1686 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1687 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1688 plugin->ipv6_queue_tail, udpw);
1694 * Fragment message was transmitted via UDP, let fragmentation know
1695 * to send the next fragment now.
1697 * @param cls the 'struct UDPMessageWrapper' of the fragment
1698 * @param target destination peer (ignored)
1699 * @param result GNUNET_OK on success (ignored)
1700 * @param payload bytes payload sent
1701 * @param physical bytes physical sent
1704 send_next_fragment (void *cls,
1705 const struct GNUNET_PeerIdentity *target,
1706 int result, size_t payload, size_t physical)
1708 struct UDP_MessageWrapper *udpw = cls;
1709 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1714 * Function that is called with messages created by the fragmentation
1715 * module. In the case of the 'proc' callback of the
1716 * GNUNET_FRAGMENT_context_create function, this function must
1717 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1719 * @param cls closure, the 'struct FragmentationContext'
1720 * @param msg the message that was created
1723 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1725 struct UDP_FragmentationContext *frag_ctx = cls;
1726 struct Plugin *plugin = frag_ctx->plugin;
1727 struct UDP_MessageWrapper * udpw;
1728 size_t msg_len = ntohs (msg->size);
1730 LOG (GNUNET_ERROR_TYPE_DEBUG,
1731 "Enqueuing fragment with %u bytes\n", msg_len);
1732 frag_ctx->fragments_used ++;
1733 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1734 udpw->session = frag_ctx->session;
1735 udpw->msg_buf = (char *) &udpw[1];
1736 udpw->msg_size = msg_len;
1737 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1738 udpw->cont = &send_next_fragment;
1739 udpw->cont_cls = udpw;
1740 udpw->timeout = frag_ctx->timeout;
1741 udpw->frag_ctx = frag_ctx;
1742 udpw->msg_type = MSG_FRAGMENTED;
1743 memcpy (udpw->msg_buf, msg, msg_len);
1744 enqueue (plugin, udpw);
1745 schedule_select (plugin);
1750 * Function that can be used by the transport service to transmit
1751 * a message using the plugin. Note that in the case of a
1752 * peer disconnecting, the continuation MUST be called
1753 * prior to the disconnect notification itself. This function
1754 * will be called with this peer's HELLO message to initiate
1755 * a fresh connection to another peer.
1757 * @param cls closure
1758 * @param s which session must be used
1759 * @param msgbuf the message to transmit
1760 * @param msgbuf_size number of bytes in 'msgbuf'
1761 * @param priority how important is the message (most plugins will
1762 * ignore message priority and just FIFO)
1763 * @param to how long to wait at most for the transmission (does not
1764 * require plugins to discard the message after the timeout,
1765 * just advisory for the desired delay; most plugins will ignore
1767 * @param cont continuation to call once the message has
1768 * been transmitted (or if the transport is ready
1769 * for the next transmission call; or if the
1770 * peer disconnected...); can be NULL
1771 * @param cont_cls closure for cont
1772 * @return number of bytes used (on the physical network, with overheads);
1773 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1774 * and does NOT mean that the message was not transmitted (DV)
1777 udp_plugin_send (void *cls,
1779 const char *msgbuf, size_t msgbuf_size,
1780 unsigned int priority,
1781 struct GNUNET_TIME_Relative to,
1782 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1784 struct Plugin *plugin = cls;
1785 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1786 struct UDP_FragmentationContext * frag_ctx;
1787 struct UDP_MessageWrapper * udpw;
1788 struct UDPMessage *udp;
1790 GNUNET_assert (plugin != NULL);
1791 GNUNET_assert (s != NULL);
1793 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1794 return GNUNET_SYSERR;
1795 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1796 return GNUNET_SYSERR;
1797 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1800 return GNUNET_SYSERR;
1802 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1805 return GNUNET_SYSERR;
1807 LOG (GNUNET_ERROR_TYPE_DEBUG,
1808 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1810 GNUNET_i2s (&s->target),
1811 GNUNET_a2s(s->sock_addr, s->addrlen));
1815 udp = (struct UDPMessage *) mbuf;
1816 udp->header.size = htons (udpmlen);
1817 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1818 udp->reserved = htonl (0);
1819 udp->sender = *plugin->env->my_identity;
1821 reschedule_session_timeout(s);
1822 if (udpmlen <= UDP_MTU)
1824 /* unfragmented message */
1825 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1827 udpw->msg_buf = (char *) &udpw[1];
1828 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1829 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1830 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1832 udpw->cont_cls = cont_cls;
1833 udpw->frag_ctx = NULL;
1834 udpw->msg_type = MSG_UNFRAGMENTED;
1835 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1836 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1837 enqueue (plugin, udpw);
1839 GNUNET_STATISTICS_update (plugin->env->stats,
1840 "# UDP, unfragmented msgs, messages, attempt",
1842 GNUNET_STATISTICS_update (plugin->env->stats,
1843 "# UDP, unfragmented msgs, bytes payload, attempt",
1844 udpw->payload_size, GNUNET_NO);
1848 /* fragmented message */
1849 if (s->frag_ctx != NULL)
1850 return GNUNET_SYSERR;
1851 memcpy (&udp[1], msgbuf, msgbuf_size);
1852 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1853 frag_ctx->plugin = plugin;
1854 frag_ctx->session = s;
1855 frag_ctx->cont = cont;
1856 frag_ctx->cont_cls = cont_cls;
1857 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1858 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1859 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1860 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1863 s->last_expected_msg_delay,
1864 s->last_expected_ack_delay,
1868 s->frag_ctx = frag_ctx;
1869 GNUNET_STATISTICS_update (plugin->env->stats,
1870 "# UDP, fragmented msgs, messages, pending",
1872 GNUNET_STATISTICS_update (plugin->env->stats,
1873 "# UDP, fragmented msgs, messages, attempt",
1875 GNUNET_STATISTICS_update (plugin->env->stats,
1876 "# UDP, fragmented msgs, bytes payload, attempt",
1877 frag_ctx->payload_size, GNUNET_NO);
1879 schedule_select (plugin);
1885 * Our external IP address/port mapping has changed.
1887 * @param cls closure, the 'struct LocalAddrList'
1888 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1889 * the previous (now invalid) one
1890 * @param addr either the previous or the new public IP address
1891 * @param addrlen actual lenght of the address
1894 udp_nat_port_map_callback (void *cls, int add_remove,
1895 const struct sockaddr *addr, socklen_t addrlen)
1897 struct Plugin *plugin = cls;
1898 struct IPv4UdpAddress u4;
1899 struct IPv6UdpAddress u6;
1903 LOG (GNUNET_ERROR_TYPE_INFO,
1904 "NAT notification to %s address `%s'\n",
1905 (GNUNET_YES == add_remove) ? "add" : "remove",
1906 GNUNET_a2s (addr, addrlen));
1908 /* convert 'addr' to our internal format */
1909 switch (addr->sa_family)
1912 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1913 memset (&u4, 0, sizeof (u4));
1914 u4.options = htonl(myoptions);
1915 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1916 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1917 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1920 args = sizeof (struct IPv4UdpAddress);
1923 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1924 memset (&u4, 0, sizeof (u4));
1925 u6.options = htonl(myoptions);
1926 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1928 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1929 sizeof (struct in6_addr));
1930 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1932 args = sizeof (struct IPv6UdpAddress);
1938 /* modify our published address list */
1939 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1945 * Message tokenizer has broken up an incomming message. Pass it on
1948 * @param cls the 'struct Plugin'
1949 * @param client the 'struct SourceInformation'
1950 * @param hdr the actual message
1953 process_inbound_tokenized_messages (void *cls, void *client,
1954 const struct GNUNET_MessageHeader *hdr)
1956 struct Plugin *plugin = cls;
1957 struct SourceInformation *si = client;
1958 struct GNUNET_TIME_Relative delay;
1960 GNUNET_assert (si->session != NULL);
1961 if (GNUNET_YES == si->session->in_destroy)
1964 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1965 delay = plugin->env->receive (plugin->env->cls,
1969 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1970 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1972 plugin->env->update_address_metrics (plugin->env->cls,
1974 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1975 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1977 &si->session->ats, 1);
1979 si->session->flow_delay_for_other_peer = delay;
1980 reschedule_session_timeout(si->session);
1986 * We've received a UDP Message. Process it (pass contents to main service).
1988 * @param plugin plugin context
1989 * @param msg the message
1990 * @param sender_addr sender address
1991 * @param sender_addr_len number of bytes in sender_addr
1994 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1995 const struct sockaddr *sender_addr,
1996 socklen_t sender_addr_len)
1998 struct SourceInformation si;
2000 struct IPv4UdpAddress u4;
2001 struct IPv6UdpAddress u6;
2005 if (0 != ntohl (msg->reserved))
2007 GNUNET_break_op (0);
2010 if (ntohs (msg->header.size) <
2011 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2013 GNUNET_break_op (0);
2017 /* convert address */
2018 switch (sender_addr->sa_family)
2021 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2022 memset (&u4, 0, sizeof (u4));
2023 u6.options = htonl (0);
2024 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2025 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2030 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2031 memset (&u6, 0, sizeof (u6));
2032 u6.options = htonl (0);
2033 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2034 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2042 LOG (GNUNET_ERROR_TYPE_DEBUG,
2043 "Received message with %u bytes from peer `%s' at `%s'\n",
2044 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2045 GNUNET_a2s (sender_addr, sender_addr_len));
2047 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2048 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2050 s = udp_plugin_create_session(plugin, address);
2051 s->inbound = GNUNET_YES;
2052 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2053 (GNUNET_YES == s->inbound) ? NULL : address->address,
2054 (GNUNET_YES == s->inbound) ? 0 : address->address_length,
2057 GNUNET_free (address);
2059 /* iterate over all embedded messages */
2061 si.sender = msg->sender;
2065 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2066 ntohs (msg->header.size) -
2067 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
2069 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2075 * Scan the heap for a receive context with the given address.
2077 * @param cls the 'struct FindReceiveContext'
2078 * @param node internal node of the heap
2079 * @param element value stored at the node (a 'struct ReceiveContext')
2080 * @param cost cost associated with the node
2081 * @return GNUNET_YES if we should continue to iterate,
2085 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2086 void *element, GNUNET_CONTAINER_HeapCostType cost)
2088 struct FindReceiveContext *frc = cls;
2089 struct DefragContext *e = element;
2091 if ((frc->addr_len == e->addr_len) &&
2092 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2102 * Process a defragmented message.
2104 * @param cls the 'struct ReceiveContext'
2105 * @param msg the message
2108 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2110 struct DefragContext *rc = cls;
2112 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2117 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2122 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2123 rc->src_addr, rc->addr_len);
2127 struct LookupContext
2129 const struct sockaddr * addr;
2131 struct Session *res;
2138 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
2140 struct LookupContext *l_ctx = cls;
2141 struct Session * s = value;
2143 if ((s->addrlen == l_ctx->addrlen) &&
2144 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2154 * Transmit an acknowledgement.
2156 * @param cls the 'struct ReceiveContext'
2157 * @param id message ID (unused)
2158 * @param msg ack to transmit
2161 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2163 struct DefragContext *rc = cls;
2164 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2165 struct UDP_ACK_Message *udp_ack;
2167 struct UDP_MessageWrapper *udpw;
2169 struct LookupContext l_ctx;
2171 l_ctx.addr = rc->src_addr;
2172 l_ctx.addrlen = rc->addr_len;
2174 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
2175 &lookup_session_by_addr_it,
2182 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2183 delay = s->flow_delay_for_other_peer.rel_value_us;
2185 LOG (GNUNET_ERROR_TYPE_DEBUG,
2186 "Sending ACK to `%s' including delay of %s\n",
2187 GNUNET_a2s (rc->src_addr,
2188 (rc->src_addr->sa_family ==
2189 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2191 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2193 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2194 udpw->msg_size = msize;
2195 udpw->payload_size = 0;
2197 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2198 udpw->msg_buf = (char *)&udpw[1];
2199 udpw->msg_type = MSG_ACK;
2200 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2201 udp_ack->header.size = htons ((uint16_t) msize);
2202 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2203 udp_ack->delay = htonl (delay);
2204 udp_ack->sender = *rc->plugin->env->my_identity;
2205 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2206 enqueue (rc->plugin, udpw);
2211 read_process_msg (struct Plugin *plugin,
2212 const struct GNUNET_MessageHeader *msg,
2216 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2218 GNUNET_break_op (0);
2221 process_udp_message (plugin, (const struct UDPMessage *) msg,
2222 (const struct sockaddr *) addr, fromlen);
2227 read_process_ack (struct Plugin *plugin,
2228 const struct GNUNET_MessageHeader *msg,
2232 const struct GNUNET_MessageHeader *ack;
2233 const struct UDP_ACK_Message *udp_ack;
2234 struct LookupContext l_ctx;
2236 struct GNUNET_TIME_Relative flow_delay;
2238 if (ntohs (msg->size) <
2239 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2241 GNUNET_break_op (0);
2244 udp_ack = (const struct UDP_ACK_Message *) msg;
2245 l_ctx.addr = (const struct sockaddr *) addr;
2246 l_ctx.addrlen = fromlen;
2248 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2249 &lookup_session_by_addr_it,
2253 if ((NULL == s) || (NULL == s->frag_ctx))
2258 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2259 LOG (GNUNET_ERROR_TYPE_DEBUG,
2260 "We received a sending delay of %s\n",
2261 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2263 s->flow_delay_from_other_peer =
2264 GNUNET_TIME_relative_to_absolute (flow_delay);
2266 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2267 if (ntohs (ack->size) !=
2268 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2270 GNUNET_break_op (0);
2274 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2276 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2278 LOG (GNUNET_ERROR_TYPE_DEBUG,
2279 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2280 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2281 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2282 /* Expect more ACKs to arrive */
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Message full ACK'ed\n",
2288 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2289 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2291 /* Remove fragmented message after successful sending */
2292 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2297 read_process_fragment (struct Plugin *plugin,
2298 const struct GNUNET_MessageHeader *msg,
2302 struct DefragContext *d_ctx;
2303 struct GNUNET_TIME_Absolute now;
2304 struct FindReceiveContext frc;
2307 frc.addr = (const struct sockaddr *) addr;
2308 frc.addr_len = fromlen;
2310 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2311 (unsigned int) ntohs (msg->size),
2312 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2313 /* Lookup existing receive context for this address */
2314 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2315 &find_receive_context,
2317 now = GNUNET_TIME_absolute_get ();
2322 /* Create a new defragmentation context */
2323 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2324 memcpy (&d_ctx[1], addr, fromlen);
2325 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2326 d_ctx->addr_len = fromlen;
2327 d_ctx->plugin = plugin;
2329 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2330 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2331 &fragment_msg_proc, &ack_proc);
2333 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2334 (GNUNET_CONTAINER_HeapCostType)
2336 LOG (GNUNET_ERROR_TYPE_DEBUG,
2337 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2338 (unsigned int) ntohs (msg->size),
2339 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2343 LOG (GNUNET_ERROR_TYPE_DEBUG,
2344 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2345 (unsigned int) ntohs (msg->size),
2346 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2349 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2351 /* keep this 'rc' from expiring */
2352 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2353 (GNUNET_CONTAINER_HeapCostType)
2356 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2357 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2359 /* remove 'rc' that was inactive the longest */
2360 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2361 GNUNET_assert (NULL != d_ctx);
2362 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2363 GNUNET_free (d_ctx);
2369 * Read and process a message from the given socket.
2371 * @param plugin the overall plugin
2372 * @param rsock socket to read from
2375 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2379 char buf[65536] GNUNET_ALIGN;
2381 const struct GNUNET_MessageHeader *msg;
2383 fromlen = sizeof (addr);
2384 memset (&addr, 0, sizeof (addr));
2385 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2386 (struct sockaddr *) &addr, &fromlen);
2388 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2389 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2390 * on this socket has failed.
2392 * WSAECONNRESET - The virtual circuit was reset by the remote side
2393 * executing a hard or abortive close. The application should close
2394 * the socket; it is no longer usable. On a UDP-datagram socket this
2395 * error indicates a previous send operation resulted in an ICMP Port
2396 * Unreachable message.
2398 if ( (-1 == size) && (ECONNRESET == errno) )
2403 LOG (GNUNET_ERROR_TYPE_DEBUG,
2404 "UDP failed to receive data: %s\n", STRERROR (errno));
2405 /* Connection failure or something. Not a protocol violation. */
2408 if (size < sizeof (struct GNUNET_MessageHeader))
2410 LOG (GNUNET_ERROR_TYPE_WARNING,
2411 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2412 (unsigned int) size);
2413 /* _MAY_ be a connection failure (got partial message) */
2414 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2415 GNUNET_break_op (0);
2418 msg = (const struct GNUNET_MessageHeader *) buf;
2420 LOG (GNUNET_ERROR_TYPE_DEBUG,
2421 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2422 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2424 if (size != ntohs (msg->size))
2426 GNUNET_break_op (0);
2430 GNUNET_STATISTICS_update (plugin->env->stats,
2431 "# UDP, total, bytes, received",
2434 switch (ntohs (msg->type))
2436 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2437 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2440 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2441 read_process_msg (plugin, msg, addr, fromlen);
2444 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2445 read_process_ack (plugin, msg, addr, fromlen);
2448 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2449 read_process_fragment (plugin, msg, addr, fromlen);
2453 GNUNET_break_op (0);
2458 static struct UDP_MessageWrapper *
2459 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2460 struct GNUNET_NETWORK_Handle *sock)
2462 struct UDP_MessageWrapper *udpw = NULL;
2463 struct GNUNET_TIME_Relative remaining;
2466 while (udpw != NULL)
2468 /* Find messages with timeout */
2469 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2470 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2472 /* Message timed out */
2473 switch (udpw->msg_type) {
2474 case MSG_UNFRAGMENTED:
2475 GNUNET_STATISTICS_update (plugin->env->stats,
2476 "# UDP, total, bytes, sent, timeout",
2477 udpw->msg_size, GNUNET_NO);
2478 GNUNET_STATISTICS_update (plugin->env->stats,
2479 "# UDP, total, messages, sent, timeout",
2481 GNUNET_STATISTICS_update (plugin->env->stats,
2482 "# UDP, unfragmented msgs, messages, sent, timeout",
2484 GNUNET_STATISTICS_update (plugin->env->stats,
2485 "# UDP, unfragmented msgs, bytes, sent, timeout",
2486 udpw->payload_size, GNUNET_NO);
2487 /* Not fragmented message */
2488 LOG (GNUNET_ERROR_TYPE_DEBUG,
2489 "Message for peer `%s' with size %u timed out\n",
2490 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2491 call_continuation (udpw, GNUNET_SYSERR);
2492 /* Remove message */
2493 dequeue (plugin, udpw);
2496 case MSG_FRAGMENTED:
2497 /* Fragmented message */
2498 GNUNET_STATISTICS_update (plugin->env->stats,
2499 "# UDP, total, bytes, sent, timeout",
2500 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2501 GNUNET_STATISTICS_update (plugin->env->stats,
2502 "# UDP, total, messages, sent, timeout",
2504 call_continuation (udpw, GNUNET_SYSERR);
2505 LOG (GNUNET_ERROR_TYPE_DEBUG,
2506 "Fragment for message for peer `%s' with size %u timed out\n",
2507 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2510 GNUNET_STATISTICS_update (plugin->env->stats,
2511 "# UDP, fragmented msgs, messages, sent, timeout",
2513 GNUNET_STATISTICS_update (plugin->env->stats,
2514 "# UDP, fragmented msgs, bytes, sent, timeout",
2515 udpw->frag_ctx->payload_size, GNUNET_NO);
2516 /* Remove fragmented message due to timeout */
2517 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2520 GNUNET_STATISTICS_update (plugin->env->stats,
2521 "# UDP, total, bytes, sent, timeout",
2522 udpw->msg_size, GNUNET_NO);
2523 GNUNET_STATISTICS_update (plugin->env->stats,
2524 "# UDP, total, messages, sent, timeout",
2526 LOG (GNUNET_ERROR_TYPE_DEBUG,
2527 "ACK Message for peer `%s' with size %u timed out\n",
2528 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2529 call_continuation (udpw, GNUNET_SYSERR);
2530 dequeue (plugin, udpw);
2536 if (sock == plugin->sockv4)
2537 udpw = plugin->ipv4_queue_head;
2538 else if (sock == plugin->sockv6)
2539 udpw = plugin->ipv6_queue_head;
2542 GNUNET_break (0); /* should never happen */
2545 GNUNET_STATISTICS_update (plugin->env->stats,
2546 "# messages dismissed due to timeout",
2551 /* Message did not time out, check flow delay */
2552 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2553 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2555 /* this message is not delayed */
2556 LOG (GNUNET_ERROR_TYPE_DEBUG,
2557 "Message for peer `%s' (%u bytes) is not delayed \n",
2558 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2559 break; /* Found message to send, break */
2563 /* Message is delayed, try next */
2564 LOG (GNUNET_ERROR_TYPE_DEBUG,
2565 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2566 GNUNET_i2s(&udpw->session->target), udpw->payload_size,
2567 GNUNET_STRINGS_relative_time_to_string (remaining,
2578 analyze_send_error (struct Plugin *plugin,
2579 const struct sockaddr * sa,
2583 static int network_down_error;
2584 struct GNUNET_ATS_Information type;
2586 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2587 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2588 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2590 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2592 /* IPv4: "Network unreachable" or "Network down"
2594 * This indicates we do not have connectivity
2596 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2597 _("UDP could not transmit message to `%s': "
2598 "Network seems down, please check your network configuration\n"),
2599 GNUNET_a2s (sa, slen));
2601 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2603 /* IPv6: "Network unreachable" or "Network down"
2605 * This indicates that this system is IPv6 enabled, but does not
2606 * have a valid global IPv6 address assigned or we do not have
2610 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2611 _("UDP could not transmit message to `%s': "
2612 "Please check your network configuration and disable IPv6 if your "
2613 "connection does not have a global IPv6 address\n"),
2614 GNUNET_a2s (sa, slen));
2619 LOG (GNUNET_ERROR_TYPE_WARNING,
2620 "UDP could not transmit message to `%s': `%s'\n",
2621 GNUNET_a2s (sa, slen), STRERROR (error));
2626 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2628 const struct sockaddr * sa;
2632 struct UDP_MessageWrapper *udpw = NULL;
2634 /* Find message to send */
2635 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2638 return 0; /* No message to send */
2640 sa = udpw->session->sock_addr;
2641 slen = udpw->session->addrlen;
2643 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2645 if (GNUNET_SYSERR == sent)
2648 analyze_send_error (plugin, sa, slen, errno);
2649 call_continuation(udpw, GNUNET_SYSERR);
2650 GNUNET_STATISTICS_update (plugin->env->stats,
2651 "# UDP, total, bytes, sent, failure",
2653 GNUNET_STATISTICS_update (plugin->env->stats,
2654 "# UDP, total, messages, sent, failure",
2660 LOG (GNUNET_ERROR_TYPE_DEBUG,
2661 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2662 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2663 (sent < 0) ? STRERROR (errno) : "ok");
2664 GNUNET_STATISTICS_update (plugin->env->stats,
2665 "# UDP, total, bytes, sent, success",
2667 GNUNET_STATISTICS_update (plugin->env->stats,
2668 "# UDP, total, messages, sent, success",
2670 if (NULL != udpw->frag_ctx)
2671 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2672 call_continuation (udpw, GNUNET_OK);
2674 dequeue (plugin, udpw);
2683 * We have been notified that our readset has something to read. We don't
2684 * know which socket needs to be read, so we have to check each one
2685 * Then reschedule this function to be called again once more is available.
2687 * @param cls the plugin handle
2688 * @param tc the scheduling context (for rescheduling this function again)
2691 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2693 struct Plugin *plugin = cls;
2695 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2696 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2698 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2699 (NULL != plugin->sockv4) &&
2700 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2701 udp_select_read (plugin, plugin->sockv4);
2702 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2703 (NULL != plugin->sockv4) &&
2704 (NULL != plugin->ipv4_queue_head) &&
2705 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2706 udp_select_send (plugin, plugin->sockv4);
2707 schedule_select (plugin);
2712 * We have been notified that our readset has something to read. We don't
2713 * know which socket needs to be read, so we have to check each one
2714 * Then reschedule this function to be called again once more is available.
2716 * @param cls the plugin handle
2717 * @param tc the scheduling context (for rescheduling this function again)
2720 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2722 struct Plugin *plugin = cls;
2724 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2725 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2727 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2728 (NULL != plugin->sockv6) &&
2729 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2730 udp_select_read (plugin, plugin->sockv6);
2731 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2732 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2733 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2734 udp_select_send (plugin, plugin->sockv6);
2735 schedule_select (plugin);
2741 * @return number of sockets that were successfully bound
2744 setup_sockets (struct Plugin *plugin,
2745 const struct sockaddr_in6 *bind_v6,
2746 const struct sockaddr_in *bind_v4)
2749 int sockets_created = 0;
2750 struct sockaddr_in6 serverAddrv6;
2751 struct sockaddr_in serverAddrv4;
2752 struct sockaddr *serverAddr;
2753 struct sockaddr *addrs[2];
2754 socklen_t addrlens[2];
2758 /* Create IPv6 socket */
2760 if (plugin->enable_ipv6 == GNUNET_YES)
2762 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2763 if (NULL == plugin->sockv6)
2765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2766 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2767 plugin->enable_ipv6 = GNUNET_NO;
2771 memset (&serverAddrv6, '\0', sizeof (struct sockaddr_in6));
2772 #if HAVE_SOCKADDR_IN_SIN_LEN
2773 serverAddrv6.sin6_len = sizeof (struct sockaddr_in6);
2775 serverAddrv6.sin6_family = AF_INET6;
2776 if (NULL != bind_v6)
2777 serverAddrv6.sin6_addr = bind_v6->sin6_addr;
2779 serverAddrv6.sin6_addr = in6addr_any;
2781 if (0 == plugin->port) /* autodetect */
2782 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2784 serverAddrv6.sin6_port = htons (plugin->port);
2785 addrlen = sizeof (struct sockaddr_in6);
2786 serverAddr = (struct sockaddr *) &serverAddrv6;
2791 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2792 GNUNET_a2s (serverAddr, addrlen));
2794 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2795 serverAddr, addrlen, 0))
2798 if (0 != plugin->port)
2800 tries = 10; /* fail */
2801 break; /* bind failed on specific port */
2804 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2809 GNUNET_NETWORK_socket_close (plugin->sockv6);
2810 plugin->enable_ipv6 = GNUNET_NO;
2811 plugin->sockv6 = NULL;
2814 if (plugin->sockv6 != NULL)
2816 LOG (GNUNET_ERROR_TYPE_DEBUG,
2817 "IPv6 socket created on port %s\n",
2818 GNUNET_a2s (serverAddr, addrlen));
2819 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2820 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2825 LOG (GNUNET_ERROR_TYPE_ERROR,
2826 "Failed to bind UDP socket to %s: %s\n",
2827 GNUNET_a2s (serverAddr, addrlen),
2833 /* Create IPv4 socket */
2835 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2836 if (NULL == plugin->sockv4)
2838 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2839 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2840 plugin->enable_ipv4 = GNUNET_NO;
2844 memset (&serverAddrv4, '\0', sizeof (struct sockaddr_in));
2845 #if HAVE_SOCKADDR_IN_SIN_LEN
2846 serverAddrv4.sin_len = sizeof (struct sockaddr_in);
2848 serverAddrv4.sin_family = AF_INET;
2849 if (NULL != bind_v4)
2850 serverAddrv4.sin_addr = bind_v4->sin_addr;
2852 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2854 if (0 == plugin->port)
2856 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2858 serverAddrv4.sin_port = htons (plugin->port);
2861 addrlen = sizeof (struct sockaddr_in);
2862 serverAddr = (struct sockaddr *) &serverAddrv4;
2867 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2868 GNUNET_a2s (serverAddr, addrlen));
2871 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2872 serverAddr, addrlen, 0))
2875 if (0 != plugin->port)
2877 tries = 10; /* fail */
2878 break; /* bind failed on specific port */
2882 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2888 GNUNET_NETWORK_socket_close (plugin->sockv4);
2889 plugin->enable_ipv4 = GNUNET_NO;
2890 plugin->sockv4 = NULL;
2893 if (plugin->sockv4 != NULL)
2895 LOG (GNUNET_ERROR_TYPE_DEBUG,
2896 "IPv4 socket created on port %s\n", GNUNET_a2s (serverAddr, addrlen));
2897 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2898 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2903 LOG (GNUNET_ERROR_TYPE_ERROR,
2904 "Failed to bind UDP socket to %s: %s\n",
2905 GNUNET_a2s (serverAddr, addrlen), STRERROR (eno));
2909 if (0 == sockets_created)
2911 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2912 return 0; /* No sockets created, return */
2915 /* Create file descriptors */
2916 if (plugin->enable_ipv4 == GNUNET_YES)
2918 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2919 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2920 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2921 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2922 if (NULL != plugin->sockv4)
2924 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2925 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2929 if (plugin->enable_ipv6 == GNUNET_YES)
2931 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2932 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2933 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2934 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2935 if (NULL != plugin->sockv6)
2937 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2938 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2942 schedule_select (plugin);
2943 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2944 GNUNET_NO, plugin->port,
2946 (const struct sockaddr **) addrs, addrlens,
2947 &udp_nat_port_map_callback, NULL, plugin);
2949 return sockets_created;
2954 * The exported method. Makes the core api available via a global and
2955 * returns the udp transport API.
2957 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2958 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2961 libgnunet_plugin_transport_udp_init (void *cls)
2963 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2964 struct GNUNET_TRANSPORT_PluginFunctions *api;
2966 unsigned long long port;
2967 unsigned long long aport;
2968 unsigned long long broadcast;
2969 unsigned long long udp_max_bps;
2970 unsigned long long enable_v6;
2971 char * bind4_address;
2972 char * bind6_address;
2973 char * fancy_interval;
2974 struct GNUNET_TIME_Relative interval;
2975 struct sockaddr_in serverAddrv4;
2976 struct sockaddr_in6 serverAddrv6;
2981 if (NULL == env->receive)
2983 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2984 initialze the plugin or the API */
2985 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2987 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2988 api->address_to_string = &udp_address_to_string;
2989 api->string_to_address = &udp_string_to_address;
2993 GNUNET_assert (NULL != env->stats);
2995 /* Get port number: port == 0 : autodetect a port,
2996 * > 0 : use this port,
2997 * not given : 2086 default */
2999 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
3003 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3004 "ADVERTISED_PORT", &aport))
3008 LOG (GNUNET_ERROR_TYPE_WARNING,
3009 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3016 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3018 enable_v6 = GNUNET_NO;
3020 enable_v6 = GNUNET_YES;
3023 have_bind4 = GNUNET_NO;
3024 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
3026 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3027 "BINDTO", &bind4_address))
3029 LOG (GNUNET_ERROR_TYPE_DEBUG,
3030 "Binding udp plugin to specific address: `%s'\n",
3032 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
3034 GNUNET_free (bind4_address);
3037 have_bind4 = GNUNET_YES;
3039 GNUNET_free_non_null (bind4_address);
3040 have_bind6 = GNUNET_NO;
3041 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
3043 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3044 "BINDTO6", &bind6_address))
3046 LOG (GNUNET_ERROR_TYPE_DEBUG,
3047 "Binding udp plugin to specific address: `%s'\n",
3050 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
3052 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3054 GNUNET_free (bind6_address);
3057 have_bind6 = GNUNET_YES;
3059 GNUNET_free_non_null (bind6_address);
3061 /* Initialize my flags */
3064 /* Enable neighbour discovery */
3065 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3067 if (broadcast == GNUNET_SYSERR)
3068 broadcast = GNUNET_NO;
3070 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3071 "BROADCAST_INTERVAL", &fancy_interval))
3073 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3077 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3079 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3081 GNUNET_free (fancy_interval);
3084 /* Maximum datarate */
3085 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3086 "MAX_BPS", &udp_max_bps))
3088 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3091 p = GNUNET_malloc (sizeof (struct Plugin));
3094 p->broadcast_interval = interval;
3095 p->enable_ipv6 = enable_v6;
3096 p->enable_ipv4 = GNUNET_YES; /* default */
3098 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
3099 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3100 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3101 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3102 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
3105 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3106 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &serverAddrv6 : NULL,
3107 (GNUNET_YES == have_bind4) ? &serverAddrv4 : NULL);
3108 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
3110 LOG (GNUNET_ERROR_TYPE_ERROR,
3111 _("Failed to create network sockets, plugin failed\n"));
3112 GNUNET_CONTAINER_multihashmap_destroy (p->sessions);
3113 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3114 GNUNET_SERVER_mst_destroy (p->mst);
3118 else if (broadcast == GNUNET_YES)
3120 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3121 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
3124 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3127 api->disconnect = &udp_disconnect;
3128 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3129 api->address_to_string = &udp_address_to_string;
3130 api->string_to_address = &udp_string_to_address;
3131 api->check_address = &udp_plugin_check_address;
3132 api->get_session = &udp_plugin_get_session;
3133 api->send = &udp_plugin_send;
3134 api->get_network = &udp_get_network;
3141 heap_cleanup_iterator (void *cls,
3142 struct GNUNET_CONTAINER_HeapNode *
3143 node, void *element,
3144 GNUNET_CONTAINER_HeapCostType
3147 struct DefragContext * d_ctx = element;
3149 GNUNET_CONTAINER_heap_remove_node (node);
3150 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3151 GNUNET_free (d_ctx);
3158 * The exported method. Makes the core api available via a global and
3159 * returns the udp transport API.
3161 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3165 libgnunet_plugin_transport_udp_done (void *cls)
3167 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3168 struct Plugin *plugin = api->cls;
3169 struct PrettyPrinterContext *cur;
3170 struct PrettyPrinterContext *next;
3178 stop_broadcast (plugin);
3179 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3181 GNUNET_SCHEDULER_cancel (plugin->select_task);
3182 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3184 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3186 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3187 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3190 /* Closing sockets */
3191 if (GNUNET_YES ==plugin->enable_ipv4)
3193 if (plugin->sockv4 != NULL)
3195 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3196 plugin->sockv4 = NULL;
3198 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3199 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3201 if (GNUNET_YES ==plugin->enable_ipv6)
3203 if (plugin->sockv6 != NULL)
3205 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3206 plugin->sockv6 = NULL;
3208 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3209 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3212 if (NULL != plugin->nat)
3213 GNUNET_NAT_unregister (plugin->nat);
3215 if (plugin->defrag_ctxs != NULL)
3217 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3218 heap_cleanup_iterator, NULL);
3219 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3220 plugin->defrag_ctxs = NULL;
3222 if (plugin->mst != NULL)
3224 GNUNET_SERVER_mst_destroy(plugin->mst);
3228 /* Clean up leftover messages */
3229 struct UDP_MessageWrapper * udpw;
3230 udpw = plugin->ipv4_queue_head;
3231 while (udpw != NULL)
3233 struct UDP_MessageWrapper *tmp = udpw->next;
3234 dequeue (plugin, udpw);
3235 call_continuation(udpw, GNUNET_SYSERR);
3240 udpw = plugin->ipv6_queue_head;
3241 while (udpw != NULL)
3243 struct UDP_MessageWrapper *tmp = udpw->next;
3244 dequeue (plugin, udpw);
3245 call_continuation(udpw, GNUNET_SYSERR);
3251 /* Clean up sessions */
3252 LOG (GNUNET_ERROR_TYPE_DEBUG,
3253 "Cleaning up sessions\n");
3254 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3255 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
3257 next = ppc_dll_head;
3258 for (cur = next; NULL != cur; cur = next)
3261 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3262 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3263 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3269 GNUNET_free (plugin);
3272 struct Allocation *allocation;
3273 while (NULL != ahead)
3276 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3277 GNUNET_free (allocation);
3279 struct Allocator *allocator;
3280 while (NULL != aehead)
3283 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3284 GNUNET_free (allocator);
3291 /* end of plugin_transport_udp.c */