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