2 This file is part of GNUnet
3 (C) 2002--2013 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_constants.h"
28 #include "gnunet_connection_lib.h"
29 #include "gnunet_container_lib.h"
30 #include "gnunet_nat_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_service_lib.h"
36 #include "gnunet_signatures.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "gnunet_transport_plugin.h"
40 #include "transport.h"
42 #define LOG(kind,...) GNUNET_log_from (kind, "transport-tcp",__VA_ARGS__)
44 #define PLUGIN_NAME "tcp"
47 * How long until we give up on establishing an NAT connection?
50 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
53 GNUNET_NETWORK_STRUCT_BEGIN
58 static uint32_t myoptions;
61 * Initial handshake message for a session.
66 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
68 struct GNUNET_MessageHeader header;
71 * Identity of the node connecting (TCP client)
73 struct GNUNET_PeerIdentity clientIdentity;
79 * Basically a WELCOME message, but with the purpose
80 * of giving the waiting peer a client handle to use
82 struct TCP_NAT_ProbeMessage
85 * Type is GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
87 struct GNUNET_MessageHeader header;
90 * Identity of the sender of the message.
92 struct GNUNET_PeerIdentity clientIdentity;
95 GNUNET_NETWORK_STRUCT_END
98 * Context for sending a NAT probe via TCP.
100 struct TCPProbeContext
104 * Active probes are kept in a DLL.
106 struct TCPProbeContext *next;
109 * Active probes are kept in a DLL.
111 struct TCPProbeContext *prev;
116 struct GNUNET_CONNECTION_Handle *sock;
119 * Message to be sent.
121 struct TCP_NAT_ProbeMessage message;
124 * Handle to the transmission.
126 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
129 * Transport plugin handle.
131 struct Plugin *plugin;
135 GNUNET_NETWORK_STRUCT_BEGIN
138 * Network format for IPv4 addresses.
140 struct IPv4TcpAddress
143 * Optional options and flags for this address
148 * IPv4 address, in network byte order.
150 uint32_t ipv4_addr GNUNET_PACKED;
153 * Port number, in network byte order.
155 uint16_t t4_port GNUNET_PACKED;
161 * Network format for IPv6 addresses.
163 struct IPv6TcpAddress
166 * Optional flags for this address
173 struct in6_addr ipv6_addr GNUNET_PACKED;
176 * Port number, in network byte order.
178 uint16_t t6_port GNUNET_PACKED;
181 GNUNET_NETWORK_STRUCT_END
184 * Encapsulation of all of the state of the plugin.
190 * Information kept for each message that is yet to
193 struct PendingMessage
197 * This is a doubly-linked list.
199 struct PendingMessage *next;
202 * This is a doubly-linked list.
204 struct PendingMessage *prev;
207 * The pending message
212 * Continuation function to call once the message
213 * has been sent. Can be NULL if there is no
214 * continuation to call.
216 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
219 * Closure for transmit_cont.
221 void *transmit_cont_cls;
224 * Timeout value for the pending message.
226 struct GNUNET_TIME_Absolute timeout;
229 * So that the gnunet-service-transport can group messages together,
230 * these pending messages need to accept a message buffer and size
231 * instead of just a GNUNET_MessageHeader.
239 * Session handle for TCP connections.
244 * To whom are we talking to (set to our identity
245 * if we are still waiting for the welcome message)
247 struct GNUNET_PeerIdentity target;
252 struct SessionHeader header;
255 * Pointer to the global plugin struct.
257 struct Plugin *plugin;
260 * The client (used to identify this connection)
262 struct GNUNET_SERVER_Client *client;
265 * Task cleaning up a NAT client connection establishment attempt;
267 GNUNET_SCHEDULER_TaskIdentifier nat_connection_timeout;
270 * Messages currently pending for transmission
271 * to this peer, if any.
273 struct PendingMessage *pending_messages_head;
276 * Messages currently pending for transmission
277 * to this peer, if any.
279 struct PendingMessage *pending_messages_tail;
282 * Handle for pending transmission request.
284 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
287 * ID of task used to delay receiving more to throttle sender.
289 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
292 * Session timeout task
294 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
297 * Address of the other peer (either based on our 'connect'
298 * call or on our 'accept' call).
300 * struct IPv4TcpAddress or struct IPv6TcpAddress
310 * Last activity on this connection. Used to select preferred
313 struct GNUNET_TIME_Absolute last_activity;
316 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
318 int expecting_welcome;
321 * Was this a connection that was inbound (we accepted)? (#GNUNET_YES/#GNUNET_NO)
326 * Was this session created using NAT traversal?
331 * ATS network type in NBO
333 enum GNUNET_ATS_Network_Type ats_address_network_type;
338 * Encapsulation of all of the state of the plugin.
345 struct GNUNET_TRANSPORT_PluginEnvironment *env;
350 struct GNUNET_CONNECTION_Handle *lsock;
353 * Our handle to the NAT module.
355 struct GNUNET_NAT_Handle *nat;
358 * Map from peer identities to sessions for the given peer.
360 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
363 * Handle to the network service.
365 struct GNUNET_SERVICE_Context *service;
368 * Handle to the server for this service.
370 struct GNUNET_SERVER_Handle *server;
373 * Copy of the handler array where the closures are
374 * set to this struct's instance.
376 struct GNUNET_SERVER_MessageHandler *handlers;
379 * Map of peers we have tried to contact behind a NAT
381 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
384 * List of active TCP probes.
386 struct TCPProbeContext *probe_head;
389 * List of active TCP probes.
391 struct TCPProbeContext *probe_tail;
394 * Handle for (DYN)DNS lookup of our external IP.
396 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
399 * How many more TCP sessions are we allowed to open right now?
401 unsigned long long max_connections;
404 * How many more TCP sessions do we have right now?
406 unsigned long long cur_connections;
409 * ID of task used to update our addresses when one expires.
411 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
414 * Port that we are actually listening on.
419 * Port that the user said we would have visible to the
428 * Start session timeout
431 start_session_timeout (struct Session *s);
435 * Increment session timeout due to activity
438 reschedule_session_timeout (struct Session *s);
444 * @param s session to cancel timeout for
447 stop_session_timeout (struct Session *s);
452 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
455 static unsigned int sessions;
459 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
462 unsigned int size = GNUNET_CONTAINER_multipeermap_size (plugin->sessionmap);
463 if (sessions != size)
464 LOG (GNUNET_ERROR_TYPE_DEBUG,
465 "Inconsistent sessions %u <-> session map size: %u\n",
467 LOG (GNUNET_ERROR_TYPE_DEBUG,
468 "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
472 GNUNET_i2s (&session->target),
473 tcp_address_to_string (NULL, session->addr, session->addrlen));
478 dec_sessions (struct Plugin *plugin,
479 struct Session *session, int line)
481 GNUNET_assert (sessions > 0);
482 unsigned int size = GNUNET_CONTAINER_multipeermap_size(plugin->sessionmap);
484 if (sessions != size)
485 LOG (GNUNET_ERROR_TYPE_DEBUG,
486 "Inconsistent sessions %u <-> session map size: %u\n",
488 LOG (GNUNET_ERROR_TYPE_DEBUG,
489 "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
493 GNUNET_i2s (&session->target),
494 tcp_address_to_string (NULL, session->addr, session->addrlen));
500 * Function to check if an inbound connection is acceptable.
501 * Mostly used to limit the total number of open connections
504 * @param cls the 'struct Plugin'
505 * @param ucred credentials, if available, otherwise NULL
506 * @param addr address
507 * @param addrlen length of address
508 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
509 * for unknown address family (will be denied).
512 plugin_tcp_access_check (void *cls,
513 const struct GNUNET_CONNECTION_Credentials *ucred,
514 const struct sockaddr *addr, socklen_t addrlen)
516 struct Plugin *plugin = cls;
517 LOG (GNUNET_ERROR_TYPE_DEBUG,
518 "Accepting new incoming TCP connection from `%s'\n",
519 GNUNET_a2s (addr, addrlen));
520 if (plugin->cur_connections >= plugin->max_connections)
522 plugin->cur_connections ++;
528 * Our external IP address/port mapping has changed.
530 * @param cls closure, the 'struct LocalAddrList'
531 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
532 * the previous (now invalid) one
533 * @param addr either the previous or the new public IP address
534 * @param addrlen actual lenght of the address
537 tcp_nat_port_map_callback (void *cls, int add_remove,
538 const struct sockaddr *addr, socklen_t addrlen)
540 struct Plugin *plugin = cls;
541 struct IPv4TcpAddress t4;
542 struct IPv6TcpAddress t6;
546 LOG (GNUNET_ERROR_TYPE_INFO,
547 "NAT notification to %s address `%s'\n",
548 (GNUNET_YES == add_remove) ? "add" : "remove",
549 GNUNET_a2s (addr, addrlen));
550 /* convert 'addr' to our internal format */
551 switch (addr->sa_family)
554 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
555 memset (&t4,0, sizeof (t4));
556 t4.options = htonl (myoptions);
557 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
558 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
563 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
564 memset (&t6, 0, sizeof (t6));
565 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
566 sizeof (struct in6_addr));
567 t6.options = htonl (myoptions);
568 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
576 /* modify our published address list */
577 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
582 * Function called for a quick conversion of the binary address to
583 * a numeric address. Note that the caller must not free the
584 * address and that the next call to this function is allowed
585 * to override the address again.
587 * @param cls closure ('struct Plugin*')
588 * @param addr binary address
589 * @param addrlen length of the address
590 * @return string representing the same address
593 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
595 static char rbuf[INET6_ADDRSTRLEN + 12];
596 char buf[INET6_ADDRSTRLEN];
600 const struct IPv4TcpAddress *t4;
601 const struct IPv6TcpAddress *t6;
608 case sizeof (struct IPv6TcpAddress):
611 port = ntohs (t6->t6_port);
612 options = ntohl (t6->options);
613 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
616 case sizeof (struct IPv4TcpAddress):
619 port = ntohs (t4->t4_port);
620 options = ntohl (t4->options);
621 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
626 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s",
627 TRANSPORT_SESSION_INBOUND_STRING);
631 LOG (GNUNET_ERROR_TYPE_WARNING,
632 _("Unexpected address length: %u bytes\n"),
633 (unsigned int) addrlen);
636 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
638 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
641 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
642 PLUGIN_NAME, options, buf, port);
648 * Function called to convert a string address to
651 * @param cls closure ('struct Plugin*')
652 * @param addr string address
653 * @param addrlen length of the address
654 * @param buf location to store the buffer
655 * @param added location to store the number of bytes in the buffer.
656 * If the function returns #GNUNET_SYSERR, its contents are undefined.
657 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
660 tcp_string_to_address (void *cls,
663 void **buf, size_t *added)
665 struct sockaddr_storage socket_address;
671 /* Format tcp.options.address:port */
675 if ((NULL == addr) || (addrlen == 0))
678 return GNUNET_SYSERR;
680 if ('\0' != addr[addrlen - 1])
683 return GNUNET_SYSERR;
685 if (strlen (addr) != addrlen - 1)
688 return GNUNET_SYSERR;
690 plugin = GNUNET_strdup (addr);
691 optionstr = strchr (plugin, '.');
692 if (NULL == optionstr)
695 GNUNET_free (plugin);
696 return GNUNET_SYSERR;
700 options = atol (optionstr);
701 address = strchr (optionstr, '.');
705 GNUNET_free (plugin);
706 return GNUNET_SYSERR;
712 GNUNET_STRINGS_to_address_ip (address, strlen (address),
716 GNUNET_free (plugin);
717 return GNUNET_SYSERR;
720 GNUNET_free (plugin);
721 switch (socket_address.ss_family)
725 struct IPv4TcpAddress *t4;
726 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
727 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
728 t4->options = htonl (options);
729 t4->ipv4_addr = in4->sin_addr.s_addr;
730 t4->t4_port = in4->sin_port;
732 *added = sizeof (struct IPv4TcpAddress);
737 struct IPv6TcpAddress *t6;
738 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
739 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
740 t6->options = htonl (options);
741 t6->ipv6_addr = in6->sin6_addr;
742 t6->t6_port = in6->sin6_port;
744 *added = sizeof (struct IPv6TcpAddress);
748 return GNUNET_SYSERR;
753 struct SessionClientCtx
755 const struct GNUNET_SERVER_Client *client;
761 session_lookup_by_client_it (void *cls,
762 const struct GNUNET_PeerIdentity *key,
765 struct SessionClientCtx *sc_ctx = cls;
766 struct Session *s = value;
768 if (s->client == sc_ctx->client)
778 * Find the session handle for the given client.
780 * @param plugin the plugin
781 * @param client which client to find the session handle for
782 * @return NULL if no matching session exists
784 static struct Session *
785 lookup_session_by_client (struct Plugin *plugin,
786 const struct GNUNET_SERVER_Client *client)
788 struct SessionClientCtx sc_ctx;
790 sc_ctx.client = client;
792 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
798 * Create a new session. Also queues a welcome message.
800 * @param plugin the plugin
801 * @param target peer to connect to
802 * @param client client to use, reference counter must have already been increased
803 * @param is_nat this a NAT session, we should wait for a client to
804 * connect to us from an address, then assign that to
806 * @return new session object
808 static struct Session *
809 create_session (struct Plugin *plugin,
810 const struct GNUNET_PeerIdentity *target,
811 struct GNUNET_SERVER_Client *client, int is_nat)
813 struct Session *session;
814 struct PendingMessage *pm;
815 struct WelcomeMessage welcome;
817 if (GNUNET_YES != is_nat)
818 GNUNET_assert (NULL != client);
820 GNUNET_assert (NULL == client);
822 LOG (GNUNET_ERROR_TYPE_DEBUG,
823 "Creating new session for peer `%4s'\n",
824 GNUNET_i2s (target));
825 session = GNUNET_new (struct Session);
826 session->last_activity = GNUNET_TIME_absolute_get ();
827 session->plugin = plugin;
828 session->is_nat = is_nat;
829 session->client = client;
830 session->target = *target;
831 session->expecting_welcome = GNUNET_YES;
832 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
833 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
834 sizeof (struct WelcomeMessage));
835 pm->msg = (const char *) &pm[1];
836 pm->message_size = sizeof (struct WelcomeMessage);
837 welcome.header.size = htons (sizeof (struct WelcomeMessage));
838 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
839 welcome.clientIdentity = *plugin->env->my_identity;
840 memcpy (&pm[1], &welcome, sizeof (welcome));
841 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
842 GNUNET_STATISTICS_update (plugin->env->stats,
843 gettext_noop ("# bytes currently in TCP buffers"),
844 pm->message_size, GNUNET_NO);
845 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
846 session->pending_messages_tail, pm);
847 if (GNUNET_YES != is_nat)
849 GNUNET_STATISTICS_update (plugin->env->stats,
850 gettext_noop ("# TCP sessions active"), 1,
853 start_session_timeout (session);
860 * If we have pending messages, ask the server to
861 * transmit them (schedule the respective tasks, etc.)
863 * @param session for which session should we do this
866 process_pending_messages (struct Session *session);
870 * Function called to notify a client about the socket
871 * being ready to queue more data. "buf" will be
872 * NULL and "size" zero if the socket was closed for
873 * writing in the meantime.
876 * @param size number of bytes available in buf
877 * @param buf where the callee should write the message
878 * @return number of bytes written to buf
881 do_transmit (void *cls, size_t size, void *buf)
883 struct Session *session = cls;
884 struct GNUNET_PeerIdentity pid;
885 struct Plugin *plugin;
886 struct PendingMessage *pos;
887 struct PendingMessage *hd;
888 struct PendingMessage *tl;
889 struct GNUNET_TIME_Absolute now;
893 GNUNET_assert (NULL != session);
894 session->transmit_handle = NULL;
895 plugin = session->plugin;
898 LOG (GNUNET_ERROR_TYPE_DEBUG,
899 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
900 GNUNET_i2s (&session->target));
901 /* timeout; cancel all messages that have already expired */
905 now = GNUNET_TIME_absolute_get ();
906 while ((NULL != (pos = session->pending_messages_head)) &&
907 (pos->timeout.abs_value_us <= now.abs_value_us))
909 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
910 session->pending_messages_tail, pos);
911 LOG (GNUNET_ERROR_TYPE_DEBUG,
912 "Failed to transmit %u byte message to `%4s'.\n",
913 pos->message_size, GNUNET_i2s (&session->target));
914 ret += pos->message_size;
915 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
917 /* do this call before callbacks (so that if callbacks destroy
918 * session, they have a chance to cancel actions done by this
920 process_pending_messages (session);
921 pid = session->target;
922 /* no do callbacks and do not use session again since
923 * the callbacks may abort the session */
924 while (NULL != (pos = hd))
926 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
927 if (pos->transmit_cont != NULL)
928 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 gettext_noop ("# bytes currently in TCP buffers"),
933 -(int64_t) ret, GNUNET_NO);
934 GNUNET_STATISTICS_update (plugin->env->stats,
936 ("# bytes discarded by TCP (timeout)"), ret,
940 /* copy all pending messages that would fit */
945 while (NULL != (pos = session->pending_messages_head))
947 if (ret + pos->message_size > size)
949 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
950 session->pending_messages_tail, pos);
951 GNUNET_assert (size >= pos->message_size);
952 LOG (GNUNET_ERROR_TYPE_DEBUG,
953 "Transmitting message of type %u\n",
954 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
955 /* FIXME: this memcpy can be up to 7% of our total runtime */
956 memcpy (cbuf, pos->msg, pos->message_size);
957 cbuf += pos->message_size;
958 ret += pos->message_size;
959 size -= pos->message_size;
960 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
962 /* schedule 'continuation' before callbacks so that callbacks that
963 * cancel everything don't cause us to use a session that no longer
965 process_pending_messages (session);
966 session->last_activity = GNUNET_TIME_absolute_get ();
967 pid = session->target;
968 /* we'll now call callbacks that may cancel the session; hence
969 * we should not use 'session' after this point */
970 while (NULL != (pos = hd))
972 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
973 if (pos->transmit_cont != NULL)
974 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
977 GNUNET_assert (hd == NULL);
978 GNUNET_assert (tl == NULL);
979 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
981 GNUNET_STATISTICS_update (plugin->env->stats,
982 gettext_noop ("# bytes currently in TCP buffers"),
983 -(int64_t) ret, GNUNET_NO);
984 GNUNET_STATISTICS_update (plugin->env->stats,
985 gettext_noop ("# bytes transmitted via TCP"), ret,
992 * If we have pending messages, ask the server to
993 * transmit them (schedule the respective tasks, etc.)
995 * @param session for which session should we do this
998 process_pending_messages (struct Session *session)
1000 struct PendingMessage *pm;
1002 GNUNET_assert (session->client != NULL);
1003 if (session->transmit_handle != NULL)
1005 if (NULL == (pm = session->pending_messages_head))
1008 session->transmit_handle =
1009 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
1010 GNUNET_TIME_absolute_get_remaining
1011 (pm->timeout), &do_transmit,
1017 * Functions with this signature are called whenever we need
1018 * to close a session due to a disconnect or failure to
1019 * establish a connection.
1021 * @param cls the `struct Plugin`
1022 * @param session session to close down
1023 * @return #GNUNET_OK on success
1026 tcp_disconnect_session (void *cls,
1027 struct Session *session)
1029 struct Plugin *plugin = cls;
1030 struct PendingMessage *pm;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "Disconnecting session of peer `%s' address `%s'\n",
1034 GNUNET_i2s (&session->target),
1035 tcp_address_to_string (NULL, session->addr, session->addrlen));
1037 stop_session_timeout (session);
1040 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
1044 GNUNET_STATISTICS_update (session->plugin->env->stats,
1045 gettext_noop ("# TCP sessions active"), -1,
1047 dec_sessions (plugin, session, __LINE__);
1051 GNUNET_assert (GNUNET_YES ==
1052 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
1056 /* clean up state */
1057 if (NULL != session->transmit_handle)
1059 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1060 session->transmit_handle = NULL;
1062 session->plugin->env->session_end (session->plugin->env->cls,
1063 &session->target, session);
1065 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
1067 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1068 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1071 while (NULL != (pm = session->pending_messages_head))
1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1074 pm->transmit_cont !=
1075 NULL ? "Could not deliver message to `%4s'.\n" :
1076 "Could not deliver message to `%4s', notifying.\n",
1077 GNUNET_i2s (&session->target));
1078 GNUNET_STATISTICS_update (session->plugin->env->stats,
1079 gettext_noop ("# bytes currently in TCP buffers"),
1080 -(int64_t) pm->message_size, GNUNET_NO);
1081 GNUNET_STATISTICS_update (session->plugin->env->stats,
1083 ("# bytes discarded by TCP (disconnect)"),
1084 pm->message_size, GNUNET_NO);
1085 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1086 session->pending_messages_tail, pm);
1087 if (NULL != pm->transmit_cont)
1088 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1089 GNUNET_SYSERR, pm->message_size, 0);
1092 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1094 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1095 if (NULL != session->client)
1096 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1098 if (NULL != session->client)
1100 GNUNET_SERVER_client_disconnect (session->client);
1101 GNUNET_SERVER_client_drop (session->client);
1102 session->client = NULL;
1104 GNUNET_free_non_null (session->addr);
1105 GNUNET_assert (NULL == session->transmit_handle);
1106 GNUNET_free (session);
1111 struct FindSessionContext
1119 session_it (void *cls,
1120 const struct GNUNET_PeerIdentity * key,
1123 struct FindSessionContext *res = cls;
1125 if (res->s == value)
1127 res->res = GNUNET_OK;
1135 find_session (struct Plugin *plugin, struct Session *session)
1137 struct FindSessionContext session_map_res;
1138 struct FindSessionContext nat_map_res;
1140 session_map_res.s = session;
1141 session_map_res.res = GNUNET_SYSERR;
1142 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1144 nat_map_res.s = session;
1145 nat_map_res.res = GNUNET_SYSERR;
1146 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1148 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1151 return GNUNET_SYSERR;
1158 * Function that can be used by the transport service to transmit
1159 * a message using the plugin. Note that in the case of a
1160 * peer disconnecting, the continuation MUST be called
1161 * prior to the disconnect notification itself. This function
1162 * will be called with this peer's HELLO message to initiate
1163 * a fresh connection to another peer.
1165 * @param cls closure
1166 * @param session which session must be used
1167 * @param msgbuf the message to transmit
1168 * @param msgbuf_size number of bytes in 'msgbuf'
1169 * @param priority how important is the message (most plugins will
1170 * ignore message priority and just FIFO)
1171 * @param to how long to wait at most for the transmission (does not
1172 * require plugins to discard the message after the timeout,
1173 * just advisory for the desired delay; most plugins will ignore
1175 * @param cont continuation to call once the message has
1176 * been transmitted (or if the transport is ready
1177 * for the next transmission call; or if the
1178 * peer disconnected...); can be NULL
1179 * @param cont_cls closure for @a cont
1180 * @return number of bytes used (on the physical network, with overheads);
1181 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1182 * and does NOT mean that the message was not transmitted (DV)
1185 tcp_plugin_send (void *cls,
1186 struct Session *session,
1187 const char *msgbuf, size_t msgbuf_size,
1188 unsigned int priority,
1189 struct GNUNET_TIME_Relative to,
1190 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1192 struct Plugin * plugin = cls;
1193 struct PendingMessage *pm;
1195 GNUNET_assert (NULL != plugin);
1196 GNUNET_assert (NULL != session);
1198 if (GNUNET_SYSERR == find_session(plugin, session))
1200 LOG (GNUNET_ERROR_TYPE_ERROR,
1201 _("Trying to send with invalid session %p\n"));
1202 return GNUNET_SYSERR;
1205 /* create new message entry */
1206 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1207 pm->msg = (const char *) &pm[1];
1208 memcpy (&pm[1], msgbuf, msgbuf_size);
1209 pm->message_size = msgbuf_size;
1210 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1211 pm->transmit_cont = cont;
1212 pm->transmit_cont_cls = cont_cls;
1214 LOG (GNUNET_ERROR_TYPE_DEBUG,
1215 "Asked to transmit %u bytes to `%s', added message to list.\n",
1216 msgbuf_size, GNUNET_i2s (&session->target));
1218 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1222 GNUNET_assert (session->client != NULL);
1223 reschedule_session_timeout (session);
1224 GNUNET_SERVER_client_set_timeout (session->client,
1225 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1226 GNUNET_STATISTICS_update (plugin->env->stats,
1227 gettext_noop ("# bytes currently in TCP buffers"),
1228 msgbuf_size, GNUNET_NO);
1230 /* append pm to pending_messages list */
1231 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1232 session->pending_messages_tail, pm);
1234 process_pending_messages (session);
1237 else if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value(plugin->nat_wait_conns, &session->target, session))
1239 LOG (GNUNET_ERROR_TYPE_DEBUG,
1240 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1241 GNUNET_i2s (&session->target));
1242 reschedule_session_timeout (session);
1243 GNUNET_STATISTICS_update (plugin->env->stats,
1244 gettext_noop ("# bytes currently in TCP buffers"),
1245 msgbuf_size, GNUNET_NO);
1247 /* append pm to pending_messages list */
1248 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1249 session->pending_messages_tail, pm);
1254 LOG (GNUNET_ERROR_TYPE_ERROR,
1255 "Invalid session %p\n", session);
1257 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1260 return GNUNET_SYSERR; /* session does not exist here */
1269 struct Session *result;
1274 session_lookup_it (void *cls,
1275 const struct GNUNET_PeerIdentity *key,
1278 struct SessionItCtx * si_ctx = cls;
1279 struct Session * session = value;
1281 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1282 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1283 LOG (GNUNET_ERROR_TYPE_DEBUG,
1284 "Comparing: %s %u <-> %s %u\n",
1292 if (session->addrlen != si_ctx->addrlen)
1296 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1301 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1302 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1303 LOG (GNUNET_ERROR_TYPE_DEBUG,
1304 "Comparing: %s %u <-> %s %u , OK!\n",
1312 /* Found existing session */
1313 si_ctx->result = session;
1319 * Task cleaning up a NAT connection attempt after timeout
1322 nat_connect_timeout (void *cls,
1323 const struct GNUNET_SCHEDULER_TaskContext *tc)
1325 struct Session *session = cls;
1327 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1328 LOG (GNUNET_ERROR_TYPE_DEBUG,
1329 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1330 GNUNET_i2s (&session->target),
1331 tcp_address_to_string (NULL,
1332 session->addr, session->addrlen));
1333 tcp_disconnect_session (session->plugin,
1339 * Create a new session to transmit data to the target
1340 * This session will used to send data to this peer and the plugin will
1341 * notify us by calling the env->session_end function
1343 * @param cls closure
1344 * @param address pointer to the GNUNET_HELLO_Address
1345 * @return the session if the address is valid, NULL otherwise
1347 static struct Session *
1348 tcp_plugin_get_session (void *cls,
1349 const struct GNUNET_HELLO_Address *address)
1351 struct Plugin *plugin = cls;
1352 struct Session *session = NULL;
1356 struct GNUNET_CONNECTION_Handle *sa;
1357 struct sockaddr_in a4;
1358 struct sockaddr_in6 a6;
1359 const struct IPv4TcpAddress *t4;
1360 const struct IPv6TcpAddress *t6;
1361 struct GNUNET_ATS_Information ats;
1362 unsigned int is_natd = GNUNET_NO;
1365 GNUNET_assert (plugin != NULL);
1366 GNUNET_assert (address != NULL);
1367 addrlen = address->address_length;
1368 LOG (GNUNET_ERROR_TYPE_DEBUG,
1369 "Trying to get session for `%s' address of peer `%s'\n",
1370 tcp_address_to_string(NULL, address->address, address->address_length),
1371 GNUNET_i2s (&address->peer));
1373 /* look for existing session */
1375 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1378 struct SessionItCtx si_ctx;
1380 si_ctx.addr = (void *) address->address;
1381 si_ctx.addrlen = address->address_length;
1383 si_ctx.result = NULL;
1385 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1387 &session_lookup_it, &si_ctx);
1388 if (si_ctx.result != NULL)
1390 session = si_ctx.result;
1391 LOG (GNUNET_ERROR_TYPE_DEBUG,
1392 "Found existing session for `%s' address `%s' session %p\n",
1393 GNUNET_i2s (&address->peer),
1394 tcp_address_to_string(NULL, address->address, address->address_length),
1398 LOG (GNUNET_ERROR_TYPE_DEBUG,
1399 "Existing sessions did not match address `%s' or peer `%s'\n",
1400 tcp_address_to_string(NULL, address->address, address->address_length),
1401 GNUNET_i2s (&address->peer));
1404 if (addrlen == sizeof (struct IPv6TcpAddress))
1406 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1407 t6 = address->address;
1409 memset (&a6, 0, sizeof (a6));
1410 #if HAVE_SOCKADDR_IN_SIN_LEN
1411 a6.sin6_len = sizeof (a6);
1413 a6.sin6_family = AF_INET6;
1414 a6.sin6_port = t6->t6_port;
1415 if (t6->t6_port == 0)
1416 is_natd = GNUNET_YES;
1417 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1421 else if (addrlen == sizeof (struct IPv4TcpAddress))
1423 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1424 t4 = address->address;
1426 memset (&a4, 0, sizeof (a4));
1427 #if HAVE_SOCKADDR_IN_SIN_LEN
1428 a4.sin_len = sizeof (a4);
1430 a4.sin_family = AF_INET;
1431 a4.sin_port = t4->t4_port;
1432 if (t4->t4_port == 0)
1433 is_natd = GNUNET_YES;
1434 a4.sin_addr.s_addr = t4->ipv4_addr;
1440 GNUNET_STATISTICS_update (plugin->env->stats,
1442 ("# requests to create session with invalid address"),
1447 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1449 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1451 /* NAT client only works with IPv4 addresses */
1455 if (plugin->cur_connections >= plugin->max_connections)
1461 if ((is_natd == GNUNET_YES) &&
1463 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1466 /* Only do one NAT punch attempt per peer identity */
1470 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1472 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1475 LOG (GNUNET_ERROR_TYPE_DEBUG,
1476 "Found valid IPv4 NAT address (creating session)!\n") ;
1477 session = create_session (plugin,
1481 session->addrlen = 0;
1482 session->addr = NULL;
1483 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value)
1485 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1486 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1487 &nat_connect_timeout,
1489 GNUNET_assert (session != NULL);
1490 GNUNET_assert (GNUNET_OK ==
1491 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1496 LOG (GNUNET_ERROR_TYPE_DEBUG,
1497 "Created NAT WAIT connection to `%4s' at `%s'\n",
1498 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1500 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Running NAT client for `%4s' at `%s' failed\n",
1506 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1507 tcp_disconnect_session (plugin, session);
1512 /* create new outbound session */
1513 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1514 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1517 LOG (GNUNET_ERROR_TYPE_DEBUG,
1518 "Failed to create connection to `%4s' at `%s'\n",
1519 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1522 plugin->cur_connections++;
1523 if (plugin->cur_connections == plugin->max_connections)
1524 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1526 LOG (GNUNET_ERROR_TYPE_DEBUG,
1527 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1528 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1530 session = create_session (plugin,
1532 GNUNET_SERVER_connect_socket (plugin->server, sa),
1534 session->addr = GNUNET_malloc (addrlen);
1535 memcpy (session->addr, address->address, addrlen);
1536 session->addrlen = addrlen;
1537 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
1538 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1539 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1541 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1542 inc_sessions (plugin, session, __LINE__);
1543 LOG (GNUNET_ERROR_TYPE_DEBUG,
1544 "Creating new session for `%s' address `%s' session %p\n",
1545 GNUNET_i2s (&address->peer),
1546 tcp_address_to_string(NULL, address->address, address->address_length),
1548 /* Send TCP Welcome */
1549 process_pending_messages (session);
1556 session_disconnect_it (void *cls,
1557 const struct GNUNET_PeerIdentity *key,
1560 struct Plugin *plugin = cls;
1561 struct Session *session = value;
1563 GNUNET_STATISTICS_update (session->plugin->env->stats,
1565 ("# transport-service disconnect requests for TCP"),
1567 tcp_disconnect_session (plugin,
1574 * Function that can be called to force a disconnect from the
1575 * specified neighbour. This should also cancel all previously
1576 * scheduled transmissions. Obviously the transmission may have been
1577 * partially completed already, which is OK. The plugin is supposed
1578 * to close the connection (if applicable) and no longer call the
1579 * transmit continuation(s).
1581 * Finally, plugin MUST NOT call the services's receive function to
1582 * notify the service that the connection to the specified target was
1583 * closed after a getting this call.
1585 * @param cls closure
1586 * @param target peer for which the last transmission is
1590 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1592 struct Plugin *plugin = cls;
1594 LOG (GNUNET_ERROR_TYPE_DEBUG,
1595 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1596 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1597 &session_disconnect_it, plugin);
1598 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1599 &session_disconnect_it, plugin);
1604 * Running pretty printers: head
1606 static struct PrettyPrinterContext *ppc_dll_head;
1609 * Running pretty printers: tail
1611 static struct PrettyPrinterContext *ppc_dll_tail;
1614 * Context for address to string conversion.
1616 struct PrettyPrinterContext
1621 struct PrettyPrinterContext *next;
1626 struct PrettyPrinterContext *prev;
1631 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1636 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1639 * Function to call with the result.
1641 GNUNET_TRANSPORT_AddressStringCallback asc;
1644 * Clsoure for 'asc'.
1649 * Port to add after the IP address.
1666 ppc_cancel_task (void *cls,
1667 const struct GNUNET_SCHEDULER_TaskContext *tc)
1670 struct PrettyPrinterContext *ppc = cls;
1671 struct PrettyPrinterContext *cur;
1673 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1679 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cancel request %p, %u pending\n", ppc, count);
1680 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1681 if (NULL != ppc->resolver_handle)
1683 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1684 ppc->resolver_handle = NULL;
1686 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1692 * Append our port and forward the result.
1694 * @param cls the 'struct PrettyPrinterContext*'
1695 * @param hostname hostname part of the address
1698 append_port (void *cls, const char *hostname)
1700 struct PrettyPrinterContext *ppc = cls;
1701 struct PrettyPrinterContext *cur;
1705 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1710 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Callback request %p == %s, %u pending\n", ppc, hostname, count);
1711 if (hostname == NULL)
1713 ppc->asc (ppc->asc_cls, NULL);
1714 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1715 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1716 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1717 ppc->resolver_handle = NULL;
1718 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done request %p, %u pending\n", ppc, count);
1722 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
1733 if (GNUNET_YES == ppc->ipv6)
1734 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1736 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1737 ppc->asc (ppc->asc_cls, ret);
1743 * Convert the transports address to a nice, human-readable
1746 * @param cls closure
1747 * @param type name of the transport that generated the address
1748 * @param addr one of the addresses of the host, NULL for the last address
1749 * the specific address format depends on the transport
1750 * @param addrlen length of the address
1751 * @param numeric should (IP) addresses be displayed in numeric form?
1752 * @param timeout after how long should we give up?
1753 * @param asc function to call on each string
1754 * @param asc_cls closure for asc
1757 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1758 const void *addr, size_t addrlen,
1760 struct GNUNET_TIME_Relative timeout,
1761 GNUNET_TRANSPORT_AddressStringCallback asc,
1764 struct PrettyPrinterContext *ppc;
1767 struct sockaddr_in a4;
1768 struct sockaddr_in6 a6;
1769 const struct IPv4TcpAddress *t4;
1770 const struct IPv6TcpAddress *t6;
1774 if (addrlen == sizeof (struct IPv6TcpAddress))
1777 memset (&a6, 0, sizeof (a6));
1778 a6.sin6_family = AF_INET6;
1779 a6.sin6_port = t6->t6_port;
1780 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1781 port = ntohs (t6->t6_port);
1782 options = ntohl (t6->options);
1786 else if (addrlen == sizeof (struct IPv4TcpAddress))
1789 memset (&a4, 0, sizeof (a4));
1790 a4.sin_family = AF_INET;
1791 a4.sin_port = t4->t4_port;
1792 a4.sin_addr.s_addr = t4->ipv4_addr;
1793 port = ntohs (t4->t4_port);
1794 options = ntohl (t4->options);
1798 else if (0 == addrlen)
1800 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1801 asc (asc_cls, NULL);
1806 /* invalid address */
1807 GNUNET_break_op (0);
1808 asc (asc_cls, NULL);
1811 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1812 if (addrlen == sizeof (struct IPv6TcpAddress))
1813 ppc->ipv6 = GNUNET_YES;
1815 ppc->ipv6 = GNUNET_NO;
1817 ppc->asc_cls = asc_cls;
1819 ppc->options = options;
1820 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
1821 &ppc_cancel_task, ppc);
1822 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1825 if (NULL != ppc->resolver_handle)
1827 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1828 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
1839 * Check if the given port is plausible (must be either our listen
1840 * port or our advertised port), or any port if we are behind NAT
1841 * and do not have a port open. If it is neither, we return
1844 * @param plugin global variables
1845 * @param in_port port number to check
1846 * @return GNUNET_OK if port is either open_port or adv_port
1849 check_port (struct Plugin *plugin, uint16_t in_port)
1851 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1853 return GNUNET_SYSERR;
1858 * Function that will be called to check if a binary address for this
1859 * plugin is well-formed and corresponds to an address for THIS peer
1860 * (as per our configuration). Naturally, if absolutely necessary,
1861 * plugins can be a bit conservative in their answer, but in general
1862 * plugins should make sure that the address does not redirect
1863 * traffic to a 3rd party that might try to man-in-the-middle our
1866 * @param cls closure, our 'struct Plugin*'
1867 * @param addr pointer to the address
1868 * @param addrlen length of addr
1869 * @return GNUNET_OK if this is a plausible address for this peer
1870 * and transport, GNUNET_SYSERR if not
1873 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1875 struct Plugin *plugin = cls;
1876 struct IPv4TcpAddress *v4;
1877 struct IPv6TcpAddress *v6;
1879 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1880 (addrlen != sizeof (struct IPv6TcpAddress)))
1884 return GNUNET_SYSERR;
1887 if (addrlen == sizeof (struct IPv4TcpAddress))
1889 v4 = (struct IPv4TcpAddress *) addr;
1890 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions)))
1893 return GNUNET_SYSERR;
1895 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1896 return GNUNET_SYSERR;
1898 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1899 sizeof (struct in_addr)))
1900 return GNUNET_SYSERR;
1904 v6 = (struct IPv6TcpAddress *) addr;
1905 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1907 GNUNET_break_op (0);
1908 return GNUNET_SYSERR;
1910 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions)))
1913 return GNUNET_SYSERR;
1915 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1916 return GNUNET_SYSERR;
1918 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1919 sizeof (struct in6_addr)))
1920 return GNUNET_SYSERR;
1927 * We've received a nat probe from this peer via TCP. Finish
1928 * creating the client session and resume sending of queued
1931 * @param cls closure
1932 * @param client identification of the client
1933 * @param message the actual message
1936 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1937 const struct GNUNET_MessageHeader *message)
1939 struct Plugin *plugin = cls;
1940 struct Session *session;
1941 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1944 struct IPv4TcpAddress *t4;
1945 struct IPv6TcpAddress *t6;
1946 const struct sockaddr_in *s4;
1947 const struct sockaddr_in6 *s6;
1949 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1951 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1952 * a connection to this peer by running gnunet-nat-client. This peer
1953 * received the punch message and now wants us to use the new connection
1954 * as the default for that peer. Do so and then send a WELCOME message
1955 * so we can really be connected!
1957 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1959 GNUNET_break_op (0);
1960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1964 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1966 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1967 sizeof (struct GNUNET_PeerIdentity)))
1969 /* refuse connections from ourselves */
1970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1975 GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
1978 if (session == NULL)
1980 LOG (GNUNET_ERROR_TYPE_DEBUG,
1981 "Did NOT find session for NAT probe!\n");
1982 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1985 LOG (GNUNET_ERROR_TYPE_DEBUG,
1986 "Found session for NAT probe!\n");
1988 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1990 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1991 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1994 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1998 tcp_disconnect_session (plugin, session);
2001 GNUNET_assert (GNUNET_CONTAINER_multipeermap_remove
2002 (plugin->nat_wait_conns,
2003 &tcp_nat_probe->clientIdentity,
2004 session) == GNUNET_YES);
2005 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2006 &session->target, session,
2007 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2008 session->last_activity = GNUNET_TIME_absolute_get ();
2009 session->inbound = GNUNET_NO;
2010 LOG (GNUNET_ERROR_TYPE_DEBUG,
2011 "Found address `%s' for incoming connection\n",
2012 GNUNET_a2s (vaddr, alen));
2013 switch (((const struct sockaddr *) vaddr)->sa_family)
2017 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2019 t4->t4_port = s4->sin_port;
2020 t4->ipv4_addr = s4->sin_addr.s_addr;
2022 session->addrlen = sizeof (struct IPv4TcpAddress);
2026 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2028 t6->t6_port = s6->sin6_port;
2029 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2031 session->addrlen = sizeof (struct IPv6TcpAddress);
2034 GNUNET_break_op (0);
2035 LOG (GNUNET_ERROR_TYPE_DEBUG,
2036 "Bad address for incoming connection!\n");
2037 GNUNET_free (vaddr);
2038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2039 tcp_disconnect_session (plugin, session);
2042 GNUNET_free (vaddr);
2043 GNUNET_break (NULL == session->client);
2044 GNUNET_SERVER_client_keep (client);
2045 session->client = client;
2046 inc_sessions (plugin, session, __LINE__);
2047 GNUNET_STATISTICS_update (plugin->env->stats,
2048 gettext_noop ("# TCP sessions active"), 1,
2050 process_pending_messages (session);
2051 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2056 * We've received a welcome from this peer via TCP. Possibly create a
2057 * fresh client record and send back our welcome.
2059 * @param cls closure
2060 * @param client identification of the client
2061 * @param message the actual message
2064 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2065 const struct GNUNET_MessageHeader *message)
2067 struct Plugin *plugin = cls;
2068 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2069 struct Session *session;
2072 struct IPv4TcpAddress *t4;
2073 struct IPv6TcpAddress *t6;
2074 const struct sockaddr_in *s4;
2075 const struct sockaddr_in6 *s6;
2076 struct GNUNET_ATS_Information ats;
2079 memcmp (&wm->clientIdentity, plugin->env->my_identity,
2080 sizeof (struct GNUNET_PeerIdentity)))
2082 /* refuse connections from ourselves */
2083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2084 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2086 LOG (GNUNET_ERROR_TYPE_WARNING,
2087 "Received %s message from my own identity `%4s' on address `%s'\n",
2088 "WELCOME", GNUNET_i2s (&wm->clientIdentity), GNUNET_a2s (vaddr, alen));
2089 GNUNET_free (vaddr);
2091 GNUNET_break_op (0);
2094 LOG (GNUNET_ERROR_TYPE_DEBUG,
2095 "Received %s message from `%4s' %p\n", "WELCOME",
2096 GNUNET_i2s (&wm->clientIdentity), client);
2097 GNUNET_STATISTICS_update (plugin->env->stats,
2098 gettext_noop ("# TCP WELCOME messages received"), 1,
2100 session = lookup_session_by_client (plugin, client);
2101 if (session != NULL)
2103 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2105 LOG (GNUNET_ERROR_TYPE_DEBUG,
2106 "Found existing session %p for peer `%s'\n",
2108 GNUNET_a2s (vaddr, alen));
2109 GNUNET_free (vaddr);
2114 GNUNET_SERVER_client_keep (client);
2115 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
2116 plugin->cur_connections++;
2117 if (plugin->cur_connections == plugin->max_connections)
2118 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2120 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
2121 session->inbound = GNUNET_YES;
2122 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2124 if (alen == sizeof (struct sockaddr_in))
2127 t4 = GNUNET_new (struct IPv4TcpAddress);
2128 t4->options = htonl (0);
2129 t4->t4_port = s4->sin_port;
2130 t4->ipv4_addr = s4->sin_addr.s_addr;
2132 session->addrlen = sizeof (struct IPv4TcpAddress);
2134 else if (alen == sizeof (struct sockaddr_in6))
2137 t6 = GNUNET_new (struct IPv6TcpAddress);
2138 t6->options = htonl (0);
2139 t6->t6_port = s6->sin6_port;
2140 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2142 session->addrlen = sizeof (struct IPv6TcpAddress);
2145 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
2146 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (ats.value);
2147 LOG (GNUNET_ERROR_TYPE_DEBUG,
2148 "Creating new session %p for peer `%s'\n",
2150 GNUNET_a2s (vaddr, alen));
2151 GNUNET_free (vaddr);
2152 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2155 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2156 inc_sessions (plugin, session, __LINE__);
2160 LOG (GNUNET_ERROR_TYPE_DEBUG,
2161 "Did not obtain TCP socket address for incoming connection\n");
2166 if (session->expecting_welcome != GNUNET_YES)
2168 GNUNET_break_op (0);
2169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2173 session->last_activity = GNUNET_TIME_absolute_get ();
2174 session->expecting_welcome = GNUNET_NO;
2176 /* Notify transport and ATS about new session */
2177 if (GNUNET_YES == session->inbound)
2179 plugin->env->session_start (NULL, &wm->clientIdentity, PLUGIN_NAME,
2180 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2181 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2185 process_pending_messages (session);
2187 GNUNET_SERVER_client_set_timeout (client,
2188 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2194 * Task to signal the server that we can continue
2195 * receiving from the TCP client now.
2197 * @param cls the 'struct Session*'
2198 * @param tc task context (unused)
2201 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2203 struct Session *session = cls;
2205 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2206 reschedule_session_timeout (session);
2208 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2213 * We've received data for this peer via TCP. Unbox,
2214 * compute latency and forward.
2216 * @param cls closure
2217 * @param client identification of the client
2218 * @param message the actual message
2221 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2222 const struct GNUNET_MessageHeader *message)
2224 struct Plugin *plugin = cls;
2225 struct Session *session;
2226 struct GNUNET_TIME_Relative delay;
2229 type = ntohs (message->type);
2230 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2231 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2233 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2234 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2237 session = lookup_session_by_client (plugin, client);
2238 if (NULL == session)
2240 /* No inbound session found */
2244 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2245 LOG (GNUNET_ERROR_TYPE_ERROR,
2246 "Received unexpected %u bytes of type %u from `%s'\n",
2247 (unsigned int) ntohs (message->size),
2248 (unsigned int) ntohs (message->type),
2249 GNUNET_a2s(vaddr, alen));
2250 GNUNET_break_op (0);
2251 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2252 GNUNET_free_non_null(vaddr);
2255 else if (GNUNET_YES == session->expecting_welcome)
2257 /* Session is expecting WELCOME message */
2261 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2262 LOG (GNUNET_ERROR_TYPE_ERROR,
2263 "Received unexpected %u bytes of type %u from `%s'\n",
2264 (unsigned int) ntohs (message->size),
2265 (unsigned int) ntohs (message->type),
2266 GNUNET_a2s(vaddr, alen));
2267 GNUNET_break_op (0);
2268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2269 GNUNET_free_non_null(vaddr);
2273 session->last_activity = GNUNET_TIME_absolute_get ();
2274 LOG (GNUNET_ERROR_TYPE_DEBUG,
2275 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2276 (unsigned int) ntohs (message->size),
2277 (unsigned int) ntohs (message->type),
2278 GNUNET_i2s (&session->target));
2280 GNUNET_STATISTICS_update (plugin->env->stats,
2281 gettext_noop ("# bytes received via TCP"),
2282 ntohs (message->size), GNUNET_NO);
2283 struct GNUNET_ATS_Information distance;
2285 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2286 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2287 GNUNET_break (session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2289 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2293 delay = plugin->env->receive (plugin->env->cls,
2297 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2298 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2299 plugin->env->update_address_metrics (plugin->env->cls,
2301 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2302 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2303 session, &distance, 1);
2305 reschedule_session_timeout (session);
2307 if (0 == delay.rel_value_us)
2309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2313 LOG (GNUNET_ERROR_TYPE_DEBUG,
2314 "Throttling receiving from `%s' for %s\n",
2315 GNUNET_i2s (&session->target),
2316 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2317 GNUNET_SERVER_disable_receive_done_warning (client);
2318 session->receive_delay_task =
2319 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2325 * Functions with this signature are called whenever a peer
2326 * is disconnected on the network level.
2328 * @param cls closure
2329 * @param client identification of the client
2332 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2334 struct Plugin *plugin = cls;
2335 struct Session *session;
2339 session = lookup_session_by_client (plugin, client);
2340 if (session == NULL)
2341 return; /* unknown, nothing to do */
2342 LOG (GNUNET_ERROR_TYPE_DEBUG,
2343 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2344 GNUNET_i2s (&session->target),
2346 NULL) ? tcp_address_to_string (session->plugin,
2351 if (plugin->cur_connections == plugin->max_connections)
2352 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2354 if (plugin->cur_connections < 1)
2357 plugin->cur_connections--;
2359 GNUNET_STATISTICS_update (session->plugin->env->stats,
2361 ("# network-level TCP disconnect events"), 1,
2363 tcp_disconnect_session (plugin, session);
2368 * We can now send a probe message, copy into buffer to really send.
2370 * @param cls closure, a struct TCPProbeContext
2371 * @param size max size to copy
2372 * @param buf buffer to copy message to
2373 * @return number of bytes copied into buf
2376 notify_send_probe (void *cls, size_t size, void *buf)
2378 struct TCPProbeContext *tcp_probe_ctx = cls;
2379 struct Plugin *plugin = tcp_probe_ctx->plugin;
2382 tcp_probe_ctx->transmit_handle = NULL;
2383 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2387 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2388 GNUNET_free (tcp_probe_ctx);
2391 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2392 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2393 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2394 tcp_probe_ctx->sock);
2395 ret = sizeof (tcp_probe_ctx->message);
2396 GNUNET_free (tcp_probe_ctx);
2402 * Function called by the NAT subsystem suggesting another peer wants
2403 * to connect to us via connection reversal. Try to connect back to the
2406 * @param cls closure
2407 * @param addr address to try
2408 * @param addrlen number of bytes in @a addr
2411 try_connection_reversal (void *cls, const struct sockaddr *addr,
2414 struct Plugin *plugin = cls;
2415 struct GNUNET_CONNECTION_Handle *sock;
2416 struct TCPProbeContext *tcp_probe_ctx;
2419 * We have received an ICMP response, ostensibly from a peer
2420 * that wants to connect to us! Send a message to establish a connection.
2422 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2425 /* failed for some odd reason (out of sockets?); ignore attempt */
2429 /* FIXME: do we need to track these probe context objects so that
2430 * we can clean them up on plugin unload? */
2431 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2432 tcp_probe_ctx->message.header.size =
2433 htons (sizeof (struct TCP_NAT_ProbeMessage));
2434 tcp_probe_ctx->message.header.type =
2435 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2436 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2437 sizeof (struct GNUNET_PeerIdentity));
2438 tcp_probe_ctx->plugin = plugin;
2439 tcp_probe_ctx->sock = sock;
2440 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2442 tcp_probe_ctx->transmit_handle =
2443 GNUNET_CONNECTION_notify_transmit_ready (sock,
2444 ntohs (tcp_probe_ctx->
2445 message.header.size),
2446 GNUNET_TIME_UNIT_FOREVER_REL,
2454 * Session was idle, so disconnect it
2457 session_timeout (void *cls,
2458 const struct GNUNET_SCHEDULER_TaskContext *tc)
2460 struct Session *s = cls;
2462 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Session %p was idle for %s, disconnecting\n",
2466 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2468 /* call session destroy function */
2469 tcp_disconnect_session(s->plugin, s);
2474 * Start session timeout
2477 start_session_timeout (struct Session *s)
2479 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2480 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "Timeout for session %p set to %s\n",
2486 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2492 * Increment session timeout due to activity
2495 reschedule_session_timeout (struct Session *s)
2497 GNUNET_assert (NULL != s);
2498 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2500 GNUNET_SCHEDULER_cancel (s->timeout_task);
2501 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Timeout rescheduled for session %p set to %s\n",
2507 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2515 * @param s session to cancel timeout for
2518 stop_session_timeout (struct Session *s)
2520 GNUNET_assert (NULL != s);
2522 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2524 GNUNET_SCHEDULER_cancel (s->timeout_task);
2525 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2527 "Timeout stopped for session %p canceled\n",
2533 * Function obtain the network type for a session
2535 * @param cls closure ('struct Plugin*')
2536 * @param session the session
2537 * @return the network type in HBO or #GNUNET_SYSERR
2539 static enum GNUNET_ATS_Network_Type
2540 tcp_get_network (void *cls,
2541 struct Session *session)
2543 GNUNET_assert (NULL != session);
2544 return session->ats_address_network_type;
2549 * Entry point for the plugin.
2551 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2552 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2555 libgnunet_plugin_transport_tcp_init (void *cls)
2557 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2558 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2559 sizeof (struct WelcomeMessage)},
2560 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2561 sizeof (struct TCP_NAT_ProbeMessage)},
2562 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2565 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2566 struct GNUNET_TRANSPORT_PluginFunctions *api;
2567 struct Plugin *plugin;
2568 struct GNUNET_SERVICE_Context *service;
2569 unsigned long long aport;
2570 unsigned long long bport;
2571 unsigned long long max_connections;
2573 struct GNUNET_TIME_Relative idle_timeout;
2576 struct sockaddr **addrs;
2577 socklen_t *addrlens;
2580 if (NULL == env->receive)
2582 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2583 initialze the plugin or the API */
2584 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2586 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2587 api->address_to_string = &tcp_address_to_string;
2588 api->string_to_address = &tcp_string_to_address;
2592 GNUNET_assert (NULL != env->cfg);
2594 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2597 max_connections = 128;
2601 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2602 &bport)) || (bport > 65535) ||
2604 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2605 "ADVERTISED-PORT", &aport)) &&
2608 LOG (GNUNET_ERROR_TYPE_ERROR,
2609 _("Require valid port number for service `%s' in configuration!\n"),
2619 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2620 if (service == NULL)
2622 LOG (GNUNET_ERROR_TYPE_WARNING,
2623 _("Failed to start service.\n"));
2630 /* Initialize my flags */
2633 plugin = GNUNET_new (struct Plugin);
2634 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections, GNUNET_YES);
2635 plugin->max_connections = max_connections;
2636 plugin->cur_connections = 0;
2637 plugin->open_port = bport;
2638 plugin->adv_port = aport;
2640 plugin->lsock = NULL;
2641 if ((service != NULL) &&
2644 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2647 for (ret = ret_s-1; ret >= 0; ret--)
2648 LOG (GNUNET_ERROR_TYPE_INFO,
2649 "Binding to address `%s'\n",
2650 GNUNET_a2s (addrs[ret], addrlens[ret]));
2652 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2653 (const struct sockaddr **) addrs, addrlens,
2654 &tcp_nat_port_map_callback,
2655 &try_connection_reversal, plugin);
2656 for (ret = ret_s -1; ret >= 0; ret--)
2658 GNUNET_assert (addrs[ret] != NULL);
2659 GNUNET_free (addrs[ret]);
2661 GNUNET_free_non_null (addrs);
2662 GNUNET_free_non_null (addrlens);
2666 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2667 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2668 &try_connection_reversal, plugin);
2670 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2672 api->send = &tcp_plugin_send;
2673 api->get_session = &tcp_plugin_get_session;
2675 api->disconnect_session = &tcp_disconnect_session;
2676 api->disconnect_peer = &tcp_plugin_disconnect;
2677 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2678 api->check_address = &tcp_plugin_check_address;
2679 api->address_to_string = &tcp_address_to_string;
2680 api->string_to_address = &tcp_string_to_address;
2681 api->get_network = &tcp_get_network;
2682 plugin->service = service;
2683 if (NULL != service)
2685 plugin->server = GNUNET_SERVICE_get_server (service);
2690 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2691 "TIMEOUT", &idle_timeout))
2693 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2694 "transport-tcp", "TIMEOUT");
2695 if (plugin->nat != NULL)
2696 GNUNET_NAT_unregister (plugin->nat);
2697 GNUNET_free (plugin);
2702 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2703 NULL, idle_timeout, GNUNET_YES);
2705 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2706 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2708 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2710 plugin->handlers[i].callback_cls = plugin;
2712 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2713 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2714 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES);
2716 LOG (GNUNET_ERROR_TYPE_INFO,
2717 _("TCP transport listening on port %llu\n"), bport);
2719 LOG (GNUNET_ERROR_TYPE_INFO,
2720 _("TCP transport not listening on any port (client only)\n"));
2722 LOG (GNUNET_ERROR_TYPE_INFO,
2723 _("TCP transport advertises itself as being on port %llu\n"),
2725 /* Initially set connections to 0 */
2726 GNUNET_assert (NULL != plugin->env->stats);
2727 GNUNET_STATISTICS_set (plugin->env->stats,
2728 gettext_noop ("# TCP sessions active"), 0,
2735 * Exit point from the plugin.
2738 libgnunet_plugin_transport_tcp_done (void *cls)
2740 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2741 struct Plugin *plugin = api->cls;
2742 struct TCPProbeContext *tcp_probe;
2743 struct PrettyPrinterContext *cur;
2744 struct PrettyPrinterContext *next;
2751 LOG (GNUNET_ERROR_TYPE_DEBUG,
2752 "Shutting down TCP plugin\n");
2754 /* Removing leftover sessions */
2755 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2756 &session_disconnect_it, plugin);
2757 /* Removing leftover NAT sessions */
2758 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2759 &session_disconnect_it, plugin);
2761 next = ppc_dll_head;
2762 for (cur = next; NULL != cur; cur = next)
2765 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
2766 if (NULL != cur->resolver_handle)
2767 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2768 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2773 if (plugin->service != NULL)
2774 GNUNET_SERVICE_stop (plugin->service);
2776 GNUNET_SERVER_destroy (plugin->server);
2777 GNUNET_free (plugin->handlers);
2778 if (plugin->nat != NULL)
2779 GNUNET_NAT_unregister (plugin->nat);
2780 while (NULL != (tcp_probe = plugin->probe_head))
2782 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2784 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2785 GNUNET_free (tcp_probe);
2787 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2788 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2789 GNUNET_free (plugin);
2794 /* end of plugin_transport_tcp.c */