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)
60 * Entry in neighbours.
62 struct NeighbourMapEntry;
65 * Message a peer sends to another to indicate its
66 * preference for communicating via a particular
67 * session (and the desire to establish a real
70 struct SessionConnectMessage
73 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
75 struct GNUNET_MessageHeader header;
80 uint32_t reserved GNUNET_PACKED;
83 * Absolute time at the sender. Only the most recent connect
84 * message implies which session is preferred by the sender.
86 struct GNUNET_TIME_AbsoluteNBO timestamp;
91 struct SessionDisconnectMessage
94 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
96 struct GNUNET_MessageHeader header;
101 uint32_t reserved GNUNET_PACKED;
104 * Purpose of the signature. Extends over the timestamp.
105 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
107 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
110 * Absolute time at the sender. Only the most recent connect
111 * message implies which session is preferred by the sender.
113 struct GNUNET_TIME_AbsoluteNBO timestamp;
116 * Public key of the sender.
118 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
121 * Signature of the peer that sends us the disconnect. Only
122 * valid if the timestamp is AFTER the timestamp from the
123 * corresponding 'CONNECT' message.
125 struct GNUNET_CRYPTO_RsaSignature signature;
131 * For each neighbour we keep a list of messages
132 * that we still want to transmit to the neighbour.
138 * This is a doubly linked list.
140 struct MessageQueue *next;
143 * This is a doubly linked list.
145 struct MessageQueue *prev;
148 * Once this message is actively being transmitted, which
149 * neighbour is it associated with?
151 struct NeighbourMapEntry *n;
154 * Function to call once we're done.
156 GST_NeighbourSendContinuation cont;
164 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
165 * stuck together in memory. Allocated at the end of this struct.
167 const char *message_buf;
170 * Size of the message buf
172 size_t message_buf_size;
175 * At what time should we fail?
177 struct GNUNET_TIME_Absolute timeout;
183 /* fresh peer or completely disconnected */
185 /* sent CONNECT message to other peer, waiting for CONNECT_ACK */
187 /* received CONNECT message to other peer, sending CONNECT_ACK */
189 /* sent CONNECT_ACK message to other peer, wait for ACK or payload */
190 S_CONNECT_RECV_ACK_SENT = 8,
191 /* received ACK or payload */
193 /* Disconnect in progress */
198 * Entry in neighbours.
200 struct NeighbourMapEntry
204 * Head of list of messages we would like to send to this peer;
205 * must contain at most one message per client.
207 struct MessageQueue *messages_head;
210 * Tail of list of messages we would like to send to this peer; must
211 * contain at most one message per client.
213 struct MessageQueue *messages_tail;
216 * Performance data for the peer.
218 //struct GNUNET_ATS_Information *ats;
221 * Are we currently trying to send a message? If so, which one?
223 struct MessageQueue *is_active;
226 * Active session for communicating with the peer.
228 struct Session *session;
231 * Name of the plugin we currently use.
236 * Address used for communicating with the peer, NULL for inbound connections.
241 * Number of bytes in 'addr'.
246 * Identity of this neighbour.
248 struct GNUNET_PeerIdentity id;
251 * ID of task scheduled to run when this peer is about to
252 * time out (will free resources associated with the peer).
254 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
257 * ID of task scheduled to send keepalives.
259 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
262 * ID of task scheduled to run when we should try transmitting
263 * the head of the message queue.
265 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
268 * Tracker for inbound bandwidth.
270 struct GNUNET_BANDWIDTH_Tracker in_tracker;
273 * Inbound bandwidth from ATS, activated when connection is up
275 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
278 * Inbound bandwidth from ATS, activated when connection is up
280 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
283 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
285 struct GNUNET_TIME_Absolute connect_ts;
288 * How often has the other peer (recently) violated the inbound
289 * traffic limit? Incremented by 10 per violation, decremented by 1
290 * per non-violation (for each time interval).
292 unsigned int quota_violation_count;
295 * Number of values in 'ats' array.
297 //unsigned int ats_count;
301 * Do we currently consider this neighbour connected? (as far as
302 * the connect/disconnect callbacks are concerned)?
312 * All known neighbours and their HELLOs.
314 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
317 * Closure for connect_notify_cb and disconnect_notify_cb
319 static void *callback_cls;
322 * Function to call when we connected to a neighbour.
324 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
327 * Function to call when we disconnected from a neighbour.
329 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
332 * counter for connected neighbours
334 static int neighbours_connected;
337 * Lookup a neighbour entry in the neighbours hash map.
339 * @param pid identity of the peer to look up
340 * @return the entry, NULL if there is no existing record
342 static struct NeighbourMapEntry *
343 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
345 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
348 #define change_state(n, state, ...) change (n, state, __LINE__)
351 is_connecting (struct NeighbourMapEntry * n)
353 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
359 is_connected (struct NeighbourMapEntry * n)
361 if (n->state == S_CONNECTED)
367 is_disconnecting (struct NeighbourMapEntry * n)
369 if (n->state == S_DISCONNECT)
375 print_state (int state)
379 return "S_CONNECTED";
382 return "S_CONNECT_RECV";
384 case S_CONNECT_RECV_ACK_SENT:
385 return"S_CONNECT_RECV_ACK_SENT";
388 return "S_CONNECT_SENT";
391 return "S_DISCONNECT";
393 case S_NOT_CONNECTED:
394 return "S_NOT_CONNECTED";
404 change (struct NeighbourMapEntry * n, int state, int line)
406 char * old = strdup(print_state(n->state));
407 char * new = strdup(print_state(state));
409 /* allowed transitions */
410 int allowed = GNUNET_NO;
412 case S_NOT_CONNECTED:
413 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
414 (state == S_DISCONNECT))
416 allowed = GNUNET_YES;
421 if ((state == S_NOT_CONNECTED) || (state == S_DISCONNECT) ||
422 (state == S_CONNECTED))
424 allowed = GNUNET_YES;
429 if ((state == S_NOT_CONNECTED) || (state == S_CONNECTED) ||
430 (state == S_DISCONNECT) || /* FIXME SENT -> RECV ISSUE!*/ (state == S_CONNECT_RECV))
432 allowed = GNUNET_YES;
437 if (state == S_DISCONNECT)
439 allowed = GNUNET_YES;
445 if (state == S_NOT_CONNECTED)
447 allowed = GNUNET_YES;
457 if (allowed == GNUNET_NO)
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 "Illegal state transition from `%s' to `%s' in line %u \n",
465 return GNUNET_SYSERR;
469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
470 GNUNET_i2s (&n->id), n, old, new, line);
477 send_with_plugin ( const struct GNUNET_PeerIdentity * target,
481 struct GNUNET_TIME_Relative timeout,
482 struct Session * session,
483 const char * plugin_name,
487 GNUNET_TRANSPORT_TransmitContinuation cont,
491 struct GNUNET_TRANSPORT_PluginFunctions *papi;
492 size_t ret = GNUNET_SYSERR;
494 papi = GST_plugins_find (plugin_name);
498 cont (cont_cls, target, GNUNET_SYSERR);
499 return GNUNET_SYSERR;
502 ret = papi->send (papi->cls,
515 cont (cont_cls, target, GNUNET_SYSERR);
521 * Task invoked to start a transmission to another peer.
523 * @param cls the 'struct NeighbourMapEntry'
524 * @param tc scheduler context
527 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
531 * We're done with our transmission attempt, continue processing.
533 * @param cls the 'struct MessageQueue' of the message
534 * @param receiver intended receiver
535 * @param success whether it worked or not
538 transmit_send_continuation (void *cls,
539 const struct GNUNET_PeerIdentity *receiver,
542 struct MessageQueue *mq;
543 struct NeighbourMapEntry *n;
549 GNUNET_assert (n->is_active == mq);
551 if (success == GNUNET_YES)
553 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
554 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
559 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
560 (success == GNUNET_OK) ? "successful" : "FAILED");
562 if (NULL != mq->cont)
563 mq->cont (mq->cont_cls, success);
569 * Check the ready list for the given neighbour and if a plugin is
570 * ready for transmission (and if we have a message), do so!
572 * @param n target peer for which to transmit
575 try_transmission_to_peer (struct NeighbourMapEntry *n)
577 struct MessageQueue *mq;
578 struct GNUNET_TIME_Relative timeout;
581 if (n->is_active != NULL)
584 return; /* transmission already pending */
586 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
589 return; /* currently waiting for bandwidth */
591 while (NULL != (mq = n->messages_head))
593 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
594 if (timeout.rel_value > 0)
596 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
599 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
602 return; /* no more messages */
604 if (GST_plugins_find (n->plugin_name) == NULL)
609 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
613 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0))
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
616 GNUNET_i2s (&n->id));
617 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
618 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
619 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
623 ret = send_with_plugin (&n->id,
624 mq->message_buf, mq->message_buf_size, 0,
626 n->session, n->plugin_name, n->addr, n->addrlen,
628 &transmit_send_continuation, mq);
631 /* failure, but 'send' would not call continuation in this case,
632 * so we need to do it here! */
633 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
640 * Task invoked to start a transmission to another peer.
642 * @param cls the 'struct NeighbourMapEntry'
643 * @param tc scheduler context
646 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
648 struct NeighbourMapEntry *n = cls;
649 GNUNET_assert (NULL != lookup_neighbour(&n->id));
650 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
651 try_transmission_to_peer (n);
656 * Initialize the neighbours subsystem.
658 * @param cls closure for callbacks
659 * @param connect_cb function to call if we connect to a peer
660 * @param disconnect_cb function to call if we disconnect from a peer
663 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
664 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
667 connect_notify_cb = connect_cb;
668 disconnect_notify_cb = disconnect_cb;
669 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
674 send_disconnect_cont (void *cls,
675 const struct GNUNET_PeerIdentity * target,
678 struct NeighbourMapEntry *n = cls;
683 send_disconnect (struct NeighbourMapEntry *n)
686 struct SessionDisconnectMessage disconnect_msg;
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending DISCONNECT message to peer `%4s'\n",
690 GNUNET_i2s (&n->id));
693 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
694 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
695 disconnect_msg.reserved = htonl (0);
696 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
697 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
698 sizeof (struct GNUNET_TIME_AbsoluteNBO) );
699 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
700 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
701 disconnect_msg.public_key = GST_my_public_key;
702 GNUNET_assert (GNUNET_OK ==
703 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
704 &disconnect_msg.purpose,
705 &disconnect_msg.signature));
707 ret = send_with_plugin(&n->id,
708 (const char *) &disconnect_msg, sizeof (disconnect_msg),
709 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->plugin_name, n->addr, n->addrlen,
710 GNUNET_YES, NULL, NULL);
712 if (ret == GNUNET_SYSERR)
713 return GNUNET_SYSERR;
715 GNUNET_STATISTICS_update (GST_stats,
716 gettext_noop ("# peers disconnected due to external request"), 1,
722 * Disconnect from the given neighbour, clean up the record.
724 * @param n neighbour to disconnect from
727 disconnect_neighbour (struct NeighbourMapEntry *n)
729 struct MessageQueue *mq;
730 int was_connected = is_connected(n);
732 if (is_disconnecting(n) == GNUNET_YES)
735 /* send DISCONNECT MESSAGE */
736 if (is_connected(n) || is_connecting(n))
738 if (GNUNET_OK == send_disconnect(n))
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
740 GNUNET_i2s (&n->id));
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Could not send DISCONNECT_MSG to `%s'\n",
743 GNUNET_i2s (&n->id));
747 if (is_disconnecting(n))
749 change_state (n, S_DISCONNECT);
751 while (NULL != (mq = n->messages_head))
753 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
754 if (NULL != mq->cont)
755 mq->cont (mq->cont_cls, GNUNET_SYSERR);
758 if (NULL != n->is_active)
760 n->is_active->n = NULL;
765 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
766 GNUNET_SCHEDULER_cancel (n->keepalive_task);
767 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
768 GNUNET_assert (neighbours_connected > 0);
769 neighbours_connected--;
770 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
772 disconnect_notify_cb (callback_cls, &n->id);
774 GNUNET_assert (GNUNET_YES ==
775 GNUNET_CONTAINER_multihashmap_remove (neighbours,
776 &n->id.hashPubKey, n));
777 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
779 GNUNET_SCHEDULER_cancel (n->timeout_task);
780 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
782 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
784 GNUNET_SCHEDULER_cancel (n->transmission_task);
785 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
787 if (NULL != n->plugin_name)
789 GNUNET_free (n->plugin_name);
790 n->plugin_name = NULL;
794 GNUNET_free (n->addr);
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
800 GNUNET_i2s (&n->id), n);
806 * Peer has been idle for too long. Disconnect.
808 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
809 * @param tc scheduler context
812 neighbour_timeout_task (void *cls,
813 const struct GNUNET_SCHEDULER_TaskContext *tc)
815 struct NeighbourMapEntry *n = cls;
817 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
819 GNUNET_STATISTICS_update (GST_stats,
820 gettext_noop ("# peers disconnected due to timeout"), 1,
822 disconnect_neighbour (n);
827 * Send another keepalive message.
829 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
830 * @param tc scheduler context
833 neighbour_keepalive_task (void *cls,
834 const struct GNUNET_SCHEDULER_TaskContext *tc)
836 struct NeighbourMapEntry *n = cls;
837 struct GNUNET_MessageHeader m;
839 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
840 &neighbour_keepalive_task,
842 GNUNET_assert (is_connected(n));
843 GNUNET_STATISTICS_update (GST_stats,
844 gettext_noop ("# keepalives sent"), 1,
846 m.size = htons (sizeof (struct GNUNET_MessageHeader));
847 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
849 send_with_plugin(&n->id, (const void *) &m,
851 UINT32_MAX /* priority */ ,
852 GNUNET_TIME_UNIT_FOREVER_REL,
853 n->session, n->plugin_name, n->addr, n->addrlen,
854 GNUNET_YES, NULL, NULL);
859 * Disconnect from the given neighbour.
862 * @param key hash of neighbour's public key (not used)
863 * @param value the 'struct NeighbourMapEntry' of the neighbour
866 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
868 struct NeighbourMapEntry *n = value;
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
872 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
875 GNUNET_STATISTICS_update (GST_stats,
876 gettext_noop ("# peers disconnected due to global disconnect"), 1,
878 disconnect_neighbour (n);
884 * Cleanup the neighbours subsystem.
887 GST_neighbours_stop ()
889 GNUNET_assert (neighbours != NULL);
891 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
893 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
894 GNUNET_assert (neighbours_connected == 0);
897 connect_notify_cb = NULL;
898 disconnect_notify_cb = NULL;
903 * We tried to send a SESSION_CONNECT message to another peer. If this
904 * succeeded, we change the state. If it failed, we should tell
905 * ATS to not use this address anymore (until it is re-validated).
907 * @param cls the 'struct NeighbourMapEntry'
908 * @param success GNUNET_OK on success
911 send_connect_continuation (void *cls,
912 const struct GNUNET_PeerIdentity * target,
916 struct NeighbourMapEntry *n = cls;
918 GNUNET_assert (n != NULL);
919 GNUNET_assert (!is_connected(n));
921 if (is_disconnecting(n))
922 return; /* neighbour is going away */
923 if (GNUNET_YES != success)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
928 GNUNET_i2s (&n->id), n->plugin_name,
929 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
935 GNUNET_ATS_address_destroyed (GST_ats,
942 GNUNET_ATS_suggest_address(GST_ats, &n->id);
945 change_state(n, S_CONNECT_SENT);
950 * We tried to switch addresses with an peer already connected. If it failed,
951 * we should tell ATS to not use this address anymore (until it is re-validated).
953 * @param cls the 'struct NeighbourMapEntry'
954 * @param success GNUNET_OK on success
957 send_switch_address_continuation (void *cls,
958 const struct GNUNET_PeerIdentity * target,
962 struct NeighbourMapEntry *n = cls;
964 GNUNET_assert (n != NULL);
965 if (is_disconnecting(n))
966 return; /* neighbour is going away */
968 GNUNET_assert (n->state == S_CONNECTED);
969 if (GNUNET_YES != success)
972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
973 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
974 GNUNET_i2s (&n->id), n->plugin_name,
975 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Do not forget to fix this!\n");
982 /* FIXME: We have to change the state away from connected:
983 * If ATS can not suggest another address we do not get a callback
984 * but we still think we are connected
986 //change_state(n, S_NOT_CONNECTED);
988 GNUNET_ATS_address_destroyed (GST_ats,
995 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1001 * We tried to send a SESSION_CONNECT message to another peer. If this
1002 * succeeded, we change the state. If it failed, we should tell
1003 * ATS to not use this address anymore (until it is re-validated).
1005 * @param cls the 'struct NeighbourMapEntry'
1006 * @param success GNUNET_OK on success
1009 send_connect_ack_continuation (void *cls,
1010 const struct GNUNET_PeerIdentity * target,
1014 struct NeighbourMapEntry *n = cls;
1016 GNUNET_assert (n != NULL);
1018 if (GNUNET_YES == success)
1019 return; /* sending successful */
1021 /* sending failed, ask for next address */
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1025 GNUNET_i2s (&n->id), n->plugin_name,
1026 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1031 change_state(n, S_NOT_CONNECTED);
1033 GNUNET_ATS_address_destroyed (GST_ats,
1040 GNUNET_ATS_suggest_address(GST_ats, &n->id);
1044 * For an existing neighbour record, set the active connection to
1045 * the given address.
1047 * @param peer identity of the peer to switch the address for
1048 * @param plugin_name name of transport that delivered the PONG
1049 * @param address address of the other peer, NULL if other peer
1051 * @param address_len number of bytes in address
1052 * @param session session to use (or NULL)
1053 * @param ats performance data
1054 * @param ats_count number of entries in ats
1055 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1056 * connection is not up (yet)
1059 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1060 const char *plugin_name, const void *address,
1061 size_t address_len, struct Session *session,
1062 const struct GNUNET_ATS_Information
1063 *ats, uint32_t ats_count,
1064 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1065 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1067 struct NeighbourMapEntry *n;
1068 struct SessionConnectMessage connect_msg;
1072 GNUNET_assert (neighbours != NULL);
1073 n = lookup_neighbour (peer);
1076 if (NULL == session)
1077 GNUNET_ATS_address_destroyed (GST_ats,
1079 plugin_name, address,
1085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1086 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1088 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1091 session, (is_connected(n) ? "CONNECTED" : "NOT CONNECTED"),
1095 GNUNET_free_non_null (n->addr);
1096 n->addr = GNUNET_malloc (address_len);
1097 memcpy (n->addr, address, address_len);
1098 n->bandwidth_in = bandwidth_in;
1099 n->bandwidth_out = bandwidth_out;
1100 n->addrlen = address_len;
1101 n->session = session;
1102 GNUNET_free_non_null (n->plugin_name);
1103 n->plugin_name = GNUNET_strdup (plugin_name);
1104 GNUNET_SCHEDULER_cancel (n->timeout_task);
1106 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1107 &neighbour_timeout_task, n);
1109 if (n->state == S_DISCONNECT)
1111 /* We are disconnecting, nothing to do here */
1114 /* We are not connected/connecting and initiate a fresh connect */
1115 if (n->state == S_NOT_CONNECTED)
1117 msg_len = sizeof (struct SessionConnectMessage);
1118 connect_msg.header.size = htons (msg_len);
1119 connect_msg.header.type =
1120 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1121 connect_msg.reserved = htonl (0);
1122 connect_msg.timestamp =
1123 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1125 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, 0, GNUNET_TIME_UNIT_FOREVER_REL, session, plugin_name, address, address_len, GNUNET_YES, &send_connect_continuation, n);
1126 if (ret == GNUNET_SYSERR)
1128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1129 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1130 GNUNET_i2s (peer), plugin_name,
1131 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1138 /* We received a CONNECT message and asked ATS for an address */
1139 else if (n->state == S_CONNECT_RECV)
1141 msg_len = sizeof (struct SessionConnectMessage);
1142 connect_msg.header.size = htons (msg_len);
1143 connect_msg.header.type =
1144 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1145 connect_msg.reserved = htonl (0);
1146 connect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1148 ret = send_with_plugin(&n->id, (const void *) &connect_msg, msg_len, 0, GNUNET_TIME_UNIT_FOREVER_REL, session, plugin_name, address, address_len, GNUNET_YES, &send_connect_ack_continuation, n);
1149 if (ret == GNUNET_SYSERR)
1151 change_state (n, S_NOT_CONNECTED);
1156 /* connected peer is switching addresses */
1157 else if (n->state == S_CONNECTED)
1159 msg_len = sizeof (struct SessionConnectMessage);
1160 connect_msg.header.size = htons (msg_len);
1161 connect_msg.header.type =
1162 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1163 connect_msg.reserved = htonl (0);
1164 connect_msg.timestamp =
1165 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1167 ret = send_with_plugin (peer, (const char *) &connect_msg, msg_len, 0, GNUNET_TIME_UNIT_FOREVER_REL, session, plugin_name, address, address_len, GNUNET_YES, &send_switch_address_continuation, n);
1168 if (ret == GNUNET_SYSERR)
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Failed to send CONNECT_MESSAGE to `%4s' using plugin `%s' address '%s' session %X\n",
1172 GNUNET_i2s (peer), plugin_name,
1173 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
1181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid connection state to switch addresses %u ", n->state);
1182 GNUNET_break_op (0);
1188 * Create an entry in the neighbour map for the given peer
1190 * @param peer peer to create an entry for
1191 * @return new neighbour map entry
1193 static struct NeighbourMapEntry *
1194 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1196 struct NeighbourMapEntry *n;
1199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1200 "Unknown peer `%s', creating new neighbour\n",
1203 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1205 n->state = S_NOT_CONNECTED;
1206 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1207 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1208 MAX_BANDWIDTH_CARRY_S);
1210 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1211 &neighbour_timeout_task, n);
1212 GNUNET_assert (GNUNET_OK ==
1213 GNUNET_CONTAINER_multihashmap_put (neighbours,
1214 &n->id.hashPubKey, n,
1215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1221 * Try to create a connection to the given target (eventually).
1223 * @param target peer to try to connect to
1226 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1228 struct NeighbourMapEntry *n;
1230 GNUNET_assert (neighbours != NULL);
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1233 GNUNET_i2s (target));
1236 memcmp (target, &GST_my_identity,
1237 sizeof (struct GNUNET_PeerIdentity)));
1238 n = lookup_neighbour (target);
1242 if ((is_connected(n)) || (is_connecting(n)))
1243 return; /* already connecting or connected */
1244 if (is_disconnecting(n))
1245 change_state (n, S_NOT_CONNECTED);
1250 n = setup_neighbour (target);
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Asking ATS for suggested address to connect to peer `%s'\n",
1254 GNUNET_i2s (&n->id));
1256 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1261 * Test if we're connected to the given peer.
1263 * @param target peer to test
1264 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1267 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1269 struct NeighbourMapEntry *n;
1271 GNUNET_assert (neighbours != NULL);
1273 n = lookup_neighbour (target);
1275 if ((NULL == n) || (!is_connected(n)))
1276 return GNUNET_NO; /* not connected */
1282 * A session was terminated. Take note.
1284 * @param peer identity of the peer where the session died
1285 * @param session session that is gone
1288 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1289 struct Session *session)
1291 struct NeighbourMapEntry *n;
1293 GNUNET_assert (neighbours != NULL);
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Session %X to peer `%s' ended \n",
1298 session, GNUNET_i2s (peer));
1301 n = lookup_neighbour (peer);
1304 if (session != n->session)
1305 return; /* doesn't affect us */
1308 GNUNET_free (n->addr);
1312 /* not connected anymore anyway, shouldn't matter */
1313 if ((!is_connected(n)) && (!is_connecting(n)))
1316 // FIXME: switch address what is the state
1317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Do not forget to fix this!\n");
1319 /* We are connected, so ask ATS to switch addresses */
1320 GNUNET_SCHEDULER_cancel (n->timeout_task);
1322 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1323 &neighbour_timeout_task, n);
1324 /* try QUICKLY to re-establish a connection, reduce timeout! */
1325 GNUNET_ATS_suggest_address (GST_ats, peer);
1330 * Transmit a message to the given target using the active connection.
1332 * @param target destination
1333 * @param msg message to send
1334 * @param msg_size number of bytes in msg
1335 * @param timeout when to fail with timeout
1336 * @param cont function to call when done
1337 * @param cont_cls closure for 'cont'
1340 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1341 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1342 GST_NeighbourSendContinuation cont, void *cont_cls)
1344 struct NeighbourMapEntry *n;
1345 struct MessageQueue *mq;
1347 GNUNET_assert (neighbours != NULL);
1349 n = lookup_neighbour (target);
1350 if ((n == NULL) || (!is_connected(n)))
1352 GNUNET_STATISTICS_update (GST_stats,
1354 ("# messages not sent (no such peer or not connected)"),
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Could not send message to peer `%s': unknown neighbour",
1360 GNUNET_i2s (target));
1361 else if (!is_connected(n))
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 "Could not send message to peer `%s': not connected\n",
1364 GNUNET_i2s (target));
1367 cont (cont_cls, GNUNET_SYSERR);
1371 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
1373 GNUNET_STATISTICS_update (GST_stats,
1375 ("# messages not sent (no such peer or not connected)"),
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Could not send message to peer `%s': no address available\n",
1380 GNUNET_i2s (target));
1384 cont (cont_cls, GNUNET_SYSERR);
1388 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1389 GNUNET_STATISTICS_update (GST_stats,
1391 ("# bytes in message queue for other peers"),
1392 msg_size, GNUNET_NO);
1393 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1395 mq->cont_cls = cont_cls;
1396 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1397 memcpy (&mq[1], msg, msg_size);
1398 mq->message_buf = (const char *) &mq[1];
1399 mq->message_buf_size = msg_size;
1400 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1401 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1403 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1404 (NULL == n->is_active))
1405 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1410 * We have received a message from the given sender. How long should
1411 * we delay before receiving more? (Also used to keep the peer marked
1414 * @param sender sender of the message
1415 * @param size size of the message
1416 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1417 * GNUNET_NO if the neighbour is not connected or violates the quota,
1418 * GNUNET_SYSERR if the connection is not fully up yet
1419 * @return how long to wait before reading more from this sender
1421 struct GNUNET_TIME_Relative
1422 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1423 *sender, ssize_t size, int *do_forward)
1425 struct NeighbourMapEntry *n;
1426 struct GNUNET_TIME_Relative ret;
1428 GNUNET_assert (neighbours != NULL);
1430 n = lookup_neighbour (sender);
1433 GST_neighbours_try_connect (sender);
1434 n = lookup_neighbour (sender);
1437 GNUNET_STATISTICS_update (GST_stats,
1439 ("# messages discarded due to lack of neighbour record"),
1441 *do_forward = GNUNET_NO;
1442 return GNUNET_TIME_UNIT_ZERO;
1445 if (!is_connected(n))
1447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1448 _("Plugin gave us %d bytes of data but somehow the session is not marked as UP yet!\n"),
1450 *do_forward = GNUNET_SYSERR;
1451 return GNUNET_TIME_UNIT_ZERO;
1453 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1455 n->quota_violation_count++;
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1459 n->in_tracker.available_bytes_per_s__,
1460 n->quota_violation_count);
1462 /* Discount 32k per violation */
1463 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1467 if (n->quota_violation_count > 0)
1469 /* try to add 32k back */
1470 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1471 n->quota_violation_count--;
1474 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1476 GNUNET_STATISTICS_update (GST_stats,
1478 ("# bandwidth quota violations by other peers"),
1480 *do_forward = GNUNET_NO;
1481 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1483 *do_forward = GNUNET_YES;
1484 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1485 if (ret.rel_value > 0)
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1490 (unsigned long long) n->in_tracker.
1491 consumption_since_last_update__,
1492 (unsigned int) n->in_tracker.available_bytes_per_s__,
1493 (unsigned long long) ret.rel_value);
1495 GNUNET_STATISTICS_update (GST_stats,
1496 gettext_noop ("# ms throttling suggested"),
1497 (int64_t) ret.rel_value, GNUNET_NO);
1504 * Keep the connection to the given neighbour alive longer,
1505 * we received a KEEPALIVE (or equivalent).
1507 * @param neighbour neighbour to keep alive
1510 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1512 struct NeighbourMapEntry *n;
1514 GNUNET_assert (neighbours != NULL);
1516 n = lookup_neighbour (neighbour);
1519 GNUNET_STATISTICS_update (GST_stats,
1521 ("# KEEPALIVE messages discarded (not connected)"),
1525 GNUNET_SCHEDULER_cancel (n->timeout_task);
1527 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1528 &neighbour_timeout_task, n);
1533 * Change the incoming quota for the given peer.
1535 * @param neighbour identity of peer to change qutoa for
1536 * @param quota new quota
1539 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1540 struct GNUNET_BANDWIDTH_Value32NBO quota)
1542 struct NeighbourMapEntry *n;
1544 GNUNET_assert (neighbours != NULL);
1546 n = lookup_neighbour (neighbour);
1549 GNUNET_STATISTICS_update (GST_stats,
1551 ("# SET QUOTA messages ignored (no such peer)"),
1555 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1556 if (0 != ntohl (quota.value__))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1560 GNUNET_i2s (&n->id), "SET_QUOTA");
1562 if (is_connected(n))
1563 GNUNET_STATISTICS_update (GST_stats,
1564 gettext_noop ("# disconnects due to quota of 0"), 1,
1566 disconnect_neighbour (n);
1571 * Closure for the neighbours_iterate function.
1573 struct IteratorContext
1576 * Function to call on each connected neighbour.
1578 GST_NeighbourIterator cb;
1588 * Call the callback from the closure for each connected neighbour.
1590 * @param cls the 'struct IteratorContext'
1591 * @param key the hash of the public key of the neighbour
1592 * @param value the 'struct NeighbourMapEntry'
1593 * @return GNUNET_OK (continue to iterate)
1596 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1598 struct IteratorContext *ic = cls;
1599 struct NeighbourMapEntry *n = value;
1601 if (is_connected(n))
1604 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->plugin_name, n->addr, n->addrlen);
1610 * Iterate over all connected neighbours.
1612 * @param cb function to call
1613 * @param cb_cls closure for cb
1616 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1618 struct IteratorContext ic;
1620 GNUNET_assert (neighbours != NULL);
1624 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1628 * If we have an active connection to the given target, it must be shutdown.
1630 * @param target peer to disconnect from
1633 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1635 struct NeighbourMapEntry *n;
1637 GNUNET_assert (neighbours != NULL);
1639 n = lookup_neighbour (target);
1641 return; /* not active */
1642 if (is_connected(n))
1646 n = lookup_neighbour (target);
1648 return; /* gone already */
1650 disconnect_neighbour (n);
1655 * We received a disconnect message from the given peer,
1656 * validate and process.
1658 * @param peer sender of the message
1659 * @param msg the disconnect message
1662 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
1663 const struct GNUNET_MessageHeader *msg)
1665 struct NeighbourMapEntry *n;
1666 const struct SessionDisconnectMessage *sdm;
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1671 "Received DISCONNECT message from peer `%s'\n", GNUNET_i2s (peer));
1674 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1676 // GNUNET_break_op (0);
1677 GNUNET_STATISTICS_update (GST_stats,
1678 gettext_noop ("# disconnect messages ignored (old format)"), 1,
1682 sdm = (const struct SessionDisconnectMessage* ) msg;
1683 n = lookup_neighbour (peer);
1685 return; /* gone already */
1686 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1687 n->connect_ts.abs_value)
1689 GNUNET_STATISTICS_update (GST_stats,
1690 gettext_noop ("# disconnect messages ignored (timestamp)"), 1,
1694 GNUNET_CRYPTO_hash (&sdm->public_key,
1695 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1697 if (0 != memcmp (peer,
1699 sizeof (struct GNUNET_PeerIdentity)))
1701 GNUNET_break_op (0);
1704 if (ntohl (sdm->purpose.size) !=
1705 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1706 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1707 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1709 GNUNET_break_op (0);
1713 GNUNET_CRYPTO_rsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
1718 GNUNET_break_op (0);
1721 GST_neighbours_force_disconnect (peer);
1725 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1726 * Consider switching to it.
1728 * @param message possibly a 'struct SessionConnectMessage' (check format)
1729 * @param peer identity of the peer to switch the address for
1730 * @param plugin_name name of transport that delivered the PONG
1731 * @param address address of the other peer, NULL if other peer
1733 * @param address_len number of bytes in address
1734 * @param session session to use (or NULL)
1735 * @param ats performance data
1736 * @param ats_count number of entries in ats
1739 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1740 const struct GNUNET_PeerIdentity *peer,
1741 const char *plugin_name,
1742 const char *sender_address, uint16_t sender_address_len,
1743 struct Session *session,
1744 const struct GNUNET_ATS_Information *ats,
1747 const struct SessionConnectMessage *scm;
1748 struct QuotaSetMessage q_msg;
1749 struct GNUNET_MessageHeader msg;
1750 struct GNUNET_TIME_Absolute ts;
1751 struct NeighbourMapEntry *n;
1757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1758 "Received CONNECT_ACK message from peer `%s'\n", GNUNET_i2s (peer));
1761 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1763 GNUNET_break_op (0);
1767 scm = (const struct SessionConnectMessage *) message;
1768 GNUNET_break_op (ntohl (scm->reserved) == 0);
1769 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1770 n = lookup_neighbour (peer);
1772 n = setup_neighbour (peer);
1774 if (n->state != S_CONNECT_SENT)
1781 if (NULL != session)
1782 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1784 "Giving ATS session %p of plugin %s for peer %s\n",
1788 GNUNET_ATS_address_update (GST_ats,
1790 plugin_name, sender_address, sender_address_len,
1791 session, ats, ats_count);
1793 change_state (n, S_CONNECTED);
1796 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1797 "Setting inbound quota of %u for peer `%s' to \n",
1798 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
1800 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1802 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1803 &neighbour_keepalive_task,
1806 msg_len = sizeof (msg);
1807 msg.size = htons (msg_len);
1808 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
1810 ret = send_with_plugin (&n->id, (const char *) &msg, msg_len, 0,
1811 GNUNET_TIME_UNIT_FOREVER_REL,
1812 n->session, n->plugin_name, n->addr, n->addrlen,
1813 GNUNET_YES, NULL, NULL);
1815 if (ret == GNUNET_SYSERR)
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 "Failed to send SESSION_ACK to `%4s' using plugin `%s' address '%s' session %X\n",
1818 GNUNET_i2s (&n->id), n->plugin_name,
1819 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->plugin_name,
1824 neighbours_connected++;
1825 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1827 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1832 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
1834 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1835 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1836 q_msg.quota = n->bandwidth_out;
1837 q_msg.peer = (*peer);
1838 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1842 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
1843 const struct GNUNET_PeerIdentity *peer,
1844 const char *plugin_name,
1845 const char *sender_address, uint16_t sender_address_len,
1846 struct Session *session,
1847 const struct GNUNET_ATS_Information *ats,
1850 struct NeighbourMapEntry *n;
1851 struct QuotaSetMessage q_msg;
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 "Received ACK message from peer `%s'\n", GNUNET_i2s (peer));
1858 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
1860 GNUNET_break_op (0);
1864 n = lookup_neighbour (peer);
1871 // if (n->state != S_CONNECT_RECV)
1872 if (is_connecting(n))
1874 send_disconnect (n);
1875 change_state (n, S_DISCONNECT);
1880 if (is_connected(n))
1883 if (NULL != session)
1884 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1886 "Giving ATS session %p of plugin %s for peer %s\n",
1890 GNUNET_ATS_address_update (GST_ats,
1892 plugin_name, sender_address, sender_address_len,
1893 session, ats, ats_count);
1895 change_state (n, S_CONNECTED);
1897 GST_neighbours_set_incoming_quota(&n->id, n->bandwidth_in);
1899 n->keepalive_task = GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1900 &neighbour_keepalive_task,
1903 neighbours_connected++;
1904 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1906 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1911 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
1914 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1915 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1916 q_msg.quota = n->bandwidth_out;
1917 q_msg.peer = (*peer);
1918 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1921 struct BlackListCheckContext
1923 struct GNUNET_ATS_Information *ats;
1927 struct Session *session;
1929 char *sender_address;
1931 uint16_t sender_address_len;
1935 struct GNUNET_TIME_Absolute ts;
1940 handle_connect_blacklist_cont (void *cls,
1941 const struct GNUNET_PeerIdentity
1944 struct NeighbourMapEntry *n;
1945 struct BlackListCheckContext * bcc = cls;
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Blacklist check due to CONNECT message: `%s'\n", GNUNET_i2s (peer), (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
1953 if (GNUNET_OK != result)
1959 n = lookup_neighbour (peer);
1961 n = setup_neighbour (peer);
1963 if (bcc->ts.abs_value > n->connect_ts.abs_value)
1965 if (NULL != bcc->session)
1966 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1968 "Giving ATS session %p of plugin %s address `%s' for peer %s\n",
1971 GST_plugins_a2s (bcc->plugin_name, bcc->sender_address, bcc->sender_address_len),
1973 GNUNET_ATS_address_update (GST_ats,
1975 bcc->plugin_name, bcc->sender_address, bcc->sender_address_len,
1976 bcc->session, bcc->ats, bcc->ats_count);
1977 n->connect_ts = bcc->ts;
1981 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1982 "Blacklist check due to CONNECT message: `%s'\n");
1984 if (n->state != S_NOT_CONNECTED)
1986 change_state (n, S_CONNECT_RECV);
1988 /* Ask ATS for an address to connect via that address */
1989 GNUNET_ATS_suggest_address(GST_ats, peer);
1993 * We received a 'SESSION_CONNECT' message from the other peer.
1994 * Consider switching to it.
1996 * @param message possibly a 'struct SessionConnectMessage' (check format)
1997 * @param peer identity of the peer to switch the address for
1998 * @param plugin_name name of transport that delivered the PONG
1999 * @param address address of the other peer, NULL if other peer
2001 * @param address_len number of bytes in address
2002 * @param session session to use (or NULL)
2003 * @param ats performance data
2004 * @param ats_count number of entries in ats (excluding 0-termination)
2007 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2008 const struct GNUNET_PeerIdentity *peer,
2009 const char *plugin_name,
2010 const char *sender_address, uint16_t sender_address_len,
2011 struct Session *session,
2012 const struct GNUNET_ATS_Information *ats,
2015 const struct SessionConnectMessage *scm;
2016 struct NeighbourMapEntry * n;
2017 struct BlackListCheckContext * bcc = NULL;
2021 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2022 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2025 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2027 GNUNET_break_op (0);
2031 scm = (const struct SessionConnectMessage *) message;
2032 GNUNET_break_op (ntohl (scm->reserved) == 0);
2034 n = lookup_neighbour(peer);
2037 /* connected peer switches addresses */
2038 if (is_connected(n))
2040 GNUNET_ATS_address_update(GST_ats, peer, plugin_name, sender_address, sender_address_len, session, ats, ats_count);
2045 /* we are not connected to this peer */
2046 /* do blacklist check*/
2047 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2048 sizeof (struct GNUNET_ATS_Information) * ats_count +
2049 sender_address_len +
2050 strlen (plugin_name)+1);
2052 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2054 bcc->ats_count = ats_count;
2055 bcc->sender_address_len = sender_address_len;
2056 bcc->session = session;
2058 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2059 memcpy (bcc->ats, ats,sizeof (struct GNUNET_ATS_Information) * ats_count );
2061 bcc->sender_address = (char *) &bcc->ats[ats_count];
2062 memcpy (bcc->sender_address, sender_address , sender_address_len);
2064 bcc->plugin_name = &bcc->sender_address[sender_address_len];
2065 strcpy (bcc->plugin_name, plugin_name);
2067 GST_blacklist_test_allowed (peer, plugin_name, handle_connect_blacklist_cont, bcc);
2071 /* end of file gnunet-service-transport_neighbours.c */