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