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