daab2819160cc1e6dc7456679aa88c9d67d20f03
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010-2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_neighbours.c
23  * @brief neighbour management
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
37
38
39
40 /**
41  * Size of the neighbour hash map.
42  */
43 #define NEIGHBOUR_TABLE_SIZE 256
44
45 /**
46  * Time we give plugin to transmit DISCONNECT message before the
47  * neighbour entry self-destructs.
48  */
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
50
51 /**
52  * How often must a peer violate bandwidth quotas before we start
53  * to simply drop its messages?
54  */
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
56
57 /**
58  * How long are we willing to wait for a response from ATS before timing out?
59  */
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
61
62 /**
63  * How long are we willing to wait for an ACK from the other peer before
64  * giving up on our connect operation?
65  */
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
67
68 /**
69  * How long are we willing to wait for a successful reconnect if
70  * an existing connection went down?  Much shorter than the
71  * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
72  * higher layers about the disconnect during this period.
73  */
74 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
75
76 /**
77  * How long are we willing to wait for a response from the blacklist
78  * subsystem before timing out?
79  */
80 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
81
82 /**
83  * Interval to send utilization data
84  */
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
86
87 /**
88  * State describing which kind a reply this neighbour should send
89  */
90 enum GST_ACK_State
91 {
92   /**
93    * We did not receive a CONNECT message for this neighbour
94    */
95   ACK_UNDEFINED = 0,
96
97   /* The neighbour received a CONNECT message and has to send a CONNECT_ACK
98    * as reply */
99   ACK_SEND_CONNECT_ACK = 1,
100
101   /* The neighbour sent a CONNECT_ACK message and has to send a SESSION_ACK
102    * as reply */
103   ACK_SEND_SESSION_ACK = 2
104 };
105
106
107 GNUNET_NETWORK_STRUCT_BEGIN
108
109 /**
110  * Message a peer sends to another to indicate that it intends to
111  * setup a connection/session for data exchange.  A 'SESSION_CONNECT'
112  * should be answered with a 'SESSION_CONNECT_ACK' with the same body
113  * to confirm.  A 'SESSION_CONNECT_ACK' should then be followed with
114  * a 'SESSION_ACK'.  Once the 'SESSION_ACK' is received, both peers
115  * should be connected.
116  */
117 struct SessionConnectMessage
118 {
119   /**
120    * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
121    * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
122    */
123   struct GNUNET_MessageHeader header;
124
125   /**
126    * Always zero.
127    */
128   uint32_t reserved GNUNET_PACKED;
129
130   /**
131    * Absolute time at the sender.  Only the most recent connect
132    * message implies which session is preferred by the sender.
133    */
134   struct GNUNET_TIME_AbsoluteNBO timestamp;
135
136 };
137
138
139 /**
140  * Message a peer sends to another when connected to indicate that a
141  * session is in use and the peer is still alive or to respond to a keep alive.
142  * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
143  * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
144  * When the keep alive response with type is received, transport service
145  * will call the respective plugin to update the session timeout
146  */
147 struct SessionKeepAliveMessage
148 {
149   /**
150    * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
151    * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
152    */
153   struct GNUNET_MessageHeader header;
154
155   /**
156    * A nonce to identify the session the keep alive is used for
157    */
158   uint32_t nonce GNUNET_PACKED;
159 };
160
161 /**
162  * Message we send to the other peer to notify him that we intentionally
163  * are disconnecting (to reduce timeouts).  This is just a friendly
164  * notification, peers must not rely on always receiving disconnect
165  * messages.
166  */
167 struct SessionDisconnectMessage
168 {
169   /**
170    * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
171    */
172   struct GNUNET_MessageHeader header;
173
174   /**
175    * Always zero.
176    */
177   uint32_t reserved GNUNET_PACKED;
178
179   /**
180    * Purpose of the signature.  Extends over the timestamp.
181    * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
182    */
183   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
184
185   /**
186    * Absolute time at the sender.  Only the most recent connect
187    * message implies which session is preferred by the sender.
188    */
189   struct GNUNET_TIME_AbsoluteNBO timestamp;
190
191   /**
192    * Public key of the sender.
193    */
194   struct GNUNET_CRYPTO_EddsaPublicKey public_key;
195
196   /**
197    * Signature of the peer that sends us the disconnect.  Only
198    * valid if the timestamp is AFTER the timestamp from the
199    * corresponding 'CONNECT' message.
200    */
201   struct GNUNET_CRYPTO_EddsaSignature signature;
202
203 };
204
205 GNUNET_NETWORK_STRUCT_END
206
207
208 /**
209  * For each neighbour we keep a list of messages
210  * that we still want to transmit to the neighbour.
211  */
212 struct MessageQueue
213 {
214
215   /**
216    * This is a doubly linked list.
217    */
218   struct MessageQueue *next;
219
220   /**
221    * This is a doubly linked list.
222    */
223   struct MessageQueue *prev;
224
225   /**
226    * Function to call once we're done.
227    */
228   GST_NeighbourSendContinuation cont;
229
230   /**
231    * Closure for @e cont
232    */
233   void *cont_cls;
234
235   /**
236    * The message(s) we want to transmit, GNUNET_MessageHeader(s)
237    * stuck together in memory.  Allocated at the end of this struct.
238    */
239   const char *message_buf;
240
241   /**
242    * Size of the message buf
243    */
244   size_t message_buf_size;
245
246   /**
247    * At what time should we fail?
248    */
249   struct GNUNET_TIME_Absolute timeout;
250
251 };
252
253
254
255
256 /**
257  * A possible address we could use to communicate with a neighbour.
258  */
259 struct NeighbourAddress
260 {
261
262   /**
263    * Active session for this address.
264    */
265   struct Session *session;
266
267   /**
268    * Network-level address information.
269    */
270   struct GNUNET_HELLO_Address *address;
271
272   /**
273    * Timestamp of the 'SESSION_CONNECT' message we sent to the other
274    * peer for this address.  Use to check that the ACK is in response
275    * to our most recent 'CONNECT'.
276    */
277   struct GNUNET_TIME_Absolute connect_timestamp;
278
279   /**
280    * Inbound bandwidth from ATS for this address.
281    */
282   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
283
284   /**
285    * Outbound bandwidth from ATS for this address.
286    */
287   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
288
289   /**
290    * Did we tell ATS that this is our 'active' address?
291    */
292   int ats_active;
293
294   /**
295    * The current nonce sent in the last keep alive messages
296    */
297   uint32_t keep_alive_nonce;
298 };
299
300 /**
301  * Entry in neighbours.
302  */
303 struct NeighbourMapEntry
304 {
305
306   /**
307    * Head of list of messages we would like to send to this peer;
308    * must contain at most one message per client.
309    */
310   struct MessageQueue *messages_head;
311
312   /**
313    * Tail of list of messages we would like to send to this peer; must
314    * contain at most one message per client.
315    */
316   struct MessageQueue *messages_tail;
317
318   /**
319    * Are we currently trying to send a message? If so, which one?
320    */
321   struct MessageQueue *is_active;
322
323   /**
324    * Primary address we currently use to communicate with the neighbour.
325    */
326   struct NeighbourAddress primary_address;
327
328   /**
329    * Alternative address currently under consideration for communicating
330    * with the neighbour.
331    */
332   struct NeighbourAddress alternative_address;
333
334   /**
335    * Identity of this neighbour.
336    */
337   struct GNUNET_PeerIdentity id;
338
339   /**
340    * Main task that drives this peer (timeouts, keepalives, etc.).
341    * Always runs the 'master_task'.
342    */
343   GNUNET_SCHEDULER_TaskIdentifier task;
344
345   /**
346    * At what time should we sent the next keep-alive message?
347    */
348   struct GNUNET_TIME_Absolute keep_alive_time;
349
350   /**
351    * At what time did we sent the last keep-alive message?  Used
352    * to calculate round-trip time ("latency").
353    */
354   struct GNUNET_TIME_Absolute last_keep_alive_time;
355
356   /**
357    * Timestamp we should include in our next CONNECT_ACK message.
358    * (only valid if 'send_connect_ack' is #GNUNET_YES).  Used to build
359    * our CONNECT_ACK message.
360    */
361   struct GNUNET_TIME_Absolute connect_ack_timestamp;
362
363   /**
364    * ATS address suggest handle
365    */
366   struct GNUNET_ATS_SuggestHandle *suggest_handle;
367
368   /**
369    * Time where we should cut the connection (timeout) if we don't
370    * make progress in the state machine (or get a KEEPALIVE_RESPONSE
371    * if we are in S_CONNECTED).
372    */
373   struct GNUNET_TIME_Absolute timeout;
374
375   /**
376    * Latest calculated latency value
377    */
378   struct GNUNET_TIME_Relative latency;
379
380   /**
381    * Tracker for inbound bandwidth.
382    */
383   struct GNUNET_BANDWIDTH_Tracker in_tracker;
384
385   /**
386    * How often has the other peer (recently) violated the inbound
387    * traffic limit?  Incremented by 10 per violation, decremented by 1
388    * per non-violation (for each time interval).
389    */
390   unsigned int quota_violation_count;
391
392   /**
393    * The current state of the peer.
394    */
395   enum GNUNET_TRANSPORT_PeerState state;
396
397   /**
398    * Did we sent an KEEP_ALIVE message and are we expecting a response?
399    */
400   int expect_latency_response;
401
402   /**
403    * Flag to set if we still need to send a CONNECT_ACK message to the other peer
404    * (once we have an address to use and the peer has been allowed by our
405    * blacklist).  Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_CONNECT_ACK
406    * if we need to send a CONNECT_ACK.  Set to #ACK_SEND_CONNECT_ACK if we did
407    * send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving a
408    * 'SESSION_ACK' (regardless of what our own state machine might say).
409    */
410   enum GST_ACK_State ack_state;
411
412   /**
413    * Tracking utilization of outbound bandwidth
414    */
415   uint32_t util_payload_bytes_sent;
416
417   /**
418    * Tracking utilization of inbound bandwidth
419    */
420   uint32_t util_payload_bytes_recv;
421
422   /**
423    * Tracking utilization of outbound bandwidth
424    */
425   uint32_t util_total_bytes_sent;
426
427   /**
428    * Tracking utilization of inbound bandwidth
429    */
430   uint32_t util_total_bytes_recv;
431
432   /**
433    * Date of last utilization transmission
434    */
435   struct GNUNET_TIME_Absolute last_util_transmission;
436 };
437
438
439 /**
440  * Context for blacklist checks and the #handle_test_blacklist_cont()
441  * function.  Stores information about ongoing blacklist checks.
442  */
443 struct BlackListCheckContext
444 {
445
446   /**
447    * We keep blacklist checks in a DLL.
448    */
449   struct BlackListCheckContext *next;
450
451   /**
452    * We keep blacklist checks in a DLL.
453    */
454   struct BlackListCheckContext *prev;
455
456   /**
457    * Address that is being checked.
458    */
459   struct NeighbourAddress na;
460
461   /**
462    * Handle to the ongoing blacklist check.
463    */
464   struct GST_BlacklistCheck *bc;
465 };
466
467
468 /**
469  * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
470  */
471 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
472
473 /**
474  * We keep blacklist checks in a DLL so that we can find
475  * the 'sessions' in their 'struct NeighbourAddress' if
476  * a session goes down.
477  */
478 static struct BlackListCheckContext *bc_head;
479
480 /**
481  * We keep blacklist checks in a DLL.
482  */
483 static struct BlackListCheckContext *bc_tail;
484
485 /**
486  * List of pending blacklist checks: head
487  */
488 static struct BlacklistCheckSwitchContext *pending_bc_head;
489
490 /**
491  * List of pending blacklist checks: tail
492  */
493 static struct BlacklistCheckSwitchContext *pending_bc_tail;
494
495 /**
496  * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
497  */
498 static void *callback_cls;
499
500 /**
501  * Function to call when we connected to a neighbour.
502  */
503 static NotifyConnect connect_notify_cb;
504
505 /**
506  * Function to call when we disconnected from a neighbour.
507  */
508 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
509
510 /**
511  * Function to call when a neighbour changed address, state or bandwidth.
512  */
513 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
514
515 /**
516  * counter for connected neighbours
517  */
518 static unsigned int neighbours_connected;
519
520 /**
521  * Number of bytes we have currently queued for transmission.
522  */
523 static unsigned long long bytes_in_send_queue;
524
525 /**
526  * Task transmitting utilization data
527  */
528 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
529
530
531 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
532
533 static char *
534 print_ack_state (enum GST_ACK_State s)
535 {
536   switch (s) {
537     case ACK_UNDEFINED:
538       return "UNDEFINED";
539     case ACK_SEND_CONNECT_ACK:
540       return "SEND_CONNECT_ACK";
541     case ACK_SEND_SESSION_ACK:
542       return "SEND_SESSION_ACK";
543     default:
544       GNUNET_break (0);
545       return "N/A";
546   }
547 }
548
549 /**
550  * Lookup a neighbour entry in the neighbours hash map.
551  *
552  * @param pid identity of the peer to look up
553  * @return the entry, NULL if there is no existing record
554  */
555 static struct NeighbourMapEntry *
556 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
557 {
558   if (NULL == neighbours)
559     return NULL;
560   return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
561 }
562
563
564 /**
565  * Test if we're connected to the given peer.
566  *
567  * @param n neighbour entry of peer to test
568  * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
569  */
570 static int
571 test_connected (struct NeighbourMapEntry *n)
572 {
573   if (NULL == n)
574     return GNUNET_NO;
575   return GNUNET_TRANSPORT_is_connected (n->state);
576 }
577
578 /**
579  * Send information about a new outbound quota to our clients.
580  *
581  * @param target affected peer
582  * @param quota new quota
583  */
584 static void
585 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
586                      struct GNUNET_BANDWIDTH_Value32NBO quota)
587 {
588   struct QuotaSetMessage q_msg;
589
590   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591               "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
592               ntohl (quota.value__), GNUNET_i2s (target));
593   q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
594   q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
595   q_msg.quota = quota;
596   q_msg.peer = (*target);
597   GST_clients_broadcast (&q_msg.header, GNUNET_NO);
598 }
599
600
601 /**
602  * We don't need a given neighbour address any more.
603  * Release its resources and give appropriate notifications
604  * to ATS and other subsystems.
605  *
606  * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
607  */
608 static void
609 free_address (struct NeighbourAddress *na)
610 {
611   if (GNUNET_YES == na->ats_active)
612   {
613     GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
614     GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
615   }
616
617   na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
618   na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
619   na->ats_active = GNUNET_NO;
620   na->keep_alive_nonce = 0;
621   if (NULL != na->address)
622   {
623     GNUNET_HELLO_address_free (na->address);
624     na->address = NULL;
625   }
626   na->session = NULL;
627 }
628
629
630 /**
631  * Set net state for this neighbour and notify monitoring
632  *
633  * @param n the respective neighbour
634  * @param s the new state
635  */
636 static void
637 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
638 {
639   n->state = s;
640   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
641       GNUNET_i2s (&n->id),
642       GNUNET_TRANSPORT_ps2s(s));
643   neighbour_change_cb (callback_cls,
644       &n->id,
645       n->primary_address.address,
646       n->state, n->timeout,
647       n->primary_address.bandwidth_in,
648       n->primary_address.bandwidth_out);
649 }
650
651
652 /**
653  * Set net state and state timeout for this neighbour and notify monitoring
654  *
655  * @param n the respective neighbour
656  * @param s the new state
657  * @param timeout the new timeout
658  */
659 static void
660 set_state_and_timeout (struct NeighbourMapEntry *n,
661     enum GNUNET_TRANSPORT_PeerState s,
662     struct GNUNET_TIME_Absolute timeout)
663 {
664   n->state = s;
665   n->timeout = timeout;
666   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
667       GNUNET_i2s (&n->id),
668       GNUNET_TRANSPORT_ps2s(s),
669       GNUNET_STRINGS_absolute_time_to_string (timeout));
670   neighbour_change_cb (callback_cls,
671       &n->id,
672       n->primary_address.address,
673       n->state, n->timeout,
674       n->primary_address.bandwidth_in,
675       n->primary_address.bandwidth_out);
676 }
677
678
679 /**
680  * Set new state timeout for this neighbour and notify monitoring
681  *
682  * @param n the respective neighbour
683  * @param timeout the new timeout
684  */
685 static void
686 set_timeout (struct NeighbourMapEntry *n,
687     struct GNUNET_TIME_Absolute timeout)
688 {
689   n->timeout = timeout;
690   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
691       GNUNET_i2s (&n->id),
692       GNUNET_STRINGS_absolute_time_to_string (timeout));
693   neighbour_change_cb (callback_cls,
694       &n->id,
695       n->primary_address.address,
696       n->state, n->timeout,
697       n->primary_address.bandwidth_in,
698       n->primary_address.bandwidth_out);
699 }
700
701
702 /**
703  * Initialize the alternative address of a neighbour
704  *
705  * @param n the neighbour
706  * @param address address of the other peer, NULL if other peer
707  *                       connected to us
708  * @param session session to use (or NULL, in which case an
709  *        address must be setup)
710  * @param bandwidth_in inbound quota to be used when connection is up
711  * @param bandwidth_out outbound quota to be used when connection is up
712  */
713 static void
714 set_alternative_address (struct NeighbourMapEntry *n,
715              const struct GNUNET_HELLO_Address *address,
716              struct Session *session,
717              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
718              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
719 {
720   struct GNUNET_TRANSPORT_PluginFunctions *papi;
721   if (NULL == (papi = GST_plugins_find (address->transport_name)))
722   {
723     GNUNET_break (0);
724     return;
725   }
726   if (session == n->alternative_address.session)
727   {
728     n->alternative_address.bandwidth_in = bandwidth_in;
729     n->alternative_address.bandwidth_out = bandwidth_out;
730     return;
731   }
732   free_address (&n->alternative_address);
733   if (NULL == session)
734     session = papi->get_session (papi->cls, address);
735   if (NULL == session)
736   {
737     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738                 "Failed to obtain new session for peer `%s' and  address '%s'\n",
739                 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
740     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
741     return;
742   }
743
744   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
745       GNUNET_i2s (&n->id),
746       GST_plugins_a2s(address));
747
748   n->alternative_address.address = GNUNET_HELLO_address_copy (address);
749   n->alternative_address.bandwidth_in = bandwidth_in;
750   n->alternative_address.bandwidth_out = bandwidth_out;
751   n->alternative_address.session = session;
752   n->alternative_address.ats_active = GNUNET_NO;
753   n->alternative_address.keep_alive_nonce = 0;
754 }
755
756
757 /**
758  * Initialize the primary address of a neighbour
759  *
760  * @param n the neighbour
761  * @param address address of the other peer, NULL if other peer
762  *                       connected to us
763  * @param session session to use (or NULL, in which case an
764  *        address must be setup)
765  * @param bandwidth_in inbound quota to be used when connection is up
766  * @param bandwidth_out outbound quota to be used when connection is up
767  * @param is_active #GNUNET_YES to mark this as the active address with ATS
768  */
769 static void
770 set_primary_address (struct NeighbourMapEntry *n,
771              const struct GNUNET_HELLO_Address *address,
772              struct Session *session,
773              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
774              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
775              int is_active)
776 {
777   struct GNUNET_TRANSPORT_PluginFunctions *papi;
778
779   if (NULL == (papi = GST_plugins_find (address->transport_name)))
780   {
781     GNUNET_break (0);
782     return;
783   }
784   if (session == n->primary_address.session)
785   {
786     n->primary_address.bandwidth_in = bandwidth_in;
787     n->primary_address.bandwidth_out = bandwidth_out;
788     if (is_active != n->primary_address.ats_active)
789     {
790       n->primary_address.ats_active = is_active;
791       GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
792       GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
793     }
794     if (GNUNET_YES == is_active)
795     {
796       GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
797       send_outbound_quota (&address->peer, bandwidth_out);
798     }
799     return;
800   }
801   free_address (&n->primary_address);
802   if (NULL == session)
803     session = papi->get_session (papi->cls, address);
804   if (NULL == session)
805   {
806     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807                 "Failed to obtain new session for peer `%s' and  address '%s'\n",
808                 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
809     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
810     return;
811   }
812
813   n->primary_address.address = GNUNET_HELLO_address_copy (address);
814   n->primary_address.bandwidth_in = bandwidth_in;
815   n->primary_address.bandwidth_out = bandwidth_out;
816   n->primary_address.session = session;
817   n->primary_address.ats_active = is_active;
818   n->primary_address.keep_alive_nonce = 0;
819   if (GNUNET_YES == is_active)
820   {
821     /* Telling ATS about new session */
822     GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
823     GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
824     GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
825     send_outbound_quota (&address->peer, bandwidth_out);
826   }
827
828   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
829       GNUNET_i2s (&n->id),
830       GST_plugins_a2s(address));
831
832   neighbour_change_cb (callback_cls,
833       &n->id,
834       n->primary_address.address,
835       n->state, n->timeout,
836       n->primary_address.bandwidth_in,
837       n->primary_address.bandwidth_out);
838 }
839
840 /**
841  * Clear the primary address of a neighbour since this primary address is not
842  * valid anymore and notify monitoring about it
843  *
844  * @param n the neighbour
845  */
846 static void
847 unset_primary_address (struct NeighbourMapEntry *n)
848 {
849   /* Unset primary address */
850   free_address (&n->primary_address);
851
852   /* Notify monitoring about it */
853   neighbour_change_cb (callback_cls,
854       &n->id,
855       NULL,
856       n->state, n->timeout,
857       n->primary_address.bandwidth_in,
858       n->primary_address.bandwidth_out);
859 }
860
861
862
863 /**
864  * Free a neighbour map entry.
865  *
866  * @param n entry to free
867  * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
868  *                      #GNUNET_YES to keep all sessions
869  */
870 static void
871 free_neighbour (struct NeighbourMapEntry *n,
872                 int keep_sessions)
873 {
874   struct MessageQueue *mq;
875   struct GNUNET_TRANSPORT_PluginFunctions *papi;
876   struct GNUNET_HELLO_Address *backup_primary;
877
878   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879               "Freeing neighbour state of peer `%s'\n",
880               GNUNET_i2s (&n->id));
881   n->is_active = NULL; /* always free'd by its own continuation! */
882
883   /* fail messages currently in the queue */
884   while (NULL != (mq = n->messages_head))
885   {
886     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
887     if (NULL != mq->cont)
888       mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
889     GNUNET_free (mq);
890   }
891   /* It is too late to send other peer disconnect notifications, but at
892      least internally we need to get clean... */
893   if (GNUNET_YES == test_connected (n))
894   {
895     GNUNET_STATISTICS_set (GST_stats,
896                            gettext_noop ("# peers connected"),
897                            --neighbours_connected,
898                            GNUNET_NO);
899     disconnect_notify_cb (callback_cls, &n->id);
900   }
901
902   /* Mark peer as disconnected */
903   set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
904
905   if (NULL != n->primary_address.address)
906     backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
907   else
908     backup_primary = NULL;
909
910   /* free addresses and mark as unused */
911   unset_primary_address (n);
912   free_address (&n->alternative_address);
913
914   /* cut all transport-level connection for this peer */
915   if ((GNUNET_NO == keep_sessions) &&
916       (NULL != backup_primary) &&
917       (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
918     papi->disconnect_peer (papi->cls, &n->id);
919
920   if (NULL != backup_primary)
921     GNUNET_HELLO_address_free (backup_primary);
922   GNUNET_assert (GNUNET_YES ==
923                  GNUNET_CONTAINER_multipeermap_remove (neighbours,
924                                                        &n->id, n));
925
926   /* Cancel address requests for this peer */
927   if (NULL != n->suggest_handle)
928   {
929     GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
930     n->suggest_handle = NULL;
931   }
932
933   /* Cancel the master task */
934   if (GNUNET_SCHEDULER_NO_TASK != n->task)
935   {
936     GNUNET_SCHEDULER_cancel (n->task);
937     n->task = GNUNET_SCHEDULER_NO_TASK;
938   }
939   /* free rest of memory */
940   GNUNET_free (n);
941 }
942
943
944 /**
945  * Transmit a message using the current session of the given
946  * neighbour.
947  *
948  * @param n entry for the recipient
949  * @param msgbuf buffer to transmit
950  * @param msgbuf_size number of bytes in @a msgbuf buffer
951  * @param priority transmission priority
952  * @param timeout transmission timeout
953  * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
954  *        timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
955  * @param cont continuation to call when finished (can be NULL)
956  * @param cont_cls closure for @a cont
957  * @return timeout (copy of @a timeout or a calculated one if
958  *         @a use_keepalive_timeout is #GNUNET_YES.
959  */
960 static struct GNUNET_TIME_Relative
961 send_with_session (struct NeighbourMapEntry *n,
962                    const char *msgbuf, size_t msgbuf_size,
963                    uint32_t priority,
964                    struct GNUNET_TIME_Relative timeout,
965                    unsigned int use_keepalive_timeout,
966                    GNUNET_TRANSPORT_TransmitContinuation cont,
967                    void *cont_cls)
968 {
969   struct GNUNET_TRANSPORT_PluginFunctions *papi;
970   struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
971
972   GNUNET_assert (n->primary_address.session != NULL);
973   if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
974          (-1 == papi->send (papi->cls,
975                             n->primary_address.session,
976                             msgbuf, msgbuf_size,
977                             priority,
978                             (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
979                                 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
980                                                              papi->query_keepalive_factor (papi->cls))),
981                             cont, cont_cls)))) &&
982        (NULL != cont))
983     cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
984   GST_neighbours_notify_data_sent (&n->id,
985       n->primary_address.address, n->primary_address.session, msgbuf_size);
986   GNUNET_break (NULL != papi);
987   return result;
988 }
989
990
991 /**
992  * Master task run for every neighbour.  Performs all of the time-related
993  * activities (keep alive, send next message, disconnect if idle, finish
994  * clean up after disconnect).
995  *
996  * @param cls the `struct NeighbourMapEntry` for which we are running
997  * @param tc scheduler context (unused)
998  */
999 static void
1000 master_task (void *cls,
1001              const struct GNUNET_SCHEDULER_TaskContext *tc);
1002
1003
1004 /**
1005  * Function called when the 'DISCONNECT' message has been sent by the
1006  * plugin.  Frees the neighbour --- if the entry still exists.
1007  *
1008  * @param cls NULL
1009  * @param target identity of the neighbour that was disconnected
1010  * @param result #GNUNET_OK if the disconnect got out successfully
1011  * @param payload bytes payload
1012  * @param physical bytes physical
1013  */
1014 static void
1015 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1016                       int result, size_t payload, size_t physical)
1017 {
1018   struct NeighbourMapEntry *n;
1019
1020   n = lookup_neighbour (target);
1021   if (NULL == n)
1022     return; /* already gone */
1023   if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1024     return; /* have created a fresh entry since */
1025   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1026     GNUNET_SCHEDULER_cancel (n->task);
1027   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1028 }
1029
1030
1031 /**
1032  * Transmit a DISCONNECT message to the other peer.
1033  *
1034  * @param n neighbour to send DISCONNECT message.
1035  */
1036 static void
1037 send_disconnect (struct NeighbourMapEntry *n)
1038 {
1039   struct SessionDisconnectMessage disconnect_msg;
1040
1041   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042               "Sending DISCONNECT message to peer `%4s'\n",
1043               GNUNET_i2s (&n->id));
1044   disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1045   disconnect_msg.header.type =
1046       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1047   disconnect_msg.reserved = htonl (0);
1048   disconnect_msg.purpose.size =
1049       htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1050              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1051              sizeof (struct GNUNET_TIME_AbsoluteNBO));
1052   disconnect_msg.purpose.purpose =
1053       htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1054   disconnect_msg.timestamp =
1055       GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1056   disconnect_msg.public_key = GST_my_identity.public_key;
1057   GNUNET_assert (GNUNET_OK ==
1058                  GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1059                                          &disconnect_msg.purpose,
1060                                          &disconnect_msg.signature));
1061
1062   (void) send_with_session (n,
1063                             (const char *) &disconnect_msg, sizeof (disconnect_msg),
1064                             UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1065                             GNUNET_NO, &send_disconnect_cont, NULL);
1066   GNUNET_STATISTICS_update (GST_stats,
1067                             gettext_noop
1068                             ("# DISCONNECT messages sent"), 1,
1069                             GNUNET_NO);
1070 }
1071
1072
1073 /**
1074  * Disconnect from the given neighbour, clean up the record.
1075  *
1076  * @param n neighbour to disconnect from
1077  */
1078 static void
1079 disconnect_neighbour (struct NeighbourMapEntry *n)
1080 {
1081   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1082               "Disconnecting from peer %s in state %s\n",
1083               GNUNET_i2s (&n->id),
1084               GNUNET_TRANSPORT_ps2s (n->state));
1085   /* depending on state, notify neighbour and/or upper layers of this peer
1086      about disconnect */
1087   switch (n->state)
1088   {
1089   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1090   case GNUNET_TRANSPORT_PS_INIT_ATS:
1091     /* other peer is completely unaware of us, no need to send DISCONNECT */
1092     free_neighbour (n, GNUNET_NO);
1093     return;
1094   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1095     send_disconnect (n);
1096     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1097     break;
1098   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1099   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1100   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1101     /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1102     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1103     free_neighbour (n, GNUNET_NO);
1104     return;
1105   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1106     /* we DID ACK the other peer's request, must send DISCONNECT */
1107     send_disconnect (n);
1108     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1109     break;
1110   case GNUNET_TRANSPORT_PS_CONNECTED:
1111   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1112   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1113   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1114   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1115     /* we are currently connected, need to send disconnect and do
1116        internal notifications and update statistics */
1117     send_disconnect (n);
1118     GNUNET_STATISTICS_set (GST_stats,
1119                            gettext_noop ("# peers connected"),
1120                            --neighbours_connected,
1121                            GNUNET_NO);
1122     disconnect_notify_cb (callback_cls, &n->id);
1123     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1124     break;
1125   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1126     /* ATS address request timeout, disconnect without sending disconnect message */
1127     GNUNET_STATISTICS_set (GST_stats,
1128                            gettext_noop ("# peers connected"),
1129                            --neighbours_connected,
1130                            GNUNET_NO);
1131     disconnect_notify_cb (callback_cls, &n->id);
1132     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1133     break;
1134   case GNUNET_TRANSPORT_PS_DISCONNECT:
1135     /* already disconnected, ignore */
1136     break;
1137   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1138     /* already cleaned up, how did we get here!? */
1139     GNUNET_assert (0);
1140     break;
1141   default:
1142     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1143                 "Unhandled state `%s'\n",
1144                 GNUNET_TRANSPORT_ps2s (n->state));
1145     GNUNET_break (0);
1146     break;
1147   }
1148   /* schedule timeout to clean up */
1149   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1150     GNUNET_SCHEDULER_cancel (n->task);
1151   n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1152                                           &master_task, n);
1153 }
1154
1155
1156 /**
1157  * We're done with our transmission attempt, continue processing.
1158  *
1159  * @param cls the `struct MessageQueue` of the message
1160  * @param receiver intended receiver
1161  * @param success whether it worked or not
1162  * @param size_payload bytes payload sent
1163  * @param physical bytes sent on wire
1164  */
1165 static void
1166 transmit_send_continuation (void *cls,
1167                             const struct GNUNET_PeerIdentity *receiver,
1168                             int success, size_t size_payload, size_t physical)
1169 {
1170   struct MessageQueue *mq = cls;
1171   struct NeighbourMapEntry *n;
1172
1173   if (NULL == (n = lookup_neighbour (receiver)))
1174   {
1175     GNUNET_free (mq);
1176     return; /* disconnect or other error while transmitting, can happen */
1177   }
1178   if (n->is_active == mq)
1179   {
1180     /* this is still "our" neighbour, remove us from its queue
1181        and allow it to send the next message now */
1182     n->is_active = NULL;
1183     if (GNUNET_SCHEDULER_NO_TASK != n->task)
1184       GNUNET_SCHEDULER_cancel (n->task);
1185     n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1186   }
1187   if (bytes_in_send_queue < mq->message_buf_size)
1188   {
1189     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1190                 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1191                 bytes_in_send_queue, mq->message_buf_size,
1192                 (GNUNET_OK == success) ? "OK" : "FAIL",
1193                 size_payload, physical);
1194     GNUNET_break (0);
1195   }
1196
1197
1198   GNUNET_break (size_payload == mq->message_buf_size);
1199   bytes_in_send_queue -= mq->message_buf_size;
1200   GNUNET_STATISTICS_set (GST_stats,
1201                         gettext_noop
1202                          ("# bytes in message queue for other peers"),
1203                          bytes_in_send_queue, GNUNET_NO);
1204   if (GNUNET_OK == success)
1205     GNUNET_STATISTICS_update (GST_stats,
1206                               gettext_noop
1207                               ("# messages transmitted to other peers"),
1208                               1, GNUNET_NO);
1209   else
1210     GNUNET_STATISTICS_update (GST_stats,
1211                               gettext_noop
1212                               ("# transmission failures for messages to other peers"),
1213                               1, GNUNET_NO);
1214   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215               "Sending message to `%s' of type %u was a %s\n",
1216               GNUNET_i2s (receiver),
1217               ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1218               (success == GNUNET_OK) ? "success" : "FAILURE");
1219   if (NULL != mq->cont)
1220     mq->cont (mq->cont_cls, success, size_payload, physical);
1221   GNUNET_free (mq);
1222 }
1223
1224
1225 /**
1226  * Check the message list for the given neighbour and if we can
1227  * send a message, do so.  This function should only be called
1228  * if the connection is at least generally ready for transmission.
1229  * While we will only send one message at a time, no bandwidth
1230  * quota management is performed here.  If a message was given to
1231  * the plugin, the continuation will automatically re-schedule
1232  * the 'master' task once the next message might be transmitted.
1233  *
1234  * @param n target peer for which to transmit
1235  */
1236 static void
1237 try_transmission_to_peer (struct NeighbourMapEntry *n)
1238 {
1239   struct MessageQueue *mq;
1240   struct GNUNET_TIME_Relative timeout;
1241
1242   if (NULL == n->primary_address.address)
1243   {
1244     /* no address, why are we here? */
1245     GNUNET_break (0);
1246     return;
1247   }
1248   if ((0 == n->primary_address.address->address_length) &&
1249       (NULL == n->primary_address.session))
1250   {
1251     /* no address, why are we here? */
1252     GNUNET_break (0);
1253     return;
1254   }
1255   if (NULL != n->is_active)
1256   {
1257     /* transmission already pending */
1258     return;
1259   }
1260
1261   /* timeout messages from the queue that are past their due date */
1262   while (NULL != (mq = n->messages_head))
1263   {
1264     timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1265     if (timeout.rel_value_us > 0)
1266       break;
1267     GNUNET_STATISTICS_update (GST_stats,
1268                               gettext_noop
1269                               ("# messages timed out while in transport queue"),
1270                               1, GNUNET_NO);
1271     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1272     n->is_active = mq;
1273     transmit_send_continuation (mq, &n->id,
1274                                 GNUNET_SYSERR,
1275                                 mq->message_buf_size, 0);     /* timeout */
1276   }
1277   if (NULL == mq)
1278     return;                     /* no more messages */
1279   GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1280   n->is_active = mq;
1281   (void) send_with_session (n,
1282                             mq->message_buf, mq->message_buf_size,
1283                             0 /* priority */, timeout, GNUNET_NO,
1284                             &transmit_send_continuation, mq);
1285 }
1286
1287
1288 /**
1289  * Send keepalive message to the neighbour.  Must only be called
1290  * if we are on 'connected' state or while trying to switch addresses.
1291  * Will internally determine if a keepalive is truly needed (so can
1292  * always be called).
1293  *
1294  * @param n neighbour that went idle and needs a keepalive
1295  */
1296 static void
1297 send_keepalive (struct NeighbourMapEntry *n)
1298 {
1299   struct SessionKeepAliveMessage m;
1300   struct GNUNET_TIME_Relative timeout;
1301   uint32_t nonce;
1302
1303   GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1304                  (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1305                  (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1306   if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1307     return; /* no keepalive needed at this time */
1308
1309   nonce = 0; /* 0 indicates 'not set' */
1310   while (0 == nonce)
1311     nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1312
1313   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314       "Sending keep alive to peer `%s' with nonce %u\n",
1315       GNUNET_i2s (&n->id), nonce);
1316
1317   m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1318   m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1319   m.nonce = htonl (nonce);
1320
1321   timeout = send_with_session (n,
1322                                (const void *) &m, sizeof (m),
1323                                UINT32_MAX /* priority */,
1324                                GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1325                                NULL, NULL);
1326   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1327                             GNUNET_NO);
1328   n->primary_address.keep_alive_nonce = nonce;
1329   n->expect_latency_response = GNUNET_YES;
1330   n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1331   n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1332
1333 }
1334
1335
1336 /**
1337  * Keep the connection to the given neighbour alive longer,
1338  * we received a KEEPALIVE (or equivalent); send a response.
1339  *
1340  * @param neighbour neighbour to keep alive (by sending keep alive response)
1341  * @param m the keep alive message containing the nonce to respond to
1342  */
1343 void
1344 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1345     const struct GNUNET_MessageHeader *m)
1346 {
1347   struct NeighbourMapEntry *n;
1348   const struct SessionKeepAliveMessage *msg_in;
1349   struct SessionKeepAliveMessage msg;
1350
1351   if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1352     return;
1353
1354   msg_in = (struct SessionKeepAliveMessage *) m;
1355   if (NULL == (n = lookup_neighbour (neighbour)))
1356   {
1357     GNUNET_STATISTICS_update (GST_stats,
1358                               gettext_noop
1359                               ("# KEEPALIVE messages discarded (peer unknown)"),
1360                               1, GNUNET_NO);
1361     return;
1362   }
1363   if (NULL == n->primary_address.session)
1364   {
1365     GNUNET_STATISTICS_update (GST_stats,
1366                               gettext_noop
1367                               ("# KEEPALIVE messages discarded (no session)"),
1368                               1, GNUNET_NO);
1369     return;
1370   }
1371
1372   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373       "Received keep alive request from peer `%s' with nonce %u\n",
1374       GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1375
1376   /* send reply to allow neighbour to measure latency */
1377   msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1378   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1379   msg.nonce = msg_in->nonce;
1380   (void) send_with_session(n,
1381                            (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1382                            UINT32_MAX /* priority */,
1383                            GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1384                            NULL, NULL);
1385 }
1386
1387
1388 /**
1389  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1390  * latency to this peer.  Pass the updated information (existing ats
1391  * plus calculated latency) to ATS.
1392  *
1393  * @param neighbour neighbour to keep alive
1394  * @param m the message containing the keep alive response
1395  */
1396 void
1397 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1398     const struct GNUNET_MessageHeader *m)
1399 {
1400   struct NeighbourMapEntry *n;
1401   const struct SessionKeepAliveMessage *msg;
1402   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1403   uint32_t latency;
1404   struct GNUNET_ATS_Information ats;
1405
1406   if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1407     return;
1408
1409   msg = (const struct SessionKeepAliveMessage *) m;
1410   if (NULL == (n = lookup_neighbour (neighbour)))
1411   {
1412     GNUNET_STATISTICS_update (GST_stats,
1413                               gettext_noop
1414                               ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1415                               1, GNUNET_NO);
1416     return;
1417   }
1418   if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1419        (GNUNET_YES != n->expect_latency_response) )
1420   {
1421     GNUNET_STATISTICS_update (GST_stats,
1422                               gettext_noop
1423                               ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1424                               1, GNUNET_NO);
1425     return;
1426   }
1427   if (NULL == n->primary_address.address)
1428   {
1429     GNUNET_STATISTICS_update (GST_stats,
1430                               gettext_noop
1431                               ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1432                               1, GNUNET_NO);
1433     return;
1434   }
1435   if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1436   {
1437     GNUNET_STATISTICS_update (GST_stats,
1438                               gettext_noop
1439                               ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1440                               1, GNUNET_NO);
1441     return;
1442   }
1443   else
1444   {
1445     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446         "Received keep alive response from peer `%s' for session %p\n",
1447         GNUNET_i2s (&n->id), n->primary_address.session);
1448
1449   }
1450
1451   /* Update session timeout here */
1452   if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1453   {
1454     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455         "Updating session for peer `%s' for session %p\n",
1456         GNUNET_i2s (&n->id), n->primary_address.session);
1457     papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1458   }
1459   else
1460   {
1461     GNUNET_break (0);
1462   }
1463
1464   n->primary_address.keep_alive_nonce = 0;
1465   n->expect_latency_response = GNUNET_NO;
1466   n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1467   set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1468
1469   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470               "Latency for peer `%s' is %s\n",
1471               GNUNET_i2s (&n->id),
1472               GNUNET_STRINGS_relative_time_to_string (n->latency,
1473                                                       GNUNET_YES));
1474   /* append latency */
1475   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1476   if (n->latency.rel_value_us > UINT32_MAX)
1477     latency = UINT32_MAX;
1478   else
1479     latency = n->latency.rel_value_us;
1480   ats.value = htonl (latency);
1481   GST_ats_update_metrics (&n->id, n->primary_address.address,
1482       n->primary_address.session, &ats, 1);
1483 }
1484
1485
1486 /**
1487  * We have received a message from the given sender.  How long should
1488  * we delay before receiving more?  (Also used to keep the peer marked
1489  * as live).
1490  *
1491  * @param sender sender of the message
1492  * @param size size of the message
1493  * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1494  *                   #GNUNET_NO if the neighbour is not connected or violates the quota,
1495  *                   #GNUNET_SYSERR if the connection is not fully up yet
1496  * @return how long to wait before reading more from this sender
1497  */
1498 struct GNUNET_TIME_Relative
1499 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1500                                         *sender, ssize_t size, int *do_forward)
1501 {
1502   struct NeighbourMapEntry *n;
1503   struct GNUNET_TIME_Relative ret;
1504
1505   if (NULL == neighbours)
1506   {
1507     *do_forward = GNUNET_NO;
1508     return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1509   }
1510   if (NULL == (n = lookup_neighbour (sender)))
1511   {
1512     GST_neighbours_try_connect (sender);
1513     if (NULL == (n = lookup_neighbour (sender)))
1514     {
1515       GNUNET_STATISTICS_update (GST_stats,
1516                                 gettext_noop
1517                                 ("# messages discarded due to lack of neighbour record"),
1518                                 1, GNUNET_NO);
1519       *do_forward = GNUNET_NO;
1520       return GNUNET_TIME_UNIT_ZERO;
1521     }
1522   }
1523   if (! test_connected (n))
1524   {
1525     *do_forward = GNUNET_SYSERR;
1526     return GNUNET_TIME_UNIT_ZERO;
1527   }
1528   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1529   {
1530     n->quota_violation_count++;
1531     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532                 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1533                 n->in_tracker.available_bytes_per_s__,
1534                 n->quota_violation_count);
1535     /* Discount 32k per violation */
1536     GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1537   }
1538   else
1539   {
1540     if (n->quota_violation_count > 0)
1541     {
1542       /* try to add 32k back */
1543       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1544       n->quota_violation_count--;
1545     }
1546   }
1547   if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1548   {
1549     GNUNET_STATISTICS_update (GST_stats,
1550                               gettext_noop
1551                               ("# bandwidth quota violations by other peers"),
1552                               1, GNUNET_NO);
1553     *do_forward = GNUNET_NO;
1554     return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1555   }
1556   *do_forward = GNUNET_YES;
1557   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1558   if (ret.rel_value_us > 0)
1559   {
1560     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561                 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1562                 (unsigned long long) n->in_tracker.
1563                 consumption_since_last_update__,
1564                 (unsigned int) n->in_tracker.available_bytes_per_s__,
1565                 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1566     GNUNET_STATISTICS_update (GST_stats,
1567                               gettext_noop ("# ms throttling suggested"),
1568                               (int64_t) ret.rel_value_us / 1000LL,
1569                               GNUNET_NO);
1570   }
1571   return ret;
1572 }
1573
1574
1575 /**
1576  * Transmit a message to the given target using the active connection.
1577  *
1578  * @param target destination
1579  * @param msg message to send
1580  * @param msg_size number of bytes in msg
1581  * @param timeout when to fail with timeout
1582  * @param cont function to call when done
1583  * @param cont_cls closure for 'cont'
1584  */
1585 void
1586 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1587                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
1588                      GST_NeighbourSendContinuation cont, void *cont_cls)
1589 {
1590   struct NeighbourMapEntry *n;
1591   struct MessageQueue *mq;
1592
1593   /* All ove these cases should never happen; they are all API violations.
1594      But we check anyway, just to be sure. */
1595   if (NULL == (n = lookup_neighbour (target)))
1596   {
1597     GNUNET_break (0);
1598     if (NULL != cont)
1599       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1600     return;
1601   }
1602   if (GNUNET_YES != test_connected (n))
1603   {
1604     GNUNET_break (0);
1605     if (NULL != cont)
1606       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1607     return;
1608   }
1609   bytes_in_send_queue += msg_size;
1610   GNUNET_STATISTICS_set (GST_stats,
1611                          gettext_noop
1612                          ("# bytes in message queue for other peers"),
1613                          bytes_in_send_queue, GNUNET_NO);
1614   mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1615   mq->cont = cont;
1616   mq->cont_cls = cont_cls;
1617   memcpy (&mq[1], msg, msg_size);
1618   mq->message_buf = (const char *) &mq[1];
1619   mq->message_buf_size = msg_size;
1620   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1621   GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1622   if ( (NULL != n->is_active) ||
1623        ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1624     return;
1625   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1626     GNUNET_SCHEDULER_cancel (n->task);
1627   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1628 }
1629
1630 static void
1631 send_session_connect_cont (void *cls,
1632                       const struct GNUNET_PeerIdentity *target,
1633                       int result,
1634                       size_t size_payload,
1635                       size_t size_on_wire)
1636 {
1637   struct NeighbourMapEntry *n;
1638
1639   n = lookup_neighbour (target);
1640   if (NULL == n)
1641   {
1642     /* CONNECT continuation was called after neighbor was freed,
1643      * for example due to a time out for the state or the session
1644      * used was already terminated: nothing to do here... */
1645     return;
1646   }
1647
1648   if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
1649   {
1650     /* CONNECT continuation was called after neighbor changed state,
1651      * for example due to a time out for the state or the session
1652      * used was already terminated: nothing to do here... */
1653     return;
1654   }
1655   if (GNUNET_OK == result)
1656     return;
1657
1658   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1659             _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1660             GNUNET_i2s (target),
1661             GST_plugins_a2s (n->primary_address.address),
1662             n->primary_address.session);
1663
1664   /* Failed to send CONNECT message with this address */
1665   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1666       n->primary_address.session);
1667   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1668       NULL);
1669
1670   /* Remove address and request and additional one */
1671   unset_primary_address (n);
1672
1673   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1674       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1675   return;
1676
1677 }
1678
1679 /**
1680  * Send a SESSION_CONNECT message via the given address.
1681  *
1682  * @param na address to use
1683  */
1684 static void
1685 send_session_connect (struct NeighbourAddress *na)
1686 {
1687   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1688   struct SessionConnectMessage connect_msg;
1689   struct NeighbourMapEntry *n;
1690
1691   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692               "Sending SESSION_CONNECT message to peer %s\n",
1693               GNUNET_i2s (&na->address->peer));
1694   if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1695   {
1696     GNUNET_break (0);
1697     return;
1698   }
1699   if (NULL == na->session)
1700     na->session = papi->get_session (papi->cls, na->address);
1701   if (NULL == na->session)
1702   {
1703     GNUNET_break (0);
1704     return;
1705   }
1706   GNUNET_STATISTICS_update (GST_stats,
1707                             gettext_noop
1708                             ("# SESSION_CONNECT messages sent"),
1709                             1, GNUNET_NO);
1710   na->connect_timestamp = GNUNET_TIME_absolute_get ();
1711   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1712   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1713   connect_msg.reserved = htonl (0);
1714   connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1715   if (-1 ==
1716       papi->send (papi->cls,
1717                   na->session,
1718                   (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1719                   UINT_MAX,
1720                   SETUP_CONNECTION_TIMEOUT,
1721                   send_session_connect_cont, NULL))
1722   {
1723     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1724                 _("Failed to transmit CONNECT message via plugin to %s\n"),
1725                 GST_plugins_a2s (na->address));
1726
1727     n = lookup_neighbour (&na->address->peer);
1728     if (NULL == n)
1729     {
1730       GNUNET_break (0);
1731       return;
1732     }
1733     /* Hard failure to send the CONNECT message with this address:
1734        Destroy address and session */
1735     GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1736     GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1737
1738     /* Remove address and request and additional one */
1739     unset_primary_address (n);
1740     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1741         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1742     return;
1743   }
1744
1745   GST_neighbours_notify_data_sent (&na->address->peer,
1746                                    na->address,
1747                                    na->session,
1748                                    sizeof (struct SessionConnectMessage));
1749
1750 }
1751
1752
1753 /**
1754  * Send a SESSION_CONNECT_ACK message via the given address.
1755  *
1756  * @param address address to use
1757  * @param session session to use
1758  * @param timestamp timestamp to use for the ACK message
1759  */
1760 static void
1761 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1762                                   struct Session *session,
1763                                   struct GNUNET_TIME_Absolute timestamp)
1764 {
1765   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1766   struct SessionConnectMessage connect_msg;
1767
1768   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769               "Sending CONNECT_ACK to peer `%s'\n",
1770               GNUNET_i2s (&address->peer));
1771   if (NULL == (papi = GST_plugins_find (address->transport_name)))
1772   {
1773     GNUNET_break (0);
1774     return;
1775   }
1776   if (NULL == session)
1777     session = papi->get_session (papi->cls, address);
1778   if (NULL == session)
1779   {
1780     GNUNET_break (0);
1781     return;
1782   }
1783   GNUNET_STATISTICS_update (GST_stats,
1784                             gettext_noop
1785                             ("# CONNECT_ACK messages sent"),
1786                             1, GNUNET_NO);
1787   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1788   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1789   connect_msg.reserved = htonl (0);
1790   connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1791   (void) papi->send (papi->cls,
1792                      session,
1793                      (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1794                      UINT_MAX,
1795                      GNUNET_TIME_UNIT_FOREVER_REL,
1796                      NULL, NULL);
1797
1798 }
1799
1800 struct QuotaNotificationRequest
1801 {
1802   struct GNUNET_PeerIdentity peer;
1803   struct Session *session;
1804   char *plugin;
1805 };
1806
1807 struct QNR_LookContext
1808 {
1809   struct GNUNET_PeerIdentity peer;
1810   struct Session *session;
1811   const char *plugin;
1812
1813   struct QuotaNotificationRequest *res;
1814 };
1815
1816 static int
1817 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1818 {
1819   struct QNR_LookContext *qnr_ctx = cls;
1820   struct QuotaNotificationRequest *qnr = value;
1821
1822   if ((qnr->session == qnr_ctx->session) &&
1823       (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1824       (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1825   {
1826     qnr_ctx->res = value;
1827     return GNUNET_NO;
1828   }
1829   return GNUNET_YES;
1830 }
1831
1832 void
1833 GST_neighbours_register_quota_notification(void *cls,
1834     const struct GNUNET_PeerIdentity *peer, const char *plugin,
1835     struct Session *session)
1836 {
1837   struct QuotaNotificationRequest *qnr;
1838   struct QNR_LookContext qnr_ctx;
1839
1840   if (NULL == registered_quota_notifications)
1841   {
1842     return; /* init or shutdown */
1843   }
1844
1845   qnr_ctx.peer = (*peer);
1846   qnr_ctx.plugin = plugin;
1847   qnr_ctx.session = session;
1848   qnr_ctx.res = NULL;
1849
1850   GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1851       peer, &find_notification_request, &qnr_ctx);
1852   if (NULL != qnr_ctx.res)
1853   {
1854     GNUNET_break(0);
1855     return;
1856   }
1857
1858   qnr = GNUNET_new (struct QuotaNotificationRequest);
1859   qnr->peer =  (*peer);
1860   qnr->plugin = GNUNET_strdup (plugin);
1861   qnr->session = session;
1862
1863   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864       "Adding notification for peer `%s' plugin `%s' session %p \n",
1865       GNUNET_i2s (peer), plugin, session);
1866
1867   GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1868       qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1869 }
1870
1871
1872 void
1873 GST_neighbours_unregister_quota_notification(void *cls,
1874     const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1875 {
1876   struct QNR_LookContext qnr_ctx;
1877
1878   if (NULL == registered_quota_notifications)
1879   {
1880     return; /* init or shutdown */
1881   }
1882
1883   qnr_ctx.peer = (*peer);
1884   qnr_ctx.plugin = plugin;
1885   qnr_ctx.session = session;
1886   qnr_ctx.res = NULL;
1887
1888   GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1889       &find_notification_request, &qnr_ctx);
1890   if (NULL == qnr_ctx.res)
1891   {
1892     GNUNET_break(0);
1893     return;
1894   }
1895
1896   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897       "Removing notification for peer `%s' plugin `%s' session %p \n",
1898       GNUNET_i2s (peer), plugin, session);
1899
1900   GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1901       qnr_ctx.res);
1902   GNUNET_free (qnr_ctx.res->plugin);
1903   GNUNET_free (qnr_ctx.res);
1904 }
1905
1906 static int
1907 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1908 {
1909   /* struct NeighbourMapEntry *n = cls; */
1910   struct QuotaNotificationRequest *qnr = value;
1911   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1912   struct GNUNET_TIME_Relative delay;
1913   int do_forward;
1914
1915   papi = GST_plugins_find(qnr->plugin);
1916   if (NULL == papi)
1917   {
1918     GNUNET_break (0);
1919     return GNUNET_OK;
1920   }
1921
1922   delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1923   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924       "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1925       delay.rel_value_us / 1000);
1926
1927   if (NULL != papi->update_inbound_delay)
1928     papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1929   return GNUNET_OK;
1930 }
1931
1932 static
1933 int
1934 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1935     void *value)
1936 {
1937   /* struct NeighbourMapEntry *n = cls; */
1938   struct QuotaNotificationRequest *qnr = value;
1939
1940   GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1941       qnr);
1942   GNUNET_free(qnr->plugin);
1943   GNUNET_free(qnr);
1944
1945   return GNUNET_OK;
1946 }
1947
1948 static void
1949 inbound_bw_tracker_update(void *cls)
1950 {
1951   struct NeighbourMapEntry *n = cls;
1952
1953   /* Quota was updated, tell plugins to update the time to receive next */
1954   GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1955       &n->id, &notification_cb, n);
1956 }
1957
1958
1959 /**
1960  * Create a fresh entry in the neighbour map for the given peer
1961  *
1962  * @param peer peer to create an entry for
1963  * @return new neighbour map entry
1964  */
1965 static struct NeighbourMapEntry *
1966 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1967 {
1968   struct NeighbourMapEntry *n;
1969
1970   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971               "Creating new neighbour entry for `%s'\n",
1972               GNUNET_i2s (peer));
1973   n = GNUNET_new (struct NeighbourMapEntry);
1974   n->id = *peer;
1975   n->ack_state = ACK_UNDEFINED;
1976   n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1977   n->last_util_transmission = GNUNET_TIME_absolute_get();
1978   n->util_payload_bytes_recv = 0;
1979   n->util_payload_bytes_sent = 0;
1980   n->util_total_bytes_recv = 0;
1981   n->util_total_bytes_sent = 0;
1982   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
1983                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1984                                  MAX_BANDWIDTH_CARRY_S);
1985   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1986   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1987   GNUNET_assert (GNUNET_OK ==
1988                  GNUNET_CONTAINER_multipeermap_put (neighbours,
1989                                                     &n->id, n,
1990                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1991   return n;
1992 }
1993
1994
1995 /**
1996  * Check if the two given addresses are the same.
1997  * Actually only checks if the sessions are non-NULL
1998  * (which they should be) and then if they are identical;
1999  * the actual addresses don't matter if the session
2000  * pointers match anyway, and we must have session pointers
2001  * at this time.
2002  *
2003  * @param a1 first address to compare
2004  * @param a2 other address to compare
2005  * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
2006  */
2007 static int
2008 address_matches (const struct NeighbourAddress *a1,
2009                  const struct NeighbourAddress *a2)
2010 {
2011   if ( (NULL == a1->session) ||
2012        (NULL == a2->session) )
2013   {
2014     GNUNET_break (0);
2015     return 0;
2016   }
2017   return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
2018 }
2019
2020
2021 /* We received a address suggestion after requesting an address in
2022  * try_connect or after receiving a connect, switch to address
2023  */
2024 static void
2025 address_suggest_cont (void *cls,
2026     const struct GNUNET_PeerIdentity *peer,
2027     const struct GNUNET_HELLO_Address *address, struct Session *session,
2028     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2029     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2030     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2031 {
2032   GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2033       bandwidth_in, bandwidth_out);
2034 }
2035
2036
2037 struct BlacklistCheckSwitchContext
2038 {
2039   struct BlacklistCheckSwitchContext *prev;
2040   struct BlacklistCheckSwitchContext *next;
2041
2042
2043   struct GST_BlacklistCheck *blc;
2044
2045   struct GNUNET_HELLO_Address *address;
2046   struct Session *session;
2047   struct GNUNET_ATS_Information *ats;
2048   uint32_t ats_count;
2049
2050   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2051   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2052 };
2053
2054 /**
2055  * Black list check result for try_connect call
2056  * If connection to the peer is allowed request adddress and
2057  *
2058  * @param cls blc_ctx bl context
2059  * @param peer the peer
2060  * @param result the result
2061  */
2062 static void
2063 try_connect_bl_check_cont (void *cls,
2064     const struct GNUNET_PeerIdentity *peer, int result)
2065 {
2066   struct BlacklistCheckSwitchContext *blc_ctx = cls;
2067   struct NeighbourMapEntry *n;
2068
2069   GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2070   GNUNET_free (blc_ctx);
2071
2072   if (GNUNET_OK != result)
2073   {
2074     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2075         _("Blacklisting disapproved to connect to peer `%s'\n"),
2076         GNUNET_i2s (peer));
2077     return;
2078   }
2079
2080   /* Setup a new neighbour */
2081   n = setup_neighbour (peer);
2082
2083   /* Request address suggestions for this peer */
2084   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2085       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2086   GNUNET_ATS_reset_backoff (GST_ats, peer);
2087   n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2088       &address_suggest_cont, n);
2089 }
2090
2091
2092
2093 /**
2094  * Try to create a connection to the given target (eventually).
2095  *
2096  * @param target peer to try to connect to
2097  */
2098 void
2099 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2100 {
2101   struct NeighbourMapEntry *n;
2102   struct GST_BlacklistCheck *blc;
2103   struct BlacklistCheckSwitchContext *blc_ctx;
2104
2105   if (NULL == neighbours)
2106   {
2107     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108                 "Asked to connect to peer `%s' during shutdown\n",
2109                 GNUNET_i2s (target));
2110     return; /* during shutdown, do nothing */
2111   }
2112   n = lookup_neighbour (target);
2113   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2114               "Asked to connect to peer `%s' (state: %s)\n",
2115               GNUNET_i2s (target),
2116               (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2117   if (NULL != n)
2118   {
2119     switch (n->state)
2120     {
2121     case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2122       /* this should not be possible */
2123       GNUNET_break (0);
2124       free_neighbour (n, GNUNET_NO);
2125       break;
2126     case GNUNET_TRANSPORT_PS_INIT_ATS:
2127     case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2128     case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2129     case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2130     case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2131     case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2132       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2133                   "Ignoring request to try to connect to `%s', already trying!\n",
2134                   GNUNET_i2s (target));
2135       return; /* already trying */
2136     case GNUNET_TRANSPORT_PS_CONNECTED:
2137     case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2138     case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2139     case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2140     case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2141     case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2142       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2143                   "Ignoring request to try to connect, already connected to `%s'!\n",
2144                   GNUNET_i2s (target));
2145       return; /* already connected */
2146     case GNUNET_TRANSPORT_PS_DISCONNECT:
2147       /* get rid of remains, ready to re-try immediately */
2148       free_neighbour (n, GNUNET_NO);
2149       break;
2150     case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2151       /* should not be possible */
2152       GNUNET_assert (0);
2153     default:
2154       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2155                   "Unhandled state `%s'\n",
2156                   GNUNET_TRANSPORT_ps2s (n->state));
2157       GNUNET_break (0);
2158       free_neighbour (n, GNUNET_NO);
2159       break;
2160     }
2161   }
2162
2163   /* Do blacklist check if connecting to this peer is allowed */
2164   blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2165   GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2166
2167   if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2168         &try_connect_bl_check_cont, blc_ctx)))
2169   {
2170     blc_ctx->blc = blc;
2171   }
2172 }
2173
2174
2175 /**
2176  * Function called with the result of a blacklist check.
2177  *
2178  * @param cls closure with the `struct BlackListCheckContext`
2179  * @param peer peer this check affects
2180  * @param result #GNUNET_OK if the address is allowed
2181  */
2182 static void
2183 handle_test_blacklist_cont (void *cls,
2184                             const struct GNUNET_PeerIdentity *peer,
2185                             int result)
2186 {
2187   struct BlackListCheckContext *bcc = cls;
2188   struct NeighbourMapEntry *n;
2189
2190   bcc->bc = NULL;
2191   GNUNET_CONTAINER_DLL_remove (bc_head,
2192                                bc_tail,
2193                                bcc);
2194   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195               "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2196               GNUNET_i2s (peer),
2197               (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2198   if (GNUNET_OK == result)
2199   {
2200     GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2201   }
2202   else
2203   {
2204     /* Blacklist disagreed on connecting to a peer with this address
2205      * Destroy address because we are not allowed to use it
2206      */
2207     if (NULL != bcc->na.session)
2208       GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
2209     GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
2210   }
2211   if (NULL == (n = lookup_neighbour (peer)))
2212   {
2213     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214                 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2215                 GNUNET_i2s (peer));
2216     goto cleanup; /* nobody left to care about new address */
2217   }
2218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219               "Received blacklist result for peer `%s' in state %s/%s\n",
2220               GNUNET_i2s (peer),
2221               GNUNET_TRANSPORT_ps2s (n->state),
2222               print_ack_state (n->ack_state));
2223   switch (n->state)
2224   {
2225   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2226     /* this should not be possible */
2227     GNUNET_break (0);
2228     free_neighbour (n, GNUNET_NO);
2229     break;
2230   case GNUNET_TRANSPORT_PS_INIT_ATS:
2231     /* waiting on ATS suggestion; still, pass address to ATS as a
2232        possibility */
2233     break;
2234   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2235     /* waiting on CONNECT_ACK, send ACK if one is pending */
2236     if ( (GNUNET_OK == result) &&
2237          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2238     {
2239       n->ack_state = ACK_SEND_SESSION_ACK;
2240       send_session_connect_ack_message (n->primary_address.address,
2241                                         n->primary_address.session,
2242                                         n->connect_ack_timestamp);
2243     }
2244     break;
2245   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2246     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2247         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2248     GNUNET_ATS_reset_backoff (GST_ats, peer);
2249     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250                 "Suggesting address for peer %s to ATS\n",
2251                 GNUNET_i2s (peer));
2252     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2253         &address_suggest_cont, n);
2254     break;
2255   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2256     /* waiting on ATS suggestion, don't care about blacklist */
2257     break;
2258   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2259     if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2260     {
2261       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262                   "Blacklist result ignored, as it is not for our primary address\n");
2263       break; /* result for an address we currently don't care about */
2264     }
2265     if (GNUNET_OK == result)
2266     {
2267       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2268           GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2269       send_session_connect_ack_message (bcc->na.address,
2270                                         bcc->na.session,
2271                                         n->connect_ack_timestamp);
2272       if (ACK_SEND_CONNECT_ACK == n->ack_state)
2273         n->ack_state = ACK_SEND_SESSION_ACK;
2274     }
2275     else
2276     {
2277       struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2278
2279       plugin = GST_plugins_find (bcc->na.address->transport_name);
2280       if ( (NULL != plugin) &&
2281            (NULL != bcc->na.session) )
2282       {
2283         plugin->disconnect_session (plugin->cls,
2284                                     bcc->na.session);
2285         break;
2286       }
2287       GNUNET_break (NULL != plugin);
2288       free_address (&n->primary_address);
2289       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2290           GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2291       GNUNET_ATS_reset_backoff (GST_ats, peer);
2292     }
2293     break;
2294   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2295     /* waiting on SESSION_ACK, send ACK if one is pending */
2296     if ( (GNUNET_OK == result) &&
2297          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2298     {
2299       n->ack_state = ACK_SEND_SESSION_ACK;
2300       send_session_connect_ack_message (n->primary_address.address,
2301                                         n->primary_address.session,
2302                                         n->connect_ack_timestamp);
2303     }
2304     break;
2305   case GNUNET_TRANSPORT_PS_CONNECTED:
2306     /* already connected, don't care about blacklist */
2307     break;
2308   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2309     /* still waiting on ATS suggestion, don't care about blacklist */
2310     break;
2311   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2312     if ( (GNUNET_OK == result) &&
2313          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2314     {
2315       n->ack_state = ACK_SEND_SESSION_ACK;
2316       send_session_connect_ack_message (bcc->na.address,
2317                                         bcc->na.session,
2318                                         n->connect_ack_timestamp);
2319     }
2320     if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2321     {
2322       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323                   "Blacklist result ignored, as it is not for our primary address\n");
2324       break; /* result for an address we currently don't care about */
2325     }
2326     if (GNUNET_OK == result)
2327     {
2328       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2329           GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2330       send_session_connect (&n->primary_address);
2331     }
2332     else
2333     {
2334       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
2335           GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2336     }
2337     break;
2338   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2339     /* waiting on CONNECT_ACK, don't care about blacklist */
2340     if ( (GNUNET_OK == result) &&
2341          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2342     {
2343       n->ack_state = ACK_SEND_SESSION_ACK;
2344       send_session_connect_ack_message (n->primary_address.address,
2345                                         n->primary_address.session,
2346                                         n->connect_ack_timestamp);
2347     }
2348     break;
2349   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2350     if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2351     {
2352       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353                   "Blacklist result ignored, as it is not for our primary address\n");
2354       break; /* result for an address we currently don't care about */
2355     }
2356     if (GNUNET_OK == result)
2357     {
2358       send_session_connect (&n->alternative_address);
2359       set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2360     }
2361     else
2362     {
2363       set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2364       free_address (&n->alternative_address);
2365     }
2366     break;
2367   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2368     /* waiting on CONNECT_ACK, don't care about blacklist */
2369     if ( (GNUNET_OK == result) &&
2370          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2371     {
2372       n->ack_state = ACK_SEND_SESSION_ACK;
2373       send_session_connect_ack_message (n->primary_address.address,
2374                                         n->primary_address.session,
2375                                         n->connect_ack_timestamp);
2376     }
2377     break;
2378   case GNUNET_TRANSPORT_PS_DISCONNECT:
2379     /* Nothing to do here, ATS will already do what can be done */
2380     break;
2381   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2382     /* should not be possible */
2383     GNUNET_assert (0);
2384     break;
2385   default:
2386     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2387                 "Unhandled state `%s'\n",
2388                 GNUNET_TRANSPORT_ps2s (n->state));
2389     GNUNET_break (0);
2390     free_neighbour (n, GNUNET_NO);
2391     break;
2392   }
2393  cleanup:
2394   GNUNET_HELLO_address_free (bcc->na.address);
2395   GNUNET_free (bcc);
2396 }
2397
2398
2399 /**
2400  * We want to know if connecting to a particular peer via
2401  * a particular address is allowed.  Check it!
2402  *
2403  * @param peer identity of the peer to switch the address for
2404  * @param ts time at which the check was initiated
2405  * @param address address of the other peer, NULL if other peer
2406  *                       connected to us
2407  * @param session session to use (or NULL)
2408  */
2409 static void
2410 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2411                  struct GNUNET_TIME_Absolute ts,
2412                  const struct GNUNET_HELLO_Address *address,
2413                  struct Session *session)
2414 {
2415   struct BlackListCheckContext *bcc;
2416   struct GST_BlacklistCheck *bc;
2417
2418   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2419               "Checking peer `%s' against blacklist\n",
2420               GNUNET_i2s (peer));
2421   bcc = GNUNET_new (struct BlackListCheckContext);
2422   bcc->na.address = GNUNET_HELLO_address_copy (address);
2423   bcc->na.session = session;
2424   bcc->na.connect_timestamp = ts;
2425   GNUNET_CONTAINER_DLL_insert (bc_head,
2426                                bc_tail,
2427                                bcc);
2428   if (NULL != (bc = GST_blacklist_test_allowed (peer,
2429                                                 address->transport_name,
2430                                                 &handle_test_blacklist_cont, bcc)))
2431     bcc->bc = bc;
2432   /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2433      we must only store 'bc' if 'bc' is non-NULL... */
2434 }
2435
2436
2437 /**
2438  * We received a 'SESSION_CONNECT' message from the other peer.
2439  * Consider switching to it.
2440  *
2441  * @param message possibly a 'struct SessionConnectMessage' (check format)
2442  * @param peer identity of the peer to switch the address for
2443  * @param address address of the other peer, NULL if other peer
2444  *                       connected to us
2445  * @param session session to use (or NULL)
2446  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2447  */
2448 int
2449 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2450                                const struct GNUNET_PeerIdentity *peer,
2451                                const struct GNUNET_HELLO_Address *address,
2452                                struct Session *session)
2453 {
2454   const struct SessionConnectMessage *scm;
2455   struct NeighbourMapEntry *n;
2456   struct GNUNET_TIME_Absolute ts;
2457
2458   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2459               "Received CONNECT message from peer `%s' with `%s' %p\n",
2460               GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2461   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2462   {
2463     GNUNET_break_op (0);
2464     return GNUNET_SYSERR;
2465   }
2466   GNUNET_STATISTICS_update (GST_stats,
2467                             gettext_noop
2468                             ("# CONNECT messages received"),
2469                             1, GNUNET_NO);
2470   if (NULL == neighbours)
2471   {
2472     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2473                 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2474                 GNUNET_i2s (peer));
2475     return GNUNET_OK; /* we're shutting down */
2476   }
2477   scm = (const struct SessionConnectMessage *) message;
2478   GNUNET_break_op (0 == ntohl (scm->reserved));
2479   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2480   n = lookup_neighbour (peer);
2481   if (NULL == n)
2482     n = setup_neighbour (peer);
2483
2484   /* Remember this CONNECT message in neighbour */
2485   n->ack_state = ACK_SEND_CONNECT_ACK;
2486   n->connect_ack_timestamp = ts;
2487
2488   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489               "Received SESSION_CONNECT for peer `%s' in state %s/%s\n",
2490               GNUNET_i2s (peer),
2491               GNUNET_TRANSPORT_ps2s (n->state),
2492               print_ack_state (n->ack_state));
2493   switch (n->state)
2494   {
2495   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2496     /* Do a blacklist check for the new address */
2497     set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2498     check_blacklist (peer, ts, address, session);
2499     break;
2500   case GNUNET_TRANSPORT_PS_INIT_ATS:
2501     /* CONNECT message takes priority over us asking ATS for address */
2502     set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2503     /* fallthrough */
2504   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2505   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2506   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2507   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2508   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2509     /* It can never hurt to have an alternative address in the above cases,
2510        see if it is allowed */
2511     check_blacklist (peer, ts, address, session);
2512     break;
2513   case GNUNET_TRANSPORT_PS_CONNECTED:
2514     /* we are already connected and can thus send the ACK immediately;
2515        still, it can never hurt to have an alternative address, so also
2516        tell ATS  about it */
2517     GNUNET_assert (NULL != n->primary_address.address);
2518     GNUNET_assert (NULL != n->primary_address.session);
2519     n->ack_state = ACK_UNDEFINED;
2520     send_session_connect_ack_message (n->primary_address.address,
2521                                       n->primary_address.session, ts);
2522     check_blacklist (peer, ts, address, session);
2523     break;
2524   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2525   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2526   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2527     /* It can never hurt to have an alternative address in the above cases,
2528        see if it is allowed */
2529     check_blacklist (peer, ts, address, session);
2530     break;
2531   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2532   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2533     /* we are already connected and can thus send the ACK immediately;
2534        still, it can never hurt to have an alternative address, so also
2535        tell ATS  about it */
2536     GNUNET_assert (NULL != n->primary_address.address);
2537     GNUNET_assert (NULL != n->primary_address.session);
2538     n->ack_state = ACK_UNDEFINED;
2539     send_session_connect_ack_message (n->primary_address.address,
2540                                       n->primary_address.session, ts);
2541     check_blacklist (peer, ts, address, session);
2542     break;
2543   case GNUNET_TRANSPORT_PS_DISCONNECT:
2544     /* get rid of remains without terminating sessions, ready to re-try */
2545     free_neighbour (n, GNUNET_YES);
2546     n = setup_neighbour (peer);
2547     /* Remember the CONNECT timestamp for ACK message */
2548     n->ack_state = ACK_SEND_CONNECT_ACK;
2549     n->connect_ack_timestamp = ts;
2550     /* Request an address for the peer */
2551     GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2552     GNUNET_ATS_reset_backoff (GST_ats, peer);
2553     set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2554     break;
2555   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2556     /* should not be possible */
2557     GNUNET_assert (0);
2558     break;
2559   default:
2560     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2561                 "Unhandled state `%s'\n",
2562                 GNUNET_TRANSPORT_ps2s (n->state));
2563     GNUNET_break (0);
2564     return GNUNET_SYSERR;
2565   }
2566   return GNUNET_OK;
2567 }
2568
2569 static void
2570 switch_address_bl_check_cont (void *cls,
2571     const struct GNUNET_PeerIdentity *peer, int result)
2572 {
2573   struct BlacklistCheckSwitchContext *blc_ctx = cls;
2574   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2575   struct NeighbourMapEntry *n;
2576
2577   if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2578        (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2579   {
2580     if (NULL == n)
2581     {
2582       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583                   "Peer %s is unknown, suggestion ignored\n",
2584                   GNUNET_i2s (peer));
2585     }
2586     if (result == GNUNET_NO)
2587     {
2588       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589           "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2590           GST_plugins_a2s (blc_ctx->address),
2591           blc_ctx->session,
2592           GNUNET_i2s (&blc_ctx->address->peer));
2593     }
2594     if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2595     {
2596       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597           "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2598           blc_ctx->address->transport_name,
2599           GST_plugins_a2s (blc_ctx->address),
2600           blc_ctx->session,
2601           GNUNET_i2s (&blc_ctx->address->peer));
2602     }
2603
2604     /* This address is blacklisted, delete address and session (if existing) in ATS */
2605     GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2606
2607     if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2608           GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2609     {
2610       /* This is an inbound address, destroy full  address */
2611       GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2612     }
2613
2614     /* Remove blacklist check and clean up */
2615     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2616     GNUNET_HELLO_address_free (blc_ctx->address);
2617     GNUNET_free_non_null (blc_ctx->ats);
2618     GNUNET_free (blc_ctx);
2619     return;
2620   }
2621
2622   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2623       "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2624       GST_plugins_a2s (blc_ctx->address),
2625       blc_ctx->session,
2626       GNUNET_i2s (&blc_ctx->address->peer));
2627
2628   if (NULL == blc_ctx->session)
2629   {
2630     blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2631     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632                 "Obtained new session for peer `%s' and  address '%s': %p\n",
2633                 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2634   }
2635   if (NULL == blc_ctx->session)
2636   {
2637     /* No session could be obtained, remove blacklist check and clean up */
2638     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639                 "Failed to obtain new session for peer `%s' and  address '%s'\n",
2640                 GNUNET_i2s (&blc_ctx->address->peer),
2641                 GST_plugins_a2s (blc_ctx->address));
2642     /* Delete address in ATS */
2643     GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2644
2645     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2646     GNUNET_HELLO_address_free (blc_ctx->address);
2647     GNUNET_free_non_null (blc_ctx->ats);
2648     GNUNET_free (blc_ctx);
2649     return;
2650   }
2651
2652   switch (n->state)
2653   {
2654   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2655     GNUNET_break (0);
2656     free_neighbour (n, GNUNET_NO);
2657     return;
2658   case GNUNET_TRANSPORT_PS_INIT_ATS:
2659     /* We requested an address and ATS suggests one:
2660      * set primary address and send CONNECT message*/
2661     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2662         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2663     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2664         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2665     send_session_connect (&n->primary_address);
2666     break;
2667   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2668     /* waiting on CONNECT_ACK, send ACK if one is pending */
2669     if (ACK_SEND_CONNECT_ACK == n->ack_state)
2670     {
2671       n->ack_state = ACK_SEND_SESSION_ACK;
2672       send_session_connect_ack_message (n->primary_address.address,
2673                                         n->primary_address.session,
2674                                         n->connect_ack_timestamp);
2675     }
2676     /* ATS suggests a different address, switch again */
2677     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2678         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2679     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2680         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2681     send_session_connect (&n->primary_address);
2682     break;
2683   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2684     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2685         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2686     /* Send an ACK message as a response to the CONNECT msg */
2687     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2688         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2689     send_session_connect_ack_message (n->primary_address.address,
2690                                       n->primary_address.session,
2691                                       n->connect_ack_timestamp);
2692     if (ACK_SEND_CONNECT_ACK == n->ack_state)
2693       n->ack_state = ACK_SEND_SESSION_ACK;
2694
2695     break;
2696   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2697     set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2698     check_blacklist (&n->id, n->connect_ack_timestamp,
2699         blc_ctx->address, blc_ctx->session);
2700     break;
2701   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2702   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2703     /* ATS asks us to switch while we were trying to connect; switch to new
2704        address and check blacklist again */
2705     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2706         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2707     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2708         GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2709     check_blacklist (&n->id, n->connect_ack_timestamp,
2710         blc_ctx->address, blc_ctx->session);
2711     break;
2712   case GNUNET_TRANSPORT_PS_CONNECTED:
2713     GNUNET_assert (NULL != n->primary_address.address);
2714     GNUNET_assert (NULL != n->primary_address.session);
2715     if (n->primary_address.session == blc_ctx->session)
2716     {
2717       /* not an address change, just a quota change */
2718       set_primary_address (n, blc_ctx->address, blc_ctx->session,
2719           blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2720       break;
2721     }
2722     /* ATS asks us to switch a life connection; see if we can get
2723        a CONNECT_ACK on it before we actually do this! */
2724     set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2725     set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2726         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2727     check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2728         blc_ctx->address, blc_ctx->session);
2729     break;
2730   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2731     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2732         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2733     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2734         GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2735     check_blacklist (&n->id, n->connect_ack_timestamp,
2736         blc_ctx->address, blc_ctx->session);
2737     break;
2738   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2739     /* ATS asks us to switch while we were trying to reconnect; switch to new
2740        address and check blacklist again */
2741     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2742         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2743     set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2744     check_blacklist (&n->id, n->connect_ack_timestamp,
2745         blc_ctx->address, blc_ctx->session);
2746     break;
2747   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2748     /* ATS asks us to switch while we were trying to reconnect; switch to new
2749        address and check blacklist again */
2750     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2751         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2752     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2753         GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2754     check_blacklist (&n->id, n->connect_ack_timestamp,
2755         blc_ctx->address, blc_ctx->session);
2756     break;
2757   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2758     if (n->primary_address.session == blc_ctx->session)
2759     {
2760       /* ATS switches back to still-active session */
2761       set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2762       free_address (&n->alternative_address);
2763       break;
2764     }
2765     /* ATS asks us to switch a life connection, update blacklist check */
2766     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2767         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2768     check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2769         blc_ctx->address, blc_ctx->session);
2770     break;
2771   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2772     if (n->primary_address.session == blc_ctx->session)
2773     {
2774       /* ATS switches back to still-active session */
2775       free_address (&n->alternative_address);
2776       set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2777       break;
2778     }
2779     /* ATS asks us to switch a life connection, update blacklist check */
2780     set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2781     set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2782         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2783     check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2784         blc_ctx->address, blc_ctx->session);
2785     break;
2786   case GNUNET_TRANSPORT_PS_DISCONNECT:
2787     /* not going to switch addresses while disconnecting */
2788     return;
2789   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2790     GNUNET_assert (0);
2791     break;
2792   default:
2793     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2794                 "Unhandled state `%s'\n",
2795                 GNUNET_TRANSPORT_ps2s (n->state));
2796     GNUNET_break (0);
2797     break;
2798   }
2799
2800   GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2801   GNUNET_HELLO_address_free(blc_ctx->address);
2802   GNUNET_free_non_null (blc_ctx->ats);
2803   GNUNET_free (blc_ctx);
2804   return;
2805 }
2806
2807
2808 /**
2809  * For the given peer, switch to this address.
2810  *
2811  * Before accepting this addresses and actively using it, a blacklist check
2812  * is performed. If this blacklist check fails the address will be destroyed.
2813  *
2814  * @param peer identity of the peer to switch the address for
2815  * @param address address of the other peer,
2816  * @param session session to use or NULL if transport should initiate a session
2817  * @param ats performance data
2818  * @param ats_count number of entries in ats
2819  * @param bandwidth_in inbound quota to be used when connection is up,
2820  *      0 to disconnect from peer
2821  * @param bandwidth_out outbound quota to be used when connection is up,
2822  *      0 to disconnect from peer
2823  */
2824 void
2825 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2826                                   const struct GNUNET_HELLO_Address *address,
2827                                   struct Session *session,
2828                                   const struct GNUNET_ATS_Information *ats,
2829                                   uint32_t ats_count,
2830                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2831                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2832 {
2833   struct NeighbourMapEntry *n;
2834   struct GST_BlacklistCheck *blc;
2835   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2836   struct BlacklistCheckSwitchContext *blc_ctx;
2837   int c;
2838
2839   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840               "ATS has decided on an address for peer %s\n",
2841               GNUNET_i2s (peer));
2842   GNUNET_assert (NULL != address->transport_name);
2843   if (NULL == (n = lookup_neighbour (peer)))
2844   {
2845     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846                 "Peer %s is unknown, suggestion ignored\n",
2847                 GNUNET_i2s (peer));
2848     return;
2849   }
2850
2851   /* Check if plugin is available */
2852   if (NULL == (papi = GST_plugins_find (address->transport_name)))
2853   {
2854     /* we don't have the plugin for this address */
2855     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856                 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2857                 address->transport_name,
2858                 GNUNET_i2s (peer));
2859     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2860     return;
2861   }
2862   if ((NULL == session) &&
2863       (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2864   {
2865     /* This is a inbound address and we do not have a session to use! */
2866     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867                 "Inbound address without session `%s'! Destroying address...\n",
2868                 GST_plugins_a2s (address));
2869     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2870     return;
2871   }
2872
2873   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2874     "ATS tells us to switch to %s address '%s' session %p for "
2875     "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2876     GNUNET_HELLO_address_check_option (address,
2877         GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2878     GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2879     GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2880     ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2881
2882   /* Perform blacklist check */
2883   blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2884   blc_ctx->address = GNUNET_HELLO_address_copy (address);
2885   blc_ctx->session = session;
2886   blc_ctx->bandwidth_in = bandwidth_in;
2887   blc_ctx->bandwidth_out = bandwidth_out;
2888   blc_ctx->ats_count = ats_count;
2889   blc_ctx->ats = NULL;
2890   if (ats_count > 0)
2891   {
2892     blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2893     for (c = 0; c < ats_count; c++)
2894     {
2895       blc_ctx->ats[c].type = ats[c].type;
2896       blc_ctx->ats[c].value = ats[c].value;
2897     }
2898   }
2899
2900   GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2901   if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2902       &switch_address_bl_check_cont, blc_ctx)))
2903   {
2904     blc_ctx->blc = blc;
2905   }
2906 }
2907
2908
2909 static int
2910 send_utilization_data (void *cls,
2911                        const struct GNUNET_PeerIdentity *key,
2912                        void *value)
2913 {
2914   struct NeighbourMapEntry *n = value;
2915   struct GNUNET_ATS_Information atsi[4];
2916   uint32_t bps_pl_in;
2917   uint32_t bps_pl_out;
2918   uint32_t bps_in;
2919   uint32_t bps_out;
2920   struct GNUNET_TIME_Relative delta;
2921
2922   delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2923                                                GNUNET_TIME_absolute_get ());
2924
2925   bps_pl_in = 0;
2926
2927   if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2928     bps_pl_in =  (1000LL * 1000LL *  n->util_payload_bytes_recv) / (delta.rel_value_us);
2929   bps_pl_out = 0;
2930   if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2931     bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2932   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2933               "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2934               GNUNET_i2s (key),
2935               bps_pl_in,
2936               bps_pl_out);
2937   bps_in = 0;
2938   if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2939     bps_in =  (1000LL * 1000LL *  n->util_total_bytes_recv) / (delta.rel_value_us);
2940   bps_out = 0;
2941   if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2942     bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2943
2944
2945   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2946               "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2947               GNUNET_i2s (key),
2948               bps_in,
2949               bps_out);
2950   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2951   atsi[0].value = htonl (bps_out);
2952   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2953   atsi[1].value = htonl (bps_in);
2954
2955   atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2956   atsi[2].value = htonl (bps_pl_out);
2957   atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2958   atsi[3].value = htonl (bps_pl_in);
2959
2960   GST_ats_update_metrics (key, n->primary_address.address,
2961       n->primary_address.session, atsi, 4);
2962   n->util_payload_bytes_recv = 0;
2963   n->util_payload_bytes_sent = 0;
2964   n->util_total_bytes_recv = 0;
2965   n->util_total_bytes_sent = 0;
2966   n->last_util_transmission = GNUNET_TIME_absolute_get();
2967   return GNUNET_OK;
2968 }
2969
2970
2971 /**
2972  * Task transmitting utilization in a regular interval
2973  *
2974  * @param cls the 'struct NeighbourMapEntry' for which we are running
2975  * @param tc scheduler context (unused)
2976  */
2977 static void
2978 utilization_transmission (void *cls,
2979                           const struct GNUNET_SCHEDULER_TaskContext *tc)
2980 {
2981   util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2982
2983   if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2984     GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2985
2986   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2987       utilization_transmission, NULL);
2988
2989 }
2990
2991
2992 void
2993 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2994                                  const struct GNUNET_HELLO_Address *address,
2995                                  struct Session *session,
2996                                  const struct GNUNET_MessageHeader *message)
2997 {
2998   struct NeighbourMapEntry *n;
2999
3000   n = lookup_neighbour (peer);
3001   if (NULL == n)
3002     return;
3003   n->util_total_bytes_recv += ntohs(message->size);
3004 }
3005
3006
3007 void
3008 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3009                                     const struct GNUNET_HELLO_Address *address,
3010                                     struct Session *session,
3011                                     const struct GNUNET_MessageHeader *message)
3012 {
3013   struct NeighbourMapEntry *n;
3014   n = lookup_neighbour (peer);
3015   if (NULL == n)
3016     return;
3017   n->util_payload_bytes_recv += ntohs(message->size);
3018 }
3019
3020
3021 void
3022 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3023                                  const struct GNUNET_HELLO_Address *address,
3024                                  struct Session *session,
3025                                  size_t size)
3026 {
3027   struct NeighbourMapEntry *n;
3028   n = lookup_neighbour (peer);
3029   if (NULL == n)
3030       return;
3031   if (n->primary_address.session != session)
3032     return;
3033   n->util_total_bytes_sent += size;
3034 }
3035
3036
3037 void
3038 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3039                                     size_t size)
3040 {
3041   struct NeighbourMapEntry *n;
3042   n = lookup_neighbour (peer);
3043   if (NULL == n)
3044     return;
3045   n->util_payload_bytes_sent += size;
3046 }
3047
3048
3049 /**
3050  * Master task run for every neighbour.  Performs all of the time-related
3051  * activities (keep alive, send next message, disconnect if idle, finish
3052  * clean up after disconnect).
3053  *
3054  * @param cls the 'struct NeighbourMapEntry' for which we are running
3055  * @param tc scheduler context (unused)
3056  */
3057 static void
3058 master_task (void *cls,
3059              const struct GNUNET_SCHEDULER_TaskContext *tc)
3060 {
3061   struct NeighbourMapEntry *n = cls;
3062   struct GNUNET_TIME_Relative delay;
3063
3064   n->task = GNUNET_SCHEDULER_NO_TASK;
3065   delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3066   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3067               "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3068               GNUNET_i2s (&n->id),
3069               GNUNET_TRANSPORT_ps2s(n->state),
3070               GNUNET_STRINGS_relative_time_to_string (delay,
3071                                                       GNUNET_YES));
3072   switch (n->state)
3073   {
3074   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3075     /* invalid state for master task, clean up */
3076     GNUNET_break (0);
3077     free_neighbour (n, GNUNET_NO);
3078     return;
3079   case GNUNET_TRANSPORT_PS_INIT_ATS:
3080     if (0 == delay.rel_value_us)
3081     {
3082       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3083                   "Connection to `%s' timed out waiting for ATS to provide address\n",
3084                   GNUNET_i2s (&n->id));
3085       free_neighbour (n, GNUNET_NO);
3086       return;
3087     }
3088     break;
3089   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3090     if (0 == delay.rel_value_us)
3091     {
3092       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3093                   "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3094                   GNUNET_i2s (&n->id));
3095       /* We could not send to this address, delete address and session */
3096       if (NULL != n->primary_address.session)
3097         GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3098             n->primary_address.session);
3099       GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3100
3101       /* Remove address and request and additional one */
3102       GNUNET_break (0);
3103       unset_primary_address (n);
3104       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3105           GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3106       return;
3107     }
3108     break;
3109   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3110     if (0 == delay.rel_value_us)
3111     {
3112       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113                   "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3114                   GNUNET_i2s (&n->id));
3115       free_neighbour (n, GNUNET_NO);
3116       return;
3117     }
3118     break;
3119   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3120     if (0 == delay.rel_value_us)
3121     {
3122       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123                   "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3124                   GNUNET_i2s (&n->id));
3125       free_neighbour (n, GNUNET_NO);
3126       return;
3127     }
3128     break;
3129   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3130     if (0 == delay.rel_value_us)
3131     {
3132       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133                   "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3134                   GNUNET_i2s (&n->id));
3135       free_neighbour (n, GNUNET_NO);
3136       return;
3137     }
3138     break;
3139   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3140     if (0 == delay.rel_value_us)
3141     {
3142       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3143                   "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3144                   GNUNET_i2s (&n->id));
3145       disconnect_neighbour (n);
3146       return;
3147     }
3148     break;
3149   case GNUNET_TRANSPORT_PS_CONNECTED:
3150     if (0 == delay.rel_value_us)
3151     {
3152       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3154                   GNUNET_i2s (&n->id));
3155       disconnect_neighbour (n);
3156       return;
3157     }
3158     try_transmission_to_peer (n);
3159     send_keepalive (n);
3160     break;
3161   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3162     if (0 == delay.rel_value_us)
3163     {
3164       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165                   "Connection to `%s' timed out, waiting for ATS replacement address\n",
3166                   GNUNET_i2s (&n->id));
3167       disconnect_neighbour (n);
3168       return;
3169     }
3170     break;
3171   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3172     if (0 == delay.rel_value_us)
3173     {
3174       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175                   "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3176                   GNUNET_i2s (&n->id));
3177       disconnect_neighbour (n);
3178       return;
3179     }
3180     break;
3181   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3182     if (0 == delay.rel_value_us)
3183     {
3184       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185                   "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3186                   GNUNET_i2s (&n->id));
3187       disconnect_neighbour (n);
3188       return;
3189     }
3190     break;
3191   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3192     if (0 == delay.rel_value_us)
3193     {
3194       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3196                   GNUNET_i2s (&n->id));
3197       disconnect_neighbour (n);
3198       return;
3199     }
3200     try_transmission_to_peer (n);
3201     send_keepalive (n);
3202     break;
3203   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3204     if (0 == delay.rel_value_us)
3205     {
3206       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3208                   GNUNET_i2s (&n->id));
3209       disconnect_neighbour (n);
3210       return;
3211     }
3212     try_transmission_to_peer (n);
3213     send_keepalive (n);
3214     break;
3215   case GNUNET_TRANSPORT_PS_DISCONNECT:
3216     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217                 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3218                 GNUNET_i2s (&n->id));
3219     free_neighbour (n, GNUNET_NO);
3220     return;
3221   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3222     /* how did we get here!? */
3223     GNUNET_assert (0);
3224     break;
3225   default:
3226     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3227                 "Unhandled state `%s'\n",
3228                 GNUNET_TRANSPORT_ps2s (n->state));
3229     GNUNET_break (0);
3230     break;
3231   }
3232   if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3233        (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3234        (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3235   {
3236     /* if we are *now* in one of these three states, we're sending
3237        keep alive messages, so we need to consider the keepalive
3238        delay, not just the connection timeout */
3239     delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3240                                       delay);
3241   }
3242   if (GNUNET_SCHEDULER_NO_TASK == n->task)
3243     n->task = GNUNET_SCHEDULER_add_delayed (delay,
3244                                             &master_task,
3245                                             n);
3246 }
3247
3248
3249 /**
3250  * Send a SESSION_ACK message to the neighbour to confirm that we
3251  * got his CONNECT_ACK.
3252  *
3253  * @param n neighbour to send the SESSION_ACK to
3254  */
3255 static void
3256 send_session_ack_message (struct NeighbourMapEntry *n)
3257 {
3258   struct GNUNET_MessageHeader msg;
3259
3260   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3261   msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3262   (void) send_with_session(n,
3263                            (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3264                            UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3265                            NULL, NULL);
3266 }
3267
3268
3269 /**
3270  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3271  * Consider switching to it.
3272  *
3273  * @param message possibly a 'struct SessionConnectMessage' (check format)
3274  * @param peer identity of the peer to switch the address for
3275  * @param address address of the other peer, NULL if other peer
3276  *                       connected to us
3277  * @param session session to use (or NULL)
3278  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3279  */
3280 int
3281 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3282                                    const struct GNUNET_PeerIdentity *peer,
3283                                    const struct GNUNET_HELLO_Address *address,
3284                                    struct Session *session)
3285 {
3286   const struct SessionConnectMessage *scm;
3287   struct GNUNET_TIME_Absolute ts;
3288   struct NeighbourMapEntry *n;
3289
3290   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3291               "Received CONNECT_ACK message from peer `%s'\n",
3292               GNUNET_i2s (peer));
3293
3294   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3295   {
3296     GNUNET_break_op (0);
3297     return GNUNET_SYSERR;
3298   }
3299   GNUNET_STATISTICS_update (GST_stats,
3300                             gettext_noop
3301                             ("# CONNECT_ACK messages received"),
3302                             1, GNUNET_NO);
3303   scm = (const struct SessionConnectMessage *) message;
3304   GNUNET_break_op (ntohl (scm->reserved) == 0);
3305   if (NULL == (n = lookup_neighbour (peer)))
3306   {
3307     GNUNET_STATISTICS_update (GST_stats,
3308                               gettext_noop
3309                               ("# unexpected CONNECT_ACK messages (no peer)"),
3310                               1, GNUNET_NO);
3311     return GNUNET_SYSERR;
3312   }
3313   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3314   switch (n->state)
3315   {
3316   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3317     GNUNET_break (0);
3318     free_neighbour (n, GNUNET_NO);
3319     return GNUNET_SYSERR;
3320   case GNUNET_TRANSPORT_PS_INIT_ATS:
3321     GNUNET_STATISTICS_update (GST_stats,
3322                               gettext_noop
3323                               ("# unexpected CONNECT_ACK messages (not ready)"),
3324                               1, GNUNET_NO);
3325     break;
3326   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3327     if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3328     {
3329       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3330                   "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3331       return GNUNET_OK;
3332     }
3333     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3334         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3335     GNUNET_STATISTICS_set (GST_stats,
3336                            gettext_noop ("# peers connected"),
3337                            ++neighbours_connected,
3338                            GNUNET_NO);
3339     connect_notify_cb (callback_cls, &n->id,
3340                        n->primary_address.bandwidth_in,
3341                        n->primary_address.bandwidth_out);
3342     /* Tell ATS that the outbound session we created to send CONNECT was successful */
3343     GST_ats_add_address (n->primary_address.address,
3344                          n->primary_address.session,
3345                          NULL, 0);
3346     set_primary_address (n,
3347                  n->primary_address.address,
3348                  n->primary_address.session,
3349                  n->primary_address.bandwidth_in,
3350                  n->primary_address.bandwidth_out,
3351                  GNUNET_YES);
3352     send_session_ack_message (n);
3353     break;
3354   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3355   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3356   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3357   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3358     GNUNET_STATISTICS_update (GST_stats,
3359                               gettext_noop
3360                               ("# unexpected CONNECT_ACK messages (not ready)"),
3361                               1, GNUNET_NO);
3362     break;
3363   case GNUNET_TRANSPORT_PS_CONNECTED:
3364     /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3365     send_session_ack_message (n);
3366     break;
3367   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3368   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3369     /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3370        to give us a new address... */
3371     GNUNET_STATISTICS_update (GST_stats,
3372                               gettext_noop
3373                               ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3374                               1, GNUNET_NO);
3375     break;
3376   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3377     /* new address worked; go back to connected! */
3378     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3379         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3380     send_session_ack_message (n);
3381     break;
3382   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3383     /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3384     send_session_ack_message (n);
3385     break;
3386   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3387     /* new address worked; adopt it and go back to connected! */
3388     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3389     GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3390
3391     GST_ats_add_address (n->alternative_address.address,
3392                          n->alternative_address.session,
3393                          NULL, 0);
3394     set_primary_address (n, n->alternative_address.address,
3395         n->alternative_address.session, n->alternative_address.bandwidth_in,
3396         n->alternative_address.bandwidth_out, GNUNET_YES);
3397
3398     free_address (&n->alternative_address);
3399     send_session_ack_message (n);
3400     break;
3401   case GNUNET_TRANSPORT_PS_DISCONNECT:
3402     GNUNET_STATISTICS_update (GST_stats,
3403                               gettext_noop
3404                               ("# unexpected CONNECT_ACK messages (disconnecting)"),
3405                               1, GNUNET_NO);
3406     return GNUNET_SYSERR;
3407   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3408     GNUNET_assert (0);
3409     break;
3410   default:
3411     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3412                 "Unhandled state `%s'\n",
3413                 GNUNET_TRANSPORT_ps2s (n->state));
3414     GNUNET_break (0);
3415     return GNUNET_SYSERR;
3416   }
3417   return GNUNET_OK;
3418 }
3419
3420
3421 /**
3422  * A session was terminated. Take note; if needed, try to get
3423  * an alternative address from ATS.
3424  *
3425  * @param peer identity of the peer where the session died
3426  * @param session session that is gone
3427  * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3428  *        this session was not in use
3429  */
3430 int
3431 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3432                                    struct Session *session)
3433 {
3434   struct NeighbourMapEntry *n;
3435   struct BlackListCheckContext *bcc;
3436   struct BlackListCheckContext *bcc_next;
3437
3438   /* make sure to cancel all ongoing blacklist checks involving 'session' */
3439   bcc_next = bc_head;
3440   while (NULL != (bcc = bcc_next))
3441   {
3442     bcc_next = bcc->next;
3443     if (bcc->na.session == session)
3444     {
3445       if (NULL != bcc->bc)
3446         GST_blacklist_test_cancel (bcc->bc);
3447       GNUNET_HELLO_address_free (bcc->na.address);
3448       GNUNET_CONTAINER_DLL_remove (bc_head,
3449                                    bc_tail,
3450                                    bcc);
3451       GNUNET_free (bcc);
3452     }
3453   }
3454   if (NULL == (n = lookup_neighbour (peer)))
3455     return GNUNET_NO; /* can't affect us */
3456   if (session != n->primary_address.session)
3457   {
3458     if (session == n->alternative_address.session)
3459     {
3460       if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3461            (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3462         set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3463       else
3464         GNUNET_break (0);
3465       free_address (&n->alternative_address);
3466     }
3467     return GNUNET_NO; /* doesn't affect us further */
3468   }
3469
3470   n->expect_latency_response = GNUNET_NO;
3471   /* The session for neighbour's primary address died */
3472   switch (n->state)
3473   {
3474   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3475     GNUNET_break (0);
3476     free_neighbour (n, GNUNET_NO);
3477     return GNUNET_YES;
3478   case GNUNET_TRANSPORT_PS_INIT_ATS:
3479     GNUNET_break (0);
3480     free_neighbour (n, GNUNET_NO);
3481     return GNUNET_YES;
3482   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3483     /* The session used to send the CONNECT terminated:
3484      * this implies a connect error*/
3485     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3486                 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3487                 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3488                 GNUNET_i2s (peer));
3489     GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3490     unset_primary_address (n);
3491     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3492         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3493     break;
3494   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3495   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3496   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3497   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3498     /* error on inbound session; free neighbour entirely */
3499     free_address (&n->primary_address);
3500     free_neighbour (n, GNUNET_NO);
3501     return GNUNET_YES;
3502   case GNUNET_TRANSPORT_PS_CONNECTED:
3503     unset_primary_address (n);
3504     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3505         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3506     break;
3507   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3508     /* we don't have an address, how can it go down? */
3509     GNUNET_break (0);
3510     break;
3511   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3512   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3513     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3514     break;
3515   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3516     /* primary went down while we were checking secondary against
3517        blacklist, adopt secondary as primary */
3518     free_address (&n->primary_address);
3519     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3520     n->primary_address = n->alternative_address;
3521     memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3522     break;
3523   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3524     /* primary went down while we were waiting for CONNECT_ACK on secondary;
3525        secondary as primary */
3526     free_address (&n->primary_address);
3527     n->primary_address = n->alternative_address;
3528     memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3529     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3530     break;
3531   case GNUNET_TRANSPORT_PS_DISCONNECT:
3532     free_address (&n->primary_address);
3533     break;
3534   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3535     /* neighbour was freed and plugins told to terminate session */
3536     return GNUNET_NO;
3537     break;
3538   default:
3539     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3540                 "Unhandled state `%s'\n",
3541                 GNUNET_TRANSPORT_ps2s (n->state));
3542     GNUNET_break (0);
3543     break;
3544   }
3545   if (GNUNET_SCHEDULER_NO_TASK != n->task)
3546     GNUNET_SCHEDULER_cancel (n->task);
3547   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3548   return GNUNET_YES;
3549 }
3550
3551
3552 /**
3553  * We received a 'SESSION_ACK' message from the other peer.
3554  * If we sent a 'CONNECT_ACK' last, this means we are now
3555  * connected.  Otherwise, do nothing.
3556  *
3557  * @param message possibly a 'struct SessionConnectMessage' (check format)
3558  * @param peer identity of the peer to switch the address for
3559  * @param address address of the other peer, NULL if other peer
3560  *                       connected to us
3561  * @param session session to use (or NULL)
3562  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3563  */
3564 int
3565 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3566                                    const struct GNUNET_PeerIdentity *peer,
3567                                    const struct GNUNET_HELLO_Address *address,
3568                                    struct Session *session)
3569 {
3570   struct NeighbourMapEntry *n;
3571
3572   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3573               "Received SESSION_ACK message from peer `%s'\n",
3574               GNUNET_i2s (peer));
3575   if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3576   {
3577     GNUNET_break_op (0);
3578     return GNUNET_SYSERR;
3579   }
3580   GNUNET_STATISTICS_update (GST_stats,
3581                             gettext_noop
3582                             ("# SESSION_ACK messages received"),
3583                             1, GNUNET_NO);
3584   if (NULL == (n = lookup_neighbour (peer)))
3585   {
3586     GNUNET_break_op (0);
3587     return GNUNET_SYSERR;
3588   }
3589   /* check if we are in a plausible state for having sent
3590      a CONNECT_ACK.  If not, return, otherwise break */
3591   if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3592          (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3593        (ACK_SEND_SESSION_ACK != n->ack_state) )
3594   {
3595     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3596                 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3597                 GNUNET_i2s (peer),
3598                 GNUNET_TRANSPORT_ps2s (n->state),
3599                 print_ack_state (n->ack_state));
3600     GNUNET_STATISTICS_update (GST_stats,
3601                               gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3602                               GNUNET_NO);
3603     return GNUNET_OK;
3604   }
3605   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3606   GNUNET_STATISTICS_set (GST_stats,
3607                          gettext_noop ("# peers connected"),
3608                          ++neighbours_connected,
3609                          GNUNET_NO);
3610   connect_notify_cb (callback_cls, &n->id,
3611                      n->primary_address.bandwidth_in,
3612                      n->primary_address.bandwidth_out);
3613
3614   GST_ats_add_address (n->primary_address.address,
3615                        n->primary_address.session,
3616                        NULL, 0);
3617   set_primary_address (n,
3618                n->primary_address.address,
3619                n->primary_address.session,
3620                n->primary_address.bandwidth_in,
3621                n->primary_address.bandwidth_out,
3622                GNUNET_YES);
3623   return GNUNET_OK;
3624 }
3625
3626
3627 /**
3628  * Test if we're connected to the given peer.
3629  *
3630  * @param target peer to test
3631  * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3632  */
3633 int
3634 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3635 {
3636   return test_connected (lookup_neighbour (target));
3637 }
3638
3639 /**
3640  * Change the incoming quota for the given peer.
3641  *
3642  * @param neighbour identity of peer to change qutoa for
3643  * @param quota new quota
3644  */
3645 void
3646 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3647                                    struct GNUNET_BANDWIDTH_Value32NBO quota)
3648 {
3649   struct NeighbourMapEntry *n;
3650
3651   if (NULL == (n = lookup_neighbour (neighbour)))
3652   {
3653     GNUNET_STATISTICS_update (GST_stats,
3654                               gettext_noop
3655                               ("# SET QUOTA messages ignored (no such peer)"),
3656                               1, GNUNET_NO);
3657     return;
3658   }
3659   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3660               "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3661               ntohl (quota.value__), GNUNET_i2s (&n->id));
3662   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3663   if (0 != ntohl (quota.value__))
3664     return;
3665   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3666               "Disconnecting peer `%4s' due to SET_QUOTA\n",
3667               GNUNET_i2s (&n->id));
3668   if (GNUNET_YES == test_connected (n))
3669     GNUNET_STATISTICS_update (GST_stats,
3670                               gettext_noop ("# disconnects due to quota of 0"),
3671                               1, GNUNET_NO);
3672   disconnect_neighbour (n);
3673 }
3674
3675
3676 /**
3677  * We received a disconnect message from the given peer,
3678  * validate and process.
3679  *
3680  * @param peer sender of the message
3681  * @param msg the disconnect message
3682  */
3683 void
3684 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3685                                           const struct GNUNET_MessageHeader *msg)
3686 {
3687   struct NeighbourMapEntry *n;
3688   const struct SessionDisconnectMessage *sdm;
3689
3690   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3691               "Received DISCONNECT message from peer `%s'\n",
3692               GNUNET_i2s (peer));
3693   if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3694   {
3695     // GNUNET_break_op (0);
3696     GNUNET_STATISTICS_update (GST_stats,
3697                               gettext_noop
3698                               ("# disconnect messages ignored (old format)"), 1,
3699                               GNUNET_NO);
3700     return;
3701   }
3702   GNUNET_STATISTICS_update (GST_stats,
3703                             gettext_noop
3704                             ("# DISCONNECT messages received"),
3705                             1, GNUNET_NO);
3706   sdm = (const struct SessionDisconnectMessage *) msg;
3707   if (NULL == (n = lookup_neighbour (peer)))
3708     return;                     /* gone already */
3709   if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3710   {
3711     GNUNET_STATISTICS_update (GST_stats,
3712                               gettext_noop
3713                               ("# disconnect messages ignored (timestamp)"), 1,
3714                               GNUNET_NO);
3715     return;
3716   }
3717   if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3718   {
3719     GNUNET_break_op (0);
3720     return;
3721   }
3722   if (ntohl (sdm->purpose.size) !=
3723       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3724       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3725       sizeof (struct GNUNET_TIME_AbsoluteNBO))
3726   {
3727     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3728                 "%s message from peer `%s' has invalid size \n",
3729                 "DISCONNECT",
3730                 GNUNET_i2s (peer));
3731     GNUNET_break_op (0);
3732     return;
3733   }
3734   if (GNUNET_OK !=
3735       GNUNET_CRYPTO_eddsa_verify
3736       (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3737        &sdm->signature, &sdm->public_key))
3738   {
3739     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3740                 "%s message from peer `%s' cannot be verified \n",
3741                 "DISCONNECT",
3742                 GNUNET_i2s (peer));
3743     GNUNET_break_op (0);
3744     return;
3745   }
3746   if (GNUNET_YES == test_connected (n))
3747     GNUNET_STATISTICS_update (GST_stats,
3748                               gettext_noop
3749                               ("# other peer asked to disconnect from us"), 1,
3750                               GNUNET_NO);
3751   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3752               "Disconnecting by request from peer %s\n",
3753               GNUNET_i2s (peer));
3754   disconnect_neighbour (n);
3755 }
3756
3757
3758 /**
3759  * Closure for the neighbours_iterate function.
3760  */
3761 struct IteratorContext
3762 {
3763   /**
3764    * Function to call on each connected neighbour.
3765    */
3766   GST_NeighbourIterator cb;
3767
3768   /**
3769    * Closure for 'cb'.
3770    */
3771   void *cb_cls;
3772 };
3773
3774
3775 /**
3776  * Call the callback from the closure for each neighbour.
3777  *
3778  * @param cls the `struct IteratorContext`
3779  * @param key the hash of the public key of the neighbour
3780  * @param value the `struct NeighbourMapEntry`
3781  * @return #GNUNET_OK (continue to iterate)
3782  */
3783 static int
3784 neighbours_iterate (void *cls,
3785                     const struct GNUNET_PeerIdentity *key,
3786                     void *value)
3787 {
3788   struct IteratorContext *ic = cls;
3789   struct NeighbourMapEntry *n = value;
3790   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3791   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3792
3793
3794   if (NULL != n->primary_address.address)
3795   {
3796     bandwidth_in = n->primary_address.bandwidth_in;
3797     bandwidth_out = n->primary_address.bandwidth_out;
3798   }
3799   else
3800   {
3801     bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3802     bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3803   }
3804   ic->cb (ic->cb_cls,
3805           &n->id,
3806           n->primary_address.address,
3807           n->state,
3808           n->timeout,
3809           bandwidth_in, bandwidth_out);
3810   return GNUNET_OK;
3811 }
3812
3813
3814 /**
3815  * Iterate over all connected neighbours.
3816  *
3817  * @param cb function to call
3818  * @param cb_cls closure for cb
3819  */
3820 void
3821 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3822 {
3823   struct IteratorContext ic;
3824
3825   if (NULL == neighbours)
3826     return; /* can happen during shutdown */
3827   ic.cb = cb;
3828   ic.cb_cls = cb_cls;
3829   GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3830 }
3831
3832
3833 /**
3834  * If we have an active connection to the given target, it must be shutdown.
3835  *
3836  * @param target peer to disconnect from
3837  */
3838 void
3839 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3840 {
3841   struct NeighbourMapEntry *n;
3842
3843   if (NULL == (n = lookup_neighbour (target)))
3844     return;  /* not active */
3845   if (GNUNET_YES == test_connected (n))
3846     GNUNET_STATISTICS_update (GST_stats,
3847                               gettext_noop
3848                               ("# disconnected from peer upon explicit request"), 1,
3849                               GNUNET_NO);
3850   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3851               "Forced disconnect from peer %s\n",
3852               GNUNET_i2s (target));
3853   disconnect_neighbour (n);
3854 }
3855
3856
3857 /**
3858  * Obtain current latency information for the given neighbour.
3859  *
3860  * @param peer to get the latency for
3861  * @return observed latency of the address, FOREVER if the
3862  *         the connection is not up
3863  */
3864 struct GNUNET_TIME_Relative
3865 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3866 {
3867   struct NeighbourMapEntry *n;
3868
3869   n = lookup_neighbour (peer);
3870   if (NULL == n)
3871     return GNUNET_TIME_UNIT_FOREVER_REL;
3872   switch (n->state)
3873   {
3874   case GNUNET_TRANSPORT_PS_CONNECTED:
3875   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3876   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3877   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3878   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3879   case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3880     return n->latency;
3881   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3882   case GNUNET_TRANSPORT_PS_INIT_ATS:
3883   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3884   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3885   case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3886   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3887   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3888   case GNUNET_TRANSPORT_PS_DISCONNECT:
3889   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3890     return GNUNET_TIME_UNIT_FOREVER_REL;
3891   default:
3892     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3893                 "Unhandled state `%s'\n",
3894                 GNUNET_TRANSPORT_ps2s (n->state));
3895     GNUNET_break (0);
3896     break;
3897   }
3898   return GNUNET_TIME_UNIT_FOREVER_REL;
3899 }
3900
3901
3902 /**
3903  * Obtain current address information for the given neighbour.
3904  *
3905  * @param peer
3906  * @return address currently used
3907  */
3908 struct GNUNET_HELLO_Address *
3909 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3910 {
3911   struct NeighbourMapEntry *n;
3912
3913   n = lookup_neighbour (peer);
3914   if (NULL == n)
3915     return NULL;
3916   return n->primary_address.address;
3917 }
3918
3919
3920 /**
3921  * Initialize the neighbours subsystem.
3922  *
3923  * @param cls closure for callbacks
3924  * @param connect_cb function to call if we connect to a peer
3925  * @param disconnect_cb function to call if we disconnect from a peer
3926  * @param peer_address_cb function to call if we change an active address
3927  *                   of a neighbour
3928  * @param max_fds maximum number of fds to use
3929  */
3930 void
3931 GST_neighbours_start (void *cls,
3932                       NotifyConnect connect_cb,
3933                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3934                       GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3935                       unsigned int max_fds)
3936 {
3937   callback_cls = cls;
3938   connect_notify_cb = connect_cb;
3939   disconnect_notify_cb = disconnect_cb;
3940   neighbour_change_cb = peer_address_cb;
3941   neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3942   registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3943   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3944       utilization_transmission, NULL);
3945 }
3946
3947
3948 /**
3949  * Disconnect from the given neighbour.
3950  *
3951  * @param cls unused
3952  * @param key hash of neighbour's public key (not used)
3953  * @param value the 'struct NeighbourMapEntry' of the neighbour
3954  * @return #GNUNET_OK (continue to iterate)
3955  */
3956 static int
3957 disconnect_all_neighbours (void *cls,
3958                            const struct GNUNET_PeerIdentity *key,
3959                            void *value)
3960 {
3961   struct NeighbourMapEntry *n = value;
3962
3963   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3964               "Disconnecting peer `%4s', %s\n",
3965               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3966   free_neighbour (n, GNUNET_NO);
3967   return GNUNET_OK;
3968 }
3969
3970
3971 /**
3972  * Cleanup the neighbours subsystem.
3973  */
3974 void
3975 GST_neighbours_stop ()
3976 {
3977   struct BlacklistCheckSwitchContext *cur;
3978   struct BlacklistCheckSwitchContext *next;
3979
3980   if (NULL == neighbours)
3981     return;
3982   if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3983   {
3984     GNUNET_SCHEDULER_cancel (util_transmission_tk);
3985     util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3986   }
3987
3988   GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3989                                          &disconnect_all_neighbours,
3990                                          NULL);
3991   GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3992
3993   next = pending_bc_head;
3994   for (cur = next; NULL != cur; cur = next )
3995   {
3996     next = cur->next;
3997     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3998
3999     if (NULL != cur->blc)
4000     {
4001       GST_blacklist_test_cancel (cur->blc);
4002       cur->blc = NULL;
4003     }
4004     if (NULL != cur->address)
4005       GNUNET_HELLO_address_free (cur->address);
4006     GNUNET_free_non_null (cur->ats);
4007     GNUNET_free (cur);
4008   }
4009
4010   GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4011       &free_notification_cb, NULL);
4012   GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4013   registered_quota_notifications = NULL;
4014
4015   neighbours = NULL;
4016   callback_cls = NULL;
4017   connect_notify_cb = NULL;
4018   disconnect_notify_cb = NULL;
4019   neighbour_change_cb = NULL;
4020 }
4021
4022
4023 /* end of file gnunet-service-transport_neighbours.c */