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);
446 stop_session_timeout (struct Session *s);
451 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
454 static unsigned int sessions;
458 inc_sessions (struct Plugin *plugin, struct Session *session, int line)
461 unsigned int size = GNUNET_CONTAINER_multipeermap_size(plugin->sessionmap);
462 if (sessions != size)
463 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session increased to %u (session map size: %u): `%s' `%s'\n",
469 GNUNET_i2s (&session->target),
470 tcp_address_to_string (NULL, session->addr, session->addrlen));
475 dec_sessions (struct Plugin *plugin, struct Session *session, int line)
477 GNUNET_assert (sessions > 0);
478 unsigned int size = GNUNET_CONTAINER_multipeermap_size(plugin->sessionmap);
480 if (sessions != size)
481 LOG (GNUNET_ERROR_TYPE_DEBUG, "Inconsistent sessions %u <-> session map size: %u\n",
483 LOG (GNUNET_ERROR_TYPE_DEBUG, "%4i Session decreased to %u (session map size: %u): `%s' `%s'\n",
487 GNUNET_i2s (&session->target),
488 tcp_address_to_string (NULL, session->addr, session->addrlen));
494 * Function to check if an inbound connection is acceptable.
495 * Mostly used to limit the total number of open connections
498 * @param cls the 'struct Plugin'
499 * @param ucred credentials, if available, otherwise NULL
500 * @param addr address
501 * @param addrlen length of address
502 * @return GNUNET_YES to allow, GNUNET_NO to deny, GNUNET_SYSERR
503 * for unknown address family (will be denied).
506 plugin_tcp_access_check (void *cls,
507 const struct GNUNET_CONNECTION_Credentials *ucred,
508 const struct sockaddr *addr, socklen_t addrlen)
510 struct Plugin *plugin = cls;
511 LOG (GNUNET_ERROR_TYPE_DEBUG,
512 "Accepting new incoming TCP connection from `%s'\n",
513 GNUNET_a2s (addr, addrlen));
514 if (plugin->cur_connections >= plugin->max_connections)
516 plugin->cur_connections ++;
522 * Our external IP address/port mapping has changed.
524 * @param cls closure, the 'struct LocalAddrList'
525 * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
526 * the previous (now invalid) one
527 * @param addr either the previous or the new public IP address
528 * @param addrlen actual lenght of the address
531 tcp_nat_port_map_callback (void *cls, int add_remove,
532 const struct sockaddr *addr, socklen_t addrlen)
534 struct Plugin *plugin = cls;
535 struct IPv4TcpAddress t4;
536 struct IPv6TcpAddress t6;
540 LOG (GNUNET_ERROR_TYPE_INFO,
541 "NAT notification to %s address `%s'\n",
542 (GNUNET_YES == add_remove) ? "add" : "remove",
543 GNUNET_a2s (addr, addrlen));
544 /* convert 'addr' to our internal format */
545 switch (addr->sa_family)
548 GNUNET_assert (addrlen == sizeof (struct sockaddr_in));
549 memset (&t4,0, sizeof (t4));
550 t4.options = htonl (myoptions);
551 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
552 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
557 GNUNET_assert (addrlen == sizeof (struct sockaddr_in6));
558 memset (&t6, 0, sizeof (t6));
559 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
560 sizeof (struct in6_addr));
561 t6.options = htonl (myoptions);
562 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
570 /* modify our published address list */
571 plugin->env->notify_address (plugin->env->cls, add_remove, arg, args, "tcp");
576 * Function called for a quick conversion of the binary address to
577 * a numeric address. Note that the caller must not free the
578 * address and that the next call to this function is allowed
579 * to override the address again.
581 * @param cls closure ('struct Plugin*')
582 * @param addr binary address
583 * @param addrlen length of the address
584 * @return string representing the same address
587 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
589 static char rbuf[INET6_ADDRSTRLEN + 12];
590 char buf[INET6_ADDRSTRLEN];
594 const struct IPv4TcpAddress *t4;
595 const struct IPv6TcpAddress *t6;
602 case sizeof (struct IPv6TcpAddress):
605 port = ntohs (t6->t6_port);
606 options = ntohl (t6->options);
607 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
610 case sizeof (struct IPv4TcpAddress):
613 port = ntohs (t4->t4_port);
614 options = ntohl (t4->options);
615 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
620 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s",
621 TRANSPORT_SESSION_INBOUND_STRING);
625 LOG (GNUNET_ERROR_TYPE_WARNING,
626 _("Unexpected address length: %u bytes\n"),
627 (unsigned int) addrlen);
630 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
632 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
635 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
636 PLUGIN_NAME, options, buf, port);
642 * Function called to convert a string address to
645 * @param cls closure ('struct Plugin*')
646 * @param addr string address
647 * @param addrlen length of the address
648 * @param buf location to store the buffer
649 * @param added location to store the number of bytes in the buffer.
650 * If the function returns GNUNET_SYSERR, its contents are undefined.
651 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
654 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
655 void **buf, size_t *added)
657 struct sockaddr_storage socket_address;
663 /* Format tcp.options.address:port */
667 if ((NULL == addr) || (addrlen == 0))
670 return GNUNET_SYSERR;
672 if ('\0' != addr[addrlen - 1])
675 return GNUNET_SYSERR;
677 if (strlen (addr) != addrlen - 1)
680 return GNUNET_SYSERR;
682 plugin = GNUNET_strdup (addr);
683 optionstr = strchr (plugin, '.');
684 if (NULL == optionstr)
687 GNUNET_free (plugin);
688 return GNUNET_SYSERR;
692 options = atol (optionstr);
693 address = strchr (optionstr, '.');
697 GNUNET_free (plugin);
698 return GNUNET_SYSERR;
704 GNUNET_STRINGS_to_address_ip (address, strlen (address),
708 GNUNET_free (plugin);
709 return GNUNET_SYSERR;
712 GNUNET_free (plugin);
713 switch (socket_address.ss_family)
717 struct IPv4TcpAddress *t4;
718 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
719 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
720 t4->options = htonl (options);
721 t4->ipv4_addr = in4->sin_addr.s_addr;
722 t4->t4_port = in4->sin_port;
724 *added = sizeof (struct IPv4TcpAddress);
729 struct IPv6TcpAddress *t6;
730 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
731 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
732 t6->options = htonl (options);
733 t6->ipv6_addr = in6->sin6_addr;
734 t6->t6_port = in6->sin6_port;
736 *added = sizeof (struct IPv6TcpAddress);
740 return GNUNET_SYSERR;
745 struct SessionClientCtx
747 const struct GNUNET_SERVER_Client *client;
753 session_lookup_by_client_it (void *cls,
754 const struct GNUNET_PeerIdentity *key,
757 struct SessionClientCtx *sc_ctx = cls;
758 struct Session *s = value;
760 if (s->client == sc_ctx->client)
770 * Find the session handle for the given client.
772 * @param plugin the plugin
773 * @param client which client to find the session handle for
774 * @return NULL if no matching session exists
776 static struct Session *
777 lookup_session_by_client (struct Plugin *plugin,
778 const struct GNUNET_SERVER_Client *client)
780 struct SessionClientCtx sc_ctx;
782 sc_ctx.client = client;
784 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
790 * Create a new session. Also queues a welcome message.
792 * @param plugin the plugin
793 * @param target peer to connect to
794 * @param client client to use, reference counter must have already been increased
795 * @param is_nat this a NAT session, we should wait for a client to
796 * connect to us from an address, then assign that to
798 * @return new session object
800 static struct Session *
801 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
802 struct GNUNET_SERVER_Client *client, int is_nat)
804 struct Session *session;
805 struct PendingMessage *pm;
806 struct WelcomeMessage welcome;
808 if (GNUNET_YES != is_nat)
809 GNUNET_assert (NULL != client);
811 GNUNET_assert (NULL == client);
813 LOG (GNUNET_ERROR_TYPE_DEBUG,
814 "Creating new session for peer `%4s'\n",
815 GNUNET_i2s (target));
816 session = GNUNET_malloc (sizeof (struct Session));
817 session->last_activity = GNUNET_TIME_absolute_get ();
818 session->plugin = plugin;
819 session->is_nat = is_nat;
820 session->client = client;
821 session->target = *target;
822 session->expecting_welcome = GNUNET_YES;
823 session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
824 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
825 sizeof (struct WelcomeMessage));
826 pm->msg = (const char *) &pm[1];
827 pm->message_size = sizeof (struct WelcomeMessage);
828 welcome.header.size = htons (sizeof (struct WelcomeMessage));
829 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
830 welcome.clientIdentity = *plugin->env->my_identity;
831 memcpy (&pm[1], &welcome, sizeof (welcome));
832 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
833 GNUNET_STATISTICS_update (plugin->env->stats,
834 gettext_noop ("# bytes currently in TCP buffers"),
835 pm->message_size, GNUNET_NO);
836 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
837 session->pending_messages_tail, pm);
838 if (GNUNET_YES != is_nat)
840 GNUNET_STATISTICS_update (plugin->env->stats,
841 gettext_noop ("# TCP sessions active"), 1,
844 start_session_timeout (session);
851 * If we have pending messages, ask the server to
852 * transmit them (schedule the respective tasks, etc.)
854 * @param session for which session should we do this
857 process_pending_messages (struct Session *session);
861 * Function called to notify a client about the socket
862 * being ready to queue more data. "buf" will be
863 * NULL and "size" zero if the socket was closed for
864 * writing in the meantime.
867 * @param size number of bytes available in buf
868 * @param buf where the callee should write the message
869 * @return number of bytes written to buf
872 do_transmit (void *cls, size_t size, void *buf)
874 struct Session *session = cls;
875 struct GNUNET_PeerIdentity pid;
876 struct Plugin *plugin;
877 struct PendingMessage *pos;
878 struct PendingMessage *hd;
879 struct PendingMessage *tl;
880 struct GNUNET_TIME_Absolute now;
884 GNUNET_assert (NULL != session);
885 session->transmit_handle = NULL;
886 plugin = session->plugin;
889 LOG (GNUNET_ERROR_TYPE_DEBUG,
890 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
891 GNUNET_i2s (&session->target));
892 /* timeout; cancel all messages that have already expired */
896 now = GNUNET_TIME_absolute_get ();
897 while ((NULL != (pos = session->pending_messages_head)) &&
898 (pos->timeout.abs_value_us <= now.abs_value_us))
900 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
901 session->pending_messages_tail, pos);
902 LOG (GNUNET_ERROR_TYPE_DEBUG,
903 "Failed to transmit %u byte message to `%4s'.\n",
904 pos->message_size, GNUNET_i2s (&session->target));
905 ret += pos->message_size;
906 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
908 /* do this call before callbacks (so that if callbacks destroy
909 * session, they have a chance to cancel actions done by this
911 process_pending_messages (session);
912 pid = session->target;
913 /* no do callbacks and do not use session again since
914 * the callbacks may abort the session */
915 while (NULL != (pos = hd))
917 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
918 if (pos->transmit_cont != NULL)
919 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
922 GNUNET_STATISTICS_update (plugin->env->stats,
923 gettext_noop ("# bytes currently in TCP buffers"),
924 -(int64_t) ret, GNUNET_NO);
925 GNUNET_STATISTICS_update (plugin->env->stats,
927 ("# bytes discarded by TCP (timeout)"), ret,
931 /* copy all pending messages that would fit */
936 while (NULL != (pos = session->pending_messages_head))
938 if (ret + pos->message_size > size)
940 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
941 session->pending_messages_tail, pos);
942 GNUNET_assert (size >= pos->message_size);
943 LOG (GNUNET_ERROR_TYPE_DEBUG,
944 "Transmitting message of type %u\n",
945 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
946 /* FIXME: this memcpy can be up to 7% of our total runtime */
947 memcpy (cbuf, pos->msg, pos->message_size);
948 cbuf += pos->message_size;
949 ret += pos->message_size;
950 size -= pos->message_size;
951 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
953 /* schedule 'continuation' before callbacks so that callbacks that
954 * cancel everything don't cause us to use a session that no longer
956 process_pending_messages (session);
957 session->last_activity = GNUNET_TIME_absolute_get ();
958 pid = session->target;
959 /* we'll now call callbacks that may cancel the session; hence
960 * we should not use 'session' after this point */
961 while (NULL != (pos = hd))
963 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
964 if (pos->transmit_cont != NULL)
965 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
968 GNUNET_assert (hd == NULL);
969 GNUNET_assert (tl == NULL);
970 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
972 GNUNET_STATISTICS_update (plugin->env->stats,
973 gettext_noop ("# bytes currently in TCP buffers"),
974 -(int64_t) ret, GNUNET_NO);
975 GNUNET_STATISTICS_update (plugin->env->stats,
976 gettext_noop ("# bytes transmitted via TCP"), ret,
983 * If we have pending messages, ask the server to
984 * transmit them (schedule the respective tasks, etc.)
986 * @param session for which session should we do this
989 process_pending_messages (struct Session *session)
991 struct PendingMessage *pm;
993 GNUNET_assert (session->client != NULL);
994 if (session->transmit_handle != NULL)
996 if (NULL == (pm = session->pending_messages_head))
999 session->transmit_handle =
1000 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
1001 GNUNET_TIME_absolute_get_remaining
1002 (pm->timeout), &do_transmit,
1008 * Functions with this signature are called whenever we need
1009 * to close a session due to a disconnect or failure to
1010 * establish a connection.
1012 * @param session session to close down
1015 disconnect_session (struct Session *session)
1017 struct PendingMessage *pm;
1018 struct Plugin * plugin = session->plugin;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 "Disconnecting session of peer `%s' address `%s'\n",
1022 GNUNET_i2s (&session->target),
1023 tcp_address_to_string (NULL, session->addr, session->addrlen));
1025 stop_session_timeout (session);
1027 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap, &session->target, session))
1029 GNUNET_STATISTICS_update (session->plugin->env->stats,
1030 gettext_noop ("# TCP sessions active"), -1,
1032 dec_sessions (plugin, session, __LINE__);
1034 else GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &session->target, session));
1036 /* clean up state */
1037 if (session->transmit_handle != NULL)
1039 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1040 session->transmit_handle = NULL;
1042 session->plugin->env->session_end (session->plugin->env->cls,
1043 &session->target, session);
1045 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
1047 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1048 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1051 while (NULL != (pm = session->pending_messages_head))
1053 LOG (GNUNET_ERROR_TYPE_DEBUG,
1054 pm->transmit_cont !=
1055 NULL ? "Could not deliver message to `%4s'.\n" :
1056 "Could not deliver message to `%4s', notifying.\n",
1057 GNUNET_i2s (&session->target));
1058 GNUNET_STATISTICS_update (session->plugin->env->stats,
1059 gettext_noop ("# bytes currently in TCP buffers"),
1060 -(int64_t) pm->message_size, GNUNET_NO);
1061 GNUNET_STATISTICS_update (session->plugin->env->stats,
1063 ("# bytes discarded by TCP (disconnect)"),
1064 pm->message_size, GNUNET_NO);
1065 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1066 session->pending_messages_tail, pm);
1067 if (NULL != pm->transmit_cont)
1068 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1069 GNUNET_SYSERR, pm->message_size, 0);
1072 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1074 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1075 if (NULL != session->client)
1076 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1078 if (NULL != session->client)
1080 GNUNET_SERVER_client_disconnect (session->client);
1081 GNUNET_SERVER_client_drop (session->client);
1082 session->client = NULL;
1084 GNUNET_free_non_null (session->addr);
1085 GNUNET_assert (NULL == session->transmit_handle);
1086 GNUNET_free (session);
1090 struct FindSessionContext
1098 session_it (void *cls,
1099 const struct GNUNET_PeerIdentity * key,
1102 struct FindSessionContext *res = cls;
1104 if (res->s == value)
1106 res->res = GNUNET_OK;
1114 find_session (struct Plugin *plugin, struct Session *session)
1116 struct FindSessionContext session_map_res;
1117 struct FindSessionContext nat_map_res;
1119 session_map_res.s = session;
1120 session_map_res.res = GNUNET_SYSERR;
1121 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1123 nat_map_res.s = session;
1124 nat_map_res.res = GNUNET_SYSERR;
1125 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1127 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1130 return GNUNET_SYSERR;
1137 * Function that can be used by the transport service to transmit
1138 * a message using the plugin. Note that in the case of a
1139 * peer disconnecting, the continuation MUST be called
1140 * prior to the disconnect notification itself. This function
1141 * will be called with this peer's HELLO message to initiate
1142 * a fresh connection to another peer.
1144 * @param cls closure
1145 * @param session which session must be used
1146 * @param msgbuf the message to transmit
1147 * @param msgbuf_size number of bytes in 'msgbuf'
1148 * @param priority how important is the message (most plugins will
1149 * ignore message priority and just FIFO)
1150 * @param to how long to wait at most for the transmission (does not
1151 * require plugins to discard the message after the timeout,
1152 * just advisory for the desired delay; most plugins will ignore
1154 * @param cont continuation to call once the message has
1155 * been transmitted (or if the transport is ready
1156 * for the next transmission call; or if the
1157 * peer disconnected...); can be NULL
1158 * @param cont_cls closure for cont
1159 * @return number of bytes used (on the physical network, with overheads);
1160 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1161 * and does NOT mean that the message was not transmitted (DV)
1164 tcp_plugin_send (void *cls,
1165 struct Session *session,
1166 const char *msgbuf, size_t msgbuf_size,
1167 unsigned int priority,
1168 struct GNUNET_TIME_Relative to,
1169 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1171 struct Plugin * plugin = cls;
1172 struct PendingMessage *pm;
1174 GNUNET_assert (NULL != plugin);
1175 GNUNET_assert (NULL != session);
1177 if (GNUNET_SYSERR == find_session(plugin, session))
1179 LOG (GNUNET_ERROR_TYPE_ERROR,
1180 _("Trying to send with invalid session %p\n"));
1181 return GNUNET_SYSERR;
1184 /* create new message entry */
1185 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1186 pm->msg = (const char *) &pm[1];
1187 memcpy (&pm[1], msgbuf, msgbuf_size);
1188 pm->message_size = msgbuf_size;
1189 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1190 pm->transmit_cont = cont;
1191 pm->transmit_cont_cls = cont_cls;
1193 LOG (GNUNET_ERROR_TYPE_DEBUG,
1194 "Asked to transmit %u bytes to `%s', added message to list.\n",
1195 msgbuf_size, GNUNET_i2s (&session->target));
1197 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1201 GNUNET_assert (session->client != NULL);
1202 reschedule_session_timeout (session);
1203 GNUNET_SERVER_client_set_timeout (session->client,
1204 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1205 GNUNET_STATISTICS_update (plugin->env->stats,
1206 gettext_noop ("# bytes currently in TCP buffers"),
1207 msgbuf_size, GNUNET_NO);
1209 /* append pm to pending_messages list */
1210 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1211 session->pending_messages_tail, pm);
1213 process_pending_messages (session);
1216 else if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains_value(plugin->nat_wait_conns, &session->target, session))
1218 LOG (GNUNET_ERROR_TYPE_DEBUG,
1219 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1220 GNUNET_i2s (&session->target));
1221 reschedule_session_timeout (session);
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);
1233 LOG (GNUNET_ERROR_TYPE_ERROR,
1234 "Invalid session %p\n", session);
1236 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1239 return GNUNET_SYSERR; /* session does not exist here */
1248 struct Session *result;
1253 session_lookup_it (void *cls,
1254 const struct GNUNET_PeerIdentity *key,
1257 struct SessionItCtx * si_ctx = cls;
1258 struct Session * session = value;
1260 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1261 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1262 LOG (GNUNET_ERROR_TYPE_DEBUG,
1263 "Comparing: %s %u <-> %s %u\n",
1271 if (session->addrlen != si_ctx->addrlen)
1275 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1280 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1281 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1282 LOG (GNUNET_ERROR_TYPE_DEBUG,
1283 "Comparing: %s %u <-> %s %u , OK!\n",
1291 /* Found existing session */
1292 si_ctx->result = session;
1298 * Task cleaning up a NAT connection attempt after timeout
1301 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1303 struct Session *session = cls;
1305 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1308 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1309 disconnect_session (session);
1314 * Create a new session to transmit data to the target
1315 * This session will used to send data to this peer and the plugin will
1316 * notify us by calling the env->session_end function
1318 * @param cls closure
1319 * @param address pointer to the GNUNET_HELLO_Address
1320 * @return the session if the address is valid, NULL otherwise
1322 static struct Session *
1323 tcp_plugin_get_session (void *cls,
1324 const struct GNUNET_HELLO_Address *address)
1326 struct Plugin *plugin = cls;
1327 struct Session *session = NULL;
1331 struct GNUNET_CONNECTION_Handle *sa;
1332 struct sockaddr_in a4;
1333 struct sockaddr_in6 a6;
1334 const struct IPv4TcpAddress *t4;
1335 const struct IPv6TcpAddress *t6;
1336 struct GNUNET_ATS_Information ats;
1337 unsigned int is_natd = GNUNET_NO;
1340 GNUNET_assert (plugin != NULL);
1341 GNUNET_assert (address != NULL);
1342 addrlen = address->address_length;
1343 LOG (GNUNET_ERROR_TYPE_DEBUG,
1344 "Trying to get session for `%s' address of peer `%s'\n",
1345 tcp_address_to_string(NULL, address->address, address->address_length),
1346 GNUNET_i2s (&address->peer));
1348 /* look for existing session */
1350 GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1353 struct SessionItCtx si_ctx;
1355 si_ctx.addr = (void *) address->address;
1356 si_ctx.addrlen = address->address_length;
1358 si_ctx.result = NULL;
1360 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1362 &session_lookup_it, &si_ctx);
1363 if (si_ctx.result != NULL)
1365 session = si_ctx.result;
1366 LOG (GNUNET_ERROR_TYPE_DEBUG,
1367 "Found existing session for `%s' address `%s' session %p\n",
1368 GNUNET_i2s (&address->peer),
1369 tcp_address_to_string(NULL, address->address, address->address_length),
1373 LOG (GNUNET_ERROR_TYPE_DEBUG,
1374 "Existing sessions did not match address `%s' or peer `%s'\n",
1375 tcp_address_to_string(NULL, address->address, address->address_length),
1376 GNUNET_i2s (&address->peer));
1379 if (addrlen == sizeof (struct IPv6TcpAddress))
1381 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1382 t6 = address->address;
1384 memset (&a6, 0, sizeof (a6));
1385 #if HAVE_SOCKADDR_IN_SIN_LEN
1386 a6.sin6_len = sizeof (a6);
1388 a6.sin6_family = AF_INET6;
1389 a6.sin6_port = t6->t6_port;
1390 if (t6->t6_port == 0)
1391 is_natd = GNUNET_YES;
1392 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1396 else if (addrlen == sizeof (struct IPv4TcpAddress))
1398 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1399 t4 = address->address;
1401 memset (&a4, 0, sizeof (a4));
1402 #if HAVE_SOCKADDR_IN_SIN_LEN
1403 a4.sin_len = sizeof (a4);
1405 a4.sin_family = AF_INET;
1406 a4.sin_port = t4->t4_port;
1407 if (t4->t4_port == 0)
1408 is_natd = GNUNET_YES;
1409 a4.sin_addr.s_addr = t4->ipv4_addr;
1415 GNUNET_STATISTICS_update (plugin->env->stats,
1417 ("# requests to create session with invalid address"),
1422 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1424 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1426 /* NAT client only works with IPv4 addresses */
1430 if (plugin->cur_connections >= plugin->max_connections)
1436 if ((is_natd == GNUNET_YES) &&
1438 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1441 /* Only do one NAT punch attempt per peer identity */
1445 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1447 GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1450 LOG (GNUNET_ERROR_TYPE_DEBUG,
1451 "Found valid IPv4 NAT address (creating session)!\n") ;
1452 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1453 session->addrlen = 0;
1454 session->addr = NULL;
1455 session->ats_address_network_type = ats.value;
1456 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1457 &nat_connect_timeout,
1459 GNUNET_assert (session != NULL);
1460 GNUNET_assert (GNUNET_OK ==
1461 GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1466 LOG (GNUNET_ERROR_TYPE_DEBUG,
1467 "Created NAT WAIT connection to `%4s' at `%s'\n",
1468 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1470 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1474 LOG (GNUNET_ERROR_TYPE_DEBUG,
1475 "Running NAT client for `%4s' at `%s' failed\n",
1476 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1477 disconnect_session (session);
1482 /* create new outbound session */
1483 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1484 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1487 LOG (GNUNET_ERROR_TYPE_DEBUG,
1488 "Failed to create connection to `%4s' at `%s'\n",
1489 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1492 plugin->cur_connections++;
1493 if (plugin->cur_connections == plugin->max_connections)
1494 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1496 LOG (GNUNET_ERROR_TYPE_DEBUG,
1497 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1498 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1500 session = create_session (plugin,
1502 GNUNET_SERVER_connect_socket (plugin->server, sa),
1504 session->addr = GNUNET_malloc (addrlen);
1505 memcpy (session->addr, address->address, addrlen);
1506 session->addrlen = addrlen;
1507 session->ats_address_network_type = ats.value;
1509 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1511 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1512 inc_sessions (plugin, session, __LINE__);
1513 LOG (GNUNET_ERROR_TYPE_DEBUG,
1514 "Creating new session for `%s' address `%s' session %p\n",
1515 GNUNET_i2s (&address->peer),
1516 tcp_address_to_string(NULL, address->address, address->address_length),
1518 /* Send TCP Welcome */
1519 process_pending_messages (session);
1526 session_disconnect_it (void *cls,
1527 const struct GNUNET_PeerIdentity *key,
1530 struct Session *session = value;
1532 GNUNET_STATISTICS_update (session->plugin->env->stats,
1534 ("# transport-service disconnect requests for TCP"),
1536 disconnect_session (session);
1542 * Function that can be called to force a disconnect from the
1543 * specified neighbour. This should also cancel all previously
1544 * scheduled transmissions. Obviously the transmission may have been
1545 * partially completed already, which is OK. The plugin is supposed
1546 * to close the connection (if applicable) and no longer call the
1547 * transmit continuation(s).
1549 * Finally, plugin MUST NOT call the services's receive function to
1550 * notify the service that the connection to the specified target was
1551 * closed after a getting this call.
1553 * @param cls closure
1554 * @param target peer for which the last transmission is
1558 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1560 struct Plugin *plugin = cls;
1562 LOG (GNUNET_ERROR_TYPE_DEBUG,
1563 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1564 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1565 &session_disconnect_it, plugin);
1566 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1567 &session_disconnect_it, plugin);
1572 * Running pretty printers: head
1574 static struct PrettyPrinterContext *ppc_dll_head;
1577 * Running pretty printers: tail
1579 static struct PrettyPrinterContext *ppc_dll_tail;
1582 * Context for address to string conversion.
1584 struct PrettyPrinterContext
1589 struct PrettyPrinterContext *next;
1594 struct PrettyPrinterContext *prev;
1599 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1604 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1607 * Function to call with the result.
1609 GNUNET_TRANSPORT_AddressStringCallback asc;
1612 * Clsoure for 'asc'.
1617 * Port to add after the IP address.
1635 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1638 struct PrettyPrinterContext *ppc = cls;
1639 struct PrettyPrinterContext *cur;
1640 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1646 // GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cancel request %p, %u pending\n", ppc, count);
1647 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1648 if (NULL != ppc->resolver_handle)
1650 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1651 ppc->resolver_handle = NULL;
1654 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1660 * Append our port and forward the result.
1662 * @param cls the 'struct PrettyPrinterContext*'
1663 * @param hostname hostname part of the address
1666 append_port (void *cls, const char *hostname)
1668 struct PrettyPrinterContext *ppc = cls;
1669 struct PrettyPrinterContext *cur;
1673 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1678 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Callback request %p == %s, %u pending\n", ppc, hostname, count);
1679 if (hostname == NULL)
1681 ppc->asc (ppc->asc_cls, NULL);
1682 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1683 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1684 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1685 ppc->resolver_handle = NULL;
1686 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Done request %p, %u pending\n", ppc, count);
1690 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
1701 if (GNUNET_YES == ppc->ipv6)
1702 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1704 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1705 ppc->asc (ppc->asc_cls, ret);
1711 * Convert the transports address to a nice, human-readable
1714 * @param cls closure
1715 * @param type name of the transport that generated the address
1716 * @param addr one of the addresses of the host, NULL for the last address
1717 * the specific address format depends on the transport
1718 * @param addrlen length of the address
1719 * @param numeric should (IP) addresses be displayed in numeric form?
1720 * @param timeout after how long should we give up?
1721 * @param asc function to call on each string
1722 * @param asc_cls closure for asc
1725 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1726 const void *addr, size_t addrlen,
1728 struct GNUNET_TIME_Relative timeout,
1729 GNUNET_TRANSPORT_AddressStringCallback asc,
1732 struct PrettyPrinterContext *ppc;
1735 struct sockaddr_in a4;
1736 struct sockaddr_in6 a6;
1737 const struct IPv4TcpAddress *t4;
1738 const struct IPv6TcpAddress *t6;
1742 if (addrlen == sizeof (struct IPv6TcpAddress))
1745 memset (&a6, 0, sizeof (a6));
1746 a6.sin6_family = AF_INET6;
1747 a6.sin6_port = t6->t6_port;
1748 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1749 port = ntohs (t6->t6_port);
1750 options = ntohl (t6->options);
1754 else if (addrlen == sizeof (struct IPv4TcpAddress))
1757 memset (&a4, 0, sizeof (a4));
1758 a4.sin_family = AF_INET;
1759 a4.sin_port = t4->t4_port;
1760 a4.sin_addr.s_addr = t4->ipv4_addr;
1761 port = ntohs (t4->t4_port);
1762 options = ntohl (t4->options);
1766 else if (0 == addrlen)
1768 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1769 asc (asc_cls, NULL);
1774 /* invalid address */
1775 GNUNET_break_op (0);
1776 asc (asc_cls, NULL);
1779 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1780 if (addrlen == sizeof (struct IPv6TcpAddress))
1781 ppc->ipv6 = GNUNET_YES;
1783 ppc->ipv6 = GNUNET_NO;
1785 ppc->asc_cls = asc_cls;
1787 ppc->options = options;
1788 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
1789 &ppc_cancel_task, ppc);
1790 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1791 timeout, &append_port, ppc);
1792 if (NULL != ppc->resolver_handle)
1794 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1795 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
1806 * Check if the given port is plausible (must be either our listen
1807 * port or our advertised port), or any port if we are behind NAT
1808 * and do not have a port open. If it is neither, we return
1811 * @param plugin global variables
1812 * @param in_port port number to check
1813 * @return GNUNET_OK if port is either open_port or adv_port
1816 check_port (struct Plugin *plugin, uint16_t in_port)
1818 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1820 return GNUNET_SYSERR;
1825 * Function that will be called to check if a binary address for this
1826 * plugin is well-formed and corresponds to an address for THIS peer
1827 * (as per our configuration). Naturally, if absolutely necessary,
1828 * plugins can be a bit conservative in their answer, but in general
1829 * plugins should make sure that the address does not redirect
1830 * traffic to a 3rd party that might try to man-in-the-middle our
1833 * @param cls closure, our 'struct Plugin*'
1834 * @param addr pointer to the address
1835 * @param addrlen length of addr
1836 * @return GNUNET_OK if this is a plausible address for this peer
1837 * and transport, GNUNET_SYSERR if not
1840 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1842 struct Plugin *plugin = cls;
1843 struct IPv4TcpAddress *v4;
1844 struct IPv6TcpAddress *v6;
1846 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1847 (addrlen != sizeof (struct IPv6TcpAddress)))
1851 return GNUNET_SYSERR;
1854 if (addrlen == sizeof (struct IPv4TcpAddress))
1856 v4 = (struct IPv4TcpAddress *) addr;
1857 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions)))
1860 return GNUNET_SYSERR;
1862 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1863 return GNUNET_SYSERR;
1865 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1866 sizeof (struct in_addr)))
1867 return GNUNET_SYSERR;
1871 v6 = (struct IPv6TcpAddress *) addr;
1872 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1874 GNUNET_break_op (0);
1875 return GNUNET_SYSERR;
1877 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions)))
1880 return GNUNET_SYSERR;
1882 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1883 return GNUNET_SYSERR;
1885 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1886 sizeof (struct in6_addr)))
1887 return GNUNET_SYSERR;
1894 * We've received a nat probe from this peer via TCP. Finish
1895 * creating the client session and resume sending of queued
1898 * @param cls closure
1899 * @param client identification of the client
1900 * @param message the actual message
1903 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1904 const struct GNUNET_MessageHeader *message)
1906 struct Plugin *plugin = cls;
1907 struct Session *session;
1908 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1911 struct IPv4TcpAddress *t4;
1912 struct IPv6TcpAddress *t6;
1913 const struct sockaddr_in *s4;
1914 const struct sockaddr_in6 *s6;
1916 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1918 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1919 * a connection to this peer by running gnunet-nat-client. This peer
1920 * received the punch message and now wants us to use the new connection
1921 * as the default for that peer. Do so and then send a WELCOME message
1922 * so we can really be connected!
1924 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1926 GNUNET_break_op (0);
1927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1931 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1933 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1934 sizeof (struct GNUNET_PeerIdentity)))
1936 /* refuse connections from ourselves */
1937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1942 GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
1945 if (session == NULL)
1947 LOG (GNUNET_ERROR_TYPE_DEBUG,
1948 "Did NOT find session for NAT probe!\n");
1949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1952 LOG (GNUNET_ERROR_TYPE_DEBUG,
1953 "Found session for NAT probe!\n");
1955 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1957 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1958 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1961 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1965 disconnect_session (session);
1968 GNUNET_assert (GNUNET_CONTAINER_multipeermap_remove
1969 (plugin->nat_wait_conns,
1970 &tcp_nat_probe->clientIdentity,
1971 session) == GNUNET_YES);
1972 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
1973 &session->target, session,
1974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1975 session->last_activity = GNUNET_TIME_absolute_get ();
1976 session->inbound = GNUNET_NO;
1977 LOG (GNUNET_ERROR_TYPE_DEBUG,
1978 "Found address `%s' for incoming connection\n",
1979 GNUNET_a2s (vaddr, alen));
1980 switch (((const struct sockaddr *) vaddr)->sa_family)
1984 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1986 t4->t4_port = s4->sin_port;
1987 t4->ipv4_addr = s4->sin_addr.s_addr;
1989 session->addrlen = sizeof (struct IPv4TcpAddress);
1993 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1995 t6->t6_port = s6->sin6_port;
1996 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1998 session->addrlen = sizeof (struct IPv6TcpAddress);
2001 GNUNET_break_op (0);
2002 LOG (GNUNET_ERROR_TYPE_DEBUG,
2003 "Bad address for incoming connection!\n");
2004 GNUNET_free (vaddr);
2005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2006 disconnect_session (session);
2009 GNUNET_free (vaddr);
2010 GNUNET_break (NULL == session->client);
2011 GNUNET_SERVER_client_keep (client);
2012 session->client = client;
2013 inc_sessions (plugin, session, __LINE__);
2014 GNUNET_STATISTICS_update (plugin->env->stats,
2015 gettext_noop ("# TCP sessions active"), 1,
2017 process_pending_messages (session);
2018 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2023 * We've received a welcome from this peer via TCP. Possibly create a
2024 * fresh client record and send back our welcome.
2026 * @param cls closure
2027 * @param client identification of the client
2028 * @param message the actual message
2031 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2032 const struct GNUNET_MessageHeader *message)
2034 struct Plugin *plugin = cls;
2035 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2036 struct Session *session;
2039 struct IPv4TcpAddress *t4;
2040 struct IPv6TcpAddress *t6;
2041 const struct sockaddr_in *s4;
2042 const struct sockaddr_in6 *s6;
2043 struct GNUNET_ATS_Information ats;
2046 memcmp (&wm->clientIdentity, plugin->env->my_identity,
2047 sizeof (struct GNUNET_PeerIdentity)))
2049 /* refuse connections from ourselves */
2050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2051 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2053 LOG (GNUNET_ERROR_TYPE_WARNING,
2054 "Received %s message from my own identity `%4s' on address `%s'\n",
2055 "WELCOME", GNUNET_i2s (&wm->clientIdentity), GNUNET_a2s (vaddr, alen));
2056 GNUNET_free (vaddr);
2058 GNUNET_break_op (0);
2061 LOG (GNUNET_ERROR_TYPE_DEBUG,
2062 "Received %s message from `%4s' %p\n", "WELCOME",
2063 GNUNET_i2s (&wm->clientIdentity), client);
2064 GNUNET_STATISTICS_update (plugin->env->stats,
2065 gettext_noop ("# TCP WELCOME messages received"), 1,
2067 session = lookup_session_by_client (plugin, client);
2068 if (session != NULL)
2070 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2072 LOG (GNUNET_ERROR_TYPE_DEBUG,
2073 "Found existing session %p for peer `%s'\n",
2075 GNUNET_a2s (vaddr, alen));
2076 GNUNET_free (vaddr);
2081 GNUNET_SERVER_client_keep (client);
2082 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
2083 plugin->cur_connections++;
2084 if (plugin->cur_connections == plugin->max_connections)
2085 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2087 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
2088 session->inbound = GNUNET_YES;
2089 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2091 if (alen == sizeof (struct sockaddr_in))
2094 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2095 t4->options = htonl (0);
2096 t4->t4_port = s4->sin_port;
2097 t4->ipv4_addr = s4->sin_addr.s_addr;
2099 session->addrlen = sizeof (struct IPv4TcpAddress);
2101 else if (alen == sizeof (struct sockaddr_in6))
2104 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2105 t6->options = htonl (0);
2106 t6->t6_port = s6->sin6_port;
2107 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2109 session->addrlen = sizeof (struct IPv6TcpAddress);
2112 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
2113 session->ats_address_network_type = ats.value;
2114 LOG (GNUNET_ERROR_TYPE_DEBUG,
2115 "Creating new session %p for peer `%s'\n",
2117 GNUNET_a2s (vaddr, alen));
2118 GNUNET_free (vaddr);
2119 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap,
2122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2123 inc_sessions (plugin, session, __LINE__);
2127 LOG (GNUNET_ERROR_TYPE_DEBUG,
2128 "Did not obtain TCP socket address for incoming connection\n");
2133 if (session->expecting_welcome != GNUNET_YES)
2135 GNUNET_break_op (0);
2136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2140 session->last_activity = GNUNET_TIME_absolute_get ();
2141 session->expecting_welcome = GNUNET_NO;
2143 /* Notify transport and ATS about new session */
2144 if (GNUNET_YES == session->inbound)
2146 plugin->env->session_start (NULL, &wm->clientIdentity, PLUGIN_NAME,
2147 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2148 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2152 process_pending_messages (session);
2154 GNUNET_SERVER_client_set_timeout (client,
2155 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2156 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2161 * Task to signal the server that we can continue
2162 * receiving from the TCP client now.
2164 * @param cls the 'struct Session*'
2165 * @param tc task context (unused)
2168 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2170 struct Session *session = cls;
2172 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2173 reschedule_session_timeout (session);
2175 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2180 * We've received data for this peer via TCP. Unbox,
2181 * compute latency and forward.
2183 * @param cls closure
2184 * @param client identification of the client
2185 * @param message the actual message
2188 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2189 const struct GNUNET_MessageHeader *message)
2191 struct Plugin *plugin = cls;
2192 struct Session *session;
2193 struct GNUNET_TIME_Relative delay;
2196 type = ntohs (message->type);
2197 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2198 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2200 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2204 session = lookup_session_by_client (plugin, client);
2205 if (NULL == session)
2207 /* No inbound session found */
2211 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2212 LOG (GNUNET_ERROR_TYPE_ERROR,
2213 "Received unexpected %u bytes of type %u from `%s'\n",
2214 (unsigned int) ntohs (message->size),
2215 (unsigned int) ntohs (message->type),
2216 GNUNET_a2s(vaddr, alen));
2217 GNUNET_break_op (0);
2218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2219 GNUNET_free_non_null(vaddr);
2222 else if (GNUNET_YES == session->expecting_welcome)
2224 /* Session is expecting WELCOME message */
2228 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2229 LOG (GNUNET_ERROR_TYPE_ERROR,
2230 "Received unexpected %u bytes of type %u from `%s'\n",
2231 (unsigned int) ntohs (message->size),
2232 (unsigned int) ntohs (message->type),
2233 GNUNET_a2s(vaddr, alen));
2234 GNUNET_break_op (0);
2235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2236 GNUNET_free_non_null(vaddr);
2240 session->last_activity = GNUNET_TIME_absolute_get ();
2241 LOG (GNUNET_ERROR_TYPE_DEBUG,
2242 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2243 (unsigned int) ntohs (message->size),
2244 (unsigned int) ntohs (message->type),
2245 GNUNET_i2s (&session->target));
2247 GNUNET_STATISTICS_update (plugin->env->stats,
2248 gettext_noop ("# bytes received via TCP"),
2249 ntohs (message->size), GNUNET_NO);
2250 struct GNUNET_ATS_Information distance;
2252 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2253 distance.value = session->ats_address_network_type;
2254 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2256 GNUNET_assert (GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2260 delay = plugin->env->receive (plugin->env->cls,
2264 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2265 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2266 plugin->env->update_address_metrics (plugin->env->cls,
2268 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2269 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2270 session, &distance, 1);
2272 reschedule_session_timeout (session);
2274 if (0 == delay.rel_value_us)
2276 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2280 LOG (GNUNET_ERROR_TYPE_DEBUG,
2281 "Throttling receiving from `%s' for %s\n",
2282 GNUNET_i2s (&session->target),
2283 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2284 GNUNET_SERVER_disable_receive_done_warning (client);
2285 session->receive_delay_task =
2286 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2292 * Functions with this signature are called whenever a peer
2293 * is disconnected on the network level.
2295 * @param cls closure
2296 * @param client identification of the client
2299 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2301 struct Plugin *plugin = cls;
2302 struct Session *session;
2306 session = lookup_session_by_client (plugin, client);
2307 if (session == NULL)
2308 return; /* unknown, nothing to do */
2309 LOG (GNUNET_ERROR_TYPE_DEBUG,
2310 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2311 GNUNET_i2s (&session->target),
2313 NULL) ? tcp_address_to_string (session->plugin,
2318 if (plugin->cur_connections == plugin->max_connections)
2319 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2321 if (plugin->cur_connections < 1)
2324 plugin->cur_connections--;
2326 GNUNET_STATISTICS_update (session->plugin->env->stats,
2328 ("# network-level TCP disconnect events"), 1,
2330 disconnect_session (session);
2335 * We can now send a probe message, copy into buffer to really send.
2337 * @param cls closure, a struct TCPProbeContext
2338 * @param size max size to copy
2339 * @param buf buffer to copy message to
2340 * @return number of bytes copied into buf
2343 notify_send_probe (void *cls, size_t size, void *buf)
2345 struct TCPProbeContext *tcp_probe_ctx = cls;
2346 struct Plugin *plugin = tcp_probe_ctx->plugin;
2349 tcp_probe_ctx->transmit_handle = NULL;
2350 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2354 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2355 GNUNET_free (tcp_probe_ctx);
2358 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2359 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2360 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2361 tcp_probe_ctx->sock);
2362 ret = sizeof (tcp_probe_ctx->message);
2363 GNUNET_free (tcp_probe_ctx);
2369 * Function called by the NAT subsystem suggesting another peer wants
2370 * to connect to us via connection reversal. Try to connect back to the
2373 * @param cls closure
2374 * @param addr address to try
2375 * @param addrlen number of bytes in addr
2378 try_connection_reversal (void *cls, const struct sockaddr *addr,
2381 struct Plugin *plugin = cls;
2382 struct GNUNET_CONNECTION_Handle *sock;
2383 struct TCPProbeContext *tcp_probe_ctx;
2386 * We have received an ICMP response, ostensibly from a peer
2387 * that wants to connect to us! Send a message to establish a connection.
2389 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2392 /* failed for some odd reason (out of sockets?); ignore attempt */
2396 /* FIXME: do we need to track these probe context objects so that
2397 * we can clean them up on plugin unload? */
2398 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2399 tcp_probe_ctx->message.header.size =
2400 htons (sizeof (struct TCP_NAT_ProbeMessage));
2401 tcp_probe_ctx->message.header.type =
2402 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2403 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2404 sizeof (struct GNUNET_PeerIdentity));
2405 tcp_probe_ctx->plugin = plugin;
2406 tcp_probe_ctx->sock = sock;
2407 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2409 tcp_probe_ctx->transmit_handle =
2410 GNUNET_CONNECTION_notify_transmit_ready (sock,
2411 ntohs (tcp_probe_ctx->
2412 message.header.size),
2413 GNUNET_TIME_UNIT_FOREVER_REL,
2421 * Session was idle, so disconnect it
2424 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2426 GNUNET_assert (NULL != cls);
2427 struct Session *s = cls;
2429 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2431 "Session %p was idle for %s, disconnecting\n",
2433 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2435 /* call session destroy function */
2436 disconnect_session(s);
2441 * Start session timeout
2444 start_session_timeout (struct Session *s)
2446 GNUNET_assert (NULL != s);
2447 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2448 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Timeout for session %p set to %s\n",
2454 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2460 * Increment session timeout due to activity
2463 reschedule_session_timeout (struct Session *s)
2465 GNUNET_assert (NULL != s);
2466 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2468 GNUNET_SCHEDULER_cancel (s->timeout_task);
2469 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 "Timeout rescheduled for session %p set to %s\n",
2475 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2484 stop_session_timeout (struct Session *s)
2486 GNUNET_assert (NULL != s);
2488 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2490 GNUNET_SCHEDULER_cancel (s->timeout_task);
2491 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2493 "Timeout stopped for session %p canceled\n",
2499 * Function obtain the network type for a session
2501 * @param cls closure ('struct Plugin*')
2502 * @param session the session
2503 * @return the network type in HBO or GNUNET_SYSERR
2505 static enum GNUNET_ATS_Network_Type
2506 tcp_get_network (void *cls,
2507 struct Session *session)
2509 GNUNET_assert (NULL != session);
2510 return ntohl (session->ats_address_network_type);
2515 * Entry point for the plugin.
2517 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2518 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2521 libgnunet_plugin_transport_tcp_init (void *cls)
2523 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2524 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2525 sizeof (struct WelcomeMessage)},
2526 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2527 sizeof (struct TCP_NAT_ProbeMessage)},
2528 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2531 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2532 struct GNUNET_TRANSPORT_PluginFunctions *api;
2533 struct Plugin *plugin;
2534 struct GNUNET_SERVICE_Context *service;
2535 unsigned long long aport;
2536 unsigned long long bport;
2537 unsigned long long max_connections;
2539 struct GNUNET_TIME_Relative idle_timeout;
2542 struct sockaddr **addrs;
2543 socklen_t *addrlens;
2546 if (NULL == env->receive)
2548 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2549 initialze the plugin or the API */
2550 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2552 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2553 api->address_to_string = &tcp_address_to_string;
2554 api->string_to_address = &tcp_string_to_address;
2558 GNUNET_assert (NULL != env->cfg);
2560 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2563 max_connections = 128;
2567 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2568 &bport)) || (bport > 65535) ||
2570 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2571 "ADVERTISED-PORT", &aport)) &&
2574 LOG (GNUNET_ERROR_TYPE_ERROR,
2576 ("Require valid port number for service `%s' in configuration!\n"),
2586 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2587 if (service == NULL)
2589 LOG (GNUNET_ERROR_TYPE_WARNING,
2590 _("Failed to start service.\n"));
2597 /* Initialize my flags */
2600 plugin = GNUNET_malloc (sizeof (struct Plugin));
2601 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections, GNUNET_YES);
2602 plugin->max_connections = max_connections;
2603 plugin->cur_connections = 0;
2604 plugin->open_port = bport;
2605 plugin->adv_port = aport;
2607 plugin->lsock = NULL;
2608 if ((service != NULL) &&
2611 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2614 for (ret = ret_s-1; ret >= 0; ret--)
2615 LOG (GNUNET_ERROR_TYPE_INFO,
2616 "Binding to address `%s'\n",
2617 GNUNET_a2s (addrs[ret], addrlens[ret]));
2619 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2620 (const struct sockaddr **) addrs, addrlens,
2621 &tcp_nat_port_map_callback,
2622 &try_connection_reversal, plugin);
2623 for (ret = ret_s -1; ret >= 0; ret--)
2625 GNUNET_assert (addrs[ret] != NULL);
2626 GNUNET_free (addrs[ret]);
2628 GNUNET_free_non_null (addrs);
2629 GNUNET_free_non_null (addrlens);
2633 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2634 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2635 &try_connection_reversal, plugin);
2637 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2639 api->send = &tcp_plugin_send;
2640 api->get_session = &tcp_plugin_get_session;
2642 api->disconnect = &tcp_plugin_disconnect;
2643 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2644 api->check_address = &tcp_plugin_check_address;
2645 api->address_to_string = &tcp_address_to_string;
2646 api->string_to_address = &tcp_string_to_address;
2647 api->get_network = &tcp_get_network;
2648 plugin->service = service;
2649 if (service != NULL)
2651 plugin->server = GNUNET_SERVICE_get_server (service);
2656 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2657 "TIMEOUT", &idle_timeout))
2659 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2660 "transport-tcp", "TIMEOUT");
2661 if (plugin->nat != NULL)
2662 GNUNET_NAT_unregister (plugin->nat);
2663 GNUNET_free (plugin);
2668 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2669 NULL, idle_timeout, GNUNET_YES);
2671 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2672 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2674 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2676 plugin->handlers[i].callback_cls = plugin;
2678 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2679 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2680 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16, GNUNET_YES);
2682 LOG (GNUNET_ERROR_TYPE_INFO,
2683 _("TCP transport listening on port %llu\n"), bport);
2685 LOG (GNUNET_ERROR_TYPE_INFO,
2687 ("TCP transport not listening on any port (client only)\n"));
2689 LOG (GNUNET_ERROR_TYPE_INFO,
2691 ("TCP transport advertises itself as being on port %llu\n"),
2693 /* Initially set connections to 0 */
2694 GNUNET_assert (NULL != plugin->env->stats);
2695 GNUNET_STATISTICS_set (plugin->env->stats,
2696 gettext_noop ("# TCP sessions active"), 0,
2703 * Exit point from the plugin.
2706 libgnunet_plugin_transport_tcp_done (void *cls)
2708 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2709 struct Plugin *plugin = api->cls;
2710 struct TCPProbeContext *tcp_probe;
2711 struct PrettyPrinterContext *cur;
2712 struct PrettyPrinterContext *next;
2719 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2721 /* Removing leftover sessions */
2722 GNUNET_CONTAINER_multipeermap_iterate(plugin->sessionmap, &session_disconnect_it, NULL);
2723 /* Removing leftover NAT sessions */
2724 GNUNET_CONTAINER_multipeermap_iterate(plugin->nat_wait_conns, &session_disconnect_it, NULL);
2726 next = ppc_dll_head;
2727 for (cur = next; NULL != cur; cur = next)
2730 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
2731 if (NULL != cur->resolver_handle)
2732 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2733 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2738 if (plugin->service != NULL)
2739 GNUNET_SERVICE_stop (plugin->service);
2741 GNUNET_SERVER_destroy (plugin->server);
2742 GNUNET_free (plugin->handlers);
2743 if (plugin->nat != NULL)
2744 GNUNET_NAT_unregister (plugin->nat);
2745 while (NULL != (tcp_probe = plugin->probe_head))
2747 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2749 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2750 GNUNET_free (tcp_probe);
2752 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2753 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2754 GNUNET_free (plugin);
2759 /* end of plugin_transport_tcp.c */