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 address = GNUNET_HELLO_address_allocate (plugin->env->my_identity,
504 PLUGIN_NAME, arg, args, GNUNET_HELLO_ADDRESS_INFO_NONE);
505 plugin->env->notify_address (plugin->env->cls, add_remove, address);
506 GNUNET_HELLO_address_free(address);
510 * Function called for a quick conversion of the binary address to
511 * a numeric address. Note that the caller must not free the
512 * address and that the next call to this function is allowed
513 * to override the address again.
515 * @param cls closure (`struct Plugin*`)
516 * @param addr binary address
517 * @param addrlen length of the address
518 * @return string representing the same address
521 tcp_address_to_string (void *cls, const void *addr, size_t addrlen)
523 static char rbuf[INET6_ADDRSTRLEN + 12];
524 char buf[INET6_ADDRSTRLEN];
528 const struct IPv4TcpAddress *t4;
529 const struct IPv6TcpAddress *t6;
536 case sizeof(struct IPv6TcpAddress):
539 port = ntohs (t6->t6_port);
540 options = ntohl (t6->options);
541 memcpy (&a6, &t6->ipv6_addr, sizeof(a6));
544 case sizeof(struct IPv4TcpAddress):
547 port = ntohs (t4->t4_port);
548 options = ntohl (t4->options);
549 memcpy (&a4, &t4->ipv4_addr, sizeof(a4));
554 GNUNET_snprintf (rbuf, sizeof(rbuf), "%s",
555 TRANSPORT_SESSION_INBOUND_STRING);
559 LOG(GNUNET_ERROR_TYPE_WARNING, _("Unexpected address length: %u bytes\n"),
560 (unsigned int ) addrlen);
563 if (NULL == inet_ntop (af, sb, buf, INET6_ADDRSTRLEN))
565 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
568 GNUNET_snprintf (rbuf, sizeof(rbuf),
569 (af == AF_INET6) ? "%s.%u.[%s]:%u" : "%s.%u.%s:%u", PLUGIN_NAME, options,
575 * Function called to convert a string address to
578 * @param cls closure (`struct Plugin*`)
579 * @param addr string address
580 * @param addrlen length of the address
581 * @param buf location to store the buffer
582 * @param added location to store the number of bytes in the buffer.
583 * If the function returns #GNUNET_SYSERR, its contents are undefined.
584 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
587 tcp_string_to_address (void *cls, const char *addr, uint16_t addrlen,
588 void **buf, size_t *added)
590 struct sockaddr_storage socket_address;
596 /* Format tcp.options.address:port */
600 if ((NULL == addr) || (addrlen == 0))
603 return GNUNET_SYSERR;
605 if ('\0' != addr[addrlen - 1])
608 return GNUNET_SYSERR;
610 if (strlen (addr) != addrlen - 1)
613 return GNUNET_SYSERR;
615 plugin = GNUNET_strdup (addr);
616 optionstr = strchr (plugin, '.');
617 if (NULL == optionstr)
621 return GNUNET_SYSERR;
625 options = atol (optionstr);
626 address = strchr (optionstr, '.');
631 return GNUNET_SYSERR;
637 != GNUNET_STRINGS_to_address_ip (address, strlen (address),
642 return GNUNET_SYSERR;
646 switch (socket_address.ss_family)
650 struct IPv4TcpAddress *t4;
651 struct sockaddr_in *in4 = (struct sockaddr_in *) &socket_address;
652 t4 = GNUNET_new (struct IPv4TcpAddress);
653 t4->options = htonl (options);
654 t4->ipv4_addr = in4->sin_addr.s_addr;
655 t4->t4_port = in4->sin_port;
657 *added = sizeof(struct IPv4TcpAddress);
662 struct IPv6TcpAddress *t6;
663 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &socket_address;
664 t6 = GNUNET_new (struct IPv6TcpAddress);
665 t6->options = htonl (options);
666 t6->ipv6_addr = in6->sin6_addr;
667 t6->t6_port = in6->sin6_port;
669 *added = sizeof(struct IPv6TcpAddress);
673 return GNUNET_SYSERR;
678 * Closure for #session_lookup_by_client_it().
680 struct SessionClientCtx
683 * Client we are looking for.
685 const struct GNUNET_SERVER_Client *client;
688 * Session that was found.
694 session_lookup_by_client_it (void *cls, const struct GNUNET_PeerIdentity *key,
697 struct SessionClientCtx *sc_ctx = cls;
698 struct Session *s = value;
700 if (s->client == sc_ctx->client)
709 * Find the session handle for the given client.
710 * Currently uses both the hashmap and the client
711 * context, as the client context is new and the
712 * logic still needs to be tested.
714 * @param plugin the plugin
715 * @param client which client to find the session handle for
716 * @return NULL if no matching session exists
718 static struct Session *
719 lookup_session_by_client (struct Plugin *plugin,
720 struct GNUNET_SERVER_Client *client)
723 struct SessionClientCtx sc_ctx;
725 ret = GNUNET_SERVER_client_get_user_context (client, struct Session);
726 sc_ctx.client = client;
728 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
729 &session_lookup_by_client_it, &sc_ctx);
730 /* check both methods yield the same result */
731 GNUNET_break(ret == sc_ctx.ret);
736 * Functions with this signature are called whenever we need
737 * to close a session due to a disconnect or failure to
738 * establish a connection.
740 * @param cls the `struct Plugin`
741 * @param session session to close down
742 * @return #GNUNET_OK on success
745 tcp_disconnect_session (void *cls, struct Session *session)
747 struct Plugin *plugin = cls;
748 struct PendingMessage *pm;
750 LOG(GNUNET_ERROR_TYPE_DEBUG,
751 "Disconnecting session of peer `%s' address `%s'\n",
752 GNUNET_i2s (&session->target),
753 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
755 if (GNUNET_SCHEDULER_NO_TASK != session->timeout_task)
757 GNUNET_SCHEDULER_cancel (session->timeout_task);
758 session->timeout_task = GNUNET_SCHEDULER_NO_TASK;
762 == GNUNET_CONTAINER_multipeermap_remove (plugin->sessionmap,
763 &session->target, session))
765 GNUNET_STATISTICS_update (session->plugin->env->stats,
766 gettext_noop ("# TCP sessions active"), -1, GNUNET_NO);
771 GNUNET_YES == GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &session->target, session));
773 if (NULL != session->client)
774 GNUNET_SERVER_client_set_user_context(session->client, (void *) NULL);
777 if (NULL != session->transmit_handle)
779 GNUNET_SERVER_notify_transmit_ready_cancel (session->transmit_handle);
780 session->transmit_handle = NULL;
782 plugin->env->unregister_quota_notification (plugin->env->cls,
783 &session->target, PLUGIN_NAME, session);
784 session->plugin->env->session_end (session->plugin->env->cls,
785 session->address, session);
787 if (GNUNET_SCHEDULER_NO_TASK != session->nat_connection_timeout)
789 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
790 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
793 while (NULL != (pm = session->pending_messages_head))
795 LOG(GNUNET_ERROR_TYPE_DEBUG,
796 pm->transmit_cont != NULL ? "Could not deliver message to `%4s'.\n" : "Could not deliver message to `%4s', notifying.\n",
797 GNUNET_i2s (&session->target));
798 GNUNET_STATISTICS_update (session->plugin->env->stats,
799 gettext_noop ("# bytes currently in TCP buffers"),
800 -(int64_t) pm->message_size, GNUNET_NO);
801 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
802 ("# bytes discarded by TCP (disconnect)"), pm->message_size, GNUNET_NO);
803 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
804 session->pending_messages_tail, pm);
805 if (NULL != pm->transmit_cont)
806 pm->transmit_cont (pm->transmit_cont_cls, &session->target, GNUNET_SYSERR,
807 pm->message_size, 0);
810 if (session->receive_delay_task != GNUNET_SCHEDULER_NO_TASK )
812 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
813 if (NULL != session->client)
814 GNUNET_SERVER_receive_done (session->client, GNUNET_SYSERR);
816 if (NULL != session->client)
818 GNUNET_SERVER_client_disconnect (session->client);
819 GNUNET_SERVER_client_drop (session->client);
820 session->client = NULL;
822 GNUNET_HELLO_address_free (session->address);
823 GNUNET_assert(NULL == session->transmit_handle);
824 GNUNET_free(session);
829 * Function that is called to get the keepalive factor.
830 * GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
831 * calculate the interval between keepalive packets.
833 * @param cls closure with the `struct Plugin`
834 * @return keepalive factor
837 tcp_query_keepalive_factor (void *cls)
843 * Session was idle, so disconnect it
845 * @param cls the `struct Session` of the idle session
846 * @param tc scheduler context
849 session_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
851 struct Session *s = cls;
853 s->timeout_task = GNUNET_SCHEDULER_NO_TASK;
854 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
855 "Session %p was idle for %s, disconnecting\n", s,
856 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
857 /* call session destroy function */
858 tcp_disconnect_session (s->plugin, s);
862 * Increment session timeout due to activity
864 * @param s session to increment timeout for
867 reschedule_session_timeout (struct Session *s)
869 GNUNET_assert(GNUNET_SCHEDULER_NO_TASK != s->timeout_task);
870 GNUNET_SCHEDULER_cancel (s->timeout_task);
871 s->timeout_task = GNUNET_SCHEDULER_add_delayed (
872 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, s);
873 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
874 "Timeout rescheduled for session %p set to %s\n", s,
875 GNUNET_STRINGS_relative_time_to_string (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, GNUNET_YES));
879 * Create a new session. Also queues a welcome message.
881 * @param plugin the plugin
882 * @param address the address to create the session for
883 * @param client client to use, reference counter must have already been increased
884 * @param is_nat this a NAT session, we should wait for a client to
885 * connect to us from an address, then assign that to
887 * @return new session object
889 static struct Session *
890 create_session (struct Plugin *plugin,
891 const struct GNUNET_HELLO_Address *address,
892 struct GNUNET_SERVER_Client *client,
895 struct Session *session;
896 struct PendingMessage *pm;
897 struct WelcomeMessage welcome;
899 if (GNUNET_YES != is_nat)
900 GNUNET_assert(NULL != client);
902 GNUNET_assert(NULL == client);
904 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new session for peer `%4s'\n",
905 GNUNET_i2s (&address->peer));
906 session = GNUNET_new (struct Session);
907 session->last_activity = GNUNET_TIME_absolute_get ();
908 session->plugin = plugin;
909 session->is_nat = is_nat;
910 session->client = client;
911 session->address = GNUNET_HELLO_address_copy (address);
912 session->target = address->peer;
913 session->expecting_welcome = GNUNET_YES;
914 session->ats_address_network_type = GNUNET_ATS_NET_UNSPECIFIED;
915 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
916 sizeof (struct WelcomeMessage));
917 pm->msg = (const char *) &pm[1];
918 pm->message_size = sizeof(struct WelcomeMessage);
919 welcome.header.size = htons (sizeof(struct WelcomeMessage));
920 welcome.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
921 welcome.clientIdentity = *plugin->env->my_identity;
922 memcpy (&pm[1], &welcome, sizeof(welcome));
923 pm->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
924 GNUNET_STATISTICS_update (plugin->env->stats,
925 gettext_noop ("# bytes currently in TCP buffers"), pm->message_size,
927 GNUNET_CONTAINER_DLL_insert(session->pending_messages_head,
928 session->pending_messages_tail, pm);
929 if (GNUNET_YES != is_nat)
931 GNUNET_STATISTICS_update (plugin->env->stats,
932 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
934 plugin->env->register_quota_notification (plugin->env->cls,
935 &address->peer, PLUGIN_NAME, session);
936 session->timeout_task = GNUNET_SCHEDULER_add_delayed (
937 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT, &session_timeout, session);
942 * If we have pending messages, ask the server to
943 * transmit them (schedule the respective tasks, etc.)
945 * @param session for which session should we do this
948 process_pending_messages (struct Session *session);
951 * Function called to notify a client about the socket
952 * being ready to queue more data. "buf" will be
953 * NULL and "size" zero if the socket was closed for
954 * writing in the meantime.
957 * @param size number of bytes available in buf
958 * @param buf where the callee should write the message
959 * @return number of bytes written to buf
962 do_transmit (void *cls, size_t size, void *buf)
964 struct Session *session = cls;
965 struct GNUNET_PeerIdentity pid;
966 struct Plugin *plugin;
967 struct PendingMessage *pos;
968 struct PendingMessage *hd;
969 struct PendingMessage *tl;
970 struct GNUNET_TIME_Absolute now;
974 session->transmit_handle = NULL;
975 plugin = session->plugin;
978 LOG(GNUNET_ERROR_TYPE_DEBUG,
979 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
980 GNUNET_i2s (&session->target));
981 /* timeout; cancel all messages that have already expired */
985 now = GNUNET_TIME_absolute_get ();
986 while ((NULL != (pos = session->pending_messages_head))
987 && (pos->timeout.abs_value_us <= now.abs_value_us))
989 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
990 session->pending_messages_tail, pos);
991 LOG(GNUNET_ERROR_TYPE_DEBUG,
992 "Failed to transmit %u byte message to `%4s'.\n", pos->message_size,
993 GNUNET_i2s (&session->target));
994 ret += pos->message_size;
995 GNUNET_CONTAINER_DLL_insert_after(hd, tl, tl, pos);
997 /* do this call before callbacks (so that if callbacks destroy
998 * session, they have a chance to cancel actions done by this
1000 process_pending_messages (session);
1001 pid = session->target;
1002 /* no do callbacks and do not use session again since
1003 * the callbacks may abort the session */
1004 while (NULL != (pos = hd))
1006 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1007 if (pos->transmit_cont != NULL )
1008 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_SYSERR,
1009 pos->message_size, 0);
1012 GNUNET_STATISTICS_update (plugin->env->stats,
1013 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1015 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1016 ("# bytes discarded by TCP (timeout)"), ret, GNUNET_NO);
1019 /* copy all pending messages that would fit */
1024 while (NULL != (pos = session->pending_messages_head))
1026 if (ret + pos->message_size > size)
1028 GNUNET_CONTAINER_DLL_remove(session->pending_messages_head,
1029 session->pending_messages_tail, pos);
1030 GNUNET_assert(size >= pos->message_size);
1031 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting message of type %u\n",
1032 ntohs (((struct GNUNET_MessageHeader * ) pos->msg)->type));
1033 /* FIXME: this memcpy can be up to 7% of our total runtime */
1034 memcpy (cbuf, pos->msg, pos->message_size);
1035 cbuf += pos->message_size;
1036 ret += pos->message_size;
1037 size -= pos->message_size;
1038 GNUNET_CONTAINER_DLL_insert_tail(hd, tl, pos);
1040 /* schedule 'continuation' before callbacks so that callbacks that
1041 * cancel everything don't cause us to use a session that no longer
1043 process_pending_messages (session);
1044 session->last_activity = GNUNET_TIME_absolute_get ();
1045 pid = session->target;
1046 /* we'll now call callbacks that may cancel the session; hence
1047 * we should not use 'session' after this point */
1048 while (NULL != (pos = hd))
1050 GNUNET_CONTAINER_DLL_remove(hd, tl, pos);
1051 if (pos->transmit_cont != NULL )
1052 pos->transmit_cont (pos->transmit_cont_cls, &pid, GNUNET_OK,
1053 pos->message_size, pos->message_size); /* FIXME: include TCP overhead */
1056 GNUNET_assert(hd == NULL);
1057 GNUNET_assert(tl == NULL);
1058 LOG(GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes\n", ret);
1059 GNUNET_STATISTICS_update (plugin->env->stats,
1060 gettext_noop ("# bytes currently in TCP buffers"), -(int64_t) ret,
1062 GNUNET_STATISTICS_update (plugin->env->stats,
1063 gettext_noop ("# bytes transmitted via TCP"), ret, GNUNET_NO);
1068 * If we have pending messages, ask the server to
1069 * transmit them (schedule the respective tasks, etc.)
1071 * @param session for which session should we do this
1074 process_pending_messages (struct Session *session)
1076 struct PendingMessage *pm;
1078 GNUNET_assert(NULL != session->client);
1079 if (NULL != session->transmit_handle)
1081 if (NULL == (pm = session->pending_messages_head))
1084 session->transmit_handle = GNUNET_SERVER_notify_transmit_ready (
1085 session->client, pm->message_size,
1086 GNUNET_TIME_absolute_get_remaining (pm->timeout), &do_transmit, session);
1091 * Closure for #session_it().
1093 struct FindSessionContext
1096 * Session we are looking for.
1101 * Set to #GNUNET_OK if we found the session.
1107 * Function called to check if a session is in our maps.
1109 * @param cls the `struct FindSessionContext`
1110 * @param key peer identity
1111 * @param value session in the map
1112 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1115 session_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1117 struct FindSessionContext *res = cls;
1118 struct Session *session = value;
1120 if (res->s == session)
1122 res->res = GNUNET_OK;
1129 * Check that the given session is known to the plugin and
1130 * is in one of our maps.
1132 * @param plugin the plugin to check against
1133 * @param session the session to check
1134 * @return #GNUNET_OK if all is well, #GNUNET_SYSERR if the session is invalid
1137 find_session (struct Plugin *plugin, struct Session *session)
1139 struct FindSessionContext session_map_res;
1140 struct FindSessionContext nat_map_res;
1142 session_map_res.s = session;
1143 session_map_res.res = GNUNET_SYSERR;
1144 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap, &session_it,
1146 if (GNUNET_SYSERR != session_map_res.res)
1148 nat_map_res.s = session;
1149 nat_map_res.res = GNUNET_SYSERR;
1150 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns, &session_it,
1152 if (GNUNET_SYSERR != nat_map_res.res)
1155 return GNUNET_SYSERR;
1160 * Function that can be used by the transport service to transmit
1161 * a message using the plugin. Note that in the case of a
1162 * peer disconnecting, the continuation MUST be called
1163 * prior to the disconnect notification itself. This function
1164 * will be called with this peer's HELLO message to initiate
1165 * a fresh connection to another peer.
1167 * @param cls closure
1168 * @param session which session must be used
1169 * @param msgbuf the message to transmit
1170 * @param msgbuf_size number of bytes in 'msgbuf'
1171 * @param priority how important is the message (most plugins will
1172 * ignore message priority and just FIFO)
1173 * @param to how long to wait at most for the transmission (does not
1174 * require plugins to discard the message after the timeout,
1175 * just advisory for the desired delay; most plugins will ignore
1177 * @param cont continuation to call once the message has
1178 * been transmitted (or if the transport is ready
1179 * for the next transmission call; or if the
1180 * peer disconnected...); can be NULL
1181 * @param cont_cls closure for @a cont
1182 * @return number of bytes used (on the physical network, with overheads);
1183 * -1 on hard errors (i.e. address invalid); 0 is a legal value
1184 * and does NOT mean that the message was not transmitted (DV)
1187 tcp_plugin_send (void *cls, struct Session *session, const char *msgbuf,
1188 size_t msgbuf_size, unsigned int priority, struct GNUNET_TIME_Relative to,
1189 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1191 struct Plugin * plugin = cls;
1192 struct PendingMessage *pm;
1195 if (GNUNET_SYSERR == find_session (plugin, session))
1197 LOG(GNUNET_ERROR_TYPE_ERROR, _("Trying to send with invalid session %p\n"));
1199 return GNUNET_SYSERR;
1202 /* create new message entry */
1203 pm = GNUNET_malloc (sizeof (struct PendingMessage) + msgbuf_size);
1204 pm->msg = (const char *) &pm[1];
1205 memcpy (&pm[1], msgbuf, msgbuf_size);
1206 pm->message_size = msgbuf_size;
1207 pm->timeout = GNUNET_TIME_relative_to_absolute (to);
1208 pm->transmit_cont = cont;
1209 pm->transmit_cont_cls = cont_cls;
1211 LOG(GNUNET_ERROR_TYPE_DEBUG,
1212 "Asked to transmit %u bytes to `%s', added message to list.\n",
1213 msgbuf_size, GNUNET_i2s (&session->target));
1216 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
1217 &session->target, session))
1219 GNUNET_assert(NULL != session->client);
1220 GNUNET_SERVER_client_set_timeout (session->client,
1221 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1222 GNUNET_STATISTICS_update (plugin->env->stats,
1223 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1226 /* append pm to pending_messages list */
1227 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1228 session->pending_messages_tail, pm);
1230 process_pending_messages (session);
1234 == GNUNET_CONTAINER_multipeermap_contains_value (plugin->nat_wait_conns,
1235 &session->target, session))
1237 LOG(GNUNET_ERROR_TYPE_DEBUG,
1238 "This NAT WAIT session for peer `%s' is not yet ready!\n",
1239 GNUNET_i2s (&session->target));
1240 GNUNET_STATISTICS_update (plugin->env->stats,
1241 gettext_noop ("# bytes currently in TCP buffers"), msgbuf_size,
1244 /* append pm to pending_messages list */
1245 GNUNET_CONTAINER_DLL_insert_tail(session->pending_messages_head,
1246 session->pending_messages_tail, pm);
1251 LOG(GNUNET_ERROR_TYPE_ERROR, "Invalid session %p\n", session);
1253 cont (cont_cls, &session->target, GNUNET_SYSERR, pm->message_size, 0);
1256 return GNUNET_SYSERR; /* session does not exist here */
1261 * Closure for #session_lookup_it().
1266 * Address we are looking for.
1268 const struct GNUNET_HELLO_Address *address;
1271 * Where to store the session (if we found it).
1273 struct Session *result;
1278 * Look for a session by address.
1280 * @param cls the `struct SessionItCtx`
1282 * @param value a `struct Session`
1283 * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the session
1286 session_lookup_it (void *cls, const struct GNUNET_PeerIdentity *key,
1289 struct SessionItCtx * si_ctx = cls;
1290 struct Session * session = value;
1292 if (0 != GNUNET_HELLO_address_cmp (si_ctx->address, session->address))
1294 /* Found existing session */
1295 si_ctx->result = session;
1300 * Task cleaning up a NAT connection attempt after timeout
1302 * @param cls the `struct Session`
1303 * @param tc scheduler context (unused)
1306 nat_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1308 struct Session *session = cls;
1310 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1311 LOG(GNUNET_ERROR_TYPE_DEBUG,
1312 "NAT WAIT connection to `%4s' at `%s' could not be established, removing session\n",
1313 GNUNET_i2s (&session->target),
1314 tcp_address_to_string (NULL, session->address->address, session->address->address_length));
1315 tcp_disconnect_session (session->plugin, session);
1319 tcp_plugin_update_session_timeout (void *cls,
1320 const struct GNUNET_PeerIdentity *peer, struct Session *session)
1322 struct Plugin *plugin = cls;
1324 if (GNUNET_SYSERR == find_session (plugin, session))
1326 reschedule_session_timeout (session);
1330 * Task to signal the server that we can continue
1331 * receiving from the TCP client now.
1333 * @param cls the `struct Session*`
1334 * @param tc task context (unused)
1337 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1339 struct Session *session = cls;
1341 session->receive_delay_task = GNUNET_SCHEDULER_NO_TASK;
1342 reschedule_session_timeout (session);
1344 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1347 static void tcp_plugin_update_inbound_delay (void *cls,
1348 const struct GNUNET_PeerIdentity *peer,
1349 struct Session *session,
1350 struct GNUNET_TIME_Relative delay)
1352 if (GNUNET_SCHEDULER_NO_TASK == session->receive_delay_task)
1355 LOG(GNUNET_ERROR_TYPE_DEBUG,
1356 "New inbound delay %llu us\n",delay.rel_value_us);
1358 GNUNET_SCHEDULER_cancel (session->receive_delay_task);
1359 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
1360 &delayed_done, session);
1365 * Create a new session to transmit data to the target
1366 * This session will used to send data to this peer and the plugin will
1367 * notify us by calling the env->session_end function
1369 * @param cls closure
1370 * @param address the address to use
1371 * @return the session if the address is valid, NULL otherwise
1373 static struct Session *
1374 tcp_plugin_get_session (void *cls, const struct GNUNET_HELLO_Address *address)
1376 struct Plugin *plugin = cls;
1377 struct Session *session = NULL;
1381 struct GNUNET_CONNECTION_Handle *sa;
1382 struct sockaddr_in a4;
1383 struct sockaddr_in6 a6;
1384 const struct IPv4TcpAddress *t4;
1385 const struct IPv6TcpAddress *t6;
1386 struct GNUNET_ATS_Information ats;
1387 unsigned int is_natd = GNUNET_NO;
1390 addrlen = address->address_length;
1391 LOG(GNUNET_ERROR_TYPE_DEBUG,
1392 "Trying to get session for `%s' address of peer `%s'\n",
1393 tcp_address_to_string(NULL, address->address, address->address_length),
1394 GNUNET_i2s (&address->peer));
1396 if (GNUNET_HELLO_address_check_option(address, GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1402 /* look for existing session */
1403 if (GNUNET_YES == GNUNET_CONTAINER_multipeermap_contains (plugin->sessionmap,
1406 struct SessionItCtx si_ctx;
1408 si_ctx.address = address;
1409 si_ctx.result = NULL;
1411 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap,
1412 &address->peer, &session_lookup_it, &si_ctx);
1413 if (si_ctx.result != NULL )
1415 session = si_ctx.result;
1416 LOG(GNUNET_ERROR_TYPE_DEBUG,
1417 "Found existing session for `%s' address `%s' session %p\n",
1418 GNUNET_i2s (&address->peer),
1419 tcp_address_to_string(NULL, address->address, address->address_length),
1423 LOG(GNUNET_ERROR_TYPE_DEBUG,
1424 "Existing sessions did not match address `%s' or peer `%s'\n",
1425 tcp_address_to_string(NULL, address->address, address->address_length),
1426 GNUNET_i2s (&address->peer));
1429 if (addrlen == sizeof(struct IPv6TcpAddress))
1431 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1432 t6 = address->address;
1434 memset (&a6, 0, sizeof(a6));
1435 #if HAVE_SOCKADDR_IN_SIN_LEN
1436 a6.sin6_len = sizeof (a6);
1438 a6.sin6_family = AF_INET6;
1439 a6.sin6_port = t6->t6_port;
1440 if (t6->t6_port == 0)
1441 is_natd = GNUNET_YES;
1442 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1446 else if (addrlen == sizeof(struct IPv4TcpAddress))
1448 GNUNET_assert(NULL != address->address); /* make static analysis happy */
1449 t4 = address->address;
1451 memset (&a4, 0, sizeof(a4));
1452 #if HAVE_SOCKADDR_IN_SIN_LEN
1453 a4.sin_len = sizeof (a4);
1455 a4.sin_family = AF_INET;
1456 a4.sin_port = t4->t4_port;
1457 if (t4->t4_port == 0)
1458 is_natd = GNUNET_YES;
1459 a4.sin_addr.s_addr = t4->ipv4_addr;
1465 GNUNET_STATISTICS_update (plugin->env->stats, gettext_noop
1466 ("# requests to create session with invalid address"), 1, GNUNET_NO);
1470 ats = plugin->env->get_address_type (plugin->env->cls, sb, sbs);
1472 if ((is_natd == GNUNET_YES) && (addrlen == sizeof(struct IPv6TcpAddress)))
1474 /* NAT client only works with IPv4 addresses */
1478 if (plugin->cur_connections >= plugin->max_connections)
1484 if ((is_natd == GNUNET_YES)
1486 == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1489 /* Only do one NAT punch attempt per peer identity */
1493 if ((is_natd == GNUNET_YES) && (NULL != plugin->nat) &&
1494 (GNUNET_NO == GNUNET_CONTAINER_multipeermap_contains (plugin->nat_wait_conns,
1497 LOG (GNUNET_ERROR_TYPE_DEBUG,
1498 "Found valid IPv4 NAT address (creating session)!\n");
1499 session = create_session (plugin, address, NULL, GNUNET_YES);
1500 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1503 session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1504 session->nat_connection_timeout = GNUNET_SCHEDULER_add_delayed (NAT_TIMEOUT,
1505 &nat_connect_timeout, session);
1506 GNUNET_assert(session != NULL);
1507 GNUNET_assert(GNUNET_OK == GNUNET_CONTAINER_multipeermap_put (plugin->nat_wait_conns,
1508 &session->target, session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1510 LOG(GNUNET_ERROR_TYPE_DEBUG,
1511 "Created NAT WAIT connection to `%4s' at `%s'\n",
1512 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1514 if (GNUNET_OK == GNUNET_NAT_run_client (plugin->nat, &a4))
1518 LOG(GNUNET_ERROR_TYPE_DEBUG,
1519 "Running NAT client for `%4s' at `%s' failed\n",
1520 GNUNET_i2s (&session->target), GNUNET_a2s (sb, sbs));
1521 tcp_disconnect_session (plugin, session);
1526 /* create new outbound session */
1527 GNUNET_assert(plugin->cur_connections <= plugin->max_connections);
1528 sa = GNUNET_CONNECTION_create_from_sockaddr (af, sb, sbs);
1531 LOG(GNUNET_ERROR_TYPE_DEBUG,
1532 "Failed to create connection to `%4s' at `%s'\n",
1533 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1536 plugin->cur_connections++;
1537 if (plugin->cur_connections == plugin->max_connections)
1538 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
1540 LOG(GNUNET_ERROR_TYPE_DEBUG,
1541 "Asked to transmit to `%4s', creating fresh session using address `%s'.\n",
1542 GNUNET_i2s (&address->peer), GNUNET_a2s (sb, sbs));
1544 session = create_session (plugin, address,
1545 GNUNET_SERVER_connect_socket (plugin->server, sa), GNUNET_NO);
1546 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
1548 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
1549 GNUNET_SERVER_client_set_user_context(session->client, session);
1550 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1551 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1552 LOG(GNUNET_ERROR_TYPE_DEBUG,
1553 "Creating new session for `%s' address `%s' session %p\n",
1554 GNUNET_i2s (&address->peer),
1555 tcp_address_to_string(NULL, address->address, address->address_length),
1557 /* Send TCP Welcome */
1558 process_pending_messages (session);
1564 session_disconnect_it (void *cls, const struct GNUNET_PeerIdentity *key,
1567 struct Plugin *plugin = cls;
1568 struct Session *session = value;
1570 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
1571 ("# transport-service disconnect requests for TCP"), 1, GNUNET_NO);
1572 tcp_disconnect_session (plugin, session);
1577 * Function that can be called to force a disconnect from the
1578 * specified neighbour. This should also cancel all previously
1579 * scheduled transmissions. Obviously the transmission may have been
1580 * partially completed already, which is OK. The plugin is supposed
1581 * to close the connection (if applicable) and no longer call the
1582 * transmit continuation(s).
1584 * Finally, plugin MUST NOT call the services's receive function to
1585 * notify the service that the connection to the specified target was
1586 * closed after a getting this call.
1588 * @param cls closure
1589 * @param target peer for which the last transmission is
1593 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1595 struct Plugin *plugin = cls;
1597 LOG(GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s'\n",
1598 GNUNET_i2s (target));
1599 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->sessionmap, target,
1600 &session_disconnect_it, plugin);
1601 GNUNET_CONTAINER_multipeermap_get_multiple (plugin->nat_wait_conns, target,
1602 &session_disconnect_it, plugin);
1606 * Running pretty printers: head
1608 static struct PrettyPrinterContext *ppc_dll_head;
1611 * Running pretty printers: tail
1613 static struct PrettyPrinterContext *ppc_dll_tail;
1616 * Context for address to string conversion.
1618 struct PrettyPrinterContext
1623 struct PrettyPrinterContext *next;
1628 struct PrettyPrinterContext *prev;
1633 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1638 struct GNUNET_RESOLVER_RequestHandle *resolver_handle;
1641 * Function to call with the result.
1643 GNUNET_TRANSPORT_AddressStringCallback asc;
1646 * Clsoure for 'asc'.
1651 * Port to add after the IP address.
1667 ppc_cancel_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1669 struct PrettyPrinterContext *ppc = cls;
1671 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1672 if (NULL != ppc->resolver_handle)
1674 GNUNET_RESOLVER_request_cancel (ppc->resolver_handle);
1675 ppc->resolver_handle = NULL;
1677 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1682 * Append our port and forward the result.
1684 * @param cls the 'struct PrettyPrinterContext*'
1685 * @param hostname hostname part of the address
1688 append_port (void *cls, const char *hostname)
1690 struct PrettyPrinterContext *ppc = cls;
1691 struct PrettyPrinterContext *cur;
1694 if (NULL == hostname)
1696 ppc->asc (ppc->asc_cls, NULL );
1697 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, ppc);
1698 GNUNET_SCHEDULER_cancel (ppc->timeout_task);
1699 ppc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1700 ppc->resolver_handle = NULL;
1704 for (cur = ppc_dll_head; (NULL != cur); cur = cur->next)
1713 if (GNUNET_YES == ppc->ipv6)
1714 GNUNET_asprintf (&ret, "%s.%u.[%s]:%d", PLUGIN_NAME, ppc->options, hostname,
1717 GNUNET_asprintf (&ret, "%s.%u.%s:%d", PLUGIN_NAME, ppc->options, hostname,
1719 ppc->asc (ppc->asc_cls, ret);
1724 * Convert the transports address to a nice, human-readable
1727 * @param cls closure
1728 * @param type name of the transport that generated the address
1729 * @param addr one of the addresses of the host, NULL for the last address
1730 * the specific address format depends on the transport
1731 * @param addrlen length of the address
1732 * @param numeric should (IP) addresses be displayed in numeric form?
1733 * @param timeout after how long should we give up?
1734 * @param asc function to call on each string
1735 * @param asc_cls closure for asc
1738 tcp_plugin_address_pretty_printer (void *cls, const char *type,
1739 const void *addr, size_t addrlen, int numeric,
1740 struct GNUNET_TIME_Relative timeout,
1741 GNUNET_TRANSPORT_AddressStringCallback asc, void *asc_cls)
1743 struct PrettyPrinterContext *ppc;
1746 struct sockaddr_in a4;
1747 struct sockaddr_in6 a6;
1748 const struct IPv4TcpAddress *t4;
1749 const struct IPv6TcpAddress *t6;
1753 if (addrlen == sizeof(struct IPv6TcpAddress))
1756 memset (&a6, 0, sizeof(a6));
1757 a6.sin6_family = AF_INET6;
1758 a6.sin6_port = t6->t6_port;
1759 memcpy (&a6.sin6_addr, &t6->ipv6_addr, sizeof(struct in6_addr));
1760 port = ntohs (t6->t6_port);
1761 options = ntohl (t6->options);
1765 else if (addrlen == sizeof(struct IPv4TcpAddress))
1768 memset (&a4, 0, sizeof(a4));
1769 a4.sin_family = AF_INET;
1770 a4.sin_port = t4->t4_port;
1771 a4.sin_addr.s_addr = t4->ipv4_addr;
1772 port = ntohs (t4->t4_port);
1773 options = ntohl (t4->options);
1777 else if (0 == addrlen)
1779 asc (asc_cls, TRANSPORT_SESSION_INBOUND_STRING);
1780 asc (asc_cls, NULL );
1785 /* invalid address */
1787 asc (asc_cls, NULL );
1790 ppc = GNUNET_new (struct PrettyPrinterContext);
1791 if (addrlen == sizeof(struct IPv6TcpAddress))
1792 ppc->ipv6 = GNUNET_YES;
1794 ppc->ipv6 = GNUNET_NO;
1796 ppc->asc_cls = asc_cls;
1798 ppc->options = options;
1799 ppc->timeout_task = GNUNET_SCHEDULER_add_delayed (
1800 GNUNET_TIME_relative_multiply (timeout, 2), &ppc_cancel_task, ppc);
1801 ppc->resolver_handle = GNUNET_RESOLVER_hostname_get (sb, sbs, !numeric,
1802 timeout, &append_port, ppc);
1803 if (NULL != ppc->resolver_handle)
1805 //GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Adding request %p\n", ppc);
1806 GNUNET_CONTAINER_DLL_insert(ppc_dll_head, ppc_dll_tail, ppc);
1816 * Check if the given port is plausible (must be either our listen
1817 * port or our advertised port), or any port if we are behind NAT
1818 * and do not have a port open. If it is neither, we return
1821 * @param plugin global variables
1822 * @param in_port port number to check
1823 * @return #GNUNET_OK if port is either open_port or adv_port
1826 check_port (struct Plugin *plugin, uint16_t in_port)
1828 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1830 return GNUNET_SYSERR;
1834 * Function that will be called to check if a binary address for this
1835 * plugin is well-formed and corresponds to an address for THIS peer
1836 * (as per our configuration). Naturally, if absolutely necessary,
1837 * plugins can be a bit conservative in their answer, but in general
1838 * plugins should make sure that the address does not redirect
1839 * traffic to a 3rd party that might try to man-in-the-middle our
1842 * @param cls closure, our `struct Plugin *`
1843 * @param addr pointer to the address
1844 * @param addrlen length of addr
1845 * @return #GNUNET_OK if this is a plausible address for this peer
1846 * and transport, #GNUNET_SYSERR if not
1849 tcp_plugin_check_address (void *cls, const void *addr, size_t addrlen)
1851 struct Plugin *plugin = cls;
1852 struct IPv4TcpAddress *v4;
1853 struct IPv6TcpAddress *v6;
1855 if ((addrlen != sizeof(struct IPv4TcpAddress))
1856 && (addrlen != sizeof(struct IPv6TcpAddress)))
1859 return GNUNET_SYSERR;
1862 if (addrlen == sizeof(struct IPv4TcpAddress))
1864 v4 = (struct IPv4TcpAddress *) addr;
1865 if (0 != memcmp (&v4->options, &myoptions, sizeof(myoptions)))
1868 return GNUNET_SYSERR;
1870 if (GNUNET_OK != check_port (plugin, ntohs (v4->t4_port)))
1871 return GNUNET_SYSERR;
1873 != GNUNET_NAT_test_address (plugin->nat, &v4->ipv4_addr,
1874 sizeof(struct in_addr)))
1875 return GNUNET_SYSERR;
1879 v6 = (struct IPv6TcpAddress *) addr;
1880 if (IN6_IS_ADDR_LINKLOCAL (&v6->ipv6_addr))
1883 return GNUNET_SYSERR;
1885 if (0 != memcmp (&v6->options, &myoptions, sizeof(myoptions)))
1888 return GNUNET_SYSERR;
1890 if (GNUNET_OK != check_port (plugin, ntohs (v6->t6_port)))
1891 return GNUNET_SYSERR;
1893 != GNUNET_NAT_test_address (plugin->nat, &v6->ipv6_addr,
1894 sizeof(struct in6_addr)))
1895 return GNUNET_SYSERR;
1901 * We've received a nat probe from this peer via TCP. Finish
1902 * creating the client session and resume sending of queued
1905 * @param cls closure
1906 * @param client identification of the client
1907 * @param message the actual message
1910 handle_tcp_nat_probe (void *cls, struct GNUNET_SERVER_Client *client,
1911 const struct GNUNET_MessageHeader *message)
1913 struct Plugin *plugin = cls;
1914 struct Session *session;
1915 const struct TCP_NAT_ProbeMessage *tcp_nat_probe;
1918 struct IPv4TcpAddress *t4;
1919 struct IPv6TcpAddress *t6;
1920 const struct sockaddr_in *s4;
1921 const struct sockaddr_in6 *s6;
1923 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received NAT probe\n");
1924 /* We have received a TCP NAT probe, meaning we (hopefully) initiated
1925 * a connection to this peer by running gnunet-nat-client. This peer
1926 * received the punch message and now wants us to use the new connection
1927 * as the default for that peer. Do so and then send a WELCOME message
1928 * so we can really be connected!
1930 if (ntohs (message->size) != sizeof(struct TCP_NAT_ProbeMessage))
1933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1937 tcp_nat_probe = (const struct TCP_NAT_ProbeMessage *) message;
1938 if (0 == memcmp (&tcp_nat_probe->clientIdentity, plugin->env->my_identity,
1939 sizeof(struct GNUNET_PeerIdentity)))
1941 /* refuse connections from ourselves */
1942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1946 session = GNUNET_CONTAINER_multipeermap_get (plugin->nat_wait_conns,
1947 &tcp_nat_probe->clientIdentity);
1948 if (session == NULL )
1950 LOG(GNUNET_ERROR_TYPE_DEBUG, "Did NOT find session for NAT probe!\n");
1951 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1954 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found session for NAT probe!\n");
1956 if (session->nat_connection_timeout != GNUNET_SCHEDULER_NO_TASK )
1958 GNUNET_SCHEDULER_cancel (session->nat_connection_timeout);
1959 session->nat_connection_timeout = GNUNET_SCHEDULER_NO_TASK;
1962 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
1965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1966 tcp_disconnect_session (plugin, session);
1970 GNUNET_CONTAINER_multipeermap_remove (plugin->nat_wait_conns, &tcp_nat_probe->clientIdentity, session) == GNUNET_YES);
1971 GNUNET_SERVER_client_set_user_context(client, session);
1972 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
1973 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1974 session->last_activity = GNUNET_TIME_absolute_get ();
1975 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found address `%s' for incoming connection\n",
1976 GNUNET_a2s (vaddr, alen));
1977 switch (((const struct sockaddr *) vaddr)->sa_family)
1981 t4 = GNUNET_new (struct IPv4TcpAddress);
1983 t4->t4_port = s4->sin_port;
1984 t4->ipv4_addr = s4->sin_addr.s_addr;
1985 session->address = GNUNET_HELLO_address_allocate (
1986 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t4,
1987 sizeof(struct IPv4TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
1991 t6 = GNUNET_new (struct IPv6TcpAddress);
1993 t6->t6_port = s6->sin6_port;
1994 memcpy (&t6->ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
1995 session->address = GNUNET_HELLO_address_allocate (
1996 &tcp_nat_probe->clientIdentity, PLUGIN_NAME, &t6,
1997 sizeof(struct IPv6TcpAddress), GNUNET_HELLO_ADDRESS_INFO_NONE);
2001 LOG(GNUNET_ERROR_TYPE_DEBUG, "Bad address for incoming connection!\n");
2003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2004 tcp_disconnect_session (plugin, session);
2008 GNUNET_break(NULL == session->client);
2009 GNUNET_SERVER_client_keep (client);
2010 session->client = client;
2011 GNUNET_STATISTICS_update (plugin->env->stats,
2012 gettext_noop ("# TCP sessions active"), 1, GNUNET_NO);
2013 process_pending_messages (session);
2014 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2018 * We've received a welcome from this peer via TCP. Possibly create a
2019 * fresh client record and send back our welcome.
2021 * @param cls closure
2022 * @param client identification of the client
2023 * @param message the actual message
2026 handle_tcp_welcome (void *cls, struct GNUNET_SERVER_Client *client,
2027 const struct GNUNET_MessageHeader *message)
2029 struct Plugin *plugin = cls;
2030 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
2031 struct GNUNET_HELLO_Address *address;
2032 struct Session *session;
2035 struct IPv4TcpAddress t4;
2036 struct IPv6TcpAddress t6;
2037 const struct sockaddr_in *s4;
2038 const struct sockaddr_in6 *s6;
2039 struct GNUNET_ATS_Information ats;
2042 if (0 == memcmp (&wm->clientIdentity, plugin->env->my_identity,
2043 sizeof(struct GNUNET_PeerIdentity)))
2045 /* refuse connections from ourselves */
2046 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2047 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2049 LOG(GNUNET_ERROR_TYPE_WARNING,
2050 "Received %s message from my own identity `%4s' on address `%s'\n",
2051 "WELCOME", GNUNET_i2s (&wm->clientIdentity),
2052 GNUNET_a2s (vaddr, alen));
2059 LOG(GNUNET_ERROR_TYPE_DEBUG, "Received %s message from `%4s' %p\n", "WELCOME",
2060 GNUNET_i2s (&wm->clientIdentity), client);
2061 GNUNET_STATISTICS_update (plugin->env->stats,
2062 gettext_noop ("# TCP WELCOME messages received"), 1, GNUNET_NO);
2063 session = lookup_session_by_client (plugin, client);
2064 if (NULL != session)
2066 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2068 LOG(GNUNET_ERROR_TYPE_DEBUG, "Found existing session %p for peer `%s'\n",
2069 session, GNUNET_a2s (vaddr, alen));
2075 GNUNET_SERVER_client_keep (client);
2076 if (NULL != plugin->service) /* Otherwise value is incremented in tcp_access_check */
2077 plugin->cur_connections++;
2078 if (plugin->cur_connections == plugin->max_connections)
2079 GNUNET_SERVER_suspend (plugin->server); /* Maximum number of connections rechead */
2081 if (GNUNET_OK == GNUNET_SERVER_client_get_address (client, &vaddr, &alen))
2083 if (alen == sizeof(struct sockaddr_in))
2086 memset (&t4, '\0', sizeof (t4));
2087 t4.options = htonl (0);
2088 t4.t4_port = s4->sin_port;
2089 t4.ipv4_addr = s4->sin_addr.s_addr;
2090 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2091 PLUGIN_NAME, &t4, sizeof(t4),
2092 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2094 else if (alen == sizeof(struct sockaddr_in6))
2097 memset (&t6, '\0', sizeof (t6));
2098 t6.options = htonl (0);
2099 t6.t6_port = s6->sin6_port;
2100 memcpy (&t6.ipv6_addr, &s6->sin6_addr, sizeof(struct in6_addr));
2101 address = GNUNET_HELLO_address_allocate (&wm->clientIdentity,
2102 PLUGIN_NAME, &t6, sizeof (t6),
2103 GNUNET_HELLO_ADDRESS_INFO_INBOUND);
2110 session = create_session (plugin, address, client, GNUNET_NO);
2111 GNUNET_HELLO_address_free (address);
2112 ats = plugin->env->get_address_type (plugin->env->cls, vaddr, alen);
2113 session->ats_address_network_type = (enum GNUNET_ATS_Network_Type) ntohl (
2115 LOG(GNUNET_ERROR_TYPE_DEBUG, "Creating new%s session %p for peer `%s' client %p \n",
2116 GNUNET_HELLO_address_check_option (session->address,
2117 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? " inbound" : "", session,
2118 tcp_address_to_string(NULL, (void *) session->address->address,
2119 session->address->address_length),
2122 GNUNET_SERVER_client_set_user_context(session->client, session);
2123 GNUNET_CONTAINER_multipeermap_put (plugin->sessionmap, &session->target,
2124 session, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2126 /* Notify transport and ATS about new session */
2127 plugin->env->session_start (NULL, session->address, session, &ats, 1);
2131 LOG(GNUNET_ERROR_TYPE_DEBUG,
2132 "Did not obtain TCP socket address for incoming connection\n");
2138 if (session->expecting_welcome != GNUNET_YES)
2141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2145 session->last_activity = GNUNET_TIME_absolute_get ();
2146 session->expecting_welcome = GNUNET_NO;
2148 process_pending_messages (session);
2149 GNUNET_SERVER_client_set_timeout (client,
2150 GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2151 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2156 * We've received data for this peer via TCP. Unbox,
2157 * compute latency and forward.
2159 * @param cls closure
2160 * @param client identification of the client
2161 * @param message the actual message
2164 handle_tcp_data (void *cls, struct GNUNET_SERVER_Client *client,
2165 const struct GNUNET_MessageHeader *message)
2167 struct Plugin *plugin = cls;
2168 struct Session *session;
2169 struct GNUNET_TIME_Relative delay;
2172 type = ntohs (message->type);
2173 if ((GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME == type)
2174 || (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE == type))
2176 /* We don't want to propagate WELCOME and NAT Probe messages up! */
2177 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2180 session = lookup_session_by_client (plugin, client);
2181 if (NULL == session)
2183 /* No inbound session found */
2187 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2188 LOG(GNUNET_ERROR_TYPE_ERROR,
2189 "Received unexpected %u bytes of type %u from `%s'\n",
2190 (unsigned int ) ntohs (message->size),
2191 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
2193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2194 GNUNET_free_non_null(vaddr);
2197 else if (GNUNET_YES == session->expecting_welcome)
2199 /* Session is expecting WELCOME message */
2203 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
2204 LOG(GNUNET_ERROR_TYPE_ERROR,
2205 "Received unexpected %u bytes of type %u from `%s'\n",
2206 (unsigned int ) ntohs (message->size),
2207 (unsigned int ) ntohs (message->type), GNUNET_a2s (vaddr, alen));
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), GNUNET_i2s (&session->target));
2220 GNUNET_STATISTICS_update (plugin->env->stats,
2221 gettext_noop ("# bytes received via TCP"), ntohs (message->size),
2223 struct GNUNET_ATS_Information distance;
2225 distance.type = htonl (GNUNET_ATS_NETWORK_TYPE);
2226 distance.value = htonl ((uint32_t) session->ats_address_network_type);
2227 GNUNET_break(session->ats_address_network_type != GNUNET_ATS_NET_UNSPECIFIED);
2230 GNUNET_CONTAINER_multipeermap_contains_value (plugin->sessionmap,
2231 &session->target, session));
2233 delay = plugin->env->receive (plugin->env->cls, session->address, session, message);
2234 plugin->env->update_address_metrics (plugin->env->cls, session->address,
2235 session, &distance, 1);
2236 reschedule_session_timeout (session);
2237 if (0 == delay.rel_value_us)
2239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2243 LOG(GNUNET_ERROR_TYPE_DEBUG, "Throttling receiving from `%s' for %s\n",
2244 GNUNET_i2s (&session->target),
2245 GNUNET_STRINGS_relative_time_to_string (delay, GNUNET_YES));
2246 GNUNET_SERVER_disable_receive_done_warning (client);
2247 session->receive_delay_task = GNUNET_SCHEDULER_add_delayed (delay,
2248 &delayed_done, session);
2253 * Functions with this signature are called whenever a peer
2254 * is disconnected on the network level.
2256 * @param cls closure
2257 * @param client identification of the client
2260 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
2262 struct Plugin *plugin = cls;
2263 struct Session *session;
2265 if (client == NULL )
2267 session = lookup_session_by_client (plugin, client);
2268 if (session == NULL )
2269 return; /* unknown, nothing to do */
2270 LOG(GNUNET_ERROR_TYPE_DEBUG,
2271 "Destroying session of `%4s' with %s due to network-level disconnect.\n",
2272 GNUNET_i2s (&session->target),
2273 tcp_address_to_string (session->plugin, session->address->address,
2274 session->address->address_length));
2276 if (plugin->cur_connections == plugin->max_connections)
2277 GNUNET_SERVER_resume (plugin->server); /* Resume server */
2279 if (plugin->cur_connections < 1)
2282 plugin->cur_connections--;
2284 GNUNET_STATISTICS_update (session->plugin->env->stats, gettext_noop
2285 ("# network-level TCP disconnect events"), 1, GNUNET_NO);
2286 tcp_disconnect_session (plugin, session);
2290 * We can now send a probe message, copy into buffer to really send.
2292 * @param cls closure, a struct TCPProbeContext
2293 * @param size max size to copy
2294 * @param buf buffer to copy message to
2295 * @return number of bytes copied into buf
2298 notify_send_probe (void *cls, size_t size, void *buf)
2300 struct TCPProbeContext *tcp_probe_ctx = cls;
2301 struct Plugin *plugin = tcp_probe_ctx->plugin;
2304 tcp_probe_ctx->transmit_handle = NULL;
2305 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2309 GNUNET_CONNECTION_destroy (tcp_probe_ctx->sock);
2310 GNUNET_free(tcp_probe_ctx);
2313 GNUNET_assert(size >= sizeof(tcp_probe_ctx->message));
2314 memcpy (buf, &tcp_probe_ctx->message, sizeof(tcp_probe_ctx->message));
2315 GNUNET_SERVER_connect_socket (tcp_probe_ctx->plugin->server,
2316 tcp_probe_ctx->sock);
2317 ret = sizeof(tcp_probe_ctx->message);
2318 GNUNET_free(tcp_probe_ctx);
2323 * Function called by the NAT subsystem suggesting another peer wants
2324 * to connect to us via connection reversal. Try to connect back to the
2327 * @param cls closure
2328 * @param addr address to try
2329 * @param addrlen number of bytes in @a addr
2332 try_connection_reversal (void *cls, const struct sockaddr *addr,
2335 struct Plugin *plugin = cls;
2336 struct GNUNET_CONNECTION_Handle *sock;
2337 struct TCPProbeContext *tcp_probe_ctx;
2340 * We have received an ICMP response, ostensibly from a peer
2341 * that wants to connect to us! Send a message to establish a connection.
2343 sock = GNUNET_CONNECTION_create_from_sockaddr (AF_INET, addr, addrlen);
2346 /* failed for some odd reason (out of sockets?); ignore attempt */
2350 /* FIXME: do we need to track these probe context objects so that
2351 * we can clean them up on plugin unload? */
2352 tcp_probe_ctx = GNUNET_new (struct TCPProbeContext);
2353 tcp_probe_ctx->message.header.size = htons (
2354 sizeof(struct TCP_NAT_ProbeMessage));
2355 tcp_probe_ctx->message.header.type = htons (
2356 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE);
2357 memcpy (&tcp_probe_ctx->message.clientIdentity, plugin->env->my_identity,
2358 sizeof(struct GNUNET_PeerIdentity));
2359 tcp_probe_ctx->plugin = plugin;
2360 tcp_probe_ctx->sock = sock;
2361 GNUNET_CONTAINER_DLL_insert(plugin->probe_head, plugin->probe_tail,
2363 tcp_probe_ctx->transmit_handle = GNUNET_CONNECTION_notify_transmit_ready (
2364 sock, ntohs (tcp_probe_ctx->message.header.size),
2365 GNUNET_TIME_UNIT_FOREVER_REL, ¬ify_send_probe, tcp_probe_ctx);
2370 * Function obtain the network type for a session
2372 * @param cls closure ('struct Plugin*')
2373 * @param session the session
2374 * @return the network type in HBO or #GNUNET_SYSERR
2376 static enum GNUNET_ATS_Network_Type
2377 tcp_get_network (void *cls, struct Session *session)
2379 GNUNET_assert(NULL != session);
2380 return session->ats_address_network_type;
2384 * Entry point for the plugin.
2386 * @param cls closure, the 'struct GNUNET_TRANSPORT_PluginEnvironment*'
2387 * @return the 'struct GNUNET_TRANSPORT_PluginFunctions*' or NULL on error
2390 libgnunet_plugin_transport_tcp_init (void *cls)
2392 static const struct GNUNET_SERVER_MessageHandler my_handlers[] = { {
2393 &handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
2394 sizeof(struct WelcomeMessage) }, { &handle_tcp_nat_probe, NULL,
2395 GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_NAT_PROBE,
2396 sizeof(struct TCP_NAT_ProbeMessage) }, { &handle_tcp_data, NULL,
2397 GNUNET_MESSAGE_TYPE_ALL, 0 }, { NULL, NULL, 0, 0 } };
2398 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2399 struct GNUNET_TRANSPORT_PluginFunctions *api;
2400 struct Plugin *plugin;
2401 struct GNUNET_SERVICE_Context *service;
2402 unsigned long long aport;
2403 unsigned long long bport;
2404 unsigned long long max_connections;
2406 struct GNUNET_TIME_Relative idle_timeout;
2409 struct sockaddr **addrs;
2410 socklen_t *addrlens;
2412 if (NULL == env->receive)
2414 /* run in 'stub' mode (i.e. as part of gnunet-peerinfo), don't fully
2415 initialze the plugin or the API */
2416 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2418 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2419 api->address_to_string = &tcp_address_to_string;
2420 api->string_to_address = &tcp_string_to_address;
2424 GNUNET_assert(NULL != env->cfg);
2426 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2427 "MAX_CONNECTIONS", &max_connections))
2428 max_connections = 128;
2432 != GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2433 "PORT", &bport)) || (bport > 65535)
2435 == GNUNET_CONFIGURATION_get_value_number (env->cfg, "transport-tcp",
2436 "ADVERTISED-PORT", &aport)) && (aport > 65535)))
2438 LOG(GNUNET_ERROR_TYPE_ERROR,
2439 _("Require valid port number for service `%s' in configuration!\n"),
2449 service = GNUNET_SERVICE_start ("transport-tcp", env->cfg,
2450 GNUNET_SERVICE_OPTION_NONE);
2451 if (service == NULL )
2453 LOG(GNUNET_ERROR_TYPE_WARNING, _("Failed to start service.\n"));
2460 /* Initialize my flags */
2463 plugin = GNUNET_new (struct Plugin);
2464 plugin->sessionmap = GNUNET_CONTAINER_multipeermap_create (max_connections,
2466 plugin->max_connections = max_connections;
2467 plugin->cur_connections = 0;
2468 plugin->open_port = bport;
2469 plugin->adv_port = aport;
2471 plugin->lsock = NULL;
2472 if ((service != NULL )&&
2475 GNUNET_SERVICE_get_server_addresses ("transport-tcp", env->cfg, &addrs,
2477 for (ret = ret_s-1; ret >= 0; ret--)
2478 LOG (GNUNET_ERROR_TYPE_INFO,
2479 "Binding to address `%s'\n",
2480 GNUNET_a2s (addrs[ret], addrlens[ret]));
2482 GNUNET_NAT_register (env->cfg, GNUNET_YES, aport, (unsigned int) ret_s,
2483 (const struct sockaddr **) addrs, addrlens,
2484 &tcp_nat_port_map_callback,
2485 &try_connection_reversal, plugin);
2486 for (ret = ret_s -1; ret >= 0; ret--)
2488 GNUNET_assert (addrs[ret] != NULL);
2489 GNUNET_free (addrs[ret]);
2491 GNUNET_free_non_null (addrs);
2492 GNUNET_free_non_null (addrlens);
2496 plugin->nat = GNUNET_NAT_register (plugin->env->cfg,
2497 GNUNET_YES, 0, 0, NULL, NULL, NULL,
2498 &try_connection_reversal, plugin);
2500 api = GNUNET_new (struct GNUNET_TRANSPORT_PluginFunctions);
2502 api->send = &tcp_plugin_send;
2503 api->get_session = &tcp_plugin_get_session;
2505 api->disconnect_session = &tcp_disconnect_session;
2506 api->query_keepalive_factor = &tcp_query_keepalive_factor;
2507 api->disconnect_peer = &tcp_plugin_disconnect;
2508 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2509 api->check_address = &tcp_plugin_check_address;
2510 api->address_to_string = &tcp_address_to_string;
2511 api->string_to_address = &tcp_string_to_address;
2512 api->get_network = &tcp_get_network;
2513 api->update_session_timeout = &tcp_plugin_update_session_timeout;
2514 api->update_inbound_delay = &tcp_plugin_update_inbound_delay;
2515 plugin->service = service;
2516 if (NULL != service)
2518 plugin->server = GNUNET_SERVICE_get_server (service);
2523 != GNUNET_CONFIGURATION_get_value_time (env->cfg, "transport-tcp",
2524 "TIMEOUT", &idle_timeout))
2526 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "transport-tcp",
2528 if (plugin->nat != NULL )
2529 GNUNET_NAT_unregister (plugin->nat);
2530 GNUNET_free(plugin);
2534 plugin->server = GNUNET_SERVER_create_with_sockets (
2535 &plugin_tcp_access_check, plugin, NULL, idle_timeout, GNUNET_YES);
2537 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
2538 memcpy (plugin->handlers, my_handlers, sizeof(my_handlers));
2540 i < sizeof(my_handlers) / sizeof(struct GNUNET_SERVER_MessageHandler);
2542 plugin->handlers[i].callback_cls = plugin;
2544 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
2545 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify, plugin);
2546 plugin->nat_wait_conns = GNUNET_CONTAINER_multipeermap_create (16,
2549 LOG(GNUNET_ERROR_TYPE_INFO, _("TCP transport listening on port %llu\n"),
2552 LOG(GNUNET_ERROR_TYPE_INFO,
2553 _("TCP transport not listening on any port (client only)\n"));
2555 LOG(GNUNET_ERROR_TYPE_INFO,
2556 _("TCP transport advertises itself as being on port %llu\n"), aport);
2557 /* Initially set connections to 0 */
2558 GNUNET_assert(NULL != plugin->env->stats);
2559 GNUNET_STATISTICS_set (plugin->env->stats,
2560 gettext_noop ("# TCP sessions active"), 0, GNUNET_NO);
2565 * Exit point from the plugin.
2567 * @param cls the `struct GNUNET_TRANSPORT_PluginFunctions`
2571 libgnunet_plugin_transport_tcp_done (void *cls)
2573 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2574 struct Plugin *plugin = api->cls;
2575 struct TCPProbeContext *tcp_probe;
2576 struct PrettyPrinterContext *cur;
2577 struct PrettyPrinterContext *next;
2584 LOG(GNUNET_ERROR_TYPE_DEBUG, "Shutting down TCP plugin\n");
2586 /* Removing leftover sessions */
2587 GNUNET_CONTAINER_multipeermap_iterate (plugin->sessionmap,
2588 &session_disconnect_it, plugin);
2589 /* Removing leftover NAT sessions */
2590 GNUNET_CONTAINER_multipeermap_iterate (plugin->nat_wait_conns,
2591 &session_disconnect_it, plugin);
2593 next = ppc_dll_head;
2594 for (cur = next; NULL != cur; cur = next)
2597 GNUNET_CONTAINER_DLL_remove(ppc_dll_head, ppc_dll_tail, cur);
2598 if (NULL != cur->resolver_handle)
2599 GNUNET_RESOLVER_request_cancel (cur->resolver_handle);
2600 GNUNET_SCHEDULER_cancel (cur->timeout_task);
2605 if (plugin->service != NULL )
2606 GNUNET_SERVICE_stop (plugin->service);
2608 GNUNET_SERVER_destroy (plugin->server);
2609 GNUNET_free(plugin->handlers);
2610 if (plugin->nat != NULL )
2611 GNUNET_NAT_unregister (plugin->nat);
2612 while (NULL != (tcp_probe = plugin->probe_head))
2614 GNUNET_CONTAINER_DLL_remove(plugin->probe_head, plugin->probe_tail,
2616 GNUNET_CONNECTION_destroy (tcp_probe->sock);
2617 GNUNET_free(tcp_probe);
2619 GNUNET_CONTAINER_multipeermap_destroy (plugin->nat_wait_conns);
2620 GNUNET_CONTAINER_multipeermap_destroy (plugin->sessionmap);
2621 GNUNET_free(plugin);
2626 /* end of plugin_transport_tcp.c */