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;
124 GNUNET_NETWORK_STRUCT_BEGIN
127 * Network format for IPv4 addresses.
129 struct IPv4TcpAddress
132 * Optional options and flags for this address
137 * IPv4 address, in network byte order.
139 uint32_t ipv4_addr GNUNET_PACKED;
142 * Port number, in network byte order.
144 uint16_t t4_port GNUNET_PACKED;
149 * Network format for IPv6 addresses.
151 struct IPv6TcpAddress
154 * Optional flags for this address
161 struct in6_addr ipv6_addr GNUNET_PACKED;
164 * Port number, in network byte order.
166 uint16_t t6_port GNUNET_PACKED;
169 GNUNET_NETWORK_STRUCT_END
172 * Encapsulation of all of the state of the plugin.
177 * Information kept for each message that is yet to
180 struct PendingMessage
184 * This is a doubly-linked list.
186 struct PendingMessage *next;
189 * This is a doubly-linked list.
191 struct PendingMessage *prev;
194 * The pending message
199 * Continuation function to call once the message
200 * has been sent. Can be NULL if there is no
201 * continuation to call.
203 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
206 * Closure for transmit_cont.
208 void *transmit_cont_cls;
211 * Timeout value for the pending message.
213 struct GNUNET_TIME_Absolute timeout;
216 * So that the gnunet-service-transport can group messages together,
217 * these pending messages need to accept a message buffer and size
218 * instead of just a GNUNET_MessageHeader.
225 * Session handle for TCP connections.
230 * To whom are we talking to (set to our identity
231 * if we are still waiting for the welcome message)
233 struct GNUNET_PeerIdentity target;
236 * Pointer to the global plugin struct.
238 struct Plugin *plugin;
241 * The client (used to identify this connection)
243 struct GNUNET_SERVER_Client *client;
246 * Task cleaning up a NAT client connection establishment attempt;
248 GNUNET_SCHEDULER_TaskIdentifier nat_connection_timeout;
251 * Messages currently pending for transmission
252 * to this peer, if any.
254 struct PendingMessage *pending_messages_head;
257 * Messages currently pending for transmission
258 * to this peer, if any.
260 struct PendingMessage *pending_messages_tail;
263 * Handle for pending transmission request.
265 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
268 * Address of the other peer.
270 struct GNUNET_HELLO_Address *address;
273 * ID of task used to delay receiving more to throttle sender.
275 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
278 * Session timeout task
280 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
283 * When will this session time out?
285 struct GNUNET_TIME_Absolute timeout;
288 * When will we continue to read from the socket?
289 * (used to enforce inbound quota).
291 struct GNUNET_TIME_Absolute receive_delay;
294 * Last activity on this connection. Used to select preferred
297 struct GNUNET_TIME_Absolute last_activity;
300 * Number of bytes waiting for transmission to this peer.
302 unsigned long long bytes_in_queue;
305 * Number of messages waiting for transmission to this peer.
307 unsigned int msgs_in_queue;
310 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
312 int expecting_welcome;
315 * Was this session created using NAT traversal?
320 * ATS network type in NBO
322 enum GNUNET_ATS_Network_Type ats_address_network_type;
326 * Encapsulation of all of the state of the plugin.
333 struct GNUNET_TRANSPORT_PluginEnvironment *env;
338 struct GNUNET_CONNECTION_Handle *lsock;
341 * Our handle to the NAT module.
343 struct GNUNET_NAT_Handle *nat;
346 * Map from peer identities to sessions for the given peer.
348 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
351 * Handle to the network service.
353 struct GNUNET_SERVICE_Context *service;
356 * Handle to the server for this service.
358 struct GNUNET_SERVER_Handle *server;
361 * Copy of the handler array where the closures are
362 * set to this struct's instance.
364 struct GNUNET_SERVER_MessageHandler *handlers;
367 * Map of peers we have tried to contact behind a NAT
369 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
372 * List of active TCP probes.
374 struct TCPProbeContext *probe_head;
377 * List of active TCP probes.
379 struct TCPProbeContext *probe_tail;
382 * Handle for (DYN)DNS lookup of our external IP.
384 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
387 * Function to call about session status changes.
389 GNUNET_TRANSPORT_SessionInfoCallback sic;
392 * Closure for @e sic.
397 * How many more TCP sessions are we allowed to open right now?
399 unsigned long long max_connections;
402 * How many more TCP sessions do we have right now?
404 unsigned long long cur_connections;
407 * ID of task used to update our addresses when one expires.
409 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
417 * Port that we are actually listening on.
422 * Port that the user said we would have visible to the
431 * If a session monitor is attached, notify it about the new
434 * @param plugin our plugin
435 * @param session session that changed state
436 * @param state new state of the session
439 notify_session_monitor (struct Plugin *plugin,
440 struct Session *session,
441 enum GNUNET_TRANSPORT_SessionState state)
443 struct GNUNET_TRANSPORT_SessionInfo info;
445 if (NULL == plugin->sic)
447 memset (&info, 0, sizeof (info));
449 info.is_inbound = GNUNET_SYSERR; /* hard to say */
450 info.num_msg_pending = session->msgs_in_queue;
451 info.num_bytes_pending = session->bytes_in_queue;
452 if (GNUNET_SCHEDULER_NO_TASK != session->receive_delay_task)
453 info.receive_delay = session->receive_delay;
454 info.session_timeout = session->timeout;
455 info.address = session->address;
456 plugin->sic (plugin->sic_cls,
463 * Function called for a quick conversion of the binary address to
464 * a numeric address. Note that the caller must not free the
465 * address and that the next call to this function is allowed
466 * to override the address again.
468 * @param cls closure ('struct Plugin*')
469 * @param addr binary address
470 * @param addrlen length of the address
471 * @return string representing the same address
474 tcp_plugin_address_to_string (void *cls,
480 * Function to check if an inbound connection is acceptable.
481 * Mostly used to limit the total number of open connections
484 * @param cls the `struct Plugin`
485 * @param ucred credentials, if available, otherwise NULL
486 * @param addr address
487 * @param addrlen length of address
488 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
489 * for unknown address family (will be denied).
492 plugin_tcp_access_check (void *cls,
493 const struct GNUNET_CONNECTION_Credentials *ucred,
494 const struct sockaddr *addr,
497 struct Plugin *plugin = cls;
499 LOG (GNUNET_ERROR_TYPE_DEBUG,
500 "Accepting new incoming TCP connection from `%s'\n",
501 GNUNET_a2s (addr, addrlen));
502 if (plugin->cur_connections >= plugin->max_connections)
504 plugin->cur_connections++;
510 * Our external IP address/port mapping has changed.
512 * @param cls closure, the `struct Plugin`
513 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
514 * the previous (now invalid) one
515 * @param addr either the previous or the new public IP address
516 * @param addrlen actual length of @a addr
519 tcp_nat_port_map_callback (void *cls,
521 const struct sockaddr *addr,
524 struct Plugin *plugin = cls;
525 struct GNUNET_HELLO_Address *address;
526 struct IPv4TcpAddress t4;
527 struct IPv6TcpAddress t6;
531 LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
532 (GNUNET_YES == add_remove) ? "add" : "remove",
533 GNUNET_a2s (addr, addrlen));
534 /* convert 'addr' to our internal format */
535 switch (addr->sa_family)
538 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
539 memset (&t4, 0, sizeof(t4));
540 t4.options = htonl (plugin->myoptions);
541 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
542 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
547 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
548 memset (&t6, 0, sizeof(t6));
549 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
550 sizeof(struct in6_addr));
551 t6.options = htonl (plugin->myoptions);
552 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
560 /* modify our published address list */
561 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
562 (args == sizeof (struct IPv6TcpAddress)));
563 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
564 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
565 plugin->env->notify_address (plugin->env->cls, add_remove, address);
566 GNUNET_HELLO_address_free(address);
571 * Function called for a quick conversion of the binary address to
572 * a numeric address. Note that the caller must not free the
573 * address and that the next call to this function is allowed
574 * to override the address again.
576 * @param cls closure (`struct Plugin*`)
577 * @param addr binary address
578 * @param addrlen length of @a addr
579 * @return string representing the same address
582 tcp_plugin_address_to_string (void *cls,
586 static char rbuf[INET6_ADDRSTRLEN + 12];
587 char buf[INET6_ADDRSTRLEN];
591 const struct IPv4TcpAddress *t4;
592 const struct IPv6TcpAddress *t6;
599 case sizeof(struct IPv6TcpAddress):
602 port = ntohs (t6->t6_port);
603 options = ntohl (t6->options);
604 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
607 case sizeof(struct IPv4TcpAddress):
610 port = ntohs (t4->t4_port);
611 options = ntohl (t4->options);
612 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
616 LOG (GNUNET_ERROR_TYPE_WARNING,
617 _("Unexpected address length: %u bytes\n"),
618 (unsigned int) addrlen);
621 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
623 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
627 GNUNET_snprintf (rbuf, sizeof(rbuf),
628 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
638 * Function called to convert a string address to
641 * @param cls closure (`struct Plugin*`)
642 * @param addr string address
643 * @param addrlen length of the address
644 * @param buf location to store the buffer
645 * @param added location to store the number of bytes in the buffer.
646 * If the function returns #GNUNET_SYSERR, its contents are undefined.
647 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
650 tcp_plugin_string_to_address (void *cls,
656 struct sockaddr_storage socket_address;
662 /* Format tcp.options.address:port */
666 if ((NULL == addr) || (addrlen == 0))
669 return GNUNET_SYSERR;
671 if ('\0' != addr[addrlen - 1])
674 return GNUNET_SYSERR;
676 if (strlen (addr) != addrlen - 1)
679 return GNUNET_SYSERR;
681 plugin = GNUNET_strdup (addr);
682 optionstr = strchr (plugin, '.');
683 if (NULL == optionstr)
687 return GNUNET_SYSERR;
691 options = atol (optionstr);
692 address = strchr (optionstr, '.');
697 return GNUNET_SYSERR;
703 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
708 return GNUNET_SYSERR;
712 switch (socket_address.ss_family)
716 struct IPv4TcpAddress *t4;
717 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
718 t4 = GNUNET_new (struct IPv4TcpAddress);
719 t4->options = htonl (options);
720 t4->ipv4_addr = in4->sin_addr.s_addr;
721 t4->t4_port = in4->sin_port;
723 *added = sizeof(struct IPv4TcpAddress);
728 struct IPv6TcpAddress *t6;
729 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
730 t6 = GNUNET_new (struct IPv6TcpAddress);
731 t6->options = htonl (options);
732 t6->ipv6_addr = in6->sin6_addr;
733 t6->t6_port = in6->sin6_port;
735 *added = sizeof(struct IPv6TcpAddress);
739 return GNUNET_SYSERR;
745 * Find the session handle for the given client.
746 * Currently uses both the hashmap and the client
747 * context, as the client context is new and the
748 * logic still needs to be tested.
750 * @param plugin the plugin
751 * @param client which client to find the session handle for
752 * @return NULL if no matching session exists
754 static struct Session *
755 lookup_session_by_client (struct Plugin *plugin,
756 struct GNUNET_SERVER_Client *client)
758 return GNUNET_SERVER_client_get_user_context (client, struct Session);
763 * Functions with this signature are called whenever we need
764 * to close a session due to a disconnect or failure to
765 * establish a connection.
767 * @param cls the `struct Plugin`
768 * @param session session to close down
769 * @return #GNUNET_OK on success
772 tcp_plugin_disconnect_session (void *cls,
773 struct Session *session)
775 struct Plugin *plugin = cls;
776 struct PendingMessage *pm;
778 LOG (GNUNET_ERROR_TYPE_DEBUG,
779 "Disconnecting session of peer `%s' address `%s'\n",
780 GNUNET_i2s (&session->target),
781 tcp_plugin_address_to_string (NULL,
782 session->address->address,
783 session->address->address_length));
785 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
787 GNUNET_SCHEDULER_cancel (session->timeout_task);
788 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
789 session->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
793 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
797 GNUNET_STATISTICS_update (session->plugin->env->stats,
798 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
802 GNUNET_assert(GNUNET_YES ==
803 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
807 if (NULL != session->client)
808 GNUNET_SERVER_client_set_user_context (session->client,
812 if (NULL != session->transmit_handle)
814 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
815 session->transmit_handle = NULL;
817 plugin->env->unregister_quota_notification (plugin->env->cls,
821 session->plugin->env->session_end (session->plugin->env->cls,
825 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
827 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
828 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
831 while (NULL != (pm = session->pending_messages_head))
833 LOG (GNUNET_ERROR_TYPE_DEBUG,
834 (NULL != pm->transmit_cont)
835 ? "Could not deliver message to `%4s'.\n"
836 : "Could not deliver message to `%4s', notifying.\n",
837 GNUNET_i2s (&session->target));
838 GNUNET_STATISTICS_update (session->plugin->env->stats,
839 gettext_noop ("# bytes currently in TCP buffers"),
840 -(int64_t) pm->message_size, GNUNET_NO);
841 GNUNET_STATISTICS_update (session->plugin->env->stats,
842 gettext_noop ("# bytes discarded by TCP (disconnect)"),
845 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
846 session->pending_messages_tail,
848 GNUNET_assert (0 < session->msgs_in_queue);
849 session->msgs_in_queue--;
850 GNUNET_assert (pm->message_size <= session->bytes_in_queue);
851 session->bytes_in_queue -= pm->message_size;
852 if (NULL != pm->transmit_cont)
853 pm->transmit_cont (pm->transmit_cont_cls,
860 GNUNET_assert (0 == session->msgs_in_queue);
861 GNUNET_assert (0 == session->bytes_in_queue);
862 notify_session_monitor (session->plugin,
864 GNUNET_TRANSPORT_SS_UP);
866 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
868 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
869 if (NULL != session->client)
870 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
872 if (NULL != session->client)
874 GNUNET_SERVER_client_disconnect (session->client);
875 GNUNET_SERVER_client_drop (session->client);
876 session->client = NULL;
878 GNUNET_HELLO_address_free (session->address);
879 GNUNET_assert(NULL == session->transmit_handle);
880 GNUNET_free(session);
886 * Function that is called to get the keepalive factor.
887 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
888 * calculate the interval between keepalive packets.
890 * @param cls closure with the `struct Plugin`
891 * @return keepalive factor
894 tcp_plugin_query_keepalive_factor (void *cls)
901 * Session was idle, so disconnect it
903 * @param cls the `struct Session` of the idle session
904 * @param tc scheduler context
907 session_timeout (void *cls,
908 const struct GNUNET_SCHEDULER_TaskContext *tc)
910 struct Session *s = cls;
911 struct GNUNET_TIME_Relative left;
913 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
914 left = GNUNET_TIME_absolute_get_remaining (s->timeout);
915 if (0 != left.rel_value_us)
917 /* not actually our turn yet, but let's at least update
918 the monitor, it may think we're about to die ... */
919 notify_session_monitor (s->plugin,
921 GNUNET_TRANSPORT_SS_UP);
922 s->timeout_task = GNUNET_SCHEDULER_add_delayed (left,
927 LOG (GNUNET_ERROR_TYPE_DEBUG,
928 "Session %p was idle for %s, disconnecting\n",
930 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
932 /* call session destroy function */
933 tcp_plugin_disconnect_session (s->plugin, s);
938 * Increment session timeout due to activity
940 * @param s session to increment timeout for
943 reschedule_session_timeout (struct Session *s)
945 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
946 s->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
951 * Create a new session. Also queues a welcome message.
953 * @param plugin the plugin
954 * @param address the address to create the session for
955 * @param client client to use, reference counter must have already been increased
956 * @param is_nat this a NAT session, we should wait for a client to
957 * connect to us from an address, then assign that to
959 * @return new session object
961 static struct Session *
962 create_session (struct Plugin *plugin,
963 const struct GNUNET_HELLO_Address *address,
964 struct GNUNET_SERVER_Client *client,
967 struct Session *session;
968 struct PendingMessage *pm;
969 struct WelcomeMessage welcome;
971 if (GNUNET_YES != is_nat)
972 GNUNET_assert (NULL != client);
974 GNUNET_assert (NULL == client);
976 LOG (GNUNET_ERROR_TYPE_DEBUG,
977 "Creating new session for peer `%4s'\n",
978 GNUNET_i2s (&address->peer));
979 session = GNUNET_new (struct Session);
980 session->last_activity = GNUNET_TIME_absolute_get ();
981 session->plugin = plugin;
982 session->is_nat = is_nat;
983 session->client = client;
984 session->address = GNUNET_HELLO_address_copy (address);
985 session->target = address->peer;
986 session->expecting_welcome = GNUNET_YES;
987 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
988 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
989 sizeof (struct WelcomeMessage));
990 pm->msg = (const char *) &pm[1];
991 pm->message_size = sizeof(struct WelcomeMessage);
992 welcome.header.size = htons (sizeof(struct WelcomeMessage));
993 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
994 welcome.clientIdentity = *plugin->env->my_identity;
995 memcpy (&pm[1], &welcome, sizeof(welcome));
996 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
997 GNUNET_STATISTICS_update (plugin->env->stats,
998 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
1000 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
1001 session->pending_messages_tail,
1003 session->msgs_in_queue++;
1004 session->bytes_in_queue += pm->message_size;
1005 session->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1006 session->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1009 if (GNUNET_YES != is_nat)
1011 GNUNET_STATISTICS_update (plugin->env->stats,
1012 gettext_noop ("# TCP sessions active"),
1015 notify_session_monitor (session->plugin,
1017 GNUNET_TRANSPORT_SS_UP);
1021 notify_session_monitor (session->plugin,
1023 GNUNET_TRANSPORT_SS_HANDSHAKE);
1025 plugin->env->register_quota_notification (plugin->env->cls,
1034 * If we have pending messages, ask the server to
1035 * transmit them (schedule the respective tasks, etc.)
1037 * @param session for which session should we do this
1040 process_pending_messages (struct Session *session);
1044 * Function called to notify a client about the socket
1045 * being ready to queue more data. "buf" will be
1046 * NULL and "size" zero if the socket was closed for
1047 * writing in the meantime.
1049 * @param cls closure
1050 * @param size number of bytes available in @a buf
1051 * @param buf where the callee should write the message
1052 * @return number of bytes written to @a buf
1055 do_transmit (void *cls, size_t size, void *buf)
1057 struct Session *session = cls;
1058 struct GNUNET_PeerIdentity pid;
1059 struct Plugin *plugin;
1060 struct PendingMessage *pos;
1061 struct PendingMessage *hd;
1062 struct PendingMessage *tl;
1063 struct GNUNET_TIME_Absolute now;
1067 session->transmit_handle = NULL;
1068 plugin = session->plugin;
1071 LOG (GNUNET_ERROR_TYPE_DEBUG,
1072 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
1073 GNUNET_i2s (&session->target));
1074 /* timeout; cancel all messages that have already expired */
1078 now = GNUNET_TIME_absolute_get ();
1079 while ((NULL != (pos = session->pending_messages_head))
1080 && (pos->timeout.abs_value_us <= now.abs_value_us))
1082 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1083 session->pending_messages_tail,
1085 GNUNET_assert (0 < session->msgs_in_queue);
1086 session->msgs_in_queue--;
1087 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1088 session->bytes_in_queue -= pos->message_size;
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Failed to transmit %u byte message to `%4s'.\n",
1092 GNUNET_i2s (&session->target));
1093 ret += pos->message_size;
1094 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
1096 /* do this call before callbacks (so that if callbacks destroy
1097 * session, they have a chance to cancel actions done by this
1099 process_pending_messages (session);
1100 pid = session->target;
1101 /* no do callbacks and do not use session again since
1102 * the callbacks may abort the session */
1103 while (NULL != (pos = hd))
1105 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1106 if (pos->transmit_cont != NULL)
1107 pos->transmit_cont (pos->transmit_cont_cls,
1110 pos->message_size, 0);
1113 GNUNET_STATISTICS_update (plugin->env->stats,
1114 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1116 GNUNET_STATISTICS_update (plugin->env->stats,
1117 gettext_noop ("# bytes discarded by TCP (timeout)"),
1121 notify_session_monitor (session->plugin,
1123 GNUNET_TRANSPORT_SS_UP);
1126 /* copy all pending messages that would fit */
1131 while (NULL != (pos = session->pending_messages_head))
1133 if (ret + pos->message_size > size)
1135 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1136 session->pending_messages_tail,
1138 GNUNET_assert (0 < session->msgs_in_queue);
1139 session->msgs_in_queue--;
1140 GNUNET_assert (pos->message_size <= session->bytes_in_queue);
1141 session->bytes_in_queue -= pos->message_size;
1142 GNUNET_assert(size >= pos->message_size);
1143 LOG(GNUNET_ERROR_TYPE_DEBUG,
1144 "Transmitting message of type %u size %u\n",
1145 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type),
1147 /* FIXME: this memcpy can be up to 7% of our total runtime */
1148 memcpy (cbuf, pos->msg, pos->message_size);
1149 cbuf += pos->message_size;
1150 ret += pos->message_size;
1151 size -= pos->message_size;
1152 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
1154 notify_session_monitor (session->plugin,
1156 GNUNET_TRANSPORT_SS_UP);
1157 /* schedule 'continuation' before callbacks so that callbacks that
1158 * cancel everything don't cause us to use a session that no longer
1160 process_pending_messages (session);
1161 session->last_activity = GNUNET_TIME_absolute_get ();
1162 pid = session->target;
1163 /* we'll now call callbacks that may cancel the session; hence
1164 * we should not use 'session' after this point */
1165 while (NULL != (pos = hd))
1167 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1168 if (pos->transmit_cont != NULL)
1169 pos->transmit_cont (pos->transmit_cont_cls,
1173 pos->message_size); /* FIXME: include TCP overhead */
1176 GNUNET_assert (NULL == hd);
1177 GNUNET_assert (NULL == tl);
1178 LOG(GNUNET_ERROR_TYPE_DEBUG,
1179 "Transmitting %u bytes\n",
1181 GNUNET_STATISTICS_update (plugin->env->stats,
1182 gettext_noop ("# bytes currently in TCP buffers"),
1185 GNUNET_STATISTICS_update (plugin->env->stats,
1186 gettext_noop ("# bytes transmitted via TCP"),
1194 * If we have pending messages, ask the server to
1195 * transmit them (schedule the respective tasks, etc.)
1197 * @param session for which session should we do this
1200 process_pending_messages (struct Session *session)
1202 struct PendingMessage *pm;
1204 GNUNET_assert (NULL != session->client);
1205 if (NULL != session->transmit_handle)
1207 if (NULL == (pm = session->pending_messages_head))
1210 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (session->client,
1212 GNUNET_TIME_absolute_get_remaining (pm->timeout),
1219 * Function that can be used by the transport service to transmit
1220 * a message using the plugin. Note that in the case of a
1221 * peer disconnecting, the continuation MUST be called
1222 * prior to the disconnect notification itself. This function
1223 * will be called with this peer's HELLO message to initiate
1224 * a fresh connection to another peer.
1226 * @param cls closure
1227 * @param session which session must be used
1228 * @param msgbuf the message to transmit
1229 * @param msgbuf_size number of bytes in 'msgbuf'
1230 * @param priority how important is the message (most plugins will
1231 * ignore message priority and just FIFO)
1232 * @param to how long to wait at most for the transmission (does not
1233 * require plugins to discard the message after the timeout,
1234 * just advisory for the desired delay; most plugins will ignore
1236 * @param cont continuation to call once the message has
1237 * been transmitted (or if the transport is ready
1238 * for the next transmission call; or if the
1239 * peer disconnected...); can be NULL
1240 * @param cont_cls closure for @a cont
1241 * @return number of bytes used (on the physical network, with overheads);
1242 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1243 * and does NOT mean that the message was not transmitted (DV)
1246 tcp_plugin_send (void *cls,
1247 struct Session *session,
1250 unsigned int priority,
1251 struct GNUNET_TIME_Relative to,
1252 GNUNET_TRANSPORT_TransmitContinuation cont,
1255 struct Plugin * plugin = cls;
1256 struct PendingMessage *pm;
1258 /* create new message entry */
1259 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1260 pm->msg = (const char *) &pm[1];
1261 memcpy (&pm[1], msgbuf, msgbuf_size);
1262 pm->message_size = msgbuf_size;
1263 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1264 pm->transmit_cont = cont;
1265 pm->transmit_cont_cls = cont_cls;
1267 LOG(GNUNET_ERROR_TYPE_DEBUG,
1268 "Asked to transmit %u bytes to `%s', added message to list.\n",
1269 msgbuf_size, GNUNET_i2s (&session->target));
1272 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1276 GNUNET_assert (NULL != session->client);
1277 GNUNET_SERVER_client_set_timeout (session->client,
1278 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1279 GNUNET_STATISTICS_update (plugin->env->stats,
1280 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1283 /* append pm to pending_messages list */
1284 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1285 session->pending_messages_tail,
1287 notify_session_monitor (session->plugin,
1289 GNUNET_TRANSPORT_SS_UP);
1290 session->msgs_in_queue++;
1291 session->bytes_in_queue += pm->message_size;
1292 process_pending_messages (session);
1295 else if (GNUNET_YES ==
1296 GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1300 LOG (GNUNET_ERROR_TYPE_DEBUG,
1301 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1302 GNUNET_i2s (&session->target));
1303 GNUNET_STATISTICS_update (plugin->env->stats,
1304 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1306 /* append pm to pending_messages list */
1307 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1308 session->pending_messages_tail,
1310 session->msgs_in_queue++;
1311 session->bytes_in_queue += pm->message_size;
1312 notify_session_monitor (session->plugin,
1314 GNUNET_TRANSPORT_SS_HANDSHAKE);
1319 LOG(GNUNET_ERROR_TYPE_ERROR,
1320 "Invalid session %p\n",
1330 return GNUNET_SYSERR; /* session does not exist here */
1335 * Closure for #session_lookup_it().
1340 * Address we are looking for.
1342 const struct GNUNET_HELLO_Address *address;
1345 * Where to store the session (if we found it).
1347 struct Session *result;
1353 * Look for a session by address.
1355 * @param cls the `struct SessionItCtx`
1357 * @param value a `struct Session`
1358 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1361 session_lookup_it (void *cls,
1362 const struct GNUNET_PeerIdentity *key,
1365 struct SessionItCtx * si_ctx = cls;
1366 struct Session * session = value;
1368 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1370 /* Found existing session */
1371 si_ctx->result = session;
1377 * Task cleaning up a NAT connection attempt after timeout
1379 * @param cls the `struct Session`
1380 * @param tc scheduler context (unused)
1383 nat_connect_timeout (void *cls,
1384 const struct GNUNET_SCHEDULER_TaskContext *tc)
1386 struct Session *session = cls;
1388 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1389 LOG(GNUNET_ERROR_TYPE_DEBUG,
1390 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1391 GNUNET_i2s (&session->target),
1392 tcp_plugin_address_to_string (NULL,
1393 session->address->address,
1394 session->address->address_length));
1395 tcp_plugin_disconnect_session (session->plugin,
1401 * Function that will be called whenever the transport service wants to
1402 * notify the plugin that a session is still active and in use and
1403 * therefore the session timeout for this session has to be updated
1405 * @param cls closure
1406 * @param peer which peer was the session for
1407 * @param session which session is being updated
1410 tcp_plugin_update_session_timeout (void *cls,
1411 const struct GNUNET_PeerIdentity *peer,
1412 struct Session *session)
1414 reschedule_session_timeout (session);
1419 * Task to signal the server that we can continue
1420 * receiving from the TCP client now.
1422 * @param cls the `struct Session*`
1423 * @param tc task context (unused)
1426 delayed_done (void *cls,
1427 const struct GNUNET_SCHEDULER_TaskContext *tc)
1429 struct Session *session = cls;
1431 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1432 reschedule_session_timeout (session);
1434 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1439 * Function that will be called whenever the transport service wants to
1440 * notify the plugin that the inbound quota changed and that the plugin
1441 * should update it's delay for the next receive value
1443 * @param cls closure
1444 * @param peer which peer was the session for
1445 * @param session which session is being updated
1446 * @param delay new delay to use for receiving
1449 tcp_plugin_update_inbound_delay (void *cls,
1450 const struct GNUNET_PeerIdentity *peer,
1451 struct Session *session,
1452 struct GNUNET_TIME_Relative delay)
1454 if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
1456 LOG (GNUNET_ERROR_TYPE_DEBUG,
1457 "New inbound delay %s\n",
1458 GNUNET_STRINGS_relative_time_to_string (delay,
1460 session->receive_delay = GNUNET_TIME_relative_to_absolute (delay);
1461 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1462 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1469 * Create a new session to transmit data to the target
1470 * This session will used to send data to this peer and the plugin will
1471 * notify us by calling the env->session_end function
1473 * @param cls closure
1474 * @param address the address to use
1475 * @return the session if the address is valid, NULL otherwise
1477 static struct Session *
1478 tcp_plugin_get_session (void *cls,
1479 const struct GNUNET_HELLO_Address *address)
1481 struct Plugin *plugin = cls;
1482 struct Session *session = NULL;
1486 struct GNUNET_CONNECTION_Handle *sa;
1487 struct sockaddr_in a4;
1488 struct sockaddr_in6 a6;
1489 const struct IPv4TcpAddress *t4;
1490 const struct IPv6TcpAddress *t6;
1491 struct GNUNET_ATS_Information ats;
1492 unsigned int is_natd = GNUNET_NO;
1495 addrlen = address->address_length;
1496 LOG(GNUNET_ERROR_TYPE_DEBUG,
1497 "Trying to get session for `%s' address of peer `%s'\n",
1498 tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1499 GNUNET_i2s (&address->peer));
1501 if (GNUNET_HELLO_address_check_option(address, GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1507 /* look for existing session */
1508 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1511 struct SessionItCtx si_ctx;
1513 si_ctx.address = address;
1514 si_ctx.result = NULL;
1516 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1517 &address->peer, &session_lookup_it, &si_ctx);
1518 if (si_ctx.result != NULL)
1520 session = si_ctx.result;
1521 LOG(GNUNET_ERROR_TYPE_DEBUG,
1522 "Found existing session for `%s' address `%s' session %p\n",
1523 GNUNET_i2s (&address->peer),
1524 tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1528 LOG (GNUNET_ERROR_TYPE_DEBUG,
1529 "Existing sessions did not match address `%s' or peer `%s'\n",
1530 tcp_plugin_address_to_string (NULL,
1532 address->address_length),
1533 GNUNET_i2s (&address->peer));
1536 if (addrlen == sizeof(struct IPv6TcpAddress))
1538 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1539 t6 = address->address;
1541 memset (&a6, 0, sizeof(a6));
1542 #if HAVE_SOCKADDR_IN_SIN_LEN
1543 a6.sin6_len = sizeof (a6);
1545 a6.sin6_family = AF_INET6;
1546 a6.sin6_port = t6->t6_port;
1547 if (t6->t6_port == 0)
1548 is_natd = GNUNET_YES;
1549 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1553 else if (addrlen == sizeof(struct IPv4TcpAddress))
1555 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1556 t4 = address->address;
1558 memset (&a4, 0, sizeof(a4));
1559 #if HAVE_SOCKADDR_IN_SIN_LEN
1560 a4.sin_len = sizeof (a4);
1562 a4.sin_family = AF_INET;
1563 a4.sin_port = t4->t4_port;
1564 if (t4->t4_port == 0)
1565 is_natd = GNUNET_YES;
1566 a4.sin_addr.s_addr = t4->ipv4_addr;
1572 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1573 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1577 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1579 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1581 /* NAT client only works with IPv4 addresses */
1585 if (plugin->cur_connections >= plugin->max_connections)
1591 if ((is_natd == GNUNET_YES)
1593 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1596 /* Only do one NAT punch attempt per peer identity */
1600 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1601 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1604 LOG (GNUNET_ERROR_TYPE_DEBUG,
1605 "Found valid IPv4 NAT address (creating session)!\n");
1606 session = create_session (plugin, address, NULL, GNUNET_YES);
1607 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1610 session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1611 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1612 &nat_connect_timeout, session);
1613 GNUNET_assert(session != NULL);
1614 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1615 &session->target, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1617 LOG(GNUNET_ERROR_TYPE_DEBUG,
1618 "Created NAT WAIT connection to `%4s' at `%s'\n",
1619 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1621 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1625 LOG(GNUNET_ERROR_TYPE_DEBUG,
1626 "Running NAT client for `%4s' at `%s' failed\n",
1627 GNUNET_i2s (&session->target),
1628 GNUNET_a2s (sb, sbs));
1629 tcp_plugin_disconnect_session (plugin,
1635 /* create new outbound session */
1636 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1637 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1640 LOG(GNUNET_ERROR_TYPE_DEBUG,
1641 "Failed to create connection to `%4s' at `%s'\n",
1642 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1645 plugin->cur_connections++;
1646 if (plugin->cur_connections == plugin->max_connections)
1647 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1649 LOG(GNUNET_ERROR_TYPE_DEBUG,
1650 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1651 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1653 session = create_session (plugin, address,
1654 GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO);
1655 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1657 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1658 GNUNET_SERVER_client_set_user_context(session->client, session);
1659 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1660 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1661 LOG(GNUNET_ERROR_TYPE_DEBUG,
1662 "Creating new session for `%s' address `%s' session %p\n",
1663 GNUNET_i2s (&address->peer),
1664 tcp_plugin_address_to_string(NULL, address->address, address->address_length),
1666 /* Send TCP Welcome */
1667 process_pending_messages (session);
1674 session_disconnect_it (void *cls,
1675 const struct GNUNET_PeerIdentity *key,
1678 struct Plugin *plugin = cls;
1679 struct Session *session = value;
1681 GNUNET_STATISTICS_update (session->plugin->env->stats,
1682 gettext_noop ("# transport-service disconnect requests for TCP"),
1685 tcp_plugin_disconnect_session (plugin,
1692 * Function that can be called to force a disconnect from the
1693 * specified neighbour. This should also cancel all previously
1694 * scheduled transmissions. Obviously the transmission may have been
1695 * partially completed already, which is OK. The plugin is supposed
1696 * to close the connection (if applicable) and no longer call the
1697 * transmit continuation(s).
1699 * Finally, plugin MUST NOT call the services's receive function to
1700 * notify the service that the connection to the specified target was
1701 * closed after a getting this call.
1703 * @param cls closure
1704 * @param target peer for which the last transmission is
1708 tcp_plugin_disconnect (void *cls,
1709 const struct GNUNET_PeerIdentity *target)
1711 struct Plugin *plugin = cls;
1713 LOG(GNUNET_ERROR_TYPE_DEBUG,
1714 "Disconnecting peer `%4s'\n",
1715 GNUNET_i2s (target));
1716 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1718 &session_disconnect_it,
1720 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns,
1722 &session_disconnect_it,
1728 * Running pretty printers: head
1730 static struct PrettyPrinterContext *ppc_dll_head;
1733 * Running pretty printers: tail
1735 static struct PrettyPrinterContext *ppc_dll_tail;
1738 * Context for address to string conversion, closure
1739 * for #append_port().
1741 struct PrettyPrinterContext
1746 struct PrettyPrinterContext *next;
1751 struct PrettyPrinterContext *prev;
1756 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1761 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1764 * Function to call with the result.
1766 GNUNET_TRANSPORT_AddressStringCallback asc;
1769 * Clsoure for @e asc.
1774 * Port to add after the IP address.
1791 * Append our port and forward the result.
1793 * @param cls the `struct PrettyPrinterContext *`
1794 * @param hostname hostname part of the address
1797 append_port (void *cls,
1798 const char *hostname)
1800 struct PrettyPrinterContext *ppc = cls;
1803 if (NULL == hostname)
1805 /* Final call, done */
1806 ppc->resolver_handle = NULL;
1807 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
1810 ppc->asc (ppc->asc_cls,
1816 if (GNUNET_YES == ppc->ipv6)
1817 GNUNET_asprintf (&ret,
1824 GNUNET_asprintf (&ret,
1830 ppc->asc (ppc->asc_cls,
1838 * Convert the transports address to a nice, human-readable
1841 * @param cls closure
1842 * @param type name of the transport that generated the address
1843 * @param addr one of the addresses of the host, NULL for the last address
1844 * the specific address format depends on the transport
1845 * @param addrlen length of the @a addr
1846 * @param numeric should (IP) addresses be displayed in numeric form?
1847 * @param timeout after how long should we give up?
1848 * @param asc function to call on each string
1849 * @param asc_cls closure for @a asc
1852 tcp_plugin_address_pretty_printer (void *cls,
1857 struct GNUNET_TIME_Relative timeout,
1858 GNUNET_TRANSPORT_AddressStringCallback asc,
1861 struct PrettyPrinterContext *ppc;
1864 struct sockaddr_in a4;
1865 struct sockaddr_in6 a6;
1866 const struct IPv4TcpAddress *t4;
1867 const struct IPv6TcpAddress *t6;
1871 if (sizeof(struct IPv6TcpAddress) == addrlen)
1874 memset (&a6, 0, sizeof(a6));
1875 a6.sin6_family = AF_INET6;
1876 a6.sin6_port = t6->t6_port;
1877 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1878 port = ntohs (t6->t6_port);
1879 options = ntohl (t6->options);
1883 else if (sizeof(struct IPv4TcpAddress) == addrlen)
1886 memset (&a4, 0, sizeof(a4));
1887 a4.sin_family = AF_INET;
1888 a4.sin_port = t4->t4_port;
1889 a4.sin_addr.s_addr = t4->ipv4_addr;
1890 port = ntohs (t4->t4_port);
1891 options = ntohl (t4->options);
1897 /* invalid address */
1898 asc (asc_cls, NULL, GNUNET_SYSERR);
1899 asc (asc_cls, NULL, GNUNET_OK);
1902 ppc = GNUNET_new (struct PrettyPrinterContext);
1903 if (addrlen == sizeof(struct IPv6TcpAddress))
1904 ppc->ipv6 = GNUNET_YES;
1906 ppc->ipv6 = GNUNET_NO;
1908 ppc->asc_cls = asc_cls;
1910 ppc->options = options;
1911 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb,
1916 if (NULL == ppc->resolver_handle)
1922 GNUNET_CONTAINER_DLL_insert (ppc_dll_head,
1929 * Check if the given port is plausible (must be either our listen
1930 * port or our advertised port), or any port if we are behind NAT
1931 * and do not have a port open. If it is neither, we return
1934 * @param plugin global variables
1935 * @param in_port port number to check
1936 * @return #GNUNET_OK if port is either open_port or adv_port
1939 check_port (struct Plugin *plugin, uint16_t in_port)
1941 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1943 return GNUNET_SYSERR;
1947 * Function that will be called to check if a binary address for this
1948 * plugin is well-formed and corresponds to an address for THIS peer
1949 * (as per our configuration). Naturally, if absolutely necessary,
1950 * plugins can be a bit conservative in their answer, but in general
1951 * plugins should make sure that the address does not redirect
1952 * traffic to a 3rd party that might try to man-in-the-middle our
1955 * @param cls closure, our `struct Plugin *`
1956 * @param addr pointer to the address
1957 * @param addrlen length of addr
1958 * @return #GNUNET_OK if this is a plausible address for this peer
1959 * and transport, #GNUNET_SYSERR if not
1962 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1964 struct Plugin *plugin = cls;
1965 struct IPv4TcpAddress *v4;
1966 struct IPv6TcpAddress *v6;
1968 if ((addrlen != sizeof(struct IPv4TcpAddress))
1969 && (addrlen != sizeof(struct IPv6TcpAddress)))
1972 return GNUNET_SYSERR;
1975 if (addrlen == sizeof(struct IPv4TcpAddress))
1977 v4 = (struct IPv4TcpAddress *) addr;
1978 if (0 != memcmp (&v4->options,
1983 return GNUNET_SYSERR;
1985 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1986 return GNUNET_SYSERR;
1988 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1989 sizeof(struct in_addr)))
1990 return GNUNET_SYSERR;
1994 v6 = (struct IPv6TcpAddress *) addr;
1995 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1998 return GNUNET_SYSERR;
2000 if (0 != memcmp (&v6->options,
2005 return GNUNET_SYSERR;
2007 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
2008 return GNUNET_SYSERR;
2010 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
2011 sizeof(struct in6_addr)))
2012 return GNUNET_SYSERR;
2018 * We've received a nat probe from this peer via TCP. Finish
2019 * creating the client session and resume sending of queued
2022 * @param cls closure
2023 * @param client identification of the client
2024 * @param message the actual message
2027 handle_tcp_nat_probe (void *cls,
2028 struct GNUNET_SERVER_Client *client,
2029 const struct GNUNET_MessageHeader *message)
2031 struct Plugin *plugin = cls;
2032 struct Session *session;
2033 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
2036 struct IPv4TcpAddress *t4;
2037 struct IPv6TcpAddress *t6;
2038 const struct sockaddr_in *s4;
2039 const struct sockaddr_in6 *s6;
2041 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
2042 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
2043 * a connection to this peer by running gnunet-nat-client. This peer
2044 * received the punch message and now wants us to use the new connection
2045 * as the default for that peer. Do so and then send a WELCOME message
2046 * so we can really be connected!
2048 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
2051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2055 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
2056 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
2057 sizeof(struct GNUNET_PeerIdentity)))
2059 /* refuse connections from ourselves */
2060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2064 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
2065 &tcp_nat_probe->clientIdentity);
2066 if (session == NULL)
2068 LOG (GNUNET_ERROR_TYPE_DEBUG,
2069 "Did NOT find session for NAT probe!\n");
2070 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2073 LOG (GNUNET_ERROR_TYPE_DEBUG,
2074 "Found session for NAT probe!\n");
2076 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
2078 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
2079 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
2082 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2086 tcp_plugin_disconnect_session (plugin,
2091 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
2092 GNUNET_SERVER_client_set_user_context(client, session);
2093 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2094 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2095 session->last_activity = GNUNET_TIME_absolute_get ();
2096 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
2097 GNUNET_a2s (vaddr, alen));
2098 switch (((const struct sockaddr *) vaddr)->sa_family)
2102 t4 = GNUNET_new (struct IPv4TcpAddress);
2103 t4->options = htonl(0);
2104 t4->t4_port = s4->sin_port;
2105 t4->ipv4_addr = s4->sin_addr.s_addr;
2106 session->address = GNUNET_HELLO_address_allocate (
2107 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
2108 sizeof(struct IPv4TcpAddress),
2109 GNUNET_HELLO_ADDRESS_INFO_NONE);
2113 t6 = GNUNET_new (struct IPv6TcpAddress);
2114 t6->options = htonl(0);
2115 t6->t6_port = s6->sin6_port;
2116 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2117 session->address = GNUNET_HELLO_address_allocate (
2118 &tcp_nat_probe->clientIdentity,
2119 PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
2120 GNUNET_HELLO_ADDRESS_INFO_NONE);
2124 LOG(GNUNET_ERROR_TYPE_DEBUG,
2125 "Bad address for incoming connection!\n");
2127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2128 tcp_plugin_disconnect_session (plugin,
2133 GNUNET_break(NULL == session->client);
2134 GNUNET_SERVER_client_keep (client);
2135 session->client = client;
2136 GNUNET_STATISTICS_update (plugin->env->stats,
2137 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2138 process_pending_messages (session);
2139 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2143 * We've received a welcome from this peer via TCP. Possibly create a
2144 * fresh client record and send back our welcome.
2146 * @param cls closure
2147 * @param client identification of the client
2148 * @param message the actual message
2151 handle_tcp_welcome (void *cls,
2152 struct GNUNET_SERVER_Client *client,
2153 const struct GNUNET_MessageHeader *message)
2155 struct Plugin *plugin = cls;
2156 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2157 struct GNUNET_HELLO_Address *address;
2158 struct Session *session;
2161 struct IPv4TcpAddress t4;
2162 struct IPv6TcpAddress t6;
2163 const struct sockaddr_in *s4;
2164 const struct sockaddr_in6 *s6;
2165 struct GNUNET_ATS_Information ats;
2168 if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
2169 sizeof(struct GNUNET_PeerIdentity)))
2171 /* refuse connections from ourselves */
2172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2173 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2175 LOG(GNUNET_ERROR_TYPE_INFO,
2176 "Received %s message from my own identity `%4s' on address `%s'\n",
2177 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2178 GNUNET_a2s (vaddr, alen));
2184 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
2185 GNUNET_i2s (&wm->clientIdentity), client);
2186 GNUNET_STATISTICS_update (plugin->env->stats,
2187 gettext_noop ("# TCP WELCOME messages received"), 1, GNUNET_NO);
2188 session = lookup_session_by_client (plugin, client);
2189 if (NULL != session)
2191 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2193 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for peer `%s'\n",
2194 session, GNUNET_a2s (vaddr, alen));
2200 GNUNET_SERVER_client_keep (client);
2201 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2202 plugin->cur_connections++;
2203 if (plugin->cur_connections == plugin->max_connections)
2204 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2206 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2208 if (alen == sizeof(struct sockaddr_in))
2211 memset (&t4, '\0', sizeof (t4));
2212 t4.options = htonl (0);
2213 t4.t4_port = s4->sin_port;
2214 t4.ipv4_addr = s4->sin_addr.s_addr;
2215 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2216 PLUGIN_NAME, &t4, sizeof(t4),
2217 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2219 else if (alen == sizeof(struct sockaddr_in6))
2222 memset (&t6, '\0', sizeof (t6));
2223 t6.options = htonl (0);
2224 t6.t6_port = s6->sin6_port;
2225 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2226 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2227 PLUGIN_NAME, &t6, sizeof (t6),
2228 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2233 GNUNET_free_non_null (vaddr);
2236 session = create_session (plugin, address, client, GNUNET_NO);
2237 GNUNET_HELLO_address_free (address);
2238 ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2239 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
2241 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n",
2242 GNUNET_HELLO_address_check_option (session->address,
2243 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "", session,
2244 tcp_plugin_address_to_string(NULL, (void *) session->address->address,
2245 session->address->address_length),
2248 GNUNET_SERVER_client_set_user_context(session->client, session);
2249 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2250 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2252 /* Notify transport and ATS about new session */
2253 plugin->env->session_start (NULL, session->address, session, &ats, 1);
2257 LOG(GNUNET_ERROR_TYPE_DEBUG,
2258 "Did not obtain TCP socket address for incoming connection\n");
2264 if (session->expecting_welcome != GNUNET_YES)
2267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2271 session->last_activity = GNUNET_TIME_absolute_get ();
2272 session->expecting_welcome = GNUNET_NO;
2274 process_pending_messages (session);
2275 GNUNET_SERVER_client_set_timeout (client,
2276 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2277 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2282 * We've received data for this peer via TCP. Unbox,
2283 * compute latency and forward.
2285 * @param cls closure
2286 * @param client identification of the client
2287 * @param message the actual message
2290 handle_tcp_data (void *cls,
2291 struct GNUNET_SERVER_Client *client,
2292 const struct GNUNET_MessageHeader *message)
2294 struct Plugin *plugin = cls;
2295 struct Session *session;
2296 struct GNUNET_TIME_Relative delay;
2299 type = ntohs (message->type);
2300 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2301 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2303 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2304 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2307 session = lookup_session_by_client (plugin, client);
2308 if (NULL == session)
2310 /* No inbound session found */
2314 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2315 LOG(GNUNET_ERROR_TYPE_ERROR,
2316 "Received unexpected %u bytes of type %u from `%s'\n",
2317 (unsigned int) ntohs (message->size),
2318 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2321 GNUNET_free_non_null(vaddr);
2324 else if (GNUNET_YES == session->expecting_welcome)
2326 /* Session is expecting WELCOME message */
2330 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2331 LOG(GNUNET_ERROR_TYPE_ERROR,
2332 "Received unexpected %u bytes of type %u from `%s'\n",
2333 (unsigned int) ntohs (message->size),
2334 (unsigned int) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2337 GNUNET_free_non_null(vaddr);
2341 session->last_activity = GNUNET_TIME_absolute_get ();
2342 LOG(GNUNET_ERROR_TYPE_DEBUG,
2343 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2344 (unsigned int) ntohs (message->size),
2345 (unsigned int) ntohs (message->type), GNUNET_i2s (&session->target));
2347 GNUNET_STATISTICS_update (plugin->env->stats,
2348 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2350 struct GNUNET_ATS_Information distance;
2352 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2353 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2354 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2356 GNUNET_assert(GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2360 delay = plugin->env->receive (plugin->env->cls,
2364 plugin->env->update_address_metrics (plugin->env->cls,
2368 reschedule_session_timeout (session);
2369 if (0 == delay.rel_value_us)
2371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2375 LOG(GNUNET_ERROR_TYPE_DEBUG,
2376 "Throttling receiving from `%s' for %s\n",
2377 GNUNET_i2s (&session->target),
2378 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2379 GNUNET_SERVER_disable_receive_done_warning (client);
2380 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2381 &delayed_done, session);
2387 * Functions with this signature are called whenever a peer
2388 * is disconnected on the network level.
2390 * @param cls closure
2391 * @param client identification of the client
2394 disconnect_notify (void *cls,
2395 struct GNUNET_SERVER_Client *client)
2397 struct Plugin *plugin = cls;
2398 struct Session *session;
2402 session = lookup_session_by_client (plugin, client);
2403 if (NULL == session)
2404 return; /* unknown, nothing to do */
2405 LOG (GNUNET_ERROR_TYPE_DEBUG,
2406 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2407 GNUNET_i2s (&session->target),
2408 tcp_plugin_address_to_string (session->plugin, session->address->address,
2409 session->address->address_length));
2411 if (plugin->cur_connections == plugin->max_connections)
2412 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2414 if (plugin->cur_connections < 1)
2417 plugin->cur_connections--;
2419 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2420 ("# network-level TCP disconnect events"),
2423 tcp_plugin_disconnect_session (plugin, session);
2428 * We can now send a probe message, copy into buffer to really send.
2430 * @param cls closure, a struct TCPProbeContext
2431 * @param size max size to copy
2432 * @param buf buffer to copy message to
2433 * @return number of bytes copied into @a buf
2436 notify_send_probe (void *cls,
2440 struct TCPProbeContext *tcp_probe_ctx = cls;
2441 struct Plugin *plugin = tcp_probe_ctx->plugin;
2444 tcp_probe_ctx->transmit_handle = NULL;
2445 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2450 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2451 GNUNET_free(tcp_probe_ctx);
2454 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2455 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2456 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2457 tcp_probe_ctx->sock);
2458 ret = sizeof(tcp_probe_ctx->message);
2459 GNUNET_free(tcp_probe_ctx);
2465 * Function called by the NAT subsystem suggesting another peer wants
2466 * to connect to us via connection reversal. Try to connect back to the
2469 * @param cls closure
2470 * @param addr address to try
2471 * @param addrlen number of bytes in @a addr
2474 try_connection_reversal (void *cls,
2475 const struct sockaddr *addr,
2478 struct Plugin *plugin = cls;
2479 struct GNUNET_CONNECTION_Handle *sock;
2480 struct TCPProbeContext *tcp_probe_ctx;
2483 * We have received an ICMP response, ostensibly from a peer
2484 * that wants to connect to us! Send a message to establish a connection.
2486 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2489 /* failed for some odd reason (out of sockets?); ignore attempt */
2493 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2494 tcp_probe_ctx->message.header.size
2495 = htons (sizeof (struct TCP_NAT_ProbeMessage));
2496 tcp_probe_ctx->message.header.type
2497 = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2498 tcp_probe_ctx->message.clientIdentity
2499 = *plugin->env->my_identity;
2500 tcp_probe_ctx->plugin = plugin;
2501 tcp_probe_ctx->sock = sock;
2502 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2505 tcp_probe_ctx->transmit_handle
2506 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2507 ntohs (tcp_probe_ctx->message.header.size),
2508 GNUNET_TIME_UNIT_FOREVER_REL,
2515 * Function obtain the network type for a session
2517 * @param cls closure ('struct Plugin*')
2518 * @param session the session
2519 * @return the network type in HBO or #GNUNET_SYSERR
2521 static enum GNUNET_ATS_Network_Type
2522 tcp_plugin_get_network (void *cls,
2523 struct Session *session)
2525 return session->ats_address_network_type;
2530 * Return information about the given session to the
2533 * @param cls the `struct Plugin` with the monitor callback (`sic`)
2534 * @param peer peer we send information about
2535 * @param value our `struct Session` to send information about
2536 * @return #GNUNET_OK (continue to iterate)
2539 send_session_info_iter (void *cls,
2540 const struct GNUNET_PeerIdentity *peer,
2543 struct Plugin *plugin = cls;
2544 struct Session *session = value;
2546 notify_session_monitor (plugin,
2548 GNUNET_TRANSPORT_SS_UP);
2554 * Begin monitoring sessions of a plugin. There can only
2555 * be one active monitor per plugin (i.e. if there are
2556 * multiple monitors, the transport service needs to
2557 * multiplex the generated events over all of them).
2559 * @param cls closure of the plugin
2560 * @param sic callback to invoke, NULL to disable monitor;
2561 * plugin will being by iterating over all active
2562 * sessions immediately and then enter monitor mode
2563 * @param sic_cls closure for @a sic
2566 tcp_plugin_setup_monitor (void *cls,
2567 GNUNET_TRANSPORT_SessionInfoCallback sic,
2570 struct Plugin *plugin = cls;
2573 plugin->sic_cls = sic_cls;
2576 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2577 &send_session_info_iter,
2579 /* signal end of first iteration */
2580 sic (sic_cls, NULL, NULL);
2586 * Entry point for the plugin.
2588 * @param cls closure, the `struct GNUNET_TRANSPORT_PluginEnvironment *`
2589 * @return the `struct GNUNET_TRANSPORT_PluginFunctions *` or NULL on error
2592 libgnunet_plugin_transport_tcp_init (void *cls)
2594 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2595 { &handle_tcp_welcome, NULL,
2596 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, sizeof(struct WelcomeMessage) },
2597 { &handle_tcp_nat_probe, NULL,
2598 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof(struct TCP_NAT_ProbeMessage) },
2599 { &handle_tcp_data, NULL,
2600 GNUNET_MESSAGE_TYPE_ALL, 0 },
2601 { NULL, NULL, 0, 0 }
2603 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2604 struct GNUNET_TRANSPORT_PluginFunctions *api;
2605 struct Plugin *plugin;
2606 struct GNUNET_SERVICE_Context *service;
2607 unsigned long long aport;
2608 unsigned long long bport;
2609 unsigned long long max_connections;
2611 struct GNUNET_TIME_Relative idle_timeout;
2614 struct sockaddr **addrs;
2615 socklen_t *addrlens;
2617 if (NULL == env->receive)
2619 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2620 initialze the plugin or the API */
2621 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2623 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2624 api->address_to_string = &tcp_plugin_address_to_string;
2625 api->string_to_address = &tcp_plugin_string_to_address;
2629 GNUNET_assert (NULL != env->cfg);
2631 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2635 max_connections = 128;
2639 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2640 "PORT", &bport)) || (bport > 65535)
2642 == GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2643 "ADVERTISED-PORT", &aport)) && (aport > 65535)))
2645 LOG(GNUNET_ERROR_TYPE_ERROR,
2646 _("Require valid port number for service `%s' in configuration!\n"),
2656 service = GNUNET_SERVICE_start ("transport-tcp",
2658 GNUNET_SERVICE_OPTION_NONE);
2659 if (service == NULL)
2661 LOG (GNUNET_ERROR_TYPE_WARNING,
2662 _("Failed to start service.\n"));
2669 plugin = GNUNET_new (struct Plugin);
2670 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2672 plugin->max_connections = max_connections;
2673 plugin->open_port = bport;
2674 plugin->adv_port = aport;
2676 if ( (service != NULL) &&
2679 GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2684 for (ret = ret_s-1; ret >= 0; ret--)
2685 LOG (GNUNET_ERROR_TYPE_INFO,
2686 "Binding to address `%s'\n",
2687 GNUNET_a2s (addrs[ret], addrlens[ret]));
2689 = GNUNET_NAT_register (env->cfg,
2692 (unsigned int) ret_s,
2693 (const struct sockaddr **) addrs, addrlens,
2694 &tcp_nat_port_map_callback,
2695 &try_connection_reversal,
2697 for (ret = ret_s -1; ret >= 0; ret--)
2698 GNUNET_free (addrs[ret]);
2699 GNUNET_free_non_null (addrs);
2700 GNUNET_free_non_null (addrlens);
2704 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2705 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2706 &try_connection_reversal, plugin);
2708 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2710 api->send = &tcp_plugin_send;
2711 api->get_session = &tcp_plugin_get_session;
2712 api->disconnect_session = &tcp_plugin_disconnect_session;
2713 api->query_keepalive_factor = &tcp_plugin_query_keepalive_factor;
2714 api->disconnect_peer = &tcp_plugin_disconnect;
2715 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2716 api->check_address = &tcp_plugin_check_address;
2717 api->address_to_string = &tcp_plugin_address_to_string;
2718 api->string_to_address = &tcp_plugin_string_to_address;
2719 api->get_network = &tcp_plugin_get_network;
2720 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2721 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2722 api->setup_monitor = &tcp_plugin_setup_monitor;
2723 plugin->service = service;
2724 if (NULL != service)
2726 plugin->server = GNUNET_SERVICE_get_server (service);
2731 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2736 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2739 if (NULL != plugin->nat)
2740 GNUNET_NAT_unregister (plugin->nat);
2741 GNUNET_free(plugin);
2746 = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check,
2748 idle_timeout, GNUNET_YES);
2750 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2751 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2752 for (i = 0;i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);i++)
2753 plugin->handlers[i].callback_cls = plugin;
2755 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2756 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2757 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2760 LOG (GNUNET_ERROR_TYPE_INFO,
2761 _("TCP transport listening on port %llu\n"),
2764 LOG (GNUNET_ERROR_TYPE_INFO,
2765 _("TCP transport not listening on any port (client only)\n"));
2766 if ( (aport != bport) &&
2768 LOG (GNUNET_ERROR_TYPE_INFO,
2769 _("TCP transport advertises itself as being on port %llu\n"),
2771 /* Initially set connections to 0 */
2772 GNUNET_assert(NULL != plugin->env->stats);
2773 GNUNET_STATISTICS_set (plugin->env->stats,
2774 gettext_noop ("# TCP sessions active"),
2782 * Exit point from the plugin.
2784 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2788 libgnunet_plugin_transport_tcp_done (void *cls)
2790 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2791 struct Plugin *plugin = api->cls;
2792 struct TCPProbeContext *tcp_probe;
2793 struct PrettyPrinterContext *cur;
2794 struct PrettyPrinterContext *next;
2801 LOG (GNUNET_ERROR_TYPE_DEBUG,
2802 "Shutting down TCP plugin\n");
2804 /* Removing leftover sessions */
2805 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2806 &session_disconnect_it,
2808 /* Removing leftover NAT sessions */
2809 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2810 &session_disconnect_it,
2813 next = ppc_dll_head;
2814 for (cur = next; NULL != cur; cur = next)
2818 GNUNET_CONTAINER_DLL_remove (ppc_dll_head,
2821 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2825 if (NULL != plugin->service)
2826 GNUNET_SERVICE_stop (plugin->service);
2828 GNUNET_SERVER_destroy (plugin->server);
2829 GNUNET_free(plugin->handlers);
2830 if (NULL != plugin->nat)
2831 GNUNET_NAT_unregister (plugin->nat);
2832 while (NULL != (tcp_probe = plugin->probe_head))
2834 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2837 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2838 GNUNET_free(tcp_probe);
2840 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2841 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2842 GNUNET_free(plugin);
2847 /* end of plugin_transport_tcp.c */