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