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