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->msg_head;
557 if (udpw->session == s)
559 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
561 if (udpw->cont != NULL)
562 udpw->cont (udpw->cont_cls, &s->target, GNUNET_SYSERR);
568 GNUNET_assert (GNUNET_YES ==
569 GNUNET_CONTAINER_multihashmap_remove (plugin->sessions,
570 &s->target.hashPubKey,
580 * Disconnect from a remote node. Clean up session if we have one for this peer
582 * @param cls closure for this call (should be handle to Plugin)
583 * @param target the peeridentity of the peer to disconnect
584 * @return GNUNET_OK on success, GNUNET_SYSERR if the operation failed
587 udp_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
589 struct Plugin *plugin = cls;
590 GNUNET_assert (plugin != NULL);
592 GNUNET_assert (target != NULL);
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Disconnecting from peer `%s'\n", GNUNET_i2s (target));
597 /* Clean up sessions */
598 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessions, &target->hashPubKey, &disconnect_and_free_it, plugin);
601 static struct Session *
602 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
603 const void *addr, size_t addrlen,
604 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
607 const struct IPv4UdpAddress *t4;
608 const struct IPv6UdpAddress *t6;
609 struct sockaddr_in *v4;
610 struct sockaddr_in6 *v6;
615 case sizeof (struct IPv4UdpAddress):
616 if (NULL == plugin->sockv4)
621 s = GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in));
622 len = sizeof (struct sockaddr_in);
623 v4 = (struct sockaddr_in *) &s[1];
624 v4->sin_family = AF_INET;
625 #if HAVE_SOCKADDR_IN_SIN_LEN
626 v4->sin_len = sizeof (struct sockaddr_in);
628 v4->sin_port = t4->u4_port;
629 v4->sin_addr.s_addr = t4->ipv4_addr;
630 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v4, sizeof (struct sockaddr_in));
632 case sizeof (struct IPv6UdpAddress):
633 if (NULL == plugin->sockv6)
639 GNUNET_malloc (sizeof (struct Session) + sizeof (struct sockaddr_in6));
640 len = sizeof (struct sockaddr_in6);
641 v6 = (struct sockaddr_in6 *) &s[1];
642 v6->sin6_family = AF_INET6;
643 #if HAVE_SOCKADDR_IN_SIN_LEN
644 v6->sin6_len = sizeof (struct sockaddr_in6);
646 v6->sin6_port = t6->u6_port;
647 v6->sin6_addr = t6->ipv6_addr;
648 s->ats = plugin->env->get_address_type (plugin->env->cls, (const struct sockaddr *) v6, sizeof (struct sockaddr_in6));
651 /* Must have a valid address to send to */
658 s->sock_addr = (const struct sockaddr *) &s[1];
659 s->flow_delay_for_other_peer = GNUNET_TIME_relative_get_zero();
660 s->flow_delay_from_other_peer = GNUNET_TIME_absolute_get_zero();
661 s->last_expected_delay = GNUNET_TIME_UNIT_SECONDS;
666 static int session_cmp_it (void *cls,
667 const GNUNET_HashCode * key,
670 struct SessionCompareContext * cctx = cls;
671 const struct GNUNET_HELLO_Address *address = cctx->addr;
672 struct Session *s = value;
674 socklen_t s_addrlen = s->addrlen;
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Comparing address %s <-> %s\n",
678 udp_address_to_string (NULL, (void *) address->address, address->address_length),
679 GNUNET_a2s (s->sock_addr, s->addrlen));
682 if ((address->address_length == sizeof (struct IPv4UdpAddress)) &&
683 (s_addrlen == sizeof (struct sockaddr_in)))
685 struct IPv4UdpAddress * u4 = NULL;
686 u4 = (struct IPv4UdpAddress *) address->address;
687 const struct sockaddr_in *s4 = (const struct sockaddr_in *) s->sock_addr;
688 if ((0 == memcmp ((const void *) &u4->ipv4_addr,(const void *) &s4->sin_addr, sizeof (struct in_addr))) &&
689 (u4->u4_port == s4->sin_port))
696 if ((address->address_length == sizeof (struct IPv6UdpAddress)) &&
697 (s_addrlen == sizeof (struct sockaddr_in6)))
699 struct IPv6UdpAddress * u6 = NULL;
700 u6 = (struct IPv6UdpAddress *) address->address;
701 const struct sockaddr_in6 *s6 = (const struct sockaddr_in6 *) s->sock_addr;
702 if ((0 == memcmp (&u6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr))) &&
703 (u6->u6_port == s6->sin6_port))
716 * Creates a new outbound session the transport service will use to send data to the
719 * @param cls the plugin
720 * @param address the address
721 * @return the session or NULL of max connections exceeded
723 static struct Session *
724 udp_plugin_get_session (void *cls,
725 const struct GNUNET_HELLO_Address *address)
727 struct Session * s = NULL;
728 struct Plugin * plugin = cls;
730 GNUNET_assert (plugin != NULL);
731 GNUNET_assert (address != NULL);
733 if ((address->address == NULL) ||
734 ((address->address_length != sizeof (struct IPv4UdpAddress)) &&
735 (address->address_length != sizeof (struct IPv6UdpAddress))))
741 /* check if session already exists */
742 struct SessionCompareContext cctx;
746 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));
748 GNUNET_CONTAINER_multihashmap_get_multiple(plugin->sessions, &address->peer.hashPubKey, session_cmp_it, &cctx);
749 if (cctx.res != NULL)
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p\n", cctx.res);
757 /* otherwise create new */
758 s = create_session (plugin,
761 address->address_length,
764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
765 "Creating new session %p for peer `%s' address `%s'\n",
767 GNUNET_i2s(&address->peer),
768 udp_address_to_string(NULL,address->address,address->address_length));
770 GNUNET_assert (GNUNET_OK ==
771 GNUNET_CONTAINER_multihashmap_put (plugin->sessions,
772 &s->target.hashPubKey,
774 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
780 * Function that is called with messages created by the fragmentation
781 * module. In the case of the 'proc' callback of the
782 * GNUNET_FRAGMENT_context_create function, this function must
783 * eventually call 'GNUNET_FRAGMENT_context_transmission_done'.
785 * @param cls closure, the 'struct FragmentationContext'
786 * @param msg the message that was created
789 enqueue_fragment (void *cls, const struct GNUNET_MessageHeader *msg)
791 struct FragmentationContext *frag_ctx = cls;
792 struct Plugin *plugin = frag_ctx->plugin;
793 struct UDPMessageWrapper * udpw;
795 size_t msg_len = ntohs (msg->size);
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueuing fragment with %u bytes %u\n", msg_len , sizeof (struct UDPMessageWrapper));
801 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msg_len);
802 udpw->session = frag_ctx->session;
803 udpw->udp = (char *) &udpw[1];
805 udpw->msg_size = msg_len;
806 udpw->cont = frag_ctx->cont;
807 udpw->cont_cls = frag_ctx->cont_cls;
808 udpw->timeout = frag_ctx->timeout;
809 udpw->frag_ctx = frag_ctx;
810 memcpy (udpw->udp, msg, msg_len);
812 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, udpw);
817 * Function that can be used by the transport service to transmit
818 * a message using the plugin. Note that in the case of a
819 * peer disconnecting, the continuation MUST be called
820 * prior to the disconnect notification itself. This function
821 * will be called with this peer's HELLO message to initiate
822 * a fresh connection to another peer.
825 * @param s which session must be used
826 * @param msgbuf the message to transmit
827 * @param msgbuf_size number of bytes in 'msgbuf'
828 * @param priority how important is the message (most plugins will
829 * ignore message priority and just FIFO)
830 * @param to how long to wait at most for the transmission (does not
831 * require plugins to discard the message after the timeout,
832 * just advisory for the desired delay; most plugins will ignore
834 * @param cont continuation to call once the message has
835 * been transmitted (or if the transport is ready
836 * for the next transmission call; or if the
837 * peer disconnected...); can be NULL
838 * @param cont_cls closure for cont
839 * @return number of bytes used (on the physical network, with overheads);
840 * -1 on hard errors (i.e. address invalid); 0 is a legal value
841 * and does NOT mean that the message was not transmitted (DV)
844 udp_plugin_send (void *cls,
846 const char *msgbuf, size_t msgbuf_size,
847 unsigned int priority,
848 struct GNUNET_TIME_Relative to,
849 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
851 struct Plugin *plugin = cls;
852 size_t mlen = msgbuf_size + sizeof (struct UDPMessage);
854 struct UDPMessageWrapper * udpw;
855 struct UDPMessage *udp;
857 GNUNET_assert (plugin != NULL);
858 GNUNET_assert (s != NULL);
860 if (mlen >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
863 return GNUNET_SYSERR;
866 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains_value(plugin->sessions, &s->target.hashPubKey, s))
869 return GNUNET_SYSERR;
872 LOG (GNUNET_ERROR_TYPE_DEBUG,
873 "UDP transmits %u-byte message to `%s' using address `%s'\n",
875 GNUNET_i2s (&s->target),
876 GNUNET_a2s(s->sock_addr, s->addrlen));
879 udp = (struct UDPMessage *) mbuf;
880 udp->header.size = htons (mlen);
881 udp->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE);
882 udp->reserved = htonl (0);
883 udp->sender = *plugin->env->my_identity;
887 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + mlen);
889 udpw->udp = (char *) &udpw[1];
890 udpw->msg_size = mlen;
891 udpw->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
893 udpw->cont_cls = cont_cls;
894 udpw->frag_ctx = NULL;
896 memcpy (udpw->udp, udp, sizeof (struct UDPMessage));
897 memcpy (&udpw->udp[sizeof (struct UDPMessage)], msgbuf, msgbuf_size);
899 GNUNET_CONTAINER_DLL_insert(plugin->msg_head, plugin->msg_tail, udpw);
903 LOG (GNUNET_ERROR_TYPE_DEBUG,
904 "UDP has to fragment message \n");
905 if (s->frag_ctx != NULL)
906 return GNUNET_SYSERR;
907 memcpy (&udp[1], msgbuf, msgbuf_size);
908 struct FragmentationContext * frag_ctx = GNUNET_malloc(sizeof (struct FragmentationContext));
910 frag_ctx->plugin = plugin;
911 frag_ctx->session = s;
912 frag_ctx->cont = cont;
913 frag_ctx->cont_cls = cont_cls;
914 frag_ctx->timeout = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), to);
915 frag_ctx->bytes_to_send = mlen;
916 frag_ctx->frag = GNUNET_FRAGMENT_context_create (plugin->env->stats,
919 s->last_expected_delay,
924 s->frag_ctx = frag_ctx;
932 * Our external IP address/port mapping has changed.
934 * @param cls closure, the 'struct LocalAddrList'
935 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
936 * the previous (now invalid) one
937 * @param addr either the previous or the new public IP address
938 * @param addrlen actual lenght of the address
941 udp_nat_port_map_callback (void *cls, int add_remove,
942 const struct sockaddr *addr, socklen_t addrlen)
944 struct Plugin *plugin = cls;
945 struct IPv4UdpAddress u4;
946 struct IPv6UdpAddress u6;
950 /* convert 'addr' to our internal format */
951 switch (addr->sa_family)
954 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
955 u4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
956 u4.u4_port = ((struct sockaddr_in *) addr)->sin_port;
961 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
962 memcpy (&u6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
963 sizeof (struct in6_addr));
964 u6.u6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
972 /* modify our published address list */
973 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args);
979 * Message tokenizer has broken up an incomming message. Pass it on
982 * @param cls the 'struct Plugin'
983 * @param client the 'struct SourceInformation'
984 * @param hdr the actual message
987 process_inbound_tokenized_messages (void *cls, void *client,
988 const struct GNUNET_MessageHeader *hdr)
990 struct Plugin *plugin = cls;
991 struct SourceInformation *si = client;
992 struct GNUNET_ATS_Information ats[2];
993 struct GNUNET_TIME_Relative delay;
995 GNUNET_assert (si->session != NULL);
997 ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
998 ats[0].value = htonl (1);
999 ats[1] = si->session->ats;
1000 GNUNET_break (ntohl(ats[1].value) != GNUNET_ATS_NET_UNSPECIFIED);
1002 delay = plugin->env->receive (plugin->env->cls,
1005 (const struct GNUNET_ATS_Information *) &ats, 2,
1009 si->session->flow_delay_for_other_peer = delay;
1014 * We've received a UDP Message. Process it (pass contents to main service).
1016 * @param plugin plugin context
1017 * @param msg the message
1018 * @param sender_addr sender address
1019 * @param sender_addr_len number of bytes in sender_addr
1022 process_udp_message (struct Plugin *plugin, const struct UDPMessage *msg,
1023 const struct sockaddr *sender_addr,
1024 socklen_t sender_addr_len)
1026 struct SourceInformation si;
1027 struct Session * s = NULL;
1028 struct IPv4UdpAddress u4;
1029 struct IPv6UdpAddress u6;
1033 if (0 != ntohl (msg->reserved))
1035 GNUNET_break_op (0);
1038 if (ntohs (msg->header.size) <
1039 sizeof (struct GNUNET_MessageHeader) + sizeof (struct UDPMessage))
1041 GNUNET_break_op (0);
1045 /* convert address */
1046 switch (sender_addr->sa_family)
1049 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in));
1050 u4.ipv4_addr = ((struct sockaddr_in *) sender_addr)->sin_addr.s_addr;
1051 u4.u4_port = ((struct sockaddr_in *) sender_addr)->sin_port;
1056 GNUNET_assert (sender_addr_len == sizeof (struct sockaddr_in6));
1057 u6.ipv6_addr = ((struct sockaddr_in6 *) sender_addr)->sin6_addr;
1058 u6.u6_port = ((struct sockaddr_in6 *) sender_addr)->sin6_port;
1067 LOG (GNUNET_ERROR_TYPE_DEBUG,
1068 "Received message with %u bytes from peer `%s' at `%s'\n",
1069 (unsigned int) ntohs (msg->header.size), GNUNET_i2s (&msg->sender),
1070 GNUNET_a2s (sender_addr, sender_addr_len));
1073 struct GNUNET_HELLO_Address * address = GNUNET_HELLO_address_allocate(&msg->sender, "udp", arg, args);
1074 s = udp_plugin_get_session(plugin, address);
1075 GNUNET_free (address);
1077 /* iterate over all embedded messages */
1079 si.sender = msg->sender;
1083 GNUNET_SERVER_mst_receive (plugin->mst, &si, (const char *) &msg[1],
1084 ntohs (msg->header.size) -
1085 sizeof (struct UDPMessage), GNUNET_YES, GNUNET_NO);
1090 * Scan the heap for a receive context with the given address.
1092 * @param cls the 'struct FindReceiveContext'
1093 * @param node internal node of the heap
1094 * @param element value stored at the node (a 'struct ReceiveContext')
1095 * @param cost cost associated with the node
1096 * @return GNUNET_YES if we should continue to iterate,
1100 find_receive_context (void *cls, struct GNUNET_CONTAINER_HeapNode *node,
1101 void *element, GNUNET_CONTAINER_HeapCostType cost)
1103 struct FindReceiveContext *frc = cls;
1104 struct DefragContext *e = element;
1106 if ((frc->addr_len == e->addr_len) &&
1107 (0 == memcmp (frc->addr, e->src_addr, frc->addr_len)))
1117 * Process a defragmented message.
1119 * @param cls the 'struct ReceiveContext'
1120 * @param msg the message
1123 fragment_msg_proc (void *cls, const struct GNUNET_MessageHeader *msg)
1125 struct DefragContext *rc = cls;
1127 if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE)
1132 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1137 process_udp_message (rc->plugin, (const struct UDPMessage *) msg,
1138 rc->src_addr, rc->addr_len);
1141 struct LookupContext
1143 const struct sockaddr * addr;
1146 struct Session *res;
1150 lookup_session_by_addr_it (void *cls, const GNUNET_HashCode * key, void *value)
1152 struct LookupContext *l_ctx = cls;
1153 struct Session * s = value;
1155 if ((s->addrlen == l_ctx->addrlen) &&
1156 (0 == memcmp (s->sock_addr, l_ctx->addr, s->addrlen)))
1165 * Transmit an acknowledgement.
1167 * @param cls the 'struct ReceiveContext'
1168 * @param id message ID (unused)
1169 * @param msg ack to transmit
1172 ack_proc (void *cls, uint32_t id, const struct GNUNET_MessageHeader *msg)
1174 struct DefragContext *rc = cls;
1176 size_t msize = sizeof (struct UDP_ACK_Message) + ntohs (msg->size);
1177 struct UDP_ACK_Message *udp_ack;
1179 struct UDPMessageWrapper *udpw;
1182 struct LookupContext l_ctx;
1183 l_ctx.addr = rc->src_addr;
1184 l_ctx.addrlen = rc->addr_len;
1186 GNUNET_CONTAINER_multihashmap_iterate (rc->plugin->sessions,
1187 &lookup_session_by_addr_it,
1191 GNUNET_assert (s != NULL);
1193 if (s->flow_delay_for_other_peer.rel_value <= UINT32_MAX)
1194 delay = s->flow_delay_for_other_peer.rel_value;
1197 LOG (GNUNET_ERROR_TYPE_DEBUG,
1198 "Sending ACK to `%s' including delay of %u ms\n",
1199 GNUNET_a2s (rc->src_addr,
1200 (rc->src_addr->sa_family ==
1201 AF_INET) ? sizeof (struct sockaddr_in) : sizeof (struct
1205 udpw = GNUNET_malloc (sizeof (struct UDPMessageWrapper) + msize);
1207 udpw->cont_cls = NULL;
1208 udpw->frag_ctx = NULL;
1209 udpw->msg_size = msize;
1211 udpw->timeout = GNUNET_TIME_absolute_get_forever();
1212 udpw->udp = (char *)&udpw[1];
1214 udp_ack = (struct UDP_ACK_Message *) udpw->udp;
1215 udp_ack->header.size = htons ((uint16_t) msize);
1216 udp_ack->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK);
1217 udp_ack->delay = htonl (delay);
1218 udp_ack->sender = *rc->plugin->env->my_identity;
1219 memcpy (&udp_ack[1], msg, ntohs (msg->size));
1221 GNUNET_CONTAINER_DLL_insert(rc->plugin->msg_head, rc->plugin->msg_tail, udpw);
1225 static void read_process_msg (struct Plugin *plugin,
1226 const struct GNUNET_MessageHeader *msg,
1230 if (ntohs (msg->size) < sizeof (struct UDPMessage))
1232 GNUNET_break_op (0);
1235 process_udp_message (plugin, (const struct UDPMessage *) msg,
1236 (const struct sockaddr *) addr, fromlen);
1240 static void read_process_ack (struct Plugin *plugin,
1241 const struct GNUNET_MessageHeader *msg,
1245 const struct GNUNET_MessageHeader *ack;
1246 const struct UDP_ACK_Message *udp_ack;
1247 struct LookupContext l_ctx;
1248 struct Session *s = NULL;
1249 struct GNUNET_TIME_Relative flow_delay;
1251 if (ntohs (msg->size) <
1252 sizeof (struct UDP_ACK_Message) + sizeof (struct GNUNET_MessageHeader))
1254 GNUNET_break_op (0);
1258 udp_ack = (const struct UDP_ACK_Message *) msg;
1260 l_ctx.addr = (const struct sockaddr *) addr;
1261 l_ctx.addrlen = fromlen;
1263 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions,
1264 &lookup_session_by_addr_it,
1268 if ((s == NULL) || (s->frag_ctx == NULL))
1271 flow_delay.rel_value = (uint64_t) ntohl (udp_ack->delay);
1272 LOG (GNUNET_ERROR_TYPE_DEBUG, "We received a sending delay of %llu\n",
1273 flow_delay.rel_value);
1274 s->flow_delay_from_other_peer =
1275 GNUNET_TIME_relative_to_absolute (flow_delay);
1277 ack = (const struct GNUNET_MessageHeader *) &udp_ack[1];
1278 if (ntohs (ack->size) !=
1279 ntohs (msg->size) - sizeof (struct UDP_ACK_Message))
1281 GNUNET_break_op (0);
1285 if (GNUNET_OK != GNUNET_FRAGMENT_process_ack (s->frag_ctx->frag, ack))
1288 LOG (GNUNET_ERROR_TYPE_DEBUG,
1289 "UDP processes %u-byte acknowledgement from `%s' at `%s'\n",
1290 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1291 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1297 LOG (GNUNET_ERROR_TYPE_DEBUG,
1298 "FULL MESSAGE ACKed\n",
1299 (unsigned int) ntohs (msg->size), GNUNET_i2s (&udp_ack->sender),
1300 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1302 s->last_expected_delay = GNUNET_FRAGMENT_context_destroy (s->frag_ctx->frag);
1304 struct UDPMessageWrapper * udpw = plugin->msg_head;
1307 if ((udpw->frag_ctx != NULL) && (udpw->frag_ctx == s->frag_ctx))
1309 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1315 if (s->frag_ctx->cont != NULL)
1317 (s->frag_ctx->cont_cls, &udp_ack->sender, GNUNET_OK);
1318 GNUNET_free (s->frag_ctx);
1323 static void read_process_fragment (struct Plugin *plugin,
1324 const struct GNUNET_MessageHeader *msg,
1328 struct DefragContext *d_ctx;
1329 struct GNUNET_TIME_Absolute now;
1330 struct FindReceiveContext frc;
1334 frc.addr = (const struct sockaddr *) addr;
1335 frc.addr_len = fromlen;
1338 LOG (GNUNET_ERROR_TYPE_DEBUG, "UDP processes %u-byte fragment from `%s'\n",
1339 (unsigned int) ntohs (msg->size),
1340 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1343 /* Lookup existing receive context for this address */
1344 GNUNET_CONTAINER_heap_iterate (plugin->defrag_ctxs,
1345 &find_receive_context,
1347 now = GNUNET_TIME_absolute_get ();
1352 /* Create a new defragmentation context */
1353 d_ctx = GNUNET_malloc (sizeof (struct DefragContext) + fromlen);
1354 memcpy (&d_ctx[1], addr, fromlen);
1355 d_ctx->src_addr = (const struct sockaddr *) &d_ctx[1];
1356 d_ctx->addr_len = fromlen;
1357 d_ctx->plugin = plugin;
1359 GNUNET_DEFRAGMENT_context_create (plugin->env->stats, UDP_MTU,
1360 UDP_MAX_MESSAGES_IN_DEFRAG, d_ctx,
1361 &fragment_msg_proc, &ack_proc);
1363 GNUNET_CONTAINER_heap_insert (plugin->defrag_ctxs, d_ctx,
1364 (GNUNET_CONTAINER_HeapCostType)
1367 LOG (GNUNET_ERROR_TYPE_DEBUG, "Created new defragmentation context for %u-byte fragment from `%s'\n",
1368 (unsigned int) ntohs (msg->size),
1369 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1375 LOG (GNUNET_ERROR_TYPE_DEBUG, "Found existing defragmentation context for %u-byte fragment from `%s'\n",
1376 (unsigned int) ntohs (msg->size),
1377 GNUNET_a2s ((const struct sockaddr *) addr, fromlen));
1381 if (GNUNET_OK == GNUNET_DEFRAGMENT_process_fragment (d_ctx->defrag, msg))
1383 /* keep this 'rc' from expiring */
1384 GNUNET_CONTAINER_heap_update_cost (plugin->defrag_ctxs, d_ctx->hnode,
1385 (GNUNET_CONTAINER_HeapCostType)
1388 if (GNUNET_CONTAINER_heap_get_size (plugin->defrag_ctxs) >
1389 UDP_MAX_SENDER_ADDRESSES_WITH_DEFRAG)
1391 /* remove 'rc' that was inactive the longest */
1392 d_ctx = GNUNET_CONTAINER_heap_remove_root (plugin->defrag_ctxs);
1393 GNUNET_assert (NULL != d_ctx);
1394 GNUNET_DEFRAGMENT_context_destroy (d_ctx->defrag);
1395 GNUNET_free (d_ctx);
1400 * Read and process a message from the given socket.
1402 * @param plugin the overall plugin
1403 * @param rsock socket to read from
1406 udp_select_read (struct Plugin *plugin, struct GNUNET_NETWORK_Handle *rsock)
1412 const struct GNUNET_MessageHeader *msg;
1414 fromlen = sizeof (addr);
1415 memset (&addr, 0, sizeof (addr));
1416 size = GNUNET_NETWORK_socket_recvfrom (rsock, buf, sizeof (buf),
1417 (struct sockaddr *) &addr, &fromlen);
1419 if (size < sizeof (struct GNUNET_MessageHeader))
1421 GNUNET_break_op (0);
1424 msg = (const struct GNUNET_MessageHeader *) buf;
1426 LOG (GNUNET_ERROR_TYPE_DEBUG,
1427 "UDP received %u-byte message from `%s' type %i\n", (unsigned int) size,
1428 GNUNET_a2s ((const struct sockaddr *) addr, fromlen), ntohs (msg->type));
1430 if (size != ntohs (msg->size))
1432 GNUNET_break_op (0);
1436 switch (ntohs (msg->type))
1438 case GNUNET_MESSAGE_TYPE_TRANSPORT_BROADCAST_BEACON:
1439 udp_broadcast_receive (plugin, &buf, size, addr, fromlen);
1442 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_MESSAGE:
1443 read_process_msg (plugin, msg, addr, fromlen);
1446 case GNUNET_MESSAGE_TYPE_TRANSPORT_UDP_ACK:
1447 read_process_ack (plugin, msg, addr, fromlen);;
1450 case GNUNET_MESSAGE_TYPE_FRAGMENT:
1451 read_process_fragment (plugin, msg, addr, fromlen);
1455 GNUNET_break_op (0);
1461 udp_select_send (struct Plugin *plugin)
1465 struct GNUNET_TIME_Absolute max;
1466 struct GNUNET_TIME_Absolute ;
1468 struct UDPMessageWrapper *udpw = plugin->msg_head;
1469 const struct sockaddr * sa = udpw->session->sock_addr;
1471 max = GNUNET_TIME_absolute_max(udpw->timeout, GNUNET_TIME_absolute_get());
1473 while (udpw != NULL)
1475 if (max.abs_value != udpw->timeout.abs_value)
1477 /* Message timed out */
1479 if (udpw->cont != NULL)
1480 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1481 if (udpw->frag_ctx != NULL)
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fragmented message for peer `%s' with size %u timed out\n",
1485 GNUNET_i2s(&udpw->session->target), udpw->frag_ctx->bytes_to_send);
1487 udpw->session->last_expected_delay = GNUNET_FRAGMENT_context_destroy(udpw->frag_ctx->frag);
1488 GNUNET_free (udpw->frag_ctx);
1489 udpw->session->frag_ctx = NULL;
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' with size %u timed out\n",
1495 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1499 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1501 udpw = plugin->msg_head;
1505 struct GNUNET_TIME_Relative delta = GNUNET_TIME_absolute_get_remaining (udpw->session->flow_delay_from_other_peer);
1506 if (delta.rel_value == 0)
1508 /* this message is not delayed */
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is not delayed \n",
1510 GNUNET_i2s(&udpw->session->target), udpw->msg_size);
1515 /* this message is delayed, try next */
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Message for peer `%s' (%u bytes) is delayed for %llu \n",
1517 GNUNET_i2s(&udpw->session->target), udpw->msg_size,
1527 /* No message left */
1531 switch (sa->sa_family)
1534 if (NULL == plugin->sockv4)
1537 GNUNET_NETWORK_socket_sendto (plugin->sockv4, udpw->udp, udpw->msg_size,
1538 sa, slen = sizeof (struct sockaddr_in));
1541 if (NULL == plugin->sockv6)
1544 GNUNET_NETWORK_socket_sendto (plugin->sockv6, udpw->udp, udpw->msg_size,
1545 sa, slen = sizeof (struct sockaddr_in6));
1551 if (GNUNET_SYSERR == sent)
1553 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "sendto");
1554 LOG (GNUNET_ERROR_TYPE_DEBUG,
1555 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1556 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1557 (sent < 0) ? STRERROR (errno) : "ok");
1558 if (udpw->cont != NULL)
1559 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
1561 LOG (GNUNET_ERROR_TYPE_DEBUG,
1562 "UDP transmitted %u-byte message to %s (%d: %s)\n",
1563 (unsigned int) (udpw->msg_size), GNUNET_a2s (sa, slen), (int) sent,
1564 (sent < 0) ? STRERROR (errno) : "ok");
1566 /* This was just a message fragment */
1567 if (udpw->frag_ctx != NULL)
1569 GNUNET_FRAGMENT_context_transmission_done (udpw->frag_ctx->frag);
1571 /* This was a complete message*/
1574 if (udpw->cont != NULL)
1575 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_OK);
1578 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1585 * We have been notified that our readset has something to read. We don't
1586 * know which socket needs to be read, so we have to check each one
1587 * Then reschedule this function to be called again once more is available.
1589 * @param cls the plugin handle
1590 * @param tc the scheduling context (for rescheduling this function again)
1593 udp_plugin_select (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1595 struct Plugin *plugin = cls;
1597 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1598 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1601 if ((tc->reason & GNUNET_SCHEDULER_REASON_READ_READY) != 0)
1603 if ((NULL != plugin->sockv4) &&
1604 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv4)))
1605 udp_select_read (plugin, plugin->sockv4);
1606 if ((NULL != plugin->sockv6) &&
1607 (GNUNET_NETWORK_fdset_isset (tc->read_ready, plugin->sockv6)))
1608 udp_select_read (plugin, plugin->sockv6);
1611 if ((tc->reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0)
1613 if (plugin->msg_head != NULL)
1614 udp_select_send (plugin);
1617 plugin->select_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1618 GNUNET_SCHEDULER_NO_TASK,
1619 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1620 plugin->ws, &udp_plugin_select, plugin);
1626 setup_sockets (struct Plugin *plugin, struct sockaddr_in6 *serverAddrv6, struct sockaddr_in *serverAddrv4)
1629 int sockets_created = 0;
1630 struct sockaddr *serverAddr;
1631 struct sockaddr *addrs[2];
1632 socklen_t addrlens[2];
1635 /* Create IPv6 socket */
1636 if (plugin->enable_ipv6 == GNUNET_YES)
1638 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_DGRAM, 0);
1639 if (NULL == plugin->sockv6)
1641 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Disabling IPv6 since it is not supported on this system!\n");
1642 plugin->enable_ipv6 = GNUNET_NO;
1646 #if HAVE_SOCKADDR_IN_SIN_LEN
1647 serverAddrv6->sin6_len = sizeof (serverAddrv6);
1649 serverAddrv6->sin6_family = AF_INET6;
1650 serverAddrv6->sin6_addr = in6addr_any;
1651 serverAddrv6->sin6_port = htons (plugin->port);
1652 addrlen = sizeof (struct sockaddr_in6);
1653 serverAddr = (struct sockaddr *) serverAddrv6;
1655 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv6 port %d\n",
1656 ntohs (serverAddrv6->sin6_port));
1659 while (GNUNET_NETWORK_socket_bind (plugin->sockv6, serverAddr, addrlen) !=
1662 serverAddrv6->sin6_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1664 LOG (GNUNET_ERROR_TYPE_DEBUG,
1665 "IPv6 Binding failed, trying new port %d\n",
1666 ntohs (serverAddrv6->sin6_port));
1671 GNUNET_NETWORK_socket_close (plugin->sockv6);
1672 plugin->sockv6 = NULL;
1676 if (plugin->sockv6 != NULL)
1679 LOG (GNUNET_ERROR_TYPE_DEBUG,
1680 "IPv6 socket created on port %d\n",
1681 ntohs (serverAddrv6->sin6_port));
1683 addrs[sockets_created] = (struct sockaddr *) serverAddrv6;
1684 addrlens[sockets_created] = sizeof (struct sockaddr_in6);
1690 /* Create IPv4 socket */
1691 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET, SOCK_DGRAM, 0);
1692 if (NULL == plugin->sockv4)
1694 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "socket");
1698 #if HAVE_SOCKADDR_IN_SIN_LEN
1699 serverAddrv4->sin_len = sizeof (serverAddrv4);
1701 serverAddrv4->sin_family = AF_INET;
1702 serverAddrv4->sin_addr.s_addr = INADDR_ANY;
1703 serverAddrv4->sin_port = htons (plugin->port);
1704 addrlen = sizeof (struct sockaddr_in);
1705 serverAddr = (struct sockaddr *) serverAddrv4;
1708 LOG (GNUNET_ERROR_TYPE_DEBUG, "Binding to IPv4 port %d\n",
1709 ntohs (serverAddrv4->sin_port));
1712 while (GNUNET_NETWORK_socket_bind (plugin->sockv4, serverAddr, addrlen) !=
1715 serverAddrv4->sin_port = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG, 33537) + 32000); /* Find a good, non-root port */
1717 LOG (GNUNET_ERROR_TYPE_DEBUG, "IPv4 Binding failed, trying new port %d\n",
1718 ntohs (serverAddrv4->sin_port));
1723 GNUNET_NETWORK_socket_close (plugin->sockv4);
1724 plugin->sockv4 = NULL;
1728 if (plugin->sockv4 != NULL)
1730 addrs[sockets_created] = (struct sockaddr *) serverAddrv4;
1731 addrlens[sockets_created] = sizeof (struct sockaddr_in);
1736 /* Create file descriptors */
1737 plugin->rs = GNUNET_NETWORK_fdset_create ();
1738 plugin->ws = GNUNET_NETWORK_fdset_create ();
1739 GNUNET_NETWORK_fdset_zero (plugin->rs);
1740 GNUNET_NETWORK_fdset_zero (plugin->ws);
1741 if (NULL != plugin->sockv4)
1743 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv4);
1744 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv4);
1746 if (NULL != plugin->sockv6)
1748 GNUNET_NETWORK_fdset_set (plugin->rs, plugin->sockv6);
1749 GNUNET_NETWORK_fdset_set (plugin->ws, plugin->sockv6);
1752 if (sockets_created == 0)
1753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Failed to open UDP sockets\n"));
1755 plugin->select_task =
1756 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1757 GNUNET_SCHEDULER_NO_TASK,
1758 GNUNET_TIME_UNIT_FOREVER_REL, plugin->rs,
1759 plugin->ws, &udp_plugin_select, plugin);
1761 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
1762 GNUNET_NO, plugin->port,
1764 (const struct sockaddr **) addrs, addrlens,
1765 &udp_nat_port_map_callback, NULL, plugin);
1767 return sockets_created;
1772 * The exported method. Makes the core api available via a global and
1773 * returns the udp transport API.
1775 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1776 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1779 libgnunet_plugin_transport_udp_init (void *cls)
1781 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1782 struct GNUNET_TRANSPORT_PluginFunctions *api;
1783 struct Plugin *plugin;
1785 unsigned long long port;
1786 unsigned long long aport;
1787 unsigned long long broadcast;
1788 unsigned long long udp_max_bps;
1789 unsigned long long enable_v6;
1790 char * bind4_address;
1791 char * bind6_address;
1792 struct GNUNET_TIME_Relative interval;
1794 struct sockaddr_in serverAddrv4;
1795 struct sockaddr_in6 serverAddrv6;
1799 /* Get port number */
1801 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp", "PORT",
1805 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1806 "ADVERTISED_PORT", &aport))
1810 LOG (GNUNET_ERROR_TYPE_WARNING,
1811 _("Given `%s' option is out of range: %llu > %u\n"), "PORT", port,
1818 GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "nat",
1821 enable_v6 = GNUNET_NO;
1824 enable_v6 = GNUNET_YES;
1828 memset (&serverAddrv6, 0, sizeof (serverAddrv6));
1829 memset (&serverAddrv4, 0, sizeof (serverAddrv4));
1832 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1833 "BINDTO", &bind4_address))
1835 LOG (GNUNET_ERROR_TYPE_DEBUG,
1836 "Binding udp plugin to specific address: `%s'\n",
1838 if (1 != inet_pton (AF_INET, bind4_address, &serverAddrv4.sin_addr))
1840 GNUNET_free (bind4_address);
1846 GNUNET_CONFIGURATION_get_value_string (env->cfg, "transport-udp",
1847 "BINDTO6", &bind6_address))
1849 LOG (GNUNET_ERROR_TYPE_DEBUG,
1850 "Binding udp plugin to specific address: `%s'\n",
1853 inet_pton (AF_INET6, bind6_address, &serverAddrv6.sin6_addr))
1855 LOG (GNUNET_ERROR_TYPE_ERROR, _("Invalid IPv6 address: `%s'\n"),
1857 GNUNET_free_non_null (bind4_address);
1858 GNUNET_free (bind6_address);
1864 /* Enable neighbour discovery */
1865 broadcast = GNUNET_CONFIGURATION_get_value_yesno (env->cfg, "transport-udp",
1867 if (broadcast == GNUNET_SYSERR)
1868 broadcast = GNUNET_NO;
1870 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-udp",
1871 "BROADCAST_INTERVAL", &interval))
1873 interval = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
1876 /* Maximum datarate */
1877 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-udp",
1878 "MAX_BPS", &udp_max_bps))
1880 udp_max_bps = 1024 * 1024 * 50; /* 50 MB/s == infinity for practical purposes */
1883 plugin = GNUNET_malloc (sizeof (struct Plugin));
1884 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1886 GNUNET_BANDWIDTH_tracker_init (&plugin->tracker,
1887 GNUNET_BANDWIDTH_value_init ((uint32_t)udp_max_bps), 30);
1890 plugin->sessions = GNUNET_CONTAINER_multihashmap_create (10);
1891 plugin->defrag_ctxs = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
1892 plugin->mst = GNUNET_SERVER_mst_create (&process_inbound_tokenized_messages, plugin);
1893 plugin->port = port;
1894 plugin->aport = aport;
1895 plugin->broadcast_interval = interval;
1896 plugin->enable_ipv6 = enable_v6;
1901 api->disconnect = &udp_disconnect;
1902 api->address_pretty_printer = &udp_plugin_address_pretty_printer;
1903 api->address_to_string = &udp_address_to_string;
1904 api->check_address = &udp_plugin_check_address;
1905 api->get_session = &udp_plugin_get_session;
1906 api->send = &udp_plugin_send;
1908 LOG (GNUNET_ERROR_TYPE_DEBUG, "Setting up sockets\n");
1909 res = setup_sockets (plugin, &serverAddrv6, &serverAddrv4);
1910 if ((res == 0) || ((plugin->sockv4 == NULL) && (plugin->sockv6 == NULL)))
1912 LOG (GNUNET_ERROR_TYPE_ERROR, "Failed to create network sockets, plugin failed\n");
1913 GNUNET_free (plugin);
1918 LOG (GNUNET_ERROR_TYPE_DEBUG, "Starting broadcasting\n");
1919 if (broadcast == GNUNET_YES)
1920 setup_broadcast (plugin, &serverAddrv6, &serverAddrv4);
1923 GNUNET_free_non_null (bind4_address);
1924 GNUNET_free_non_null (bind6_address);
1928 int heap_cleanup_iterator (void *cls,
1929 struct GNUNET_CONTAINER_HeapNode *
1930 node, void *element,
1931 GNUNET_CONTAINER_HeapCostType
1934 struct DefragContext * d_ctx = element;
1936 GNUNET_CONTAINER_heap_remove_node (node);
1937 GNUNET_DEFRAGMENT_context_destroy(d_ctx->defrag);
1938 GNUNET_free (d_ctx);
1945 * The exported method. Makes the core api available via a global and
1946 * returns the udp transport API.
1948 * @param cls our 'struct GNUNET_TRANSPORT_PluginEnvironment'
1949 * @return our 'struct GNUNET_TRANSPORT_PluginFunctions'
1952 libgnunet_plugin_transport_udp_done (void *cls)
1954 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1955 struct Plugin *plugin = api->cls;
1956 stop_broadcast (plugin);
1958 if (plugin->select_task != GNUNET_SCHEDULER_NO_TASK)
1960 GNUNET_SCHEDULER_cancel (plugin->select_task);
1961 plugin->select_task = GNUNET_SCHEDULER_NO_TASK;
1964 /* Closing sockets */
1965 if (plugin->sockv4 != NULL)
1967 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv4));
1968 plugin->sockv4 = NULL;
1970 if (plugin->sockv6 != NULL)
1972 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (plugin->sockv6));
1973 plugin->sockv6 = NULL;
1975 GNUNET_NETWORK_fdset_destroy (plugin->rs);
1976 GNUNET_NETWORK_fdset_destroy (plugin->ws);
1977 GNUNET_NAT_unregister (plugin->nat);
1979 if (plugin->defrag_ctxs != NULL)
1981 GNUNET_CONTAINER_heap_iterate(plugin->defrag_ctxs,
1982 heap_cleanup_iterator, NULL);
1983 GNUNET_CONTAINER_heap_destroy(plugin->defrag_ctxs);
1984 plugin->defrag_ctxs = NULL;
1986 if (plugin->mst != NULL)
1988 GNUNET_SERVER_mst_destroy(plugin->mst);
1992 /* Clean up leftover messages */
1993 struct UDPMessageWrapper *udpw = plugin->msg_head;
1994 while (udpw != NULL)
1996 struct UDPMessageWrapper *tmp = udpw->next;
1997 GNUNET_CONTAINER_DLL_remove(plugin->msg_head, plugin->msg_tail, udpw);
1998 if (udpw->cont != NULL)
1999 udpw->cont (udpw->cont_cls, &udpw->session->target, GNUNET_SYSERR);
2004 /* Clean up sessions */
2006 LOG (GNUNET_ERROR_TYPE_DEBUG,
2007 "Cleaning up sessions\n");
2009 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessions, &disconnect_and_free_it, plugin);
2010 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessions);
2013 GNUNET_free (plugin);
2019 /* end of plugin_transport_udp.c */