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 GNUNET_assert (plugin_name != NULL);
583 papi = GST_plugins_find (plugin_name);
587 cont (cont_cls, target, GNUNET_SYSERR);
588 return GNUNET_SYSERR;
591 ret = papi->send (papi->cls,
604 cont (cont_cls, target, GNUNET_SYSERR);
610 * Task invoked to start a transmission to another peer.
612 * @param cls the 'struct NeighbourMapEntry'
613 * @param tc scheduler context
616 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
620 * We're done with our transmission attempt, continue processing.
622 * @param cls the 'struct MessageQueue' of the message
623 * @param receiver intended receiver
624 * @param success whether it worked or not
627 transmit_send_continuation (void *cls,
628 const struct GNUNET_PeerIdentity *receiver,
631 struct MessageQueue *mq;
632 struct NeighbourMapEntry *n;
638 GNUNET_assert (n->is_active == mq);
640 if (success == GNUNET_YES)
642 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
643 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
648 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
649 (success == GNUNET_OK) ? "successful" : "FAILED");
651 if (NULL != mq->cont)
652 mq->cont (mq->cont_cls, success);
658 * Check the ready list for the given neighbour and if a plugin is
659 * ready for transmission (and if we have a message), do so!
661 * @param n target peer for which to transmit
664 try_transmission_to_peer (struct NeighbourMapEntry *n)
666 struct MessageQueue *mq;
667 struct GNUNET_TIME_Relative timeout;
670 if (n->is_active != NULL)
673 return; /* transmission already pending */
675 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
678 return; /* currently waiting for bandwidth */
680 while (NULL != (mq = n->messages_head))
682 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
683 if (timeout.rel_value > 0)
685 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
688 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
691 return; /* no more messages */
693 if (GST_plugins_find (n->plugin_name) == NULL)
698 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
702 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
705 GNUNET_i2s (&n->id));
706 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
707 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
708 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
712 ret = send_with_plugin (&n->id,
713 mq->message_buf, mq->message_buf_size, 0,
715 n->session, n->plugin_name, n->addr, n->addrlen,
717 &transmit_send_continuation, mq);
720 /* failure, but 'send' would not call continuation in this case,
721 * so we need to do it here! */
722 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
729 * Task invoked to start a transmission to another peer.
731 * @param cls the 'struct NeighbourMapEntry'
732 * @param tc scheduler context
735 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
737 struct NeighbourMapEntry *n = cls;
738 GNUNET_assert (NULL != lookup_neighbour(&n->id));
739 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
740 try_transmission_to_peer (n);
745 * Initialize the neighbours subsystem.
747 * @param cls closure for callbacks
748 * @param connect_cb function to call if we connect to a peer
749 * @param disconnect_cb function to call if we disconnect from a peer
752 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
753 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
756 connect_notify_cb = connect_cb;
757 disconnect_notify_cb = disconnect_cb;
758 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
763 send_disconnect_cont (void *cls,
764 const struct GNUNET_PeerIdentity * target,
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s': %i\n",
769 GNUNET_i2s (target), result);
775 send_disconnect (const struct GNUNET_PeerIdentity * target,
776 const char *plugin_name,
777 const char *sender_address, uint16_t sender_address_len,
778 struct Session *session)
781 struct SessionDisconnectMessage disconnect_msg;
784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s'\n",
785 GNUNET_i2s (target));
788 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
789 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
790 disconnect_msg.reserved = htonl (0);
791 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
792 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
793 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
794 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
795 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
796 disconnect_msg.public_key = GST_my_public_key;
797 GNUNET_assert (GNUNET_OK ==
798 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
799 &disconnect_msg.purpose,
800 &disconnect_msg.signature));
802 ret = send_with_plugin(target,
803 (const char *) &disconnect_msg, sizeof (disconnect_msg),
804 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
805 session, plugin_name, sender_address, sender_address_len,
806 GNUNET_YES, &send_disconnect_cont, NULL);
808 if (ret == GNUNET_SYSERR)
809 return GNUNET_SYSERR;
811 GNUNET_STATISTICS_update (GST_stats,
812 gettext_noop ("# peers disconnected due to external request"), 1,
818 * Disconnect from the given neighbour, clean up the record.
820 * @param n neighbour to disconnect from
823 disconnect_neighbour (struct NeighbourMapEntry *n)
825 struct MessageQueue *mq;
826 int was_connected = is_connected(n);
828 /* send DISCONNECT MESSAGE */
829 if (is_connected(n) || is_connecting(n))
831 if (GNUNET_OK == send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session))
832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
833 GNUNET_i2s (&n->id));
835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send DISCONNECT_MSG to `%s'\n",
836 GNUNET_i2s (&n->id));
840 if (is_disconnecting(n))
842 change_state (n, S_DISCONNECT);
844 while (NULL != (mq = n->messages_head))
846 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
847 if (NULL != mq->cont)
848 mq->cont (mq->cont_cls, GNUNET_SYSERR);
851 if (NULL != n->is_active)
853 n->is_active->n = NULL;
858 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
859 GNUNET_SCHEDULER_cancel (n->keepalive_task);
860 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
861 GNUNET_assert (neighbours_connected > 0);
862 neighbours_connected--;
863 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
865 disconnect_notify_cb (callback_cls, &n->id);
867 GNUNET_assert (GNUNET_YES ==
868 GNUNET_CONTAINER_multihashmap_remove (neighbours,
869 &n->id.hashPubKey, n));
870 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
872 GNUNET_SCHEDULER_cancel (n->ats_suggest);
873 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
875 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
877 GNUNET_SCHEDULER_cancel (n->timeout_task);
878 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
880 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
882 GNUNET_SCHEDULER_cancel (n->transmission_task);
883 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
885 if (NULL != n->plugin_name)
887 GNUNET_free (n->plugin_name);
888 n->plugin_name = NULL;
892 GNUNET_free (n->addr);
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
898 GNUNET_i2s (&n->id), n);
904 * Peer has been idle for too long. Disconnect.
906 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
907 * @param tc scheduler context
910 neighbour_timeout_task (void *cls,
911 const struct GNUNET_SCHEDULER_TaskContext *tc)
913 struct NeighbourMapEntry *n = cls;
915 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
917 GNUNET_STATISTICS_update (GST_stats,
918 gettext_noop ("# peers disconnected due to timeout"), 1,
920 disconnect_neighbour (n);
925 * Send another keepalive message.
927 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
928 * @param tc scheduler context
931 neighbour_keepalive_task (void *cls,
932 const struct GNUNET_SCHEDULER_TaskContext *tc)
934 struct NeighbourMapEntry *n = cls;
935 struct GNUNET_MessageHeader m;
937 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
938 &neighbour_keepalive_task,
940 GNUNET_assert (is_connected(n));
941 GNUNET_STATISTICS_update (GST_stats,
942 gettext_noop ("# keepalives sent"), 1,
944 m.size = htons (sizeof (struct GNUNET_MessageHeader));
945 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
947 send_with_plugin(&n->id, (const void *) &m,
949 UINT32_MAX /* priority */ ,
950 GNUNET_TIME_UNIT_FOREVER_REL,
951 n->session, n->plugin_name, n->addr, n->addrlen,
952 GNUNET_YES, NULL, NULL);
957 * Disconnect from the given neighbour.
960 * @param key hash of neighbour's public key (not used)
961 * @param value the 'struct NeighbourMapEntry' of the neighbour
964 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
966 struct NeighbourMapEntry *n = value;
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
970 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
973 GNUNET_STATISTICS_update (GST_stats,
974 gettext_noop ("# peers disconnected due to global disconnect"), 1,
976 disconnect_neighbour (n);
982 ats_suggest_cancel (void *cls,
983 const struct GNUNET_SCHEDULER_TaskContext *tc)
985 struct NeighbourMapEntry *n = cls;
987 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
990 " ATS did not suggested address to connect to peer `%s'\n",
991 GNUNET_i2s (&n->id));
993 disconnect_neighbour(n);
998 * Cleanup the neighbours subsystem.
1001 GST_neighbours_stop ()
1003 GNUNET_assert (neighbours != NULL);
1005 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1007 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1008 GNUNET_assert (neighbours_connected == 0);
1010 callback_cls = NULL;
1011 connect_notify_cb = NULL;
1012 disconnect_notify_cb = NULL;
1017 * We tried to send a SESSION_CONNECT message to another peer. If this
1018 * succeeded, we change the state. If it failed, we should tell
1019 * ATS to not use this address anymore (until it is re-validated).
1021 * @param cls the 'struct NeighbourMapEntry'
1022 * @param success GNUNET_OK on success
1025 send_connect_continuation (void *cls,
1026 const struct GNUNET_PeerIdentity * target,
1030 struct NeighbourMapEntry *n = cls;
1032 GNUNET_assert (n != NULL);
1033 GNUNET_assert (!is_connected(n));
1035 if (is_disconnecting(n))
1036 return; /* neighbour is going away */
1038 if (GNUNET_YES != success)
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1043 GNUNET_i2s (&n->id), n->plugin_name,
1044 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1050 GNUNET_ATS_address_destroyed (GST_ats,
1057 change_state(n, S_NOT_CONNECTED);
1059 if (n->ats_suggest!= GNUNET_SCHEDULER_NO_TASK)
1060 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1061 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1062 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1071 * We tried to switch addresses with an peer already connected. If it failed,
1072 * we should tell ATS to not use this address anymore (until it is re-validated).
1074 * @param cls the 'struct NeighbourMapEntry'
1075 * @param success GNUNET_OK on success
1078 send_switch_address_continuation (void *cls,
1079 const struct GNUNET_PeerIdentity * target,
1083 struct NeighbourMapEntry *n = cls;
1085 GNUNET_assert (n != NULL);
1086 if (is_disconnecting(n))
1087 return; /* neighbour is going away */
1089 GNUNET_assert (n->state == S_CONNECTED);
1090 if (GNUNET_YES != success)
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1095 GNUNET_i2s (&n->id), n->plugin_name,
1096 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1102 GNUNET_ATS_address_destroyed (GST_ats,
1109 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1110 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1111 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1112 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1118 * We tried to send a SESSION_CONNECT message to another peer. If this
1119 * succeeded, we change the state. If it failed, we should tell
1120 * ATS to not use this address anymore (until it is re-validated).
1122 * @param cls the 'struct NeighbourMapEntry'
1123 * @param success GNUNET_OK on success
1126 send_connect_ack_continuation (void *cls,
1127 const struct GNUNET_PeerIdentity * target,
1131 struct NeighbourMapEntry *n = cls;
1133 GNUNET_assert (n != NULL);
1135 if (GNUNET_YES == success)
1136 return; /* sending successful */
1138 /* sending failed, ask for next address */
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1142 GNUNET_i2s (&n->id), n->plugin_name,
1143 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1148 change_state(n, S_NOT_CONNECTED);
1150 GNUNET_ATS_address_destroyed (GST_ats,
1157 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1158 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1159 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1160 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1164 * For an existing neighbour record, set the active connection to
1165 * the given address.
1167 * @param peer identity of the peer to switch the address for
1168 * @param plugin_name name of transport that delivered the PONG
1169 * @param address address of the other peer, NULL if other peer
1171 * @param address_len number of bytes in address
1172 * @param session session to use (or NULL)
1173 * @param ats performance data
1174 * @param ats_count number of entries in ats
1175 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1176 * connection is not up (yet)
1179 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1180 const char *plugin_name, const void *address,
1181 size_t address_len, struct Session *session,
1182 const struct GNUNET_ATS_Information
1183 *ats, uint32_t ats_count,
1184 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1185 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1187 struct NeighbourMapEntry *n;
1188 struct SessionConnectMessage connect_msg;
1192 GNUNET_assert (neighbours != NULL);
1193 n = lookup_neighbour (peer);
1196 if (NULL == session)
1197 GNUNET_ATS_address_destroyed (GST_ats,
1199 plugin_name, address,
1204 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1206 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1207 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1212 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1214 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1217 session, (is_connected(n) ? "CONNECTED" : "NOT CONNECTED"),
1221 // do not switch addresses just update quotas
1224 if ((is_connected(n)) && (address_len == n->addrlen))
1226 if ((0 == memcmp (address, n->addr, address_len)) &&
1227 (n->session == session))
1229 struct QuotaSetMessage q_msg;
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Sending outbound quota of %u Bps and inbound quota of % Bps for peer `%s' to all clients\n",
1234 ntohl (n->bandwidth_out.value__),
1235 ntohl (n->bandwidth_in.value__),
1239 n->bandwidth_in = bandwidth_in;
1240 n->bandwidth_out = bandwidth_out;
1241 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1243 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1244 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1245 q_msg.quota = n->bandwidth_out;
1246 q_msg.peer = (*peer);
1247 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1253 GNUNET_free_non_null (n->addr);
1254 n->addr = GNUNET_malloc (address_len);
1255 memcpy (n->addr, address, address_len);
1256 n->bandwidth_in = bandwidth_in;
1257 n->bandwidth_out = bandwidth_out;
1258 n->addrlen = address_len;
1259 n->session = session;
1260 GNUNET_free_non_null (n->plugin_name);
1261 n->plugin_name = GNUNET_strdup (plugin_name);
1262 GNUNET_SCHEDULER_cancel (n->timeout_task);
1264 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1265 &neighbour_timeout_task, n);
1267 if (n->state == S_DISCONNECT)
1269 /* We are disconnecting, nothing to do here */
1272 /* We are not connected/connecting and initiate a fresh connect */
1273 if (n->state == S_NOT_CONNECTED)
1275 msg_len = sizeof (struct SessionConnectMessage);
1276 connect_msg.header.size = htons (msg_len);
1277 connect_msg.header.type =
1278 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1279 connect_msg.reserved = htonl (0);
1280 connect_msg.timestamp =
1281 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1283 change_state (n, S_CONNECT_SENT);
1285 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1286 session, plugin_name, address, address_len,
1287 GNUNET_YES, &send_connect_continuation, n);
1291 /* We received a CONNECT message and asked ATS for an address */
1292 else if (n->state == S_CONNECT_RECV)
1294 msg_len = sizeof (struct SessionConnectMessage);
1295 connect_msg.header.size = htons (msg_len);
1296 connect_msg.header.type =
1297 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1298 connect_msg.reserved = htonl (0);
1299 connect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1301 ret = send_with_plugin(&n->id, (const void *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1302 session, plugin_name, address, address_len,
1303 GNUNET_YES, &send_connect_ack_continuation, n);
1306 /* connected peer is switching addresses */
1307 else if (n->state == S_CONNECTED)
1309 msg_len = sizeof (struct SessionConnectMessage);
1310 connect_msg.header.size = htons (msg_len);
1311 connect_msg.header.type =
1312 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1313 connect_msg.reserved = htonl (0);
1314 connect_msg.timestamp =
1315 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1317 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1318 session, plugin_name, address, address_len,
1319 GNUNET_YES, &send_switch_address_continuation, n);
1320 if (ret == GNUNET_SYSERR)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1323 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1324 GNUNET_i2s (peer), plugin_name,
1325 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1332 else if (n->state == S_CONNECT_SENT)
1336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid connection state to switch addresses %u \n", n->state);
1337 GNUNET_break_op (0);
1343 * Create an entry in the neighbour map for the given peer
1345 * @param peer peer to create an entry for
1346 * @return new neighbour map entry
1348 static struct NeighbourMapEntry *
1349 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1351 struct NeighbourMapEntry *n;
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Unknown peer `%s', creating new neighbour\n",
1358 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1360 n->state = S_NOT_CONNECTED;
1361 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1362 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1363 MAX_BANDWIDTH_CARRY_S);
1365 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1366 &neighbour_timeout_task, n);
1367 GNUNET_assert (GNUNET_OK ==
1368 GNUNET_CONTAINER_multihashmap_put (neighbours,
1369 &n->id.hashPubKey, n,
1370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1376 * Try to create a connection to the given target (eventually).
1378 * @param target peer to try to connect to
1381 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1383 struct NeighbourMapEntry *n;
1385 GNUNET_assert (neighbours != NULL);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1388 GNUNET_i2s (target));
1391 memcmp (target, &GST_my_identity,
1392 sizeof (struct GNUNET_PeerIdentity)));
1393 n = lookup_neighbour (target);
1397 if ((is_connected(n)) || (is_connecting(n)))
1398 return; /* already connecting or connected */
1399 if (is_disconnecting(n))
1400 change_state (n, S_NOT_CONNECTED);
1405 n = setup_neighbour (target);
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1408 "Asking ATS for suggested address to connect to peer `%s'\n",
1409 GNUNET_i2s (&n->id));
1412 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1416 * Test if we're connected to the given peer.
1418 * @param target peer to test
1419 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1422 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1424 struct NeighbourMapEntry *n;
1426 GNUNET_assert (neighbours != NULL);
1428 n = lookup_neighbour (target);
1430 if ((NULL == n) || (!is_connected(n)))
1431 return GNUNET_NO; /* not connected */
1437 * A session was terminated. Take note.
1439 * @param peer identity of the peer where the session died
1440 * @param session session that is gone
1443 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1444 struct Session *session)
1446 struct NeighbourMapEntry *n;
1448 GNUNET_assert (neighbours != NULL);
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1452 "Session %X to peer `%s' ended \n",
1453 session, GNUNET_i2s (peer));
1456 n = lookup_neighbour (peer);
1459 if (session != n->session)
1460 return; /* doesn't affect us */
1463 GNUNET_free (n->addr);
1467 /* not connected anymore anyway, shouldn't matter */
1468 if ((!is_connected(n)) && (!is_connecting(n)))
1471 /* We are connected, so ask ATS to switch addresses */
1472 GNUNET_SCHEDULER_cancel (n->timeout_task);
1474 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1475 &neighbour_timeout_task, n);
1476 /* try QUICKLY to re-establish a connection, reduce timeout! */
1477 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1478 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1479 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1480 GNUNET_ATS_suggest_address (GST_ats, peer);
1485 * Transmit a message to the given target using the active connection.
1487 * @param target destination
1488 * @param msg message to send
1489 * @param msg_size number of bytes in msg
1490 * @param timeout when to fail with timeout
1491 * @param cont function to call when done
1492 * @param cont_cls closure for 'cont'
1495 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1496 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1497 GST_NeighbourSendContinuation cont, void *cont_cls)
1499 struct NeighbourMapEntry *n;
1500 struct MessageQueue *mq;
1502 GNUNET_assert (neighbours != NULL);
1504 n = lookup_neighbour (target);
1505 if ((n == NULL) || (!is_connected(n)))
1507 GNUNET_STATISTICS_update (GST_stats,
1509 ("# messages not sent (no such peer or not connected)"),
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Could not send message to peer `%s': unknown neighbour",
1515 GNUNET_i2s (target));
1516 else if (!is_connected(n))
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1518 "Could not send message to peer `%s': not connected\n",
1519 GNUNET_i2s (target));
1522 cont (cont_cls, GNUNET_SYSERR);
1526 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
1528 GNUNET_STATISTICS_update (GST_stats,
1530 ("# messages not sent (no such peer or not connected)"),
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "Could not send message to peer `%s': no address available\n",
1535 GNUNET_i2s (target));
1539 cont (cont_cls, GNUNET_SYSERR);
1543 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1544 GNUNET_STATISTICS_update (GST_stats,
1546 ("# bytes in message queue for other peers"),
1547 msg_size, GNUNET_NO);
1548 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1550 mq->cont_cls = cont_cls;
1551 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1552 memcpy (&mq[1], msg, msg_size);
1553 mq->message_buf = (const char *) &mq[1];
1554 mq->message_buf_size = msg_size;
1555 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1556 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1558 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1559 (NULL == n->is_active))
1560 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1565 * We have received a message from the given sender. How long should
1566 * we delay before receiving more? (Also used to keep the peer marked
1569 * @param sender sender of the message
1570 * @param size size of the message
1571 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1572 * GNUNET_NO if the neighbour is not connected or violates the quota,
1573 * GNUNET_SYSERR if the connection is not fully up yet
1574 * @return how long to wait before reading more from this sender
1576 struct GNUNET_TIME_Relative
1577 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1578 *sender, ssize_t size, int *do_forward)
1580 struct NeighbourMapEntry *n;
1581 struct GNUNET_TIME_Relative ret;
1583 GNUNET_assert (neighbours != NULL);
1585 n = lookup_neighbour (sender);
1588 GST_neighbours_try_connect (sender);
1589 n = lookup_neighbour (sender);
1592 GNUNET_STATISTICS_update (GST_stats,
1594 ("# messages discarded due to lack of neighbour record"),
1596 *do_forward = GNUNET_NO;
1597 return GNUNET_TIME_UNIT_ZERO;
1600 if (!is_connected(n))
1602 *do_forward = GNUNET_SYSERR;
1603 return GNUNET_TIME_UNIT_ZERO;
1605 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1607 n->quota_violation_count++;
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1611 n->in_tracker.available_bytes_per_s__,
1612 n->quota_violation_count);
1614 /* Discount 32k per violation */
1615 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1619 if (n->quota_violation_count > 0)
1621 /* try to add 32k back */
1622 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1623 n->quota_violation_count--;
1626 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1628 GNUNET_STATISTICS_update (GST_stats,
1630 ("# bandwidth quota violations by other peers"),
1632 *do_forward = GNUNET_NO;
1633 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1635 *do_forward = GNUNET_YES;
1636 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1637 if (ret.rel_value > 0)
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1641 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1642 (unsigned long long) n->in_tracker.
1643 consumption_since_last_update__,
1644 (unsigned int) n->in_tracker.available_bytes_per_s__,
1645 (unsigned long long) ret.rel_value);
1647 GNUNET_STATISTICS_update (GST_stats,
1648 gettext_noop ("# ms throttling suggested"),
1649 (int64_t) ret.rel_value, GNUNET_NO);
1656 * Keep the connection to the given neighbour alive longer,
1657 * we received a KEEPALIVE (or equivalent).
1659 * @param neighbour neighbour to keep alive
1662 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1664 struct NeighbourMapEntry *n;
1666 GNUNET_assert (neighbours != NULL);
1668 n = lookup_neighbour (neighbour);
1671 GNUNET_STATISTICS_update (GST_stats,
1673 ("# KEEPALIVE messages discarded (not connected)"),
1677 GNUNET_SCHEDULER_cancel (n->timeout_task);
1679 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1680 &neighbour_timeout_task, n);
1685 * Change the incoming quota for the given peer.
1687 * @param neighbour identity of peer to change qutoa for
1688 * @param quota new quota
1691 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1692 struct GNUNET_BANDWIDTH_Value32NBO quota)
1694 struct NeighbourMapEntry *n;
1696 GNUNET_assert (neighbours != NULL);
1698 n = lookup_neighbour (neighbour);
1701 GNUNET_STATISTICS_update (GST_stats,
1703 ("# SET QUOTA messages ignored (no such peer)"),
1707 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1708 if (0 != ntohl (quota.value__))
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1712 GNUNET_i2s (&n->id), "SET_QUOTA");
1714 if (is_connected(n))
1715 GNUNET_STATISTICS_update (GST_stats,
1716 gettext_noop ("# disconnects due to quota of 0"), 1,
1718 disconnect_neighbour (n);
1723 * Closure for the neighbours_iterate function.
1725 struct IteratorContext
1728 * Function to call on each connected neighbour.
1730 GST_NeighbourIterator cb;
1740 * Call the callback from the closure for each connected neighbour.
1742 * @param cls the 'struct IteratorContext'
1743 * @param key the hash of the public key of the neighbour
1744 * @param value the 'struct NeighbourMapEntry'
1745 * @return GNUNET_OK (continue to iterate)
1748 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1750 struct IteratorContext *ic = cls;
1751 struct NeighbourMapEntry *n = value;
1753 if (!is_connected(n))
1756 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1762 * Iterate over all connected neighbours.
1764 * @param cb function to call
1765 * @param cb_cls closure for cb
1768 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1770 struct IteratorContext ic;
1772 GNUNET_assert (neighbours != NULL);
1776 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1780 * If we have an active connection to the given target, it must be shutdown.
1782 * @param target peer to disconnect from
1785 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1787 struct NeighbourMapEntry *n;
1789 GNUNET_assert (neighbours != NULL);
1791 n = lookup_neighbour (target);
1793 return; /* not active */
1794 if (is_connected(n))
1796 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1798 n = lookup_neighbour (target);
1800 return; /* gone already */
1802 disconnect_neighbour (n);
1807 * We received a disconnect message from the given peer,
1808 * validate and process.
1810 * @param peer sender of the message
1811 * @param msg the disconnect message
1814 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1815 const struct GNUNET_MessageHeader *msg)
1817 struct NeighbourMapEntry *n;
1818 const struct SessionDisconnectMessage *sdm;
1822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 "Received DISCONNECT message from peer `%s'\n", GNUNET_i2s (peer));
1826 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1828 // GNUNET_break_op (0);
1829 GNUNET_STATISTICS_update (GST_stats,
1830 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1834 sdm = (const struct SessionDisconnectMessage* ) msg;
1835 n = lookup_neighbour (peer);
1837 return; /* gone already */
1838 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1839 n->connect_ts.abs_value)
1841 GNUNET_STATISTICS_update (GST_stats,
1842 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1846 GNUNET_CRYPTO_hash (&sdm->public_key,
1847 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1849 if (0 != memcmp (peer,
1851 sizeof (struct GNUNET_PeerIdentity)))
1853 GNUNET_break_op (0);
1856 if (ntohl (sdm->purpose.size) !=
1857 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1858 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1859 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1861 GNUNET_break_op (0);
1865 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1870 GNUNET_break_op (0);
1873 GST_neighbours_force_disconnect (peer);
1877 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1878 * Consider switching to it.
1880 * @param message possibly a 'struct SessionConnectMessage' (check format)
1881 * @param peer identity of the peer to switch the address for
1882 * @param plugin_name name of transport that delivered the PONG
1883 * @param address address of the other peer, NULL if other peer
1885 * @param address_len number of bytes in address
1886 * @param session session to use (or NULL)
1887 * @param ats performance data
1888 * @param ats_count number of entries in ats
1891 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1892 const struct GNUNET_PeerIdentity *peer,
1893 const char *plugin_name,
1894 const char *sender_address, uint16_t sender_address_len,
1895 struct Session *session,
1896 const struct GNUNET_ATS_Information *ats,
1899 const struct SessionConnectMessage *scm;
1900 struct QuotaSetMessage q_msg;
1901 struct GNUNET_MessageHeader msg;
1902 struct NeighbourMapEntry *n;
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "Received CONNECT_ACK message from peer `%s'\n", GNUNET_i2s (peer));
1912 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1914 GNUNET_break_op (0);
1918 scm = (const struct SessionConnectMessage *) message;
1919 GNUNET_break_op (ntohl (scm->reserved) == 0);
1920 n = lookup_neighbour (peer);
1922 n = setup_neighbour (peer);
1924 if (n->state != S_CONNECT_SENT)
1927 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1931 if (NULL != session)
1932 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1934 "Giving ATS session %p of plugin %s for peer %s\n",
1938 GNUNET_ATS_address_update (GST_ats,
1940 plugin_name, sender_address, sender_address_len,
1941 session, ats, ats_count);
1943 was_connected = is_connected(n);
1944 if (!is_connected(n))
1945 change_state (n, S_CONNECTED);
1948 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1949 "Setting inbound quota of %u for peer `%s' to \n",
1950 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
1952 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1955 msg_len = sizeof (msg);
1956 msg.size = htons (msg_len);
1957 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
1959 ret = send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
1960 GNUNET_TIME_UNIT_FOREVER_REL,
1961 n->session, n->plugin_name, n->addr, n->addrlen,
1962 GNUNET_YES, NULL, NULL);
1964 if (ret == GNUNET_SYSERR)
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1966 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
1967 GNUNET_i2s (&n->id), n->plugin_name,
1968 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1976 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1977 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1978 &neighbour_keepalive_task,
1981 neighbours_connected++;
1982 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
1987 GNUNET_i2s (&n->id), n->plugin_name,
1988 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1991 n->session, __LINE__);
1993 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1998 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1999 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2001 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2002 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2003 q_msg.quota = n->bandwidth_out;
2004 q_msg.peer = (*peer);
2005 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2010 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2011 const struct GNUNET_PeerIdentity *peer,
2012 const char *plugin_name,
2013 const char *sender_address, uint16_t sender_address_len,
2014 struct Session *session,
2015 const struct GNUNET_ATS_Information *ats,
2018 struct NeighbourMapEntry *n;
2019 struct QuotaSetMessage q_msg;
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 "Received ACK message from peer `%s'\n", GNUNET_i2s (peer));
2027 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2029 GNUNET_break_op (0);
2033 n = lookup_neighbour (peer);
2036 send_disconnect(peer, plugin_name, sender_address, sender_address_len, session);
2041 if (is_connected(n))
2044 if (NULL != session)
2045 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2047 "Giving ATS session %p of plugin %s for peer %s\n",
2051 GNUNET_ATS_address_update (GST_ats,
2053 plugin_name, sender_address, sender_address_len,
2054 session, ats, ats_count);
2056 was_connected = is_connected(n);
2057 change_state (n, S_CONNECTED);
2059 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
2061 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2062 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2063 &neighbour_keepalive_task,
2068 neighbours_connected++;
2069 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2074 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2075 GNUNET_i2s (&n->id), n->plugin_name,
2076 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
2079 n->session, __LINE__);
2081 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2086 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2089 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2090 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2091 q_msg.quota = n->bandwidth_out;
2092 q_msg.peer = (*peer);
2093 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2097 struct BlackListCheckContext
2099 struct GNUNET_ATS_Information *ats;
2103 struct Session *session;
2105 char *sender_address;
2107 uint16_t sender_address_len;
2111 struct GNUNET_TIME_Absolute ts;
2116 handle_connect_blacklist_cont (void *cls,
2117 const struct GNUNET_PeerIdentity
2120 struct NeighbourMapEntry *n;
2121 struct BlackListCheckContext * bcc = cls;
2124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2125 "Blacklist check due to CONNECT message: `%s'\n", GNUNET_i2s (peer), (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2129 if (GNUNET_OK != result)
2135 n = lookup_neighbour (peer);
2137 n = setup_neighbour (peer);
2139 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2141 if (NULL != bcc->session)
2142 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2144 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
2147 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address, bcc->sender_address_len),
2149 GNUNET_ATS_address_update (GST_ats,
2151 bcc->plugin_name, bcc->sender_address, bcc->sender_address_len,
2152 bcc->session, bcc->ats, bcc->ats_count);
2153 n->connect_ts = bcc->ts;
2158 if (n->state != S_NOT_CONNECTED)
2160 change_state (n, S_CONNECT_RECV);
2162 /* Ask ATS for an address to connect via that address */
2163 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2164 GNUNET_SCHEDULER_cancel(n->ats_suggest);
2165 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
2166 GNUNET_ATS_suggest_address(GST_ats, peer);
2170 * We received a 'SESSION_CONNECT' message from the other peer.
2171 * Consider switching to it.
2173 * @param message possibly a 'struct SessionConnectMessage' (check format)
2174 * @param peer identity of the peer to switch the address for
2175 * @param plugin_name name of transport that delivered the PONG
2176 * @param address address of the other peer, NULL if other peer
2178 * @param address_len number of bytes in address
2179 * @param session session to use (or NULL)
2180 * @param ats performance data
2181 * @param ats_count number of entries in ats (excluding 0-termination)
2184 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2185 const struct GNUNET_PeerIdentity *peer,
2186 const char *plugin_name,
2187 const char *sender_address, uint16_t sender_address_len,
2188 struct Session *session,
2189 const struct GNUNET_ATS_Information *ats,
2192 const struct SessionConnectMessage *scm;
2193 struct NeighbourMapEntry * n;
2194 struct BlackListCheckContext * bcc = NULL;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2201 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2203 GNUNET_break_op (0);
2207 scm = (const struct SessionConnectMessage *) message;
2208 GNUNET_break_op (ntohl (scm->reserved) == 0);
2210 n = lookup_neighbour(peer);
2213 /* connected peer switches addresses */
2214 if (is_connected(n))
2216 GNUNET_ATS_address_update(GST_ats, peer, plugin_name, sender_address, sender_address_len, session, ats, ats_count);
2221 /* we are not connected to this peer */
2222 /* do blacklist check*/
2223 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2224 sizeof (struct GNUNET_ATS_Information) * ats_count +
2225 sender_address_len +
2226 strlen (plugin_name)+1);
2228 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2230 bcc->ats_count = ats_count;
2231 bcc->sender_address_len = sender_address_len;
2232 bcc->session = session;
2234 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2235 memcpy (bcc->ats, ats,sizeof (struct GNUNET_ATS_Information) * ats_count );
2237 bcc->sender_address = (char *) &bcc->ats[ats_count];
2238 memcpy (bcc->sender_address, sender_address , sender_address_len);
2240 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2241 strcpy (bcc->plugin_name, plugin_name);
2243 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont, bcc);
2247 /* end of file gnunet-service-transport_neighbours.c */