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
306 * Length of connect_addr.
311 * Last activity on this connection. Used to select preferred
314 struct GNUNET_TIME_Absolute last_activity;
317 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
319 int expecting_welcome;
322 * Was this a connection that was inbound (we accepted)? (#GNUNET_YES/#GNUNET_NO)
327 * Was this session created using NAT traversal?
332 * ATS network type in NBO
334 uint32_t ats_address_network_type;
339 * Encapsulation of all of the state of the plugin.
346 struct GNUNET_TRANSPORT_PluginEnvironment *env;
351 struct GNUNET_CONNECTION_Handle *lsock;
354 * Our handle to the NAT module.
356 struct GNUNET_NAT_Handle *nat;
359 * Map from peer identities to sessions for the given peer.
361 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
364 * Handle to the network service.
366 struct GNUNET_SERVICE_Context *service;
369 * Handle to the server for this service.
371 struct GNUNET_SERVER_Handle *server;
374 * Copy of the handler array where the closures are
375 * set to this struct's instance.
377 struct GNUNET_SERVER_MessageHandler *handlers;
380 * Map of peers we have tried to contact behind a NAT
382 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
385 * List of active TCP probes.
387 struct TCPProbeContext *probe_head;
390 * List of active TCP probes.
392 struct TCPProbeContext *probe_tail;
395 * Handle for (DYN)DNS lookup of our external IP.
397 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
400 * How many more TCP sessions are we allowed to open right now?
402 unsigned long long max_connections;
405 * How many more TCP sessions do we have right now?
407 unsigned long long cur_connections;
410 * ID of task used to update our addresses when one expires.
412 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
415 * Port that we are actually listening on.
420 * Port that the user said we would have visible to the
429 * Start session timeout
432 start_session_timeout (struct Session *s);
436 * Increment session timeout due to activity
439 reschedule_session_timeout (struct Session *s);
445 * @param s session to cancel timeout for
448 stop_session_timeout (struct Session *s);
453 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
456 static unsigned int sessions;
460 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
463 unsigned int size = GNUNET_CONTAINER_multipeermap_size (plugin->sessionmap);
464 if (sessions != size)
465 LOG (GNUNET_ERROR_TYPE_DEBUG,
466 "Inconsistent sessions %u <-> session map size: %u\n",
468 LOG (GNUNET_ERROR_TYPE_DEBUG,
469 "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
473 GNUNET_i2s (&session->target),
474 tcp_address_to_string (NULL, session->addr, session->addrlen));
479 dec_sessions (struct Plugin *plugin,
480 struct Session *session, int line)
482 GNUNET_assert (sessions > 0);
483 unsigned int size = GNUNET_CONTAINER_multipeermap_size(plugin->sessionmap);
485 if (sessions != size)
486 LOG (GNUNET_ERROR_TYPE_DEBUG,
487 "Inconsistent sessions %u <-> session map size: %u\n",
489 LOG (GNUNET_ERROR_TYPE_DEBUG,
490 "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
494 GNUNET_i2s (&session->target),
495 tcp_address_to_string (NULL, session->addr, session->addrlen));
501 * Function to check if an inbound connection is acceptable.
502 * Mostly used to limit the total number of open connections
505 * @param cls the 'struct Plugin'
506 * @param ucred credentials, if available, otherwise NULL
507 * @param addr address
508 * @param addrlen length of address
509 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
510 * for unknown address family (will be denied).
513 plugin_tcp_access_check (void *cls,
514 const struct GNUNET_CONNECTION_Credentials *ucred,
515 const struct sockaddr *addr, socklen_t addrlen)
517 struct Plugin *plugin = cls;
518 LOG (GNUNET_ERROR_TYPE_DEBUG,
519 "Accepting new incoming TCP connection from `%s'\n",
520 GNUNET_a2s (addr, addrlen));
521 if (plugin->cur_connections >= plugin->max_connections)
523 plugin->cur_connections ++;
529 * Our external IP address/port mapping has changed.
531 * @param cls closure, the 'struct LocalAddrList'
532 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
533 * the previous (now invalid) one
534 * @param addr either the previous or the new public IP address
535 * @param addrlen actual lenght of the address
538 tcp_nat_port_map_callback (void *cls, int add_remove,
539 const struct sockaddr *addr, socklen_t addrlen)
541 struct Plugin *plugin = cls;
542 struct IPv4TcpAddress t4;
543 struct IPv6TcpAddress t6;
547 LOG (GNUNET_ERROR_TYPE_INFO,
548 "NAT notification to %s address `%s'\n",
549 (GNUNET_YES == add_remove) ? "add" : "remove",
550 GNUNET_a2s (addr, addrlen));
551 /* convert 'addr' to our internal format */
552 switch (addr->sa_family)
555 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
556 memset (&t4,0, sizeof (t4));
557 t4.options = htonl (myoptions);
558 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
559 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
564 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
565 memset (&t6, 0, sizeof (t6));
566 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
567 sizeof (struct in6_addr));
568 t6.options = htonl (myoptions);
569 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
577 /* modify our published address list */
578 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
583 * Function called for a quick conversion of the binary address to
584 * a numeric address. Note that the caller must not free the
585 * address and that the next call to this function is allowed
586 * to override the address again.
588 * @param cls closure ('struct Plugin*')
589 * @param addr binary address
590 * @param addrlen length of the address
591 * @return string representing the same address
594 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
596 static char rbuf[INET6_ADDRSTRLEN + 12];
597 char buf[INET6_ADDRSTRLEN];
601 const struct IPv4TcpAddress *t4;
602 const struct IPv6TcpAddress *t6;
609 case sizeof (struct IPv6TcpAddress):
612 port = ntohs (t6->t6_port);
613 options = ntohl (t6->options);
614 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
617 case sizeof (struct IPv4TcpAddress):
620 port = ntohs (t4->t4_port);
621 options = ntohl (t4->options);
622 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
627 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s",
628 TRANSPORT_SESSION_INBOUND_STRING);
632 LOG (GNUNET_ERROR_TYPE_WARNING,
633 _("Unexpected address length: %u bytes\n"),
634 (unsigned int) addrlen);
637 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
639 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
642 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
643 PLUGIN_NAME, options, buf, port);
649 * Function called to convert a string address to
652 * @param cls closure ('struct Plugin*')
653 * @param addr string address
654 * @param addrlen length of the address
655 * @param buf location to store the buffer
656 * @param added location to store the number of bytes in the buffer.
657 * If the function returns #GNUNET_SYSERR, its contents are undefined.
658 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
661 tcp_string_to_address (void *cls,
664 void **buf, size_t *added)
666 struct sockaddr_storage socket_address;
672 /* Format tcp.options.address:port */
676 if ((NULL == addr) || (addrlen == 0))
679 return GNUNET_SYSERR;
681 if ('\0' != addr[addrlen - 1])
684 return GNUNET_SYSERR;
686 if (strlen (addr) != addrlen - 1)
689 return GNUNET_SYSERR;
691 plugin = GNUNET_strdup (addr);
692 optionstr = strchr (plugin, '.');
693 if (NULL == optionstr)
696 GNUNET_free (plugin);
697 return GNUNET_SYSERR;
701 options = atol (optionstr);
702 address = strchr (optionstr, '.');
706 GNUNET_free (plugin);
707 return GNUNET_SYSERR;
713 GNUNET_STRINGS_to_address_ip (address, strlen (address),
717 GNUNET_free (plugin);
718 return GNUNET_SYSERR;
721 GNUNET_free (plugin);
722 switch (socket_address.ss_family)
726 struct IPv4TcpAddress *t4;
727 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
728 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
729 t4->options = htonl (options);
730 t4->ipv4_addr = in4->sin_addr.s_addr;
731 t4->t4_port = in4->sin_port;
733 *added = sizeof (struct IPv4TcpAddress);
738 struct IPv6TcpAddress *t6;
739 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
740 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
741 t6->options = htonl (options);
742 t6->ipv6_addr = in6->sin6_addr;
743 t6->t6_port = in6->sin6_port;
745 *added = sizeof (struct IPv6TcpAddress);
749 return GNUNET_SYSERR;
754 struct SessionClientCtx
756 const struct GNUNET_SERVER_Client *client;
762 session_lookup_by_client_it (void *cls,
763 const struct GNUNET_PeerIdentity *key,
766 struct SessionClientCtx *sc_ctx = cls;
767 struct Session *s = value;
769 if (s->client == sc_ctx->client)
779 * Find the session handle for the given client.
781 * @param plugin the plugin
782 * @param client which client to find the session handle for
783 * @return NULL if no matching session exists
785 static struct Session *
786 lookup_session_by_client (struct Plugin *plugin,
787 const struct GNUNET_SERVER_Client *client)
789 struct SessionClientCtx sc_ctx;
791 sc_ctx.client = client;
793 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
799 * Create a new session. Also queues a welcome message.
801 * @param plugin the plugin
802 * @param target peer to connect to
803 * @param client client to use, reference counter must have already been increased
804 * @param is_nat this a NAT session, we should wait for a client to
805 * connect to us from an address, then assign that to
807 * @return new session object
809 static struct Session *
810 create_session (struct Plugin *plugin, 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_malloc (sizeof (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 = htonl (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 session session to close down
1024 disconnect_session (struct Session *session)
1026 struct PendingMessage *pm;
1027 struct Plugin * plugin = session->plugin;
1029 LOG (GNUNET_ERROR_TYPE_DEBUG,
1030 "Disconnecting session of peer `%s' address `%s'\n",
1031 GNUNET_i2s (&session->target),
1032 tcp_address_to_string (NULL, session->addr, session->addrlen));
1034 stop_session_timeout (session);
1037 GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
1041 GNUNET_STATISTICS_update (session->plugin->env->stats,
1042 gettext_noop ("# TCP sessions active"), -1,
1044 dec_sessions (plugin, session, __LINE__);
1048 GNUNET_assert (GNUNET_YES ==
1049 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns,
1053 /* clean up state */
1054 if (NULL != session->transmit_handle)
1056 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1057 session->transmit_handle = NULL;
1059 session->plugin->env->session_end (session->plugin->env->cls,
1060 &session->target, session);
1062 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
1064 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1065 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1068 while (NULL != (pm = session->pending_messages_head))
1070 LOG (GNUNET_ERROR_TYPE_DEBUG,
1071 pm->transmit_cont !=
1072 NULL ? "Could not deliver message to `%4s'.\n" :
1073 "Could not deliver message to `%4s', notifying.\n",
1074 GNUNET_i2s (&session->target));
1075 GNUNET_STATISTICS_update (session->plugin->env->stats,
1076 gettext_noop ("# bytes currently in TCP buffers"),
1077 -(int64_t) pm->message_size, GNUNET_NO);
1078 GNUNET_STATISTICS_update (session->plugin->env->stats,
1080 ("# bytes discarded by TCP (disconnect)"),
1081 pm->message_size, GNUNET_NO);
1082 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1083 session->pending_messages_tail, pm);
1084 if (NULL != pm->transmit_cont)
1085 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1086 GNUNET_SYSERR, pm->message_size, 0);
1089 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1091 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1092 if (NULL != session->client)
1093 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1095 if (NULL != session->client)
1097 GNUNET_SERVER_client_disconnect (session->client);
1098 GNUNET_SERVER_client_drop (session->client);
1099 session->client = NULL;
1101 GNUNET_free_non_null (session->addr);
1102 GNUNET_assert (NULL == session->transmit_handle);
1103 GNUNET_free (session);
1107 struct FindSessionContext
1115 session_it (void *cls,
1116 const struct GNUNET_PeerIdentity * key,
1119 struct FindSessionContext *res = cls;
1121 if (res->s == value)
1123 res->res = GNUNET_OK;
1131 find_session (struct Plugin *plugin, struct Session *session)
1133 struct FindSessionContext session_map_res;
1134 struct FindSessionContext nat_map_res;
1136 session_map_res.s = session;
1137 session_map_res.res = GNUNET_SYSERR;
1138 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1140 nat_map_res.s = session;
1141 nat_map_res.res = GNUNET_SYSERR;
1142 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1144 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1147 return GNUNET_SYSERR;
1154 * Function that can be used by the transport service to transmit
1155 * a message using the plugin. Note that in the case of a
1156 * peer disconnecting, the continuation MUST be called
1157 * prior to the disconnect notification itself. This function
1158 * will be called with this peer's HELLO message to initiate
1159 * a fresh connection to another peer.
1161 * @param cls closure
1162 * @param session which session must be used
1163 * @param msgbuf the message to transmit
1164 * @param msgbuf_size number of bytes in 'msgbuf'
1165 * @param priority how important is the message (most plugins will
1166 * ignore message priority and just FIFO)
1167 * @param to how long to wait at most for the transmission (does not
1168 * require plugins to discard the message after the timeout,
1169 * just advisory for the desired delay; most plugins will ignore
1171 * @param cont continuation to call once the message has
1172 * been transmitted (or if the transport is ready
1173 * for the next transmission call; or if the
1174 * peer disconnected...); can be NULL
1175 * @param cont_cls closure for cont
1176 * @return number of bytes used (on the physical network, with overheads);
1177 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1178 * and does NOT mean that the message was not transmitted (DV)
1181 tcp_plugin_send (void *cls,
1182 struct Session *session,
1183 const char *msgbuf, size_t msgbuf_size,
1184 unsigned int priority,
1185 struct GNUNET_TIME_Relative to,
1186 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1188 struct Plugin * plugin = cls;
1189 struct PendingMessage *pm;
1191 GNUNET_assert (NULL != plugin);
1192 GNUNET_assert (NULL != session);
1194 if (GNUNET_SYSERR == find_session(plugin, session))
1196 LOG (GNUNET_ERROR_TYPE_ERROR,
1197 _("Trying to send with invalid session %p\n"));
1198 return GNUNET_SYSERR;
1201 /* create new message entry */
1202 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1203 pm->msg = (const char *) &pm[1];
1204 memcpy (&pm[1], msgbuf, msgbuf_size);
1205 pm->message_size = msgbuf_size;
1206 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1207 pm->transmit_cont = cont;
1208 pm->transmit_cont_cls = cont_cls;
1210 LOG (GNUNET_ERROR_TYPE_DEBUG,
1211 "Asked to transmit %u bytes to `%s', added message to list.\n",
1212 msgbuf_size, GNUNET_i2s (&session->target));
1214 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1218 GNUNET_assert (session->client != NULL);
1219 reschedule_session_timeout (session);
1220 GNUNET_SERVER_client_set_timeout (session->client,
1221 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1222 GNUNET_STATISTICS_update (plugin->env->stats,
1223 gettext_noop ("# bytes currently in TCP buffers"),
1224 msgbuf_size, GNUNET_NO);
1226 /* append pm to pending_messages list */
1227 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1228 session->pending_messages_tail, pm);
1230 process_pending_messages (session);
1233 else if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value(plugin->nat_wait_conns, &session->target, session))
1235 LOG (GNUNET_ERROR_TYPE_DEBUG,
1236 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1237 GNUNET_i2s (&session->target));
1238 reschedule_session_timeout (session);
1239 GNUNET_STATISTICS_update (plugin->env->stats,
1240 gettext_noop ("# bytes currently in TCP buffers"),
1241 msgbuf_size, GNUNET_NO);
1243 /* append pm to pending_messages list */
1244 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1245 session->pending_messages_tail, pm);
1250 LOG (GNUNET_ERROR_TYPE_ERROR,
1251 "Invalid session %p\n", session);
1253 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1256 return GNUNET_SYSERR; /* session does not exist here */
1265 struct Session *result;
1270 session_lookup_it (void *cls,
1271 const struct GNUNET_PeerIdentity *key,
1274 struct SessionItCtx * si_ctx = cls;
1275 struct Session * session = value;
1277 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1278 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1279 LOG (GNUNET_ERROR_TYPE_DEBUG,
1280 "Comparing: %s %u <-> %s %u\n",
1288 if (session->addrlen != si_ctx->addrlen)
1292 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1297 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1298 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1299 LOG (GNUNET_ERROR_TYPE_DEBUG,
1300 "Comparing: %s %u <-> %s %u , OK!\n",
1308 /* Found existing session */
1309 si_ctx->result = session;
1315 * Task cleaning up a NAT connection attempt after timeout
1318 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1320 struct Session *session = cls;
1322 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1323 LOG (GNUNET_ERROR_TYPE_DEBUG,
1324 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1325 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1326 disconnect_session (session);
1331 * Create a new session to transmit data to the target
1332 * This session will used to send data to this peer and the plugin will
1333 * notify us by calling the env->session_end function
1335 * @param cls closure
1336 * @param address pointer to the GNUNET_HELLO_Address
1337 * @return the session if the address is valid, NULL otherwise
1339 static struct Session *
1340 tcp_plugin_get_session (void *cls,
1341 const struct GNUNET_HELLO_Address *address)
1343 struct Plugin *plugin = cls;
1344 struct Session *session = NULL;
1348 struct GNUNET_CONNECTION_Handle *sa;
1349 struct sockaddr_in a4;
1350 struct sockaddr_in6 a6;
1351 const struct IPv4TcpAddress *t4;
1352 const struct IPv6TcpAddress *t6;
1353 struct GNUNET_ATS_Information ats;
1354 unsigned int is_natd = GNUNET_NO;
1357 GNUNET_assert (plugin != NULL);
1358 GNUNET_assert (address != NULL);
1359 addrlen = address->address_length;
1360 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "Trying to get session for `%s' address of peer `%s'\n",
1362 tcp_address_to_string(NULL, address->address, address->address_length),
1363 GNUNET_i2s (&address->peer));
1365 /* look for existing session */
1367 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1370 struct SessionItCtx si_ctx;
1372 si_ctx.addr = (void *) address->address;
1373 si_ctx.addrlen = address->address_length;
1375 si_ctx.result = NULL;
1377 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1379 &session_lookup_it, &si_ctx);
1380 if (si_ctx.result != NULL)
1382 session = si_ctx.result;
1383 LOG (GNUNET_ERROR_TYPE_DEBUG,
1384 "Found existing session for `%s' address `%s' session %p\n",
1385 GNUNET_i2s (&address->peer),
1386 tcp_address_to_string(NULL, address->address, address->address_length),
1390 LOG (GNUNET_ERROR_TYPE_DEBUG,
1391 "Existing sessions did not match address `%s' or peer `%s'\n",
1392 tcp_address_to_string(NULL, address->address, address->address_length),
1393 GNUNET_i2s (&address->peer));
1396 if (addrlen == sizeof (struct IPv6TcpAddress))
1398 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1399 t6 = address->address;
1401 memset (&a6, 0, sizeof (a6));
1402 #if HAVE_SOCKADDR_IN_SIN_LEN
1403 a6.sin6_len = sizeof (a6);
1405 a6.sin6_family = AF_INET6;
1406 a6.sin6_port = t6->t6_port;
1407 if (t6->t6_port == 0)
1408 is_natd = GNUNET_YES;
1409 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1413 else if (addrlen == sizeof (struct IPv4TcpAddress))
1415 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1416 t4 = address->address;
1418 memset (&a4, 0, sizeof (a4));
1419 #if HAVE_SOCKADDR_IN_SIN_LEN
1420 a4.sin_len = sizeof (a4);
1422 a4.sin_family = AF_INET;
1423 a4.sin_port = t4->t4_port;
1424 if (t4->t4_port == 0)
1425 is_natd = GNUNET_YES;
1426 a4.sin_addr.s_addr = t4->ipv4_addr;
1432 GNUNET_STATISTICS_update (plugin->env->stats,
1434 ("# requests to create session with invalid address"),
1439 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1441 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1443 /* NAT client only works with IPv4 addresses */
1447 if (plugin->cur_connections >= plugin->max_connections)
1453 if ((is_natd == GNUNET_YES) &&
1455 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1458 /* Only do one NAT punch attempt per peer identity */
1462 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1464 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1467 LOG (GNUNET_ERROR_TYPE_DEBUG,
1468 "Found valid IPv4 NAT address (creating session)!\n") ;
1469 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1470 session->addrlen = 0;
1471 session->addr = NULL;
1472 session->ats_address_network_type = ats.value;
1473 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1474 &nat_connect_timeout,
1476 GNUNET_assert (session != NULL);
1477 GNUNET_assert (GNUNET_OK ==
1478 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1483 LOG (GNUNET_ERROR_TYPE_DEBUG,
1484 "Created NAT WAIT connection to `%4s' at `%s'\n",
1485 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1487 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1491 LOG (GNUNET_ERROR_TYPE_DEBUG,
1492 "Running NAT client for `%4s' at `%s' failed\n",
1493 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1494 disconnect_session (session);
1499 /* create new outbound session */
1500 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1501 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1504 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 "Failed to create connection to `%4s' at `%s'\n",
1506 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1509 plugin->cur_connections++;
1510 if (plugin->cur_connections == plugin->max_connections)
1511 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1514 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1515 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1517 session = create_session (plugin,
1519 GNUNET_SERVER_connect_socket (plugin->server, sa),
1521 session->addr = GNUNET_malloc (addrlen);
1522 memcpy (session->addr, address->address, addrlen);
1523 session->addrlen = addrlen;
1524 session->ats_address_network_type = ats.value;
1526 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1528 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1529 inc_sessions (plugin, session, __LINE__);
1530 LOG (GNUNET_ERROR_TYPE_DEBUG,
1531 "Creating new session for `%s' address `%s' session %p\n",
1532 GNUNET_i2s (&address->peer),
1533 tcp_address_to_string(NULL, address->address, address->address_length),
1535 /* Send TCP Welcome */
1536 process_pending_messages (session);
1543 session_disconnect_it (void *cls,
1544 const struct GNUNET_PeerIdentity *key,
1547 struct Session *session = value;
1549 GNUNET_STATISTICS_update (session->plugin->env->stats,
1551 ("# transport-service disconnect requests for TCP"),
1553 disconnect_session (session);
1559 * Function that can be called to force a disconnect from the
1560 * specified neighbour. This should also cancel all previously
1561 * scheduled transmissions. Obviously the transmission may have been
1562 * partially completed already, which is OK. The plugin is supposed
1563 * to close the connection (if applicable) and no longer call the
1564 * transmit continuation(s).
1566 * Finally, plugin MUST NOT call the services's receive function to
1567 * notify the service that the connection to the specified target was
1568 * closed after a getting this call.
1570 * @param cls closure
1571 * @param target peer for which the last transmission is
1575 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1577 struct Plugin *plugin = cls;
1579 LOG (GNUNET_ERROR_TYPE_DEBUG,
1580 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1581 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1582 &session_disconnect_it, plugin);
1583 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1584 &session_disconnect_it, plugin);
1589 * Running pretty printers: head
1591 static struct PrettyPrinterContext *ppc_dll_head;
1594 * Running pretty printers: tail
1596 static struct PrettyPrinterContext *ppc_dll_tail;
1599 * Context for address to string conversion.
1601 struct PrettyPrinterContext
1606 struct PrettyPrinterContext *next;
1611 struct PrettyPrinterContext *prev;
1616 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1621 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1624 * Function to call with the result.
1626 GNUNET_TRANSPORT_AddressStringCallback asc;
1629 * Clsoure for 'asc'.
1634 * Port to add after the IP address.
1651 ppc_cancel_task (void *cls,
1652 const struct GNUNET_SCHEDULER_TaskContext *tc)
1655 struct PrettyPrinterContext *ppc = cls;
1656 struct PrettyPrinterContext *cur;
1658 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1664 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cancel request %p, %u pending\n", ppc, count);
1665 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1666 if (NULL != ppc->resolver_handle)
1668 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1669 ppc->resolver_handle = NULL;
1671 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1677 * Append our port and forward the result.
1679 * @param cls the 'struct PrettyPrinterContext*'
1680 * @param hostname hostname part of the address
1683 append_port (void *cls, const char *hostname)
1685 struct PrettyPrinterContext *ppc = cls;
1686 struct PrettyPrinterContext *cur;
1690 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1695 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Callback request %p == %s, %u pending\n", ppc, hostname, count);
1696 if (hostname == NULL)
1698 ppc->asc (ppc->asc_cls, NULL);
1699 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1700 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1701 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1702 ppc->resolver_handle = NULL;
1703 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done request %p, %u pending\n", ppc, count);
1707 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
1718 if (GNUNET_YES == ppc->ipv6)
1719 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1721 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1722 ppc->asc (ppc->asc_cls, ret);
1728 * Convert the transports address to a nice, human-readable
1731 * @param cls closure
1732 * @param type name of the transport that generated the address
1733 * @param addr one of the addresses of the host, NULL for the last address
1734 * the specific address format depends on the transport
1735 * @param addrlen length of the address
1736 * @param numeric should (IP) addresses be displayed in numeric form?
1737 * @param timeout after how long should we give up?
1738 * @param asc function to call on each string
1739 * @param asc_cls closure for asc
1742 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1743 const void *addr, size_t addrlen,
1745 struct GNUNET_TIME_Relative timeout,
1746 GNUNET_TRANSPORT_AddressStringCallback asc,
1749 struct PrettyPrinterContext *ppc;
1752 struct sockaddr_in a4;
1753 struct sockaddr_in6 a6;
1754 const struct IPv4TcpAddress *t4;
1755 const struct IPv6TcpAddress *t6;
1759 if (addrlen == sizeof (struct IPv6TcpAddress))
1762 memset (&a6, 0, sizeof (a6));
1763 a6.sin6_family = AF_INET6;
1764 a6.sin6_port = t6->t6_port;
1765 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1766 port = ntohs (t6->t6_port);
1767 options = ntohl (t6->options);
1771 else if (addrlen == sizeof (struct IPv4TcpAddress))
1774 memset (&a4, 0, sizeof (a4));
1775 a4.sin_family = AF_INET;
1776 a4.sin_port = t4->t4_port;
1777 a4.sin_addr.s_addr = t4->ipv4_addr;
1778 port = ntohs (t4->t4_port);
1779 options = ntohl (t4->options);
1783 else if (0 == addrlen)
1785 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1786 asc (asc_cls, NULL);
1791 /* invalid address */
1792 GNUNET_break_op (0);
1793 asc (asc_cls, NULL);
1796 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1797 if (addrlen == sizeof (struct IPv6TcpAddress))
1798 ppc->ipv6 = GNUNET_YES;
1800 ppc->ipv6 = GNUNET_NO;
1802 ppc->asc_cls = asc_cls;
1804 ppc->options = options;
1805 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
1806 &ppc_cancel_task, ppc);
1807 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1810 if (NULL != ppc->resolver_handle)
1812 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1813 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
1824 * Check if the given port is plausible (must be either our listen
1825 * port or our advertised port), or any port if we are behind NAT
1826 * and do not have a port open. If it is neither, we return
1829 * @param plugin global variables
1830 * @param in_port port number to check
1831 * @return GNUNET_OK if port is either open_port or adv_port
1834 check_port (struct Plugin *plugin, uint16_t in_port)
1836 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1838 return GNUNET_SYSERR;
1843 * Function that will be called to check if a binary address for this
1844 * plugin is well-formed and corresponds to an address for THIS peer
1845 * (as per our configuration). Naturally, if absolutely necessary,
1846 * plugins can be a bit conservative in their answer, but in general
1847 * plugins should make sure that the address does not redirect
1848 * traffic to a 3rd party that might try to man-in-the-middle our
1851 * @param cls closure, our 'struct Plugin*'
1852 * @param addr pointer to the address
1853 * @param addrlen length of addr
1854 * @return GNUNET_OK if this is a plausible address for this peer
1855 * and transport, GNUNET_SYSERR if not
1858 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1860 struct Plugin *plugin = cls;
1861 struct IPv4TcpAddress *v4;
1862 struct IPv6TcpAddress *v6;
1864 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1865 (addrlen != sizeof (struct IPv6TcpAddress)))
1869 return GNUNET_SYSERR;
1872 if (addrlen == sizeof (struct IPv4TcpAddress))
1874 v4 = (struct IPv4TcpAddress *) addr;
1875 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions)))
1878 return GNUNET_SYSERR;
1880 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1881 return GNUNET_SYSERR;
1883 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1884 sizeof (struct in_addr)))
1885 return GNUNET_SYSERR;
1889 v6 = (struct IPv6TcpAddress *) addr;
1890 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1892 GNUNET_break_op (0);
1893 return GNUNET_SYSERR;
1895 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions)))
1898 return GNUNET_SYSERR;
1900 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1901 return GNUNET_SYSERR;
1903 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1904 sizeof (struct in6_addr)))
1905 return GNUNET_SYSERR;
1912 * We've received a nat probe from this peer via TCP. Finish
1913 * creating the client session and resume sending of queued
1916 * @param cls closure
1917 * @param client identification of the client
1918 * @param message the actual message
1921 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1922 const struct GNUNET_MessageHeader *message)
1924 struct Plugin *plugin = cls;
1925 struct Session *session;
1926 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1929 struct IPv4TcpAddress *t4;
1930 struct IPv6TcpAddress *t6;
1931 const struct sockaddr_in *s4;
1932 const struct sockaddr_in6 *s6;
1934 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1936 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1937 * a connection to this peer by running gnunet-nat-client. This peer
1938 * received the punch message and now wants us to use the new connection
1939 * as the default for that peer. Do so and then send a WELCOME message
1940 * so we can really be connected!
1942 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1944 GNUNET_break_op (0);
1945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1949 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1951 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1952 sizeof (struct GNUNET_PeerIdentity)))
1954 /* refuse connections from ourselves */
1955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1960 GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
1963 if (session == NULL)
1965 LOG (GNUNET_ERROR_TYPE_DEBUG,
1966 "Did NOT find session for NAT probe!\n");
1967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1970 LOG (GNUNET_ERROR_TYPE_DEBUG,
1971 "Found session for NAT probe!\n");
1973 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1975 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1976 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1979 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1983 disconnect_session (session);
1986 GNUNET_assert (GNUNET_CONTAINER_multipeermap_remove
1987 (plugin->nat_wait_conns,
1988 &tcp_nat_probe->clientIdentity,
1989 session) == GNUNET_YES);
1990 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1991 &session->target, session,
1992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1993 session->last_activity = GNUNET_TIME_absolute_get ();
1994 session->inbound = GNUNET_NO;
1995 LOG (GNUNET_ERROR_TYPE_DEBUG,
1996 "Found address `%s' for incoming connection\n",
1997 GNUNET_a2s (vaddr, alen));
1998 switch (((const struct sockaddr *) vaddr)->sa_family)
2002 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2004 t4->t4_port = s4->sin_port;
2005 t4->ipv4_addr = s4->sin_addr.s_addr;
2007 session->addrlen = sizeof (struct IPv4TcpAddress);
2011 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2013 t6->t6_port = s6->sin6_port;
2014 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2016 session->addrlen = sizeof (struct IPv6TcpAddress);
2019 GNUNET_break_op (0);
2020 LOG (GNUNET_ERROR_TYPE_DEBUG,
2021 "Bad address for incoming connection!\n");
2022 GNUNET_free (vaddr);
2023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2024 disconnect_session (session);
2027 GNUNET_free (vaddr);
2028 GNUNET_break (NULL == session->client);
2029 GNUNET_SERVER_client_keep (client);
2030 session->client = client;
2031 inc_sessions (plugin, session, __LINE__);
2032 GNUNET_STATISTICS_update (plugin->env->stats,
2033 gettext_noop ("# TCP sessions active"), 1,
2035 process_pending_messages (session);
2036 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2041 * We've received a welcome from this peer via TCP. Possibly create a
2042 * fresh client record and send back our welcome.
2044 * @param cls closure
2045 * @param client identification of the client
2046 * @param message the actual message
2049 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2050 const struct GNUNET_MessageHeader *message)
2052 struct Plugin *plugin = cls;
2053 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2054 struct Session *session;
2057 struct IPv4TcpAddress *t4;
2058 struct IPv6TcpAddress *t6;
2059 const struct sockaddr_in *s4;
2060 const struct sockaddr_in6 *s6;
2061 struct GNUNET_ATS_Information ats;
2064 memcmp (&wm->clientIdentity, plugin->env->my_identity,
2065 sizeof (struct GNUNET_PeerIdentity)))
2067 /* refuse connections from ourselves */
2068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2069 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2071 LOG (GNUNET_ERROR_TYPE_WARNING,
2072 "Received %s message from my own identity `%4s' on address `%s'\n",
2073 "WELCOME", GNUNET_i2s (&wm->clientIdentity), GNUNET_a2s (vaddr, alen));
2074 GNUNET_free (vaddr);
2076 GNUNET_break_op (0);
2079 LOG (GNUNET_ERROR_TYPE_DEBUG,
2080 "Received %s message from `%4s' %p\n", "WELCOME",
2081 GNUNET_i2s (&wm->clientIdentity), client);
2082 GNUNET_STATISTICS_update (plugin->env->stats,
2083 gettext_noop ("# TCP WELCOME messages received"), 1,
2085 session = lookup_session_by_client (plugin, client);
2086 if (session != NULL)
2088 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2090 LOG (GNUNET_ERROR_TYPE_DEBUG,
2091 "Found existing session %p for peer `%s'\n",
2093 GNUNET_a2s (vaddr, alen));
2094 GNUNET_free (vaddr);
2099 GNUNET_SERVER_client_keep (client);
2100 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
2101 plugin->cur_connections++;
2102 if (plugin->cur_connections == plugin->max_connections)
2103 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2105 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
2106 session->inbound = GNUNET_YES;
2107 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2109 if (alen == sizeof (struct sockaddr_in))
2112 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2113 t4->options = htonl (0);
2114 t4->t4_port = s4->sin_port;
2115 t4->ipv4_addr = s4->sin_addr.s_addr;
2117 session->addrlen = sizeof (struct IPv4TcpAddress);
2119 else if (alen == sizeof (struct sockaddr_in6))
2122 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2123 t6->options = htonl (0);
2124 t6->t6_port = s6->sin6_port;
2125 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2127 session->addrlen = sizeof (struct IPv6TcpAddress);
2130 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
2131 session->ats_address_network_type = ats.value;
2132 LOG (GNUNET_ERROR_TYPE_DEBUG,
2133 "Creating new session %p for peer `%s'\n",
2135 GNUNET_a2s (vaddr, alen));
2136 GNUNET_free (vaddr);
2137 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2141 inc_sessions (plugin, session, __LINE__);
2145 LOG (GNUNET_ERROR_TYPE_DEBUG,
2146 "Did not obtain TCP socket address for incoming connection\n");
2151 if (session->expecting_welcome != GNUNET_YES)
2153 GNUNET_break_op (0);
2154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2158 session->last_activity = GNUNET_TIME_absolute_get ();
2159 session->expecting_welcome = GNUNET_NO;
2161 /* Notify transport and ATS about new session */
2162 if (GNUNET_YES == session->inbound)
2164 plugin->env->session_start (NULL, &wm->clientIdentity, PLUGIN_NAME,
2165 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2166 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2170 process_pending_messages (session);
2172 GNUNET_SERVER_client_set_timeout (client,
2173 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2179 * Task to signal the server that we can continue
2180 * receiving from the TCP client now.
2182 * @param cls the 'struct Session*'
2183 * @param tc task context (unused)
2186 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2188 struct Session *session = cls;
2190 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2191 reschedule_session_timeout (session);
2193 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2198 * We've received data for this peer via TCP. Unbox,
2199 * compute latency and forward.
2201 * @param cls closure
2202 * @param client identification of the client
2203 * @param message the actual message
2206 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2207 const struct GNUNET_MessageHeader *message)
2209 struct Plugin *plugin = cls;
2210 struct Session *session;
2211 struct GNUNET_TIME_Relative delay;
2214 type = ntohs (message->type);
2215 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2216 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2218 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2219 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2222 session = lookup_session_by_client (plugin, client);
2223 if (NULL == session)
2225 /* No inbound session found */
2229 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2230 LOG (GNUNET_ERROR_TYPE_ERROR,
2231 "Received unexpected %u bytes of type %u from `%s'\n",
2232 (unsigned int) ntohs (message->size),
2233 (unsigned int) ntohs (message->type),
2234 GNUNET_a2s(vaddr, alen));
2235 GNUNET_break_op (0);
2236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2237 GNUNET_free_non_null(vaddr);
2240 else if (GNUNET_YES == session->expecting_welcome)
2242 /* Session is expecting WELCOME message */
2246 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2247 LOG (GNUNET_ERROR_TYPE_ERROR,
2248 "Received unexpected %u bytes of type %u from `%s'\n",
2249 (unsigned int) ntohs (message->size),
2250 (unsigned int) ntohs (message->type),
2251 GNUNET_a2s(vaddr, alen));
2252 GNUNET_break_op (0);
2253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2254 GNUNET_free_non_null(vaddr);
2258 session->last_activity = GNUNET_TIME_absolute_get ();
2259 LOG (GNUNET_ERROR_TYPE_DEBUG,
2260 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2261 (unsigned int) ntohs (message->size),
2262 (unsigned int) ntohs (message->type),
2263 GNUNET_i2s (&session->target));
2265 GNUNET_STATISTICS_update (plugin->env->stats,
2266 gettext_noop ("# bytes received via TCP"),
2267 ntohs (message->size), GNUNET_NO);
2268 struct GNUNET_ATS_Information distance;
2270 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2271 distance.value = session->ats_address_network_type;
2272 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2274 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2278 delay = plugin->env->receive (plugin->env->cls,
2282 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2283 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2284 plugin->env->update_address_metrics (plugin->env->cls,
2286 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2287 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2288 session, &distance, 1);
2290 reschedule_session_timeout (session);
2292 if (0 == delay.rel_value_us)
2294 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2298 LOG (GNUNET_ERROR_TYPE_DEBUG,
2299 "Throttling receiving from `%s' for %s\n",
2300 GNUNET_i2s (&session->target),
2301 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2302 GNUNET_SERVER_disable_receive_done_warning (client);
2303 session->receive_delay_task =
2304 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2310 * Functions with this signature are called whenever a peer
2311 * is disconnected on the network level.
2313 * @param cls closure
2314 * @param client identification of the client
2317 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2319 struct Plugin *plugin = cls;
2320 struct Session *session;
2324 session = lookup_session_by_client (plugin, client);
2325 if (session == NULL)
2326 return; /* unknown, nothing to do */
2327 LOG (GNUNET_ERROR_TYPE_DEBUG,
2328 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2329 GNUNET_i2s (&session->target),
2331 NULL) ? tcp_address_to_string (session->plugin,
2336 if (plugin->cur_connections == plugin->max_connections)
2337 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2339 if (plugin->cur_connections < 1)
2342 plugin->cur_connections--;
2344 GNUNET_STATISTICS_update (session->plugin->env->stats,
2346 ("# network-level TCP disconnect events"), 1,
2348 disconnect_session (session);
2353 * We can now send a probe message, copy into buffer to really send.
2355 * @param cls closure, a struct TCPProbeContext
2356 * @param size max size to copy
2357 * @param buf buffer to copy message to
2358 * @return number of bytes copied into buf
2361 notify_send_probe (void *cls, size_t size, void *buf)
2363 struct TCPProbeContext *tcp_probe_ctx = cls;
2364 struct Plugin *plugin = tcp_probe_ctx->plugin;
2367 tcp_probe_ctx->transmit_handle = NULL;
2368 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2372 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2373 GNUNET_free (tcp_probe_ctx);
2376 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2377 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2378 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2379 tcp_probe_ctx->sock);
2380 ret = sizeof (tcp_probe_ctx->message);
2381 GNUNET_free (tcp_probe_ctx);
2387 * Function called by the NAT subsystem suggesting another peer wants
2388 * to connect to us via connection reversal. Try to connect back to the
2391 * @param cls closure
2392 * @param addr address to try
2393 * @param addrlen number of bytes in @a addr
2396 try_connection_reversal (void *cls, const struct sockaddr *addr,
2399 struct Plugin *plugin = cls;
2400 struct GNUNET_CONNECTION_Handle *sock;
2401 struct TCPProbeContext *tcp_probe_ctx;
2404 * We have received an ICMP response, ostensibly from a peer
2405 * that wants to connect to us! Send a message to establish a connection.
2407 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2410 /* failed for some odd reason (out of sockets?); ignore attempt */
2414 /* FIXME: do we need to track these probe context objects so that
2415 * we can clean them up on plugin unload? */
2416 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2417 tcp_probe_ctx->message.header.size =
2418 htons (sizeof (struct TCP_NAT_ProbeMessage));
2419 tcp_probe_ctx->message.header.type =
2420 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2421 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2422 sizeof (struct GNUNET_PeerIdentity));
2423 tcp_probe_ctx->plugin = plugin;
2424 tcp_probe_ctx->sock = sock;
2425 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2427 tcp_probe_ctx->transmit_handle =
2428 GNUNET_CONNECTION_notify_transmit_ready (sock,
2429 ntohs (tcp_probe_ctx->
2430 message.header.size),
2431 GNUNET_TIME_UNIT_FOREVER_REL,
2439 * Session was idle, so disconnect it
2442 session_timeout (void *cls,
2443 const struct GNUNET_SCHEDULER_TaskContext *tc)
2445 struct Session *s = cls;
2447 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2449 "Session %p was idle for %s, disconnecting\n",
2451 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2453 /* call session destroy function */
2454 disconnect_session (s);
2459 * Start session timeout
2462 start_session_timeout (struct Session *s)
2464 GNUNET_assert (NULL != s);
2465 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2466 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "Timeout for session %p set to %s\n",
2472 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2478 * Increment session timeout due to activity
2481 reschedule_session_timeout (struct Session *s)
2483 GNUNET_assert (NULL != s);
2484 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2486 GNUNET_SCHEDULER_cancel (s->timeout_task);
2487 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491 "Timeout rescheduled for session %p set to %s\n",
2493 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2501 * @param s session to cancel timeout for
2504 stop_session_timeout (struct Session *s)
2506 GNUNET_assert (NULL != s);
2508 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2510 GNUNET_SCHEDULER_cancel (s->timeout_task);
2511 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "Timeout stopped for session %p canceled\n",
2519 * Function obtain the network type for a session
2521 * @param cls closure ('struct Plugin*')
2522 * @param session the session
2523 * @return the network type in HBO or GNUNET_SYSERR
2525 static enum GNUNET_ATS_Network_Type
2526 tcp_get_network (void *cls,
2527 struct Session *session)
2529 GNUNET_assert (NULL != session);
2530 return ntohl (session->ats_address_network_type);
2535 * Entry point for the plugin.
2537 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2538 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2541 libgnunet_plugin_transport_tcp_init (void *cls)
2543 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2544 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2545 sizeof (struct WelcomeMessage)},
2546 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2547 sizeof (struct TCP_NAT_ProbeMessage)},
2548 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2551 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2552 struct GNUNET_TRANSPORT_PluginFunctions *api;
2553 struct Plugin *plugin;
2554 struct GNUNET_SERVICE_Context *service;
2555 unsigned long long aport;
2556 unsigned long long bport;
2557 unsigned long long max_connections;
2559 struct GNUNET_TIME_Relative idle_timeout;
2562 struct sockaddr **addrs;
2563 socklen_t *addrlens;
2566 if (NULL == env->receive)
2568 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2569 initialze the plugin or the API */
2570 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2572 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2573 api->address_to_string = &tcp_address_to_string;
2574 api->string_to_address = &tcp_string_to_address;
2578 GNUNET_assert (NULL != env->cfg);
2580 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2583 max_connections = 128;
2587 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2588 &bport)) || (bport > 65535) ||
2590 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2591 "ADVERTISED-PORT", &aport)) &&
2594 LOG (GNUNET_ERROR_TYPE_ERROR,
2596 ("Require valid port number for service `%s' in configuration!\n"),
2606 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2607 if (service == NULL)
2609 LOG (GNUNET_ERROR_TYPE_WARNING,
2610 _("Failed to start service.\n"));
2617 /* Initialize my flags */
2620 plugin = GNUNET_malloc (sizeof (struct Plugin));
2621 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections, GNUNET_YES);
2622 plugin->max_connections = max_connections;
2623 plugin->cur_connections = 0;
2624 plugin->open_port = bport;
2625 plugin->adv_port = aport;
2627 plugin->lsock = NULL;
2628 if ((service != NULL) &&
2631 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2634 for (ret = ret_s-1; ret >= 0; ret--)
2635 LOG (GNUNET_ERROR_TYPE_INFO,
2636 "Binding to address `%s'\n",
2637 GNUNET_a2s (addrs[ret], addrlens[ret]));
2639 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2640 (const struct sockaddr **) addrs, addrlens,
2641 &tcp_nat_port_map_callback,
2642 &try_connection_reversal, plugin);
2643 for (ret = ret_s -1; ret >= 0; ret--)
2645 GNUNET_assert (addrs[ret] != NULL);
2646 GNUNET_free (addrs[ret]);
2648 GNUNET_free_non_null (addrs);
2649 GNUNET_free_non_null (addrlens);
2653 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2654 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2655 &try_connection_reversal, plugin);
2657 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2659 api->send = &tcp_plugin_send;
2660 api->get_session = &tcp_plugin_get_session;
2662 api->disconnect = &tcp_plugin_disconnect;
2663 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2664 api->check_address = &tcp_plugin_check_address;
2665 api->address_to_string = &tcp_address_to_string;
2666 api->string_to_address = &tcp_string_to_address;
2667 api->get_network = &tcp_get_network;
2668 plugin->service = service;
2669 if (service != NULL)
2671 plugin->server = GNUNET_SERVICE_get_server (service);
2676 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2677 "TIMEOUT", &idle_timeout))
2679 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2680 "transport-tcp", "TIMEOUT");
2681 if (plugin->nat != NULL)
2682 GNUNET_NAT_unregister (plugin->nat);
2683 GNUNET_free (plugin);
2688 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2689 NULL, idle_timeout, GNUNET_YES);
2691 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2692 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2694 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2696 plugin->handlers[i].callback_cls = plugin;
2698 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2699 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2700 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES);
2702 LOG (GNUNET_ERROR_TYPE_INFO,
2703 _("TCP transport listening on port %llu\n"), bport);
2705 LOG (GNUNET_ERROR_TYPE_INFO,
2707 ("TCP transport not listening on any port (client only)\n"));
2709 LOG (GNUNET_ERROR_TYPE_INFO,
2711 ("TCP transport advertises itself as being on port %llu\n"),
2713 /* Initially set connections to 0 */
2714 GNUNET_assert (NULL != plugin->env->stats);
2715 GNUNET_STATISTICS_set (plugin->env->stats,
2716 gettext_noop ("# TCP sessions active"), 0,
2723 * Exit point from the plugin.
2726 libgnunet_plugin_transport_tcp_done (void *cls)
2728 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2729 struct Plugin *plugin = api->cls;
2730 struct TCPProbeContext *tcp_probe;
2731 struct PrettyPrinterContext *cur;
2732 struct PrettyPrinterContext *next;
2739 LOG (GNUNET_ERROR_TYPE_DEBUG,
2740 "Shutting down TCP plugin\n");
2742 /* Removing leftover sessions */
2743 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2744 &session_disconnect_it, NULL);
2745 /* Removing leftover NAT sessions */
2746 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2747 &session_disconnect_it, NULL);
2749 next = ppc_dll_head;
2750 for (cur = next; NULL != cur; cur = next)
2753 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
2754 if (NULL != cur->resolver_handle)
2755 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2756 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2761 if (plugin->service != NULL)
2762 GNUNET_SERVICE_stop (plugin->service);
2764 GNUNET_SERVER_destroy (plugin->server);
2765 GNUNET_free (plugin->handlers);
2766 if (plugin->nat != NULL)
2767 GNUNET_NAT_unregister (plugin->nat);
2768 while (NULL != (tcp_probe = plugin->probe_head))
2770 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2772 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2773 GNUNET_free (tcp_probe);
2775 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2776 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2777 GNUNET_free (plugin);
2782 /* end of plugin_transport_tcp.c */