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 (plugin->env->cfg,
646 &nat_port_map_callback,
652 * Check if the given address is in the list of 'local' addresses.
654 * @param plugin the plugin
655 * @param arg the address, either an IPv4 or an IPv6 IP address
656 * @param arg_size number of bytes in arg
657 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
660 check_local_addr (struct Plugin *plugin,
664 struct LocalAddrList *lal;
666 lal = plugin->lal_head;
669 if ( (lal->size == arg_size) &&
670 (0 == memcmp (&lal[1], arg, arg_size)) )
674 return GNUNET_SYSERR;
679 * Check if the given address is in the list of 'mapped' addresses.
681 * @param plugin the plugin
682 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
683 * @param arg_size number of bytes in arg
684 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
687 check_mapped_addr (struct Plugin *plugin,
691 struct LocalAddrList *lal;
693 lal = plugin->lal_head;
696 if ( (lal->ena_size == arg_size) &&
697 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
701 return GNUNET_SYSERR;
706 * Function called for a quick conversion of the binary address to
707 * a numeric address. Note that the caller must not free the
708 * address and that the next call to this function is allowed
709 * to override the address again.
711 * @param cls closure ('struct Plugin*')
712 * @param addr binary address
713 * @param addrlen length of the address
714 * @return string representing the same address
717 tcp_address_to_string (void *cls,
721 static char rbuf[INET6_ADDRSTRLEN + 12];
722 char buf[INET6_ADDRSTRLEN];
726 const struct IPv4TcpAddress *t4;
727 const struct IPv6TcpAddress *t6;
731 if (addrlen == sizeof (struct IPv6TcpAddress))
735 port = ntohs (t6->t6_port);
736 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
739 else if (addrlen == sizeof (struct IPv4TcpAddress))
743 port = ntohs (t4->t_port);
744 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
749 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
751 _("Unexpected address length: %u bytes\n"),
752 (unsigned int) addrlen);
756 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
758 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
761 GNUNET_snprintf (rbuf,
763 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
771 * Find the session handle for the given client.
773 * @param plugin the plugin
774 * @param client which client to find the session handle for
775 * @return NULL if no matching session exists
777 static struct Session *
778 find_session_by_client (struct Plugin *plugin,
779 const struct GNUNET_SERVER_Client *client)
783 ret = plugin->sessions;
784 while ((ret != NULL) && (client != ret->client))
791 * Create a new session. Also queues a welcome message.
793 * @param plugin the plugin
794 * @param target peer to connect to
795 * @param client client to use
796 * @param is_nat this a NAT session, we should wait for a client to
797 * connect to us from an address, then assign that to
799 * @return new session object
801 static struct Session *
802 create_session (struct Plugin *plugin,
803 const struct GNUNET_PeerIdentity *target,
804 struct GNUNET_SERVER_Client *client,
808 struct PendingMessage *pm;
809 struct WelcomeMessage welcome;
811 if (is_nat != GNUNET_YES)
812 GNUNET_assert (client != NULL);
814 GNUNET_assert (client == NULL);
816 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
818 "Creating new session for peer `%4s'\n",
819 GNUNET_i2s (target));
821 ret = GNUNET_malloc (sizeof (struct Session));
822 ret->last_activity = GNUNET_TIME_absolute_get ();
823 ret->plugin = plugin;
824 ret->is_nat = is_nat;
825 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
827 ret->next = plugin->sessions;
828 plugin->sessions = ret;
830 ret->client = client;
831 ret->target = *target;
832 ret->expecting_welcome = GNUNET_YES;
833 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
834 pm->msg = (const char*) &pm[1];
835 pm->message_size = sizeof (struct WelcomeMessage);
836 welcome.header.size = htons (sizeof (struct WelcomeMessage));
837 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
838 welcome.clientIdentity = *plugin->env->my_identity;
839 memcpy (&pm[1], &welcome, sizeof (welcome));
840 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
841 GNUNET_STATISTICS_update (plugin->env->stats,
842 gettext_noop ("# bytes currently in TCP buffers"),
845 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
846 ret->pending_messages_tail,
848 if (is_nat != GNUNET_YES)
849 GNUNET_STATISTICS_update (plugin->env->stats,
850 gettext_noop ("# TCP sessions active"),
858 * If we have pending messages, ask the server to
859 * transmit them (schedule the respective tasks, etc.)
861 * @param session for which session should we do this
863 static void process_pending_messages (struct Session *session);
867 * Function called to notify a client about the socket
868 * being ready to queue more data. "buf" will be
869 * NULL and "size" zero if the socket was closed for
870 * writing in the meantime.
873 * @param size number of bytes available in buf
874 * @param buf where the callee should write the message
875 * @return number of bytes written to buf
878 do_transmit (void *cls, size_t size, void *buf)
880 struct Session *session = cls;
881 struct GNUNET_PeerIdentity pid;
882 struct Plugin *plugin;
883 struct PendingMessage *pos;
884 struct PendingMessage *hd;
885 struct PendingMessage *tl;
886 struct GNUNET_TIME_Absolute now;
890 session->transmit_handle = NULL;
891 plugin = session->plugin;
895 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
897 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
898 GNUNET_i2s (&session->target));
900 /* timeout; cancel all messages that have already expired */
904 now = GNUNET_TIME_absolute_get ();
905 while ( (NULL != (pos = session->pending_messages_head)) &&
906 (pos->timeout.abs_value <= now.abs_value) )
908 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
909 session->pending_messages_tail,
912 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
914 "Failed to transmit %u byte message to `%4s'.\n",
916 GNUNET_i2s (&session->target));
918 ret += pos->message_size;
919 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
921 /* do this call before callbacks (so that if callbacks destroy
922 session, they have a chance to cancel actions done by this
924 process_pending_messages (session);
925 pid = session->target;
926 /* no do callbacks and do not use session again since
927 the callbacks may abort the session */
928 while (NULL != (pos = hd))
930 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
931 if (pos->transmit_cont != NULL)
932 pos->transmit_cont (pos->transmit_cont_cls,
933 &pid, GNUNET_SYSERR);
936 GNUNET_STATISTICS_update (plugin->env->stats,
937 gettext_noop ("# bytes currently in TCP buffers"),
940 GNUNET_STATISTICS_update (plugin->env->stats,
941 gettext_noop ("# bytes discarded by TCP (timeout)"),
946 /* copy all pending messages that would fit */
951 while (NULL != (pos = session->pending_messages_head))
953 if (ret + pos->message_size > size)
955 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
956 session->pending_messages_tail,
958 GNUNET_assert (size >= pos->message_size);
959 /* FIXME: this memcpy can be up to 7% of our total runtime */
960 memcpy (cbuf, pos->msg, pos->message_size);
961 cbuf += pos->message_size;
962 ret += pos->message_size;
963 size -= pos->message_size;
964 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
966 /* schedule 'continuation' before callbacks so that callbacks that
967 cancel everything don't cause us to use a session that no longer
969 process_pending_messages (session);
970 session->last_activity = GNUNET_TIME_absolute_get ();
971 pid = session->target;
972 /* we'll now call callbacks that may cancel the session; hence
973 we should not use 'session' after this point */
974 while (NULL != (pos = hd))
976 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
977 if (pos->transmit_cont != NULL)
978 pos->transmit_cont (pos->transmit_cont_cls,
982 GNUNET_assert (hd == NULL);
983 GNUNET_assert (tl == NULL);
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
987 "Transmitting %u bytes\n",
990 GNUNET_STATISTICS_update (plugin->env->stats,
991 gettext_noop ("# bytes currently in TCP buffers"),
994 GNUNET_STATISTICS_update (plugin->env->stats,
995 gettext_noop ("# bytes transmitted via TCP"),
1003 * If we have pending messages, ask the server to
1004 * transmit them (schedule the respective tasks, etc.)
1006 * @param session for which session should we do this
1009 process_pending_messages (struct Session *session)
1011 struct PendingMessage *pm;
1013 GNUNET_assert (session->client != NULL);
1014 if (session->transmit_handle != NULL)
1016 if (NULL == (pm = session->pending_messages_head))
1019 session->transmit_handle
1020 = GNUNET_SERVER_notify_transmit_ready (session->client,
1022 GNUNET_TIME_absolute_get_remaining
1024 &do_transmit, session);
1029 * Functions with this signature are called whenever we need
1030 * to close a session due to a disconnect or failure to
1031 * establish a connection.
1033 * @param session session to close down
1036 disconnect_session (struct Session *session)
1038 struct Session *prev;
1039 struct Session *pos;
1040 struct PendingMessage *pm;
1043 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1045 "Disconnecting from `%4s' at %s.\n",
1046 GNUNET_i2s (&session->target),
1047 (session->connect_addr != NULL) ?
1048 tcp_address_to_string (session->plugin,
1049 session->connect_addr,
1050 session->connect_alen) : "*");
1052 /* remove from session list */
1054 pos = session->plugin->sessions;
1055 while (pos != session)
1061 session->plugin->sessions = session->next;
1063 prev->next = session->next;
1065 /* clean up state */
1066 if (session->transmit_handle != NULL)
1068 GNUNET_CONNECTION_notify_transmit_ready_cancel
1069 (session->transmit_handle);
1070 session->transmit_handle = NULL;
1072 while (NULL != (pm = session->pending_messages_head))
1075 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1077 pm->transmit_cont != NULL
1078 ? "Could not deliver message to `%4s'.\n"
1079 : "Could not deliver message to `%4s', notifying.\n",
1080 GNUNET_i2s (&session->target));
1082 GNUNET_STATISTICS_update (session->plugin->env->stats,
1083 gettext_noop ("# bytes currently in TCP buffers"),
1084 - (int64_t) pm->message_size,
1086 GNUNET_STATISTICS_update (session->plugin->env->stats,
1087 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1090 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1091 session->pending_messages_tail,
1093 if (NULL != pm->transmit_cont)
1094 pm->transmit_cont (pm->transmit_cont_cls,
1095 &session->target, GNUNET_SYSERR);
1098 GNUNET_break (session->client != NULL);
1099 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1101 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1102 if (session->client != NULL)
1103 GNUNET_SERVER_receive_done (session->client,
1106 else if (session->client != NULL)
1107 GNUNET_SERVER_client_drop (session->client);
1108 GNUNET_STATISTICS_update (session->plugin->env->stats,
1109 gettext_noop ("# TCP sessions active"),
1112 GNUNET_free_non_null (session->connect_addr);
1114 session->plugin->env->session_end (session->plugin->env->cls,
1118 GNUNET_free (session);
1123 * Given two otherwise equivalent sessions, pick the better one.
1125 * @param s1 one session (also default)
1126 * @param s2 other session
1127 * @return "better" session (more active)
1129 static struct Session *
1130 select_better_session (struct Session *s1,
1137 if ( (s1->expecting_welcome == GNUNET_NO) &&
1138 (s2->expecting_welcome == GNUNET_YES) )
1140 if ( (s1->expecting_welcome == GNUNET_YES) &&
1141 (s2->expecting_welcome == GNUNET_NO) )
1143 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1145 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1147 if ( (GNUNET_YES == s1->inbound) &&
1148 (GNUNET_NO == s2->inbound) )
1150 if ( (GNUNET_NO == s1->inbound) &&
1151 (GNUNET_YES == s2->inbound) )
1158 * We learned about a peer (possibly behind NAT) so run the
1159 * gnunet-nat-client to send dummy ICMP responses.
1161 * @param plugin the plugin for this transport
1162 * @param sa the address of the peer (IPv4-only)
1165 run_gnunet_nat_client (struct Plugin *plugin,
1166 const struct sockaddr_in *sa)
1168 char inet4[INET_ADDRSTRLEN];
1169 char port_as_string[6];
1170 struct GNUNET_OS_Process *proc;
1172 if (plugin->internal_address == NULL)
1174 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1176 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1179 GNUNET_assert (sa->sin_family == AF_INET);
1180 if (NULL == inet_ntop (AF_INET,
1182 inet4, INET_ADDRSTRLEN))
1184 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1187 GNUNET_snprintf(port_as_string,
1188 sizeof (port_as_string),
1192 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1194 _("Running gnunet-nat-client %s %s %u\n"),
1195 plugin->internal_address,
1197 (unsigned int) plugin->adv_port);
1199 proc = GNUNET_OS_start_process (NULL,
1201 "gnunet-nat-client",
1202 "gnunet-nat-client",
1203 plugin->internal_address,
1209 /* we know that the gnunet-nat-client will terminate virtually
1211 GNUNET_OS_process_wait (proc);
1212 GNUNET_OS_process_close (proc);
1217 * Function that can be used by the transport service to transmit
1218 * a message using the plugin. Note that in the case of a
1219 * peer disconnecting, the continuation MUST be called
1220 * prior to the disconnect notification itself. This function
1221 * will be called with this peer's HELLO message to initiate
1222 * a fresh connection to another peer.
1224 * @param cls closure
1225 * @param target who should receive this message
1226 * @param msg the message to transmit
1227 * @param msgbuf_size number of bytes in 'msg'
1228 * @param priority how important is the message (most plugins will
1229 * ignore message priority and just FIFO)
1230 * @param timeout how long to wait at most for the transmission (does not
1231 * require plugins to discard the message after the timeout,
1232 * just advisory for the desired delay; most plugins will ignore
1234 * @param session which session must be used (or NULL for "any")
1235 * @param addr the address to use (can be NULL if the plugin
1236 * is "on its own" (i.e. re-use existing TCP connection))
1237 * @param addrlen length of the address in bytes
1238 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1239 * GNUNET_NO means the plugin may use any other address and
1240 * GNUNET_SYSERR means that only reliable existing
1241 * bi-directional connections should be used (regardless
1243 * @param cont continuation to call once the message has
1244 * been transmitted (or if the transport is ready
1245 * for the next transmission call; or if the
1246 * peer disconnected...); can be NULL
1247 * @param cont_cls closure for cont
1248 * @return number of bytes used (on the physical network, with overheads);
1249 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1250 * and does NOT mean that the message was not transmitted (DV and NAT)
1253 tcp_plugin_send (void *cls,
1254 const struct GNUNET_PeerIdentity *target,
1258 struct GNUNET_TIME_Relative timeout,
1259 struct Session *session,
1263 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1265 struct Plugin *plugin = cls;
1266 struct Session *cand_session;
1267 struct Session *next;
1268 struct PendingMessage *pm;
1269 struct GNUNET_CONNECTION_Handle *sa;
1273 struct sockaddr_in a4;
1274 struct sockaddr_in6 a6;
1275 const struct IPv4TcpAddress *t4;
1276 const struct IPv6TcpAddress *t6;
1277 unsigned int is_natd;
1279 GNUNET_STATISTICS_update (plugin->env->stats,
1280 gettext_noop ("# bytes TCP was asked to transmit"),
1283 /* FIXME: we could do this cheaper with a hash table
1284 where we could restrict the iteration to entries that match
1285 the target peer... */
1286 is_natd = GNUNET_NO;
1287 if (session == NULL)
1289 cand_session = NULL;
1290 next = plugin->sessions;
1291 while (NULL != (session = next))
1293 next = session->next;
1294 GNUNET_assert (session->client != NULL);
1295 if (0 != memcmp (target,
1297 sizeof (struct GNUNET_PeerIdentity)))
1299 if ( ( (GNUNET_SYSERR == force_address) &&
1300 (session->expecting_welcome == GNUNET_NO) ) ||
1301 (GNUNET_NO == force_address) )
1303 cand_session = select_better_session (cand_session,
1307 if (GNUNET_SYSERR == force_address)
1309 GNUNET_break (GNUNET_YES == force_address);
1315 if ( (addrlen != session->connect_alen) &&
1316 (session->is_nat == GNUNET_NO) )
1318 if ((0 != memcmp (session->connect_addr,
1320 addrlen)) && (session->is_nat == GNUNET_NO))
1322 cand_session = select_better_session (cand_session,
1325 session = cand_session;
1327 if ( (session == NULL) &&
1331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1333 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1334 GNUNET_i2s (target));
1336 GNUNET_STATISTICS_update (plugin->env->stats,
1337 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1342 if (session == NULL)
1344 if (addrlen == sizeof (struct IPv6TcpAddress))
1348 memset (&a6, 0, sizeof (a6));
1349 #if HAVE_SOCKADDR_IN_SIN_LEN
1350 a6.sin6_len = sizeof (a6);
1352 a6.sin6_family = AF_INET6;
1353 a6.sin6_port = t6->t6_port;
1354 if (t6->t6_port == 0)
1355 is_natd = GNUNET_YES;
1356 memcpy (&a6.sin6_addr,
1358 sizeof (struct in6_addr));
1362 else if (addrlen == sizeof (struct IPv4TcpAddress))
1366 memset (&a4, 0, sizeof (a4));
1367 #if HAVE_SOCKADDR_IN_SIN_LEN
1368 a4.sin_len = sizeof (a4);
1370 a4.sin_family = AF_INET;
1371 a4.sin_port = t4->t_port;
1372 if (t4->t_port == 0)
1373 is_natd = GNUNET_YES;
1374 a4.sin_addr.s_addr = t4->ipv4_addr;
1380 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1382 _("Address of unexpected length: %u\n"),
1388 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1389 return -1; /* NAT client only works with IPv4 addresses */
1392 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1393 (is_natd == GNUNET_YES) &&
1394 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1395 &target->hashPubKey)) )
1398 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1400 _("Found valid IPv4 NAT address (creating session)!\n"));
1402 session = create_session (plugin,
1407 /* create new message entry */
1408 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1409 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1410 pm->msg = (const char*) &pm[1];
1411 memcpy (&pm[1], msg, msgbuf_size);
1412 /* FIXME: this memcpy can be up to 7% of our total run-time
1413 (for transport service) */
1414 pm->message_size = msgbuf_size;
1415 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1416 pm->transmit_cont = cont;
1417 pm->transmit_cont_cls = cont_cls;
1419 /* append pm to pending_messages list */
1420 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1421 session->pending_messages_tail,
1422 session->pending_messages_tail,
1425 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1426 &target->hashPubKey,
1428 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1432 "Created NAT WAIT connection to `%4s' at `%s'\n",
1433 GNUNET_i2s (target),
1434 GNUNET_a2s (sb, sbs));
1436 run_gnunet_nat_client (plugin, &a4);
1439 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1440 (is_natd == GNUNET_YES) &&
1441 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1442 &target->hashPubKey)) )
1444 /* Only do one NAT punch attempt per peer identity */
1447 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1451 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1453 "Failed to create connection to `%4s' at `%s'\n",
1454 GNUNET_i2s (target),
1455 GNUNET_a2s (sb, sbs));
1457 GNUNET_STATISTICS_update (plugin->env->stats,
1458 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1464 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1466 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1467 GNUNET_i2s (target),
1468 GNUNET_a2s (sb, sbs));
1470 session = create_session (plugin,
1472 GNUNET_SERVER_connect_socket (plugin->server,
1475 session->connect_addr = GNUNET_malloc (addrlen);
1476 memcpy (session->connect_addr,
1479 session->connect_alen = addrlen;
1481 GNUNET_assert (session != NULL);
1482 GNUNET_assert (session->client != NULL);
1483 GNUNET_STATISTICS_update (plugin->env->stats,
1484 gettext_noop ("# bytes currently in TCP buffers"),
1487 /* create new message entry */
1488 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1489 pm->msg = (const char*) &pm[1];
1490 memcpy (&pm[1], msg, msgbuf_size);
1491 pm->message_size = msgbuf_size;
1492 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1493 pm->transmit_cont = cont;
1494 pm->transmit_cont_cls = cont_cls;
1496 /* append pm to pending_messages list */
1497 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1498 session->pending_messages_tail,
1499 session->pending_messages_tail,
1502 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1504 "Asked to transmit %u bytes to `%s', added message to list.\n",
1506 GNUNET_i2s (target));
1508 process_pending_messages (session);
1514 * Function that can be called to force a disconnect from the
1515 * specified neighbour. This should also cancel all previously
1516 * scheduled transmissions. Obviously the transmission may have been
1517 * partially completed already, which is OK. The plugin is supposed
1518 * to close the connection (if applicable) and no longer call the
1519 * transmit continuation(s).
1521 * Finally, plugin MUST NOT call the services's receive function to
1522 * notify the service that the connection to the specified target was
1523 * closed after a getting this call.
1525 * @param cls closure
1526 * @param target peer for which the last transmission is
1530 tcp_plugin_disconnect (void *cls,
1531 const struct GNUNET_PeerIdentity *target)
1533 struct Plugin *plugin = cls;
1534 struct Session *session;
1535 struct Session *next;
1536 struct PendingMessage *pm;
1539 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1541 "Asked to cancel session with `%4s'\n",
1542 GNUNET_i2s (target));
1544 next = plugin->sessions;
1545 while (NULL != (session = next))
1547 next = session->next;
1548 if (0 != memcmp (target,
1550 sizeof (struct GNUNET_PeerIdentity)))
1552 pm = session->pending_messages_head;
1555 pm->transmit_cont = NULL;
1556 pm->transmit_cont_cls = NULL;
1559 GNUNET_STATISTICS_update (session->plugin->env->stats,
1560 gettext_noop ("# transport-service disconnect requests for TCP"),
1563 disconnect_session (session);
1569 * Context for address to string conversion.
1571 struct PrettyPrinterContext
1574 * Function to call with the result.
1576 GNUNET_TRANSPORT_AddressStringCallback asc;
1579 * Clsoure for 'asc'.
1584 * Port to add after the IP address.
1591 * Append our port and forward the result.
1593 * @param cls the 'struct PrettyPrinterContext*'
1594 * @param hostname hostname part of the address
1597 append_port (void *cls, const char *hostname)
1599 struct PrettyPrinterContext *ppc = cls;
1602 if (hostname == NULL)
1604 ppc->asc (ppc->asc_cls, NULL);
1608 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1609 ppc->asc (ppc->asc_cls, ret);
1615 * Convert the transports address to a nice, human-readable
1618 * @param cls closure
1619 * @param type name of the transport that generated the address
1620 * @param addr one of the addresses of the host, NULL for the last address
1621 * the specific address format depends on the transport
1622 * @param addrlen length of the address
1623 * @param numeric should (IP) addresses be displayed in numeric form?
1624 * @param timeout after how long should we give up?
1625 * @param asc function to call on each string
1626 * @param asc_cls closure for asc
1629 tcp_plugin_address_pretty_printer (void *cls,
1634 struct GNUNET_TIME_Relative timeout,
1635 GNUNET_TRANSPORT_AddressStringCallback asc,
1638 struct Plugin *plugin = cls;
1639 struct PrettyPrinterContext *ppc;
1642 struct sockaddr_in a4;
1643 struct sockaddr_in6 a6;
1644 const struct IPv4TcpAddress *t4;
1645 const struct IPv6TcpAddress *t6;
1648 if (addrlen == sizeof (struct IPv6TcpAddress))
1651 memset (&a6, 0, sizeof (a6));
1652 a6.sin6_family = AF_INET6;
1653 a6.sin6_port = t6->t6_port;
1654 memcpy (&a6.sin6_addr,
1656 sizeof (struct in6_addr));
1657 port = ntohs (t6->t6_port);
1661 else if (addrlen == sizeof (struct IPv4TcpAddress))
1664 memset (&a4, 0, sizeof (a4));
1665 a4.sin_family = AF_INET;
1666 a4.sin_port = t4->t_port;
1667 a4.sin_addr.s_addr = t4->ipv4_addr;
1668 port = ntohs (t4->t_port);
1674 /* invalid address */
1675 GNUNET_break_op (0);
1676 asc (asc_cls, NULL);
1679 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1681 ppc->asc_cls = asc_cls;
1683 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1686 !numeric, timeout, &append_port, ppc);
1691 * Check if the given port is plausible (must be either our listen
1692 * port or our advertised port), or any port if we are behind NAT
1693 * and do not have a port open. If it is neither, we return
1696 * @param plugin global variables
1697 * @param in_port port number to check
1698 * @return GNUNET_OK if port is either open_port or adv_port
1701 check_port (struct Plugin *plugin,
1704 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1706 return GNUNET_SYSERR;
1711 * Function that will be called to check if a binary address for this
1712 * plugin is well-formed and corresponds to an address for THIS peer
1713 * (as per our configuration). Naturally, if absolutely necessary,
1714 * plugins can be a bit conservative in their answer, but in general
1715 * plugins should make sure that the address does not redirect
1716 * traffic to a 3rd party that might try to man-in-the-middle our
1719 * @param cls closure, our 'struct Plugin*'
1720 * @param addr pointer to the address
1721 * @param addrlen length of addr
1722 * @return GNUNET_OK if this is a plausible address for this peer
1723 * and transport, GNUNET_SYSERR if not
1726 tcp_plugin_check_address (void *cls,
1730 struct Plugin *plugin = cls;
1731 struct IPv4TcpAddress *v4;
1732 struct IPv6TcpAddress *v6;
1734 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1735 (addrlen != sizeof (struct IPv6TcpAddress)))
1737 GNUNET_break_op (0);
1738 return GNUNET_SYSERR;
1740 if (addrlen == sizeof (struct IPv4TcpAddress))
1742 v4 = (struct IPv4TcpAddress *) addr;
1744 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1747 check_port (plugin, ntohs (v4->t_port)))
1748 return GNUNET_SYSERR;
1750 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1751 return GNUNET_SYSERR;
1755 v6 = (struct IPv6TcpAddress *) addr;
1756 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1758 GNUNET_break_op (0);
1759 return GNUNET_SYSERR;
1762 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1765 check_port (plugin, ntohs (v6->t6_port)))
1766 return GNUNET_SYSERR;
1768 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1769 return GNUNET_SYSERR;
1776 * We've received a nat probe from this peer via TCP. Finish
1777 * creating the client session and resume sending of queued
1780 * @param cls closure
1781 * @param client identification of the client
1782 * @param message the actual message
1785 handle_tcp_nat_probe (void *cls,
1786 struct GNUNET_SERVER_Client *client,
1787 const struct GNUNET_MessageHeader *message)
1789 struct Plugin *plugin = cls;
1790 struct Session *session;
1791 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1794 struct IPv4TcpAddress *t4;
1795 struct IPv6TcpAddress *t6;
1796 const struct sockaddr_in *s4;
1797 const struct sockaddr_in6 *s6;
1800 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1802 "received tcp NAT probe\n");
1804 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1805 * a connection to this peer by running gnunet-nat-client. This peer
1806 * received the punch message and now wants us to use the new connection
1807 * as the default for that peer. Do so and then send a WELCOME message
1808 * so we can really be connected!
1810 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1815 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1816 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1817 &tcp_nat_probe->clientIdentity.hashPubKey);
1818 if (session == NULL)
1821 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1823 "Did NOT find session for NAT probe!\n");
1825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1829 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1831 "Found session for NAT probe!\n");
1833 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1834 &tcp_nat_probe->clientIdentity.hashPubKey,
1835 session) == GNUNET_YES);
1837 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1840 GNUNET_free (session);
1841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1845 GNUNET_SERVER_client_keep (client);
1846 session->client = client;
1847 session->last_activity = GNUNET_TIME_absolute_get ();
1848 session->inbound = GNUNET_NO;
1851 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1853 "Found address `%s' for incoming connection\n",
1854 GNUNET_a2s (vaddr, alen));
1856 switch (((const struct sockaddr *)vaddr)->sa_family)
1860 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1861 t4->t_port = s4->sin_port;
1862 t4->ipv4_addr = s4->sin_addr.s_addr;
1863 session->connect_addr = t4;
1864 session->connect_alen = sizeof (struct IPv4TcpAddress);
1868 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1869 t6->t6_port = s6->sin6_port;
1870 memcpy (&t6->ipv6_addr,
1872 sizeof (struct in6_addr));
1873 session->connect_addr = t6;
1874 session->connect_alen = sizeof (struct IPv6TcpAddress);
1877 GNUNET_break_op (0);
1879 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1881 "Bad address for incoming connection!\n");
1883 GNUNET_free (vaddr);
1884 GNUNET_SERVER_client_drop (client);
1885 GNUNET_free (session);
1886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1889 GNUNET_free (vaddr);
1891 session->next = plugin->sessions;
1892 plugin->sessions = session;
1893 GNUNET_STATISTICS_update (plugin->env->stats,
1894 gettext_noop ("# TCP sessions active"),
1897 process_pending_messages (session);
1898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1903 * We've received a welcome from this peer via TCP. Possibly create a
1904 * fresh client record and send back our welcome.
1906 * @param cls closure
1907 * @param client identification of the client
1908 * @param message the actual message
1911 handle_tcp_welcome (void *cls,
1912 struct GNUNET_SERVER_Client *client,
1913 const struct GNUNET_MessageHeader *message)
1915 struct Plugin *plugin = cls;
1916 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1917 struct Session *session;
1920 struct IPv4TcpAddress *t4;
1921 struct IPv6TcpAddress *t6;
1922 const struct sockaddr_in *s4;
1923 const struct sockaddr_in6 *s6;
1926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1928 "Received %s message from `%4s'.\n",
1930 GNUNET_i2s (&wm->clientIdentity));
1932 GNUNET_STATISTICS_update (plugin->env->stats,
1933 gettext_noop ("# TCP WELCOME messages received"),
1936 session = find_session_by_client (plugin, client);
1938 if (session == NULL)
1941 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1943 "Received %s message from a `%4s', creating new session\n",
1945 GNUNET_i2s (&wm->clientIdentity));
1947 GNUNET_SERVER_client_keep (client);
1948 session = create_session (plugin,
1949 &wm->clientIdentity,
1952 session->inbound = GNUNET_YES;
1954 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1957 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1959 "Found address `%s' for incoming connection\n",
1960 GNUNET_a2s (vaddr, alen));
1962 if (alen == sizeof (struct sockaddr_in))
1965 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1966 t4->t_port = s4->sin_port;
1967 t4->ipv4_addr = s4->sin_addr.s_addr;
1968 session->connect_addr = t4;
1969 session->connect_alen = sizeof (struct IPv4TcpAddress);
1971 else if (alen == sizeof (struct sockaddr_in6))
1974 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1975 t6->t6_port = s6->sin6_port;
1976 memcpy (&t6->ipv6_addr,
1978 sizeof (struct in6_addr));
1979 session->connect_addr = t6;
1980 session->connect_alen = sizeof (struct IPv6TcpAddress);
1983 GNUNET_free (vaddr);
1988 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1990 "Did not obtain TCP socket address for incoming connection\n");
1993 process_pending_messages (session);
1999 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2001 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2003 "Found address `%s' (already have session)\n",
2004 GNUNET_a2s (vaddr, alen));
2005 GNUNET_free (vaddr);
2010 if (session->expecting_welcome != GNUNET_YES)
2012 GNUNET_break_op (0);
2013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2016 session->last_activity = GNUNET_TIME_absolute_get ();
2017 session->expecting_welcome = GNUNET_NO;
2018 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2023 * Task to signal the server that we can continue
2024 * receiving from the TCP client now.
2026 * @param cls the 'struct Session*'
2027 * @param tc task context (unused)
2030 delayed_done (void *cls,
2031 const struct GNUNET_SCHEDULER_TaskContext *tc)
2033 struct Session *session = cls;
2034 struct GNUNET_TIME_Relative delay;
2036 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2037 delay = session->plugin->env->receive (session->plugin->env->cls,
2043 if (delay.rel_value == 0)
2044 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2046 session->receive_delay_task =
2047 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2052 * We've received data for this peer via TCP. Unbox,
2053 * compute latency and forward.
2055 * @param cls closure
2056 * @param client identification of the client
2057 * @param message the actual message
2060 handle_tcp_data (void *cls,
2061 struct GNUNET_SERVER_Client *client,
2062 const struct GNUNET_MessageHeader *message)
2064 struct Plugin *plugin = cls;
2065 struct Session *session;
2066 struct GNUNET_TIME_Relative delay;
2069 type = ntohs (message->type);
2070 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2071 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2073 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2074 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2077 session = find_session_by_client (plugin, client);
2078 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2083 session->last_activity = GNUNET_TIME_absolute_get ();
2085 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2087 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2088 (unsigned int) ntohs (message->size),
2089 (unsigned int) ntohs (message->type),
2090 GNUNET_i2s (&session->target));
2092 GNUNET_STATISTICS_update (plugin->env->stats,
2093 gettext_noop ("# bytes received via TCP"),
2094 ntohs (message->size),
2096 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2097 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2098 distance[0].value = htonl (1);
2099 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2100 distance[1].value = htonl (0);
2101 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2102 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2105 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2106 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2107 if (delay.rel_value == 0)
2109 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2114 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2116 "Throttling receiving from `%s' for %llu ms\n",
2117 GNUNET_i2s (&session->target),
2118 (unsigned long long) delay.rel_value);
2120 GNUNET_SERVER_disable_receive_done_warning (client);
2121 session->receive_delay_task =
2122 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2128 * Functions with this signature are called whenever a peer
2129 * is disconnected on the network level.
2131 * @param cls closure
2132 * @param client identification of the client
2135 disconnect_notify (void *cls,
2136 struct GNUNET_SERVER_Client *client)
2138 struct Plugin *plugin = cls;
2139 struct Session *session;
2143 session = find_session_by_client (plugin, client);
2144 if (session == NULL)
2145 return; /* unknown, nothing to do */
2147 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2149 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2150 GNUNET_i2s (&session->target),
2151 (session->connect_addr != NULL) ?
2152 tcp_address_to_string (session->plugin,
2153 session->connect_addr,
2154 session->connect_alen) : "*");
2156 GNUNET_STATISTICS_update (session->plugin->env->stats,
2157 gettext_noop ("# network-level TCP disconnect events"),
2160 disconnect_session (session);
2164 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
2167 int local = GNUNET_NO;
2168 int af = addr->sa_family;
2173 uint32_t netmask = 0x7F000000;
2174 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
2175 res = (address >> 24) ^ (netmask >> 24);
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2188 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
2189 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2204 * Add the IP of our network interface to the list of
2205 * our internal IP addresses.
2207 * @param cls the 'struct Plugin*'
2208 * @param name name of the interface
2209 * @param isDefault do we think this may be our default interface
2210 * @param addr address of the interface
2211 * @param addrlen number of bytes in addr
2212 * @return GNUNET_OK to continue iterating
2215 process_interfaces (void *cls,
2218 const struct sockaddr *addr, socklen_t addrlen)
2220 struct Plugin *plugin = cls;
2222 struct IPv4TcpAddress t4;
2223 struct IPv6TcpAddress t6;
2224 struct IPv4TcpAddress t4_nat;
2225 struct IPv6TcpAddress t6_nat;
2229 char buf[INET6_ADDRSTRLEN];
2231 af = addr->sa_family;
2234 if (plugin->use_localaddresses == GNUNET_NO)
2236 if (GNUNET_YES == check_localaddress (addr, addrlen))
2239 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2241 "Not notifying transport of address `%s' (local address)\n",
2242 GNUNET_a2s (addr, addrlen));
2251 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2252 GNUNET_assert (NULL != inet_ntop(AF_INET,
2256 if ( (plugin->bind_address != NULL) &&
2257 (0 != strcmp(buf, plugin->bind_address)) )
2260 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2262 "Not notifying transport of address `%s' (does not match bind address)\n",
2263 GNUNET_a2s (addr, addrlen));
2267 if ( (plugin->internal_address == NULL) &&
2269 plugin->internal_address = GNUNET_strdup (buf);
2270 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2271 if (plugin->behind_nat == GNUNET_YES)
2273 /* Also advertise as NAT (with port 0) */
2274 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2275 t4_nat.t_port = htons(0);
2278 t4.t_port = htons (plugin->adv_port);
2283 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2284 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2288 /* skip link local addresses */
2291 memcpy (&t6.ipv6_addr,
2292 &((struct sockaddr_in6 *) addr)->sin6_addr,
2293 sizeof (struct in6_addr));
2295 /* check bind address */
2296 GNUNET_assert (NULL != inet_ntop(AF_INET6,
2301 if ( (plugin->bind_address != NULL) &&
2302 (0 != strcmp(buf, plugin->bind_address)) )
2305 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2307 "Not notifying transport of address `%s' (does not match bind address)\n",
2308 GNUNET_a2s (addr, addrlen));
2313 add_to_address_list (plugin,
2315 sizeof (struct in6_addr));
2316 if (plugin->behind_nat == GNUNET_YES)
2318 /* Also advertise as NAT (with port 0) */
2319 memcpy (&t6_nat.ipv6_addr,
2320 &((struct sockaddr_in6 *) addr)->sin6_addr,
2321 sizeof (struct in6_addr));
2322 t6_nat.t6_port = htons(0);
2325 t6.t6_port = htons (plugin->adv_port);
2333 if (plugin->adv_port != 0)
2336 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2338 "Found address `%s' (%s) len %d\n",
2339 GNUNET_a2s (addr, addrlen), name, args);
2341 plugin->env->notify_address (plugin->env->cls,
2343 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2346 if (arg_nat != NULL)
2348 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2350 _("Found address `%s' (%s) len %d\n"),
2351 GNUNET_a2s (addr, addrlen), name, args);
2352 plugin->env->notify_address (plugin->env->cls,
2354 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2362 * Function called by the resolver for each address obtained from DNS
2363 * for our own hostname. Add the addresses to the list of our
2364 * external IP addresses.
2366 * @param cls closure
2367 * @param addr one of the addresses of the host, NULL for the last address
2368 * @param addrlen length of the address
2371 process_hostname_ips (void *cls,
2372 const struct sockaddr *addr, socklen_t addrlen)
2374 struct Plugin *plugin = cls;
2378 plugin->hostname_dns = NULL;
2381 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2382 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2387 * We can now send a probe message, copy into buffer to really send.
2389 * @param cls closure, a struct TCPProbeContext
2390 * @param size max size to copy
2391 * @param buf buffer to copy message to
2392 * @return number of bytes copied into buf
2395 notify_send_probe (void *cls,
2399 struct TCPProbeContext *tcp_probe_ctx = cls;
2400 struct Plugin *plugin = tcp_probe_ctx->plugin;
2403 tcp_probe_ctx->transmit_handle = NULL;
2404 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2409 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2410 GNUNET_free(tcp_probe_ctx);
2413 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2414 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2415 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2416 tcp_probe_ctx->sock);
2417 ret = sizeof(tcp_probe_ctx->message);
2418 GNUNET_free(tcp_probe_ctx);
2424 * We have been notified that gnunet-nat-server has written something to stdout.
2425 * Handle the output, then reschedule this function to be called again once
2426 * more is available.
2428 * @param cls the plugin handle
2429 * @param tc the scheduling context
2432 tcp_plugin_server_read (void *cls,
2433 const struct GNUNET_SCHEDULER_TaskContext *tc)
2435 struct Plugin *plugin = cls;
2440 const char *port_start;
2441 struct sockaddr_in sin_addr;
2442 struct TCPProbeContext *tcp_probe_ctx;
2443 struct GNUNET_CONNECTION_Handle *sock;
2445 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2447 memset (mybuf, 0, sizeof(mybuf));
2448 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2454 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2456 "Finished reading from server stdout with code: %d\n",
2459 /* FIXME: consider process_wait here? */
2464 for (i = 0; i < sizeof(mybuf); i++)
2466 if (mybuf[i] == '\n')
2471 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2474 port_start = &mybuf[i + 1];
2478 /* construct socket address of sender */
2479 memset (&sin_addr, 0, sizeof (sin_addr));
2480 sin_addr.sin_family = AF_INET;
2481 #if HAVE_SOCKADDR_IN_SIN_LEN
2482 sin_addr.sin_len = sizeof (sin_addr);
2484 if ( (NULL == port_start) ||
2485 (1 != sscanf (port_start, "%d", &port)) ||
2486 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2488 /* should we restart gnunet-nat-server? */
2489 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2491 _("gnunet-nat-server generated malformed address `%s'\n"),
2493 plugin->server_read_task
2494 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2495 plugin->server_stdout_handle,
2496 &tcp_plugin_server_read,
2500 sin_addr.sin_port = htons((uint16_t) port);
2502 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2504 "gnunet-nat-server read: %s:%d\n",
2509 * We have received an ICMP response, ostensibly from a peer
2510 * that wants to connect to us! Send a message to establish a connection.
2512 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2513 (const struct sockaddr *)&sin_addr,
2517 /* failed for some odd reason (out of sockets?); ignore attempt */
2518 plugin->server_read_task =
2519 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2520 plugin->server_stdout_handle,
2521 &tcp_plugin_server_read,
2526 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2527 "Sending TCP probe message to `%s:%u'!\n",
2529 (unsigned int) port);
2530 /* FIXME: do we need to track these probe context objects so that
2531 we can clean them up on plugin unload? */
2533 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2534 tcp_probe_ctx->message.header.size
2535 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2536 tcp_probe_ctx->message.header.type
2537 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2538 memcpy (&tcp_probe_ctx->message.clientIdentity,
2539 plugin->env->my_identity,
2540 sizeof(struct GNUNET_PeerIdentity));
2541 tcp_probe_ctx->plugin = plugin;
2542 tcp_probe_ctx->sock = sock;
2543 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2546 tcp_probe_ctx->transmit_handle
2547 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2548 ntohs (tcp_probe_ctx->message.header.size),
2549 GNUNET_TIME_UNIT_FOREVER_REL,
2550 ¬ify_send_probe, tcp_probe_ctx);
2552 plugin->server_read_task =
2553 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2554 plugin->server_stdout_handle,
2555 &tcp_plugin_server_read,
2561 * Start the gnunet-nat-server process for users behind NAT.
2563 * @param plugin the transport plugin
2564 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2567 tcp_transport_start_nat_server (struct Plugin *plugin)
2569 if (plugin->internal_address == NULL)
2570 return GNUNET_SYSERR;
2571 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2574 if (plugin->server_stdout == NULL)
2575 return GNUNET_SYSERR;
2577 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2579 "Starting %s %s\n", "gnunet-nat-server", plugin->internal_address);
2581 /* Start the server process */
2582 plugin->server_proc = GNUNET_OS_start_process (NULL,
2583 plugin->server_stdout,
2584 "gnunet-nat-server",
2585 "gnunet-nat-server",
2586 plugin->internal_address,
2588 if (plugin->server_proc == NULL)
2590 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2592 _("Failed to start %s\n"),
2593 "gnunet-nat-server");
2594 GNUNET_DISK_pipe_close (plugin->server_stdout);
2595 plugin->server_stdout = NULL;
2596 return GNUNET_SYSERR;
2598 /* Close the write end of the read pipe */
2599 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2600 GNUNET_DISK_PIPE_END_WRITE);
2601 plugin->server_stdout_handle
2602 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2603 GNUNET_DISK_PIPE_END_READ);
2604 plugin->server_read_task
2605 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2606 plugin->server_stdout_handle,
2607 &tcp_plugin_server_read,
2614 * Return the actual path to a file found in the current
2615 * PATH environment variable.
2617 * @param binary the name of the file to find
2618 * @return path to binary, NULL if not found
2621 get_path_from_PATH (const char *binary)
2629 p = getenv ("PATH");
2632 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2634 _("PATH environment variable is unset.\n"));
2637 path = GNUNET_strdup (p); /* because we write on it */
2638 buf = GNUNET_malloc (strlen (path) + 20);
2641 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2644 sprintf (buf, "%s/%s", pos, binary);
2645 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2652 sprintf (buf, "%s/%s", pos, binary);
2653 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2665 * Check whether the suid bit is set on a file.
2666 * Attempts to find the file using the current
2667 * PATH environment variable as a search path.
2669 * @param binary the name of the file to check
2670 * @return GNUNET_YES if the file is SUID,
2672 * GNUNET_SYSERR on error
2675 check_gnunet_nat_binary (const char *binary)
2677 struct stat statbuf;
2683 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2684 p = get_path_from_PATH (binaryexe);
2687 p = get_path_from_PATH (binary);
2691 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2693 _("Could not find binary `%s' in PATH!\n"),
2697 if (0 != STAT (p, &statbuf))
2699 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2700 _("stat (%s) failed: %s\n"),
2704 return GNUNET_SYSERR;
2708 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2709 (statbuf.st_uid == 0) )
2713 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2714 if (INVALID_SOCKET == rawsock)
2716 DWORD err = GetLastError ();
2717 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2719 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2720 return GNUNET_NO; /* not running as administrator */
2722 closesocket (rawsock);
2729 * Our (external) hostname was resolved.
2731 * @param cls the 'struct Plugin'
2732 * @param addr NULL on error, otherwise result of DNS lookup
2733 * @param addrlen number of bytes in addr
2736 process_external_ip (void *cls,
2737 const struct sockaddr *addr,
2740 struct Plugin *plugin = cls;
2741 const struct sockaddr_in *s;
2742 struct IPv4TcpAddress t4;
2743 char buf[INET_ADDRSTRLEN];
2745 plugin->ext_dns = NULL;
2748 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2749 s = (const struct sockaddr_in *) addr;
2750 t4.ipv4_addr = s->sin_addr.s_addr;
2751 if ( (plugin->behind_nat == GNUNET_YES) &&
2752 (plugin->enable_nat_server == GNUNET_YES) )
2754 t4.t_port = htons(0);
2755 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2757 "Notifying transport of address %s:%d\n",
2758 plugin->external_address,
2763 t4.t_port = htons(plugin->adv_port);
2764 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2766 "Notifying transport of address %s:%d\n",
2767 plugin->external_address,
2768 (int) plugin->adv_port);
2771 if ((plugin->bind_address != NULL) && (plugin->behind_nat == GNUNET_NO))
2773 GNUNET_assert (NULL != inet_ntop(AF_INET,
2777 if (0 != strcmp (plugin->bind_address, buf))
2779 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2781 "NAT is not enabled and specific bind address `%s' differs from external address `%s'! Not notifying about external address `%s'\n",
2782 plugin->bind_address,
2783 plugin->external_address,
2784 plugin->external_address);
2789 add_to_address_list (plugin,
2791 sizeof (struct in_addr));
2793 plugin->env->notify_address (plugin->env->cls,
2796 GNUNET_TIME_UNIT_FOREVER_REL);
2801 * Entry point for the plugin.
2803 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2804 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2807 libgnunet_plugin_transport_tcp_init (void *cls)
2809 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2810 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2811 sizeof (struct WelcomeMessage)},
2812 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2813 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2816 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2817 struct GNUNET_TRANSPORT_PluginFunctions *api;
2818 struct Plugin *plugin;
2819 struct GNUNET_SERVICE_Context *service;
2820 unsigned long long aport;
2821 unsigned long long bport;
2825 int enable_nat_client;
2826 int enable_nat_server;
2828 int use_localaddresses;
2829 char *internal_address;
2830 char *external_address;
2832 struct sockaddr_in in_addr;
2833 struct GNUNET_TIME_Relative idle_timeout;
2835 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2838 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2841 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2843 "ENABLE_NAT_CLIENT");
2844 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2846 "ENABLE_NAT_SERVER");
2847 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2851 if ( (GNUNET_YES == enable_nat_server) &&
2852 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2854 enable_nat_server = GNUNET_NO;
2855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2856 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2857 "gnunet-nat-server");
2860 if ( (GNUNET_YES == enable_nat_client) &&
2861 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2863 enable_nat_client = GNUNET_NO;
2864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2865 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2866 "gnunet-nat-client");
2869 external_address = NULL;
2871 GNUNET_CONFIGURATION_have_value (env->cfg,
2873 "EXTERNAL_ADDRESS"))
2875 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2881 if ( (external_address != NULL) &&
2882 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2885 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2887 _("Malformed %s `%s' given in configuration!\n"),
2892 if ( (external_address == NULL) &&
2893 (nat_punched == GNUNET_YES) )
2895 nat_punched = GNUNET_NO;
2896 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2897 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2898 "EXTERNAL_ADDRESS");
2901 if (GNUNET_YES == nat_punched)
2903 enable_nat_server = GNUNET_NO;
2904 enable_upnp = GNUNET_NO;
2907 bind_address = NULL;
2908 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2913 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2915 _("Binding TCP plugin to specific address: `%s'\n"),
2919 internal_address = NULL;
2921 GNUNET_CONFIGURATION_have_value (env->cfg,
2923 "INTERNAL_ADDRESS"))
2925 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2931 if ( (internal_address != NULL) &&
2932 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2934 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2936 _("Malformed %s `%s' given in configuration!\n"),
2939 GNUNET_free_non_null(internal_address);
2940 GNUNET_free_non_null(external_address);
2944 if ((bind_address != NULL) && (internal_address != NULL))
2946 if (0 != strcmp(internal_address, bind_address ))
2948 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2950 "Specific bind address `%s' and internal address `%s' must not differ, forcing internal address to bind address!\n",
2951 bind_address, internal_address);
2952 GNUNET_free (internal_address);
2953 internal_address = bind_address;
2954 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2955 "tcp","New internal address `%s'\n", internal_address);
2961 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2967 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2973 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2975 _("Require valid port number for service `%s' in configuration!\n"),
2977 GNUNET_free_non_null(external_address);
2978 GNUNET_free_non_null(internal_address);
2982 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2985 if (use_localaddresses == GNUNET_SYSERR)
2986 use_localaddresses = GNUNET_NO;
2995 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2996 if (service == NULL)
2998 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
3000 _("Failed to start service.\n"));
3007 plugin = GNUNET_malloc (sizeof (struct Plugin));
3008 plugin->open_port = bport;
3009 plugin->adv_port = aport;
3010 plugin->bind_address = bind_address;
3011 plugin->external_address = external_address;
3012 plugin->internal_address = internal_address;
3013 plugin->behind_nat = behind_nat;
3014 plugin->nat_punched = nat_punched;
3015 plugin->enable_nat_client = enable_nat_client;
3016 plugin->enable_nat_server = enable_nat_server;
3017 plugin->enable_upnp = enable_upnp;
3018 plugin->use_localaddresses = use_localaddresses;
3020 plugin->lsock = NULL;
3021 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3023 api->send = &tcp_plugin_send;
3024 api->disconnect = &tcp_plugin_disconnect;
3025 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3026 api->check_address = &tcp_plugin_check_address;
3027 api->address_to_string = &tcp_address_to_string;
3028 plugin->service = service;
3029 if (service != NULL)
3031 plugin->server = GNUNET_SERVICE_get_server (service);
3036 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3041 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3043 _("Failed to find option %s in section %s!\n"),
3046 GNUNET_free_non_null(external_address);
3047 GNUNET_free_non_null(internal_address);
3051 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
3052 idle_timeout, GNUNET_YES);
3054 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3055 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
3057 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
3059 plugin->handlers[i].callback_cls = plugin;
3060 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
3061 GNUNET_SERVER_disconnect_notify (plugin->server,
3064 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3066 if ( (plugin->behind_nat == GNUNET_YES) &&
3067 (plugin->enable_nat_server == GNUNET_YES) &&
3068 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
3070 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3072 _("Failed to start %s required for NAT in %s!\n"),
3075 GNUNET_free_non_null(external_address);
3076 GNUNET_free_non_null(internal_address);
3077 if (service != NULL)
3078 GNUNET_SERVICE_stop (service);
3080 GNUNET_SERVER_destroy (plugin->server);
3085 if (enable_nat_client == GNUNET_YES)
3087 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
3088 GNUNET_assert (plugin->nat_wait_conns != NULL);
3092 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3094 _("TCP transport listening on port %llu\n"),
3097 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3099 _("TCP transport not listening on any port (client only)\n"));
3101 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3103 _("TCP transport advertises itself as being on port %llu\n"),
3106 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
3108 HOSTNAME_RESOLVE_TIMEOUT,
3109 &process_hostname_ips,
3112 if (plugin->external_address != NULL)
3114 plugin->ext_dns = GNUNET_RESOLVER_ip_get (env->cfg,
3115 plugin->external_address,
3117 GNUNET_TIME_UNIT_MINUTES,
3118 &process_external_ip,
3126 * Exit point from the plugin.
3129 libgnunet_plugin_transport_tcp_done (void *cls)
3131 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3132 struct Plugin *plugin = api->cls;
3133 struct Session *session;
3134 struct LocalAddrList *lal;
3135 struct TCPProbeContext *tcp_probe;
3137 if (plugin->ext_dns != NULL)
3139 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
3140 plugin->ext_dns = NULL;
3142 while (NULL != (session = plugin->sessions))
3143 disconnect_session (session);
3144 if (NULL != plugin->hostname_dns)
3146 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
3147 plugin->hostname_dns = NULL;
3149 if (plugin->service != NULL)
3150 GNUNET_SERVICE_stop (plugin->service);
3152 GNUNET_SERVER_destroy (plugin->server);
3153 GNUNET_free (plugin->handlers);
3154 while (NULL != (lal = plugin->lal_head))
3156 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3159 if (lal->nat != NULL)
3160 GNUNET_NAT_unregister (lal->nat);
3161 GNUNET_free_non_null (lal->external_nat_address);
3164 while (NULL != (tcp_probe = plugin->probe_head))
3166 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3169 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3170 GNUNET_free (tcp_probe);
3173 if ((plugin->behind_nat == GNUNET_YES) &&
3174 (plugin->enable_nat_server == GNUNET_YES))
3176 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3177 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3178 GNUNET_OS_process_wait (plugin->server_proc);
3179 GNUNET_OS_process_close (plugin->server_proc);
3180 plugin->server_proc = NULL;
3182 GNUNET_free_non_null(plugin->bind_address);
3183 GNUNET_free (plugin);
3188 /* end of plugin_transport_tcp.c */