2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_connection_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
43 #define DEBUG_TCP_NAT GNUNET_NO
46 * 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 * List of our IP addresses.
439 struct LocalAddrList *lal_head;
442 * Tail of our IP address list.
444 struct LocalAddrList *lal_tail;
447 * List of active TCP probes.
449 struct TCPProbeContext *probe_head;
452 * List of active TCP probes.
454 struct TCPProbeContext *probe_tail;
457 * Handle for (DYN)DNS lookup of our external IP.
459 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
462 * ID of task used to update our addresses when one expires.
464 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
467 * Port that we are actually listening on.
472 * Port that the user said we would have visible to the
478 * Is this transport configured to be behind a NAT?
483 * Has the NAT been punched?
488 * Is this transport configured to allow connections to NAT'd peers?
490 int enable_nat_client;
493 * Should we run the gnunet-nat-server?
495 int enable_nat_server;
498 * Are we allowed to try UPnP/PMP for NAT traversal?
506 * Our external IP address/port mapping has changed.
508 * @param cls closure, the 'struct LocalAddrList'
509 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
510 * the previous (now invalid) one
511 * @param addr either the previous or the new public IP address
512 * @param addrlen actual lenght of the address
515 nat_port_map_callback (void *cls,
517 const struct sockaddr *addr,
520 struct LocalAddrList *lal = cls;
521 struct Plugin *plugin = lal->plugin;
523 struct IPv4TcpAddress t4;
524 struct IPv6TcpAddress t6;
528 /* convert 'addr' to our internal format */
529 af = addr->sa_family;
533 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
534 t6.t6_port = ((struct sockaddr_in *) addr)->sin_port;
539 memcpy (&t6.ipv6_addr,
540 &((struct sockaddr_in6 *) addr)->sin6_addr,
541 sizeof (struct in6_addr));
542 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
551 /* modify our published address list */
552 if (GNUNET_YES == add_remove)
554 plugin->env->notify_address (plugin->env->cls,
556 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
557 GNUNET_free_non_null (lal->external_nat_address);
558 lal->external_nat_address = GNUNET_memdup (arg, args);
559 lal->ena_size = args;
563 plugin->env->notify_address (plugin->env->cls,
565 arg, args, GNUNET_TIME_UNIT_ZERO);
566 GNUNET_free_non_null (lal->external_nat_address);
573 * Add the given address to the list of 'local' addresses, thereby
574 * making it a 'legal' address for this peer to have.
576 * @param plugin the plugin
577 * @param arg the address, either an IPv4 or an IPv6 IP address
578 * @param arg_size number of bytes in arg
581 add_to_address_list (struct Plugin *plugin,
585 struct LocalAddrList *lal;
586 struct sockaddr_in v4;
587 struct sockaddr_in6 v6;
588 const struct sockaddr *sa;
591 lal = plugin->lal_head;
594 if ( (lal->size == arg_size) &&
595 (0 == memcmp (&lal[1], arg, arg_size)) )
599 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
600 lal->plugin = plugin;
601 lal->size = arg_size;
602 memcpy (&lal[1], arg, arg_size);
603 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
606 if (plugin->open_port == 0)
607 return; /* we're not listening at all... */
608 if (arg_size == sizeof (struct in_addr))
610 memset (&v4, 0, sizeof (v4));
611 v4.sin_family = AF_INET;
612 v4.sin_port = htons (plugin->open_port);
613 memcpy (&v4.sin_addr, arg, arg_size);
614 #if HAVE_SOCKADDR_IN_SIN_LEN
615 v4.sin_len = sizeof (struct sockaddr_in);
617 sa = (const struct sockaddr*) &v4;
620 else if (arg_size == sizeof (struct in6_addr))
622 memset (&v6, 0, sizeof (v6));
623 v6.sin6_family = AF_INET6;
624 v6.sin6_port = htons (plugin->open_port);
625 memcpy (&v6.sin6_addr, arg, arg_size);
626 #if HAVE_SOCKADDR_IN_SIN_LEN
627 v6.sin6_len = sizeof (struct sockaddr_in6);
629 sa = (const struct sockaddr*) &v6;
637 if ( (plugin->behind_nat == GNUNET_YES) &&
638 (plugin->enable_upnp == GNUNET_YES) )
639 lal->nat = GNUNET_NAT_register (sa, salen,
640 &nat_port_map_callback,
646 * Check if the given address is in the list of 'local' addresses.
648 * @param plugin the plugin
649 * @param arg the address, either an IPv4 or an IPv6 IP address
650 * @param arg_size number of bytes in arg
651 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
654 check_local_addr (struct Plugin *plugin,
658 struct LocalAddrList *lal;
660 lal = plugin->lal_head;
663 if ( (lal->size == arg_size) &&
664 (0 == memcmp (&lal[1], arg, arg_size)) )
668 return GNUNET_SYSERR;
673 * Check if the given address is in the list of 'mapped' addresses.
675 * @param plugin the plugin
676 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
677 * @param arg_size number of bytes in arg
678 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
681 check_mapped_addr (struct Plugin *plugin,
685 struct LocalAddrList *lal;
687 lal = plugin->lal_head;
690 if ( (lal->ena_size == arg_size) &&
691 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
695 return GNUNET_SYSERR;
700 * Function called for a quick conversion of the binary address to
701 * a numeric address. Note that the caller must not free the
702 * address and that the next call to this function is allowed
703 * to override the address again.
705 * @param cls closure ('struct Plugin*')
706 * @param addr binary address
707 * @param addrlen length of the address
708 * @return string representing the same address
711 tcp_address_to_string (void *cls,
715 static char rbuf[INET6_ADDRSTRLEN + 12];
716 char buf[INET6_ADDRSTRLEN];
720 const struct IPv4TcpAddress *t4;
721 const struct IPv6TcpAddress *t6;
725 if (addrlen == sizeof (struct IPv6TcpAddress))
729 port = ntohs (t6->t6_port);
730 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
733 else if (addrlen == sizeof (struct IPv4TcpAddress))
737 port = ntohs (t4->t_port);
738 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
743 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
745 _("Unexpected address length: %u\n"),
750 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
752 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
755 GNUNET_snprintf (rbuf,
757 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
765 * Find the session handle for the given client.
767 * @param plugin the plugin
768 * @param client which client to find the session handle for
769 * @return NULL if no matching session exists
771 static struct Session *
772 find_session_by_client (struct Plugin *plugin,
773 const struct GNUNET_SERVER_Client *client)
777 ret = plugin->sessions;
778 while ((ret != NULL) && (client != ret->client))
785 * Create a new session. Also queues a welcome message.
787 * @param plugin the plugin
788 * @param target peer to connect to
789 * @param client client to use
790 * @param is_nat this a NAT session, we should wait for a client to
791 * connect to us from an address, then assign that to
793 * @return new session object
795 static struct Session *
796 create_session (struct Plugin *plugin,
797 const struct GNUNET_PeerIdentity *target,
798 struct GNUNET_SERVER_Client *client,
802 struct PendingMessage *pm;
803 struct WelcomeMessage welcome;
805 if (is_nat != GNUNET_YES)
806 GNUNET_assert (client != NULL);
808 GNUNET_assert (client == NULL);
810 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
812 "Creating new session for peer `%4s'\n",
813 GNUNET_i2s (target));
815 ret = GNUNET_malloc (sizeof (struct Session));
816 ret->last_activity = GNUNET_TIME_absolute_get ();
817 ret->plugin = plugin;
818 ret->is_nat = is_nat;
819 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
821 ret->next = plugin->sessions;
822 plugin->sessions = ret;
824 ret->client = client;
825 ret->target = *target;
826 ret->expecting_welcome = GNUNET_YES;
827 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
828 pm->msg = (const char*) &pm[1];
829 pm->message_size = sizeof (struct WelcomeMessage);
830 welcome.header.size = htons (sizeof (struct WelcomeMessage));
831 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
832 welcome.clientIdentity = *plugin->env->my_identity;
833 memcpy (&pm[1], &welcome, sizeof (welcome));
834 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
835 GNUNET_STATISTICS_update (plugin->env->stats,
836 gettext_noop ("# bytes currently in TCP buffers"),
839 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
840 ret->pending_messages_tail,
842 if (is_nat != GNUNET_YES)
843 GNUNET_STATISTICS_update (plugin->env->stats,
844 gettext_noop ("# TCP sessions active"),
852 * If we have pending messages, ask the server to
853 * transmit them (schedule the respective tasks, etc.)
855 * @param session for which session should we do this
857 static void process_pending_messages (struct Session *session);
861 * Function called to notify a client about the socket
862 * being ready to queue more data. "buf" will be
863 * NULL and "size" zero if the socket was closed for
864 * writing in the meantime.
867 * @param size number of bytes available in buf
868 * @param buf where the callee should write the message
869 * @return number of bytes written to buf
872 do_transmit (void *cls, size_t size, void *buf)
874 struct Session *session = cls;
875 struct GNUNET_PeerIdentity pid;
876 struct Plugin *plugin;
877 struct PendingMessage *pos;
878 struct PendingMessage *hd;
879 struct PendingMessage *tl;
880 struct GNUNET_TIME_Absolute now;
884 session->transmit_handle = NULL;
885 plugin = session->plugin;
889 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
891 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
892 GNUNET_i2s (&session->target));
894 /* timeout; cancel all messages that have already expired */
898 now = GNUNET_TIME_absolute_get ();
899 while ( (NULL != (pos = session->pending_messages_head)) &&
900 (pos->timeout.abs_value <= now.abs_value) )
902 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
903 session->pending_messages_tail,
906 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
908 "Failed to transmit %u byte message to `%4s'.\n",
910 GNUNET_i2s (&session->target));
912 ret += pos->message_size;
913 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
915 /* do this call before callbacks (so that if callbacks destroy
916 session, they have a chance to cancel actions done by this
918 process_pending_messages (session);
919 pid = session->target;
920 /* no do callbacks and do not use session again since
921 the callbacks may abort the session */
922 while (NULL != (pos = hd))
924 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
925 if (pos->transmit_cont != NULL)
926 pos->transmit_cont (pos->transmit_cont_cls,
927 &pid, GNUNET_SYSERR);
930 GNUNET_STATISTICS_update (plugin->env->stats,
931 gettext_noop ("# bytes currently in TCP buffers"),
934 GNUNET_STATISTICS_update (plugin->env->stats,
935 gettext_noop ("# bytes discarded by TCP (timeout)"),
940 /* copy all pending messages that would fit */
945 while (NULL != (pos = session->pending_messages_head))
947 if (ret + pos->message_size > size)
949 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
950 session->pending_messages_tail,
952 GNUNET_assert (size >= pos->message_size);
953 /* FIXME: this memcpy can be up to 7% of our total runtime */
954 memcpy (cbuf, pos->msg, pos->message_size);
955 cbuf += pos->message_size;
956 ret += pos->message_size;
957 size -= pos->message_size;
958 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
960 /* schedule 'continuation' before callbacks so that callbacks that
961 cancel everything don't cause us to use a session that no longer
963 process_pending_messages (session);
964 session->last_activity = GNUNET_TIME_absolute_get ();
965 pid = session->target;
966 /* we'll now call callbacks that may cancel the session; hence
967 we should not use 'session' after this point */
968 while (NULL != (pos = hd))
970 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
971 if (pos->transmit_cont != NULL)
972 pos->transmit_cont (pos->transmit_cont_cls,
976 GNUNET_assert (hd == NULL);
977 GNUNET_assert (tl == NULL);
979 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
981 "Transmitting %u bytes\n",
984 GNUNET_STATISTICS_update (plugin->env->stats,
985 gettext_noop ("# bytes currently in TCP buffers"),
988 GNUNET_STATISTICS_update (plugin->env->stats,
989 gettext_noop ("# bytes transmitted via TCP"),
997 * If we have pending messages, ask the server to
998 * transmit them (schedule the respective tasks, etc.)
1000 * @param session for which session should we do this
1003 process_pending_messages (struct Session *session)
1005 struct PendingMessage *pm;
1007 GNUNET_assert (session->client != NULL);
1008 if (session->transmit_handle != NULL)
1010 if (NULL == (pm = session->pending_messages_head))
1013 session->transmit_handle
1014 = GNUNET_SERVER_notify_transmit_ready (session->client,
1016 GNUNET_TIME_absolute_get_remaining
1018 &do_transmit, session);
1023 * Functions with this signature are called whenever we need
1024 * to close a session due to a disconnect or failure to
1025 * establish a connection.
1027 * @param session session to close down
1030 disconnect_session (struct Session *session)
1032 struct Session *prev;
1033 struct Session *pos;
1034 struct PendingMessage *pm;
1037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1039 "Disconnecting from `%4s' at %s.\n",
1040 GNUNET_i2s (&session->target),
1041 (session->connect_addr != NULL) ?
1042 tcp_address_to_string (session->plugin,
1043 session->connect_addr,
1044 session->connect_alen) : "*");
1046 /* remove from session list */
1048 pos = session->plugin->sessions;
1049 while (pos != session)
1055 session->plugin->sessions = session->next;
1057 prev->next = session->next;
1058 session->plugin->env->session_end (session->plugin->env->cls,
1061 /* clean up state */
1062 if (session->transmit_handle != NULL)
1064 GNUNET_CONNECTION_notify_transmit_ready_cancel
1065 (session->transmit_handle);
1066 session->transmit_handle = NULL;
1068 while (NULL != (pm = session->pending_messages_head))
1071 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1073 pm->transmit_cont != NULL
1074 ? "Could not deliver message to `%4s'.\n"
1075 : "Could not deliver message to `%4s', notifying.\n",
1076 GNUNET_i2s (&session->target));
1078 GNUNET_STATISTICS_update (session->plugin->env->stats,
1079 gettext_noop ("# bytes currently in TCP buffers"),
1080 - (int64_t) pm->message_size,
1082 GNUNET_STATISTICS_update (session->plugin->env->stats,
1083 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1086 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1087 session->pending_messages_tail,
1089 if (NULL != pm->transmit_cont)
1090 pm->transmit_cont (pm->transmit_cont_cls,
1091 &session->target, GNUNET_SYSERR);
1094 GNUNET_break (session->client != NULL);
1095 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1097 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1098 if (session->client != NULL)
1099 GNUNET_SERVER_receive_done (session->client,
1102 else if (session->client != NULL)
1103 GNUNET_SERVER_client_drop (session->client);
1104 GNUNET_STATISTICS_update (session->plugin->env->stats,
1105 gettext_noop ("# TCP sessions active"),
1108 GNUNET_free_non_null (session->connect_addr);
1109 GNUNET_free (session);
1114 * Given two otherwise equivalent sessions, pick the better one.
1116 * @param s1 one session (also default)
1117 * @param s2 other session
1118 * @return "better" session (more active)
1120 static struct Session *
1121 select_better_session (struct Session *s1,
1128 if ( (s1->expecting_welcome == GNUNET_NO) &&
1129 (s2->expecting_welcome == GNUNET_YES) )
1131 if ( (s1->expecting_welcome == GNUNET_YES) &&
1132 (s2->expecting_welcome == GNUNET_NO) )
1134 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1136 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1138 if ( (GNUNET_YES == s1->inbound) &&
1139 (GNUNET_NO == s2->inbound) )
1141 if ( (GNUNET_NO == s1->inbound) &&
1142 (GNUNET_YES == s2->inbound) )
1149 * We learned about a peer (possibly behind NAT) so run the
1150 * gnunet-nat-client to send dummy ICMP responses.
1152 * @param plugin the plugin for this transport
1153 * @param sa the address of the peer (IPv4-only)
1156 run_gnunet_nat_client (struct Plugin *plugin,
1157 const struct sockaddr_in *sa)
1159 char inet4[INET_ADDRSTRLEN];
1160 char port_as_string[6];
1161 struct GNUNET_OS_Process *proc;
1163 if (plugin->internal_address == NULL)
1165 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1167 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1170 GNUNET_assert (sa->sin_family == AF_INET);
1171 if (NULL == inet_ntop (AF_INET,
1173 inet4, INET_ADDRSTRLEN))
1175 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1178 GNUNET_snprintf(port_as_string,
1179 sizeof (port_as_string),
1183 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1185 _("Running gnunet-nat-client %s %s %u\n"),
1186 plugin->internal_address,
1188 (unsigned int) plugin->adv_port);
1190 proc = GNUNET_OS_start_process (NULL,
1192 "gnunet-nat-client",
1193 "gnunet-nat-client",
1194 plugin->internal_address,
1200 /* we know that the gnunet-nat-client will terminate virtually
1202 GNUNET_OS_process_wait (proc);
1203 GNUNET_OS_process_close (proc);
1208 * Function that can be used by the transport service to transmit
1209 * a message using the plugin. Note that in the case of a
1210 * peer disconnecting, the continuation MUST be called
1211 * prior to the disconnect notification itself. This function
1212 * will be called with this peer's HELLO message to initiate
1213 * a fresh connection to another peer.
1215 * @param cls closure
1216 * @param target who should receive this message
1217 * @param msg the message to transmit
1218 * @param msgbuf_size number of bytes in 'msg'
1219 * @param priority how important is the message (most plugins will
1220 * ignore message priority and just FIFO)
1221 * @param timeout how long to wait at most for the transmission (does not
1222 * require plugins to discard the message after the timeout,
1223 * just advisory for the desired delay; most plugins will ignore
1225 * @param session which session must be used (or NULL for "any")
1226 * @param addr the address to use (can be NULL if the plugin
1227 * is "on its own" (i.e. re-use existing TCP connection))
1228 * @param addrlen length of the address in bytes
1229 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1230 * GNUNET_NO means the plugin may use any other address and
1231 * GNUNET_SYSERR means that only reliable existing
1232 * bi-directional connections should be used (regardless
1234 * @param cont continuation to call once the message has
1235 * been transmitted (or if the transport is ready
1236 * for the next transmission call; or if the
1237 * peer disconnected...); can be NULL
1238 * @param cont_cls closure for cont
1239 * @return number of bytes used (on the physical network, with overheads);
1240 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1241 * and does NOT mean that the message was not transmitted (DV and NAT)
1244 tcp_plugin_send (void *cls,
1245 const struct GNUNET_PeerIdentity *target,
1249 struct GNUNET_TIME_Relative timeout,
1250 struct Session *session,
1254 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1256 struct Plugin *plugin = cls;
1257 struct Session *cand_session;
1258 struct Session *next;
1259 struct PendingMessage *pm;
1260 struct GNUNET_CONNECTION_Handle *sa;
1264 struct sockaddr_in a4;
1265 struct sockaddr_in6 a6;
1266 const struct IPv4TcpAddress *t4;
1267 const struct IPv6TcpAddress *t6;
1268 unsigned int is_natd;
1270 GNUNET_STATISTICS_update (plugin->env->stats,
1271 gettext_noop ("# bytes TCP was asked to transmit"),
1274 /* FIXME: we could do this cheaper with a hash table
1275 where we could restrict the iteration to entries that match
1276 the target peer... */
1277 is_natd = GNUNET_NO;
1278 if (session == NULL)
1280 cand_session = NULL;
1281 next = plugin->sessions;
1282 while (NULL != (session = next))
1284 next = session->next;
1285 GNUNET_assert (session->client != NULL);
1286 if (0 != memcmp (target,
1288 sizeof (struct GNUNET_PeerIdentity)))
1290 if ( ( (GNUNET_SYSERR == force_address) &&
1291 (session->expecting_welcome == GNUNET_NO) ) ||
1292 (GNUNET_NO == force_address) )
1294 cand_session = select_better_session (cand_session,
1298 if (GNUNET_SYSERR == force_address)
1300 GNUNET_break (GNUNET_YES == force_address);
1306 if ( (addrlen != session->connect_alen) &&
1307 (session->is_nat == GNUNET_NO) )
1309 if ((0 != memcmp (session->connect_addr,
1311 addrlen)) && (session->is_nat == GNUNET_NO))
1313 cand_session = select_better_session (cand_session,
1316 session = cand_session;
1318 if ( (session == NULL) &&
1322 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1324 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1325 GNUNET_i2s (target));
1327 GNUNET_STATISTICS_update (plugin->env->stats,
1328 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1333 if (session == NULL)
1335 if (addrlen == sizeof (struct IPv6TcpAddress))
1339 memset (&a6, 0, sizeof (a6));
1340 #if HAVE_SOCKADDR_IN_SIN_LEN
1341 a6.sin6_len = sizeof (a6);
1343 a6.sin6_family = AF_INET6;
1344 a6.sin6_port = t6->t6_port;
1345 if (t6->t6_port == 0)
1346 is_natd = GNUNET_YES;
1347 memcpy (&a6.sin6_addr,
1349 sizeof (struct in6_addr));
1353 else if (addrlen == sizeof (struct IPv4TcpAddress))
1357 memset (&a4, 0, sizeof (a4));
1358 #if HAVE_SOCKADDR_IN_SIN_LEN
1359 a4.sin_len = sizeof (a4);
1361 a4.sin_family = AF_INET;
1362 a4.sin_port = t4->t_port;
1363 if (t4->t_port == 0)
1364 is_natd = GNUNET_YES;
1365 a4.sin_addr.s_addr = t4->ipv4_addr;
1371 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1373 _("Address of unexpected length: %u\n"),
1379 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1380 return -1; /* NAT client only works with IPv4 addresses */
1383 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1384 (is_natd == GNUNET_YES) &&
1385 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1386 &target->hashPubKey)) )
1389 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1391 _("Found valid IPv4 NAT address (creating session)!\n"));
1393 session = create_session (plugin,
1398 /* create new message entry */
1399 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1400 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1401 pm->msg = (const char*) &pm[1];
1402 memcpy (&pm[1], msg, msgbuf_size);
1403 /* FIXME: this memcpy can be up to 7% of our total run-time
1404 (for transport service) */
1405 pm->message_size = msgbuf_size;
1406 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1407 pm->transmit_cont = cont;
1408 pm->transmit_cont_cls = cont_cls;
1410 /* append pm to pending_messages list */
1411 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1412 session->pending_messages_tail,
1413 session->pending_messages_tail,
1416 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1417 &target->hashPubKey,
1419 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1423 "Created NAT WAIT connection to `%4s' at `%s'\n",
1424 GNUNET_i2s (target),
1425 GNUNET_a2s (sb, sbs));
1427 run_gnunet_nat_client (plugin, &a4);
1430 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1431 (is_natd == GNUNET_YES) &&
1432 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1433 &target->hashPubKey)) )
1435 /* Only do one NAT punch attempt per peer identity */
1438 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1442 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1444 "Failed to create connection to `%4s' at `%s'\n",
1445 GNUNET_i2s (target),
1446 GNUNET_a2s (sb, sbs));
1448 GNUNET_STATISTICS_update (plugin->env->stats,
1449 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1455 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1457 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1458 GNUNET_i2s (target),
1459 GNUNET_a2s (sb, sbs));
1461 session = create_session (plugin,
1463 GNUNET_SERVER_connect_socket (plugin->server,
1466 session->connect_addr = GNUNET_malloc (addrlen);
1467 memcpy (session->connect_addr,
1470 session->connect_alen = addrlen;
1472 GNUNET_assert (session != NULL);
1473 GNUNET_assert (session->client != NULL);
1474 GNUNET_STATISTICS_update (plugin->env->stats,
1475 gettext_noop ("# bytes currently in TCP buffers"),
1478 /* create new message entry */
1479 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1480 pm->msg = (const char*) &pm[1];
1481 memcpy (&pm[1], msg, msgbuf_size);
1482 pm->message_size = msgbuf_size;
1483 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1484 pm->transmit_cont = cont;
1485 pm->transmit_cont_cls = cont_cls;
1487 /* append pm to pending_messages list */
1488 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1489 session->pending_messages_tail,
1490 session->pending_messages_tail,
1493 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1495 "Asked to transmit %u bytes to `%s', added message to list.\n",
1497 GNUNET_i2s (target));
1499 process_pending_messages (session);
1505 * Function that can be called to force a disconnect from the
1506 * specified neighbour. This should also cancel all previously
1507 * scheduled transmissions. Obviously the transmission may have been
1508 * partially completed already, which is OK. The plugin is supposed
1509 * to close the connection (if applicable) and no longer call the
1510 * transmit continuation(s).
1512 * Finally, plugin MUST NOT call the services's receive function to
1513 * notify the service that the connection to the specified target was
1514 * closed after a getting this call.
1516 * @param cls closure
1517 * @param target peer for which the last transmission is
1521 tcp_plugin_disconnect (void *cls,
1522 const struct GNUNET_PeerIdentity *target)
1524 struct Plugin *plugin = cls;
1525 struct Session *session;
1526 struct Session *next;
1527 struct PendingMessage *pm;
1530 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1532 "Asked to cancel session with `%4s'\n",
1533 GNUNET_i2s (target));
1535 next = plugin->sessions;
1536 while (NULL != (session = next))
1538 next = session->next;
1539 if (0 != memcmp (target,
1541 sizeof (struct GNUNET_PeerIdentity)))
1543 pm = session->pending_messages_head;
1546 pm->transmit_cont = NULL;
1547 pm->transmit_cont_cls = NULL;
1550 disconnect_session (session);
1556 * Context for address to string conversion.
1558 struct PrettyPrinterContext
1561 * Function to call with the result.
1563 GNUNET_TRANSPORT_AddressStringCallback asc;
1566 * Clsoure for 'asc'.
1571 * Port to add after the IP address.
1578 * Append our port and forward the result.
1580 * @param cls the 'struct PrettyPrinterContext*'
1581 * @param hostname hostname part of the address
1584 append_port (void *cls, const char *hostname)
1586 struct PrettyPrinterContext *ppc = cls;
1589 if (hostname == NULL)
1591 ppc->asc (ppc->asc_cls, NULL);
1595 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1596 ppc->asc (ppc->asc_cls, ret);
1602 * Convert the transports address to a nice, human-readable
1605 * @param cls closure
1606 * @param type name of the transport that generated the address
1607 * @param addr one of the addresses of the host, NULL for the last address
1608 * the specific address format depends on the transport
1609 * @param addrlen length of the address
1610 * @param numeric should (IP) addresses be displayed in numeric form?
1611 * @param timeout after how long should we give up?
1612 * @param asc function to call on each string
1613 * @param asc_cls closure for asc
1616 tcp_plugin_address_pretty_printer (void *cls,
1621 struct GNUNET_TIME_Relative timeout,
1622 GNUNET_TRANSPORT_AddressStringCallback asc,
1625 struct Plugin *plugin = cls;
1626 struct PrettyPrinterContext *ppc;
1629 struct sockaddr_in a4;
1630 struct sockaddr_in6 a6;
1631 const struct IPv4TcpAddress *t4;
1632 const struct IPv6TcpAddress *t6;
1635 if (addrlen == sizeof (struct IPv6TcpAddress))
1638 memset (&a6, 0, sizeof (a6));
1639 a6.sin6_family = AF_INET6;
1640 a6.sin6_port = t6->t6_port;
1641 memcpy (&a6.sin6_addr,
1643 sizeof (struct in6_addr));
1644 port = ntohs (t6->t6_port);
1648 else if (addrlen == sizeof (struct IPv4TcpAddress))
1651 memset (&a4, 0, sizeof (a4));
1652 a4.sin_family = AF_INET;
1653 a4.sin_port = t4->t_port;
1654 a4.sin_addr.s_addr = t4->ipv4_addr;
1655 port = ntohs (t4->t_port);
1661 /* invalid address */
1662 GNUNET_break_op (0);
1663 asc (asc_cls, NULL);
1666 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1668 ppc->asc_cls = asc_cls;
1670 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1673 !numeric, timeout, &append_port, ppc);
1678 * Check if the given port is plausible (must be either our listen
1679 * port or our advertised port), or any port if we are behind NAT
1680 * and do not have a port open. If it is neither, we return
1683 * @param plugin global variables
1684 * @param in_port port number to check
1685 * @return GNUNET_OK if port is either open_port or adv_port
1688 check_port (struct Plugin *plugin,
1691 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1693 return GNUNET_SYSERR;
1698 * Function that will be called to check if a binary address for this
1699 * plugin is well-formed and corresponds to an address for THIS peer
1700 * (as per our configuration). Naturally, if absolutely necessary,
1701 * plugins can be a bit conservative in their answer, but in general
1702 * plugins should make sure that the address does not redirect
1703 * traffic to a 3rd party that might try to man-in-the-middle our
1706 * @param cls closure, our 'struct Plugin*'
1707 * @param addr pointer to the address
1708 * @param addrlen length of addr
1709 * @return GNUNET_OK if this is a plausible address for this peer
1710 * and transport, GNUNET_SYSERR if not
1713 tcp_plugin_check_address (void *cls,
1717 struct Plugin *plugin = cls;
1718 struct IPv4TcpAddress *v4;
1719 struct IPv6TcpAddress *v6;
1721 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1722 (addrlen != sizeof (struct IPv6TcpAddress)))
1724 GNUNET_break_op (0);
1725 return GNUNET_SYSERR;
1727 if (addrlen == sizeof (struct IPv4TcpAddress))
1729 v4 = (struct IPv4TcpAddress *) addr;
1731 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1734 check_port (plugin, ntohs (v4->t_port)))
1735 return GNUNET_SYSERR;
1737 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1738 return GNUNET_SYSERR;
1742 v6 = (struct IPv6TcpAddress *) addr;
1743 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1745 GNUNET_break_op (0);
1746 return GNUNET_SYSERR;
1749 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1752 check_port (plugin, ntohs (v6->t6_port)))
1753 return GNUNET_SYSERR;
1755 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1756 return GNUNET_SYSERR;
1763 * We've received a nat probe from this peer via TCP. Finish
1764 * creating the client session and resume sending of queued
1767 * @param cls closure
1768 * @param client identification of the client
1769 * @param message the actual message
1772 handle_tcp_nat_probe (void *cls,
1773 struct GNUNET_SERVER_Client *client,
1774 const struct GNUNET_MessageHeader *message)
1776 struct Plugin *plugin = cls;
1777 struct Session *session;
1778 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1781 struct IPv4TcpAddress *t4;
1782 struct IPv6TcpAddress *t6;
1783 const struct sockaddr_in *s4;
1784 const struct sockaddr_in6 *s6;
1787 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1789 "received tcp NAT probe\n");
1791 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1792 * a connection to this peer by running gnunet-nat-client. This peer
1793 * received the punch message and now wants us to use the new connection
1794 * as the default for that peer. Do so and then send a WELCOME message
1795 * so we can really be connected!
1797 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1802 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1803 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1804 &tcp_nat_probe->clientIdentity.hashPubKey);
1805 if (session == NULL)
1808 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1810 "Did NOT find session for NAT probe!\n");
1812 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1816 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1818 "Found session for NAT probe!\n");
1820 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1821 &tcp_nat_probe->clientIdentity.hashPubKey,
1822 session) == GNUNET_YES);
1824 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1827 GNUNET_free (session);
1828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1832 GNUNET_SERVER_client_keep (client);
1833 session->client = client;
1834 session->last_activity = GNUNET_TIME_absolute_get ();
1835 session->inbound = GNUNET_NO;
1838 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1840 "Found address `%s' for incoming connection\n",
1841 GNUNET_a2s (vaddr, alen));
1843 switch (((const struct sockaddr *)vaddr)->sa_family)
1847 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1848 t4->t_port = s4->sin_port;
1849 t4->ipv4_addr = s4->sin_addr.s_addr;
1850 session->connect_addr = t4;
1851 session->connect_alen = sizeof (struct IPv4TcpAddress);
1855 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1856 t6->t6_port = s6->sin6_port;
1857 memcpy (&t6->ipv6_addr,
1859 sizeof (struct in6_addr));
1860 session->connect_addr = t6;
1861 session->connect_alen = sizeof (struct IPv6TcpAddress);
1864 GNUNET_break_op (0);
1866 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1868 "Bad address for incoming connection!\n");
1870 GNUNET_free (vaddr);
1871 GNUNET_SERVER_client_drop (client);
1872 GNUNET_free (session);
1873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1876 GNUNET_free (vaddr);
1878 session->next = plugin->sessions;
1879 plugin->sessions = session;
1880 GNUNET_STATISTICS_update (plugin->env->stats,
1881 gettext_noop ("# TCP sessions active"),
1884 process_pending_messages (session);
1885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1890 * We've received a welcome from this peer via TCP. Possibly create a
1891 * fresh client record and send back our welcome.
1893 * @param cls closure
1894 * @param client identification of the client
1895 * @param message the actual message
1898 handle_tcp_welcome (void *cls,
1899 struct GNUNET_SERVER_Client *client,
1900 const struct GNUNET_MessageHeader *message)
1902 struct Plugin *plugin = cls;
1903 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1904 struct Session *session;
1907 struct IPv4TcpAddress *t4;
1908 struct IPv6TcpAddress *t6;
1909 const struct sockaddr_in *s4;
1910 const struct sockaddr_in6 *s6;
1913 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1915 "Received %s message from `%4s'.\n",
1917 GNUNET_i2s (&wm->clientIdentity));
1919 GNUNET_STATISTICS_update (plugin->env->stats,
1920 gettext_noop ("# TCP WELCOME messages received"),
1923 session = find_session_by_client (plugin, client);
1925 if (session == NULL)
1928 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1930 "Received %s message from a `%4s', creating new session\n",
1932 GNUNET_i2s (&wm->clientIdentity));
1934 GNUNET_SERVER_client_keep (client);
1935 session = create_session (plugin,
1936 &wm->clientIdentity,
1939 session->inbound = GNUNET_YES;
1941 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1944 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1946 "Found address `%s' for incoming connection\n",
1947 GNUNET_a2s (vaddr, alen));
1949 if (alen == sizeof (struct sockaddr_in))
1952 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1953 t4->t_port = s4->sin_port;
1954 t4->ipv4_addr = s4->sin_addr.s_addr;
1955 session->connect_addr = t4;
1956 session->connect_alen = sizeof (struct IPv4TcpAddress);
1958 else if (alen == sizeof (struct sockaddr_in6))
1961 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1962 t6->t6_port = s6->sin6_port;
1963 memcpy (&t6->ipv6_addr,
1965 sizeof (struct in6_addr));
1966 session->connect_addr = t6;
1967 session->connect_alen = sizeof (struct IPv6TcpAddress);
1970 GNUNET_free (vaddr);
1975 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1977 "Did not obtain TCP socket address for incoming connection\n");
1980 process_pending_messages (session);
1986 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1990 "Found address `%s' (already have session)\n",
1991 GNUNET_a2s (vaddr, alen));
1996 if (session->expecting_welcome != GNUNET_YES)
1998 GNUNET_break_op (0);
1999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2002 session->last_activity = GNUNET_TIME_absolute_get ();
2003 session->expecting_welcome = GNUNET_NO;
2004 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2009 * Task to signal the server that we can continue
2010 * receiving from the TCP client now.
2012 * @param cls the 'struct Session*'
2013 * @param tc task context (unused)
2016 delayed_done (void *cls,
2017 const struct GNUNET_SCHEDULER_TaskContext *tc)
2019 struct Session *session = cls;
2020 struct GNUNET_TIME_Relative delay;
2022 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2023 delay = session->plugin->env->receive (session->plugin->env->cls,
2029 if (delay.rel_value == 0)
2030 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2032 session->receive_delay_task =
2033 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2038 * We've received data for this peer via TCP. Unbox,
2039 * compute latency and forward.
2041 * @param cls closure
2042 * @param client identification of the client
2043 * @param message the actual message
2046 handle_tcp_data (void *cls,
2047 struct GNUNET_SERVER_Client *client,
2048 const struct GNUNET_MessageHeader *message)
2050 struct Plugin *plugin = cls;
2051 struct Session *session;
2052 struct GNUNET_TIME_Relative delay;
2055 type = ntohs (message->type);
2056 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2057 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2059 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2060 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2063 session = find_session_by_client (plugin, client);
2064 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2069 session->last_activity = GNUNET_TIME_absolute_get ();
2071 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2073 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2074 (unsigned int) ntohs (message->size),
2075 (unsigned int) ntohs (message->type),
2076 GNUNET_i2s (&session->target));
2078 GNUNET_STATISTICS_update (plugin->env->stats,
2079 gettext_noop ("# bytes received via TCP"),
2080 ntohs (message->size),
2082 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2083 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2084 distance[0].value = htonl (1);
2085 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2086 distance[1].value = htonl (0);
2087 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2088 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2091 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2092 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2093 if (delay.rel_value == 0)
2094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2096 session->receive_delay_task =
2097 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2102 * Functions with this signature are called whenever a peer
2103 * is disconnected on the network level.
2105 * @param cls closure
2106 * @param client identification of the client
2109 disconnect_notify (void *cls,
2110 struct GNUNET_SERVER_Client *client)
2112 struct Plugin *plugin = cls;
2113 struct Session *session;
2117 session = find_session_by_client (plugin, client);
2118 if (session == NULL)
2119 return; /* unknown, nothing to do */
2121 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2123 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2124 GNUNET_i2s (&session->target),
2125 (session->connect_addr != NULL) ?
2126 tcp_address_to_string (session->plugin,
2127 session->connect_addr,
2128 session->connect_alen) : "*");
2130 disconnect_session (session);
2135 * Add the IP of our network interface to the list of
2136 * our internal IP addresses.
2138 * @param cls the 'struct Plugin*'
2139 * @param name name of the interface
2140 * @param isDefault do we think this may be our default interface
2141 * @param addr address of the interface
2142 * @param addrlen number of bytes in addr
2143 * @return GNUNET_OK to continue iterating
2146 process_interfaces (void *cls,
2149 const struct sockaddr *addr, socklen_t addrlen)
2151 struct Plugin *plugin = cls;
2153 struct IPv4TcpAddress t4;
2154 struct IPv6TcpAddress t6;
2155 struct IPv4TcpAddress t4_nat;
2156 struct IPv6TcpAddress t6_nat;
2160 char buf[INET_ADDRSTRLEN];
2162 af = addr->sa_family;
2167 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2168 GNUNET_assert (NULL != inet_ntop(AF_INET,
2172 if ( (plugin->bind_address != NULL) &&
2173 (0 != strcmp(buf, plugin->bind_address)) )
2176 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2178 "Not notifying transport of address `%s' (redundant)\n",
2179 GNUNET_a2s (addr, addrlen));
2183 if ( (plugin->internal_address == NULL) &&
2185 plugin->internal_address = GNUNET_strdup (buf);
2186 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2187 if (plugin->behind_nat == GNUNET_YES)
2189 /* Also advertise as NAT (with port 0) */
2190 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2191 t4_nat.t_port = htons(0);
2194 t4.t_port = htons (plugin->adv_port);
2199 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2200 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2204 /* skip link local addresses */
2207 memcpy (&t6.ipv6_addr,
2208 &((struct sockaddr_in6 *) addr)->sin6_addr,
2209 sizeof (struct in6_addr));
2210 add_to_address_list (plugin,
2212 sizeof (struct in6_addr));
2213 if (plugin->behind_nat == GNUNET_YES)
2215 /* Also advertise as NAT (with port 0) */
2216 memcpy (&t6_nat.ipv6_addr,
2217 &((struct sockaddr_in6 *) addr)->sin6_addr,
2218 sizeof (struct in6_addr));
2219 t6_nat.t6_port = htons(0);
2222 t6.t6_port = htons (plugin->adv_port);
2231 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2233 "Found address `%s' (%s) len %d\n",
2234 GNUNET_a2s (addr, addrlen), name, args);
2236 plugin->env->notify_address (plugin->env->cls,
2238 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2240 if (arg_nat != NULL)
2242 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2244 _("Found address `%s' (%s) len %d\n"),
2245 GNUNET_a2s (addr, addrlen), name, args);
2246 plugin->env->notify_address (plugin->env->cls,
2248 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2256 * Function called by the resolver for each address obtained from DNS
2257 * for our own hostname. Add the addresses to the list of our
2258 * external IP addresses.
2260 * @param cls closure
2261 * @param addr one of the addresses of the host, NULL for the last address
2262 * @param addrlen length of the address
2265 process_hostname_ips (void *cls,
2266 const struct sockaddr *addr, socklen_t addrlen)
2268 struct Plugin *plugin = cls;
2272 plugin->hostname_dns = NULL;
2275 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2276 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2281 * We can now send a probe message, copy into buffer to really send.
2283 * @param cls closure, a struct TCPProbeContext
2284 * @param size max size to copy
2285 * @param buf buffer to copy message to
2286 * @return number of bytes copied into buf
2289 notify_send_probe (void *cls,
2293 struct TCPProbeContext *tcp_probe_ctx = cls;
2294 struct Plugin *plugin = tcp_probe_ctx->plugin;
2297 tcp_probe_ctx->transmit_handle = NULL;
2298 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2303 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2304 GNUNET_free(tcp_probe_ctx);
2307 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2308 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2309 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2310 tcp_probe_ctx->sock);
2311 ret = sizeof(tcp_probe_ctx->message);
2312 GNUNET_free(tcp_probe_ctx);
2318 * We have been notified that gnunet-nat-server has written something to stdout.
2319 * Handle the output, then reschedule this function to be called again once
2320 * more is available.
2322 * @param cls the plugin handle
2323 * @param tc the scheduling context
2326 tcp_plugin_server_read (void *cls,
2327 const struct GNUNET_SCHEDULER_TaskContext *tc)
2329 struct Plugin *plugin = cls;
2334 const char *port_start;
2335 struct sockaddr_in sin_addr;
2336 struct TCPProbeContext *tcp_probe_ctx;
2337 struct GNUNET_CONNECTION_Handle *sock;
2339 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2341 memset (mybuf, 0, sizeof(mybuf));
2342 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2350 "Finished reading from server stdout with code: %d\n",
2353 /* FIXME: consider process_wait here? */
2358 for (i = 0; i < sizeof(mybuf); i++)
2360 if (mybuf[i] == '\n')
2365 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2368 port_start = &mybuf[i + 1];
2372 /* construct socket address of sender */
2373 memset (&sin_addr, 0, sizeof (sin_addr));
2374 sin_addr.sin_family = AF_INET;
2375 #if HAVE_SOCKADDR_IN_SIN_LEN
2376 sin_addr.sin_len = sizeof (sin_addr);
2378 if ( (NULL == port_start) ||
2379 (1 != sscanf (port_start, "%d", &port)) ||
2380 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2382 /* should we restart gnunet-nat-server? */
2383 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2385 _("gnunet-nat-server generated malformed address `%s'\n"),
2387 plugin->server_read_task
2388 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2389 plugin->server_stdout_handle,
2390 &tcp_plugin_server_read,
2394 sin_addr.sin_port = htons((uint16_t) port);
2396 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2398 "gnunet-nat-server read: %s:%d\n",
2403 * We have received an ICMP response, ostensibly from a peer
2404 * that wants to connect to us! Send a message to establish a connection.
2406 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2407 (const struct sockaddr *)&sin_addr,
2411 /* failed for some odd reason (out of sockets?); ignore attempt */
2412 plugin->server_read_task =
2413 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2414 plugin->server_stdout_handle,
2415 &tcp_plugin_server_read,
2420 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2421 "Sending TCP probe message to `%s:%u'!\n",
2423 (unsigned int) port);
2424 /* FIXME: do we need to track these probe context objects so that
2425 we can clean them up on plugin unload? */
2427 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2428 tcp_probe_ctx->message.header.size
2429 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2430 tcp_probe_ctx->message.header.type
2431 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2432 memcpy (&tcp_probe_ctx->message.clientIdentity,
2433 plugin->env->my_identity,
2434 sizeof(struct GNUNET_PeerIdentity));
2435 tcp_probe_ctx->plugin = plugin;
2436 tcp_probe_ctx->sock = sock;
2437 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2440 tcp_probe_ctx->transmit_handle
2441 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2442 ntohs (tcp_probe_ctx->message.header.size),
2443 GNUNET_TIME_UNIT_FOREVER_REL,
2444 ¬ify_send_probe, tcp_probe_ctx);
2446 plugin->server_read_task =
2447 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2448 plugin->server_stdout_handle,
2449 &tcp_plugin_server_read,
2455 * Start the gnunet-nat-server process for users behind NAT.
2457 * @param plugin the transport plugin
2458 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2461 tcp_transport_start_nat_server (struct Plugin *plugin)
2463 if (plugin->internal_address == NULL)
2464 return GNUNET_SYSERR;
2465 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2468 if (plugin->server_stdout == NULL)
2469 return GNUNET_SYSERR;
2471 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2474 "gnunet-nat-server",
2475 plugin->internal_address);
2477 /* Start the server process */
2478 plugin->server_proc = GNUNET_OS_start_process (NULL,
2479 plugin->server_stdout,
2480 "gnunet-nat-server",
2481 "gnunet-nat-server",
2482 plugin->internal_address,
2484 if (plugin->server_proc == NULL)
2486 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2488 _("Failed to start %s\n"),
2489 "gnunet-nat-server");
2490 GNUNET_DISK_pipe_close (plugin->server_stdout);
2491 plugin->server_stdout = NULL;
2492 return GNUNET_SYSERR;
2494 /* Close the write end of the read pipe */
2495 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2496 GNUNET_DISK_PIPE_END_WRITE);
2497 plugin->server_stdout_handle
2498 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2499 GNUNET_DISK_PIPE_END_READ);
2500 plugin->server_read_task
2501 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2502 plugin->server_stdout_handle,
2503 &tcp_plugin_server_read,
2510 * Return the actual path to a file found in the current
2511 * PATH environment variable.
2513 * @param binary the name of the file to find
2514 * @return path to binary, NULL if not found
2517 get_path_from_PATH (const char *binary)
2525 p = getenv ("PATH");
2528 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2530 _("PATH environment variable is unset.\n"));
2533 path = GNUNET_strdup (p); /* because we write on it */
2534 buf = GNUNET_malloc (strlen (path) + 20);
2537 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2540 sprintf (buf, "%s/%s", pos, binary);
2541 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2548 sprintf (buf, "%s/%s", pos, binary);
2549 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2561 * Check whether the suid bit is set on a file.
2562 * Attempts to find the file using the current
2563 * PATH environment variable as a search path.
2565 * @param binary the name of the file to check
2566 * @return GNUNET_YES if the file is SUID,
2568 * GNUNET_SYSERR on error
2571 check_gnunet_nat_binary (const char *binary)
2573 struct stat statbuf;
2579 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2580 p = get_path_from_PATH (binaryexe);
2583 p = get_path_from_PATH (binary);
2587 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2589 _("Could not find binary `%s' in PATH!\n"),
2593 if (0 != STAT (p, &statbuf))
2595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2596 _("stat (%s) failed: %s\n"),
2600 return GNUNET_SYSERR;
2604 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2605 (statbuf.st_uid == 0) )
2609 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2610 if (INVALID_SOCKET == rawsock)
2612 DWORD err = GetLastError ();
2613 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2615 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2616 return GNUNET_NO; /* not running as administrator */
2618 closesocket (rawsock);
2625 * Our (external) hostname was resolved.
2627 * @param cls the 'struct Plugin'
2628 * @param addr NULL on error, otherwise result of DNS lookup
2629 * @param addrlen number of bytes in addr
2632 process_external_ip (void *cls,
2633 const struct sockaddr *addr,
2636 struct Plugin *plugin = cls;
2637 const struct sockaddr_in *s;
2638 struct IPv4TcpAddress t4;
2641 plugin->ext_dns = NULL;
2644 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2645 s = (const struct sockaddr_in *) addr;
2646 t4.ipv4_addr = s->sin_addr.s_addr;
2647 if ( (plugin->behind_nat == GNUNET_YES) &&
2648 (plugin->enable_nat_server == GNUNET_YES) )
2650 t4.t_port = htons(0);
2651 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2653 "Notifying transport of address %s:%d\n",
2654 plugin->external_address,
2659 t4.t_port = htons(plugin->adv_port);
2660 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2662 "Notifying transport of address %s:%d\n",
2663 plugin->external_address,
2664 (int) plugin->adv_port);
2666 add_to_address_list (plugin,
2668 sizeof (struct in_addr));
2669 plugin->env->notify_address (plugin->env->cls,
2672 GNUNET_TIME_UNIT_FOREVER_REL);
2677 * Entry point for the plugin.
2679 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2680 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2683 libgnunet_plugin_transport_tcp_init (void *cls)
2685 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2686 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2687 sizeof (struct WelcomeMessage)},
2688 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2689 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2692 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2693 struct GNUNET_TRANSPORT_PluginFunctions *api;
2694 struct Plugin *plugin;
2695 struct GNUNET_SERVICE_Context *service;
2696 unsigned long long aport;
2697 unsigned long long bport;
2701 int enable_nat_client;
2702 int enable_nat_server;
2704 char *internal_address;
2705 char *external_address;
2706 struct sockaddr_in in_addr;
2707 struct GNUNET_TIME_Relative idle_timeout;
2709 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2712 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2715 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2717 "ENABLE_NAT_CLIENT");
2718 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2720 "ENABLE_NAT_SERVER");
2721 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2725 if ( (GNUNET_YES == enable_nat_server) &&
2726 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2728 enable_nat_server = GNUNET_NO;
2729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2730 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2731 "gnunet-nat-server");
2734 if ( (GNUNET_YES == enable_nat_client) &&
2735 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2737 enable_nat_client = GNUNET_NO;
2738 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2739 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2740 "gnunet-nat-client");
2743 external_address = NULL;
2745 GNUNET_CONFIGURATION_have_value (env->cfg,
2747 "EXTERNAL_ADDRESS"))
2749 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2755 if ( (external_address != NULL) &&
2756 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2759 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2761 _("Malformed %s `%s' given in configuration!\n"),
2766 if ( (external_address == NULL) &&
2767 (nat_punched == GNUNET_YES) )
2769 nat_punched = GNUNET_NO;
2770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2771 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2772 "EXTERNAL_ADDRESS");
2775 if (GNUNET_YES == nat_punched)
2777 enable_nat_server = GNUNET_NO;
2778 enable_upnp = GNUNET_NO;
2781 internal_address = NULL;
2783 GNUNET_CONFIGURATION_have_value (env->cfg,
2785 "INTERNAL_ADDRESS"))
2787 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2793 if ( (internal_address != NULL) &&
2794 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2796 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2798 _("Malformed %s `%s' given in configuration!\n"),
2801 GNUNET_free_non_null(internal_address);
2802 GNUNET_free_non_null(external_address);
2808 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2814 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2820 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2822 _("Require valid port number for service `%s' in configuration!\n"),
2824 GNUNET_free_non_null(external_address);
2825 GNUNET_free_non_null(internal_address);
2836 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2837 if (service == NULL)
2839 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2841 _("Failed to start service.\n"));
2848 plugin = GNUNET_malloc (sizeof (struct Plugin));
2849 plugin->open_port = bport;
2850 plugin->adv_port = aport;
2851 plugin->external_address = external_address;
2852 plugin->internal_address = internal_address;
2853 plugin->behind_nat = behind_nat;
2854 plugin->nat_punched = nat_punched;
2855 plugin->enable_nat_client = enable_nat_client;
2856 plugin->enable_nat_server = enable_nat_server;
2857 plugin->enable_upnp = enable_upnp;
2859 plugin->lsock = NULL;
2860 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2862 api->send = &tcp_plugin_send;
2863 api->disconnect = &tcp_plugin_disconnect;
2864 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2865 api->check_address = &tcp_plugin_check_address;
2866 api->address_to_string = &tcp_address_to_string;
2867 plugin->service = service;
2868 if (service != NULL)
2870 plugin->server = GNUNET_SERVICE_get_server (service);
2875 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2880 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2882 _("Failed to find option %s in section %s!\n"),
2885 GNUNET_free_non_null(external_address);
2886 GNUNET_free_non_null(internal_address);
2890 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
2891 idle_timeout, GNUNET_YES);
2893 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2894 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2896 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2898 plugin->handlers[i].callback_cls = plugin;
2899 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2900 GNUNET_SERVER_disconnect_notify (plugin->server,
2903 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2905 if ( (plugin->behind_nat == GNUNET_YES) &&
2906 (plugin->enable_nat_server == GNUNET_YES) &&
2907 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
2909 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2911 _("Failed to start %s required for NAT in %s!\n"),
2914 GNUNET_free_non_null(external_address);
2915 GNUNET_free_non_null(internal_address);
2916 if (service != NULL)
2917 GNUNET_SERVICE_stop (service);
2919 GNUNET_SERVER_destroy (plugin->server);
2924 if (enable_nat_client == GNUNET_YES)
2926 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
2927 GNUNET_assert (plugin->nat_wait_conns != NULL);
2931 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2933 _("TCP transport listening on port %llu\n"),
2936 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2938 _("TCP transport not listening on any port (client only)\n"));
2940 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2942 _("TCP transport advertises itself as being on port %llu\n"),
2945 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2948 &plugin->bind_address))
2950 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2952 _("Binding TCP plugin to specific address: `%s'\n"),
2953 plugin->bind_address);
2956 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
2958 HOSTNAME_RESOLVE_TIMEOUT,
2959 &process_hostname_ips,
2962 if (plugin->external_address != NULL)
2964 plugin->ext_dns = GNUNET_RESOLVER_ip_get (env->cfg,
2965 plugin->external_address,
2967 GNUNET_TIME_UNIT_MINUTES,
2968 &process_external_ip,
2976 * Exit point from the plugin.
2979 libgnunet_plugin_transport_tcp_done (void *cls)
2981 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2982 struct Plugin *plugin = api->cls;
2983 struct Session *session;
2984 struct LocalAddrList *lal;
2985 struct TCPProbeContext *tcp_probe;
2987 if (plugin->ext_dns != NULL)
2989 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
2990 plugin->ext_dns = NULL;
2992 while (NULL != (session = plugin->sessions))
2993 disconnect_session (session);
2994 if (NULL != plugin->hostname_dns)
2996 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2997 plugin->hostname_dns = NULL;
2999 if (plugin->service != NULL)
3000 GNUNET_SERVICE_stop (plugin->service);
3002 GNUNET_SERVER_destroy (plugin->server);
3003 GNUNET_free (plugin->handlers);
3004 while (NULL != (lal = plugin->lal_head))
3006 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3009 if (lal->nat != NULL)
3010 GNUNET_NAT_unregister (lal->nat);
3011 GNUNET_free_non_null (lal->external_nat_address);
3014 while (NULL != (tcp_probe = plugin->probe_head))
3016 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3019 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3020 GNUNET_free (tcp_probe);
3023 if ((plugin->behind_nat == GNUNET_YES) &&
3024 (plugin->enable_nat_server == GNUNET_YES))
3026 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3027 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3028 GNUNET_OS_process_wait (plugin->server_proc);
3029 GNUNET_OS_process_close (plugin->server_proc);
3030 plugin->server_proc = NULL;
3032 GNUNET_free_non_null(plugin->bind_address);
3033 GNUNET_free (plugin);
3038 /* end of plugin_transport_tcp.c */