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