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.
91 * Which peer is this session for?
93 struct GNUNET_PeerIdentity target;
96 * Address of the other peer
98 const struct sockaddr *sock_addr;
103 * Desired delay for next sending we send to other peer
105 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
108 * Desired delay for next sending we received from other peer
110 struct GNUNET_TIME_Absolute flow_delay_from_other_peer;
113 * expected delay for ACKs
115 struct GNUNET_TIME_Relative last_expected_delay;
118 struct GNUNET_ATS_Information ats;
120 struct FragmentationContext * frag_ctx;
124 struct SessionCompareContext
127 const struct GNUNET_HELLO_Address *addr;
132 * Closure for 'process_inbound_tokenized_messages'
134 struct SourceInformation
139 struct GNUNET_PeerIdentity sender;
147 * Number of bytes in source address.
151 struct Session *session;
156 * Closure for 'find_receive_context'.
158 struct FindReceiveContext
161 * Where to store the result.
163 struct DefragContext *rc;
168 const struct sockaddr *addr;
171 * Number of bytes in 'addr'.
175 struct Session *session;
181 * Data structure to track defragmentation contexts based
182 * on the source of the UDP traffic.
188 * Defragmentation context.
190 struct GNUNET_DEFRAGMENT_Context *defrag;
193 * Source address this receive context is for (allocated at the
194 * end of the struct).
196 const struct sockaddr *src_addr;
199 * Reference to master plugin struct.
201 struct Plugin *plugin;
204 * Node in the defrag heap.
206 struct GNUNET_CONTAINER_HeapNode *hnode;
209 * Length of 'src_addr'
217 * Closure for 'process_inbound_tokenized_messages'
219 struct FragmentationContext
221 struct FragmentationContext * next;
222 struct FragmentationContext * prev;
224 struct Plugin * plugin;
225 struct GNUNET_FRAGMENT_Context * frag;
226 struct Session * session;
228 struct GNUNET_TIME_Absolute timeout;
232 * Function to call upon completion of the transmission.
234 GNUNET_TRANSPORT_TransmitContinuation cont;
237 * Closure for 'cont'.
241 size_t bytes_to_send;
245 struct UDPMessageWrapper
247 struct Session *session;
248 struct UDPMessageWrapper *prev;
249 struct UDPMessageWrapper *next;
253 struct GNUNET_TIME_Absolute timeout;
256 * Function to call upon completion of the transmission.
258 GNUNET_TRANSPORT_TransmitContinuation cont;
261 * Closure for 'cont'.
265 struct FragmentationContext *frag_ctx;
271 * UDP ACK Message-Packet header (after defragmentation).
273 struct UDP_ACK_Message
278 struct GNUNET_MessageHeader header;
281 * Desired delay for flow control
286 * What is the identity of the sender
288 struct GNUNET_PeerIdentity sender;
295 * Function called for a quick conversion of the binary address to
296 * a numeric address. Note that the caller must not free the
297 * address and that the next call to this function is allowed
298 * to override the address again.
301 * @param addr binary address
302 * @param addrlen length of the address
303 * @return string representing the same address
306 udp_address_to_string (void *cls, const void *addr, size_t addrlen)
308 static char rbuf[INET6_ADDRSTRLEN + 10];
309 char buf[INET6_ADDRSTRLEN];
313 const struct IPv4UdpAddress *t4;
314 const struct IPv6UdpAddress *t6;
318 if (addrlen == sizeof (struct IPv6UdpAddress))
322 port = ntohs (t6->u6_port);
323 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
326 else if (addrlen == sizeof (struct IPv4UdpAddress))
330 port = ntohs (t4->u4_port);
331 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
339 inet_ntop (af, sb, buf, INET6_ADDRSTRLEN);
340 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
347 * Append our port and forward the result.
349 * @param cls a 'struct PrettyPrinterContext'
350 * @param hostname result from DNS resolver
353 append_port (void *cls, const char *hostname)
355 struct PrettyPrinterContext *ppc = cls;
358 if (hostname == NULL)
360 ppc->asc (ppc->asc_cls, NULL);
364 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
365 ppc->asc (ppc->asc_cls, ret);
371 * Convert the transports address to a nice, human-readable
375 * @param type name of the transport that generated the address
376 * @param addr one of the addresses of the host, NULL for the last address
377 * the specific address format depends on the transport
378 * @param addrlen length of the address
379 * @param numeric should (IP) addresses be displayed in numeric form?
380 * @param timeout after how long should we give up?
381 * @param asc function to call on each string
382 * @param asc_cls closure for asc
385 udp_plugin_address_pretty_printer (void *cls, const char *type,
386 const void *addr, size_t addrlen,
388 struct GNUNET_TIME_Relative timeout,
389 GNUNET_TRANSPORT_AddressStringCallback asc,
392 struct PrettyPrinterContext *ppc;
395 struct sockaddr_in a4;
396 struct sockaddr_in6 a6;
397 const struct IPv4UdpAddress *u4;
398 const struct IPv6UdpAddress *u6;
401 if (addrlen == sizeof (struct IPv6UdpAddress))
404 memset (&a6, 0, sizeof (a6));
405 a6.sin6_family = AF_INET6;
406 #if HAVE_SOCKADDR_IN_SIN_LEN
407 a6.sin6_len = sizeof (a6);
409 a6.sin6_port = u6->u6_port;
410 memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof (struct in6_addr));
411 port = ntohs (u6->u6_port);
415 else if (addrlen == sizeof (struct IPv4UdpAddress))
418 memset (&a4, 0, sizeof (a4));
419 a4.sin_family = AF_INET;
420 #if HAVE_SOCKADDR_IN_SIN_LEN
421 a4.sin_len = sizeof (a4);
423 a4.sin_port = u4->u4_port;
424 a4.sin_addr.s_addr = u4->ipv4_addr;
425 port = ntohs (u4->u4_port);
431 /* invalid address */
436 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
438 ppc->asc_cls = asc_cls;
440 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
445 * Check if the given port is plausible (must be either our listen
446 * port or our advertised port). If it is neither, we return
449 * @param plugin global variables
450 * @param in_port port number to check
451 * @return GNUNET_OK if port is either open_port or adv_port
454 check_port (struct Plugin *plugin, uint16_t in_port)
456 if ((in_port == plugin->port) || (in_port == plugin->aport))
458 return GNUNET_SYSERR;
464 * Function that will be called to check if a binary address for this
465 * plugin is well-formed and corresponds to an address for THIS peer
466 * (as per our configuration). Naturally, if absolutely necessary,
467 * plugins can be a bit conservative in their answer, but in general
468 * plugins should make sure that the address does not redirect
469 * traffic to a 3rd party that might try to man-in-the-middle our
472 * @param cls closure, should be our handle to the Plugin
473 * @param addr pointer to the address
474 * @param addrlen length of addr
475 * @return GNUNET_OK if this is a plausible address for this peer
476 * and transport, GNUNET_SYSERR if not
480 udp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
482 struct Plugin *plugin = cls;
483 struct IPv4UdpAddress *v4;
484 struct IPv6UdpAddress *v6;
486 if ((addrlen != sizeof (struct IPv4UdpAddress)) &&
487 (addrlen != sizeof (struct IPv6UdpAddress)))
490 return GNUNET_SYSERR;
492 if (addrlen == sizeof (struct IPv4UdpAddress))
494 v4 = (struct IPv4UdpAddress *) addr;
495 if (GNUNET_OK != check_port (plugin, ntohs (v4->u4_port)))
496 return GNUNET_SYSERR;
498 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
499 sizeof (struct in_addr)))
500 return GNUNET_SYSERR;
504 v6 = (struct IPv6UdpAddress *) addr;
505 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
508 return GNUNET_SYSERR;
510 if (GNUNET_OK != check_port (plugin, ntohs (v6->u6_port)))
511 return GNUNET_SYSERR;
513 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
514 sizeof (struct in6_addr)))
515 return GNUNET_SYSERR;
522 * Destroy a session, plugin is being unloaded.
525 * @param key hash of public key of target peer
526 * @param value a 'struct PeerSession*' to clean up
527 * @return GNUNET_OK (continue to iterate)
530 disconnect_and_free_it (void *cls, const GNUNET_HashCode * key, void *value)
532 struct Plugin *plugin = cls;
533 struct Session *s = value;
534 struct UDPMessageWrapper *udpw;
535 struct UDPMessageWrapper *next;
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "Session %p to peer `%s' address ended \n",
541 GNUNET_i2s (&s->target),
542 GNUNET_a2s (s->sock_addr, s->addrlen));
544 plugin->env->session_end (plugin->env->cls, &s->target, s);
546 if (s->frag_ctx != NULL)
548 GNUNET_FRAGMENT_context_destroy(s->frag_ctx->frag);
549 GNUNET_free (s->frag_ctx);
553 udpw = plugin->ipv4_queue_head;
557 if (udpw->session == s)
559 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
561 if (udpw->cont != NULL)
562 udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
568 udpw = plugin->ipv6_queue_head;
572 if (udpw->session == s)
574 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
576 if (udpw->cont != NULL)
577 udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
583 GNUNET_assert (GNUNET_YES ==
584 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
585 &s->target.hashPubKey,
595 * Disconnect from a remote node. Clean up session if we have one for this peer
597 * @param cls closure for this call (should be handle to Plugin)
598 * @param target the peeridentity of the peer to disconnect
599 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
602 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
604 struct Plugin *plugin = cls;
605 GNUNET_assert (plugin != NULL);
607 GNUNET_assert (target != NULL);
609 LOG (GNUNET_ERROR_TYPE_DEBUG,
610 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
612 /* Clean up sessions */
613 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
616 static struct Session *
617 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
618 const void *addr, size_t addrlen,
619 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
622 const struct IPv4UdpAddress *t4;
623 const struct IPv6UdpAddress *t6;
624 struct sockaddr_in *v4;
625 struct sockaddr_in6 *v6;
630 case sizeof (struct IPv4UdpAddress):
631 if (NULL == plugin->sockv4)
636 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
637 len = sizeof (struct sockaddr_in);
638 v4 = (struct sockaddr_in *) &s[1];
639 v4->sin_family = AF_INET;
640 #if HAVE_SOCKADDR_IN_SIN_LEN
641 v4->sin_len = sizeof (struct sockaddr_in);
643 v4->sin_port = t4->u4_port;
644 v4->sin_addr.s_addr = t4->ipv4_addr;
645 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
647 case sizeof (struct IPv6UdpAddress):
648 if (NULL == plugin->sockv6)
654 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
655 len = sizeof (struct sockaddr_in6);
656 v6 = (struct sockaddr_in6 *) &s[1];
657 v6->sin6_family = AF_INET6;
658 #if HAVE_SOCKADDR_IN_SIN_LEN
659 v6->sin6_len = sizeof (struct sockaddr_in6);
661 v6->sin6_port = t6->u6_port;
662 v6->sin6_addr = t6->ipv6_addr;
663 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
666 /* Must have a valid address to send to */
673 s->sock_addr = (const struct sockaddr *) &s[1];
674 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
675 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
676 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
681 static int session_cmp_it (void *cls,
682 const GNUNET_HashCode * key,
685 struct SessionCompareContext * cctx = cls;
686 const struct GNUNET_HELLO_Address *address = cctx->addr;
687 struct Session *s = value;
689 socklen_t s_addrlen = s->addrlen;
692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
693 udp_address_to_string (NULL, (void *) address->address, address->address_length),
694 GNUNET_a2s (s->sock_addr, s->addrlen));
697 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
698 (s_addrlen == sizeof (struct sockaddr_in)))
700 struct IPv4UdpAddress * u4 = NULL;
701 u4 = (struct IPv4UdpAddress *) address->address;
702 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
703 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
704 (u4->u4_port == s4->sin_port))
711 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
712 (s_addrlen == sizeof (struct sockaddr_in6)))
714 struct IPv6UdpAddress * u6 = NULL;
715 u6 = (struct IPv6UdpAddress *) address->address;
716 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
717 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
718 (u6->u6_port == s6->sin6_port))
731 * Creates a new outbound session the transport service will use to send data to the
734 * @param cls the plugin
735 * @param address the address
736 * @return the session or NULL of max connections exceeded
738 static struct Session *
739 udp_plugin_get_session (void *cls,
740 const struct GNUNET_HELLO_Address *address)
742 struct Session * s = NULL;
743 struct Plugin * plugin = cls;
745 GNUNET_assert (plugin != NULL);
746 GNUNET_assert (address != NULL);
748 if ((address->address == NULL) ||
749 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
750 (address->address_length != sizeof (struct IPv6UdpAddress))))
756 /* check if session already exists */
757 struct SessionCompareContext cctx;
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Looking for existing session for peer `%s' `%s' \n", GNUNET_i2s (&address->peer), udp_address_to_string(NULL, address->address, address->address_length));
763 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
764 if (cctx.res != NULL)
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
772 /* otherwise create new */
773 s = create_session (plugin,
776 address->address_length,
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Creating new session %p for peer `%s' address `%s'\n",
782 GNUNET_i2s(&address->peer),
783 udp_address_to_string(NULL,address->address,address->address_length));
785 GNUNET_assert (GNUNET_OK ==
786 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
787 &s->target.hashPubKey,
789 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
794 static void enqueue (struct Plugin *plugin, struct UDPMessageWrapper * udpw)
797 if (udpw->session->addrlen == sizeof (struct sockaddr_in))
798 GNUNET_CONTAINER_DLL_insert(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
799 if (udpw->session->addrlen == sizeof (struct sockaddr_in6))
800 GNUNET_CONTAINER_DLL_insert(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
804 * Function that is called with messages created by the fragmentation
805 * module. In the case of the 'proc' callback of the
806 * GNUNET_FRAGMENT_context_create function, this function must
807 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
809 * @param cls closure, the 'struct FragmentationContext'
810 * @param msg the message that was created
813 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
815 struct FragmentationContext *frag_ctx = cls;
816 struct Plugin *plugin = frag_ctx->plugin;
817 struct UDPMessageWrapper * udpw;
819 size_t msg_len = ntohs (msg->size);
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
825 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
826 udpw->session = frag_ctx->session;
827 udpw->udp = (char *) &udpw[1];
829 udpw->msg_size = msg_len;
830 udpw->cont = frag_ctx->cont;
831 udpw->cont_cls = frag_ctx->cont_cls;
832 udpw->timeout = frag_ctx->timeout;
833 udpw->frag_ctx = frag_ctx;
834 memcpy (udpw->udp, msg, msg_len);
836 enqueue (plugin, udpw);
842 * Function that can be used by the transport service to transmit
843 * a message using the plugin. Note that in the case of a
844 * peer disconnecting, the continuation MUST be called
845 * prior to the disconnect notification itself. This function
846 * will be called with this peer's HELLO message to initiate
847 * a fresh connection to another peer.
850 * @param s which session must be used
851 * @param msgbuf the message to transmit
852 * @param msgbuf_size number of bytes in 'msgbuf'
853 * @param priority how important is the message (most plugins will
854 * ignore message priority and just FIFO)
855 * @param to how long to wait at most for the transmission (does not
856 * require plugins to discard the message after the timeout,
857 * just advisory for the desired delay; most plugins will ignore
859 * @param cont continuation to call once the message has
860 * been transmitted (or if the transport is ready
861 * for the next transmission call; or if the
862 * peer disconnected...); can be NULL
863 * @param cont_cls closure for cont
864 * @return number of bytes used (on the physical network, with overheads);
865 * -1 on hard errors (i.e. address invalid); 0 is a legal value
866 * and does NOT mean that the message was not transmitted (DV)
869 udp_plugin_send (void *cls,
871 const char *msgbuf, size_t msgbuf_size,
872 unsigned int priority,
873 struct GNUNET_TIME_Relative to,
874 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
876 struct Plugin *plugin = cls;
877 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
879 struct UDPMessageWrapper * udpw;
880 struct UDPMessage *udp;
882 GNUNET_assert (plugin != NULL);
883 GNUNET_assert (s != NULL);
885 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
888 return GNUNET_SYSERR;
891 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
894 return GNUNET_SYSERR;
897 LOG (GNUNET_ERROR_TYPE_DEBUG,
898 "UDP transmits %u-byte message to `%s' using address `%s'\n",
900 GNUNET_i2s (&s->target),
901 GNUNET_a2s(s->sock_addr, s->addrlen));
904 udp = (struct UDPMessage *) mbuf;
905 udp->header.size = htons (mlen);
906 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
907 udp->reserved = htonl (0);
908 udp->sender = *plugin->env->my_identity;
912 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
914 udpw->udp = (char *) &udpw[1];
915 udpw->msg_size = mlen;
916 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
918 udpw->cont_cls = cont_cls;
919 udpw->frag_ctx = NULL;
921 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
922 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
924 enqueue (plugin, udpw);
928 LOG (GNUNET_ERROR_TYPE_DEBUG,
929 "UDP has to fragment message \n");
930 if (s->frag_ctx != NULL)
931 return GNUNET_SYSERR;
932 memcpy (&udp[1], msgbuf, msgbuf_size);
933 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
935 frag_ctx->plugin = plugin;
936 frag_ctx->session = s;
937 frag_ctx->cont = cont;
938 frag_ctx->cont_cls = cont_cls;
939 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
940 frag_ctx->bytes_to_send = mlen;
941 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
944 s->last_expected_delay,
949 s->frag_ctx = frag_ctx;
957 * Our external IP address/port mapping has changed.
959 * @param cls closure, the 'struct LocalAddrList'
960 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
961 * the previous (now invalid) one
962 * @param addr either the previous or the new public IP address
963 * @param addrlen actual lenght of the address
966 udp_nat_port_map_callback (void *cls, int add_remove,
967 const struct sockaddr *addr, socklen_t addrlen)
969 struct Plugin *plugin = cls;
970 struct IPv4UdpAddress u4;
971 struct IPv6UdpAddress u6;
975 /* convert 'addr' to our internal format */
976 switch (addr->sa_family)
979 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
980 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
981 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
986 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
987 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
988 sizeof (struct in6_addr));
989 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
997 /* modify our published address list */
998 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
1004 * Message tokenizer has broken up an incomming message. Pass it on
1007 * @param cls the 'struct Plugin'
1008 * @param client the 'struct SourceInformation'
1009 * @param hdr the actual message
1012 process_inbound_tokenized_messages (void *cls, void *client,
1013 const struct GNUNET_MessageHeader *hdr)
1015 struct Plugin *plugin = cls;
1016 struct SourceInformation *si = client;
1017 struct GNUNET_ATS_Information ats[2];
1018 struct GNUNET_TIME_Relative delay;
1020 GNUNET_assert (si->session != NULL);
1022 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
1023 ats[0].value = htonl (1);
1024 ats[1] = si->session->ats;
1025 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1027 delay = plugin->env->receive (plugin->env->cls,
1030 (const struct GNUNET_ATS_Information *) &ats, 2,
1034 si->session->flow_delay_for_other_peer = delay;
1039 * We've received a UDP Message. Process it (pass contents to main service).
1041 * @param plugin plugin context
1042 * @param msg the message
1043 * @param sender_addr sender address
1044 * @param sender_addr_len number of bytes in sender_addr
1047 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1048 const struct sockaddr *sender_addr,
1049 socklen_t sender_addr_len)
1051 struct SourceInformation si;
1052 struct Session * s = NULL;
1053 struct IPv4UdpAddress u4;
1054 struct IPv6UdpAddress u6;
1058 if (0 != ntohl (msg->reserved))
1060 GNUNET_break_op (0);
1063 if (ntohs (msg->header.size) <
1064 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1066 GNUNET_break_op (0);
1070 /* convert address */
1071 switch (sender_addr->sa_family)
1074 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1075 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1076 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1081 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1082 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1083 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1093 "Received message with %u bytes from peer `%s' at `%s'\n",
1094 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1095 GNUNET_a2s (sender_addr, sender_addr_len));
1098 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1099 s = udp_plugin_get_session(plugin, address);
1100 GNUNET_free (address);
1102 /* iterate over all embedded messages */
1104 si.sender = msg->sender;
1108 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1109 ntohs (msg->header.size) -
1110 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1115 * Scan the heap for a receive context with the given address.
1117 * @param cls the 'struct FindReceiveContext'
1118 * @param node internal node of the heap
1119 * @param element value stored at the node (a 'struct ReceiveContext')
1120 * @param cost cost associated with the node
1121 * @return GNUNET_YES if we should continue to iterate,
1125 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1126 void *element, GNUNET_CONTAINER_HeapCostType cost)
1128 struct FindReceiveContext *frc = cls;
1129 struct DefragContext *e = element;
1131 if ((frc->addr_len == e->addr_len) &&
1132 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1142 * Process a defragmented message.
1144 * @param cls the 'struct ReceiveContext'
1145 * @param msg the message
1148 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1150 struct DefragContext *rc = cls;
1152 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1157 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1162 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1163 rc->src_addr, rc->addr_len);
1166 struct LookupContext
1168 const struct sockaddr * addr;
1171 struct Session *res;
1175 lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1177 struct LookupContext *l_ctx = cls;
1178 struct Session * s = value;
1180 if ((s->addrlen == l_ctx->addrlen) &&
1181 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1190 * Transmit an acknowledgement.
1192 * @param cls the 'struct ReceiveContext'
1193 * @param id message ID (unused)
1194 * @param msg ack to transmit
1197 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1199 struct DefragContext *rc = cls;
1201 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1202 struct UDP_ACK_Message *udp_ack;
1204 struct UDPMessageWrapper *udpw;
1207 struct LookupContext l_ctx;
1208 l_ctx.addr = rc->src_addr;
1209 l_ctx.addrlen = rc->addr_len;
1211 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1212 &lookup_session_by_addr_it,
1216 GNUNET_assert (s != NULL);
1218 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1219 delay = s->flow_delay_for_other_peer.rel_value;
1222 LOG (GNUNET_ERROR_TYPE_DEBUG,
1223 "Sending ACK to `%s' including delay of %u ms\n",
1224 GNUNET_a2s (rc->src_addr,
1225 (rc->src_addr->sa_family ==
1226 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1230 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1232 udpw->cont_cls = NULL;
1233 udpw->frag_ctx = NULL;
1234 udpw->msg_size = msize;
1236 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1237 udpw->udp = (char *)&udpw[1];
1239 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1240 udp_ack->header.size = htons ((uint16_t) msize);
1241 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1242 udp_ack->delay = htonl (delay);
1243 udp_ack->sender = *rc->plugin->env->my_identity;
1244 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1246 enqueue (rc->plugin, udpw);
1250 static void read_process_msg (struct Plugin *plugin,
1251 const struct GNUNET_MessageHeader *msg,
1255 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1257 GNUNET_break_op (0);
1260 process_udp_message (plugin, (const struct UDPMessage *) msg,
1261 (const struct sockaddr *) addr, fromlen);
1265 static void read_process_ack (struct Plugin *plugin,
1266 const struct GNUNET_MessageHeader *msg,
1270 const struct GNUNET_MessageHeader *ack;
1271 const struct UDP_ACK_Message *udp_ack;
1272 struct LookupContext l_ctx;
1273 struct Session *s = NULL;
1274 struct GNUNET_TIME_Relative flow_delay;
1276 if (ntohs (msg->size) <
1277 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1279 GNUNET_break_op (0);
1283 udp_ack = (const struct UDP_ACK_Message *) msg;
1285 l_ctx.addr = (const struct sockaddr *) addr;
1286 l_ctx.addrlen = fromlen;
1288 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1289 &lookup_session_by_addr_it,
1293 if ((s == NULL) || (s->frag_ctx == NULL))
1296 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1297 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1298 flow_delay.rel_value);
1299 s->flow_delay_from_other_peer =
1300 GNUNET_TIME_relative_to_absolute (flow_delay);
1302 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1303 if (ntohs (ack->size) !=
1304 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1306 GNUNET_break_op (0);
1310 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1313 LOG (GNUNET_ERROR_TYPE_DEBUG,
1314 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1315 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1316 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "FULL MESSAGE ACKed\n",
1324 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1325 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1327 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1329 struct UDPMessageWrapper * udpw = NULL;
1330 if (s->addrlen == sizeof (struct sockaddr_in6))
1332 udpw = plugin->ipv6_queue_head;
1335 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1337 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1343 if (s->addrlen == sizeof (struct sockaddr_in))
1345 udpw = plugin->ipv4_queue_head;
1348 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1350 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1357 if (s->frag_ctx->cont != NULL)
1359 (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1360 GNUNET_free (s->frag_ctx);
1365 static void read_process_fragment (struct Plugin *plugin,
1366 const struct GNUNET_MessageHeader *msg,
1370 struct DefragContext *d_ctx;
1371 struct GNUNET_TIME_Absolute now;
1372 struct FindReceiveContext frc;
1376 frc.addr = (const struct sockaddr *) addr;
1377 frc.addr_len = fromlen;
1380 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1381 (unsigned int) ntohs (msg->size),
1382 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1385 /* Lookup existing receive context for this address */
1386 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1387 &find_receive_context,
1389 now = GNUNET_TIME_absolute_get ();
1394 /* Create a new defragmentation context */
1395 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1396 memcpy (&d_ctx[1], addr, fromlen);
1397 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1398 d_ctx->addr_len = fromlen;
1399 d_ctx->plugin = plugin;
1401 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1402 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1403 &fragment_msg_proc, &ack_proc);
1405 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1406 (GNUNET_CONTAINER_HeapCostType)
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "Created new defragmentation context for %u-byte fragment from `%s'\n",
1410 (unsigned int) ntohs (msg->size),
1411 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1417 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1418 (unsigned int) ntohs (msg->size),
1419 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1423 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1425 /* keep this 'rc' from expiring */
1426 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1427 (GNUNET_CONTAINER_HeapCostType)
1430 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1431 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1433 /* remove 'rc' that was inactive the longest */
1434 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1435 GNUNET_assert (NULL != d_ctx);
1436 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1437 GNUNET_free (d_ctx);
1442 * Read and process a message from the given socket.
1444 * @param plugin the overall plugin
1445 * @param rsock socket to read from
1448 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1454 const struct GNUNET_MessageHeader *msg;
1456 fromlen = sizeof (addr);
1457 memset (&addr, 0, sizeof (addr));
1458 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1459 (struct sockaddr *) &addr, &fromlen);
1461 if (size < sizeof (struct GNUNET_MessageHeader))
1463 GNUNET_break_op (0);
1466 msg = (const struct GNUNET_MessageHeader *) buf;
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1470 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1472 if (size != ntohs (msg->size))
1474 GNUNET_break_op (0);
1478 switch (ntohs (msg->type))
1480 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1481 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1484 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1485 read_process_msg (plugin, msg, addr, fromlen);
1488 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1489 read_process_ack (plugin, msg, addr, fromlen);;
1492 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1493 read_process_fragment (plugin, msg, addr, fromlen);
1497 GNUNET_break_op (0);
1503 udp_select_send (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *sock)
1507 struct GNUNET_TIME_Absolute max;
1508 struct GNUNET_TIME_Absolute ;
1510 struct UDPMessageWrapper *udpw = NULL;
1512 if (sock == plugin->sockv4)
1514 udpw = plugin->ipv4_queue_head;
1516 else if (sock == plugin->sockv6)
1518 udpw = plugin->ipv6_queue_head;
1523 const struct sockaddr * sa = udpw->session->sock_addr;
1524 slen = udpw->session->addrlen;
1526 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1528 while (udpw != NULL)
1530 if (max.abs_value != udpw->timeout.abs_value)
1532 /* Message timed out */
1534 if (udpw->cont != NULL)
1535 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1536 if (udpw->frag_ctx != NULL)
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fragmented message for peer `%s' with size %u timed out\n",
1540 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1542 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1543 GNUNET_free (udpw->frag_ctx);
1544 udpw->session->frag_ctx = NULL;
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' with size %u timed out\n",
1550 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1554 if (sock == plugin->sockv4)
1556 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1558 udpw = plugin->ipv4_queue_head;
1560 else if (sock == plugin->sockv6)
1562 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1564 udpw = plugin->ipv6_queue_head;
1569 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1570 if (delta.rel_value == 0)
1572 /* this message is not delayed */
1573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is not delayed \n",
1574 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1579 /* this message is delayed, try next */
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1581 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1591 /* No message left */
1595 sent = GNUNET_NETWORK_socket_sendto (sock, udpw->udp, udpw->msg_size, sa, slen);
1597 if (GNUNET_SYSERR == sent)
1599 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1600 LOG (GNUNET_ERROR_TYPE_DEBUG,
1601 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1602 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1603 (sent < 0) ? STRERROR (errno) : "ok");
1604 if (udpw->cont != NULL)
1605 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1607 LOG (GNUNET_ERROR_TYPE_DEBUG,
1608 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1609 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1610 (sent < 0) ? STRERROR (errno) : "ok");
1612 /* This was just a message fragment */
1613 if (udpw->frag_ctx != NULL)
1615 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1617 /* This was a complete message*/
1620 if (udpw->cont != NULL)
1621 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_OK);
1624 if (sock == plugin->sockv4)
1625 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
1626 else if (sock == plugin->sockv6)
1627 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
1635 * We have been notified that our readset has something to read. We don't
1636 * know which socket needs to be read, so we have to check each one
1637 * Then reschedule this function to be called again once more is available.
1639 * @param cls the plugin handle
1640 * @param tc the scheduling context (for rescheduling this function again)
1643 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1645 struct Plugin *plugin = cls;
1647 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1648 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1651 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1653 if ((NULL != plugin->sockv4) &&
1654 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1655 udp_select_read (plugin, plugin->sockv4);
1656 if ((NULL != plugin->sockv6) &&
1657 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1658 udp_select_read (plugin, plugin->sockv6);
1661 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1663 if ((NULL != plugin->sockv4) && (plugin->ipv4_queue_head != NULL) &&
1664 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv4)))
1666 udp_select_send (plugin, plugin->sockv4);
1668 if ((NULL != plugin->sockv6) && (plugin->ipv6_queue_head != NULL) &&
1669 (GNUNET_NETWORK_fdset_isset (tc->write_ready, plugin->sockv6)))
1671 udp_select_send (plugin, plugin->sockv6);
1675 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1676 GNUNET_SCHEDULER_NO_TASK,
1677 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1678 plugin->ws, &udp_plugin_select, plugin);
1684 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1687 int sockets_created = 0;
1688 struct sockaddr *serverAddr;
1689 struct sockaddr *addrs[2];
1690 socklen_t addrlens[2];
1693 /* Create IPv6 socket */
1694 if (plugin->enable_ipv6 == GNUNET_YES)
1696 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1697 if (NULL == plugin->sockv6)
1699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
1700 plugin->enable_ipv6 = GNUNET_NO;
1704 #if HAVE_SOCKADDR_IN_SIN_LEN
1705 serverAddrv6->sin6_len = sizeof (serverAddrv6);
1707 serverAddrv6->sin6_family = AF_INET6;
1708 serverAddrv6->sin6_addr = in6addr_any;
1709 serverAddrv6->sin6_port = htons (plugin->port);
1710 addrlen = sizeof (struct sockaddr_in6);
1711 serverAddr = (struct sockaddr *) serverAddrv6;
1713 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1714 ntohs (serverAddrv6->sin6_port));
1717 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1720 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1722 LOG (GNUNET_ERROR_TYPE_DEBUG,
1723 "IPv6 Binding failed, trying new port %d\n",
1724 ntohs (serverAddrv6->sin6_port));
1729 GNUNET_NETWORK_socket_close (plugin->sockv6);
1730 plugin->sockv6 = NULL;
1734 if (plugin->sockv6 != NULL)
1737 LOG (GNUNET_ERROR_TYPE_DEBUG,
1738 "IPv6 socket created on port %d\n",
1739 ntohs (serverAddrv6->sin6_port));
1741 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
1742 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
1748 /* Create IPv4 socket */
1749 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1750 if (NULL == plugin->sockv4)
1752 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1756 #if HAVE_SOCKADDR_IN_SIN_LEN
1757 serverAddrv4->sin_len = sizeof (serverAddrv4);
1759 serverAddrv4->sin_family = AF_INET;
1760 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
1761 serverAddrv4->sin_port = htons (plugin->port);
1762 addrlen = sizeof (struct sockaddr_in);
1763 serverAddr = (struct sockaddr *) serverAddrv4;
1766 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1767 ntohs (serverAddrv4->sin_port));
1770 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1773 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1775 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
1776 ntohs (serverAddrv4->sin_port));
1781 GNUNET_NETWORK_socket_close (plugin->sockv4);
1782 plugin->sockv4 = NULL;
1786 if (plugin->sockv4 != NULL)
1788 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
1789 addrlens[sockets_created] = sizeof (struct sockaddr_in);
1794 /* Create file descriptors */
1795 plugin->rs = GNUNET_NETWORK_fdset_create ();
1796 plugin->ws = GNUNET_NETWORK_fdset_create ();
1797 GNUNET_NETWORK_fdset_zero (plugin->rs);
1798 GNUNET_NETWORK_fdset_zero (plugin->ws);
1799 if (NULL != plugin->sockv4)
1801 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1802 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv4);
1804 if (NULL != plugin->sockv6)
1806 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1807 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv6);
1810 if (sockets_created == 0)
1811 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1813 plugin->select_task =
1814 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1815 GNUNET_SCHEDULER_NO_TASK,
1816 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1817 plugin->ws, &udp_plugin_select, plugin);
1819 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
1820 GNUNET_NO, plugin->port,
1822 (const struct sockaddr **) addrs, addrlens,
1823 &udp_nat_port_map_callback, NULL, plugin);
1825 return sockets_created;
1830 * The exported method. Makes the core api available via a global and
1831 * returns the udp transport API.
1833 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1834 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1837 libgnunet_plugin_transport_udp_init (void *cls)
1839 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1840 struct GNUNET_TRANSPORT_PluginFunctions *api;
1841 struct Plugin *plugin;
1843 unsigned long long port;
1844 unsigned long long aport;
1845 unsigned long long broadcast;
1846 unsigned long long udp_max_bps;
1847 unsigned long long enable_v6;
1848 char * bind4_address;
1849 char * bind6_address;
1850 struct GNUNET_TIME_Relative interval;
1852 struct sockaddr_in serverAddrv4;
1853 struct sockaddr_in6 serverAddrv6;
1857 /* Get port number */
1859 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1863 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1864 "ADVERTISED_PORT", &aport))
1868 LOG (GNUNET_ERROR_TYPE_WARNING,
1869 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1876 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
1879 enable_v6 = GNUNET_NO;
1882 enable_v6 = GNUNET_YES;
1886 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1887 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1890 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1891 "BINDTO", &bind4_address))
1893 LOG (GNUNET_ERROR_TYPE_DEBUG,
1894 "Binding udp plugin to specific address: `%s'\n",
1896 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
1898 GNUNET_free (bind4_address);
1904 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1905 "BINDTO6", &bind6_address))
1907 LOG (GNUNET_ERROR_TYPE_DEBUG,
1908 "Binding udp plugin to specific address: `%s'\n",
1911 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
1913 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1915 GNUNET_free_non_null (bind4_address);
1916 GNUNET_free (bind6_address);
1922 /* Enable neighbour discovery */
1923 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1925 if (broadcast == GNUNET_SYSERR)
1926 broadcast = GNUNET_NO;
1928 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1929 "BROADCAST_INTERVAL", &interval))
1931 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1934 /* Maximum datarate */
1935 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1936 "MAX_BPS", &udp_max_bps))
1938 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1941 plugin = GNUNET_malloc (sizeof (struct Plugin));
1942 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1944 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1945 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
1948 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1949 plugin->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1950 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1951 plugin->port = port;
1952 plugin->aport = aport;
1953 plugin->broadcast_interval = interval;
1954 plugin->enable_ipv6 = enable_v6;
1959 api->disconnect = &udp_disconnect;
1960 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1961 api->address_to_string = &udp_address_to_string;
1962 api->check_address = &udp_plugin_check_address;
1963 api->get_session = &udp_plugin_get_session;
1964 api->send = &udp_plugin_send;
1966 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
1967 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
1968 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
1970 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
1971 GNUNET_free (plugin);
1976 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
1977 if (broadcast == GNUNET_YES)
1978 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
1981 GNUNET_free_non_null (bind4_address);
1982 GNUNET_free_non_null (bind6_address);
1986 int heap_cleanup_iterator (void *cls,
1987 struct GNUNET_CONTAINER_HeapNode *
1988 node, void *element,
1989 GNUNET_CONTAINER_HeapCostType
1992 struct DefragContext * d_ctx = element;
1994 GNUNET_CONTAINER_heap_remove_node (node);
1995 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
1996 GNUNET_free (d_ctx);
2003 * The exported method. Makes the core api available via a global and
2004 * returns the udp transport API.
2006 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
2007 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
2010 libgnunet_plugin_transport_udp_done (void *cls)
2012 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2013 struct Plugin *plugin = api->cls;
2014 stop_broadcast (plugin);
2016 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
2018 GNUNET_SCHEDULER_cancel (plugin->select_task);
2019 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
2022 /* Closing sockets */
2023 if (plugin->sockv4 != NULL)
2025 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
2026 plugin->sockv4 = NULL;
2028 if (plugin->sockv6 != NULL)
2030 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
2031 plugin->sockv6 = NULL;
2033 GNUNET_NETWORK_fdset_destroy (plugin->rs);
2034 GNUNET_NETWORK_fdset_destroy (plugin->ws);
2035 GNUNET_NAT_unregister (plugin->nat);
2037 if (plugin->defrag_ctxs != NULL)
2039 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
2040 heap_cleanup_iterator, NULL);
2041 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
2042 plugin->defrag_ctxs = NULL;
2044 if (plugin->mst != NULL)
2046 GNUNET_SERVER_mst_destroy(plugin->mst);
2050 /* Clean up leftover messages */
2051 struct UDPMessageWrapper * udpw;
2052 udpw = plugin->ipv4_queue_head;
2053 while (udpw != NULL)
2055 struct UDPMessageWrapper *tmp = udpw->next;
2056 GNUNET_CONTAINER_DLL_remove(plugin->ipv4_queue_head, plugin->ipv4_queue_tail, udpw);
2057 if (udpw->cont != NULL)
2058 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
2062 udpw = plugin->ipv6_queue_head;
2063 while (udpw != NULL)
2065 struct UDPMessageWrapper *tmp = udpw->next;
2066 GNUNET_CONTAINER_DLL_remove(plugin->ipv6_queue_head, plugin->ipv6_queue_tail, udpw);
2067 if (udpw->cont != NULL)
2068 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
2073 /* Clean up sessions */
2075 LOG (GNUNET_ERROR_TYPE_DEBUG,
2076 "Cleaning up sessions\n");
2078 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2079 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2082 GNUNET_free (plugin);
2088 /* end of plugin_transport_udp.c */