2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file transport/plugin_transport_tcp.c
22 * @brief Implementation of the TCP transport service
23 * @author Christian Grothoff
26 #include "gnunet_hello_lib.h"
27 #include "gnunet_connection_lib.h"
28 #include "gnunet_container_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_service_lib.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_transport_plugin.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
43 #define DEBUG_TCP_NAT GNUNET_NO
46 * How long until we give up on transmitting the welcome message?
48 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
52 * Initial handshake message for a session.
57 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
59 struct GNUNET_MessageHeader header;
62 * Identity of the node connecting (TCP client)
64 struct GNUNET_PeerIdentity clientIdentity;
70 * Basically a WELCOME message, but with the purpose
71 * of giving the waiting peer a client handle to use
73 struct TCP_NAT_ProbeMessage
76 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
78 struct GNUNET_MessageHeader header;
81 * Identity of the sender of the message.
83 struct GNUNET_PeerIdentity clientIdentity;
89 * Context for sending a NAT probe via TCP.
91 struct TCPProbeContext
95 * Active probes are kept in a DLL.
97 struct TCPProbeContext *next;
100 * Active probes are kept in a DLL.
102 struct TCPProbeContext *prev;
107 struct GNUNET_CONNECTION_Handle *sock;
110 * Message to be sent.
112 struct TCP_NAT_ProbeMessage message;
115 * Handle to the transmission.
117 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
120 * Transport plugin handle.
122 struct Plugin *plugin;
127 * Network format for IPv4 addresses.
129 struct IPv4TcpAddress
132 * IPv4 address, in network byte order.
134 uint32_t ipv4_addr GNUNET_PACKED;
137 * Port number, in network byte order.
139 uint16_t t_port GNUNET_PACKED;
145 * Network format for IPv6 addresses.
147 struct IPv6TcpAddress
152 struct in6_addr ipv6_addr GNUNET_PACKED;
155 * Port number, in network byte order.
157 uint16_t t6_port GNUNET_PACKED;
163 * Encapsulation of all of the state of the plugin.
169 * Local network addresses (actual IP address follows this struct).
170 * PORT is NOT included!
176 * This is a doubly linked list.
178 struct LocalAddrList *next;
181 * This is a doubly linked list.
183 struct LocalAddrList *prev;
188 struct Plugin *plugin;
191 * Handle to NAT holes we've tried to punch for this address.
193 struct GNUNET_NAT_Handle *nat;
196 * Pointer to a 'struct IPv4/V6TcpAddress' describing our external IP and port
197 * as obtained from the NAT by automatic port mapping.
199 void *external_nat_address;
202 * Number of bytes in 'external_nat_address'
207 * Number of bytes of the address that follow
215 * Information kept for each message that is yet to
218 struct PendingMessage
222 * This is a doubly-linked list.
224 struct PendingMessage *next;
227 * This is a doubly-linked list.
229 struct PendingMessage *prev;
232 * The pending message
237 * Continuation function to call once the message
238 * has been sent. Can be NULL if there is no
239 * continuation to call.
241 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
244 * Closure for transmit_cont.
246 void *transmit_cont_cls;
249 * Timeout value for the pending message.
251 struct GNUNET_TIME_Absolute timeout;
254 * So that the gnunet-service-transport can group messages together,
255 * these pending messages need to accept a message buffer and size
256 * instead of just a GNUNET_MessageHeader.
264 * Session handle for TCP connections.
272 struct SessionHeader header;
275 * Stored in a linked list.
277 struct Session *next;
280 * Pointer to the global plugin struct.
282 struct Plugin *plugin;
285 * The client (used to identify this connection)
287 struct GNUNET_SERVER_Client *client;
290 * Messages currently pending for transmission
291 * to this peer, if any.
293 struct PendingMessage *pending_messages_head;
296 * Messages currently pending for transmission
297 * to this peer, if any.
299 struct PendingMessage *pending_messages_tail;
302 * Handle for pending transmission request.
304 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
307 * To whom are we talking to (set to our identity
308 * if we are still waiting for the welcome message)
310 struct GNUNET_PeerIdentity target;
313 * ID of task used to delay receiving more to throttle sender.
315 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
318 * Address of the other peer (either based on our 'connect'
319 * call or on our 'accept' call).
324 * Last activity on this connection. Used to select preferred
327 struct GNUNET_TIME_Absolute last_activity;
330 * Length of connect_addr.
335 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
337 int expecting_welcome;
340 * Was this a connection that was inbound (we accepted)? (GNUNET_YES/GNUNET_NO)
345 * Was this session created using NAT traversal?
353 * Encapsulation of all of the state of the plugin.
360 struct GNUNET_TRANSPORT_PluginEnvironment *env;
365 struct GNUNET_CONNECTION_Handle *lsock;
368 * stdout pipe handle for the gnunet-nat-server process
370 struct GNUNET_DISK_PipeHandle *server_stdout;
373 * stdout file handle (for reading) for the gnunet-nat-server process
375 const struct GNUNET_DISK_FileHandle *server_stdout_handle;
378 * ID of select gnunet-nat-server stdout read task
380 GNUNET_SCHEDULER_TaskIdentifier server_read_task;
383 * The process id of the server process (if behind NAT)
385 struct GNUNET_OS_Process *server_proc;
388 * List of open TCP sessions.
390 struct Session *sessions;
393 * Handle to the network service.
395 struct GNUNET_SERVICE_Context *service;
398 * Handle to the server for this service.
400 struct GNUNET_SERVER_Handle *server;
403 * Copy of the handler array where the closures are
404 * set to this struct's instance.
406 struct GNUNET_SERVER_MessageHandler *handlers;
409 * Handle for request of hostname resolution, non-NULL if pending.
411 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
414 * Map of peers we have tried to contact behind a NAT
416 struct GNUNET_CONTAINER_MultiHashMap *nat_wait_conns;
419 * The external address given to us by the user. Used for HELLOs
420 * and address validation.
422 char *external_address;
425 * The internal address given to us by the user (or discovered).
426 * Used for NAT traversal (ICMP method), but not as a 'validateable'
429 char *internal_address;
432 * Address given for us to bind to (ONLY).
437 * List of our IP addresses.
439 struct LocalAddrList *lal_head;
442 * Tail of our IP address list.
444 struct LocalAddrList *lal_tail;
447 * List of active TCP probes.
449 struct TCPProbeContext *probe_head;
452 * List of active TCP probes.
454 struct TCPProbeContext *probe_tail;
457 * ID of task used to update our addresses when one expires.
459 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
462 * Port that we are actually listening on.
467 * Port that the user said we would have visible to the
473 * Is this transport configured to be behind a NAT?
478 * Is this transport configured to allow connections to NAT'd peers?
483 * Are we allowed to try UPnP/PMP for NAT traversal?
491 * Our external IP address/port mapping has changed.
493 * @param cls closure, the 'struct LocalAddrList'
494 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
495 * the previous (now invalid) one
496 * @param addr either the previous or the new public IP address
497 * @param addrlen actual lenght of the address
500 nat_port_map_callback (void *cls,
502 const struct sockaddr *addr,
505 struct LocalAddrList *lal = cls;
506 struct Plugin *plugin = lal->plugin;
508 struct IPv4TcpAddress t4;
509 struct IPv6TcpAddress t6;
513 /* convert 'addr' to our internal format */
514 af = addr->sa_family;
518 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
519 t6.t6_port = ((struct sockaddr_in *) addr)->sin_port;
524 memcpy (&t6.ipv6_addr,
525 &((struct sockaddr_in6 *) addr)->sin6_addr,
526 sizeof (struct in6_addr));
527 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
536 /* modify our published address list */
537 if (GNUNET_YES == add_remove)
539 plugin->env->notify_address (plugin->env->cls,
541 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
542 GNUNET_free_non_null (lal->external_nat_address);
543 lal->external_nat_address = GNUNET_memdup (arg, args);
544 lal->ena_size = args;
548 plugin->env->notify_address (plugin->env->cls,
550 arg, args, GNUNET_TIME_UNIT_ZERO);
551 GNUNET_free_non_null (lal->external_nat_address);
558 * Add the given address to the list of 'local' addresses, thereby
559 * making it a 'legal' address for this peer to have.
561 * @param plugin the plugin
562 * @param arg the address, either an IPv4 or an IPv6 IP address
563 * @param arg_size number of bytes in arg
566 add_to_address_list (struct Plugin *plugin,
570 struct LocalAddrList *lal;
571 struct sockaddr_in v4;
572 struct sockaddr_in6 v6;
573 const struct sockaddr *sa;
576 lal = plugin->lal_head;
579 if ( (lal->size == arg_size) &&
580 (0 == memcmp (&lal[1], arg, arg_size)) )
584 lal = GNUNET_malloc (sizeof (struct LocalAddrList) + arg_size);
585 lal->plugin = plugin;
586 lal->size = arg_size;
587 memcpy (&lal[1], arg, arg_size);
588 GNUNET_CONTAINER_DLL_insert (plugin->lal_head,
591 if (plugin->open_port == 0)
592 return; /* we're not listening at all... */
593 if (arg_size == sizeof (struct in_addr))
595 memset (&v4, 0, sizeof (v4));
596 v4.sin_family = AF_INET;
597 v4.sin_port = htons (plugin->open_port);
598 memcpy (&v4.sin_addr, arg, arg_size);
599 #if HAVE_SOCKADDR_IN_SIN_LEN
600 v4.sin_len = sizeof (struct sockaddr_in);
602 sa = (const struct sockaddr*) &v4;
605 else if (arg_size == sizeof (struct in6_addr))
607 memset (&v6, 0, sizeof (v6));
608 v6.sin6_family = AF_INET6;
609 v6.sin6_port = htons (plugin->open_port);
610 memcpy (&v6.sin6_addr, arg, arg_size);
611 #if HAVE_SOCKADDR_IN_SIN_LEN
612 v6.sin6_len = sizeof (struct sockaddr_in6);
614 sa = (const struct sockaddr*) &v6;
622 if (plugin->allow_upnp)
623 lal->nat = GNUNET_NAT_register (sa, salen,
624 &nat_port_map_callback,
630 * Check if the given address is in the list of 'local' addresses.
632 * @param plugin the plugin
633 * @param arg the address, either an IPv4 or an IPv6 IP address
634 * @param arg_size number of bytes in arg
635 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
638 check_local_addr (struct Plugin *plugin,
642 struct LocalAddrList *lal;
644 lal = plugin->lal_head;
647 if ( (lal->size == arg_size) &&
648 (0 == memcmp (&lal[1], arg, arg_size)) )
652 return GNUNET_SYSERR;
657 * Check if the given address is in the list of 'mapped' addresses.
659 * @param plugin the plugin
660 * @param arg the address, either a 'struct IPv4TcpAddress' or a 'struct IPv6TcpAddress'
661 * @param arg_size number of bytes in arg
662 * @return GNUNET_OK if this is one of our IPs, GNUNET_SYSERR if not
665 check_mapped_addr (struct Plugin *plugin,
669 struct LocalAddrList *lal;
671 lal = plugin->lal_head;
674 if ( (lal->ena_size == arg_size) &&
675 (0 == memcmp (lal->external_nat_address, arg, arg_size)) )
679 return GNUNET_SYSERR;
684 * Function called for a quick conversion of the binary address to
685 * a numeric address. Note that the caller must not free the
686 * address and that the next call to this function is allowed
687 * to override the address again.
689 * @param cls closure ('struct Plugin*')
690 * @param addr binary address
691 * @param addrlen length of the address
692 * @return string representing the same address
695 tcp_address_to_string (void *cls,
699 static char rbuf[INET6_ADDRSTRLEN + 12];
700 char buf[INET6_ADDRSTRLEN];
704 const struct IPv4TcpAddress *t4;
705 const struct IPv6TcpAddress *t6;
709 if (addrlen == sizeof (struct IPv6TcpAddress))
713 port = ntohs (t6->t6_port);
714 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
717 else if (addrlen == sizeof (struct IPv4TcpAddress))
721 port = ntohs (t4->t_port);
722 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
727 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
729 _("Unexpected address length: %u\n"),
734 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
736 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
739 GNUNET_snprintf (rbuf,
741 (af == AF_INET6) ? "[%s]:%u" : "%s:%u",
749 * Find the session handle for the given client.
751 * @param plugin the plugin
752 * @param client which client to find the session handle for
753 * @return NULL if no matching session exists
755 static struct Session *
756 find_session_by_client (struct Plugin *plugin,
757 const struct GNUNET_SERVER_Client *client)
761 ret = plugin->sessions;
762 while ((ret != NULL) && (client != ret->client))
769 * Create a new session. Also queues a welcome message.
771 * @param plugin the plugin
772 * @param target peer to connect to
773 * @param client client to use
774 * @param is_nat this a NAT session, we should wait for a client to
775 * connect to us from an address, then assign that to
777 * @return new session object
779 static struct Session *
780 create_session (struct Plugin *plugin,
781 const struct GNUNET_PeerIdentity *target,
782 struct GNUNET_SERVER_Client *client,
786 struct PendingMessage *pm;
787 struct WelcomeMessage welcome;
789 if (is_nat != GNUNET_YES)
790 GNUNET_assert (client != NULL);
792 GNUNET_assert (client == NULL);
794 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
796 "Creating new session for peer `%4s'\n",
797 GNUNET_i2s (target));
799 ret = GNUNET_malloc (sizeof (struct Session));
800 ret->last_activity = GNUNET_TIME_absolute_get ();
801 ret->plugin = plugin;
802 ret->is_nat = is_nat;
803 if (is_nat != GNUNET_YES) /* If not a NAT WAIT conn, add it to global list */
805 ret->next = plugin->sessions;
806 plugin->sessions = ret;
808 ret->client = client;
809 ret->target = *target;
810 ret->expecting_welcome = GNUNET_YES;
811 pm = GNUNET_malloc (sizeof (struct PendingMessage) + sizeof (struct WelcomeMessage));
812 pm->msg = (const char*) &pm[1];
813 pm->message_size = sizeof (struct WelcomeMessage);
814 welcome.header.size = htons (sizeof (struct WelcomeMessage));
815 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
816 welcome.clientIdentity = *plugin->env->my_identity;
817 memcpy (&pm[1], &welcome, sizeof (welcome));
818 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
819 GNUNET_STATISTICS_update (plugin->env->stats,
820 gettext_noop ("# bytes currently in TCP buffers"),
823 GNUNET_CONTAINER_DLL_insert (ret->pending_messages_head,
824 ret->pending_messages_tail,
826 if (is_nat != GNUNET_YES)
827 GNUNET_STATISTICS_update (plugin->env->stats,
828 gettext_noop ("# TCP sessions active"),
836 * If we have pending messages, ask the server to
837 * transmit them (schedule the respective tasks, etc.)
839 * @param session for which session should we do this
841 static void process_pending_messages (struct Session *session);
845 * Function called to notify a client about the socket
846 * being ready to queue more data. "buf" will be
847 * NULL and "size" zero if the socket was closed for
848 * writing in the meantime.
851 * @param size number of bytes available in buf
852 * @param buf where the callee should write the message
853 * @return number of bytes written to buf
856 do_transmit (void *cls, size_t size, void *buf)
858 struct Session *session = cls;
859 struct GNUNET_PeerIdentity pid;
860 struct Plugin *plugin;
861 struct PendingMessage *pos;
862 struct PendingMessage *hd;
863 struct PendingMessage *tl;
864 struct GNUNET_TIME_Absolute now;
868 session->transmit_handle = NULL;
869 plugin = session->plugin;
873 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
875 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
876 GNUNET_i2s (&session->target));
878 /* timeout; cancel all messages that have already expired */
882 now = GNUNET_TIME_absolute_get ();
883 while ( (NULL != (pos = session->pending_messages_head)) &&
884 (pos->timeout.abs_value <= now.abs_value) )
886 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
887 session->pending_messages_tail,
890 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
892 "Failed to transmit %u byte message to `%4s'.\n",
894 GNUNET_i2s (&session->target));
896 ret += pos->message_size;
897 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
899 /* do this call before callbacks (so that if callbacks destroy
900 session, they have a chance to cancel actions done by this
902 process_pending_messages (session);
903 pid = session->target;
904 /* no do callbacks and do not use session again since
905 the callbacks may abort the session */
906 while (NULL != (pos = hd))
908 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
909 if (pos->transmit_cont != NULL)
910 pos->transmit_cont (pos->transmit_cont_cls,
911 &pid, GNUNET_SYSERR);
914 GNUNET_STATISTICS_update (plugin->env->stats,
915 gettext_noop ("# bytes currently in TCP buffers"),
918 GNUNET_STATISTICS_update (plugin->env->stats,
919 gettext_noop ("# bytes discarded by TCP (timeout)"),
924 /* copy all pending messages that would fit */
929 while (NULL != (pos = session->pending_messages_head))
931 if (ret + pos->message_size > size)
933 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
934 session->pending_messages_tail,
936 GNUNET_assert (size >= pos->message_size);
937 /* FIXME: this memcpy can be up to 7% of our total runtime */
938 memcpy (cbuf, pos->msg, pos->message_size);
939 cbuf += pos->message_size;
940 ret += pos->message_size;
941 size -= pos->message_size;
942 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
944 /* schedule 'continuation' before callbacks so that callbacks that
945 cancel everything don't cause us to use a session that no longer
947 process_pending_messages (session);
948 session->last_activity = GNUNET_TIME_absolute_get ();
949 pid = session->target;
950 /* we'll now call callbacks that may cancel the session; hence
951 we should not use 'session' after this point */
952 while (NULL != (pos = hd))
954 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
955 if (pos->transmit_cont != NULL)
956 pos->transmit_cont (pos->transmit_cont_cls,
960 GNUNET_assert (hd == NULL);
961 GNUNET_assert (tl == NULL);
963 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
965 "Transmitting %u bytes\n",
968 GNUNET_STATISTICS_update (plugin->env->stats,
969 gettext_noop ("# bytes currently in TCP buffers"),
972 GNUNET_STATISTICS_update (plugin->env->stats,
973 gettext_noop ("# bytes transmitted via TCP"),
981 * If we have pending messages, ask the server to
982 * transmit them (schedule the respective tasks, etc.)
984 * @param session for which session should we do this
987 process_pending_messages (struct Session *session)
989 struct PendingMessage *pm;
991 GNUNET_assert (session->client != NULL);
992 if (session->transmit_handle != NULL)
994 if (NULL == (pm = session->pending_messages_head))
997 session->transmit_handle
998 = GNUNET_SERVER_notify_transmit_ready (session->client,
1000 GNUNET_TIME_absolute_get_remaining
1002 &do_transmit, session);
1007 * Functions with this signature are called whenever we need
1008 * to close a session due to a disconnect or failure to
1009 * establish a connection.
1011 * @param session session to close down
1014 disconnect_session (struct Session *session)
1016 struct Session *prev;
1017 struct Session *pos;
1018 struct PendingMessage *pm;
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1023 "Disconnecting from `%4s' at %s.\n",
1024 GNUNET_i2s (&session->target),
1025 (session->connect_addr != NULL) ?
1026 tcp_address_to_string (session->plugin,
1027 session->connect_addr,
1028 session->connect_alen) : "*");
1030 /* remove from session list */
1032 pos = session->plugin->sessions;
1033 while (pos != session)
1039 session->plugin->sessions = session->next;
1041 prev->next = session->next;
1042 session->plugin->env->session_end (session->plugin->env->cls,
1045 /* clean up state */
1046 if (session->transmit_handle != NULL)
1048 GNUNET_CONNECTION_notify_transmit_ready_cancel
1049 (session->transmit_handle);
1050 session->transmit_handle = NULL;
1052 while (NULL != (pm = session->pending_messages_head))
1055 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1057 pm->transmit_cont != NULL
1058 ? "Could not deliver message to `%4s'.\n"
1059 : "Could not deliver message to `%4s', notifying.\n",
1060 GNUNET_i2s (&session->target));
1062 GNUNET_STATISTICS_update (session->plugin->env->stats,
1063 gettext_noop ("# bytes currently in TCP buffers"),
1064 - (int64_t) pm->message_size,
1066 GNUNET_STATISTICS_update (session->plugin->env->stats,
1067 gettext_noop ("# bytes discarded by TCP (disconnect)"),
1070 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1071 session->pending_messages_tail,
1073 if (NULL != pm->transmit_cont)
1074 pm->transmit_cont (pm->transmit_cont_cls,
1075 &session->target, GNUNET_SYSERR);
1078 GNUNET_break (session->client != NULL);
1079 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1081 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1082 if (session->client != NULL)
1083 GNUNET_SERVER_receive_done (session->client,
1086 if (session->client != NULL)
1087 GNUNET_SERVER_client_drop (session->client);
1088 GNUNET_STATISTICS_update (session->plugin->env->stats,
1089 gettext_noop ("# TCP sessions active"),
1092 GNUNET_free_non_null (session->connect_addr);
1093 GNUNET_free (session);
1098 * Given two otherwise equivalent sessions, pick the better one.
1100 * @param s1 one session (also default)
1101 * @param s2 other session
1102 * @return "better" session (more active)
1104 static struct Session *
1105 select_better_session (struct Session *s1,
1112 if ( (s1->expecting_welcome == GNUNET_NO) &&
1113 (s2->expecting_welcome == GNUNET_YES) )
1115 if ( (s1->expecting_welcome == GNUNET_YES) &&
1116 (s2->expecting_welcome == GNUNET_NO) )
1118 if (s1->last_activity.abs_value < s2->last_activity.abs_value)
1120 if (s1->last_activity.abs_value > s2->last_activity.abs_value)
1122 if ( (GNUNET_YES == s1->inbound) &&
1123 (GNUNET_NO == s2->inbound) )
1125 if ( (GNUNET_NO == s1->inbound) &&
1126 (GNUNET_YES == s2->inbound) )
1133 * We learned about a peer (possibly behind NAT) so run the
1134 * gnunet-nat-client to send dummy ICMP responses.
1136 * @param plugin the plugin for this transport
1137 * @param addr the address of the peer (IPv4-only)
1140 run_gnunet_nat_client (struct Plugin *plugin,
1141 const struct sockaddr_in *sa)
1143 char inet4[INET_ADDRSTRLEN];
1144 char port_as_string[6];
1145 struct GNUNET_OS_Process *proc;
1147 if (plugin->internal_address == NULL)
1149 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1151 _("Internal IP address not known, cannot use ICMP NAT traversal method\n"));
1154 GNUNET_assert (sa->sin_family == AF_INET);
1155 if (NULL == inet_ntop (AF_INET,
1157 inet4, INET_ADDRSTRLEN))
1159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
1162 GNUNET_snprintf(port_as_string,
1163 sizeof (port_as_string),
1167 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1169 _("Running gnunet-nat-client %s %s %u\n"),
1170 plugin->internal_address,
1172 (unsigned int) plugin->adv_port);
1174 proc = GNUNET_OS_start_process (NULL,
1176 "gnunet-nat-client",
1177 "gnunet-nat-client",
1178 plugin->internal_address,
1182 /* we know that the gnunet-nat-client will terminate virtually
1184 GNUNET_OS_process_wait (proc);
1185 GNUNET_OS_process_close (proc);
1190 * Function that can be used by the transport service to transmit
1191 * a message using the plugin. Note that in the case of a
1192 * peer disconnecting, the continuation MUST be called
1193 * prior to the disconnect notification itself. This function
1194 * will be called with this peer's HELLO message to initiate
1195 * a fresh connection to another peer.
1197 * @param cls closure
1198 * @param target who should receive this message
1199 * @param msg the message to transmit
1200 * @param msgbuf_size number of bytes in 'msg'
1201 * @param priority how important is the message (most plugins will
1202 * ignore message priority and just FIFO)
1203 * @param timeout how long to wait at most for the transmission (does not
1204 * require plugins to discard the message after the timeout,
1205 * just advisory for the desired delay; most plugins will ignore
1207 * @param session which session must be used (or NULL for "any")
1208 * @param addr the address to use (can be NULL if the plugin
1209 * is "on its own" (i.e. re-use existing TCP connection))
1210 * @param addrlen length of the address in bytes
1211 * @param force_address GNUNET_YES if the plugin MUST use the given address,
1212 * GNUNET_NO means the plugin may use any other address and
1213 * GNUNET_SYSERR means that only reliable existing
1214 * bi-directional connections should be used (regardless
1216 * @param cont continuation to call once the message has
1217 * been transmitted (or if the transport is ready
1218 * for the next transmission call; or if the
1219 * peer disconnected...); can be NULL
1220 * @param cont_cls closure for cont
1221 * @return number of bytes used (on the physical network, with overheads);
1222 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1223 * and does NOT mean that the message was not transmitted (DV and NAT)
1226 tcp_plugin_send (void *cls,
1227 const struct GNUNET_PeerIdentity *target,
1231 struct GNUNET_TIME_Relative timeout,
1232 struct Session *session,
1236 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1238 struct Plugin *plugin = cls;
1239 struct Session *cand_session;
1240 struct Session *next;
1241 struct PendingMessage *pm;
1242 struct GNUNET_CONNECTION_Handle *sa;
1246 struct sockaddr_in a4;
1247 struct sockaddr_in6 a6;
1248 const struct IPv4TcpAddress *t4;
1249 const struct IPv6TcpAddress *t6;
1250 unsigned int is_natd;
1252 GNUNET_STATISTICS_update (plugin->env->stats,
1253 gettext_noop ("# bytes TCP was asked to transmit"),
1256 /* FIXME: we could do this cheaper with a hash table
1257 where we could restrict the iteration to entries that match
1258 the target peer... */
1259 is_natd = GNUNET_NO;
1260 if (session == NULL)
1262 cand_session = NULL;
1263 next = plugin->sessions;
1264 while (NULL != (session = next))
1266 next = session->next;
1267 GNUNET_assert (session->client != NULL);
1268 if (0 != memcmp (target,
1270 sizeof (struct GNUNET_PeerIdentity)))
1272 if ( ( (GNUNET_SYSERR == force_address) &&
1273 (session->expecting_welcome == GNUNET_NO) ) ||
1274 (GNUNET_NO == force_address) )
1276 cand_session = select_better_session (cand_session,
1280 if (GNUNET_SYSERR == force_address)
1282 GNUNET_break (GNUNET_YES == force_address);
1288 if ( (addrlen != session->connect_alen) &&
1289 (session->is_nat == GNUNET_NO) )
1291 if ((0 != memcmp (session->connect_addr,
1293 addrlen)) && (session->is_nat == GNUNET_NO))
1295 cand_session = select_better_session (cand_session,
1298 session = cand_session;
1300 if ( (session == NULL) &&
1304 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1306 "Asked to transmit to `%4s' without address and I have no existing connection (failing).\n",
1307 GNUNET_i2s (target));
1309 GNUNET_STATISTICS_update (plugin->env->stats,
1310 gettext_noop ("# bytes discarded by TCP (no address and no connection)"),
1315 if (session == NULL)
1317 if (addrlen == sizeof (struct IPv6TcpAddress))
1321 memset (&a6, 0, sizeof (a6));
1322 #if HAVE_SOCKADDR_IN_SIN_LEN
1323 a6.sin6_len = sizeof (a6);
1325 a6.sin6_family = AF_INET6;
1326 a6.sin6_port = t6->t6_port;
1327 if (t6->t6_port == 0)
1328 is_natd = GNUNET_YES;
1329 memcpy (&a6.sin6_addr,
1331 sizeof (struct in6_addr));
1335 else if (addrlen == sizeof (struct IPv4TcpAddress))
1339 memset (&a4, 0, sizeof (a4));
1340 #if HAVE_SOCKADDR_IN_SIN_LEN
1341 a4.sin_len = sizeof (a4);
1343 a4.sin_family = AF_INET;
1344 a4.sin_port = t4->t_port;
1345 if (t4->t_port == 0)
1346 is_natd = GNUNET_YES;
1347 a4.sin_addr.s_addr = t4->ipv4_addr;
1353 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1355 _("Address of unexpected length: %u\n"),
1361 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1362 return -1; /* NAT client only works with IPv4 addresses */
1365 if ((plugin->allow_nat == GNUNET_YES) && (is_natd == GNUNET_YES) &&
1366 (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns, &target->hashPubKey)))
1369 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1371 _("Found valid IPv4 NAT address (creating session)!\n"));
1373 session = create_session (plugin,
1378 /* create new message entry */
1379 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1380 /* FIXME: the memset of this malloc can be up to 2% of our total runtime */
1381 pm->msg = (const char*) &pm[1];
1382 memcpy (&pm[1], msg, msgbuf_size);
1383 /* FIXME: this memcpy can be up to 7% of our total run-time
1384 (for transport service) */
1385 pm->message_size = msgbuf_size;
1386 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1387 pm->transmit_cont = cont;
1388 pm->transmit_cont_cls = cont_cls;
1390 /* append pm to pending_messages list */
1391 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1392 session->pending_messages_tail,
1393 session->pending_messages_tail,
1396 GNUNET_assert(GNUNET_CONTAINER_multihashmap_put(plugin->nat_wait_conns,
1397 &target->hashPubKey,
1399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY) == GNUNET_OK);
1401 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1403 "Created NAT WAIT connection to `%4s' at `%s'\n",
1404 GNUNET_i2s (target),
1405 GNUNET_a2s (sb, sbs));
1407 run_gnunet_nat_client (plugin, &a4);
1410 if ( (plugin->allow_nat == GNUNET_YES) &&
1411 (is_natd == GNUNET_YES) &&
1412 (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains(plugin->nat_wait_conns, &target->hashPubKey)) )
1414 /* Only do one NAT punch attempt per peer identity */
1417 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1423 "Failed to create connection to `%4s' at `%s'\n",
1424 GNUNET_i2s (target),
1425 GNUNET_a2s (sb, sbs));
1427 GNUNET_STATISTICS_update (plugin->env->stats,
1428 gettext_noop ("# bytes discarded by TCP (failed to connect)"),
1434 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1436 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1437 GNUNET_i2s (target),
1438 GNUNET_a2s (sb, sbs));
1440 session = create_session (plugin,
1442 GNUNET_SERVER_connect_socket (plugin->server,
1445 session->connect_addr = GNUNET_malloc (addrlen);
1446 memcpy (session->connect_addr,
1449 session->connect_alen = addrlen;
1451 GNUNET_assert (session != NULL);
1452 GNUNET_assert (session->client != NULL);
1453 GNUNET_STATISTICS_update (plugin->env->stats,
1454 gettext_noop ("# bytes currently in TCP buffers"),
1457 /* create new message entry */
1458 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1459 pm->msg = (const char*) &pm[1];
1460 memcpy (&pm[1], msg, msgbuf_size);
1461 pm->message_size = msgbuf_size;
1462 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1463 pm->transmit_cont = cont;
1464 pm->transmit_cont_cls = cont_cls;
1466 /* append pm to pending_messages list */
1467 GNUNET_CONTAINER_DLL_insert_after (session->pending_messages_head,
1468 session->pending_messages_tail,
1469 session->pending_messages_tail,
1472 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1474 "Asked to transmit %u bytes to `%s', added message to list.\n",
1476 GNUNET_i2s (target));
1478 process_pending_messages (session);
1484 * Function that can be called to force a disconnect from the
1485 * specified neighbour. This should also cancel all previously
1486 * scheduled transmissions. Obviously the transmission may have been
1487 * partially completed already, which is OK. The plugin is supposed
1488 * to close the connection (if applicable) and no longer call the
1489 * transmit continuation(s).
1491 * Finally, plugin MUST NOT call the services's receive function to
1492 * notify the service that the connection to the specified target was
1493 * closed after a getting this call.
1495 * @param cls closure
1496 * @param target peer for which the last transmission is
1500 tcp_plugin_disconnect (void *cls,
1501 const struct GNUNET_PeerIdentity *target)
1503 struct Plugin *plugin = cls;
1504 struct Session *session;
1505 struct Session *next;
1506 struct PendingMessage *pm;
1509 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1511 "Asked to cancel session with `%4s'\n",
1512 GNUNET_i2s (target));
1514 next = plugin->sessions;
1515 while (NULL != (session = next))
1517 next = session->next;
1518 if (0 != memcmp (target,
1520 sizeof (struct GNUNET_PeerIdentity)))
1522 pm = session->pending_messages_head;
1525 pm->transmit_cont = NULL;
1526 pm->transmit_cont_cls = NULL;
1529 disconnect_session (session);
1535 * Context for address to string conversion.
1537 struct PrettyPrinterContext
1540 * Function to call with the result.
1542 GNUNET_TRANSPORT_AddressStringCallback asc;
1545 * Clsoure for 'asc'.
1550 * Port to add after the IP address.
1557 * Append our port and forward the result.
1559 * @param cls the 'struct PrettyPrinterContext*'
1560 * @param hostname hostname part of the address
1563 append_port (void *cls, const char *hostname)
1565 struct PrettyPrinterContext *ppc = cls;
1568 if (hostname == NULL)
1570 ppc->asc (ppc->asc_cls, NULL);
1574 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1575 ppc->asc (ppc->asc_cls, ret);
1581 * Convert the transports address to a nice, human-readable
1584 * @param cls closure
1585 * @param type name of the transport that generated the address
1586 * @param addr one of the addresses of the host, NULL for the last address
1587 * the specific address format depends on the transport
1588 * @param addrlen length of the address
1589 * @param numeric should (IP) addresses be displayed in numeric form?
1590 * @param timeout after how long should we give up?
1591 * @param asc function to call on each string
1592 * @param asc_cls closure for asc
1595 tcp_plugin_address_pretty_printer (void *cls,
1600 struct GNUNET_TIME_Relative timeout,
1601 GNUNET_TRANSPORT_AddressStringCallback asc,
1604 struct Plugin *plugin = cls;
1605 struct PrettyPrinterContext *ppc;
1608 struct sockaddr_in a4;
1609 struct sockaddr_in6 a6;
1610 const struct IPv4TcpAddress *t4;
1611 const struct IPv6TcpAddress *t6;
1614 if (addrlen == sizeof (struct IPv6TcpAddress))
1617 memset (&a6, 0, sizeof (a6));
1618 a6.sin6_family = AF_INET6;
1619 a6.sin6_port = t6->t6_port;
1620 memcpy (&a6.sin6_addr,
1622 sizeof (struct in6_addr));
1623 port = ntohs (t6->t6_port);
1627 else if (addrlen == sizeof (struct IPv4TcpAddress))
1630 memset (&a4, 0, sizeof (a4));
1631 a4.sin_family = AF_INET;
1632 a4.sin_port = t4->t_port;
1633 a4.sin_addr.s_addr = t4->ipv4_addr;
1634 port = ntohs (t4->t_port);
1640 /* invalid address */
1641 GNUNET_break_op (0);
1642 asc (asc_cls, NULL);
1645 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1647 ppc->asc_cls = asc_cls;
1649 GNUNET_RESOLVER_hostname_get (plugin->env->cfg,
1652 !numeric, timeout, &append_port, ppc);
1657 * Check if the given port is plausible (must be either our listen
1658 * port or our advertised port), or any port if we are behind NAT
1659 * and do not have a port open. If it is neither, we return
1662 * @param plugin global variables
1663 * @param in_port port number to check
1664 * @return GNUNET_OK if port is either open_port or adv_port
1667 check_port (struct Plugin *plugin,
1670 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1672 return GNUNET_SYSERR;
1677 * Function that will be called to check if a binary address for this
1678 * plugin is well-formed and corresponds to an address for THIS peer
1679 * (as per our configuration). Naturally, if absolutely necessary,
1680 * plugins can be a bit conservative in their answer, but in general
1681 * plugins should make sure that the address does not redirect
1682 * traffic to a 3rd party that might try to man-in-the-middle our
1685 * @param cls closure, our 'struct Plugin*'
1686 * @param addr pointer to the address
1687 * @param addrlen length of addr
1688 * @return GNUNET_OK if this is a plausible address for this peer
1689 * and transport, GNUNET_SYSERR if not
1692 tcp_plugin_check_address (void *cls,
1696 struct Plugin *plugin = cls;
1697 struct IPv4TcpAddress *v4;
1698 struct IPv6TcpAddress *v6;
1700 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1701 (addrlen != sizeof (struct IPv6TcpAddress)))
1703 GNUNET_break_op (0);
1704 return GNUNET_SYSERR;
1706 if (addrlen == sizeof (struct IPv4TcpAddress))
1708 v4 = (struct IPv4TcpAddress *) addr;
1710 check_mapped_addr (plugin, v4, sizeof (struct IPv4TcpAddress)))
1713 check_port (plugin, ntohs (v4->t_port)))
1714 return GNUNET_SYSERR;
1716 check_local_addr (plugin, &v4->ipv4_addr, sizeof (struct in_addr)))
1717 return GNUNET_SYSERR;
1721 v6 = (struct IPv6TcpAddress *) addr;
1722 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1724 GNUNET_break_op (0);
1725 return GNUNET_SYSERR;
1728 check_mapped_addr (plugin, v6, sizeof (struct IPv6TcpAddress)))
1731 check_port (plugin, ntohs (v6->t6_port)))
1732 return GNUNET_SYSERR;
1734 check_local_addr (plugin, &v6->ipv6_addr, sizeof (struct in6_addr)))
1735 return GNUNET_SYSERR;
1742 * We've received a nat probe from this peer via TCP. Finish
1743 * creating the client session and resume sending of queued
1746 * @param cls closure
1747 * @param client identification of the client
1748 * @param message the actual message
1751 handle_tcp_nat_probe (void *cls,
1752 struct GNUNET_SERVER_Client *client,
1753 const struct GNUNET_MessageHeader *message)
1755 struct Plugin *plugin = cls;
1756 struct Session *session;
1757 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1760 struct IPv4TcpAddress *t4;
1761 struct IPv6TcpAddress *t6;
1762 const struct sockaddr_in *s4;
1763 const struct sockaddr_in6 *s6;
1766 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1768 "received tcp NAT probe\n");
1770 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1771 * a connection to this peer by running gnunet-nat-client. This peer
1772 * received the punch message and now wants us to use the new connection
1773 * as the default for that peer. Do so and then send a WELCOME message
1774 * so we can really be connected!
1776 if (ntohs(message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1781 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *)message;
1782 session = GNUNET_CONTAINER_multihashmap_get(plugin->nat_wait_conns,
1783 &tcp_nat_probe->clientIdentity.hashPubKey);
1784 if (session == NULL)
1787 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1789 "Did NOT find session for NAT probe!\n");
1791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1795 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1797 "Found session for NAT probe!\n");
1799 GNUNET_assert(GNUNET_CONTAINER_multihashmap_remove(plugin->nat_wait_conns,
1800 &tcp_nat_probe->clientIdentity.hashPubKey,
1801 session) == GNUNET_YES);
1803 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1806 GNUNET_free (session);
1807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1811 GNUNET_SERVER_client_keep (client);
1812 session->client = client;
1813 session->last_activity = GNUNET_TIME_absolute_get ();
1814 session->inbound = GNUNET_NO;
1817 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1819 "Found address `%s' for incoming connection\n",
1820 GNUNET_a2s (vaddr, alen));
1822 switch (((const struct sockaddr *)vaddr)->sa_family)
1826 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1827 t4->t_port = s4->sin_port;
1828 t4->ipv4_addr = s4->sin_addr.s_addr;
1829 session->connect_addr = t4;
1830 session->connect_alen = sizeof (struct IPv4TcpAddress);
1834 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1835 t6->t6_port = s6->sin6_port;
1836 memcpy (&t6->ipv6_addr,
1838 sizeof (struct in6_addr));
1839 session->connect_addr = t6;
1840 session->connect_alen = sizeof (struct IPv6TcpAddress);
1843 GNUNET_break_op (0);
1845 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1847 "Bad address for incoming connection!\n");
1849 GNUNET_free (vaddr);
1850 GNUNET_SERVER_client_drop (client);
1851 GNUNET_free (session);
1852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1855 GNUNET_free (vaddr);
1857 session->next = plugin->sessions;
1858 plugin->sessions = session;
1859 GNUNET_STATISTICS_update (plugin->env->stats,
1860 gettext_noop ("# TCP sessions active"),
1863 process_pending_messages (session);
1864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1869 * We've received a welcome from this peer via TCP. Possibly create a
1870 * fresh client record and send back our welcome.
1872 * @param cls closure
1873 * @param client identification of the client
1874 * @param message the actual message
1877 handle_tcp_welcome (void *cls,
1878 struct GNUNET_SERVER_Client *client,
1879 const struct GNUNET_MessageHeader *message)
1881 struct Plugin *plugin = cls;
1882 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1883 struct Session *session;
1886 struct IPv4TcpAddress *t4;
1887 struct IPv6TcpAddress *t6;
1888 const struct sockaddr_in *s4;
1889 const struct sockaddr_in6 *s6;
1892 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1894 "Received %s message from `%4s'.\n",
1896 GNUNET_i2s (&wm->clientIdentity));
1898 GNUNET_STATISTICS_update (plugin->env->stats,
1899 gettext_noop ("# TCP WELCOME messages received"),
1902 session = find_session_by_client (plugin, client);
1904 if (session == NULL)
1907 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1909 "Received %s message from a `%4s', creating new session\n",
1911 GNUNET_i2s (&wm->clientIdentity));
1913 GNUNET_SERVER_client_keep (client);
1914 session = create_session (plugin,
1915 &wm->clientIdentity,
1918 session->inbound = GNUNET_YES;
1920 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1923 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1925 "Found address `%s' for incoming connection\n",
1926 GNUNET_a2s (vaddr, alen));
1928 if (alen == sizeof (struct sockaddr_in))
1931 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1932 t4->t_port = s4->sin_port;
1933 t4->ipv4_addr = s4->sin_addr.s_addr;
1934 session->connect_addr = t4;
1935 session->connect_alen = sizeof (struct IPv4TcpAddress);
1937 else if (alen == sizeof (struct sockaddr_in6))
1940 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1941 t6->t6_port = s6->sin6_port;
1942 memcpy (&t6->ipv6_addr,
1944 sizeof (struct in6_addr));
1945 session->connect_addr = t6;
1946 session->connect_alen = sizeof (struct IPv6TcpAddress);
1949 GNUNET_free (vaddr);
1954 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1956 "Did not obtain TCP socket address for incoming connection\n");
1959 process_pending_messages (session);
1965 GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1967 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1969 "Found address `%s' (already have session)\n",
1970 GNUNET_a2s (vaddr, alen));
1975 if (session->expecting_welcome != GNUNET_YES)
1977 GNUNET_break_op (0);
1978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1981 session->last_activity = GNUNET_TIME_absolute_get ();
1982 session->expecting_welcome = GNUNET_NO;
1983 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1988 * Task to signal the server that we can continue
1989 * receiving from the TCP client now.
1991 * @param cls the 'struct Session*'
1992 * @param tc task context (unused)
1995 delayed_done (void *cls,
1996 const struct GNUNET_SCHEDULER_TaskContext *tc)
1998 struct Session *session = cls;
1999 struct GNUNET_TIME_Relative delay;
2001 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2002 delay = session->plugin->env->receive (session->plugin->env->cls,
2007 if (delay.rel_value == 0)
2008 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2010 session->receive_delay_task =
2011 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2016 * We've received data for this peer via TCP. Unbox,
2017 * compute latency and forward.
2019 * @param cls closure
2020 * @param client identification of the client
2021 * @param message the actual message
2024 handle_tcp_data (void *cls,
2025 struct GNUNET_SERVER_Client *client,
2026 const struct GNUNET_MessageHeader *message)
2028 struct Plugin *plugin = cls;
2029 struct Session *session;
2030 struct GNUNET_TIME_Relative delay;
2033 type = ntohs (message->type);
2034 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2035 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type) )
2037 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2038 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2041 session = find_session_by_client (plugin, client);
2042 if ( (NULL == session) || (GNUNET_YES == session->expecting_welcome) )
2044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2047 session->last_activity = GNUNET_TIME_absolute_get ();
2049 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2051 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2052 (unsigned int) ntohs (message->size),
2053 (unsigned int) ntohs (message->type),
2054 GNUNET_i2s (&session->target));
2056 GNUNET_STATISTICS_update (plugin->env->stats,
2057 gettext_noop ("# bytes received via TCP"),
2058 ntohs (message->size),
2060 delay = plugin->env->receive (plugin->env->cls, &session->target, message, 1,
2062 (GNUNET_YES == session->inbound) ? NULL : session->connect_addr,
2063 (GNUNET_YES == session->inbound) ? 0 : session->connect_alen);
2064 if (delay.rel_value == 0)
2065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2067 session->receive_delay_task =
2068 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2073 * Functions with this signature are called whenever a peer
2074 * is disconnected on the network level.
2076 * @param cls closure
2077 * @param client identification of the client
2080 disconnect_notify (void *cls,
2081 struct GNUNET_SERVER_Client *client)
2083 struct Plugin *plugin = cls;
2084 struct Session *session;
2088 session = find_session_by_client (plugin, client);
2089 if (session == NULL)
2090 return; /* unknown, nothing to do */
2092 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2094 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2095 GNUNET_i2s (&session->target),
2096 (session->connect_addr != NULL) ?
2097 tcp_address_to_string (session->plugin,
2098 session->connect_addr,
2099 session->connect_alen) : "*");
2101 disconnect_session (session);
2106 * Add the IP of our network interface to the list of
2107 * our internal IP addresses.
2109 * @param cls the 'struct Plugin*'
2110 * @param name name of the interface
2111 * @param isDefault do we think this may be our default interface
2112 * @param addr address of the interface
2113 * @param addrlen number of bytes in addr
2114 * @return GNUNET_OK to continue iterating
2117 process_interfaces (void *cls,
2120 const struct sockaddr *addr, socklen_t addrlen)
2122 struct Plugin *plugin = cls;
2124 struct IPv4TcpAddress t4;
2125 struct IPv6TcpAddress t6;
2126 struct IPv4TcpAddress t4_nat;
2127 struct IPv6TcpAddress t6_nat;
2131 char buf[INET_ADDRSTRLEN];
2133 af = addr->sa_family;
2138 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2139 GNUNET_assert (NULL != inet_ntop(AF_INET,
2143 if ( (plugin->bind_address != NULL) &&
2144 (0 != strcmp(buf, plugin->bind_address)) )
2147 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2149 "Not notifying transport of address `%s' (redundant)\n",
2150 GNUNET_a2s (addr, addrlen));
2154 if ( (plugin->internal_address == NULL) &&
2156 plugin->internal_address = GNUNET_strdup (buf);
2157 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2158 if (plugin->behind_nat == GNUNET_YES)
2160 /* Also advertise as NAT (with port 0) */
2161 t4_nat.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
2162 t4_nat.t_port = htons(0);
2165 t4.t_port = htons (plugin->adv_port);
2170 if ( (IN6_IS_ADDR_LINKLOCAL (&((struct sockaddr_in6 *) addr)->sin6_addr)) ||
2171 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno(plugin->env->cfg,
2175 /* skip link local addresses */
2178 memcpy (&t6.ipv6_addr,
2179 &((struct sockaddr_in6 *) addr)->sin6_addr,
2180 sizeof (struct in6_addr));
2181 add_to_address_list (plugin,
2183 sizeof (struct in6_addr));
2184 if (plugin->behind_nat == GNUNET_YES)
2186 /* Also advertise as NAT (with port 0) */
2187 memcpy (&t6_nat.ipv6_addr,
2188 &((struct sockaddr_in6 *) addr)->sin6_addr,
2189 sizeof (struct in6_addr));
2190 t6_nat.t6_port = htons(0);
2193 t6.t6_port = htons (plugin->adv_port);
2202 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2204 "Found address `%s' (%s) len %d\n",
2205 GNUNET_a2s (addr, addrlen), name, args);
2207 plugin->env->notify_address (plugin->env->cls,
2209 arg, args, GNUNET_TIME_UNIT_FOREVER_REL);
2211 if (arg_nat != NULL)
2213 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2215 _("Found address `%s' (%s) len %d\n"),
2216 GNUNET_a2s (addr, addrlen), name, args);
2217 plugin->env->notify_address (plugin->env->cls,
2219 arg_nat, args, GNUNET_TIME_UNIT_FOREVER_REL);
2227 * Function called by the resolver for each address obtained from DNS
2228 * for our own hostname. Add the addresses to the list of our
2229 * external IP addresses.
2231 * @param cls closure
2232 * @param addr one of the addresses of the host, NULL for the last address
2233 * @param addrlen length of the address
2236 process_hostname_ips (void *cls,
2237 const struct sockaddr *addr, socklen_t addrlen)
2239 struct Plugin *plugin = cls;
2243 plugin->hostname_dns = NULL;
2246 /* FIXME: Can we figure out our external address here so it doesn't need to be user specified? */
2247 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
2252 * We can now send a probe message, copy into buffer to really send.
2254 * @param cls closure, a struct TCPProbeContext
2255 * @param size max size to copy
2256 * @param buf buffer to copy message to
2257 * @return number of bytes copied into buf
2260 notify_send_probe (void *cls,
2264 struct TCPProbeContext *tcp_probe_ctx = cls;
2265 struct Plugin *plugin = tcp_probe_ctx->plugin;
2268 tcp_probe_ctx->transmit_handle = NULL;
2269 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2274 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock, GNUNET_NO);
2275 GNUNET_free(tcp_probe_ctx);
2278 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2279 memcpy(buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2280 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2281 tcp_probe_ctx->sock);
2282 ret = sizeof(tcp_probe_ctx->message);
2283 GNUNET_free(tcp_probe_ctx);
2289 * We have been notified that gnunet-nat-server has written something to stdout.
2290 * Handle the output, then reschedule this function to be called again once
2291 * more is available.
2293 * @param cls the plugin handle
2294 * @param tc the scheduling context
2297 tcp_plugin_server_read (void *cls,
2298 const struct GNUNET_SCHEDULER_TaskContext *tc)
2300 struct Plugin *plugin = cls;
2305 const char *port_start;
2306 struct sockaddr_in sin_addr;
2307 struct TCPProbeContext *tcp_probe_ctx;
2308 struct GNUNET_CONNECTION_Handle *sock;
2310 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
2312 memset (mybuf, 0, sizeof(mybuf));
2313 bytes = GNUNET_DISK_file_read(plugin->server_stdout_handle,
2319 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2321 "Finished reading from server stdout with code: %d\n",
2328 for (i = 0; i < sizeof(mybuf); i++)
2330 if (mybuf[i] == '\n')
2335 if ( (mybuf[i] == ':') && (i + 1 < sizeof(mybuf)) )
2338 port_start = &mybuf[i + 1];
2342 /* construct socket address of sender */
2343 memset (&sin_addr, 0, sizeof (sin_addr));
2344 sin_addr.sin_family = AF_INET;
2345 sin_addr.sin_port = htons((uint16_t) port);
2346 #if HAVE_SOCKADDR_IN_SIN_LEN
2347 sin_addr.sin_len = sizeof (sin_addr);
2349 if ( (NULL == port_start) ||
2350 (1 != sscanf (port_start, "%d", &port)) ||
2351 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)) )
2353 /* should we restart gnunet-nat-server? */
2354 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2356 _("gnunet-nat-server generated malformed address `%s'\n"),
2358 plugin->server_read_task
2359 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2360 plugin->server_stdout_handle,
2361 &tcp_plugin_server_read,
2367 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2369 "gnunet-nat-server read: %s:%d\n",
2374 * We have received an ICMP response, ostensibly from a peer
2375 * that wants to connect to us! Send a message to establish a connection.
2377 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET,
2378 (const struct sockaddr *)&sin_addr,
2382 /* failed for some odd reason (out of sockets?); ignore attempt */
2383 plugin->server_read_task =
2384 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2385 plugin->server_stdout_handle,
2386 &tcp_plugin_server_read,
2391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2392 "Sending TCP probe message to `%s:%u'!\n",
2394 (unsigned int) port);
2395 /* FIXME: do we need to track these probe context objects so that
2396 we can clean them up on plugin unload? */
2398 = GNUNET_malloc(sizeof(struct TCPProbeContext));
2399 tcp_probe_ctx->message.header.size
2400 = htons(sizeof(struct TCP_NAT_ProbeMessage));
2401 tcp_probe_ctx->message.header.type
2402 = htons(GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2403 memcpy (&tcp_probe_ctx->message.clientIdentity,
2404 plugin->env->my_identity,
2405 sizeof(struct GNUNET_PeerIdentity));
2406 tcp_probe_ctx->plugin = plugin;
2407 tcp_probe_ctx->sock = sock;
2408 GNUNET_CONTAINER_DLL_insert (plugin->probe_head,
2411 tcp_probe_ctx->transmit_handle
2412 = GNUNET_CONNECTION_notify_transmit_ready (sock,
2413 ntohs (tcp_probe_ctx->message.header.size),
2414 GNUNET_TIME_UNIT_FOREVER_REL,
2415 ¬ify_send_probe, tcp_probe_ctx);
2417 plugin->server_read_task =
2418 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2419 plugin->server_stdout_handle,
2420 &tcp_plugin_server_read,
2426 * Start the gnunet-nat-server process for users behind NAT.
2428 * @param plugin the transport plugin
2429 * @return GNUNET_YES if process was started, GNUNET_SYSERR on error
2432 tcp_transport_start_nat_server (struct Plugin *plugin)
2434 if (plugin->internal_address == NULL)
2435 return GNUNET_SYSERR;
2436 plugin->server_stdout = GNUNET_DISK_pipe (GNUNET_YES,
2439 if (plugin->server_stdout == NULL)
2440 return GNUNET_SYSERR;
2442 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2445 "gnunet-nat-server",
2446 plugin->internal_address);
2448 /* Start the server process */
2449 plugin->server_proc = GNUNET_OS_start_process (NULL,
2450 plugin->server_stdout,
2451 "gnunet-nat-server",
2452 "gnunet-nat-server",
2453 plugin->internal_address,
2455 if (plugin->server_proc == NULL)
2457 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2459 _("Failed to start %s\n"),
2460 "gnunet-nat-server");
2461 GNUNET_DISK_pipe_close (plugin->server_stdout);
2462 plugin->server_stdout = NULL;
2463 return GNUNET_SYSERR;
2465 /* Close the write end of the read pipe */
2466 GNUNET_DISK_pipe_close_end(plugin->server_stdout,
2467 GNUNET_DISK_PIPE_END_WRITE);
2468 plugin->server_stdout_handle
2469 = GNUNET_DISK_pipe_handle (plugin->server_stdout,
2470 GNUNET_DISK_PIPE_END_READ);
2471 plugin->server_read_task
2472 = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2473 plugin->server_stdout_handle,
2474 &tcp_plugin_server_read,
2481 * Return the actual path to a file found in the current
2482 * PATH environment variable.
2484 * @param binary the name of the file to find
2485 * @return path to binary, NULL if not found
2488 get_path_from_PATH (const char *binary)
2496 p = getenv ("PATH");
2499 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2501 _("PATH environment variable is unset.\n"));
2504 path = GNUNET_strdup (p); /* because we write on it */
2505 buf = GNUNET_malloc (strlen (path) + 20);
2508 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
2511 sprintf (buf, "%s/%s", pos, binary);
2512 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2519 sprintf (buf, "%s/%s", pos, binary);
2520 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
2532 * Check whether the suid bit is set on a file.
2533 * Attempts to find the file using the current
2534 * PATH environment variable as a search path.
2536 * @param binary the name of the file to check
2537 * @return GNUNET_YES if the file is SUID,
2539 * GNUNET_SYSERR on error
2542 check_gnunet_nat_binary (const char *binary)
2544 struct stat statbuf;
2550 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
2551 p = get_path_from_PATH (binaryexe);
2554 p = get_path_from_PATH (binary);
2558 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2560 _("Could not find binary `%s' in PATH!\n"),
2564 if (0 != STAT (p, &statbuf))
2566 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2567 _("stat (%s) failed: %s\n"),
2571 return GNUNET_SYSERR;
2575 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
2576 (statbuf.st_uid == 0) )
2580 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
2581 if (INVALID_SOCKET == rawsock)
2583 DWORD err = GetLastError ();
2584 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2586 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) failed! GLE = %d\n", err);
2587 return GNUNET_NO; /* not running as administrator */
2589 closesocket (rawsock);
2596 * Entry point for the plugin.
2598 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2599 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2602 libgnunet_plugin_transport_tcp_init (void *cls)
2604 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2605 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2606 sizeof (struct WelcomeMessage)},
2607 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE, sizeof (struct TCP_NAT_ProbeMessage)},
2608 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2611 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2612 struct GNUNET_TRANSPORT_PluginFunctions *api;
2613 struct Plugin *plugin;
2614 struct GNUNET_SERVICE_Context *service;
2615 unsigned long long aport;
2616 unsigned long long bport;
2620 char *internal_address;
2621 char *external_address;
2622 struct sockaddr_in in_addr;
2623 struct IPv4TcpAddress t4;
2624 struct GNUNET_TIME_Relative idle_timeout;
2626 behind_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2629 if ( (GNUNET_YES == behind_nat) &&
2630 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server")) )
2632 behind_nat = GNUNET_NO;
2633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2634 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2635 "gnunet-nat-server");
2638 allow_nat = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2641 if ( (GNUNET_YES == allow_nat) &&
2642 (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-client")) )
2644 allow_nat = GNUNET_NO;
2645 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2646 _("Configuration requires `%s', but binary is not installed properly (SUID bit not set). Option disabled.\n"),
2647 "gnunet-nat-client");
2650 external_address = NULL;
2652 GNUNET_CONFIGURATION_have_value (env->cfg,
2654 "EXTERNAL_ADDRESS"))
2656 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2662 if ( (external_address != NULL) &&
2663 (inet_pton(AF_INET, external_address, &in_addr.sin_addr) != 1) )
2665 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2667 _("Malformed %s `%s' given in configuration!\n"),
2673 internal_address = NULL;
2675 GNUNET_CONFIGURATION_have_value (env->cfg,
2677 "INTERNAL_ADDRESS"))
2679 (void) GNUNET_CONFIGURATION_get_value_string (env->cfg,
2685 if ( (internal_address != NULL) &&
2686 (inet_pton(AF_INET, internal_address, &in_addr.sin_addr) != 1) )
2688 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2690 _("Malformed %s `%s' given in configuration!\n"),
2693 GNUNET_free_non_null(internal_address);
2694 GNUNET_free_non_null(external_address);
2700 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2706 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2712 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2714 _("Require valid port number for service `%s' in configuration!\n"),
2716 GNUNET_free_non_null(external_address);
2717 GNUNET_free_non_null(internal_address);
2728 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg);
2729 if (service == NULL)
2731 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2733 _("Failed to start service.\n"));
2740 plugin = GNUNET_malloc (sizeof (struct Plugin));
2741 plugin->open_port = bport;
2742 plugin->adv_port = aport;
2743 plugin->external_address = external_address;
2744 plugin->internal_address = internal_address;
2745 plugin->behind_nat = behind_nat;
2746 plugin->allow_nat = allow_nat;
2747 plugin->allow_upnp = GNUNET_CONFIGURATION_get_value_yesno (env->cfg,
2751 plugin->lsock = NULL;
2752 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2754 api->send = &tcp_plugin_send;
2755 api->disconnect = &tcp_plugin_disconnect;
2756 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2757 api->check_address = &tcp_plugin_check_address;
2758 api->address_to_string = &tcp_address_to_string;
2759 plugin->service = service;
2760 if (service != NULL)
2762 plugin->server = GNUNET_SERVICE_get_server (service);
2767 GNUNET_CONFIGURATION_get_value_time (env->cfg,
2772 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2774 _("Failed to find option %s in section %s!\n"),
2777 GNUNET_free_non_null(external_address);
2778 GNUNET_free_non_null(internal_address);
2782 plugin->server = GNUNET_SERVER_create_with_sockets (NULL, NULL, NULL,
2783 idle_timeout, GNUNET_YES);
2785 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2786 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2788 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2790 plugin->handlers[i].callback_cls = plugin;
2791 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2792 GNUNET_SERVER_disconnect_notify (plugin->server,
2795 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2797 if ( (plugin->behind_nat == GNUNET_YES) &&
2798 (GNUNET_YES != tcp_transport_start_nat_server(plugin)) )
2800 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2802 _("Failed to start %s required for NAT in %s!\n"),
2805 GNUNET_free_non_null(external_address);
2806 GNUNET_free_non_null(internal_address);
2807 if (service != NULL)
2808 GNUNET_SERVICE_stop (service);
2810 GNUNET_SERVER_destroy (plugin->server);
2815 if (allow_nat == GNUNET_YES)
2817 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create(16);
2818 GNUNET_assert (plugin->nat_wait_conns != NULL);
2822 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2824 _("TCP transport listening on port %llu\n"),
2827 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2829 _("TCP transport not listening on any port (client only)\n"));
2831 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2833 _("TCP transport advertises itself as being on port %llu\n"),
2836 if (GNUNET_YES == GNUNET_CONFIGURATION_get_value_string (env->cfg,
2839 &plugin->bind_address))
2841 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2843 _("Binding TCP plugin to specific address: `%s'\n"),
2844 plugin->bind_address);
2847 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->cfg,
2849 HOSTNAME_RESOLVE_TIMEOUT,
2850 &process_hostname_ips,
2853 if ( (plugin->external_address != NULL) &&
2855 plugin->external_address,
2856 &t4.ipv4_addr) == 1) )
2858 if (plugin->behind_nat == GNUNET_YES)
2860 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2862 "Notifying transport of address %s:0\n",
2863 plugin->external_address);
2864 t4.t_port = htons(0);
2868 t4.t_port = htons(plugin->adv_port);
2869 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
2871 "Notifying transport of address %s:%d\n",
2872 plugin->external_address,
2875 add_to_address_list (plugin, &t4.ipv4_addr, sizeof (struct in_addr));
2876 plugin->env->notify_address (plugin->env->cls,
2878 &t4, sizeof(t4), GNUNET_TIME_UNIT_FOREVER_REL);
2885 * Exit point from the plugin.
2888 libgnunet_plugin_transport_tcp_done (void *cls)
2890 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2891 struct Plugin *plugin = api->cls;
2892 struct Session *session;
2893 struct LocalAddrList *lal;
2894 struct TCPProbeContext *tcp_probe;
2896 while (NULL != (session = plugin->sessions))
2897 disconnect_session (session);
2898 if (NULL != plugin->hostname_dns)
2900 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2901 plugin->hostname_dns = NULL;
2903 if (plugin->service != NULL)
2904 GNUNET_SERVICE_stop (plugin->service);
2906 GNUNET_SERVER_destroy (plugin->server);
2907 GNUNET_free (plugin->handlers);
2908 while (NULL != (lal = plugin->lal_head))
2910 GNUNET_CONTAINER_DLL_remove (plugin->lal_head,
2913 if (lal->nat != NULL)
2914 GNUNET_NAT_unregister (lal->nat);
2915 GNUNET_free_non_null (lal->external_nat_address);
2918 while (NULL != (tcp_probe = plugin->probe_head))
2920 GNUNET_CONTAINER_DLL_remove (plugin->probe_head,
2923 GNUNET_CONNECTION_destroy (tcp_probe->sock, GNUNET_NO);
2924 GNUNET_free (tcp_probe);
2927 if (plugin->behind_nat == GNUNET_YES)
2929 if (0 != GNUNET_OS_process_kill (plugin->server_proc, SIGTERM))
2930 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
2931 GNUNET_OS_process_wait (plugin->server_proc);
2932 GNUNET_OS_process_close (plugin->server_proc);
2933 plugin->server_proc = NULL;
2935 GNUNET_free_non_null(plugin->bind_address);
2936 GNUNET_free (plugin);
2941 /* end of plugin_transport_tcp.c */