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