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_util_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_resolver_service.h"
32 #include "gnunet_signatures.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_transport_plugin.h"
36 #include "transport.h"
38 #define LOG(kind,...) GNUNET_log_from (kind, "transport-tcp",__VA_ARGS__)
40 #define PLUGIN_NAME "tcp"
42 #define EXTRA_CHECKS ALLOW_EXTRA_CHECKS
45 * How long until we give up on establishing an NAT connection?
48 #define NAT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
50 GNUNET_NETWORK_STRUCT_BEGIN
55 static uint32_t myoptions;
58 * Initial handshake message for a session.
63 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME.
65 struct GNUNET_MessageHeader header;
68 * Identity of the node connecting (TCP client)
70 struct GNUNET_PeerIdentity clientIdentity;
75 * Basically a WELCOME message, but with the purpose
76 * of giving the waiting peer a client handle to use
78 struct TCP_NAT_ProbeMessage
81 * Type is #GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE.
83 struct GNUNET_MessageHeader header;
86 * Identity of the sender of the message.
88 struct GNUNET_PeerIdentity clientIdentity;
91 GNUNET_NETWORK_STRUCT_END
94 * Context for sending a NAT probe via TCP.
96 struct TCPProbeContext
100 * Active probes are kept in a DLL.
102 struct TCPProbeContext *next;
105 * Active probes are kept in a DLL.
107 struct TCPProbeContext *prev;
112 struct GNUNET_CONNECTION_Handle *sock;
115 * Message to be sent.
117 struct TCP_NAT_ProbeMessage message;
120 * Handle to the transmission.
122 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
125 * Transport plugin handle.
127 struct Plugin *plugin;
130 GNUNET_NETWORK_STRUCT_BEGIN
133 * Network format for IPv4 addresses.
135 struct IPv4TcpAddress
138 * Optional options and flags for this address
143 * IPv4 address, in network byte order.
145 uint32_t ipv4_addr GNUNET_PACKED;
148 * Port number, in network byte order.
150 uint16_t t4_port GNUNET_PACKED;
155 * Network format for IPv6 addresses.
157 struct IPv6TcpAddress
160 * Optional flags for this address
167 struct in6_addr ipv6_addr GNUNET_PACKED;
170 * Port number, in network byte order.
172 uint16_t t6_port GNUNET_PACKED;
175 GNUNET_NETWORK_STRUCT_END
178 * Encapsulation of all of the state of the plugin.
183 * Information kept for each message that is yet to
186 struct PendingMessage
190 * This is a doubly-linked list.
192 struct PendingMessage *next;
195 * This is a doubly-linked list.
197 struct PendingMessage *prev;
200 * The pending message
205 * Continuation function to call once the message
206 * has been sent. Can be NULL if there is no
207 * continuation to call.
209 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
212 * Closure for transmit_cont.
214 void *transmit_cont_cls;
217 * Timeout value for the pending message.
219 struct GNUNET_TIME_Absolute timeout;
222 * So that the gnunet-service-transport can group messages together,
223 * these pending messages need to accept a message buffer and size
224 * instead of just a GNUNET_MessageHeader.
231 * Session handle for TCP connections.
236 * To whom are we talking to (set to our identity
237 * if we are still waiting for the welcome message)
239 struct GNUNET_PeerIdentity target;
244 struct SessionHeader header;
247 * Pointer to the global plugin struct.
249 struct Plugin *plugin;
252 * The client (used to identify this connection)
254 struct GNUNET_SERVER_Client *client;
257 * Task cleaning up a NAT client connection establishment attempt;
259 GNUNET_SCHEDULER_TaskIdentifier nat_connection_timeout;
262 * Messages currently pending for transmission
263 * to this peer, if any.
265 struct PendingMessage *pending_messages_head;
268 * Messages currently pending for transmission
269 * to this peer, if any.
271 struct PendingMessage *pending_messages_tail;
274 * Handle for pending transmission request.
276 struct GNUNET_SERVER_TransmitHandle *transmit_handle;
279 * ID of task used to delay receiving more to throttle sender.
281 GNUNET_SCHEDULER_TaskIdentifier receive_delay_task;
284 * Session timeout task
286 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
288 struct GNUNET_HELLO_Address *address;
291 * Address of the other peer (either based on our 'connect'
292 * call or on our 'accept' call).
294 * struct IPv4TcpAddress or struct IPv6TcpAddress
302 * Last activity on this connection. Used to select preferred
305 struct GNUNET_TIME_Absolute last_activity;
308 * Are we still expecting the welcome message? (#GNUNET_YES/#GNUNET_NO)
310 int expecting_welcome;
313 * Was this session created using NAT traversal?
318 * ATS network type in NBO
320 enum GNUNET_ATS_Network_Type ats_address_network_type;
324 * Encapsulation of all of the state of the plugin.
331 struct GNUNET_TRANSPORT_PluginEnvironment *env;
336 struct GNUNET_CONNECTION_Handle *lsock;
339 * Our handle to the NAT module.
341 struct GNUNET_NAT_Handle *nat;
344 * Map from peer identities to sessions for the given peer.
346 struct GNUNET_CONTAINER_MultiPeerMap *sessionmap;
349 * Handle to the network service.
351 struct GNUNET_SERVICE_Context *service;
354 * Handle to the server for this service.
356 struct GNUNET_SERVER_Handle *server;
359 * Copy of the handler array where the closures are
360 * set to this struct's instance.
362 struct GNUNET_SERVER_MessageHandler *handlers;
365 * Map of peers we have tried to contact behind a NAT
367 struct GNUNET_CONTAINER_MultiPeerMap *nat_wait_conns;
370 * List of active TCP probes.
372 struct TCPProbeContext *probe_head;
375 * List of active TCP probes.
377 struct TCPProbeContext *probe_tail;
380 * Handle for (DYN)DNS lookup of our external IP.
382 struct GNUNET_RESOLVER_RequestHandle *ext_dns;
385 * How many more TCP sessions are we allowed to open right now?
387 unsigned long long max_connections;
390 * How many more TCP sessions do we have right now?
392 unsigned long long cur_connections;
395 * ID of task used to update our addresses when one expires.
397 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
400 * Port that we are actually listening on.
405 * Port that the user said we would have visible to the
413 * Function called for a quick conversion of the binary address to
414 * a numeric address. Note that the caller must not free the
415 * address and that the next call to this function is allowed
416 * to override the address again.
418 * @param cls closure ('struct Plugin*')
419 * @param addr binary address
420 * @param addrlen length of the address
421 * @return string representing the same address
424 tcp_address_to_string (void *cls, const void *addr, size_t addrlen);
427 * Function to check if an inbound connection is acceptable.
428 * Mostly used to limit the total number of open connections
431 * @param cls the `struct Plugin`
432 * @param ucred credentials, if available, otherwise NULL
433 * @param addr address
434 * @param addrlen length of address
435 * @return #GNUNET_YES to allow, #GNUNET_NO to deny, #GNUNET_SYSERR
436 * for unknown address family (will be denied).
439 plugin_tcp_access_check (void *cls,
440 const struct GNUNET_CONNECTION_Credentials *ucred,
441 const struct sockaddr *addr, socklen_t addrlen)
443 struct Plugin *plugin = cls;
444 LOG(GNUNET_ERROR_TYPE_DEBUG,
445 "Accepting new incoming TCP connection from `%s'\n",
446 GNUNET_a2s (addr, addrlen));
447 if (plugin->cur_connections >= plugin->max_connections)
449 plugin->cur_connections++;
454 * Our external IP address/port mapping has changed.
456 * @param cls closure, the 'struct Plugin'
457 * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
458 * the previous (now invalid) one
459 * @param addr either the previous or the new public IP address
460 * @param addrlen actual lenght of the address
463 tcp_nat_port_map_callback (void *cls, int add_remove,
464 const struct sockaddr *addr, socklen_t addrlen)
466 struct Plugin *plugin = cls;
467 struct GNUNET_HELLO_Address *address;
468 struct IPv4TcpAddress t4;
469 struct IPv6TcpAddress t6;
473 LOG(GNUNET_ERROR_TYPE_INFO, "NAT notification to %s address `%s'\n",
474 (GNUNET_YES == add_remove) ? "add" : "remove",
475 GNUNET_a2s (addr, addrlen));
476 /* convert 'addr' to our internal format */
477 switch (addr->sa_family)
480 GNUNET_assert(addrlen == sizeof(struct sockaddr_in));
481 memset (&t4, 0, sizeof(t4));
482 t4.options = htonl (myoptions);
483 t4.ipv4_addr = ((struct sockaddr_in *) addr)->sin_addr.s_addr;
484 t4.t4_port = ((struct sockaddr_in *) addr)->sin_port;
489 GNUNET_assert(addrlen == sizeof(struct sockaddr_in6));
490 memset (&t6, 0, sizeof(t6));
491 memcpy (&t6.ipv6_addr, &((struct sockaddr_in6 *) addr)->sin6_addr,
492 sizeof(struct in6_addr));
493 t6.options = htonl (myoptions);
494 t6.t6_port = ((struct sockaddr_in6 *) addr)->sin6_port;
502 /* modify our published address list */
503 GNUNET_assert ((args == sizeof (struct IPv4TcpAddress)) ||
504 (args == sizeof (struct IPv6TcpAddress)));
505 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
506 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
507 plugin->env->notify_address (plugin->env->cls, add_remove, address);
508 GNUNET_HELLO_address_free(address);
512 * Function called for a quick conversion of the binary address to
513 * a numeric address. Note that the caller must not free the
514 * address and that the next call to this function is allowed
515 * to override the address again.
517 * @param cls closure (`struct Plugin*`)
518 * @param addr binary address
519 * @param addrlen length of the address
520 * @return string representing the same address
523 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
525 static char rbuf[INET6_ADDRSTRLEN + 12];
526 char buf[INET6_ADDRSTRLEN];
530 const struct IPv4TcpAddress *t4;
531 const struct IPv6TcpAddress *t6;
538 case sizeof(struct IPv6TcpAddress):
541 port = ntohs (t6->t6_port);
542 options = ntohl (t6->options);
543 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
546 case sizeof(struct IPv4TcpAddress):
549 port = ntohs (t4->t4_port);
550 options = ntohl (t4->options);
551 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
555 LOG(GNUNET_ERROR_TYPE_WARNING, _("Unexpected address length: %u bytes\n"),
556 (unsigned int ) addrlen);
559 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
561 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
564 GNUNET_snprintf (rbuf, sizeof(rbuf),
565 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
571 * Function called to convert a string address to
574 * @param cls closure (`struct Plugin*`)
575 * @param addr string address
576 * @param addrlen length of the address
577 * @param buf location to store the buffer
578 * @param added location to store the number of bytes in the buffer.
579 * If the function returns #GNUNET_SYSERR, its contents are undefined.
580 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
583 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
584 void **buf, size_t *added)
586 struct sockaddr_storage socket_address;
592 /* Format tcp.options.address:port */
596 if ((NULL == addr) || (addrlen == 0))
599 return GNUNET_SYSERR;
601 if ('\0' != addr[addrlen - 1])
604 return GNUNET_SYSERR;
606 if (strlen (addr) != addrlen - 1)
609 return GNUNET_SYSERR;
611 plugin = GNUNET_strdup (addr);
612 optionstr = strchr (plugin, '.');
613 if (NULL == optionstr)
617 return GNUNET_SYSERR;
621 options = atol (optionstr);
622 address = strchr (optionstr, '.');
627 return GNUNET_SYSERR;
633 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
638 return GNUNET_SYSERR;
642 switch (socket_address.ss_family)
646 struct IPv4TcpAddress *t4;
647 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
648 t4 = GNUNET_new (struct IPv4TcpAddress);
649 t4->options = htonl (options);
650 t4->ipv4_addr = in4->sin_addr.s_addr;
651 t4->t4_port = in4->sin_port;
653 *added = sizeof(struct IPv4TcpAddress);
658 struct IPv6TcpAddress *t6;
659 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
660 t6 = GNUNET_new (struct IPv6TcpAddress);
661 t6->options = htonl (options);
662 t6->ipv6_addr = in6->sin6_addr;
663 t6->t6_port = in6->sin6_port;
665 *added = sizeof(struct IPv6TcpAddress);
669 return GNUNET_SYSERR;
674 * Closure for #session_lookup_by_client_it().
676 struct SessionClientCtx
679 * Client we are looking for.
681 const struct GNUNET_SERVER_Client *client;
684 * Session that was found.
690 session_lookup_by_client_it (void *cls, const struct GNUNET_PeerIdentity *key,
693 struct SessionClientCtx *sc_ctx = cls;
694 struct Session *s = value;
696 if (s->client == sc_ctx->client)
705 * Find the session handle for the given client.
706 * Currently uses both the hashmap and the client
707 * context, as the client context is new and the
708 * logic still needs to be tested.
710 * @param plugin the plugin
711 * @param client which client to find the session handle for
712 * @return NULL if no matching session exists
714 static struct Session *
715 lookup_session_by_client (struct Plugin *plugin,
716 struct GNUNET_SERVER_Client *client)
719 struct SessionClientCtx sc_ctx;
721 ret = GNUNET_SERVER_client_get_user_context (client, struct Session);
722 sc_ctx.client = client;
724 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
725 &session_lookup_by_client_it, &sc_ctx);
726 /* check both methods yield the same result */
727 GNUNET_break(ret == sc_ctx.ret);
732 * Functions with this signature are called whenever we need
733 * to close a session due to a disconnect or failure to
734 * establish a connection.
736 * @param cls the `struct Plugin`
737 * @param session session to close down
738 * @return #GNUNET_OK on success
741 tcp_disconnect_session (void *cls, struct Session *session)
743 struct Plugin *plugin = cls;
744 struct PendingMessage *pm;
746 LOG(GNUNET_ERROR_TYPE_DEBUG,
747 "Disconnecting session of peer `%s' address `%s'\n",
748 GNUNET_i2s (&session->target),
749 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
751 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
753 GNUNET_SCHEDULER_cancel (session->timeout_task);
754 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
758 == GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
759 &session->target, session))
761 GNUNET_STATISTICS_update (session->plugin->env->stats,
762 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
767 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &session->target, session));
769 if (NULL != session->client)
770 GNUNET_SERVER_client_set_user_context(session->client, (void *) NULL);
773 if (NULL != session->transmit_handle)
775 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
776 session->transmit_handle = NULL;
778 plugin->env->unregister_quota_notification (plugin->env->cls,
779 &session->target, PLUGIN_NAME, session);
780 session->plugin->env->session_end (session->plugin->env->cls,
781 session->address, session);
783 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
785 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
786 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
789 while (NULL != (pm = session->pending_messages_head))
791 LOG(GNUNET_ERROR_TYPE_DEBUG,
792 pm->transmit_cont != NULL ? "Could not deliver message to `%4s'.\n" : "Could not deliver message to `%4s', notifying.\n",
793 GNUNET_i2s (&session->target));
794 GNUNET_STATISTICS_update (session->plugin->env->stats,
795 gettext_noop ("# bytes currently in TCP buffers"),
796 -(int64_t) pm->message_size, GNUNET_NO);
797 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
798 ("# bytes discarded by TCP (disconnect)"), pm->message_size, GNUNET_NO);
799 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
800 session->pending_messages_tail, pm);
801 if (NULL != pm->transmit_cont)
802 pm->transmit_cont (pm->transmit_cont_cls, &session->target, GNUNET_SYSERR,
803 pm->message_size, 0);
806 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK )
808 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
809 if (NULL != session->client)
810 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
812 if (NULL != session->client)
814 GNUNET_SERVER_client_disconnect (session->client);
815 GNUNET_SERVER_client_drop (session->client);
816 session->client = NULL;
818 GNUNET_HELLO_address_free (session->address);
819 GNUNET_assert(NULL == session->transmit_handle);
820 GNUNET_free(session);
825 * Function that is called to get the keepalive factor.
826 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
827 * calculate the interval between keepalive packets.
829 * @param cls closure with the `struct Plugin`
830 * @return keepalive factor
833 tcp_query_keepalive_factor (void *cls)
839 * Session was idle, so disconnect it
841 * @param cls the `struct Session` of the idle session
842 * @param tc scheduler context
845 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
847 struct Session *s = cls;
849 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
850 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
851 "Session %p was idle for %s, disconnecting\n", s,
852 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
853 /* call session destroy function */
854 tcp_disconnect_session (s->plugin, s);
858 * Increment session timeout due to activity
860 * @param s session to increment timeout for
863 reschedule_session_timeout (struct Session *s)
865 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
866 GNUNET_SCHEDULER_cancel (s->timeout_task);
867 s->timeout_task = GNUNET_SCHEDULER_add_delayed (
868 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, s);
869 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
870 "Timeout rescheduled for session %p set to %s\n", s,
871 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
875 * Create a new session. Also queues a welcome message.
877 * @param plugin the plugin
878 * @param address the address to create the session for
879 * @param client client to use, reference counter must have already been increased
880 * @param is_nat this a NAT session, we should wait for a client to
881 * connect to us from an address, then assign that to
883 * @return new session object
885 static struct Session *
886 create_session (struct Plugin *plugin,
887 const struct GNUNET_HELLO_Address *address,
888 struct GNUNET_SERVER_Client *client,
891 struct Session *session;
892 struct PendingMessage *pm;
893 struct WelcomeMessage welcome;
895 if (GNUNET_YES != is_nat)
896 GNUNET_assert(NULL != client);
898 GNUNET_assert(NULL == client);
900 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new session for peer `%4s'\n",
901 GNUNET_i2s (&address->peer));
902 session = GNUNET_new (struct Session);
903 session->last_activity = GNUNET_TIME_absolute_get ();
904 session->plugin = plugin;
905 session->is_nat = is_nat;
906 session->client = client;
907 session->address = GNUNET_HELLO_address_copy (address);
908 session->target = address->peer;
909 session->expecting_welcome = GNUNET_YES;
910 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
911 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
912 sizeof (struct WelcomeMessage));
913 pm->msg = (const char *) &pm[1];
914 pm->message_size = sizeof(struct WelcomeMessage);
915 welcome.header.size = htons (sizeof(struct WelcomeMessage));
916 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
917 welcome.clientIdentity = *plugin->env->my_identity;
918 memcpy (&pm[1], &welcome, sizeof(welcome));
919 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
920 GNUNET_STATISTICS_update (plugin->env->stats,
921 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
923 GNUNET_CONTAINER_DLL_insert(session->pending_messages_head,
924 session->pending_messages_tail, pm);
925 if (GNUNET_YES != is_nat)
927 GNUNET_STATISTICS_update (plugin->env->stats,
928 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
930 plugin->env->register_quota_notification (plugin->env->cls,
931 &address->peer, PLUGIN_NAME, session);
932 session->timeout_task = GNUNET_SCHEDULER_add_delayed (
933 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
938 * If we have pending messages, ask the server to
939 * transmit them (schedule the respective tasks, etc.)
941 * @param session for which session should we do this
944 process_pending_messages (struct Session *session);
947 * Function called to notify a client about the socket
948 * being ready to queue more data. "buf" will be
949 * NULL and "size" zero if the socket was closed for
950 * writing in the meantime.
953 * @param size number of bytes available in buf
954 * @param buf where the callee should write the message
955 * @return number of bytes written to buf
958 do_transmit (void *cls, size_t size, void *buf)
960 struct Session *session = cls;
961 struct GNUNET_PeerIdentity pid;
962 struct Plugin *plugin;
963 struct PendingMessage *pos;
964 struct PendingMessage *hd;
965 struct PendingMessage *tl;
966 struct GNUNET_TIME_Absolute now;
970 session->transmit_handle = NULL;
971 plugin = session->plugin;
974 LOG(GNUNET_ERROR_TYPE_DEBUG,
975 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
976 GNUNET_i2s (&session->target));
977 /* timeout; cancel all messages that have already expired */
981 now = GNUNET_TIME_absolute_get ();
982 while ((NULL != (pos = session->pending_messages_head))
983 && (pos->timeout.abs_value_us <= now.abs_value_us))
985 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
986 session->pending_messages_tail, pos);
987 LOG(GNUNET_ERROR_TYPE_DEBUG,
988 "Failed to transmit %u byte message to `%4s'.\n", pos->message_size,
989 GNUNET_i2s (&session->target));
990 ret += pos->message_size;
991 GNUNET_CONTAINER_DLL_insert_after(hd, tl, tl, pos);
993 /* do this call before callbacks (so that if callbacks destroy
994 * session, they have a chance to cancel actions done by this
996 process_pending_messages (session);
997 pid = session->target;
998 /* no do callbacks and do not use session again since
999 * the callbacks may abort the session */
1000 while (NULL != (pos = hd))
1002 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1003 if (pos->transmit_cont != NULL )
1004 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR,
1005 pos->message_size, 0);
1008 GNUNET_STATISTICS_update (plugin->env->stats,
1009 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1011 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1012 ("# bytes discarded by TCP (timeout)"), ret, GNUNET_NO);
1015 /* copy all pending messages that would fit */
1020 while (NULL != (pos = session->pending_messages_head))
1022 if (ret + pos->message_size > size)
1024 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
1025 session->pending_messages_tail, pos);
1026 GNUNET_assert(size >= pos->message_size);
1027 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting message of type %u\n",
1028 ntohs (((struct GNUNET_MessageHeader * ) pos->msg)->type));
1029 /* FIXME: this memcpy can be up to 7% of our total runtime */
1030 memcpy (cbuf, pos->msg, pos->message_size);
1031 cbuf += pos->message_size;
1032 ret += pos->message_size;
1033 size -= pos->message_size;
1034 GNUNET_CONTAINER_DLL_insert_tail(hd, tl, pos);
1036 /* schedule 'continuation' before callbacks so that callbacks that
1037 * cancel everything don't cause us to use a session that no longer
1039 process_pending_messages (session);
1040 session->last_activity = GNUNET_TIME_absolute_get ();
1041 pid = session->target;
1042 /* we'll now call callbacks that may cancel the session; hence
1043 * we should not use 'session' after this point */
1044 while (NULL != (pos = hd))
1046 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1047 if (pos->transmit_cont != NULL )
1048 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK,
1049 pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
1052 GNUNET_assert(hd == NULL);
1053 GNUNET_assert(tl == NULL);
1054 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n", ret);
1055 GNUNET_STATISTICS_update (plugin->env->stats,
1056 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1058 GNUNET_STATISTICS_update (plugin->env->stats,
1059 gettext_noop ("# bytes transmitted via TCP"), ret, GNUNET_NO);
1064 * If we have pending messages, ask the server to
1065 * transmit them (schedule the respective tasks, etc.)
1067 * @param session for which session should we do this
1070 process_pending_messages (struct Session *session)
1072 struct PendingMessage *pm;
1074 GNUNET_assert(NULL != session->client);
1075 if (NULL != session->transmit_handle)
1077 if (NULL == (pm = session->pending_messages_head))
1080 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (
1081 session->client, pm->message_size,
1082 GNUNET_TIME_absolute_get_remaining (pm->timeout), &do_transmit, session);
1087 * Closure for #session_it().
1089 struct FindSessionContext
1092 * Session we are looking for.
1097 * Set to #GNUNET_OK if we found the session.
1103 * Function called to check if a session is in our maps.
1105 * @param cls the `struct FindSessionContext`
1106 * @param key peer identity
1107 * @param value session in the map
1108 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1111 session_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1113 struct FindSessionContext *res = cls;
1114 struct Session *session = value;
1116 if (res->s == session)
1118 res->res = GNUNET_OK;
1125 * Check that the given session is known to the plugin and
1126 * is in one of our maps.
1128 * @param plugin the plugin to check against
1129 * @param session the session to check
1130 * @return #GNUNET_OK if all is well, #GNUNET_SYSERR if the session is invalid
1133 find_session (struct Plugin *plugin, struct Session *session)
1135 struct FindSessionContext session_map_res;
1136 struct FindSessionContext nat_map_res;
1138 session_map_res.s = session;
1139 session_map_res.res = GNUNET_SYSERR;
1140 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it,
1142 if (GNUNET_SYSERR != session_map_res.res)
1144 nat_map_res.s = session;
1145 nat_map_res.res = GNUNET_SYSERR;
1146 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it,
1148 if (GNUNET_SYSERR != nat_map_res.res)
1151 return GNUNET_SYSERR;
1156 * Function that can be used by the transport service to transmit
1157 * a message using the plugin. Note that in the case of a
1158 * peer disconnecting, the continuation MUST be called
1159 * prior to the disconnect notification itself. This function
1160 * will be called with this peer's HELLO message to initiate
1161 * a fresh connection to another peer.
1163 * @param cls closure
1164 * @param session which session must be used
1165 * @param msgbuf the message to transmit
1166 * @param msgbuf_size number of bytes in 'msgbuf'
1167 * @param priority how important is the message (most plugins will
1168 * ignore message priority and just FIFO)
1169 * @param to how long to wait at most for the transmission (does not
1170 * require plugins to discard the message after the timeout,
1171 * just advisory for the desired delay; most plugins will ignore
1173 * @param cont continuation to call once the message has
1174 * been transmitted (or if the transport is ready
1175 * for the next transmission call; or if the
1176 * peer disconnected...); can be NULL
1177 * @param cont_cls closure for @a cont
1178 * @return number of bytes used (on the physical network, with overheads);
1179 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1180 * and does NOT mean that the message was not transmitted (DV)
1183 tcp_plugin_send (void *cls, struct Session *session, const char *msgbuf,
1184 size_t msgbuf_size, unsigned int priority, struct GNUNET_TIME_Relative to,
1185 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1187 struct Plugin * plugin = cls;
1188 struct PendingMessage *pm;
1191 if (GNUNET_SYSERR == find_session (plugin, session))
1193 LOG(GNUNET_ERROR_TYPE_ERROR, _("Trying to send with invalid session %p\n"));
1195 return GNUNET_SYSERR;
1198 /* create new message entry */
1199 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1200 pm->msg = (const char *) &pm[1];
1201 memcpy (&pm[1], msgbuf, msgbuf_size);
1202 pm->message_size = msgbuf_size;
1203 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1204 pm->transmit_cont = cont;
1205 pm->transmit_cont_cls = cont_cls;
1207 LOG(GNUNET_ERROR_TYPE_DEBUG,
1208 "Asked to transmit %u bytes to `%s', added message to list.\n",
1209 msgbuf_size, GNUNET_i2s (&session->target));
1212 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1213 &session->target, session))
1215 GNUNET_assert(NULL != session->client);
1216 GNUNET_SERVER_client_set_timeout (session->client,
1217 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1218 GNUNET_STATISTICS_update (plugin->env->stats,
1219 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1222 /* append pm to pending_messages list */
1223 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1224 session->pending_messages_tail, pm);
1226 process_pending_messages (session);
1230 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1231 &session->target, session))
1233 LOG(GNUNET_ERROR_TYPE_DEBUG,
1234 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1235 GNUNET_i2s (&session->target));
1236 GNUNET_STATISTICS_update (plugin->env->stats,
1237 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1240 /* append pm to pending_messages list */
1241 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1242 session->pending_messages_tail, pm);
1247 LOG(GNUNET_ERROR_TYPE_ERROR, "Invalid session %p\n", session);
1249 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1252 return GNUNET_SYSERR; /* session does not exist here */
1257 * Closure for #session_lookup_it().
1262 * Address we are looking for.
1264 const struct GNUNET_HELLO_Address *address;
1267 * Where to store the session (if we found it).
1269 struct Session *result;
1274 * Look for a session by address.
1276 * @param cls the `struct SessionItCtx`
1278 * @param value a `struct Session`
1279 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1282 session_lookup_it (void *cls, const struct GNUNET_PeerIdentity *key,
1285 struct SessionItCtx * si_ctx = cls;
1286 struct Session * session = value;
1288 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1290 /* Found existing session */
1291 si_ctx->result = session;
1296 * Task cleaning up a NAT connection attempt after timeout
1298 * @param cls the `struct Session`
1299 * @param tc scheduler context (unused)
1302 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1304 struct Session *session = cls;
1306 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1307 LOG(GNUNET_ERROR_TYPE_DEBUG,
1308 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1309 GNUNET_i2s (&session->target),
1310 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
1311 tcp_disconnect_session (session->plugin, session);
1315 tcp_plugin_update_session_timeout (void *cls,
1316 const struct GNUNET_PeerIdentity *peer, struct Session *session)
1318 struct Plugin *plugin = cls;
1320 if (GNUNET_SYSERR == find_session (plugin, session))
1322 reschedule_session_timeout (session);
1326 * Task to signal the server that we can continue
1327 * receiving from the TCP client now.
1329 * @param cls the `struct Session*`
1330 * @param tc task context (unused)
1333 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1335 struct Session *session = cls;
1337 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1338 reschedule_session_timeout (session);
1340 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1343 static void tcp_plugin_update_inbound_delay (void *cls,
1344 const struct GNUNET_PeerIdentity *peer,
1345 struct Session *session,
1346 struct GNUNET_TIME_Relative delay)
1348 if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
1351 LOG(GNUNET_ERROR_TYPE_DEBUG,
1352 "New inbound delay %llu us\n",delay.rel_value_us);
1354 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1355 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1356 &delayed_done, session);
1361 * Create a new session to transmit data to the target
1362 * This session will used to send data to this peer and the plugin will
1363 * notify us by calling the env->session_end function
1365 * @param cls closure
1366 * @param address the address to use
1367 * @return the session if the address is valid, NULL otherwise
1369 static struct Session *
1370 tcp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
1372 struct Plugin *plugin = cls;
1373 struct Session *session = NULL;
1377 struct GNUNET_CONNECTION_Handle *sa;
1378 struct sockaddr_in a4;
1379 struct sockaddr_in6 a6;
1380 const struct IPv4TcpAddress *t4;
1381 const struct IPv6TcpAddress *t6;
1382 struct GNUNET_ATS_Information ats;
1383 unsigned int is_natd = GNUNET_NO;
1386 addrlen = address->address_length;
1387 LOG(GNUNET_ERROR_TYPE_DEBUG,
1388 "Trying to get session for `%s' address of peer `%s'\n",
1389 tcp_address_to_string(NULL, address->address, address->address_length),
1390 GNUNET_i2s (&address->peer));
1392 if (GNUNET_HELLO_address_check_option(address, GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1398 /* look for existing session */
1399 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1402 struct SessionItCtx si_ctx;
1404 si_ctx.address = address;
1405 si_ctx.result = NULL;
1407 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1408 &address->peer, &session_lookup_it, &si_ctx);
1409 if (si_ctx.result != NULL )
1411 session = si_ctx.result;
1412 LOG(GNUNET_ERROR_TYPE_DEBUG,
1413 "Found existing session for `%s' address `%s' session %p\n",
1414 GNUNET_i2s (&address->peer),
1415 tcp_address_to_string(NULL, address->address, address->address_length),
1419 LOG(GNUNET_ERROR_TYPE_DEBUG,
1420 "Existing sessions did not match address `%s' or peer `%s'\n",
1421 tcp_address_to_string(NULL, address->address, address->address_length),
1422 GNUNET_i2s (&address->peer));
1425 if (addrlen == sizeof(struct IPv6TcpAddress))
1427 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1428 t6 = address->address;
1430 memset (&a6, 0, sizeof(a6));
1431 #if HAVE_SOCKADDR_IN_SIN_LEN
1432 a6.sin6_len = sizeof (a6);
1434 a6.sin6_family = AF_INET6;
1435 a6.sin6_port = t6->t6_port;
1436 if (t6->t6_port == 0)
1437 is_natd = GNUNET_YES;
1438 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1442 else if (addrlen == sizeof(struct IPv4TcpAddress))
1444 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1445 t4 = address->address;
1447 memset (&a4, 0, sizeof(a4));
1448 #if HAVE_SOCKADDR_IN_SIN_LEN
1449 a4.sin_len = sizeof (a4);
1451 a4.sin_family = AF_INET;
1452 a4.sin_port = t4->t4_port;
1453 if (t4->t4_port == 0)
1454 is_natd = GNUNET_YES;
1455 a4.sin_addr.s_addr = t4->ipv4_addr;
1461 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1462 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1466 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1468 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1470 /* NAT client only works with IPv4 addresses */
1474 if (plugin->cur_connections >= plugin->max_connections)
1480 if ((is_natd == GNUNET_YES)
1482 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1485 /* Only do one NAT punch attempt per peer identity */
1489 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1490 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1493 LOG (GNUNET_ERROR_TYPE_DEBUG,
1494 "Found valid IPv4 NAT address (creating session)!\n");
1495 session = create_session (plugin, address, NULL, GNUNET_YES);
1496 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1499 session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1500 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1501 &nat_connect_timeout, session);
1502 GNUNET_assert(session != NULL);
1503 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1504 &session->target, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1506 LOG(GNUNET_ERROR_TYPE_DEBUG,
1507 "Created NAT WAIT connection to `%4s' at `%s'\n",
1508 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1510 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1514 LOG(GNUNET_ERROR_TYPE_DEBUG,
1515 "Running NAT client for `%4s' at `%s' failed\n",
1516 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1517 tcp_disconnect_session (plugin, session);
1522 /* create new outbound session */
1523 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1524 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1527 LOG(GNUNET_ERROR_TYPE_DEBUG,
1528 "Failed to create connection to `%4s' at `%s'\n",
1529 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1532 plugin->cur_connections++;
1533 if (plugin->cur_connections == plugin->max_connections)
1534 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1536 LOG(GNUNET_ERROR_TYPE_DEBUG,
1537 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1538 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1540 session = create_session (plugin, address,
1541 GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO);
1542 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1544 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1545 GNUNET_SERVER_client_set_user_context(session->client, session);
1546 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1547 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1548 LOG(GNUNET_ERROR_TYPE_DEBUG,
1549 "Creating new session for `%s' address `%s' session %p\n",
1550 GNUNET_i2s (&address->peer),
1551 tcp_address_to_string(NULL, address->address, address->address_length),
1553 /* Send TCP Welcome */
1554 process_pending_messages (session);
1560 session_disconnect_it (void *cls, const struct GNUNET_PeerIdentity *key,
1563 struct Plugin *plugin = cls;
1564 struct Session *session = value;
1566 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
1567 ("# transport-service disconnect requests for TCP"), 1, GNUNET_NO);
1568 tcp_disconnect_session (plugin, session);
1573 * Function that can be called to force a disconnect from the
1574 * specified neighbour. This should also cancel all previously
1575 * scheduled transmissions. Obviously the transmission may have been
1576 * partially completed already, which is OK. The plugin is supposed
1577 * to close the connection (if applicable) and no longer call the
1578 * transmit continuation(s).
1580 * Finally, plugin MUST NOT call the services's receive function to
1581 * notify the service that the connection to the specified target was
1582 * closed after a getting this call.
1584 * @param cls closure
1585 * @param target peer for which the last transmission is
1589 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1591 struct Plugin *plugin = cls;
1593 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s'\n",
1594 GNUNET_i2s (target));
1595 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1596 &session_disconnect_it, plugin);
1597 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1598 &session_disconnect_it, plugin);
1602 * Running pretty printers: head
1604 static struct PrettyPrinterContext *ppc_dll_head;
1607 * Running pretty printers: tail
1609 static struct PrettyPrinterContext *ppc_dll_tail;
1612 * Context for address to string conversion.
1614 struct PrettyPrinterContext
1619 struct PrettyPrinterContext *next;
1624 struct PrettyPrinterContext *prev;
1629 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1634 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1637 * Function to call with the result.
1639 GNUNET_TRANSPORT_AddressStringCallback asc;
1642 * Clsoure for 'asc'.
1647 * Port to add after the IP address.
1663 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1665 struct PrettyPrinterContext *ppc = cls;
1667 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1668 if (NULL != ppc->resolver_handle)
1670 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1671 ppc->resolver_handle = NULL;
1673 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1678 * Append our port and forward the result.
1680 * @param cls the 'struct PrettyPrinterContext*'
1681 * @param hostname hostname part of the address
1684 append_port (void *cls, const char *hostname)
1686 struct PrettyPrinterContext *ppc = cls;
1687 struct PrettyPrinterContext *cur;
1690 if (NULL == hostname)
1692 ppc->asc (ppc->asc_cls, NULL );
1693 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1694 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1695 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1696 ppc->resolver_handle = NULL;
1700 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1709 if (GNUNET_YES == ppc->ipv6)
1710 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname,
1713 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname,
1715 ppc->asc (ppc->asc_cls, ret);
1720 * Convert the transports address to a nice, human-readable
1723 * @param cls closure
1724 * @param type name of the transport that generated the address
1725 * @param addr one of the addresses of the host, NULL for the last address
1726 * the specific address format depends on the transport
1727 * @param addrlen length of the address
1728 * @param numeric should (IP) addresses be displayed in numeric form?
1729 * @param timeout after how long should we give up?
1730 * @param asc function to call on each string
1731 * @param asc_cls closure for asc
1734 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1735 const void *addr, size_t addrlen, int numeric,
1736 struct GNUNET_TIME_Relative timeout,
1737 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
1739 struct PrettyPrinterContext *ppc;
1742 struct sockaddr_in a4;
1743 struct sockaddr_in6 a6;
1744 const struct IPv4TcpAddress *t4;
1745 const struct IPv6TcpAddress *t6;
1749 if (addrlen == sizeof(struct IPv6TcpAddress))
1752 memset (&a6, 0, sizeof(a6));
1753 a6.sin6_family = AF_INET6;
1754 a6.sin6_port = t6->t6_port;
1755 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1756 port = ntohs (t6->t6_port);
1757 options = ntohl (t6->options);
1761 else if (addrlen == sizeof(struct IPv4TcpAddress))
1764 memset (&a4, 0, sizeof(a4));
1765 a4.sin_family = AF_INET;
1766 a4.sin_port = t4->t4_port;
1767 a4.sin_addr.s_addr = t4->ipv4_addr;
1768 port = ntohs (t4->t4_port);
1769 options = ntohl (t4->options);
1775 /* invalid address */
1776 LOG (GNUNET_ERROR_TYPE_ERROR,
1777 "Trying to print invalid `%s' address with size %u\n", type, addrlen);
1778 asc (asc_cls, NULL );
1781 ppc = GNUNET_new (struct PrettyPrinterContext);
1782 if (addrlen == sizeof(struct IPv6TcpAddress))
1783 ppc->ipv6 = GNUNET_YES;
1785 ppc->ipv6 = GNUNET_NO;
1787 ppc->asc_cls = asc_cls;
1789 ppc->options = options;
1790 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (
1791 GNUNET_TIME_relative_multiply (timeout, 2), &ppc_cancel_task, ppc);
1792 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1793 timeout, &append_port, ppc);
1794 if (NULL != ppc->resolver_handle)
1796 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1797 GNUNET_CONTAINER_DLL_insert(ppc_dll_head, ppc_dll_tail, ppc);
1807 * Check if the given port is plausible (must be either our listen
1808 * port or our advertised port), or any port if we are behind NAT
1809 * and do not have a port open. If it is neither, we return
1812 * @param plugin global variables
1813 * @param in_port port number to check
1814 * @return #GNUNET_OK if port is either open_port or adv_port
1817 check_port (struct Plugin *plugin, uint16_t in_port)
1819 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1821 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)))
1850 return GNUNET_SYSERR;
1853 if (addrlen == sizeof(struct IPv4TcpAddress))
1855 v4 = (struct IPv4TcpAddress *) addr;
1856 if (0 != memcmp (&v4->options, &myoptions, sizeof(myoptions)))
1859 return GNUNET_SYSERR;
1861 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1862 return GNUNET_SYSERR;
1864 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1865 sizeof(struct in_addr)))
1866 return GNUNET_SYSERR;
1870 v6 = (struct IPv6TcpAddress *) addr;
1871 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1874 return GNUNET_SYSERR;
1876 if (0 != memcmp (&v6->options, &myoptions, sizeof(myoptions)))
1879 return GNUNET_SYSERR;
1881 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1882 return GNUNET_SYSERR;
1884 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1885 sizeof(struct in6_addr)))
1886 return GNUNET_SYSERR;
1892 * We've received a nat probe from this peer via TCP. Finish
1893 * creating the client session and resume sending of queued
1896 * @param cls closure
1897 * @param client identification of the client
1898 * @param message the actual message
1901 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1902 const struct GNUNET_MessageHeader *message)
1904 struct Plugin *plugin = cls;
1905 struct Session *session;
1906 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1909 struct IPv4TcpAddress *t4;
1910 struct IPv6TcpAddress *t6;
1911 const struct sockaddr_in *s4;
1912 const struct sockaddr_in6 *s6;
1914 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1915 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1916 * a connection to this peer by running gnunet-nat-client. This peer
1917 * received the punch message and now wants us to use the new connection
1918 * as the default for that peer. Do so and then send a WELCOME message
1919 * so we can really be connected!
1921 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1928 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1929 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1930 sizeof(struct GNUNET_PeerIdentity)))
1932 /* refuse connections from ourselves */
1933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1937 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
1938 &tcp_nat_probe->clientIdentity);
1939 if (session == NULL )
1941 LOG(GNUNET_ERROR_TYPE_DEBUG, "Did NOT find session for NAT probe!\n");
1942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1945 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found session for NAT probe!\n");
1947 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK )
1949 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1950 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1953 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1957 tcp_disconnect_session (plugin, session);
1961 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
1962 GNUNET_SERVER_client_set_user_context(client, session);
1963 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1964 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1965 session->last_activity = GNUNET_TIME_absolute_get ();
1966 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
1967 GNUNET_a2s (vaddr, alen));
1968 switch (((const struct sockaddr *) vaddr)->sa_family)
1972 t4 = GNUNET_new (struct IPv4TcpAddress);
1973 t4->options = htonl(0);
1974 t4->t4_port = s4->sin_port;
1975 t4->ipv4_addr = s4->sin_addr.s_addr;
1976 session->address = GNUNET_HELLO_address_allocate (
1977 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
1978 sizeof(struct IPv4TcpAddress),
1979 GNUNET_HELLO_ADDRESS_INFO_NONE);
1983 t6 = GNUNET_new (struct IPv6TcpAddress);
1984 t6->options = htonl(0);
1985 t6->t6_port = s6->sin6_port;
1986 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
1987 session->address = GNUNET_HELLO_address_allocate (
1988 &tcp_nat_probe->clientIdentity,
1989 PLUGIN_NAME, &t6, sizeof(struct IPv6TcpAddress),
1990 GNUNET_HELLO_ADDRESS_INFO_NONE);
1994 LOG(GNUNET_ERROR_TYPE_DEBUG, "Bad address for incoming connection!\n");
1996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1997 tcp_disconnect_session (plugin, session);
2001 GNUNET_break(NULL == session->client);
2002 GNUNET_SERVER_client_keep (client);
2003 session->client = client;
2004 GNUNET_STATISTICS_update (plugin->env->stats,
2005 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2006 process_pending_messages (session);
2007 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2011 * We've received a welcome from this peer via TCP. Possibly create a
2012 * fresh client record and send back our welcome.
2014 * @param cls closure
2015 * @param client identification of the client
2016 * @param message the actual message
2019 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2020 const struct GNUNET_MessageHeader *message)
2022 struct Plugin *plugin = cls;
2023 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2024 struct GNUNET_HELLO_Address *address;
2025 struct Session *session;
2028 struct IPv4TcpAddress t4;
2029 struct IPv6TcpAddress t6;
2030 const struct sockaddr_in *s4;
2031 const struct sockaddr_in6 *s6;
2032 struct GNUNET_ATS_Information ats;
2035 if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
2036 sizeof(struct GNUNET_PeerIdentity)))
2038 /* refuse connections from ourselves */
2039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2040 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2042 LOG(GNUNET_ERROR_TYPE_WARNING,
2043 "Received %s message from my own identity `%4s' on address `%s'\n",
2044 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2045 GNUNET_a2s (vaddr, alen));
2052 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
2053 GNUNET_i2s (&wm->clientIdentity), client);
2054 GNUNET_STATISTICS_update (plugin->env->stats,
2055 gettext_noop ("# TCP WELCOME messages received"), 1, GNUNET_NO);
2056 session = lookup_session_by_client (plugin, client);
2057 if (NULL != session)
2059 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2061 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for peer `%s'\n",
2062 session, GNUNET_a2s (vaddr, alen));
2068 GNUNET_SERVER_client_keep (client);
2069 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2070 plugin->cur_connections++;
2071 if (plugin->cur_connections == plugin->max_connections)
2072 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2074 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2076 if (alen == sizeof(struct sockaddr_in))
2079 memset (&t4, '\0', sizeof (t4));
2080 t4.options = htonl (0);
2081 t4.t4_port = s4->sin_port;
2082 t4.ipv4_addr = s4->sin_addr.s_addr;
2083 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2084 PLUGIN_NAME, &t4, sizeof(t4),
2085 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2087 else if (alen == sizeof(struct sockaddr_in6))
2090 memset (&t6, '\0', sizeof (t6));
2091 t6.options = htonl (0);
2092 t6.t6_port = s6->sin6_port;
2093 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2094 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2095 PLUGIN_NAME, &t6, sizeof (t6),
2096 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2101 GNUNET_free_non_null (vaddr);
2104 session = create_session (plugin, address, client, GNUNET_NO);
2105 GNUNET_HELLO_address_free (address);
2106 ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2107 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
2109 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n",
2110 GNUNET_HELLO_address_check_option (session->address,
2111 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "", session,
2112 tcp_address_to_string(NULL, (void *) session->address->address,
2113 session->address->address_length),
2116 GNUNET_SERVER_client_set_user_context(session->client, session);
2117 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2118 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2120 /* Notify transport and ATS about new session */
2121 plugin->env->session_start (NULL, session->address, session, &ats, 1);
2125 LOG(GNUNET_ERROR_TYPE_DEBUG,
2126 "Did not obtain TCP socket address for incoming connection\n");
2132 if (session->expecting_welcome != GNUNET_YES)
2135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2139 session->last_activity = GNUNET_TIME_absolute_get ();
2140 session->expecting_welcome = GNUNET_NO;
2142 process_pending_messages (session);
2143 GNUNET_SERVER_client_set_timeout (client,
2144 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2150 * We've received data for this peer via TCP. Unbox,
2151 * compute latency and forward.
2153 * @param cls closure
2154 * @param client identification of the client
2155 * @param message the actual message
2158 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2159 const struct GNUNET_MessageHeader *message)
2161 struct Plugin *plugin = cls;
2162 struct Session *session;
2163 struct GNUNET_TIME_Relative delay;
2166 type = ntohs (message->type);
2167 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2168 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2170 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2171 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2174 session = lookup_session_by_client (plugin, client);
2175 if (NULL == session)
2177 /* No inbound session found */
2181 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2182 LOG(GNUNET_ERROR_TYPE_ERROR,
2183 "Received unexpected %u bytes of type %u from `%s'\n",
2184 (unsigned int ) ntohs (message->size),
2185 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2188 GNUNET_free_non_null(vaddr);
2191 else if (GNUNET_YES == session->expecting_welcome)
2193 /* Session is expecting WELCOME message */
2197 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2198 LOG(GNUNET_ERROR_TYPE_ERROR,
2199 "Received unexpected %u bytes of type %u from `%s'\n",
2200 (unsigned int ) ntohs (message->size),
2201 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2204 GNUNET_free_non_null(vaddr);
2208 session->last_activity = GNUNET_TIME_absolute_get ();
2209 LOG(GNUNET_ERROR_TYPE_DEBUG,
2210 "Passing %u bytes of type %u from `%4s' to transport service.\n",
2211 (unsigned int ) ntohs (message->size),
2212 (unsigned int ) ntohs (message->type), GNUNET_i2s (&session->target));
2214 GNUNET_STATISTICS_update (plugin->env->stats,
2215 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2217 struct GNUNET_ATS_Information distance;
2219 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2220 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2221 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2224 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2225 &session->target, session));
2227 delay = plugin->env->receive (plugin->env->cls, session->address, session, message);
2228 plugin->env->update_address_metrics (plugin->env->cls, session->address,
2229 session, &distance, 1);
2230 reschedule_session_timeout (session);
2231 if (0 == delay.rel_value_us)
2233 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2237 LOG(GNUNET_ERROR_TYPE_DEBUG, "Throttling receiving from `%s' for %s\n",
2238 GNUNET_i2s (&session->target),
2239 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2240 GNUNET_SERVER_disable_receive_done_warning (client);
2241 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2242 &delayed_done, session);
2247 * Functions with this signature are called whenever a peer
2248 * is disconnected on the network level.
2250 * @param cls closure
2251 * @param client identification of the client
2254 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2256 struct Plugin *plugin = cls;
2257 struct Session *session;
2259 if (client == NULL )
2261 session = lookup_session_by_client (plugin, client);
2262 if (session == NULL )
2263 return; /* unknown, nothing to do */
2264 LOG(GNUNET_ERROR_TYPE_DEBUG,
2265 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2266 GNUNET_i2s (&session->target),
2267 tcp_address_to_string (session->plugin, session->address->address,
2268 session->address->address_length));
2270 if (plugin->cur_connections == plugin->max_connections)
2271 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2273 if (plugin->cur_connections < 1)
2276 plugin->cur_connections--;
2278 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2279 ("# network-level TCP disconnect events"), 1, GNUNET_NO);
2280 tcp_disconnect_session (plugin, session);
2284 * We can now send a probe message, copy into buffer to really send.
2286 * @param cls closure, a struct TCPProbeContext
2287 * @param size max size to copy
2288 * @param buf buffer to copy message to
2289 * @return number of bytes copied into buf
2292 notify_send_probe (void *cls, size_t size, void *buf)
2294 struct TCPProbeContext *tcp_probe_ctx = cls;
2295 struct Plugin *plugin = tcp_probe_ctx->plugin;
2298 tcp_probe_ctx->transmit_handle = NULL;
2299 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2303 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2304 GNUNET_free(tcp_probe_ctx);
2307 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2308 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2309 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2310 tcp_probe_ctx->sock);
2311 ret = sizeof(tcp_probe_ctx->message);
2312 GNUNET_free(tcp_probe_ctx);
2317 * Function called by the NAT subsystem suggesting another peer wants
2318 * to connect to us via connection reversal. Try to connect back to the
2321 * @param cls closure
2322 * @param addr address to try
2323 * @param addrlen number of bytes in @a addr
2326 try_connection_reversal (void *cls, const struct sockaddr *addr,
2329 struct Plugin *plugin = cls;
2330 struct GNUNET_CONNECTION_Handle *sock;
2331 struct TCPProbeContext *tcp_probe_ctx;
2334 * We have received an ICMP response, ostensibly from a peer
2335 * that wants to connect to us! Send a message to establish a connection.
2337 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2340 /* failed for some odd reason (out of sockets?); ignore attempt */
2344 /* FIXME: do we need to track these probe context objects so that
2345 * we can clean them up on plugin unload? */
2346 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2347 tcp_probe_ctx->message.header.size = htons (
2348 sizeof(struct TCP_NAT_ProbeMessage));
2349 tcp_probe_ctx->message.header.type = htons (
2350 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2351 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2352 sizeof(struct GNUNET_PeerIdentity));
2353 tcp_probe_ctx->plugin = plugin;
2354 tcp_probe_ctx->sock = sock;
2355 GNUNET_CONTAINER_DLL_insert(plugin->probe_head, plugin->probe_tail,
2357 tcp_probe_ctx->transmit_handle = GNUNET_CONNECTION_notify_transmit_ready (
2358 sock, ntohs (tcp_probe_ctx->message.header.size),
2359 GNUNET_TIME_UNIT_FOREVER_REL, ¬ify_send_probe, tcp_probe_ctx);
2364 * Function obtain the network type for a session
2366 * @param cls closure ('struct Plugin*')
2367 * @param session the session
2368 * @return the network type in HBO or #GNUNET_SYSERR
2370 static enum GNUNET_ATS_Network_Type
2371 tcp_get_network (void *cls, struct Session *session)
2373 struct Plugin * plugin = cls;
2374 GNUNET_assert (NULL != plugin);
2375 GNUNET_assert (NULL != session);
2376 if (GNUNET_SYSERR == find_session (plugin,session))
2377 return GNUNET_ATS_NET_UNSPECIFIED;
2378 return session->ats_address_network_type;
2382 * Entry point for the plugin.
2384 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2385 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2388 libgnunet_plugin_transport_tcp_init (void *cls)
2390 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = { {
2391 &handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2392 sizeof(struct WelcomeMessage) }, { &handle_tcp_nat_probe, NULL,
2393 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2394 sizeof(struct TCP_NAT_ProbeMessage) }, { &handle_tcp_data, NULL,
2395 GNUNET_MESSAGE_TYPE_ALL, 0 }, { NULL, NULL, 0, 0 } };
2396 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2397 struct GNUNET_TRANSPORT_PluginFunctions *api;
2398 struct Plugin *plugin;
2399 struct GNUNET_SERVICE_Context *service;
2400 unsigned long long aport;
2401 unsigned long long bport;
2402 unsigned long long max_connections;
2404 struct GNUNET_TIME_Relative idle_timeout;
2407 struct sockaddr **addrs;
2408 socklen_t *addrlens;
2410 if (NULL == env->receive)
2412 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2413 initialze the plugin or the API */
2414 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2416 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2417 api->address_to_string = &tcp_address_to_string;
2418 api->string_to_address = &tcp_string_to_address;
2422 GNUNET_assert(NULL != env->cfg);
2424 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2425 "MAX_CONNECTIONS", &max_connections))
2426 max_connections = 128;
2430 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2431 "PORT", &bport)) || (bport > 65535)
2433 == GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2434 "ADVERTISED-PORT", &aport)) && (aport > 65535)))
2436 LOG(GNUNET_ERROR_TYPE_ERROR,
2437 _("Require valid port number for service `%s' in configuration!\n"),
2447 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg,
2448 GNUNET_SERVICE_OPTION_NONE);
2449 if (service == NULL )
2451 LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to start service.\n"));
2458 /* Initialize my flags */
2461 plugin = GNUNET_new (struct Plugin);
2462 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2464 plugin->max_connections = max_connections;
2465 plugin->cur_connections = 0;
2466 plugin->open_port = bport;
2467 plugin->adv_port = aport;
2469 plugin->lsock = NULL;
2470 if ((service != NULL )&&
2473 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2475 for (ret = ret_s-1; ret >= 0; ret--)
2476 LOG (GNUNET_ERROR_TYPE_INFO,
2477 "Binding to address `%s'\n",
2478 GNUNET_a2s (addrs[ret], addrlens[ret]));
2480 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2481 (const struct sockaddr **) addrs, addrlens,
2482 &tcp_nat_port_map_callback,
2483 &try_connection_reversal, plugin);
2484 for (ret = ret_s -1; ret >= 0; ret--)
2486 GNUNET_assert (addrs[ret] != NULL);
2487 GNUNET_free (addrs[ret]);
2489 GNUNET_free_non_null (addrs);
2490 GNUNET_free_non_null (addrlens);
2494 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2495 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2496 &try_connection_reversal, plugin);
2498 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2500 api->send = &tcp_plugin_send;
2501 api->get_session = &tcp_plugin_get_session;
2503 api->disconnect_session = &tcp_disconnect_session;
2504 api->query_keepalive_factor = &tcp_query_keepalive_factor;
2505 api->disconnect_peer = &tcp_plugin_disconnect;
2506 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2507 api->check_address = &tcp_plugin_check_address;
2508 api->address_to_string = &tcp_address_to_string;
2509 api->string_to_address = &tcp_string_to_address;
2510 api->get_network = &tcp_get_network;
2511 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2512 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2513 plugin->service = service;
2514 if (NULL != service)
2516 plugin->server = GNUNET_SERVICE_get_server (service);
2521 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2522 "TIMEOUT", &idle_timeout))
2524 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "transport-tcp",
2526 if (plugin->nat != NULL )
2527 GNUNET_NAT_unregister (plugin->nat);
2528 GNUNET_free(plugin);
2532 plugin->server = GNUNET_SERVER_create_with_sockets (
2533 &plugin_tcp_access_check, plugin, NULL, idle_timeout, GNUNET_YES);
2535 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2536 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2538 i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);
2540 plugin->handlers[i].callback_cls = plugin;
2542 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2543 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2544 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2547 LOG(GNUNET_ERROR_TYPE_INFO, _("TCP transport listening on port %llu\n"),
2550 LOG(GNUNET_ERROR_TYPE_INFO,
2551 _("TCP transport not listening on any port (client only)\n"));
2553 LOG(GNUNET_ERROR_TYPE_INFO,
2554 _("TCP transport advertises itself as being on port %llu\n"), aport);
2555 /* Initially set connections to 0 */
2556 GNUNET_assert(NULL != plugin->env->stats);
2557 GNUNET_STATISTICS_set (plugin->env->stats,
2558 gettext_noop ("# TCP sessions active"), 0, GNUNET_NO);
2563 * Exit point from the plugin.
2565 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2569 libgnunet_plugin_transport_tcp_done (void *cls)
2571 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2572 struct Plugin *plugin = api->cls;
2573 struct TCPProbeContext *tcp_probe;
2574 struct PrettyPrinterContext *cur;
2575 struct PrettyPrinterContext *next;
2582 LOG(GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2584 /* Removing leftover sessions */
2585 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2586 &session_disconnect_it, plugin);
2587 /* Removing leftover NAT sessions */
2588 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2589 &session_disconnect_it, plugin);
2591 next = ppc_dll_head;
2592 for (cur = next; NULL != cur; cur = next)
2595 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, cur);
2596 if (NULL != cur->resolver_handle)
2597 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2598 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2603 if (plugin->service != NULL )
2604 GNUNET_SERVICE_stop (plugin->service);
2606 GNUNET_SERVER_destroy (plugin->server);
2607 GNUNET_free(plugin->handlers);
2608 if (plugin->nat != NULL )
2609 GNUNET_NAT_unregister (plugin->nat);
2610 while (NULL != (tcp_probe = plugin->probe_head))
2612 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2614 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2615 GNUNET_free(tcp_probe);
2617 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2618 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2619 GNUNET_free(plugin);
2624 /* end of plugin_transport_tcp.c */