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__)
44 #define PLUGIN_NAME "tcp"
47 * How long until we give up on establishing an NAT connection?
50 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
53 GNUNET_NETWORK_STRUCT_BEGIN
58 static uint32_t myoptions;
61 * Initial handshake message for a session.
66 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
68 struct GNUNET_MessageHeader header;
71 * Identity of the node connecting (TCP client)
73 struct GNUNET_PeerIdentity clientIdentity;
79 * Basically a WELCOME message, but with the purpose
80 * of giving the waiting peer a client handle to use
82 struct TCP_NAT_ProbeMessage
85 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
87 struct GNUNET_MessageHeader header;
90 * Identity of the sender of the message.
92 struct GNUNET_PeerIdentity clientIdentity;
95 GNUNET_NETWORK_STRUCT_END
98 * Context for sending a NAT probe via TCP.
100 struct TCPProbeContext
104 * Active probes are kept in a DLL.
106 struct TCPProbeContext *next;
109 * Active probes are kept in a DLL.
111 struct TCPProbeContext *prev;
116 struct GNUNET_CONNECTION_Handle *sock;
119 * Message to be sent.
121 struct TCP_NAT_ProbeMessage message;
124 * Handle to the transmission.
126 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
129 * Transport plugin handle.
131 struct Plugin *plugin;
135 GNUNET_NETWORK_STRUCT_BEGIN
138 * Network format for IPv4 addresses.
140 struct IPv4TcpAddress
143 * Optional options and flags for this address
148 * IPv4 address, in network byte order.
150 uint32_t ipv4_addr GNUNET_PACKED;
153 * Port number, in network byte order.
155 uint16_t t4_port GNUNET_PACKED;
161 * Network format for IPv6 addresses.
163 struct IPv6TcpAddress
166 * Optional flags for this address
173 struct in6_addr ipv6_addr GNUNET_PACKED;
176 * Port number, in network byte order.
178 uint16_t t6_port GNUNET_PACKED;
181 GNUNET_NETWORK_STRUCT_END
184 * Encapsulation of all of the state of the plugin.
190 * Information kept for each message that is yet to
193 struct PendingMessage
197 * This is a doubly-linked list.
199 struct PendingMessage *next;
202 * This is a doubly-linked list.
204 struct PendingMessage *prev;
207 * The pending message
212 * Continuation function to call once the message
213 * has been sent. Can be NULL if there is no
214 * continuation to call.
216 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
219 * Closure for transmit_cont.
221 void *transmit_cont_cls;
224 * Timeout value for the pending message.
226 struct GNUNET_TIME_Absolute timeout;
229 * So that the gnunet-service-transport can group messages together,
230 * these pending messages need to accept a message buffer and size
231 * instead of just a GNUNET_MessageHeader.
239 * Session handle for TCP connections.
244 * To whom are we talking to (set to our identity
245 * if we are still waiting for the welcome message)
247 struct GNUNET_PeerIdentity target;
252 struct SessionHeader header;
255 * Pointer to the global plugin struct.
257 struct Plugin *plugin;
260 * The client (used to identify this connection)
262 struct GNUNET_SERVER_Client *client;
265 * Task cleaning up a NAT client connection establishment attempt;
267 GNUNET_SCHEDULER_TaskIdentifier nat_connection_timeout;
270 * Messages currently pending for transmission
271 * to this peer, if any.
273 struct PendingMessage *pending_messages_head;
276 * Messages currently pending for transmission
277 * to this peer, if any.
279 struct PendingMessage *pending_messages_tail;
282 * Handle for pending transmission request.
284 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
287 * ID of task used to delay receiving more to throttle sender.
289 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
292 * Session timeout task
294 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
297 * Address of the other peer (either based on our 'connect'
298 * call or on our 'accept' call).
300 * struct IPv4TcpAddress or struct IPv6TcpAddress
306 * Length of connect_addr.
311 * Last activity on this connection. Used to select preferred
314 struct GNUNET_TIME_Absolute last_activity;
317 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
319 int expecting_welcome;
322 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
327 * Was this session created using NAT traversal?
332 * ATS network type in NBO
334 uint32_t ats_address_network_type;
339 * Encapsulation of all of the state of the plugin.
346 struct GNUNET_TRANSPORT_PluginEnvironment *env;
351 struct GNUNET_CONNECTION_Handle *lsock;
354 * Our handle to the NAT module.
356 struct GNUNET_NAT_Handle *nat;
359 * Map from peer identities to sessions for the given peer.
361 struct GNUNET_CONTAINER_MultiHashMap *sessionmap;
364 * Handle to the network service.
366 struct GNUNET_SERVICE_Context *service;
369 * Handle to the server for this service.
371 struct GNUNET_SERVER_Handle *server;
374 * Copy of the handler array where the closures are
375 * set to this struct's instance.
377 struct GNUNET_SERVER_MessageHandler *handlers;
380 * Map of peers we have tried to contact behind a NAT
382 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
385 * List of active TCP probes.
387 struct TCPProbeContext *probe_head;
390 * List of active TCP probes.
392 struct TCPProbeContext *probe_tail;
395 * Handle for (DYN)DNS lookup of our external IP.
397 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
400 * How many more TCP sessions are we allowed to open right now?
402 unsigned long long max_connections;
405 * How many more TCP sessions do we have right now?
407 unsigned long long cur_connections;
410 * ID of task used to update our addresses when one expires.
412 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
415 * Port that we are actually listening on.
420 * Port that the user said we would have visible to the
429 * Start session timeout
432 start_session_timeout (struct Session *s);
436 * Increment session timeout due to activity
439 reschedule_session_timeout (struct Session *s);
446 stop_session_timeout (struct Session *s);
451 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
454 static unsigned int sessions;
458 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
461 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
462 if (sessions != size)
463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
469 GNUNET_i2s (&session->target),
470 tcp_address_to_string (NULL, session->addr, session->addrlen));
475 dec_sessions (struct Plugin *plugin, struct Session *session, int line)
477 GNUNET_assert (sessions > 0);
478 unsigned int size = GNUNET_CONTAINER_multihashmap_size(plugin->sessionmap);
480 if (sessions != size)
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
487 GNUNET_i2s (&session->target),
488 tcp_address_to_string (NULL, session->addr, session->addrlen));
494 * Function to check if an inbound connection is acceptable.
495 * Mostly used to limit the total number of open connections
498 * @param cls the 'struct Plugin'
499 * @param ucred credentials, if available, otherwise NULL
500 * @param addr address
501 * @param addrlen length of address
502 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
503 * for unknown address family (will be denied).
506 plugin_tcp_access_check (void *cls,
507 const struct GNUNET_CONNECTION_Credentials *ucred,
508 const struct sockaddr *addr, socklen_t addrlen)
510 struct Plugin *plugin = cls;
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Accepting new incoming TCP connection from `%s'\n",
513 GNUNET_a2s (addr, addrlen));
514 if (plugin->cur_connections >= plugin->max_connections)
516 plugin->cur_connections ++;
522 * Our external IP address/port mapping has changed.
524 * @param cls closure, the 'struct LocalAddrList'
525 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
526 * the previous (now invalid) one
527 * @param addr either the previous or the new public IP address
528 * @param addrlen actual lenght of the address
531 tcp_nat_port_map_callback (void *cls, int add_remove,
532 const struct sockaddr *addr, socklen_t addrlen)
534 struct Plugin *plugin = cls;
535 struct IPv4TcpAddress t4;
536 struct IPv6TcpAddress t6;
540 LOG (GNUNET_ERROR_TYPE_INFO,
541 "NAT notification to %s address `%s'\n",
542 (GNUNET_YES == add_remove) ? "add" : "remove",
543 GNUNET_a2s (addr, addrlen));
544 /* convert 'addr' to our internal format */
545 switch (addr->sa_family)
548 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
549 memset (&t4,0, sizeof (t4));
550 t4.options = htonl (myoptions);
551 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
552 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
557 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
558 memset (&t6, 0, sizeof (t6));
559 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
560 sizeof (struct in6_addr));
561 t6.options = htonl (myoptions);
562 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
570 /* modify our published address list */
571 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
576 * Function called for a quick conversion of the binary address to
577 * a numeric address. Note that the caller must not free the
578 * address and that the next call to this function is allowed
579 * to override the address again.
581 * @param cls closure ('struct Plugin*')
582 * @param addr binary address
583 * @param addrlen length of the address
584 * @return string representing the same address
587 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
589 static char rbuf[INET6_ADDRSTRLEN + 12];
590 char buf[INET6_ADDRSTRLEN];
594 const struct IPv4TcpAddress *t4;
595 const struct IPv6TcpAddress *t6;
603 case sizeof (struct IPv6TcpAddress):
606 port = ntohs (t6->t6_port);
607 options = ntohl (t6->options);
608 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
611 case sizeof (struct IPv4TcpAddress):
614 port = ntohs (t4->t4_port);
615 options = ntohl (t4->options);
616 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
620 LOG (GNUNET_ERROR_TYPE_ERROR,
621 _("Unexpected address length: %u bytes\n"),
622 (unsigned int) addrlen);
626 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
628 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
631 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
632 PLUGIN_NAME, options, buf, port);
638 * Function called to convert a string address to
641 * @param cls closure ('struct Plugin*')
642 * @param address string address
643 * @param addrlen length of the address
644 * @param buf location to store the buffer
645 * @param added location to store the number of bytes in the buffer.
646 * If the function returns GNUNET_SYSERR, its contents are undefined.
647 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
650 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
651 void **buf, size_t *added)
653 struct sockaddr_storage socket_address;
659 /* Format tcp.options.address:port */
664 if ((NULL == addr) || (addrlen == 0))
667 return GNUNET_SYSERR;
669 if ('\0' != addr[addrlen - 1])
672 return GNUNET_SYSERR;
674 if (strlen (addr) != addrlen - 1)
677 return GNUNET_SYSERR;
679 plugin = GNUNET_strdup (addr);
680 optionstr = strchr (plugin, '.');
681 if (NULL == optionstr)
684 GNUNET_free (plugin);
685 return GNUNET_SYSERR;
689 options = atol (optionstr);
690 address = strchr (optionstr, '.');
694 GNUNET_free (plugin);
695 return GNUNET_SYSERR;
701 GNUNET_STRINGS_to_address_ip (address, strlen (address),
705 return GNUNET_SYSERR;
708 GNUNET_free (plugin);
709 switch (socket_address.ss_family)
713 struct IPv4TcpAddress *t4;
714 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
715 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
716 t4->options = htonl (options);
717 t4->ipv4_addr = in4->sin_addr.s_addr;
718 t4->t4_port = in4->sin_port;
720 *added = sizeof (struct IPv4TcpAddress);
725 struct IPv6TcpAddress *t6;
726 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
727 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
728 t6->options = htonl (options);
729 t6->ipv6_addr = in6->sin6_addr;
730 t6->t6_port = in6->sin6_port;
732 *added = sizeof (struct IPv6TcpAddress);
736 return GNUNET_SYSERR;
741 struct SessionClientCtx
743 const struct GNUNET_SERVER_Client *client;
749 session_lookup_by_client_it (void *cls,
750 const struct GNUNET_HashCode * key,
753 struct SessionClientCtx *sc_ctx = cls;
754 struct Session *s = value;
756 if (s->client == sc_ctx->client)
766 * Find the session handle for the given client.
768 * @param plugin the plugin
769 * @param client which client to find the session handle for
770 * @return NULL if no matching session exists
772 static struct Session *
773 lookup_session_by_client (struct Plugin *plugin,
774 const struct GNUNET_SERVER_Client *client)
776 struct SessionClientCtx sc_ctx;
778 sc_ctx.client = client;
780 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
786 * Create a new session. Also queues a welcome message.
788 * @param plugin the plugin
789 * @param target peer to connect to
790 * @param client client to use, reference counter must have already been increased
791 * @param is_nat this a NAT session, we should wait for a client to
792 * connect to us from an address, then assign that to
794 * @return new session object
796 static struct Session *
797 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
798 struct GNUNET_SERVER_Client *client, int is_nat)
800 struct Session *session;
801 struct PendingMessage *pm;
802 struct WelcomeMessage welcome;
804 if (GNUNET_YES != is_nat)
805 GNUNET_assert (NULL != client);
807 GNUNET_assert (NULL == client);
809 LOG (GNUNET_ERROR_TYPE_DEBUG,
810 "Creating new session for peer `%4s'\n",
811 GNUNET_i2s (target));
812 session = GNUNET_malloc (sizeof (struct Session));
813 session->last_activity = GNUNET_TIME_absolute_get ();
814 session->plugin = plugin;
815 session->is_nat = is_nat;
816 session->client = client;
817 session->target = *target;
818 session->expecting_welcome = GNUNET_YES;
819 session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
820 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
821 sizeof (struct WelcomeMessage));
822 pm->msg = (const char *) &pm[1];
823 pm->message_size = sizeof (struct WelcomeMessage);
824 welcome.header.size = htons (sizeof (struct WelcomeMessage));
825 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
826 welcome.clientIdentity = *plugin->env->my_identity;
827 memcpy (&pm[1], &welcome, sizeof (welcome));
828 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
829 GNUNET_STATISTICS_update (plugin->env->stats,
830 gettext_noop ("# bytes currently in TCP buffers"),
831 pm->message_size, GNUNET_NO);
832 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
833 session->pending_messages_tail, pm);
834 if (GNUNET_YES != is_nat)
836 GNUNET_STATISTICS_update (plugin->env->stats,
837 gettext_noop ("# TCP sessions active"), 1,
840 start_session_timeout (session);
847 * If we have pending messages, ask the server to
848 * transmit them (schedule the respective tasks, etc.)
850 * @param session for which session should we do this
853 process_pending_messages (struct Session *session);
857 * Function called to notify a client about the socket
858 * being ready to queue more data. "buf" will be
859 * NULL and "size" zero if the socket was closed for
860 * writing in the meantime.
863 * @param size number of bytes available in buf
864 * @param buf where the callee should write the message
865 * @return number of bytes written to buf
868 do_transmit (void *cls, size_t size, void *buf)
870 struct Session *session = cls;
871 struct GNUNET_PeerIdentity pid;
872 struct Plugin *plugin;
873 struct PendingMessage *pos;
874 struct PendingMessage *hd;
875 struct PendingMessage *tl;
876 struct GNUNET_TIME_Absolute now;
880 GNUNET_assert (NULL != session);
881 session->transmit_handle = NULL;
882 plugin = session->plugin;
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
887 GNUNET_i2s (&session->target));
888 /* timeout; cancel all messages that have already expired */
892 now = GNUNET_TIME_absolute_get ();
893 while ((NULL != (pos = session->pending_messages_head)) &&
894 (pos->timeout.abs_value <= now.abs_value))
896 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
897 session->pending_messages_tail, pos);
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Failed to transmit %u byte message to `%4s'.\n",
900 pos->message_size, GNUNET_i2s (&session->target));
901 ret += pos->message_size;
902 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
904 /* do this call before callbacks (so that if callbacks destroy
905 * session, they have a chance to cancel actions done by this
907 process_pending_messages (session);
908 pid = session->target;
909 /* no do callbacks and do not use session again since
910 * the callbacks may abort the session */
911 while (NULL != (pos = hd))
913 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
914 if (pos->transmit_cont != NULL)
915 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
918 GNUNET_STATISTICS_update (plugin->env->stats,
919 gettext_noop ("# bytes currently in TCP buffers"),
920 -(int64_t) ret, GNUNET_NO);
921 GNUNET_STATISTICS_update (plugin->env->stats,
923 ("# bytes discarded by TCP (timeout)"), ret,
927 /* copy all pending messages that would fit */
932 while (NULL != (pos = session->pending_messages_head))
934 if (ret + pos->message_size > size)
936 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
937 session->pending_messages_tail, pos);
938 GNUNET_assert (size >= pos->message_size);
939 LOG (GNUNET_ERROR_TYPE_DEBUG,
940 "Transmitting message of type %u\n",
941 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
942 /* FIXME: this memcpy can be up to 7% of our total runtime */
943 memcpy (cbuf, pos->msg, pos->message_size);
944 cbuf += pos->message_size;
945 ret += pos->message_size;
946 size -= pos->message_size;
947 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
949 /* schedule 'continuation' before callbacks so that callbacks that
950 * cancel everything don't cause us to use a session that no longer
952 process_pending_messages (session);
953 session->last_activity = GNUNET_TIME_absolute_get ();
954 pid = session->target;
955 /* we'll now call callbacks that may cancel the session; hence
956 * we should not use 'session' after this point */
957 while (NULL != (pos = hd))
959 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
960 if (pos->transmit_cont != NULL)
961 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
964 GNUNET_assert (hd == NULL);
965 GNUNET_assert (tl == NULL);
966 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 gettext_noop ("# bytes currently in TCP buffers"),
970 -(int64_t) ret, GNUNET_NO);
971 GNUNET_STATISTICS_update (plugin->env->stats,
972 gettext_noop ("# bytes transmitted via TCP"), ret,
979 * If we have pending messages, ask the server to
980 * transmit them (schedule the respective tasks, etc.)
982 * @param session for which session should we do this
985 process_pending_messages (struct Session *session)
987 struct PendingMessage *pm;
989 GNUNET_assert (session->client != NULL);
990 if (session->transmit_handle != NULL)
992 if (NULL == (pm = session->pending_messages_head))
995 session->transmit_handle =
996 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
997 GNUNET_TIME_absolute_get_remaining
998 (pm->timeout), &do_transmit,
1004 * Functions with this signature are called whenever we need
1005 * to close a session due to a disconnect or failure to
1006 * establish a connection.
1008 * @param session session to close down
1011 disconnect_session (struct Session *session)
1013 struct PendingMessage *pm;
1014 struct Plugin * plugin = session->plugin;
1016 LOG (GNUNET_ERROR_TYPE_DEBUG,
1017 "Disconnecting session of peer `%s' address `%s'\n",
1018 GNUNET_i2s (&session->target),
1019 tcp_address_to_string (NULL, session->addr, session->addrlen));
1021 stop_session_timeout (session);
1023 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->sessionmap, &session->target.hashPubKey, session))
1025 GNUNET_STATISTICS_update (session->plugin->env->stats,
1026 gettext_noop ("# TCP sessions active"), -1,
1028 dec_sessions (plugin, session, __LINE__);
1030 else GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->nat_wait_conns, &session->target.hashPubKey, session));
1032 /* clean up state */
1033 if (session->transmit_handle != NULL)
1035 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1036 session->transmit_handle = NULL;
1038 session->plugin->env->session_end (session->plugin->env->cls,
1039 &session->target, session);
1041 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
1043 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1044 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1047 while (NULL != (pm = session->pending_messages_head))
1049 LOG (GNUNET_ERROR_TYPE_DEBUG,
1050 pm->transmit_cont !=
1051 NULL ? "Could not deliver message to `%4s'.\n" :
1052 "Could not deliver message to `%4s', notifying.\n",
1053 GNUNET_i2s (&session->target));
1054 GNUNET_STATISTICS_update (session->plugin->env->stats,
1055 gettext_noop ("# bytes currently in TCP buffers"),
1056 -(int64_t) pm->message_size, GNUNET_NO);
1057 GNUNET_STATISTICS_update (session->plugin->env->stats,
1059 ("# bytes discarded by TCP (disconnect)"),
1060 pm->message_size, GNUNET_NO);
1061 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1062 session->pending_messages_tail, pm);
1063 if (NULL != pm->transmit_cont)
1064 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1065 GNUNET_SYSERR, pm->message_size, 0);
1068 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1070 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1071 if (NULL != session->client)
1072 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1074 if (NULL != session->client)
1076 GNUNET_SERVER_client_disconnect (session->client);
1077 GNUNET_SERVER_client_drop (session->client);
1078 session->client = NULL;
1080 GNUNET_free_non_null (session->addr);
1081 GNUNET_assert (NULL == session->transmit_handle);
1082 GNUNET_free (session);
1086 struct FindSessionContext
1092 int session_it (void *cls,
1093 const struct GNUNET_HashCode * key,
1096 struct FindSessionContext *res = cls;
1097 if (res->s == value)
1099 res->res = GNUNET_OK;
1106 int find_session (struct Plugin *plugin, struct Session *session)
1108 struct FindSessionContext session_map_res;
1109 struct FindSessionContext nat_map_res;
1111 session_map_res.s = session;
1112 session_map_res.res = GNUNET_SYSERR;
1113 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1115 nat_map_res.s = session;
1116 nat_map_res.res = GNUNET_SYSERR;
1117 GNUNET_CONTAINER_multihashmap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1119 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1122 return GNUNET_SYSERR;
1129 * Function that can be used by the transport service to transmit
1130 * a message using the plugin. Note that in the case of a
1131 * peer disconnecting, the continuation MUST be called
1132 * prior to the disconnect notification itself. This function
1133 * will be called with this peer's HELLO message to initiate
1134 * a fresh connection to another peer.
1136 * @param cls closure
1137 * @param session which session must be used
1138 * @param msgbuf the message to transmit
1139 * @param msgbuf_size number of bytes in 'msgbuf'
1140 * @param priority how important is the message (most plugins will
1141 * ignore message priority and just FIFO)
1142 * @param to how long to wait at most for the transmission (does not
1143 * require plugins to discard the message after the timeout,
1144 * just advisory for the desired delay; most plugins will ignore
1146 * @param cont continuation to call once the message has
1147 * been transmitted (or if the transport is ready
1148 * for the next transmission call; or if the
1149 * peer disconnected...); can be NULL
1150 * @param cont_cls closure for cont
1151 * @return number of bytes used (on the physical network, with overheads);
1152 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1153 * and does NOT mean that the message was not transmitted (DV)
1156 tcp_plugin_send (void *cls,
1157 struct Session *session,
1158 const char *msgbuf, size_t msgbuf_size,
1159 unsigned int priority,
1160 struct GNUNET_TIME_Relative to,
1161 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1163 struct Plugin * plugin = cls;
1164 struct PendingMessage *pm;
1166 GNUNET_assert (NULL != plugin);
1167 GNUNET_assert (NULL != session);
1169 if (GNUNET_SYSERR == find_session(plugin, session))
1171 LOG (GNUNET_ERROR_TYPE_ERROR,
1172 _("Trying to send with invalid session %p\n"));
1173 return GNUNET_SYSERR;
1176 /* create new message entry */
1177 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1178 pm->msg = (const char *) &pm[1];
1179 memcpy (&pm[1], msgbuf, msgbuf_size);
1180 pm->message_size = msgbuf_size;
1181 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1182 pm->transmit_cont = cont;
1183 pm->transmit_cont_cls = cont_cls;
1185 LOG (GNUNET_ERROR_TYPE_DEBUG,
1186 "Asked to transmit %u bytes to `%s', added message to list.\n",
1187 msgbuf_size, GNUNET_i2s (&session->target));
1189 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
1190 &session->target.hashPubKey,
1193 GNUNET_assert (session->client != NULL);
1194 reschedule_session_timeout (session);
1195 GNUNET_SERVER_client_set_timeout (session->client,
1196 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1197 GNUNET_STATISTICS_update (plugin->env->stats,
1198 gettext_noop ("# bytes currently in TCP buffers"),
1199 msgbuf_size, GNUNET_NO);
1201 /* append pm to pending_messages list */
1202 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1203 session->pending_messages_tail, pm);
1205 process_pending_messages (session);
1208 else if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(plugin->nat_wait_conns, &session->target.hashPubKey, session))
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1212 GNUNET_i2s (&session->target));
1213 reschedule_session_timeout (session);
1214 GNUNET_STATISTICS_update (plugin->env->stats,
1215 gettext_noop ("# bytes currently in TCP buffers"),
1216 msgbuf_size, GNUNET_NO);
1218 /* append pm to pending_messages list */
1219 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1220 session->pending_messages_tail, pm);
1225 LOG (GNUNET_ERROR_TYPE_ERROR,
1226 "Invalid session %p\n", session);
1228 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1231 return GNUNET_SYSERR; /* session does not exist here */
1240 struct Session *result;
1245 session_lookup_it (void *cls,
1246 const struct GNUNET_HashCode *key,
1249 struct SessionItCtx * si_ctx = cls;
1250 struct Session * session = value;
1252 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1253 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1254 LOG (GNUNET_ERROR_TYPE_DEBUG,
1255 "Comparing: %s %u <-> %s %u\n",
1263 if (session->addrlen != si_ctx->addrlen)
1267 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1272 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1273 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Comparing: %s %u <-> %s %u , OK!\n",
1283 /* Found existing session */
1284 si_ctx->result = session;
1290 * Task cleaning up a NAT connection attempt after timeout
1293 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1295 struct Session *session = cls;
1297 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1298 LOG (GNUNET_ERROR_TYPE_DEBUG,
1299 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1300 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1301 disconnect_session (session);
1306 * Create a new session to transmit data to the target
1307 * This session will used to send data to this peer and the plugin will
1308 * notify us by calling the env->session_end function
1310 * @param cls closure
1311 * @param address pointer to the GNUNET_HELLO_Address
1312 * @return the session if the address is valid, NULL otherwise
1314 static struct Session *
1315 tcp_plugin_get_session (void *cls,
1316 const struct GNUNET_HELLO_Address *address)
1318 struct Plugin *plugin = cls;
1319 struct Session *session = NULL;
1323 struct GNUNET_CONNECTION_Handle *sa;
1324 struct sockaddr_in a4;
1325 struct sockaddr_in6 a6;
1326 const struct IPv4TcpAddress *t4;
1327 const struct IPv6TcpAddress *t6;
1328 struct GNUNET_ATS_Information ats;
1329 unsigned int is_natd = GNUNET_NO;
1332 GNUNET_assert (plugin != NULL);
1333 GNUNET_assert (address != NULL);
1334 addrlen = address->address_length;
1335 LOG (GNUNET_ERROR_TYPE_DEBUG,
1336 "Trying to get session for `%s' address of peer `%s'\n",
1337 tcp_address_to_string(NULL, address->address, address->address_length),
1338 GNUNET_i2s (&address->peer));
1340 /* look for existing session */
1342 GNUNET_CONTAINER_multihashmap_contains (plugin->sessionmap,
1343 &address->peer.hashPubKey))
1345 struct SessionItCtx si_ctx;
1347 si_ctx.addr = (void *) address->address;
1348 si_ctx.addrlen = address->address_length;
1350 si_ctx.result = NULL;
1352 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap,
1353 &address->peer.hashPubKey,
1354 &session_lookup_it, &si_ctx);
1355 if (si_ctx.result != NULL)
1357 session = si_ctx.result;
1358 LOG (GNUNET_ERROR_TYPE_DEBUG,
1359 "Found exisiting session for `%s' address `%s' session %p\n",
1360 GNUNET_i2s (&address->peer),
1361 tcp_address_to_string(NULL, address->address, address->address_length),
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Existing sessions did not match address `%s' or peer `%s'\n",
1367 tcp_address_to_string(NULL, address->address, address->address_length),
1368 GNUNET_i2s (&address->peer));
1371 if (addrlen == sizeof (struct IPv6TcpAddress))
1373 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1374 t6 = address->address;
1376 memset (&a6, 0, sizeof (a6));
1377 #if HAVE_SOCKADDR_IN_SIN_LEN
1378 a6.sin6_len = sizeof (a6);
1380 a6.sin6_family = AF_INET6;
1381 a6.sin6_port = t6->t6_port;
1382 if (t6->t6_port == 0)
1383 is_natd = GNUNET_YES;
1384 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1388 else if (addrlen == sizeof (struct IPv4TcpAddress))
1390 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1391 t4 = address->address;
1393 memset (&a4, 0, sizeof (a4));
1394 #if HAVE_SOCKADDR_IN_SIN_LEN
1395 a4.sin_len = sizeof (a4);
1397 a4.sin_family = AF_INET;
1398 a4.sin_port = t4->t4_port;
1399 if (t4->t4_port == 0)
1400 is_natd = GNUNET_YES;
1401 a4.sin_addr.s_addr = t4->ipv4_addr;
1407 LOG (GNUNET_ERROR_TYPE_WARNING,
1408 _("%s:%u: Address of unexpected length %u (should be %u or %u)\n"),
1409 addrlen, sizeof (struct IPv4TcpAddress), sizeof (struct IPv6TcpAddress));
1413 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1415 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1417 /* NAT client only works with IPv4 addresses */
1421 if (plugin->cur_connections >= plugin->max_connections)
1427 if ((is_natd == GNUNET_YES) &&
1429 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1430 &address->peer.hashPubKey)))
1432 /* Only do one NAT punch attempt per peer identity */
1436 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1438 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1439 &address->peer.hashPubKey)))
1441 LOG (GNUNET_ERROR_TYPE_DEBUG,
1442 "Found valid IPv4 NAT address (creating session)!\n") ;
1443 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1444 session->addrlen = 0;
1445 session->addr = NULL;
1446 session->ats_address_network_type = ats.value;
1447 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1448 &nat_connect_timeout,
1450 GNUNET_assert (session != NULL);
1451 GNUNET_assert (GNUNET_OK ==
1452 GNUNET_CONTAINER_multihashmap_put (plugin->nat_wait_conns,
1453 &session->target.hashPubKey,
1455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1457 LOG (GNUNET_ERROR_TYPE_DEBUG,
1458 "Created NAT WAIT connection to `%4s' at `%s'\n",
1459 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1461 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1465 LOG (GNUNET_ERROR_TYPE_DEBUG,
1466 "Running NAT client for `%4s' at `%s' failed\n",
1467 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1468 disconnect_session (session);
1473 /* create new outbound session */
1474 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1475 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1478 LOG (GNUNET_ERROR_TYPE_DEBUG,
1479 "Failed to create connection to `%4s' at `%s'\n",
1480 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1483 plugin->cur_connections++;
1484 if (plugin->cur_connections == plugin->max_connections)
1485 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1489 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1491 session = create_session (plugin,
1493 GNUNET_SERVER_connect_socket (plugin->server, sa),
1495 session->addr = GNUNET_malloc (addrlen);
1496 memcpy (session->addr, address->address, addrlen);
1497 session->addrlen = addrlen;
1498 session->ats_address_network_type = ats.value;
1500 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1501 &session->target.hashPubKey,
1502 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1503 inc_sessions (plugin, session, __LINE__);
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Creating new session for `%s' address `%s' session %p\n",
1506 GNUNET_i2s (&address->peer),
1507 tcp_address_to_string(NULL, address->address, address->address_length),
1509 /* Send TCP Welcome */
1510 process_pending_messages (session);
1517 session_disconnect_it (void *cls,
1518 const struct GNUNET_HashCode * key,
1521 struct Session *session = value;
1523 GNUNET_STATISTICS_update (session->plugin->env->stats,
1525 ("# transport-service disconnect requests for TCP"),
1527 disconnect_session (session);
1533 * Function that can be called to force a disconnect from the
1534 * specified neighbour. This should also cancel all previously
1535 * scheduled transmissions. Obviously the transmission may have been
1536 * partially completed already, which is OK. The plugin is supposed
1537 * to close the connection (if applicable) and no longer call the
1538 * transmit continuation(s).
1540 * Finally, plugin MUST NOT call the services's receive function to
1541 * notify the service that the connection to the specified target was
1542 * closed after a getting this call.
1544 * @param cls closure
1545 * @param target peer for which the last transmission is
1549 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1551 struct Plugin *plugin = cls;
1553 LOG (GNUNET_ERROR_TYPE_DEBUG,
1554 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1555 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap, &target->hashPubKey, &session_disconnect_it, plugin);
1556 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->nat_wait_conns, &target->hashPubKey, &session_disconnect_it, plugin);
1561 * Context for address to string conversion.
1563 struct PrettyPrinterContext
1566 * Function to call with the result.
1568 GNUNET_TRANSPORT_AddressStringCallback asc;
1571 * Clsoure for 'asc'.
1576 * Port to add after the IP address.
1590 * Append our port and forward the result.
1592 * @param cls the 'struct PrettyPrinterContext*'
1593 * @param hostname hostname part of the address
1596 append_port (void *cls, const char *hostname)
1598 struct PrettyPrinterContext *ppc = cls;
1601 if (hostname == NULL)
1603 ppc->asc (ppc->asc_cls, NULL);
1607 if (GNUNET_YES == ppc->ipv6)
1608 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1610 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1611 ppc->asc (ppc->asc_cls, ret);
1617 * Convert the transports address to a nice, human-readable
1620 * @param cls closure
1621 * @param type name of the transport that generated the address
1622 * @param addr one of the addresses of the host, NULL for the last address
1623 * the specific address format depends on the transport
1624 * @param addrlen length of the address
1625 * @param numeric should (IP) addresses be displayed in numeric form?
1626 * @param timeout after how long should we give up?
1627 * @param asc function to call on each string
1628 * @param asc_cls closure for asc
1631 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1632 const void *addr, size_t addrlen,
1634 struct GNUNET_TIME_Relative timeout,
1635 GNUNET_TRANSPORT_AddressStringCallback asc,
1638 struct PrettyPrinterContext *ppc;
1641 struct sockaddr_in a4;
1642 struct sockaddr_in6 a6;
1643 const struct IPv4TcpAddress *t4;
1644 const struct IPv6TcpAddress *t6;
1650 if (addrlen == sizeof (struct IPv6TcpAddress))
1653 memset (&a6, 0, sizeof (a6));
1654 a6.sin6_family = AF_INET6;
1655 a6.sin6_port = t6->t6_port;
1656 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1657 port = ntohs (t6->t6_port);
1658 options = ntohl (t6->options);
1662 else if (addrlen == sizeof (struct IPv4TcpAddress))
1665 memset (&a4, 0, sizeof (a4));
1666 a4.sin_family = AF_INET;
1667 a4.sin_port = t4->t4_port;
1668 a4.sin_addr.s_addr = t4->ipv4_addr;
1669 port = ntohs (t4->t4_port);
1670 options = ntohl (t4->options);
1674 else if (0 == addrlen)
1676 asc (asc_cls, "<inbound connection>");
1677 asc (asc_cls, NULL);
1682 /* invalid address */
1683 GNUNET_break_op (0);
1684 asc (asc_cls, NULL);
1687 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1688 if (addrlen == sizeof (struct IPv6TcpAddress))
1689 ppc->ipv6 = GNUNET_YES;
1691 ppc->ipv6 = GNUNET_NO;
1693 ppc->asc_cls = asc_cls;
1695 ppc->options = options;
1696 GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric, timeout, &append_port, ppc);
1701 * Check if the given port is plausible (must be either our listen
1702 * port or our advertised port), or any port if we are behind NAT
1703 * and do not have a port open. If it is neither, we return
1706 * @param plugin global variables
1707 * @param in_port port number to check
1708 * @return GNUNET_OK if port is either open_port or adv_port
1711 check_port (struct Plugin *plugin, uint16_t in_port)
1713 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1715 return GNUNET_SYSERR;
1720 * Function that will be called to check if a binary address for this
1721 * plugin is well-formed and corresponds to an address for THIS peer
1722 * (as per our configuration). Naturally, if absolutely necessary,
1723 * plugins can be a bit conservative in their answer, but in general
1724 * plugins should make sure that the address does not redirect
1725 * traffic to a 3rd party that might try to man-in-the-middle our
1728 * @param cls closure, our 'struct Plugin*'
1729 * @param addr pointer to the address
1730 * @param addrlen length of addr
1731 * @return GNUNET_OK if this is a plausible address for this peer
1732 * and transport, GNUNET_SYSERR if not
1735 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1737 struct Plugin *plugin = cls;
1738 struct IPv4TcpAddress *v4;
1739 struct IPv6TcpAddress *v6;
1741 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1742 (addrlen != sizeof (struct IPv6TcpAddress)))
1744 GNUNET_break_op (0);
1745 return GNUNET_SYSERR;
1749 if (addrlen == sizeof (struct IPv4TcpAddress))
1751 v4 = (struct IPv4TcpAddress *) addr;
1752 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions)))
1755 return GNUNET_SYSERR;
1757 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1758 return GNUNET_SYSERR;
1760 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1761 sizeof (struct in_addr)))
1762 return GNUNET_SYSERR;
1766 v6 = (struct IPv6TcpAddress *) addr;
1767 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1769 GNUNET_break_op (0);
1770 return GNUNET_SYSERR;
1772 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions)))
1775 return GNUNET_SYSERR;
1777 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1778 return GNUNET_SYSERR;
1780 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1781 sizeof (struct in6_addr)))
1782 return GNUNET_SYSERR;
1789 * We've received a nat probe from this peer via TCP. Finish
1790 * creating the client session and resume sending of queued
1793 * @param cls closure
1794 * @param client identification of the client
1795 * @param message the actual message
1798 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1799 const struct GNUNET_MessageHeader *message)
1801 struct Plugin *plugin = cls;
1802 struct Session *session;
1803 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1806 struct IPv4TcpAddress *t4;
1807 struct IPv6TcpAddress *t6;
1808 const struct sockaddr_in *s4;
1809 const struct sockaddr_in6 *s6;
1811 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1813 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1814 * a connection to this peer by running gnunet-nat-client. This peer
1815 * received the punch message and now wants us to use the new connection
1816 * as the default for that peer. Do so and then send a WELCOME message
1817 * so we can really be connected!
1819 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1821 GNUNET_break_op (0);
1822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1826 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1828 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1829 sizeof (struct GNUNET_PeerIdentity)))
1831 /* refuse connections from ourselves */
1832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1837 GNUNET_CONTAINER_multihashmap_get (plugin->nat_wait_conns,
1839 clientIdentity.hashPubKey);
1840 if (session == NULL)
1842 LOG (GNUNET_ERROR_TYPE_DEBUG,
1843 "Did NOT find session for NAT probe!\n");
1844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1847 LOG (GNUNET_ERROR_TYPE_DEBUG,
1848 "Found session for NAT probe!\n");
1850 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1852 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1853 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1856 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1860 disconnect_session (session);
1863 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
1864 (plugin->nat_wait_conns,
1865 &tcp_nat_probe->clientIdentity.hashPubKey,
1866 session) == GNUNET_YES);
1867 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1868 &session->target.hashPubKey, session,
1869 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1870 session->last_activity = GNUNET_TIME_absolute_get ();
1871 session->inbound = GNUNET_NO;
1872 LOG (GNUNET_ERROR_TYPE_DEBUG,
1873 "Found address `%s' for incoming connection\n",
1874 GNUNET_a2s (vaddr, alen));
1875 switch (((const struct sockaddr *) vaddr)->sa_family)
1879 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1881 t4->t4_port = s4->sin_port;
1882 t4->ipv4_addr = s4->sin_addr.s_addr;
1884 session->addrlen = sizeof (struct IPv4TcpAddress);
1888 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1890 t6->t6_port = s6->sin6_port;
1891 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1893 session->addrlen = sizeof (struct IPv6TcpAddress);
1896 GNUNET_break_op (0);
1897 LOG (GNUNET_ERROR_TYPE_DEBUG,
1898 "Bad address for incoming connection!\n");
1899 GNUNET_free (vaddr);
1900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1901 disconnect_session (session);
1904 GNUNET_free (vaddr);
1905 GNUNET_break (NULL == session->client);
1906 GNUNET_SERVER_client_keep (client);
1907 session->client = client;
1908 inc_sessions (plugin, session, __LINE__);
1909 GNUNET_STATISTICS_update (plugin->env->stats,
1910 gettext_noop ("# TCP sessions active"), 1,
1912 process_pending_messages (session);
1913 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1918 * We've received a welcome from this peer via TCP. Possibly create a
1919 * fresh client record and send back our welcome.
1921 * @param cls closure
1922 * @param client identification of the client
1923 * @param message the actual message
1926 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
1927 const struct GNUNET_MessageHeader *message)
1929 struct Plugin *plugin = cls;
1930 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1931 struct Session *session;
1934 struct IPv4TcpAddress *t4;
1935 struct IPv6TcpAddress *t6;
1936 const struct sockaddr_in *s4;
1937 const struct sockaddr_in6 *s6;
1940 memcmp (&wm->clientIdentity, plugin->env->my_identity,
1941 sizeof (struct GNUNET_PeerIdentity)))
1943 /* refuse connections from ourselves */
1944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1947 LOG (GNUNET_ERROR_TYPE_DEBUG,
1948 "Received %s message from `%4s'\n", "WELCOME",
1949 GNUNET_i2s (&wm->clientIdentity));
1950 GNUNET_STATISTICS_update (plugin->env->stats,
1951 gettext_noop ("# TCP WELCOME messages received"), 1,
1953 session = lookup_session_by_client (plugin, client);
1954 if (session != NULL)
1956 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1958 LOG (GNUNET_ERROR_TYPE_DEBUG,
1959 "Found existing session %p for peer `%s'\n",
1961 GNUNET_a2s (vaddr, alen));
1962 GNUNET_free (vaddr);
1967 GNUNET_SERVER_client_keep (client);
1968 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
1969 plugin->cur_connections++;
1970 if (plugin->cur_connections == plugin->max_connections)
1971 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1973 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
1974 session->inbound = GNUNET_YES;
1975 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1977 if (alen == sizeof (struct sockaddr_in))
1980 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1981 t4->options = htonl (0);
1982 t4->t4_port = s4->sin_port;
1983 t4->ipv4_addr = s4->sin_addr.s_addr;
1985 session->addrlen = sizeof (struct IPv4TcpAddress);
1987 else if (alen == sizeof (struct sockaddr_in6))
1990 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1991 t6->options = htonl (0);
1992 t6->t6_port = s6->sin6_port;
1993 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1995 session->addrlen = sizeof (struct IPv6TcpAddress);
1998 struct GNUNET_ATS_Information ats;
1999 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
2000 session->ats_address_network_type = ats.value;
2002 GNUNET_free (vaddr);
2006 LOG (GNUNET_ERROR_TYPE_DEBUG,
2007 "Did not obtain TCP socket address for incoming connection\n");
2009 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
2010 &session->target.hashPubKey,
2012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2013 inc_sessions (plugin, session, __LINE__);
2016 if (session->expecting_welcome != GNUNET_YES)
2018 GNUNET_break_op (0);
2019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2022 session->last_activity = GNUNET_TIME_absolute_get ();
2023 session->expecting_welcome = GNUNET_NO;
2026 process_pending_messages (session);
2028 GNUNET_SERVER_client_set_timeout (client,
2029 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2030 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2035 * Task to signal the server that we can continue
2036 * receiving from the TCP client now.
2038 * @param cls the 'struct Session*'
2039 * @param tc task context (unused)
2042 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2044 struct Session *session = cls;
2046 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2047 reschedule_session_timeout (session);
2049 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2054 * We've received data for this peer via TCP. Unbox,
2055 * compute latency and forward.
2057 * @param cls closure
2058 * @param client identification of the client
2059 * @param message the actual message
2062 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2063 const struct GNUNET_MessageHeader *message)
2065 struct Plugin *plugin = cls;
2066 struct Session *session;
2067 struct GNUNET_TIME_Relative delay;
2070 type = ntohs (message->type);
2071 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2072 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2074 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2075 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2078 session = lookup_session_by_client (plugin, client);
2079 if (NULL == session)
2081 /* No inbound session found */
2085 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2086 LOG (GNUNET_ERROR_TYPE_ERROR,
2087 "Received unexpected %u bytes of type %u from `%s'\n",
2088 (unsigned int) ntohs (message->size),
2089 (unsigned int) ntohs (message->type),
2090 GNUNET_a2s(vaddr, alen));
2091 GNUNET_break_op (0);
2092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2093 GNUNET_free_non_null(vaddr);
2096 else if (GNUNET_YES == session->expecting_welcome)
2098 /* Session is expecting WELCOME message */
2102 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2103 LOG (GNUNET_ERROR_TYPE_ERROR,
2104 "Received unexpected %u bytes of type %u from `%s'\n",
2105 (unsigned int) ntohs (message->size),
2106 (unsigned int) ntohs (message->type),
2107 GNUNET_a2s(vaddr, alen));
2108 GNUNET_break_op (0);
2109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2110 GNUNET_free_non_null(vaddr);
2114 session->last_activity = GNUNET_TIME_absolute_get ();
2115 LOG (GNUNET_ERROR_TYPE_DEBUG,
2116 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2117 (unsigned int) ntohs (message->size),
2118 (unsigned int) ntohs (message->type),
2119 GNUNET_i2s (&session->target));
2121 GNUNET_STATISTICS_update (plugin->env->stats,
2122 gettext_noop ("# bytes received via TCP"),
2123 ntohs (message->size), GNUNET_NO);
2124 struct GNUNET_ATS_Information distance;
2126 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2127 distance.value = session->ats_address_network_type;
2128 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2130 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
2131 &session->target.hashPubKey,
2134 delay = plugin->env->receive (plugin->env->cls,
2138 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2139 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2140 plugin->env->update_address_metrics (plugin->env->cls,
2142 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2143 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2148 reschedule_session_timeout (session);
2150 if (delay.rel_value == 0)
2152 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2156 LOG (GNUNET_ERROR_TYPE_DEBUG,
2157 "Throttling receiving from `%s' for %llu ms\n",
2158 GNUNET_i2s (&session->target),
2159 (unsigned long long) delay.rel_value);
2160 GNUNET_SERVER_disable_receive_done_warning (client);
2161 session->receive_delay_task =
2162 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2168 * Functions with this signature are called whenever a peer
2169 * is disconnected on the network level.
2171 * @param cls closure
2172 * @param client identification of the client
2175 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2177 struct Plugin *plugin = cls;
2178 struct Session *session;
2182 session = lookup_session_by_client (plugin, client);
2183 if (session == NULL)
2184 return; /* unknown, nothing to do */
2185 LOG (GNUNET_ERROR_TYPE_DEBUG,
2186 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2187 GNUNET_i2s (&session->target),
2189 NULL) ? tcp_address_to_string (session->plugin,
2194 if (plugin->cur_connections == plugin->max_connections)
2195 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2197 if (plugin->cur_connections < 1)
2200 plugin->cur_connections--;
2202 GNUNET_STATISTICS_update (session->plugin->env->stats,
2204 ("# network-level TCP disconnect events"), 1,
2206 disconnect_session (session);
2211 * We can now send a probe message, copy into buffer to really send.
2213 * @param cls closure, a struct TCPProbeContext
2214 * @param size max size to copy
2215 * @param buf buffer to copy message to
2216 * @return number of bytes copied into buf
2219 notify_send_probe (void *cls, size_t size, void *buf)
2221 struct TCPProbeContext *tcp_probe_ctx = cls;
2222 struct Plugin *plugin = tcp_probe_ctx->plugin;
2225 tcp_probe_ctx->transmit_handle = NULL;
2226 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2230 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2231 GNUNET_free (tcp_probe_ctx);
2234 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2235 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2236 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2237 tcp_probe_ctx->sock);
2238 ret = sizeof (tcp_probe_ctx->message);
2239 GNUNET_free (tcp_probe_ctx);
2245 * Function called by the NAT subsystem suggesting another peer wants
2246 * to connect to us via connection reversal. Try to connect back to the
2249 * @param cls closure
2250 * @param addr address to try
2251 * @param addrlen number of bytes in addr
2254 try_connection_reversal (void *cls, const struct sockaddr *addr,
2257 struct Plugin *plugin = cls;
2258 struct GNUNET_CONNECTION_Handle *sock;
2259 struct TCPProbeContext *tcp_probe_ctx;
2262 * We have received an ICMP response, ostensibly from a peer
2263 * that wants to connect to us! Send a message to establish a connection.
2265 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2268 /* failed for some odd reason (out of sockets?); ignore attempt */
2272 /* FIXME: do we need to track these probe context objects so that
2273 * we can clean them up on plugin unload? */
2274 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2275 tcp_probe_ctx->message.header.size =
2276 htons (sizeof (struct TCP_NAT_ProbeMessage));
2277 tcp_probe_ctx->message.header.type =
2278 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2279 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2280 sizeof (struct GNUNET_PeerIdentity));
2281 tcp_probe_ctx->plugin = plugin;
2282 tcp_probe_ctx->sock = sock;
2283 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2285 tcp_probe_ctx->transmit_handle =
2286 GNUNET_CONNECTION_notify_transmit_ready (sock,
2287 ntohs (tcp_probe_ctx->
2288 message.header.size),
2289 GNUNET_TIME_UNIT_FOREVER_REL,
2297 * Session was idle, so disconnect it
2300 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2302 GNUNET_assert (NULL != cls);
2303 struct Session *s = cls;
2305 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2307 "Session %p was idle for %llu ms, disconnecting\n",
2308 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2309 /* call session destroy function */
2310 disconnect_session(s);
2315 * Start session timeout
2318 start_session_timeout (struct Session *s)
2320 GNUNET_assert (NULL != s);
2321 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2322 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "Timeout for session %p set to %llu ms\n",
2327 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2332 * Increment session timeout due to activity
2335 reschedule_session_timeout (struct Session *s)
2337 GNUNET_assert (NULL != s);
2338 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2340 GNUNET_SCHEDULER_cancel (s->timeout_task);
2341 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2345 "Timeout rescheduled for session %p set to %llu ms\n",
2346 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2354 stop_session_timeout (struct Session *s)
2356 GNUNET_assert (NULL != s);
2358 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2360 GNUNET_SCHEDULER_cancel (s->timeout_task);
2361 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2363 "Timeout stopped for session %p canceled\n",
2364 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2370 * Entry point for the plugin.
2372 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2373 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2376 libgnunet_plugin_transport_tcp_init (void *cls)
2378 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2379 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2380 sizeof (struct WelcomeMessage)},
2381 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2382 sizeof (struct TCP_NAT_ProbeMessage)},
2383 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2386 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2387 struct GNUNET_TRANSPORT_PluginFunctions *api;
2388 struct Plugin *plugin;
2389 struct GNUNET_SERVICE_Context *service;
2390 unsigned long long aport;
2391 unsigned long long bport;
2392 unsigned long long max_connections;
2394 struct GNUNET_TIME_Relative idle_timeout;
2397 struct sockaddr **addrs;
2398 socklen_t *addrlens;
2401 if (NULL == env->receive)
2403 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2404 initialze the plugin or the API */
2405 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2407 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2408 api->address_to_string = &tcp_address_to_string;
2409 api->string_to_address = &tcp_string_to_address;
2413 GNUNET_assert (NULL != env->cfg);
2415 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2418 max_connections = 128;
2422 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2423 &bport)) || (bport > 65535) ||
2425 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2426 "ADVERTISED-PORT", &aport)) &&
2429 LOG (GNUNET_ERROR_TYPE_ERROR,
2431 ("Require valid port number for service `%s' in configuration!\n"),
2441 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2442 if (service == NULL)
2444 LOG (GNUNET_ERROR_TYPE_WARNING,
2445 _("Failed to start service.\n"));
2452 /* Initialize my flags */
2455 plugin = GNUNET_malloc (sizeof (struct Plugin));
2456 plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create (max_connections, GNUNET_YES);
2457 plugin->max_connections = max_connections;
2458 plugin->cur_connections = 0;
2459 plugin->open_port = bport;
2460 plugin->adv_port = aport;
2462 plugin->lsock = NULL;
2463 if ((service != NULL) &&
2466 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2469 for (ret = ret_s-1; ret >= 0; ret--)
2470 LOG (GNUNET_ERROR_TYPE_INFO,
2471 "Binding to address `%s'\n",
2472 GNUNET_a2s (addrs[ret], addrlens[ret]));
2474 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2475 (const struct sockaddr **) addrs, addrlens,
2476 &tcp_nat_port_map_callback,
2477 &try_connection_reversal, plugin);
2478 for (ret = ret_s -1; ret >= 0; ret--)
2480 GNUNET_assert (addrs[ret] != NULL);
2481 GNUNET_free (addrs[ret]);
2483 GNUNET_free_non_null (addrs);
2484 GNUNET_free_non_null (addrlens);
2488 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2489 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2490 &try_connection_reversal, plugin);
2492 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2494 api->send = &tcp_plugin_send;
2495 api->get_session = &tcp_plugin_get_session;
2497 api->disconnect = &tcp_plugin_disconnect;
2498 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2499 api->check_address = &tcp_plugin_check_address;
2500 api->address_to_string = &tcp_address_to_string;
2501 api->string_to_address = &tcp_string_to_address;
2502 plugin->service = service;
2503 if (service != NULL)
2505 plugin->server = GNUNET_SERVICE_get_server (service);
2510 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2511 "TIMEOUT", &idle_timeout))
2513 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2514 "transport-tcp", "TIMEOUT");
2515 if (plugin->nat != NULL)
2516 GNUNET_NAT_unregister (plugin->nat);
2517 GNUNET_free (plugin);
2522 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2523 NULL, idle_timeout, GNUNET_YES);
2525 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2526 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2528 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2530 plugin->handlers[i].callback_cls = plugin;
2532 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2533 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2534 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
2536 LOG (GNUNET_ERROR_TYPE_INFO,
2537 _("TCP transport listening on port %llu\n"), bport);
2539 LOG (GNUNET_ERROR_TYPE_INFO,
2541 ("TCP transport not listening on any port (client only)\n"));
2543 LOG (GNUNET_ERROR_TYPE_INFO,
2545 ("TCP transport advertises itself as being on port %llu\n"),
2547 /* Initially set connections to 0 */
2548 GNUNET_assert (NULL != plugin->env->stats);
2549 GNUNET_STATISTICS_set (plugin->env->stats,
2550 gettext_noop ("# TCP sessions active"), 0,
2557 * Exit point from the plugin.
2560 libgnunet_plugin_transport_tcp_done (void *cls)
2562 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2563 struct Plugin *plugin = api->cls;
2564 struct TCPProbeContext *tcp_probe;
2571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2573 /* Removing leftover sessions */
2574 GNUNET_CONTAINER_multihashmap_iterate(plugin->sessionmap, &session_disconnect_it, NULL);
2575 /* Removing leftover NAT sessions */
2576 GNUNET_CONTAINER_multihashmap_iterate(plugin->nat_wait_conns, &session_disconnect_it, NULL);
2578 if (plugin->service != NULL)
2579 GNUNET_SERVICE_stop (plugin->service);
2581 GNUNET_SERVER_destroy (plugin->server);
2582 GNUNET_free (plugin->handlers);
2583 if (plugin->nat != NULL)
2584 GNUNET_NAT_unregister (plugin->nat);
2585 while (NULL != (tcp_probe = plugin->probe_head))
2587 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2589 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2590 GNUNET_free (tcp_probe);
2592 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2593 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessionmap);
2594 GNUNET_free (plugin);
2599 /* end of plugin_transport_tcp.c */