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 * How many more TCP sessions are we allowed to open right now?
469 unsigned long long max_connections;
472 * ID of task used to update our addresses when one expires.
474 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
477 * Port that we are actually listening on.
482 * Port that the user said we would have visible to the
488 * Is this transport configured to be behind a NAT?
493 * Has the NAT been punched?
498 * Is this transport configured to allow connections to NAT'd peers?
500 int enable_nat_client;
503 * Should we run the gnunet-nat-server?
505 int enable_nat_server;
508 * Are we allowed to try UPnP/PMP for NAT traversal?
516 * Function to check if an inbound connection is acceptable.
517 * Mostly used to limit the total number of open connections
520 * @param cls the 'struct Plugin'
521 * @param ucred credentials, if available, otherwise NULL
522 * @param addr address
523 * @param addrlen length of address
524 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
525 * for unknown address family (will be denied).
528 plugin_tcp_access_check (void *cls,
529 const struct GNUNET_CONNECTION_Credentials *ucred,
530 const struct sockaddr *addr,
533 struct Plugin *plugin = cls;
535 if (0 == plugin->max_connections)
537 plugin->max_connections--;
543 * Our external IP address/port mapping has changed.
545 * @param cls closure, the 'struct LocalAddrList'
546 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
547 * the previous (now invalid) one
548 * @param addr either the previous or the new public IP address
549 * @param addrlen actual lenght of the address
552 nat_port_map_callback (void *cls,
554 const struct sockaddr *addr,
557 struct LocalAddrList *lal = cls;
558 struct Plugin *plugin = lal->plugin;
560 struct IPv4TcpAddress t4;
561 struct IPv6TcpAddress t6;
565 /* convert 'addr' to our internal format */
566 af = addr->sa_family;
570 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
571 t4.t_port = ((struct sockaddr_in *) addr)->sin_port;
576 memcpy (&t6.ipv6_addr,
577 &((struct sockaddr_in6 *) addr)->sin6_addr,
578 sizeof (struct in6_addr));
579 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
588 /* modify our published address list */
589 if (GNUNET_YES == add_remove)
591 plugin->env->notify_address (plugin->env->cls,
593 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
594 GNUNET_free_non_null (lal->external_nat_address);
595 lal->external_nat_address = GNUNET_memdup (arg, args);
596 lal->ena_size = args;
600 plugin->env->notify_address (plugin->env->cls,
602 arg, args, GNUNET_TIME_UNIT_ZERO);
603 GNUNET_free_non_null (lal->external_nat_address);
610 * Add the given address to the list of 'local' addresses, thereby
611 * making it a 'legal' address for this peer to have.
613 * @param plugin the plugin
614 * @param arg the address, either an IPv4 or an IPv6 IP address
615 * @param arg_size number of bytes in arg
618 add_to_address_list (struct Plugin *plugin,
622 struct LocalAddrList *lal;
623 struct sockaddr_in v4;
624 struct sockaddr_in6 v6;
625 const struct sockaddr *sa;
628 lal = plugin->lal_head;
631 if ( (lal->size == arg_size) &&
632 (0 == memcmp (&lal[1], arg, arg_size)) )
636 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
637 lal->plugin = plugin;
638 lal->size = arg_size;
639 memcpy (&lal[1], arg, arg_size);
640 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
643 if (plugin->open_port == 0)
644 return; /* we're not listening at all... */
645 if (arg_size == sizeof (struct in_addr))
647 memset (&v4, 0, sizeof (v4));
648 v4.sin_family = AF_INET;
649 v4.sin_port = htons (plugin->open_port);
650 memcpy (&v4.sin_addr, arg, arg_size);
651 #if HAVE_SOCKADDR_IN_SIN_LEN
652 v4.sin_len = sizeof (struct sockaddr_in);
654 sa = (const struct sockaddr*) &v4;
657 else if (arg_size == sizeof (struct in6_addr))
659 memset (&v6, 0, sizeof (v6));
660 v6.sin6_family = AF_INET6;
661 v6.sin6_port = htons (plugin->open_port);
662 memcpy (&v6.sin6_addr, arg, arg_size);
663 #if HAVE_SOCKADDR_IN_SIN_LEN
664 v6.sin6_len = sizeof (struct sockaddr_in6);
666 sa = (const struct sockaddr*) &v6;
674 if ( (plugin->behind_nat == GNUNET_YES) &&
675 (plugin->enable_upnp == GNUNET_YES) )
676 lal->nat = GNUNET_NAT_register (plugin->env->cfg,
678 &nat_port_map_callback,
684 * Check if the given address is in the list of 'local' addresses.
686 * @param plugin the plugin
687 * @param arg the address, either an IPv4 or an IPv6 IP address
688 * @param arg_size number of bytes in arg
689 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
692 check_local_addr (struct Plugin *plugin,
696 struct LocalAddrList *lal;
698 lal = plugin->lal_head;
701 if ( (lal->size == arg_size) &&
702 (0 == memcmp (&lal[1], arg, arg_size)) )
706 return GNUNET_SYSERR;
711 * Check if the given address is in the list of 'mapped' addresses.
713 * @param plugin the plugin
714 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
715 * @param arg_size number of bytes in arg
716 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
719 check_mapped_addr (struct Plugin *plugin,
723 struct LocalAddrList *lal;
725 lal = plugin->lal_head;
728 if ( (lal->ena_size == arg_size) &&
729 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
733 return GNUNET_SYSERR;
738 * Function called for a quick conversion of the binary address to
739 * a numeric address. Note that the caller must not free the
740 * address and that the next call to this function is allowed
741 * to override the address again.
743 * @param cls closure ('struct Plugin*')
744 * @param addr binary address
745 * @param addrlen length of the address
746 * @return string representing the same address
749 tcp_address_to_string (void *cls,
753 static char rbuf[INET6_ADDRSTRLEN + 12];
754 char buf[INET6_ADDRSTRLEN];
758 const struct IPv4TcpAddress *t4;
759 const struct IPv6TcpAddress *t6;
763 if (addrlen == sizeof (struct IPv6TcpAddress))
767 port = ntohs (t6->t6_port);
768 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
771 else if (addrlen == sizeof (struct IPv4TcpAddress))
775 port = ntohs (t4->t_port);
776 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
781 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
783 _("Unexpected address length: %u bytes\n"),
784 (unsigned int) addrlen);
788 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
790 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
793 GNUNET_snprintf (rbuf,
795 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
803 * Find the session handle for the given client.
805 * @param plugin the plugin
806 * @param client which client to find the session handle for
807 * @return NULL if no matching session exists
809 static struct Session *
810 find_session_by_client (struct Plugin *plugin,
811 const struct GNUNET_SERVER_Client *client)
815 ret = plugin->sessions;
816 while ((ret != NULL) && (client != ret->client))
823 * Create a new session. Also queues a welcome message.
825 * @param plugin the plugin
826 * @param target peer to connect to
827 * @param client client to use
828 * @param is_nat this a NAT session, we should wait for a client to
829 * connect to us from an address, then assign that to
831 * @return new session object
833 static struct Session *
834 create_session (struct Plugin *plugin,
835 const struct GNUNET_PeerIdentity *target,
836 struct GNUNET_SERVER_Client *client,
840 struct PendingMessage *pm;
841 struct WelcomeMessage welcome;
843 if (is_nat != GNUNET_YES)
844 GNUNET_assert (client != NULL);
846 GNUNET_assert (client == NULL);
848 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
850 "Creating new session for peer `%4s'\n",
851 GNUNET_i2s (target));
853 ret = GNUNET_malloc (sizeof (struct Session));
854 ret->last_activity = GNUNET_TIME_absolute_get ();
855 ret->plugin = plugin;
856 ret->is_nat = is_nat;
857 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
859 ret->next = plugin->sessions;
860 plugin->sessions = ret;
862 ret->client = client;
863 ret->target = *target;
864 ret->expecting_welcome = GNUNET_YES;
865 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
866 pm->msg = (const char*) &pm[1];
867 pm->message_size = sizeof (struct WelcomeMessage);
868 welcome.header.size = htons (sizeof (struct WelcomeMessage));
869 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
870 welcome.clientIdentity = *plugin->env->my_identity;
871 memcpy (&pm[1], &welcome, sizeof (welcome));
872 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
873 GNUNET_STATISTICS_update (plugin->env->stats,
874 gettext_noop ("# bytes currently in TCP buffers"),
877 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
878 ret->pending_messages_tail,
880 if (is_nat != GNUNET_YES)
881 GNUNET_STATISTICS_update (plugin->env->stats,
882 gettext_noop ("# TCP sessions active"),
890 * If we have pending messages, ask the server to
891 * transmit them (schedule the respective tasks, etc.)
893 * @param session for which session should we do this
895 static void process_pending_messages (struct Session *session);
899 * Function called to notify a client about the socket
900 * being ready to queue more data. "buf" will be
901 * NULL and "size" zero if the socket was closed for
902 * writing in the meantime.
905 * @param size number of bytes available in buf
906 * @param buf where the callee should write the message
907 * @return number of bytes written to buf
910 do_transmit (void *cls, size_t size, void *buf)
912 struct Session *session = cls;
913 struct GNUNET_PeerIdentity pid;
914 struct Plugin *plugin;
915 struct PendingMessage *pos;
916 struct PendingMessage *hd;
917 struct PendingMessage *tl;
918 struct GNUNET_TIME_Absolute now;
922 session->transmit_handle = NULL;
923 plugin = session->plugin;
927 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
929 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
930 GNUNET_i2s (&session->target));
932 /* timeout; cancel all messages that have already expired */
936 now = GNUNET_TIME_absolute_get ();
937 while ( (NULL != (pos = session->pending_messages_head)) &&
938 (pos->timeout.abs_value <= now.abs_value) )
940 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
941 session->pending_messages_tail,
944 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
946 "Failed to transmit %u byte message to `%4s'.\n",
948 GNUNET_i2s (&session->target));
950 ret += pos->message_size;
951 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
953 /* do this call before callbacks (so that if callbacks destroy
954 session, they have a chance to cancel actions done by this
956 process_pending_messages (session);
957 pid = session->target;
958 /* no do callbacks and do not use session again since
959 the callbacks may abort the session */
960 while (NULL != (pos = hd))
962 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
963 if (pos->transmit_cont != NULL)
964 pos->transmit_cont (pos->transmit_cont_cls,
965 &pid, GNUNET_SYSERR);
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 gettext_noop ("# bytes currently in TCP buffers"),
972 GNUNET_STATISTICS_update (plugin->env->stats,
973 gettext_noop ("# bytes discarded by TCP (timeout)"),
978 /* copy all pending messages that would fit */
983 while (NULL != (pos = session->pending_messages_head))
985 if (ret + pos->message_size > size)
987 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
988 session->pending_messages_tail,
990 GNUNET_assert (size >= pos->message_size);
991 /* FIXME: this memcpy can be up to 7% of our total runtime */
992 memcpy (cbuf, pos->msg, pos->message_size);
993 cbuf += pos->message_size;
994 ret += pos->message_size;
995 size -= pos->message_size;
996 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
998 /* schedule 'continuation' before callbacks so that callbacks that
999 cancel everything don't cause us to use a session that no longer
1001 process_pending_messages (session);
1002 session->last_activity = GNUNET_TIME_absolute_get ();
1003 pid = session->target;
1004 /* we'll now call callbacks that may cancel the session; hence
1005 we should not use 'session' after this point */
1006 while (NULL != (pos = hd))
1008 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
1009 if (pos->transmit_cont != NULL)
1010 pos->transmit_cont (pos->transmit_cont_cls,
1014 GNUNET_assert (hd == NULL);
1015 GNUNET_assert (tl == NULL);
1017 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1019 "Transmitting %u bytes\n",
1022 GNUNET_STATISTICS_update (plugin->env->stats,
1023 gettext_noop ("# bytes currently in TCP buffers"),
1026 GNUNET_STATISTICS_update (plugin->env->stats,
1027 gettext_noop ("# bytes transmitted via TCP"),
1035 * If we have pending messages, ask the server to
1036 * transmit them (schedule the respective tasks, etc.)
1038 * @param session for which session should we do this
1041 process_pending_messages (struct Session *session)
1043 struct PendingMessage *pm;
1045 GNUNET_assert (session->client != NULL);
1046 if (session->transmit_handle != NULL)
1048 if (NULL == (pm = session->pending_messages_head))
1051 session->transmit_handle
1052 = GNUNET_SERVER_notify_transmit_ready (session->client,
1054 GNUNET_TIME_absolute_get_remaining
1056 &do_transmit, session);
1061 * Functions with this signature are called whenever we need
1062 * to close a session due to a disconnect or failure to
1063 * establish a connection.
1065 * @param session session to close down
1068 disconnect_session (struct Session *session)
1070 struct Session *prev;
1071 struct Session *pos;
1072 struct PendingMessage *pm;
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1077 "Disconnecting from `%4s' at %s.\n",
1078 GNUNET_i2s (&session->target),
1079 (session->connect_addr != NULL) ?
1080 tcp_address_to_string (session->plugin,
1081 session->connect_addr,
1082 session->connect_alen) : "*");
1084 /* remove from session list */
1086 pos = session->plugin->sessions;
1087 while (pos != session)
1093 session->plugin->sessions = session->next;
1095 prev->next = session->next;
1097 /* clean up state */
1098 if (session->transmit_handle != NULL)
1100 GNUNET_CONNECTION_notify_transmit_ready_cancel
1101 (session->transmit_handle);
1102 session->transmit_handle = NULL;
1104 while (NULL != (pm = session->pending_messages_head))
1107 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1109 pm->transmit_cont != NULL
1110 ? "Could not deliver message to `%4s'.\n"
1111 : "Could not deliver message to `%4s', notifying.\n",
1112 GNUNET_i2s (&session->target));
1114 GNUNET_STATISTICS_update (session->plugin->env->stats,
1115 gettext_noop ("# bytes currently in TCP buffers"),
1116 - (int64_t) pm->message_size,
1118 GNUNET_STATISTICS_update (session->plugin->env->stats,
1119 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1122 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1123 session->pending_messages_tail,
1125 if (NULL != pm->transmit_cont)
1126 pm->transmit_cont (pm->transmit_cont_cls,
1127 &session->target, GNUNET_SYSERR);
1130 GNUNET_break (session->client != NULL);
1131 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1133 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1134 if (session->client != NULL)
1135 GNUNET_SERVER_receive_done (session->client,
1138 else if (session->client != NULL)
1139 GNUNET_SERVER_client_drop (session->client);
1140 GNUNET_STATISTICS_update (session->plugin->env->stats,
1141 gettext_noop ("# TCP sessions active"),
1144 GNUNET_free_non_null (session->connect_addr);
1146 session->plugin->env->session_end (session->plugin->env->cls,
1150 GNUNET_free (session);
1155 * Given two otherwise equivalent sessions, pick the better one.
1157 * @param s1 one session (also default)
1158 * @param s2 other session
1159 * @return "better" session (more active)
1161 static struct Session *
1162 select_better_session (struct Session *s1,
1169 if ( (s1->expecting_welcome == GNUNET_NO) &&
1170 (s2->expecting_welcome == GNUNET_YES) )
1172 if ( (s1->expecting_welcome == GNUNET_YES) &&
1173 (s2->expecting_welcome == GNUNET_NO) )
1175 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1177 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1179 if ( (GNUNET_YES == s1->inbound) &&
1180 (GNUNET_NO == s2->inbound) )
1182 if ( (GNUNET_NO == s1->inbound) &&
1183 (GNUNET_YES == s2->inbound) )
1190 * We learned about a peer (possibly behind NAT) so run the
1191 * gnunet-nat-client to send dummy ICMP responses.
1193 * @param plugin the plugin for this transport
1194 * @param sa the address of the peer (IPv4-only)
1197 run_gnunet_nat_client (struct Plugin *plugin,
1198 const struct sockaddr_in *sa)
1200 char inet4[INET_ADDRSTRLEN];
1201 char port_as_string[6];
1202 struct GNUNET_OS_Process *proc;
1204 if (plugin->internal_address == NULL)
1206 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1208 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1211 GNUNET_assert (sa->sin_family == AF_INET);
1212 if (NULL == inet_ntop (AF_INET,
1214 inet4, INET_ADDRSTRLEN))
1216 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1219 GNUNET_snprintf(port_as_string,
1220 sizeof (port_as_string),
1224 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1226 _("Running gnunet-nat-client %s %s %u\n"),
1227 plugin->internal_address,
1229 (unsigned int) plugin->adv_port);
1231 proc = GNUNET_OS_start_process (NULL,
1233 "gnunet-nat-client",
1234 "gnunet-nat-client",
1235 plugin->internal_address,
1241 /* we know that the gnunet-nat-client will terminate virtually
1243 GNUNET_OS_process_wait (proc);
1244 GNUNET_OS_process_close (proc);
1249 * Function that can be used by the transport service to transmit
1250 * a message using the plugin. Note that in the case of a
1251 * peer disconnecting, the continuation MUST be called
1252 * prior to the disconnect notification itself. This function
1253 * will be called with this peer's HELLO message to initiate
1254 * a fresh connection to another peer.
1256 * @param cls closure
1257 * @param target who should receive this message
1258 * @param msg the message to transmit
1259 * @param msgbuf_size number of bytes in 'msg'
1260 * @param priority how important is the message (most plugins will
1261 * ignore message priority and just FIFO)
1262 * @param timeout how long to wait at most for the transmission (does not
1263 * require plugins to discard the message after the timeout,
1264 * just advisory for the desired delay; most plugins will ignore
1266 * @param session which session must be used (or NULL for "any")
1267 * @param addr the address to use (can be NULL if the plugin
1268 * is "on its own" (i.e. re-use existing TCP connection))
1269 * @param addrlen length of the address in bytes
1270 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1271 * GNUNET_NO means the plugin may use any other address and
1272 * GNUNET_SYSERR means that only reliable existing
1273 * bi-directional connections should be used (regardless
1275 * @param cont continuation to call once the message has
1276 * been transmitted (or if the transport is ready
1277 * for the next transmission call; or if the
1278 * peer disconnected...); can be NULL
1279 * @param cont_cls closure for cont
1280 * @return number of bytes used (on the physical network, with overheads);
1281 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1282 * and does NOT mean that the message was not transmitted (DV and NAT)
1285 tcp_plugin_send (void *cls,
1286 const struct GNUNET_PeerIdentity *target,
1290 struct GNUNET_TIME_Relative timeout,
1291 struct Session *session,
1295 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1297 struct Plugin *plugin = cls;
1298 struct Session *cand_session;
1299 struct Session *next;
1300 struct PendingMessage *pm;
1301 struct GNUNET_CONNECTION_Handle *sa;
1305 struct sockaddr_in a4;
1306 struct sockaddr_in6 a6;
1307 const struct IPv4TcpAddress *t4;
1308 const struct IPv6TcpAddress *t6;
1309 unsigned int is_natd;
1311 GNUNET_STATISTICS_update (plugin->env->stats,
1312 gettext_noop ("# bytes TCP was asked to transmit"),
1315 /* FIXME: we could do this cheaper with a hash table
1316 where we could restrict the iteration to entries that match
1317 the target peer... */
1318 is_natd = GNUNET_NO;
1319 if (session == NULL)
1321 cand_session = NULL;
1322 next = plugin->sessions;
1323 while (NULL != (session = next))
1325 next = session->next;
1326 GNUNET_assert (session->client != NULL);
1327 if (0 != memcmp (target,
1329 sizeof (struct GNUNET_PeerIdentity)))
1331 if ( ( (GNUNET_SYSERR == force_address) &&
1332 (session->expecting_welcome == GNUNET_NO) ) ||
1333 (GNUNET_NO == force_address) )
1335 cand_session = select_better_session (cand_session,
1339 if (GNUNET_SYSERR == force_address)
1341 GNUNET_break (GNUNET_YES == force_address);
1347 if ( (addrlen != session->connect_alen) &&
1348 (session->is_nat == GNUNET_NO) )
1350 if ((0 != memcmp (session->connect_addr,
1352 addrlen)) && (session->is_nat == GNUNET_NO))
1354 cand_session = select_better_session (cand_session,
1357 session = cand_session;
1359 if ( (session == NULL) &&
1363 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1365 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1366 GNUNET_i2s (target));
1368 GNUNET_STATISTICS_update (plugin->env->stats,
1369 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1374 if (session == NULL)
1376 if (addrlen == sizeof (struct IPv6TcpAddress))
1380 memset (&a6, 0, sizeof (a6));
1381 #if HAVE_SOCKADDR_IN_SIN_LEN
1382 a6.sin6_len = sizeof (a6);
1384 a6.sin6_family = AF_INET6;
1385 a6.sin6_port = t6->t6_port;
1386 if (t6->t6_port == 0)
1387 is_natd = GNUNET_YES;
1388 memcpy (&a6.sin6_addr,
1390 sizeof (struct in6_addr));
1394 else if (addrlen == sizeof (struct IPv4TcpAddress))
1398 memset (&a4, 0, sizeof (a4));
1399 #if HAVE_SOCKADDR_IN_SIN_LEN
1400 a4.sin_len = sizeof (a4);
1402 a4.sin_family = AF_INET;
1403 a4.sin_port = t4->t_port;
1404 if (t4->t_port == 0)
1405 is_natd = GNUNET_YES;
1406 a4.sin_addr.s_addr = t4->ipv4_addr;
1412 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1414 _("Address of unexpected length: %u\n"),
1420 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1421 return -1; /* NAT client only works with IPv4 addresses */
1422 if (0 == plugin->max_connections)
1423 return -1; /* saturated */
1425 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1426 (is_natd == GNUNET_YES) &&
1427 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1428 &target->hashPubKey)) )
1431 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1433 _("Found valid IPv4 NAT address (creating session)!\n"));
1435 session = create_session (plugin,
1440 /* create new message entry */
1441 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1442 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1443 pm->msg = (const char*) &pm[1];
1444 memcpy (&pm[1], msg, msgbuf_size);
1445 /* FIXME: this memcpy can be up to 7% of our total run-time
1446 (for transport service) */
1447 pm->message_size = msgbuf_size;
1448 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1449 pm->transmit_cont = cont;
1450 pm->transmit_cont_cls = cont_cls;
1452 /* append pm to pending_messages list */
1453 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1454 session->pending_messages_tail,
1455 session->pending_messages_tail,
1458 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1459 &target->hashPubKey,
1461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1463 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1465 "Created NAT WAIT connection to `%4s' at `%s'\n",
1466 GNUNET_i2s (target),
1467 GNUNET_a2s (sb, sbs));
1469 run_gnunet_nat_client (plugin, &a4);
1472 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1473 (is_natd == GNUNET_YES) &&
1474 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1475 &target->hashPubKey)) )
1477 /* Only do one NAT punch attempt per peer identity */
1480 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1484 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1486 "Failed to create connection to `%4s' at `%s'\n",
1487 GNUNET_i2s (target),
1488 GNUNET_a2s (sb, sbs));
1490 GNUNET_STATISTICS_update (plugin->env->stats,
1491 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1496 GNUNET_assert (0 != plugin->max_connections);
1497 plugin->max_connections--;
1499 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1501 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1502 GNUNET_i2s (target),
1503 GNUNET_a2s (sb, sbs));
1505 session = create_session (plugin,
1507 GNUNET_SERVER_connect_socket (plugin->server,
1510 session->connect_addr = GNUNET_malloc (addrlen);
1511 memcpy (session->connect_addr,
1514 session->connect_alen = addrlen;
1516 GNUNET_assert (session != NULL);
1517 GNUNET_assert (session->client != NULL);
1518 GNUNET_STATISTICS_update (plugin->env->stats,
1519 gettext_noop ("# bytes currently in TCP buffers"),
1522 /* create new message entry */
1523 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1524 pm->msg = (const char*) &pm[1];
1525 memcpy (&pm[1], msg, msgbuf_size);
1526 pm->message_size = msgbuf_size;
1527 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1528 pm->transmit_cont = cont;
1529 pm->transmit_cont_cls = cont_cls;
1531 /* append pm to pending_messages list */
1532 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1533 session->pending_messages_tail,
1534 session->pending_messages_tail,
1537 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1539 "Asked to transmit %u bytes to `%s', added message to list.\n",
1541 GNUNET_i2s (target));
1543 process_pending_messages (session);
1549 * Function that can be called to force a disconnect from the
1550 * specified neighbour. This should also cancel all previously
1551 * scheduled transmissions. Obviously the transmission may have been
1552 * partially completed already, which is OK. The plugin is supposed
1553 * to close the connection (if applicable) and no longer call the
1554 * transmit continuation(s).
1556 * Finally, plugin MUST NOT call the services's receive function to
1557 * notify the service that the connection to the specified target was
1558 * closed after a getting this call.
1560 * @param cls closure
1561 * @param target peer for which the last transmission is
1565 tcp_plugin_disconnect (void *cls,
1566 const struct GNUNET_PeerIdentity *target)
1568 struct Plugin *plugin = cls;
1569 struct Session *session;
1570 struct Session *next;
1571 struct PendingMessage *pm;
1574 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1576 "Asked to cancel session with `%4s'\n",
1577 GNUNET_i2s (target));
1579 next = plugin->sessions;
1580 while (NULL != (session = next))
1582 next = session->next;
1583 if (0 != memcmp (target,
1585 sizeof (struct GNUNET_PeerIdentity)))
1587 pm = session->pending_messages_head;
1590 pm->transmit_cont = NULL;
1591 pm->transmit_cont_cls = NULL;
1594 GNUNET_STATISTICS_update (session->plugin->env->stats,
1595 gettext_noop ("# transport-service disconnect requests for TCP"),
1598 disconnect_session (session);
1604 * Context for address to string conversion.
1606 struct PrettyPrinterContext
1609 * Function to call with the result.
1611 GNUNET_TRANSPORT_AddressStringCallback asc;
1614 * Clsoure for 'asc'.
1619 * Port to add after the IP address.
1626 * Append our port and forward the result.
1628 * @param cls the 'struct PrettyPrinterContext*'
1629 * @param hostname hostname part of the address
1632 append_port (void *cls, const char *hostname)
1634 struct PrettyPrinterContext *ppc = cls;
1637 if (hostname == NULL)
1639 ppc->asc (ppc->asc_cls, NULL);
1643 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1644 ppc->asc (ppc->asc_cls, ret);
1650 * Convert the transports address to a nice, human-readable
1653 * @param cls closure
1654 * @param type name of the transport that generated the address
1655 * @param addr one of the addresses of the host, NULL for the last address
1656 * the specific address format depends on the transport
1657 * @param addrlen length of the address
1658 * @param numeric should (IP) addresses be displayed in numeric form?
1659 * @param timeout after how long should we give up?
1660 * @param asc function to call on each string
1661 * @param asc_cls closure for asc
1664 tcp_plugin_address_pretty_printer (void *cls,
1669 struct GNUNET_TIME_Relative timeout,
1670 GNUNET_TRANSPORT_AddressStringCallback asc,
1673 struct PrettyPrinterContext *ppc;
1676 struct sockaddr_in a4;
1677 struct sockaddr_in6 a6;
1678 const struct IPv4TcpAddress *t4;
1679 const struct IPv6TcpAddress *t6;
1682 if (addrlen == sizeof (struct IPv6TcpAddress))
1685 memset (&a6, 0, sizeof (a6));
1686 a6.sin6_family = AF_INET6;
1687 a6.sin6_port = t6->t6_port;
1688 memcpy (&a6.sin6_addr,
1690 sizeof (struct in6_addr));
1691 port = ntohs (t6->t6_port);
1695 else if (addrlen == sizeof (struct IPv4TcpAddress))
1698 memset (&a4, 0, sizeof (a4));
1699 a4.sin_family = AF_INET;
1700 a4.sin_port = t4->t_port;
1701 a4.sin_addr.s_addr = t4->ipv4_addr;
1702 port = ntohs (t4->t_port);
1708 /* invalid address */
1709 GNUNET_break_op (0);
1710 asc (asc_cls, NULL);
1713 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1715 ppc->asc_cls = asc_cls;
1717 GNUNET_RESOLVER_hostname_get (sb,
1719 !numeric, timeout, &append_port, ppc);
1724 * Check if the given port is plausible (must be either our listen
1725 * port or our advertised port), or any port if we are behind NAT
1726 * and do not have a port open. If it is neither, we return
1729 * @param plugin global variables
1730 * @param in_port port number to check
1731 * @return GNUNET_OK if port is either open_port or adv_port
1734 check_port (struct Plugin *plugin,
1737 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1739 return GNUNET_SYSERR;
1744 * Function that will be called to check if a binary address for this
1745 * plugin is well-formed and corresponds to an address for THIS peer
1746 * (as per our configuration). Naturally, if absolutely necessary,
1747 * plugins can be a bit conservative in their answer, but in general
1748 * plugins should make sure that the address does not redirect
1749 * traffic to a 3rd party that might try to man-in-the-middle our
1752 * @param cls closure, our 'struct Plugin*'
1753 * @param addr pointer to the address
1754 * @param addrlen length of addr
1755 * @return GNUNET_OK if this is a plausible address for this peer
1756 * and transport, GNUNET_SYSERR if not
1759 tcp_plugin_check_address (void *cls,
1763 struct Plugin *plugin = cls;
1764 struct IPv4TcpAddress *v4;
1765 struct IPv6TcpAddress *v6;
1767 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1768 (addrlen != sizeof (struct IPv6TcpAddress)))
1770 GNUNET_break_op (0);
1771 return GNUNET_SYSERR;
1773 if (addrlen == sizeof (struct IPv4TcpAddress))
1775 v4 = (struct IPv4TcpAddress *) addr;
1777 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1780 check_port (plugin, ntohs (v4->t_port)))
1781 return GNUNET_SYSERR;
1783 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1784 return GNUNET_SYSERR;
1788 v6 = (struct IPv6TcpAddress *) addr;
1789 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1791 GNUNET_break_op (0);
1792 return GNUNET_SYSERR;
1795 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1798 check_port (plugin, ntohs (v6->t6_port)))
1799 return GNUNET_SYSERR;
1801 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1802 return GNUNET_SYSERR;
1809 * We've received a nat probe from this peer via TCP. Finish
1810 * creating the client session and resume sending of queued
1813 * @param cls closure
1814 * @param client identification of the client
1815 * @param message the actual message
1818 handle_tcp_nat_probe (void *cls,
1819 struct GNUNET_SERVER_Client *client,
1820 const struct GNUNET_MessageHeader *message)
1822 struct Plugin *plugin = cls;
1823 struct Session *session;
1824 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1827 struct IPv4TcpAddress *t4;
1828 struct IPv6TcpAddress *t6;
1829 const struct sockaddr_in *s4;
1830 const struct sockaddr_in6 *s6;
1833 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1835 "received NAT probe\n");
1837 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1838 * a connection to this peer by running gnunet-nat-client. This peer
1839 * received the punch message and now wants us to use the new connection
1840 * as the default for that peer. Do so and then send a WELCOME message
1841 * so we can really be connected!
1843 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1850 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1852 if (0 == memcmp (&tcp_nat_probe->clientIdentity,
1853 plugin->env->my_identity,
1854 sizeof (struct GNUNET_PeerIdentity)))
1856 /* refuse connections from ourselves */
1857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1861 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1862 &tcp_nat_probe->clientIdentity.hashPubKey);
1863 if (session == NULL)
1866 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1868 "Did NOT find session for NAT probe!\n");
1870 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1876 "Found session for NAT probe!\n");
1878 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1879 &tcp_nat_probe->clientIdentity.hashPubKey,
1880 session) == GNUNET_YES);
1882 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1885 GNUNET_free (session);
1886 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1890 GNUNET_SERVER_client_keep (client);
1891 session->client = client;
1892 session->last_activity = GNUNET_TIME_absolute_get ();
1893 session->inbound = GNUNET_NO;
1896 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1898 "Found address `%s' for incoming connection\n",
1899 GNUNET_a2s (vaddr, alen));
1901 switch (((const struct sockaddr *)vaddr)->sa_family)
1905 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1906 t4->t_port = s4->sin_port;
1907 t4->ipv4_addr = s4->sin_addr.s_addr;
1908 session->connect_addr = t4;
1909 session->connect_alen = sizeof (struct IPv4TcpAddress);
1913 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1914 t6->t6_port = s6->sin6_port;
1915 memcpy (&t6->ipv6_addr,
1917 sizeof (struct in6_addr));
1918 session->connect_addr = t6;
1919 session->connect_alen = sizeof (struct IPv6TcpAddress);
1922 GNUNET_break_op (0);
1924 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1926 "Bad address for incoming connection!\n");
1928 GNUNET_free (vaddr);
1929 GNUNET_SERVER_client_drop (client);
1930 GNUNET_free (session);
1931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1934 GNUNET_free (vaddr);
1936 session->next = plugin->sessions;
1937 plugin->sessions = session;
1938 GNUNET_STATISTICS_update (plugin->env->stats,
1939 gettext_noop ("# TCP sessions active"),
1942 process_pending_messages (session);
1943 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1948 * We've received a welcome from this peer via TCP. Possibly create a
1949 * fresh client record and send back our welcome.
1951 * @param cls closure
1952 * @param client identification of the client
1953 * @param message the actual message
1956 handle_tcp_welcome (void *cls,
1957 struct GNUNET_SERVER_Client *client,
1958 const struct GNUNET_MessageHeader *message)
1960 struct Plugin *plugin = cls;
1961 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1962 struct Session *session;
1965 struct IPv4TcpAddress *t4;
1966 struct IPv6TcpAddress *t6;
1967 const struct sockaddr_in *s4;
1968 const struct sockaddr_in6 *s6;
1970 if (0 == memcmp (&wm->clientIdentity,
1971 plugin->env->my_identity,
1972 sizeof (struct GNUNET_PeerIdentity)))
1974 /* refuse connections from ourselves */
1975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1979 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1981 "Received %s message from `%4s'.\n",
1983 GNUNET_i2s (&wm->clientIdentity));
1985 GNUNET_STATISTICS_update (plugin->env->stats,
1986 gettext_noop ("# TCP WELCOME messages received"),
1989 session = find_session_by_client (plugin, client);
1991 if (session == NULL)
1994 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1996 "Received %s message from a `%4s', creating new session\n",
1998 GNUNET_i2s (&wm->clientIdentity));
2000 GNUNET_SERVER_client_keep (client);
2001 session = create_session (plugin,
2002 &wm->clientIdentity,
2005 session->inbound = GNUNET_YES;
2007 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2010 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2012 "Found address `%s' for incoming connection\n",
2013 GNUNET_a2s (vaddr, alen));
2015 if (alen == sizeof (struct sockaddr_in))
2018 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2019 t4->t_port = s4->sin_port;
2020 t4->ipv4_addr = s4->sin_addr.s_addr;
2021 session->connect_addr = t4;
2022 session->connect_alen = sizeof (struct IPv4TcpAddress);
2024 else if (alen == sizeof (struct sockaddr_in6))
2027 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2028 t6->t6_port = s6->sin6_port;
2029 memcpy (&t6->ipv6_addr,
2031 sizeof (struct in6_addr));
2032 session->connect_addr = t6;
2033 session->connect_alen = sizeof (struct IPv6TcpAddress);
2036 GNUNET_free (vaddr);
2041 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2043 "Did not obtain TCP socket address for incoming connection\n");
2046 process_pending_messages (session);
2052 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2054 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2056 "Found address `%s' (already have session)\n",
2057 GNUNET_a2s (vaddr, alen));
2058 GNUNET_free (vaddr);
2063 if (session->expecting_welcome != GNUNET_YES)
2065 GNUNET_break_op (0);
2066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2069 session->last_activity = GNUNET_TIME_absolute_get ();
2070 session->expecting_welcome = GNUNET_NO;
2071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2076 * Task to signal the server that we can continue
2077 * receiving from the TCP client now.
2079 * @param cls the 'struct Session*'
2080 * @param tc task context (unused)
2083 delayed_done (void *cls,
2084 const struct GNUNET_SCHEDULER_TaskContext *tc)
2086 struct Session *session = cls;
2087 struct GNUNET_TIME_Relative delay;
2089 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2090 delay = session->plugin->env->receive (session->plugin->env->cls,
2096 if (delay.rel_value == 0)
2097 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2099 session->receive_delay_task =
2100 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2105 * We've received data for this peer via TCP. Unbox,
2106 * compute latency and forward.
2108 * @param cls closure
2109 * @param client identification of the client
2110 * @param message the actual message
2113 handle_tcp_data (void *cls,
2114 struct GNUNET_SERVER_Client *client,
2115 const struct GNUNET_MessageHeader *message)
2117 struct Plugin *plugin = cls;
2118 struct Session *session;
2119 struct GNUNET_TIME_Relative delay;
2122 type = ntohs (message->type);
2123 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2124 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2126 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2127 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2130 session = find_session_by_client (plugin, client);
2131 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2136 session->last_activity = GNUNET_TIME_absolute_get ();
2138 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2140 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2141 (unsigned int) ntohs (message->size),
2142 (unsigned int) ntohs (message->type),
2143 GNUNET_i2s (&session->target));
2145 GNUNET_STATISTICS_update (plugin->env->stats,
2146 gettext_noop ("# bytes received via TCP"),
2147 ntohs (message->size),
2149 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2150 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2151 distance[0].value = htonl (1);
2152 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2153 distance[1].value = htonl (0);
2154 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2155 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2158 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2159 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2160 if (delay.rel_value == 0)
2162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2167 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2169 "Throttling receiving from `%s' for %llu ms\n",
2170 GNUNET_i2s (&session->target),
2171 (unsigned long long) delay.rel_value);
2173 GNUNET_SERVER_disable_receive_done_warning (client);
2174 session->receive_delay_task =
2175 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2181 * Functions with this signature are called whenever a peer
2182 * is disconnected on the network level.
2184 * @param cls closure
2185 * @param client identification of the client
2188 disconnect_notify (void *cls,
2189 struct GNUNET_SERVER_Client *client)
2191 struct Plugin *plugin = cls;
2192 struct Session *session;
2196 plugin->max_connections++;
2197 session = find_session_by_client (plugin, client);
2198 if (session == NULL)
2199 return; /* unknown, nothing to do */
2201 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2203 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2204 GNUNET_i2s (&session->target),
2205 (session->connect_addr != NULL) ?
2206 tcp_address_to_string (session->plugin,
2207 session->connect_addr,
2208 session->connect_alen) : "*");
2210 GNUNET_STATISTICS_update (session->plugin->env->stats,
2211 gettext_noop ("# network-level TCP disconnect events"),
2214 disconnect_session (session);
2218 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
2221 int local = GNUNET_NO;
2222 int af = addr->sa_family;
2227 uint32_t netmask = 0x7F000000;
2228 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
2229 res = (address >> 24) ^ (netmask >> 24);
2235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2236 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2242 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
2243 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2249 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2258 * Add the IP of our network interface to the list of
2259 * our internal IP addresses.
2261 * @param cls the 'struct Plugin*'
2262 * @param name name of the interface
2263 * @param isDefault do we think this may be our default interface
2264 * @param addr address of the interface
2265 * @param addrlen number of bytes in addr
2266 * @return GNUNET_OK to continue iterating
2269 process_interfaces (void *cls,
2272 const struct sockaddr *addr, socklen_t addrlen)
2274 struct Plugin *plugin = cls;
2276 struct IPv4TcpAddress t4;
2277 struct IPv6TcpAddress t6;
2278 struct IPv4TcpAddress t4_nat;
2279 struct IPv6TcpAddress t6_nat;
2283 char buf[INET6_ADDRSTRLEN];
2285 af = addr->sa_family;
2288 if (plugin->use_localaddresses == GNUNET_NO)
2290 if (GNUNET_YES == check_localaddress (addr, addrlen))
2293 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2295 "Not notifying transport of address `%s' (local address)\n",
2296 GNUNET_a2s (addr, addrlen));
2305 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2306 GNUNET_assert (NULL != inet_ntop(AF_INET,
2310 if ( (plugin->bind_address != NULL) &&
2311 (0 != strcmp(buf, plugin->bind_address)) )
2314 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2316 "Not notifying transport of address `%s' (does not match bind address)\n",
2317 GNUNET_a2s (addr, addrlen));
2321 if ( (plugin->internal_address == NULL) &&
2323 plugin->internal_address = GNUNET_strdup (buf);
2324 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2325 if (plugin->behind_nat == GNUNET_YES)
2327 /* Also advertise as NAT (with port 0) */
2328 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2329 t4_nat.t_port = htons(0);
2332 t4.t_port = htons (plugin->adv_port);
2337 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2338 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2342 /* skip link local addresses */
2345 memcpy (&t6.ipv6_addr,
2346 &((struct sockaddr_in6 *) addr)->sin6_addr,
2347 sizeof (struct in6_addr));
2349 /* check bind address */
2350 GNUNET_assert (NULL != inet_ntop(AF_INET6,
2355 if ( (plugin->bind_address != NULL) &&
2356 (0 != strcmp(buf, plugin->bind_address)) )
2359 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2361 "Not notifying transport of address `%s' (does not match bind address)\n",
2362 GNUNET_a2s (addr, addrlen));
2367 add_to_address_list (plugin,
2369 sizeof (struct in6_addr));
2370 if (plugin->behind_nat == GNUNET_YES)
2372 /* Also advertise as NAT (with port 0) */
2373 memcpy (&t6_nat.ipv6_addr,
2374 &((struct sockaddr_in6 *) addr)->sin6_addr,
2375 sizeof (struct in6_addr));
2376 t6_nat.t6_port = htons(0);
2379 t6.t6_port = htons (plugin->adv_port);
2387 if (plugin->adv_port != 0)
2390 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2392 "Found address `%s' (%s) len %d\n",
2393 GNUNET_a2s (addr, addrlen), name, args);
2395 plugin->env->notify_address (plugin->env->cls,
2397 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2400 if (arg_nat != NULL)
2402 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2404 _("Found address `%s' (%s) len %d\n"),
2405 GNUNET_a2s (addr, addrlen), name, args);
2406 plugin->env->notify_address (plugin->env->cls,
2408 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2416 * Function called by the resolver for each address obtained from DNS
2417 * for our own hostname. Add the addresses to the list of our
2418 * external IP addresses.
2420 * @param cls closure
2421 * @param addr one of the addresses of the host, NULL for the last address
2422 * @param addrlen length of the address
2425 process_hostname_ips (void *cls,
2426 const struct sockaddr *addr, socklen_t addrlen)
2428 struct Plugin *plugin = cls;
2432 plugin->hostname_dns = NULL;
2435 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2436 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2441 * We can now send a probe message, copy into buffer to really send.
2443 * @param cls closure, a struct TCPProbeContext
2444 * @param size max size to copy
2445 * @param buf buffer to copy message to
2446 * @return number of bytes copied into buf
2449 notify_send_probe (void *cls,
2453 struct TCPProbeContext *tcp_probe_ctx = cls;
2454 struct Plugin *plugin = tcp_probe_ctx->plugin;
2457 tcp_probe_ctx->transmit_handle = NULL;
2458 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2463 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2464 GNUNET_free(tcp_probe_ctx);
2467 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2468 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2469 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2470 tcp_probe_ctx->sock);
2471 ret = sizeof(tcp_probe_ctx->message);
2472 GNUNET_free(tcp_probe_ctx);
2478 * We have been notified that gnunet-nat-server has written something to stdout.
2479 * Handle the output, then reschedule this function to be called again once
2480 * more is available.
2482 * @param cls the plugin handle
2483 * @param tc the scheduling context
2486 tcp_plugin_server_read (void *cls,
2487 const struct GNUNET_SCHEDULER_TaskContext *tc)
2489 struct Plugin *plugin = cls;
2494 const char *port_start;
2495 struct sockaddr_in sin_addr;
2496 struct TCPProbeContext *tcp_probe_ctx;
2497 struct GNUNET_CONNECTION_Handle *sock;
2499 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2501 memset (mybuf, 0, sizeof(mybuf));
2502 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2508 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2510 "Finished reading from server stdout with code: %d\n",
2513 /* FIXME: consider process_wait here? */
2518 for (i = 0; i < sizeof(mybuf); i++)
2520 if (mybuf[i] == '\n')
2525 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2528 port_start = &mybuf[i + 1];
2532 /* construct socket address of sender */
2533 memset (&sin_addr, 0, sizeof (sin_addr));
2534 sin_addr.sin_family = AF_INET;
2535 #if HAVE_SOCKADDR_IN_SIN_LEN
2536 sin_addr.sin_len = sizeof (sin_addr);
2538 if ( (NULL == port_start) ||
2539 (1 != sscanf (port_start, "%d", &port)) ||
2540 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2542 /* should we restart gnunet-nat-server? */
2543 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2545 _("gnunet-nat-server generated malformed address `%s'\n"),
2547 plugin->server_read_task
2548 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2549 plugin->server_stdout_handle,
2550 &tcp_plugin_server_read,
2554 sin_addr.sin_port = htons((uint16_t) port);
2556 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2558 "gnunet-nat-server read: %s:%d\n",
2563 * We have received an ICMP response, ostensibly from a peer
2564 * that wants to connect to us! Send a message to establish a connection.
2566 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2567 (const struct sockaddr *)&sin_addr,
2571 /* failed for some odd reason (out of sockets?); ignore attempt */
2572 plugin->server_read_task =
2573 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2574 plugin->server_stdout_handle,
2575 &tcp_plugin_server_read,
2580 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2581 "Sending TCP probe message to `%s:%u'!\n",
2583 (unsigned int) port);
2584 /* FIXME: do we need to track these probe context objects so that
2585 we can clean them up on plugin unload? */
2587 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2588 tcp_probe_ctx->message.header.size
2589 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2590 tcp_probe_ctx->message.header.type
2591 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2592 memcpy (&tcp_probe_ctx->message.clientIdentity,
2593 plugin->env->my_identity,
2594 sizeof(struct GNUNET_PeerIdentity));
2595 tcp_probe_ctx->plugin = plugin;
2596 tcp_probe_ctx->sock = sock;
2597 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2600 tcp_probe_ctx->transmit_handle
2601 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2602 ntohs (tcp_probe_ctx->message.header.size),
2603 GNUNET_TIME_UNIT_FOREVER_REL,
2604 ¬ify_send_probe, tcp_probe_ctx);
2606 plugin->server_read_task =
2607 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2608 plugin->server_stdout_handle,
2609 &tcp_plugin_server_read,
2615 * Start the gnunet-nat-server process for users behind NAT.
2617 * @param plugin the transport plugin
2618 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2621 tcp_transport_start_nat_server (struct Plugin *plugin)
2623 if (plugin->internal_address == NULL)
2624 return GNUNET_SYSERR;
2625 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2628 if (plugin->server_stdout == NULL)
2629 return GNUNET_SYSERR;
2631 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2633 "Starting %s %s\n", "gnunet-nat-server", plugin->internal_address);
2635 /* Start the server process */
2636 plugin->server_proc = GNUNET_OS_start_process (NULL,
2637 plugin->server_stdout,
2638 "gnunet-nat-server",
2639 "gnunet-nat-server",
2640 plugin->internal_address,
2642 if (plugin->server_proc == NULL)
2644 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2646 _("Failed to start %s\n"),
2647 "gnunet-nat-server");
2648 GNUNET_DISK_pipe_close (plugin->server_stdout);
2649 plugin->server_stdout = NULL;
2650 return GNUNET_SYSERR;
2652 /* Close the write end of the read pipe */
2653 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2654 GNUNET_DISK_PIPE_END_WRITE);
2655 plugin->server_stdout_handle
2656 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2657 GNUNET_DISK_PIPE_END_READ);
2658 plugin->server_read_task
2659 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2660 plugin->server_stdout_handle,
2661 &tcp_plugin_server_read,
2668 * Return the actual path to a file found in the current
2669 * PATH environment variable.
2671 * @param binary the name of the file to find
2672 * @return path to binary, NULL if not found
2675 get_path_from_PATH (const char *binary)
2683 p = getenv ("PATH");
2686 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2688 _("PATH environment variable is unset.\n"));
2691 path = GNUNET_strdup (p); /* because we write on it */
2692 buf = GNUNET_malloc (strlen (path) + 20);
2695 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2698 sprintf (buf, "%s/%s", pos, binary);
2699 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2706 sprintf (buf, "%s/%s", pos, binary);
2707 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2719 * Check whether the suid bit is set on a file.
2720 * Attempts to find the file using the current
2721 * PATH environment variable as a search path.
2723 * @param binary the name of the file to check
2724 * @return GNUNET_YES if the file is SUID,
2726 * GNUNET_SYSERR on error
2729 check_gnunet_nat_binary (const char *binary)
2731 struct stat statbuf;
2737 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2738 p = get_path_from_PATH (binaryexe);
2741 p = get_path_from_PATH (binary);
2745 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2747 _("Could not find binary `%s' in PATH!\n"),
2751 if (0 != STAT (p, &statbuf))
2753 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2754 _("stat (%s) failed: %s\n"),
2758 return GNUNET_SYSERR;
2762 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2763 (statbuf.st_uid == 0) )
2767 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2768 if (INVALID_SOCKET == rawsock)
2770 DWORD err = GetLastError ();
2771 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2773 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2774 return GNUNET_NO; /* not running as administrator */
2776 closesocket (rawsock);
2783 * Our (external) hostname was resolved.
2785 * @param cls the 'struct Plugin'
2786 * @param addr NULL on error, otherwise result of DNS lookup
2787 * @param addrlen number of bytes in addr
2790 process_external_ip (void *cls,
2791 const struct sockaddr *addr,
2794 struct Plugin *plugin = cls;
2795 const struct sockaddr_in *s;
2796 struct IPv4TcpAddress t4;
2797 char buf[INET_ADDRSTRLEN];
2799 plugin->ext_dns = NULL;
2802 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2803 s = (const struct sockaddr_in *) addr;
2804 t4.ipv4_addr = s->sin_addr.s_addr;
2805 if ( (plugin->behind_nat == GNUNET_YES) &&
2806 (plugin->enable_nat_server == GNUNET_YES) )
2808 t4.t_port = htons(0);
2809 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2811 "Notifying transport of address %s:%d\n",
2812 plugin->external_address,
2817 t4.t_port = htons(plugin->adv_port);
2818 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2820 "Notifying transport of address %s:%d\n",
2821 plugin->external_address,
2822 (int) plugin->adv_port);
2825 if ((plugin->bind_address != NULL) && (plugin->behind_nat == GNUNET_NO))
2827 GNUNET_assert (NULL != inet_ntop(AF_INET,
2831 if (0 != strcmp (plugin->bind_address, buf))
2833 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2835 "NAT is not enabled and specific bind address `%s' differs from external address `%s'! Not notifying about external address `%s'\n",
2836 plugin->bind_address,
2837 plugin->external_address,
2838 plugin->external_address);
2843 add_to_address_list (plugin,
2845 sizeof (struct in_addr));
2847 plugin->env->notify_address (plugin->env->cls,
2850 GNUNET_TIME_UNIT_FOREVER_REL);
2855 * Entry point for the plugin.
2857 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2858 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2861 libgnunet_plugin_transport_tcp_init (void *cls)
2863 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2864 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2865 sizeof (struct WelcomeMessage)},
2866 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2867 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2870 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2871 struct GNUNET_TRANSPORT_PluginFunctions *api;
2872 struct Plugin *plugin;
2873 struct GNUNET_SERVICE_Context *service;
2874 unsigned long long aport;
2875 unsigned long long bport;
2876 unsigned long long max_connections;
2880 int enable_nat_client;
2881 int enable_nat_server;
2883 int use_localaddresses;
2884 char *internal_address;
2885 char *external_address;
2887 struct sockaddr_in in_addr;
2888 struct GNUNET_TIME_Relative idle_timeout;
2890 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2893 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2896 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2898 "ENABLE_NAT_CLIENT");
2899 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2901 "ENABLE_NAT_SERVER");
2902 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2906 if ( (GNUNET_YES == enable_nat_server) &&
2907 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2909 enable_nat_server = GNUNET_NO;
2910 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2911 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2912 "gnunet-nat-server");
2915 if ( (GNUNET_YES == enable_nat_client) &&
2916 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2918 enable_nat_client = GNUNET_NO;
2919 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2920 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2921 "gnunet-nat-client");
2924 external_address = NULL;
2926 GNUNET_CONFIGURATION_have_value (env->cfg,
2928 "EXTERNAL_ADDRESS"))
2930 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2936 if ( (external_address != NULL) &&
2937 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2940 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2942 _("Malformed %s `%s' given in configuration!\n"),
2947 if ( (external_address == NULL) &&
2948 (nat_punched == GNUNET_YES) )
2950 nat_punched = GNUNET_NO;
2951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2952 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2953 "EXTERNAL_ADDRESS");
2956 if (GNUNET_YES == nat_punched)
2958 enable_nat_server = GNUNET_NO;
2959 enable_upnp = GNUNET_NO;
2962 bind_address = NULL;
2963 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2968 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2970 _("Binding TCP plugin to specific address: `%s'\n"),
2974 internal_address = NULL;
2976 GNUNET_CONFIGURATION_have_value (env->cfg,
2978 "INTERNAL_ADDRESS"))
2980 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2986 if ( (internal_address != NULL) &&
2987 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2989 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2991 _("Malformed %s `%s' given in configuration!\n"),
2994 GNUNET_free_non_null(internal_address);
2995 GNUNET_free_non_null(external_address);
2999 if ((bind_address != NULL) && (internal_address != NULL))
3001 if (0 != strcmp(internal_address, bind_address ))
3003 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3005 "Specific bind address `%s' and internal address `%s' must not differ, forcing internal address to bind address!\n",
3006 bind_address, internal_address);
3007 GNUNET_free (internal_address);
3008 internal_address = bind_address;
3009 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3010 "tcp","New internal address `%s'\n", internal_address);
3014 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3018 max_connections = 128;
3022 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3028 GNUNET_CONFIGURATION_get_value_number (env->cfg,
3034 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3036 _("Require valid port number for service `%s' in configuration!\n"),
3038 GNUNET_free_non_null(external_address);
3039 GNUNET_free_non_null(internal_address);
3043 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
3046 if (use_localaddresses == GNUNET_SYSERR)
3047 use_localaddresses = GNUNET_NO;
3056 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
3057 if (service == NULL)
3059 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
3061 _("Failed to start service.\n"));
3068 plugin = GNUNET_malloc (sizeof (struct Plugin));
3069 plugin->max_connections = max_connections;
3070 plugin->open_port = bport;
3071 plugin->adv_port = aport;
3072 plugin->bind_address = bind_address;
3073 plugin->external_address = external_address;
3074 plugin->internal_address = internal_address;
3075 plugin->behind_nat = behind_nat;
3076 plugin->nat_punched = nat_punched;
3077 plugin->enable_nat_client = enable_nat_client;
3078 plugin->enable_nat_server = enable_nat_server;
3079 plugin->enable_upnp = enable_upnp;
3080 plugin->use_localaddresses = use_localaddresses;
3082 plugin->lsock = NULL;
3083 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3085 api->send = &tcp_plugin_send;
3086 api->disconnect = &tcp_plugin_disconnect;
3087 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3088 api->check_address = &tcp_plugin_check_address;
3089 api->address_to_string = &tcp_address_to_string;
3090 plugin->service = service;
3091 if (service != NULL)
3093 plugin->server = GNUNET_SERVICE_get_server (service);
3098 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3103 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3105 _("Failed to find option %s in section %s!\n"),
3108 GNUNET_free_non_null(external_address);
3109 GNUNET_free_non_null(internal_address);
3113 plugin->server = GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin, NULL,
3114 idle_timeout, GNUNET_YES);
3116 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3117 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
3119 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
3121 plugin->handlers[i].callback_cls = plugin;
3122 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
3123 GNUNET_SERVER_disconnect_notify (plugin->server,
3126 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3128 if ( (plugin->behind_nat == GNUNET_YES) &&
3129 (plugin->enable_nat_server == GNUNET_YES) &&
3130 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
3132 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3134 _("Failed to start %s required for NAT in %s!\n"),
3137 GNUNET_free_non_null(external_address);
3138 GNUNET_free_non_null(internal_address);
3139 if (service != NULL)
3140 GNUNET_SERVICE_stop (service);
3142 GNUNET_SERVER_destroy (plugin->server);
3147 if (enable_nat_client == GNUNET_YES)
3149 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
3150 GNUNET_assert (plugin->nat_wait_conns != NULL);
3154 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3156 _("TCP transport listening on port %llu\n"),
3159 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3161 _("TCP transport not listening on any port (client only)\n"));
3163 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3165 _("TCP transport advertises itself as being on port %llu\n"),
3168 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (AF_UNSPEC,
3169 HOSTNAME_RESOLVE_TIMEOUT,
3170 &process_hostname_ips,
3173 if (plugin->external_address != NULL)
3175 plugin->ext_dns = GNUNET_RESOLVER_ip_get (plugin->external_address,
3177 GNUNET_TIME_UNIT_MINUTES,
3178 &process_external_ip,
3186 * Exit point from the plugin.
3189 libgnunet_plugin_transport_tcp_done (void *cls)
3191 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3192 struct Plugin *plugin = api->cls;
3193 struct Session *session;
3194 struct LocalAddrList *lal;
3195 struct TCPProbeContext *tcp_probe;
3197 if (plugin->ext_dns != NULL)
3199 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
3200 plugin->ext_dns = NULL;
3202 while (NULL != (session = plugin->sessions))
3203 disconnect_session (session);
3204 if (NULL != plugin->hostname_dns)
3206 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
3207 plugin->hostname_dns = NULL;
3209 if (plugin->service != NULL)
3210 GNUNET_SERVICE_stop (plugin->service);
3212 GNUNET_SERVER_destroy (plugin->server);
3213 GNUNET_free (plugin->handlers);
3214 while (NULL != (lal = plugin->lal_head))
3216 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3219 if (lal->nat != NULL)
3220 GNUNET_NAT_unregister (lal->nat);
3221 GNUNET_free_non_null (lal->external_nat_address);
3224 while (NULL != (tcp_probe = plugin->probe_head))
3226 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3229 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3230 GNUNET_free (tcp_probe);
3233 if ((plugin->behind_nat == GNUNET_YES) &&
3234 (plugin->enable_nat_server == GNUNET_YES))
3236 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3237 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3238 GNUNET_OS_process_wait (plugin->server_proc);
3239 GNUNET_OS_process_close (plugin->server_proc);
3240 plugin->server_proc = NULL;
3242 GNUNET_free_non_null(plugin->bind_address);
3243 GNUNET_free_non_null(plugin->internal_address);
3244 GNUNET_free_non_null(plugin->external_address);
3245 GNUNET_free (plugin);
3250 /* end of plugin_transport_tcp.c */