Forget to commit some files
[oweals/gnunet.git] / src / topology / gnunet-daemon-topology.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2007-2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file topology/gnunet-daemon-topology.c
23  * @brief code for maintaining the cadet topology
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_friends_lib.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_statistics_service.h"
34 #include "gnunet_transport_service.h"
35
36
37 /**
38  * Minimum required delay between calls to GNUNET_TRANSPORT_try_connect.
39  */
40 #define MAX_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 250)
41
42 /**
43  * Maximum delay allowed delay between calls to GNUNET_TRANSPORT_try_connect.
44  */
45 #define MIN_CONNECT_FREQUENCY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 2500)
46
47 /**
48  * For how long do we blacklist a peer after a failed connection
49  * attempt?  This is the baseline factor which is then multiplied by
50  * two to the power of the number of failed attempts.
51  */
52 #define GREYLIST_AFTER_ATTEMPT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
53
54 /**
55  * For how long do we blacklist a friend after a failed connection
56  * attempt?  This is the baseline factor which is then multiplied by
57  * two to the power of the number of failed attempts.
58  */
59 #define GREYLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
60
61 /**
62  * For how long do we blacklist anyone under any cirumstances at least after a failed connection
63  * attempt?  This is the absolute minimum, regardless of what the calculation based on
64  * exponential backoff returns.
65  */
66 #define GREYLIST_AFTER_ATTEMPT_MIN GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
67
68 /**
69  * For how long do we blacklist anyone under any cirumstances at most after a failed connection
70  * attempt?  This is the absolute maximum, regardless of what the calculation based on
71  * exponential back-off returns.
72  */
73 #define GREYLIST_AFTER_ATTEMPT_MAX GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 1)
74
75 /**
76  * At what frequency do we sent HELLOs to a peer?
77  */
78 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5)
79
80 /**
81  * After what time period do we expire the HELLO Bloom filter?
82  */
83 #define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4)
84
85
86 /**
87  * Record for neighbours, friends and blacklisted peers.
88  */
89 struct Peer
90 {
91   /**
92    * Which peer is this entry about?
93    */
94   struct GNUNET_PeerIdentity pid;
95
96   /**
97    * Our handle for the request to transmit HELLOs to this peer; NULL
98    * if no such request is pending.
99    */
100   struct GNUNET_CORE_TransmitHandle *hello_req;
101
102   /**
103    * Pointer to the HELLO message of this peer; can be NULL.
104    */
105   struct GNUNET_HELLO_Message *hello;
106
107   /**
108    * Bloom filter used to mark which peers already got the HELLO
109    * from this peer.
110    */
111   struct GNUNET_CONTAINER_BloomFilter *filter;
112
113   /**
114    * Until what time should we not try to connect again
115    * to this peer?
116    */
117   struct GNUNET_TIME_Absolute greylisted_until;
118
119   /**
120    * Next time we are allowed to transmit a HELLO to this peer?
121    */
122   struct GNUNET_TIME_Absolute next_hello_allowed;
123
124   /**
125    * When should we reset the bloom filter of this entry?
126    */
127   struct GNUNET_TIME_Absolute filter_expiration;
128
129   /**
130    * When should try next connection attempt?
131    */
132   struct GNUNET_TIME_Absolute  next_connect_attempt;
133
134   /**
135    * ID of task we use to wait for the time to send the next HELLO
136    * to this peer.
137    */
138   struct GNUNET_SCHEDULER_Task * hello_delay_task;
139
140   /**
141    * Task for issuing GNUNET_TRANSPORT_try_connect for this peer.
142    */
143   struct GNUNET_SCHEDULER_Task * attempt_connect_task;
144
145   /**
146    * ID of task we use to clear peers from the greylist.
147    */
148   struct GNUNET_SCHEDULER_Task * greylist_clean_task;
149
150   /**
151    * How often have we tried so far?
152    */
153   unsigned int connect_attempts;
154
155   /**
156    * Is this peer listed here because he is a friend?
157    */
158   int is_friend;
159
160   /**
161    * Are we connected to this peer right now?
162    */
163   int is_connected;
164
165 };
166
167
168 /**
169  * Our peerinfo notification context.  We use notification
170  * to instantly learn about new peers as they are discovered.
171  */
172 static struct GNUNET_PEERINFO_NotifyContext *peerinfo_notify;
173
174 /**
175  * Our configuration.
176  */
177 static const struct GNUNET_CONFIGURATION_Handle *cfg;
178
179 /**
180  * Handle to the core API.
181  */
182 static struct GNUNET_CORE_Handle *handle;
183
184 /**
185  * Handle to the transport API.
186  */
187 static struct GNUNET_TRANSPORT_Handle *transport;
188
189 /**
190  * Identity of this peer.
191  */
192 static struct GNUNET_PeerIdentity my_identity;
193
194 /**
195  * All of our friends, all of our current neighbours and all peers for
196  * which we have HELLOs.  So pretty much everyone.  Maps peer identities
197  * to 'struct Peer *' values.
198  */
199 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
200
201 /**
202  * Handle for reporting statistics.
203  */
204 static struct GNUNET_STATISTICS_Handle *stats;
205
206 /**
207  * Blacklist (NULL if we have none).
208  */
209 static struct GNUNET_TRANSPORT_Blacklist *blacklist;
210
211 /**
212  * When can we next ask transport to create a connection?
213  */
214 static struct GNUNET_TIME_Absolute next_connect_attempt;
215
216 /**
217  * Task scheduled to try to add peers.
218  */
219 static struct GNUNET_SCHEDULER_Task * add_task;
220
221 /**
222  * Flag to disallow non-friend connections (pure F2F mode).
223  */
224 static int friends_only;
225
226 /**
227  * Minimum number of friends to have in the
228  * connection set before we allow non-friends.
229  */
230 static unsigned int minimum_friend_count;
231
232 /**
233  * Number of peers (friends and others) that we are currently connected to.
234  */
235 static unsigned int connection_count;
236
237 /**
238  * Target number of connections.
239  */
240 static unsigned int target_connection_count;
241
242 /**
243  * Number of friends that we are currently connected to.
244  */
245 static unsigned int friend_count;
246
247
248 /**
249  * Function that decides if a connection is acceptable or not.
250  * If we have a blacklist, only friends are allowed, so the check
251  * is rather simple.
252  *
253  * @param cls closure
254  * @param pid peer to approve or disapprove
255  * @return GNUNET_OK if the connection is allowed
256  */
257 static int
258 blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
259 {
260   struct Peer *pos;
261
262   pos = GNUNET_CONTAINER_multipeermap_get (peers, pid);
263   if ((pos != NULL) && (pos->is_friend == GNUNET_YES))
264     return GNUNET_OK;
265   GNUNET_STATISTICS_update (stats, gettext_noop ("# peers blacklisted"), 1,
266                             GNUNET_NO);
267   return GNUNET_SYSERR;
268 }
269
270
271 /**
272  * Whitelist all peers that we blacklisted; we've passed
273  * the minimum number of friends.
274  */
275 static void
276 whitelist_peers ()
277 {
278   if (blacklist != NULL)
279   {
280     GNUNET_TRANSPORT_blacklist_cancel (blacklist);
281     blacklist = NULL;
282   }
283 }
284
285
286 /**
287  * Check if an additional connection from the given peer is allowed.
288  *
289  * @param peer connection to check
290  * @return GNUNET_OK if the connection is allowed
291  */
292 static int
293 is_connection_allowed (struct Peer *peer)
294 {
295   if (0 ==
296       memcmp (&my_identity, &peer->pid, sizeof (struct GNUNET_PeerIdentity)))
297     return GNUNET_SYSERR;       /* disallow connections to self */
298   if (peer->is_friend)
299     return GNUNET_OK;
300   if (GNUNET_YES == friends_only)
301   {
302     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303                 "Determined that `%s' is not allowed to connect (not a friend)\n",
304                 GNUNET_i2s (&peer->pid));
305     return GNUNET_SYSERR;
306   }
307   if (friend_count >= minimum_friend_count)
308     return GNUNET_OK;
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Determined that `%s' is not allowed to connect (not enough connected friends)\n",
311               GNUNET_i2s (&peer->pid));
312   return GNUNET_SYSERR;
313 }
314
315
316 /**
317  * Free all resources associated with the given peer.
318  *
319  * @param cls closure (not used)
320  * @param pid identity of the peer
321  * @param value peer to free
322  * @return GNUNET_YES (always: continue to iterate)
323  */
324 static int
325 free_peer (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
326 {
327   struct Peer *pos = value;
328
329   GNUNET_break (GNUNET_NO == pos->is_connected);
330   GNUNET_break (GNUNET_OK ==
331                 GNUNET_CONTAINER_multipeermap_remove (peers, pid, pos));
332   if (pos->hello_req != NULL)
333     GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
334   if (pos->hello_delay_task != NULL)
335     GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
336   if (pos->attempt_connect_task != NULL)
337     GNUNET_SCHEDULER_cancel (pos->attempt_connect_task);
338   if (pos->greylist_clean_task != NULL)
339     GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
340   GNUNET_free_non_null (pos->hello);
341   if (pos->filter != NULL)
342     GNUNET_CONTAINER_bloomfilter_free (pos->filter);
343   GNUNET_free (pos);
344   return GNUNET_YES;
345 }
346
347
348 /**
349  * Discard peer entries for greylisted peers
350  * where the greylisting has expired.
351  *
352  * @param cls 'struct Peer' to greylist
353  * @param tc scheduler context
354  */
355 static void
356 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
357
358
359 /**
360  * Try to connect to the specified peer.
361  *
362  * @param pos peer to connect to
363  */
364 static void
365 attempt_connect (struct Peer *pos)
366 {
367   struct GNUNET_TIME_Relative rem;
368
369   if ((connection_count >= target_connection_count) &&
370       (friend_count >= minimum_friend_count))
371     return;
372   if (GNUNET_YES == pos->is_connected)
373     return;
374   if (GNUNET_OK != is_connection_allowed (pos))
375     return;
376   if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
377     return;
378   if (GNUNET_YES == pos->is_friend)
379     rem = GREYLIST_AFTER_ATTEMPT_FRIEND;
380   else
381     rem = GREYLIST_AFTER_ATTEMPT;
382   rem = GNUNET_TIME_relative_multiply (rem, connection_count);
383   rem = GNUNET_TIME_relative_divide (rem, target_connection_count);
384   if (pos->connect_attempts > 30)
385     pos->connect_attempts = 30;
386   else
387     pos->connect_attempts ++;
388   rem = GNUNET_TIME_relative_multiply (rem, 1 << pos->connect_attempts);
389   rem = GNUNET_TIME_relative_max (rem, GREYLIST_AFTER_ATTEMPT_MIN);
390   rem = GNUNET_TIME_relative_min (rem, GREYLIST_AFTER_ATTEMPT_MAX);
391   pos->greylisted_until = GNUNET_TIME_relative_to_absolute (rem);
392
393   pos->next_connect_attempt = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(),
394       GNUNET_TIME_relative_multiply (MAX_CONNECT_FREQUENCY_DELAY, pos->connect_attempts));
395   pos->next_connect_attempt = GNUNET_TIME_absolute_min (pos->next_connect_attempt,
396       GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), MIN_CONNECT_FREQUENCY_DELAY));
397
398   if (pos->greylist_clean_task != NULL)
399     GNUNET_SCHEDULER_cancel (pos->greylist_clean_task);
400   pos->greylist_clean_task =
401       GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
402   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking  to connect to `%s'\n",
403               GNUNET_i2s (&pos->pid));
404   GNUNET_STATISTICS_update (stats,
405                             gettext_noop
406                             ("# connect requests issued to transport"), 1,
407                             GNUNET_NO);
408   GNUNET_TRANSPORT_try_connect (transport, &pos->pid, NULL, NULL);
409 }
410
411
412 /**
413  * Try to connect to the specified peer.
414  *
415  * @param cls peer to connect to
416  * @param tc scheduler context
417  */
418 static void
419 do_attempt_connect (void *cls,
420                     const struct GNUNET_SCHEDULER_TaskContext *tc)
421 {
422   struct Peer *pos = cls;
423   struct GNUNET_TIME_Relative delay;
424
425   pos->attempt_connect_task = NULL;
426   if (GNUNET_YES == pos->is_connected)
427     return;
428
429   /* Try next connection attempt, when:
430    * - topology allows the next transport connection attempt
431    * and
432    * - the next connection event for this peer is allowed
433    */
434   delay = GNUNET_TIME_relative_max (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
435       GNUNET_TIME_absolute_get_remaining (pos->next_connect_attempt));
436
437   if (delay.rel_value_us > 0)
438   {
439     pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (delay,
440                                                               &do_attempt_connect,
441                                                               pos);
442     return;
443   }
444   next_connect_attempt = GNUNET_TIME_relative_to_absolute (MAX_CONNECT_FREQUENCY_DELAY);
445   attempt_connect (pos);
446 }
447
448
449 /**
450  * Schedule a task to try to connect to the specified peer.
451  *
452  * @param pos peer to connect to
453  */
454 static void
455 schedule_attempt_connect (struct Peer *pos)
456 {
457   if (NULL != pos->attempt_connect_task)
458     return;
459   pos->attempt_connect_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (next_connect_attempt),
460                                                             &do_attempt_connect,
461                                                             pos);
462 }
463
464
465 /**
466  * Discard peer entries for greylisted peers
467  * where the greylisting has expired.
468  *
469  * @param cls 'struct Peer' to greylist
470  * @param tc scheduler context
471  */
472 static void
473 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
474 {
475   struct Peer *pos = cls;
476   struct GNUNET_TIME_Relative rem;
477
478   pos->greylist_clean_task = NULL;
479   rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
480   if (0 == rem.rel_value_us)
481   {
482     schedule_attempt_connect (pos);
483   }
484   else
485   {
486     pos->greylist_clean_task =
487         GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
488   }
489   if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
490       (NULL == pos->hello))
491   {
492     free_peer (NULL, &pos->pid, pos);
493     return;
494   }
495 }
496
497
498 /**
499  * Create a new entry in the peer list.
500  *
501  * @param peer identity of the new entry
502  * @param hello hello message, can be NULL
503  * @param is_friend is the new entry for a friend?
504  * @return the new entry
505  */
506 static struct Peer *
507 make_peer (const struct GNUNET_PeerIdentity *peer,
508            const struct GNUNET_HELLO_Message *hello, int is_friend)
509 {
510   struct Peer *ret;
511
512   ret = GNUNET_new (struct Peer);
513   ret->pid = *peer;
514   ret->is_friend = is_friend;
515   ret->next_connect_attempt = GNUNET_TIME_absolute_get();
516   if (hello != NULL)
517   {
518     ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
519     memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
520   }
521   GNUNET_break (GNUNET_OK ==
522                 GNUNET_CONTAINER_multipeermap_put (peers, peer,
523                                                    ret,
524                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
525   return ret;
526 }
527
528
529 /**
530  * Setup bloom filter for the given peer entry.
531  *
532  * @param peer entry to initialize
533  */
534 static void
535 setup_filter (struct Peer *peer)
536 {
537   struct GNUNET_HashCode hc;
538
539   /* 2^{-5} chance of not sending a HELLO to a peer is
540    * acceptably small (if the filter is 50% full);
541    * 64 bytes of memory are small compared to the rest
542    * of the data structure and would only really become
543    * "useless" once a HELLO has been passed on to ~100
544    * other peers, which is likely more than enough in
545    * any case; hence 64, 5 as bloomfilter parameters. */
546   peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
547   peer->filter_expiration =
548       GNUNET_TIME_relative_to_absolute
549       (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
550   /* never send a peer its own HELLO */
551   GNUNET_CRYPTO_hash (&peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
552   GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc);
553 }
554
555
556 /**
557  * Function to fill send buffer with HELLO.
558  *
559  * @param cls 'struct Peer' of the target peer
560  * @param size number of bytes available in @a buf
561  * @param buf where the callee should write the message
562  * @return number of bytes written to @a buf
563  */
564 static size_t
565 hello_advertising_ready (void *cls, size_t size, void *buf);
566
567
568 /**
569  * Closure for 'find_advertisable_hello'.
570  */
571 struct FindAdvHelloContext
572 {
573
574   /**
575    * Peer we want to advertise to.
576    */
577   struct Peer *peer;
578
579   /**
580    * Where to store the result (peer selected for advertising).
581    */
582   struct Peer *result;
583
584   /**
585    * Maximum HELLO size we can use right now.
586    */
587   size_t max_size;
588
589   struct GNUNET_TIME_Relative next_adv;
590 };
591
592
593 /**
594  * Find a peer that would be reasonable for advertising.
595  *
596  * @param cls closure
597  * @param pid identity of a peer
598  * @param value 'struct Peer*' for the peer we are considering
599  * @return GNUNET_YES (continue iteration)
600  */
601 static int
602 find_advertisable_hello (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
603 {
604   struct FindAdvHelloContext *fah = cls;
605   struct Peer *pos = value;
606   struct GNUNET_TIME_Relative rst_time;
607   struct GNUNET_HashCode hc;
608   size_t hs;
609
610   if (pos == fah->peer)
611     return GNUNET_YES;
612   if (pos->hello == NULL)
613     return GNUNET_YES;
614   rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
615   if (0 == rst_time.rel_value_us)
616   {
617     /* time to discard... */
618     GNUNET_CONTAINER_bloomfilter_free (pos->filter);
619     setup_filter (pos);
620   }
621   fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
622   hs = GNUNET_HELLO_size (pos->hello);
623   if (hs > fah->max_size)
624     return GNUNET_YES;
625   GNUNET_CRYPTO_hash (&fah->peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
626   if (GNUNET_NO ==
627       GNUNET_CONTAINER_bloomfilter_test (pos->filter,
628                                          &hc))
629     fah->result = pos;
630   return GNUNET_YES;
631 }
632
633
634 /**
635  * Calculate when we would like to send the next HELLO to this
636  * peer and ask for it.
637  *
638  * @param cls for which peer to schedule the HELLO
639  * @param tc task context
640  */
641 static void
642 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
643 {
644   struct Peer *pl = cls;
645   struct FindAdvHelloContext fah;
646   size_t next_want;
647   struct GNUNET_TIME_Relative delay;
648
649   pl->hello_delay_task = NULL;
650   GNUNET_assert (GNUNET_YES == pl->is_connected);
651   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
652     return;                     /* we're out of here */
653   if (pl->hello_req != NULL)
654     return;                     /* did not finish sending the previous one */
655   /* find applicable HELLOs */
656   fah.peer = pl;
657   fah.result = NULL;
658   fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
659   fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
660   GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
661   pl->hello_delay_task =
662       GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
663   if (fah.result == NULL)
664     return;
665   next_want = GNUNET_HELLO_size (fah.result->hello);
666   delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
667   if (0 == delay.rel_value_us)
668   {
669     /* now! */
670     pl->hello_req =
671         GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES,
672                                            GNUNET_CORE_PRIO_BEST_EFFORT,
673                                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
674                                            &pl->pid, next_want,
675                                            &hello_advertising_ready, pl);
676   }
677 }
678
679
680 /**
681  * Cancel existing requests for sending HELLOs to this peer
682  * and recalculate when we should send HELLOs to it based
683  * on our current state (something changed!).
684  *
685  * @param cls closure, 'struct Peer' to skip, or NULL
686  * @param pid identity of a peer
687  * @param value 'struct Peer*' for the peer
688  * @return GNUNET_YES (always)
689  */
690 static int
691 reschedule_hellos (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
692 {
693   struct Peer *peer = value;
694   struct Peer *skip = cls;
695
696   if (skip == peer)
697     return GNUNET_YES;
698   if (!peer->is_connected)
699     return GNUNET_YES;
700   if (peer->hello_req != NULL)
701   {
702     GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
703     peer->hello_req = NULL;
704   }
705   if (peer->hello_delay_task != NULL)
706   {
707     GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
708     peer->hello_delay_task = NULL;
709   }
710   peer->hello_delay_task =
711       GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
712   return GNUNET_YES;
713 }
714
715
716 /**
717  * Method called whenever a peer connects.
718  *
719  * @param cls closure
720  * @param peer peer identity this notification is about
721  */
722 static void
723 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
724 {
725   struct Peer *pos;
726
727   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
728               "Core told us that we are connecting to `%s'\n",
729               GNUNET_i2s (peer));
730   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
731     return;
732
733   connection_count++;
734   GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
735                          connection_count, GNUNET_NO);
736   pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
737   if (NULL == pos)
738   {
739     pos = make_peer (peer, NULL, GNUNET_NO);
740     GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
741   }
742   else
743   {
744     GNUNET_assert (GNUNET_NO == pos->is_connected);
745     pos->greylisted_until.abs_value_us = 0;        /* remove greylisting */
746   }
747   pos->is_connected = GNUNET_YES;
748   pos->connect_attempts = 0;    /* re-set back-off factor */
749   pos->next_connect_attempt = GNUNET_TIME_absolute_get(); /* re-set back-off factor */
750   if (pos->is_friend)
751   {
752     if ((friend_count == minimum_friend_count - 1) &&
753         (GNUNET_YES != friends_only))
754       whitelist_peers ();
755     friend_count++;
756     GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
757                            friend_count, GNUNET_NO);
758   }
759   reschedule_hellos (NULL, peer, pos);
760 }
761
762
763 /**
764  * Try to add more peers to our connection set.
765  *
766  * @param cls closure, not used
767  * @param pid identity of a peer
768  * @param value 'struct Peer*' for the peer
769  * @return GNUNET_YES (continue to iterate)
770  */
771 static int
772 try_add_peers (void *cls, const struct GNUNET_PeerIdentity * pid, void *value)
773 {
774   struct Peer *pos = value;
775
776   schedule_attempt_connect (pos);
777   return GNUNET_YES;
778 }
779
780
781 /**
782  * Add peers and schedule connection attempt
783  *
784  * @param cls unused, NULL
785  * @param tc scheduler context
786  */
787 static void
788 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
789 {
790   add_task = NULL;
791
792   GNUNET_CONTAINER_multipeermap_iterate (peers, &try_add_peers, NULL);
793 }
794
795
796 /**
797  * Method called whenever a peer disconnects.
798  *
799  * @param cls closure
800  * @param peer peer identity this notification is about
801  */
802 static void
803 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
804 {
805   struct Peer *pos;
806
807   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
808     return;
809   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810               "Core told us that we disconnected from `%s'\n",
811               GNUNET_i2s (peer));
812   pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
813   if (NULL == pos)
814   {
815     GNUNET_break (0);
816     return;
817   }
818   if (pos->is_connected != GNUNET_YES)
819   {
820     GNUNET_break (0);
821     return;
822   }
823   pos->is_connected = GNUNET_NO;
824   connection_count--;
825   if (NULL != pos->hello_req)
826   {
827     GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
828     pos->hello_req = NULL;
829   }
830   if (NULL != pos->hello_delay_task)
831   {
832     GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
833     pos->hello_delay_task = NULL;
834   }
835   GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
836                          connection_count, GNUNET_NO);
837   if (pos->is_friend)
838   {
839     friend_count--;
840     GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
841                            friend_count, GNUNET_NO);
842   }
843   if (((connection_count < target_connection_count) ||
844        (friend_count < minimum_friend_count)) &&
845       (NULL == add_task))
846     add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
847   if ((friend_count < minimum_friend_count) && (blacklist == NULL))
848     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
849 }
850
851
852 /**
853  * Iterator called on each address.
854  *
855  * @param cls flag that we will set if we see any addresses
856  * @param address the address of the peer
857  * @param expiration when will the given address expire
858  * @return GNUNET_SYSERR always, to terminate iteration
859  */
860 static int
861 address_iterator (void *cls, const struct GNUNET_HELLO_Address *address,
862                   struct GNUNET_TIME_Absolute expiration)
863 {
864   int *flag = cls;
865
866   *flag = GNUNET_YES;
867   return GNUNET_SYSERR;
868 }
869
870
871 /**
872  * We've gotten a HELLO from another peer.  Consider it for
873  * advertising.
874  *
875  * @param hello the HELLO we got
876  */
877 static void
878 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
879 {
880   int have_address;
881   struct GNUNET_PeerIdentity pid;
882   struct GNUNET_TIME_Absolute dt;
883   struct GNUNET_HELLO_Message *nh;
884   struct Peer *peer;
885   uint16_t size;
886
887   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
888   {
889     GNUNET_break (0);
890     return;
891   }
892   if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
893     return;                     /* that's me! */
894   have_address = GNUNET_NO;
895   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
896                                   &have_address);
897   if (GNUNET_NO == have_address)
898     return;                     /* no point in advertising this one... */
899   peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
900   if (NULL == peer)
901   {
902     peer = make_peer (&pid, hello, GNUNET_NO);
903   }
904   else if (peer->hello != NULL)
905   {
906     dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
907     if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
908       return;                   /* nothing new here */
909   }
910   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
911               "Found `%s' from peer `%s' for advertising\n", "HELLO",
912               GNUNET_i2s (&pid));
913   if (peer->hello != NULL)
914   {
915     nh = GNUNET_HELLO_merge (peer->hello, hello);
916     GNUNET_free (peer->hello);
917     peer->hello = nh;
918   }
919   else
920   {
921     size = GNUNET_HELLO_size (hello);
922     peer->hello = GNUNET_malloc (size);
923     memcpy (peer->hello, hello, size);
924   }
925   if (peer->filter != NULL)
926     GNUNET_CONTAINER_bloomfilter_free (peer->filter);
927   setup_filter (peer);
928   /* since we have a new HELLO to pick from, re-schedule all
929    * HELLO requests that are not bound by the HELLO send rate! */
930   GNUNET_CONTAINER_multipeermap_iterate (peers, &reschedule_hellos, peer);
931 }
932
933
934 /**
935  * PEERINFO calls this function to let us know about a possible peer
936  * that we might want to connect to.
937  *
938  * @param cls closure (not used)
939  * @param peer potential peer to connect to
940  * @param hello HELLO for this peer (or NULL)
941  * @param err_msg NULL if successful, otherwise contains error message
942  */
943 static void
944 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
945               const struct GNUNET_HELLO_Message *hello, const char *err_msg)
946 {
947   struct Peer *pos;
948
949   if (err_msg != NULL)
950   {
951     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952                 _("Error in communication with PEERINFO service: %s\n"),
953                 err_msg);
954     GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
955     peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL);
956     return;
957   }
958   GNUNET_assert (peer != NULL);
959   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
960     return;                     /* that's me! */
961   if (hello == NULL)
962   {
963     /* free existing HELLO, if any */
964     pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
965     if (NULL != pos)
966     {
967       GNUNET_free_non_null (pos->hello);
968       pos->hello = NULL;
969       if (pos->filter != NULL)
970       {
971         GNUNET_CONTAINER_bloomfilter_free (pos->filter);
972         pos->filter = NULL;
973       }
974       if ((GNUNET_NO == pos->is_connected) && (GNUNET_NO == pos->is_friend) &&
975           (0 ==
976            GNUNET_TIME_absolute_get_remaining (pos->
977                                                greylisted_until).rel_value_us))
978         free_peer (NULL, &pos->pid, pos);
979     }
980     return;
981   }
982   consider_for_advertising (hello);
983   pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
984   if (pos == NULL)
985     pos = make_peer (peer, hello, GNUNET_NO);
986   GNUNET_assert (NULL != pos);
987   if (GNUNET_YES == pos->is_connected)
988   {
989     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
990                 GNUNET_i2s (peer));
991     return;
992   }
993   if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value_us > 0)
994   {
995     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
996                 GNUNET_i2s (peer));
997     return;                     /* peer still greylisted */
998   }
999   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
1000               GNUNET_i2s (peer));
1001   schedule_attempt_connect (pos);
1002 }
1003
1004
1005 /**
1006  * Function called after #GNUNET_CORE_connect has succeeded
1007  * (or failed for good).
1008  *
1009  * @param cls closure
1010  * @param my_id ID of this peer, NULL if we failed
1011  */
1012 static void
1013 core_init (void *cls,
1014            const struct GNUNET_PeerIdentity *my_id)
1015 {
1016   if (NULL == my_id)
1017   {
1018     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1019                 _
1020                 ("Failed to connect to core service, can not manage topology!\n"));
1021     GNUNET_SCHEDULER_shutdown ();
1022     return;
1023   }
1024   my_identity = *my_id;
1025   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026               "I am peer `%s'\n",
1027               GNUNET_i2s (my_id));
1028   peerinfo_notify = GNUNET_PEERINFO_notify (cfg, GNUNET_NO,
1029                                             &process_peer, NULL);
1030 }
1031
1032
1033 /**
1034  * Process friend found in FRIENDS file.
1035  *
1036  * @param cls pointer to an `unsigned int` to be incremented per friend found
1037  * @param pid identity of the friend
1038  */
1039 static void
1040 handle_friend (void *cls,
1041                const struct GNUNET_PeerIdentity *pid)
1042 {
1043   unsigned int *entries_found = cls;
1044   struct Peer *fl;
1045
1046   if (0 == memcmp (pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1047   {
1048     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1049                 _("Found myself `%s' in friend list (useless, ignored)\n"),
1050                 GNUNET_i2s (pid));
1051     return;
1052   }
1053   (*entries_found)++;
1054   fl = make_peer (pid, NULL, GNUNET_YES);
1055   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1056               _("Found friend `%s' in configuration\n"),
1057               GNUNET_i2s (&fl->pid));
1058 }
1059
1060
1061 /**
1062  * Read the friends file.
1063  */
1064 static void
1065 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
1066 {
1067   unsigned int entries_found;
1068
1069   entries_found = 0;
1070   if (GNUNET_OK !=
1071       GNUNET_FRIENDS_parse (cfg,
1072                             &handle_friend,
1073                             &entries_found))
1074   {
1075     if ((friends_only) || (minimum_friend_count > 0))
1076       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1077                   _("Encountered errors parsing friends list!\n"));
1078   }
1079   GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1080                             entries_found, GNUNET_NO);
1081   if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1082   {
1083     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1084                 _("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1085   }
1086   if ((minimum_friend_count > target_connection_count) &&
1087       (friends_only == GNUNET_NO))
1088   {
1089     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1090                 _("More friendly connections required than target total number of connections.\n"));
1091   }
1092 }
1093
1094
1095 /**
1096  * This function is called whenever an encrypted HELLO message is
1097  * received.
1098  *
1099  * @param cls closure
1100  * @param other the other peer involved (sender or receiver, NULL
1101  *        for loopback messages where we are both sender and receiver)
1102  * @param message the actual HELLO message
1103  * @return GNUNET_OK to keep the connection open,
1104  *         GNUNET_SYSERR to close it (signal serious error)
1105  */
1106 static int
1107 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1108                         const struct GNUNET_MessageHeader *message)
1109 {
1110   struct Peer *peer;
1111   struct GNUNET_PeerIdentity pid;
1112
1113   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1114               "HELLO", GNUNET_i2s (other));
1115   if (GNUNET_OK !=
1116       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1117   {
1118     GNUNET_break_op (0);
1119     return GNUNET_SYSERR;
1120   }
1121   GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1122                             1, GNUNET_NO);
1123   peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid);
1124   if (NULL == peer)
1125   {
1126     if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1127       return GNUNET_OK;
1128   }
1129   else
1130   {
1131     if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1132       return GNUNET_OK;
1133     if ((GNUNET_YES != peer->is_friend) &&
1134         (friend_count < minimum_friend_count))
1135       return GNUNET_OK;
1136   }
1137   if (transport != NULL)
1138     GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1139   return GNUNET_OK;
1140 }
1141
1142
1143 /**
1144  * Function to fill send buffer with HELLO.
1145  *
1146  * @param cls 'struct Peer' of the target peer
1147  * @param size number of bytes available in buf
1148  * @param buf where the callee should write the message
1149  * @return number of bytes written to buf
1150  */
1151 static size_t
1152 hello_advertising_ready (void *cls, size_t size, void *buf)
1153 {
1154   struct Peer *pl = cls;
1155   struct FindAdvHelloContext fah;
1156   size_t want;
1157   struct GNUNET_HashCode hc;
1158
1159   pl->hello_req = NULL;
1160   GNUNET_assert (GNUNET_YES == pl->is_connected);
1161   /* find applicable HELLOs */
1162   fah.peer = pl;
1163   fah.result = NULL;
1164   fah.max_size = size;
1165   fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1166   GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah);
1167   want = 0;
1168   if (fah.result != NULL)
1169   {
1170     want = GNUNET_HELLO_size (fah.result->hello);
1171     GNUNET_assert (want <= size);
1172     memcpy (buf, fah.result->hello, want);
1173     GNUNET_CRYPTO_hash (&pl->pid, sizeof (struct GNUNET_PeerIdentity), &hc);
1174     GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &hc);
1175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1176                 (unsigned int) want);
1177     GNUNET_STATISTICS_update (stats,
1178                               gettext_noop ("# HELLO messages gossipped"), 1,
1179                               GNUNET_NO);
1180   }
1181
1182   if (pl->hello_delay_task != NULL)
1183     GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1184   pl->next_hello_allowed =
1185       GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1186   pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1187   return want;
1188 }
1189
1190
1191 /**
1192  * Last task run during shutdown.  Disconnects us from
1193  * the transport and core.
1194  *
1195  * @param cls unused, NULL
1196  * @param tc scheduler context
1197  */
1198 static void
1199 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1200 {
1201   if (NULL != peerinfo_notify)
1202   {
1203     GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1204     peerinfo_notify = NULL;
1205   }
1206   GNUNET_TRANSPORT_disconnect (transport);
1207   transport = NULL;
1208   if (handle != NULL)
1209   {
1210     GNUNET_CORE_disconnect (handle);
1211     handle = NULL;
1212   }
1213   whitelist_peers ();
1214   if (NULL != add_task)
1215   {
1216     GNUNET_SCHEDULER_cancel (add_task);
1217     add_task = NULL;
1218   }
1219   GNUNET_CONTAINER_multipeermap_iterate (peers, &free_peer, NULL);
1220   GNUNET_CONTAINER_multipeermap_destroy (peers);
1221   peers = NULL;
1222   if (stats != NULL)
1223   {
1224     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1225     stats = NULL;
1226   }
1227 }
1228
1229
1230 /**
1231  * Main function that will be run.
1232  *
1233  * @param cls closure
1234  * @param args remaining command-line arguments
1235  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1236  * @param c configuration
1237  */
1238 static void
1239 run (void *cls,
1240      char *const *args,
1241      const char *cfgfile,
1242      const struct GNUNET_CONFIGURATION_Handle *c)
1243 {
1244   static struct GNUNET_CORE_MessageHandler handlers[] = {
1245     {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1246     {NULL, 0, 0}
1247   };
1248   unsigned long long opt;
1249
1250   cfg = c;
1251   stats = GNUNET_STATISTICS_create ("topology", cfg);
1252   friends_only =
1253       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1254   if (GNUNET_OK !=
1255       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1256                                              &opt))
1257     opt = 0;
1258   minimum_friend_count = (unsigned int) opt;
1259   if (GNUNET_OK !=
1260       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1261                                              "TARGET-CONNECTION-COUNT", &opt))
1262     opt = 16;
1263   target_connection_count = (unsigned int) opt;
1264   peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2, GNUNET_NO);
1265
1266   if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1267     read_friends_file (cfg);
1268   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1269               "Topology would like %u connections with at least %u friends\n",
1270               target_connection_count, minimum_friend_count);
1271   if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1272     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1273   transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1274   handle =
1275       GNUNET_CORE_connect (cfg, NULL,
1276                            &core_init,
1277                            &connect_notify,
1278                            &disconnect_notify,
1279                            NULL, GNUNET_NO,
1280                            NULL, GNUNET_NO,
1281                            handlers);
1282   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1283                                 NULL);
1284   if (NULL == transport)
1285   {
1286     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1287                 _("Failed to connect to `%s' service.\n"), "transport");
1288     GNUNET_SCHEDULER_shutdown ();
1289     return;
1290   }
1291   if (NULL == handle)
1292   {
1293     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1294                 _("Failed to connect to `%s' service.\n"), "core");
1295     GNUNET_SCHEDULER_shutdown ();
1296     return;
1297   }
1298 }
1299
1300
1301 /**
1302  * The main function for the topology daemon.
1303  *
1304  * @param argc number of arguments from the command line
1305  * @param argv command line arguments
1306  * @return 0 ok, 1 on error
1307  */
1308 int
1309 main (int argc, char *const *argv)
1310 {
1311   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1312     GNUNET_GETOPT_OPTION_END
1313   };
1314   int ret;
1315
1316   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1317     return 2;
1318
1319   ret =
1320       (GNUNET_OK ==
1321        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1322                            _
1323                            ("GNUnet topology control (maintaining P2P cadet and F2F constraints)"),
1324                            options, &run, NULL)) ? 0 : 1;
1325   GNUNET_free ((void*) argv);
1326   return ret;
1327 }
1328
1329
1330 #if defined(LINUX) && defined(__GLIBC__)
1331 #include <malloc.h>
1332
1333 /**
1334  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1335  */
1336 void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
1337 {
1338   mallopt (M_TRIM_THRESHOLD, 4 * 1024);
1339   mallopt (M_TOP_PAD, 1 * 1024);
1340   malloc_trim (0);
1341 }
1342 #endif
1343
1344 /* end of gnunet-daemon-topology.c */