2 This file is part of GNUnet
3 (C) 2010, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/plugin_transport_udp.c
23 * @brief Implementation of the UDP transport protocol
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Matthias Wachs
29 #include "plugin_transport_udp.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_util_lib.h"
32 #include "gnunet_fragmentation_lib.h"
33 #include "gnunet_nat_lib.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_resolver_service.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_constants.h"
38 #include "gnunet_statistics_service.h"
39 #include "gnunet_transport_service.h"
40 #include "gnunet_transport_plugin.h"
41 #include "transport.h"
43 #define LOG(kind,...) GNUNET_log_from (kind, "transport-udp", __VA_ARGS__)
45 #define PLUGIN_NAME "udp"
48 * Number of messages we can defragment in parallel. We only really
49 * defragment 1 message at a time, but if messages get re-ordered, we
50 * may want to keep knowledge about the previous message to avoid
51 * discarding the current message in favor of a single fragment of a
52 * previous message. 3 should be good since we don't expect massive
53 * message reorderings with UDP.
55 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
58 * We keep a defragmentation queue per sender address. How many
59 * sender addresses do we support at the same time? Memory consumption
60 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
61 * value. (So 128 corresponds to 12 MB and should suffice for
62 * connecting to roughly 128 peers via UDP).
64 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
67 * 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) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
514 (0 == min_delay.rel_value) ? 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) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
530 (0 == min_delay.rel_value) ? 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);
590 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
592 GNUNET_snprintf (rbuf, sizeof (rbuf), (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;
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;
798 if (addrlen == sizeof (struct IPv6UdpAddress))
801 memset (&a6, 0, sizeof (a6));
802 a6.sin6_family = AF_INET6;
803 #if HAVE_SOCKADDR_IN_SIN_LEN
804 a6.sin6_len = sizeof (a6);
806 a6.sin6_port = u6->u6_port;
807 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
808 port = ntohs (u6->u6_port);
809 options = ntohl (u6->options);
813 else if (addrlen == sizeof (struct IPv4UdpAddress))
816 memset (&a4, 0, sizeof (a4));
817 a4.sin_family = AF_INET;
818 #if HAVE_SOCKADDR_IN_SIN_LEN
819 a4.sin_len = sizeof (a4);
821 a4.sin_port = u4->u4_port;
822 a4.sin_addr.s_addr = u4->ipv4_addr;
823 port = ntohs (u4->u4_port);
824 options = ntohl (u4->options);
828 else if (0 == addrlen)
830 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
836 /* invalid address */
841 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
843 ppc->asc_cls = asc_cls;
845 ppc->options = options;
846 if (addrlen == sizeof (struct IPv6UdpAddress))
847 ppc->ipv6 = GNUNET_YES;
849 ppc->ipv6 = GNUNET_NO;
850 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
851 &ppc_cancel_task, ppc);
852 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
853 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was created!\n", ppc); */
854 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
860 call_continuation (struct UDP_MessageWrapper *udpw, int result)
864 LOG (GNUNET_ERROR_TYPE_DEBUG,
865 "Calling continuation for %u byte message to `%s' with result %s\n",
866 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
867 (GNUNET_OK == result) ? "OK" : "SYSERR");
869 if (udpw->msg_size >= udpw->payload_size)
870 overhead = udpw->msg_size - udpw->payload_size;
872 overhead = udpw->msg_size;
876 switch (udpw->msg_type) {
877 case MSG_UNFRAGMENTED:
878 if (NULL != udpw->cont)
880 /* Transport continuation */
881 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
882 udpw->payload_size, udpw->msg_size);
884 GNUNET_STATISTICS_update (plugin->env->stats,
885 "# UDP, unfragmented msgs, messages, sent, success",
887 GNUNET_STATISTICS_update (plugin->env->stats,
888 "# UDP, unfragmented msgs, bytes payload, sent, success",
889 udpw->payload_size, GNUNET_NO);
890 GNUNET_STATISTICS_update (plugin->env->stats,
891 "# UDP, unfragmented msgs, bytes overhead, sent, success",
892 overhead, GNUNET_NO);
893 GNUNET_STATISTICS_update (plugin->env->stats,
894 "# UDP, total, bytes overhead, sent",
895 overhead, GNUNET_NO);
896 GNUNET_STATISTICS_update (plugin->env->stats,
897 "# UDP, total, bytes payload, sent",
898 udpw->payload_size, GNUNET_NO);
900 case MSG_FRAGMENTED_COMPLETE:
901 GNUNET_assert (NULL != udpw->frag_ctx);
902 if (udpw->frag_ctx->cont != NULL)
903 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
904 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
905 GNUNET_STATISTICS_update (plugin->env->stats,
906 "# UDP, fragmented msgs, messages, sent, success",
908 GNUNET_STATISTICS_update (plugin->env->stats,
909 "# UDP, fragmented msgs, bytes payload, sent, success",
910 udpw->payload_size, GNUNET_NO);
911 GNUNET_STATISTICS_update (plugin->env->stats,
912 "# UDP, fragmented msgs, bytes overhead, sent, success",
913 overhead, GNUNET_NO);
914 GNUNET_STATISTICS_update (plugin->env->stats,
915 "# UDP, total, bytes overhead, sent",
916 overhead, GNUNET_NO);
917 GNUNET_STATISTICS_update (plugin->env->stats,
918 "# UDP, total, bytes payload, sent",
919 udpw->payload_size, GNUNET_NO);
920 GNUNET_STATISTICS_update (plugin->env->stats,
921 "# UDP, fragmented msgs, messages, pending",
925 /* Fragmented message: enqueue next fragment */
926 if (NULL != udpw->cont)
927 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
928 udpw->payload_size, udpw->msg_size);
929 GNUNET_STATISTICS_update (plugin->env->stats,
930 "# UDP, fragmented msgs, fragments, sent, success",
932 GNUNET_STATISTICS_update (plugin->env->stats,
933 "# UDP, fragmented msgs, fragments bytes, sent, success",
934 udpw->msg_size, GNUNET_NO);
937 /* No continuation */
938 GNUNET_STATISTICS_update (plugin->env->stats,
939 "# UDP, ACK msgs, messages, sent, success",
941 GNUNET_STATISTICS_update (plugin->env->stats,
942 "# UDP, ACK msgs, bytes overhead, sent, success",
943 overhead, GNUNET_NO);
944 GNUNET_STATISTICS_update (plugin->env->stats,
945 "# UDP, total, bytes overhead, sent",
946 overhead, GNUNET_NO);
952 LOG (GNUNET_ERROR_TYPE_ERROR,
953 "ERROR: %u\n", udpw->msg_type);
959 switch (udpw->msg_type) {
960 case MSG_UNFRAGMENTED:
961 /* Unfragmented message: failed to send */
962 if (NULL != udpw->cont)
963 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
964 udpw->payload_size, overhead);
965 GNUNET_STATISTICS_update (plugin->env->stats,
966 "# UDP, unfragmented msgs, messages, sent, failure",
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 "# UDP, unfragmented msgs, bytes payload, sent, failure",
970 udpw->payload_size, GNUNET_NO);
971 GNUNET_STATISTICS_update (plugin->env->stats,
972 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
973 overhead, GNUNET_NO);
975 case MSG_FRAGMENTED_COMPLETE:
976 GNUNET_assert (NULL != udpw->frag_ctx);
977 if (udpw->frag_ctx->cont != NULL)
978 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
979 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
980 GNUNET_STATISTICS_update (plugin->env->stats,
981 "# UDP, fragmented msgs, messages, sent, failure",
983 GNUNET_STATISTICS_update (plugin->env->stats,
984 "# UDP, fragmented msgs, bytes payload, sent, failure",
985 udpw->payload_size, GNUNET_NO);
986 GNUNET_STATISTICS_update (plugin->env->stats,
987 "# UDP, fragmented msgs, bytes payload, sent, failure",
988 overhead, GNUNET_NO);
989 GNUNET_STATISTICS_update (plugin->env->stats,
990 "# UDP, fragmented msgs, bytes payload, sent, failure",
991 overhead, GNUNET_NO);
992 GNUNET_STATISTICS_update (plugin->env->stats,
993 "# UDP, fragmented msgs, messages, pending",
997 GNUNET_assert (NULL != udpw->frag_ctx);
998 /* Fragmented message: failed to send */
999 GNUNET_STATISTICS_update (plugin->env->stats,
1000 "# UDP, fragmented msgs, fragments, sent, failure",
1002 GNUNET_STATISTICS_update (plugin->env->stats,
1003 "# UDP, fragmented msgs, fragments bytes, sent, failure",
1004 udpw->msg_size, GNUNET_NO);
1007 /* ACK message: failed to send */
1008 GNUNET_STATISTICS_update (plugin->env->stats,
1009 "# UDP, ACK msgs, messages, sent, failure",
1013 /* Beacon message: failed to send */
1029 * Check if the given port is plausible (must be either our listen
1030 * port or our advertised port). If it is neither, we return
1033 * @param plugin global variables
1034 * @param in_port port number to check
1035 * @return GNUNET_OK if port is either open_port or adv_port
1038 check_port (struct Plugin *plugin, uint16_t in_port)
1040 if ((in_port == plugin->port) || (in_port == plugin->aport))
1042 return GNUNET_SYSERR;
1047 * Function that will be called to check if a binary address for this
1048 * plugin is well-formed and corresponds to an address for THIS peer
1049 * (as per our configuration). Naturally, if absolutely necessary,
1050 * plugins can be a bit conservative in their answer, but in general
1051 * plugins should make sure that the address does not redirect
1052 * traffic to a 3rd party that might try to man-in-the-middle our
1055 * @param cls closure, should be our handle to the Plugin
1056 * @param addr pointer to the address
1057 * @param addrlen length of addr
1058 * @return GNUNET_OK if this is a plausible address for this peer
1059 * and transport, GNUNET_SYSERR if not
1063 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1065 struct Plugin *plugin = cls;
1066 struct IPv4UdpAddress *v4;
1067 struct IPv6UdpAddress *v6;
1069 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
1070 (addrlen != sizeof (struct IPv6UdpAddress)))
1072 GNUNET_break_op (0);
1073 return GNUNET_SYSERR;
1075 if (addrlen == sizeof (struct IPv4UdpAddress))
1077 v4 = (struct IPv4UdpAddress *) addr;
1078 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
1079 return GNUNET_SYSERR;
1081 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1082 sizeof (struct in_addr)))
1083 return GNUNET_SYSERR;
1087 v6 = (struct IPv6UdpAddress *) addr;
1088 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1090 GNUNET_break_op (0);
1091 return GNUNET_SYSERR;
1093 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
1094 return GNUNET_SYSERR;
1096 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1097 sizeof (struct in6_addr)))
1098 return GNUNET_SYSERR;
1105 * Task to free resources associated with a session.
1107 * @param s session to free
1110 free_session (struct Session *s)
1112 if (NULL != s->frag_ctx)
1114 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
1115 GNUNET_free (s->frag_ctx);
1123 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1125 if (plugin->bytes_in_buffer < udpw->msg_size)
1129 GNUNET_STATISTICS_update (plugin->env->stats,
1130 "# UDP, total, bytes in buffers",
1131 - (long long) udpw->msg_size, GNUNET_NO);
1132 plugin->bytes_in_buffer -= udpw->msg_size;
1134 GNUNET_STATISTICS_update (plugin->env->stats,
1135 "# UDP, total, msgs in buffers",
1137 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1138 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
1139 plugin->ipv4_queue_tail, udpw);
1140 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1141 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
1142 plugin->ipv6_queue_tail, udpw);
1146 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1148 struct UDP_MessageWrapper *udpw;
1149 struct UDP_MessageWrapper *tmp;
1150 struct UDP_MessageWrapper dummy;
1151 struct Session *s = fc->session;
1153 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1155 /* Call continuation for fragmented message */
1156 memset (&dummy, 0, sizeof (dummy));
1157 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1158 dummy.msg_size = s->frag_ctx->on_wire_size;
1159 dummy.payload_size = s->frag_ctx->payload_size;
1160 dummy.frag_ctx = s->frag_ctx;
1162 dummy.cont_cls = NULL;
1165 call_continuation (&dummy, result);
1167 /* Remove leftover fragments from queue */
1168 if (s->addrlen == sizeof (struct sockaddr_in6))
1170 udpw = plugin->ipv6_queue_head;
1171 while (NULL != udpw)
1174 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1176 dequeue (plugin, udpw);
1177 call_continuation (udpw, GNUNET_SYSERR);
1183 if (s->addrlen == sizeof (struct sockaddr_in))
1185 udpw = plugin->ipv4_queue_head;
1189 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1191 dequeue (plugin, udpw);
1192 call_continuation (udpw, GNUNET_SYSERR);
1199 /* Destroy fragmentation context */
1200 GNUNET_FRAGMENT_context_destroy (fc->frag,
1201 &s->last_expected_msg_delay,
1202 &s->last_expected_ack_delay);
1208 * Functions with this signature are called whenever we need
1209 * to close a session due to a disconnect or failure to
1210 * establish a connection.
1212 * @param s session to close down
1215 disconnect_session (struct Session *s)
1217 struct UDP_MessageWrapper *udpw;
1218 struct UDP_MessageWrapper *next;
1220 GNUNET_assert (GNUNET_YES != s->in_destroy);
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "Session %p to peer `%s' address ended \n",
1224 GNUNET_i2s (&s->target),
1225 GNUNET_a2s (s->sock_addr, s->addrlen));
1226 stop_session_timeout (s);
1228 if (NULL != s->frag_ctx)
1230 /* Remove fragmented message due to disconnect */
1231 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1234 next = plugin->ipv4_queue_head;
1235 while (NULL != (udpw = next))
1238 if (udpw->session == s)
1240 dequeue (plugin, udpw);
1241 call_continuation(udpw, GNUNET_SYSERR);
1245 next = plugin->ipv6_queue_head;
1246 while (NULL != (udpw = next))
1249 if (udpw->session == s)
1251 dequeue (plugin, udpw);
1252 call_continuation(udpw, GNUNET_SYSERR);
1257 plugin->env->session_end (plugin->env->cls, &s->target, s);
1259 if (NULL != s->frag_ctx)
1261 if (NULL != s->frag_ctx->cont)
1263 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1264 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1267 GNUNET_i2s (&s->target));
1271 GNUNET_assert (GNUNET_YES ==
1272 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1273 &s->target.hashPubKey,
1275 GNUNET_STATISTICS_set(plugin->env->stats,
1276 "# UDP, sessions active",
1277 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1280 s->in_destroy = GNUNET_YES;
1286 * Destroy a session, plugin is being unloaded.
1289 * @param key hash of public key of target peer
1290 * @param value a 'struct PeerSession*' to clean up
1291 * @return GNUNET_OK (continue to iterate)
1294 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1296 disconnect_session(value);
1302 * Disconnect from a remote node. Clean up session if we have one for this peer
1304 * @param cls closure for this call (should be handle to Plugin)
1305 * @param target the peeridentity of the peer to disconnect
1306 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1309 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1311 struct Plugin *plugin = cls;
1312 GNUNET_assert (plugin != NULL);
1314 GNUNET_assert (target != NULL);
1315 LOG (GNUNET_ERROR_TYPE_DEBUG,
1316 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1317 /* Clean up sessions */
1318 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1323 * Session was idle, so disconnect it
1326 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1328 GNUNET_assert (NULL != cls);
1329 struct Session *s = cls;
1331 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1333 "Session %p was idle for %llu ms, disconnecting\n",
1334 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
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 %llu ms\n",
1353 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1358 * Increment session timeout due to activity
1361 reschedule_session_timeout (struct Session *s)
1363 GNUNET_assert (NULL != s);
1364 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1366 GNUNET_SCHEDULER_cancel (s->timeout_task);
1367 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Timeout rescheduled for session %p set to %llu ms\n",
1372 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1380 stop_session_timeout (struct Session *s)
1382 GNUNET_assert (NULL != s);
1384 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1386 GNUNET_SCHEDULER_cancel (s->timeout_task);
1387 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1389 "Timeout stopped for session %p canceled\n",
1390 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1395 static struct Session *
1396 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1397 const void *addr, size_t addrlen,
1398 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1401 const struct IPv4UdpAddress *t4;
1402 const struct IPv6UdpAddress *t6;
1403 struct sockaddr_in *v4;
1404 struct sockaddr_in6 *v6;
1409 case sizeof (struct IPv4UdpAddress):
1410 if (NULL == plugin->sockv4)
1412 LOG (GNUNET_ERROR_TYPE_DEBUG,
1413 "Could not create session for peer `%s' address `%s': IPv4 is not enabled\n",
1415 udp_address_to_string(NULL, addr, addrlen));
1419 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1420 len = sizeof (struct sockaddr_in);
1421 v4 = (struct sockaddr_in *) &s[1];
1422 v4->sin_family = AF_INET;
1423 #if HAVE_SOCKADDR_IN_SIN_LEN
1424 v4->sin_len = sizeof (struct sockaddr_in);
1426 v4->sin_port = t4->u4_port;
1427 v4->sin_addr.s_addr = t4->ipv4_addr;
1428 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1430 case sizeof (struct IPv6UdpAddress):
1431 if (NULL == plugin->sockv6)
1433 LOG (GNUNET_ERROR_TYPE_INFO,
1434 "Could not create session for peer `%s' address `%s': IPv6 is not enabled\n",
1436 udp_address_to_string(NULL, addr, addrlen));
1441 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1442 len = sizeof (struct sockaddr_in6);
1443 v6 = (struct sockaddr_in6 *) &s[1];
1444 v6->sin6_family = AF_INET6;
1445 #if HAVE_SOCKADDR_IN_SIN_LEN
1446 v6->sin6_len = sizeof (struct sockaddr_in6);
1448 v6->sin6_port = t6->u6_port;
1449 v6->sin6_addr = t6->ipv6_addr;
1450 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1453 /* Must have a valid address to send to */
1454 GNUNET_break_op (0);
1458 s->target = *target;
1459 s->sock_addr = (const struct sockaddr *) &s[1];
1460 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1461 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1462 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1463 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1464 s->inbound = GNUNET_NO;
1465 start_session_timeout (s);
1471 session_cmp_it (void *cls,
1472 const struct GNUNET_HashCode * key,
1475 struct SessionCompareContext * cctx = cls;
1476 const struct GNUNET_HELLO_Address *address = cctx->addr;
1477 struct Session *s = value;
1479 socklen_t s_addrlen = s->addrlen;
1481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1482 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1483 GNUNET_a2s (s->sock_addr, s->addrlen));
1484 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1485 (s_addrlen == sizeof (struct sockaddr_in)))
1487 struct IPv4UdpAddress * u4 = NULL;
1488 u4 = (struct IPv4UdpAddress *) address->address;
1489 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1490 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1491 (u4->u4_port == s4->sin_port))
1498 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1499 (s_addrlen == sizeof (struct sockaddr_in6)))
1501 struct IPv6UdpAddress * u6 = NULL;
1502 u6 = (struct IPv6UdpAddress *) address->address;
1503 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1504 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1505 (u6->u6_port == s6->sin6_port))
1516 * Function obtain the network type for a session
1518 * @param cls closure ('struct Plugin*')
1519 * @param session the session
1520 * @return the network type in HBO or GNUNET_SYSERR
1522 static enum GNUNET_ATS_Network_Type
1523 udp_get_network (void *cls,
1524 struct Session *session)
1526 return ntohl (session->ats.value);
1531 * Creates a new outbound session the transport service will use to send data to the
1534 * @param cls the plugin
1535 * @param address the address
1536 * @return the session or NULL of max connections exceeded
1538 static struct Session *
1539 udp_plugin_lookup_session (void *cls,
1540 const struct GNUNET_HELLO_Address *address)
1542 struct Plugin * plugin = cls;
1543 struct IPv6UdpAddress * udp_a6;
1544 struct IPv4UdpAddress * udp_a4;
1546 GNUNET_assert (plugin != NULL);
1547 GNUNET_assert (address != NULL);
1550 if ((address->address == NULL) ||
1551 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1552 (address->address_length != sizeof (struct IPv6UdpAddress))))
1554 LOG (GNUNET_ERROR_TYPE_WARNING,
1555 _("Trying to create session for address of unexpected length %u (should be %u or %u)\n"),
1556 address->address_length,
1557 sizeof (struct IPv4UdpAddress),
1558 sizeof (struct IPv6UdpAddress));
1562 if (address->address_length == sizeof (struct IPv4UdpAddress))
1564 if (plugin->sockv4 == NULL)
1566 udp_a4 = (struct IPv4UdpAddress *) address->address;
1567 if (udp_a4->u4_port == 0)
1571 if (address->address_length == sizeof (struct IPv6UdpAddress))
1573 if (plugin->sockv6 == NULL)
1575 udp_a6 = (struct IPv6UdpAddress *) address->address;
1576 if (udp_a6->u6_port == 0)
1580 /* check if session already exists */
1581 struct SessionCompareContext cctx;
1582 cctx.addr = address;
1584 LOG (GNUNET_ERROR_TYPE_DEBUG,
1585 "Looking for existing session for peer `%s' `%s' \n",
1586 GNUNET_i2s (&address->peer),
1587 udp_address_to_string(NULL, address->address, address->address_length));
1588 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1589 if (cctx.res != NULL)
1591 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1597 static struct Session *
1598 udp_plugin_create_session (void *cls,
1599 const struct GNUNET_HELLO_Address *address)
1601 struct Session * s = NULL;
1603 /* otherwise create new */
1604 s = create_session (plugin,
1607 address->address_length,
1610 return NULL; /* protocol not supported or address invalid */
1611 LOG (GNUNET_ERROR_TYPE_DEBUG,
1612 "Creating new session %p for peer `%s' address `%s'\n",
1614 GNUNET_i2s(&address->peer),
1615 udp_address_to_string(NULL,address->address,address->address_length));
1616 GNUNET_assert (GNUNET_OK ==
1617 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1618 &s->target.hashPubKey,
1620 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1621 GNUNET_STATISTICS_set(plugin->env->stats,
1622 "# UDP, sessions active",
1623 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1631 * Creates a new outbound session the transport service will use to send data to the
1634 * @param cls the plugin
1635 * @param address the address
1636 * @return the session or NULL of max connections exceeded
1638 static struct Session *
1639 udp_plugin_get_session (void *cls,
1640 const struct GNUNET_HELLO_Address *address)
1642 struct Session * s = NULL;
1644 if (NULL == address)
1649 if ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1650 (address->address_length != sizeof (struct IPv6UdpAddress)))
1653 /* otherwise create new */
1654 if (NULL != (s = udp_plugin_lookup_session(cls, address)))
1657 return udp_plugin_create_session (cls, address);
1662 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1664 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1668 GNUNET_STATISTICS_update (plugin->env->stats,
1669 "# UDP, total, bytes in buffers",
1670 udpw->msg_size, GNUNET_NO);
1671 plugin->bytes_in_buffer += udpw->msg_size;
1673 GNUNET_STATISTICS_update (plugin->env->stats,
1674 "# UDP, total, msgs in buffers",
1676 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1677 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1678 plugin->ipv4_queue_tail, udpw);
1679 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1680 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1681 plugin->ipv6_queue_tail, udpw);
1687 * Fragment message was transmitted via UDP, let fragmentation know
1688 * to send the next fragment now.
1690 * @param cls the 'struct UDPMessageWrapper' of the fragment
1691 * @param target destination peer (ignored)
1692 * @param result GNUNET_OK on success (ignored)
1693 * @param payload bytes payload sent
1694 * @param physical bytes physical sent
1697 send_next_fragment (void *cls,
1698 const struct GNUNET_PeerIdentity *target,
1699 int result, size_t payload, size_t physical)
1701 struct UDP_MessageWrapper *udpw = cls;
1702 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1707 * Function that is called with messages created by the fragmentation
1708 * module. In the case of the 'proc' callback of the
1709 * GNUNET_FRAGMENT_context_create function, this function must
1710 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1712 * @param cls closure, the 'struct FragmentationContext'
1713 * @param msg the message that was created
1716 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1718 struct UDP_FragmentationContext *frag_ctx = cls;
1719 struct Plugin *plugin = frag_ctx->plugin;
1720 struct UDP_MessageWrapper * udpw;
1721 size_t msg_len = ntohs (msg->size);
1723 LOG (GNUNET_ERROR_TYPE_DEBUG,
1724 "Enqueuing fragment with %u bytes\n", msg_len);
1725 frag_ctx->fragments_used ++;
1726 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1727 udpw->session = frag_ctx->session;
1728 udpw->msg_buf = (char *) &udpw[1];
1729 udpw->msg_size = msg_len;
1730 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1731 udpw->cont = &send_next_fragment;
1732 udpw->cont_cls = udpw;
1733 udpw->timeout = frag_ctx->timeout;
1734 udpw->frag_ctx = frag_ctx;
1735 udpw->msg_type = MSG_FRAGMENTED;
1736 memcpy (udpw->msg_buf, msg, msg_len);
1737 enqueue (plugin, udpw);
1738 schedule_select (plugin);
1743 * Function that can be used by the transport service to transmit
1744 * a message using the plugin. Note that in the case of a
1745 * peer disconnecting, the continuation MUST be called
1746 * prior to the disconnect notification itself. This function
1747 * will be called with this peer's HELLO message to initiate
1748 * a fresh connection to another peer.
1750 * @param cls closure
1751 * @param s which session must be used
1752 * @param msgbuf the message to transmit
1753 * @param msgbuf_size number of bytes in 'msgbuf'
1754 * @param priority how important is the message (most plugins will
1755 * ignore message priority and just FIFO)
1756 * @param to how long to wait at most for the transmission (does not
1757 * require plugins to discard the message after the timeout,
1758 * just advisory for the desired delay; most plugins will ignore
1760 * @param cont continuation to call once the message has
1761 * been transmitted (or if the transport is ready
1762 * for the next transmission call; or if the
1763 * peer disconnected...); can be NULL
1764 * @param cont_cls closure for cont
1765 * @return number of bytes used (on the physical network, with overheads);
1766 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1767 * and does NOT mean that the message was not transmitted (DV)
1770 udp_plugin_send (void *cls,
1772 const char *msgbuf, size_t msgbuf_size,
1773 unsigned int priority,
1774 struct GNUNET_TIME_Relative to,
1775 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1777 struct Plugin *plugin = cls;
1778 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1779 struct UDP_FragmentationContext * frag_ctx;
1780 struct UDP_MessageWrapper * udpw;
1781 struct UDPMessage *udp;
1783 GNUNET_assert (plugin != NULL);
1784 GNUNET_assert (s != NULL);
1786 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1787 return GNUNET_SYSERR;
1788 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1789 return GNUNET_SYSERR;
1790 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1793 return GNUNET_SYSERR;
1795 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1798 return GNUNET_SYSERR;
1800 LOG (GNUNET_ERROR_TYPE_DEBUG,
1801 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1803 GNUNET_i2s (&s->target),
1804 GNUNET_a2s(s->sock_addr, s->addrlen));
1808 udp = (struct UDPMessage *) mbuf;
1809 udp->header.size = htons (udpmlen);
1810 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1811 udp->reserved = htonl (0);
1812 udp->sender = *plugin->env->my_identity;
1814 reschedule_session_timeout(s);
1815 if (udpmlen <= UDP_MTU)
1817 /* unfragmented message */
1818 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1820 udpw->msg_buf = (char *) &udpw[1];
1821 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1822 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1823 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1825 udpw->cont_cls = cont_cls;
1826 udpw->frag_ctx = NULL;
1827 udpw->msg_type = MSG_UNFRAGMENTED;
1828 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1829 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1830 enqueue (plugin, udpw);
1832 GNUNET_STATISTICS_update (plugin->env->stats,
1833 "# UDP, unfragmented msgs, messages, attempt",
1835 GNUNET_STATISTICS_update (plugin->env->stats,
1836 "# UDP, unfragmented msgs, bytes payload, attempt",
1837 udpw->payload_size, GNUNET_NO);
1841 /* fragmented message */
1842 if (s->frag_ctx != NULL)
1843 return GNUNET_SYSERR;
1844 memcpy (&udp[1], msgbuf, msgbuf_size);
1845 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1846 frag_ctx->plugin = plugin;
1847 frag_ctx->session = s;
1848 frag_ctx->cont = cont;
1849 frag_ctx->cont_cls = cont_cls;
1850 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1851 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1852 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1853 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1856 s->last_expected_msg_delay,
1857 s->last_expected_ack_delay,
1861 s->frag_ctx = frag_ctx;
1862 GNUNET_STATISTICS_update (plugin->env->stats,
1863 "# UDP, fragmented msgs, messages, pending",
1865 GNUNET_STATISTICS_update (plugin->env->stats,
1866 "# UDP, fragmented msgs, messages, attempt",
1868 GNUNET_STATISTICS_update (plugin->env->stats,
1869 "# UDP, fragmented msgs, bytes payload, attempt",
1870 frag_ctx->payload_size, GNUNET_NO);
1872 schedule_select (plugin);
1878 * Our external IP address/port mapping has changed.
1880 * @param cls closure, the 'struct LocalAddrList'
1881 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1882 * the previous (now invalid) one
1883 * @param addr either the previous or the new public IP address
1884 * @param addrlen actual lenght of the address
1887 udp_nat_port_map_callback (void *cls, int add_remove,
1888 const struct sockaddr *addr, socklen_t addrlen)
1890 struct Plugin *plugin = cls;
1891 struct IPv4UdpAddress u4;
1892 struct IPv6UdpAddress u6;
1896 LOG (GNUNET_ERROR_TYPE_INFO,
1897 "NAT notification to %s address `%s'\n",
1898 (GNUNET_YES == add_remove) ? "add" : "remove",
1899 GNUNET_a2s (addr, addrlen));
1901 /* convert 'addr' to our internal format */
1902 switch (addr->sa_family)
1905 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1906 memset (&u4, 0, sizeof (u4));
1907 u4.options = htonl(myoptions);
1908 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1909 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1911 args = sizeof (struct IPv4UdpAddress);
1914 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1915 memset (&u4, 0, sizeof (u4));
1916 u6.options = htonl(myoptions);
1917 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1918 sizeof (struct in6_addr));
1919 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1921 args = sizeof (struct IPv6UdpAddress);
1927 /* modify our published address list */
1928 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1934 * Message tokenizer has broken up an incomming message. Pass it on
1937 * @param cls the 'struct Plugin'
1938 * @param client the 'struct SourceInformation'
1939 * @param hdr the actual message
1942 process_inbound_tokenized_messages (void *cls, void *client,
1943 const struct GNUNET_MessageHeader *hdr)
1945 struct Plugin *plugin = cls;
1946 struct SourceInformation *si = client;
1947 struct GNUNET_TIME_Relative delay;
1949 GNUNET_assert (si->session != NULL);
1950 if (GNUNET_YES == si->session->in_destroy)
1953 GNUNET_break (ntohl(si->session->ats.value) != GNUNET_ATS_NET_UNSPECIFIED);
1954 delay = plugin->env->receive (plugin->env->cls,
1958 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1959 (GNUNET_YES == si->session->inbound) ? 0 : si->args);
1961 plugin->env->update_address_metrics (plugin->env->cls,
1963 (GNUNET_YES == si->session->inbound) ? NULL : si->arg,
1964 (GNUNET_YES == si->session->inbound) ? 0 : si->args,
1966 &si->session->ats, 1);
1968 si->session->flow_delay_for_other_peer = delay;
1969 reschedule_session_timeout(si->session);
1975 * We've received a UDP Message. Process it (pass contents to main service).
1977 * @param plugin plugin context
1978 * @param msg the message
1979 * @param sender_addr sender address
1980 * @param sender_addr_len number of bytes in sender_addr
1983 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1984 const struct sockaddr *sender_addr,
1985 socklen_t sender_addr_len)
1987 struct SourceInformation si;
1989 struct IPv4UdpAddress u4;
1990 struct IPv6UdpAddress u6;
1994 if (0 != ntohl (msg->reserved))
1996 GNUNET_break_op (0);
1999 if (ntohs (msg->header.size) <
2000 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
2002 GNUNET_break_op (0);
2006 /* convert address */
2007 switch (sender_addr->sa_family)
2010 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
2011 memset (&u4, 0, sizeof (u4));
2012 u6.options = htonl (0);
2013 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
2014 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
2019 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
2020 memset (&u6, 0, sizeof (u6));
2021 u6.options = htonl (0);
2022 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
2023 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
2031 LOG (GNUNET_ERROR_TYPE_DEBUG,
2032 "Received message with %u bytes from peer `%s' at `%s'\n",
2033 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
2034 GNUNET_a2s (sender_addr, sender_addr_len));
2036 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
2037 if (NULL == (s = udp_plugin_lookup_session (plugin, address)))
2039 s = udp_plugin_create_session(plugin, address);
2040 s->inbound = GNUNET_YES;
2041 plugin->env->session_start (NULL, &address->peer, PLUGIN_NAME,
2042 (GNUNET_YES == s->inbound) ? NULL : address->address,
2043 (GNUNET_YES == s->inbound) ? 0 : address->address_length,
2046 GNUNET_free (address);
2048 /* iterate over all embedded messages */
2050 si.sender = msg->sender;
2054 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
2055 ntohs (msg->header.size) -
2056 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
2058 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
2064 * Scan the heap for a receive context with the given address.
2066 * @param cls the 'struct FindReceiveContext'
2067 * @param node internal node of the heap
2068 * @param element value stored at the node (a 'struct ReceiveContext')
2069 * @param cost cost associated with the node
2070 * @return GNUNET_YES if we should continue to iterate,
2074 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
2075 void *element, GNUNET_CONTAINER_HeapCostType cost)
2077 struct FindReceiveContext *frc = cls;
2078 struct DefragContext *e = element;
2080 if ((frc->addr_len == e->addr_len) &&
2081 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
2091 * Process a defragmented message.
2093 * @param cls the 'struct ReceiveContext'
2094 * @param msg the message
2097 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
2099 struct DefragContext *rc = cls;
2101 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
2106 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2111 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
2112 rc->src_addr, rc->addr_len);
2116 struct LookupContext
2118 const struct sockaddr * addr;
2120 struct Session *res;
2127 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
2129 struct LookupContext *l_ctx = cls;
2130 struct Session * s = value;
2132 if ((s->addrlen == l_ctx->addrlen) &&
2133 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
2143 * Transmit an acknowledgement.
2145 * @param cls the 'struct ReceiveContext'
2146 * @param id message ID (unused)
2147 * @param msg ack to transmit
2150 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
2152 struct DefragContext *rc = cls;
2153 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
2154 struct UDP_ACK_Message *udp_ack;
2156 struct UDP_MessageWrapper *udpw;
2158 struct LookupContext l_ctx;
2160 l_ctx.addr = rc->src_addr;
2161 l_ctx.addrlen = rc->addr_len;
2163 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
2164 &lookup_session_by_addr_it,
2171 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
2172 delay = s->flow_delay_for_other_peer.rel_value;
2174 LOG (GNUNET_ERROR_TYPE_DEBUG,
2175 "Sending ACK to `%s' including delay of %u ms\n",
2176 GNUNET_a2s (rc->src_addr,
2177 (rc->src_addr->sa_family ==
2178 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
2181 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
2182 udpw->msg_size = msize;
2183 udpw->payload_size = 0;
2185 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
2186 udpw->msg_buf = (char *)&udpw[1];
2187 udpw->msg_type = MSG_ACK;
2188 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
2189 udp_ack->header.size = htons ((uint16_t) msize);
2190 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
2191 udp_ack->delay = htonl (delay);
2192 udp_ack->sender = *rc->plugin->env->my_identity;
2193 memcpy (&udp_ack[1], msg, ntohs (msg->size));
2194 enqueue (rc->plugin, udpw);
2199 read_process_msg (struct Plugin *plugin,
2200 const struct GNUNET_MessageHeader *msg,
2204 if (ntohs (msg->size) < sizeof (struct UDPMessage))
2206 GNUNET_break_op (0);
2209 process_udp_message (plugin, (const struct UDPMessage *) msg,
2210 (const struct sockaddr *) addr, fromlen);
2215 read_process_ack (struct Plugin *plugin,
2216 const struct GNUNET_MessageHeader *msg,
2220 const struct GNUNET_MessageHeader *ack;
2221 const struct UDP_ACK_Message *udp_ack;
2222 struct LookupContext l_ctx;
2224 struct GNUNET_TIME_Relative flow_delay;
2226 if (ntohs (msg->size) <
2227 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
2229 GNUNET_break_op (0);
2232 udp_ack = (const struct UDP_ACK_Message *) msg;
2233 l_ctx.addr = (const struct sockaddr *) addr;
2234 l_ctx.addrlen = fromlen;
2236 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2237 &lookup_session_by_addr_it,
2241 if ((NULL == s) || (NULL == s->frag_ctx))
2246 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
2247 LOG (GNUNET_ERROR_TYPE_DEBUG,
2248 "We received a sending delay of %llu\n",
2249 flow_delay.rel_value);
2250 s->flow_delay_from_other_peer =
2251 GNUNET_TIME_relative_to_absolute (flow_delay);
2253 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2254 if (ntohs (ack->size) !=
2255 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2257 GNUNET_break_op (0);
2261 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2263 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2265 LOG (GNUNET_ERROR_TYPE_DEBUG,
2266 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2267 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2268 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2269 /* Expect more ACKs to arrive */
2273 LOG (GNUNET_ERROR_TYPE_DEBUG,
2274 "Message full ACK'ed\n",
2275 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2276 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2278 /* Remove fragmented message after successful sending */
2279 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2284 read_process_fragment (struct Plugin *plugin,
2285 const struct GNUNET_MessageHeader *msg,
2289 struct DefragContext *d_ctx;
2290 struct GNUNET_TIME_Absolute now;
2291 struct FindReceiveContext frc;
2294 frc.addr = (const struct sockaddr *) addr;
2295 frc.addr_len = fromlen;
2297 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2298 (unsigned int) ntohs (msg->size),
2299 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2300 /* Lookup existing receive context for this address */
2301 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2302 &find_receive_context,
2304 now = GNUNET_TIME_absolute_get ();
2309 /* Create a new defragmentation context */
2310 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2311 memcpy (&d_ctx[1], addr, fromlen);
2312 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2313 d_ctx->addr_len = fromlen;
2314 d_ctx->plugin = plugin;
2316 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2317 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2318 &fragment_msg_proc, &ack_proc);
2320 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2321 (GNUNET_CONTAINER_HeapCostType)
2323 LOG (GNUNET_ERROR_TYPE_DEBUG,
2324 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2325 (unsigned int) ntohs (msg->size),
2326 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2330 LOG (GNUNET_ERROR_TYPE_DEBUG,
2331 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2332 (unsigned int) ntohs (msg->size),
2333 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2336 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2338 /* keep this 'rc' from expiring */
2339 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2340 (GNUNET_CONTAINER_HeapCostType)
2343 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2344 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2346 /* remove 'rc' that was inactive the longest */
2347 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2348 GNUNET_assert (NULL != d_ctx);
2349 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2350 GNUNET_free (d_ctx);
2356 * Read and process a message from the given socket.
2358 * @param plugin the overall plugin
2359 * @param rsock socket to read from
2362 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2366 char buf[65536] GNUNET_ALIGN;
2368 const struct GNUNET_MessageHeader *msg;
2370 fromlen = sizeof (addr);
2371 memset (&addr, 0, sizeof (addr));
2372 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2373 (struct sockaddr *) &addr, &fromlen);
2375 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2376 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
2377 * on this socket has failed.
2379 * WSAECONNRESET - The virtual circuit was reset by the remote side
2380 * executing a hard or abortive close. The application should close
2381 * the socket; it is no longer usable. On a UDP-datagram socket this
2382 * error indicates a previous send operation resulted in an ICMP Port
2383 * Unreachable message.
2385 if ( (-1 == size) && (ECONNRESET == errno) )
2390 LOG (GNUNET_ERROR_TYPE_DEBUG,
2391 "UDP failed to receive data: %s\n", STRERROR (errno));
2392 /* Connection failure or something. Not a protocol violation. */
2395 if (size < sizeof (struct GNUNET_MessageHeader))
2397 LOG (GNUNET_ERROR_TYPE_WARNING,
2398 "UDP got %u bytes, which is not enough for a GNUnet message header\n",
2399 (unsigned int) size);
2400 /* _MAY_ be a connection failure (got partial message) */
2401 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
2402 GNUNET_break_op (0);
2405 msg = (const struct GNUNET_MessageHeader *) buf;
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2409 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2411 if (size != ntohs (msg->size))
2413 GNUNET_break_op (0);
2417 GNUNET_STATISTICS_update (plugin->env->stats,
2418 "# UDP, total, bytes, received",
2421 switch (ntohs (msg->type))
2423 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2424 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2427 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2428 read_process_msg (plugin, msg, addr, fromlen);
2431 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2432 read_process_ack (plugin, msg, addr, fromlen);
2435 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2436 read_process_fragment (plugin, msg, addr, fromlen);
2440 GNUNET_break_op (0);
2445 static struct UDP_MessageWrapper *
2446 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2447 struct GNUNET_NETWORK_Handle *sock)
2449 struct UDP_MessageWrapper *udpw = NULL;
2450 struct GNUNET_TIME_Relative remaining;
2453 while (udpw != NULL)
2455 /* Find messages with timeout */
2456 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2457 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2459 /* Message timed out */
2460 switch (udpw->msg_type) {
2461 case MSG_UNFRAGMENTED:
2462 GNUNET_STATISTICS_update (plugin->env->stats,
2463 "# UDP, total, bytes, sent, timeout",
2464 udpw->msg_size, GNUNET_NO);
2465 GNUNET_STATISTICS_update (plugin->env->stats,
2466 "# UDP, total, messages, sent, timeout",
2468 GNUNET_STATISTICS_update (plugin->env->stats,
2469 "# UDP, unfragmented msgs, messages, sent, timeout",
2471 GNUNET_STATISTICS_update (plugin->env->stats,
2472 "# UDP, unfragmented msgs, bytes, sent, timeout",
2473 udpw->payload_size, GNUNET_NO);
2474 /* Not fragmented message */
2475 LOG (GNUNET_ERROR_TYPE_DEBUG,
2476 "Message for peer `%s' with size %u timed out\n",
2477 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2478 call_continuation (udpw, GNUNET_SYSERR);
2479 /* Remove message */
2480 dequeue (plugin, udpw);
2483 case MSG_FRAGMENTED:
2484 /* Fragmented message */
2485 GNUNET_STATISTICS_update (plugin->env->stats,
2486 "# UDP, total, bytes, sent, timeout",
2487 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2488 GNUNET_STATISTICS_update (plugin->env->stats,
2489 "# UDP, total, messages, sent, timeout",
2491 call_continuation (udpw, GNUNET_SYSERR);
2492 LOG (GNUNET_ERROR_TYPE_DEBUG,
2493 "Fragment for message for peer `%s' with size %u timed out\n",
2494 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2497 GNUNET_STATISTICS_update (plugin->env->stats,
2498 "# UDP, fragmented msgs, messages, sent, timeout",
2500 GNUNET_STATISTICS_update (plugin->env->stats,
2501 "# UDP, fragmented msgs, bytes, sent, timeout",
2502 udpw->frag_ctx->payload_size, GNUNET_NO);
2503 /* Remove fragmented message due to timeout */
2504 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2507 GNUNET_STATISTICS_update (plugin->env->stats,
2508 "# UDP, total, bytes, sent, timeout",
2509 udpw->msg_size, GNUNET_NO);
2510 GNUNET_STATISTICS_update (plugin->env->stats,
2511 "# UDP, total, messages, sent, timeout",
2513 LOG (GNUNET_ERROR_TYPE_DEBUG,
2514 "ACK Message for peer `%s' with size %u timed out\n",
2515 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2516 call_continuation (udpw, GNUNET_SYSERR);
2517 dequeue (plugin, udpw);
2523 if (sock == plugin->sockv4)
2524 udpw = plugin->ipv4_queue_head;
2525 else if (sock == plugin->sockv6)
2526 udpw = plugin->ipv6_queue_head;
2529 GNUNET_break (0); /* should never happen */
2532 GNUNET_STATISTICS_update (plugin->env->stats,
2533 "# messages dismissed due to timeout",
2538 /* Message did not time out, check flow delay */
2539 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2540 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2542 /* this message is not delayed */
2543 LOG (GNUNET_ERROR_TYPE_DEBUG,
2544 "Message for peer `%s' (%u bytes) is not delayed \n",
2545 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2546 break; /* Found message to send, break */
2550 /* Message is delayed, try next */
2551 LOG (GNUNET_ERROR_TYPE_DEBUG,
2552 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
2553 GNUNET_i2s(&udpw->session->target), udpw->payload_size, remaining.rel_value);
2563 analyze_send_error (struct Plugin *plugin,
2564 const struct sockaddr * sa,
2568 static int network_down_error;
2569 struct GNUNET_ATS_Information type;
2571 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2572 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2573 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2575 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2577 /* IPv4: "Network unreachable" or "Network down"
2579 * This indicates we do not have connectivity
2581 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2582 _("UDP could not transmit message to `%s': "
2583 "Network seems down, please check your network configuration\n"),
2584 GNUNET_a2s (sa, slen));
2586 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2588 /* IPv6: "Network unreachable" or "Network down"
2590 * This indicates that this system is IPv6 enabled, but does not
2591 * have a valid global IPv6 address assigned or we do not have
2595 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2596 _("UDP could not transmit message to `%s': "
2597 "Please check your network configuration and disable IPv6 if your "
2598 "connection does not have a global IPv6 address\n"),
2599 GNUNET_a2s (sa, slen));
2604 LOG (GNUNET_ERROR_TYPE_WARNING,
2605 "UDP could not transmit message to `%s': `%s'\n",
2606 GNUNET_a2s (sa, slen), STRERROR (error));
2611 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2613 const struct sockaddr * sa;
2617 struct UDP_MessageWrapper *udpw = NULL;
2619 /* Find message to send */
2620 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2623 return 0; /* No message to send */
2625 sa = udpw->session->sock_addr;
2626 slen = udpw->session->addrlen;
2628 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2630 if (GNUNET_SYSERR == sent)
2633 analyze_send_error (plugin, sa, slen, errno);
2634 call_continuation(udpw, GNUNET_SYSERR);
2635 GNUNET_STATISTICS_update (plugin->env->stats,
2636 "# UDP, total, bytes, sent, failure",
2638 GNUNET_STATISTICS_update (plugin->env->stats,
2639 "# UDP, total, messages, sent, failure",
2645 LOG (GNUNET_ERROR_TYPE_DEBUG,
2646 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2647 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2648 (sent < 0) ? STRERROR (errno) : "ok");
2649 GNUNET_STATISTICS_update (plugin->env->stats,
2650 "# UDP, total, bytes, sent, success",
2652 GNUNET_STATISTICS_update (plugin->env->stats,
2653 "# UDP, total, messages, sent, success",
2655 if (NULL != udpw->frag_ctx)
2656 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2657 call_continuation (udpw, GNUNET_OK);
2659 dequeue (plugin, udpw);
2668 * We have been notified that our readset has something to read. We don't
2669 * know which socket needs to be read, so we have to check each one
2670 * Then reschedule this function to be called again once more is available.
2672 * @param cls the plugin handle
2673 * @param tc the scheduling context (for rescheduling this function again)
2676 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2678 struct Plugin *plugin = cls;
2680 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2681 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2683 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2684 (NULL != plugin->sockv4) &&
2685 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2686 udp_select_read (plugin, plugin->sockv4);
2687 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2688 (NULL != plugin->sockv4) &&
2689 (NULL != plugin->ipv4_queue_head) &&
2690 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2691 udp_select_send (plugin, plugin->sockv4);
2692 schedule_select (plugin);
2697 * We have been notified that our readset has something to read. We don't
2698 * know which socket needs to be read, so we have to check each one
2699 * Then reschedule this function to be called again once more is available.
2701 * @param cls the plugin handle
2702 * @param tc the scheduling context (for rescheduling this function again)
2705 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2707 struct Plugin *plugin = cls;
2709 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2710 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2712 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2713 (NULL != plugin->sockv6) &&
2714 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2715 udp_select_read (plugin, plugin->sockv6);
2716 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2717 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2718 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2719 udp_select_send (plugin, plugin->sockv6);
2720 schedule_select (plugin);
2726 * @return number of sockets that were successfully bound
2729 setup_sockets (struct Plugin *plugin,
2730 const struct sockaddr_in6 *bind_v6,
2731 const struct sockaddr_in *bind_v4)
2734 int sockets_created = 0;
2735 struct sockaddr_in6 serverAddrv6;
2736 struct sockaddr_in serverAddrv4;
2737 struct sockaddr *serverAddr;
2738 struct sockaddr *addrs[2];
2739 socklen_t addrlens[2];
2743 /* Create IPv6 socket */
2745 if (plugin->enable_ipv6 == GNUNET_YES)
2747 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2748 if (NULL == plugin->sockv6)
2750 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2751 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2752 plugin->enable_ipv6 = GNUNET_NO;
2756 memset (&serverAddrv6, '\0', sizeof (struct sockaddr_in6));
2757 #if HAVE_SOCKADDR_IN_SIN_LEN
2758 serverAddrv6.sin6_len = sizeof (struct sockaddr_in6);
2760 serverAddrv6.sin6_family = AF_INET6;
2761 if (NULL != bind_v6)
2762 serverAddrv6.sin6_addr = bind_v6->sin6_addr;
2764 serverAddrv6.sin6_addr = in6addr_any;
2766 if (0 == plugin->port) /* autodetect */
2767 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2769 serverAddrv6.sin6_port = htons (plugin->port);
2770 addrlen = sizeof (struct sockaddr_in6);
2771 serverAddr = (struct sockaddr *) &serverAddrv6;
2776 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 `%s'\n",
2777 GNUNET_a2s (serverAddr, addrlen));
2779 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv6,
2780 serverAddr, addrlen, 0))
2783 if (0 != plugin->port)
2785 tries = 10; /* fail */
2786 break; /* bind failed on specific port */
2789 serverAddrv6.sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2794 GNUNET_NETWORK_socket_close (plugin->sockv6);
2795 plugin->enable_ipv6 = GNUNET_NO;
2796 plugin->sockv6 = NULL;
2799 if (plugin->sockv6 != NULL)
2801 LOG (GNUNET_ERROR_TYPE_DEBUG,
2802 "IPv6 socket created on port %s\n",
2803 GNUNET_a2s (serverAddr, addrlen));
2804 addrs[sockets_created] = (struct sockaddr *) &serverAddrv6;
2805 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2810 LOG (GNUNET_ERROR_TYPE_ERROR,
2811 "Failed to bind UDP socket to %s: %s\n",
2812 GNUNET_a2s (serverAddr, addrlen),
2818 /* Create IPv4 socket */
2820 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2821 if (NULL == plugin->sockv4)
2823 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2824 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv4 since it is not supported on this system!\n");
2825 plugin->enable_ipv4 = GNUNET_NO;
2829 memset (&serverAddrv4, '\0', sizeof (struct sockaddr_in));
2830 #if HAVE_SOCKADDR_IN_SIN_LEN
2831 serverAddrv4.sin_len = sizeof (struct sockaddr_in);
2833 serverAddrv4.sin_family = AF_INET;
2834 if (NULL != bind_v4)
2835 serverAddrv4.sin_addr = bind_v4->sin_addr;
2837 serverAddrv4.sin_addr.s_addr = INADDR_ANY;
2839 if (0 == plugin->port)
2841 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2843 serverAddrv4.sin_port = htons (plugin->port);
2846 addrlen = sizeof (struct sockaddr_in);
2847 serverAddr = (struct sockaddr *) &serverAddrv4;
2852 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 `%s'\n",
2853 GNUNET_a2s (serverAddr, addrlen));
2856 if (GNUNET_OK == GNUNET_NETWORK_socket_bind (plugin->sockv4,
2857 serverAddr, addrlen, 0))
2860 if (0 != plugin->port)
2862 tries = 10; /* fail */
2863 break; /* bind failed on specific port */
2867 serverAddrv4.sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000);
2873 GNUNET_NETWORK_socket_close (plugin->sockv4);
2874 plugin->enable_ipv4 = GNUNET_NO;
2875 plugin->sockv4 = NULL;
2878 if (plugin->sockv4 != NULL)
2880 LOG (GNUNET_ERROR_TYPE_DEBUG,
2881 "IPv4 socket created on port %s\n", GNUNET_a2s (serverAddr, addrlen));
2882 addrs[sockets_created] = (struct sockaddr *) &serverAddrv4;
2883 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2888 LOG (GNUNET_ERROR_TYPE_ERROR,
2889 "Failed to bind UDP socket to %s: %s\n",
2890 GNUNET_a2s (serverAddr, addrlen), STRERROR (eno));
2894 if (0 == sockets_created)
2896 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2897 return 0; /* No sockets created, return */
2900 /* Create file descriptors */
2901 if (plugin->enable_ipv4 == GNUNET_YES)
2903 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2904 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2905 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2906 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2907 if (NULL != plugin->sockv4)
2909 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2910 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2914 if (plugin->enable_ipv6 == GNUNET_YES)
2916 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2917 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2918 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2919 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2920 if (NULL != plugin->sockv6)
2922 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2923 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2927 schedule_select (plugin);
2928 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2929 GNUNET_NO, plugin->port,
2931 (const struct sockaddr **) addrs, addrlens,
2932 &udp_nat_port_map_callback, NULL, plugin);
2934 return sockets_created;
2939 * The exported method. Makes the core api available via a global and
2940 * returns the udp transport API.
2942 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2943 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2946 libgnunet_plugin_transport_udp_init (void *cls)
2948 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2949 struct GNUNET_TRANSPORT_PluginFunctions *api;
2951 unsigned long long port;
2952 unsigned long long aport;
2953 unsigned long long broadcast;
2954 unsigned long long udp_max_bps;
2955 unsigned long long enable_v6;
2956 char * bind4_address;
2957 char * bind6_address;
2958 char * fancy_interval;
2959 struct GNUNET_TIME_Relative interval;
2960 struct sockaddr_in serverAddrv4;
2961 struct sockaddr_in6 serverAddrv6;
2966 if (NULL == env->receive)
2968 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2969 initialze the plugin or the API */
2970 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2972 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2973 api->address_to_string = &udp_address_to_string;
2974 api->string_to_address = &udp_string_to_address;
2978 GNUNET_assert (NULL != env->stats);
2980 /* Get port number: port == 0 : autodetect a port,
2981 * > 0 : use this port,
2982 * not given : 2086 default */
2984 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2988 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2989 "ADVERTISED_PORT", &aport))
2993 LOG (GNUNET_ERROR_TYPE_WARNING,
2994 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
3001 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
3003 enable_v6 = GNUNET_NO;
3005 enable_v6 = GNUNET_YES;
3008 have_bind4 = GNUNET_NO;
3009 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
3011 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3012 "BINDTO", &bind4_address))
3014 LOG (GNUNET_ERROR_TYPE_DEBUG,
3015 "Binding udp plugin to specific address: `%s'\n",
3017 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
3019 GNUNET_free (bind4_address);
3022 have_bind4 = GNUNET_YES;
3024 GNUNET_free_non_null (bind4_address);
3025 have_bind6 = GNUNET_NO;
3026 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
3028 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3029 "BINDTO6", &bind6_address))
3031 LOG (GNUNET_ERROR_TYPE_DEBUG,
3032 "Binding udp plugin to specific address: `%s'\n",
3035 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
3037 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
3039 GNUNET_free (bind6_address);
3042 have_bind6 = GNUNET_YES;
3044 GNUNET_free_non_null (bind6_address);
3046 /* Initialize my flags */
3049 /* Enable neighbour discovery */
3050 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
3052 if (broadcast == GNUNET_SYSERR)
3053 broadcast = GNUNET_NO;
3055 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
3056 "BROADCAST_INTERVAL", &fancy_interval))
3058 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
3062 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
3064 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
3066 GNUNET_free (fancy_interval);
3069 /* Maximum datarate */
3070 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
3071 "MAX_BPS", &udp_max_bps))
3073 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
3076 p = GNUNET_malloc (sizeof (struct Plugin));
3079 p->broadcast_interval = interval;
3080 p->enable_ipv6 = enable_v6;
3081 p->enable_ipv4 = GNUNET_YES; /* default */
3083 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
3084 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
3085 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
3086 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
3087 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
3090 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
3091 res = setup_sockets (p, (GNUNET_YES == have_bind6) ? &serverAddrv6 : NULL,
3092 (GNUNET_YES == have_bind4) ? &serverAddrv4 : NULL);
3093 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
3095 LOG (GNUNET_ERROR_TYPE_ERROR,
3096 _("Failed to create network sockets, plugin failed\n"));
3097 GNUNET_CONTAINER_multihashmap_destroy (p->sessions);
3098 GNUNET_CONTAINER_heap_destroy (p->defrag_ctxs);
3099 GNUNET_SERVER_mst_destroy (p->mst);
3103 else if (broadcast == GNUNET_YES)
3105 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
3106 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
3109 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3112 api->disconnect = &udp_disconnect;
3113 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
3114 api->address_to_string = &udp_address_to_string;
3115 api->string_to_address = &udp_string_to_address;
3116 api->check_address = &udp_plugin_check_address;
3117 api->get_session = &udp_plugin_get_session;
3118 api->send = &udp_plugin_send;
3119 api->get_network = &udp_get_network;
3126 heap_cleanup_iterator (void *cls,
3127 struct GNUNET_CONTAINER_HeapNode *
3128 node, void *element,
3129 GNUNET_CONTAINER_HeapCostType
3132 struct DefragContext * d_ctx = element;
3134 GNUNET_CONTAINER_heap_remove_node (node);
3135 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
3136 GNUNET_free (d_ctx);
3143 * The exported method. Makes the core api available via a global and
3144 * returns the udp transport API.
3146 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
3150 libgnunet_plugin_transport_udp_done (void *cls)
3152 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3153 struct Plugin *plugin = api->cls;
3154 struct PrettyPrinterContext *cur;
3155 struct PrettyPrinterContext *next;
3163 stop_broadcast (plugin);
3164 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
3166 GNUNET_SCHEDULER_cancel (plugin->select_task);
3167 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
3169 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
3171 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
3172 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
3175 /* Closing sockets */
3176 if (GNUNET_YES ==plugin->enable_ipv4)
3178 if (plugin->sockv4 != NULL)
3180 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
3181 plugin->sockv4 = NULL;
3183 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
3184 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
3186 if (GNUNET_YES ==plugin->enable_ipv6)
3188 if (plugin->sockv6 != NULL)
3190 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
3191 plugin->sockv6 = NULL;
3193 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
3194 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
3197 if (NULL != plugin->nat)
3198 GNUNET_NAT_unregister (plugin->nat);
3200 if (plugin->defrag_ctxs != NULL)
3202 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
3203 heap_cleanup_iterator, NULL);
3204 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
3205 plugin->defrag_ctxs = NULL;
3207 if (plugin->mst != NULL)
3209 GNUNET_SERVER_mst_destroy(plugin->mst);
3213 /* Clean up leftover messages */
3214 struct UDP_MessageWrapper * udpw;
3215 udpw = plugin->ipv4_queue_head;
3216 while (udpw != NULL)
3218 struct UDP_MessageWrapper *tmp = udpw->next;
3219 dequeue (plugin, udpw);
3220 call_continuation(udpw, GNUNET_SYSERR);
3225 udpw = plugin->ipv6_queue_head;
3226 while (udpw != NULL)
3228 struct UDP_MessageWrapper *tmp = udpw->next;
3229 dequeue (plugin, udpw);
3230 call_continuation(udpw, GNUNET_SYSERR);
3236 /* Clean up sessions */
3237 LOG (GNUNET_ERROR_TYPE_DEBUG,
3238 "Cleaning up sessions\n");
3239 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
3240 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
3242 next = ppc_dll_head;
3243 for (cur = next; NULL != cur; cur = next)
3246 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
3247 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3248 GNUNET_SCHEDULER_cancel (cur->timeout_task);
3254 GNUNET_free (plugin);
3257 struct Allocation *allocation;
3258 while (NULL != ahead)
3261 GNUNET_CONTAINER_DLL_remove (ahead, atail, allocation);
3262 GNUNET_free (allocation);
3264 struct Allocator *allocator;
3265 while (NULL != aehead)
3268 GNUNET_CONTAINER_DLL_remove (aehead, aetail, allocator);
3269 GNUNET_free (allocator);
3276 /* end of plugin_transport_udp.c */