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 * 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 struct GNUNET_SCHEDULER_Task * 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 = (0 != (GNUNET_HELLO_ADDRESS_INFO_INBOUND & session->address->local_info))
481 info.num_msg_pending = session->msgs_in_queue;
482 info.num_bytes_pending = session->bytes_in_queue;
483 if (NULL != session->receive_delay_task)
484 info.receive_delay = session->receive_delay;
485 info.session_timeout = session->timeout;
486 info.address = session->address;
487 plugin->sic (plugin->sic_cls,
494 * Function called for a quick conversion of the binary address to
495 * a numeric address. Note that the caller must not free the
496 * address and that the next call to this function is allowed
497 * to override the address again.
499 * @param cls closure (`struct Plugin *`)
500 * @param addr binary address
501 * @param addrlen length of @a addr
502 * @return string representing the same address
505 tcp_plugin_address_to_string (void *cls,
511 * Function to check if an inbound connection is acceptable.
512 * Mostly used to limit the total number of open connections
515 * @param cls the `struct Plugin`
516 * @param ucred credentials, if available, otherwise NULL
517 * @param addr address
518 * @param addrlen length of @a addr
519 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
520 * for unknown address family (will be denied).
523 plugin_tcp_access_check (void *cls,
524 const struct GNUNET_CONNECTION_Credentials *ucred,
525 const struct sockaddr *addr,
528 struct Plugin *plugin = cls;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "Accepting new incoming TCP connection from `%s'\n",
532 GNUNET_a2s (addr, addrlen));
533 if (plugin->cur_connections >= plugin->max_connections)
535 plugin->cur_connections++;
541 * Our external IP address/port mapping has changed.
543 * @param cls closure, the `struct Plugin`
544 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
545 * the previous (now invalid) one
546 * @param addr either the previous or the new public IP address
547 * @param addrlen actual length of @a addr
550 tcp_nat_port_map_callback (void *cls,
552 const struct sockaddr *addr,
555 struct Plugin *plugin = cls;
556 struct GNUNET_HELLO_Address *address;
557 struct IPv4TcpAddress t4;
558 struct IPv6TcpAddress t6;
562 LOG(GNUNET_ERROR_TYPE_INFO,
563 "NAT notification to %s address `%s'\n",
564 (GNUNET_YES == add_remove) ? "add" : "remove",
565 GNUNET_a2s (addr, addrlen));
566 /* convert 'addr' to our internal format */
567 switch (addr->sa_family)
570 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
571 memset (&t4, 0, sizeof(t4));
572 t4.options = htonl (plugin->myoptions);
573 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
574 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
579 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
580 memset (&t6, 0, sizeof(t6));
581 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
582 sizeof(struct in6_addr));
583 t6.options = htonl (plugin->myoptions);
584 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
592 /* modify our published address list */
593 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
594 (args == sizeof (struct IPv6TcpAddress)));
595 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
596 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
597 plugin->env->notify_address (plugin->env->cls, add_remove, address);
598 GNUNET_HELLO_address_free(address);
603 * Function called for a quick conversion of the binary address to
604 * a numeric address. Note that the caller must not free the
605 * address and that the next call to this function is allowed
606 * to override the address again.
608 * @param cls closure (`struct Plugin*`)
609 * @param addr binary address
610 * @param addrlen length of @a addr
611 * @return string representing the same address
614 tcp_plugin_address_to_string (void *cls,
618 static char rbuf[INET6_ADDRSTRLEN + 12];
619 char buf[INET6_ADDRSTRLEN];
623 const struct IPv4TcpAddress *t4;
624 const struct IPv6TcpAddress *t6;
631 case sizeof(struct IPv6TcpAddress):
634 port = ntohs (t6->t6_port);
635 options = ntohl (t6->options);
636 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
639 case sizeof(struct IPv4TcpAddress):
642 port = ntohs (t4->t4_port);
643 options = ntohl (t4->options);
644 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
648 LOG (GNUNET_ERROR_TYPE_WARNING,
649 _("Unexpected address length: %u bytes\n"),
650 (unsigned int) addrlen);
653 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
655 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
659 GNUNET_snprintf (rbuf, sizeof(rbuf),
660 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
670 * Function called to convert a string address to
673 * @param cls closure (`struct Plugin*`)
674 * @param addr string address
675 * @param addrlen length of the address
676 * @param buf location to store the buffer
677 * @param added location to store the number of bytes in the buffer.
678 * If the function returns #GNUNET_SYSERR, its contents are undefined.
679 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
682 tcp_plugin_string_to_address (void *cls,
688 struct sockaddr_storage socket_address;
694 /* Format tcp.options.address:port */
698 if ((NULL == addr) || (addrlen == 0))
701 return GNUNET_SYSERR;
703 if ('\0' != addr[addrlen - 1])
706 return GNUNET_SYSERR;
708 if (strlen (addr) != addrlen - 1)
711 return GNUNET_SYSERR;
713 plugin = GNUNET_strdup (addr);
714 optionstr = strchr (plugin, '.');
715 if (NULL == optionstr)
719 return GNUNET_SYSERR;
723 options = atol (optionstr);
724 address = strchr (optionstr, '.');
729 return GNUNET_SYSERR;
735 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
740 return GNUNET_SYSERR;
744 switch (socket_address.ss_family)
748 struct IPv4TcpAddress *t4;
749 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
750 t4 = GNUNET_new (struct IPv4TcpAddress);
751 t4->options = htonl (options);
752 t4->ipv4_addr = in4->sin_addr.s_addr;
753 t4->t4_port = in4->sin_port;
755 *added = sizeof(struct IPv4TcpAddress);
760 struct IPv6TcpAddress *t6;
761 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
762 t6 = GNUNET_new (struct IPv6TcpAddress);
763 t6->options = htonl (options);
764 t6->ipv6_addr = in6->sin6_addr;
765 t6->t6_port = in6->sin6_port;
767 *added = sizeof(struct IPv6TcpAddress);
771 return GNUNET_SYSERR;
777 * Find the session handle for the given client.
778 * Currently uses both the hashmap and the client
779 * context, as the client context is new and the
780 * logic still needs to be tested.
782 * @param plugin the plugin
783 * @param client which client to find the session handle for
784 * @return NULL if no matching session exists
786 static struct Session *
787 lookup_session_by_client (struct Plugin *plugin,
788 struct GNUNET_SERVER_Client *client)
790 return GNUNET_SERVER_client_get_user_context (client, struct Session);
795 * Functions with this signature are called whenever we need
796 * to close a session due to a disconnect or failure to
797 * establish a connection.
799 * @param cls the `struct Plugin`
800 * @param session session to close down
801 * @return #GNUNET_OK on success
804 tcp_plugin_disconnect_session (void *cls,
805 struct Session *session)
807 struct Plugin *plugin = cls;
808 struct PendingMessage *pm;
810 LOG (GNUNET_ERROR_TYPE_DEBUG,
811 "Disconnecting session of peer `%s' address `%s'\n",
812 GNUNET_i2s (&session->target),
813 tcp_plugin_address_to_string (NULL,
814 session->address->address,
815 session->address->address_length));
817 if (NULL != session->timeout_task)
819 GNUNET_SCHEDULER_cancel (session->timeout_task);
820 session->timeout_task = NULL;
821 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
825 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
829 GNUNET_STATISTICS_update (session->plugin->env->stats,
830 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
834 GNUNET_assert(GNUNET_YES ==
835 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
839 if (NULL != session->client)
840 GNUNET_SERVER_client_set_user_context (session->client,
844 if (NULL != session->transmit_handle)
846 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
847 session->transmit_handle = NULL;
849 session->plugin->env->session_end (session->plugin->env->cls,
853 if (NULL != session->nat_connection_timeout)
855 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
856 session->nat_connection_timeout = NULL;
859 while (NULL != (pm = session->pending_messages_head))
861 LOG (GNUNET_ERROR_TYPE_DEBUG,
862 (NULL != pm->transmit_cont)
863 ? "Could not deliver message to `%4s'.\n"
864 : "Could not deliver message to `%4s', notifying.\n",
865 GNUNET_i2s (&session->target));
866 GNUNET_STATISTICS_update (session->plugin->env->stats,
867 gettext_noop ("# bytes currently in TCP buffers"),
868 -(int64_t) pm->message_size, GNUNET_NO);
869 GNUNET_STATISTICS_update (session->plugin->env->stats,
870 gettext_noop ("# bytes discarded by TCP (disconnect)"),
873 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
874 session->pending_messages_tail,
876 GNUNET_assert (0 < session->msgs_in_queue);
877 session->msgs_in_queue--;
878 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
879 session->bytes_in_queue -= pm->message_size;
880 if (NULL != pm->transmit_cont)
881 pm->transmit_cont (pm->transmit_cont_cls,
888 GNUNET_assert (0 == session->msgs_in_queue);
889 GNUNET_assert (0 == session->bytes_in_queue);
890 notify_session_monitor (session->plugin,
892 GNUNET_TRANSPORT_SS_DONE);
894 if (session->receive_delay_task != NULL)
896 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
897 if (NULL != session->client)
898 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
900 if (NULL != session->client)
902 GNUNET_SERVER_client_disconnect (session->client);
903 GNUNET_SERVER_client_drop (session->client);
904 session->client = NULL;
906 GNUNET_HELLO_address_free (session->address);
907 GNUNET_assert(NULL == session->transmit_handle);
908 GNUNET_free(session);
914 * Function that is called to get the keepalive factor.
915 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
916 * calculate the interval between keepalive packets.
918 * @param cls closure with the `struct Plugin`
919 * @return keepalive factor
922 tcp_plugin_query_keepalive_factor (void *cls)
929 * Session was idle, so disconnect it
931 * @param cls the `struct Session` of the idle session
932 * @param tc scheduler context
935 session_timeout (void *cls,
936 const struct GNUNET_SCHEDULER_TaskContext *tc)
938 struct Session *s = cls;
939 struct GNUNET_TIME_Relative left;
941 s->timeout_task = NULL;
942 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
943 if (0 != left.rel_value_us)
945 /* not actually our turn yet, but let's at least update
946 the monitor, it may think we're about to die ... */
947 notify_session_monitor (s->plugin,
949 GNUNET_TRANSPORT_SS_UPDATE);
950 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
955 LOG (GNUNET_ERROR_TYPE_DEBUG,
956 "Session %p was idle for %s, disconnecting\n",
958 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
960 /* call session destroy function */
961 tcp_plugin_disconnect_session (s->plugin, s);
966 * Increment session timeout due to activity
968 * @param s session to increment timeout for
971 reschedule_session_timeout (struct Session *s)
973 GNUNET_assert(NULL != s->timeout_task);
974 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
979 * Create a new session. Also queues a welcome message.
981 * @param plugin the plugin
982 * @param address the address to create the session for
983 * @param client client to use, reference counter must have already been increased
984 * @param is_nat this a NAT session, we should wait for a client to
985 * connect to us from an address, then assign that to
987 * @return new session object
989 static struct Session *
990 create_session (struct Plugin *plugin,
991 const struct GNUNET_HELLO_Address *address,
992 struct GNUNET_SERVER_Client *client,
995 struct Session *session;
996 struct PendingMessage *pm;
998 if (GNUNET_YES != is_nat)
999 GNUNET_assert (NULL != client);
1001 GNUNET_assert (NULL == client);
1003 LOG (GNUNET_ERROR_TYPE_DEBUG,
1004 "Creating new session for peer `%4s'\n",
1005 GNUNET_i2s (&address->peer));
1006 session = GNUNET_new (struct Session);
1007 session->last_activity = GNUNET_TIME_absolute_get ();
1008 session->plugin = plugin;
1009 session->is_nat = is_nat;
1010 session->client = client;
1011 session->address = GNUNET_HELLO_address_copy (address);
1012 session->target = address->peer;
1013 session->expecting_welcome = GNUNET_YES;
1014 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
1015 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
1016 sizeof (struct WelcomeMessage));
1017 pm->msg = (const char *) &pm[1];
1018 pm->message_size = sizeof(struct WelcomeMessage);
1020 &plugin->my_welcome,
1021 sizeof(struct WelcomeMessage));
1022 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1023 GNUNET_STATISTICS_update (plugin->env->stats,
1024 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
1026 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1027 session->pending_messages_tail,
1029 session->msgs_in_queue++;
1030 session->bytes_in_queue += pm->message_size;
1031 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1032 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1035 notify_session_monitor (session->plugin,
1037 GNUNET_TRANSPORT_SS_INIT);
1038 if (GNUNET_YES != is_nat)
1040 GNUNET_STATISTICS_update (plugin->env->stats,
1041 gettext_noop ("# TCP sessions active"),
1044 notify_session_monitor (session->plugin,
1046 GNUNET_TRANSPORT_SS_UP);
1050 notify_session_monitor (session->plugin,
1052 GNUNET_TRANSPORT_SS_HANDSHAKE);
1059 * If we have pending messages, ask the server to
1060 * transmit them (schedule the respective tasks, etc.)
1062 * @param session for which session should we do this
1065 process_pending_messages (struct Session *session);
1069 * Function called to notify a client about the socket
1070 * being ready to queue more data. "buf" will be
1071 * NULL and "size" zero if the socket was closed for
1072 * writing in the meantime.
1074 * @param cls closure
1075 * @param size number of bytes available in @a buf
1076 * @param buf where the callee should write the message
1077 * @return number of bytes written to @a buf
1080 do_transmit (void *cls, size_t size, void *buf)
1082 struct Session *session = cls;
1083 struct GNUNET_PeerIdentity pid;
1084 struct Plugin *plugin;
1085 struct PendingMessage *pos;
1086 struct PendingMessage *hd;
1087 struct PendingMessage *tl;
1088 struct GNUNET_TIME_Absolute now;
1092 session->transmit_handle = NULL;
1093 plugin = session->plugin;
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1098 GNUNET_i2s (&session->target));
1099 /* timeout; cancel all messages that have already expired */
1103 now = GNUNET_TIME_absolute_get ();
1104 while ((NULL != (pos = session->pending_messages_head))
1105 && (pos->timeout.abs_value_us <= now.abs_value_us))
1107 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1108 session->pending_messages_tail,
1110 GNUNET_assert (0 < session->msgs_in_queue);
1111 session->msgs_in_queue--;
1112 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1113 session->bytes_in_queue -= pos->message_size;
1114 LOG (GNUNET_ERROR_TYPE_DEBUG,
1115 "Failed to transmit %u byte message to `%4s'.\n",
1117 GNUNET_i2s (&session->target));
1118 ret += pos->message_size;
1119 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
1121 /* do this call before callbacks (so that if callbacks destroy
1122 * session, they have a chance to cancel actions done by this
1124 process_pending_messages (session);
1125 pid = session->target;
1126 /* no do callbacks and do not use session again since
1127 * the callbacks may abort the session */
1128 while (NULL != (pos = hd))
1130 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1131 if (pos->transmit_cont != NULL)
1132 pos->transmit_cont (pos->transmit_cont_cls,
1135 pos->message_size, 0);
1138 GNUNET_STATISTICS_update (plugin->env->stats,
1139 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1141 GNUNET_STATISTICS_update (plugin->env->stats,
1142 gettext_noop ("# bytes discarded by TCP (timeout)"),
1146 notify_session_monitor (session->plugin,
1148 GNUNET_TRANSPORT_SS_UPDATE);
1151 /* copy all pending messages that would fit */
1156 while (NULL != (pos = session->pending_messages_head))
1158 if (ret + pos->message_size > size)
1160 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1161 session->pending_messages_tail,
1163 GNUNET_assert (0 < session->msgs_in_queue);
1164 session->msgs_in_queue--;
1165 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1166 session->bytes_in_queue -= pos->message_size;
1167 GNUNET_assert(size >= pos->message_size);
1168 LOG(GNUNET_ERROR_TYPE_DEBUG,
1169 "Transmitting message of type %u size %u\n",
1170 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1172 /* FIXME: this memcpy can be up to 7% of our total runtime */
1173 memcpy (cbuf, pos->msg, pos->message_size);
1174 cbuf += pos->message_size;
1175 ret += pos->message_size;
1176 size -= pos->message_size;
1177 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
1179 notify_session_monitor (session->plugin,
1181 GNUNET_TRANSPORT_SS_UPDATE);
1182 /* schedule 'continuation' before callbacks so that callbacks that
1183 * cancel everything don't cause us to use a session that no longer
1185 process_pending_messages (session);
1186 session->last_activity = GNUNET_TIME_absolute_get ();
1187 pid = session->target;
1188 /* we'll now call callbacks that may cancel the session; hence
1189 * we should not use 'session' after this point */
1190 while (NULL != (pos = hd))
1192 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1193 if (pos->transmit_cont != NULL)
1194 pos->transmit_cont (pos->transmit_cont_cls,
1198 pos->message_size); /* FIXME: include TCP overhead */
1201 GNUNET_assert (NULL == hd);
1202 GNUNET_assert (NULL == tl);
1203 LOG(GNUNET_ERROR_TYPE_DEBUG,
1204 "Transmitting %u bytes\n",
1206 GNUNET_STATISTICS_update (plugin->env->stats,
1207 gettext_noop ("# bytes currently in TCP buffers"),
1210 GNUNET_STATISTICS_update (plugin->env->stats,
1211 gettext_noop ("# bytes transmitted via TCP"),
1219 * If we have pending messages, ask the server to
1220 * transmit them (schedule the respective tasks, etc.)
1222 * @param session for which session should we do this
1225 process_pending_messages (struct Session *session)
1227 struct PendingMessage *pm;
1229 GNUNET_assert (NULL != session->client);
1230 if (NULL != session->transmit_handle)
1232 if (NULL == (pm = session->pending_messages_head))
1235 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1237 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1244 * Function that can be used by the transport service to transmit
1245 * a message using the plugin. Note that in the case of a
1246 * peer disconnecting, the continuation MUST be called
1247 * prior to the disconnect notification itself. This function
1248 * will be called with this peer's HELLO message to initiate
1249 * a fresh connection to another peer.
1251 * @param cls closure
1252 * @param session which session must be used
1253 * @param msgbuf the message to transmit
1254 * @param msgbuf_size number of bytes in 'msgbuf'
1255 * @param priority how important is the message (most plugins will
1256 * ignore message priority and just FIFO)
1257 * @param to how long to wait at most for the transmission (does not
1258 * require plugins to discard the message after the timeout,
1259 * just advisory for the desired delay; most plugins will ignore
1261 * @param cont continuation to call once the message has
1262 * been transmitted (or if the transport is ready
1263 * for the next transmission call; or if the
1264 * peer disconnected...); can be NULL
1265 * @param cont_cls closure for @a cont
1266 * @return number of bytes used (on the physical network, with overheads);
1267 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1268 * and does NOT mean that the message was not transmitted (DV)
1271 tcp_plugin_send (void *cls,
1272 struct Session *session,
1275 unsigned int priority,
1276 struct GNUNET_TIME_Relative to,
1277 GNUNET_TRANSPORT_TransmitContinuation cont,
1280 struct Plugin * plugin = cls;
1281 struct PendingMessage *pm;
1283 /* create new message entry */
1284 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1285 pm->msg = (const char *) &pm[1];
1286 memcpy (&pm[1], msgbuf, msgbuf_size);
1287 pm->message_size = msgbuf_size;
1288 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1289 pm->transmit_cont = cont;
1290 pm->transmit_cont_cls = cont_cls;
1292 LOG(GNUNET_ERROR_TYPE_DEBUG,
1293 "Asked to transmit %u bytes to `%s', added message to list.\n",
1294 msgbuf_size, GNUNET_i2s (&session->target));
1297 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1301 GNUNET_assert (NULL != session->client);
1302 GNUNET_SERVER_client_set_timeout (session->client,
1303 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1304 GNUNET_STATISTICS_update (plugin->env->stats,
1305 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1308 /* append pm to pending_messages list */
1309 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1310 session->pending_messages_tail,
1312 notify_session_monitor (session->plugin,
1314 GNUNET_TRANSPORT_SS_UPDATE);
1315 session->msgs_in_queue++;
1316 session->bytes_in_queue += pm->message_size;
1317 process_pending_messages (session);
1320 else if (GNUNET_YES ==
1321 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1327 GNUNET_i2s (&session->target));
1328 GNUNET_STATISTICS_update (plugin->env->stats,
1329 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1331 /* append pm to pending_messages list */
1332 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1333 session->pending_messages_tail,
1335 session->msgs_in_queue++;
1336 session->bytes_in_queue += pm->message_size;
1337 notify_session_monitor (session->plugin,
1339 GNUNET_TRANSPORT_SS_HANDSHAKE);
1344 LOG(GNUNET_ERROR_TYPE_ERROR,
1345 "Invalid session %p\n",
1355 return GNUNET_SYSERR; /* session does not exist here */
1360 * Closure for #session_lookup_it().
1365 * Address we are looking for.
1367 const struct GNUNET_HELLO_Address *address;
1370 * Where to store the session (if we found it).
1372 struct Session *result;
1378 * Look for a session by address.
1380 * @param cls the `struct SessionItCtx`
1382 * @param value a `struct Session`
1383 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1386 session_lookup_it (void *cls,
1387 const struct GNUNET_PeerIdentity *key,
1390 struct SessionItCtx * si_ctx = cls;
1391 struct Session * session = value;
1393 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1395 /* Found existing session */
1396 si_ctx->result = session;
1402 * Task cleaning up a NAT connection attempt after timeout
1404 * @param cls the `struct Session`
1405 * @param tc scheduler context (unused)
1408 nat_connect_timeout (void *cls,
1409 const struct GNUNET_SCHEDULER_TaskContext *tc)
1411 struct Session *session = cls;
1413 session->nat_connection_timeout = NULL;
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1416 GNUNET_i2s (&session->target),
1417 tcp_plugin_address_to_string (NULL,
1418 session->address->address,
1419 session->address->address_length));
1420 tcp_plugin_disconnect_session (session->plugin,
1426 * Function that will be called whenever the transport service wants to
1427 * notify the plugin that a session is still active and in use and
1428 * therefore the session timeout for this session has to be updated
1430 * @param cls closure
1431 * @param peer which peer was the session for
1432 * @param session which session is being updated
1435 tcp_plugin_update_session_timeout (void *cls,
1436 const struct GNUNET_PeerIdentity *peer,
1437 struct Session *session)
1439 reschedule_session_timeout (session);
1444 * Task to signal the server that we can continue
1445 * receiving from the TCP client now.
1447 * @param cls the `struct Session*`
1448 * @param tc task context (unused)
1451 delayed_done (void *cls,
1452 const struct GNUNET_SCHEDULER_TaskContext *tc)
1454 struct Session *session = cls;
1456 session->receive_delay_task = NULL;
1457 reschedule_session_timeout (session);
1459 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1464 * Function that will be called whenever the transport service wants to
1465 * notify the plugin that the inbound quota changed and that the plugin
1466 * should update it's delay for the next receive value
1468 * @param cls closure
1469 * @param peer which peer was the session for
1470 * @param session which session is being updated
1471 * @param delay new delay to use for receiving
1474 tcp_plugin_update_inbound_delay (void *cls,
1475 const struct GNUNET_PeerIdentity *peer,
1476 struct Session *session,
1477 struct GNUNET_TIME_Relative delay)
1479 if (NULL == session->receive_delay_task)
1481 LOG (GNUNET_ERROR_TYPE_DEBUG,
1482 "New inbound delay %s\n",
1483 GNUNET_STRINGS_relative_time_to_string (delay,
1485 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1486 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1487 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1494 * Create a new session to transmit data to the target
1495 * This session will used to send data to this peer and the plugin will
1496 * notify us by calling the env->session_end function
1498 * @param cls closure
1499 * @param address the address to use
1500 * @return the session if the address is valid, NULL otherwise
1502 static struct Session *
1503 tcp_plugin_get_session (void *cls,
1504 const struct GNUNET_HELLO_Address *address)
1506 struct Plugin *plugin = cls;
1507 struct Session *session = NULL;
1511 struct GNUNET_CONNECTION_Handle *sa;
1512 struct sockaddr_in a4;
1513 struct sockaddr_in6 a6;
1514 const struct IPv4TcpAddress *t4;
1515 const struct IPv6TcpAddress *t6;
1516 unsigned int options;
1517 enum GNUNET_ATS_Network_Type net_type;
1518 unsigned int is_natd = GNUNET_NO;
1521 struct GNUNET_NETWORK_Handle *s;
1524 addrlen = address->address_length;
1525 LOG (GNUNET_ERROR_TYPE_DEBUG,
1526 "Trying to get session for `%s' address of peer `%s'\n",
1527 tcp_plugin_address_to_string (NULL,
1529 address->address_length),
1530 GNUNET_i2s (&address->peer));
1532 if (GNUNET_HELLO_address_check_option (address,
1533 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1539 /* look for existing session */
1541 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1544 struct SessionItCtx si_ctx;
1546 si_ctx.address = address;
1547 si_ctx.result = NULL;
1549 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1551 &session_lookup_it, &si_ctx);
1552 if (NULL != si_ctx.result)
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,
1560 address->address_length),
1564 LOG (GNUNET_ERROR_TYPE_DEBUG,
1565 "Existing sessions did not match address `%s' or peer `%s'\n",
1566 tcp_plugin_address_to_string (NULL,
1568 address->address_length),
1569 GNUNET_i2s (&address->peer));
1572 if (addrlen == sizeof(struct IPv6TcpAddress))
1574 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1575 t6 = address->address;
1576 options = t6->options;
1578 memset (&a6, 0, sizeof(a6));
1579 #if HAVE_SOCKADDR_IN_SIN_LEN
1580 a6.sin6_len = sizeof (a6);
1582 a6.sin6_family = AF_INET6;
1583 a6.sin6_port = t6->t6_port;
1584 if (t6->t6_port == 0)
1585 is_natd = GNUNET_YES;
1586 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1590 else if (addrlen == sizeof(struct IPv4TcpAddress))
1592 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1593 t4 = address->address;
1594 options = t4->options;
1596 memset (&a4, 0, sizeof(a4));
1597 #if HAVE_SOCKADDR_IN_SIN_LEN
1598 a4.sin_len = sizeof (a4);
1600 a4.sin_family = AF_INET;
1601 a4.sin_port = t4->t4_port;
1602 if (t4->t4_port == 0)
1603 is_natd = GNUNET_YES;
1604 a4.sin_addr.s_addr = t4->ipv4_addr;
1610 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1611 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1615 net_type = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1618 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1620 /* NAT client only works with IPv4 addresses */
1624 if (plugin->cur_connections >= plugin->max_connections)
1630 if ((is_natd == GNUNET_YES)
1632 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1635 /* Only do one NAT punch attempt per peer identity */
1639 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1641 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1644 LOG (GNUNET_ERROR_TYPE_DEBUG,
1645 "Found valid IPv4 NAT address (creating session)!\n");
1646 session = create_session (plugin,
1650 session->ats_address_network_type = net_type;
1651 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1652 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1653 &nat_connect_timeout,
1655 GNUNET_assert(GNUNET_OK ==
1656 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1661 LOG (GNUNET_ERROR_TYPE_DEBUG,
1662 "Created NAT WAIT connection to `%4s' at `%s'\n",
1663 GNUNET_i2s (&session->target),
1664 GNUNET_a2s (sb, sbs));
1665 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1671 LOG(GNUNET_ERROR_TYPE_DEBUG,
1672 "Running NAT client for `%4s' at `%s' failed\n",
1673 GNUNET_i2s (&session->target),
1674 GNUNET_a2s (sb, sbs));
1675 tcp_plugin_disconnect_session (plugin,
1681 /* create new outbound session */
1682 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1684 if (0 != (options & TCP_OPTIONS_TCP_STEALTH))
1687 s = GNUNET_NETWORK_socket_create (af, SOCK_STREAM, 0);
1690 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
1697 GNUNET_NETWORK_socket_setsockopt (s,
1701 sizeof (struct GNUNET_PeerIdentity))) ||
1703 GNUNET_NETWORK_socket_setsockopt (s,
1705 TCP_STEALTH_INTEGRITY,
1706 &plugin->my_welcome,
1707 sizeof (struct WelcomeMessage))) )
1709 /* TCP STEALTH not supported by kernel */
1710 GNUNET_break (GNUNET_OK ==
1711 GNUNET_NETWORK_socket_close (s));
1716 sa = GNUNET_CONNECTION_connect_socket (s, sb, sbs);
1725 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1729 LOG (GNUNET_ERROR_TYPE_DEBUG,
1730 "Failed to create connection to `%4s' at `%s'\n",
1731 GNUNET_i2s (&address->peer),
1732 GNUNET_a2s (sb, sbs));
1735 plugin->cur_connections++;
1736 if (plugin->cur_connections == plugin->max_connections)
1737 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1739 LOG (GNUNET_ERROR_TYPE_DEBUG,
1740 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1741 GNUNET_i2s (&address->peer),
1742 GNUNET_a2s (sb, sbs));
1744 session = create_session (plugin,
1746 GNUNET_SERVER_connect_socket (plugin->server, sa),
1748 session->ats_address_network_type = net_type;
1749 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1750 GNUNET_SERVER_client_set_user_context(session->client, session);
1751 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1755 LOG (GNUNET_ERROR_TYPE_DEBUG,
1756 "Creating new session for `%s' address `%s' session %p\n",
1757 GNUNET_i2s (&address->peer),
1758 tcp_plugin_address_to_string (NULL,
1760 address->address_length),
1762 /* Send TCP Welcome */
1763 process_pending_messages (session);
1770 session_disconnect_it (void *cls,
1771 const struct GNUNET_PeerIdentity *key,
1774 struct Plugin *plugin = cls;
1775 struct Session *session = value;
1777 GNUNET_STATISTICS_update (session->plugin->env->stats,
1778 gettext_noop ("# transport-service disconnect requests for TCP"),
1781 tcp_plugin_disconnect_session (plugin,
1788 * Function that can be called to force a disconnect from the
1789 * specified neighbour. This should also cancel all previously
1790 * scheduled transmissions. Obviously the transmission may have been
1791 * partially completed already, which is OK. The plugin is supposed
1792 * to close the connection (if applicable) and no longer call the
1793 * transmit continuation(s).
1795 * Finally, plugin MUST NOT call the services's receive function to
1796 * notify the service that the connection to the specified target was
1797 * closed after a getting this call.
1799 * @param cls closure
1800 * @param target peer for which the last transmission is
1804 tcp_plugin_disconnect (void *cls,
1805 const struct GNUNET_PeerIdentity *target)
1807 struct Plugin *plugin = cls;
1809 LOG(GNUNET_ERROR_TYPE_DEBUG,
1810 "Disconnecting peer `%4s'\n",
1811 GNUNET_i2s (target));
1812 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1814 &session_disconnect_it,
1816 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1818 &session_disconnect_it,
1824 * Running pretty printers: head
1826 static struct PrettyPrinterContext *ppc_dll_head;
1829 * Running pretty printers: tail
1831 static struct PrettyPrinterContext *ppc_dll_tail;
1834 * Context for address to string conversion, closure
1835 * for #append_port().
1837 struct PrettyPrinterContext
1842 struct PrettyPrinterContext *next;
1847 struct PrettyPrinterContext *prev;
1852 struct GNUNET_SCHEDULER_Task * timeout_task;
1857 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1860 * Function to call with the result.
1862 GNUNET_TRANSPORT_AddressStringCallback asc;
1865 * Clsoure for @e asc.
1870 * Port to add after the IP address.
1887 * Append our port and forward the result.
1889 * @param cls the `struct PrettyPrinterContext *`
1890 * @param hostname hostname part of the address
1893 append_port (void *cls,
1894 const char *hostname)
1896 struct PrettyPrinterContext *ppc = cls;
1899 if (NULL == hostname)
1901 /* Final call, done */
1902 ppc->resolver_handle = NULL;
1903 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
1906 ppc->asc (ppc->asc_cls,
1912 if (GNUNET_YES == ppc->ipv6)
1913 GNUNET_asprintf (&ret,
1920 GNUNET_asprintf (&ret,
1926 ppc->asc (ppc->asc_cls,
1934 * Convert the transports address to a nice, human-readable
1937 * @param cls closure
1938 * @param type name of the transport that generated the address
1939 * @param addr one of the addresses of the host, NULL for the last address
1940 * the specific address format depends on the transport
1941 * @param addrlen length of the @a addr
1942 * @param numeric should (IP) addresses be displayed in numeric form?
1943 * @param timeout after how long should we give up?
1944 * @param asc function to call on each string
1945 * @param asc_cls closure for @a asc
1948 tcp_plugin_address_pretty_printer (void *cls,
1953 struct GNUNET_TIME_Relative timeout,
1954 GNUNET_TRANSPORT_AddressStringCallback asc,
1957 struct PrettyPrinterContext *ppc;
1960 struct sockaddr_in a4;
1961 struct sockaddr_in6 a6;
1962 const struct IPv4TcpAddress *t4;
1963 const struct IPv6TcpAddress *t6;
1967 if (sizeof(struct IPv6TcpAddress) == addrlen)
1970 memset (&a6, 0, sizeof(a6));
1971 a6.sin6_family = AF_INET6;
1972 a6.sin6_port = t6->t6_port;
1973 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1974 port = ntohs (t6->t6_port);
1975 options = ntohl (t6->options);
1979 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1982 memset (&a4, 0, sizeof(a4));
1983 a4.sin_family = AF_INET;
1984 a4.sin_port = t4->t4_port;
1985 a4.sin_addr.s_addr = t4->ipv4_addr;
1986 port = ntohs (t4->t4_port);
1987 options = ntohl (t4->options);
1993 /* invalid address */
1994 asc (asc_cls, NULL, GNUNET_SYSERR);
1995 asc (asc_cls, NULL, GNUNET_OK);
1998 ppc = GNUNET_new (struct PrettyPrinterContext);
1999 if (addrlen == sizeof(struct IPv6TcpAddress))
2000 ppc->ipv6 = GNUNET_YES;
2002 ppc->ipv6 = GNUNET_NO;
2004 ppc->asc_cls = asc_cls;
2006 ppc->options = options;
2007 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
2012 if (NULL == ppc->resolver_handle)
2018 GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
2025 * Check if the given port is plausible (must be either our listen
2026 * port or our advertised port), or any port if we are behind NAT
2027 * and do not have a port open. If it is neither, we return
2030 * @param plugin global variables
2031 * @param in_port port number to check
2032 * @return #GNUNET_OK if port is either open_port or adv_port
2035 check_port (struct Plugin *plugin, uint16_t in_port)
2037 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
2039 return GNUNET_SYSERR;
2043 * Function that will be called to check if a binary address for this
2044 * plugin is well-formed and corresponds to an address for THIS peer
2045 * (as per our configuration). Naturally, if absolutely necessary,
2046 * plugins can be a bit conservative in their answer, but in general
2047 * plugins should make sure that the address does not redirect
2048 * traffic to a 3rd party that might try to man-in-the-middle our
2051 * @param cls closure, our `struct Plugin *`
2052 * @param addr pointer to the address
2053 * @param addrlen length of addr
2054 * @return #GNUNET_OK if this is a plausible address for this peer
2055 * and transport, #GNUNET_SYSERR if not
2058 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
2060 struct Plugin *plugin = cls;
2061 struct IPv4TcpAddress *v4;
2062 struct IPv6TcpAddress *v6;
2064 if ((addrlen != sizeof(struct IPv4TcpAddress))
2065 && (addrlen != sizeof(struct IPv6TcpAddress)))
2068 return GNUNET_SYSERR;
2071 if (addrlen == sizeof(struct IPv4TcpAddress))
2073 v4 = (struct IPv4TcpAddress *) addr;
2074 if (0 != memcmp (&v4->options,
2079 return GNUNET_SYSERR;
2081 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
2082 return GNUNET_SYSERR;
2084 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
2085 sizeof(struct in_addr)))
2086 return GNUNET_SYSERR;
2090 v6 = (struct IPv6TcpAddress *) addr;
2091 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
2094 return GNUNET_SYSERR;
2096 if (0 != memcmp (&v6->options,
2101 return GNUNET_SYSERR;
2103 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
2104 return GNUNET_SYSERR;
2106 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
2107 sizeof(struct in6_addr)))
2108 return GNUNET_SYSERR;
2114 * We've received a nat probe from this peer via TCP. Finish
2115 * creating the client session and resume sending of queued
2118 * @param cls closure
2119 * @param client identification of the client
2120 * @param message the actual message
2123 handle_tcp_nat_probe (void *cls,
2124 struct GNUNET_SERVER_Client *client,
2125 const struct GNUNET_MessageHeader *message)
2127 struct Plugin *plugin = cls;
2128 struct Session *session;
2129 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2132 struct IPv4TcpAddress *t4;
2133 struct IPv6TcpAddress *t6;
2134 const struct sockaddr_in *s4;
2135 const struct sockaddr_in6 *s6;
2137 LOG (GNUNET_ERROR_TYPE_DEBUG,
2138 "Received NAT probe\n");
2139 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2140 * a connection to this peer by running gnunet-nat-client. This peer
2141 * received the punch message and now wants us to use the new connection
2142 * as the default for that peer. Do so and then send a WELCOME message
2143 * so we can really be connected!
2145 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2152 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2153 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2154 sizeof(struct GNUNET_PeerIdentity)))
2156 /* refuse connections from ourselves */
2157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2161 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2162 &tcp_nat_probe->clientIdentity);
2163 if (session == NULL)
2165 LOG (GNUNET_ERROR_TYPE_DEBUG,
2166 "Did NOT find session for NAT probe!\n");
2167 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2170 LOG (GNUNET_ERROR_TYPE_DEBUG,
2171 "Found session for NAT probe!\n");
2173 if (session->nat_connection_timeout != NULL)
2175 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2176 session->nat_connection_timeout = NULL;
2179 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2183 tcp_plugin_disconnect_session (plugin,
2188 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2189 GNUNET_SERVER_client_set_user_context(client, session);
2190 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2191 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2192 session->last_activity = GNUNET_TIME_absolute_get ();
2193 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
2194 GNUNET_a2s (vaddr, alen));
2195 switch (((const struct sockaddr *) vaddr)->sa_family)
2199 t4 = GNUNET_new (struct IPv4TcpAddress);
2200 t4->options = htonl (TCP_OPTIONS_NONE);
2201 t4->t4_port = s4->sin_port;
2202 t4->ipv4_addr = s4->sin_addr.s_addr;
2203 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2206 sizeof(struct IPv4TcpAddress),
2207 GNUNET_HELLO_ADDRESS_INFO_NONE);
2211 t6 = GNUNET_new (struct IPv6TcpAddress);
2212 t6->options = htonl (TCP_OPTIONS_NONE);
2213 t6->t6_port = s6->sin6_port;
2214 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2215 session->address = GNUNET_HELLO_address_allocate (&tcp_nat_probe->clientIdentity,
2218 sizeof(struct IPv6TcpAddress),
2219 GNUNET_HELLO_ADDRESS_INFO_NONE);
2223 LOG(GNUNET_ERROR_TYPE_DEBUG,
2224 "Bad address for incoming connection!\n");
2226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2227 tcp_plugin_disconnect_session (plugin,
2232 GNUNET_break(NULL == session->client);
2233 GNUNET_SERVER_client_keep (client);
2234 session->client = client;
2235 GNUNET_STATISTICS_update (plugin->env->stats,
2236 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2237 process_pending_messages (session);
2238 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2242 * We've received a welcome from this peer via TCP. Possibly create a
2243 * fresh client record and send back our welcome.
2245 * @param cls closure
2246 * @param client identification of the client
2247 * @param message the actual message
2250 handle_tcp_welcome (void *cls,
2251 struct GNUNET_SERVER_Client *client,
2252 const struct GNUNET_MessageHeader *message)
2254 struct Plugin *plugin = cls;
2255 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2256 struct GNUNET_HELLO_Address *address;
2257 struct Session *session;
2260 struct IPv4TcpAddress t4;
2261 struct IPv6TcpAddress t6;
2262 const struct sockaddr_in *s4;
2263 const struct sockaddr_in6 *s6;
2264 struct GNUNET_ATS_Information ats;
2267 if (0 == memcmp (&wm->clientIdentity,
2268 plugin->env->my_identity,
2269 sizeof(struct GNUNET_PeerIdentity)))
2271 /* refuse connections from ourselves */
2272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2273 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2275 LOG(GNUNET_ERROR_TYPE_INFO,
2276 "Received %s message from my own identity `%4s' on address `%s'\n",
2277 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2278 GNUNET_a2s (vaddr, alen));
2284 LOG(GNUNET_ERROR_TYPE_DEBUG,
2285 "Received %s message from `%4s' %p\n",
2287 GNUNET_i2s (&wm->clientIdentity), client);
2288 GNUNET_STATISTICS_update (plugin->env->stats,
2289 gettext_noop ("# TCP WELCOME messages received"),
2292 session = lookup_session_by_client (plugin, client);
2293 if (NULL != session)
2295 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2297 LOG (GNUNET_ERROR_TYPE_DEBUG,
2298 "Found existing session %p for peer `%s'\n",
2300 GNUNET_a2s (vaddr, alen));
2301 GNUNET_free (vaddr);
2306 GNUNET_SERVER_client_keep (client);
2307 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2308 plugin->cur_connections++;
2309 if (plugin->cur_connections == plugin->max_connections)
2310 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2312 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2314 if (alen == sizeof(struct sockaddr_in))
2317 memset (&t4, '\0', sizeof (t4));
2318 t4.options = htonl (TCP_OPTIONS_NONE);
2319 t4.t4_port = s4->sin_port;
2320 t4.ipv4_addr = s4->sin_addr.s_addr;
2321 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2325 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2327 else if (alen == sizeof(struct sockaddr_in6))
2330 memset (&t6, '\0', sizeof (t6));
2331 t6.options = htonl (TCP_OPTIONS_NONE);
2332 t6.t6_port = s6->sin6_port;
2333 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2334 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2335 PLUGIN_NAME, &t6, sizeof (t6),
2336 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2341 GNUNET_free_non_null (vaddr);
2344 session = create_session (plugin, address, client, GNUNET_NO);
2345 GNUNET_HELLO_address_free (address);
2346 session->ats_address_network_type = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2347 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2348 ats.value = htonl (session->ats_address_network_type);
2349 LOG (GNUNET_ERROR_TYPE_DEBUG,
2350 "Creating new%s session %p for peer `%s' client %p \n",
2351 GNUNET_HELLO_address_check_option (session->address,
2352 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2355 tcp_plugin_address_to_string (NULL,
2356 (void *) session->address->address,
2357 session->address->address_length),
2359 GNUNET_free (vaddr);
2360 GNUNET_SERVER_client_set_user_context (session->client, session);
2361 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2365 /* Notify transport and ATS about new session */
2366 plugin->env->session_start (plugin->env->cls,
2370 notify_session_monitor (plugin,
2372 GNUNET_TRANSPORT_SS_INIT);
2376 LOG(GNUNET_ERROR_TYPE_DEBUG,
2377 "Did not obtain TCP socket address for incoming connection\n");
2383 if (session->expecting_welcome != GNUNET_YES)
2386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2390 session->last_activity = GNUNET_TIME_absolute_get ();
2391 session->expecting_welcome = GNUNET_NO;
2393 process_pending_messages (session);
2394 GNUNET_SERVER_client_set_timeout (client,
2395 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2396 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2401 * We've received data for this peer via TCP. Unbox,
2402 * compute latency and forward.
2404 * @param cls closure
2405 * @param client identification of the client
2406 * @param message the actual message
2409 handle_tcp_data (void *cls,
2410 struct GNUNET_SERVER_Client *client,
2411 const struct GNUNET_MessageHeader *message)
2413 struct Plugin *plugin = cls;
2414 struct Session *session;
2415 struct GNUNET_TIME_Relative delay;
2418 type = ntohs (message->type);
2419 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2420 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2422 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2426 session = lookup_session_by_client (plugin, client);
2427 if (NULL == session)
2429 /* No inbound session found */
2433 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2434 LOG(GNUNET_ERROR_TYPE_ERROR,
2435 "Received unexpected %u bytes of type %u from `%s'\n",
2436 (unsigned int) ntohs (message->size),
2437 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2440 GNUNET_free_non_null(vaddr);
2443 else if (GNUNET_YES == session->expecting_welcome)
2445 /* Session is expecting WELCOME message */
2449 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2450 LOG(GNUNET_ERROR_TYPE_ERROR,
2451 "Received unexpected %u bytes of type %u from `%s'\n",
2452 (unsigned int) ntohs (message->size),
2453 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2456 GNUNET_free_non_null(vaddr);
2460 session->last_activity = GNUNET_TIME_absolute_get ();
2461 LOG(GNUNET_ERROR_TYPE_DEBUG,
2462 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2463 (unsigned int) ntohs (message->size),
2464 (unsigned int) ntohs (message->type), GNUNET_i2s (&session->target));
2466 GNUNET_STATISTICS_update (plugin->env->stats,
2467 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2469 struct GNUNET_ATS_Information distance;
2471 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2472 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2473 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2475 GNUNET_assert(GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2479 delay = plugin->env->receive (plugin->env->cls,
2483 plugin->env->update_address_metrics (plugin->env->cls,
2487 reschedule_session_timeout (session);
2488 if (0 == delay.rel_value_us)
2490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2494 LOG(GNUNET_ERROR_TYPE_DEBUG,
2495 "Throttling receiving from `%s' for %s\n",
2496 GNUNET_i2s (&session->target),
2497 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2498 GNUNET_SERVER_disable_receive_done_warning (client);
2499 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2500 &delayed_done, session);
2506 * Functions with this signature are called whenever a peer
2507 * is disconnected on the network level.
2509 * @param cls closure
2510 * @param client identification of the client
2513 disconnect_notify (void *cls,
2514 struct GNUNET_SERVER_Client *client)
2516 struct Plugin *plugin = cls;
2517 struct Session *session;
2521 session = lookup_session_by_client (plugin, client);
2522 if (NULL == session)
2523 return; /* unknown, nothing to do */
2524 LOG (GNUNET_ERROR_TYPE_DEBUG,
2525 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2526 GNUNET_i2s (&session->target),
2527 tcp_plugin_address_to_string (session->plugin, session->address->address,
2528 session->address->address_length));
2530 if (plugin->cur_connections == plugin->max_connections)
2531 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2533 if (plugin->cur_connections < 1)
2536 plugin->cur_connections--;
2538 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2539 ("# network-level TCP disconnect events"),
2542 tcp_plugin_disconnect_session (plugin, session);
2547 * We can now send a probe message, copy into buffer to really send.
2549 * @param cls closure, a struct TCPProbeContext
2550 * @param size max size to copy
2551 * @param buf buffer to copy message to
2552 * @return number of bytes copied into @a buf
2555 notify_send_probe (void *cls,
2559 struct TCPProbeContext *tcp_probe_ctx = cls;
2560 struct Plugin *plugin = tcp_probe_ctx->plugin;
2563 tcp_probe_ctx->transmit_handle = NULL;
2564 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2569 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2570 GNUNET_free(tcp_probe_ctx);
2573 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2574 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2575 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2576 tcp_probe_ctx->sock);
2577 ret = sizeof(tcp_probe_ctx->message);
2578 GNUNET_free(tcp_probe_ctx);
2584 * Function called by the NAT subsystem suggesting another peer wants
2585 * to connect to us via connection reversal. Try to connect back to the
2588 * @param cls closure
2589 * @param addr address to try
2590 * @param addrlen number of bytes in @a addr
2593 try_connection_reversal (void *cls,
2594 const struct sockaddr *addr,
2597 struct Plugin *plugin = cls;
2598 struct GNUNET_CONNECTION_Handle *sock;
2599 struct TCPProbeContext *tcp_probe_ctx;
2602 * We have received an ICMP response, ostensibly from a peer
2603 * that wants to connect to us! Send a message to establish a connection.
2605 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2608 /* failed for some odd reason (out of sockets?); ignore attempt */
2612 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2613 tcp_probe_ctx->message.header.size
2614 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2615 tcp_probe_ctx->message.header.type
2616 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2617 tcp_probe_ctx->message.clientIdentity
2618 = *plugin->env->my_identity;
2619 tcp_probe_ctx->plugin = plugin;
2620 tcp_probe_ctx->sock = sock;
2621 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2624 tcp_probe_ctx->transmit_handle
2625 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2626 ntohs (tcp_probe_ctx->message.header.size),
2627 GNUNET_TIME_UNIT_FOREVER_REL,
2634 * Function obtain the network type for a session
2636 * @param cls closure ('struct Plugin*')
2637 * @param session the session
2638 * @return the network type in HBO or #GNUNET_SYSERR
2640 static enum GNUNET_ATS_Network_Type
2641 tcp_plugin_get_network (void *cls,
2642 struct Session *session)
2644 return session->ats_address_network_type;
2649 * Return information about the given session to the
2652 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2653 * @param peer peer we send information about
2654 * @param value our `struct Session` to send information about
2655 * @return #GNUNET_OK (continue to iterate)
2658 send_session_info_iter (void *cls,
2659 const struct GNUNET_PeerIdentity *peer,
2662 struct Plugin *plugin = cls;
2663 struct Session *session = value;
2665 notify_session_monitor (plugin,
2667 GNUNET_TRANSPORT_SS_INIT);
2668 /* FIXME: cannot tell if this is up or not from current
2670 notify_session_monitor (plugin,
2672 GNUNET_TRANSPORT_SS_UP);
2678 * Begin monitoring sessions of a plugin. There can only
2679 * be one active monitor per plugin (i.e. if there are
2680 * multiple monitors, the transport service needs to
2681 * multiplex the generated events over all of them).
2683 * @param cls closure of the plugin
2684 * @param sic callback to invoke, NULL to disable monitor;
2685 * plugin will being by iterating over all active
2686 * sessions immediately and then enter monitor mode
2687 * @param sic_cls closure for @a sic
2690 tcp_plugin_setup_monitor (void *cls,
2691 GNUNET_TRANSPORT_SessionInfoCallback sic,
2694 struct Plugin *plugin = cls;
2697 plugin->sic_cls = sic_cls;
2700 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2701 &send_session_info_iter,
2703 /* signal end of first iteration */
2704 sic (sic_cls, NULL, NULL);
2710 * Entry point for the plugin.
2712 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2713 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2716 libgnunet_plugin_transport_tcp_init (void *cls)
2718 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2719 { &handle_tcp_welcome, NULL,
2720 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, sizeof(struct WelcomeMessage) },
2721 { &handle_tcp_nat_probe, NULL,
2722 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof(struct TCP_NAT_ProbeMessage) },
2723 { &handle_tcp_data, NULL,
2724 GNUNET_MESSAGE_TYPE_ALL, 0 },
2725 { NULL, NULL, 0, 0 }
2727 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2728 struct GNUNET_TRANSPORT_PluginFunctions *api;
2729 struct Plugin *plugin;
2730 struct GNUNET_SERVICE_Context *service;
2731 unsigned long long aport;
2732 unsigned long long bport;
2733 unsigned long long max_connections;
2735 struct GNUNET_TIME_Relative idle_timeout;
2737 struct GNUNET_NETWORK_Handle *const*lsocks;
2741 struct sockaddr **addrs;
2742 socklen_t *addrlens;
2744 if (NULL == env->receive)
2746 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2747 initialze the plugin or the API */
2748 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2750 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2751 api->address_to_string = &tcp_plugin_address_to_string;
2752 api->string_to_address = &tcp_plugin_string_to_address;
2756 GNUNET_assert (NULL != env->cfg);
2758 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2762 max_connections = 128;
2766 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2770 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2771 "ADVERTISED-PORT", &aport)) &&
2774 LOG(GNUNET_ERROR_TYPE_ERROR,
2775 _("Require valid port number for service `%s' in configuration!\n"),
2785 service = GNUNET_SERVICE_start ("transport-tcp",
2787 GNUNET_SERVICE_OPTION_NONE);
2788 if (NULL == service)
2790 LOG (GNUNET_ERROR_TYPE_WARNING,
2791 _("Failed to start service.\n"));
2799 plugin = GNUNET_new (struct Plugin);
2800 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2802 plugin->max_connections = max_connections;
2803 plugin->open_port = bport;
2804 plugin->adv_port = aport;
2806 plugin->my_welcome.header.size = htons (sizeof(struct WelcomeMessage));
2807 plugin->my_welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
2808 plugin->my_welcome.clientIdentity = *plugin->env->my_identity;
2810 if ( (NULL != service) &&
2812 GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2817 plugin->myoptions |= TCP_OPTIONS_TCP_STEALTH;
2818 lsocks = GNUNET_SERVICE_get_listen_sockets (service);
2821 uint32_t len = sizeof (struct WelcomeMessage);
2823 for (i=0;NULL!=lsocks[i];i++)
2826 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2830 sizeof (struct GNUNET_PeerIdentity))) ||
2832 GNUNET_NETWORK_socket_setsockopt (lsocks[i],
2834 TCP_STEALTH_INTEGRITY_LEN,
2838 /* TCP STEALTH not supported by kernel */
2839 GNUNET_assert (0 == i);
2840 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2841 _("TCP_STEALTH not supported on this platform.\n"));
2847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2848 _("TCP_STEALTH not supported on this platform.\n"));
2853 if ( (NULL != service) &&
2856 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2861 for (ret = ret_s-1; ret >= 0; ret--)
2862 LOG (GNUNET_ERROR_TYPE_INFO,
2863 "Binding to address `%s'\n",
2864 GNUNET_a2s (addrs[ret], addrlens[ret]));
2866 = GNUNET_NAT_register (env->cfg,
2869 (unsigned int) ret_s,
2870 (const struct sockaddr **) addrs, addrlens,
2871 &tcp_nat_port_map_callback,
2872 &try_connection_reversal,
2874 for (ret = ret_s -1; ret >= 0; ret--)
2875 GNUNET_free (addrs[ret]);
2876 GNUNET_free_non_null (addrs);
2877 GNUNET_free_non_null (addrlens);
2881 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2888 &try_connection_reversal,
2891 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2893 api->send = &tcp_plugin_send;
2894 api->get_session = &tcp_plugin_get_session;
2895 api->disconnect_session = &tcp_plugin_disconnect_session;
2896 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2897 api->disconnect_peer = &tcp_plugin_disconnect;
2898 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2899 api->check_address = &tcp_plugin_check_address;
2900 api->address_to_string = &tcp_plugin_address_to_string;
2901 api->string_to_address = &tcp_plugin_string_to_address;
2902 api->get_network = &tcp_plugin_get_network;
2903 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2904 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2905 api->setup_monitor = &tcp_plugin_setup_monitor;
2906 plugin->service = service;
2907 if (NULL != service)
2909 plugin->server = GNUNET_SERVICE_get_server (service);
2914 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2919 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2925 = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
2927 idle_timeout, GNUNET_YES);
2929 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2930 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2931 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2932 plugin->handlers[i].callback_cls = plugin;
2934 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2935 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2936 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2939 LOG (GNUNET_ERROR_TYPE_INFO,
2940 _("TCP transport listening on port %llu\n"),
2943 LOG (GNUNET_ERROR_TYPE_INFO,
2944 _("TCP transport not listening on any port (client only)\n"));
2945 if ( (aport != bport) &&
2947 LOG (GNUNET_ERROR_TYPE_INFO,
2948 _("TCP transport advertises itself as being on port %llu\n"),
2950 /* Initially set connections to 0 */
2951 GNUNET_assert(NULL != plugin->env->stats);
2952 GNUNET_STATISTICS_set (plugin->env->stats,
2953 gettext_noop ("# TCP sessions active"),
2959 if (NULL != plugin->nat)
2960 GNUNET_NAT_unregister (plugin->nat);
2961 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2962 if (NULL != service)
2963 GNUNET_SERVICE_stop (service);
2964 GNUNET_free (plugin);
2965 GNUNET_free_non_null (api);
2971 * Exit point from the plugin.
2973 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2977 libgnunet_plugin_transport_tcp_done (void *cls)
2979 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2980 struct Plugin *plugin = api->cls;
2981 struct TCPProbeContext *tcp_probe;
2982 struct PrettyPrinterContext *cur;
2983 struct PrettyPrinterContext *next;
2990 LOG (GNUNET_ERROR_TYPE_DEBUG,
2991 "Shutting down TCP plugin\n");
2993 /* Removing leftover sessions */
2994 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2995 &session_disconnect_it,
2997 /* Removing leftover NAT sessions */
2998 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2999 &session_disconnect_it,
3002 for (cur = ppc_dll_head; NULL != cur; cur = next)
3005 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
3008 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
3009 cur->asc (cur->asc_cls, NULL, GNUNET_OK);
3013 if (NULL != plugin->service)
3014 GNUNET_SERVICE_stop (plugin->service);
3016 GNUNET_SERVER_destroy (plugin->server);
3017 GNUNET_free(plugin->handlers);
3018 if (NULL != plugin->nat)
3019 GNUNET_NAT_unregister (plugin->nat);
3020 while (NULL != (tcp_probe = plugin->probe_head))
3022 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3025 GNUNET_CONNECTION_destroy (tcp_probe->sock);
3026 GNUNET_free(tcp_probe);
3028 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
3029 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
3030 GNUNET_free(plugin);
3035 /* end of plugin_transport_tcp.c */