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 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
66 * Entry in neighbours.
68 struct NeighbourMapEntry;
71 * Message a peer sends to another to indicate its
72 * preference for communicating via a particular
73 * session (and the desire to establish a real
76 struct SessionConnectMessage
79 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
81 struct GNUNET_MessageHeader header;
86 uint32_t reserved GNUNET_PACKED;
89 * Absolute time at the sender. Only the most recent connect
90 * message implies which session is preferred by the sender.
92 struct GNUNET_TIME_AbsoluteNBO timestamp;
97 struct SessionDisconnectMessage
100 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
102 struct GNUNET_MessageHeader header;
107 uint32_t reserved GNUNET_PACKED;
110 * Purpose of the signature. Extends over the timestamp.
111 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
113 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
116 * Absolute time at the sender. Only the most recent connect
117 * message implies which session is preferred by the sender.
119 struct GNUNET_TIME_AbsoluteNBO timestamp;
122 * Public key of the sender.
124 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
127 * Signature of the peer that sends us the disconnect. Only
128 * valid if the timestamp is AFTER the timestamp from the
129 * corresponding 'CONNECT' message.
131 struct GNUNET_CRYPTO_RsaSignature signature;
137 * For each neighbour we keep a list of messages
138 * that we still want to transmit to the neighbour.
144 * This is a doubly linked list.
146 struct MessageQueue *next;
149 * This is a doubly linked list.
151 struct MessageQueue *prev;
154 * Once this message is actively being transmitted, which
155 * neighbour is it associated with?
157 struct NeighbourMapEntry *n;
160 * Function to call once we're done.
162 GST_NeighbourSendContinuation cont;
170 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
171 * stuck together in memory. Allocated at the end of this struct.
173 const char *message_buf;
176 * Size of the message buf
178 size_t message_buf_size;
181 * At what time should we fail?
183 struct GNUNET_TIME_Absolute timeout;
189 /* fresh peer or completely disconnected */
191 /* sent CONNECT message to other peer, waiting for CONNECT_ACK */
193 /* received CONNECT message to other peer, sending CONNECT_ACK */
195 /* sent CONNECT_ACK message to other peer, wait for ACK or payload */
196 S_CONNECT_RECV_ACK_SENT = 8,
197 /* received ACK or payload */
199 /* Disconnect in progress */
204 * Entry in neighbours.
206 struct NeighbourMapEntry
210 * Head of list of messages we would like to send to this peer;
211 * must contain at most one message per client.
213 struct MessageQueue *messages_head;
216 * Tail of list of messages we would like to send to this peer; must
217 * contain at most one message per client.
219 struct MessageQueue *messages_tail;
222 * Performance data for the peer.
224 //struct GNUNET_ATS_Information *ats;
227 * Are we currently trying to send a message? If so, which one?
229 struct MessageQueue *is_active;
232 * Active session for communicating with the peer.
234 struct Session *session;
237 * Name of the plugin we currently use.
242 * Address used for communicating with the peer, NULL for inbound connections.
247 * Number of bytes in 'addr'.
252 * Identity of this neighbour.
254 struct GNUNET_PeerIdentity id;
257 * ID of task scheduled to run when this peer is about to
258 * time out (will free resources associated with the peer).
260 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
263 * ID of task scheduled to send keepalives.
265 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
268 * ID of task scheduled to run when we should try transmitting
269 * the head of the message queue.
271 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
274 * Tracker for inbound bandwidth.
276 struct GNUNET_BANDWIDTH_Tracker in_tracker;
279 * Inbound bandwidth from ATS, activated when connection is up
281 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
284 * Inbound bandwidth from ATS, activated when connection is up
286 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
289 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
291 struct GNUNET_TIME_Absolute connect_ts;
295 * We asked ATS for a new address for this peer
297 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
300 * Task the resets the peer state after due to an pending
301 * unsuccessful connection setup
303 GNUNET_SCHEDULER_TaskIdentifier state_reset;
306 * How often has the other peer (recently) violated the inbound
307 * traffic limit? Incremented by 10 per violation, decremented by 1
308 * per non-violation (for each time interval).
310 unsigned int quota_violation_count;
314 * The current state of the peer
315 * Element of enum State
323 * All known neighbours and their HELLOs.
325 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
328 * Closure for connect_notify_cb and disconnect_notify_cb
330 static void *callback_cls;
333 * Function to call when we connected to a neighbour.
335 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
338 * Function to call when we disconnected from a neighbour.
340 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
343 * counter for connected neighbours
345 static int neighbours_connected;
348 * Lookup a neighbour entry in the neighbours hash map.
350 * @param pid identity of the peer to look up
351 * @return the entry, NULL if there is no existing record
353 static struct NeighbourMapEntry *
354 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
356 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
359 #define change_state(n, state, ...) change (n, state, __LINE__)
362 is_connecting (struct NeighbourMapEntry * n)
364 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
370 is_connected (struct NeighbourMapEntry * n)
372 if (n->state == S_CONNECTED)
378 is_disconnecting (struct NeighbourMapEntry * n)
380 if (n->state == S_DISCONNECT)
386 print_state (int state)
390 return "S_CONNECTED";
393 return "S_CONNECT_RECV";
395 case S_CONNECT_RECV_ACK_SENT:
396 return"S_CONNECT_RECV_ACK_SENT";
399 return "S_CONNECT_SENT";
402 return "S_DISCONNECT";
404 case S_NOT_CONNECTED:
405 return "S_NOT_CONNECTED";
415 change (struct NeighbourMapEntry * n, int state, int line);
418 ats_suggest_cancel (void *cls,
419 const struct GNUNET_SCHEDULER_TaskContext *tc);
422 reset_task (void *cls,
423 const struct GNUNET_SCHEDULER_TaskContext *tc)
425 struct NeighbourMapEntry * n = cls;
427 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431 "Connection to peer `%s' %s failed in state `%s', resetting connection attempt \n",
432 GNUNET_i2s (&n->id), GST_plugins_a2s(n->plugin_name, n->addr, n->addrlen), print_state(n->state));
434 GNUNET_STATISTICS_update (GST_stats,
435 gettext_noop ("# failed connection attempts due to timeout"),
439 /* resetting state */
440 n->state = S_NOT_CONNECTED;
442 /* destroying address */
443 GNUNET_ATS_address_destroyed (GST_ats,
450 /* request new address */
451 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
452 GNUNET_SCHEDULER_cancel(n->ats_suggest);
453 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
454 GNUNET_ATS_suggest_address(GST_ats, &n->id);
458 change (struct NeighbourMapEntry * n, int state, int line)
460 char * old = strdup(print_state(n->state));
461 char * new = strdup(print_state(state));
463 /* allowed transitions */
464 int allowed = GNUNET_NO;
466 case S_NOT_CONNECTED:
467 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
468 (state == S_DISCONNECT))
470 allowed = GNUNET_YES;
472 /* Schedule reset task */
473 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) )
475 GNUNET_assert (n->state_reset == GNUNET_SCHEDULER_NO_TASK);
476 n->state_reset = GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
483 if ((state == S_NOT_CONNECTED) || (state == S_DISCONNECT) ||
484 (state == S_CONNECTED) || /* FIXME SENT -> RECV ISSUE!*/ (state == S_CONNECT_SENT))
486 if ((state == S_CONNECTED) || (state == S_DISCONNECT) || (state == S_NOT_CONNECTED))
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
491 GNUNET_i2s (&n->id), GST_plugins_a2s(n->plugin_name, n->addr, n->addrlen), print_state(n->state), print_state(state));
493 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
494 GNUNET_SCHEDULER_cancel (n->state_reset);
495 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
498 allowed = GNUNET_YES;
503 if ((state == S_NOT_CONNECTED) || (state == S_CONNECTED) ||
504 (state == S_DISCONNECT) || /* FIXME SENT -> RECV ISSUE!*/ (state == S_CONNECT_RECV))
506 if ((state == S_CONNECTED) || (state == S_DISCONNECT) || (state == S_NOT_CONNECTED))
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
511 GNUNET_i2s (&n->id), GST_plugins_a2s(n->plugin_name, n->addr, n->addrlen), print_state(n->state), print_state(state));
513 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
514 GNUNET_SCHEDULER_cancel (n->state_reset);
515 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
518 allowed = GNUNET_YES;
523 if (state == S_DISCONNECT)
525 allowed = GNUNET_YES;
531 if (state == S_NOT_CONNECTED)
533 allowed = GNUNET_YES;
543 if (allowed == GNUNET_NO)
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 "Illegal state transition from `%s' to `%s' in line %u \n",
551 return GNUNET_SYSERR;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
557 GNUNET_i2s (&n->id), n, old, new, line);
565 send_with_plugin ( const struct GNUNET_PeerIdentity * target,
569 struct GNUNET_TIME_Relative timeout,
570 struct Session * session,
571 const char * plugin_name,
575 GNUNET_TRANSPORT_TransmitContinuation cont,
579 struct GNUNET_TRANSPORT_PluginFunctions *papi;
580 size_t ret = GNUNET_SYSERR;
582 papi = GST_plugins_find (plugin_name);
586 cont (cont_cls, target, GNUNET_SYSERR);
587 return GNUNET_SYSERR;
590 ret = papi->send (papi->cls,
603 cont (cont_cls, target, GNUNET_SYSERR);
609 * Task invoked to start a transmission to another peer.
611 * @param cls the 'struct NeighbourMapEntry'
612 * @param tc scheduler context
615 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
619 * We're done with our transmission attempt, continue processing.
621 * @param cls the 'struct MessageQueue' of the message
622 * @param receiver intended receiver
623 * @param success whether it worked or not
626 transmit_send_continuation (void *cls,
627 const struct GNUNET_PeerIdentity *receiver,
630 struct MessageQueue *mq;
631 struct NeighbourMapEntry *n;
637 GNUNET_assert (n->is_active == mq);
639 if (success == GNUNET_YES)
641 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
642 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
647 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
648 (success == GNUNET_OK) ? "successful" : "FAILED");
650 if (NULL != mq->cont)
651 mq->cont (mq->cont_cls, success);
657 * Check the ready list for the given neighbour and if a plugin is
658 * ready for transmission (and if we have a message), do so!
660 * @param n target peer for which to transmit
663 try_transmission_to_peer (struct NeighbourMapEntry *n)
665 struct MessageQueue *mq;
666 struct GNUNET_TIME_Relative timeout;
669 if (n->is_active != NULL)
672 return; /* transmission already pending */
674 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
677 return; /* currently waiting for bandwidth */
679 while (NULL != (mq = n->messages_head))
681 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
682 if (timeout.rel_value > 0)
684 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
687 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
690 return; /* no more messages */
692 if (GST_plugins_find (n->plugin_name) == NULL)
697 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
701 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
704 GNUNET_i2s (&n->id));
705 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
706 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
707 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
711 ret = send_with_plugin (&n->id,
712 mq->message_buf, mq->message_buf_size, 0,
714 n->session, n->plugin_name, n->addr, n->addrlen,
716 &transmit_send_continuation, mq);
719 /* failure, but 'send' would not call continuation in this case,
720 * so we need to do it here! */
721 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
728 * Task invoked to start a transmission to another peer.
730 * @param cls the 'struct NeighbourMapEntry'
731 * @param tc scheduler context
734 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
736 struct NeighbourMapEntry *n = cls;
737 GNUNET_assert (NULL != lookup_neighbour(&n->id));
738 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
739 try_transmission_to_peer (n);
744 * Initialize the neighbours subsystem.
746 * @param cls closure for callbacks
747 * @param connect_cb function to call if we connect to a peer
748 * @param disconnect_cb function to call if we disconnect from a peer
751 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
752 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
755 connect_notify_cb = connect_cb;
756 disconnect_notify_cb = disconnect_cb;
757 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
762 send_disconnect_cont (void *cls,
763 const struct GNUNET_PeerIdentity * target,
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s': %i\n",
768 GNUNET_i2s (target), result);
774 send_disconnect (const struct GNUNET_PeerIdentity * target,
775 const char *plugin_name,
776 const char *sender_address, uint16_t sender_address_len,
777 struct Session *session)
780 struct SessionDisconnectMessage disconnect_msg;
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s'\n",
784 GNUNET_i2s (target));
787 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
788 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
789 disconnect_msg.reserved = htonl (0);
790 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
791 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
792 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
793 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
794 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
795 disconnect_msg.public_key = GST_my_public_key;
796 GNUNET_assert (GNUNET_OK ==
797 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
798 &disconnect_msg.purpose,
799 &disconnect_msg.signature));
801 ret = send_with_plugin(target,
802 (const char *) &disconnect_msg, sizeof (disconnect_msg),
803 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
804 session, plugin_name, sender_address, sender_address_len,
805 GNUNET_YES, &send_disconnect_cont, NULL);
807 if (ret == GNUNET_SYSERR)
808 return GNUNET_SYSERR;
810 GNUNET_STATISTICS_update (GST_stats,
811 gettext_noop ("# peers disconnected due to external request"), 1,
817 * Disconnect from the given neighbour, clean up the record.
819 * @param n neighbour to disconnect from
822 disconnect_neighbour (struct NeighbourMapEntry *n)
824 struct MessageQueue *mq;
825 int was_connected = is_connected(n);
827 /* send DISCONNECT MESSAGE */
828 if (is_connected(n) || is_connecting(n))
830 if (GNUNET_OK == send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session))
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
832 GNUNET_i2s (&n->id));
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send DISCONNECT_MSG to `%s'\n",
835 GNUNET_i2s (&n->id));
839 if (is_disconnecting(n))
841 change_state (n, S_DISCONNECT);
843 while (NULL != (mq = n->messages_head))
845 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
846 if (NULL != mq->cont)
847 mq->cont (mq->cont_cls, GNUNET_SYSERR);
850 if (NULL != n->is_active)
852 n->is_active->n = NULL;
857 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
858 GNUNET_SCHEDULER_cancel (n->keepalive_task);
859 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
860 GNUNET_assert (neighbours_connected > 0);
861 neighbours_connected--;
862 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
864 disconnect_notify_cb (callback_cls, &n->id);
866 GNUNET_assert (GNUNET_YES ==
867 GNUNET_CONTAINER_multihashmap_remove (neighbours,
868 &n->id.hashPubKey, n));
869 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
871 GNUNET_SCHEDULER_cancel (n->ats_suggest);
872 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
874 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
876 GNUNET_SCHEDULER_cancel (n->timeout_task);
877 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
879 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
881 GNUNET_SCHEDULER_cancel (n->transmission_task);
882 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
884 if (NULL != n->plugin_name)
886 GNUNET_free (n->plugin_name);
887 n->plugin_name = NULL;
891 GNUNET_free (n->addr);
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
897 GNUNET_i2s (&n->id), n);
903 * Peer has been idle for too long. Disconnect.
905 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
906 * @param tc scheduler context
909 neighbour_timeout_task (void *cls,
910 const struct GNUNET_SCHEDULER_TaskContext *tc)
912 struct NeighbourMapEntry *n = cls;
914 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
916 GNUNET_STATISTICS_update (GST_stats,
917 gettext_noop ("# peers disconnected due to timeout"), 1,
919 disconnect_neighbour (n);
924 * Send another keepalive message.
926 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
927 * @param tc scheduler context
930 neighbour_keepalive_task (void *cls,
931 const struct GNUNET_SCHEDULER_TaskContext *tc)
933 struct NeighbourMapEntry *n = cls;
934 struct GNUNET_MessageHeader m;
936 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
937 &neighbour_keepalive_task,
939 GNUNET_assert (is_connected(n));
940 GNUNET_STATISTICS_update (GST_stats,
941 gettext_noop ("# keepalives sent"), 1,
943 m.size = htons (sizeof (struct GNUNET_MessageHeader));
944 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
946 send_with_plugin(&n->id, (const void *) &m,
948 UINT32_MAX /* priority */ ,
949 GNUNET_TIME_UNIT_FOREVER_REL,
950 n->session, n->plugin_name, n->addr, n->addrlen,
951 GNUNET_YES, NULL, NULL);
956 * Disconnect from the given neighbour.
959 * @param key hash of neighbour's public key (not used)
960 * @param value the 'struct NeighbourMapEntry' of the neighbour
963 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
965 struct NeighbourMapEntry *n = value;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
969 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
972 GNUNET_STATISTICS_update (GST_stats,
973 gettext_noop ("# peers disconnected due to global disconnect"), 1,
975 disconnect_neighbour (n);
981 ats_suggest_cancel (void *cls,
982 const struct GNUNET_SCHEDULER_TaskContext *tc)
984 struct NeighbourMapEntry *n = cls;
986 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 " ATS did not suggested address to connect to peer `%s'\n",
990 GNUNET_i2s (&n->id));
992 disconnect_neighbour(n);
997 * Cleanup the neighbours subsystem.
1000 GST_neighbours_stop ()
1002 GNUNET_assert (neighbours != NULL);
1004 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1006 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1007 GNUNET_assert (neighbours_connected == 0);
1009 callback_cls = NULL;
1010 connect_notify_cb = NULL;
1011 disconnect_notify_cb = NULL;
1016 * We tried to send a SESSION_CONNECT message to another peer. If this
1017 * succeeded, we change the state. If it failed, we should tell
1018 * ATS to not use this address anymore (until it is re-validated).
1020 * @param cls the 'struct NeighbourMapEntry'
1021 * @param success GNUNET_OK on success
1024 send_connect_continuation (void *cls,
1025 const struct GNUNET_PeerIdentity * target,
1029 struct NeighbourMapEntry *n = cls;
1031 GNUNET_assert (n != NULL);
1032 GNUNET_assert (!is_connected(n));
1034 if (is_disconnecting(n))
1035 return; /* neighbour is going away */
1036 if (GNUNET_YES != success)
1039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1040 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1041 GNUNET_i2s (&n->id), n->plugin_name,
1042 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1048 GNUNET_ATS_address_destroyed (GST_ats,
1055 if (n->ats_suggest!= GNUNET_SCHEDULER_NO_TASK)
1056 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1057 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1058 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1061 change_state(n, S_CONNECT_SENT);
1066 * We tried to switch addresses with an peer already connected. If it failed,
1067 * we should tell ATS to not use this address anymore (until it is re-validated).
1069 * @param cls the 'struct NeighbourMapEntry'
1070 * @param success GNUNET_OK on success
1073 send_switch_address_continuation (void *cls,
1074 const struct GNUNET_PeerIdentity * target,
1078 struct NeighbourMapEntry *n = cls;
1080 GNUNET_assert (n != NULL);
1081 if (is_disconnecting(n))
1082 return; /* neighbour is going away */
1084 GNUNET_assert (n->state == S_CONNECTED);
1085 if (GNUNET_YES != success)
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1090 GNUNET_i2s (&n->id), n->plugin_name,
1091 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1097 GNUNET_ATS_address_destroyed (GST_ats,
1104 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1105 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1106 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1107 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1113 * We tried to send a SESSION_CONNECT message to another peer. If this
1114 * succeeded, we change the state. If it failed, we should tell
1115 * ATS to not use this address anymore (until it is re-validated).
1117 * @param cls the 'struct NeighbourMapEntry'
1118 * @param success GNUNET_OK on success
1121 send_connect_ack_continuation (void *cls,
1122 const struct GNUNET_PeerIdentity * target,
1126 struct NeighbourMapEntry *n = cls;
1128 GNUNET_assert (n != NULL);
1130 if (GNUNET_YES == success)
1131 return; /* sending successful */
1133 /* sending failed, ask for next address */
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1137 GNUNET_i2s (&n->id), n->plugin_name,
1138 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1143 change_state(n, S_NOT_CONNECTED);
1145 GNUNET_ATS_address_destroyed (GST_ats,
1152 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1153 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1154 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1155 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1159 * For an existing neighbour record, set the active connection to
1160 * the given address.
1162 * @param peer identity of the peer to switch the address for
1163 * @param plugin_name name of transport that delivered the PONG
1164 * @param address address of the other peer, NULL if other peer
1166 * @param address_len number of bytes in address
1167 * @param session session to use (or NULL)
1168 * @param ats performance data
1169 * @param ats_count number of entries in ats
1170 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1171 * connection is not up (yet)
1174 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1175 const char *plugin_name, const void *address,
1176 size_t address_len, struct Session *session,
1177 const struct GNUNET_ATS_Information
1178 *ats, uint32_t ats_count,
1179 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1180 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1182 struct NeighbourMapEntry *n;
1183 struct SessionConnectMessage connect_msg;
1187 GNUNET_assert (neighbours != NULL);
1188 n = lookup_neighbour (peer);
1191 if (NULL == session)
1192 GNUNET_ATS_address_destroyed (GST_ats,
1194 plugin_name, address,
1199 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1201 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1202 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1209 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1212 session, (is_connected(n) ? "CONNECTED" : "NOT CONNECTED"),
1216 // do not switch addresses just update quotas
1219 if ((is_connected(n)) && (address_len == n->addrlen))
1221 if ((0 == memcmp (address, n->addr, address_len)) &&
1222 (n->session == session))
1224 struct QuotaSetMessage q_msg;
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Sending outbound quota of %u Bps and inbound quota of % Bps for peer `%s' to all clients\n",
1229 ntohl (n->bandwidth_out.value__),
1230 ntohl (n->bandwidth_in.value__),
1234 n->bandwidth_in = bandwidth_in;
1235 n->bandwidth_out = bandwidth_out;
1236 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1238 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1239 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1240 q_msg.quota = n->bandwidth_out;
1241 q_msg.peer = (*peer);
1242 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1248 GNUNET_free_non_null (n->addr);
1249 n->addr = GNUNET_malloc (address_len);
1250 memcpy (n->addr, address, address_len);
1251 n->bandwidth_in = bandwidth_in;
1252 n->bandwidth_out = bandwidth_out;
1253 n->addrlen = address_len;
1254 n->session = session;
1255 GNUNET_free_non_null (n->plugin_name);
1256 n->plugin_name = GNUNET_strdup (plugin_name);
1257 GNUNET_SCHEDULER_cancel (n->timeout_task);
1259 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1260 &neighbour_timeout_task, n);
1262 if (n->state == S_DISCONNECT)
1264 /* We are disconnecting, nothing to do here */
1267 /* We are not connected/connecting and initiate a fresh connect */
1268 if (n->state == S_NOT_CONNECTED)
1270 msg_len = sizeof (struct SessionConnectMessage);
1271 connect_msg.header.size = htons (msg_len);
1272 connect_msg.header.type =
1273 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1274 connect_msg.reserved = htonl (0);
1275 connect_msg.timestamp =
1276 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1278 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1279 session, plugin_name, address, address_len,
1280 GNUNET_YES, &send_connect_continuation, n);
1284 /* We received a CONNECT message and asked ATS for an address */
1285 else if (n->state == S_CONNECT_RECV)
1287 msg_len = sizeof (struct SessionConnectMessage);
1288 connect_msg.header.size = htons (msg_len);
1289 connect_msg.header.type =
1290 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1291 connect_msg.reserved = htonl (0);
1292 connect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1294 ret = send_with_plugin(&n->id, (const void *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1295 session, plugin_name, address, address_len,
1296 GNUNET_YES, &send_connect_ack_continuation, n);
1299 /* connected peer is switching addresses */
1300 else if (n->state == S_CONNECTED)
1302 msg_len = sizeof (struct SessionConnectMessage);
1303 connect_msg.header.size = htons (msg_len);
1304 connect_msg.header.type =
1305 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1306 connect_msg.reserved = htonl (0);
1307 connect_msg.timestamp =
1308 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1310 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1311 session, plugin_name, address, address_len,
1312 GNUNET_YES, &send_switch_address_continuation, n);
1313 if (ret == GNUNET_SYSERR)
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1316 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1317 GNUNET_i2s (peer), plugin_name,
1318 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1325 else if (n->state == S_CONNECT_SENT)
1330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid connection state to switch addresses %u \n", n->state);
1331 GNUNET_break_op (0);
1337 * Create an entry in the neighbour map for the given peer
1339 * @param peer peer to create an entry for
1340 * @return new neighbour map entry
1342 static struct NeighbourMapEntry *
1343 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1345 struct NeighbourMapEntry *n;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Unknown peer `%s', creating new neighbour\n",
1352 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1354 n->state = S_NOT_CONNECTED;
1355 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1356 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1357 MAX_BANDWIDTH_CARRY_S);
1359 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1360 &neighbour_timeout_task, n);
1361 GNUNET_assert (GNUNET_OK ==
1362 GNUNET_CONTAINER_multihashmap_put (neighbours,
1363 &n->id.hashPubKey, n,
1364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1370 * Try to create a connection to the given target (eventually).
1372 * @param target peer to try to connect to
1375 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1377 struct NeighbourMapEntry *n;
1379 GNUNET_assert (neighbours != NULL);
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1382 GNUNET_i2s (target));
1385 memcmp (target, &GST_my_identity,
1386 sizeof (struct GNUNET_PeerIdentity)));
1387 n = lookup_neighbour (target);
1391 if ((is_connected(n)) || (is_connecting(n)))
1392 return; /* already connecting or connected */
1393 if (is_disconnecting(n))
1394 change_state (n, S_NOT_CONNECTED);
1399 n = setup_neighbour (target);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Asking ATS for suggested address to connect to peer `%s'\n",
1403 GNUNET_i2s (&n->id));
1406 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1410 * Test if we're connected to the given peer.
1412 * @param target peer to test
1413 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1416 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1418 struct NeighbourMapEntry *n;
1420 GNUNET_assert (neighbours != NULL);
1422 n = lookup_neighbour (target);
1424 if ((NULL == n) || (!is_connected(n)))
1425 return GNUNET_NO; /* not connected */
1431 * A session was terminated. Take note.
1433 * @param peer identity of the peer where the session died
1434 * @param session session that is gone
1437 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1438 struct Session *session)
1440 struct NeighbourMapEntry *n;
1442 GNUNET_assert (neighbours != NULL);
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "Session %X to peer `%s' ended \n",
1447 session, GNUNET_i2s (peer));
1450 n = lookup_neighbour (peer);
1453 if (session != n->session)
1454 return; /* doesn't affect us */
1457 GNUNET_free (n->addr);
1461 /* not connected anymore anyway, shouldn't matter */
1462 if ((!is_connected(n)) && (!is_connecting(n)))
1465 /* We are connected, so ask ATS to switch addresses */
1466 GNUNET_SCHEDULER_cancel (n->timeout_task);
1468 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1469 &neighbour_timeout_task, n);
1470 /* try QUICKLY to re-establish a connection, reduce timeout! */
1471 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1472 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1473 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1474 GNUNET_ATS_suggest_address (GST_ats, peer);
1479 * Transmit a message to the given target using the active connection.
1481 * @param target destination
1482 * @param msg message to send
1483 * @param msg_size number of bytes in msg
1484 * @param timeout when to fail with timeout
1485 * @param cont function to call when done
1486 * @param cont_cls closure for 'cont'
1489 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1490 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1491 GST_NeighbourSendContinuation cont, void *cont_cls)
1493 struct NeighbourMapEntry *n;
1494 struct MessageQueue *mq;
1496 GNUNET_assert (neighbours != NULL);
1498 n = lookup_neighbour (target);
1499 if ((n == NULL) || (!is_connected(n)))
1501 GNUNET_STATISTICS_update (GST_stats,
1503 ("# messages not sent (no such peer or not connected)"),
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Could not send message to peer `%s': unknown neighbour",
1509 GNUNET_i2s (target));
1510 else if (!is_connected(n))
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Could not send message to peer `%s': not connected\n",
1513 GNUNET_i2s (target));
1516 cont (cont_cls, GNUNET_SYSERR);
1520 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
1522 GNUNET_STATISTICS_update (GST_stats,
1524 ("# messages not sent (no such peer or not connected)"),
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "Could not send message to peer `%s': no address available\n",
1529 GNUNET_i2s (target));
1533 cont (cont_cls, GNUNET_SYSERR);
1537 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1538 GNUNET_STATISTICS_update (GST_stats,
1540 ("# bytes in message queue for other peers"),
1541 msg_size, GNUNET_NO);
1542 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1544 mq->cont_cls = cont_cls;
1545 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1546 memcpy (&mq[1], msg, msg_size);
1547 mq->message_buf = (const char *) &mq[1];
1548 mq->message_buf_size = msg_size;
1549 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1550 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1552 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1553 (NULL == n->is_active))
1554 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1559 * We have received a message from the given sender. How long should
1560 * we delay before receiving more? (Also used to keep the peer marked
1563 * @param sender sender of the message
1564 * @param size size of the message
1565 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1566 * GNUNET_NO if the neighbour is not connected or violates the quota,
1567 * GNUNET_SYSERR if the connection is not fully up yet
1568 * @return how long to wait before reading more from this sender
1570 struct GNUNET_TIME_Relative
1571 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1572 *sender, ssize_t size, int *do_forward)
1574 struct NeighbourMapEntry *n;
1575 struct GNUNET_TIME_Relative ret;
1577 GNUNET_assert (neighbours != NULL);
1579 n = lookup_neighbour (sender);
1582 GST_neighbours_try_connect (sender);
1583 n = lookup_neighbour (sender);
1586 GNUNET_STATISTICS_update (GST_stats,
1588 ("# messages discarded due to lack of neighbour record"),
1590 *do_forward = GNUNET_NO;
1591 return GNUNET_TIME_UNIT_ZERO;
1594 if (!is_connected(n))
1596 *do_forward = GNUNET_SYSERR;
1597 return GNUNET_TIME_UNIT_ZERO;
1599 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1601 n->quota_violation_count++;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1605 n->in_tracker.available_bytes_per_s__,
1606 n->quota_violation_count);
1608 /* Discount 32k per violation */
1609 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1613 if (n->quota_violation_count > 0)
1615 /* try to add 32k back */
1616 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1617 n->quota_violation_count--;
1620 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1622 GNUNET_STATISTICS_update (GST_stats,
1624 ("# bandwidth quota violations by other peers"),
1626 *do_forward = GNUNET_NO;
1627 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1629 *do_forward = GNUNET_YES;
1630 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1631 if (ret.rel_value > 0)
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1636 (unsigned long long) n->in_tracker.
1637 consumption_since_last_update__,
1638 (unsigned int) n->in_tracker.available_bytes_per_s__,
1639 (unsigned long long) ret.rel_value);
1641 GNUNET_STATISTICS_update (GST_stats,
1642 gettext_noop ("# ms throttling suggested"),
1643 (int64_t) ret.rel_value, GNUNET_NO);
1650 * Keep the connection to the given neighbour alive longer,
1651 * we received a KEEPALIVE (or equivalent).
1653 * @param neighbour neighbour to keep alive
1656 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1658 struct NeighbourMapEntry *n;
1660 GNUNET_assert (neighbours != NULL);
1662 n = lookup_neighbour (neighbour);
1665 GNUNET_STATISTICS_update (GST_stats,
1667 ("# KEEPALIVE messages discarded (not connected)"),
1671 GNUNET_SCHEDULER_cancel (n->timeout_task);
1673 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1674 &neighbour_timeout_task, n);
1679 * Change the incoming quota for the given peer.
1681 * @param neighbour identity of peer to change qutoa for
1682 * @param quota new quota
1685 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1686 struct GNUNET_BANDWIDTH_Value32NBO quota)
1688 struct NeighbourMapEntry *n;
1690 GNUNET_assert (neighbours != NULL);
1692 n = lookup_neighbour (neighbour);
1695 GNUNET_STATISTICS_update (GST_stats,
1697 ("# SET QUOTA messages ignored (no such peer)"),
1701 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1702 if (0 != ntohl (quota.value__))
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1706 GNUNET_i2s (&n->id), "SET_QUOTA");
1708 if (is_connected(n))
1709 GNUNET_STATISTICS_update (GST_stats,
1710 gettext_noop ("# disconnects due to quota of 0"), 1,
1712 disconnect_neighbour (n);
1717 * Closure for the neighbours_iterate function.
1719 struct IteratorContext
1722 * Function to call on each connected neighbour.
1724 GST_NeighbourIterator cb;
1734 * Call the callback from the closure for each connected neighbour.
1736 * @param cls the 'struct IteratorContext'
1737 * @param key the hash of the public key of the neighbour
1738 * @param value the 'struct NeighbourMapEntry'
1739 * @return GNUNET_OK (continue to iterate)
1742 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1744 struct IteratorContext *ic = cls;
1745 struct NeighbourMapEntry *n = value;
1747 if (!is_connected(n))
1750 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1756 * Iterate over all connected neighbours.
1758 * @param cb function to call
1759 * @param cb_cls closure for cb
1762 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1764 struct IteratorContext ic;
1766 GNUNET_assert (neighbours != NULL);
1770 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1774 * If we have an active connection to the given target, it must be shutdown.
1776 * @param target peer to disconnect from
1779 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1781 struct NeighbourMapEntry *n;
1783 GNUNET_assert (neighbours != NULL);
1785 n = lookup_neighbour (target);
1787 return; /* not active */
1788 if (is_connected(n))
1790 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1792 n = lookup_neighbour (target);
1794 return; /* gone already */
1796 disconnect_neighbour (n);
1801 * We received a disconnect message from the given peer,
1802 * validate and process.
1804 * @param peer sender of the message
1805 * @param msg the disconnect message
1808 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1809 const struct GNUNET_MessageHeader *msg)
1811 struct NeighbourMapEntry *n;
1812 const struct SessionDisconnectMessage *sdm;
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Received DISCONNECT message from peer `%s'\n", GNUNET_i2s (peer));
1820 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1822 // GNUNET_break_op (0);
1823 GNUNET_STATISTICS_update (GST_stats,
1824 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1828 sdm = (const struct SessionDisconnectMessage* ) msg;
1829 n = lookup_neighbour (peer);
1831 return; /* gone already */
1832 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1833 n->connect_ts.abs_value)
1835 GNUNET_STATISTICS_update (GST_stats,
1836 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1840 GNUNET_CRYPTO_hash (&sdm->public_key,
1841 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1843 if (0 != memcmp (peer,
1845 sizeof (struct GNUNET_PeerIdentity)))
1847 GNUNET_break_op (0);
1850 if (ntohl (sdm->purpose.size) !=
1851 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1852 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1853 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1855 GNUNET_break_op (0);
1859 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1864 GNUNET_break_op (0);
1867 GST_neighbours_force_disconnect (peer);
1871 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1872 * Consider switching to it.
1874 * @param message possibly a 'struct SessionConnectMessage' (check format)
1875 * @param peer identity of the peer to switch the address for
1876 * @param plugin_name name of transport that delivered the PONG
1877 * @param address address of the other peer, NULL if other peer
1879 * @param address_len number of bytes in address
1880 * @param session session to use (or NULL)
1881 * @param ats performance data
1882 * @param ats_count number of entries in ats
1885 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1886 const struct GNUNET_PeerIdentity *peer,
1887 const char *plugin_name,
1888 const char *sender_address, uint16_t sender_address_len,
1889 struct Session *session,
1890 const struct GNUNET_ATS_Information *ats,
1893 const struct SessionConnectMessage *scm;
1894 struct QuotaSetMessage q_msg;
1895 struct GNUNET_MessageHeader msg;
1896 struct NeighbourMapEntry *n;
1902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1903 "Received CONNECT_ACK message from peer `%s'\n", GNUNET_i2s (peer));
1906 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1908 GNUNET_break_op (0);
1912 scm = (const struct SessionConnectMessage *) message;
1913 GNUNET_break_op (ntohl (scm->reserved) == 0);
1914 n = lookup_neighbour (peer);
1916 n = setup_neighbour (peer);
1918 if (n->state != S_CONNECT_SENT)
1921 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1925 if (NULL != session)
1926 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1928 "Giving ATS session %p of plugin %s for peer %s\n",
1932 GNUNET_ATS_address_update (GST_ats,
1934 plugin_name, sender_address, sender_address_len,
1935 session, ats, ats_count);
1937 was_connected = is_connected(n);
1938 if (!is_connected(n))
1939 change_state (n, S_CONNECTED);
1942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1943 "Setting inbound quota of %u for peer `%s' to \n",
1944 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
1946 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1949 msg_len = sizeof (msg);
1950 msg.size = htons (msg_len);
1951 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
1953 ret = send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
1954 GNUNET_TIME_UNIT_FOREVER_REL,
1955 n->session, n->plugin_name, n->addr, n->addrlen,
1956 GNUNET_YES, NULL, NULL);
1958 if (ret == GNUNET_SYSERR)
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
1961 GNUNET_i2s (&n->id), n->plugin_name,
1962 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1970 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1971 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1972 &neighbour_keepalive_task,
1975 neighbours_connected++;
1976 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
1981 GNUNET_i2s (&n->id), n->plugin_name,
1982 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1985 n->session, __LINE__);
1987 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1993 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
1995 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1996 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1997 q_msg.quota = n->bandwidth_out;
1998 q_msg.peer = (*peer);
1999 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2004 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2005 const struct GNUNET_PeerIdentity *peer,
2006 const char *plugin_name,
2007 const char *sender_address, uint16_t sender_address_len,
2008 struct Session *session,
2009 const struct GNUNET_ATS_Information *ats,
2012 struct NeighbourMapEntry *n;
2013 struct QuotaSetMessage q_msg;
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "Received ACK message from peer `%s'\n", GNUNET_i2s (peer));
2021 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2023 GNUNET_break_op (0);
2027 n = lookup_neighbour (peer);
2030 send_disconnect(peer, plugin_name, sender_address, sender_address_len, session);
2035 if (is_connected(n))
2038 if (NULL != session)
2039 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2041 "Giving ATS session %p of plugin %s for peer %s\n",
2045 GNUNET_ATS_address_update (GST_ats,
2047 plugin_name, sender_address, sender_address_len,
2048 session, ats, ats_count);
2050 was_connected = is_connected(n);
2051 change_state (n, S_CONNECTED);
2053 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
2055 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2056 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2057 &neighbour_keepalive_task,
2062 neighbours_connected++;
2063 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2069 GNUNET_i2s (&n->id), n->plugin_name,
2070 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
2073 n->session, __LINE__);
2075 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2080 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2083 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2084 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2085 q_msg.quota = n->bandwidth_out;
2086 q_msg.peer = (*peer);
2087 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2091 struct BlackListCheckContext
2093 struct GNUNET_ATS_Information *ats;
2097 struct Session *session;
2099 char *sender_address;
2101 uint16_t sender_address_len;
2105 struct GNUNET_TIME_Absolute ts;
2110 handle_connect_blacklist_cont (void *cls,
2111 const struct GNUNET_PeerIdentity
2114 struct NeighbourMapEntry *n;
2115 struct BlackListCheckContext * bcc = cls;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "Blacklist check due to CONNECT message: `%s'\n", GNUNET_i2s (peer), (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2123 if (GNUNET_OK != result)
2129 n = lookup_neighbour (peer);
2131 n = setup_neighbour (peer);
2133 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2135 if (NULL != bcc->session)
2136 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2138 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
2141 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address, bcc->sender_address_len),
2143 GNUNET_ATS_address_update (GST_ats,
2145 bcc->plugin_name, bcc->sender_address, bcc->sender_address_len,
2146 bcc->session, bcc->ats, bcc->ats_count);
2147 n->connect_ts = bcc->ts;
2152 if (n->state != S_NOT_CONNECTED)
2154 change_state (n, S_CONNECT_RECV);
2156 /* Ask ATS for an address to connect via that address */
2157 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2158 GNUNET_SCHEDULER_cancel(n->ats_suggest);
2159 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
2160 GNUNET_ATS_suggest_address(GST_ats, peer);
2164 * We received a 'SESSION_CONNECT' message from the other peer.
2165 * Consider switching to it.
2167 * @param message possibly a 'struct SessionConnectMessage' (check format)
2168 * @param peer identity of the peer to switch the address for
2169 * @param plugin_name name of transport that delivered the PONG
2170 * @param address address of the other peer, NULL if other peer
2172 * @param address_len number of bytes in address
2173 * @param session session to use (or NULL)
2174 * @param ats performance data
2175 * @param ats_count number of entries in ats (excluding 0-termination)
2178 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2179 const struct GNUNET_PeerIdentity *peer,
2180 const char *plugin_name,
2181 const char *sender_address, uint16_t sender_address_len,
2182 struct Session *session,
2183 const struct GNUNET_ATS_Information *ats,
2186 const struct SessionConnectMessage *scm;
2187 struct NeighbourMapEntry * n;
2188 struct BlackListCheckContext * bcc = NULL;
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2195 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2197 GNUNET_break_op (0);
2201 scm = (const struct SessionConnectMessage *) message;
2202 GNUNET_break_op (ntohl (scm->reserved) == 0);
2204 n = lookup_neighbour(peer);
2207 /* connected peer switches addresses */
2208 if (is_connected(n))
2210 GNUNET_ATS_address_update(GST_ats, peer, plugin_name, sender_address, sender_address_len, session, ats, ats_count);
2215 /* we are not connected to this peer */
2216 /* do blacklist check*/
2217 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2218 sizeof (struct GNUNET_ATS_Information) * ats_count +
2219 sender_address_len +
2220 strlen (plugin_name)+1);
2222 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2224 bcc->ats_count = ats_count;
2225 bcc->sender_address_len = sender_address_len;
2226 bcc->session = session;
2228 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2229 memcpy (bcc->ats, ats,sizeof (struct GNUNET_ATS_Information) * ats_count );
2231 bcc->sender_address = (char *) &bcc->ats[ats_count];
2232 memcpy (bcc->sender_address, sender_address , sender_address_len);
2234 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2235 strcpy (bcc->plugin_name, plugin_name);
2237 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont, bcc);
2241 /* end of file gnunet-service-transport_neighbours.c */