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 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 * Closure for 'process_inbound_tokenized_messages'
230 struct FragmentationContext
232 struct FragmentationContext * next;
233 struct FragmentationContext * prev;
235 struct Plugin * plugin;
236 struct GNUNET_FRAGMENT_Context * frag;
237 struct Session * session;
240 * Function to call upon completion of the transmission.
242 GNUNET_TRANSPORT_TransmitContinuation cont;
245 * Closure for 'cont'.
249 struct GNUNET_TIME_Absolute timeout;
251 size_t bytes_to_send;
255 struct UDPMessageWrapper
257 struct Session *session;
258 struct UDPMessageWrapper *prev;
259 struct UDPMessageWrapper *next;
263 * Function to call upon completion of the transmission.
265 GNUNET_TRANSPORT_TransmitContinuation cont;
268 * Closure for 'cont'.
272 struct FragmentationContext *frag_ctx;
276 struct GNUNET_TIME_Absolute timeout;
281 * UDP ACK Message-Packet header (after defragmentation).
283 struct UDP_ACK_Message
288 struct GNUNET_MessageHeader header;
291 * Desired delay for flow control
296 * What is the identity of the sender
298 struct GNUNET_PeerIdentity sender;
303 * Encapsulation of all of the state of the plugin.
305 struct Plugin * plugin;
309 * We have been notified that our readset has something to read. We don't
310 * know which socket needs to be read, so we have to check each one
311 * Then reschedule this function to be called again once more is available.
313 * @param cls the plugin handle
314 * @param tc the scheduling context (for rescheduling this function again)
317 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
321 * We have been notified that our readset has something to read. We don't
322 * know which socket needs to be read, so we have to check each one
323 * Then reschedule this function to be called again once more is available.
325 * @param cls the plugin handle
326 * @param tc the scheduling context (for rescheduling this function again)
329 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
333 * Start session timeout
336 start_session_timeout (struct Session *s);
339 * Increment session timeout due to activity
342 reschedule_session_timeout (struct Session *s);
348 stop_session_timeout (struct Session *s);
352 * (re)schedule select tasks for this plugin.
354 * @param plugin plugin to reschedule
357 schedule_select (struct Plugin *plugin)
359 struct GNUNET_TIME_Relative min_delay;
360 struct UDPMessageWrapper *udpw;
362 if (NULL != plugin->sockv4)
364 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
365 for (udpw = plugin->ipv4_queue_head; NULL != udpw; udpw = udpw->next)
366 min_delay = GNUNET_TIME_relative_min (min_delay,
367 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
369 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
370 GNUNET_SCHEDULER_cancel(plugin->select_task);
371 plugin->select_task =
372 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
373 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
375 (0 == min_delay.rel_value) ? plugin->ws_v4 : NULL,
376 &udp_plugin_select, plugin);
378 if (NULL != plugin->sockv6)
380 min_delay = GNUNET_TIME_UNIT_FOREVER_REL;
381 for (udpw = plugin->ipv6_queue_head; NULL != udpw; udpw = udpw->next)
382 min_delay = GNUNET_TIME_relative_min (min_delay,
383 GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer));
385 if (GNUNET_SCHEDULER_NO_TASK != plugin->select_task_v6)
386 GNUNET_SCHEDULER_cancel(plugin->select_task_v6);
387 plugin->select_task_v6 =
388 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
389 (0 == min_delay.rel_value) ? GNUNET_TIME_UNIT_FOREVER_REL : min_delay,
391 (0 == min_delay.rel_value) ? plugin->ws_v6 : NULL,
392 &udp_plugin_select_v6, plugin);
398 * Function called for a quick conversion of the binary address to
399 * a numeric address. Note that the caller must not free the
400 * address and that the next call to this function is allowed
401 * to override the address again.
404 * @param addr binary address
405 * @param addrlen length of the address
406 * @return string representing the same address
409 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
411 static char rbuf[INET6_ADDRSTRLEN + 10];
412 char buf[INET6_ADDRSTRLEN];
416 const struct IPv4UdpAddress *t4;
417 const struct IPv6UdpAddress *t6;
421 if (addrlen == sizeof (struct IPv6UdpAddress))
425 port = ntohs (t6->u6_port);
426 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
429 else if (addrlen == sizeof (struct IPv4UdpAddress))
433 port = ntohs (t4->u4_port);
434 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
442 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
443 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
450 * Function called to convert a string address to
453 * @param cls closure ('struct Plugin*')
454 * @param addr string address
455 * @param addrlen length of the address
456 * @param buf location to store the buffer
457 * @param added location to store the number of bytes in the buffer.
458 * If the function returns GNUNET_SYSERR, its contents are undefined.
459 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
462 udp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
463 void **buf, size_t *added)
465 struct sockaddr_storage socket_address;
467 if ((NULL == addr) || (0 == addrlen))
470 return GNUNET_SYSERR;
473 if ('\0' != addr[addrlen - 1])
475 return GNUNET_SYSERR;
478 if (strlen (addr) != addrlen - 1)
480 return GNUNET_SYSERR;
483 if (GNUNET_OK != GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
486 return GNUNET_SYSERR;
489 switch (socket_address.ss_family)
493 struct IPv4UdpAddress *u4;
494 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
495 u4 = GNUNET_malloc (sizeof (struct IPv4UdpAddress));
496 u4->ipv4_addr = in4->sin_addr.s_addr;
497 u4->u4_port = in4->sin_port;
499 *added = sizeof (struct IPv4UdpAddress);
504 struct IPv6UdpAddress *u6;
505 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
506 u6 = GNUNET_malloc (sizeof (struct IPv6UdpAddress));
507 u6->ipv6_addr = in6->sin6_addr;
508 u6->u6_port = in6->sin6_port;
510 *added = sizeof (struct IPv6UdpAddress);
515 return GNUNET_SYSERR;
521 * Append our port and forward the result.
523 * @param cls a 'struct PrettyPrinterContext'
524 * @param hostname result from DNS resolver
527 append_port (void *cls, const char *hostname)
529 struct PrettyPrinterContext *ppc = cls;
532 if (hostname == NULL)
534 ppc->asc (ppc->asc_cls, NULL);
538 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
539 ppc->asc (ppc->asc_cls, ret);
545 * Convert the transports address to a nice, human-readable
549 * @param type name of the transport that generated the address
550 * @param addr one of the addresses of the host, NULL for the last address
551 * the specific address format depends on the transport
552 * @param addrlen length of the address
553 * @param numeric should (IP) addresses be displayed in numeric form?
554 * @param timeout after how long should we give up?
555 * @param asc function to call on each string
556 * @param asc_cls closure for asc
559 udp_plugin_address_pretty_printer (void *cls, const char *type,
560 const void *addr, size_t addrlen,
562 struct GNUNET_TIME_Relative timeout,
563 GNUNET_TRANSPORT_AddressStringCallback asc,
566 struct PrettyPrinterContext *ppc;
569 struct sockaddr_in a4;
570 struct sockaddr_in6 a6;
571 const struct IPv4UdpAddress *u4;
572 const struct IPv6UdpAddress *u6;
575 if (addrlen == sizeof (struct IPv6UdpAddress))
578 memset (&a6, 0, sizeof (a6));
579 a6.sin6_family = AF_INET6;
580 #if HAVE_SOCKADDR_IN_SIN_LEN
581 a6.sin6_len = sizeof (a6);
583 a6.sin6_port = u6->u6_port;
584 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
585 port = ntohs (u6->u6_port);
589 else if (addrlen == sizeof (struct IPv4UdpAddress))
592 memset (&a4, 0, sizeof (a4));
593 a4.sin_family = AF_INET;
594 #if HAVE_SOCKADDR_IN_SIN_LEN
595 a4.sin_len = sizeof (a4);
597 a4.sin_port = u4->u4_port;
598 a4.sin_addr.s_addr = u4->ipv4_addr;
599 port = ntohs (u4->u4_port);
603 else if (0 == addrlen)
605 asc (asc_cls, "<inbound connection>");
611 /* invalid address */
616 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
618 ppc->asc_cls = asc_cls;
620 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
625 call_continuation (struct UDPMessageWrapper *udpw, int result)
627 LOG (GNUNET_ERROR_TYPE_DEBUG,
628 "Calling continuation for %u byte message to `%s' with result %s\n",
629 udpw->msg_size, GNUNET_i2s (&udpw->session->target),
630 (GNUNET_OK == result) ? "OK" : "SYSERR");
631 if (NULL != udpw->cont)
633 udpw->cont (udpw->cont_cls, &udpw->session->target,result);
640 * Check if the given port is plausible (must be either our listen
641 * port or our advertised port). If it is neither, we return
644 * @param plugin global variables
645 * @param in_port port number to check
646 * @return GNUNET_OK if port is either open_port or adv_port
649 check_port (struct Plugin *plugin, uint16_t in_port)
651 if ((in_port == plugin->port) || (in_port == plugin->aport))
653 return GNUNET_SYSERR;
658 * Function that will be called to check if a binary address for this
659 * plugin is well-formed and corresponds to an address for THIS peer
660 * (as per our configuration). Naturally, if absolutely necessary,
661 * plugins can be a bit conservative in their answer, but in general
662 * plugins should make sure that the address does not redirect
663 * traffic to a 3rd party that might try to man-in-the-middle our
666 * @param cls closure, should be our handle to the Plugin
667 * @param addr pointer to the address
668 * @param addrlen length of addr
669 * @return GNUNET_OK if this is a plausible address for this peer
670 * and transport, GNUNET_SYSERR if not
674 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
676 struct Plugin *plugin = cls;
677 struct IPv4UdpAddress *v4;
678 struct IPv6UdpAddress *v6;
680 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
681 (addrlen != sizeof (struct IPv6UdpAddress)))
684 return GNUNET_SYSERR;
686 if (addrlen == sizeof (struct IPv4UdpAddress))
688 v4 = (struct IPv4UdpAddress *) addr;
689 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
690 return GNUNET_SYSERR;
692 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
693 sizeof (struct in_addr)))
694 return GNUNET_SYSERR;
698 v6 = (struct IPv6UdpAddress *) addr;
699 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
702 return GNUNET_SYSERR;
704 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
705 return GNUNET_SYSERR;
707 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
708 sizeof (struct in6_addr)))
709 return GNUNET_SYSERR;
716 * Task to free resources associated with a session.
718 * @param s session to free
721 free_session (struct Session *s)
723 if (s->frag_ctx != NULL)
725 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
726 GNUNET_free (s->frag_ctx);
734 * Functions with this signature are called whenever we need
735 * to close a session due to a disconnect or failure to
736 * establish a connection.
738 * @param s session to close down
741 disconnect_session (struct Session *s)
743 struct UDPMessageWrapper *udpw;
744 struct UDPMessageWrapper *next;
746 GNUNET_assert (GNUNET_YES != s->in_destroy);
747 LOG (GNUNET_ERROR_TYPE_DEBUG,
748 "Session %p to peer `%s' address ended \n",
750 GNUNET_i2s (&s->target),
751 GNUNET_a2s (s->sock_addr, s->addrlen));
752 stop_session_timeout (s);
753 next = plugin->ipv4_queue_head;
754 while (NULL != (udpw = next))
757 if (udpw->session == s)
759 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
760 call_continuation(udpw, GNUNET_SYSERR);
764 next = plugin->ipv6_queue_head;
765 while (NULL != (udpw = next))
768 if (udpw->session == s)
770 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
771 call_continuation(udpw, GNUNET_SYSERR);
776 plugin->env->session_end (plugin->env->cls, &s->target, s);
778 if (NULL != s->frag_ctx)
780 if (NULL != s->frag_ctx->cont)
782 s->frag_ctx->cont (s->frag_ctx->cont_cls, &s->target, GNUNET_SYSERR);
783 LOG (GNUNET_ERROR_TYPE_DEBUG,
784 "Calling continuation for fragemented message to `%s' with result SYSERR\n",
785 GNUNET_i2s (&s->target));
789 GNUNET_assert (GNUNET_YES ==
790 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
791 &s->target.hashPubKey,
793 GNUNET_STATISTICS_set(plugin->env->stats,
794 "# UDP sessions active",
795 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
798 s->in_destroy = GNUNET_YES;
804 * Destroy a session, plugin is being unloaded.
807 * @param key hash of public key of target peer
808 * @param value a 'struct PeerSession*' to clean up
809 * @return GNUNET_OK (continue to iterate)
812 disconnect_and_free_it (void *cls, const struct GNUNET_HashCode * key, void *value)
814 disconnect_session(value);
820 * Disconnect from a remote node. Clean up session if we have one for this peer
822 * @param cls closure for this call (should be handle to Plugin)
823 * @param target the peeridentity of the peer to disconnect
824 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
827 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
829 struct Plugin *plugin = cls;
830 GNUNET_assert (plugin != NULL);
832 GNUNET_assert (target != NULL);
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
835 /* Clean up sessions */
836 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
841 * Session was idle, so disconnect it
844 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
846 GNUNET_assert (NULL != cls);
847 struct Session *s = cls;
849 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Session %p was idle for %llu ms, disconnecting\n",
852 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
853 /* call session destroy function */
854 disconnect_session (s);
859 * Start session timeout
862 start_session_timeout (struct Session *s)
864 GNUNET_assert (NULL != s);
865 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
866 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
870 "Timeout for session %p set to %llu ms\n",
871 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
876 * Increment session timeout due to activity
879 reschedule_session_timeout (struct Session *s)
881 GNUNET_assert (NULL != s);
882 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
884 GNUNET_SCHEDULER_cancel (s->timeout_task);
885 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Timeout rescheduled for session %p set to %llu ms\n",
890 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
898 stop_session_timeout (struct Session *s)
900 GNUNET_assert (NULL != s);
902 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
904 GNUNET_SCHEDULER_cancel (s->timeout_task);
905 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Timeout stopped for session %p canceled\n",
908 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
913 static struct Session *
914 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
915 const void *addr, size_t addrlen,
916 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
919 const struct IPv4UdpAddress *t4;
920 const struct IPv6UdpAddress *t6;
921 struct sockaddr_in *v4;
922 struct sockaddr_in6 *v6;
927 case sizeof (struct IPv4UdpAddress):
928 if (NULL == plugin->sockv4)
933 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
934 len = sizeof (struct sockaddr_in);
935 v4 = (struct sockaddr_in *) &s[1];
936 v4->sin_family = AF_INET;
937 #if HAVE_SOCKADDR_IN_SIN_LEN
938 v4->sin_len = sizeof (struct sockaddr_in);
940 v4->sin_port = t4->u4_port;
941 v4->sin_addr.s_addr = t4->ipv4_addr;
942 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
944 case sizeof (struct IPv6UdpAddress):
945 if (NULL == plugin->sockv6)
951 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
952 len = sizeof (struct sockaddr_in6);
953 v6 = (struct sockaddr_in6 *) &s[1];
954 v6->sin6_family = AF_INET6;
955 #if HAVE_SOCKADDR_IN_SIN_LEN
956 v6->sin6_len = sizeof (struct sockaddr_in6);
958 v6->sin6_port = t6->u6_port;
959 v6->sin6_addr = t6->ipv6_addr;
960 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
963 /* Must have a valid address to send to */
969 s->sock_addr = (const struct sockaddr *) &s[1];
970 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
971 start_session_timeout (s);
977 session_cmp_it (void *cls,
978 const struct GNUNET_HashCode * key,
981 struct SessionCompareContext * cctx = cls;
982 const struct GNUNET_HELLO_Address *address = cctx->addr;
983 struct Session *s = value;
985 socklen_t s_addrlen = s->addrlen;
987 LOG (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
988 udp_address_to_string (NULL, (void *) address->address, address->address_length),
989 GNUNET_a2s (s->sock_addr, s->addrlen));
990 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
991 (s_addrlen == sizeof (struct sockaddr_in)))
993 struct IPv4UdpAddress * u4 = NULL;
994 u4 = (struct IPv4UdpAddress *) address->address;
995 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
996 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
997 (u4->u4_port == s4->sin_port))
1004 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
1005 (s_addrlen == sizeof (struct sockaddr_in6)))
1007 struct IPv6UdpAddress * u6 = NULL;
1008 u6 = (struct IPv6UdpAddress *) address->address;
1009 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
1010 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
1011 (u6->u6_port == s6->sin6_port))
1022 * Creates a new outbound session the transport service will use to send data to the
1025 * @param cls the plugin
1026 * @param address the address
1027 * @return the session or NULL of max connections exceeded
1029 static struct Session *
1030 udp_plugin_get_session (void *cls,
1031 const struct GNUNET_HELLO_Address *address)
1033 struct Session * s = NULL;
1034 struct Plugin * plugin = cls;
1035 struct IPv6UdpAddress * udp_a6;
1036 struct IPv4UdpAddress * udp_a4;
1038 GNUNET_assert (plugin != NULL);
1039 GNUNET_assert (address != NULL);
1042 if ((address->address == NULL) ||
1043 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
1044 (address->address_length != sizeof (struct IPv6UdpAddress))))
1050 if (address->address_length == sizeof (struct IPv4UdpAddress))
1052 if (plugin->sockv4 == NULL)
1054 udp_a4 = (struct IPv4UdpAddress *) address->address;
1055 if (udp_a4->u4_port == 0)
1059 if (address->address_length == sizeof (struct IPv6UdpAddress))
1061 if (plugin->sockv6 == NULL)
1063 udp_a6 = (struct IPv6UdpAddress *) address->address;
1064 if (udp_a6->u6_port == 0)
1068 /* check if session already exists */
1069 struct SessionCompareContext cctx;
1070 cctx.addr = address;
1072 LOG (GNUNET_ERROR_TYPE_DEBUG,
1073 "Looking for existing session for peer `%s' `%s' \n",
1074 GNUNET_i2s (&address->peer),
1075 udp_address_to_string(NULL, address->address, address->address_length));
1076 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
1077 if (cctx.res != NULL)
1079 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
1083 /* otherwise create new */
1084 s = create_session (plugin,
1087 address->address_length,
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Creating new session %p for peer `%s' address `%s'\n",
1092 GNUNET_i2s(&address->peer),
1093 udp_address_to_string(NULL,address->address,address->address_length));
1094 GNUNET_assert (GNUNET_OK ==
1095 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
1096 &s->target.hashPubKey,
1098 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1100 GNUNET_STATISTICS_set(plugin->env->stats,
1101 "# UDP sessions active",
1102 GNUNET_CONTAINER_multihashmap_size(plugin->sessions),
1110 enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
1113 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
1114 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1115 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
1116 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1121 * Fragment message was transmitted via UDP, let fragmentation know
1122 * to send the next fragment now.
1124 * @param cls the 'struct UDPMessageWrapper' of the fragment
1125 * @param target destination peer (ignored)
1126 * @param result GNUNET_OK on success (ignored)
1129 send_next_fragment (void *cls,
1130 const struct GNUNET_PeerIdentity *target,
1133 struct UDPMessageWrapper *udpw = cls;
1135 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1140 * Function that is called with messages created by the fragmentation
1141 * module. In the case of the 'proc' callback of the
1142 * GNUNET_FRAGMENT_context_create function, this function must
1143 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
1145 * @param cls closure, the 'struct FragmentationContext'
1146 * @param msg the message that was created
1149 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
1151 struct FragmentationContext *frag_ctx = cls;
1152 struct Plugin *plugin = frag_ctx->plugin;
1153 struct UDPMessageWrapper * udpw;
1154 size_t msg_len = ntohs (msg->size);
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
1158 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
1159 udpw->session = frag_ctx->session;
1160 udpw->udp = (char *) &udpw[1];
1162 udpw->msg_size = msg_len;
1163 udpw->cont = &send_next_fragment;
1164 udpw->cont_cls = udpw;
1165 udpw->timeout = frag_ctx->timeout;
1166 udpw->frag_ctx = frag_ctx;
1167 memcpy (udpw->udp, msg, msg_len);
1168 enqueue (plugin, udpw);
1169 schedule_select (plugin);
1174 * Function that can be used by the transport service to transmit
1175 * a message using the plugin. Note that in the case of a
1176 * peer disconnecting, the continuation MUST be called
1177 * prior to the disconnect notification itself. This function
1178 * will be called with this peer's HELLO message to initiate
1179 * a fresh connection to another peer.
1181 * @param cls closure
1182 * @param s which session must be used
1183 * @param msgbuf the message to transmit
1184 * @param msgbuf_size number of bytes in 'msgbuf'
1185 * @param priority how important is the message (most plugins will
1186 * ignore message priority and just FIFO)
1187 * @param to how long to wait at most for the transmission (does not
1188 * require plugins to discard the message after the timeout,
1189 * just advisory for the desired delay; most plugins will ignore
1191 * @param cont continuation to call once the message has
1192 * been transmitted (or if the transport is ready
1193 * for the next transmission call; or if the
1194 * peer disconnected...); can be NULL
1195 * @param cont_cls closure for cont
1196 * @return number of bytes used (on the physical network, with overheads);
1197 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1198 * and does NOT mean that the message was not transmitted (DV)
1201 udp_plugin_send (void *cls,
1203 const char *msgbuf, size_t msgbuf_size,
1204 unsigned int priority,
1205 struct GNUNET_TIME_Relative to,
1206 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1208 struct Plugin *plugin = cls;
1209 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
1210 struct UDPMessageWrapper * udpw;
1211 struct UDPMessage *udp;
1213 GNUNET_assert (plugin != NULL);
1214 GNUNET_assert (s != NULL);
1216 if ((s->addrlen == sizeof (struct sockaddr_in6)) && (plugin->sockv6 == NULL))
1217 return GNUNET_SYSERR;
1218 if ((s->addrlen == sizeof (struct sockaddr_in)) && (plugin->sockv4 == NULL))
1219 return GNUNET_SYSERR;
1220 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1223 return GNUNET_SYSERR;
1225 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
1228 return GNUNET_SYSERR;
1230 LOG (GNUNET_ERROR_TYPE_DEBUG,
1231 "UDP transmits %u-byte message to `%s' using address `%s'\n",
1233 GNUNET_i2s (&s->target),
1234 GNUNET_a2s(s->sock_addr, s->addrlen));
1237 udp = (struct UDPMessage *) mbuf;
1238 udp->header.size = htons (mlen);
1239 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
1240 udp->reserved = htonl (0);
1241 udp->sender = *plugin->env->my_identity;
1243 reschedule_session_timeout(s);
1244 if (mlen <= UDP_MTU)
1246 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
1248 udpw->udp = (char *) &udpw[1];
1249 udpw->msg_size = mlen;
1250 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1252 udpw->cont_cls = cont_cls;
1253 udpw->frag_ctx = NULL;
1254 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
1255 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
1256 enqueue (plugin, udpw);
1260 LOG (GNUNET_ERROR_TYPE_DEBUG,
1261 "UDP has to fragment message\n");
1262 if (s->frag_ctx != NULL)
1263 return GNUNET_SYSERR;
1264 memcpy (&udp[1], msgbuf, msgbuf_size);
1265 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
1267 frag_ctx->plugin = plugin;
1268 frag_ctx->session = s;
1269 frag_ctx->cont = cont;
1270 frag_ctx->cont_cls = cont_cls;
1271 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
1272 frag_ctx->bytes_to_send = mlen;
1273 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
1276 s->last_expected_delay,
1281 s->frag_ctx = frag_ctx;
1283 schedule_select (plugin);
1289 * Our external IP address/port mapping has changed.
1291 * @param cls closure, the 'struct LocalAddrList'
1292 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
1293 * the previous (now invalid) one
1294 * @param addr either the previous or the new public IP address
1295 * @param addrlen actual lenght of the address
1298 udp_nat_port_map_callback (void *cls, int add_remove,
1299 const struct sockaddr *addr, socklen_t addrlen)
1301 struct Plugin *plugin = cls;
1302 struct IPv4UdpAddress u4;
1303 struct IPv6UdpAddress u6;
1307 /* convert 'addr' to our internal format */
1308 switch (addr->sa_family)
1311 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
1312 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
1313 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
1318 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
1319 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
1320 sizeof (struct in6_addr));
1321 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
1329 /* modify our published address list */
1330 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "udp");
1336 * Message tokenizer has broken up an incomming message. Pass it on
1339 * @param cls the 'struct Plugin'
1340 * @param client the 'struct SourceInformation'
1341 * @param hdr the actual message
1344 process_inbound_tokenized_messages (void *cls, void *client,
1345 const struct GNUNET_MessageHeader *hdr)
1347 struct Plugin *plugin = cls;
1348 struct SourceInformation *si = client;
1349 struct GNUNET_ATS_Information ats[2];
1350 struct GNUNET_TIME_Relative delay;
1352 GNUNET_assert (si->session != NULL);
1353 if (GNUNET_YES == si->session->in_destroy)
1356 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1357 ats[0].value = htonl (1);
1358 ats[1] = si->session->ats;
1359 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1360 delay = plugin->env->receive (plugin->env->cls,
1363 (const struct GNUNET_ATS_Information *) &ats, 2,
1367 si->session->flow_delay_for_other_peer = delay;
1368 reschedule_session_timeout(si->session);
1374 * We've received a UDP Message. Process it (pass contents to main service).
1376 * @param plugin plugin context
1377 * @param msg the message
1378 * @param sender_addr sender address
1379 * @param sender_addr_len number of bytes in sender_addr
1382 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1383 const struct sockaddr *sender_addr,
1384 socklen_t sender_addr_len)
1386 struct SourceInformation si;
1388 struct IPv4UdpAddress u4;
1389 struct IPv6UdpAddress u6;
1393 if (0 != ntohl (msg->reserved))
1395 GNUNET_break_op (0);
1398 if (ntohs (msg->header.size) <
1399 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1401 GNUNET_break_op (0);
1405 /* convert address */
1406 switch (sender_addr->sa_family)
1409 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1410 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1411 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1416 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1417 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1418 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "Received message with %u bytes from peer `%s' at `%s'\n",
1428 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1429 GNUNET_a2s (sender_addr, sender_addr_len));
1431 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1432 s = udp_plugin_get_session(plugin, address);
1433 GNUNET_free (address);
1435 /* iterate over all embedded messages */
1437 si.sender = msg->sender;
1441 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1442 ntohs (msg->header.size) -
1443 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1445 if ( (0 == s->rc) && (GNUNET_YES == s->in_destroy))
1451 * Scan the heap for a receive context with the given address.
1453 * @param cls the 'struct FindReceiveContext'
1454 * @param node internal node of the heap
1455 * @param element value stored at the node (a 'struct ReceiveContext')
1456 * @param cost cost associated with the node
1457 * @return GNUNET_YES if we should continue to iterate,
1461 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1462 void *element, GNUNET_CONTAINER_HeapCostType cost)
1464 struct FindReceiveContext *frc = cls;
1465 struct DefragContext *e = element;
1467 if ((frc->addr_len == e->addr_len) &&
1468 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1478 * Process a defragmented message.
1480 * @param cls the 'struct ReceiveContext'
1481 * @param msg the message
1484 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1486 struct DefragContext *rc = cls;
1488 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1493 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1498 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1499 rc->src_addr, rc->addr_len);
1503 struct LookupContext
1505 const struct sockaddr * addr;
1507 struct Session *res;
1514 lookup_session_by_addr_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1516 struct LookupContext *l_ctx = cls;
1517 struct Session * s = value;
1519 if ((s->addrlen == l_ctx->addrlen) &&
1520 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1530 * Transmit an acknowledgement.
1532 * @param cls the 'struct ReceiveContext'
1533 * @param id message ID (unused)
1534 * @param msg ack to transmit
1537 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1539 struct DefragContext *rc = cls;
1540 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1541 struct UDP_ACK_Message *udp_ack;
1543 struct UDPMessageWrapper *udpw;
1546 struct LookupContext l_ctx;
1547 l_ctx.addr = rc->src_addr;
1548 l_ctx.addrlen = rc->addr_len;
1550 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1551 &lookup_session_by_addr_it,
1558 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1559 delay = s->flow_delay_for_other_peer.rel_value;
1561 LOG (GNUNET_ERROR_TYPE_DEBUG,
1562 "Sending ACK to `%s' including delay of %u ms\n",
1563 GNUNET_a2s (rc->src_addr,
1564 (rc->src_addr->sa_family ==
1565 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1568 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1569 udpw->msg_size = msize;
1571 udpw->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1572 udpw->udp = (char *)&udpw[1];
1573 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1574 udp_ack->header.size = htons ((uint16_t) msize);
1575 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1576 udp_ack->delay = htonl (delay);
1577 udp_ack->sender = *rc->plugin->env->my_identity;
1578 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1580 enqueue (rc->plugin, udpw);
1585 read_process_msg (struct Plugin *plugin,
1586 const struct GNUNET_MessageHeader *msg,
1590 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1592 GNUNET_break_op (0);
1595 process_udp_message (plugin, (const struct UDPMessage *) msg,
1596 (const struct sockaddr *) addr, fromlen);
1601 read_process_ack (struct Plugin *plugin,
1602 const struct GNUNET_MessageHeader *msg,
1606 const struct GNUNET_MessageHeader *ack;
1607 const struct UDP_ACK_Message *udp_ack;
1608 struct LookupContext l_ctx;
1610 struct GNUNET_TIME_Relative flow_delay;
1612 if (ntohs (msg->size) <
1613 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1615 GNUNET_break_op (0);
1618 udp_ack = (const struct UDP_ACK_Message *) msg;
1619 l_ctx.addr = (const struct sockaddr *) addr;
1620 l_ctx.addrlen = fromlen;
1622 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1623 &lookup_session_by_addr_it,
1627 if ((s == NULL) || (s->frag_ctx == NULL))
1630 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1631 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1632 flow_delay.rel_value);
1633 s->flow_delay_from_other_peer =
1634 GNUNET_TIME_relative_to_absolute (flow_delay);
1636 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1637 if (ntohs (ack->size) !=
1638 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1640 GNUNET_break_op (0);
1644 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1646 LOG (GNUNET_ERROR_TYPE_DEBUG,
1647 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1648 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1649 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1653 LOG (GNUNET_ERROR_TYPE_DEBUG,
1654 "FULL MESSAGE ACKed\n",
1655 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1656 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1657 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1659 struct UDPMessageWrapper * udpw;
1660 struct UDPMessageWrapper * tmp;
1661 if (s->addrlen == sizeof (struct sockaddr_in6))
1663 udpw = plugin->ipv6_queue_head;
1664 while (NULL != udpw)
1667 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1669 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1675 if (s->addrlen == sizeof (struct sockaddr_in))
1677 udpw = plugin->ipv4_queue_head;
1681 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1683 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1690 if (s->frag_ctx->cont != NULL)
1692 LOG (GNUNET_ERROR_TYPE_DEBUG,
1693 "Calling continuation for fragmented message to `%s' with result %s\n",
1694 GNUNET_i2s (&s->target), "OK");
1695 s->frag_ctx->cont (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1698 GNUNET_free (s->frag_ctx);
1704 read_process_fragment (struct Plugin *plugin,
1705 const struct GNUNET_MessageHeader *msg,
1709 struct DefragContext *d_ctx;
1710 struct GNUNET_TIME_Absolute now;
1711 struct FindReceiveContext frc;
1714 frc.addr = (const struct sockaddr *) addr;
1715 frc.addr_len = fromlen;
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1718 (unsigned int) ntohs (msg->size),
1719 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1720 /* Lookup existing receive context for this address */
1721 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1722 &find_receive_context,
1724 now = GNUNET_TIME_absolute_get ();
1729 /* Create a new defragmentation context */
1730 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1731 memcpy (&d_ctx[1], addr, fromlen);
1732 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1733 d_ctx->addr_len = fromlen;
1734 d_ctx->plugin = plugin;
1736 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1737 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1738 &fragment_msg_proc, &ack_proc);
1740 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1741 (GNUNET_CONTAINER_HeapCostType)
1743 LOG (GNUNET_ERROR_TYPE_DEBUG,
1744 "Created new defragmentation context for %u-byte fragment from `%s'\n",
1745 (unsigned int) ntohs (msg->size),
1746 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1750 LOG (GNUNET_ERROR_TYPE_DEBUG,
1751 "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1752 (unsigned int) ntohs (msg->size),
1753 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1756 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1758 /* keep this 'rc' from expiring */
1759 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1760 (GNUNET_CONTAINER_HeapCostType)
1763 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1764 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1766 /* remove 'rc' that was inactive the longest */
1767 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1768 GNUNET_assert (NULL != d_ctx);
1769 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1770 GNUNET_free (d_ctx);
1776 * Read and process a message from the given socket.
1778 * @param plugin the overall plugin
1779 * @param rsock socket to read from
1782 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1786 char buf[65536] GNUNET_ALIGN;
1788 const struct GNUNET_MessageHeader *msg;
1790 fromlen = sizeof (addr);
1791 memset (&addr, 0, sizeof (addr));
1792 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1793 (struct sockaddr *) &addr, &fromlen);
1795 /* On SOCK_DGRAM UDP sockets recvfrom might fail with a
1796 * WSAECONNRESET error to indicate that previous sendto() (???)
1797 * on this socket has failed.
1799 if ( (-1 == size) && (ECONNRESET == errno) )
1802 if ( (-1 == size) || (size < sizeof (struct GNUNET_MessageHeader)))
1804 GNUNET_break_op (0);
1807 msg = (const struct GNUNET_MessageHeader *) buf;
1809 LOG (GNUNET_ERROR_TYPE_DEBUG,
1810 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1811 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1813 if (size != ntohs (msg->size))
1815 GNUNET_break_op (0);
1819 switch (ntohs (msg->type))
1821 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1822 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1825 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1826 read_process_msg (plugin, msg, addr, fromlen);
1829 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1830 read_process_ack (plugin, msg, addr, fromlen);
1833 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1834 read_process_fragment (plugin, msg, addr, fromlen);
1838 GNUNET_break_op (0);
1845 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
1849 struct GNUNET_TIME_Absolute max;
1850 struct UDPMessageWrapper *udpw = NULL;
1851 static int network_down_error;
1853 if (sock == plugin->sockv4)
1855 udpw = plugin->ipv4_queue_head;
1857 else if (sock == plugin->sockv6)
1859 udpw = plugin->ipv6_queue_head;
1867 const struct sockaddr * sa = udpw->session->sock_addr;
1868 slen = udpw->session->addrlen;
1870 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1872 while (udpw != NULL)
1874 if (max.abs_value != udpw->timeout.abs_value)
1876 /* Message timed out */
1877 call_continuation(udpw, GNUNET_SYSERR);
1878 if (udpw->frag_ctx != NULL)
1880 LOG (GNUNET_ERROR_TYPE_DEBUG,
1881 "Fragmented message for peer `%s' with size %u timed out\n",
1882 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1883 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1884 GNUNET_free (udpw->frag_ctx);
1885 udpw->session->frag_ctx = NULL;
1889 LOG (GNUNET_ERROR_TYPE_DEBUG,
1890 "Message for peer `%s' with size %u timed out\n",
1891 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1894 if (sock == plugin->sockv4)
1896 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1898 udpw = plugin->ipv4_queue_head;
1900 else if (sock == plugin->sockv6)
1902 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1904 udpw = plugin->ipv6_queue_head;
1909 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1910 if (delta.rel_value == 0)
1912 /* this message is not delayed */
1913 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 "Message for peer `%s' (%u bytes) is not delayed \n",
1915 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1920 /* this message is delayed, try next */
1921 LOG (GNUNET_ERROR_TYPE_DEBUG,
1922 "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1923 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1932 /* No message left */
1936 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
1938 if (GNUNET_SYSERR == sent)
1940 const struct GNUNET_ATS_Information type = plugin->env->get_address_type
1941 (plugin->env->cls,sa, slen);
1943 if (((GNUNET_ATS_NET_LAN == ntohl(type.value)) || (GNUNET_ATS_NET_WAN == ntohl(type.value))) &&
1944 ((ENETUNREACH == errno) || (ENETDOWN == errno)))
1946 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in)))
1948 /* IPv4: "Network unreachable" or "Network down"
1950 * This indicates we do not have connectivity
1952 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1953 _("UDP could not transmit message to `%s': "
1954 "Network seems down, please check your network configuration\n"),
1955 GNUNET_a2s (sa, slen));
1957 if ((network_down_error == GNUNET_NO) && (slen == sizeof (struct sockaddr_in6)))
1959 /* IPv6: "Network unreachable" or "Network down"
1961 * This indicates that this system is IPv6 enabled, but does not
1962 * have a valid global IPv6 address assigned or we do not have
1966 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1967 _("UDP could not transmit message to `%s': "
1968 "Please check your network configuration and disable IPv6 if your "
1969 "connection does not have a global IPv6 address\n"),
1970 GNUNET_a2s (sa, slen));
1975 LOG (GNUNET_ERROR_TYPE_WARNING,
1976 "UDP could not transmit %u-byte message to `%s': `%s'\n",
1977 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen),
1980 call_continuation(udpw, GNUNET_SYSERR);
1984 LOG (GNUNET_ERROR_TYPE_DEBUG,
1985 "UDP transmitted %u-byte message to `%s' (%d: %s)\n",
1986 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1987 (sent < 0) ? STRERROR (errno) : "ok");
1988 call_continuation(udpw, GNUNET_OK);
1989 network_down_error = GNUNET_NO;
1992 if (sock == plugin->sockv4)
1993 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1994 else if (sock == plugin->sockv6)
1995 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2004 * We have been notified that our readset has something to read. We don't
2005 * know which socket needs to be read, so we have to check each one
2006 * Then reschedule this function to be called again once more is available.
2008 * @param cls the plugin handle
2009 * @param tc the scheduling context (for rescheduling this function again)
2012 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2014 struct Plugin *plugin = cls;
2016 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2017 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2019 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2020 (NULL != plugin->sockv4) &&
2021 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)) )
2022 udp_select_read (plugin, plugin->sockv4);
2023 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2024 (NULL != plugin->sockv4) &&
2025 (NULL != plugin->ipv4_queue_head) &&
2026 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)) )
2027 udp_select_send (plugin, plugin->sockv4);
2028 schedule_select (plugin);
2033 * We have been notified that our readset has something to read. We don't
2034 * know which socket needs to be read, so we have to check each one
2035 * Then reschedule this function to be called again once more is available.
2037 * @param cls the plugin handle
2038 * @param tc the scheduling context (for rescheduling this function again)
2041 udp_plugin_select_v6 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2043 struct Plugin *plugin = cls;
2045 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2046 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2048 if ( ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0) &&
2049 (NULL != plugin->sockv6) &&
2050 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)) )
2051 udp_select_read (plugin, plugin->sockv6);
2052 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
2053 (NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
2054 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)) )
2055 udp_select_send (plugin, plugin->sockv6);
2056 schedule_select (plugin);
2061 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
2064 int sockets_created = 0;
2065 struct sockaddr *serverAddr;
2066 struct sockaddr *addrs[2];
2067 socklen_t addrlens[2];
2070 /* Create IPv6 socket */
2071 if (plugin->enable_ipv6 == GNUNET_YES)
2073 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
2074 if (NULL == plugin->sockv6)
2076 LOG (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
2077 plugin->enable_ipv6 = GNUNET_NO;
2081 #if HAVE_SOCKADDR_IN_SIN_LEN
2082 serverAddrv6->sin6_len = sizeof (serverAddrv6);
2084 serverAddrv6->sin6_family = AF_INET6;
2085 serverAddrv6->sin6_addr = in6addr_any;
2086 serverAddrv6->sin6_port = htons (plugin->port);
2087 addrlen = sizeof (struct sockaddr_in6);
2088 serverAddr = (struct sockaddr *) serverAddrv6;
2089 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
2090 ntohs (serverAddrv6->sin6_port));
2092 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
2095 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2096 LOG (GNUNET_ERROR_TYPE_DEBUG,
2097 "IPv6 Binding failed, trying new port %d\n",
2098 ntohs (serverAddrv6->sin6_port));
2102 GNUNET_NETWORK_socket_close (plugin->sockv6);
2103 plugin->sockv6 = NULL;
2107 if (plugin->sockv6 != NULL)
2109 LOG (GNUNET_ERROR_TYPE_DEBUG,
2110 "IPv6 socket created on port %d\n",
2111 ntohs (serverAddrv6->sin6_port));
2112 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
2113 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
2119 /* Create IPv4 socket */
2120 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
2121 if (NULL == plugin->sockv4)
2123 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
2127 #if HAVE_SOCKADDR_IN_SIN_LEN
2128 serverAddrv4->sin_len = sizeof (serverAddrv4);
2130 serverAddrv4->sin_family = AF_INET;
2131 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
2132 serverAddrv4->sin_port = htons (plugin->port);
2133 addrlen = sizeof (struct sockaddr_in);
2134 serverAddr = (struct sockaddr *) serverAddrv4;
2136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
2137 ntohs (serverAddrv4->sin_port));
2139 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
2142 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
2143 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
2144 ntohs (serverAddrv4->sin_port));
2148 GNUNET_NETWORK_socket_close (plugin->sockv4);
2149 plugin->sockv4 = NULL;
2153 if (plugin->sockv4 != NULL)
2155 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
2156 addrlens[sockets_created] = sizeof (struct sockaddr_in);
2161 /* Create file descriptors */
2162 plugin->rs_v4 = GNUNET_NETWORK_fdset_create ();
2163 plugin->ws_v4 = GNUNET_NETWORK_fdset_create ();
2164 GNUNET_NETWORK_fdset_zero (plugin->rs_v4);
2165 GNUNET_NETWORK_fdset_zero (plugin->ws_v4);
2166 if (NULL != plugin->sockv4)
2168 GNUNET_NETWORK_fdset_set (plugin->rs_v4, plugin->sockv4);
2169 GNUNET_NETWORK_fdset_set (plugin->ws_v4, plugin->sockv4);
2172 if (0 == sockets_created)
2173 LOG (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
2174 if (plugin->enable_ipv6 == GNUNET_YES)
2176 plugin->rs_v6 = GNUNET_NETWORK_fdset_create ();
2177 plugin->ws_v6 = GNUNET_NETWORK_fdset_create ();
2178 GNUNET_NETWORK_fdset_zero (plugin->rs_v6);
2179 GNUNET_NETWORK_fdset_zero (plugin->ws_v6);
2180 if (NULL != plugin->sockv6)
2182 GNUNET_NETWORK_fdset_set (plugin->rs_v6, plugin->sockv6);
2183 GNUNET_NETWORK_fdset_set (plugin->ws_v6, plugin->sockv6);
2186 schedule_select (plugin);
2187 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2188 GNUNET_NO, plugin->port,
2190 (const struct sockaddr **) addrs, addrlens,
2191 &udp_nat_port_map_callback, NULL, plugin);
2193 return sockets_created;
2198 * The exported method. Makes the core api available via a global and
2199 * returns the udp transport API.
2201 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2202 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2205 libgnunet_plugin_transport_udp_init (void *cls)
2207 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2208 struct GNUNET_TRANSPORT_PluginFunctions *api;
2210 unsigned long long port;
2211 unsigned long long aport;
2212 unsigned long long broadcast;
2213 unsigned long long udp_max_bps;
2214 unsigned long long enable_v6;
2215 char * bind4_address;
2216 char * bind6_address;
2217 char * fancy_interval;
2218 struct GNUNET_TIME_Relative interval;
2219 struct sockaddr_in serverAddrv4;
2220 struct sockaddr_in6 serverAddrv6;
2223 if (NULL == env->receive)
2225 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2226 initialze the plugin or the API */
2227 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2229 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2230 api->address_to_string = &udp_address_to_string;
2231 api->string_to_address = &udp_string_to_address;
2235 GNUNET_assert( NULL != env->stats);
2237 /* Get port number */
2239 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
2243 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2244 "ADVERTISED_PORT", &aport))
2248 LOG (GNUNET_ERROR_TYPE_WARNING,
2249 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
2256 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
2259 enable_v6 = GNUNET_NO;
2262 enable_v6 = GNUNET_YES;
2265 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
2266 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
2269 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2270 "BINDTO", &bind4_address))
2272 LOG (GNUNET_ERROR_TYPE_DEBUG,
2273 "Binding udp plugin to specific address: `%s'\n",
2275 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
2277 GNUNET_free (bind4_address);
2283 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2284 "BINDTO6", &bind6_address))
2286 LOG (GNUNET_ERROR_TYPE_DEBUG,
2287 "Binding udp plugin to specific address: `%s'\n",
2290 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
2292 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
2294 GNUNET_free_non_null (bind4_address);
2295 GNUNET_free (bind6_address);
2300 /* Enable neighbour discovery */
2301 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
2303 if (broadcast == GNUNET_SYSERR)
2304 broadcast = GNUNET_NO;
2306 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
2307 "BROADCAST_INTERVAL", &fancy_interval))
2309 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
2313 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_time_to_relative(fancy_interval, &interval))
2315 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
2317 GNUNET_free (fancy_interval);
2320 /* Maximum datarate */
2321 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
2322 "MAX_BPS", &udp_max_bps))
2324 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
2327 p = GNUNET_malloc (sizeof (struct Plugin));
2328 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2330 GNUNET_BANDWIDTH_tracker_init (&p->tracker,
2331 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
2332 p->sessions = GNUNET_CONTAINER_multihashmap_create (10);
2333 p->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
2334 p->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, p);
2337 p->broadcast_interval = interval;
2338 p->enable_ipv6 = enable_v6;
2345 api->disconnect = &udp_disconnect;
2346 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
2347 api->address_to_string = &udp_address_to_string;
2348 api->string_to_address = &udp_string_to_address;
2349 api->check_address = &udp_plugin_check_address;
2350 api->get_session = &udp_plugin_get_session;
2351 api->send = &udp_plugin_send;
2353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
2354 res = setup_sockets (p, &serverAddrv6, &serverAddrv4);
2355 if ((res == 0) || ((p->sockv4 == NULL) && (p->sockv6 == NULL)))
2357 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
2363 if (broadcast == GNUNET_YES)
2365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
2366 setup_broadcast (p, &serverAddrv6, &serverAddrv4);
2369 GNUNET_free_non_null (bind4_address);
2370 GNUNET_free_non_null (bind6_address);
2376 heap_cleanup_iterator (void *cls,
2377 struct GNUNET_CONTAINER_HeapNode *
2378 node, void *element,
2379 GNUNET_CONTAINER_HeapCostType
2382 struct DefragContext * d_ctx = element;
2384 GNUNET_CONTAINER_heap_remove_node (node);
2385 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
2386 GNUNET_free (d_ctx);
2393 * The exported method. Makes the core api available via a global and
2394 * returns the udp transport API.
2396 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2400 libgnunet_plugin_transport_udp_done (void *cls)
2402 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2403 struct Plugin *plugin = api->cls;
2411 stop_broadcast (plugin);
2412 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2414 GNUNET_SCHEDULER_cancel (plugin->select_task);
2415 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2417 if (plugin->select_task_v6 != GNUNET_SCHEDULER_NO_TASK)
2419 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2420 plugin->select_task_v6 = GNUNET_SCHEDULER_NO_TASK;
2423 /* Closing sockets */
2424 if (plugin->sockv4 != NULL)
2426 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2427 plugin->sockv4 = NULL;
2429 GNUNET_NETWORK_fdset_destroy (plugin->rs_v4);
2430 GNUNET_NETWORK_fdset_destroy (plugin->ws_v4);
2432 if (plugin->sockv6 != NULL)
2434 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2435 plugin->sockv6 = NULL;
2437 GNUNET_NETWORK_fdset_destroy (plugin->rs_v6);
2438 GNUNET_NETWORK_fdset_destroy (plugin->ws_v6);
2441 GNUNET_NAT_unregister (plugin->nat);
2443 if (plugin->defrag_ctxs != NULL)
2445 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2446 heap_cleanup_iterator, NULL);
2447 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2448 plugin->defrag_ctxs = NULL;
2450 if (plugin->mst != NULL)
2452 GNUNET_SERVER_mst_destroy(plugin->mst);
2456 /* Clean up leftover messages */
2457 struct UDPMessageWrapper * udpw;
2458 udpw = plugin->ipv4_queue_head;
2459 while (udpw != NULL)
2461 struct UDPMessageWrapper *tmp = udpw->next;
2462 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
2463 call_continuation(udpw, GNUNET_SYSERR);
2467 udpw = plugin->ipv6_queue_head;
2468 while (udpw != NULL)
2470 struct UDPMessageWrapper *tmp = udpw->next;
2471 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2472 call_continuation(udpw, GNUNET_SYSERR);
2477 /* Clean up sessions */
2478 LOG (GNUNET_ERROR_TYPE_DEBUG,
2479 "Cleaning up sessions\n");
2480 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2481 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2484 GNUNET_free (plugin);
2490 /* end of plugin_transport_udp.c */