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