2 This file is part of GNUnet.
3 (C) 2010-2013 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 * - "address_change_cb" is NEVER invoked; when should we call this one exactly?
28 * - TEST, TEST, TEST...
31 #include "gnunet_ats_service.h"
32 #include "gnunet-service-transport_neighbours.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport_clients.h"
36 #include "gnunet-service-transport.h"
37 #include "gnunet_peerinfo_service.h"
38 #include "gnunet-service-transport_blacklist.h"
39 #include "gnunet_constants.h"
40 #include "transport.h"
45 * Size of the neighbour hash map.
47 #define NEIGHBOUR_TABLE_SIZE 256
50 * Time we give plugin to transmit DISCONNECT message before the
51 * neighbour entry self-destructs.
53 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
56 * How often must a peer violate bandwidth quotas before we start
57 * to simply drop its messages?
59 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
62 * How often do we send KEEPALIVE messages to each of our neighbours and measure
63 * the latency with this neighbour?
64 * (idle timeout is 5 minutes or 300 seconds, so with 100s interval we
65 * send 3 keepalives in each interval, so 3 messages would need to be
66 * lost in a row for a disconnect).
68 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
71 * How long are we willing to wait for a response from ATS before timing out?
73 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
76 * How long are we willing to wait for an ACK from the other peer before
77 * giving up on our connect operation?
79 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
82 * How long are we willing to wait for a successful reconnect if
83 * an existing connection went down? Much shorter than the
84 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
85 * higher layers about the disconnect during this period.
87 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
90 * How long are we willing to wait for a response from the blacklist
91 * subsystem before timing out?
93 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
96 * Interval to send utilization data
98 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
100 GNUNET_NETWORK_STRUCT_BEGIN
103 * Message a peer sends to another to indicate that it intends to
104 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
105 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
106 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
107 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
108 * should be connected.
110 struct SessionConnectMessage
113 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
114 * or 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK'
116 struct GNUNET_MessageHeader header;
121 uint32_t reserved GNUNET_PACKED;
124 * Absolute time at the sender. Only the most recent connect
125 * message implies which session is preferred by the sender.
127 struct GNUNET_TIME_AbsoluteNBO timestamp;
133 * Message we send to the other peer to notify him that we intentionally
134 * are disconnecting (to reduce timeouts). This is just a friendly
135 * notification, peers must not rely on always receiving disconnect
138 struct SessionDisconnectMessage
141 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
143 struct GNUNET_MessageHeader header;
148 uint32_t reserved GNUNET_PACKED;
151 * Purpose of the signature. Extends over the timestamp.
152 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
154 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
157 * Absolute time at the sender. Only the most recent connect
158 * message implies which session is preferred by the sender.
160 struct GNUNET_TIME_AbsoluteNBO timestamp;
163 * Public key of the sender.
165 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
168 * Signature of the peer that sends us the disconnect. Only
169 * valid if the timestamp is AFTER the timestamp from the
170 * corresponding 'CONNECT' message.
172 struct GNUNET_CRYPTO_EddsaSignature signature;
176 GNUNET_NETWORK_STRUCT_END
180 * For each neighbour we keep a list of messages
181 * that we still want to transmit to the neighbour.
187 * This is a doubly linked list.
189 struct MessageQueue *next;
192 * This is a doubly linked list.
194 struct MessageQueue *prev;
197 * Function to call once we're done.
199 GST_NeighbourSendContinuation cont;
207 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
208 * stuck together in memory. Allocated at the end of this struct.
210 const char *message_buf;
213 * Size of the message buf
215 size_t message_buf_size;
218 * At what time should we fail?
220 struct GNUNET_TIME_Absolute timeout;
226 * Possible state of a neighbour. Initially, we are S_NOT_CONNECTED.
228 * Then, there are two main paths. If we receive a CONNECT message, we
229 * first run a check against the blacklist (S_CONNECT_RECV_BLACKLIST_INBOUND).
230 * If this check is successful, we give the inbound address to ATS.
231 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
232 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
233 * (S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
234 * address we got from ATS, we send our CONNECT_ACK and go to
235 * S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
236 * S_CONNECTED (and notify everyone about the new connection). If the
237 * operation times out, we go to S_DISCONNECT.
239 * The other case is where we transmit a CONNECT message first. We
240 * start with S_INIT_ATS. If we get an address, we enter
241 * S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
242 * with the connection, we actually send the CONNECT message and go to
243 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
244 * S_CONNECTED (and notify everyone about the new connection and send
245 * back a SESSION_ACK). If the operation times out, we go to
248 * If the session is in trouble (i.e. transport-level disconnect or
249 * timeout), we go to S_RECONNECT_ATS where we ask ATS for a new
250 * address (we don't notify anyone about the disconnect yet). Once we
251 * have a new address, we go to S_RECONNECT_BLACKLIST to check the new
252 * address against the blacklist. If the blacklist approves, we enter
253 * S_RECONNECT_SENT and send a CONNECT message. If we receive a
254 * CONNECT_ACK, we go to S_CONNECTED and nobody noticed that we had
255 * trouble; we also send a SESSION_ACK at this time just in case. If
256 * the operation times out, we go to S_DISCONNECT (and notify everyone
257 * about the lost connection).
259 * If ATS decides to switch addresses while we have a normal
260 * connection, we go to S_CONNECTED_SWITCHING_BLACKLIST to check the
261 * new address against the blacklist. If the blacklist approves, we
262 * go to S_CONNECTED_SWITCHING_CONNECT_SENT and send a
263 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
264 * primary connection to the suggested alternative from ATS, go back
265 * to S_CONNECTED and send a SESSION_ACK to the other peer just to be
266 * sure. If the operation times out (or the blacklist disapproves),
267 * we go to S_CONNECTED (and notify ATS that the given alternative
268 * address is "invalid").
270 * Once a session is in S_DISCONNECT, it is cleaned up and then goes
271 * to (S_DISCONNECT_FINISHED). If we receive an explicit disconnect
272 * request, we can go from any state to S_DISCONNECT, possibly after
273 * generating disconnect notifications.
275 * Note that it is quite possible that while we are in any of these
276 * states, we could receive a 'CONNECT' request from the other peer.
277 * We then enter a 'weird' state where we pursue our own primary state
278 * machine (as described above), but with the 'send_connect_ack' flag
279 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
280 * (because we have an acceptable address), we send the 'CONNECT_ACK'
281 * and set the 'send_connect_ack' to 2. If we then receive a
282 * 'SESSION_ACK', we go to 'S_CONNECTED' (and reset 'send_connect_ack'
289 * fresh peer or completely disconnected
294 * Asked to initiate connection, trying to get address from ATS
299 * Asked to initiate connection, trying to get address approved
305 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
310 * Received a CONNECT, do a blacklist check for inbound address
312 S_CONNECT_RECV_BLACKLIST_INBOUND,
315 * Received a CONNECT, asking ATS about address suggestions.
320 * Received CONNECT from other peer, got an address, checking with blacklist.
322 S_CONNECT_RECV_BLACKLIST,
325 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
331 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
336 * Connection got into trouble, rest of the system still believes
337 * it to be up, but we're getting a new address from ATS.
342 * Connection got into trouble, rest of the system still believes
343 * it to be up; we are checking the new address against the blacklist.
345 S_RECONNECT_BLACKLIST,
348 * Sent CONNECT over new address (either by ATS telling us to switch
349 * addresses or from RECONNECT_ATS); if this fails, we need to tell
350 * the rest of the system about a disconnect.
355 * We have some primary connection, but ATS suggested we switch
356 * to some alternative; we're now checking the alternative against
359 S_CONNECTED_SWITCHING_BLACKLIST,
362 * We have some primary connection, but ATS suggested we switch
363 * to some alternative; we now sent a CONNECT message for the
364 * alternative session to the other peer and waiting for a
365 * CONNECT_ACK to make this our primary connection.
367 S_CONNECTED_SWITCHING_CONNECT_SENT,
370 * Disconnect in progress (we're sending the DISCONNECT message to the
371 * other peer; after that is finished, the state will be cleaned up).
376 * We're finished with the disconnect; and are cleaning up the state
377 * now! We put the struct into this state when we are really in the
378 * task that calls 'free' on it and are about to remove the record
379 * from the map. We should never find a 'struct NeighbourMapEntry'
380 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
381 * in this state virtually always means using memory that has been
382 * freed (the exception being the cleanup code in 'free_neighbour').
384 S_DISCONNECT_FINISHED
389 * A possible address we could use to communicate with a neighbour.
391 struct NeighbourAddress
395 * Active session for this address.
397 struct Session *session;
400 * Network-level address information.
402 struct GNUNET_HELLO_Address *address;
405 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
406 * peer for this address. Use to check that the ACK is in response
407 * to our most recent 'CONNECT'.
409 struct GNUNET_TIME_Absolute connect_timestamp;
412 * Inbound bandwidth from ATS for this address.
414 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
417 * Outbound bandwidth from ATS for this address.
419 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
422 * Did we tell ATS that this is our 'active' address?
430 * Entry in neighbours.
432 struct NeighbourMapEntry
436 * Head of list of messages we would like to send to this peer;
437 * must contain at most one message per client.
439 struct MessageQueue *messages_head;
442 * Tail of list of messages we would like to send to this peer; must
443 * contain at most one message per client.
445 struct MessageQueue *messages_tail;
448 * Are we currently trying to send a message? If so, which one?
450 struct MessageQueue *is_active;
453 * Primary address we currently use to communicate with the neighbour.
455 struct NeighbourAddress primary_address;
458 * Alternative address currently under consideration for communicating
459 * with the neighbour.
461 struct NeighbourAddress alternative_address;
464 * Identity of this neighbour.
466 struct GNUNET_PeerIdentity id;
469 * Main task that drives this peer (timeouts, keepalives, etc.).
470 * Always runs the 'master_task'.
472 GNUNET_SCHEDULER_TaskIdentifier task;
475 * At what time should we sent the next keep-alive message?
477 struct GNUNET_TIME_Absolute keep_alive_time;
480 * At what time did we sent the last keep-alive message? Used
481 * to calculate round-trip time ("latency").
483 struct GNUNET_TIME_Absolute last_keep_alive_time;
486 * Timestamp we should include in our next CONNECT_ACK message.
487 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
488 * our CONNECT_ACK message.
490 struct GNUNET_TIME_Absolute connect_ack_timestamp;
493 * ATS address suggest handle
495 struct GNUNET_ATS_SuggestHandle *suggest_handle;
498 * Time where we should cut the connection (timeout) if we don't
499 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
500 * if we are in S_CONNECTED).
502 struct GNUNET_TIME_Absolute timeout;
505 * Latest calculated latency value
507 struct GNUNET_TIME_Relative latency;
510 * Tracker for inbound bandwidth.
512 struct GNUNET_BANDWIDTH_Tracker in_tracker;
515 * How often has the other peer (recently) violated the inbound
516 * traffic limit? Incremented by 10 per violation, decremented by 1
517 * per non-violation (for each time interval).
519 unsigned int quota_violation_count;
522 * The current state of the peer.
527 * Did we sent an KEEP_ALIVE message and are we expecting a response?
529 int expect_latency_response;
532 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
533 * (once we have an address to use and the peer has been allowed by our
534 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
535 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
536 * a 'SESSION_ACK' (regardless of what our own state machine might say).
538 int send_connect_ack;
541 * Tracking utilization of outbound bandwidth
543 uint32_t util_payload_bytes_sent;
546 * Tracking utilization of inbound bandwidth
548 uint32_t util_payload_bytes_recv;
551 * Tracking utilization of outbound bandwidth
553 uint32_t util_total_bytes_sent;
556 * Tracking utilization of inbound bandwidth
558 uint32_t util_total_bytes_recv;
564 * Date of last utilization transmission
566 struct GNUNET_TIME_Absolute last_util_transmission;
571 * Context for blacklist checks and the 'handle_test_blacklist_cont'
572 * function. Stores information about ongoing blacklist checks.
574 struct BlackListCheckContext
578 * We keep blacklist checks in a DLL.
580 struct BlackListCheckContext *next;
583 * We keep blacklist checks in a DLL.
585 struct BlackListCheckContext *prev;
588 * Address that is being checked.
590 struct NeighbourAddress na;
593 * Handle to the ongoing blacklist check.
595 struct GST_BlacklistCheck *bc;
600 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
602 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
605 * We keep blacklist checks in a DLL so that we can find
606 * the 'sessions' in their 'struct NeighbourAddress' if
607 * a session goes down.
609 static struct BlackListCheckContext *bc_head;
612 * We keep blacklist checks in a DLL.
614 static struct BlackListCheckContext *bc_tail;
617 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
619 static void *callback_cls;
622 * Function to call when we connected to a neighbour.
624 static NotifyConnect connect_notify_cb;
627 * Function to call when we disconnected from a neighbour.
629 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
632 * Function to call when we changed an active address of a neighbour.
634 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
637 * counter for connected neighbours
639 static unsigned int neighbours_connected;
642 * Number of bytes we have currently queued for transmission.
644 static unsigned long long bytes_in_send_queue;
647 * Task transmitting utilization data
649 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
653 * Lookup a neighbour entry in the neighbours hash map.
655 * @param pid identity of the peer to look up
656 * @return the entry, NULL if there is no existing record
658 static struct NeighbourMapEntry *
659 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
661 if (NULL == neighbours)
663 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
667 print_state (int state)
672 case S_NOT_CONNECTED:
673 return "S_NOT_CONNECTED";
676 case S_INIT_BLACKLIST:
677 return "S_INIT_BLACKLIST";
679 return "S_CONNECT_SENT";
680 case S_CONNECT_RECV_BLACKLIST_INBOUND:
681 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
682 case S_CONNECT_RECV_ATS:
683 return "S_CONNECT_RECV_ATS";
684 case S_CONNECT_RECV_BLACKLIST:
685 return "S_CONNECT_RECV_BLACKLIST";
686 case S_CONNECT_RECV_ACK:
687 return "S_CONNECT_RECV_ACK";
689 return "S_CONNECTED";
690 case S_RECONNECT_ATS:
691 return "S_RECONNECT_ATS";
692 case S_RECONNECT_BLACKLIST:
693 return "S_RECONNECT_BLACKLIST";
694 case S_RECONNECT_SENT:
695 return "S_RECONNECT_SENT";
696 case S_CONNECTED_SWITCHING_BLACKLIST:
697 return "S_CONNECTED_SWITCHING_BLACKLIST";
698 case S_CONNECTED_SWITCHING_CONNECT_SENT:
699 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
701 return "S_DISCONNECT";
702 case S_DISCONNECT_FINISHED:
703 return "S_DISCONNECT_FINISHED";
711 * Test if we're connected to the given peer.
713 * @param n neighbour entry of peer to test
714 * @return GNUNET_YES if we are connected, GNUNET_NO if not
717 test_connected (struct NeighbourMapEntry *n)
723 case S_NOT_CONNECTED:
725 case S_INIT_BLACKLIST:
727 case S_CONNECT_RECV_BLACKLIST_INBOUND:
728 case S_CONNECT_RECV_ATS:
729 case S_CONNECT_RECV_BLACKLIST:
730 case S_CONNECT_RECV_ACK:
733 case S_RECONNECT_ATS:
734 case S_RECONNECT_BLACKLIST:
735 case S_RECONNECT_SENT:
736 case S_CONNECTED_SWITCHING_BLACKLIST:
737 case S_CONNECTED_SWITCHING_CONNECT_SENT:
740 case S_DISCONNECT_FINISHED:
743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
747 return GNUNET_SYSERR;
751 * Send information about a new outbound quota to our clients.
753 * @param target affected peer
754 * @param quota new quota
757 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
758 struct GNUNET_BANDWIDTH_Value32NBO quota)
760 struct QuotaSetMessage q_msg;
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
764 ntohl (quota.value__), GNUNET_i2s (target));
765 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
766 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
768 q_msg.peer = (*target);
769 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
774 * We don't need a given neighbour address any more.
775 * Release its resources and give appropriate notifications
776 * to ATS and other subsystems.
778 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
781 free_address (struct NeighbourAddress *na)
783 if (GNUNET_YES == na->ats_active)
785 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
786 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
787 address_change_cb (NULL, &na->address->peer, NULL);
790 na->ats_active = GNUNET_NO;
791 if (NULL != na->address)
793 GNUNET_HELLO_address_free (na->address);
801 * Initialize the 'struct NeighbourAddress'.
803 * @param na neighbour address to initialize
804 * @param address address of the other peer, NULL if other peer
806 * @param session session to use (or NULL, in which case an
807 * address must be setup)
808 * @param bandwidth_in inbound quota to be used when connection is up
809 * @param bandwidth_out outbound quota to be used when connection is up
810 * @param is_active GNUNET_YES to mark this as the active address with ATS
813 set_address (struct NeighbourAddress *na,
814 const struct GNUNET_HELLO_Address *address,
815 struct Session *session,
816 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
817 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
820 struct GNUNET_TRANSPORT_PluginFunctions *papi;
821 if (NULL == (papi = GST_plugins_find (address->transport_name)))
826 if (session == na->session)
828 na->bandwidth_in = bandwidth_in;
829 na->bandwidth_out = bandwidth_out;
830 if (is_active != na->ats_active)
832 na->ats_active = is_active;
833 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
834 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
836 address_change_cb (NULL, &address->peer, address);
838 if (GNUNET_YES == is_active)
840 /* FIXME: is this the right place to set quotas? */
841 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
842 send_outbound_quota (&address->peer, bandwidth_out);
848 session = papi->get_session (papi->cls, address);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Failed to obtain new session for peer `%s' and address '%s'\n",
853 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
854 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
857 na->address = GNUNET_HELLO_address_copy (address);
858 na->bandwidth_in = bandwidth_in;
859 na->bandwidth_out = bandwidth_out;
860 na->session = session;
861 na->ats_active = is_active;
862 if (GNUNET_YES == is_active)
864 /* Telling ATS about new session */
865 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
866 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
867 address_change_cb (NULL, &address->peer, address);
868 /* FIXME: is this the right place to set quotas? */
869 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
870 send_outbound_quota (&address->peer, bandwidth_out);
876 * Free a neighbour map entry.
878 * @param n entry to free
879 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
880 * GNUNET_YES to keep all sessions
883 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
885 struct MessageQueue *mq;
886 struct GNUNET_TRANSPORT_PluginFunctions *papi;
887 struct GNUNET_HELLO_Address *backup_primary;
889 n->is_active = NULL; /* always free'd by its own continuation! */
891 /* fail messages currently in the queue */
892 while (NULL != (mq = n->messages_head))
894 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
895 if (NULL != mq->cont)
896 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
899 /* It is too late to send other peer disconnect notifications, but at
900 least internally we need to get clean... */
901 if (GNUNET_YES == test_connected (n))
903 GNUNET_STATISTICS_set (GST_stats,
904 gettext_noop ("# peers connected"),
905 --neighbours_connected,
907 disconnect_notify_cb (callback_cls, &n->id);
910 n->state = S_DISCONNECT_FINISHED;
912 if (NULL != n->primary_address.address)
914 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
917 backup_primary = NULL;
919 /* free addresses and mark as unused */
920 free_address (&n->primary_address);
921 free_address (&n->alternative_address);
923 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
924 transport sessions eventually get killed due to inactivity; they
925 MUST have their own timeout logic (but at least TCP doesn't have
926 one yet). Are we sure that EVERY 'session' of a plugin is
927 actually cleaned up this way!? Note that if we are switching
928 between two TCP sessions to the same peer, the existing plugin
929 API gives us not even the means to selectively kill only one of
930 them! Killing all sessions like this seems to be very, very
933 /* cut transport-level connection */
934 if ((GNUNET_NO == keep_sessions) &&
935 (NULL != backup_primary) &&
936 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
937 papi->disconnect (papi->cls, &n->id);
939 GNUNET_free_non_null (backup_primary);
941 GNUNET_assert (GNUNET_YES ==
942 GNUNET_CONTAINER_multipeermap_remove (neighbours,
945 // FIXME-ATS-API: we might want to be more specific about
946 // which states we do this from in the future (ATS should
947 // have given us a 'suggest_address' handle, and if we have
948 // such a handle, we should cancel the operation here!
949 if (NULL != n->suggest_handle)
951 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
952 n->suggest_handle = NULL;
955 if (GNUNET_SCHEDULER_NO_TASK != n->task)
957 GNUNET_SCHEDULER_cancel (n->task);
958 n->task = GNUNET_SCHEDULER_NO_TASK;
960 /* free rest of memory */
965 * Transmit a message using the current session of the given
968 * @param n entry for the recipient
969 * @param msgbuf buffer to transmit
970 * @param msgbuf_size number of bytes in buffer
971 * @param priority transmission priority
972 * @param timeout transmission timeout
973 * @param cont continuation to call when finished (can be NULL)
974 * @param cont_cls closure for cont
977 send_with_session (struct NeighbourMapEntry *n,
978 const char *msgbuf, size_t msgbuf_size,
980 struct GNUNET_TIME_Relative timeout,
981 GNUNET_TRANSPORT_TransmitContinuation cont,
984 struct GNUNET_TRANSPORT_PluginFunctions *papi;
986 GNUNET_assert (n->primary_address.session != NULL);
987 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
988 (-1 == papi->send (papi->cls,
989 n->primary_address.session,
993 cont, cont_cls)))) &&
995 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
996 GST_neighbours_notify_data_sent (&n->id,
997 n->primary_address.address, n->primary_address.session, msgbuf_size);
998 GNUNET_break (NULL != papi);
1003 * Master task run for every neighbour. Performs all of the time-related
1004 * activities (keep alive, send next message, disconnect if idle, finish
1005 * clean up after disconnect).
1007 * @param cls the 'struct NeighbourMapEntry' for which we are running
1008 * @param tc scheduler context (unused)
1011 master_task (void *cls,
1012 const struct GNUNET_SCHEDULER_TaskContext *tc);
1016 * Function called when the 'DISCONNECT' message has been sent by the
1017 * plugin. Frees the neighbour --- if the entry still exists.
1020 * @param target identity of the neighbour that was disconnected
1021 * @param result GNUNET_OK if the disconnect got out successfully
1022 * @param payload bytes payload
1023 * @param physical bytes physical
1026 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1027 int result, size_t payload, size_t physical)
1029 struct NeighbourMapEntry *n;
1031 n = lookup_neighbour (target);
1033 return; /* already gone */
1034 if (S_DISCONNECT != n->state)
1035 return; /* have created a fresh entry since */
1036 n->state = S_DISCONNECT;
1037 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1038 GNUNET_SCHEDULER_cancel (n->task);
1039 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1044 * Transmit a DISCONNECT message to the other peer.
1046 * @param n neighbour to send DISCONNECT message.
1049 send_disconnect (struct NeighbourMapEntry *n)
1051 struct SessionDisconnectMessage disconnect_msg;
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 "Sending DISCONNECT message to peer `%4s'\n",
1055 GNUNET_i2s (&n->id));
1056 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1057 disconnect_msg.header.type =
1058 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1059 disconnect_msg.reserved = htonl (0);
1060 disconnect_msg.purpose.size =
1061 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1062 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1063 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1064 disconnect_msg.purpose.purpose =
1065 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1066 disconnect_msg.timestamp =
1067 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1068 disconnect_msg.public_key = GST_my_identity.public_key;
1069 GNUNET_assert (GNUNET_OK ==
1070 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1071 &disconnect_msg.purpose,
1072 &disconnect_msg.signature));
1074 send_with_session (n,
1075 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1076 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1077 &send_disconnect_cont, NULL);
1078 GNUNET_STATISTICS_update (GST_stats,
1080 ("# DISCONNECT messages sent"), 1,
1086 * Disconnect from the given neighbour, clean up the record.
1088 * @param n neighbour to disconnect from
1091 disconnect_neighbour (struct NeighbourMapEntry *n)
1093 /* depending on state, notify neighbour and/or upper layers of this peer
1097 case S_NOT_CONNECTED:
1099 case S_INIT_BLACKLIST:
1100 /* other peer is completely unaware of us, no need to send DISCONNECT */
1101 n->state = S_DISCONNECT_FINISHED;
1102 free_neighbour (n, GNUNET_NO);
1104 case S_CONNECT_SENT:
1105 send_disconnect (n);
1106 n->state = S_DISCONNECT;
1108 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1109 case S_CONNECT_RECV_ATS:
1110 case S_CONNECT_RECV_BLACKLIST:
1111 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1112 n->state = S_DISCONNECT_FINISHED;
1113 free_neighbour (n, GNUNET_NO);
1115 case S_CONNECT_RECV_ACK:
1116 /* we DID ACK the other peer's request, must send DISCONNECT */
1117 send_disconnect (n);
1118 n->state = S_DISCONNECT;
1121 case S_RECONNECT_BLACKLIST:
1122 case S_RECONNECT_SENT:
1123 case S_CONNECTED_SWITCHING_BLACKLIST:
1124 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1125 /* we are currently connected, need to send disconnect and do
1126 internal notifications and update statistics */
1127 send_disconnect (n);
1128 GNUNET_STATISTICS_set (GST_stats,
1129 gettext_noop ("# peers connected"),
1130 --neighbours_connected,
1132 disconnect_notify_cb (callback_cls, &n->id);
1133 n->state = S_DISCONNECT;
1135 case S_RECONNECT_ATS:
1136 /* ATS address request timeout, disconnect without sending disconnect message */
1137 GNUNET_STATISTICS_set (GST_stats,
1138 gettext_noop ("# peers connected"),
1139 --neighbours_connected,
1141 disconnect_notify_cb (callback_cls, &n->id);
1142 n->state = S_DISCONNECT;
1145 /* already disconnected, ignore */
1147 case S_DISCONNECT_FINISHED:
1148 /* already cleaned up, how did we get here!? */
1152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1156 /* schedule timeout to clean up */
1157 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1158 GNUNET_SCHEDULER_cancel (n->task);
1159 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1165 * We're done with our transmission attempt, continue processing.
1167 * @param cls the 'struct MessageQueue' of the message
1168 * @param receiver intended receiver
1169 * @param success whether it worked or not
1170 * @param size_payload bytes payload sent
1171 * @param physical bytes sent on wire
1174 transmit_send_continuation (void *cls,
1175 const struct GNUNET_PeerIdentity *receiver,
1176 int success, size_t size_payload, size_t physical)
1178 struct MessageQueue *mq = cls;
1179 struct NeighbourMapEntry *n;
1181 if (NULL == (n = lookup_neighbour (receiver)))
1184 return; /* disconnect or other error while transmitting, can happen */
1186 if (n->is_active == mq)
1188 /* this is still "our" neighbour, remove us from its queue
1189 and allow it to send the next message now */
1190 n->is_active = NULL;
1191 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1192 GNUNET_SCHEDULER_cancel (n->task);
1193 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1195 if (bytes_in_send_queue < mq->message_buf_size)
1197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1198 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1199 bytes_in_send_queue, mq->message_buf_size,
1200 (GNUNET_OK == success) ? "OK" : "FAIL",
1201 size_payload, physical);
1206 GNUNET_break (size_payload == mq->message_buf_size);
1207 bytes_in_send_queue -= mq->message_buf_size;
1208 GNUNET_STATISTICS_set (GST_stats,
1210 ("# bytes in message queue for other peers"),
1211 bytes_in_send_queue, GNUNET_NO);
1212 if (GNUNET_OK == success)
1213 GNUNET_STATISTICS_update (GST_stats,
1215 ("# messages transmitted to other peers"),
1218 GNUNET_STATISTICS_update (GST_stats,
1220 ("# transmission failures for messages to other peers"),
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Sending message to `%s' of type %u was a %s\n",
1224 GNUNET_i2s (receiver),
1225 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1226 (success == GNUNET_OK) ? "success" : "FAILURE");
1227 if (NULL != mq->cont)
1228 mq->cont (mq->cont_cls, success, size_payload, physical);
1234 * Check the message list for the given neighbour and if we can
1235 * send a message, do so. This function should only be called
1236 * if the connection is at least generally ready for transmission.
1237 * While we will only send one message at a time, no bandwidth
1238 * quota management is performed here. If a message was given to
1239 * the plugin, the continuation will automatically re-schedule
1240 * the 'master' task once the next message might be transmitted.
1242 * @param n target peer for which to transmit
1245 try_transmission_to_peer (struct NeighbourMapEntry *n)
1247 struct MessageQueue *mq;
1248 struct GNUNET_TIME_Relative timeout;
1250 if (NULL == n->primary_address.address)
1252 /* no address, why are we here? */
1256 if ((0 == n->primary_address.address->address_length) &&
1257 (NULL == n->primary_address.session))
1259 /* no address, why are we here? */
1263 if (NULL != n->is_active)
1265 /* transmission already pending */
1269 /* timeout messages from the queue that are past their due date */
1270 while (NULL != (mq = n->messages_head))
1272 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1273 if (timeout.rel_value_us > 0)
1275 GNUNET_STATISTICS_update (GST_stats,
1277 ("# messages timed out while in transport queue"),
1279 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1281 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1284 return; /* no more messages */
1285 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1287 send_with_session (n,
1288 mq->message_buf, mq->message_buf_size,
1289 0 /* priority */, timeout,
1290 &transmit_send_continuation, mq);
1295 * Send keepalive message to the neighbour. Must only be called
1296 * if we are on 'connected' state or while trying to switch addresses.
1297 * Will internally determine if a keepalive is truly needed (so can
1298 * always be called).
1300 * @param n neighbour that went idle and needs a keepalive
1303 send_keepalive (struct NeighbourMapEntry *n)
1305 struct GNUNET_MessageHeader m;
1307 GNUNET_assert ((S_CONNECTED == n->state) ||
1308 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1309 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1310 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1311 return; /* no keepalive needed at this time */
1312 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1313 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1314 send_with_session (n,
1315 (const void *) &m, sizeof (m),
1316 UINT32_MAX /* priority */,
1317 KEEPALIVE_FREQUENCY,
1319 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1321 n->expect_latency_response = GNUNET_YES;
1322 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1323 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1328 * Keep the connection to the given neighbour alive longer,
1329 * we received a KEEPALIVE (or equivalent); send a response.
1331 * @param neighbour neighbour to keep alive (by sending keep alive response)
1334 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1336 struct NeighbourMapEntry *n;
1337 struct GNUNET_MessageHeader m;
1339 if (NULL == (n = lookup_neighbour (neighbour)))
1341 GNUNET_STATISTICS_update (GST_stats,
1343 ("# KEEPALIVE messages discarded (peer unknown)"),
1347 if (NULL == n->primary_address.session)
1349 GNUNET_STATISTICS_update (GST_stats,
1351 ("# KEEPALIVE messages discarded (no session)"),
1355 /* send reply to allow neighbour to measure latency */
1356 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1357 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1358 send_with_session(n,
1359 (const void *) &m, sizeof (m),
1360 UINT32_MAX /* priority */,
1361 KEEPALIVE_FREQUENCY,
1367 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1368 * latency to this peer. Pass the updated information (existing ats
1369 * plus calculated latency) to ATS.
1371 * @param neighbour neighbour to keep alive
1374 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1376 struct NeighbourMapEntry *n;
1378 struct GNUNET_ATS_Information ats;
1380 if (NULL == (n = lookup_neighbour (neighbour)))
1382 GNUNET_STATISTICS_update (GST_stats,
1384 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1388 if ( (S_CONNECTED != n->state) ||
1389 (GNUNET_YES != n->expect_latency_response) )
1391 GNUNET_STATISTICS_update (GST_stats,
1393 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1397 n->expect_latency_response = GNUNET_NO;
1398 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1399 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 "Latency for peer `%s' is %s\n",
1402 GNUNET_i2s (&n->id),
1403 GNUNET_STRINGS_relative_time_to_string (n->latency,
1405 /* append latency */
1406 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1407 if (n->latency.rel_value_us > UINT32_MAX)
1408 latency = UINT32_MAX;
1410 latency = n->latency.rel_value_us;
1411 ats.value = htonl (latency);
1412 GST_ats_update_metrics (&n->id,
1413 n->primary_address.address,
1414 n->primary_address.session,
1420 * We have received a message from the given sender. How long should
1421 * we delay before receiving more? (Also used to keep the peer marked
1424 * @param sender sender of the message
1425 * @param size size of the message
1426 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1427 * GNUNET_NO if the neighbour is not connected or violates the quota,
1428 * GNUNET_SYSERR if the connection is not fully up yet
1429 * @return how long to wait before reading more from this sender
1431 struct GNUNET_TIME_Relative
1432 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1433 *sender, ssize_t size, int *do_forward)
1435 struct NeighbourMapEntry *n;
1436 struct GNUNET_TIME_Relative ret;
1438 if (NULL == neighbours)
1440 *do_forward = GNUNET_NO;
1441 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1443 if (NULL == (n = lookup_neighbour (sender)))
1445 GST_neighbours_try_connect (sender);
1446 if (NULL == (n = lookup_neighbour (sender)))
1448 GNUNET_STATISTICS_update (GST_stats,
1450 ("# messages discarded due to lack of neighbour record"),
1452 *do_forward = GNUNET_NO;
1453 return GNUNET_TIME_UNIT_ZERO;
1456 if (! test_connected (n))
1458 *do_forward = GNUNET_SYSERR;
1459 return GNUNET_TIME_UNIT_ZERO;
1461 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1463 n->quota_violation_count++;
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1466 n->in_tracker.available_bytes_per_s__,
1467 n->quota_violation_count);
1468 /* Discount 32k per violation */
1469 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1473 if (n->quota_violation_count > 0)
1475 /* try to add 32k back */
1476 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1477 n->quota_violation_count--;
1480 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1482 GNUNET_STATISTICS_update (GST_stats,
1484 ("# bandwidth quota violations by other peers"),
1486 *do_forward = GNUNET_NO;
1487 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1489 *do_forward = GNUNET_YES;
1490 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1491 if (ret.rel_value_us > 0)
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1495 (unsigned long long) n->in_tracker.
1496 consumption_since_last_update__,
1497 (unsigned int) n->in_tracker.available_bytes_per_s__,
1498 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1499 GNUNET_STATISTICS_update (GST_stats,
1500 gettext_noop ("# ms throttling suggested"),
1501 (int64_t) ret.rel_value_us / 1000LL,
1509 * Transmit a message to the given target using the active connection.
1511 * @param target destination
1512 * @param msg message to send
1513 * @param msg_size number of bytes in msg
1514 * @param timeout when to fail with timeout
1515 * @param cont function to call when done
1516 * @param cont_cls closure for 'cont'
1519 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1520 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1521 GST_NeighbourSendContinuation cont, void *cont_cls)
1523 struct NeighbourMapEntry *n;
1524 struct MessageQueue *mq;
1526 /* All ove these cases should never happen; they are all API violations.
1527 But we check anyway, just to be sure. */
1528 if (NULL == (n = lookup_neighbour (target)))
1532 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1535 if (GNUNET_YES != test_connected (n))
1539 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1542 bytes_in_send_queue += msg_size;
1543 GNUNET_STATISTICS_set (GST_stats,
1545 ("# bytes in message queue for other peers"),
1546 bytes_in_send_queue, GNUNET_NO);
1547 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1549 mq->cont_cls = cont_cls;
1550 memcpy (&mq[1], msg, msg_size);
1551 mq->message_buf = (const char *) &mq[1];
1552 mq->message_buf_size = msg_size;
1553 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1554 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1555 if ( (NULL != n->is_active) ||
1556 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1558 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1559 GNUNET_SCHEDULER_cancel (n->task);
1560 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1565 * Send a SESSION_CONNECT message via the given address.
1567 * @param na address to use
1570 send_session_connect (struct NeighbourAddress *na)
1572 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1573 struct SessionConnectMessage connect_msg;
1575 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1580 if (NULL == na->session)
1581 na->session = papi->get_session (papi->cls, na->address);
1582 if (NULL == na->session)
1587 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1588 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1589 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1590 connect_msg.reserved = htonl (0);
1591 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1592 (void) papi->send (papi->cls,
1594 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1596 GNUNET_TIME_UNIT_FOREVER_REL,
1598 GST_neighbours_notify_data_sent (&na->address->peer,
1599 na->address, na->session, sizeof (struct SessionConnectMessage));
1605 * Send a SESSION_CONNECT_ACK message via the given address.
1607 * @param address address to use
1608 * @param session session to use
1609 * @param timestamp timestamp to use for the ACK message
1612 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1613 struct Session *session,
1614 struct GNUNET_TIME_Absolute timestamp)
1616 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1617 struct SessionConnectMessage connect_msg;
1619 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1624 if (NULL == session)
1625 session = papi->get_session (papi->cls, address);
1626 if (NULL == session)
1631 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1632 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1633 connect_msg.reserved = htonl (0);
1634 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1635 (void) papi->send (papi->cls,
1637 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1639 GNUNET_TIME_UNIT_FOREVER_REL,
1646 * Create a fresh entry in the neighbour map for the given peer
1648 * @param peer peer to create an entry for
1649 * @return new neighbour map entry
1651 static struct NeighbourMapEntry *
1652 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1654 struct NeighbourMapEntry *n;
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Creating new neighbour entry for `%s'\n",
1659 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1661 n->state = S_NOT_CONNECTED;
1662 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1663 n->last_util_transmission = GNUNET_TIME_absolute_get();
1664 n->util_payload_bytes_recv = 0;
1665 n->util_payload_bytes_sent = 0;
1666 n->util_total_bytes_recv = 0;
1667 n->util_total_bytes_sent = 0;
1668 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1669 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1670 MAX_BANDWIDTH_CARRY_S);
1671 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1672 GNUNET_assert (GNUNET_OK ==
1673 GNUNET_CONTAINER_multipeermap_put (neighbours,
1675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1681 * Check if the two given addresses are the same.
1682 * Actually only checks if the sessions are non-NULL
1683 * (which they should be) and then if they are identical;
1684 * the actual addresses don't matter if the session
1685 * pointers match anyway, and we must have session pointers
1688 * @param a1 first address to compare
1689 * @param a2 other address to compare
1690 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1693 address_matches (const struct NeighbourAddress *a1,
1694 const struct NeighbourAddress *a2)
1696 if ( (NULL == a1->session) ||
1697 (NULL == a2->session) )
1702 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1707 * Try to create a connection to the given target (eventually).
1709 * @param target peer to try to connect to
1712 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1714 struct NeighbourMapEntry *n;
1716 if (NULL == neighbours)
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Asked to connect to peer `%s' during shutdown\n",
1720 GNUNET_i2s (target));
1721 return; /* during shutdown, do nothing */
1723 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1724 "Asked to connect to peer `%s'\n",
1725 GNUNET_i2s (target));
1726 if (0 == memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1728 /* refuse to connect to myself */
1729 /* FIXME: can this happen? Is this not an API violation? */
1730 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1731 "Refusing to try to connect to myself.\n");
1734 n = lookup_neighbour (target);
1739 case S_NOT_CONNECTED:
1740 /* this should not be possible */
1742 free_neighbour (n, GNUNET_NO);
1745 case S_INIT_BLACKLIST:
1746 case S_CONNECT_SENT:
1747 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1748 case S_CONNECT_RECV_ATS:
1749 case S_CONNECT_RECV_BLACKLIST:
1750 case S_CONNECT_RECV_ACK:
1751 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1752 "Ignoring request to try to connect to `%s', already trying!\n",
1753 GNUNET_i2s (target));
1754 return; /* already trying */
1756 case S_RECONNECT_ATS:
1757 case S_RECONNECT_BLACKLIST:
1758 case S_RECONNECT_SENT:
1759 case S_CONNECTED_SWITCHING_BLACKLIST:
1760 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1761 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1762 "Ignoring request to try to connect, already connected to `%s'!\n",
1763 GNUNET_i2s (target));
1764 return; /* already connected */
1766 /* get rid of remains, ready to re-try immediately */
1767 free_neighbour (n, GNUNET_NO);
1769 case S_DISCONNECT_FINISHED:
1770 /* should not be possible */
1773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1775 free_neighbour (n, GNUNET_NO);
1779 n = setup_neighbour (target);
1780 n->state = S_INIT_ATS;
1781 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1783 GNUNET_ATS_reset_backoff (GST_ats, target);
1784 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1789 * Function called with the result of a blacklist check.
1791 * @param cls closure with the 'struct BlackListCheckContext'
1792 * @param peer peer this check affects
1793 * @param result GNUNET_OK if the address is allowed
1796 handle_test_blacklist_cont (void *cls,
1797 const struct GNUNET_PeerIdentity *peer,
1800 struct BlackListCheckContext *bcc = cls;
1801 struct NeighbourMapEntry *n;
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1807 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1808 if (NULL == (n = lookup_neighbour (peer)))
1809 goto cleanup; /* nobody left to care about new address */
1812 case S_NOT_CONNECTED:
1813 /* this should not be possible */
1815 free_neighbour (n, GNUNET_NO);
1818 /* still waiting on ATS suggestion */
1820 case S_INIT_BLACKLIST:
1821 /* check if the address the blacklist was fine with matches
1822 ATS suggestion, if so, we can move on! */
1823 if ( (GNUNET_OK == result) &&
1824 (1 == n->send_connect_ack) )
1826 n->send_connect_ack = 2;
1827 send_session_connect_ack_message (bcc->na.address,
1829 n->connect_ack_timestamp);
1831 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1832 break; /* result for an address we currently don't care about */
1833 if (GNUNET_OK == result)
1835 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1836 n->state = S_CONNECT_SENT;
1837 send_session_connect (&n->primary_address);
1841 // FIXME: should also possibly destroy session with plugin!?
1842 GNUNET_ATS_address_destroyed (GST_ats,
1845 free_address (&n->primary_address);
1846 n->state = S_INIT_ATS;
1847 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1848 // FIXME: do we need to ask ATS again for suggestions?
1849 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1852 case S_CONNECT_SENT:
1853 /* waiting on CONNECT_ACK, send ACK if one is pending */
1854 if ( (GNUNET_OK == result) &&
1855 (1 == n->send_connect_ack) )
1857 n->send_connect_ack = 2;
1858 send_session_connect_ack_message (n->primary_address.address,
1859 n->primary_address.session,
1860 n->connect_ack_timestamp);
1863 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1864 if (GNUNET_OK == result)
1865 GST_ats_add_address (bcc->na.address, bcc->na.session);
1867 n->state = S_CONNECT_RECV_ATS;
1868 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1869 GNUNET_ATS_reset_backoff (GST_ats, peer);
1870 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1872 case S_CONNECT_RECV_ATS:
1873 /* still waiting on ATS suggestion, don't care about blacklist */
1875 case S_CONNECT_RECV_BLACKLIST:
1876 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1877 break; /* result for an address we currently don't care about */
1878 if (GNUNET_OK == result)
1880 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1881 n->state = S_CONNECT_RECV_ACK;
1882 send_session_connect_ack_message (bcc->na.address,
1884 n->connect_ack_timestamp);
1885 if (1 == n->send_connect_ack)
1886 n->send_connect_ack = 2;
1890 // FIXME: should also possibly destroy session with plugin!?
1891 GNUNET_ATS_address_destroyed (GST_ats,
1894 free_address (&n->primary_address);
1895 n->state = S_INIT_ATS;
1896 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1897 // FIXME: do we need to ask ATS again for suggestions?
1898 GNUNET_ATS_reset_backoff (GST_ats, peer);
1899 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1902 case S_CONNECT_RECV_ACK:
1903 /* waiting on SESSION_ACK, send ACK if one is pending */
1904 if ( (GNUNET_OK == result) &&
1905 (1 == n->send_connect_ack) )
1907 n->send_connect_ack = 2;
1908 send_session_connect_ack_message (n->primary_address.address,
1909 n->primary_address.session,
1910 n->connect_ack_timestamp);
1914 /* already connected, don't care about blacklist */
1916 case S_RECONNECT_ATS:
1917 /* still waiting on ATS suggestion, don't care about blacklist */
1919 case S_RECONNECT_BLACKLIST:
1920 if ( (GNUNET_OK == result) &&
1921 (1 == n->send_connect_ack) )
1923 n->send_connect_ack = 2;
1924 send_session_connect_ack_message (bcc->na.address,
1926 n->connect_ack_timestamp);
1928 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1929 break; /* result for an address we currently don't care about */
1930 if (GNUNET_OK == result)
1932 send_session_connect (&n->primary_address);
1933 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1934 n->state = S_RECONNECT_SENT;
1938 GNUNET_ATS_address_destroyed (GST_ats,
1941 n->state = S_RECONNECT_ATS;
1942 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1943 // FIXME: do we need to ask ATS again for suggestions?
1944 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1947 case S_RECONNECT_SENT:
1948 /* waiting on CONNECT_ACK, don't care about blacklist */
1949 if ( (GNUNET_OK == result) &&
1950 (1 == n->send_connect_ack) )
1952 n->send_connect_ack = 2;
1953 send_session_connect_ack_message (n->primary_address.address,
1954 n->primary_address.session,
1955 n->connect_ack_timestamp);
1958 case S_CONNECTED_SWITCHING_BLACKLIST:
1959 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1960 break; /* result for an address we currently don't care about */
1961 if (GNUNET_OK == result)
1963 send_session_connect (&n->alternative_address);
1964 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1968 GNUNET_ATS_address_destroyed (GST_ats,
1971 free_address (&n->alternative_address);
1972 n->state = S_CONNECTED;
1975 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1976 /* waiting on CONNECT_ACK, don't care about blacklist */
1977 if ( (GNUNET_OK == result) &&
1978 (1 == n->send_connect_ack) )
1980 n->send_connect_ack = 2;
1981 send_session_connect_ack_message (n->primary_address.address,
1982 n->primary_address.session,
1983 n->connect_ack_timestamp);
1987 /* Nothing to do here, ATS will already do what can be done */
1989 case S_DISCONNECT_FINISHED:
1990 /* should not be possible */
1994 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1996 free_neighbour (n, GNUNET_NO);
2000 GNUNET_HELLO_address_free (bcc->na.address);
2001 GNUNET_CONTAINER_DLL_remove (bc_head,
2009 * We want to know if connecting to a particular peer via
2010 * a particular address is allowed. Check it!
2012 * @param peer identity of the peer to switch the address for
2013 * @param ts time at which the check was initiated
2014 * @param address address of the other peer, NULL if other peer
2016 * @param session session to use (or NULL)
2019 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2020 struct GNUNET_TIME_Absolute ts,
2021 const struct GNUNET_HELLO_Address *address,
2022 struct Session *session)
2024 struct BlackListCheckContext *bcc;
2025 struct GST_BlacklistCheck *bc;
2027 bcc = GNUNET_malloc (sizeof (struct BlackListCheckContext));
2028 bcc->na.address = GNUNET_HELLO_address_copy (address);
2029 bcc->na.session = session;
2030 bcc->na.connect_timestamp = ts;
2031 GNUNET_CONTAINER_DLL_insert (bc_head,
2034 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2035 address->transport_name,
2036 &handle_test_blacklist_cont, bcc)))
2038 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2039 we must only store 'bc' if 'bc' is non-NULL... */
2044 * We received a 'SESSION_CONNECT' message from the other peer.
2045 * Consider switching to it.
2047 * @param message possibly a 'struct SessionConnectMessage' (check format)
2048 * @param peer identity of the peer to switch the address for
2049 * @param address address of the other peer, NULL if other peer
2051 * @param session session to use (or NULL)
2054 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2055 const struct GNUNET_PeerIdentity *peer,
2056 const struct GNUNET_HELLO_Address *address,
2057 struct Session *session)
2059 const struct SessionConnectMessage *scm;
2060 struct NeighbourMapEntry *n;
2061 struct GNUNET_TIME_Absolute ts;
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2064 "Received CONNECT message from peer `%s'\n",
2067 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2069 GNUNET_break_op (0);
2072 if (NULL == neighbours)
2073 return; /* we're shutting down */
2074 scm = (const struct SessionConnectMessage *) message;
2075 GNUNET_break_op (0 == ntohl (scm->reserved));
2076 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2077 n = lookup_neighbour (peer);
2079 n = setup_neighbour (peer);
2080 n->send_connect_ack = 1;
2081 n->connect_ack_timestamp = ts;
2085 case S_NOT_CONNECTED:
2086 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2087 /* Do a blacklist check for the new address */
2088 check_blacklist (peer, ts, address, session);
2091 /* CONNECT message takes priority over us asking ATS for address */
2092 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2094 case S_INIT_BLACKLIST:
2095 case S_CONNECT_SENT:
2096 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2097 case S_CONNECT_RECV_ATS:
2098 case S_CONNECT_RECV_BLACKLIST:
2099 case S_CONNECT_RECV_ACK:
2100 /* It can never hurt to have an alternative address in the above cases,
2101 see if it is allowed */
2102 check_blacklist (peer, ts, address, session);
2105 /* we are already connected and can thus send the ACK immediately;
2106 still, it can never hurt to have an alternative address, so also
2107 tell ATS about it */
2108 GNUNET_assert (NULL != n->primary_address.address);
2109 GNUNET_assert (NULL != n->primary_address.session);
2110 n->send_connect_ack = 0;
2111 send_session_connect_ack_message (n->primary_address.address,
2112 n->primary_address.session, ts);
2113 check_blacklist (peer, ts, address, session);
2115 case S_RECONNECT_ATS:
2116 case S_RECONNECT_BLACKLIST:
2117 case S_RECONNECT_SENT:
2118 /* It can never hurt to have an alternative address in the above cases,
2119 see if it is allowed */
2120 check_blacklist (peer, ts, address, session);
2122 case S_CONNECTED_SWITCHING_BLACKLIST:
2123 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2124 /* we are already connected and can thus send the ACK immediately;
2125 still, it can never hurt to have an alternative address, so also
2126 tell ATS about it */
2127 GNUNET_assert (NULL != n->primary_address.address);
2128 GNUNET_assert (NULL != n->primary_address.session);
2129 n->send_connect_ack = 0;
2130 send_session_connect_ack_message (n->primary_address.address,
2131 n->primary_address.session, ts);
2132 check_blacklist (peer, ts, address, session);
2135 /* get rid of remains without terminating sessions, ready to re-try */
2136 free_neighbour (n, GNUNET_YES);
2137 n = setup_neighbour (peer);
2138 n->state = S_CONNECT_RECV_ATS;
2139 GNUNET_ATS_reset_backoff (GST_ats, peer);
2140 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2142 case S_DISCONNECT_FINISHED:
2143 /* should not be possible */
2147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2149 free_neighbour (n, GNUNET_NO);
2156 * For an existing neighbour record, set the active connection to
2157 * use the given address.
2159 * @param peer identity of the peer to switch the address for
2160 * @param address address of the other peer, NULL if other peer
2162 * @param session session to use (or NULL)
2163 * @param ats performance data
2164 * @param ats_count number of entries in ats
2165 * @param bandwidth_in inbound quota to be used when connection is up,
2166 * 0 to disconnect from peer
2167 * @param bandwidth_out outbound quota to be used when connection is up,
2168 * 0 to disconnect from peer
2171 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2172 const struct GNUNET_HELLO_Address *address,
2173 struct Session *session,
2174 const struct GNUNET_ATS_Information *ats,
2176 struct GNUNET_BANDWIDTH_Value32NBO
2178 struct GNUNET_BANDWIDTH_Value32NBO
2181 struct NeighbourMapEntry *n;
2182 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2184 GNUNET_assert (address->transport_name != NULL);
2185 if (NULL == (n = lookup_neighbour (peer)))
2188 /* Obtain an session for this address from plugin */
2189 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2191 /* we don't have the plugin for this address */
2192 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2195 if ((NULL == session) && (0 == address->address_length))
2198 if (strlen (address->transport_name) > 0)
2199 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2204 "ATS tells us to switch to address '%s' session %p for "
2205 "peer `%s' in state %s (quota in/out %u %u )\n",
2206 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2209 print_state (n->state),
2210 ntohl (bandwidth_in.value__),
2211 ntohl (bandwidth_out.value__));
2213 if (NULL == session)
2215 session = papi->get_session (papi->cls, address);
2216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2217 "Obtained new session for peer `%s' and address '%s': %p\n",
2218 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2220 if (NULL == session)
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Failed to obtain new session for peer `%s' and address '%s'\n",
2224 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2225 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2230 case S_NOT_CONNECTED:
2232 free_neighbour (n, GNUNET_NO);
2235 set_address (&n->primary_address,
2236 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2237 n->state = S_INIT_BLACKLIST;
2238 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2239 check_blacklist (&n->id,
2240 n->connect_ack_timestamp,
2243 case S_INIT_BLACKLIST:
2244 /* ATS suggests a different address, switch again */
2245 set_address (&n->primary_address,
2246 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2247 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2248 check_blacklist (&n->id,
2249 n->connect_ack_timestamp,
2252 case S_CONNECT_SENT:
2253 /* ATS suggests a different address, switch again */
2254 set_address (&n->primary_address,
2255 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2256 n->state = S_INIT_BLACKLIST;
2257 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2258 check_blacklist (&n->id,
2259 n->connect_ack_timestamp,
2262 case S_CONNECT_RECV_ATS:
2263 set_address (&n->primary_address,
2264 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2265 n->state = S_CONNECT_RECV_BLACKLIST;
2266 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2267 check_blacklist (&n->id,
2268 n->connect_ack_timestamp,
2271 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2272 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2273 check_blacklist (&n->id,
2274 n->connect_ack_timestamp,
2277 case S_CONNECT_RECV_BLACKLIST:
2278 case S_CONNECT_RECV_ACK:
2279 /* ATS asks us to switch while we were trying to connect; switch to new
2280 address and check blacklist again */
2281 set_address (&n->primary_address,
2282 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2283 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2284 check_blacklist (&n->id,
2285 n->connect_ack_timestamp,
2289 GNUNET_assert (NULL != n->primary_address.address);
2290 GNUNET_assert (NULL != n->primary_address.session);
2291 if (n->primary_address.session == session)
2293 /* not an address change, just a quota change */
2294 set_address (&n->primary_address,
2295 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2298 /* ATS asks us to switch a life connection; see if we can get
2299 a CONNECT_ACK on it before we actually do this! */
2300 set_address (&n->alternative_address,
2301 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2302 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2303 check_blacklist (&n->id,
2304 GNUNET_TIME_absolute_get (),
2307 case S_RECONNECT_ATS:
2308 set_address (&n->primary_address,
2309 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2310 n->state = S_RECONNECT_BLACKLIST;
2311 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2312 check_blacklist (&n->id,
2313 n->connect_ack_timestamp,
2316 case S_RECONNECT_BLACKLIST:
2317 /* ATS asks us to switch while we were trying to reconnect; switch to new
2318 address and check blacklist again */
2319 set_address (&n->primary_address,
2320 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2321 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2322 check_blacklist (&n->id,
2323 n->connect_ack_timestamp,
2326 case S_RECONNECT_SENT:
2327 /* ATS asks us to switch while we were trying to reconnect; switch to new
2328 address and check blacklist again */
2329 set_address (&n->primary_address,
2330 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2331 n->state = S_RECONNECT_BLACKLIST;
2332 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2333 check_blacklist (&n->id,
2334 n->connect_ack_timestamp,
2337 case S_CONNECTED_SWITCHING_BLACKLIST:
2338 if (n->primary_address.session == session)
2340 /* ATS switches back to still-active session */
2341 free_address (&n->alternative_address);
2342 n->state = S_CONNECTED;
2345 /* ATS asks us to switch a life connection, update blacklist check */
2346 set_address (&n->alternative_address,
2347 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2348 check_blacklist (&n->id,
2349 GNUNET_TIME_absolute_get (),
2352 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2353 if (n->primary_address.session == session)
2355 /* ATS switches back to still-active session */
2356 free_address (&n->alternative_address);
2357 n->state = S_CONNECTED;
2360 /* ATS asks us to switch a life connection, update blacklist check */
2361 set_address (&n->alternative_address,
2362 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2363 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2364 check_blacklist (&n->id,
2365 GNUNET_TIME_absolute_get (),
2369 /* not going to switch addresses while disconnecting */
2371 case S_DISCONNECT_FINISHED:
2375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2382 send_utilization_data (void *cls,
2383 const struct GNUNET_PeerIdentity *key,
2386 struct NeighbourMapEntry *n = value;
2387 struct GNUNET_ATS_Information atsi[4];
2389 uint32_t bps_pl_out;
2392 struct GNUNET_TIME_Relative delta;
2394 delta = GNUNET_TIME_absolute_get_difference(n->last_util_transmission, GNUNET_TIME_absolute_get());
2397 if (0 != n->util_payload_bytes_recv)
2398 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2400 if (0 != n->util_payload_bytes_sent)
2401 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' payload: received %u Bytes/s, sent %u Bytes/s \n",
2403 GNUNET_i2s (key), bps_pl_in, bps_pl_out);
2406 if (0 != n->util_total_bytes_recv)
2407 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2409 if (0 != n->util_total_bytes_sent)
2410 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' total: received %u Bytes/s, sent %u Bytes/s \n",
2414 GNUNET_i2s (key), bps_in, bps_out);
2416 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2417 atsi[0].value = htonl (bps_out);
2418 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2419 atsi[1].value = htonl (bps_in);
2421 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2422 atsi[2].value = htonl (bps_pl_out);
2423 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2424 atsi[3].value = htonl (bps_pl_in);
2426 GST_ats_update_metrics (key, n->primary_address.address,
2427 n->primary_address.session, atsi, 4);
2428 n->util_payload_bytes_recv = 0;
2429 n->util_payload_bytes_sent = 0;
2430 n->util_total_bytes_recv = 0;
2431 n->util_total_bytes_sent = 0;
2432 n->last_util_transmission = GNUNET_TIME_absolute_get();
2437 * Task transmitting utilization in a regular interval
2439 * @param cls the 'struct NeighbourMapEntry' for which we are running
2440 * @param tc scheduler context (unused)
2443 utilization_transmission (void *cls,
2444 const struct GNUNET_SCHEDULER_TaskContext *tc)
2446 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2448 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2449 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2451 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2452 utilization_transmission, NULL);
2457 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2458 const struct GNUNET_HELLO_Address *address,
2459 struct Session *session,
2460 const struct GNUNET_MessageHeader *message)
2462 struct NeighbourMapEntry *n;
2463 n = lookup_neighbour (peer);
2468 n->util_total_bytes_recv += ntohs(message->size);
2472 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2473 const struct GNUNET_HELLO_Address *address,
2474 struct Session *session,
2475 const struct GNUNET_MessageHeader *message)
2477 struct NeighbourMapEntry *n;
2478 n = lookup_neighbour (peer);
2483 n->util_payload_bytes_recv += ntohs(message->size);
2488 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2489 const struct GNUNET_HELLO_Address *address,
2490 struct Session *session,
2493 struct NeighbourMapEntry *n;
2494 n = lookup_neighbour (peer);
2497 if (n->primary_address.session != session)
2499 n->util_total_bytes_sent += size;
2503 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2506 struct NeighbourMapEntry *n;
2507 n = lookup_neighbour (peer);
2512 n->util_payload_bytes_sent += size;
2517 * Master task run for every neighbour. Performs all of the time-related
2518 * activities (keep alive, send next message, disconnect if idle, finish
2519 * clean up after disconnect).
2521 * @param cls the 'struct NeighbourMapEntry' for which we are running
2522 * @param tc scheduler context (unused)
2525 master_task (void *cls,
2526 const struct GNUNET_SCHEDULER_TaskContext *tc)
2528 struct NeighbourMapEntry *n = cls;
2529 struct GNUNET_TIME_Relative delay;
2531 n->task = GNUNET_SCHEDULER_NO_TASK;
2532 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2535 GNUNET_i2s (&n->id),
2536 print_state(n->state),
2537 GNUNET_STRINGS_relative_time_to_string (delay,
2541 case S_NOT_CONNECTED:
2542 /* invalid state for master task, clean up */
2544 n->state = S_DISCONNECT_FINISHED;
2545 free_neighbour (n, GNUNET_NO);
2548 if (0 == delay.rel_value_us)
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2551 "Connection to `%s' timed out waiting for ATS to provide address\n",
2552 GNUNET_i2s (&n->id));
2553 n->state = S_DISCONNECT_FINISHED;
2554 free_neighbour (n, GNUNET_NO);
2558 case S_INIT_BLACKLIST:
2559 if (0 == delay.rel_value_us)
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2563 GNUNET_i2s (&n->id));
2564 n->state = S_DISCONNECT_FINISHED;
2565 free_neighbour (n, GNUNET_NO);
2569 case S_CONNECT_SENT:
2570 if (0 == delay.rel_value_us)
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2574 GNUNET_i2s (&n->id));
2575 disconnect_neighbour (n);
2579 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2580 if (0 == delay.rel_value_us)
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2584 GNUNET_i2s (&n->id));
2585 n->state = S_DISCONNECT_FINISHED;
2586 free_neighbour (n, GNUNET_NO);
2590 case S_CONNECT_RECV_ATS:
2591 if (0 == delay.rel_value_us)
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2595 GNUNET_i2s (&n->id));
2596 n->state = S_DISCONNECT_FINISHED;
2597 free_neighbour (n, GNUNET_NO);
2601 case S_CONNECT_RECV_BLACKLIST:
2602 if (0 == delay.rel_value_us)
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2606 GNUNET_i2s (&n->id));
2607 n->state = S_DISCONNECT_FINISHED;
2608 free_neighbour (n, GNUNET_NO);
2612 case S_CONNECT_RECV_ACK:
2613 if (0 == delay.rel_value_us)
2615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2616 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2617 GNUNET_i2s (&n->id));
2618 disconnect_neighbour (n);
2623 if (0 == delay.rel_value_us)
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2627 GNUNET_i2s (&n->id));
2628 disconnect_neighbour (n);
2631 try_transmission_to_peer (n);
2634 case S_RECONNECT_ATS:
2635 if (0 == delay.rel_value_us)
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2639 GNUNET_i2s (&n->id));
2640 disconnect_neighbour (n);
2644 case S_RECONNECT_BLACKLIST:
2645 if (0 == delay.rel_value_us)
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2649 GNUNET_i2s (&n->id));
2650 disconnect_neighbour (n);
2654 case S_RECONNECT_SENT:
2655 if (0 == delay.rel_value_us)
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2658 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2659 GNUNET_i2s (&n->id));
2660 disconnect_neighbour (n);
2664 case S_CONNECTED_SWITCHING_BLACKLIST:
2665 if (0 == delay.rel_value_us)
2667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2668 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2669 GNUNET_i2s (&n->id));
2670 disconnect_neighbour (n);
2673 try_transmission_to_peer (n);
2676 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2677 if (0 == delay.rel_value_us)
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2681 GNUNET_i2s (&n->id));
2682 disconnect_neighbour (n);
2685 try_transmission_to_peer (n);
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2691 GNUNET_i2s (&n->id));
2692 free_neighbour (n, GNUNET_NO);
2694 case S_DISCONNECT_FINISHED:
2695 /* how did we get here!? */
2699 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2703 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2704 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2705 (S_CONNECTED == n->state) )
2707 /* if we are *now* in one of these three states, we're sending
2708 keep alive messages, so we need to consider the keepalive
2709 delay, not just the connection timeout */
2710 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2713 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2714 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2721 * Send a SESSION_ACK message to the neighbour to confirm that we
2722 * got his CONNECT_ACK.
2724 * @param n neighbour to send the SESSION_ACK to
2727 send_session_ack_message (struct NeighbourMapEntry *n)
2729 struct GNUNET_MessageHeader msg;
2731 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2732 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2733 (void) send_with_session(n,
2734 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2735 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2741 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2742 * Consider switching to it.
2744 * @param message possibly a 'struct SessionConnectMessage' (check format)
2745 * @param peer identity of the peer to switch the address for
2746 * @param address address of the other peer, NULL if other peer
2748 * @param session session to use (or NULL)
2751 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2752 const struct GNUNET_PeerIdentity *peer,
2753 const struct GNUNET_HELLO_Address *address,
2754 struct Session *session)
2756 const struct SessionConnectMessage *scm;
2757 struct GNUNET_TIME_Absolute ts;
2758 struct NeighbourMapEntry *n;
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2761 "Received CONNECT_ACK message from peer `%s'\n",
2764 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2766 GNUNET_break_op (0);
2769 scm = (const struct SessionConnectMessage *) message;
2770 GNUNET_break_op (ntohl (scm->reserved) == 0);
2771 if (NULL == (n = lookup_neighbour (peer)))
2773 GNUNET_STATISTICS_update (GST_stats,
2775 ("# unexpected CONNECT_ACK messages (no peer)"),
2779 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2782 case S_NOT_CONNECTED:
2784 free_neighbour (n, GNUNET_NO);
2787 case S_INIT_BLACKLIST:
2788 GNUNET_STATISTICS_update (GST_stats,
2790 ("# unexpected CONNECT_ACK messages (not ready)"),
2793 case S_CONNECT_SENT:
2794 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2795 break; /* ACK does not match our original CONNECT message */
2796 n->state = S_CONNECTED;
2797 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2798 GNUNET_STATISTICS_set (GST_stats,
2799 gettext_noop ("# peers connected"),
2800 ++neighbours_connected,
2802 connect_notify_cb (callback_cls, &n->id,
2803 n->primary_address.bandwidth_in,
2804 n->primary_address.bandwidth_out);
2805 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2806 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2807 set_address (&n->primary_address,
2808 n->primary_address.address,
2809 n->primary_address.session,
2810 n->primary_address.bandwidth_in,
2811 n->primary_address.bandwidth_out,
2813 send_session_ack_message (n);
2815 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2816 case S_CONNECT_RECV_ATS:
2817 case S_CONNECT_RECV_BLACKLIST:
2818 case S_CONNECT_RECV_ACK:
2819 GNUNET_STATISTICS_update (GST_stats,
2821 ("# unexpected CONNECT_ACK messages (not ready)"),
2825 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2826 send_session_ack_message (n);
2828 case S_RECONNECT_ATS:
2829 case S_RECONNECT_BLACKLIST:
2830 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2831 to give us a new address... */
2832 GNUNET_STATISTICS_update (GST_stats,
2834 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2837 case S_RECONNECT_SENT:
2838 /* new address worked; go back to connected! */
2839 n->state = S_CONNECTED;
2840 send_session_ack_message (n);
2842 case S_CONNECTED_SWITCHING_BLACKLIST:
2843 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2844 send_session_ack_message (n);
2846 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2847 /* new address worked; adopt it and go back to connected! */
2848 n->state = S_CONNECTED;
2849 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2850 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2852 GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2853 set_address (&n->primary_address,
2854 n->alternative_address.address,
2855 n->alternative_address.session,
2856 n->alternative_address.bandwidth_in,
2857 n->alternative_address.bandwidth_out,
2859 free_address (&n->alternative_address);
2860 send_session_ack_message (n);
2863 GNUNET_STATISTICS_update (GST_stats,
2865 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2868 case S_DISCONNECT_FINISHED:
2872 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2880 * A session was terminated. Take note; if needed, try to get
2881 * an alternative address from ATS.
2883 * @param peer identity of the peer where the session died
2884 * @param session session that is gone
2885 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2886 * this session was not in use
2889 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2890 struct Session *session)
2892 struct NeighbourMapEntry *n;
2893 struct BlackListCheckContext *bcc;
2894 struct BlackListCheckContext *bcc_next;
2896 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2898 while (NULL != (bcc = bcc_next))
2900 bcc_next = bcc->next;
2901 if (bcc->na.session == session)
2903 GST_blacklist_test_cancel (bcc->bc);
2904 GNUNET_HELLO_address_free (bcc->na.address);
2905 GNUNET_CONTAINER_DLL_remove (bc_head,
2911 if (NULL == (n = lookup_neighbour (peer)))
2912 return GNUNET_NO; /* can't affect us */
2913 if (session != n->primary_address.session)
2915 if (session == n->alternative_address.session)
2917 free_address (&n->alternative_address);
2918 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2919 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2920 n->state = S_CONNECTED;
2924 return GNUNET_NO; /* doesn't affect us further */
2927 n->expect_latency_response = GNUNET_NO;
2930 case S_NOT_CONNECTED:
2932 free_neighbour (n, GNUNET_NO);
2936 free_neighbour (n, GNUNET_NO);
2938 case S_INIT_BLACKLIST:
2939 case S_CONNECT_SENT:
2940 free_address (&n->primary_address);
2941 n->state = S_INIT_ATS;
2942 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2943 // FIXME: need to ask ATS for suggestions again?
2944 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2946 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2947 case S_CONNECT_RECV_ATS:
2948 case S_CONNECT_RECV_BLACKLIST:
2949 case S_CONNECT_RECV_ACK:
2950 /* error on inbound session; free neighbour entirely */
2951 free_address (&n->primary_address);
2952 free_neighbour (n, GNUNET_NO);
2955 free_address (&n->primary_address);
2956 n->state = S_RECONNECT_ATS;
2957 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2958 /* FIXME: is this ATS call needed? */
2959 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2961 case S_RECONNECT_ATS:
2962 /* we don't have an address, how can it go down? */
2965 case S_RECONNECT_BLACKLIST:
2966 case S_RECONNECT_SENT:
2967 n->state = S_RECONNECT_ATS;
2968 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2969 // FIXME: need to ask ATS for suggestions again?
2970 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2972 case S_CONNECTED_SWITCHING_BLACKLIST:
2973 /* primary went down while we were checking secondary against
2974 blacklist, adopt secondary as primary */
2975 free_address (&n->primary_address);
2976 n->primary_address = n->alternative_address;
2977 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2978 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2979 n->state = S_RECONNECT_BLACKLIST;
2981 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2982 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2983 secondary as primary */
2984 free_address (&n->primary_address);
2985 n->primary_address = n->alternative_address;
2986 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2987 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2988 n->state = S_RECONNECT_SENT;
2991 free_address (&n->primary_address);
2993 case S_DISCONNECT_FINISHED:
2994 /* neighbour was freed and plugins told to terminate session */
2998 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3002 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3003 GNUNET_SCHEDULER_cancel (n->task);
3004 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3010 * We received a 'SESSION_ACK' message from the other peer.
3011 * If we sent a 'CONNECT_ACK' last, this means we are now
3012 * connected. Otherwise, do nothing.
3014 * @param message possibly a 'struct SessionConnectMessage' (check format)
3015 * @param peer identity of the peer to switch the address for
3016 * @param address address of the other peer, NULL if other peer
3018 * @param session session to use (or NULL)
3021 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3022 const struct GNUNET_PeerIdentity *peer,
3023 const struct GNUNET_HELLO_Address *address,
3024 struct Session *session)
3026 struct NeighbourMapEntry *n;
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3029 "Received SESSION_ACK message from peer `%s'\n",
3031 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3033 GNUNET_break_op (0);
3036 if (NULL == (n = lookup_neighbour (peer)))
3038 /* check if we are in a plausible state for having sent
3039 a CONNECT_ACK. If not, return, otherwise break */
3040 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3041 (S_CONNECT_SENT != n->state) ) ||
3042 (2 != n->send_connect_ack) )
3044 GNUNET_STATISTICS_update (GST_stats,
3045 gettext_noop ("# unexpected SESSION ACK messages"), 1,
3049 n->state = S_CONNECTED;
3050 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3051 GNUNET_STATISTICS_set (GST_stats,
3052 gettext_noop ("# peers connected"),
3053 ++neighbours_connected,
3055 connect_notify_cb (callback_cls, &n->id,
3056 n->primary_address.bandwidth_in,
3057 n->primary_address.bandwidth_out);
3059 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3060 set_address (&n->primary_address,
3061 n->primary_address.address,
3062 n->primary_address.session,
3063 n->primary_address.bandwidth_in,
3064 n->primary_address.bandwidth_out,
3070 * Test if we're connected to the given peer.
3072 * @param target peer to test
3073 * @return GNUNET_YES if we are connected, GNUNET_NO if not
3076 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3078 return test_connected (lookup_neighbour (target));
3083 * Change the incoming quota for the given peer.
3085 * @param neighbour identity of peer to change qutoa for
3086 * @param quota new quota
3089 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3090 struct GNUNET_BANDWIDTH_Value32NBO quota)
3092 struct NeighbourMapEntry *n;
3094 if (NULL == (n = lookup_neighbour (neighbour)))
3096 GNUNET_STATISTICS_update (GST_stats,
3098 ("# SET QUOTA messages ignored (no such peer)"),
3102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3103 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3104 ntohl (quota.value__), GNUNET_i2s (&n->id));
3105 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3106 if (0 != ntohl (quota.value__))
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3109 GNUNET_i2s (&n->id), "SET_QUOTA");
3110 if (GNUNET_YES == test_connected (n))
3111 GNUNET_STATISTICS_update (GST_stats,
3112 gettext_noop ("# disconnects due to quota of 0"),
3114 disconnect_neighbour (n);
3119 * We received a disconnect message from the given peer,
3120 * validate and process.
3122 * @param peer sender of the message
3123 * @param msg the disconnect message
3126 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3128 const struct GNUNET_MessageHeader
3131 struct NeighbourMapEntry *n;
3132 const struct SessionDisconnectMessage *sdm;
3133 struct GNUNET_HashCode hc;
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Received DISCONNECT message from peer `%s'\n",
3138 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3140 // GNUNET_break_op (0);
3141 GNUNET_STATISTICS_update (GST_stats,
3143 ("# disconnect messages ignored (old format)"), 1,
3147 sdm = (const struct SessionDisconnectMessage *) msg;
3148 if (NULL == (n = lookup_neighbour (peer)))
3149 return; /* gone already */
3150 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3152 GNUNET_STATISTICS_update (GST_stats,
3154 ("# disconnect messages ignored (timestamp)"), 1,
3158 GNUNET_CRYPTO_hash (&sdm->public_key,
3159 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey),
3161 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
3163 GNUNET_break_op (0);
3166 if (ntohl (sdm->purpose.size) !=
3167 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3168 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3169 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3172 "%s message from peer `%s' has invalid size \n",
3175 GNUNET_break_op (0);
3179 GNUNET_CRYPTO_eddsa_verify
3180 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3181 &sdm->signature, &sdm->public_key))
3183 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3184 "%s message from peer `%s' cannot be verified \n",
3187 GNUNET_break_op (0);
3190 if (GNUNET_YES == test_connected (n))
3191 GNUNET_STATISTICS_update (GST_stats,
3193 ("# other peer asked to disconnect from us"), 1,
3195 disconnect_neighbour (n);
3200 * Closure for the neighbours_iterate function.
3202 struct IteratorContext
3205 * Function to call on each connected neighbour.
3207 GST_NeighbourIterator cb;
3217 * Call the callback from the closure for each connected neighbour.
3219 * @param cls the 'struct IteratorContext'
3220 * @param key the hash of the public key of the neighbour
3221 * @param value the 'struct NeighbourMapEntry'
3222 * @return GNUNET_OK (continue to iterate)
3225 neighbours_iterate (void *cls, const struct GNUNET_PeerIdentity * key, void *value)
3227 struct IteratorContext *ic = cls;
3228 struct NeighbourMapEntry *n = value;
3230 if (GNUNET_YES == test_connected (n))
3232 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3233 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3235 if (NULL != n->primary_address.address)
3237 bandwidth_in = n->primary_address.bandwidth_in;
3238 bandwidth_out = n->primary_address.bandwidth_out;
3242 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3243 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3246 ic->cb (ic->cb_cls, &n->id,
3247 n->primary_address.address,
3248 bandwidth_in, bandwidth_out);
3255 * Iterate over all connected neighbours.
3257 * @param cb function to call
3258 * @param cb_cls closure for cb
3261 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3263 struct IteratorContext ic;
3265 if (NULL == neighbours)
3266 return; /* can happen during shutdown */
3269 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3274 * If we have an active connection to the given target, it must be shutdown.
3276 * @param target peer to disconnect from
3279 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3281 struct NeighbourMapEntry *n;
3283 if (NULL == (n = lookup_neighbour (target)))
3284 return; /* not active */
3285 if (GNUNET_YES == test_connected (n))
3286 GNUNET_STATISTICS_update (GST_stats,
3288 ("# disconnected from peer upon explicit request"), 1,
3290 disconnect_neighbour (n);
3295 * Obtain current latency information for the given neighbour.
3297 * @param peer to get the latency for
3298 * @return observed latency of the address, FOREVER if the
3299 * the connection is not up
3301 struct GNUNET_TIME_Relative
3302 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3304 struct NeighbourMapEntry *n;
3306 n = lookup_neighbour (peer);
3308 return GNUNET_TIME_UNIT_FOREVER_REL;
3312 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3313 case S_CONNECTED_SWITCHING_BLACKLIST:
3314 case S_RECONNECT_SENT:
3315 case S_RECONNECT_ATS:
3316 case S_RECONNECT_BLACKLIST:
3318 case S_NOT_CONNECTED:
3319 case S_INIT_BLACKLIST:
3321 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3322 case S_CONNECT_RECV_ATS:
3323 case S_CONNECT_RECV_BLACKLIST:
3324 case S_CONNECT_RECV_ACK:
3325 case S_CONNECT_SENT:
3327 case S_DISCONNECT_FINISHED:
3328 return GNUNET_TIME_UNIT_FOREVER_REL;
3330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3334 return GNUNET_TIME_UNIT_FOREVER_REL;
3339 * Obtain current address information for the given neighbour.
3342 * @return address currently used
3344 struct GNUNET_HELLO_Address *
3345 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3347 struct NeighbourMapEntry *n;
3349 n = lookup_neighbour (peer);
3352 return n->primary_address.address;
3357 * Initialize the neighbours subsystem.
3359 * @param cls closure for callbacks
3360 * @param connect_cb function to call if we connect to a peer
3361 * @param disconnect_cb function to call if we disconnect from a peer
3362 * @param peer_address_cb function to call if we change an active address
3364 * @param max_fds maximum number of fds to use
3367 GST_neighbours_start (void *cls,
3368 NotifyConnect connect_cb,
3369 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3370 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3371 unsigned int max_fds)
3374 connect_notify_cb = connect_cb;
3375 disconnect_notify_cb = disconnect_cb;
3376 address_change_cb = peer_address_cb;
3377 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3378 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3379 utilization_transmission, NULL);
3384 * Disconnect from the given neighbour.
3387 * @param key hash of neighbour's public key (not used)
3388 * @param value the 'struct NeighbourMapEntry' of the neighbour
3389 * @return GNUNET_OK (continue to iterate)
3392 disconnect_all_neighbours (void *cls,
3393 const struct GNUNET_PeerIdentity *key,
3396 struct NeighbourMapEntry *n = value;
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3399 "Disconnecting peer `%4s', %s\n",
3400 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3401 n->state = S_DISCONNECT_FINISHED;
3402 free_neighbour (n, GNUNET_NO);
3408 * Cleanup the neighbours subsystem.
3411 GST_neighbours_stop ()
3413 if (NULL == neighbours)
3415 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3417 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3418 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3421 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3422 &disconnect_all_neighbours,
3424 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3426 callback_cls = NULL;
3427 connect_notify_cb = NULL;
3428 disconnect_notify_cb = NULL;
3429 address_change_cb = NULL;
3433 /* end of file gnunet-service-transport_neighbours.c */