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 if (plugin->bytes_in_buffer - udpw->msg_size < 0)
986 GNUNET_STATISTICS_update (plugin->env->stats,
987 "# UDP, total, bytes in buffers",
988 (long long) -udpw->msg_size, GNUNET_NO);
989 plugin->bytes_in_buffer -= udpw->msg_size;
991 GNUNET_STATISTICS_update (plugin->env->stats,
992 "# UDP, total, msgs in buffers",
994 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
995 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
996 plugin->ipv4_queue_tail, udpw);
997 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
998 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
999 plugin->ipv6_queue_tail, udpw);
1003 fragmented_message_done (struct UDP_FragmentationContext *fc, int result)
1005 struct UDP_MessageWrapper *udpw;
1006 struct UDP_MessageWrapper *tmp;
1007 struct UDP_MessageWrapper dummy;
1008 struct Session *s = fc->session;
1010 LOG (GNUNET_ERROR_TYPE_DEBUG, "%p : Fragmented message removed with result %s\n", fc, (result == GNUNET_SYSERR) ? "FAIL" : "SUCCESS");
1012 /* Call continuation for fragmented message */
1013 memset (&dummy, 0, sizeof (dummy));
1014 dummy.msg_type = MSG_FRAGMENTED_COMPLETE;
1015 dummy.msg_size = s->frag_ctx->on_wire_size;
1016 dummy.payload_size = s->frag_ctx->payload_size;
1017 dummy.frag_ctx = s->frag_ctx;
1019 dummy.cont_cls = NULL;
1022 call_continuation (&dummy, result);
1024 /* Remove leftover fragments from queue */
1025 if (s->addrlen == sizeof (struct sockaddr_in6))
1027 udpw = plugin->ipv6_queue_head;
1028 while (NULL != udpw)
1031 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1033 dequeue (plugin, udpw);
1034 call_continuation (udpw, GNUNET_SYSERR);
1040 if (s->addrlen == sizeof (struct sockaddr_in))
1042 udpw = plugin->ipv4_queue_head;
1046 if ((NULL != udpw->frag_ctx) && (udpw->frag_ctx == s->frag_ctx))
1048 dequeue (plugin, udpw);
1049 call_continuation (udpw, GNUNET_SYSERR);
1056 /* Destroy fragmentation context */
1057 GNUNET_FRAGMENT_context_destroy (fc->frag,
1058 &s->last_expected_msg_delay,
1059 &s->last_expected_ack_delay);
1065 * Functions with this signature are called whenever we need
1066 * to close a session due to a disconnect or failure to
1067 * establish a connection.
1069 * @param s session to close down
1072 disconnect_session (struct Session *s)
1074 struct UDP_MessageWrapper *udpw;
1075 struct UDP_MessageWrapper *next;
1077 GNUNET_assert (GNUNET_YES != s->in_destroy);
1078 LOG (GNUNET_ERROR_TYPE_DEBUG,
1079 "Session %p to peer `%s' address ended \n",
1081 GNUNET_i2s (&s->target),
1082 GNUNET_a2s (s->sock_addr, s->addrlen));
1083 stop_session_timeout (s);
1085 if (NULL != s->frag_ctx)
1087 /* Remove fragmented message due to disconnect */
1088 fragmented_message_done (s->frag_ctx, GNUNET_SYSERR);
1091 next = plugin->ipv4_queue_head;
1092 while (NULL != (udpw = next))
1095 if (udpw->session == s)
1097 dequeue (plugin, udpw);
1098 call_continuation(udpw, GNUNET_SYSERR);
1102 next = plugin->ipv6_queue_head;
1103 while (NULL != (udpw = next))
1106 if (udpw->session == s)
1108 dequeue (plugin, udpw);
1109 call_continuation(udpw, GNUNET_SYSERR);
1114 plugin->env->session_end (plugin->env->cls, &s->target, s);
1116 if (NULL != s->frag_ctx)
1118 if (NULL != s->frag_ctx->cont)
1120 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
1121 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
1124 GNUNET_i2s (&s->target));
1128 GNUNET_assert (GNUNET_YES ==
1129 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
1130 &s->target.hashPubKey,
1132 GNUNET_STATISTICS_set(plugin->env->stats,
1133 "# UDP, sessions active",
1134 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1137 s->in_destroy = GNUNET_YES;
1143 * Destroy a session, plugin is being unloaded.
1146 * @param key hash of public key of target peer
1147 * @param value a 'struct PeerSession*' to clean up
1148 * @return GNUNET_OK (continue to iterate)
1151 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1153 disconnect_session(value);
1159 * Disconnect from a remote node. Clean up session if we have one for this peer
1161 * @param cls closure for this call (should be handle to Plugin)
1162 * @param target the peeridentity of the peer to disconnect
1163 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
1166 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1168 struct Plugin *plugin = cls;
1169 GNUNET_assert (plugin != NULL);
1171 GNUNET_assert (target != NULL);
1172 LOG (GNUNET_ERROR_TYPE_DEBUG,
1173 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
1174 /* Clean up sessions */
1175 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
1180 * Session was idle, so disconnect it
1183 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1185 GNUNET_assert (NULL != cls);
1186 struct Session *s = cls;
1188 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Session %p was idle for %llu ms, disconnecting\n",
1191 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1192 /* call session destroy function */
1193 disconnect_session (s);
1198 * Start session timeout
1201 start_session_timeout (struct Session *s)
1203 GNUNET_assert (NULL != s);
1204 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
1205 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Timeout for session %p set to %llu ms\n",
1210 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1215 * Increment session timeout due to activity
1218 reschedule_session_timeout (struct Session *s)
1220 GNUNET_assert (NULL != s);
1221 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1223 GNUNET_SCHEDULER_cancel (s->timeout_task);
1224 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Timeout rescheduled for session %p set to %llu ms\n",
1229 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1237 stop_session_timeout (struct Session *s)
1239 GNUNET_assert (NULL != s);
1241 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1243 GNUNET_SCHEDULER_cancel (s->timeout_task);
1244 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 "Timeout stopped for session %p canceled\n",
1247 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1252 static struct Session *
1253 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1254 const void *addr, size_t addrlen,
1255 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1258 const struct IPv4UdpAddress *t4;
1259 const struct IPv6UdpAddress *t6;
1260 struct sockaddr_in *v4;
1261 struct sockaddr_in6 *v6;
1266 case sizeof (struct IPv4UdpAddress):
1267 if (NULL == plugin->sockv4)
1272 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1273 len = sizeof (struct sockaddr_in);
1274 v4 = (struct sockaddr_in *) &s[1];
1275 v4->sin_family = AF_INET;
1276 #if HAVE_SOCKADDR_IN_SIN_LEN
1277 v4->sin_len = sizeof (struct sockaddr_in);
1279 v4->sin_port = t4->u4_port;
1280 v4->sin_addr.s_addr = t4->ipv4_addr;
1281 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1283 case sizeof (struct IPv6UdpAddress):
1284 if (NULL == plugin->sockv6)
1290 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1291 len = sizeof (struct sockaddr_in6);
1292 v6 = (struct sockaddr_in6 *) &s[1];
1293 v6->sin6_family = AF_INET6;
1294 #if HAVE_SOCKADDR_IN_SIN_LEN
1295 v6->sin6_len = sizeof (struct sockaddr_in6);
1297 v6->sin6_port = t6->u6_port;
1298 v6->sin6_addr = t6->ipv6_addr;
1299 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1302 /* Must have a valid address to send to */
1303 GNUNET_break_op (0);
1307 s->target = *target;
1308 s->sock_addr = (const struct sockaddr *) &s[1];
1309 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250);
1310 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1311 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1312 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1313 start_session_timeout (s);
1319 session_cmp_it (void *cls,
1320 const struct GNUNET_HashCode * key,
1323 struct SessionCompareContext * cctx = cls;
1324 const struct GNUNET_HELLO_Address *address = cctx->addr;
1325 struct Session *s = value;
1327 socklen_t s_addrlen = s->addrlen;
1329 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1330 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1331 GNUNET_a2s (s->sock_addr, s->addrlen));
1332 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1333 (s_addrlen == sizeof (struct sockaddr_in)))
1335 struct IPv4UdpAddress * u4 = NULL;
1336 u4 = (struct IPv4UdpAddress *) address->address;
1337 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1338 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1339 (u4->u4_port == s4->sin_port))
1346 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1347 (s_addrlen == sizeof (struct sockaddr_in6)))
1349 struct IPv6UdpAddress * u6 = NULL;
1350 u6 = (struct IPv6UdpAddress *) address->address;
1351 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1352 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1353 (u6->u6_port == s6->sin6_port))
1364 * Creates a new outbound session the transport service will use to send data to the
1367 * @param cls the plugin
1368 * @param address the address
1369 * @return the session or NULL of max connections exceeded
1371 static struct Session *
1372 udp_plugin_get_session (void *cls,
1373 const struct GNUNET_HELLO_Address *address)
1375 struct Session * s = NULL;
1376 struct Plugin * plugin = cls;
1377 struct IPv6UdpAddress * udp_a6;
1378 struct IPv4UdpAddress * udp_a4;
1380 GNUNET_assert (plugin != NULL);
1381 GNUNET_assert (address != NULL);
1384 if ((address->address == NULL) ||
1385 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1386 (address->address_length != sizeof (struct IPv6UdpAddress))))
1392 if (address->address_length == sizeof (struct IPv4UdpAddress))
1394 if (plugin->sockv4 == NULL)
1396 udp_a4 = (struct IPv4UdpAddress *) address->address;
1397 if (udp_a4->u4_port == 0)
1401 if (address->address_length == sizeof (struct IPv6UdpAddress))
1403 if (plugin->sockv6 == NULL)
1405 udp_a6 = (struct IPv6UdpAddress *) address->address;
1406 if (udp_a6->u6_port == 0)
1410 /* check if session already exists */
1411 struct SessionCompareContext cctx;
1412 cctx.addr = address;
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "Looking for existing session for peer `%s' `%s' \n",
1416 GNUNET_i2s (&address->peer),
1417 udp_address_to_string(NULL, address->address, address->address_length));
1418 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1419 if (cctx.res != NULL)
1421 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1425 /* otherwise create new */
1426 s = create_session (plugin,
1429 address->address_length,
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 "Creating new session %p for peer `%s' address `%s'\n",
1434 GNUNET_i2s(&address->peer),
1435 udp_address_to_string(NULL,address->address,address->address_length));
1436 GNUNET_assert (GNUNET_OK ==
1437 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1438 &s->target.hashPubKey,
1440 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1441 GNUNET_STATISTICS_set(plugin->env->stats,
1442 "# UDP, sessions active",
1443 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1449 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1451 if (plugin->bytes_in_buffer + udpw->msg_size > INT64_MAX)
1455 GNUNET_STATISTICS_update (plugin->env->stats,
1456 "# UDP, total, bytes in buffers",
1457 udpw->msg_size, GNUNET_NO);
1458 plugin->bytes_in_buffer += udpw->msg_size;
1460 GNUNET_STATISTICS_update (plugin->env->stats,
1461 "# UDP, total, msgs in buffers",
1463 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1464 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1465 plugin->ipv4_queue_tail, udpw);
1466 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1467 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1468 plugin->ipv6_queue_tail, udpw);
1474 * Fragment message was transmitted via UDP, let fragmentation know
1475 * to send the next fragment now.
1477 * @param cls the 'struct UDPMessageWrapper' of the fragment
1478 * @param target destination peer (ignored)
1479 * @param result GNUNET_OK on success (ignored)
1480 * @param payload bytes payload sent
1481 * @param physical bytes physical sent
1484 send_next_fragment (void *cls,
1485 const struct GNUNET_PeerIdentity *target,
1486 int result, size_t payload, size_t physical)
1488 struct UDP_MessageWrapper *udpw = cls;
1489 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1494 * Function that is called with messages created by the fragmentation
1495 * module. In the case of the 'proc' callback of the
1496 * GNUNET_FRAGMENT_context_create function, this function must
1497 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1499 * @param cls closure, the 'struct FragmentationContext'
1500 * @param msg the message that was created
1503 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1505 struct UDP_FragmentationContext *frag_ctx = cls;
1506 struct Plugin *plugin = frag_ctx->plugin;
1507 struct UDP_MessageWrapper * udpw;
1508 size_t msg_len = ntohs (msg->size);
1510 LOG (GNUNET_ERROR_TYPE_DEBUG,
1511 "Enqueuing fragment with %u bytes\n", msg_len);
1512 frag_ctx->fragments_used ++;
1513 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1514 udpw->session = frag_ctx->session;
1515 udpw->msg_buf = (char *) &udpw[1];
1516 udpw->msg_size = msg_len;
1517 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1518 udpw->cont = &send_next_fragment;
1519 udpw->cont_cls = udpw;
1520 udpw->timeout = frag_ctx->timeout;
1521 udpw->frag_ctx = frag_ctx;
1522 udpw->msg_type = MSG_FRAGMENTED;
1523 memcpy (udpw->msg_buf, msg, msg_len);
1524 enqueue (plugin, udpw);
1525 schedule_select (plugin);
1530 * Function that can be used by the transport service to transmit
1531 * a message using the plugin. Note that in the case of a
1532 * peer disconnecting, the continuation MUST be called
1533 * prior to the disconnect notification itself. This function
1534 * will be called with this peer's HELLO message to initiate
1535 * a fresh connection to another peer.
1537 * @param cls closure
1538 * @param s which session must be used
1539 * @param msgbuf the message to transmit
1540 * @param msgbuf_size number of bytes in 'msgbuf'
1541 * @param priority how important is the message (most plugins will
1542 * ignore message priority and just FIFO)
1543 * @param to how long to wait at most for the transmission (does not
1544 * require plugins to discard the message after the timeout,
1545 * just advisory for the desired delay; most plugins will ignore
1547 * @param cont continuation to call once the message has
1548 * been transmitted (or if the transport is ready
1549 * for the next transmission call; or if the
1550 * peer disconnected...); can be NULL
1551 * @param cont_cls closure for cont
1552 * @return number of bytes used (on the physical network, with overheads);
1553 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1554 * and does NOT mean that the message was not transmitted (DV)
1557 udp_plugin_send (void *cls,
1559 const char *msgbuf, size_t msgbuf_size,
1560 unsigned int priority,
1561 struct GNUNET_TIME_Relative to,
1562 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1564 struct Plugin *plugin = cls;
1565 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1566 struct UDP_FragmentationContext * frag_ctx;
1567 struct UDP_MessageWrapper * udpw;
1568 struct UDPMessage *udp;
1570 GNUNET_assert (plugin != NULL);
1571 GNUNET_assert (s != NULL);
1573 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1574 return GNUNET_SYSERR;
1575 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1576 return GNUNET_SYSERR;
1577 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1580 return GNUNET_SYSERR;
1582 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1585 return GNUNET_SYSERR;
1587 LOG (GNUNET_ERROR_TYPE_DEBUG,
1588 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1590 GNUNET_i2s (&s->target),
1591 GNUNET_a2s(s->sock_addr, s->addrlen));
1595 udp = (struct UDPMessage *) mbuf;
1596 udp->header.size = htons (udpmlen);
1597 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1598 udp->reserved = htonl (0);
1599 udp->sender = *plugin->env->my_identity;
1601 reschedule_session_timeout(s);
1602 if (udpmlen <= UDP_MTU)
1604 /* unfragmented message */
1605 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1607 udpw->msg_buf = (char *) &udpw[1];
1608 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1609 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1610 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1612 udpw->cont_cls = cont_cls;
1613 udpw->frag_ctx = NULL;
1614 udpw->msg_type = MSG_UNFRAGMENTED;
1615 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1616 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1617 enqueue (plugin, udpw);
1619 GNUNET_STATISTICS_update (plugin->env->stats,
1620 "# UDP, unfragmented msgs, messages, attempt",
1622 GNUNET_STATISTICS_update (plugin->env->stats,
1623 "# UDP, unfragmented msgs, bytes payload, attempt",
1624 udpw->payload_size, GNUNET_NO);
1628 /* fragmented message */
1629 if (s->frag_ctx != NULL)
1630 return GNUNET_SYSERR;
1631 memcpy (&udp[1], msgbuf, msgbuf_size);
1632 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1633 frag_ctx->plugin = plugin;
1634 frag_ctx->session = s;
1635 frag_ctx->cont = cont;
1636 frag_ctx->cont_cls = cont_cls;
1637 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1638 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1639 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1640 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1643 s->last_expected_msg_delay,
1644 s->last_expected_ack_delay,
1648 s->frag_ctx = frag_ctx;
1649 GNUNET_STATISTICS_update (plugin->env->stats,
1650 "# UDP, fragmented msgs, messages, pending",
1652 GNUNET_STATISTICS_update (plugin->env->stats,
1653 "# UDP, fragmented msgs, messages, attempt",
1655 GNUNET_STATISTICS_update (plugin->env->stats,
1656 "# UDP, fragmented msgs, bytes payload, attempt",
1657 frag_ctx->payload_size, GNUNET_NO);
1659 schedule_select (plugin);
1665 * Our external IP address/port mapping has changed.
1667 * @param cls closure, the 'struct LocalAddrList'
1668 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1669 * the previous (now invalid) one
1670 * @param addr either the previous or the new public IP address
1671 * @param addrlen actual lenght of the address
1674 udp_nat_port_map_callback (void *cls, int add_remove,
1675 const struct sockaddr *addr, socklen_t addrlen)
1677 struct Plugin *plugin = cls;
1678 struct IPv4UdpAddress u4;
1679 struct IPv6UdpAddress u6;
1683 /* convert 'addr' to our internal format */
1684 switch (addr->sa_family)
1687 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1688 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1689 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1694 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1695 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1696 sizeof (struct in6_addr));
1697 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1705 /* modify our published address list */
1706 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1712 * Message tokenizer has broken up an incomming message. Pass it on
1715 * @param cls the 'struct Plugin'
1716 * @param client the 'struct SourceInformation'
1717 * @param hdr the actual message
1720 process_inbound_tokenized_messages (void *cls, void *client,
1721 const struct GNUNET_MessageHeader *hdr)
1723 struct Plugin *plugin = cls;
1724 struct SourceInformation *si = client;
1725 struct GNUNET_ATS_Information ats[2];
1726 struct GNUNET_TIME_Relative delay;
1728 GNUNET_assert (si->session != NULL);
1729 if (GNUNET_YES == si->session->in_destroy)
1732 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1733 ats[0].value = htonl (1);
1734 ats[1] = si->session->ats;
1735 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1736 delay = plugin->env->receive (plugin->env->cls,
1739 (const struct GNUNET_ATS_Information *) &ats, 2,
1743 si->session->flow_delay_for_other_peer = delay;
1744 reschedule_session_timeout(si->session);
1750 * We've received a UDP Message. Process it (pass contents to main service).
1752 * @param plugin plugin context
1753 * @param msg the message
1754 * @param sender_addr sender address
1755 * @param sender_addr_len number of bytes in sender_addr
1758 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1759 const struct sockaddr *sender_addr,
1760 socklen_t sender_addr_len)
1762 struct SourceInformation si;
1764 struct IPv4UdpAddress u4;
1765 struct IPv6UdpAddress u6;
1769 if (0 != ntohl (msg->reserved))
1771 GNUNET_break_op (0);
1774 if (ntohs (msg->header.size) <
1775 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1777 GNUNET_break_op (0);
1781 /* convert address */
1782 switch (sender_addr->sa_family)
1785 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1786 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1787 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1792 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1793 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1794 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1802 LOG (GNUNET_ERROR_TYPE_DEBUG,
1803 "Received message with %u bytes from peer `%s' at `%s'\n",
1804 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1805 GNUNET_a2s (sender_addr, sender_addr_len));
1807 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1808 s = udp_plugin_get_session(plugin, address);
1809 GNUNET_free (address);
1811 /* iterate over all embedded messages */
1813 si.sender = msg->sender;
1817 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1818 ntohs (msg->header.size) -
1819 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1821 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1827 * Scan the heap for a receive context with the given address.
1829 * @param cls the 'struct FindReceiveContext'
1830 * @param node internal node of the heap
1831 * @param element value stored at the node (a 'struct ReceiveContext')
1832 * @param cost cost associated with the node
1833 * @return GNUNET_YES if we should continue to iterate,
1837 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1838 void *element, GNUNET_CONTAINER_HeapCostType cost)
1840 struct FindReceiveContext *frc = cls;
1841 struct DefragContext *e = element;
1843 if ((frc->addr_len == e->addr_len) &&
1844 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1854 * Process a defragmented message.
1856 * @param cls the 'struct ReceiveContext'
1857 * @param msg the message
1860 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1862 struct DefragContext *rc = cls;
1864 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1869 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1874 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1875 rc->src_addr, rc->addr_len);
1879 struct LookupContext
1881 const struct sockaddr * addr;
1883 struct Session *res;
1890 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1892 struct LookupContext *l_ctx = cls;
1893 struct Session * s = value;
1895 if ((s->addrlen == l_ctx->addrlen) &&
1896 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1906 * Transmit an acknowledgement.
1908 * @param cls the 'struct ReceiveContext'
1909 * @param id message ID (unused)
1910 * @param msg ack to transmit
1913 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1915 struct DefragContext *rc = cls;
1916 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1917 struct UDP_ACK_Message *udp_ack;
1919 struct UDP_MessageWrapper *udpw;
1921 struct LookupContext l_ctx;
1923 l_ctx.addr = rc->src_addr;
1924 l_ctx.addrlen = rc->addr_len;
1926 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1927 &lookup_session_by_addr_it,
1934 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1935 delay = s->flow_delay_for_other_peer.rel_value;
1937 LOG (GNUNET_ERROR_TYPE_DEBUG,
1938 "Sending ACK to `%s' including delay of %u ms\n",
1939 GNUNET_a2s (rc->src_addr,
1940 (rc->src_addr->sa_family ==
1941 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1944 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
1945 udpw->msg_size = msize;
1946 udpw->payload_size = 0;
1948 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1949 udpw->msg_buf = (char *)&udpw[1];
1950 udpw->msg_type = MSG_ACK;
1951 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
1952 udp_ack->header.size = htons ((uint16_t) msize);
1953 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1954 udp_ack->delay = htonl (delay);
1955 udp_ack->sender = *rc->plugin->env->my_identity;
1956 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1957 enqueue (rc->plugin, udpw);
1962 read_process_msg (struct Plugin *plugin,
1963 const struct GNUNET_MessageHeader *msg,
1967 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1969 GNUNET_break_op (0);
1972 process_udp_message (plugin, (const struct UDPMessage *) msg,
1973 (const struct sockaddr *) addr, fromlen);
1978 read_process_ack (struct Plugin *plugin,
1979 const struct GNUNET_MessageHeader *msg,
1983 const struct GNUNET_MessageHeader *ack;
1984 const struct UDP_ACK_Message *udp_ack;
1985 struct LookupContext l_ctx;
1987 struct GNUNET_TIME_Relative flow_delay;
1989 if (ntohs (msg->size) <
1990 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1992 GNUNET_break_op (0);
1995 udp_ack = (const struct UDP_ACK_Message *) msg;
1996 l_ctx.addr = (const struct sockaddr *) addr;
1997 l_ctx.addrlen = fromlen;
1999 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
2000 &lookup_session_by_addr_it,
2004 if ((NULL == s) || (NULL == s->frag_ctx))
2009 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
2010 LOG (GNUNET_ERROR_TYPE_DEBUG,
2011 "We received a sending delay of %llu\n",
2012 flow_delay.rel_value);
2013 s->flow_delay_from_other_peer =
2014 GNUNET_TIME_relative_to_absolute (flow_delay);
2016 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
2017 if (ntohs (ack->size) !=
2018 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
2020 GNUNET_break_op (0);
2024 if (0 != memcmp (&l_ctx.res->target, &udp_ack->sender, sizeof (struct GNUNET_PeerIdentity)))
2026 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
2030 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2031 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2032 /* Expect more ACKs to arrive */
2036 LOG (GNUNET_ERROR_TYPE_DEBUG,
2037 "Message full ACK'ed\n",
2038 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
2039 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2041 /* Remove fragmented message after successful sending */
2042 fragmented_message_done (s->frag_ctx, GNUNET_OK);
2047 read_process_fragment (struct Plugin *plugin,
2048 const struct GNUNET_MessageHeader *msg,
2052 struct DefragContext *d_ctx;
2053 struct GNUNET_TIME_Absolute now;
2054 struct FindReceiveContext frc;
2057 frc.addr = (const struct sockaddr *) addr;
2058 frc.addr_len = fromlen;
2060 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
2061 (unsigned int) ntohs (msg->size),
2062 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2063 /* Lookup existing receive context for this address */
2064 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
2065 &find_receive_context,
2067 now = GNUNET_TIME_absolute_get ();
2072 /* Create a new defragmentation context */
2073 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
2074 memcpy (&d_ctx[1], addr, fromlen);
2075 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
2076 d_ctx->addr_len = fromlen;
2077 d_ctx->plugin = plugin;
2079 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
2080 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
2081 &fragment_msg_proc, &ack_proc);
2083 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
2084 (GNUNET_CONTAINER_HeapCostType)
2086 LOG (GNUNET_ERROR_TYPE_DEBUG,
2087 "Created new defragmentation context for %u-byte fragment from `%s'\n",
2088 (unsigned int) ntohs (msg->size),
2089 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2093 LOG (GNUNET_ERROR_TYPE_DEBUG,
2094 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
2095 (unsigned int) ntohs (msg->size),
2096 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
2099 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
2101 /* keep this 'rc' from expiring */
2102 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
2103 (GNUNET_CONTAINER_HeapCostType)
2106 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
2107 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
2109 /* remove 'rc' that was inactive the longest */
2110 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
2111 GNUNET_assert (NULL != d_ctx);
2112 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
2113 GNUNET_free (d_ctx);
2119 * Read and process a message from the given socket.
2121 * @param plugin the overall plugin
2122 * @param rsock socket to read from
2125 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
2129 char buf[65536] GNUNET_ALIGN;
2131 const struct GNUNET_MessageHeader *msg;
2133 fromlen = sizeof (addr);
2134 memset (&addr, 0, sizeof (addr));
2135 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
2136 (struct sockaddr *) &addr, &fromlen);
2138 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
2139 * WSAECONNRESET error to indicate that previous sendto() (???)
2140 * on this socket has failed.
2142 if ( (-1 == size) && (ECONNRESET == errno) )
2145 if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
2147 GNUNET_break_op (0);
2150 msg = (const struct GNUNET_MessageHeader *) buf;
2152 LOG (GNUNET_ERROR_TYPE_DEBUG,
2153 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
2154 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
2156 if (size != ntohs (msg->size))
2158 GNUNET_break_op (0);
2162 GNUNET_STATISTICS_update (plugin->env->stats,
2163 "# UDP, total, bytes, received",
2166 switch (ntohs (msg->type))
2168 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
2169 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
2172 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
2173 read_process_msg (plugin, msg, addr, fromlen);
2176 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
2177 read_process_ack (plugin, msg, addr, fromlen);
2180 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2181 read_process_fragment (plugin, msg, addr, fromlen);
2185 GNUNET_break_op (0);
2190 static struct UDP_MessageWrapper *
2191 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2192 struct GNUNET_NETWORK_Handle *sock)
2194 struct UDP_MessageWrapper *udpw = NULL;
2195 struct GNUNET_TIME_Relative remaining;
2198 while (udpw != NULL)
2200 /* Find messages with timeout */
2201 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2202 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2204 /* Message timed out */
2205 switch (udpw->msg_type) {
2206 case MSG_UNFRAGMENTED:
2207 GNUNET_STATISTICS_update (plugin->env->stats,
2208 "# UDP, total, bytes, sent, timeout",
2209 udpw->msg_size, GNUNET_NO);
2210 GNUNET_STATISTICS_update (plugin->env->stats,
2211 "# UDP, total, messages, sent, timeout",
2213 GNUNET_STATISTICS_update (plugin->env->stats,
2214 "# UDP, unfragmented msgs, messages, sent, timeout",
2216 GNUNET_STATISTICS_update (plugin->env->stats,
2217 "# UDP, unfragmented msgs, bytes, sent, timeout",
2218 udpw->payload_size, GNUNET_NO);
2219 /* Not fragmented message */
2220 LOG (GNUNET_ERROR_TYPE_DEBUG,
2221 "Message for peer `%s' with size %u timed out\n",
2222 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2223 call_continuation (udpw, GNUNET_SYSERR);
2224 /* Remove message */
2225 dequeue (plugin, udpw);
2228 case MSG_FRAGMENTED:
2229 /* Fragmented message */
2230 GNUNET_STATISTICS_update (plugin->env->stats,
2231 "# UDP, total, bytes, sent, timeout",
2232 udpw->frag_ctx->on_wire_size, GNUNET_NO);
2233 GNUNET_STATISTICS_update (plugin->env->stats,
2234 "# UDP, total, messages, sent, timeout",
2236 call_continuation (udpw, GNUNET_SYSERR);
2237 LOG (GNUNET_ERROR_TYPE_DEBUG,
2238 "Fragment for message for peer `%s' with size %u timed out\n",
2239 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2242 GNUNET_STATISTICS_update (plugin->env->stats,
2243 "# UDP, fragmented msgs, messages, sent, timeout",
2245 GNUNET_STATISTICS_update (plugin->env->stats,
2246 "# UDP, fragmented msgs, bytes, sent, timeout",
2247 udpw->frag_ctx->payload_size, GNUNET_NO);
2248 /* Remove fragmented message due to timeout */
2249 fragmented_message_done (udpw->frag_ctx, GNUNET_SYSERR);
2252 GNUNET_STATISTICS_update (plugin->env->stats,
2253 "# UDP, total, bytes, sent, timeout",
2254 udpw->msg_size, GNUNET_NO);
2255 GNUNET_STATISTICS_update (plugin->env->stats,
2256 "# UDP, total, messages, sent, timeout",
2258 LOG (GNUNET_ERROR_TYPE_DEBUG,
2259 "ACK Message for peer `%s' with size %u timed out\n",
2260 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2261 call_continuation (udpw, GNUNET_SYSERR);
2262 dequeue (plugin, udpw);
2268 if (sock == plugin->sockv4)
2269 udpw = plugin->ipv4_queue_head;
2270 else if (sock == plugin->sockv6)
2271 udpw = plugin->ipv6_queue_head;
2274 GNUNET_break (0); /* should never happen */
2277 GNUNET_STATISTICS_update (plugin->env->stats,
2278 "# messages dismissed due to timeout",
2283 /* Message did not time out, check flow delay */
2284 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2285 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2287 /* this message is not delayed */
2288 LOG (GNUNET_ERROR_TYPE_DEBUG,
2289 "Message for peer `%s' (%u bytes) is not delayed \n",
2290 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2291 break; /* Found message to send, break */
2295 /* Message is delayed, try next */
2296 LOG (GNUNET_ERROR_TYPE_DEBUG,
2297 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
2298 GNUNET_i2s(&udpw->session->target), udpw->payload_size, remaining.rel_value);
2308 analyze_send_error (struct Plugin *plugin,
2309 const struct sockaddr * sa,
2313 static int network_down_error;
2314 struct GNUNET_ATS_Information type;
2316 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2317 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2318 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2320 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2322 /* IPv4: "Network unreachable" or "Network down"
2324 * This indicates we do not have connectivity
2326 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2327 _("UDP could not transmit message to `%s': "
2328 "Network seems down, please check your network configuration\n"),
2329 GNUNET_a2s (sa, slen));
2331 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2333 /* IPv6: "Network unreachable" or "Network down"
2335 * This indicates that this system is IPv6 enabled, but does not
2336 * have a valid global IPv6 address assigned or we do not have
2340 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2341 _("UDP could not transmit message to `%s': "
2342 "Please check your network configuration and disable IPv6 if your "
2343 "connection does not have a global IPv6 address\n"),
2344 GNUNET_a2s (sa, slen));
2349 LOG (GNUNET_ERROR_TYPE_WARNING,
2350 "UDP could not transmit message to `%s': `%s'\n",
2351 GNUNET_a2s (sa, slen), STRERROR (error));
2356 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2358 const struct sockaddr * sa;
2362 struct UDP_MessageWrapper *udpw = NULL;
2364 /* Find message to send */
2365 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2368 return 0; /* No message to send */
2370 sa = udpw->session->sock_addr;
2371 slen = udpw->session->addrlen;
2373 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2375 if (GNUNET_SYSERR == sent)
2378 analyze_send_error (plugin, sa, slen, errno);
2379 call_continuation(udpw, GNUNET_SYSERR);
2380 GNUNET_STATISTICS_update (plugin->env->stats,
2381 "# UDP, total, bytes, sent, failure",
2383 GNUNET_STATISTICS_update (plugin->env->stats,
2384 "# UDP, total, messages, sent, failure",
2390 LOG (GNUNET_ERROR_TYPE_DEBUG,
2391 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2392 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2393 (sent < 0) ? STRERROR (errno) : "ok");
2394 GNUNET_STATISTICS_update (plugin->env->stats,
2395 "# UDP, total, bytes, sent, success",
2397 GNUNET_STATISTICS_update (plugin->env->stats,
2398 "# UDP, total, messages, sent, success",
2400 if (NULL != udpw->frag_ctx)
2401 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2402 call_continuation (udpw, GNUNET_OK);
2404 dequeue (plugin, udpw);
2413 * We have been notified that our readset has something to read. We don't
2414 * know which socket needs to be read, so we have to check each one
2415 * Then reschedule this function to be called again once more is available.
2417 * @param cls the plugin handle
2418 * @param tc the scheduling context (for rescheduling this function again)
2421 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2423 struct Plugin *plugin = cls;
2425 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2426 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2428 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2429 (NULL != plugin->sockv4) &&
2430 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2431 udp_select_read (plugin, plugin->sockv4);
2432 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2433 (NULL != plugin->sockv4) &&
2434 (NULL != plugin->ipv4_queue_head) &&
2435 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2436 udp_select_send (plugin, plugin->sockv4);
2437 schedule_select (plugin);
2442 * We have been notified that our readset has something to read. We don't
2443 * know which socket needs to be read, so we have to check each one
2444 * Then reschedule this function to be called again once more is available.
2446 * @param cls the plugin handle
2447 * @param tc the scheduling context (for rescheduling this function again)
2450 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2452 struct Plugin *plugin = cls;
2454 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2455 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2457 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2458 (NULL != plugin->sockv6) &&
2459 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2460 udp_select_read (plugin, plugin->sockv6);
2461 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2462 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2463 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2464 udp_select_send (plugin, plugin->sockv6);
2465 schedule_select (plugin);
2470 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
2473 int sockets_created = 0;
2474 struct sockaddr *serverAddr;
2475 struct sockaddr *addrs[2];
2476 socklen_t addrlens[2];
2479 /* Create IPv6 socket */
2480 if (plugin->enable_ipv6 == GNUNET_YES)
2482 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2483 if (NULL == plugin->sockv6)
2485 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2486 plugin->enable_ipv6 = GNUNET_NO;
2490 #if HAVE_SOCKADDR_IN_SIN_LEN
2491 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2493 serverAddrv6->sin6_family = AF_INET6;
2494 serverAddrv6->sin6_addr = in6addr_any;
2495 serverAddrv6->sin6_port = htons (plugin->port);
2496 addrlen = sizeof (struct sockaddr_in6);
2497 serverAddr = (struct sockaddr *) serverAddrv6;
2498 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2499 ntohs (serverAddrv6->sin6_port));
2501 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2504 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2505 LOG (GNUNET_ERROR_TYPE_DEBUG,
2506 "IPv6 Binding failed, trying new port %d\n",
2507 ntohs (serverAddrv6->sin6_port));
2511 GNUNET_NETWORK_socket_close (plugin->sockv6);
2512 plugin->sockv6 = NULL;
2516 if (plugin->sockv6 != NULL)
2518 LOG (GNUNET_ERROR_TYPE_DEBUG,
2519 "IPv6 socket created on port %d\n",
2520 ntohs (serverAddrv6->sin6_port));
2521 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2522 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2528 /* Create IPv4 socket */
2529 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2530 if (NULL == plugin->sockv4)
2532 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2536 #if HAVE_SOCKADDR_IN_SIN_LEN
2537 serverAddrv4->sin_len = sizeof (serverAddrv4);
2539 serverAddrv4->sin_family = AF_INET;
2540 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2541 serverAddrv4->sin_port = htons (plugin->port);
2542 addrlen = sizeof (struct sockaddr_in);
2543 serverAddr = (struct sockaddr *) serverAddrv4;
2545 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2546 ntohs (serverAddrv4->sin_port));
2548 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2551 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2552 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2553 ntohs (serverAddrv4->sin_port));
2557 GNUNET_NETWORK_socket_close (plugin->sockv4);
2558 plugin->sockv4 = NULL;
2562 if (plugin->sockv4 != NULL)
2564 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2565 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2570 /* Create file descriptors */
2571 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2572 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2573 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2574 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2575 if (NULL != plugin->sockv4)
2577 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2578 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2581 if (0 == sockets_created)
2582 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2583 if (plugin->enable_ipv6 == GNUNET_YES)
2585 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2586 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2587 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2588 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2589 if (NULL != plugin->sockv6)
2591 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2592 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2595 schedule_select (plugin);
2596 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2597 GNUNET_NO, plugin->port,
2599 (const struct sockaddr **) addrs, addrlens,
2600 &udp_nat_port_map_callback, NULL, plugin);
2602 return sockets_created;
2607 * The exported method. Makes the core api available via a global and
2608 * returns the udp transport API.
2610 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2611 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2614 libgnunet_plugin_transport_udp_init (void *cls)
2616 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2617 struct GNUNET_TRANSPORT_PluginFunctions *api;
2619 unsigned long long port;
2620 unsigned long long aport;
2621 unsigned long long broadcast;
2622 unsigned long long udp_max_bps;
2623 unsigned long long enable_v6;
2624 char * bind4_address;
2625 char * bind6_address;
2626 char * fancy_interval;
2627 struct GNUNET_TIME_Relative interval;
2628 struct sockaddr_in serverAddrv4;
2629 struct sockaddr_in6 serverAddrv6;
2632 if (NULL == env->receive)
2634 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2635 initialze the plugin or the API */
2636 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2638 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2639 api->address_to_string = &udp_address_to_string;
2640 api->string_to_address = &udp_string_to_address;
2644 GNUNET_assert( NULL != env->stats);
2646 /* Get port number */
2648 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2652 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2653 "ADVERTISED_PORT", &aport))
2657 LOG (GNUNET_ERROR_TYPE_WARNING,
2658 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2665 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2668 enable_v6 = GNUNET_NO;
2671 enable_v6 = GNUNET_YES;
2674 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2675 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2678 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2679 "BINDTO", &bind4_address))
2681 LOG (GNUNET_ERROR_TYPE_DEBUG,
2682 "Binding udp plugin to specific address: `%s'\n",
2684 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2686 GNUNET_free (bind4_address);
2692 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2693 "BINDTO6", &bind6_address))
2695 LOG (GNUNET_ERROR_TYPE_DEBUG,
2696 "Binding udp plugin to specific address: `%s'\n",
2699 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2701 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2703 GNUNET_free_non_null (bind4_address);
2704 GNUNET_free (bind6_address);
2709 /* Enable neighbour discovery */
2710 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2712 if (broadcast == GNUNET_SYSERR)
2713 broadcast = GNUNET_NO;
2715 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2716 "BROADCAST_INTERVAL", &fancy_interval))
2718 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2722 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
2724 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2726 GNUNET_free (fancy_interval);
2729 /* Maximum datarate */
2730 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2731 "MAX_BPS", &udp_max_bps))
2733 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2736 p = GNUNET_malloc (sizeof (struct Plugin));
2737 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2739 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2740 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2741 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2742 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2743 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2746 p->broadcast_interval = interval;
2747 p->enable_ipv6 = enable_v6;
2754 api->disconnect = &udp_disconnect;
2755 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2756 api->address_to_string = &udp_address_to_string;
2757 api->string_to_address = &udp_string_to_address;
2758 api->check_address = &udp_plugin_check_address;
2759 api->get_session = &udp_plugin_get_session;
2760 api->send = &udp_plugin_send;
2762 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2763 res = setup_sockets (p, &serverAddrv6, &serverAddrv4);
2764 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
2766 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2772 if (broadcast == GNUNET_YES)
2774 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2775 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
2778 GNUNET_free_non_null (bind4_address);
2779 GNUNET_free_non_null (bind6_address);
2785 heap_cleanup_iterator (void *cls,
2786 struct GNUNET_CONTAINER_HeapNode *
2787 node, void *element,
2788 GNUNET_CONTAINER_HeapCostType
2791 struct DefragContext * d_ctx = element;
2793 GNUNET_CONTAINER_heap_remove_node (node);
2794 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2795 GNUNET_free (d_ctx);
2802 * The exported method. Makes the core api available via a global and
2803 * returns the udp transport API.
2805 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2809 libgnunet_plugin_transport_udp_done (void *cls)
2811 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2812 struct Plugin *plugin = api->cls;
2820 stop_broadcast (plugin);
2821 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2823 GNUNET_SCHEDULER_cancel (plugin->select_task);
2824 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2826 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2828 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2829 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2832 /* Closing sockets */
2833 if (plugin->sockv4 != NULL)
2835 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2836 plugin->sockv4 = NULL;
2838 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2839 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2841 if (plugin->sockv6 != NULL)
2843 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2844 plugin->sockv6 = NULL;
2846 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2847 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2850 GNUNET_NAT_unregister (plugin->nat);
2852 if (plugin->defrag_ctxs != NULL)
2854 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2855 heap_cleanup_iterator, NULL);
2856 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2857 plugin->defrag_ctxs = NULL;
2859 if (plugin->mst != NULL)
2861 GNUNET_SERVER_mst_destroy(plugin->mst);
2865 /* Clean up leftover messages */
2866 struct UDP_MessageWrapper * udpw;
2867 udpw = plugin->ipv4_queue_head;
2868 while (udpw != NULL)
2870 struct UDP_MessageWrapper *tmp = udpw->next;
2871 dequeue (plugin, udpw);
2872 call_continuation(udpw, GNUNET_SYSERR);
2877 udpw = plugin->ipv6_queue_head;
2878 while (udpw != NULL)
2880 struct UDP_MessageWrapper *tmp = udpw->next;
2881 dequeue (plugin, udpw);
2882 call_continuation(udpw, GNUNET_SYSERR);
2888 /* Clean up sessions */
2889 LOG (GNUNET_ERROR_TYPE_DEBUG,
2890 "Cleaning up sessions\n");
2891 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2892 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2895 GNUNET_free (plugin);
2901 /* end of plugin_transport_udp.c */