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