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 and measure
52 * the latency with this neighbour?
53 * (idle timeout is 5 minutes or 300 seconds, so with 30s interval we
54 * send 10 keepalives in each interval, so 10 messages would need to be
55 * lost in a row for a disconnect).
57 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
59 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
61 #define FAST_RECONNECT_RATE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
63 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
65 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
68 * Entry in neighbours.
70 struct NeighbourMapEntry;
72 GNUNET_NETWORK_STRUCT_BEGIN
75 * Message a peer sends to another to indicate its
76 * preference for communicating via a particular
77 * session (and the desire to establish a real
80 struct SessionConnectMessage
83 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
85 struct GNUNET_MessageHeader header;
90 uint32_t reserved GNUNET_PACKED;
93 * Absolute time at the sender. Only the most recent connect
94 * message implies which session is preferred by the sender.
96 struct GNUNET_TIME_AbsoluteNBO timestamp;
101 struct SessionDisconnectMessage
104 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
106 struct GNUNET_MessageHeader header;
111 uint32_t reserved GNUNET_PACKED;
114 * Purpose of the signature. Extends over the timestamp.
115 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
117 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
120 * Absolute time at the sender. Only the most recent connect
121 * message implies which session is preferred by the sender.
123 struct GNUNET_TIME_AbsoluteNBO timestamp;
126 * Public key of the sender.
128 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
131 * Signature of the peer that sends us the disconnect. Only
132 * valid if the timestamp is AFTER the timestamp from the
133 * corresponding 'CONNECT' message.
135 struct GNUNET_CRYPTO_RsaSignature signature;
138 GNUNET_NETWORK_STRUCT_END
141 * For each neighbour we keep a list of messages
142 * that we still want to transmit to the neighbour.
148 * This is a doubly linked list.
150 struct MessageQueue *next;
153 * This is a doubly linked list.
155 struct MessageQueue *prev;
158 * Once this message is actively being transmitted, which
159 * neighbour is it associated with?
161 struct NeighbourMapEntry *n;
164 * Function to call once we're done.
166 GST_NeighbourSendContinuation cont;
174 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
175 * stuck together in memory. Allocated at the end of this struct.
177 const char *message_buf;
180 * Size of the message buf
182 size_t message_buf_size;
185 * At what time should we fail?
187 struct GNUNET_TIME_Absolute timeout;
195 * fresh peer or completely disconnected
200 * sent CONNECT message to other peer, waiting for CONNECT_ACK
205 * received CONNECT message to other peer, sending CONNECT_ACK
210 * received ACK or payload
215 * connection ended, fast reconnect
220 * Disconnect in progress
234 * Entry in neighbours.
236 struct NeighbourMapEntry
240 * Head of list of messages we would like to send to this peer;
241 * must contain at most one message per client.
243 struct MessageQueue *messages_head;
246 * Tail of list of messages we would like to send to this peer; must
247 * contain at most one message per client.
249 struct MessageQueue *messages_tail;
252 * Are we currently trying to send a message? If so, which one?
254 struct MessageQueue *is_active;
257 * Active session for communicating with the peer.
259 struct Session *session;
262 * Address we currently use.
264 struct GNUNET_HELLO_Address *address;
267 * Address we currently use.
269 struct GNUNET_HELLO_Address *fast_reconnect_address;
273 * Identity of this neighbour.
275 struct GNUNET_PeerIdentity id;
278 * ID of task scheduled to run when this peer is about to
279 * time out (will free resources associated with the peer).
281 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
284 * ID of task scheduled to send keepalives.
286 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
289 * ID of task scheduled to run when we should try transmitting
290 * the head of the message queue.
292 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
295 * Tracker for inbound bandwidth.
297 struct GNUNET_BANDWIDTH_Tracker in_tracker;
300 * Inbound bandwidth from ATS, activated when connection is up
302 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
305 * Inbound bandwidth from ATS, activated when connection is up
307 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
310 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
312 struct GNUNET_TIME_Absolute connect_ts;
315 * When did we sent the last keep-alive message?
317 struct GNUNET_TIME_Absolute keep_alive_sent;
320 * Latest calculated latency value
322 struct GNUNET_TIME_Relative latency;
326 * We asked ATS for a new address for this peer
328 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
331 * Delay for ATS request
333 GNUNET_SCHEDULER_TaskIdentifier ats_request;
336 * Task the resets the peer state after due to an pending
337 * unsuccessful connection setup
339 GNUNET_SCHEDULER_TaskIdentifier state_reset;
343 * How often has the other peer (recently) violated the inbound
344 * traffic limit? Incremented by 10 per violation, decremented by 1
345 * per non-violation (for each time interval).
347 unsigned int quota_violation_count;
351 * The current state of the peer
352 * Element of enum State
357 * Did we sent an KEEP_ALIVE message and are we expecting a response?
359 int expect_latency_response;
362 * Was the address used successfully
367 * Fast reconnect attempts for identical address
369 unsigned int fast_reconnect_attempts;
374 * All known neighbours and their HELLOs.
376 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
379 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
381 static void *callback_cls;
384 * Function to call when we connected to a neighbour.
386 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
389 * Function to call when we disconnected from a neighbour.
391 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
394 * Function to call when we changed an active address of a neighbour.
396 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
399 * counter for connected neighbours
401 static int neighbours_connected;
403 static unsigned int bytes_in_send_queue;
404 static unsigned int bytes_received;
407 * Lookup a neighbour entry in the neighbours hash map.
409 * @param pid identity of the peer to look up
410 * @return the entry, NULL if there is no existing record
412 static struct NeighbourMapEntry *
413 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
415 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
419 * Disconnect from the given neighbour, clean up the record.
421 * @param n neighbour to disconnect from
424 disconnect_neighbour (struct NeighbourMapEntry *n);
426 #define change_state(n, state, ...) change (n, state, __LINE__)
429 is_connecting (struct NeighbourMapEntry *n)
431 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
437 is_connected (struct NeighbourMapEntry *n)
439 if (n->state == S_CONNECTED)
445 is_disconnecting (struct NeighbourMapEntry *n)
447 if (n->state == S_DISCONNECT)
453 print_state (int state)
458 return "S_CONNECTED";
461 return "S_CONNECT_RECV";
464 return "S_CONNECT_SENT";
467 return "S_DISCONNECT";
469 case S_NOT_CONNECTED:
470 return "S_NOT_CONNECTED";
472 case S_FAST_RECONNECT:
473 return "S_FAST_RECONNECT";
483 change (struct NeighbourMapEntry *n, int state, int line);
486 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
490 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
492 struct NeighbourMapEntry *n = cls;
497 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
498 if (n->state == S_CONNECTED)
500 GNUNET_STATISTICS_update (GST_stats,
502 ("# failed connection attempts due to timeout"), 1,
504 /* resetting state */
506 if (n->state == S_FAST_RECONNECT)
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Fast reconnect time out, disconnecting peer `%s'\n",
510 GNUNET_i2s (&n->id));
511 disconnect_neighbour(n);
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
517 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
519 n->state = S_NOT_CONNECTED;
521 /* destroying address */
522 if (n->address != NULL)
524 GNUNET_assert (strlen (n->address->transport_name) > 0);
525 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
528 /* request new address */
529 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
530 GNUNET_SCHEDULER_cancel (n->ats_suggest);
532 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
534 GNUNET_ATS_suggest_address (GST_ats, &n->id);
538 change (struct NeighbourMapEntry *n, int state, int line)
541 /* allowed transitions */
542 int allowed = GNUNET_NO;
544 previous_state = n->state;
548 case S_NOT_CONNECTED:
549 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
550 (state == S_DISCONNECT))
551 allowed = GNUNET_YES;
554 allowed = GNUNET_YES;
557 allowed = GNUNET_YES;
560 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
561 allowed = GNUNET_YES;
565 case S_FAST_RECONNECT:
566 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
567 allowed = GNUNET_YES;
573 if (allowed == GNUNET_NO)
575 char *old = GNUNET_strdup (print_state (n->state));
576 char *new = GNUNET_strdup (print_state (state));
578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
579 "Illegal state transition from `%s' to `%s' in line %u \n", old,
584 return GNUNET_SYSERR;
587 char *old = GNUNET_strdup (print_state (n->state));
588 char *new = GNUNET_strdup (print_state (state));
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
592 GNUNET_i2s (&n->id), n, old, new, line);
600 case S_FAST_RECONNECT:
603 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
604 GNUNET_SCHEDULER_cancel (n->state_reset);
606 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
609 case S_NOT_CONNECTED:
611 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
614 char *old = GNUNET_strdup (print_state (n->state));
615 char *new = GNUNET_strdup (print_state (state));
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
619 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
623 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
624 GNUNET_SCHEDULER_cancel (n->state_reset);
625 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
633 if (NULL != address_change_cb)
635 if (n->state == S_CONNECTED)
636 address_change_cb (callback_cls, &n->id, n->address);
637 else if (previous_state == S_CONNECTED)
638 address_change_cb (callback_cls, &n->id, NULL);
645 send_with_session (struct NeighbourMapEntry *n,
646 const char *msgbuf, size_t msgbuf_size,
648 struct GNUNET_TIME_Relative timeout,
649 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
651 struct GNUNET_TRANSPORT_PluginFunctions *papi;
652 size_t ret = GNUNET_SYSERR;
654 GNUNET_assert (n != NULL);
655 GNUNET_assert (n->session != NULL);
657 papi = GST_plugins_find (n->address->transport_name);
661 cont (cont_cls, &n->id, GNUNET_SYSERR);
662 return GNUNET_SYSERR;
665 ret = papi->send (papi->cls,
672 if ((ret == -1) && (cont != NULL))
673 cont (cont_cls, &n->id, GNUNET_SYSERR);
678 * Task invoked to start a transmission to another peer.
680 * @param cls the 'struct NeighbourMapEntry'
681 * @param tc scheduler context
684 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
688 * We're done with our transmission attempt, continue processing.
690 * @param cls the 'struct MessageQueue' of the message
691 * @param receiver intended receiver
692 * @param success whether it worked or not
695 transmit_send_continuation (void *cls,
696 const struct GNUNET_PeerIdentity *receiver,
699 struct MessageQueue *mq = cls;
700 struct NeighbourMapEntry *n;
701 struct NeighbourMapEntry *tmp;
703 tmp = lookup_neighbour (receiver);
705 if ((NULL != n) && (tmp != NULL) && (tmp == n))
707 GNUNET_assert (n->is_active == mq);
709 if (success == GNUNET_YES)
711 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
712 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
716 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
717 bytes_in_send_queue -= mq->message_buf_size;
718 GNUNET_STATISTICS_set (GST_stats,
720 ("# bytes in message queue for other peers"),
721 bytes_in_send_queue, GNUNET_NO);
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
724 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
725 (success == GNUNET_OK) ? "successful" : "FAILED");
726 if (NULL != mq->cont)
727 mq->cont (mq->cont_cls, success);
733 * Check the ready list for the given neighbour and if a plugin is
734 * ready for transmission (and if we have a message), do so!
736 * @param n target peer for which to transmit
739 try_transmission_to_peer (struct NeighbourMapEntry *n)
741 struct MessageQueue *mq;
742 struct GNUNET_TIME_Relative timeout;
745 if (n->is_active != NULL)
748 return; /* transmission already pending */
750 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
753 return; /* currently waiting for bandwidth */
755 while (NULL != (mq = n->messages_head))
757 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
758 if (timeout.rel_value > 0)
760 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
763 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
766 return; /* no more messages */
768 if (n->address == NULL)
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
771 GNUNET_i2s (&n->id));
772 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
773 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
774 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
775 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
779 if (GST_plugins_find (n->address->transport_name) == NULL)
784 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
788 if ((n->address->address_length == 0) && (n->session == NULL))
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
791 GNUNET_i2s (&n->id));
792 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
793 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
794 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
798 ret = send_with_session(n,
799 mq->message_buf, mq->message_buf_size,
801 &transmit_send_continuation, mq);
805 /* failure, but 'send' would not call continuation in this case,
806 * so we need to do it here! */
807 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
814 * Task invoked to start a transmission to another peer.
816 * @param cls the 'struct NeighbourMapEntry'
817 * @param tc scheduler context
820 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
822 struct NeighbourMapEntry *n = cls;
824 GNUNET_assert (NULL != lookup_neighbour (&n->id));
825 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
826 try_transmission_to_peer (n);
831 * Initialize the neighbours subsystem.
833 * @param cls closure for callbacks
834 * @param connect_cb function to call if we connect to a peer
835 * @param disconnect_cb function to call if we disconnect from a peer
836 * @param peer_address_cb function to call if we change an active address
840 GST_neighbours_start (void *cls,
841 GNUNET_TRANSPORT_NotifyConnect connect_cb,
842 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
843 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
846 connect_notify_cb = connect_cb;
847 disconnect_notify_cb = disconnect_cb;
848 address_change_cb = peer_address_cb;
849 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
850 neighbours_connected = 0;
851 bytes_in_send_queue = 0;
857 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Sending DISCONNECT message to peer `%4s': %i\n",
862 GNUNET_i2s (target), result);
867 send_disconnect (struct NeighbourMapEntry * n)
870 struct SessionDisconnectMessage disconnect_msg;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Sending DISCONNECT message to peer `%4s'\n",
874 GNUNET_i2s (&n->id));
875 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
876 disconnect_msg.header.type =
877 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
878 disconnect_msg.reserved = htonl (0);
879 disconnect_msg.purpose.size =
880 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
881 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
882 sizeof (struct GNUNET_TIME_AbsoluteNBO));
883 disconnect_msg.purpose.purpose =
884 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
885 disconnect_msg.timestamp =
886 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
887 disconnect_msg.public_key = GST_my_public_key;
888 GNUNET_assert (GNUNET_OK ==
889 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
890 &disconnect_msg.purpose,
891 &disconnect_msg.signature));
893 ret = send_with_session (n,
894 (const char *) &disconnect_msg, sizeof (disconnect_msg),
895 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
896 &send_disconnect_cont, NULL);
898 if (ret == GNUNET_SYSERR)
899 return GNUNET_SYSERR;
901 GNUNET_STATISTICS_update (GST_stats,
903 ("# peers disconnected due to external request"), 1,
910 * Disconnect from the given neighbour, clean up the record.
912 * @param n neighbour to disconnect from
915 disconnect_neighbour (struct NeighbourMapEntry *n)
917 struct MessageQueue *mq;
920 previous_state = n->state;
922 if (is_disconnecting (n))
925 /* send DISCONNECT MESSAGE */
926 if (previous_state == S_CONNECTED)
928 if (GNUNET_OK == send_disconnect (n))
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
930 GNUNET_i2s (&n->id));
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Could not send DISCONNECT_MSG to `%s'\n",
934 GNUNET_i2s (&n->id));
937 change_state (n, S_DISCONNECT);
939 if (previous_state == S_CONNECTED)
941 GNUNET_assert (NULL != n->address);
942 if (n->address_state == USED)
944 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
945 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
946 n->address_state = UNUSED;
950 if (n->address != NULL)
952 struct GNUNET_TRANSPORT_PluginFunctions *papi;
954 papi = GST_plugins_find (n->address->transport_name);
956 papi->disconnect (papi->cls, &n->id);
958 while (NULL != (mq = n->messages_head))
960 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
961 if (NULL != mq->cont)
962 mq->cont (mq->cont_cls, GNUNET_SYSERR);
965 if (NULL != n->is_active)
967 n->is_active->n = NULL;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Failing transmission of active message due to disconnect\n");
970 if (NULL != n->is_active->cont)
971 n->is_active->cont (n->is_active->cont_cls, GNUNET_SYSERR);
972 GNUNET_free (n->is_active);
976 switch (previous_state)
979 GNUNET_assert (neighbours_connected > 0);
980 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
981 GNUNET_SCHEDULER_cancel (n->keepalive_task);
982 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
983 n->expect_latency_response = GNUNET_NO;
984 neighbours_connected--;
985 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
987 disconnect_notify_cb (callback_cls, &n->id);
989 case S_FAST_RECONNECT:
990 GNUNET_STATISTICS_update (GST_stats,
991 gettext_noop ("# fast reconnects failed"), 1,
993 disconnect_notify_cb (callback_cls, &n->id);
999 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1001 GNUNET_assert (GNUNET_YES ==
1002 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1003 &n->id.hashPubKey, n));
1004 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
1006 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1007 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1009 if (n->ats_request != GNUNET_SCHEDULER_NO_TASK)
1011 GNUNET_SCHEDULER_cancel (n->ats_request);
1012 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1015 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1017 GNUNET_SCHEDULER_cancel (n->timeout_task);
1018 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1020 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1022 GNUNET_SCHEDULER_cancel (n->transmission_task);
1023 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1025 if (NULL != n->fast_reconnect_address)
1027 GNUNET_HELLO_address_free (n->fast_reconnect_address);
1028 n->fast_reconnect_address = NULL;
1030 if (NULL != n->address)
1032 GNUNET_HELLO_address_free (n->address);
1036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1037 GNUNET_i2s (&n->id), n);
1043 * Peer has been idle for too long. Disconnect.
1045 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1046 * @param tc scheduler context
1049 neighbour_timeout_task (void *cls,
1050 const struct GNUNET_SCHEDULER_TaskContext *tc)
1052 struct NeighbourMapEntry *n = cls;
1054 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer`%4s' disconnected due to timeout\n",
1056 GNUNET_i2s (&n->id));
1058 GNUNET_STATISTICS_update (GST_stats,
1060 ("# peers disconnected due to timeout"), 1,
1062 disconnect_neighbour (n);
1067 * Send another keepalive message.
1069 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1070 * @param tc scheduler context
1073 neighbour_keepalive_task (void *cls,
1074 const struct GNUNET_SCHEDULER_TaskContext *tc)
1076 struct NeighbourMapEntry *n = cls;
1077 struct GNUNET_MessageHeader m;
1080 GNUNET_assert (S_CONNECTED == n->state);
1082 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1083 &neighbour_keepalive_task, n);
1085 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1087 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1088 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1090 ret = send_with_session (n,
1091 (const void *) &m, sizeof (m),
1092 UINT32_MAX /* priority */ ,
1093 GNUNET_TIME_UNIT_FOREVER_REL,
1096 n->expect_latency_response = GNUNET_NO;
1097 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1098 if (ret != GNUNET_SYSERR)
1100 n->expect_latency_response = GNUNET_YES;
1101 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1108 * Disconnect from the given neighbour.
1111 * @param key hash of neighbour's public key (not used)
1112 * @param value the 'struct NeighbourMapEntry' of the neighbour
1115 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1117 struct NeighbourMapEntry *n = value;
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1120 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1121 if (S_CONNECTED == n->state)
1122 GNUNET_STATISTICS_update (GST_stats,
1124 ("# peers disconnected due to global disconnect"),
1126 disconnect_neighbour (n);
1132 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1134 struct NeighbourMapEntry *n = cls;
1136 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "ATS did not suggested address to connect to peer `%s'\n",
1140 GNUNET_i2s (&n->id));
1142 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# ATS address suggestions failed"), 1,
1146 disconnect_neighbour (n);
1150 * Cleanup the neighbours subsystem.
1153 GST_neighbours_stop ()
1155 // This can happen during shutdown
1156 if (neighbours == NULL)
1161 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1163 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1164 // GNUNET_assert (neighbours_connected == 0);
1166 callback_cls = NULL;
1167 connect_notify_cb = NULL;
1168 disconnect_notify_cb = NULL;
1169 address_change_cb = NULL;
1172 struct ContinutionContext
1174 struct GNUNET_HELLO_Address *address;
1176 struct Session *session;
1180 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1181 struct GNUNET_BANDWIDTH_Value32NBO quota)
1183 struct QuotaSetMessage q_msg;
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1187 ntohl (quota.value__), GNUNET_i2s (target));
1188 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1189 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1190 q_msg.quota = quota;
1191 q_msg.peer = (*target);
1192 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1196 * We tried to send a SESSION_CONNECT message to another peer. If this
1197 * succeeded, we change the state. If it failed, we should tell
1198 * ATS to not use this address anymore (until it is re-validated).
1200 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1201 * @param target peer to send the message to
1202 * @param success GNUNET_OK on success
1205 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1208 struct ContinutionContext *cc = cls;
1209 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1211 if (GNUNET_YES != success)
1213 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1214 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1216 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1218 GNUNET_HELLO_address_free (cc->address);
1223 if ((GNUNET_YES == success) &&
1224 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1226 change_state (n, S_CONNECT_SENT);
1227 GNUNET_HELLO_address_free (cc->address);
1232 if ((GNUNET_NO == success) &&
1233 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1236 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1237 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1238 change_state (n, S_NOT_CONNECTED);
1239 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1240 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1242 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1244 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1246 GNUNET_HELLO_address_free (cc->address);
1252 ats_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1254 struct NeighbourMapEntry *n = cls;
1255 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1258 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1264 * We tried to switch addresses with an peer already connected. If it failed,
1265 * we should tell ATS to not use this address anymore (until it is re-validated).
1267 * @param cls the 'struct NeighbourMapEntry'
1268 * @param target peer to send the message to
1269 * @param success GNUNET_OK on success
1272 send_switch_address_continuation (void *cls,
1273 const struct GNUNET_PeerIdentity *target,
1276 struct ContinutionContext *cc = cls;
1277 struct NeighbourMapEntry *n;
1279 if (neighbours == NULL)
1281 GNUNET_HELLO_address_free (cc->address);
1283 return; /* neighbour is going away */
1286 n = lookup_neighbour (&cc->address->peer);
1287 if ((n == NULL) || (is_disconnecting (n)))
1289 GNUNET_HELLO_address_free (cc->address);
1291 return; /* neighbour is going away */
1294 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1295 if (GNUNET_YES != success)
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1299 "Failed to switch connected peer `%s' in state %s to address '%s' session %X, asking ATS for new address \n",
1300 GNUNET_i2s (&n->id), print_state(n->state), GST_plugins_a2s (cc->address), cc->session);
1302 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1303 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1305 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1307 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1308 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1311 if (n->state == S_FAST_RECONNECT)
1313 if (GNUNET_SCHEDULER_NO_TASK == n->ats_request)
1315 /* Throtteled ATS request for fast reconnect */
1316 struct GNUNET_TIME_Relative delay = GNUNET_TIME_relative_multiply(FAST_RECONNECT_RATE, n->fast_reconnect_attempts);
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318 "Fast reconnect attempt %u failed, delay ATS request for %llu ms\n", n->fast_reconnect_attempts, delay.rel_value);
1320 GNUNET_SCHEDULER_add_delayed (delay,
1327 /* Immediate ATS request for connected peers */
1329 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1331 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1333 GNUNET_HELLO_address_free (cc->address);
1337 /* Tell ATS that switching addresses was successful */
1341 if (n->address_state == FRESH)
1343 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1344 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1345 GNUNET_break (cc->session == n->session);
1346 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1347 n->address_state = USED;
1350 case S_FAST_RECONNECT:
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 "Successful fast reconnect to peer `%s'\n",
1353 GNUNET_i2s (&n->id));
1354 change_state (n, S_CONNECTED);
1355 neighbours_connected++;
1356 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
1359 if (n->address_state == FRESH)
1361 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1362 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1363 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1364 n->address_state = USED;
1367 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1369 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1371 /* Updating quotas */
1372 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1373 send_outbound_quota (target, n->bandwidth_out);
1378 GNUNET_HELLO_address_free (cc->address);
1384 * We tried to send a SESSION_CONNECT message to another peer. If this
1385 * succeeded, we change the state. If it failed, we should tell
1386 * ATS to not use this address anymore (until it is re-validated).
1388 * @param cls the 'struct NeighbourMapEntry'
1389 * @param target peer to send the message to
1390 * @param success GNUNET_OK on success
1393 send_connect_ack_continuation (void *cls,
1394 const struct GNUNET_PeerIdentity *target,
1397 struct ContinutionContext *cc = cls;
1398 struct NeighbourMapEntry *n;
1400 if (neighbours == NULL)
1402 GNUNET_HELLO_address_free (cc->address);
1404 return; /* neighbour is going away */
1407 n = lookup_neighbour (&cc->address->peer);
1408 if ((n == NULL) || (is_disconnecting (n)))
1410 GNUNET_HELLO_address_free (cc->address);
1412 return; /* neighbour is going away */
1415 if (GNUNET_YES == success)
1417 GNUNET_HELLO_address_free (cc->address);
1419 return; /* sending successful */
1422 /* sending failed, ask for next address */
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1425 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1426 if (n->state != S_NOT_CONNECTED)
1427 change_state (n, S_NOT_CONNECTED);
1428 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1429 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1431 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1432 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1434 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1436 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1437 GNUNET_HELLO_address_free (cc->address);
1443 * For an existing neighbour record, set the active connection to
1444 * use the given address.
1446 * @param peer identity of the peer to switch the address for
1447 * @param address address of the other peer, NULL if other peer
1449 * @param session session to use (or NULL)
1450 * @param ats performance data
1451 * @param ats_count number of entries in ats
1452 * @param bandwidth_in inbound quota to be used when connection is up
1453 * @param bandwidth_out outbound quota to be used when connection is up
1454 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1455 * connection is not up (yet)
1458 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1459 const struct GNUNET_HELLO_Address
1461 struct Session *session,
1462 const struct GNUNET_ATS_Information *ats,
1464 struct GNUNET_BANDWIDTH_Value32NBO
1466 struct GNUNET_BANDWIDTH_Value32NBO
1469 struct NeighbourMapEntry *n;
1470 struct SessionConnectMessage connect_msg;
1471 struct ContinutionContext *cc;
1475 if (neighbours == NULL)
1477 /* This can happen during shutdown */
1480 n = lookup_neighbour (peer);
1483 if (n->state == S_DISCONNECT)
1485 /* We are disconnecting, nothing to do here */
1488 GNUNET_assert (address->transport_name != NULL);
1489 if ((session == NULL) && (0 == address->address_length))
1491 GNUNET_break_op (0);
1492 /* FIXME: is this actually possible? When does this happen? */
1493 if (strlen (address->transport_name) > 0)
1494 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1495 GNUNET_ATS_suggest_address (GST_ats, peer);
1499 /* checks successful and neighbour != NULL */
1500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1501 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1502 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1505 print_state (n->state));
1507 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1509 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1510 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1514 if (n->state == S_FAST_RECONNECT)
1516 /* Throtteled fast reconnect */
1518 if (NULL == n->fast_reconnect_address)
1520 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1523 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1525 n->fast_reconnect_attempts ++;
1527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1528 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1529 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1534 n->fast_reconnect_attempts = 0;
1537 /* do not switch addresses just update quotas */
1538 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1539 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1540 (n->session == session))
1542 n->bandwidth_in = bandwidth_in;
1543 n->bandwidth_out = bandwidth_out;
1544 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1545 send_outbound_quota (peer, n->bandwidth_out);
1548 if (n->state == S_CONNECTED)
1550 /* mark old address as no longer used */
1551 GNUNET_assert (NULL != n->address);
1552 if (n->address_state == USED)
1554 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1555 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1556 n->address_state = UNUSED;
1560 /* set new address */
1561 if (NULL != n->address)
1562 GNUNET_HELLO_address_free (n->address);
1563 n->address = GNUNET_HELLO_address_copy (address);
1564 n->address_state = FRESH;
1565 n->bandwidth_in = bandwidth_in;
1566 n->bandwidth_out = bandwidth_out;
1567 GNUNET_SCHEDULER_cancel (n->timeout_task);
1569 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1570 &neighbour_timeout_task, n);
1572 if (NULL != address_change_cb && n->state == S_CONNECTED)
1573 address_change_cb (callback_cls, &n->id, n->address);
1575 /* Obtain an session for this address from plugin */
1576 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1577 papi = GST_plugins_find (address->transport_name);
1581 /* we don't have the plugin for this address */
1582 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1584 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1585 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1586 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1589 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1590 GNUNET_HELLO_address_free (n->address);
1596 if (session == NULL)
1598 n->session = papi->get_session (papi->cls, address);
1599 /* Session could not be initiated */
1600 if (n->session == NULL)
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1604 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1606 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1608 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1609 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1610 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1613 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1614 GNUNET_HELLO_address_free (n->address);
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Obtained new session %p for peer `%s' and address '%s'\n",
1622 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1623 /* Telling ATS about new session */
1624 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1628 n->session = session;
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1630 "Using existing session %p for peer `%s' and address '%s'\n",
1632 GNUNET_i2s (&n->id),
1633 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1638 case S_NOT_CONNECTED:
1639 case S_CONNECT_SENT:
1640 msg_len = sizeof (struct SessionConnectMessage);
1641 connect_msg.header.size = htons (msg_len);
1642 connect_msg.header.type =
1643 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1644 connect_msg.reserved = htonl (0);
1645 connect_msg.timestamp =
1646 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1648 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1649 cc->session = n->session;
1650 cc->address = GNUNET_HELLO_address_copy (address);
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Sending CONNECT message to %s\n",
1653 GNUNET_i2s (&n->id));
1654 if (n->state != S_CONNECT_SENT)
1655 change_state (n, S_CONNECT_SENT);
1656 ret = send_with_session (n,
1657 (const char *) &connect_msg, msg_len,
1658 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1659 &send_connect_continuation, cc);
1662 case S_CONNECT_RECV:
1663 /* We received a CONNECT message and asked ATS for an address */
1664 msg_len = sizeof (struct SessionConnectMessage);
1665 connect_msg.header.size = htons (msg_len);
1666 connect_msg.header.type =
1667 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1668 connect_msg.reserved = htonl (0);
1669 connect_msg.timestamp =
1670 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1671 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1672 cc->session = n->session;
1673 cc->address = GNUNET_HELLO_address_copy (address);
1675 ret = send_with_session(n,
1676 (const void *) &connect_msg, msg_len,
1677 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1678 &send_connect_ack_continuation,
1682 case S_FAST_RECONNECT:
1683 /* connected peer is switching addresses or tries fast reconnect */
1684 msg_len = sizeof (struct SessionConnectMessage);
1685 connect_msg.header.size = htons (msg_len);
1686 connect_msg.header.type =
1687 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1688 connect_msg.reserved = htonl (0);
1689 connect_msg.timestamp =
1690 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1691 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1692 cc->session = n->session;
1693 cc->address = GNUNET_HELLO_address_copy (address);
1694 ret = send_with_session(n,
1695 (const void *) &connect_msg, msg_len,
1696 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1697 &send_switch_address_continuation, cc);
1698 if (ret == GNUNET_SYSERR)
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1701 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1702 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1707 "Invalid connection state to switch addresses %u \n", n->state);
1708 GNUNET_break_op (0);
1715 * Obtain current latency information for the given neighbour.
1718 * @return observed latency of the address, FOREVER if the address was
1719 * never successfully validated
1721 struct GNUNET_TIME_Relative
1722 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1724 struct NeighbourMapEntry *n;
1726 n = lookup_neighbour (peer);
1727 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1728 return GNUNET_TIME_UNIT_FOREVER_REL;
1734 * Obtain current address information for the given neighbour.
1737 * @return address currently used
1739 struct GNUNET_HELLO_Address *
1740 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1742 struct NeighbourMapEntry *n;
1744 n = lookup_neighbour (peer);
1745 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1754 * Create an entry in the neighbour map for the given peer
1756 * @param peer peer to create an entry for
1757 * @return new neighbour map entry
1759 static struct NeighbourMapEntry *
1760 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1762 struct NeighbourMapEntry *n;
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1765 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1766 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1768 n->state = S_NOT_CONNECTED;
1769 n->latency = GNUNET_TIME_relative_get_forever ();
1770 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1771 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1772 MAX_BANDWIDTH_CARRY_S);
1774 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1775 &neighbour_timeout_task, n);
1776 GNUNET_assert (GNUNET_OK ==
1777 GNUNET_CONTAINER_multihashmap_put (neighbours,
1778 &n->id.hashPubKey, n,
1779 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1785 * Try to create a connection to the given target (eventually).
1787 * @param target peer to try to connect to
1790 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1792 struct NeighbourMapEntry *n;
1794 // This can happen during shutdown
1795 if (neighbours == NULL)
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1800 GNUNET_i2s (target));
1802 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1807 n = lookup_neighbour (target);
1811 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1812 return; /* already connecting or connected */
1813 if (is_disconnecting (n))
1814 change_state (n, S_NOT_CONNECTED);
1819 n = setup_neighbour (target);
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1821 "Asking ATS for suggested address to connect to peer `%s'\n",
1822 GNUNET_i2s (&n->id));
1823 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1827 * Test if we're connected to the given peer.
1829 * @param target peer to test
1830 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1833 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1835 struct NeighbourMapEntry *n;
1837 // This can happen during shutdown
1838 if (neighbours == NULL)
1843 n = lookup_neighbour (target);
1845 if ((NULL == n) || (S_CONNECTED != n->state))
1846 return GNUNET_NO; /* not connected */
1851 * A session was terminated. Take note.
1853 * @param peer identity of the peer where the session died
1854 * @param session session that is gone
1857 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1858 struct Session *session)
1860 struct NeighbourMapEntry *n;
1862 if (neighbours == NULL)
1864 /* This can happen during shutdown */
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %p to peer `%s' ended \n",
1868 session, GNUNET_i2s (peer));
1869 n = lookup_neighbour (peer);
1872 if (session != n->session)
1873 return; /* doesn't affect us */
1874 if (n->state == S_CONNECTED)
1876 if (n->address_state == USED)
1878 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1879 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1880 n->address_state = UNUSED;
1885 if (NULL != n->address)
1887 GNUNET_HELLO_address_free (n->address);
1892 /* not connected anymore anyway, shouldn't matter */
1893 if (S_CONNECTED != n->state)
1896 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1898 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1899 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1900 n->expect_latency_response = GNUNET_NO;
1903 /* connected, try fast reconnect */
1904 /* statistics "transport" : "# peers connected" -= 1
1905 * neighbours_connected -= 1
1906 * BUT: no disconnect_cb to notify clients about disconnect
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1912 GNUNET_assert (neighbours_connected > 0);
1913 change_state (n, S_FAST_RECONNECT);
1914 neighbours_connected--;
1915 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
1919 /* We are connected, so ask ATS to switch addresses */
1920 GNUNET_SCHEDULER_cancel (n->timeout_task);
1921 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1922 &neighbour_timeout_task, n);
1923 /* try QUICKLY to re-establish a connection, reduce timeout! */
1924 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1925 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1926 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1927 &ats_suggest_cancel,
1929 GNUNET_ATS_suggest_address (GST_ats, peer);
1934 * Transmit a message to the given target using the active connection.
1936 * @param target destination
1937 * @param msg message to send
1938 * @param msg_size number of bytes in msg
1939 * @param timeout when to fail with timeout
1940 * @param cont function to call when done
1941 * @param cont_cls closure for 'cont'
1944 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1945 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1946 GST_NeighbourSendContinuation cont, void *cont_cls)
1948 struct NeighbourMapEntry *n;
1949 struct MessageQueue *mq;
1951 // This can happen during shutdown
1952 if (neighbours == NULL)
1957 n = lookup_neighbour (target);
1958 if ((n == NULL) || (!is_connected (n)))
1960 GNUNET_STATISTICS_update (GST_stats,
1962 ("# messages not sent (no such peer or not connected)"),
1965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1966 "Could not send message to peer `%s': unknown neighbour",
1967 GNUNET_i2s (target));
1968 else if (!is_connected (n))
1969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1970 "Could not send message to peer `%s': not connected\n",
1971 GNUNET_i2s (target));
1973 cont (cont_cls, GNUNET_SYSERR);
1977 if ((n->session == NULL) && (n->address == NULL))
1979 GNUNET_STATISTICS_update (GST_stats,
1981 ("# messages not sent (no such peer or not connected)"),
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984 "Could not send message to peer `%s': no address available\n",
1985 GNUNET_i2s (target));
1987 cont (cont_cls, GNUNET_SYSERR);
1991 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1992 bytes_in_send_queue += msg_size;
1993 GNUNET_STATISTICS_set (GST_stats,
1995 ("# bytes in message queue for other peers"),
1996 bytes_in_send_queue, GNUNET_NO);
1997 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1999 mq->cont_cls = cont_cls;
2000 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2001 memcpy (&mq[1], msg, msg_size);
2002 mq->message_buf = (const char *) &mq[1];
2003 mq->message_buf_size = msg_size;
2004 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2005 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
2007 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
2008 (NULL == n->is_active))
2009 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
2014 * We have received a message from the given sender. How long should
2015 * we delay before receiving more? (Also used to keep the peer marked
2018 * @param sender sender of the message
2019 * @param size size of the message
2020 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
2021 * GNUNET_NO if the neighbour is not connected or violates the quota,
2022 * GNUNET_SYSERR if the connection is not fully up yet
2023 * @return how long to wait before reading more from this sender
2025 struct GNUNET_TIME_Relative
2026 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2027 *sender, ssize_t size, int *do_forward)
2029 struct NeighbourMapEntry *n;
2030 struct GNUNET_TIME_Relative ret;
2032 // This can happen during shutdown
2033 if (neighbours == NULL)
2035 return GNUNET_TIME_UNIT_FOREVER_REL;
2038 n = lookup_neighbour (sender);
2041 GST_neighbours_try_connect (sender);
2042 n = lookup_neighbour (sender);
2045 GNUNET_STATISTICS_update (GST_stats,
2047 ("# messages discarded due to lack of neighbour record"),
2049 *do_forward = GNUNET_NO;
2050 return GNUNET_TIME_UNIT_ZERO;
2053 if (!is_connected (n))
2055 *do_forward = GNUNET_SYSERR;
2056 return GNUNET_TIME_UNIT_ZERO;
2058 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2060 n->quota_violation_count++;
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2062 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2063 n->in_tracker.available_bytes_per_s__,
2064 n->quota_violation_count);
2065 /* Discount 32k per violation */
2066 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2070 if (n->quota_violation_count > 0)
2072 /* try to add 32k back */
2073 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2074 n->quota_violation_count--;
2077 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2079 GNUNET_STATISTICS_update (GST_stats,
2081 ("# bandwidth quota violations by other peers"),
2083 *do_forward = GNUNET_NO;
2084 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2086 *do_forward = GNUNET_YES;
2087 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2088 if (ret.rel_value > 0)
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2092 (unsigned long long) n->in_tracker.
2093 consumption_since_last_update__,
2094 (unsigned int) n->in_tracker.available_bytes_per_s__,
2095 (unsigned long long) ret.rel_value);
2096 GNUNET_STATISTICS_update (GST_stats,
2097 gettext_noop ("# ms throttling suggested"),
2098 (int64_t) ret.rel_value, GNUNET_NO);
2105 * Keep the connection to the given neighbour alive longer,
2106 * we received a KEEPALIVE (or equivalent).
2108 * @param neighbour neighbour to keep alive
2111 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2113 struct NeighbourMapEntry *n;
2115 // This can happen during shutdown
2116 if (neighbours == NULL)
2121 n = lookup_neighbour (neighbour);
2124 GNUNET_STATISTICS_update (GST_stats,
2126 ("# KEEPALIVE messages discarded (not connected)"),
2130 GNUNET_SCHEDULER_cancel (n->timeout_task);
2132 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2133 &neighbour_timeout_task, n);
2135 /* send reply to measure latency */
2136 if (S_CONNECTED != n->state)
2139 struct GNUNET_MessageHeader m;
2141 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2142 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2144 send_with_session(n,
2145 (const void *) &m, sizeof (m),
2147 GNUNET_TIME_UNIT_FOREVER_REL,
2152 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2155 * @param neighbour neighbour to keep alive
2156 * @param ats performance data
2157 * @param ats_count number of entries in ats
2160 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2161 const struct GNUNET_ATS_Information *ats,
2164 struct NeighbourMapEntry *n;
2165 struct GNUNET_ATS_Information *ats_new;
2168 if (neighbours == NULL)
2170 // This can happen during shutdown
2174 n = lookup_neighbour (neighbour);
2175 if ((NULL == n) || (n->state != S_CONNECTED))
2177 GNUNET_STATISTICS_update (GST_stats,
2179 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2183 if (n->expect_latency_response != GNUNET_YES)
2185 GNUNET_STATISTICS_update (GST_stats,
2187 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2191 n->expect_latency_response = GNUNET_NO;
2193 GNUNET_assert (n->keep_alive_sent.abs_value !=
2194 GNUNET_TIME_absolute_get_zero ().abs_value);
2196 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2197 GNUNET_TIME_absolute_get ());
2198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2199 GNUNET_i2s (&n->id), n->latency.rel_value);
2201 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2203 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2208 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2210 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2213 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2214 if (n->latency.rel_value > UINT32_MAX)
2215 latency = UINT32_MAX;
2217 latency = n->latency.rel_value;
2218 ats_new[ats_count].value = htonl (latency);
2220 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2222 GNUNET_free (ats_new);
2228 * Change the incoming quota for the given peer.
2230 * @param neighbour identity of peer to change qutoa for
2231 * @param quota new quota
2234 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2235 struct GNUNET_BANDWIDTH_Value32NBO quota)
2237 struct NeighbourMapEntry *n;
2239 // This can happen during shutdown
2240 if (neighbours == NULL)
2245 n = lookup_neighbour (neighbour);
2248 GNUNET_STATISTICS_update (GST_stats,
2250 ("# SET QUOTA messages ignored (no such peer)"),
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2256 ntohl (quota.value__), GNUNET_i2s (&n->id));
2257 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2258 if (0 != ntohl (quota.value__))
2260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2261 GNUNET_i2s (&n->id), "SET_QUOTA");
2262 if (is_connected (n))
2263 GNUNET_STATISTICS_update (GST_stats,
2264 gettext_noop ("# disconnects due to quota of 0"),
2266 disconnect_neighbour (n);
2271 * Closure for the neighbours_iterate function.
2273 struct IteratorContext
2276 * Function to call on each connected neighbour.
2278 GST_NeighbourIterator cb;
2288 * Call the callback from the closure for each connected neighbour.
2290 * @param cls the 'struct IteratorContext'
2291 * @param key the hash of the public key of the neighbour
2292 * @param value the 'struct NeighbourMapEntry'
2293 * @return GNUNET_OK (continue to iterate)
2296 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2298 struct IteratorContext *ic = cls;
2299 struct NeighbourMapEntry *n = value;
2301 if (!is_connected (n))
2304 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2310 * Iterate over all connected neighbours.
2312 * @param cb function to call
2313 * @param cb_cls closure for cb
2316 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2318 struct IteratorContext ic;
2320 // This can happen during shutdown
2321 if (neighbours == NULL)
2328 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2332 * If we have an active connection to the given target, it must be shutdown.
2334 * @param target peer to disconnect from
2337 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2339 struct NeighbourMapEntry *n;
2341 // This can happen during shutdown
2342 if (neighbours == NULL)
2347 n = lookup_neighbour (target);
2349 return; /* not active */
2350 disconnect_neighbour (n);
2355 * We received a disconnect message from the given peer,
2356 * validate and process.
2358 * @param peer sender of the message
2359 * @param msg the disconnect message
2362 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2364 const struct GNUNET_MessageHeader
2367 struct NeighbourMapEntry *n;
2368 const struct SessionDisconnectMessage *sdm;
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "Received DISCONNECT message from peer `%s'\n",
2374 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2376 // GNUNET_break_op (0);
2377 GNUNET_STATISTICS_update (GST_stats,
2379 ("# disconnect messages ignored (old format)"), 1,
2383 sdm = (const struct SessionDisconnectMessage *) msg;
2384 n = lookup_neighbour (peer);
2386 return; /* gone already */
2387 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2388 n->connect_ts.abs_value)
2390 GNUNET_STATISTICS_update (GST_stats,
2392 ("# disconnect messages ignored (timestamp)"), 1,
2396 GNUNET_CRYPTO_hash (&sdm->public_key,
2397 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2399 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2401 GNUNET_break_op (0);
2404 if (ntohl (sdm->purpose.size) !=
2405 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2406 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2407 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2409 GNUNET_break_op (0);
2413 GNUNET_CRYPTO_rsa_verify
2414 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2415 &sdm->signature, &sdm->public_key))
2417 GNUNET_break_op (0);
2420 GST_neighbours_force_disconnect (peer);
2425 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2426 * Consider switching to it.
2428 * @param message possibly a 'struct SessionConnectMessage' (check format)
2429 * @param peer identity of the peer to switch the address for
2430 * @param address address of the other peer, NULL if other peer
2432 * @param session session to use (or NULL)
2433 * @param ats performance data
2434 * @param ats_count number of entries in ats
2437 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2438 const struct GNUNET_PeerIdentity *peer,
2439 const struct GNUNET_HELLO_Address *address,
2440 struct Session *session,
2441 const struct GNUNET_ATS_Information *ats,
2444 const struct SessionConnectMessage *scm;
2445 struct GNUNET_MessageHeader msg;
2446 struct NeighbourMapEntry *n;
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2451 "Received CONNECT_ACK message from peer `%s'\n",
2454 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2456 GNUNET_break_op (0);
2459 scm = (const struct SessionConnectMessage *) message;
2460 GNUNET_break_op (ntohl (scm->reserved) == 0);
2461 n = lookup_neighbour (peer);
2464 /* we did not send 'CONNECT' -- at least not recently */
2465 GNUNET_STATISTICS_update (GST_stats,
2467 ("# unexpected CONNECT_ACK messages (no peer)"),
2474 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2476 * We also received an CONNECT message, switched from SENDT to RECV and
2477 * ATS already suggested us an address after a successful blacklist check
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2483 print_state(n->state));
2485 if ((n->address != NULL) && (n->state == S_CONNECTED))
2487 /* After fast reconnect: send ACK (ACK) even when we are connected */
2488 msg_len = sizeof (msg);
2489 msg.size = htons (msg_len);
2490 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2492 ret = send_with_session(n,
2493 (const char *) &msg, msg_len,
2494 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2497 if (ret == GNUNET_SYSERR)
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2500 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2504 if ((NULL == n->address) ||
2505 ((n->state != S_CONNECT_SENT) &&
2506 (n->state != S_CONNECT_RECV)))
2508 GNUNET_STATISTICS_update (GST_stats,
2510 ("# unexpected CONNECT_ACK messages"), 1,
2514 if (n->state != S_CONNECTED)
2515 change_state (n, S_CONNECTED);
2517 if (NULL != session)
2519 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2521 "Giving ATS session %p of plugin %s for peer %s\n",
2522 session, address->transport_name, GNUNET_i2s (peer));
2524 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2525 GNUNET_assert (NULL != n->address);
2527 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2529 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2530 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2531 n->address_state = USED;
2534 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2536 /* send ACK (ACK) */
2537 msg_len = sizeof (msg);
2538 msg.size = htons (msg_len);
2539 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2541 ret = send_with_session(n,
2542 (const char *) &msg, msg_len,
2543 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2546 if (ret == GNUNET_SYSERR)
2547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2549 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2552 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2553 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2555 neighbours_connected++;
2556 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2560 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2562 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2563 send_outbound_quota (peer, n->bandwidth_out);
2569 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2570 const struct GNUNET_PeerIdentity *peer,
2571 const struct GNUNET_HELLO_Address *address,
2572 struct Session *session,
2573 const struct GNUNET_ATS_Information *ats,
2576 struct NeighbourMapEntry *n;
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2580 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2582 GNUNET_break_op (0);
2585 n = lookup_neighbour (peer);
2591 if (S_CONNECTED == n->state)
2593 if (!is_connecting (n))
2595 GNUNET_STATISTICS_update (GST_stats,
2596 gettext_noop ("# unexpected ACK messages"), 1,
2600 change_state (n, S_CONNECTED);
2601 if (NULL != session)
2602 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2604 "Giving ATS session %p of plugin %s for peer %s\n",
2605 session, address->transport_name, GNUNET_i2s (peer));
2606 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2607 GNUNET_assert (n->address != NULL);
2609 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2611 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2612 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2613 n->address_state = USED;
2617 neighbours_connected++;
2618 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2621 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2622 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2623 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2626 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2628 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2629 send_outbound_quota (peer, n->bandwidth_out);
2632 struct BlackListCheckContext
2634 struct GNUNET_ATS_Information *ats;
2638 struct Session *session;
2640 struct GNUNET_HELLO_Address *address;
2642 struct GNUNET_TIME_Absolute ts;
2647 handle_connect_blacklist_cont (void *cls,
2648 const struct GNUNET_PeerIdentity *peer,
2651 struct NeighbourMapEntry *n;
2652 struct BlackListCheckContext *bcc = cls;
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 "Blacklist check due to CONNECT message: `%s'\n",
2657 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2659 if (GNUNET_OK != result)
2661 GNUNET_HELLO_address_free (bcc->address);
2666 n = lookup_neighbour (peer);
2668 n = setup_neighbour (peer);
2670 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2672 if (NULL != bcc->session)
2673 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2675 "Giving ATS session %p of address `%s' for peer %s\n",
2676 bcc->session, GST_plugins_a2s (bcc->address),
2678 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2680 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2682 n->connect_ts = bcc->ts;
2685 GNUNET_HELLO_address_free (bcc->address);
2688 if (n->state != S_CONNECT_RECV)
2689 change_state (n, S_CONNECT_RECV);
2692 /* Ask ATS for an address to connect via that address */
2693 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2694 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2696 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2698 GNUNET_ATS_suggest_address (GST_ats, peer);
2702 * We received a 'SESSION_CONNECT' message from the other peer.
2703 * Consider switching to it.
2705 * @param message possibly a 'struct SessionConnectMessage' (check format)
2706 * @param peer identity of the peer to switch the address for
2707 * @param address address of the other peer, NULL if other peer
2709 * @param session session to use (or NULL)
2710 * @param ats performance data
2711 * @param ats_count number of entries in ats (excluding 0-termination)
2714 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2715 const struct GNUNET_PeerIdentity *peer,
2716 const struct GNUNET_HELLO_Address *address,
2717 struct Session *session,
2718 const struct GNUNET_ATS_Information *ats,
2721 const struct SessionConnectMessage *scm;
2722 struct BlackListCheckContext *bcc = NULL;
2723 struct NeighbourMapEntry *n;
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2727 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2729 GNUNET_break_op (0);
2733 scm = (const struct SessionConnectMessage *) message;
2734 GNUNET_break_op (ntohl (scm->reserved) == 0);
2736 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2738 n = lookup_neighbour (peer);
2739 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2741 /* connected peer switches addresses or is trying to do a fast reconnect*/
2746 /* we are not connected to this peer */
2747 /* do blacklist check */
2749 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2750 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2751 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2752 bcc->ats_count = ats_count + 1;
2753 bcc->address = GNUNET_HELLO_address_copy (address);
2754 bcc->session = session;
2755 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2756 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2757 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2758 bcc->ats[ats_count].value =
2759 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2760 GST_blacklist_test_allowed (peer, address->transport_name,
2761 &handle_connect_blacklist_cont, bcc);
2765 /* end of file gnunet-service-transport_neighbours.c */