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