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);
470 stop_session_timeout (struct Session *s)
472 GNUNET_assert (NULL != s);
474 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
476 GNUNET_SCHEDULER_cancel (s->timeout_task);
477 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
479 "Timeout stopped for session %p canceled\n",
486 * (re)schedule select tasks for this plugin.
488 * @param plugin plugin to reschedule
491 schedule_select (struct Plugin *plugin)
493 struct GNUNET_TIME_Relative min_delay;
494 struct UDP_MessageWrapper *udpw;
496 if ((GNUNET_YES == plugin->enable_ipv4) && (NULL != plugin->sockv4))
498 /* Find a message ready to send:
499 * Flow delay from other peer is expired or not set (0) */
500 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
501 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
502 min_delay = GNUNET_TIME_relative_min (min_delay,
503 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
505 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
506 GNUNET_SCHEDULER_cancel(plugin->select_task);
509 * - write active set if message is ready
510 * - timeout minimum delay */
511 plugin->select_task =
512 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
513 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
515 (0 == min_delay.rel_value_us) ? plugin->ws_v4 : NULL,
516 &udp_plugin_select, plugin);
518 if ((GNUNET_YES == plugin->enable_ipv6) && (NULL != plugin->sockv6))
520 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
521 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
522 min_delay = GNUNET_TIME_relative_min (min_delay,
523 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
525 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
526 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
527 plugin->select_task_v6 =
528 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
529 (0 == min_delay.rel_value_us) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
531 (0 == min_delay.rel_value_us) ? plugin->ws_v6 : NULL,
532 &udp_plugin_select_v6, plugin);
538 * Function called for a quick conversion of the binary address to
539 * a numeric address. Note that the caller must not free the
540 * address and that the next call to this function is allowed
541 * to override the address again.
544 * @param addr binary address
545 * @param addrlen length of the address
546 * @return string representing the same address
549 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
551 static char rbuf[INET6_ADDRSTRLEN + 10];
552 char buf[INET6_ADDRSTRLEN];
556 const struct IPv4UdpAddress *t4;
557 const struct IPv6UdpAddress *t6;
562 if ((NULL != addr) && (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 ((NULL != addr) && (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),
592 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
593 PLUGIN_NAME, options, buf, port);
599 * Function called to convert a string address to
602 * @param cls closure ('struct Plugin*')
603 * @param addr string address
604 * @param addrlen length of the address
605 * @param buf location to store the buffer
606 * @param added location to store the number of bytes in the buffer.
607 * If the function returns GNUNET_SYSERR, its contents are undefined.
608 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
611 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
612 void **buf, size_t *added)
614 struct sockaddr_storage socket_address;
620 /* Format tcp.options.address:port */
625 if ((NULL == addr) || (addrlen == 0))
628 return GNUNET_SYSERR;
630 if ('\0' != addr[addrlen - 1])
633 return GNUNET_SYSERR;
635 if (strlen (addr) != addrlen - 1)
638 return GNUNET_SYSERR;
640 plugin = GNUNET_strdup (addr);
641 optionstr = strchr (plugin, '.');
642 if (NULL == optionstr)
645 GNUNET_free (plugin);
646 return GNUNET_SYSERR;
650 options = atol (optionstr);
651 address = strchr (optionstr, '.');
655 GNUNET_free (plugin);
656 return GNUNET_SYSERR;
662 GNUNET_STRINGS_to_address_ip (address, strlen (address),
666 GNUNET_free (plugin);
667 return GNUNET_SYSERR;
670 GNUNET_free (plugin);
672 switch (socket_address.ss_family)
676 struct IPv4UdpAddress *u4;
677 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
678 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
679 u4->options = htonl (options);
680 u4->ipv4_addr = in4->sin_addr.s_addr;
681 u4->u4_port = in4->sin_port;
683 *added = sizeof (struct IPv4UdpAddress);
688 struct IPv6UdpAddress *u6;
689 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
690 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
691 u6->options = htonl (options);
692 u6->ipv6_addr = in6->sin6_addr;
693 u6->u6_port = in6->sin6_port;
695 *added = sizeof (struct IPv6UdpAddress);
700 return GNUNET_SYSERR;
706 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
708 struct PrettyPrinterContext *ppc = cls;
709 /* GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PPC %p was not removed!\n", ppc); */
710 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
711 if (NULL != ppc->resolver_handle)
713 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
714 ppc->resolver_handle = NULL;
717 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
723 * Append our port and forward the result.
725 * @param cls a 'struct PrettyPrinterContext'
726 * @param hostname result from DNS resolver
729 append_port (void *cls, const char *hostname)
731 struct PrettyPrinterContext *ppc = cls;
732 struct PrettyPrinterContext *cur;
734 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC callback: %p `%s'\n",ppc, hostname); */
735 if (hostname == NULL)
737 ppc->asc (ppc->asc_cls, NULL);
738 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
739 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
740 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
741 ppc->resolver_handle = NULL;
742 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was removed!\n", ppc); */
746 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
757 if (GNUNET_YES == ppc->ipv6)
758 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
760 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
761 ppc->asc (ppc->asc_cls, ret);
766 * Convert the transports address to a nice, human-readable
770 * @param type name of the transport that generated the address
771 * @param addr one of the addresses of the host, NULL for the last address
772 * the specific address format depends on the transport
773 * @param addrlen length of the address
774 * @param numeric should (IP) addresses be displayed in numeric form?
775 * @param timeout after how long should we give up?
776 * @param asc function to call on each string
777 * @param asc_cls closure for asc
780 udp_plugin_address_pretty_printer (void *cls, const char *type,
781 const void *addr, size_t addrlen,
783 struct GNUNET_TIME_Relative timeout,
784 GNUNET_TRANSPORT_AddressStringCallback asc,
787 struct PrettyPrinterContext *ppc;
790 struct sockaddr_in a4;
791 struct sockaddr_in6 a6;
792 const struct IPv4UdpAddress *u4;
793 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);
1145 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1147 struct UDP_MessageWrapper *udpw;
1148 struct UDP_MessageWrapper *tmp;
1149 struct UDP_MessageWrapper dummy;
1150 struct Session *s = fc->session;
1152 LOG (GNUNET_ERROR_TYPE_DEBUG,
1153 "%p : Fragmented message removed with result %s\n",
1155 (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1157 /* Call continuation for fragmented message */
1158 memset (&dummy, 0, sizeof (dummy));
1159 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1160 dummy.msg_size = s->frag_ctx->on_wire_size;
1161 dummy.payload_size = s->frag_ctx->payload_size;
1162 dummy.frag_ctx = s->frag_ctx;
1164 dummy.cont_cls = NULL;
1167 call_continuation (&dummy, result);
1169 /* Remove leftover fragments from queue */
1170 if (s->addrlen == sizeof (struct sockaddr_in6))
1172 udpw = plugin->ipv6_queue_head;
1173 while (NULL != udpw)
1176 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1178 dequeue (plugin, udpw);
1179 call_continuation (udpw, GNUNET_SYSERR);
1185 if (s->addrlen == sizeof (struct sockaddr_in))
1187 udpw = plugin->ipv4_queue_head;
1191 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1193 dequeue (plugin, udpw);
1194 call_continuation (udpw, GNUNET_SYSERR);
1201 /* Destroy fragmentation context */
1202 GNUNET_FRAGMENT_context_destroy (fc->frag,
1203 &s->last_expected_msg_delay,
1204 &s->last_expected_ack_delay);
1210 * Functions with this signature are called whenever we need
1211 * to close a session due to a disconnect or failure to
1212 * establish a connection.
1214 * @param s session to close down
1217 disconnect_session (struct Session *s)
1219 struct UDP_MessageWrapper *udpw;
1220 struct UDP_MessageWrapper *next;
1222 GNUNET_assert (GNUNET_YES != s->in_destroy);
1223 LOG (GNUNET_ERROR_TYPE_DEBUG,
1224 "Session %p to peer `%s' address ended \n",
1226 GNUNET_i2s (&s->target),
1227 GNUNET_a2s (s->sock_addr, s->addrlen));
1228 stop_session_timeout (s);
1230 if (NULL != s->frag_ctx)
1232 /* Remove fragmented message due to disconnect */
1233 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1236 next = plugin->ipv4_queue_head;
1237 while (NULL != (udpw = next))
1240 if (udpw->session == s)
1242 dequeue (plugin, udpw);
1243 call_continuation(udpw, GNUNET_SYSERR);
1247 next = plugin->ipv6_queue_head;
1248 while (NULL != (udpw = next))
1251 if (udpw->session == s)
1253 dequeue (plugin, udpw);
1254 call_continuation(udpw, GNUNET_SYSERR);
1258 plugin->env->session_end (plugin->env->cls, &s->target, s);
1260 if (NULL != s->frag_ctx)
1262 if (NULL != s->frag_ctx->cont)
1264 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1265 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1266 LOG (GNUNET_ERROR_TYPE_DEBUG,
1267 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1268 GNUNET_i2s (&s->target));
1272 GNUNET_assert (GNUNET_YES ==
1273 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1274 &s->target.hashPubKey,
1276 GNUNET_STATISTICS_set(plugin->env->stats,
1277 "# UDP, sessions active",
1278 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1281 s->in_destroy = GNUNET_YES;
1287 * Destroy a session, plugin is being unloaded.
1290 * @param key hash of public key of target peer
1291 * @param value a 'struct PeerSession*' to clean up
1292 * @return GNUNET_OK (continue to iterate)
1295 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1297 disconnect_session(value);
1303 * Disconnect from a remote node. Clean up session if we have one for this peer
1305 * @param cls closure for this call (should be handle to Plugin)
1306 * @param target the peeridentity of the peer to disconnect
1307 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1310 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1312 struct Plugin *plugin = cls;
1313 GNUNET_assert (plugin != NULL);
1315 GNUNET_assert (target != NULL);
1316 LOG (GNUNET_ERROR_TYPE_DEBUG,
1317 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1318 /* Clean up sessions */
1319 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1324 * Session was idle, so disconnect it
1327 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1329 GNUNET_assert (NULL != cls);
1330 struct Session *s = cls;
1332 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Session %p was idle for %s, disconnecting\n",
1336 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1338 /* call session destroy function */
1339 disconnect_session (s);
1344 * Start session timeout
1347 start_session_timeout (struct Session *s)
1349 GNUNET_assert (NULL != s);
1350 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1351 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Timeout for session %p set to %s\n",
1357 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1363 * Increment session timeout due to activity
1366 reschedule_session_timeout (struct Session *s)
1368 GNUNET_assert (NULL != s);
1369 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1371 GNUNET_SCHEDULER_cancel (s->timeout_task);
1372 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376 "Timeout rescheduled for session %p set to %s\n",
1378 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1383 static struct Session *
1384 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1385 const void *addr, size_t addrlen,
1386 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1389 const struct IPv4UdpAddress *t4;
1390 const struct IPv6UdpAddress *t6;
1391 struct sockaddr_in *v4;
1392 struct sockaddr_in6 *v6;
1403 case sizeof (struct IPv4UdpAddress):
1404 if (NULL == plugin->sockv4)
1406 LOG (GNUNET_ERROR_TYPE_DEBUG,
1407 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1409 udp_address_to_string(NULL, addr, addrlen));
1413 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1414 len = sizeof (struct sockaddr_in);
1415 v4 = (struct sockaddr_in *) &s[1];
1416 v4->sin_family = AF_INET;
1417 #if HAVE_SOCKADDR_IN_SIN_LEN
1418 v4->sin_len = sizeof (struct sockaddr_in);
1420 v4->sin_port = t4->u4_port;
1421 v4->sin_addr.s_addr = t4->ipv4_addr;
1422 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1424 case sizeof (struct IPv6UdpAddress):
1425 if (NULL == plugin->sockv6)
1427 LOG (GNUNET_ERROR_TYPE_INFO,
1428 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1430 udp_address_to_string(NULL, addr, addrlen));
1434 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1435 len = sizeof (struct sockaddr_in6);
1436 v6 = (struct sockaddr_in6 *) &s[1];
1437 v6->sin6_family = AF_INET6;
1438 #if HAVE_SOCKADDR_IN_SIN_LEN
1439 v6->sin6_len = sizeof (struct sockaddr_in6);
1441 v6->sin6_port = t6->u6_port;
1442 v6->sin6_addr = t6->ipv6_addr;
1443 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1446 /* Must have a valid address to send to */
1447 GNUNET_STATISTICS_update (plugin->env->stats,
1449 ("# requests to create session with invalid address"),
1454 s->target = *target;
1455 s->sock_addr = (const struct sockaddr *) &s[1];
1456 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1457 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1458 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1459 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1460 s->inbound = GNUNET_NO;
1461 start_session_timeout (s);
1467 session_cmp_it (void *cls,
1468 const struct GNUNET_HashCode * key,
1471 struct SessionCompareContext * cctx = cls;
1472 const struct GNUNET_HELLO_Address *address = cctx->addr;
1473 struct Session *s = value;
1475 socklen_t s_addrlen = s->addrlen;
1477 LOG (GNUNET_ERROR_TYPE_DEBUG,
1478 "Comparing address %s <-> %s\n",
1479 udp_address_to_string (NULL, (void *) address->address,
1480 address->address_length),
1481 GNUNET_a2s (s->sock_addr, s->addrlen));
1482 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1483 (s_addrlen == sizeof (struct sockaddr_in)))
1485 struct IPv4UdpAddress * u4 = NULL;
1486 u4 = (struct IPv4UdpAddress *) address->address;
1487 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1488 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1489 (u4->u4_port == s4->sin_port))
1496 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1497 (s_addrlen == sizeof (struct sockaddr_in6)))
1499 struct IPv6UdpAddress * u6 = NULL;
1500 u6 = (struct IPv6UdpAddress *) address->address;
1501 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1502 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1503 (u6->u6_port == s6->sin6_port))
1514 * Function obtain the network type for a session
1516 * @param cls closure ('struct Plugin*')
1517 * @param session the session
1518 * @return the network type in HBO or GNUNET_SYSERR
1520 static enum GNUNET_ATS_Network_Type
1521 udp_get_network (void *cls,
1522 struct Session *session)
1524 return ntohl (session->ats.value);
1529 * Creates a new outbound session the transport service will use to send data to the
1532 * @param cls the plugin
1533 * @param address the address
1534 * @return the session or NULL of max connections exceeded
1536 static struct Session *
1537 udp_plugin_lookup_session (void *cls,
1538 const struct GNUNET_HELLO_Address *address)
1540 struct Plugin * plugin = cls;
1541 struct IPv6UdpAddress * udp_a6;
1542 struct IPv4UdpAddress * udp_a4;
1544 GNUNET_assert (plugin != NULL);
1545 GNUNET_assert (address != NULL);
1548 if ((address->address == NULL) ||
1549 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1550 (address->address_length != sizeof (struct IPv6UdpAddress))))
1552 LOG (GNUNET_ERROR_TYPE_WARNING,
1553 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1554 address->address_length,
1555 sizeof (struct IPv4UdpAddress),
1556 sizeof (struct IPv6UdpAddress));
1560 if (address->address_length == sizeof (struct IPv4UdpAddress))
1562 if (plugin->sockv4 == NULL)
1564 udp_a4 = (struct IPv4UdpAddress *) address->address;
1565 if (udp_a4->u4_port == 0)
1569 if (address->address_length == sizeof (struct IPv6UdpAddress))
1571 if (plugin->sockv6 == NULL)
1573 udp_a6 = (struct IPv6UdpAddress *) address->address;
1574 if (udp_a6->u6_port == 0)
1578 /* check if session already exists */
1579 struct SessionCompareContext cctx;
1580 cctx.addr = address;
1582 LOG (GNUNET_ERROR_TYPE_DEBUG,
1583 "Looking for existing session for peer `%s' `%s' \n",
1584 GNUNET_i2s (&address->peer),
1585 udp_address_to_string(NULL, address->address, address->address_length));
1586 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1587 if (cctx.res != NULL)
1589 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1596 static struct Session *
1597 udp_plugin_create_session (void *cls,
1598 const struct GNUNET_HELLO_Address *address)
1602 s = create_session (plugin,
1605 address->address_length,
1608 return NULL; /* protocol not supported or address invalid */
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Creating new session %p for peer `%s' address `%s'\n",
1612 GNUNET_i2s(&address->peer),
1613 udp_address_to_string(NULL,address->address,address->address_length));
1614 GNUNET_assert (GNUNET_OK ==
1615 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1616 &s->target.hashPubKey,
1618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1619 GNUNET_STATISTICS_set(plugin->env->stats,
1620 "# UDP, sessions active",
1621 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1628 * Creates a new outbound session the transport service will use to send data to the
1631 * @param cls the plugin
1632 * @param address the address
1633 * @return the session or NULL of max connections exceeded
1635 static struct Session *
1636 udp_plugin_get_session (void *cls,
1637 const struct GNUNET_HELLO_Address *address)
1641 if (NULL == address)
1646 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1647 (address->address_length != sizeof (struct IPv6UdpAddress)))
1650 /* otherwise create new */
1651 if (NULL != (s = udp_plugin_lookup_session (cls, address)))
1653 return udp_plugin_create_session (cls, address);
1658 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1660 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1664 GNUNET_STATISTICS_update (plugin->env->stats,
1665 "# UDP, total, bytes in buffers",
1666 udpw->msg_size, GNUNET_NO);
1667 plugin->bytes_in_buffer += udpw->msg_size;
1669 GNUNET_STATISTICS_update (plugin->env->stats,
1670 "# UDP, total, msgs in buffers",
1672 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1673 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1674 plugin->ipv4_queue_tail, udpw);
1675 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1676 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1677 plugin->ipv6_queue_tail, udpw);
1683 * Fragment message was transmitted via UDP, let fragmentation know
1684 * to send the next fragment now.
1686 * @param cls the 'struct UDPMessageWrapper' of the fragment
1687 * @param target destination peer (ignored)
1688 * @param result GNUNET_OK on success (ignored)
1689 * @param payload bytes payload sent
1690 * @param physical bytes physical sent
1693 send_next_fragment (void *cls,
1694 const struct GNUNET_PeerIdentity *target,
1695 int result, size_t payload, size_t physical)
1697 struct UDP_MessageWrapper *udpw = cls;
1699 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1704 * Function that is called with messages created by the fragmentation
1705 * module. In the case of the 'proc' callback of the
1706 * GNUNET_FRAGMENT_context_create function, this function must
1707 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1709 * @param cls closure, the 'struct FragmentationContext'
1710 * @param msg the message that was created
1713 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1715 struct UDP_FragmentationContext *frag_ctx = cls;
1716 struct Plugin *plugin = frag_ctx->plugin;
1717 struct UDP_MessageWrapper * udpw;
1718 size_t msg_len = ntohs (msg->size);
1720 LOG (GNUNET_ERROR_TYPE_DEBUG,
1721 "Enqueuing fragment with %u bytes\n", msg_len);
1722 frag_ctx->fragments_used ++;
1723 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1724 udpw->session = frag_ctx->session;
1725 udpw->msg_buf = (char *) &udpw[1];
1726 udpw->msg_size = msg_len;
1727 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1728 udpw->cont = &send_next_fragment;
1729 udpw->cont_cls = udpw;
1730 udpw->timeout = frag_ctx->timeout;
1731 udpw->frag_ctx = frag_ctx;
1732 udpw->msg_type = MSG_FRAGMENTED;
1733 memcpy (udpw->msg_buf, msg, msg_len);
1734 enqueue (plugin, udpw);
1735 schedule_select (plugin);
1740 * Function that can be used by the transport service to transmit
1741 * a message using the plugin. Note that in the case of a
1742 * peer disconnecting, the continuation MUST be called
1743 * prior to the disconnect notification itself. This function
1744 * will be called with this peer's HELLO message to initiate
1745 * a fresh connection to another peer.
1747 * @param cls closure
1748 * @param s which session must be used
1749 * @param msgbuf the message to transmit
1750 * @param msgbuf_size number of bytes in 'msgbuf'
1751 * @param priority how important is the message (most plugins will
1752 * ignore message priority and just FIFO)
1753 * @param to how long to wait at most for the transmission (does not
1754 * require plugins to discard the message after the timeout,
1755 * just advisory for the desired delay; most plugins will ignore
1757 * @param cont continuation to call once the message has
1758 * been transmitted (or if the transport is ready
1759 * for the next transmission call; or if the
1760 * peer disconnected...); can be NULL
1761 * @param cont_cls closure for cont
1762 * @return number of bytes used (on the physical network, with overheads);
1763 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1764 * and does NOT mean that the message was not transmitted (DV)
1767 udp_plugin_send (void *cls,
1769 const char *msgbuf, size_t msgbuf_size,
1770 unsigned int priority,
1771 struct GNUNET_TIME_Relative to,
1772 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1774 struct Plugin *plugin = cls;
1775 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1776 struct UDP_FragmentationContext * frag_ctx;
1777 struct UDP_MessageWrapper * udpw;
1778 struct UDPMessage *udp;
1780 GNUNET_assert (plugin != NULL);
1781 GNUNET_assert (s != NULL);
1783 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1784 return GNUNET_SYSERR;
1785 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1786 return GNUNET_SYSERR;
1787 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1790 return GNUNET_SYSERR;
1792 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1795 return GNUNET_SYSERR;
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1798 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1800 GNUNET_i2s (&s->target),
1801 GNUNET_a2s(s->sock_addr, s->addrlen));
1805 udp = (struct UDPMessage *) mbuf;
1806 udp->header.size = htons (udpmlen);
1807 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1808 udp->reserved = htonl (0);
1809 udp->sender = *plugin->env->my_identity;
1811 reschedule_session_timeout(s);
1812 if (udpmlen <= UDP_MTU)
1814 /* unfragmented message */
1815 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1817 udpw->msg_buf = (char *) &udpw[1];
1818 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1819 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1820 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1822 udpw->cont_cls = cont_cls;
1823 udpw->frag_ctx = NULL;
1824 udpw->msg_type = MSG_UNFRAGMENTED;
1825 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1826 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1827 enqueue (plugin, udpw);
1829 GNUNET_STATISTICS_update (plugin->env->stats,
1830 "# UDP, unfragmented msgs, messages, attempt",
1832 GNUNET_STATISTICS_update (plugin->env->stats,
1833 "# UDP, unfragmented msgs, bytes payload, attempt",
1834 udpw->payload_size, GNUNET_NO);
1838 /* fragmented message */
1839 if (s->frag_ctx != NULL)
1840 return GNUNET_SYSERR;
1841 memcpy (&udp[1], msgbuf, msgbuf_size);
1842 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1843 frag_ctx->plugin = plugin;
1844 frag_ctx->session = s;
1845 frag_ctx->cont = cont;
1846 frag_ctx->cont_cls = cont_cls;
1847 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1848 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1849 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1850 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1853 s->last_expected_msg_delay,
1854 s->last_expected_ack_delay,
1858 s->frag_ctx = frag_ctx;
1859 GNUNET_STATISTICS_update (plugin->env->stats,
1860 "# UDP, fragmented msgs, messages, pending",
1862 GNUNET_STATISTICS_update (plugin->env->stats,
1863 "# UDP, fragmented msgs, messages, attempt",
1865 GNUNET_STATISTICS_update (plugin->env->stats,
1866 "# UDP, fragmented msgs, bytes payload, attempt",
1867 frag_ctx->payload_size, GNUNET_NO);
1869 schedule_select (plugin);
1875 * Our external IP address/port mapping has changed.
1877 * @param cls closure, the 'struct LocalAddrList'
1878 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1879 * the previous (now invalid) one
1880 * @param addr either the previous or the new public IP address
1881 * @param addrlen actual lenght of the address
1884 udp_nat_port_map_callback (void *cls, int add_remove,
1885 const struct sockaddr *addr, socklen_t addrlen)
1887 struct Plugin *plugin = cls;
1888 struct IPv4UdpAddress u4;
1889 struct IPv6UdpAddress u6;
1893 LOG (GNUNET_ERROR_TYPE_INFO,
1894 "NAT notification to %s address `%s'\n",
1895 (GNUNET_YES == add_remove) ? "add" : "remove",
1896 GNUNET_a2s (addr, addrlen));
1898 /* convert 'addr' to our internal format */
1899 switch (addr->sa_family)
1902 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1903 memset (&u4, 0, sizeof (u4));
1904 u4.options = htonl(myoptions);
1905 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1906 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1907 if (0 == ((struct sockaddr_in *) addr)->sin_port)
1910 args = sizeof (struct IPv4UdpAddress);
1913 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1914 memset (&u4, 0, sizeof (u4));
1915 u6.options = htonl(myoptions);
1916 if (0 == ((struct sockaddr_in6 *) addr)->sin6_port)
1918 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1919 sizeof (struct in6_addr));
1920 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1922 args = sizeof (struct IPv6UdpAddress);
1928 /* modify our published address list */
1929 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1935 * Message tokenizer has broken up an incomming message. Pass it on
1938 * @param cls the 'struct Plugin'
1939 * @param client the 'struct SourceInformation'
1940 * @param hdr the actual message
1943 process_inbound_tokenized_messages (void *cls, void *client,
1944 const struct GNUNET_MessageHeader *hdr)
1946 struct Plugin *plugin = cls;
1947 struct SourceInformation *si = client;
1948 struct GNUNET_TIME_Relative delay;
1950 GNUNET_assert (si->session != NULL);
1951 if (GNUNET_YES == si->session->in_destroy)
1954 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1955 delay = plugin->env->receive (plugin->env->cls,
1959 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1960 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1962 plugin->env->update_address_metrics (plugin->env->cls,
1964 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1965 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1967 &si->session->ats, 1);
1969 si->session->flow_delay_for_other_peer = delay;
1970 reschedule_session_timeout(si->session);
1976 * We've received a UDP Message. Process it (pass contents to main service).
1978 * @param plugin plugin context
1979 * @param msg the message
1980 * @param sender_addr sender address
1981 * @param sender_addr_len number of bytes in sender_addr
1984 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1985 const struct sockaddr *sender_addr,
1986 socklen_t sender_addr_len)
1988 struct SourceInformation si;
1990 struct IPv4UdpAddress u4;
1991 struct IPv6UdpAddress u6;
1995 if (0 != ntohl (msg->reserved))
1997 GNUNET_break_op (0);
2000 if (ntohs (msg->header.size) <
2001 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2003 GNUNET_break_op (0);
2007 /* convert address */
2008 switch (sender_addr->sa_family)
2011 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2012 memset (&u4, 0, sizeof (u4));
2013 u6.options = htonl (0);
2014 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2015 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2020 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2021 memset (&u6, 0, sizeof (u6));
2022 u6.options = htonl (0);
2023 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2024 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2032 LOG (GNUNET_ERROR_TYPE_DEBUG,
2033 "Received message with %u bytes from peer `%s' at `%s'\n",
2034 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2035 GNUNET_a2s (sender_addr, sender_addr_len));
2037 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2038 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2040 s = udp_plugin_create_session(plugin, address);
2041 s->inbound = GNUNET_YES;
2042 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2043 (GNUNET_YES == s->inbound) ? NULL : address->address,
2044 (GNUNET_YES == s->inbound) ? 0 : address->address_length,
2047 GNUNET_free (address);
2049 /* iterate over all embedded messages */
2051 si.sender = msg->sender;
2055 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2056 ntohs (msg->header.size) -
2057 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
2059 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2065 * Scan the heap for a receive context with the given address.
2067 * @param cls the 'struct FindReceiveContext'
2068 * @param node internal node of the heap
2069 * @param element value stored at the node (a 'struct ReceiveContext')
2070 * @param cost cost associated with the node
2071 * @return GNUNET_YES if we should continue to iterate,
2075 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2076 void *element, GNUNET_CONTAINER_HeapCostType cost)
2078 struct FindReceiveContext *frc = cls;
2079 struct DefragContext *e = element;
2081 if ((frc->addr_len == e->addr_len) &&
2082 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2092 * Process a defragmented message.
2094 * @param cls the 'struct ReceiveContext'
2095 * @param msg the message
2098 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2100 struct DefragContext *rc = cls;
2102 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2107 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2112 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2113 rc->src_addr, rc->addr_len);
2117 struct LookupContext
2119 const struct sockaddr * addr;
2121 struct Session *res;
2128 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
2130 struct LookupContext *l_ctx = cls;
2131 struct Session * s = value;
2133 if ((s->addrlen == l_ctx->addrlen) &&
2134 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2144 * Transmit an acknowledgement.
2146 * @param cls the 'struct ReceiveContext'
2147 * @param id message ID (unused)
2148 * @param msg ack to transmit
2151 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2153 struct DefragContext *rc = cls;
2154 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2155 struct UDP_ACK_Message *udp_ack;
2157 struct UDP_MessageWrapper *udpw;
2159 struct LookupContext l_ctx;
2161 l_ctx.addr = rc->src_addr;
2162 l_ctx.addrlen = rc->addr_len;
2164 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
2165 &lookup_session_by_addr_it,
2172 if (s->flow_delay_for_other_peer.rel_value_us <= UINT32_MAX)
2173 delay = s->flow_delay_for_other_peer.rel_value_us;
2175 LOG (GNUNET_ERROR_TYPE_DEBUG,
2176 "Sending ACK to `%s' including delay of %s\n",
2177 GNUNET_a2s (rc->src_addr,
2178 (rc->src_addr->sa_family ==
2179 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2181 GNUNET_STRINGS_relative_time_to_string (s->flow_delay_for_other_peer,
2183 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2184 udpw->msg_size = msize;
2185 udpw->payload_size = 0;
2187 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2188 udpw->msg_buf = (char *)&udpw[1];
2189 udpw->msg_type = MSG_ACK;
2190 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2191 udp_ack->header.size = htons ((uint16_t) msize);
2192 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2193 udp_ack->delay = htonl (delay);
2194 udp_ack->sender = *rc->plugin->env->my_identity;
2195 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2196 enqueue (rc->plugin, udpw);
2201 read_process_msg (struct Plugin *plugin,
2202 const struct GNUNET_MessageHeader *msg,
2206 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2208 GNUNET_break_op (0);
2211 process_udp_message (plugin, (const struct UDPMessage *) msg,
2212 (const struct sockaddr *) addr, fromlen);
2217 read_process_ack (struct Plugin *plugin,
2218 const struct GNUNET_MessageHeader *msg,
2222 const struct GNUNET_MessageHeader *ack;
2223 const struct UDP_ACK_Message *udp_ack;
2224 struct LookupContext l_ctx;
2226 struct GNUNET_TIME_Relative flow_delay;
2228 if (ntohs (msg->size) <
2229 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2231 GNUNET_break_op (0);
2234 udp_ack = (const struct UDP_ACK_Message *) msg;
2235 l_ctx.addr = (const struct sockaddr *) addr;
2236 l_ctx.addrlen = fromlen;
2238 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2239 &lookup_session_by_addr_it,
2243 if ((NULL == s) || (NULL == s->frag_ctx))
2248 flow_delay.rel_value_us = (uint64_t) ntohl (udp_ack->delay);
2249 LOG (GNUNET_ERROR_TYPE_DEBUG,
2250 "We received a sending delay of %s\n",
2251 GNUNET_STRINGS_relative_time_to_string (flow_delay,
2253 s->flow_delay_from_other_peer =
2254 GNUNET_TIME_relative_to_absolute (flow_delay);
2256 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2257 if (ntohs (ack->size) !=
2258 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2260 GNUNET_break_op (0);
2264 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2266 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2268 LOG (GNUNET_ERROR_TYPE_DEBUG,
2269 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2270 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2271 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2272 /* Expect more ACKs to arrive */
2276 LOG (GNUNET_ERROR_TYPE_DEBUG,
2277 "Message full ACK'ed\n",
2278 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2279 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2281 /* Remove fragmented message after successful sending */
2282 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2287 read_process_fragment (struct Plugin *plugin,
2288 const struct GNUNET_MessageHeader *msg,
2292 struct DefragContext *d_ctx;
2293 struct GNUNET_TIME_Absolute now;
2294 struct FindReceiveContext frc;
2297 frc.addr = (const struct sockaddr *) addr;
2298 frc.addr_len = fromlen;
2300 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2301 (unsigned int) ntohs (msg->size),
2302 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2303 /* Lookup existing receive context for this address */
2304 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2305 &find_receive_context,
2307 now = GNUNET_TIME_absolute_get ();
2312 /* Create a new defragmentation context */
2313 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2314 memcpy (&d_ctx[1], addr, fromlen);
2315 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2316 d_ctx->addr_len = fromlen;
2317 d_ctx->plugin = plugin;
2319 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2320 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2321 &fragment_msg_proc, &ack_proc);
2323 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2324 (GNUNET_CONTAINER_HeapCostType)
2326 LOG (GNUNET_ERROR_TYPE_DEBUG,
2327 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2328 (unsigned int) ntohs (msg->size),
2329 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2333 LOG (GNUNET_ERROR_TYPE_DEBUG,
2334 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2335 (unsigned int) ntohs (msg->size),
2336 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2339 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2341 /* keep this 'rc' from expiring */
2342 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2343 (GNUNET_CONTAINER_HeapCostType)
2346 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2347 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2349 /* remove 'rc' that was inactive the longest */
2350 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2351 GNUNET_assert (NULL != d_ctx);
2352 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2353 GNUNET_free (d_ctx);
2359 * Read and process a message from the given socket.
2361 * @param plugin the overall plugin
2362 * @param rsock socket to read from
2365 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2369 char buf[65536] GNUNET_ALIGN;
2371 const struct GNUNET_MessageHeader *msg;
2373 fromlen = sizeof (addr);
2374 memset (&addr, 0, sizeof (addr));
2375 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2376 (struct sockaddr *) &addr, &fromlen);
2378 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2379 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2380 * on this socket has failed.
2382 * WSAECONNRESET - The virtual circuit was reset by the remote side
2383 * executing a hard or abortive close. The application should close
2384 * the socket; it is no longer usable. On a UDP-datagram socket this
2385 * error indicates a previous send operation resulted in an ICMP Port
2386 * Unreachable message.
2388 if ( (-1 == size) && (ECONNRESET == errno) )
2393 LOG (GNUNET_ERROR_TYPE_DEBUG,
2394 "UDP failed to receive data: %s\n", STRERROR (errno));
2395 /* Connection failure or something. Not a protocol violation. */
2398 if (size < sizeof (struct GNUNET_MessageHeader))
2400 LOG (GNUNET_ERROR_TYPE_WARNING,
2401 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2402 (unsigned int) size);
2403 /* _MAY_ be a connection failure (got partial message) */
2404 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2405 GNUNET_break_op (0);
2408 msg = (const struct GNUNET_MessageHeader *) buf;
2410 LOG (GNUNET_ERROR_TYPE_DEBUG,
2411 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2412 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2414 if (size != ntohs (msg->size))
2416 GNUNET_break_op (0);
2420 GNUNET_STATISTICS_update (plugin->env->stats,
2421 "# UDP, total, bytes, received",
2424 switch (ntohs (msg->type))
2426 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2427 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2430 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2431 read_process_msg (plugin, msg, addr, fromlen);
2434 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2435 read_process_ack (plugin, msg, addr, fromlen);
2438 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2439 read_process_fragment (plugin, msg, addr, fromlen);
2443 GNUNET_break_op (0);
2449 static struct UDP_MessageWrapper *
2450 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2451 struct GNUNET_NETWORK_Handle *sock)
2453 struct UDP_MessageWrapper *udpw = NULL;
2454 struct GNUNET_TIME_Relative remaining;
2457 while (udpw != NULL)
2459 /* Find messages with timeout */
2460 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2461 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2463 /* Message timed out */
2464 switch (udpw->msg_type) {
2465 case MSG_UNFRAGMENTED:
2466 GNUNET_STATISTICS_update (plugin->env->stats,
2467 "# UDP, total, bytes, sent, timeout",
2468 udpw->msg_size, GNUNET_NO);
2469 GNUNET_STATISTICS_update (plugin->env->stats,
2470 "# UDP, total, messages, sent, timeout",
2472 GNUNET_STATISTICS_update (plugin->env->stats,
2473 "# UDP, unfragmented msgs, messages, sent, timeout",
2475 GNUNET_STATISTICS_update (plugin->env->stats,
2476 "# UDP, unfragmented msgs, bytes, sent, timeout",
2477 udpw->payload_size, GNUNET_NO);
2478 /* Not fragmented message */
2479 LOG (GNUNET_ERROR_TYPE_DEBUG,
2480 "Message for peer `%s' with size %u timed out\n",
2481 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2482 call_continuation (udpw, GNUNET_SYSERR);
2483 /* Remove message */
2484 dequeue (plugin, udpw);
2487 case MSG_FRAGMENTED:
2488 /* Fragmented message */
2489 GNUNET_STATISTICS_update (plugin->env->stats,
2490 "# UDP, total, bytes, sent, timeout",
2491 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2492 GNUNET_STATISTICS_update (plugin->env->stats,
2493 "# UDP, total, messages, sent, timeout",
2495 call_continuation (udpw, GNUNET_SYSERR);
2496 LOG (GNUNET_ERROR_TYPE_DEBUG,
2497 "Fragment for message for peer `%s' with size %u timed out\n",
2498 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2501 GNUNET_STATISTICS_update (plugin->env->stats,
2502 "# UDP, fragmented msgs, messages, sent, timeout",
2504 GNUNET_STATISTICS_update (plugin->env->stats,
2505 "# UDP, fragmented msgs, bytes, sent, timeout",
2506 udpw->frag_ctx->payload_size, GNUNET_NO);
2507 /* Remove fragmented message due to timeout */
2508 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2511 GNUNET_STATISTICS_update (plugin->env->stats,
2512 "# UDP, total, bytes, sent, timeout",
2513 udpw->msg_size, GNUNET_NO);
2514 GNUNET_STATISTICS_update (plugin->env->stats,
2515 "# UDP, total, messages, sent, timeout",
2517 LOG (GNUNET_ERROR_TYPE_DEBUG,
2518 "ACK Message for peer `%s' with size %u timed out\n",
2519 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2520 call_continuation (udpw, GNUNET_SYSERR);
2521 dequeue (plugin, udpw);
2527 if (sock == plugin->sockv4)
2528 udpw = plugin->ipv4_queue_head;
2529 else if (sock == plugin->sockv6)
2530 udpw = plugin->ipv6_queue_head;
2533 GNUNET_break (0); /* should never happen */
2536 GNUNET_STATISTICS_update (plugin->env->stats,
2537 "# messages dismissed due to timeout",
2542 /* Message did not time out, check flow delay */
2543 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2544 if (GNUNET_TIME_UNIT_ZERO.rel_value_us == remaining.rel_value_us)
2546 /* this message is not delayed */
2547 LOG (GNUNET_ERROR_TYPE_DEBUG,
2548 "Message for peer `%s' (%u bytes) is not delayed \n",
2549 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2550 break; /* Found message to send, break */
2554 /* Message is delayed, try next */
2555 LOG (GNUNET_ERROR_TYPE_DEBUG,
2556 "Message for peer `%s' (%u bytes) is delayed for %s\n",
2557 GNUNET_i2s(&udpw->session->target), udpw->payload_size,
2558 GNUNET_STRINGS_relative_time_to_string (remaining,
2569 analyze_send_error (struct Plugin *plugin,
2570 const struct sockaddr * sa,
2574 static int network_down_error;
2575 struct GNUNET_ATS_Information type;
2577 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2578 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2579 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2581 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2583 /* IPv4: "Network unreachable" or "Network down"
2585 * This indicates we do not have connectivity
2587 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2588 _("UDP could not transmit message to `%s': "
2589 "Network seems down, please check your network configuration\n"),
2590 GNUNET_a2s (sa, slen));
2592 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2594 /* IPv6: "Network unreachable" or "Network down"
2596 * This indicates that this system is IPv6 enabled, but does not
2597 * have a valid global IPv6 address assigned or we do not have
2601 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2602 _("UDP could not transmit message to `%s': "
2603 "Please check your network configuration and disable IPv6 if your "
2604 "connection does not have a global IPv6 address\n"),
2605 GNUNET_a2s (sa, slen));
2610 LOG (GNUNET_ERROR_TYPE_WARNING,
2611 "UDP could not transmit message to `%s': `%s'\n",
2612 GNUNET_a2s (sa, slen), STRERROR (error));
2618 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2620 const struct sockaddr * sa;
2624 struct UDP_MessageWrapper *udpw = NULL;
2626 /* Find message to send */
2627 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2630 return 0; /* No message to send */
2632 sa = udpw->session->sock_addr;
2633 slen = udpw->session->addrlen;
2635 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2637 if (GNUNET_SYSERR == sent)
2640 analyze_send_error (plugin, sa, slen, errno);
2641 call_continuation(udpw, GNUNET_SYSERR);
2642 GNUNET_STATISTICS_update (plugin->env->stats,
2643 "# UDP, total, bytes, sent, failure",
2645 GNUNET_STATISTICS_update (plugin->env->stats,
2646 "# UDP, total, messages, sent, failure",
2652 LOG (GNUNET_ERROR_TYPE_DEBUG,
2653 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2654 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2655 (sent < 0) ? STRERROR (errno) : "ok");
2656 GNUNET_STATISTICS_update (plugin->env->stats,
2657 "# UDP, total, bytes, sent, success",
2659 GNUNET_STATISTICS_update (plugin->env->stats,
2660 "# UDP, total, messages, sent, success",
2662 if (NULL != udpw->frag_ctx)
2663 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2664 call_continuation (udpw, GNUNET_OK);
2666 dequeue (plugin, udpw);
2675 * We have been notified that our readset has something to read. We don't
2676 * know which socket needs to be read, so we have to check each one
2677 * Then reschedule this function to be called again once more is available.
2679 * @param cls the plugin handle
2680 * @param tc the scheduling context (for rescheduling this function again)
2683 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2685 struct Plugin *plugin = cls;
2687 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2688 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2690 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2691 (NULL != plugin->sockv4) &&
2692 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2693 udp_select_read (plugin, plugin->sockv4);
2694 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2695 (NULL != plugin->sockv4) &&
2696 (NULL != plugin->ipv4_queue_head) &&
2697 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2698 udp_select_send (plugin, plugin->sockv4);
2699 schedule_select (plugin);
2704 * We have been notified that our readset has something to read. We don't
2705 * know which socket needs to be read, so we have to check each one
2706 * Then reschedule this function to be called again once more is available.
2708 * @param cls the plugin handle
2709 * @param tc the scheduling context (for rescheduling this function again)
2712 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2714 struct Plugin *plugin = cls;
2716 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2717 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2719 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2720 (NULL != plugin->sockv6) &&
2721 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2722 udp_select_read (plugin, plugin->sockv6);
2723 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2724 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2725 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2726 udp_select_send (plugin, plugin->sockv6);
2727 schedule_select (plugin);
2733 * @return number of sockets that were successfully bound
2736 setup_sockets (struct Plugin *plugin,
2737 const struct sockaddr_in6 *bind_v6,
2738 const struct sockaddr_in *bind_v4)
2741 int sockets_created = 0;
2742 struct sockaddr_in6 serverAddrv6;
2743 struct sockaddr_in serverAddrv4;
2744 struct sockaddr *serverAddr;
2745 struct sockaddr *addrs[2];
2746 socklen_t addrlens[2];
2750 /* Create IPv6 socket */
2752 if (plugin->enable_ipv6 == GNUNET_YES)
2754 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2755 if (NULL == plugin->sockv6)
2757 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2758 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2759 plugin->enable_ipv6 = GNUNET_NO;
2763 memset (&serverAddrv6, '\0', sizeof (struct sockaddr_in6));
2764 #if HAVE_SOCKADDR_IN_SIN_LEN
2765 serverAddrv6.sin6_len = sizeof (struct sockaddr_in6);
2767 serverAddrv6.sin6_family = AF_INET6;
2768 if (NULL != bind_v6)
2769 serverAddrv6.sin6_addr = bind_v6->sin6_addr;
2771 serverAddrv6.sin6_addr = in6addr_any;
2773 if (0 == plugin->port) /* autodetect */
2774 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2776 serverAddrv6.sin6_port = htons (plugin->port);
2777 addrlen = sizeof (struct sockaddr_in6);
2778 serverAddr = (struct sockaddr *) &serverAddrv6;
2783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2784 GNUNET_a2s (serverAddr, addrlen));
2786 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2787 serverAddr, addrlen, 0))
2790 if (0 != plugin->port)
2792 tries = 10; /* fail */
2793 break; /* bind failed on specific port */
2796 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2801 GNUNET_NETWORK_socket_close (plugin->sockv6);
2802 plugin->enable_ipv6 = GNUNET_NO;
2803 plugin->sockv6 = NULL;
2806 if (plugin->sockv6 != NULL)
2808 LOG (GNUNET_ERROR_TYPE_DEBUG,
2809 "IPv6 socket created on port %s\n",
2810 GNUNET_a2s (serverAddr, addrlen));
2811 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2812 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2817 LOG (GNUNET_ERROR_TYPE_ERROR,
2818 "Failed to bind UDP socket to %s: %s\n",
2819 GNUNET_a2s (serverAddr, addrlen),
2825 /* Create IPv4 socket */
2827 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2828 if (NULL == plugin->sockv4)
2830 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2831 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2832 plugin->enable_ipv4 = GNUNET_NO;
2836 memset (&serverAddrv4, '\0', sizeof (struct sockaddr_in));
2837 #if HAVE_SOCKADDR_IN_SIN_LEN
2838 serverAddrv4.sin_len = sizeof (struct sockaddr_in);
2840 serverAddrv4.sin_family = AF_INET;
2841 if (NULL != bind_v4)
2842 serverAddrv4.sin_addr = bind_v4->sin_addr;
2844 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2846 if (0 == plugin->port)
2848 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2850 serverAddrv4.sin_port = htons (plugin->port);
2853 addrlen = sizeof (struct sockaddr_in);
2854 serverAddr = (struct sockaddr *) &serverAddrv4;
2859 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2860 GNUNET_a2s (serverAddr, addrlen));
2863 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2864 serverAddr, addrlen, 0))
2867 if (0 != plugin->port)
2869 tries = 10; /* fail */
2870 break; /* bind failed on specific port */
2874 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2880 GNUNET_NETWORK_socket_close (plugin->sockv4);
2881 plugin->enable_ipv4 = GNUNET_NO;
2882 plugin->sockv4 = NULL;
2885 if (plugin->sockv4 != NULL)
2887 LOG (GNUNET_ERROR_TYPE_DEBUG,
2888 "IPv4 socket created on port %s\n", GNUNET_a2s (serverAddr, addrlen));
2889 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2890 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2895 LOG (GNUNET_ERROR_TYPE_ERROR,
2896 "Failed to bind UDP socket to %s: %s\n",
2897 GNUNET_a2s (serverAddr, addrlen), STRERROR (eno));
2901 if (0 == sockets_created)
2903 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2904 return 0; /* No sockets created, return */
2907 /* Create file descriptors */
2908 if (plugin->enable_ipv4 == GNUNET_YES)
2910 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2911 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2912 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2913 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2914 if (NULL != plugin->sockv4)
2916 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2917 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2921 if (plugin->enable_ipv6 == GNUNET_YES)
2923 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2924 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2925 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2926 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2927 if (NULL != plugin->sockv6)
2929 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2930 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2934 schedule_select (plugin);
2935 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2936 GNUNET_NO, plugin->port,
2938 (const struct sockaddr **) addrs, addrlens,
2939 &udp_nat_port_map_callback, NULL, plugin);
2941 return sockets_created;
2946 * The exported method. Makes the core api available via a global and
2947 * returns the udp transport API.
2949 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2950 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2953 libgnunet_plugin_transport_udp_init (void *cls)
2955 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2956 struct GNUNET_TRANSPORT_PluginFunctions *api;
2958 unsigned long long port;
2959 unsigned long long aport;
2960 unsigned long long broadcast;
2961 unsigned long long udp_max_bps;
2962 unsigned long long enable_v6;
2963 char * bind4_address;
2964 char * bind6_address;
2965 char * fancy_interval;
2966 struct GNUNET_TIME_Relative interval;
2967 struct sockaddr_in serverAddrv4;
2968 struct sockaddr_in6 serverAddrv6;
2973 if (NULL == env->receive)
2975 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2976 initialze the plugin or the API */
2977 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2979 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2980 api->address_to_string = &udp_address_to_string;
2981 api->string_to_address = &udp_string_to_address;
2985 GNUNET_assert (NULL != env->stats);
2987 /* Get port number: port == 0 : autodetect a port,
2988 * > 0 : use this port,
2989 * not given : 2086 default */
2991 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2995 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2996 "ADVERTISED_PORT", &aport))
3000 LOG (GNUNET_ERROR_TYPE_WARNING,
3001 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3008 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3010 enable_v6 = GNUNET_NO;
3012 enable_v6 = GNUNET_YES;
3015 have_bind4 = GNUNET_NO;
3016 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
3018 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3019 "BINDTO", &bind4_address))
3021 LOG (GNUNET_ERROR_TYPE_DEBUG,
3022 "Binding udp plugin to specific address: `%s'\n",
3024 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
3026 GNUNET_free (bind4_address);
3029 have_bind4 = GNUNET_YES;
3031 GNUNET_free_non_null (bind4_address);
3032 have_bind6 = GNUNET_NO;
3033 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
3035 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3036 "BINDTO6", &bind6_address))
3038 LOG (GNUNET_ERROR_TYPE_DEBUG,
3039 "Binding udp plugin to specific address: `%s'\n",
3042 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
3044 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3046 GNUNET_free (bind6_address);
3049 have_bind6 = GNUNET_YES;
3051 GNUNET_free_non_null (bind6_address);
3053 /* Initialize my flags */
3056 /* Enable neighbour discovery */
3057 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3059 if (broadcast == GNUNET_SYSERR)
3060 broadcast = GNUNET_NO;
3062 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3063 "BROADCAST_INTERVAL", &fancy_interval))
3065 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3069 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3071 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3073 GNUNET_free (fancy_interval);
3076 /* Maximum datarate */
3077 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3078 "MAX_BPS", &udp_max_bps))
3080 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3083 p = GNUNET_malloc (sizeof (struct Plugin));
3086 p->broadcast_interval = interval;
3087 p->enable_ipv6 = enable_v6;
3088 p->enable_ipv4 = GNUNET_YES; /* default */
3090 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
3091 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3092 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3093 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3094 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
3097 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3098 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &serverAddrv6 : NULL,
3099 (GNUNET_YES == have_bind4) ? &serverAddrv4 : NULL);
3100 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
3102 LOG (GNUNET_ERROR_TYPE_ERROR,
3103 _("Failed to create network sockets, plugin failed\n"));
3104 GNUNET_CONTAINER_multihashmap_destroy (p->sessions);
3105 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3106 GNUNET_SERVER_mst_destroy (p->mst);
3110 else if (broadcast == GNUNET_YES)
3112 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3113 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
3116 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3119 api->disconnect = &udp_disconnect;
3120 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3121 api->address_to_string = &udp_address_to_string;
3122 api->string_to_address = &udp_string_to_address;
3123 api->check_address = &udp_plugin_check_address;
3124 api->get_session = &udp_plugin_get_session;
3125 api->send = &udp_plugin_send;
3126 api->get_network = &udp_get_network;
3133 heap_cleanup_iterator (void *cls,
3134 struct GNUNET_CONTAINER_HeapNode *
3135 node, void *element,
3136 GNUNET_CONTAINER_HeapCostType
3139 struct DefragContext * d_ctx = element;
3141 GNUNET_CONTAINER_heap_remove_node (node);
3142 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3143 GNUNET_free (d_ctx);
3150 * The exported method. Makes the core api available via a global and
3151 * returns the udp transport API.
3153 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3157 libgnunet_plugin_transport_udp_done (void *cls)
3159 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3160 struct Plugin *plugin = api->cls;
3161 struct PrettyPrinterContext *cur;
3162 struct PrettyPrinterContext *next;
3170 stop_broadcast (plugin);
3171 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3173 GNUNET_SCHEDULER_cancel (plugin->select_task);
3174 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3176 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3178 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3179 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3182 /* Closing sockets */
3183 if (GNUNET_YES ==plugin->enable_ipv4)
3185 if (plugin->sockv4 != NULL)
3187 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3188 plugin->sockv4 = NULL;
3190 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3191 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3193 if (GNUNET_YES ==plugin->enable_ipv6)
3195 if (plugin->sockv6 != NULL)
3197 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3198 plugin->sockv6 = NULL;
3200 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3201 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3204 if (NULL != plugin->nat)
3205 GNUNET_NAT_unregister (plugin->nat);
3207 if (plugin->defrag_ctxs != NULL)
3209 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3210 heap_cleanup_iterator, NULL);
3211 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3212 plugin->defrag_ctxs = NULL;
3214 if (plugin->mst != NULL)
3216 GNUNET_SERVER_mst_destroy(plugin->mst);
3220 /* Clean up leftover messages */
3221 struct UDP_MessageWrapper * udpw;
3222 udpw = plugin->ipv4_queue_head;
3223 while (udpw != NULL)
3225 struct UDP_MessageWrapper *tmp = udpw->next;
3226 dequeue (plugin, udpw);
3227 call_continuation(udpw, GNUNET_SYSERR);
3232 udpw = plugin->ipv6_queue_head;
3233 while (udpw != NULL)
3235 struct UDP_MessageWrapper *tmp = udpw->next;
3236 dequeue (plugin, udpw);
3237 call_continuation(udpw, GNUNET_SYSERR);
3243 /* Clean up sessions */
3244 LOG (GNUNET_ERROR_TYPE_DEBUG,
3245 "Cleaning up sessions\n");
3246 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3247 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
3249 next = ppc_dll_head;
3250 for (cur = next; NULL != cur; cur = next)
3253 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3254 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3255 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3261 GNUNET_free (plugin);
3264 struct Allocation *allocation;
3265 while (NULL != ahead)
3268 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3269 GNUNET_free (allocation);
3271 struct Allocator *allocator;
3272 while (NULL != aehead)
3275 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3276 GNUNET_free (allocator);
3283 /* end of plugin_transport_udp.c */