2 This file is part of GNUnet
3 (C) 2002--2012 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_connection_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_nat_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_service_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-tcp",__VA_ARGS__)
45 * How long until we give up on establishing an NAT connection?
48 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
51 GNUNET_NETWORK_STRUCT_BEGIN
54 * Initial handshake message for a session.
59 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
61 struct GNUNET_MessageHeader header;
64 * Identity of the node connecting (TCP client)
66 struct GNUNET_PeerIdentity clientIdentity;
72 * Basically a WELCOME message, but with the purpose
73 * of giving the waiting peer a client handle to use
75 struct TCP_NAT_ProbeMessage
78 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
80 struct GNUNET_MessageHeader header;
83 * Identity of the sender of the message.
85 struct GNUNET_PeerIdentity clientIdentity;
88 GNUNET_NETWORK_STRUCT_END
91 * Context for sending a NAT probe via TCP.
93 struct TCPProbeContext
97 * Active probes are kept in a DLL.
99 struct TCPProbeContext *next;
102 * Active probes are kept in a DLL.
104 struct TCPProbeContext *prev;
109 struct GNUNET_CONNECTION_Handle *sock;
112 * Message to be sent.
114 struct TCP_NAT_ProbeMessage message;
117 * Handle to the transmission.
119 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
122 * Transport plugin handle.
124 struct Plugin *plugin;
128 GNUNET_NETWORK_STRUCT_BEGIN
131 * Network format for IPv4 addresses.
133 struct IPv4TcpAddress
136 * IPv4 address, in network byte order.
138 uint32_t ipv4_addr GNUNET_PACKED;
141 * Port number, in network byte order.
143 uint16_t t4_port GNUNET_PACKED;
149 * Network format for IPv6 addresses.
151 struct IPv6TcpAddress
156 struct in6_addr ipv6_addr GNUNET_PACKED;
159 * Port number, in network byte order.
161 uint16_t t6_port GNUNET_PACKED;
164 GNUNET_NETWORK_STRUCT_END
167 * Encapsulation of all of the state of the plugin.
173 * Information kept for each message that is yet to
176 struct PendingMessage
180 * This is a doubly-linked list.
182 struct PendingMessage *next;
185 * This is a doubly-linked list.
187 struct PendingMessage *prev;
190 * The pending message
195 * Continuation function to call once the message
196 * has been sent. Can be NULL if there is no
197 * continuation to call.
199 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
202 * Closure for transmit_cont.
204 void *transmit_cont_cls;
207 * Timeout value for the pending message.
209 struct GNUNET_TIME_Absolute timeout;
212 * So that the gnunet-service-transport can group messages together,
213 * these pending messages need to accept a message buffer and size
214 * instead of just a GNUNET_MessageHeader.
222 * Session handle for TCP connections.
227 * To whom are we talking to (set to our identity
228 * if we are still waiting for the welcome message)
230 struct GNUNET_PeerIdentity target;
235 struct SessionHeader header;
238 * Pointer to the global plugin struct.
240 struct Plugin *plugin;
243 * The client (used to identify this connection)
245 struct GNUNET_SERVER_Client *client;
248 * Task cleaning up a NAT client connection establishment attempt;
250 GNUNET_SCHEDULER_TaskIdentifier nat_connection_timeout;
253 * Messages currently pending for transmission
254 * to this peer, if any.
256 struct PendingMessage *pending_messages_head;
259 * Messages currently pending for transmission
260 * to this peer, if any.
262 struct PendingMessage *pending_messages_tail;
265 * Handle for pending transmission request.
267 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
270 * ID of task used to delay receiving more to throttle sender.
272 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
275 * Session timeout task
277 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
280 * Address of the other peer (either based on our 'connect'
281 * call or on our 'accept' call).
283 * struct IPv4TcpAddress or struct IPv6TcpAddress
289 * Length of connect_addr.
294 * Last activity on this connection. Used to select preferred
297 struct GNUNET_TIME_Absolute last_activity;
300 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
302 int expecting_welcome;
305 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
310 * Was this session created using NAT traversal?
315 * ATS network type in NBO
317 uint32_t ats_address_network_type;
322 * Encapsulation of all of the state of the plugin.
329 struct GNUNET_TRANSPORT_PluginEnvironment *env;
334 struct GNUNET_CONNECTION_Handle *lsock;
337 * Our handle to the NAT module.
339 struct GNUNET_NAT_Handle *nat;
342 * Map from peer identities to sessions for the given peer.
344 struct GNUNET_CONTAINER_MultiHashMap *sessionmap;
347 * Handle to the network service.
349 struct GNUNET_SERVICE_Context *service;
352 * Handle to the server for this service.
354 struct GNUNET_SERVER_Handle *server;
357 * Copy of the handler array where the closures are
358 * set to this struct's instance.
360 struct GNUNET_SERVER_MessageHandler *handlers;
363 * Map of peers we have tried to contact behind a NAT
365 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
368 * List of active TCP probes.
370 struct TCPProbeContext *probe_head;
373 * List of active TCP probes.
375 struct TCPProbeContext *probe_tail;
378 * Handle for (DYN)DNS lookup of our external IP.
380 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
383 * How many more TCP sessions are we allowed to open right now?
385 unsigned long long max_connections;
388 * ID of task used to update our addresses when one expires.
390 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
393 * Port that we are actually listening on.
398 * Port that the user said we would have visible to the
407 * Start session timeout
410 start_session_timeout (struct Session *s);
414 * Increment session timeout due to activity
417 reschedule_session_timeout (struct Session *s);
424 stop_session_timeout (struct Session *s);
429 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
432 static unsigned int sessions;
436 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
439 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
440 if (sessions != size)
441 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
443 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
447 GNUNET_i2s (&session->target),
448 tcp_address_to_string (NULL, session->addr, session->addrlen));
453 dec_sessions (struct Plugin *plugin, struct Session *session, int line)
455 GNUNET_assert (sessions > 0);
456 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
458 if (sessions != size)
459 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
461 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
465 GNUNET_i2s (&session->target),
466 tcp_address_to_string (NULL, session->addr, session->addrlen));
472 * Function to check if an inbound connection is acceptable.
473 * Mostly used to limit the total number of open connections
476 * @param cls the 'struct Plugin'
477 * @param ucred credentials, if available, otherwise NULL
478 * @param addr address
479 * @param addrlen length of address
480 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
481 * for unknown address family (will be denied).
484 plugin_tcp_access_check (void *cls,
485 const struct GNUNET_CONNECTION_Credentials *ucred,
486 const struct sockaddr *addr, socklen_t addrlen)
488 struct Plugin *plugin = cls;
490 LOG (GNUNET_ERROR_TYPE_ERROR,
491 "Accepting new incoming TCP connection from `%s'\n",
492 GNUNET_a2s (addr, addrlen));
493 if (0 == plugin->max_connections)
495 plugin->max_connections--;
501 * Our external IP address/port mapping has changed.
503 * @param cls closure, the 'struct LocalAddrList'
504 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
505 * the previous (now invalid) one
506 * @param addr either the previous or the new public IP address
507 * @param addrlen actual lenght of the address
510 tcp_nat_port_map_callback (void *cls, int add_remove,
511 const struct sockaddr *addr, socklen_t addrlen)
513 struct Plugin *plugin = cls;
514 struct IPv4TcpAddress t4;
515 struct IPv6TcpAddress t6;
519 LOG (GNUNET_ERROR_TYPE_DEBUG,
520 "NPMC called with %d for address `%s'\n", add_remove,
521 GNUNET_a2s (addr, addrlen));
522 /* convert 'addr' to our internal format */
523 switch (addr->sa_family)
526 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
527 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
528 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
533 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
534 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
535 sizeof (struct in6_addr));
536 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
544 /* modify our published address list */
545 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
550 * Function called for a quick conversion of the binary address to
551 * a numeric address. Note that the caller must not free the
552 * address and that the next call to this function is allowed
553 * to override the address again.
555 * @param cls closure ('struct Plugin*')
556 * @param addr binary address
557 * @param addrlen length of the address
558 * @return string representing the same address
561 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
563 static char rbuf[INET6_ADDRSTRLEN + 12];
564 char buf[INET6_ADDRSTRLEN];
568 const struct IPv4TcpAddress *t4;
569 const struct IPv6TcpAddress *t6;
575 case sizeof (struct IPv6TcpAddress):
578 port = ntohs (t6->t6_port);
579 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
582 case sizeof (struct IPv4TcpAddress):
585 port = ntohs (t4->t4_port);
586 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
590 LOG (GNUNET_ERROR_TYPE_ERROR,
591 _("Unexpected address length: %u bytes\n"),
592 (unsigned int) addrlen);
596 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
598 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
601 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
608 * Function called to convert a string address to
611 * @param cls closure ('struct Plugin*')
612 * @param addr string address
613 * @param addrlen length of the address
614 * @param buf location to store the buffer
615 * @param added location to store the number of bytes in the buffer.
616 * If the function returns GNUNET_SYSERR, its contents are undefined.
617 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
620 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
621 void **buf, size_t *added)
623 struct sockaddr_storage socket_address;
625 if ((NULL == addr) || (addrlen == 0))
628 return GNUNET_SYSERR;
630 if ('\0' != addr[addrlen - 1])
633 return GNUNET_SYSERR;
635 if (strlen (addr) != addrlen - 1)
638 return GNUNET_SYSERR;
641 GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
645 return GNUNET_SYSERR;
647 switch (socket_address.ss_family)
651 struct IPv4TcpAddress *t4;
652 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
654 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
655 t4->ipv4_addr = in4->sin_addr.s_addr;
656 t4->t4_port = in4->sin_port;
658 *added = sizeof (struct IPv4TcpAddress);
663 struct IPv6TcpAddress *t6;
664 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
665 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
666 t6->ipv6_addr = in6->sin6_addr;
667 t6->t6_port = in6->sin6_port;
669 *added = sizeof (struct IPv6TcpAddress);
673 return GNUNET_SYSERR;
678 struct SessionClientCtx
680 const struct GNUNET_SERVER_Client *client;
686 session_lookup_by_client_it (void *cls,
687 const struct GNUNET_HashCode * key,
690 struct SessionClientCtx *sc_ctx = cls;
691 struct Session *s = value;
693 if (s->client == sc_ctx->client)
703 * Find the session handle for the given client.
705 * @param plugin the plugin
706 * @param client which client to find the session handle for
707 * @return NULL if no matching session exists
709 static struct Session *
710 lookup_session_by_client (struct Plugin *plugin,
711 const struct GNUNET_SERVER_Client *client)
713 struct SessionClientCtx sc_ctx;
715 sc_ctx.client = client;
717 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
723 * Create a new session. Also queues a welcome message.
725 * @param plugin the plugin
726 * @param target peer to connect to
727 * @param client client to use, reference counter must have already been increased
728 * @param is_nat this a NAT session, we should wait for a client to
729 * connect to us from an address, then assign that to
731 * @return new session object
733 static struct Session *
734 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
735 struct GNUNET_SERVER_Client *client, int is_nat)
737 struct Session *session;
738 struct PendingMessage *pm;
739 struct WelcomeMessage welcome;
741 if (GNUNET_YES != is_nat)
742 GNUNET_assert (NULL != client);
744 GNUNET_assert (NULL == client);
746 LOG (GNUNET_ERROR_TYPE_DEBUG,
747 "Creating new session for peer `%4s'\n",
748 GNUNET_i2s (target));
749 session = GNUNET_malloc (sizeof (struct Session));
750 session->last_activity = GNUNET_TIME_absolute_get ();
751 session->plugin = plugin;
752 session->is_nat = is_nat;
753 session->client = client;
754 session->target = *target;
755 session->expecting_welcome = GNUNET_YES;
756 session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
757 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
758 sizeof (struct WelcomeMessage));
759 pm->msg = (const char *) &pm[1];
760 pm->message_size = sizeof (struct WelcomeMessage);
761 welcome.header.size = htons (sizeof (struct WelcomeMessage));
762 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
763 welcome.clientIdentity = *plugin->env->my_identity;
764 memcpy (&pm[1], &welcome, sizeof (welcome));
765 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
766 GNUNET_STATISTICS_update (plugin->env->stats,
767 gettext_noop ("# bytes currently in TCP buffers"),
768 pm->message_size, GNUNET_NO);
769 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
770 session->pending_messages_tail, pm);
771 if (GNUNET_YES != is_nat)
773 GNUNET_STATISTICS_update (plugin->env->stats,
774 gettext_noop ("# TCP sessions active"), 1,
777 start_session_timeout (session);
784 * If we have pending messages, ask the server to
785 * transmit them (schedule the respective tasks, etc.)
787 * @param session for which session should we do this
790 process_pending_messages (struct Session *session);
794 * Function called to notify a client about the socket
795 * being ready to queue more data. "buf" will be
796 * NULL and "size" zero if the socket was closed for
797 * writing in the meantime.
800 * @param size number of bytes available in buf
801 * @param buf where the callee should write the message
802 * @return number of bytes written to buf
805 do_transmit (void *cls, size_t size, void *buf)
807 struct Session *session = cls;
808 struct GNUNET_PeerIdentity pid;
809 struct Plugin *plugin;
810 struct PendingMessage *pos;
811 struct PendingMessage *hd;
812 struct PendingMessage *tl;
813 struct GNUNET_TIME_Absolute now;
817 GNUNET_assert (NULL != session);
818 session->transmit_handle = NULL;
819 plugin = session->plugin;
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
824 GNUNET_i2s (&session->target));
825 /* timeout; cancel all messages that have already expired */
829 now = GNUNET_TIME_absolute_get ();
830 while ((NULL != (pos = session->pending_messages_head)) &&
831 (pos->timeout.abs_value <= now.abs_value))
833 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
834 session->pending_messages_tail, pos);
835 LOG (GNUNET_ERROR_TYPE_DEBUG,
836 "Failed to transmit %u byte message to `%4s'.\n",
837 pos->message_size, GNUNET_i2s (&session->target));
838 ret += pos->message_size;
839 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
841 /* do this call before callbacks (so that if callbacks destroy
842 * session, they have a chance to cancel actions done by this
844 process_pending_messages (session);
845 pid = session->target;
846 /* no do callbacks and do not use session again since
847 * the callbacks may abort the session */
848 while (NULL != (pos = hd))
850 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
851 if (pos->transmit_cont != NULL)
852 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
855 GNUNET_STATISTICS_update (plugin->env->stats,
856 gettext_noop ("# bytes currently in TCP buffers"),
857 -(int64_t) ret, GNUNET_NO);
858 GNUNET_STATISTICS_update (plugin->env->stats,
860 ("# bytes discarded by TCP (timeout)"), ret,
864 /* copy all pending messages that would fit */
869 while (NULL != (pos = session->pending_messages_head))
871 if (ret + pos->message_size > size)
873 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
874 session->pending_messages_tail, pos);
875 GNUNET_assert (size >= pos->message_size);
876 LOG (GNUNET_ERROR_TYPE_DEBUG,
877 "Transmitting message of type %u\n",
878 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
879 /* FIXME: this memcpy can be up to 7% of our total runtime */
880 memcpy (cbuf, pos->msg, pos->message_size);
881 cbuf += pos->message_size;
882 ret += pos->message_size;
883 size -= pos->message_size;
884 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
886 /* schedule 'continuation' before callbacks so that callbacks that
887 * cancel everything don't cause us to use a session that no longer
889 process_pending_messages (session);
890 session->last_activity = GNUNET_TIME_absolute_get ();
891 pid = session->target;
892 /* we'll now call callbacks that may cancel the session; hence
893 * we should not use 'session' after this point */
894 while (NULL != (pos = hd))
896 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
897 if (pos->transmit_cont != NULL)
898 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
901 GNUNET_assert (hd == NULL);
902 GNUNET_assert (tl == NULL);
903 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
905 GNUNET_STATISTICS_update (plugin->env->stats,
906 gettext_noop ("# bytes currently in TCP buffers"),
907 -(int64_t) ret, GNUNET_NO);
908 GNUNET_STATISTICS_update (plugin->env->stats,
909 gettext_noop ("# bytes transmitted via TCP"), ret,
916 * If we have pending messages, ask the server to
917 * transmit them (schedule the respective tasks, etc.)
919 * @param session for which session should we do this
922 process_pending_messages (struct Session *session)
924 struct PendingMessage *pm;
926 GNUNET_assert (session->client != NULL);
927 if (session->transmit_handle != NULL)
929 if (NULL == (pm = session->pending_messages_head))
932 session->transmit_handle =
933 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
934 GNUNET_TIME_absolute_get_remaining
935 (pm->timeout), &do_transmit,
941 * Functions with this signature are called whenever we need
942 * to close a session due to a disconnect or failure to
943 * establish a connection.
945 * @param session session to close down
948 disconnect_session (struct Session *session)
950 struct PendingMessage *pm;
951 struct Plugin * plugin = session->plugin;
953 LOG (GNUNET_ERROR_TYPE_DEBUG,
954 "Disconnecting session of peer `%s' address `%s'\n",
955 GNUNET_i2s (&session->target),
956 tcp_address_to_string (NULL, session->addr, session->addrlen));
958 stop_session_timeout (session);
960 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->sessionmap, &session->target.hashPubKey, session))
962 GNUNET_STATISTICS_update (session->plugin->env->stats,
963 gettext_noop ("# TCP sessions active"), -1,
965 dec_sessions (plugin, session, __LINE__);
967 else GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->nat_wait_conns, &session->target.hashPubKey, session));
970 if (session->transmit_handle != NULL)
972 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
973 session->transmit_handle = NULL;
975 session->plugin->env->session_end (session->plugin->env->cls,
976 &session->target, session);
978 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
980 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
981 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
984 while (NULL != (pm = session->pending_messages_head))
986 LOG (GNUNET_ERROR_TYPE_DEBUG,
988 NULL ? "Could not deliver message to `%4s'.\n" :
989 "Could not deliver message to `%4s', notifying.\n",
990 GNUNET_i2s (&session->target));
991 GNUNET_STATISTICS_update (session->plugin->env->stats,
992 gettext_noop ("# bytes currently in TCP buffers"),
993 -(int64_t) pm->message_size, GNUNET_NO);
994 GNUNET_STATISTICS_update (session->plugin->env->stats,
996 ("# bytes discarded by TCP (disconnect)"),
997 pm->message_size, GNUNET_NO);
998 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
999 session->pending_messages_tail, pm);
1000 if (NULL != pm->transmit_cont)
1001 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1002 GNUNET_SYSERR, pm->message_size, 0);
1005 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1007 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1008 if (NULL != session->client)
1009 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1011 if (NULL != session->client)
1013 GNUNET_SERVER_client_disconnect (session->client);
1014 GNUNET_SERVER_client_drop (session->client);
1015 session->client = NULL;
1017 GNUNET_free_non_null (session->addr);
1018 GNUNET_assert (NULL == session->transmit_handle);
1019 GNUNET_free (session);
1023 struct FindSessionContext
1029 int session_it (void *cls,
1030 const struct GNUNET_HashCode * key,
1033 struct FindSessionContext *res = cls;
1034 if (res->s == value)
1036 res->res = GNUNET_OK;
1043 int find_session (struct Plugin *plugin, struct Session *session)
1045 struct FindSessionContext session_map_res;
1046 struct FindSessionContext nat_map_res;
1048 session_map_res.s = session;
1049 session_map_res.res = GNUNET_SYSERR;
1050 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1052 nat_map_res.s = session;
1053 nat_map_res.res = GNUNET_SYSERR;
1054 GNUNET_CONTAINER_multihashmap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1056 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1059 return GNUNET_SYSERR;
1066 * Function that can be used by the transport service to transmit
1067 * a message using the plugin. Note that in the case of a
1068 * peer disconnecting, the continuation MUST be called
1069 * prior to the disconnect notification itself. This function
1070 * will be called with this peer's HELLO message to initiate
1071 * a fresh connection to another peer.
1073 * @param cls closure
1074 * @param session which session must be used
1075 * @param msgbuf the message to transmit
1076 * @param msgbuf_size number of bytes in 'msgbuf'
1077 * @param priority how important is the message (most plugins will
1078 * ignore message priority and just FIFO)
1079 * @param to how long to wait at most for the transmission (does not
1080 * require plugins to discard the message after the timeout,
1081 * just advisory for the desired delay; most plugins will ignore
1083 * @param cont continuation to call once the message has
1084 * been transmitted (or if the transport is ready
1085 * for the next transmission call; or if the
1086 * peer disconnected...); can be NULL
1087 * @param cont_cls closure for cont
1088 * @return number of bytes used (on the physical network, with overheads);
1089 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1090 * and does NOT mean that the message was not transmitted (DV)
1093 tcp_plugin_send (void *cls,
1094 struct Session *session,
1095 const char *msgbuf, size_t msgbuf_size,
1096 unsigned int priority,
1097 struct GNUNET_TIME_Relative to,
1098 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1100 struct Plugin * plugin = cls;
1101 struct PendingMessage *pm;
1103 GNUNET_assert (NULL != plugin);
1104 GNUNET_assert (NULL != session);
1106 if (GNUNET_SYSERR == find_session(plugin, session))
1108 LOG (GNUNET_ERROR_TYPE_ERROR,
1109 _("Trying to send with invalid session %p\n"));
1110 return GNUNET_SYSERR;
1113 /* create new message entry */
1114 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1115 pm->msg = (const char *) &pm[1];
1116 memcpy (&pm[1], msgbuf, msgbuf_size);
1117 pm->message_size = msgbuf_size;
1118 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1119 pm->transmit_cont = cont;
1120 pm->transmit_cont_cls = cont_cls;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Asked to transmit %u bytes to `%s', added message to list.\n",
1124 msgbuf_size, GNUNET_i2s (&session->target));
1126 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
1127 &session->target.hashPubKey,
1130 GNUNET_assert (session->client != NULL);
1131 reschedule_session_timeout (session);
1132 GNUNET_SERVER_client_set_timeout (session->client,
1133 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1134 GNUNET_STATISTICS_update (plugin->env->stats,
1135 gettext_noop ("# bytes currently in TCP buffers"),
1136 msgbuf_size, GNUNET_NO);
1138 /* append pm to pending_messages list */
1139 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1140 session->pending_messages_tail, pm);
1142 process_pending_messages (session);
1145 else if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(plugin->nat_wait_conns, &session->target.hashPubKey, session))
1147 LOG (GNUNET_ERROR_TYPE_DEBUG,
1148 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1149 GNUNET_i2s (&session->target));
1150 reschedule_session_timeout (session);
1151 GNUNET_STATISTICS_update (plugin->env->stats,
1152 gettext_noop ("# bytes currently in TCP buffers"),
1153 msgbuf_size, GNUNET_NO);
1155 /* append pm to pending_messages list */
1156 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1157 session->pending_messages_tail, pm);
1162 LOG (GNUNET_ERROR_TYPE_ERROR,
1163 "Invalid session %p\n", session);
1165 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1168 return GNUNET_SYSERR; /* session does not exist here */
1177 struct Session *result;
1182 session_lookup_it (void *cls,
1183 const struct GNUNET_HashCode *key,
1186 struct SessionItCtx * si_ctx = cls;
1187 struct Session * session = value;
1189 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1190 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1191 LOG (GNUNET_ERROR_TYPE_DEBUG,
1192 "Comparing: %s %u <-> %s %u\n",
1200 if (session->addrlen != si_ctx->addrlen)
1204 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1209 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1210 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1211 LOG (GNUNET_ERROR_TYPE_DEBUG,
1212 "Comparing: %s %u <-> %s %u , OK!\n",
1220 /* Found existing session */
1221 si_ctx->result = session;
1227 * Task cleaning up a NAT connection attempt after timeout
1230 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1232 struct Session *session = cls;
1234 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1235 LOG (GNUNET_ERROR_TYPE_DEBUG,
1236 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1237 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1238 disconnect_session (session);
1243 * Create a new session to transmit data to the target
1244 * This session will used to send data to this peer and the plugin will
1245 * notify us by calling the env->session_end function
1247 * @param cls closure
1248 * @param address pointer to the GNUNET_HELLO_Address
1249 * @return the session if the address is valid, NULL otherwise
1251 static struct Session *
1252 tcp_plugin_get_session (void *cls,
1253 const struct GNUNET_HELLO_Address *address)
1255 struct Plugin *plugin = cls;
1256 struct Session *session = NULL;
1260 struct GNUNET_CONNECTION_Handle *sa;
1261 struct sockaddr_in a4;
1262 struct sockaddr_in6 a6;
1263 const struct IPv4TcpAddress *t4;
1264 const struct IPv6TcpAddress *t6;
1265 struct GNUNET_ATS_Information ats;
1266 unsigned int is_natd = GNUNET_NO;
1269 GNUNET_assert (plugin != NULL);
1270 GNUNET_assert (address != NULL);
1271 addrlen = address->address_length;
1272 LOG (GNUNET_ERROR_TYPE_DEBUG,
1273 "Trying to get session for `%s' address of peer `%s'\n",
1274 tcp_address_to_string(NULL, address->address, address->address_length),
1275 GNUNET_i2s (&address->peer));
1277 /* look for existing session */
1279 GNUNET_CONTAINER_multihashmap_contains (plugin->sessionmap,
1280 &address->peer.hashPubKey))
1282 struct SessionItCtx si_ctx;
1284 si_ctx.addr = (void *) address->address;
1285 si_ctx.addrlen = address->address_length;
1287 si_ctx.result = NULL;
1289 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap,
1290 &address->peer.hashPubKey,
1291 &session_lookup_it, &si_ctx);
1292 if (si_ctx.result != NULL)
1294 session = si_ctx.result;
1295 LOG (GNUNET_ERROR_TYPE_DEBUG,
1296 "Found exisiting session for `%s' address `%s' session %p\n",
1297 GNUNET_i2s (&address->peer),
1298 tcp_address_to_string(NULL, address->address, address->address_length),
1302 LOG (GNUNET_ERROR_TYPE_DEBUG,
1303 "Existing sessions did not match address `%s' or peer `%s'\n",
1304 tcp_address_to_string(NULL, address->address, address->address_length),
1305 GNUNET_i2s (&address->peer));
1308 if (addrlen == sizeof (struct IPv6TcpAddress))
1310 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1311 t6 = address->address;
1313 memset (&a6, 0, sizeof (a6));
1314 #if HAVE_SOCKADDR_IN_SIN_LEN
1315 a6.sin6_len = sizeof (a6);
1317 a6.sin6_family = AF_INET6;
1318 a6.sin6_port = t6->t6_port;
1319 if (t6->t6_port == 0)
1320 is_natd = GNUNET_YES;
1321 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1325 else if (addrlen == sizeof (struct IPv4TcpAddress))
1327 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1328 t4 = address->address;
1330 memset (&a4, 0, sizeof (a4));
1331 #if HAVE_SOCKADDR_IN_SIN_LEN
1332 a4.sin_len = sizeof (a4);
1334 a4.sin_family = AF_INET;
1335 a4.sin_port = t4->t4_port;
1336 if (t4->t4_port == 0)
1337 is_natd = GNUNET_YES;
1338 a4.sin_addr.s_addr = t4->ipv4_addr;
1344 LOG (GNUNET_ERROR_TYPE_ERROR,
1345 _("Address of unexpected length: %u\n"), addrlen);
1350 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1352 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1354 /* NAT client only works with IPv4 addresses */
1358 if (0 == plugin->max_connections)
1364 if ((is_natd == GNUNET_YES) &&
1366 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1367 &address->peer.hashPubKey)))
1369 /* Only do one NAT punch attempt per peer identity */
1373 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1375 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1376 &address->peer.hashPubKey)))
1378 LOG (GNUNET_ERROR_TYPE_DEBUG,
1379 "Found valid IPv4 NAT address (creating session)!\n") ;
1380 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1381 session->addrlen = 0;
1382 session->addr = NULL;
1383 session->ats_address_network_type = ats.value;
1384 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1385 &nat_connect_timeout,
1387 GNUNET_assert (session != NULL);
1388 GNUNET_assert (GNUNET_OK ==
1389 GNUNET_CONTAINER_multihashmap_put (plugin->nat_wait_conns,
1390 &session->target.hashPubKey,
1392 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1394 LOG (GNUNET_ERROR_TYPE_DEBUG,
1395 "Created NAT WAIT connection to `%4s' at `%s'\n",
1396 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1398 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1402 LOG (GNUNET_ERROR_TYPE_DEBUG,
1403 "Running NAT client for `%4s' at `%s' failed\n",
1404 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1405 disconnect_session (session);
1410 /* create new outbound session */
1411 GNUNET_assert (0 != plugin->max_connections);
1412 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1415 LOG (GNUNET_ERROR_TYPE_DEBUG,
1416 "Failed to create connection to `%4s' at `%s'\n",
1417 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1420 plugin->max_connections--;
1422 LOG (GNUNET_ERROR_TYPE_DEBUG,
1423 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1424 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1426 session = create_session (plugin,
1428 GNUNET_SERVER_connect_socket (plugin->server, sa),
1430 session->addr = GNUNET_malloc (addrlen);
1431 memcpy (session->addr, address->address, addrlen);
1432 session->addrlen = addrlen;
1433 session->ats_address_network_type = ats.value;
1435 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1436 &session->target.hashPubKey,
1437 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1438 inc_sessions (plugin, session, __LINE__);
1439 LOG (GNUNET_ERROR_TYPE_DEBUG,
1440 "Creating new session for `%s' address `%s' session %p\n",
1441 GNUNET_i2s (&address->peer),
1442 tcp_address_to_string(NULL, address->address, address->address_length),
1444 /* Send TCP Welcome */
1445 process_pending_messages (session);
1452 session_disconnect_it (void *cls,
1453 const struct GNUNET_HashCode * key,
1456 struct Session *session = value;
1458 GNUNET_STATISTICS_update (session->plugin->env->stats,
1460 ("# transport-service disconnect requests for TCP"),
1462 disconnect_session (session);
1468 * Function that can be called to force a disconnect from the
1469 * specified neighbour. This should also cancel all previously
1470 * scheduled transmissions. Obviously the transmission may have been
1471 * partially completed already, which is OK. The plugin is supposed
1472 * to close the connection (if applicable) and no longer call the
1473 * transmit continuation(s).
1475 * Finally, plugin MUST NOT call the services's receive function to
1476 * notify the service that the connection to the specified target was
1477 * closed after a getting this call.
1479 * @param cls closure
1480 * @param target peer for which the last transmission is
1484 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1486 struct Plugin *plugin = cls;
1488 LOG (GNUNET_ERROR_TYPE_DEBUG,
1489 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1490 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap, &target->hashPubKey, &session_disconnect_it, plugin);
1491 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->nat_wait_conns, &target->hashPubKey, &session_disconnect_it, plugin);
1496 * Context for address to string conversion.
1498 struct PrettyPrinterContext
1501 * Function to call with the result.
1503 GNUNET_TRANSPORT_AddressStringCallback asc;
1506 * Clsoure for 'asc'.
1511 * Port to add after the IP address.
1520 * Append our port and forward the result.
1522 * @param cls the 'struct PrettyPrinterContext*'
1523 * @param hostname hostname part of the address
1526 append_port (void *cls, const char *hostname)
1528 struct PrettyPrinterContext *ppc = cls;
1531 if (hostname == NULL)
1533 ppc->asc (ppc->asc_cls, NULL);
1537 if (GNUNET_YES == ppc->ipv6)
1538 GNUNET_asprintf (&ret, "[%s]:%d", hostname, ppc->port);
1540 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1541 ppc->asc (ppc->asc_cls, ret);
1547 * Convert the transports address to a nice, human-readable
1550 * @param cls closure
1551 * @param type name of the transport that generated the address
1552 * @param addr one of the addresses of the host, NULL for the last address
1553 * the specific address format depends on the transport
1554 * @param addrlen length of the address
1555 * @param numeric should (IP) addresses be displayed in numeric form?
1556 * @param timeout after how long should we give up?
1557 * @param asc function to call on each string
1558 * @param asc_cls closure for asc
1561 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1562 const void *addr, size_t addrlen,
1564 struct GNUNET_TIME_Relative timeout,
1565 GNUNET_TRANSPORT_AddressStringCallback asc,
1568 struct PrettyPrinterContext *ppc;
1571 struct sockaddr_in a4;
1572 struct sockaddr_in6 a6;
1573 const struct IPv4TcpAddress *t4;
1574 const struct IPv6TcpAddress *t6;
1577 if (addrlen == sizeof (struct IPv6TcpAddress))
1580 memset (&a6, 0, sizeof (a6));
1581 a6.sin6_family = AF_INET6;
1582 a6.sin6_port = t6->t6_port;
1583 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1584 port = ntohs (t6->t6_port);
1588 else if (addrlen == sizeof (struct IPv4TcpAddress))
1591 memset (&a4, 0, sizeof (a4));
1592 a4.sin_family = AF_INET;
1593 a4.sin_port = t4->t4_port;
1594 a4.sin_addr.s_addr = t4->ipv4_addr;
1595 port = ntohs (t4->t4_port);
1599 else if (0 == addrlen)
1601 asc (asc_cls, "<inbound connection>");
1602 asc (asc_cls, NULL);
1607 /* invalid address */
1608 GNUNET_break_op (0);
1609 asc (asc_cls, NULL);
1612 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1613 if (addrlen == sizeof (struct IPv6TcpAddress))
1614 ppc->ipv6 = GNUNET_YES;
1616 ppc->ipv6 = GNUNET_NO;
1618 ppc->asc_cls = asc_cls;
1620 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1625 * Check if the given port is plausible (must be either our listen
1626 * port or our advertised port), or any port if we are behind NAT
1627 * and do not have a port open. If it is neither, we return
1630 * @param plugin global variables
1631 * @param in_port port number to check
1632 * @return GNUNET_OK if port is either open_port or adv_port
1635 check_port (struct Plugin *plugin, uint16_t in_port)
1637 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1639 return GNUNET_SYSERR;
1644 * Function that will be called to check if a binary address for this
1645 * plugin is well-formed and corresponds to an address for THIS peer
1646 * (as per our configuration). Naturally, if absolutely necessary,
1647 * plugins can be a bit conservative in their answer, but in general
1648 * plugins should make sure that the address does not redirect
1649 * traffic to a 3rd party that might try to man-in-the-middle our
1652 * @param cls closure, our 'struct Plugin*'
1653 * @param addr pointer to the address
1654 * @param addrlen length of addr
1655 * @return GNUNET_OK if this is a plausible address for this peer
1656 * and transport, GNUNET_SYSERR if not
1659 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1661 struct Plugin *plugin = cls;
1662 struct IPv4TcpAddress *v4;
1663 struct IPv6TcpAddress *v6;
1665 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1666 (addrlen != sizeof (struct IPv6TcpAddress)))
1668 GNUNET_break_op (0);
1669 return GNUNET_SYSERR;
1671 if (addrlen == sizeof (struct IPv4TcpAddress))
1673 v4 = (struct IPv4TcpAddress *) addr;
1674 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1675 return GNUNET_SYSERR;
1677 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1678 sizeof (struct in_addr)))
1679 return GNUNET_SYSERR;
1683 v6 = (struct IPv6TcpAddress *) addr;
1684 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1686 GNUNET_break_op (0);
1687 return GNUNET_SYSERR;
1689 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1690 return GNUNET_SYSERR;
1692 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1693 sizeof (struct in6_addr)))
1694 return GNUNET_SYSERR;
1701 * We've received a nat probe from this peer via TCP. Finish
1702 * creating the client session and resume sending of queued
1705 * @param cls closure
1706 * @param client identification of the client
1707 * @param message the actual message
1710 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1711 const struct GNUNET_MessageHeader *message)
1713 struct Plugin *plugin = cls;
1714 struct Session *session;
1715 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1718 struct IPv4TcpAddress *t4;
1719 struct IPv6TcpAddress *t6;
1720 const struct sockaddr_in *s4;
1721 const struct sockaddr_in6 *s6;
1723 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1725 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1726 * a connection to this peer by running gnunet-nat-client. This peer
1727 * received the punch message and now wants us to use the new connection
1728 * as the default for that peer. Do so and then send a WELCOME message
1729 * so we can really be connected!
1731 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1733 GNUNET_break_op (0);
1734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1738 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1740 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1741 sizeof (struct GNUNET_PeerIdentity)))
1743 /* refuse connections from ourselves */
1744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1749 GNUNET_CONTAINER_multihashmap_get (plugin->nat_wait_conns,
1751 clientIdentity.hashPubKey);
1752 if (session == NULL)
1754 LOG (GNUNET_ERROR_TYPE_DEBUG,
1755 "Did NOT find session for NAT probe!\n");
1756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1759 LOG (GNUNET_ERROR_TYPE_DEBUG,
1760 "Found session for NAT probe!\n");
1762 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1764 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1765 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1768 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1772 disconnect_session (session);
1775 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
1776 (plugin->nat_wait_conns,
1777 &tcp_nat_probe->clientIdentity.hashPubKey,
1778 session) == GNUNET_YES);
1779 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1780 &session->target.hashPubKey, session,
1781 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1782 session->last_activity = GNUNET_TIME_absolute_get ();
1783 session->inbound = GNUNET_NO;
1784 LOG (GNUNET_ERROR_TYPE_DEBUG,
1785 "Found address `%s' for incoming connection\n",
1786 GNUNET_a2s (vaddr, alen));
1787 switch (((const struct sockaddr *) vaddr)->sa_family)
1791 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1792 t4->t4_port = s4->sin_port;
1793 t4->ipv4_addr = s4->sin_addr.s_addr;
1795 session->addrlen = sizeof (struct IPv4TcpAddress);
1799 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1800 t6->t6_port = s6->sin6_port;
1801 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1803 session->addrlen = sizeof (struct IPv6TcpAddress);
1806 GNUNET_break_op (0);
1807 LOG (GNUNET_ERROR_TYPE_DEBUG,
1808 "Bad address for incoming connection!\n");
1809 GNUNET_free (vaddr);
1810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1811 disconnect_session (session);
1814 GNUNET_free (vaddr);
1815 GNUNET_break (NULL == session->client);
1816 GNUNET_SERVER_client_keep (client);
1817 session->client = client;
1818 inc_sessions (plugin, session, __LINE__);
1819 GNUNET_STATISTICS_update (plugin->env->stats,
1820 gettext_noop ("# TCP sessions active"), 1,
1822 process_pending_messages (session);
1823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1828 * We've received a welcome from this peer via TCP. Possibly create a
1829 * fresh client record and send back our welcome.
1831 * @param cls closure
1832 * @param client identification of the client
1833 * @param message the actual message
1836 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
1837 const struct GNUNET_MessageHeader *message)
1839 struct Plugin *plugin = cls;
1840 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1841 struct Session *session;
1844 struct IPv4TcpAddress *t4;
1845 struct IPv6TcpAddress *t6;
1846 const struct sockaddr_in *s4;
1847 const struct sockaddr_in6 *s6;
1850 memcmp (&wm->clientIdentity, plugin->env->my_identity,
1851 sizeof (struct GNUNET_PeerIdentity)))
1853 /* refuse connections from ourselves */
1854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1857 LOG (GNUNET_ERROR_TYPE_DEBUG,
1858 "Received %s message from `%4s'\n", "WELCOME",
1859 GNUNET_i2s (&wm->clientIdentity));
1860 GNUNET_STATISTICS_update (plugin->env->stats,
1861 gettext_noop ("# TCP WELCOME messages received"), 1,
1863 session = lookup_session_by_client (plugin, client);
1864 if (session != NULL)
1866 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1868 LOG (GNUNET_ERROR_TYPE_DEBUG,
1869 "Found existing session %p for peer `%s'\n",
1871 GNUNET_a2s (vaddr, alen));
1872 GNUNET_free (vaddr);
1877 GNUNET_SERVER_client_keep (client);
1878 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
1879 session->inbound = GNUNET_YES;
1880 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1882 if (alen == sizeof (struct sockaddr_in))
1885 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1886 t4->t4_port = s4->sin_port;
1887 t4->ipv4_addr = s4->sin_addr.s_addr;
1889 session->addrlen = sizeof (struct IPv4TcpAddress);
1891 else if (alen == sizeof (struct sockaddr_in6))
1894 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1895 t6->t6_port = s6->sin6_port;
1896 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1898 session->addrlen = sizeof (struct IPv6TcpAddress);
1901 struct GNUNET_ATS_Information ats;
1902 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
1903 session->ats_address_network_type = ats.value;
1905 GNUNET_free (vaddr);
1909 LOG (GNUNET_ERROR_TYPE_DEBUG,
1910 "Did not obtain TCP socket address for incoming connection\n");
1912 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1913 &session->target.hashPubKey,
1915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1916 inc_sessions (plugin, session, __LINE__);
1919 if (session->expecting_welcome != GNUNET_YES)
1921 GNUNET_break_op (0);
1922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1925 session->last_activity = GNUNET_TIME_absolute_get ();
1926 session->expecting_welcome = GNUNET_NO;
1929 process_pending_messages (session);
1931 GNUNET_SERVER_client_set_timeout (client,
1932 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1938 * Task to signal the server that we can continue
1939 * receiving from the TCP client now.
1941 * @param cls the 'struct Session*'
1942 * @param tc task context (unused)
1945 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1947 struct Session *session = cls;
1948 struct GNUNET_TIME_Relative delay;
1949 struct GNUNET_ATS_Information ats;
1951 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1953 session->plugin->env->receive (session->plugin->env->cls,
1954 &session->target, NULL, &ats, 0, session,
1956 reschedule_session_timeout (session);
1958 if (delay.rel_value == 0)
1959 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1961 session->receive_delay_task =
1962 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
1967 * We've received data for this peer via TCP. Unbox,
1968 * compute latency and forward.
1970 * @param cls closure
1971 * @param client identification of the client
1972 * @param message the actual message
1975 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
1976 const struct GNUNET_MessageHeader *message)
1978 struct Plugin *plugin = cls;
1979 struct Session *session;
1980 struct GNUNET_TIME_Relative delay;
1983 type = ntohs (message->type);
1984 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
1985 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
1987 /* We don't want to propagate WELCOME and NAT Probe messages up! */
1988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1991 session = lookup_session_by_client (plugin, client);
1992 if (NULL == session)
1994 /* No inbound session found */
1998 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1999 LOG (GNUNET_ERROR_TYPE_ERROR,
2000 "Received unexpected %u bytes of type %u from `%s'\n",
2001 (unsigned int) ntohs (message->size),
2002 (unsigned int) ntohs (message->type),
2003 GNUNET_a2s(vaddr, alen));
2004 GNUNET_break_op (0);
2005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2006 GNUNET_free_non_null(vaddr);
2009 else if (GNUNET_YES == session->expecting_welcome)
2011 /* Session is expecting WELCOME message */
2015 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2016 LOG (GNUNET_ERROR_TYPE_ERROR,
2017 "Received unexpected %u bytes of type %u from `%s'\n",
2018 (unsigned int) ntohs (message->size),
2019 (unsigned int) ntohs (message->type),
2020 GNUNET_a2s(vaddr, alen));
2021 GNUNET_break_op (0);
2022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2023 GNUNET_free_non_null(vaddr);
2027 session->last_activity = GNUNET_TIME_absolute_get ();
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2030 (unsigned int) ntohs (message->size),
2031 (unsigned int) ntohs (message->type),
2032 GNUNET_i2s (&session->target));
2034 GNUNET_STATISTICS_update (plugin->env->stats,
2035 gettext_noop ("# bytes received via TCP"),
2036 ntohs (message->size), GNUNET_NO);
2037 struct GNUNET_ATS_Information distance[2];
2039 distance[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2040 distance[0].value = htonl (1);
2041 distance[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2042 distance[1].value = session->ats_address_network_type;
2043 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2045 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
2046 &session->target.hashPubKey,
2049 delay = plugin->env->receive (plugin->env->cls,
2052 (const struct GNUNET_ATS_Information *) &distance,
2054 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2055 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2057 reschedule_session_timeout (session);
2059 if (delay.rel_value == 0)
2061 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2065 LOG (GNUNET_ERROR_TYPE_DEBUG,
2066 "Throttling receiving from `%s' for %llu ms\n",
2067 GNUNET_i2s (&session->target),
2068 (unsigned long long) delay.rel_value);
2069 GNUNET_SERVER_disable_receive_done_warning (client);
2070 session->receive_delay_task =
2071 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2077 * Functions with this signature are called whenever a peer
2078 * is disconnected on the network level.
2080 * @param cls closure
2081 * @param client identification of the client
2084 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2086 struct Plugin *plugin = cls;
2087 struct Session *session;
2091 plugin->max_connections++;
2092 session = lookup_session_by_client (plugin, client);
2093 if (session == NULL)
2094 return; /* unknown, nothing to do */
2095 LOG (GNUNET_ERROR_TYPE_DEBUG,
2096 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2097 GNUNET_i2s (&session->target),
2099 NULL) ? tcp_address_to_string (session->plugin,
2103 GNUNET_STATISTICS_update (session->plugin->env->stats,
2105 ("# network-level TCP disconnect events"), 1,
2107 disconnect_session (session);
2112 * We can now send a probe message, copy into buffer to really send.
2114 * @param cls closure, a struct TCPProbeContext
2115 * @param size max size to copy
2116 * @param buf buffer to copy message to
2117 * @return number of bytes copied into buf
2120 notify_send_probe (void *cls, size_t size, void *buf)
2122 struct TCPProbeContext *tcp_probe_ctx = cls;
2123 struct Plugin *plugin = tcp_probe_ctx->plugin;
2126 tcp_probe_ctx->transmit_handle = NULL;
2127 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2131 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2132 GNUNET_free (tcp_probe_ctx);
2135 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2136 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2137 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2138 tcp_probe_ctx->sock);
2139 ret = sizeof (tcp_probe_ctx->message);
2140 GNUNET_free (tcp_probe_ctx);
2146 * Function called by the NAT subsystem suggesting another peer wants
2147 * to connect to us via connection reversal. Try to connect back to the
2150 * @param cls closure
2151 * @param addr address to try
2152 * @param addrlen number of bytes in addr
2155 try_connection_reversal (void *cls, const struct sockaddr *addr,
2158 struct Plugin *plugin = cls;
2159 struct GNUNET_CONNECTION_Handle *sock;
2160 struct TCPProbeContext *tcp_probe_ctx;
2163 * We have received an ICMP response, ostensibly from a peer
2164 * that wants to connect to us! Send a message to establish a connection.
2166 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2169 /* failed for some odd reason (out of sockets?); ignore attempt */
2173 /* FIXME: do we need to track these probe context objects so that
2174 * we can clean them up on plugin unload? */
2175 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2176 tcp_probe_ctx->message.header.size =
2177 htons (sizeof (struct TCP_NAT_ProbeMessage));
2178 tcp_probe_ctx->message.header.type =
2179 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2180 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2181 sizeof (struct GNUNET_PeerIdentity));
2182 tcp_probe_ctx->plugin = plugin;
2183 tcp_probe_ctx->sock = sock;
2184 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2186 tcp_probe_ctx->transmit_handle =
2187 GNUNET_CONNECTION_notify_transmit_ready (sock,
2188 ntohs (tcp_probe_ctx->
2189 message.header.size),
2190 GNUNET_TIME_UNIT_FOREVER_REL,
2198 * Session was idle, so disconnect it
2201 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2203 GNUNET_assert (NULL != cls);
2204 struct Session *s = cls;
2206 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2208 "Session %p was idle for %llu ms, disconnecting\n",
2209 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2210 /* call session destroy function */
2211 disconnect_session(s);
2216 * Start session timeout
2219 start_session_timeout (struct Session *s)
2221 GNUNET_assert (NULL != s);
2222 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2223 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "Timeout for session %p set to %llu ms\n",
2228 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2233 * Increment session timeout due to activity
2236 reschedule_session_timeout (struct Session *s)
2238 GNUNET_assert (NULL != s);
2239 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2241 GNUNET_SCHEDULER_cancel (s->timeout_task);
2242 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 "Timeout rescheduled for session %p set to %llu ms\n",
2247 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2255 stop_session_timeout (struct Session *s)
2257 GNUNET_assert (NULL != s);
2259 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2261 GNUNET_SCHEDULER_cancel (s->timeout_task);
2262 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2264 "Timeout stopped for session %p canceled\n",
2265 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2271 * Entry point for the plugin.
2273 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2274 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2277 libgnunet_plugin_transport_tcp_init (void *cls)
2279 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2280 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2281 sizeof (struct WelcomeMessage)},
2282 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2283 sizeof (struct TCP_NAT_ProbeMessage)},
2284 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2287 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2288 struct GNUNET_TRANSPORT_PluginFunctions *api;
2289 struct Plugin *plugin;
2290 struct GNUNET_SERVICE_Context *service;
2291 unsigned long long aport;
2292 unsigned long long bport;
2293 unsigned long long max_connections;
2295 struct GNUNET_TIME_Relative idle_timeout;
2297 struct sockaddr **addrs;
2298 socklen_t *addrlens;
2300 if (NULL == env->receive)
2302 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2303 initialze the plugin or the API */
2304 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2306 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2307 api->address_to_string = &tcp_address_to_string;
2308 api->string_to_address = &tcp_string_to_address;
2313 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2316 max_connections = 128;
2320 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2321 &bport)) || (bport > 65535) ||
2323 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2324 "ADVERTISED-PORT", &aport)) &&
2327 LOG (GNUNET_ERROR_TYPE_ERROR,
2329 ("Require valid port number for service `%s' in configuration!\n"),
2339 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2340 if (service == NULL)
2342 LOG (GNUNET_ERROR_TYPE_WARNING,
2343 _("Failed to start service.\n"));
2350 plugin = GNUNET_malloc (sizeof (struct Plugin));
2351 plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create (max_connections, GNUNET_YES);
2352 plugin->max_connections = max_connections;
2353 plugin->open_port = bport;
2354 plugin->adv_port = aport;
2356 plugin->lsock = NULL;
2357 if ((service != NULL) &&
2360 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2364 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret,
2365 (const struct sockaddr **) addrs, addrlens,
2366 &tcp_nat_port_map_callback,
2367 &try_connection_reversal, plugin);
2371 GNUNET_assert (addrs[ret] != NULL);
2372 GNUNET_free (addrs[ret]);
2374 GNUNET_free_non_null (addrs);
2375 GNUNET_free_non_null (addrlens);
2380 GNUNET_NAT_register (env->cfg, GNUNET_YES, 0, 0, NULL, NULL, NULL,
2381 &try_connection_reversal, plugin);
2383 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2385 api->send = &tcp_plugin_send;
2386 api->get_session = &tcp_plugin_get_session;
2388 api->disconnect = &tcp_plugin_disconnect;
2389 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2390 api->check_address = &tcp_plugin_check_address;
2391 api->address_to_string = &tcp_address_to_string;
2392 api->string_to_address = &tcp_string_to_address;
2393 plugin->service = service;
2394 if (service != NULL)
2396 plugin->server = GNUNET_SERVICE_get_server (service);
2401 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2402 "TIMEOUT", &idle_timeout))
2404 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2405 "transport-tcp", "TIMEOUT");
2406 if (plugin->nat != NULL)
2407 GNUNET_NAT_unregister (plugin->nat);
2408 GNUNET_free (plugin);
2413 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2414 NULL, idle_timeout, GNUNET_YES);
2416 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2417 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2419 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2421 plugin->handlers[i].callback_cls = plugin;
2422 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2423 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2424 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
2426 LOG (GNUNET_ERROR_TYPE_INFO,
2427 _("TCP transport listening on port %llu\n"), bport);
2429 LOG (GNUNET_ERROR_TYPE_INFO,
2431 ("TCP transport not listening on any port (client only)\n"));
2433 LOG (GNUNET_ERROR_TYPE_INFO,
2435 ("TCP transport advertises itself as being on port %llu\n"),
2437 /* Initially set connections to 0 */
2438 GNUNET_STATISTICS_set(plugin->env->stats,
2439 gettext_noop ("# TCP sessions active"), 0,
2446 * Exit point from the plugin.
2449 libgnunet_plugin_transport_tcp_done (void *cls)
2451 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2452 struct Plugin *plugin = api->cls;
2453 struct TCPProbeContext *tcp_probe;
2460 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2462 /* Removing leftover sessions */
2463 GNUNET_CONTAINER_multihashmap_iterate(plugin->sessionmap, &session_disconnect_it, NULL);
2464 /* Removing leftover NAT sessions */
2465 GNUNET_CONTAINER_multihashmap_iterate(plugin->nat_wait_conns, &session_disconnect_it, NULL);
2467 if (plugin->service != NULL)
2468 GNUNET_SERVICE_stop (plugin->service);
2470 GNUNET_SERVER_destroy (plugin->server);
2471 GNUNET_free (plugin->handlers);
2472 if (plugin->nat != NULL)
2473 GNUNET_NAT_unregister (plugin->nat);
2474 while (NULL != (tcp_probe = plugin->probe_head))
2476 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2478 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2479 GNUNET_free (tcp_probe);
2481 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2482 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessionmap);
2483 GNUNET_free (plugin);
2488 /* end of plugin_transport_tcp.c */