2 This file is part of GNUnet
3 Copyright (C) 2002--2015 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_constants.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_resolver_service.h"
32 #include "gnunet_signatures.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-tcp",__VA_ARGS__)
40 #define PLUGIN_NAME "tcp"
43 * How long until we give up on establishing an NAT connection?
46 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
48 GNUNET_NETWORK_STRUCT_BEGIN
52 * Initial handshake message for a session.
57 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
59 struct GNUNET_MessageHeader header;
62 * Identity of the node connecting (TCP client)
64 struct GNUNET_PeerIdentity clientIdentity;
69 * Basically a WELCOME message, but with the purpose
70 * of giving the waiting peer a client handle to use
72 struct TCP_NAT_ProbeMessage
75 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
77 struct GNUNET_MessageHeader header;
80 * Identity of the sender of the message.
82 struct GNUNET_PeerIdentity clientIdentity;
85 GNUNET_NETWORK_STRUCT_END
88 * Context for sending a NAT probe via TCP.
90 struct TCPProbeContext
94 * Active probes are kept in a DLL.
96 struct TCPProbeContext *next;
99 * Active probes are kept in a DLL.
101 struct TCPProbeContext *prev;
106 struct GNUNET_CONNECTION_Handle *sock;
109 * Message to be sent.
111 struct TCP_NAT_ProbeMessage message;
114 * Handle to the transmission.
116 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
119 * Transport plugin handle.
121 struct Plugin *plugin;
125 * Bits in the `options` field of TCP addresses.
127 enum TcpAddressOptions
133 TCP_OPTIONS_NONE = 0,
136 * See #HTTP_OPTIONS_VERIFY_CERTIFICATE.
138 TCP_OPTIONS_RESERVED = 1,
141 * Enable TCP Stealth-style port knocking.
143 TCP_OPTIONS_TCP_STEALTH = 2
146 GNUNET_NETWORK_STRUCT_BEGIN
149 * Network format for IPv4 addresses.
151 struct IPv4TcpAddress
154 * Optional options and flags for this address,
155 * see `enum TcpAddressOptions`
157 uint32_t options GNUNET_PACKED;
160 * IPv4 address, in network byte order.
162 uint32_t ipv4_addr GNUNET_PACKED;
165 * Port number, in network byte order.
167 uint16_t t4_port GNUNET_PACKED;
172 * Network format for IPv6 addresses.
174 struct IPv6TcpAddress
177 * Optional flags for this address
178 * see `enum TcpAddressOptions`
180 uint32_t options GNUNET_PACKED;
185 struct in6_addr ipv6_addr GNUNET_PACKED;
188 * Port number, in network byte order.
190 uint16_t t6_port GNUNET_PACKED;
193 GNUNET_NETWORK_STRUCT_END
196 * Encapsulation of all of the state of the plugin.
201 * Information kept for each message that is yet to
204 struct PendingMessage
208 * This is a doubly-linked list.
210 struct PendingMessage *next;
213 * This is a doubly-linked list.
215 struct PendingMessage *prev;
218 * The pending message
223 * Continuation function to call once the message
224 * has been sent. Can be NULL if there is no
225 * continuation to call.
227 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
230 * Closure for @e transmit_cont.
232 void *transmit_cont_cls;
235 * Timeout value for the pending message.
237 struct GNUNET_TIME_Absolute timeout;
240 * So that the gnunet-service-transport can group messages together,
241 * these pending messages need to accept a message buffer and size
242 * instead of just a `struct GNUNET_MessageHeader`.
249 * Session handle for TCP connections.
251 struct GNUNET_ATS_Session
254 * To whom are we talking to (set to our identity
255 * if we are still waiting for the welcome message)
257 struct GNUNET_PeerIdentity target;
260 * Pointer to the global plugin struct.
262 struct Plugin *plugin;
265 * The client (used to identify this connection)
267 struct GNUNET_SERVER_Client *client;
270 * Task cleaning up a NAT client connection establishment attempt;
272 struct GNUNET_SCHEDULER_Task *nat_connection_timeout;
275 * Messages currently pending for transmission
276 * to this peer, if any.
278 struct PendingMessage *pending_messages_head;
281 * Messages currently pending for transmission
282 * to this peer, if any.
284 struct PendingMessage *pending_messages_tail;
287 * Handle for pending transmission request.
289 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
292 * Address of the other peer.
294 struct GNUNET_HELLO_Address *address;
297 * ID of task used to delay receiving more to throttle sender.
299 struct GNUNET_SCHEDULER_Task *receive_delay_task;
302 * Session timeout task
304 struct GNUNET_SCHEDULER_Task *timeout_task;
307 * When will this session time out?
309 struct GNUNET_TIME_Absolute timeout;
312 * When will we continue to read from the socket?
313 * (used to enforce inbound quota).
315 struct GNUNET_TIME_Absolute receive_delay;
318 * Last activity on this connection. Used to select preferred
321 struct GNUNET_TIME_Absolute last_activity;
324 * Number of bytes waiting for transmission to this peer.
326 unsigned long long bytes_in_queue;
329 * Number of messages waiting for transmission to this peer.
331 unsigned int msgs_in_queue;
334 * Network type of the address.
336 enum GNUNET_ATS_Network_Type scope;
339 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
341 int expecting_welcome;
344 * Was this session created using NAT traversal?
352 * Context for address to string conversion, closure
353 * for #append_port().
355 struct PrettyPrinterContext
360 struct PrettyPrinterContext *next;
365 struct PrettyPrinterContext *prev;
370 struct Plugin *plugin;
375 struct GNUNET_SCHEDULER_Task *timeout_task;
380 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
383 * Function to call with the result.
385 GNUNET_TRANSPORT_AddressStringCallback asc;
388 * Clsoure for @e asc.
403 * Port to add after the IP address.
410 * Encapsulation of all of the state of the plugin.
417 struct GNUNET_TRANSPORT_PluginEnvironment *env;
422 struct GNUNET_CONNECTION_Handle *lsock;
425 * Our handle to the NAT module.
427 struct GNUNET_NAT_Handle *nat;
430 * Map from peer identities to sessions for the given peer.
432 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
435 * Handle to the network service.
437 struct GNUNET_SERVICE_Context *service;
440 * Handle to the server for this service.
442 struct GNUNET_SERVER_Handle *server;
445 * Copy of the handler array where the closures are
446 * set to this struct's instance.
448 struct GNUNET_SERVER_MessageHandler *handlers;
451 * Map of peers we have tried to contact behind a NAT
453 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
456 * List of active TCP probes.
458 struct TCPProbeContext *probe_head;
461 * List of active TCP probes.
463 struct TCPProbeContext *probe_tail;
466 * Function to call about session status changes.
468 GNUNET_TRANSPORT_SessionInfoCallback sic;
471 * Closure for @e sic.
476 * ID of task used to update our addresses when one expires.
478 struct GNUNET_SCHEDULER_Task *address_update_task;
481 * Running pretty printers: head
483 struct PrettyPrinterContext *ppc_dll_head;
486 * Running pretty printers: tail
488 struct PrettyPrinterContext *ppc_dll_tail;
491 * Welcome message used by this peer.
493 struct WelcomeMessage my_welcome;
496 * How many more TCP sessions are we allowed to open right now?
498 unsigned long long max_connections;
501 * How many more TCP sessions do we have right now?
503 unsigned long long cur_connections;
511 * Port that we are actually listening on.
516 * Port that the user said we would have visible to the
524 /* begin of ancient copy-and-pasted code that should be
525 specialized for TCP ...*/
527 * Add the given UNIX domain path as an address to the
528 * list (as the first entry).
530 * @param saddrs array to update
531 * @param saddrlens where to store the address length
532 * @param unixpath path to add
533 * @param abstract #GNUNET_YES to add an abstract UNIX domain socket. This
534 * parameter is ignore on systems other than LINUX
537 add_unixpath (struct sockaddr **saddrs,
538 socklen_t *saddrlens,
539 const char *unixpath,
543 struct sockaddr_un *un;
545 un = GNUNET_new (struct sockaddr_un);
546 un->sun_family = AF_UNIX;
547 strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1);
549 if (GNUNET_YES == abstract)
550 un->sun_path[0] = '\0';
552 #if HAVE_SOCKADDR_IN_SIN_LEN
553 un->sun_len = (u_char) sizeof (struct sockaddr_un);
555 *saddrs = (struct sockaddr *) un;
556 *saddrlens = sizeof (struct sockaddr_un);
558 /* this function should never be called
559 * unless AF_UNIX is defined! */
566 * Get the list of addresses that a server for the given service
569 * @param service_name name of the service
570 * @param cfg configuration (which specifies the addresses)
571 * @param addrs set (call by reference) to an array of pointers to the
572 * addresses the server should bind to and listen on; the
573 * array will be NULL-terminated (on success)
574 * @param addr_lens set (call by reference) to an array of the lengths
575 * of the respective `struct sockaddr` struct in the @a addrs
577 * @return number of addresses found on success,
578 * #GNUNET_SYSERR if the configuration
579 * did not specify reasonable finding information or
580 * if it specified a hostname that could not be resolved;
581 * #GNUNET_NO if the number of addresses configured is
582 * zero (in this case, `*addrs` and `*addr_lens` will be
586 get_server_addresses (const char *service_name,
587 const struct GNUNET_CONFIGURATION_Handle *cfg,
588 struct sockaddr ***addrs,
589 socklen_t ** addr_lens)
592 struct GNUNET_NETWORK_Handle *desc;
593 unsigned long long port;
595 struct addrinfo hints;
596 struct addrinfo *res;
597 struct addrinfo *pos;
598 struct addrinfo *next;
603 struct sockaddr **saddrs;
604 socklen_t *saddrlens;
610 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "DISABLEV6"))
614 GNUNET_CONFIGURATION_get_value_yesno (cfg, service_name, "DISABLEV6")))
615 return GNUNET_SYSERR;
618 disablev6 = GNUNET_NO;
622 /* probe IPv6 support */
623 desc = GNUNET_NETWORK_socket_create (PF_INET6, SOCK_STREAM, 0);
626 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
629 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
630 return GNUNET_SYSERR;
632 LOG (GNUNET_ERROR_TYPE_INFO,
633 _("Disabling IPv6 support for service `%s', failed to create IPv6 socket: %s\n"),
634 service_name, STRERROR (errno));
635 disablev6 = GNUNET_YES;
639 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
645 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "PORT"))
648 GNUNET_CONFIGURATION_get_value_number (cfg, service_name,
651 LOG (GNUNET_ERROR_TYPE_ERROR,
652 _("Require valid port number for service `%s' in configuration!\n"),
657 LOG (GNUNET_ERROR_TYPE_ERROR,
658 _("Require valid port number for service `%s' in configuration!\n"),
660 return GNUNET_SYSERR;
664 if (GNUNET_CONFIGURATION_have_value (cfg, service_name, "BINDTO"))
666 GNUNET_break (GNUNET_OK ==
667 GNUNET_CONFIGURATION_get_value_string (cfg, service_name,
668 "BINDTO", &hostname));
674 abstract = GNUNET_NO;
677 GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) &&
679 GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH",
681 (0 < strlen (unixpath)))
683 /* probe UNIX support */
684 struct sockaddr_un s_un;
686 if (strlen (unixpath) >= sizeof (s_un.sun_path))
688 LOG (GNUNET_ERROR_TYPE_WARNING,
689 _("UNIXPATH `%s' too long, maximum length is %llu\n"), unixpath,
690 (unsigned long long) sizeof (s_un.sun_path));
691 unixpath = GNUNET_NETWORK_shorten_unixpath (unixpath);
692 LOG (GNUNET_ERROR_TYPE_INFO,
693 _("Using `%s' instead\n"),
697 abstract = GNUNET_CONFIGURATION_get_value_yesno (cfg,
699 "USE_ABSTRACT_SOCKETS");
700 if (GNUNET_SYSERR == abstract)
701 abstract = GNUNET_NO;
703 if ((GNUNET_YES != abstract)
705 GNUNET_DISK_directory_create_for_file (unixpath)))
706 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
710 if (NULL != unixpath)
712 desc = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0);
715 if ((ENOBUFS == errno) || (ENOMEM == errno) || (ENFILE == errno) ||
718 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "socket");
719 GNUNET_free_non_null (hostname);
720 GNUNET_free (unixpath);
721 return GNUNET_SYSERR;
723 LOG (GNUNET_ERROR_TYPE_INFO,
724 _("Disabling UNIX domain socket support for service `%s', failed to create UNIX domain socket: %s\n"),
727 GNUNET_free (unixpath);
732 GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (desc));
738 if ((0 == port) && (NULL == unixpath))
740 LOG (GNUNET_ERROR_TYPE_ERROR,
741 _("Have neither PORT nor UNIXPATH for service `%s', but one is required\n"),
743 GNUNET_free_non_null (hostname);
744 return GNUNET_SYSERR;
748 saddrs = GNUNET_malloc (2 * sizeof (struct sockaddr *));
749 saddrlens = GNUNET_malloc (2 * sizeof (socklen_t));
750 add_unixpath (saddrs, saddrlens, unixpath, abstract);
751 GNUNET_free_non_null (unixpath);
752 GNUNET_free_non_null (hostname);
754 *addr_lens = saddrlens;
758 if (NULL != hostname)
760 LOG (GNUNET_ERROR_TYPE_DEBUG,
761 "Resolving `%s' since that is where `%s' will bind to.\n",
764 memset (&hints, 0, sizeof (struct addrinfo));
766 hints.ai_family = AF_INET;
767 hints.ai_protocol = IPPROTO_TCP;
768 if ((0 != (ret = getaddrinfo (hostname, NULL, &hints, &res))) ||
771 LOG (GNUNET_ERROR_TYPE_ERROR,
772 _("Failed to resolve `%s': %s\n"),
775 GNUNET_free (hostname);
776 GNUNET_free_non_null (unixpath);
777 return GNUNET_SYSERR;
781 while (NULL != (pos = next))
784 if ((disablev6) && (pos->ai_family == AF_INET6))
790 LOG (GNUNET_ERROR_TYPE_ERROR,
791 _("Failed to find %saddress for `%s'.\n"),
792 disablev6 ? "IPv4 " : "",
795 GNUNET_free (hostname);
796 GNUNET_free_non_null (unixpath);
797 return GNUNET_SYSERR;
800 if (NULL != unixpath)
802 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
803 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
805 if (NULL != unixpath)
807 add_unixpath (saddrs, saddrlens, unixpath, abstract);
811 while (NULL != (pos = next))
814 if ((disablev6) && (AF_INET6 == pos->ai_family))
816 if ((IPPROTO_TCP != pos->ai_protocol) && (0 != pos->ai_protocol))
817 continue; /* not TCP */
818 if ((SOCK_STREAM != pos->ai_socktype) && (0 != pos->ai_socktype))
820 LOG (GNUNET_ERROR_TYPE_DEBUG, "Service `%s' will bind to `%s'\n",
821 service_name, GNUNET_a2s (pos->ai_addr, pos->ai_addrlen));
822 if (AF_INET == pos->ai_family)
824 GNUNET_assert (sizeof (struct sockaddr_in) == pos->ai_addrlen);
825 saddrlens[i] = pos->ai_addrlen;
826 saddrs[i] = GNUNET_malloc (saddrlens[i]);
827 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
828 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
832 GNUNET_assert (AF_INET6 == pos->ai_family);
833 GNUNET_assert (sizeof (struct sockaddr_in6) == pos->ai_addrlen);
834 saddrlens[i] = pos->ai_addrlen;
835 saddrs[i] = GNUNET_malloc (saddrlens[i]);
836 GNUNET_memcpy (saddrs[i], pos->ai_addr, saddrlens[i]);
837 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
841 GNUNET_free (hostname);
847 /* will bind against everything, just set port */
852 if (NULL != unixpath)
855 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
856 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
857 if (NULL != unixpath)
859 add_unixpath (saddrs, saddrlens, unixpath, abstract);
862 saddrlens[i] = sizeof (struct sockaddr_in);
863 saddrs[i] = GNUNET_malloc (saddrlens[i]);
864 #if HAVE_SOCKADDR_IN_SIN_LEN
865 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[i];
867 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
868 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
874 if (NULL != unixpath)
876 saddrs = GNUNET_malloc ((resi + 1) * sizeof (struct sockaddr *));
877 saddrlens = GNUNET_malloc ((resi + 1) * sizeof (socklen_t));
879 if (NULL != unixpath)
881 add_unixpath (saddrs, saddrlens, unixpath, abstract);
884 saddrlens[i] = sizeof (struct sockaddr_in6);
885 saddrs[i] = GNUNET_malloc (saddrlens[i]);
886 #if HAVE_SOCKADDR_IN_SIN_LEN
887 ((struct sockaddr_in6 *) saddrs[i])->sin6_len = saddrlens[0];
889 ((struct sockaddr_in6 *) saddrs[i])->sin6_family = AF_INET6;
890 ((struct sockaddr_in6 *) saddrs[i])->sin6_port = htons (port);
892 saddrlens[i] = sizeof (struct sockaddr_in);
893 saddrs[i] = GNUNET_malloc (saddrlens[i]);
894 #if HAVE_SOCKADDR_IN_SIN_LEN
895 ((struct sockaddr_in *) saddrs[i])->sin_len = saddrlens[1];
897 ((struct sockaddr_in *) saddrs[i])->sin_family = AF_INET;
898 ((struct sockaddr_in *) saddrs[i])->sin_port = htons (port);
901 GNUNET_free_non_null (unixpath);
903 *addr_lens = saddrlens;
906 /* end ancient copy-and-paste */
910 * If a session monitor is attached, notify it about the new
913 * @param plugin our plugin
914 * @param session session that changed state
915 * @param state new state of the session
918 notify_session_monitor (struct Plugin *plugin,
919 struct GNUNET_ATS_Session *session,
920 enum GNUNET_TRANSPORT_SessionState state)
922 struct GNUNET_TRANSPORT_SessionInfo info;
924 if (NULL == plugin->sic)
926 memset (&info, 0, sizeof (info));
928 info.is_inbound = GNUNET_HELLO_address_check_option (session->address,
929 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
930 info.num_msg_pending = session->msgs_in_queue;
931 info.num_bytes_pending = session->bytes_in_queue;
932 if (NULL != session->receive_delay_task)
933 info.receive_delay = session->receive_delay;
934 info.session_timeout = session->timeout;
935 info.address = session->address;
936 plugin->sic (plugin->sic_cls,
943 * Our external IP address/port mapping has changed.
945 * @param cls closure, the `struct Plugin`
946 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
947 * the previous (now invalid) one
948 * @param addr either the previous or the new public IP address
949 * @param addrlen actual length of @a addr
952 tcp_nat_port_map_callback (void *cls,
954 const struct sockaddr *addr,
957 struct Plugin *plugin = cls;
958 struct GNUNET_HELLO_Address *address;
959 struct IPv4TcpAddress t4;
960 struct IPv6TcpAddress t6;
964 LOG(GNUNET_ERROR_TYPE_INFO,
965 "NAT notification to %s address `%s'\n",
966 (GNUNET_YES == add_remove) ? "add" : "remove",
967 GNUNET_a2s (addr, addrlen));
968 /* convert 'addr' to our internal format */
969 switch (addr->sa_family)
972 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
973 memset (&t4, 0, sizeof(t4));
974 t4.options = htonl (plugin->myoptions);
975 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
976 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
981 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
982 memset (&t6, 0, sizeof(t6));
983 GNUNET_memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
984 sizeof(struct in6_addr));
985 t6.options = htonl (plugin->myoptions);
986 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
994 /* modify our published address list */
995 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
996 (args == sizeof (struct IPv6TcpAddress)));
997 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
998 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
999 plugin->env->notify_address (plugin->env->cls, add_remove, address);
1000 GNUNET_HELLO_address_free(address);
1005 * Function called for a quick conversion of the binary address to
1006 * a numeric address. Note that the caller must not free the
1007 * address and that the next call to this function is allowed
1008 * to override the address again.
1010 * @param cls closure (`struct Plugin*`)
1011 * @param addr binary address
1012 * @param addrlen length of @a addr
1013 * @return string representing the same address
1016 tcp_plugin_address_to_string (void *cls,
1020 static char rbuf[INET6_ADDRSTRLEN + 12];
1021 char buf[INET6_ADDRSTRLEN];
1025 const struct IPv4TcpAddress *t4;
1026 const struct IPv6TcpAddress *t6;
1033 case sizeof(struct IPv6TcpAddress):
1036 port = ntohs (t6->t6_port);
1037 options = ntohl (t6->options);
1038 GNUNET_memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
1041 case sizeof(struct IPv4TcpAddress):
1044 port = ntohs (t4->t4_port);
1045 options = ntohl (t4->options);
1046 GNUNET_memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
1050 LOG (GNUNET_ERROR_TYPE_WARNING,
1051 _("Unexpected address length: %u bytes\n"),
1052 (unsigned int) addrlen);
1055 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
1057 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
1061 GNUNET_snprintf (rbuf, sizeof(rbuf),
1062 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
1072 * Function called to convert a string address to
1075 * @param cls closure (`struct Plugin*`)
1076 * @param addr string address
1077 * @param addrlen length of the address
1078 * @param buf location to store the buffer
1079 * @param added location to store the number of bytes in the buffer.
1080 * If the function returns #GNUNET_SYSERR, its contents are undefined.
1081 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1084 tcp_plugin_string_to_address (void *cls,
1090 struct sockaddr_storage socket_address;
1096 /* Format tcp.options.address:port */
1100 if ((NULL == addr) || (0 == addrlen))
1103 return GNUNET_SYSERR;
1105 if ('\0' != addr[addrlen - 1])
1108 return GNUNET_SYSERR;
1110 if (strlen (addr) != addrlen - 1)
1113 return GNUNET_SYSERR;
1115 plugin = GNUNET_strdup (addr);
1116 optionstr = strchr (plugin, '.');
1117 if (NULL == optionstr)
1120 GNUNET_free(plugin);
1121 return GNUNET_SYSERR;
1123 optionstr[0] = '\0';
1125 options = atol (optionstr);
1126 address = strchr (optionstr, '.');
1127 if (NULL == address)
1130 GNUNET_free(plugin);
1131 return GNUNET_SYSERR;
1137 GNUNET_STRINGS_to_address_ip (address,
1142 GNUNET_free(plugin);
1143 return GNUNET_SYSERR;
1146 GNUNET_free(plugin);
1147 switch (socket_address.ss_family)
1151 struct IPv4TcpAddress *t4;
1152 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
1153 t4 = GNUNET_new (struct IPv4TcpAddress);
1154 t4->options = htonl (options);
1155 t4->ipv4_addr = in4->sin_addr.s_addr;
1156 t4->t4_port = in4->sin_port;
1158 *added = sizeof(struct IPv4TcpAddress);
1163 struct IPv6TcpAddress *t6;
1164 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
1165 t6 = GNUNET_new (struct IPv6TcpAddress);
1166 t6->options = htonl (options);
1167 t6->ipv6_addr = in6->sin6_addr;
1168 t6->t6_port = in6->sin6_port;
1170 *added = sizeof(struct IPv6TcpAddress);
1174 return GNUNET_SYSERR;
1180 * Find the session handle for the given client.
1181 * Currently uses both the hashmap and the client
1182 * context, as the client context is new and the
1183 * logic still needs to be tested.
1185 * @param plugin the plugin
1186 * @param client which client to find the session handle for
1187 * @return NULL if no matching session exists
1189 static struct GNUNET_ATS_Session *
1190 lookup_session_by_client (struct Plugin *plugin,
1191 struct GNUNET_SERVER_Client *client)
1193 return GNUNET_SERVER_client_get_user_context (client,
1194 struct GNUNET_ATS_Session);
1199 * Functions with this signature are called whenever we need
1200 * to close a session due to a disconnect or failure to
1201 * establish a connection.
1203 * @param cls the `struct Plugin`
1204 * @param session session to close down
1205 * @return #GNUNET_OK on success
1208 tcp_plugin_disconnect_session (void *cls,
1209 struct GNUNET_ATS_Session *session)
1211 struct Plugin *plugin = cls;
1212 struct PendingMessage *pm;
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "Disconnecting session of peer `%s' address `%s'\n",
1216 GNUNET_i2s (&session->target),
1217 tcp_plugin_address_to_string (session->plugin,
1218 session->address->address,
1219 session->address->address_length));
1221 if (NULL != session->timeout_task)
1223 GNUNET_SCHEDULER_cancel (session->timeout_task);
1224 session->timeout_task = NULL;
1225 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
1229 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
1233 GNUNET_STATISTICS_update (session->plugin->env->stats,
1234 gettext_noop ("# TCP sessions active"),
1240 GNUNET_assert (GNUNET_YES ==
1241 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
1245 if (NULL != session->client)
1246 GNUNET_SERVER_client_set_user_context (session->client,
1249 /* clean up state */
1250 if (NULL != session->transmit_handle)
1252 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1253 session->transmit_handle = NULL;
1255 session->plugin->env->session_end (session->plugin->env->cls,
1259 if (NULL != session->nat_connection_timeout)
1261 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1262 session->nat_connection_timeout = NULL;
1265 while (NULL != (pm = session->pending_messages_head))
1267 LOG (GNUNET_ERROR_TYPE_DEBUG,
1268 (NULL != pm->transmit_cont)
1269 ? "Could not deliver message to `%s' at %s.\n"
1270 : "Could not deliver message to `%s' at %s, notifying.\n",
1271 GNUNET_i2s (&session->target),
1272 tcp_plugin_address_to_string (session->plugin,
1273 session->address->address,
1274 session->address->address_length));
1275 GNUNET_STATISTICS_update (session->plugin->env->stats,
1276 gettext_noop ("# bytes currently in TCP buffers"),
1277 -(int64_t) pm->message_size, GNUNET_NO);
1278 GNUNET_STATISTICS_update (session->plugin->env->stats,
1279 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1282 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1283 session->pending_messages_tail,
1285 GNUNET_assert (0 < session->msgs_in_queue);
1286 session->msgs_in_queue--;
1287 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
1288 session->bytes_in_queue -= pm->message_size;
1289 if (NULL != pm->transmit_cont)
1290 pm->transmit_cont (pm->transmit_cont_cls,
1297 GNUNET_assert (0 == session->msgs_in_queue);
1298 GNUNET_assert (0 == session->bytes_in_queue);
1299 notify_session_monitor (session->plugin,
1301 GNUNET_TRANSPORT_SS_DONE);
1303 if (NULL != session->receive_delay_task)
1305 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1306 session->receive_delay_task = NULL;
1308 if (NULL != session->client)
1310 GNUNET_SERVER_client_disconnect (session->client);
1311 session->client = NULL;
1313 GNUNET_HELLO_address_free (session->address);
1314 GNUNET_assert (NULL == session->transmit_handle);
1315 GNUNET_free (session);
1321 * Function that is called to get the keepalive factor.
1322 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
1323 * calculate the interval between keepalive packets.
1325 * @param cls closure with the `struct Plugin`
1326 * @return keepalive factor
1329 tcp_plugin_query_keepalive_factor (void *cls)
1336 * Session was idle for too long, so disconnect it
1338 * @param cls the `struct GNUNET_ATS_Session` of the idle session
1341 session_timeout (void *cls)
1343 struct GNUNET_ATS_Session *s = cls;
1344 struct GNUNET_TIME_Relative left;
1346 s->timeout_task = NULL;
1347 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
1348 if (0 != left.rel_value_us)
1350 /* not actually our turn yet, but let's at least update
1351 the monitor, it may think we're about to die ... */
1352 notify_session_monitor (s->plugin,
1354 GNUNET_TRANSPORT_SS_UPDATE);
1355 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
1360 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "Session %p was idle for %s, disconnecting\n",
1363 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1365 /* call session destroy function */
1366 tcp_plugin_disconnect_session (s->plugin,
1372 * Increment session timeout due to activity.
1374 * @param s session to increment timeout for
1377 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
1379 GNUNET_assert (NULL != s->timeout_task);
1380 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1385 * Create a new session. Also queues a welcome message.
1387 * @param plugin the plugin
1388 * @param address the address to create the session for
1389 * @param scope network scope the address is from
1390 * @param client client to use, reference counter must have already been increased
1391 * @param is_nat this a NAT session, we should wait for a client to
1392 * connect to us from an address, then assign that to
1394 * @return new session object
1396 static struct GNUNET_ATS_Session *
1397 create_session (struct Plugin *plugin,
1398 const struct GNUNET_HELLO_Address *address,
1399 enum GNUNET_ATS_Network_Type scope,
1400 struct GNUNET_SERVER_Client *client,
1403 struct GNUNET_ATS_Session *session;
1404 struct PendingMessage *pm;
1406 if (GNUNET_YES != is_nat)
1407 GNUNET_assert (NULL != client);
1409 GNUNET_assert (NULL == client);
1411 LOG (GNUNET_ERROR_TYPE_DEBUG,
1412 "Creating new session for peer `%4s' at address %s\n",
1413 GNUNET_i2s (&address->peer),
1414 tcp_plugin_address_to_string (plugin,
1416 address->address_length));
1417 session = GNUNET_new (struct GNUNET_ATS_Session);
1418 session->last_activity = GNUNET_TIME_absolute_get ();
1419 session->plugin = plugin;
1420 session->is_nat = is_nat;
1423 session->client = client;
1424 GNUNET_SERVER_client_set_user_context (client,
1427 session->address = GNUNET_HELLO_address_copy (address);
1428 session->target = address->peer;
1429 session->expecting_welcome = GNUNET_YES;
1430 session->scope = scope;
1431 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1432 sizeof (struct WelcomeMessage));
1433 pm->msg = (const char *) &pm[1];
1434 pm->message_size = sizeof(struct WelcomeMessage);
1435 GNUNET_memcpy (&pm[1],
1436 &plugin->my_welcome,
1437 sizeof(struct WelcomeMessage));
1438 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1439 GNUNET_STATISTICS_update (plugin->env->stats,
1440 gettext_noop ("# bytes currently in TCP buffers"),
1443 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1444 session->pending_messages_tail,
1446 session->msgs_in_queue++;
1447 session->bytes_in_queue += pm->message_size;
1448 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1449 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1452 notify_session_monitor (session->plugin,
1454 GNUNET_TRANSPORT_SS_INIT);
1455 if (GNUNET_YES != is_nat)
1457 GNUNET_STATISTICS_update (plugin->env->stats,
1458 gettext_noop ("# TCP sessions active"),
1461 notify_session_monitor (session->plugin,
1463 GNUNET_TRANSPORT_SS_UP);
1467 notify_session_monitor (session->plugin,
1469 GNUNET_TRANSPORT_SS_HANDSHAKE);
1476 * If we have pending messages, ask the server to
1477 * transmit them (schedule the respective tasks, etc.)
1479 * @param session for which session should we do this
1482 process_pending_messages (struct GNUNET_ATS_Session *session);
1486 * Function called to notify a client about the socket
1487 * being ready to queue more data. "buf" will be
1488 * NULL and "size" zero if the socket was closed for
1489 * writing in the meantime.
1491 * @param cls closure
1492 * @param size number of bytes available in @a buf
1493 * @param buf where the callee should write the message
1494 * @return number of bytes written to @a buf
1497 do_transmit (void *cls,
1501 struct GNUNET_ATS_Session *session = cls;
1502 struct GNUNET_PeerIdentity pid;
1503 struct Plugin *plugin;
1504 struct PendingMessage *pos;
1505 struct PendingMessage *hd;
1506 struct PendingMessage *tl;
1507 struct GNUNET_TIME_Absolute now;
1511 session->transmit_handle = NULL;
1512 plugin = session->plugin;
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1516 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1517 GNUNET_i2s (&session->target));
1518 /* timeout; cancel all messages that have already expired */
1522 now = GNUNET_TIME_absolute_get ();
1523 while ( (NULL != (pos = session->pending_messages_head)) &&
1524 (pos->timeout.abs_value_us <= now.abs_value_us) )
1526 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1527 session->pending_messages_tail,
1529 GNUNET_assert (0 < session->msgs_in_queue);
1530 session->msgs_in_queue--;
1531 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1532 session->bytes_in_queue -= pos->message_size;
1533 LOG (GNUNET_ERROR_TYPE_DEBUG,
1534 "Failed to transmit %u byte message to `%4s'.\n",
1536 GNUNET_i2s (&session->target));
1537 ret += pos->message_size;
1538 GNUNET_CONTAINER_DLL_insert_after (hd,
1543 /* do this call before callbacks (so that if callbacks destroy
1544 * session, they have a chance to cancel actions done by this
1546 process_pending_messages (session);
1547 pid = session->target;
1548 /* no do callbacks and do not use session again since
1549 * the callbacks may abort the session */
1550 while (NULL != (pos = hd))
1552 GNUNET_CONTAINER_DLL_remove (hd,
1555 if (NULL != pos->transmit_cont)
1556 pos->transmit_cont (pos->transmit_cont_cls,
1563 GNUNET_STATISTICS_update (plugin->env->stats,
1564 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1566 GNUNET_STATISTICS_update (plugin->env->stats,
1567 gettext_noop ("# bytes discarded by TCP (timeout)"),
1571 notify_session_monitor (session->plugin,
1573 GNUNET_TRANSPORT_SS_UPDATE);
1576 /* copy all pending messages that would fit */
1581 while (NULL != (pos = session->pending_messages_head))
1583 if (ret + pos->message_size > size)
1585 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1586 session->pending_messages_tail,
1588 GNUNET_assert (0 < session->msgs_in_queue);
1589 session->msgs_in_queue--;
1590 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1591 session->bytes_in_queue -= pos->message_size;
1592 GNUNET_assert(size >= pos->message_size);
1593 LOG (GNUNET_ERROR_TYPE_DEBUG,
1594 "Transmitting message of type %u size %u to peer %s at %s\n",
1595 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1597 GNUNET_i2s (&session->target),
1598 tcp_plugin_address_to_string (session->plugin,
1599 session->address->address,
1600 session->address->address_length));
1601 /* FIXME: this GNUNET_memcpy can be up to 7% of our total runtime */
1602 GNUNET_memcpy (cbuf,
1605 cbuf += pos->message_size;
1606 ret += pos->message_size;
1607 size -= pos->message_size;
1608 GNUNET_CONTAINER_DLL_insert_tail (hd,
1612 notify_session_monitor (session->plugin,
1614 GNUNET_TRANSPORT_SS_UPDATE);
1615 /* schedule 'continuation' before callbacks so that callbacks that
1616 * cancel everything don't cause us to use a session that no longer
1618 process_pending_messages (session);
1619 session->last_activity = GNUNET_TIME_absolute_get ();
1620 pid = session->target;
1621 /* we'll now call callbacks that may cancel the session; hence
1622 * we should not use 'session' after this point */
1623 while (NULL != (pos = hd))
1625 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1626 if (NULL != pos->transmit_cont)
1627 pos->transmit_cont (pos->transmit_cont_cls,
1631 pos->message_size); /* FIXME: include TCP overhead */
1634 GNUNET_assert (NULL == hd);
1635 GNUNET_assert (NULL == tl);
1636 GNUNET_STATISTICS_update (plugin->env->stats,
1637 gettext_noop ("# bytes currently in TCP buffers"),
1640 GNUNET_STATISTICS_update (plugin->env->stats,
1641 gettext_noop ("# bytes transmitted via TCP"),
1649 * If we have pending messages, ask the server to
1650 * transmit them (schedule the respective tasks, etc.)
1652 * @param session for which session should we do this
1655 process_pending_messages (struct GNUNET_ATS_Session *session)
1657 struct PendingMessage *pm;
1659 GNUNET_assert (NULL != session->client);
1660 if (NULL != session->transmit_handle)
1662 if (NULL == (pm = session->pending_messages_head))
1665 session->transmit_handle
1666 = GNUNET_SERVER_notify_transmit_ready (session->client,
1668 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1675 * Function that can be used by the transport service to transmit
1676 * a message using the plugin. Note that in the case of a
1677 * peer disconnecting, the continuation MUST be called
1678 * prior to the disconnect notification itself. This function
1679 * will be called with this peer's HELLO message to initiate
1680 * a fresh connection to another peer.
1682 * @param cls closure
1683 * @param session which session must be used
1684 * @param msgbuf the message to transmit
1685 * @param msgbuf_size number of bytes in @a msgbuf
1686 * @param priority how important is the message (most plugins will
1687 * ignore message priority and just FIFO)
1688 * @param to how long to wait at most for the transmission (does not
1689 * require plugins to discard the message after the timeout,
1690 * just advisory for the desired delay; most plugins will ignore
1692 * @param cont continuation to call once the message has
1693 * been transmitted (or if the transport is ready
1694 * for the next transmission call; or if the
1695 * peer disconnected...); can be NULL
1696 * @param cont_cls closure for @a cont
1697 * @return number of bytes used (on the physical network, with overheads);
1698 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1699 * and does NOT mean that the message was not transmitted (DV)
1702 tcp_plugin_send (void *cls,
1703 struct GNUNET_ATS_Session *session,
1706 unsigned int priority,
1707 struct GNUNET_TIME_Relative to,
1708 GNUNET_TRANSPORT_TransmitContinuation cont,
1711 struct Plugin * plugin = cls;
1712 struct PendingMessage *pm;
1714 /* create new message entry */
1715 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1716 pm->msg = (const char *) &pm[1];
1717 GNUNET_memcpy (&pm[1], msgbuf, msgbuf_size);
1718 pm->message_size = msgbuf_size;
1719 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1720 pm->transmit_cont = cont;
1721 pm->transmit_cont_cls = cont_cls;
1723 LOG(GNUNET_ERROR_TYPE_DEBUG,
1724 "Asked to transmit %u bytes to `%s', added message to list.\n",
1726 GNUNET_i2s (&session->target));
1729 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1733 GNUNET_assert (NULL != session->client);
1734 GNUNET_SERVER_client_set_timeout (session->client,
1735 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1736 GNUNET_STATISTICS_update (plugin->env->stats,
1737 gettext_noop ("# bytes currently in TCP buffers"),
1741 /* append pm to pending_messages list */
1742 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1743 session->pending_messages_tail,
1745 notify_session_monitor (session->plugin,
1747 GNUNET_TRANSPORT_SS_UPDATE);
1748 session->msgs_in_queue++;
1749 session->bytes_in_queue += pm->message_size;
1750 process_pending_messages (session);
1754 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1758 LOG (GNUNET_ERROR_TYPE_DEBUG,
1759 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1760 GNUNET_i2s (&session->target));
1761 GNUNET_STATISTICS_update (plugin->env->stats,
1762 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1764 /* append pm to pending_messages list */
1765 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1766 session->pending_messages_tail,
1768 session->msgs_in_queue++;
1769 session->bytes_in_queue += pm->message_size;
1770 notify_session_monitor (session->plugin,
1772 GNUNET_TRANSPORT_SS_HANDSHAKE);
1775 LOG (GNUNET_ERROR_TYPE_ERROR,
1776 "Invalid session %p\n",
1786 return GNUNET_SYSERR; /* session does not exist here */
1791 * Closure for #session_lookup_it().
1793 struct GNUNET_ATS_SessionItCtx
1796 * Address we are looking for.
1798 const struct GNUNET_HELLO_Address *address;
1801 * Where to store the session (if we found it).
1803 struct GNUNET_ATS_Session *result;
1809 * Look for a session by address.
1811 * @param cls the `struct GNUNET_ATS_SessionItCtx`
1813 * @param value a `struct GNUNET_ATS_Session`
1814 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1817 session_lookup_it (void *cls,
1818 const struct GNUNET_PeerIdentity *key,
1821 struct GNUNET_ATS_SessionItCtx *si_ctx = cls;
1822 struct GNUNET_ATS_Session *session = value;
1825 GNUNET_HELLO_address_cmp (si_ctx->address,
1828 si_ctx->result = session;
1834 * Task cleaning up a NAT connection attempt after timeout
1836 * @param cls the `struct GNUNET_ATS_Session`
1839 nat_connect_timeout (void *cls)
1841 struct GNUNET_ATS_Session *session = cls;
1843 session->nat_connection_timeout = NULL;
1844 LOG (GNUNET_ERROR_TYPE_DEBUG,
1845 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1846 GNUNET_i2s (&session->target),
1847 tcp_plugin_address_to_string (session->plugin,
1848 session->address->address,
1849 session->address->address_length));
1850 tcp_plugin_disconnect_session (session->plugin,
1856 * Function that will be called whenever the transport service wants to
1857 * notify the plugin that a session is still active and in use and
1858 * therefore the session timeout for this session has to be updated
1860 * @param cls closure
1861 * @param peer which peer was the session for
1862 * @param session which session is being updated
1865 tcp_plugin_update_session_timeout (void *cls,
1866 const struct GNUNET_PeerIdentity *peer,
1867 struct GNUNET_ATS_Session *session)
1869 reschedule_session_timeout (session);
1874 * Task to signal the server that we can continue
1875 * receiving from the TCP client now.
1877 * @param cls the `struct GNUNET_ATS_Session *`
1880 delayed_done (void *cls)
1882 struct GNUNET_ATS_Session *session = cls;
1884 session->receive_delay_task = NULL;
1885 reschedule_session_timeout (session);
1886 GNUNET_SERVER_receive_done (session->client,
1892 * Function that will be called whenever the transport service wants to
1893 * notify the plugin that the inbound quota changed and that the plugin
1894 * should update it's delay for the next receive value
1896 * @param cls closure
1897 * @param peer which peer was the session for
1898 * @param session which session is being updated
1899 * @param delay new delay to use for receiving
1902 tcp_plugin_update_inbound_delay (void *cls,
1903 const struct GNUNET_PeerIdentity *peer,
1904 struct GNUNET_ATS_Session *session,
1905 struct GNUNET_TIME_Relative delay)
1907 if (NULL == session->receive_delay_task)
1909 LOG (GNUNET_ERROR_TYPE_DEBUG,
1910 "New inbound delay %s\n",
1911 GNUNET_STRINGS_relative_time_to_string (delay,
1913 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1914 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1915 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1922 * Create a new session to transmit data to the target
1923 * This session will used to send data to this peer and the plugin will
1924 * notify us by calling the env->session_end function
1926 * @param cls closure
1927 * @param address the address to use
1928 * @return the session if the address is valid, NULL otherwise
1930 static struct GNUNET_ATS_Session *
1931 tcp_plugin_get_session (void *cls,
1932 const struct GNUNET_HELLO_Address *address)
1934 struct Plugin *plugin = cls;
1935 struct GNUNET_ATS_Session *session = NULL;
1939 struct GNUNET_CONNECTION_Handle *sa;
1940 struct sockaddr_in a4;
1941 struct sockaddr_in6 a6;
1942 const struct IPv4TcpAddress *t4;
1943 const struct IPv6TcpAddress *t6;
1944 unsigned int options;
1945 enum GNUNET_ATS_Network_Type net_type;
1946 unsigned int is_natd = GNUNET_NO;
1949 struct GNUNET_NETWORK_Handle *s;
1952 addrlen = address->address_length;
1953 LOG (GNUNET_ERROR_TYPE_DEBUG,
1954 "Trying to get session for `%s' address of peer `%s'\n",
1955 tcp_plugin_address_to_string (plugin,
1957 address->address_length),
1958 GNUNET_i2s (&address->peer));
1960 if (GNUNET_HELLO_address_check_option (address,
1961 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1967 /* look for existing session */
1969 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1972 struct GNUNET_ATS_SessionItCtx si_ctx;
1974 si_ctx.address = address;
1975 si_ctx.result = NULL;
1976 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1980 if (NULL != si_ctx.result)
1982 session = si_ctx.result;
1983 LOG (GNUNET_ERROR_TYPE_DEBUG,
1984 "Found existing session for `%s' address `%s'\n",
1985 GNUNET_i2s (&address->peer),
1986 tcp_plugin_address_to_string (plugin,
1988 address->address_length));
1993 if (addrlen == sizeof(struct IPv6TcpAddress))
1995 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1996 t6 = address->address;
1997 options = t6->options;
1999 memset (&a6, 0, sizeof(a6));
2000 #if HAVE_SOCKADDR_IN_SIN_LEN
2001 a6.sin6_len = sizeof (a6);
2003 a6.sin6_family = AF_INET6;
2004 a6.sin6_port = t6->t6_port;
2005 if (t6->t6_port == 0)
2006 is_natd = GNUNET_YES;
2007 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2011 else if (addrlen == sizeof(struct IPv4TcpAddress))
2013 GNUNET_assert(NULL != address->address); /* make static analysis happy */
2014 t4 = address->address;
2015 options = t4->options;
2017 memset (&a4, 0, sizeof(a4));
2018 #if HAVE_SOCKADDR_IN_SIN_LEN
2019 a4.sin_len = sizeof (a4);
2021 a4.sin_family = AF_INET;
2022 a4.sin_port = t4->t4_port;
2023 if (t4->t4_port == 0)
2024 is_natd = GNUNET_YES;
2025 a4.sin_addr.s_addr = t4->ipv4_addr;
2031 GNUNET_STATISTICS_update (plugin->env->stats,
2032 gettext_noop ("# requests to create session with invalid address"),
2038 net_type = plugin->env->get_address_type (plugin->env->cls,
2041 GNUNET_break (net_type != GNUNET_ATS_NET_UNSPECIFIED);
2043 if ( (is_natd == GNUNET_YES) &&
2044 (addrlen == sizeof(struct IPv6TcpAddress)) )
2046 /* NAT client only works with IPv4 addresses */
2050 if (plugin->cur_connections >= plugin->max_connections)
2056 if ( (is_natd == GNUNET_YES) &&
2058 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
2061 /* Only do one NAT punch attempt per peer identity */
2065 if ( (is_natd == GNUNET_YES) &&
2066 (NULL != plugin->nat) &&
2068 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2072 "Found valid IPv4 NAT address (creating session)!\n");
2073 session = create_session (plugin,
2078 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
2079 &nat_connect_timeout,
2081 GNUNET_assert (GNUNET_OK ==
2082 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
2085 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2087 LOG (GNUNET_ERROR_TYPE_DEBUG,
2088 "Created NAT WAIT connection to `%4s' at `%s'\n",
2089 GNUNET_i2s (&session->target),
2090 GNUNET_a2s (sb, sbs));
2091 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
2097 LOG(GNUNET_ERROR_TYPE_DEBUG,
2098 "Running NAT client for `%4s' at `%s' failed\n",
2099 GNUNET_i2s (&session->target),
2100 GNUNET_a2s (sb, sbs));
2101 tcp_plugin_disconnect_session (plugin,
2107 /* create new outbound session */
2108 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
2111 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
2114 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
2121 GNUNET_NETWORK_socket_setsockopt (s,
2125 sizeof (struct GNUNET_PeerIdentity))) ||
2127 GNUNET_NETWORK_socket_setsockopt (s,
2129 TCP_STEALTH_INTEGRITY,
2130 &plugin->my_welcome,
2131 sizeof (struct WelcomeMessage))) )
2133 /* TCP STEALTH not supported by kernel */
2134 GNUNET_break (GNUNET_OK ==
2135 GNUNET_NETWORK_socket_close (s));
2140 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
2149 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2154 "Failed to create connection to `%4s' at `%s'\n",
2155 GNUNET_i2s (&address->peer),
2156 GNUNET_a2s (sb, sbs));
2159 LOG (GNUNET_ERROR_TYPE_DEBUG,
2160 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
2161 GNUNET_i2s (&address->peer),
2162 GNUNET_a2s (sb, sbs));
2164 session = create_session (plugin,
2167 GNUNET_SERVER_connect_socket (plugin->server,
2170 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2173 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2174 /* Send TCP Welcome */
2175 process_pending_messages (session);
2182 * We have been asked to destroy all connections to a particular peer.
2183 * This function is called on each applicable session and must tear it
2186 * @param cls the `struct Plugin *`
2187 * @param key the peer which the session belongs to (unused)
2188 * @param value the `struct GNUNET_ATS_Session`
2189 * @return #GNUNET_YES (continue to iterate)
2192 session_disconnect_it (void *cls,
2193 const struct GNUNET_PeerIdentity *key,
2196 struct Plugin *plugin = cls;
2197 struct GNUNET_ATS_Session *session = value;
2199 GNUNET_STATISTICS_update (session->plugin->env->stats,
2200 gettext_noop ("# transport-service disconnect requests for TCP"),
2203 tcp_plugin_disconnect_session (plugin,
2210 * Function that can be called to force a disconnect from the
2211 * specified neighbour. This should also cancel all previously
2212 * scheduled transmissions. Obviously the transmission may have been
2213 * partially completed already, which is OK. The plugin is supposed
2214 * to close the connection (if applicable) and no longer call the
2215 * transmit continuation(s).
2217 * Finally, plugin MUST NOT call the services's receive function to
2218 * notify the service that the connection to the specified target was
2219 * closed after a getting this call.
2221 * @param cls closure
2222 * @param target peer for which the last transmission is
2226 tcp_plugin_disconnect (void *cls,
2227 const struct GNUNET_PeerIdentity *target)
2229 struct Plugin *plugin = cls;
2231 LOG (GNUNET_ERROR_TYPE_DEBUG,
2232 "Disconnecting peer `%4s'\n",
2233 GNUNET_i2s (target));
2234 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
2236 &session_disconnect_it,
2238 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
2240 &session_disconnect_it,
2246 * We are processing an address pretty printing request and finished
2247 * the IP resolution (if applicable). Append our port and forward the
2248 * result. If called with @a hostname NULL, we are done and should
2249 * clean up the pretty printer (otherwise, there might be multiple
2250 * hostnames for the IP address and we might receive more).
2252 * @param cls the `struct PrettyPrinterContext *`
2253 * @param hostname hostname part of the address
2256 append_port (void *cls,
2257 const char *hostname)
2259 struct PrettyPrinterContext *ppc = cls;
2260 struct Plugin *plugin = ppc->plugin;
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "append_port called with hostname `%s'\n",
2266 if (NULL == hostname)
2268 /* Final call, done */
2269 ppc->resolver_handle = NULL;
2270 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
2271 plugin->ppc_dll_tail,
2273 ppc->asc (ppc->asc_cls,
2279 if (GNUNET_YES == ppc->ipv6)
2280 GNUNET_asprintf (&ret,
2287 GNUNET_asprintf (&ret,
2293 ppc->asc (ppc->asc_cls,
2301 * Convert the transports address to a nice, human-readable format.
2303 * @param cls closure with the `struct Plugin`
2304 * @param type name of the transport that generated the address
2305 * @param addr one of the addresses of the host, NULL for the last address
2306 * the specific address format depends on the transport
2307 * @param addrlen length of the @a addr
2308 * @param numeric should (IP) addresses be displayed in numeric form?
2309 * @param timeout after how long should we give up?
2310 * @param asc function to call on each string
2311 * @param asc_cls closure for @a asc
2314 tcp_plugin_address_pretty_printer (void *cls,
2319 struct GNUNET_TIME_Relative timeout,
2320 GNUNET_TRANSPORT_AddressStringCallback asc,
2323 struct Plugin *plugin = cls;
2324 struct PrettyPrinterContext *ppc;
2327 struct sockaddr_in a4;
2328 struct sockaddr_in6 a6;
2329 const struct IPv4TcpAddress *t4;
2330 const struct IPv6TcpAddress *t6;
2334 if (sizeof(struct IPv6TcpAddress) == addrlen)
2337 memset (&a6, 0, sizeof(a6));
2338 a6.sin6_family = AF_INET6;
2339 a6.sin6_port = t6->t6_port;
2340 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2341 port = ntohs (t6->t6_port);
2342 options = ntohl (t6->options);
2346 else if (sizeof(struct IPv4TcpAddress) == addrlen)
2349 memset (&a4, 0, sizeof(a4));
2350 a4.sin_family = AF_INET;
2351 a4.sin_port = t4->t4_port;
2352 a4.sin_addr.s_addr = t4->ipv4_addr;
2353 port = ntohs (t4->t4_port);
2354 options = ntohl (t4->options);
2360 /* invalid address */
2361 LOG (GNUNET_ERROR_TYPE_WARNING,
2362 _("Unexpected address length: %u bytes\n"),
2363 (unsigned int) addrlen);
2364 asc (asc_cls, NULL, GNUNET_SYSERR);
2365 asc (asc_cls, NULL, GNUNET_OK);
2368 ppc = GNUNET_new (struct PrettyPrinterContext);
2369 ppc->plugin = plugin;
2370 if (addrlen == sizeof(struct IPv6TcpAddress))
2371 ppc->ipv6 = GNUNET_YES;
2373 ppc->ipv6 = GNUNET_NO;
2375 ppc->asc_cls = asc_cls;
2377 ppc->options = options;
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "Starting DNS reverse lookup\n");
2380 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2386 if (NULL == ppc->resolver_handle)
2392 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
2393 plugin->ppc_dll_tail,
2399 * Check if the given port is plausible (must be either our listen
2400 * port or our advertised port), or any port if we are behind NAT
2401 * and do not have a port open. If it is neither, we return
2404 * @param plugin global variables
2405 * @param in_port port number to check
2406 * @return #GNUNET_OK if port is either open_port or adv_port
2409 check_port (struct Plugin *plugin,
2412 if ( (in_port == plugin->adv_port) ||
2413 (in_port == plugin->open_port) )
2415 return GNUNET_SYSERR;
2420 * Function that will be called to check if a binary address for this
2421 * plugin is well-formed and corresponds to an address for THIS peer
2422 * (as per our configuration). Naturally, if absolutely necessary,
2423 * plugins can be a bit conservative in their answer, but in general
2424 * plugins should make sure that the address does not redirect
2425 * traffic to a 3rd party that might try to man-in-the-middle our
2428 * @param cls closure, our `struct Plugin *`
2429 * @param addr pointer to the address
2430 * @param addrlen length of @a addr
2431 * @return #GNUNET_OK if this is a plausible address for this peer
2432 * and transport, #GNUNET_SYSERR if not
2435 tcp_plugin_check_address (void *cls,
2439 struct Plugin *plugin = cls;
2440 const struct IPv4TcpAddress *v4;
2441 const struct IPv6TcpAddress *v6;
2443 if ( (addrlen != sizeof(struct IPv4TcpAddress)) &&
2444 (addrlen != sizeof(struct IPv6TcpAddress)) )
2446 GNUNET_break_op (0);
2447 return GNUNET_SYSERR;
2450 if (addrlen == sizeof(struct IPv4TcpAddress))
2452 v4 = (const struct IPv4TcpAddress *) addr;
2453 if (0 != memcmp (&v4->options,
2458 return GNUNET_SYSERR;
2460 if (GNUNET_OK != check_port (plugin,
2461 ntohs (v4->t4_port)))
2462 return GNUNET_SYSERR;
2464 GNUNET_NAT_test_address (plugin->nat,
2466 sizeof (struct in_addr)))
2467 return GNUNET_SYSERR;
2471 v6 = (const struct IPv6TcpAddress *) addr;
2472 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2474 GNUNET_break_op (0);
2475 return GNUNET_SYSERR;
2477 if (0 != memcmp (&v6->options,
2482 return GNUNET_SYSERR;
2484 if (GNUNET_OK != check_port (plugin,
2485 ntohs (v6->t6_port)))
2486 return GNUNET_SYSERR;
2488 GNUNET_NAT_test_address (plugin->nat,
2490 sizeof(struct in6_addr)))
2491 return GNUNET_SYSERR;
2498 * We've received a nat probe from this peer via TCP. Finish
2499 * creating the client session and resume sending of queued
2502 * @param cls closure
2503 * @param client identification of the client
2504 * @param message the actual message
2507 handle_tcp_nat_probe (void *cls,
2508 struct GNUNET_SERVER_Client *client,
2509 const struct GNUNET_MessageHeader *message)
2511 struct Plugin *plugin = cls;
2512 struct GNUNET_ATS_Session *session;
2513 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2516 struct IPv4TcpAddress *t4;
2517 struct IPv6TcpAddress *t6;
2518 const struct sockaddr_in *s4;
2519 const struct sockaddr_in6 *s6;
2521 LOG (GNUNET_ERROR_TYPE_DEBUG,
2522 "Received NAT probe\n");
2523 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2524 * a connection to this peer by running gnunet-nat-client. This peer
2525 * received the punch message and now wants us to use the new connection
2526 * as the default for that peer. Do so and then send a WELCOME message
2527 * so we can really be connected!
2529 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2532 GNUNET_SERVER_receive_done (client,
2537 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2538 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2539 sizeof(struct GNUNET_PeerIdentity)))
2541 /* refuse connections from ourselves */
2542 GNUNET_SERVER_receive_done (client,
2547 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2548 &tcp_nat_probe->clientIdentity);
2549 if (NULL == session)
2551 LOG (GNUNET_ERROR_TYPE_DEBUG,
2552 "Did NOT find session for NAT probe!\n");
2553 GNUNET_SERVER_receive_done (client,
2557 LOG (GNUNET_ERROR_TYPE_DEBUG,
2558 "Found session for NAT probe!\n");
2560 if (NULL != session->nat_connection_timeout)
2562 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2563 session->nat_connection_timeout = NULL;
2567 GNUNET_SERVER_client_get_address (client,
2572 GNUNET_SERVER_receive_done (client,
2574 tcp_plugin_disconnect_session (plugin,
2578 GNUNET_assert (GNUNET_YES ==
2579 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
2580 &tcp_nat_probe->clientIdentity,
2582 GNUNET_SERVER_client_set_user_context (client,
2584 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2588 session->last_activity = GNUNET_TIME_absolute_get ();
2589 LOG (GNUNET_ERROR_TYPE_DEBUG,
2590 "Found address `%s' for incoming connection\n",
2591 GNUNET_a2s (vaddr, alen));
2592 switch (((const struct sockaddr *) vaddr)->sa_family)
2596 t4 = GNUNET_new (struct IPv4TcpAddress);
2597 t4->options = htonl (TCP_OPTIONS_NONE);
2598 t4->t4_port = s4->sin_port;
2599 t4->ipv4_addr = s4->sin_addr.s_addr;
2600 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2603 sizeof(struct IPv4TcpAddress),
2604 GNUNET_HELLO_ADDRESS_INFO_NONE);
2608 t6 = GNUNET_new (struct IPv6TcpAddress);
2609 t6->options = htonl (TCP_OPTIONS_NONE);
2610 t6->t6_port = s6->sin6_port;
2611 GNUNET_memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2612 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2615 sizeof(struct IPv6TcpAddress),
2616 GNUNET_HELLO_ADDRESS_INFO_NONE);
2620 LOG(GNUNET_ERROR_TYPE_DEBUG,
2621 "Bad address for incoming connection!\n");
2623 GNUNET_SERVER_receive_done (client,
2625 tcp_plugin_disconnect_session (plugin,
2629 GNUNET_free (vaddr);
2630 GNUNET_break (NULL == session->client);
2631 session->client = client;
2632 GNUNET_STATISTICS_update (plugin->env->stats,
2633 gettext_noop ("# TCP sessions active"),
2636 process_pending_messages (session);
2637 GNUNET_SERVER_receive_done (client,
2643 * We've received a welcome from this peer via TCP. Possibly create a
2644 * fresh client record and send back our welcome.
2646 * @param cls closure
2647 * @param client identification of the client
2648 * @param message the actual message
2651 handle_tcp_welcome (void *cls,
2652 struct GNUNET_SERVER_Client *client,
2653 const struct GNUNET_MessageHeader *message)
2655 struct Plugin *plugin = cls;
2656 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2657 struct GNUNET_HELLO_Address *address;
2658 struct GNUNET_ATS_Session *session;
2661 struct IPv4TcpAddress t4;
2662 struct IPv6TcpAddress t6;
2663 const struct sockaddr_in *s4;
2664 const struct sockaddr_in6 *s6;
2666 if (0 == memcmp (&wm->clientIdentity,
2667 plugin->env->my_identity,
2668 sizeof(struct GNUNET_PeerIdentity)))
2670 /* refuse connections from ourselves */
2671 GNUNET_SERVER_receive_done (client,
2674 GNUNET_SERVER_client_get_address (client,
2678 LOG (GNUNET_ERROR_TYPE_INFO,
2679 "Received WELCOME message from my own identity `%4s' on address `%s'\n",
2680 GNUNET_i2s (&wm->clientIdentity),
2681 GNUNET_a2s (vaddr, alen));
2687 LOG(GNUNET_ERROR_TYPE_DEBUG,
2688 "Received WELCOME message from `%4s' %p\n",
2689 GNUNET_i2s (&wm->clientIdentity),
2691 GNUNET_STATISTICS_update (plugin->env->stats,
2692 gettext_noop ("# TCP WELCOME messages received"),
2695 session = lookup_session_by_client (plugin, client);
2696 if (NULL != session)
2699 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2701 LOG (GNUNET_ERROR_TYPE_DEBUG,
2702 "Found existing session %p for peer `%s'\n",
2704 GNUNET_a2s (vaddr, alen));
2705 GNUNET_free (vaddr);
2711 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2713 if (alen == sizeof(struct sockaddr_in))
2716 memset (&t4, '\0', sizeof (t4));
2717 t4.options = htonl (TCP_OPTIONS_NONE);
2718 t4.t4_port = s4->sin_port;
2719 t4.ipv4_addr = s4->sin_addr.s_addr;
2720 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2724 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2726 else if (alen == sizeof(struct sockaddr_in6))
2729 memset (&t6, '\0', sizeof (t6));
2730 t6.options = htonl (TCP_OPTIONS_NONE);
2731 t6.t6_port = s6->sin6_port;
2732 GNUNET_memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2733 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2737 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2742 GNUNET_free_non_null (vaddr);
2743 GNUNET_SERVER_receive_done (client,
2747 session = create_session (plugin,
2749 plugin->env->get_address_type (plugin->env->cls,
2754 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != session->scope);
2755 GNUNET_HELLO_address_free (address);
2756 LOG (GNUNET_ERROR_TYPE_DEBUG,
2757 "Creating new%s session %p for peer `%s' client %p\n",
2758 GNUNET_HELLO_address_check_option (session->address,
2759 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2762 tcp_plugin_address_to_string (plugin,
2763 session->address->address,
2764 session->address->address_length),
2766 GNUNET_free (vaddr);
2767 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2770 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2771 /* Notify transport and ATS about new session */
2772 plugin->env->session_start (plugin->env->cls,
2779 LOG(GNUNET_ERROR_TYPE_DEBUG,
2780 "Did not obtain TCP socket address for incoming connection\n");
2782 GNUNET_SERVER_receive_done (client,
2788 if (session->expecting_welcome != GNUNET_YES)
2791 GNUNET_SERVER_receive_done (client,
2796 session->last_activity = GNUNET_TIME_absolute_get ();
2797 session->expecting_welcome = GNUNET_NO;
2799 process_pending_messages (session);
2800 GNUNET_SERVER_client_set_timeout (client,
2801 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2802 GNUNET_SERVER_receive_done (client,
2808 * We've received data for this peer via TCP. Unbox,
2809 * compute latency and forward.
2811 * @param cls closure
2812 * @param client identification of the client
2813 * @param message the actual message
2816 handle_tcp_data (void *cls,
2817 struct GNUNET_SERVER_Client *client,
2818 const struct GNUNET_MessageHeader *message)
2820 struct Plugin *plugin = cls;
2821 struct GNUNET_ATS_Session *session;
2822 struct GNUNET_TIME_Relative delay;
2825 type = ntohs (message->type);
2826 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2827 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2829 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2830 GNUNET_SERVER_receive_done (client,
2834 session = lookup_session_by_client (plugin, client);
2835 if (NULL == session)
2837 /* No inbound session found */
2841 GNUNET_SERVER_client_get_address (client,
2844 LOG (GNUNET_ERROR_TYPE_ERROR,
2845 "Received unexpected %u bytes of type %u from `%s'\n",
2846 (unsigned int) ntohs (message->size),
2847 (unsigned int) ntohs (message->type),
2851 GNUNET_SERVER_receive_done (client,
2853 GNUNET_free_non_null (vaddr);
2856 if (GNUNET_YES == session->expecting_welcome)
2858 /* Session is expecting WELCOME message */
2862 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2863 LOG (GNUNET_ERROR_TYPE_ERROR,
2864 "Received unexpected %u bytes of type %u from `%s'\n",
2865 (unsigned int) ntohs (message->size),
2866 (unsigned int) ntohs (message->type),
2867 GNUNET_a2s (vaddr, alen));
2869 GNUNET_SERVER_receive_done (client,
2871 GNUNET_free_non_null (vaddr);
2875 session->last_activity = GNUNET_TIME_absolute_get ();
2876 LOG (GNUNET_ERROR_TYPE_DEBUG,
2877 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2878 (unsigned int) ntohs (message->size),
2879 (unsigned int) ntohs (message->type),
2880 GNUNET_i2s (&session->target));
2882 GNUNET_STATISTICS_update (plugin->env->stats,
2883 gettext_noop ("# bytes received via TCP"),
2884 ntohs (message->size),
2887 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2890 delay = plugin->env->receive (plugin->env->cls,
2894 reschedule_session_timeout (session);
2895 if (0 == delay.rel_value_us)
2897 GNUNET_SERVER_receive_done (client,
2902 LOG (GNUNET_ERROR_TYPE_DEBUG,
2903 "Throttling receiving from `%s' for %s\n",
2904 GNUNET_i2s (&session->target),
2905 GNUNET_STRINGS_relative_time_to_string (delay,
2907 GNUNET_SERVER_disable_receive_done_warning (client);
2908 GNUNET_assert (NULL == session->receive_delay_task);
2909 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2917 * Function called whenever a peer is connected on the "SERVER" level.
2918 * Increments number of active connections and suspends server if we
2919 * have reached the limit.
2921 * @param cls closure
2922 * @param client identification of the client
2925 connect_notify (void *cls,
2926 struct GNUNET_SERVER_Client *client)
2928 struct Plugin *plugin = cls;
2932 plugin->cur_connections++;
2933 GNUNET_STATISTICS_set (plugin->env->stats,
2934 gettext_noop ("# TCP server connections active"),
2935 plugin->cur_connections,
2937 GNUNET_STATISTICS_update (plugin->env->stats,
2938 gettext_noop ("# TCP server connect events"),
2941 if (plugin->cur_connections != plugin->max_connections)
2943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2944 _("TCP connection limit reached, suspending server\n"));
2945 GNUNET_STATISTICS_update (plugin->env->stats,
2946 gettext_noop ("# TCP service suspended"),
2949 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2954 * Function called whenever a peer is disconnected on the "SERVER"
2955 * level. Cleans up the connection, decrements number of active
2956 * connections and if applicable resumes listening.
2958 * @param cls closure
2959 * @param client identification of the client
2962 disconnect_notify (void *cls,
2963 struct GNUNET_SERVER_Client *client)
2965 struct Plugin *plugin = cls;
2966 struct GNUNET_ATS_Session *session;
2970 GNUNET_assert (plugin->cur_connections >= 1);
2971 plugin->cur_connections--;
2972 session = lookup_session_by_client (plugin,
2974 if (NULL == session)
2975 return; /* unknown, nothing to do */
2976 LOG (GNUNET_ERROR_TYPE_DEBUG,
2977 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2978 GNUNET_i2s (&session->target),
2979 tcp_plugin_address_to_string (session->plugin,
2980 session->address->address,
2981 session->address->address_length));
2983 if (plugin->cur_connections == plugin->max_connections)
2985 GNUNET_STATISTICS_update (session->plugin->env->stats,
2986 gettext_noop ("# TCP service resumed"),
2989 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2991 GNUNET_STATISTICS_set (plugin->env->stats,
2992 gettext_noop ("# TCP server connections active"),
2993 plugin->cur_connections,
2995 GNUNET_STATISTICS_update (session->plugin->env->stats,
2996 gettext_noop ("# network-level TCP disconnect events"),
2999 tcp_plugin_disconnect_session (plugin,
3005 * We can now send a probe message, copy into buffer to really send.
3007 * @param cls closure, a `struct TCPProbeContext`
3008 * @param size max size to copy
3009 * @param buf buffer to copy message to
3010 * @return number of bytes copied into @a buf
3013 notify_send_probe (void *cls,
3017 struct TCPProbeContext *tcp_probe_ctx = cls;
3018 struct Plugin *plugin = tcp_probe_ctx->plugin;
3021 tcp_probe_ctx->transmit_handle = NULL;
3022 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3027 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
3028 GNUNET_free(tcp_probe_ctx);
3031 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
3033 &tcp_probe_ctx->message,
3034 sizeof(tcp_probe_ctx->message));
3035 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
3036 tcp_probe_ctx->sock);
3037 ret = sizeof(tcp_probe_ctx->message);
3038 GNUNET_free (tcp_probe_ctx);
3044 * Function called by the NAT subsystem suggesting another peer wants
3045 * to connect to us via connection reversal. Try to connect back to the
3048 * @param cls closure
3049 * @param addr address to try
3050 * @param addrlen number of bytes in @a addr
3053 try_connection_reversal (void *cls,
3054 const struct sockaddr *addr,
3057 struct Plugin *plugin = cls;
3058 struct GNUNET_CONNECTION_Handle *sock;
3059 struct TCPProbeContext *tcp_probe_ctx;
3062 * We have received an ICMP response, ostensibly from a peer
3063 * that wants to connect to us! Send a message to establish a connection.
3065 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
3070 /* failed for some odd reason (out of sockets?); ignore attempt */
3074 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
3075 tcp_probe_ctx->message.header.size
3076 = htons (sizeof (struct TCP_NAT_ProbeMessage));
3077 tcp_probe_ctx->message.header.type
3078 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
3079 tcp_probe_ctx->message.clientIdentity
3080 = *plugin->env->my_identity;
3081 tcp_probe_ctx->plugin = plugin;
3082 tcp_probe_ctx->sock = sock;
3083 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
3086 tcp_probe_ctx->transmit_handle
3087 = GNUNET_CONNECTION_notify_transmit_ready (sock,
3088 ntohs (tcp_probe_ctx->message.header.size),
3089 GNUNET_TIME_UNIT_FOREVER_REL,
3096 * Function obtain the network type for a session
3098 * @param cls closure (`struct Plugin *`)
3099 * @param session the session
3100 * @return the network type in HBO or #GNUNET_SYSERR
3102 static enum GNUNET_ATS_Network_Type
3103 tcp_plugin_get_network (void *cls,
3104 struct GNUNET_ATS_Session *session)
3106 return session->scope;
3111 * Function obtain the network type for an address.
3113 * @param cls closure (`struct Plugin *`)
3114 * @param address the address
3115 * @return the network type
3117 static enum GNUNET_ATS_Network_Type
3118 tcp_plugin_get_network_for_address (void *cls,
3119 const struct GNUNET_HELLO_Address *address)
3121 struct Plugin *plugin = cls;
3123 struct sockaddr_in a4;
3124 struct sockaddr_in6 a6;
3125 const struct IPv4TcpAddress *t4;
3126 const struct IPv6TcpAddress *t6;
3130 addrlen = address->address_length;
3131 if (addrlen == sizeof(struct IPv6TcpAddress))
3133 GNUNET_assert (NULL != address->address); /* make static analysis happy */
3134 t6 = address->address;
3135 memset (&a6, 0, sizeof(a6));
3136 #if HAVE_SOCKADDR_IN_SIN_LEN
3137 a6.sin6_len = sizeof (a6);
3139 a6.sin6_family = AF_INET6;
3140 a6.sin6_port = t6->t6_port;
3141 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
3145 else if (addrlen == sizeof(struct IPv4TcpAddress))
3147 GNUNET_assert (NULL != address->address); /* make static analysis happy */
3148 t4 = address->address;
3149 memset (&a4, 0, sizeof(a4));
3150 #if HAVE_SOCKADDR_IN_SIN_LEN
3151 a4.sin_len = sizeof (a4);
3153 a4.sin_family = AF_INET;
3154 a4.sin_port = t4->t4_port;
3155 a4.sin_addr.s_addr = t4->ipv4_addr;
3162 return GNUNET_ATS_NET_UNSPECIFIED;
3164 return plugin->env->get_address_type (plugin->env->cls,
3171 * Return information about the given session to the
3174 * @param cls the `struct Plugin` with the monitor callback (`sic`)
3175 * @param peer peer we send information about
3176 * @param value our `struct GNUNET_ATS_Session` to send information about
3177 * @return #GNUNET_OK (continue to iterate)
3180 send_session_info_iter (void *cls,
3181 const struct GNUNET_PeerIdentity *peer,
3184 struct Plugin *plugin = cls;
3185 struct GNUNET_ATS_Session *session = value;
3187 notify_session_monitor (plugin,
3189 GNUNET_TRANSPORT_SS_INIT);
3190 /* FIXME: cannot tell if this is up or not from current
3192 notify_session_monitor (plugin,
3194 GNUNET_TRANSPORT_SS_UP);
3200 * Begin monitoring sessions of a plugin. There can only
3201 * be one active monitor per plugin (i.e. if there are
3202 * multiple monitors, the transport service needs to
3203 * multiplex the generated events over all of them).
3205 * @param cls closure of the plugin
3206 * @param sic callback to invoke, NULL to disable monitor;
3207 * plugin will being by iterating over all active
3208 * sessions immediately and then enter monitor mode
3209 * @param sic_cls closure for @a sic
3212 tcp_plugin_setup_monitor (void *cls,
3213 GNUNET_TRANSPORT_SessionInfoCallback sic,
3216 struct Plugin *plugin = cls;
3219 plugin->sic_cls = sic_cls;
3222 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3223 &send_session_info_iter,
3225 /* signal end of first iteration */
3226 sic (sic_cls, NULL, NULL);
3232 * Entry point for the plugin.
3234 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
3235 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
3238 libgnunet_plugin_transport_tcp_init (void *cls)
3240 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
3241 { &handle_tcp_welcome, NULL,
3242 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
3243 sizeof(struct WelcomeMessage) },
3244 { &handle_tcp_nat_probe, NULL,
3245 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
3246 sizeof(struct TCP_NAT_ProbeMessage) },
3247 { &handle_tcp_data, NULL,
3248 GNUNET_MESSAGE_TYPE_ALL, 0 },
3249 { NULL, NULL, 0, 0 }
3251 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
3252 struct GNUNET_TRANSPORT_PluginFunctions *api;
3253 struct Plugin *plugin;
3254 struct GNUNET_SERVICE_Context *service;
3255 unsigned long long aport;
3256 unsigned long long bport;
3257 unsigned long long max_connections;
3259 struct GNUNET_TIME_Relative idle_timeout;
3261 struct GNUNET_NETWORK_Handle *const*lsocks;
3265 struct sockaddr **addrs;
3266 socklen_t *addrlens;
3268 if (NULL == env->receive)
3270 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
3271 initialze the plugin or the API */
3272 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3274 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3275 api->address_to_string = &tcp_plugin_address_to_string;
3276 api->string_to_address = &tcp_plugin_string_to_address;
3280 GNUNET_assert (NULL != env->cfg);
3282 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3286 max_connections = 128;
3290 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
3294 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
3295 "ADVERTISED-PORT", &aport)) &&
3298 LOG(GNUNET_ERROR_TYPE_ERROR,
3299 _("Require valid port number for service `%s' in configuration!\n"),
3309 service = GNUNET_SERVICE_start ("transport-tcp",
3311 GNUNET_SERVICE_OPTION_NONE);
3312 if (NULL == service)
3314 LOG (GNUNET_ERROR_TYPE_WARNING,
3315 _("Failed to start service.\n"));
3323 plugin = GNUNET_new (struct Plugin);
3324 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
3326 plugin->max_connections = max_connections;
3327 plugin->open_port = bport;
3328 plugin->adv_port = aport;
3330 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
3331 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
3332 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
3334 if ( (NULL != service) &&
3336 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3341 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
3342 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
3345 uint32_t len = sizeof (struct WelcomeMessage);
3347 for (i=0;NULL!=lsocks[i];i++)
3350 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
3354 sizeof (struct GNUNET_PeerIdentity))) ||
3356 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
3358 TCP_STEALTH_INTEGRITY_LEN,
3362 /* TCP STEALTH not supported by kernel */
3363 GNUNET_assert (0 == i);
3364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3365 _("TCP_STEALTH not supported on this platform.\n"));
3371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3372 _("TCP_STEALTH not supported on this platform.\n"));
3377 if ( (NULL != service) &&
3380 get_server_addresses ("transport-tcp",
3385 for (ret = ret_s-1; ret >= 0; ret--)
3386 LOG (GNUNET_ERROR_TYPE_INFO,
3387 "Binding to address `%s'\n",
3388 GNUNET_a2s (addrs[ret], addrlens[ret]));
3390 = GNUNET_NAT_register (env->cfg,
3393 (unsigned int) ret_s,
3394 (const struct sockaddr **) addrs,
3396 &tcp_nat_port_map_callback,
3397 &try_connection_reversal,
3400 for (ret = ret_s -1; ret >= 0; ret--)
3401 GNUNET_free (addrs[ret]);
3402 GNUNET_free_non_null (addrs);
3403 GNUNET_free_non_null (addrlens);
3407 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3414 &try_connection_reversal,
3418 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3420 api->send = &tcp_plugin_send;
3421 api->get_session = &tcp_plugin_get_session;
3422 api->disconnect_session = &tcp_plugin_disconnect_session;
3423 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
3424 api->disconnect_peer = &tcp_plugin_disconnect;
3425 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3426 api->check_address = &tcp_plugin_check_address;
3427 api->address_to_string = &tcp_plugin_address_to_string;
3428 api->string_to_address = &tcp_plugin_string_to_address;
3429 api->get_network = &tcp_plugin_get_network;
3430 api->get_network_for_address = &tcp_plugin_get_network_for_address;
3431 api->update_session_timeout = &tcp_plugin_update_session_timeout;
3432 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
3433 api->setup_monitor = &tcp_plugin_setup_monitor;
3434 plugin->service = service;
3435 if (NULL != service)
3437 plugin->server = GNUNET_SERVICE_get_server (service);
3442 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3447 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3453 = GNUNET_SERVER_create_with_sockets (NULL,
3459 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3460 GNUNET_memcpy (plugin->handlers,
3462 sizeof(my_handlers));
3463 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
3464 plugin->handlers[i].callback_cls = plugin;
3466 GNUNET_SERVER_add_handlers (plugin->server,
3468 GNUNET_SERVER_connect_notify (plugin->server,
3471 GNUNET_SERVER_disconnect_notify (plugin->server,
3474 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
3477 LOG (GNUNET_ERROR_TYPE_INFO,
3478 _("TCP transport listening on port %llu\n"),
3481 LOG (GNUNET_ERROR_TYPE_INFO,
3482 _("TCP transport not listening on any port (client only)\n"));
3483 if ( (aport != bport) &&
3485 LOG (GNUNET_ERROR_TYPE_INFO,
3486 _("TCP transport advertises itself as being on port %llu\n"),
3488 /* Initially set connections to 0 */
3489 GNUNET_STATISTICS_set (plugin->env->stats,
3490 gettext_noop ("# TCP sessions active"),
3496 if (NULL != plugin->nat)
3497 GNUNET_NAT_unregister (plugin->nat);
3498 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3499 if (NULL != service)
3500 GNUNET_SERVICE_stop (service);
3501 GNUNET_free (plugin);
3502 GNUNET_free_non_null (api);
3508 * Exit point from the plugin.
3510 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
3514 libgnunet_plugin_transport_tcp_done (void *cls)
3516 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3517 struct Plugin *plugin = api->cls;
3518 struct TCPProbeContext *tcp_probe;
3519 struct PrettyPrinterContext *cur;
3520 struct PrettyPrinterContext *next;
3527 LOG (GNUNET_ERROR_TYPE_DEBUG,
3528 "Shutting down TCP plugin\n");
3530 /* Removing leftover sessions */
3531 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3532 &session_disconnect_it,
3534 /* Removing leftover NAT sessions */
3535 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
3536 &session_disconnect_it,
3539 for (cur = plugin->ppc_dll_head; NULL != cur; cur = next)
3542 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3543 plugin->ppc_dll_tail,
3545 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3546 cur->asc (cur->asc_cls,
3552 if (NULL != plugin->service)
3553 GNUNET_SERVICE_stop (plugin->service);
3555 GNUNET_SERVER_destroy (plugin->server);
3556 GNUNET_free (plugin->handlers);
3557 if (NULL != plugin->nat)
3558 GNUNET_NAT_unregister (plugin->nat);
3559 while (NULL != (tcp_probe = plugin->probe_head))
3561 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3564 GNUNET_CONNECTION_destroy (tcp_probe->sock);
3565 GNUNET_free (tcp_probe);
3567 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3568 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3569 GNUNET_break (0 == plugin->cur_connections);
3570 GNUNET_free (plugin);
3575 /* end of plugin_transport_tcp.c */