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 * Messages currently pending for transmission
268 * to this peer, if any.
270 struct PendingMessage *pending_messages;
273 * Handle for pending transmission request.
275 struct GNUNET_CONNECTION_TransmitHandle *transmit_handle;
278 * To whom are we talking to (set to our identity
279 * if we are still waiting for the welcome message)
281 struct GNUNET_PeerIdentity target;
284 * At what time did we reset last_received last?
286 struct GNUNET_TIME_Absolute last_quota_update;
289 * Context for our iteration to find HELLOs for this peer. NULL
290 * after iteration has completed.
292 struct GNUNET_PEERINFO_IteratorContext *ic;
295 * Address of the other peer if WE initiated the connection
296 * (and hence can be sure what it is), otherwise NULL.
301 * How many bytes have we received since the "last_quota_update"
304 uint64_t last_received;
307 * Our current latency estimate (in ms).
309 double latency_estimate;
312 * Time when we generated the last ACK_LOG_SIZE acks.
313 * (the "last" refers to the "out_msg_counter" here)
315 struct GNUNET_TIME_Absolute gen_time[ACK_LOG_SIZE];
318 * Our current sequence number.
320 uint64_t out_msg_counter;
323 * Highest received incoming sequence number.
325 uint64_t max_in_msg_counter;
328 * Number of bytes per ms that this peer is allowed
334 * Length of connect_addr, can be 0.
339 * Are we still expecting the welcome message? (GNUNET_YES/GNUNET_NO)
340 * GNUNET_SYSERR is used to mark non-welcoming connections (HELLO
343 int expecting_welcome;
349 * Encapsulation of all of the state of the plugin.
356 struct GNUNET_TRANSPORT_PluginEnvironment *env;
361 struct GNUNET_CONNECTION_Handle *lsock;
364 * List of open TCP sessions.
366 struct Session *sessions;
369 * Handle for the statistics service.
371 struct GNUNET_STATISTICS_Handle *statistics;
374 * Handle to the network service.
376 struct GNUNET_SERVICE_Context *service;
379 * Handle to the server for this service.
381 struct GNUNET_SERVER_Handle *server;
384 * Copy of the handler array where the closures are
385 * set to this struct's instance.
387 struct GNUNET_SERVER_MessageHandler *handlers;
390 * Handle for request of hostname resolution, non-NULL if pending.
392 struct GNUNET_RESOLVER_RequestHandle *hostname_dns;
395 * ID of task used to update our addresses when one expires.
397 GNUNET_SCHEDULER_TaskIdentifier address_update_task;
400 * Port that we are actually listening on.
405 * Port that the user said we would have visible to the
414 * Find the session handle for the given peer.
416 static struct Session *
417 find_session_by_target (struct Plugin *plugin,
418 const struct GNUNET_PeerIdentity *target)
422 ret = plugin->sessions;
423 while ((ret != NULL) &&
424 ((GNUNET_SYSERR == ret->expecting_welcome) ||
425 (0 != memcmp (target,
426 &ret->target, sizeof (struct GNUNET_PeerIdentity)))))
433 * Find the session handle for the given peer.
435 static struct Session *
436 find_session_by_client (struct Plugin *plugin,
437 const struct GNUNET_SERVER_Client *client)
441 ret = plugin->sessions;
442 while ((ret != NULL) && (client != ret->client))
449 * Create a welcome message.
451 static struct PendingMessage *
452 create_welcome (struct Plugin *plugin)
454 struct PendingMessage *pm;
455 struct WelcomeMessage *welcome;
457 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
458 sizeof (struct WelcomeMessage));
459 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
460 welcome = (struct WelcomeMessage *) &pm[1];
461 welcome->header.size = htons (sizeof (struct WelcomeMessage));
462 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
463 welcome->clientIdentity = *plugin->env->my_identity;
464 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
465 pm->is_welcome = GNUNET_YES;
471 * Create a new session.
474 * @param target peer to connect to
475 * @param client client to use
476 * @return new session object
478 static struct Session *
479 create_session (struct Plugin *plugin,
480 const struct GNUNET_PeerIdentity *target,
481 struct GNUNET_SERVER_Client *client)
485 ret = GNUNET_malloc (sizeof (struct Session));
486 ret->plugin = plugin;
487 ret->next = plugin->sessions;
488 plugin->sessions = ret;
489 ret->client = client;
490 ret->target = *target;
491 ret->last_quota_update = GNUNET_TIME_absolute_get ();
492 ret->quota_in = plugin->env->default_quota_in;
493 ret->expecting_welcome = GNUNET_YES;
494 ret->pending_messages = create_welcome (plugin);
500 * If we have pending messages, ask the server to
501 * transmit them (schedule the respective tasks, etc.)
503 * @param session for which session should we do this
505 static void process_pending_messages (struct Session *session);
509 * Function called to notify a client about the socket
510 * being ready to queue more data. "buf" will be
511 * NULL and "size" zero if the socket was closed for
512 * writing in the meantime.
515 * @param size number of bytes available in buf
516 * @param buf where the callee should write the message
517 * @return number of bytes written to buf
520 do_transmit (void *cls, size_t size, void *buf)
522 struct Session *session = cls;
523 struct PendingMessage *pm;
527 struct DataMessage *dm;
529 session->transmit_handle = NULL;
533 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
535 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
536 GNUNET_i2s (&session->target));
539 while (NULL != (pm = session->pending_messages))
541 session->pending_messages = pm->next;
543 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
545 "Failed to transmit message of type %u to `%4s'.\n",
546 ntohs (pm->msg->type),
547 GNUNET_i2s (&session->target));
549 if (pm->transmit_cont != NULL)
550 pm->transmit_cont (pm->transmit_cont_cls,
551 &session->target, GNUNET_SYSERR);
558 while (NULL != (pm = session->pending_messages))
562 if (size < (msize = ntohs (pm->msg->size)))
564 memcpy (cbuf, pm->msg, msize);
572 sizeof (struct DataMessage) + (msize = ntohs (pm->msg->size)))
574 dm = (struct DataMessage *) cbuf;
575 dm->header.size = htons (sizeof (struct DataMessage) + msize);
576 dm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA);
577 dm->ack_out = GNUNET_htonll (++session->out_msg_counter);
578 dm->ack_in = GNUNET_htonll (session->max_in_msg_counter);
579 cbuf += sizeof (struct DataMessage);
580 ret += sizeof (struct DataMessage);
581 size -= sizeof (struct DataMessage);
582 memcpy (cbuf, pm->msg, msize);
587 session->pending_messages = pm->next;
588 if (pm->transmit_cont != NULL)
589 pm->transmit_cont (pm->transmit_cont_cls,
590 &session->target, GNUNET_OK);
592 session->gen_time[session->out_msg_counter % ACK_LOG_SIZE]
593 = GNUNET_TIME_absolute_get ();
595 process_pending_messages (session);
597 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
598 "tcp", "Transmitting %u bytes\n", ret);
605 * If we have pending messages, ask the server to
606 * transmit them (schedule the respective tasks, etc.)
608 * @param session for which session should we do this
611 process_pending_messages (struct Session *session)
613 GNUNET_assert (session->client != NULL);
614 if (session->pending_messages == NULL)
616 if (session->transmit_handle != NULL)
618 session->transmit_handle
619 = GNUNET_SERVER_notify_transmit_ready (session->client,
621 pending_messages->msg->
624 pending_messages->is_welcome ? 0 :
625 sizeof (struct DataMessage)),
626 GNUNET_TIME_absolute_get_remaining
628 pending_messages[0].timeout),
629 &do_transmit, session);
634 * Functions with this signature are called whenever we need
635 * to close a session due to a disconnect or failure to
636 * establish a connection.
638 * @param session session to close down
641 disconnect_session (struct Session *session)
643 struct Session *prev;
645 struct PendingMessage *pm;
648 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
650 "Disconnecting from `%4s' at %s (session %p).\n",
651 GNUNET_i2s (&session->target),
652 (session->connect_addr != NULL) ?
653 GNUNET_a2s (session->connect_addr,
654 session->connect_alen) : "*", session);
656 /* remove from session list */
658 pos = session->plugin->sessions;
659 while (pos != session)
665 session->plugin->sessions = session->next;
667 prev->next = session->next;
669 if (session->ic != NULL)
671 GNUNET_PEERINFO_iterate_cancel (session->ic);
674 if (session->transmit_handle != NULL)
676 GNUNET_CONNECTION_notify_transmit_ready_cancel
677 (session->transmit_handle);
678 session->transmit_handle = NULL;
680 while (NULL != (pm = session->pending_messages))
683 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
685 pm->transmit_cont != NULL
686 ? "Could not deliver message of type %u to `%4s'.\n"
688 "Could not deliver message of type %u to `%4s', notifying.\n",
689 ntohs (pm->msg->type), GNUNET_i2s (&session->target));
691 session->pending_messages = pm->next;
692 if (NULL != pm->transmit_cont)
693 pm->transmit_cont (pm->transmit_cont_cls,
694 &session->target, GNUNET_SYSERR);
697 if (GNUNET_NO == session->expecting_welcome)
700 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
702 "Notifying transport service about loss of data connection with `%4s'.\n",
703 GNUNET_i2s (&session->target));
705 /* Data session that actually went past the
706 initial handshake; transport service may
707 know about this one, so we need to
708 notify transport service about disconnect */
709 session->plugin->env->receive (session->plugin->env->cls,
711 NULL, 0, /* FIXME: address! */
712 &session->target, NULL);
714 if (session->client != NULL)
716 GNUNET_SERVER_client_drop (session->client);
717 session->client = NULL;
719 GNUNET_free_non_null (session->connect_addr);
720 GNUNET_free (session);
725 * Iterator callback to go over all addresses. If we get
726 * a TCP address, increment the counter
728 * @param cls closure, points to the counter
729 * @param tname name of the transport
730 * @param expiration expiration time
731 * @param addr the address
732 * @param addrlen length of the address
733 * @return GNUNET_OK to keep the address,
734 * GNUNET_NO to delete it from the HELLO
735 * GNUNET_SYSERR to stop iterating (but keep current address)
738 count_tcp_addresses (void *cls,
740 struct GNUNET_TIME_Absolute expiration,
741 const void *addr, size_t addrlen)
743 unsigned int *counter = cls;
745 if (0 != strcmp (tname, "tcp"))
746 return GNUNET_OK; /* not one of ours */
748 return GNUNET_OK; /* failed to connect */
752 struct ConnectContext
754 struct Plugin *plugin;
756 struct GNUNET_CONNECTION_Handle *sa;
758 struct PendingMessage *welcome;
765 * Iterator callback to go over all addresses. If we get
766 * the "pos" TCP address, try to connect to it.
769 * @param tname name of the transport
770 * @param expiration expiration time
771 * @param addrlen length of the address
772 * @param addr the address
773 * @return GNUNET_OK to keep the address,
774 * GNUNET_NO to delete it from the HELLO
775 * GNUNET_SYSERR to stop iterating (but keep current address)
778 try_connect_to_address (void *cls,
780 struct GNUNET_TIME_Absolute expiration,
781 const void *addr, size_t addrlen)
783 struct ConnectContext *cc = cls;
786 if (0 != strcmp (tname, "tcp"))
787 return GNUNET_OK; /* not one of ours */
788 if (sizeof (struct sockaddr_in) == addrlen)
790 else if (sizeof (struct sockaddr_in6) == addrlen)
794 /* not a valid address */
800 cc->welcome = create_welcome (cc->plugin);
802 GNUNET_CONNECTION_create_from_sockaddr (cc->plugin->env->sched,
804 GNUNET_SERVER_MAX_MESSAGE_SIZE);
806 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
808 "Connecting using address %s.\n",
809 GNUNET_a2s (addr, addrlen));
811 return GNUNET_SYSERR;
813 return GNUNET_OK; /* failed to connect */
818 * Type of an iterator over the hosts. Note that each
819 * host will be called with each available protocol.
822 * @param peer id of the peer, NULL for last call
823 * @param hello hello message for the peer (can be NULL)
824 * @param trust amount of trust we have in the peer
827 session_try_connect (void *cls,
828 const struct GNUNET_PeerIdentity *peer,
829 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
831 struct Session *session = cls;
833 struct ConnectContext cctx;
834 struct PendingMessage *pm;
839 /* last call, destroy session if we are still not
841 if (session->client != NULL)
844 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
846 "Now connected to `%4s', now processing messages.\n",
847 GNUNET_i2s (&session->target));
849 process_pending_messages (session);
854 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
856 "Failed to connect to `%4s' (no working `%s'), closing session.\n",
857 GNUNET_i2s (&session->target), "HELLO");
859 disconnect_session (session);
863 if ((hello == NULL) || (session->client != NULL))
865 GNUNET_break (0); /* should this ever happen!? */
869 GNUNET_HELLO_iterate_addresses (hello,
870 GNUNET_NO, &count_tcp_addresses, &count);
874 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
876 "Asked to connect to `%4s', but have no addresses to try.\n",
877 GNUNET_i2s (&session->target));
881 cctx.plugin = session->plugin;
884 cctx.pos = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
885 GNUNET_HELLO_iterate_addresses (hello,
886 GNUNET_NO, &try_connect_to_address, &cctx);
890 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
892 "Asked to connect, but all addresses failed.\n");
894 GNUNET_free_non_null (cctx.welcome);
897 session->client = GNUNET_SERVER_connect_socket (session->plugin->server,
900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901 "Connected to `%4s' for session %p\n",
902 GNUNET_i2s (&session->target), session->client);
904 if (session->client == NULL)
906 GNUNET_break (0); /* how could this happen? */
907 GNUNET_free_non_null (cctx.welcome);
912 pm->next = session->pending_messages;
913 session->pending_messages = pm;
915 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
917 "Connected to `%4s', now sending `%s' message.\n",
918 GNUNET_i2s (&session->target), "WELCOME");
924 * Function that can be used by the transport service to transmit
925 * a message using the plugin. Note that in the case of a
926 * peer disconnecting, the continuation MUST be called
927 * prior to the disconnect notification itself. This function
928 * will be called with this peer's HELLO message to initiate
929 * a fresh connection to another peer.
932 * @param target who should receive this message
933 * @param msg the message to transmit
934 * @param priority how important is the message (most plugins will
935 * ignore message priority and just FIFO)
936 * @param timeout how long to wait at most for the transmission (does not
937 * require plugins to discard the message after the timeout,
938 * just advisory for the desired delay; most plugins will ignore
940 * @param addr the address to use (can be NULL if the plugin
941 * is "on its own" (i.e. re-use existing TCP connection))
942 * @param addrlen length of the address in bytes
943 * @param force_address GNUNET_YES if the plugin MUST use the given address,
944 * otherwise the plugin may use other addresses or
945 * existing connections (if available)
946 * @param cont continuation to call once the message has
947 * been transmitted (or if the transport is ready
948 * for the next transmission call; or if the
949 * peer disconnected...); can be NULL
950 * @param cont_cls closure for cont
951 * @return number of bytes used (on the physical network, with overheads);
952 * -1 on hard errors (i.e. address invalid); 0 is a legal value
953 * and does NOT mean that the message was not transmitted (DV)
956 tcp_plugin_send (void *cls,
957 const struct GNUNET_PeerIdentity *target,
958 const struct GNUNET_MessageHeader *msg,
959 unsigned int priority,
960 struct GNUNET_TIME_Relative timeout,
964 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
966 struct Plugin *plugin = cls;
967 struct Session *session;
968 struct PendingMessage *pm;
969 struct PendingMessage *pme;
972 /* FIXME: support 'force_address' */
973 mlen = ntohs (msg->size);
974 session = find_session_by_target (plugin, target);
975 pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
976 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
977 memcpy (pm->msg, msg, ntohs (msg->size));
978 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
979 pm->transmit_cont = cont;
980 pm->transmit_cont_cls = cont_cls;
983 session = GNUNET_malloc (sizeof (struct Session));
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
987 "Asked to transmit, creating fresh session %p.\n",
990 session->next = plugin->sessions;
991 plugin->sessions = session;
992 session->plugin = plugin;
993 session->target = *target;
994 session->last_quota_update = GNUNET_TIME_absolute_get ();
995 session->quota_in = plugin->env->default_quota_in;
996 session->expecting_welcome = GNUNET_YES;
997 session->pending_messages = pm;
998 session->ic = GNUNET_PEERINFO_iterate (plugin->env->cfg,
1001 0, timeout, &session_try_connect,
1003 return mlen + sizeof (struct GNUNET_MessageHeader);
1005 GNUNET_assert (session != NULL);
1006 GNUNET_assert (session->client != NULL);
1007 /* append pm to pending_messages list */
1008 pme = session->pending_messages;
1011 session->pending_messages = pm;
1015 while (NULL != pme->next)
1020 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1021 "tcp", "Asked to transmit, added message to list.\n");
1023 process_pending_messages (session);
1024 return mlen + sizeof (struct GNUNET_MessageHeader);
1030 * Function that can be called to force a disconnect from the
1031 * specified neighbour. This should also cancel all previously
1032 * scheduled transmissions. Obviously the transmission may have been
1033 * partially completed already, which is OK. The plugin is supposed
1034 * to close the connection (if applicable) and no longer call the
1035 * transmit continuation(s).
1037 * Finally, plugin MUST NOT call the services's receive function to
1038 * notify the service that the connection to the specified target was
1039 * closed after a getting this call.
1041 * @param cls closure
1042 * @param target peer for which the last transmission is
1046 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1048 struct Plugin *plugin = cls;
1049 struct Session *session;
1050 struct PendingMessage *pm;
1052 session = find_session_by_target (plugin, target);
1053 if (session == NULL)
1059 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1061 "Asked to cancel session with `%4s'\n",
1062 GNUNET_i2s (target));
1064 pm = session->pending_messages;
1067 pm->transmit_cont = NULL;
1068 pm->transmit_cont_cls = NULL;
1071 if (session->client != NULL)
1073 GNUNET_SERVER_client_drop (session->client);
1074 session->client = NULL;
1076 /* rest of the clean-up of the session will be done as part of
1077 disconnect_notify which should be triggered any time now
1078 (or which may be triggering this call in the first place) */
1082 struct PrettyPrinterContext
1084 GNUNET_TRANSPORT_AddressStringCallback asc;
1091 * Append our port and forward the result.
1094 append_port (void *cls, const char *hostname)
1096 struct PrettyPrinterContext *ppc = cls;
1099 if (hostname == NULL)
1101 ppc->asc (ppc->asc_cls, NULL);
1105 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1106 ppc->asc (ppc->asc_cls, ret);
1112 * Convert the transports address to a nice, human-readable
1115 * @param cls closure
1116 * @param type name of the transport that generated the address
1117 * @param addr one of the addresses of the host, NULL for the last address
1118 * the specific address format depends on the transport
1119 * @param addrlen length of the address
1120 * @param numeric should (IP) addresses be displayed in numeric form?
1121 * @param timeout after how long should we give up?
1122 * @param asc function to call on each string
1123 * @param asc_cls closure for asc
1126 tcp_plugin_address_pretty_printer (void *cls,
1131 struct GNUNET_TIME_Relative timeout,
1132 GNUNET_TRANSPORT_AddressStringCallback asc,
1135 struct Plugin *plugin = cls;
1136 const struct sockaddr_in *v4;
1137 const struct sockaddr_in6 *v6;
1138 struct PrettyPrinterContext *ppc;
1140 if ((addrlen != sizeof (struct sockaddr_in)) &&
1141 (addrlen != sizeof (struct sockaddr_in6)))
1143 /* invalid address */
1144 GNUNET_break_op (0);
1145 asc (asc_cls, NULL);
1148 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1150 ppc->asc_cls = asc_cls;
1151 if (addrlen == sizeof (struct sockaddr_in))
1153 v4 = (const struct sockaddr_in *) addr;
1154 ppc->port = ntohs (v4->sin_port);
1158 v6 = (const struct sockaddr_in6 *) addr;
1159 ppc->port = ntohs (v6->sin6_port);
1162 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1166 !numeric, timeout, &append_port, ppc);
1171 * Update the last-received and bandwidth quota values
1174 * @param session session to update
1175 * @param force set to GNUNET_YES if we should update even
1176 * though the minimum refresh time has not yet expired
1179 update_quota (struct Session *session, int force)
1181 struct GNUNET_TIME_Absolute now;
1182 unsigned long long delta;
1183 unsigned long long total_allowed;
1184 unsigned long long total_remaining;
1186 now = GNUNET_TIME_absolute_get ();
1187 delta = now.value - session->last_quota_update.value;
1188 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1189 return; /* too early, not enough data */
1191 total_allowed = session->quota_in * delta;
1192 if (total_allowed > session->last_received)
1194 /* got less than acceptable */
1195 total_remaining = total_allowed - session->last_received;
1196 session->last_received = 0;
1197 delta = total_remaining / session->quota_in; /* bonus seconds */
1198 if (delta > MAX_BANDWIDTH_CARRY)
1199 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
1203 /* got more than acceptable */
1204 session->last_received -= total_allowed;
1207 session->last_quota_update.value = now.value - delta;
1212 * Set a quota for receiving data from the given peer; this is a
1213 * per-transport limit. The transport should limit its read/select
1214 * calls to stay below the quota (in terms of incoming data).
1216 * @param cls closure
1217 * @param target the peer for whom the quota is given
1218 * @param quota_in quota for receiving/sending data in bytes per ms
1221 tcp_plugin_set_receive_quota (void *cls,
1222 const struct GNUNET_PeerIdentity *target,
1225 struct Plugin *plugin = cls;
1226 struct Session *session;
1228 session = find_session_by_target (plugin, target);
1229 if (session == NULL)
1230 return; /* peer must have disconnected, ignore */
1231 if (session->quota_in != quota_in)
1233 update_quota (session, GNUNET_YES);
1234 if (session->quota_in > quota_in)
1235 session->last_quota_update = GNUNET_TIME_absolute_get ();
1236 session->quota_in = quota_in;
1242 * Check if the given port is plausible (must be either
1243 * our listen port or our advertised port). If it is
1244 * neither, we return one of these two ports at random.
1246 * @return either in_port or a more plausible port
1249 check_port (struct Plugin *plugin, uint16_t in_port)
1251 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1253 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1255 ? plugin->open_port : plugin->adv_port;
1260 * Another peer has suggested an address for this peer and transport
1261 * plugin. Check that this could be a valid address.
1263 * @param cls closure
1264 * @param addr pointer to the address
1265 * @param addrlen length of addr
1266 * @return GNUNET_OK if this is a plausible address for this peer
1270 tcp_plugin_check_address (void *cls, void *addr, size_t addrlen)
1272 struct Plugin *plugin = cls;
1273 char buf[sizeof (struct sockaddr_in6)];
1274 struct sockaddr_in *v4;
1275 struct sockaddr_in6 *v6;
1277 if ((addrlen != sizeof (struct sockaddr_in)) &&
1278 (addrlen != sizeof (struct sockaddr_in6)))
1280 GNUNET_break_op (0);
1281 return GNUNET_SYSERR;
1283 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1284 if (addrlen == sizeof (struct sockaddr_in))
1286 v4 = (struct sockaddr_in *) buf;
1287 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1291 v6 = (struct sockaddr_in6 *) buf;
1292 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1295 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1297 "Informing transport service about my address `%s'.\n",
1298 GNUNET_a2s (addr, addrlen));
1305 * We've received a welcome from this peer via TCP. Possibly create a
1306 * fresh client record and send back our welcome.
1308 * @param cls closure
1309 * @param client identification of the client
1310 * @param message the actual message
1313 handle_tcp_welcome (void *cls,
1314 struct GNUNET_SERVER_Client *client,
1315 const struct GNUNET_MessageHeader *message)
1317 struct Plugin *plugin = cls;
1318 const struct WelcomeMessage *wm = (const struct WelcomeMessage *) message;
1319 struct Session *session_c;
1324 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1326 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
1327 GNUNET_i2s (&wm->clientIdentity), client);
1329 session_c = find_session_by_client (plugin, client);
1330 if (session_c == NULL)
1333 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1334 /* FIXME: keep vaddr / alen! */
1335 GNUNET_SERVER_client_keep (client);
1336 session_c = create_session (plugin,
1337 &wm->clientIdentity, client);
1339 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1341 "Creating new session %p for incoming `%s' message.\n",
1342 session_c, "WELCOME");
1344 GNUNET_free_non_null (vaddr);
1345 process_pending_messages (session_c);
1347 if (session_c->expecting_welcome != GNUNET_YES)
1349 GNUNET_break_op (0);
1350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1353 session_c->expecting_welcome = GNUNET_NO;
1354 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1359 * Calculate how long we should delay reading from the TCP socket to
1360 * ensure that we stay within our bandwidth limits (push back).
1362 * @param session for which client should this be calculated
1364 static struct GNUNET_TIME_Relative
1365 calculate_throttle_delay (struct Session *session)
1367 struct GNUNET_TIME_Relative ret;
1368 struct GNUNET_TIME_Absolute now;
1373 now = GNUNET_TIME_absolute_get ();
1374 del = now.value - session->last_quota_update.value;
1375 if (del > MAX_BANDWIDTH_CARRY)
1377 update_quota (session, GNUNET_YES);
1378 del = now.value - session->last_quota_update.value;
1379 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1381 if (session->quota_in == 0)
1382 session->quota_in = 1; /* avoid divison by zero */
1383 avail = del * session->quota_in;
1384 if (avail > session->last_received)
1385 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1386 excess = session->last_received - avail;
1387 ret.value = excess / session->quota_in;
1393 * Task to signal the server that we can continue
1394 * receiving from the TCP client now.
1397 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1399 struct Session *session = cls;
1400 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1405 * We've received data for this peer via TCP. Unbox,
1406 * compute latency and forward.
1408 * @param cls closure
1409 * @param client identification of the client
1410 * @param message the actual message
1413 handle_tcp_data (void *cls,
1414 struct GNUNET_SERVER_Client *client,
1415 const struct GNUNET_MessageHeader *message)
1417 struct Plugin *plugin = cls;
1418 struct Session *session;
1419 const struct DataMessage *dm;
1421 const struct GNUNET_MessageHeader *msg;
1422 struct GNUNET_TIME_Relative latency;
1423 struct GNUNET_TIME_Absolute ttime;
1424 struct GNUNET_TIME_Absolute now;
1425 struct GNUNET_TIME_Relative delay;
1428 msize = ntohs (message->size);
1430 sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1432 GNUNET_break_op (0);
1433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1436 session = find_session_by_client (plugin, client);
1437 if ((NULL == session) || (GNUNET_NO != session->expecting_welcome))
1439 GNUNET_break_op (0);
1440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1444 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1445 "tcp", "Receiving %u bytes from `%4s'.\n",
1446 msize, GNUNET_i2s (&session->target));
1448 dm = (const struct DataMessage *) message;
1449 session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1450 GNUNET_ntohll (dm->ack_out));
1451 msg = (const struct GNUNET_MessageHeader *) &dm[1];
1452 if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1454 GNUNET_break_op (0);
1455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1458 /* estimate latency */
1459 ack_in = GNUNET_ntohll (dm->ack_in);
1460 if ((ack_in <= session->out_msg_counter) &&
1461 (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1463 delay = GNUNET_TIME_relative_ntoh (dm->delay);
1464 ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1465 now = GNUNET_TIME_absolute_get ();
1466 if (delay.value > now.value - ttime.value)
1467 delay.value = 0; /* not plausible */
1468 /* update (round-trip) latency using ageing; we
1469 use 7:1 so that we can reasonably quickly react
1470 to changes, but not so fast that latency is largely
1472 session->latency_estimate
1473 = ((7 * session->latency_estimate) +
1474 (now.value - ttime.value - delay.value)) / 8;
1476 latency.value = (uint64_t) session->latency_estimate;
1479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1481 "Forwarding data of type %u to transport service.\n",
1484 plugin->env->receive (plugin->env->cls, 1,
1485 NULL, 0, /* FIXME: sender IP! */
1486 &session->target, msg);
1487 /* update bandwidth used */
1488 session->last_received += msize;
1489 update_quota (session, GNUNET_NO);
1491 delay = calculate_throttle_delay (session);
1492 if (delay.value == 0)
1493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1495 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1496 delay, &delayed_done, session);
1501 * Handlers for the various TCP messages.
1503 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1504 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME,
1505 sizeof (struct WelcomeMessage)},
1506 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1512 create_tcp_handlers (struct Plugin *plugin)
1515 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1516 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1519 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1521 plugin->handlers[i].callback_cls = plugin;
1522 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1527 * Functions with this signature are called whenever a peer
1528 * is disconnected on the network level.
1530 * @param cls closure
1531 * @param client identification of the client
1534 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1536 struct Plugin *plugin = cls;
1537 struct Session *session;
1539 session = find_session_by_client (plugin, client);
1540 if (session == NULL)
1541 return; /* unknown, nothing to do */
1543 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1545 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1546 GNUNET_i2s (&session->target),
1547 (session->connect_addr != NULL) ?
1548 GNUNET_a2s (session->connect_addr,
1549 session->connect_alen) : "*", client);
1551 disconnect_session (session);
1556 * Add the IP of our network interface to the list of
1557 * our external IP addresses.
1560 process_interfaces (void *cls,
1563 const struct sockaddr *addr, socklen_t addrlen)
1565 struct Plugin *plugin = cls;
1567 struct sockaddr_in *v4;
1568 struct sockaddr_in6 *v6;
1570 af = addr->sa_family;
1573 v4 = (struct sockaddr_in *) addr;
1574 v4->sin_port = htons (plugin->adv_port);
1578 GNUNET_assert (af == AF_INET6);
1579 v6 = (struct sockaddr_in6 *) addr;
1580 v6->sin6_port = htons (plugin->adv_port);
1582 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1583 GNUNET_ERROR_TYPE_BULK,
1584 "tcp", _("Found address `%s' (%s)\n"),
1585 GNUNET_a2s (addr, addrlen), name);
1586 plugin->env->notify_address (plugin->env->cls,
1588 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1594 * Function called by the resolver for each address obtained from DNS
1595 * for our own hostname. Add the addresses to the list of our
1596 * external IP addresses.
1598 * @param cls closure
1599 * @param addr one of the addresses of the host, NULL for the last address
1600 * @param addrlen length of the address
1603 process_hostname_ips (void *cls,
1604 const struct sockaddr *addr, socklen_t addrlen)
1606 struct Plugin *plugin = cls;
1610 plugin->hostname_dns = NULL;
1613 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1618 * Entry point for the plugin.
1621 libgnunet_plugin_transport_tcp_init (void *cls)
1623 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1624 struct GNUNET_TRANSPORT_PluginFunctions *api;
1625 struct Plugin *plugin;
1626 struct GNUNET_SERVICE_Context *service;
1627 unsigned long long aport;
1628 unsigned long long bport;
1630 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1631 if (service == NULL)
1633 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1636 ("Failed to start service for `%s' transport plugin.\n"),
1642 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1648 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1651 &aport)) && (aport > 65535)))
1653 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
1656 ("Require valid port number for service `%s' in configuration!\n"),
1658 GNUNET_SERVICE_stop (service);
1663 plugin = GNUNET_malloc (sizeof (struct Plugin));
1664 plugin->open_port = bport;
1665 plugin->adv_port = aport;
1667 plugin->lsock = NULL;
1668 plugin->statistics = NULL;
1669 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
1671 api->send = &tcp_plugin_send;
1672 api->disconnect = &tcp_plugin_disconnect;
1673 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
1674 api->set_receive_quota = &tcp_plugin_set_receive_quota;
1675 api->check_address = &tcp_plugin_check_address;
1676 plugin->service = service;
1677 plugin->server = GNUNET_SERVICE_get_server (service);
1678 create_tcp_handlers (plugin);
1679 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1680 "tcp", _("TCP transport listening on port %llu\n"), bport);
1682 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1685 ("TCP transport advertises itself as being on port %llu\n"),
1687 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
1689 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
1690 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
1693 HOSTNAME_RESOLVE_TIMEOUT,
1694 &process_hostname_ips,
1701 * Exit point from the plugin.
1704 libgnunet_plugin_transport_tcp_done (void *cls)
1706 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
1707 struct Plugin *plugin = api->cls;
1708 struct Session *session;
1710 while (NULL != (session = plugin->sessions))
1711 disconnect_session (session);
1712 if (NULL != plugin->hostname_dns)
1714 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
1715 plugin->hostname_dns = NULL;
1717 GNUNET_SERVICE_stop (plugin->service);
1718 GNUNET_free (plugin->handlers);
1719 GNUNET_free (plugin);
1724 /* end of plugin_transport_tcp.c */