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 * begin 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 *target,
656 const void *addr, size_t addrlen)
658 struct GNUNET_SERVER_Client *client;
659 struct GNUNET_CONNECTION_Handle *conn;
660 struct Session *session;
663 if (addrlen == sizeof (struct sockaddr_in))
665 else if (addrlen == sizeof (struct sockaddr_in6))
670 return NULL; /* invalid address */
672 conn = GNUNET_CONNECTION_create_from_sockaddr (plugin->env->sched,
676 GNUNET_SERVER_MAX_MESSAGE_SIZE);
680 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
682 "Failed to create connection to peer at `%s'.\n",
683 GNUNET_a2s(addr, addrlen));
687 client = GNUNET_SERVER_connect_socket (plugin->server, conn);
688 GNUNET_assert (client != NULL);
689 session = create_session (plugin, target, client, addr, addrlen);
691 GNUNET_free (session->pending_messages);
692 session->pending_messages = NULL;
693 session->connect_alen = addrlen;
694 session->connect_addr = GNUNET_malloc (addrlen);
695 session->expecting_welcome = GNUNET_SYSERR;
696 memcpy (session->connect_addr, addr, addrlen);
698 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
700 "Creating new session %p with `%s' for `%4s' based on `%s' request.\n",
702 GNUNET_a2s (addr, addrlen),
703 GNUNET_i2s (&session->target),
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;
736 session = connect_and_create_validation_session (plugin, target, addr, addrlen);
740 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
741 "tcp", "Failed to create fresh session.\n");
743 return GNUNET_SYSERR;
745 pm = GNUNET_malloc (sizeof (struct PendingMessage) +
746 sizeof (struct ValidationChallengeMessage) + addrlen);
747 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
748 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
749 pm->is_welcome = GNUNET_YES;
750 vcm = (struct ValidationChallengeMessage*) &pm[1];
752 htons (sizeof (struct ValidationChallengeMessage) + addrlen);
753 vcm->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING);
754 vcm->challenge = htonl(challenge);
755 vcm->target = *target;
756 memcpy (&vcm[1], addr, addrlen);
757 GNUNET_assert (session->pending_messages == NULL);
758 session->pending_messages = pm;
759 process_pending_messages (session);
765 * Functions with this signature are called whenever we need
766 * to close a session due to a disconnect or failure to
767 * establish a connection.
769 * @param session session to close down
772 disconnect_session (struct Session *session)
774 struct Session *prev;
776 struct PendingMessage *pm;
779 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
781 "Disconnecting from `%4s' at %s (session %p).\n",
782 GNUNET_i2s(&session->target),
783 (session->connect_addr != NULL) ?
784 GNUNET_a2s(session->connect_addr,
785 session->connect_alen) : "*",
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 (session->transmit_handle);
809 session->transmit_handle = NULL;
811 while (NULL != (pm = session->pending_messages))
814 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
816 pm->transmit_cont != NULL
817 ? "Could not deliver message of type %u to `%4s'.\n"
818 : "Could not deliver message of type %u to `%4s', notifying.\n",
819 ntohs(pm->msg->type),
820 GNUNET_i2s(&session->target));
822 session->pending_messages = pm->next;
823 if (NULL != pm->transmit_cont)
824 pm->transmit_cont (pm->transmit_cont_cls,
825 &session->target, GNUNET_SYSERR);
828 if (GNUNET_NO == session->expecting_welcome)
831 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
833 "Notifying transport service about loss of data connection with `%4s'.\n",
834 GNUNET_i2s(&session->target));
836 /* Data session that actually went past the
837 initial handshake; transport service may
838 know about this one, so we need to
839 notify transport service about disconnect */
840 session->plugin->env->receive (session->plugin->env->cls,
841 GNUNET_TIME_UNIT_ZERO,
842 &session->target, NULL);
844 if (session->client != NULL)
846 GNUNET_SERVER_client_drop (session->client);
847 session->client = NULL;
849 GNUNET_free_non_null (session->connect_addr);
850 GNUNET_free (session);
855 * Iterator callback to go over all addresses. If we get
856 * a TCP address, increment the counter
858 * @param cls closure, points to the counter
859 * @param tname name of the transport
860 * @param expiration expiration time
861 * @param addr the address
862 * @param addrlen length of the address
863 * @return GNUNET_OK to keep the address,
864 * GNUNET_NO to delete it from the HELLO
865 * GNUNET_SYSERR to stop iterating (but keep current address)
868 count_tcp_addresses (void *cls,
870 struct GNUNET_TIME_Absolute expiration,
871 const void *addr, size_t addrlen)
873 unsigned int *counter = cls;
875 if (0 != strcmp (tname, "tcp"))
876 return GNUNET_OK; /* not one of ours */
878 return GNUNET_OK; /* failed to connect */
882 struct ConnectContext
884 struct Plugin *plugin;
886 struct GNUNET_CONNECTION_Handle *sa;
888 struct PendingMessage *welcome;
895 * Iterator callback to go over all addresses. If we get
896 * the "pos" TCP address, try to connect to it.
899 * @param tname name of the transport
900 * @param expiration expiration time
901 * @param addrlen length of the address
902 * @param addr the address
903 * @return GNUNET_OK to keep the address,
904 * GNUNET_NO to delete it from the HELLO
905 * GNUNET_SYSERR to stop iterating (but keep current address)
908 try_connect_to_address (void *cls,
910 struct GNUNET_TIME_Absolute expiration,
911 const void *addr, size_t addrlen)
913 struct ConnectContext *cc = cls;
916 if (0 != strcmp (tname, "tcp"))
917 return GNUNET_OK; /* not one of ours */
918 if (sizeof (struct sockaddr_in) == addrlen)
920 else if (sizeof (struct sockaddr_in6) == addrlen)
924 /* not a valid address */
930 cc->welcome = create_welcome (addrlen, addr, cc->plugin);
932 GNUNET_CONNECTION_create_from_sockaddr (cc->plugin->env->sched,
934 GNUNET_SERVER_MAX_MESSAGE_SIZE);
936 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
938 "Connecting using address %s.\n",
939 GNUNET_a2s(addr, addrlen));
941 return GNUNET_SYSERR;
943 return GNUNET_OK; /* failed to connect */
948 * Type of an iterator over the hosts. Note that each
949 * host will be called with each available protocol.
952 * @param peer id of the peer, NULL for last call
953 * @param hello hello message for the peer (can be NULL)
954 * @param trust amount of trust we have in the peer
957 session_try_connect (void *cls,
958 const struct GNUNET_PeerIdentity *peer,
959 const struct GNUNET_HELLO_Message *hello, uint32_t trust)
961 struct Session *session = cls;
963 struct ConnectContext cctx;
964 struct PendingMessage *pm;
969 /* last call, destroy session if we are still not
971 if (session->client != NULL)
974 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
976 "Now connected to `%4s', now processing messages.\n",
977 GNUNET_i2s(&session->target));
979 process_pending_messages (session);
984 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
986 "Failed to connect to `%4s' (no working `%s'), closing session.\n",
987 GNUNET_i2s(&session->target),
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),
1036 if (session->client == NULL)
1038 GNUNET_break (0); /* how could this happen? */
1039 GNUNET_free_non_null (cctx.welcome);
1044 pm->next = session->pending_messages;
1045 session->pending_messages = pm;
1047 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1049 "Connected to `%4s', now sending `%s' message.\n",
1050 GNUNET_i2s(&session->target),
1057 * Function that can be used by the transport service to transmit
1058 * a message using the plugin.
1060 * @param cls closure
1061 * @param service_context value passed to the transport-service
1062 * to identify the neighbour
1063 * @param target who should receive this message
1064 * @param priority how important is the message
1065 * @param msg the message to transmit
1066 * @param timeout when should we time out (give up) if we can not transmit?
1067 * @param cont continuation to call once the message has
1068 * been transmitted (or if the transport is ready
1069 * for the next transmission call; or if the
1070 * peer disconnected...)
1071 * @param cont_cls closure for cont
1074 tcp_plugin_send (void *cls,
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;
1083 struct PendingMessage *pm;
1084 struct PendingMessage *pme;
1086 session = find_session_by_target (plugin, target);
1087 pm = GNUNET_malloc (sizeof (struct PendingMessage) + ntohs (msg->size));
1088 pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
1089 memcpy (pm->msg, msg, ntohs (msg->size));
1090 pm->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1091 pm->transmit_cont = cont;
1092 pm->transmit_cont_cls = cont_cls;
1093 if (session == NULL)
1095 session = GNUNET_malloc (sizeof (struct Session));
1097 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1099 "Asked to transmit, creating fresh session %p.\n",
1102 session->next = plugin->sessions;
1103 plugin->sessions = session;
1104 session->plugin = plugin;
1105 session->target = *target;
1106 session->last_quota_update = GNUNET_TIME_absolute_get ();
1107 session->quota_in = plugin->env->default_quota_in;
1108 session->expecting_welcome = GNUNET_YES;
1109 session->pending_messages = pm;
1110 session->ic = GNUNET_PEERINFO_iterate (plugin->env->cfg,
1113 0, timeout, &session_try_connect, session);
1116 GNUNET_assert (session != NULL);
1117 GNUNET_assert (session->client != NULL);
1118 /* append pm to pending_messages list */
1119 pme = session->pending_messages;
1122 session->pending_messages = pm;
1126 while (NULL != pme->next)
1131 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1132 "tcp", "Asked to transmit, added message to list.\n");
1134 process_pending_messages (session);
1140 * Function that can be called to force a disconnect from the
1141 * specified neighbour. This should also cancel all previously
1142 * scheduled transmissions. Obviously the transmission may have been
1143 * partially completed already, which is OK. The plugin is supposed
1144 * to close the connection (if applicable) and no longer call the
1145 * transmit continuation(s).
1147 * Finally, plugin MUST NOT call the services's receive function to
1148 * notify the service that the connection to the specified target was
1149 * closed after a getting this call.
1151 * @param cls closure
1152 * @param target peer for which the last transmission is
1156 tcp_plugin_disconnect (void *cls,
1157 const struct GNUNET_PeerIdentity *target)
1159 struct Plugin *plugin = cls;
1160 struct Session *session;
1161 struct PendingMessage *pm;
1163 session = find_session_by_target (plugin, target);
1164 if (session == NULL)
1170 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1172 "Asked to cancel session with `%4s'\n",
1173 GNUNET_i2s(target));
1175 pm = session->pending_messages;
1178 pm->transmit_cont = NULL;
1179 pm->transmit_cont_cls = NULL;
1182 if (session->client != NULL)
1184 GNUNET_SERVER_client_drop (session->client);
1185 session->client = NULL;
1187 /* rest of the clean-up of the session will be done as part of
1188 disconnect_notify which should be triggered any time now
1189 (or which may be triggering this call in the first place) */
1193 struct PrettyPrinterContext
1195 GNUNET_TRANSPORT_AddressStringCallback asc;
1202 * Append our port and forward the result.
1205 append_port (void *cls, const char *hostname)
1207 struct PrettyPrinterContext *ppc = cls;
1210 if (hostname == NULL)
1212 ppc->asc (ppc->asc_cls, NULL);
1216 GNUNET_asprintf (&ret, "%s:%d", hostname, ppc->port);
1217 ppc->asc (ppc->asc_cls, ret);
1223 * Convert the transports address to a nice, human-readable
1226 * @param cls closure
1227 * @param type name of the transport that generated the address
1228 * @param addr one of the addresses of the host, NULL for the last address
1229 * the specific address format depends on the transport
1230 * @param addrlen length of the address
1231 * @param numeric should (IP) addresses be displayed in numeric form?
1232 * @param timeout after how long should we give up?
1233 * @param asc function to call on each string
1234 * @param asc_cls closure for asc
1237 tcp_plugin_address_pretty_printer (void *cls,
1242 struct GNUNET_TIME_Relative timeout,
1243 GNUNET_TRANSPORT_AddressStringCallback asc,
1246 struct Plugin *plugin = cls;
1247 const struct sockaddr_in *v4;
1248 const struct sockaddr_in6 *v6;
1249 struct PrettyPrinterContext *ppc;
1251 if ((addrlen != sizeof (struct sockaddr_in)) &&
1252 (addrlen != sizeof (struct sockaddr_in6)))
1254 /* invalid address */
1255 GNUNET_break_op (0);
1256 asc (asc_cls, NULL);
1259 ppc = GNUNET_malloc (sizeof (struct PrettyPrinterContext));
1261 ppc->asc_cls = asc_cls;
1262 if (addrlen == sizeof (struct sockaddr_in))
1264 v4 = (const struct sockaddr_in *) addr;
1265 ppc->port = ntohs (v4->sin_port);
1269 v6 = (const struct sockaddr_in6 *) addr;
1270 ppc->port = ntohs (v6->sin6_port);
1273 GNUNET_RESOLVER_hostname_get (plugin->env->sched,
1277 !numeric, timeout, &append_port, ppc);
1282 * Update the last-received and bandwidth quota values
1285 * @param session session to update
1286 * @param force set to GNUNET_YES if we should update even
1287 * though the minimum refresh time has not yet expired
1290 update_quota (struct Session *session, int force)
1292 struct GNUNET_TIME_Absolute now;
1293 unsigned long long delta;
1294 unsigned long long total_allowed;
1295 unsigned long long total_remaining;
1297 now = GNUNET_TIME_absolute_get ();
1298 delta = now.value - session->last_quota_update.value;
1299 if ((delta < MIN_QUOTA_REFRESH_TIME) && (!force))
1300 return; /* too early, not enough data */
1302 total_allowed = session->quota_in * delta;
1303 if (total_allowed > session->last_received)
1305 /* got less than acceptable */
1306 total_remaining = total_allowed - session->last_received;
1307 session->last_received = 0;
1308 delta = total_remaining / session->quota_in; /* bonus seconds */
1309 if (delta > MAX_BANDWIDTH_CARRY)
1310 delta = MAX_BANDWIDTH_CARRY; /* limit amount of carry-over */
1314 /* got more than acceptable */
1315 session->last_received -= total_allowed;
1318 session->last_quota_update.value = now.value - delta;
1323 * Set a quota for receiving data from the given peer; this is a
1324 * per-transport limit. The transport should limit its read/select
1325 * calls to stay below the quota (in terms of incoming data).
1327 * @param cls closure
1328 * @param target the peer for whom the quota is given
1329 * @param quota_in quota for receiving/sending data in bytes per ms
1332 tcp_plugin_set_receive_quota (void *cls,
1333 const struct GNUNET_PeerIdentity *target,
1336 struct Plugin *plugin = cls;
1337 struct Session *session;
1339 session = find_session_by_target (plugin, target);
1340 if (session == NULL)
1341 return; /* peer must have disconnected, ignore */
1342 if (session->quota_in != quota_in)
1344 update_quota (session, GNUNET_YES);
1345 if (session->quota_in > quota_in)
1346 session->last_quota_update = GNUNET_TIME_absolute_get ();
1347 session->quota_in = quota_in;
1353 * Check if the given port is plausible (must be either
1354 * our listen port or our advertised port). If it is
1355 * neither, we return one of these two ports at random.
1357 * @return either in_port or a more plausible port
1360 check_port (struct Plugin *plugin, uint16_t in_port)
1362 if ((in_port == plugin->adv_port) || (in_port == plugin->open_port))
1364 return (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
1366 ? plugin->open_port : plugin->adv_port;
1371 * Another peer has suggested an address for this
1372 * peer and transport plugin. Check that this could be a valid
1373 * address. If so, consider adding it to the list
1376 * @param cls closure
1377 * @param addr pointer to the address
1378 * @param addrlen length of addr
1379 * @return GNUNET_OK if this is a plausible address for this peer
1383 tcp_plugin_address_suggested (void *cls, const void *addr, size_t addrlen)
1385 struct Plugin *plugin = cls;
1386 char buf[sizeof (struct sockaddr_in6)];
1387 struct sockaddr_in *v4;
1388 struct sockaddr_in6 *v6;
1390 if ((addrlen != sizeof (struct sockaddr_in)) &&
1391 (addrlen != sizeof (struct sockaddr_in6)))
1393 GNUNET_break_op (0);
1394 return GNUNET_SYSERR;
1396 memcpy (buf, addr, sizeof (struct sockaddr_in6));
1397 if (addrlen == sizeof (struct sockaddr_in))
1399 v4 = (struct sockaddr_in *) buf;
1400 v4->sin_port = htons (check_port (plugin, ntohs (v4->sin_port)));
1404 v6 = (struct sockaddr_in6 *) buf;
1405 v6->sin6_port = htons (check_port (plugin, ntohs (v6->sin6_port)));
1408 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1410 "Informing transport service about my address `%s'.\n",
1411 GNUNET_a2s(addr, addrlen));
1413 plugin->env->notify_address (plugin->env->cls,
1415 buf, addrlen, LEARNED_ADDRESS_EXPIRATION);
1421 * Send a validation challenge response.
1424 send_vcr (void *cls,
1428 struct ValidationChallengeResponse *vcr = cls;
1436 msize = ntohs(vcr->header.size);
1437 GNUNET_assert (size >= msize);
1438 memcpy (buf, vcr, msize);
1445 * We've received a PING from this peer via TCP.
1446 * Send back our PONG.
1448 * @param cls closure
1449 * @param client identification of the client
1450 * @param message the actual message
1453 handle_tcp_ping (void *cls,
1454 struct GNUNET_SERVER_Client *client,
1455 const struct GNUNET_MessageHeader *message)
1457 struct Plugin *plugin = cls;
1458 const struct ValidationChallengeMessage *vcm;
1459 struct ValidationChallengeResponse *vcr;
1466 GNUNET_SERVER_client_get_address (client,
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1471 "Processing `%s' from `%s'\n",
1473 GNUNET_a2s (addr, addrlen));
1477 msize = ntohs (message->size);
1478 if (msize < sizeof (struct ValidationChallengeMessage))
1480 GNUNET_break_op (0);
1481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1484 vcm = (const struct ValidationChallengeMessage *) message;
1485 if (0 != memcmp (&vcm->target,
1486 plugin->env->my_identity, sizeof (struct GNUNET_PeerIdentity)))
1488 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1489 _("Received `%s' message not destined for me!\n"), "PING");
1490 /* TODO: call statistics */
1491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1494 msize -= sizeof (struct ValidationChallengeMessage);
1496 tcp_plugin_address_suggested (plugin, &vcm[1], msize))
1498 GNUNET_break_op (0);
1499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1503 GNUNET_SERVER_client_get_address (client,
1508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1511 vcr = GNUNET_malloc (sizeof (struct ValidationChallengeResponse) + addrlen);
1512 vcr->header.size = htons (sizeof (struct ValidationChallengeResponse) + addrlen);
1513 vcr->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG);
1515 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1517 sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1519 vcr->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING);
1520 vcr->challenge = vcm->challenge;
1521 vcr->signer = *plugin->env->my_public_key;
1525 GNUNET_assert (GNUNET_OK ==
1526 GNUNET_CRYPTO_rsa_sign (plugin->env->my_private_key,
1530 GNUNET_assert (GNUNET_OK ==
1531 GNUNET_CRYPTO_rsa_verify
1532 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1535 plugin->env->my_public_key));
1539 GNUNET_SERVER_notify_transmit_ready (client,
1540 sizeof (struct ValidationChallengeResponse) + addrlen,
1541 GNUNET_TIME_UNIT_SECONDS,
1548 /* after a PING, we always close the connection */
1549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1554 * Handle PONG-message.
1556 * @param cls handle for this plugin
1557 * @param client from where did we receive the PONG
1558 * @param message the actual message
1561 handle_tcp_pong (void *cls,
1562 struct GNUNET_SERVER_Client *client,
1563 const struct GNUNET_MessageHeader *message)
1565 struct Plugin *plugin = cls;
1566 const struct ValidationChallengeResponse *vcr;
1567 struct GNUNET_PeerIdentity peer;
1570 const struct sockaddr *addr;
1571 struct sockaddr_in v4;
1572 struct sockaddr_in6 v6;
1575 struct sockaddr *claddr;
1578 GNUNET_SERVER_client_get_address (client,
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1583 "Processing `%s' from `%s'\n",
1585 GNUNET_a2s (claddr, addrlen));
1586 GNUNET_free (claddr);
1589 if (ntohs(message->size) < sizeof(struct ValidationChallengeResponse))
1591 GNUNET_break_op (0);
1592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1595 addrlen = ntohs(message->size) - sizeof(struct ValidationChallengeResponse);
1596 vcr = (const struct ValidationChallengeResponse *) message;
1597 if ( (ntohl(vcr->purpose.size) !=
1598 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1600 sizeof ( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1603 GNUNET_break_op (0);
1604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1608 GNUNET_CRYPTO_rsa_verify
1609 (GNUNET_SIGNATURE_PURPOSE_TRANSPORT_TCP_PING,
1614 GNUNET_break_op (0);
1615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1618 GNUNET_CRYPTO_hash (&vcr->signer,
1619 sizeof( struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1621 addr = (const struct sockaddr*) &vcr[1];
1622 if (addrlen == sizeof (struct sockaddr_in))
1624 memcpy (&v4, addr, sizeof (struct sockaddr_in));
1625 v4.sin_port = htons(check_port (plugin, ntohs (v4.sin_port)));
1626 sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v4,
1629 else if (addrlen == sizeof (struct sockaddr_in6))
1631 memcpy (&v6, addr, sizeof (struct sockaddr_in6));
1632 v6.sin6_port = htons(check_port (plugin, ntohs (v6.sin6_port)));
1633 sender_addr = GNUNET_strdup (GNUNET_a2s((const struct sockaddr*) &v6,
1638 GNUNET_break_op (0);
1639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1642 plugin->env->notify_validation (plugin->env->cls,
1645 ntohl(vcr->challenge),
1647 GNUNET_free (sender_addr);
1648 /* after a PONG, we always close the connection */
1649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1654 * We've received a welcome from this peer via TCP.
1655 * Possibly create a fresh client record and send back
1658 * @param cls closure
1659 * @param client identification of the client
1660 * @param message the actual message
1663 handle_tcp_welcome (void *cls,
1664 struct GNUNET_SERVER_Client *client,
1665 const struct GNUNET_MessageHeader *message)
1667 struct Plugin *plugin = cls;
1668 struct Session *session_c;
1669 const struct WelcomeMessage *wm;
1674 const struct sockaddr *addr;
1676 msize = ntohs (message->size);
1677 if (msize < sizeof (struct WelcomeMessage))
1679 GNUNET_break_op (0);
1680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1683 wm = (const struct WelcomeMessage *) message;
1685 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1687 "Received `%s' message from `%4s/%p'.\n", "WELCOME",
1688 GNUNET_i2s(&wm->clientIdentity),
1691 session_c = find_session_by_client (plugin, client);
1692 if (session_c == NULL)
1695 GNUNET_SERVER_client_get_address (client, &vaddr, &alen);
1696 GNUNET_SERVER_client_keep (client);
1697 session_c = create_session (plugin,
1698 &wm->clientIdentity, client, vaddr, alen);
1700 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1702 "Creating new session %p for incoming `%s' message.\n",
1703 session_c, "WELCOME");
1705 GNUNET_free_non_null (vaddr);
1706 process_pending_messages (session_c);
1708 if (session_c->expecting_welcome != GNUNET_YES)
1710 GNUNET_break_op (0);
1711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1714 session_c->expecting_welcome = GNUNET_NO;
1715 if (0 < (addrlen = msize - sizeof (struct WelcomeMessage)))
1717 addr = (const struct sockaddr *) &wm[1];
1718 tcp_plugin_address_suggested (plugin, addr, addrlen);
1720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1725 * Calculate how long we should delay reading from the TCP socket to
1726 * ensure that we stay within our bandwidth limits (push back).
1728 * @param session for which client should this be calculated
1730 static struct GNUNET_TIME_Relative
1731 calculate_throttle_delay (struct Session *session)
1733 struct GNUNET_TIME_Relative ret;
1734 struct GNUNET_TIME_Absolute now;
1739 now = GNUNET_TIME_absolute_get ();
1740 del = now.value - session->last_quota_update.value;
1741 if (del > MAX_BANDWIDTH_CARRY)
1743 update_quota (session, GNUNET_YES);
1744 del = now.value - session->last_quota_update.value;
1745 GNUNET_assert (del <= MAX_BANDWIDTH_CARRY);
1747 if (session->quota_in == 0)
1748 session->quota_in = 1; /* avoid divison by zero */
1749 avail = del * session->quota_in;
1750 if (avail > session->last_received)
1751 return GNUNET_TIME_UNIT_ZERO; /* can receive right now */
1752 excess = session->last_received - avail;
1753 ret.value = excess / session->quota_in;
1759 * Task to signal the server that we can continue
1760 * receiving from the TCP client now.
1763 delayed_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1765 struct Session *session = cls;
1766 GNUNET_SERVER_receive_done (session->client, GNUNET_OK);
1771 * We've received data for this peer via TCP. Unbox,
1772 * compute latency and forward.
1774 * @param cls closure
1775 * @param client identification of the client
1776 * @param message the actual message
1779 handle_tcp_data (void *cls,
1780 struct GNUNET_SERVER_Client *client,
1781 const struct GNUNET_MessageHeader *message)
1783 struct Plugin *plugin = cls;
1784 struct Session *session;
1785 const struct DataMessage *dm;
1787 const struct GNUNET_MessageHeader *msg;
1788 struct GNUNET_TIME_Relative latency;
1789 struct GNUNET_TIME_Absolute ttime;
1790 struct GNUNET_TIME_Absolute now;
1791 struct GNUNET_TIME_Relative delay;
1794 msize = ntohs (message->size);
1796 sizeof (struct DataMessage) + sizeof (struct GNUNET_MessageHeader)))
1798 GNUNET_break_op (0);
1799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1802 session = find_session_by_client (plugin, client);
1803 if ((NULL == session) || (GNUNET_NO != session->expecting_welcome))
1805 GNUNET_break_op (0);
1806 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1810 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1811 "tcp", "Receiving %u bytes from `%4s'.\n",
1813 GNUNET_i2s(&session->target));
1815 dm = (const struct DataMessage *) message;
1816 session->max_in_msg_counter = GNUNET_MAX (session->max_in_msg_counter,
1817 GNUNET_ntohll (dm->ack_out));
1818 msg = (const struct GNUNET_MessageHeader *) &dm[1];
1819 if (msize != sizeof (struct DataMessage) + ntohs (msg->size))
1821 GNUNET_break_op (0);
1822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1825 /* estimate latency */
1826 ack_in = GNUNET_ntohll (dm->ack_in);
1827 if ((ack_in <= session->out_msg_counter) &&
1828 (session->out_msg_counter - ack_in < ACK_LOG_SIZE))
1830 delay = GNUNET_TIME_relative_ntoh (dm->delay);
1831 ttime = session->gen_time[ack_in % ACK_LOG_SIZE];
1832 now = GNUNET_TIME_absolute_get ();
1833 if (delay.value > now.value - ttime.value)
1834 delay.value = 0; /* not plausible */
1835 /* update (round-trip) latency using ageing; we
1836 use 7:1 so that we can reasonably quickly react
1837 to changes, but not so fast that latency is largely
1839 session->latency_estimate
1840 = ((7 * session->latency_estimate) +
1841 (now.value - ttime.value - delay.value)) / 8;
1843 latency.value = (uint64_t) session->latency_estimate;
1846 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1848 "Forwarding data of type %u to transport service.\n",
1851 plugin->env->receive (plugin->env->cls,
1852 latency, &session->target, msg);
1853 /* update bandwidth used */
1854 session->last_received += msize;
1855 update_quota (session, GNUNET_NO);
1857 delay = calculate_throttle_delay (session);
1858 if (delay.value == 0)
1859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1861 GNUNET_SCHEDULER_add_delayed (session->plugin->env->sched,
1862 delay, &delayed_done, session);
1867 * Handlers for the various TCP messages.
1869 static struct GNUNET_SERVER_MessageHandler my_handlers[] = {
1870 {&handle_tcp_ping, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PING, 0},
1871 {&handle_tcp_pong, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_PONG, 0},
1872 {&handle_tcp_welcome, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_WELCOME, 0},
1873 {&handle_tcp_data, NULL, GNUNET_MESSAGE_TYPE_TRANSPORT_TCP_DATA, 0},
1879 create_tcp_handlers (struct Plugin *plugin)
1882 plugin->handlers = GNUNET_malloc (sizeof (my_handlers));
1883 memcpy (plugin->handlers, my_handlers, sizeof (my_handlers));
1886 sizeof (my_handlers) / sizeof (struct GNUNET_SERVER_MessageHandler);
1888 plugin->handlers[i].callback_cls = plugin;
1889 GNUNET_SERVER_add_handlers (plugin->server, plugin->handlers);
1894 * Functions with this signature are called whenever a peer
1895 * is disconnected on the network level.
1897 * @param cls closure
1898 * @param client identification of the client
1901 disconnect_notify (void *cls, struct GNUNET_SERVER_Client *client)
1903 struct Plugin *plugin = cls;
1904 struct Session *session;
1906 session = find_session_by_client (plugin, client);
1907 if (session == NULL)
1908 return; /* unknown, nothing to do */
1910 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1912 "Destroying session of `%4s' with %s (%p) due to network-level disconnect.\n",
1913 GNUNET_i2s(&session->target),
1914 (session->connect_addr != NULL) ?
1915 GNUNET_a2s(session->connect_addr,
1916 session->connect_alen) : "*",
1919 disconnect_session (session);
1924 * Add the IP of our network interface to the list of
1925 * our external IP addresses.
1928 process_interfaces (void *cls,
1931 const struct sockaddr *addr, socklen_t addrlen)
1933 struct Plugin *plugin = cls;
1935 struct sockaddr_in *v4;
1936 struct sockaddr_in6 *v6;
1938 af = addr->sa_family;
1941 v4 = (struct sockaddr_in *) addr;
1942 v4->sin_port = htons (plugin->adv_port);
1946 GNUNET_assert (af == AF_INET6);
1947 v6 = (struct sockaddr_in6 *) addr;
1948 v6->sin6_port = htons (plugin->adv_port);
1950 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO |
1951 GNUNET_ERROR_TYPE_BULK,
1952 "tcp", _("Found address `%s' (%s)\n"),
1953 GNUNET_a2s(addr, addrlen), name);
1954 plugin->env->notify_address (plugin->env->cls,
1956 addr, addrlen, GNUNET_TIME_UNIT_FOREVER_REL);
1962 * Function called by the resolver for each address obtained from DNS
1963 * for our own hostname. Add the addresses to the list of our
1964 * external IP addresses.
1966 * @param cls closure
1967 * @param addr one of the addresses of the host, NULL for the last address
1968 * @param addrlen length of the address
1971 process_hostname_ips (void *cls,
1972 const struct sockaddr *addr, socklen_t addrlen)
1974 struct Plugin *plugin = cls;
1978 plugin->hostname_dns = NULL;
1981 process_interfaces (plugin,
1990 * Entry point for the plugin.
1993 libgnunet_plugin_transport_tcp_init (void *cls)
1995 struct GNUNET_TRANSPORT_PluginEnvironment *env = cls;
1996 struct GNUNET_TRANSPORT_PluginFunctions *api;
1997 struct Plugin *plugin;
1998 struct GNUNET_SERVICE_Context *service;
1999 unsigned long long aport;
2000 unsigned long long bport;
2002 service = GNUNET_SERVICE_start ("transport-tcp", env->sched, env->cfg);
2003 if (service == NULL)
2005 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
2008 ("Failed to start service for `%s' transport plugin.\n"),
2014 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2020 GNUNET_CONFIGURATION_get_value_number (env->cfg,
2023 &aport)) && (aport > 65535)))
2025 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
2028 ("Require valid port number for service `%s' in configuration!\n"),
2030 GNUNET_SERVICE_stop (service);
2035 plugin = GNUNET_malloc (sizeof (struct Plugin));
2036 plugin->open_port = bport;
2037 plugin->adv_port = aport;
2039 plugin->lsock = NULL;
2040 plugin->statistics = NULL;
2041 api = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PluginFunctions));
2043 api->validate = &tcp_plugin_validate;
2044 api->send = &tcp_plugin_send;
2045 api->disconnect = &tcp_plugin_disconnect;
2046 api->address_pretty_printer = &tcp_plugin_address_pretty_printer;
2047 api->set_receive_quota = &tcp_plugin_set_receive_quota;
2048 api->address_suggested = &tcp_plugin_address_suggested;
2049 api->cost_estimate = 42; /* TODO: ATS */
2050 plugin->service = service;
2051 plugin->server = GNUNET_SERVICE_get_server (service);
2052 create_tcp_handlers (plugin);
2053 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2054 "tcp", _("TCP transport listening on port %llu\n"), bport);
2056 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
2059 ("TCP transport advertises itself as being on port %llu\n"),
2061 GNUNET_SERVER_disconnect_notify (plugin->server, &disconnect_notify,
2063 GNUNET_OS_network_interfaces_list (&process_interfaces, plugin);
2064 plugin->hostname_dns = GNUNET_RESOLVER_hostname_resolve (env->sched,
2067 HOSTNAME_RESOLVE_TIMEOUT,
2068 &process_hostname_ips, plugin);
2074 * Exit point from the plugin.
2077 libgnunet_plugin_transport_tcp_done (void *cls)
2079 struct GNUNET_TRANSPORT_PluginFunctions *api = cls;
2080 struct Plugin *plugin = api->cls;
2081 struct Session *session;
2083 while (NULL != (session = plugin->sessions))
2084 disconnect_session (session);
2085 if (NULL != plugin->hostname_dns)
2087 GNUNET_RESOLVER_request_cancel (plugin->hostname_dns);
2088 plugin->hostname_dns = NULL;
2090 GNUNET_SERVICE_stop (plugin->service);
2091 GNUNET_free (plugin->handlers);
2092 GNUNET_free (plugin);
2097 /* end of plugin_transport_tcp.c */