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