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