2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_connection_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
43 #define DEBUG_TCP_NAT GNUNET_YES
46 * How long until we give up on transmitting the welcome message?
48 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52 * Initial handshake message for a session.
57 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
59 struct GNUNET_MessageHeader header;
62 * Identity of the node connecting (TCP client)
64 struct GNUNET_PeerIdentity clientIdentity;
70 * Basically a WELCOME message, but with the purpose
71 * of giving the waiting peer a client handle to use
73 struct TCP_NAT_ProbeMessage
76 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
78 struct GNUNET_MessageHeader header;
81 * Identity of the sender of the message.
83 struct GNUNET_PeerIdentity clientIdentity;
89 * Context for sending a NAT probe via TCP.
91 struct TCPProbeContext
95 * Active probes are kept in a DLL.
97 struct TCPProbeContext *next;
100 * Active probes are kept in a DLL.
102 struct TCPProbeContext *prev;
107 struct GNUNET_CONNECTION_Handle *sock;
110 * Message to be sent.
112 struct TCP_NAT_ProbeMessage message;
115 * Handle to the transmission.
117 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
120 * Transport plugin handle.
122 struct Plugin *plugin;
127 * Network format for IPv4 addresses.
129 struct IPv4TcpAddress
132 * IPv4 address, in network byte order.
134 uint32_t ipv4_addr GNUNET_PACKED;
137 * Port number, in network byte order.
139 uint16_t t_port GNUNET_PACKED;
145 * Network format for IPv6 addresses.
147 struct IPv6TcpAddress
152 struct in6_addr ipv6_addr GNUNET_PACKED;
155 * Port number, in network byte order.
157 uint16_t t6_port GNUNET_PACKED;
163 * Encapsulation of all of the state of the plugin.
169 * Local network addresses (actual IP address follows this struct).
170 * PORT is NOT included!
176 * This is a doubly linked list.
178 struct LocalAddrList *next;
181 * This is a doubly linked list.
183 struct LocalAddrList *prev;
188 struct Plugin *plugin;
191 * Handle to NAT holes we've tried to punch for this address.
193 struct GNUNET_NAT_Handle *nat;
196 * Pointer to a 'struct IPv4/V6TcpAddress' describing our external IP and port
197 * as obtained from the NAT by automatic port mapping.
199 void *external_nat_address;
202 * Number of bytes in 'external_nat_address'
207 * Number of bytes of the address that follow
215 * Information kept for each message that is yet to
218 struct PendingMessage
222 * This is a doubly-linked list.
224 struct PendingMessage *next;
227 * This is a doubly-linked list.
229 struct PendingMessage *prev;
232 * The pending message
237 * Continuation function to call once the message
238 * has been sent. Can be NULL if there is no
239 * continuation to call.
241 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
244 * Closure for transmit_cont.
246 void *transmit_cont_cls;
249 * Timeout value for the pending message.
251 struct GNUNET_TIME_Absolute timeout;
254 * So that the gnunet-service-transport can group messages together,
255 * these pending messages need to accept a message buffer and size
256 * instead of just a GNUNET_MessageHeader.
264 * Session handle for TCP connections.
272 struct SessionHeader header;
275 * Stored in a linked list.
277 struct Session *next;
280 * Pointer to the global plugin struct.
282 struct Plugin *plugin;
285 * The client (used to identify this connection)
287 struct GNUNET_SERVER_Client *client;
290 * Messages currently pending for transmission
291 * to this peer, if any.
293 struct PendingMessage *pending_messages_head;
296 * Messages currently pending for transmission
297 * to this peer, if any.
299 struct PendingMessage *pending_messages_tail;
302 * Handle for pending transmission request.
304 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
307 * To whom are we talking to (set to our identity
308 * if we are still waiting for the welcome message)
310 struct GNUNET_PeerIdentity target;
313 * ID of task used to delay receiving more to throttle sender.
315 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
318 * Address of the other peer (either based on our 'connect'
319 * call or on our 'accept' call).
324 * Last activity on this connection. Used to select preferred
327 struct GNUNET_TIME_Absolute last_activity;
330 * Length of connect_addr.
335 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
337 int expecting_welcome;
340 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
345 * Was this session created using NAT traversal?
353 * Encapsulation of all of the state of the plugin.
360 struct GNUNET_TRANSPORT_PluginEnvironment *env;
365 struct GNUNET_CONNECTION_Handle *lsock;
368 * stdout pipe handle for the gnunet-nat-server process
370 struct GNUNET_DISK_PipeHandle *server_stdout;
373 * stdout file handle (for reading) for the gnunet-nat-server process
375 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
378 * ID of select gnunet-nat-server stdout read task
380 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
383 * The process id of the server process (if behind NAT)
385 struct GNUNET_OS_Process *server_proc;
388 * List of open TCP sessions.
390 struct Session *sessions;
393 * Handle to the network service.
395 struct GNUNET_SERVICE_Context *service;
398 * Handle to the server for this service.
400 struct GNUNET_SERVER_Handle *server;
403 * Copy of the handler array where the closures are
404 * set to this struct's instance.
406 struct GNUNET_SERVER_MessageHandler *handlers;
409 * Handle for request of hostname resolution, non-NULL if pending.
411 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
414 * Map of peers we have tried to contact behind a NAT
416 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
419 * The external address given to us by the user. Used for HELLOs
420 * and address validation.
422 char *external_address;
425 * The internal address given to us by the user (or discovered).
426 * Used for NAT traversal (ICMP method), but not as a 'validateable'
429 char *internal_address;
432 * Address given for us to bind to (ONLY).
437 * use local addresses?
439 int use_localaddresses;
442 * List of our IP addresses.
444 struct LocalAddrList *lal_head;
447 * Tail of our IP address list.
449 struct LocalAddrList *lal_tail;
452 * List of active TCP probes.
454 struct TCPProbeContext *probe_head;
457 * List of active TCP probes.
459 struct TCPProbeContext *probe_tail;
462 * Handle for (DYN)DNS lookup of our external IP.
464 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
467 * ID of task used to update our addresses when one expires.
469 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
472 * Port that we are actually listening on.
477 * Port that the user said we would have visible to the
483 * Is this transport configured to be behind a NAT?
488 * Has the NAT been punched?
493 * Is this transport configured to allow connections to NAT'd peers?
495 int enable_nat_client;
498 * Should we run the gnunet-nat-server?
500 int enable_nat_server;
503 * Are we allowed to try UPnP/PMP for NAT traversal?
511 * Our external IP address/port mapping has changed.
513 * @param cls closure, the 'struct LocalAddrList'
514 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
515 * the previous (now invalid) one
516 * @param addr either the previous or the new public IP address
517 * @param addrlen actual lenght of the address
520 nat_port_map_callback (void *cls,
522 const struct sockaddr *addr,
525 struct LocalAddrList *lal = cls;
526 struct Plugin *plugin = lal->plugin;
528 struct IPv4TcpAddress t4;
529 struct IPv6TcpAddress t6;
533 /* convert 'addr' to our internal format */
534 af = addr->sa_family;
538 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
539 t6.t6_port = ((struct sockaddr_in *) addr)->sin_port;
544 memcpy (&t6.ipv6_addr,
545 &((struct sockaddr_in6 *) addr)->sin6_addr,
546 sizeof (struct in6_addr));
547 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
556 /* modify our published address list */
557 if (GNUNET_YES == add_remove)
559 plugin->env->notify_address (plugin->env->cls,
561 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
562 GNUNET_free_non_null (lal->external_nat_address);
563 lal->external_nat_address = GNUNET_memdup (arg, args);
564 lal->ena_size = args;
568 plugin->env->notify_address (plugin->env->cls,
570 arg, args, GNUNET_TIME_UNIT_ZERO);
571 GNUNET_free_non_null (lal->external_nat_address);
578 * Add the given address to the list of 'local' addresses, thereby
579 * making it a 'legal' address for this peer to have.
581 * @param plugin the plugin
582 * @param arg the address, either an IPv4 or an IPv6 IP address
583 * @param arg_size number of bytes in arg
586 add_to_address_list (struct Plugin *plugin,
590 struct LocalAddrList *lal;
591 struct sockaddr_in v4;
592 struct sockaddr_in6 v6;
593 const struct sockaddr *sa;
596 lal = plugin->lal_head;
599 if ( (lal->size == arg_size) &&
600 (0 == memcmp (&lal[1], arg, arg_size)) )
604 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
605 lal->plugin = plugin;
606 lal->size = arg_size;
607 memcpy (&lal[1], arg, arg_size);
608 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
611 if (plugin->open_port == 0)
612 return; /* we're not listening at all... */
613 if (arg_size == sizeof (struct in_addr))
615 memset (&v4, 0, sizeof (v4));
616 v4.sin_family = AF_INET;
617 v4.sin_port = htons (plugin->open_port);
618 memcpy (&v4.sin_addr, arg, arg_size);
619 #if HAVE_SOCKADDR_IN_SIN_LEN
620 v4.sin_len = sizeof (struct sockaddr_in);
622 sa = (const struct sockaddr*) &v4;
625 else if (arg_size == sizeof (struct in6_addr))
627 memset (&v6, 0, sizeof (v6));
628 v6.sin6_family = AF_INET6;
629 v6.sin6_port = htons (plugin->open_port);
630 memcpy (&v6.sin6_addr, arg, arg_size);
631 #if HAVE_SOCKADDR_IN_SIN_LEN
632 v6.sin6_len = sizeof (struct sockaddr_in6);
634 sa = (const struct sockaddr*) &v6;
642 if ( (plugin->behind_nat == GNUNET_YES) &&
643 (plugin->enable_upnp == GNUNET_YES) )
644 lal->nat = GNUNET_NAT_register (sa, salen,
645 &nat_port_map_callback,
651 * Check if the given address is in the list of 'local' addresses.
653 * @param plugin the plugin
654 * @param arg the address, either an IPv4 or an IPv6 IP address
655 * @param arg_size number of bytes in arg
656 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
659 check_local_addr (struct Plugin *plugin,
663 struct LocalAddrList *lal;
665 lal = plugin->lal_head;
668 if ( (lal->size == arg_size) &&
669 (0 == memcmp (&lal[1], arg, arg_size)) )
673 return GNUNET_SYSERR;
678 * Check if the given address is in the list of 'mapped' addresses.
680 * @param plugin the plugin
681 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
682 * @param arg_size number of bytes in arg
683 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
686 check_mapped_addr (struct Plugin *plugin,
690 struct LocalAddrList *lal;
692 lal = plugin->lal_head;
695 if ( (lal->ena_size == arg_size) &&
696 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
700 return GNUNET_SYSERR;
705 * Function called for a quick conversion of the binary address to
706 * a numeric address. Note that the caller must not free the
707 * address and that the next call to this function is allowed
708 * to override the address again.
710 * @param cls closure ('struct Plugin*')
711 * @param addr binary address
712 * @param addrlen length of the address
713 * @return string representing the same address
716 tcp_address_to_string (void *cls,
720 static char rbuf[INET6_ADDRSTRLEN + 12];
721 char buf[INET6_ADDRSTRLEN];
725 const struct IPv4TcpAddress *t4;
726 const struct IPv6TcpAddress *t6;
730 if (addrlen == sizeof (struct IPv6TcpAddress))
734 port = ntohs (t6->t6_port);
735 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
738 else if (addrlen == sizeof (struct IPv4TcpAddress))
742 port = ntohs (t4->t_port);
743 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
748 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
750 _("Unexpected address length: %u bytes\n"),
751 (unsigned int) addrlen);
755 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
757 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
760 GNUNET_snprintf (rbuf,
762 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
770 * Find the session handle for the given client.
772 * @param plugin the plugin
773 * @param client which client to find the session handle for
774 * @return NULL if no matching session exists
776 static struct Session *
777 find_session_by_client (struct Plugin *plugin,
778 const struct GNUNET_SERVER_Client *client)
782 ret = plugin->sessions;
783 while ((ret != NULL) && (client != ret->client))
790 * Create a new session. Also queues a welcome message.
792 * @param plugin the plugin
793 * @param target peer to connect to
794 * @param client client to use
795 * @param is_nat this a NAT session, we should wait for a client to
796 * connect to us from an address, then assign that to
798 * @return new session object
800 static struct Session *
801 create_session (struct Plugin *plugin,
802 const struct GNUNET_PeerIdentity *target,
803 struct GNUNET_SERVER_Client *client,
807 struct PendingMessage *pm;
808 struct WelcomeMessage welcome;
810 if (is_nat != GNUNET_YES)
811 GNUNET_assert (client != NULL);
813 GNUNET_assert (client == NULL);
815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
817 "Creating new session for peer `%4s'\n",
818 GNUNET_i2s (target));
820 ret = GNUNET_malloc (sizeof (struct Session));
821 ret->last_activity = GNUNET_TIME_absolute_get ();
822 ret->plugin = plugin;
823 ret->is_nat = is_nat;
824 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
826 ret->next = plugin->sessions;
827 plugin->sessions = ret;
829 ret->client = client;
830 ret->target = *target;
831 ret->expecting_welcome = GNUNET_YES;
832 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
833 pm->msg = (const char*) &pm[1];
834 pm->message_size = sizeof (struct WelcomeMessage);
835 welcome.header.size = htons (sizeof (struct WelcomeMessage));
836 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
837 welcome.clientIdentity = *plugin->env->my_identity;
838 memcpy (&pm[1], &welcome, sizeof (welcome));
839 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
840 GNUNET_STATISTICS_update (plugin->env->stats,
841 gettext_noop ("# bytes currently in TCP buffers"),
844 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
845 ret->pending_messages_tail,
847 if (is_nat != GNUNET_YES)
848 GNUNET_STATISTICS_update (plugin->env->stats,
849 gettext_noop ("# TCP sessions active"),
857 * If we have pending messages, ask the server to
858 * transmit them (schedule the respective tasks, etc.)
860 * @param session for which session should we do this
862 static void process_pending_messages (struct Session *session);
866 * Function called to notify a client about the socket
867 * being ready to queue more data. "buf" will be
868 * NULL and "size" zero if the socket was closed for
869 * writing in the meantime.
872 * @param size number of bytes available in buf
873 * @param buf where the callee should write the message
874 * @return number of bytes written to buf
877 do_transmit (void *cls, size_t size, void *buf)
879 struct Session *session = cls;
880 struct GNUNET_PeerIdentity pid;
881 struct Plugin *plugin;
882 struct PendingMessage *pos;
883 struct PendingMessage *hd;
884 struct PendingMessage *tl;
885 struct GNUNET_TIME_Absolute now;
889 session->transmit_handle = NULL;
890 plugin = session->plugin;
894 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
896 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
897 GNUNET_i2s (&session->target));
899 /* timeout; cancel all messages that have already expired */
903 now = GNUNET_TIME_absolute_get ();
904 while ( (NULL != (pos = session->pending_messages_head)) &&
905 (pos->timeout.abs_value <= now.abs_value) )
907 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
908 session->pending_messages_tail,
911 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
913 "Failed to transmit %u byte message to `%4s'.\n",
915 GNUNET_i2s (&session->target));
917 ret += pos->message_size;
918 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
920 /* do this call before callbacks (so that if callbacks destroy
921 session, they have a chance to cancel actions done by this
923 process_pending_messages (session);
924 pid = session->target;
925 /* no do callbacks and do not use session again since
926 the callbacks may abort the session */
927 while (NULL != (pos = hd))
929 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
930 if (pos->transmit_cont != NULL)
931 pos->transmit_cont (pos->transmit_cont_cls,
932 &pid, GNUNET_SYSERR);
935 GNUNET_STATISTICS_update (plugin->env->stats,
936 gettext_noop ("# bytes currently in TCP buffers"),
939 GNUNET_STATISTICS_update (plugin->env->stats,
940 gettext_noop ("# bytes discarded by TCP (timeout)"),
945 /* copy all pending messages that would fit */
950 while (NULL != (pos = session->pending_messages_head))
952 if (ret + pos->message_size > size)
954 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
955 session->pending_messages_tail,
957 GNUNET_assert (size >= pos->message_size);
958 /* FIXME: this memcpy can be up to 7% of our total runtime */
959 memcpy (cbuf, pos->msg, pos->message_size);
960 cbuf += pos->message_size;
961 ret += pos->message_size;
962 size -= pos->message_size;
963 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
965 /* schedule 'continuation' before callbacks so that callbacks that
966 cancel everything don't cause us to use a session that no longer
968 process_pending_messages (session);
969 session->last_activity = GNUNET_TIME_absolute_get ();
970 pid = session->target;
971 /* we'll now call callbacks that may cancel the session; hence
972 we should not use 'session' after this point */
973 while (NULL != (pos = hd))
975 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
976 if (pos->transmit_cont != NULL)
977 pos->transmit_cont (pos->transmit_cont_cls,
981 GNUNET_assert (hd == NULL);
982 GNUNET_assert (tl == NULL);
984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
986 "Transmitting %u bytes\n",
989 GNUNET_STATISTICS_update (plugin->env->stats,
990 gettext_noop ("# bytes currently in TCP buffers"),
993 GNUNET_STATISTICS_update (plugin->env->stats,
994 gettext_noop ("# bytes transmitted via TCP"),
1002 * If we have pending messages, ask the server to
1003 * transmit them (schedule the respective tasks, etc.)
1005 * @param session for which session should we do this
1008 process_pending_messages (struct Session *session)
1010 struct PendingMessage *pm;
1012 GNUNET_assert (session->client != NULL);
1013 if (session->transmit_handle != NULL)
1015 if (NULL == (pm = session->pending_messages_head))
1018 session->transmit_handle
1019 = GNUNET_SERVER_notify_transmit_ready (session->client,
1021 GNUNET_TIME_absolute_get_remaining
1023 &do_transmit, session);
1028 * Functions with this signature are called whenever we need
1029 * to close a session due to a disconnect or failure to
1030 * establish a connection.
1032 * @param session session to close down
1035 disconnect_session (struct Session *session)
1037 struct Session *prev;
1038 struct Session *pos;
1039 struct PendingMessage *pm;
1042 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1044 "Disconnecting from `%4s' at %s.\n",
1045 GNUNET_i2s (&session->target),
1046 (session->connect_addr != NULL) ?
1047 tcp_address_to_string (session->plugin,
1048 session->connect_addr,
1049 session->connect_alen) : "*");
1051 /* remove from session list */
1053 pos = session->plugin->sessions;
1054 while (pos != session)
1060 session->plugin->sessions = session->next;
1062 prev->next = session->next;
1063 session->plugin->env->session_end (session->plugin->env->cls,
1066 /* clean up state */
1067 if (session->transmit_handle != NULL)
1069 GNUNET_CONNECTION_notify_transmit_ready_cancel
1070 (session->transmit_handle);
1071 session->transmit_handle = NULL;
1073 while (NULL != (pm = session->pending_messages_head))
1076 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1078 pm->transmit_cont != NULL
1079 ? "Could not deliver message to `%4s'.\n"
1080 : "Could not deliver message to `%4s', notifying.\n",
1081 GNUNET_i2s (&session->target));
1083 GNUNET_STATISTICS_update (session->plugin->env->stats,
1084 gettext_noop ("# bytes currently in TCP buffers"),
1085 - (int64_t) pm->message_size,
1087 GNUNET_STATISTICS_update (session->plugin->env->stats,
1088 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1091 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1092 session->pending_messages_tail,
1094 if (NULL != pm->transmit_cont)
1095 pm->transmit_cont (pm->transmit_cont_cls,
1096 &session->target, GNUNET_SYSERR);
1099 GNUNET_break (session->client != NULL);
1100 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1102 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1103 if (session->client != NULL)
1104 GNUNET_SERVER_receive_done (session->client,
1107 else if (session->client != NULL)
1108 GNUNET_SERVER_client_drop (session->client);
1109 GNUNET_STATISTICS_update (session->plugin->env->stats,
1110 gettext_noop ("# TCP sessions active"),
1113 GNUNET_free_non_null (session->connect_addr);
1114 GNUNET_free (session);
1119 * Given two otherwise equivalent sessions, pick the better one.
1121 * @param s1 one session (also default)
1122 * @param s2 other session
1123 * @return "better" session (more active)
1125 static struct Session *
1126 select_better_session (struct Session *s1,
1133 if ( (s1->expecting_welcome == GNUNET_NO) &&
1134 (s2->expecting_welcome == GNUNET_YES) )
1136 if ( (s1->expecting_welcome == GNUNET_YES) &&
1137 (s2->expecting_welcome == GNUNET_NO) )
1139 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1141 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1143 if ( (GNUNET_YES == s1->inbound) &&
1144 (GNUNET_NO == s2->inbound) )
1146 if ( (GNUNET_NO == s1->inbound) &&
1147 (GNUNET_YES == s2->inbound) )
1154 * We learned about a peer (possibly behind NAT) so run the
1155 * gnunet-nat-client to send dummy ICMP responses.
1157 * @param plugin the plugin for this transport
1158 * @param sa the address of the peer (IPv4-only)
1161 run_gnunet_nat_client (struct Plugin *plugin,
1162 const struct sockaddr_in *sa)
1164 char inet4[INET_ADDRSTRLEN];
1165 char port_as_string[6];
1166 struct GNUNET_OS_Process *proc;
1168 if (plugin->internal_address == NULL)
1170 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1172 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1175 GNUNET_assert (sa->sin_family == AF_INET);
1176 if (NULL == inet_ntop (AF_INET,
1178 inet4, INET_ADDRSTRLEN))
1180 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1183 GNUNET_snprintf(port_as_string,
1184 sizeof (port_as_string),
1188 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1190 _("Running gnunet-nat-client %s %s %u\n"),
1191 plugin->internal_address,
1193 (unsigned int) plugin->adv_port);
1195 proc = GNUNET_OS_start_process (NULL,
1197 "gnunet-nat-client",
1198 "gnunet-nat-client",
1199 plugin->internal_address,
1205 /* we know that the gnunet-nat-client will terminate virtually
1207 GNUNET_OS_process_wait (proc);
1208 GNUNET_OS_process_close (proc);
1213 * Function that can be used by the transport service to transmit
1214 * a message using the plugin. Note that in the case of a
1215 * peer disconnecting, the continuation MUST be called
1216 * prior to the disconnect notification itself. This function
1217 * will be called with this peer's HELLO message to initiate
1218 * a fresh connection to another peer.
1220 * @param cls closure
1221 * @param target who should receive this message
1222 * @param msg the message to transmit
1223 * @param msgbuf_size number of bytes in 'msg'
1224 * @param priority how important is the message (most plugins will
1225 * ignore message priority and just FIFO)
1226 * @param timeout how long to wait at most for the transmission (does not
1227 * require plugins to discard the message after the timeout,
1228 * just advisory for the desired delay; most plugins will ignore
1230 * @param session which session must be used (or NULL for "any")
1231 * @param addr the address to use (can be NULL if the plugin
1232 * is "on its own" (i.e. re-use existing TCP connection))
1233 * @param addrlen length of the address in bytes
1234 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1235 * GNUNET_NO means the plugin may use any other address and
1236 * GNUNET_SYSERR means that only reliable existing
1237 * bi-directional connections should be used (regardless
1239 * @param cont continuation to call once the message has
1240 * been transmitted (or if the transport is ready
1241 * for the next transmission call; or if the
1242 * peer disconnected...); can be NULL
1243 * @param cont_cls closure for cont
1244 * @return number of bytes used (on the physical network, with overheads);
1245 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1246 * and does NOT mean that the message was not transmitted (DV and NAT)
1249 tcp_plugin_send (void *cls,
1250 const struct GNUNET_PeerIdentity *target,
1254 struct GNUNET_TIME_Relative timeout,
1255 struct Session *session,
1259 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1261 struct Plugin *plugin = cls;
1262 struct Session *cand_session;
1263 struct Session *next;
1264 struct PendingMessage *pm;
1265 struct GNUNET_CONNECTION_Handle *sa;
1269 struct sockaddr_in a4;
1270 struct sockaddr_in6 a6;
1271 const struct IPv4TcpAddress *t4;
1272 const struct IPv6TcpAddress *t6;
1273 unsigned int is_natd;
1275 GNUNET_STATISTICS_update (plugin->env->stats,
1276 gettext_noop ("# bytes TCP was asked to transmit"),
1279 /* FIXME: we could do this cheaper with a hash table
1280 where we could restrict the iteration to entries that match
1281 the target peer... */
1282 is_natd = GNUNET_NO;
1283 if (session == NULL)
1285 cand_session = NULL;
1286 next = plugin->sessions;
1287 while (NULL != (session = next))
1289 next = session->next;
1290 GNUNET_assert (session->client != NULL);
1291 if (0 != memcmp (target,
1293 sizeof (struct GNUNET_PeerIdentity)))
1295 if ( ( (GNUNET_SYSERR == force_address) &&
1296 (session->expecting_welcome == GNUNET_NO) ) ||
1297 (GNUNET_NO == force_address) )
1299 cand_session = select_better_session (cand_session,
1303 if (GNUNET_SYSERR == force_address)
1305 GNUNET_break (GNUNET_YES == force_address);
1311 if ( (addrlen != session->connect_alen) &&
1312 (session->is_nat == GNUNET_NO) )
1314 if ((0 != memcmp (session->connect_addr,
1316 addrlen)) && (session->is_nat == GNUNET_NO))
1318 cand_session = select_better_session (cand_session,
1321 session = cand_session;
1323 if ( (session == NULL) &&
1327 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1329 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1330 GNUNET_i2s (target));
1332 GNUNET_STATISTICS_update (plugin->env->stats,
1333 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1338 if (session == NULL)
1340 if (addrlen == sizeof (struct IPv6TcpAddress))
1344 memset (&a6, 0, sizeof (a6));
1345 #if HAVE_SOCKADDR_IN_SIN_LEN
1346 a6.sin6_len = sizeof (a6);
1348 a6.sin6_family = AF_INET6;
1349 a6.sin6_port = t6->t6_port;
1350 if (t6->t6_port == 0)
1351 is_natd = GNUNET_YES;
1352 memcpy (&a6.sin6_addr,
1354 sizeof (struct in6_addr));
1358 else if (addrlen == sizeof (struct IPv4TcpAddress))
1362 memset (&a4, 0, sizeof (a4));
1363 #if HAVE_SOCKADDR_IN_SIN_LEN
1364 a4.sin_len = sizeof (a4);
1366 a4.sin_family = AF_INET;
1367 a4.sin_port = t4->t_port;
1368 if (t4->t_port == 0)
1369 is_natd = GNUNET_YES;
1370 a4.sin_addr.s_addr = t4->ipv4_addr;
1376 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1378 _("Address of unexpected length: %u\n"),
1384 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1385 return -1; /* NAT client only works with IPv4 addresses */
1388 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1389 (is_natd == GNUNET_YES) &&
1390 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1391 &target->hashPubKey)) )
1394 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1396 _("Found valid IPv4 NAT address (creating session)!\n"));
1398 session = create_session (plugin,
1403 /* create new message entry */
1404 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1405 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1406 pm->msg = (const char*) &pm[1];
1407 memcpy (&pm[1], msg, msgbuf_size);
1408 /* FIXME: this memcpy can be up to 7% of our total run-time
1409 (for transport service) */
1410 pm->message_size = msgbuf_size;
1411 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1412 pm->transmit_cont = cont;
1413 pm->transmit_cont_cls = cont_cls;
1415 /* append pm to pending_messages list */
1416 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1417 session->pending_messages_tail,
1418 session->pending_messages_tail,
1421 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1422 &target->hashPubKey,
1424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1426 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1428 "Created NAT WAIT connection to `%4s' at `%s'\n",
1429 GNUNET_i2s (target),
1430 GNUNET_a2s (sb, sbs));
1432 run_gnunet_nat_client (plugin, &a4);
1435 if ( (plugin->enable_nat_client == GNUNET_YES) &&
1436 (is_natd == GNUNET_YES) &&
1437 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns,
1438 &target->hashPubKey)) )
1440 /* Only do one NAT punch attempt per peer identity */
1443 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1447 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1449 "Failed to create connection to `%4s' at `%s'\n",
1450 GNUNET_i2s (target),
1451 GNUNET_a2s (sb, sbs));
1453 GNUNET_STATISTICS_update (plugin->env->stats,
1454 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1460 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1462 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1463 GNUNET_i2s (target),
1464 GNUNET_a2s (sb, sbs));
1466 session = create_session (plugin,
1468 GNUNET_SERVER_connect_socket (plugin->server,
1471 session->connect_addr = GNUNET_malloc (addrlen);
1472 memcpy (session->connect_addr,
1475 session->connect_alen = addrlen;
1477 GNUNET_assert (session != NULL);
1478 GNUNET_assert (session->client != NULL);
1479 GNUNET_STATISTICS_update (plugin->env->stats,
1480 gettext_noop ("# bytes currently in TCP buffers"),
1483 /* create new message entry */
1484 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1485 pm->msg = (const char*) &pm[1];
1486 memcpy (&pm[1], msg, msgbuf_size);
1487 pm->message_size = msgbuf_size;
1488 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1489 pm->transmit_cont = cont;
1490 pm->transmit_cont_cls = cont_cls;
1492 /* append pm to pending_messages list */
1493 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1494 session->pending_messages_tail,
1495 session->pending_messages_tail,
1498 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1500 "Asked to transmit %u bytes to `%s', added message to list.\n",
1502 GNUNET_i2s (target));
1504 process_pending_messages (session);
1510 * Function that can be called to force a disconnect from the
1511 * specified neighbour. This should also cancel all previously
1512 * scheduled transmissions. Obviously the transmission may have been
1513 * partially completed already, which is OK. The plugin is supposed
1514 * to close the connection (if applicable) and no longer call the
1515 * transmit continuation(s).
1517 * Finally, plugin MUST NOT call the services's receive function to
1518 * notify the service that the connection to the specified target was
1519 * closed after a getting this call.
1521 * @param cls closure
1522 * @param target peer for which the last transmission is
1526 tcp_plugin_disconnect (void *cls,
1527 const struct GNUNET_PeerIdentity *target)
1529 struct Plugin *plugin = cls;
1530 struct Session *session;
1531 struct Session *next;
1532 struct PendingMessage *pm;
1535 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1537 "Asked to cancel session with `%4s'\n",
1538 GNUNET_i2s (target));
1540 next = plugin->sessions;
1541 while (NULL != (session = next))
1543 next = session->next;
1544 if (0 != memcmp (target,
1546 sizeof (struct GNUNET_PeerIdentity)))
1548 pm = session->pending_messages_head;
1551 pm->transmit_cont = NULL;
1552 pm->transmit_cont_cls = NULL;
1555 GNUNET_STATISTICS_update (session->plugin->env->stats,
1556 gettext_noop ("# transport-service disconnect requests for TCP"),
1559 disconnect_session (session);
1565 * Context for address to string conversion.
1567 struct PrettyPrinterContext
1570 * Function to call with the result.
1572 GNUNET_TRANSPORT_AddressStringCallback asc;
1575 * Clsoure for 'asc'.
1580 * Port to add after the IP address.
1587 * Append our port and forward the result.
1589 * @param cls the 'struct PrettyPrinterContext*'
1590 * @param hostname hostname part of the address
1593 append_port (void *cls, const char *hostname)
1595 struct PrettyPrinterContext *ppc = cls;
1598 if (hostname == NULL)
1600 ppc->asc (ppc->asc_cls, NULL);
1604 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1605 ppc->asc (ppc->asc_cls, ret);
1611 * Convert the transports address to a nice, human-readable
1614 * @param cls closure
1615 * @param type name of the transport that generated the address
1616 * @param addr one of the addresses of the host, NULL for the last address
1617 * the specific address format depends on the transport
1618 * @param addrlen length of the address
1619 * @param numeric should (IP) addresses be displayed in numeric form?
1620 * @param timeout after how long should we give up?
1621 * @param asc function to call on each string
1622 * @param asc_cls closure for asc
1625 tcp_plugin_address_pretty_printer (void *cls,
1630 struct GNUNET_TIME_Relative timeout,
1631 GNUNET_TRANSPORT_AddressStringCallback asc,
1634 struct Plugin *plugin = cls;
1635 struct PrettyPrinterContext *ppc;
1638 struct sockaddr_in a4;
1639 struct sockaddr_in6 a6;
1640 const struct IPv4TcpAddress *t4;
1641 const struct IPv6TcpAddress *t6;
1644 if (addrlen == sizeof (struct IPv6TcpAddress))
1647 memset (&a6, 0, sizeof (a6));
1648 a6.sin6_family = AF_INET6;
1649 a6.sin6_port = t6->t6_port;
1650 memcpy (&a6.sin6_addr,
1652 sizeof (struct in6_addr));
1653 port = ntohs (t6->t6_port);
1657 else if (addrlen == sizeof (struct IPv4TcpAddress))
1660 memset (&a4, 0, sizeof (a4));
1661 a4.sin_family = AF_INET;
1662 a4.sin_port = t4->t_port;
1663 a4.sin_addr.s_addr = t4->ipv4_addr;
1664 port = ntohs (t4->t_port);
1670 /* invalid address */
1671 GNUNET_break_op (0);
1672 asc (asc_cls, NULL);
1675 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1677 ppc->asc_cls = asc_cls;
1679 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1682 !numeric, timeout, &append_port, ppc);
1687 * Check if the given port is plausible (must be either our listen
1688 * port or our advertised port), or any port if we are behind NAT
1689 * and do not have a port open. If it is neither, we return
1692 * @param plugin global variables
1693 * @param in_port port number to check
1694 * @return GNUNET_OK if port is either open_port or adv_port
1697 check_port (struct Plugin *plugin,
1700 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1702 return GNUNET_SYSERR;
1707 * Function that will be called to check if a binary address for this
1708 * plugin is well-formed and corresponds to an address for THIS peer
1709 * (as per our configuration). Naturally, if absolutely necessary,
1710 * plugins can be a bit conservative in their answer, but in general
1711 * plugins should make sure that the address does not redirect
1712 * traffic to a 3rd party that might try to man-in-the-middle our
1715 * @param cls closure, our 'struct Plugin*'
1716 * @param addr pointer to the address
1717 * @param addrlen length of addr
1718 * @return GNUNET_OK if this is a plausible address for this peer
1719 * and transport, GNUNET_SYSERR if not
1722 tcp_plugin_check_address (void *cls,
1726 struct Plugin *plugin = cls;
1727 struct IPv4TcpAddress *v4;
1728 struct IPv6TcpAddress *v6;
1730 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1731 (addrlen != sizeof (struct IPv6TcpAddress)))
1733 GNUNET_break_op (0);
1734 return GNUNET_SYSERR;
1736 if (addrlen == sizeof (struct IPv4TcpAddress))
1738 v4 = (struct IPv4TcpAddress *) addr;
1740 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1743 check_port (plugin, ntohs (v4->t_port)))
1744 return GNUNET_SYSERR;
1746 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1747 return GNUNET_SYSERR;
1751 v6 = (struct IPv6TcpAddress *) addr;
1752 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1754 GNUNET_break_op (0);
1755 return GNUNET_SYSERR;
1758 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1761 check_port (plugin, ntohs (v6->t6_port)))
1762 return GNUNET_SYSERR;
1764 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1765 return GNUNET_SYSERR;
1772 * We've received a nat probe from this peer via TCP. Finish
1773 * creating the client session and resume sending of queued
1776 * @param cls closure
1777 * @param client identification of the client
1778 * @param message the actual message
1781 handle_tcp_nat_probe (void *cls,
1782 struct GNUNET_SERVER_Client *client,
1783 const struct GNUNET_MessageHeader *message)
1785 struct Plugin *plugin = cls;
1786 struct Session *session;
1787 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1790 struct IPv4TcpAddress *t4;
1791 struct IPv6TcpAddress *t6;
1792 const struct sockaddr_in *s4;
1793 const struct sockaddr_in6 *s6;
1796 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1798 "received tcp NAT probe\n");
1800 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1801 * a connection to this peer by running gnunet-nat-client. This peer
1802 * received the punch message and now wants us to use the new connection
1803 * as the default for that peer. Do so and then send a WELCOME message
1804 * so we can really be connected!
1806 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1811 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1812 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1813 &tcp_nat_probe->clientIdentity.hashPubKey);
1814 if (session == NULL)
1817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1819 "Did NOT find session for NAT probe!\n");
1821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1825 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1827 "Found session for NAT probe!\n");
1829 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1830 &tcp_nat_probe->clientIdentity.hashPubKey,
1831 session) == GNUNET_YES);
1833 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1836 GNUNET_free (session);
1837 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1841 GNUNET_SERVER_client_keep (client);
1842 session->client = client;
1843 session->last_activity = GNUNET_TIME_absolute_get ();
1844 session->inbound = GNUNET_NO;
1847 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1849 "Found address `%s' for incoming connection\n",
1850 GNUNET_a2s (vaddr, alen));
1852 switch (((const struct sockaddr *)vaddr)->sa_family)
1856 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1857 t4->t_port = s4->sin_port;
1858 t4->ipv4_addr = s4->sin_addr.s_addr;
1859 session->connect_addr = t4;
1860 session->connect_alen = sizeof (struct IPv4TcpAddress);
1864 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1865 t6->t6_port = s6->sin6_port;
1866 memcpy (&t6->ipv6_addr,
1868 sizeof (struct in6_addr));
1869 session->connect_addr = t6;
1870 session->connect_alen = sizeof (struct IPv6TcpAddress);
1873 GNUNET_break_op (0);
1875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1877 "Bad address for incoming connection!\n");
1879 GNUNET_free (vaddr);
1880 GNUNET_SERVER_client_drop (client);
1881 GNUNET_free (session);
1882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1885 GNUNET_free (vaddr);
1887 session->next = plugin->sessions;
1888 plugin->sessions = session;
1889 GNUNET_STATISTICS_update (plugin->env->stats,
1890 gettext_noop ("# TCP sessions active"),
1893 process_pending_messages (session);
1894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1899 * We've received a welcome from this peer via TCP. Possibly create a
1900 * fresh client record and send back our welcome.
1902 * @param cls closure
1903 * @param client identification of the client
1904 * @param message the actual message
1907 handle_tcp_welcome (void *cls,
1908 struct GNUNET_SERVER_Client *client,
1909 const struct GNUNET_MessageHeader *message)
1911 struct Plugin *plugin = cls;
1912 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1913 struct Session *session;
1916 struct IPv4TcpAddress *t4;
1917 struct IPv6TcpAddress *t6;
1918 const struct sockaddr_in *s4;
1919 const struct sockaddr_in6 *s6;
1922 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1924 "Received %s message from `%4s'.\n",
1926 GNUNET_i2s (&wm->clientIdentity));
1928 GNUNET_STATISTICS_update (plugin->env->stats,
1929 gettext_noop ("# TCP WELCOME messages received"),
1932 session = find_session_by_client (plugin, client);
1934 if (session == NULL)
1937 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1939 "Received %s message from a `%4s', creating new session\n",
1941 GNUNET_i2s (&wm->clientIdentity));
1943 GNUNET_SERVER_client_keep (client);
1944 session = create_session (plugin,
1945 &wm->clientIdentity,
1948 session->inbound = GNUNET_YES;
1950 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1953 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1955 "Found address `%s' for incoming connection\n",
1956 GNUNET_a2s (vaddr, alen));
1958 if (alen == sizeof (struct sockaddr_in))
1961 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1962 t4->t_port = s4->sin_port;
1963 t4->ipv4_addr = s4->sin_addr.s_addr;
1964 session->connect_addr = t4;
1965 session->connect_alen = sizeof (struct IPv4TcpAddress);
1967 else if (alen == sizeof (struct sockaddr_in6))
1970 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1971 t6->t6_port = s6->sin6_port;
1972 memcpy (&t6->ipv6_addr,
1974 sizeof (struct in6_addr));
1975 session->connect_addr = t6;
1976 session->connect_alen = sizeof (struct IPv6TcpAddress);
1979 GNUNET_free (vaddr);
1984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1986 "Did not obtain TCP socket address for incoming connection\n");
1989 process_pending_messages (session);
1995 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1997 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1999 "Found address `%s' (already have session)\n",
2000 GNUNET_a2s (vaddr, alen));
2001 GNUNET_free (vaddr);
2006 if (session->expecting_welcome != GNUNET_YES)
2008 GNUNET_break_op (0);
2009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2012 session->last_activity = GNUNET_TIME_absolute_get ();
2013 session->expecting_welcome = GNUNET_NO;
2014 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2019 * Task to signal the server that we can continue
2020 * receiving from the TCP client now.
2022 * @param cls the 'struct Session*'
2023 * @param tc task context (unused)
2026 delayed_done (void *cls,
2027 const struct GNUNET_SCHEDULER_TaskContext *tc)
2029 struct Session *session = cls;
2030 struct GNUNET_TIME_Relative delay;
2032 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2033 delay = session->plugin->env->receive (session->plugin->env->cls,
2039 if (delay.rel_value == 0)
2040 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2042 session->receive_delay_task =
2043 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2048 * We've received data for this peer via TCP. Unbox,
2049 * compute latency and forward.
2051 * @param cls closure
2052 * @param client identification of the client
2053 * @param message the actual message
2056 handle_tcp_data (void *cls,
2057 struct GNUNET_SERVER_Client *client,
2058 const struct GNUNET_MessageHeader *message)
2060 struct Plugin *plugin = cls;
2061 struct Session *session;
2062 struct GNUNET_TIME_Relative delay;
2065 type = ntohs (message->type);
2066 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2067 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2069 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2070 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2073 session = find_session_by_client (plugin, client);
2074 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2079 session->last_activity = GNUNET_TIME_absolute_get ();
2081 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2083 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2084 (unsigned int) ntohs (message->size),
2085 (unsigned int) ntohs (message->type),
2086 GNUNET_i2s (&session->target));
2088 GNUNET_STATISTICS_update (plugin->env->stats,
2089 gettext_noop ("# bytes received via TCP"),
2090 ntohs (message->size),
2092 struct GNUNET_TRANSPORT_ATS_Information distance[2];
2093 distance[0].type = htonl (GNUNET_TRANSPORT_ATS_QUALITY_NET_DISTANCE);
2094 distance[0].value = htonl (1);
2095 distance[1].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);
2096 distance[1].value = htonl (0);
2097 delay = plugin->env->receive (plugin->env->cls, &session->target, message,
2098 (const struct GNUNET_TRANSPORT_ATS_Information *) &distance,
2101 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2102 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2103 if (delay.rel_value == 0)
2104 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2106 session->receive_delay_task =
2107 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2112 * Functions with this signature are called whenever a peer
2113 * is disconnected on the network level.
2115 * @param cls closure
2116 * @param client identification of the client
2119 disconnect_notify (void *cls,
2120 struct GNUNET_SERVER_Client *client)
2122 struct Plugin *plugin = cls;
2123 struct Session *session;
2127 session = find_session_by_client (plugin, client);
2128 if (session == NULL)
2129 return; /* unknown, nothing to do */
2131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2133 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2134 GNUNET_i2s (&session->target),
2135 (session->connect_addr != NULL) ?
2136 tcp_address_to_string (session->plugin,
2137 session->connect_addr,
2138 session->connect_alen) : "*");
2140 GNUNET_STATISTICS_update (session->plugin->env->stats,
2141 gettext_noop ("# network-level TCP disconnect events"),
2144 disconnect_session (session);
2148 static int check_localaddress (const struct sockaddr *addr, socklen_t addrlen)
2151 int local = GNUNET_NO;
2152 int af = addr->sa_family;
2157 uint32_t netmask = 0x7F000000;
2158 uint32_t address = ntohl (((struct sockaddr_in *) addr)->sin_addr.s_addr);
2159 res = (address >> 24) ^ (netmask >> 24);
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Checking IPv4 address `%s': %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2172 if (IN6_IS_ADDR_LOOPBACK (&((struct sockaddr_in6 *) addr)->sin6_addr) ||
2173 IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr))
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 "Checking IPv6 address `%s' : %s\n", GNUNET_a2s (addr, addrlen), (local==GNUNET_YES) ? "local" : "global");
2188 * Add the IP of our network interface to the list of
2189 * our internal IP addresses.
2191 * @param cls the 'struct Plugin*'
2192 * @param name name of the interface
2193 * @param isDefault do we think this may be our default interface
2194 * @param addr address of the interface
2195 * @param addrlen number of bytes in addr
2196 * @return GNUNET_OK to continue iterating
2199 process_interfaces (void *cls,
2202 const struct sockaddr *addr, socklen_t addrlen)
2204 struct Plugin *plugin = cls;
2206 struct IPv4TcpAddress t4;
2207 struct IPv6TcpAddress t6;
2208 struct IPv4TcpAddress t4_nat;
2209 struct IPv6TcpAddress t6_nat;
2213 char buf[INET6_ADDRSTRLEN];
2215 af = addr->sa_family;
2218 if (plugin->use_localaddresses == GNUNET_NO)
2220 if (GNUNET_YES == check_localaddress (addr, addrlen))
2223 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2225 "Not notifying transport of address `%s' (local address)\n",
2226 GNUNET_a2s (addr, addrlen));
2235 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2236 GNUNET_assert (NULL != inet_ntop(AF_INET,
2240 if ( (plugin->bind_address != NULL) &&
2241 (0 != strcmp(buf, plugin->bind_address)) )
2244 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2246 "Not notifying transport of address `%s' (does not match bind address)\n",
2247 GNUNET_a2s (addr, addrlen));
2251 if ( (plugin->internal_address == NULL) &&
2253 plugin->internal_address = GNUNET_strdup (buf);
2254 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2255 if (plugin->behind_nat == GNUNET_YES)
2257 /* Also advertise as NAT (with port 0) */
2258 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2259 t4_nat.t_port = htons(0);
2262 t4.t_port = htons (plugin->adv_port);
2267 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2268 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2272 /* skip link local addresses */
2275 memcpy (&t6.ipv6_addr,
2276 &((struct sockaddr_in6 *) addr)->sin6_addr,
2277 sizeof (struct in6_addr));
2279 /* check bind address */
2280 GNUNET_assert (NULL != inet_ntop(AF_INET6,
2285 if ( (plugin->bind_address != NULL) &&
2286 (0 != strcmp(buf, plugin->bind_address)) )
2289 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2291 "Not notifying transport of address `%s' (does not match bind address)\n",
2292 GNUNET_a2s (addr, addrlen));
2297 add_to_address_list (plugin,
2299 sizeof (struct in6_addr));
2300 if (plugin->behind_nat == GNUNET_YES)
2302 /* Also advertise as NAT (with port 0) */
2303 memcpy (&t6_nat.ipv6_addr,
2304 &((struct sockaddr_in6 *) addr)->sin6_addr,
2305 sizeof (struct in6_addr));
2306 t6_nat.t6_port = htons(0);
2309 t6.t6_port = htons (plugin->adv_port);
2317 if (plugin->adv_port != 0)
2320 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2322 "Found address `%s' (%s) len %d\n",
2323 GNUNET_a2s (addr, addrlen), name, args);
2325 plugin->env->notify_address (plugin->env->cls,
2327 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2330 if (arg_nat != NULL)
2332 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2334 _("Found address `%s' (%s) len %d\n"),
2335 GNUNET_a2s (addr, addrlen), name, args);
2336 plugin->env->notify_address (plugin->env->cls,
2338 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2346 * Function called by the resolver for each address obtained from DNS
2347 * for our own hostname. Add the addresses to the list of our
2348 * external IP addresses.
2350 * @param cls closure
2351 * @param addr one of the addresses of the host, NULL for the last address
2352 * @param addrlen length of the address
2355 process_hostname_ips (void *cls,
2356 const struct sockaddr *addr, socklen_t addrlen)
2358 struct Plugin *plugin = cls;
2362 plugin->hostname_dns = NULL;
2365 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2366 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2371 * We can now send a probe message, copy into buffer to really send.
2373 * @param cls closure, a struct TCPProbeContext
2374 * @param size max size to copy
2375 * @param buf buffer to copy message to
2376 * @return number of bytes copied into buf
2379 notify_send_probe (void *cls,
2383 struct TCPProbeContext *tcp_probe_ctx = cls;
2384 struct Plugin *plugin = tcp_probe_ctx->plugin;
2387 tcp_probe_ctx->transmit_handle = NULL;
2388 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2393 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2394 GNUNET_free(tcp_probe_ctx);
2397 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2398 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2399 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2400 tcp_probe_ctx->sock);
2401 ret = sizeof(tcp_probe_ctx->message);
2402 GNUNET_free(tcp_probe_ctx);
2408 * We have been notified that gnunet-nat-server has written something to stdout.
2409 * Handle the output, then reschedule this function to be called again once
2410 * more is available.
2412 * @param cls the plugin handle
2413 * @param tc the scheduling context
2416 tcp_plugin_server_read (void *cls,
2417 const struct GNUNET_SCHEDULER_TaskContext *tc)
2419 struct Plugin *plugin = cls;
2424 const char *port_start;
2425 struct sockaddr_in sin_addr;
2426 struct TCPProbeContext *tcp_probe_ctx;
2427 struct GNUNET_CONNECTION_Handle *sock;
2429 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2431 memset (mybuf, 0, sizeof(mybuf));
2432 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2440 "Finished reading from server stdout with code: %d\n",
2443 /* FIXME: consider process_wait here? */
2448 for (i = 0; i < sizeof(mybuf); i++)
2450 if (mybuf[i] == '\n')
2455 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2458 port_start = &mybuf[i + 1];
2462 /* construct socket address of sender */
2463 memset (&sin_addr, 0, sizeof (sin_addr));
2464 sin_addr.sin_family = AF_INET;
2465 #if HAVE_SOCKADDR_IN_SIN_LEN
2466 sin_addr.sin_len = sizeof (sin_addr);
2468 if ( (NULL == port_start) ||
2469 (1 != sscanf (port_start, "%d", &port)) ||
2470 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2472 /* should we restart gnunet-nat-server? */
2473 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2475 _("gnunet-nat-server generated malformed address `%s'\n"),
2477 plugin->server_read_task
2478 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2479 plugin->server_stdout_handle,
2480 &tcp_plugin_server_read,
2484 sin_addr.sin_port = htons((uint16_t) port);
2486 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2488 "gnunet-nat-server read: %s:%d\n",
2493 * We have received an ICMP response, ostensibly from a peer
2494 * that wants to connect to us! Send a message to establish a connection.
2496 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2497 (const struct sockaddr *)&sin_addr,
2501 /* failed for some odd reason (out of sockets?); ignore attempt */
2502 plugin->server_read_task =
2503 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2504 plugin->server_stdout_handle,
2505 &tcp_plugin_server_read,
2510 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2511 "Sending TCP probe message to `%s:%u'!\n",
2513 (unsigned int) port);
2514 /* FIXME: do we need to track these probe context objects so that
2515 we can clean them up on plugin unload? */
2517 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2518 tcp_probe_ctx->message.header.size
2519 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2520 tcp_probe_ctx->message.header.type
2521 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2522 memcpy (&tcp_probe_ctx->message.clientIdentity,
2523 plugin->env->my_identity,
2524 sizeof(struct GNUNET_PeerIdentity));
2525 tcp_probe_ctx->plugin = plugin;
2526 tcp_probe_ctx->sock = sock;
2527 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2530 tcp_probe_ctx->transmit_handle
2531 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2532 ntohs (tcp_probe_ctx->message.header.size),
2533 GNUNET_TIME_UNIT_FOREVER_REL,
2534 ¬ify_send_probe, tcp_probe_ctx);
2536 plugin->server_read_task =
2537 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2538 plugin->server_stdout_handle,
2539 &tcp_plugin_server_read,
2545 * Start the gnunet-nat-server process for users behind NAT.
2547 * @param plugin the transport plugin
2548 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2551 tcp_transport_start_nat_server (struct Plugin *plugin)
2553 if (plugin->internal_address == NULL)
2554 return GNUNET_SYSERR;
2555 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2558 if (plugin->server_stdout == NULL)
2559 return GNUNET_SYSERR;
2561 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2563 "Starting %s %s\n", "gnunet-nat-server", plugin->internal_address);
2565 /* Start the server process */
2566 plugin->server_proc = GNUNET_OS_start_process (NULL,
2567 plugin->server_stdout,
2568 "gnunet-nat-server",
2569 "gnunet-nat-server",
2570 plugin->internal_address,
2572 if (plugin->server_proc == NULL)
2574 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2576 _("Failed to start %s\n"),
2577 "gnunet-nat-server");
2578 GNUNET_DISK_pipe_close (plugin->server_stdout);
2579 plugin->server_stdout = NULL;
2580 return GNUNET_SYSERR;
2582 /* Close the write end of the read pipe */
2583 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2584 GNUNET_DISK_PIPE_END_WRITE);
2585 plugin->server_stdout_handle
2586 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2587 GNUNET_DISK_PIPE_END_READ);
2588 plugin->server_read_task
2589 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2590 plugin->server_stdout_handle,
2591 &tcp_plugin_server_read,
2598 * Return the actual path to a file found in the current
2599 * PATH environment variable.
2601 * @param binary the name of the file to find
2602 * @return path to binary, NULL if not found
2605 get_path_from_PATH (const char *binary)
2613 p = getenv ("PATH");
2616 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2618 _("PATH environment variable is unset.\n"));
2621 path = GNUNET_strdup (p); /* because we write on it */
2622 buf = GNUNET_malloc (strlen (path) + 20);
2625 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2628 sprintf (buf, "%s/%s", pos, binary);
2629 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2636 sprintf (buf, "%s/%s", pos, binary);
2637 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2649 * Check whether the suid bit is set on a file.
2650 * Attempts to find the file using the current
2651 * PATH environment variable as a search path.
2653 * @param binary the name of the file to check
2654 * @return GNUNET_YES if the file is SUID,
2656 * GNUNET_SYSERR on error
2659 check_gnunet_nat_binary (const char *binary)
2661 struct stat statbuf;
2667 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2668 p = get_path_from_PATH (binaryexe);
2671 p = get_path_from_PATH (binary);
2675 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2677 _("Could not find binary `%s' in PATH!\n"),
2681 if (0 != STAT (p, &statbuf))
2683 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2684 _("stat (%s) failed: %s\n"),
2688 return GNUNET_SYSERR;
2692 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2693 (statbuf.st_uid == 0) )
2697 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2698 if (INVALID_SOCKET == rawsock)
2700 DWORD err = GetLastError ();
2701 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2703 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2704 return GNUNET_NO; /* not running as administrator */
2706 closesocket (rawsock);
2713 * Our (external) hostname was resolved.
2715 * @param cls the 'struct Plugin'
2716 * @param addr NULL on error, otherwise result of DNS lookup
2717 * @param addrlen number of bytes in addr
2720 process_external_ip (void *cls,
2721 const struct sockaddr *addr,
2724 struct Plugin *plugin = cls;
2725 const struct sockaddr_in *s;
2726 struct IPv4TcpAddress t4;
2727 char buf[INET_ADDRSTRLEN];
2729 plugin->ext_dns = NULL;
2732 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
2733 s = (const struct sockaddr_in *) addr;
2734 t4.ipv4_addr = s->sin_addr.s_addr;
2735 if ( (plugin->behind_nat == GNUNET_YES) &&
2736 (plugin->enable_nat_server == GNUNET_YES) )
2738 t4.t_port = htons(0);
2739 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2741 "Notifying transport of address %s:%d\n",
2742 plugin->external_address,
2747 t4.t_port = htons(plugin->adv_port);
2748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2750 "Notifying transport of address %s:%d\n",
2751 plugin->external_address,
2752 (int) plugin->adv_port);
2755 if ((plugin->bind_address != NULL) && (plugin->behind_nat == GNUNET_NO))
2757 GNUNET_assert (NULL != inet_ntop(AF_INET,
2761 if (0 != strcmp (plugin->bind_address, buf))
2763 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2765 "NAT is not enabled and specific bind address `%s' differs from external address `%s'! Not notifying about external address `%s'\n",
2766 plugin->bind_address,
2767 plugin->external_address,
2768 plugin->external_address);
2773 add_to_address_list (plugin,
2775 sizeof (struct in_addr));
2777 plugin->env->notify_address (plugin->env->cls,
2780 GNUNET_TIME_UNIT_FOREVER_REL);
2785 * Entry point for the plugin.
2787 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2788 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2791 libgnunet_plugin_transport_tcp_init (void *cls)
2793 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2794 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2795 sizeof (struct WelcomeMessage)},
2796 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2797 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2800 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2801 struct GNUNET_TRANSPORT_PluginFunctions *api;
2802 struct Plugin *plugin;
2803 struct GNUNET_SERVICE_Context *service;
2804 unsigned long long aport;
2805 unsigned long long bport;
2809 int enable_nat_client;
2810 int enable_nat_server;
2812 int use_localaddresses;
2813 char *internal_address;
2814 char *external_address;
2816 struct sockaddr_in in_addr;
2817 struct GNUNET_TIME_Relative idle_timeout;
2819 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2822 nat_punched = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2825 enable_nat_client = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2827 "ENABLE_NAT_CLIENT");
2828 enable_nat_server = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2830 "ENABLE_NAT_SERVER");
2831 enable_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2835 if ( (GNUNET_YES == enable_nat_server) &&
2836 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2838 enable_nat_server = GNUNET_NO;
2839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2840 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2841 "gnunet-nat-server");
2844 if ( (GNUNET_YES == enable_nat_client) &&
2845 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2847 enable_nat_client = GNUNET_NO;
2848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2849 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2850 "gnunet-nat-client");
2853 external_address = NULL;
2855 GNUNET_CONFIGURATION_have_value (env->cfg,
2857 "EXTERNAL_ADDRESS"))
2859 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2865 if ( (external_address != NULL) &&
2866 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2869 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2871 _("Malformed %s `%s' given in configuration!\n"),
2876 if ( (external_address == NULL) &&
2877 (nat_punched == GNUNET_YES) )
2879 nat_punched = GNUNET_NO;
2880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2881 _("Configuration says NAT was punched, but `%s' is not given. Option ignored.\n"),
2882 "EXTERNAL_ADDRESS");
2885 if (GNUNET_YES == nat_punched)
2887 enable_nat_server = GNUNET_NO;
2888 enable_upnp = GNUNET_NO;
2891 bind_address = NULL;
2892 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2897 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2899 _("Binding TCP plugin to specific address: `%s'\n"),
2903 internal_address = NULL;
2905 GNUNET_CONFIGURATION_have_value (env->cfg,
2907 "INTERNAL_ADDRESS"))
2909 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2915 if ( (internal_address != NULL) &&
2916 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2918 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2920 _("Malformed %s `%s' given in configuration!\n"),
2923 GNUNET_free_non_null(internal_address);
2924 GNUNET_free_non_null(external_address);
2928 if ((bind_address != NULL) && (internal_address != NULL))
2930 if (0 != strcmp(internal_address, bind_address ))
2932 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2933 "tcp","Specific bind address `%s' and internal address `%s' must not differ, forcing internal address to bind address!\n", bind_address, internal_address);
2934 GNUNET_free (internal_address);
2935 internal_address = bind_address;
2936 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2937 "tcp","New internal address `%s'\n", internal_address);
2943 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2949 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2955 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2957 _("Require valid port number for service `%s' in configuration!\n"),
2959 GNUNET_free_non_null(external_address);
2960 GNUNET_free_non_null(internal_address);
2964 use_localaddresses = GNUNET_NO;
2965 if (GNUNET_CONFIGURATION_have_value (env->cfg,
2966 "transport-tcp", "USE_LOCALADDR"))
2968 use_localaddresses = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2980 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2981 if (service == NULL)
2983 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2985 _("Failed to start service.\n"));
2992 plugin = GNUNET_malloc (sizeof (struct Plugin));
2993 plugin->open_port = bport;
2994 plugin->adv_port = aport;
2995 plugin->bind_address = bind_address;
2996 plugin->external_address = external_address;
2997 plugin->internal_address = internal_address;
2998 plugin->behind_nat = behind_nat;
2999 plugin->nat_punched = nat_punched;
3000 plugin->enable_nat_client = enable_nat_client;
3001 plugin->enable_nat_server = enable_nat_server;
3002 plugin->enable_upnp = enable_upnp;
3003 plugin->use_localaddresses = use_localaddresses;
3005 plugin->lsock = NULL;
3006 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
3008 api->send = &tcp_plugin_send;
3009 api->disconnect = &tcp_plugin_disconnect;
3010 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
3011 api->check_address = &tcp_plugin_check_address;
3012 api->address_to_string = &tcp_address_to_string;
3013 plugin->service = service;
3014 if (service != NULL)
3016 plugin->server = GNUNET_SERVICE_get_server (service);
3021 GNUNET_CONFIGURATION_get_value_time (env->cfg,
3026 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3028 _("Failed to find option %s in section %s!\n"),
3031 GNUNET_free_non_null(external_address);
3032 GNUNET_free_non_null(internal_address);
3036 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
3037 idle_timeout, GNUNET_YES);
3039 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
3040 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
3042 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
3044 plugin->handlers[i].callback_cls = plugin;
3045 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
3046 GNUNET_SERVER_disconnect_notify (plugin->server,
3049 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
3051 if ( (plugin->behind_nat == GNUNET_YES) &&
3052 (plugin->enable_nat_server == GNUNET_YES) &&
3053 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
3055 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
3057 _("Failed to start %s required for NAT in %s!\n"),
3060 GNUNET_free_non_null(external_address);
3061 GNUNET_free_non_null(internal_address);
3062 if (service != NULL)
3063 GNUNET_SERVICE_stop (service);
3065 GNUNET_SERVER_destroy (plugin->server);
3070 if (enable_nat_client == GNUNET_YES)
3072 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
3073 GNUNET_assert (plugin->nat_wait_conns != NULL);
3077 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3079 _("TCP transport listening on port %llu\n"),
3082 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3084 _("TCP transport not listening on any port (client only)\n"));
3086 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
3088 _("TCP transport advertises itself as being on port %llu\n"),
3091 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
3093 HOSTNAME_RESOLVE_TIMEOUT,
3094 &process_hostname_ips,
3097 if (plugin->external_address != NULL)
3099 plugin->ext_dns = GNUNET_RESOLVER_ip_get (env->cfg,
3100 plugin->external_address,
3102 GNUNET_TIME_UNIT_MINUTES,
3103 &process_external_ip,
3111 * Exit point from the plugin.
3114 libgnunet_plugin_transport_tcp_done (void *cls)
3116 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
3117 struct Plugin *plugin = api->cls;
3118 struct Session *session;
3119 struct LocalAddrList *lal;
3120 struct TCPProbeContext *tcp_probe;
3122 if (plugin->ext_dns != NULL)
3124 GNUNET_RESOLVER_request_cancel (plugin->ext_dns);
3125 plugin->ext_dns = NULL;
3127 while (NULL != (session = plugin->sessions))
3128 disconnect_session (session);
3129 if (NULL != plugin->hostname_dns)
3131 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
3132 plugin->hostname_dns = NULL;
3134 if (plugin->service != NULL)
3135 GNUNET_SERVICE_stop (plugin->service);
3137 GNUNET_SERVER_destroy (plugin->server);
3138 GNUNET_free (plugin->handlers);
3139 while (NULL != (lal = plugin->lal_head))
3141 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
3144 if (lal->nat != NULL)
3145 GNUNET_NAT_unregister (lal->nat);
3146 GNUNET_free_non_null (lal->external_nat_address);
3149 while (NULL != (tcp_probe = plugin->probe_head))
3151 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
3154 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
3155 GNUNET_free (tcp_probe);
3158 if ((plugin->behind_nat == GNUNET_YES) &&
3159 (plugin->enable_nat_server == GNUNET_YES))
3161 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
3162 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
3163 GNUNET_OS_process_wait (plugin->server_proc);
3164 GNUNET_OS_process_close (plugin->server_proc);
3165 plugin->server_proc = NULL;
3167 GNUNET_free_non_null(plugin->bind_address);
3168 GNUNET_free (plugin);
3173 /* end of plugin_transport_tcp.c */