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 t6.t6_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 (sa, salen,
645 &nat_port_map_callback,
651 * Check if the given address is in the list of 'local' addresses.
653 * @param plugin the plugin
654 * @param arg the address, either an IPv4 or an IPv6 IP address
655 * @param arg_size number of bytes in arg
656 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
659 check_local_addr (struct Plugin *plugin,
663 struct LocalAddrList *lal;
665 lal = plugin->lal_head;
668 if ( (lal->size == arg_size) &&
669 (0 == memcmp (&lal[1], arg, arg_size)) )
673 return GNUNET_SYSERR;
678 * Check if the given address is in the list of 'mapped' addresses.
680 * @param plugin the plugin
681 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
682 * @param arg_size number of bytes in arg
683 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
686 check_mapped_addr (struct Plugin *plugin,
690 struct LocalAddrList *lal;
692 lal = plugin->lal_head;
695 if ( (lal->ena_size == arg_size) &&
696 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
700 return GNUNET_SYSERR;
705 * Function called for a quick conversion of the binary address to
706 * a numeric address. Note that the caller must not free the
707 * address and that the next call to this function is allowed
708 * to override the address again.
710 * @param cls closure ('struct Plugin*')
711 * @param addr binary address
712 * @param addrlen length of the address
713 * @return string representing the same address
716 tcp_address_to_string (void *cls,
720 static char rbuf[INET6_ADDRSTRLEN + 12];
721 char buf[INET6_ADDRSTRLEN];
725 const struct IPv4TcpAddress *t4;
726 const struct IPv6TcpAddress *t6;
730 if (addrlen == sizeof (struct IPv6TcpAddress))
734 port = ntohs (t6->t6_port);
735 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
738 else if (addrlen == sizeof (struct IPv4TcpAddress))
742 port = ntohs (t4->t_port);
743 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
748 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
750 _("Unexpected address length: %u bytes\n"),
751 (unsigned int) addrlen);
755 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
757 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
760 GNUNET_snprintf (rbuf,
762 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
770 * Find the session handle for the given client.
772 * @param plugin the plugin
773 * @param client which client to find the session handle for
774 * @return NULL if no matching session exists
776 static struct Session *
777 find_session_by_client (struct Plugin *plugin,
778 const struct GNUNET_SERVER_Client *client)
782 ret = plugin->sessions;
783 while ((ret != NULL) && (client != ret->client))
790 * Create a new session. Also queues a welcome message.
792 * @param plugin the plugin
793 * @param target peer to connect to
794 * @param client client to use
795 * @param is_nat this a NAT session, we should wait for a client to
796 * connect to us from an address, then assign that to
798 * @return new session object
800 static struct Session *
801 create_session (struct Plugin *plugin,
802 const struct GNUNET_PeerIdentity *target,
803 struct GNUNET_SERVER_Client *client,
807 struct PendingMessage *pm;
808 struct WelcomeMessage welcome;
810 if (is_nat != GNUNET_YES)
811 GNUNET_assert (client != NULL);
813 GNUNET_assert (client == NULL);
815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
817 "Creating new session for peer `%4s'\n",
818 GNUNET_i2s (target));
820 ret = GNUNET_malloc (sizeof (struct Session));
821 ret->last_activity = GNUNET_TIME_absolute_get ();
822 ret->plugin = plugin;
823 ret->is_nat = is_nat;
824 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
826 ret->next = plugin->sessions;
827 plugin->sessions = ret;
829 ret->client = client;
830 ret->target = *target;
831 ret->expecting_welcome = GNUNET_YES;
832 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
833 pm->msg = (const char*) &pm[1];
834 pm->message_size = sizeof (struct WelcomeMessage);
835 welcome.header.size = htons (sizeof (struct WelcomeMessage));
836 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
837 welcome.clientIdentity = *plugin->env->my_identity;
838 memcpy (&pm[1], &welcome, sizeof (welcome));
839 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
840 GNUNET_STATISTICS_update (plugin->env->stats,
841 gettext_noop ("# bytes currently in TCP buffers"),
844 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
845 ret->pending_messages_tail,
847 if (is_nat != GNUNET_YES)
848 GNUNET_STATISTICS_update (plugin->env->stats,
849 gettext_noop ("# TCP sessions active"),
857 * If we have pending messages, ask the server to
858 * transmit them (schedule the respective tasks, etc.)
860 * @param session for which session should we do this
862 static void process_pending_messages (struct Session *session);
866 * Function called to notify a client about the socket
867 * being ready to queue more data. "buf" will be
868 * NULL and "size" zero if the socket was closed for
869 * writing in the meantime.
872 * @param size number of bytes available in buf
873 * @param buf where the callee should write the message
874 * @return number of bytes written to buf
877 do_transmit (void *cls, size_t size, void *buf)
879 struct Session *session = cls;
880 struct GNUNET_PeerIdentity pid;
881 struct Plugin *plugin;
882 struct PendingMessage *pos;
883 struct PendingMessage *hd;
884 struct PendingMessage *tl;
885 struct GNUNET_TIME_Absolute now;
889 session->transmit_handle = NULL;
890 plugin = session->plugin;
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
896 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
897 GNUNET_i2s (&session->target));
899 /* timeout; cancel all messages that have already expired */
903 now = GNUNET_TIME_absolute_get ();
904 while ( (NULL != (pos = session->pending_messages_head)) &&
905 (pos->timeout.abs_value <= now.abs_value) )
907 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
908 session->pending_messages_tail,
911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
913 "Failed to transmit %u byte message to `%4s'.\n",
915 GNUNET_i2s (&session->target));
917 ret += pos->message_size;
918 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
920 /* do this call before callbacks (so that if callbacks destroy
921 session, they have a chance to cancel actions done by this
923 process_pending_messages (session);
924 pid = session->target;
925 /* no do callbacks and do not use session again since
926 the callbacks may abort the session */
927 while (NULL != (pos = hd))
929 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
930 if (pos->transmit_cont != NULL)
931 pos->transmit_cont (pos->transmit_cont_cls,
932 &pid, GNUNET_SYSERR);
935 GNUNET_STATISTICS_update (plugin->env->stats,
936 gettext_noop ("# bytes currently in TCP buffers"),
939 GNUNET_STATISTICS_update (plugin->env->stats,
940 gettext_noop ("# bytes discarded by TCP (timeout)"),
945 /* copy all pending messages that would fit */
950 while (NULL != (pos = session->pending_messages_head))
952 if (ret + pos->message_size > size)
954 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
955 session->pending_messages_tail,
957 GNUNET_assert (size >= pos->message_size);
958 /* FIXME: this memcpy can be up to 7% of our total runtime */
959 memcpy (cbuf, pos->msg, pos->message_size);
960 cbuf += pos->message_size;
961 ret += pos->message_size;
962 size -= pos->message_size;
963 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
965 /* schedule 'continuation' before callbacks so that callbacks that
966 cancel everything don't cause us to use a session that no longer
968 process_pending_messages (session);
969 session->last_activity = GNUNET_TIME_absolute_get ();
970 pid = session->target;
971 /* we'll now call callbacks that may cancel the session; hence
972 we should not use 'session' after this point */
973 while (NULL != (pos = hd))
975 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
976 if (pos->transmit_cont != NULL)
977 pos->transmit_cont (pos->transmit_cont_cls,
981 GNUNET_assert (hd == NULL);
982 GNUNET_assert (tl == NULL);
984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
986 "Transmitting %u bytes\n",
989 GNUNET_STATISTICS_update (plugin->env->stats,
990 gettext_noop ("# bytes currently in TCP buffers"),
993 GNUNET_STATISTICS_update (plugin->env->stats,
994 gettext_noop ("# bytes transmitted via TCP"),
1002 * If we have pending messages, ask the server to
1003 * transmit them (schedule the respective tasks, etc.)
1005 * @param session for which session should we do this
1008 process_pending_messages (struct Session *session)
1010 struct PendingMessage *pm;
1012 GNUNET_assert (session->client != NULL);
1013 if (session->transmit_handle != NULL)
1015 if (NULL == (pm = session->pending_messages_head))
1018 session->transmit_handle
1019 = GNUNET_SERVER_notify_transmit_ready (session->client,
1021 GNUNET_TIME_absolute_get_remaining
1023 &do_transmit, session);
1028 * Functions with this signature are called whenever we need
1029 * to close a session due to a disconnect or failure to
1030 * establish a connection.
1032 * @param session session to close down
1035 disconnect_session (struct Session *session)
1037 struct Session *prev;
1038 struct Session *pos;
1039 struct PendingMessage *pm;
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1044 "Disconnecting from `%4s' at %s.\n",
1045 GNUNET_i2s (&session->target),
1046 (session->connect_addr != NULL) ?
1047 tcp_address_to_string (session->plugin,
1048 session->connect_addr,
1049 session->connect_alen) : "*");
1051 /* remove from session list */
1053 pos = session->plugin->sessions;
1054 while (pos != session)
1060 session->plugin->sessions = session->next;
1062 prev->next = session->next;
1064 /* clean up state */
1065 if (session->transmit_handle != NULL)
1067 GNUNET_CONNECTION_notify_transmit_ready_cancel
1068 (session->transmit_handle);
1069 session->transmit_handle = NULL;
1071 while (NULL != (pm = session->pending_messages_head))
1074 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1076 pm->transmit_cont != NULL
1077 ? "Could not deliver message to `%4s'.\n"
1078 : "Could not deliver message to `%4s', notifying.\n",
1079 GNUNET_i2s (&session->target));
1081 GNUNET_STATISTICS_update (session->plugin->env->stats,
1082 gettext_noop ("# bytes currently in TCP buffers"),
1083 - (int64_t) pm->message_size,
1085 GNUNET_STATISTICS_update (session->plugin->env->stats,
1086 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1089 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1090 session->pending_messages_tail,
1092 if (NULL != pm->transmit_cont)
1093 pm->transmit_cont (pm->transmit_cont_cls,
1094 &session->target, GNUNET_SYSERR);
1097 GNUNET_break (session->client != NULL);
1098 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1100 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1101 if (session->client != NULL)
1102 GNUNET_SERVER_receive_done (session->client,
1105 else if (session->client != NULL)
1106 GNUNET_SERVER_client_drop (session->client);
1107 GNUNET_STATISTICS_update (session->plugin->env->stats,
1108 gettext_noop ("# TCP sessions active"),
1111 GNUNET_free_non_null (session->connect_addr);
1113 session->plugin->env->session_end (session->plugin->env->cls,
1117 GNUNET_free (session);
1122 * Given two otherwise equivalent sessions, pick the better one.
1124 * @param s1 one session (also default)
1125 * @param s2 other session
1126 * @return "better" session (more active)
1128 static struct Session *
1129 select_better_session (struct Session *s1,
1136 if ( (s1->expecting_welcome == GNUNET_NO) &&
1137 (s2->expecting_welcome == GNUNET_YES) )
1139 if ( (s1->expecting_welcome == GNUNET_YES) &&
1140 (s2->expecting_welcome == GNUNET_NO) )
1142 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1144 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1146 if ( (GNUNET_YES == s1->inbound) &&
1147 (GNUNET_NO == s2->inbound) )
1149 if ( (GNUNET_NO == s1->inbound) &&
1150 (GNUNET_YES == s2->inbound) )
1157 * We learned about a peer (possibly behind NAT) so run the
1158 * gnunet-nat-client to send dummy ICMP responses.
1160 * @param plugin the plugin for this transport
1161 * @param sa the address of the peer (IPv4-only)
1164 run_gnunet_nat_client (struct Plugin *plugin,
1165 const struct sockaddr_in *sa)
1167 char inet4[INET_ADDRSTRLEN];
1168 char port_as_string[6];
1169 struct GNUNET_OS_Process *proc;
1171 if (plugin->internal_address == NULL)
1173 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1175 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1178 GNUNET_assert (sa->sin_family == AF_INET);
1179 if (NULL == inet_ntop (AF_INET,
1181 inet4, INET_ADDRSTRLEN))
1183 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1186 GNUNET_snprintf(port_as_string,
1187 sizeof (port_as_string),
1191 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1193 _("Running gnunet-nat-client %s %s %u\n"),
1194 plugin->internal_address,
1196 (unsigned int) plugin->adv_port);
1198 proc = GNUNET_OS_start_process (NULL,
1200 "gnunet-nat-client",
1201 "gnunet-nat-client",
1202 plugin->internal_address,
1208 /* we know that the gnunet-nat-client will terminate virtually
1210 GNUNET_OS_process_wait (proc);
1211 GNUNET_OS_process_close (proc);
1216 * Function that can be used by the transport service to transmit
1217 * a message using the plugin. Note that in the case of a
1218 * peer disconnecting, the continuation MUST be called
1219 * prior to the disconnect notification itself. This function
1220 * will be called with this peer's HELLO message to initiate
1221 * a fresh connection to another peer.
1223 * @param cls closure
1224 * @param target who should receive this message
1225 * @param msg the message to transmit
1226 * @param msgbuf_size number of bytes in 'msg'
1227 * @param priority how important is the message (most plugins will
1228 * ignore message priority and just FIFO)
1229 * @param timeout how long to wait at most for the transmission (does not
1230 * require plugins to discard the message after the timeout,
1231 * just advisory for the desired delay; most plugins will ignore
1233 * @param session which session must be used (or NULL for "any")
1234 * @param addr the address to use (can be NULL if the plugin
1235 * is "on its own" (i.e. re-use existing TCP connection))
1236 * @param addrlen length of the address in bytes
1237 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1238 * GNUNET_NO means the plugin may use any other address and
1239 * GNUNET_SYSERR means that only reliable existing
1240 * bi-directional connections should be used (regardless
1242 * @param cont continuation to call once the message has
1243 * been transmitted (or if the transport is ready
1244 * for the next transmission call; or if the
1245 * peer disconnected...); can be NULL
1246 * @param cont_cls closure for cont
1247 * @return number of bytes used (on the physical network, with overheads);
1248 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1249 * and does NOT mean that the message was not transmitted (DV and NAT)
1252 tcp_plugin_send (void *cls,
1253 const struct GNUNET_PeerIdentity *target,
1257 struct GNUNET_TIME_Relative timeout,
1258 struct Session *session,
1262 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1264 struct Plugin *plugin = cls;
1265 struct Session *cand_session;
1266 struct Session *next;
1267 struct PendingMessage *pm;
1268 struct GNUNET_CONNECTION_Handle *sa;
1272 struct sockaddr_in a4;
1273 struct sockaddr_in6 a6;
1274 const struct IPv4TcpAddress *t4;
1275 const struct IPv6TcpAddress *t6;
1276 unsigned int is_natd;
1278 GNUNET_STATISTICS_update (plugin->env->stats,
1279 gettext_noop ("# bytes TCP was asked to transmit"),
1282 /* FIXME: we could do this cheaper with a hash table
1283 where we could restrict the iteration to entries that match
1284 the target peer... */
1285 is_natd = GNUNET_NO;
1286 if (session == NULL)
1288 cand_session = NULL;
1289 next = plugin->sessions;
1290 while (NULL != (session = next))
1292 next = session->next;
1293 GNUNET_assert (session->client != NULL);
1294 if (0 != memcmp (target,
1296 sizeof (struct GNUNET_PeerIdentity)))
1298 if ( ( (GNUNET_SYSERR == force_address) &&
1299 (session->expecting_welcome == GNUNET_NO) ) ||
1300 (GNUNET_NO == force_address) )
1302 cand_session = select_better_session (cand_session,
1306 if (GNUNET_SYSERR == force_address)
1308 GNUNET_break (GNUNET_YES == force_address);
1314 if ( (addrlen != session->connect_alen) &&
1315 (session->is_nat == GNUNET_NO) )
1317 if ((0 != memcmp (session->connect_addr,
1319 addrlen)) && (session->is_nat == GNUNET_NO))
1321 cand_session = select_better_session (cand_session,
1324 session = cand_session;
1326 if ( (session == NULL) &&
1330 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1332 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1333 GNUNET_i2s (target));
1335 GNUNET_STATISTICS_update (plugin->env->stats,
1336 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1341 if (session == NULL)
1343 if (addrlen == sizeof (struct IPv6TcpAddress))
1347 memset (&a6, 0, sizeof (a6));
1348 #if HAVE_SOCKADDR_IN_SIN_LEN
1349 a6.sin6_len = sizeof (a6);
1351 a6.sin6_family = AF_INET6;
1352 a6.sin6_port = t6->t6_port;
1353 if (t6->t6_port == 0)
1354 is_natd = GNUNET_YES;
1355 memcpy (&a6.sin6_addr,
1357 sizeof (struct in6_addr));
1361 else if (addrlen == sizeof (struct IPv4TcpAddress))
1365 memset (&a4, 0, sizeof (a4));
1366 #if HAVE_SOCKADDR_IN_SIN_LEN
1367 a4.sin_len = sizeof (a4);
1369 a4.sin_family = AF_INET;
1370 a4.sin_port = t4->t_port;
1371 if (t4->t_port == 0)
1372 is_natd = GNUNET_YES;
1373 a4.sin_addr.s_addr = t4->ipv4_addr;
1379 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1381 _("Address of unexpected length: %u\n"),
1387 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1388 return -1; /* NAT client only works with IPv4 addresses */
1391 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1392 (is_natd == GNUNET_YES) &&
1393 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1394 &target->hashPubKey)) )
1397 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1399 _("Found valid IPv4 NAT address (creating session)!\n"));
1401 session = create_session (plugin,
1406 /* create new message entry */
1407 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1408 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1409 pm->msg = (const char*) &pm[1];
1410 memcpy (&pm[1], msg, msgbuf_size);
1411 /* FIXME: this memcpy can be up to 7% of our total run-time
1412 (for transport service) */
1413 pm->message_size = msgbuf_size;
1414 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1415 pm->transmit_cont = cont;
1416 pm->transmit_cont_cls = cont_cls;
1418 /* append pm to pending_messages list */
1419 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1420 session->pending_messages_tail,
1421 session->pending_messages_tail,
1424 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1425 &target->hashPubKey,
1427 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1431 "Created NAT WAIT connection to `%4s' at `%s'\n",
1432 GNUNET_i2s (target),
1433 GNUNET_a2s (sb, sbs));
1435 run_gnunet_nat_client (plugin, &a4);
1438 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1439 (is_natd == GNUNET_YES) &&
1440 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1441 &target->hashPubKey)) )
1443 /* Only do one NAT punch attempt per peer identity */
1446 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1450 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1452 "Failed to create connection to `%4s' at `%s'\n",
1453 GNUNET_i2s (target),
1454 GNUNET_a2s (sb, sbs));
1456 GNUNET_STATISTICS_update (plugin->env->stats,
1457 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1465 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1466 GNUNET_i2s (target),
1467 GNUNET_a2s (sb, sbs));
1469 session = create_session (plugin,
1471 GNUNET_SERVER_connect_socket (plugin->server,
1474 session->connect_addr = GNUNET_malloc (addrlen);
1475 memcpy (session->connect_addr,
1478 session->connect_alen = addrlen;
1480 GNUNET_assert (session != NULL);
1481 GNUNET_assert (session->client != NULL);
1482 GNUNET_STATISTICS_update (plugin->env->stats,
1483 gettext_noop ("# bytes currently in TCP buffers"),
1486 /* create new message entry */
1487 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1488 pm->msg = (const char*) &pm[1];
1489 memcpy (&pm[1], msg, msgbuf_size);
1490 pm->message_size = msgbuf_size;
1491 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1492 pm->transmit_cont = cont;
1493 pm->transmit_cont_cls = cont_cls;
1495 /* append pm to pending_messages list */
1496 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1497 session->pending_messages_tail,
1498 session->pending_messages_tail,
1501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1503 "Asked to transmit %u bytes to `%s', added message to list.\n",
1505 GNUNET_i2s (target));
1507 process_pending_messages (session);
1513 * Function that can be called to force a disconnect from the
1514 * specified neighbour. This should also cancel all previously
1515 * scheduled transmissions. Obviously the transmission may have been
1516 * partially completed already, which is OK. The plugin is supposed
1517 * to close the connection (if applicable) and no longer call the
1518 * transmit continuation(s).
1520 * Finally, plugin MUST NOT call the services's receive function to
1521 * notify the service that the connection to the specified target was
1522 * closed after a getting this call.
1524 * @param cls closure
1525 * @param target peer for which the last transmission is
1529 tcp_plugin_disconnect (void *cls,
1530 const struct GNUNET_PeerIdentity *target)
1532 struct Plugin *plugin = cls;
1533 struct Session *session;
1534 struct Session *next;
1535 struct PendingMessage *pm;
1538 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1540 "Asked to cancel session with `%4s'\n",
1541 GNUNET_i2s (target));
1543 next = plugin->sessions;
1544 while (NULL != (session = next))
1546 next = session->next;
1547 if (0 != memcmp (target,
1549 sizeof (struct GNUNET_PeerIdentity)))
1551 pm = session->pending_messages_head;
1554 pm->transmit_cont = NULL;
1555 pm->transmit_cont_cls = NULL;
1558 GNUNET_STATISTICS_update (session->plugin->env->stats,
1559 gettext_noop ("# transport-service disconnect requests for TCP"),
1562 disconnect_session (session);
1568 * Context for address to string conversion.
1570 struct PrettyPrinterContext
1573 * Function to call with the result.
1575 GNUNET_TRANSPORT_AddressStringCallback asc;
1578 * Clsoure for 'asc'.
1583 * Port to add after the IP address.
1590 * Append our port and forward the result.
1592 * @param cls the 'struct PrettyPrinterContext*'
1593 * @param hostname hostname part of the address
1596 append_port (void *cls, const char *hostname)
1598 struct PrettyPrinterContext *ppc = cls;
1601 if (hostname == NULL)
1603 ppc->asc (ppc->asc_cls, NULL);
1607 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1608 ppc->asc (ppc->asc_cls, ret);
1614 * Convert the transports address to a nice, human-readable
1617 * @param cls closure
1618 * @param type name of the transport that generated the address
1619 * @param addr one of the addresses of the host, NULL for the last address
1620 * the specific address format depends on the transport
1621 * @param addrlen length of the address
1622 * @param numeric should (IP) addresses be displayed in numeric form?
1623 * @param timeout after how long should we give up?
1624 * @param asc function to call on each string
1625 * @param asc_cls closure for asc
1628 tcp_plugin_address_pretty_printer (void *cls,
1633 struct GNUNET_TIME_Relative timeout,
1634 GNUNET_TRANSPORT_AddressStringCallback asc,
1637 struct Plugin *plugin = cls;
1638 struct PrettyPrinterContext *ppc;
1641 struct sockaddr_in a4;
1642 struct sockaddr_in6 a6;
1643 const struct IPv4TcpAddress *t4;
1644 const struct IPv6TcpAddress *t6;
1647 if (addrlen == sizeof (struct IPv6TcpAddress))
1650 memset (&a6, 0, sizeof (a6));
1651 a6.sin6_family = AF_INET6;
1652 a6.sin6_port = t6->t6_port;
1653 memcpy (&a6.sin6_addr,
1655 sizeof (struct in6_addr));
1656 port = ntohs (t6->t6_port);
1660 else if (addrlen == sizeof (struct IPv4TcpAddress))
1663 memset (&a4, 0, sizeof (a4));
1664 a4.sin_family = AF_INET;
1665 a4.sin_port = t4->t_port;
1666 a4.sin_addr.s_addr = t4->ipv4_addr;
1667 port = ntohs (t4->t_port);
1673 /* invalid address */
1674 GNUNET_break_op (0);
1675 asc (asc_cls, NULL);
1678 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1680 ppc->asc_cls = asc_cls;
1682 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1685 !numeric, timeout, &append_port, ppc);
1690 * Check if the given port is plausible (must be either our listen
1691 * port or our advertised port), or any port if we are behind NAT
1692 * and do not have a port open. If it is neither, we return
1695 * @param plugin global variables
1696 * @param in_port port number to check
1697 * @return GNUNET_OK if port is either open_port or adv_port
1700 check_port (struct Plugin *plugin,
1703 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1705 return GNUNET_SYSERR;
1710 * Function that will be called to check if a binary address for this
1711 * plugin is well-formed and corresponds to an address for THIS peer
1712 * (as per our configuration). Naturally, if absolutely necessary,
1713 * plugins can be a bit conservative in their answer, but in general
1714 * plugins should make sure that the address does not redirect
1715 * traffic to a 3rd party that might try to man-in-the-middle our
1718 * @param cls closure, our 'struct Plugin*'
1719 * @param addr pointer to the address
1720 * @param addrlen length of addr
1721 * @return GNUNET_OK if this is a plausible address for this peer
1722 * and transport, GNUNET_SYSERR if not
1725 tcp_plugin_check_address (void *cls,
1729 struct Plugin *plugin = cls;
1730 struct IPv4TcpAddress *v4;
1731 struct IPv6TcpAddress *v6;
1733 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1734 (addrlen != sizeof (struct IPv6TcpAddress)))
1736 GNUNET_break_op (0);
1737 return GNUNET_SYSERR;
1739 if (addrlen == sizeof (struct IPv4TcpAddress))
1741 v4 = (struct IPv4TcpAddress *) addr;
1743 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1746 check_port (plugin, ntohs (v4->t_port)))
1747 return GNUNET_SYSERR;
1749 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1750 return GNUNET_SYSERR;
1754 v6 = (struct IPv6TcpAddress *) addr;
1755 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1757 GNUNET_break_op (0);
1758 return GNUNET_SYSERR;
1761 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1764 check_port (plugin, ntohs (v6->t6_port)))
1765 return GNUNET_SYSERR;
1767 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1768 return GNUNET_SYSERR;
1775 * We've received a nat probe from this peer via TCP. Finish
1776 * creating the client session and resume sending of queued
1779 * @param cls closure
1780 * @param client identification of the client
1781 * @param message the actual message
1784 handle_tcp_nat_probe (void *cls,
1785 struct GNUNET_SERVER_Client *client,
1786 const struct GNUNET_MessageHeader *message)
1788 struct Plugin *plugin = cls;
1789 struct Session *session;
1790 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1793 struct IPv4TcpAddress *t4;
1794 struct IPv6TcpAddress *t6;
1795 const struct sockaddr_in *s4;
1796 const struct sockaddr_in6 *s6;
1799 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1801 "received tcp NAT probe\n");
1803 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1804 * a connection to this peer by running gnunet-nat-client. This peer
1805 * received the punch message and now wants us to use the new connection
1806 * as the default for that peer. Do so and then send a WELCOME message
1807 * so we can really be connected!
1809 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1814 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1815 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1816 &tcp_nat_probe->clientIdentity.hashPubKey);
1817 if (session == NULL)
1820 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1822 "Did NOT find session for NAT probe!\n");
1824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1828 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1830 "Found session for NAT probe!\n");
1832 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1833 &tcp_nat_probe->clientIdentity.hashPubKey,
1834 session) == GNUNET_YES);
1836 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1839 GNUNET_free (session);
1840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1844 GNUNET_SERVER_client_keep (client);
1845 session->client = client;
1846 session->last_activity = GNUNET_TIME_absolute_get ();
1847 session->inbound = GNUNET_NO;
1850 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1852 "Found address `%s' for incoming connection\n",
1853 GNUNET_a2s (vaddr, alen));
1855 switch (((const struct sockaddr *)vaddr)->sa_family)
1859 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1860 t4->t_port = s4->sin_port;
1861 t4->ipv4_addr = s4->sin_addr.s_addr;
1862 session->connect_addr = t4;
1863 session->connect_alen = sizeof (struct IPv4TcpAddress);
1867 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1868 t6->t6_port = s6->sin6_port;
1869 memcpy (&t6->ipv6_addr,
1871 sizeof (struct in6_addr));
1872 session->connect_addr = t6;
1873 session->connect_alen = sizeof (struct IPv6TcpAddress);
1876 GNUNET_break_op (0);
1878 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1880 "Bad address for incoming connection!\n");
1882 GNUNET_free (vaddr);
1883 GNUNET_SERVER_client_drop (client);
1884 GNUNET_free (session);
1885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1888 GNUNET_free (vaddr);
1890 session->next = plugin->sessions;
1891 plugin->sessions = session;
1892 GNUNET_STATISTICS_update (plugin->env->stats,
1893 gettext_noop ("# TCP sessions active"),
1896 process_pending_messages (session);
1897 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1902 * We've received a welcome from this peer via TCP. Possibly create a
1903 * fresh client record and send back our welcome.
1905 * @param cls closure
1906 * @param client identification of the client
1907 * @param message the actual message
1910 handle_tcp_welcome (void *cls,
1911 struct GNUNET_SERVER_Client *client,
1912 const struct GNUNET_MessageHeader *message)
1914 struct Plugin *plugin = cls;
1915 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1916 struct Session *session;
1919 struct IPv4TcpAddress *t4;
1920 struct IPv6TcpAddress *t6;
1921 const struct sockaddr_in *s4;
1922 const struct sockaddr_in6 *s6;
1925 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1927 "Received %s message from `%4s'.\n",
1929 GNUNET_i2s (&wm->clientIdentity));
1931 GNUNET_STATISTICS_update (plugin->env->stats,
1932 gettext_noop ("# TCP WELCOME messages received"),
1935 session = find_session_by_client (plugin, client);
1937 if (session == NULL)
1940 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1942 "Received %s message from a `%4s', creating new session\n",
1944 GNUNET_i2s (&wm->clientIdentity));
1946 GNUNET_SERVER_client_keep (client);
1947 session = create_session (plugin,
1948 &wm->clientIdentity,
1951 session->inbound = GNUNET_YES;
1953 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1956 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1958 "Found address `%s' for incoming connection\n",
1959 GNUNET_a2s (vaddr, alen));
1961 if (alen == sizeof (struct sockaddr_in))
1964 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1965 t4->t_port = s4->sin_port;
1966 t4->ipv4_addr = s4->sin_addr.s_addr;
1967 session->connect_addr = t4;
1968 session->connect_alen = sizeof (struct IPv4TcpAddress);
1970 else if (alen == sizeof (struct sockaddr_in6))
1973 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1974 t6->t6_port = s6->sin6_port;
1975 memcpy (&t6->ipv6_addr,
1977 sizeof (struct in6_addr));
1978 session->connect_addr = t6;
1979 session->connect_alen = sizeof (struct IPv6TcpAddress);
1982 GNUNET_free (vaddr);
1987 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1989 "Did not obtain TCP socket address for incoming connection\n");
1992 process_pending_messages (session);
1998 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2000 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2002 "Found address `%s' (already have session)\n",
2003 GNUNET_a2s (vaddr, alen));
2004 GNUNET_free (vaddr);
2009 if (session->expecting_welcome != GNUNET_YES)
2011 GNUNET_break_op (0);
2012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2015 session->last_activity = GNUNET_TIME_absolute_get ();
2016 session->expecting_welcome = GNUNET_NO;
2017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2022 * Task to signal the server that we can continue
2023 * receiving from the TCP client now.
2025 * @param cls the 'struct Session*'
2026 * @param tc task context (unused)
2029 delayed_done (void *cls,
2030 const struct GNUNET_SCHEDULER_TaskContext *tc)
2032 struct Session *session = cls;
2033 struct GNUNET_TIME_Relative delay;
2035 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2036 delay = session->plugin->env->receive (session->plugin->env->cls,
2042 if (delay.rel_value == 0)
2043 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2045 session->receive_delay_task =
2046 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2051 * We've received data for this peer via TCP. Unbox,
2052 * compute latency and forward.
2054 * @param cls closure
2055 * @param client identification of the client
2056 * @param message the actual message
2059 handle_tcp_data (void *cls,
2060 struct GNUNET_SERVER_Client *client,
2061 const struct GNUNET_MessageHeader *message)
2063 struct Plugin *plugin = cls;
2064 struct Session *session;
2065 struct GNUNET_TIME_Relative delay;
2068 type = ntohs (message->type);
2069 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2070 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2072 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2073 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2076 session = find_session_by_client (plugin, client);
2077 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2082 session->last_activity = GNUNET_TIME_absolute_get ();
2084 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2086 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2087 (unsigned int) ntohs (message->size),
2088 (unsigned int) ntohs (message->type),
2089 GNUNET_i2s (&session->target));
2091 GNUNET_STATISTICS_update (plugin->env->stats,
2092 gettext_noop ("# bytes received via TCP"),
2093 ntohs (message->size),
2095 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2096 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2097 distance[0].value = htonl (1);
2098 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2099 distance[1].value = htonl (0);
2100 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2101 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2104 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2105 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2106 if (delay.rel_value == 0)
2108 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2113 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2115 "Throttling receiving from `%s' for %llu ms\n",
2116 GNUNET_i2s (&session->target),
2117 (unsigned long long) delay.rel_value);
2119 GNUNET_SERVER_disable_receive_done_warning (client);
2120 session->receive_delay_task =
2121 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2127 * Functions with this signature are called whenever a peer
2128 * is disconnected on the network level.
2130 * @param cls closure
2131 * @param client identification of the client
2134 disconnect_notify (void *cls,
2135 struct GNUNET_SERVER_Client *client)
2137 struct Plugin *plugin = cls;
2138 struct Session *session;
2142 session = find_session_by_client (plugin, client);
2143 if (session == NULL)
2144 return; /* unknown, nothing to do */
2146 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2148 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2149 GNUNET_i2s (&session->target),
2150 (session->connect_addr != NULL) ?
2151 tcp_address_to_string (session->plugin,
2152 session->connect_addr,
2153 session->connect_alen) : "*");
2155 GNUNET_STATISTICS_update (session->plugin->env->stats,
2156 gettext_noop ("# network-level TCP disconnect events"),
2159 disconnect_session (session);
2163 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
2166 int local = GNUNET_NO;
2167 int af = addr->sa_family;
2172 uint32_t netmask = 0x7F000000;
2173 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
2174 res = (address >> 24) ^ (netmask >> 24);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2181 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2187 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
2188 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2194 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2203 * Add the IP of our network interface to the list of
2204 * our internal IP addresses.
2206 * @param cls the 'struct Plugin*'
2207 * @param name name of the interface
2208 * @param isDefault do we think this may be our default interface
2209 * @param addr address of the interface
2210 * @param addrlen number of bytes in addr
2211 * @return GNUNET_OK to continue iterating
2214 process_interfaces (void *cls,
2217 const struct sockaddr *addr, socklen_t addrlen)
2219 struct Plugin *plugin = cls;
2221 struct IPv4TcpAddress t4;
2222 struct IPv6TcpAddress t6;
2223 struct IPv4TcpAddress t4_nat;
2224 struct IPv6TcpAddress t6_nat;
2228 char buf[INET6_ADDRSTRLEN];
2230 af = addr->sa_family;
2233 if (plugin->use_localaddresses == GNUNET_NO)
2235 if (GNUNET_YES == check_localaddress (addr, addrlen))
2238 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2240 "Not notifying transport of address `%s' (local address)\n",
2241 GNUNET_a2s (addr, addrlen));
2250 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2251 GNUNET_assert (NULL != inet_ntop(AF_INET,
2255 if ( (plugin->bind_address != NULL) &&
2256 (0 != strcmp(buf, plugin->bind_address)) )
2259 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2261 "Not notifying transport of address `%s' (does not match bind address)\n",
2262 GNUNET_a2s (addr, addrlen));
2266 if ( (plugin->internal_address == NULL) &&
2268 plugin->internal_address = GNUNET_strdup (buf);
2269 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2270 if (plugin->behind_nat == GNUNET_YES)
2272 /* Also advertise as NAT (with port 0) */
2273 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2274 t4_nat.t_port = htons(0);
2277 t4.t_port = htons (plugin->adv_port);
2282 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2283 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2287 /* skip link local addresses */
2290 memcpy (&t6.ipv6_addr,
2291 &((struct sockaddr_in6 *) addr)->sin6_addr,
2292 sizeof (struct in6_addr));
2294 /* check bind address */
2295 GNUNET_assert (NULL != inet_ntop(AF_INET6,
2300 if ( (plugin->bind_address != NULL) &&
2301 (0 != strcmp(buf, plugin->bind_address)) )
2304 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2306 "Not notifying transport of address `%s' (does not match bind address)\n",
2307 GNUNET_a2s (addr, addrlen));
2312 add_to_address_list (plugin,
2314 sizeof (struct in6_addr));
2315 if (plugin->behind_nat == GNUNET_YES)
2317 /* Also advertise as NAT (with port 0) */
2318 memcpy (&t6_nat.ipv6_addr,
2319 &((struct sockaddr_in6 *) addr)->sin6_addr,
2320 sizeof (struct in6_addr));
2321 t6_nat.t6_port = htons(0);
2324 t6.t6_port = htons (plugin->adv_port);
2332 if (plugin->adv_port != 0)
2335 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2337 "Found address `%s' (%s) len %d\n",
2338 GNUNET_a2s (addr, addrlen), name, args);
2340 plugin->env->notify_address (plugin->env->cls,
2342 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2345 if (arg_nat != NULL)
2347 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2349 _("Found address `%s' (%s) len %d\n"),
2350 GNUNET_a2s (addr, addrlen), name, args);
2351 plugin->env->notify_address (plugin->env->cls,
2353 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2361 * Function called by the resolver for each address obtained from DNS
2362 * for our own hostname. Add the addresses to the list of our
2363 * external IP addresses.
2365 * @param cls closure
2366 * @param addr one of the addresses of the host, NULL for the last address
2367 * @param addrlen length of the address
2370 process_hostname_ips (void *cls,
2371 const struct sockaddr *addr, socklen_t addrlen)
2373 struct Plugin *plugin = cls;
2377 plugin->hostname_dns = NULL;
2380 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2381 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2386 * We can now send a probe message, copy into buffer to really send.
2388 * @param cls closure, a struct TCPProbeContext
2389 * @param size max size to copy
2390 * @param buf buffer to copy message to
2391 * @return number of bytes copied into buf
2394 notify_send_probe (void *cls,
2398 struct TCPProbeContext *tcp_probe_ctx = cls;
2399 struct Plugin *plugin = tcp_probe_ctx->plugin;
2402 tcp_probe_ctx->transmit_handle = NULL;
2403 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2408 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2409 GNUNET_free(tcp_probe_ctx);
2412 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2413 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2414 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2415 tcp_probe_ctx->sock);
2416 ret = sizeof(tcp_probe_ctx->message);
2417 GNUNET_free(tcp_probe_ctx);
2423 * We have been notified that gnunet-nat-server has written something to stdout.
2424 * Handle the output, then reschedule this function to be called again once
2425 * more is available.
2427 * @param cls the plugin handle
2428 * @param tc the scheduling context
2431 tcp_plugin_server_read (void *cls,
2432 const struct GNUNET_SCHEDULER_TaskContext *tc)
2434 struct Plugin *plugin = cls;
2439 const char *port_start;
2440 struct sockaddr_in sin_addr;
2441 struct TCPProbeContext *tcp_probe_ctx;
2442 struct GNUNET_CONNECTION_Handle *sock;
2444 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2446 memset (mybuf, 0, sizeof(mybuf));
2447 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2453 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2455 "Finished reading from server stdout with code: %d\n",
2458 /* FIXME: consider process_wait here? */
2463 for (i = 0; i < sizeof(mybuf); i++)
2465 if (mybuf[i] == '\n')
2470 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2473 port_start = &mybuf[i + 1];
2477 /* construct socket address of sender */
2478 memset (&sin_addr, 0, sizeof (sin_addr));
2479 sin_addr.sin_family = AF_INET;
2480 #if HAVE_SOCKADDR_IN_SIN_LEN
2481 sin_addr.sin_len = sizeof (sin_addr);
2483 if ( (NULL == port_start) ||
2484 (1 != sscanf (port_start, "%d", &port)) ||
2485 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2487 /* should we restart gnunet-nat-server? */
2488 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2490 _("gnunet-nat-server generated malformed address `%s'\n"),
2492 plugin->server_read_task
2493 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2494 plugin->server_stdout_handle,
2495 &tcp_plugin_server_read,
2499 sin_addr.sin_port = htons((uint16_t) port);
2501 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2503 "gnunet-nat-server read: %s:%d\n",
2508 * We have received an ICMP response, ostensibly from a peer
2509 * that wants to connect to us! Send a message to establish a connection.
2511 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2512 (const struct sockaddr *)&sin_addr,
2516 /* failed for some odd reason (out of sockets?); ignore attempt */
2517 plugin->server_read_task =
2518 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2519 plugin->server_stdout_handle,
2520 &tcp_plugin_server_read,
2525 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2526 "Sending TCP probe message to `%s:%u'!\n",
2528 (unsigned int) port);
2529 /* FIXME: do we need to track these probe context objects so that
2530 we can clean them up on plugin unload? */
2532 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2533 tcp_probe_ctx->message.header.size
2534 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2535 tcp_probe_ctx->message.header.type
2536 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2537 memcpy (&tcp_probe_ctx->message.clientIdentity,
2538 plugin->env->my_identity,
2539 sizeof(struct GNUNET_PeerIdentity));
2540 tcp_probe_ctx->plugin = plugin;
2541 tcp_probe_ctx->sock = sock;
2542 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2545 tcp_probe_ctx->transmit_handle
2546 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2547 ntohs (tcp_probe_ctx->message.header.size),
2548 GNUNET_TIME_UNIT_FOREVER_REL,
2549 ¬ify_send_probe, tcp_probe_ctx);
2551 plugin->server_read_task =
2552 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2553 plugin->server_stdout_handle,
2554 &tcp_plugin_server_read,
2560 * Start the gnunet-nat-server process for users behind NAT.
2562 * @param plugin the transport plugin
2563 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2566 tcp_transport_start_nat_server (struct Plugin *plugin)
2568 if (plugin->internal_address == NULL)
2569 return GNUNET_SYSERR;
2570 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2573 if (plugin->server_stdout == NULL)
2574 return GNUNET_SYSERR;
2576 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2578 "Starting %s %s\n", "gnunet-nat-server", plugin->internal_address);
2580 /* Start the server process */
2581 plugin->server_proc = GNUNET_OS_start_process (NULL,
2582 plugin->server_stdout,
2583 "gnunet-nat-server",
2584 "gnunet-nat-server",
2585 plugin->internal_address,
2587 if (plugin->server_proc == NULL)
2589 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2591 _("Failed to start %s\n"),
2592 "gnunet-nat-server");
2593 GNUNET_DISK_pipe_close (plugin->server_stdout);
2594 plugin->server_stdout = NULL;
2595 return GNUNET_SYSERR;
2597 /* Close the write end of the read pipe */
2598 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2599 GNUNET_DISK_PIPE_END_WRITE);
2600 plugin->server_stdout_handle
2601 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2602 GNUNET_DISK_PIPE_END_READ);
2603 plugin->server_read_task
2604 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2605 plugin->server_stdout_handle,
2606 &tcp_plugin_server_read,
2613 * Return the actual path to a file found in the current
2614 * PATH environment variable.
2616 * @param binary the name of the file to find
2617 * @return path to binary, NULL if not found
2620 get_path_from_PATH (const char *binary)
2628 p = getenv ("PATH");
2631 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2633 _("PATH environment variable is unset.\n"));
2636 path = GNUNET_strdup (p); /* because we write on it */
2637 buf = GNUNET_malloc (strlen (path) + 20);
2640 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2643 sprintf (buf, "%s/%s", pos, binary);
2644 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2651 sprintf (buf, "%s/%s", pos, binary);
2652 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2664 * Check whether the suid bit is set on a file.
2665 * Attempts to find the file using the current
2666 * PATH environment variable as a search path.
2668 * @param binary the name of the file to check
2669 * @return GNUNET_YES if the file is SUID,
2671 * GNUNET_SYSERR on error
2674 check_gnunet_nat_binary (const char *binary)
2676 struct stat statbuf;
2682 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2683 p = get_path_from_PATH (binaryexe);
2686 p = get_path_from_PATH (binary);
2690 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2692 _("Could not find binary `%s' in PATH!\n"),
2696 if (0 != STAT (p, &statbuf))
2698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2699 _("stat (%s) failed: %s\n"),
2703 return GNUNET_SYSERR;
2707 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2708 (statbuf.st_uid == 0) )
2712 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2713 if (INVALID_SOCKET == rawsock)
2715 DWORD err = GetLastError ();
2716 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2718 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2719 return GNUNET_NO; /* not running as administrator */
2721 closesocket (rawsock);
2728 * Our (external) hostname was resolved.
2730 * @param cls the 'struct Plugin'
2731 * @param addr NULL on error, otherwise result of DNS lookup
2732 * @param addrlen number of bytes in addr
2735 process_external_ip (void *cls,
2736 const struct sockaddr *addr,
2739 struct Plugin *plugin = cls;
2740 const struct sockaddr_in *s;
2741 struct IPv4TcpAddress t4;
2742 char buf[INET_ADDRSTRLEN];
2744 plugin->ext_dns = NULL;
2747 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2748 s = (const struct sockaddr_in *) addr;
2749 t4.ipv4_addr = s->sin_addr.s_addr;
2750 if ( (plugin->behind_nat == GNUNET_YES) &&
2751 (plugin->enable_nat_server == GNUNET_YES) )
2753 t4.t_port = htons(0);
2754 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2756 "Notifying transport of address %s:%d\n",
2757 plugin->external_address,
2762 t4.t_port = htons(plugin->adv_port);
2763 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2765 "Notifying transport of address %s:%d\n",
2766 plugin->external_address,
2767 (int) plugin->adv_port);
2770 if ((plugin->bind_address != NULL) && (plugin->behind_nat == GNUNET_NO))
2772 GNUNET_assert (NULL != inet_ntop(AF_INET,
2776 if (0 != strcmp (plugin->bind_address, buf))
2778 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2780 "NAT is not enabled and specific bind address `%s' differs from external address `%s'! Not notifying about external address `%s'\n",
2781 plugin->bind_address,
2782 plugin->external_address,
2783 plugin->external_address);
2788 add_to_address_list (plugin,
2790 sizeof (struct in_addr));
2792 plugin->env->notify_address (plugin->env->cls,
2795 GNUNET_TIME_UNIT_FOREVER_REL);
2800 * Entry point for the plugin.
2802 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2803 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2806 libgnunet_plugin_transport_tcp_init (void *cls)
2808 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2809 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2810 sizeof (struct WelcomeMessage)},
2811 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2812 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2815 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2816 struct GNUNET_TRANSPORT_PluginFunctions *api;
2817 struct Plugin *plugin;
2818 struct GNUNET_SERVICE_Context *service;
2819 unsigned long long aport;
2820 unsigned long long bport;
2824 int enable_nat_client;
2825 int enable_nat_server;
2827 int use_localaddresses;
2828 char *internal_address;
2829 char *external_address;
2831 struct sockaddr_in in_addr;
2832 struct GNUNET_TIME_Relative idle_timeout;
2834 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2837 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2840 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2842 "ENABLE_NAT_CLIENT");
2843 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2845 "ENABLE_NAT_SERVER");
2846 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2850 if ( (GNUNET_YES == enable_nat_server) &&
2851 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2853 enable_nat_server = GNUNET_NO;
2854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2855 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2856 "gnunet-nat-server");
2859 if ( (GNUNET_YES == enable_nat_client) &&
2860 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2862 enable_nat_client = GNUNET_NO;
2863 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2864 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2865 "gnunet-nat-client");
2868 external_address = NULL;
2870 GNUNET_CONFIGURATION_have_value (env->cfg,
2872 "EXTERNAL_ADDRESS"))
2874 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2880 if ( (external_address != NULL) &&
2881 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2884 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2886 _("Malformed %s `%s' given in configuration!\n"),
2891 if ( (external_address == NULL) &&
2892 (nat_punched == GNUNET_YES) )
2894 nat_punched = GNUNET_NO;
2895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2896 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2897 "EXTERNAL_ADDRESS");
2900 if (GNUNET_YES == nat_punched)
2902 enable_nat_server = GNUNET_NO;
2903 enable_upnp = GNUNET_NO;
2906 bind_address = NULL;
2907 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2912 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2914 _("Binding TCP plugin to specific address: `%s'\n"),
2918 internal_address = NULL;
2920 GNUNET_CONFIGURATION_have_value (env->cfg,
2922 "INTERNAL_ADDRESS"))
2924 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2930 if ( (internal_address != NULL) &&
2931 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2933 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2935 _("Malformed %s `%s' given in configuration!\n"),
2938 GNUNET_free_non_null(internal_address);
2939 GNUNET_free_non_null(external_address);
2943 if ((bind_address != NULL) && (internal_address != NULL))
2945 if (0 != strcmp(internal_address, bind_address ))
2947 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2949 "Specific bind address `%s' and internal address `%s' must not differ, forcing internal address to bind address!\n",
2950 bind_address, internal_address);
2951 GNUNET_free (internal_address);
2952 internal_address = bind_address;
2953 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2954 "tcp","New internal address `%s'\n", internal_address);
2960 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2966 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2972 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2974 _("Require valid port number for service `%s' in configuration!\n"),
2976 GNUNET_free_non_null(external_address);
2977 GNUNET_free_non_null(internal_address);
2981 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2984 if (use_localaddresses == GNUNET_SYSERR)
2985 use_localaddresses = GNUNET_NO;
2994 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2995 if (service == NULL)
2997 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2999 _("Failed to start service.\n"));
3006 plugin = GNUNET_malloc (sizeof (struct Plugin));
3007 plugin->open_port = bport;
3008 plugin->adv_port = aport;
3009 plugin->bind_address = bind_address;
3010 plugin->external_address = external_address;
3011 plugin->internal_address = internal_address;
3012 plugin->behind_nat = behind_nat;
3013 plugin->nat_punched = nat_punched;
3014 plugin->enable_nat_client = enable_nat_client;
3015 plugin->enable_nat_server = enable_nat_server;
3016 plugin->enable_upnp = enable_upnp;
3017 plugin->use_localaddresses = use_localaddresses;
3019 plugin->lsock = NULL;
3020 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3022 api->send = &tcp_plugin_send;
3023 api->disconnect = &tcp_plugin_disconnect;
3024 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3025 api->check_address = &tcp_plugin_check_address;
3026 api->address_to_string = &tcp_address_to_string;
3027 plugin->service = service;
3028 if (service != NULL)
3030 plugin->server = GNUNET_SERVICE_get_server (service);
3035 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3040 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3042 _("Failed to find option %s in section %s!\n"),
3045 GNUNET_free_non_null(external_address);
3046 GNUNET_free_non_null(internal_address);
3050 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
3051 idle_timeout, GNUNET_YES);
3053 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3054 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
3056 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
3058 plugin->handlers[i].callback_cls = plugin;
3059 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
3060 GNUNET_SERVER_disconnect_notify (plugin->server,
3063 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3065 if ( (plugin->behind_nat == GNUNET_YES) &&
3066 (plugin->enable_nat_server == GNUNET_YES) &&
3067 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
3069 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3071 _("Failed to start %s required for NAT in %s!\n"),
3074 GNUNET_free_non_null(external_address);
3075 GNUNET_free_non_null(internal_address);
3076 if (service != NULL)
3077 GNUNET_SERVICE_stop (service);
3079 GNUNET_SERVER_destroy (plugin->server);
3084 if (enable_nat_client == GNUNET_YES)
3086 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
3087 GNUNET_assert (plugin->nat_wait_conns != NULL);
3091 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3093 _("TCP transport listening on port %llu\n"),
3096 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3098 _("TCP transport not listening on any port (client only)\n"));
3100 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3102 _("TCP transport advertises itself as being on port %llu\n"),
3105 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
3107 HOSTNAME_RESOLVE_TIMEOUT,
3108 &process_hostname_ips,
3111 if (plugin->external_address != NULL)
3113 plugin->ext_dns = GNUNET_RESOLVER_ip_get (env->cfg,
3114 plugin->external_address,
3116 GNUNET_TIME_UNIT_MINUTES,
3117 &process_external_ip,
3125 * Exit point from the plugin.
3128 libgnunet_plugin_transport_tcp_done (void *cls)
3130 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3131 struct Plugin *plugin = api->cls;
3132 struct Session *session;
3133 struct LocalAddrList *lal;
3134 struct TCPProbeContext *tcp_probe;
3136 if (plugin->ext_dns != NULL)
3138 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
3139 plugin->ext_dns = NULL;
3141 while (NULL != (session = plugin->sessions))
3142 disconnect_session (session);
3143 if (NULL != plugin->hostname_dns)
3145 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
3146 plugin->hostname_dns = NULL;
3148 if (plugin->service != NULL)
3149 GNUNET_SERVICE_stop (plugin->service);
3151 GNUNET_SERVER_destroy (plugin->server);
3152 GNUNET_free (plugin->handlers);
3153 while (NULL != (lal = plugin->lal_head))
3155 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3158 if (lal->nat != NULL)
3159 GNUNET_NAT_unregister (lal->nat);
3160 GNUNET_free_non_null (lal->external_nat_address);
3163 while (NULL != (tcp_probe = plugin->probe_head))
3165 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3168 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3169 GNUNET_free (tcp_probe);
3172 if ((plugin->behind_nat == GNUNET_YES) &&
3173 (plugin->enable_nat_server == GNUNET_YES))
3175 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3176 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3177 GNUNET_OS_process_wait (plugin->server_proc);
3178 GNUNET_OS_process_close (plugin->server_proc);
3179 plugin->server_proc = NULL;
3181 GNUNET_free_non_null(plugin->bind_address);
3182 GNUNET_free (plugin);
3187 /* end of plugin_transport_tcp.c */