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_YES
46 * How long until we give up on transmitting the welcome message?
48 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52 * Initial handshake message for a session.
57 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
59 struct GNUNET_MessageHeader header;
62 * Identity of the node connecting (TCP client)
64 struct GNUNET_PeerIdentity clientIdentity;
70 * Basically a WELCOME message, but with the purpose
71 * of giving the waiting peer a client handle to use
73 struct TCP_NAT_ProbeMessage
76 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
78 struct GNUNET_MessageHeader header;
81 * Identity of the sender of the message.
83 struct GNUNET_PeerIdentity clientIdentity;
89 * Context for sending a NAT probe via TCP.
91 struct TCPProbeContext
95 * Active probes are kept in a DLL.
97 struct TCPProbeContext *next;
100 * Active probes are kept in a DLL.
102 struct TCPProbeContext *prev;
107 struct GNUNET_CONNECTION_Handle *sock;
110 * Message to be sent.
112 struct TCP_NAT_ProbeMessage message;
115 * Handle to the transmission.
117 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
120 * Transport plugin handle.
122 struct Plugin *plugin;
127 * Network format for IPv4 addresses.
129 struct IPv4TcpAddress
132 * IPv4 address, in network byte order.
134 uint32_t ipv4_addr GNUNET_PACKED;
137 * Port number, in network byte order.
139 uint16_t t_port GNUNET_PACKED;
145 * Network format for IPv6 addresses.
147 struct IPv6TcpAddress
152 struct in6_addr ipv6_addr GNUNET_PACKED;
155 * Port number, in network byte order.
157 uint16_t t6_port GNUNET_PACKED;
163 * Encapsulation of all of the state of the plugin.
169 * Local network addresses (actual IP address follows this struct).
170 * PORT is NOT included!
176 * This is a doubly linked list.
178 struct LocalAddrList *next;
181 * This is a doubly linked list.
183 struct LocalAddrList *prev;
188 struct Plugin *plugin;
191 * Handle to NAT holes we've tried to punch for this address.
193 struct GNUNET_NAT_Handle *nat;
196 * Pointer to a 'struct IPv4/V6TcpAddress' describing our external IP and port
197 * as obtained from the NAT by automatic port mapping.
199 void *external_nat_address;
202 * Number of bytes in 'external_nat_address'
207 * Number of bytes of the address that follow
215 * Information kept for each message that is yet to
218 struct PendingMessage
222 * This is a doubly-linked list.
224 struct PendingMessage *next;
227 * This is a doubly-linked list.
229 struct PendingMessage *prev;
232 * The pending message
237 * Continuation function to call once the message
238 * has been sent. Can be NULL if there is no
239 * continuation to call.
241 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
244 * Closure for transmit_cont.
246 void *transmit_cont_cls;
249 * Timeout value for the pending message.
251 struct GNUNET_TIME_Absolute timeout;
254 * So that the gnunet-service-transport can group messages together,
255 * these pending messages need to accept a message buffer and size
256 * instead of just a GNUNET_MessageHeader.
264 * Session handle for TCP connections.
272 struct SessionHeader header;
275 * Stored in a linked list.
277 struct Session *next;
280 * Pointer to the global plugin struct.
282 struct Plugin *plugin;
285 * The client (used to identify this connection)
287 struct GNUNET_SERVER_Client *client;
290 * Messages currently pending for transmission
291 * to this peer, if any.
293 struct PendingMessage *pending_messages_head;
296 * Messages currently pending for transmission
297 * to this peer, if any.
299 struct PendingMessage *pending_messages_tail;
302 * Handle for pending transmission request.
304 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
307 * To whom are we talking to (set to our identity
308 * if we are still waiting for the welcome message)
310 struct GNUNET_PeerIdentity target;
313 * ID of task used to delay receiving more to throttle sender.
315 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
318 * Address of the other peer (either based on our 'connect'
319 * call or on our 'accept' call).
324 * Last activity on this connection. Used to select preferred
327 struct GNUNET_TIME_Absolute last_activity;
330 * Length of connect_addr.
335 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
337 int expecting_welcome;
340 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
345 * Was this session created using NAT traversal?
353 * Encapsulation of all of the state of the plugin.
360 struct GNUNET_TRANSPORT_PluginEnvironment *env;
365 struct GNUNET_CONNECTION_Handle *lsock;
368 * stdout pipe handle for the gnunet-nat-server process
370 struct GNUNET_DISK_PipeHandle *server_stdout;
373 * stdout file handle (for reading) for the gnunet-nat-server process
375 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
378 * ID of select gnunet-nat-server stdout read task
380 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
383 * The process id of the server process (if behind NAT)
385 struct GNUNET_OS_Process *server_proc;
388 * List of open TCP sessions.
390 struct Session *sessions;
393 * Handle to the network service.
395 struct GNUNET_SERVICE_Context *service;
398 * Handle to the server for this service.
400 struct GNUNET_SERVER_Handle *server;
403 * Copy of the handler array where the closures are
404 * set to this struct's instance.
406 struct GNUNET_SERVER_MessageHandler *handlers;
409 * Handle for request of hostname resolution, non-NULL if pending.
411 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
414 * Map of peers we have tried to contact behind a NAT
416 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
419 * The external address given to us by the user. Used for HELLOs
420 * and address validation.
422 char *external_address;
425 * The internal address given to us by the user (or discovered).
426 * Used for NAT traversal (ICMP method), but not as a 'validateable'
429 char *internal_address;
432 * Address given for us to bind to (ONLY).
437 * use local addresses?
439 int use_localaddresses;
442 * List of our IP addresses.
444 struct LocalAddrList *lal_head;
447 * Tail of our IP address list.
449 struct LocalAddrList *lal_tail;
452 * List of active TCP probes.
454 struct TCPProbeContext *probe_head;
457 * List of active TCP probes.
459 struct TCPProbeContext *probe_tail;
462 * Handle for (DYN)DNS lookup of our external IP.
464 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
467 * ID of task used to update our addresses when one expires.
469 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
472 * Port that we are actually listening on.
477 * Port that the user said we would have visible to the
483 * Is this transport configured to be behind a NAT?
488 * Has the NAT been punched?
493 * Is this transport configured to allow connections to NAT'd peers?
495 int enable_nat_client;
498 * Should we run the gnunet-nat-server?
500 int enable_nat_server;
503 * Are we allowed to try UPnP/PMP for NAT traversal?
511 * Our external IP address/port mapping has changed.
513 * @param cls closure, the 'struct LocalAddrList'
514 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
515 * the previous (now invalid) one
516 * @param addr either the previous or the new public IP address
517 * @param addrlen actual lenght of the address
520 nat_port_map_callback (void *cls,
522 const struct sockaddr *addr,
525 struct LocalAddrList *lal = cls;
526 struct Plugin *plugin = lal->plugin;
528 struct IPv4TcpAddress t4;
529 struct IPv6TcpAddress t6;
533 /* convert 'addr' to our internal format */
534 af = addr->sa_family;
538 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
539 t4.t_port = ((struct sockaddr_in *) addr)->sin_port;
544 memcpy (&t6.ipv6_addr,
545 &((struct sockaddr_in6 *) addr)->sin6_addr,
546 sizeof (struct in6_addr));
547 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
556 /* modify our published address list */
557 if (GNUNET_YES == add_remove)
559 plugin->env->notify_address (plugin->env->cls,
561 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
562 GNUNET_free_non_null (lal->external_nat_address);
563 lal->external_nat_address = GNUNET_memdup (arg, args);
564 lal->ena_size = args;
568 plugin->env->notify_address (plugin->env->cls,
570 arg, args, GNUNET_TIME_UNIT_ZERO);
571 GNUNET_free_non_null (lal->external_nat_address);
578 * Add the given address to the list of 'local' addresses, thereby
579 * making it a 'legal' address for this peer to have.
581 * @param plugin the plugin
582 * @param arg the address, either an IPv4 or an IPv6 IP address
583 * @param arg_size number of bytes in arg
586 add_to_address_list (struct Plugin *plugin,
590 struct LocalAddrList *lal;
591 struct sockaddr_in v4;
592 struct sockaddr_in6 v6;
593 const struct sockaddr *sa;
596 lal = plugin->lal_head;
599 if ( (lal->size == arg_size) &&
600 (0 == memcmp (&lal[1], arg, arg_size)) )
604 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
605 lal->plugin = plugin;
606 lal->size = arg_size;
607 memcpy (&lal[1], arg, arg_size);
608 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
611 if (plugin->open_port == 0)
612 return; /* we're not listening at all... */
613 if (arg_size == sizeof (struct in_addr))
615 memset (&v4, 0, sizeof (v4));
616 v4.sin_family = AF_INET;
617 v4.sin_port = htons (plugin->open_port);
618 memcpy (&v4.sin_addr, arg, arg_size);
619 #if HAVE_SOCKADDR_IN_SIN_LEN
620 v4.sin_len = sizeof (struct sockaddr_in);
622 sa = (const struct sockaddr*) &v4;
625 else if (arg_size == sizeof (struct in6_addr))
627 memset (&v6, 0, sizeof (v6));
628 v6.sin6_family = AF_INET6;
629 v6.sin6_port = htons (plugin->open_port);
630 memcpy (&v6.sin6_addr, arg, arg_size);
631 #if HAVE_SOCKADDR_IN_SIN_LEN
632 v6.sin6_len = sizeof (struct sockaddr_in6);
634 sa = (const struct sockaddr*) &v6;
642 if ( (plugin->behind_nat == GNUNET_YES) &&
643 (plugin->enable_upnp == GNUNET_YES) )
644 lal->nat = GNUNET_NAT_register (plugin->env->cfg,
646 &nat_port_map_callback,
652 * Check if the given address is in the list of 'local' addresses.
654 * @param plugin the plugin
655 * @param arg the address, either an IPv4 or an IPv6 IP address
656 * @param arg_size number of bytes in arg
657 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
660 check_local_addr (struct Plugin *plugin,
664 struct LocalAddrList *lal;
666 lal = plugin->lal_head;
669 if ( (lal->size == arg_size) &&
670 (0 == memcmp (&lal[1], arg, arg_size)) )
674 return GNUNET_SYSERR;
679 * Check if the given address is in the list of 'mapped' addresses.
681 * @param plugin the plugin
682 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
683 * @param arg_size number of bytes in arg
684 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
687 check_mapped_addr (struct Plugin *plugin,
691 struct LocalAddrList *lal;
693 lal = plugin->lal_head;
696 if ( (lal->ena_size == arg_size) &&
697 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
701 return GNUNET_SYSERR;
706 * Function called for a quick conversion of the binary address to
707 * a numeric address. Note that the caller must not free the
708 * address and that the next call to this function is allowed
709 * to override the address again.
711 * @param cls closure ('struct Plugin*')
712 * @param addr binary address
713 * @param addrlen length of the address
714 * @return string representing the same address
717 tcp_address_to_string (void *cls,
721 static char rbuf[INET6_ADDRSTRLEN + 12];
722 char buf[INET6_ADDRSTRLEN];
726 const struct IPv4TcpAddress *t4;
727 const struct IPv6TcpAddress *t6;
731 if (addrlen == sizeof (struct IPv6TcpAddress))
735 port = ntohs (t6->t6_port);
736 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
739 else if (addrlen == sizeof (struct IPv4TcpAddress))
743 port = ntohs (t4->t_port);
744 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
749 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
751 _("Unexpected address length: %u bytes\n"),
752 (unsigned int) addrlen);
756 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
758 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
761 GNUNET_snprintf (rbuf,
763 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
771 * Find the session handle for the given client.
773 * @param plugin the plugin
774 * @param client which client to find the session handle for
775 * @return NULL if no matching session exists
777 static struct Session *
778 find_session_by_client (struct Plugin *plugin,
779 const struct GNUNET_SERVER_Client *client)
783 ret = plugin->sessions;
784 while ((ret != NULL) && (client != ret->client))
791 * Create a new session. Also queues a welcome message.
793 * @param plugin the plugin
794 * @param target peer to connect to
795 * @param client client to use
796 * @param is_nat this a NAT session, we should wait for a client to
797 * connect to us from an address, then assign that to
799 * @return new session object
801 static struct Session *
802 create_session (struct Plugin *plugin,
803 const struct GNUNET_PeerIdentity *target,
804 struct GNUNET_SERVER_Client *client,
808 struct PendingMessage *pm;
809 struct WelcomeMessage welcome;
811 if (is_nat != GNUNET_YES)
812 GNUNET_assert (client != NULL);
814 GNUNET_assert (client == NULL);
816 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
818 "Creating new session for peer `%4s'\n",
819 GNUNET_i2s (target));
821 ret = GNUNET_malloc (sizeof (struct Session));
822 ret->last_activity = GNUNET_TIME_absolute_get ();
823 ret->plugin = plugin;
824 ret->is_nat = is_nat;
825 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
827 ret->next = plugin->sessions;
828 plugin->sessions = ret;
830 ret->client = client;
831 ret->target = *target;
832 ret->expecting_welcome = GNUNET_YES;
833 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
834 pm->msg = (const char*) &pm[1];
835 pm->message_size = sizeof (struct WelcomeMessage);
836 welcome.header.size = htons (sizeof (struct WelcomeMessage));
837 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
838 welcome.clientIdentity = *plugin->env->my_identity;
839 memcpy (&pm[1], &welcome, sizeof (welcome));
840 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
841 GNUNET_STATISTICS_update (plugin->env->stats,
842 gettext_noop ("# bytes currently in TCP buffers"),
845 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
846 ret->pending_messages_tail,
848 if (is_nat != GNUNET_YES)
849 GNUNET_STATISTICS_update (plugin->env->stats,
850 gettext_noop ("# TCP sessions active"),
858 * If we have pending messages, ask the server to
859 * transmit them (schedule the respective tasks, etc.)
861 * @param session for which session should we do this
863 static void process_pending_messages (struct Session *session);
867 * Function called to notify a client about the socket
868 * being ready to queue more data. "buf" will be
869 * NULL and "size" zero if the socket was closed for
870 * writing in the meantime.
873 * @param size number of bytes available in buf
874 * @param buf where the callee should write the message
875 * @return number of bytes written to buf
878 do_transmit (void *cls, size_t size, void *buf)
880 struct Session *session = cls;
881 struct GNUNET_PeerIdentity pid;
882 struct Plugin *plugin;
883 struct PendingMessage *pos;
884 struct PendingMessage *hd;
885 struct PendingMessage *tl;
886 struct GNUNET_TIME_Absolute now;
890 session->transmit_handle = NULL;
891 plugin = session->plugin;
895 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
897 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
898 GNUNET_i2s (&session->target));
900 /* timeout; cancel all messages that have already expired */
904 now = GNUNET_TIME_absolute_get ();
905 while ( (NULL != (pos = session->pending_messages_head)) &&
906 (pos->timeout.abs_value <= now.abs_value) )
908 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
909 session->pending_messages_tail,
912 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
914 "Failed to transmit %u byte message to `%4s'.\n",
916 GNUNET_i2s (&session->target));
918 ret += pos->message_size;
919 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
921 /* do this call before callbacks (so that if callbacks destroy
922 session, they have a chance to cancel actions done by this
924 process_pending_messages (session);
925 pid = session->target;
926 /* no do callbacks and do not use session again since
927 the callbacks may abort the session */
928 while (NULL != (pos = hd))
930 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
931 if (pos->transmit_cont != NULL)
932 pos->transmit_cont (pos->transmit_cont_cls,
933 &pid, GNUNET_SYSERR);
936 GNUNET_STATISTICS_update (plugin->env->stats,
937 gettext_noop ("# bytes currently in TCP buffers"),
940 GNUNET_STATISTICS_update (plugin->env->stats,
941 gettext_noop ("# bytes discarded by TCP (timeout)"),
946 /* copy all pending messages that would fit */
951 while (NULL != (pos = session->pending_messages_head))
953 if (ret + pos->message_size > size)
955 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
956 session->pending_messages_tail,
958 GNUNET_assert (size >= pos->message_size);
959 /* FIXME: this memcpy can be up to 7% of our total runtime */
960 memcpy (cbuf, pos->msg, pos->message_size);
961 cbuf += pos->message_size;
962 ret += pos->message_size;
963 size -= pos->message_size;
964 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
966 /* schedule 'continuation' before callbacks so that callbacks that
967 cancel everything don't cause us to use a session that no longer
969 process_pending_messages (session);
970 session->last_activity = GNUNET_TIME_absolute_get ();
971 pid = session->target;
972 /* we'll now call callbacks that may cancel the session; hence
973 we should not use 'session' after this point */
974 while (NULL != (pos = hd))
976 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
977 if (pos->transmit_cont != NULL)
978 pos->transmit_cont (pos->transmit_cont_cls,
982 GNUNET_assert (hd == NULL);
983 GNUNET_assert (tl == NULL);
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
987 "Transmitting %u bytes\n",
990 GNUNET_STATISTICS_update (plugin->env->stats,
991 gettext_noop ("# bytes currently in TCP buffers"),
994 GNUNET_STATISTICS_update (plugin->env->stats,
995 gettext_noop ("# bytes transmitted via TCP"),
1003 * If we have pending messages, ask the server to
1004 * transmit them (schedule the respective tasks, etc.)
1006 * @param session for which session should we do this
1009 process_pending_messages (struct Session *session)
1011 struct PendingMessage *pm;
1013 GNUNET_assert (session->client != NULL);
1014 if (session->transmit_handle != NULL)
1016 if (NULL == (pm = session->pending_messages_head))
1019 session->transmit_handle
1020 = GNUNET_SERVER_notify_transmit_ready (session->client,
1022 GNUNET_TIME_absolute_get_remaining
1024 &do_transmit, session);
1029 * Functions with this signature are called whenever we need
1030 * to close a session due to a disconnect or failure to
1031 * establish a connection.
1033 * @param session session to close down
1036 disconnect_session (struct Session *session)
1038 struct Session *prev;
1039 struct Session *pos;
1040 struct PendingMessage *pm;
1043 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1045 "Disconnecting from `%4s' at %s.\n",
1046 GNUNET_i2s (&session->target),
1047 (session->connect_addr != NULL) ?
1048 tcp_address_to_string (session->plugin,
1049 session->connect_addr,
1050 session->connect_alen) : "*");
1052 /* remove from session list */
1054 pos = session->plugin->sessions;
1055 while (pos != session)
1061 session->plugin->sessions = session->next;
1063 prev->next = session->next;
1065 /* clean up state */
1066 if (session->transmit_handle != NULL)
1068 GNUNET_CONNECTION_notify_transmit_ready_cancel
1069 (session->transmit_handle);
1070 session->transmit_handle = NULL;
1072 while (NULL != (pm = session->pending_messages_head))
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1077 pm->transmit_cont != NULL
1078 ? "Could not deliver message to `%4s'.\n"
1079 : "Could not deliver message to `%4s', notifying.\n",
1080 GNUNET_i2s (&session->target));
1082 GNUNET_STATISTICS_update (session->plugin->env->stats,
1083 gettext_noop ("# bytes currently in TCP buffers"),
1084 - (int64_t) pm->message_size,
1086 GNUNET_STATISTICS_update (session->plugin->env->stats,
1087 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1090 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1091 session->pending_messages_tail,
1093 if (NULL != pm->transmit_cont)
1094 pm->transmit_cont (pm->transmit_cont_cls,
1095 &session->target, GNUNET_SYSERR);
1098 GNUNET_break (session->client != NULL);
1099 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1101 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1102 if (session->client != NULL)
1103 GNUNET_SERVER_receive_done (session->client,
1106 else if (session->client != NULL)
1107 GNUNET_SERVER_client_drop (session->client);
1108 GNUNET_STATISTICS_update (session->plugin->env->stats,
1109 gettext_noop ("# TCP sessions active"),
1112 GNUNET_free_non_null (session->connect_addr);
1114 session->plugin->env->session_end (session->plugin->env->cls,
1118 GNUNET_free (session);
1123 * Given two otherwise equivalent sessions, pick the better one.
1125 * @param s1 one session (also default)
1126 * @param s2 other session
1127 * @return "better" session (more active)
1129 static struct Session *
1130 select_better_session (struct Session *s1,
1137 if ( (s1->expecting_welcome == GNUNET_NO) &&
1138 (s2->expecting_welcome == GNUNET_YES) )
1140 if ( (s1->expecting_welcome == GNUNET_YES) &&
1141 (s2->expecting_welcome == GNUNET_NO) )
1143 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1145 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1147 if ( (GNUNET_YES == s1->inbound) &&
1148 (GNUNET_NO == s2->inbound) )
1150 if ( (GNUNET_NO == s1->inbound) &&
1151 (GNUNET_YES == s2->inbound) )
1158 * We learned about a peer (possibly behind NAT) so run the
1159 * gnunet-nat-client to send dummy ICMP responses.
1161 * @param plugin the plugin for this transport
1162 * @param sa the address of the peer (IPv4-only)
1165 run_gnunet_nat_client (struct Plugin *plugin,
1166 const struct sockaddr_in *sa)
1168 char inet4[INET_ADDRSTRLEN];
1169 char port_as_string[6];
1170 struct GNUNET_OS_Process *proc;
1172 if (plugin->internal_address == NULL)
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1176 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1179 GNUNET_assert (sa->sin_family == AF_INET);
1180 if (NULL == inet_ntop (AF_INET,
1182 inet4, INET_ADDRSTRLEN))
1184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1187 GNUNET_snprintf(port_as_string,
1188 sizeof (port_as_string),
1192 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1194 _("Running gnunet-nat-client %s %s %u\n"),
1195 plugin->internal_address,
1197 (unsigned int) plugin->adv_port);
1199 proc = GNUNET_OS_start_process (NULL,
1201 "gnunet-nat-client",
1202 "gnunet-nat-client",
1203 plugin->internal_address,
1209 /* we know that the gnunet-nat-client will terminate virtually
1211 GNUNET_OS_process_wait (proc);
1212 GNUNET_OS_process_close (proc);
1217 * Function that can be used by the transport service to transmit
1218 * a message using the plugin. Note that in the case of a
1219 * peer disconnecting, the continuation MUST be called
1220 * prior to the disconnect notification itself. This function
1221 * will be called with this peer's HELLO message to initiate
1222 * a fresh connection to another peer.
1224 * @param cls closure
1225 * @param target who should receive this message
1226 * @param msg the message to transmit
1227 * @param msgbuf_size number of bytes in 'msg'
1228 * @param priority how important is the message (most plugins will
1229 * ignore message priority and just FIFO)
1230 * @param timeout how long to wait at most for the transmission (does not
1231 * require plugins to discard the message after the timeout,
1232 * just advisory for the desired delay; most plugins will ignore
1234 * @param session which session must be used (or NULL for "any")
1235 * @param addr the address to use (can be NULL if the plugin
1236 * is "on its own" (i.e. re-use existing TCP connection))
1237 * @param addrlen length of the address in bytes
1238 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1239 * GNUNET_NO means the plugin may use any other address and
1240 * GNUNET_SYSERR means that only reliable existing
1241 * bi-directional connections should be used (regardless
1243 * @param cont continuation to call once the message has
1244 * been transmitted (or if the transport is ready
1245 * for the next transmission call; or if the
1246 * peer disconnected...); can be NULL
1247 * @param cont_cls closure for cont
1248 * @return number of bytes used (on the physical network, with overheads);
1249 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1250 * and does NOT mean that the message was not transmitted (DV and NAT)
1253 tcp_plugin_send (void *cls,
1254 const struct GNUNET_PeerIdentity *target,
1258 struct GNUNET_TIME_Relative timeout,
1259 struct Session *session,
1263 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1265 struct Plugin *plugin = cls;
1266 struct Session *cand_session;
1267 struct Session *next;
1268 struct PendingMessage *pm;
1269 struct GNUNET_CONNECTION_Handle *sa;
1273 struct sockaddr_in a4;
1274 struct sockaddr_in6 a6;
1275 const struct IPv4TcpAddress *t4;
1276 const struct IPv6TcpAddress *t6;
1277 unsigned int is_natd;
1279 GNUNET_STATISTICS_update (plugin->env->stats,
1280 gettext_noop ("# bytes TCP was asked to transmit"),
1283 /* FIXME: we could do this cheaper with a hash table
1284 where we could restrict the iteration to entries that match
1285 the target peer... */
1286 is_natd = GNUNET_NO;
1287 if (session == NULL)
1289 cand_session = NULL;
1290 next = plugin->sessions;
1291 while (NULL != (session = next))
1293 next = session->next;
1294 GNUNET_assert (session->client != NULL);
1295 if (0 != memcmp (target,
1297 sizeof (struct GNUNET_PeerIdentity)))
1299 if ( ( (GNUNET_SYSERR == force_address) &&
1300 (session->expecting_welcome == GNUNET_NO) ) ||
1301 (GNUNET_NO == force_address) )
1303 cand_session = select_better_session (cand_session,
1307 if (GNUNET_SYSERR == force_address)
1309 GNUNET_break (GNUNET_YES == force_address);
1315 if ( (addrlen != session->connect_alen) &&
1316 (session->is_nat == GNUNET_NO) )
1318 if ((0 != memcmp (session->connect_addr,
1320 addrlen)) && (session->is_nat == GNUNET_NO))
1322 cand_session = select_better_session (cand_session,
1325 session = cand_session;
1327 if ( (session == NULL) &&
1331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1333 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1334 GNUNET_i2s (target));
1336 GNUNET_STATISTICS_update (plugin->env->stats,
1337 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1342 if (session == NULL)
1344 if (addrlen == sizeof (struct IPv6TcpAddress))
1348 memset (&a6, 0, sizeof (a6));
1349 #if HAVE_SOCKADDR_IN_SIN_LEN
1350 a6.sin6_len = sizeof (a6);
1352 a6.sin6_family = AF_INET6;
1353 a6.sin6_port = t6->t6_port;
1354 if (t6->t6_port == 0)
1355 is_natd = GNUNET_YES;
1356 memcpy (&a6.sin6_addr,
1358 sizeof (struct in6_addr));
1362 else if (addrlen == sizeof (struct IPv4TcpAddress))
1366 memset (&a4, 0, sizeof (a4));
1367 #if HAVE_SOCKADDR_IN_SIN_LEN
1368 a4.sin_len = sizeof (a4);
1370 a4.sin_family = AF_INET;
1371 a4.sin_port = t4->t_port;
1372 if (t4->t_port == 0)
1373 is_natd = GNUNET_YES;
1374 a4.sin_addr.s_addr = t4->ipv4_addr;
1380 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1382 _("Address of unexpected length: %u\n"),
1388 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1389 return -1; /* NAT client only works with IPv4 addresses */
1392 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1393 (is_natd == GNUNET_YES) &&
1394 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1395 &target->hashPubKey)) )
1398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1400 _("Found valid IPv4 NAT address (creating session)!\n"));
1402 session = create_session (plugin,
1407 /* create new message entry */
1408 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1409 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1410 pm->msg = (const char*) &pm[1];
1411 memcpy (&pm[1], msg, msgbuf_size);
1412 /* FIXME: this memcpy can be up to 7% of our total run-time
1413 (for transport service) */
1414 pm->message_size = msgbuf_size;
1415 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1416 pm->transmit_cont = cont;
1417 pm->transmit_cont_cls = cont_cls;
1419 /* append pm to pending_messages list */
1420 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1421 session->pending_messages_tail,
1422 session->pending_messages_tail,
1425 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1426 &target->hashPubKey,
1428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1432 "Created NAT WAIT connection to `%4s' at `%s'\n",
1433 GNUNET_i2s (target),
1434 GNUNET_a2s (sb, sbs));
1436 run_gnunet_nat_client (plugin, &a4);
1439 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1440 (is_natd == GNUNET_YES) &&
1441 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1442 &target->hashPubKey)) )
1444 /* Only do one NAT punch attempt per peer identity */
1447 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1453 "Failed to create connection to `%4s' at `%s'\n",
1454 GNUNET_i2s (target),
1455 GNUNET_a2s (sb, sbs));
1457 GNUNET_STATISTICS_update (plugin->env->stats,
1458 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1466 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1467 GNUNET_i2s (target),
1468 GNUNET_a2s (sb, sbs));
1470 session = create_session (plugin,
1472 GNUNET_SERVER_connect_socket (plugin->server,
1475 session->connect_addr = GNUNET_malloc (addrlen);
1476 memcpy (session->connect_addr,
1479 session->connect_alen = addrlen;
1481 GNUNET_assert (session != NULL);
1482 GNUNET_assert (session->client != NULL);
1483 GNUNET_STATISTICS_update (plugin->env->stats,
1484 gettext_noop ("# bytes currently in TCP buffers"),
1487 /* create new message entry */
1488 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1489 pm->msg = (const char*) &pm[1];
1490 memcpy (&pm[1], msg, msgbuf_size);
1491 pm->message_size = msgbuf_size;
1492 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1493 pm->transmit_cont = cont;
1494 pm->transmit_cont_cls = cont_cls;
1496 /* append pm to pending_messages list */
1497 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1498 session->pending_messages_tail,
1499 session->pending_messages_tail,
1502 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1504 "Asked to transmit %u bytes to `%s', added message to list.\n",
1506 GNUNET_i2s (target));
1508 process_pending_messages (session);
1514 * Function that can be called to force a disconnect from the
1515 * specified neighbour. This should also cancel all previously
1516 * scheduled transmissions. Obviously the transmission may have been
1517 * partially completed already, which is OK. The plugin is supposed
1518 * to close the connection (if applicable) and no longer call the
1519 * transmit continuation(s).
1521 * Finally, plugin MUST NOT call the services's receive function to
1522 * notify the service that the connection to the specified target was
1523 * closed after a getting this call.
1525 * @param cls closure
1526 * @param target peer for which the last transmission is
1530 tcp_plugin_disconnect (void *cls,
1531 const struct GNUNET_PeerIdentity *target)
1533 struct Plugin *plugin = cls;
1534 struct Session *session;
1535 struct Session *next;
1536 struct PendingMessage *pm;
1539 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1541 "Asked to cancel session with `%4s'\n",
1542 GNUNET_i2s (target));
1544 next = plugin->sessions;
1545 while (NULL != (session = next))
1547 next = session->next;
1548 if (0 != memcmp (target,
1550 sizeof (struct GNUNET_PeerIdentity)))
1552 pm = session->pending_messages_head;
1555 pm->transmit_cont = NULL;
1556 pm->transmit_cont_cls = NULL;
1559 GNUNET_STATISTICS_update (session->plugin->env->stats,
1560 gettext_noop ("# transport-service disconnect requests for TCP"),
1563 disconnect_session (session);
1569 * Context for address to string conversion.
1571 struct PrettyPrinterContext
1574 * Function to call with the result.
1576 GNUNET_TRANSPORT_AddressStringCallback asc;
1579 * Clsoure for 'asc'.
1584 * Port to add after the IP address.
1591 * Append our port and forward the result.
1593 * @param cls the 'struct PrettyPrinterContext*'
1594 * @param hostname hostname part of the address
1597 append_port (void *cls, const char *hostname)
1599 struct PrettyPrinterContext *ppc = cls;
1602 if (hostname == NULL)
1604 ppc->asc (ppc->asc_cls, NULL);
1608 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1609 ppc->asc (ppc->asc_cls, ret);
1615 * Convert the transports address to a nice, human-readable
1618 * @param cls closure
1619 * @param type name of the transport that generated the address
1620 * @param addr one of the addresses of the host, NULL for the last address
1621 * the specific address format depends on the transport
1622 * @param addrlen length of the address
1623 * @param numeric should (IP) addresses be displayed in numeric form?
1624 * @param timeout after how long should we give up?
1625 * @param asc function to call on each string
1626 * @param asc_cls closure for asc
1629 tcp_plugin_address_pretty_printer (void *cls,
1634 struct GNUNET_TIME_Relative timeout,
1635 GNUNET_TRANSPORT_AddressStringCallback asc,
1638 struct Plugin *plugin = cls;
1639 struct PrettyPrinterContext *ppc;
1642 struct sockaddr_in a4;
1643 struct sockaddr_in6 a6;
1644 const struct IPv4TcpAddress *t4;
1645 const struct IPv6TcpAddress *t6;
1648 if (addrlen == sizeof (struct IPv6TcpAddress))
1651 memset (&a6, 0, sizeof (a6));
1652 a6.sin6_family = AF_INET6;
1653 a6.sin6_port = t6->t6_port;
1654 memcpy (&a6.sin6_addr,
1656 sizeof (struct in6_addr));
1657 port = ntohs (t6->t6_port);
1661 else if (addrlen == sizeof (struct IPv4TcpAddress))
1664 memset (&a4, 0, sizeof (a4));
1665 a4.sin_family = AF_INET;
1666 a4.sin_port = t4->t_port;
1667 a4.sin_addr.s_addr = t4->ipv4_addr;
1668 port = ntohs (t4->t_port);
1674 /* invalid address */
1675 GNUNET_break_op (0);
1676 asc (asc_cls, NULL);
1679 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1681 ppc->asc_cls = asc_cls;
1683 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1686 !numeric, timeout, &append_port, ppc);
1691 * Check if the given port is plausible (must be either our listen
1692 * port or our advertised port), or any port if we are behind NAT
1693 * and do not have a port open. If it is neither, we return
1696 * @param plugin global variables
1697 * @param in_port port number to check
1698 * @return GNUNET_OK if port is either open_port or adv_port
1701 check_port (struct Plugin *plugin,
1704 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1706 return GNUNET_SYSERR;
1711 * Function that will be called to check if a binary address for this
1712 * plugin is well-formed and corresponds to an address for THIS peer
1713 * (as per our configuration). Naturally, if absolutely necessary,
1714 * plugins can be a bit conservative in their answer, but in general
1715 * plugins should make sure that the address does not redirect
1716 * traffic to a 3rd party that might try to man-in-the-middle our
1719 * @param cls closure, our 'struct Plugin*'
1720 * @param addr pointer to the address
1721 * @param addrlen length of addr
1722 * @return GNUNET_OK if this is a plausible address for this peer
1723 * and transport, GNUNET_SYSERR if not
1726 tcp_plugin_check_address (void *cls,
1730 struct Plugin *plugin = cls;
1731 struct IPv4TcpAddress *v4;
1732 struct IPv6TcpAddress *v6;
1734 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1735 (addrlen != sizeof (struct IPv6TcpAddress)))
1737 GNUNET_break_op (0);
1738 return GNUNET_SYSERR;
1740 if (addrlen == sizeof (struct IPv4TcpAddress))
1742 v4 = (struct IPv4TcpAddress *) addr;
1744 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1747 check_port (plugin, ntohs (v4->t_port)))
1748 return GNUNET_SYSERR;
1750 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1751 return GNUNET_SYSERR;
1755 v6 = (struct IPv6TcpAddress *) addr;
1756 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1758 GNUNET_break_op (0);
1759 return GNUNET_SYSERR;
1762 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1765 check_port (plugin, ntohs (v6->t6_port)))
1766 return GNUNET_SYSERR;
1768 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1769 return GNUNET_SYSERR;
1776 * We've received a nat probe from this peer via TCP. Finish
1777 * creating the client session and resume sending of queued
1780 * @param cls closure
1781 * @param client identification of the client
1782 * @param message the actual message
1785 handle_tcp_nat_probe (void *cls,
1786 struct GNUNET_SERVER_Client *client,
1787 const struct GNUNET_MessageHeader *message)
1789 struct Plugin *plugin = cls;
1790 struct Session *session;
1791 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1794 struct IPv4TcpAddress *t4;
1795 struct IPv6TcpAddress *t6;
1796 const struct sockaddr_in *s4;
1797 const struct sockaddr_in6 *s6;
1800 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1802 "received tcp NAT probe\n");
1804 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1805 * a connection to this peer by running gnunet-nat-client. This peer
1806 * received the punch message and now wants us to use the new connection
1807 * as the default for that peer. Do so and then send a WELCOME message
1808 * so we can really be connected!
1810 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1816 if (0 == memcmp (&tcp_nat_probe->clientIdentity,
1817 plugin->env->my_identity,
1818 sizeof (struct GNUNET_PeerIdentity)))
1820 /* refuse connections from ourselves */
1821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1824 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1825 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1826 &tcp_nat_probe->clientIdentity.hashPubKey);
1827 if (session == NULL)
1830 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1832 "Did NOT find session for NAT probe!\n");
1834 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1840 "Found session for NAT probe!\n");
1842 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1843 &tcp_nat_probe->clientIdentity.hashPubKey,
1844 session) == GNUNET_YES);
1846 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1849 GNUNET_free (session);
1850 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1854 GNUNET_SERVER_client_keep (client);
1855 session->client = client;
1856 session->last_activity = GNUNET_TIME_absolute_get ();
1857 session->inbound = GNUNET_NO;
1860 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1862 "Found address `%s' for incoming connection\n",
1863 GNUNET_a2s (vaddr, alen));
1865 switch (((const struct sockaddr *)vaddr)->sa_family)
1869 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1870 t4->t_port = s4->sin_port;
1871 t4->ipv4_addr = s4->sin_addr.s_addr;
1872 session->connect_addr = t4;
1873 session->connect_alen = sizeof (struct IPv4TcpAddress);
1877 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1878 t6->t6_port = s6->sin6_port;
1879 memcpy (&t6->ipv6_addr,
1881 sizeof (struct in6_addr));
1882 session->connect_addr = t6;
1883 session->connect_alen = sizeof (struct IPv6TcpAddress);
1886 GNUNET_break_op (0);
1888 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1890 "Bad address for incoming connection!\n");
1892 GNUNET_free (vaddr);
1893 GNUNET_SERVER_client_drop (client);
1894 GNUNET_free (session);
1895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1898 GNUNET_free (vaddr);
1900 session->next = plugin->sessions;
1901 plugin->sessions = session;
1902 GNUNET_STATISTICS_update (plugin->env->stats,
1903 gettext_noop ("# TCP sessions active"),
1906 process_pending_messages (session);
1907 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1912 * We've received a welcome from this peer via TCP. Possibly create a
1913 * fresh client record and send back our welcome.
1915 * @param cls closure
1916 * @param client identification of the client
1917 * @param message the actual message
1920 handle_tcp_welcome (void *cls,
1921 struct GNUNET_SERVER_Client *client,
1922 const struct GNUNET_MessageHeader *message)
1924 struct Plugin *plugin = cls;
1925 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1926 struct Session *session;
1929 struct IPv4TcpAddress *t4;
1930 struct IPv6TcpAddress *t6;
1931 const struct sockaddr_in *s4;
1932 const struct sockaddr_in6 *s6;
1934 if (0 == memcmp (&wm->clientIdentity,
1935 plugin->env->my_identity,
1936 sizeof (struct GNUNET_PeerIdentity)))
1938 /* refuse connections from ourselves */
1939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1943 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1945 "Received %s message from `%4s'.\n",
1947 GNUNET_i2s (&wm->clientIdentity));
1949 GNUNET_STATISTICS_update (plugin->env->stats,
1950 gettext_noop ("# TCP WELCOME messages received"),
1953 session = find_session_by_client (plugin, client);
1955 if (session == NULL)
1958 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1960 "Received %s message from a `%4s', creating new session\n",
1962 GNUNET_i2s (&wm->clientIdentity));
1964 GNUNET_SERVER_client_keep (client);
1965 session = create_session (plugin,
1966 &wm->clientIdentity,
1969 session->inbound = GNUNET_YES;
1971 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1974 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1976 "Found address `%s' for incoming connection\n",
1977 GNUNET_a2s (vaddr, alen));
1979 if (alen == sizeof (struct sockaddr_in))
1982 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1983 t4->t_port = s4->sin_port;
1984 t4->ipv4_addr = s4->sin_addr.s_addr;
1985 session->connect_addr = t4;
1986 session->connect_alen = sizeof (struct IPv4TcpAddress);
1988 else if (alen == sizeof (struct sockaddr_in6))
1991 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1992 t6->t6_port = s6->sin6_port;
1993 memcpy (&t6->ipv6_addr,
1995 sizeof (struct in6_addr));
1996 session->connect_addr = t6;
1997 session->connect_alen = sizeof (struct IPv6TcpAddress);
2000 GNUNET_free (vaddr);
2005 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2007 "Did not obtain TCP socket address for incoming connection\n");
2010 process_pending_messages (session);
2016 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2018 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2020 "Found address `%s' (already have session)\n",
2021 GNUNET_a2s (vaddr, alen));
2022 GNUNET_free (vaddr);
2027 if (session->expecting_welcome != GNUNET_YES)
2029 GNUNET_break_op (0);
2030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2033 session->last_activity = GNUNET_TIME_absolute_get ();
2034 session->expecting_welcome = GNUNET_NO;
2035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2040 * Task to signal the server that we can continue
2041 * receiving from the TCP client now.
2043 * @param cls the 'struct Session*'
2044 * @param tc task context (unused)
2047 delayed_done (void *cls,
2048 const struct GNUNET_SCHEDULER_TaskContext *tc)
2050 struct Session *session = cls;
2051 struct GNUNET_TIME_Relative delay;
2053 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2054 delay = session->plugin->env->receive (session->plugin->env->cls,
2060 if (delay.rel_value == 0)
2061 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2063 session->receive_delay_task =
2064 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2069 * We've received data for this peer via TCP. Unbox,
2070 * compute latency and forward.
2072 * @param cls closure
2073 * @param client identification of the client
2074 * @param message the actual message
2077 handle_tcp_data (void *cls,
2078 struct GNUNET_SERVER_Client *client,
2079 const struct GNUNET_MessageHeader *message)
2081 struct Plugin *plugin = cls;
2082 struct Session *session;
2083 struct GNUNET_TIME_Relative delay;
2086 type = ntohs (message->type);
2087 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2088 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2090 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2094 session = find_session_by_client (plugin, client);
2095 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2100 session->last_activity = GNUNET_TIME_absolute_get ();
2102 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2104 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2105 (unsigned int) ntohs (message->size),
2106 (unsigned int) ntohs (message->type),
2107 GNUNET_i2s (&session->target));
2109 GNUNET_STATISTICS_update (plugin->env->stats,
2110 gettext_noop ("# bytes received via TCP"),
2111 ntohs (message->size),
2113 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2114 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2115 distance[0].value = htonl (1);
2116 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2117 distance[1].value = htonl (0);
2118 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2119 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2122 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2123 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2124 if (delay.rel_value == 0)
2126 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2133 "Throttling receiving from `%s' for %llu ms\n",
2134 GNUNET_i2s (&session->target),
2135 (unsigned long long) delay.rel_value);
2137 GNUNET_SERVER_disable_receive_done_warning (client);
2138 session->receive_delay_task =
2139 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2145 * Functions with this signature are called whenever a peer
2146 * is disconnected on the network level.
2148 * @param cls closure
2149 * @param client identification of the client
2152 disconnect_notify (void *cls,
2153 struct GNUNET_SERVER_Client *client)
2155 struct Plugin *plugin = cls;
2156 struct Session *session;
2160 session = find_session_by_client (plugin, client);
2161 if (session == NULL)
2162 return; /* unknown, nothing to do */
2164 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2166 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2167 GNUNET_i2s (&session->target),
2168 (session->connect_addr != NULL) ?
2169 tcp_address_to_string (session->plugin,
2170 session->connect_addr,
2171 session->connect_alen) : "*");
2173 GNUNET_STATISTICS_update (session->plugin->env->stats,
2174 gettext_noop ("# network-level TCP disconnect events"),
2177 disconnect_session (session);
2181 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
2184 int local = GNUNET_NO;
2185 int af = addr->sa_family;
2190 uint32_t netmask = 0x7F000000;
2191 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
2192 res = (address >> 24) ^ (netmask >> 24);
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2199 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2205 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
2206 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2221 * Add the IP of our network interface to the list of
2222 * our internal IP addresses.
2224 * @param cls the 'struct Plugin*'
2225 * @param name name of the interface
2226 * @param isDefault do we think this may be our default interface
2227 * @param addr address of the interface
2228 * @param addrlen number of bytes in addr
2229 * @return GNUNET_OK to continue iterating
2232 process_interfaces (void *cls,
2235 const struct sockaddr *addr, socklen_t addrlen)
2237 struct Plugin *plugin = cls;
2239 struct IPv4TcpAddress t4;
2240 struct IPv6TcpAddress t6;
2241 struct IPv4TcpAddress t4_nat;
2242 struct IPv6TcpAddress t6_nat;
2246 char buf[INET6_ADDRSTRLEN];
2248 af = addr->sa_family;
2251 if (plugin->use_localaddresses == GNUNET_NO)
2253 if (GNUNET_YES == check_localaddress (addr, addrlen))
2256 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2258 "Not notifying transport of address `%s' (local address)\n",
2259 GNUNET_a2s (addr, addrlen));
2268 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2269 GNUNET_assert (NULL != inet_ntop(AF_INET,
2273 if ( (plugin->bind_address != NULL) &&
2274 (0 != strcmp(buf, plugin->bind_address)) )
2277 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2279 "Not notifying transport of address `%s' (does not match bind address)\n",
2280 GNUNET_a2s (addr, addrlen));
2284 if ( (plugin->internal_address == NULL) &&
2286 plugin->internal_address = GNUNET_strdup (buf);
2287 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2288 if (plugin->behind_nat == GNUNET_YES)
2290 /* Also advertise as NAT (with port 0) */
2291 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2292 t4_nat.t_port = htons(0);
2295 t4.t_port = htons (plugin->adv_port);
2300 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2301 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2305 /* skip link local addresses */
2308 memcpy (&t6.ipv6_addr,
2309 &((struct sockaddr_in6 *) addr)->sin6_addr,
2310 sizeof (struct in6_addr));
2312 /* check bind address */
2313 GNUNET_assert (NULL != inet_ntop(AF_INET6,
2318 if ( (plugin->bind_address != NULL) &&
2319 (0 != strcmp(buf, plugin->bind_address)) )
2322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2324 "Not notifying transport of address `%s' (does not match bind address)\n",
2325 GNUNET_a2s (addr, addrlen));
2330 add_to_address_list (plugin,
2332 sizeof (struct in6_addr));
2333 if (plugin->behind_nat == GNUNET_YES)
2335 /* Also advertise as NAT (with port 0) */
2336 memcpy (&t6_nat.ipv6_addr,
2337 &((struct sockaddr_in6 *) addr)->sin6_addr,
2338 sizeof (struct in6_addr));
2339 t6_nat.t6_port = htons(0);
2342 t6.t6_port = htons (plugin->adv_port);
2350 if (plugin->adv_port != 0)
2353 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2355 "Found address `%s' (%s) len %d\n",
2356 GNUNET_a2s (addr, addrlen), name, args);
2358 plugin->env->notify_address (plugin->env->cls,
2360 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2363 if (arg_nat != NULL)
2365 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2367 _("Found address `%s' (%s) len %d\n"),
2368 GNUNET_a2s (addr, addrlen), name, args);
2369 plugin->env->notify_address (plugin->env->cls,
2371 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2379 * Function called by the resolver for each address obtained from DNS
2380 * for our own hostname. Add the addresses to the list of our
2381 * external IP addresses.
2383 * @param cls closure
2384 * @param addr one of the addresses of the host, NULL for the last address
2385 * @param addrlen length of the address
2388 process_hostname_ips (void *cls,
2389 const struct sockaddr *addr, socklen_t addrlen)
2391 struct Plugin *plugin = cls;
2395 plugin->hostname_dns = NULL;
2398 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2399 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2404 * We can now send a probe message, copy into buffer to really send.
2406 * @param cls closure, a struct TCPProbeContext
2407 * @param size max size to copy
2408 * @param buf buffer to copy message to
2409 * @return number of bytes copied into buf
2412 notify_send_probe (void *cls,
2416 struct TCPProbeContext *tcp_probe_ctx = cls;
2417 struct Plugin *plugin = tcp_probe_ctx->plugin;
2420 tcp_probe_ctx->transmit_handle = NULL;
2421 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2426 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2427 GNUNET_free(tcp_probe_ctx);
2430 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2431 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2432 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2433 tcp_probe_ctx->sock);
2434 ret = sizeof(tcp_probe_ctx->message);
2435 GNUNET_free(tcp_probe_ctx);
2441 * We have been notified that gnunet-nat-server has written something to stdout.
2442 * Handle the output, then reschedule this function to be called again once
2443 * more is available.
2445 * @param cls the plugin handle
2446 * @param tc the scheduling context
2449 tcp_plugin_server_read (void *cls,
2450 const struct GNUNET_SCHEDULER_TaskContext *tc)
2452 struct Plugin *plugin = cls;
2457 const char *port_start;
2458 struct sockaddr_in sin_addr;
2459 struct TCPProbeContext *tcp_probe_ctx;
2460 struct GNUNET_CONNECTION_Handle *sock;
2462 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2464 memset (mybuf, 0, sizeof(mybuf));
2465 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2471 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2473 "Finished reading from server stdout with code: %d\n",
2476 /* FIXME: consider process_wait here? */
2481 for (i = 0; i < sizeof(mybuf); i++)
2483 if (mybuf[i] == '\n')
2488 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2491 port_start = &mybuf[i + 1];
2495 /* construct socket address of sender */
2496 memset (&sin_addr, 0, sizeof (sin_addr));
2497 sin_addr.sin_family = AF_INET;
2498 #if HAVE_SOCKADDR_IN_SIN_LEN
2499 sin_addr.sin_len = sizeof (sin_addr);
2501 if ( (NULL == port_start) ||
2502 (1 != sscanf (port_start, "%d", &port)) ||
2503 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2505 /* should we restart gnunet-nat-server? */
2506 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2508 _("gnunet-nat-server generated malformed address `%s'\n"),
2510 plugin->server_read_task
2511 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2512 plugin->server_stdout_handle,
2513 &tcp_plugin_server_read,
2517 sin_addr.sin_port = htons((uint16_t) port);
2519 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2521 "gnunet-nat-server read: %s:%d\n",
2526 * We have received an ICMP response, ostensibly from a peer
2527 * that wants to connect to us! Send a message to establish a connection.
2529 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2530 (const struct sockaddr *)&sin_addr,
2534 /* failed for some odd reason (out of sockets?); ignore attempt */
2535 plugin->server_read_task =
2536 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2537 plugin->server_stdout_handle,
2538 &tcp_plugin_server_read,
2543 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2544 "Sending TCP probe message to `%s:%u'!\n",
2546 (unsigned int) port);
2547 /* FIXME: do we need to track these probe context objects so that
2548 we can clean them up on plugin unload? */
2550 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2551 tcp_probe_ctx->message.header.size
2552 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2553 tcp_probe_ctx->message.header.type
2554 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2555 memcpy (&tcp_probe_ctx->message.clientIdentity,
2556 plugin->env->my_identity,
2557 sizeof(struct GNUNET_PeerIdentity));
2558 tcp_probe_ctx->plugin = plugin;
2559 tcp_probe_ctx->sock = sock;
2560 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2563 tcp_probe_ctx->transmit_handle
2564 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2565 ntohs (tcp_probe_ctx->message.header.size),
2566 GNUNET_TIME_UNIT_FOREVER_REL,
2567 ¬ify_send_probe, tcp_probe_ctx);
2569 plugin->server_read_task =
2570 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2571 plugin->server_stdout_handle,
2572 &tcp_plugin_server_read,
2578 * Start the gnunet-nat-server process for users behind NAT.
2580 * @param plugin the transport plugin
2581 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2584 tcp_transport_start_nat_server (struct Plugin *plugin)
2586 if (plugin->internal_address == NULL)
2587 return GNUNET_SYSERR;
2588 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2591 if (plugin->server_stdout == NULL)
2592 return GNUNET_SYSERR;
2594 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2596 "Starting %s %s\n", "gnunet-nat-server", plugin->internal_address);
2598 /* Start the server process */
2599 plugin->server_proc = GNUNET_OS_start_process (NULL,
2600 plugin->server_stdout,
2601 "gnunet-nat-server",
2602 "gnunet-nat-server",
2603 plugin->internal_address,
2605 if (plugin->server_proc == NULL)
2607 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2609 _("Failed to start %s\n"),
2610 "gnunet-nat-server");
2611 GNUNET_DISK_pipe_close (plugin->server_stdout);
2612 plugin->server_stdout = NULL;
2613 return GNUNET_SYSERR;
2615 /* Close the write end of the read pipe */
2616 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2617 GNUNET_DISK_PIPE_END_WRITE);
2618 plugin->server_stdout_handle
2619 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2620 GNUNET_DISK_PIPE_END_READ);
2621 plugin->server_read_task
2622 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2623 plugin->server_stdout_handle,
2624 &tcp_plugin_server_read,
2631 * Return the actual path to a file found in the current
2632 * PATH environment variable.
2634 * @param binary the name of the file to find
2635 * @return path to binary, NULL if not found
2638 get_path_from_PATH (const char *binary)
2646 p = getenv ("PATH");
2649 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2651 _("PATH environment variable is unset.\n"));
2654 path = GNUNET_strdup (p); /* because we write on it */
2655 buf = GNUNET_malloc (strlen (path) + 20);
2658 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2661 sprintf (buf, "%s/%s", pos, binary);
2662 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2669 sprintf (buf, "%s/%s", pos, binary);
2670 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2682 * Check whether the suid bit is set on a file.
2683 * Attempts to find the file using the current
2684 * PATH environment variable as a search path.
2686 * @param binary the name of the file to check
2687 * @return GNUNET_YES if the file is SUID,
2689 * GNUNET_SYSERR on error
2692 check_gnunet_nat_binary (const char *binary)
2694 struct stat statbuf;
2700 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2701 p = get_path_from_PATH (binaryexe);
2704 p = get_path_from_PATH (binary);
2708 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2710 _("Could not find binary `%s' in PATH!\n"),
2714 if (0 != STAT (p, &statbuf))
2716 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2717 _("stat (%s) failed: %s\n"),
2721 return GNUNET_SYSERR;
2725 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2726 (statbuf.st_uid == 0) )
2730 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2731 if (INVALID_SOCKET == rawsock)
2733 DWORD err = GetLastError ();
2734 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2736 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2737 return GNUNET_NO; /* not running as administrator */
2739 closesocket (rawsock);
2746 * Our (external) hostname was resolved.
2748 * @param cls the 'struct Plugin'
2749 * @param addr NULL on error, otherwise result of DNS lookup
2750 * @param addrlen number of bytes in addr
2753 process_external_ip (void *cls,
2754 const struct sockaddr *addr,
2757 struct Plugin *plugin = cls;
2758 const struct sockaddr_in *s;
2759 struct IPv4TcpAddress t4;
2760 char buf[INET_ADDRSTRLEN];
2762 plugin->ext_dns = NULL;
2765 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2766 s = (const struct sockaddr_in *) addr;
2767 t4.ipv4_addr = s->sin_addr.s_addr;
2768 if ( (plugin->behind_nat == GNUNET_YES) &&
2769 (plugin->enable_nat_server == GNUNET_YES) )
2771 t4.t_port = htons(0);
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2774 "Notifying transport of address %s:%d\n",
2775 plugin->external_address,
2780 t4.t_port = htons(plugin->adv_port);
2781 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2783 "Notifying transport of address %s:%d\n",
2784 plugin->external_address,
2785 (int) plugin->adv_port);
2788 if ((plugin->bind_address != NULL) && (plugin->behind_nat == GNUNET_NO))
2790 GNUNET_assert (NULL != inet_ntop(AF_INET,
2794 if (0 != strcmp (plugin->bind_address, buf))
2796 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2798 "NAT is not enabled and specific bind address `%s' differs from external address `%s'! Not notifying about external address `%s'\n",
2799 plugin->bind_address,
2800 plugin->external_address,
2801 plugin->external_address);
2806 add_to_address_list (plugin,
2808 sizeof (struct in_addr));
2810 plugin->env->notify_address (plugin->env->cls,
2813 GNUNET_TIME_UNIT_FOREVER_REL);
2818 * Entry point for the plugin.
2820 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2821 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2824 libgnunet_plugin_transport_tcp_init (void *cls)
2826 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2827 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2828 sizeof (struct WelcomeMessage)},
2829 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2830 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2833 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2834 struct GNUNET_TRANSPORT_PluginFunctions *api;
2835 struct Plugin *plugin;
2836 struct GNUNET_SERVICE_Context *service;
2837 unsigned long long aport;
2838 unsigned long long bport;
2842 int enable_nat_client;
2843 int enable_nat_server;
2845 int use_localaddresses;
2846 char *internal_address;
2847 char *external_address;
2849 struct sockaddr_in in_addr;
2850 struct GNUNET_TIME_Relative idle_timeout;
2852 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2855 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2858 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2860 "ENABLE_NAT_CLIENT");
2861 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2863 "ENABLE_NAT_SERVER");
2864 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2868 if ( (GNUNET_YES == enable_nat_server) &&
2869 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2871 enable_nat_server = GNUNET_NO;
2872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2873 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2874 "gnunet-nat-server");
2877 if ( (GNUNET_YES == enable_nat_client) &&
2878 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2880 enable_nat_client = GNUNET_NO;
2881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2882 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2883 "gnunet-nat-client");
2886 external_address = NULL;
2888 GNUNET_CONFIGURATION_have_value (env->cfg,
2890 "EXTERNAL_ADDRESS"))
2892 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2898 if ( (external_address != NULL) &&
2899 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2902 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2904 _("Malformed %s `%s' given in configuration!\n"),
2909 if ( (external_address == NULL) &&
2910 (nat_punched == GNUNET_YES) )
2912 nat_punched = GNUNET_NO;
2913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2914 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2915 "EXTERNAL_ADDRESS");
2918 if (GNUNET_YES == nat_punched)
2920 enable_nat_server = GNUNET_NO;
2921 enable_upnp = GNUNET_NO;
2924 bind_address = NULL;
2925 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2930 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2932 _("Binding TCP plugin to specific address: `%s'\n"),
2936 internal_address = NULL;
2938 GNUNET_CONFIGURATION_have_value (env->cfg,
2940 "INTERNAL_ADDRESS"))
2942 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2948 if ( (internal_address != NULL) &&
2949 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2951 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2953 _("Malformed %s `%s' given in configuration!\n"),
2956 GNUNET_free_non_null(internal_address);
2957 GNUNET_free_non_null(external_address);
2961 if ((bind_address != NULL) && (internal_address != NULL))
2963 if (0 != strcmp(internal_address, bind_address ))
2965 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2967 "Specific bind address `%s' and internal address `%s' must not differ, forcing internal address to bind address!\n",
2968 bind_address, internal_address);
2969 GNUNET_free (internal_address);
2970 internal_address = bind_address;
2971 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2972 "tcp","New internal address `%s'\n", internal_address);
2978 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2984 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2990 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2992 _("Require valid port number for service `%s' in configuration!\n"),
2994 GNUNET_free_non_null(external_address);
2995 GNUNET_free_non_null(internal_address);
2999 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3002 if (use_localaddresses == GNUNET_SYSERR)
3003 use_localaddresses = GNUNET_NO;
3012 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
3013 if (service == NULL)
3015 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
3017 _("Failed to start service.\n"));
3024 plugin = GNUNET_malloc (sizeof (struct Plugin));
3025 plugin->open_port = bport;
3026 plugin->adv_port = aport;
3027 plugin->bind_address = bind_address;
3028 plugin->external_address = external_address;
3029 plugin->internal_address = internal_address;
3030 plugin->behind_nat = behind_nat;
3031 plugin->nat_punched = nat_punched;
3032 plugin->enable_nat_client = enable_nat_client;
3033 plugin->enable_nat_server = enable_nat_server;
3034 plugin->enable_upnp = enable_upnp;
3035 plugin->use_localaddresses = use_localaddresses;
3037 plugin->lsock = NULL;
3038 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3040 api->send = &tcp_plugin_send;
3041 api->disconnect = &tcp_plugin_disconnect;
3042 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3043 api->check_address = &tcp_plugin_check_address;
3044 api->address_to_string = &tcp_address_to_string;
3045 plugin->service = service;
3046 if (service != NULL)
3048 plugin->server = GNUNET_SERVICE_get_server (service);
3053 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3058 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3060 _("Failed to find option %s in section %s!\n"),
3063 GNUNET_free_non_null(external_address);
3064 GNUNET_free_non_null(internal_address);
3068 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
3069 idle_timeout, GNUNET_YES);
3071 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3072 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
3074 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
3076 plugin->handlers[i].callback_cls = plugin;
3077 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
3078 GNUNET_SERVER_disconnect_notify (plugin->server,
3081 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3083 if ( (plugin->behind_nat == GNUNET_YES) &&
3084 (plugin->enable_nat_server == GNUNET_YES) &&
3085 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
3087 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3089 _("Failed to start %s required for NAT in %s!\n"),
3092 GNUNET_free_non_null(external_address);
3093 GNUNET_free_non_null(internal_address);
3094 if (service != NULL)
3095 GNUNET_SERVICE_stop (service);
3097 GNUNET_SERVER_destroy (plugin->server);
3102 if (enable_nat_client == GNUNET_YES)
3104 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
3105 GNUNET_assert (plugin->nat_wait_conns != NULL);
3109 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3111 _("TCP transport listening on port %llu\n"),
3114 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3116 _("TCP transport not listening on any port (client only)\n"));
3118 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3120 _("TCP transport advertises itself as being on port %llu\n"),
3123 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
3125 HOSTNAME_RESOLVE_TIMEOUT,
3126 &process_hostname_ips,
3129 if (plugin->external_address != NULL)
3131 plugin->ext_dns = GNUNET_RESOLVER_ip_get (env->cfg,
3132 plugin->external_address,
3134 GNUNET_TIME_UNIT_MINUTES,
3135 &process_external_ip,
3143 * Exit point from the plugin.
3146 libgnunet_plugin_transport_tcp_done (void *cls)
3148 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3149 struct Plugin *plugin = api->cls;
3150 struct Session *session;
3151 struct LocalAddrList *lal;
3152 struct TCPProbeContext *tcp_probe;
3154 if (plugin->ext_dns != NULL)
3156 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
3157 plugin->ext_dns = NULL;
3159 while (NULL != (session = plugin->sessions))
3160 disconnect_session (session);
3161 if (NULL != plugin->hostname_dns)
3163 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
3164 plugin->hostname_dns = NULL;
3166 if (plugin->service != NULL)
3167 GNUNET_SERVICE_stop (plugin->service);
3169 GNUNET_SERVER_destroy (plugin->server);
3170 GNUNET_free (plugin->handlers);
3171 while (NULL != (lal = plugin->lal_head))
3173 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3176 if (lal->nat != NULL)
3177 GNUNET_NAT_unregister (lal->nat);
3178 GNUNET_free_non_null (lal->external_nat_address);
3181 while (NULL != (tcp_probe = plugin->probe_head))
3183 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3186 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3187 GNUNET_free (tcp_probe);
3190 if ((plugin->behind_nat == GNUNET_YES) &&
3191 (plugin->enable_nat_server == GNUNET_YES))
3193 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3194 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3195 GNUNET_OS_process_wait (plugin->server_proc);
3196 GNUNET_OS_process_close (plugin->server_proc);
3197 plugin->server_proc = NULL;
3199 GNUNET_free_non_null(plugin->bind_address);
3200 GNUNET_free (plugin);
3205 /* end of plugin_transport_tcp.c */