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 * How many more TCP sessions do we have right now?
390 unsigned long long cur_connections;
393 * ID of task used to update our addresses when one expires.
395 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
398 * Port that we are actually listening on.
403 * Port that the user said we would have visible to the
412 * Start session timeout
415 start_session_timeout (struct Session *s);
419 * Increment session timeout due to activity
422 reschedule_session_timeout (struct Session *s);
429 stop_session_timeout (struct Session *s);
434 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
437 static unsigned int sessions;
441 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
444 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
445 if (sessions != size)
446 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
448 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
452 GNUNET_i2s (&session->target),
453 tcp_address_to_string (NULL, session->addr, session->addrlen));
458 dec_sessions (struct Plugin *plugin, struct Session *session, int line)
460 GNUNET_assert (sessions > 0);
461 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
463 if (sessions != size)
464 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
466 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
470 GNUNET_i2s (&session->target),
471 tcp_address_to_string (NULL, session->addr, session->addrlen));
477 * Function to check if an inbound connection is acceptable.
478 * Mostly used to limit the total number of open connections
481 * @param cls the 'struct Plugin'
482 * @param ucred credentials, if available, otherwise NULL
483 * @param addr address
484 * @param addrlen length of address
485 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
486 * for unknown address family (will be denied).
489 plugin_tcp_access_check (void *cls,
490 const struct GNUNET_CONNECTION_Credentials *ucred,
491 const struct sockaddr *addr, socklen_t addrlen)
493 struct Plugin *plugin = cls;
494 LOG (GNUNET_ERROR_TYPE_DEBUG,
495 "Accepting new incoming TCP connection from `%s'\n",
496 GNUNET_a2s (addr, addrlen));
497 if (plugin->cur_connections >= plugin->max_connections)
499 plugin->cur_connections ++;
505 * Our external IP address/port mapping has changed.
507 * @param cls closure, the 'struct LocalAddrList'
508 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
509 * the previous (now invalid) one
510 * @param addr either the previous or the new public IP address
511 * @param addrlen actual lenght of the address
514 tcp_nat_port_map_callback (void *cls, int add_remove,
515 const struct sockaddr *addr, socklen_t addrlen)
517 struct Plugin *plugin = cls;
518 struct IPv4TcpAddress t4;
519 struct IPv6TcpAddress t6;
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "NPMC called with %d for address `%s'\n", add_remove,
525 GNUNET_a2s (addr, addrlen));
526 /* convert 'addr' to our internal format */
527 switch (addr->sa_family)
530 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
531 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
532 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
537 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
538 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
539 sizeof (struct in6_addr));
540 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
548 /* modify our published address list */
549 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
554 * Function called for a quick conversion of the binary address to
555 * a numeric address. Note that the caller must not free the
556 * address and that the next call to this function is allowed
557 * to override the address again.
559 * @param cls closure ('struct Plugin*')
560 * @param addr binary address
561 * @param addrlen length of the address
562 * @return string representing the same address
565 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
567 static char rbuf[INET6_ADDRSTRLEN + 12];
568 char buf[INET6_ADDRSTRLEN];
572 const struct IPv4TcpAddress *t4;
573 const struct IPv6TcpAddress *t6;
579 case sizeof (struct IPv6TcpAddress):
582 port = ntohs (t6->t6_port);
583 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
586 case sizeof (struct IPv4TcpAddress):
589 port = ntohs (t4->t4_port);
590 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
594 LOG (GNUNET_ERROR_TYPE_ERROR,
595 _("Unexpected address length: %u bytes\n"),
596 (unsigned int) addrlen);
600 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
602 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
605 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
612 * Function called to convert a string address to
615 * @param cls closure ('struct Plugin*')
616 * @param addr string address
617 * @param addrlen length of the address
618 * @param buf location to store the buffer
619 * @param added location to store the number of bytes in the buffer.
620 * If the function returns GNUNET_SYSERR, its contents are undefined.
621 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
624 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
625 void **buf, size_t *added)
627 struct sockaddr_storage socket_address;
629 if ((NULL == addr) || (addrlen == 0))
632 return GNUNET_SYSERR;
634 if ('\0' != addr[addrlen - 1])
637 return GNUNET_SYSERR;
639 if (strlen (addr) != addrlen - 1)
642 return GNUNET_SYSERR;
645 GNUNET_STRINGS_to_address_ip (addr, strlen (addr),
649 return GNUNET_SYSERR;
651 switch (socket_address.ss_family)
655 struct IPv4TcpAddress *t4;
656 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
658 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
659 t4->ipv4_addr = in4->sin_addr.s_addr;
660 t4->t4_port = in4->sin_port;
662 *added = sizeof (struct IPv4TcpAddress);
667 struct IPv6TcpAddress *t6;
668 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
669 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
670 t6->ipv6_addr = in6->sin6_addr;
671 t6->t6_port = in6->sin6_port;
673 *added = sizeof (struct IPv6TcpAddress);
677 return GNUNET_SYSERR;
682 struct SessionClientCtx
684 const struct GNUNET_SERVER_Client *client;
690 session_lookup_by_client_it (void *cls,
691 const struct GNUNET_HashCode * key,
694 struct SessionClientCtx *sc_ctx = cls;
695 struct Session *s = value;
697 if (s->client == sc_ctx->client)
707 * Find the session handle for the given client.
709 * @param plugin the plugin
710 * @param client which client to find the session handle for
711 * @return NULL if no matching session exists
713 static struct Session *
714 lookup_session_by_client (struct Plugin *plugin,
715 const struct GNUNET_SERVER_Client *client)
717 struct SessionClientCtx sc_ctx;
719 sc_ctx.client = client;
721 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
727 * Create a new session. Also queues a welcome message.
729 * @param plugin the plugin
730 * @param target peer to connect to
731 * @param client client to use, reference counter must have already been increased
732 * @param is_nat this a NAT session, we should wait for a client to
733 * connect to us from an address, then assign that to
735 * @return new session object
737 static struct Session *
738 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
739 struct GNUNET_SERVER_Client *client, int is_nat)
741 struct Session *session;
742 struct PendingMessage *pm;
743 struct WelcomeMessage welcome;
745 if (GNUNET_YES != is_nat)
746 GNUNET_assert (NULL != client);
748 GNUNET_assert (NULL == client);
750 LOG (GNUNET_ERROR_TYPE_DEBUG,
751 "Creating new session for peer `%4s'\n",
752 GNUNET_i2s (target));
753 session = GNUNET_malloc (sizeof (struct Session));
754 session->last_activity = GNUNET_TIME_absolute_get ();
755 session->plugin = plugin;
756 session->is_nat = is_nat;
757 session->client = client;
758 session->target = *target;
759 session->expecting_welcome = GNUNET_YES;
760 session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
761 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
762 sizeof (struct WelcomeMessage));
763 pm->msg = (const char *) &pm[1];
764 pm->message_size = sizeof (struct WelcomeMessage);
765 welcome.header.size = htons (sizeof (struct WelcomeMessage));
766 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
767 welcome.clientIdentity = *plugin->env->my_identity;
768 memcpy (&pm[1], &welcome, sizeof (welcome));
769 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
770 GNUNET_STATISTICS_update (plugin->env->stats,
771 gettext_noop ("# bytes currently in TCP buffers"),
772 pm->message_size, GNUNET_NO);
773 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
774 session->pending_messages_tail, pm);
775 if (GNUNET_YES != is_nat)
777 GNUNET_STATISTICS_update (plugin->env->stats,
778 gettext_noop ("# TCP sessions active"), 1,
781 start_session_timeout (session);
788 * If we have pending messages, ask the server to
789 * transmit them (schedule the respective tasks, etc.)
791 * @param session for which session should we do this
794 process_pending_messages (struct Session *session);
798 * Function called to notify a client about the socket
799 * being ready to queue more data. "buf" will be
800 * NULL and "size" zero if the socket was closed for
801 * writing in the meantime.
804 * @param size number of bytes available in buf
805 * @param buf where the callee should write the message
806 * @return number of bytes written to buf
809 do_transmit (void *cls, size_t size, void *buf)
811 struct Session *session = cls;
812 struct GNUNET_PeerIdentity pid;
813 struct Plugin *plugin;
814 struct PendingMessage *pos;
815 struct PendingMessage *hd;
816 struct PendingMessage *tl;
817 struct GNUNET_TIME_Absolute now;
821 GNUNET_assert (NULL != session);
822 session->transmit_handle = NULL;
823 plugin = session->plugin;
826 LOG (GNUNET_ERROR_TYPE_DEBUG,
827 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
828 GNUNET_i2s (&session->target));
829 /* timeout; cancel all messages that have already expired */
833 now = GNUNET_TIME_absolute_get ();
834 while ((NULL != (pos = session->pending_messages_head)) &&
835 (pos->timeout.abs_value <= now.abs_value))
837 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
838 session->pending_messages_tail, pos);
839 LOG (GNUNET_ERROR_TYPE_DEBUG,
840 "Failed to transmit %u byte message to `%4s'.\n",
841 pos->message_size, GNUNET_i2s (&session->target));
842 ret += pos->message_size;
843 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
845 /* do this call before callbacks (so that if callbacks destroy
846 * session, they have a chance to cancel actions done by this
848 process_pending_messages (session);
849 pid = session->target;
850 /* no do callbacks and do not use session again since
851 * the callbacks may abort the session */
852 while (NULL != (pos = hd))
854 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
855 if (pos->transmit_cont != NULL)
856 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
859 GNUNET_STATISTICS_update (plugin->env->stats,
860 gettext_noop ("# bytes currently in TCP buffers"),
861 -(int64_t) ret, GNUNET_NO);
862 GNUNET_STATISTICS_update (plugin->env->stats,
864 ("# bytes discarded by TCP (timeout)"), ret,
868 /* copy all pending messages that would fit */
873 while (NULL != (pos = session->pending_messages_head))
875 if (ret + pos->message_size > size)
877 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
878 session->pending_messages_tail, pos);
879 GNUNET_assert (size >= pos->message_size);
880 LOG (GNUNET_ERROR_TYPE_DEBUG,
881 "Transmitting message of type %u\n",
882 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
883 /* FIXME: this memcpy can be up to 7% of our total runtime */
884 memcpy (cbuf, pos->msg, pos->message_size);
885 cbuf += pos->message_size;
886 ret += pos->message_size;
887 size -= pos->message_size;
888 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
890 /* schedule 'continuation' before callbacks so that callbacks that
891 * cancel everything don't cause us to use a session that no longer
893 process_pending_messages (session);
894 session->last_activity = GNUNET_TIME_absolute_get ();
895 pid = session->target;
896 /* we'll now call callbacks that may cancel the session; hence
897 * we should not use 'session' after this point */
898 while (NULL != (pos = hd))
900 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
901 if (pos->transmit_cont != NULL)
902 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
905 GNUNET_assert (hd == NULL);
906 GNUNET_assert (tl == NULL);
907 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
909 GNUNET_STATISTICS_update (plugin->env->stats,
910 gettext_noop ("# bytes currently in TCP buffers"),
911 -(int64_t) ret, GNUNET_NO);
912 GNUNET_STATISTICS_update (plugin->env->stats,
913 gettext_noop ("# bytes transmitted via TCP"), ret,
920 * If we have pending messages, ask the server to
921 * transmit them (schedule the respective tasks, etc.)
923 * @param session for which session should we do this
926 process_pending_messages (struct Session *session)
928 struct PendingMessage *pm;
930 GNUNET_assert (session->client != NULL);
931 if (session->transmit_handle != NULL)
933 if (NULL == (pm = session->pending_messages_head))
936 session->transmit_handle =
937 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
938 GNUNET_TIME_absolute_get_remaining
939 (pm->timeout), &do_transmit,
945 * Functions with this signature are called whenever we need
946 * to close a session due to a disconnect or failure to
947 * establish a connection.
949 * @param session session to close down
952 disconnect_session (struct Session *session)
954 struct PendingMessage *pm;
955 struct Plugin * plugin = session->plugin;
957 LOG (GNUNET_ERROR_TYPE_DEBUG,
958 "Disconnecting session of peer `%s' address `%s'\n",
959 GNUNET_i2s (&session->target),
960 tcp_address_to_string (NULL, session->addr, session->addrlen));
962 stop_session_timeout (session);
964 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->sessionmap, &session->target.hashPubKey, session))
966 GNUNET_STATISTICS_update (session->plugin->env->stats,
967 gettext_noop ("# TCP sessions active"), -1,
969 dec_sessions (plugin, session, __LINE__);
971 else GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->nat_wait_conns, &session->target.hashPubKey, session));
974 if (session->transmit_handle != NULL)
976 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
977 session->transmit_handle = NULL;
979 session->plugin->env->session_end (session->plugin->env->cls,
980 &session->target, session);
982 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
984 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
985 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
988 while (NULL != (pm = session->pending_messages_head))
990 LOG (GNUNET_ERROR_TYPE_DEBUG,
992 NULL ? "Could not deliver message to `%4s'.\n" :
993 "Could not deliver message to `%4s', notifying.\n",
994 GNUNET_i2s (&session->target));
995 GNUNET_STATISTICS_update (session->plugin->env->stats,
996 gettext_noop ("# bytes currently in TCP buffers"),
997 -(int64_t) pm->message_size, GNUNET_NO);
998 GNUNET_STATISTICS_update (session->plugin->env->stats,
1000 ("# bytes discarded by TCP (disconnect)"),
1001 pm->message_size, GNUNET_NO);
1002 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1003 session->pending_messages_tail, pm);
1004 if (NULL != pm->transmit_cont)
1005 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1006 GNUNET_SYSERR, pm->message_size, 0);
1009 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1011 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1012 if (NULL != session->client)
1013 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1015 if (NULL != session->client)
1017 GNUNET_SERVER_client_disconnect (session->client);
1018 GNUNET_SERVER_client_drop (session->client);
1019 session->client = NULL;
1021 GNUNET_free_non_null (session->addr);
1022 GNUNET_assert (NULL == session->transmit_handle);
1023 GNUNET_free (session);
1027 struct FindSessionContext
1033 int session_it (void *cls,
1034 const struct GNUNET_HashCode * key,
1037 struct FindSessionContext *res = cls;
1038 if (res->s == value)
1040 res->res = GNUNET_OK;
1047 int find_session (struct Plugin *plugin, struct Session *session)
1049 struct FindSessionContext session_map_res;
1050 struct FindSessionContext nat_map_res;
1052 session_map_res.s = session;
1053 session_map_res.res = GNUNET_SYSERR;
1054 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1056 nat_map_res.s = session;
1057 nat_map_res.res = GNUNET_SYSERR;
1058 GNUNET_CONTAINER_multihashmap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1060 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1063 return GNUNET_SYSERR;
1070 * Function that can be used by the transport service to transmit
1071 * a message using the plugin. Note that in the case of a
1072 * peer disconnecting, the continuation MUST be called
1073 * prior to the disconnect notification itself. This function
1074 * will be called with this peer's HELLO message to initiate
1075 * a fresh connection to another peer.
1077 * @param cls closure
1078 * @param session which session must be used
1079 * @param msgbuf the message to transmit
1080 * @param msgbuf_size number of bytes in 'msgbuf'
1081 * @param priority how important is the message (most plugins will
1082 * ignore message priority and just FIFO)
1083 * @param to how long to wait at most for the transmission (does not
1084 * require plugins to discard the message after the timeout,
1085 * just advisory for the desired delay; most plugins will ignore
1087 * @param cont continuation to call once the message has
1088 * been transmitted (or if the transport is ready
1089 * for the next transmission call; or if the
1090 * peer disconnected...); can be NULL
1091 * @param cont_cls closure for cont
1092 * @return number of bytes used (on the physical network, with overheads);
1093 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1094 * and does NOT mean that the message was not transmitted (DV)
1097 tcp_plugin_send (void *cls,
1098 struct Session *session,
1099 const char *msgbuf, size_t msgbuf_size,
1100 unsigned int priority,
1101 struct GNUNET_TIME_Relative to,
1102 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1104 struct Plugin * plugin = cls;
1105 struct PendingMessage *pm;
1107 GNUNET_assert (NULL != plugin);
1108 GNUNET_assert (NULL != session);
1110 if (GNUNET_SYSERR == find_session(plugin, session))
1112 LOG (GNUNET_ERROR_TYPE_ERROR,
1113 _("Trying to send with invalid session %p\n"));
1114 return GNUNET_SYSERR;
1117 /* create new message entry */
1118 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1119 pm->msg = (const char *) &pm[1];
1120 memcpy (&pm[1], msgbuf, msgbuf_size);
1121 pm->message_size = msgbuf_size;
1122 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1123 pm->transmit_cont = cont;
1124 pm->transmit_cont_cls = cont_cls;
1126 LOG (GNUNET_ERROR_TYPE_DEBUG,
1127 "Asked to transmit %u bytes to `%s', added message to list.\n",
1128 msgbuf_size, GNUNET_i2s (&session->target));
1130 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
1131 &session->target.hashPubKey,
1134 GNUNET_assert (session->client != NULL);
1135 reschedule_session_timeout (session);
1136 GNUNET_SERVER_client_set_timeout (session->client,
1137 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1138 GNUNET_STATISTICS_update (plugin->env->stats,
1139 gettext_noop ("# bytes currently in TCP buffers"),
1140 msgbuf_size, GNUNET_NO);
1142 /* append pm to pending_messages list */
1143 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1144 session->pending_messages_tail, pm);
1146 process_pending_messages (session);
1149 else if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(plugin->nat_wait_conns, &session->target.hashPubKey, session))
1151 LOG (GNUNET_ERROR_TYPE_DEBUG,
1152 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1153 GNUNET_i2s (&session->target));
1154 reschedule_session_timeout (session);
1155 GNUNET_STATISTICS_update (plugin->env->stats,
1156 gettext_noop ("# bytes currently in TCP buffers"),
1157 msgbuf_size, GNUNET_NO);
1159 /* append pm to pending_messages list */
1160 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1161 session->pending_messages_tail, pm);
1166 LOG (GNUNET_ERROR_TYPE_ERROR,
1167 "Invalid session %p\n", session);
1169 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1172 return GNUNET_SYSERR; /* session does not exist here */
1181 struct Session *result;
1186 session_lookup_it (void *cls,
1187 const struct GNUNET_HashCode *key,
1190 struct SessionItCtx * si_ctx = cls;
1191 struct Session * session = value;
1193 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1194 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1195 LOG (GNUNET_ERROR_TYPE_DEBUG,
1196 "Comparing: %s %u <-> %s %u\n",
1204 if (session->addrlen != si_ctx->addrlen)
1208 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1213 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1214 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1215 LOG (GNUNET_ERROR_TYPE_DEBUG,
1216 "Comparing: %s %u <-> %s %u , OK!\n",
1224 /* Found existing session */
1225 si_ctx->result = session;
1231 * Task cleaning up a NAT connection attempt after timeout
1234 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1236 struct Session *session = cls;
1238 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1241 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1242 disconnect_session (session);
1247 * Create a new session to transmit data to the target
1248 * This session will used to send data to this peer and the plugin will
1249 * notify us by calling the env->session_end function
1251 * @param cls closure
1252 * @param address pointer to the GNUNET_HELLO_Address
1253 * @return the session if the address is valid, NULL otherwise
1255 static struct Session *
1256 tcp_plugin_get_session (void *cls,
1257 const struct GNUNET_HELLO_Address *address)
1259 struct Plugin *plugin = cls;
1260 struct Session *session = NULL;
1264 struct GNUNET_CONNECTION_Handle *sa;
1265 struct sockaddr_in a4;
1266 struct sockaddr_in6 a6;
1267 const struct IPv4TcpAddress *t4;
1268 const struct IPv6TcpAddress *t6;
1269 struct GNUNET_ATS_Information ats;
1270 unsigned int is_natd = GNUNET_NO;
1273 GNUNET_assert (plugin != NULL);
1274 GNUNET_assert (address != NULL);
1275 addrlen = address->address_length;
1276 LOG (GNUNET_ERROR_TYPE_DEBUG,
1277 "Trying to get session for `%s' address of peer `%s'\n",
1278 tcp_address_to_string(NULL, address->address, address->address_length),
1279 GNUNET_i2s (&address->peer));
1281 /* look for existing session */
1283 GNUNET_CONTAINER_multihashmap_contains (plugin->sessionmap,
1284 &address->peer.hashPubKey))
1286 struct SessionItCtx si_ctx;
1288 si_ctx.addr = (void *) address->address;
1289 si_ctx.addrlen = address->address_length;
1291 si_ctx.result = NULL;
1293 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap,
1294 &address->peer.hashPubKey,
1295 &session_lookup_it, &si_ctx);
1296 if (si_ctx.result != NULL)
1298 session = si_ctx.result;
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "Found exisiting session for `%s' address `%s' session %p\n",
1301 GNUNET_i2s (&address->peer),
1302 tcp_address_to_string(NULL, address->address, address->address_length),
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Existing sessions did not match address `%s' or peer `%s'\n",
1308 tcp_address_to_string(NULL, address->address, address->address_length),
1309 GNUNET_i2s (&address->peer));
1312 if (addrlen == sizeof (struct IPv6TcpAddress))
1314 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1315 t6 = address->address;
1317 memset (&a6, 0, sizeof (a6));
1318 #if HAVE_SOCKADDR_IN_SIN_LEN
1319 a6.sin6_len = sizeof (a6);
1321 a6.sin6_family = AF_INET6;
1322 a6.sin6_port = t6->t6_port;
1323 if (t6->t6_port == 0)
1324 is_natd = GNUNET_YES;
1325 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1329 else if (addrlen == sizeof (struct IPv4TcpAddress))
1331 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1332 t4 = address->address;
1334 memset (&a4, 0, sizeof (a4));
1335 #if HAVE_SOCKADDR_IN_SIN_LEN
1336 a4.sin_len = sizeof (a4);
1338 a4.sin_family = AF_INET;
1339 a4.sin_port = t4->t4_port;
1340 if (t4->t4_port == 0)
1341 is_natd = GNUNET_YES;
1342 a4.sin_addr.s_addr = t4->ipv4_addr;
1348 LOG (GNUNET_ERROR_TYPE_ERROR,
1349 _("Address of unexpected length: %u\n"), addrlen);
1354 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1356 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1358 /* NAT client only works with IPv4 addresses */
1362 if (plugin->cur_connections >= plugin->max_connections)
1368 if ((is_natd == GNUNET_YES) &&
1370 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1371 &address->peer.hashPubKey)))
1373 /* Only do one NAT punch attempt per peer identity */
1377 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1379 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1380 &address->peer.hashPubKey)))
1382 LOG (GNUNET_ERROR_TYPE_DEBUG,
1383 "Found valid IPv4 NAT address (creating session)!\n") ;
1384 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1385 session->addrlen = 0;
1386 session->addr = NULL;
1387 session->ats_address_network_type = ats.value;
1388 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1389 &nat_connect_timeout,
1391 GNUNET_assert (session != NULL);
1392 GNUNET_assert (GNUNET_OK ==
1393 GNUNET_CONTAINER_multihashmap_put (plugin->nat_wait_conns,
1394 &session->target.hashPubKey,
1396 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Created NAT WAIT connection to `%4s' at `%s'\n",
1400 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1402 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1406 LOG (GNUNET_ERROR_TYPE_DEBUG,
1407 "Running NAT client for `%4s' at `%s' failed\n",
1408 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1409 disconnect_session (session);
1414 /* create new outbound session */
1415 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1416 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1419 LOG (GNUNET_ERROR_TYPE_DEBUG,
1420 "Failed to create connection to `%4s' at `%s'\n",
1421 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1424 plugin->cur_connections++;
1425 if (plugin->cur_connections == plugin->max_connections)
1426 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1428 LOG (GNUNET_ERROR_TYPE_DEBUG,
1429 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1430 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1432 session = create_session (plugin,
1434 GNUNET_SERVER_connect_socket (plugin->server, sa),
1436 session->addr = GNUNET_malloc (addrlen);
1437 memcpy (session->addr, address->address, addrlen);
1438 session->addrlen = addrlen;
1439 session->ats_address_network_type = ats.value;
1441 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1442 &session->target.hashPubKey,
1443 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1444 inc_sessions (plugin, session, __LINE__);
1445 LOG (GNUNET_ERROR_TYPE_DEBUG,
1446 "Creating new session for `%s' address `%s' session %p\n",
1447 GNUNET_i2s (&address->peer),
1448 tcp_address_to_string(NULL, address->address, address->address_length),
1450 /* Send TCP Welcome */
1451 process_pending_messages (session);
1458 session_disconnect_it (void *cls,
1459 const struct GNUNET_HashCode * key,
1462 struct Session *session = value;
1464 GNUNET_STATISTICS_update (session->plugin->env->stats,
1466 ("# transport-service disconnect requests for TCP"),
1468 disconnect_session (session);
1474 * Function that can be called to force a disconnect from the
1475 * specified neighbour. This should also cancel all previously
1476 * scheduled transmissions. Obviously the transmission may have been
1477 * partially completed already, which is OK. The plugin is supposed
1478 * to close the connection (if applicable) and no longer call the
1479 * transmit continuation(s).
1481 * Finally, plugin MUST NOT call the services's receive function to
1482 * notify the service that the connection to the specified target was
1483 * closed after a getting this call.
1485 * @param cls closure
1486 * @param target peer for which the last transmission is
1490 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1492 struct Plugin *plugin = cls;
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1496 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap, &target->hashPubKey, &session_disconnect_it, plugin);
1497 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->nat_wait_conns, &target->hashPubKey, &session_disconnect_it, plugin);
1502 * Context for address to string conversion.
1504 struct PrettyPrinterContext
1507 * Function to call with the result.
1509 GNUNET_TRANSPORT_AddressStringCallback asc;
1512 * Clsoure for 'asc'.
1517 * Port to add after the IP address.
1526 * Append our port and forward the result.
1528 * @param cls the 'struct PrettyPrinterContext*'
1529 * @param hostname hostname part of the address
1532 append_port (void *cls, const char *hostname)
1534 struct PrettyPrinterContext *ppc = cls;
1537 if (hostname == NULL)
1539 ppc->asc (ppc->asc_cls, NULL);
1543 if (GNUNET_YES == ppc->ipv6)
1544 GNUNET_asprintf (&ret, "[%s]:%d", hostname, ppc->port);
1546 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1547 ppc->asc (ppc->asc_cls, ret);
1553 * Convert the transports address to a nice, human-readable
1556 * @param cls closure
1557 * @param type name of the transport that generated the address
1558 * @param addr one of the addresses of the host, NULL for the last address
1559 * the specific address format depends on the transport
1560 * @param addrlen length of the address
1561 * @param numeric should (IP) addresses be displayed in numeric form?
1562 * @param timeout after how long should we give up?
1563 * @param asc function to call on each string
1564 * @param asc_cls closure for asc
1567 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1568 const void *addr, size_t addrlen,
1570 struct GNUNET_TIME_Relative timeout,
1571 GNUNET_TRANSPORT_AddressStringCallback asc,
1574 struct PrettyPrinterContext *ppc;
1577 struct sockaddr_in a4;
1578 struct sockaddr_in6 a6;
1579 const struct IPv4TcpAddress *t4;
1580 const struct IPv6TcpAddress *t6;
1583 if (addrlen == sizeof (struct IPv6TcpAddress))
1586 memset (&a6, 0, sizeof (a6));
1587 a6.sin6_family = AF_INET6;
1588 a6.sin6_port = t6->t6_port;
1589 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1590 port = ntohs (t6->t6_port);
1594 else if (addrlen == sizeof (struct IPv4TcpAddress))
1597 memset (&a4, 0, sizeof (a4));
1598 a4.sin_family = AF_INET;
1599 a4.sin_port = t4->t4_port;
1600 a4.sin_addr.s_addr = t4->ipv4_addr;
1601 port = ntohs (t4->t4_port);
1605 else if (0 == addrlen)
1607 asc (asc_cls, "<inbound connection>");
1608 asc (asc_cls, NULL);
1613 /* invalid address */
1614 GNUNET_break_op (0);
1615 asc (asc_cls, NULL);
1618 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1619 if (addrlen == sizeof (struct IPv6TcpAddress))
1620 ppc->ipv6 = GNUNET_YES;
1622 ppc->ipv6 = GNUNET_NO;
1624 ppc->asc_cls = asc_cls;
1626 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1631 * Check if the given port is plausible (must be either our listen
1632 * port or our advertised port), or any port if we are behind NAT
1633 * and do not have a port open. If it is neither, we return
1636 * @param plugin global variables
1637 * @param in_port port number to check
1638 * @return GNUNET_OK if port is either open_port or adv_port
1641 check_port (struct Plugin *plugin, uint16_t in_port)
1643 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1645 return GNUNET_SYSERR;
1650 * Function that will be called to check if a binary address for this
1651 * plugin is well-formed and corresponds to an address for THIS peer
1652 * (as per our configuration). Naturally, if absolutely necessary,
1653 * plugins can be a bit conservative in their answer, but in general
1654 * plugins should make sure that the address does not redirect
1655 * traffic to a 3rd party that might try to man-in-the-middle our
1658 * @param cls closure, our 'struct Plugin*'
1659 * @param addr pointer to the address
1660 * @param addrlen length of addr
1661 * @return GNUNET_OK if this is a plausible address for this peer
1662 * and transport, GNUNET_SYSERR if not
1665 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1667 struct Plugin *plugin = cls;
1668 struct IPv4TcpAddress *v4;
1669 struct IPv6TcpAddress *v6;
1671 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1672 (addrlen != sizeof (struct IPv6TcpAddress)))
1674 GNUNET_break_op (0);
1675 return GNUNET_SYSERR;
1677 if (addrlen == sizeof (struct IPv4TcpAddress))
1679 v4 = (struct IPv4TcpAddress *) addr;
1680 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1681 return GNUNET_SYSERR;
1683 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1684 sizeof (struct in_addr)))
1685 return GNUNET_SYSERR;
1689 v6 = (struct IPv6TcpAddress *) addr;
1690 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1692 GNUNET_break_op (0);
1693 return GNUNET_SYSERR;
1695 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1696 return GNUNET_SYSERR;
1698 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1699 sizeof (struct in6_addr)))
1700 return GNUNET_SYSERR;
1707 * We've received a nat probe from this peer via TCP. Finish
1708 * creating the client session and resume sending of queued
1711 * @param cls closure
1712 * @param client identification of the client
1713 * @param message the actual message
1716 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1717 const struct GNUNET_MessageHeader *message)
1719 struct Plugin *plugin = cls;
1720 struct Session *session;
1721 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1724 struct IPv4TcpAddress *t4;
1725 struct IPv6TcpAddress *t6;
1726 const struct sockaddr_in *s4;
1727 const struct sockaddr_in6 *s6;
1729 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1731 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1732 * a connection to this peer by running gnunet-nat-client. This peer
1733 * received the punch message and now wants us to use the new connection
1734 * as the default for that peer. Do so and then send a WELCOME message
1735 * so we can really be connected!
1737 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1739 GNUNET_break_op (0);
1740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1744 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1746 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1747 sizeof (struct GNUNET_PeerIdentity)))
1749 /* refuse connections from ourselves */
1750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1755 GNUNET_CONTAINER_multihashmap_get (plugin->nat_wait_conns,
1757 clientIdentity.hashPubKey);
1758 if (session == NULL)
1760 LOG (GNUNET_ERROR_TYPE_DEBUG,
1761 "Did NOT find session for NAT probe!\n");
1762 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1765 LOG (GNUNET_ERROR_TYPE_DEBUG,
1766 "Found session for NAT probe!\n");
1768 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1770 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1771 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1774 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1778 disconnect_session (session);
1781 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
1782 (plugin->nat_wait_conns,
1783 &tcp_nat_probe->clientIdentity.hashPubKey,
1784 session) == GNUNET_YES);
1785 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1786 &session->target.hashPubKey, session,
1787 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1788 session->last_activity = GNUNET_TIME_absolute_get ();
1789 session->inbound = GNUNET_NO;
1790 LOG (GNUNET_ERROR_TYPE_DEBUG,
1791 "Found address `%s' for incoming connection\n",
1792 GNUNET_a2s (vaddr, alen));
1793 switch (((const struct sockaddr *) vaddr)->sa_family)
1797 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1798 t4->t4_port = s4->sin_port;
1799 t4->ipv4_addr = s4->sin_addr.s_addr;
1801 session->addrlen = sizeof (struct IPv4TcpAddress);
1805 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1806 t6->t6_port = s6->sin6_port;
1807 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1809 session->addrlen = sizeof (struct IPv6TcpAddress);
1812 GNUNET_break_op (0);
1813 LOG (GNUNET_ERROR_TYPE_DEBUG,
1814 "Bad address for incoming connection!\n");
1815 GNUNET_free (vaddr);
1816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1817 disconnect_session (session);
1820 GNUNET_free (vaddr);
1821 GNUNET_break (NULL == session->client);
1822 GNUNET_SERVER_client_keep (client);
1823 session->client = client;
1824 inc_sessions (plugin, session, __LINE__);
1825 GNUNET_STATISTICS_update (plugin->env->stats,
1826 gettext_noop ("# TCP sessions active"), 1,
1828 process_pending_messages (session);
1829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1834 * We've received a welcome from this peer via TCP. Possibly create a
1835 * fresh client record and send back our welcome.
1837 * @param cls closure
1838 * @param client identification of the client
1839 * @param message the actual message
1842 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
1843 const struct GNUNET_MessageHeader *message)
1845 struct Plugin *plugin = cls;
1846 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1847 struct Session *session;
1850 struct IPv4TcpAddress *t4;
1851 struct IPv6TcpAddress *t6;
1852 const struct sockaddr_in *s4;
1853 const struct sockaddr_in6 *s6;
1856 memcmp (&wm->clientIdentity, plugin->env->my_identity,
1857 sizeof (struct GNUNET_PeerIdentity)))
1859 /* refuse connections from ourselves */
1860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1863 LOG (GNUNET_ERROR_TYPE_DEBUG,
1864 "Received %s message from `%4s'\n", "WELCOME",
1865 GNUNET_i2s (&wm->clientIdentity));
1866 GNUNET_STATISTICS_update (plugin->env->stats,
1867 gettext_noop ("# TCP WELCOME messages received"), 1,
1869 session = lookup_session_by_client (plugin, client);
1870 if (session != NULL)
1872 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1874 LOG (GNUNET_ERROR_TYPE_DEBUG,
1875 "Found existing session %p for peer `%s'\n",
1877 GNUNET_a2s (vaddr, alen));
1878 GNUNET_free (vaddr);
1883 GNUNET_SERVER_client_keep (client);
1884 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
1885 plugin->cur_connections++;
1886 if (plugin->cur_connections == plugin->max_connections)
1887 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1889 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
1890 session->inbound = GNUNET_YES;
1891 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1893 if (alen == sizeof (struct sockaddr_in))
1896 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1897 t4->t4_port = s4->sin_port;
1898 t4->ipv4_addr = s4->sin_addr.s_addr;
1900 session->addrlen = sizeof (struct IPv4TcpAddress);
1902 else if (alen == sizeof (struct sockaddr_in6))
1905 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1906 t6->t6_port = s6->sin6_port;
1907 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1909 session->addrlen = sizeof (struct IPv6TcpAddress);
1912 struct GNUNET_ATS_Information ats;
1913 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
1914 session->ats_address_network_type = ats.value;
1916 GNUNET_free (vaddr);
1920 LOG (GNUNET_ERROR_TYPE_DEBUG,
1921 "Did not obtain TCP socket address for incoming connection\n");
1923 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1924 &session->target.hashPubKey,
1926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1927 inc_sessions (plugin, session, __LINE__);
1930 if (session->expecting_welcome != GNUNET_YES)
1932 GNUNET_break_op (0);
1933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1936 session->last_activity = GNUNET_TIME_absolute_get ();
1937 session->expecting_welcome = GNUNET_NO;
1940 process_pending_messages (session);
1942 GNUNET_SERVER_client_set_timeout (client,
1943 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1949 * Task to signal the server that we can continue
1950 * receiving from the TCP client now.
1952 * @param cls the 'struct Session*'
1953 * @param tc task context (unused)
1956 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1958 struct Session *session = cls;
1960 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1961 reschedule_session_timeout (session);
1963 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1968 * We've received data for this peer via TCP. Unbox,
1969 * compute latency and forward.
1971 * @param cls closure
1972 * @param client identification of the client
1973 * @param message the actual message
1976 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
1977 const struct GNUNET_MessageHeader *message)
1979 struct Plugin *plugin = cls;
1980 struct Session *session;
1981 struct GNUNET_TIME_Relative delay;
1984 type = ntohs (message->type);
1985 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
1986 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
1988 /* We don't want to propagate WELCOME and NAT Probe messages up! */
1989 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1992 session = lookup_session_by_client (plugin, client);
1993 if (NULL == session)
1995 /* No inbound session found */
1999 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2000 LOG (GNUNET_ERROR_TYPE_ERROR,
2001 "Received unexpected %u bytes of type %u from `%s'\n",
2002 (unsigned int) ntohs (message->size),
2003 (unsigned int) ntohs (message->type),
2004 GNUNET_a2s(vaddr, alen));
2005 GNUNET_break_op (0);
2006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2007 GNUNET_free_non_null(vaddr);
2010 else if (GNUNET_YES == session->expecting_welcome)
2012 /* Session is expecting WELCOME message */
2016 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2017 LOG (GNUNET_ERROR_TYPE_ERROR,
2018 "Received unexpected %u bytes of type %u from `%s'\n",
2019 (unsigned int) ntohs (message->size),
2020 (unsigned int) ntohs (message->type),
2021 GNUNET_a2s(vaddr, alen));
2022 GNUNET_break_op (0);
2023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2024 GNUNET_free_non_null(vaddr);
2028 session->last_activity = GNUNET_TIME_absolute_get ();
2029 LOG (GNUNET_ERROR_TYPE_DEBUG,
2030 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2031 (unsigned int) ntohs (message->size),
2032 (unsigned int) ntohs (message->type),
2033 GNUNET_i2s (&session->target));
2035 GNUNET_STATISTICS_update (plugin->env->stats,
2036 gettext_noop ("# bytes received via TCP"),
2037 ntohs (message->size), GNUNET_NO);
2038 struct GNUNET_ATS_Information distance[2];
2040 distance[0].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
2041 distance[0].value = htonl (1);
2042 distance[1].type = htonl (GNUNET_ATS_NETWORK_TYPE);
2043 distance[1].value = session->ats_address_network_type;
2044 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2046 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
2047 &session->target.hashPubKey,
2050 delay = plugin->env->receive (plugin->env->cls,
2054 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2055 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2056 plugin->env->update_address_metrics (plugin->env->cls,
2058 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2059 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2061 (struct GNUNET_ATS_Information *) &distance,
2064 reschedule_session_timeout (session);
2066 if (delay.rel_value == 0)
2068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2072 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "Throttling receiving from `%s' for %llu ms\n",
2074 GNUNET_i2s (&session->target),
2075 (unsigned long long) delay.rel_value);
2076 GNUNET_SERVER_disable_receive_done_warning (client);
2077 session->receive_delay_task =
2078 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2084 * Functions with this signature are called whenever a peer
2085 * is disconnected on the network level.
2087 * @param cls closure
2088 * @param client identification of the client
2091 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2093 struct Plugin *plugin = cls;
2094 struct Session *session;
2098 session = lookup_session_by_client (plugin, client);
2099 if (session == NULL)
2100 return; /* unknown, nothing to do */
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2103 GNUNET_i2s (&session->target),
2105 NULL) ? tcp_address_to_string (session->plugin,
2110 if (plugin->cur_connections == plugin->max_connections)
2111 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2113 if (plugin->cur_connections < 1)
2116 plugin->cur_connections--;
2118 GNUNET_STATISTICS_update (session->plugin->env->stats,
2120 ("# network-level TCP disconnect events"), 1,
2122 disconnect_session (session);
2127 * We can now send a probe message, copy into buffer to really send.
2129 * @param cls closure, a struct TCPProbeContext
2130 * @param size max size to copy
2131 * @param buf buffer to copy message to
2132 * @return number of bytes copied into buf
2135 notify_send_probe (void *cls, size_t size, void *buf)
2137 struct TCPProbeContext *tcp_probe_ctx = cls;
2138 struct Plugin *plugin = tcp_probe_ctx->plugin;
2141 tcp_probe_ctx->transmit_handle = NULL;
2142 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2146 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2147 GNUNET_free (tcp_probe_ctx);
2150 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2151 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2152 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2153 tcp_probe_ctx->sock);
2154 ret = sizeof (tcp_probe_ctx->message);
2155 GNUNET_free (tcp_probe_ctx);
2161 * Function called by the NAT subsystem suggesting another peer wants
2162 * to connect to us via connection reversal. Try to connect back to the
2165 * @param cls closure
2166 * @param addr address to try
2167 * @param addrlen number of bytes in addr
2170 try_connection_reversal (void *cls, const struct sockaddr *addr,
2173 struct Plugin *plugin = cls;
2174 struct GNUNET_CONNECTION_Handle *sock;
2175 struct TCPProbeContext *tcp_probe_ctx;
2178 * We have received an ICMP response, ostensibly from a peer
2179 * that wants to connect to us! Send a message to establish a connection.
2181 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2184 /* failed for some odd reason (out of sockets?); ignore attempt */
2188 /* FIXME: do we need to track these probe context objects so that
2189 * we can clean them up on plugin unload? */
2190 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2191 tcp_probe_ctx->message.header.size =
2192 htons (sizeof (struct TCP_NAT_ProbeMessage));
2193 tcp_probe_ctx->message.header.type =
2194 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2195 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2196 sizeof (struct GNUNET_PeerIdentity));
2197 tcp_probe_ctx->plugin = plugin;
2198 tcp_probe_ctx->sock = sock;
2199 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2201 tcp_probe_ctx->transmit_handle =
2202 GNUNET_CONNECTION_notify_transmit_ready (sock,
2203 ntohs (tcp_probe_ctx->
2204 message.header.size),
2205 GNUNET_TIME_UNIT_FOREVER_REL,
2213 * Session was idle, so disconnect it
2216 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2218 GNUNET_assert (NULL != cls);
2219 struct Session *s = cls;
2221 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Session %p was idle for %llu ms, disconnecting\n",
2224 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2225 /* call session destroy function */
2226 disconnect_session(s);
2231 * Start session timeout
2234 start_session_timeout (struct Session *s)
2236 GNUNET_assert (NULL != s);
2237 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2238 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2242 "Timeout for session %p set to %llu ms\n",
2243 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2248 * Increment session timeout due to activity
2251 reschedule_session_timeout (struct Session *s)
2253 GNUNET_assert (NULL != s);
2254 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2256 GNUNET_SCHEDULER_cancel (s->timeout_task);
2257 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2261 "Timeout rescheduled for session %p set to %llu ms\n",
2262 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2270 stop_session_timeout (struct Session *s)
2272 GNUNET_assert (NULL != s);
2274 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2276 GNUNET_SCHEDULER_cancel (s->timeout_task);
2277 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "Timeout stopped for session %p canceled\n",
2280 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2286 * Entry point for the plugin.
2288 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2289 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2292 libgnunet_plugin_transport_tcp_init (void *cls)
2294 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2295 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2296 sizeof (struct WelcomeMessage)},
2297 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2298 sizeof (struct TCP_NAT_ProbeMessage)},
2299 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2302 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2303 struct GNUNET_TRANSPORT_PluginFunctions *api;
2304 struct Plugin *plugin;
2305 struct GNUNET_SERVICE_Context *service;
2306 unsigned long long aport;
2307 unsigned long long bport;
2308 unsigned long long max_connections;
2310 struct GNUNET_TIME_Relative idle_timeout;
2312 struct sockaddr **addrs;
2313 socklen_t *addrlens;
2315 if (NULL == env->receive)
2317 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2318 initialze the plugin or the API */
2319 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2321 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2322 api->address_to_string = &tcp_address_to_string;
2323 api->string_to_address = &tcp_string_to_address;
2327 GNUNET_assert (NULL != env->cfg);
2328 GNUNET_assert (NULL != env->stats);
2330 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2333 max_connections = 128;
2337 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2338 &bport)) || (bport > 65535) ||
2340 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2341 "ADVERTISED-PORT", &aport)) &&
2344 LOG (GNUNET_ERROR_TYPE_ERROR,
2346 ("Require valid port number for service `%s' in configuration!\n"),
2356 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2357 if (service == NULL)
2359 LOG (GNUNET_ERROR_TYPE_WARNING,
2360 _("Failed to start service.\n"));
2367 plugin = GNUNET_malloc (sizeof (struct Plugin));
2368 plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create (max_connections, GNUNET_YES);
2369 plugin->max_connections = max_connections;
2370 plugin->cur_connections = 0;
2371 plugin->open_port = bport;
2372 plugin->adv_port = aport;
2374 plugin->lsock = NULL;
2375 if ((service != NULL) &&
2378 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2382 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret,
2383 (const struct sockaddr **) addrs, addrlens,
2384 &tcp_nat_port_map_callback,
2385 &try_connection_reversal, plugin);
2389 GNUNET_assert (addrs[ret] != NULL);
2390 GNUNET_free (addrs[ret]);
2392 GNUNET_free_non_null (addrs);
2393 GNUNET_free_non_null (addrlens);
2397 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2398 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2399 &try_connection_reversal, plugin);
2401 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2403 api->send = &tcp_plugin_send;
2404 api->get_session = &tcp_plugin_get_session;
2406 api->disconnect = &tcp_plugin_disconnect;
2407 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2408 api->check_address = &tcp_plugin_check_address;
2409 api->address_to_string = &tcp_address_to_string;
2410 api->string_to_address = &tcp_string_to_address;
2411 plugin->service = service;
2412 if (service != NULL)
2414 plugin->server = GNUNET_SERVICE_get_server (service);
2419 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2420 "TIMEOUT", &idle_timeout))
2422 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2423 "transport-tcp", "TIMEOUT");
2424 if (plugin->nat != NULL)
2425 GNUNET_NAT_unregister (plugin->nat);
2426 GNUNET_free (plugin);
2431 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2432 NULL, idle_timeout, GNUNET_YES);
2434 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2435 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2437 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2439 plugin->handlers[i].callback_cls = plugin;
2441 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2442 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2443 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
2445 LOG (GNUNET_ERROR_TYPE_INFO,
2446 _("TCP transport listening on port %llu\n"), bport);
2448 LOG (GNUNET_ERROR_TYPE_INFO,
2450 ("TCP transport not listening on any port (client only)\n"));
2452 LOG (GNUNET_ERROR_TYPE_INFO,
2454 ("TCP transport advertises itself as being on port %llu\n"),
2456 /* Initially set connections to 0 */
2457 GNUNET_STATISTICS_set(plugin->env->stats,
2458 gettext_noop ("# TCP sessions active"), 0,
2465 * Exit point from the plugin.
2468 libgnunet_plugin_transport_tcp_done (void *cls)
2470 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2471 struct Plugin *plugin = api->cls;
2472 struct TCPProbeContext *tcp_probe;
2479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2481 /* Removing leftover sessions */
2482 GNUNET_CONTAINER_multihashmap_iterate(plugin->sessionmap, &session_disconnect_it, NULL);
2483 /* Removing leftover NAT sessions */
2484 GNUNET_CONTAINER_multihashmap_iterate(plugin->nat_wait_conns, &session_disconnect_it, NULL);
2486 if (plugin->service != NULL)
2487 GNUNET_SERVICE_stop (plugin->service);
2489 GNUNET_SERVER_destroy (plugin->server);
2490 GNUNET_free (plugin->handlers);
2491 if (plugin->nat != NULL)
2492 GNUNET_NAT_unregister (plugin->nat);
2493 while (NULL != (tcp_probe = plugin->probe_head))
2495 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2497 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2498 GNUNET_free (tcp_probe);
2500 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2501 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessionmap);
2502 GNUNET_free (plugin);
2507 /* end of plugin_transport_tcp.c */