2 This file is part of GNUnet
3 Copyright (C) 2002--2014 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 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 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 * Encapsulation of all of the state of the plugin.
359 struct GNUNET_TRANSPORT_PluginEnvironment *env;
364 struct GNUNET_CONNECTION_Handle *lsock;
367 * Our handle to the NAT module.
369 struct GNUNET_NAT_Handle *nat;
372 * Map from peer identities to sessions for the given peer.
374 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
377 * Handle to the network service.
379 struct GNUNET_SERVICE_Context *service;
382 * Handle to the server for this service.
384 struct GNUNET_SERVER_Handle *server;
387 * Copy of the handler array where the closures are
388 * set to this struct's instance.
390 struct GNUNET_SERVER_MessageHandler *handlers;
393 * Map of peers we have tried to contact behind a NAT
395 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
398 * List of active TCP probes.
400 struct TCPProbeContext *probe_head;
403 * List of active TCP probes.
405 struct TCPProbeContext *probe_tail;
408 * Handle for (DYN)DNS lookup of our external IP.
410 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
413 * Function to call about session status changes.
415 GNUNET_TRANSPORT_SessionInfoCallback sic;
418 * Closure for @e sic.
423 * Welcome message used by this peer.
425 struct WelcomeMessage my_welcome;
428 * How many more TCP sessions are we allowed to open right now?
430 unsigned long long max_connections;
433 * How many more TCP sessions do we have right now?
435 unsigned long long cur_connections;
438 * ID of task used to update our addresses when one expires.
440 struct GNUNET_SCHEDULER_Task * address_update_task;
448 * Port that we are actually listening on.
453 * Port that the user said we would have visible to the
462 * If a session monitor is attached, notify it about the new
465 * @param plugin our plugin
466 * @param session session that changed state
467 * @param state new state of the session
470 notify_session_monitor (struct Plugin *plugin,
471 struct Session *session,
472 enum GNUNET_TRANSPORT_SessionState state)
474 struct GNUNET_TRANSPORT_SessionInfo info;
476 if (NULL == plugin->sic)
478 memset (&info, 0, sizeof (info));
480 info.is_inbound = (0 != (GNUNET_HELLO_ADDRESS_INFO_INBOUND & session->address->local_info))
483 info.num_msg_pending = session->msgs_in_queue;
484 info.num_bytes_pending = session->bytes_in_queue;
485 if (NULL != session->receive_delay_task)
486 info.receive_delay = session->receive_delay;
487 info.session_timeout = session->timeout;
488 info.address = session->address;
489 plugin->sic (plugin->sic_cls,
496 * Function called for a quick conversion of the binary address to
497 * a numeric address. Note that the caller must not free the
498 * address and that the next call to this function is allowed
499 * to override the address again.
501 * @param cls closure (`struct Plugin *`)
502 * @param addr binary address
503 * @param addrlen length of @a addr
504 * @return string representing the same address
507 tcp_plugin_address_to_string (void *cls,
513 * Function to check if an inbound connection is acceptable.
514 * Mostly used to limit the total number of open connections
517 * @param cls the `struct Plugin`
518 * @param ucred credentials, if available, otherwise NULL
519 * @param addr address
520 * @param addrlen length of @a addr
521 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
522 * for unknown address family (will be denied).
525 plugin_tcp_access_check (void *cls,
526 const struct GNUNET_CONNECTION_Credentials *ucred,
527 const struct sockaddr *addr,
530 struct Plugin *plugin = cls;
532 LOG (GNUNET_ERROR_TYPE_DEBUG,
533 "Accepting new incoming TCP connection from `%s'\n",
534 GNUNET_a2s (addr, addrlen));
535 if (plugin->cur_connections >= plugin->max_connections)
537 plugin->cur_connections++;
543 * Our external IP address/port mapping has changed.
545 * @param cls closure, the `struct Plugin`
546 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
547 * the previous (now invalid) one
548 * @param addr either the previous or the new public IP address
549 * @param addrlen actual length of @a addr
552 tcp_nat_port_map_callback (void *cls,
554 const struct sockaddr *addr,
557 struct Plugin *plugin = cls;
558 struct GNUNET_HELLO_Address *address;
559 struct IPv4TcpAddress t4;
560 struct IPv6TcpAddress t6;
564 LOG(GNUNET_ERROR_TYPE_INFO,
565 "NAT notification to %s address `%s'\n",
566 (GNUNET_YES == add_remove) ? "add" : "remove",
567 GNUNET_a2s (addr, addrlen));
568 /* convert 'addr' to our internal format */
569 switch (addr->sa_family)
572 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
573 memset (&t4, 0, sizeof(t4));
574 t4.options = htonl (plugin->myoptions);
575 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
576 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
581 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
582 memset (&t6, 0, sizeof(t6));
583 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
584 sizeof(struct in6_addr));
585 t6.options = htonl (plugin->myoptions);
586 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
594 /* modify our published address list */
595 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
596 (args == sizeof (struct IPv6TcpAddress)));
597 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
598 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
599 plugin->env->notify_address (plugin->env->cls, add_remove, address);
600 GNUNET_HELLO_address_free(address);
605 * Function called for a quick conversion of the binary address to
606 * a numeric address. Note that the caller must not free the
607 * address and that the next call to this function is allowed
608 * to override the address again.
610 * @param cls closure (`struct Plugin*`)
611 * @param addr binary address
612 * @param addrlen length of @a addr
613 * @return string representing the same address
616 tcp_plugin_address_to_string (void *cls,
620 static char rbuf[INET6_ADDRSTRLEN + 12];
621 char buf[INET6_ADDRSTRLEN];
625 const struct IPv4TcpAddress *t4;
626 const struct IPv6TcpAddress *t6;
633 case sizeof(struct IPv6TcpAddress):
636 port = ntohs (t6->t6_port);
637 options = ntohl (t6->options);
638 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
641 case sizeof(struct IPv4TcpAddress):
644 port = ntohs (t4->t4_port);
645 options = ntohl (t4->options);
646 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
650 LOG (GNUNET_ERROR_TYPE_WARNING,
651 _("Unexpected address length: %u bytes\n"),
652 (unsigned int) addrlen);
655 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
657 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
661 GNUNET_snprintf (rbuf, sizeof(rbuf),
662 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
672 * Function called to convert a string address to
675 * @param cls closure (`struct Plugin*`)
676 * @param addr string address
677 * @param addrlen length of the address
678 * @param buf location to store the buffer
679 * @param added location to store the number of bytes in the buffer.
680 * If the function returns #GNUNET_SYSERR, its contents are undefined.
681 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
684 tcp_plugin_string_to_address (void *cls,
690 struct sockaddr_storage socket_address;
696 /* Format tcp.options.address:port */
700 if ((NULL == addr) || (addrlen == 0))
703 return GNUNET_SYSERR;
705 if ('\0' != addr[addrlen - 1])
708 return GNUNET_SYSERR;
710 if (strlen (addr) != addrlen - 1)
713 return GNUNET_SYSERR;
715 plugin = GNUNET_strdup (addr);
716 optionstr = strchr (plugin, '.');
717 if (NULL == optionstr)
721 return GNUNET_SYSERR;
725 options = atol (optionstr);
726 address = strchr (optionstr, '.');
731 return GNUNET_SYSERR;
737 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
742 return GNUNET_SYSERR;
746 switch (socket_address.ss_family)
750 struct IPv4TcpAddress *t4;
751 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
752 t4 = GNUNET_new (struct IPv4TcpAddress);
753 t4->options = htonl (options);
754 t4->ipv4_addr = in4->sin_addr.s_addr;
755 t4->t4_port = in4->sin_port;
757 *added = sizeof(struct IPv4TcpAddress);
762 struct IPv6TcpAddress *t6;
763 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
764 t6 = GNUNET_new (struct IPv6TcpAddress);
765 t6->options = htonl (options);
766 t6->ipv6_addr = in6->sin6_addr;
767 t6->t6_port = in6->sin6_port;
769 *added = sizeof(struct IPv6TcpAddress);
773 return GNUNET_SYSERR;
779 * Find the session handle for the given client.
780 * Currently uses both the hashmap and the client
781 * context, as the client context is new and the
782 * logic still needs to be tested.
784 * @param plugin the plugin
785 * @param client which client to find the session handle for
786 * @return NULL if no matching session exists
788 static struct Session *
789 lookup_session_by_client (struct Plugin *plugin,
790 struct GNUNET_SERVER_Client *client)
792 return GNUNET_SERVER_client_get_user_context (client, struct Session);
797 * Functions with this signature are called whenever we need
798 * to close a session due to a disconnect or failure to
799 * establish a connection.
801 * @param cls the `struct Plugin`
802 * @param session session to close down
803 * @return #GNUNET_OK on success
806 tcp_plugin_disconnect_session (void *cls,
807 struct Session *session)
809 struct Plugin *plugin = cls;
810 struct PendingMessage *pm;
812 LOG (GNUNET_ERROR_TYPE_DEBUG,
813 "Disconnecting session of peer `%s' address `%s'\n",
814 GNUNET_i2s (&session->target),
815 tcp_plugin_address_to_string (session->plugin,
816 session->address->address,
817 session->address->address_length));
819 if (NULL != session->timeout_task)
821 GNUNET_SCHEDULER_cancel (session->timeout_task);
822 session->timeout_task = NULL;
823 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
827 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
831 GNUNET_STATISTICS_update (session->plugin->env->stats,
832 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
836 GNUNET_assert(GNUNET_YES ==
837 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
841 if (NULL != session->client)
842 GNUNET_SERVER_client_set_user_context (session->client,
846 if (NULL != session->transmit_handle)
848 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
849 session->transmit_handle = NULL;
851 session->plugin->env->session_end (session->plugin->env->cls,
855 if (NULL != session->nat_connection_timeout)
857 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
858 session->nat_connection_timeout = NULL;
861 while (NULL != (pm = session->pending_messages_head))
863 LOG (GNUNET_ERROR_TYPE_DEBUG,
864 (NULL != pm->transmit_cont)
865 ? "Could not deliver message to `%4s'.\n"
866 : "Could not deliver message to `%4s', notifying.\n",
867 GNUNET_i2s (&session->target));
868 GNUNET_STATISTICS_update (session->plugin->env->stats,
869 gettext_noop ("# bytes currently in TCP buffers"),
870 -(int64_t) pm->message_size, GNUNET_NO);
871 GNUNET_STATISTICS_update (session->plugin->env->stats,
872 gettext_noop ("# bytes discarded by TCP (disconnect)"),
875 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
876 session->pending_messages_tail,
878 GNUNET_assert (0 < session->msgs_in_queue);
879 session->msgs_in_queue--;
880 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
881 session->bytes_in_queue -= pm->message_size;
882 if (NULL != pm->transmit_cont)
883 pm->transmit_cont (pm->transmit_cont_cls,
890 GNUNET_assert (0 == session->msgs_in_queue);
891 GNUNET_assert (0 == session->bytes_in_queue);
892 notify_session_monitor (session->plugin,
894 GNUNET_TRANSPORT_SS_DONE);
896 if (NULL != session->receive_delay_task)
898 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
899 if (NULL != session->client)
900 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
902 if (NULL != session->client)
904 GNUNET_SERVER_client_disconnect (session->client);
905 GNUNET_SERVER_client_drop (session->client);
906 session->client = NULL;
908 GNUNET_HELLO_address_free (session->address);
909 GNUNET_assert(NULL == session->transmit_handle);
910 GNUNET_free(session);
916 * Function that is called to get the keepalive factor.
917 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
918 * calculate the interval between keepalive packets.
920 * @param cls closure with the `struct Plugin`
921 * @return keepalive factor
924 tcp_plugin_query_keepalive_factor (void *cls)
931 * Session was idle, so disconnect it
933 * @param cls the `struct Session` of the idle session
934 * @param tc scheduler context
937 session_timeout (void *cls,
938 const struct GNUNET_SCHEDULER_TaskContext *tc)
940 struct Session *s = cls;
941 struct GNUNET_TIME_Relative left;
943 s->timeout_task = NULL;
944 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
945 if (0 != left.rel_value_us)
947 /* not actually our turn yet, but let's at least update
948 the monitor, it may think we're about to die ... */
949 notify_session_monitor (s->plugin,
951 GNUNET_TRANSPORT_SS_UPDATE);
952 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Session %p was idle for %s, disconnecting\n",
960 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
962 /* call session destroy function */
963 tcp_plugin_disconnect_session (s->plugin, s);
968 * Increment session timeout due to activity
970 * @param s session to increment timeout for
973 reschedule_session_timeout (struct Session *s)
975 GNUNET_assert(NULL != s->timeout_task);
976 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
981 * Create a new session. Also queues a welcome message.
983 * @param plugin the plugin
984 * @param address the address to create the session for
985 * @param client client to use, reference counter must have already been increased
986 * @param is_nat this a NAT session, we should wait for a client to
987 * connect to us from an address, then assign that to
989 * @return new session object
991 static struct Session *
992 create_session (struct Plugin *plugin,
993 const struct GNUNET_HELLO_Address *address,
994 struct GNUNET_SERVER_Client *client,
997 struct Session *session;
998 struct PendingMessage *pm;
1000 if (GNUNET_YES != is_nat)
1001 GNUNET_assert (NULL != client);
1003 GNUNET_assert (NULL == client);
1005 LOG (GNUNET_ERROR_TYPE_DEBUG,
1006 "Creating new session for peer `%4s'\n",
1007 GNUNET_i2s (&address->peer));
1008 session = GNUNET_new (struct Session);
1009 session->last_activity = GNUNET_TIME_absolute_get ();
1010 session->plugin = plugin;
1011 session->is_nat = is_nat;
1012 session->client = client;
1013 session->address = GNUNET_HELLO_address_copy (address);
1014 session->target = address->peer;
1015 session->expecting_welcome = GNUNET_YES;
1016 session->scope = GNUNET_ATS_NET_UNSPECIFIED;
1017 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1018 sizeof (struct WelcomeMessage));
1019 pm->msg = (const char *) &pm[1];
1020 pm->message_size = sizeof(struct WelcomeMessage);
1022 &plugin->my_welcome,
1023 sizeof(struct WelcomeMessage));
1024 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1025 GNUNET_STATISTICS_update (plugin->env->stats,
1026 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
1028 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1029 session->pending_messages_tail,
1031 session->msgs_in_queue++;
1032 session->bytes_in_queue += pm->message_size;
1033 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1034 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1037 notify_session_monitor (session->plugin,
1039 GNUNET_TRANSPORT_SS_INIT);
1040 if (GNUNET_YES != is_nat)
1042 GNUNET_STATISTICS_update (plugin->env->stats,
1043 gettext_noop ("# TCP sessions active"),
1046 notify_session_monitor (session->plugin,
1048 GNUNET_TRANSPORT_SS_UP);
1052 notify_session_monitor (session->plugin,
1054 GNUNET_TRANSPORT_SS_HANDSHAKE);
1061 * If we have pending messages, ask the server to
1062 * transmit them (schedule the respective tasks, etc.)
1064 * @param session for which session should we do this
1067 process_pending_messages (struct Session *session);
1071 * Function called to notify a client about the socket
1072 * being ready to queue more data. "buf" will be
1073 * NULL and "size" zero if the socket was closed for
1074 * writing in the meantime.
1076 * @param cls closure
1077 * @param size number of bytes available in @a buf
1078 * @param buf where the callee should write the message
1079 * @return number of bytes written to @a buf
1082 do_transmit (void *cls, size_t size, void *buf)
1084 struct Session *session = cls;
1085 struct GNUNET_PeerIdentity pid;
1086 struct Plugin *plugin;
1087 struct PendingMessage *pos;
1088 struct PendingMessage *hd;
1089 struct PendingMessage *tl;
1090 struct GNUNET_TIME_Absolute now;
1094 session->transmit_handle = NULL;
1095 plugin = session->plugin;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1099 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1100 GNUNET_i2s (&session->target));
1101 /* timeout; cancel all messages that have already expired */
1105 now = GNUNET_TIME_absolute_get ();
1106 while ( (NULL != (pos = session->pending_messages_head)) &&
1107 (pos->timeout.abs_value_us <= now.abs_value_us) )
1109 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1110 session->pending_messages_tail,
1112 GNUNET_assert (0 < session->msgs_in_queue);
1113 session->msgs_in_queue--;
1114 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1115 session->bytes_in_queue -= pos->message_size;
1116 LOG (GNUNET_ERROR_TYPE_DEBUG,
1117 "Failed to transmit %u byte message to `%4s'.\n",
1119 GNUNET_i2s (&session->target));
1120 ret += pos->message_size;
1121 GNUNET_CONTAINER_DLL_insert_after (hd,
1126 /* do this call before callbacks (so that if callbacks destroy
1127 * session, they have a chance to cancel actions done by this
1129 process_pending_messages (session);
1130 pid = session->target;
1131 /* no do callbacks and do not use session again since
1132 * the callbacks may abort the session */
1133 while (NULL != (pos = hd))
1135 GNUNET_CONTAINER_DLL_remove (hd,
1138 if (NULL != pos->transmit_cont)
1139 pos->transmit_cont (pos->transmit_cont_cls,
1146 GNUNET_STATISTICS_update (plugin->env->stats,
1147 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1149 GNUNET_STATISTICS_update (plugin->env->stats,
1150 gettext_noop ("# bytes discarded by TCP (timeout)"),
1154 notify_session_monitor (session->plugin,
1156 GNUNET_TRANSPORT_SS_UPDATE);
1159 /* copy all pending messages that would fit */
1164 while (NULL != (pos = session->pending_messages_head))
1166 if (ret + pos->message_size > size)
1168 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1169 session->pending_messages_tail,
1171 GNUNET_assert (0 < session->msgs_in_queue);
1172 session->msgs_in_queue--;
1173 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1174 session->bytes_in_queue -= pos->message_size;
1175 GNUNET_assert(size >= pos->message_size);
1176 LOG (GNUNET_ERROR_TYPE_DEBUG,
1177 "Transmitting message of type %u size %u to %s\n",
1178 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1180 tcp_plugin_address_to_string (session->plugin,
1181 session->address->address,
1182 session->address->address_length));
1183 /* FIXME: this memcpy can be up to 7% of our total runtime */
1187 cbuf += pos->message_size;
1188 ret += pos->message_size;
1189 size -= pos->message_size;
1190 GNUNET_CONTAINER_DLL_insert_tail (hd,
1194 notify_session_monitor (session->plugin,
1196 GNUNET_TRANSPORT_SS_UPDATE);
1197 /* schedule 'continuation' before callbacks so that callbacks that
1198 * cancel everything don't cause us to use a session that no longer
1200 process_pending_messages (session);
1201 session->last_activity = GNUNET_TIME_absolute_get ();
1202 pid = session->target;
1203 /* we'll now call callbacks that may cancel the session; hence
1204 * we should not use 'session' after this point */
1205 while (NULL != (pos = hd))
1207 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1208 if (NULL != pos->transmit_cont)
1209 pos->transmit_cont (pos->transmit_cont_cls,
1213 pos->message_size); /* FIXME: include TCP overhead */
1216 GNUNET_assert (NULL == hd);
1217 GNUNET_assert (NULL == tl);
1218 LOG (GNUNET_ERROR_TYPE_DEBUG,
1219 "Transmitting %u bytes\n",
1221 GNUNET_STATISTICS_update (plugin->env->stats,
1222 gettext_noop ("# bytes currently in TCP buffers"),
1225 GNUNET_STATISTICS_update (plugin->env->stats,
1226 gettext_noop ("# bytes transmitted via TCP"),
1234 * If we have pending messages, ask the server to
1235 * transmit them (schedule the respective tasks, etc.)
1237 * @param session for which session should we do this
1240 process_pending_messages (struct Session *session)
1242 struct PendingMessage *pm;
1244 GNUNET_assert (NULL != session->client);
1245 if (NULL != session->transmit_handle)
1247 if (NULL == (pm = session->pending_messages_head))
1250 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1252 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1259 * Function that can be used by the transport service to transmit
1260 * a message using the plugin. Note that in the case of a
1261 * peer disconnecting, the continuation MUST be called
1262 * prior to the disconnect notification itself. This function
1263 * will be called with this peer's HELLO message to initiate
1264 * a fresh connection to another peer.
1266 * @param cls closure
1267 * @param session which session must be used
1268 * @param msgbuf the message to transmit
1269 * @param msgbuf_size number of bytes in 'msgbuf'
1270 * @param priority how important is the message (most plugins will
1271 * ignore message priority and just FIFO)
1272 * @param to how long to wait at most for the transmission (does not
1273 * require plugins to discard the message after the timeout,
1274 * just advisory for the desired delay; most plugins will ignore
1276 * @param cont continuation to call once the message has
1277 * been transmitted (or if the transport is ready
1278 * for the next transmission call; or if the
1279 * peer disconnected...); can be NULL
1280 * @param cont_cls closure for @a cont
1281 * @return number of bytes used (on the physical network, with overheads);
1282 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1283 * and does NOT mean that the message was not transmitted (DV)
1286 tcp_plugin_send (void *cls,
1287 struct Session *session,
1290 unsigned int priority,
1291 struct GNUNET_TIME_Relative to,
1292 GNUNET_TRANSPORT_TransmitContinuation cont,
1295 struct Plugin * plugin = cls;
1296 struct PendingMessage *pm;
1298 /* create new message entry */
1299 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1300 pm->msg = (const char *) &pm[1];
1301 memcpy (&pm[1], msgbuf, msgbuf_size);
1302 pm->message_size = msgbuf_size;
1303 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1304 pm->transmit_cont = cont;
1305 pm->transmit_cont_cls = cont_cls;
1307 LOG(GNUNET_ERROR_TYPE_DEBUG,
1308 "Asked to transmit %u bytes to `%s', added message to list.\n",
1310 GNUNET_i2s (&session->target));
1313 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1317 GNUNET_assert (NULL != session->client);
1318 GNUNET_SERVER_client_set_timeout (session->client,
1319 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1320 GNUNET_STATISTICS_update (plugin->env->stats,
1321 gettext_noop ("# bytes currently in TCP buffers"),
1325 /* append pm to pending_messages list */
1326 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1327 session->pending_messages_tail,
1329 notify_session_monitor (session->plugin,
1331 GNUNET_TRANSPORT_SS_UPDATE);
1332 session->msgs_in_queue++;
1333 session->bytes_in_queue += pm->message_size;
1334 process_pending_messages (session);
1338 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1342 LOG (GNUNET_ERROR_TYPE_DEBUG,
1343 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1344 GNUNET_i2s (&session->target));
1345 GNUNET_STATISTICS_update (plugin->env->stats,
1346 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1348 /* append pm to pending_messages list */
1349 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1350 session->pending_messages_tail,
1352 session->msgs_in_queue++;
1353 session->bytes_in_queue += pm->message_size;
1354 notify_session_monitor (session->plugin,
1356 GNUNET_TRANSPORT_SS_HANDSHAKE);
1359 LOG(GNUNET_ERROR_TYPE_ERROR,
1360 "Invalid session %p\n",
1370 return GNUNET_SYSERR; /* session does not exist here */
1375 * Closure for #session_lookup_it().
1380 * Address we are looking for.
1382 const struct GNUNET_HELLO_Address *address;
1385 * Where to store the session (if we found it).
1387 struct Session *result;
1393 * Look for a session by address.
1395 * @param cls the `struct SessionItCtx`
1397 * @param value a `struct Session`
1398 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1401 session_lookup_it (void *cls,
1402 const struct GNUNET_PeerIdentity *key,
1405 struct SessionItCtx *si_ctx = cls;
1406 struct Session *session = value;
1409 GNUNET_HELLO_address_cmp (si_ctx->address,
1412 si_ctx->result = session;
1418 * Task cleaning up a NAT connection attempt after timeout
1420 * @param cls the `struct Session`
1421 * @param tc scheduler context (unused)
1424 nat_connect_timeout (void *cls,
1425 const struct GNUNET_SCHEDULER_TaskContext *tc)
1427 struct Session *session = cls;
1429 session->nat_connection_timeout = NULL;
1430 LOG (GNUNET_ERROR_TYPE_DEBUG,
1431 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1432 GNUNET_i2s (&session->target),
1433 tcp_plugin_address_to_string (session->plugin,
1434 session->address->address,
1435 session->address->address_length));
1436 tcp_plugin_disconnect_session (session->plugin,
1442 * Function that will be called whenever the transport service wants to
1443 * notify the plugin that a session is still active and in use and
1444 * therefore the session timeout for this session has to be updated
1446 * @param cls closure
1447 * @param peer which peer was the session for
1448 * @param session which session is being updated
1451 tcp_plugin_update_session_timeout (void *cls,
1452 const struct GNUNET_PeerIdentity *peer,
1453 struct Session *session)
1455 reschedule_session_timeout (session);
1460 * Task to signal the server that we can continue
1461 * receiving from the TCP client now.
1463 * @param cls the `struct Session*`
1464 * @param tc task context (unused)
1467 delayed_done (void *cls,
1468 const struct GNUNET_SCHEDULER_TaskContext *tc)
1470 struct Session *session = cls;
1472 session->receive_delay_task = NULL;
1473 reschedule_session_timeout (session);
1475 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1480 * Function that will be called whenever the transport service wants to
1481 * notify the plugin that the inbound quota changed and that the plugin
1482 * should update it's delay for the next receive value
1484 * @param cls closure
1485 * @param peer which peer was the session for
1486 * @param session which session is being updated
1487 * @param delay new delay to use for receiving
1490 tcp_plugin_update_inbound_delay (void *cls,
1491 const struct GNUNET_PeerIdentity *peer,
1492 struct Session *session,
1493 struct GNUNET_TIME_Relative delay)
1495 if (NULL == session->receive_delay_task)
1497 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "New inbound delay %s\n",
1499 GNUNET_STRINGS_relative_time_to_string (delay,
1501 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1502 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1503 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1510 * Create a new session to transmit data to the target
1511 * This session will used to send data to this peer and the plugin will
1512 * notify us by calling the env->session_end function
1514 * @param cls closure
1515 * @param address the address to use
1516 * @return the session if the address is valid, NULL otherwise
1518 static struct Session *
1519 tcp_plugin_get_session (void *cls,
1520 const struct GNUNET_HELLO_Address *address)
1522 struct Plugin *plugin = cls;
1523 struct Session *session = NULL;
1527 struct GNUNET_CONNECTION_Handle *sa;
1528 struct sockaddr_in a4;
1529 struct sockaddr_in6 a6;
1530 const struct IPv4TcpAddress *t4;
1531 const struct IPv6TcpAddress *t6;
1532 unsigned int options;
1533 enum GNUNET_ATS_Network_Type net_type;
1534 unsigned int is_natd = GNUNET_NO;
1537 struct GNUNET_NETWORK_Handle *s;
1540 addrlen = address->address_length;
1541 LOG (GNUNET_ERROR_TYPE_DEBUG,
1542 "Trying to get session for `%s' address of peer `%s'\n",
1543 tcp_plugin_address_to_string (plugin,
1545 address->address_length),
1546 GNUNET_i2s (&address->peer));
1548 if (GNUNET_HELLO_address_check_option (address,
1549 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1555 /* look for existing session */
1557 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1560 struct SessionItCtx si_ctx;
1562 si_ctx.address = address;
1563 si_ctx.result = NULL;
1565 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1567 &session_lookup_it, &si_ctx);
1568 if (NULL != si_ctx.result)
1570 session = si_ctx.result;
1571 LOG (GNUNET_ERROR_TYPE_DEBUG,
1572 "Found existing session for `%s' address `%s' session %p\n",
1573 GNUNET_i2s (&address->peer),
1574 tcp_plugin_address_to_string (plugin,
1576 address->address_length),
1580 LOG (GNUNET_ERROR_TYPE_DEBUG,
1581 "Existing sessions did not match address `%s' or peer `%s'\n",
1582 tcp_plugin_address_to_string (plugin,
1584 address->address_length),
1585 GNUNET_i2s (&address->peer));
1588 if (addrlen == sizeof(struct IPv6TcpAddress))
1590 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1591 t6 = address->address;
1592 options = t6->options;
1594 memset (&a6, 0, sizeof(a6));
1595 #if HAVE_SOCKADDR_IN_SIN_LEN
1596 a6.sin6_len = sizeof (a6);
1598 a6.sin6_family = AF_INET6;
1599 a6.sin6_port = t6->t6_port;
1600 if (t6->t6_port == 0)
1601 is_natd = GNUNET_YES;
1602 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1606 else if (addrlen == sizeof(struct IPv4TcpAddress))
1608 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1609 t4 = address->address;
1610 options = t4->options;
1612 memset (&a4, 0, sizeof(a4));
1613 #if HAVE_SOCKADDR_IN_SIN_LEN
1614 a4.sin_len = sizeof (a4);
1616 a4.sin_family = AF_INET;
1617 a4.sin_port = t4->t4_port;
1618 if (t4->t4_port == 0)
1619 is_natd = GNUNET_YES;
1620 a4.sin_addr.s_addr = t4->ipv4_addr;
1626 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1627 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1631 net_type = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1632 GNUNET_break (net_type != GNUNET_ATS_NET_UNSPECIFIED);
1634 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1636 /* NAT client only works with IPv4 addresses */
1640 if (plugin->cur_connections >= plugin->max_connections)
1646 if ((is_natd == GNUNET_YES)
1648 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1651 /* Only do one NAT punch attempt per peer identity */
1655 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1657 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1660 LOG (GNUNET_ERROR_TYPE_DEBUG,
1661 "Found valid IPv4 NAT address (creating session)!\n");
1662 session = create_session (plugin,
1666 session->scope = net_type;
1667 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1668 &nat_connect_timeout,
1670 GNUNET_assert(GNUNET_OK ==
1671 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1676 LOG (GNUNET_ERROR_TYPE_DEBUG,
1677 "Created NAT WAIT connection to `%4s' at `%s'\n",
1678 GNUNET_i2s (&session->target),
1679 GNUNET_a2s (sb, sbs));
1680 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1686 LOG(GNUNET_ERROR_TYPE_DEBUG,
1687 "Running NAT client for `%4s' at `%s' failed\n",
1688 GNUNET_i2s (&session->target),
1689 GNUNET_a2s (sb, sbs));
1690 tcp_plugin_disconnect_session (plugin,
1696 /* create new outbound session */
1697 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1699 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1702 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1705 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1712 GNUNET_NETWORK_socket_setsockopt (s,
1716 sizeof (struct GNUNET_PeerIdentity))) ||
1718 GNUNET_NETWORK_socket_setsockopt (s,
1720 TCP_STEALTH_INTEGRITY,
1721 &plugin->my_welcome,
1722 sizeof (struct WelcomeMessage))) )
1724 /* TCP STEALTH not supported by kernel */
1725 GNUNET_break (GNUNET_OK ==
1726 GNUNET_NETWORK_socket_close (s));
1731 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1740 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1744 LOG (GNUNET_ERROR_TYPE_DEBUG,
1745 "Failed to create connection to `%4s' at `%s'\n",
1746 GNUNET_i2s (&address->peer),
1747 GNUNET_a2s (sb, sbs));
1750 plugin->cur_connections++;
1751 if (plugin->cur_connections == plugin->max_connections)
1752 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1754 LOG (GNUNET_ERROR_TYPE_DEBUG,
1755 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1756 GNUNET_i2s (&address->peer),
1757 GNUNET_a2s (sb, sbs));
1759 session = create_session (plugin,
1761 GNUNET_SERVER_connect_socket (plugin->server, sa),
1763 session->scope = net_type;
1764 GNUNET_SERVER_client_set_user_context (session->client,
1766 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1769 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1770 LOG (GNUNET_ERROR_TYPE_DEBUG,
1771 "Creating new session for `%s' address `%s' session %p\n",
1772 GNUNET_i2s (&address->peer),
1773 tcp_plugin_address_to_string (plugin,
1775 address->address_length),
1777 /* Send TCP Welcome */
1778 process_pending_messages (session);
1785 session_disconnect_it (void *cls,
1786 const struct GNUNET_PeerIdentity *key,
1789 struct Plugin *plugin = cls;
1790 struct Session *session = value;
1792 GNUNET_STATISTICS_update (session->plugin->env->stats,
1793 gettext_noop ("# transport-service disconnect requests for TCP"),
1796 tcp_plugin_disconnect_session (plugin,
1803 * Function that can be called to force a disconnect from the
1804 * specified neighbour. This should also cancel all previously
1805 * scheduled transmissions. Obviously the transmission may have been
1806 * partially completed already, which is OK. The plugin is supposed
1807 * to close the connection (if applicable) and no longer call the
1808 * transmit continuation(s).
1810 * Finally, plugin MUST NOT call the services's receive function to
1811 * notify the service that the connection to the specified target was
1812 * closed after a getting this call.
1814 * @param cls closure
1815 * @param target peer for which the last transmission is
1819 tcp_plugin_disconnect (void *cls,
1820 const struct GNUNET_PeerIdentity *target)
1822 struct Plugin *plugin = cls;
1824 LOG(GNUNET_ERROR_TYPE_DEBUG,
1825 "Disconnecting peer `%4s'\n",
1826 GNUNET_i2s (target));
1827 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1829 &session_disconnect_it,
1831 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1833 &session_disconnect_it,
1839 * Running pretty printers: head
1841 static struct PrettyPrinterContext *ppc_dll_head;
1844 * Running pretty printers: tail
1846 static struct PrettyPrinterContext *ppc_dll_tail;
1849 * Context for address to string conversion, closure
1850 * for #append_port().
1852 struct PrettyPrinterContext
1857 struct PrettyPrinterContext *next;
1862 struct PrettyPrinterContext *prev;
1867 struct GNUNET_SCHEDULER_Task * timeout_task;
1872 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1875 * Function to call with the result.
1877 GNUNET_TRANSPORT_AddressStringCallback asc;
1880 * Clsoure for @e asc.
1885 * Port to add after the IP address.
1902 * Append our port and forward the result.
1904 * @param cls the `struct PrettyPrinterContext *`
1905 * @param hostname hostname part of the address
1908 append_port (void *cls,
1909 const char *hostname)
1911 struct PrettyPrinterContext *ppc = cls;
1914 if (NULL == hostname)
1916 /* Final call, done */
1917 ppc->resolver_handle = NULL;
1918 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
1921 ppc->asc (ppc->asc_cls,
1927 if (GNUNET_YES == ppc->ipv6)
1928 GNUNET_asprintf (&ret,
1935 GNUNET_asprintf (&ret,
1941 ppc->asc (ppc->asc_cls,
1949 * Convert the transports address to a nice, human-readable
1952 * @param cls closure
1953 * @param type name of the transport that generated the address
1954 * @param addr one of the addresses of the host, NULL for the last address
1955 * the specific address format depends on the transport
1956 * @param addrlen length of the @a addr
1957 * @param numeric should (IP) addresses be displayed in numeric form?
1958 * @param timeout after how long should we give up?
1959 * @param asc function to call on each string
1960 * @param asc_cls closure for @a asc
1963 tcp_plugin_address_pretty_printer (void *cls,
1968 struct GNUNET_TIME_Relative timeout,
1969 GNUNET_TRANSPORT_AddressStringCallback asc,
1972 struct PrettyPrinterContext *ppc;
1975 struct sockaddr_in a4;
1976 struct sockaddr_in6 a6;
1977 const struct IPv4TcpAddress *t4;
1978 const struct IPv6TcpAddress *t6;
1982 if (sizeof(struct IPv6TcpAddress) == addrlen)
1985 memset (&a6, 0, sizeof(a6));
1986 a6.sin6_family = AF_INET6;
1987 a6.sin6_port = t6->t6_port;
1988 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1989 port = ntohs (t6->t6_port);
1990 options = ntohl (t6->options);
1994 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1997 memset (&a4, 0, sizeof(a4));
1998 a4.sin_family = AF_INET;
1999 a4.sin_port = t4->t4_port;
2000 a4.sin_addr.s_addr = t4->ipv4_addr;
2001 port = ntohs (t4->t4_port);
2002 options = ntohl (t4->options);
2008 /* invalid address */
2009 asc (asc_cls, NULL, GNUNET_SYSERR);
2010 asc (asc_cls, NULL, GNUNET_OK);
2013 ppc = GNUNET_new (struct PrettyPrinterContext);
2014 if (addrlen == sizeof(struct IPv6TcpAddress))
2015 ppc->ipv6 = GNUNET_YES;
2017 ppc->ipv6 = GNUNET_NO;
2019 ppc->asc_cls = asc_cls;
2021 ppc->options = options;
2022 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2027 if (NULL == ppc->resolver_handle)
2033 GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
2040 * Check if the given port is plausible (must be either our listen
2041 * port or our advertised port), or any port if we are behind NAT
2042 * and do not have a port open. If it is neither, we return
2045 * @param plugin global variables
2046 * @param in_port port number to check
2047 * @return #GNUNET_OK if port is either open_port or adv_port
2050 check_port (struct Plugin *plugin, uint16_t in_port)
2052 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
2054 return GNUNET_SYSERR;
2058 * Function that will be called to check if a binary address for this
2059 * plugin is well-formed and corresponds to an address for THIS peer
2060 * (as per our configuration). Naturally, if absolutely necessary,
2061 * plugins can be a bit conservative in their answer, but in general
2062 * plugins should make sure that the address does not redirect
2063 * traffic to a 3rd party that might try to man-in-the-middle our
2066 * @param cls closure, our `struct Plugin *`
2067 * @param addr pointer to the address
2068 * @param addrlen length of addr
2069 * @return #GNUNET_OK if this is a plausible address for this peer
2070 * and transport, #GNUNET_SYSERR if not
2073 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
2075 struct Plugin *plugin = cls;
2076 struct IPv4TcpAddress *v4;
2077 struct IPv6TcpAddress *v6;
2079 if ((addrlen != sizeof(struct IPv4TcpAddress))
2080 && (addrlen != sizeof(struct IPv6TcpAddress)))
2083 return GNUNET_SYSERR;
2086 if (addrlen == sizeof(struct IPv4TcpAddress))
2088 v4 = (struct IPv4TcpAddress *) addr;
2089 if (0 != memcmp (&v4->options,
2094 return GNUNET_SYSERR;
2096 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
2097 return GNUNET_SYSERR;
2099 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
2100 sizeof(struct in_addr)))
2101 return GNUNET_SYSERR;
2105 v6 = (struct IPv6TcpAddress *) addr;
2106 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2109 return GNUNET_SYSERR;
2111 if (0 != memcmp (&v6->options,
2116 return GNUNET_SYSERR;
2118 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
2119 return GNUNET_SYSERR;
2121 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
2122 sizeof(struct in6_addr)))
2123 return GNUNET_SYSERR;
2129 * We've received a nat probe from this peer via TCP. Finish
2130 * creating the client session and resume sending of queued
2133 * @param cls closure
2134 * @param client identification of the client
2135 * @param message the actual message
2138 handle_tcp_nat_probe (void *cls,
2139 struct GNUNET_SERVER_Client *client,
2140 const struct GNUNET_MessageHeader *message)
2142 struct Plugin *plugin = cls;
2143 struct Session *session;
2144 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2147 struct IPv4TcpAddress *t4;
2148 struct IPv6TcpAddress *t6;
2149 const struct sockaddr_in *s4;
2150 const struct sockaddr_in6 *s6;
2152 LOG (GNUNET_ERROR_TYPE_DEBUG,
2153 "Received NAT probe\n");
2154 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2155 * a connection to this peer by running gnunet-nat-client. This peer
2156 * received the punch message and now wants us to use the new connection
2157 * as the default for that peer. Do so and then send a WELCOME message
2158 * so we can really be connected!
2160 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2167 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2168 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2169 sizeof(struct GNUNET_PeerIdentity)))
2171 /* refuse connections from ourselves */
2172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2176 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2177 &tcp_nat_probe->clientIdentity);
2178 if (session == NULL)
2180 LOG (GNUNET_ERROR_TYPE_DEBUG,
2181 "Did NOT find session for NAT probe!\n");
2182 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2185 LOG (GNUNET_ERROR_TYPE_DEBUG,
2186 "Found session for NAT probe!\n");
2188 if (session->nat_connection_timeout != NULL)
2190 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2191 session->nat_connection_timeout = NULL;
2194 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2198 tcp_plugin_disconnect_session (plugin,
2203 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2204 GNUNET_SERVER_client_set_user_context(client, session);
2205 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2206 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2207 session->last_activity = GNUNET_TIME_absolute_get ();
2208 LOG (GNUNET_ERROR_TYPE_DEBUG,
2209 "Found address `%s' for incoming connection\n",
2210 GNUNET_a2s (vaddr, alen));
2211 switch (((const struct sockaddr *) vaddr)->sa_family)
2215 t4 = GNUNET_new (struct IPv4TcpAddress);
2216 t4->options = htonl (TCP_OPTIONS_NONE);
2217 t4->t4_port = s4->sin_port;
2218 t4->ipv4_addr = s4->sin_addr.s_addr;
2219 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2222 sizeof(struct IPv4TcpAddress),
2223 GNUNET_HELLO_ADDRESS_INFO_NONE);
2227 t6 = GNUNET_new (struct IPv6TcpAddress);
2228 t6->options = htonl (TCP_OPTIONS_NONE);
2229 t6->t6_port = s6->sin6_port;
2230 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2231 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2234 sizeof(struct IPv6TcpAddress),
2235 GNUNET_HELLO_ADDRESS_INFO_NONE);
2239 LOG(GNUNET_ERROR_TYPE_DEBUG,
2240 "Bad address for incoming connection!\n");
2242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2243 tcp_plugin_disconnect_session (plugin,
2248 GNUNET_break(NULL == session->client);
2249 GNUNET_SERVER_client_keep (client);
2250 session->client = client;
2251 GNUNET_STATISTICS_update (plugin->env->stats,
2252 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2253 process_pending_messages (session);
2254 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2259 * We've received a welcome from this peer via TCP. Possibly create a
2260 * fresh client record and send back our welcome.
2262 * @param cls closure
2263 * @param client identification of the client
2264 * @param message the actual message
2267 handle_tcp_welcome (void *cls,
2268 struct GNUNET_SERVER_Client *client,
2269 const struct GNUNET_MessageHeader *message)
2271 struct Plugin *plugin = cls;
2272 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2273 struct GNUNET_HELLO_Address *address;
2274 struct Session *session;
2277 struct IPv4TcpAddress t4;
2278 struct IPv6TcpAddress t6;
2279 const struct sockaddr_in *s4;
2280 const struct sockaddr_in6 *s6;
2282 if (0 == memcmp (&wm->clientIdentity,
2283 plugin->env->my_identity,
2284 sizeof(struct GNUNET_PeerIdentity)))
2286 /* refuse connections from ourselves */
2287 GNUNET_SERVER_receive_done (client,
2290 GNUNET_SERVER_client_get_address (client,
2294 LOG (GNUNET_ERROR_TYPE_INFO,
2295 "Received WELCOME message from my own identity `%4s' on address `%s'\n",
2296 GNUNET_i2s (&wm->clientIdentity),
2297 GNUNET_a2s (vaddr, alen));
2303 LOG(GNUNET_ERROR_TYPE_DEBUG,
2304 "Received WELCOME message from `%4s' %p\n",
2305 GNUNET_i2s (&wm->clientIdentity),
2307 GNUNET_STATISTICS_update (plugin->env->stats,
2308 gettext_noop ("# TCP WELCOME messages received"),
2311 session = lookup_session_by_client (plugin, client);
2312 if (NULL != session)
2315 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2317 LOG (GNUNET_ERROR_TYPE_DEBUG,
2318 "Found existing session %p for peer `%s'\n",
2320 GNUNET_a2s (vaddr, alen));
2321 GNUNET_free (vaddr);
2326 GNUNET_SERVER_client_keep (client);
2327 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2328 plugin->cur_connections++;
2329 if (plugin->cur_connections == plugin->max_connections)
2330 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2333 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2335 if (alen == sizeof(struct sockaddr_in))
2338 memset (&t4, '\0', sizeof (t4));
2339 t4.options = htonl (TCP_OPTIONS_NONE);
2340 t4.t4_port = s4->sin_port;
2341 t4.ipv4_addr = s4->sin_addr.s_addr;
2342 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2346 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2348 else if (alen == sizeof(struct sockaddr_in6))
2351 memset (&t6, '\0', sizeof (t6));
2352 t6.options = htonl (TCP_OPTIONS_NONE);
2353 t6.t6_port = s6->sin6_port;
2354 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2355 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2359 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2364 GNUNET_free_non_null (vaddr);
2367 session = create_session (plugin,
2371 GNUNET_HELLO_address_free (address);
2373 = plugin->env->get_address_type (plugin->env->cls,
2376 LOG (GNUNET_ERROR_TYPE_DEBUG,
2377 "Creating new%s session %p for peer `%s' client %p \n",
2378 GNUNET_HELLO_address_check_option (session->address,
2379 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2382 tcp_plugin_address_to_string (plugin,
2383 session->address->address,
2384 session->address->address_length),
2386 GNUNET_free (vaddr);
2387 GNUNET_SERVER_client_set_user_context (session->client, session);
2388 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2391 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2392 /* Notify transport and ATS about new session */
2393 plugin->env->session_start (plugin->env->cls,
2397 notify_session_monitor (plugin,
2399 GNUNET_TRANSPORT_SS_INIT);
2403 LOG(GNUNET_ERROR_TYPE_DEBUG,
2404 "Did not obtain TCP socket address for incoming connection\n");
2410 if (session->expecting_welcome != GNUNET_YES)
2413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2417 session->last_activity = GNUNET_TIME_absolute_get ();
2418 session->expecting_welcome = GNUNET_NO;
2420 process_pending_messages (session);
2421 GNUNET_SERVER_client_set_timeout (client,
2422 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2428 * We've received data for this peer via TCP. Unbox,
2429 * compute latency and forward.
2431 * @param cls closure
2432 * @param client identification of the client
2433 * @param message the actual message
2436 handle_tcp_data (void *cls,
2437 struct GNUNET_SERVER_Client *client,
2438 const struct GNUNET_MessageHeader *message)
2440 struct Plugin *plugin = cls;
2441 struct Session *session;
2442 struct GNUNET_TIME_Relative delay;
2445 type = ntohs (message->type);
2446 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2447 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2449 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2450 GNUNET_SERVER_receive_done (client,
2454 session = lookup_session_by_client (plugin, client);
2455 if (NULL == session)
2457 /* No inbound session found */
2461 GNUNET_SERVER_client_get_address (client,
2464 LOG (GNUNET_ERROR_TYPE_ERROR,
2465 "Received unexpected %u bytes of type %u from `%s'\n",
2466 (unsigned int) ntohs (message->size),
2467 (unsigned int) ntohs (message->type),
2471 GNUNET_SERVER_receive_done (client,
2473 GNUNET_free_non_null (vaddr);
2476 else if (GNUNET_YES == session->expecting_welcome)
2478 /* Session is expecting WELCOME message */
2482 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2483 LOG (GNUNET_ERROR_TYPE_ERROR,
2484 "Received unexpected %u bytes of type %u from `%s'\n",
2485 (unsigned int) ntohs (message->size),
2486 (unsigned int) ntohs (message->type),
2487 GNUNET_a2s (vaddr, alen));
2489 GNUNET_SERVER_receive_done (client,
2491 GNUNET_free_non_null (vaddr);
2495 session->last_activity = GNUNET_TIME_absolute_get ();
2496 LOG (GNUNET_ERROR_TYPE_DEBUG,
2497 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2498 (unsigned int) ntohs (message->size),
2499 (unsigned int) ntohs (message->type),
2500 GNUNET_i2s (&session->target));
2502 GNUNET_STATISTICS_update (plugin->env->stats,
2503 gettext_noop ("# bytes received via TCP"),
2504 ntohs (message->size),
2507 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2510 delay = plugin->env->receive (plugin->env->cls,
2514 reschedule_session_timeout (session);
2515 if (0 == delay.rel_value_us)
2517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2521 LOG(GNUNET_ERROR_TYPE_DEBUG,
2522 "Throttling receiving from `%s' for %s\n",
2523 GNUNET_i2s (&session->target),
2524 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2525 GNUNET_SERVER_disable_receive_done_warning (client);
2526 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2527 &delayed_done, session);
2533 * Functions with this signature are called whenever a peer
2534 * is disconnected on the network level.
2536 * @param cls closure
2537 * @param client identification of the client
2540 disconnect_notify (void *cls,
2541 struct GNUNET_SERVER_Client *client)
2543 struct Plugin *plugin = cls;
2544 struct Session *session;
2548 session = lookup_session_by_client (plugin, client);
2549 if (NULL == session)
2550 return; /* unknown, nothing to do */
2551 LOG (GNUNET_ERROR_TYPE_DEBUG,
2552 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2553 GNUNET_i2s (&session->target),
2554 tcp_plugin_address_to_string (session->plugin,
2555 session->address->address,
2556 session->address->address_length));
2558 if (plugin->cur_connections == plugin->max_connections)
2559 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2561 if (plugin->cur_connections < 1)
2564 plugin->cur_connections--;
2566 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2567 ("# network-level TCP disconnect events"),
2570 tcp_plugin_disconnect_session (plugin, session);
2575 * We can now send a probe message, copy into buffer to really send.
2577 * @param cls closure, a struct TCPProbeContext
2578 * @param size max size to copy
2579 * @param buf buffer to copy message to
2580 * @return number of bytes copied into @a buf
2583 notify_send_probe (void *cls,
2587 struct TCPProbeContext *tcp_probe_ctx = cls;
2588 struct Plugin *plugin = tcp_probe_ctx->plugin;
2591 tcp_probe_ctx->transmit_handle = NULL;
2592 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2597 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2598 GNUNET_free(tcp_probe_ctx);
2601 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2602 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2603 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2604 tcp_probe_ctx->sock);
2605 ret = sizeof(tcp_probe_ctx->message);
2606 GNUNET_free(tcp_probe_ctx);
2612 * Function called by the NAT subsystem suggesting another peer wants
2613 * to connect to us via connection reversal. Try to connect back to the
2616 * @param cls closure
2617 * @param addr address to try
2618 * @param addrlen number of bytes in @a addr
2621 try_connection_reversal (void *cls,
2622 const struct sockaddr *addr,
2625 struct Plugin *plugin = cls;
2626 struct GNUNET_CONNECTION_Handle *sock;
2627 struct TCPProbeContext *tcp_probe_ctx;
2630 * We have received an ICMP response, ostensibly from a peer
2631 * that wants to connect to us! Send a message to establish a connection.
2633 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2636 /* failed for some odd reason (out of sockets?); ignore attempt */
2640 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2641 tcp_probe_ctx->message.header.size
2642 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2643 tcp_probe_ctx->message.header.type
2644 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2645 tcp_probe_ctx->message.clientIdentity
2646 = *plugin->env->my_identity;
2647 tcp_probe_ctx->plugin = plugin;
2648 tcp_probe_ctx->sock = sock;
2649 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2652 tcp_probe_ctx->transmit_handle
2653 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2654 ntohs (tcp_probe_ctx->message.header.size),
2655 GNUNET_TIME_UNIT_FOREVER_REL,
2662 * Function obtain the network type for a session
2664 * @param cls closure ('struct Plugin*')
2665 * @param session the session
2666 * @return the network type in HBO or #GNUNET_SYSERR
2668 static enum GNUNET_ATS_Network_Type
2669 tcp_plugin_get_network (void *cls,
2670 struct Session *session)
2672 return session->scope;
2677 * Return information about the given session to the
2680 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2681 * @param peer peer we send information about
2682 * @param value our `struct Session` to send information about
2683 * @return #GNUNET_OK (continue to iterate)
2686 send_session_info_iter (void *cls,
2687 const struct GNUNET_PeerIdentity *peer,
2690 struct Plugin *plugin = cls;
2691 struct Session *session = value;
2693 notify_session_monitor (plugin,
2695 GNUNET_TRANSPORT_SS_INIT);
2696 /* FIXME: cannot tell if this is up or not from current
2698 notify_session_monitor (plugin,
2700 GNUNET_TRANSPORT_SS_UP);
2706 * Begin monitoring sessions of a plugin. There can only
2707 * be one active monitor per plugin (i.e. if there are
2708 * multiple monitors, the transport service needs to
2709 * multiplex the generated events over all of them).
2711 * @param cls closure of the plugin
2712 * @param sic callback to invoke, NULL to disable monitor;
2713 * plugin will being by iterating over all active
2714 * sessions immediately and then enter monitor mode
2715 * @param sic_cls closure for @a sic
2718 tcp_plugin_setup_monitor (void *cls,
2719 GNUNET_TRANSPORT_SessionInfoCallback sic,
2722 struct Plugin *plugin = cls;
2725 plugin->sic_cls = sic_cls;
2728 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2729 &send_session_info_iter,
2731 /* signal end of first iteration */
2732 sic (sic_cls, NULL, NULL);
2738 * Entry point for the plugin.
2740 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2741 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2744 libgnunet_plugin_transport_tcp_init (void *cls)
2746 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2747 { &handle_tcp_welcome, NULL,
2748 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2749 sizeof(struct WelcomeMessage) },
2750 { &handle_tcp_nat_probe, NULL,
2751 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2752 sizeof(struct TCP_NAT_ProbeMessage) },
2753 { &handle_tcp_data, NULL,
2754 GNUNET_MESSAGE_TYPE_ALL, 0 },
2755 { NULL, NULL, 0, 0 }
2757 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2758 struct GNUNET_TRANSPORT_PluginFunctions *api;
2759 struct Plugin *plugin;
2760 struct GNUNET_SERVICE_Context *service;
2761 unsigned long long aport;
2762 unsigned long long bport;
2763 unsigned long long max_connections;
2765 struct GNUNET_TIME_Relative idle_timeout;
2767 struct GNUNET_NETWORK_Handle *const*lsocks;
2771 struct sockaddr **addrs;
2772 socklen_t *addrlens;
2774 if (NULL == env->receive)
2776 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2777 initialze the plugin or the API */
2778 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2780 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2781 api->address_to_string = &tcp_plugin_address_to_string;
2782 api->string_to_address = &tcp_plugin_string_to_address;
2786 GNUNET_assert (NULL != env->cfg);
2788 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2792 max_connections = 128;
2796 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2800 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2801 "ADVERTISED-PORT", &aport)) &&
2804 LOG(GNUNET_ERROR_TYPE_ERROR,
2805 _("Require valid port number for service `%s' in configuration!\n"),
2815 service = GNUNET_SERVICE_start ("transport-tcp",
2817 GNUNET_SERVICE_OPTION_NONE);
2818 if (NULL == service)
2820 LOG (GNUNET_ERROR_TYPE_WARNING,
2821 _("Failed to start service.\n"));
2829 plugin = GNUNET_new (struct Plugin);
2830 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2832 plugin->max_connections = max_connections;
2833 plugin->open_port = bport;
2834 plugin->adv_port = aport;
2836 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2837 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2838 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2840 if ( (NULL != service) &&
2842 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2847 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2848 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2851 uint32_t len = sizeof (struct WelcomeMessage);
2853 for (i=0;NULL!=lsocks[i];i++)
2856 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2860 sizeof (struct GNUNET_PeerIdentity))) ||
2862 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2864 TCP_STEALTH_INTEGRITY_LEN,
2868 /* TCP STEALTH not supported by kernel */
2869 GNUNET_assert (0 == i);
2870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2871 _("TCP_STEALTH not supported on this platform.\n"));
2877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2878 _("TCP_STEALTH not supported on this platform.\n"));
2883 if ( (NULL != service) &&
2886 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2891 for (ret = ret_s-1; ret >= 0; ret--)
2892 LOG (GNUNET_ERROR_TYPE_INFO,
2893 "Binding to address `%s'\n",
2894 GNUNET_a2s (addrs[ret], addrlens[ret]));
2896 = GNUNET_NAT_register (env->cfg,
2899 (unsigned int) ret_s,
2900 (const struct sockaddr **) addrs, addrlens,
2901 &tcp_nat_port_map_callback,
2902 &try_connection_reversal,
2904 for (ret = ret_s -1; ret >= 0; ret--)
2905 GNUNET_free (addrs[ret]);
2906 GNUNET_free_non_null (addrs);
2907 GNUNET_free_non_null (addrlens);
2911 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2918 &try_connection_reversal,
2921 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2923 api->send = &tcp_plugin_send;
2924 api->get_session = &tcp_plugin_get_session;
2925 api->disconnect_session = &tcp_plugin_disconnect_session;
2926 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2927 api->disconnect_peer = &tcp_plugin_disconnect;
2928 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2929 api->check_address = &tcp_plugin_check_address;
2930 api->address_to_string = &tcp_plugin_address_to_string;
2931 api->string_to_address = &tcp_plugin_string_to_address;
2932 api->get_network = &tcp_plugin_get_network;
2933 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2934 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2935 api->setup_monitor = &tcp_plugin_setup_monitor;
2936 plugin->service = service;
2937 if (NULL != service)
2939 plugin->server = GNUNET_SERVICE_get_server (service);
2944 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2949 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2955 = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
2957 idle_timeout, GNUNET_YES);
2959 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2960 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2961 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2962 plugin->handlers[i].callback_cls = plugin;
2964 GNUNET_SERVER_add_handlers (plugin->server,
2966 GNUNET_SERVER_disconnect_notify (plugin->server,
2969 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2972 LOG (GNUNET_ERROR_TYPE_INFO,
2973 _("TCP transport listening on port %llu\n"),
2976 LOG (GNUNET_ERROR_TYPE_INFO,
2977 _("TCP transport not listening on any port (client only)\n"));
2978 if ( (aport != bport) &&
2980 LOG (GNUNET_ERROR_TYPE_INFO,
2981 _("TCP transport advertises itself as being on port %llu\n"),
2983 /* Initially set connections to 0 */
2984 GNUNET_assert(NULL != plugin->env->stats);
2985 GNUNET_STATISTICS_set (plugin->env->stats,
2986 gettext_noop ("# TCP sessions active"),
2992 if (NULL != plugin->nat)
2993 GNUNET_NAT_unregister (plugin->nat);
2994 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2995 if (NULL != service)
2996 GNUNET_SERVICE_stop (service);
2997 GNUNET_free (plugin);
2998 GNUNET_free_non_null (api);
3004 * Exit point from the plugin.
3006 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
3010 libgnunet_plugin_transport_tcp_done (void *cls)
3012 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3013 struct Plugin *plugin = api->cls;
3014 struct TCPProbeContext *tcp_probe;
3015 struct PrettyPrinterContext *cur;
3016 struct PrettyPrinterContext *next;
3023 LOG (GNUNET_ERROR_TYPE_DEBUG,
3024 "Shutting down TCP plugin\n");
3026 /* Removing leftover sessions */
3027 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
3028 &session_disconnect_it,
3030 /* Removing leftover NAT sessions */
3031 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
3032 &session_disconnect_it,
3035 for (cur = ppc_dll_head; NULL != cur; cur = next)
3038 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
3041 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3042 cur->asc (cur->asc_cls, NULL, GNUNET_OK);
3046 if (NULL != plugin->service)
3047 GNUNET_SERVICE_stop (plugin->service);
3049 GNUNET_SERVER_destroy (plugin->server);
3050 GNUNET_free(plugin->handlers);
3051 if (NULL != plugin->nat)
3052 GNUNET_NAT_unregister (plugin->nat);
3053 while (NULL != (tcp_probe = plugin->probe_head))
3055 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3058 GNUNET_CONNECTION_destroy (tcp_probe->sock);
3059 GNUNET_free(tcp_probe);
3061 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3062 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3063 GNUNET_free(plugin);
3068 /* end of plugin_transport_tcp.c */