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