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_connection_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_signatures.h"
37 #include "gnunet_statistics_service.h"
38 #include "gnunet_transport_service.h"
39 #include "plugin_transport.h"
40 #include "transport.h"
42 #define DEBUG_TCP GNUNET_NO
45 * After how long do we expire an address that we
46 * learned from another peer if it is not reconfirmed
49 #define LEARNED_ADDRESS_EXPIRATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 6)
52 * How long until we give up on transmitting the welcome message?
54 #define WELCOME_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
57 * How long until we give up on transmitting the welcome message?
59 #define HOSTNAME_RESOLVE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 * For how many messages back to we keep transmission times?
64 #define ACK_LOG_SIZE 32
69 * Message used to ask a peer to validate receipt (to check an address
70 * from a HELLO). Followed by the address used. Note that the
71 * recipients response does not affirm that he has this address,
72 * only that he got the challenge message.
74 struct ValidationChallengeMessage
78 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING
80 struct GNUNET_MessageHeader header;
83 * Random challenge number (in network byte order).
85 uint32_t challenge GNUNET_PACKED;
88 * Who is the intended recipient?
90 struct GNUNET_PeerIdentity target;
96 * Message used to validate a HELLO. The challenge is included in the
97 * confirmation to make matching of replies to requests possible. The
98 * signature signs the original challenge number, our public key, the
99 * sender's address (so that the sender can check that the address we
100 * saw is plausible for him and possibly detect a MiM attack) and a
101 * timestamp (to limit replay).<p>
103 * This message is followed by the address of the
104 * client that we are observing (which is part of what
107 struct ValidationChallengeResponse
111 * Type will be GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG
113 struct GNUNET_MessageHeader header;
116 * For padding, always zero.
118 uint32_t reserved GNUNET_PACKED;
123 struct GNUNET_CRYPTO_RsaSignature signature;
126 * What are we signing and why?
128 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
131 * Random challenge number (in network byte order).
133 uint32_t challenge GNUNET_PACKED;
136 * Who signed this message?
138 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded signer;
145 * Initial handshake message for a session. This header
146 * is followed by the address that the other peer used to
147 * connect to us (so that we may learn it) or the address
148 * that the other peer got from the accept call.
150 struct WelcomeMessage
152 struct GNUNET_MessageHeader header;
155 * Identity of the node connecting (TCP client)
157 struct GNUNET_PeerIdentity clientIdentity;
163 * Encapsulation for normal TCP traffic.
167 struct GNUNET_MessageHeader header;
172 uint32_t reserved GNUNET_PACKED;
175 * Number of the last message that was received from the other peer.
177 uint64_t ack_in GNUNET_PACKED;
180 * Number of this outgoing message.
182 uint64_t ack_out GNUNET_PACKED;
185 * How long was sending this ack delayed by the other peer
186 * (estimate). The receiver of this message can use the delay
187 * between sending his message number 'ack' and receiving this ack
188 * minus the delay as an estimate of the round-trip time.
190 struct GNUNET_TIME_RelativeNBO delay;
196 * Encapsulation of all of the state of the plugin.
202 * Information kept for each message that is yet to
205 struct PendingMessage
209 * This is a linked list.
211 struct PendingMessage *next;
214 * The pending message, pointer to the end
215 * of this struct, do not free!
217 struct GNUNET_MessageHeader *msg;
220 * Continuation function to call once the message
221 * has been sent. Can be NULL if there is no
222 * continuation to call.
224 GNUNET_TRANSPORT_TransmitContinuation transmit_cont;
227 * Closure for transmit_cont.
229 void *transmit_cont_cls;
232 * Timeout value for the pending message.
234 struct GNUNET_TIME_Absolute timeout;
237 * GNUNET_YES if this is a welcome message;
238 * otherwise this should be a DATA message.
246 * Session handle for TCP connections.
252 * Stored in a linked list.
254 struct Session *next;
257 * Pointer to the global plugin struct.
259 struct Plugin *plugin;
262 * The client (used to identify this connection)
264 struct GNUNET_SERVER_Client *client;
267 * gnunet-service-transport context for this connection.
269 struct ReadyList *service_context;
272 * Messages currently pending for transmission
273 * to this peer, if any.
275 struct PendingMessage *pending_messages;
278 * Handle for pending transmission request.
280 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
283 * To whom are we talking to (set to our identity
284 * if we are still waiting for the welcome message)
286 struct GNUNET_PeerIdentity target;
289 * At what time did we reset last_received last?
291 struct GNUNET_TIME_Absolute last_quota_update;
294 * Address of the other peer if WE initiated the connection
295 * (and hence can be sure what it is), otherwise NULL.
300 * How many bytes have we received since the "last_quota_update"
303 uint64_t last_received;
306 * Our current latency estimate (in ms).
308 double latency_estimate;
311 * Time when we generated the last ACK_LOG_SIZE acks.
312 * (the "last" refers to the "out_msg_counter" here)
314 struct GNUNET_TIME_Absolute gen_time[ACK_LOG_SIZE];
317 * Our current sequence number.
319 uint64_t out_msg_counter;
322 * Highest received incoming sequence number.
324 uint64_t max_in_msg_counter;
327 * Number of bytes per ms that this peer is allowed
333 * Length of connect_addr, can be 0.
338 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
339 * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
342 int expecting_welcome;
348 * Encapsulation of all of the state of the plugin.
355 struct GNUNET_TRANSPORT_PluginEnvironment *env;
360 struct GNUNET_CONNECTION_Handle *lsock;
363 * List of open TCP sessions.
365 struct Session *sessions;
368 * Handle for the statistics service.
370 struct GNUNET_STATISTICS_Handle *statistics;
373 * Handle to the network service.
375 struct GNUNET_SERVICE_Context *service;
378 * Handle to the server for this service.
380 struct GNUNET_SERVER_Handle *server;
383 * Copy of the handler array where the closures are
384 * set to this struct's instance.
386 struct GNUNET_SERVER_MessageHandler *handlers;
389 * ID of task used to update our addresses when one expires.
391 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
394 * Port that we are actually listening on.
399 * Port that the user said we would have visible to the
408 * Find the session handle for the given peer.
410 static struct Session *
411 find_session_by_target (struct Plugin *plugin,
412 const struct GNUNET_PeerIdentity *target)
416 ret = plugin->sessions;
417 while ((ret != NULL) &&
418 ( (GNUNET_SYSERR == ret->expecting_welcome) ||
419 (0 != memcmp (target,
420 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
427 * Find the session handle for the given peer.
429 static struct Session *
430 find_session_by_client (struct Plugin *plugin,
431 const struct GNUNET_SERVER_Client *client)
435 ret = plugin->sessions;
436 while ((ret != NULL) && (client != ret->client))
443 * Create a welcome message.
445 static struct PendingMessage *
446 create_welcome (size_t addrlen, const void *addr, struct Plugin *plugin)
448 struct PendingMessage *pm;
449 struct WelcomeMessage *welcome;
451 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
452 sizeof (struct WelcomeMessage) + addrlen);
453 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
454 welcome = (struct WelcomeMessage *) &pm[1];
455 welcome->header.size = htons (sizeof (struct WelcomeMessage) + addrlen);
456 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
457 GNUNET_CRYPTO_hash (plugin->env->my_public_key,
458 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
459 &welcome->clientIdentity.hashPubKey);
460 memcpy (&welcome[1], addr, addrlen);
461 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
462 pm->is_welcome = GNUNET_YES;
468 * Create a new session using the specified address
469 * for the welcome message.
472 * @param target peer to connect to
473 * @param client client to use
474 * @param addrlen IPv4 or IPv6
475 * @param addr either struct sockaddr_in or struct sockaddr_in6
476 * @return NULL connection failed / invalid address
478 static struct Session *
479 create_session (struct Plugin *plugin,
480 const struct GNUNET_PeerIdentity *target,
481 struct GNUNET_SERVER_Client *client,
482 const void *addr, size_t addrlen)
486 ret = GNUNET_malloc (sizeof (struct Session));
487 ret->plugin = plugin;
488 ret->next = plugin->sessions;
489 plugin->sessions = ret;
490 ret->client = client;
491 ret->target = *target;
492 ret->last_quota_update = GNUNET_TIME_absolute_get ();
493 ret->quota_in = plugin->env->default_quota_in;
494 ret->expecting_welcome = GNUNET_YES;
495 ret->pending_messages = create_welcome (addrlen, addr, plugin);
501 * If we have pending messages, ask the server to
502 * transmit them (schedule the respective tasks, etc.)
504 * @param session for which session should we do this
506 static void process_pending_messages (struct Session *session);
510 * Function called to notify a client about the socket
511 * begin ready to queue more data. "buf" will be
512 * NULL and "size" zero if the socket was closed for
513 * writing in the meantime.
516 * @param size number of bytes available in buf
517 * @param buf where the callee should write the message
518 * @return number of bytes written to buf
521 do_transmit (void *cls, size_t size, void *buf)
523 struct Session *session = cls;
524 struct PendingMessage *pm;
528 struct DataMessage *dm;
530 session->transmit_handle = NULL;
534 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
536 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
537 GNUNET_i2s(&session->target));
540 while (NULL != (pm = session->pending_messages))
542 session->pending_messages = pm->next;
544 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
546 "Failed to transmit message of type %u to `%4s'.\n",
547 ntohs(pm->msg->type),
548 GNUNET_i2s(&session->target));
550 if (pm->transmit_cont != NULL)
551 pm->transmit_cont (pm->transmit_cont_cls,
552 session->service_context,
553 &session->target, GNUNET_SYSERR);
560 while (NULL != (pm = session->pending_messages))
564 if (size < (msize = htons (pm->msg->size)))
566 memcpy (cbuf, pm->msg, msize);
574 sizeof (struct DataMessage) + (msize = htons (pm->msg->size)))
576 dm = (struct DataMessage *) cbuf;
577 dm->header.size = htons (sizeof (struct DataMessage) + msize);
578 dm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA);
579 dm->ack_out = GNUNET_htonll (++session->out_msg_counter);
580 dm->ack_in = GNUNET_htonll (session->max_in_msg_counter);
581 cbuf += sizeof (struct DataMessage);
582 ret += sizeof (struct DataMessage);
583 size -= sizeof (struct DataMessage);
584 memcpy (cbuf, pm->msg, msize);
589 session->pending_messages = pm->next;
590 if (pm->transmit_cont != NULL)
591 pm->transmit_cont (pm->transmit_cont_cls,
592 session->service_context,
593 &session->target, GNUNET_OK);
595 session->gen_time[session->out_msg_counter % ACK_LOG_SIZE]
596 = GNUNET_TIME_absolute_get ();
598 process_pending_messages (session);
600 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
601 "tcp", "Transmitting %u bytes\n", ret);
608 * If we have pending messages, ask the server to
609 * transmit them (schedule the respective tasks, etc.)
611 * @param session for which session should we do this
614 process_pending_messages (struct Session *session)
616 GNUNET_assert (session->client != NULL);
617 if (session->pending_messages == NULL)
619 if (session->transmit_handle != NULL)
621 session->transmit_handle
622 = GNUNET_SERVER_notify_transmit_ready (session->client,
624 pending_messages->msg->
627 pending_messages->is_welcome ? 0 :
628 sizeof (struct DataMessage)),
629 GNUNET_TIME_absolute_get_remaining
631 pending_messages[0].timeout),
632 &do_transmit, session);
638 * Create a new session connecting to the specified
639 * target at the specified address. The session will
640 * be used to verify an address in a HELLO and should
641 * not expect to receive a WELCOME.
644 * @param target peer to connect to
645 * @param addrlen IPv4 or IPv6
646 * @param addr either struct sockaddr_in or struct sockaddr_in6
647 * @return NULL connection failed / invalid address
649 static struct Session *
650 connect_and_create_validation_session (struct Plugin *plugin,
651 const struct GNUNET_PeerIdentity *target,
652 const void *addr, size_t addrlen)
654 struct GNUNET_SERVER_Client *client;
655 struct GNUNET_CONNECTION_Handle *conn;
656 struct Session *session;
659 if (addrlen == sizeof (struct sockaddr_in))
661 else if (addrlen == sizeof (struct sockaddr_in6))
666 return NULL; /* invalid address */
668 conn = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
672 GNUNET_SERVER_MAX_MESSAGE_SIZE);
676 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
678 "Failed to create connection to peer at `%s'.\n",
679 GNUNET_a2s(addr, addrlen));
683 client = GNUNET_SERVER_connect_socket (plugin->server, conn);
684 GNUNET_assert (client != NULL);
685 session = create_session (plugin, target, client, addr, addrlen);
687 GNUNET_free (session->pending_messages);
688 session->pending_messages = NULL;
689 session->connect_alen = addrlen;
690 session->connect_addr = GNUNET_malloc (addrlen);
691 session->expecting_welcome = GNUNET_SYSERR;
692 memcpy (session->connect_addr, addr, addrlen);
694 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
696 "Creating new session %p with `%s' for `%4s' based on `%s' request.\n",
698 GNUNET_a2s (addr, addrlen),
699 GNUNET_i2s (&session->target),
707 * Function that can be used by the transport service to validate that
708 * another peer is reachable at a particular address (even if we
709 * already have a connection to this peer, this function is required
710 * to establish a new one).
713 * @param target who should receive this message
714 * @param challenge challenge code to use
715 * @param addrlen length of the address
716 * @param addr the address
717 * @param timeout how long should we try to transmit these?
718 * @return GNUNET_OK if the transmission has been scheduled
721 tcp_plugin_validate (void *cls,
722 const struct GNUNET_PeerIdentity *target,
724 struct GNUNET_TIME_Relative timeout,
725 const void *addr, size_t addrlen)
727 struct Plugin *plugin = cls;
728 struct Session *session;
729 struct PendingMessage *pm;
730 struct ValidationChallengeMessage *vcm;
732 session = connect_and_create_validation_session (plugin, target, addr, addrlen);
736 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
737 "tcp", "Failed to create fresh session.\n");
739 return GNUNET_SYSERR;
741 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
742 sizeof (struct ValidationChallengeMessage) + addrlen);
743 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
744 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
745 pm->is_welcome = GNUNET_YES;
746 vcm = (struct ValidationChallengeMessage*) &pm[1];
748 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
749 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING);
750 vcm->challenge = htonl(challenge);
751 vcm->target = *target;
752 memcpy (&vcm[1], addr, addrlen);
753 GNUNET_assert (session->pending_messages == NULL);
754 session->pending_messages = pm;
755 process_pending_messages (session);
761 * Functions with this signature are called whenever we need
762 * to close a session due to a disconnect or failure to
763 * establish a connection.
765 * @param session session to close down
768 disconnect_session (struct Session *session)
770 struct Session *prev;
772 struct PendingMessage *pm;
775 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
777 "Disconnecting from `%4s' at %s (session %p).\n",
778 GNUNET_i2s(&session->target),
779 (session->connect_addr != NULL) ?
780 GNUNET_a2s(session->connect_addr,
781 session->connect_alen) : "*",
784 /* remove from session list */
786 pos = session->plugin->sessions;
787 while (pos != session)
793 session->plugin->sessions = session->next;
795 prev->next = session->next;
797 if (session->transmit_handle != NULL)
799 GNUNET_CONNECTION_notify_transmit_ready_cancel (session->transmit_handle);
800 session->transmit_handle = NULL;
802 while (NULL != (pm = session->pending_messages))
805 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
807 pm->transmit_cont != NULL
808 ? "Could not deliver message of type %u to `%4s'.\n"
809 : "Could not deliver message of type %u to `%4s', notifying.\n",
810 ntohs(pm->msg->type),
811 GNUNET_i2s(&session->target));
813 session->pending_messages = pm->next;
814 if (NULL != pm->transmit_cont)
815 pm->transmit_cont (pm->transmit_cont_cls,
816 session->service_context,
817 &session->target, GNUNET_SYSERR);
820 if (GNUNET_NO == session->expecting_welcome)
823 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
825 "Notifying transport service about loss of data connection with `%4s'.\n",
826 GNUNET_i2s(&session->target));
828 /* Data session that actually went past the
829 initial handshake; transport service may
830 know about this one, so we need to
831 notify transport service about disconnect */
832 session->plugin->env->receive (session->plugin->env->cls,
834 session->service_context,
835 GNUNET_TIME_UNIT_ZERO,
836 &session->target, NULL);
838 if (session->client != NULL)
840 GNUNET_SERVER_client_drop (session->client);
841 session->client = NULL;
843 GNUNET_free_non_null (session->connect_addr);
844 GNUNET_free (session);
849 * Iterator callback to go over all addresses. If we get
850 * a TCP address, increment the counter
852 * @param cls closure, points to the counter
853 * @param tname name of the transport
854 * @param expiration expiration time
855 * @param addr the address
856 * @param addrlen length of the address
857 * @return GNUNET_OK to keep the address,
858 * GNUNET_NO to delete it from the HELLO
859 * GNUNET_SYSERR to stop iterating (but keep current address)
862 count_tcp_addresses (void *cls,
864 struct GNUNET_TIME_Absolute expiration,
865 const void *addr, size_t addrlen)
867 unsigned int *counter = cls;
869 if (0 != strcmp (tname, "tcp"))
870 return GNUNET_OK; /* not one of ours */
872 return GNUNET_OK; /* failed to connect */
876 struct ConnectContext
878 struct Plugin *plugin;
880 struct GNUNET_CONNECTION_Handle *sa;
882 struct PendingMessage *welcome;
889 * Iterator callback to go over all addresses. If we get
890 * the "pos" TCP address, try to connect to it.
893 * @param tname name of the transport
894 * @param expiration expiration time
895 * @param addrlen length of the address
896 * @param addr the address
897 * @return GNUNET_OK to keep the address,
898 * GNUNET_NO to delete it from the HELLO
899 * GNUNET_SYSERR to stop iterating (but keep current address)
902 try_connect_to_address (void *cls,
904 struct GNUNET_TIME_Absolute expiration,
905 const void *addr, size_t addrlen)
907 struct ConnectContext *cc = cls;
910 if (0 != strcmp (tname, "tcp"))
911 return GNUNET_OK; /* not one of ours */
912 if (sizeof (struct sockaddr_in) == addrlen)
914 else if (sizeof (struct sockaddr_in6) == addrlen)
918 /* not a valid address */
924 cc->welcome = create_welcome (addrlen, addr, cc->plugin);
926 GNUNET_CONNECTION_create_from_sockaddr (cc->plugin->env->sched,
928 GNUNET_SERVER_MAX_MESSAGE_SIZE);
930 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
932 "Connecting using address %s.\n",
933 GNUNET_a2s(addr, addrlen));
935 return GNUNET_SYSERR;
937 return GNUNET_OK; /* failed to connect */
942 * Type of an iterator over the hosts. Note that each
943 * host will be called with each available protocol.
946 * @param peer id of the peer, NULL for last call
947 * @param hello hello message for the peer (can be NULL)
948 * @param trust amount of trust we have in the peer
951 session_try_connect (void *cls,
952 const struct GNUNET_PeerIdentity *peer,
953 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
955 struct Session *session = cls;
957 struct ConnectContext cctx;
958 struct PendingMessage *pm;
962 /* last call, destroy session if we are still not
964 if (session->client != NULL)
967 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
969 "Now connected to `%4s', now processing messages.\n",
970 GNUNET_i2s(&session->target));
972 process_pending_messages (session);
977 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
979 "Failed to connect to `%4s' (no working `%s'), closing session.\n",
980 GNUNET_i2s(&session->target),
983 disconnect_session (session);
987 if ((hello == NULL) || (session->client != NULL))
989 GNUNET_break (0); /* should this ever happen!? */
993 GNUNET_HELLO_iterate_addresses (hello,
994 GNUNET_NO, &count_tcp_addresses, &count);
998 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1000 "Asked to connect to `%4s', but have no addresses to try.\n",
1001 GNUNET_i2s(&session->target));
1005 cctx.plugin = session->plugin;
1007 cctx.welcome = NULL;
1008 cctx.pos = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1009 GNUNET_HELLO_iterate_addresses (hello,
1010 GNUNET_NO, &try_connect_to_address, &cctx);
1011 if (cctx.sa == NULL)
1014 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1016 "Asked to connect, but all addresses failed.\n");
1018 GNUNET_free_non_null (cctx.welcome);
1021 session->client = GNUNET_SERVER_connect_socket (session->plugin->server,
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 "Connected to `%4s' for session %p\n",
1026 GNUNET_i2s(&session->target),
1029 if (session->client == NULL)
1031 GNUNET_break (0); /* how could this happen? */
1032 GNUNET_free_non_null (cctx.welcome);
1037 pm->next = session->pending_messages;
1038 session->pending_messages = pm;
1040 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1042 "Connected to `%4s', now sending `%s' message.\n",
1043 GNUNET_i2s(&session->target),
1050 * Function that can be used by the transport service to transmit
1051 * a message using the plugin.
1053 * @param cls closure
1054 * @param plugin_context value we were asked to pass to this plugin
1055 * to respond to the given peer (use is optional,
1056 * but may speed up processing), can be NULL
1057 * @param service_context value passed to the transport-service
1058 * to identify the neighbour
1059 * @param target who should receive this message
1060 * @param priority how important is the message
1061 * @param msg the message to transmit
1062 * @param timeout when should we time out (give up) if we can not transmit?
1063 * @param cont continuation to call once the message has
1064 * been transmitted (or if the transport is ready
1065 * for the next transmission call; or if the
1066 * peer disconnected...)
1067 * @param cont_cls closure for cont
1068 * @return plugin_context that should be used next time for
1069 * sending messages to the specified peer
1072 tcp_plugin_send (void *cls,
1073 void *plugin_context,
1074 struct ReadyList *service_context,
1075 const struct GNUNET_PeerIdentity *target,
1076 unsigned int priority,
1077 const struct GNUNET_MessageHeader *msg,
1078 struct GNUNET_TIME_Relative timeout,
1079 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1081 struct Plugin *plugin = cls;
1082 struct Session *session = plugin_context;
1083 struct PendingMessage *pm;
1084 struct PendingMessage *pme;
1086 if (session == NULL)
1087 session = find_session_by_target (plugin, target);
1088 pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
1089 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
1090 memcpy (pm->msg, msg, ntohs (msg->size));
1091 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1092 pm->transmit_cont = cont;
1093 pm->transmit_cont_cls = cont_cls;
1094 if (session == NULL)
1096 session = GNUNET_malloc (sizeof (struct Session));
1098 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1100 "Asked to transmit, creating fresh session %p.\n",
1103 session->next = plugin->sessions;
1104 plugin->sessions = session;
1105 session->plugin = plugin;
1106 session->target = *target;
1107 session->last_quota_update = GNUNET_TIME_absolute_get ();
1108 session->quota_in = plugin->env->default_quota_in;
1109 session->expecting_welcome = GNUNET_YES;
1110 session->pending_messages = pm;
1111 session->service_context = service_context;
1112 GNUNET_PEERINFO_for_all (plugin->env->cfg,
1115 0, timeout, &session_try_connect, session);
1118 GNUNET_assert (session != NULL);
1119 GNUNET_assert (session->client != NULL);
1120 session->service_context = service_context;
1121 /* append pm to pending_messages list */
1122 pme = session->pending_messages;
1125 session->pending_messages = pm;
1129 while (NULL != pme->next)
1134 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1135 "tcp", "Asked to transmit, added message to list.\n");
1137 process_pending_messages (session);
1144 * Function that can be called to force a disconnect from the
1145 * specified neighbour. This should also cancel all previously
1146 * scheduled transmissions. Obviously the transmission may have been
1147 * partially completed already, which is OK. The plugin is supposed
1148 * to close the connection (if applicable) and no longer call the
1149 * transmit continuation(s).
1151 * Finally, plugin MUST NOT call the services's receive function to
1152 * notify the service that the connection to the specified target was
1153 * closed after a getting this call.
1155 * @param cls closure
1156 * @param plugin_context value we were asked to pass to this plugin
1157 * to respond to the given peer (use is optional,
1158 * but may speed up processing), can be NULL (if
1159 * NULL was returned from the transmit function); note
1160 * that use of NULL is dangerous since then this call may
1161 * cancel any session with the target peer (including
1162 * HELLO validation sessions), which is likely not what
1164 * @param service_context must correspond to the service context
1165 * of the corresponding Transmit call; the plugin should
1166 * not cancel a send call made with a different service
1167 * context pointer! Never NULL.
1168 * @param target peer for which the last transmission is
1172 tcp_plugin_cancel (void *cls,
1173 void *plugin_context,
1174 struct ReadyList *service_context,
1175 const struct GNUNET_PeerIdentity *target)
1177 struct Plugin *plugin = cls;
1178 struct Session *session = plugin_context;
1179 struct PendingMessage *pm;
1181 if (session == NULL)
1184 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1186 "Asked to cancel with `%4s' without specification of specifics; will try to find an applicable session\n",
1187 GNUNET_i2s(target));
1189 session = find_session_by_target (plugin, target);
1191 if (session == NULL)
1197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1199 "Asked to cancel session %p with `%4s'\n",
1201 GNUNET_i2s(target));
1203 pm = session->pending_messages;
1206 pm->transmit_cont = NULL;
1207 pm->transmit_cont_cls = NULL;
1210 session->service_context = NULL;
1211 if (session->client != NULL)
1213 GNUNET_SERVER_client_drop (session->client);
1214 session->client = NULL;
1216 /* rest of the clean-up of the session will be done as part of
1217 disconnect_notify which should be triggered any time now
1218 (or which may be triggering this call in the first place) */
1222 struct PrettyPrinterContext
1224 GNUNET_TRANSPORT_AddressStringCallback asc;
1231 * Append our port and forward the result.
1234 append_port (void *cls, const char *hostname)
1236 struct PrettyPrinterContext *ppc = cls;
1239 if (hostname == NULL)
1241 ppc->asc (ppc->asc_cls, NULL);
1245 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1246 ppc->asc (ppc->asc_cls, ret);
1252 * Convert the transports address to a nice, human-readable
1255 * @param cls closure
1256 * @param type name of the transport that generated the address
1257 * @param addr one of the addresses of the host, NULL for the last address
1258 * the specific address format depends on the transport
1259 * @param addrlen length of the address
1260 * @param numeric should (IP) addresses be displayed in numeric form?
1261 * @param timeout after how long should we give up?
1262 * @param asc function to call on each string
1263 * @param asc_cls closure for asc
1266 tcp_plugin_address_pretty_printer (void *cls,
1271 struct GNUNET_TIME_Relative timeout,
1272 GNUNET_TRANSPORT_AddressStringCallback asc,
1275 struct Plugin *plugin = cls;
1276 const struct sockaddr_in *v4;
1277 const struct sockaddr_in6 *v6;
1278 struct PrettyPrinterContext *ppc;
1280 if ((addrlen != sizeof (struct sockaddr_in)) &&
1281 (addrlen != sizeof (struct sockaddr_in6)))
1283 /* invalid address */
1284 GNUNET_break_op (0);
1285 asc (asc_cls, NULL);
1288 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1290 ppc->asc_cls = asc_cls;
1291 if (addrlen == sizeof (struct sockaddr_in))
1293 v4 = (const struct sockaddr_in *) addr;
1294 ppc->port = ntohs (v4->sin_port);
1298 v6 = (const struct sockaddr_in6 *) addr;
1299 ppc->port = ntohs (v6->sin6_port);
1302 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1306 !numeric, timeout, &append_port, ppc);
1311 * Update the last-received and bandwidth quota values
1314 * @param session session to update
1315 * @param force set to GNUNET_YES if we should update even
1316 * though the minimum refresh time has not yet expired
1319 update_quota (struct Session *session, int force)
1321 struct GNUNET_TIME_Absolute now;
1322 unsigned long long delta;
1323 unsigned long long total_allowed;
1324 unsigned long long total_remaining;
1326 now = GNUNET_TIME_absolute_get ();
1327 delta = now.value - session->last_quota_update.value;
1328 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1329 return; /* too early, not enough data */
1331 total_allowed = session->quota_in * delta;
1332 if (total_allowed > session->last_received)
1334 /* got less than acceptable */
1335 total_remaining = total_allowed - session->last_received;
1336 session->last_received = 0;
1337 delta = total_remaining / session->quota_in; /* bonus seconds */
1338 if (delta > MAX_BANDWIDTH_CARRY)
1339 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
1343 /* got more than acceptable */
1344 session->last_received -= total_allowed;
1347 session->last_quota_update.value = now.value - delta;
1352 * Set a quota for receiving data from the given peer; this is a
1353 * per-transport limit. The transport should limit its read/select
1354 * calls to stay below the quota (in terms of incoming data).
1356 * @param cls closure
1357 * @param target the peer for whom the quota is given
1358 * @param quota_in quota for receiving/sending data in bytes per ms
1361 tcp_plugin_set_receive_quota (void *cls,
1362 const struct GNUNET_PeerIdentity *target,
1365 struct Plugin *plugin = cls;
1366 struct Session *session;
1368 session = find_session_by_target (plugin, target);
1369 if (session == NULL)
1370 return; /* peer must have disconnected, ignore */
1371 if (session->quota_in != quota_in)
1373 update_quota (session, GNUNET_YES);
1374 if (session->quota_in > quota_in)
1375 session->last_quota_update = GNUNET_TIME_absolute_get ();
1376 session->quota_in = quota_in;
1382 * Check if the given port is plausible (must be either
1383 * our listen port or our advertised port). If it is
1384 * neither, we return one of these two ports at random.
1386 * @return either in_port or a more plausible port
1389 check_port (struct Plugin *plugin, uint16_t in_port)
1391 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1393 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1395 ? plugin->open_port : plugin->adv_port;
1400 * Another peer has suggested an address for this
1401 * peer and transport plugin. Check that this could be a valid
1402 * address. If so, consider adding it to the list
1405 * @param cls closure
1406 * @param addr pointer to the address
1407 * @param addrlen length of addr
1408 * @return GNUNET_OK if this is a plausible address for this peer
1412 tcp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1414 struct Plugin *plugin = cls;
1415 char buf[sizeof (struct sockaddr_in6)];
1416 struct sockaddr_in *v4;
1417 struct sockaddr_in6 *v6;
1419 if ((addrlen != sizeof (struct sockaddr_in)) &&
1420 (addrlen != sizeof (struct sockaddr_in6)))
1422 GNUNET_break_op (0);
1423 return GNUNET_SYSERR;
1425 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1426 if (addrlen == sizeof (struct sockaddr_in))
1428 v4 = (struct sockaddr_in *) buf;
1429 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1433 v6 = (struct sockaddr_in6 *) buf;
1434 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1437 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1439 "Informing transport service about my address `%s'.\n",
1440 GNUNET_a2s(addr, addrlen));
1442 plugin->env->notify_address (plugin->env->cls,
1444 buf, addrlen, LEARNED_ADDRESS_EXPIRATION);
1450 * Send a validation challenge response.
1453 send_vcr (void *cls,
1457 struct ValidationChallengeResponse *vcr = cls;
1465 msize = ntohs(vcr->header.size);
1466 GNUNET_assert (size >= msize);
1467 memcpy (buf, vcr, msize);
1474 * We've received a PING from this peer via TCP.
1475 * Send back our PONG.
1477 * @param cls closure
1478 * @param client identification of the client
1479 * @param message the actual message
1482 handle_tcp_ping (void *cls,
1483 struct GNUNET_SERVER_Client *client,
1484 const struct GNUNET_MessageHeader *message)
1486 struct Plugin *plugin = cls;
1487 const struct ValidationChallengeMessage *vcm;
1488 struct ValidationChallengeResponse *vcr;
1495 GNUNET_SERVER_client_get_address (client,
1499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1500 "Processing `%s' from `%s'\n",
1502 GNUNET_a2s (addr, addrlen));
1506 msize = ntohs (message->size);
1507 if (msize < sizeof (struct ValidationChallengeMessage))
1509 GNUNET_break_op (0);
1510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1513 vcm = (const struct ValidationChallengeMessage *) message;
1514 if (0 != memcmp (&vcm->target,
1515 plugin->env->my_identity, sizeof (struct GNUNET_PeerIdentity)))
1517 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1518 _("Received `%s' message not destined for me!\n"), "PING");
1519 /* TODO: call statistics */
1520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1523 msize -= sizeof (struct ValidationChallengeMessage);
1525 tcp_plugin_address_suggested (plugin, &vcm[1], msize))
1527 GNUNET_break_op (0);
1528 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1532 GNUNET_SERVER_client_get_address (client,
1537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1540 vcr = GNUNET_malloc (sizeof (struct ValidationChallengeResponse) + addrlen);
1541 vcr->header.size = htons (sizeof (struct ValidationChallengeResponse) + addrlen);
1542 vcr->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG);
1544 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1546 sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1548 vcr->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
1549 vcr->challenge = vcm->challenge;
1550 vcr->signer = *plugin->env->my_public_key;
1554 GNUNET_assert (GNUNET_OK ==
1555 GNUNET_CRYPTO_rsa_sign (plugin->env->my_private_key,
1559 GNUNET_assert (GNUNET_OK ==
1560 GNUNET_CRYPTO_rsa_verify
1561 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1564 plugin->env->my_public_key));
1568 GNUNET_SERVER_notify_transmit_ready (client,
1569 sizeof (struct ValidationChallengeResponse) + addrlen,
1570 GNUNET_TIME_UNIT_SECONDS,
1577 /* after a PING, we always close the connection */
1578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1583 * Handle PONG-message.
1585 * @param cls handle for this plugin
1586 * @param client from where did we receive the PONG
1587 * @param message the actual message
1590 handle_tcp_pong (void *cls,
1591 struct GNUNET_SERVER_Client *client,
1592 const struct GNUNET_MessageHeader *message)
1594 struct Plugin *plugin = cls;
1595 const struct ValidationChallengeResponse *vcr;
1596 struct GNUNET_PeerIdentity peer;
1599 const struct sockaddr *addr;
1600 struct sockaddr_in v4;
1601 struct sockaddr_in6 v6;
1604 struct sockaddr *claddr;
1607 GNUNET_SERVER_client_get_address (client,
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1612 "Processing `%s' from `%s'\n",
1614 GNUNET_a2s (claddr, addrlen));
1615 GNUNET_free (claddr);
1618 if (ntohs(message->size) < sizeof(struct ValidationChallengeResponse))
1620 GNUNET_break_op (0);
1621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1624 addrlen = ntohs(message->size) - sizeof(struct ValidationChallengeResponse);
1625 vcr = (const struct ValidationChallengeResponse *) message;
1626 if ( (ntohl(vcr->purpose.size) !=
1627 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1629 sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1632 GNUNET_break_op (0);
1633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1637 GNUNET_CRYPTO_rsa_verify
1638 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1643 GNUNET_break_op (0);
1644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1647 GNUNET_CRYPTO_hash (&vcr->signer,
1648 sizeof( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1650 addr = (const struct sockaddr*) &vcr[1];
1651 if (addrlen == sizeof (struct sockaddr_in))
1653 memcpy (&v4, addr, sizeof (struct sockaddr_in));
1654 v4.sin_port = htons(check_port (plugin, ntohs (v4.sin_port)));
1655 sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v4,
1658 else if (addrlen == sizeof (struct sockaddr_in6))
1660 memcpy (&v6, addr, sizeof (struct sockaddr_in6));
1661 v6.sin6_port = htons(check_port (plugin, ntohs (v6.sin6_port)));
1662 sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v6,
1667 GNUNET_break_op (0);
1668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1671 plugin->env->notify_validation (plugin->env->cls,
1674 ntohl(vcr->challenge),
1676 GNUNET_free (sender_addr);
1677 /* after a PONG, we always close the connection */
1678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1683 * We've received a welcome from this peer via TCP.
1684 * Possibly create a fresh client record and send back
1687 * @param cls closure
1688 * @param client identification of the client
1689 * @param message the actual message
1692 handle_tcp_welcome (void *cls,
1693 struct GNUNET_SERVER_Client *client,
1694 const struct GNUNET_MessageHeader *message)
1696 struct Plugin *plugin = cls;
1697 struct Session *session_c;
1698 const struct WelcomeMessage *wm;
1703 const struct sockaddr *addr;
1705 msize = ntohs (message->size);
1706 if (msize < sizeof (struct WelcomeMessage))
1708 GNUNET_break_op (0);
1709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1712 wm = (const struct WelcomeMessage *) message;
1714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1716 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
1717 GNUNET_i2s(&wm->clientIdentity),
1720 session_c = find_session_by_client (plugin, client);
1721 if (session_c == NULL)
1724 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1725 GNUNET_SERVER_client_keep (client);
1726 session_c = create_session (plugin,
1727 &wm->clientIdentity, client, vaddr, alen);
1729 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1731 "Creating new session %p for incoming `%s' message.\n",
1732 session_c, "WELCOME");
1734 GNUNET_free_non_null (vaddr);
1735 process_pending_messages (session_c);
1737 if (session_c->expecting_welcome != GNUNET_YES)
1739 GNUNET_break_op (0);
1740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1743 session_c->expecting_welcome = GNUNET_NO;
1744 if (0 < (addrlen = msize - sizeof (struct WelcomeMessage)))
1746 addr = (const struct sockaddr *) &wm[1];
1747 tcp_plugin_address_suggested (plugin, addr, addrlen);
1749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1754 * Calculate how long we should delay reading from the TCP socket to
1755 * ensure that we stay within our bandwidth limits (push back).
1757 * @param session for which client should this be calculated
1759 static struct GNUNET_TIME_Relative
1760 calculate_throttle_delay (struct Session *session)
1762 struct GNUNET_TIME_Relative ret;
1763 struct GNUNET_TIME_Absolute now;
1768 now = GNUNET_TIME_absolute_get ();
1769 del = now.value - session->last_quota_update.value;
1770 if (del > MAX_BANDWIDTH_CARRY)
1772 update_quota (session, GNUNET_YES);
1773 del = now.value - session->last_quota_update.value;
1774 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1776 if (session->quota_in == 0)
1777 session->quota_in = 1; /* avoid divison by zero */
1778 avail = del * session->quota_in;
1779 if (avail > session->last_received)
1780 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1781 excess = session->last_received - avail;
1782 ret.value = excess / session->quota_in;
1788 * Task to signal the server that we can continue
1789 * receiving from the TCP client now.
1792 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1794 struct Session *session = cls;
1795 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1800 * We've received data for this peer via TCP. Unbox,
1801 * compute latency and forward.
1803 * @param cls closure
1804 * @param client identification of the client
1805 * @param message the actual message
1808 handle_tcp_data (void *cls,
1809 struct GNUNET_SERVER_Client *client,
1810 const struct GNUNET_MessageHeader *message)
1812 struct Plugin *plugin = cls;
1813 struct Session *session;
1814 const struct DataMessage *dm;
1816 const struct GNUNET_MessageHeader *msg;
1817 struct GNUNET_TIME_Relative latency;
1818 struct GNUNET_TIME_Absolute ttime;
1819 struct GNUNET_TIME_Absolute now;
1820 struct GNUNET_TIME_Relative delay;
1823 msize = ntohs (message->size);
1825 sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1827 GNUNET_break_op (0);
1828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1831 session = find_session_by_client (plugin, client);
1832 if ((NULL == session) || (GNUNET_NO != session->expecting_welcome))
1834 GNUNET_break_op (0);
1835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1839 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1840 "tcp", "Receiving %u bytes from `%4s'.\n",
1842 GNUNET_i2s(&session->target));
1844 dm = (const struct DataMessage *) message;
1845 session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1846 GNUNET_ntohll (dm->ack_out));
1847 msg = (const struct GNUNET_MessageHeader *) &dm[1];
1848 if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1850 GNUNET_break_op (0);
1851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1854 /* estimate latency */
1855 ack_in = GNUNET_ntohll (dm->ack_in);
1856 if ((ack_in <= session->out_msg_counter) &&
1857 (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1859 delay = GNUNET_TIME_relative_ntoh (dm->delay);
1860 ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1861 now = GNUNET_TIME_absolute_get ();
1862 if (delay.value > now.value - ttime.value)
1863 delay.value = 0; /* not plausible */
1864 /* update (round-trip) latency using ageing; we
1865 use 7:1 so that we can reasonably quickly react
1866 to changes, but not so fast that latency is largely
1868 session->latency_estimate
1869 = ((7 * session->latency_estimate) +
1870 (now.value - ttime.value - delay.value)) / 8;
1872 latency.value = (uint64_t) session->latency_estimate;
1875 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1877 "Forwarding data of type %u to transport service.\n",
1880 session->service_context
1881 = plugin->env->receive (plugin->env->cls,
1883 session->service_context,
1884 latency, &session->target, msg);
1885 /* update bandwidth used */
1886 session->last_received += msize;
1887 update_quota (session, GNUNET_NO);
1889 delay = calculate_throttle_delay (session);
1890 if (delay.value == 0)
1891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1893 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1895 GNUNET_SCHEDULER_PRIORITY_HIGH,
1896 GNUNET_SCHEDULER_NO_TASK,
1897 delay, &delayed_done, session);
1902 * Handlers for the various TCP messages.
1904 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1905 {&handle_tcp_ping, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING, 0},
1906 {&handle_tcp_pong, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG, 0},
1907 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 0},
1908 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1914 create_tcp_handlers (struct Plugin *plugin)
1917 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1918 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1921 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1923 plugin->handlers[i].callback_cls = plugin;
1924 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1929 * Functions with this signature are called whenever a peer
1930 * is disconnected on the network level.
1932 * @param cls closure
1933 * @param client identification of the client
1936 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1938 struct Plugin *plugin = cls;
1939 struct Session *session;
1941 session = find_session_by_client (plugin, client);
1942 if (session == NULL)
1943 return; /* unknown, nothing to do */
1945 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1947 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1948 GNUNET_i2s(&session->target),
1949 (session->connect_addr != NULL) ?
1950 GNUNET_a2s(session->connect_addr,
1951 session->connect_alen) : "*",
1954 disconnect_session (session);
1959 * Add the IP of our network interface to the list of
1960 * our external IP addresses.
1963 process_interfaces (void *cls,
1966 const struct sockaddr *addr, socklen_t addrlen)
1968 struct Plugin *plugin = cls;
1970 struct sockaddr_in *v4;
1971 struct sockaddr_in6 *v6;
1973 af = addr->sa_family;
1976 v4 = (struct sockaddr_in *) addr;
1977 v4->sin_port = htons (plugin->adv_port);
1981 GNUNET_assert (af == AF_INET6);
1982 v6 = (struct sockaddr_in6 *) addr;
1983 v6->sin6_port = htons (plugin->adv_port);
1985 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1986 GNUNET_ERROR_TYPE_BULK,
1987 "tcp", _("Found address `%s' (%s)\n"),
1988 GNUNET_a2s(addr, addrlen), name);
1989 plugin->env->notify_address (plugin->env->cls,
1991 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1997 * Function called by the resolver for each address obtained from DNS
1998 * for our own hostname. Add the addresses to the list of our
1999 * external IP addresses.
2001 * @param cls closure
2002 * @param addr one of the addresses of the host, NULL for the last address
2003 * @param addrlen length of the address
2006 process_hostname_ips (void *cls,
2007 const struct sockaddr *addr, socklen_t addrlen)
2009 struct Plugin *plugin = cls;
2013 process_interfaces (plugin,
2022 * Entry point for the plugin.
2025 libgnunet_plugin_transport_tcp_init (void *cls)
2027 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
2028 struct GNUNET_TRANSPORT_PluginFunctions *api;
2029 struct Plugin *plugin;
2030 struct GNUNET_SERVICE_Context *service;
2031 unsigned long long aport;
2032 unsigned long long bport;
2034 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
2035 if (service == NULL)
2037 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2040 ("Failed to start service for `%s' transport plugin.\n"),
2046 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2052 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2055 &aport)) && (aport > 65535)))
2057 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2060 ("Require valid port number for service `%s' in configuration!\n"),
2062 GNUNET_SERVICE_stop (service);
2067 plugin = GNUNET_malloc (sizeof (struct Plugin));
2068 plugin->open_port = bport;
2069 plugin->adv_port = aport;
2071 plugin->lsock = NULL;
2072 plugin->statistics = NULL;
2073 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2075 api->validate = &tcp_plugin_validate;
2076 api->send = &tcp_plugin_send;
2077 api->cancel = &tcp_plugin_cancel;
2078 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2079 api->set_receive_quota = &tcp_plugin_set_receive_quota;
2080 api->address_suggested = &tcp_plugin_address_suggested;
2081 api->cost_estimate = 42; /* TODO: ATS */
2082 plugin->service = service;
2083 plugin->server = GNUNET_SERVICE_get_server (service);
2084 create_tcp_handlers (plugin);
2085 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2086 "tcp", _("TCP transport listening on port %u\n"), bport);
2088 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2091 ("TCP transport advertises itself as being on port %u\n"),
2093 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
2095 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2096 GNUNET_RESOLVER_hostname_resolve (env->sched,
2099 HOSTNAME_RESOLVE_TIMEOUT,
2100 &process_hostname_ips, plugin);
2106 * Exit point from the plugin.
2109 libgnunet_plugin_transport_tcp_done (void *cls)
2111 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2112 struct Plugin *plugin = api->cls;
2113 struct Session *session;
2115 while (NULL != (session = plugin->sessions))
2116 disconnect_session (session);
2117 GNUNET_SERVICE_stop (plugin->service);
2118 GNUNET_free (plugin->handlers);
2119 GNUNET_free (plugin);
2124 /* end of plugin_transport_tcp.c */