+ moved outbound quota setting to separate function
[oweals/gnunet.git] / src / transport / gnunet-service-transport_neighbours.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,2011 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file transport/gnunet-service-transport_neighbours.c
23  * @brief neighbour management
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
37
38
39 /**
40  * Size of the neighbour hash map.
41  */
42 #define NEIGHBOUR_TABLE_SIZE 256
43
44 /**
45  * How often must a peer violate bandwidth quotas before we start
46  * to simply drop its messages?
47  */
48 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
49
50 /**
51  * How often do we send KEEPALIVE messages to each of our neighbours and measure
52  * the latency with this neighbour?
53  * (idle timeout is 5 minutes or 300 seconds, so with 30s interval we
54  * send 10 keepalives in each interval, so 10 messages would need to be
55  * lost in a row for a disconnect).
56  */
57 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
58
59
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
61
62 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
63
64 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
65
66
67 /**
68  * Entry in neighbours.
69  */
70 struct NeighbourMapEntry;
71
72 /**
73  * Message a peer sends to another to indicate its
74  * preference for communicating via a particular
75  * session (and the desire to establish a real
76  * connection).
77  */
78 struct SessionConnectMessage
79 {
80   /**
81    * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
82    */
83   struct GNUNET_MessageHeader header;
84
85   /**
86    * Always zero.
87    */
88   uint32_t reserved GNUNET_PACKED;
89
90   /**
91    * Absolute time at the sender.  Only the most recent connect
92    * message implies which session is preferred by the sender.
93    */
94   struct GNUNET_TIME_AbsoluteNBO timestamp;
95
96 };
97
98
99 struct SessionDisconnectMessage
100 {
101   /**
102    * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
103    */
104   struct GNUNET_MessageHeader header;
105
106   /**
107    * Always zero.
108    */
109   uint32_t reserved GNUNET_PACKED;
110
111   /**
112    * Purpose of the signature.  Extends over the timestamp.
113    * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
114    */
115   struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
116
117   /**
118    * Absolute time at the sender.  Only the most recent connect
119    * message implies which session is preferred by the sender.
120    */
121   struct GNUNET_TIME_AbsoluteNBO timestamp;
122
123   /**
124    * Public key of the sender.
125    */
126   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
127
128   /**
129    * Signature of the peer that sends us the disconnect.  Only
130    * valid if the timestamp is AFTER the timestamp from the
131    * corresponding 'CONNECT' message.
132    */
133   struct GNUNET_CRYPTO_RsaSignature signature;
134
135 };
136
137
138 /**
139  * For each neighbour we keep a list of messages
140  * that we still want to transmit to the neighbour.
141  */
142 struct MessageQueue
143 {
144
145   /**
146    * This is a doubly linked list.
147    */
148   struct MessageQueue *next;
149
150   /**
151    * This is a doubly linked list.
152    */
153   struct MessageQueue *prev;
154
155   /**
156    * Once this message is actively being transmitted, which
157    * neighbour is it associated with?
158    */
159   struct NeighbourMapEntry *n;
160
161   /**
162    * Function to call once we're done.
163    */
164   GST_NeighbourSendContinuation cont;
165
166   /**
167    * Closure for 'cont'
168    */
169   void *cont_cls;
170
171   /**
172    * The message(s) we want to transmit, GNUNET_MessageHeader(s)
173    * stuck together in memory.  Allocated at the end of this struct.
174    */
175   const char *message_buf;
176
177   /**
178    * Size of the message buf
179    */
180   size_t message_buf_size;
181
182   /**
183    * At what time should we fail?
184    */
185   struct GNUNET_TIME_Absolute timeout;
186
187 };
188
189
190 enum State
191 {
192   /**
193    * fresh peer or completely disconnected 
194    */
195   S_NOT_CONNECTED,
196
197   /**
198    * sent CONNECT message to other peer, waiting for CONNECT_ACK 
199    */
200   S_CONNECT_SENT,
201
202   /**
203    * received CONNECT message to other peer, sending CONNECT_ACK 
204    */
205   S_CONNECT_RECV,
206
207   /**
208    * received ACK or payload 
209    */
210   S_CONNECTED,
211
212   /**
213    * connection ended, fast reconnect
214    */
215   S_FAST_RECONNECT,
216
217   /**
218    * Disconnect in progress 
219    */
220   S_DISCONNECT
221 };
222
223 enum Address_State
224 {
225   USED,
226   UNUSED,
227   FRESH,
228 };
229
230 /**
231  * Entry in neighbours.
232  */
233 struct NeighbourMapEntry
234 {
235
236   /**
237    * Head of list of messages we would like to send to this peer;
238    * must contain at most one message per client.
239    */
240   struct MessageQueue *messages_head;
241
242   /**
243    * Tail of list of messages we would like to send to this peer; must
244    * contain at most one message per client.
245    */
246   struct MessageQueue *messages_tail;
247
248   /**
249    * Performance data for the peer.
250    */
251   //struct GNUNET_ATS_Information *ats;
252
253   /**
254    * Are we currently trying to send a message? If so, which one?
255    */
256   struct MessageQueue *is_active;
257
258   /**
259    * Active session for communicating with the peer.
260    */
261   struct Session *session;
262
263   /**
264    * Address we currently use.
265    */
266   struct GNUNET_HELLO_Address *address;
267
268   /**
269    * Identity of this neighbour.
270    */
271   struct GNUNET_PeerIdentity id;
272
273   /**
274    * ID of task scheduled to run when this peer is about to
275    * time out (will free resources associated with the peer).
276    */
277   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
278
279   /**
280    * ID of task scheduled to send keepalives.
281    */
282   GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
283
284   /**
285    * ID of task scheduled to run when we should try transmitting
286    * the head of the message queue.
287    */
288   GNUNET_SCHEDULER_TaskIdentifier transmission_task;
289
290   /**
291    * Tracker for inbound bandwidth.
292    */
293   struct GNUNET_BANDWIDTH_Tracker in_tracker;
294
295   /**
296    * Inbound bandwidth from ATS, activated when connection is up
297    */
298   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
299
300   /**
301    * Inbound bandwidth from ATS, activated when connection is up
302    */
303   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
304
305   /**
306    * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
307    */
308   struct GNUNET_TIME_Absolute connect_ts;
309
310   /**
311    * When did we sent the last keep-alive message?
312    */
313   struct GNUNET_TIME_Absolute keep_alive_sent;
314
315   /**
316    * Latest calculated latency value
317    */
318   struct GNUNET_TIME_Relative latency;
319
320   /**
321    * Timeout for ATS
322    * We asked ATS for a new address for this peer
323    */
324   GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
325
326   /**
327    * Task the resets the peer state after due to an pending
328    * unsuccessful connection setup
329    */
330   GNUNET_SCHEDULER_TaskIdentifier state_reset;
331
332   /**
333    * How often has the other peer (recently) violated the inbound
334    * traffic limit?  Incremented by 10 per violation, decremented by 1
335    * per non-violation (for each time interval).
336    */
337   unsigned int quota_violation_count;
338
339
340   /**
341    * The current state of the peer
342    * Element of enum State
343    */
344   int state;
345
346   /**
347    * Did we sent an KEEP_ALIVE message and are we expecting a response?
348    */
349   int expect_latency_response;
350   int address_state;
351 };
352
353
354 /**
355  * All known neighbours and their HELLOs.
356  */
357 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
358
359 /**
360  * Closure for connect_notify_cb and disconnect_notify_cb
361  */
362 static void *callback_cls;
363
364 /**
365  * Function to call when we connected to a neighbour.
366  */
367 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
368
369 /**
370  * Function to call when we disconnected from a neighbour.
371  */
372 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
373
374 /**
375  * counter for connected neighbours
376  */
377 static int neighbours_connected;
378
379 /**
380  * Lookup a neighbour entry in the neighbours hash map.
381  *
382  * @param pid identity of the peer to look up
383  * @return the entry, NULL if there is no existing record
384  */
385 static struct NeighbourMapEntry *
386 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
387 {
388   return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
389 }
390
391 #define change_state(n, state, ...) change (n, state, __LINE__)
392
393 static int
394 is_connecting (struct NeighbourMapEntry *n)
395 {
396   if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
397     return GNUNET_YES;
398   return GNUNET_NO;
399 }
400
401 static int
402 is_connected (struct NeighbourMapEntry *n)
403 {
404   if (n->state == S_CONNECTED)
405     return GNUNET_YES;
406   return GNUNET_NO;
407 }
408
409 static int
410 is_disconnecting (struct NeighbourMapEntry *n)
411 {
412   if (n->state == S_DISCONNECT)
413     return GNUNET_YES;
414   return GNUNET_NO;
415 }
416
417 static const char *
418 print_state (int state)
419 {
420   switch (state)
421   {
422   case S_CONNECTED:
423     return "S_CONNECTED";
424     break;
425   case S_CONNECT_RECV:
426     return "S_CONNECT_RECV";
427     break;
428   case S_CONNECT_SENT:
429     return "S_CONNECT_SENT";
430     break;
431   case S_DISCONNECT:
432     return "S_DISCONNECT";
433     break;
434   case S_NOT_CONNECTED:
435     return "S_NOT_CONNECTED";
436     break;
437   case S_FAST_RECONNECT:
438     return "S_FAST_RECONNECT";
439     break;
440   default:
441     GNUNET_break (0);
442     break;
443   }
444   return NULL;
445 }
446
447 static int
448 change (struct NeighbourMapEntry *n, int state, int line);
449
450 static void
451 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
452
453 static void
454 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
455 {
456   struct NeighbourMapEntry *n = cls;
457
458   n->state_reset = GNUNET_SCHEDULER_NO_TASK;
459
460 #if DEBUG_TRANSPORT
461 #endif
462
463   GNUNET_STATISTICS_update (GST_stats,
464                             gettext_noop
465                             ("# failed connection attempts due to timeout"), 1,
466                             GNUNET_NO);
467
468   /* resetting state */
469   n->state = S_NOT_CONNECTED;
470
471   /* destroying address */
472   GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
473
474   /* request new address */
475   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
476     GNUNET_SCHEDULER_cancel (n->ats_suggest);
477   n->ats_suggest =
478       GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
479                                     n);
480   GNUNET_ATS_suggest_address (GST_ats, &n->id);
481 }
482
483 static int
484 change (struct NeighbourMapEntry *n, int state, int line)
485 {
486   /* allowed transitions */
487   int allowed = GNUNET_NO;
488
489   switch (n->state)
490   {
491   case S_NOT_CONNECTED:
492     if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
493         (state == S_DISCONNECT))
494       allowed = GNUNET_YES;
495     break;
496   case S_CONNECT_RECV:
497     allowed = GNUNET_YES;
498     break;
499   case S_CONNECT_SENT:
500     allowed = GNUNET_YES;
501     break;
502   case S_CONNECTED:
503     if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
504       allowed = GNUNET_YES;
505     break;
506   case S_DISCONNECT:
507     break;
508   case S_FAST_RECONNECT:
509     if ((state == S_CONNECTED) || (state == S_DISCONNECT))
510       allowed = GNUNET_YES;
511     break;
512   default:
513     GNUNET_break (0);
514     break;
515   }
516   if (allowed == GNUNET_NO)
517   {
518     char *old = GNUNET_strdup (print_state (n->state));
519     char *new = GNUNET_strdup (print_state (state));
520     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521                 "Illegal state transition from `%s' to `%s' in line %u \n", old,
522                 new, line);
523     GNUNET_break (0);
524     GNUNET_free (old);
525     GNUNET_free (new);
526     return GNUNET_SYSERR;
527   }
528 #if DEBUG_TRANSPORT
529
530   {
531     char *old = GNUNET_strdup (print_state (n->state));
532     char *new = GNUNET_strdup (print_state (state));
533     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534                 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
535                 GNUNET_i2s (&n->id), n, old, new, line);
536     GNUNET_free (old);
537     GNUNET_free (new);
538   }
539 #endif
540   n->state = state;
541
542   switch (n->state)
543   {
544   case S_FAST_RECONNECT:
545   case S_CONNECT_RECV:
546   case S_CONNECT_SENT:
547     if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
548       GNUNET_SCHEDULER_cancel (n->state_reset);
549     n->state_reset =
550       GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
551                                     n);   
552     break;
553   case S_CONNECTED:
554   case S_NOT_CONNECTED:
555   case S_DISCONNECT:
556     if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
557     {
558 #if DEBUG_TRANSPORT
559       char *old = GNUNET_strdup (print_state (n->state));
560       char *new = GNUNET_strdup (print_state (state));
561       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562                   "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
563                   GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
564                   old, new);
565       GNUNET_free (old);
566       GNUNET_free (new);
567 #endif
568       GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
569       GNUNET_SCHEDULER_cancel (n->state_reset);
570       n->state_reset = GNUNET_SCHEDULER_NO_TASK;
571     }
572     break;
573
574   default:
575     GNUNET_assert (0);
576   }
577   
578
579
580   return GNUNET_OK;
581 }
582
583 static ssize_t
584 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
585                   size_t msgbuf_size, uint32_t priority,
586                   struct GNUNET_TIME_Relative timeout, struct Session *session,
587                   const struct GNUNET_HELLO_Address *address,
588                   int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
589                   void *cont_cls)
590 {
591   struct GNUNET_TRANSPORT_PluginFunctions *papi;
592   size_t ret = GNUNET_SYSERR;
593
594   /* FIXME : ats returns an address with all values 0 */
595   if (address == NULL)
596   {
597     if (cont != NULL)
598       cont (cont_cls, target, GNUNET_SYSERR);
599     return GNUNET_SYSERR;
600   }
601
602   if ((session == NULL) && (address->address_length == 0))
603   {
604     if (cont != NULL)
605       cont (cont_cls, target, GNUNET_SYSERR);
606     return GNUNET_SYSERR;
607   }
608
609   papi = GST_plugins_find (address->transport_name);
610   if (papi == NULL)
611   {
612     if (cont != NULL)
613       cont (cont_cls, target, GNUNET_SYSERR);
614     return GNUNET_SYSERR;
615   }
616
617   ret =
618       papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
619                   address->address, 
620                   address->address_length, GNUNET_YES, cont, cont_cls);
621
622   if (ret == -1)
623   {
624     if (cont != NULL)
625       cont (cont_cls, target, GNUNET_SYSERR);
626   }
627   return ret;
628 }
629
630 /**
631  * Task invoked to start a transmission to another peer.
632  *
633  * @param cls the 'struct NeighbourMapEntry'
634  * @param tc scheduler context
635  */
636 static void
637 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
638
639
640 /**
641  * We're done with our transmission attempt, continue processing.
642  *
643  * @param cls the 'struct MessageQueue' of the message
644  * @param receiver intended receiver
645  * @param success whether it worked or not
646  */
647 static void
648 transmit_send_continuation (void *cls,
649                             const struct GNUNET_PeerIdentity *receiver,
650                             int success)
651 {
652   struct MessageQueue *mq;
653   struct NeighbourMapEntry *n;
654
655   mq = cls;
656   n = mq->n;
657   if (NULL != n)
658   {
659     GNUNET_assert (n->is_active == mq);
660     n->is_active = NULL;
661     if (success == GNUNET_YES)
662     {
663       GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
664       n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
665     }
666   }
667 #if DEBUG_TRANSPORT
668   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
669               ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
670               (success == GNUNET_OK) ? "successful" : "FAILED");
671 #endif
672   if (NULL != mq->cont)
673     mq->cont (mq->cont_cls, success);
674   GNUNET_free (mq);
675 }
676
677
678 /**
679  * Check the ready list for the given neighbour and if a plugin is
680  * ready for transmission (and if we have a message), do so!
681  *
682  * @param n target peer for which to transmit
683  */
684 static void
685 try_transmission_to_peer (struct NeighbourMapEntry *n)
686 {
687   struct MessageQueue *mq;
688   struct GNUNET_TIME_Relative timeout;
689   ssize_t ret;
690
691   if (n->is_active != NULL)
692   {
693     GNUNET_break (0);
694     return;                     /* transmission already pending */
695   }
696   if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
697   {
698     GNUNET_break (0);
699     return;                     /* currently waiting for bandwidth */
700   }
701   while (NULL != (mq = n->messages_head))
702   {
703     timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
704     if (timeout.rel_value > 0)
705       break;
706     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
707     n->is_active = mq;
708     mq->n = n;
709     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);     /* timeout */
710   }
711   if (NULL == mq)
712     return;                     /* no more messages */
713
714   if (n->address == NULL)
715   {
716     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717                 "No address for peer `%s'\n",
718                 GNUNET_i2s (&n->id));
719     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
720     GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
721     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
722     return;
723   }
724
725   if (GST_plugins_find (n->address->transport_name) == NULL)
726   {
727     GNUNET_break (0);
728     return;
729   }
730   GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
731   n->is_active = mq;
732   mq->n = n;
733
734   if ((n->address->address_length == 0) && (n->session == NULL))
735   {
736     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
737                 "No address for peer `%s'\n",
738                 GNUNET_i2s (&n->id));
739     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
740     GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
741     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
742     return;
743   }
744
745   ret =
746       send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
747                         timeout, n->session, n->address, 
748                         GNUNET_YES, &transmit_send_continuation,
749                         mq);
750   if (ret == -1)
751   {
752     /* failure, but 'send' would not call continuation in this case,
753      * so we need to do it here! */
754     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
755   }
756
757 }
758
759
760 /**
761  * Task invoked to start a transmission to another peer.
762  *
763  * @param cls the 'struct NeighbourMapEntry'
764  * @param tc scheduler context
765  */
766 static void
767 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
768 {
769   struct NeighbourMapEntry *n = cls;
770
771   GNUNET_assert (NULL != lookup_neighbour (&n->id));
772   n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
773   try_transmission_to_peer (n);
774 }
775
776
777 /**
778  * Initialize the neighbours subsystem.
779  *
780  * @param cls closure for callbacks
781  * @param connect_cb function to call if we connect to a peer
782  * @param disconnect_cb function to call if we disconnect from a peer
783  */
784 void
785 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
786                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
787 {
788   callback_cls = cls;
789   connect_notify_cb = connect_cb;
790   disconnect_notify_cb = disconnect_cb;
791   neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
792 }
793
794
795 static void
796 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
797                       int result)
798 {
799 #if DEBUG_TRANSPORT
800   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801               "Sending DISCONNECT message to peer `%4s': %i\n",
802               GNUNET_i2s (target), result);
803 #endif
804 }
805
806
807 static int
808 send_disconnect (const struct GNUNET_PeerIdentity *target,
809                  const struct GNUNET_HELLO_Address *address,
810                  struct Session *session)
811 {
812   size_t ret;
813   struct SessionDisconnectMessage disconnect_msg;
814
815 #if DEBUG_TRANSPORT
816   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817               "Sending DISCONNECT message to peer `%4s'\n",
818               GNUNET_i2s (target));
819 #endif
820
821   disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
822   disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
823   disconnect_msg.reserved = htonl (0);
824   disconnect_msg.purpose.size =
825       htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
826              sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
827              sizeof (struct GNUNET_TIME_AbsoluteNBO));
828   disconnect_msg.purpose.purpose =
829       htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
830   disconnect_msg.timestamp =
831       GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
832   disconnect_msg.public_key = GST_my_public_key;
833   GNUNET_assert (GNUNET_OK ==
834                  GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
835                                          &disconnect_msg.purpose,
836                                          &disconnect_msg.signature));
837
838   ret =
839       send_with_plugin (target, (const char *) &disconnect_msg,
840                         sizeof (disconnect_msg), UINT32_MAX,
841                         GNUNET_TIME_UNIT_FOREVER_REL, session, address,
842                         GNUNET_YES,
843                         &send_disconnect_cont, NULL);
844
845   if (ret == GNUNET_SYSERR)
846     return GNUNET_SYSERR;
847
848   GNUNET_STATISTICS_update (GST_stats,
849                             gettext_noop
850                             ("# peers disconnected due to external request"), 1,
851                             GNUNET_NO);
852   return GNUNET_OK;
853 }
854
855
856 /**
857  * Disconnect from the given neighbour, clean up the record.
858  *
859  * @param n neighbour to disconnect from
860  */
861 static void
862 disconnect_neighbour (struct NeighbourMapEntry *n)
863 {
864   struct MessageQueue *mq;
865   int previous_state;
866
867   previous_state = n->state;
868
869   if (is_disconnecting (n))
870     return;
871
872
873   /* send DISCONNECT MESSAGE */
874   if ((previous_state == S_CONNECTED) || is_connecting (n))
875   {
876     if (GNUNET_OK ==
877         send_disconnect (&n->id, n->address,
878                          n->session))
879       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
880                   GNUNET_i2s (&n->id));
881     else
882       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883                   "Could not send DISCONNECT_MSG to `%s'\n",
884                   GNUNET_i2s (&n->id));
885   }
886
887   change_state (n, S_DISCONNECT);
888
889   if (previous_state == S_CONNECTED)
890   {
891     GNUNET_assert (NULL != n->address);
892     if (n->address_state == USED)
893     {
894       GST_validation_set_address_use (&n->id,
895                                       n->address,
896                                       n->session,
897                                       GNUNET_NO);
898
899       GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
900       n->address_state = UNUSED;
901     }
902   }
903
904   if (n->address != NULL)
905   {
906     struct GNUNET_TRANSPORT_PluginFunctions *papi;
907     papi = GST_plugins_find (n->address->transport_name);
908     if (papi != NULL)
909       papi->disconnect (papi->cls, &n->id);
910   }
911   while (NULL != (mq = n->messages_head))
912   {
913     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
914     if (NULL != mq->cont)
915       mq->cont (mq->cont_cls, GNUNET_SYSERR);
916     GNUNET_free (mq);
917   }
918   if (NULL != n->is_active)
919   {
920     n->is_active->n = NULL;
921     n->is_active = NULL;
922   }
923
924   switch (previous_state) {
925     case S_CONNECTED:
926 //      GNUNET_assert (neighbours_connected > 0);
927       neighbours_connected--;
928       GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
929       GNUNET_SCHEDULER_cancel (n->keepalive_task);
930       n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
931       GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
932                                 GNUNET_NO);
933       disconnect_notify_cb (callback_cls, &n->id);
934       break;
935     case S_FAST_RECONNECT:
936       GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
937                                 GNUNET_NO);
938       GNUNET_STATISTICS_update (GST_stats,
939                                 gettext_noop
940                                 ("# fast reconnects failed"),
941                                 1, GNUNET_NO);
942       disconnect_notify_cb (callback_cls, &n->id);
943     default:
944       break;
945   }
946
947   GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
948
949   GNUNET_assert (GNUNET_YES ==
950                  GNUNET_CONTAINER_multihashmap_remove (neighbours,
951                                                        &n->id.hashPubKey, n));
952   if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
953   {
954     GNUNET_SCHEDULER_cancel (n->ats_suggest);
955     n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
956   }
957   if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
958   {
959     GNUNET_SCHEDULER_cancel (n->timeout_task);
960     n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
961   }
962   if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
963   {
964     GNUNET_SCHEDULER_cancel (n->transmission_task);
965     n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
966   }
967   if (NULL != n->address)
968   {
969     GNUNET_HELLO_address_free (n->address);
970     n->address = NULL;
971   }
972   n->session = NULL;
973   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
974               GNUNET_i2s (&n->id), n);
975   GNUNET_free (n);
976 }
977
978
979 /**
980  * Peer has been idle for too long. Disconnect.
981  *
982  * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
983  * @param tc scheduler context
984  */
985 static void
986 neighbour_timeout_task (void *cls,
987                         const struct GNUNET_SCHEDULER_TaskContext *tc)
988 {
989   struct NeighbourMapEntry *n = cls;
990
991   n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
992
993   GNUNET_STATISTICS_update (GST_stats,
994                             gettext_noop
995                             ("# peers disconnected due to timeout"), 1,
996                             GNUNET_NO);
997   disconnect_neighbour (n);
998 }
999
1000
1001 /**
1002  * Send another keepalive message.
1003  *
1004  * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1005  * @param tc scheduler context
1006  */
1007 static void
1008 neighbour_keepalive_task (void *cls,
1009                           const struct GNUNET_SCHEDULER_TaskContext *tc)
1010 {
1011   struct NeighbourMapEntry *n = cls;
1012   struct GNUNET_MessageHeader m;
1013   int ret;
1014
1015   n->keepalive_task =
1016       GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1017                                     &neighbour_keepalive_task, n);
1018
1019   GNUNET_assert (S_CONNECTED == n->state);
1020   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1021                             GNUNET_NO);
1022   m.size = htons (sizeof (struct GNUNET_MessageHeader));
1023   m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1024
1025
1026   ret = send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1027                     UINT32_MAX /* priority */ ,
1028                     GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address, 
1029                     GNUNET_YES, NULL, NULL);
1030
1031   n->expect_latency_response = GNUNET_NO;
1032   n->keep_alive_sent = GNUNET_TIME_absolute_get_zero();
1033   if (ret != GNUNET_SYSERR)
1034   {
1035     n->expect_latency_response = GNUNET_YES;
1036     n->keep_alive_sent = GNUNET_TIME_absolute_get();
1037   }
1038
1039 }
1040
1041
1042 /**
1043  * Disconnect from the given neighbour.
1044  *
1045  * @param cls unused
1046  * @param key hash of neighbour's public key (not used)
1047  * @param value the 'struct NeighbourMapEntry' of the neighbour
1048  */
1049 static int
1050 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1051 {
1052   struct NeighbourMapEntry *n = value;
1053
1054 #if DEBUG_TRANSPORT
1055   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1056               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1057 #endif
1058   if (S_CONNECTED == n->state)
1059     GNUNET_STATISTICS_update (GST_stats,
1060                               gettext_noop
1061                               ("# peers disconnected due to global disconnect"),
1062                               1, GNUNET_NO);
1063   disconnect_neighbour (n);
1064   return GNUNET_OK;
1065 }
1066
1067
1068 static void
1069 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1070 {
1071   struct NeighbourMapEntry *n = cls;
1072
1073   n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1074
1075   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076               " ATS did not suggested address to connect to peer `%s'\n",
1077               GNUNET_i2s (&n->id));
1078
1079   disconnect_neighbour (n);
1080 }
1081
1082 /**
1083  * Cleanup the neighbours subsystem.
1084  */
1085 void
1086 GST_neighbours_stop ()
1087 {
1088   // This can happen during shutdown
1089   if (neighbours == NULL)
1090   {
1091     return;
1092   }
1093
1094   GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1095                                          NULL);
1096   GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1097 //  GNUNET_assert (neighbours_connected == 0);
1098   neighbours = NULL;
1099   callback_cls = NULL;
1100   connect_notify_cb = NULL;
1101   disconnect_notify_cb = NULL;
1102 }
1103
1104 struct ContinutionContext
1105 {
1106   struct GNUNET_HELLO_Address *address;
1107
1108   struct Session *session;
1109 };
1110
1111 static void send_outbound_quota (const struct GNUNET_PeerIdentity *target, struct GNUNET_BANDWIDTH_Value32NBO quota)
1112 {
1113    struct QuotaSetMessage q_msg;
1114 #if DEBUG_TRANSPORT
1115     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1116                 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1117                 ntohl (quota.value__), GNUNET_i2s (target));
1118 #endif
1119     q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1120     q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1121     q_msg.quota = quota;
1122     q_msg.peer = (*target);
1123     GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1124 }
1125
1126 /**
1127  * We tried to send a SESSION_CONNECT message to another peer.  If this
1128  * succeeded, we change the state.  If it failed, we should tell
1129  * ATS to not use this address anymore (until it is re-validated).
1130  *
1131  * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1132  * @param success GNUNET_OK on success
1133  */
1134 static void
1135 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1136                            int success)
1137 {
1138   struct ContinutionContext * cc = cls;
1139   struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1140   
1141   if (GNUNET_YES != success)
1142     GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1143     //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1144   if ( (NULL == neighbours) ||
1145        (NULL == n) ||
1146        (n->state == S_DISCONNECT))
1147   {
1148     GNUNET_HELLO_address_free (cc->address);
1149     GNUNET_free (cc);
1150     return;
1151   }
1152
1153   if ((GNUNET_YES == success) &&
1154      ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1155   {
1156     change_state (n, S_CONNECT_SENT);
1157     GNUNET_HELLO_address_free (cc->address);
1158     GNUNET_free (cc);
1159     return;
1160   }
1161
1162   if ((GNUNET_NO == success) &&
1163      ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1164   {
1165 #if DEBUG_TRANSPORT
1166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167                 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1168                 GNUNET_i2s (&n->id),
1169                 GST_plugins_a2s (n->address),
1170                 n->session);
1171 #endif
1172     change_state (n, S_NOT_CONNECTED);
1173     GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1174     //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1175
1176     if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1177       GNUNET_SCHEDULER_cancel (n->ats_suggest);
1178     n->ats_suggest =
1179       GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1180                                     n);
1181     GNUNET_ATS_suggest_address (GST_ats, &n->id);
1182   }
1183   GNUNET_HELLO_address_free (cc->address);
1184   GNUNET_free (cc);
1185 }
1186
1187 /**
1188  * We tried to switch addresses with an peer already connected. If it failed,
1189  * we should tell ATS to not use this address anymore (until it is re-validated).
1190  *
1191  * @param cls the 'struct NeighbourMapEntry'
1192  * @param success GNUNET_OK on success
1193  */
1194 static void
1195 send_switch_address_continuation (void *cls,
1196                                   const struct GNUNET_PeerIdentity *target,
1197                                   int success)
1198 {
1199   struct ContinutionContext * cc = cls;
1200   struct NeighbourMapEntry *n;
1201
1202   if (neighbours == NULL)
1203   {
1204     GNUNET_HELLO_address_free (cc->address);
1205     GNUNET_free (cc);
1206     return;                     /* neighbour is going away */
1207   }
1208
1209   n = lookup_neighbour(&cc->address->peer);
1210   if ((n == NULL) || (is_disconnecting (n)))
1211   {
1212     GNUNET_HELLO_address_free (cc->address);
1213     GNUNET_free (cc);
1214     return;                     /* neighbour is going away */
1215   }
1216
1217   GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1218   if (GNUNET_YES != success)
1219   {
1220 #if DEBUG_TRANSPORT
1221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222                 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1223                 GNUNET_i2s (&n->id), 
1224                 GST_plugins_a2s (n->address), n->session);
1225 #endif
1226     GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1227     //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1228
1229     if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1230       GNUNET_SCHEDULER_cancel (n->ats_suggest);
1231     n->ats_suggest =
1232         GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1233                                       n);
1234     GNUNET_ATS_suggest_address (GST_ats, &n->id);
1235     GNUNET_HELLO_address_free (cc->address);
1236     GNUNET_free (cc);
1237     return;
1238   }
1239   /* Tell ATS that switching addresses was successful */
1240   switch (n->state) {
1241     case S_CONNECTED:
1242       GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
1243       break;
1244     case S_FAST_RECONNECT:
1245 #if DEBUG_TRANSPORT
1246       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247                   "Successful fast reconnect to peer `%s'\n", GNUNET_i2s (&n->id));
1248 #endif
1249       change_state (n, S_CONNECTED);
1250       neighbours_connected++;
1251       GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1252                                 GNUNET_NO);
1253
1254       if (n->address_state == FRESH)
1255       {
1256           GST_validation_set_address_use (&n->id,
1257                     cc->address,
1258                     cc->session,
1259                     GNUNET_YES);
1260           n->address_state = USED;
1261       }
1262       GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1263
1264       if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1265             n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1266
1267       /* Updating quotas */
1268       GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1269       send_outbound_quota(target, n->bandwidth_out);
1270
1271     default:
1272       break;
1273   }
1274   GNUNET_HELLO_address_free (cc->address);
1275   GNUNET_free (cc);
1276 }
1277
1278 /**
1279  * We tried to send a SESSION_CONNECT message to another peer.  If this
1280  * succeeded, we change the state.  If it failed, we should tell
1281  * ATS to not use this address anymore (until it is re-validated).
1282  *
1283  * @param cls the 'struct NeighbourMapEntry'
1284  * @param success GNUNET_OK on success
1285  */
1286 static void
1287 send_connect_ack_continuation (void *cls,
1288                                const struct GNUNET_PeerIdentity *target,
1289                                int success)
1290 {
1291   struct ContinutionContext * cc = cls;
1292   struct NeighbourMapEntry *n;
1293
1294   if (neighbours == NULL)
1295   {
1296     GNUNET_HELLO_address_free (cc->address);
1297     GNUNET_free (cc);
1298     return;                     /* neighbour is going away */
1299   }
1300
1301   n = lookup_neighbour(&cc->address->peer);
1302   if ((n == NULL) || (is_disconnecting (n)))
1303   {
1304     GNUNET_HELLO_address_free (cc->address);
1305     GNUNET_free (cc);
1306     return;                     /* neighbour is going away */
1307   }
1308
1309   if (GNUNET_YES == success)
1310   {
1311     GNUNET_HELLO_address_free (cc->address);
1312     GNUNET_free (cc);
1313     return;                     /* sending successful */
1314   }
1315
1316   /* sending failed, ask for next address  */
1317 #if DEBUG_TRANSPORT
1318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319               "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1320               GNUNET_i2s (&n->id), 
1321               GST_plugins_a2s (n->address),
1322               n->session);
1323 #endif
1324   change_state (n, S_NOT_CONNECTED);
1325
1326   GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1327   //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1328
1329   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1330     GNUNET_SCHEDULER_cancel (n->ats_suggest);
1331   n->ats_suggest =
1332       GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1333                                     n);
1334   GNUNET_ATS_suggest_address (GST_ats, &n->id);
1335
1336   GNUNET_HELLO_address_free (cc->address);
1337   GNUNET_free (cc);
1338 }
1339
1340 /**
1341  * For an existing neighbour record, set the active connection to
1342  * the given address.
1343  *
1344  * @param peer identity of the peer to switch the address for
1345  * @param address address of the other peer, NULL if other peer
1346  *                       connected to us
1347  * @param session session to use (or NULL)
1348  * @param ats performance data
1349  * @param ats_count number of entries in ats
1350  * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1351  *         connection is not up (yet)
1352  */
1353 int
1354 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1355                                        const struct GNUNET_HELLO_Address *address,
1356                                        struct Session *session,
1357                                        const struct GNUNET_ATS_Information *ats,
1358                                        uint32_t ats_count,
1359                                        struct GNUNET_BANDWIDTH_Value32NBO
1360                                        bandwidth_in,
1361                                        struct GNUNET_BANDWIDTH_Value32NBO
1362                                        bandwidth_out)
1363 {
1364   struct NeighbourMapEntry *n;
1365   struct SessionConnectMessage connect_msg;
1366   struct ContinutionContext * cc;
1367   size_t msg_len;
1368   size_t ret;
1369
1370   if (neighbours == NULL)
1371   {
1372     /* This can happen during shutdown */
1373     return GNUNET_NO;
1374   }
1375   n = lookup_neighbour (peer);
1376   if (NULL == n)
1377     return GNUNET_NO;
1378   if (n->state == S_DISCONNECT)
1379   {
1380     /* We are disconnecting, nothing to do here */
1381     return GNUNET_NO;
1382   }
1383   GNUNET_assert (address->transport_name != NULL);
1384   if ( (session == NULL) && (0 == address->address_length) )
1385   {
1386     GNUNET_break_op (0);
1387     GNUNET_ATS_address_destroyed (GST_ats, address, session);
1388     GNUNET_ATS_suggest_address (GST_ats, peer);
1389     return GNUNET_NO;
1390   }
1391
1392   /* checks successful and neighbour != NULL */
1393 #if DEBUG_TRANSPORT
1394   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1395               "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1396               GST_plugins_a2s (address),
1397               session,
1398               GNUNET_i2s (peer),
1399               print_state(n->state));
1400 #endif
1401   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1402   {
1403     GNUNET_SCHEDULER_cancel (n->ats_suggest);
1404     n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1405   }
1406   /* do not switch addresses just update quotas */
1407   if ( (n->state == S_CONNECTED) && 
1408        (NULL != n->address) &&
1409        (0 == GNUNET_HELLO_address_cmp (address,
1410                                        n->address)) &&
1411        (n->session == session) )
1412   {
1413     n->bandwidth_in = bandwidth_in;
1414     n->bandwidth_out = bandwidth_out;
1415     GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1416     send_outbound_quota(peer, n->bandwidth_out);
1417     return GNUNET_NO;    
1418   }
1419   if (n->state == S_CONNECTED)
1420   {
1421     /* mark old address as no longer used */
1422     GNUNET_assert (NULL != n->address);
1423     if (n->address_state == USED)
1424     {
1425       GST_validation_set_address_use (&n->id,
1426                                       n->address,
1427                                       n->session,
1428                                       GNUNET_NO);
1429       GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1430       n->address_state = UNUSED;
1431     }
1432
1433   }
1434
1435   /* set new address */
1436   if (NULL != n->address)
1437     GNUNET_HELLO_address_free (n->address);
1438   n->address = GNUNET_HELLO_address_copy (address);
1439   n->address_state = FRESH;
1440   n->session = session;
1441   n->bandwidth_in = bandwidth_in;
1442   n->bandwidth_out = bandwidth_out;
1443   GNUNET_SCHEDULER_cancel (n->timeout_task);
1444   n->timeout_task =
1445       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1446                                     &neighbour_timeout_task, n);
1447   switch (n->state)
1448   {
1449   case S_NOT_CONNECTED:  
1450   case S_CONNECT_SENT:
1451     msg_len = sizeof (struct SessionConnectMessage);
1452     connect_msg.header.size = htons (msg_len);
1453     connect_msg.header.type =
1454         htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1455     connect_msg.reserved = htonl (0);
1456     connect_msg.timestamp =
1457         GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1458
1459     cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1460     cc->session = session;
1461     cc->address = GNUNET_HELLO_address_copy (address);
1462     ret =
1463         send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1464                           UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1465                           address, GNUNET_YES,
1466                           &send_connect_continuation, 
1467                           cc);
1468     return GNUNET_NO;
1469   case S_CONNECT_RECV:
1470     /* We received a CONNECT message and asked ATS for an address */
1471     msg_len = sizeof (struct SessionConnectMessage);
1472     connect_msg.header.size = htons (msg_len);
1473     connect_msg.header.type =
1474         htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1475     connect_msg.reserved = htonl (0);
1476     connect_msg.timestamp =
1477         GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1478     cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1479     cc->session = session;
1480     cc->address = GNUNET_HELLO_address_copy (address);
1481     ret =
1482         send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1483                           UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1484                           address, GNUNET_YES,
1485                           &send_connect_ack_continuation, cc);
1486     return GNUNET_NO;
1487   case S_CONNECTED:
1488   case S_FAST_RECONNECT:
1489     /* connected peer is switching addresses or tries fast reconnect*/
1490     msg_len = sizeof (struct SessionConnectMessage);
1491     connect_msg.header.size = htons (msg_len);
1492     connect_msg.header.type =
1493         htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1494     connect_msg.reserved = htonl (0);
1495     connect_msg.timestamp =
1496         GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1497     cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1498     cc->session = session;
1499     cc->address = GNUNET_HELLO_address_copy (address);
1500     ret =
1501         send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1502                           UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1503                           address, GNUNET_YES,
1504                           &send_switch_address_continuation, cc);
1505     if (ret == GNUNET_SYSERR)
1506     {
1507       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508                   "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1509                   GNUNET_i2s (peer), 
1510                   GST_plugins_a2s (address), session);
1511     }
1512     return GNUNET_NO;
1513   default:
1514     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1515                 "Invalid connection state to switch addresses %u \n", n->state);
1516     GNUNET_break_op (0);
1517     return GNUNET_NO;
1518   }
1519 }
1520
1521
1522 /**
1523  * Obtain current latency information for the given neighbour.
1524  *
1525  * @param peer 
1526  * @return observed latency of the address, FOREVER if the address was
1527  *         never successfully validated
1528  */
1529 struct GNUNET_TIME_Relative
1530 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1531 {
1532   struct NeighbourMapEntry *n;
1533
1534   n = lookup_neighbour (peer);
1535   if ( (NULL == n) ||
1536        ( (n->address == NULL) && (n->session == NULL) ) )
1537     return GNUNET_TIME_UNIT_FOREVER_REL;
1538
1539   return n->latency;
1540 }
1541
1542
1543 /**
1544  * Create an entry in the neighbour map for the given peer
1545  *
1546  * @param peer peer to create an entry for
1547  * @return new neighbour map entry
1548  */
1549 static struct NeighbourMapEntry *
1550 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1551 {
1552   struct NeighbourMapEntry *n;
1553
1554 #if DEBUG_TRANSPORT
1555   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556               "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1557 #endif
1558   n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1559   n->id = *peer;
1560   n->state = S_NOT_CONNECTED;
1561   n->latency = GNUNET_TIME_relative_get_forever();
1562   GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1563                                  GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1564                                  MAX_BANDWIDTH_CARRY_S);
1565   n->timeout_task =
1566       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1567                                     &neighbour_timeout_task, n);
1568   GNUNET_assert (GNUNET_OK ==
1569                  GNUNET_CONTAINER_multihashmap_put (neighbours,
1570                                                     &n->id.hashPubKey, n,
1571                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1572   return n;
1573 }
1574
1575
1576 /**
1577  * Try to create a connection to the given target (eventually).
1578  *
1579  * @param target peer to try to connect to
1580  */
1581 void
1582 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1583 {
1584   struct NeighbourMapEntry *n;
1585
1586   // This can happen during shutdown
1587   if (neighbours == NULL)
1588   {
1589     return;
1590   }
1591 #if DEBUG_TRANSPORT
1592   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1593               GNUNET_i2s (target));
1594 #endif
1595   if (0 ==
1596       memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1597   {
1598     /* my own hello */
1599     return;
1600   }
1601   n = lookup_neighbour (target);
1602
1603   if (NULL != n)
1604   {
1605     if ((S_CONNECTED == n->state) || (is_connecting (n)))
1606       return;                   /* already connecting or connected */
1607     if (is_disconnecting (n))
1608       change_state (n, S_NOT_CONNECTED);
1609   }
1610
1611
1612   if (n == NULL)
1613     n = setup_neighbour (target);
1614 #if DEBUG_TRANSPORT
1615   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616               "Asking ATS for suggested address to connect to peer `%s'\n",
1617               GNUNET_i2s (&n->id));
1618 #endif
1619
1620   GNUNET_ATS_suggest_address (GST_ats, &n->id);
1621 }
1622
1623 /**
1624  * Test if we're connected to the given peer.
1625  *
1626  * @param target peer to test
1627  * @return GNUNET_YES if we are connected, GNUNET_NO if not
1628  */
1629 int
1630 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1631 {
1632   struct NeighbourMapEntry *n;
1633
1634   // This can happen during shutdown
1635   if (neighbours == NULL)
1636   {
1637     return GNUNET_NO;
1638   }
1639
1640   n = lookup_neighbour (target);
1641
1642   if ((NULL == n) || (S_CONNECTED != n->state))
1643     return GNUNET_NO;           /* not connected */
1644   return GNUNET_YES;
1645 }
1646
1647 /**
1648  * A session was terminated. Take note.
1649  *
1650  * @param peer identity of the peer where the session died
1651  * @param session session that is gone
1652  */
1653 void
1654 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1655                                    struct Session *session)
1656 {
1657   struct NeighbourMapEntry *n;
1658
1659   if (neighbours == NULL)
1660   {
1661     /* This can happen during shutdown */
1662     return;
1663   }
1664
1665 #if DEBUG_TRANSPORT
1666   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Session %X to peer `%s' ended \n",
1667               session, GNUNET_i2s (peer));
1668 #endif
1669
1670   n = lookup_neighbour (peer);
1671   if (NULL == n)
1672     return;
1673   if (session != n->session)
1674     return;                     /* doesn't affect us */
1675   if (n->state == S_CONNECTED)
1676   {
1677     if (n->address_state == USED)
1678     {
1679       GST_validation_set_address_use (&n->id,
1680                                       n->address,
1681                                       n->session,
1682                                       GNUNET_NO);
1683       GNUNET_ATS_address_in_use (GST_ats,n->address, n->session, GNUNET_NO);
1684       n->address_state = UNUSED;
1685     }
1686   }
1687
1688
1689   //GNUNET_ATS_address_destroyed(GST_ats, n->address, n->session);
1690
1691   if (NULL != n->address)
1692   {
1693     GNUNET_HELLO_address_free (n->address);
1694     n->address = NULL;
1695   }
1696   n->session = NULL;
1697   
1698   /* not connected anymore anyway, shouldn't matter */
1699   if ((S_CONNECTED != n->state) && (!is_connecting (n)))
1700     return;
1701
1702   /* connected, try fast reconnect */
1703 #if DEBUG_TRANSPORT
1704   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705               "Trying fast reconnect to peer `%s'\n", GNUNET_i2s (peer));
1706 #endif
1707   change_state (n, S_FAST_RECONNECT);
1708   GNUNET_assert (neighbours_connected > 0);
1709   neighbours_connected--;
1710
1711   if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1712   {
1713     GNUNET_SCHEDULER_cancel (n->keepalive_task);
1714     n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1715   }
1716
1717   /* We are connected, so ask ATS to switch addresses */
1718   GNUNET_SCHEDULER_cancel (n->timeout_task);
1719   n->timeout_task =
1720       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1721                                     &neighbour_timeout_task, n);
1722   /* try QUICKLY to re-establish a connection, reduce timeout! */
1723   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1724     GNUNET_SCHEDULER_cancel (n->ats_suggest);
1725   n->ats_suggest =
1726       GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1727                                     n);
1728   GNUNET_ATS_suggest_address (GST_ats, peer);
1729 }
1730
1731
1732 /**
1733  * Transmit a message to the given target using the active connection.
1734  *
1735  * @param target destination
1736  * @param msg message to send
1737  * @param msg_size number of bytes in msg
1738  * @param timeout when to fail with timeout
1739  * @param cont function to call when done
1740  * @param cont_cls closure for 'cont'
1741  */
1742 void
1743 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1744                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
1745                      GST_NeighbourSendContinuation cont, void *cont_cls)
1746 {
1747   struct NeighbourMapEntry *n;
1748   struct MessageQueue *mq;
1749
1750   // This can happen during shutdown
1751   if (neighbours == NULL)
1752   {
1753     return;
1754   }
1755
1756   n = lookup_neighbour (target);
1757   if ((n == NULL) || (!is_connected (n)))
1758   {
1759     GNUNET_STATISTICS_update (GST_stats,
1760                               gettext_noop
1761                               ("# messages not sent (no such peer or not connected)"),
1762                               1, GNUNET_NO);
1763 #if DEBUG_TRANSPORT
1764     if (n == NULL)
1765       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766                   "Could not send message to peer `%s': unknown neighbour",
1767                   GNUNET_i2s (target));
1768     else if (!is_connected (n))
1769       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770                   "Could not send message to peer `%s': not connected\n",
1771                   GNUNET_i2s (target));
1772 #endif
1773     if (NULL != cont)
1774       cont (cont_cls, GNUNET_SYSERR);
1775     return;
1776   }
1777
1778   if ((n->session == NULL) && (n->address == NULL) )
1779   {
1780     GNUNET_STATISTICS_update (GST_stats,
1781                               gettext_noop
1782                               ("# messages not sent (no such peer or not connected)"),
1783                               1, GNUNET_NO);
1784 #if DEBUG_TRANSPORT
1785     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786                 "Could not send message to peer `%s': no address available\n",
1787                 GNUNET_i2s (target));
1788 #endif
1789
1790     if (NULL != cont)
1791       cont (cont_cls, GNUNET_SYSERR);
1792     return;
1793   }
1794
1795   GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1796   GNUNET_STATISTICS_update (GST_stats,
1797                             gettext_noop
1798                             ("# bytes in message queue for other peers"),
1799                             msg_size, GNUNET_NO);
1800   mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1801   mq->cont = cont;
1802   mq->cont_cls = cont_cls;
1803   /* FIXME: this memcpy can be up to 7% of our total runtime! */
1804   memcpy (&mq[1], msg, msg_size);
1805   mq->message_buf = (const char *) &mq[1];
1806   mq->message_buf_size = msg_size;
1807   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1808   GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1809
1810   if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1811       (NULL == n->is_active))
1812     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1813 }
1814
1815
1816 /**
1817  * We have received a message from the given sender.  How long should
1818  * we delay before receiving more?  (Also used to keep the peer marked
1819  * as live).
1820  *
1821  * @param sender sender of the message
1822  * @param size size of the message
1823  * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1824  *                   GNUNET_NO if the neighbour is not connected or violates the quota,
1825  *                   GNUNET_SYSERR if the connection is not fully up yet
1826  * @return how long to wait before reading more from this sender
1827  */
1828 struct GNUNET_TIME_Relative
1829 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1830                                         *sender, ssize_t size, int *do_forward)
1831 {
1832   struct NeighbourMapEntry *n;
1833   struct GNUNET_TIME_Relative ret;
1834
1835   // This can happen during shutdown
1836   if (neighbours == NULL)
1837   {
1838     return GNUNET_TIME_UNIT_FOREVER_REL;
1839   }
1840
1841   n = lookup_neighbour (sender);
1842   if (n == NULL)
1843   {
1844     GST_neighbours_try_connect (sender);
1845     n = lookup_neighbour (sender);
1846     if (NULL == n)
1847     {
1848       GNUNET_STATISTICS_update (GST_stats,
1849                                 gettext_noop
1850                                 ("# messages discarded due to lack of neighbour record"),
1851                                 1, GNUNET_NO);
1852       *do_forward = GNUNET_NO;
1853       return GNUNET_TIME_UNIT_ZERO;
1854     }
1855   }
1856   if (!is_connected (n))
1857   {
1858     *do_forward = GNUNET_SYSERR;
1859     return GNUNET_TIME_UNIT_ZERO;
1860   }
1861   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1862   {
1863     n->quota_violation_count++;
1864 #if DEBUG_TRANSPORT
1865     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866                 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1867                 n->in_tracker.available_bytes_per_s__,
1868                 n->quota_violation_count);
1869 #endif
1870     /* Discount 32k per violation */
1871     GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1872   }
1873   else
1874   {
1875     if (n->quota_violation_count > 0)
1876     {
1877       /* try to add 32k back */
1878       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1879       n->quota_violation_count--;
1880     }
1881   }
1882   if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1883   {
1884     GNUNET_STATISTICS_update (GST_stats,
1885                               gettext_noop
1886                               ("# bandwidth quota violations by other peers"),
1887                               1, GNUNET_NO);
1888     *do_forward = GNUNET_NO;
1889     return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1890   }
1891   *do_forward = GNUNET_YES;
1892   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1893   if (ret.rel_value > 0)
1894   {
1895 #if DEBUG_TRANSPORT
1896     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897                 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1898                 (unsigned long long) n->in_tracker.
1899                 consumption_since_last_update__,
1900                 (unsigned int) n->in_tracker.available_bytes_per_s__,
1901                 (unsigned long long) ret.rel_value);
1902 #endif
1903     GNUNET_STATISTICS_update (GST_stats,
1904                               gettext_noop ("# ms throttling suggested"),
1905                               (int64_t) ret.rel_value, GNUNET_NO);
1906   }
1907   return ret;
1908 }
1909
1910
1911 /**
1912  * Keep the connection to the given neighbour alive longer,
1913  * we received a KEEPALIVE (or equivalent).
1914  *
1915  * @param neighbour neighbour to keep alive
1916  */
1917 void
1918 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1919 {
1920   struct NeighbourMapEntry *n;
1921
1922   // This can happen during shutdown
1923   if (neighbours == NULL)
1924   {
1925     return;
1926   }
1927
1928   n = lookup_neighbour (neighbour);
1929   if (NULL == n)
1930   {
1931     GNUNET_STATISTICS_update (GST_stats,
1932                               gettext_noop
1933                               ("# KEEPALIVE messages discarded (not connected)"),
1934                               1, GNUNET_NO);
1935     return;
1936   }
1937   GNUNET_SCHEDULER_cancel (n->timeout_task);
1938   n->timeout_task =
1939       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1940                                     &neighbour_timeout_task, n);
1941
1942   /* send reply to measure latency */
1943   if (S_CONNECTED != n->state)
1944     return;
1945
1946   struct GNUNET_MessageHeader m;
1947   m.size = htons (sizeof (struct GNUNET_MessageHeader));
1948   m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1949
1950   send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1951                     UINT32_MAX /* priority */ ,
1952                     GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1953                     GNUNET_YES, NULL, NULL);
1954 }
1955
1956 /**
1957  * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
1958  * to this peer
1959  *
1960  * @param neighbour neighbour to keep alive
1961  */
1962 void
1963 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1964                                    const struct GNUNET_ATS_Information * ats,
1965                                    uint32_t ats_count)
1966 {
1967   struct NeighbourMapEntry *n;
1968   struct GNUNET_ATS_Information * ats_new;
1969   uint32_t latency;
1970
1971   if (neighbours == NULL)
1972   {
1973     // This can happen during shutdown
1974     return;
1975   }
1976
1977   n = lookup_neighbour (neighbour);
1978   if (NULL == n)
1979   {
1980     GNUNET_STATISTICS_update (GST_stats,
1981                               gettext_noop
1982                               ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1983                               1, GNUNET_NO);
1984     return;
1985   }
1986   if (n->expect_latency_response != GNUNET_YES)
1987   {
1988     GNUNET_STATISTICS_update (GST_stats,
1989                               gettext_noop
1990                               ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1991                               1, GNUNET_NO);
1992     return;
1993   }
1994   n->expect_latency_response = GNUNET_NO;
1995
1996   GNUNET_assert (n->keep_alive_sent.abs_value != GNUNET_TIME_absolute_get_zero().abs_value);
1997   n->latency = GNUNET_TIME_absolute_get_difference(n->keep_alive_sent, GNUNET_TIME_absolute_get());
1998 #if DEBUG_TRANSPORT
1999   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000               "Latency for peer `%s' is %llu ms\n",
2001               GNUNET_i2s (&n->id), n->latency.rel_value);
2002 #endif
2003
2004
2005   if (n->latency.rel_value == GNUNET_TIME_relative_get_forever().rel_value)
2006     GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2007   else
2008   {
2009     ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2010     memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2011
2012     /* add latency */
2013     ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2014     if (n->latency.rel_value > UINT32_MAX)
2015       latency = UINT32_MAX;
2016     else
2017       latency = n->latency.rel_value;
2018     ats_new[ats_count].value = htonl (latency);
2019
2020     GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new, ats_count + 1);
2021     GNUNET_free (ats_new);
2022   }
2023 }
2024
2025
2026 /**
2027  * Change the incoming quota for the given peer.
2028  *
2029  * @param neighbour identity of peer to change qutoa for
2030  * @param quota new quota
2031  */
2032 void
2033 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2034                                    struct GNUNET_BANDWIDTH_Value32NBO quota)
2035 {
2036   struct NeighbourMapEntry *n;
2037
2038   // This can happen during shutdown
2039   if (neighbours == NULL)
2040   {
2041     return;
2042   }
2043
2044   n = lookup_neighbour (neighbour);
2045   if (n == NULL)
2046   {
2047     GNUNET_STATISTICS_update (GST_stats,
2048                               gettext_noop
2049                               ("# SET QUOTA messages ignored (no such peer)"),
2050                               1, GNUNET_NO);
2051     return;
2052   }
2053 #if DEBUG_TRANSPORT
2054   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055               "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2056               ntohl (quota.value__), GNUNET_i2s (&n->id));
2057 #endif
2058   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2059   if (0 != ntohl (quota.value__))
2060     return;
2061 #if DEBUG_TRANSPORT
2062   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2063               GNUNET_i2s (&n->id), "SET_QUOTA");
2064 #endif
2065   if (is_connected (n))
2066     GNUNET_STATISTICS_update (GST_stats,
2067                               gettext_noop ("# disconnects due to quota of 0"),
2068                               1, GNUNET_NO);
2069   disconnect_neighbour (n);
2070 }
2071
2072
2073 /**
2074  * Closure for the neighbours_iterate function.
2075  */
2076 struct IteratorContext
2077 {
2078   /**
2079    * Function to call on each connected neighbour.
2080    */
2081   GST_NeighbourIterator cb;
2082
2083   /**
2084    * Closure for 'cb'.
2085    */
2086   void *cb_cls;
2087 };
2088
2089
2090 /**
2091  * Call the callback from the closure for each connected neighbour.
2092  *
2093  * @param cls the 'struct IteratorContext'
2094  * @param key the hash of the public key of the neighbour
2095  * @param value the 'struct NeighbourMapEntry'
2096  * @return GNUNET_OK (continue to iterate)
2097  */
2098 static int
2099 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2100 {
2101   struct IteratorContext *ic = cls;
2102   struct NeighbourMapEntry *n = value;
2103
2104   if (!is_connected (n))
2105     return GNUNET_OK;
2106
2107   ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2108   return GNUNET_OK;
2109 }
2110
2111
2112 /**
2113  * Iterate over all connected neighbours.
2114  *
2115  * @param cb function to call
2116  * @param cb_cls closure for cb
2117  */
2118 void
2119 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2120 {
2121   struct IteratorContext ic;
2122
2123   // This can happen during shutdown
2124   if (neighbours == NULL)
2125   {
2126     return;
2127   }
2128
2129   ic.cb = cb;
2130   ic.cb_cls = cb_cls;
2131   GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2132 }
2133
2134 /**
2135  * If we have an active connection to the given target, it must be shutdown.
2136  *
2137  * @param target peer to disconnect from
2138  */
2139 void
2140 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2141 {
2142   struct NeighbourMapEntry *n;
2143
2144   // This can happen during shutdown
2145   if (neighbours == NULL)
2146   {
2147     return;
2148   }
2149
2150   n = lookup_neighbour (target);
2151   if (NULL == n)
2152     return;                     /* not active */
2153   disconnect_neighbour (n);
2154 }
2155
2156
2157 /**
2158  * We received a disconnect message from the given peer,
2159  * validate and process.
2160  *
2161  * @param peer sender of the message
2162  * @param msg the disconnect message
2163  */
2164 void
2165 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2166                                           *peer,
2167                                           const struct GNUNET_MessageHeader
2168                                           *msg)
2169 {
2170   struct NeighbourMapEntry *n;
2171   const struct SessionDisconnectMessage *sdm;
2172   GNUNET_HashCode hc;
2173
2174 #if DEBUG_TRANSPORT
2175   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176               "Received DISCONNECT message from peer `%s'\n",
2177               GNUNET_i2s (peer));
2178 #endif
2179
2180   if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2181   {
2182     // GNUNET_break_op (0);
2183     GNUNET_STATISTICS_update (GST_stats,
2184                               gettext_noop
2185                               ("# disconnect messages ignored (old format)"), 1,
2186                               GNUNET_NO);
2187     return;
2188   }
2189   sdm = (const struct SessionDisconnectMessage *) msg;
2190   n = lookup_neighbour (peer);
2191   if (NULL == n)
2192     return;                     /* gone already */
2193   if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2194       n->connect_ts.abs_value)
2195   {
2196     GNUNET_STATISTICS_update (GST_stats,
2197                               gettext_noop
2198                               ("# disconnect messages ignored (timestamp)"), 1,
2199                               GNUNET_NO);
2200     return;
2201   }
2202   GNUNET_CRYPTO_hash (&sdm->public_key,
2203                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2204                       &hc);
2205   if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2206   {
2207     GNUNET_break_op (0);
2208     return;
2209   }
2210   if (ntohl (sdm->purpose.size) !=
2211       sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2212       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2213       sizeof (struct GNUNET_TIME_AbsoluteNBO))
2214   {
2215     GNUNET_break_op (0);
2216     return;
2217   }
2218   if (GNUNET_OK !=
2219       GNUNET_CRYPTO_rsa_verify
2220       (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2221        &sdm->signature, &sdm->public_key))
2222   {
2223     GNUNET_break_op (0);
2224     return;
2225   }
2226   GST_neighbours_force_disconnect (peer);
2227 }
2228
2229
2230 /**
2231  * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2232  * Consider switching to it.
2233  *
2234  * @param message possibly a 'struct SessionConnectMessage' (check format)
2235  * @param peer identity of the peer to switch the address for
2236  * @param address address of the other peer, NULL if other peer
2237  *                       connected to us
2238  * @param session session to use (or NULL)
2239  * @param ats performance data
2240  * @param ats_count number of entries in ats
2241  */
2242 void
2243 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2244                                    const struct GNUNET_PeerIdentity *peer,
2245                                    const struct GNUNET_HELLO_Address *address,
2246                                    struct Session *session,
2247                                    const struct GNUNET_ATS_Information *ats,
2248                                    uint32_t ats_count)
2249 {
2250   const struct SessionConnectMessage *scm;
2251   struct GNUNET_MessageHeader msg;
2252   struct NeighbourMapEntry *n;
2253   size_t msg_len;
2254   size_t ret;
2255
2256 #if DEBUG_TRANSPORT
2257   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258               "Received CONNECT_ACK message from peer `%s'\n",
2259               GNUNET_i2s (peer));
2260 #endif
2261
2262   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2263   {
2264     GNUNET_break_op (0);
2265     return;
2266   }
2267   scm = (const struct SessionConnectMessage *) message;
2268   GNUNET_break_op (ntohl (scm->reserved) == 0);
2269   n = lookup_neighbour (peer);
2270   if (NULL == n)
2271   {
2272     /* we did not send 'CONNECT' (how could we? no record for this peer!) */
2273     GNUNET_break_op (0);
2274     return;
2275   }  
2276
2277   /* Additional check
2278    *
2279    * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2280    *
2281    * We also received an CONNECT message, switched from SENDT to RECV and
2282    * ATS already suggested us an address after a successful blacklist check
2283    */
2284   if ((n->state != S_CONNECT_SENT) && ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2285   {
2286     GNUNET_STATISTICS_update (GST_stats,
2287                               gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2288                               GNUNET_NO);
2289     return;
2290   }
2291
2292   if (NULL != session)
2293     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2294                      "transport-ats",
2295                      "Giving ATS session %p of plugin %s for peer %s\n",
2296                      session, address->transport_name, GNUNET_i2s (peer));
2297   GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2298   GNUNET_assert (NULL != n->address);
2299
2300   change_state (n, S_CONNECTED);
2301   if (n->address_state == FRESH)
2302   {
2303     GST_validation_set_address_use (&n->id,
2304             n->address,
2305             n->session,
2306             GNUNET_YES);
2307     n->address_state = USED;
2308   }
2309   GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2310
2311   GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2312
2313   /* send ACK (ACK) */
2314   msg_len = sizeof (msg);
2315   msg.size = htons (msg_len);
2316   msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2317
2318   ret =
2319       send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2320                         GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2321                         n->address, GNUNET_YES, NULL,
2322                         NULL);
2323
2324   if (ret == GNUNET_SYSERR)
2325     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326                 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2327                 GNUNET_i2s (&n->id), 
2328                 GST_plugins_a2s (n->address), n->session);
2329
2330
2331   if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2332     n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2333   
2334   neighbours_connected++;
2335   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2336                             GNUNET_NO);
2337 #if DEBUG_TRANSPORT
2338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339               "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2340               GNUNET_i2s (&n->id),
2341               GST_plugins_a2s (n->address), n->session,
2342               __LINE__);
2343 #endif
2344   connect_notify_cb (callback_cls, &n->id, ats, ats_count);  
2345   send_outbound_quota(peer, n->bandwidth_out);
2346
2347 }
2348
2349
2350 void
2351 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2352                            const struct GNUNET_PeerIdentity *peer,
2353                            const struct GNUNET_HELLO_Address *address,
2354                            struct Session *session,
2355                            const struct GNUNET_ATS_Information *ats,
2356                            uint32_t ats_count)
2357 {
2358   struct NeighbourMapEntry *n;
2359
2360 #if DEBUG_TRANSPORT
2361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
2362               "Received ACK message from peer `%s'\n",
2363               GNUNET_i2s (peer));
2364 #endif
2365
2366   if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2367   {
2368     GNUNET_break_op (0);
2369     return;
2370   }
2371   n = lookup_neighbour (peer);
2372   if (NULL == n)
2373   {
2374     send_disconnect (peer, address,
2375                      session);
2376     GNUNET_break (0);
2377     return;
2378   }
2379   if (S_CONNECTED == n->state)
2380     return;
2381   if (!is_connecting(n))
2382   {
2383     GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2384                               GNUNET_NO);
2385     return;
2386   }
2387   if (NULL != session)
2388     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2389                      "transport-ats",
2390                      "Giving ATS session %p of plugin %s for peer %s\n",
2391                      session, address->transport_name, GNUNET_i2s (peer));
2392   GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2393   GNUNET_assert (n->address != NULL);
2394   change_state (n, S_CONNECTED);
2395   GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2396
2397   GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2398
2399   if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2400         n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2401   if (n->address_state == FRESH)
2402   {
2403     GST_validation_set_address_use (&n->id,
2404                                   n->address,
2405                                   n->session,
2406                                   GNUNET_YES);
2407     n->address_state = USED;
2408   }
2409   neighbours_connected++;
2410   GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2411                             GNUNET_NO);
2412   
2413 #if DEBUG_TRANSPORT
2414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415               "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2416               GNUNET_i2s (&n->id),
2417               GST_plugins_a2s (n->address), n->session,
2418               __LINE__);
2419 #endif
2420   connect_notify_cb (callback_cls, &n->id, ats, ats_count);  
2421   send_outbound_quota(peer, n->bandwidth_out);
2422 }
2423
2424 struct BlackListCheckContext
2425 {
2426   struct GNUNET_ATS_Information *ats;
2427
2428   uint32_t ats_count;
2429
2430   struct Session *session;
2431
2432   struct GNUNET_HELLO_Address *address;
2433
2434   struct GNUNET_TIME_Absolute ts;
2435 };
2436
2437
2438 static void
2439 handle_connect_blacklist_cont (void *cls,
2440                                const struct GNUNET_PeerIdentity *peer,
2441                                int result)
2442 {
2443   struct NeighbourMapEntry *n;
2444   struct BlackListCheckContext *bcc = cls;
2445
2446 #if DEBUG_TRANSPORT
2447   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2448               "Blacklist check due to CONNECT message: `%s'\n",
2449               GNUNET_i2s (peer),
2450               (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2451 #endif
2452
2453   /* not allowed */
2454   if (GNUNET_OK != result)
2455   {
2456     GNUNET_HELLO_address_free (bcc->address);
2457     GNUNET_free (bcc);
2458     return;
2459   }
2460
2461   n = lookup_neighbour (peer);
2462   if (NULL == n)
2463     n = setup_neighbour (peer);
2464
2465   if (bcc->ts.abs_value > n->connect_ts.abs_value)
2466   {
2467     if (NULL != bcc->session)
2468       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2469                        "transport-ats",
2470                        "Giving ATS session %p of address `%s' for peer %s\n",
2471                        bcc->session, 
2472                        GST_plugins_a2s (bcc->address),
2473                        GNUNET_i2s (peer));
2474     GNUNET_ATS_address_update (GST_ats, bcc->address,
2475                                bcc->session, bcc->ats, bcc->ats_count);
2476     n->connect_ts = bcc->ts;
2477   }
2478
2479   GNUNET_HELLO_address_free (bcc->address);
2480   GNUNET_free (bcc);
2481
2482   if (n->state != S_CONNECT_RECV)
2483     change_state (n, S_CONNECT_RECV);
2484
2485   /* Ask ATS for an address to connect via that address */
2486   if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2487     GNUNET_SCHEDULER_cancel (n->ats_suggest);
2488   n->ats_suggest =
2489       GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2490                                     n);
2491   GNUNET_ATS_suggest_address (GST_ats, peer);
2492 }
2493
2494 /**
2495  * We received a 'SESSION_CONNECT' message from the other peer.
2496  * Consider switching to it.
2497  *
2498  * @param message possibly a 'struct SessionConnectMessage' (check format)
2499  * @param peer identity of the peer to switch the address for
2500  * @param address address of the other peer, NULL if other peer
2501  *                       connected to us
2502  * @param session session to use (or NULL)
2503  * @param ats performance data
2504  * @param ats_count number of entries in ats (excluding 0-termination)
2505  */
2506 void
2507 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2508                                const struct GNUNET_PeerIdentity *peer,
2509                                const struct GNUNET_HELLO_Address *address,
2510                                struct Session *session,
2511                                const struct GNUNET_ATS_Information *ats,
2512                                uint32_t ats_count)
2513 {
2514   const struct SessionConnectMessage *scm;
2515   struct NeighbourMapEntry *n;
2516   struct BlackListCheckContext *bcc = NULL;
2517
2518 #if DEBUG_TRANSPORT
2519   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520               "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2521 #endif
2522
2523   if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2524   {
2525     GNUNET_break_op (0);
2526     return;
2527   }
2528
2529   scm = (const struct SessionConnectMessage *) message;
2530   GNUNET_break_op (ntohl (scm->reserved) == 0);
2531
2532   n = lookup_neighbour (peer);
2533   if ( (n != NULL) &&
2534        (S_CONNECTED == n->state) )
2535   {
2536     /* connected peer switches addresses */
2537     GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2538     return;
2539   }
2540
2541   /* we are not connected to this peer */
2542   /* do blacklist check */
2543   bcc =
2544       GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2545                      sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2546   bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2547   bcc->ats_count = ats_count + 1;
2548   bcc->address = GNUNET_HELLO_address_copy (address);
2549   bcc->session = session;
2550   bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2551   memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2552   bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2553   bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2554   GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2555                               bcc);
2556 }
2557
2558
2559 /* end of file gnunet-service-transport_neighbours.c */