2 This file is part of GNUnet
3 (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 GNUNET_SCHEDULER_TaskIdentifier 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 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
302 * Session timeout task
304 GNUNET_SCHEDULER_TaskIdentifier 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 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
336 int expecting_welcome;
339 * Was this session created using NAT traversal?
344 * ATS network type in NBO
346 enum GNUNET_ATS_Network_Type ats_address_network_type;
350 * Encapsulation of all of the state of the plugin.
357 struct GNUNET_TRANSPORT_PluginEnvironment *env;
362 struct GNUNET_CONNECTION_Handle *lsock;
365 * Our handle to the NAT module.
367 struct GNUNET_NAT_Handle *nat;
370 * Map from peer identities to sessions for the given peer.
372 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
375 * Handle to the network service.
377 struct GNUNET_SERVICE_Context *service;
380 * Handle to the server for this service.
382 struct GNUNET_SERVER_Handle *server;
385 * Copy of the handler array where the closures are
386 * set to this struct's instance.
388 struct GNUNET_SERVER_MessageHandler *handlers;
391 * Map of peers we have tried to contact behind a NAT
393 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
396 * List of active TCP probes.
398 struct TCPProbeContext *probe_head;
401 * List of active TCP probes.
403 struct TCPProbeContext *probe_tail;
406 * Handle for (DYN)DNS lookup of our external IP.
408 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
411 * Function to call about session status changes.
413 GNUNET_TRANSPORT_SessionInfoCallback sic;
416 * Closure for @e sic.
421 * Welcome message used by this peer.
423 struct WelcomeMessage my_welcome;
426 * How many more TCP sessions are we allowed to open right now?
428 unsigned long long max_connections;
431 * How many more TCP sessions do we have right now?
433 unsigned long long cur_connections;
436 * ID of task used to update our addresses when one expires.
438 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
446 * Port that we are actually listening on.
451 * Port that the user said we would have visible to the
460 * If a session monitor is attached, notify it about the new
463 * @param plugin our plugin
464 * @param session session that changed state
465 * @param state new state of the session
468 notify_session_monitor (struct Plugin *plugin,
469 struct Session *session,
470 enum GNUNET_TRANSPORT_SessionState state)
472 struct GNUNET_TRANSPORT_SessionInfo info;
474 if (NULL == plugin->sic)
476 memset (&info, 0, sizeof (info));
478 info.is_inbound = GNUNET_SYSERR; /* hard to say */
479 info.num_msg_pending = session->msgs_in_queue;
480 info.num_bytes_pending = session->bytes_in_queue;
481 if (GNUNET_SCHEDULER_NO_TASK != session->receive_delay_task)
482 info.receive_delay = session->receive_delay;
483 info.session_timeout = session->timeout;
484 info.address = session->address;
485 plugin->sic (plugin->sic_cls,
492 * Function called for a quick conversion of the binary address to
493 * a numeric address. Note that the caller must not free the
494 * address and that the next call to this function is allowed
495 * to override the address again.
497 * @param cls closure ('struct Plugin*')
498 * @param addr binary address
499 * @param addrlen length of the address
500 * @return string representing the same address
503 tcp_plugin_address_to_string (void *cls,
509 * Function to check if an inbound connection is acceptable.
510 * Mostly used to limit the total number of open connections
513 * @param cls the `struct Plugin`
514 * @param ucred credentials, if available, otherwise NULL
515 * @param addr address
516 * @param addrlen length of address
517 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
518 * for unknown address family (will be denied).
521 plugin_tcp_access_check (void *cls,
522 const struct GNUNET_CONNECTION_Credentials *ucred,
523 const struct sockaddr *addr,
526 struct Plugin *plugin = cls;
528 LOG (GNUNET_ERROR_TYPE_DEBUG,
529 "Accepting new incoming TCP connection from `%s'\n",
530 GNUNET_a2s (addr, addrlen));
531 if (plugin->cur_connections >= plugin->max_connections)
533 plugin->cur_connections++;
539 * Our external IP address/port mapping has changed.
541 * @param cls closure, the `struct Plugin`
542 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
543 * the previous (now invalid) one
544 * @param addr either the previous or the new public IP address
545 * @param addrlen actual length of @a addr
548 tcp_nat_port_map_callback (void *cls,
550 const struct sockaddr *addr,
553 struct Plugin *plugin = cls;
554 struct GNUNET_HELLO_Address *address;
555 struct IPv4TcpAddress t4;
556 struct IPv6TcpAddress t6;
560 LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
561 (GNUNET_YES == add_remove) ? "add" : "remove",
562 GNUNET_a2s (addr, addrlen));
563 /* convert 'addr' to our internal format */
564 switch (addr->sa_family)
567 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
568 memset (&t4, 0, sizeof(t4));
569 t4.options = htonl (plugin->myoptions);
570 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
571 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
576 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
577 memset (&t6, 0, sizeof(t6));
578 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
579 sizeof(struct in6_addr));
580 t6.options = htonl (plugin->myoptions);
581 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
589 /* modify our published address list */
590 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
591 (args == sizeof (struct IPv6TcpAddress)));
592 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
593 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
594 plugin->env->notify_address (plugin->env->cls, add_remove, address);
595 GNUNET_HELLO_address_free(address);
600 * Function called for a quick conversion of the binary address to
601 * a numeric address. Note that the caller must not free the
602 * address and that the next call to this function is allowed
603 * to override the address again.
605 * @param cls closure (`struct Plugin*`)
606 * @param addr binary address
607 * @param addrlen length of @a addr
608 * @return string representing the same address
611 tcp_plugin_address_to_string (void *cls,
615 static char rbuf[INET6_ADDRSTRLEN + 12];
616 char buf[INET6_ADDRSTRLEN];
620 const struct IPv4TcpAddress *t4;
621 const struct IPv6TcpAddress *t6;
628 case sizeof(struct IPv6TcpAddress):
631 port = ntohs (t6->t6_port);
632 options = ntohl (t6->options);
633 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
636 case sizeof(struct IPv4TcpAddress):
639 port = ntohs (t4->t4_port);
640 options = ntohl (t4->options);
641 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
645 LOG (GNUNET_ERROR_TYPE_WARNING,
646 _("Unexpected address length: %u bytes\n"),
647 (unsigned int) addrlen);
650 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
652 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
656 GNUNET_snprintf (rbuf, sizeof(rbuf),
657 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
667 * Function called to convert a string address to
670 * @param cls closure (`struct Plugin*`)
671 * @param addr string address
672 * @param addrlen length of the address
673 * @param buf location to store the buffer
674 * @param added location to store the number of bytes in the buffer.
675 * If the function returns #GNUNET_SYSERR, its contents are undefined.
676 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
679 tcp_plugin_string_to_address (void *cls,
685 struct sockaddr_storage socket_address;
691 /* Format tcp.options.address:port */
695 if ((NULL == addr) || (addrlen == 0))
698 return GNUNET_SYSERR;
700 if ('\0' != addr[addrlen - 1])
703 return GNUNET_SYSERR;
705 if (strlen (addr) != addrlen - 1)
708 return GNUNET_SYSERR;
710 plugin = GNUNET_strdup (addr);
711 optionstr = strchr (plugin, '.');
712 if (NULL == optionstr)
716 return GNUNET_SYSERR;
720 options = atol (optionstr);
721 address = strchr (optionstr, '.');
726 return GNUNET_SYSERR;
732 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
737 return GNUNET_SYSERR;
741 switch (socket_address.ss_family)
745 struct IPv4TcpAddress *t4;
746 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
747 t4 = GNUNET_new (struct IPv4TcpAddress);
748 t4->options = htonl (options);
749 t4->ipv4_addr = in4->sin_addr.s_addr;
750 t4->t4_port = in4->sin_port;
752 *added = sizeof(struct IPv4TcpAddress);
757 struct IPv6TcpAddress *t6;
758 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
759 t6 = GNUNET_new (struct IPv6TcpAddress);
760 t6->options = htonl (options);
761 t6->ipv6_addr = in6->sin6_addr;
762 t6->t6_port = in6->sin6_port;
764 *added = sizeof(struct IPv6TcpAddress);
768 return GNUNET_SYSERR;
774 * Find the session handle for the given client.
775 * Currently uses both the hashmap and the client
776 * context, as the client context is new and the
777 * logic still needs to be tested.
779 * @param plugin the plugin
780 * @param client which client to find the session handle for
781 * @return NULL if no matching session exists
783 static struct Session *
784 lookup_session_by_client (struct Plugin *plugin,
785 struct GNUNET_SERVER_Client *client)
787 return GNUNET_SERVER_client_get_user_context (client, struct Session);
792 * Functions with this signature are called whenever we need
793 * to close a session due to a disconnect or failure to
794 * establish a connection.
796 * @param cls the `struct Plugin`
797 * @param session session to close down
798 * @return #GNUNET_OK on success
801 tcp_plugin_disconnect_session (void *cls,
802 struct Session *session)
804 struct Plugin *plugin = cls;
805 struct PendingMessage *pm;
807 LOG (GNUNET_ERROR_TYPE_DEBUG,
808 "Disconnecting session of peer `%s' address `%s'\n",
809 GNUNET_i2s (&session->target),
810 tcp_plugin_address_to_string (NULL,
811 session->address->address,
812 session->address->address_length));
814 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
816 GNUNET_SCHEDULER_cancel (session->timeout_task);
817 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
818 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
822 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
826 GNUNET_STATISTICS_update (session->plugin->env->stats,
827 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
831 GNUNET_assert(GNUNET_YES ==
832 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
836 if (NULL != session->client)
837 GNUNET_SERVER_client_set_user_context (session->client,
841 if (NULL != session->transmit_handle)
843 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
844 session->transmit_handle = NULL;
846 plugin->env->unregister_quota_notification (plugin->env->cls,
850 session->plugin->env->session_end (session->plugin->env->cls,
854 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
856 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
857 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
860 while (NULL != (pm = session->pending_messages_head))
862 LOG (GNUNET_ERROR_TYPE_DEBUG,
863 (NULL != pm->transmit_cont)
864 ? "Could not deliver message to `%4s'.\n"
865 : "Could not deliver message to `%4s', notifying.\n",
866 GNUNET_i2s (&session->target));
867 GNUNET_STATISTICS_update (session->plugin->env->stats,
868 gettext_noop ("# bytes currently in TCP buffers"),
869 -(int64_t) pm->message_size, GNUNET_NO);
870 GNUNET_STATISTICS_update (session->plugin->env->stats,
871 gettext_noop ("# bytes discarded by TCP (disconnect)"),
874 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
875 session->pending_messages_tail,
877 GNUNET_assert (0 < session->msgs_in_queue);
878 session->msgs_in_queue--;
879 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
880 session->bytes_in_queue -= pm->message_size;
881 if (NULL != pm->transmit_cont)
882 pm->transmit_cont (pm->transmit_cont_cls,
889 GNUNET_assert (0 == session->msgs_in_queue);
890 GNUNET_assert (0 == session->bytes_in_queue);
891 notify_session_monitor (session->plugin,
893 GNUNET_TRANSPORT_SS_DONE);
895 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
897 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
898 if (NULL != session->client)
899 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
901 if (NULL != session->client)
903 GNUNET_SERVER_client_disconnect (session->client);
904 GNUNET_SERVER_client_drop (session->client);
905 session->client = NULL;
907 GNUNET_HELLO_address_free (session->address);
908 GNUNET_assert(NULL == session->transmit_handle);
909 GNUNET_free(session);
915 * Function that is called to get the keepalive factor.
916 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
917 * calculate the interval between keepalive packets.
919 * @param cls closure with the `struct Plugin`
920 * @return keepalive factor
923 tcp_plugin_query_keepalive_factor (void *cls)
930 * Session was idle, so disconnect it
932 * @param cls the `struct Session` of the idle session
933 * @param tc scheduler context
936 session_timeout (void *cls,
937 const struct GNUNET_SCHEDULER_TaskContext *tc)
939 struct Session *s = cls;
940 struct GNUNET_TIME_Relative left;
942 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
943 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
944 if (0 != left.rel_value_us)
946 /* not actually our turn yet, but let's at least update
947 the monitor, it may think we're about to die ... */
948 notify_session_monitor (s->plugin,
950 GNUNET_TRANSPORT_SS_UPDATE);
951 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
956 LOG (GNUNET_ERROR_TYPE_DEBUG,
957 "Session %p was idle for %s, disconnecting\n",
959 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
961 /* call session destroy function */
962 tcp_plugin_disconnect_session (s->plugin, s);
967 * Increment session timeout due to activity
969 * @param s session to increment timeout for
972 reschedule_session_timeout (struct Session *s)
974 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
975 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
980 * Create a new session. Also queues a welcome message.
982 * @param plugin the plugin
983 * @param address the address to create the session for
984 * @param client client to use, reference counter must have already been increased
985 * @param is_nat this a NAT session, we should wait for a client to
986 * connect to us from an address, then assign that to
988 * @return new session object
990 static struct Session *
991 create_session (struct Plugin *plugin,
992 const struct GNUNET_HELLO_Address *address,
993 struct GNUNET_SERVER_Client *client,
996 struct Session *session;
997 struct PendingMessage *pm;
999 if (GNUNET_YES != is_nat)
1000 GNUNET_assert (NULL != client);
1002 GNUNET_assert (NULL == client);
1004 LOG (GNUNET_ERROR_TYPE_DEBUG,
1005 "Creating new session for peer `%4s'\n",
1006 GNUNET_i2s (&address->peer));
1007 session = GNUNET_new (struct Session);
1008 session->last_activity = GNUNET_TIME_absolute_get ();
1009 session->plugin = plugin;
1010 session->is_nat = is_nat;
1011 session->client = client;
1012 session->address = GNUNET_HELLO_address_copy (address);
1013 session->target = address->peer;
1014 session->expecting_welcome = GNUNET_YES;
1015 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
1016 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1017 sizeof (struct WelcomeMessage));
1018 pm->msg = (const char *) &pm[1];
1019 pm->message_size = sizeof(struct WelcomeMessage);
1021 &plugin->my_welcome,
1022 sizeof(struct WelcomeMessage));
1023 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1024 GNUNET_STATISTICS_update (plugin->env->stats,
1025 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
1027 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1028 session->pending_messages_tail,
1030 session->msgs_in_queue++;
1031 session->bytes_in_queue += pm->message_size;
1032 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1033 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1036 notify_session_monitor (session->plugin,
1038 GNUNET_TRANSPORT_SS_INIT);
1039 if (GNUNET_YES != is_nat)
1041 GNUNET_STATISTICS_update (plugin->env->stats,
1042 gettext_noop ("# TCP sessions active"),
1045 notify_session_monitor (session->plugin,
1047 GNUNET_TRANSPORT_SS_UP);
1051 notify_session_monitor (session->plugin,
1053 GNUNET_TRANSPORT_SS_HANDSHAKE);
1055 plugin->env->register_quota_notification (plugin->env->cls,
1064 * If we have pending messages, ask the server to
1065 * transmit them (schedule the respective tasks, etc.)
1067 * @param session for which session should we do this
1070 process_pending_messages (struct Session *session);
1074 * Function called to notify a client about the socket
1075 * being ready to queue more data. "buf" will be
1076 * NULL and "size" zero if the socket was closed for
1077 * writing in the meantime.
1079 * @param cls closure
1080 * @param size number of bytes available in @a buf
1081 * @param buf where the callee should write the message
1082 * @return number of bytes written to @a buf
1085 do_transmit (void *cls, size_t size, void *buf)
1087 struct Session *session = cls;
1088 struct GNUNET_PeerIdentity pid;
1089 struct Plugin *plugin;
1090 struct PendingMessage *pos;
1091 struct PendingMessage *hd;
1092 struct PendingMessage *tl;
1093 struct GNUNET_TIME_Absolute now;
1097 session->transmit_handle = NULL;
1098 plugin = session->plugin;
1101 LOG (GNUNET_ERROR_TYPE_DEBUG,
1102 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1103 GNUNET_i2s (&session->target));
1104 /* timeout; cancel all messages that have already expired */
1108 now = GNUNET_TIME_absolute_get ();
1109 while ((NULL != (pos = session->pending_messages_head))
1110 && (pos->timeout.abs_value_us <= now.abs_value_us))
1112 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1113 session->pending_messages_tail,
1115 GNUNET_assert (0 < session->msgs_in_queue);
1116 session->msgs_in_queue--;
1117 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1118 session->bytes_in_queue -= pos->message_size;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG,
1120 "Failed to transmit %u byte message to `%4s'.\n",
1122 GNUNET_i2s (&session->target));
1123 ret += pos->message_size;
1124 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
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, tl, pos);
1136 if (pos->transmit_cont != NULL)
1137 pos->transmit_cont (pos->transmit_cont_cls,
1140 pos->message_size, 0);
1143 GNUNET_STATISTICS_update (plugin->env->stats,
1144 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1146 GNUNET_STATISTICS_update (plugin->env->stats,
1147 gettext_noop ("# bytes discarded by TCP (timeout)"),
1151 notify_session_monitor (session->plugin,
1153 GNUNET_TRANSPORT_SS_UPDATE);
1156 /* copy all pending messages that would fit */
1161 while (NULL != (pos = session->pending_messages_head))
1163 if (ret + pos->message_size > size)
1165 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1166 session->pending_messages_tail,
1168 GNUNET_assert (0 < session->msgs_in_queue);
1169 session->msgs_in_queue--;
1170 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1171 session->bytes_in_queue -= pos->message_size;
1172 GNUNET_assert(size >= pos->message_size);
1173 LOG(GNUNET_ERROR_TYPE_DEBUG,
1174 "Transmitting message of type %u size %u\n",
1175 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1177 /* FIXME: this memcpy can be up to 7% of our total runtime */
1178 memcpy (cbuf, pos->msg, pos->message_size);
1179 cbuf += pos->message_size;
1180 ret += pos->message_size;
1181 size -= pos->message_size;
1182 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
1184 notify_session_monitor (session->plugin,
1186 GNUNET_TRANSPORT_SS_UPDATE);
1187 /* schedule 'continuation' before callbacks so that callbacks that
1188 * cancel everything don't cause us to use a session that no longer
1190 process_pending_messages (session);
1191 session->last_activity = GNUNET_TIME_absolute_get ();
1192 pid = session->target;
1193 /* we'll now call callbacks that may cancel the session; hence
1194 * we should not use 'session' after this point */
1195 while (NULL != (pos = hd))
1197 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1198 if (pos->transmit_cont != NULL)
1199 pos->transmit_cont (pos->transmit_cont_cls,
1203 pos->message_size); /* FIXME: include TCP overhead */
1206 GNUNET_assert (NULL == hd);
1207 GNUNET_assert (NULL == tl);
1208 LOG(GNUNET_ERROR_TYPE_DEBUG,
1209 "Transmitting %u bytes\n",
1211 GNUNET_STATISTICS_update (plugin->env->stats,
1212 gettext_noop ("# bytes currently in TCP buffers"),
1215 GNUNET_STATISTICS_update (plugin->env->stats,
1216 gettext_noop ("# bytes transmitted via TCP"),
1224 * If we have pending messages, ask the server to
1225 * transmit them (schedule the respective tasks, etc.)
1227 * @param session for which session should we do this
1230 process_pending_messages (struct Session *session)
1232 struct PendingMessage *pm;
1234 GNUNET_assert (NULL != session->client);
1235 if (NULL != session->transmit_handle)
1237 if (NULL == (pm = session->pending_messages_head))
1240 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1242 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1249 * Function that can be used by the transport service to transmit
1250 * a message using the plugin. Note that in the case of a
1251 * peer disconnecting, the continuation MUST be called
1252 * prior to the disconnect notification itself. This function
1253 * will be called with this peer's HELLO message to initiate
1254 * a fresh connection to another peer.
1256 * @param cls closure
1257 * @param session which session must be used
1258 * @param msgbuf the message to transmit
1259 * @param msgbuf_size number of bytes in 'msgbuf'
1260 * @param priority how important is the message (most plugins will
1261 * ignore message priority and just FIFO)
1262 * @param to how long to wait at most for the transmission (does not
1263 * require plugins to discard the message after the timeout,
1264 * just advisory for the desired delay; most plugins will ignore
1266 * @param cont continuation to call once the message has
1267 * been transmitted (or if the transport is ready
1268 * for the next transmission call; or if the
1269 * peer disconnected...); can be NULL
1270 * @param cont_cls closure for @a cont
1271 * @return number of bytes used (on the physical network, with overheads);
1272 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1273 * and does NOT mean that the message was not transmitted (DV)
1276 tcp_plugin_send (void *cls,
1277 struct Session *session,
1280 unsigned int priority,
1281 struct GNUNET_TIME_Relative to,
1282 GNUNET_TRANSPORT_TransmitContinuation cont,
1285 struct Plugin * plugin = cls;
1286 struct PendingMessage *pm;
1288 /* create new message entry */
1289 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1290 pm->msg = (const char *) &pm[1];
1291 memcpy (&pm[1], msgbuf, msgbuf_size);
1292 pm->message_size = msgbuf_size;
1293 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1294 pm->transmit_cont = cont;
1295 pm->transmit_cont_cls = cont_cls;
1297 LOG(GNUNET_ERROR_TYPE_DEBUG,
1298 "Asked to transmit %u bytes to `%s', added message to list.\n",
1299 msgbuf_size, GNUNET_i2s (&session->target));
1302 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1306 GNUNET_assert (NULL != session->client);
1307 GNUNET_SERVER_client_set_timeout (session->client,
1308 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1309 GNUNET_STATISTICS_update (plugin->env->stats,
1310 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1313 /* append pm to pending_messages list */
1314 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1315 session->pending_messages_tail,
1317 notify_session_monitor (session->plugin,
1319 GNUNET_TRANSPORT_SS_UPDATE);
1320 session->msgs_in_queue++;
1321 session->bytes_in_queue += pm->message_size;
1322 process_pending_messages (session);
1325 else if (GNUNET_YES ==
1326 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1330 LOG (GNUNET_ERROR_TYPE_DEBUG,
1331 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1332 GNUNET_i2s (&session->target));
1333 GNUNET_STATISTICS_update (plugin->env->stats,
1334 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1336 /* append pm to pending_messages list */
1337 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1338 session->pending_messages_tail,
1340 session->msgs_in_queue++;
1341 session->bytes_in_queue += pm->message_size;
1342 notify_session_monitor (session->plugin,
1344 GNUNET_TRANSPORT_SS_HANDSHAKE);
1349 LOG(GNUNET_ERROR_TYPE_ERROR,
1350 "Invalid session %p\n",
1360 return GNUNET_SYSERR; /* session does not exist here */
1365 * Closure for #session_lookup_it().
1370 * Address we are looking for.
1372 const struct GNUNET_HELLO_Address *address;
1375 * Where to store the session (if we found it).
1377 struct Session *result;
1383 * Look for a session by address.
1385 * @param cls the `struct SessionItCtx`
1387 * @param value a `struct Session`
1388 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1391 session_lookup_it (void *cls,
1392 const struct GNUNET_PeerIdentity *key,
1395 struct SessionItCtx * si_ctx = cls;
1396 struct Session * session = value;
1398 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1400 /* Found existing session */
1401 si_ctx->result = session;
1407 * Task cleaning up a NAT connection attempt after timeout
1409 * @param cls the `struct Session`
1410 * @param tc scheduler context (unused)
1413 nat_connect_timeout (void *cls,
1414 const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct Session *session = cls;
1418 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1419 LOG (GNUNET_ERROR_TYPE_DEBUG,
1420 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1421 GNUNET_i2s (&session->target),
1422 tcp_plugin_address_to_string (NULL,
1423 session->address->address,
1424 session->address->address_length));
1425 tcp_plugin_disconnect_session (session->plugin,
1431 * Function that will be called whenever the transport service wants to
1432 * notify the plugin that a session is still active and in use and
1433 * therefore the session timeout for this session has to be updated
1435 * @param cls closure
1436 * @param peer which peer was the session for
1437 * @param session which session is being updated
1440 tcp_plugin_update_session_timeout (void *cls,
1441 const struct GNUNET_PeerIdentity *peer,
1442 struct Session *session)
1444 reschedule_session_timeout (session);
1449 * Task to signal the server that we can continue
1450 * receiving from the TCP client now.
1452 * @param cls the `struct Session*`
1453 * @param tc task context (unused)
1456 delayed_done (void *cls,
1457 const struct GNUNET_SCHEDULER_TaskContext *tc)
1459 struct Session *session = cls;
1461 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1462 reschedule_session_timeout (session);
1464 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1469 * Function that will be called whenever the transport service wants to
1470 * notify the plugin that the inbound quota changed and that the plugin
1471 * should update it's delay for the next receive value
1473 * @param cls closure
1474 * @param peer which peer was the session for
1475 * @param session which session is being updated
1476 * @param delay new delay to use for receiving
1479 tcp_plugin_update_inbound_delay (void *cls,
1480 const struct GNUNET_PeerIdentity *peer,
1481 struct Session *session,
1482 struct GNUNET_TIME_Relative delay)
1484 if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
1486 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 "New inbound delay %s\n",
1488 GNUNET_STRINGS_relative_time_to_string (delay,
1490 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1491 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1492 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1499 * Create a new session to transmit data to the target
1500 * This session will used to send data to this peer and the plugin will
1501 * notify us by calling the env->session_end function
1503 * @param cls closure
1504 * @param address the address to use
1505 * @return the session if the address is valid, NULL otherwise
1507 static struct Session *
1508 tcp_plugin_get_session (void *cls,
1509 const struct GNUNET_HELLO_Address *address)
1511 struct Plugin *plugin = cls;
1512 struct Session *session = NULL;
1516 struct GNUNET_CONNECTION_Handle *sa;
1517 struct sockaddr_in a4;
1518 struct sockaddr_in6 a6;
1519 const struct IPv4TcpAddress *t4;
1520 const struct IPv6TcpAddress *t6;
1521 unsigned int options;
1522 struct GNUNET_ATS_Information ats;
1523 unsigned int is_natd = GNUNET_NO;
1525 #ifdef SO_TCPSTEALTH
1526 struct GNUNET_NETWORK_Handle *s;
1529 addrlen = address->address_length;
1530 LOG(GNUNET_ERROR_TYPE_DEBUG,
1531 "Trying to get session for `%s' address of peer `%s'\n",
1532 tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1533 GNUNET_i2s (&address->peer));
1535 if (GNUNET_HELLO_address_check_option(address, GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1541 /* look for existing session */
1542 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1545 struct SessionItCtx si_ctx;
1547 si_ctx.address = address;
1548 si_ctx.result = NULL;
1550 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1551 &address->peer, &session_lookup_it, &si_ctx);
1552 if (si_ctx.result != NULL)
1554 session = si_ctx.result;
1555 LOG(GNUNET_ERROR_TYPE_DEBUG,
1556 "Found existing session for `%s' address `%s' session %p\n",
1557 GNUNET_i2s (&address->peer),
1558 tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1562 LOG (GNUNET_ERROR_TYPE_DEBUG,
1563 "Existing sessions did not match address `%s' or peer `%s'\n",
1564 tcp_plugin_address_to_string (NULL,
1566 address->address_length),
1567 GNUNET_i2s (&address->peer));
1570 if (addrlen == sizeof(struct IPv6TcpAddress))
1572 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1573 t6 = address->address;
1574 options = t6->options;
1576 memset (&a6, 0, sizeof(a6));
1577 #if HAVE_SOCKADDR_IN_SIN_LEN
1578 a6.sin6_len = sizeof (a6);
1580 a6.sin6_family = AF_INET6;
1581 a6.sin6_port = t6->t6_port;
1582 if (t6->t6_port == 0)
1583 is_natd = GNUNET_YES;
1584 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1588 else if (addrlen == sizeof(struct IPv4TcpAddress))
1590 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1591 t4 = address->address;
1592 options = t4->options;
1594 memset (&a4, 0, sizeof(a4));
1595 #if HAVE_SOCKADDR_IN_SIN_LEN
1596 a4.sin_len = sizeof (a4);
1598 a4.sin_family = AF_INET;
1599 a4.sin_port = t4->t4_port;
1600 if (t4->t4_port == 0)
1601 is_natd = GNUNET_YES;
1602 a4.sin_addr.s_addr = t4->ipv4_addr;
1608 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1609 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1613 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1615 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1617 /* NAT client only works with IPv4 addresses */
1621 if (plugin->cur_connections >= plugin->max_connections)
1627 if ((is_natd == GNUNET_YES)
1629 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1632 /* Only do one NAT punch attempt per peer identity */
1636 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1637 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1640 LOG (GNUNET_ERROR_TYPE_DEBUG,
1641 "Found valid IPv4 NAT address (creating session)!\n");
1642 session = create_session (plugin,
1646 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
1647 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1648 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1649 &nat_connect_timeout,
1651 GNUNET_assert(GNUNET_OK ==
1652 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1655 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1657 LOG (GNUNET_ERROR_TYPE_DEBUG,
1658 "Created NAT WAIT connection to `%4s' at `%s'\n",
1659 GNUNET_i2s (&session->target),
1660 GNUNET_a2s (sb, sbs));
1661 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1667 LOG(GNUNET_ERROR_TYPE_DEBUG,
1668 "Running NAT client for `%4s' at `%s' failed\n",
1669 GNUNET_i2s (&session->target),
1670 GNUNET_a2s (sb, sbs));
1671 tcp_plugin_disconnect_session (plugin,
1677 /* create new outbound session */
1678 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1680 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1682 #ifdef SO_TCPSTEALTH
1683 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1686 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1693 GNUNET_NETWORK_socket_setsockopt (s,
1697 sizeof (struct GNUNET_PeerIdentity)))
1699 /* TCP STEALTH not supported by kernel */
1700 GNUNET_break (GNUNET_OK ==
1701 GNUNET_NETWORK_socket_close (s));
1706 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1715 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1719 LOG (GNUNET_ERROR_TYPE_DEBUG,
1720 "Failed to create connection to `%4s' at `%s'\n",
1721 GNUNET_i2s (&address->peer),
1722 GNUNET_a2s (sb, sbs));
1725 plugin->cur_connections++;
1726 if (plugin->cur_connections == plugin->max_connections)
1727 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1729 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1731 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1733 session = create_session (plugin,
1735 GNUNET_SERVER_connect_socket (plugin->server, sa),
1737 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
1738 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1739 GNUNET_SERVER_client_set_user_context(session->client, session);
1740 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1743 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1744 LOG (GNUNET_ERROR_TYPE_DEBUG,
1745 "Creating new session for `%s' address `%s' session %p\n",
1746 GNUNET_i2s (&address->peer),
1747 tcp_plugin_address_to_string (NULL,
1749 address->address_length),
1751 /* Send TCP Welcome */
1752 process_pending_messages (session);
1759 session_disconnect_it (void *cls,
1760 const struct GNUNET_PeerIdentity *key,
1763 struct Plugin *plugin = cls;
1764 struct Session *session = value;
1766 GNUNET_STATISTICS_update (session->plugin->env->stats,
1767 gettext_noop ("# transport-service disconnect requests for TCP"),
1770 tcp_plugin_disconnect_session (plugin,
1777 * Function that can be called to force a disconnect from the
1778 * specified neighbour. This should also cancel all previously
1779 * scheduled transmissions. Obviously the transmission may have been
1780 * partially completed already, which is OK. The plugin is supposed
1781 * to close the connection (if applicable) and no longer call the
1782 * transmit continuation(s).
1784 * Finally, plugin MUST NOT call the services's receive function to
1785 * notify the service that the connection to the specified target was
1786 * closed after a getting this call.
1788 * @param cls closure
1789 * @param target peer for which the last transmission is
1793 tcp_plugin_disconnect (void *cls,
1794 const struct GNUNET_PeerIdentity *target)
1796 struct Plugin *plugin = cls;
1798 LOG(GNUNET_ERROR_TYPE_DEBUG,
1799 "Disconnecting peer `%4s'\n",
1800 GNUNET_i2s (target));
1801 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1803 &session_disconnect_it,
1805 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1807 &session_disconnect_it,
1813 * Running pretty printers: head
1815 static struct PrettyPrinterContext *ppc_dll_head;
1818 * Running pretty printers: tail
1820 static struct PrettyPrinterContext *ppc_dll_tail;
1823 * Context for address to string conversion, closure
1824 * for #append_port().
1826 struct PrettyPrinterContext
1831 struct PrettyPrinterContext *next;
1836 struct PrettyPrinterContext *prev;
1841 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1846 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1849 * Function to call with the result.
1851 GNUNET_TRANSPORT_AddressStringCallback asc;
1854 * Clsoure for @e asc.
1859 * Port to add after the IP address.
1876 * Append our port and forward the result.
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;
1888 if (NULL == hostname)
1890 /* Final call, done */
1891 ppc->resolver_handle = NULL;
1892 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
1895 ppc->asc (ppc->asc_cls,
1901 if (GNUNET_YES == ppc->ipv6)
1902 GNUNET_asprintf (&ret,
1909 GNUNET_asprintf (&ret,
1915 ppc->asc (ppc->asc_cls,
1923 * Convert the transports address to a nice, human-readable
1926 * @param cls closure
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 PrettyPrinterContext *ppc;
1949 struct sockaddr_in a4;
1950 struct sockaddr_in6 a6;
1951 const struct IPv4TcpAddress *t4;
1952 const struct IPv6TcpAddress *t6;
1956 if (sizeof(struct IPv6TcpAddress) == addrlen)
1959 memset (&a6, 0, sizeof(a6));
1960 a6.sin6_family = AF_INET6;
1961 a6.sin6_port = t6->t6_port;
1962 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1963 port = ntohs (t6->t6_port);
1964 options = ntohl (t6->options);
1968 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1971 memset (&a4, 0, sizeof(a4));
1972 a4.sin_family = AF_INET;
1973 a4.sin_port = t4->t4_port;
1974 a4.sin_addr.s_addr = t4->ipv4_addr;
1975 port = ntohs (t4->t4_port);
1976 options = ntohl (t4->options);
1982 /* invalid address */
1983 asc (asc_cls, NULL, GNUNET_SYSERR);
1984 asc (asc_cls, NULL, GNUNET_OK);
1987 ppc = GNUNET_new (struct PrettyPrinterContext);
1988 if (addrlen == sizeof(struct IPv6TcpAddress))
1989 ppc->ipv6 = GNUNET_YES;
1991 ppc->ipv6 = GNUNET_NO;
1993 ppc->asc_cls = asc_cls;
1995 ppc->options = options;
1996 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2001 if (NULL == ppc->resolver_handle)
2007 GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
2014 * Check if the given port is plausible (must be either our listen
2015 * port or our advertised port), or any port if we are behind NAT
2016 * and do not have a port open. If it is neither, we return
2019 * @param plugin global variables
2020 * @param in_port port number to check
2021 * @return #GNUNET_OK if port is either open_port or adv_port
2024 check_port (struct Plugin *plugin, uint16_t in_port)
2026 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
2028 return GNUNET_SYSERR;
2032 * Function that will be called to check if a binary address for this
2033 * plugin is well-formed and corresponds to an address for THIS peer
2034 * (as per our configuration). Naturally, if absolutely necessary,
2035 * plugins can be a bit conservative in their answer, but in general
2036 * plugins should make sure that the address does not redirect
2037 * traffic to a 3rd party that might try to man-in-the-middle our
2040 * @param cls closure, our `struct Plugin *`
2041 * @param addr pointer to the address
2042 * @param addrlen length of addr
2043 * @return #GNUNET_OK if this is a plausible address for this peer
2044 * and transport, #GNUNET_SYSERR if not
2047 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
2049 struct Plugin *plugin = cls;
2050 struct IPv4TcpAddress *v4;
2051 struct IPv6TcpAddress *v6;
2053 if ((addrlen != sizeof(struct IPv4TcpAddress))
2054 && (addrlen != sizeof(struct IPv6TcpAddress)))
2057 return GNUNET_SYSERR;
2060 if (addrlen == sizeof(struct IPv4TcpAddress))
2062 v4 = (struct IPv4TcpAddress *) addr;
2063 if (0 != memcmp (&v4->options,
2068 return GNUNET_SYSERR;
2070 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
2071 return GNUNET_SYSERR;
2073 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
2074 sizeof(struct in_addr)))
2075 return GNUNET_SYSERR;
2079 v6 = (struct IPv6TcpAddress *) addr;
2080 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2083 return GNUNET_SYSERR;
2085 if (0 != memcmp (&v6->options,
2090 return GNUNET_SYSERR;
2092 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
2093 return GNUNET_SYSERR;
2095 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
2096 sizeof(struct in6_addr)))
2097 return GNUNET_SYSERR;
2103 * We've received a nat probe from this peer via TCP. Finish
2104 * creating the client session and resume sending of queued
2107 * @param cls closure
2108 * @param client identification of the client
2109 * @param message the actual message
2112 handle_tcp_nat_probe (void *cls,
2113 struct GNUNET_SERVER_Client *client,
2114 const struct GNUNET_MessageHeader *message)
2116 struct Plugin *plugin = cls;
2117 struct Session *session;
2118 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2121 struct IPv4TcpAddress *t4;
2122 struct IPv6TcpAddress *t6;
2123 const struct sockaddr_in *s4;
2124 const struct sockaddr_in6 *s6;
2126 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
2127 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2128 * a connection to this peer by running gnunet-nat-client. This peer
2129 * received the punch message and now wants us to use the new connection
2130 * as the default for that peer. Do so and then send a WELCOME message
2131 * so we can really be connected!
2133 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2140 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2141 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2142 sizeof(struct GNUNET_PeerIdentity)))
2144 /* refuse connections from ourselves */
2145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2149 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2150 &tcp_nat_probe->clientIdentity);
2151 if (session == NULL)
2153 LOG (GNUNET_ERROR_TYPE_DEBUG,
2154 "Did NOT find session for NAT probe!\n");
2155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2158 LOG (GNUNET_ERROR_TYPE_DEBUG,
2159 "Found session for NAT probe!\n");
2161 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
2163 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2164 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
2167 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2171 tcp_plugin_disconnect_session (plugin,
2176 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2177 GNUNET_SERVER_client_set_user_context(client, session);
2178 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2179 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2180 session->last_activity = GNUNET_TIME_absolute_get ();
2181 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
2182 GNUNET_a2s (vaddr, alen));
2183 switch (((const struct sockaddr *) vaddr)->sa_family)
2187 t4 = GNUNET_new (struct IPv4TcpAddress);
2188 t4->options = htonl (TCP_OPTIONS_NONE);
2189 t4->t4_port = s4->sin_port;
2190 t4->ipv4_addr = s4->sin_addr.s_addr;
2191 session->address = GNUNET_HELLO_address_allocate (
2192 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
2193 sizeof(struct IPv4TcpAddress),
2194 GNUNET_HELLO_ADDRESS_INFO_NONE);
2198 t6 = GNUNET_new (struct IPv6TcpAddress);
2199 t6->options = htonl (TCP_OPTIONS_NONE);
2200 t6->t6_port = s6->sin6_port;
2201 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2202 session->address = GNUNET_HELLO_address_allocate (
2203 &tcp_nat_probe->clientIdentity,
2204 PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
2205 GNUNET_HELLO_ADDRESS_INFO_NONE);
2209 LOG(GNUNET_ERROR_TYPE_DEBUG,
2210 "Bad address for incoming connection!\n");
2212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2213 tcp_plugin_disconnect_session (plugin,
2218 GNUNET_break(NULL == session->client);
2219 GNUNET_SERVER_client_keep (client);
2220 session->client = client;
2221 GNUNET_STATISTICS_update (plugin->env->stats,
2222 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2223 process_pending_messages (session);
2224 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2228 * We've received a welcome from this peer via TCP. Possibly create a
2229 * fresh client record and send back our welcome.
2231 * @param cls closure
2232 * @param client identification of the client
2233 * @param message the actual message
2236 handle_tcp_welcome (void *cls,
2237 struct GNUNET_SERVER_Client *client,
2238 const struct GNUNET_MessageHeader *message)
2240 struct Plugin *plugin = cls;
2241 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2242 struct GNUNET_HELLO_Address *address;
2243 struct Session *session;
2246 struct IPv4TcpAddress t4;
2247 struct IPv6TcpAddress t6;
2248 const struct sockaddr_in *s4;
2249 const struct sockaddr_in6 *s6;
2250 struct GNUNET_ATS_Information ats;
2253 if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
2254 sizeof(struct GNUNET_PeerIdentity)))
2256 /* refuse connections from ourselves */
2257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2258 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2260 LOG(GNUNET_ERROR_TYPE_INFO,
2261 "Received %s message from my own identity `%4s' on address `%s'\n",
2262 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2263 GNUNET_a2s (vaddr, alen));
2269 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
2270 GNUNET_i2s (&wm->clientIdentity), client);
2271 GNUNET_STATISTICS_update (plugin->env->stats,
2272 gettext_noop ("# TCP WELCOME messages received"), 1, GNUNET_NO);
2273 session = lookup_session_by_client (plugin, client);
2274 if (NULL != session)
2276 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2278 LOG (GNUNET_ERROR_TYPE_DEBUG,
2279 "Found existing session %p for peer `%s'\n",
2280 session, GNUNET_a2s (vaddr, alen));
2286 GNUNET_SERVER_client_keep (client);
2287 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2288 plugin->cur_connections++;
2289 if (plugin->cur_connections == plugin->max_connections)
2290 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2292 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2294 if (alen == sizeof(struct sockaddr_in))
2297 memset (&t4, '\0', sizeof (t4));
2298 t4.options = htonl (TCP_OPTIONS_NONE);
2299 t4.t4_port = s4->sin_port;
2300 t4.ipv4_addr = s4->sin_addr.s_addr;
2301 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2302 PLUGIN_NAME, &t4, sizeof(t4),
2303 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2305 else if (alen == sizeof(struct sockaddr_in6))
2308 memset (&t6, '\0', sizeof (t6));
2309 t6.options = htonl (TCP_OPTIONS_NONE);
2310 t6.t6_port = s6->sin6_port;
2311 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2312 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2313 PLUGIN_NAME, &t6, sizeof (t6),
2314 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2319 GNUNET_free_non_null (vaddr);
2322 session = create_session (plugin, address, client, GNUNET_NO);
2323 GNUNET_HELLO_address_free (address);
2324 ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2325 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
2326 LOG(GNUNET_ERROR_TYPE_DEBUG,
2327 "Creating new%s session %p for peer `%s' client %p \n",
2328 GNUNET_HELLO_address_check_option (session->address,
2329 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2332 tcp_plugin_address_to_string(NULL, (void *) session->address->address,
2333 session->address->address_length),
2336 GNUNET_SERVER_client_set_user_context(session->client, session);
2337 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2341 /* Notify transport and ATS about new session */
2342 plugin->env->session_start (NULL, session->address, session, &ats, 1);
2343 notify_session_monitor (plugin,
2345 GNUNET_TRANSPORT_SS_INIT);
2349 LOG(GNUNET_ERROR_TYPE_DEBUG,
2350 "Did not obtain TCP socket address for incoming connection\n");
2356 if (session->expecting_welcome != GNUNET_YES)
2359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2363 session->last_activity = GNUNET_TIME_absolute_get ();
2364 session->expecting_welcome = GNUNET_NO;
2366 process_pending_messages (session);
2367 GNUNET_SERVER_client_set_timeout (client,
2368 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2374 * We've received data for this peer via TCP. Unbox,
2375 * compute latency and forward.
2377 * @param cls closure
2378 * @param client identification of the client
2379 * @param message the actual message
2382 handle_tcp_data (void *cls,
2383 struct GNUNET_SERVER_Client *client,
2384 const struct GNUNET_MessageHeader *message)
2386 struct Plugin *plugin = cls;
2387 struct Session *session;
2388 struct GNUNET_TIME_Relative delay;
2391 type = ntohs (message->type);
2392 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2393 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2395 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2399 session = lookup_session_by_client (plugin, client);
2400 if (NULL == session)
2402 /* No inbound session found */
2406 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2407 LOG(GNUNET_ERROR_TYPE_ERROR,
2408 "Received unexpected %u bytes of type %u from `%s'\n",
2409 (unsigned int) ntohs (message->size),
2410 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2413 GNUNET_free_non_null(vaddr);
2416 else if (GNUNET_YES == session->expecting_welcome)
2418 /* Session is expecting WELCOME message */
2422 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2423 LOG(GNUNET_ERROR_TYPE_ERROR,
2424 "Received unexpected %u bytes of type %u from `%s'\n",
2425 (unsigned int) ntohs (message->size),
2426 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2429 GNUNET_free_non_null(vaddr);
2433 session->last_activity = GNUNET_TIME_absolute_get ();
2434 LOG(GNUNET_ERROR_TYPE_DEBUG,
2435 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2436 (unsigned int) ntohs (message->size),
2437 (unsigned int) ntohs (message->type), GNUNET_i2s (&session->target));
2439 GNUNET_STATISTICS_update (plugin->env->stats,
2440 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2442 struct GNUNET_ATS_Information distance;
2444 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2445 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2446 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2448 GNUNET_assert(GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2452 delay = plugin->env->receive (plugin->env->cls,
2456 plugin->env->update_address_metrics (plugin->env->cls,
2460 reschedule_session_timeout (session);
2461 if (0 == delay.rel_value_us)
2463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2467 LOG(GNUNET_ERROR_TYPE_DEBUG,
2468 "Throttling receiving from `%s' for %s\n",
2469 GNUNET_i2s (&session->target),
2470 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2471 GNUNET_SERVER_disable_receive_done_warning (client);
2472 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2473 &delayed_done, session);
2479 * Functions with this signature are called whenever a peer
2480 * is disconnected on the network level.
2482 * @param cls closure
2483 * @param client identification of the client
2486 disconnect_notify (void *cls,
2487 struct GNUNET_SERVER_Client *client)
2489 struct Plugin *plugin = cls;
2490 struct Session *session;
2494 session = lookup_session_by_client (plugin, client);
2495 if (NULL == session)
2496 return; /* unknown, nothing to do */
2497 LOG (GNUNET_ERROR_TYPE_DEBUG,
2498 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2499 GNUNET_i2s (&session->target),
2500 tcp_plugin_address_to_string (session->plugin, session->address->address,
2501 session->address->address_length));
2503 if (plugin->cur_connections == plugin->max_connections)
2504 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2506 if (plugin->cur_connections < 1)
2509 plugin->cur_connections--;
2511 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2512 ("# network-level TCP disconnect events"),
2515 tcp_plugin_disconnect_session (plugin, session);
2520 * We can now send a probe message, copy into buffer to really send.
2522 * @param cls closure, a struct TCPProbeContext
2523 * @param size max size to copy
2524 * @param buf buffer to copy message to
2525 * @return number of bytes copied into @a buf
2528 notify_send_probe (void *cls,
2532 struct TCPProbeContext *tcp_probe_ctx = cls;
2533 struct Plugin *plugin = tcp_probe_ctx->plugin;
2536 tcp_probe_ctx->transmit_handle = NULL;
2537 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2542 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2543 GNUNET_free(tcp_probe_ctx);
2546 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2547 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2548 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2549 tcp_probe_ctx->sock);
2550 ret = sizeof(tcp_probe_ctx->message);
2551 GNUNET_free(tcp_probe_ctx);
2557 * Function called by the NAT subsystem suggesting another peer wants
2558 * to connect to us via connection reversal. Try to connect back to the
2561 * @param cls closure
2562 * @param addr address to try
2563 * @param addrlen number of bytes in @a addr
2566 try_connection_reversal (void *cls,
2567 const struct sockaddr *addr,
2570 struct Plugin *plugin = cls;
2571 struct GNUNET_CONNECTION_Handle *sock;
2572 struct TCPProbeContext *tcp_probe_ctx;
2575 * We have received an ICMP response, ostensibly from a peer
2576 * that wants to connect to us! Send a message to establish a connection.
2578 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2581 /* failed for some odd reason (out of sockets?); ignore attempt */
2585 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2586 tcp_probe_ctx->message.header.size
2587 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2588 tcp_probe_ctx->message.header.type
2589 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2590 tcp_probe_ctx->message.clientIdentity
2591 = *plugin->env->my_identity;
2592 tcp_probe_ctx->plugin = plugin;
2593 tcp_probe_ctx->sock = sock;
2594 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2597 tcp_probe_ctx->transmit_handle
2598 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2599 ntohs (tcp_probe_ctx->message.header.size),
2600 GNUNET_TIME_UNIT_FOREVER_REL,
2607 * Function obtain the network type for a session
2609 * @param cls closure ('struct Plugin*')
2610 * @param session the session
2611 * @return the network type in HBO or #GNUNET_SYSERR
2613 static enum GNUNET_ATS_Network_Type
2614 tcp_plugin_get_network (void *cls,
2615 struct Session *session)
2617 return session->ats_address_network_type;
2622 * Return information about the given session to the
2625 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2626 * @param peer peer we send information about
2627 * @param value our `struct Session` to send information about
2628 * @return #GNUNET_OK (continue to iterate)
2631 send_session_info_iter (void *cls,
2632 const struct GNUNET_PeerIdentity *peer,
2635 struct Plugin *plugin = cls;
2636 struct Session *session = value;
2638 notify_session_monitor (plugin,
2640 GNUNET_TRANSPORT_SS_INIT);
2641 /* FIXME: cannot tell if this is up or not from current
2643 notify_session_monitor (plugin,
2645 GNUNET_TRANSPORT_SS_UP);
2651 * Begin monitoring sessions of a plugin. There can only
2652 * be one active monitor per plugin (i.e. if there are
2653 * multiple monitors, the transport service needs to
2654 * multiplex the generated events over all of them).
2656 * @param cls closure of the plugin
2657 * @param sic callback to invoke, NULL to disable monitor;
2658 * plugin will being by iterating over all active
2659 * sessions immediately and then enter monitor mode
2660 * @param sic_cls closure for @a sic
2663 tcp_plugin_setup_monitor (void *cls,
2664 GNUNET_TRANSPORT_SessionInfoCallback sic,
2667 struct Plugin *plugin = cls;
2670 plugin->sic_cls = sic_cls;
2673 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2674 &send_session_info_iter,
2676 /* signal end of first iteration */
2677 sic (sic_cls, NULL, NULL);
2683 * Entry point for the plugin.
2685 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2686 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2689 libgnunet_plugin_transport_tcp_init (void *cls)
2691 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2692 { &handle_tcp_welcome, NULL,
2693 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, sizeof(struct WelcomeMessage) },
2694 { &handle_tcp_nat_probe, NULL,
2695 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof(struct TCP_NAT_ProbeMessage) },
2696 { &handle_tcp_data, NULL,
2697 GNUNET_MESSAGE_TYPE_ALL, 0 },
2698 { NULL, NULL, 0, 0 }
2700 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2701 struct GNUNET_TRANSPORT_PluginFunctions *api;
2702 struct Plugin *plugin;
2703 struct GNUNET_SERVICE_Context *service;
2704 unsigned long long aport;
2705 unsigned long long bport;
2706 unsigned long long max_connections;
2708 struct GNUNET_TIME_Relative idle_timeout;
2709 #ifdef SO_TCPSTEALTH
2710 struct GNUNET_NETWORK_Handle *const*lsocks;
2714 struct sockaddr **addrs;
2715 socklen_t *addrlens;
2717 if (NULL == env->receive)
2719 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2720 initialze the plugin or the API */
2721 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2723 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2724 api->address_to_string = &tcp_plugin_address_to_string;
2725 api->string_to_address = &tcp_plugin_string_to_address;
2729 GNUNET_assert (NULL != env->cfg);
2731 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2735 max_connections = 128;
2739 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2743 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2744 "ADVERTISED-PORT", &aport)) &&
2747 LOG(GNUNET_ERROR_TYPE_ERROR,
2748 _("Require valid port number for service `%s' in configuration!\n"),
2758 service = GNUNET_SERVICE_start ("transport-tcp",
2760 GNUNET_SERVICE_OPTION_NONE);
2761 if (NULL == service)
2763 LOG (GNUNET_ERROR_TYPE_WARNING,
2764 _("Failed to start service.\n"));
2772 plugin = GNUNET_new (struct Plugin);
2773 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2775 plugin->max_connections = max_connections;
2776 plugin->open_port = bport;
2777 plugin->adv_port = aport;
2779 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2780 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2781 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2783 if ( (NULL != service) &&
2785 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2789 #ifdef SO_TCPSTEALTH
2790 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2791 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2794 for (i=0;NULL!=lsocks[i];i++)
2797 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2801 sizeof (struct GNUNET_PeerIdentity)))
2803 /* TCP STEALTH not supported by kernel */
2804 GNUNET_assert (0 == i);
2805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2806 _("TCP_STEALTH not supported on this platform.\n"));
2812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2813 _("TCP_STEALTH not supported on this platform.\n"));
2818 if ( (NULL != service) &&
2821 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2826 for (ret = ret_s-1; ret >= 0; ret--)
2827 LOG (GNUNET_ERROR_TYPE_INFO,
2828 "Binding to address `%s'\n",
2829 GNUNET_a2s (addrs[ret], addrlens[ret]));
2831 = GNUNET_NAT_register (env->cfg,
2834 (unsigned int) ret_s,
2835 (const struct sockaddr **) addrs, addrlens,
2836 &tcp_nat_port_map_callback,
2837 &try_connection_reversal,
2839 for (ret = ret_s -1; ret >= 0; ret--)
2840 GNUNET_free (addrs[ret]);
2841 GNUNET_free_non_null (addrs);
2842 GNUNET_free_non_null (addrlens);
2846 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2847 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2848 &try_connection_reversal, plugin);
2850 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2852 api->send = &tcp_plugin_send;
2853 api->get_session = &tcp_plugin_get_session;
2854 api->disconnect_session = &tcp_plugin_disconnect_session;
2855 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2856 api->disconnect_peer = &tcp_plugin_disconnect;
2857 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2858 api->check_address = &tcp_plugin_check_address;
2859 api->address_to_string = &tcp_plugin_address_to_string;
2860 api->string_to_address = &tcp_plugin_string_to_address;
2861 api->get_network = &tcp_plugin_get_network;
2862 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2863 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2864 api->setup_monitor = &tcp_plugin_setup_monitor;
2865 plugin->service = service;
2866 if (NULL != service)
2868 plugin->server = GNUNET_SERVICE_get_server (service);
2873 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2878 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2884 = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
2886 idle_timeout, GNUNET_YES);
2888 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2889 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2890 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2891 plugin->handlers[i].callback_cls = plugin;
2893 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2894 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2895 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2898 LOG (GNUNET_ERROR_TYPE_INFO,
2899 _("TCP transport listening on port %llu\n"),
2902 LOG (GNUNET_ERROR_TYPE_INFO,
2903 _("TCP transport not listening on any port (client only)\n"));
2904 if ( (aport != bport) &&
2906 LOG (GNUNET_ERROR_TYPE_INFO,
2907 _("TCP transport advertises itself as being on port %llu\n"),
2909 /* Initially set connections to 0 */
2910 GNUNET_assert(NULL != plugin->env->stats);
2911 GNUNET_STATISTICS_set (plugin->env->stats,
2912 gettext_noop ("# TCP sessions active"),
2918 if (NULL != plugin->nat)
2919 GNUNET_NAT_unregister (plugin->nat);
2920 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2921 if (NULL != service)
2922 GNUNET_SERVICE_stop (service);
2923 GNUNET_free (plugin);
2924 GNUNET_free_non_null (api);
2930 * Exit point from the plugin.
2932 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2936 libgnunet_plugin_transport_tcp_done (void *cls)
2938 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2939 struct Plugin *plugin = api->cls;
2940 struct TCPProbeContext *tcp_probe;
2941 struct PrettyPrinterContext *cur;
2942 struct PrettyPrinterContext *next;
2949 LOG (GNUNET_ERROR_TYPE_DEBUG,
2950 "Shutting down TCP plugin\n");
2952 /* Removing leftover sessions */
2953 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2954 &session_disconnect_it,
2956 /* Removing leftover NAT sessions */
2957 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2958 &session_disconnect_it,
2961 next = ppc_dll_head;
2962 for (cur = next; NULL != cur; cur = next)
2966 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
2969 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2973 if (NULL != plugin->service)
2974 GNUNET_SERVICE_stop (plugin->service);
2976 GNUNET_SERVER_destroy (plugin->server);
2977 GNUNET_free(plugin->handlers);
2978 if (NULL != plugin->nat)
2979 GNUNET_NAT_unregister (plugin->nat);
2980 while (NULL != (tcp_probe = plugin->probe_head))
2982 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2985 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2986 GNUNET_free(tcp_probe);
2988 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2989 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2990 GNUNET_free(plugin);
2995 /* end of plugin_transport_tcp.c */