2 This file is part of GNUnet
3 Copyright (C) 2010-2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file transport/plugin_transport_xu.c
18 * @brief Implementation of the XU transport protocol
19 * @author Christian Grothoff
20 * @author Nathan Evans
21 * @author Matthias Wachs
24 #include "plugin_transport_xu.h"
25 #include "gnunet_hello_lib.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_fragmentation_lib.h"
28 #include "gnunet_nat_service.h"
29 #include "gnunet_protocols.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_signatures.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-xu", __VA_ARGS__)
41 * After how much inactivity should a XU session time out?
43 #define XU_SESSION_TIME_OUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
47 * XU Message-Packet header (after defragmentation).
54 struct GNUNET_MessageHeader header;
57 * Always zero for now.
62 * What is the identity of the sender
64 struct GNUNET_PeerIdentity sender;
70 * Closure for #append_port().
72 struct PrettyPrinterContext
77 struct PrettyPrinterContext *next;
82 struct PrettyPrinterContext *prev;
87 struct Plugin *plugin;
92 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
95 * Function to call with the result.
97 GNUNET_TRANSPORT_AddressStringCallback asc;
100 * Clsoure for @e asc.
107 struct GNUNET_SCHEDULER_Task *timeout_task;
110 * Is this an IPv6 address?
120 * Port to add after the IP address.
128 * Session with another peer.
130 struct GNUNET_ATS_Session
133 * Which peer is this session for?
135 struct GNUNET_PeerIdentity target;
138 * Tokenizer for inbound messages.
140 struct GNUNET_MessageStreamTokenizer *mst;
143 * Plugin this session belongs to.
145 struct Plugin *plugin;
148 * Session timeout task
150 struct GNUNET_SCHEDULER_Task *timeout_task;
153 * When does this session time out?
155 struct GNUNET_TIME_Absolute timeout;
158 * What time did we last transmit?
160 struct GNUNET_TIME_Absolute last_transmit_time;
163 * expected delay for ACKs
165 struct GNUNET_TIME_Relative last_expected_ack_delay;
168 * desired delay between XU messages
170 struct GNUNET_TIME_Relative last_expected_msg_delay;
174 struct GNUNET_TIME_Relative flow_delay_for_other_peer;
175 struct GNUNET_TIME_Relative flow_delay_from_other_peer;
180 struct GNUNET_HELLO_Address *address;
183 * Number of bytes waiting for transmission to this peer.
185 unsigned long long bytes_in_queue;
188 * Number of messages waiting for transmission to this peer.
190 unsigned int msgs_in_queue;
193 * Reference counter to indicate that this session is
194 * currently being used and must not be destroyed;
195 * setting @e in_destroy will destroy it as soon as
201 * Network type of the address.
203 enum GNUNET_ATS_Network_Type scope;
206 * Is this session about to be destroyed (sometimes we cannot
207 * destroy a session immediately as below us on the stack
208 * there might be code that still uses it; in this case,
209 * @e rc is non-zero).
217 * If a session monitor is attached, notify it about the new
220 * @param plugin our plugin
221 * @param session session that changed state
222 * @param state new state of the session
225 notify_session_monitor (struct Plugin *plugin,
226 struct GNUNET_ATS_Session *session,
227 enum GNUNET_TRANSPORT_SessionState state)
229 struct GNUNET_TRANSPORT_SessionInfo info;
231 if (NULL == plugin->sic)
233 if (GNUNET_YES == session->in_destroy)
234 return; /* already destroyed, just RC>0 left-over actions */
239 info.is_inbound = GNUNET_SYSERR; /* hard to say */
240 info.num_msg_pending = session->msgs_in_queue;
241 info.num_bytes_pending = session->bytes_in_queue;
242 /* info.receive_delay remains zero as this is not supported by XU
243 (cannot selectively not receive from 'some' peer while continuing
244 to receive from others) */
245 info.session_timeout = session->timeout;
246 info.address = session->address;
247 plugin->sic (plugin->sic_cls,
254 * Return information about the given session to the monitor callback.
256 * @param cls the `struct Plugin` with the monitor callback (`sic`)
257 * @param peer peer we send information about
258 * @param value our `struct GNUNET_ATS_Session` to send information about
259 * @return #GNUNET_OK (continue to iterate)
262 send_session_info_iter (void *cls,
263 const struct GNUNET_PeerIdentity *peer,
266 struct Plugin *plugin = cls;
267 struct GNUNET_ATS_Session *session = value;
270 notify_session_monitor (plugin,
272 GNUNET_TRANSPORT_SS_INIT);
273 notify_session_monitor (plugin,
275 GNUNET_TRANSPORT_SS_UP);
281 * Begin monitoring sessions of a plugin. There can only
282 * be one active monitor per plugin (i.e. if there are
283 * multiple monitors, the transport service needs to
284 * multiplex the generated events over all of them).
286 * @param cls closure of the plugin
287 * @param sic callback to invoke, NULL to disable monitor;
288 * plugin will being by iterating over all active
289 * sessions immediately and then enter monitor mode
290 * @param sic_cls closure for @a sic
293 xu_plugin_setup_monitor (void *cls,
294 GNUNET_TRANSPORT_SessionInfoCallback sic,
297 struct Plugin *plugin = cls;
300 plugin->sic_cls = sic_cls;
303 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessions,
304 &send_session_info_iter,
306 /* signal end of first iteration */
314 /* ****************** Little Helpers ****************** */
318 * Function to free last resources associated with a session.
320 * @param s session to free
323 free_session (struct GNUNET_ATS_Session *s)
325 if (NULL != s->address)
327 GNUNET_HELLO_address_free (s->address);
332 GNUNET_MST_destroy (s->mst);
340 * Function that is called to get the keepalive factor.
341 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
342 * calculate the interval between keepalive packets.
344 * @param cls closure with the `struct Plugin`
345 * @return keepalive factor
348 xu_query_keepalive_factor (void *cls)
356 * Function obtain the network type for a session
358 * @param cls closure (`struct Plugin *`)
359 * @param session the session
360 * @return the network type
362 static enum GNUNET_ATS_Network_Type
363 xu_plugin_get_network (void *cls,
364 struct GNUNET_ATS_Session *session)
367 return session->scope;
372 * Function obtain the network type for an address.
374 * @param cls closure (`struct Plugin *`)
375 * @param address the address
376 * @return the network type
378 static enum GNUNET_ATS_Network_Type
379 xu_plugin_get_network_for_address (void *cls,
380 const struct GNUNET_HELLO_Address *address)
382 struct Plugin *plugin = cls;
384 struct sockaddr_in a4;
385 struct sockaddr_in6 a6;
386 const struct IPv4XuAddress *u4;
387 const struct IPv6XuAddress *u6;
391 addrlen = address->address_length;
392 if (addrlen == sizeof(struct IPv6XuAddress))
394 GNUNET_assert (NULL != address->address); /* make static analysis happy */
395 u6 = address->address;
396 memset (&a6, 0, sizeof(a6));
397 #if HAVE_SOCKADDR_IN_SIN_LEN
398 a6.sin6_len = sizeof (a6);
400 a6.sin6_family = AF_INET6;
401 a6.sin6_port = u6->u6_port;
402 GNUNET_memcpy (&a6.sin6_addr, &u6->ipv6_addr, sizeof(struct in6_addr));
406 else if (addrlen == sizeof(struct IPv4XuAddress))
408 GNUNET_assert (NULL != address->address); /* make static analysis happy */
409 u4 = address->address;
410 memset (&a4, 0, sizeof(a4));
411 #if HAVE_SOCKADDR_IN_SIN_LEN
412 a4.sin_len = sizeof (a4);
414 a4.sin_family = AF_INET;
415 a4.sin_port = u4->u4_port;
416 a4.sin_addr.s_addr = u4->ipv4_addr;
423 return GNUNET_ATS_NET_UNSPECIFIED;
425 return plugin->env->get_address_type (plugin->env->cls,
431 /* ******************* Event loop ******************** */
434 * We have been notified that our readset has something to read. We don't
435 * know which socket needs to be read, so we have to check each one
436 * Then reschedule this function to be called again once more is available.
438 * @param cls the plugin handle
441 xu_plugin_select_v4 (void *cls);
445 * We have been notified that our readset has something to read. We don't
446 * know which socket needs to be read, so we have to check each one
447 * Then reschedule this function to be called again once more is available.
449 * @param cls the plugin handle
452 xu_plugin_select_v6 (void *cls);
456 * (re)schedule IPv4-select tasks for this plugin.
458 * @param plugin plugin to reschedule
461 schedule_select_v4 (struct Plugin *plugin)
463 if ( (GNUNET_YES != plugin->enable_ipv4) ||
464 (NULL == plugin->sockv4) )
466 if (NULL != plugin->select_task_v4)
467 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
468 plugin->select_task_v4
469 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
471 &xu_plugin_select_v4,
477 * (re)schedule IPv6-select tasks for this plugin.
479 * @param plugin plugin to reschedule
482 schedule_select_v6 (struct Plugin *plugin)
484 if ( (GNUNET_YES != plugin->enable_ipv6) ||
485 (NULL == plugin->sockv6) )
487 if (NULL != plugin->select_task_v6)
488 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
489 plugin->select_task_v6
490 = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
492 &xu_plugin_select_v6,
497 /* ******************* Address to string and back ***************** */
501 * Function called for a quick conversion of the binary address to
502 * a numeric address. Note that the caller must not free the
503 * address and that the next call to this function is allowed
504 * to override the address again.
507 * @param addr binary address (a `union XuAddress`)
508 * @param addrlen length of the @a addr
509 * @return string representing the same address
512 xu_address_to_string (void *cls,
516 static char rbuf[INET6_ADDRSTRLEN + 10];
517 char buf[INET6_ADDRSTRLEN];
521 const struct IPv4XuAddress *t4;
522 const struct IPv6XuAddress *t6;
534 if (addrlen == sizeof(struct IPv6XuAddress))
538 options = ntohl (t6->options);
539 port = ntohs (t6->u6_port);
543 else if (addrlen == sizeof(struct IPv4XuAddress))
547 options = ntohl (t4->options);
548 port = ntohs (t4->u4_port);
549 a4.s_addr = t4->ipv4_addr;
561 GNUNET_snprintf (rbuf,
575 * Function called to convert a string address to a binary address.
577 * @param cls closure (`struct Plugin *`)
578 * @param addr string address
579 * @param addrlen length of the address
580 * @param buf location to store the buffer
581 * @param added location to store the number of bytes in the buffer.
582 * If the function returns #GNUNET_SYSERR, its contents are undefined.
583 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
586 xu_string_to_address (void *cls,
592 struct sockaddr_storage socket_address;
599 /* Format tcp.options.address:port */
604 if ((NULL == addr) || (0 == addrlen))
607 return GNUNET_SYSERR;
609 if ('\0' != addr[addrlen - 1])
612 return GNUNET_SYSERR;
614 if (strlen (addr) + 1 != (size_t) addrlen)
617 return GNUNET_SYSERR;
619 plugin = GNUNET_strdup (addr);
620 optionstr = strchr (plugin, '.');
621 if (NULL == optionstr)
624 GNUNET_free (plugin);
625 return GNUNET_SYSERR;
629 options = atol (optionstr);
630 address = strchr (optionstr, '.');
634 GNUNET_free (plugin);
635 return GNUNET_SYSERR;
641 GNUNET_STRINGS_to_address_ip (address,
646 GNUNET_free (plugin);
647 return GNUNET_SYSERR;
651 switch (socket_address.ss_family)
655 struct IPv4XuAddress *u4;
656 const struct sockaddr_in *in4 = (const struct sockaddr_in *) &socket_address;
658 u4 = GNUNET_new (struct IPv4XuAddress);
659 u4->options = htonl (options);
660 u4->ipv4_addr = in4->sin_addr.s_addr;
661 u4->u4_port = in4->sin_port;
663 *added = sizeof (struct IPv4XuAddress);
668 struct IPv6XuAddress *u6;
669 const struct sockaddr_in6 *in6 = (const struct sockaddr_in6 *) &socket_address;
671 u6 = GNUNET_new (struct IPv6XuAddress);
672 u6->options = htonl (options);
673 u6->ipv6_addr = in6->sin6_addr;
674 u6->u6_port = in6->sin6_port;
676 *added = sizeof (struct IPv6XuAddress);
681 return GNUNET_SYSERR;
687 * Append our port and forward the result.
689 * @param cls a `struct PrettyPrinterContext *`
690 * @param hostname result from DNS resolver
693 append_port (void *cls,
694 const char *hostname)
696 struct PrettyPrinterContext *ppc = cls;
697 struct Plugin *plugin = ppc->plugin;
700 if (NULL == hostname)
702 /* Final call, done */
703 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
704 plugin->ppc_dll_tail,
706 ppc->resolver_handle = NULL;
707 ppc->asc (ppc->asc_cls,
713 if (GNUNET_YES == ppc->ipv6)
714 GNUNET_asprintf (&ret,
721 GNUNET_asprintf (&ret,
727 ppc->asc (ppc->asc_cls,
735 * Convert the transports address to a nice, human-readable format.
737 * @param cls closure with the `struct Plugin *`
738 * @param type name of the transport that generated the address
739 * @param addr one of the addresses of the host, NULL for the last address
740 * the specific address format depends on the transport;
741 * a `union XuAddress`
742 * @param addrlen length of the address
743 * @param numeric should (IP) addresses be displayed in numeric form?
744 * @param timeout after how long should we give up?
745 * @param asc function to call on each string
746 * @param asc_cls closure for @a asc
749 xu_plugin_address_pretty_printer (void *cls,
754 struct GNUNET_TIME_Relative timeout,
755 GNUNET_TRANSPORT_AddressStringCallback asc,
758 struct Plugin *plugin = cls;
759 struct PrettyPrinterContext *ppc;
760 const struct sockaddr *sb;
762 struct sockaddr_in a4;
763 struct sockaddr_in6 a6;
764 const struct IPv4XuAddress *u4;
765 const struct IPv6XuAddress *u6;
770 if (addrlen == sizeof(struct IPv6XuAddress))
776 a6.sin6_family = AF_INET6;
777 #if HAVE_SOCKADDR_IN_SIN_LEN
778 a6.sin6_len = sizeof (a6);
780 a6.sin6_port = u6->u6_port;
781 a6.sin6_addr = u6->ipv6_addr;
782 port = ntohs (u6->u6_port);
783 options = ntohl (u6->options);
784 sb = (const struct sockaddr *) &a6;
787 else if (addrlen == sizeof (struct IPv4XuAddress))
793 a4.sin_family = AF_INET;
794 #if HAVE_SOCKADDR_IN_SIN_LEN
795 a4.sin_len = sizeof (a4);
797 a4.sin_port = u4->u4_port;
798 a4.sin_addr.s_addr = u4->ipv4_addr;
799 port = ntohs (u4->u4_port);
800 options = ntohl (u4->options);
801 sb = (const struct sockaddr *) &a4;
806 /* invalid address */
816 ppc = GNUNET_new (struct PrettyPrinterContext);
817 ppc->plugin = plugin;
819 ppc->asc_cls = asc_cls;
821 ppc->options = options;
822 if (addrlen == sizeof (struct IPv6XuAddress))
823 ppc->ipv6 = GNUNET_YES;
825 ppc->ipv6 = GNUNET_NO;
826 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
827 plugin->ppc_dll_tail,
830 = GNUNET_RESOLVER_hostname_get (sb,
840 * Check if the given port is plausible (must be either our listen
841 * port or our advertised port). If it is neither, we return
844 * @param plugin global variables
845 * @param in_port port number to check
846 * @return #GNUNET_OK if port is either our open or advertised port
849 check_port (const struct Plugin *plugin,
852 if ( (plugin->port == in_port) ||
853 (plugin->aport == in_port) )
855 return GNUNET_SYSERR;
860 * Function that will be called to check if a binary address for this
861 * plugin is well-formed and corresponds to an address for THIS peer
862 * (as per our configuration). Naturally, if absolutely necessary,
863 * plugins can be a bit conservative in their answer, but in general
864 * plugins should make sure that the address does not redirect
865 * traffic to a 3rd party that might try to man-in-the-middle our
868 * @param cls closure, should be our handle to the Plugin
869 * @param addr pointer to a `union XuAddress`
870 * @param addrlen length of @a addr
871 * @return #GNUNET_OK if this is a plausible address for this peer
872 * and transport, #GNUNET_SYSERR if not
875 xu_plugin_check_address (void *cls,
879 struct Plugin *plugin = cls;
880 const struct IPv4XuAddress *v4;
881 const struct IPv6XuAddress *v6;
883 if (sizeof(struct IPv4XuAddress) == addrlen)
885 struct sockaddr_in s4;
887 v4 = (const struct IPv4XuAddress *) addr;
888 if (GNUNET_OK != check_port (plugin,
889 ntohs (v4->u4_port)))
890 return GNUNET_SYSERR;
891 memset (&s4, 0, sizeof (s4));
892 s4.sin_family = AF_INET;
893 #if HAVE_SOCKADDR_IN_SIN_LEN
894 s4.sin_len = sizeof (s4);
896 s4.sin_port = v4->u4_port;
897 s4.sin_addr.s_addr = v4->ipv4_addr;
900 GNUNET_NAT_test_address (plugin->nat,
902 sizeof (struct sockaddr_in)))
903 return GNUNET_SYSERR;
905 else if (sizeof(struct IPv6XuAddress) == addrlen)
907 struct sockaddr_in6 s6;
909 v6 = (const struct IPv6XuAddress *) addr;
910 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
911 return GNUNET_OK; /* plausible, if unlikely... */
912 memset (&s6, 0, sizeof (s6));
913 s6.sin6_family = AF_INET6;
914 #if HAVE_SOCKADDR_IN_SIN_LEN
915 s6.sin6_len = sizeof (s6);
917 s6.sin6_port = v6->u6_port;
918 s6.sin6_addr = v6->ipv6_addr;
921 GNUNET_NAT_test_address (plugin->nat,
923 sizeof(struct sockaddr_in6)))
924 return GNUNET_SYSERR;
929 return GNUNET_SYSERR;
936 * Our external IP address/port mapping has changed.
938 * @param cls closure, the `struct Plugin`
939 * @param add_remove #GNUNET_YES to mean the new public IP address,
940 * #GNUNET_NO to mean the previous (now invalid) one
941 * @param ac address class the address belongs to
942 * @param addr either the previous or the new public IP address
943 * @param addrlen actual length of the @a addr
946 xu_nat_port_map_callback (void *cls,
948 enum GNUNET_NAT_AddressClass ac,
949 const struct sockaddr *addr,
952 struct Plugin *plugin = cls;
953 struct GNUNET_HELLO_Address *address;
954 struct IPv4XuAddress u4;
955 struct IPv6XuAddress u6;
959 if (GNUNET_NAT_AC_LOOPBACK == ac)
961 if (GNUNET_NAT_AC_LAN == ac)
963 if (GNUNET_NAT_AC_LAN_PRIVATE == ac)
965 LOG (GNUNET_ERROR_TYPE_DEBUG,
966 (GNUNET_YES == add_remove)
967 ? "NAT notification to add address `%s'\n"
968 : "NAT notification to remove address `%s'\n",
971 /* convert 'address' to our internal format */
972 switch (addr->sa_family)
976 const struct sockaddr_in *i4;
978 GNUNET_assert (sizeof(struct sockaddr_in) == addrlen);
979 i4 = (const struct sockaddr_in *) addr;
980 if (0 == ntohs (i4->sin_port))
981 return; /* Port = 0 means unmapped, ignore these for XU. */
985 u4.options = htonl (plugin->myoptions);
986 u4.ipv4_addr = i4->sin_addr.s_addr;
987 u4.u4_port = i4->sin_port;
989 args = sizeof (struct IPv4XuAddress);
994 const struct sockaddr_in6 *i6;
996 GNUNET_assert (sizeof(struct sockaddr_in6) == addrlen);
997 i6 = (const struct sockaddr_in6 *) addr;
998 if (0 == ntohs (i6->sin6_port))
999 return; /* Port = 0 means unmapped, ignore these for XU. */
1003 u6.options = htonl (plugin->myoptions);
1004 u6.ipv6_addr = i6->sin6_addr;
1005 u6.u6_port = i6->sin6_port;
1007 args = sizeof (struct IPv6XuAddress);
1014 /* modify our published address list */
1015 /* TODO: use 'ac' here in the future... */
1016 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
1020 GNUNET_HELLO_ADDRESS_INFO_NONE);
1021 plugin->env->notify_address (plugin->env->cls,
1024 GNUNET_HELLO_address_free (address);
1028 /* ********************* Finding sessions ******************* */
1032 * Closure for #session_cmp_it().
1034 struct GNUNET_ATS_SessionCompareContext
1037 * Set to session matching the address.
1039 struct GNUNET_ATS_Session *res;
1042 * Address we are looking for.
1044 const struct GNUNET_HELLO_Address *address;
1049 * Find a session with a matching address.
1051 * @param cls the `struct GNUNET_ATS_SessionCompareContext *`
1052 * @param key peer identity (unused)
1053 * @param value the `struct GNUNET_ATS_Session *`
1054 * @return #GNUNET_NO if we found the session, #GNUNET_OK if not
1057 session_cmp_it (void *cls,
1058 const struct GNUNET_PeerIdentity *key,
1061 struct GNUNET_ATS_SessionCompareContext *cctx = cls;
1062 struct GNUNET_ATS_Session *s = value;
1065 if (0 == GNUNET_HELLO_address_cmp (s->address,
1068 GNUNET_assert (GNUNET_NO == s->in_destroy);
1077 * Locate an existing session the transport service is using to
1078 * send data to another peer. Performs some basic sanity checks
1079 * on the address and then tries to locate a matching session.
1081 * @param cls the plugin
1082 * @param address the address we should locate the session by
1083 * @return the session if it exists, or NULL if it is not found
1085 static struct GNUNET_ATS_Session *
1086 xu_plugin_lookup_session (void *cls,
1087 const struct GNUNET_HELLO_Address *address)
1089 struct Plugin *plugin = cls;
1090 const struct IPv6XuAddress *xu_a6;
1091 const struct IPv4XuAddress *xu_a4;
1092 struct GNUNET_ATS_SessionCompareContext cctx;
1094 if (NULL == address->address)
1099 if (sizeof(struct IPv4XuAddress) == address->address_length)
1101 if (NULL == plugin->sockv4)
1103 xu_a4 = (const struct IPv4XuAddress *) address->address;
1104 if (0 == xu_a4->u4_port)
1110 else if (sizeof(struct IPv6XuAddress) == address->address_length)
1112 if (NULL == plugin->sockv6)
1114 xu_a6 = (const struct IPv6XuAddress *) address->address;
1115 if (0 == xu_a6->u6_port)
1127 /* check if session already exists */
1128 cctx.address = address;
1130 LOG (GNUNET_ERROR_TYPE_DEBUG,
1131 "Looking for existing session for peer `%s' with address `%s'\n",
1132 GNUNET_i2s (&address->peer),
1133 xu_address_to_string (plugin,
1135 address->address_length));
1136 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1140 if (NULL == cctx.res)
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "Found existing session %p\n",
1149 /* ********************** Timeout ****************** */
1153 * Increment session timeout due to activity.
1155 * @param s session to reschedule timeout activity for
1158 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1160 if (GNUNET_YES == s->in_destroy)
1162 GNUNET_assert (NULL != s->timeout_task);
1163 s->timeout = GNUNET_TIME_relative_to_absolute (XU_SESSION_TIME_OUT);
1169 * Function that will be called whenever the transport service wants to
1170 * notify the plugin that a session is still active and in use and
1171 * therefore the session timeout for this session has to be updated
1173 * @param cls closure with the `struct Plugin`
1174 * @param peer which peer was the session for
1175 * @param session which session is being updated
1178 xu_plugin_update_session_timeout (void *cls,
1179 const struct GNUNET_PeerIdentity *peer,
1180 struct GNUNET_ATS_Session *session)
1182 struct Plugin *plugin = cls;
1185 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1192 /* Reschedule session timeout */
1193 reschedule_session_timeout (session);
1197 /* ************************* Sending ************************ */
1201 * We failed to transmit a message via XU. Generate
1202 * a descriptive error message.
1204 * @param plugin our plugin
1205 * @param sa target address we were trying to reach
1206 * @param slen number of bytes in @a sa
1207 * @param error the errno value returned from the sendto() call
1210 analyze_send_error (struct Plugin *plugin,
1211 const struct sockaddr *sa,
1215 enum GNUNET_ATS_Network_Type type;
1217 type = plugin->env->get_address_type (plugin->env->cls,
1220 if ( ( (GNUNET_ATS_NET_LAN == type) ||
1221 (GNUNET_ATS_NET_WAN == type) ) &&
1222 ( (ENETUNREACH == errno) ||
1223 (ENETDOWN == errno) ) )
1225 if (slen == sizeof (struct sockaddr_in))
1227 /* IPv4: "Network unreachable" or "Network down"
1229 * This indicates we do not have connectivity
1231 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1232 _("XU could not transmit message to `%s': "
1233 "Network seems down, please check your network configuration\n"),
1237 if (slen == sizeof (struct sockaddr_in6))
1239 /* IPv6: "Network unreachable" or "Network down"
1241 * This indicates that this system is IPv6 enabled, but does not
1242 * have a valid global IPv6 address assigned or we do not have
1245 LOG (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1246 _("XU could not transmit IPv6 message! "
1247 "Please check your network configuration and disable IPv6 if your "
1248 "connection does not have a global IPv6 address\n"));
1253 LOG (GNUNET_ERROR_TYPE_WARNING,
1254 "XU could not transmit message to `%s': `%s'\n",
1265 * Function that can be used by the transport service to transmit a
1266 * message using the plugin. Note that in the case of a peer
1267 * disconnecting, the continuation MUST be called prior to the
1268 * disconnect notification itself. This function will be called with
1269 * this peer's HELLO message to initiate a fresh connection to another
1272 * @param cls closure
1273 * @param s which session must be used
1274 * @param msgbuf the message to transmit
1275 * @param msgbuf_size number of bytes in @a msgbuf
1276 * @param priority how important is the message (most plugins will
1277 * ignore message priority and just FIFO)
1278 * @param to how long to wait at most for the transmission (does not
1279 * require plugins to discard the message after the timeout,
1280 * just advisory for the desired delay; most plugins will ignore
1282 * @param cont continuation to call once the message has
1283 * been transmitted (or if the transport is ready
1284 * for the next transmission call; or if the
1285 * peer disconnected...); can be NULL
1286 * @param cont_cls closure for @a cont
1287 * @return number of bytes used (on the physical network, with overheads);
1288 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1289 * and does NOT mean that the message was not transmitted (DV)
1292 xu_plugin_send (void *cls,
1293 struct GNUNET_ATS_Session *s,
1296 unsigned int priority,
1297 struct GNUNET_TIME_Relative to,
1298 GNUNET_TRANSPORT_TransmitContinuation cont,
1301 struct Plugin *plugin = cls;
1302 size_t xumlen = msgbuf_size + sizeof(struct XUMessage);
1303 struct XUMessage *xu;
1304 char mbuf[xumlen] GNUNET_ALIGN;
1307 const struct sockaddr *a;
1308 const struct IPv4XuAddress *u4;
1309 struct sockaddr_in a4;
1310 const struct IPv6XuAddress *u6;
1311 struct sockaddr_in6 a6;
1312 struct GNUNET_NETWORK_Handle *sock;
1316 if ( (sizeof(struct IPv6XuAddress) == s->address->address_length) &&
1317 (NULL == plugin->sockv6) )
1318 return GNUNET_SYSERR;
1319 if ( (sizeof(struct IPv4XuAddress) == s->address->address_length) &&
1320 (NULL == plugin->sockv4) )
1321 return GNUNET_SYSERR;
1322 if (xumlen >= GNUNET_MAX_MESSAGE_SIZE)
1325 return GNUNET_SYSERR;
1328 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessions,
1333 return GNUNET_SYSERR;
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "XU transmits %u-byte message to `%s' using address `%s'\n",
1338 GNUNET_i2s (&s->target),
1339 xu_address_to_string (plugin,
1340 s->address->address,
1341 s->address->address_length));
1342 xu = (struct XUMessage *) mbuf;
1343 xu->header.size = htons (xumlen);
1344 xu->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_XU_MESSAGE);
1345 xu->reserved = htonl (0);
1346 xu->sender = *plugin->env->my_identity;
1347 GNUNET_memcpy (&xu[1],
1351 if (sizeof (struct IPv4XuAddress) == s->address->address_length)
1353 u4 = s->address->address;
1357 a4.sin_family = AF_INET;
1358 #if HAVE_SOCKADDR_IN_SIN_LEN
1359 a4.sin_len = sizeof (a4);
1361 a4.sin_port = u4->u4_port;
1362 a4.sin_addr.s_addr = u4->ipv4_addr;
1363 a = (const struct sockaddr *) &a4;
1365 sock = plugin->sockv4;
1367 else if (sizeof (struct IPv6XuAddress) == s->address->address_length)
1369 u6 = s->address->address;
1373 a6.sin6_family = AF_INET6;
1374 #if HAVE_SOCKADDR_IN_SIN_LEN
1375 a6.sin6_len = sizeof (a6);
1377 a6.sin6_port = u6->u6_port;
1378 a6.sin6_addr = u6->ipv6_addr;
1379 a = (const struct sockaddr *) &a6;
1381 sock = plugin->sockv6;
1386 return GNUNET_SYSERR;
1389 sent = GNUNET_NETWORK_socket_sendto (sock,
1394 s->last_transmit_time
1395 = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_get (),
1396 s->last_transmit_time);
1398 if (GNUNET_SYSERR == sent)
1401 analyze_send_error (plugin,
1405 GNUNET_STATISTICS_update (plugin->env->stats,
1406 "# XU, total, bytes, sent, failure",
1409 GNUNET_STATISTICS_update (plugin->env->stats,
1410 "# XU, total, messages, sent, failure",
1413 return GNUNET_SYSERR;
1416 LOG (GNUNET_ERROR_TYPE_DEBUG,
1417 "XU transmitted %u-byte message to `%s' `%s' (%d: %s)\n",
1418 (unsigned int) (msgbuf_size),
1419 GNUNET_i2s (&s->target),
1423 (sent < 0) ? STRERROR (errno) : "ok");
1424 GNUNET_STATISTICS_update (plugin->env->stats,
1425 "# XU, total, bytes, sent, success",
1428 GNUNET_STATISTICS_update (plugin->env->stats,
1429 "# XU, total, messages, sent, success",
1437 notify_session_monitor (s->plugin,
1439 GNUNET_TRANSPORT_SS_UPDATE);
1444 /* ********************** Receiving ********************** */
1448 * Functions with this signature are called whenever we need to close
1449 * a session due to a disconnect or failure to establish a connection.
1451 * @param cls closure with the `struct Plugin`
1452 * @param s session to close down
1453 * @return #GNUNET_OK on success
1456 xu_disconnect_session (void *cls,
1457 struct GNUNET_ATS_Session *s)
1459 struct Plugin *plugin = cls;
1461 GNUNET_assert (GNUNET_YES != s->in_destroy);
1462 LOG (GNUNET_ERROR_TYPE_DEBUG,
1463 "Session %p to peer `%s' at address %s ended\n",
1465 GNUNET_i2s (&s->target),
1466 xu_address_to_string (plugin,
1467 s->address->address,
1468 s->address->address_length));
1469 if (NULL != s->timeout_task)
1471 GNUNET_SCHEDULER_cancel (s->timeout_task);
1472 s->timeout_task = NULL;
1474 GNUNET_assert (GNUNET_YES ==
1475 GNUNET_CONTAINER_multipeermap_remove (plugin->sessions,
1478 s->in_destroy = GNUNET_YES;
1479 notify_session_monitor (s->plugin,
1481 GNUNET_TRANSPORT_SS_DONE);
1482 plugin->env->session_end (plugin->env->cls,
1485 GNUNET_STATISTICS_set (plugin->env->stats,
1486 "# XU sessions active",
1487 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1496 * Message tokenizer has broken up an incomming message. Pass it on
1499 * @param cls the `struct GNUNET_ATS_Session *`
1500 * @param hdr the actual message
1501 * @return #GNUNET_OK (always)
1504 process_inbound_tokenized_messages (void *cls,
1505 const struct GNUNET_MessageHeader *hdr)
1507 struct GNUNET_ATS_Session *session = cls;
1508 struct Plugin *plugin = session->plugin;
1510 if (GNUNET_YES == session->in_destroy)
1512 reschedule_session_timeout (session);
1513 session->flow_delay_for_other_peer
1514 = plugin->env->receive (plugin->env->cls,
1523 * Destroy a session, plugin is being unloaded.
1525 * @param cls the `struct Plugin`
1526 * @param key hash of public key of target peer
1527 * @param value a `struct PeerSession *` to clean up
1528 * @return #GNUNET_OK (continue to iterate)
1531 disconnect_and_free_it (void *cls,
1532 const struct GNUNET_PeerIdentity *key,
1535 struct Plugin *plugin = cls;
1538 xu_disconnect_session (plugin,
1545 * Disconnect from a remote node. Clean up session if we have one for
1548 * @param cls closure for this call (should be handle to Plugin)
1549 * @param target the peeridentity of the peer to disconnect
1550 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the operation failed
1553 xu_disconnect (void *cls,
1554 const struct GNUNET_PeerIdentity *target)
1556 struct Plugin *plugin = cls;
1558 LOG (GNUNET_ERROR_TYPE_DEBUG,
1559 "Disconnecting from peer `%s'\n",
1560 GNUNET_i2s (target));
1561 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessions,
1563 &disconnect_and_free_it,
1569 * Session was idle, so disconnect it.
1571 * @param cls the `struct GNUNET_ATS_Session` to time out
1574 session_timeout (void *cls)
1576 struct GNUNET_ATS_Session *s = cls;
1577 struct Plugin *plugin = s->plugin;
1578 struct GNUNET_TIME_Relative left;
1580 s->timeout_task = NULL;
1581 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1582 if (left.rel_value_us > 0)
1584 /* not actually our turn yet, but let's at least update
1585 the monitor, it may think we're about to die ... */
1586 notify_session_monitor (s->plugin,
1588 GNUNET_TRANSPORT_SS_UPDATE);
1589 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1594 LOG (GNUNET_ERROR_TYPE_DEBUG,
1595 "Session %p was idle for %s, disconnecting\n",
1597 GNUNET_STRINGS_relative_time_to_string (XU_SESSION_TIME_OUT,
1599 /* call session destroy function */
1600 xu_disconnect_session (plugin,
1606 * Allocate a new session for the given endpoint address.
1607 * Note that this function does not inform the service
1608 * of the new session, this is the responsibility of the
1609 * caller (if needed).
1611 * @param cls the `struct Plugin`
1612 * @param address address of the other peer to use
1613 * @param network_type network type the address belongs to
1614 * @return NULL on error, otherwise session handle
1616 static struct GNUNET_ATS_Session *
1617 xu_plugin_create_session (void *cls,
1618 const struct GNUNET_HELLO_Address *address,
1619 enum GNUNET_ATS_Network_Type network_type)
1621 struct Plugin *plugin = cls;
1622 struct GNUNET_ATS_Session *s;
1624 s = GNUNET_new (struct GNUNET_ATS_Session);
1625 s->mst = GNUNET_MST_create (&process_inbound_tokenized_messages,
1628 s->address = GNUNET_HELLO_address_copy (address);
1629 s->target = address->peer;
1630 s->last_transmit_time = GNUNET_TIME_absolute_get ();
1631 s->last_expected_ack_delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
1633 s->last_expected_msg_delay = GNUNET_TIME_UNIT_MILLISECONDS;
1634 s->flow_delay_from_other_peer = GNUNET_TIME_UNIT_ZERO;
1635 s->flow_delay_for_other_peer = GNUNET_TIME_UNIT_ZERO;
1636 s->timeout = GNUNET_TIME_relative_to_absolute (XU_SESSION_TIME_OUT);
1637 s->timeout_task = GNUNET_SCHEDULER_add_delayed (XU_SESSION_TIME_OUT,
1640 s->scope = network_type;
1642 LOG (GNUNET_ERROR_TYPE_DEBUG,
1643 "Creating new session %p for peer `%s' address `%s'\n",
1645 GNUNET_i2s (&address->peer),
1646 xu_address_to_string (plugin,
1648 address->address_length));
1649 GNUNET_assert (GNUNET_OK ==
1650 GNUNET_CONTAINER_multipeermap_put (plugin->sessions,
1653 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1654 GNUNET_STATISTICS_set (plugin->env->stats,
1655 "# XU sessions active",
1656 GNUNET_CONTAINER_multipeermap_size (plugin->sessions),
1658 notify_session_monitor (plugin,
1660 GNUNET_TRANSPORT_SS_INIT);
1666 * Creates a new outbound session the transport service will use to
1667 * send data to the peer.
1669 * @param cls the `struct Plugin *`
1670 * @param address the address
1671 * @return the session or NULL of max connections exceeded
1673 static struct GNUNET_ATS_Session *
1674 xu_plugin_get_session (void *cls,
1675 const struct GNUNET_HELLO_Address *address)
1677 struct Plugin *plugin = cls;
1678 struct GNUNET_ATS_Session *s;
1679 enum GNUNET_ATS_Network_Type network_type = GNUNET_ATS_NET_UNSPECIFIED;
1680 const struct IPv4XuAddress *xu_v4;
1681 const struct IPv6XuAddress *xu_v6;
1683 if (NULL == address)
1688 if ( (address->address_length != sizeof(struct IPv4XuAddress)) &&
1689 (address->address_length != sizeof(struct IPv6XuAddress)) )
1691 GNUNET_break_op (0);
1694 if (NULL != (s = xu_plugin_lookup_session (cls,
1698 /* need to create new session */
1699 if (sizeof (struct IPv4XuAddress) == address->address_length)
1701 struct sockaddr_in v4;
1703 xu_v4 = (const struct IPv4XuAddress *) address->address;
1704 memset (&v4, '\0', sizeof (v4));
1705 v4.sin_family = AF_INET;
1706 #if HAVE_SOCKADDR_IN_SIN_LEN
1707 v4.sin_len = sizeof (struct sockaddr_in);
1709 v4.sin_port = xu_v4->u4_port;
1710 v4.sin_addr.s_addr = xu_v4->ipv4_addr;
1711 network_type = plugin->env->get_address_type (plugin->env->cls,
1712 (const struct sockaddr *) &v4,
1715 if (sizeof (struct IPv6XuAddress) == address->address_length)
1717 struct sockaddr_in6 v6;
1719 xu_v6 = (const struct IPv6XuAddress *) address->address;
1720 memset (&v6, '\0', sizeof (v6));
1721 v6.sin6_family = AF_INET6;
1722 #if HAVE_SOCKADDR_IN_SIN_LEN
1723 v6.sin6_len = sizeof (struct sockaddr_in6);
1725 v6.sin6_port = xu_v6->u6_port;
1726 v6.sin6_addr = xu_v6->ipv6_addr;
1727 network_type = plugin->env->get_address_type (plugin->env->cls,
1728 (const struct sockaddr *) &v6,
1731 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
1732 return xu_plugin_create_session (cls,
1739 * We've received a XU Message. Process it (pass contents to main service).
1741 * @param plugin plugin context
1742 * @param msg the message
1743 * @param xu_addr sender address
1744 * @param xu_addr_len number of bytes in @a xu_addr
1745 * @param network_type network type the address belongs to
1748 process_xu_message (struct Plugin *plugin,
1749 const struct XUMessage *msg,
1750 const union XuAddress *xu_addr,
1752 enum GNUNET_ATS_Network_Type network_type)
1754 struct GNUNET_ATS_Session *s;
1755 struct GNUNET_HELLO_Address *address;
1757 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != network_type);
1758 if (0 != ntohl (msg->reserved))
1763 if (ntohs (msg->header.size)
1764 < sizeof(struct GNUNET_MessageHeader) + sizeof(struct XUMessage))
1770 address = GNUNET_HELLO_address_allocate (&msg->sender,
1774 GNUNET_HELLO_ADDRESS_INFO_NONE);
1776 (s = xu_plugin_lookup_session (plugin,
1779 s = xu_plugin_create_session (plugin,
1782 plugin->env->session_start (plugin->env->cls,
1786 notify_session_monitor (plugin,
1788 GNUNET_TRANSPORT_SS_UP);
1790 GNUNET_free (address);
1793 GNUNET_MST_from_buffer (s->mst,
1794 (const char *) &msg[1],
1795 ntohs (msg->header.size) - sizeof(struct XUMessage),
1799 if ( (0 == s->rc) &&
1800 (GNUNET_YES == s->in_destroy) )
1806 * Read and process a message from the given socket.
1808 * @param plugin the overall plugin
1809 * @param rsock socket to read from
1812 xu_select_read (struct Plugin *plugin,
1813 struct GNUNET_NETWORK_Handle *rsock)
1816 struct sockaddr_storage addr;
1817 char buf[65536] GNUNET_ALIGN;
1819 const struct GNUNET_MessageHeader *msg;
1820 struct IPv4XuAddress v4;
1821 struct IPv6XuAddress v6;
1822 const struct sockaddr *sa;
1823 const struct sockaddr_in *sa4;
1824 const struct sockaddr_in6 *sa6;
1825 const union XuAddress *int_addr;
1826 size_t int_addr_len;
1827 enum GNUNET_ATS_Network_Type network_type;
1829 fromlen = sizeof (addr);
1833 size = GNUNET_NETWORK_socket_recvfrom (rsock,
1836 (struct sockaddr *) &addr,
1838 sa = (const struct sockaddr *) &addr;
1840 /* On SOCK_DGRAM XU sockets recvfrom might fail with a
1841 * WSAECONNRESET error to indicate that previous sendto() (yes, sendto!)
1842 * on this socket has failed.
1844 * WSAECONNRESET - The virtual circuit was reset by the remote side
1845 * executing a hard or abortive close. The application should close
1846 * the socket; it is no longer usable. On a XU-datagram socket this
1847 * error indicates a previous send operation resulted in an ICMP Port
1848 * Unreachable message.
1850 if ( (-1 == size) &&
1851 (ECONNRESET == errno) )
1856 LOG (GNUNET_ERROR_TYPE_DEBUG,
1857 "XU failed to receive data: %s\n",
1859 /* Connection failure or something. Not a protocol violation. */
1863 /* Check if this is a STUN packet */
1865 GNUNET_NAT_stun_handle_packet (plugin->nat,
1866 (const struct sockaddr *) &addr,
1870 return; /* was STUN, do not process further */
1872 if (((size_t) size) < sizeof(struct GNUNET_MessageHeader))
1874 LOG (GNUNET_ERROR_TYPE_WARNING,
1875 "XU got %u bytes from %s, which is not enough for a GNUnet message header\n",
1876 (unsigned int ) size,
1879 /* _MAY_ be a connection failure (got partial message) */
1880 /* But it _MAY_ also be that the other side uses non-GNUnet protocol. */
1881 GNUNET_break_op (0);
1885 msg = (const struct GNUNET_MessageHeader *) buf;
1886 LOG (GNUNET_ERROR_TYPE_DEBUG,
1887 "XU received %u-byte message from `%s' type %u\n",
1888 (unsigned int) size,
1892 if (size != ntohs (msg->size))
1894 LOG (GNUNET_ERROR_TYPE_WARNING,
1895 "XU malformed message (size %u) header from %s\n",
1896 (unsigned int) size,
1899 GNUNET_break_op (0);
1902 GNUNET_STATISTICS_update (plugin->env->stats,
1903 "# XU, total bytes received",
1906 network_type = plugin->env->get_address_type (plugin->env->cls,
1909 switch (sa->sa_family)
1912 sa4 = (const struct sockaddr_in *) &addr;
1914 v4.ipv4_addr = sa4->sin_addr.s_addr;
1915 v4.u4_port = sa4->sin_port;
1916 int_addr = (union XuAddress *) &v4;
1917 int_addr_len = sizeof (v4);
1920 sa6 = (const struct sockaddr_in6 *) &addr;
1922 v6.ipv6_addr = sa6->sin6_addr;
1923 v6.u6_port = sa6->sin6_port;
1924 int_addr = (union XuAddress *) &v6;
1925 int_addr_len = sizeof (v6);
1932 switch (ntohs (msg->type))
1934 case GNUNET_MESSAGE_TYPE_TRANSPORT_XU_MESSAGE:
1935 if (ntohs (msg->size) < sizeof(struct XUMessage))
1940 process_xu_message (plugin,
1941 (const struct XUMessage *) msg,
1953 /* ***************** Event loop (part 2) *************** */
1957 * We have been notified that our readset has something to read. We don't
1958 * know which socket needs to be read, so we have to check each one
1959 * Then reschedule this function to be called again once more is available.
1961 * @param cls the plugin handle
1964 xu_plugin_select_v4 (void *cls)
1966 struct Plugin *plugin = cls;
1967 const struct GNUNET_SCHEDULER_TaskContext *tc;
1969 plugin->select_task_v4 = NULL;
1970 if (NULL == plugin->sockv4)
1972 tc = GNUNET_SCHEDULER_get_task_context ();
1973 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1974 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
1976 xu_select_read (plugin,
1978 schedule_select_v4 (plugin);
1983 * We have been notified that our readset has something to read. We don't
1984 * know which socket needs to be read, so we have to check each one
1985 * Then reschedule this function to be called again once more is available.
1987 * @param cls the plugin handle
1990 xu_plugin_select_v6 (void *cls)
1992 struct Plugin *plugin = cls;
1993 const struct GNUNET_SCHEDULER_TaskContext *tc;
1995 plugin->select_task_v6 = NULL;
1996 if (NULL == plugin->sockv6)
1998 tc = GNUNET_SCHEDULER_get_task_context ();
1999 if ( (0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
2000 (GNUNET_NETWORK_fdset_isset (tc->read_ready,
2002 xu_select_read (plugin,
2004 schedule_select_v6 (plugin);
2008 /* ******************* Initialization *************** */
2012 * Setup the XU sockets (for IPv4 and IPv6) for the plugin.
2014 * @param plugin the plugin to initialize
2015 * @param bind_v6 IPv6 address to bind to (can be NULL, for 'any')
2016 * @param bind_v4 IPv4 address to bind to (can be NULL, for 'any')
2017 * @return number of sockets that were successfully bound
2020 setup_sockets (struct Plugin *plugin,
2021 const struct sockaddr_in6 *bind_v6,
2022 const struct sockaddr_in *bind_v4)
2025 unsigned int sockets_created = 0;
2026 struct sockaddr_in6 server_addrv6;
2027 struct sockaddr_in server_addrv4;
2028 const struct sockaddr *server_addr;
2029 const struct sockaddr *addrs[2];
2030 socklen_t addrlens[2];
2034 /* Create IPv6 socket */
2036 if (GNUNET_YES == plugin->enable_ipv6)
2038 plugin->sockv6 = GNUNET_NETWORK_socket_create (PF_INET6,
2041 if (NULL == plugin->sockv6)
2043 LOG (GNUNET_ERROR_TYPE_INFO,
2044 _("Disabling IPv6 since it is not supported on this system!\n"));
2045 plugin->enable_ipv6 = GNUNET_NO;
2049 memset (&server_addrv6,
2051 sizeof(struct sockaddr_in6));
2052 #if HAVE_SOCKADDR_IN_SIN_LEN
2053 server_addrv6.sin6_len = sizeof (struct sockaddr_in6);
2055 server_addrv6.sin6_family = AF_INET6;
2056 if (NULL != bind_v6)
2057 server_addrv6.sin6_addr = bind_v6->sin6_addr;
2059 server_addrv6.sin6_addr = in6addr_any;
2061 if (0 == plugin->port) /* autodetect */
2062 server_addrv6.sin6_port
2063 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2067 server_addrv6.sin6_port = htons (plugin->port);
2068 addrlen = sizeof (struct sockaddr_in6);
2069 server_addr = (const struct sockaddr *) &server_addrv6;
2074 LOG(GNUNET_ERROR_TYPE_DEBUG,
2075 "Binding to IPv6 `%s'\n",
2076 GNUNET_a2s (server_addr,
2080 GNUNET_NETWORK_socket_bind (plugin->sockv6,
2085 if (0 != plugin->port)
2087 tries = 10; /* fail immediately */
2088 break; /* bind failed on specific port */
2091 server_addrv6.sin6_port
2092 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2099 GNUNET_NETWORK_socket_close (plugin->sockv6);
2100 plugin->enable_ipv6 = GNUNET_NO;
2101 plugin->sockv6 = NULL;
2105 plugin->port = ntohs (server_addrv6.sin6_port);
2107 if (NULL != plugin->sockv6)
2109 LOG (GNUNET_ERROR_TYPE_DEBUG,
2110 "IPv6 XU socket created listinging at %s\n",
2111 GNUNET_a2s (server_addr,
2113 addrs[sockets_created] = server_addr;
2114 addrlens[sockets_created] = addrlen;
2119 LOG (GNUNET_ERROR_TYPE_WARNING,
2120 _("Failed to bind XU socket to %s: %s\n"),
2121 GNUNET_a2s (server_addr,
2128 /* Create IPv4 socket */
2130 plugin->sockv4 = GNUNET_NETWORK_socket_create (PF_INET,
2133 if (NULL == plugin->sockv4)
2135 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
2137 LOG (GNUNET_ERROR_TYPE_INFO,
2138 _("Disabling IPv4 since it is not supported on this system!\n"));
2139 plugin->enable_ipv4 = GNUNET_NO;
2143 memset (&server_addrv4,
2145 sizeof(struct sockaddr_in));
2146 #if HAVE_SOCKADDR_IN_SIN_LEN
2147 server_addrv4.sin_len = sizeof (struct sockaddr_in);
2149 server_addrv4.sin_family = AF_INET;
2150 if (NULL != bind_v4)
2151 server_addrv4.sin_addr = bind_v4->sin_addr;
2153 server_addrv4.sin_addr.s_addr = INADDR_ANY;
2155 if (0 == plugin->port)
2157 server_addrv4.sin_port
2158 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2162 server_addrv4.sin_port = htons (plugin->port);
2164 addrlen = sizeof (struct sockaddr_in);
2165 server_addr = (const struct sockaddr *) &server_addrv4;
2170 LOG (GNUNET_ERROR_TYPE_DEBUG,
2171 "Binding to IPv4 `%s'\n",
2172 GNUNET_a2s (server_addr,
2177 GNUNET_NETWORK_socket_bind (plugin->sockv4,
2182 if (0 != plugin->port)
2184 tries = 10; /* fail */
2185 break; /* bind failed on specific port */
2189 server_addrv4.sin_port
2190 = htons (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_STRONG,
2197 GNUNET_NETWORK_socket_close (plugin->sockv4);
2198 plugin->enable_ipv4 = GNUNET_NO;
2199 plugin->sockv4 = NULL;
2203 plugin->port = ntohs (server_addrv4.sin_port);
2206 if (NULL != plugin->sockv4)
2208 LOG (GNUNET_ERROR_TYPE_DEBUG,
2209 "IPv4 socket created on port %s\n",
2210 GNUNET_a2s (server_addr,
2212 addrs[sockets_created] = server_addr;
2213 addrlens[sockets_created] = addrlen;
2218 LOG (GNUNET_ERROR_TYPE_ERROR,
2219 _("Failed to bind XU socket to %s: %s\n"),
2220 GNUNET_a2s (server_addr,
2226 if (0 == sockets_created)
2228 LOG (GNUNET_ERROR_TYPE_WARNING,
2229 _("Failed to open XU sockets\n"));
2230 return 0; /* No sockets created, return */
2232 schedule_select_v4 (plugin);
2233 schedule_select_v6 (plugin);
2234 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2240 &xu_nat_port_map_callback,
2243 return sockets_created;
2248 * The exported method. Makes the core api available via a global and
2249 * returns the xu transport API.
2251 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
2252 * @return our `struct GNUNET_TRANSPORT_PluginFunctions`
2255 libgnunet_plugin_transport_xu_init (void *cls)
2257 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2258 struct GNUNET_TRANSPORT_PluginFunctions *api;
2260 unsigned long long port;
2261 unsigned long long aport;
2263 char *bind4_address;
2264 char *bind6_address;
2265 struct sockaddr_in server_addrv4;
2266 struct sockaddr_in6 server_addrv6;
2271 if (NULL == env->receive)
2273 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2274 initialze the plugin or the API */
2275 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2277 api->address_pretty_printer = &xu_plugin_address_pretty_printer;
2278 api->address_to_string = &xu_address_to_string;
2279 api->string_to_address = &xu_string_to_address;
2283 /* Get port number: port == 0 : autodetect a port,
2284 * > 0 : use this port, not given : 2086 default */
2286 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2293 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2296 _("must be in [0,65535]"));
2300 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2307 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2310 _("must be in [0,65535]"));
2315 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2318 enable_v6 = GNUNET_NO;
2320 enable_v6 = GNUNET_YES;
2322 have_bind4 = GNUNET_NO;
2323 memset (&server_addrv4,
2325 sizeof (server_addrv4));
2327 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2332 LOG (GNUNET_ERROR_TYPE_DEBUG,
2333 "Binding XU plugin to specific address: `%s'\n",
2335 if (1 != inet_pton (AF_INET,
2337 &server_addrv4.sin_addr))
2339 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2342 _("must be valid IPv4 address"));
2343 GNUNET_free (bind4_address);
2346 have_bind4 = GNUNET_YES;
2348 GNUNET_free_non_null (bind4_address);
2349 have_bind6 = GNUNET_NO;
2350 memset (&server_addrv6,
2352 sizeof (server_addrv6));
2354 GNUNET_CONFIGURATION_get_value_string (env->cfg,
2359 LOG (GNUNET_ERROR_TYPE_DEBUG,
2360 "Binding xu plugin to specific address: `%s'\n",
2362 if (1 != inet_pton (AF_INET6,
2364 &server_addrv6.sin6_addr))
2366 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
2369 _("must be valid IPv6 address"));
2370 GNUNET_free (bind6_address);
2373 have_bind6 = GNUNET_YES;
2375 GNUNET_free_non_null (bind6_address);
2377 p = GNUNET_new (struct Plugin);
2380 p->enable_ipv6 = enable_v6;
2381 p->enable_ipv4 = GNUNET_YES; /* default */
2383 p->sessions = GNUNET_CONTAINER_multipeermap_create (16,
2385 res = setup_sockets (p,
2386 (GNUNET_YES == have_bind6) ? &server_addrv6 : NULL,
2387 (GNUNET_YES == have_bind4) ? &server_addrv4 : NULL);
2389 ( (NULL == p->sockv4) &&
2390 (NULL == p->sockv6) ) )
2392 LOG (GNUNET_ERROR_TYPE_ERROR,
2393 _("Failed to create XU network sockets\n"));
2394 GNUNET_CONTAINER_multipeermap_destroy (p->sessions);
2396 GNUNET_NAT_unregister (p->nat);
2401 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2403 api->disconnect_session = &xu_disconnect_session;
2404 api->query_keepalive_factor = &xu_query_keepalive_factor;
2405 api->disconnect_peer = &xu_disconnect;
2406 api->address_pretty_printer = &xu_plugin_address_pretty_printer;
2407 api->address_to_string = &xu_address_to_string;
2408 api->string_to_address = &xu_string_to_address;
2409 api->check_address = &xu_plugin_check_address;
2410 api->get_session = &xu_plugin_get_session;
2411 api->send = &xu_plugin_send;
2412 api->get_network = &xu_plugin_get_network;
2413 api->get_network_for_address = &xu_plugin_get_network_for_address;
2414 api->update_session_timeout = &xu_plugin_update_session_timeout;
2415 api->setup_monitor = &xu_plugin_setup_monitor;
2421 * The exported method. Makes the core api available via a global and
2422 * returns the xu transport API.
2424 * @param cls our `struct GNUNET_TRANSPORT_PluginEnvironment`
2428 libgnunet_plugin_transport_xu_done (void *cls)
2430 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2431 struct Plugin *plugin = api->cls;
2432 struct PrettyPrinterContext *cur;
2439 if (NULL != plugin->select_task_v4)
2441 GNUNET_SCHEDULER_cancel (plugin->select_task_v4);
2442 plugin->select_task_v4 = NULL;
2444 if (NULL != plugin->select_task_v6)
2446 GNUNET_SCHEDULER_cancel (plugin->select_task_v6);
2447 plugin->select_task_v6 = NULL;
2449 if (NULL != plugin->sockv4)
2451 GNUNET_break (GNUNET_OK ==
2452 GNUNET_NETWORK_socket_close (plugin->sockv4));
2453 plugin->sockv4 = NULL;
2455 if (NULL != plugin->sockv6)
2457 GNUNET_break (GNUNET_OK ==
2458 GNUNET_NETWORK_socket_close (plugin->sockv6));
2459 plugin->sockv6 = NULL;
2461 if (NULL != plugin->nat)
2463 GNUNET_NAT_unregister (plugin->nat);
2466 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessions);
2468 while (NULL != (cur = plugin->ppc_dll_head))
2471 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
2472 plugin->ppc_dll_tail,
2474 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2475 if (NULL != cur->timeout_task)
2477 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2478 cur->timeout_task = NULL;
2482 GNUNET_free (plugin);
2487 /* end of plugin_transport_xu.c */