2 This file is part of GNUnet
3 (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 2, 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.
22 * @file transport/plugin_transport_tcp.c
23 * @brief Implementation of the TCP transport service
24 * @author Christian Grothoff
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_network_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_peerinfo_service.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_server_lib.h"
35 #include "gnunet_service_lib.h"
36 #include "gnunet_statistics_service.h"
37 #include "gnunet_transport_service.h"
38 #include "plugin_transport.h"
39 #include "transport.h"
41 #define DEBUG_TCP GNUNET_NO
44 * After how long do we expire an address that we
45 * learned from another peer if it is not reconfirmed
48 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
51 * How long until we give up on transmitting the welcome message?
53 #define WELCOME_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
56 * How long until we give up on transmitting the welcome message?
58 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 * For how many messages back to we keep transmission times?
63 #define ACK_LOG_SIZE 32
66 * Initial handshake message for a session. This header
67 * is followed by the address that the other peer used to
68 * connect to us (so that we may learn it) or the address
69 * that the other peer got from the accept call.
73 struct GNUNET_MessageHeader header;
76 * Identity of the node connecting (TCP client)
78 struct GNUNET_PeerIdentity clientIdentity;
84 * Encapsulation for normal TCP traffic.
88 struct GNUNET_MessageHeader header;
93 uint32_t reserved GNUNET_PACKED;
96 * Number of the last message that was received from the other peer.
98 uint64_t ack_in GNUNET_PACKED;
101 * Number of this outgoing message.
103 uint64_t ack_out GNUNET_PACKED;
106 * How long was sending this ack delayed by the other peer
107 * (estimate). The receiver of this message can use the delay
108 * between sending his message number 'ack' and receiving this ack
109 * minus the delay as an estimate of the round-trip time.
111 struct GNUNET_TIME_RelativeNBO delay;
117 * Encapsulation of all of the state of the plugin.
123 * Information kept for each message that is yet to
126 struct PendingMessage
130 * This is a linked list.
132 struct PendingMessage *next;
135 * The pending message, pointer to the end
136 * of this struct, do not free!
138 struct GNUNET_MessageHeader *msg;
142 * Continuation function to call once the message
143 * has been sent. Can be NULL if there is no
144 * continuation to call.
146 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
149 * Closure for transmit_cont.
151 void *transmit_cont_cls;
154 * Timeout value for the pending message.
156 struct GNUNET_TIME_Absolute timeout;
159 * GNUNET_YES if this is a welcome message;
160 * otherwise this should be a DATA message.
168 * Session handle for TCP connections.
174 * Stored in a linked list.
176 struct Session *next;
179 * Pointer to the global plugin struct.
181 struct Plugin *plugin;
184 * The client (used to identify this connection)
186 struct GNUNET_SERVER_Client *client;
189 * gnunet-service-transport context for this connection.
191 struct ReadyList *service_context;
194 * Messages currently pending for transmission
195 * to this peer, if any.
197 struct PendingMessage *pending_messages;
200 * Handle for pending transmission request.
202 struct GNUNET_NETWORK_TransmitHandle *transmit_handle;
205 * To whom are we talking to (set to our identity
206 * if we are still waiting for the welcome message)
208 struct GNUNET_PeerIdentity target;
211 * At what time did we reset last_received last?
213 struct GNUNET_TIME_Absolute last_quota_update;
216 * Address of the other peer if WE initiated the connection
217 * (and hence can be sure what it is), otherwise NULL.
222 * How many bytes have we received since the "last_quota_update"
225 uint64_t last_received;
228 * Our current latency estimate (in ms).
230 double latency_estimate;
233 * Time when we generated the last ACK_LOG_SIZE acks.
234 * (the "last" refers to the "out_msg_counter" here)
236 struct GNUNET_TIME_Absolute gen_time[ACK_LOG_SIZE];
239 * Our current sequence number.
241 uint64_t out_msg_counter;
244 * Highest received incoming sequence number.
246 uint64_t max_in_msg_counter;
249 * Number of bytes per ms that this peer is allowed
255 * Length of connect_addr, can be 0.
260 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
262 int expecting_welcome;
265 * Are we still trying to connect?
267 int still_connecting;
273 * Encapsulation of all of the state of the plugin.
280 struct GNUNET_TRANSPORT_PluginEnvironment *env;
285 struct GNUNET_NETWORK_SocketHandle *lsock;
288 * List of open TCP sessions.
290 struct Session *sessions;
293 * Handle for the statistics service.
295 struct GNUNET_STATISTICS_Handle *statistics;
298 * Handle to the network service.
300 struct GNUNET_SERVICE_Context *service;
303 * Handle to the server for this service.
305 struct GNUNET_SERVER_Handle *server;
308 * Copy of the handler array where the closures are
309 * set to this struct's instance.
311 struct GNUNET_SERVER_MessageHandler *handlers;
314 * ID of task used to update our addresses when one expires.
316 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
319 * Port that we are actually listening on.
324 * Port that the user said we would have visible to the
333 * Find the session handle for the given peer.
335 static struct Session *
336 find_session_by_target (struct Plugin *plugin,
337 const struct GNUNET_PeerIdentity *target)
341 ret = plugin->sessions;
342 while ((ret != NULL) &&
343 (0 != memcmp (target,
344 &ret->target, sizeof (struct GNUNET_PeerIdentity))))
351 * Find the session handle for the given peer.
353 static struct Session *
354 find_session_by_client (struct Plugin *plugin,
355 const struct GNUNET_SERVER_Client *client)
359 ret = plugin->sessions;
360 while ((ret != NULL) && (client != ret->client))
367 * Create a welcome message.
369 static struct PendingMessage *
370 create_welcome (size_t addrlen, const void *addr, struct Plugin *plugin)
372 struct PendingMessage *pm;
373 struct WelcomeMessage *welcome;
375 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
376 sizeof (struct WelcomeMessage) + addrlen);
377 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
378 welcome = (struct WelcomeMessage *) &pm[1];
379 welcome->header.size = htons (sizeof (struct WelcomeMessage) + addrlen);
380 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
381 GNUNET_CRYPTO_hash (plugin->env->my_public_key,
382 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
383 &welcome->clientIdentity.hashPubKey);
384 memcpy (&welcome[1], addr, addrlen);
385 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
386 pm->is_welcome = GNUNET_YES;
392 * Create a new session using the specified address
393 * for the welcome message.
396 * @param target peer to connect to
397 * @param client client to use
398 * @param addrlen IPv4 or IPv6
399 * @param addr either struct sockaddr_in or struct sockaddr_in6
400 * @return NULL connection failed / invalid address
402 static struct Session *
403 create_session (struct Plugin *plugin,
404 const struct GNUNET_PeerIdentity *target,
405 struct GNUNET_SERVER_Client *client,
406 const void *addr, size_t addrlen)
410 ret = GNUNET_malloc (sizeof (struct Session));
411 ret->plugin = plugin;
412 ret->next = plugin->sessions;
413 plugin->sessions = ret;
414 ret->client = client;
415 ret->target = *target;
416 ret->last_quota_update = GNUNET_TIME_absolute_get ();
417 ret->quota_in = plugin->env->default_quota_in;
418 ret->expecting_welcome = GNUNET_YES;
419 ret->pending_messages = create_welcome (addrlen, addr, plugin);
425 * Create a new session connecting to the specified
426 * target at the specified address.
429 * @param target peer to connect to
430 * @param addrlen IPv4 or IPv6
431 * @param addr either struct sockaddr_in or struct sockaddr_in6
432 * @return NULL connection failed / invalid address
434 static struct Session *
435 connect_and_create_session (struct Plugin *plugin,
436 const struct GNUNET_PeerIdentity *target,
437 const void *addr, size_t addrlen)
439 struct GNUNET_SERVER_Client *client;
440 struct GNUNET_NETWORK_SocketHandle *conn;
441 struct Session *session;
444 session = plugin->sessions;
445 while (session != NULL)
447 if ((0 == memcmp (target,
449 sizeof (struct GNUNET_PeerIdentity))) &&
450 (session->connect_alen == addrlen) &&
451 (0 == memcmp (session->connect_addr, addr, addrlen)))
452 return session; /* already exists! */
453 session = session->next;
456 if (addrlen == sizeof (struct sockaddr_in))
458 else if (addrlen == sizeof (struct sockaddr_in6))
463 return NULL; /* invalid address */
465 conn = GNUNET_NETWORK_socket_create_from_sockaddr (plugin->env->sched,
469 GNUNET_SERVER_MAX_MESSAGE_SIZE);
473 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
475 "Failed to create connection to peer at `%s'.\n",
476 GNUNET_a2s(addr, addrlen));
480 client = GNUNET_SERVER_connect_socket (plugin->server, conn);
481 GNUNET_assert (client != NULL);
482 session = create_session (plugin, target, client, addr, addrlen);
483 session->connect_alen = addrlen;
484 session->connect_addr = GNUNET_malloc (addrlen);
485 memcpy (session->connect_addr, addr, addrlen);
487 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
489 "Creating new session %p with `%s' based on `%s' request.\n",
491 GNUNET_a2s(addr, addrlen),
499 * If we have pending messages, ask the server to
500 * transmit them (schedule the respective tasks, etc.)
502 * @param session for which session should we do this
504 static void process_pending_messages (struct Session *session);
508 * Function called to notify a client about the socket
509 * begin ready to queue more data. "buf" will be
510 * NULL and "size" zero if the socket was closed for
511 * writing in the meantime.
514 * @param size number of bytes available in buf
515 * @param buf where the callee should write the message
516 * @return number of bytes written to buf
519 do_transmit (void *cls, size_t size, void *buf)
521 struct Session *session = cls;
522 struct PendingMessage *pm;
526 struct DataMessage *dm;
528 session->transmit_handle = NULL;
532 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
534 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
535 GNUNET_i2s(&session->target));
538 while (NULL != (pm = session->pending_messages))
540 session->pending_messages = pm->next;
541 if (pm->transmit_cont != NULL)
542 pm->transmit_cont (pm->transmit_cont_cls,
543 session->service_context,
544 &session->target, GNUNET_SYSERR);
551 while (NULL != (pm = session->pending_messages))
555 if (size < (msize = htons (pm->msg->size)))
557 memcpy (cbuf, pm->msg, msize);
565 sizeof (struct DataMessage) + (msize = htons (pm->msg->size)))
567 dm = (struct DataMessage *) cbuf;
568 dm->header.size = htons (sizeof (struct DataMessage) + msize);
569 dm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA);
570 dm->ack_out = GNUNET_htonll (++session->out_msg_counter);
571 dm->ack_in = GNUNET_htonll (session->max_in_msg_counter);
572 cbuf += sizeof (struct DataMessage);
573 ret += sizeof (struct DataMessage);
574 size -= sizeof (struct DataMessage);
575 memcpy (cbuf, pm->msg, msize);
580 session->pending_messages = pm->next;
581 if (pm->transmit_cont != NULL)
582 pm->transmit_cont (pm->transmit_cont_cls,
583 session->service_context,
584 &session->target, GNUNET_OK);
586 session->gen_time[session->out_msg_counter % ACK_LOG_SIZE]
587 = GNUNET_TIME_absolute_get ();
589 process_pending_messages (session);
591 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
592 "tcp", "Transmitting %u bytes\n", ret);
599 * If we have pending messages, ask the server to
600 * transmit them (schedule the respective tasks, etc.)
602 * @param session for which session should we do this
605 process_pending_messages (struct Session *session)
607 GNUNET_assert (session->client != NULL);
608 if (session->pending_messages == NULL)
610 if (session->transmit_handle != NULL)
612 session->transmit_handle
613 = GNUNET_SERVER_notify_transmit_ready (session->client,
615 pending_messages->msg->
618 pending_messages->is_welcome ? 0 :
619 sizeof (struct DataMessage)),
620 GNUNET_TIME_absolute_get_remaining
622 pending_messages[0].timeout),
623 &do_transmit, session);
628 * Function that can be used by the transport service to transmit
629 * a message using the plugin using a fresh connection (even if
630 * we already have a connection to this peer, this function is
631 * required to establish a new one).
634 * @param target who should receive this message
635 * @param msg1 first message to transmit
636 * @param msg2 second message to transmit (can be NULL)
637 * @param timeout how long should we try to transmit these?
638 * @param addrlen length of the address
639 * @param addr the address
640 * @return session if the transmission has been scheduled
641 * NULL if the address format is invalid
644 tcp_plugin_send_to (void *cls,
645 const struct GNUNET_PeerIdentity *target,
646 const struct GNUNET_MessageHeader *msg1,
647 const struct GNUNET_MessageHeader *msg2,
648 struct GNUNET_TIME_Relative timeout,
649 const void *addr, size_t addrlen)
651 struct Plugin *plugin = cls;
652 struct Session *session;
653 struct PendingMessage *pl;
654 struct PendingMessage *pm;
656 session = connect_and_create_session (plugin, target, addr, addrlen);
660 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
661 "tcp", "Failed to create fresh session.\n");
668 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
670 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
671 memcpy (pm->msg, msg2, ntohs (msg2->size));
672 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
673 pm->is_welcome = GNUNET_NO;
678 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
680 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
681 memcpy (pm->msg, msg1, ntohs (msg1->size));
682 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
683 pm->is_welcome = GNUNET_NO;
688 if (session->pending_messages != NULL)
690 pm = session->pending_messages;
691 while (pm->next != NULL)
697 session->pending_messages = pl;
699 process_pending_messages (session);
705 * Functions with this signature are called whenever we need
706 * to close a session due to a disconnect or failure to
707 * establish a connection.
709 * @param session session to close down
712 disconnect_session (struct Session *session)
714 struct Session *prev;
716 struct PendingMessage *pm;
719 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
721 "Disconnecting from other peer (session %p).\n", session);
723 /* remove from session list */
725 pos = session->plugin->sessions;
726 while (pos != session)
732 session->plugin->sessions = session->next;
734 prev->next = session->next;
736 if (session->client != NULL)
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Disconnecting from client address %p\n", session->client);
742 GNUNET_SERVER_client_drop (session->client);
743 session->client = NULL;
745 if (session->transmit_handle != NULL)
747 GNUNET_NETWORK_notify_transmit_ready_cancel (session->transmit_handle);
748 session->transmit_handle = NULL;
750 while (NULL != (pm = session->pending_messages))
752 session->pending_messages = pm->next;
753 if (NULL != pm->transmit_cont)
754 pm->transmit_cont (pm->transmit_cont_cls,
755 session->service_context,
756 &session->target, GNUNET_SYSERR);
759 /* notify transport service about disconnect */
760 session->plugin->env->receive (session->plugin->env->cls,
762 session->service_context,
763 GNUNET_TIME_UNIT_ZERO,
764 &session->target, NULL);
765 GNUNET_free_non_null (session->connect_addr);
766 GNUNET_free (session);
771 * Iterator callback to go over all addresses. If we get
772 * a TCP address, increment the counter
774 * @param cls closure, points to the counter
775 * @param tname name of the transport
776 * @param expiration expiration time
777 * @param addr the address
778 * @param addrlen length of the address
779 * @return GNUNET_OK to keep the address,
780 * GNUNET_NO to delete it from the HELLO
781 * GNUNET_SYSERR to stop iterating (but keep current address)
784 count_tcp_addresses (void *cls,
786 struct GNUNET_TIME_Absolute expiration,
787 const void *addr, size_t addrlen)
789 unsigned int *counter = cls;
791 if (0 != strcmp (tname, "tcp"))
792 return GNUNET_OK; /* not one of ours */
794 return GNUNET_OK; /* failed to connect */
798 struct ConnectContext
800 struct Plugin *plugin;
802 struct GNUNET_NETWORK_SocketHandle *sa;
804 struct PendingMessage *welcome;
811 * Iterator callback to go over all addresses. If we get
812 * the "pos" TCP address, try to connect to it.
815 * @param tname name of the transport
816 * @param expiration expiration time
817 * @param addrlen length of the address
818 * @param addr the address
819 * @return GNUNET_OK to keep the address,
820 * GNUNET_NO to delete it from the HELLO
821 * GNUNET_SYSERR to stop iterating (but keep current address)
824 try_connect_to_address (void *cls,
826 struct GNUNET_TIME_Absolute expiration,
827 const void *addr, size_t addrlen)
829 struct ConnectContext *cc = cls;
832 if (0 != strcmp (tname, "tcp"))
833 return GNUNET_OK; /* not one of ours */
834 if (sizeof (struct sockaddr_in) == addrlen)
836 else if (sizeof (struct sockaddr_in6) == addrlen)
840 /* not a valid address */
846 cc->welcome = create_welcome (addrlen, addr, cc->plugin);
848 GNUNET_NETWORK_socket_create_from_sockaddr (cc->plugin->env->sched,
850 GNUNET_SERVER_MAX_MESSAGE_SIZE);
852 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
853 "tcp", "Connected to other peer.\n");
855 return GNUNET_SYSERR;
857 return GNUNET_OK; /* failed to connect */
862 * Type of an iterator over the hosts. Note that each
863 * host will be called with each available protocol.
866 * @param peer id of the peer, NULL for last call
867 * @param hello hello message for the peer (can be NULL)
868 * @param trust amount of trust we have in the peer
871 session_try_connect (void *cls,
872 const struct GNUNET_PeerIdentity *peer,
873 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
875 struct Session *session = cls;
877 struct ConnectContext cctx;
878 struct PendingMessage *pm;
882 /* last call, destroy session if we are still not
884 if (session->still_connecting == GNUNET_NO)
887 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
889 "Connected to other peer, now processing messages.\n");
891 process_pending_messages (session);
896 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
898 "Failed to connect to other peer, now closing session.\n");
900 disconnect_session (session);
904 if ((hello == NULL) || (session->client != NULL))
906 GNUNET_break (0); /* should this ever happen!? */
910 GNUNET_HELLO_iterate_addresses (hello,
911 GNUNET_NO, &count_tcp_addresses, &count);
915 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
917 "Asked to connect, but have no addresses to try.\n");
921 cctx.plugin = session->plugin;
924 cctx.pos = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
925 GNUNET_HELLO_iterate_addresses (hello,
926 GNUNET_NO, &try_connect_to_address, &cctx);
930 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
932 "Asked to connect, but all addresses failed.\n");
934 GNUNET_free_non_null (cctx.welcome);
937 session->client = GNUNET_SERVER_connect_socket (session->plugin->server,
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Connected getting client address %p\n", session->client);
943 if (session->client == NULL)
945 GNUNET_break (0); /* how could this happen? */
946 GNUNET_free_non_null (cctx.welcome);
951 pm->next = session->pending_messages;
952 session->pending_messages = pm;
953 session->still_connecting = GNUNET_NO;
955 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
957 "Connected to other peer, now sending `%s' message.\n",
964 * Function that can be used by the transport service to transmit
965 * a message using the plugin.
968 * @param plugin_context value we were asked to pass to this plugin
969 * to respond to the given peer (use is optional,
970 * but may speed up processing), can be NULL
971 * @param service_context value passed to the transport-service
972 * to identify the neighbour
973 * @param target who should receive this message
974 * @param msg the message to transmit
975 * @param cont continuation to call once the message has
976 * been transmitted (or if the transport is ready
977 * for the next transmission call; or if the
978 * peer disconnected...)
979 * @param cont_cls closure for cont
980 * @return plugin_context that should be used next time for
981 * sending messages to the specified peer
984 tcp_plugin_send (void *cls,
985 void *plugin_context,
986 struct ReadyList *service_context,
987 const struct GNUNET_PeerIdentity *target,
988 const struct GNUNET_MessageHeader *msg,
989 struct GNUNET_TIME_Relative timeout,
990 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
992 struct Plugin *plugin = cls;
993 struct Session *session = plugin_context;
994 struct PendingMessage *pm;
995 struct PendingMessage *pme;
998 session = find_session_by_target (plugin, target);
999 pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
1000 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
1001 memcpy (pm->msg, msg, ntohs (msg->size));
1002 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1003 pm->transmit_cont = cont;
1004 pm->transmit_cont_cls = cont_cls;
1005 if (session == NULL)
1007 session = GNUNET_malloc (sizeof (struct Session));
1009 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1011 "Asked to transmit, creating fresh session %p.\n",
1014 session->next = plugin->sessions;
1015 plugin->sessions = session;
1016 session->plugin = plugin;
1017 session->target = *target;
1018 session->last_quota_update = GNUNET_TIME_absolute_get ();
1019 session->quota_in = plugin->env->default_quota_in;
1020 session->expecting_welcome = GNUNET_YES;
1021 session->still_connecting = GNUNET_YES;
1022 session->pending_messages = pm;
1023 session->service_context = service_context;
1024 GNUNET_PEERINFO_for_all (plugin->env->cfg,
1027 0, timeout, &session_try_connect, session);
1030 GNUNET_assert (session != NULL);
1031 GNUNET_assert (session->still_connecting == GNUNET_NO);
1032 session->service_context = service_context;
1033 /* append pm to pending_messages list */
1034 pme = session->pending_messages;
1037 session->pending_messages = pm;
1041 while (NULL != pme->next)
1046 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1047 "tcp", "Asked to transmit, added message to list.\n");
1049 process_pending_messages (session);
1056 * Function that can be called to force a disconnect from the
1057 * specified neighbour. This should also cancel all previously
1058 * scheduled transmissions. Obviously the transmission may have been
1059 * partially completed already, which is OK. The plugin is supposed
1060 * to close the connection (if applicable) and no longer call the
1061 * transmit continuation(s).
1063 * Finally, plugin MUST NOT call the services's receive function to
1064 * notify the service that the connection to the specified target was
1065 * closed after a getting this call.
1067 * @param cls closure
1068 * @param plugin_context value we were asked to pass to this plugin
1069 * to respond to the given peer (use is optional,
1070 * but may speed up processing), can be NULL (if
1071 * NULL was returned from the transmit function)
1072 * @param service_context must correspond to the service context
1073 * of the corresponding Transmit call; the plugin should
1074 * not cancel a send call made with a different service
1075 * context pointer! Never NULL.
1076 * @param target peer for which the last transmission is
1080 tcp_plugin_cancel (void *cls,
1081 void *plugin_context,
1082 struct ReadyList *service_context,
1083 const struct GNUNET_PeerIdentity *target)
1085 struct Plugin *plugin = cls;
1086 struct PendingMessage *pm;
1087 struct Session *session;
1088 struct Session *next;
1090 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1092 "Asked to close session with `%4s'\n",
1093 GNUNET_i2s(target));
1094 session = plugin->sessions;
1095 while (session != NULL)
1097 next = session->next;
1098 if (0 == memcmp (target,
1099 &session->target, sizeof (struct GNUNET_PeerIdentity)))
1101 pm = session->pending_messages;
1104 pm->transmit_cont = NULL;
1105 pm->transmit_cont_cls = NULL;
1108 session->service_context = NULL;
1109 GNUNET_SERVER_client_disconnect (session->client);
1110 /* rest of the clean-up of the session will be done as part of
1111 disconnect_notify which should be triggered any time now */
1118 struct PrettyPrinterContext
1120 GNUNET_TRANSPORT_AddressStringCallback asc;
1127 * Append our port and forward the result.
1130 append_port (void *cls, const char *hostname)
1132 struct PrettyPrinterContext *ppc = cls;
1135 if (hostname == NULL)
1137 ppc->asc (ppc->asc_cls, NULL);
1141 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1142 ppc->asc (ppc->asc_cls, ret);
1148 * Convert the transports address to a nice, human-readable
1151 * @param cls closure
1152 * @param name name of the transport that generated the address
1153 * @param addr one of the addresses of the host, NULL for the last address
1154 * the specific address format depends on the transport
1155 * @param addrlen length of the address
1156 * @param numeric should (IP) addresses be displayed in numeric form?
1157 * @param timeout after how long should we give up?
1158 * @param asc function to call on each string
1159 * @param asc_cls closure for asc
1162 tcp_plugin_address_pretty_printer (void *cls,
1167 struct GNUNET_TIME_Relative timeout,
1168 GNUNET_TRANSPORT_AddressStringCallback asc,
1171 struct Plugin *plugin = cls;
1172 const struct sockaddr_in *v4;
1173 const struct sockaddr_in6 *v6;
1174 struct PrettyPrinterContext *ppc;
1176 if ((addrlen != sizeof (struct sockaddr_in)) &&
1177 (addrlen != sizeof (struct sockaddr_in6)))
1179 /* invalid address */
1180 GNUNET_break_op (0);
1181 asc (asc_cls, NULL);
1184 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1186 ppc->asc_cls = asc_cls;
1187 if (addrlen == sizeof (struct sockaddr_in))
1189 v4 = (const struct sockaddr_in *) addr;
1190 ppc->port = ntohs (v4->sin_port);
1194 v6 = (const struct sockaddr_in6 *) addr;
1195 ppc->port = ntohs (v6->sin6_port);
1198 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1202 !numeric, timeout, &append_port, ppc);
1207 * Update the last-received and bandwidth quota values
1210 * @param session session to update
1211 * @param force set to GNUNET_YES if we should update even
1212 * though the minimum refresh time has not yet expired
1215 update_quota (struct Session *session, int force)
1217 struct GNUNET_TIME_Absolute now;
1218 unsigned long long delta;
1219 unsigned long long total_allowed;
1220 unsigned long long total_remaining;
1222 now = GNUNET_TIME_absolute_get ();
1223 delta = now.value - session->last_quota_update.value;
1224 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1225 return; /* too early, not enough data */
1227 total_allowed = session->quota_in * delta;
1228 if (total_allowed > session->last_received)
1230 /* got less than acceptable */
1231 total_remaining = total_allowed - session->last_received;
1232 session->last_received = 0;
1233 delta = total_remaining / session->quota_in; /* bonus seconds */
1234 if (delta > MAX_BANDWIDTH_CARRY)
1235 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
1239 /* got more than acceptable */
1240 total_remaining = 0;
1241 session->last_received -= total_allowed;
1244 session->last_quota_update.value = now.value - delta;
1249 * Set a quota for receiving data from the given peer; this is a
1250 * per-transport limit. The transport should limit its read/select
1251 * calls to stay below the quota (in terms of incoming data).
1253 * @param cls closure
1254 * @param peer the peer for whom the quota is given
1255 * @param quota_in quota for receiving/sending data in bytes per ms
1258 tcp_plugin_set_receive_quota (void *cls,
1259 const struct GNUNET_PeerIdentity *target,
1262 struct Plugin *plugin = cls;
1263 struct Session *session;
1265 session = find_session_by_target (plugin, target);
1266 if (session->quota_in != quota_in)
1268 update_quota (session, GNUNET_YES);
1269 if (session->quota_in > quota_in)
1270 session->last_quota_update = GNUNET_TIME_absolute_get ();
1271 session->quota_in = quota_in;
1277 * Check if the given port is plausible (must be either
1278 * our listen port or our advertised port). If it is
1279 * neither, we return one of these two ports at random.
1281 * @return either in_port or a more plausible port
1284 check_port (struct Plugin *plugin, uint16_t in_port)
1286 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1288 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1290 ? plugin->open_port : plugin->adv_port;
1295 * Another peer has suggested an address for this
1296 * peer and transport plugin. Check that this could be a valid
1297 * address. If so, consider adding it to the list
1300 * @param cls closure
1301 * @param addr pointer to the address
1302 * @param addrlen length of addr
1303 * @return GNUNET_OK if this is a plausible address for this peer
1307 tcp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1309 struct Plugin *plugin = cls;
1310 char buf[sizeof (struct sockaddr_in6)];
1311 struct sockaddr_in *v4;
1312 struct sockaddr_in6 *v6;
1314 if ((addrlen != sizeof (struct sockaddr_in)) &&
1315 (addrlen != sizeof (struct sockaddr_in6)))
1317 GNUNET_break_op (0);
1318 return GNUNET_SYSERR;
1320 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1321 if (addrlen == sizeof (struct sockaddr_in))
1323 v4 = (struct sockaddr_in *) buf;
1324 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1328 v6 = (struct sockaddr_in6 *) buf;
1329 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1332 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1334 "Informing transport service about my address `%s'.\n",
1335 GNUNET_a2s(addr, addrlen));
1337 plugin->env->notify_address (plugin->env->cls,
1339 buf, addrlen, LEARNED_ADDRESS_EXPIRATION);
1345 * We've received a welcome from this peer via TCP.
1346 * Possibly create a fresh client record and send back
1349 * @param cls closure
1350 * @param client identification of the client
1351 * @param message the actual message
1354 handle_tcp_welcome (void *cls,
1355 struct GNUNET_SERVER_Client *client,
1356 const struct GNUNET_MessageHeader *message)
1358 struct Plugin *plugin = cls;
1359 struct Session *session_c;
1360 const struct WelcomeMessage *wm;
1365 const struct sockaddr *addr;
1368 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1370 "Received `%s' message from %p.\n", "WELCOME", client);
1372 msize = ntohs (message->size);
1373 if (msize < sizeof (struct WelcomeMessage))
1375 GNUNET_break_op (0);
1376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1379 wm = (const struct WelcomeMessage *) message;
1380 session_c = find_session_by_client (plugin, client);
1381 if (session_c == NULL)
1384 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1385 GNUNET_SERVER_client_keep (client);
1386 session_c = create_session (plugin,
1387 &wm->clientIdentity, client, vaddr, alen);
1389 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1391 "Creating new session %p for incoming `%s' message.\n",
1392 session_c, "WELCOME");
1394 GNUNET_free_non_null (vaddr);
1395 process_pending_messages (session_c);
1397 session_c->expecting_welcome = GNUNET_NO;
1398 if (0 < (addrlen = msize - sizeof (struct WelcomeMessage)))
1400 addr = (const struct sockaddr *) &wm[1];
1401 tcp_plugin_address_suggested (plugin, addr, addrlen);
1403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1408 * Calculate how long we should delay reading from the TCP socket to
1409 * ensure that we stay within our bandwidth limits (push back).
1411 * @param session for which client should this be calculated
1413 static struct GNUNET_TIME_Relative
1414 calculate_throttle_delay (struct Session *session)
1416 struct GNUNET_TIME_Relative ret;
1417 struct GNUNET_TIME_Absolute now;
1422 now = GNUNET_TIME_absolute_get ();
1423 del = now.value - session->last_quota_update.value;
1424 if (del > MAX_BANDWIDTH_CARRY)
1426 update_quota (session, GNUNET_YES);
1427 del = now.value - session->last_quota_update.value;
1428 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1430 if (session->quota_in == 0)
1431 session->quota_in = 1; /* avoid divison by zero */
1432 avail = del * session->quota_in;
1433 if (avail > session->last_received)
1434 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1435 excess = session->last_received - avail;
1436 ret.value = excess / session->quota_in;
1442 * Task to signal the server that we can continue
1443 * receiving from the TCP client now.
1446 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1448 struct Session *session = cls;
1449 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1454 * We've received data for this peer via TCP. Unbox,
1455 * compute latency and forward.
1457 * @param cls closure
1458 * @param client identification of the client
1459 * @param message the actual message
1462 handle_tcp_data (void *cls,
1463 struct GNUNET_SERVER_Client *client,
1464 const struct GNUNET_MessageHeader *message)
1466 struct Plugin *plugin = cls;
1467 struct Session *session;
1468 const struct DataMessage *dm;
1470 const struct GNUNET_MessageHeader *msg;
1471 struct GNUNET_TIME_Relative latency;
1472 struct GNUNET_TIME_Absolute ttime;
1473 struct GNUNET_TIME_Absolute now;
1474 struct GNUNET_TIME_Relative delay;
1478 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1479 "tcp", "Receiving data from other peer.\n");
1481 msize = ntohs (message->size);
1483 sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1485 GNUNET_break_op (0);
1486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1489 session = find_session_by_client (plugin, client);
1490 if ((NULL == session) || (GNUNET_YES == session->expecting_welcome))
1492 GNUNET_break_op (0);
1493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1496 dm = (const struct DataMessage *) message;
1497 session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1498 GNUNET_ntohll (dm->ack_out));
1499 msg = (const struct GNUNET_MessageHeader *) &dm[1];
1500 if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1502 GNUNET_break_op (0);
1503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1506 /* estimate latency */
1507 ack_in = GNUNET_ntohll (dm->ack_in);
1508 if ((ack_in <= session->out_msg_counter) &&
1509 (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1511 delay = GNUNET_TIME_relative_ntoh (dm->delay);
1512 ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1513 now = GNUNET_TIME_absolute_get ();
1514 if (delay.value > now.value - ttime.value)
1515 delay.value = 0; /* not plausible */
1516 /* update (round-trip) latency using ageing; we
1517 use 7:1 so that we can reasonably quickly react
1518 to changes, but not so fast that latency is largely
1520 session->latency_estimate
1521 = ((7 * session->latency_estimate) +
1522 (now.value - ttime.value - delay.value)) / 8;
1524 latency.value = (uint64_t) session->latency_estimate;
1527 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1529 "Forwarding data of type %u to transport service.\n",
1532 session->service_context
1533 = plugin->env->receive (plugin->env->cls,
1535 session->service_context,
1536 latency, &session->target, msg);
1537 /* update bandwidth used */
1538 session->last_received += msize;
1539 update_quota (session, GNUNET_NO);
1541 delay = calculate_throttle_delay (session);
1542 if (delay.value == 0)
1543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1545 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1547 GNUNET_SCHEDULER_PRIORITY_HIGH,
1548 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
1549 delay, &delayed_done, session);
1554 * Handlers for the various TCP messages.
1556 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1557 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 0},
1558 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1564 create_tcp_handlers (struct Plugin *plugin)
1567 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1568 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1571 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1573 plugin->handlers[i].callback_cls = plugin;
1574 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1579 * Functions with this signature are called whenever a peer
1580 * is disconnected on the network level.
1582 * @param cls closure
1583 * @param client identification of the client
1586 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1588 struct Plugin *plugin = cls;
1589 struct Session *session;
1592 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1594 "Notified about network-level disconnect of client %p.\n",
1597 session = find_session_by_client (plugin, client);
1598 if (session == NULL)
1599 return; /* unknown, nothing to do */
1601 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1602 "tcp", "Will now destroy session %p.\n", session);
1604 disconnect_session (session);
1609 * Add the IP of our network interface to the list of
1610 * our external IP addresses.
1613 process_interfaces (void *cls,
1616 const struct sockaddr *addr, socklen_t addrlen)
1618 struct Plugin *plugin = cls;
1620 struct sockaddr_in *v4;
1621 struct sockaddr_in6 *v6;
1623 af = addr->sa_family;
1626 v4 = (struct sockaddr_in *) addr;
1627 v4->sin_port = htons (plugin->adv_port);
1631 GNUNET_assert (af == AF_INET6);
1632 v6 = (struct sockaddr_in6 *) addr;
1633 v6->sin6_port = htons (plugin->adv_port);
1635 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1636 GNUNET_ERROR_TYPE_BULK,
1637 "tcp", _("Found address `%s' (%s)\n"),
1638 GNUNET_a2s(addr, addrlen), name);
1639 plugin->env->notify_address (plugin->env->cls,
1641 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1647 * Function called by the resolver for each address obtained from DNS
1648 * for our own hostname. Add the addresses to the list of our
1649 * external IP addresses.
1651 * @param cls closure
1652 * @param addr one of the addresses of the host, NULL for the last address
1653 * @param addrlen length of the address
1656 process_hostname_ips (void *cls,
1657 const struct sockaddr *addr, socklen_t addrlen)
1659 struct Plugin *plugin = cls;
1663 plugin->env->notify_address (plugin->env->cls,
1665 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1670 * Entry point for the plugin.
1673 libgnunet_plugin_transport_tcp_init (void *cls)
1675 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1676 struct GNUNET_TRANSPORT_PluginFunctions *api;
1677 struct Plugin *plugin;
1678 struct GNUNET_SERVICE_Context *service;
1679 unsigned long long aport;
1680 unsigned long long bport;
1682 service = GNUNET_SERVICE_start ("tcp", env->sched, env->cfg);
1683 if (service == NULL)
1685 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1688 ("Failed to start service for `%s' transport plugin.\n"),
1694 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1700 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1703 &aport)) && (aport > 65535)))
1705 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1708 ("Require valid port number for service `%s' in configuration!\n"),
1710 GNUNET_SERVICE_stop (service);
1715 plugin = GNUNET_malloc (sizeof (struct Plugin));
1716 plugin->open_port = bport;
1717 plugin->adv_port = aport;
1719 plugin->lsock = NULL;
1720 plugin->statistics = NULL;
1721 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1723 api->send_to = &tcp_plugin_send_to;
1724 api->send = &tcp_plugin_send;
1725 api->cancel = &tcp_plugin_cancel;
1726 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1727 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1728 api->address_suggested = &tcp_plugin_address_suggested;
1729 api->cost_estimate = 42; /* TODO: ATS */
1730 plugin->service = service;
1731 plugin->server = GNUNET_SERVICE_get_server (service);
1732 create_tcp_handlers (plugin);
1733 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1734 "tcp", _("TCP transport listening on port %u\n"), bport);
1736 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1739 ("TCP transport advertises itself as being on port %u\n"),
1741 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1743 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1744 GNUNET_RESOLVER_hostname_resolve (env->sched,
1747 HOSTNAME_RESOLVE_TIMEOUT,
1748 &process_hostname_ips, plugin);
1754 * Exit point from the plugin.
1757 libgnunet_plugin_transport_tcp_done (void *cls)
1759 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1760 struct Plugin *plugin = api->cls;
1762 GNUNET_SERVICE_stop (plugin->service);
1763 GNUNET_free (plugin->handlers);
1764 GNUNET_free (plugin);
1769 /* end of plugin_transport_tcp.c */