run indent twice, it alternates between two 'canonical' forms, also run whitespace...
[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.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_constants.h"
34 #include "transport.h"
35
36
37 /**
38  * Size of the neighbour hash map.
39  */
40 #define NEIGHBOUR_TABLE_SIZE 256
41
42 /**
43  * How often must a peer violate bandwidth quotas before we start
44  * to simply drop its messages?
45  */
46 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
47
48
49 /**
50  * Entry in neighbours.
51  */
52 struct NeighbourMapEntry;
53
54
55 /**
56  * For each neighbour we keep a list of messages
57  * that we still want to transmit to the neighbour.
58  */
59 struct MessageQueue
60 {
61
62   /**
63    * This is a doubly linked list.
64    */
65   struct MessageQueue *next;
66
67   /**
68    * This is a doubly linked list.
69    */
70   struct MessageQueue *prev;
71
72   /**
73    * Once this message is actively being transmitted, which
74    * neighbour is it associated with?
75    */
76   struct NeighbourMapEntry *n;
77
78   /**
79    * Function to call once we're done.
80    */
81   GST_NeighbourSendContinuation cont;
82
83   /**
84    * Closure for 'cont'
85    */
86   void *cont_cls;
87
88   /**
89    * The message(s) we want to transmit, GNUNET_MessageHeader(s)
90    * stuck together in memory.  Allocated at the end of this struct.
91    */
92   const char *message_buf;
93
94   /**
95    * Size of the message buf
96    */
97   size_t message_buf_size;
98
99   /**
100    * At what time should we fail?
101    */
102   struct GNUNET_TIME_Absolute timeout;
103
104 };
105
106
107 /**
108  * Entry in neighbours.
109  */
110 struct NeighbourMapEntry
111 {
112
113   /**
114    * Head of list of messages we would like to send to this peer;
115    * must contain at most one message per client.
116    */
117   struct MessageQueue *messages_head;
118
119   /**
120    * Tail of list of messages we would like to send to this peer; must
121    * contain at most one message per client.
122    */
123   struct MessageQueue *messages_tail;
124
125   /**
126    * Context for address suggestion.
127    * NULL after we are connected.
128    */
129   struct GNUNET_ATS_SuggestionContext *asc;
130
131   /**
132    * Performance data for the peer.
133    */
134   struct GNUNET_TRANSPORT_ATS_Information *ats;
135
136   /**
137    * Are we currently trying to send a message? If so, which one?
138    */
139   struct MessageQueue *is_active;
140
141   /**
142    * Active session for communicating with the peer.
143    */
144   struct Session *session;
145
146   /**
147    * Name of the plugin we currently use.
148    */
149   char *plugin_name;
150
151   /**
152    * Address used for communicating with the peer, NULL for inbound connections.
153    */
154   void *addr;
155
156   /**
157    * Number of bytes in 'addr'.
158    */
159   size_t addrlen;
160
161   /**
162    * Identity of this neighbour.
163    */
164   struct GNUNET_PeerIdentity id;
165
166   /**
167    * ID of task scheduled to run when this peer is about to
168    * time out (will free resources associated with the peer).
169    */
170   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
171
172   /**
173    * ID of task scheduled to run when we should try transmitting
174    * the head of the message queue.
175    */
176   GNUNET_SCHEDULER_TaskIdentifier transmission_task;
177
178   /**
179    * Tracker for inbound bandwidth.
180    */
181   struct GNUNET_BANDWIDTH_Tracker in_tracker;
182
183   /**
184    * How often has the other peer (recently) violated the inbound
185    * traffic limit?  Incremented by 10 per violation, decremented by 1
186    * per non-violation (for each time interval).
187    */
188   unsigned int quota_violation_count;
189
190   /**
191    * Number of values in 'ats' array.
192    */
193   unsigned int ats_count;
194
195   /**
196    * Are we already in the process of disconnecting this neighbour?
197    */
198   int in_disconnect;
199
200   /**
201    * Do we currently consider this neighbour connected? (as far as
202    * the connect/disconnect callbacks are concerned)?
203    */
204   int is_connected;
205
206 };
207
208
209 /**
210  * All known neighbours and their HELLOs.
211  */
212 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
213
214 /**
215  * Closure for connect_notify_cb and disconnect_notify_cb
216  */
217 static void *callback_cls;
218
219 /**
220  * Function to call when we connected to a neighbour.
221  */
222 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
223
224 /**
225  * Function to call when we disconnected from a neighbour.
226  */
227 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
228
229
230 /**
231  * Lookup a neighbour entry in the neighbours hash map.
232  *
233  * @param pid identity of the peer to look up
234  * @return the entry, NULL if there is no existing record
235  */
236 static struct NeighbourMapEntry *
237 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
238 {
239   return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
240 }
241
242
243 /**
244  * Task invoked to start a transmission to another peer.
245  *
246  * @param cls the 'struct NeighbourMapEntry'
247  * @param tc scheduler context
248  */
249 static void
250 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
251
252
253 /**
254  * We're done with our transmission attempt, continue processing.
255  *
256  * @param cls the 'struct MessageQueue' of the message
257  * @param receiver intended receiver
258  * @param success whether it worked or not
259  */
260 static void
261 transmit_send_continuation (void *cls,
262                             const struct GNUNET_PeerIdentity *receiver,
263                             int success)
264 {
265   struct MessageQueue *mq;
266   struct NeighbourMapEntry *n;
267
268   mq = cls;
269   n = mq->n;
270   if (NULL != n)
271   {
272     GNUNET_assert (n->is_active == mq);
273     n->is_active = NULL;
274     GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
275     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
276   }
277   if (NULL != mq->cont)
278     mq->cont (mq->cont_cls, success);
279   GNUNET_free (mq);
280 }
281
282
283 /**
284  * Check the ready list for the given neighbour and if a plugin is
285  * ready for transmission (and if we have a message), do so!
286  *
287  * @param neighbour target peer for which to transmit
288  */
289 static void
290 try_transmission_to_peer (struct NeighbourMapEntry *n)
291 {
292   struct MessageQueue *mq;
293   struct GNUNET_TIME_Relative timeout;
294   ssize_t ret;
295   struct GNUNET_TRANSPORT_PluginFunctions *papi;
296
297   if (n->is_active != NULL)
298     return;                     /* transmission already pending */
299   if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
300     return;                     /* currently waiting for bandwidth */
301   mq = n->messages_head;
302   while (NULL != (mq = n->messages_head))
303   {
304     timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
305     if (timeout.rel_value > 0)
306       break;
307     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);     /* timeout */
308   }
309   if (NULL == mq)
310     return;                     /* no more messages */
311
312   papi = GST_plugins_find (n->plugin_name);
313   if (papi == NULL)
314   {
315     GNUNET_break (0);
316     return;
317   }
318   GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
319   n->is_active = mq;
320   mq->n = n;
321
322
323   ret =
324       papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
325                   0 /* priority -- remove from plugin API? */ ,
326                   timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
327                   &transmit_send_continuation, mq);
328   if (ret == -1)
329   {
330     /* failure, but 'send' would not call continuation in this case,
331      * so we need to do it here! */
332     transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
333     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
334   }
335 }
336
337
338 /**
339  * Task invoked to start a transmission to another peer.
340  *
341  * @param cls the 'struct NeighbourMapEntry'
342  * @param tc scheduler context
343  */
344 static void
345 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
346 {
347   struct NeighbourMapEntry *n = cls;
348
349   n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
350   try_transmission_to_peer (n);
351 }
352
353
354 /**
355  * Initialize the neighbours subsystem.
356  *
357  * @param cls closure for callbacks
358  * @param connect_cb function to call if we connect to a peer
359  * @param disconnect_cb function to call if we disconnect from a peer
360  */
361 void
362 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
363                       GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
364 {
365   callback_cls = cls;
366   connect_notify_cb = connect_cb;
367   disconnect_notify_cb = disconnect_cb;
368   neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
369 }
370
371
372 /**
373  * Disconnect from the given neighbour, clean up the record.
374  *
375  * @param n neighbour to disconnect from
376  */
377 static void
378 disconnect_neighbour (struct NeighbourMapEntry *n)
379 {
380   struct MessageQueue *mq;
381
382   if (GNUNET_YES == n->in_disconnect)
383     return;
384   n->in_disconnect = GNUNET_YES;
385   while (NULL != (mq = n->messages_head))
386   {
387     GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
388     mq->cont (mq->cont_cls, GNUNET_SYSERR);
389     GNUNET_free (mq);
390   }
391   if (NULL != n->is_active)
392   {
393     n->is_active->n = NULL;
394     n->is_active = NULL;
395   }
396   if (GNUNET_YES == n->is_connected)
397   {
398     n->is_connected = GNUNET_NO;
399     disconnect_notify_cb (callback_cls, &n->id);
400   }
401   GNUNET_assert (GNUNET_YES ==
402                  GNUNET_CONTAINER_multihashmap_remove (neighbours,
403                                                        &n->id.hashPubKey, n));
404   if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
405   {
406     GNUNET_SCHEDULER_cancel (n->timeout_task);
407     n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
408   }
409   if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
410   {
411     GNUNET_SCHEDULER_cancel (n->timeout_task);
412     n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
413   }
414   if (NULL != n->asc)
415   {
416     GNUNET_ATS_suggest_address_cancel (n->asc);
417     n->asc = NULL;
418   }
419   GNUNET_array_grow (n->ats, n->ats_count, 0);
420   if (NULL != n->plugin_name)
421   {
422     GNUNET_free (n->plugin_name);
423     n->plugin_name = NULL;
424   }
425   if (NULL != n->addr)
426   {
427     GNUNET_free (n->addr);
428     n->addr = NULL;
429     n->addrlen = 0;
430   }
431   n->session = NULL;
432   GNUNET_free (n);
433 }
434
435
436 /**
437  * Peer has been idle for too long. Disconnect.
438  *
439  * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
440  * @param tc scheduler context
441  */
442 static void
443 neighbour_timeout_task (void *cls,
444                         const struct GNUNET_SCHEDULER_TaskContext *tc)
445 {
446   struct NeighbourMapEntry *n = cls;
447
448   n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
449   disconnect_neighbour (n);
450 }
451
452
453 /**
454  * Disconnect from the given neighbour.
455  *
456  * @param cls unused
457  * @param key hash of neighbour's public key (not used)
458  * @param value the 'struct NeighbourMapEntry' of the neighbour
459  */
460 static int
461 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
462 {
463   struct NeighbourMapEntry *n = value;
464
465 #if DEBUG_TRANSPORT
466   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
467               GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
468 #endif
469   disconnect_neighbour (n);
470   return GNUNET_OK;
471 }
472
473
474 /**
475  * Cleanup the neighbours subsystem.
476  */
477 void
478 GST_neighbours_stop ()
479 {
480   GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
481                                          NULL);
482   GNUNET_CONTAINER_multihashmap_destroy (neighbours);
483   neighbours = NULL;
484   callback_cls = NULL;
485   connect_notify_cb = NULL;
486   disconnect_notify_cb = NULL;
487 }
488
489
490 /**
491  * For an existing neighbour record, set the active connection to
492  * the given address.
493  *
494  * @param peer identity of the peer to switch the address for
495  * @param plugin_name name of transport that delivered the PONG
496  * @param address address of the other peer, NULL if other peer
497  *                       connected to us
498  * @param address_len number of bytes in address
499  * @param session session to use (or NULL)
500  * @param ats performance data
501  * @param ats_count number of entries in ats (excluding 0-termination)
502  */
503 void
504 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
505                                   const char *plugin_name, const void *address,
506                                   size_t address_len, struct Session *session,
507                                   const struct GNUNET_TRANSPORT_ATS_Information
508                                   *ats, uint32_t ats_count)
509 {
510   struct NeighbourMapEntry *n;
511   struct GNUNET_MessageHeader connect_msg;
512
513   n = lookup_neighbour (peer);
514   if (NULL == n)
515   {
516     GNUNET_break (0);
517     return;
518   }
519
520 #if DEBUG_TRANSPORT
521   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
522               "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
523               GNUNET_i2s (peer), plugin_name,
524               (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
525                                                                   address,
526                                                                   address_len),
527               session);
528 #endif
529
530   GNUNET_free_non_null (n->addr);
531   n->addr = GNUNET_malloc (address_len);
532   memcpy (n->addr, address, address_len);
533   n->addrlen = address_len;
534   n->session = session;
535   GNUNET_array_grow (n->ats, n->ats_count, ats_count);
536   memcpy (n->ats, ats,
537           ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
538   GNUNET_free_non_null (n->plugin_name);
539   n->plugin_name = GNUNET_strdup (plugin_name);
540   GNUNET_SCHEDULER_cancel (n->timeout_task);
541   n->timeout_task =
542       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
543                                     &neighbour_timeout_task, n);
544   connect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
545   connect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
546   GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
547                        GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
548 }
549
550
551 /**
552  * Try to connect to the target peer using the given address
553  *
554  * @param cls the 'struct NeighbourMapEntry' of the target
555  * @param target identity of the target peer
556  * @param plugin_name name of the plugin
557  * @param plugin_address binary address
558  * @param plugin_address_len length of address
559  * @param bandwidth available bandwidth
560  * @param ats performance data for the address (as far as known)
561  * @param ats_count number of performance records in 'ats'
562  */
563 static void
564 try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
565                            const char *plugin_name, const void *plugin_address,
566                            size_t plugin_address_len, struct Session *session,
567                            struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
568                            const struct GNUNET_TRANSPORT_ATS_Information *ats,
569                            uint32_t ats_count)
570 {
571   struct NeighbourMapEntry *n = cls;
572
573   n->asc = NULL;
574   GST_neighbours_switch_to_address (target, plugin_name, plugin_address,
575                                     plugin_address_len, session, ats,
576                                     ats_count);
577   if (GNUNET_YES == n->is_connected)
578     return;
579   n->is_connected = GNUNET_YES;
580   connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
581 }
582
583
584 /**
585  * Try to create a connection to the given target (eventually).
586  *
587  * @param target peer to try to connect to
588  */
589 void
590 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
591 {
592   struct NeighbourMapEntry *n;
593
594   GNUNET_assert (0 !=
595                  memcmp (target, &GST_my_identity,
596                          sizeof (struct GNUNET_PeerIdentity)));
597   n = lookup_neighbour (target);
598   if ((NULL != n) && (GNUNET_YES == n->is_connected))
599     return;                     /* already connected */
600   if (n == NULL)
601   {
602     n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
603     n->id = *target;
604     GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
605                                    GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
606                                    MAX_BANDWIDTH_CARRY_S);
607     n->timeout_task =
608         GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
609                                       &neighbour_timeout_task, n);
610     GNUNET_assert (GNUNET_OK ==
611                    GNUNET_CONTAINER_multihashmap_put (neighbours,
612                                                       &n->id.hashPubKey, n,
613                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
614   }
615   if (n->asc != NULL)
616     return;                     /* already trying */
617   n->asc =
618       GNUNET_ATS_suggest_address (GST_ats, target, &try_connect_using_address,
619                                   n);
620 }
621
622
623 /**
624  * Test if we're connected to the given peer.
625  *
626  * @param target peer to test
627  * @return GNUNET_YES if we are connected, GNUNET_NO if not
628  */
629 int
630 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
631 {
632   struct NeighbourMapEntry *n;
633
634   n = lookup_neighbour (target);
635   if ((NULL == n) || (n->is_connected != GNUNET_YES))
636     return GNUNET_NO;           /* not connected */
637   return GNUNET_YES;
638 }
639
640
641 /**
642  * A session was terminated. Take note.
643  *
644  * @param peer identity of the peer where the session died
645  * @param session session that is gone
646  */
647 void
648 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
649                                    struct Session *session)
650 {
651   struct NeighbourMapEntry *n;
652
653   n = lookup_neighbour (peer);
654   if (NULL == n)
655     return;
656   if (session != n->session)
657     return;                     /* doesn't affect us */
658   n->session = NULL;
659   if (GNUNET_YES != n->is_connected)
660     return;                     /* not connected anymore anyway, shouldn't matter */
661
662   GNUNET_SCHEDULER_cancel (n->timeout_task);
663   n->timeout_task =
664       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
665                                     &neighbour_timeout_task, n);
666   /* try QUICKLY to re-establish a connection, reduce timeout! */
667   if (NULL != n->ats)
668   {
669     /* how can this be!? */
670     //GNUNET_break (0);
671     return;
672   }
673   n->asc =
674       GNUNET_ATS_suggest_address (GST_ats, peer, &try_connect_using_address, n);
675 }
676
677
678 /**
679  * Transmit a message to the given target using the active connection.
680  *
681  * @param target destination
682  * @param msg message to send
683  * @param msg_size number of bytes in msg
684  * @param timeout when to fail with timeout
685  * @param cont function to call when done
686  * @param cont_cls closure for 'cont'
687  */
688 void
689 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
690                      size_t msg_size, struct GNUNET_TIME_Relative timeout,
691                      GST_NeighbourSendContinuation cont, void *cont_cls)
692 {
693   struct NeighbourMapEntry *n;
694   struct MessageQueue *mq;
695
696   n = lookup_neighbour (target);
697   if ((n == NULL) || (GNUNET_YES != n->is_connected))
698   {
699     GNUNET_STATISTICS_update (GST_stats,
700                               gettext_noop
701                               ("# SET QUOTA messages ignored (no such peer)"),
702                               1, GNUNET_NO);
703 #if DEBUG_TRANSPORT
704     if (n == NULL)
705       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
706                   "Could not send message to peer `%s': unknown neighbor",
707                   GNUNET_i2s (target));
708     if (GNUNET_YES != n->is_connected)
709       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
710                   "Could not send message to peer `%s': not connected\n",
711                   GNUNET_i2s (target));
712 #endif
713     if (NULL != cont)
714       cont (cont_cls, GNUNET_SYSERR);
715     return;
716   }
717   GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
718   GNUNET_STATISTICS_update (GST_stats,
719                             gettext_noop
720                             ("# bytes in message queue for other peers"),
721                             msg_size, GNUNET_NO);
722   mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
723   mq->cont = cont;
724   mq->cont_cls = cont_cls;
725   /* FIXME: this memcpy can be up to 7% of our total runtime! */
726   memcpy (&mq[1], msg, msg_size);
727   mq->message_buf = (const char *) &mq[1];
728   mq->message_buf_size = msg_size;
729   mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
730   GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
731   if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
732       (NULL == n->is_active))
733     n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
734 }
735
736
737 /**
738  * We have received a message from the given sender.  How long should
739  * we delay before receiving more?  (Also used to keep the peer marked
740  * as live).
741  *
742  * @param sender sender of the message
743  * @param size size of the message
744  * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
745  *                   GNUNET_NO if the neighbour is not connected or violates the quota
746  * @return how long to wait before reading more from this sender
747  */
748 struct GNUNET_TIME_Relative
749 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
750                                         *sender, ssize_t size, int *do_forward)
751 {
752   struct NeighbourMapEntry *n;
753   struct GNUNET_TIME_Relative ret;
754
755   n = lookup_neighbour (sender);
756   if (n == NULL)
757   {
758     *do_forward = GNUNET_NO;
759     return GNUNET_TIME_UNIT_ZERO;
760   }
761   if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
762   {
763     n->quota_violation_count++;
764 #if DEBUG_TRANSPORT
765     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
766                 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
767                 n->in_tracker.available_bytes_per_s__,
768                 n->quota_violation_count);
769 #endif
770     /* Discount 32k per violation */
771     GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
772   }
773   else
774   {
775     if (n->quota_violation_count > 0)
776     {
777       /* try to add 32k back */
778       GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
779       n->quota_violation_count--;
780     }
781   }
782   if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
783   {
784     GNUNET_STATISTICS_update (GST_stats,
785                               gettext_noop
786                               ("# bandwidth quota violations by other peers"),
787                               1, GNUNET_NO);
788     *do_forward = GNUNET_NO;
789     return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
790   }
791   *do_forward = GNUNET_YES;
792   ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
793   if (ret.rel_value > 0)
794   {
795 #if DEBUG_TRANSPORT
796     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
797                 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
798                 (unsigned long long) n->in_tracker.
799                 consumption_since_last_update__,
800                 (unsigned int) n->in_tracker.available_bytes_per_s__,
801                 (unsigned long long) ret.rel_value);
802 #endif
803     GNUNET_STATISTICS_update (GST_stats,
804                               gettext_noop ("# ms throttling suggested"),
805                               (int64_t) ret.rel_value, GNUNET_NO);
806   }
807   return ret;
808 }
809
810
811 /**
812  * Keep the connection to the given neighbour alive longer,
813  * we received a KEEPALIVE (or equivalent).
814  *
815  * @param neighbour neighbour to keep alive
816  */
817 void
818 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
819 {
820   struct NeighbourMapEntry *n;
821
822   n = lookup_neighbour (neighbour);
823   if (NULL == n)
824   {
825     GNUNET_STATISTICS_update (GST_stats,
826                               gettext_noop
827                               ("# KEEPALIVE messages discarded (not connected)"),
828                               1, GNUNET_NO);
829     return;
830   }
831   GNUNET_SCHEDULER_cancel (n->timeout_task);
832   n->timeout_task =
833       GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
834                                     &neighbour_timeout_task, n);
835 }
836
837
838 /**
839  * Change the incoming quota for the given peer.
840  *
841  * @param neighbour identity of peer to change qutoa for
842  * @param quota new quota
843  */
844 void
845 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
846                                    struct GNUNET_BANDWIDTH_Value32NBO quota)
847 {
848   struct NeighbourMapEntry *n;
849
850   n = lookup_neighbour (neighbour);
851   if (n == NULL)
852   {
853     GNUNET_STATISTICS_update (GST_stats,
854                               gettext_noop
855                               ("# SET QUOTA messages ignored (no such peer)"),
856                               1, GNUNET_NO);
857     return;
858   }
859   GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
860   if (0 != ntohl (quota.value__))
861     return;
862 #if DEBUG_TRANSPORT
863   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
864               GNUNET_i2s (&n->id), "SET_QUOTA");
865 #endif
866   GNUNET_STATISTICS_update (GST_stats,
867                             gettext_noop ("# disconnects due to quota of 0"), 1,
868                             GNUNET_NO);
869   disconnect_neighbour (n);
870 }
871
872
873 /**
874  * Closure for the neighbours_iterate function.
875  */
876 struct IteratorContext
877 {
878   /**
879    * Function to call on each connected neighbour.
880    */
881   GST_NeighbourIterator cb;
882
883   /**
884    * Closure for 'cb'.
885    */
886   void *cb_cls;
887 };
888
889
890 /**
891  * Call the callback from the closure for each connected neighbour.
892  *
893  * @param cls the 'struct IteratorContext'
894  * @param key the hash of the public key of the neighbour
895  * @param value the 'struct NeighbourMapEntry'
896  * @return GNUNET_OK (continue to iterate)
897  */
898 static int
899 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
900 {
901   struct IteratorContext *ic = cls;
902   struct NeighbourMapEntry *n = value;
903
904   if (GNUNET_YES != n->is_connected)
905     return GNUNET_OK;
906   GNUNET_assert (n->ats_count > 0);
907   ic->cb (ic->cb_cls, &n->id, n->ats, n->ats_count - 1);
908   return GNUNET_OK;
909 }
910
911
912 /**
913  * Iterate over all connected neighbours.
914  *
915  * @param cb function to call
916  * @param cb_cls closure for cb
917  */
918 void
919 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
920 {
921   struct IteratorContext ic;
922
923   ic.cb = cb;
924   ic.cb_cls = cb_cls;
925   GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
926 }
927
928
929 /**
930  * If we have an active connection to the given target, it must be shutdown.
931  *
932  * @param target peer to disconnect from
933  */
934 void
935 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
936 {
937   struct NeighbourMapEntry *n;
938   struct GNUNET_TRANSPORT_PluginFunctions *papi;
939   struct GNUNET_MessageHeader disconnect_msg;
940
941   n = lookup_neighbour (target);
942   if (NULL == n)
943     return;                     /* not active */
944   if (GNUNET_YES == n->is_connected)
945   {
946     /* we're actually connected, send DISCONNECT message */
947     disconnect_msg.size = htons (sizeof (struct GNUNET_MessageHeader));
948     disconnect_msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
949     papi = GST_plugins_find (n->plugin_name);
950     if (papi != NULL)
951       papi->send (papi->cls, target, (const void *) &disconnect_msg,
952                   sizeof (struct GNUNET_MessageHeader),
953                   UINT32_MAX /* priority */ ,
954                   GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
955                   GNUNET_YES, NULL, NULL);
956   }
957   disconnect_neighbour (n);
958 }
959
960
961 /* end of file gnunet-service-transport_neighbours.c */