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 /* FIXME : ats returns an address with all values 0 */
583 if (((plugin_name == NULL) && (addr == NULL) && (addrlen == 0 )) ||
584 ((plugin_name == NULL) && (session == NULL)) ||
585 ((plugin_name == NULL) && (addr == NULL) && (addrlen == 0 ) && (force_address == GNUNET_YES)))
588 cont (cont_cls, target, GNUNET_SYSERR);
589 return GNUNET_SYSERR;
591 papi = GST_plugins_find (plugin_name);
595 cont (cont_cls, target, GNUNET_SYSERR);
596 return GNUNET_SYSERR;
599 ret = papi->send (papi->cls,
612 cont (cont_cls, target, GNUNET_SYSERR);
618 * Task invoked to start a transmission to another peer.
620 * @param cls the 'struct NeighbourMapEntry'
621 * @param tc scheduler context
624 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
628 * We're done with our transmission attempt, continue processing.
630 * @param cls the 'struct MessageQueue' of the message
631 * @param receiver intended receiver
632 * @param success whether it worked or not
635 transmit_send_continuation (void *cls,
636 const struct GNUNET_PeerIdentity *receiver,
639 struct MessageQueue *mq;
640 struct NeighbourMapEntry *n;
646 GNUNET_assert (n->is_active == mq);
648 if (success == GNUNET_YES)
650 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
651 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
656 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
657 (success == GNUNET_OK) ? "successful" : "FAILED");
659 if (NULL != mq->cont)
660 mq->cont (mq->cont_cls, success);
666 * Check the ready list for the given neighbour and if a plugin is
667 * ready for transmission (and if we have a message), do so!
669 * @param n target peer for which to transmit
672 try_transmission_to_peer (struct NeighbourMapEntry *n)
674 struct MessageQueue *mq;
675 struct GNUNET_TIME_Relative timeout;
678 if (n->is_active != NULL)
681 return; /* transmission already pending */
683 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
686 return; /* currently waiting for bandwidth */
688 while (NULL != (mq = n->messages_head))
690 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
691 if (timeout.rel_value > 0)
693 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
696 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
699 return; /* no more messages */
701 if (GST_plugins_find (n->plugin_name) == NULL)
706 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
710 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
713 GNUNET_i2s (&n->id));
714 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
715 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
716 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
720 ret = send_with_plugin (&n->id,
721 mq->message_buf, mq->message_buf_size, 0,
723 n->session, n->plugin_name, n->addr, n->addrlen,
725 &transmit_send_continuation, mq);
728 /* failure, but 'send' would not call continuation in this case,
729 * so we need to do it here! */
730 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
737 * Task invoked to start a transmission to another peer.
739 * @param cls the 'struct NeighbourMapEntry'
740 * @param tc scheduler context
743 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
745 struct NeighbourMapEntry *n = cls;
746 GNUNET_assert (NULL != lookup_neighbour(&n->id));
747 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
748 try_transmission_to_peer (n);
753 * Initialize the neighbours subsystem.
755 * @param cls closure for callbacks
756 * @param connect_cb function to call if we connect to a peer
757 * @param disconnect_cb function to call if we disconnect from a peer
760 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
761 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
764 connect_notify_cb = connect_cb;
765 disconnect_notify_cb = disconnect_cb;
766 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
771 send_disconnect_cont (void *cls,
772 const struct GNUNET_PeerIdentity * target,
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s': %i\n",
777 GNUNET_i2s (target), result);
783 send_disconnect (const struct GNUNET_PeerIdentity * target,
784 const char *plugin_name,
785 const char *sender_address, uint16_t sender_address_len,
786 struct Session *session)
789 struct SessionDisconnectMessage disconnect_msg;
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s'\n",
793 GNUNET_i2s (target));
796 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
797 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
798 disconnect_msg.reserved = htonl (0);
799 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
800 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
801 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
802 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
803 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
804 disconnect_msg.public_key = GST_my_public_key;
805 GNUNET_assert (GNUNET_OK ==
806 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
807 &disconnect_msg.purpose,
808 &disconnect_msg.signature));
810 ret = send_with_plugin(target,
811 (const char *) &disconnect_msg, sizeof (disconnect_msg),
812 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
813 session, plugin_name, sender_address, sender_address_len,
814 GNUNET_YES, &send_disconnect_cont, NULL);
816 if (ret == GNUNET_SYSERR)
817 return GNUNET_SYSERR;
819 GNUNET_STATISTICS_update (GST_stats,
820 gettext_noop ("# peers disconnected due to external request"), 1,
826 * Disconnect from the given neighbour, clean up the record.
828 * @param n neighbour to disconnect from
831 disconnect_neighbour (struct NeighbourMapEntry *n)
833 struct MessageQueue *mq;
834 int was_connected = is_connected(n);
836 /* send DISCONNECT MESSAGE */
837 if (is_connected(n) || is_connecting(n))
839 if (GNUNET_OK == send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session))
840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
841 GNUNET_i2s (&n->id));
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send DISCONNECT_MSG to `%s'\n",
844 GNUNET_i2s (&n->id));
848 if (is_disconnecting(n))
850 change_state (n, S_DISCONNECT);
852 while (NULL != (mq = n->messages_head))
854 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
855 if (NULL != mq->cont)
856 mq->cont (mq->cont_cls, GNUNET_SYSERR);
859 if (NULL != n->is_active)
861 n->is_active->n = NULL;
866 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
867 GNUNET_SCHEDULER_cancel (n->keepalive_task);
868 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
869 GNUNET_assert (neighbours_connected > 0);
870 neighbours_connected--;
871 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
873 disconnect_notify_cb (callback_cls, &n->id);
875 GNUNET_assert (GNUNET_YES ==
876 GNUNET_CONTAINER_multihashmap_remove (neighbours,
877 &n->id.hashPubKey, n));
878 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
880 GNUNET_SCHEDULER_cancel (n->ats_suggest);
881 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
883 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
885 GNUNET_SCHEDULER_cancel (n->timeout_task);
886 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
888 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
890 GNUNET_SCHEDULER_cancel (n->transmission_task);
891 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
893 if (NULL != n->plugin_name)
895 GNUNET_free (n->plugin_name);
896 n->plugin_name = NULL;
900 GNUNET_free (n->addr);
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
906 GNUNET_i2s (&n->id), n);
912 * Peer has been idle for too long. Disconnect.
914 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
915 * @param tc scheduler context
918 neighbour_timeout_task (void *cls,
919 const struct GNUNET_SCHEDULER_TaskContext *tc)
921 struct NeighbourMapEntry *n = cls;
923 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
925 GNUNET_STATISTICS_update (GST_stats,
926 gettext_noop ("# peers disconnected due to timeout"), 1,
928 disconnect_neighbour (n);
933 * Send another keepalive message.
935 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
936 * @param tc scheduler context
939 neighbour_keepalive_task (void *cls,
940 const struct GNUNET_SCHEDULER_TaskContext *tc)
942 struct NeighbourMapEntry *n = cls;
943 struct GNUNET_MessageHeader m;
945 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
946 &neighbour_keepalive_task,
948 GNUNET_assert (is_connected(n));
949 GNUNET_STATISTICS_update (GST_stats,
950 gettext_noop ("# keepalives sent"), 1,
952 m.size = htons (sizeof (struct GNUNET_MessageHeader));
953 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
955 send_with_plugin(&n->id, (const void *) &m,
957 UINT32_MAX /* priority */ ,
958 GNUNET_TIME_UNIT_FOREVER_REL,
959 n->session, n->plugin_name, n->addr, n->addrlen,
960 GNUNET_YES, NULL, NULL);
965 * Disconnect from the given neighbour.
968 * @param key hash of neighbour's public key (not used)
969 * @param value the 'struct NeighbourMapEntry' of the neighbour
972 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
974 struct NeighbourMapEntry *n = value;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
978 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
981 GNUNET_STATISTICS_update (GST_stats,
982 gettext_noop ("# peers disconnected due to global disconnect"), 1,
984 disconnect_neighbour (n);
990 ats_suggest_cancel (void *cls,
991 const struct GNUNET_SCHEDULER_TaskContext *tc)
993 struct NeighbourMapEntry *n = cls;
995 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 " ATS did not suggested address to connect to peer `%s'\n",
999 GNUNET_i2s (&n->id));
1001 disconnect_neighbour(n);
1006 * Cleanup the neighbours subsystem.
1009 GST_neighbours_stop ()
1011 // This can happen during shutdown
1012 if (neighbours == NULL)
1017 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1019 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1020 GNUNET_assert (neighbours_connected == 0);
1022 callback_cls = NULL;
1023 connect_notify_cb = NULL;
1024 disconnect_notify_cb = NULL;
1029 * We tried to send a SESSION_CONNECT message to another peer. If this
1030 * succeeded, we change the state. If it failed, we should tell
1031 * ATS to not use this address anymore (until it is re-validated).
1033 * @param cls the 'struct NeighbourMapEntry'
1034 * @param success GNUNET_OK on success
1037 send_connect_continuation (void *cls,
1038 const struct GNUNET_PeerIdentity * target,
1042 struct NeighbourMapEntry *n = cls;
1044 GNUNET_assert (n != NULL);
1045 GNUNET_assert (!is_connected(n));
1047 if (is_disconnecting(n))
1048 return; /* neighbour is going away */
1050 if (GNUNET_YES != success)
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1055 GNUNET_i2s (&n->id), n->plugin_name,
1056 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1062 GNUNET_ATS_address_destroyed (GST_ats,
1069 change_state(n, S_NOT_CONNECTED);
1071 if (n->ats_suggest!= GNUNET_SCHEDULER_NO_TASK)
1072 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1073 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1074 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1083 * We tried to switch addresses with an peer already connected. If it failed,
1084 * we should tell ATS to not use this address anymore (until it is re-validated).
1086 * @param cls the 'struct NeighbourMapEntry'
1087 * @param success GNUNET_OK on success
1090 send_switch_address_continuation (void *cls,
1091 const struct GNUNET_PeerIdentity * target,
1095 struct NeighbourMapEntry *n = cls;
1097 GNUNET_assert (n != NULL);
1098 if (is_disconnecting(n))
1099 return; /* neighbour is going away */
1101 GNUNET_assert (n->state == S_CONNECTED);
1102 if (GNUNET_YES != success)
1105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1106 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1107 GNUNET_i2s (&n->id), n->plugin_name,
1108 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1114 GNUNET_ATS_address_destroyed (GST_ats,
1121 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1122 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1123 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1124 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1130 * We tried to send a SESSION_CONNECT message to another peer. If this
1131 * succeeded, we change the state. If it failed, we should tell
1132 * ATS to not use this address anymore (until it is re-validated).
1134 * @param cls the 'struct NeighbourMapEntry'
1135 * @param success GNUNET_OK on success
1138 send_connect_ack_continuation (void *cls,
1139 const struct GNUNET_PeerIdentity * target,
1143 struct NeighbourMapEntry *n = cls;
1145 GNUNET_assert (n != NULL);
1147 if (GNUNET_YES == success)
1148 return; /* sending successful */
1150 /* sending failed, ask for next address */
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1154 GNUNET_i2s (&n->id), n->plugin_name,
1155 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1160 change_state(n, S_NOT_CONNECTED);
1162 GNUNET_ATS_address_destroyed (GST_ats,
1169 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1170 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1171 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1172 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1176 * For an existing neighbour record, set the active connection to
1177 * the given address.
1179 * @param peer identity of the peer to switch the address for
1180 * @param plugin_name name of transport that delivered the PONG
1181 * @param address address of the other peer, NULL if other peer
1183 * @param address_len number of bytes in address
1184 * @param session session to use (or NULL)
1185 * @param ats performance data
1186 * @param ats_count number of entries in ats
1187 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1188 * connection is not up (yet)
1191 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1192 const char *plugin_name, const void *address,
1193 size_t address_len, struct Session *session,
1194 const struct GNUNET_ATS_Information
1195 *ats, uint32_t ats_count,
1196 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1197 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1199 struct NeighbourMapEntry *n;
1200 struct SessionConnectMessage connect_msg;
1204 // This can happen during shutdown
1205 if (neighbours == NULL)
1209 n = lookup_neighbour (peer);
1212 if (NULL == session)
1213 GNUNET_ATS_address_destroyed (GST_ats,
1215 plugin_name, address,
1220 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1222 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1223 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1230 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1233 session, (is_connected(n) ? "CONNECTED" : "NOT CONNECTED"),
1237 // do not switch addresses just update quotas
1240 if ((is_connected(n)) && (address_len == n->addrlen))
1242 if ((0 == memcmp (address, n->addr, address_len)) &&
1243 (n->session == session))
1245 struct QuotaSetMessage q_msg;
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Sending outbound quota of %u Bps and inbound quota of %u Bps for peer `%s' to all clients\n",
1250 ntohl (n->bandwidth_out.value__),
1251 ntohl (n->bandwidth_in.value__),
1255 n->bandwidth_in = bandwidth_in;
1256 n->bandwidth_out = bandwidth_out;
1257 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1259 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1260 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1261 q_msg.quota = n->bandwidth_out;
1262 q_msg.peer = (*peer);
1263 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1269 GNUNET_free_non_null (n->addr);
1270 n->addr = GNUNET_malloc (address_len);
1271 memcpy (n->addr, address, address_len);
1272 n->bandwidth_in = bandwidth_in;
1273 n->bandwidth_out = bandwidth_out;
1274 n->addrlen = address_len;
1275 n->session = session;
1276 GNUNET_free_non_null (n->plugin_name);
1277 n->plugin_name = GNUNET_strdup (plugin_name);
1278 GNUNET_SCHEDULER_cancel (n->timeout_task);
1280 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1281 &neighbour_timeout_task, n);
1283 if (n->state == S_DISCONNECT)
1285 /* We are disconnecting, nothing to do here */
1288 /* We are not connected/connecting and initiate a fresh connect */
1289 if (n->state == S_NOT_CONNECTED)
1291 msg_len = sizeof (struct SessionConnectMessage);
1292 connect_msg.header.size = htons (msg_len);
1293 connect_msg.header.type =
1294 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1295 connect_msg.reserved = htonl (0);
1296 connect_msg.timestamp =
1297 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1299 change_state (n, S_CONNECT_SENT);
1301 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1302 session, plugin_name, address, address_len,
1303 GNUNET_YES, &send_connect_continuation, n);
1307 /* We received a CONNECT message and asked ATS for an address */
1308 else if (n->state == S_CONNECT_RECV)
1310 msg_len = sizeof (struct SessionConnectMessage);
1311 connect_msg.header.size = htons (msg_len);
1312 connect_msg.header.type =
1313 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1314 connect_msg.reserved = htonl (0);
1315 connect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1317 ret = send_with_plugin(&n->id, (const void *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1318 session, plugin_name, address, address_len,
1319 GNUNET_YES, &send_connect_ack_continuation, n);
1322 /* connected peer is switching addresses */
1323 else if (n->state == S_CONNECTED)
1325 msg_len = sizeof (struct SessionConnectMessage);
1326 connect_msg.header.size = htons (msg_len);
1327 connect_msg.header.type =
1328 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1329 connect_msg.reserved = htonl (0);
1330 connect_msg.timestamp =
1331 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1333 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1334 session, plugin_name, address, address_len,
1335 GNUNET_YES, &send_switch_address_continuation, n);
1336 if (ret == GNUNET_SYSERR)
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1339 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1340 GNUNET_i2s (peer), plugin_name,
1341 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1348 else if (n->state == S_CONNECT_SENT)
1352 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid connection state to switch addresses %u \n", n->state);
1353 GNUNET_break_op (0);
1359 * Create an entry in the neighbour map for the given peer
1361 * @param peer peer to create an entry for
1362 * @return new neighbour map entry
1364 static struct NeighbourMapEntry *
1365 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1367 struct NeighbourMapEntry *n;
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Unknown peer `%s', creating new neighbour\n",
1374 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1376 n->state = S_NOT_CONNECTED;
1377 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1378 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1379 MAX_BANDWIDTH_CARRY_S);
1381 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1382 &neighbour_timeout_task, n);
1383 GNUNET_assert (GNUNET_OK ==
1384 GNUNET_CONTAINER_multihashmap_put (neighbours,
1385 &n->id.hashPubKey, n,
1386 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1392 * Try to create a connection to the given target (eventually).
1394 * @param target peer to try to connect to
1397 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1399 struct NeighbourMapEntry *n;
1401 // This can happen during shutdown
1402 if (neighbours == NULL)
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1408 GNUNET_i2s (target));
1410 if (0 == memcmp (target, &GST_my_identity,
1411 sizeof (struct GNUNET_PeerIdentity)))
1416 n = lookup_neighbour (target);
1420 if ((is_connected(n)) || (is_connecting(n)))
1421 return; /* already connecting or connected */
1422 if (is_disconnecting(n))
1423 change_state (n, S_NOT_CONNECTED);
1428 n = setup_neighbour (target);
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Asking ATS for suggested address to connect to peer `%s'\n",
1432 GNUNET_i2s (&n->id));
1435 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1439 * Test if we're connected to the given peer.
1441 * @param target peer to test
1442 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1445 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1447 struct NeighbourMapEntry *n;
1449 // This can happen during shutdown
1450 if (neighbours == NULL)
1455 n = lookup_neighbour (target);
1457 if ((NULL == n) || (!is_connected(n)))
1458 return GNUNET_NO; /* not connected */
1464 * A session was terminated. Take note.
1466 * @param peer identity of the peer where the session died
1467 * @param session session that is gone
1470 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1471 struct Session *session)
1473 struct NeighbourMapEntry *n;
1475 // This can happen during shutdown
1476 if (neighbours == NULL)
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483 "Session %X to peer `%s' ended \n",
1484 session, GNUNET_i2s (peer));
1487 n = lookup_neighbour (peer);
1490 if (session != n->session)
1491 return; /* doesn't affect us */
1494 GNUNET_free (n->addr);
1498 /* not connected anymore anyway, shouldn't matter */
1499 if ((!is_connected(n)) && (!is_connecting(n)))
1502 /* We are connected, so ask ATS to switch addresses */
1503 GNUNET_SCHEDULER_cancel (n->timeout_task);
1505 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1506 &neighbour_timeout_task, n);
1507 /* try QUICKLY to re-establish a connection, reduce timeout! */
1508 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1509 GNUNET_SCHEDULER_cancel(n->ats_suggest);
1510 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
1511 GNUNET_ATS_suggest_address (GST_ats, peer);
1516 * Transmit a message to the given target using the active connection.
1518 * @param target destination
1519 * @param msg message to send
1520 * @param msg_size number of bytes in msg
1521 * @param timeout when to fail with timeout
1522 * @param cont function to call when done
1523 * @param cont_cls closure for 'cont'
1526 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1527 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1528 GST_NeighbourSendContinuation cont, void *cont_cls)
1530 struct NeighbourMapEntry *n;
1531 struct MessageQueue *mq;
1533 // This can happen during shutdown
1534 if (neighbours == NULL)
1539 n = lookup_neighbour (target);
1540 if ((n == NULL) || (!is_connected(n)))
1542 GNUNET_STATISTICS_update (GST_stats,
1544 ("# messages not sent (no such peer or not connected)"),
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Could not send message to peer `%s': unknown neighbour",
1550 GNUNET_i2s (target));
1551 else if (!is_connected(n))
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1553 "Could not send message to peer `%s': not connected\n",
1554 GNUNET_i2s (target));
1557 cont (cont_cls, GNUNET_SYSERR);
1561 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
1563 GNUNET_STATISTICS_update (GST_stats,
1565 ("# messages not sent (no such peer or not connected)"),
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Could not send message to peer `%s': no address available\n",
1570 GNUNET_i2s (target));
1574 cont (cont_cls, GNUNET_SYSERR);
1578 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1579 GNUNET_STATISTICS_update (GST_stats,
1581 ("# bytes in message queue for other peers"),
1582 msg_size, GNUNET_NO);
1583 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1585 mq->cont_cls = cont_cls;
1586 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1587 memcpy (&mq[1], msg, msg_size);
1588 mq->message_buf = (const char *) &mq[1];
1589 mq->message_buf_size = msg_size;
1590 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1591 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1593 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1594 (NULL == n->is_active))
1595 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1600 * We have received a message from the given sender. How long should
1601 * we delay before receiving more? (Also used to keep the peer marked
1604 * @param sender sender of the message
1605 * @param size size of the message
1606 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1607 * GNUNET_NO if the neighbour is not connected or violates the quota,
1608 * GNUNET_SYSERR if the connection is not fully up yet
1609 * @return how long to wait before reading more from this sender
1611 struct GNUNET_TIME_Relative
1612 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1613 *sender, ssize_t size, int *do_forward)
1615 struct NeighbourMapEntry *n;
1616 struct GNUNET_TIME_Relative ret;
1618 // This can happen during shutdown
1619 if (neighbours == NULL)
1621 return GNUNET_TIME_UNIT_FOREVER_REL;
1624 n = lookup_neighbour (sender);
1627 GST_neighbours_try_connect (sender);
1628 n = lookup_neighbour (sender);
1631 GNUNET_STATISTICS_update (GST_stats,
1633 ("# messages discarded due to lack of neighbour record"),
1635 *do_forward = GNUNET_NO;
1636 return GNUNET_TIME_UNIT_ZERO;
1639 if (!is_connected(n))
1641 *do_forward = GNUNET_SYSERR;
1642 return GNUNET_TIME_UNIT_ZERO;
1644 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1646 n->quota_violation_count++;
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1650 n->in_tracker.available_bytes_per_s__,
1651 n->quota_violation_count);
1653 /* Discount 32k per violation */
1654 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1658 if (n->quota_violation_count > 0)
1660 /* try to add 32k back */
1661 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1662 n->quota_violation_count--;
1665 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1667 GNUNET_STATISTICS_update (GST_stats,
1669 ("# bandwidth quota violations by other peers"),
1671 *do_forward = GNUNET_NO;
1672 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1674 *do_forward = GNUNET_YES;
1675 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1676 if (ret.rel_value > 0)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1681 (unsigned long long) n->in_tracker.
1682 consumption_since_last_update__,
1683 (unsigned int) n->in_tracker.available_bytes_per_s__,
1684 (unsigned long long) ret.rel_value);
1686 GNUNET_STATISTICS_update (GST_stats,
1687 gettext_noop ("# ms throttling suggested"),
1688 (int64_t) ret.rel_value, GNUNET_NO);
1695 * Keep the connection to the given neighbour alive longer,
1696 * we received a KEEPALIVE (or equivalent).
1698 * @param neighbour neighbour to keep alive
1701 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1703 struct NeighbourMapEntry *n;
1705 // This can happen during shutdown
1706 if (neighbours == NULL)
1711 n = lookup_neighbour (neighbour);
1714 GNUNET_STATISTICS_update (GST_stats,
1716 ("# KEEPALIVE messages discarded (not connected)"),
1720 GNUNET_SCHEDULER_cancel (n->timeout_task);
1722 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1723 &neighbour_timeout_task, n);
1728 * Change the incoming quota for the given peer.
1730 * @param neighbour identity of peer to change qutoa for
1731 * @param quota new quota
1734 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1735 struct GNUNET_BANDWIDTH_Value32NBO quota)
1737 struct NeighbourMapEntry *n;
1739 // This can happen during shutdown
1740 if (neighbours == NULL)
1745 n = lookup_neighbour (neighbour);
1748 GNUNET_STATISTICS_update (GST_stats,
1750 ("# SET QUOTA messages ignored (no such peer)"),
1754 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1755 if (0 != ntohl (quota.value__))
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1759 GNUNET_i2s (&n->id), "SET_QUOTA");
1761 if (is_connected(n))
1762 GNUNET_STATISTICS_update (GST_stats,
1763 gettext_noop ("# disconnects due to quota of 0"), 1,
1765 disconnect_neighbour (n);
1770 * Closure for the neighbours_iterate function.
1772 struct IteratorContext
1775 * Function to call on each connected neighbour.
1777 GST_NeighbourIterator cb;
1787 * Call the callback from the closure for each connected neighbour.
1789 * @param cls the 'struct IteratorContext'
1790 * @param key the hash of the public key of the neighbour
1791 * @param value the 'struct NeighbourMapEntry'
1792 * @return GNUNET_OK (continue to iterate)
1795 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1797 struct IteratorContext *ic = cls;
1798 struct NeighbourMapEntry *n = value;
1800 if (!is_connected(n))
1803 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1809 * Iterate over all connected neighbours.
1811 * @param cb function to call
1812 * @param cb_cls closure for cb
1815 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1817 struct IteratorContext ic;
1819 // This can happen during shutdown
1820 if (neighbours == NULL)
1827 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1831 * If we have an active connection to the given target, it must be shutdown.
1833 * @param target peer to disconnect from
1836 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1838 struct NeighbourMapEntry *n;
1840 // This can happen during shutdown
1841 if (neighbours == NULL)
1846 n = lookup_neighbour (target);
1848 return; /* not active */
1849 if (is_connected(n))
1851 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1853 n = lookup_neighbour (target);
1855 return; /* gone already */
1857 disconnect_neighbour (n);
1862 * We received a disconnect message from the given peer,
1863 * validate and process.
1865 * @param peer sender of the message
1866 * @param msg the disconnect message
1869 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1870 const struct GNUNET_MessageHeader *msg)
1872 struct NeighbourMapEntry *n;
1873 const struct SessionDisconnectMessage *sdm;
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Received DISCONNECT message from peer `%s'\n", GNUNET_i2s (peer));
1881 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1883 // GNUNET_break_op (0);
1884 GNUNET_STATISTICS_update (GST_stats,
1885 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1889 sdm = (const struct SessionDisconnectMessage* ) msg;
1890 n = lookup_neighbour (peer);
1892 return; /* gone already */
1893 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1894 n->connect_ts.abs_value)
1896 GNUNET_STATISTICS_update (GST_stats,
1897 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1901 GNUNET_CRYPTO_hash (&sdm->public_key,
1902 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1904 if (0 != memcmp (peer,
1906 sizeof (struct GNUNET_PeerIdentity)))
1908 GNUNET_break_op (0);
1911 if (ntohl (sdm->purpose.size) !=
1912 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1913 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1914 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1916 GNUNET_break_op (0);
1920 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1925 GNUNET_break_op (0);
1928 GST_neighbours_force_disconnect (peer);
1932 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1933 * Consider switching to it.
1935 * @param message possibly a 'struct SessionConnectMessage' (check format)
1936 * @param peer identity of the peer to switch the address for
1937 * @param plugin_name name of transport that delivered the PONG
1938 * @param address address of the other peer, NULL if other peer
1940 * @param address_len number of bytes in address
1941 * @param session session to use (or NULL)
1942 * @param ats performance data
1943 * @param ats_count number of entries in ats
1946 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1947 const struct GNUNET_PeerIdentity *peer,
1948 const char *plugin_name,
1949 const char *sender_address, uint16_t sender_address_len,
1950 struct Session *session,
1951 const struct GNUNET_ATS_Information *ats,
1954 const struct SessionConnectMessage *scm;
1955 struct QuotaSetMessage q_msg;
1956 struct GNUNET_MessageHeader msg;
1957 struct NeighbourMapEntry *n;
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 "Received CONNECT_ACK message from peer `%s'\n", GNUNET_i2s (peer));
1967 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1969 GNUNET_break_op (0);
1973 scm = (const struct SessionConnectMessage *) message;
1974 GNUNET_break_op (ntohl (scm->reserved) == 0);
1975 n = lookup_neighbour (peer);
1977 n = setup_neighbour (peer);
1979 if (n->state != S_CONNECT_SENT)
1982 send_disconnect(&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1986 if (NULL != session)
1987 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1989 "Giving ATS session %p of plugin %s for peer %s\n",
1993 GNUNET_ATS_address_update (GST_ats,
1995 plugin_name, sender_address, sender_address_len,
1996 session, ats, ats_count);
1998 was_connected = is_connected(n);
1999 if (!is_connected(n))
2000 change_state (n, S_CONNECTED);
2003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2004 "Setting inbound quota of %u for peer `%s' to \n",
2005 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
2007 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
2010 msg_len = sizeof (msg);
2011 msg.size = htons (msg_len);
2012 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2014 ret = send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2015 GNUNET_TIME_UNIT_FOREVER_REL,
2016 n->session, n->plugin_name, n->addr, n->addrlen,
2017 GNUNET_YES, NULL, NULL);
2019 if (ret == GNUNET_SYSERR)
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
2022 GNUNET_i2s (&n->id), n->plugin_name,
2023 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
2031 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2032 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2033 &neighbour_keepalive_task,
2036 neighbours_connected++;
2037 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2042 GNUNET_i2s (&n->id), n->plugin_name,
2043 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
2046 n->session, __LINE__);
2048 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2053 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2054 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2056 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2057 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2058 q_msg.quota = n->bandwidth_out;
2059 q_msg.peer = (*peer);
2060 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2065 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2066 const struct GNUNET_PeerIdentity *peer,
2067 const char *plugin_name,
2068 const char *sender_address, uint16_t sender_address_len,
2069 struct Session *session,
2070 const struct GNUNET_ATS_Information *ats,
2073 struct NeighbourMapEntry *n;
2074 struct QuotaSetMessage q_msg;
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Received ACK message from peer `%s'\n", GNUNET_i2s (peer));
2082 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2084 GNUNET_break_op (0);
2088 n = lookup_neighbour (peer);
2091 send_disconnect(peer, plugin_name, sender_address, sender_address_len, session);
2096 if (is_connected(n))
2099 if (NULL != session)
2100 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2102 "Giving ATS session %p of plugin %s for peer %s\n",
2106 GNUNET_ATS_address_update (GST_ats,
2108 plugin_name, sender_address, sender_address_len,
2109 session, ats, ats_count);
2111 was_connected = is_connected(n);
2112 change_state (n, S_CONNECTED);
2114 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
2116 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2117 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2118 &neighbour_keepalive_task,
2123 neighbours_connected++;
2124 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2130 GNUNET_i2s (&n->id), n->plugin_name,
2131 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
2134 n->session, __LINE__);
2136 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2141 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2144 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2145 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2146 q_msg.quota = n->bandwidth_out;
2147 q_msg.peer = (*peer);
2148 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2152 struct BlackListCheckContext
2154 struct GNUNET_ATS_Information *ats;
2158 struct Session *session;
2160 char *sender_address;
2162 uint16_t sender_address_len;
2166 struct GNUNET_TIME_Absolute ts;
2171 handle_connect_blacklist_cont (void *cls,
2172 const struct GNUNET_PeerIdentity
2175 struct NeighbourMapEntry *n;
2176 struct BlackListCheckContext * bcc = cls;
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 "Blacklist check due to CONNECT message: `%s'\n", GNUNET_i2s (peer), (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2184 if (GNUNET_OK != result)
2190 n = lookup_neighbour (peer);
2192 n = setup_neighbour (peer);
2194 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2196 if (NULL != bcc->session)
2197 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2199 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
2202 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address, bcc->sender_address_len),
2204 GNUNET_ATS_address_update (GST_ats,
2206 bcc->plugin_name, bcc->sender_address, bcc->sender_address_len,
2207 bcc->session, bcc->ats, bcc->ats_count);
2208 n->connect_ts = bcc->ts;
2213 if (n->state != S_CONNECT_RECV)
2214 change_state (n, S_CONNECT_RECV);
2216 /* Ask ATS for an address to connect via that address */
2217 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2218 GNUNET_SCHEDULER_cancel(n->ats_suggest);
2219 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel, n);
2220 GNUNET_ATS_suggest_address(GST_ats, peer);
2224 * We received a 'SESSION_CONNECT' message from the other peer.
2225 * Consider switching to it.
2227 * @param message possibly a 'struct SessionConnectMessage' (check format)
2228 * @param peer identity of the peer to switch the address for
2229 * @param plugin_name name of transport that delivered the PONG
2230 * @param address address of the other peer, NULL if other peer
2232 * @param address_len number of bytes in address
2233 * @param session session to use (or NULL)
2234 * @param ats performance data
2235 * @param ats_count number of entries in ats (excluding 0-termination)
2238 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2239 const struct GNUNET_PeerIdentity *peer,
2240 const char *plugin_name,
2241 const char *sender_address, uint16_t sender_address_len,
2242 struct Session *session,
2243 const struct GNUNET_ATS_Information *ats,
2246 const struct SessionConnectMessage *scm;
2247 struct NeighbourMapEntry * n;
2248 struct BlackListCheckContext * bcc = NULL;
2251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2252 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2255 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2257 GNUNET_break_op (0);
2261 scm = (const struct SessionConnectMessage *) message;
2262 GNUNET_break_op (ntohl (scm->reserved) == 0);
2264 n = lookup_neighbour(peer);
2267 /* connected peer switches addresses */
2268 if (is_connected(n))
2270 GNUNET_ATS_address_update(GST_ats, peer, plugin_name, sender_address, sender_address_len, session, ats, ats_count);
2275 /* we are not connected to this peer */
2276 /* do blacklist check*/
2277 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2278 sizeof (struct GNUNET_ATS_Information) * ats_count +
2279 sender_address_len +
2280 strlen (plugin_name)+1);
2282 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2284 bcc->ats_count = ats_count;
2285 bcc->sender_address_len = sender_address_len;
2286 bcc->session = session;
2288 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2289 memcpy (bcc->ats, ats,sizeof (struct GNUNET_ATS_Information) * ats_count );
2291 bcc->sender_address = (char *) &bcc->ats[ats_count];
2292 memcpy (bcc->sender_address, sender_address , sender_address_len);
2294 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2295 strcpy (bcc->plugin_name, plugin_name);
2297 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont, bcc);
2301 /* end of file gnunet-service-transport_neighbours.c */