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)
391 return "S_CONNECTED";
394 return "S_CONNECT_RECV";
396 case S_CONNECT_RECV_ACK_SENT:
397 return "S_CONNECT_RECV_ACK_SENT";
400 return "S_CONNECT_SENT";
403 return "S_DISCONNECT";
405 case S_NOT_CONNECTED:
406 return "S_NOT_CONNECTED";
416 change (struct NeighbourMapEntry *n, int state, int line);
419 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
422 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
424 struct NeighbourMapEntry *n = cls;
426 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
430 /* This jut a temporary debug message to check if a the value
431 * SETUP_CONNECTION_TIMEOUT was choosen to small for slow machines
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434 "Information for developers: Connection to peer `%s' %s failed in state `%s', resetting connection attempt \n",
435 GNUNET_i2s (&n->id), GST_plugins_a2s (n->plugin_name, n->addr,
437 print_state (n->state));
439 GNUNET_STATISTICS_update (GST_stats,
441 ("# failed connection attempts due to timeout"), 1,
444 /* resetting state */
445 n->state = S_NOT_CONNECTED;
447 /* destroying address */
448 GNUNET_ATS_address_destroyed (GST_ats, &n->id, n->plugin_name, n->addr,
451 /* request new address */
452 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
453 GNUNET_SCHEDULER_cancel (n->ats_suggest);
455 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
457 GNUNET_ATS_suggest_address (GST_ats, &n->id);
461 change (struct NeighbourMapEntry *n, int state, int line)
463 char *old = strdup (print_state (n->state));
464 char *new = strdup (print_state (state));
466 /* allowed transitions */
467 int allowed = GNUNET_NO;
471 case S_NOT_CONNECTED:
472 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
473 (state == S_DISCONNECT))
475 allowed = GNUNET_YES;
477 /* Schedule reset task */
478 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT))
480 GNUNET_assert (n->state_reset == GNUNET_SCHEDULER_NO_TASK);
482 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
489 if ((state == S_NOT_CONNECTED) || (state == S_DISCONNECT) ||
490 (state == S_CONNECTED) ||
491 /* FIXME SENT -> RECV ISSUE! */ (state == S_CONNECT_SENT))
493 if ((state == S_CONNECTED) || (state == S_DISCONNECT) ||
494 (state == S_NOT_CONNECTED))
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
499 GNUNET_i2s (&n->id), GST_plugins_a2s (n->plugin_name,
500 n->addr, n->addrlen),
501 print_state (n->state), print_state (state));
503 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
504 GNUNET_SCHEDULER_cancel (n->state_reset);
505 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
508 allowed = GNUNET_YES;
513 if ((state == S_NOT_CONNECTED) || (state == S_CONNECTED) ||
514 (state == S_DISCONNECT) ||
515 /* FIXME SENT -> RECV ISSUE! */ (state == S_CONNECT_RECV))
517 if ((state == S_CONNECTED) || (state == S_DISCONNECT) ||
518 (state == S_NOT_CONNECTED))
521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
523 GNUNET_i2s (&n->id), GST_plugins_a2s (n->plugin_name,
524 n->addr, n->addrlen),
525 print_state (n->state), print_state (state));
527 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
528 GNUNET_SCHEDULER_cancel (n->state_reset);
529 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
532 allowed = GNUNET_YES;
537 if (state == S_DISCONNECT)
539 allowed = GNUNET_YES;
545 * if (state == S_NOT_CONNECTED)
547 * allowed = GNUNET_YES;
557 if (allowed == GNUNET_NO)
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560 "Illegal state transition from `%s' to `%s' in line %u \n", old,
565 return GNUNET_SYSERR;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
572 GNUNET_i2s (&n->id), n, old, new, line);
580 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
581 size_t msgbuf_size, uint32_t priority,
582 struct GNUNET_TIME_Relative timeout, struct Session *session,
583 const char *plugin_name, const void *addr, size_t addrlen,
584 int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
587 struct GNUNET_TRANSPORT_PluginFunctions *papi;
588 size_t ret = GNUNET_SYSERR;
590 /* FIXME : ats returns an address with all values 0 */
591 if (plugin_name == NULL)
594 cont (cont_cls, target, GNUNET_SYSERR);
595 return GNUNET_SYSERR;
598 if ((session == NULL) && (addr == NULL) && (addrlen == 0))
601 cont (cont_cls, target, GNUNET_SYSERR);
602 return GNUNET_SYSERR;
605 papi = GST_plugins_find (plugin_name);
609 cont (cont_cls, target, GNUNET_SYSERR);
610 return GNUNET_SYSERR;
614 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
615 addr, addrlen, GNUNET_YES, cont, cont_cls);
620 cont (cont_cls, target, GNUNET_SYSERR);
626 * Task invoked to start a transmission to another peer.
628 * @param cls the 'struct NeighbourMapEntry'
629 * @param tc scheduler context
632 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
636 * We're done with our transmission attempt, continue processing.
638 * @param cls the 'struct MessageQueue' of the message
639 * @param receiver intended receiver
640 * @param success whether it worked or not
643 transmit_send_continuation (void *cls,
644 const struct GNUNET_PeerIdentity *receiver,
647 struct MessageQueue *mq;
648 struct NeighbourMapEntry *n;
654 GNUNET_assert (n->is_active == mq);
656 if (success == GNUNET_YES)
658 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
659 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
664 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
665 (success == GNUNET_OK) ? "successful" : "FAILED");
667 if (NULL != mq->cont)
668 mq->cont (mq->cont_cls, success);
674 * Check the ready list for the given neighbour and if a plugin is
675 * ready for transmission (and if we have a message), do so!
677 * @param n target peer for which to transmit
680 try_transmission_to_peer (struct NeighbourMapEntry *n)
682 struct MessageQueue *mq;
683 struct GNUNET_TIME_Relative timeout;
686 if (n->is_active != NULL)
689 return; /* transmission already pending */
691 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
694 return; /* currently waiting for bandwidth */
696 while (NULL != (mq = n->messages_head))
698 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
699 if (timeout.rel_value > 0)
701 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
704 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
707 return; /* no more messages */
709 if (GST_plugins_find (n->plugin_name) == NULL)
714 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
718 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
721 GNUNET_i2s (&n->id));
722 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
723 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
724 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
729 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
730 timeout, n->session, n->plugin_name, n->addr,
731 n->addrlen, GNUNET_YES, &transmit_send_continuation,
735 /* failure, but 'send' would not call continuation in this case,
736 * so we need to do it here! */
737 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
744 * Task invoked to start a transmission to another peer.
746 * @param cls the 'struct NeighbourMapEntry'
747 * @param tc scheduler context
750 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
752 struct NeighbourMapEntry *n = cls;
754 GNUNET_assert (NULL != lookup_neighbour (&n->id));
755 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
756 try_transmission_to_peer (n);
761 * Initialize the neighbours subsystem.
763 * @param cls closure for callbacks
764 * @param connect_cb function to call if we connect to a peer
765 * @param disconnect_cb function to call if we disconnect from a peer
768 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
769 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
772 connect_notify_cb = connect_cb;
773 disconnect_notify_cb = disconnect_cb;
774 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
779 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
784 "Sending DISCONNECT message to peer `%4s': %i\n",
785 GNUNET_i2s (target), result);
791 send_disconnect (const struct GNUNET_PeerIdentity *target,
792 const char *plugin_name, const char *sender_address,
793 uint16_t sender_address_len, struct Session *session)
796 struct SessionDisconnectMessage disconnect_msg;
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Sending DISCONNECT message to peer `%4s'\n",
801 GNUNET_i2s (target));
804 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
805 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
806 disconnect_msg.reserved = htonl (0);
807 disconnect_msg.purpose.size =
808 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
809 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
810 sizeof (struct GNUNET_TIME_AbsoluteNBO));
811 disconnect_msg.purpose.purpose =
812 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
813 disconnect_msg.timestamp =
814 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
815 disconnect_msg.public_key = GST_my_public_key;
816 GNUNET_assert (GNUNET_OK ==
817 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
818 &disconnect_msg.purpose,
819 &disconnect_msg.signature));
822 send_with_plugin (target, (const char *) &disconnect_msg,
823 sizeof (disconnect_msg), UINT32_MAX,
824 GNUNET_TIME_UNIT_FOREVER_REL, session, plugin_name,
825 sender_address, sender_address_len, GNUNET_YES,
826 &send_disconnect_cont, NULL);
828 if (ret == GNUNET_SYSERR)
829 return GNUNET_SYSERR;
831 GNUNET_STATISTICS_update (GST_stats,
833 ("# peers disconnected due to external request"), 1,
839 * Disconnect from the given neighbour, clean up the record.
841 * @param n neighbour to disconnect from
844 disconnect_neighbour (struct NeighbourMapEntry *n)
846 struct MessageQueue *mq;
847 int was_connected = is_connected (n);
849 /* send DISCONNECT MESSAGE */
850 if (is_connected (n) || is_connecting (n))
853 send_disconnect (&n->id, n->plugin_name, n->addr, n->addrlen,
855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
856 GNUNET_i2s (&n->id));
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Could not send DISCONNECT_MSG to `%s'\n",
860 GNUNET_i2s (&n->id));
865 GNUNET_ATS_address_in_use (GST_ats, &n->id, n->plugin_name,
866 n->addr, n->addrlen, n->session, GNUNET_NO);
870 if (is_disconnecting (n))
872 change_state (n, S_DISCONNECT);
873 GST_validation_set_address_use (&n->id,
880 if (n->plugin_name != NULL)
882 struct GNUNET_TRANSPORT_PluginFunctions *papi;
883 papi = GST_plugins_find (n->plugin_name);
885 papi->disconnect (papi->cls, &n->id);
888 while (NULL != (mq = n->messages_head))
890 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
891 if (NULL != mq->cont)
892 mq->cont (mq->cont_cls, GNUNET_SYSERR);
895 if (NULL != n->is_active)
897 n->is_active->n = NULL;
902 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
903 GNUNET_SCHEDULER_cancel (n->keepalive_task);
904 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
905 GNUNET_assert (neighbours_connected > 0);
906 neighbours_connected--;
907 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
909 disconnect_notify_cb (callback_cls, &n->id);
911 GNUNET_assert (GNUNET_YES ==
912 GNUNET_CONTAINER_multihashmap_remove (neighbours,
913 &n->id.hashPubKey, n));
914 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
916 GNUNET_SCHEDULER_cancel (n->ats_suggest);
917 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
919 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
921 GNUNET_SCHEDULER_cancel (n->timeout_task);
922 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
924 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
926 GNUNET_SCHEDULER_cancel (n->transmission_task);
927 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
929 if (NULL != n->plugin_name)
931 GNUNET_free (n->plugin_name);
932 n->plugin_name = NULL;
936 GNUNET_free (n->addr);
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
942 GNUNET_i2s (&n->id), n);
948 * Peer has been idle for too long. Disconnect.
950 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
951 * @param tc scheduler context
954 neighbour_timeout_task (void *cls,
955 const struct GNUNET_SCHEDULER_TaskContext *tc)
957 struct NeighbourMapEntry *n = cls;
959 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
961 GNUNET_STATISTICS_update (GST_stats,
963 ("# peers disconnected due to timeout"), 1,
965 disconnect_neighbour (n);
970 * Send another keepalive message.
972 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
973 * @param tc scheduler context
976 neighbour_keepalive_task (void *cls,
977 const struct GNUNET_SCHEDULER_TaskContext *tc)
979 struct NeighbourMapEntry *n = cls;
980 struct GNUNET_MessageHeader m;
983 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
984 &neighbour_keepalive_task, n);
985 GNUNET_assert (is_connected (n));
986 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
988 m.size = htons (sizeof (struct GNUNET_MessageHeader));
989 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
991 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
992 UINT32_MAX /* priority */ ,
993 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->plugin_name,
994 n->addr, n->addrlen, GNUNET_YES, NULL, NULL);
999 * Disconnect from the given neighbour.
1002 * @param key hash of neighbour's public key (not used)
1003 * @param value the 'struct NeighbourMapEntry' of the neighbour
1006 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1008 struct NeighbourMapEntry *n = value;
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1012 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1014 if (is_connected (n))
1015 GNUNET_STATISTICS_update (GST_stats,
1017 ("# peers disconnected due to global disconnect"),
1019 disconnect_neighbour (n);
1025 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1027 struct NeighbourMapEntry *n = cls;
1029 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1032 " ATS did not suggested address to connect to peer `%s'\n",
1033 GNUNET_i2s (&n->id));
1035 disconnect_neighbour (n);
1039 * Cleanup the neighbours subsystem.
1042 GST_neighbours_stop ()
1044 // This can happen during shutdown
1045 if (neighbours == NULL)
1050 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1052 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1053 GNUNET_assert (neighbours_connected == 0);
1055 callback_cls = NULL;
1056 connect_notify_cb = NULL;
1057 disconnect_notify_cb = NULL;
1062 * We tried to send a SESSION_CONNECT message to another peer. If this
1063 * succeeded, we change the state. If it failed, we should tell
1064 * ATS to not use this address anymore (until it is re-validated).
1066 * @param cls the 'struct NeighbourMapEntry'
1067 * @param success GNUNET_OK on success
1070 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1073 struct NeighbourMapEntry *n = cls;
1075 GNUNET_assert (n != NULL);
1076 GNUNET_assert (!is_connected (n));
1078 if (is_disconnecting (n))
1079 return; /* neighbour is going away */
1081 if (GNUNET_YES != success)
1084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1085 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1086 GNUNET_i2s (&n->id), n->plugin_name,
1088 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name, n->addr,
1089 n->addrlen), n->session);
1092 GNUNET_ATS_address_destroyed (GST_ats, &n->id, n->plugin_name, n->addr,
1095 change_state (n, S_NOT_CONNECTED);
1097 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1098 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1100 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1102 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1111 * We tried to switch addresses with an peer already connected. If it failed,
1112 * we should tell ATS to not use this address anymore (until it is re-validated).
1114 * @param cls the 'struct NeighbourMapEntry'
1115 * @param success GNUNET_OK on success
1118 send_switch_address_continuation (void *cls,
1119 const struct GNUNET_PeerIdentity *target,
1122 struct NeighbourMapEntry *n = cls;
1124 GNUNET_assert (n != NULL);
1125 if (is_disconnecting (n))
1126 return; /* neighbour is going away */
1128 GNUNET_assert (n->state == S_CONNECTED);
1129 if (GNUNET_YES != success)
1132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1133 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1134 GNUNET_i2s (&n->id), n->plugin_name,
1136 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name, n->addr,
1137 n->addrlen), n->session);
1140 GNUNET_ATS_address_destroyed (GST_ats, &n->id, n->plugin_name, n->addr,
1143 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1144 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1146 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1148 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1154 * We tried to send a SESSION_CONNECT message to another peer. If this
1155 * succeeded, we change the state. If it failed, we should tell
1156 * ATS to not use this address anymore (until it is re-validated).
1158 * @param cls the 'struct NeighbourMapEntry'
1159 * @param success GNUNET_OK on success
1162 send_connect_ack_continuation (void *cls,
1163 const struct GNUNET_PeerIdentity *target,
1166 struct NeighbourMapEntry *n = cls;
1168 GNUNET_assert (n != NULL);
1170 if (GNUNET_YES == success)
1171 return; /* sending successful */
1173 /* sending failed, ask for next address */
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1177 GNUNET_i2s (&n->id), n->plugin_name,
1178 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1183 change_state (n, S_NOT_CONNECTED);
1185 GNUNET_ATS_address_destroyed (GST_ats, &n->id, n->plugin_name, n->addr,
1188 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1189 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1191 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1193 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1197 * For an existing neighbour record, set the active connection to
1198 * the given address.
1200 * @param peer identity of the peer to switch the address for
1201 * @param plugin_name name of transport that delivered the PONG
1202 * @param address address of the other peer, NULL if other peer
1204 * @param address_len number of bytes in address
1205 * @param session session to use (or NULL)
1206 * @param ats performance data
1207 * @param ats_count number of entries in ats
1208 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1209 * connection is not up (yet)
1212 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1213 const char *plugin_name,
1214 const void *address, size_t address_len,
1215 struct Session *session,
1216 const struct GNUNET_ATS_Information *ats,
1218 struct GNUNET_BANDWIDTH_Value32NBO
1220 struct GNUNET_BANDWIDTH_Value32NBO
1223 struct NeighbourMapEntry *n;
1224 struct SessionConnectMessage connect_msg;
1229 // This can happen during shutdown
1230 if (neighbours == NULL)
1235 checks_failed = GNUNET_NO;
1237 if (plugin_name == NULL)
1239 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1240 "ATS offered suggested us empty address: plugin NULL");
1241 GNUNET_break_op (0);
1242 checks_failed = GNUNET_YES;
1244 if ((address == NULL) && (address_len == 0) && (session == NULL))
1246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1247 "ATS offered suggested us empty address: address NULL & session NULL");
1248 GNUNET_break_op (0);
1249 checks_failed = GNUNET_YES;
1252 n = lookup_neighbour (peer);
1254 checks_failed = GNUNET_YES;
1256 if (checks_failed == GNUNET_YES)
1258 GNUNET_ATS_address_destroyed (GST_ats, peer, plugin_name, address,
1259 address_len, session);
1261 GNUNET_ATS_suggest_address (GST_ats, peer);
1265 /* checks successful and neighbour != NULL */
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1270 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1273 session, (is_connected (n) ? "CONNECTED" : "NOT CONNECTED"),
1277 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1279 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1280 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1283 // do not switch addresses just update quotas
1284 if ((is_connected (n)) && (address_len == n->addrlen))
1286 if ((0 == memcmp (address, n->addr, address_len)) &&
1287 (n->session == session))
1289 struct QuotaSetMessage q_msg;
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1293 "Sending outbound quota of %u Bps and inbound quota of %u Bps for peer `%s' to all clients\n",
1294 ntohl (n->bandwidth_out.value__),
1295 ntohl (n->bandwidth_in.value__), GNUNET_i2s (peer));
1298 n->bandwidth_in = bandwidth_in;
1299 n->bandwidth_out = bandwidth_out;
1300 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1302 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1303 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1304 q_msg.quota = n->bandwidth_out;
1305 q_msg.peer = (*peer);
1306 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1310 if (n->state == S_CONNECTED)
1311 GST_validation_set_address_use (&n->id,
1317 GNUNET_free_non_null (n->addr);
1318 n->addr = GNUNET_malloc (address_len);
1319 memcpy (n->addr, address, address_len);
1320 n->bandwidth_in = bandwidth_in;
1321 n->bandwidth_out = bandwidth_out;
1322 n->addrlen = address_len;
1323 n->session = session;
1324 GNUNET_free_non_null (n->plugin_name);
1325 n->plugin_name = GNUNET_strdup (plugin_name);
1326 GNUNET_SCHEDULER_cancel (n->timeout_task);
1328 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1329 &neighbour_timeout_task, n);
1330 if (n->state == S_CONNECTED)
1331 GST_validation_set_address_use (&n->id,
1339 if (n->state == S_DISCONNECT)
1341 /* We are disconnecting, nothing to do here */
1344 /* We are not connected/connecting and initiate a fresh connect */
1345 if (n->state == S_NOT_CONNECTED)
1347 msg_len = sizeof (struct SessionConnectMessage);
1348 connect_msg.header.size = htons (msg_len);
1349 connect_msg.header.type =
1350 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1351 connect_msg.reserved = htonl (0);
1352 connect_msg.timestamp =
1353 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1355 change_state (n, S_CONNECT_SENT);
1358 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1359 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1360 plugin_name, address, address_len, GNUNET_YES,
1361 &send_connect_continuation, n);
1366 /* We received a CONNECT message and asked ATS for an address */
1367 else if (n->state == S_CONNECT_RECV)
1369 msg_len = sizeof (struct SessionConnectMessage);
1370 connect_msg.header.size = htons (msg_len);
1371 connect_msg.header.type =
1372 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1373 connect_msg.reserved = htonl (0);
1374 connect_msg.timestamp =
1375 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1378 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1379 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1380 plugin_name, address, address_len, GNUNET_YES,
1381 &send_connect_ack_continuation, n);
1384 /* connected peer is switching addresses */
1385 else if (n->state == S_CONNECTED)
1387 msg_len = sizeof (struct SessionConnectMessage);
1388 connect_msg.header.size = htons (msg_len);
1389 connect_msg.header.type =
1390 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1391 connect_msg.reserved = htonl (0);
1392 connect_msg.timestamp =
1393 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1396 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1397 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1398 plugin_name, address, address_len, GNUNET_YES,
1399 &send_switch_address_continuation, n);
1400 if (ret == GNUNET_SYSERR)
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1404 GNUNET_i2s (peer), plugin_name,
1406 0) ? "<inbound>" : GST_plugins_a2s (plugin_name, address,
1407 address_len), session);
1411 else if (n->state == S_CONNECT_SENT)
1415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1416 "Invalid connection state to switch addresses %u \n", n->state);
1417 GNUNET_break_op (0);
1423 * Obtain current latency information for the given neighbour.
1426 * @return observed latency of the address, FOREVER if the address was
1427 * never successfully validated
1429 struct GNUNET_TIME_Relative
1430 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1432 struct NeighbourMapEntry *n;
1434 n = lookup_neighbour (peer);
1436 return GNUNET_TIME_UNIT_FOREVER_REL;
1437 return GST_validation_get_address_latency (peer,
1446 * Create an entry in the neighbour map for the given peer
1448 * @param peer peer to create an entry for
1449 * @return new neighbour map entry
1451 static struct NeighbourMapEntry *
1452 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1454 struct NeighbourMapEntry *n;
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1460 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1462 n->state = S_NOT_CONNECTED;
1463 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1464 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1465 MAX_BANDWIDTH_CARRY_S);
1467 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1468 &neighbour_timeout_task, n);
1469 GNUNET_assert (GNUNET_OK ==
1470 GNUNET_CONTAINER_multihashmap_put (neighbours,
1471 &n->id.hashPubKey, n,
1472 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1478 * Try to create a connection to the given target (eventually).
1480 * @param target peer to try to connect to
1483 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1485 struct NeighbourMapEntry *n;
1487 // This can happen during shutdown
1488 if (neighbours == NULL)
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1494 GNUNET_i2s (target));
1497 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1502 n = lookup_neighbour (target);
1506 if ((is_connected (n)) || (is_connecting (n)))
1507 return; /* already connecting or connected */
1508 if (is_disconnecting (n))
1509 change_state (n, S_NOT_CONNECTED);
1514 n = setup_neighbour (target);
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Asking ATS for suggested address to connect to peer `%s'\n",
1518 GNUNET_i2s (&n->id));
1521 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1525 * Test if we're connected to the given peer.
1527 * @param target peer to test
1528 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1531 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1533 struct NeighbourMapEntry *n;
1535 // This can happen during shutdown
1536 if (neighbours == NULL)
1541 n = lookup_neighbour (target);
1543 if ((NULL == n) || (!is_connected (n)))
1544 return GNUNET_NO; /* not connected */
1550 * A session was terminated. Take note.
1552 * @param peer identity of the peer where the session died
1553 * @param session session that is gone
1556 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1557 struct Session *session)
1559 struct NeighbourMapEntry *n;
1561 // This can happen during shutdown
1562 if (neighbours == NULL)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1569 session, GNUNET_i2s (peer));
1572 n = lookup_neighbour (peer);
1575 if (session != n->session)
1576 return; /* doesn't affect us */
1579 GNUNET_free (n->addr);
1583 /* not connected anymore anyway, shouldn't matter */
1584 if ((!is_connected (n)) && (!is_connecting (n)))
1587 /* We are connected, so ask ATS to switch addresses */
1588 GNUNET_SCHEDULER_cancel (n->timeout_task);
1590 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1591 &neighbour_timeout_task, n);
1592 /* try QUICKLY to re-establish a connection, reduce timeout! */
1593 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1594 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1596 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1598 GNUNET_ATS_suggest_address (GST_ats, peer);
1603 * Transmit a message to the given target using the active connection.
1605 * @param target destination
1606 * @param msg message to send
1607 * @param msg_size number of bytes in msg
1608 * @param timeout when to fail with timeout
1609 * @param cont function to call when done
1610 * @param cont_cls closure for 'cont'
1613 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1614 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1615 GST_NeighbourSendContinuation cont, void *cont_cls)
1617 struct NeighbourMapEntry *n;
1618 struct MessageQueue *mq;
1620 // This can happen during shutdown
1621 if (neighbours == NULL)
1626 n = lookup_neighbour (target);
1627 if ((n == NULL) || (!is_connected (n)))
1629 GNUNET_STATISTICS_update (GST_stats,
1631 ("# messages not sent (no such peer or not connected)"),
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Could not send message to peer `%s': unknown neighbour",
1637 GNUNET_i2s (target));
1638 else if (!is_connected (n))
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1640 "Could not send message to peer `%s': not connected\n",
1641 GNUNET_i2s (target));
1644 cont (cont_cls, GNUNET_SYSERR);
1648 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
1650 GNUNET_STATISTICS_update (GST_stats,
1652 ("# messages not sent (no such peer or not connected)"),
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Could not send message to peer `%s': no address available\n",
1657 GNUNET_i2s (target));
1661 cont (cont_cls, GNUNET_SYSERR);
1665 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1666 GNUNET_STATISTICS_update (GST_stats,
1668 ("# bytes in message queue for other peers"),
1669 msg_size, GNUNET_NO);
1670 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1672 mq->cont_cls = cont_cls;
1673 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1674 memcpy (&mq[1], msg, msg_size);
1675 mq->message_buf = (const char *) &mq[1];
1676 mq->message_buf_size = msg_size;
1677 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1678 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1680 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1681 (NULL == n->is_active))
1682 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1687 * We have received a message from the given sender. How long should
1688 * we delay before receiving more? (Also used to keep the peer marked
1691 * @param sender sender of the message
1692 * @param size size of the message
1693 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1694 * GNUNET_NO if the neighbour is not connected or violates the quota,
1695 * GNUNET_SYSERR if the connection is not fully up yet
1696 * @return how long to wait before reading more from this sender
1698 struct GNUNET_TIME_Relative
1699 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1700 *sender, ssize_t size, int *do_forward)
1702 struct NeighbourMapEntry *n;
1703 struct GNUNET_TIME_Relative ret;
1705 // This can happen during shutdown
1706 if (neighbours == NULL)
1708 return GNUNET_TIME_UNIT_FOREVER_REL;
1711 n = lookup_neighbour (sender);
1714 GST_neighbours_try_connect (sender);
1715 n = lookup_neighbour (sender);
1718 GNUNET_STATISTICS_update (GST_stats,
1720 ("# messages discarded due to lack of neighbour record"),
1722 *do_forward = GNUNET_NO;
1723 return GNUNET_TIME_UNIT_ZERO;
1726 if (!is_connected (n))
1728 *do_forward = GNUNET_SYSERR;
1729 return GNUNET_TIME_UNIT_ZERO;
1731 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1733 n->quota_violation_count++;
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1737 n->in_tracker.available_bytes_per_s__,
1738 n->quota_violation_count);
1740 /* Discount 32k per violation */
1741 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1745 if (n->quota_violation_count > 0)
1747 /* try to add 32k back */
1748 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1749 n->quota_violation_count--;
1752 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1754 GNUNET_STATISTICS_update (GST_stats,
1756 ("# bandwidth quota violations by other peers"),
1758 *do_forward = GNUNET_NO;
1759 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1761 *do_forward = GNUNET_YES;
1762 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1763 if (ret.rel_value > 0)
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1768 (unsigned long long) n->in_tracker.
1769 consumption_since_last_update__,
1770 (unsigned int) n->in_tracker.available_bytes_per_s__,
1771 (unsigned long long) ret.rel_value);
1773 GNUNET_STATISTICS_update (GST_stats,
1774 gettext_noop ("# ms throttling suggested"),
1775 (int64_t) ret.rel_value, GNUNET_NO);
1782 * Keep the connection to the given neighbour alive longer,
1783 * we received a KEEPALIVE (or equivalent).
1785 * @param neighbour neighbour to keep alive
1788 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1790 struct NeighbourMapEntry *n;
1792 // This can happen during shutdown
1793 if (neighbours == NULL)
1798 n = lookup_neighbour (neighbour);
1801 GNUNET_STATISTICS_update (GST_stats,
1803 ("# KEEPALIVE messages discarded (not connected)"),
1807 GNUNET_SCHEDULER_cancel (n->timeout_task);
1809 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1810 &neighbour_timeout_task, n);
1815 * Change the incoming quota for the given peer.
1817 * @param neighbour identity of peer to change qutoa for
1818 * @param quota new quota
1821 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1822 struct GNUNET_BANDWIDTH_Value32NBO quota)
1824 struct NeighbourMapEntry *n;
1826 // This can happen during shutdown
1827 if (neighbours == NULL)
1832 n = lookup_neighbour (neighbour);
1835 GNUNET_STATISTICS_update (GST_stats,
1837 ("# SET QUOTA messages ignored (no such peer)"),
1841 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1842 if (0 != ntohl (quota.value__))
1845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1846 GNUNET_i2s (&n->id), "SET_QUOTA");
1848 if (is_connected (n))
1849 GNUNET_STATISTICS_update (GST_stats,
1850 gettext_noop ("# disconnects due to quota of 0"),
1852 disconnect_neighbour (n);
1857 * Closure for the neighbours_iterate function.
1859 struct IteratorContext
1862 * Function to call on each connected neighbour.
1864 GST_NeighbourIterator cb;
1874 * Call the callback from the closure for each connected neighbour.
1876 * @param cls the 'struct IteratorContext'
1877 * @param key the hash of the public key of the neighbour
1878 * @param value the 'struct NeighbourMapEntry'
1879 * @return GNUNET_OK (continue to iterate)
1882 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1884 struct IteratorContext *ic = cls;
1885 struct NeighbourMapEntry *n = value;
1887 if (!is_connected (n))
1890 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1896 * Iterate over all connected neighbours.
1898 * @param cb function to call
1899 * @param cb_cls closure for cb
1902 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1904 struct IteratorContext ic;
1906 // This can happen during shutdown
1907 if (neighbours == NULL)
1914 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1918 * If we have an active connection to the given target, it must be shutdown.
1920 * @param target peer to disconnect from
1923 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1925 struct NeighbourMapEntry *n;
1927 // This can happen during shutdown
1928 if (neighbours == NULL)
1933 n = lookup_neighbour (target);
1935 return; /* not active */
1936 if (is_connected (n))
1938 send_disconnect (&n->id, n->plugin_name, n->addr, n->addrlen, n->session);
1940 n = lookup_neighbour (target);
1942 return; /* gone already */
1944 disconnect_neighbour (n);
1949 * We received a disconnect message from the given peer,
1950 * validate and process.
1952 * @param peer sender of the message
1953 * @param msg the disconnect message
1956 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
1958 const struct GNUNET_MessageHeader
1961 struct NeighbourMapEntry *n;
1962 const struct SessionDisconnectMessage *sdm;
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Received DISCONNECT message from peer `%s'\n",
1971 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1973 // GNUNET_break_op (0);
1974 GNUNET_STATISTICS_update (GST_stats,
1976 ("# disconnect messages ignored (old format)"), 1,
1980 sdm = (const struct SessionDisconnectMessage *) msg;
1981 n = lookup_neighbour (peer);
1983 return; /* gone already */
1984 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1985 n->connect_ts.abs_value)
1987 GNUNET_STATISTICS_update (GST_stats,
1989 ("# disconnect messages ignored (timestamp)"), 1,
1993 GNUNET_CRYPTO_hash (&sdm->public_key,
1994 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1996 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
1998 GNUNET_break_op (0);
2001 if (ntohl (sdm->purpose.size) !=
2002 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2003 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2004 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2006 GNUNET_break_op (0);
2010 GNUNET_CRYPTO_rsa_verify
2011 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2012 &sdm->signature, &sdm->public_key))
2014 GNUNET_break_op (0);
2017 GST_neighbours_force_disconnect (peer);
2021 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2022 * Consider switching to it.
2024 * @param message possibly a 'struct SessionConnectMessage' (check format)
2025 * @param peer identity of the peer to switch the address for
2026 * @param plugin_name name of transport that delivered the PONG
2027 * @param address address of the other peer, NULL if other peer
2029 * @param address_len number of bytes in address
2030 * @param session session to use (or NULL)
2031 * @param ats performance data
2032 * @param ats_count number of entries in ats
2035 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2036 const struct GNUNET_PeerIdentity *peer,
2037 const char *plugin_name,
2038 const char *sender_address,
2039 uint16_t sender_address_len,
2040 struct Session *session,
2041 const struct GNUNET_ATS_Information *ats,
2044 const struct SessionConnectMessage *scm;
2045 struct QuotaSetMessage q_msg;
2046 struct GNUNET_MessageHeader msg;
2047 struct NeighbourMapEntry *n;
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "Received CONNECT_ACK message from peer `%s'\n",
2058 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2060 GNUNET_break_op (0);
2064 scm = (const struct SessionConnectMessage *) message;
2065 GNUNET_break_op (ntohl (scm->reserved) == 0);
2066 n = lookup_neighbour (peer);
2068 n = setup_neighbour (peer);
2070 if (!is_connecting(n))
2072 GNUNET_STATISTICS_update (GST_stats,
2073 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2078 if (NULL != session)
2079 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2081 "Giving ATS session %p of plugin %s for peer %s\n",
2082 session, plugin_name, GNUNET_i2s (peer));
2083 GNUNET_ATS_address_update (GST_ats, peer, plugin_name, sender_address,
2084 sender_address_len, session, ats, ats_count);
2086 was_connected = is_connected (n);
2087 if (!is_connected (n))
2089 change_state (n, S_CONNECTED);
2090 GST_validation_set_address_use (&n->id,
2098 GNUNET_ATS_address_in_use (GST_ats, &n->id, n->plugin_name, n->addr,
2099 n->addrlen, n->addr, GNUNET_YES);
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "Setting inbound quota of %u for peer `%s' to \n",
2104 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
2106 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2108 /* send ACK (ACK) */
2109 msg_len = sizeof (msg);
2110 msg.size = htons (msg_len);
2111 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2114 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2115 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2116 n->plugin_name, n->addr, n->addrlen, GNUNET_YES, NULL,
2119 if (ret == GNUNET_SYSERR)
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
2122 GNUNET_i2s (&n->id), n->plugin_name,
2124 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name, n->addr,
2125 n->addrlen), n->session);
2130 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2132 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2133 &neighbour_keepalive_task, n);
2135 neighbours_connected++;
2136 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2141 GNUNET_i2s (&n->id), n->plugin_name,
2143 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name, n->addr,
2144 n->addrlen), n->session,
2147 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2153 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2155 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2156 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2157 q_msg.quota = n->bandwidth_out;
2158 q_msg.peer = (*peer);
2159 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2164 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2165 const struct GNUNET_PeerIdentity *peer,
2166 const char *plugin_name, const char *sender_address,
2167 uint16_t sender_address_len, struct Session *session,
2168 const struct GNUNET_ATS_Information *ats,
2171 struct NeighbourMapEntry *n;
2172 struct QuotaSetMessage q_msg;
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2180 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2182 GNUNET_break_op (0);
2186 n = lookup_neighbour (peer);
2189 send_disconnect (peer, plugin_name, sender_address, sender_address_len,
2195 if (is_connected (n))
2198 if (!is_connecting(n))
2200 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2205 if (NULL != session)
2206 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2208 "Giving ATS session %p of plugin %s for peer %s\n",
2209 session, plugin_name, GNUNET_i2s (peer));
2210 GNUNET_ATS_address_update (GST_ats, peer, plugin_name, sender_address,
2211 sender_address_len, session, ats, ats_count);
2213 was_connected = is_connected (n);
2214 change_state (n, S_CONNECTED);
2216 GNUNET_ATS_address_in_use (GST_ats, &n->id, n->plugin_name, n->addr,
2217 n->addrlen, n->addr, GNUNET_YES);
2219 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2221 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2223 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2224 &neighbour_keepalive_task, n);
2228 GST_validation_set_address_use (&n->id,
2234 neighbours_connected++;
2235 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2240 "Notify about connect of `%4s' using plugin `%s' address '%s' session %X LINE %u\n",
2241 GNUNET_i2s (&n->id), n->plugin_name,
2243 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name, n->addr,
2244 n->addrlen), n->session,
2247 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2252 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2254 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2255 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2256 q_msg.quota = n->bandwidth_out;
2257 q_msg.peer = (*peer);
2258 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2261 struct BlackListCheckContext
2263 struct GNUNET_ATS_Information *ats;
2267 struct Session *session;
2269 char *sender_address;
2271 uint16_t sender_address_len;
2275 struct GNUNET_TIME_Absolute ts;
2280 handle_connect_blacklist_cont (void *cls,
2281 const struct GNUNET_PeerIdentity *peer,
2284 struct NeighbourMapEntry *n;
2285 struct BlackListCheckContext *bcc = cls;
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Blacklist check due to CONNECT message: `%s'\n",
2291 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2295 if (GNUNET_OK != result)
2301 n = lookup_neighbour (peer);
2303 n = setup_neighbour (peer);
2305 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2307 if (NULL != bcc->session)
2308 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2310 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
2311 bcc->session, bcc->plugin_name,
2312 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address,
2313 bcc->sender_address_len),
2315 GNUNET_ATS_address_update (GST_ats, peer, bcc->plugin_name,
2316 bcc->sender_address, bcc->sender_address_len,
2317 bcc->session, bcc->ats, bcc->ats_count);
2318 n->connect_ts = bcc->ts;
2323 if (n->state != S_CONNECT_RECV)
2324 change_state (n, S_CONNECT_RECV);
2326 /* Ask ATS for an address to connect via that address */
2327 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2328 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2330 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2332 GNUNET_ATS_suggest_address (GST_ats, peer);
2336 * We received a 'SESSION_CONNECT' message from the other peer.
2337 * Consider switching to it.
2339 * @param message possibly a 'struct SessionConnectMessage' (check format)
2340 * @param peer identity of the peer to switch the address for
2341 * @param plugin_name name of transport that delivered the PONG
2342 * @param address address of the other peer, NULL if other peer
2344 * @param address_len number of bytes in address
2345 * @param session session to use (or NULL)
2346 * @param ats performance data
2347 * @param ats_count number of entries in ats (excluding 0-termination)
2350 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2351 const struct GNUNET_PeerIdentity *peer,
2352 const char *plugin_name,
2353 const char *sender_address,
2354 uint16_t sender_address_len,
2355 struct Session *session,
2356 const struct GNUNET_ATS_Information *ats,
2359 const struct SessionConnectMessage *scm;
2360 struct NeighbourMapEntry *n;
2361 struct BlackListCheckContext *bcc = NULL;
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2368 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2370 GNUNET_break_op (0);
2374 scm = (const struct SessionConnectMessage *) message;
2375 GNUNET_break_op (ntohl (scm->reserved) == 0);
2377 n = lookup_neighbour (peer);
2380 /* connected peer switches addresses */
2381 if (is_connected (n))
2383 GNUNET_ATS_address_update (GST_ats, peer, plugin_name, sender_address,
2384 sender_address_len, session, ats, ats_count);
2389 /* we are not connected to this peer */
2390 /* do blacklist check */
2392 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2393 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1) +
2394 sender_address_len + strlen (plugin_name) + 1);
2396 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2398 bcc->ats_count = ats_count + 1;
2399 bcc->sender_address_len = sender_address_len;
2400 bcc->session = session;
2402 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2403 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2404 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2405 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2406 bcc->sender_address = (char *) &bcc->ats[ats_count + 1];
2407 memcpy (bcc->sender_address, sender_address, sender_address_len);
2409 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2410 strcpy (bcc->plugin_name, plugin_name);
2412 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont,
2417 /* end of file gnunet-service-transport_neighbours.c */