2 This file is part of GNUnet.
3 (C) 2010,2011 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 3, 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/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
40 * Size of the neighbour hash map.
42 #define NEIGHBOUR_TABLE_SIZE 256
45 * How often must a peer violate bandwidth quotas before we start
46 * to simply drop its messages?
48 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
51 * How often do we send KEEPALIVE messages to each of our neighbours?
52 * (idle timeout is 5 minutes or 300 seconds, so with 90s interval we
53 * send 3 keepalives in each interval, so 3 messages would need to be
54 * lost in a row for a disconnect).
56 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
59 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
62 * Entry in neighbours.
64 struct NeighbourMapEntry;
67 * Message a peer sends to another to indicate its
68 * preference for communicating via a particular
69 * session (and the desire to establish a real
72 struct SessionConnectMessage
75 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
77 struct GNUNET_MessageHeader header;
82 uint32_t reserved GNUNET_PACKED;
85 * Absolute time at the sender. Only the most recent connect
86 * message implies which session is preferred by the sender.
88 struct GNUNET_TIME_AbsoluteNBO timestamp;
93 struct SessionDisconnectMessage
96 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
98 struct GNUNET_MessageHeader header;
103 uint32_t reserved GNUNET_PACKED;
106 * Purpose of the signature. Extends over the timestamp.
107 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
109 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
112 * Absolute time at the sender. Only the most recent connect
113 * message implies which session is preferred by the sender.
115 struct GNUNET_TIME_AbsoluteNBO timestamp;
118 * Public key of the sender.
120 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
123 * Signature of the peer that sends us the disconnect. Only
124 * valid if the timestamp is AFTER the timestamp from the
125 * corresponding 'CONNECT' message.
127 struct GNUNET_CRYPTO_RsaSignature signature;
133 * For each neighbour we keep a list of messages
134 * that we still want to transmit to the neighbour.
140 * This is a doubly linked list.
142 struct MessageQueue *next;
145 * This is a doubly linked list.
147 struct MessageQueue *prev;
150 * Once this message is actively being transmitted, which
151 * neighbour is it associated with?
153 struct NeighbourMapEntry *n;
156 * Function to call once we're done.
158 GST_NeighbourSendContinuation cont;
166 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
167 * stuck together in memory. Allocated at the end of this struct.
169 const char *message_buf;
172 * Size of the message buf
174 size_t message_buf_size;
177 * At what time should we fail?
179 struct GNUNET_TIME_Absolute timeout;
185 /* fresh peer or completely disconnected */
187 /* sent CONNECT message to other peer, waiting for CONNECT_ACK */
189 /* received CONNECT message to other peer, sending CONNECT_ACK */
191 /* sent CONNECT_ACK message to other peer, wait for ACK or payload */
192 S_CONNECT_RECV_ACK_SENT = 8,
193 /* received ACK or payload */
195 /* Disconnect in progress */
200 * Entry in neighbours.
202 struct NeighbourMapEntry
206 * Head of list of messages we would like to send to this peer;
207 * must contain at most one message per client.
209 struct MessageQueue *messages_head;
212 * Tail of list of messages we would like to send to this peer; must
213 * contain at most one message per client.
215 struct MessageQueue *messages_tail;
218 * Performance data for the peer.
220 //struct GNUNET_ATS_Information *ats;
223 * Are we currently trying to send a message? If so, which one?
225 struct MessageQueue *is_active;
228 * Active session for communicating with the peer.
230 struct Session *session;
233 * Name of the plugin we currently use.
238 * Address used for communicating with the peer, NULL for inbound connections.
243 * Number of bytes in 'addr'.
248 * Identity of this neighbour.
250 struct GNUNET_PeerIdentity id;
253 * ID of task scheduled to run when this peer is about to
254 * time out (will free resources associated with the peer).
256 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
259 * ID of task scheduled to send keepalives.
261 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
264 * ID of task scheduled to run when we should try transmitting
265 * the head of the message queue.
267 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
270 * Tracker for inbound bandwidth.
272 struct GNUNET_BANDWIDTH_Tracker in_tracker;
275 * Inbound bandwidth from ATS, activated when connection is up
277 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
280 * Inbound bandwidth from ATS, activated when connection is up
282 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
285 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
287 struct GNUNET_TIME_Absolute connect_ts;
289 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
292 * How often has the other peer (recently) violated the inbound
293 * traffic limit? Incremented by 10 per violation, decremented by 1
294 * per non-violation (for each time interval).
296 unsigned int quota_violation_count;
299 * Number of values in 'ats' array.
301 //unsigned int ats_count;
305 * Do we currently consider this neighbour connected? (as far as
306 * the connect/disconnect callbacks are concerned)?
316 * All known neighbours and their HELLOs.
318 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
321 * Closure for connect_notify_cb and disconnect_notify_cb
323 static void *callback_cls;
326 * Function to call when we connected to a neighbour.
328 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
331 * Function to call when we disconnected from a neighbour.
333 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
336 * counter for connected neighbours
338 static int neighbours_connected;
341 * Lookup a neighbour entry in the neighbours hash map.
343 * @param pid identity of the peer to look up
344 * @return the entry, NULL if there is no existing record
346 static struct NeighbourMapEntry *
347 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
349 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
352 #define change_state(n, state, ...) change (n, state, __LINE__)
355 is_connecting (struct NeighbourMapEntry * n)
357 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
363 is_connected (struct NeighbourMapEntry * n)
365 if (n->state == S_CONNECTED)
371 is_disconnecting (struct NeighbourMapEntry * n)
373 if (n->state == S_DISCONNECT)
379 print_state (int state)
383 return "S_CONNECTED";
386 return "S_CONNECT_RECV";
388 case S_CONNECT_RECV_ACK_SENT:
389 return"S_CONNECT_RECV_ACK_SENT";
392 return "S_CONNECT_SENT";
395 return "S_DISCONNECT";
397 case S_NOT_CONNECTED:
398 return "S_NOT_CONNECTED";
408 change (struct NeighbourMapEntry * n, int state, int line)
410 char * old = strdup(print_state(n->state));
411 char * new = strdup(print_state(state));
413 /* allowed transitions */
414 int allowed = GNUNET_NO;
416 case S_NOT_CONNECTED:
417 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
418 (state == S_DISCONNECT))
420 allowed = GNUNET_YES;
425 if ((state == S_NOT_CONNECTED) || (state == S_DISCONNECT) ||
426 (state == S_CONNECTED))
428 allowed = GNUNET_YES;
433 if ((state == S_NOT_CONNECTED) || (state == S_CONNECTED) ||
434 (state == S_DISCONNECT) || /* FIXME SENT -> RECV ISSUE!*/ (state == S_CONNECT_RECV))
436 allowed = GNUNET_YES;
441 if (state == S_DISCONNECT)
443 allowed = GNUNET_YES;
449 if (state == S_NOT_CONNECTED)
451 allowed = GNUNET_YES;
461 if (allowed == GNUNET_NO)
463 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
464 "Illegal state transition from `%s' to `%s' in line %u \n",
469 return GNUNET_SYSERR;
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
474 GNUNET_i2s (&n->id), n, old, new, line);
481 send_with_plugin ( const struct GNUNET_PeerIdentity * target,
485 struct GNUNET_TIME_Relative timeout,
486 struct Session * session,
487 const char * plugin_name,
491 GNUNET_TRANSPORT_TransmitContinuation cont,
495 struct GNUNET_TRANSPORT_PluginFunctions *papi;
496 size_t ret = GNUNET_SYSERR;
498 papi = GST_plugins_find (plugin_name);
502 cont (cont_cls, target, GNUNET_SYSERR);
503 return GNUNET_SYSERR;
506 ret = papi->send (papi->cls,
519 cont (cont_cls, target, GNUNET_SYSERR);
525 * Task invoked to start a transmission to another peer.
527 * @param cls the 'struct NeighbourMapEntry'
528 * @param tc scheduler context
531 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
535 * We're done with our transmission attempt, continue processing.
537 * @param cls the 'struct MessageQueue' of the message
538 * @param receiver intended receiver
539 * @param success whether it worked or not
542 transmit_send_continuation (void *cls,
543 const struct GNUNET_PeerIdentity *receiver,
546 struct MessageQueue *mq;
547 struct NeighbourMapEntry *n;
553 GNUNET_assert (n->is_active == mq);
555 if (success == GNUNET_YES)
557 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
558 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
563 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
564 (success == GNUNET_OK) ? "successful" : "FAILED");
566 if (NULL != mq->cont)
567 mq->cont (mq->cont_cls, success);
573 * Check the ready list for the given neighbour and if a plugin is
574 * ready for transmission (and if we have a message), do so!
576 * @param n target peer for which to transmit
579 try_transmission_to_peer (struct NeighbourMapEntry *n)
581 struct MessageQueue *mq;
582 struct GNUNET_TIME_Relative timeout;
585 if (n->is_active != NULL)
588 return; /* transmission already pending */
590 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
593 return; /* currently waiting for bandwidth */
595 while (NULL != (mq = n->messages_head))
597 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
598 if (timeout.rel_value > 0)
600 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
603 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
606 return; /* no more messages */
608 if (GST_plugins_find (n->plugin_name) == NULL)
613 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
617 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
620 GNUNET_i2s (&n->id));
621 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
622 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
623 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
627 ret = send_with_plugin (&n->id,
628 mq->message_buf, mq->message_buf_size, 0,
630 n->session, n->plugin_name, n->addr, n->addrlen,
632 &transmit_send_continuation, mq);
635 /* failure, but 'send' would not call continuation in this case,
636 * so we need to do it here! */
637 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
644 * Task invoked to start a transmission to another peer.
646 * @param cls the 'struct NeighbourMapEntry'
647 * @param tc scheduler context
650 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
652 struct NeighbourMapEntry *n = cls;
653 GNUNET_assert (NULL != lookup_neighbour(&n->id));
654 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
655 try_transmission_to_peer (n);
660 * Initialize the neighbours subsystem.
662 * @param cls closure for callbacks
663 * @param connect_cb function to call if we connect to a peer
664 * @param disconnect_cb function to call if we disconnect from a peer
667 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
668 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
671 connect_notify_cb = connect_cb;
672 disconnect_notify_cb = disconnect_cb;
673 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
678 send_disconnect_cont (void *cls,
679 const struct GNUNET_PeerIdentity * target,
682 struct NeighbourMapEntry *n = cls;
687 send_disconnect (struct NeighbourMapEntry *n)
690 struct SessionDisconnectMessage disconnect_msg;
693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s'\n",
694 GNUNET_i2s (&n->id));
697 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
698 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
699 disconnect_msg.reserved = htonl (0);
700 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
701 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
702 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
703 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
704 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
705 disconnect_msg.public_key = GST_my_public_key;
706 GNUNET_assert (GNUNET_OK ==
707 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
708 &disconnect_msg.purpose,
709 &disconnect_msg.signature));
711 ret = send_with_plugin(&n->id, (const char *) &disconnect_msg, sizeof (disconnect_msg),
712 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
713 n->session, n->plugin_name, n->addr, n->addrlen,
714 GNUNET_YES, NULL, NULL);
716 if (ret == GNUNET_SYSERR)
717 return GNUNET_SYSERR;
719 GNUNET_STATISTICS_update (GST_stats,
720 gettext_noop ("# peers disconnected due to external request"), 1,
726 * Disconnect from the given neighbour, clean up the record.
728 * @param n neighbour to disconnect from
731 disconnect_neighbour (struct NeighbourMapEntry *n)
733 struct MessageQueue *mq;
734 int was_connected = is_connected(n);
736 if (is_disconnecting(n) == GNUNET_YES)
739 /* send DISCONNECT MESSAGE */
740 if (is_connected(n) || is_connecting(n))
742 if (GNUNET_OK == send_disconnect(n))
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
744 GNUNET_i2s (&n->id));
746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send DISCONNECT_MSG to `%s'\n",
747 GNUNET_i2s (&n->id));
751 if (is_disconnecting(n))
753 change_state (n, S_DISCONNECT);
755 while (NULL != (mq = n->messages_head))
757 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
758 if (NULL != mq->cont)
759 mq->cont (mq->cont_cls, GNUNET_SYSERR);
762 if (NULL != n->is_active)
764 n->is_active->n = NULL;
769 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
770 GNUNET_SCHEDULER_cancel (n->keepalive_task);
771 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
772 GNUNET_assert (neighbours_connected > 0);
773 neighbours_connected--;
774 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
776 disconnect_notify_cb (callback_cls, &n->id);
778 GNUNET_assert (GNUNET_YES ==
779 GNUNET_CONTAINER_multihashmap_remove (neighbours,
780 &n->id.hashPubKey, n));
781 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
783 GNUNET_SCHEDULER_cancel (n->ats_suggest);
784 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
786 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
788 GNUNET_SCHEDULER_cancel (n->timeout_task);
789 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
791 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
793 GNUNET_SCHEDULER_cancel (n->transmission_task);
794 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
796 if (NULL != n->plugin_name)
798 GNUNET_free (n->plugin_name);
799 n->plugin_name = NULL;
803 GNUNET_free (n->addr);
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
809 GNUNET_i2s (&n->id), n);
815 * Peer has been idle for too long. Disconnect.
817 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
818 * @param tc scheduler context
821 neighbour_timeout_task (void *cls,
822 const struct GNUNET_SCHEDULER_TaskContext *tc)
824 struct NeighbourMapEntry *n = cls;
826 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
828 GNUNET_STATISTICS_update (GST_stats,
829 gettext_noop ("# peers disconnected due to timeout"), 1,
831 disconnect_neighbour (n);
836 * Send another keepalive message.
838 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
839 * @param tc scheduler context
842 neighbour_keepalive_task (void *cls,
843 const struct GNUNET_SCHEDULER_TaskContext *tc)
845 struct NeighbourMapEntry *n = cls;
846 struct GNUNET_MessageHeader m;
848 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
849 &neighbour_keepalive_task,
851 GNUNET_assert (is_connected(n));
852 GNUNET_STATISTICS_update (GST_stats,
853 gettext_noop ("# keepalives sent"), 1,
855 m.size = htons (sizeof (struct GNUNET_MessageHeader));
856 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
858 send_with_plugin(&n->id, (const void *) &m,
860 UINT32_MAX /* priority */ ,
861 GNUNET_TIME_UNIT_FOREVER_REL,
862 n->session, n->plugin_name, n->addr, n->addrlen,
863 GNUNET_YES, NULL, NULL);
868 * Disconnect from the given neighbour.
871 * @param key hash of neighbour's public key (not used)
872 * @param value the 'struct NeighbourMapEntry' of the neighbour
875 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
877 struct NeighbourMapEntry *n = value;
880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
881 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
884 GNUNET_STATISTICS_update (GST_stats,
885 gettext_noop ("# peers disconnected due to global disconnect"), 1,
887 disconnect_neighbour (n);
893 ats_suggest_cancel (void *cls,
894 const struct GNUNET_SCHEDULER_TaskContext *tc)
896 struct NeighbourMapEntry *n = cls;
898 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
901 " ATS did not suggested address to connect to peer `%s'\n",
902 GNUNET_i2s (&n->id));
904 disconnect_neighbour(n);
909 * Cleanup the neighbours subsystem.
912 GST_neighbours_stop ()
914 GNUNET_assert (neighbours != NULL);
916 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
918 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
919 GNUNET_assert (neighbours_connected == 0);
922 connect_notify_cb = NULL;
923 disconnect_notify_cb = NULL;
928 * We tried to send a SESSION_CONNECT message to another peer. If this
929 * succeeded, we change the state. If it failed, we should tell
930 * ATS to not use this address anymore (until it is re-validated).
932 * @param cls the 'struct NeighbourMapEntry'
933 * @param success GNUNET_OK on success
936 send_connect_continuation (void *cls,
937 const struct GNUNET_PeerIdentity * target,
941 struct NeighbourMapEntry *n = cls;
943 GNUNET_assert (n != NULL);
944 GNUNET_assert (!is_connected(n));
946 if (is_disconnecting(n))
947 return; /* neighbour is going away */
948 if (GNUNET_YES != success)
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
953 GNUNET_i2s (&n->id), n->plugin_name,
954 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
960 GNUNET_ATS_address_destroyed (GST_ats,
967 if (n->ats_suggest!= GNUNET_SCHEDULER_NO_TASK)
968 GNUNET_SCHEDULER_cancel(n->ats_suggest);
969 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
970 GNUNET_ATS_suggest_address(GST_ats, &n->id);
973 change_state(n, S_CONNECT_SENT);
978 * We tried to switch addresses with an peer already connected. If it failed,
979 * we should tell ATS to not use this address anymore (until it is re-validated).
981 * @param cls the 'struct NeighbourMapEntry'
982 * @param success GNUNET_OK on success
985 send_switch_address_continuation (void *cls,
986 const struct GNUNET_PeerIdentity * target,
990 struct NeighbourMapEntry *n = cls;
992 GNUNET_assert (n != NULL);
993 if (is_disconnecting(n))
994 return; /* neighbour is going away */
996 GNUNET_assert (n->state == S_CONNECTED);
997 if (GNUNET_YES != success)
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1002 GNUNET_i2s (&n->id), n->plugin_name,
1003 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1009 GNUNET_ATS_address_destroyed (GST_ats,
1016 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1017 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1018 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1019 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1025 * We tried to send a SESSION_CONNECT message to another peer. If this
1026 * succeeded, we change the state. If it failed, we should tell
1027 * ATS to not use this address anymore (until it is re-validated).
1029 * @param cls the 'struct NeighbourMapEntry'
1030 * @param success GNUNET_OK on success
1033 send_connect_ack_continuation (void *cls,
1034 const struct GNUNET_PeerIdentity * target,
1038 struct NeighbourMapEntry *n = cls;
1040 GNUNET_assert (n != NULL);
1042 if (GNUNET_YES == success)
1043 return; /* sending successful */
1045 /* sending failed, ask for next address */
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1049 GNUNET_i2s (&n->id), n->plugin_name,
1050 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1055 change_state(n, S_NOT_CONNECTED);
1057 GNUNET_ATS_address_destroyed (GST_ats,
1064 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1065 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1066 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1067 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1071 * For an existing neighbour record, set the active connection to
1072 * the given address.
1074 * @param peer identity of the peer to switch the address for
1075 * @param plugin_name name of transport that delivered the PONG
1076 * @param address address of the other peer, NULL if other peer
1078 * @param address_len number of bytes in address
1079 * @param session session to use (or NULL)
1080 * @param ats performance data
1081 * @param ats_count number of entries in ats
1082 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1083 * connection is not up (yet)
1086 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1087 const char *plugin_name, const void *address,
1088 size_t address_len, struct Session *session,
1089 const struct GNUNET_ATS_Information
1090 *ats, uint32_t ats_count,
1091 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1092 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1094 struct NeighbourMapEntry *n;
1095 struct SessionConnectMessage connect_msg;
1099 GNUNET_assert (neighbours != NULL);
1100 n = lookup_neighbour (peer);
1103 if (NULL == session)
1104 GNUNET_ATS_address_destroyed (GST_ats,
1106 plugin_name, address,
1111 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1113 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1114 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1121 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1124 session, (is_connected(n) ? "CONNECTED" : "NOT CONNECTED"),
1128 GNUNET_free_non_null (n->addr);
1129 n->addr = GNUNET_malloc (address_len);
1130 memcpy (n->addr, address, address_len);
1131 n->bandwidth_in = bandwidth_in;
1132 n->bandwidth_out = bandwidth_out;
1133 n->addrlen = address_len;
1134 n->session = session;
1135 GNUNET_free_non_null (n->plugin_name);
1136 n->plugin_name = GNUNET_strdup (plugin_name);
1137 GNUNET_SCHEDULER_cancel (n->timeout_task);
1139 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1140 &neighbour_timeout_task, n);
1142 if (n->state == S_DISCONNECT)
1144 /* We are disconnecting, nothing to do here */
1147 /* We are not connected/connecting and initiate a fresh connect */
1148 if (n->state == S_NOT_CONNECTED)
1150 msg_len = sizeof (struct SessionConnectMessage);
1151 connect_msg.header.size = htons (msg_len);
1152 connect_msg.header.type =
1153 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1154 connect_msg.reserved = htonl (0);
1155 connect_msg.timestamp =
1156 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1158 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1159 session, plugin_name, address, address_len,
1160 GNUNET_YES, &send_connect_continuation, n);
1164 /* We received a CONNECT message and asked ATS for an address */
1165 else if (n->state == S_CONNECT_RECV)
1167 msg_len = sizeof (struct SessionConnectMessage);
1168 connect_msg.header.size = htons (msg_len);
1169 connect_msg.header.type =
1170 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1171 connect_msg.reserved = htonl (0);
1172 connect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1174 ret = send_with_plugin(&n->id, (const void *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1175 session, plugin_name, address, address_len,
1176 GNUNET_YES, &send_connect_ack_continuation, n);
1177 if (ret == GNUNET_SYSERR)
1179 change_state (n, S_NOT_CONNECTED);
1184 /* connected peer is switching addresses */
1185 else if (n->state == S_CONNECTED)
1187 msg_len = sizeof (struct SessionConnectMessage);
1188 connect_msg.header.size = htons (msg_len);
1189 connect_msg.header.type =
1190 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1191 connect_msg.reserved = htonl (0);
1192 connect_msg.timestamp =
1193 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1195 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1196 session, plugin_name, address, address_len,
1197 GNUNET_YES, &send_switch_address_continuation, n);
1198 if (ret == GNUNET_SYSERR)
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1202 GNUNET_i2s (peer), plugin_name,
1203 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid connection state to switch addresses %u ", n->state);
1212 GNUNET_break_op (0);
1218 * Create an entry in the neighbour map for the given peer
1220 * @param peer peer to create an entry for
1221 * @return new neighbour map entry
1223 static struct NeighbourMapEntry *
1224 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1226 struct NeighbourMapEntry *n;
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Unknown peer `%s', creating new neighbour\n",
1233 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1235 n->state = S_NOT_CONNECTED;
1236 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1237 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1238 MAX_BANDWIDTH_CARRY_S);
1240 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1241 &neighbour_timeout_task, n);
1242 GNUNET_assert (GNUNET_OK ==
1243 GNUNET_CONTAINER_multihashmap_put (neighbours,
1244 &n->id.hashPubKey, n,
1245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1251 * Try to create a connection to the given target (eventually).
1253 * @param target peer to try to connect to
1256 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1258 struct NeighbourMapEntry *n;
1260 GNUNET_assert (neighbours != NULL);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1263 GNUNET_i2s (target));
1266 memcmp (target, &GST_my_identity,
1267 sizeof (struct GNUNET_PeerIdentity)));
1268 n = lookup_neighbour (target);
1272 if ((is_connected(n)) || (is_connecting(n)))
1273 return; /* already connecting or connected */
1274 if (is_disconnecting(n))
1275 change_state (n, S_NOT_CONNECTED);
1280 n = setup_neighbour (target);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Asking ATS for suggested address to connect to peer `%s'\n",
1284 GNUNET_i2s (&n->id));
1287 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1288 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1289 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1290 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1294 * Test if we're connected to the given peer.
1296 * @param target peer to test
1297 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1300 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1302 struct NeighbourMapEntry *n;
1304 GNUNET_assert (neighbours != NULL);
1306 n = lookup_neighbour (target);
1308 if ((NULL == n) || (!is_connected(n)))
1309 return GNUNET_NO; /* not connected */
1315 * A session was terminated. Take note.
1317 * @param peer identity of the peer where the session died
1318 * @param session session that is gone
1321 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1322 struct Session *session)
1324 struct NeighbourMapEntry *n;
1326 GNUNET_assert (neighbours != NULL);
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Session %X to peer `%s' ended \n",
1331 session, GNUNET_i2s (peer));
1334 n = lookup_neighbour (peer);
1337 if (session != n->session)
1338 return; /* doesn't affect us */
1341 GNUNET_free (n->addr);
1345 /* not connected anymore anyway, shouldn't matter */
1346 if ((!is_connected(n)) && (!is_connecting(n)))
1349 /* We are connected, so ask ATS to switch addresses */
1350 GNUNET_SCHEDULER_cancel (n->timeout_task);
1352 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1353 &neighbour_timeout_task, n);
1354 /* try QUICKLY to re-establish a connection, reduce timeout! */
1355 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1356 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1357 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1358 GNUNET_ATS_suggest_address (GST_ats, peer);
1363 * Transmit a message to the given target using the active connection.
1365 * @param target destination
1366 * @param msg message to send
1367 * @param msg_size number of bytes in msg
1368 * @param timeout when to fail with timeout
1369 * @param cont function to call when done
1370 * @param cont_cls closure for 'cont'
1373 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1374 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1375 GST_NeighbourSendContinuation cont, void *cont_cls)
1377 struct NeighbourMapEntry *n;
1378 struct MessageQueue *mq;
1380 GNUNET_assert (neighbours != NULL);
1382 n = lookup_neighbour (target);
1383 if ((n == NULL) || (!is_connected(n)))
1385 GNUNET_STATISTICS_update (GST_stats,
1387 ("# messages not sent (no such peer or not connected)"),
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Could not send message to peer `%s': unknown neighbour",
1393 GNUNET_i2s (target));
1394 else if (!is_connected(n))
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Could not send message to peer `%s': not connected\n",
1397 GNUNET_i2s (target));
1400 cont (cont_cls, GNUNET_SYSERR);
1404 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
1406 GNUNET_STATISTICS_update (GST_stats,
1408 ("# messages not sent (no such peer or not connected)"),
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Could not send message to peer `%s': no address available\n",
1413 GNUNET_i2s (target));
1417 cont (cont_cls, GNUNET_SYSERR);
1421 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1422 GNUNET_STATISTICS_update (GST_stats,
1424 ("# bytes in message queue for other peers"),
1425 msg_size, GNUNET_NO);
1426 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1428 mq->cont_cls = cont_cls;
1429 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1430 memcpy (&mq[1], msg, msg_size);
1431 mq->message_buf = (const char *) &mq[1];
1432 mq->message_buf_size = msg_size;
1433 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1434 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1436 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1437 (NULL == n->is_active))
1438 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1443 * We have received a message from the given sender. How long should
1444 * we delay before receiving more? (Also used to keep the peer marked
1447 * @param sender sender of the message
1448 * @param size size of the message
1449 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1450 * GNUNET_NO if the neighbour is not connected or violates the quota,
1451 * GNUNET_SYSERR if the connection is not fully up yet
1452 * @return how long to wait before reading more from this sender
1454 struct GNUNET_TIME_Relative
1455 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1456 *sender, ssize_t size, int *do_forward)
1458 struct NeighbourMapEntry *n;
1459 struct GNUNET_TIME_Relative ret;
1461 GNUNET_assert (neighbours != NULL);
1463 n = lookup_neighbour (sender);
1466 GST_neighbours_try_connect (sender);
1467 n = lookup_neighbour (sender);
1470 GNUNET_STATISTICS_update (GST_stats,
1472 ("# messages discarded due to lack of neighbour record"),
1474 *do_forward = GNUNET_NO;
1475 return GNUNET_TIME_UNIT_ZERO;
1478 if (!is_connected(n))
1480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1481 _("Plugin gave us %d bytes of data but somehow the session is not marked as UP yet!\n"),
1483 *do_forward = GNUNET_SYSERR;
1484 return GNUNET_TIME_UNIT_ZERO;
1486 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1488 n->quota_violation_count++;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1492 n->in_tracker.available_bytes_per_s__,
1493 n->quota_violation_count);
1495 /* Discount 32k per violation */
1496 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1500 if (n->quota_violation_count > 0)
1502 /* try to add 32k back */
1503 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1504 n->quota_violation_count--;
1507 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1509 GNUNET_STATISTICS_update (GST_stats,
1511 ("# bandwidth quota violations by other peers"),
1513 *do_forward = GNUNET_NO;
1514 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1516 *do_forward = GNUNET_YES;
1517 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1518 if (ret.rel_value > 0)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1523 (unsigned long long) n->in_tracker.
1524 consumption_since_last_update__,
1525 (unsigned int) n->in_tracker.available_bytes_per_s__,
1526 (unsigned long long) ret.rel_value);
1528 GNUNET_STATISTICS_update (GST_stats,
1529 gettext_noop ("# ms throttling suggested"),
1530 (int64_t) ret.rel_value, GNUNET_NO);
1537 * Keep the connection to the given neighbour alive longer,
1538 * we received a KEEPALIVE (or equivalent).
1540 * @param neighbour neighbour to keep alive
1543 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1545 struct NeighbourMapEntry *n;
1547 GNUNET_assert (neighbours != NULL);
1549 n = lookup_neighbour (neighbour);
1552 GNUNET_STATISTICS_update (GST_stats,
1554 ("# KEEPALIVE messages discarded (not connected)"),
1558 GNUNET_SCHEDULER_cancel (n->timeout_task);
1560 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1561 &neighbour_timeout_task, n);
1566 * Change the incoming quota for the given peer.
1568 * @param neighbour identity of peer to change qutoa for
1569 * @param quota new quota
1572 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1573 struct GNUNET_BANDWIDTH_Value32NBO quota)
1575 struct NeighbourMapEntry *n;
1577 GNUNET_assert (neighbours != NULL);
1579 n = lookup_neighbour (neighbour);
1582 GNUNET_STATISTICS_update (GST_stats,
1584 ("# SET QUOTA messages ignored (no such peer)"),
1588 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1589 if (0 != ntohl (quota.value__))
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1593 GNUNET_i2s (&n->id), "SET_QUOTA");
1595 if (is_connected(n))
1596 GNUNET_STATISTICS_update (GST_stats,
1597 gettext_noop ("# disconnects due to quota of 0"), 1,
1599 disconnect_neighbour (n);
1604 * Closure for the neighbours_iterate function.
1606 struct IteratorContext
1609 * Function to call on each connected neighbour.
1611 GST_NeighbourIterator cb;
1621 * Call the callback from the closure for each connected neighbour.
1623 * @param cls the 'struct IteratorContext'
1624 * @param key the hash of the public key of the neighbour
1625 * @param value the 'struct NeighbourMapEntry'
1626 * @return GNUNET_OK (continue to iterate)
1629 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1631 struct IteratorContext *ic = cls;
1632 struct NeighbourMapEntry *n = value;
1634 if (is_connected(n))
1637 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1643 * Iterate over all connected neighbours.
1645 * @param cb function to call
1646 * @param cb_cls closure for cb
1649 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1651 struct IteratorContext ic;
1653 GNUNET_assert (neighbours != NULL);
1657 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1661 * If we have an active connection to the given target, it must be shutdown.
1663 * @param target peer to disconnect from
1666 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1668 struct NeighbourMapEntry *n;
1670 GNUNET_assert (neighbours != NULL);
1672 n = lookup_neighbour (target);
1674 return; /* not active */
1675 if (is_connected(n))
1679 n = lookup_neighbour (target);
1681 return; /* gone already */
1683 disconnect_neighbour (n);
1688 * We received a disconnect message from the given peer,
1689 * validate and process.
1691 * @param peer sender of the message
1692 * @param msg the disconnect message
1695 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1696 const struct GNUNET_MessageHeader *msg)
1698 struct NeighbourMapEntry *n;
1699 const struct SessionDisconnectMessage *sdm;
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Received DISCONNECT message from peer `%s'\n", GNUNET_i2s (peer));
1707 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1709 // GNUNET_break_op (0);
1710 GNUNET_STATISTICS_update (GST_stats,
1711 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1715 sdm = (const struct SessionDisconnectMessage* ) msg;
1716 n = lookup_neighbour (peer);
1718 return; /* gone already */
1719 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1720 n->connect_ts.abs_value)
1722 GNUNET_STATISTICS_update (GST_stats,
1723 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1727 GNUNET_CRYPTO_hash (&sdm->public_key,
1728 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1730 if (0 != memcmp (peer,
1732 sizeof (struct GNUNET_PeerIdentity)))
1734 GNUNET_break_op (0);
1737 if (ntohl (sdm->purpose.size) !=
1738 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1739 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1740 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1742 GNUNET_break_op (0);
1746 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1751 GNUNET_break_op (0);
1754 GST_neighbours_force_disconnect (peer);
1758 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1759 * Consider switching to it.
1761 * @param message possibly a 'struct SessionConnectMessage' (check format)
1762 * @param peer identity of the peer to switch the address for
1763 * @param plugin_name name of transport that delivered the PONG
1764 * @param address address of the other peer, NULL if other peer
1766 * @param address_len number of bytes in address
1767 * @param session session to use (or NULL)
1768 * @param ats performance data
1769 * @param ats_count number of entries in ats
1772 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1773 const struct GNUNET_PeerIdentity *peer,
1774 const char *plugin_name,
1775 const char *sender_address, uint16_t sender_address_len,
1776 struct Session *session,
1777 const struct GNUNET_ATS_Information *ats,
1780 const struct SessionConnectMessage *scm;
1781 struct QuotaSetMessage q_msg;
1782 struct GNUNET_MessageHeader msg;
1783 struct GNUNET_TIME_Absolute ts;
1784 struct NeighbourMapEntry *n;
1790 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1791 "Received CONNECT_ACK message from peer `%s'\n", GNUNET_i2s (peer));
1794 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1796 GNUNET_break_op (0);
1800 scm = (const struct SessionConnectMessage *) message;
1801 GNUNET_break_op (ntohl (scm->reserved) == 0);
1802 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1803 n = lookup_neighbour (peer);
1805 n = setup_neighbour (peer);
1807 if (n->state != S_CONNECT_SENT)
1814 if (NULL != session)
1815 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1817 "Giving ATS session %p of plugin %s for peer %s\n",
1821 GNUNET_ATS_address_update (GST_ats,
1823 plugin_name, sender_address, sender_address_len,
1824 session, ats, ats_count);
1826 change_state (n, S_CONNECTED);
1829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1830 "Setting inbound quota of %u for peer `%s' to \n",
1831 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
1833 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1835 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1836 &neighbour_keepalive_task,
1839 msg_len = sizeof (msg);
1840 msg.size = htons (msg_len);
1841 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
1843 ret = send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
1844 GNUNET_TIME_UNIT_FOREVER_REL,
1845 n->session, n->plugin_name, n->addr, n->addrlen,
1846 GNUNET_YES, NULL, NULL);
1848 if (ret == GNUNET_SYSERR)
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
1851 GNUNET_i2s (&n->id), n->plugin_name,
1852 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1857 neighbours_connected++;
1858 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1860 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1865 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
1867 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1868 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1869 q_msg.quota = n->bandwidth_out;
1870 q_msg.peer = (*peer);
1871 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1875 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
1876 const struct GNUNET_PeerIdentity *peer,
1877 const char *plugin_name,
1878 const char *sender_address, uint16_t sender_address_len,
1879 struct Session *session,
1880 const struct GNUNET_ATS_Information *ats,
1883 struct NeighbourMapEntry *n;
1884 struct QuotaSetMessage q_msg;
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "Received ACK message from peer `%s'\n", GNUNET_i2s (peer));
1891 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
1893 GNUNET_break_op (0);
1897 n = lookup_neighbour (peer);
1904 // if (n->state != S_CONNECT_RECV)
1905 /* if (is_connecting(n))
1907 send_disconnect (n);
1908 change_state (n, S_DISCONNECT);
1913 if (is_connected(n))
1916 if (NULL != session)
1917 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1919 "Giving ATS session %p of plugin %s for peer %s\n",
1923 GNUNET_ATS_address_update (GST_ats,
1925 plugin_name, sender_address, sender_address_len,
1926 session, ats, ats_count);
1928 change_state (n, S_CONNECTED);
1930 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1932 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1933 &neighbour_keepalive_task,
1936 neighbours_connected++;
1937 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1939 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1944 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
1947 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1948 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1949 q_msg.quota = n->bandwidth_out;
1950 q_msg.peer = (*peer);
1951 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1954 struct BlackListCheckContext
1956 struct GNUNET_ATS_Information *ats;
1960 struct Session *session;
1962 char *sender_address;
1964 uint16_t sender_address_len;
1968 struct GNUNET_TIME_Absolute ts;
1973 handle_connect_blacklist_cont (void *cls,
1974 const struct GNUNET_PeerIdentity
1977 struct NeighbourMapEntry *n;
1978 struct BlackListCheckContext * bcc = cls;
1981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1982 "Blacklist check due to CONNECT message: `%s'\n", GNUNET_i2s (peer), (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
1986 if (GNUNET_OK != result)
1992 n = lookup_neighbour (peer);
1994 n = setup_neighbour (peer);
1996 if (bcc->ts.abs_value > n->connect_ts.abs_value)
1998 if (NULL != bcc->session)
1999 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2001 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
2004 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address, bcc->sender_address_len),
2006 GNUNET_ATS_address_update (GST_ats,
2008 bcc->plugin_name, bcc->sender_address, bcc->sender_address_len,
2009 bcc->session, bcc->ats, bcc->ats_count);
2010 n->connect_ts = bcc->ts;
2015 if (n->state != S_NOT_CONNECTED)
2017 change_state (n, S_CONNECT_RECV);
2019 /* Ask ATS for an address to connect via that address */
2020 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2021 GNUNET_SCHEDULER_cancel(n->ats_suggest);
2022 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
2023 GNUNET_ATS_suggest_address(GST_ats, peer);
2027 * We received a 'SESSION_CONNECT' message from the other peer.
2028 * Consider switching to it.
2030 * @param message possibly a 'struct SessionConnectMessage' (check format)
2031 * @param peer identity of the peer to switch the address for
2032 * @param plugin_name name of transport that delivered the PONG
2033 * @param address address of the other peer, NULL if other peer
2035 * @param address_len number of bytes in address
2036 * @param session session to use (or NULL)
2037 * @param ats performance data
2038 * @param ats_count number of entries in ats (excluding 0-termination)
2041 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2042 const struct GNUNET_PeerIdentity *peer,
2043 const char *plugin_name,
2044 const char *sender_address, uint16_t sender_address_len,
2045 struct Session *session,
2046 const struct GNUNET_ATS_Information *ats,
2049 const struct SessionConnectMessage *scm;
2050 struct NeighbourMapEntry * n;
2051 struct BlackListCheckContext * bcc = NULL;
2055 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2056 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2059 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2061 GNUNET_break_op (0);
2065 scm = (const struct SessionConnectMessage *) message;
2066 GNUNET_break_op (ntohl (scm->reserved) == 0);
2068 n = lookup_neighbour(peer);
2071 /* connected peer switches addresses */
2072 if (is_connected(n))
2074 GNUNET_ATS_address_update(GST_ats, peer, plugin_name, sender_address, sender_address_len, session, ats, ats_count);
2079 /* we are not connected to this peer */
2080 /* do blacklist check*/
2081 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2082 sizeof (struct GNUNET_ATS_Information) * ats_count +
2083 sender_address_len +
2084 strlen (plugin_name)+1);
2086 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2088 bcc->ats_count = ats_count;
2089 bcc->sender_address_len = sender_address_len;
2090 bcc->session = session;
2092 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2093 memcpy (bcc->ats, ats,sizeof (struct GNUNET_ATS_Information) * ats_count );
2095 bcc->sender_address = (char *) &bcc->ats[ats_count];
2096 memcpy (bcc->sender_address, sender_address , sender_address_len);
2098 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2099 strcpy (bcc->plugin_name, plugin_name);
2101 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont, bcc);
2105 /* end of file gnunet-service-transport_neighbours.c */