2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 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_connection_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
43 #define DEBUG_TCP_NAT GNUNET_NO
46 * Initial handshake message for a session.
51 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
53 struct GNUNET_MessageHeader header;
56 * Identity of the node connecting (TCP client)
58 struct GNUNET_PeerIdentity clientIdentity;
64 * Basically a WELCOME message, but with the purpose
65 * of giving the waiting peer a client handle to use
67 struct TCP_NAT_ProbeMessage
70 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
72 struct GNUNET_MessageHeader header;
75 * Identity of the sender of the message.
77 struct GNUNET_PeerIdentity clientIdentity;
83 * Context for sending a NAT probe via TCP.
85 struct TCPProbeContext
89 * Active probes are kept in a DLL.
91 struct TCPProbeContext *next;
94 * Active probes are kept in a DLL.
96 struct TCPProbeContext *prev;
101 struct GNUNET_CONNECTION_Handle *sock;
104 * Message to be sent.
106 struct TCP_NAT_ProbeMessage message;
109 * Handle to the transmission.
111 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
114 * Transport plugin handle.
116 struct Plugin *plugin;
121 * Network format for IPv4 addresses.
123 struct IPv4TcpAddress
126 * IPv4 address, in network byte order.
128 uint32_t ipv4_addr GNUNET_PACKED;
131 * Port number, in network byte order.
133 uint16_t t4_port GNUNET_PACKED;
139 * Network format for IPv6 addresses.
141 struct IPv6TcpAddress
146 struct in6_addr ipv6_addr GNUNET_PACKED;
149 * Port number, in network byte order.
151 uint16_t t6_port GNUNET_PACKED;
157 * Encapsulation of all of the state of the plugin.
163 * Information kept for each message that is yet to
166 struct PendingMessage
170 * This is a doubly-linked list.
172 struct PendingMessage *next;
175 * This is a doubly-linked list.
177 struct PendingMessage *prev;
180 * The pending message
185 * Continuation function to call once the message
186 * has been sent. Can be NULL if there is no
187 * continuation to call.
189 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
192 * Closure for transmit_cont.
194 void *transmit_cont_cls;
197 * Timeout value for the pending message.
199 struct GNUNET_TIME_Absolute timeout;
202 * So that the gnunet-service-transport can group messages together,
203 * these pending messages need to accept a message buffer and size
204 * instead of just a GNUNET_MessageHeader.
212 * Session handle for TCP connections.
220 struct SessionHeader header;
223 * Stored in a linked list.
225 struct Session *next;
228 * Pointer to the global plugin struct.
230 struct Plugin *plugin;
233 * The client (used to identify this connection)
235 struct GNUNET_SERVER_Client *client;
238 * Messages currently pending for transmission
239 * to this peer, if any.
241 struct PendingMessage *pending_messages_head;
244 * Messages currently pending for transmission
245 * to this peer, if any.
247 struct PendingMessage *pending_messages_tail;
250 * Handle for pending transmission request.
252 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
255 * To whom are we talking to (set to our identity
256 * if we are still waiting for the welcome message)
258 struct GNUNET_PeerIdentity target;
261 * ID of task used to delay receiving more to throttle sender.
263 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
266 * Address of the other peer (either based on our 'connect'
267 * call or on our 'accept' call).
272 * Last activity on this connection. Used to select preferred
275 struct GNUNET_TIME_Absolute last_activity;
278 * Length of connect_addr.
283 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
285 int expecting_welcome;
288 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
293 * Was this session created using NAT traversal?
301 * Encapsulation of all of the state of the plugin.
308 struct GNUNET_TRANSPORT_PluginEnvironment *env;
313 struct GNUNET_CONNECTION_Handle *lsock;
316 * Our handle to the NAT module.
318 struct GNUNET_NAT_Handle *nat;
321 * List of open TCP sessions.
323 struct Session *sessions;
326 * Handle to the network service.
328 struct GNUNET_SERVICE_Context *service;
331 * Handle to the server for this service.
333 struct GNUNET_SERVER_Handle *server;
336 * Copy of the handler array where the closures are
337 * set to this struct's instance.
339 struct GNUNET_SERVER_MessageHandler *handlers;
342 * Map of peers we have tried to contact behind a NAT
344 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
347 * List of active TCP probes.
349 struct TCPProbeContext *probe_head;
352 * List of active TCP probes.
354 struct TCPProbeContext *probe_tail;
357 * Handle for (DYN)DNS lookup of our external IP.
359 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
362 * How many more TCP sessions are we allowed to open right now?
364 unsigned long long max_connections;
367 * ID of task used to update our addresses when one expires.
369 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
372 * Port that we are actually listening on.
377 * Port that the user said we would have visible to the
386 * Function to check if an inbound connection is acceptable.
387 * Mostly used to limit the total number of open connections
390 * @param cls the 'struct Plugin'
391 * @param ucred credentials, if available, otherwise NULL
392 * @param addr address
393 * @param addrlen length of address
394 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
395 * for unknown address family (will be denied).
398 plugin_tcp_access_check (void *cls,
399 const struct GNUNET_CONNECTION_Credentials *ucred,
400 const struct sockaddr *addr,
403 struct Plugin *plugin = cls;
405 if (0 == plugin->max_connections)
407 plugin->max_connections--;
413 * Our external IP address/port mapping has changed.
415 * @param cls closure, the 'struct LocalAddrList'
416 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
417 * the previous (now invalid) one
418 * @param addr either the previous or the new public IP address
419 * @param addrlen actual lenght of the address
422 tcp_nat_port_map_callback (void *cls,
424 const struct sockaddr *addr,
427 struct Plugin *plugin = cls;
428 struct IPv4TcpAddress t4;
429 struct IPv6TcpAddress t6;
433 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
435 "NPMC called with %d for address `%s'\n",
437 GNUNET_a2s (addr, addrlen));
438 /* convert 'addr' to our internal format */
439 switch (addr->sa_family)
442 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
443 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
444 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
449 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
450 memcpy (&t6.ipv6_addr,
451 &((struct sockaddr_in6 *) addr)->sin6_addr,
452 sizeof (struct in6_addr));
453 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
461 /* modify our published address list */
462 plugin->env->notify_address (plugin->env->cls,
469 * Function called for a quick conversion of the binary address to
470 * a numeric address. Note that the caller must not free the
471 * address and that the next call to this function is allowed
472 * to override the address again.
474 * @param cls closure ('struct Plugin*')
475 * @param addr binary address
476 * @param addrlen length of the address
477 * @return string representing the same address
480 tcp_address_to_string (void *cls,
484 static char rbuf[INET6_ADDRSTRLEN + 12];
485 char buf[INET6_ADDRSTRLEN];
489 const struct IPv4TcpAddress *t4;
490 const struct IPv6TcpAddress *t6;
494 if (addrlen == sizeof (struct IPv6TcpAddress))
498 port = ntohs (t6->t6_port);
499 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
502 else if (addrlen == sizeof (struct IPv4TcpAddress))
506 port = ntohs (t4->t4_port);
507 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
512 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
514 _("Unexpected address length: %u bytes\n"),
515 (unsigned int) addrlen);
519 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
521 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
524 GNUNET_snprintf (rbuf,
526 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
534 * Find the session handle for the given client.
536 * @param plugin the plugin
537 * @param client which client to find the session handle for
538 * @return NULL if no matching session exists
540 static struct Session *
541 find_session_by_client (struct Plugin *plugin,
542 const struct GNUNET_SERVER_Client *client)
546 ret = plugin->sessions;
547 while ((ret != NULL) && (client != ret->client))
554 * Create a new session. Also queues a welcome message.
556 * @param plugin the plugin
557 * @param target peer to connect to
558 * @param client client to use
559 * @param is_nat this a NAT session, we should wait for a client to
560 * connect to us from an address, then assign that to
562 * @return new session object
564 static struct Session *
565 create_session (struct Plugin *plugin,
566 const struct GNUNET_PeerIdentity *target,
567 struct GNUNET_SERVER_Client *client,
571 struct PendingMessage *pm;
572 struct WelcomeMessage welcome;
574 if (is_nat != GNUNET_YES)
575 GNUNET_assert (client != NULL);
577 GNUNET_assert (client == NULL);
579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
581 "Creating new session for peer `%4s'\n",
582 GNUNET_i2s (target));
584 ret = GNUNET_malloc (sizeof (struct Session));
585 ret->last_activity = GNUNET_TIME_absolute_get ();
586 ret->plugin = plugin;
587 ret->is_nat = is_nat;
588 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
590 ret->next = plugin->sessions;
591 plugin->sessions = ret;
593 ret->client = client;
594 ret->target = *target;
595 ret->expecting_welcome = GNUNET_YES;
596 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
597 pm->msg = (const char*) &pm[1];
598 pm->message_size = sizeof (struct WelcomeMessage);
599 welcome.header.size = htons (sizeof (struct WelcomeMessage));
600 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
601 welcome.clientIdentity = *plugin->env->my_identity;
602 memcpy (&pm[1], &welcome, sizeof (welcome));
603 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
604 GNUNET_STATISTICS_update (plugin->env->stats,
605 gettext_noop ("# bytes currently in TCP buffers"),
608 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
609 ret->pending_messages_tail,
611 if (is_nat != GNUNET_YES)
612 GNUNET_STATISTICS_update (plugin->env->stats,
613 gettext_noop ("# TCP sessions active"),
621 * If we have pending messages, ask the server to
622 * transmit them (schedule the respective tasks, etc.)
624 * @param session for which session should we do this
626 static void process_pending_messages (struct Session *session);
630 * Function called to notify a client about the socket
631 * being ready to queue more data. "buf" will be
632 * NULL and "size" zero if the socket was closed for
633 * writing in the meantime.
636 * @param size number of bytes available in buf
637 * @param buf where the callee should write the message
638 * @return number of bytes written to buf
641 do_transmit (void *cls, size_t size, void *buf)
643 struct Session *session = cls;
644 struct GNUNET_PeerIdentity pid;
645 struct Plugin *plugin;
646 struct PendingMessage *pos;
647 struct PendingMessage *hd;
648 struct PendingMessage *tl;
649 struct GNUNET_TIME_Absolute now;
653 GNUNET_assert (session != NULL);
654 session->transmit_handle = NULL;
655 plugin = session->plugin;
659 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
661 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
662 GNUNET_i2s (&session->target));
664 /* timeout; cancel all messages that have already expired */
668 now = GNUNET_TIME_absolute_get ();
669 while ( (NULL != (pos = session->pending_messages_head)) &&
670 (pos->timeout.abs_value <= now.abs_value) )
672 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
673 session->pending_messages_tail,
676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
678 "Failed to transmit %u byte message to `%4s'.\n",
680 GNUNET_i2s (&session->target));
682 ret += pos->message_size;
683 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
685 /* do this call before callbacks (so that if callbacks destroy
686 session, they have a chance to cancel actions done by this
688 process_pending_messages (session);
689 pid = session->target;
690 /* no do callbacks and do not use session again since
691 the callbacks may abort the session */
692 while (NULL != (pos = hd))
694 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
695 if (pos->transmit_cont != NULL)
696 pos->transmit_cont (pos->transmit_cont_cls,
697 &pid, GNUNET_SYSERR);
700 GNUNET_STATISTICS_update (plugin->env->stats,
701 gettext_noop ("# bytes currently in TCP buffers"),
704 GNUNET_STATISTICS_update (plugin->env->stats,
705 gettext_noop ("# bytes discarded by TCP (timeout)"),
710 /* copy all pending messages that would fit */
715 while (NULL != (pos = session->pending_messages_head))
717 if (ret + pos->message_size > size)
719 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
720 session->pending_messages_tail,
722 GNUNET_assert (size >= pos->message_size);
723 /* FIXME: this memcpy can be up to 7% of our total runtime */
724 memcpy (cbuf, pos->msg, pos->message_size);
725 cbuf += pos->message_size;
726 ret += pos->message_size;
727 size -= pos->message_size;
728 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
730 /* schedule 'continuation' before callbacks so that callbacks that
731 cancel everything don't cause us to use a session that no longer
733 process_pending_messages (session);
734 session->last_activity = GNUNET_TIME_absolute_get ();
735 pid = session->target;
736 /* we'll now call callbacks that may cancel the session; hence
737 we should not use 'session' after this point */
738 while (NULL != (pos = hd))
740 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
741 if (pos->transmit_cont != NULL)
742 pos->transmit_cont (pos->transmit_cont_cls,
746 GNUNET_assert (hd == NULL);
747 GNUNET_assert (tl == NULL);
749 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
751 "Transmitting %u bytes\n",
754 GNUNET_STATISTICS_update (plugin->env->stats,
755 gettext_noop ("# bytes currently in TCP buffers"),
758 GNUNET_STATISTICS_update (plugin->env->stats,
759 gettext_noop ("# bytes transmitted via TCP"),
767 * If we have pending messages, ask the server to
768 * transmit them (schedule the respective tasks, etc.)
770 * @param session for which session should we do this
773 process_pending_messages (struct Session *session)
775 struct PendingMessage *pm;
777 GNUNET_assert (session->client != NULL);
778 if (session->transmit_handle != NULL)
780 if (NULL == (pm = session->pending_messages_head))
783 session->transmit_handle
784 = GNUNET_SERVER_notify_transmit_ready (session->client,
786 GNUNET_TIME_absolute_get_remaining
788 &do_transmit, session);
793 * Functions with this signature are called whenever we need
794 * to close a session due to a disconnect or failure to
795 * establish a connection.
797 * @param session session to close down
800 disconnect_session (struct Session *session)
802 struct Session *prev;
804 struct PendingMessage *pm;
807 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
809 "Disconnecting from `%4s' at %s.\n",
810 GNUNET_i2s (&session->target),
811 (session->connect_addr != NULL) ?
812 tcp_address_to_string (session->plugin,
813 session->connect_addr,
814 session->connect_alen) : "*");
816 /* remove from session list */
818 pos = session->plugin->sessions;
819 while (pos != session)
825 session->plugin->sessions = session->next;
827 prev->next = session->next;
830 if (session->transmit_handle != NULL)
832 GNUNET_CONNECTION_notify_transmit_ready_cancel
833 (session->transmit_handle);
834 session->transmit_handle = NULL;
836 while (NULL != (pm = session->pending_messages_head))
839 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
841 pm->transmit_cont != NULL
842 ? "Could not deliver message to `%4s'.\n"
843 : "Could not deliver message to `%4s', notifying.\n",
844 GNUNET_i2s (&session->target));
846 GNUNET_STATISTICS_update (session->plugin->env->stats,
847 gettext_noop ("# bytes currently in TCP buffers"),
848 - (int64_t) pm->message_size,
850 GNUNET_STATISTICS_update (session->plugin->env->stats,
851 gettext_noop ("# bytes discarded by TCP (disconnect)"),
854 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
855 session->pending_messages_tail,
857 if (NULL != pm->transmit_cont)
858 pm->transmit_cont (pm->transmit_cont_cls,
859 &session->target, GNUNET_SYSERR);
862 GNUNET_break (session->client != NULL);
863 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
865 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
866 if (session->client != NULL)
867 GNUNET_SERVER_receive_done (session->client,
870 if (session->client != NULL)
872 GNUNET_SERVER_client_drop (session->client);
873 session->client = NULL;
875 GNUNET_STATISTICS_update (session->plugin->env->stats,
876 gettext_noop ("# TCP sessions active"),
879 GNUNET_free_non_null (session->connect_addr);
880 session->plugin->env->session_end (session->plugin->env->cls,
883 GNUNET_assert (NULL == session->transmit_handle);
884 GNUNET_free (session);
889 * Given two otherwise equivalent sessions, pick the better one.
891 * @param s1 one session (also default)
892 * @param s2 other session
893 * @return "better" session (more active)
895 static struct Session *
896 select_better_session (struct Session *s1,
903 if ( (s1->expecting_welcome == GNUNET_NO) &&
904 (s2->expecting_welcome == GNUNET_YES) )
906 if ( (s1->expecting_welcome == GNUNET_YES) &&
907 (s2->expecting_welcome == GNUNET_NO) )
909 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
911 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
913 if ( (GNUNET_YES == s1->inbound) &&
914 (GNUNET_NO == s2->inbound) )
916 if ( (GNUNET_NO == s1->inbound) &&
917 (GNUNET_YES == s2->inbound) )
925 * Function that can be used by the transport service to transmit
926 * a message using the plugin. Note that in the case of a
927 * peer disconnecting, the continuation MUST be called
928 * prior to the disconnect notification itself. This function
929 * will be called with this peer's HELLO message to initiate
930 * a fresh connection to another peer.
933 * @param target who should receive this message
934 * @param msg the message to transmit
935 * @param msgbuf_size number of bytes in 'msg'
936 * @param priority how important is the message (most plugins will
937 * ignore message priority and just FIFO)
938 * @param timeout how long to wait at most for the transmission (does not
939 * require plugins to discard the message after the timeout,
940 * just advisory for the desired delay; most plugins will ignore
942 * @param session which session must be used (or NULL for "any")
943 * @param addr the address to use (can be NULL if the plugin
944 * is "on its own" (i.e. re-use existing TCP connection))
945 * @param addrlen length of the address in bytes
946 * @param force_address GNUNET_YES if the plugin MUST use the given address,
947 * GNUNET_NO means the plugin may use any other address and
948 * GNUNET_SYSERR means that only reliable existing
949 * bi-directional connections should be used (regardless
951 * @param cont continuation to call once the message has
952 * been transmitted (or if the transport is ready
953 * for the next transmission call; or if the
954 * peer disconnected...); can be NULL
955 * @param cont_cls closure for cont
956 * @return number of bytes used (on the physical network, with overheads);
957 * -1 on hard errors (i.e. address invalid); 0 is a legal value
958 * and does NOT mean that the message was not transmitted (DV and NAT)
961 tcp_plugin_send (void *cls,
962 const struct GNUNET_PeerIdentity *target,
966 struct GNUNET_TIME_Relative timeout,
967 struct Session *session,
971 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
973 struct Plugin *plugin = cls;
974 struct Session *cand_session;
975 struct Session *next;
976 struct PendingMessage *pm;
977 struct GNUNET_CONNECTION_Handle *sa;
981 struct sockaddr_in a4;
982 struct sockaddr_in6 a6;
983 const struct IPv4TcpAddress *t4;
984 const struct IPv6TcpAddress *t6;
985 unsigned int is_natd;
987 GNUNET_STATISTICS_update (plugin->env->stats,
988 gettext_noop ("# bytes TCP was asked to transmit"),
991 /* FIXME: we could do this cheaper with a hash table
992 where we could restrict the iteration to entries that match
993 the target peer... */
998 next = plugin->sessions;
999 while (NULL != (session = next))
1001 next = session->next;
1002 GNUNET_assert (session->client != NULL);
1003 if (0 != memcmp (target,
1005 sizeof (struct GNUNET_PeerIdentity)))
1007 if ( ( (GNUNET_SYSERR == force_address) &&
1008 (session->expecting_welcome == GNUNET_NO) ) ||
1009 (GNUNET_NO == force_address) )
1011 cand_session = select_better_session (cand_session,
1015 if (GNUNET_SYSERR == force_address)
1017 GNUNET_break (GNUNET_YES == force_address);
1023 if ( (addrlen != session->connect_alen) &&
1024 (session->is_nat == GNUNET_NO) )
1026 if ((0 != memcmp (session->connect_addr,
1028 addrlen)) && (session->is_nat == GNUNET_NO))
1030 cand_session = select_better_session (cand_session,
1033 session = cand_session;
1035 if ( (session == NULL) &&
1039 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1041 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1042 GNUNET_i2s (target));
1044 GNUNET_STATISTICS_update (plugin->env->stats,
1045 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1050 if (session == NULL)
1052 if (addrlen == sizeof (struct IPv6TcpAddress))
1056 memset (&a6, 0, sizeof (a6));
1057 #if HAVE_SOCKADDR_IN_SIN_LEN
1058 a6.sin6_len = sizeof (a6);
1060 a6.sin6_family = AF_INET6;
1061 a6.sin6_port = t6->t6_port;
1062 if (t6->t6_port == 0)
1063 is_natd = GNUNET_YES;
1064 memcpy (&a6.sin6_addr,
1066 sizeof (struct in6_addr));
1070 else if (addrlen == sizeof (struct IPv4TcpAddress))
1074 memset (&a4, 0, sizeof (a4));
1075 #if HAVE_SOCKADDR_IN_SIN_LEN
1076 a4.sin_len = sizeof (a4);
1078 a4.sin_family = AF_INET;
1079 a4.sin_port = t4->t4_port;
1080 if (t4->t4_port == 0)
1081 is_natd = GNUNET_YES;
1082 a4.sin_addr.s_addr = t4->ipv4_addr;
1088 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1090 _("Address of unexpected length: %u\n"),
1096 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1097 return -1; /* NAT client only works with IPv4 addresses */
1098 if (0 == plugin->max_connections)
1099 return -1; /* saturated */
1101 if ( (is_natd == GNUNET_YES) &&
1102 (NULL != plugin->nat) &&
1103 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1104 &target->hashPubKey)) )
1107 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1109 _("Found valid IPv4 NAT address (creating session)!\n"));
1111 session = create_session (plugin,
1116 /* create new message entry */
1117 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1118 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1119 pm->msg = (const char*) &pm[1];
1120 memcpy (&pm[1], msg, msgbuf_size);
1121 /* FIXME: this memcpy can be up to 7% of our total run-time
1122 (for transport service) */
1123 pm->message_size = msgbuf_size;
1124 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1125 pm->transmit_cont = cont;
1126 pm->transmit_cont_cls = cont_cls;
1128 /* append pm to pending_messages list */
1129 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1130 session->pending_messages_tail,
1131 session->pending_messages_tail,
1134 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1135 &target->hashPubKey,
1137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1139 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1141 "Created NAT WAIT connection to `%4s' at `%s'\n",
1142 GNUNET_i2s (target),
1143 GNUNET_a2s (sb, sbs));
1145 GNUNET_NAT_run_client (plugin->nat, &a4);
1148 if ( (is_natd == GNUNET_YES) &&
1149 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1150 &target->hashPubKey)) )
1152 /* Only do one NAT punch attempt per peer identity */
1155 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1159 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1161 "Failed to create connection to `%4s' at `%s'\n",
1162 GNUNET_i2s (target),
1163 GNUNET_a2s (sb, sbs));
1165 GNUNET_STATISTICS_update (plugin->env->stats,
1166 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1171 GNUNET_assert (0 != plugin->max_connections);
1172 plugin->max_connections--;
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1176 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1177 GNUNET_i2s (target),
1178 GNUNET_a2s (sb, sbs));
1180 session = create_session (plugin,
1182 GNUNET_SERVER_connect_socket (plugin->server,
1185 session->connect_addr = GNUNET_malloc (addrlen);
1186 memcpy (session->connect_addr,
1189 session->connect_alen = addrlen;
1191 GNUNET_assert (session != NULL);
1192 GNUNET_assert (session->client != NULL);
1194 GNUNET_SERVER_client_set_timeout(session->client, CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
1195 GNUNET_STATISTICS_update (plugin->env->stats,
1196 gettext_noop ("# bytes currently in TCP buffers"),
1199 /* create new message entry */
1200 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1201 pm->msg = (const char*) &pm[1];
1202 memcpy (&pm[1], msg, msgbuf_size);
1203 pm->message_size = msgbuf_size;
1204 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1205 pm->transmit_cont = cont;
1206 pm->transmit_cont_cls = cont_cls;
1208 /* append pm to pending_messages list */
1209 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1210 session->pending_messages_tail,
1211 session->pending_messages_tail,
1214 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1216 "Asked to transmit %u bytes to `%s', added message to list.\n",
1218 GNUNET_i2s (target));
1220 process_pending_messages (session);
1226 * Function that can be called to force a disconnect from the
1227 * specified neighbour. This should also cancel all previously
1228 * scheduled transmissions. Obviously the transmission may have been
1229 * partially completed already, which is OK. The plugin is supposed
1230 * to close the connection (if applicable) and no longer call the
1231 * transmit continuation(s).
1233 * Finally, plugin MUST NOT call the services's receive function to
1234 * notify the service that the connection to the specified target was
1235 * closed after a getting this call.
1237 * @param cls closure
1238 * @param target peer for which the last transmission is
1242 tcp_plugin_disconnect (void *cls,
1243 const struct GNUNET_PeerIdentity *target)
1245 struct Plugin *plugin = cls;
1246 struct Session *session;
1247 struct Session *next;
1248 struct PendingMessage *pm;
1251 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1253 "Asked to cancel session with `%4s'\n",
1254 GNUNET_i2s (target));
1256 next = plugin->sessions;
1257 while (NULL != (session = next))
1259 next = session->next;
1260 if (0 != memcmp (target,
1262 sizeof (struct GNUNET_PeerIdentity)))
1264 pm = session->pending_messages_head;
1267 pm->transmit_cont = NULL;
1268 pm->transmit_cont_cls = NULL;
1271 GNUNET_STATISTICS_update (session->plugin->env->stats,
1272 gettext_noop ("# transport-service disconnect requests for TCP"),
1275 disconnect_session (session);
1281 * Context for address to string conversion.
1283 struct PrettyPrinterContext
1286 * Function to call with the result.
1288 GNUNET_TRANSPORT_AddressStringCallback asc;
1291 * Clsoure for 'asc'.
1296 * Port to add after the IP address.
1303 * Append our port and forward the result.
1305 * @param cls the 'struct PrettyPrinterContext*'
1306 * @param hostname hostname part of the address
1309 append_port (void *cls, const char *hostname)
1311 struct PrettyPrinterContext *ppc = cls;
1314 if (hostname == NULL)
1316 ppc->asc (ppc->asc_cls, NULL);
1320 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1321 ppc->asc (ppc->asc_cls, ret);
1327 * Convert the transports address to a nice, human-readable
1330 * @param cls closure
1331 * @param type name of the transport that generated the address
1332 * @param addr one of the addresses of the host, NULL for the last address
1333 * the specific address format depends on the transport
1334 * @param addrlen length of the address
1335 * @param numeric should (IP) addresses be displayed in numeric form?
1336 * @param timeout after how long should we give up?
1337 * @param asc function to call on each string
1338 * @param asc_cls closure for asc
1341 tcp_plugin_address_pretty_printer (void *cls,
1346 struct GNUNET_TIME_Relative timeout,
1347 GNUNET_TRANSPORT_AddressStringCallback asc,
1350 struct PrettyPrinterContext *ppc;
1353 struct sockaddr_in a4;
1354 struct sockaddr_in6 a6;
1355 const struct IPv4TcpAddress *t4;
1356 const struct IPv6TcpAddress *t6;
1359 if (addrlen == sizeof (struct IPv6TcpAddress))
1362 memset (&a6, 0, sizeof (a6));
1363 a6.sin6_family = AF_INET6;
1364 a6.sin6_port = t6->t6_port;
1365 memcpy (&a6.sin6_addr,
1367 sizeof (struct in6_addr));
1368 port = ntohs (t6->t6_port);
1372 else if (addrlen == sizeof (struct IPv4TcpAddress))
1375 memset (&a4, 0, sizeof (a4));
1376 a4.sin_family = AF_INET;
1377 a4.sin_port = t4->t4_port;
1378 a4.sin_addr.s_addr = t4->ipv4_addr;
1379 port = ntohs (t4->t4_port);
1385 /* invalid address */
1386 GNUNET_break_op (0);
1387 asc (asc_cls, NULL);
1390 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1392 ppc->asc_cls = asc_cls;
1394 GNUNET_RESOLVER_hostname_get (sb,
1396 !numeric, timeout, &append_port, ppc);
1401 * Check if the given port is plausible (must be either our listen
1402 * port or our advertised port), or any port if we are behind NAT
1403 * and do not have a port open. If it is neither, we return
1406 * @param plugin global variables
1407 * @param in_port port number to check
1408 * @return GNUNET_OK if port is either open_port or adv_port
1411 check_port (struct Plugin *plugin,
1414 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1416 return GNUNET_SYSERR;
1421 * Function that will be called to check if a binary address for this
1422 * plugin is well-formed and corresponds to an address for THIS peer
1423 * (as per our configuration). Naturally, if absolutely necessary,
1424 * plugins can be a bit conservative in their answer, but in general
1425 * plugins should make sure that the address does not redirect
1426 * traffic to a 3rd party that might try to man-in-the-middle our
1429 * @param cls closure, our 'struct Plugin*'
1430 * @param addr pointer to the address
1431 * @param addrlen length of addr
1432 * @return GNUNET_OK if this is a plausible address for this peer
1433 * and transport, GNUNET_SYSERR if not
1436 tcp_plugin_check_address (void *cls,
1440 struct Plugin *plugin = cls;
1441 struct IPv4TcpAddress *v4;
1442 struct IPv6TcpAddress *v6;
1444 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1445 (addrlen != sizeof (struct IPv6TcpAddress)))
1447 GNUNET_break_op (0);
1448 return GNUNET_SYSERR;
1450 if (addrlen == sizeof (struct IPv4TcpAddress))
1452 v4 = (struct IPv4TcpAddress *) addr;
1454 check_port (plugin, ntohs (v4->t4_port)))
1455 return GNUNET_SYSERR;
1457 GNUNET_NAT_test_address (plugin->nat,
1458 &v4->ipv4_addr, sizeof (struct in_addr)))
1459 return GNUNET_SYSERR;
1463 v6 = (struct IPv6TcpAddress *) addr;
1464 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1466 GNUNET_break_op (0);
1467 return GNUNET_SYSERR;
1470 check_port (plugin, ntohs (v6->t6_port)))
1471 return GNUNET_SYSERR;
1473 GNUNET_NAT_test_address (plugin->nat,
1474 &v6->ipv6_addr, sizeof (struct in6_addr)))
1475 return GNUNET_SYSERR;
1482 * We've received a nat probe from this peer via TCP. Finish
1483 * creating the client session and resume sending of queued
1486 * @param cls closure
1487 * @param client identification of the client
1488 * @param message the actual message
1491 handle_tcp_nat_probe (void *cls,
1492 struct GNUNET_SERVER_Client *client,
1493 const struct GNUNET_MessageHeader *message)
1495 struct Plugin *plugin = cls;
1496 struct Session *session;
1497 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1500 struct IPv4TcpAddress *t4;
1501 struct IPv6TcpAddress *t6;
1502 const struct sockaddr_in *s4;
1503 const struct sockaddr_in6 *s6;
1506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1508 "received NAT probe\n");
1510 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1511 * a connection to this peer by running gnunet-nat-client. This peer
1512 * received the punch message and now wants us to use the new connection
1513 * as the default for that peer. Do so and then send a WELCOME message
1514 * so we can really be connected!
1516 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1523 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1524 if (0 == memcmp (&tcp_nat_probe->clientIdentity,
1525 plugin->env->my_identity,
1526 sizeof (struct GNUNET_PeerIdentity)))
1528 /* refuse connections from ourselves */
1529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1533 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1534 &tcp_nat_probe->clientIdentity.hashPubKey);
1535 if (session == NULL)
1538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1540 "Did NOT find session for NAT probe!\n");
1542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1546 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1548 "Found session for NAT probe!\n");
1550 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1551 &tcp_nat_probe->clientIdentity.hashPubKey,
1552 session) == GNUNET_YES);
1554 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1557 GNUNET_free (session);
1558 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1562 GNUNET_SERVER_client_keep (client);
1563 session->client = client;
1564 session->last_activity = GNUNET_TIME_absolute_get ();
1565 session->inbound = GNUNET_NO;
1568 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1570 "Found address `%s' for incoming connection\n",
1571 GNUNET_a2s (vaddr, alen));
1573 switch (((const struct sockaddr *)vaddr)->sa_family)
1577 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1578 t4->t4_port = s4->sin_port;
1579 t4->ipv4_addr = s4->sin_addr.s_addr;
1580 session->connect_addr = t4;
1581 session->connect_alen = sizeof (struct IPv4TcpAddress);
1585 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1586 t6->t6_port = s6->sin6_port;
1587 memcpy (&t6->ipv6_addr,
1589 sizeof (struct in6_addr));
1590 session->connect_addr = t6;
1591 session->connect_alen = sizeof (struct IPv6TcpAddress);
1594 GNUNET_break_op (0);
1596 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1598 "Bad address for incoming connection!\n");
1600 GNUNET_free (vaddr);
1601 GNUNET_SERVER_client_drop (client);
1602 GNUNET_free (session);
1603 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1606 GNUNET_free (vaddr);
1608 session->next = plugin->sessions;
1609 plugin->sessions = session;
1610 GNUNET_STATISTICS_update (plugin->env->stats,
1611 gettext_noop ("# TCP sessions active"),
1614 process_pending_messages (session);
1615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1620 * We've received a welcome from this peer via TCP. Possibly create a
1621 * fresh client record and send back our welcome.
1623 * @param cls closure
1624 * @param client identification of the client
1625 * @param message the actual message
1628 handle_tcp_welcome (void *cls,
1629 struct GNUNET_SERVER_Client *client,
1630 const struct GNUNET_MessageHeader *message)
1632 struct Plugin *plugin = cls;
1633 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1634 struct Session *session;
1637 struct IPv4TcpAddress *t4;
1638 struct IPv6TcpAddress *t6;
1639 const struct sockaddr_in *s4;
1640 const struct sockaddr_in6 *s6;
1642 if (0 == memcmp (&wm->clientIdentity,
1643 plugin->env->my_identity,
1644 sizeof (struct GNUNET_PeerIdentity)))
1646 /* refuse connections from ourselves */
1647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1651 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1653 "Received %s message from `%4s'.\n",
1655 GNUNET_i2s (&wm->clientIdentity));
1657 GNUNET_STATISTICS_update (plugin->env->stats,
1658 gettext_noop ("# TCP WELCOME messages received"),
1661 session = find_session_by_client (plugin, client);
1663 if (session == NULL)
1666 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1668 "Received %s message from a `%4s', creating new session\n",
1670 GNUNET_i2s (&wm->clientIdentity));
1672 GNUNET_SERVER_client_keep (client);
1673 session = create_session (plugin,
1674 &wm->clientIdentity,
1677 session->inbound = GNUNET_YES;
1679 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1682 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1684 "Found address `%s' for incoming connection\n",
1685 GNUNET_a2s (vaddr, alen));
1687 if (alen == sizeof (struct sockaddr_in))
1690 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1691 t4->t4_port = s4->sin_port;
1692 t4->ipv4_addr = s4->sin_addr.s_addr;
1693 session->connect_addr = t4;
1694 session->connect_alen = sizeof (struct IPv4TcpAddress);
1696 else if (alen == sizeof (struct sockaddr_in6))
1699 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1700 t6->t6_port = s6->sin6_port;
1701 memcpy (&t6->ipv6_addr,
1703 sizeof (struct in6_addr));
1704 session->connect_addr = t6;
1705 session->connect_alen = sizeof (struct IPv6TcpAddress);
1708 GNUNET_free (vaddr);
1713 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1715 "Did not obtain TCP socket address for incoming connection\n");
1718 process_pending_messages (session);
1724 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1726 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1728 "Found address `%s' (already have session)\n",
1729 GNUNET_a2s (vaddr, alen));
1730 GNUNET_free (vaddr);
1735 if (session->expecting_welcome != GNUNET_YES)
1737 GNUNET_break_op (0);
1738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1741 session->last_activity = GNUNET_TIME_absolute_get ();
1742 session->expecting_welcome = GNUNET_NO;
1743 GNUNET_SERVER_client_set_timeout(client, CONNECTED_LATENCY_EVALUATION_MAX_DELAY);
1744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1749 * Task to signal the server that we can continue
1750 * receiving from the TCP client now.
1752 * @param cls the 'struct Session*'
1753 * @param tc task context (unused)
1756 delayed_done (void *cls,
1757 const struct GNUNET_SCHEDULER_TaskContext *tc)
1759 struct Session *session = cls;
1760 struct GNUNET_TIME_Relative delay;
1762 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1763 delay = session->plugin->env->receive (session->plugin->env->cls,
1769 if (delay.rel_value == 0)
1770 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1772 session->receive_delay_task =
1773 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
1778 * We've received data for this peer via TCP. Unbox,
1779 * compute latency and forward.
1781 * @param cls closure
1782 * @param client identification of the client
1783 * @param message the actual message
1786 handle_tcp_data (void *cls,
1787 struct GNUNET_SERVER_Client *client,
1788 const struct GNUNET_MessageHeader *message)
1790 struct Plugin *plugin = cls;
1791 struct Session *session;
1792 struct GNUNET_TIME_Relative delay;
1795 type = ntohs (message->type);
1796 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
1797 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
1799 /* We don't want to propagate WELCOME and NAT Probe messages up! */
1800 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1803 session = find_session_by_client (plugin, client);
1804 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
1806 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1809 session->last_activity = GNUNET_TIME_absolute_get ();
1811 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1813 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1814 (unsigned int) ntohs (message->size),
1815 (unsigned int) ntohs (message->type),
1816 GNUNET_i2s (&session->target));
1818 GNUNET_STATISTICS_update (plugin->env->stats,
1819 gettext_noop ("# bytes received via TCP"),
1820 ntohs (message->size),
1822 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1823 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1824 distance[0].value = htonl (1);
1825 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1826 distance[1].value = htonl (0);
1827 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
1828 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1831 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
1832 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
1833 if (delay.rel_value == 0)
1835 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1840 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1842 "Throttling receiving from `%s' for %llu ms\n",
1843 GNUNET_i2s (&session->target),
1844 (unsigned long long) delay.rel_value);
1846 GNUNET_SERVER_disable_receive_done_warning (client);
1847 session->receive_delay_task =
1848 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
1854 * Functions with this signature are called whenever a peer
1855 * is disconnected on the network level.
1857 * @param cls closure
1858 * @param client identification of the client
1861 disconnect_notify (void *cls,
1862 struct GNUNET_SERVER_Client *client)
1864 struct Plugin *plugin = cls;
1865 struct Session *session;
1869 plugin->max_connections++;
1870 session = find_session_by_client (plugin, client);
1871 if (session == NULL)
1872 return; /* unknown, nothing to do */
1874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1876 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
1877 GNUNET_i2s (&session->target),
1878 (session->connect_addr != NULL) ?
1879 tcp_address_to_string (session->plugin,
1880 session->connect_addr,
1881 session->connect_alen) : "*");
1883 GNUNET_STATISTICS_update (session->plugin->env->stats,
1884 gettext_noop ("# network-level TCP disconnect events"),
1887 disconnect_session (session);
1892 * We can now send a probe message, copy into buffer to really send.
1894 * @param cls closure, a struct TCPProbeContext
1895 * @param size max size to copy
1896 * @param buf buffer to copy message to
1897 * @return number of bytes copied into buf
1900 notify_send_probe (void *cls,
1904 struct TCPProbeContext *tcp_probe_ctx = cls;
1905 struct Plugin *plugin = tcp_probe_ctx->plugin;
1908 tcp_probe_ctx->transmit_handle = NULL;
1909 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
1914 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
1915 GNUNET_free(tcp_probe_ctx);
1918 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
1919 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
1920 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
1921 tcp_probe_ctx->sock);
1922 ret = sizeof(tcp_probe_ctx->message);
1923 GNUNET_free(tcp_probe_ctx);
1929 * Function called by the NAT subsystem suggesting another peer wants
1930 * to connect to us via connection reversal. Try to connect back to the
1933 * @param cls closure
1934 * @param addr address to try
1935 * @param addrlen number of bytes in addr
1938 try_connection_reversal (void *cls,
1939 const struct sockaddr *addr,
1942 struct Plugin *plugin = cls;
1943 struct GNUNET_CONNECTION_Handle *sock;
1944 struct TCPProbeContext *tcp_probe_ctx;
1947 * We have received an ICMP response, ostensibly from a peer
1948 * that wants to connect to us! Send a message to establish a connection.
1950 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
1955 /* failed for some odd reason (out of sockets?); ignore attempt */
1959 /* FIXME: do we need to track these probe context objects so that
1960 we can clean them up on plugin unload? */
1962 = GNUNET_malloc(sizeof(struct TCPProbeContext));
1963 tcp_probe_ctx->message.header.size
1964 = htons(sizeof(struct TCP_NAT_ProbeMessage));
1965 tcp_probe_ctx->message.header.type
1966 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
1967 memcpy (&tcp_probe_ctx->message.clientIdentity,
1968 plugin->env->my_identity,
1969 sizeof(struct GNUNET_PeerIdentity));
1970 tcp_probe_ctx->plugin = plugin;
1971 tcp_probe_ctx->sock = sock;
1972 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
1975 tcp_probe_ctx->transmit_handle
1976 = GNUNET_CONNECTION_notify_transmit_ready (sock,
1977 ntohs (tcp_probe_ctx->message.header.size),
1978 GNUNET_TIME_UNIT_FOREVER_REL,
1979 ¬ify_send_probe, tcp_probe_ctx);
1985 * Entry point for the plugin.
1987 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1988 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1991 libgnunet_plugin_transport_tcp_init (void *cls)
1993 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1994 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1995 sizeof (struct WelcomeMessage)},
1996 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
1997 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2000 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2001 struct GNUNET_TRANSPORT_PluginFunctions *api;
2002 struct Plugin *plugin;
2003 struct GNUNET_SERVICE_Context *service;
2004 unsigned long long aport;
2005 unsigned long long bport;
2006 unsigned long long max_connections;
2008 struct GNUNET_TIME_Relative idle_timeout;
2010 struct sockaddr **addrs;
2011 socklen_t *addrlens;
2014 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2018 max_connections = 128;
2022 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2028 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2034 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2036 _("Require valid port number for service `%s' in configuration!\n"),
2046 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2047 if (service == NULL)
2049 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2051 _("Failed to start service.\n"));
2060 plugin = GNUNET_malloc (sizeof (struct Plugin));
2061 plugin->max_connections = max_connections;
2062 plugin->open_port = bport;
2063 plugin->adv_port = aport;
2065 plugin->lsock = NULL;
2066 if ( (service != NULL) &&
2068 (ret = GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2073 plugin->nat = GNUNET_NAT_register (env->cfg,
2077 (const struct sockaddr **) addrs,
2079 &tcp_nat_port_map_callback,
2080 &try_connection_reversal,
2085 GNUNET_assert (addrs[ret] != NULL);
2086 GNUNET_free (addrs[ret]);
2088 GNUNET_free_non_null (addrs);
2089 GNUNET_free_non_null (addrlens);
2093 plugin->nat = GNUNET_NAT_register (env->cfg,
2098 &try_connection_reversal,
2101 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2103 api->send = &tcp_plugin_send;
2104 api->disconnect = &tcp_plugin_disconnect;
2105 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2106 api->check_address = &tcp_plugin_check_address;
2107 api->address_to_string = &tcp_address_to_string;
2108 plugin->service = service;
2109 if (service != NULL)
2111 plugin->server = GNUNET_SERVICE_get_server (service);
2116 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2121 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2123 _("Failed to find option %s in section %s!\n"),
2126 if (plugin->nat != NULL)
2127 GNUNET_NAT_unregister (plugin->nat);
2128 GNUNET_free (plugin);
2132 plugin->server = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin, NULL,
2133 idle_timeout, GNUNET_YES);
2135 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2136 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2138 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2140 plugin->handlers[i].callback_cls = plugin;
2141 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2142 GNUNET_SERVER_disconnect_notify (plugin->server,
2145 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
2147 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2149 _("TCP transport listening on port %llu\n"),
2152 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2154 _("TCP transport not listening on any port (client only)\n"));
2156 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2158 _("TCP transport advertises itself as being on port %llu\n"),
2165 * Exit point from the plugin.
2168 libgnunet_plugin_transport_tcp_done (void *cls)
2170 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2171 struct Plugin *plugin = api->cls;
2172 struct Session *session;
2173 struct TCPProbeContext *tcp_probe;
2175 while (NULL != (session = plugin->sessions))
2176 disconnect_session (session);
2177 if (plugin->service != NULL)
2178 GNUNET_SERVICE_stop (plugin->service);
2180 GNUNET_SERVER_destroy (plugin->server);
2181 GNUNET_free (plugin->handlers);
2182 if (plugin->nat != NULL)
2183 GNUNET_NAT_unregister (plugin->nat);
2184 while (NULL != (tcp_probe = plugin->probe_head))
2186 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2189 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
2190 GNUNET_free (tcp_probe);
2192 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2193 GNUNET_free (plugin);
2198 /* end of plugin_transport_tcp.c */