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__)
47 * Number of messages we can defragment in parallel. We only really
48 * defragment 1 message at a time, but if messages get re-ordered, we
49 * may want to keep knowledge about the previous message to avoid
50 * discarding the current message in favor of a single fragment of a
51 * previous message. 3 should be good since we don't expect massive
52 * message reorderings with UDP.
54 #define UDP_MAX_MESSAGES_IN_DEFRAG 3
57 * We keep a defragmentation queue per sender address. How many
58 * sender addresses do we support at the same time? Memory consumption
59 * is roughly a factor of 32k * UDP_MAX_MESSAGES_IN_DEFRAG times this
60 * value. (So 128 corresponds to 12 MB and should suffice for
61 * connecting to roughly 128 peers via UDP).
63 #define UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG 128
68 * Closure for 'append_port'.
70 struct PrettyPrinterContext
73 * Function to call with the result.
75 GNUNET_TRANSPORT_AddressStringCallback asc;
83 * Port to add after the IP address.
93 MSG_FRAGMENTED_COMPLETE = 2,
102 * Which peer is this session for?
104 struct GNUNET_PeerIdentity target;
106 struct UDP_FragmentationContext * frag_ctx;
109 * Address of the other peer
111 const struct sockaddr *sock_addr;
114 * Desired delay for next sending we send to other peer
116 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
119 * Desired delay for next sending we received from other peer
121 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
124 * Session timeout task
126 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
129 * expected delay for ACKs
131 struct GNUNET_TIME_Relative last_expected_ack_delay;
134 * desired delay between UDP messages
136 struct GNUNET_TIME_Relative last_expected_msg_delay;
138 struct GNUNET_ATS_Information ats;
149 struct SessionCompareContext
152 const struct GNUNET_HELLO_Address *addr;
157 * Closure for 'process_inbound_tokenized_messages'
159 struct SourceInformation
164 struct GNUNET_PeerIdentity sender;
171 struct Session *session;
173 * Number of bytes in source address.
181 * Closure for 'find_receive_context'.
183 struct FindReceiveContext
186 * Where to store the result.
188 struct DefragContext *rc;
193 const struct sockaddr *addr;
195 struct Session *session;
198 * Number of bytes in 'addr'.
207 * Data structure to track defragmentation contexts based
208 * on the source of the UDP traffic.
214 * Defragmentation context.
216 struct GNUNET_DEFRAGMENT_Context *defrag;
219 * Source address this receive context is for (allocated at the
220 * end of the struct).
222 const struct sockaddr *src_addr;
225 * Reference to master plugin struct.
227 struct Plugin *plugin;
230 * Node in the defrag heap.
232 struct GNUNET_CONTAINER_HeapNode *hnode;
235 * Length of 'src_addr'
243 * Context to send fragmented messages
245 struct UDP_FragmentationContext
248 * Next in linked list
250 struct UDP_FragmentationContext * next;
253 * Previous in linked list
255 struct UDP_FragmentationContext * prev;
260 struct Plugin * plugin;
263 * Handle for GNUNET_FRAGMENT context
265 struct GNUNET_FRAGMENT_Context * frag;
268 * The session this fragmentation context belongs to
270 struct Session * session;
273 * Function to call upon completion of the transmission.
275 GNUNET_TRANSPORT_TransmitContinuation cont;
278 * Closure for 'cont'.
285 struct GNUNET_TIME_Absolute timeout;
288 * Payload size of original unfragmented message
293 * Bytes used to send all fragments on wire including UDP overhead
297 unsigned int fragments_used;
302 struct UDP_MessageWrapper
305 * Session this message belongs to
307 struct Session *session;
313 struct UDP_MessageWrapper *prev;
319 struct UDP_MessageWrapper *next;
323 * According to UDP_MessageType
328 * Message with size msg_size including UDP specific overhead
333 * Size of UDP message to send including UDP specific overhead
338 * Payload size of original message
345 struct GNUNET_TIME_Absolute timeout;
348 * Function to call upon completion of the transmission.
350 GNUNET_TRANSPORT_TransmitContinuation cont;
353 * Closure for 'cont'.
358 * Fragmentation context
359 * frag_ctx == NULL if transport <= MTU
360 * frag_ctx != NULL if transport > MTU
362 struct UDP_FragmentationContext *frag_ctx;
367 * UDP ACK Message-Packet header (after defragmentation).
369 struct UDP_ACK_Message
374 struct GNUNET_MessageHeader header;
377 * Desired delay for flow control
382 * What is the identity of the sender
384 struct GNUNET_PeerIdentity sender;
389 * Encapsulation of all of the state of the plugin.
391 struct Plugin * plugin;
395 * We have been notified that our readset has something to read. We don't
396 * know which socket needs to be read, so we have to check each one
397 * Then reschedule this function to be called again once more is available.
399 * @param cls the plugin handle
400 * @param tc the scheduling context (for rescheduling this function again)
403 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
407 * We have been notified that our readset has something to read. We don't
408 * know which socket needs to be read, so we have to check each one
409 * Then reschedule this function to be called again once more is available.
411 * @param cls the plugin handle
412 * @param tc the scheduling context (for rescheduling this function again)
415 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
419 * Start session timeout
422 start_session_timeout (struct Session *s);
425 * Increment session timeout due to activity
428 reschedule_session_timeout (struct Session *s);
434 stop_session_timeout (struct Session *s);
438 * (re)schedule select tasks for this plugin.
440 * @param plugin plugin to reschedule
443 schedule_select (struct Plugin *plugin)
445 struct GNUNET_TIME_Relative min_delay;
446 struct UDP_MessageWrapper *udpw;
448 if (NULL != plugin->sockv4)
450 /* Find a message ready to send:
451 * Flow delay from other peer is expired or not set (0) */
452 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
453 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
454 min_delay = GNUNET_TIME_relative_min (min_delay,
455 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
457 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
458 GNUNET_SCHEDULER_cancel(plugin->select_task);
461 * - write active set if message is ready
462 * - timeout minimum delay */
463 plugin->select_task =
464 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
465 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
467 (0 == min_delay.rel_value) ? plugin->ws_v4 : NULL,
468 &udp_plugin_select, plugin);
470 if (NULL != plugin->sockv6)
472 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
473 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
474 min_delay = GNUNET_TIME_relative_min (min_delay,
475 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
477 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
478 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
479 plugin->select_task_v6 =
480 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
481 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
483 (0 == min_delay.rel_value) ? plugin->ws_v6 : NULL,
484 &udp_plugin_select_v6, plugin);
490 * Function called for a quick conversion of the binary address to
491 * a numeric address. Note that the caller must not free the
492 * address and that the next call to this function is allowed
493 * to override the address again.
496 * @param addr binary address
497 * @param addrlen length of the address
498 * @return string representing the same address
501 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
503 static char rbuf[INET6_ADDRSTRLEN + 10];
504 char buf[INET6_ADDRSTRLEN];
508 const struct IPv4UdpAddress *t4;
509 const struct IPv6UdpAddress *t6;
513 if (addrlen == sizeof (struct IPv6UdpAddress))
517 port = ntohs (t6->u6_port);
518 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
521 else if (addrlen == sizeof (struct IPv4UdpAddress))
525 port = ntohs (t4->u4_port);
526 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
534 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
535 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
542 * Function called to convert a string address to
545 * @param cls closure ('struct Plugin*')
546 * @param addr string address
547 * @param addrlen length of the address
548 * @param buf location to store the buffer
549 * @param added location to store the number of bytes in the buffer.
550 * If the function returns GNUNET_SYSERR, its contents are undefined.
551 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
554 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
555 void **buf, size_t *added)
557 struct sockaddr_storage socket_address;
559 if ((NULL == addr) || (0 == addrlen))
562 return GNUNET_SYSERR;
565 if ('\0' != addr[addrlen - 1])
567 return GNUNET_SYSERR;
570 if (strlen (addr) != addrlen - 1)
572 return GNUNET_SYSERR;
575 if (GNUNET_OK != GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
578 return GNUNET_SYSERR;
581 switch (socket_address.ss_family)
585 struct IPv4UdpAddress *u4;
586 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
587 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
588 u4->ipv4_addr = in4->sin_addr.s_addr;
589 u4->u4_port = in4->sin_port;
591 *added = sizeof (struct IPv4UdpAddress);
596 struct IPv6UdpAddress *u6;
597 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
598 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
599 u6->ipv6_addr = in6->sin6_addr;
600 u6->u6_port = in6->sin6_port;
602 *added = sizeof (struct IPv6UdpAddress);
607 return GNUNET_SYSERR;
613 * Append our port and forward the result.
615 * @param cls a 'struct PrettyPrinterContext'
616 * @param hostname result from DNS resolver
619 append_port (void *cls, const char *hostname)
621 struct PrettyPrinterContext *ppc = cls;
624 if (hostname == NULL)
626 ppc->asc (ppc->asc_cls, NULL);
630 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
631 ppc->asc (ppc->asc_cls, ret);
637 * Convert the transports address to a nice, human-readable
641 * @param type name of the transport that generated the address
642 * @param addr one of the addresses of the host, NULL for the last address
643 * the specific address format depends on the transport
644 * @param addrlen length of the address
645 * @param numeric should (IP) addresses be displayed in numeric form?
646 * @param timeout after how long should we give up?
647 * @param asc function to call on each string
648 * @param asc_cls closure for asc
651 udp_plugin_address_pretty_printer (void *cls, const char *type,
652 const void *addr, size_t addrlen,
654 struct GNUNET_TIME_Relative timeout,
655 GNUNET_TRANSPORT_AddressStringCallback asc,
658 struct PrettyPrinterContext *ppc;
661 struct sockaddr_in a4;
662 struct sockaddr_in6 a6;
663 const struct IPv4UdpAddress *u4;
664 const struct IPv6UdpAddress *u6;
667 if (addrlen == sizeof (struct IPv6UdpAddress))
670 memset (&a6, 0, sizeof (a6));
671 a6.sin6_family = AF_INET6;
672 #if HAVE_SOCKADDR_IN_SIN_LEN
673 a6.sin6_len = sizeof (a6);
675 a6.sin6_port = u6->u6_port;
676 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
677 port = ntohs (u6->u6_port);
681 else if (addrlen == sizeof (struct IPv4UdpAddress))
684 memset (&a4, 0, sizeof (a4));
685 a4.sin_family = AF_INET;
686 #if HAVE_SOCKADDR_IN_SIN_LEN
687 a4.sin_len = sizeof (a4);
689 a4.sin_port = u4->u4_port;
690 a4.sin_addr.s_addr = u4->ipv4_addr;
691 port = ntohs (u4->u4_port);
695 else if (0 == addrlen)
697 asc (asc_cls, "<inbound connection>");
703 /* invalid address */
708 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
710 ppc->asc_cls = asc_cls;
712 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
717 call_continuation (struct UDP_MessageWrapper *udpw, int result)
721 LOG (GNUNET_ERROR_TYPE_DEBUG,
722 "Calling continuation for %u byte message to `%s' with result %s\n",
723 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
724 (GNUNET_OK == result) ? "OK" : "SYSERR");
726 if (udpw->msg_size >= udpw->payload_size)
727 overhead = udpw->msg_size - udpw->payload_size;
729 overhead = udpw->msg_size;
733 switch (udpw->msg_type) {
734 case MSG_UNFRAGMENTED:
735 if (NULL != udpw->cont)
737 /* Transport continuation */
738 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
739 udpw->payload_size, udpw->msg_size);
741 GNUNET_STATISTICS_update (plugin->env->stats,
742 "# UDP, unfragmented msgs, messages, sent, success",
744 GNUNET_STATISTICS_update (plugin->env->stats,
745 "# UDP, unfragmented msgs, bytes payload, sent, success",
746 udpw->payload_size, GNUNET_NO);
747 GNUNET_STATISTICS_update (plugin->env->stats,
748 "# UDP, unfragmented msgs, bytes overhead, sent, success",
749 overhead, GNUNET_NO);
750 GNUNET_STATISTICS_update (plugin->env->stats,
751 "# UDP, total, bytes overhead, sent",
752 overhead, GNUNET_NO);
753 GNUNET_STATISTICS_update (plugin->env->stats,
754 "# UDP, total, bytes payload, sent",
755 udpw->payload_size, GNUNET_NO);
757 case MSG_FRAGMENTED_COMPLETE:
758 GNUNET_assert (NULL != udpw->frag_ctx);
759 if (udpw->frag_ctx->cont != NULL)
760 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_OK,
761 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
762 GNUNET_STATISTICS_update (plugin->env->stats,
763 "# UDP, fragmented msgs, messages, sent, success",
765 GNUNET_STATISTICS_update (plugin->env->stats,
766 "# UDP, fragmented msgs, bytes payload, sent, success",
767 udpw->payload_size, GNUNET_NO);
768 GNUNET_STATISTICS_update (plugin->env->stats,
769 "# UDP, fragmented msgs, bytes overhead, sent, success",
770 overhead, GNUNET_NO);
771 GNUNET_STATISTICS_update (plugin->env->stats,
772 "# UDP, total, bytes overhead, sent",
773 overhead, GNUNET_NO);
774 GNUNET_STATISTICS_update (plugin->env->stats,
775 "# UDP, total, bytes payload, sent",
776 udpw->payload_size, GNUNET_NO);
777 GNUNET_STATISTICS_update (plugin->env->stats,
778 "# UDP, fragmented msgs, messages, pending",
782 /* Fragmented message: enqueue next fragment */
783 if (NULL != udpw->cont)
784 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
785 udpw->payload_size, udpw->msg_size);
786 GNUNET_STATISTICS_update (plugin->env->stats,
787 "# UDP, fragmented msgs, fragments, sent, success",
789 GNUNET_STATISTICS_update (plugin->env->stats,
790 "# UDP, fragmented msgs, fragments bytes, sent, success",
791 udpw->msg_size, GNUNET_NO);
794 /* No continuation */
795 GNUNET_STATISTICS_update (plugin->env->stats,
796 "# UDP, ACK msgs, messages, sent, success",
798 GNUNET_STATISTICS_update (plugin->env->stats,
799 "# UDP, ACK msgs, bytes overhead, sent, success",
800 overhead, GNUNET_NO);
801 GNUNET_STATISTICS_update (plugin->env->stats,
802 "# UDP, total, bytes overhead, sent",
803 overhead, GNUNET_NO);
809 LOG (GNUNET_ERROR_TYPE_ERROR,
810 "ERROR: %u\n", udpw->msg_type);
816 switch (udpw->msg_type) {
817 case MSG_UNFRAGMENTED:
818 /* Unfragmented message: failed to send */
819 if (NULL != udpw->cont)
820 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
821 udpw->payload_size, overhead);
822 GNUNET_STATISTICS_update (plugin->env->stats,
823 "# UDP, unfragmented msgs, messages, sent, failure",
825 GNUNET_STATISTICS_update (plugin->env->stats,
826 "# UDP, unfragmented msgs, bytes payload, sent, failure",
827 udpw->payload_size, GNUNET_NO);
828 GNUNET_STATISTICS_update (plugin->env->stats,
829 "# UDP, unfragmented msgs, bytes overhead, sent, failure",
830 overhead, GNUNET_NO);
832 case MSG_FRAGMENTED_COMPLETE:
833 GNUNET_assert (NULL != udpw->frag_ctx);
834 if (udpw->frag_ctx->cont != NULL)
835 udpw->frag_ctx->cont (udpw->frag_ctx->cont_cls, &udpw->session->target, GNUNET_SYSERR,
836 udpw->frag_ctx->payload_size, udpw->frag_ctx->on_wire_size);
837 GNUNET_STATISTICS_update (plugin->env->stats,
838 "# UDP, fragmented msgs, messages, sent, failure",
840 GNUNET_STATISTICS_update (plugin->env->stats,
841 "# UDP, fragmented msgs, bytes payload, sent, failure",
842 udpw->payload_size, GNUNET_NO);
843 GNUNET_STATISTICS_update (plugin->env->stats,
844 "# UDP, fragmented msgs, bytes payload, sent, failure",
845 overhead, GNUNET_NO);
846 GNUNET_STATISTICS_update (plugin->env->stats,
847 "# UDP, fragmented msgs, bytes payload, sent, failure",
848 overhead, GNUNET_NO);
849 GNUNET_STATISTICS_update (plugin->env->stats,
850 "# UDP, fragmented msgs, messages, pending",
854 GNUNET_assert (NULL != udpw->frag_ctx);
855 /* Fragmented message: failed to send */
856 GNUNET_STATISTICS_update (plugin->env->stats,
857 "# UDP, fragmented msgs, fragments, sent, failure",
859 GNUNET_STATISTICS_update (plugin->env->stats,
860 "# UDP, fragmented msgs, fragments bytes, sent, failure",
861 udpw->msg_size, GNUNET_NO);
864 /* ACK message: failed to send */
865 GNUNET_STATISTICS_update (plugin->env->stats,
866 "# UDP, ACK msgs, messages, sent, failure",
870 /* Beacon message: failed to send */
886 * Check if the given port is plausible (must be either our listen
887 * port or our advertised port). If it is neither, we return
890 * @param plugin global variables
891 * @param in_port port number to check
892 * @return GNUNET_OK if port is either open_port or adv_port
895 check_port (struct Plugin *plugin, uint16_t in_port)
897 if ((in_port == plugin->port) || (in_port == plugin->aport))
899 return GNUNET_SYSERR;
904 * Function that will be called to check if a binary address for this
905 * plugin is well-formed and corresponds to an address for THIS peer
906 * (as per our configuration). Naturally, if absolutely necessary,
907 * plugins can be a bit conservative in their answer, but in general
908 * plugins should make sure that the address does not redirect
909 * traffic to a 3rd party that might try to man-in-the-middle our
912 * @param cls closure, should be our handle to the Plugin
913 * @param addr pointer to the address
914 * @param addrlen length of addr
915 * @return GNUNET_OK if this is a plausible address for this peer
916 * and transport, GNUNET_SYSERR if not
920 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
922 struct Plugin *plugin = cls;
923 struct IPv4UdpAddress *v4;
924 struct IPv6UdpAddress *v6;
926 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
927 (addrlen != sizeof (struct IPv6UdpAddress)))
930 return GNUNET_SYSERR;
932 if (addrlen == sizeof (struct IPv4UdpAddress))
934 v4 = (struct IPv4UdpAddress *) addr;
935 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
936 return GNUNET_SYSERR;
938 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
939 sizeof (struct in_addr)))
940 return GNUNET_SYSERR;
944 v6 = (struct IPv6UdpAddress *) addr;
945 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
948 return GNUNET_SYSERR;
950 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
951 return GNUNET_SYSERR;
953 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
954 sizeof (struct in6_addr)))
955 return GNUNET_SYSERR;
962 * Task to free resources associated with a session.
964 * @param s session to free
967 free_session (struct Session *s)
969 if (NULL != s->frag_ctx)
971 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag, NULL, NULL);
972 GNUNET_free (s->frag_ctx);
980 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
982 GNUNET_STATISTICS_update (plugin->env->stats,
983 "# UDP, total, bytes in buffers",
984 -udpw->msg_size, GNUNET_NO);
985 GNUNET_STATISTICS_update (plugin->env->stats,
986 "# UDP, total, msgs in buffers",
988 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
989 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
990 plugin->ipv4_queue_tail, udpw);
991 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
992 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
993 plugin->ipv6_queue_tail, udpw);
997 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
999 struct UDP_MessageWrapper *udpw;
1000 struct UDP_MessageWrapper *tmp;
1001 struct UDP_MessageWrapper dummy;
1002 struct Session *s = fc->session;
1004 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1006 /* Call continuation for fragmented message */
1007 memset (&dummy, 0, sizeof (dummy));
1008 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1009 dummy.msg_size = s->frag_ctx->on_wire_size;
1010 dummy.payload_size = s->frag_ctx->payload_size;
1011 dummy.frag_ctx = s->frag_ctx;
1013 dummy.cont_cls = NULL;
1016 call_continuation (&dummy, result);
1018 /* Remove left-over fragments from queue */
1019 /* Remove leftover fragments from queue */
1020 if (s->addrlen == sizeof (struct sockaddr_in6))
1022 udpw = plugin->ipv6_queue_head;
1023 while (NULL != udpw)
1026 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1028 dequeue (plugin, udpw);
1029 call_continuation (udpw, GNUNET_SYSERR);
1035 if (s->addrlen == sizeof (struct sockaddr_in))
1037 udpw = plugin->ipv4_queue_head;
1041 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1043 dequeue (plugin, udpw);
1044 call_continuation (udpw, GNUNET_SYSERR);
1051 /* Destroy fragmentation context */
1052 GNUNET_FRAGMENT_context_destroy (fc->frag,
1053 &s->last_expected_msg_delay,
1054 &s->last_expected_ack_delay);
1060 * Functions with this signature are called whenever we need
1061 * to close a session due to a disconnect or failure to
1062 * establish a connection.
1064 * @param s session to close down
1067 disconnect_session (struct Session *s)
1069 struct UDP_MessageWrapper *udpw;
1070 struct UDP_MessageWrapper *next;
1072 GNUNET_assert (GNUNET_YES != s->in_destroy);
1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1074 "Session %p to peer `%s' address ended \n",
1076 GNUNET_i2s (&s->target),
1077 GNUNET_a2s (s->sock_addr, s->addrlen));
1078 stop_session_timeout (s);
1080 if (NULL != s->frag_ctx)
1082 /* Remove fragmented message due to disconnect */
1083 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1086 next = plugin->ipv4_queue_head;
1087 while (NULL != (udpw = next))
1090 if (udpw->session == s)
1092 dequeue (plugin, udpw);
1093 call_continuation(udpw, GNUNET_SYSERR);
1097 next = plugin->ipv6_queue_head;
1098 while (NULL != (udpw = next))
1101 if (udpw->session == s)
1103 dequeue (plugin, udpw);
1104 call_continuation(udpw, GNUNET_SYSERR);
1109 plugin->env->session_end (plugin->env->cls, &s->target, s);
1111 if (NULL != s->frag_ctx)
1113 if (NULL != s->frag_ctx->cont)
1115 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1116 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1117 LOG (GNUNET_ERROR_TYPE_DEBUG,
1118 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1119 GNUNET_i2s (&s->target));
1123 GNUNET_assert (GNUNET_YES ==
1124 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1125 &s->target.hashPubKey,
1127 GNUNET_STATISTICS_set(plugin->env->stats,
1128 "# UDP, sessions active",
1129 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1132 s->in_destroy = GNUNET_YES;
1138 * Destroy a session, plugin is being unloaded.
1141 * @param key hash of public key of target peer
1142 * @param value a 'struct PeerSession*' to clean up
1143 * @return GNUNET_OK (continue to iterate)
1146 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1148 disconnect_session(value);
1154 * Disconnect from a remote node. Clean up session if we have one for this peer
1156 * @param cls closure for this call (should be handle to Plugin)
1157 * @param target the peeridentity of the peer to disconnect
1158 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1161 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1163 struct Plugin *plugin = cls;
1164 GNUNET_assert (plugin != NULL);
1166 GNUNET_assert (target != NULL);
1167 LOG (GNUNET_ERROR_TYPE_DEBUG,
1168 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1169 /* Clean up sessions */
1170 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1175 * Session was idle, so disconnect it
1178 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1180 GNUNET_assert (NULL != cls);
1181 struct Session *s = cls;
1183 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1185 "Session %p was idle for %llu ms, disconnecting\n",
1186 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1187 /* call session destroy function */
1188 disconnect_session (s);
1193 * Start session timeout
1196 start_session_timeout (struct Session *s)
1198 GNUNET_assert (NULL != s);
1199 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1200 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1204 "Timeout for session %p set to %llu ms\n",
1205 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1210 * Increment session timeout due to activity
1213 reschedule_session_timeout (struct Session *s)
1215 GNUNET_assert (NULL != s);
1216 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1218 GNUNET_SCHEDULER_cancel (s->timeout_task);
1219 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Timeout rescheduled for session %p set to %llu ms\n",
1224 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1232 stop_session_timeout (struct Session *s)
1234 GNUNET_assert (NULL != s);
1236 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1238 GNUNET_SCHEDULER_cancel (s->timeout_task);
1239 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1241 "Timeout stopped for session %p canceled\n",
1242 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1247 static struct Session *
1248 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1249 const void *addr, size_t addrlen,
1250 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1253 const struct IPv4UdpAddress *t4;
1254 const struct IPv6UdpAddress *t6;
1255 struct sockaddr_in *v4;
1256 struct sockaddr_in6 *v6;
1261 case sizeof (struct IPv4UdpAddress):
1262 if (NULL == plugin->sockv4)
1267 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1268 len = sizeof (struct sockaddr_in);
1269 v4 = (struct sockaddr_in *) &s[1];
1270 v4->sin_family = AF_INET;
1271 #if HAVE_SOCKADDR_IN_SIN_LEN
1272 v4->sin_len = sizeof (struct sockaddr_in);
1274 v4->sin_port = t4->u4_port;
1275 v4->sin_addr.s_addr = t4->ipv4_addr;
1276 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1278 case sizeof (struct IPv6UdpAddress):
1279 if (NULL == plugin->sockv6)
1285 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1286 len = sizeof (struct sockaddr_in6);
1287 v6 = (struct sockaddr_in6 *) &s[1];
1288 v6->sin6_family = AF_INET6;
1289 #if HAVE_SOCKADDR_IN_SIN_LEN
1290 v6->sin6_len = sizeof (struct sockaddr_in6);
1292 v6->sin6_port = t6->u6_port;
1293 v6->sin6_addr = t6->ipv6_addr;
1294 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1297 /* Must have a valid address to send to */
1298 GNUNET_break_op (0);
1302 s->target = *target;
1303 s->sock_addr = (const struct sockaddr *) &s[1];
1304 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1305 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1306 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1307 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1308 start_session_timeout (s);
1314 session_cmp_it (void *cls,
1315 const struct GNUNET_HashCode * key,
1318 struct SessionCompareContext * cctx = cls;
1319 const struct GNUNET_HELLO_Address *address = cctx->addr;
1320 struct Session *s = value;
1322 socklen_t s_addrlen = s->addrlen;
1324 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1325 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1326 GNUNET_a2s (s->sock_addr, s->addrlen));
1327 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1328 (s_addrlen == sizeof (struct sockaddr_in)))
1330 struct IPv4UdpAddress * u4 = NULL;
1331 u4 = (struct IPv4UdpAddress *) address->address;
1332 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1333 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1334 (u4->u4_port == s4->sin_port))
1341 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1342 (s_addrlen == sizeof (struct sockaddr_in6)))
1344 struct IPv6UdpAddress * u6 = NULL;
1345 u6 = (struct IPv6UdpAddress *) address->address;
1346 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1347 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1348 (u6->u6_port == s6->sin6_port))
1359 * Creates a new outbound session the transport service will use to send data to the
1362 * @param cls the plugin
1363 * @param address the address
1364 * @return the session or NULL of max connections exceeded
1366 static struct Session *
1367 udp_plugin_get_session (void *cls,
1368 const struct GNUNET_HELLO_Address *address)
1370 struct Session * s = NULL;
1371 struct Plugin * plugin = cls;
1372 struct IPv6UdpAddress * udp_a6;
1373 struct IPv4UdpAddress * udp_a4;
1375 GNUNET_assert (plugin != NULL);
1376 GNUNET_assert (address != NULL);
1379 if ((address->address == NULL) ||
1380 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1381 (address->address_length != sizeof (struct IPv6UdpAddress))))
1387 if (address->address_length == sizeof (struct IPv4UdpAddress))
1389 if (plugin->sockv4 == NULL)
1391 udp_a4 = (struct IPv4UdpAddress *) address->address;
1392 if (udp_a4->u4_port == 0)
1396 if (address->address_length == sizeof (struct IPv6UdpAddress))
1398 if (plugin->sockv6 == NULL)
1400 udp_a6 = (struct IPv6UdpAddress *) address->address;
1401 if (udp_a6->u6_port == 0)
1405 /* check if session already exists */
1406 struct SessionCompareContext cctx;
1407 cctx.addr = address;
1409 LOG (GNUNET_ERROR_TYPE_DEBUG,
1410 "Looking for existing session for peer `%s' `%s' \n",
1411 GNUNET_i2s (&address->peer),
1412 udp_address_to_string(NULL, address->address, address->address_length));
1413 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1414 if (cctx.res != NULL)
1416 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1420 /* otherwise create new */
1421 s = create_session (plugin,
1424 address->address_length,
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "Creating new session %p for peer `%s' address `%s'\n",
1429 GNUNET_i2s(&address->peer),
1430 udp_address_to_string(NULL,address->address,address->address_length));
1431 GNUNET_assert (GNUNET_OK ==
1432 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1433 &s->target.hashPubKey,
1435 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1436 GNUNET_STATISTICS_set(plugin->env->stats,
1437 "# UDP, sessions active",
1438 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1445 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1447 GNUNET_STATISTICS_update (plugin->env->stats,
1448 "# UDP, total, bytes in buffers",
1449 udpw->msg_size, GNUNET_NO);
1450 GNUNET_STATISTICS_update (plugin->env->stats,
1451 "# UDP, total, msgs in buffers",
1453 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1454 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1455 plugin->ipv4_queue_tail, udpw);
1456 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1457 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1458 plugin->ipv6_queue_tail, udpw);
1464 * Fragment message was transmitted via UDP, let fragmentation know
1465 * to send the next fragment now.
1467 * @param cls the 'struct UDPMessageWrapper' of the fragment
1468 * @param target destination peer (ignored)
1469 * @param result GNUNET_OK on success (ignored)
1470 * @param payload bytes payload sent
1471 * @param physical bytes physical sent
1474 send_next_fragment (void *cls,
1475 const struct GNUNET_PeerIdentity *target,
1476 int result, size_t payload, size_t physical)
1478 struct UDP_MessageWrapper *udpw = cls;
1479 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1484 * Function that is called with messages created by the fragmentation
1485 * module. In the case of the 'proc' callback of the
1486 * GNUNET_FRAGMENT_context_create function, this function must
1487 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1489 * @param cls closure, the 'struct FragmentationContext'
1490 * @param msg the message that was created
1493 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1495 struct UDP_FragmentationContext *frag_ctx = cls;
1496 struct Plugin *plugin = frag_ctx->plugin;
1497 struct UDP_MessageWrapper * udpw;
1498 size_t msg_len = ntohs (msg->size);
1500 LOG (GNUNET_ERROR_TYPE_DEBUG,
1501 "Enqueuing fragment with %u bytes\n", msg_len);
1502 frag_ctx->fragments_used ++;
1503 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1504 udpw->session = frag_ctx->session;
1505 udpw->msg_buf = (char *) &udpw[1];
1506 udpw->msg_size = msg_len;
1507 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1508 udpw->cont = &send_next_fragment;
1509 udpw->cont_cls = udpw;
1510 udpw->timeout = frag_ctx->timeout;
1511 udpw->frag_ctx = frag_ctx;
1512 udpw->msg_type = MSG_FRAGMENTED;
1513 memcpy (udpw->msg_buf, msg, msg_len);
1514 enqueue (plugin, udpw);
1515 schedule_select (plugin);
1520 * Function that can be used by the transport service to transmit
1521 * a message using the plugin. Note that in the case of a
1522 * peer disconnecting, the continuation MUST be called
1523 * prior to the disconnect notification itself. This function
1524 * will be called with this peer's HELLO message to initiate
1525 * a fresh connection to another peer.
1527 * @param cls closure
1528 * @param s which session must be used
1529 * @param msgbuf the message to transmit
1530 * @param msgbuf_size number of bytes in 'msgbuf'
1531 * @param priority how important is the message (most plugins will
1532 * ignore message priority and just FIFO)
1533 * @param to how long to wait at most for the transmission (does not
1534 * require plugins to discard the message after the timeout,
1535 * just advisory for the desired delay; most plugins will ignore
1537 * @param cont continuation to call once the message has
1538 * been transmitted (or if the transport is ready
1539 * for the next transmission call; or if the
1540 * peer disconnected...); can be NULL
1541 * @param cont_cls closure for cont
1542 * @return number of bytes used (on the physical network, with overheads);
1543 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1544 * and does NOT mean that the message was not transmitted (DV)
1547 udp_plugin_send (void *cls,
1549 const char *msgbuf, size_t msgbuf_size,
1550 unsigned int priority,
1551 struct GNUNET_TIME_Relative to,
1552 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1554 struct Plugin *plugin = cls;
1555 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1556 struct UDP_FragmentationContext * frag_ctx;
1557 struct UDP_MessageWrapper * udpw;
1558 struct UDPMessage *udp;
1560 GNUNET_assert (plugin != NULL);
1561 GNUNET_assert (s != NULL);
1563 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1564 return GNUNET_SYSERR;
1565 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1566 return GNUNET_SYSERR;
1567 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1570 return GNUNET_SYSERR;
1572 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1575 return GNUNET_SYSERR;
1577 LOG (GNUNET_ERROR_TYPE_DEBUG,
1578 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1580 GNUNET_i2s (&s->target),
1581 GNUNET_a2s(s->sock_addr, s->addrlen));
1585 udp = (struct UDPMessage *) mbuf;
1586 udp->header.size = htons (udpmlen);
1587 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1588 udp->reserved = htonl (0);
1589 udp->sender = *plugin->env->my_identity;
1591 reschedule_session_timeout(s);
1592 if (udpmlen <= UDP_MTU)
1594 /* unfragmented message */
1595 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1597 udpw->msg_buf = (char *) &udpw[1];
1598 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1599 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1600 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1602 udpw->cont_cls = cont_cls;
1603 udpw->frag_ctx = NULL;
1604 udpw->msg_type = MSG_UNFRAGMENTED;
1605 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1606 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1607 enqueue (plugin, udpw);
1609 GNUNET_STATISTICS_update (plugin->env->stats,
1610 "# UDP, unfragmented msgs, messages, attempt",
1612 GNUNET_STATISTICS_update (plugin->env->stats,
1613 "# UDP, unfragmented msgs, bytes payload, attempt",
1614 udpw->payload_size, GNUNET_NO);
1618 /* fragmented message */
1619 if (s->frag_ctx != NULL)
1620 return GNUNET_SYSERR;
1621 memcpy (&udp[1], msgbuf, msgbuf_size);
1622 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1623 frag_ctx->plugin = plugin;
1624 frag_ctx->session = s;
1625 frag_ctx->cont = cont;
1626 frag_ctx->cont_cls = cont_cls;
1627 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1628 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1629 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1630 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1633 s->last_expected_msg_delay,
1634 s->last_expected_ack_delay,
1638 s->frag_ctx = frag_ctx;
1639 GNUNET_STATISTICS_update (plugin->env->stats,
1640 "# UDP, fragmented msgs, messages, pending",
1642 GNUNET_STATISTICS_update (plugin->env->stats,
1643 "# UDP, fragmented msgs, messages, attempt",
1645 GNUNET_STATISTICS_update (plugin->env->stats,
1646 "# UDP, fragmented msgs, bytes payload, attempt",
1647 frag_ctx->payload_size, GNUNET_NO);
1649 schedule_select (plugin);
1655 * Our external IP address/port mapping has changed.
1657 * @param cls closure, the 'struct LocalAddrList'
1658 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1659 * the previous (now invalid) one
1660 * @param addr either the previous or the new public IP address
1661 * @param addrlen actual lenght of the address
1664 udp_nat_port_map_callback (void *cls, int add_remove,
1665 const struct sockaddr *addr, socklen_t addrlen)
1667 struct Plugin *plugin = cls;
1668 struct IPv4UdpAddress u4;
1669 struct IPv6UdpAddress u6;
1673 /* convert 'addr' to our internal format */
1674 switch (addr->sa_family)
1677 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1678 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1679 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1684 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1685 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1686 sizeof (struct in6_addr));
1687 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1695 /* modify our published address list */
1696 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1702 * Message tokenizer has broken up an incomming message. Pass it on
1705 * @param cls the 'struct Plugin'
1706 * @param client the 'struct SourceInformation'
1707 * @param hdr the actual message
1710 process_inbound_tokenized_messages (void *cls, void *client,
1711 const struct GNUNET_MessageHeader *hdr)
1713 struct Plugin *plugin = cls;
1714 struct SourceInformation *si = client;
1715 struct GNUNET_ATS_Information ats[2];
1716 struct GNUNET_TIME_Relative delay;
1718 GNUNET_assert (si->session != NULL);
1719 if (GNUNET_YES == si->session->in_destroy)
1722 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1723 ats[0].value = htonl (1);
1724 ats[1] = si->session->ats;
1725 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1726 delay = plugin->env->receive (plugin->env->cls,
1729 (const struct GNUNET_ATS_Information *) &ats, 2,
1733 si->session->flow_delay_for_other_peer = delay;
1734 reschedule_session_timeout(si->session);
1740 * We've received a UDP Message. Process it (pass contents to main service).
1742 * @param plugin plugin context
1743 * @param msg the message
1744 * @param sender_addr sender address
1745 * @param sender_addr_len number of bytes in sender_addr
1748 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1749 const struct sockaddr *sender_addr,
1750 socklen_t sender_addr_len)
1752 struct SourceInformation si;
1754 struct IPv4UdpAddress u4;
1755 struct IPv6UdpAddress u6;
1759 if (0 != ntohl (msg->reserved))
1761 GNUNET_break_op (0);
1764 if (ntohs (msg->header.size) <
1765 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1767 GNUNET_break_op (0);
1771 /* convert address */
1772 switch (sender_addr->sa_family)
1775 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1776 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1777 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1782 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1783 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1784 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 "Received message with %u bytes from peer `%s' at `%s'\n",
1794 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1795 GNUNET_a2s (sender_addr, sender_addr_len));
1797 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1798 s = udp_plugin_get_session(plugin, address);
1799 GNUNET_free (address);
1801 /* iterate over all embedded messages */
1803 si.sender = msg->sender;
1807 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1808 ntohs (msg->header.size) -
1809 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1811 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1817 * Scan the heap for a receive context with the given address.
1819 * @param cls the 'struct FindReceiveContext'
1820 * @param node internal node of the heap
1821 * @param element value stored at the node (a 'struct ReceiveContext')
1822 * @param cost cost associated with the node
1823 * @return GNUNET_YES if we should continue to iterate,
1827 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1828 void *element, GNUNET_CONTAINER_HeapCostType cost)
1830 struct FindReceiveContext *frc = cls;
1831 struct DefragContext *e = element;
1833 if ((frc->addr_len == e->addr_len) &&
1834 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1844 * Process a defragmented message.
1846 * @param cls the 'struct ReceiveContext'
1847 * @param msg the message
1850 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1852 struct DefragContext *rc = cls;
1854 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1859 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1864 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1865 rc->src_addr, rc->addr_len);
1869 struct LookupContext
1871 const struct sockaddr * addr;
1873 struct Session *res;
1880 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1882 struct LookupContext *l_ctx = cls;
1883 struct Session * s = value;
1885 if ((s->addrlen == l_ctx->addrlen) &&
1886 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1896 * Transmit an acknowledgement.
1898 * @param cls the 'struct ReceiveContext'
1899 * @param id message ID (unused)
1900 * @param msg ack to transmit
1903 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1905 struct DefragContext *rc = cls;
1906 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1907 struct UDP_ACK_Message *udp_ack;
1909 struct UDP_MessageWrapper *udpw;
1911 struct LookupContext l_ctx;
1913 l_ctx.addr = rc->src_addr;
1914 l_ctx.addrlen = rc->addr_len;
1916 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1917 &lookup_session_by_addr_it,
1924 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1925 delay = s->flow_delay_for_other_peer.rel_value;
1927 LOG (GNUNET_ERROR_TYPE_DEBUG,
1928 "Sending ACK to `%s' including delay of %u ms\n",
1929 GNUNET_a2s (rc->src_addr,
1930 (rc->src_addr->sa_family ==
1931 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1934 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
1935 udpw->msg_size = msize;
1936 udpw->payload_size = 0;
1938 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1939 udpw->msg_buf = (char *)&udpw[1];
1940 udpw->msg_type = MSG_ACK;
1941 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
1942 udp_ack->header.size = htons ((uint16_t) msize);
1943 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1944 udp_ack->delay = htonl (delay);
1945 udp_ack->sender = *rc->plugin->env->my_identity;
1946 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1947 enqueue (rc->plugin, udpw);
1952 read_process_msg (struct Plugin *plugin,
1953 const struct GNUNET_MessageHeader *msg,
1957 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1959 GNUNET_break_op (0);
1962 process_udp_message (plugin, (const struct UDPMessage *) msg,
1963 (const struct sockaddr *) addr, fromlen);
1968 read_process_ack (struct Plugin *plugin,
1969 const struct GNUNET_MessageHeader *msg,
1973 const struct GNUNET_MessageHeader *ack;
1974 const struct UDP_ACK_Message *udp_ack;
1975 struct LookupContext l_ctx;
1977 struct GNUNET_TIME_Relative flow_delay;
1979 if (ntohs (msg->size) <
1980 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1982 GNUNET_break_op (0);
1985 udp_ack = (const struct UDP_ACK_Message *) msg;
1986 l_ctx.addr = (const struct sockaddr *) addr;
1987 l_ctx.addrlen = fromlen;
1989 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1990 &lookup_session_by_addr_it,
1994 if ((NULL == s) || (NULL == s->frag_ctx))
1999 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
2000 LOG (GNUNET_ERROR_TYPE_DEBUG,
2001 "We received a sending delay of %llu\n",
2002 flow_delay.rel_value);
2003 s->flow_delay_from_other_peer =
2004 GNUNET_TIME_relative_to_absolute (flow_delay);
2006 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2007 if (ntohs (ack->size) !=
2008 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2010 GNUNET_break_op (0);
2014 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2016 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2018 LOG (GNUNET_ERROR_TYPE_DEBUG,
2019 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2020 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2021 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2022 /* Expect more ACKs to arrive */
2026 LOG (GNUNET_ERROR_TYPE_DEBUG,
2027 "Message full ACK'ed\n",
2028 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2029 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2031 /* Remove fragmented message after successful sending */
2032 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2037 read_process_fragment (struct Plugin *plugin,
2038 const struct GNUNET_MessageHeader *msg,
2042 struct DefragContext *d_ctx;
2043 struct GNUNET_TIME_Absolute now;
2044 struct FindReceiveContext frc;
2047 frc.addr = (const struct sockaddr *) addr;
2048 frc.addr_len = fromlen;
2050 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2051 (unsigned int) ntohs (msg->size),
2052 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2053 /* Lookup existing receive context for this address */
2054 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2055 &find_receive_context,
2057 now = GNUNET_TIME_absolute_get ();
2062 /* Create a new defragmentation context */
2063 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2064 memcpy (&d_ctx[1], addr, fromlen);
2065 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2066 d_ctx->addr_len = fromlen;
2067 d_ctx->plugin = plugin;
2069 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2070 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2071 &fragment_msg_proc, &ack_proc);
2073 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2074 (GNUNET_CONTAINER_HeapCostType)
2076 LOG (GNUNET_ERROR_TYPE_DEBUG,
2077 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2078 (unsigned int) ntohs (msg->size),
2079 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2083 LOG (GNUNET_ERROR_TYPE_DEBUG,
2084 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2085 (unsigned int) ntohs (msg->size),
2086 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2089 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2091 /* keep this 'rc' from expiring */
2092 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2093 (GNUNET_CONTAINER_HeapCostType)
2096 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2097 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2099 /* remove 'rc' that was inactive the longest */
2100 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2101 GNUNET_assert (NULL != d_ctx);
2102 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2103 GNUNET_free (d_ctx);
2109 * Read and process a message from the given socket.
2111 * @param plugin the overall plugin
2112 * @param rsock socket to read from
2115 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2119 char buf[65536] GNUNET_ALIGN;
2121 const struct GNUNET_MessageHeader *msg;
2123 fromlen = sizeof (addr);
2124 memset (&addr, 0, sizeof (addr));
2125 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2126 (struct sockaddr *) &addr, &fromlen);
2128 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2129 * WSAECONNRESET error to indicate that previous sendto() (???)
2130 * on this socket has failed.
2132 if ( (-1 == size) && (ECONNRESET == errno) )
2135 if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
2137 GNUNET_break_op (0);
2140 msg = (const struct GNUNET_MessageHeader *) buf;
2142 LOG (GNUNET_ERROR_TYPE_DEBUG,
2143 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2144 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2146 if (size != ntohs (msg->size))
2148 GNUNET_break_op (0);
2152 GNUNET_STATISTICS_update (plugin->env->stats,
2153 "# UDP, total, bytes, received",
2156 switch (ntohs (msg->type))
2158 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2159 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2162 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2163 read_process_msg (plugin, msg, addr, fromlen);
2166 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2167 read_process_ack (plugin, msg, addr, fromlen);
2170 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2171 read_process_fragment (plugin, msg, addr, fromlen);
2175 GNUNET_break_op (0);
2180 static struct UDP_MessageWrapper *
2181 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2182 struct GNUNET_NETWORK_Handle *sock)
2184 struct UDP_MessageWrapper *udpw = NULL;
2185 struct GNUNET_TIME_Relative remaining;
2188 while (udpw != NULL)
2190 /* Find messages with timeout */
2191 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2192 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2194 /* Message timed out */
2195 switch (udpw->msg_type) {
2196 case MSG_UNFRAGMENTED:
2197 GNUNET_STATISTICS_update (plugin->env->stats,
2198 "# UDP, total, bytes, sent, timeout",
2199 udpw->msg_size, GNUNET_NO);
2200 GNUNET_STATISTICS_update (plugin->env->stats,
2201 "# UDP, total, messages, sent, timeout",
2203 GNUNET_STATISTICS_update (plugin->env->stats,
2204 "# UDP, unfragmented msgs, messages, sent, timeout",
2206 GNUNET_STATISTICS_update (plugin->env->stats,
2207 "# UDP, unfragmented msgs, bytes, sent, timeout",
2208 udpw->payload_size, GNUNET_NO);
2209 /* Not fragmented message */
2210 LOG (GNUNET_ERROR_TYPE_DEBUG,
2211 "Message for peer `%s' with size %u timed out\n",
2212 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2213 call_continuation (udpw, GNUNET_SYSERR);
2214 /* Remove message */
2215 dequeue (plugin, udpw);
2218 case MSG_FRAGMENTED:
2219 /* Fragmented message */
2220 GNUNET_STATISTICS_update (plugin->env->stats,
2221 "# UDP, total, bytes, sent, timeout",
2222 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2223 GNUNET_STATISTICS_update (plugin->env->stats,
2224 "# UDP, total, messages, sent, timeout",
2226 call_continuation (udpw, GNUNET_SYSERR);
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 "Fragment for message for peer `%s' with size %u timed out\n",
2229 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2232 GNUNET_STATISTICS_update (plugin->env->stats,
2233 "# UDP, fragmented msgs, messages, sent, timeout",
2235 GNUNET_STATISTICS_update (plugin->env->stats,
2236 "# UDP, fragmented msgs, bytes, sent, timeout",
2237 udpw->frag_ctx->payload_size, GNUNET_NO);
2238 /* Remove fragmented message due to timeout */
2239 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2242 GNUNET_STATISTICS_update (plugin->env->stats,
2243 "# UDP, total, bytes, sent, timeout",
2244 udpw->msg_size, GNUNET_NO);
2245 GNUNET_STATISTICS_update (plugin->env->stats,
2246 "# UDP, total, messages, sent, timeout",
2248 LOG (GNUNET_ERROR_TYPE_DEBUG,
2249 "ACK Message for peer `%s' with size %u timed out\n",
2250 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2251 call_continuation (udpw, GNUNET_SYSERR);
2252 dequeue (plugin, udpw);
2258 if (sock == plugin->sockv4)
2259 udpw = plugin->ipv4_queue_head;
2260 else if (sock == plugin->sockv6)
2261 udpw = plugin->ipv6_queue_head;
2264 GNUNET_break (0); /* should never happen */
2267 GNUNET_STATISTICS_update (plugin->env->stats,
2268 "# messages dismissed due to timeout",
2273 /* Message did not time out, check flow delay */
2274 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2275 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2277 /* this message is not delayed */
2278 LOG (GNUNET_ERROR_TYPE_DEBUG,
2279 "Message for peer `%s' (%u bytes) is not delayed \n",
2280 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2281 break; /* Found message to send, break */
2285 /* Message is delayed, try next */
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
2288 GNUNET_i2s(&udpw->session->target), udpw->payload_size, remaining.rel_value);
2298 analyze_send_error (struct Plugin *plugin,
2299 const struct sockaddr * sa,
2303 static int network_down_error;
2304 struct GNUNET_ATS_Information type;
2306 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2307 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2308 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2310 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2312 /* IPv4: "Network unreachable" or "Network down"
2314 * This indicates we do not have connectivity
2316 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2317 _("UDP could not transmit message to `%s': "
2318 "Network seems down, please check your network configuration\n"),
2319 GNUNET_a2s (sa, slen));
2321 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2323 /* IPv6: "Network unreachable" or "Network down"
2325 * This indicates that this system is IPv6 enabled, but does not
2326 * have a valid global IPv6 address assigned or we do not have
2330 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2331 _("UDP could not transmit message to `%s': "
2332 "Please check your network configuration and disable IPv6 if your "
2333 "connection does not have a global IPv6 address\n"),
2334 GNUNET_a2s (sa, slen));
2339 LOG (GNUNET_ERROR_TYPE_WARNING,
2340 "UDP could not transmit message to `%s': `%s'\n",
2341 GNUNET_a2s (sa, slen), STRERROR (error));
2346 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2348 const struct sockaddr * sa;
2352 struct UDP_MessageWrapper *udpw = NULL;
2354 /* Find message to send */
2355 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2358 return 0; /* No message to send */
2360 sa = udpw->session->sock_addr;
2361 slen = udpw->session->addrlen;
2363 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2365 if (GNUNET_SYSERR == sent)
2368 analyze_send_error (plugin, sa, slen, errno);
2369 call_continuation(udpw, GNUNET_SYSERR);
2370 GNUNET_STATISTICS_update (plugin->env->stats,
2371 "# UDP, total, bytes, sent, failure",
2373 GNUNET_STATISTICS_update (plugin->env->stats,
2374 "# UDP, total, messages, sent, failure",
2380 LOG (GNUNET_ERROR_TYPE_DEBUG,
2381 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2382 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2383 (sent < 0) ? STRERROR (errno) : "ok");
2384 GNUNET_STATISTICS_update (plugin->env->stats,
2385 "# UDP, total, bytes, sent, success",
2387 GNUNET_STATISTICS_update (plugin->env->stats,
2388 "# UDP, total, messages, sent, success",
2390 if (NULL != udpw->frag_ctx)
2391 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2392 call_continuation (udpw, GNUNET_OK);
2394 dequeue (plugin, udpw);
2403 * We have been notified that our readset has something to read. We don't
2404 * know which socket needs to be read, so we have to check each one
2405 * Then reschedule this function to be called again once more is available.
2407 * @param cls the plugin handle
2408 * @param tc the scheduling context (for rescheduling this function again)
2411 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2413 struct Plugin *plugin = cls;
2415 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2416 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2418 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2419 (NULL != plugin->sockv4) &&
2420 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2421 udp_select_read (plugin, plugin->sockv4);
2422 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2423 (NULL != plugin->sockv4) &&
2424 (NULL != plugin->ipv4_queue_head) &&
2425 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2426 udp_select_send (plugin, plugin->sockv4);
2427 schedule_select (plugin);
2432 * We have been notified that our readset has something to read. We don't
2433 * know which socket needs to be read, so we have to check each one
2434 * Then reschedule this function to be called again once more is available.
2436 * @param cls the plugin handle
2437 * @param tc the scheduling context (for rescheduling this function again)
2440 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2442 struct Plugin *plugin = cls;
2444 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2445 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2447 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2448 (NULL != plugin->sockv6) &&
2449 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2450 udp_select_read (plugin, plugin->sockv6);
2451 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2452 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2453 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2454 udp_select_send (plugin, plugin->sockv6);
2455 schedule_select (plugin);
2460 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
2463 int sockets_created = 0;
2464 struct sockaddr *serverAddr;
2465 struct sockaddr *addrs[2];
2466 socklen_t addrlens[2];
2469 /* Create IPv6 socket */
2470 if (plugin->enable_ipv6 == GNUNET_YES)
2472 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2473 if (NULL == plugin->sockv6)
2475 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2476 plugin->enable_ipv6 = GNUNET_NO;
2480 #if HAVE_SOCKADDR_IN_SIN_LEN
2481 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2483 serverAddrv6->sin6_family = AF_INET6;
2484 serverAddrv6->sin6_addr = in6addr_any;
2485 serverAddrv6->sin6_port = htons (plugin->port);
2486 addrlen = sizeof (struct sockaddr_in6);
2487 serverAddr = (struct sockaddr *) serverAddrv6;
2488 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2489 ntohs (serverAddrv6->sin6_port));
2491 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2494 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2495 LOG (GNUNET_ERROR_TYPE_DEBUG,
2496 "IPv6 Binding failed, trying new port %d\n",
2497 ntohs (serverAddrv6->sin6_port));
2501 GNUNET_NETWORK_socket_close (plugin->sockv6);
2502 plugin->sockv6 = NULL;
2506 if (plugin->sockv6 != NULL)
2508 LOG (GNUNET_ERROR_TYPE_DEBUG,
2509 "IPv6 socket created on port %d\n",
2510 ntohs (serverAddrv6->sin6_port));
2511 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2512 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2518 /* Create IPv4 socket */
2519 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2520 if (NULL == plugin->sockv4)
2522 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2526 #if HAVE_SOCKADDR_IN_SIN_LEN
2527 serverAddrv4->sin_len = sizeof (serverAddrv4);
2529 serverAddrv4->sin_family = AF_INET;
2530 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2531 serverAddrv4->sin_port = htons (plugin->port);
2532 addrlen = sizeof (struct sockaddr_in);
2533 serverAddr = (struct sockaddr *) serverAddrv4;
2535 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2536 ntohs (serverAddrv4->sin_port));
2538 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2541 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2542 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2543 ntohs (serverAddrv4->sin_port));
2547 GNUNET_NETWORK_socket_close (plugin->sockv4);
2548 plugin->sockv4 = NULL;
2552 if (plugin->sockv4 != NULL)
2554 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2555 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2560 /* Create file descriptors */
2561 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2562 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2563 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2564 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2565 if (NULL != plugin->sockv4)
2567 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2568 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2571 if (0 == sockets_created)
2572 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2573 if (plugin->enable_ipv6 == GNUNET_YES)
2575 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2576 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2577 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2578 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2579 if (NULL != plugin->sockv6)
2581 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2582 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2585 schedule_select (plugin);
2586 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2587 GNUNET_NO, plugin->port,
2589 (const struct sockaddr **) addrs, addrlens,
2590 &udp_nat_port_map_callback, NULL, plugin);
2592 return sockets_created;
2597 * The exported method. Makes the core api available via a global and
2598 * returns the udp transport API.
2600 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2601 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2604 libgnunet_plugin_transport_udp_init (void *cls)
2606 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2607 struct GNUNET_TRANSPORT_PluginFunctions *api;
2609 unsigned long long port;
2610 unsigned long long aport;
2611 unsigned long long broadcast;
2612 unsigned long long udp_max_bps;
2613 unsigned long long enable_v6;
2614 char * bind4_address;
2615 char * bind6_address;
2616 char * fancy_interval;
2617 struct GNUNET_TIME_Relative interval;
2618 struct sockaddr_in serverAddrv4;
2619 struct sockaddr_in6 serverAddrv6;
2622 if (NULL == env->receive)
2624 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2625 initialze the plugin or the API */
2626 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2628 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2629 api->address_to_string = &udp_address_to_string;
2630 api->string_to_address = &udp_string_to_address;
2634 GNUNET_assert( NULL != env->stats);
2636 /* Get port number */
2638 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2642 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2643 "ADVERTISED_PORT", &aport))
2647 LOG (GNUNET_ERROR_TYPE_WARNING,
2648 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2655 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2658 enable_v6 = GNUNET_NO;
2661 enable_v6 = GNUNET_YES;
2664 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2665 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2668 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2669 "BINDTO", &bind4_address))
2671 LOG (GNUNET_ERROR_TYPE_DEBUG,
2672 "Binding udp plugin to specific address: `%s'\n",
2674 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2676 GNUNET_free (bind4_address);
2682 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2683 "BINDTO6", &bind6_address))
2685 LOG (GNUNET_ERROR_TYPE_DEBUG,
2686 "Binding udp plugin to specific address: `%s'\n",
2689 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2691 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2693 GNUNET_free_non_null (bind4_address);
2694 GNUNET_free (bind6_address);
2699 /* Enable neighbour discovery */
2700 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2702 if (broadcast == GNUNET_SYSERR)
2703 broadcast = GNUNET_NO;
2705 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2706 "BROADCAST_INTERVAL", &fancy_interval))
2708 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2712 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
2714 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2716 GNUNET_free (fancy_interval);
2719 /* Maximum datarate */
2720 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2721 "MAX_BPS", &udp_max_bps))
2723 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2726 p = GNUNET_malloc (sizeof (struct Plugin));
2727 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2729 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2730 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2731 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2732 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2733 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2736 p->broadcast_interval = interval;
2737 p->enable_ipv6 = enable_v6;
2744 api->disconnect = &udp_disconnect;
2745 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2746 api->address_to_string = &udp_address_to_string;
2747 api->string_to_address = &udp_string_to_address;
2748 api->check_address = &udp_plugin_check_address;
2749 api->get_session = &udp_plugin_get_session;
2750 api->send = &udp_plugin_send;
2752 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2753 res = setup_sockets (p, &serverAddrv6, &serverAddrv4);
2754 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
2756 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2762 if (broadcast == GNUNET_YES)
2764 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2765 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
2768 GNUNET_free_non_null (bind4_address);
2769 GNUNET_free_non_null (bind6_address);
2775 heap_cleanup_iterator (void *cls,
2776 struct GNUNET_CONTAINER_HeapNode *
2777 node, void *element,
2778 GNUNET_CONTAINER_HeapCostType
2781 struct DefragContext * d_ctx = element;
2783 GNUNET_CONTAINER_heap_remove_node (node);
2784 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2785 GNUNET_free (d_ctx);
2792 * The exported method. Makes the core api available via a global and
2793 * returns the udp transport API.
2795 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2799 libgnunet_plugin_transport_udp_done (void *cls)
2801 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2802 struct Plugin *plugin = api->cls;
2810 stop_broadcast (plugin);
2811 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2813 GNUNET_SCHEDULER_cancel (plugin->select_task);
2814 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2816 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2818 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2819 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2822 /* Closing sockets */
2823 if (plugin->sockv4 != NULL)
2825 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2826 plugin->sockv4 = NULL;
2828 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2829 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2831 if (plugin->sockv6 != NULL)
2833 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2834 plugin->sockv6 = NULL;
2836 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2837 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2840 GNUNET_NAT_unregister (plugin->nat);
2842 if (plugin->defrag_ctxs != NULL)
2844 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2845 heap_cleanup_iterator, NULL);
2846 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2847 plugin->defrag_ctxs = NULL;
2849 if (plugin->mst != NULL)
2851 GNUNET_SERVER_mst_destroy(plugin->mst);
2855 /* Clean up leftover messages */
2856 struct UDP_MessageWrapper * udpw;
2857 udpw = plugin->ipv4_queue_head;
2858 while (udpw != NULL)
2860 struct UDP_MessageWrapper *tmp = udpw->next;
2861 dequeue (plugin, udpw);
2862 call_continuation(udpw, GNUNET_SYSERR);
2867 udpw = plugin->ipv6_queue_head;
2868 while (udpw != NULL)
2870 struct UDP_MessageWrapper *tmp = udpw->next;
2871 dequeue (plugin, udpw);
2872 call_continuation(udpw, GNUNET_SYSERR);
2878 /* Clean up sessions */
2879 LOG (GNUNET_ERROR_TYPE_DEBUG,
2880 "Cleaning up sessions\n");
2881 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2882 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2885 GNUNET_free (plugin);
2891 /* end of plugin_transport_udp.c */