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 (size_t addrlen, const void *addr, struct Plugin *plugin)
454 struct PendingMessage *pm;
455 struct WelcomeMessage *welcome;
457 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
458 sizeof (struct WelcomeMessage) + addrlen);
459 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
460 welcome = (struct WelcomeMessage *) &pm[1];
461 welcome->header.size = htons (sizeof (struct WelcomeMessage) + addrlen);
462 welcome->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME);
463 GNUNET_CRYPTO_hash (plugin->env->my_public_key,
464 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
465 &welcome->clientIdentity.hashPubKey);
466 memcpy (&welcome[1], addr, addrlen);
467 pm->timeout = GNUNET_TIME_relative_to_absolute (WELCOME_TIMEOUT);
468 pm->is_welcome = GNUNET_YES;
474 * Create a new session using the specified address
475 * for the welcome message.
478 * @param target peer to connect to
479 * @param client client to use
480 * @param addrlen IPv4 or IPv6
481 * @param addr either struct sockaddr_in or struct sockaddr_in6
482 * @return NULL connection failed / invalid address
484 static struct Session *
485 create_session (struct Plugin *plugin,
486 const struct GNUNET_PeerIdentity *target,
487 struct GNUNET_SERVER_Client *client,
488 const void *addr, size_t addrlen)
492 ret = GNUNET_malloc (sizeof (struct Session));
493 ret->plugin = plugin;
494 ret->next = plugin->sessions;
495 plugin->sessions = ret;
496 ret->client = client;
497 ret->target = *target;
498 ret->last_quota_update = GNUNET_TIME_absolute_get ();
499 ret->quota_in = plugin->env->default_quota_in;
500 ret->expecting_welcome = GNUNET_YES;
501 ret->pending_messages = create_welcome (addrlen, addr, plugin);
507 * If we have pending messages, ask the server to
508 * transmit them (schedule the respective tasks, etc.)
510 * @param session for which session should we do this
512 static void process_pending_messages (struct Session *session);
516 * Function called to notify a client about the socket
517 * being ready to queue more data. "buf" will be
518 * NULL and "size" zero if the socket was closed for
519 * writing in the meantime.
522 * @param size number of bytes available in buf
523 * @param buf where the callee should write the message
524 * @return number of bytes written to buf
527 do_transmit (void *cls, size_t size, void *buf)
529 struct Session *session = cls;
530 struct PendingMessage *pm;
534 struct DataMessage *dm;
536 session->transmit_handle = NULL;
540 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
542 "Timeout trying to transmit to peer `%4s', discarding message queue.\n",
543 GNUNET_i2s (&session->target));
546 while (NULL != (pm = session->pending_messages))
548 session->pending_messages = pm->next;
550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
552 "Failed to transmit message of type %u to `%4s'.\n",
553 ntohs (pm->msg->type),
554 GNUNET_i2s (&session->target));
556 if (pm->transmit_cont != NULL)
557 pm->transmit_cont (pm->transmit_cont_cls,
558 &session->target, GNUNET_SYSERR);
565 while (NULL != (pm = session->pending_messages))
569 if (size < (msize = ntohs (pm->msg->size)))
571 memcpy (cbuf, pm->msg, msize);
579 sizeof (struct DataMessage) + (msize = ntohs (pm->msg->size)))
581 dm = (struct DataMessage *) cbuf;
582 dm->header.size = htons (sizeof (struct DataMessage) + msize);
583 dm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA);
584 dm->ack_out = GNUNET_htonll (++session->out_msg_counter);
585 dm->ack_in = GNUNET_htonll (session->max_in_msg_counter);
586 cbuf += sizeof (struct DataMessage);
587 ret += sizeof (struct DataMessage);
588 size -= sizeof (struct DataMessage);
589 memcpy (cbuf, pm->msg, msize);
594 session->pending_messages = pm->next;
595 if (pm->transmit_cont != NULL)
596 pm->transmit_cont (pm->transmit_cont_cls,
597 &session->target, GNUNET_OK);
599 session->gen_time[session->out_msg_counter % ACK_LOG_SIZE]
600 = GNUNET_TIME_absolute_get ();
602 process_pending_messages (session);
604 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
605 "tcp", "Transmitting %u bytes\n", ret);
612 * If we have pending messages, ask the server to
613 * transmit them (schedule the respective tasks, etc.)
615 * @param session for which session should we do this
618 process_pending_messages (struct Session *session)
620 GNUNET_assert (session->client != NULL);
621 if (session->pending_messages == NULL)
623 if (session->transmit_handle != NULL)
625 session->transmit_handle
626 = GNUNET_SERVER_notify_transmit_ready (session->client,
628 pending_messages->msg->
631 pending_messages->is_welcome ? 0 :
632 sizeof (struct DataMessage)),
633 GNUNET_TIME_absolute_get_remaining
635 pending_messages[0].timeout),
636 &do_transmit, session);
642 * Create a new session connecting to the specified
643 * target at the specified address. The session will
644 * be used to verify an address in a HELLO and should
645 * not expect to receive a WELCOME.
648 * @param target peer to connect to
649 * @param addrlen IPv4 or IPv6
650 * @param addr either struct sockaddr_in or struct sockaddr_in6
651 * @return NULL connection failed / invalid address
653 static struct Session *
654 connect_and_create_validation_session (struct Plugin *plugin,
655 const struct GNUNET_PeerIdentity
656 *target, const void *addr,
659 struct GNUNET_SERVER_Client *client;
660 struct GNUNET_CONNECTION_Handle *conn;
661 struct Session *session;
664 if (addrlen == sizeof (struct sockaddr_in))
666 else if (addrlen == sizeof (struct sockaddr_in6))
671 return NULL; /* invalid address */
673 conn = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
677 GNUNET_SERVER_MAX_MESSAGE_SIZE);
681 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
683 "Failed to create connection to peer at `%s'.\n",
684 GNUNET_a2s (addr, addrlen));
688 client = GNUNET_SERVER_connect_socket (plugin->server, conn);
689 GNUNET_assert (client != NULL);
690 session = create_session (plugin, target, client, addr, addrlen);
692 GNUNET_free (session->pending_messages);
693 session->pending_messages = NULL;
694 session->connect_alen = addrlen;
695 session->connect_addr = GNUNET_malloc (addrlen);
696 session->expecting_welcome = GNUNET_SYSERR;
697 memcpy (session->connect_addr, addr, addrlen);
699 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
701 "Creating new session %p with `%s' for `%4s' based on `%s' request.\n",
703 GNUNET_a2s (addr, addrlen),
704 GNUNET_i2s (&session->target), "VALIDATE");
711 * Function that can be used by the transport service to validate that
712 * another peer is reachable at a particular address (even if we
713 * already have a connection to this peer, this function is required
714 * to establish a new one).
717 * @param target who should receive this message
718 * @param challenge challenge code to use
719 * @param addrlen length of the address
720 * @param addr the address
721 * @param timeout how long should we try to transmit these?
722 * @return GNUNET_OK if the transmission has been scheduled
725 tcp_plugin_validate (void *cls,
726 const struct GNUNET_PeerIdentity *target,
728 struct GNUNET_TIME_Relative timeout,
729 const void *addr, size_t addrlen)
731 struct Plugin *plugin = cls;
732 struct Session *session;
733 struct PendingMessage *pm;
734 struct ValidationChallengeMessage *vcm;
737 connect_and_create_validation_session (plugin, target, addr, addrlen);
741 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
742 "tcp", "Failed to create fresh session.\n");
744 return GNUNET_SYSERR;
746 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
747 sizeof (struct ValidationChallengeMessage) + addrlen);
748 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
749 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
750 pm->is_welcome = GNUNET_YES;
751 vcm = (struct ValidationChallengeMessage *) &pm[1];
753 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
754 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING);
755 vcm->challenge = htonl (challenge);
756 vcm->target = *target;
757 memcpy (&vcm[1], addr, addrlen);
758 GNUNET_assert (session->pending_messages == NULL);
759 session->pending_messages = pm;
760 process_pending_messages (session);
766 * Functions with this signature are called whenever we need
767 * to close a session due to a disconnect or failure to
768 * establish a connection.
770 * @param session session to close down
773 disconnect_session (struct Session *session)
775 struct Session *prev;
777 struct PendingMessage *pm;
780 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
782 "Disconnecting from `%4s' at %s (session %p).\n",
783 GNUNET_i2s (&session->target),
784 (session->connect_addr != NULL) ?
785 GNUNET_a2s (session->connect_addr,
786 session->connect_alen) : "*", session);
788 /* remove from session list */
790 pos = session->plugin->sessions;
791 while (pos != session)
797 session->plugin->sessions = session->next;
799 prev->next = session->next;
801 if (session->ic != NULL)
803 GNUNET_PEERINFO_iterate_cancel (session->ic);
806 if (session->transmit_handle != NULL)
808 GNUNET_CONNECTION_notify_transmit_ready_cancel
809 (session->transmit_handle);
810 session->transmit_handle = NULL;
812 while (NULL != (pm = session->pending_messages))
815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
817 pm->transmit_cont != NULL
818 ? "Could not deliver message of type %u to `%4s'.\n"
820 "Could not deliver message of type %u to `%4s', notifying.\n",
821 ntohs (pm->msg->type), GNUNET_i2s (&session->target));
823 session->pending_messages = pm->next;
824 if (NULL != pm->transmit_cont)
825 pm->transmit_cont (pm->transmit_cont_cls,
826 &session->target, GNUNET_SYSERR);
829 if (GNUNET_NO == session->expecting_welcome)
832 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
834 "Notifying transport service about loss of data connection with `%4s'.\n",
835 GNUNET_i2s (&session->target));
837 /* Data session that actually went past the
838 initial handshake; transport service may
839 know about this one, so we need to
840 notify transport service about disconnect */
841 session->plugin->env->receive (session->plugin->env->cls,
842 GNUNET_TIME_UNIT_ZERO,
843 &session->target, NULL);
845 if (session->client != NULL)
847 GNUNET_SERVER_client_drop (session->client);
848 session->client = NULL;
850 GNUNET_free_non_null (session->connect_addr);
851 GNUNET_free (session);
856 * Iterator callback to go over all addresses. If we get
857 * a TCP address, increment the counter
859 * @param cls closure, points to the counter
860 * @param tname name of the transport
861 * @param expiration expiration time
862 * @param addr the address
863 * @param addrlen length of the address
864 * @return GNUNET_OK to keep the address,
865 * GNUNET_NO to delete it from the HELLO
866 * GNUNET_SYSERR to stop iterating (but keep current address)
869 count_tcp_addresses (void *cls,
871 struct GNUNET_TIME_Absolute expiration,
872 const void *addr, size_t addrlen)
874 unsigned int *counter = cls;
876 if (0 != strcmp (tname, "tcp"))
877 return GNUNET_OK; /* not one of ours */
879 return GNUNET_OK; /* failed to connect */
883 struct ConnectContext
885 struct Plugin *plugin;
887 struct GNUNET_CONNECTION_Handle *sa;
889 struct PendingMessage *welcome;
896 * Iterator callback to go over all addresses. If we get
897 * the "pos" TCP address, try to connect to it.
900 * @param tname name of the transport
901 * @param expiration expiration time
902 * @param addrlen length of the address
903 * @param addr the address
904 * @return GNUNET_OK to keep the address,
905 * GNUNET_NO to delete it from the HELLO
906 * GNUNET_SYSERR to stop iterating (but keep current address)
909 try_connect_to_address (void *cls,
911 struct GNUNET_TIME_Absolute expiration,
912 const void *addr, size_t addrlen)
914 struct ConnectContext *cc = cls;
917 if (0 != strcmp (tname, "tcp"))
918 return GNUNET_OK; /* not one of ours */
919 if (sizeof (struct sockaddr_in) == addrlen)
921 else if (sizeof (struct sockaddr_in6) == addrlen)
925 /* not a valid address */
931 cc->welcome = create_welcome (addrlen, addr, cc->plugin);
933 GNUNET_CONNECTION_create_from_sockaddr (cc->plugin->env->sched,
935 GNUNET_SERVER_MAX_MESSAGE_SIZE);
937 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
939 "Connecting using address %s.\n",
940 GNUNET_a2s (addr, addrlen));
942 return GNUNET_SYSERR;
944 return GNUNET_OK; /* failed to connect */
949 * Type of an iterator over the hosts. Note that each
950 * host will be called with each available protocol.
953 * @param peer id of the peer, NULL for last call
954 * @param hello hello message for the peer (can be NULL)
955 * @param trust amount of trust we have in the peer
958 session_try_connect (void *cls,
959 const struct GNUNET_PeerIdentity *peer,
960 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
962 struct Session *session = cls;
964 struct ConnectContext cctx;
965 struct PendingMessage *pm;
970 /* last call, destroy session if we are still not
972 if (session->client != NULL)
975 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
977 "Now connected to `%4s', now processing messages.\n",
978 GNUNET_i2s (&session->target));
980 process_pending_messages (session);
985 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
987 "Failed to connect to `%4s' (no working `%s'), closing session.\n",
988 GNUNET_i2s (&session->target), "HELLO");
990 disconnect_session (session);
994 if ((hello == NULL) || (session->client != NULL))
996 GNUNET_break (0); /* should this ever happen!? */
1000 GNUNET_HELLO_iterate_addresses (hello,
1001 GNUNET_NO, &count_tcp_addresses, &count);
1005 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1007 "Asked to connect to `%4s', but have no addresses to try.\n",
1008 GNUNET_i2s (&session->target));
1012 cctx.plugin = session->plugin;
1014 cctx.welcome = NULL;
1015 cctx.pos = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, count);
1016 GNUNET_HELLO_iterate_addresses (hello,
1017 GNUNET_NO, &try_connect_to_address, &cctx);
1018 if (cctx.sa == NULL)
1021 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1023 "Asked to connect, but all addresses failed.\n");
1025 GNUNET_free_non_null (cctx.welcome);
1028 session->client = GNUNET_SERVER_connect_socket (session->plugin->server,
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 "Connected to `%4s' for session %p\n",
1033 GNUNET_i2s (&session->target), session->client);
1035 if (session->client == NULL)
1037 GNUNET_break (0); /* how could this happen? */
1038 GNUNET_free_non_null (cctx.welcome);
1043 pm->next = session->pending_messages;
1044 session->pending_messages = pm;
1046 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1048 "Connected to `%4s', now sending `%s' message.\n",
1049 GNUNET_i2s (&session->target), "WELCOME");
1055 * Function that can be used by the transport service to transmit
1056 * a message using the plugin.
1058 * @param cls closure
1059 * @param service_context value passed to the transport-service
1060 * to identify the neighbour
1061 * @param target who should receive this message
1062 * @param priority how important is the message
1063 * @param msg the message to transmit
1064 * @param timeout when should we time out (give up) if we can not transmit?
1065 * @param cont continuation to call once the message has
1066 * been transmitted (or if the transport is ready
1067 * for the next transmission call; or if the
1068 * peer disconnected...)
1069 * @param cont_cls closure for cont
1072 tcp_plugin_send (void *cls,
1073 const struct GNUNET_PeerIdentity *target,
1074 unsigned int priority,
1075 const struct GNUNET_MessageHeader *msg,
1076 struct GNUNET_TIME_Relative timeout,
1077 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
1079 struct Plugin *plugin = cls;
1080 struct Session *session;
1081 struct PendingMessage *pm;
1082 struct PendingMessage *pme;
1084 session = find_session_by_target (plugin, target);
1085 pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
1086 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
1087 memcpy (pm->msg, msg, ntohs (msg->size));
1088 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1089 pm->transmit_cont = cont;
1090 pm->transmit_cont_cls = cont_cls;
1091 if (session == NULL)
1093 session = GNUNET_malloc (sizeof (struct Session));
1095 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1097 "Asked to transmit, creating fresh session %p.\n",
1100 session->next = plugin->sessions;
1101 plugin->sessions = session;
1102 session->plugin = plugin;
1103 session->target = *target;
1104 session->last_quota_update = GNUNET_TIME_absolute_get ();
1105 session->quota_in = plugin->env->default_quota_in;
1106 session->expecting_welcome = GNUNET_YES;
1107 session->pending_messages = pm;
1108 session->ic = GNUNET_PEERINFO_iterate (plugin->env->cfg,
1111 0, timeout, &session_try_connect,
1115 GNUNET_assert (session != NULL);
1116 GNUNET_assert (session->client != NULL);
1117 /* append pm to pending_messages list */
1118 pme = session->pending_messages;
1121 session->pending_messages = pm;
1125 while (NULL != pme->next)
1130 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1131 "tcp", "Asked to transmit, added message to list.\n");
1133 process_pending_messages (session);
1139 * Function that can be called to force a disconnect from the
1140 * specified neighbour. This should also cancel all previously
1141 * scheduled transmissions. Obviously the transmission may have been
1142 * partially completed already, which is OK. The plugin is supposed
1143 * to close the connection (if applicable) and no longer call the
1144 * transmit continuation(s).
1146 * Finally, plugin MUST NOT call the services's receive function to
1147 * notify the service that the connection to the specified target was
1148 * closed after a getting this call.
1150 * @param cls closure
1151 * @param target peer for which the last transmission is
1155 tcp_plugin_disconnect (void *cls, const struct GNUNET_PeerIdentity *target)
1157 struct Plugin *plugin = cls;
1158 struct Session *session;
1159 struct PendingMessage *pm;
1161 session = find_session_by_target (plugin, target);
1162 if (session == NULL)
1168 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1170 "Asked to cancel session with `%4s'\n",
1171 GNUNET_i2s (target));
1173 pm = session->pending_messages;
1176 pm->transmit_cont = NULL;
1177 pm->transmit_cont_cls = NULL;
1180 if (session->client != NULL)
1182 GNUNET_SERVER_client_drop (session->client);
1183 session->client = NULL;
1185 /* rest of the clean-up of the session will be done as part of
1186 disconnect_notify which should be triggered any time now
1187 (or which may be triggering this call in the first place) */
1191 struct PrettyPrinterContext
1193 GNUNET_TRANSPORT_AddressStringCallback asc;
1200 * Append our port and forward the result.
1203 append_port (void *cls, const char *hostname)
1205 struct PrettyPrinterContext *ppc = cls;
1208 if (hostname == NULL)
1210 ppc->asc (ppc->asc_cls, NULL);
1214 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1215 ppc->asc (ppc->asc_cls, ret);
1221 * Convert the transports address to a nice, human-readable
1224 * @param cls closure
1225 * @param type name of the transport that generated the address
1226 * @param addr one of the addresses of the host, NULL for the last address
1227 * the specific address format depends on the transport
1228 * @param addrlen length of the address
1229 * @param numeric should (IP) addresses be displayed in numeric form?
1230 * @param timeout after how long should we give up?
1231 * @param asc function to call on each string
1232 * @param asc_cls closure for asc
1235 tcp_plugin_address_pretty_printer (void *cls,
1240 struct GNUNET_TIME_Relative timeout,
1241 GNUNET_TRANSPORT_AddressStringCallback asc,
1244 struct Plugin *plugin = cls;
1245 const struct sockaddr_in *v4;
1246 const struct sockaddr_in6 *v6;
1247 struct PrettyPrinterContext *ppc;
1249 if ((addrlen != sizeof (struct sockaddr_in)) &&
1250 (addrlen != sizeof (struct sockaddr_in6)))
1252 /* invalid address */
1253 GNUNET_break_op (0);
1254 asc (asc_cls, NULL);
1257 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1259 ppc->asc_cls = asc_cls;
1260 if (addrlen == sizeof (struct sockaddr_in))
1262 v4 = (const struct sockaddr_in *) addr;
1263 ppc->port = ntohs (v4->sin_port);
1267 v6 = (const struct sockaddr_in6 *) addr;
1268 ppc->port = ntohs (v6->sin6_port);
1271 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1275 !numeric, timeout, &append_port, ppc);
1280 * Update the last-received and bandwidth quota values
1283 * @param session session to update
1284 * @param force set to GNUNET_YES if we should update even
1285 * though the minimum refresh time has not yet expired
1288 update_quota (struct Session *session, int force)
1290 struct GNUNET_TIME_Absolute now;
1291 unsigned long long delta;
1292 unsigned long long total_allowed;
1293 unsigned long long total_remaining;
1295 now = GNUNET_TIME_absolute_get ();
1296 delta = now.value - session->last_quota_update.value;
1297 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1298 return; /* too early, not enough data */
1300 total_allowed = session->quota_in * delta;
1301 if (total_allowed > session->last_received)
1303 /* got less than acceptable */
1304 total_remaining = total_allowed - session->last_received;
1305 session->last_received = 0;
1306 delta = total_remaining / session->quota_in; /* bonus seconds */
1307 if (delta > MAX_BANDWIDTH_CARRY)
1308 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
1312 /* got more than acceptable */
1313 session->last_received -= total_allowed;
1316 session->last_quota_update.value = now.value - delta;
1321 * Set a quota for receiving data from the given peer; this is a
1322 * per-transport limit. The transport should limit its read/select
1323 * calls to stay below the quota (in terms of incoming data).
1325 * @param cls closure
1326 * @param target the peer for whom the quota is given
1327 * @param quota_in quota for receiving/sending data in bytes per ms
1330 tcp_plugin_set_receive_quota (void *cls,
1331 const struct GNUNET_PeerIdentity *target,
1334 struct Plugin *plugin = cls;
1335 struct Session *session;
1337 session = find_session_by_target (plugin, target);
1338 if (session == NULL)
1339 return; /* peer must have disconnected, ignore */
1340 if (session->quota_in != quota_in)
1342 update_quota (session, GNUNET_YES);
1343 if (session->quota_in > quota_in)
1344 session->last_quota_update = GNUNET_TIME_absolute_get ();
1345 session->quota_in = quota_in;
1351 * Check if the given port is plausible (must be either
1352 * our listen port or our advertised port). If it is
1353 * neither, we return one of these two ports at random.
1355 * @return either in_port or a more plausible port
1358 check_port (struct Plugin *plugin, uint16_t in_port)
1360 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1362 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1364 ? plugin->open_port : plugin->adv_port;
1369 * Another peer has suggested an address for this
1370 * peer and transport plugin. Check that this could be a valid
1371 * address. If so, consider adding it to the list
1374 * @param cls closure
1375 * @param addr pointer to the address
1376 * @param addrlen length of addr
1377 * @return GNUNET_OK if this is a plausible address for this peer
1381 tcp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1383 struct Plugin *plugin = cls;
1384 char buf[sizeof (struct sockaddr_in6)];
1385 struct sockaddr_in *v4;
1386 struct sockaddr_in6 *v6;
1388 if ((addrlen != sizeof (struct sockaddr_in)) &&
1389 (addrlen != sizeof (struct sockaddr_in6)))
1391 GNUNET_break_op (0);
1392 return GNUNET_SYSERR;
1394 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1395 if (addrlen == sizeof (struct sockaddr_in))
1397 v4 = (struct sockaddr_in *) buf;
1398 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1402 v6 = (struct sockaddr_in6 *) buf;
1403 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1408 "Informing transport service about my address `%s'.\n",
1409 GNUNET_a2s (addr, addrlen));
1411 plugin->env->notify_address (plugin->env->cls,
1413 buf, addrlen, LEARNED_ADDRESS_EXPIRATION);
1419 * Send a validation challenge response.
1422 send_vcr (void *cls, size_t size, void *buf)
1424 struct ValidationChallengeResponse *vcr = cls;
1432 msize = ntohs (vcr->header.size);
1433 GNUNET_assert (size >= msize);
1434 memcpy (buf, vcr, msize);
1441 * We've received a PING from this peer via TCP.
1442 * Send back our PONG.
1444 * @param cls closure
1445 * @param client identification of the client
1446 * @param message the actual message
1449 handle_tcp_ping (void *cls,
1450 struct GNUNET_SERVER_Client *client,
1451 const struct GNUNET_MessageHeader *message)
1453 struct Plugin *plugin = cls;
1454 const struct ValidationChallengeMessage *vcm;
1455 struct ValidationChallengeResponse *vcr;
1462 GNUNET_SERVER_client_get_address (client, (void **) &addr, &addrlen))
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1465 "Processing `%s' from `%s'\n",
1466 "PING", GNUNET_a2s (addr, addrlen));
1470 msize = ntohs (message->size);
1471 if (msize < sizeof (struct ValidationChallengeMessage))
1473 GNUNET_break_op (0);
1474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1477 vcm = (const struct ValidationChallengeMessage *) message;
1478 if (0 != memcmp (&vcm->target,
1479 plugin->env->my_identity,
1480 sizeof (struct GNUNET_PeerIdentity)))
1482 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1483 _("Received `%s' message not destined for me!\n"), "PING");
1484 /* TODO: call statistics */
1485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1488 msize -= sizeof (struct ValidationChallengeMessage);
1489 if (GNUNET_OK != tcp_plugin_address_suggested (plugin, &vcm[1], msize))
1491 GNUNET_break_op (0);
1492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1495 if (GNUNET_OK != GNUNET_SERVER_client_get_address (client, &addr, &addrlen))
1498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1501 vcr = GNUNET_malloc (sizeof (struct ValidationChallengeResponse) + addrlen);
1503 htons (sizeof (struct ValidationChallengeResponse) + addrlen);
1504 vcr->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG);
1506 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1508 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + addrlen);
1509 vcr->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
1510 vcr->challenge = vcm->challenge;
1511 vcr->signer = *plugin->env->my_public_key;
1512 memcpy (&vcr[1], addr, addrlen);
1513 GNUNET_assert (GNUNET_OK ==
1514 GNUNET_CRYPTO_rsa_sign (plugin->env->my_private_key,
1515 &vcr->purpose, &vcr->signature));
1517 GNUNET_assert (GNUNET_OK ==
1518 GNUNET_CRYPTO_rsa_verify
1519 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1521 &vcr->signature, plugin->env->my_public_key));
1525 GNUNET_SERVER_notify_transmit_ready (client,
1527 ValidationChallengeResponse)
1529 GNUNET_TIME_UNIT_SECONDS,
1535 /* after a PING, we always close the connection */
1536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1541 * Handle PONG-message.
1543 * @param cls handle for this plugin
1544 * @param client from where did we receive the PONG
1545 * @param message the actual message
1548 handle_tcp_pong (void *cls,
1549 struct GNUNET_SERVER_Client *client,
1550 const struct GNUNET_MessageHeader *message)
1552 struct Plugin *plugin = cls;
1553 const struct ValidationChallengeResponse *vcr;
1554 struct GNUNET_PeerIdentity peer;
1557 const struct sockaddr *addr;
1558 struct sockaddr_in v4;
1559 struct sockaddr_in6 v6;
1562 struct sockaddr *claddr;
1565 GNUNET_SERVER_client_get_address (client, (void **) &claddr, &addrlen))
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1568 "Processing `%s' from `%s'\n",
1569 "PONG", GNUNET_a2s (claddr, addrlen));
1570 GNUNET_free (claddr);
1573 if (ntohs (message->size) < sizeof (struct ValidationChallengeResponse))
1575 GNUNET_break_op (0);
1576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1580 ntohs (message->size) - sizeof (struct ValidationChallengeResponse);
1581 vcr = (const struct ValidationChallengeResponse *) message;
1582 if ((ntohl (vcr->purpose.size) !=
1583 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1585 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) + addrlen))
1587 GNUNET_break_op (0);
1588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1592 GNUNET_CRYPTO_rsa_verify
1593 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1594 &vcr->purpose, &vcr->signature, &vcr->signer))
1596 GNUNET_break_op (0);
1597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1600 GNUNET_CRYPTO_hash (&vcr->signer,
1601 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1603 addr = (const struct sockaddr *) &vcr[1];
1604 if (addrlen == sizeof (struct sockaddr_in))
1606 memcpy (&v4, addr, sizeof (struct sockaddr_in));
1607 v4.sin_port = htons (check_port (plugin, ntohs (v4.sin_port)));
1608 sender_addr = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) &v4,
1611 else if (addrlen == sizeof (struct sockaddr_in6))
1613 memcpy (&v6, addr, sizeof (struct sockaddr_in6));
1614 v6.sin6_port = htons (check_port (plugin, ntohs (v6.sin6_port)));
1615 sender_addr = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) &v6,
1620 GNUNET_break_op (0);
1621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1624 plugin->env->notify_validation (plugin->env->cls,
1626 &peer, ntohl (vcr->challenge), sender_addr);
1627 GNUNET_free (sender_addr);
1628 /* after a PONG, we always close the connection */
1629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1634 * We've received a welcome from this peer via TCP.
1635 * Possibly create a fresh client record and send back
1638 * @param cls closure
1639 * @param client identification of the client
1640 * @param message the actual message
1643 handle_tcp_welcome (void *cls,
1644 struct GNUNET_SERVER_Client *client,
1645 const struct GNUNET_MessageHeader *message)
1647 struct Plugin *plugin = cls;
1648 struct Session *session_c;
1649 const struct WelcomeMessage *wm;
1654 const struct sockaddr *addr;
1656 msize = ntohs (message->size);
1657 if (msize < sizeof (struct WelcomeMessage))
1659 GNUNET_break_op (0);
1660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1663 wm = (const struct WelcomeMessage *) message;
1665 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1667 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
1668 GNUNET_i2s (&wm->clientIdentity), client);
1670 session_c = find_session_by_client (plugin, client);
1671 if (session_c == NULL)
1674 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1675 GNUNET_SERVER_client_keep (client);
1676 session_c = create_session (plugin,
1677 &wm->clientIdentity, client, vaddr, alen);
1679 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1681 "Creating new session %p for incoming `%s' message.\n",
1682 session_c, "WELCOME");
1684 GNUNET_free_non_null (vaddr);
1685 process_pending_messages (session_c);
1687 if (session_c->expecting_welcome != GNUNET_YES)
1689 GNUNET_break_op (0);
1690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1693 session_c->expecting_welcome = GNUNET_NO;
1694 if (0 < (addrlen = msize - sizeof (struct WelcomeMessage)))
1696 addr = (const struct sockaddr *) &wm[1];
1697 tcp_plugin_address_suggested (plugin, addr, addrlen);
1699 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1704 * Calculate how long we should delay reading from the TCP socket to
1705 * ensure that we stay within our bandwidth limits (push back).
1707 * @param session for which client should this be calculated
1709 static struct GNUNET_TIME_Relative
1710 calculate_throttle_delay (struct Session *session)
1712 struct GNUNET_TIME_Relative ret;
1713 struct GNUNET_TIME_Absolute now;
1718 now = GNUNET_TIME_absolute_get ();
1719 del = now.value - session->last_quota_update.value;
1720 if (del > MAX_BANDWIDTH_CARRY)
1722 update_quota (session, GNUNET_YES);
1723 del = now.value - session->last_quota_update.value;
1724 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1726 if (session->quota_in == 0)
1727 session->quota_in = 1; /* avoid divison by zero */
1728 avail = del * session->quota_in;
1729 if (avail > session->last_received)
1730 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1731 excess = session->last_received - avail;
1732 ret.value = excess / session->quota_in;
1738 * Task to signal the server that we can continue
1739 * receiving from the TCP client now.
1742 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1744 struct Session *session = cls;
1745 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1750 * We've received data for this peer via TCP. Unbox,
1751 * compute latency and forward.
1753 * @param cls closure
1754 * @param client identification of the client
1755 * @param message the actual message
1758 handle_tcp_data (void *cls,
1759 struct GNUNET_SERVER_Client *client,
1760 const struct GNUNET_MessageHeader *message)
1762 struct Plugin *plugin = cls;
1763 struct Session *session;
1764 const struct DataMessage *dm;
1766 const struct GNUNET_MessageHeader *msg;
1767 struct GNUNET_TIME_Relative latency;
1768 struct GNUNET_TIME_Absolute ttime;
1769 struct GNUNET_TIME_Absolute now;
1770 struct GNUNET_TIME_Relative delay;
1773 msize = ntohs (message->size);
1775 sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1777 GNUNET_break_op (0);
1778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1781 session = find_session_by_client (plugin, client);
1782 if ((NULL == session) || (GNUNET_NO != session->expecting_welcome))
1784 GNUNET_break_op (0);
1785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1789 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1790 "tcp", "Receiving %u bytes from `%4s'.\n",
1791 msize, GNUNET_i2s (&session->target));
1793 dm = (const struct DataMessage *) message;
1794 session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1795 GNUNET_ntohll (dm->ack_out));
1796 msg = (const struct GNUNET_MessageHeader *) &dm[1];
1797 if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1799 GNUNET_break_op (0);
1800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1803 /* estimate latency */
1804 ack_in = GNUNET_ntohll (dm->ack_in);
1805 if ((ack_in <= session->out_msg_counter) &&
1806 (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1808 delay = GNUNET_TIME_relative_ntoh (dm->delay);
1809 ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1810 now = GNUNET_TIME_absolute_get ();
1811 if (delay.value > now.value - ttime.value)
1812 delay.value = 0; /* not plausible */
1813 /* update (round-trip) latency using ageing; we
1814 use 7:1 so that we can reasonably quickly react
1815 to changes, but not so fast that latency is largely
1817 session->latency_estimate
1818 = ((7 * session->latency_estimate) +
1819 (now.value - ttime.value - delay.value)) / 8;
1821 latency.value = (uint64_t) session->latency_estimate;
1824 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1826 "Forwarding data of type %u to transport service.\n",
1829 plugin->env->receive (plugin->env->cls, latency, &session->target, msg);
1830 /* update bandwidth used */
1831 session->last_received += msize;
1832 update_quota (session, GNUNET_NO);
1834 delay = calculate_throttle_delay (session);
1835 if (delay.value == 0)
1836 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1838 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1839 delay, &delayed_done, session);
1844 * Handlers for the various TCP messages.
1846 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1847 {&handle_tcp_ping, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING, 0},
1848 {&handle_tcp_pong, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG, 0},
1849 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 0},
1850 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1856 create_tcp_handlers (struct Plugin *plugin)
1859 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1860 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1863 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1865 plugin->handlers[i].callback_cls = plugin;
1866 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1871 * Functions with this signature are called whenever a peer
1872 * is disconnected on the network level.
1874 * @param cls closure
1875 * @param client identification of the client
1878 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1880 struct Plugin *plugin = cls;
1881 struct Session *session;
1883 session = find_session_by_client (plugin, client);
1884 if (session == NULL)
1885 return; /* unknown, nothing to do */
1887 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1889 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1890 GNUNET_i2s (&session->target),
1891 (session->connect_addr != NULL) ?
1892 GNUNET_a2s (session->connect_addr,
1893 session->connect_alen) : "*", client);
1895 disconnect_session (session);
1900 * Add the IP of our network interface to the list of
1901 * our external IP addresses.
1904 process_interfaces (void *cls,
1907 const struct sockaddr *addr, socklen_t addrlen)
1909 struct Plugin *plugin = cls;
1911 struct sockaddr_in *v4;
1912 struct sockaddr_in6 *v6;
1914 af = addr->sa_family;
1917 v4 = (struct sockaddr_in *) addr;
1918 v4->sin_port = htons (plugin->adv_port);
1922 GNUNET_assert (af == AF_INET6);
1923 v6 = (struct sockaddr_in6 *) addr;
1924 v6->sin6_port = htons (plugin->adv_port);
1926 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1927 GNUNET_ERROR_TYPE_BULK,
1928 "tcp", _("Found address `%s' (%s)\n"),
1929 GNUNET_a2s (addr, addrlen), name);
1930 plugin->env->notify_address (plugin->env->cls,
1932 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1938 * Function called by the resolver for each address obtained from DNS
1939 * for our own hostname. Add the addresses to the list of our
1940 * external IP addresses.
1942 * @param cls closure
1943 * @param addr one of the addresses of the host, NULL for the last address
1944 * @param addrlen length of the address
1947 process_hostname_ips (void *cls,
1948 const struct sockaddr *addr, socklen_t addrlen)
1950 struct Plugin *plugin = cls;
1954 plugin->hostname_dns = NULL;
1957 process_interfaces (plugin, "<hostname>", GNUNET_YES, addr, addrlen);
1962 * Entry point for the plugin.
1965 libgnunet_plugin_transport_tcp_init (void *cls)
1967 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1968 struct GNUNET_TRANSPORT_PluginFunctions *api;
1969 struct Plugin *plugin;
1970 struct GNUNET_SERVICE_Context *service;
1971 unsigned long long aport;
1972 unsigned long long bport;
1974 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
1975 if (service == NULL)
1977 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
1980 ("Failed to start service for `%s' transport plugin.\n"),
1986 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1992 GNUNET_CONFIGURATION_get_value_number (env->cfg,
1995 &aport)) && (aport > 65535)))
1997 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2000 ("Require valid port number for service `%s' in configuration!\n"),
2002 GNUNET_SERVICE_stop (service);
2007 plugin = GNUNET_malloc (sizeof (struct Plugin));
2008 plugin->open_port = bport;
2009 plugin->adv_port = aport;
2011 plugin->lsock = NULL;
2012 plugin->statistics = NULL;
2013 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2015 api->validate = &tcp_plugin_validate;
2016 api->send = &tcp_plugin_send;
2017 api->disconnect = &tcp_plugin_disconnect;
2018 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2019 api->set_receive_quota = &tcp_plugin_set_receive_quota;
2020 api->address_suggested = &tcp_plugin_address_suggested;
2021 api->cost_estimate = 42; /* TODO: ATS */
2022 plugin->service = service;
2023 plugin->server = GNUNET_SERVICE_get_server (service);
2024 create_tcp_handlers (plugin);
2025 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2026 "tcp", _("TCP transport listening on port %llu\n"), bport);
2028 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2031 ("TCP transport advertises itself as being on port %llu\n"),
2033 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
2035 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2036 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
2039 HOSTNAME_RESOLVE_TIMEOUT,
2040 &process_hostname_ips,
2047 * Exit point from the plugin.
2050 libgnunet_plugin_transport_tcp_done (void *cls)
2052 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2053 struct Plugin *plugin = api->cls;
2054 struct Session *session;
2056 while (NULL != (session = plugin->sessions))
2057 disconnect_session (session);
2058 if (NULL != plugin->hostname_dns)
2060 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2061 plugin->hostname_dns = NULL;
2063 GNUNET_SERVICE_stop (plugin->service);
2064 GNUNET_free (plugin->handlers);
2065 GNUNET_free (plugin);
2070 /* end of plugin_transport_tcp.c */