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.
92 * Which peer is this session for?
94 struct GNUNET_PeerIdentity target;
96 struct UDP_FragmentationContext * frag_ctx;
99 * Address of the other peer
101 const struct sockaddr *sock_addr;
104 * Desired delay for next sending we send to other peer
106 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
109 * Desired delay for next sending we received from other peer
111 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
114 * Session timeout task
116 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
119 * expected delay for ACKs
121 struct GNUNET_TIME_Relative last_expected_delay;
123 struct GNUNET_ATS_Information ats;
134 struct SessionCompareContext
137 const struct GNUNET_HELLO_Address *addr;
142 * Closure for 'process_inbound_tokenized_messages'
144 struct SourceInformation
149 struct GNUNET_PeerIdentity sender;
156 struct Session *session;
158 * Number of bytes in source address.
166 * Closure for 'find_receive_context'.
168 struct FindReceiveContext
171 * Where to store the result.
173 struct DefragContext *rc;
178 const struct sockaddr *addr;
180 struct Session *session;
183 * Number of bytes in 'addr'.
192 * Data structure to track defragmentation contexts based
193 * on the source of the UDP traffic.
199 * Defragmentation context.
201 struct GNUNET_DEFRAGMENT_Context *defrag;
204 * Source address this receive context is for (allocated at the
205 * end of the struct).
207 const struct sockaddr *src_addr;
210 * Reference to master plugin struct.
212 struct Plugin *plugin;
215 * Node in the defrag heap.
217 struct GNUNET_CONTAINER_HeapNode *hnode;
220 * Length of 'src_addr'
228 * Context to send fragmented messages
230 struct UDP_FragmentationContext
233 * Next in linked list
235 struct UDP_FragmentationContext * next;
238 * Previous in linked list
240 struct UDP_FragmentationContext * prev;
245 struct Plugin * plugin;
248 * Handle for GNUNET_FRAGMENT context
250 struct GNUNET_FRAGMENT_Context * frag;
253 * The session this fragmentation context belongs to
255 struct Session * session;
258 * Function to call upon completion of the transmission.
260 GNUNET_TRANSPORT_TransmitContinuation cont;
263 * Closure for 'cont'.
270 struct GNUNET_TIME_Absolute timeout;
273 * Payload size of original unfragmented message
278 * Bytes used to send all fragments on wire including UDP overhead
284 struct UDP_MessageWrapper
287 * Session this message belongs to
289 struct Session *session;
295 struct UDP_MessageWrapper *prev;
301 struct UDP_MessageWrapper *next;
304 * Message with size msg_size including UDP specific overhead
309 * Size of UDP message to send including UDP specific overhead
314 * Payload size of original message
321 struct GNUNET_TIME_Absolute timeout;
324 * Function to call upon completion of the transmission.
326 GNUNET_TRANSPORT_TransmitContinuation cont;
329 * Closure for 'cont'.
334 * Fragmentation context
335 * frag_ctx == NULL if transport <= MTU
336 * frag_ctx != NULL if transport > MTU
338 struct UDP_FragmentationContext *frag_ctx;
343 * UDP ACK Message-Packet header (after defragmentation).
345 struct UDP_ACK_Message
350 struct GNUNET_MessageHeader header;
353 * Desired delay for flow control
358 * What is the identity of the sender
360 struct GNUNET_PeerIdentity sender;
365 * Encapsulation of all of the state of the plugin.
367 struct Plugin * plugin;
371 * We have been notified that our readset has something to read. We don't
372 * know which socket needs to be read, so we have to check each one
373 * Then reschedule this function to be called again once more is available.
375 * @param cls the plugin handle
376 * @param tc the scheduling context (for rescheduling this function again)
379 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
383 * We have been notified that our readset has something to read. We don't
384 * know which socket needs to be read, so we have to check each one
385 * Then reschedule this function to be called again once more is available.
387 * @param cls the plugin handle
388 * @param tc the scheduling context (for rescheduling this function again)
391 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
395 * Start session timeout
398 start_session_timeout (struct Session *s);
401 * Increment session timeout due to activity
404 reschedule_session_timeout (struct Session *s);
410 stop_session_timeout (struct Session *s);
414 * (re)schedule select tasks for this plugin.
416 * @param plugin plugin to reschedule
419 schedule_select (struct Plugin *plugin)
421 struct GNUNET_TIME_Relative min_delay;
422 struct UDP_MessageWrapper *udpw;
424 if (NULL != plugin->sockv4)
426 /* Find a message ready to send:
427 * Flow delay from other peer is expired or not set (0) */
428 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
429 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
430 min_delay = GNUNET_TIME_relative_min (min_delay,
431 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
433 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
434 GNUNET_SCHEDULER_cancel(plugin->select_task);
437 * - write active set if message is ready
438 * - timeout minimum delay */
439 plugin->select_task =
440 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
441 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
443 (0 == min_delay.rel_value) ? plugin->ws_v4 : NULL,
444 &udp_plugin_select, plugin);
446 if (NULL != plugin->sockv6)
448 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
449 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
450 min_delay = GNUNET_TIME_relative_min (min_delay,
451 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
453 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
454 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
455 plugin->select_task_v6 =
456 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
457 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
459 (0 == min_delay.rel_value) ? plugin->ws_v6 : NULL,
460 &udp_plugin_select_v6, plugin);
466 * Function called for a quick conversion of the binary address to
467 * a numeric address. Note that the caller must not free the
468 * address and that the next call to this function is allowed
469 * to override the address again.
472 * @param addr binary address
473 * @param addrlen length of the address
474 * @return string representing the same address
477 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
479 static char rbuf[INET6_ADDRSTRLEN + 10];
480 char buf[INET6_ADDRSTRLEN];
484 const struct IPv4UdpAddress *t4;
485 const struct IPv6UdpAddress *t6;
489 if (addrlen == sizeof (struct IPv6UdpAddress))
493 port = ntohs (t6->u6_port);
494 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
497 else if (addrlen == sizeof (struct IPv4UdpAddress))
501 port = ntohs (t4->u4_port);
502 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
510 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
511 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
518 * Function called to convert a string address to
521 * @param cls closure ('struct Plugin*')
522 * @param addr string address
523 * @param addrlen length of the address
524 * @param buf location to store the buffer
525 * @param added location to store the number of bytes in the buffer.
526 * If the function returns GNUNET_SYSERR, its contents are undefined.
527 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
530 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
531 void **buf, size_t *added)
533 struct sockaddr_storage socket_address;
535 if ((NULL == addr) || (0 == addrlen))
538 return GNUNET_SYSERR;
541 if ('\0' != addr[addrlen - 1])
543 return GNUNET_SYSERR;
546 if (strlen (addr) != addrlen - 1)
548 return GNUNET_SYSERR;
551 if (GNUNET_OK != GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
554 return GNUNET_SYSERR;
557 switch (socket_address.ss_family)
561 struct IPv4UdpAddress *u4;
562 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
563 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
564 u4->ipv4_addr = in4->sin_addr.s_addr;
565 u4->u4_port = in4->sin_port;
567 *added = sizeof (struct IPv4UdpAddress);
572 struct IPv6UdpAddress *u6;
573 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
574 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
575 u6->ipv6_addr = in6->sin6_addr;
576 u6->u6_port = in6->sin6_port;
578 *added = sizeof (struct IPv6UdpAddress);
583 return GNUNET_SYSERR;
589 * Append our port and forward the result.
591 * @param cls a 'struct PrettyPrinterContext'
592 * @param hostname result from DNS resolver
595 append_port (void *cls, const char *hostname)
597 struct PrettyPrinterContext *ppc = cls;
600 if (hostname == NULL)
602 ppc->asc (ppc->asc_cls, NULL);
606 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
607 ppc->asc (ppc->asc_cls, ret);
613 * Convert the transports address to a nice, human-readable
617 * @param type name of the transport that generated the address
618 * @param addr one of the addresses of the host, NULL for the last address
619 * the specific address format depends on the transport
620 * @param addrlen length of the address
621 * @param numeric should (IP) addresses be displayed in numeric form?
622 * @param timeout after how long should we give up?
623 * @param asc function to call on each string
624 * @param asc_cls closure for asc
627 udp_plugin_address_pretty_printer (void *cls, const char *type,
628 const void *addr, size_t addrlen,
630 struct GNUNET_TIME_Relative timeout,
631 GNUNET_TRANSPORT_AddressStringCallback asc,
634 struct PrettyPrinterContext *ppc;
637 struct sockaddr_in a4;
638 struct sockaddr_in6 a6;
639 const struct IPv4UdpAddress *u4;
640 const struct IPv6UdpAddress *u6;
643 if (addrlen == sizeof (struct IPv6UdpAddress))
646 memset (&a6, 0, sizeof (a6));
647 a6.sin6_family = AF_INET6;
648 #if HAVE_SOCKADDR_IN_SIN_LEN
649 a6.sin6_len = sizeof (a6);
651 a6.sin6_port = u6->u6_port;
652 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
653 port = ntohs (u6->u6_port);
657 else if (addrlen == sizeof (struct IPv4UdpAddress))
660 memset (&a4, 0, sizeof (a4));
661 a4.sin_family = AF_INET;
662 #if HAVE_SOCKADDR_IN_SIN_LEN
663 a4.sin_len = sizeof (a4);
665 a4.sin_port = u4->u4_port;
666 a4.sin_addr.s_addr = u4->ipv4_addr;
667 port = ntohs (u4->u4_port);
671 else if (0 == addrlen)
673 asc (asc_cls, "<inbound connection>");
679 /* invalid address */
684 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
686 ppc->asc_cls = asc_cls;
688 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
693 call_continuation (struct UDP_MessageWrapper *udpw, int result)
695 LOG (GNUNET_ERROR_TYPE_DEBUG,
696 "Calling continuation for %u byte message to `%s' with result %s\n",
697 udpw->payload_size, GNUNET_i2s (&udpw->session->target),
698 (GNUNET_OK == result) ? "OK" : "SYSERR");
700 if (NULL == udpw->frag_ctx)
702 /* Not fragmented message */
703 if (GNUNET_OK == result)
705 GNUNET_STATISTICS_update (plugin->env->stats,
706 "# unfragmented messages transmit with success via UDP",
708 if (udpw->msg_size >= udpw->payload_size)
710 GNUNET_STATISTICS_update (plugin->env->stats,
711 "# bytes overhead transmitted via UDP",
712 udpw->msg_size - udpw->payload_size, GNUNET_NO);
714 GNUNET_STATISTICS_update (plugin->env->stats,
715 "# bytes payload transmitted via UDP",
716 udpw->payload_size, GNUNET_NO);
720 GNUNET_STATISTICS_update (plugin->env->stats,
721 "# unfragmented messages transmit with failure via UDP",
724 if (NULL != udpw->cont)
725 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
726 udpw->payload_size, udpw->msg_size);
730 /* Fragmented message */
731 if (GNUNET_OK == result)
733 /* Fragmented message: only call next_fragment continuation on success */
734 if (NULL != udpw->cont)
735 udpw->cont (udpw->cont_cls, &udpw->session->target, result,
736 udpw->payload_size, udpw->msg_size);
743 * Check if the given port is plausible (must be either our listen
744 * port or our advertised port). If it is neither, we return
747 * @param plugin global variables
748 * @param in_port port number to check
749 * @return GNUNET_OK if port is either open_port or adv_port
752 check_port (struct Plugin *plugin, uint16_t in_port)
754 if ((in_port == plugin->port) || (in_port == plugin->aport))
756 return GNUNET_SYSERR;
761 * Function that will be called to check if a binary address for this
762 * plugin is well-formed and corresponds to an address for THIS peer
763 * (as per our configuration). Naturally, if absolutely necessary,
764 * plugins can be a bit conservative in their answer, but in general
765 * plugins should make sure that the address does not redirect
766 * traffic to a 3rd party that might try to man-in-the-middle our
769 * @param cls closure, should be our handle to the Plugin
770 * @param addr pointer to the address
771 * @param addrlen length of addr
772 * @return GNUNET_OK if this is a plausible address for this peer
773 * and transport, GNUNET_SYSERR if not
777 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
779 struct Plugin *plugin = cls;
780 struct IPv4UdpAddress *v4;
781 struct IPv6UdpAddress *v6;
783 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
784 (addrlen != sizeof (struct IPv6UdpAddress)))
787 return GNUNET_SYSERR;
789 if (addrlen == sizeof (struct IPv4UdpAddress))
791 v4 = (struct IPv4UdpAddress *) addr;
792 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
793 return GNUNET_SYSERR;
795 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
796 sizeof (struct in_addr)))
797 return GNUNET_SYSERR;
801 v6 = (struct IPv6UdpAddress *) addr;
802 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
805 return GNUNET_SYSERR;
807 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
808 return GNUNET_SYSERR;
810 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
811 sizeof (struct in6_addr)))
812 return GNUNET_SYSERR;
819 * Task to free resources associated with a session.
821 * @param s session to free
824 free_session (struct Session *s)
826 if (s->frag_ctx != NULL)
828 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
829 GNUNET_free (s->frag_ctx);
837 dequeue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
839 GNUNET_STATISTICS_update (plugin->env->stats,
840 "# bytes currently in UDP buffers",
841 -udpw->msg_size, GNUNET_NO);
842 GNUNET_STATISTICS_update (plugin->env->stats,
843 "# msgs currently in UDP buffers",
845 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
846 GNUNET_CONTAINER_DLL_remove (plugin->ipv4_queue_head,
847 plugin->ipv4_queue_tail, udpw);
848 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
849 GNUNET_CONTAINER_DLL_remove (plugin->ipv6_queue_head,
850 plugin->ipv6_queue_tail, udpw);
854 * Functions with this signature are called whenever we need
855 * to close a session due to a disconnect or failure to
856 * establish a connection.
858 * @param s session to close down
861 disconnect_session (struct Session *s)
863 struct UDP_MessageWrapper *udpw;
864 struct UDP_MessageWrapper *next;
866 GNUNET_assert (GNUNET_YES != s->in_destroy);
867 LOG (GNUNET_ERROR_TYPE_DEBUG,
868 "Session %p to peer `%s' address ended \n",
870 GNUNET_i2s (&s->target),
871 GNUNET_a2s (s->sock_addr, s->addrlen));
872 stop_session_timeout (s);
873 next = plugin->ipv4_queue_head;
874 while (NULL != (udpw = next))
877 if (udpw->session == s)
879 dequeue (plugin, udpw);
880 call_continuation(udpw, GNUNET_SYSERR);
884 next = plugin->ipv6_queue_head;
885 while (NULL != (udpw = next))
888 if (udpw->session == s)
890 dequeue (plugin, udpw);
891 call_continuation(udpw, GNUNET_SYSERR);
896 plugin->env->session_end (plugin->env->cls, &s->target, s);
898 if (NULL != s->frag_ctx)
900 if (NULL != s->frag_ctx->cont)
902 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR,
903 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
906 GNUNET_i2s (&s->target));
910 GNUNET_assert (GNUNET_YES ==
911 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
912 &s->target.hashPubKey,
914 GNUNET_STATISTICS_set(plugin->env->stats,
915 "# UDP sessions active",
916 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
919 s->in_destroy = GNUNET_YES;
925 * Destroy a session, plugin is being unloaded.
928 * @param key hash of public key of target peer
929 * @param value a 'struct PeerSession*' to clean up
930 * @return GNUNET_OK (continue to iterate)
933 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
935 disconnect_session(value);
941 * Disconnect from a remote node. Clean up session if we have one for this peer
943 * @param cls closure for this call (should be handle to Plugin)
944 * @param target the peeridentity of the peer to disconnect
945 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
948 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
950 struct Plugin *plugin = cls;
951 GNUNET_assert (plugin != NULL);
953 GNUNET_assert (target != NULL);
954 LOG (GNUNET_ERROR_TYPE_DEBUG,
955 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
956 /* Clean up sessions */
957 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
962 * Session was idle, so disconnect it
965 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
967 GNUNET_assert (NULL != cls);
968 struct Session *s = cls;
970 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Session %p was idle for %llu ms, disconnecting\n",
973 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
974 /* call session destroy function */
975 disconnect_session (s);
980 * Start session timeout
983 start_session_timeout (struct Session *s)
985 GNUNET_assert (NULL != s);
986 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
987 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
991 "Timeout for session %p set to %llu ms\n",
992 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
997 * Increment session timeout due to activity
1000 reschedule_session_timeout (struct Session *s)
1002 GNUNET_assert (NULL != s);
1003 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
1005 GNUNET_SCHEDULER_cancel (s->timeout_task);
1006 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010 "Timeout rescheduled for session %p set to %llu ms\n",
1011 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1019 stop_session_timeout (struct Session *s)
1021 GNUNET_assert (NULL != s);
1023 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
1025 GNUNET_SCHEDULER_cancel (s->timeout_task);
1026 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1028 "Timeout stopped for session %p canceled\n",
1029 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
1034 static struct Session *
1035 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
1036 const void *addr, size_t addrlen,
1037 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1040 const struct IPv4UdpAddress *t4;
1041 const struct IPv6UdpAddress *t6;
1042 struct sockaddr_in *v4;
1043 struct sockaddr_in6 *v6;
1048 case sizeof (struct IPv4UdpAddress):
1049 if (NULL == plugin->sockv4)
1054 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
1055 len = sizeof (struct sockaddr_in);
1056 v4 = (struct sockaddr_in *) &s[1];
1057 v4->sin_family = AF_INET;
1058 #if HAVE_SOCKADDR_IN_SIN_LEN
1059 v4->sin_len = sizeof (struct sockaddr_in);
1061 v4->sin_port = t4->u4_port;
1062 v4->sin_addr.s_addr = t4->ipv4_addr;
1063 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
1065 case sizeof (struct IPv6UdpAddress):
1066 if (NULL == plugin->sockv6)
1072 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
1073 len = sizeof (struct sockaddr_in6);
1074 v6 = (struct sockaddr_in6 *) &s[1];
1075 v6->sin6_family = AF_INET6;
1076 #if HAVE_SOCKADDR_IN_SIN_LEN
1077 v6->sin6_len = sizeof (struct sockaddr_in6);
1079 v6->sin6_port = t6->u6_port;
1080 v6->sin6_addr = t6->ipv6_addr;
1081 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
1084 /* Must have a valid address to send to */
1085 GNUNET_break_op (0);
1089 s->target = *target;
1090 s->sock_addr = (const struct sockaddr *) &s[1];
1091 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
1092 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO_ABS;
1093 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1094 start_session_timeout (s);
1100 session_cmp_it (void *cls,
1101 const struct GNUNET_HashCode * key,
1104 struct SessionCompareContext * cctx = cls;
1105 const struct GNUNET_HELLO_Address *address = cctx->addr;
1106 struct Session *s = value;
1108 socklen_t s_addrlen = s->addrlen;
1110 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
1111 udp_address_to_string (NULL, (void *) address->address, address->address_length),
1112 GNUNET_a2s (s->sock_addr, s->addrlen));
1113 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
1114 (s_addrlen == sizeof (struct sockaddr_in)))
1116 struct IPv4UdpAddress * u4 = NULL;
1117 u4 = (struct IPv4UdpAddress *) address->address;
1118 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
1119 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
1120 (u4->u4_port == s4->sin_port))
1127 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1128 (s_addrlen == sizeof (struct sockaddr_in6)))
1130 struct IPv6UdpAddress * u6 = NULL;
1131 u6 = (struct IPv6UdpAddress *) address->address;
1132 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1133 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1134 (u6->u6_port == s6->sin6_port))
1145 * Creates a new outbound session the transport service will use to send data to the
1148 * @param cls the plugin
1149 * @param address the address
1150 * @return the session or NULL of max connections exceeded
1152 static struct Session *
1153 udp_plugin_get_session (void *cls,
1154 const struct GNUNET_HELLO_Address *address)
1156 struct Session * s = NULL;
1157 struct Plugin * plugin = cls;
1158 struct IPv6UdpAddress * udp_a6;
1159 struct IPv4UdpAddress * udp_a4;
1161 GNUNET_assert (plugin != NULL);
1162 GNUNET_assert (address != NULL);
1165 if ((address->address == NULL) ||
1166 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1167 (address->address_length != sizeof (struct IPv6UdpAddress))))
1173 if (address->address_length == sizeof (struct IPv4UdpAddress))
1175 if (plugin->sockv4 == NULL)
1177 udp_a4 = (struct IPv4UdpAddress *) address->address;
1178 if (udp_a4->u4_port == 0)
1182 if (address->address_length == sizeof (struct IPv6UdpAddress))
1184 if (plugin->sockv6 == NULL)
1186 udp_a6 = (struct IPv6UdpAddress *) address->address;
1187 if (udp_a6->u6_port == 0)
1191 /* check if session already exists */
1192 struct SessionCompareContext cctx;
1193 cctx.addr = address;
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Looking for existing session for peer `%s' `%s' \n",
1197 GNUNET_i2s (&address->peer),
1198 udp_address_to_string(NULL, address->address, address->address_length));
1199 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1200 if (cctx.res != NULL)
1202 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1206 /* otherwise create new */
1207 s = create_session (plugin,
1210 address->address_length,
1212 LOG (GNUNET_ERROR_TYPE_DEBUG,
1213 "Creating new session %p for peer `%s' address `%s'\n",
1215 GNUNET_i2s(&address->peer),
1216 udp_address_to_string(NULL,address->address,address->address_length));
1217 GNUNET_assert (GNUNET_OK ==
1218 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1219 &s->target.hashPubKey,
1221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1222 GNUNET_STATISTICS_set(plugin->env->stats,
1223 "# UDP sessions active",
1224 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1231 enqueue (struct Plugin *plugin, struct UDP_MessageWrapper * udpw)
1233 GNUNET_STATISTICS_update (plugin->env->stats,
1234 "# bytes currently in UDP buffers",
1235 udpw->msg_size, GNUNET_NO);
1236 GNUNET_STATISTICS_update (plugin->env->stats,
1237 "# msgs currently in UDP buffers",
1239 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1240 GNUNET_CONTAINER_DLL_insert (plugin->ipv4_queue_head,
1241 plugin->ipv4_queue_tail, udpw);
1242 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1243 GNUNET_CONTAINER_DLL_insert (plugin->ipv6_queue_head,
1244 plugin->ipv6_queue_tail, udpw);
1250 * Fragment message was transmitted via UDP, let fragmentation know
1251 * to send the next fragment now.
1253 * @param cls the 'struct UDPMessageWrapper' of the fragment
1254 * @param target destination peer (ignored)
1255 * @param result GNUNET_OK on success (ignored)
1256 * @param payload bytes payload sent
1257 * @param physical bytes physical sent
1260 send_next_fragment (void *cls,
1261 const struct GNUNET_PeerIdentity *target,
1262 int result, size_t payload, size_t physical)
1264 struct UDP_MessageWrapper *udpw = cls;
1265 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1270 * Function that is called with messages created by the fragmentation
1271 * module. In the case of the 'proc' callback of the
1272 * GNUNET_FRAGMENT_context_create function, this function must
1273 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1275 * @param cls closure, the 'struct FragmentationContext'
1276 * @param msg the message that was created
1279 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1281 struct UDP_FragmentationContext *frag_ctx = cls;
1282 struct Plugin *plugin = frag_ctx->plugin;
1283 struct UDP_MessageWrapper * udpw;
1284 size_t msg_len = ntohs (msg->size);
1286 LOG (GNUNET_ERROR_TYPE_DEBUG,
1287 "Enqueuing fragment with %u bytes\n", msg_len);
1288 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msg_len);
1289 udpw->session = frag_ctx->session;
1290 udpw->msg_buf = (char *) &udpw[1];
1291 udpw->msg_size = msg_len;
1292 udpw->payload_size = msg_len; /*FIXME: minus fragment overhead */
1293 udpw->cont = &send_next_fragment;
1294 udpw->cont_cls = udpw;
1295 udpw->timeout = frag_ctx->timeout;
1296 udpw->frag_ctx = frag_ctx;
1297 memcpy (udpw->msg_buf, msg, msg_len);
1298 enqueue (plugin, udpw);
1299 schedule_select (plugin);
1304 * Function that can be used by the transport service to transmit
1305 * a message using the plugin. Note that in the case of a
1306 * peer disconnecting, the continuation MUST be called
1307 * prior to the disconnect notification itself. This function
1308 * will be called with this peer's HELLO message to initiate
1309 * a fresh connection to another peer.
1311 * @param cls closure
1312 * @param s which session must be used
1313 * @param msgbuf the message to transmit
1314 * @param msgbuf_size number of bytes in 'msgbuf'
1315 * @param priority how important is the message (most plugins will
1316 * ignore message priority and just FIFO)
1317 * @param to how long to wait at most for the transmission (does not
1318 * require plugins to discard the message after the timeout,
1319 * just advisory for the desired delay; most plugins will ignore
1321 * @param cont continuation to call once the message has
1322 * been transmitted (or if the transport is ready
1323 * for the next transmission call; or if the
1324 * peer disconnected...); can be NULL
1325 * @param cont_cls closure for cont
1326 * @return number of bytes used (on the physical network, with overheads);
1327 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1328 * and does NOT mean that the message was not transmitted (DV)
1331 udp_plugin_send (void *cls,
1333 const char *msgbuf, size_t msgbuf_size,
1334 unsigned int priority,
1335 struct GNUNET_TIME_Relative to,
1336 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1338 struct Plugin *plugin = cls;
1339 size_t udpmlen = msgbuf_size + sizeof (struct UDPMessage);
1340 struct UDP_FragmentationContext * frag_ctx;
1341 struct UDP_MessageWrapper * udpw;
1342 struct UDPMessage *udp;
1344 GNUNET_assert (plugin != NULL);
1345 GNUNET_assert (s != NULL);
1347 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1348 return GNUNET_SYSERR;
1349 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1350 return GNUNET_SYSERR;
1351 if (udpmlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1354 return GNUNET_SYSERR;
1356 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1359 return GNUNET_SYSERR;
1361 LOG (GNUNET_ERROR_TYPE_DEBUG,
1362 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1364 GNUNET_i2s (&s->target),
1365 GNUNET_a2s(s->sock_addr, s->addrlen));
1367 GNUNET_STATISTICS_update (plugin->env->stats,
1368 "# bytes payload asked to transmit via UDP",
1369 msgbuf_size, GNUNET_NO);
1372 udp = (struct UDPMessage *) mbuf;
1373 udp->header.size = htons (udpmlen);
1374 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1375 udp->reserved = htonl (0);
1376 udp->sender = *plugin->env->my_identity;
1378 reschedule_session_timeout(s);
1379 if (udpmlen <= UDP_MTU)
1381 /* unfragmented message */
1382 GNUNET_STATISTICS_update (plugin->env->stats,
1383 "# unfragmented messages asked to transmit via UDP",
1385 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + udpmlen);
1387 udpw->msg_buf = (char *) &udpw[1];
1388 udpw->msg_size = udpmlen; /* message size with UDP overhead */
1389 udpw->payload_size = msgbuf_size; /* message size without UDP overhead */
1390 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1392 udpw->cont_cls = cont_cls;
1393 udpw->frag_ctx = NULL;
1394 memcpy (udpw->msg_buf, udp, sizeof (struct UDPMessage));
1395 memcpy (&udpw->msg_buf[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1396 enqueue (plugin, udpw);
1400 /* fragmented message */
1401 GNUNET_STATISTICS_update (plugin->env->stats,
1402 "# fragmented messages asked to transmit via UDP",
1404 if (s->frag_ctx != NULL)
1405 return GNUNET_SYSERR;
1406 memcpy (&udp[1], msgbuf, msgbuf_size);
1407 frag_ctx = GNUNET_malloc (sizeof (struct UDP_FragmentationContext));
1408 frag_ctx->plugin = plugin;
1409 frag_ctx->session = s;
1410 frag_ctx->cont = cont;
1411 frag_ctx->cont_cls = cont_cls;
1412 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1413 frag_ctx->payload_size = msgbuf_size; /* unfragmented message size without UDP overhead */
1414 frag_ctx->on_wire_size = 0; /* bytes with UDP and fragmentation overhead */
1415 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1418 s->last_expected_delay,
1422 s->frag_ctx = frag_ctx;
1424 schedule_select (plugin);
1430 * Our external IP address/port mapping has changed.
1432 * @param cls closure, the 'struct LocalAddrList'
1433 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1434 * the previous (now invalid) one
1435 * @param addr either the previous or the new public IP address
1436 * @param addrlen actual lenght of the address
1439 udp_nat_port_map_callback (void *cls, int add_remove,
1440 const struct sockaddr *addr, socklen_t addrlen)
1442 struct Plugin *plugin = cls;
1443 struct IPv4UdpAddress u4;
1444 struct IPv6UdpAddress u6;
1448 /* convert 'addr' to our internal format */
1449 switch (addr->sa_family)
1452 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1453 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1454 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1459 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1460 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1461 sizeof (struct in6_addr));
1462 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1470 /* modify our published address list */
1471 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1477 * Message tokenizer has broken up an incomming message. Pass it on
1480 * @param cls the 'struct Plugin'
1481 * @param client the 'struct SourceInformation'
1482 * @param hdr the actual message
1485 process_inbound_tokenized_messages (void *cls, void *client,
1486 const struct GNUNET_MessageHeader *hdr)
1488 struct Plugin *plugin = cls;
1489 struct SourceInformation *si = client;
1490 struct GNUNET_ATS_Information ats[2];
1491 struct GNUNET_TIME_Relative delay;
1493 GNUNET_assert (si->session != NULL);
1494 if (GNUNET_YES == si->session->in_destroy)
1497 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1498 ats[0].value = htonl (1);
1499 ats[1] = si->session->ats;
1500 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1501 delay = plugin->env->receive (plugin->env->cls,
1504 (const struct GNUNET_ATS_Information *) &ats, 2,
1508 si->session->flow_delay_for_other_peer = delay;
1509 reschedule_session_timeout(si->session);
1515 * We've received a UDP Message. Process it (pass contents to main service).
1517 * @param plugin plugin context
1518 * @param msg the message
1519 * @param sender_addr sender address
1520 * @param sender_addr_len number of bytes in sender_addr
1523 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1524 const struct sockaddr *sender_addr,
1525 socklen_t sender_addr_len)
1527 struct SourceInformation si;
1529 struct IPv4UdpAddress u4;
1530 struct IPv6UdpAddress u6;
1534 if (0 != ntohl (msg->reserved))
1536 GNUNET_break_op (0);
1539 if (ntohs (msg->header.size) <
1540 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1542 GNUNET_break_op (0);
1546 /* convert address */
1547 switch (sender_addr->sa_family)
1550 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1551 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1552 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1557 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1558 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1559 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1567 LOG (GNUNET_ERROR_TYPE_DEBUG,
1568 "Received message with %u bytes from peer `%s' at `%s'\n",
1569 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1570 GNUNET_a2s (sender_addr, sender_addr_len));
1572 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1573 s = udp_plugin_get_session(plugin, address);
1574 GNUNET_free (address);
1576 /* iterate over all embedded messages */
1578 si.sender = msg->sender;
1582 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1583 ntohs (msg->header.size) -
1584 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1586 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1592 * Scan the heap for a receive context with the given address.
1594 * @param cls the 'struct FindReceiveContext'
1595 * @param node internal node of the heap
1596 * @param element value stored at the node (a 'struct ReceiveContext')
1597 * @param cost cost associated with the node
1598 * @return GNUNET_YES if we should continue to iterate,
1602 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1603 void *element, GNUNET_CONTAINER_HeapCostType cost)
1605 struct FindReceiveContext *frc = cls;
1606 struct DefragContext *e = element;
1608 if ((frc->addr_len == e->addr_len) &&
1609 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1619 * Process a defragmented message.
1621 * @param cls the 'struct ReceiveContext'
1622 * @param msg the message
1625 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1627 struct DefragContext *rc = cls;
1629 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1634 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1639 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1640 rc->src_addr, rc->addr_len);
1644 struct LookupContext
1646 const struct sockaddr * addr;
1648 struct Session *res;
1655 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1657 struct LookupContext *l_ctx = cls;
1658 struct Session * s = value;
1660 if ((s->addrlen == l_ctx->addrlen) &&
1661 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1671 * Transmit an acknowledgement.
1673 * @param cls the 'struct ReceiveContext'
1674 * @param id message ID (unused)
1675 * @param msg ack to transmit
1678 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1680 struct DefragContext *rc = cls;
1681 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1682 struct UDP_ACK_Message *udp_ack;
1684 struct UDP_MessageWrapper *udpw;
1687 struct LookupContext l_ctx;
1688 l_ctx.addr = rc->src_addr;
1689 l_ctx.addrlen = rc->addr_len;
1691 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1692 &lookup_session_by_addr_it,
1699 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1700 delay = s->flow_delay_for_other_peer.rel_value;
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "Sending ACK to `%s' including delay of %u ms\n",
1704 GNUNET_a2s (rc->src_addr,
1705 (rc->src_addr->sa_family ==
1706 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1709 udpw = GNUNET_malloc (sizeof (struct UDP_MessageWrapper) + msize);
1710 udpw->msg_size = msize;
1711 udpw->payload_size = 0;
1713 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1714 udpw->msg_buf = (char *)&udpw[1];
1715 udp_ack = (struct UDP_ACK_Message *) udpw->msg_buf;
1716 udp_ack->header.size = htons ((uint16_t) msize);
1717 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1718 udp_ack->delay = htonl (delay);
1719 udp_ack->sender = *rc->plugin->env->my_identity;
1720 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1722 GNUNET_STATISTICS_update (plugin->env->stats,
1723 "# messages ACKs transmitted via UDP",
1726 enqueue (rc->plugin, udpw);
1731 read_process_msg (struct Plugin *plugin,
1732 const struct GNUNET_MessageHeader *msg,
1736 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1738 GNUNET_break_op (0);
1741 process_udp_message (plugin, (const struct UDPMessage *) msg,
1742 (const struct sockaddr *) addr, fromlen);
1747 read_process_ack (struct Plugin *plugin,
1748 const struct GNUNET_MessageHeader *msg,
1752 const struct GNUNET_MessageHeader *ack;
1753 const struct UDP_ACK_Message *udp_ack;
1754 struct LookupContext l_ctx;
1756 struct GNUNET_TIME_Relative flow_delay;
1758 if (ntohs (msg->size) <
1759 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1761 GNUNET_break_op (0);
1764 udp_ack = (const struct UDP_ACK_Message *) msg;
1765 l_ctx.addr = (const struct sockaddr *) addr;
1766 l_ctx.addrlen = fromlen;
1768 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1769 &lookup_session_by_addr_it,
1773 if ((s == NULL) || (s->frag_ctx == NULL))
1776 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1777 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1778 flow_delay.rel_value);
1779 s->flow_delay_from_other_peer =
1780 GNUNET_TIME_relative_to_absolute (flow_delay);
1782 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1783 if (ntohs (ack->size) !=
1784 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1786 GNUNET_break_op (0);
1790 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1792 LOG (GNUNET_ERROR_TYPE_DEBUG,
1793 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1794 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1795 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1796 /* Expect more ACKs to arrive */
1800 LOG (GNUNET_ERROR_TYPE_DEBUG,
1801 "Message full ACK'ed\n",
1802 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1803 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1804 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1806 struct UDP_MessageWrapper * udpw;
1807 struct UDP_MessageWrapper * tmp;
1808 if (s->addrlen == sizeof (struct sockaddr_in6))
1810 udpw = plugin->ipv6_queue_head;
1811 while (NULL != udpw)
1814 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1816 dequeue (plugin, udpw);
1822 if (s->addrlen == sizeof (struct sockaddr_in))
1824 udpw = plugin->ipv4_queue_head;
1828 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1830 dequeue (plugin, udpw);
1837 GNUNET_STATISTICS_update (plugin->env->stats,
1838 "# bytes payload transmitted via UDP",
1839 s->frag_ctx->payload_size, GNUNET_NO);
1841 GNUNET_STATISTICS_update (plugin->env->stats,
1842 "# fragmented messages transmit with success via UDP",
1845 if (s->frag_ctx->on_wire_size >= s->frag_ctx->payload_size)
1847 GNUNET_STATISTICS_update (plugin->env->stats,
1848 "# bytes overhead transmitted via UDP",
1849 s->frag_ctx->on_wire_size - s->frag_ctx->payload_size, GNUNET_NO);
1852 if (s->frag_ctx->cont != NULL)
1854 LOG (GNUNET_ERROR_TYPE_DEBUG,
1855 "Calling continuation for fragmented message to `%s' with result %s\n",
1856 GNUNET_i2s (&s->target), "OK");
1858 s->frag_ctx->cont (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK,
1859 s->frag_ctx->payload_size, s->frag_ctx->on_wire_size);
1862 GNUNET_free (s->frag_ctx);
1868 read_process_fragment (struct Plugin *plugin,
1869 const struct GNUNET_MessageHeader *msg,
1873 struct DefragContext *d_ctx;
1874 struct GNUNET_TIME_Absolute now;
1875 struct FindReceiveContext frc;
1878 frc.addr = (const struct sockaddr *) addr;
1879 frc.addr_len = fromlen;
1881 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1882 (unsigned int) ntohs (msg->size),
1883 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1884 /* Lookup existing receive context for this address */
1885 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1886 &find_receive_context,
1888 now = GNUNET_TIME_absolute_get ();
1893 /* Create a new defragmentation context */
1894 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1895 memcpy (&d_ctx[1], addr, fromlen);
1896 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1897 d_ctx->addr_len = fromlen;
1898 d_ctx->plugin = plugin;
1900 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1901 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1902 &fragment_msg_proc, &ack_proc);
1904 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1905 (GNUNET_CONTAINER_HeapCostType)
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "Created new defragmentation context for %u-byte fragment from `%s'\n",
1909 (unsigned int) ntohs (msg->size),
1910 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1914 LOG (GNUNET_ERROR_TYPE_DEBUG,
1915 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1916 (unsigned int) ntohs (msg->size),
1917 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1920 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1922 /* keep this 'rc' from expiring */
1923 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1924 (GNUNET_CONTAINER_HeapCostType)
1927 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1928 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1930 /* remove 'rc' that was inactive the longest */
1931 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1932 GNUNET_assert (NULL != d_ctx);
1933 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1934 GNUNET_free (d_ctx);
1940 * Read and process a message from the given socket.
1942 * @param plugin the overall plugin
1943 * @param rsock socket to read from
1946 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1950 char buf[65536] GNUNET_ALIGN;
1952 const struct GNUNET_MessageHeader *msg;
1954 fromlen = sizeof (addr);
1955 memset (&addr, 0, sizeof (addr));
1956 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1957 (struct sockaddr *) &addr, &fromlen);
1959 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
1960 * WSAECONNRESET error to indicate that previous sendto() (???)
1961 * on this socket has failed.
1963 if ( (-1 == size) && (ECONNRESET == errno) )
1966 if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
1968 GNUNET_break_op (0);
1971 msg = (const struct GNUNET_MessageHeader *) buf;
1973 LOG (GNUNET_ERROR_TYPE_DEBUG,
1974 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1975 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1977 if (size != ntohs (msg->size))
1979 GNUNET_break_op (0);
1983 GNUNET_STATISTICS_update (plugin->env->stats,
1984 "# bytes received via UDP",
1987 switch (ntohs (msg->type))
1989 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1990 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1993 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1994 read_process_msg (plugin, msg, addr, fromlen);
1997 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1998 read_process_ack (plugin, msg, addr, fromlen);
2001 case GNUNET_MESSAGE_TYPE_FRAGMENT:
2002 read_process_fragment (plugin, msg, addr, fromlen);
2006 GNUNET_break_op (0);
2011 static struct UDP_MessageWrapper *
2012 remove_timeout_messages_and_select (struct UDP_MessageWrapper *head,
2013 struct GNUNET_NETWORK_Handle *sock)
2015 struct UDP_MessageWrapper *udpw = NULL;
2016 struct GNUNET_TIME_Relative remaining;
2019 while (udpw != NULL)
2021 /* Find messages with timeout */
2022 remaining = GNUNET_TIME_absolute_get_remaining (udpw->timeout);
2023 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2025 /* Message timed out */
2026 call_continuation (udpw, GNUNET_SYSERR);
2027 if (NULL == udpw->frag_ctx)
2029 /* Not fragmented message */
2030 LOG (GNUNET_ERROR_TYPE_DEBUG,
2031 "Message for peer `%s' with size %u timed out\n",
2032 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2036 /* Fragmented message */
2037 LOG (GNUNET_ERROR_TYPE_DEBUG,
2038 "Fragment for message for peer `%s' with size %u timed out\n",
2039 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->payload_size);
2040 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy (udpw->frag_ctx->frag);
2041 GNUNET_free (udpw->frag_ctx);
2042 udpw->session->frag_ctx = NULL;
2045 GNUNET_STATISTICS_update (plugin->env->stats,
2046 "# messages dismissed due to timeout",
2048 /* Remove message */
2049 if (sock == plugin->sockv4)
2051 dequeue (plugin, udpw);
2053 udpw = plugin->ipv4_queue_head;
2055 if (sock == plugin->sockv6)
2057 dequeue (plugin, udpw);
2059 udpw = plugin->ipv6_queue_head;
2064 /* Message did not time out, check flow delay */
2065 remaining = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
2066 if (GNUNET_TIME_UNIT_ZERO.rel_value == remaining.rel_value)
2068 /* this message is not delayed */
2069 LOG (GNUNET_ERROR_TYPE_DEBUG,
2070 "Message for peer `%s' (%u bytes) is not delayed \n",
2071 GNUNET_i2s(&udpw->session->target), udpw->payload_size);
2072 break; /* Found message to send, break */
2076 /* Message is delayed, try next */
2077 LOG (GNUNET_ERROR_TYPE_DEBUG,
2078 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
2079 GNUNET_i2s(&udpw->session->target), udpw->payload_size, remaining.rel_value);
2089 analyze_send_error (struct Plugin *plugin,
2090 const struct sockaddr * sa,
2094 static int network_down_error;
2095 struct GNUNET_ATS_Information type;
2097 type = plugin->env->get_address_type (plugin->env->cls,sa, slen);
2098 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
2099 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
2101 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
2103 /* IPv4: "Network unreachable" or "Network down"
2105 * This indicates we do not have connectivity
2107 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2108 _("UDP could not transmit message to `%s': "
2109 "Network seems down, please check your network configuration\n"),
2110 GNUNET_a2s (sa, slen));
2112 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
2114 /* IPv6: "Network unreachable" or "Network down"
2116 * This indicates that this system is IPv6 enabled, but does not
2117 * have a valid global IPv6 address assigned or we do not have
2121 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2122 _("UDP could not transmit message to `%s': "
2123 "Please check your network configuration and disable IPv6 if your "
2124 "connection does not have a global IPv6 address\n"),
2125 GNUNET_a2s (sa, slen));
2130 LOG (GNUNET_ERROR_TYPE_WARNING,
2131 "UDP could not transmit message to `%s': `%s'\n",
2132 GNUNET_a2s (sa, slen), STRERROR (error));
2137 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
2139 const struct sockaddr * sa;
2143 struct UDP_MessageWrapper *udpw = NULL;
2145 /* Find message to send */
2146 udpw = remove_timeout_messages_and_select ((sock == plugin->sockv4) ? plugin->ipv4_queue_head : plugin->ipv6_queue_head,
2149 return 0; /* No message to send */
2151 sa = udpw->session->sock_addr;
2152 slen = udpw->session->addrlen;
2154 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->msg_buf, udpw->msg_size, sa, slen);
2156 if (GNUNET_SYSERR == sent)
2159 analyze_send_error (plugin, sa, slen, errno);
2160 call_continuation(udpw, GNUNET_SYSERR);
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 "UDP transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
2167 (unsigned int) (udpw->msg_size), GNUNET_i2s(&udpw->session->target) ,GNUNET_a2s (sa, slen), (int) sent,
2168 (sent < 0) ? STRERROR (errno) : "ok");
2169 GNUNET_STATISTICS_update (plugin->env->stats,
2170 "# bytes transmitted via UDP",
2172 if (NULL != udpw->frag_ctx)
2173 udpw->frag_ctx->on_wire_size += udpw->msg_size;
2174 call_continuation (udpw, GNUNET_OK);
2176 dequeue (plugin, udpw);
2185 * We have been notified that our readset has something to read. We don't
2186 * know which socket needs to be read, so we have to check each one
2187 * Then reschedule this function to be called again once more is available.
2189 * @param cls the plugin handle
2190 * @param tc the scheduling context (for rescheduling this function again)
2193 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2195 struct Plugin *plugin = cls;
2197 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2198 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2200 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2201 (NULL != plugin->sockv4) &&
2202 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2203 udp_select_read (plugin, plugin->sockv4);
2204 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2205 (NULL != plugin->sockv4) &&
2206 (NULL != plugin->ipv4_queue_head) &&
2207 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2208 udp_select_send (plugin, plugin->sockv4);
2209 schedule_select (plugin);
2214 * We have been notified that our readset has something to read. We don't
2215 * know which socket needs to be read, so we have to check each one
2216 * Then reschedule this function to be called again once more is available.
2218 * @param cls the plugin handle
2219 * @param tc the scheduling context (for rescheduling this function again)
2222 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2224 struct Plugin *plugin = cls;
2226 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2227 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2229 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2230 (NULL != plugin->sockv6) &&
2231 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2232 udp_select_read (plugin, plugin->sockv6);
2233 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2234 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2235 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2236 udp_select_send (plugin, plugin->sockv6);
2237 schedule_select (plugin);
2242 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
2245 int sockets_created = 0;
2246 struct sockaddr *serverAddr;
2247 struct sockaddr *addrs[2];
2248 socklen_t addrlens[2];
2251 /* Create IPv6 socket */
2252 if (plugin->enable_ipv6 == GNUNET_YES)
2254 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2255 if (NULL == plugin->sockv6)
2257 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2258 plugin->enable_ipv6 = GNUNET_NO;
2262 #if HAVE_SOCKADDR_IN_SIN_LEN
2263 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2265 serverAddrv6->sin6_family = AF_INET6;
2266 serverAddrv6->sin6_addr = in6addr_any;
2267 serverAddrv6->sin6_port = htons (plugin->port);
2268 addrlen = sizeof (struct sockaddr_in6);
2269 serverAddr = (struct sockaddr *) serverAddrv6;
2270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2271 ntohs (serverAddrv6->sin6_port));
2273 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2276 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2277 LOG (GNUNET_ERROR_TYPE_DEBUG,
2278 "IPv6 Binding failed, trying new port %d\n",
2279 ntohs (serverAddrv6->sin6_port));
2283 GNUNET_NETWORK_socket_close (plugin->sockv6);
2284 plugin->sockv6 = NULL;
2288 if (plugin->sockv6 != NULL)
2290 LOG (GNUNET_ERROR_TYPE_DEBUG,
2291 "IPv6 socket created on port %d\n",
2292 ntohs (serverAddrv6->sin6_port));
2293 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2294 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2300 /* Create IPv4 socket */
2301 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2302 if (NULL == plugin->sockv4)
2304 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2308 #if HAVE_SOCKADDR_IN_SIN_LEN
2309 serverAddrv4->sin_len = sizeof (serverAddrv4);
2311 serverAddrv4->sin_family = AF_INET;
2312 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2313 serverAddrv4->sin_port = htons (plugin->port);
2314 addrlen = sizeof (struct sockaddr_in);
2315 serverAddr = (struct sockaddr *) serverAddrv4;
2317 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2318 ntohs (serverAddrv4->sin_port));
2320 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2323 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2324 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2325 ntohs (serverAddrv4->sin_port));
2329 GNUNET_NETWORK_socket_close (plugin->sockv4);
2330 plugin->sockv4 = NULL;
2334 if (plugin->sockv4 != NULL)
2336 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2337 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2342 /* Create file descriptors */
2343 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2344 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2345 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2346 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2347 if (NULL != plugin->sockv4)
2349 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2350 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2353 if (0 == sockets_created)
2354 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2355 if (plugin->enable_ipv6 == GNUNET_YES)
2357 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2358 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2359 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2360 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2361 if (NULL != plugin->sockv6)
2363 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2364 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2367 schedule_select (plugin);
2368 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2369 GNUNET_NO, plugin->port,
2371 (const struct sockaddr **) addrs, addrlens,
2372 &udp_nat_port_map_callback, NULL, plugin);
2374 return sockets_created;
2379 * The exported method. Makes the core api available via a global and
2380 * returns the udp transport API.
2382 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2383 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2386 libgnunet_plugin_transport_udp_init (void *cls)
2388 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2389 struct GNUNET_TRANSPORT_PluginFunctions *api;
2391 unsigned long long port;
2392 unsigned long long aport;
2393 unsigned long long broadcast;
2394 unsigned long long udp_max_bps;
2395 unsigned long long enable_v6;
2396 char * bind4_address;
2397 char * bind6_address;
2398 char * fancy_interval;
2399 struct GNUNET_TIME_Relative interval;
2400 struct sockaddr_in serverAddrv4;
2401 struct sockaddr_in6 serverAddrv6;
2404 if (NULL == env->receive)
2406 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2407 initialze the plugin or the API */
2408 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2410 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2411 api->address_to_string = &udp_address_to_string;
2412 api->string_to_address = &udp_string_to_address;
2416 GNUNET_assert( NULL != env->stats);
2418 /* Get port number */
2420 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2424 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2425 "ADVERTISED_PORT", &aport))
2429 LOG (GNUNET_ERROR_TYPE_WARNING,
2430 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2437 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2440 enable_v6 = GNUNET_NO;
2443 enable_v6 = GNUNET_YES;
2446 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2447 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2450 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2451 "BINDTO", &bind4_address))
2453 LOG (GNUNET_ERROR_TYPE_DEBUG,
2454 "Binding udp plugin to specific address: `%s'\n",
2456 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2458 GNUNET_free (bind4_address);
2464 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2465 "BINDTO6", &bind6_address))
2467 LOG (GNUNET_ERROR_TYPE_DEBUG,
2468 "Binding udp plugin to specific address: `%s'\n",
2471 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2473 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2475 GNUNET_free_non_null (bind4_address);
2476 GNUNET_free (bind6_address);
2481 /* Enable neighbour discovery */
2482 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2484 if (broadcast == GNUNET_SYSERR)
2485 broadcast = GNUNET_NO;
2487 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2488 "BROADCAST_INTERVAL", &fancy_interval))
2490 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2494 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
2496 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2498 GNUNET_free (fancy_interval);
2501 /* Maximum datarate */
2502 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2503 "MAX_BPS", &udp_max_bps))
2505 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2508 p = GNUNET_malloc (sizeof (struct Plugin));
2509 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2511 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2512 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2513 p->sessions = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
2514 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2515 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2518 p->broadcast_interval = interval;
2519 p->enable_ipv6 = enable_v6;
2526 api->disconnect = &udp_disconnect;
2527 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2528 api->address_to_string = &udp_address_to_string;
2529 api->string_to_address = &udp_string_to_address;
2530 api->check_address = &udp_plugin_check_address;
2531 api->get_session = &udp_plugin_get_session;
2532 api->send = &udp_plugin_send;
2534 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2535 res = setup_sockets (p, &serverAddrv6, &serverAddrv4);
2536 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
2538 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2544 if (broadcast == GNUNET_YES)
2546 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2547 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
2550 GNUNET_free_non_null (bind4_address);
2551 GNUNET_free_non_null (bind6_address);
2557 heap_cleanup_iterator (void *cls,
2558 struct GNUNET_CONTAINER_HeapNode *
2559 node, void *element,
2560 GNUNET_CONTAINER_HeapCostType
2563 struct DefragContext * d_ctx = element;
2565 GNUNET_CONTAINER_heap_remove_node (node);
2566 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2567 GNUNET_free (d_ctx);
2574 * The exported method. Makes the core api available via a global and
2575 * returns the udp transport API.
2577 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2581 libgnunet_plugin_transport_udp_done (void *cls)
2583 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2584 struct Plugin *plugin = api->cls;
2592 stop_broadcast (plugin);
2593 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2595 GNUNET_SCHEDULER_cancel (plugin->select_task);
2596 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2598 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2600 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2601 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2604 /* Closing sockets */
2605 if (plugin->sockv4 != NULL)
2607 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2608 plugin->sockv4 = NULL;
2610 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2611 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2613 if (plugin->sockv6 != NULL)
2615 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2616 plugin->sockv6 = NULL;
2618 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2619 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2622 GNUNET_NAT_unregister (plugin->nat);
2624 if (plugin->defrag_ctxs != NULL)
2626 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2627 heap_cleanup_iterator, NULL);
2628 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2629 plugin->defrag_ctxs = NULL;
2631 if (plugin->mst != NULL)
2633 GNUNET_SERVER_mst_destroy(plugin->mst);
2637 /* Clean up leftover messages */
2638 struct UDP_MessageWrapper * udpw;
2639 udpw = plugin->ipv4_queue_head;
2640 while (udpw != NULL)
2642 struct UDP_MessageWrapper *tmp = udpw->next;
2643 dequeue (plugin, udpw);
2644 call_continuation(udpw, GNUNET_SYSERR);
2649 udpw = plugin->ipv6_queue_head;
2650 while (udpw != NULL)
2652 struct UDP_MessageWrapper *tmp = udpw->next;
2653 dequeue (plugin, udpw);
2654 call_continuation(udpw, GNUNET_SYSERR);
2660 /* Clean up sessions */
2661 LOG (GNUNET_ERROR_TYPE_DEBUG,
2662 "Cleaning up sessions\n");
2663 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2664 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2667 GNUNET_free (plugin);
2673 /* end of plugin_transport_udp.c */