remove CONNECT_SWITCHING_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_SENT:
1110     /* we are currently connected, need to send disconnect and do
1111        internal notifications and update statistics */
1112     send_disconnect (n);
1113     GNUNET_STATISTICS_set (GST_stats,
1114                            gettext_noop ("# peers connected"),
1115                            --neighbours_connected,
1116                            GNUNET_NO);
1117     disconnect_notify_cb (callback_cls, &n->id);
1118     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1119     break;
1120   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1121     /* ATS address request timeout, disconnect without sending disconnect message */
1122     GNUNET_STATISTICS_set (GST_stats,
1123                            gettext_noop ("# peers connected"),
1124                            --neighbours_connected,
1125                            GNUNET_NO);
1126     disconnect_notify_cb (callback_cls, &n->id);
1127     set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1128     break;
1129   case GNUNET_TRANSPORT_PS_DISCONNECT:
1130     /* already disconnected, ignore */
1131     break;
1132   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1133     /* already cleaned up, how did we get here!? */
1134     GNUNET_assert (0);
1135     break;
1136   default:
1137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138                 "Unhandled state `%s'\n",
1139                 GNUNET_TRANSPORT_ps2s (n->state));
1140     GNUNET_break (0);
1141     break;
1142   }
1143   /* schedule timeout to clean up */
1144   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1145     GNUNET_SCHEDULER_cancel (n->task);
1146   n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1147                                           &master_task, n);
1148 }
1149
1150
1151 /**
1152  * We're done with our transmission attempt, continue processing.
1153  *
1154  * @param cls the `struct MessageQueue` of the message
1155  * @param receiver intended receiver
1156  * @param success whether it worked or not
1157  * @param size_payload bytes payload sent
1158  * @param physical bytes sent on wire
1159  */
1160 static void
1161 transmit_send_continuation (void *cls,
1162                             const struct GNUNET_PeerIdentity *receiver,
1163                             int success, size_t size_payload, size_t physical)
1164 {
1165   struct MessageQueue *mq = cls;
1166   struct NeighbourMapEntry *n;
1167
1168   if (NULL == (n = lookup_neighbour (receiver)))
1169   {
1170     GNUNET_free (mq);
1171     return; /* disconnect or other error while transmitting, can happen */
1172   }
1173   if (n->is_active == mq)
1174   {
1175     /* this is still "our" neighbour, remove us from its queue
1176        and allow it to send the next message now */
1177     n->is_active = NULL;
1178     if (GNUNET_SCHEDULER_NO_TASK != n->task)
1179       GNUNET_SCHEDULER_cancel (n->task);
1180     n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1181   }
1182   if (bytes_in_send_queue < mq->message_buf_size)
1183   {
1184     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1185                 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1186                 bytes_in_send_queue, mq->message_buf_size,
1187                 (GNUNET_OK == success) ? "OK" : "FAIL",
1188                 size_payload, physical);
1189     GNUNET_break (0);
1190   }
1191
1192
1193   GNUNET_break (size_payload == mq->message_buf_size);
1194   bytes_in_send_queue -= mq->message_buf_size;
1195   GNUNET_STATISTICS_set (GST_stats,
1196                         gettext_noop
1197                          ("# bytes in message queue for other peers"),
1198                          bytes_in_send_queue, GNUNET_NO);
1199   if (GNUNET_OK == success)
1200     GNUNET_STATISTICS_update (GST_stats,
1201                               gettext_noop
1202                               ("# messages transmitted to other peers"),
1203                               1, GNUNET_NO);
1204   else
1205     GNUNET_STATISTICS_update (GST_stats,
1206                               gettext_noop
1207                               ("# transmission failures for messages to other peers"),
1208                               1, GNUNET_NO);
1209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210               "Sending message to `%s' of type %u was a %s\n",
1211               GNUNET_i2s (receiver),
1212               ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1213               (success == GNUNET_OK) ? "success" : "FAILURE");
1214   if (NULL != mq->cont)
1215     mq->cont (mq->cont_cls, success, size_payload, physical);
1216   GNUNET_free (mq);
1217 }
1218
1219
1220 /**
1221  * Check the message list for the given neighbour and if we can
1222  * send a message, do so.  This function should only be called
1223  * if the connection is at least generally ready for transmission.
1224  * While we will only send one message at a time, no bandwidth
1225  * quota management is performed here.  If a message was given to
1226  * the plugin, the continuation will automatically re-schedule
1227  * the 'master' task once the next message might be transmitted.
1228  *
1229  * @param n target peer for which to transmit
1230  */
1231 static void
1232 try_transmission_to_peer (struct NeighbourMapEntry *n)
1233 {
1234   struct MessageQueue *mq;
1235   struct GNUNET_TIME_Relative timeout;
1236
1237   if (NULL == n->primary_address.address)
1238   {
1239     /* no address, why are we here? */
1240     GNUNET_break (0);
1241     return;
1242   }
1243   if ((0 == n->primary_address.address->address_length) &&
1244       (NULL == n->primary_address.session))
1245   {
1246     /* no address, why are we here? */
1247     GNUNET_break (0);
1248     return;
1249   }
1250   if (NULL != n->is_active)
1251   {
1252     /* transmission already pending */
1253     return;
1254   }
1255
1256   /* timeout messages from the queue that are past their due date */
1257   while (NULL != (mq = n->messages_head))
1258   {
1259     timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1260     if (timeout.rel_value_us > 0)
1261       break;
1262     GNUNET_STATISTICS_update (GST_stats,
1263                               gettext_noop
1264                               ("# messages timed out while in transport queue"),
1265                               1, GNUNET_NO);
1266     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1267     n->is_active = mq;
1268     transmit_send_continuation (mq, &n->id,
1269                                 GNUNET_SYSERR,
1270                                 mq->message_buf_size, 0);     /* timeout */
1271   }
1272   if (NULL == mq)
1273     return;                     /* no more messages */
1274   GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1275   n->is_active = mq;
1276   (void) send_with_session (n,
1277                             mq->message_buf, mq->message_buf_size,
1278                             0 /* priority */, timeout, GNUNET_NO,
1279                             &transmit_send_continuation, mq);
1280 }
1281
1282
1283 /**
1284  * Send keepalive message to the neighbour.  Must only be called
1285  * if we are on 'connected' state or while trying to switch addresses.
1286  * Will internally determine if a keepalive is truly needed (so can
1287  * always be called).
1288  *
1289  * @param n neighbour that went idle and needs a keepalive
1290  */
1291 static void
1292 send_keepalive (struct NeighbourMapEntry *n)
1293 {
1294   struct SessionKeepAliveMessage m;
1295   struct GNUNET_TIME_Relative timeout;
1296   uint32_t nonce;
1297
1298   GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1299                  (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1300   if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1301     return; /* no keepalive needed at this time */
1302
1303   nonce = 0; /* 0 indicates 'not set' */
1304   while (0 == nonce)
1305     nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1306
1307   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308       "Sending keep alive to peer `%s' with nonce %u\n",
1309       GNUNET_i2s (&n->id), nonce);
1310
1311   m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1312   m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1313   m.nonce = htonl (nonce);
1314
1315   timeout = send_with_session (n,
1316                                (const void *) &m, sizeof (m),
1317                                UINT32_MAX /* priority */,
1318                                GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1319                                NULL, NULL);
1320   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1321                             GNUNET_NO);
1322   n->primary_address.keep_alive_nonce = nonce;
1323   n->expect_latency_response = GNUNET_YES;
1324   n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1325   n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1326
1327 }
1328
1329
1330 /**
1331  * Keep the connection to the given neighbour alive longer,
1332  * we received a KEEPALIVE (or equivalent); send a response.
1333  *
1334  * @param neighbour neighbour to keep alive (by sending keep alive response)
1335  * @param m the keep alive message containing the nonce to respond to
1336  */
1337 void
1338 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1339     const struct GNUNET_MessageHeader *m)
1340 {
1341   struct NeighbourMapEntry *n;
1342   const struct SessionKeepAliveMessage *msg_in;
1343   struct SessionKeepAliveMessage msg;
1344
1345   if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1346     return;
1347
1348   msg_in = (struct SessionKeepAliveMessage *) m;
1349   if (NULL == (n = lookup_neighbour (neighbour)))
1350   {
1351     GNUNET_STATISTICS_update (GST_stats,
1352                               gettext_noop
1353                               ("# KEEPALIVE messages discarded (peer unknown)"),
1354                               1, GNUNET_NO);
1355     return;
1356   }
1357   if (NULL == n->primary_address.session)
1358   {
1359     GNUNET_STATISTICS_update (GST_stats,
1360                               gettext_noop
1361                               ("# KEEPALIVE messages discarded (no session)"),
1362                               1, GNUNET_NO);
1363     return;
1364   }
1365
1366   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367       "Received keep alive request from peer `%s' with nonce %u\n",
1368       GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1369
1370   /* send reply to allow neighbour to measure latency */
1371   msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1372   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1373   msg.nonce = msg_in->nonce;
1374   (void) send_with_session(n,
1375                            (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1376                            UINT32_MAX /* priority */,
1377                            GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1378                            NULL, NULL);
1379 }
1380
1381
1382 /**
1383  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1384  * latency to this peer.  Pass the updated information (existing ats
1385  * plus calculated latency) to ATS.
1386  *
1387  * @param neighbour neighbour to keep alive
1388  * @param m the message containing the keep alive response
1389  */
1390 void
1391 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1392     const struct GNUNET_MessageHeader *m)
1393 {
1394   struct NeighbourMapEntry *n;
1395   const struct SessionKeepAliveMessage *msg;
1396   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1397   uint32_t latency;
1398   struct GNUNET_ATS_Information ats;
1399
1400   if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1401     return;
1402
1403   msg = (const struct SessionKeepAliveMessage *) m;
1404   if (NULL == (n = lookup_neighbour (neighbour)))
1405   {
1406     GNUNET_STATISTICS_update (GST_stats,
1407                               gettext_noop
1408                               ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1409                               1, GNUNET_NO);
1410     return;
1411   }
1412   if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1413        (GNUNET_YES != n->expect_latency_response) )
1414   {
1415     GNUNET_STATISTICS_update (GST_stats,
1416                               gettext_noop
1417                               ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1418                               1, GNUNET_NO);
1419     return;
1420   }
1421   if (NULL == n->primary_address.address)
1422   {
1423     GNUNET_STATISTICS_update (GST_stats,
1424                               gettext_noop
1425                               ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1426                               1, GNUNET_NO);
1427     return;
1428   }
1429   if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1430   {
1431     GNUNET_STATISTICS_update (GST_stats,
1432                               gettext_noop
1433                               ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1434                               1, GNUNET_NO);
1435     return;
1436   }
1437   else
1438   {
1439     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1440         "Received keep alive response from peer `%s' for session %p\n",
1441         GNUNET_i2s (&n->id), n->primary_address.session);
1442
1443   }
1444
1445   /* Update session timeout here */
1446   if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1447   {
1448     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449         "Updating session for peer `%s' for session %p\n",
1450         GNUNET_i2s (&n->id), n->primary_address.session);
1451     papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1452   }
1453   else
1454   {
1455     GNUNET_break (0);
1456   }
1457
1458   n->primary_address.keep_alive_nonce = 0;
1459   n->expect_latency_response = GNUNET_NO;
1460   n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1461   set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1462
1463   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464               "Latency for peer `%s' is %s\n",
1465               GNUNET_i2s (&n->id),
1466               GNUNET_STRINGS_relative_time_to_string (n->latency,
1467                                                       GNUNET_YES));
1468   /* append latency */
1469   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1470   if (n->latency.rel_value_us > UINT32_MAX)
1471     latency = UINT32_MAX;
1472   else
1473     latency = n->latency.rel_value_us;
1474   ats.value = htonl (latency);
1475   GST_ats_update_metrics (&n->id, n->primary_address.address,
1476       n->primary_address.session, &ats, 1);
1477 }
1478
1479
1480 /**
1481  * We have received a message from the given sender.  How long should
1482  * we delay before receiving more?  (Also used to keep the peer marked
1483  * as live).
1484  *
1485  * @param sender sender of the message
1486  * @param size size of the message
1487  * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1488  *                   #GNUNET_NO if the neighbour is not connected or violates the quota,
1489  *                   #GNUNET_SYSERR if the connection is not fully up yet
1490  * @return how long to wait before reading more from this sender
1491  */
1492 struct GNUNET_TIME_Relative
1493 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1494                                         *sender, ssize_t size, int *do_forward)
1495 {
1496   struct NeighbourMapEntry *n;
1497   struct GNUNET_TIME_Relative ret;
1498
1499   if (NULL == neighbours)
1500   {
1501     *do_forward = GNUNET_NO;
1502     return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1503   }
1504   if (NULL == (n = lookup_neighbour (sender)))
1505   {
1506     GST_neighbours_try_connect (sender);
1507     if (NULL == (n = lookup_neighbour (sender)))
1508     {
1509       GNUNET_STATISTICS_update (GST_stats,
1510                                 gettext_noop
1511                                 ("# messages discarded due to lack of neighbour record"),
1512                                 1, GNUNET_NO);
1513       *do_forward = GNUNET_NO;
1514       return GNUNET_TIME_UNIT_ZERO;
1515     }
1516   }
1517   if (! test_connected (n))
1518   {
1519     *do_forward = GNUNET_SYSERR;
1520     return GNUNET_TIME_UNIT_ZERO;
1521   }
1522   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1523   {
1524     n->quota_violation_count++;
1525     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526                 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1527                 n->in_tracker.available_bytes_per_s__,
1528                 n->quota_violation_count);
1529     /* Discount 32k per violation */
1530     GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1531   }
1532   else
1533   {
1534     if (n->quota_violation_count > 0)
1535     {
1536       /* try to add 32k back */
1537       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1538       n->quota_violation_count--;
1539     }
1540   }
1541   if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1542   {
1543     GNUNET_STATISTICS_update (GST_stats,
1544                               gettext_noop
1545                               ("# bandwidth quota violations by other peers"),
1546                               1, GNUNET_NO);
1547     *do_forward = GNUNET_NO;
1548     return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1549   }
1550   *do_forward = GNUNET_YES;
1551   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1552   if (ret.rel_value_us > 0)
1553   {
1554     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555                 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1556                 (unsigned long long) n->in_tracker.
1557                 consumption_since_last_update__,
1558                 (unsigned int) n->in_tracker.available_bytes_per_s__,
1559                 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1560     GNUNET_STATISTICS_update (GST_stats,
1561                               gettext_noop ("# ms throttling suggested"),
1562                               (int64_t) ret.rel_value_us / 1000LL,
1563                               GNUNET_NO);
1564   }
1565   return ret;
1566 }
1567
1568
1569 /**
1570  * Transmit a message to the given target using the active connection.
1571  *
1572  * @param target destination
1573  * @param msg message to send
1574  * @param msg_size number of bytes in msg
1575  * @param timeout when to fail with timeout
1576  * @param cont function to call when done
1577  * @param cont_cls closure for 'cont'
1578  */
1579 void
1580 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1581                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
1582                      GST_NeighbourSendContinuation cont, void *cont_cls)
1583 {
1584   struct NeighbourMapEntry *n;
1585   struct MessageQueue *mq;
1586
1587   /* All ove these cases should never happen; they are all API violations.
1588      But we check anyway, just to be sure. */
1589   if (NULL == (n = lookup_neighbour (target)))
1590   {
1591     GNUNET_break (0);
1592     if (NULL != cont)
1593       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1594     return;
1595   }
1596   if (GNUNET_YES != test_connected (n))
1597   {
1598     GNUNET_break (0);
1599     if (NULL != cont)
1600       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1601     return;
1602   }
1603   bytes_in_send_queue += msg_size;
1604   GNUNET_STATISTICS_set (GST_stats,
1605                          gettext_noop
1606                          ("# bytes in message queue for other peers"),
1607                          bytes_in_send_queue, GNUNET_NO);
1608   mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1609   mq->cont = cont;
1610   mq->cont_cls = cont_cls;
1611   memcpy (&mq[1], msg, msg_size);
1612   mq->message_buf = (const char *) &mq[1];
1613   mq->message_buf_size = msg_size;
1614   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1615   GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1616   if ( (NULL != n->is_active) ||
1617        ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1618     return;
1619   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1620     GNUNET_SCHEDULER_cancel (n->task);
1621   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1622 }
1623
1624 static void
1625 send_session_connect_cont (void *cls,
1626                       const struct GNUNET_PeerIdentity *target,
1627                       int result,
1628                       size_t size_payload,
1629                       size_t size_on_wire)
1630 {
1631   struct NeighbourMapEntry *n;
1632
1633   n = lookup_neighbour (target);
1634   if (NULL == n)
1635   {
1636     /* CONNECT continuation was called after neighbor was freed,
1637      * for example due to a time out for the state or the session
1638      * used was already terminated: nothing to do here... */
1639     return;
1640   }
1641
1642   if ( (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) &&
1643        (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) )
1644   {
1645     /* CONNECT continuation was called after neighbor changed state,
1646      * for example due to a time out for the state or the session
1647      * used was already terminated: nothing to do here... */
1648     return;
1649   }
1650   if (GNUNET_OK == result)
1651     return;
1652
1653   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1654             _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1655             GNUNET_i2s (target),
1656             GST_plugins_a2s (n->primary_address.address),
1657             n->primary_address.session);
1658
1659   /* Failed to send CONNECT message with this address */
1660   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1661       n->primary_address.session);
1662   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1663       NULL);
1664
1665   /* Remove address and request and additional one */
1666   unset_primary_address (n);
1667
1668   if (n->state == GNUNET_TRANSPORT_PS_RECONNECT_SENT)
1669   {
1670     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1671       GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1672
1673   }
1674   else if (n->state == GNUNET_TRANSPORT_PS_INIT_ATS)
1675   {
1676     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1677       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1678   }
1679   else
1680   {
1681     GNUNET_break (0);
1682     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1683       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1684   }
1685
1686   return;
1687 }
1688
1689 /**
1690  * Send a SESSION_CONNECT message via the given address.
1691  *
1692  * @param na address to use
1693  */
1694 static void
1695 send_session_connect (struct NeighbourAddress *na)
1696 {
1697   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1698   struct SessionConnectMessage connect_msg;
1699   struct NeighbourMapEntry *n;
1700
1701   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702               "Sending SESSION_CONNECT message to peer %s\n",
1703               GNUNET_i2s (&na->address->peer));
1704   if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1705   {
1706     GNUNET_break (0);
1707     return;
1708   }
1709   if (NULL == na->session)
1710     na->session = papi->get_session (papi->cls, na->address);
1711   if (NULL == na->session)
1712   {
1713     GNUNET_break (0);
1714     return;
1715   }
1716   GNUNET_STATISTICS_update (GST_stats,
1717                             gettext_noop
1718                             ("# SESSION_CONNECT messages sent"),
1719                             1, GNUNET_NO);
1720   na->connect_timestamp = GNUNET_TIME_absolute_get ();
1721   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1722   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1723   connect_msg.reserved = htonl (0);
1724   connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1725   if (-1 ==
1726       papi->send (papi->cls,
1727                   na->session,
1728                   (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1729                   UINT_MAX,
1730                   SETUP_CONNECTION_TIMEOUT,
1731                   send_session_connect_cont, NULL))
1732   {
1733     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1734                 _("Failed to transmit CONNECT message via plugin to %s\n"),
1735                 GST_plugins_a2s (na->address));
1736
1737     n = lookup_neighbour (&na->address->peer);
1738     if (NULL == n)
1739     {
1740       GNUNET_break (0);
1741       return;
1742     }
1743     /* Hard failure to send the CONNECT message with this address:
1744        Destroy address and session */
1745     GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1746     GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1747
1748     /* Remove address and request and additional one */
1749     unset_primary_address (n);
1750     if (n->state == GNUNET_TRANSPORT_PS_RECONNECT_SENT)
1751     {
1752       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1753         GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1754
1755     }
1756     else if (n->state == GNUNET_TRANSPORT_PS_INIT_ATS)
1757     {
1758       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1759         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1760     }
1761     else
1762     {
1763       GNUNET_break (0);
1764       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1765         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1766     }
1767     return;
1768   }
1769   GST_neighbours_notify_data_sent (&na->address->peer,
1770                                    na->address,
1771                                    na->session,
1772                                    sizeof (struct SessionConnectMessage));
1773 }
1774
1775
1776 static void
1777 send_session_connect_ack_cont (void *cls,
1778                       const struct GNUNET_PeerIdentity *target,
1779                       int result,
1780                       size_t size_payload,
1781                       size_t size_on_wire)
1782 {
1783   struct NeighbourMapEntry *n;
1784
1785   n = lookup_neighbour (target);
1786   if (NULL == n)
1787   {
1788     /* CONNECT_ACK continuation was called after neighbor was freed,
1789      * for example due to a time out for the state or the session
1790      * used was already terminated: nothing to do here... */
1791     return;
1792   }
1793
1794   if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1795   {
1796     /* CONNECT_ACK continuation was called after neighbor changed state,
1797      * for example due to a time out for the state or the session
1798      * used was already terminated: nothing to do here... */
1799     return;
1800   }
1801   if (GNUNET_OK == result)
1802     return;
1803
1804   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1805             _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1806             GNUNET_i2s (target),
1807             GST_plugins_a2s (n->primary_address.address),
1808             n->primary_address.session);
1809
1810   /* Failed to send CONNECT_ACK message with this address */
1811   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1812       n->primary_address.session);
1813   GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1814       NULL);
1815
1816   /* Remove address and request and additional one */
1817   unset_primary_address (n);
1818   n->ack_state = ACK_SEND_CONNECT_ACK;
1819   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1820       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1821   return;
1822 }
1823
1824
1825 /**
1826  * Send a CONNECT_ACK message via the given address.
1827  *
1828  * @param address address to use
1829  * @param session session to use
1830  * @param timestamp timestamp to use for the ACK message
1831  */
1832 static void
1833 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1834                                   struct Session *session,
1835                                   struct GNUNET_TIME_Absolute timestamp)
1836 {
1837   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1838   struct SessionConnectMessage connect_msg;
1839   struct NeighbourMapEntry *n;
1840
1841   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842               "Sending CONNECT_ACK to peer `%s'\n",
1843               GNUNET_i2s (&address->peer));
1844   if (NULL == (papi = GST_plugins_find (address->transport_name)))
1845   {
1846     GNUNET_break (0);
1847     return;
1848   }
1849   if (NULL == session)
1850     session = papi->get_session (papi->cls, address);
1851   if (NULL == session)
1852   {
1853     GNUNET_break (0);
1854     return;
1855   }
1856   GNUNET_STATISTICS_update (GST_stats,
1857                             gettext_noop
1858                             ("# CONNECT_ACK messages sent"),
1859                             1, GNUNET_NO);
1860   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1861   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1862   connect_msg.reserved = htonl (0);
1863   connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1864
1865   if (GNUNET_SYSERR == papi->send (papi->cls,
1866                      session,
1867                      (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1868                      UINT_MAX,
1869                      GNUNET_TIME_UNIT_FOREVER_REL,
1870                      send_session_connect_ack_cont, NULL))
1871   {
1872     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1873                 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1874                 GST_plugins_a2s (address));
1875
1876     n = lookup_neighbour (&address->peer);
1877     if (NULL == n)
1878     {
1879       GNUNET_break (0);
1880       return;
1881     }
1882     /* Hard failure to send the CONNECT_ACK message with this address:
1883        Destroy session (and address)  */
1884     if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1885         GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1886     {
1887       GNUNET_ATS_address_destroyed (GST_ats, address, session);
1888       GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1889     }
1890     else
1891       GNUNET_ATS_address_destroyed (GST_ats, address, session);
1892
1893     /* Remove address and request and additional one */
1894     unset_primary_address (n);
1895     n->ack_state = ACK_SEND_CONNECT_ACK;
1896     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1897         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1898     return;
1899   }
1900
1901 }
1902
1903 struct QuotaNotificationRequest
1904 {
1905   struct GNUNET_PeerIdentity peer;
1906   struct Session *session;
1907   char *plugin;
1908 };
1909
1910 struct QNR_LookContext
1911 {
1912   struct GNUNET_PeerIdentity peer;
1913   struct Session *session;
1914   const char *plugin;
1915
1916   struct QuotaNotificationRequest *res;
1917 };
1918
1919 static int
1920 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1921 {
1922   struct QNR_LookContext *qnr_ctx = cls;
1923   struct QuotaNotificationRequest *qnr = value;
1924
1925   if ((qnr->session == qnr_ctx->session) &&
1926       (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1927       (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1928   {
1929     qnr_ctx->res = value;
1930     return GNUNET_NO;
1931   }
1932   return GNUNET_YES;
1933 }
1934
1935 void
1936 GST_neighbours_register_quota_notification(void *cls,
1937     const struct GNUNET_PeerIdentity *peer, const char *plugin,
1938     struct Session *session)
1939 {
1940   struct QuotaNotificationRequest *qnr;
1941   struct QNR_LookContext qnr_ctx;
1942
1943   if (NULL == registered_quota_notifications)
1944   {
1945     return; /* init or shutdown */
1946   }
1947
1948   qnr_ctx.peer = (*peer);
1949   qnr_ctx.plugin = plugin;
1950   qnr_ctx.session = session;
1951   qnr_ctx.res = NULL;
1952
1953   GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1954       peer, &find_notification_request, &qnr_ctx);
1955   if (NULL != qnr_ctx.res)
1956   {
1957     GNUNET_break(0);
1958     return;
1959   }
1960
1961   qnr = GNUNET_new (struct QuotaNotificationRequest);
1962   qnr->peer =  (*peer);
1963   qnr->plugin = GNUNET_strdup (plugin);
1964   qnr->session = session;
1965
1966   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967       "Adding notification for peer `%s' plugin `%s' session %p \n",
1968       GNUNET_i2s (peer), plugin, session);
1969
1970   GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1971       qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1972 }
1973
1974
1975 void
1976 GST_neighbours_unregister_quota_notification(void *cls,
1977     const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1978 {
1979   struct QNR_LookContext qnr_ctx;
1980
1981   if (NULL == registered_quota_notifications)
1982   {
1983     return; /* init or shutdown */
1984   }
1985
1986   qnr_ctx.peer = (*peer);
1987   qnr_ctx.plugin = plugin;
1988   qnr_ctx.session = session;
1989   qnr_ctx.res = NULL;
1990
1991   GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1992       &find_notification_request, &qnr_ctx);
1993   if (NULL == qnr_ctx.res)
1994   {
1995     GNUNET_break(0);
1996     return;
1997   }
1998
1999   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000       "Removing notification for peer `%s' plugin `%s' session %p \n",
2001       GNUNET_i2s (peer), plugin, session);
2002
2003   GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2004       qnr_ctx.res);
2005   GNUNET_free (qnr_ctx.res->plugin);
2006   GNUNET_free (qnr_ctx.res);
2007 }
2008
2009 static int
2010 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2011 {
2012   /* struct NeighbourMapEntry *n = cls; */
2013   struct QuotaNotificationRequest *qnr = value;
2014   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2015   struct GNUNET_TIME_Relative delay;
2016   int do_forward;
2017
2018   papi = GST_plugins_find(qnr->plugin);
2019   if (NULL == papi)
2020   {
2021     GNUNET_break (0);
2022     return GNUNET_OK;
2023   }
2024
2025   delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2026   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027       "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2028       delay.rel_value_us / 1000);
2029
2030   if (NULL != papi->update_inbound_delay)
2031     papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2032   return GNUNET_OK;
2033 }
2034
2035 static
2036 int
2037 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2038     void *value)
2039 {
2040   /* struct NeighbourMapEntry *n = cls; */
2041   struct QuotaNotificationRequest *qnr = value;
2042
2043   GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2044       qnr);
2045   GNUNET_free(qnr->plugin);
2046   GNUNET_free(qnr);
2047
2048   return GNUNET_OK;
2049 }
2050
2051 static void
2052 inbound_bw_tracker_update(void *cls)
2053 {
2054   struct NeighbourMapEntry *n = cls;
2055
2056   /* Quota was updated, tell plugins to update the time to receive next */
2057   GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2058       &n->id, &notification_cb, n);
2059 }
2060
2061
2062 /**
2063  * Create a fresh entry in the neighbour map for the given peer
2064  *
2065  * @param peer peer to create an entry for
2066  * @return new neighbour map entry
2067  */
2068 static struct NeighbourMapEntry *
2069 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2070 {
2071   struct NeighbourMapEntry *n;
2072
2073   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2074               "Creating new neighbour entry for `%s'\n",
2075               GNUNET_i2s (peer));
2076   n = GNUNET_new (struct NeighbourMapEntry);
2077   n->id = *peer;
2078   n->ack_state = ACK_UNDEFINED;
2079   n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2080   n->last_util_transmission = GNUNET_TIME_absolute_get();
2081   n->util_payload_bytes_recv = 0;
2082   n->util_payload_bytes_sent = 0;
2083   n->util_total_bytes_recv = 0;
2084   n->util_total_bytes_sent = 0;
2085   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2086                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2087                                  MAX_BANDWIDTH_CARRY_S);
2088   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2089   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2090   GNUNET_assert (GNUNET_OK ==
2091                  GNUNET_CONTAINER_multipeermap_put (neighbours,
2092                                                     &n->id, n,
2093                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2094   return n;
2095 }
2096
2097 /* We received a address suggestion after requesting an address in
2098  * try_connect or after receiving a connect, switch to address
2099  */
2100 static void
2101 address_suggest_cont (void *cls,
2102     const struct GNUNET_PeerIdentity *peer,
2103     const struct GNUNET_HELLO_Address *address, struct Session *session,
2104     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2105     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2106     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2107 {
2108   GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2109       bandwidth_in, bandwidth_out);
2110 }
2111
2112
2113 struct BlacklistCheckSwitchContext
2114 {
2115   struct BlacklistCheckSwitchContext *prev;
2116   struct BlacklistCheckSwitchContext *next;
2117
2118
2119   struct GST_BlacklistCheck *blc;
2120
2121   struct GNUNET_HELLO_Address *address;
2122   struct Session *session;
2123   struct GNUNET_ATS_Information *ats;
2124   uint32_t ats_count;
2125
2126   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2127   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2128 };
2129
2130 /**
2131  * Black list check result for try_connect call
2132  * If connection to the peer is allowed request adddress and
2133  *
2134  * @param cls blc_ctx bl context
2135  * @param peer the peer
2136  * @param result the result
2137  */
2138 static void
2139 try_connect_bl_check_cont (void *cls,
2140     const struct GNUNET_PeerIdentity *peer, int result)
2141 {
2142   struct BlacklistCheckSwitchContext *blc_ctx = cls;
2143   struct NeighbourMapEntry *n;
2144
2145   GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2146   GNUNET_free (blc_ctx);
2147
2148   if (GNUNET_OK != result)
2149   {
2150     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2151         _("Blacklisting disapproved to connect to peer `%s'\n"),
2152         GNUNET_i2s (peer));
2153     return;
2154   }
2155
2156   /* Setup a new neighbour */
2157   n = setup_neighbour (peer);
2158
2159   /* Request address suggestions for this peer */
2160   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2161       GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2162   GNUNET_ATS_reset_backoff (GST_ats, peer);
2163   n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2164       &address_suggest_cont, n);
2165 }
2166
2167
2168
2169 /**
2170  * Try to create a connection to the given target (eventually).
2171  *
2172  * @param target peer to try to connect to
2173  */
2174 void
2175 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2176 {
2177   struct NeighbourMapEntry *n;
2178   struct GST_BlacklistCheck *blc;
2179   struct BlacklistCheckSwitchContext *blc_ctx;
2180
2181   if (NULL == neighbours)
2182   {
2183     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184                 "Asked to connect to peer `%s' during shutdown\n",
2185                 GNUNET_i2s (target));
2186     return; /* during shutdown, do nothing */
2187   }
2188   n = lookup_neighbour (target);
2189   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2190               "Asked to connect to peer `%s' (state: %s)\n",
2191               GNUNET_i2s (target),
2192               (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2193   if (NULL != n)
2194   {
2195     switch (n->state)
2196     {
2197     case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2198       /* this should not be possible */
2199       GNUNET_break (0);
2200       free_neighbour (n, GNUNET_NO);
2201       break;
2202     case GNUNET_TRANSPORT_PS_INIT_ATS:
2203     case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2204     case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2205     case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2206       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2207                   "Ignoring request to try to connect to `%s', already trying!\n",
2208                   GNUNET_i2s (target));
2209       return; /* already trying */
2210     case GNUNET_TRANSPORT_PS_CONNECTED:
2211     case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2212     case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2213     case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2214       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2215                   "Ignoring request to try to connect, already connected to `%s'!\n",
2216                   GNUNET_i2s (target));
2217       return; /* already connected */
2218     case GNUNET_TRANSPORT_PS_DISCONNECT:
2219       /* get rid of remains, ready to re-try immediately */
2220       free_neighbour (n, GNUNET_NO);
2221       break;
2222     case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2223       /* should not be possible */
2224       GNUNET_assert (0);
2225       return;
2226     default:
2227       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2228                   "Unhandled state `%s'\n",
2229                   GNUNET_TRANSPORT_ps2s (n->state));
2230       GNUNET_break (0);
2231       free_neighbour (n, GNUNET_NO);
2232       break;
2233     }
2234   }
2235
2236   /* Do blacklist check if connecting to this peer is allowed */
2237   blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2238   GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2239
2240   if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2241         &try_connect_bl_check_cont, blc_ctx)))
2242   {
2243     blc_ctx->blc = blc;
2244   }
2245 }
2246
2247
2248 /**
2249  * Function called with the result of a blacklist check.
2250  *
2251  * @param cls closure with the `struct BlackListCheckContext`
2252  * @param peer peer this check affects
2253  * @param result #GNUNET_OK if the address is allowed
2254  */
2255 static void
2256 handle_connect_blacklist_check_cont (void *cls,
2257                             const struct GNUNET_PeerIdentity *peer,
2258                             int result)
2259 {
2260   struct BlackListCheckContext *bcc = cls;
2261   struct NeighbourMapEntry *n;
2262
2263   bcc->bc = NULL;
2264   GNUNET_CONTAINER_DLL_remove (bc_head,
2265                                bc_tail,
2266                                bcc);
2267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268               "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2269               GNUNET_i2s (peer),
2270               (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2271
2272   if (NULL == (n = lookup_neighbour (peer)))
2273   {
2274     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275                 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2276                 GNUNET_i2s (peer));
2277     goto cleanup; /* nobody left to care about new address */
2278   }
2279
2280   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2281               "Blacklist check after CONNECT for peer `%s' in state %s/%s: %s\n",
2282               GNUNET_i2s (peer),
2283               GNUNET_TRANSPORT_ps2s (n->state),
2284               print_ack_state (n->ack_state),
2285               (GNUNET_OK == result) ? "OK" : "FAIL");
2286
2287   if (GNUNET_OK == result)
2288   {
2289     /* Blacklist agreed on connecting to a peer with this address, notify ATS */
2290     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2291         "Notifying ATS peer's `%s' %s address `%s' session %p\n",
2292         GNUNET_i2s (peer),
2293         (GNUNET_YES == GNUNET_HELLO_address_check_option(bcc->na.address,
2294             GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ? "inbound" : "outbound",
2295         GST_plugins_a2s (bcc->na.address), bcc->na.session);
2296     GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2297   }
2298
2299   switch (n->state)
2300   {
2301   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2302     /* This should not be possible */
2303     GNUNET_break (0);
2304     free_neighbour (n, GNUNET_NO);
2305     break;
2306   case GNUNET_TRANSPORT_PS_INIT_ATS:
2307     /* Waiting on ATS suggestion */
2308     break;
2309   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2310 #if 0
2311     /* TODO Why should I send an connect ACK message */
2312     /* waiting on CONNECT_ACK, send ACK if one is pending */
2313
2314     if ( (GNUNET_OK == result) &&
2315          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2316     {
2317       n->ack_state = ACK_SEND_SESSION_ACK;
2318       send_connect_ack_message (n->primary_address.address,
2319                                         n->primary_address.session,
2320                                         n->connect_ack_timestamp);
2321     }
2322 #endif
2323     break;
2324   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2325     /* waiting on ATS suggestion, don't care about blacklist */
2326     break;
2327   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2328     /* waiting on SESSION_ACK, send ACK if one is pending */
2329     if ( (GNUNET_OK == result) &&
2330          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2331     {
2332       /* TODO: Why should this happen? */
2333       /* *Debug message: */ GNUNET_break (0);
2334
2335       n->ack_state = ACK_SEND_SESSION_ACK;
2336       send_connect_ack_message (n->primary_address.address,
2337                                         n->primary_address.session,
2338                                         n->connect_ack_timestamp);
2339     }
2340     break;
2341   case GNUNET_TRANSPORT_PS_CONNECTED:
2342     /* already connected, don't care about blacklist */
2343     break;
2344   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2345     /* still waiting on ATS suggestion, don't care about blacklist */
2346     break;
2347   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2348     /* waiting on CONNECT_ACK, don't care about blacklist */
2349     if ( (GNUNET_OK == result) &&
2350          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2351     {
2352       n->ack_state = ACK_SEND_SESSION_ACK;
2353       send_connect_ack_message (n->primary_address.address,
2354                                         n->primary_address.session,
2355                                         n->connect_ack_timestamp);
2356     }
2357     break;
2358   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2359     /* waiting on CONNECT_ACK, don't care about blacklist */
2360     if ( (GNUNET_OK == result) &&
2361          (ACK_SEND_CONNECT_ACK == n->ack_state) )
2362     {
2363       n->ack_state = ACK_SEND_SESSION_ACK;
2364       send_connect_ack_message (n->primary_address.address,
2365                                         n->primary_address.session,
2366                                         n->connect_ack_timestamp);
2367     }
2368     break;
2369   case GNUNET_TRANSPORT_PS_DISCONNECT:
2370     /* Nothing to do here, ATS will already do what can be done */
2371     break;
2372   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2373     /* should not be possible */
2374     GNUNET_assert (0);
2375     break;
2376   default:
2377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2378                 "Unhandled state `%s'\n",
2379                 GNUNET_TRANSPORT_ps2s (n->state));
2380     GNUNET_break (0);
2381     free_neighbour (n, GNUNET_NO);
2382     break;
2383   }
2384  cleanup:
2385   GNUNET_HELLO_address_free (bcc->na.address);
2386   GNUNET_free (bcc);
2387 }
2388
2389
2390 /**
2391  * We received a CONNECT message and want to know if connecting to a particular
2392  * peer via a particular address is allowed.  Check it!
2393  *
2394  * @param peer identity of the peer to switch the address for
2395  * @param ts time at which the check was initiated
2396  * @param address address of the other peer, NULL if other peer
2397  *                       connected to us
2398  * @param session session to use (or NULL)
2399  */
2400 static void
2401 connect_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2402                  struct GNUNET_TIME_Absolute ts,
2403                  const struct GNUNET_HELLO_Address *address,
2404                  struct Session *session)
2405 {
2406   struct BlackListCheckContext *bcc;
2407   struct GST_BlacklistCheck *bc;
2408
2409   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410               "Checking peer `%s' against blacklist\n",
2411               GNUNET_i2s (peer));
2412   bcc = GNUNET_new (struct BlackListCheckContext);
2413   bcc->na.address = GNUNET_HELLO_address_copy (address);
2414   bcc->na.session = session;
2415   bcc->na.connect_timestamp = ts;
2416   GNUNET_CONTAINER_DLL_insert (bc_head,
2417                                bc_tail,
2418                                bcc);
2419   if (NULL != (bc = GST_blacklist_test_allowed (peer,
2420         (NULL != address) ? address->transport_name : NULL,
2421         &handle_connect_blacklist_check_cont, bcc)))
2422     bcc->bc = bc;
2423   /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2424      we must only store 'bc' if 'bc' is non-NULL... */
2425 }
2426
2427
2428 /**
2429  * We received a 'SESSION_CONNECT' message from the other peer.
2430  * Consider switching to it.
2431  *
2432  * @param message possibly a 'struct SessionConnectMessage' (check format)
2433  * @param peer identity of the peer to switch the address for
2434  * @param address address of the other peer, NULL if other peer
2435  *                       connected to us
2436  * @param session session to use (or NULL)
2437  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2438  */
2439 int
2440 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2441                                const struct GNUNET_PeerIdentity *peer,
2442                                const struct GNUNET_HELLO_Address *address,
2443                                struct Session *session)
2444 {
2445   const struct SessionConnectMessage *scm;
2446   struct NeighbourMapEntry *n;
2447   struct GNUNET_TIME_Absolute ts;
2448
2449   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2450               "Received CONNECT message from peer `%s' with `%s' %p\n",
2451               GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2452   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2453   {
2454     GNUNET_break_op (0);
2455     return GNUNET_SYSERR;
2456   }
2457   GNUNET_STATISTICS_update (GST_stats,
2458                             gettext_noop
2459                             ("# CONNECT messages received"),
2460                             1, GNUNET_NO);
2461   if (NULL == neighbours)
2462   {
2463     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2464                 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2465                 GNUNET_i2s (peer));
2466     return GNUNET_OK; /* we're shutting down */
2467   }
2468   scm = (const struct SessionConnectMessage *) message;
2469   GNUNET_break_op (0 == ntohl (scm->reserved));
2470   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2471   n = lookup_neighbour (peer);
2472   if (NULL == n)
2473   {
2474     /* This is a new neighbour and set to not connected */
2475     n = setup_neighbour (peer);
2476   }
2477
2478   /* Remember this CONNECT message in neighbour */
2479   n->ack_state = ACK_SEND_CONNECT_ACK;
2480   n->connect_ack_timestamp = ts;
2481
2482   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483               "Received CONNECT for peer `%s' in state %s/%s\n",
2484               GNUNET_i2s (peer),
2485               GNUNET_TRANSPORT_ps2s (n->state),
2486               print_ack_state (n->ack_state));
2487
2488   switch (n->state)
2489   {
2490   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2491     /* Request an address from ATS to send CONNECT_ACK to this peer */
2492     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2493         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2494     if (NULL == n->suggest_handle)
2495       GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2496     break;
2497   case GNUNET_TRANSPORT_PS_INIT_ATS:
2498     /* CONNECT message takes priority over us asking ATS for address:
2499      * Wait for ATS to suggest an address and send CONNECT_ACK */
2500     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2501         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2502     break;
2503   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2504   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2505   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2506     /* It can never hurt to have an alternative address in the above cases,
2507        see if it is allowed */
2508     connect_check_blacklist (peer, ts, address, session);
2509     break;
2510   case GNUNET_TRANSPORT_PS_CONNECTED:
2511     /* we are already connected and can thus send the ACK immediately;
2512        still, it can never hurt to have an alternative address, so also
2513        tell ATS  about it */
2514     GNUNET_assert (NULL != n->primary_address.address);
2515     GNUNET_assert (NULL != n->primary_address.session);
2516     n->ack_state = ACK_UNDEFINED;
2517     send_connect_ack_message (n->primary_address.address,
2518                                       n->primary_address.session, ts);
2519     connect_check_blacklist (peer, ts, address, session);
2520     break;
2521   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2522   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2523     /* It can never hurt to have an alternative address in the above cases,
2524        see if it is allowed */
2525     connect_check_blacklist (peer, ts, address, session);
2526     break;
2527   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2528     /* we are already connected and can thus send the ACK immediately;
2529        still, it can never hurt to have an alternative address, so also
2530        tell ATS  about it */
2531     GNUNET_assert (NULL != n->primary_address.address);
2532     GNUNET_assert (NULL != n->primary_address.session);
2533     n->ack_state = ACK_UNDEFINED;
2534     send_connect_ack_message (n->primary_address.address,
2535                                       n->primary_address.session, ts);
2536     connect_check_blacklist (peer, ts, address, session);
2537     break;
2538   case GNUNET_TRANSPORT_PS_DISCONNECT:
2539     /* get rid of remains without terminating sessions, ready to re-try */
2540     free_neighbour (n, GNUNET_YES);
2541     n = setup_neighbour (peer);
2542     /* Remember the CONNECT timestamp for ACK message */
2543     n->ack_state = ACK_SEND_CONNECT_ACK;
2544     n->connect_ack_timestamp = ts;
2545     /* Request an address for the peer */
2546     GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2547     GNUNET_ATS_reset_backoff (GST_ats, peer);
2548     set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2549     break;
2550   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2551     /* should not be possible */
2552     GNUNET_assert (0);
2553     break;
2554   default:
2555     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2556                 "Unhandled state `%s'\n",
2557                 GNUNET_TRANSPORT_ps2s (n->state));
2558     GNUNET_break (0);
2559     return GNUNET_SYSERR;
2560   }
2561   return GNUNET_OK;
2562 }
2563
2564 static void
2565 switch_address_bl_check_cont (void *cls,
2566     const struct GNUNET_PeerIdentity *peer, int result)
2567 {
2568   struct BlacklistCheckSwitchContext *blc_ctx = cls;
2569   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2570   struct NeighbourMapEntry *n;
2571
2572   if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2573        (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2574   {
2575     if (NULL == n)
2576     {
2577       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578                   "Peer %s is unknown, suggestion ignored\n",
2579                   GNUNET_i2s (peer));
2580     }
2581     if (result == GNUNET_NO)
2582     {
2583       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584           "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2585           GST_plugins_a2s (blc_ctx->address),
2586           blc_ctx->session,
2587           GNUNET_i2s (&blc_ctx->address->peer));
2588     }
2589     if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2590     {
2591       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592           "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2593           blc_ctx->address->transport_name,
2594           GST_plugins_a2s (blc_ctx->address),
2595           blc_ctx->session,
2596           GNUNET_i2s (&blc_ctx->address->peer));
2597     }
2598
2599     /* This address is blacklisted, delete address and session (if existing) in ATS */
2600     GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2601
2602     if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2603           GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2604     {
2605       /* This is an inbound address, destroy full  address */
2606       GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2607     }
2608
2609     /* Remove blacklist check and clean up */
2610     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2611     GNUNET_HELLO_address_free (blc_ctx->address);
2612     GNUNET_free_non_null (blc_ctx->ats);
2613     GNUNET_free (blc_ctx);
2614     return;
2615   }
2616
2617   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2618       "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2619       GST_plugins_a2s (blc_ctx->address),
2620       blc_ctx->session,
2621       GNUNET_i2s (&blc_ctx->address->peer));
2622
2623   if (NULL == blc_ctx->session)
2624   {
2625     blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2626     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627                 "Obtained new session for peer `%s' and  address '%s': %p\n",
2628                 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2629   }
2630   if (NULL == blc_ctx->session)
2631   {
2632     /* No session could be obtained, remove blacklist check and clean up */
2633     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634                 "Failed to obtain new session for peer `%s' and  address '%s'\n",
2635                 GNUNET_i2s (&blc_ctx->address->peer),
2636                 GST_plugins_a2s (blc_ctx->address));
2637     /* Delete address in ATS */
2638     GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2639
2640     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2641     GNUNET_HELLO_address_free (blc_ctx->address);
2642     GNUNET_free_non_null (blc_ctx->ats);
2643     GNUNET_free (blc_ctx);
2644     return;
2645   }
2646
2647   switch (n->state)
2648   {
2649   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2650     GNUNET_break (0);
2651     free_neighbour (n, GNUNET_NO);
2652     return;
2653   case GNUNET_TRANSPORT_PS_INIT_ATS:
2654     /* We requested an address and ATS suggests one:
2655      * set primary address and send CONNECT message*/
2656     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2657         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2658     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2659         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2660     send_session_connect (&n->primary_address);
2661     break;
2662   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2663     /* waiting on CONNECT_ACK, send ACK if one is pending */
2664     if (ACK_SEND_CONNECT_ACK == n->ack_state)
2665     {
2666       n->ack_state = ACK_SEND_SESSION_ACK;
2667       send_connect_ack_message (n->primary_address.address,
2668           n->primary_address.session, n->connect_ack_timestamp);
2669     }
2670     /* ATS suggests a different address, switch again */
2671     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2672         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2673     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2674         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2675     send_session_connect (&n->primary_address);
2676     break;
2677   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2678     /* We requested an address and ATS suggests one:
2679      * set primary address and send CONNECT_ACK message*/
2680     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2681         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2682     /* Send an ACK message as a response to the CONNECT msg */
2683     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2684         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2685     send_connect_ack_message (n->primary_address.address,
2686                               n->primary_address.session,
2687                               n->connect_ack_timestamp);
2688     if (ACK_SEND_CONNECT_ACK == n->ack_state)
2689       n->ack_state = ACK_SEND_SESSION_ACK;
2690     break;
2691   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2692     /* ATS asks us to switch while we were trying to connect; switch to new
2693        address and check blacklist again */
2694     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2695         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2696     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2697         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2698     if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2699     {
2700       n->ack_state = ACK_SEND_SESSION_ACK;
2701       send_connect_ack_message (n->primary_address.address,
2702           n->primary_address.session, n->connect_ack_timestamp);
2703     }
2704     break;
2705   case GNUNET_TRANSPORT_PS_CONNECTED:
2706     GNUNET_assert (NULL != n->primary_address.address);
2707     GNUNET_assert (NULL != n->primary_address.session);
2708     if (n->primary_address.session == blc_ctx->session)
2709     {
2710       /* not an address change, just a quota change */
2711       set_primary_address (n, blc_ctx->address, blc_ctx->session,
2712           blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2713       break;
2714     }
2715     /* ATS asks us to switch a life connection; see if we can get
2716        a CONNECT_ACK on it before we actually do this! */
2717     set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2718         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2719     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2720         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2721     send_session_connect (&n->alternative_address);
2722     break;
2723   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2724     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2725         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2726     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2727         GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2728     send_session_connect (&n->primary_address);
2729     break;
2730   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2731     /* ATS asks us to switch while we were trying to reconnect; switch to new
2732        address and send CONNECT again */
2733     set_primary_address (n, blc_ctx->address, blc_ctx->session,
2734         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2735     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2736         GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2737     send_session_connect (&n->primary_address);
2738     break;
2739   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2740     if (n->primary_address.session == blc_ctx->session)
2741     {
2742       /* ATS switches back to still-active session */
2743       free_address (&n->alternative_address);
2744       set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2745       break;
2746     }
2747     /* ATS asks us to switch a life connection, send */
2748     set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2749         blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2750     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2751         GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2752     send_session_connect (&n->alternative_address);
2753     break;
2754   case GNUNET_TRANSPORT_PS_DISCONNECT:
2755     /* not going to switch addresses while disconnecting */
2756     return;
2757   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2758     GNUNET_assert (0);
2759     break;
2760   default:
2761     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2762                 "Unhandled state `%s'\n",
2763                 GNUNET_TRANSPORT_ps2s (n->state));
2764     GNUNET_break (0);
2765     break;
2766   }
2767
2768   GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2769   GNUNET_HELLO_address_free(blc_ctx->address);
2770   GNUNET_free_non_null (blc_ctx->ats);
2771   GNUNET_free (blc_ctx);
2772   return;
2773 }
2774
2775
2776 /**
2777  * For the given peer, switch to this address.
2778  *
2779  * Before accepting this addresses and actively using it, a blacklist check
2780  * is performed. If this blacklist check fails the address will be destroyed.
2781  *
2782  * @param peer identity of the peer to switch the address for
2783  * @param address address of the other peer,
2784  * @param session session to use or NULL if transport should initiate a session
2785  * @param ats performance data
2786  * @param ats_count number of entries in ats
2787  * @param bandwidth_in inbound quota to be used when connection is up,
2788  *      0 to disconnect from peer
2789  * @param bandwidth_out outbound quota to be used when connection is up,
2790  *      0 to disconnect from peer
2791  */
2792 void
2793 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2794                                   const struct GNUNET_HELLO_Address *address,
2795                                   struct Session *session,
2796                                   const struct GNUNET_ATS_Information *ats,
2797                                   uint32_t ats_count,
2798                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2799                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2800 {
2801   struct NeighbourMapEntry *n;
2802   struct GST_BlacklistCheck *blc;
2803   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2804   struct BlacklistCheckSwitchContext *blc_ctx;
2805   int c;
2806
2807   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808               "ATS has decided on an address for peer %s\n",
2809               GNUNET_i2s (peer));
2810   GNUNET_assert (NULL != address->transport_name);
2811   if (NULL == (n = lookup_neighbour (peer)))
2812   {
2813     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814                 "Peer %s is unknown, suggestion ignored\n",
2815                 GNUNET_i2s (peer));
2816     return;
2817   }
2818
2819   /* Check if plugin is available */
2820   if (NULL == (papi = GST_plugins_find (address->transport_name)))
2821   {
2822     /* we don't have the plugin for this address */
2823     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824                 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2825                 address->transport_name,
2826                 GNUNET_i2s (peer));
2827     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2828     return;
2829   }
2830   if ((NULL == session) &&
2831       (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2832   {
2833     /* This is a inbound address and we do not have a session to use! */
2834     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2835                 "Inbound address without session `%s'! Destroying address...\n",
2836                 GST_plugins_a2s (address));
2837     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2838     return;
2839   }
2840
2841   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2842     "ATS tells us to switch to %s address '%s' session %p for "
2843     "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2844     GNUNET_HELLO_address_check_option (address,
2845         GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2846     GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2847     GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2848     ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2849
2850   /* Perform blacklist check */
2851   blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2852   blc_ctx->address = GNUNET_HELLO_address_copy (address);
2853   blc_ctx->session = session;
2854   blc_ctx->bandwidth_in = bandwidth_in;
2855   blc_ctx->bandwidth_out = bandwidth_out;
2856   blc_ctx->ats_count = ats_count;
2857   blc_ctx->ats = NULL;
2858   if (ats_count > 0)
2859   {
2860     blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2861     for (c = 0; c < ats_count; c++)
2862     {
2863       blc_ctx->ats[c].type = ats[c].type;
2864       blc_ctx->ats[c].value = ats[c].value;
2865     }
2866   }
2867
2868   GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2869   if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2870       &switch_address_bl_check_cont, blc_ctx)))
2871   {
2872     blc_ctx->blc = blc;
2873   }
2874 }
2875
2876
2877 static int
2878 send_utilization_data (void *cls,
2879                        const struct GNUNET_PeerIdentity *key,
2880                        void *value)
2881 {
2882   struct NeighbourMapEntry *n = value;
2883   struct GNUNET_ATS_Information atsi[4];
2884   uint32_t bps_pl_in;
2885   uint32_t bps_pl_out;
2886   uint32_t bps_in;
2887   uint32_t bps_out;
2888   struct GNUNET_TIME_Relative delta;
2889
2890   delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2891                                                GNUNET_TIME_absolute_get ());
2892
2893   bps_pl_in = 0;
2894
2895   if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2896     bps_pl_in =  (1000LL * 1000LL *  n->util_payload_bytes_recv) / (delta.rel_value_us);
2897   bps_pl_out = 0;
2898   if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2899     bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2900   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901               "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2902               GNUNET_i2s (key),
2903               bps_pl_in,
2904               bps_pl_out);
2905   bps_in = 0;
2906   if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2907     bps_in =  (1000LL * 1000LL *  n->util_total_bytes_recv) / (delta.rel_value_us);
2908   bps_out = 0;
2909   if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2910     bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2911
2912
2913   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914               "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2915               GNUNET_i2s (key),
2916               bps_in,
2917               bps_out);
2918   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2919   atsi[0].value = htonl (bps_out);
2920   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2921   atsi[1].value = htonl (bps_in);
2922
2923   atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2924   atsi[2].value = htonl (bps_pl_out);
2925   atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2926   atsi[3].value = htonl (bps_pl_in);
2927
2928   GST_ats_update_metrics (key, n->primary_address.address,
2929       n->primary_address.session, atsi, 4);
2930   n->util_payload_bytes_recv = 0;
2931   n->util_payload_bytes_sent = 0;
2932   n->util_total_bytes_recv = 0;
2933   n->util_total_bytes_sent = 0;
2934   n->last_util_transmission = GNUNET_TIME_absolute_get();
2935   return GNUNET_OK;
2936 }
2937
2938
2939 /**
2940  * Task transmitting utilization in a regular interval
2941  *
2942  * @param cls the 'struct NeighbourMapEntry' for which we are running
2943  * @param tc scheduler context (unused)
2944  */
2945 static void
2946 utilization_transmission (void *cls,
2947                           const struct GNUNET_SCHEDULER_TaskContext *tc)
2948 {
2949   util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2950
2951   if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2952     GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2953
2954   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2955       utilization_transmission, NULL);
2956
2957 }
2958
2959
2960 void
2961 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2962                                  const struct GNUNET_HELLO_Address *address,
2963                                  struct Session *session,
2964                                  const struct GNUNET_MessageHeader *message)
2965 {
2966   struct NeighbourMapEntry *n;
2967
2968   n = lookup_neighbour (peer);
2969   if (NULL == n)
2970     return;
2971   n->util_total_bytes_recv += ntohs(message->size);
2972 }
2973
2974
2975 void
2976 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2977                                     const struct GNUNET_HELLO_Address *address,
2978                                     struct Session *session,
2979                                     const struct GNUNET_MessageHeader *message)
2980 {
2981   struct NeighbourMapEntry *n;
2982   n = lookup_neighbour (peer);
2983   if (NULL == n)
2984     return;
2985   n->util_payload_bytes_recv += ntohs(message->size);
2986 }
2987
2988
2989 void
2990 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2991                                  const struct GNUNET_HELLO_Address *address,
2992                                  struct Session *session,
2993                                  size_t size)
2994 {
2995   struct NeighbourMapEntry *n;
2996   n = lookup_neighbour (peer);
2997   if (NULL == n)
2998       return;
2999   if (n->primary_address.session != session)
3000     return;
3001   n->util_total_bytes_sent += size;
3002 }
3003
3004
3005 void
3006 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3007                                     size_t size)
3008 {
3009   struct NeighbourMapEntry *n;
3010   n = lookup_neighbour (peer);
3011   if (NULL == n)
3012     return;
3013   n->util_payload_bytes_sent += size;
3014 }
3015
3016
3017 /**
3018  * Master task run for every neighbour.  Performs all of the time-related
3019  * activities (keep alive, send next message, disconnect if idle, finish
3020  * clean up after disconnect).
3021  *
3022  * @param cls the 'struct NeighbourMapEntry' for which we are running
3023  * @param tc scheduler context (unused)
3024  */
3025 static void
3026 master_task (void *cls,
3027              const struct GNUNET_SCHEDULER_TaskContext *tc)
3028 {
3029   struct NeighbourMapEntry *n = cls;
3030   struct GNUNET_TIME_Relative delay;
3031
3032   n->task = GNUNET_SCHEDULER_NO_TASK;
3033   delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3034   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3035               "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3036               GNUNET_i2s (&n->id),
3037               GNUNET_TRANSPORT_ps2s(n->state),
3038               GNUNET_STRINGS_relative_time_to_string (delay,
3039                                                       GNUNET_YES));
3040   switch (n->state)
3041   {
3042   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3043     /* invalid state for master task, clean up */
3044     GNUNET_break (0);
3045     free_neighbour (n, GNUNET_NO);
3046     return;
3047   case GNUNET_TRANSPORT_PS_INIT_ATS:
3048     if (0 == delay.rel_value_us)
3049     {
3050       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3051                   "Connection to `%s' timed out waiting for ATS to provide address\n",
3052                   GNUNET_i2s (&n->id));
3053       free_neighbour (n, GNUNET_NO);
3054       return;
3055     }
3056     break;
3057   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3058     if (0 == delay.rel_value_us)
3059     {
3060       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3061                   "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3062                   GNUNET_i2s (&n->id));
3063       /* We could not send to this address, delete address and session */
3064       if (NULL != n->primary_address.session)
3065         GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3066             n->primary_address.session);
3067       GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3068
3069       /* Remove address and request and additional one */
3070       unset_primary_address (n);
3071       set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3072           GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3073       return;
3074     }
3075     break;
3076   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3077     if (0 == delay.rel_value_us)
3078     {
3079       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3080                   "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3081                   GNUNET_i2s (&n->id));
3082       free_neighbour (n, GNUNET_NO);
3083       return;
3084     }
3085     break;
3086   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3087     if (0 == delay.rel_value_us)
3088     {
3089       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090                   "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3091                   GNUNET_i2s (&n->id));
3092       disconnect_neighbour (n);
3093       return;
3094     }
3095     break;
3096   case GNUNET_TRANSPORT_PS_CONNECTED:
3097     if (0 == delay.rel_value_us)
3098     {
3099       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3100                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3101                   GNUNET_i2s (&n->id));
3102       disconnect_neighbour (n);
3103       return;
3104     }
3105     try_transmission_to_peer (n);
3106     send_keepalive (n);
3107     break;
3108   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3109     if (0 == delay.rel_value_us)
3110     {
3111       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112                   "Connection to `%s' timed out, waiting for ATS replacement address\n",
3113                   GNUNET_i2s (&n->id));
3114       disconnect_neighbour (n);
3115       return;
3116     }
3117     break;
3118   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3119     if (0 == delay.rel_value_us)
3120     {
3121       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3122                   "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3123                   GNUNET_i2s (&n->id));
3124       disconnect_neighbour (n);
3125       return;
3126     }
3127     break;
3128   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3129     if (0 == delay.rel_value_us)
3130     {
3131       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3133                   GNUNET_i2s (&n->id));
3134       disconnect_neighbour (n);
3135       return;
3136     }
3137     try_transmission_to_peer (n);
3138     send_keepalive (n);
3139     break;
3140   case GNUNET_TRANSPORT_PS_DISCONNECT:
3141     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142                 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3143                 GNUNET_i2s (&n->id));
3144     free_neighbour (n, GNUNET_NO);
3145     return;
3146   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3147     /* how did we get here!? */
3148     GNUNET_assert (0);
3149     break;
3150   default:
3151     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3152                 "Unhandled state `%s'\n",
3153                 GNUNET_TRANSPORT_ps2s (n->state));
3154     GNUNET_break (0);
3155     break;
3156   }
3157   if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3158        (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3159   {
3160     /* if we are *now* in one of the two states, we're sending
3161        keep alive messages, so we need to consider the keepalive
3162        delay, not just the connection timeout */
3163     delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3164                                       delay);
3165   }
3166   if (GNUNET_SCHEDULER_NO_TASK == n->task)
3167     n->task = GNUNET_SCHEDULER_add_delayed (delay,
3168                                             &master_task,
3169                                             n);
3170 }
3171
3172
3173 /**
3174  * Send a SESSION_ACK message to the neighbour to confirm that we
3175  * got his CONNECT_ACK.
3176  *
3177  * @param n neighbour to send the SESSION_ACK to
3178  */
3179 static void
3180 send_session_ack_message (struct NeighbourMapEntry *n)
3181 {
3182   struct GNUNET_MessageHeader msg;
3183
3184   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3185   msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3186   (void) send_with_session(n,
3187                            (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3188                            UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3189                            NULL, NULL);
3190 }
3191
3192
3193 /**
3194  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3195  * Consider switching to it.
3196  *
3197  * @param message possibly a 'struct SessionConnectMessage' (check format)
3198  * @param peer identity of the peer to switch the address for
3199  * @param address address of the other peer, NULL if other peer
3200  *                       connected to us
3201  * @param session session to use (or NULL)
3202  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3203  */
3204 int
3205 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3206                                    const struct GNUNET_PeerIdentity *peer,
3207                                    const struct GNUNET_HELLO_Address *address,
3208                                    struct Session *session)
3209 {
3210   const struct SessionConnectMessage *scm;
3211   struct GNUNET_TIME_Absolute ts;
3212   struct NeighbourMapEntry *n;
3213
3214   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3215               "Received CONNECT_ACK message from peer `%s'\n",
3216               GNUNET_i2s (peer));
3217
3218   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3219   {
3220     GNUNET_break_op (0);
3221     return GNUNET_SYSERR;
3222   }
3223   GNUNET_STATISTICS_update (GST_stats,
3224                             gettext_noop
3225                             ("# CONNECT_ACK messages received"),
3226                             1, GNUNET_NO);
3227   scm = (const struct SessionConnectMessage *) message;
3228   GNUNET_break_op (ntohl (scm->reserved) == 0);
3229   if (NULL == (n = lookup_neighbour (peer)))
3230   {
3231     GNUNET_STATISTICS_update (GST_stats,
3232                               gettext_noop
3233                               ("# unexpected CONNECT_ACK messages (no peer)"),
3234                               1, GNUNET_NO);
3235     return GNUNET_SYSERR;
3236   }
3237   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3238   switch (n->state)
3239   {
3240   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3241     GNUNET_break (0);
3242     free_neighbour (n, GNUNET_NO);
3243     return GNUNET_SYSERR;
3244   case GNUNET_TRANSPORT_PS_INIT_ATS:
3245     GNUNET_STATISTICS_update (GST_stats,
3246                               gettext_noop
3247                               ("# unexpected CONNECT_ACK messages (not ready)"),
3248                               1, GNUNET_NO);
3249     break;
3250   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3251     if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3252     {
3253       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3254                   "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3255       return GNUNET_OK;
3256     }
3257     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3258         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3259     GNUNET_STATISTICS_set (GST_stats,
3260                            gettext_noop ("# peers connected"),
3261                            ++neighbours_connected,
3262                            GNUNET_NO);
3263     connect_notify_cb (callback_cls, &n->id,
3264                        n->primary_address.bandwidth_in,
3265                        n->primary_address.bandwidth_out);
3266     /* Tell ATS that the outbound session we created to send CONNECT was successful */
3267     GST_ats_add_address (n->primary_address.address,
3268                          n->primary_address.session,
3269                          NULL, 0);
3270     set_primary_address (n,
3271                  n->primary_address.address,
3272                  n->primary_address.session,
3273                  n->primary_address.bandwidth_in,
3274                  n->primary_address.bandwidth_out,
3275                  GNUNET_YES);
3276     send_session_ack_message (n);
3277     break;
3278   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3279   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3280     GNUNET_STATISTICS_update (GST_stats,
3281                               gettext_noop
3282                               ("# unexpected CONNECT_ACK messages (not ready)"),
3283                               1, GNUNET_NO);
3284     break;
3285   case GNUNET_TRANSPORT_PS_CONNECTED:
3286     /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3287     send_session_ack_message (n);
3288     break;
3289   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3290     /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3291        to give us a new address... */
3292     GNUNET_STATISTICS_update (GST_stats,
3293                               gettext_noop
3294                               ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3295                               1, GNUNET_NO);
3296     break;
3297   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3298     /* new address worked; go back to connected! */
3299     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3300         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3301     send_session_ack_message (n);
3302     break;
3303   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3304     /* new address worked; adopt it and go back to connected! */
3305     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3306         GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3307     GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3308
3309     GST_ats_add_address (n->alternative_address.address,
3310                          n->alternative_address.session,
3311                          NULL, 0);
3312     set_primary_address (n, n->alternative_address.address,
3313         n->alternative_address.session, n->alternative_address.bandwidth_in,
3314         n->alternative_address.bandwidth_out, GNUNET_YES);
3315
3316     free_address (&n->alternative_address);
3317     send_session_ack_message (n);
3318     break;
3319   case GNUNET_TRANSPORT_PS_DISCONNECT:
3320     GNUNET_STATISTICS_update (GST_stats,
3321                               gettext_noop
3322                               ("# unexpected CONNECT_ACK messages (disconnecting)"),
3323                               1, GNUNET_NO);
3324     return GNUNET_SYSERR;
3325   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3326     GNUNET_assert (0);
3327     break;
3328   default:
3329     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3330                 "Unhandled state `%s'\n",
3331                 GNUNET_TRANSPORT_ps2s (n->state));
3332     GNUNET_break (0);
3333     return GNUNET_SYSERR;
3334   }
3335   return GNUNET_OK;
3336 }
3337
3338
3339 /**
3340  * A session was terminated. Take note; if needed, try to get
3341  * an alternative address from ATS.
3342  *
3343  * @param peer identity of the peer where the session died
3344  * @param session session that is gone
3345  * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3346  *        this session was not in use
3347  */
3348 int
3349 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3350                                    struct Session *session)
3351 {
3352   struct NeighbourMapEntry *n;
3353   struct BlackListCheckContext *bcc;
3354   struct BlackListCheckContext *bcc_next;
3355
3356   /* make sure to cancel all ongoing blacklist checks involving 'session' */
3357   bcc_next = bc_head;
3358   while (NULL != (bcc = bcc_next))
3359   {
3360     bcc_next = bcc->next;
3361     if (bcc->na.session == session)
3362     {
3363       if (NULL != bcc->bc)
3364         GST_blacklist_test_cancel (bcc->bc);
3365       GNUNET_HELLO_address_free (bcc->na.address);
3366       GNUNET_CONTAINER_DLL_remove (bc_head,
3367                                    bc_tail,
3368                                    bcc);
3369       GNUNET_free (bcc);
3370     }
3371   }
3372   if (NULL == (n = lookup_neighbour (peer)))
3373     return GNUNET_NO; /* can't affect us */
3374   if (session != n->primary_address.session)
3375   {
3376     if (session == n->alternative_address.session)
3377     {
3378       if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3379         set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3380       else
3381         GNUNET_break (0);
3382       free_address (&n->alternative_address);
3383     }
3384     return GNUNET_NO; /* doesn't affect us further */
3385   }
3386
3387   n->expect_latency_response = GNUNET_NO;
3388   /* The session for neighbour's primary address died */
3389   switch (n->state)
3390   {
3391   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3392     GNUNET_break (0);
3393     free_neighbour (n, GNUNET_NO);
3394     return GNUNET_YES;
3395   case GNUNET_TRANSPORT_PS_INIT_ATS:
3396     GNUNET_break (0);
3397     free_neighbour (n, GNUNET_NO);
3398     return GNUNET_YES;
3399   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3400     /* The session used to send the CONNECT terminated:
3401      * this implies a connect error*/
3402     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3403                 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3404                 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3405                 GNUNET_i2s (peer));
3406     GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3407     unset_primary_address (n);
3408     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3409         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3410     break;
3411   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3412   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3413     /* error on inbound session; free neighbour entirely */
3414     free_address (&n->primary_address);
3415     free_neighbour (n, GNUNET_NO);
3416     return GNUNET_YES;
3417   case GNUNET_TRANSPORT_PS_CONNECTED:
3418     unset_primary_address (n);
3419     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3420         GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3421     break;
3422   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3423     /* we don't have an address, how can it go down? */
3424     GNUNET_break (0);
3425     break;
3426   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3427     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3428     break;
3429   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3430     /* primary went down while we were waiting for CONNECT_ACK on secondary;
3431        secondary as primary */
3432     GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3433     free_address (&n->primary_address);
3434     n->primary_address = n->alternative_address;
3435     memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3436     set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3437     break;
3438   case GNUNET_TRANSPORT_PS_DISCONNECT:
3439     free_address (&n->primary_address);
3440     break;
3441   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3442     /* neighbour was freed and plugins told to terminate session */
3443     return GNUNET_NO;
3444     break;
3445   default:
3446     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3447                 "Unhandled state `%s'\n",
3448                 GNUNET_TRANSPORT_ps2s (n->state));
3449     GNUNET_break (0);
3450     break;
3451   }
3452   if (GNUNET_SCHEDULER_NO_TASK != n->task)
3453     GNUNET_SCHEDULER_cancel (n->task);
3454   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3455   return GNUNET_YES;
3456 }
3457
3458
3459 /**
3460  * We received a 'SESSION_ACK' message from the other peer.
3461  * If we sent a 'CONNECT_ACK' last, this means we are now
3462  * connected.  Otherwise, do nothing.
3463  *
3464  * @param message possibly a 'struct SessionConnectMessage' (check format)
3465  * @param peer identity of the peer to switch the address for
3466  * @param address address of the other peer, NULL if other peer
3467  *                       connected to us
3468  * @param session session to use (or NULL)
3469  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3470  */
3471 int
3472 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3473                                    const struct GNUNET_PeerIdentity *peer,
3474                                    const struct GNUNET_HELLO_Address *address,
3475                                    struct Session *session)
3476 {
3477   struct NeighbourMapEntry *n;
3478
3479   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3480               "Received SESSION_ACK message from peer `%s'\n",
3481               GNUNET_i2s (peer));
3482   if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3483   {
3484     GNUNET_break_op (0);
3485     return GNUNET_SYSERR;
3486   }
3487   GNUNET_STATISTICS_update (GST_stats,
3488                             gettext_noop
3489                             ("# SESSION_ACK messages received"),
3490                             1, GNUNET_NO);
3491   if (NULL == (n = lookup_neighbour (peer)))
3492   {
3493     GNUNET_break_op (0);
3494     return GNUNET_SYSERR;
3495   }
3496   /* check if we are in a plausible state for having sent
3497      a CONNECT_ACK.  If not, return, otherwise break */
3498
3499   /* TODO I have no idea we we should state GNUNET_TRANSPORT_PS_CONNECT_SENT
3500    * Perhaps SWITCHING? Have to check */
3501   if ( /*(*/ (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) /* &&
3502          (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3503        (ACK_SEND_SESSION_ACK != n->ack_state) */)
3504   {
3505     if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
3506       GNUNET_break (0); /* TESTING */
3507
3508     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3509                 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3510                 GNUNET_i2s (peer),
3511                 GNUNET_TRANSPORT_ps2s (n->state),
3512                 print_ack_state (n->ack_state));
3513     GNUNET_STATISTICS_update (GST_stats,
3514                               gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3515                               GNUNET_NO);
3516     return GNUNET_OK;
3517   }
3518   /* We are connected */
3519   set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3520   GNUNET_STATISTICS_set (GST_stats,
3521                          gettext_noop ("# peers connected"),
3522                          ++neighbours_connected,
3523                          GNUNET_NO);
3524
3525   /* Notify about connection */
3526   connect_notify_cb (callback_cls, &n->id,
3527                      n->primary_address.bandwidth_in,
3528                      n->primary_address.bandwidth_out);
3529
3530   /* Add session to ATS since no session was given (NULL) and we may have
3531    * obtained a new session */
3532   GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3533       NULL, 0);
3534
3535   /* Set primary address to used */
3536   set_primary_address (n,
3537                n->primary_address.address,
3538                n->primary_address.session,
3539                n->primary_address.bandwidth_in,
3540                n->primary_address.bandwidth_out,
3541                GNUNET_YES);
3542   return GNUNET_OK;
3543 }
3544
3545
3546 /**
3547  * Test if we're connected to the given peer.
3548  *
3549  * @param target peer to test
3550  * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3551  */
3552 int
3553 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3554 {
3555   return test_connected (lookup_neighbour (target));
3556 }
3557
3558 /**
3559  * Change the incoming quota for the given peer.
3560  *
3561  * @param neighbour identity of peer to change qutoa for
3562  * @param quota new quota
3563  */
3564 void
3565 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3566                                    struct GNUNET_BANDWIDTH_Value32NBO quota)
3567 {
3568   struct NeighbourMapEntry *n;
3569
3570   if (NULL == (n = lookup_neighbour (neighbour)))
3571   {
3572     GNUNET_STATISTICS_update (GST_stats,
3573                               gettext_noop
3574                               ("# SET QUOTA messages ignored (no such peer)"),
3575                               1, GNUNET_NO);
3576     return;
3577   }
3578   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579               "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3580               ntohl (quota.value__), GNUNET_i2s (&n->id));
3581   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3582   if (0 != ntohl (quota.value__))
3583     return;
3584   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3585               "Disconnecting peer `%4s' due to SET_QUOTA\n",
3586               GNUNET_i2s (&n->id));
3587   if (GNUNET_YES == test_connected (n))
3588     GNUNET_STATISTICS_update (GST_stats,
3589                               gettext_noop ("# disconnects due to quota of 0"),
3590                               1, GNUNET_NO);
3591   disconnect_neighbour (n);
3592 }
3593
3594
3595 /**
3596  * We received a disconnect message from the given peer,
3597  * validate and process.
3598  *
3599  * @param peer sender of the message
3600  * @param msg the disconnect message
3601  */
3602 void
3603 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3604                                           const struct GNUNET_MessageHeader *msg)
3605 {
3606   struct NeighbourMapEntry *n;
3607   const struct SessionDisconnectMessage *sdm;
3608
3609   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3610               "Received DISCONNECT message from peer `%s'\n",
3611               GNUNET_i2s (peer));
3612   if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3613   {
3614     // GNUNET_break_op (0);
3615     GNUNET_STATISTICS_update (GST_stats,
3616                               gettext_noop
3617                               ("# disconnect messages ignored (old format)"), 1,
3618                               GNUNET_NO);
3619     return;
3620   }
3621   GNUNET_STATISTICS_update (GST_stats,
3622                             gettext_noop
3623                             ("# DISCONNECT messages received"),
3624                             1, GNUNET_NO);
3625   sdm = (const struct SessionDisconnectMessage *) msg;
3626   if (NULL == (n = lookup_neighbour (peer)))
3627     return;                     /* gone already */
3628   if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3629   {
3630     GNUNET_STATISTICS_update (GST_stats,
3631                               gettext_noop
3632                               ("# disconnect messages ignored (timestamp)"), 1,
3633                               GNUNET_NO);
3634     return;
3635   }
3636   if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3637   {
3638     GNUNET_break_op (0);
3639     return;
3640   }
3641   if (ntohl (sdm->purpose.size) !=
3642       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3643       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3644       sizeof (struct GNUNET_TIME_AbsoluteNBO))
3645   {
3646     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3647                 "%s message from peer `%s' has invalid size \n",
3648                 "DISCONNECT",
3649                 GNUNET_i2s (peer));
3650     GNUNET_break_op (0);
3651     return;
3652   }
3653   if (GNUNET_OK !=
3654       GNUNET_CRYPTO_eddsa_verify
3655       (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3656        &sdm->signature, &sdm->public_key))
3657   {
3658     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3659                 "%s message from peer `%s' cannot be verified \n",
3660                 "DISCONNECT",
3661                 GNUNET_i2s (peer));
3662     GNUNET_break_op (0);
3663     return;
3664   }
3665   if (GNUNET_YES == test_connected (n))
3666     GNUNET_STATISTICS_update (GST_stats,
3667                               gettext_noop
3668                               ("# other peer asked to disconnect from us"), 1,
3669                               GNUNET_NO);
3670   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671               "Disconnecting by request from peer %s\n",
3672               GNUNET_i2s (peer));
3673   disconnect_neighbour (n);
3674 }
3675
3676
3677 /**
3678  * Closure for the neighbours_iterate function.
3679  */
3680 struct IteratorContext
3681 {
3682   /**
3683    * Function to call on each connected neighbour.
3684    */
3685   GST_NeighbourIterator cb;
3686
3687   /**
3688    * Closure for 'cb'.
3689    */
3690   void *cb_cls;
3691 };
3692
3693
3694 /**
3695  * Call the callback from the closure for each neighbour.
3696  *
3697  * @param cls the `struct IteratorContext`
3698  * @param key the hash of the public key of the neighbour
3699  * @param value the `struct NeighbourMapEntry`
3700  * @return #GNUNET_OK (continue to iterate)
3701  */
3702 static int
3703 neighbours_iterate (void *cls,
3704                     const struct GNUNET_PeerIdentity *key,
3705                     void *value)
3706 {
3707   struct IteratorContext *ic = cls;
3708   struct NeighbourMapEntry *n = value;
3709   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3710   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3711
3712
3713   if (NULL != n->primary_address.address)
3714   {
3715     bandwidth_in = n->primary_address.bandwidth_in;
3716     bandwidth_out = n->primary_address.bandwidth_out;
3717   }
3718   else
3719   {
3720     bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3721     bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3722   }
3723   ic->cb (ic->cb_cls,
3724           &n->id,
3725           n->primary_address.address,
3726           n->state,
3727           n->timeout,
3728           bandwidth_in, bandwidth_out);
3729   return GNUNET_OK;
3730 }
3731
3732
3733 /**
3734  * Iterate over all connected neighbours.
3735  *
3736  * @param cb function to call
3737  * @param cb_cls closure for cb
3738  */
3739 void
3740 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3741 {
3742   struct IteratorContext ic;
3743
3744   if (NULL == neighbours)
3745     return; /* can happen during shutdown */
3746   ic.cb = cb;
3747   ic.cb_cls = cb_cls;
3748   GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3749 }
3750
3751
3752 /**
3753  * If we have an active connection to the given target, it must be shutdown.
3754  *
3755  * @param target peer to disconnect from
3756  */
3757 void
3758 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3759 {
3760   struct NeighbourMapEntry *n;
3761
3762   if (NULL == (n = lookup_neighbour (target)))
3763     return;  /* not active */
3764   if (GNUNET_YES == test_connected (n))
3765     GNUNET_STATISTICS_update (GST_stats,
3766                               gettext_noop
3767                               ("# disconnected from peer upon explicit request"), 1,
3768                               GNUNET_NO);
3769   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3770               "Forced disconnect from peer %s\n",
3771               GNUNET_i2s (target));
3772   disconnect_neighbour (n);
3773 }
3774
3775
3776 /**
3777  * Obtain current latency information for the given neighbour.
3778  *
3779  * @param peer to get the latency for
3780  * @return observed latency of the address, FOREVER if the
3781  *         the connection is not up
3782  */
3783 struct GNUNET_TIME_Relative
3784 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3785 {
3786   struct NeighbourMapEntry *n;
3787
3788   n = lookup_neighbour (peer);
3789   if (NULL == n)
3790     return GNUNET_TIME_UNIT_FOREVER_REL;
3791   switch (n->state)
3792   {
3793   case GNUNET_TRANSPORT_PS_CONNECTED:
3794   case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3795   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3796   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3797     return n->latency;
3798   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3799   case GNUNET_TRANSPORT_PS_INIT_ATS:
3800   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3801   case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3802   case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3803   case GNUNET_TRANSPORT_PS_DISCONNECT:
3804   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3805     return GNUNET_TIME_UNIT_FOREVER_REL;
3806   default:
3807     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3808                 "Unhandled state `%s'\n",
3809                 GNUNET_TRANSPORT_ps2s (n->state));
3810     GNUNET_break (0);
3811     break;
3812   }
3813   return GNUNET_TIME_UNIT_FOREVER_REL;
3814 }
3815
3816
3817 /**
3818  * Obtain current address information for the given neighbour.
3819  *
3820  * @param peer
3821  * @return address currently used
3822  */
3823 struct GNUNET_HELLO_Address *
3824 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3825 {
3826   struct NeighbourMapEntry *n;
3827
3828   n = lookup_neighbour (peer);
3829   if (NULL == n)
3830     return NULL;
3831   return n->primary_address.address;
3832 }
3833
3834
3835 /**
3836  * Initialize the neighbours subsystem.
3837  *
3838  * @param cls closure for callbacks
3839  * @param connect_cb function to call if we connect to a peer
3840  * @param disconnect_cb function to call if we disconnect from a peer
3841  * @param peer_address_cb function to call if we change an active address
3842  *                   of a neighbour
3843  * @param max_fds maximum number of fds to use
3844  */
3845 void
3846 GST_neighbours_start (void *cls,
3847                       NotifyConnect connect_cb,
3848                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3849                       GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3850                       unsigned int max_fds)
3851 {
3852   callback_cls = cls;
3853   connect_notify_cb = connect_cb;
3854   disconnect_notify_cb = disconnect_cb;
3855   neighbour_change_cb = peer_address_cb;
3856   neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3857   registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3858   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3859       utilization_transmission, NULL);
3860 }
3861
3862
3863 /**
3864  * Disconnect from the given neighbour.
3865  *
3866  * @param cls unused
3867  * @param key hash of neighbour's public key (not used)
3868  * @param value the 'struct NeighbourMapEntry' of the neighbour
3869  * @return #GNUNET_OK (continue to iterate)
3870  */
3871 static int
3872 disconnect_all_neighbours (void *cls,
3873                            const struct GNUNET_PeerIdentity *key,
3874                            void *value)
3875 {
3876   struct NeighbourMapEntry *n = value;
3877
3878   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879               "Disconnecting peer `%4s', %s\n",
3880               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3881   free_neighbour (n, GNUNET_NO);
3882   return GNUNET_OK;
3883 }
3884
3885
3886 /**
3887  * Cleanup the neighbours subsystem.
3888  */
3889 void
3890 GST_neighbours_stop ()
3891 {
3892   struct BlacklistCheckSwitchContext *cur;
3893   struct BlacklistCheckSwitchContext *next;
3894
3895   if (NULL == neighbours)
3896     return;
3897   if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3898   {
3899     GNUNET_SCHEDULER_cancel (util_transmission_tk);
3900     util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3901   }
3902
3903   GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3904                                          &disconnect_all_neighbours,
3905                                          NULL);
3906   GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3907
3908   next = pending_bc_head;
3909   for (cur = next; NULL != cur; cur = next )
3910   {
3911     next = cur->next;
3912     GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3913
3914     if (NULL != cur->blc)
3915     {
3916       GST_blacklist_test_cancel (cur->blc);
3917       cur->blc = NULL;
3918     }
3919     if (NULL != cur->address)
3920       GNUNET_HELLO_address_free (cur->address);
3921     GNUNET_free_non_null (cur->ats);
3922     GNUNET_free (cur);
3923   }
3924
3925   GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3926       &free_notification_cb, NULL);
3927   GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3928   registered_quota_notifications = NULL;
3929
3930   neighbours = NULL;
3931   callback_cls = NULL;
3932   connect_notify_cb = NULL;
3933   disconnect_notify_cb = NULL;
3934   neighbour_change_cb = NULL;
3935 }
3936
3937
3938 /* end of file gnunet-service-transport_neighbours.c */