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