2 This file is part of GNUnet
3 (C) 2002--2012 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_MultiHashMap *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_MultiHashMap *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_multihashmap_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_multihashmap_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;
603 case sizeof (struct IPv6TcpAddress):
606 port = ntohs (t6->t6_port);
607 options = ntohl (t6->options);
608 memcpy (&a6, &t6->ipv6_addr, sizeof (a6));
611 case sizeof (struct IPv4TcpAddress):
614 port = ntohs (t4->t4_port);
615 options = ntohl (t4->options);
616 memcpy (&a4, &t4->ipv4_addr, sizeof (a4));
621 GNUNET_snprintf (rbuf, sizeof (rbuf), "%s",
622 TRANSPORT_SESSION_INBOUND_STRING);
626 LOG (GNUNET_ERROR_TYPE_WARNING,
627 _("Unexpected address length: %u bytes\n"),
628 (unsigned int) addrlen);
631 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
633 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
636 GNUNET_snprintf (rbuf, sizeof (rbuf), (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u",
637 PLUGIN_NAME, options, buf, port);
643 * Function called to convert a string address to
646 * @param cls closure ('struct Plugin*')
647 * @param addr string address
648 * @param addrlen length of the address
649 * @param buf location to store the buffer
650 * @param added location to store the number of bytes in the buffer.
651 * If the function returns GNUNET_SYSERR, its contents are undefined.
652 * @return GNUNET_OK on success, GNUNET_SYSERR on failure
655 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
656 void **buf, size_t *added)
658 struct sockaddr_storage socket_address;
664 /* Format tcp.options.address:port */
669 if ((NULL == addr) || (addrlen == 0))
672 return GNUNET_SYSERR;
674 if ('\0' != addr[addrlen - 1])
677 return GNUNET_SYSERR;
679 if (strlen (addr) != addrlen - 1)
682 return GNUNET_SYSERR;
684 plugin = GNUNET_strdup (addr);
685 optionstr = strchr (plugin, '.');
686 if (NULL == optionstr)
689 GNUNET_free (plugin);
690 return GNUNET_SYSERR;
694 options = atol (optionstr);
695 address = strchr (optionstr, '.');
699 GNUNET_free (plugin);
700 return GNUNET_SYSERR;
706 GNUNET_STRINGS_to_address_ip (address, strlen (address),
710 GNUNET_free (plugin);
711 return GNUNET_SYSERR;
714 GNUNET_free (plugin);
715 switch (socket_address.ss_family)
719 struct IPv4TcpAddress *t4;
720 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
721 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
722 t4->options = htonl (options);
723 t4->ipv4_addr = in4->sin_addr.s_addr;
724 t4->t4_port = in4->sin_port;
726 *added = sizeof (struct IPv4TcpAddress);
731 struct IPv6TcpAddress *t6;
732 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
733 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
734 t6->options = htonl (options);
735 t6->ipv6_addr = in6->sin6_addr;
736 t6->t6_port = in6->sin6_port;
738 *added = sizeof (struct IPv6TcpAddress);
742 return GNUNET_SYSERR;
747 struct SessionClientCtx
749 const struct GNUNET_SERVER_Client *client;
755 session_lookup_by_client_it (void *cls,
756 const struct GNUNET_HashCode * key,
759 struct SessionClientCtx *sc_ctx = cls;
760 struct Session *s = value;
762 if (s->client == sc_ctx->client)
772 * Find the session handle for the given client.
774 * @param plugin the plugin
775 * @param client which client to find the session handle for
776 * @return NULL if no matching session exists
778 static struct Session *
779 lookup_session_by_client (struct Plugin *plugin,
780 const struct GNUNET_SERVER_Client *client)
782 struct SessionClientCtx sc_ctx;
784 sc_ctx.client = client;
786 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_lookup_by_client_it, &sc_ctx);
792 * Create a new session. Also queues a welcome message.
794 * @param plugin the plugin
795 * @param target peer to connect to
796 * @param client client to use, reference counter must have already been increased
797 * @param is_nat this a NAT session, we should wait for a client to
798 * connect to us from an address, then assign that to
800 * @return new session object
802 static struct Session *
803 create_session (struct Plugin *plugin, const struct GNUNET_PeerIdentity *target,
804 struct GNUNET_SERVER_Client *client, int is_nat)
806 struct Session *session;
807 struct PendingMessage *pm;
808 struct WelcomeMessage welcome;
810 if (GNUNET_YES != is_nat)
811 GNUNET_assert (NULL != client);
813 GNUNET_assert (NULL == client);
815 LOG (GNUNET_ERROR_TYPE_DEBUG,
816 "Creating new session for peer `%4s'\n",
817 GNUNET_i2s (target));
818 session = GNUNET_malloc (sizeof (struct Session));
819 session->last_activity = GNUNET_TIME_absolute_get ();
820 session->plugin = plugin;
821 session->is_nat = is_nat;
822 session->client = client;
823 session->target = *target;
824 session->expecting_welcome = GNUNET_YES;
825 session->ats_address_network_type = htonl (GNUNET_ATS_NET_UNSPECIFIED);
826 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
827 sizeof (struct WelcomeMessage));
828 pm->msg = (const char *) &pm[1];
829 pm->message_size = sizeof (struct WelcomeMessage);
830 welcome.header.size = htons (sizeof (struct WelcomeMessage));
831 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
832 welcome.clientIdentity = *plugin->env->my_identity;
833 memcpy (&pm[1], &welcome, sizeof (welcome));
834 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
835 GNUNET_STATISTICS_update (plugin->env->stats,
836 gettext_noop ("# bytes currently in TCP buffers"),
837 pm->message_size, GNUNET_NO);
838 GNUNET_CONTAINER_DLL_insert (session->pending_messages_head,
839 session->pending_messages_tail, pm);
840 if (GNUNET_YES != is_nat)
842 GNUNET_STATISTICS_update (plugin->env->stats,
843 gettext_noop ("# TCP sessions active"), 1,
846 start_session_timeout (session);
853 * If we have pending messages, ask the server to
854 * transmit them (schedule the respective tasks, etc.)
856 * @param session for which session should we do this
859 process_pending_messages (struct Session *session);
863 * Function called to notify a client about the socket
864 * being ready to queue more data. "buf" will be
865 * NULL and "size" zero if the socket was closed for
866 * writing in the meantime.
869 * @param size number of bytes available in buf
870 * @param buf where the callee should write the message
871 * @return number of bytes written to buf
874 do_transmit (void *cls, size_t size, void *buf)
876 struct Session *session = cls;
877 struct GNUNET_PeerIdentity pid;
878 struct Plugin *plugin;
879 struct PendingMessage *pos;
880 struct PendingMessage *hd;
881 struct PendingMessage *tl;
882 struct GNUNET_TIME_Absolute now;
886 GNUNET_assert (NULL != session);
887 session->transmit_handle = NULL;
888 plugin = session->plugin;
891 LOG (GNUNET_ERROR_TYPE_DEBUG,
892 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
893 GNUNET_i2s (&session->target));
894 /* timeout; cancel all messages that have already expired */
898 now = GNUNET_TIME_absolute_get ();
899 while ((NULL != (pos = session->pending_messages_head)) &&
900 (pos->timeout.abs_value <= now.abs_value))
902 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
903 session->pending_messages_tail, pos);
904 LOG (GNUNET_ERROR_TYPE_DEBUG,
905 "Failed to transmit %u byte message to `%4s'.\n",
906 pos->message_size, GNUNET_i2s (&session->target));
907 ret += pos->message_size;
908 GNUNET_CONTAINER_DLL_insert_after (hd, tl, tl, pos);
910 /* do this call before callbacks (so that if callbacks destroy
911 * session, they have a chance to cancel actions done by this
913 process_pending_messages (session);
914 pid = session->target;
915 /* no do callbacks and do not use session again since
916 * the callbacks may abort the session */
917 while (NULL != (pos = hd))
919 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
920 if (pos->transmit_cont != NULL)
921 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR, pos->message_size, 0);
924 GNUNET_STATISTICS_update (plugin->env->stats,
925 gettext_noop ("# bytes currently in TCP buffers"),
926 -(int64_t) ret, GNUNET_NO);
927 GNUNET_STATISTICS_update (plugin->env->stats,
929 ("# bytes discarded by TCP (timeout)"), ret,
933 /* copy all pending messages that would fit */
938 while (NULL != (pos = session->pending_messages_head))
940 if (ret + pos->message_size > size)
942 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
943 session->pending_messages_tail, pos);
944 GNUNET_assert (size >= pos->message_size);
945 LOG (GNUNET_ERROR_TYPE_DEBUG,
946 "Transmitting message of type %u\n",
947 ntohs (((struct GNUNET_MessageHeader *) pos->msg)->type));
948 /* FIXME: this memcpy can be up to 7% of our total runtime */
949 memcpy (cbuf, pos->msg, pos->message_size);
950 cbuf += pos->message_size;
951 ret += pos->message_size;
952 size -= pos->message_size;
953 GNUNET_CONTAINER_DLL_insert_tail (hd, tl, pos);
955 /* schedule 'continuation' before callbacks so that callbacks that
956 * cancel everything don't cause us to use a session that no longer
958 process_pending_messages (session);
959 session->last_activity = GNUNET_TIME_absolute_get ();
960 pid = session->target;
961 /* we'll now call callbacks that may cancel the session; hence
962 * we should not use 'session' after this point */
963 while (NULL != (pos = hd))
965 GNUNET_CONTAINER_DLL_remove (hd, tl, pos);
966 if (pos->transmit_cont != NULL)
967 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK, pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
970 GNUNET_assert (hd == NULL);
971 GNUNET_assert (tl == NULL);
972 LOG (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n",
974 GNUNET_STATISTICS_update (plugin->env->stats,
975 gettext_noop ("# bytes currently in TCP buffers"),
976 -(int64_t) ret, GNUNET_NO);
977 GNUNET_STATISTICS_update (plugin->env->stats,
978 gettext_noop ("# bytes transmitted via TCP"), ret,
985 * If we have pending messages, ask the server to
986 * transmit them (schedule the respective tasks, etc.)
988 * @param session for which session should we do this
991 process_pending_messages (struct Session *session)
993 struct PendingMessage *pm;
995 GNUNET_assert (session->client != NULL);
996 if (session->transmit_handle != NULL)
998 if (NULL == (pm = session->pending_messages_head))
1001 session->transmit_handle =
1002 GNUNET_SERVER_notify_transmit_ready (session->client, pm->message_size,
1003 GNUNET_TIME_absolute_get_remaining
1004 (pm->timeout), &do_transmit,
1010 * Functions with this signature are called whenever we need
1011 * to close a session due to a disconnect or failure to
1012 * establish a connection.
1014 * @param session session to close down
1017 disconnect_session (struct Session *session)
1019 struct PendingMessage *pm;
1020 struct Plugin * plugin = session->plugin;
1022 LOG (GNUNET_ERROR_TYPE_DEBUG,
1023 "Disconnecting session of peer `%s' address `%s'\n",
1024 GNUNET_i2s (&session->target),
1025 tcp_address_to_string (NULL, session->addr, session->addrlen));
1027 stop_session_timeout (session);
1029 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->sessionmap, &session->target.hashPubKey, session))
1031 GNUNET_STATISTICS_update (session->plugin->env->stats,
1032 gettext_noop ("# TCP sessions active"), -1,
1034 dec_sessions (plugin, session, __LINE__);
1036 else GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (plugin->nat_wait_conns, &session->target.hashPubKey, session));
1038 /* clean up state */
1039 if (session->transmit_handle != NULL)
1041 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
1042 session->transmit_handle = NULL;
1044 session->plugin->env->session_end (session->plugin->env->cls,
1045 &session->target, session);
1047 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
1049 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1050 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1053 while (NULL != (pm = session->pending_messages_head))
1055 LOG (GNUNET_ERROR_TYPE_DEBUG,
1056 pm->transmit_cont !=
1057 NULL ? "Could not deliver message to `%4s'.\n" :
1058 "Could not deliver message to `%4s', notifying.\n",
1059 GNUNET_i2s (&session->target));
1060 GNUNET_STATISTICS_update (session->plugin->env->stats,
1061 gettext_noop ("# bytes currently in TCP buffers"),
1062 -(int64_t) pm->message_size, GNUNET_NO);
1063 GNUNET_STATISTICS_update (session->plugin->env->stats,
1065 ("# bytes discarded by TCP (disconnect)"),
1066 pm->message_size, GNUNET_NO);
1067 GNUNET_CONTAINER_DLL_remove (session->pending_messages_head,
1068 session->pending_messages_tail, pm);
1069 if (NULL != pm->transmit_cont)
1070 pm->transmit_cont (pm->transmit_cont_cls, &session->target,
1071 GNUNET_SYSERR, pm->message_size, 0);
1074 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK)
1076 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1077 if (NULL != session->client)
1078 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
1080 if (NULL != session->client)
1082 GNUNET_SERVER_client_disconnect (session->client);
1083 GNUNET_SERVER_client_drop (session->client);
1084 session->client = NULL;
1086 GNUNET_free_non_null (session->addr);
1087 GNUNET_assert (NULL == session->transmit_handle);
1088 GNUNET_free (session);
1092 struct FindSessionContext
1098 int session_it (void *cls,
1099 const struct GNUNET_HashCode * key,
1102 struct FindSessionContext *res = cls;
1103 if (res->s == value)
1105 res->res = GNUNET_OK;
1112 int find_session (struct Plugin *plugin, struct Session *session)
1114 struct FindSessionContext session_map_res;
1115 struct FindSessionContext nat_map_res;
1117 session_map_res.s = session;
1118 session_map_res.res = GNUNET_SYSERR;
1119 GNUNET_CONTAINER_multihashmap_iterate (plugin->sessionmap, &session_it, &session_map_res);
1121 nat_map_res.s = session;
1122 nat_map_res.res = GNUNET_SYSERR;
1123 GNUNET_CONTAINER_multihashmap_iterate (plugin->nat_wait_conns, &session_it, &nat_map_res);
1125 if ((session_map_res.res == GNUNET_SYSERR) && (nat_map_res.res == GNUNET_SYSERR))
1128 return GNUNET_SYSERR;
1135 * Function that can be used by the transport service to transmit
1136 * a message using the plugin. Note that in the case of a
1137 * peer disconnecting, the continuation MUST be called
1138 * prior to the disconnect notification itself. This function
1139 * will be called with this peer's HELLO message to initiate
1140 * a fresh connection to another peer.
1142 * @param cls closure
1143 * @param session which session must be used
1144 * @param msgbuf the message to transmit
1145 * @param msgbuf_size number of bytes in 'msgbuf'
1146 * @param priority how important is the message (most plugins will
1147 * ignore message priority and just FIFO)
1148 * @param to how long to wait at most for the transmission (does not
1149 * require plugins to discard the message after the timeout,
1150 * just advisory for the desired delay; most plugins will ignore
1152 * @param cont continuation to call once the message has
1153 * been transmitted (or if the transport is ready
1154 * for the next transmission call; or if the
1155 * peer disconnected...); can be NULL
1156 * @param cont_cls closure for cont
1157 * @return number of bytes used (on the physical network, with overheads);
1158 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1159 * and does NOT mean that the message was not transmitted (DV)
1162 tcp_plugin_send (void *cls,
1163 struct Session *session,
1164 const char *msgbuf, size_t msgbuf_size,
1165 unsigned int priority,
1166 struct GNUNET_TIME_Relative to,
1167 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1169 struct Plugin * plugin = cls;
1170 struct PendingMessage *pm;
1172 GNUNET_assert (NULL != plugin);
1173 GNUNET_assert (NULL != session);
1175 if (GNUNET_SYSERR == find_session(plugin, session))
1177 LOG (GNUNET_ERROR_TYPE_ERROR,
1178 _("Trying to send with invalid session %p\n"));
1179 return GNUNET_SYSERR;
1182 /* create new message entry */
1183 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1184 pm->msg = (const char *) &pm[1];
1185 memcpy (&pm[1], msgbuf, msgbuf_size);
1186 pm->message_size = msgbuf_size;
1187 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1188 pm->transmit_cont = cont;
1189 pm->transmit_cont_cls = cont_cls;
1191 LOG (GNUNET_ERROR_TYPE_DEBUG,
1192 "Asked to transmit %u bytes to `%s', added message to list.\n",
1193 msgbuf_size, GNUNET_i2s (&session->target));
1195 if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
1196 &session->target.hashPubKey,
1199 GNUNET_assert (session->client != NULL);
1200 reschedule_session_timeout (session);
1201 GNUNET_SERVER_client_set_timeout (session->client,
1202 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1203 GNUNET_STATISTICS_update (plugin->env->stats,
1204 gettext_noop ("# bytes currently in TCP buffers"),
1205 msgbuf_size, GNUNET_NO);
1207 /* append pm to pending_messages list */
1208 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1209 session->pending_messages_tail, pm);
1211 process_pending_messages (session);
1214 else if (GNUNET_YES == GNUNET_CONTAINER_multihashmap_contains_value(plugin->nat_wait_conns, &session->target.hashPubKey, session))
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1218 GNUNET_i2s (&session->target));
1219 reschedule_session_timeout (session);
1220 GNUNET_STATISTICS_update (plugin->env->stats,
1221 gettext_noop ("# bytes currently in TCP buffers"),
1222 msgbuf_size, GNUNET_NO);
1224 /* append pm to pending_messages list */
1225 GNUNET_CONTAINER_DLL_insert_tail (session->pending_messages_head,
1226 session->pending_messages_tail, pm);
1231 LOG (GNUNET_ERROR_TYPE_ERROR,
1232 "Invalid session %p\n", session);
1234 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1237 return GNUNET_SYSERR; /* session does not exist here */
1246 struct Session *result;
1251 session_lookup_it (void *cls,
1252 const struct GNUNET_HashCode *key,
1255 struct SessionItCtx * si_ctx = cls;
1256 struct Session * session = value;
1258 char * a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1259 char * a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1260 LOG (GNUNET_ERROR_TYPE_DEBUG,
1261 "Comparing: %s %u <-> %s %u\n",
1269 if (session->addrlen != si_ctx->addrlen)
1273 if (0 != memcmp (session->addr, si_ctx->addr, si_ctx->addrlen))
1278 a1 = strdup (tcp_address_to_string(NULL, session->addr, session->addrlen));
1279 a2 = strdup (tcp_address_to_string(NULL, si_ctx->addr, si_ctx->addrlen));
1280 LOG (GNUNET_ERROR_TYPE_DEBUG,
1281 "Comparing: %s %u <-> %s %u , OK!\n",
1289 /* Found existing session */
1290 si_ctx->result = session;
1296 * Task cleaning up a NAT connection attempt after timeout
1299 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1301 struct Session *session = cls;
1303 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1304 LOG (GNUNET_ERROR_TYPE_DEBUG,
1305 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1306 GNUNET_i2s (&session->target), tcp_address_to_string(NULL, session->addr, session->addrlen));
1307 disconnect_session (session);
1312 * Create a new session to transmit data to the target
1313 * This session will used to send data to this peer and the plugin will
1314 * notify us by calling the env->session_end function
1316 * @param cls closure
1317 * @param address pointer to the GNUNET_HELLO_Address
1318 * @return the session if the address is valid, NULL otherwise
1320 static struct Session *
1321 tcp_plugin_get_session (void *cls,
1322 const struct GNUNET_HELLO_Address *address)
1324 struct Plugin *plugin = cls;
1325 struct Session *session = NULL;
1329 struct GNUNET_CONNECTION_Handle *sa;
1330 struct sockaddr_in a4;
1331 struct sockaddr_in6 a6;
1332 const struct IPv4TcpAddress *t4;
1333 const struct IPv6TcpAddress *t6;
1334 struct GNUNET_ATS_Information ats;
1335 unsigned int is_natd = GNUNET_NO;
1338 GNUNET_assert (plugin != NULL);
1339 GNUNET_assert (address != NULL);
1340 addrlen = address->address_length;
1341 LOG (GNUNET_ERROR_TYPE_DEBUG,
1342 "Trying to get session for `%s' address of peer `%s'\n",
1343 tcp_address_to_string(NULL, address->address, address->address_length),
1344 GNUNET_i2s (&address->peer));
1346 /* look for existing session */
1348 GNUNET_CONTAINER_multihashmap_contains (plugin->sessionmap,
1349 &address->peer.hashPubKey))
1351 struct SessionItCtx si_ctx;
1353 si_ctx.addr = (void *) address->address;
1354 si_ctx.addrlen = address->address_length;
1356 si_ctx.result = NULL;
1358 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap,
1359 &address->peer.hashPubKey,
1360 &session_lookup_it, &si_ctx);
1361 if (si_ctx.result != NULL)
1363 session = si_ctx.result;
1364 LOG (GNUNET_ERROR_TYPE_DEBUG,
1365 "Found existing session for `%s' address `%s' session %p\n",
1366 GNUNET_i2s (&address->peer),
1367 tcp_address_to_string(NULL, address->address, address->address_length),
1371 LOG (GNUNET_ERROR_TYPE_DEBUG,
1372 "Existing sessions did not match address `%s' or peer `%s'\n",
1373 tcp_address_to_string(NULL, address->address, address->address_length),
1374 GNUNET_i2s (&address->peer));
1377 if (addrlen == sizeof (struct IPv6TcpAddress))
1379 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1380 t6 = address->address;
1382 memset (&a6, 0, sizeof (a6));
1383 #if HAVE_SOCKADDR_IN_SIN_LEN
1384 a6.sin6_len = sizeof (a6);
1386 a6.sin6_family = AF_INET6;
1387 a6.sin6_port = t6->t6_port;
1388 if (t6->t6_port == 0)
1389 is_natd = GNUNET_YES;
1390 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1394 else if (addrlen == sizeof (struct IPv4TcpAddress))
1396 GNUNET_assert (NULL != address->address); /* make static analysis happy */
1397 t4 = address->address;
1399 memset (&a4, 0, sizeof (a4));
1400 #if HAVE_SOCKADDR_IN_SIN_LEN
1401 a4.sin_len = sizeof (a4);
1403 a4.sin_family = AF_INET;
1404 a4.sin_port = t4->t4_port;
1405 if (t4->t4_port == 0)
1406 is_natd = GNUNET_YES;
1407 a4.sin_addr.s_addr = t4->ipv4_addr;
1413 GNUNET_STATISTICS_update (plugin->env->stats,
1415 ("# requests to create session with invalid address"),
1420 ats = plugin->env->get_address_type (plugin->env->cls, sb ,sbs);
1422 if ((is_natd == GNUNET_YES) && (addrlen == sizeof (struct IPv6TcpAddress)))
1424 /* NAT client only works with IPv4 addresses */
1428 if (plugin->cur_connections >= plugin->max_connections)
1434 if ((is_natd == GNUNET_YES) &&
1436 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1437 &address->peer.hashPubKey)))
1439 /* Only do one NAT punch attempt per peer identity */
1443 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1445 GNUNET_CONTAINER_multihashmap_contains (plugin->nat_wait_conns,
1446 &address->peer.hashPubKey)))
1448 LOG (GNUNET_ERROR_TYPE_DEBUG,
1449 "Found valid IPv4 NAT address (creating session)!\n") ;
1450 session = create_session (plugin, &address->peer, NULL, GNUNET_YES);
1451 session->addrlen = 0;
1452 session->addr = NULL;
1453 session->ats_address_network_type = ats.value;
1454 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1455 &nat_connect_timeout,
1457 GNUNET_assert (session != NULL);
1458 GNUNET_assert (GNUNET_OK ==
1459 GNUNET_CONTAINER_multihashmap_put (plugin->nat_wait_conns,
1460 &session->target.hashPubKey,
1462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1464 LOG (GNUNET_ERROR_TYPE_DEBUG,
1465 "Created NAT WAIT connection to `%4s' at `%s'\n",
1466 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1468 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1472 LOG (GNUNET_ERROR_TYPE_DEBUG,
1473 "Running NAT client for `%4s' at `%s' failed\n",
1474 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1475 disconnect_session (session);
1480 /* create new outbound session */
1481 GNUNET_assert (plugin->cur_connections <= plugin->max_connections);
1482 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1485 LOG (GNUNET_ERROR_TYPE_DEBUG,
1486 "Failed to create connection to `%4s' at `%s'\n",
1487 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1490 plugin->cur_connections++;
1491 if (plugin->cur_connections == plugin->max_connections)
1492 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1494 LOG (GNUNET_ERROR_TYPE_DEBUG,
1495 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1496 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1498 session = create_session (plugin,
1500 GNUNET_SERVER_connect_socket (plugin->server, sa),
1502 session->addr = GNUNET_malloc (addrlen);
1503 memcpy (session->addr, address->address, addrlen);
1504 session->addrlen = addrlen;
1505 session->ats_address_network_type = ats.value;
1507 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1508 &session->target.hashPubKey,
1509 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1510 inc_sessions (plugin, session, __LINE__);
1511 LOG (GNUNET_ERROR_TYPE_DEBUG,
1512 "Creating new session for `%s' address `%s' session %p\n",
1513 GNUNET_i2s (&address->peer),
1514 tcp_address_to_string(NULL, address->address, address->address_length),
1516 /* Send TCP Welcome */
1517 process_pending_messages (session);
1524 session_disconnect_it (void *cls,
1525 const struct GNUNET_HashCode * key,
1528 struct Session *session = value;
1530 GNUNET_STATISTICS_update (session->plugin->env->stats,
1532 ("# transport-service disconnect requests for TCP"),
1534 disconnect_session (session);
1540 * Function that can be called to force a disconnect from the
1541 * specified neighbour. This should also cancel all previously
1542 * scheduled transmissions. Obviously the transmission may have been
1543 * partially completed already, which is OK. The plugin is supposed
1544 * to close the connection (if applicable) and no longer call the
1545 * transmit continuation(s).
1547 * Finally, plugin MUST NOT call the services's receive function to
1548 * notify the service that the connection to the specified target was
1549 * closed after a getting this call.
1551 * @param cls closure
1552 * @param target peer for which the last transmission is
1556 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1558 struct Plugin *plugin = cls;
1560 LOG (GNUNET_ERROR_TYPE_DEBUG,
1561 "Disconnecting peer `%4s'\n", GNUNET_i2s (target));
1562 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->sessionmap, &target->hashPubKey, &session_disconnect_it, plugin);
1563 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->nat_wait_conns, &target->hashPubKey, &session_disconnect_it, plugin);
1568 * Running pretty printers: head
1570 static struct PrettyPrinterContext *ppc_dll_head;
1573 * Running pretty printers: tail
1575 static struct PrettyPrinterContext *ppc_dll_tail;
1578 * Context for address to string conversion.
1580 struct PrettyPrinterContext
1585 struct PrettyPrinterContext *next;
1590 struct PrettyPrinterContext *prev;
1595 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1600 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1603 * Function to call with the result.
1605 GNUNET_TRANSPORT_AddressStringCallback asc;
1608 * Clsoure for 'asc'.
1613 * Port to add after the IP address.
1630 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1632 struct PrettyPrinterContext *ppc = cls;
1633 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was not removed!\n", ppc); */
1634 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1635 if (NULL != ppc->resolver_handle)
1637 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1638 ppc->resolver_handle = NULL;
1641 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1647 * Append our port and forward the result.
1649 * @param cls the 'struct PrettyPrinterContext*'
1650 * @param hostname hostname part of the address
1653 append_port (void *cls, const char *hostname)
1655 struct PrettyPrinterContext *ppc = cls;
1656 struct PrettyPrinterContext *cur;
1658 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC callback: %p `%s'\n",ppc, hostname); */
1659 if (hostname == NULL)
1661 ppc->asc (ppc->asc_cls, NULL);
1662 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, ppc);
1663 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1664 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1665 ppc->resolver_handle = NULL;
1666 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was removed!\n", ppc); */
1670 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1677 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid callback for PPC %p \n", ppc);
1681 if (GNUNET_YES == ppc->ipv6)
1682 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1684 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname, ppc->port);
1685 ppc->asc (ppc->asc_cls, ret);
1691 * Convert the transports address to a nice, human-readable
1694 * @param cls closure
1695 * @param type name of the transport that generated the address
1696 * @param addr one of the addresses of the host, NULL for the last address
1697 * the specific address format depends on the transport
1698 * @param addrlen length of the address
1699 * @param numeric should (IP) addresses be displayed in numeric form?
1700 * @param timeout after how long should we give up?
1701 * @param asc function to call on each string
1702 * @param asc_cls closure for asc
1705 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1706 const void *addr, size_t addrlen,
1708 struct GNUNET_TIME_Relative timeout,
1709 GNUNET_TRANSPORT_AddressStringCallback asc,
1712 struct PrettyPrinterContext *ppc;
1715 struct sockaddr_in a4;
1716 struct sockaddr_in6 a6;
1717 const struct IPv4TcpAddress *t4;
1718 const struct IPv6TcpAddress *t6;
1724 if (addrlen == sizeof (struct IPv6TcpAddress))
1727 memset (&a6, 0, sizeof (a6));
1728 a6.sin6_family = AF_INET6;
1729 a6.sin6_port = t6->t6_port;
1730 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof (struct in6_addr));
1731 port = ntohs (t6->t6_port);
1732 options = ntohl (t6->options);
1736 else if (addrlen == sizeof (struct IPv4TcpAddress))
1739 memset (&a4, 0, sizeof (a4));
1740 a4.sin_family = AF_INET;
1741 a4.sin_port = t4->t4_port;
1742 a4.sin_addr.s_addr = t4->ipv4_addr;
1743 port = ntohs (t4->t4_port);
1744 options = ntohl (t4->options);
1748 else if (0 == addrlen)
1750 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1751 asc (asc_cls, NULL);
1756 /* invalid address */
1757 GNUNET_break_op (0);
1758 asc (asc_cls, NULL);
1761 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1762 if (addrlen == sizeof (struct IPv6TcpAddress))
1763 ppc->ipv6 = GNUNET_YES;
1765 ppc->ipv6 = GNUNET_NO;
1767 ppc->asc_cls = asc_cls;
1769 ppc->options = options;
1770 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(timeout, 2),
1771 &ppc_cancel_task, ppc);
1772 GNUNET_CONTAINER_DLL_insert (ppc_dll_head, ppc_dll_tail, ppc);
1773 /* GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "PPC %p was created!\n", ppc); */
1774 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1775 timeout, &append_port, ppc);
1780 * Check if the given port is plausible (must be either our listen
1781 * port or our advertised port), or any port if we are behind NAT
1782 * and do not have a port open. If it is neither, we return
1785 * @param plugin global variables
1786 * @param in_port port number to check
1787 * @return GNUNET_OK if port is either open_port or adv_port
1790 check_port (struct Plugin *plugin, uint16_t in_port)
1792 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1794 return GNUNET_SYSERR;
1799 * Function that will be called to check if a binary address for this
1800 * plugin is well-formed and corresponds to an address for THIS peer
1801 * (as per our configuration). Naturally, if absolutely necessary,
1802 * plugins can be a bit conservative in their answer, but in general
1803 * plugins should make sure that the address does not redirect
1804 * traffic to a 3rd party that might try to man-in-the-middle our
1807 * @param cls closure, our 'struct Plugin*'
1808 * @param addr pointer to the address
1809 * @param addrlen length of addr
1810 * @return GNUNET_OK if this is a plausible address for this peer
1811 * and transport, GNUNET_SYSERR if not
1814 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1816 struct Plugin *plugin = cls;
1817 struct IPv4TcpAddress *v4;
1818 struct IPv6TcpAddress *v6;
1820 if ((addrlen != sizeof (struct IPv4TcpAddress)) &&
1821 (addrlen != sizeof (struct IPv6TcpAddress)))
1825 return GNUNET_SYSERR;
1828 if (addrlen == sizeof (struct IPv4TcpAddress))
1830 v4 = (struct IPv4TcpAddress *) addr;
1831 if (0 != memcmp (&v4->options, &myoptions, sizeof (myoptions)))
1834 return GNUNET_SYSERR;
1836 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1837 return GNUNET_SYSERR;
1839 GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1840 sizeof (struct in_addr)))
1841 return GNUNET_SYSERR;
1845 v6 = (struct IPv6TcpAddress *) addr;
1846 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1848 GNUNET_break_op (0);
1849 return GNUNET_SYSERR;
1851 if (0 != memcmp (&v6->options, &myoptions, sizeof (myoptions)))
1854 return GNUNET_SYSERR;
1856 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1857 return GNUNET_SYSERR;
1859 GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1860 sizeof (struct in6_addr)))
1861 return GNUNET_SYSERR;
1868 * We've received a nat probe from this peer via TCP. Finish
1869 * creating the client session and resume sending of queued
1872 * @param cls closure
1873 * @param client identification of the client
1874 * @param message the actual message
1877 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1878 const struct GNUNET_MessageHeader *message)
1880 struct Plugin *plugin = cls;
1881 struct Session *session;
1882 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1885 struct IPv4TcpAddress *t4;
1886 struct IPv6TcpAddress *t6;
1887 const struct sockaddr_in *s4;
1888 const struct sockaddr_in6 *s6;
1890 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1892 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1893 * a connection to this peer by running gnunet-nat-client. This peer
1894 * received the punch message and now wants us to use the new connection
1895 * as the default for that peer. Do so and then send a WELCOME message
1896 * so we can really be connected!
1898 if (ntohs (message->size) != sizeof (struct TCP_NAT_ProbeMessage))
1900 GNUNET_break_op (0);
1901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1905 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1907 memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1908 sizeof (struct GNUNET_PeerIdentity)))
1910 /* refuse connections from ourselves */
1911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1916 GNUNET_CONTAINER_multihashmap_get (plugin->nat_wait_conns,
1918 clientIdentity.hashPubKey);
1919 if (session == NULL)
1921 LOG (GNUNET_ERROR_TYPE_DEBUG,
1922 "Did NOT find session for NAT probe!\n");
1923 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1926 LOG (GNUNET_ERROR_TYPE_DEBUG,
1927 "Found session for NAT probe!\n");
1929 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK)
1931 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1932 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1935 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1939 disconnect_session (session);
1942 GNUNET_assert (GNUNET_CONTAINER_multihashmap_remove
1943 (plugin->nat_wait_conns,
1944 &tcp_nat_probe->clientIdentity.hashPubKey,
1945 session) == GNUNET_YES);
1946 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
1947 &session->target.hashPubKey, session,
1948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1949 session->last_activity = GNUNET_TIME_absolute_get ();
1950 session->inbound = GNUNET_NO;
1951 LOG (GNUNET_ERROR_TYPE_DEBUG,
1952 "Found address `%s' for incoming connection\n",
1953 GNUNET_a2s (vaddr, alen));
1954 switch (((const struct sockaddr *) vaddr)->sa_family)
1958 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
1960 t4->t4_port = s4->sin_port;
1961 t4->ipv4_addr = s4->sin_addr.s_addr;
1963 session->addrlen = sizeof (struct IPv4TcpAddress);
1967 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
1969 t6->t6_port = s6->sin6_port;
1970 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
1972 session->addrlen = sizeof (struct IPv6TcpAddress);
1975 GNUNET_break_op (0);
1976 LOG (GNUNET_ERROR_TYPE_DEBUG,
1977 "Bad address for incoming connection!\n");
1978 GNUNET_free (vaddr);
1979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1980 disconnect_session (session);
1983 GNUNET_free (vaddr);
1984 GNUNET_break (NULL == session->client);
1985 GNUNET_SERVER_client_keep (client);
1986 session->client = client;
1987 inc_sessions (plugin, session, __LINE__);
1988 GNUNET_STATISTICS_update (plugin->env->stats,
1989 gettext_noop ("# TCP sessions active"), 1,
1991 process_pending_messages (session);
1992 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1997 * We've received a welcome from this peer via TCP. Possibly create a
1998 * fresh client record and send back our welcome.
2000 * @param cls closure
2001 * @param client identification of the client
2002 * @param message the actual message
2005 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2006 const struct GNUNET_MessageHeader *message)
2008 struct Plugin *plugin = cls;
2009 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2010 struct Session *session;
2013 struct IPv4TcpAddress *t4;
2014 struct IPv6TcpAddress *t6;
2015 const struct sockaddr_in *s4;
2016 const struct sockaddr_in6 *s6;
2017 struct GNUNET_ATS_Information ats;
2020 memcmp (&wm->clientIdentity, plugin->env->my_identity,
2021 sizeof (struct GNUNET_PeerIdentity)))
2023 /* refuse connections from ourselves */
2024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2025 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2027 LOG (GNUNET_ERROR_TYPE_WARNING,
2028 "Received %s message from my own identity `%4s' on address `%s'\n",
2029 "WELCOME", GNUNET_i2s (&wm->clientIdentity), GNUNET_a2s (vaddr, alen));
2030 GNUNET_free (vaddr);
2032 GNUNET_break_op (0);
2035 LOG (GNUNET_ERROR_TYPE_DEBUG,
2036 "Received %s message from `%4s' %p\n", "WELCOME",
2037 GNUNET_i2s (&wm->clientIdentity), client);
2038 GNUNET_STATISTICS_update (plugin->env->stats,
2039 gettext_noop ("# TCP WELCOME messages received"), 1,
2041 session = lookup_session_by_client (plugin, client);
2042 if (session != NULL)
2044 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2046 LOG (GNUNET_ERROR_TYPE_DEBUG,
2047 "Found existing session %p for peer `%s'\n",
2049 GNUNET_a2s (vaddr, alen));
2050 GNUNET_free (vaddr);
2055 GNUNET_SERVER_client_keep (client);
2056 if (plugin->service != NULL) /* Otherwise value is incremented in tcp_access_check */
2057 plugin->cur_connections++;
2058 if (plugin->cur_connections == plugin->max_connections)
2059 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2061 session = create_session (plugin, &wm->clientIdentity, client, GNUNET_NO);
2062 session->inbound = GNUNET_YES;
2063 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2065 if (alen == sizeof (struct sockaddr_in))
2068 t4 = GNUNET_malloc (sizeof (struct IPv4TcpAddress));
2069 t4->options = htonl (0);
2070 t4->t4_port = s4->sin_port;
2071 t4->ipv4_addr = s4->sin_addr.s_addr;
2073 session->addrlen = sizeof (struct IPv4TcpAddress);
2075 else if (alen == sizeof (struct sockaddr_in6))
2078 t6 = GNUNET_malloc (sizeof (struct IPv6TcpAddress));
2079 t6->options = htonl (0);
2080 t6->t6_port = s6->sin6_port;
2081 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof (struct in6_addr));
2083 session->addrlen = sizeof (struct IPv6TcpAddress);
2086 ats = plugin->env->get_address_type (plugin->env->cls, vaddr ,alen);
2087 session->ats_address_network_type = ats.value;
2088 LOG (GNUNET_ERROR_TYPE_DEBUG,
2089 "Creating new session %p for peer `%s'\n",
2091 GNUNET_a2s (vaddr, alen));
2092 GNUNET_free (vaddr);
2093 GNUNET_CONTAINER_multihashmap_put (plugin->sessionmap,
2094 &session->target.hashPubKey,
2096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2097 inc_sessions (plugin, session, __LINE__);
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Did not obtain TCP socket address for incoming connection\n");
2107 if (session->expecting_welcome != GNUNET_YES)
2109 GNUNET_break_op (0);
2110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2114 session->last_activity = GNUNET_TIME_absolute_get ();
2115 session->expecting_welcome = GNUNET_NO;
2117 /* Notify transport and ATS about new session */
2118 if (GNUNET_YES == session->inbound)
2120 plugin->env->session_start (NULL, &wm->clientIdentity, PLUGIN_NAME,
2121 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2122 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2126 process_pending_messages (session);
2128 GNUNET_SERVER_client_set_timeout (client,
2129 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2130 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2135 * Task to signal the server that we can continue
2136 * receiving from the TCP client now.
2138 * @param cls the 'struct Session*'
2139 * @param tc task context (unused)
2142 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2144 struct Session *session = cls;
2146 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
2147 reschedule_session_timeout (session);
2149 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
2154 * We've received data for this peer via TCP. Unbox,
2155 * compute latency and forward.
2157 * @param cls closure
2158 * @param client identification of the client
2159 * @param message the actual message
2162 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2163 const struct GNUNET_MessageHeader *message)
2165 struct Plugin *plugin = cls;
2166 struct Session *session;
2167 struct GNUNET_TIME_Relative delay;
2170 type = ntohs (message->type);
2171 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type) ||
2172 (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2174 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2178 session = lookup_session_by_client (plugin, client);
2179 if (NULL == session)
2181 /* No inbound session found */
2185 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2186 LOG (GNUNET_ERROR_TYPE_ERROR,
2187 "Received unexpected %u bytes of type %u from `%s'\n",
2188 (unsigned int) ntohs (message->size),
2189 (unsigned int) ntohs (message->type),
2190 GNUNET_a2s(vaddr, alen));
2191 GNUNET_break_op (0);
2192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2193 GNUNET_free_non_null(vaddr);
2196 else if (GNUNET_YES == session->expecting_welcome)
2198 /* Session is expecting WELCOME message */
2202 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2203 LOG (GNUNET_ERROR_TYPE_ERROR,
2204 "Received unexpected %u bytes of type %u from `%s'\n",
2205 (unsigned int) ntohs (message->size),
2206 (unsigned int) ntohs (message->type),
2207 GNUNET_a2s(vaddr, alen));
2208 GNUNET_break_op (0);
2209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2210 GNUNET_free_non_null(vaddr);
2214 session->last_activity = GNUNET_TIME_absolute_get ();
2215 LOG (GNUNET_ERROR_TYPE_DEBUG,
2216 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2217 (unsigned int) ntohs (message->size),
2218 (unsigned int) ntohs (message->type),
2219 GNUNET_i2s (&session->target));
2221 GNUNET_STATISTICS_update (plugin->env->stats,
2222 gettext_noop ("# bytes received via TCP"),
2223 ntohs (message->size), GNUNET_NO);
2224 struct GNUNET_ATS_Information distance;
2226 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2227 distance.value = session->ats_address_network_type;
2228 GNUNET_break (ntohl(session->ats_address_network_type) != GNUNET_ATS_NET_UNSPECIFIED);
2230 GNUNET_assert (GNUNET_CONTAINER_multihashmap_contains_value (plugin->sessionmap,
2231 &session->target.hashPubKey,
2234 delay = plugin->env->receive (plugin->env->cls,
2238 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2239 (GNUNET_YES == session->inbound) ? 0 : session->addrlen);
2240 plugin->env->update_address_metrics (plugin->env->cls,
2242 (GNUNET_YES == session->inbound) ? NULL : session->addr,
2243 (GNUNET_YES == session->inbound) ? 0 : session->addrlen,
2244 session, &distance, 1);
2246 reschedule_session_timeout (session);
2248 if (delay.rel_value == 0)
2250 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2254 LOG (GNUNET_ERROR_TYPE_DEBUG,
2255 "Throttling receiving from `%s' for %llu ms\n",
2256 GNUNET_i2s (&session->target),
2257 (unsigned long long) delay.rel_value);
2258 GNUNET_SERVER_disable_receive_done_warning (client);
2259 session->receive_delay_task =
2260 GNUNET_SCHEDULER_add_delayed (delay, &delayed_done, session);
2266 * Functions with this signature are called whenever a peer
2267 * is disconnected on the network level.
2269 * @param cls closure
2270 * @param client identification of the client
2273 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2275 struct Plugin *plugin = cls;
2276 struct Session *session;
2280 session = lookup_session_by_client (plugin, client);
2281 if (session == NULL)
2282 return; /* unknown, nothing to do */
2283 LOG (GNUNET_ERROR_TYPE_DEBUG,
2284 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2285 GNUNET_i2s (&session->target),
2287 NULL) ? tcp_address_to_string (session->plugin,
2292 if (plugin->cur_connections == plugin->max_connections)
2293 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2295 if (plugin->cur_connections < 1)
2298 plugin->cur_connections--;
2300 GNUNET_STATISTICS_update (session->plugin->env->stats,
2302 ("# network-level TCP disconnect events"), 1,
2304 disconnect_session (session);
2309 * We can now send a probe message, copy into buffer to really send.
2311 * @param cls closure, a struct TCPProbeContext
2312 * @param size max size to copy
2313 * @param buf buffer to copy message to
2314 * @return number of bytes copied into buf
2317 notify_send_probe (void *cls, size_t size, void *buf)
2319 struct TCPProbeContext *tcp_probe_ctx = cls;
2320 struct Plugin *plugin = tcp_probe_ctx->plugin;
2323 tcp_probe_ctx->transmit_handle = NULL;
2324 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2328 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2329 GNUNET_free (tcp_probe_ctx);
2332 GNUNET_assert (size >= sizeof (tcp_probe_ctx->message));
2333 memcpy (buf, &tcp_probe_ctx->message, sizeof (tcp_probe_ctx->message));
2334 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2335 tcp_probe_ctx->sock);
2336 ret = sizeof (tcp_probe_ctx->message);
2337 GNUNET_free (tcp_probe_ctx);
2343 * Function called by the NAT subsystem suggesting another peer wants
2344 * to connect to us via connection reversal. Try to connect back to the
2347 * @param cls closure
2348 * @param addr address to try
2349 * @param addrlen number of bytes in addr
2352 try_connection_reversal (void *cls, const struct sockaddr *addr,
2355 struct Plugin *plugin = cls;
2356 struct GNUNET_CONNECTION_Handle *sock;
2357 struct TCPProbeContext *tcp_probe_ctx;
2360 * We have received an ICMP response, ostensibly from a peer
2361 * that wants to connect to us! Send a message to establish a connection.
2363 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2366 /* failed for some odd reason (out of sockets?); ignore attempt */
2370 /* FIXME: do we need to track these probe context objects so that
2371 * we can clean them up on plugin unload? */
2372 tcp_probe_ctx = GNUNET_malloc (sizeof (struct TCPProbeContext));
2373 tcp_probe_ctx->message.header.size =
2374 htons (sizeof (struct TCP_NAT_ProbeMessage));
2375 tcp_probe_ctx->message.header.type =
2376 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2377 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2378 sizeof (struct GNUNET_PeerIdentity));
2379 tcp_probe_ctx->plugin = plugin;
2380 tcp_probe_ctx->sock = sock;
2381 GNUNET_CONTAINER_DLL_insert (plugin->probe_head, plugin->probe_tail,
2383 tcp_probe_ctx->transmit_handle =
2384 GNUNET_CONNECTION_notify_transmit_ready (sock,
2385 ntohs (tcp_probe_ctx->
2386 message.header.size),
2387 GNUNET_TIME_UNIT_FOREVER_REL,
2395 * Session was idle, so disconnect it
2398 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2400 GNUNET_assert (NULL != cls);
2401 struct Session *s = cls;
2403 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2405 "Session %p was idle for %llu ms, disconnecting\n",
2406 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2407 /* call session destroy function */
2408 disconnect_session(s);
2413 * Start session timeout
2416 start_session_timeout (struct Session *s)
2418 GNUNET_assert (NULL != s);
2419 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == s->timeout_task);
2420 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Timeout for session %p set to %llu ms\n",
2425 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2430 * Increment session timeout due to activity
2433 reschedule_session_timeout (struct Session *s)
2435 GNUNET_assert (NULL != s);
2436 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
2438 GNUNET_SCHEDULER_cancel (s->timeout_task);
2439 s->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2443 "Timeout rescheduled for session %p set to %llu ms\n",
2444 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2452 stop_session_timeout (struct Session *s)
2454 GNUNET_assert (NULL != s);
2456 if (GNUNET_SCHEDULER_NO_TASK != s->timeout_task)
2458 GNUNET_SCHEDULER_cancel (s->timeout_task);
2459 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "Timeout stopped for session %p canceled\n",
2462 s, (unsigned long long) GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT.rel_value);
2467 * Function obtain the network type for a session
2469 * @param cls closure ('struct Plugin*')
2470 * @param session the session
2471 * @return the network type in HBO or GNUNET_SYSERR
2473 static enum GNUNET_ATS_Network_Type
2474 tcp_get_network (void *cls,
2475 struct Session *session)
2477 GNUNET_assert (NULL != session);
2478 return ntohl (session->ats_address_network_type);
2483 * Entry point for the plugin.
2485 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2486 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2489 libgnunet_plugin_transport_tcp_init (void *cls)
2491 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = {
2492 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2493 sizeof (struct WelcomeMessage)},
2494 {&handle_tcp_nat_probe, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2495 sizeof (struct TCP_NAT_ProbeMessage)},
2496 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_ALL, 0},
2499 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2500 struct GNUNET_TRANSPORT_PluginFunctions *api;
2501 struct Plugin *plugin;
2502 struct GNUNET_SERVICE_Context *service;
2503 unsigned long long aport;
2504 unsigned long long bport;
2505 unsigned long long max_connections;
2507 struct GNUNET_TIME_Relative idle_timeout;
2510 struct sockaddr **addrs;
2511 socklen_t *addrlens;
2514 if (NULL == env->receive)
2516 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2517 initialze the plugin or the API */
2518 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2520 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2521 api->address_to_string = &tcp_address_to_string;
2522 api->string_to_address = &tcp_string_to_address;
2526 GNUNET_assert (NULL != env->cfg);
2528 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2531 max_connections = 128;
2535 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp", "PORT",
2536 &bport)) || (bport > 65535) ||
2538 GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2539 "ADVERTISED-PORT", &aport)) &&
2542 LOG (GNUNET_ERROR_TYPE_ERROR,
2544 ("Require valid port number for service `%s' in configuration!\n"),
2554 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg, GNUNET_SERVICE_OPTION_NONE);
2555 if (service == NULL)
2557 LOG (GNUNET_ERROR_TYPE_WARNING,
2558 _("Failed to start service.\n"));
2565 /* Initialize my flags */
2568 plugin = GNUNET_malloc (sizeof (struct Plugin));
2569 plugin->sessionmap = GNUNET_CONTAINER_multihashmap_create (max_connections, GNUNET_YES);
2570 plugin->max_connections = max_connections;
2571 plugin->cur_connections = 0;
2572 plugin->open_port = bport;
2573 plugin->adv_port = aport;
2575 plugin->lsock = NULL;
2576 if ((service != NULL) &&
2579 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2582 for (ret = ret_s-1; ret >= 0; ret--)
2583 LOG (GNUNET_ERROR_TYPE_INFO,
2584 "Binding to address `%s'\n",
2585 GNUNET_a2s (addrs[ret], addrlens[ret]));
2587 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2588 (const struct sockaddr **) addrs, addrlens,
2589 &tcp_nat_port_map_callback,
2590 &try_connection_reversal, plugin);
2591 for (ret = ret_s -1; ret >= 0; ret--)
2593 GNUNET_assert (addrs[ret] != NULL);
2594 GNUNET_free (addrs[ret]);
2596 GNUNET_free_non_null (addrs);
2597 GNUNET_free_non_null (addrlens);
2601 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2602 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2603 &try_connection_reversal, plugin);
2605 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2607 api->send = &tcp_plugin_send;
2608 api->get_session = &tcp_plugin_get_session;
2610 api->disconnect = &tcp_plugin_disconnect;
2611 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2612 api->check_address = &tcp_plugin_check_address;
2613 api->address_to_string = &tcp_address_to_string;
2614 api->string_to_address = &tcp_string_to_address;
2615 api->get_network = &tcp_get_network;
2616 plugin->service = service;
2617 if (service != NULL)
2619 plugin->server = GNUNET_SERVICE_get_server (service);
2624 GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2625 "TIMEOUT", &idle_timeout))
2627 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2628 "transport-tcp", "TIMEOUT");
2629 if (plugin->nat != NULL)
2630 GNUNET_NAT_unregister (plugin->nat);
2631 GNUNET_free (plugin);
2636 GNUNET_SERVER_create_with_sockets (&plugin_tcp_access_check, plugin,
2637 NULL, idle_timeout, GNUNET_YES);
2639 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2640 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
2642 i < sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
2644 plugin->handlers[i].callback_cls = plugin;
2646 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2647 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2648 plugin->nat_wait_conns = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_YES);
2650 LOG (GNUNET_ERROR_TYPE_INFO,
2651 _("TCP transport listening on port %llu\n"), bport);
2653 LOG (GNUNET_ERROR_TYPE_INFO,
2655 ("TCP transport not listening on any port (client only)\n"));
2657 LOG (GNUNET_ERROR_TYPE_INFO,
2659 ("TCP transport advertises itself as being on port %llu\n"),
2661 /* Initially set connections to 0 */
2662 GNUNET_assert (NULL != plugin->env->stats);
2663 GNUNET_STATISTICS_set (plugin->env->stats,
2664 gettext_noop ("# TCP sessions active"), 0,
2671 * Exit point from the plugin.
2674 libgnunet_plugin_transport_tcp_done (void *cls)
2676 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2677 struct Plugin *plugin = api->cls;
2678 struct TCPProbeContext *tcp_probe;
2679 struct PrettyPrinterContext *cur;
2680 struct PrettyPrinterContext *next;
2687 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2689 /* Removing leftover sessions */
2690 GNUNET_CONTAINER_multihashmap_iterate(plugin->sessionmap, &session_disconnect_it, NULL);
2691 /* Removing leftover NAT sessions */
2692 GNUNET_CONTAINER_multihashmap_iterate(plugin->nat_wait_conns, &session_disconnect_it, NULL);
2694 next = ppc_dll_head;
2695 for (cur = next; NULL != cur; cur = next)
2698 GNUNET_CONTAINER_DLL_remove (ppc_dll_head, ppc_dll_tail, cur);
2699 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2700 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2705 if (plugin->service != NULL)
2706 GNUNET_SERVICE_stop (plugin->service);
2708 GNUNET_SERVER_destroy (plugin->server);
2709 GNUNET_free (plugin->handlers);
2710 if (plugin->nat != NULL)
2711 GNUNET_NAT_unregister (plugin->nat);
2712 while (NULL != (tcp_probe = plugin->probe_head))
2714 GNUNET_CONTAINER_DLL_remove (plugin->probe_head, plugin->probe_tail,
2716 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2717 GNUNET_free (tcp_probe);
2719 GNUNET_CONTAINER_multihashmap_destroy (plugin->nat_wait_conns);
2720 GNUNET_CONTAINER_multihashmap_destroy (plugin->sessionmap);
2721 GNUNET_free (plugin);
2726 /* end of plugin_transport_tcp.c */