2 This file is part of GNUnet
3 Copyright (C) 2002--2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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`
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`
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.
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 * Handle for (DYN)DNS lookup of our external IP.
468 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
471 * Function to call about session status changes.
473 GNUNET_TRANSPORT_SessionInfoCallback sic;
476 * Closure for @e sic.
481 * ID of task used to update our addresses when one expires.
483 struct GNUNET_SCHEDULER_Task *address_update_task;
486 * Running pretty printers: head
488 struct PrettyPrinterContext *ppc_dll_head;
491 * Running pretty printers: tail
493 struct PrettyPrinterContext *ppc_dll_tail;
496 * Welcome message used by this peer.
498 struct WelcomeMessage my_welcome;
501 * How many more TCP sessions are we allowed to open right now?
503 unsigned long long max_connections;
506 * How many more TCP sessions do we have right now?
508 unsigned long long cur_connections;
516 * Port that we are actually listening on.
521 * Port that the user said we would have visible to the
530 * If a session monitor is attached, notify it about the new
533 * @param plugin our plugin
534 * @param session session that changed state
535 * @param state new state of the session
538 notify_session_monitor (struct Plugin *plugin,
539 struct Session *session,
540 enum GNUNET_TRANSPORT_SessionState state)
542 struct GNUNET_TRANSPORT_SessionInfo info;
544 if (NULL == plugin->sic)
546 memset (&info, 0, sizeof (info));
548 info.is_inbound = (0 != (GNUNET_HELLO_ADDRESS_INFO_INBOUND & session->address->local_info))
551 info.num_msg_pending = session->msgs_in_queue;
552 info.num_bytes_pending = session->bytes_in_queue;
553 if (NULL != session->receive_delay_task)
554 info.receive_delay = session->receive_delay;
555 info.session_timeout = session->timeout;
556 info.address = session->address;
557 plugin->sic (plugin->sic_cls,
564 * Our external IP address/port mapping has changed.
566 * @param cls closure, the `struct Plugin`
567 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
568 * the previous (now invalid) one
569 * @param addr either the previous or the new public IP address
570 * @param addrlen actual length of @a addr
573 tcp_nat_port_map_callback (void *cls,
575 const struct sockaddr *addr,
578 struct Plugin *plugin = cls;
579 struct GNUNET_HELLO_Address *address;
580 struct IPv4TcpAddress t4;
581 struct IPv6TcpAddress t6;
585 LOG(GNUNET_ERROR_TYPE_INFO,
586 "NAT notification to %s address `%s'\n",
587 (GNUNET_YES == add_remove) ? "add" : "remove",
588 GNUNET_a2s (addr, addrlen));
589 /* convert 'addr' to our internal format */
590 switch (addr->sa_family)
593 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
594 memset (&t4, 0, sizeof(t4));
595 t4.options = htonl (plugin->myoptions);
596 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
597 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
602 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
603 memset (&t6, 0, sizeof(t6));
604 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
605 sizeof(struct in6_addr));
606 t6.options = htonl (plugin->myoptions);
607 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
615 /* modify our published address list */
616 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
617 (args == sizeof (struct IPv6TcpAddress)));
618 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
619 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
620 plugin->env->notify_address (plugin->env->cls, add_remove, address);
621 GNUNET_HELLO_address_free(address);
626 * Function called for a quick conversion of the binary address to
627 * a numeric address. Note that the caller must not free the
628 * address and that the next call to this function is allowed
629 * to override the address again.
631 * @param cls closure (`struct Plugin*`)
632 * @param addr binary address
633 * @param addrlen length of @a addr
634 * @return string representing the same address
637 tcp_plugin_address_to_string (void *cls,
641 static char rbuf[INET6_ADDRSTRLEN + 12];
642 char buf[INET6_ADDRSTRLEN];
646 const struct IPv4TcpAddress *t4;
647 const struct IPv6TcpAddress *t6;
654 case sizeof(struct IPv6TcpAddress):
657 port = ntohs (t6->t6_port);
658 options = ntohl (t6->options);
659 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
662 case sizeof(struct IPv4TcpAddress):
665 port = ntohs (t4->t4_port);
666 options = ntohl (t4->options);
667 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
671 LOG (GNUNET_ERROR_TYPE_WARNING,
672 _("Unexpected address length: %u bytes\n"),
673 (unsigned int) addrlen);
676 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
678 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
682 GNUNET_snprintf (rbuf, sizeof(rbuf),
683 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
693 * Function called to convert a string address to
696 * @param cls closure (`struct Plugin*`)
697 * @param addr string address
698 * @param addrlen length of the address
699 * @param buf location to store the buffer
700 * @param added location to store the number of bytes in the buffer.
701 * If the function returns #GNUNET_SYSERR, its contents are undefined.
702 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
705 tcp_plugin_string_to_address (void *cls,
711 struct sockaddr_storage socket_address;
717 /* Format tcp.options.address:port */
721 if ((NULL == addr) || (0 == addrlen))
724 return GNUNET_SYSERR;
726 if ('\0' != addr[addrlen - 1])
729 return GNUNET_SYSERR;
731 if (strlen (addr) != addrlen - 1)
734 return GNUNET_SYSERR;
736 plugin = GNUNET_strdup (addr);
737 optionstr = strchr (plugin, '.');
738 if (NULL == optionstr)
742 return GNUNET_SYSERR;
746 options = atol (optionstr);
747 address = strchr (optionstr, '.');
752 return GNUNET_SYSERR;
758 GNUNET_STRINGS_to_address_ip (address,
764 return GNUNET_SYSERR;
768 switch (socket_address.ss_family)
772 struct IPv4TcpAddress *t4;
773 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
774 t4 = GNUNET_new (struct IPv4TcpAddress);
775 t4->options = htonl (options);
776 t4->ipv4_addr = in4->sin_addr.s_addr;
777 t4->t4_port = in4->sin_port;
779 *added = sizeof(struct IPv4TcpAddress);
784 struct IPv6TcpAddress *t6;
785 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
786 t6 = GNUNET_new (struct IPv6TcpAddress);
787 t6->options = htonl (options);
788 t6->ipv6_addr = in6->sin6_addr;
789 t6->t6_port = in6->sin6_port;
791 *added = sizeof(struct IPv6TcpAddress);
795 return GNUNET_SYSERR;
801 * Find the session handle for the given client.
802 * Currently uses both the hashmap and the client
803 * context, as the client context is new and the
804 * logic still needs to be tested.
806 * @param plugin the plugin
807 * @param client which client to find the session handle for
808 * @return NULL if no matching session exists
810 static struct Session *
811 lookup_session_by_client (struct Plugin *plugin,
812 struct GNUNET_SERVER_Client *client)
814 return GNUNET_SERVER_client_get_user_context (client,
820 * Functions with this signature are called whenever we need
821 * to close a session due to a disconnect or failure to
822 * establish a connection.
824 * @param cls the `struct Plugin`
825 * @param session session to close down
826 * @return #GNUNET_OK on success
829 tcp_plugin_disconnect_session (void *cls,
830 struct Session *session)
832 struct Plugin *plugin = cls;
833 struct PendingMessage *pm;
835 LOG (GNUNET_ERROR_TYPE_DEBUG,
836 "Disconnecting session of peer `%s' address `%s'\n",
837 GNUNET_i2s (&session->target),
838 tcp_plugin_address_to_string (session->plugin,
839 session->address->address,
840 session->address->address_length));
842 if (NULL != session->timeout_task)
844 GNUNET_SCHEDULER_cancel (session->timeout_task);
845 session->timeout_task = NULL;
846 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
850 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
854 GNUNET_STATISTICS_update (session->plugin->env->stats,
855 gettext_noop ("# TCP sessions active"),
861 GNUNET_assert (GNUNET_YES ==
862 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
866 if (NULL != session->client)
867 GNUNET_SERVER_client_set_user_context (session->client,
871 if (NULL != session->transmit_handle)
873 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
874 session->transmit_handle = NULL;
876 session->plugin->env->session_end (session->plugin->env->cls,
880 if (NULL != session->nat_connection_timeout)
882 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
883 session->nat_connection_timeout = NULL;
886 while (NULL != (pm = session->pending_messages_head))
888 LOG (GNUNET_ERROR_TYPE_DEBUG,
889 (NULL != pm->transmit_cont)
890 ? "Could not deliver message to `%s' at %s.\n"
891 : "Could not deliver message to `%s' at %s, notifying.\n",
892 GNUNET_i2s (&session->target),
893 tcp_plugin_address_to_string (session->plugin,
894 session->address->address,
895 session->address->address_length));
896 GNUNET_STATISTICS_update (session->plugin->env->stats,
897 gettext_noop ("# bytes currently in TCP buffers"),
898 -(int64_t) pm->message_size, GNUNET_NO);
899 GNUNET_STATISTICS_update (session->plugin->env->stats,
900 gettext_noop ("# bytes discarded by TCP (disconnect)"),
903 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
904 session->pending_messages_tail,
906 GNUNET_assert (0 < session->msgs_in_queue);
907 session->msgs_in_queue--;
908 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
909 session->bytes_in_queue -= pm->message_size;
910 if (NULL != pm->transmit_cont)
911 pm->transmit_cont (pm->transmit_cont_cls,
918 GNUNET_assert (0 == session->msgs_in_queue);
919 GNUNET_assert (0 == session->bytes_in_queue);
920 notify_session_monitor (session->plugin,
922 GNUNET_TRANSPORT_SS_DONE);
924 if (NULL != session->receive_delay_task)
926 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
927 session->receive_delay_task = NULL;
929 if (NULL != session->client)
931 GNUNET_SERVER_client_disconnect (session->client);
932 session->client = NULL;
934 GNUNET_HELLO_address_free (session->address);
935 GNUNET_assert (NULL == session->transmit_handle);
936 GNUNET_free (session);
942 * Function that is called to get the keepalive factor.
943 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
944 * calculate the interval between keepalive packets.
946 * @param cls closure with the `struct Plugin`
947 * @return keepalive factor
950 tcp_plugin_query_keepalive_factor (void *cls)
957 * Session was idle for too long, so disconnect it
959 * @param cls the `struct Session` of the idle session
960 * @param tc scheduler context
963 session_timeout (void *cls,
964 const struct GNUNET_SCHEDULER_TaskContext *tc)
966 struct Session *s = cls;
967 struct GNUNET_TIME_Relative left;
969 s->timeout_task = NULL;
970 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
971 if (0 != left.rel_value_us)
973 /* not actually our turn yet, but let's at least update
974 the monitor, it may think we're about to die ... */
975 notify_session_monitor (s->plugin,
977 GNUNET_TRANSPORT_SS_UPDATE);
978 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
983 LOG (GNUNET_ERROR_TYPE_DEBUG,
984 "Session %p was idle for %s, disconnecting\n",
986 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
988 /* call session destroy function */
989 tcp_plugin_disconnect_session (s->plugin,
995 * Increment session timeout due to activity.
997 * @param s session to increment timeout for
1000 reschedule_session_timeout (struct Session *s)
1002 GNUNET_assert (NULL != s->timeout_task);
1003 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1008 * Create a new session. Also queues a welcome message.
1010 * @param plugin the plugin
1011 * @param address the address to create the session for
1012 * @param scope network scope the address is from
1013 * @param client client to use, reference counter must have already been increased
1014 * @param is_nat this a NAT session, we should wait for a client to
1015 * connect to us from an address, then assign that to
1017 * @return new session object
1019 static struct Session *
1020 create_session (struct Plugin *plugin,
1021 const struct GNUNET_HELLO_Address *address,
1022 enum GNUNET_ATS_Network_Type scope,
1023 struct GNUNET_SERVER_Client *client,
1026 struct Session *session;
1027 struct PendingMessage *pm;
1029 if (GNUNET_YES != is_nat)
1030 GNUNET_assert (NULL != client);
1032 GNUNET_assert (NULL == client);
1034 LOG (GNUNET_ERROR_TYPE_DEBUG,
1035 "Creating new session for peer `%4s' at address %s\n",
1036 GNUNET_i2s (&address->peer),
1037 tcp_plugin_address_to_string (plugin,
1039 address->address_length));
1040 session = GNUNET_new (struct Session);
1041 session->last_activity = GNUNET_TIME_absolute_get ();
1042 session->plugin = plugin;
1043 session->is_nat = is_nat;
1046 session->client = client;
1047 GNUNET_SERVER_client_set_user_context (client,
1050 session->address = GNUNET_HELLO_address_copy (address);
1051 session->target = address->peer;
1052 session->expecting_welcome = GNUNET_YES;
1053 session->scope = scope;
1054 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1055 sizeof (struct WelcomeMessage));
1056 pm->msg = (const char *) &pm[1];
1057 pm->message_size = sizeof(struct WelcomeMessage);
1059 &plugin->my_welcome,
1060 sizeof(struct WelcomeMessage));
1061 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1062 GNUNET_STATISTICS_update (plugin->env->stats,
1063 gettext_noop ("# bytes currently in TCP buffers"),
1066 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1067 session->pending_messages_tail,
1069 session->msgs_in_queue++;
1070 session->bytes_in_queue += pm->message_size;
1071 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1072 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1075 notify_session_monitor (session->plugin,
1077 GNUNET_TRANSPORT_SS_INIT);
1078 if (GNUNET_YES != is_nat)
1080 GNUNET_STATISTICS_update (plugin->env->stats,
1081 gettext_noop ("# TCP sessions active"),
1084 notify_session_monitor (session->plugin,
1086 GNUNET_TRANSPORT_SS_UP);
1090 notify_session_monitor (session->plugin,
1092 GNUNET_TRANSPORT_SS_HANDSHAKE);
1099 * If we have pending messages, ask the server to
1100 * transmit them (schedule the respective tasks, etc.)
1102 * @param session for which session should we do this
1105 process_pending_messages (struct Session *session);
1109 * Function called to notify a client about the socket
1110 * being ready to queue more data. "buf" will be
1111 * NULL and "size" zero if the socket was closed for
1112 * writing in the meantime.
1114 * @param cls closure
1115 * @param size number of bytes available in @a buf
1116 * @param buf where the callee should write the message
1117 * @return number of bytes written to @a buf
1120 do_transmit (void *cls,
1124 struct Session *session = cls;
1125 struct GNUNET_PeerIdentity pid;
1126 struct Plugin *plugin;
1127 struct PendingMessage *pos;
1128 struct PendingMessage *hd;
1129 struct PendingMessage *tl;
1130 struct GNUNET_TIME_Absolute now;
1134 session->transmit_handle = NULL;
1135 plugin = session->plugin;
1138 LOG (GNUNET_ERROR_TYPE_DEBUG,
1139 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1140 GNUNET_i2s (&session->target));
1141 /* timeout; cancel all messages that have already expired */
1145 now = GNUNET_TIME_absolute_get ();
1146 while ( (NULL != (pos = session->pending_messages_head)) &&
1147 (pos->timeout.abs_value_us <= now.abs_value_us) )
1149 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1150 session->pending_messages_tail,
1152 GNUNET_assert (0 < session->msgs_in_queue);
1153 session->msgs_in_queue--;
1154 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1155 session->bytes_in_queue -= pos->message_size;
1156 LOG (GNUNET_ERROR_TYPE_DEBUG,
1157 "Failed to transmit %u byte message to `%4s'.\n",
1159 GNUNET_i2s (&session->target));
1160 ret += pos->message_size;
1161 GNUNET_CONTAINER_DLL_insert_after (hd,
1166 /* do this call before callbacks (so that if callbacks destroy
1167 * session, they have a chance to cancel actions done by this
1169 process_pending_messages (session);
1170 pid = session->target;
1171 /* no do callbacks and do not use session again since
1172 * the callbacks may abort the session */
1173 while (NULL != (pos = hd))
1175 GNUNET_CONTAINER_DLL_remove (hd,
1178 if (NULL != pos->transmit_cont)
1179 pos->transmit_cont (pos->transmit_cont_cls,
1186 GNUNET_STATISTICS_update (plugin->env->stats,
1187 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1189 GNUNET_STATISTICS_update (plugin->env->stats,
1190 gettext_noop ("# bytes discarded by TCP (timeout)"),
1194 notify_session_monitor (session->plugin,
1196 GNUNET_TRANSPORT_SS_UPDATE);
1199 /* copy all pending messages that would fit */
1204 while (NULL != (pos = session->pending_messages_head))
1206 if (ret + pos->message_size > size)
1208 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1209 session->pending_messages_tail,
1211 GNUNET_assert (0 < session->msgs_in_queue);
1212 session->msgs_in_queue--;
1213 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1214 session->bytes_in_queue -= pos->message_size;
1215 GNUNET_assert(size >= pos->message_size);
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Transmitting message of type %u size %u to peer %s at %s\n",
1218 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1220 GNUNET_i2s (&session->target),
1221 tcp_plugin_address_to_string (session->plugin,
1222 session->address->address,
1223 session->address->address_length));
1224 /* FIXME: this memcpy can be up to 7% of our total runtime */
1228 cbuf += pos->message_size;
1229 ret += pos->message_size;
1230 size -= pos->message_size;
1231 GNUNET_CONTAINER_DLL_insert_tail (hd,
1235 notify_session_monitor (session->plugin,
1237 GNUNET_TRANSPORT_SS_UPDATE);
1238 /* schedule 'continuation' before callbacks so that callbacks that
1239 * cancel everything don't cause us to use a session that no longer
1241 process_pending_messages (session);
1242 session->last_activity = GNUNET_TIME_absolute_get ();
1243 pid = session->target;
1244 /* we'll now call callbacks that may cancel the session; hence
1245 * we should not use 'session' after this point */
1246 while (NULL != (pos = hd))
1248 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1249 if (NULL != pos->transmit_cont)
1250 pos->transmit_cont (pos->transmit_cont_cls,
1254 pos->message_size); /* FIXME: include TCP overhead */
1257 GNUNET_assert (NULL == hd);
1258 GNUNET_assert (NULL == tl);
1259 GNUNET_STATISTICS_update (plugin->env->stats,
1260 gettext_noop ("# bytes currently in TCP buffers"),
1263 GNUNET_STATISTICS_update (plugin->env->stats,
1264 gettext_noop ("# bytes transmitted via TCP"),
1272 * If we have pending messages, ask the server to
1273 * transmit them (schedule the respective tasks, etc.)
1275 * @param session for which session should we do this
1278 process_pending_messages (struct Session *session)
1280 struct PendingMessage *pm;
1282 GNUNET_assert (NULL != session->client);
1283 if (NULL != session->transmit_handle)
1285 if (NULL == (pm = session->pending_messages_head))
1288 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1290 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1297 * Function that can be used by the transport service to transmit
1298 * a message using the plugin. Note that in the case of a
1299 * peer disconnecting, the continuation MUST be called
1300 * prior to the disconnect notification itself. This function
1301 * will be called with this peer's HELLO message to initiate
1302 * a fresh connection to another peer.
1304 * @param cls closure
1305 * @param session which session must be used
1306 * @param msgbuf the message to transmit
1307 * @param msgbuf_size number of bytes in @a msgbuf
1308 * @param priority how important is the message (most plugins will
1309 * ignore message priority and just FIFO)
1310 * @param to how long to wait at most for the transmission (does not
1311 * require plugins to discard the message after the timeout,
1312 * just advisory for the desired delay; most plugins will ignore
1314 * @param cont continuation to call once the message has
1315 * been transmitted (or if the transport is ready
1316 * for the next transmission call; or if the
1317 * peer disconnected...); can be NULL
1318 * @param cont_cls closure for @a cont
1319 * @return number of bytes used (on the physical network, with overheads);
1320 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1321 * and does NOT mean that the message was not transmitted (DV)
1324 tcp_plugin_send (void *cls,
1325 struct Session *session,
1328 unsigned int priority,
1329 struct GNUNET_TIME_Relative to,
1330 GNUNET_TRANSPORT_TransmitContinuation cont,
1333 struct Plugin * plugin = cls;
1334 struct PendingMessage *pm;
1336 /* create new message entry */
1337 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1338 pm->msg = (const char *) &pm[1];
1339 memcpy (&pm[1], msgbuf, msgbuf_size);
1340 pm->message_size = msgbuf_size;
1341 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1342 pm->transmit_cont = cont;
1343 pm->transmit_cont_cls = cont_cls;
1345 LOG(GNUNET_ERROR_TYPE_DEBUG,
1346 "Asked to transmit %u bytes to `%s', added message to list.\n",
1348 GNUNET_i2s (&session->target));
1351 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1355 GNUNET_assert (NULL != session->client);
1356 GNUNET_SERVER_client_set_timeout (session->client,
1357 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1358 GNUNET_STATISTICS_update (plugin->env->stats,
1359 gettext_noop ("# bytes currently in TCP buffers"),
1363 /* append pm to pending_messages list */
1364 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1365 session->pending_messages_tail,
1367 notify_session_monitor (session->plugin,
1369 GNUNET_TRANSPORT_SS_UPDATE);
1370 session->msgs_in_queue++;
1371 session->bytes_in_queue += pm->message_size;
1372 process_pending_messages (session);
1376 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1380 LOG (GNUNET_ERROR_TYPE_DEBUG,
1381 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1382 GNUNET_i2s (&session->target));
1383 GNUNET_STATISTICS_update (plugin->env->stats,
1384 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1386 /* append pm to pending_messages list */
1387 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1388 session->pending_messages_tail,
1390 session->msgs_in_queue++;
1391 session->bytes_in_queue += pm->message_size;
1392 notify_session_monitor (session->plugin,
1394 GNUNET_TRANSPORT_SS_HANDSHAKE);
1397 LOG (GNUNET_ERROR_TYPE_ERROR,
1398 "Invalid session %p\n",
1408 return GNUNET_SYSERR; /* session does not exist here */
1413 * Closure for #session_lookup_it().
1418 * Address we are looking for.
1420 const struct GNUNET_HELLO_Address *address;
1423 * Where to store the session (if we found it).
1425 struct Session *result;
1431 * Look for a session by address.
1433 * @param cls the `struct SessionItCtx`
1435 * @param value a `struct Session`
1436 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1439 session_lookup_it (void *cls,
1440 const struct GNUNET_PeerIdentity *key,
1443 struct SessionItCtx *si_ctx = cls;
1444 struct Session *session = value;
1447 GNUNET_HELLO_address_cmp (si_ctx->address,
1450 si_ctx->result = session;
1456 * Task cleaning up a NAT connection attempt after timeout
1458 * @param cls the `struct Session`
1459 * @param tc scheduler context (unused)
1462 nat_connect_timeout (void *cls,
1463 const struct GNUNET_SCHEDULER_TaskContext *tc)
1465 struct Session *session = cls;
1467 session->nat_connection_timeout = NULL;
1468 LOG (GNUNET_ERROR_TYPE_DEBUG,
1469 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1470 GNUNET_i2s (&session->target),
1471 tcp_plugin_address_to_string (session->plugin,
1472 session->address->address,
1473 session->address->address_length));
1474 tcp_plugin_disconnect_session (session->plugin,
1480 * Function that will be called whenever the transport service wants to
1481 * notify the plugin that a session is still active and in use and
1482 * therefore the session timeout for this session has to be updated
1484 * @param cls closure
1485 * @param peer which peer was the session for
1486 * @param session which session is being updated
1489 tcp_plugin_update_session_timeout (void *cls,
1490 const struct GNUNET_PeerIdentity *peer,
1491 struct Session *session)
1493 reschedule_session_timeout (session);
1498 * Task to signal the server that we can continue
1499 * receiving from the TCP client now.
1501 * @param cls the `struct Session *`
1502 * @param tc task context (unused)
1505 delayed_done (void *cls,
1506 const struct GNUNET_SCHEDULER_TaskContext *tc)
1508 struct Session *session = cls;
1510 session->receive_delay_task = NULL;
1511 reschedule_session_timeout (session);
1512 GNUNET_SERVER_receive_done (session->client,
1518 * Function that will be called whenever the transport service wants to
1519 * notify the plugin that the inbound quota changed and that the plugin
1520 * should update it's delay for the next receive value
1522 * @param cls closure
1523 * @param peer which peer was the session for
1524 * @param session which session is being updated
1525 * @param delay new delay to use for receiving
1528 tcp_plugin_update_inbound_delay (void *cls,
1529 const struct GNUNET_PeerIdentity *peer,
1530 struct Session *session,
1531 struct GNUNET_TIME_Relative delay)
1533 if (NULL == session->receive_delay_task)
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1536 "New inbound delay %s\n",
1537 GNUNET_STRINGS_relative_time_to_string (delay,
1539 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1540 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1541 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1548 * Create a new session to transmit data to the target
1549 * This session will used to send data to this peer and the plugin will
1550 * notify us by calling the env->session_end function
1552 * @param cls closure
1553 * @param address the address to use
1554 * @return the session if the address is valid, NULL otherwise
1556 static struct Session *
1557 tcp_plugin_get_session (void *cls,
1558 const struct GNUNET_HELLO_Address *address)
1560 struct Plugin *plugin = cls;
1561 struct Session *session = NULL;
1565 struct GNUNET_CONNECTION_Handle *sa;
1566 struct sockaddr_in a4;
1567 struct sockaddr_in6 a6;
1568 const struct IPv4TcpAddress *t4;
1569 const struct IPv6TcpAddress *t6;
1570 unsigned int options;
1571 enum GNUNET_ATS_Network_Type net_type;
1572 unsigned int is_natd = GNUNET_NO;
1575 struct GNUNET_NETWORK_Handle *s;
1578 addrlen = address->address_length;
1579 LOG (GNUNET_ERROR_TYPE_DEBUG,
1580 "Trying to get session for `%s' address of peer `%s'\n",
1581 tcp_plugin_address_to_string (plugin,
1583 address->address_length),
1584 GNUNET_i2s (&address->peer));
1586 if (GNUNET_HELLO_address_check_option (address,
1587 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1593 /* look for existing session */
1595 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1598 struct SessionItCtx si_ctx;
1600 si_ctx.address = address;
1601 si_ctx.result = NULL;
1602 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1606 if (NULL != si_ctx.result)
1608 session = si_ctx.result;
1609 LOG (GNUNET_ERROR_TYPE_DEBUG,
1610 "Found existing session for `%s' address `%s'\n",
1611 GNUNET_i2s (&address->peer),
1612 tcp_plugin_address_to_string (plugin,
1614 address->address_length));
1619 if (addrlen == sizeof(struct IPv6TcpAddress))
1621 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1622 t6 = address->address;
1623 options = t6->options;
1625 memset (&a6, 0, sizeof(a6));
1626 #if HAVE_SOCKADDR_IN_SIN_LEN
1627 a6.sin6_len = sizeof (a6);
1629 a6.sin6_family = AF_INET6;
1630 a6.sin6_port = t6->t6_port;
1631 if (t6->t6_port == 0)
1632 is_natd = GNUNET_YES;
1633 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1637 else if (addrlen == sizeof(struct IPv4TcpAddress))
1639 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1640 t4 = address->address;
1641 options = t4->options;
1643 memset (&a4, 0, sizeof(a4));
1644 #if HAVE_SOCKADDR_IN_SIN_LEN
1645 a4.sin_len = sizeof (a4);
1647 a4.sin_family = AF_INET;
1648 a4.sin_port = t4->t4_port;
1649 if (t4->t4_port == 0)
1650 is_natd = GNUNET_YES;
1651 a4.sin_addr.s_addr = t4->ipv4_addr;
1657 GNUNET_STATISTICS_update (plugin->env->stats,
1658 gettext_noop ("# requests to create session with invalid address"),
1664 net_type = plugin->env->get_address_type (plugin->env->cls,
1667 GNUNET_break (net_type != GNUNET_ATS_NET_UNSPECIFIED);
1669 if ( (is_natd == GNUNET_YES) &&
1670 (addrlen == sizeof(struct IPv6TcpAddress)) )
1672 /* NAT client only works with IPv4 addresses */
1676 if (plugin->cur_connections >= plugin->max_connections)
1682 if ( (is_natd == GNUNET_YES) &&
1684 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1687 /* Only do one NAT punch attempt per peer identity */
1691 if ( (is_natd == GNUNET_YES) &&
1692 (NULL != plugin->nat) &&
1694 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1697 LOG (GNUNET_ERROR_TYPE_DEBUG,
1698 "Found valid IPv4 NAT address (creating session)!\n");
1699 session = create_session (plugin,
1704 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1705 &nat_connect_timeout,
1707 GNUNET_assert (GNUNET_OK ==
1708 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1711 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1713 LOG (GNUNET_ERROR_TYPE_DEBUG,
1714 "Created NAT WAIT connection to `%4s' at `%s'\n",
1715 GNUNET_i2s (&session->target),
1716 GNUNET_a2s (sb, sbs));
1717 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1723 LOG(GNUNET_ERROR_TYPE_DEBUG,
1724 "Running NAT client for `%4s' at `%s' failed\n",
1725 GNUNET_i2s (&session->target),
1726 GNUNET_a2s (sb, sbs));
1727 tcp_plugin_disconnect_session (plugin,
1733 /* create new outbound session */
1734 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1737 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1740 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1747 GNUNET_NETWORK_socket_setsockopt (s,
1751 sizeof (struct GNUNET_PeerIdentity))) ||
1753 GNUNET_NETWORK_socket_setsockopt (s,
1755 TCP_STEALTH_INTEGRITY,
1756 &plugin->my_welcome,
1757 sizeof (struct WelcomeMessage))) )
1759 /* TCP STEALTH not supported by kernel */
1760 GNUNET_break (GNUNET_OK ==
1761 GNUNET_NETWORK_socket_close (s));
1766 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1775 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1779 LOG (GNUNET_ERROR_TYPE_DEBUG,
1780 "Failed to create connection to `%4s' at `%s'\n",
1781 GNUNET_i2s (&address->peer),
1782 GNUNET_a2s (sb, sbs));
1785 LOG (GNUNET_ERROR_TYPE_DEBUG,
1786 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1787 GNUNET_i2s (&address->peer),
1788 GNUNET_a2s (sb, sbs));
1790 session = create_session (plugin,
1793 GNUNET_SERVER_connect_socket (plugin->server,
1796 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1799 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1800 /* Send TCP Welcome */
1801 process_pending_messages (session);
1808 * We have been asked to destroy all connections to a particular peer.
1809 * This function is called on each applicable session and must tear it
1812 * @param cls the `struct Plugin *`
1813 * @param key the peer which the session belongs to (unused)
1814 * @param value the `struct Session`
1815 * @return #GNUNET_YES (continue to iterate)
1818 session_disconnect_it (void *cls,
1819 const struct GNUNET_PeerIdentity *key,
1822 struct Plugin *plugin = cls;
1823 struct Session *session = value;
1825 GNUNET_STATISTICS_update (session->plugin->env->stats,
1826 gettext_noop ("# transport-service disconnect requests for TCP"),
1829 tcp_plugin_disconnect_session (plugin,
1836 * Function that can be called to force a disconnect from the
1837 * specified neighbour. This should also cancel all previously
1838 * scheduled transmissions. Obviously the transmission may have been
1839 * partially completed already, which is OK. The plugin is supposed
1840 * to close the connection (if applicable) and no longer call the
1841 * transmit continuation(s).
1843 * Finally, plugin MUST NOT call the services's receive function to
1844 * notify the service that the connection to the specified target was
1845 * closed after a getting this call.
1847 * @param cls closure
1848 * @param target peer for which the last transmission is
1852 tcp_plugin_disconnect (void *cls,
1853 const struct GNUNET_PeerIdentity *target)
1855 struct Plugin *plugin = cls;
1857 LOG (GNUNET_ERROR_TYPE_DEBUG,
1858 "Disconnecting peer `%4s'\n",
1859 GNUNET_i2s (target));
1860 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1862 &session_disconnect_it,
1864 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1866 &session_disconnect_it,
1872 * We are processing an address pretty printing request and finished
1873 * the IP resolution (if applicable). Append our port and forward the
1874 * result. If called with @a hostname NULL, we are done and should
1875 * clean up the pretty printer (otherwise, there might be multiple
1876 * hostnames for the IP address and we might receive more).
1878 * @param cls the `struct PrettyPrinterContext *`
1879 * @param hostname hostname part of the address
1882 append_port (void *cls,
1883 const char *hostname)
1885 struct PrettyPrinterContext *ppc = cls;
1886 struct Plugin *plugin = ppc->plugin;
1889 if (NULL == hostname)
1891 /* Final call, done */
1892 ppc->resolver_handle = NULL;
1893 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
1894 plugin->ppc_dll_tail,
1896 ppc->asc (ppc->asc_cls,
1902 if (GNUNET_YES == ppc->ipv6)
1903 GNUNET_asprintf (&ret,
1910 GNUNET_asprintf (&ret,
1916 ppc->asc (ppc->asc_cls,
1924 * Convert the transports address to a nice, human-readable format.
1926 * @param cls closure with the `struct Plugin`
1927 * @param type name of the transport that generated the address
1928 * @param addr one of the addresses of the host, NULL for the last address
1929 * the specific address format depends on the transport
1930 * @param addrlen length of the @a addr
1931 * @param numeric should (IP) addresses be displayed in numeric form?
1932 * @param timeout after how long should we give up?
1933 * @param asc function to call on each string
1934 * @param asc_cls closure for @a asc
1937 tcp_plugin_address_pretty_printer (void *cls,
1942 struct GNUNET_TIME_Relative timeout,
1943 GNUNET_TRANSPORT_AddressStringCallback asc,
1946 struct Plugin *plugin = cls;
1947 struct PrettyPrinterContext *ppc;
1950 struct sockaddr_in a4;
1951 struct sockaddr_in6 a6;
1952 const struct IPv4TcpAddress *t4;
1953 const struct IPv6TcpAddress *t6;
1957 if (sizeof(struct IPv6TcpAddress) == addrlen)
1960 memset (&a6, 0, sizeof(a6));
1961 a6.sin6_family = AF_INET6;
1962 a6.sin6_port = t6->t6_port;
1963 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1964 port = ntohs (t6->t6_port);
1965 options = ntohl (t6->options);
1969 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1972 memset (&a4, 0, sizeof(a4));
1973 a4.sin_family = AF_INET;
1974 a4.sin_port = t4->t4_port;
1975 a4.sin_addr.s_addr = t4->ipv4_addr;
1976 port = ntohs (t4->t4_port);
1977 options = ntohl (t4->options);
1983 /* invalid address */
1984 asc (asc_cls, NULL, GNUNET_SYSERR);
1985 asc (asc_cls, NULL, GNUNET_OK);
1988 ppc = GNUNET_new (struct PrettyPrinterContext);
1989 ppc->plugin = plugin;
1990 if (addrlen == sizeof(struct IPv6TcpAddress))
1991 ppc->ipv6 = GNUNET_YES;
1993 ppc->ipv6 = GNUNET_NO;
1995 ppc->asc_cls = asc_cls;
1997 ppc->options = options;
1998 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2003 if (NULL == ppc->resolver_handle)
2009 GNUNET_CONTAINER_DLL_insert (plugin->ppc_dll_head,
2010 plugin->ppc_dll_tail,
2016 * Check if the given port is plausible (must be either our listen
2017 * port or our advertised port), or any port if we are behind NAT
2018 * and do not have a port open. If it is neither, we return
2021 * @param plugin global variables
2022 * @param in_port port number to check
2023 * @return #GNUNET_OK if port is either open_port or adv_port
2026 check_port (struct Plugin *plugin,
2029 if ( (in_port == plugin->adv_port) ||
2030 (in_port == plugin->open_port) )
2032 return GNUNET_SYSERR;
2037 * Function that will be called to check if a binary address for this
2038 * plugin is well-formed and corresponds to an address for THIS peer
2039 * (as per our configuration). Naturally, if absolutely necessary,
2040 * plugins can be a bit conservative in their answer, but in general
2041 * plugins should make sure that the address does not redirect
2042 * traffic to a 3rd party that might try to man-in-the-middle our
2045 * @param cls closure, our `struct Plugin *`
2046 * @param addr pointer to the address
2047 * @param addrlen length of @a addr
2048 * @return #GNUNET_OK if this is a plausible address for this peer
2049 * and transport, #GNUNET_SYSERR if not
2052 tcp_plugin_check_address (void *cls,
2056 struct Plugin *plugin = cls;
2057 const struct IPv4TcpAddress *v4;
2058 const struct IPv6TcpAddress *v6;
2060 if ( (addrlen != sizeof(struct IPv4TcpAddress)) &&
2061 (addrlen != sizeof(struct IPv6TcpAddress)) )
2063 GNUNET_break_op (0);
2064 return GNUNET_SYSERR;
2067 if (addrlen == sizeof(struct IPv4TcpAddress))
2069 v4 = (const struct IPv4TcpAddress *) addr;
2070 if (0 != memcmp (&v4->options,
2075 return GNUNET_SYSERR;
2077 if (GNUNET_OK != check_port (plugin,
2078 ntohs (v4->t4_port)))
2079 return GNUNET_SYSERR;
2081 GNUNET_NAT_test_address (plugin->nat,
2083 sizeof (struct in_addr)))
2084 return GNUNET_SYSERR;
2088 v6 = (const struct IPv6TcpAddress *) addr;
2089 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2091 GNUNET_break_op (0);
2092 return GNUNET_SYSERR;
2094 if (0 != memcmp (&v6->options,
2099 return GNUNET_SYSERR;
2101 if (GNUNET_OK != check_port (plugin,
2102 ntohs (v6->t6_port)))
2103 return GNUNET_SYSERR;
2105 GNUNET_NAT_test_address (plugin->nat,
2107 sizeof(struct in6_addr)))
2108 return GNUNET_SYSERR;
2115 * We've received a nat probe from this peer via TCP. Finish
2116 * creating the client session and resume sending of queued
2119 * @param cls closure
2120 * @param client identification of the client
2121 * @param message the actual message
2124 handle_tcp_nat_probe (void *cls,
2125 struct GNUNET_SERVER_Client *client,
2126 const struct GNUNET_MessageHeader *message)
2128 struct Plugin *plugin = cls;
2129 struct Session *session;
2130 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2133 struct IPv4TcpAddress *t4;
2134 struct IPv6TcpAddress *t6;
2135 const struct sockaddr_in *s4;
2136 const struct sockaddr_in6 *s6;
2138 LOG (GNUNET_ERROR_TYPE_DEBUG,
2139 "Received NAT probe\n");
2140 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2141 * a connection to this peer by running gnunet-nat-client. This peer
2142 * received the punch message and now wants us to use the new connection
2143 * as the default for that peer. Do so and then send a WELCOME message
2144 * so we can really be connected!
2146 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2153 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2154 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2155 sizeof(struct GNUNET_PeerIdentity)))
2157 /* refuse connections from ourselves */
2158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
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, GNUNET_OK);
2171 LOG (GNUNET_ERROR_TYPE_DEBUG,
2172 "Found session for NAT probe!\n");
2174 if (NULL != session->nat_connection_timeout)
2176 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2177 session->nat_connection_timeout = NULL;
2181 GNUNET_SERVER_client_get_address (client,
2186 GNUNET_SERVER_receive_done (client,
2188 tcp_plugin_disconnect_session (plugin,
2192 GNUNET_assert (GNUNET_YES ==
2193 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
2194 &tcp_nat_probe->clientIdentity,
2196 GNUNET_SERVER_client_set_user_context (client,
2198 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2201 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2202 session->last_activity = GNUNET_TIME_absolute_get ();
2203 LOG (GNUNET_ERROR_TYPE_DEBUG,
2204 "Found address `%s' for incoming connection\n",
2205 GNUNET_a2s (vaddr, alen));
2206 switch (((const struct sockaddr *) vaddr)->sa_family)
2210 t4 = GNUNET_new (struct IPv4TcpAddress);
2211 t4->options = htonl (TCP_OPTIONS_NONE);
2212 t4->t4_port = s4->sin_port;
2213 t4->ipv4_addr = s4->sin_addr.s_addr;
2214 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2217 sizeof(struct IPv4TcpAddress),
2218 GNUNET_HELLO_ADDRESS_INFO_NONE);
2222 t6 = GNUNET_new (struct IPv6TcpAddress);
2223 t6->options = htonl (TCP_OPTIONS_NONE);
2224 t6->t6_port = s6->sin6_port;
2225 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2226 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2229 sizeof(struct IPv6TcpAddress),
2230 GNUNET_HELLO_ADDRESS_INFO_NONE);
2234 LOG(GNUNET_ERROR_TYPE_DEBUG,
2235 "Bad address for incoming connection!\n");
2237 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2238 tcp_plugin_disconnect_session (plugin,
2242 GNUNET_free (vaddr);
2243 GNUNET_break (NULL == session->client);
2244 session->client = client;
2245 GNUNET_STATISTICS_update (plugin->env->stats,
2246 gettext_noop ("# TCP sessions active"),
2249 process_pending_messages (session);
2250 GNUNET_SERVER_receive_done (client,
2256 * We've received a welcome from this peer via TCP. Possibly create a
2257 * fresh client record and send back our welcome.
2259 * @param cls closure
2260 * @param client identification of the client
2261 * @param message the actual message
2264 handle_tcp_welcome (void *cls,
2265 struct GNUNET_SERVER_Client *client,
2266 const struct GNUNET_MessageHeader *message)
2268 struct Plugin *plugin = cls;
2269 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2270 struct GNUNET_HELLO_Address *address;
2271 struct Session *session;
2274 struct IPv4TcpAddress t4;
2275 struct IPv6TcpAddress t6;
2276 const struct sockaddr_in *s4;
2277 const struct sockaddr_in6 *s6;
2279 if (0 == memcmp (&wm->clientIdentity,
2280 plugin->env->my_identity,
2281 sizeof(struct GNUNET_PeerIdentity)))
2283 /* refuse connections from ourselves */
2284 GNUNET_SERVER_receive_done (client,
2287 GNUNET_SERVER_client_get_address (client,
2291 LOG (GNUNET_ERROR_TYPE_INFO,
2292 "Received WELCOME message from my own identity `%4s' on address `%s'\n",
2293 GNUNET_i2s (&wm->clientIdentity),
2294 GNUNET_a2s (vaddr, alen));
2300 LOG(GNUNET_ERROR_TYPE_DEBUG,
2301 "Received WELCOME message from `%4s' %p\n",
2302 GNUNET_i2s (&wm->clientIdentity),
2304 GNUNET_STATISTICS_update (plugin->env->stats,
2305 gettext_noop ("# TCP WELCOME messages received"),
2308 session = lookup_session_by_client (plugin, client);
2309 if (NULL != session)
2312 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2314 LOG (GNUNET_ERROR_TYPE_DEBUG,
2315 "Found existing session %p for peer `%s'\n",
2317 GNUNET_a2s (vaddr, alen));
2318 GNUNET_free (vaddr);
2324 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2326 if (alen == sizeof(struct sockaddr_in))
2329 memset (&t4, '\0', sizeof (t4));
2330 t4.options = htonl (TCP_OPTIONS_NONE);
2331 t4.t4_port = s4->sin_port;
2332 t4.ipv4_addr = s4->sin_addr.s_addr;
2333 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2337 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2339 else if (alen == sizeof(struct sockaddr_in6))
2342 memset (&t6, '\0', sizeof (t6));
2343 t6.options = htonl (TCP_OPTIONS_NONE);
2344 t6.t6_port = s6->sin6_port;
2345 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2346 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2350 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2355 GNUNET_free_non_null (vaddr);
2358 session = create_session (plugin,
2360 plugin->env->get_address_type (plugin->env->cls,
2365 GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != session->scope);
2366 GNUNET_HELLO_address_free (address);
2367 LOG (GNUNET_ERROR_TYPE_DEBUG,
2368 "Creating new%s session %p for peer `%s' client %p\n",
2369 GNUNET_HELLO_address_check_option (session->address,
2370 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2373 tcp_plugin_address_to_string (plugin,
2374 session->address->address,
2375 session->address->address_length),
2377 GNUNET_free (vaddr);
2378 (void) GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2382 /* Notify transport and ATS about new session */
2383 plugin->env->session_start (plugin->env->cls,
2387 notify_session_monitor (plugin,
2389 GNUNET_TRANSPORT_SS_INIT);
2393 LOG(GNUNET_ERROR_TYPE_DEBUG,
2394 "Did not obtain TCP socket address for incoming connection\n");
2400 if (session->expecting_welcome != GNUNET_YES)
2403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2407 session->last_activity = GNUNET_TIME_absolute_get ();
2408 session->expecting_welcome = GNUNET_NO;
2410 process_pending_messages (session);
2411 GNUNET_SERVER_client_set_timeout (client,
2412 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2418 * We've received data for this peer via TCP. Unbox,
2419 * compute latency and forward.
2421 * @param cls closure
2422 * @param client identification of the client
2423 * @param message the actual message
2426 handle_tcp_data (void *cls,
2427 struct GNUNET_SERVER_Client *client,
2428 const struct GNUNET_MessageHeader *message)
2430 struct Plugin *plugin = cls;
2431 struct Session *session;
2432 struct GNUNET_TIME_Relative delay;
2435 type = ntohs (message->type);
2436 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2437 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2439 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2440 GNUNET_SERVER_receive_done (client,
2444 session = lookup_session_by_client (plugin, client);
2445 if (NULL == session)
2447 /* No inbound session found */
2451 GNUNET_SERVER_client_get_address (client,
2454 LOG (GNUNET_ERROR_TYPE_ERROR,
2455 "Received unexpected %u bytes of type %u from `%s'\n",
2456 (unsigned int) ntohs (message->size),
2457 (unsigned int) ntohs (message->type),
2461 GNUNET_SERVER_receive_done (client,
2463 GNUNET_free_non_null (vaddr);
2466 if (GNUNET_YES == session->expecting_welcome)
2468 /* Session is expecting WELCOME message */
2472 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2473 LOG (GNUNET_ERROR_TYPE_ERROR,
2474 "Received unexpected %u bytes of type %u from `%s'\n",
2475 (unsigned int) ntohs (message->size),
2476 (unsigned int) ntohs (message->type),
2477 GNUNET_a2s (vaddr, alen));
2479 GNUNET_SERVER_receive_done (client,
2481 GNUNET_free_non_null (vaddr);
2485 session->last_activity = GNUNET_TIME_absolute_get ();
2486 LOG (GNUNET_ERROR_TYPE_DEBUG,
2487 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2488 (unsigned int) ntohs (message->size),
2489 (unsigned int) ntohs (message->type),
2490 GNUNET_i2s (&session->target));
2492 GNUNET_STATISTICS_update (plugin->env->stats,
2493 gettext_noop ("# bytes received via TCP"),
2494 ntohs (message->size),
2497 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2500 delay = plugin->env->receive (plugin->env->cls,
2504 reschedule_session_timeout (session);
2505 if (0 == delay.rel_value_us)
2507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2511 LOG(GNUNET_ERROR_TYPE_DEBUG,
2512 "Throttling receiving from `%s' for %s\n",
2513 GNUNET_i2s (&session->target),
2514 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2515 GNUNET_SERVER_disable_receive_done_warning (client);
2516 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2517 &delayed_done, session);
2523 * Function called whenever a peer is connected on the "SERVER" level.
2524 * Increments number of active connections and suspends server if we
2525 * have reached the limit.
2527 * @param cls closure
2528 * @param client identification of the client
2531 connect_notify (void *cls,
2532 struct GNUNET_SERVER_Client *client)
2534 struct Plugin *plugin = cls;
2536 plugin->cur_connections++;
2537 if (plugin->cur_connections != plugin->max_connections)
2539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2540 _("TCP connection limit reached, suspending server\n"));
2541 GNUNET_STATISTICS_update (plugin->env->stats,
2542 gettext_noop ("# TCP service suspended"),
2545 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2550 * Function called whenever a peer is disconnected on the "SERVER"
2551 * level. Cleans up the connection, decrements number of active
2552 * connections and if applicable resumes listening.
2554 * @param cls closure
2555 * @param client identification of the client
2558 disconnect_notify (void *cls,
2559 struct GNUNET_SERVER_Client *client)
2561 struct Plugin *plugin = cls;
2562 struct Session *session;
2566 session = lookup_session_by_client (plugin, client);
2567 if (NULL == session)
2568 return; /* unknown, nothing to do */
2569 LOG (GNUNET_ERROR_TYPE_DEBUG,
2570 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2571 GNUNET_i2s (&session->target),
2572 tcp_plugin_address_to_string (session->plugin,
2573 session->address->address,
2574 session->address->address_length));
2576 if (plugin->cur_connections == plugin->max_connections)
2578 GNUNET_STATISTICS_update (session->plugin->env->stats,
2579 gettext_noop ("# TCP service resumed"),
2582 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2584 GNUNET_assert (plugin->cur_connections >= 1);
2585 plugin->cur_connections--;
2586 GNUNET_STATISTICS_update (session->plugin->env->stats,
2587 gettext_noop ("# network-level TCP disconnect events"),
2590 tcp_plugin_disconnect_session (plugin,
2596 * We can now send a probe message, copy into buffer to really send.
2598 * @param cls closure, a `struct TCPProbeContext`
2599 * @param size max size to copy
2600 * @param buf buffer to copy message to
2601 * @return number of bytes copied into @a buf
2604 notify_send_probe (void *cls,
2608 struct TCPProbeContext *tcp_probe_ctx = cls;
2609 struct Plugin *plugin = tcp_probe_ctx->plugin;
2612 tcp_probe_ctx->transmit_handle = NULL;
2613 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2618 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2619 GNUNET_free(tcp_probe_ctx);
2622 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2624 &tcp_probe_ctx->message,
2625 sizeof(tcp_probe_ctx->message));
2626 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2627 tcp_probe_ctx->sock);
2628 ret = sizeof(tcp_probe_ctx->message);
2629 GNUNET_free (tcp_probe_ctx);
2635 * Function called by the NAT subsystem suggesting another peer wants
2636 * to connect to us via connection reversal. Try to connect back to the
2639 * @param cls closure
2640 * @param addr address to try
2641 * @param addrlen number of bytes in @a addr
2644 try_connection_reversal (void *cls,
2645 const struct sockaddr *addr,
2648 struct Plugin *plugin = cls;
2649 struct GNUNET_CONNECTION_Handle *sock;
2650 struct TCPProbeContext *tcp_probe_ctx;
2653 * We have received an ICMP response, ostensibly from a peer
2654 * that wants to connect to us! Send a message to establish a connection.
2656 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2661 /* failed for some odd reason (out of sockets?); ignore attempt */
2665 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2666 tcp_probe_ctx->message.header.size
2667 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2668 tcp_probe_ctx->message.header.type
2669 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2670 tcp_probe_ctx->message.clientIdentity
2671 = *plugin->env->my_identity;
2672 tcp_probe_ctx->plugin = plugin;
2673 tcp_probe_ctx->sock = sock;
2674 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2677 tcp_probe_ctx->transmit_handle
2678 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2679 ntohs (tcp_probe_ctx->message.header.size),
2680 GNUNET_TIME_UNIT_FOREVER_REL,
2687 * Function obtain the network type for a session
2689 * @param cls closure (`struct Plugin *`)
2690 * @param session the session
2691 * @return the network type in HBO or #GNUNET_SYSERR
2693 static enum GNUNET_ATS_Network_Type
2694 tcp_plugin_get_network (void *cls,
2695 struct Session *session)
2697 return session->scope;
2702 * Return information about the given session to the
2705 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2706 * @param peer peer we send information about
2707 * @param value our `struct Session` to send information about
2708 * @return #GNUNET_OK (continue to iterate)
2711 send_session_info_iter (void *cls,
2712 const struct GNUNET_PeerIdentity *peer,
2715 struct Plugin *plugin = cls;
2716 struct Session *session = value;
2718 notify_session_monitor (plugin,
2720 GNUNET_TRANSPORT_SS_INIT);
2721 /* FIXME: cannot tell if this is up or not from current
2723 notify_session_monitor (plugin,
2725 GNUNET_TRANSPORT_SS_UP);
2731 * Begin monitoring sessions of a plugin. There can only
2732 * be one active monitor per plugin (i.e. if there are
2733 * multiple monitors, the transport service needs to
2734 * multiplex the generated events over all of them).
2736 * @param cls closure of the plugin
2737 * @param sic callback to invoke, NULL to disable monitor;
2738 * plugin will being by iterating over all active
2739 * sessions immediately and then enter monitor mode
2740 * @param sic_cls closure for @a sic
2743 tcp_plugin_setup_monitor (void *cls,
2744 GNUNET_TRANSPORT_SessionInfoCallback sic,
2747 struct Plugin *plugin = cls;
2750 plugin->sic_cls = sic_cls;
2753 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2754 &send_session_info_iter,
2756 /* signal end of first iteration */
2757 sic (sic_cls, NULL, NULL);
2763 * Entry point for the plugin.
2765 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2766 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2769 libgnunet_plugin_transport_tcp_init (void *cls)
2771 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2772 { &handle_tcp_welcome, NULL,
2773 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2774 sizeof(struct WelcomeMessage) },
2775 { &handle_tcp_nat_probe, NULL,
2776 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2777 sizeof(struct TCP_NAT_ProbeMessage) },
2778 { &handle_tcp_data, NULL,
2779 GNUNET_MESSAGE_TYPE_ALL, 0 },
2780 { NULL, NULL, 0, 0 }
2782 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2783 struct GNUNET_TRANSPORT_PluginFunctions *api;
2784 struct Plugin *plugin;
2785 struct GNUNET_SERVICE_Context *service;
2786 unsigned long long aport;
2787 unsigned long long bport;
2788 unsigned long long max_connections;
2790 struct GNUNET_TIME_Relative idle_timeout;
2792 struct GNUNET_NETWORK_Handle *const*lsocks;
2796 struct sockaddr **addrs;
2797 socklen_t *addrlens;
2799 if (NULL == env->receive)
2801 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2802 initialze the plugin or the API */
2803 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2805 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2806 api->address_to_string = &tcp_plugin_address_to_string;
2807 api->string_to_address = &tcp_plugin_string_to_address;
2811 GNUNET_assert (NULL != env->cfg);
2813 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2817 max_connections = 128;
2821 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2825 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2826 "ADVERTISED-PORT", &aport)) &&
2829 LOG(GNUNET_ERROR_TYPE_ERROR,
2830 _("Require valid port number for service `%s' in configuration!\n"),
2840 service = GNUNET_SERVICE_start ("transport-tcp",
2842 GNUNET_SERVICE_OPTION_NONE);
2843 if (NULL == service)
2845 LOG (GNUNET_ERROR_TYPE_WARNING,
2846 _("Failed to start service.\n"));
2854 plugin = GNUNET_new (struct Plugin);
2855 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2857 plugin->max_connections = max_connections;
2858 plugin->open_port = bport;
2859 plugin->adv_port = aport;
2861 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2862 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2863 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2865 if ( (NULL != service) &&
2867 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2872 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2873 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2876 uint32_t len = sizeof (struct WelcomeMessage);
2878 for (i=0;NULL!=lsocks[i];i++)
2881 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2885 sizeof (struct GNUNET_PeerIdentity))) ||
2887 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2889 TCP_STEALTH_INTEGRITY_LEN,
2893 /* TCP STEALTH not supported by kernel */
2894 GNUNET_assert (0 == i);
2895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2896 _("TCP_STEALTH not supported on this platform.\n"));
2902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2903 _("TCP_STEALTH not supported on this platform.\n"));
2908 if ( (NULL != service) &&
2911 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2916 for (ret = ret_s-1; ret >= 0; ret--)
2917 LOG (GNUNET_ERROR_TYPE_INFO,
2918 "Binding to address `%s'\n",
2919 GNUNET_a2s (addrs[ret], addrlens[ret]));
2921 = GNUNET_NAT_register (env->cfg,
2924 (unsigned int) ret_s,
2925 (const struct sockaddr **) addrs, addrlens,
2926 &tcp_nat_port_map_callback,
2927 &try_connection_reversal,
2929 for (ret = ret_s -1; ret >= 0; ret--)
2930 GNUNET_free (addrs[ret]);
2931 GNUNET_free_non_null (addrs);
2932 GNUNET_free_non_null (addrlens);
2936 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2943 &try_connection_reversal,
2946 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2948 api->send = &tcp_plugin_send;
2949 api->get_session = &tcp_plugin_get_session;
2950 api->disconnect_session = &tcp_plugin_disconnect_session;
2951 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2952 api->disconnect_peer = &tcp_plugin_disconnect;
2953 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2954 api->check_address = &tcp_plugin_check_address;
2955 api->address_to_string = &tcp_plugin_address_to_string;
2956 api->string_to_address = &tcp_plugin_string_to_address;
2957 api->get_network = &tcp_plugin_get_network;
2958 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2959 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2960 api->setup_monitor = &tcp_plugin_setup_monitor;
2961 plugin->service = service;
2962 if (NULL != service)
2964 plugin->server = GNUNET_SERVICE_get_server (service);
2969 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2974 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2980 = GNUNET_SERVER_create_with_sockets (NULL,
2986 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2987 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2988 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2989 plugin->handlers[i].callback_cls = plugin;
2991 GNUNET_SERVER_add_handlers (plugin->server,
2993 GNUNET_SERVER_connect_notify (plugin->server,
2996 GNUNET_SERVER_disconnect_notify (plugin->server,
2999 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
3002 LOG (GNUNET_ERROR_TYPE_INFO,
3003 _("TCP transport listening on port %llu\n"),
3006 LOG (GNUNET_ERROR_TYPE_INFO,
3007 _("TCP transport not listening on any port (client only)\n"));
3008 if ( (aport != bport) &&
3010 LOG (GNUNET_ERROR_TYPE_INFO,
3011 _("TCP transport advertises itself as being on port %llu\n"),
3013 /* Initially set connections to 0 */
3014 GNUNET_STATISTICS_set (plugin->env->stats,
3015 gettext_noop ("# TCP sessions active"),
3021 if (NULL != plugin->nat)
3022 GNUNET_NAT_unregister (plugin->nat);
3023 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3024 if (NULL != service)
3025 GNUNET_SERVICE_stop (service);
3026 GNUNET_free (plugin);
3027 GNUNET_free_non_null (api);
3033 * Exit point from the plugin.
3035 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
3039 libgnunet_plugin_transport_tcp_done (void *cls)
3041 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3042 struct Plugin *plugin = api->cls;
3043 struct TCPProbeContext *tcp_probe;
3044 struct PrettyPrinterContext *cur;
3045 struct PrettyPrinterContext *next;
3052 LOG (GNUNET_ERROR_TYPE_DEBUG,
3053 "Shutting down TCP plugin\n");
3055 /* Removing leftover sessions */
3056 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3057 &session_disconnect_it,
3059 /* Removing leftover NAT sessions */
3060 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
3061 &session_disconnect_it,
3064 for (cur = plugin->ppc_dll_head; NULL != cur; cur = next)
3067 GNUNET_CONTAINER_DLL_remove (plugin->ppc_dll_head,
3068 plugin->ppc_dll_tail,
3070 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3071 cur->asc (cur->asc_cls,
3077 if (NULL != plugin->service)
3078 GNUNET_SERVICE_stop (plugin->service);
3080 GNUNET_SERVER_destroy (plugin->server);
3081 GNUNET_free(plugin->handlers);
3082 if (NULL != plugin->nat)
3083 GNUNET_NAT_unregister (plugin->nat);
3084 while (NULL != (tcp_probe = plugin->probe_head))
3086 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3089 GNUNET_CONNECTION_destroy (tcp_probe->sock);
3090 GNUNET_free(tcp_probe);
3092 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3093 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3094 GNUNET_free(plugin);
3099 /* end of plugin_transport_tcp.c */