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