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