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