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
525 * If a session monitor is attached, notify it about the new
528 * @param plugin our plugin
529 * @param session session that changed state
530 * @param state new state of the session
533 notify_session_monitor (struct Plugin *plugin,
534 struct GNUNET_ATS_Session *session,
535 enum GNUNET_TRANSPORT_SessionState state)
537 struct GNUNET_TRANSPORT_SessionInfo info;
539 if (NULL == plugin->sic)
541 memset (&info, 0, sizeof (info));
543 info.is_inbound = GNUNET_HELLO_address_check_option (session->address,
544 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
545 info.num_msg_pending = session->msgs_in_queue;
546 info.num_bytes_pending = session->bytes_in_queue;
547 if (NULL != session->receive_delay_task)
548 info.receive_delay = session->receive_delay;
549 info.session_timeout = session->timeout;
550 info.address = session->address;
551 plugin->sic (plugin->sic_cls,
558 * Our external IP address/port mapping has changed.
560 * @param cls closure, the `struct Plugin`
561 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
562 * the previous (now invalid) one
563 * @param addr either the previous or the new public IP address
564 * @param addrlen actual length of @a addr
567 tcp_nat_port_map_callback (void *cls,
569 const struct sockaddr *addr,
572 struct Plugin *plugin = cls;
573 struct GNUNET_HELLO_Address *address;
574 struct IPv4TcpAddress t4;
575 struct IPv6TcpAddress t6;
579 LOG(GNUNET_ERROR_TYPE_INFO,
580 "NAT notification to %s address `%s'\n",
581 (GNUNET_YES == add_remove) ? "add" : "remove",
582 GNUNET_a2s (addr, addrlen));
583 /* convert 'addr' to our internal format */
584 switch (addr->sa_family)
587 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
588 memset (&t4, 0, sizeof(t4));
589 t4.options = htonl (plugin->myoptions);
590 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
591 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
596 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
597 memset (&t6, 0, sizeof(t6));
598 GNUNET_memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
599 sizeof(struct in6_addr));
600 t6.options = htonl (plugin->myoptions);
601 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
609 /* modify our published address list */
610 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
611 (args == sizeof (struct IPv6TcpAddress)));
612 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
613 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
614 plugin->env->notify_address (plugin->env->cls, add_remove, address);
615 GNUNET_HELLO_address_free(address);
620 * Function called for a quick conversion of the binary address to
621 * a numeric address. Note that the caller must not free the
622 * address and that the next call to this function is allowed
623 * to override the address again.
625 * @param cls closure (`struct Plugin*`)
626 * @param addr binary address
627 * @param addrlen length of @a addr
628 * @return string representing the same address
631 tcp_plugin_address_to_string (void *cls,
635 static char rbuf[INET6_ADDRSTRLEN + 12];
636 char buf[INET6_ADDRSTRLEN];
640 const struct IPv4TcpAddress *t4;
641 const struct IPv6TcpAddress *t6;
648 case sizeof(struct IPv6TcpAddress):
651 port = ntohs (t6->t6_port);
652 options = ntohl (t6->options);
653 GNUNET_memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
656 case sizeof(struct IPv4TcpAddress):
659 port = ntohs (t4->t4_port);
660 options = ntohl (t4->options);
661 GNUNET_memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
665 LOG (GNUNET_ERROR_TYPE_WARNING,
666 _("Unexpected address length: %u bytes\n"),
667 (unsigned int) addrlen);
670 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
672 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
676 GNUNET_snprintf (rbuf, sizeof(rbuf),
677 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
687 * Function called to convert a string address to
690 * @param cls closure (`struct Plugin*`)
691 * @param addr string address
692 * @param addrlen length of the address
693 * @param buf location to store the buffer
694 * @param added location to store the number of bytes in the buffer.
695 * If the function returns #GNUNET_SYSERR, its contents are undefined.
696 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
699 tcp_plugin_string_to_address (void *cls,
705 struct sockaddr_storage socket_address;
711 /* Format tcp.options.address:port */
715 if ((NULL == addr) || (0 == addrlen))
718 return GNUNET_SYSERR;
720 if ('\0' != addr[addrlen - 1])
723 return GNUNET_SYSERR;
725 if (strlen (addr) != addrlen - 1)
728 return GNUNET_SYSERR;
730 plugin = GNUNET_strdup (addr);
731 optionstr = strchr (plugin, '.');
732 if (NULL == optionstr)
736 return GNUNET_SYSERR;
740 options = atol (optionstr);
741 address = strchr (optionstr, '.');
746 return GNUNET_SYSERR;
752 GNUNET_STRINGS_to_address_ip (address,
758 return GNUNET_SYSERR;
762 switch (socket_address.ss_family)
766 struct IPv4TcpAddress *t4;
767 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
768 t4 = GNUNET_new (struct IPv4TcpAddress);
769 t4->options = htonl (options);
770 t4->ipv4_addr = in4->sin_addr.s_addr;
771 t4->t4_port = in4->sin_port;
773 *added = sizeof(struct IPv4TcpAddress);
778 struct IPv6TcpAddress *t6;
779 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
780 t6 = GNUNET_new (struct IPv6TcpAddress);
781 t6->options = htonl (options);
782 t6->ipv6_addr = in6->sin6_addr;
783 t6->t6_port = in6->sin6_port;
785 *added = sizeof(struct IPv6TcpAddress);
789 return GNUNET_SYSERR;
795 * Find the session handle for the given client.
796 * Currently uses both the hashmap and the client
797 * context, as the client context is new and the
798 * logic still needs to be tested.
800 * @param plugin the plugin
801 * @param client which client to find the session handle for
802 * @return NULL if no matching session exists
804 static struct GNUNET_ATS_Session *
805 lookup_session_by_client (struct Plugin *plugin,
806 struct GNUNET_SERVER_Client *client)
808 return GNUNET_SERVER_client_get_user_context (client,
809 struct GNUNET_ATS_Session);
814 * Functions with this signature are called whenever we need
815 * to close a session due to a disconnect or failure to
816 * establish a connection.
818 * @param cls the `struct Plugin`
819 * @param session session to close down
820 * @return #GNUNET_OK on success
823 tcp_plugin_disconnect_session (void *cls,
824 struct GNUNET_ATS_Session *session)
826 struct Plugin *plugin = cls;
827 struct PendingMessage *pm;
829 LOG (GNUNET_ERROR_TYPE_DEBUG,
830 "Disconnecting session of peer `%s' address `%s'\n",
831 GNUNET_i2s (&session->target),
832 tcp_plugin_address_to_string (session->plugin,
833 session->address->address,
834 session->address->address_length));
836 if (NULL != session->timeout_task)
838 GNUNET_SCHEDULER_cancel (session->timeout_task);
839 session->timeout_task = NULL;
840 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
844 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
848 GNUNET_STATISTICS_update (session->plugin->env->stats,
849 gettext_noop ("# TCP sessions active"),
855 GNUNET_assert (GNUNET_YES ==
856 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
860 if (NULL != session->client)
861 GNUNET_SERVER_client_set_user_context (session->client,
865 if (NULL != session->transmit_handle)
867 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
868 session->transmit_handle = NULL;
870 session->plugin->env->session_end (session->plugin->env->cls,
874 if (NULL != session->nat_connection_timeout)
876 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
877 session->nat_connection_timeout = NULL;
880 while (NULL != (pm = session->pending_messages_head))
882 LOG (GNUNET_ERROR_TYPE_DEBUG,
883 (NULL != pm->transmit_cont)
884 ? "Could not deliver message to `%s' at %s.\n"
885 : "Could not deliver message to `%s' at %s, notifying.\n",
886 GNUNET_i2s (&session->target),
887 tcp_plugin_address_to_string (session->plugin,
888 session->address->address,
889 session->address->address_length));
890 GNUNET_STATISTICS_update (session->plugin->env->stats,
891 gettext_noop ("# bytes currently in TCP buffers"),
892 -(int64_t) pm->message_size, GNUNET_NO);
893 GNUNET_STATISTICS_update (session->plugin->env->stats,
894 gettext_noop ("# bytes discarded by TCP (disconnect)"),
897 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
898 session->pending_messages_tail,
900 GNUNET_assert (0 < session->msgs_in_queue);
901 session->msgs_in_queue--;
902 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
903 session->bytes_in_queue -= pm->message_size;
904 if (NULL != pm->transmit_cont)
905 pm->transmit_cont (pm->transmit_cont_cls,
912 GNUNET_assert (0 == session->msgs_in_queue);
913 GNUNET_assert (0 == session->bytes_in_queue);
914 notify_session_monitor (session->plugin,
916 GNUNET_TRANSPORT_SS_DONE);
918 if (NULL != session->receive_delay_task)
920 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
921 session->receive_delay_task = NULL;
923 if (NULL != session->client)
925 GNUNET_SERVER_client_disconnect (session->client);
926 session->client = NULL;
928 GNUNET_HELLO_address_free (session->address);
929 GNUNET_assert (NULL == session->transmit_handle);
930 GNUNET_free (session);
936 * Function that is called to get the keepalive factor.
937 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
938 * calculate the interval between keepalive packets.
940 * @param cls closure with the `struct Plugin`
941 * @return keepalive factor
944 tcp_plugin_query_keepalive_factor (void *cls)
951 * Session was idle for too long, so disconnect it
953 * @param cls the `struct GNUNET_ATS_Session` of the idle session
956 session_timeout (void *cls)
958 struct GNUNET_ATS_Session *s = cls;
959 struct GNUNET_TIME_Relative left;
961 s->timeout_task = NULL;
962 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
963 if (0 != left.rel_value_us)
965 /* not actually our turn yet, but let's at least update
966 the monitor, it may think we're about to die ... */
967 notify_session_monitor (s->plugin,
969 GNUNET_TRANSPORT_SS_UPDATE);
970 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
975 LOG (GNUNET_ERROR_TYPE_DEBUG,
976 "Session %p was idle for %s, disconnecting\n",
978 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
980 /* call session destroy function */
981 tcp_plugin_disconnect_session (s->plugin,
987 * Increment session timeout due to activity.
989 * @param s session to increment timeout for
992 reschedule_session_timeout (struct GNUNET_ATS_Session *s)
994 GNUNET_assert (NULL != s->timeout_task);
995 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1000 * Create a new session. Also queues a welcome message.
1002 * @param plugin the plugin
1003 * @param address the address to create the session for
1004 * @param scope network scope the address is from
1005 * @param client client to use, reference counter must have already been increased
1006 * @param is_nat this a NAT session, we should wait for a client to
1007 * connect to us from an address, then assign that to
1009 * @return new session object
1011 static struct GNUNET_ATS_Session *
1012 create_session (struct Plugin *plugin,
1013 const struct GNUNET_HELLO_Address *address,
1014 enum GNUNET_ATS_Network_Type scope,
1015 struct GNUNET_SERVER_Client *client,
1018 struct GNUNET_ATS_Session *session;
1019 struct PendingMessage *pm;
1021 if (GNUNET_YES != is_nat)
1022 GNUNET_assert (NULL != client);
1024 GNUNET_assert (NULL == client);
1026 LOG (GNUNET_ERROR_TYPE_DEBUG,
1027 "Creating new session for peer `%4s' at address %s\n",
1028 GNUNET_i2s (&address->peer),
1029 tcp_plugin_address_to_string (plugin,
1031 address->address_length));
1032 session = GNUNET_new (struct GNUNET_ATS_Session);
1033 session->last_activity = GNUNET_TIME_absolute_get ();
1034 session->plugin = plugin;
1035 session->is_nat = is_nat;
1038 session->client = client;
1039 GNUNET_SERVER_client_set_user_context (client,
1042 session->address = GNUNET_HELLO_address_copy (address);
1043 session->target = address->peer;
1044 session->expecting_welcome = GNUNET_YES;
1045 session->scope = scope;
1046 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1047 sizeof (struct WelcomeMessage));
1048 pm->msg = (const char *) &pm[1];
1049 pm->message_size = sizeof(struct WelcomeMessage);
1050 GNUNET_memcpy (&pm[1],
1051 &plugin->my_welcome,
1052 sizeof(struct WelcomeMessage));
1053 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1054 GNUNET_STATISTICS_update (plugin->env->stats,
1055 gettext_noop ("# bytes currently in TCP buffers"),
1058 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1059 session->pending_messages_tail,
1061 session->msgs_in_queue++;
1062 session->bytes_in_queue += pm->message_size;
1063 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1064 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1067 notify_session_monitor (session->plugin,
1069 GNUNET_TRANSPORT_SS_INIT);
1070 if (GNUNET_YES != is_nat)
1072 GNUNET_STATISTICS_update (plugin->env->stats,
1073 gettext_noop ("# TCP sessions active"),
1076 notify_session_monitor (session->plugin,
1078 GNUNET_TRANSPORT_SS_UP);
1082 notify_session_monitor (session->plugin,
1084 GNUNET_TRANSPORT_SS_HANDSHAKE);
1091 * If we have pending messages, ask the server to
1092 * transmit them (schedule the respective tasks, etc.)
1094 * @param session for which session should we do this
1097 process_pending_messages (struct GNUNET_ATS_Session *session);
1101 * Function called to notify a client about the socket
1102 * being ready to queue more data. "buf" will be
1103 * NULL and "size" zero if the socket was closed for
1104 * writing in the meantime.
1106 * @param cls closure
1107 * @param size number of bytes available in @a buf
1108 * @param buf where the callee should write the message
1109 * @return number of bytes written to @a buf
1112 do_transmit (void *cls,
1116 struct GNUNET_ATS_Session *session = cls;
1117 struct GNUNET_PeerIdentity pid;
1118 struct Plugin *plugin;
1119 struct PendingMessage *pos;
1120 struct PendingMessage *hd;
1121 struct PendingMessage *tl;
1122 struct GNUNET_TIME_Absolute now;
1126 session->transmit_handle = NULL;
1127 plugin = session->plugin;
1130 LOG (GNUNET_ERROR_TYPE_DEBUG,
1131 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1132 GNUNET_i2s (&session->target));
1133 /* timeout; cancel all messages that have already expired */
1137 now = GNUNET_TIME_absolute_get ();
1138 while ( (NULL != (pos = session->pending_messages_head)) &&
1139 (pos->timeout.abs_value_us <= now.abs_value_us) )
1141 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1142 session->pending_messages_tail,
1144 GNUNET_assert (0 < session->msgs_in_queue);
1145 session->msgs_in_queue--;
1146 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1147 session->bytes_in_queue -= pos->message_size;
1148 LOG (GNUNET_ERROR_TYPE_DEBUG,
1149 "Failed to transmit %u byte message to `%4s'.\n",
1151 GNUNET_i2s (&session->target));
1152 ret += pos->message_size;
1153 GNUNET_CONTAINER_DLL_insert_after (hd,
1158 /* do this call before callbacks (so that if callbacks destroy
1159 * session, they have a chance to cancel actions done by this
1161 process_pending_messages (session);
1162 pid = session->target;
1163 /* no do callbacks and do not use session again since
1164 * the callbacks may abort the session */
1165 while (NULL != (pos = hd))
1167 GNUNET_CONTAINER_DLL_remove (hd,
1170 if (NULL != pos->transmit_cont)
1171 pos->transmit_cont (pos->transmit_cont_cls,
1178 GNUNET_STATISTICS_update (plugin->env->stats,
1179 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1181 GNUNET_STATISTICS_update (plugin->env->stats,
1182 gettext_noop ("# bytes discarded by TCP (timeout)"),
1186 notify_session_monitor (session->plugin,
1188 GNUNET_TRANSPORT_SS_UPDATE);
1191 /* copy all pending messages that would fit */
1196 while (NULL != (pos = session->pending_messages_head))
1198 if (ret + pos->message_size > size)
1200 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1201 session->pending_messages_tail,
1203 GNUNET_assert (0 < session->msgs_in_queue);
1204 session->msgs_in_queue--;
1205 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1206 session->bytes_in_queue -= pos->message_size;
1207 GNUNET_assert(size >= pos->message_size);
1208 LOG (GNUNET_ERROR_TYPE_DEBUG,
1209 "Transmitting message of type %u size %u to peer %s at %s\n",
1210 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1212 GNUNET_i2s (&session->target),
1213 tcp_plugin_address_to_string (session->plugin,
1214 session->address->address,
1215 session->address->address_length));
1216 /* FIXME: this GNUNET_memcpy can be up to 7% of our total runtime */
1217 GNUNET_memcpy (cbuf,
1220 cbuf += pos->message_size;
1221 ret += pos->message_size;
1222 size -= pos->message_size;
1223 GNUNET_CONTAINER_DLL_insert_tail (hd,
1227 notify_session_monitor (session->plugin,
1229 GNUNET_TRANSPORT_SS_UPDATE);
1230 /* schedule 'continuation' before callbacks so that callbacks that
1231 * cancel everything don't cause us to use a session that no longer
1233 process_pending_messages (session);
1234 session->last_activity = GNUNET_TIME_absolute_get ();
1235 pid = session->target;
1236 /* we'll now call callbacks that may cancel the session; hence
1237 * we should not use 'session' after this point */
1238 while (NULL != (pos = hd))
1240 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1241 if (NULL != pos->transmit_cont)
1242 pos->transmit_cont (pos->transmit_cont_cls,
1246 pos->message_size); /* FIXME: include TCP overhead */
1249 GNUNET_assert (NULL == hd);
1250 GNUNET_assert (NULL == tl);
1251 GNUNET_STATISTICS_update (plugin->env->stats,
1252 gettext_noop ("# bytes currently in TCP buffers"),
1255 GNUNET_STATISTICS_update (plugin->env->stats,
1256 gettext_noop ("# bytes transmitted via TCP"),
1264 * If we have pending messages, ask the server to
1265 * transmit them (schedule the respective tasks, etc.)
1267 * @param session for which session should we do this
1270 process_pending_messages (struct GNUNET_ATS_Session *session)
1272 struct PendingMessage *pm;
1274 GNUNET_assert (NULL != session->client);
1275 if (NULL != session->transmit_handle)
1277 if (NULL == (pm = session->pending_messages_head))
1280 session->transmit_handle
1281 = GNUNET_SERVER_notify_transmit_ready (session->client,
1283 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1290 * Function that can be used by the transport service to transmit
1291 * a message using the plugin. Note that in the case of a
1292 * peer disconnecting, the continuation MUST be called
1293 * prior to the disconnect notification itself. This function
1294 * will be called with this peer's HELLO message to initiate
1295 * a fresh connection to another peer.
1297 * @param cls closure
1298 * @param session which session must be used
1299 * @param msgbuf the message to transmit
1300 * @param msgbuf_size number of bytes in @a msgbuf
1301 * @param priority how important is the message (most plugins will
1302 * ignore message priority and just FIFO)
1303 * @param to how long to wait at most for the transmission (does not
1304 * require plugins to discard the message after the timeout,
1305 * just advisory for the desired delay; most plugins will ignore
1307 * @param cont continuation to call once the message has
1308 * been transmitted (or if the transport is ready
1309 * for the next transmission call; or if the
1310 * peer disconnected...); can be NULL
1311 * @param cont_cls closure for @a cont
1312 * @return number of bytes used (on the physical network, with overheads);
1313 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1314 * and does NOT mean that the message was not transmitted (DV)
1317 tcp_plugin_send (void *cls,
1318 struct GNUNET_ATS_Session *session,
1321 unsigned int priority,
1322 struct GNUNET_TIME_Relative to,
1323 GNUNET_TRANSPORT_TransmitContinuation cont,
1326 struct Plugin * plugin = cls;
1327 struct PendingMessage *pm;
1329 /* create new message entry */
1330 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1331 pm->msg = (const char *) &pm[1];
1332 GNUNET_memcpy (&pm[1], msgbuf, msgbuf_size);
1333 pm->message_size = msgbuf_size;
1334 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1335 pm->transmit_cont = cont;
1336 pm->transmit_cont_cls = cont_cls;
1338 LOG(GNUNET_ERROR_TYPE_DEBUG,
1339 "Asked to transmit %u bytes to `%s', added message to list.\n",
1341 GNUNET_i2s (&session->target));
1344 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1348 GNUNET_assert (NULL != session->client);
1349 GNUNET_SERVER_client_set_timeout (session->client,
1350 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1351 GNUNET_STATISTICS_update (plugin->env->stats,
1352 gettext_noop ("# bytes currently in TCP buffers"),
1356 /* append pm to pending_messages list */
1357 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1358 session->pending_messages_tail,
1360 notify_session_monitor (session->plugin,
1362 GNUNET_TRANSPORT_SS_UPDATE);
1363 session->msgs_in_queue++;
1364 session->bytes_in_queue += pm->message_size;
1365 process_pending_messages (session);
1369 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1373 LOG (GNUNET_ERROR_TYPE_DEBUG,
1374 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1375 GNUNET_i2s (&session->target));
1376 GNUNET_STATISTICS_update (plugin->env->stats,
1377 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1379 /* append pm to pending_messages list */
1380 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1381 session->pending_messages_tail,
1383 session->msgs_in_queue++;
1384 session->bytes_in_queue += pm->message_size;
1385 notify_session_monitor (session->plugin,
1387 GNUNET_TRANSPORT_SS_HANDSHAKE);
1390 LOG (GNUNET_ERROR_TYPE_ERROR,
1391 "Invalid session %p\n",
1401 return GNUNET_SYSERR; /* session does not exist here */
1406 * Closure for #session_lookup_it().
1408 struct GNUNET_ATS_SessionItCtx
1411 * Address we are looking for.
1413 const struct GNUNET_HELLO_Address *address;
1416 * Where to store the session (if we found it).
1418 struct GNUNET_ATS_Session *result;
1424 * Look for a session by address.
1426 * @param cls the `struct GNUNET_ATS_SessionItCtx`
1428 * @param value a `struct GNUNET_ATS_Session`
1429 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1432 session_lookup_it (void *cls,
1433 const struct GNUNET_PeerIdentity *key,
1436 struct GNUNET_ATS_SessionItCtx *si_ctx = cls;
1437 struct GNUNET_ATS_Session *session = value;
1440 GNUNET_HELLO_address_cmp (si_ctx->address,
1443 si_ctx->result = session;
1449 * Task cleaning up a NAT connection attempt after timeout
1451 * @param cls the `struct GNUNET_ATS_Session`
1454 nat_connect_timeout (void *cls)
1456 struct GNUNET_ATS_Session *session = cls;
1458 session->nat_connection_timeout = NULL;
1459 LOG (GNUNET_ERROR_TYPE_DEBUG,
1460 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1461 GNUNET_i2s (&session->target),
1462 tcp_plugin_address_to_string (session->plugin,
1463 session->address->address,
1464 session->address->address_length));
1465 tcp_plugin_disconnect_session (session->plugin,
1471 * Function that will be called whenever the transport service wants to
1472 * notify the plugin that a session is still active and in use and
1473 * therefore the session timeout for this session has to be updated
1475 * @param cls closure
1476 * @param peer which peer was the session for
1477 * @param session which session is being updated
1480 tcp_plugin_update_session_timeout (void *cls,
1481 const struct GNUNET_PeerIdentity *peer,
1482 struct GNUNET_ATS_Session *session)
1484 reschedule_session_timeout (session);
1489 * Task to signal the server that we can continue
1490 * receiving from the TCP client now.
1492 * @param cls the `struct GNUNET_ATS_Session *`
1495 delayed_done (void *cls)
1497 struct GNUNET_ATS_Session *session = cls;
1499 session->receive_delay_task = NULL;
1500 reschedule_session_timeout (session);
1501 GNUNET_SERVER_receive_done (session->client,
1507 * Function that will be called whenever the transport service wants to
1508 * notify the plugin that the inbound quota changed and that the plugin
1509 * should update it's delay for the next receive value
1511 * @param cls closure
1512 * @param peer which peer was the session for
1513 * @param session which session is being updated
1514 * @param delay new delay to use for receiving
1517 tcp_plugin_update_inbound_delay (void *cls,
1518 const struct GNUNET_PeerIdentity *peer,
1519 struct GNUNET_ATS_Session *session,
1520 struct GNUNET_TIME_Relative delay)
1522 if (NULL == session->receive_delay_task)
1524 LOG (GNUNET_ERROR_TYPE_DEBUG,
1525 "New inbound delay %s\n",
1526 GNUNET_STRINGS_relative_time_to_string (delay,
1528 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1529 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1530 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1537 * Create a new session to transmit data to the target
1538 * This session will used to send data to this peer and the plugin will
1539 * notify us by calling the env->session_end function
1541 * @param cls closure
1542 * @param address the address to use
1543 * @return the session if the address is valid, NULL otherwise
1545 static struct GNUNET_ATS_Session *
1546 tcp_plugin_get_session (void *cls,
1547 const struct GNUNET_HELLO_Address *address)
1549 struct Plugin *plugin = cls;
1550 struct GNUNET_ATS_Session *session = NULL;
1554 struct GNUNET_CONNECTION_Handle *sa;
1555 struct sockaddr_in a4;
1556 struct sockaddr_in6 a6;
1557 const struct IPv4TcpAddress *t4;
1558 const struct IPv6TcpAddress *t6;
1559 unsigned int options;
1560 enum GNUNET_ATS_Network_Type net_type;
1561 unsigned int is_natd = GNUNET_NO;
1564 struct GNUNET_NETWORK_Handle *s;
1567 addrlen = address->address_length;
1568 LOG (GNUNET_ERROR_TYPE_DEBUG,
1569 "Trying to get session for `%s' address of peer `%s'\n",
1570 tcp_plugin_address_to_string (plugin,
1572 address->address_length),
1573 GNUNET_i2s (&address->peer));
1575 if (GNUNET_HELLO_address_check_option (address,
1576 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1582 /* look for existing session */
1584 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1587 struct GNUNET_ATS_SessionItCtx si_ctx;
1589 si_ctx.address = address;
1590 si_ctx.result = NULL;
1591 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1595 if (NULL != si_ctx.result)
1597 session = si_ctx.result;
1598 LOG (GNUNET_ERROR_TYPE_DEBUG,
1599 "Found existing session for `%s' address `%s'\n",
1600 GNUNET_i2s (&address->peer),
1601 tcp_plugin_address_to_string (plugin,
1603 address->address_length));
1608 if (addrlen == sizeof(struct IPv6TcpAddress))
1610 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1611 t6 = address->address;
1612 options = t6->options;
1614 memset (&a6, 0, sizeof(a6));
1615 #if HAVE_SOCKADDR_IN_SIN_LEN
1616 a6.sin6_len = sizeof (a6);
1618 a6.sin6_family = AF_INET6;
1619 a6.sin6_port = t6->t6_port;
1620 if (t6->t6_port == 0)
1621 is_natd = GNUNET_YES;
1622 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1626 else if (addrlen == sizeof(struct IPv4TcpAddress))
1628 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1629 t4 = address->address;
1630 options = t4->options;
1632 memset (&a4, 0, sizeof(a4));
1633 #if HAVE_SOCKADDR_IN_SIN_LEN
1634 a4.sin_len = sizeof (a4);
1636 a4.sin_family = AF_INET;
1637 a4.sin_port = t4->t4_port;
1638 if (t4->t4_port == 0)
1639 is_natd = GNUNET_YES;
1640 a4.sin_addr.s_addr = t4->ipv4_addr;
1646 GNUNET_STATISTICS_update (plugin->env->stats,
1647 gettext_noop ("# requests to create session with invalid address"),
1653 net_type = plugin->env->get_address_type (plugin->env->cls,
1656 GNUNET_break (net_type != GNUNET_ATS_NET_UNSPECIFIED);
1658 if ( (is_natd == GNUNET_YES) &&
1659 (addrlen == sizeof(struct IPv6TcpAddress)) )
1661 /* NAT client only works with IPv4 addresses */
1665 if (plugin->cur_connections >= plugin->max_connections)
1671 if ( (is_natd == GNUNET_YES) &&
1673 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1676 /* Only do one NAT punch attempt per peer identity */
1680 if ( (is_natd == GNUNET_YES) &&
1681 (NULL != plugin->nat) &&
1683 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Found valid IPv4 NAT address (creating session)!\n");
1688 session = create_session (plugin,
1693 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1694 &nat_connect_timeout,
1696 GNUNET_assert (GNUNET_OK ==
1697 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1702 LOG (GNUNET_ERROR_TYPE_DEBUG,
1703 "Created NAT WAIT connection to `%4s' at `%s'\n",
1704 GNUNET_i2s (&session->target),
1705 GNUNET_a2s (sb, sbs));
1706 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1712 LOG(GNUNET_ERROR_TYPE_DEBUG,
1713 "Running NAT client for `%4s' at `%s' failed\n",
1714 GNUNET_i2s (&session->target),
1715 GNUNET_a2s (sb, sbs));
1716 tcp_plugin_disconnect_session (plugin,
1722 /* create new outbound session */
1723 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1726 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1729 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1736 GNUNET_NETWORK_socket_setsockopt (s,
1740 sizeof (struct GNUNET_PeerIdentity))) ||
1742 GNUNET_NETWORK_socket_setsockopt (s,
1744 TCP_STEALTH_INTEGRITY,
1745 &plugin->my_welcome,
1746 sizeof (struct WelcomeMessage))) )
1748 /* TCP STEALTH not supported by kernel */
1749 GNUNET_break (GNUNET_OK ==
1750 GNUNET_NETWORK_socket_close (s));
1755 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1764 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1768 LOG (GNUNET_ERROR_TYPE_DEBUG,
1769 "Failed to create connection to `%4s' at `%s'\n",
1770 GNUNET_i2s (&address->peer),
1771 GNUNET_a2s (sb, sbs));
1774 LOG (GNUNET_ERROR_TYPE_DEBUG,
1775 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1776 GNUNET_i2s (&address->peer),
1777 GNUNET_a2s (sb, sbs));
1779 session = create_session (plugin,
1782 GNUNET_SERVER_connect_socket (plugin->server,
1785 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1788 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1789 /* Send TCP Welcome */
1790 process_pending_messages (session);
1797 * We have been asked to destroy all connections to a particular peer.
1798 * This function is called on each applicable session and must tear it
1801 * @param cls the `struct Plugin *`
1802 * @param key the peer which the session belongs to (unused)
1803 * @param value the `struct GNUNET_ATS_Session`
1804 * @return #GNUNET_YES (continue to iterate)
1807 session_disconnect_it (void *cls,
1808 const struct GNUNET_PeerIdentity *key,
1811 struct Plugin *plugin = cls;
1812 struct GNUNET_ATS_Session *session = value;
1814 GNUNET_STATISTICS_update (session->plugin->env->stats,
1815 gettext_noop ("# transport-service disconnect requests for TCP"),
1818 tcp_plugin_disconnect_session (plugin,
1825 * Function that can be called to force a disconnect from the
1826 * specified neighbour. This should also cancel all previously
1827 * scheduled transmissions. Obviously the transmission may have been
1828 * partially completed already, which is OK. The plugin is supposed
1829 * to close the connection (if applicable) and no longer call the
1830 * transmit continuation(s).
1832 * Finally, plugin MUST NOT call the services's receive function to
1833 * notify the service that the connection to the specified target was
1834 * closed after a getting this call.
1836 * @param cls closure
1837 * @param target peer for which the last transmission is
1841 tcp_plugin_disconnect (void *cls,
1842 const struct GNUNET_PeerIdentity *target)
1844 struct Plugin *plugin = cls;
1846 LOG (GNUNET_ERROR_TYPE_DEBUG,
1847 "Disconnecting peer `%4s'\n",
1848 GNUNET_i2s (target));
1849 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1851 &session_disconnect_it,
1853 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1855 &session_disconnect_it,
1861 * We are processing an address pretty printing request and finished
1862 * the IP resolution (if applicable). Append our port and forward the
1863 * result. If called with @a hostname NULL, we are done and should
1864 * clean up the pretty printer (otherwise, there might be multiple
1865 * hostnames for the IP address and we might receive more).
1867 * @param cls the `struct PrettyPrinterContext *`
1868 * @param hostname hostname part of the address
1871 append_port (void *cls,
1872 const char *hostname)
1874 struct PrettyPrinterContext *ppc = cls;
1875 struct Plugin *plugin = ppc->plugin;
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "append_port called with hostname `%s'\n",
1881 if (NULL == hostname)
1883 /* Final call, done */
1884 ppc->resolver_handle = NULL;
1885 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1886 plugin->ppc_dll_tail,
1888 ppc->asc (ppc->asc_cls,
1894 if (GNUNET_YES == ppc->ipv6)
1895 GNUNET_asprintf (&ret,
1902 GNUNET_asprintf (&ret,
1908 ppc->asc (ppc->asc_cls,
1916 * Convert the transports address to a nice, human-readable format.
1918 * @param cls closure with the `struct Plugin`
1919 * @param type name of the transport that generated the address
1920 * @param addr one of the addresses of the host, NULL for the last address
1921 * the specific address format depends on the transport
1922 * @param addrlen length of the @a addr
1923 * @param numeric should (IP) addresses be displayed in numeric form?
1924 * @param timeout after how long should we give up?
1925 * @param asc function to call on each string
1926 * @param asc_cls closure for @a asc
1929 tcp_plugin_address_pretty_printer (void *cls,
1934 struct GNUNET_TIME_Relative timeout,
1935 GNUNET_TRANSPORT_AddressStringCallback asc,
1938 struct Plugin *plugin = cls;
1939 struct PrettyPrinterContext *ppc;
1942 struct sockaddr_in a4;
1943 struct sockaddr_in6 a6;
1944 const struct IPv4TcpAddress *t4;
1945 const struct IPv6TcpAddress *t6;
1949 if (sizeof(struct IPv6TcpAddress) == addrlen)
1952 memset (&a6, 0, sizeof(a6));
1953 a6.sin6_family = AF_INET6;
1954 a6.sin6_port = t6->t6_port;
1955 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1956 port = ntohs (t6->t6_port);
1957 options = ntohl (t6->options);
1961 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1964 memset (&a4, 0, sizeof(a4));
1965 a4.sin_family = AF_INET;
1966 a4.sin_port = t4->t4_port;
1967 a4.sin_addr.s_addr = t4->ipv4_addr;
1968 port = ntohs (t4->t4_port);
1969 options = ntohl (t4->options);
1975 /* invalid address */
1976 LOG (GNUNET_ERROR_TYPE_WARNING,
1977 _("Unexpected address length: %u bytes\n"),
1978 (unsigned int) addrlen);
1979 asc (asc_cls, NULL, GNUNET_SYSERR);
1980 asc (asc_cls, NULL, GNUNET_OK);
1983 ppc = GNUNET_new (struct PrettyPrinterContext);
1984 ppc->plugin = plugin;
1985 if (addrlen == sizeof(struct IPv6TcpAddress))
1986 ppc->ipv6 = GNUNET_YES;
1988 ppc->ipv6 = GNUNET_NO;
1990 ppc->asc_cls = asc_cls;
1992 ppc->options = options;
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1994 "Starting DNS reverse lookup\n");
1995 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2001 if (NULL == ppc->resolver_handle)
2007 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
2008 plugin->ppc_dll_tail,
2014 * Check if the given port is plausible (must be either our listen
2015 * port or our advertised port), or any port if we are behind NAT
2016 * and do not have a port open. If it is neither, we return
2019 * @param plugin global variables
2020 * @param in_port port number to check
2021 * @return #GNUNET_OK if port is either open_port or adv_port
2024 check_port (struct Plugin *plugin,
2027 if ( (in_port == plugin->adv_port) ||
2028 (in_port == plugin->open_port) )
2030 return GNUNET_SYSERR;
2035 * Function that will be called to check if a binary address for this
2036 * plugin is well-formed and corresponds to an address for THIS peer
2037 * (as per our configuration). Naturally, if absolutely necessary,
2038 * plugins can be a bit conservative in their answer, but in general
2039 * plugins should make sure that the address does not redirect
2040 * traffic to a 3rd party that might try to man-in-the-middle our
2043 * @param cls closure, our `struct Plugin *`
2044 * @param addr pointer to the address
2045 * @param addrlen length of @a addr
2046 * @return #GNUNET_OK if this is a plausible address for this peer
2047 * and transport, #GNUNET_SYSERR if not
2050 tcp_plugin_check_address (void *cls,
2054 struct Plugin *plugin = cls;
2055 const struct IPv4TcpAddress *v4;
2056 const struct IPv6TcpAddress *v6;
2058 if ( (addrlen != sizeof(struct IPv4TcpAddress)) &&
2059 (addrlen != sizeof(struct IPv6TcpAddress)) )
2061 GNUNET_break_op (0);
2062 return GNUNET_SYSERR;
2065 if (addrlen == sizeof(struct IPv4TcpAddress))
2067 v4 = (const struct IPv4TcpAddress *) addr;
2068 if (0 != memcmp (&v4->options,
2073 return GNUNET_SYSERR;
2075 if (GNUNET_OK != check_port (plugin,
2076 ntohs (v4->t4_port)))
2077 return GNUNET_SYSERR;
2079 GNUNET_NAT_test_address (plugin->nat,
2081 sizeof (struct in_addr)))
2082 return GNUNET_SYSERR;
2086 v6 = (const struct IPv6TcpAddress *) addr;
2087 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2089 GNUNET_break_op (0);
2090 return GNUNET_SYSERR;
2092 if (0 != memcmp (&v6->options,
2097 return GNUNET_SYSERR;
2099 if (GNUNET_OK != check_port (plugin,
2100 ntohs (v6->t6_port)))
2101 return GNUNET_SYSERR;
2103 GNUNET_NAT_test_address (plugin->nat,
2105 sizeof(struct in6_addr)))
2106 return GNUNET_SYSERR;
2113 * We've received a nat probe from this peer via TCP. Finish
2114 * creating the client session and resume sending of queued
2117 * @param cls closure
2118 * @param client identification of the client
2119 * @param message the actual message
2122 handle_tcp_nat_probe (void *cls,
2123 struct GNUNET_SERVER_Client *client,
2124 const struct GNUNET_MessageHeader *message)
2126 struct Plugin *plugin = cls;
2127 struct GNUNET_ATS_Session *session;
2128 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2131 struct IPv4TcpAddress *t4;
2132 struct IPv6TcpAddress *t6;
2133 const struct sockaddr_in *s4;
2134 const struct sockaddr_in6 *s6;
2136 LOG (GNUNET_ERROR_TYPE_DEBUG,
2137 "Received NAT probe\n");
2138 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2139 * a connection to this peer by running gnunet-nat-client. This peer
2140 * received the punch message and now wants us to use the new connection
2141 * as the default for that peer. Do so and then send a WELCOME message
2142 * so we can really be connected!
2144 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2147 GNUNET_SERVER_receive_done (client,
2152 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2153 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2154 sizeof(struct GNUNET_PeerIdentity)))
2156 /* refuse connections from ourselves */
2157 GNUNET_SERVER_receive_done (client,
2162 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2163 &tcp_nat_probe->clientIdentity);
2164 if (NULL == session)
2166 LOG (GNUNET_ERROR_TYPE_DEBUG,
2167 "Did NOT find session for NAT probe!\n");
2168 GNUNET_SERVER_receive_done (client,
2172 LOG (GNUNET_ERROR_TYPE_DEBUG,
2173 "Found session for NAT probe!\n");
2175 if (NULL != session->nat_connection_timeout)
2177 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2178 session->nat_connection_timeout = NULL;
2182 GNUNET_SERVER_client_get_address (client,
2187 GNUNET_SERVER_receive_done (client,
2189 tcp_plugin_disconnect_session (plugin,
2193 GNUNET_assert (GNUNET_YES ==
2194 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
2195 &tcp_nat_probe->clientIdentity,
2197 GNUNET_SERVER_client_set_user_context (client,
2199 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2203 session->last_activity = GNUNET_TIME_absolute_get ();
2204 LOG (GNUNET_ERROR_TYPE_DEBUG,
2205 "Found address `%s' for incoming connection\n",
2206 GNUNET_a2s (vaddr, alen));
2207 switch (((const struct sockaddr *) vaddr)->sa_family)
2211 t4 = GNUNET_new (struct IPv4TcpAddress);
2212 t4->options = htonl (TCP_OPTIONS_NONE);
2213 t4->t4_port = s4->sin_port;
2214 t4->ipv4_addr = s4->sin_addr.s_addr;
2215 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2218 sizeof(struct IPv4TcpAddress),
2219 GNUNET_HELLO_ADDRESS_INFO_NONE);
2223 t6 = GNUNET_new (struct IPv6TcpAddress);
2224 t6->options = htonl (TCP_OPTIONS_NONE);
2225 t6->t6_port = s6->sin6_port;
2226 GNUNET_memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2227 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2230 sizeof(struct IPv6TcpAddress),
2231 GNUNET_HELLO_ADDRESS_INFO_NONE);
2235 LOG(GNUNET_ERROR_TYPE_DEBUG,
2236 "Bad address for incoming connection!\n");
2238 GNUNET_SERVER_receive_done (client,
2240 tcp_plugin_disconnect_session (plugin,
2244 GNUNET_free (vaddr);
2245 GNUNET_break (NULL == session->client);
2246 session->client = client;
2247 GNUNET_STATISTICS_update (plugin->env->stats,
2248 gettext_noop ("# TCP sessions active"),
2251 process_pending_messages (session);
2252 GNUNET_SERVER_receive_done (client,
2258 * We've received a welcome from this peer via TCP. Possibly create a
2259 * fresh client record and send back our welcome.
2261 * @param cls closure
2262 * @param client identification of the client
2263 * @param message the actual message
2266 handle_tcp_welcome (void *cls,
2267 struct GNUNET_SERVER_Client *client,
2268 const struct GNUNET_MessageHeader *message)
2270 struct Plugin *plugin = cls;
2271 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2272 struct GNUNET_HELLO_Address *address;
2273 struct GNUNET_ATS_Session *session;
2276 struct IPv4TcpAddress t4;
2277 struct IPv6TcpAddress t6;
2278 const struct sockaddr_in *s4;
2279 const struct sockaddr_in6 *s6;
2281 if (0 == memcmp (&wm->clientIdentity,
2282 plugin->env->my_identity,
2283 sizeof(struct GNUNET_PeerIdentity)))
2285 /* refuse connections from ourselves */
2286 GNUNET_SERVER_receive_done (client,
2289 GNUNET_SERVER_client_get_address (client,
2293 LOG (GNUNET_ERROR_TYPE_INFO,
2294 "Received WELCOME message from my own identity `%4s' on address `%s'\n",
2295 GNUNET_i2s (&wm->clientIdentity),
2296 GNUNET_a2s (vaddr, alen));
2302 LOG(GNUNET_ERROR_TYPE_DEBUG,
2303 "Received WELCOME message from `%4s' %p\n",
2304 GNUNET_i2s (&wm->clientIdentity),
2306 GNUNET_STATISTICS_update (plugin->env->stats,
2307 gettext_noop ("# TCP WELCOME messages received"),
2310 session = lookup_session_by_client (plugin, client);
2311 if (NULL != session)
2314 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2316 LOG (GNUNET_ERROR_TYPE_DEBUG,
2317 "Found existing session %p for peer `%s'\n",
2319 GNUNET_a2s (vaddr, alen));
2320 GNUNET_free (vaddr);
2326 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2328 if (alen == sizeof(struct sockaddr_in))
2331 memset (&t4, '\0', sizeof (t4));
2332 t4.options = htonl (TCP_OPTIONS_NONE);
2333 t4.t4_port = s4->sin_port;
2334 t4.ipv4_addr = s4->sin_addr.s_addr;
2335 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2339 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2341 else if (alen == sizeof(struct sockaddr_in6))
2344 memset (&t6, '\0', sizeof (t6));
2345 t6.options = htonl (TCP_OPTIONS_NONE);
2346 t6.t6_port = s6->sin6_port;
2347 GNUNET_memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2348 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2352 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2357 GNUNET_free_non_null (vaddr);
2358 GNUNET_SERVER_receive_done (client,
2362 session = create_session (plugin,
2364 plugin->env->get_address_type (plugin->env->cls,
2369 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != session->scope);
2370 GNUNET_HELLO_address_free (address);
2371 LOG (GNUNET_ERROR_TYPE_DEBUG,
2372 "Creating new%s session %p for peer `%s' client %p\n",
2373 GNUNET_HELLO_address_check_option (session->address,
2374 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2377 tcp_plugin_address_to_string (plugin,
2378 session->address->address,
2379 session->address->address_length),
2381 GNUNET_free (vaddr);
2382 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2386 /* Notify transport and ATS about new session */
2387 plugin->env->session_start (plugin->env->cls,
2394 LOG(GNUNET_ERROR_TYPE_DEBUG,
2395 "Did not obtain TCP socket address for incoming connection\n");
2397 GNUNET_SERVER_receive_done (client,
2403 if (session->expecting_welcome != GNUNET_YES)
2406 GNUNET_SERVER_receive_done (client,
2411 session->last_activity = GNUNET_TIME_absolute_get ();
2412 session->expecting_welcome = GNUNET_NO;
2414 process_pending_messages (session);
2415 GNUNET_SERVER_client_set_timeout (client,
2416 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2417 GNUNET_SERVER_receive_done (client,
2423 * We've received data for this peer via TCP. Unbox,
2424 * compute latency and forward.
2426 * @param cls closure
2427 * @param client identification of the client
2428 * @param message the actual message
2431 handle_tcp_data (void *cls,
2432 struct GNUNET_SERVER_Client *client,
2433 const struct GNUNET_MessageHeader *message)
2435 struct Plugin *plugin = cls;
2436 struct GNUNET_ATS_Session *session;
2437 struct GNUNET_TIME_Relative delay;
2440 type = ntohs (message->type);
2441 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2442 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2444 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2445 GNUNET_SERVER_receive_done (client,
2449 session = lookup_session_by_client (plugin, client);
2450 if (NULL == session)
2452 /* No inbound session found */
2456 GNUNET_SERVER_client_get_address (client,
2459 LOG (GNUNET_ERROR_TYPE_ERROR,
2460 "Received unexpected %u bytes of type %u from `%s'\n",
2461 (unsigned int) ntohs (message->size),
2462 (unsigned int) ntohs (message->type),
2466 GNUNET_SERVER_receive_done (client,
2468 GNUNET_free_non_null (vaddr);
2471 if (GNUNET_YES == session->expecting_welcome)
2473 /* Session is expecting WELCOME message */
2477 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2478 LOG (GNUNET_ERROR_TYPE_ERROR,
2479 "Received unexpected %u bytes of type %u from `%s'\n",
2480 (unsigned int) ntohs (message->size),
2481 (unsigned int) ntohs (message->type),
2482 GNUNET_a2s (vaddr, alen));
2484 GNUNET_SERVER_receive_done (client,
2486 GNUNET_free_non_null (vaddr);
2490 session->last_activity = GNUNET_TIME_absolute_get ();
2491 LOG (GNUNET_ERROR_TYPE_DEBUG,
2492 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2493 (unsigned int) ntohs (message->size),
2494 (unsigned int) ntohs (message->type),
2495 GNUNET_i2s (&session->target));
2497 GNUNET_STATISTICS_update (plugin->env->stats,
2498 gettext_noop ("# bytes received via TCP"),
2499 ntohs (message->size),
2502 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2505 delay = plugin->env->receive (plugin->env->cls,
2509 reschedule_session_timeout (session);
2510 if (0 == delay.rel_value_us)
2512 GNUNET_SERVER_receive_done (client,
2517 LOG (GNUNET_ERROR_TYPE_DEBUG,
2518 "Throttling receiving from `%s' for %s\n",
2519 GNUNET_i2s (&session->target),
2520 GNUNET_STRINGS_relative_time_to_string (delay,
2522 GNUNET_SERVER_disable_receive_done_warning (client);
2523 GNUNET_assert (NULL == session->receive_delay_task);
2524 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2532 * Function called whenever a peer is connected on the "SERVER" level.
2533 * Increments number of active connections and suspends server if we
2534 * have reached the limit.
2536 * @param cls closure
2537 * @param client identification of the client
2540 connect_notify (void *cls,
2541 struct GNUNET_SERVER_Client *client)
2543 struct Plugin *plugin = cls;
2547 plugin->cur_connections++;
2548 GNUNET_STATISTICS_set (plugin->env->stats,
2549 gettext_noop ("# TCP server connections active"),
2550 plugin->cur_connections,
2552 GNUNET_STATISTICS_update (plugin->env->stats,
2553 gettext_noop ("# TCP server connect events"),
2556 if (plugin->cur_connections != plugin->max_connections)
2558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2559 _("TCP connection limit reached, suspending server\n"));
2560 GNUNET_STATISTICS_update (plugin->env->stats,
2561 gettext_noop ("# TCP service suspended"),
2564 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2569 * Function called whenever a peer is disconnected on the "SERVER"
2570 * level. Cleans up the connection, decrements number of active
2571 * connections and if applicable resumes listening.
2573 * @param cls closure
2574 * @param client identification of the client
2577 disconnect_notify (void *cls,
2578 struct GNUNET_SERVER_Client *client)
2580 struct Plugin *plugin = cls;
2581 struct GNUNET_ATS_Session *session;
2585 GNUNET_assert (plugin->cur_connections >= 1);
2586 plugin->cur_connections--;
2587 session = lookup_session_by_client (plugin,
2589 if (NULL == session)
2590 return; /* unknown, nothing to do */
2591 LOG (GNUNET_ERROR_TYPE_DEBUG,
2592 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2593 GNUNET_i2s (&session->target),
2594 tcp_plugin_address_to_string (session->plugin,
2595 session->address->address,
2596 session->address->address_length));
2598 if (plugin->cur_connections == plugin->max_connections)
2600 GNUNET_STATISTICS_update (session->plugin->env->stats,
2601 gettext_noop ("# TCP service resumed"),
2604 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2606 GNUNET_STATISTICS_set (plugin->env->stats,
2607 gettext_noop ("# TCP server connections active"),
2608 plugin->cur_connections,
2610 GNUNET_STATISTICS_update (session->plugin->env->stats,
2611 gettext_noop ("# network-level TCP disconnect events"),
2614 tcp_plugin_disconnect_session (plugin,
2620 * We can now send a probe message, copy into buffer to really send.
2622 * @param cls closure, a `struct TCPProbeContext`
2623 * @param size max size to copy
2624 * @param buf buffer to copy message to
2625 * @return number of bytes copied into @a buf
2628 notify_send_probe (void *cls,
2632 struct TCPProbeContext *tcp_probe_ctx = cls;
2633 struct Plugin *plugin = tcp_probe_ctx->plugin;
2636 tcp_probe_ctx->transmit_handle = NULL;
2637 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2642 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2643 GNUNET_free(tcp_probe_ctx);
2646 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2648 &tcp_probe_ctx->message,
2649 sizeof(tcp_probe_ctx->message));
2650 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2651 tcp_probe_ctx->sock);
2652 ret = sizeof(tcp_probe_ctx->message);
2653 GNUNET_free (tcp_probe_ctx);
2659 * Function called by the NAT subsystem suggesting another peer wants
2660 * to connect to us via connection reversal. Try to connect back to the
2663 * @param cls closure
2664 * @param addr address to try
2665 * @param addrlen number of bytes in @a addr
2668 try_connection_reversal (void *cls,
2669 const struct sockaddr *addr,
2672 struct Plugin *plugin = cls;
2673 struct GNUNET_CONNECTION_Handle *sock;
2674 struct TCPProbeContext *tcp_probe_ctx;
2677 * We have received an ICMP response, ostensibly from a peer
2678 * that wants to connect to us! Send a message to establish a connection.
2680 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2685 /* failed for some odd reason (out of sockets?); ignore attempt */
2689 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2690 tcp_probe_ctx->message.header.size
2691 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2692 tcp_probe_ctx->message.header.type
2693 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2694 tcp_probe_ctx->message.clientIdentity
2695 = *plugin->env->my_identity;
2696 tcp_probe_ctx->plugin = plugin;
2697 tcp_probe_ctx->sock = sock;
2698 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2701 tcp_probe_ctx->transmit_handle
2702 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2703 ntohs (tcp_probe_ctx->message.header.size),
2704 GNUNET_TIME_UNIT_FOREVER_REL,
2711 * Function obtain the network type for a session
2713 * @param cls closure (`struct Plugin *`)
2714 * @param session the session
2715 * @return the network type in HBO or #GNUNET_SYSERR
2717 static enum GNUNET_ATS_Network_Type
2718 tcp_plugin_get_network (void *cls,
2719 struct GNUNET_ATS_Session *session)
2721 return session->scope;
2726 * Function obtain the network type for an address.
2728 * @param cls closure (`struct Plugin *`)
2729 * @param address the address
2730 * @return the network type
2732 static enum GNUNET_ATS_Network_Type
2733 tcp_plugin_get_network_for_address (void *cls,
2734 const struct GNUNET_HELLO_Address *address)
2736 struct Plugin *plugin = cls;
2738 struct sockaddr_in a4;
2739 struct sockaddr_in6 a6;
2740 const struct IPv4TcpAddress *t4;
2741 const struct IPv6TcpAddress *t6;
2745 addrlen = address->address_length;
2746 if (addrlen == sizeof(struct IPv6TcpAddress))
2748 GNUNET_assert (NULL != address->address); /* make static analysis happy */
2749 t6 = address->address;
2750 memset (&a6, 0, sizeof(a6));
2751 #if HAVE_SOCKADDR_IN_SIN_LEN
2752 a6.sin6_len = sizeof (a6);
2754 a6.sin6_family = AF_INET6;
2755 a6.sin6_port = t6->t6_port;
2756 GNUNET_memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
2760 else if (addrlen == sizeof(struct IPv4TcpAddress))
2762 GNUNET_assert (NULL != address->address); /* make static analysis happy */
2763 t4 = address->address;
2764 memset (&a4, 0, sizeof(a4));
2765 #if HAVE_SOCKADDR_IN_SIN_LEN
2766 a4.sin_len = sizeof (a4);
2768 a4.sin_family = AF_INET;
2769 a4.sin_port = t4->t4_port;
2770 a4.sin_addr.s_addr = t4->ipv4_addr;
2777 return GNUNET_ATS_NET_UNSPECIFIED;
2779 return plugin->env->get_address_type (plugin->env->cls,
2786 * Return information about the given session to the
2789 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2790 * @param peer peer we send information about
2791 * @param value our `struct GNUNET_ATS_Session` to send information about
2792 * @return #GNUNET_OK (continue to iterate)
2795 send_session_info_iter (void *cls,
2796 const struct GNUNET_PeerIdentity *peer,
2799 struct Plugin *plugin = cls;
2800 struct GNUNET_ATS_Session *session = value;
2802 notify_session_monitor (plugin,
2804 GNUNET_TRANSPORT_SS_INIT);
2805 /* FIXME: cannot tell if this is up or not from current
2807 notify_session_monitor (plugin,
2809 GNUNET_TRANSPORT_SS_UP);
2815 * Begin monitoring sessions of a plugin. There can only
2816 * be one active monitor per plugin (i.e. if there are
2817 * multiple monitors, the transport service needs to
2818 * multiplex the generated events over all of them).
2820 * @param cls closure of the plugin
2821 * @param sic callback to invoke, NULL to disable monitor;
2822 * plugin will being by iterating over all active
2823 * sessions immediately and then enter monitor mode
2824 * @param sic_cls closure for @a sic
2827 tcp_plugin_setup_monitor (void *cls,
2828 GNUNET_TRANSPORT_SessionInfoCallback sic,
2831 struct Plugin *plugin = cls;
2834 plugin->sic_cls = sic_cls;
2837 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2838 &send_session_info_iter,
2840 /* signal end of first iteration */
2841 sic (sic_cls, NULL, NULL);
2847 * Entry point for the plugin.
2849 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2850 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2853 libgnunet_plugin_transport_tcp_init (void *cls)
2855 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2856 { &handle_tcp_welcome, NULL,
2857 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2858 sizeof(struct WelcomeMessage) },
2859 { &handle_tcp_nat_probe, NULL,
2860 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2861 sizeof(struct TCP_NAT_ProbeMessage) },
2862 { &handle_tcp_data, NULL,
2863 GNUNET_MESSAGE_TYPE_ALL, 0 },
2864 { NULL, NULL, 0, 0 }
2866 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2867 struct GNUNET_TRANSPORT_PluginFunctions *api;
2868 struct Plugin *plugin;
2869 struct GNUNET_SERVICE_Context *service;
2870 unsigned long long aport;
2871 unsigned long long bport;
2872 unsigned long long max_connections;
2874 struct GNUNET_TIME_Relative idle_timeout;
2876 struct GNUNET_NETWORK_Handle *const*lsocks;
2880 struct sockaddr **addrs;
2881 socklen_t *addrlens;
2883 if (NULL == env->receive)
2885 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2886 initialze the plugin or the API */
2887 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2889 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2890 api->address_to_string = &tcp_plugin_address_to_string;
2891 api->string_to_address = &tcp_plugin_string_to_address;
2895 GNUNET_assert (NULL != env->cfg);
2897 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2901 max_connections = 128;
2905 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2909 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2910 "ADVERTISED-PORT", &aport)) &&
2913 LOG(GNUNET_ERROR_TYPE_ERROR,
2914 _("Require valid port number for service `%s' in configuration!\n"),
2924 service = GNUNET_SERVICE_start ("transport-tcp",
2926 GNUNET_SERVICE_OPTION_NONE);
2927 if (NULL == service)
2929 LOG (GNUNET_ERROR_TYPE_WARNING,
2930 _("Failed to start service.\n"));
2938 plugin = GNUNET_new (struct Plugin);
2939 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2941 plugin->max_connections = max_connections;
2942 plugin->open_port = bport;
2943 plugin->adv_port = aport;
2945 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2946 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2947 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2949 if ( (NULL != service) &&
2951 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2956 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2957 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2960 uint32_t len = sizeof (struct WelcomeMessage);
2962 for (i=0;NULL!=lsocks[i];i++)
2965 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2969 sizeof (struct GNUNET_PeerIdentity))) ||
2971 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2973 TCP_STEALTH_INTEGRITY_LEN,
2977 /* TCP STEALTH not supported by kernel */
2978 GNUNET_assert (0 == i);
2979 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2980 _("TCP_STEALTH not supported on this platform.\n"));
2986 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2987 _("TCP_STEALTH not supported on this platform.\n"));
2992 if ( (NULL != service) &&
2995 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
3000 for (ret = ret_s-1; ret >= 0; ret--)
3001 LOG (GNUNET_ERROR_TYPE_INFO,
3002 "Binding to address `%s'\n",
3003 GNUNET_a2s (addrs[ret], addrlens[ret]));
3005 = GNUNET_NAT_register (env->cfg,
3008 (unsigned int) ret_s,
3009 (const struct sockaddr **) addrs,
3011 &tcp_nat_port_map_callback,
3012 &try_connection_reversal,
3015 for (ret = ret_s -1; ret >= 0; ret--)
3016 GNUNET_free (addrs[ret]);
3017 GNUNET_free_non_null (addrs);
3018 GNUNET_free_non_null (addrlens);
3022 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
3029 &try_connection_reversal,
3033 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
3035 api->send = &tcp_plugin_send;
3036 api->get_session = &tcp_plugin_get_session;
3037 api->disconnect_session = &tcp_plugin_disconnect_session;
3038 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
3039 api->disconnect_peer = &tcp_plugin_disconnect;
3040 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3041 api->check_address = &tcp_plugin_check_address;
3042 api->address_to_string = &tcp_plugin_address_to_string;
3043 api->string_to_address = &tcp_plugin_string_to_address;
3044 api->get_network = &tcp_plugin_get_network;
3045 api->get_network_for_address = &tcp_plugin_get_network_for_address;
3046 api->update_session_timeout = &tcp_plugin_update_session_timeout;
3047 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
3048 api->setup_monitor = &tcp_plugin_setup_monitor;
3049 plugin->service = service;
3050 if (NULL != service)
3052 plugin->server = GNUNET_SERVICE_get_server (service);
3057 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3062 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
3068 = GNUNET_SERVER_create_with_sockets (NULL,
3074 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3075 GNUNET_memcpy (plugin->handlers,
3077 sizeof(my_handlers));
3078 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
3079 plugin->handlers[i].callback_cls = plugin;
3081 GNUNET_SERVER_add_handlers (plugin->server,
3083 GNUNET_SERVER_connect_notify (plugin->server,
3086 GNUNET_SERVER_disconnect_notify (plugin->server,
3089 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
3092 LOG (GNUNET_ERROR_TYPE_INFO,
3093 _("TCP transport listening on port %llu\n"),
3096 LOG (GNUNET_ERROR_TYPE_INFO,
3097 _("TCP transport not listening on any port (client only)\n"));
3098 if ( (aport != bport) &&
3100 LOG (GNUNET_ERROR_TYPE_INFO,
3101 _("TCP transport advertises itself as being on port %llu\n"),
3103 /* Initially set connections to 0 */
3104 GNUNET_STATISTICS_set (plugin->env->stats,
3105 gettext_noop ("# TCP sessions active"),
3111 if (NULL != plugin->nat)
3112 GNUNET_NAT_unregister (plugin->nat);
3113 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3114 if (NULL != service)
3115 GNUNET_SERVICE_stop (service);
3116 GNUNET_free (plugin);
3117 GNUNET_free_non_null (api);
3123 * Exit point from the plugin.
3125 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
3129 libgnunet_plugin_transport_tcp_done (void *cls)
3131 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3132 struct Plugin *plugin = api->cls;
3133 struct TCPProbeContext *tcp_probe;
3134 struct PrettyPrinterContext *cur;
3135 struct PrettyPrinterContext *next;
3142 LOG (GNUNET_ERROR_TYPE_DEBUG,
3143 "Shutting down TCP plugin\n");
3145 /* Removing leftover sessions */
3146 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3147 &session_disconnect_it,
3149 /* Removing leftover NAT sessions */
3150 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
3151 &session_disconnect_it,
3154 for (cur = plugin->ppc_dll_head; NULL != cur; cur = next)
3157 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3158 plugin->ppc_dll_tail,
3160 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3161 cur->asc (cur->asc_cls,
3167 if (NULL != plugin->service)
3168 GNUNET_SERVICE_stop (plugin->service);
3170 GNUNET_SERVER_destroy (plugin->server);
3171 GNUNET_free (plugin->handlers);
3172 if (NULL != plugin->nat)
3173 GNUNET_NAT_unregister (plugin->nat);
3174 while (NULL != (tcp_probe = plugin->probe_head))
3176 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3179 GNUNET_CONNECTION_destroy (tcp_probe->sock);
3180 GNUNET_free (tcp_probe);
3182 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3183 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3184 GNUNET_break (0 == plugin->cur_connections);
3185 GNUNET_free (plugin);
3190 /* end of plugin_transport_tcp.c */