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
47 * Initial handshake message for a session.
52 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
54 struct GNUNET_MessageHeader header;
57 * Identity of the node connecting (TCP client)
59 struct GNUNET_PeerIdentity clientIdentity;
65 * Basically a WELCOME message, but with the purpose
66 * of giving the waiting peer a client handle to use
68 struct TCP_NAT_ProbeMessage
71 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
73 struct GNUNET_MessageHeader header;
76 * Identity of the sender of the message.
78 struct GNUNET_PeerIdentity clientIdentity;
84 * Context for sending a NAT probe via TCP.
86 struct TCPProbeContext
90 * Active probes are kept in a DLL.
92 struct TCPProbeContext *next;
95 * Active probes are kept in a DLL.
97 struct TCPProbeContext *prev;
102 struct GNUNET_CONNECTION_Handle *sock;
105 * Message to be sent.
107 struct TCP_NAT_ProbeMessage message;
110 * Handle to the transmission.
112 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
115 * Transport plugin handle.
117 struct Plugin *plugin;
122 * Network format for IPv4 addresses.
124 struct IPv4TcpAddress
127 * IPv4 address, in network byte order.
129 uint32_t ipv4_addr GNUNET_PACKED;
132 * Port number, in network byte order.
134 uint16_t t4_port GNUNET_PACKED;
140 * Network format for IPv6 addresses.
142 struct IPv6TcpAddress
147 struct in6_addr ipv6_addr GNUNET_PACKED;
150 * Port number, in network byte order.
152 uint16_t t6_port GNUNET_PACKED;
158 * Encapsulation of all of the state of the plugin.
164 * Information kept for each message that is yet to
167 struct PendingMessage
171 * This is a doubly-linked list.
173 struct PendingMessage *next;
176 * This is a doubly-linked list.
178 struct PendingMessage *prev;
181 * The pending message
186 * Continuation function to call once the message
187 * has been sent. Can be NULL if there is no
188 * continuation to call.
190 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
193 * Closure for transmit_cont.
195 void *transmit_cont_cls;
198 * Timeout value for the pending message.
200 struct GNUNET_TIME_Absolute timeout;
203 * So that the gnunet-service-transport can group messages together,
204 * these pending messages need to accept a message buffer and size
205 * instead of just a GNUNET_MessageHeader.
213 * Session handle for TCP connections.
221 struct SessionHeader header;
224 * Stored in a linked list.
226 struct Session *next;
229 * Pointer to the global plugin struct.
231 struct Plugin *plugin;
234 * The client (used to identify this connection)
236 struct GNUNET_SERVER_Client *client;
239 * Messages currently pending for transmission
240 * to this peer, if any.
242 struct PendingMessage *pending_messages_head;
245 * Messages currently pending for transmission
246 * to this peer, if any.
248 struct PendingMessage *pending_messages_tail;
251 * Handle for pending transmission request.
253 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
256 * To whom are we talking to (set to our identity
257 * if we are still waiting for the welcome message)
259 struct GNUNET_PeerIdentity target;
262 * ID of task used to delay receiving more to throttle sender.
264 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
267 * Address of the other peer (either based on our 'connect'
268 * call or on our 'accept' call).
273 * Last activity on this connection. Used to select preferred
276 struct GNUNET_TIME_Absolute last_activity;
279 * Length of connect_addr.
284 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
286 int expecting_welcome;
289 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
294 * Was this session created using NAT traversal?
302 * Encapsulation of all of the state of the plugin.
309 struct GNUNET_TRANSPORT_PluginEnvironment *env;
314 struct GNUNET_CONNECTION_Handle *lsock;
317 * Our handle to the NAT module.
319 struct GNUNET_NAT_Handle *nat;
322 * List of open TCP sessions.
324 struct Session *sessions;
327 * Handle to the network service.
329 struct GNUNET_SERVICE_Context *service;
332 * Handle to the server for this service.
334 struct GNUNET_SERVER_Handle *server;
337 * Copy of the handler array where the closures are
338 * set to this struct's instance.
340 struct GNUNET_SERVER_MessageHandler *handlers;
343 * Map of peers we have tried to contact behind a NAT
345 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
348 * List of active TCP probes.
350 struct TCPProbeContext *probe_head;
353 * List of active TCP probes.
355 struct TCPProbeContext *probe_tail;
358 * Handle for (DYN)DNS lookup of our external IP.
360 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
363 * How many more TCP sessions are we allowed to open right now?
365 unsigned long long max_connections;
368 * ID of task used to update our addresses when one expires.
370 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
373 * Port that we are actually listening on.
378 * Port that the user said we would have visible to the
387 * Function to check if an inbound connection is acceptable.
388 * Mostly used to limit the total number of open connections
391 * @param cls the 'struct Plugin'
392 * @param ucred credentials, if available, otherwise NULL
393 * @param addr address
394 * @param addrlen length of address
395 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
396 * for unknown address family (will be denied).
399 plugin_tcp_access_check (void *cls,
400 const struct GNUNET_CONNECTION_Credentials *ucred,
401 const struct sockaddr *addr,
404 struct Plugin *plugin = cls;
406 if (0 == plugin->max_connections)
408 plugin->max_connections--;
414 * Our external IP address/port mapping has changed.
416 * @param cls closure, the 'struct LocalAddrList'
417 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
418 * the previous (now invalid) one
419 * @param addr either the previous or the new public IP address
420 * @param addrlen actual lenght of the address
423 tcp_nat_port_map_callback (void *cls,
425 const struct sockaddr *addr,
428 struct Plugin *plugin = cls;
429 struct IPv4TcpAddress t4;
430 struct IPv6TcpAddress t6;
434 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
436 "NPMC called with %d for address `%s'\n",
438 GNUNET_a2s (addr, addrlen));
439 /* convert 'addr' to our internal format */
440 switch (addr->sa_family)
443 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
444 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
445 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
450 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
451 memcpy (&t6.ipv6_addr,
452 &((struct sockaddr_in6 *) addr)->sin6_addr,
453 sizeof (struct in6_addr));
454 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
462 /* modify our published address list */
463 plugin->env->notify_address (plugin->env->cls,
470 * Function called for a quick conversion of the binary address to
471 * a numeric address. Note that the caller must not free the
472 * address and that the next call to this function is allowed
473 * to override the address again.
475 * @param cls closure ('struct Plugin*')
476 * @param addr binary address
477 * @param addrlen length of the address
478 * @return string representing the same address
481 tcp_address_to_string (void *cls,
485 static char rbuf[INET6_ADDRSTRLEN + 12];
486 char buf[INET6_ADDRSTRLEN];
490 const struct IPv4TcpAddress *t4;
491 const struct IPv6TcpAddress *t6;
495 if (addrlen == sizeof (struct IPv6TcpAddress))
499 port = ntohs (t6->t6_port);
500 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
503 else if (addrlen == sizeof (struct IPv4TcpAddress))
507 port = ntohs (t4->t4_port);
508 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
513 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
515 _("Unexpected address length: %u bytes\n"),
516 (unsigned int) addrlen);
520 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
522 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
525 GNUNET_snprintf (rbuf,
527 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
535 * Find the session handle for the given client.
537 * @param plugin the plugin
538 * @param client which client to find the session handle for
539 * @return NULL if no matching session exists
541 static struct Session *
542 find_session_by_client (struct Plugin *plugin,
543 const struct GNUNET_SERVER_Client *client)
547 ret = plugin->sessions;
548 while ((ret != NULL) && (client != ret->client))
555 * Create a new session. Also queues a welcome message.
557 * @param plugin the plugin
558 * @param target peer to connect to
559 * @param client client to use
560 * @param is_nat this a NAT session, we should wait for a client to
561 * connect to us from an address, then assign that to
563 * @return new session object
565 static struct Session *
566 create_session (struct Plugin *plugin,
567 const struct GNUNET_PeerIdentity *target,
568 struct GNUNET_SERVER_Client *client,
572 struct PendingMessage *pm;
573 struct WelcomeMessage welcome;
575 if (is_nat != GNUNET_YES)
576 GNUNET_assert (client != NULL);
578 GNUNET_assert (client == NULL);
580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
582 "Creating new session for peer `%4s'\n",
583 GNUNET_i2s (target));
585 ret = GNUNET_malloc (sizeof (struct Session));
586 ret->last_activity = GNUNET_TIME_absolute_get ();
587 ret->plugin = plugin;
588 ret->is_nat = is_nat;
589 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
591 ret->next = plugin->sessions;
592 plugin->sessions = ret;
594 ret->client = client;
595 ret->target = *target;
596 ret->expecting_welcome = GNUNET_YES;
597 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
598 pm->msg = (const char*) &pm[1];
599 pm->message_size = sizeof (struct WelcomeMessage);
600 welcome.header.size = htons (sizeof (struct WelcomeMessage));
601 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
602 welcome.clientIdentity = *plugin->env->my_identity;
603 memcpy (&pm[1], &welcome, sizeof (welcome));
604 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
605 GNUNET_STATISTICS_update (plugin->env->stats,
606 gettext_noop ("# bytes currently in TCP buffers"),
609 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
610 ret->pending_messages_tail,
612 if (is_nat != GNUNET_YES)
613 GNUNET_STATISTICS_update (plugin->env->stats,
614 gettext_noop ("# TCP sessions active"),
622 * If we have pending messages, ask the server to
623 * transmit them (schedule the respective tasks, etc.)
625 * @param session for which session should we do this
627 static void process_pending_messages (struct Session *session);
631 * Function called to notify a client about the socket
632 * being ready to queue more data. "buf" will be
633 * NULL and "size" zero if the socket was closed for
634 * writing in the meantime.
637 * @param size number of bytes available in buf
638 * @param buf where the callee should write the message
639 * @return number of bytes written to buf
642 do_transmit (void *cls, size_t size, void *buf)
644 struct Session *session = cls;
645 struct GNUNET_PeerIdentity pid;
646 struct Plugin *plugin;
647 struct PendingMessage *pos;
648 struct PendingMessage *hd;
649 struct PendingMessage *tl;
650 struct GNUNET_TIME_Absolute now;
654 GNUNET_assert (session != NULL);
655 session->transmit_handle = NULL;
656 plugin = session->plugin;
660 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
662 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
663 GNUNET_i2s (&session->target));
665 /* timeout; cancel all messages that have already expired */
669 now = GNUNET_TIME_absolute_get ();
670 while ( (NULL != (pos = session->pending_messages_head)) &&
671 (pos->timeout.abs_value <= now.abs_value) )
673 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
674 session->pending_messages_tail,
677 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
679 "Failed to transmit %u byte message to `%4s'.\n",
681 GNUNET_i2s (&session->target));
683 ret += pos->message_size;
684 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
686 /* do this call before callbacks (so that if callbacks destroy
687 session, they have a chance to cancel actions done by this
689 process_pending_messages (session);
690 pid = session->target;
691 /* no do callbacks and do not use session again since
692 the callbacks may abort the session */
693 while (NULL != (pos = hd))
695 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
696 if (pos->transmit_cont != NULL)
697 pos->transmit_cont (pos->transmit_cont_cls,
698 &pid, GNUNET_SYSERR);
701 GNUNET_STATISTICS_update (plugin->env->stats,
702 gettext_noop ("# bytes currently in TCP buffers"),
705 GNUNET_STATISTICS_update (plugin->env->stats,
706 gettext_noop ("# bytes discarded by TCP (timeout)"),
711 /* copy all pending messages that would fit */
716 while (NULL != (pos = session->pending_messages_head))
718 if (ret + pos->message_size > size)
720 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
721 session->pending_messages_tail,
723 GNUNET_assert (size >= pos->message_size);
724 /* FIXME: this memcpy can be up to 7% of our total runtime */
725 memcpy (cbuf, pos->msg, pos->message_size);
726 cbuf += pos->message_size;
727 ret += pos->message_size;
728 size -= pos->message_size;
729 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
731 /* schedule 'continuation' before callbacks so that callbacks that
732 cancel everything don't cause us to use a session that no longer
734 process_pending_messages (session);
735 session->last_activity = GNUNET_TIME_absolute_get ();
736 pid = session->target;
737 /* we'll now call callbacks that may cancel the session; hence
738 we should not use 'session' after this point */
739 while (NULL != (pos = hd))
741 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
742 if (pos->transmit_cont != NULL)
743 pos->transmit_cont (pos->transmit_cont_cls,
747 GNUNET_assert (hd == NULL);
748 GNUNET_assert (tl == NULL);
750 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
752 "Transmitting %u bytes\n",
755 GNUNET_STATISTICS_update (plugin->env->stats,
756 gettext_noop ("# bytes currently in TCP buffers"),
759 GNUNET_STATISTICS_update (plugin->env->stats,
760 gettext_noop ("# bytes transmitted via TCP"),
768 * If we have pending messages, ask the server to
769 * transmit them (schedule the respective tasks, etc.)
771 * @param session for which session should we do this
774 process_pending_messages (struct Session *session)
776 struct PendingMessage *pm;
778 GNUNET_assert (session->client != NULL);
779 if (session->transmit_handle != NULL)
781 if (NULL == (pm = session->pending_messages_head))
784 session->transmit_handle
785 = GNUNET_SERVER_notify_transmit_ready (session->client,
787 GNUNET_TIME_absolute_get_remaining
789 &do_transmit, session);
794 * Functions with this signature are called whenever we need
795 * to close a session due to a disconnect or failure to
796 * establish a connection.
798 * @param session session to close down
801 disconnect_session (struct Session *session)
803 struct Session *prev;
805 struct PendingMessage *pm;
808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
810 "Disconnecting from `%4s' at %s.\n",
811 GNUNET_i2s (&session->target),
812 (session->connect_addr != NULL) ?
813 tcp_address_to_string (session->plugin,
814 session->connect_addr,
815 session->connect_alen) : "*");
817 /* remove from session list */
819 pos = session->plugin->sessions;
820 while (pos != session)
826 session->plugin->sessions = session->next;
828 prev->next = session->next;
831 if (session->transmit_handle != NULL)
833 GNUNET_CONNECTION_notify_transmit_ready_cancel
834 (session->transmit_handle);
835 session->transmit_handle = NULL;
837 while (NULL != (pm = session->pending_messages_head))
840 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
842 pm->transmit_cont != NULL
843 ? "Could not deliver message to `%4s'.\n"
844 : "Could not deliver message to `%4s', notifying.\n",
845 GNUNET_i2s (&session->target));
847 GNUNET_STATISTICS_update (session->plugin->env->stats,
848 gettext_noop ("# bytes currently in TCP buffers"),
849 - (int64_t) pm->message_size,
851 GNUNET_STATISTICS_update (session->plugin->env->stats,
852 gettext_noop ("# bytes discarded by TCP (disconnect)"),
855 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
856 session->pending_messages_tail,
858 if (NULL != pm->transmit_cont)
859 pm->transmit_cont (pm->transmit_cont_cls,
860 &session->target, GNUNET_SYSERR);
863 GNUNET_break (session->client != NULL);
864 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
866 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
867 if (session->client != NULL)
868 GNUNET_SERVER_receive_done (session->client,
871 if (session->client != NULL)
873 GNUNET_SERVER_client_drop (session->client);
874 session->client = NULL;
876 GNUNET_STATISTICS_update (session->plugin->env->stats,
877 gettext_noop ("# TCP sessions active"),
880 GNUNET_free_non_null (session->connect_addr);
881 session->plugin->env->session_end (session->plugin->env->cls,
884 GNUNET_assert (NULL == session->transmit_handle);
885 GNUNET_free (session);
890 * Given two otherwise equivalent sessions, pick the better one.
892 * @param s1 one session (also default)
893 * @param s2 other session
894 * @return "better" session (more active)
896 static struct Session *
897 select_better_session (struct Session *s1,
904 if ( (s1->expecting_welcome == GNUNET_NO) &&
905 (s2->expecting_welcome == GNUNET_YES) )
907 if ( (s1->expecting_welcome == GNUNET_YES) &&
908 (s2->expecting_welcome == GNUNET_NO) )
910 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
912 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
914 if ( (GNUNET_YES == s1->inbound) &&
915 (GNUNET_NO == s2->inbound) )
917 if ( (GNUNET_NO == s1->inbound) &&
918 (GNUNET_YES == s2->inbound) )
926 * Function that can be used by the transport service to transmit
927 * a message using the plugin. Note that in the case of a
928 * peer disconnecting, the continuation MUST be called
929 * prior to the disconnect notification itself. This function
930 * will be called with this peer's HELLO message to initiate
931 * a fresh connection to another peer.
934 * @param target who should receive this message
935 * @param msg the message to transmit
936 * @param msgbuf_size number of bytes in 'msg'
937 * @param priority how important is the message (most plugins will
938 * ignore message priority and just FIFO)
939 * @param timeout how long to wait at most for the transmission (does not
940 * require plugins to discard the message after the timeout,
941 * just advisory for the desired delay; most plugins will ignore
943 * @param session which session must be used (or NULL for "any")
944 * @param addr the address to use (can be NULL if the plugin
945 * is "on its own" (i.e. re-use existing TCP connection))
946 * @param addrlen length of the address in bytes
947 * @param force_address GNUNET_YES if the plugin MUST use the given address,
948 * GNUNET_NO means the plugin may use any other address and
949 * GNUNET_SYSERR means that only reliable existing
950 * bi-directional connections should be used (regardless
952 * @param cont continuation to call once the message has
953 * been transmitted (or if the transport is ready
954 * for the next transmission call; or if the
955 * peer disconnected...); can be NULL
956 * @param cont_cls closure for cont
957 * @return number of bytes used (on the physical network, with overheads);
958 * -1 on hard errors (i.e. address invalid); 0 is a legal value
959 * and does NOT mean that the message was not transmitted (DV and NAT)
962 tcp_plugin_send (void *cls,
963 const struct GNUNET_PeerIdentity *target,
967 struct GNUNET_TIME_Relative timeout,
968 struct Session *session,
972 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
974 struct Plugin *plugin = cls;
975 struct Session *cand_session;
976 struct Session *next;
977 struct PendingMessage *pm;
978 struct GNUNET_CONNECTION_Handle *sa;
982 struct sockaddr_in a4;
983 struct sockaddr_in6 a6;
984 const struct IPv4TcpAddress *t4;
985 const struct IPv6TcpAddress *t6;
986 unsigned int is_natd;
988 GNUNET_STATISTICS_update (plugin->env->stats,
989 gettext_noop ("# bytes TCP was asked to transmit"),
992 /* FIXME: we could do this cheaper with a hash table
993 where we could restrict the iteration to entries that match
994 the target peer... */
999 next = plugin->sessions;
1000 while (NULL != (session = next))
1002 next = session->next;
1003 GNUNET_assert (session->client != NULL);
1004 if (0 != memcmp (target,
1006 sizeof (struct GNUNET_PeerIdentity)))
1008 if ( ( (GNUNET_SYSERR == force_address) &&
1009 (session->expecting_welcome == GNUNET_NO) ) ||
1010 (GNUNET_NO == force_address) )
1012 cand_session = select_better_session (cand_session,
1016 if (GNUNET_SYSERR == force_address)
1018 GNUNET_break (GNUNET_YES == force_address);
1024 if ( (addrlen != session->connect_alen) &&
1025 (session->is_nat == GNUNET_NO) )
1027 if ((0 != memcmp (session->connect_addr,
1029 addrlen)) && (session->is_nat == GNUNET_NO))
1031 cand_session = select_better_session (cand_session,
1034 session = cand_session;
1036 if ( (session == NULL) &&
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1042 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1043 GNUNET_i2s (target));
1045 GNUNET_STATISTICS_update (plugin->env->stats,
1046 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1051 if (session == NULL)
1053 if (addrlen == sizeof (struct IPv6TcpAddress))
1057 memset (&a6, 0, sizeof (a6));
1058 #if HAVE_SOCKADDR_IN_SIN_LEN
1059 a6.sin6_len = sizeof (a6);
1061 a6.sin6_family = AF_INET6;
1062 a6.sin6_port = t6->t6_port;
1063 if (t6->t6_port == 0)
1064 is_natd = GNUNET_YES;
1065 memcpy (&a6.sin6_addr,
1067 sizeof (struct in6_addr));
1071 else if (addrlen == sizeof (struct IPv4TcpAddress))
1075 memset (&a4, 0, sizeof (a4));
1076 #if HAVE_SOCKADDR_IN_SIN_LEN
1077 a4.sin_len = sizeof (a4);
1079 a4.sin_family = AF_INET;
1080 a4.sin_port = t4->t4_port;
1081 if (t4->t4_port == 0)
1082 is_natd = GNUNET_YES;
1083 a4.sin_addr.s_addr = t4->ipv4_addr;
1089 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1091 _("Address of unexpected length: %u\n"),
1097 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1098 return -1; /* NAT client only works with IPv4 addresses */
1099 if (0 == plugin->max_connections)
1100 return -1; /* saturated */
1102 if ( (is_natd == GNUNET_YES) &&
1103 (NULL != plugin->nat) &&
1104 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1105 &target->hashPubKey)) )
1108 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1110 _("Found valid IPv4 NAT address (creating session)!\n"));
1112 session = create_session (plugin,
1117 /* create new message entry */
1118 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1119 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1120 pm->msg = (const char*) &pm[1];
1121 memcpy (&pm[1], msg, msgbuf_size);
1122 /* FIXME: this memcpy can be up to 7% of our total run-time
1123 (for transport service) */
1124 pm->message_size = msgbuf_size;
1125 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1126 pm->transmit_cont = cont;
1127 pm->transmit_cont_cls = cont_cls;
1129 /* append pm to pending_messages list */
1130 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1131 session->pending_messages_tail,
1132 session->pending_messages_tail,
1135 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1136 &target->hashPubKey,
1138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1140 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1142 "Created NAT WAIT connection to `%4s' at `%s'\n",
1143 GNUNET_i2s (target),
1144 GNUNET_a2s (sb, sbs));
1146 GNUNET_NAT_run_client (plugin->nat, &a4);
1149 if ( (is_natd == GNUNET_YES) &&
1150 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1151 &target->hashPubKey)) )
1153 /* Only do one NAT punch attempt per peer identity */
1156 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1160 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1162 "Failed to create connection to `%4s' at `%s'\n",
1163 GNUNET_i2s (target),
1164 GNUNET_a2s (sb, sbs));
1166 GNUNET_STATISTICS_update (plugin->env->stats,
1167 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1172 GNUNET_assert (0 != plugin->max_connections);
1173 plugin->max_connections--;
1175 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1177 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1178 GNUNET_i2s (target),
1179 GNUNET_a2s (sb, sbs));
1181 session = create_session (plugin,
1183 GNUNET_SERVER_connect_socket (plugin->server,
1186 session->connect_addr = GNUNET_malloc (addrlen);
1187 memcpy (session->connect_addr,
1190 session->connect_alen = addrlen;
1192 GNUNET_assert (session != NULL);
1193 GNUNET_assert (session->client != NULL);
1194 GNUNET_STATISTICS_update (plugin->env->stats,
1195 gettext_noop ("# bytes currently in TCP buffers"),
1198 /* create new message entry */
1199 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1200 pm->msg = (const char*) &pm[1];
1201 memcpy (&pm[1], msg, msgbuf_size);
1202 pm->message_size = msgbuf_size;
1203 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1204 pm->transmit_cont = cont;
1205 pm->transmit_cont_cls = cont_cls;
1207 /* append pm to pending_messages list */
1208 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1209 session->pending_messages_tail,
1210 session->pending_messages_tail,
1213 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1215 "Asked to transmit %u bytes to `%s', added message to list.\n",
1217 GNUNET_i2s (target));
1219 process_pending_messages (session);
1225 * Function that can be called to force a disconnect from the
1226 * specified neighbour. This should also cancel all previously
1227 * scheduled transmissions. Obviously the transmission may have been
1228 * partially completed already, which is OK. The plugin is supposed
1229 * to close the connection (if applicable) and no longer call the
1230 * transmit continuation(s).
1232 * Finally, plugin MUST NOT call the services's receive function to
1233 * notify the service that the connection to the specified target was
1234 * closed after a getting this call.
1236 * @param cls closure
1237 * @param target peer for which the last transmission is
1241 tcp_plugin_disconnect (void *cls,
1242 const struct GNUNET_PeerIdentity *target)
1244 struct Plugin *plugin = cls;
1245 struct Session *session;
1246 struct Session *next;
1247 struct PendingMessage *pm;
1250 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1252 "Asked to cancel session with `%4s'\n",
1253 GNUNET_i2s (target));
1255 next = plugin->sessions;
1256 while (NULL != (session = next))
1258 next = session->next;
1259 if (0 != memcmp (target,
1261 sizeof (struct GNUNET_PeerIdentity)))
1263 pm = session->pending_messages_head;
1266 pm->transmit_cont = NULL;
1267 pm->transmit_cont_cls = NULL;
1270 GNUNET_STATISTICS_update (session->plugin->env->stats,
1271 gettext_noop ("# transport-service disconnect requests for TCP"),
1274 disconnect_session (session);
1280 * Context for address to string conversion.
1282 struct PrettyPrinterContext
1285 * Function to call with the result.
1287 GNUNET_TRANSPORT_AddressStringCallback asc;
1290 * Clsoure for 'asc'.
1295 * Port to add after the IP address.
1302 * Append our port and forward the result.
1304 * @param cls the 'struct PrettyPrinterContext*'
1305 * @param hostname hostname part of the address
1308 append_port (void *cls, const char *hostname)
1310 struct PrettyPrinterContext *ppc = cls;
1313 if (hostname == NULL)
1315 ppc->asc (ppc->asc_cls, NULL);
1319 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1320 ppc->asc (ppc->asc_cls, ret);
1326 * Convert the transports address to a nice, human-readable
1329 * @param cls closure
1330 * @param type name of the transport that generated the address
1331 * @param addr one of the addresses of the host, NULL for the last address
1332 * the specific address format depends on the transport
1333 * @param addrlen length of the address
1334 * @param numeric should (IP) addresses be displayed in numeric form?
1335 * @param timeout after how long should we give up?
1336 * @param asc function to call on each string
1337 * @param asc_cls closure for asc
1340 tcp_plugin_address_pretty_printer (void *cls,
1345 struct GNUNET_TIME_Relative timeout,
1346 GNUNET_TRANSPORT_AddressStringCallback asc,
1349 struct PrettyPrinterContext *ppc;
1352 struct sockaddr_in a4;
1353 struct sockaddr_in6 a6;
1354 const struct IPv4TcpAddress *t4;
1355 const struct IPv6TcpAddress *t6;
1358 if (addrlen == sizeof (struct IPv6TcpAddress))
1361 memset (&a6, 0, sizeof (a6));
1362 a6.sin6_family = AF_INET6;
1363 a6.sin6_port = t6->t6_port;
1364 memcpy (&a6.sin6_addr,
1366 sizeof (struct in6_addr));
1367 port = ntohs (t6->t6_port);
1371 else if (addrlen == sizeof (struct IPv4TcpAddress))
1374 memset (&a4, 0, sizeof (a4));
1375 a4.sin_family = AF_INET;
1376 a4.sin_port = t4->t4_port;
1377 a4.sin_addr.s_addr = t4->ipv4_addr;
1378 port = ntohs (t4->t4_port);
1384 /* invalid address */
1385 GNUNET_break_op (0);
1386 asc (asc_cls, NULL);
1389 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1391 ppc->asc_cls = asc_cls;
1393 GNUNET_RESOLVER_hostname_get (sb,
1395 !numeric, timeout, &append_port, ppc);
1400 * Check if the given port is plausible (must be either our listen
1401 * port or our advertised port), or any port if we are behind NAT
1402 * and do not have a port open. If it is neither, we return
1405 * @param plugin global variables
1406 * @param in_port port number to check
1407 * @return GNUNET_OK if port is either open_port or adv_port
1410 check_port (struct Plugin *plugin,
1413 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1415 return GNUNET_SYSERR;
1420 * Function that will be called to check if a binary address for this
1421 * plugin is well-formed and corresponds to an address for THIS peer
1422 * (as per our configuration). Naturally, if absolutely necessary,
1423 * plugins can be a bit conservative in their answer, but in general
1424 * plugins should make sure that the address does not redirect
1425 * traffic to a 3rd party that might try to man-in-the-middle our
1428 * @param cls closure, our 'struct Plugin*'
1429 * @param addr pointer to the address
1430 * @param addrlen length of addr
1431 * @return GNUNET_OK if this is a plausible address for this peer
1432 * and transport, GNUNET_SYSERR if not
1435 tcp_plugin_check_address (void *cls,
1439 struct Plugin *plugin = cls;
1440 struct IPv4TcpAddress *v4;
1441 struct IPv6TcpAddress *v6;
1443 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1444 (addrlen != sizeof (struct IPv6TcpAddress)))
1446 GNUNET_break_op (0);
1447 return GNUNET_SYSERR;
1449 if (addrlen == sizeof (struct IPv4TcpAddress))
1451 v4 = (struct IPv4TcpAddress *) addr;
1453 check_port (plugin, ntohs (v4->t4_port)))
1454 return GNUNET_SYSERR;
1456 GNUNET_NAT_test_address (plugin->nat,
1457 &v4->ipv4_addr, sizeof (struct in_addr)))
1458 return GNUNET_SYSERR;
1462 v6 = (struct IPv6TcpAddress *) addr;
1463 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1465 GNUNET_break_op (0);
1466 return GNUNET_SYSERR;
1469 check_port (plugin, ntohs (v6->t6_port)))
1470 return GNUNET_SYSERR;
1472 GNUNET_NAT_test_address (plugin->nat,
1473 &v6->ipv6_addr, sizeof (struct in6_addr)))
1474 return GNUNET_SYSERR;
1481 * We've received a nat probe from this peer via TCP. Finish
1482 * creating the client session and resume sending of queued
1485 * @param cls closure
1486 * @param client identification of the client
1487 * @param message the actual message
1490 handle_tcp_nat_probe (void *cls,
1491 struct GNUNET_SERVER_Client *client,
1492 const struct GNUNET_MessageHeader *message)
1494 struct Plugin *plugin = cls;
1495 struct Session *session;
1496 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1499 struct IPv4TcpAddress *t4;
1500 struct IPv6TcpAddress *t6;
1501 const struct sockaddr_in *s4;
1502 const struct sockaddr_in6 *s6;
1505 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1507 "received NAT probe\n");
1509 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1510 * a connection to this peer by running gnunet-nat-client. This peer
1511 * received the punch message and now wants us to use the new connection
1512 * as the default for that peer. Do so and then send a WELCOME message
1513 * so we can really be connected!
1515 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1522 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1523 if (0 == memcmp (&tcp_nat_probe->clientIdentity,
1524 plugin->env->my_identity,
1525 sizeof (struct GNUNET_PeerIdentity)))
1527 /* refuse connections from ourselves */
1528 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1532 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1533 &tcp_nat_probe->clientIdentity.hashPubKey);
1534 if (session == NULL)
1537 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1539 "Did NOT find session for NAT probe!\n");
1541 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1545 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1547 "Found session for NAT probe!\n");
1549 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1550 &tcp_nat_probe->clientIdentity.hashPubKey,
1551 session) == GNUNET_YES);
1553 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1556 GNUNET_free (session);
1557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1561 GNUNET_SERVER_client_keep (client);
1562 session->client = client;
1563 session->last_activity = GNUNET_TIME_absolute_get ();
1564 session->inbound = GNUNET_NO;
1567 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1569 "Found address `%s' for incoming connection\n",
1570 GNUNET_a2s (vaddr, alen));
1572 switch (((const struct sockaddr *)vaddr)->sa_family)
1576 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1577 t4->t4_port = s4->sin_port;
1578 t4->ipv4_addr = s4->sin_addr.s_addr;
1579 session->connect_addr = t4;
1580 session->connect_alen = sizeof (struct IPv4TcpAddress);
1584 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1585 t6->t6_port = s6->sin6_port;
1586 memcpy (&t6->ipv6_addr,
1588 sizeof (struct in6_addr));
1589 session->connect_addr = t6;
1590 session->connect_alen = sizeof (struct IPv6TcpAddress);
1593 GNUNET_break_op (0);
1595 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1597 "Bad address for incoming connection!\n");
1599 GNUNET_free (vaddr);
1600 GNUNET_SERVER_client_drop (client);
1601 GNUNET_free (session);
1602 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1605 GNUNET_free (vaddr);
1607 session->next = plugin->sessions;
1608 plugin->sessions = session;
1609 GNUNET_STATISTICS_update (plugin->env->stats,
1610 gettext_noop ("# TCP sessions active"),
1613 process_pending_messages (session);
1614 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1619 * We've received a welcome from this peer via TCP. Possibly create a
1620 * fresh client record and send back our welcome.
1622 * @param cls closure
1623 * @param client identification of the client
1624 * @param message the actual message
1627 handle_tcp_welcome (void *cls,
1628 struct GNUNET_SERVER_Client *client,
1629 const struct GNUNET_MessageHeader *message)
1631 struct Plugin *plugin = cls;
1632 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1633 struct Session *session;
1636 struct IPv4TcpAddress *t4;
1637 struct IPv6TcpAddress *t6;
1638 const struct sockaddr_in *s4;
1639 const struct sockaddr_in6 *s6;
1641 if (0 == memcmp (&wm->clientIdentity,
1642 plugin->env->my_identity,
1643 sizeof (struct GNUNET_PeerIdentity)))
1645 /* refuse connections from ourselves */
1646 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1650 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1652 "Received %s message from `%4s'.\n",
1654 GNUNET_i2s (&wm->clientIdentity));
1656 GNUNET_STATISTICS_update (plugin->env->stats,
1657 gettext_noop ("# TCP WELCOME messages received"),
1660 session = find_session_by_client (plugin, client);
1662 if (session == NULL)
1665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1667 "Received %s message from a `%4s', creating new session\n",
1669 GNUNET_i2s (&wm->clientIdentity));
1671 GNUNET_SERVER_client_keep (client);
1672 session = create_session (plugin,
1673 &wm->clientIdentity,
1676 session->inbound = GNUNET_YES;
1678 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1681 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1683 "Found address `%s' for incoming connection\n",
1684 GNUNET_a2s (vaddr, alen));
1686 if (alen == sizeof (struct sockaddr_in))
1689 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1690 t4->t4_port = s4->sin_port;
1691 t4->ipv4_addr = s4->sin_addr.s_addr;
1692 session->connect_addr = t4;
1693 session->connect_alen = sizeof (struct IPv4TcpAddress);
1695 else if (alen == sizeof (struct sockaddr_in6))
1698 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1699 t6->t6_port = s6->sin6_port;
1700 memcpy (&t6->ipv6_addr,
1702 sizeof (struct in6_addr));
1703 session->connect_addr = t6;
1704 session->connect_alen = sizeof (struct IPv6TcpAddress);
1707 GNUNET_free (vaddr);
1712 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1714 "Did not obtain TCP socket address for incoming connection\n");
1717 process_pending_messages (session);
1723 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1725 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1727 "Found address `%s' (already have session)\n",
1728 GNUNET_a2s (vaddr, alen));
1729 GNUNET_free (vaddr);
1734 if (session->expecting_welcome != GNUNET_YES)
1736 GNUNET_break_op (0);
1737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1740 session->last_activity = GNUNET_TIME_absolute_get ();
1741 session->expecting_welcome = GNUNET_NO;
1742 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1747 * Task to signal the server that we can continue
1748 * receiving from the TCP client now.
1750 * @param cls the 'struct Session*'
1751 * @param tc task context (unused)
1754 delayed_done (void *cls,
1755 const struct GNUNET_SCHEDULER_TaskContext *tc)
1757 struct Session *session = cls;
1758 struct GNUNET_TIME_Relative delay;
1760 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1761 delay = session->plugin->env->receive (session->plugin->env->cls,
1767 if (delay.rel_value == 0)
1768 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1770 session->receive_delay_task =
1771 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
1776 * We've received data for this peer via TCP. Unbox,
1777 * compute latency and forward.
1779 * @param cls closure
1780 * @param client identification of the client
1781 * @param message the actual message
1784 handle_tcp_data (void *cls,
1785 struct GNUNET_SERVER_Client *client,
1786 const struct GNUNET_MessageHeader *message)
1788 struct Plugin *plugin = cls;
1789 struct Session *session;
1790 struct GNUNET_TIME_Relative delay;
1793 type = ntohs (message->type);
1794 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
1795 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
1797 /* We don't want to propagate WELCOME and NAT Probe messages up! */
1798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1801 session = find_session_by_client (plugin, client);
1802 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
1804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1807 session->last_activity = GNUNET_TIME_absolute_get ();
1809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1811 "Passing %u bytes of type %u from `%4s' to transport service.\n",
1812 (unsigned int) ntohs (message->size),
1813 (unsigned int) ntohs (message->type),
1814 GNUNET_i2s (&session->target));
1816 GNUNET_STATISTICS_update (plugin->env->stats,
1817 gettext_noop ("# bytes received via TCP"),
1818 ntohs (message->size),
1820 struct GNUNET_TRANSPORT_ATS_Information distance[2];
1821 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
1822 distance[0].value = htonl (1);
1823 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
1824 distance[1].value = htonl (0);
1825 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
1826 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
1829 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
1830 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
1831 if (delay.rel_value == 0)
1833 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1840 "Throttling receiving from `%s' for %llu ms\n",
1841 GNUNET_i2s (&session->target),
1842 (unsigned long long) delay.rel_value);
1844 GNUNET_SERVER_disable_receive_done_warning (client);
1845 session->receive_delay_task =
1846 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
1852 * Functions with this signature are called whenever a peer
1853 * is disconnected on the network level.
1855 * @param cls closure
1856 * @param client identification of the client
1859 disconnect_notify (void *cls,
1860 struct GNUNET_SERVER_Client *client)
1862 struct Plugin *plugin = cls;
1863 struct Session *session;
1867 plugin->max_connections++;
1868 session = find_session_by_client (plugin, client);
1869 if (session == NULL)
1870 return; /* unknown, nothing to do */
1872 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1874 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
1875 GNUNET_i2s (&session->target),
1876 (session->connect_addr != NULL) ?
1877 tcp_address_to_string (session->plugin,
1878 session->connect_addr,
1879 session->connect_alen) : "*");
1881 GNUNET_STATISTICS_update (session->plugin->env->stats,
1882 gettext_noop ("# network-level TCP disconnect events"),
1885 disconnect_session (session);
1890 * We can now send a probe message, copy into buffer to really send.
1892 * @param cls closure, a struct TCPProbeContext
1893 * @param size max size to copy
1894 * @param buf buffer to copy message to
1895 * @return number of bytes copied into buf
1898 notify_send_probe (void *cls,
1902 struct TCPProbeContext *tcp_probe_ctx = cls;
1903 struct Plugin *plugin = tcp_probe_ctx->plugin;
1906 tcp_probe_ctx->transmit_handle = NULL;
1907 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
1912 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
1913 GNUNET_free(tcp_probe_ctx);
1916 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
1917 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
1918 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
1919 tcp_probe_ctx->sock);
1920 ret = sizeof(tcp_probe_ctx->message);
1921 GNUNET_free(tcp_probe_ctx);
1927 * Function called by the NAT subsystem suggesting another peer wants
1928 * to connect to us via connection reversal. Try to connect back to the
1931 * @param cls closure
1932 * @param addr address to try
1933 * @param addrlen number of bytes in addr
1936 try_connection_reversal (void *cls,
1937 const struct sockaddr *addr,
1940 struct Plugin *plugin = cls;
1941 struct GNUNET_CONNECTION_Handle *sock;
1942 struct TCPProbeContext *tcp_probe_ctx;
1945 * We have received an ICMP response, ostensibly from a peer
1946 * that wants to connect to us! Send a message to establish a connection.
1948 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
1953 /* failed for some odd reason (out of sockets?); ignore attempt */
1957 /* FIXME: do we need to track these probe context objects so that
1958 we can clean them up on plugin unload? */
1960 = GNUNET_malloc(sizeof(struct TCPProbeContext));
1961 tcp_probe_ctx->message.header.size
1962 = htons(sizeof(struct TCP_NAT_ProbeMessage));
1963 tcp_probe_ctx->message.header.type
1964 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
1965 memcpy (&tcp_probe_ctx->message.clientIdentity,
1966 plugin->env->my_identity,
1967 sizeof(struct GNUNET_PeerIdentity));
1968 tcp_probe_ctx->plugin = plugin;
1969 tcp_probe_ctx->sock = sock;
1970 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
1973 tcp_probe_ctx->transmit_handle
1974 = GNUNET_CONNECTION_notify_transmit_ready (sock,
1975 ntohs (tcp_probe_ctx->message.header.size),
1976 GNUNET_TIME_UNIT_FOREVER_REL,
1977 ¬ify_send_probe, tcp_probe_ctx);
1983 * Entry point for the plugin.
1985 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
1986 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
1989 libgnunet_plugin_transport_tcp_init (void *cls)
1991 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1992 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1993 sizeof (struct WelcomeMessage)},
1994 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
1995 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
1998 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1999 struct GNUNET_TRANSPORT_PluginFunctions *api;
2000 struct Plugin *plugin;
2001 struct GNUNET_SERVICE_Context *service;
2002 unsigned long long aport;
2003 unsigned long long bport;
2004 unsigned long long max_connections;
2006 struct GNUNET_TIME_Relative idle_timeout;
2008 struct sockaddr **addrs;
2009 socklen_t *addrlens;
2012 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2016 max_connections = 128;
2020 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2026 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2032 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2034 _("Require valid port number for service `%s' in configuration!\n"),
2044 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2045 if (service == NULL)
2047 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2049 _("Failed to start service.\n"));
2058 plugin = GNUNET_malloc (sizeof (struct Plugin));
2059 plugin->max_connections = max_connections;
2060 plugin->open_port = bport;
2061 plugin->adv_port = aport;
2063 plugin->lsock = NULL;
2064 if ( (service != NULL) &&
2066 (ret = GNUNET_SERVICE_get_server_addresses ("transport-tcp",
2071 plugin->nat = GNUNET_NAT_register (env->cfg,
2075 (const struct sockaddr **) addrs,
2077 &tcp_nat_port_map_callback,
2078 &try_connection_reversal,
2083 GNUNET_assert (addrs[ret] != NULL);
2084 GNUNET_free (addrs[ret]);
2086 GNUNET_free_non_null (addrs);
2087 GNUNET_free_non_null (addrlens);
2091 plugin->nat = GNUNET_NAT_register (env->cfg,
2096 &try_connection_reversal,
2099 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2101 api->send = &tcp_plugin_send;
2102 api->disconnect = &tcp_plugin_disconnect;
2103 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2104 api->check_address = &tcp_plugin_check_address;
2105 api->address_to_string = &tcp_address_to_string;
2106 plugin->service = service;
2107 if (service != NULL)
2109 plugin->server = GNUNET_SERVICE_get_server (service);
2114 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2119 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2121 _("Failed to find option %s in section %s!\n"),
2124 if (plugin->nat != NULL)
2125 GNUNET_NAT_unregister (plugin->nat);
2126 GNUNET_free (plugin);
2130 plugin->server = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin, NULL,
2131 idle_timeout, GNUNET_YES);
2133 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2134 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2136 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2138 plugin->handlers[i].callback_cls = plugin;
2139 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2140 GNUNET_SERVER_disconnect_notify (plugin->server,
2143 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
2145 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2147 _("TCP transport listening on port %llu\n"),
2150 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2152 _("TCP transport not listening on any port (client only)\n"));
2154 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2156 _("TCP transport advertises itself as being on port %llu\n"),
2163 * Exit point from the plugin.
2166 libgnunet_plugin_transport_tcp_done (void *cls)
2168 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2169 struct Plugin *plugin = api->cls;
2170 struct Session *session;
2171 struct TCPProbeContext *tcp_probe;
2173 while (NULL != (session = plugin->sessions))
2174 disconnect_session (session);
2175 if (plugin->service != NULL)
2176 GNUNET_SERVICE_stop (plugin->service);
2178 GNUNET_SERVER_destroy (plugin->server);
2179 GNUNET_free (plugin->handlers);
2180 if (plugin->nat != NULL)
2181 GNUNET_NAT_unregister (plugin->nat);
2182 while (NULL != (tcp_probe = plugin->probe_head))
2184 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2187 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
2188 GNUNET_free (tcp_probe);
2190 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2191 GNUNET_free (plugin);
2196 /* end of plugin_transport_tcp.c */