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