use enum GNUNET_ATS_Network_Type instead of uint32_t where appropriate
[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, int keep_sessions)
881 {
882   struct MessageQueue *mq;
883   struct GNUNET_TRANSPORT_PluginFunctions *papi;
884   struct GNUNET_HELLO_Address *backup_primary;
885
886   n->is_active = NULL; /* always free'd by its own continuation! */
887
888   /* fail messages currently in the queue */
889   while (NULL != (mq = n->messages_head))
890   {
891     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
892     if (NULL != mq->cont)
893       mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
894     GNUNET_free (mq);
895   }
896   /* It is too late to send other peer disconnect notifications, but at
897      least internally we need to get clean... */
898   if (GNUNET_YES == test_connected (n))
899   {
900     GNUNET_STATISTICS_set (GST_stats,
901                            gettext_noop ("# peers connected"),
902                            --neighbours_connected,
903                            GNUNET_NO);
904     disconnect_notify_cb (callback_cls, &n->id);
905   }
906   n->state = S_DISCONNECT_FINISHED;
907
908   if (NULL != n->primary_address.address)
909   {
910     backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
911   }
912   else
913     backup_primary = NULL;
914
915   /* free addresses and mark as unused */
916   free_address (&n->primary_address);
917   free_address (&n->alternative_address);
918
919   /* FIXME: Note that if we are switching between two TCP sessions to
920      the same peer, we might want to selectively kill only one of
921      them! Killing all sessions like this seems to be very, very
922      wrong. */
923
924   /* cut transport-level connection */
925   if ((GNUNET_NO == keep_sessions) &&
926       (NULL != backup_primary) &&
927       (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
928     papi->disconnect_peer (papi->cls, &n->id);
929
930   GNUNET_free_non_null (backup_primary);
931
932   GNUNET_assert (GNUNET_YES ==
933                  GNUNET_CONTAINER_multipeermap_remove (neighbours,
934                                                        &n->id, n));
935
936   // FIXME-ATS-API: we might want to be more specific about
937   // which states we do this from in the future (ATS should
938   // have given us a 'suggest_address' handle, and if we have
939   // such a handle, we should cancel the operation here!
940   if (NULL != n->suggest_handle)
941   {
942     GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
943     n->suggest_handle = NULL;
944   }
945
946   if (GNUNET_SCHEDULER_NO_TASK != n->task)
947   {
948     GNUNET_SCHEDULER_cancel (n->task);
949     n->task = GNUNET_SCHEDULER_NO_TASK;
950   }
951   /* free rest of memory */
952   GNUNET_free (n);
953 }
954
955
956 /**
957  * Transmit a message using the current session of the given
958  * neighbour.
959  *
960  * @param n entry for the recipient
961  * @param msgbuf buffer to transmit
962  * @param msgbuf_size number of bytes in buffer
963  * @param priority transmission priority
964  * @param timeout transmission timeout
965  * @param cont continuation to call when finished (can be NULL)
966  * @param cont_cls closure for cont
967  */
968 static void
969 send_with_session (struct NeighbourMapEntry *n,
970                    const char *msgbuf, size_t msgbuf_size,
971                    uint32_t priority,
972                    struct GNUNET_TIME_Relative timeout,
973                    GNUNET_TRANSPORT_TransmitContinuation cont,
974                    void *cont_cls)
975 {
976   struct GNUNET_TRANSPORT_PluginFunctions *papi;
977
978   GNUNET_assert (n->primary_address.session != NULL);
979   if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
980          (-1 == papi->send (papi->cls,
981                             n->primary_address.session,
982                             msgbuf, msgbuf_size,
983                             priority,
984                             timeout,
985                             cont, cont_cls)))) &&
986        (NULL != cont))
987     cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
988   GST_neighbours_notify_data_sent (&n->id,
989       n->primary_address.address, n->primary_address.session, msgbuf_size);
990   GNUNET_break (NULL != papi);
991 }
992
993
994 /**
995  * Master task run for every neighbour.  Performs all of the time-related
996  * activities (keep alive, send next message, disconnect if idle, finish
997  * clean up after disconnect).
998  *
999  * @param cls the 'struct NeighbourMapEntry' for which we are running
1000  * @param tc scheduler context (unused)
1001  */
1002 static void
1003 master_task (void *cls,
1004              const struct GNUNET_SCHEDULER_TaskContext *tc);
1005
1006
1007 /**
1008  * Function called when the 'DISCONNECT' message has been sent by the
1009  * plugin.  Frees the neighbour --- if the entry still exists.
1010  *
1011  * @param cls NULL
1012  * @param target identity of the neighbour that was disconnected
1013  * @param result #GNUNET_OK if the disconnect got out successfully
1014  * @param payload bytes payload
1015  * @param physical bytes physical
1016  */
1017 static void
1018 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1019                       int result, size_t payload, size_t physical)
1020 {
1021   struct NeighbourMapEntry *n;
1022
1023   n = lookup_neighbour (target);
1024   if (NULL == n)
1025     return; /* already gone */
1026   if (S_DISCONNECT != n->state)
1027     return; /* have created a fresh entry since */
1028   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1029     GNUNET_SCHEDULER_cancel (n->task);
1030   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1031 }
1032
1033
1034 /**
1035  * Transmit a DISCONNECT message to the other peer.
1036  *
1037  * @param n neighbour to send DISCONNECT message.
1038  */
1039 static void
1040 send_disconnect (struct NeighbourMapEntry *n)
1041 {
1042   struct SessionDisconnectMessage disconnect_msg;
1043
1044   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045               "Sending DISCONNECT message to peer `%4s'\n",
1046               GNUNET_i2s (&n->id));
1047   disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1048   disconnect_msg.header.type =
1049       htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1050   disconnect_msg.reserved = htonl (0);
1051   disconnect_msg.purpose.size =
1052       htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1053              sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1054              sizeof (struct GNUNET_TIME_AbsoluteNBO));
1055   disconnect_msg.purpose.purpose =
1056       htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1057   disconnect_msg.timestamp =
1058       GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1059   disconnect_msg.public_key = GST_my_identity.public_key;
1060   GNUNET_assert (GNUNET_OK ==
1061                  GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1062                                          &disconnect_msg.purpose,
1063                                          &disconnect_msg.signature));
1064
1065   send_with_session (n,
1066                      (const char *) &disconnect_msg, sizeof (disconnect_msg),
1067                      UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1068                      &send_disconnect_cont, NULL);
1069   GNUNET_STATISTICS_update (GST_stats,
1070                             gettext_noop
1071                             ("# DISCONNECT messages sent"), 1,
1072                             GNUNET_NO);
1073 }
1074
1075
1076 /**
1077  * Disconnect from the given neighbour, clean up the record.
1078  *
1079  * @param n neighbour to disconnect from
1080  */
1081 static void
1082 disconnect_neighbour (struct NeighbourMapEntry *n)
1083 {
1084   /* depending on state, notify neighbour and/or upper layers of this peer
1085      about disconnect */
1086   switch (n->state)
1087   {
1088   case S_NOT_CONNECTED:
1089   case S_INIT_ATS:
1090   case S_INIT_BLACKLIST:
1091     /* other peer is completely unaware of us, no need to send DISCONNECT */
1092     n->state = S_DISCONNECT_FINISHED;
1093     free_neighbour (n, GNUNET_NO);
1094     return;
1095   case S_CONNECT_SENT:
1096     send_disconnect (n);
1097     n->state = S_DISCONNECT;
1098     break;
1099   case S_CONNECT_RECV_BLACKLIST_INBOUND:
1100   case S_CONNECT_RECV_ATS:
1101   case S_CONNECT_RECV_BLACKLIST:
1102     /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1103     n->state = S_DISCONNECT_FINISHED;
1104     free_neighbour (n, GNUNET_NO);
1105     return;
1106   case S_CONNECT_RECV_ACK:
1107     /* we DID ACK the other peer's request, must send DISCONNECT */
1108     send_disconnect (n);
1109     n->state = S_DISCONNECT;
1110     break;
1111   case S_CONNECTED:
1112   case S_RECONNECT_BLACKLIST:
1113   case S_RECONNECT_SENT:
1114   case S_CONNECTED_SWITCHING_BLACKLIST:
1115   case S_CONNECTED_SWITCHING_CONNECT_SENT:
1116     /* we are currently connected, need to send disconnect and do
1117        internal notifications and update statistics */
1118     send_disconnect (n);
1119     GNUNET_STATISTICS_set (GST_stats,
1120                            gettext_noop ("# peers connected"),
1121                            --neighbours_connected,
1122                            GNUNET_NO);
1123     disconnect_notify_cb (callback_cls, &n->id);
1124     n->state = S_DISCONNECT;
1125     break;
1126   case S_RECONNECT_ATS:
1127     /* ATS address request timeout, disconnect without sending disconnect message */
1128     GNUNET_STATISTICS_set (GST_stats,
1129                            gettext_noop ("# peers connected"),
1130                            --neighbours_connected,
1131                            GNUNET_NO);
1132     disconnect_notify_cb (callback_cls, &n->id);
1133     n->state = S_DISCONNECT;
1134     break;
1135   case S_DISCONNECT:
1136     /* already disconnected, ignore */
1137     break;
1138   case S_DISCONNECT_FINISHED:
1139     /* already cleaned up, how did we get here!? */
1140     GNUNET_assert (0);
1141     break;
1142   default:
1143     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1144                 "Unhandled state `%s'\n",
1145                 print_state (n->state));
1146     GNUNET_break (0);
1147     break;
1148   }
1149   /* schedule timeout to clean up */
1150   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1151     GNUNET_SCHEDULER_cancel (n->task);
1152   n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1153                                           &master_task, n);
1154 }
1155
1156
1157 /**
1158  * We're done with our transmission attempt, continue processing.
1159  *
1160  * @param cls the 'struct MessageQueue' of the message
1161  * @param receiver intended receiver
1162  * @param success whether it worked or not
1163  * @param size_payload bytes payload sent
1164  * @param physical bytes sent on wire
1165  */
1166 static void
1167 transmit_send_continuation (void *cls,
1168                             const struct GNUNET_PeerIdentity *receiver,
1169                             int success, size_t size_payload, size_t physical)
1170 {
1171   struct MessageQueue *mq = cls;
1172   struct NeighbourMapEntry *n;
1173
1174   if (NULL == (n = lookup_neighbour (receiver)))
1175   {
1176     GNUNET_free (mq);
1177     return; /* disconnect or other error while transmitting, can happen */
1178   }
1179   if (n->is_active == mq)
1180   {
1181     /* this is still "our" neighbour, remove us from its queue
1182        and allow it to send the next message now */
1183     n->is_active = NULL;
1184     if (GNUNET_SCHEDULER_NO_TASK != n->task)
1185       GNUNET_SCHEDULER_cancel (n->task);
1186     n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1187   }
1188   if (bytes_in_send_queue < mq->message_buf_size)
1189   {
1190       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1191                   "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1192                   bytes_in_send_queue, mq->message_buf_size,
1193                   (GNUNET_OK == success) ? "OK" : "FAIL",
1194                   size_payload, physical);
1195       GNUNET_break (0);
1196   }
1197
1198
1199   GNUNET_break (size_payload == mq->message_buf_size);
1200   bytes_in_send_queue -= mq->message_buf_size;
1201   GNUNET_STATISTICS_set (GST_stats,
1202                         gettext_noop
1203                          ("# bytes in message queue for other peers"),
1204                          bytes_in_send_queue, GNUNET_NO);
1205   if (GNUNET_OK == success)
1206     GNUNET_STATISTICS_update (GST_stats,
1207                               gettext_noop
1208                               ("# messages transmitted to other peers"),
1209                               1, GNUNET_NO);
1210   else
1211     GNUNET_STATISTICS_update (GST_stats,
1212                               gettext_noop
1213                               ("# transmission failures for messages to other peers"),
1214                               1, GNUNET_NO);
1215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216               "Sending message to `%s' of type %u was a %s\n",
1217               GNUNET_i2s (receiver),
1218               ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1219               (success == GNUNET_OK) ? "success" : "FAILURE");
1220   if (NULL != mq->cont)
1221     mq->cont (mq->cont_cls, success, size_payload, physical);
1222   GNUNET_free (mq);
1223 }
1224
1225
1226 /**
1227  * Check the message list for the given neighbour and if we can
1228  * send a message, do so.  This function should only be called
1229  * if the connection is at least generally ready for transmission.
1230  * While we will only send one message at a time, no bandwidth
1231  * quota management is performed here.  If a message was given to
1232  * the plugin, the continuation will automatically re-schedule
1233  * the 'master' task once the next message might be transmitted.
1234  *
1235  * @param n target peer for which to transmit
1236  */
1237 static void
1238 try_transmission_to_peer (struct NeighbourMapEntry *n)
1239 {
1240   struct MessageQueue *mq;
1241   struct GNUNET_TIME_Relative timeout;
1242
1243   if (NULL == n->primary_address.address)
1244   {
1245     /* no address, why are we here? */
1246     GNUNET_break (0);
1247     return;
1248   }
1249   if ((0 == n->primary_address.address->address_length) &&
1250       (NULL == n->primary_address.session))
1251   {
1252     /* no address, why are we here? */
1253     GNUNET_break (0);
1254     return;
1255   }
1256   if (NULL != n->is_active)
1257   {
1258     /* transmission already pending */
1259     return;
1260   }
1261
1262   /* timeout messages from the queue that are past their due date */
1263   while (NULL != (mq = n->messages_head))
1264   {
1265     timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1266     if (timeout.rel_value_us > 0)
1267       break;
1268     GNUNET_STATISTICS_update (GST_stats,
1269                               gettext_noop
1270                               ("# messages timed out while in transport queue"),
1271                               1, GNUNET_NO);
1272     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1273     n->is_active = mq;
1274     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0);     /* timeout */
1275   }
1276   if (NULL == mq)
1277     return;                     /* no more messages */
1278   GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1279   n->is_active = mq;
1280   send_with_session (n,
1281                      mq->message_buf, mq->message_buf_size,
1282                      0 /* priority */, timeout,
1283                      &transmit_send_continuation, mq);
1284 }
1285
1286
1287 /**
1288  * Send keepalive message to the neighbour.  Must only be called
1289  * if we are on 'connected' state or while trying to switch addresses.
1290  * Will internally determine if a keepalive is truly needed (so can
1291  * always be called).
1292  *
1293  * @param n neighbour that went idle and needs a keepalive
1294  */
1295 static void
1296 send_keepalive (struct NeighbourMapEntry *n)
1297 {
1298   struct GNUNET_MessageHeader m;
1299
1300   GNUNET_assert ((S_CONNECTED == n->state) ||
1301                  (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1302                  (S_CONNECTED_SWITCHING_CONNECT_SENT));
1303   if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1304     return; /* no keepalive needed at this time */
1305   m.size = htons (sizeof (struct GNUNET_MessageHeader));
1306   m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1307   send_with_session (n,
1308                      (const void *) &m, sizeof (m),
1309                      UINT32_MAX /* priority */,
1310                      KEEPALIVE_FREQUENCY,
1311                      NULL, NULL);
1312   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1313                             GNUNET_NO);
1314   n->expect_latency_response = GNUNET_YES;
1315   n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1316   n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1317 }
1318
1319
1320 /**
1321  * Keep the connection to the given neighbour alive longer,
1322  * we received a KEEPALIVE (or equivalent); send a response.
1323  *
1324  * @param neighbour neighbour to keep alive (by sending keep alive response)
1325  */
1326 void
1327 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1328 {
1329   struct NeighbourMapEntry *n;
1330   struct GNUNET_MessageHeader m;
1331
1332   if (NULL == (n = lookup_neighbour (neighbour)))
1333   {
1334     GNUNET_STATISTICS_update (GST_stats,
1335                               gettext_noop
1336                               ("# KEEPALIVE messages discarded (peer unknown)"),
1337                               1, GNUNET_NO);
1338     return;
1339   }
1340   if (NULL == n->primary_address.session)
1341   {
1342     GNUNET_STATISTICS_update (GST_stats,
1343                               gettext_noop
1344                               ("# KEEPALIVE messages discarded (no session)"),
1345                               1, GNUNET_NO);
1346     return;
1347   }
1348   /* send reply to allow neighbour to measure latency */
1349   m.size = htons (sizeof (struct GNUNET_MessageHeader));
1350   m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1351   send_with_session(n,
1352                     (const void *) &m, sizeof (m),
1353                     UINT32_MAX /* priority */,
1354                     KEEPALIVE_FREQUENCY,
1355                     NULL, NULL);
1356 }
1357
1358
1359 /**
1360  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1361  * latency to this peer.  Pass the updated information (existing ats
1362  * plus calculated latency) to ATS.
1363  *
1364  * @param neighbour neighbour to keep alive
1365  */
1366 void
1367 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1368 {
1369   struct NeighbourMapEntry *n;
1370   uint32_t latency;
1371   struct GNUNET_ATS_Information ats;
1372
1373   if (NULL == (n = lookup_neighbour (neighbour)))
1374   {
1375     GNUNET_STATISTICS_update (GST_stats,
1376                               gettext_noop
1377                               ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1378                               1, GNUNET_NO);
1379     return;
1380   }
1381   if ( (S_CONNECTED != n->state) ||
1382        (GNUNET_YES != n->expect_latency_response) )
1383   {
1384     GNUNET_STATISTICS_update (GST_stats,
1385                               gettext_noop
1386                               ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1387                               1, GNUNET_NO);
1388     return;
1389   }
1390   n->expect_latency_response = GNUNET_NO;
1391   n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1392   n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394               "Latency for peer `%s' is %s\n",
1395               GNUNET_i2s (&n->id),
1396               GNUNET_STRINGS_relative_time_to_string (n->latency,
1397                                                       GNUNET_YES));
1398   /* append latency */
1399   ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1400   if (n->latency.rel_value_us > UINT32_MAX)
1401     latency = UINT32_MAX;
1402   else
1403     latency = n->latency.rel_value_us;
1404   ats.value = htonl (latency);
1405   GST_ats_update_metrics (&n->id,
1406                                                                                           n->primary_address.address,
1407                                                                                         n->primary_address.session,
1408                                                                                         &ats, 1);
1409 }
1410
1411
1412 /**
1413  * We have received a message from the given sender.  How long should
1414  * we delay before receiving more?  (Also used to keep the peer marked
1415  * as live).
1416  *
1417  * @param sender sender of the message
1418  * @param size size of the message
1419  * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1420  *                   GNUNET_NO if the neighbour is not connected or violates the quota,
1421  *                   GNUNET_SYSERR if the connection is not fully up yet
1422  * @return how long to wait before reading more from this sender
1423  */
1424 struct GNUNET_TIME_Relative
1425 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1426                                         *sender, ssize_t size, int *do_forward)
1427 {
1428   struct NeighbourMapEntry *n;
1429   struct GNUNET_TIME_Relative ret;
1430
1431   if (NULL == neighbours)
1432   {
1433     *do_forward = GNUNET_NO;
1434     return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1435   }
1436   if (NULL == (n = lookup_neighbour (sender)))
1437   {
1438     GST_neighbours_try_connect (sender);
1439     if (NULL == (n = lookup_neighbour (sender)))
1440     {
1441       GNUNET_STATISTICS_update (GST_stats,
1442                                 gettext_noop
1443                                 ("# messages discarded due to lack of neighbour record"),
1444                                 1, GNUNET_NO);
1445       *do_forward = GNUNET_NO;
1446       return GNUNET_TIME_UNIT_ZERO;
1447     }
1448   }
1449   if (! test_connected (n))
1450   {
1451     *do_forward = GNUNET_SYSERR;
1452     return GNUNET_TIME_UNIT_ZERO;
1453   }
1454   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1455   {
1456     n->quota_violation_count++;
1457     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458                 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1459                 n->in_tracker.available_bytes_per_s__,
1460                 n->quota_violation_count);
1461     /* Discount 32k per violation */
1462     GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1463   }
1464   else
1465   {
1466     if (n->quota_violation_count > 0)
1467     {
1468       /* try to add 32k back */
1469       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1470       n->quota_violation_count--;
1471     }
1472   }
1473   if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1474   {
1475     GNUNET_STATISTICS_update (GST_stats,
1476                               gettext_noop
1477                               ("# bandwidth quota violations by other peers"),
1478                               1, GNUNET_NO);
1479     *do_forward = GNUNET_NO;
1480     return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1481   }
1482   *do_forward = GNUNET_YES;
1483   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1484   if (ret.rel_value_us > 0)
1485   {
1486     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487                 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1488                 (unsigned long long) n->in_tracker.
1489                 consumption_since_last_update__,
1490                 (unsigned int) n->in_tracker.available_bytes_per_s__,
1491                 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1492     GNUNET_STATISTICS_update (GST_stats,
1493                               gettext_noop ("# ms throttling suggested"),
1494                               (int64_t) ret.rel_value_us / 1000LL,
1495                               GNUNET_NO);
1496   }
1497   return ret;
1498 }
1499
1500
1501 /**
1502  * Transmit a message to the given target using the active connection.
1503  *
1504  * @param target destination
1505  * @param msg message to send
1506  * @param msg_size number of bytes in msg
1507  * @param timeout when to fail with timeout
1508  * @param cont function to call when done
1509  * @param cont_cls closure for 'cont'
1510  */
1511 void
1512 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1513                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
1514                      GST_NeighbourSendContinuation cont, void *cont_cls)
1515 {
1516   struct NeighbourMapEntry *n;
1517   struct MessageQueue *mq;
1518
1519   /* All ove these cases should never happen; they are all API violations.
1520      But we check anyway, just to be sure. */
1521   if (NULL == (n = lookup_neighbour (target)))
1522   {
1523     GNUNET_break (0);
1524     if (NULL != cont)
1525       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1526     return;
1527   }
1528   if (GNUNET_YES != test_connected (n))
1529   {
1530     GNUNET_break (0);
1531     if (NULL != cont)
1532       cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1533     return;
1534   }
1535   bytes_in_send_queue += msg_size;
1536   GNUNET_STATISTICS_set (GST_stats,
1537                          gettext_noop
1538                          ("# bytes in message queue for other peers"),
1539                          bytes_in_send_queue, GNUNET_NO);
1540   mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1541   mq->cont = cont;
1542   mq->cont_cls = cont_cls;
1543   memcpy (&mq[1], msg, msg_size);
1544   mq->message_buf = (const char *) &mq[1];
1545   mq->message_buf_size = msg_size;
1546   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1547   GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1548   if ( (NULL != n->is_active) ||
1549        ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1550     return;
1551   if (GNUNET_SCHEDULER_NO_TASK != n->task)
1552     GNUNET_SCHEDULER_cancel (n->task);
1553   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1554 }
1555
1556
1557 /**
1558  * Send a SESSION_CONNECT message via the given address.
1559  *
1560  * @param na address to use
1561  */
1562 static void
1563 send_session_connect (struct NeighbourAddress *na)
1564 {
1565   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1566   struct SessionConnectMessage connect_msg;
1567
1568   if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1569   {
1570     GNUNET_break (0);
1571     return;
1572   }
1573   if (NULL == na->session)
1574     na->session = papi->get_session (papi->cls, na->address);
1575   if (NULL == na->session)
1576   {
1577     GNUNET_break (0);
1578     return;
1579   }
1580   GNUNET_STATISTICS_update (GST_stats,
1581                             gettext_noop
1582                             ("# SESSION_CONNECT messages sent"),
1583                             1, GNUNET_NO);
1584   na->connect_timestamp = GNUNET_TIME_absolute_get ();
1585   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1586   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1587   connect_msg.reserved = htonl (0);
1588   connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1589   if (-1 ==
1590       papi->send (papi->cls,
1591                   na->session,
1592                   (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1593                   UINT_MAX,
1594                   GNUNET_TIME_UNIT_FOREVER_REL,
1595                   NULL, NULL))
1596   {
1597     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1598                 _("Failed to transmit CONNECT message via plugin to %s\n"),
1599                 GST_plugins_a2s (na->address));
1600   }
1601   GST_neighbours_notify_data_sent (&na->address->peer,
1602                                    na->address,
1603                                    na->session,
1604                                    sizeof (struct SessionConnectMessage));
1605
1606 }
1607
1608
1609 /**
1610  * Send a SESSION_CONNECT_ACK message via the given address.
1611  *
1612  * @param address address to use
1613  * @param session session to use
1614  * @param timestamp timestamp to use for the ACK message
1615  */
1616 static void
1617 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1618                                   struct Session *session,
1619                                   struct GNUNET_TIME_Absolute timestamp)
1620 {
1621   struct GNUNET_TRANSPORT_PluginFunctions *papi;
1622   struct SessionConnectMessage connect_msg;
1623
1624   if (NULL == (papi = GST_plugins_find (address->transport_name)))
1625   {
1626     GNUNET_break (0);
1627     return;
1628   }
1629   if (NULL == session)
1630     session = papi->get_session (papi->cls, address);
1631   if (NULL == session)
1632   {
1633     GNUNET_break (0);
1634     return;
1635   }
1636   GNUNET_STATISTICS_update (GST_stats,
1637                             gettext_noop
1638                             ("# CONNECT_ACK messages sent"),
1639                             1, GNUNET_NO);
1640   connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1641   connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1642   connect_msg.reserved = htonl (0);
1643   connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1644   (void) papi->send (papi->cls,
1645                      session,
1646                      (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1647                      UINT_MAX,
1648                      GNUNET_TIME_UNIT_FOREVER_REL,
1649                      NULL, NULL);
1650
1651 }
1652
1653
1654 /**
1655  * Create a fresh entry in the neighbour map for the given peer
1656  *
1657  * @param peer peer to create an entry for
1658  * @return new neighbour map entry
1659  */
1660 static struct NeighbourMapEntry *
1661 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1662 {
1663   struct NeighbourMapEntry *n;
1664
1665   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666               "Creating new neighbour entry for `%s'\n",
1667               GNUNET_i2s (peer));
1668   n = GNUNET_new (struct NeighbourMapEntry);
1669   n->id = *peer;
1670   n->state = S_NOT_CONNECTED;
1671   n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1672   n->last_util_transmission = GNUNET_TIME_absolute_get();
1673   n->util_payload_bytes_recv = 0;
1674   n->util_payload_bytes_sent = 0;
1675   n->util_total_bytes_recv = 0;
1676   n->util_total_bytes_sent = 0;
1677   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1678                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1679                                  MAX_BANDWIDTH_CARRY_S);
1680   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1681   GNUNET_assert (GNUNET_OK ==
1682                  GNUNET_CONTAINER_multipeermap_put (neighbours,
1683                                                     &n->id, n,
1684                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1685   return n;
1686 }
1687
1688
1689 /**
1690  * Check if the two given addresses are the same.
1691  * Actually only checks if the sessions are non-NULL
1692  * (which they should be) and then if they are identical;
1693  * the actual addresses don't matter if the session
1694  * pointers match anyway, and we must have session pointers
1695  * at this time.
1696  *
1697  * @param a1 first address to compare
1698  * @param a2 other address to compare
1699  * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1700  */
1701 static int
1702 address_matches (const struct NeighbourAddress *a1,
1703                  const struct NeighbourAddress *a2)
1704 {
1705   if ( (NULL == a1->session) ||
1706        (NULL == a2->session) )
1707   {
1708     GNUNET_break (0);
1709     return 0;
1710   }
1711   return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1712 }
1713
1714
1715 /**
1716  * Try to create a connection to the given target (eventually).
1717  *
1718  * @param target peer to try to connect to
1719  */
1720 void
1721 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1722 {
1723   struct NeighbourMapEntry *n;
1724
1725   if (NULL == neighbours)
1726   {
1727     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728                 "Asked to connect to peer `%s' during shutdown\n",
1729                 GNUNET_i2s (target));
1730     return; /* during shutdown, do nothing */
1731   }
1732   n = lookup_neighbour (target);
1733   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1734               "Asked to connect to peer `%s' (state: %s)\n",
1735               GNUNET_i2s (target),
1736               (NULL != n) ? print_state(n->state) : "UNKNOWN PEER");
1737   if (NULL != n)
1738   {
1739     switch (n->state)
1740     {
1741     case S_NOT_CONNECTED:
1742       /* this should not be possible */
1743       GNUNET_break (0);
1744       free_neighbour (n, GNUNET_NO);
1745       break;
1746     case S_INIT_ATS:
1747     case S_INIT_BLACKLIST:
1748     case S_CONNECT_SENT:
1749     case S_CONNECT_RECV_BLACKLIST_INBOUND:
1750     case S_CONNECT_RECV_ATS:
1751     case S_CONNECT_RECV_BLACKLIST:
1752     case S_CONNECT_RECV_ACK:
1753       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1754                   "Ignoring request to try to connect to `%s', already trying!\n",
1755                   GNUNET_i2s (target));
1756       return; /* already trying */
1757     case S_CONNECTED:
1758     case S_RECONNECT_ATS:
1759     case S_RECONNECT_BLACKLIST:
1760     case S_RECONNECT_SENT:
1761     case S_CONNECTED_SWITCHING_BLACKLIST:
1762     case S_CONNECTED_SWITCHING_CONNECT_SENT:
1763       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1764                   "Ignoring request to try to connect, already connected to `%s'!\n",
1765                   GNUNET_i2s (target));
1766       return; /* already connected */
1767     case S_DISCONNECT:
1768       /* get rid of remains, ready to re-try immediately */
1769       free_neighbour (n, GNUNET_NO);
1770       break;
1771     case S_DISCONNECT_FINISHED:
1772       /* should not be possible */
1773       GNUNET_assert (0);
1774     default:
1775       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1776                   "Unhandled state `%s'\n",
1777                   print_state (n->state));
1778       GNUNET_break (0);
1779       free_neighbour (n, GNUNET_NO);
1780       break;
1781     }
1782   }
1783   n = setup_neighbour (target);
1784   n->state = S_INIT_ATS;
1785   n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1786
1787   GNUNET_ATS_reset_backoff (GST_ats, target);
1788   n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1789 }
1790
1791
1792 /**
1793  * Function called with the result of a blacklist check.
1794  *
1795  * @param cls closure with the 'struct BlackListCheckContext'
1796  * @param peer peer this check affects
1797  * @param result #GNUNET_OK if the address is allowed
1798  */
1799 static void
1800 handle_test_blacklist_cont (void *cls,
1801                             const struct GNUNET_PeerIdentity *peer,
1802                             int result)
1803 {
1804   struct BlackListCheckContext *bcc = cls;
1805   struct NeighbourMapEntry *n;
1806
1807   bcc->bc = NULL;
1808   GNUNET_CONTAINER_DLL_remove (bc_head,
1809                                bc_tail,
1810                                bcc);
1811   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812               "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1813               GNUNET_i2s (peer),
1814               (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1815   if (GNUNET_OK == result)
1816     GST_ats_add_address (bcc->na.address, bcc->na.session);
1817   else
1818   {
1819     /* Blacklist disagreed on connecting to a peer with this address
1820      * Destroy address because we are not allowed to use it
1821      */
1822     if (NULL != bcc->na.session)
1823       GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1824     GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1825   }
1826   if (NULL == (n = lookup_neighbour (peer)))
1827     goto cleanup; /* nobody left to care about new address */
1828   switch (n->state)
1829   {
1830   case S_NOT_CONNECTED:
1831     /* this should not be possible */
1832     GNUNET_break (0);
1833     free_neighbour (n, GNUNET_NO);
1834     break;
1835   case S_INIT_ATS:
1836     /* waiting on ATS suggestion; still, pass address to ATS as a
1837        possibility */
1838     break;
1839   case S_INIT_BLACKLIST:
1840     /* check if the address the blacklist was fine with matches
1841        ATS suggestion, if so, we can move on! */
1842     if ( (GNUNET_OK == result) &&
1843          (1 == n->send_connect_ack) )
1844     {
1845       n->send_connect_ack = 2;
1846       send_session_connect_ack_message (bcc->na.address,
1847                                         bcc->na.session,
1848                                         n->connect_ack_timestamp);
1849     }
1850     if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1851       break; /* result for an address we currently don't care about */
1852     if (GNUNET_OK == result)
1853     {
1854       n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1855       n->state = S_CONNECT_SENT;
1856       send_session_connect (&n->primary_address);
1857     }
1858     else
1859     {
1860       free_address (&n->primary_address);
1861       n->state = S_INIT_ATS;
1862       n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1863     }
1864     break;
1865   case S_CONNECT_SENT:
1866     /* waiting on CONNECT_ACK, send ACK if one is pending */
1867     if ( (GNUNET_OK == result) &&
1868          (1 == n->send_connect_ack) )
1869     {
1870       n->send_connect_ack = 2;
1871       send_session_connect_ack_message (n->primary_address.address,
1872                                         n->primary_address.session,
1873                                         n->connect_ack_timestamp);
1874     }
1875     break;
1876   case S_CONNECT_RECV_BLACKLIST_INBOUND:
1877     n->state = S_CONNECT_RECV_ATS;
1878     n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1879     GNUNET_ATS_reset_backoff (GST_ats, peer);
1880     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1881     break;
1882   case S_CONNECT_RECV_ATS:
1883     /* waiting on ATS suggestion, don't care about blacklist */
1884     break;
1885   case S_CONNECT_RECV_BLACKLIST:
1886     if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1887     {
1888       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889                   "Blacklist result ignored, as it is not for our primary address\n");
1890       break; /* result for an address we currently don't care about */
1891     }
1892     if (GNUNET_OK == result)
1893     {
1894       n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1895       n->state = S_CONNECT_RECV_ACK;
1896       send_session_connect_ack_message (bcc->na.address,
1897                                         bcc->na.session,
1898                                         n->connect_ack_timestamp);
1899       if (1 == n->send_connect_ack)
1900         n->send_connect_ack = 2;
1901     }
1902     else
1903     {
1904       struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1905
1906       plugin = GST_plugins_find (bcc->na.address->transport_name);
1907       if ( (NULL != plugin) &&
1908            (NULL != bcc->na.session) )
1909       {
1910         plugin->disconnect_session (plugin->cls,
1911                                     bcc->na.session);
1912         break;
1913       }
1914       GNUNET_break (NULL != plugin);
1915       free_address (&n->primary_address);
1916       n->state = S_INIT_ATS;
1917       n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1918       GNUNET_ATS_reset_backoff (GST_ats, peer);
1919     }
1920     break;
1921   case S_CONNECT_RECV_ACK:
1922     /* waiting on SESSION_ACK, send ACK if one is pending */
1923     if ( (GNUNET_OK == result) &&
1924          (1 == n->send_connect_ack) )
1925     {
1926       n->send_connect_ack = 2;
1927       send_session_connect_ack_message (n->primary_address.address,
1928                                         n->primary_address.session,
1929                                         n->connect_ack_timestamp);
1930     }
1931     break;
1932   case S_CONNECTED:
1933     /* already connected, don't care about blacklist */
1934     break;
1935   case S_RECONNECT_ATS:
1936     /* still waiting on ATS suggestion, don't care about blacklist */
1937     break;
1938   case S_RECONNECT_BLACKLIST:
1939     if ( (GNUNET_OK == result) &&
1940          (1 == n->send_connect_ack) )
1941     {
1942       n->send_connect_ack = 2;
1943       send_session_connect_ack_message (bcc->na.address,
1944                                         bcc->na.session,
1945                                         n->connect_ack_timestamp);
1946     }
1947     if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1948       break; /* result for an address we currently don't care about */
1949     if (GNUNET_OK == result)
1950     {
1951       send_session_connect (&n->primary_address);
1952       n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1953       n->state = S_RECONNECT_SENT;
1954     }
1955     else
1956     {
1957       n->state = S_RECONNECT_ATS;
1958       n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1959     }
1960     break;
1961   case S_RECONNECT_SENT:
1962     /* waiting on CONNECT_ACK, don't care about blacklist */
1963     if ( (GNUNET_OK == result) &&
1964          (1 == n->send_connect_ack) )
1965     {
1966       n->send_connect_ack = 2;
1967       send_session_connect_ack_message (n->primary_address.address,
1968                                         n->primary_address.session,
1969                                         n->connect_ack_timestamp);
1970     }
1971     break;
1972   case S_CONNECTED_SWITCHING_BLACKLIST:
1973     if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1974       break; /* result for an address we currently don't care about */
1975     if (GNUNET_OK == result)
1976     {
1977       send_session_connect (&n->alternative_address);
1978       n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1979     }
1980     else
1981     {
1982       free_address (&n->alternative_address);
1983       n->state = S_CONNECTED;
1984     }
1985     break;
1986   case S_CONNECTED_SWITCHING_CONNECT_SENT:
1987     /* waiting on CONNECT_ACK, don't care about blacklist */
1988     if ( (GNUNET_OK == result) &&
1989          (1 == n->send_connect_ack) )
1990     {
1991       n->send_connect_ack = 2;
1992       send_session_connect_ack_message (n->primary_address.address,
1993                                         n->primary_address.session,
1994                                         n->connect_ack_timestamp);
1995     }
1996     break;
1997   case S_DISCONNECT:
1998     /* Nothing to do here, ATS will already do what can be done */
1999     break;
2000   case S_DISCONNECT_FINISHED:
2001     /* should not be possible */
2002     GNUNET_assert (0);
2003     break;
2004   default:
2005     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2006                 "Unhandled state `%s'\n",
2007                 print_state (n->state));
2008     GNUNET_break (0);
2009     free_neighbour (n, GNUNET_NO);
2010     break;
2011   }
2012  cleanup:
2013   GNUNET_HELLO_address_free (bcc->na.address);
2014   GNUNET_free (bcc);
2015 }
2016
2017
2018 /**
2019  * We want to know if connecting to a particular peer via
2020  * a particular address is allowed.  Check it!
2021  *
2022  * @param peer identity of the peer to switch the address for
2023  * @param ts time at which the check was initiated
2024  * @param address address of the other peer, NULL if other peer
2025  *                       connected to us
2026  * @param session session to use (or NULL)
2027  */
2028 static void
2029 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2030                  struct GNUNET_TIME_Absolute ts,
2031                  const struct GNUNET_HELLO_Address *address,
2032                  struct Session *session)
2033 {
2034   struct BlackListCheckContext *bcc;
2035   struct GST_BlacklistCheck *bc;
2036
2037   bcc = GNUNET_new (struct BlackListCheckContext);
2038   bcc->na.address = GNUNET_HELLO_address_copy (address);
2039   bcc->na.session = session;
2040   bcc->na.connect_timestamp = ts;
2041   GNUNET_CONTAINER_DLL_insert (bc_head,
2042                                bc_tail,
2043                                bcc);
2044   if (NULL != (bc = GST_blacklist_test_allowed (peer,
2045                                                 address->transport_name,
2046                                                 &handle_test_blacklist_cont, bcc)))
2047     bcc->bc = bc;
2048   /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2049      we must only store 'bc' if 'bc' is non-NULL... */
2050 }
2051
2052
2053 /**
2054  * We received a 'SESSION_CONNECT' message from the other peer.
2055  * Consider switching to it.
2056  *
2057  * @param message possibly a 'struct SessionConnectMessage' (check format)
2058  * @param peer identity of the peer to switch the address for
2059  * @param address address of the other peer, NULL if other peer
2060  *                       connected to us
2061  * @param session session to use (or NULL)
2062  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2063  */
2064 int
2065 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2066                                const struct GNUNET_PeerIdentity *peer,
2067                                const struct GNUNET_HELLO_Address *address,
2068                                struct Session *session)
2069 {
2070   const struct SessionConnectMessage *scm;
2071   struct NeighbourMapEntry *n;
2072   struct GNUNET_TIME_Absolute ts;
2073
2074   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075               "Received CONNECT message from peer `%s'\n",
2076               GNUNET_i2s (peer));
2077   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2078   {
2079     GNUNET_break_op (0);
2080     return GNUNET_SYSERR;
2081   }
2082   GNUNET_STATISTICS_update (GST_stats,
2083                             gettext_noop
2084                             ("# CONNECT messages received"),
2085                             1, GNUNET_NO);
2086   if (NULL == neighbours)
2087   {
2088     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2089                 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2090                 GNUNET_i2s (peer));
2091     return GNUNET_OK; /* we're shutting down */
2092   }
2093   scm = (const struct SessionConnectMessage *) message;
2094   GNUNET_break_op (0 == ntohl (scm->reserved));
2095   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2096   n = lookup_neighbour (peer);
2097   if (NULL == n)
2098     n = setup_neighbour (peer);
2099   n->send_connect_ack = 1;
2100   n->connect_ack_timestamp = ts;
2101
2102   switch (n->state)
2103   {
2104   case S_NOT_CONNECTED:
2105     n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2106     /* Do a blacklist check for the new address */
2107     check_blacklist (peer, ts, address, session);
2108     break;
2109   case S_INIT_ATS:
2110     /* CONNECT message takes priority over us asking ATS for address */
2111     n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2112     /* fallthrough */
2113   case S_INIT_BLACKLIST:
2114   case S_CONNECT_SENT:
2115   case S_CONNECT_RECV_BLACKLIST_INBOUND:
2116   case S_CONNECT_RECV_ATS:
2117   case S_CONNECT_RECV_BLACKLIST:
2118   case S_CONNECT_RECV_ACK:
2119     /* It can never hurt to have an alternative address in the above cases,
2120        see if it is allowed */
2121     check_blacklist (peer, ts, address, session);
2122     break;
2123   case S_CONNECTED:
2124     /* we are already connected and can thus send the ACK immediately;
2125        still, it can never hurt to have an alternative address, so also
2126        tell ATS  about it */
2127     GNUNET_assert (NULL != n->primary_address.address);
2128     GNUNET_assert (NULL != n->primary_address.session);
2129     n->send_connect_ack = 0;
2130     send_session_connect_ack_message (n->primary_address.address,
2131                                       n->primary_address.session, ts);
2132     check_blacklist (peer, ts, address, session);
2133     break;
2134   case S_RECONNECT_ATS:
2135   case S_RECONNECT_BLACKLIST:
2136   case S_RECONNECT_SENT:
2137     /* It can never hurt to have an alternative address in the above cases,
2138        see if it is allowed */
2139     check_blacklist (peer, ts, address, session);
2140     break;
2141   case S_CONNECTED_SWITCHING_BLACKLIST:
2142   case S_CONNECTED_SWITCHING_CONNECT_SENT:
2143     /* we are already connected and can thus send the ACK immediately;
2144        still, it can never hurt to have an alternative address, so also
2145        tell ATS  about it */
2146     GNUNET_assert (NULL != n->primary_address.address);
2147     GNUNET_assert (NULL != n->primary_address.session);
2148     n->send_connect_ack = 0;
2149     send_session_connect_ack_message (n->primary_address.address,
2150                                       n->primary_address.session, ts);
2151     check_blacklist (peer, ts, address, session);
2152     break;
2153   case S_DISCONNECT:
2154     /* get rid of remains without terminating sessions, ready to re-try */
2155     free_neighbour (n, GNUNET_YES);
2156     n = setup_neighbour (peer);
2157     n->state = S_CONNECT_RECV_ATS;
2158     GNUNET_ATS_reset_backoff (GST_ats, peer);
2159     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2160     break;
2161   case S_DISCONNECT_FINISHED:
2162     /* should not be possible */
2163     GNUNET_assert (0);
2164     break;
2165   default:
2166     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2167                 "Unhandled state `%s'\n",
2168                 print_state (n->state));
2169     GNUNET_break (0);
2170     return GNUNET_SYSERR;
2171   }
2172   return GNUNET_OK;
2173 }
2174
2175
2176 /**
2177  * For an existing neighbour record, set the active connection to
2178  * use the given address.
2179  *
2180  * @param peer identity of the peer to switch the address for
2181  * @param address address of the other peer, NULL if other peer
2182  *                       connected to us
2183  * @param session session to use (or NULL)
2184  * @param ats performance data
2185  * @param ats_count number of entries in ats
2186  * @param bandwidth_in inbound quota to be used when connection is up,
2187  *      0 to disconnect from peer
2188  * @param bandwidth_out outbound quota to be used when connection is up,
2189  *      0 to disconnect from peer
2190  */
2191 void
2192 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2193                                   const struct GNUNET_HELLO_Address *address,
2194                                   struct Session *session,
2195                                   const struct GNUNET_ATS_Information *ats,
2196                                   uint32_t ats_count,
2197                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2198                                   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2199 {
2200   struct NeighbourMapEntry *n;
2201   struct GNUNET_TRANSPORT_PluginFunctions *papi;
2202
2203   GNUNET_assert (address->transport_name != NULL);
2204   if (NULL == (n = lookup_neighbour (peer)))
2205     return;
2206
2207   /* Obtain an session for this address from plugin */
2208   if (NULL == (papi = GST_plugins_find (address->transport_name)))
2209   {
2210     /* we don't have the plugin for this address */
2211     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2212     return;
2213   }
2214   if ((NULL == session) && (0 == address->address_length))
2215   {
2216     GNUNET_break (0);
2217     if (strlen (address->transport_name) > 0)
2218       GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2219     return;
2220   }
2221
2222   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2223               "ATS tells us to switch to address '%s' session %p for "
2224               "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2225               (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2226               session,
2227               GNUNET_i2s (peer),
2228               print_state (n->state),
2229               n->send_connect_ack,
2230               ntohl (bandwidth_in.value__),
2231               ntohl (bandwidth_out.value__));
2232
2233   if (NULL == session)
2234   {
2235     session = papi->get_session (papi->cls, address);
2236     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237                 "Obtained new session for peer `%s' and  address '%s': %p\n",
2238                 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2239   }
2240   if (NULL == session)
2241   {
2242     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243                 "Failed to obtain new session for peer `%s' and  address '%s'\n",
2244                 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2245     GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2246     return;
2247   }
2248   switch (n->state)
2249   {
2250   case S_NOT_CONNECTED:
2251     GNUNET_break (0);
2252     free_neighbour (n, GNUNET_NO);
2253     return;
2254   case S_INIT_ATS:
2255     set_address (&n->primary_address,
2256                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2257     n->state = S_INIT_BLACKLIST;
2258     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2259     check_blacklist (&n->id,
2260                      n->connect_ack_timestamp,
2261                      address, session);
2262     break;
2263   case S_INIT_BLACKLIST:
2264     /* ATS suggests a different address, switch again */
2265     set_address (&n->primary_address,
2266                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2267     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2268     check_blacklist (&n->id,
2269                      n->connect_ack_timestamp,
2270                      address, session);
2271     break;
2272   case S_CONNECT_SENT:
2273     /* ATS suggests a different address, switch again */
2274     set_address (&n->primary_address,
2275                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2276     n->state = S_INIT_BLACKLIST;
2277     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2278     check_blacklist (&n->id,
2279                      n->connect_ack_timestamp,
2280                      address, session);
2281     break;
2282   case S_CONNECT_RECV_ATS:
2283     set_address (&n->primary_address,
2284                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2285     n->state = S_CONNECT_RECV_BLACKLIST;
2286     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2287     check_blacklist (&n->id,
2288                      n->connect_ack_timestamp,
2289                      address, session);
2290     break;
2291   case S_CONNECT_RECV_BLACKLIST_INBOUND:
2292     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2293     check_blacklist (&n->id,
2294                      n->connect_ack_timestamp,
2295                      address, session);
2296     break;
2297   case S_CONNECT_RECV_BLACKLIST:
2298   case S_CONNECT_RECV_ACK:
2299     /* ATS asks us to switch while we were trying to connect; switch to new
2300        address and check blacklist again */
2301     set_address (&n->primary_address,
2302                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2303     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2304     check_blacklist (&n->id,
2305                      n->connect_ack_timestamp,
2306                      address, session);
2307     break;
2308   case S_CONNECTED:
2309     GNUNET_assert (NULL != n->primary_address.address);
2310     GNUNET_assert (NULL != n->primary_address.session);
2311     if (n->primary_address.session == session)
2312     {
2313       /* not an address change, just a quota change */
2314       set_address (&n->primary_address,
2315                    address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2316       break;
2317     }
2318     /* ATS asks us to switch a life connection; see if we can get
2319        a CONNECT_ACK on it before we actually do this! */
2320     set_address (&n->alternative_address,
2321                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2322     n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2323     check_blacklist (&n->id,
2324                      GNUNET_TIME_absolute_get (),
2325                      address, session);
2326     break;
2327   case S_RECONNECT_ATS:
2328     set_address (&n->primary_address,
2329                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2330     n->state = S_RECONNECT_BLACKLIST;
2331     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2332     check_blacklist (&n->id,
2333                      n->connect_ack_timestamp,
2334                      address, session);
2335     break;
2336   case S_RECONNECT_BLACKLIST:
2337     /* ATS asks us to switch while we were trying to reconnect; switch to new
2338        address and check blacklist again */
2339     set_address (&n->primary_address,
2340                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2341     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2342     check_blacklist (&n->id,
2343                      n->connect_ack_timestamp,
2344                      address, session);
2345     break;
2346   case S_RECONNECT_SENT:
2347     /* ATS asks us to switch while we were trying to reconnect; switch to new
2348        address and check blacklist again */
2349     set_address (&n->primary_address,
2350                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2351     n->state = S_RECONNECT_BLACKLIST;
2352     n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2353     check_blacklist (&n->id,
2354                      n->connect_ack_timestamp,
2355                      address, session);
2356     break;
2357   case S_CONNECTED_SWITCHING_BLACKLIST:
2358     if (n->primary_address.session == session)
2359     {
2360       /* ATS switches back to still-active session */
2361       free_address (&n->alternative_address);
2362       n->state = S_CONNECTED;
2363       break;
2364     }
2365     /* ATS asks us to switch a life connection, update blacklist check */
2366     set_address (&n->alternative_address,
2367                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2368     check_blacklist (&n->id,
2369                      GNUNET_TIME_absolute_get (),
2370                      address, session);
2371     break;
2372   case S_CONNECTED_SWITCHING_CONNECT_SENT:
2373     if (n->primary_address.session == session)
2374     {
2375       /* ATS switches back to still-active session */
2376       free_address (&n->alternative_address);
2377       n->state = S_CONNECTED;
2378       break;
2379     }
2380     /* ATS asks us to switch a life connection, update blacklist check */
2381     set_address (&n->alternative_address,
2382                  address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2383     n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2384     check_blacklist (&n->id,
2385                      GNUNET_TIME_absolute_get (),
2386                      address, session);
2387     break;
2388   case S_DISCONNECT:
2389     /* not going to switch addresses while disconnecting */
2390     return;
2391   case S_DISCONNECT_FINISHED:
2392     GNUNET_assert (0);
2393     break;
2394   default:
2395     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2396                 "Unhandled state `%s'\n",
2397                 print_state (n->state));
2398     GNUNET_break (0);
2399     break;
2400   }
2401 }
2402
2403
2404 static int
2405 send_utilization_data (void *cls,
2406                        const struct GNUNET_PeerIdentity *key,
2407                        void *value)
2408 {
2409   struct NeighbourMapEntry *n = value;
2410   struct GNUNET_ATS_Information atsi[4];
2411   uint32_t bps_pl_in;
2412   uint32_t bps_pl_out;
2413   uint32_t bps_in;
2414   uint32_t bps_out;
2415   struct GNUNET_TIME_Relative delta;
2416
2417   delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2418                                                GNUNET_TIME_absolute_get ());
2419
2420   bps_pl_in = 0;
2421
2422   if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2423     bps_pl_in =  (1000LL * 1000LL *  n->util_payload_bytes_recv) / (delta.rel_value_us);
2424   bps_pl_out = 0;
2425   if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2426     bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2427   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428               "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2429               GNUNET_i2s (key),
2430               bps_pl_in,
2431               bps_pl_out);
2432   bps_in = 0;
2433   if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2434     bps_in =  (1000LL * 1000LL *  n->util_total_bytes_recv) / (delta.rel_value_us);
2435   bps_out = 0;
2436   if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2437     bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2438
2439
2440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441               "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2442               GNUNET_i2s (key),
2443               bps_in,
2444               bps_out);
2445   atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2446   atsi[0].value = htonl (bps_out);
2447   atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2448   atsi[1].value = htonl (bps_in);
2449
2450   atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2451   atsi[2].value = htonl (bps_pl_out);
2452   atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2453   atsi[3].value = htonl (bps_pl_in);
2454
2455   GST_ats_update_metrics (key, n->primary_address.address,
2456       n->primary_address.session, atsi, 4);
2457   n->util_payload_bytes_recv = 0;
2458   n->util_payload_bytes_sent = 0;
2459   n->util_total_bytes_recv = 0;
2460   n->util_total_bytes_sent = 0;
2461   n->last_util_transmission = GNUNET_TIME_absolute_get();
2462   return GNUNET_OK;
2463 }
2464
2465
2466 /**
2467  * Task transmitting utilization in a regular interval
2468  *
2469  * @param cls the 'struct NeighbourMapEntry' for which we are running
2470  * @param tc scheduler context (unused)
2471  */
2472 static void
2473 utilization_transmission (void *cls,
2474                           const struct GNUNET_SCHEDULER_TaskContext *tc)
2475 {
2476   util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2477
2478   if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2479     GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2480
2481   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2482       utilization_transmission, NULL);
2483
2484 }
2485
2486
2487 void
2488 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2489                                  const struct GNUNET_HELLO_Address *address,
2490                                  struct Session *session,
2491                                  const struct GNUNET_MessageHeader *message)
2492 {
2493   struct NeighbourMapEntry *n;
2494
2495   n = lookup_neighbour (peer);
2496   if (NULL == n)
2497     return;
2498   n->util_total_bytes_recv += ntohs(message->size);
2499 }
2500
2501
2502 void
2503 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2504                                     const struct GNUNET_HELLO_Address *address,
2505                                     struct Session *session,
2506                                     const struct GNUNET_MessageHeader *message)
2507 {
2508   struct NeighbourMapEntry *n;
2509   n = lookup_neighbour (peer);
2510   if (NULL == n)
2511     return;
2512   n->util_payload_bytes_recv += ntohs(message->size);
2513 }
2514
2515
2516 void
2517 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2518                                  const struct GNUNET_HELLO_Address *address,
2519                                  struct Session *session,
2520                                  size_t size)
2521 {
2522   struct NeighbourMapEntry *n;
2523   n = lookup_neighbour (peer);
2524   if (NULL == n)
2525       return;
2526   if (n->primary_address.session != session)
2527     return;
2528   n->util_total_bytes_sent += size;
2529 }
2530
2531
2532 void
2533 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2534                                     size_t size)
2535 {
2536   struct NeighbourMapEntry *n;
2537   n = lookup_neighbour (peer);
2538   if (NULL == n)
2539     return;
2540   n->util_payload_bytes_sent += size;
2541 }
2542
2543
2544 /**
2545  * Master task run for every neighbour.  Performs all of the time-related
2546  * activities (keep alive, send next message, disconnect if idle, finish
2547  * clean up after disconnect).
2548  *
2549  * @param cls the 'struct NeighbourMapEntry' for which we are running
2550  * @param tc scheduler context (unused)
2551  */
2552 static void
2553 master_task (void *cls,
2554              const struct GNUNET_SCHEDULER_TaskContext *tc)
2555 {
2556   struct NeighbourMapEntry *n = cls;
2557   struct GNUNET_TIME_Relative delay;
2558
2559   n->task = GNUNET_SCHEDULER_NO_TASK;
2560   delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2561   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562               "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2563               GNUNET_i2s (&n->id),
2564               print_state(n->state),
2565               GNUNET_STRINGS_relative_time_to_string (delay,
2566                                                       GNUNET_YES));
2567   switch (n->state)
2568   {
2569   case S_NOT_CONNECTED:
2570     /* invalid state for master task, clean up */
2571     GNUNET_break (0);
2572     n->state = S_DISCONNECT_FINISHED;
2573     free_neighbour (n, GNUNET_NO);
2574     return;
2575   case S_INIT_ATS:
2576     if (0 == delay.rel_value_us)
2577     {
2578       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579                   "Connection to `%s' timed out waiting for ATS to provide address\n",
2580                   GNUNET_i2s (&n->id));
2581       n->state = S_DISCONNECT_FINISHED;
2582       free_neighbour (n, GNUNET_NO);
2583       return;
2584     }
2585     break;
2586   case S_INIT_BLACKLIST:
2587     if (0 == delay.rel_value_us)
2588     {
2589       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590                   "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2591                   GNUNET_i2s (&n->id));
2592       n->state = S_DISCONNECT_FINISHED;
2593       free_neighbour (n, GNUNET_NO);
2594       return;
2595     }
2596     break;
2597   case S_CONNECT_SENT:
2598     if (0 == delay.rel_value_us)
2599     {
2600       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2601                   "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2602                   GNUNET_i2s (&n->id));
2603       /* We could not send to this address, delete address and session */
2604       if (NULL != n->primary_address.session)
2605         GNUNET_ATS_address_destroyed (GST_ats,
2606             n->primary_address.address, n->primary_address.session);
2607       GNUNET_ATS_address_destroyed (GST_ats,
2608           n->primary_address.address, NULL);
2609       disconnect_neighbour (n);
2610       return;
2611     }
2612     break;
2613   case S_CONNECT_RECV_BLACKLIST_INBOUND:
2614     if (0 == delay.rel_value_us)
2615     {
2616       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617                   "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2618                   GNUNET_i2s (&n->id));
2619       n->state = S_DISCONNECT_FINISHED;
2620       free_neighbour (n, GNUNET_NO);
2621       return;
2622     }
2623     break;
2624   case S_CONNECT_RECV_ATS:
2625     if (0 == delay.rel_value_us)
2626     {
2627       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628                   "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2629                   GNUNET_i2s (&n->id));
2630       n->state = S_DISCONNECT_FINISHED;
2631       free_neighbour (n, GNUNET_NO);
2632       return;
2633     }
2634     break;
2635   case S_CONNECT_RECV_BLACKLIST:
2636     if (0 == delay.rel_value_us)
2637     {
2638       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639                   "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2640                   GNUNET_i2s (&n->id));
2641       n->state = S_DISCONNECT_FINISHED;
2642       free_neighbour (n, GNUNET_NO);
2643       return;
2644     }
2645     break;
2646   case S_CONNECT_RECV_ACK:
2647     if (0 == delay.rel_value_us)
2648     {
2649       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650                   "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2651                   GNUNET_i2s (&n->id));
2652       disconnect_neighbour (n);
2653       return;
2654     }
2655     break;
2656   case S_CONNECTED:
2657     if (0 == delay.rel_value_us)
2658     {
2659       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2661                   GNUNET_i2s (&n->id));
2662       disconnect_neighbour (n);
2663       return;
2664     }
2665     try_transmission_to_peer (n);
2666     send_keepalive (n);
2667     break;
2668   case S_RECONNECT_ATS:
2669     if (0 == delay.rel_value_us)
2670     {
2671       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672                   "Connection to `%s' timed out, waiting for ATS replacement address\n",
2673                   GNUNET_i2s (&n->id));
2674       disconnect_neighbour (n);
2675       return;
2676     }
2677     break;
2678   case S_RECONNECT_BLACKLIST:
2679     if (0 == delay.rel_value_us)
2680     {
2681       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682                   "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2683                   GNUNET_i2s (&n->id));
2684       disconnect_neighbour (n);
2685       return;
2686     }
2687     break;
2688   case S_RECONNECT_SENT:
2689     if (0 == delay.rel_value_us)
2690     {
2691       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692                   "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2693                   GNUNET_i2s (&n->id));
2694       disconnect_neighbour (n);
2695       return;
2696     }
2697     break;
2698   case S_CONNECTED_SWITCHING_BLACKLIST:
2699     if (0 == delay.rel_value_us)
2700     {
2701       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2703                   GNUNET_i2s (&n->id));
2704       disconnect_neighbour (n);
2705       return;
2706     }
2707     try_transmission_to_peer (n);
2708     send_keepalive (n);
2709     break;
2710   case S_CONNECTED_SWITCHING_CONNECT_SENT:
2711     if (0 == delay.rel_value_us)
2712     {
2713       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714                   "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2715                   GNUNET_i2s (&n->id));
2716       disconnect_neighbour (n);
2717       return;
2718     }
2719     try_transmission_to_peer (n);
2720     send_keepalive (n);
2721     break;
2722   case S_DISCONNECT:
2723     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724                 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2725                 GNUNET_i2s (&n->id));
2726     free_neighbour (n, GNUNET_NO);
2727     return;
2728   case S_DISCONNECT_FINISHED:
2729     /* how did we get here!? */
2730     GNUNET_assert (0);
2731     break;
2732   default:
2733     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2734                 "Unhandled state `%s'\n",
2735                 print_state (n->state));
2736     GNUNET_break (0);
2737     break;
2738   }
2739   if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2740        (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2741        (S_CONNECTED == n->state) )
2742   {
2743     /* if we are *now* in one of these three states, we're sending
2744        keep alive messages, so we need to consider the keepalive
2745        delay, not just the connection timeout */
2746     delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2747                                       delay);
2748   }
2749   if (GNUNET_SCHEDULER_NO_TASK == n->task)
2750     n->task = GNUNET_SCHEDULER_add_delayed (delay,
2751                                             &master_task,
2752                                             n);
2753 }
2754
2755
2756 /**
2757  * Send a SESSION_ACK message to the neighbour to confirm that we
2758  * got his CONNECT_ACK.
2759  *
2760  * @param n neighbour to send the SESSION_ACK to
2761  */
2762 static void
2763 send_session_ack_message (struct NeighbourMapEntry *n)
2764 {
2765   struct GNUNET_MessageHeader msg;
2766
2767   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2768   msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2769   (void) send_with_session(n,
2770                            (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2771                            UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2772                            NULL, NULL);
2773 }
2774
2775
2776 /**
2777  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2778  * Consider switching to it.
2779  *
2780  * @param message possibly a 'struct SessionConnectMessage' (check format)
2781  * @param peer identity of the peer to switch the address for
2782  * @param address address of the other peer, NULL if other peer
2783  *                       connected to us
2784  * @param session session to use (or NULL)
2785  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2786  */
2787 int
2788 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2789                                    const struct GNUNET_PeerIdentity *peer,
2790                                    const struct GNUNET_HELLO_Address *address,
2791                                    struct Session *session)
2792 {
2793   const struct SessionConnectMessage *scm;
2794   struct GNUNET_TIME_Absolute ts;
2795   struct NeighbourMapEntry *n;
2796
2797   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798               "Received CONNECT_ACK message from peer `%s'\n",
2799               GNUNET_i2s (peer));
2800
2801   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2802   {
2803     GNUNET_break_op (0);
2804     return GNUNET_SYSERR;
2805   }
2806   GNUNET_STATISTICS_update (GST_stats,
2807                             gettext_noop
2808                             ("# CONNECT_ACK messages received"),
2809                             1, GNUNET_NO);
2810   scm = (const struct SessionConnectMessage *) message;
2811   GNUNET_break_op (ntohl (scm->reserved) == 0);
2812   if (NULL == (n = lookup_neighbour (peer)))
2813   {
2814     GNUNET_STATISTICS_update (GST_stats,
2815                               gettext_noop
2816                               ("# unexpected CONNECT_ACK messages (no peer)"),
2817                               1, GNUNET_NO);
2818     return GNUNET_SYSERR;
2819   }
2820   ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2821   switch (n->state)
2822   {
2823   case S_NOT_CONNECTED:
2824     GNUNET_break (0);
2825     free_neighbour (n, GNUNET_NO);
2826     return GNUNET_SYSERR;
2827   case S_INIT_ATS:
2828   case S_INIT_BLACKLIST:
2829     GNUNET_STATISTICS_update (GST_stats,
2830                               gettext_noop
2831                               ("# unexpected CONNECT_ACK messages (not ready)"),
2832                               1, GNUNET_NO);
2833     break;
2834   case S_CONNECT_SENT:
2835     if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2836     {
2837       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2838                   "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2839       return GNUNET_OK;
2840     }
2841     n->state = S_CONNECTED;
2842     n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2843     GNUNET_STATISTICS_set (GST_stats,
2844                            gettext_noop ("# peers connected"),
2845                            ++neighbours_connected,
2846                            GNUNET_NO);
2847     connect_notify_cb (callback_cls, &n->id,
2848                        n->primary_address.bandwidth_in,
2849                        n->primary_address.bandwidth_out);
2850     /* Tell ATS that the outbound session we created to send CONNECT was successful */
2851     GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2852     set_address (&n->primary_address,
2853                  n->primary_address.address,
2854                  n->primary_address.session,
2855                  n->primary_address.bandwidth_in,
2856                  n->primary_address.bandwidth_out,
2857                  GNUNET_YES);
2858     send_session_ack_message (n);
2859     break;
2860   case S_CONNECT_RECV_BLACKLIST_INBOUND:
2861   case S_CONNECT_RECV_ATS:
2862   case S_CONNECT_RECV_BLACKLIST:
2863   case S_CONNECT_RECV_ACK:
2864     GNUNET_STATISTICS_update (GST_stats,
2865                               gettext_noop
2866                               ("# unexpected CONNECT_ACK messages (not ready)"),
2867                               1, GNUNET_NO);
2868     break;
2869   case S_CONNECTED:
2870     /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2871     send_session_ack_message (n);
2872     break;
2873   case S_RECONNECT_ATS:
2874   case S_RECONNECT_BLACKLIST:
2875     /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2876        to give us a new address... */
2877     GNUNET_STATISTICS_update (GST_stats,
2878                               gettext_noop
2879                               ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2880                               1, GNUNET_NO);
2881     break;
2882   case S_RECONNECT_SENT:
2883     /* new address worked; go back to connected! */
2884     n->state = S_CONNECTED;
2885     send_session_ack_message (n);
2886     break;
2887   case S_CONNECTED_SWITCHING_BLACKLIST:
2888     /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2889     send_session_ack_message (n);
2890     break;
2891   case S_CONNECTED_SWITCHING_CONNECT_SENT:
2892     /* new address worked; adopt it and go back to connected! */
2893     n->state = S_CONNECTED;
2894     n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2895     GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2896
2897     GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2898     set_address (&n->primary_address,
2899                  n->alternative_address.address,
2900                  n->alternative_address.session,
2901                  n->alternative_address.bandwidth_in,
2902                  n->alternative_address.bandwidth_out,
2903                  GNUNET_YES);
2904     free_address (&n->alternative_address);
2905     send_session_ack_message (n);
2906     break;
2907   case S_DISCONNECT:
2908     GNUNET_STATISTICS_update (GST_stats,
2909                               gettext_noop
2910                               ("# unexpected CONNECT_ACK messages (disconnecting)"),
2911                               1, GNUNET_NO);
2912     return GNUNET_SYSERR;
2913   case S_DISCONNECT_FINISHED:
2914     GNUNET_assert (0);
2915     break;
2916   default:
2917     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2918                 "Unhandled state `%s'\n",
2919                 print_state (n->state));
2920     GNUNET_break (0);
2921     return GNUNET_SYSERR;
2922   }
2923   return GNUNET_OK;
2924 }
2925
2926
2927 /**
2928  * A session was terminated. Take note; if needed, try to get
2929  * an alternative address from ATS.
2930  *
2931  * @param peer identity of the peer where the session died
2932  * @param session session that is gone
2933  * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2934  *        this session was not in use
2935  */
2936 int
2937 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2938                                    struct Session *session)
2939 {
2940   struct NeighbourMapEntry *n;
2941   struct BlackListCheckContext *bcc;
2942   struct BlackListCheckContext *bcc_next;
2943
2944   /* make sure to cancel all ongoing blacklist checks involving 'session' */
2945   bcc_next = bc_head;
2946   while (NULL != (bcc = bcc_next))
2947   {
2948     bcc_next = bcc->next;
2949     if (bcc->na.session == session)
2950     {
2951       if (NULL != bcc->bc)
2952         GST_blacklist_test_cancel (bcc->bc);
2953       GNUNET_HELLO_address_free (bcc->na.address);
2954       GNUNET_CONTAINER_DLL_remove (bc_head,
2955                                    bc_tail,
2956                                    bcc);
2957       GNUNET_free (bcc);
2958     }
2959   }
2960   if (NULL == (n = lookup_neighbour (peer)))
2961     return GNUNET_NO; /* can't affect us */
2962   if (session != n->primary_address.session)
2963   {
2964     if (session == n->alternative_address.session)
2965     {
2966       free_address (&n->alternative_address);
2967       if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2968            (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2969         n->state = S_CONNECTED;
2970       else
2971         GNUNET_break (0);
2972     }
2973     return GNUNET_NO; /* doesn't affect us further */
2974   }
2975
2976   n->expect_latency_response = GNUNET_NO;
2977   switch (n->state)
2978   {
2979   case S_NOT_CONNECTED:
2980     GNUNET_break (0);
2981     free_neighbour (n, GNUNET_NO);
2982     return GNUNET_YES;
2983   case S_INIT_ATS:
2984     GNUNET_break (0);
2985     free_neighbour (n, GNUNET_NO);
2986     return GNUNET_YES;
2987   case S_INIT_BLACKLIST:
2988   case S_CONNECT_SENT:
2989     free_address (&n->primary_address);
2990     n->state = S_INIT_ATS;
2991     n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2992     // FIXME: need to ask ATS for suggestions again?
2993     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2994     break;
2995   case S_CONNECT_RECV_BLACKLIST_INBOUND:
2996   case S_CONNECT_RECV_ATS:
2997   case S_CONNECT_RECV_BLACKLIST:
2998   case S_CONNECT_RECV_ACK:
2999     /* error on inbound session; free neighbour entirely */
3000     free_address (&n->primary_address);
3001     free_neighbour (n, GNUNET_NO);
3002     return GNUNET_YES;
3003   case S_CONNECTED:
3004     free_address (&n->primary_address);
3005     n->state = S_RECONNECT_ATS;
3006     n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3007     /* FIXME: is this ATS call needed? */
3008     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3009     break;
3010   case S_RECONNECT_ATS:
3011     /* we don't have an address, how can it go down? */
3012     GNUNET_break (0);
3013     break;
3014   case S_RECONNECT_BLACKLIST:
3015   case S_RECONNECT_SENT:
3016     n->state = S_RECONNECT_ATS;
3017     n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3018     // FIXME: need to ask ATS for suggestions again?
3019     n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3020     break;
3021   case S_CONNECTED_SWITCHING_BLACKLIST:
3022     /* primary went down while we were checking secondary against
3023        blacklist, adopt secondary as primary */
3024     free_address (&n->primary_address);
3025     n->primary_address = n->alternative_address;
3026     memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3027     n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3028     n->state = S_RECONNECT_BLACKLIST;
3029     break;
3030   case S_CONNECTED_SWITCHING_CONNECT_SENT:
3031     /* primary went down while we were waiting for CONNECT_ACK on secondary;
3032        secondary as primary */
3033     free_address (&n->primary_address);
3034     n->primary_address = n->alternative_address;
3035     memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3036     n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3037     n->state = S_RECONNECT_SENT;
3038     break;
3039   case S_DISCONNECT:
3040     free_address (&n->primary_address);
3041     break;
3042   case S_DISCONNECT_FINISHED:
3043     /* neighbour was freed and plugins told to terminate session */
3044     return GNUNET_NO;
3045     break;
3046   default:
3047     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3048                 "Unhandled state `%s'\n",
3049                 print_state (n->state));
3050     GNUNET_break (0);
3051     break;
3052   }
3053   if (GNUNET_SCHEDULER_NO_TASK != n->task)
3054     GNUNET_SCHEDULER_cancel (n->task);
3055   n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3056   return GNUNET_YES;
3057 }
3058
3059
3060 /**
3061  * We received a 'SESSION_ACK' message from the other peer.
3062  * If we sent a 'CONNECT_ACK' last, this means we are now
3063  * connected.  Otherwise, do nothing.
3064  *
3065  * @param message possibly a 'struct SessionConnectMessage' (check format)
3066  * @param peer identity of the peer to switch the address for
3067  * @param address address of the other peer, NULL if other peer
3068  *                       connected to us
3069  * @param session session to use (or NULL)
3070  * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3071  */
3072 int
3073 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3074                                    const struct GNUNET_PeerIdentity *peer,
3075                                    const struct GNUNET_HELLO_Address *address,
3076                                    struct Session *session)
3077 {
3078   struct NeighbourMapEntry *n;
3079
3080   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3081               "Received SESSION_ACK message from peer `%s'\n",
3082               GNUNET_i2s (peer));
3083   if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3084   {
3085     GNUNET_break_op (0);
3086     return GNUNET_SYSERR;
3087   }
3088   GNUNET_STATISTICS_update (GST_stats,
3089                             gettext_noop
3090                             ("# SESSION_ACK messages received"),
3091                             1, GNUNET_NO);
3092   if (NULL == (n = lookup_neighbour (peer)))
3093     return GNUNET_SYSERR;
3094   /* check if we are in a plausible state for having sent
3095      a CONNECT_ACK.  If not, return, otherwise break */
3096   if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3097          (S_CONNECT_SENT != n->state) ) ||
3098        (2 != n->send_connect_ack) )
3099   {
3100     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3101                 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3102                 GNUNET_i2s (peer),
3103                 print_state (n->state),
3104                 n->send_connect_ack);
3105     GNUNET_STATISTICS_update (GST_stats,
3106                               gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3107                               GNUNET_NO);
3108     return GNUNET_SYSERR;
3109   }
3110   n->state = S_CONNECTED;
3111   n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3112   GNUNET_STATISTICS_set (GST_stats,
3113                          gettext_noop ("# peers connected"),
3114                          ++neighbours_connected,
3115                          GNUNET_NO);
3116   connect_notify_cb (callback_cls, &n->id,
3117                      n->primary_address.bandwidth_in,
3118                      n->primary_address.bandwidth_out);
3119
3120   GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3121   set_address (&n->primary_address,
3122                n->primary_address.address,
3123                n->primary_address.session,
3124                n->primary_address.bandwidth_in,
3125                n->primary_address.bandwidth_out,
3126                GNUNET_YES);
3127   return GNUNET_OK;
3128 }
3129
3130
3131 /**
3132  * Test if we're connected to the given peer.
3133  *
3134  * @param target peer to test
3135  * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3136  */
3137 int
3138 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3139 {
3140   return test_connected (lookup_neighbour (target));
3141 }
3142
3143
3144 /**
3145  * Change the incoming quota for the given peer.
3146  *
3147  * @param neighbour identity of peer to change qutoa for
3148  * @param quota new quota
3149  */
3150 void
3151 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3152                                    struct GNUNET_BANDWIDTH_Value32NBO quota)
3153 {
3154   struct NeighbourMapEntry *n;
3155
3156   if (NULL == (n = lookup_neighbour (neighbour)))
3157   {
3158     GNUNET_STATISTICS_update (GST_stats,
3159                               gettext_noop
3160                               ("# SET QUOTA messages ignored (no such peer)"),
3161                               1, GNUNET_NO);
3162     return;
3163   }
3164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165               "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3166               ntohl (quota.value__), GNUNET_i2s (&n->id));
3167   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3168   if (0 != ntohl (quota.value__))
3169     return;
3170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3171               GNUNET_i2s (&n->id), "SET_QUOTA");
3172   if (GNUNET_YES == test_connected (n))
3173     GNUNET_STATISTICS_update (GST_stats,
3174                               gettext_noop ("# disconnects due to quota of 0"),
3175                               1, GNUNET_NO);
3176   disconnect_neighbour (n);
3177 }
3178
3179
3180 /**
3181  * We received a disconnect message from the given peer,
3182  * validate and process.
3183  *
3184  * @param peer sender of the message
3185  * @param msg the disconnect message
3186  */
3187 void
3188 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3189                                           *peer,
3190                                           const struct GNUNET_MessageHeader
3191                                           *msg)
3192 {
3193   struct NeighbourMapEntry *n;
3194   const struct SessionDisconnectMessage *sdm;
3195
3196   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197               "Received DISCONNECT message from peer `%s'\n",
3198               GNUNET_i2s (peer));
3199   if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3200   {
3201     // GNUNET_break_op (0);
3202     GNUNET_STATISTICS_update (GST_stats,
3203                               gettext_noop
3204                               ("# disconnect messages ignored (old format)"), 1,
3205                               GNUNET_NO);
3206     return;
3207   }
3208   GNUNET_STATISTICS_update (GST_stats,
3209                             gettext_noop
3210                             ("# DISCONNECT messages received"),
3211                             1, GNUNET_NO);
3212   sdm = (const struct SessionDisconnectMessage *) msg;
3213   if (NULL == (n = lookup_neighbour (peer)))
3214     return;                     /* gone already */
3215   if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3216   {
3217     GNUNET_STATISTICS_update (GST_stats,
3218                               gettext_noop
3219                               ("# disconnect messages ignored (timestamp)"), 1,
3220                               GNUNET_NO);
3221     return;
3222   }
3223   if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3224   {
3225     GNUNET_break_op (0);
3226     return;
3227   }
3228   if (ntohl (sdm->purpose.size) !=
3229       sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3230       sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3231       sizeof (struct GNUNET_TIME_AbsoluteNBO))
3232   {
3233     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3234                 "%s message from peer `%s' has invalid size \n",
3235                 "DISCONNECT",
3236                 GNUNET_i2s (peer));
3237     GNUNET_break_op (0);
3238     return;
3239   }
3240   if (GNUNET_OK !=
3241       GNUNET_CRYPTO_eddsa_verify
3242       (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3243        &sdm->signature, &sdm->public_key))
3244   {
3245     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3246                 "%s message from peer `%s' cannot be verified \n",
3247                 "DISCONNECT",
3248                 GNUNET_i2s (peer));
3249     GNUNET_break_op (0);
3250     return;
3251   }
3252   if (GNUNET_YES == test_connected (n))
3253     GNUNET_STATISTICS_update (GST_stats,
3254                               gettext_noop
3255                               ("# other peer asked to disconnect from us"), 1,
3256                               GNUNET_NO);
3257   disconnect_neighbour (n);
3258 }
3259
3260
3261 /**
3262  * Closure for the neighbours_iterate function.
3263  */
3264 struct IteratorContext
3265 {
3266   /**
3267    * Function to call on each connected neighbour.
3268    */
3269   GST_NeighbourIterator cb;
3270
3271   /**
3272    * Closure for 'cb'.
3273    */
3274   void *cb_cls;
3275 };
3276
3277
3278 /**
3279  * Call the callback from the closure for each connected neighbour.
3280  *
3281  * @param cls the `struct IteratorContext`
3282  * @param key the hash of the public key of the neighbour
3283  * @param value the `struct NeighbourMapEntry`
3284  * @return #GNUNET_OK (continue to iterate)
3285  */
3286 static int
3287 neighbours_iterate (void *cls,
3288                     const struct GNUNET_PeerIdentity *key,
3289                     void *value)
3290 {
3291   struct IteratorContext *ic = cls;
3292   struct NeighbourMapEntry *n = value;
3293   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3294   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3295
3296   if (GNUNET_YES != test_connected (n))
3297     return GNUNET_OK;
3298
3299   if (NULL != n->primary_address.address)
3300   {
3301     bandwidth_in = n->primary_address.bandwidth_in;
3302     bandwidth_out = n->primary_address.bandwidth_out;
3303   }
3304   else
3305   {
3306     bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3307     bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3308   }
3309   ic->cb (ic->cb_cls, &n->id,
3310           n->primary_address.address,
3311           bandwidth_in, bandwidth_out);
3312   return GNUNET_OK;
3313 }
3314
3315
3316 /**
3317  * Iterate over all connected neighbours.
3318  *
3319  * @param cb function to call
3320  * @param cb_cls closure for cb
3321  */
3322 void
3323 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3324 {
3325   struct IteratorContext ic;
3326
3327   if (NULL == neighbours)
3328     return; /* can happen during shutdown */
3329   ic.cb = cb;
3330   ic.cb_cls = cb_cls;
3331   GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3332 }
3333
3334
3335 /**
3336  * If we have an active connection to the given target, it must be shutdown.
3337  *
3338  * @param target peer to disconnect from
3339  */
3340 void
3341 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3342 {
3343   struct NeighbourMapEntry *n;
3344
3345   if (NULL == (n = lookup_neighbour (target)))
3346     return;  /* not active */
3347   if (GNUNET_YES == test_connected (n))
3348     GNUNET_STATISTICS_update (GST_stats,
3349                               gettext_noop
3350                               ("# disconnected from peer upon explicit request"), 1,
3351                               GNUNET_NO);
3352   disconnect_neighbour (n);
3353 }
3354
3355
3356 /**
3357  * Obtain current latency information for the given neighbour.
3358  *
3359  * @param peer to get the latency for
3360  * @return observed latency of the address, FOREVER if the
3361  *         the connection is not up
3362  */
3363 struct GNUNET_TIME_Relative
3364 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3365 {
3366   struct NeighbourMapEntry *n;
3367
3368   n = lookup_neighbour (peer);
3369   if (NULL == n)
3370     return GNUNET_TIME_UNIT_FOREVER_REL;
3371   switch (n->state)
3372   {
3373   case S_CONNECTED:
3374   case S_CONNECTED_SWITCHING_CONNECT_SENT:
3375   case S_CONNECTED_SWITCHING_BLACKLIST:
3376   case S_RECONNECT_SENT:
3377   case S_RECONNECT_ATS:
3378   case S_RECONNECT_BLACKLIST:
3379     return n->latency;
3380   case S_NOT_CONNECTED:
3381   case S_INIT_BLACKLIST:
3382   case S_INIT_ATS:
3383   case S_CONNECT_RECV_BLACKLIST_INBOUND:
3384   case S_CONNECT_RECV_ATS:
3385   case S_CONNECT_RECV_BLACKLIST:
3386   case S_CONNECT_RECV_ACK:
3387   case S_CONNECT_SENT:
3388   case S_DISCONNECT:
3389   case S_DISCONNECT_FINISHED:
3390     return GNUNET_TIME_UNIT_FOREVER_REL;
3391   default:
3392     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3393                 "Unhandled state `%s'\n",
3394                 print_state (n->state));
3395     GNUNET_break (0);
3396     break;
3397   }
3398   return GNUNET_TIME_UNIT_FOREVER_REL;
3399 }
3400
3401
3402 /**
3403  * Obtain current address information for the given neighbour.
3404  *
3405  * @param peer
3406  * @return address currently used
3407  */
3408 struct GNUNET_HELLO_Address *
3409 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3410 {
3411   struct NeighbourMapEntry *n;
3412
3413   n = lookup_neighbour (peer);
3414   if (NULL == n)
3415     return NULL;
3416   return n->primary_address.address;
3417 }
3418
3419
3420 /**
3421  * Initialize the neighbours subsystem.
3422  *
3423  * @param cls closure for callbacks
3424  * @param connect_cb function to call if we connect to a peer
3425  * @param disconnect_cb function to call if we disconnect from a peer
3426  * @param peer_address_cb function to call if we change an active address
3427  *                   of a neighbour
3428  * @param max_fds maximum number of fds to use
3429  */
3430 void
3431 GST_neighbours_start (void *cls,
3432                       NotifyConnect connect_cb,
3433                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3434                       GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3435                       unsigned int max_fds)
3436 {
3437   callback_cls = cls;
3438   connect_notify_cb = connect_cb;
3439   disconnect_notify_cb = disconnect_cb;
3440   address_change_cb = peer_address_cb;
3441   neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3442   util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3443       utilization_transmission, NULL);
3444 }
3445
3446
3447 /**
3448  * Disconnect from the given neighbour.
3449  *
3450  * @param cls unused
3451  * @param key hash of neighbour's public key (not used)
3452  * @param value the 'struct NeighbourMapEntry' of the neighbour
3453  * @return #GNUNET_OK (continue to iterate)
3454  */
3455 static int
3456 disconnect_all_neighbours (void *cls,
3457                            const struct GNUNET_PeerIdentity *key,
3458                            void *value)
3459 {
3460   struct NeighbourMapEntry *n = value;
3461
3462   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3463               "Disconnecting peer `%4s', %s\n",
3464               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3465   n->state = S_DISCONNECT_FINISHED;
3466   free_neighbour (n, GNUNET_NO);
3467   return GNUNET_OK;
3468 }
3469
3470
3471 /**
3472  * Cleanup the neighbours subsystem.
3473  */
3474 void
3475 GST_neighbours_stop ()
3476 {
3477   if (NULL == neighbours)
3478     return;
3479   if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3480   {
3481     GNUNET_SCHEDULER_cancel (util_transmission_tk);
3482     util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3483   }
3484
3485   GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3486                                          &disconnect_all_neighbours,
3487                                          NULL);
3488   GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3489   neighbours = NULL;
3490   callback_cls = NULL;
3491   connect_notify_cb = NULL;
3492   disconnect_notify_cb = NULL;
3493   address_change_cb = NULL;
3494 }
3495
3496
3497 /* end of file gnunet-service-transport_neighbours.c */