3e4fd26850a27f570c6166daadd98a43808730d1
[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
380                             ("# connect requests issued to transport"), 1,
381                             GNUNET_NO);
382   GNUNET_TRANSPORT_try_connect (transport, &pos->pid);
383 }
384
385
386 /**
387  * Discard peer entries for greylisted peers
388  * where the greylisting has expired.
389  *
390  * @param cls 'struct Peer' to greylist
391  * @param tc scheduler context
392  */
393 static void
394 remove_from_greylist (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
395 {
396   struct Peer *pos = cls;
397   struct GNUNET_TIME_Relative rem;
398
399   pos->greylist_clean_task = GNUNET_SCHEDULER_NO_TASK;
400   rem = GNUNET_TIME_absolute_get_remaining (pos->greylisted_until);
401   if (rem.rel_value == 0)
402   {
403     attempt_connect (pos);
404   }
405   else
406   {
407     pos->greylist_clean_task =
408         GNUNET_SCHEDULER_add_delayed (rem, &remove_from_greylist, pos);
409   }
410   if ((GNUNET_NO == pos->is_friend) && (GNUNET_NO == pos->is_connected) &&
411       (NULL == pos->hello))
412   {
413     free_peer (NULL, &pos->pid.hashPubKey, pos);
414     return;
415   }
416 }
417
418
419 /**
420  * Create a new entry in the peer list.
421  *
422  * @param peer identity of the new entry
423  * @param hello hello message, can be NULL
424  * @param is_friend is the new entry for a friend?
425  * @return the new entry
426  */
427 static struct Peer *
428 make_peer (const struct GNUNET_PeerIdentity *peer,
429            const struct GNUNET_HELLO_Message *hello, int is_friend)
430 {
431   struct Peer *ret;
432
433   ret = GNUNET_malloc (sizeof (struct Peer));
434   ret->pid = *peer;
435   ret->is_friend = is_friend;
436   if (hello != NULL)
437   {
438     ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello));
439     memcpy (ret->hello, hello, GNUNET_HELLO_size (hello));
440   }
441   GNUNET_break (GNUNET_OK ==
442                 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
443                                                    ret,
444                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
445   return ret;
446 }
447
448
449 /**
450  * Setup bloom filter for the given peer entry.
451  *
452  * @param peer entry to initialize
453  */
454 static void
455 setup_filter (struct Peer *peer)
456 {
457   /* 2^{-5} chance of not sending a HELLO to a peer is
458    * acceptably small (if the filter is 50% full);
459    * 64 bytes of memory are small compared to the rest
460    * of the data structure and would only really become
461    * "useless" once a HELLO has been passed on to ~100
462    * other peers, which is likely more than enough in
463    * any case; hence 64, 5 as bloomfilter parameters. */
464   peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5);
465   peer->filter_expiration =
466       GNUNET_TIME_relative_to_absolute
467       (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
468   /* never send a peer its own HELLO */
469   GNUNET_CONTAINER_bloomfilter_add (peer->filter, &peer->pid.hashPubKey);
470 }
471
472
473 /**
474  * Function to fill send buffer with HELLO.
475  *
476  * @param cls 'struct Peer' of the target peer
477  * @param size number of bytes available in buf
478  * @param buf where the callee should write the message
479  * @return number of bytes written to buf
480  */
481 static size_t
482 hello_advertising_ready (void *cls, size_t size, void *buf);
483
484
485 /**
486  * Closure for 'find_advertisable_hello'.
487  */
488 struct FindAdvHelloContext
489 {
490
491   /**
492    * Peer we want to advertise to.
493    */
494   struct Peer *peer;
495
496   /**
497    * Where to store the result (peer selected for advertising).
498    */
499   struct Peer *result;
500
501   /**
502    * Maximum HELLO size we can use right now.
503    */
504   size_t max_size;
505
506   struct GNUNET_TIME_Relative next_adv;
507 };
508
509
510 /**
511  * Find a peer that would be reasonable for advertising.
512  *
513  * @param cls closure
514  * @param pid identity of a peer
515  * @param value 'struct Peer*' for the peer we are considering
516  * @return GNUNET_YES (continue iteration)
517  */
518 static int
519 find_advertisable_hello (void *cls, const GNUNET_HashCode * pid, void *value)
520 {
521   struct FindAdvHelloContext *fah = cls;
522   struct Peer *pos = value;
523   struct GNUNET_TIME_Relative rst_time;
524   size_t hs;
525
526   if (pos == fah->peer)
527     return GNUNET_YES;
528   if (pos->hello == NULL)
529     return GNUNET_YES;
530   rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration);
531   if (0 == rst_time.rel_value)
532   {
533     /* time to discard... */
534     GNUNET_CONTAINER_bloomfilter_free (pos->filter);
535     setup_filter (pos);
536   }
537   fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv);
538   hs = GNUNET_HELLO_size (pos->hello);
539   if (hs > fah->max_size)
540     return GNUNET_YES;
541   if (GNUNET_NO ==
542       GNUNET_CONTAINER_bloomfilter_test (pos->filter,
543                                          &fah->peer->pid.hashPubKey))
544     fah->result = pos;
545   return GNUNET_YES;
546 }
547
548
549 /**
550  * Calculate when we would like to send the next HELLO to this
551  * peer and ask for it.
552  *
553  * @param cls for which peer to schedule the HELLO
554  * @param tc task context
555  */
556 static void
557 schedule_next_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
558 {
559   struct Peer *pl = cls;
560   struct FindAdvHelloContext fah;
561   size_t next_want;
562   struct GNUNET_TIME_Relative delay;
563
564   pl->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
565   GNUNET_assert (GNUNET_YES == pl->is_connected);
566   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
567     return;                     /* we're out of here */
568   if (pl->hello_req != NULL)
569     return;                     /* did not finish sending the previous one */
570   /* find applicable HELLOs */
571   fah.peer = pl;
572   fah.result = NULL;
573   fah.max_size = GNUNET_SERVER_MAX_MESSAGE_SIZE - 1;
574   fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
575   GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
576   pl->hello_delay_task =
577       GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl);
578   if (fah.result == NULL)
579     return;
580   next_want = GNUNET_HELLO_size (fah.result->hello);
581   delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed);
582   if (delay.rel_value == 0)
583   {
584     /* now! */
585     pl->hello_req =
586         GNUNET_CORE_notify_transmit_ready (handle, GNUNET_YES, 0,
587                                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
588                                            &pl->pid, next_want,
589                                            &hello_advertising_ready, pl);
590   }
591 }
592
593
594 /**
595  * Cancel existing requests for sending HELLOs to this peer
596  * and recalculate when we should send HELLOs to it based
597  * on our current state (something changed!).
598  *
599  * @param cls closure, 'struct Peer' to skip, or NULL
600  * @param pid identity of a peer
601  * @param value 'struct Peer*' for the peer
602  * @return GNUNET_YES (always)
603  */
604 static int
605 reschedule_hellos (void *cls, const GNUNET_HashCode * pid, void *value)
606 {
607   struct Peer *peer = value;
608   struct Peer *skip = cls;
609
610   if (skip == peer)
611     return GNUNET_YES;
612   if (!peer->is_connected)
613     return GNUNET_YES;
614   if (peer->hello_req != NULL)
615   {
616     GNUNET_CORE_notify_transmit_ready_cancel (peer->hello_req);
617     peer->hello_req = NULL;
618   }
619   if (peer->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
620   {
621     GNUNET_SCHEDULER_cancel (peer->hello_delay_task);
622     peer->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
623   }
624   peer->hello_delay_task =
625       GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer);
626   return GNUNET_YES;
627 }
628
629
630 /**
631  * Method called whenever a peer connects.
632  *
633  * @param cls closure
634  * @param peer peer identity this notification is about
635  * @param atsi performance data
636  * @param atsi_count number of records in 'atsi'
637  */
638 static void
639 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
640                 const struct GNUNET_ATS_Information *atsi,
641                 unsigned int atsi_count)
642 {
643   struct Peer *pos;
644
645 #if DEBUG_TOPOLOGY
646   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
647               "Core told us that we are connecting to `%s'\n",
648               GNUNET_i2s (peer));
649 #endif
650   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
651     return;
652
653   connection_count++;
654   GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
655                          connection_count, GNUNET_NO);
656   pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
657   if (pos == NULL)
658   {
659     pos = make_peer (peer, NULL, GNUNET_NO);
660     GNUNET_break (GNUNET_OK == is_connection_allowed (pos));
661   }
662   else
663   {
664     GNUNET_assert (GNUNET_NO == pos->is_connected);
665     pos->greylisted_until.abs_value = 0;        /* remove greylisting */
666   }
667   pos->is_connected = GNUNET_YES;
668   pos->connect_attempts = 0;    /* re-set back-off factor */
669   if (pos->is_friend)
670   {
671     if ((friend_count == minimum_friend_count - 1) &&
672         (GNUNET_YES != friends_only))
673       whitelist_peers ();
674     friend_count++;
675     GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
676                            friend_count, GNUNET_NO);
677   }
678   reschedule_hellos (NULL, &peer->hashPubKey, pos);
679 }
680
681
682 /**
683  * Try to add more peers to our connection set.
684  *
685  * @param cls closure, not used
686  * @param pid identity of a peer
687  * @param value 'struct Peer*' for the peer
688  * @return GNUNET_YES (continue to iterate)
689  */
690 static int
691 try_add_peers (void *cls, const GNUNET_HashCode * pid, void *value)
692 {
693   struct Peer *pos = value;
694
695   attempt_connect (pos);
696   return GNUNET_YES;
697 }
698
699
700 /**
701  * Last task run during shutdown.  Disconnects us from
702  * the transport and core.
703  *
704  * @param cls unused, NULL
705  * @param tc scheduler context
706  */
707 static void
708 add_peer_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
709 {
710   add_task = GNUNET_SCHEDULER_NO_TASK;
711
712   GNUNET_CONTAINER_multihashmap_iterate (peers, &try_add_peers, NULL);
713 }
714
715 /**
716  * Method called whenever a peer disconnects.
717  *
718  * @param cls closure
719  * @param peer peer identity this notification is about
720  */
721 static void
722 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
723 {
724   struct Peer *pos;
725
726   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
727     return;
728 #if DEBUG_TOPOLOGY
729   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
730               "Core told us that we disconnected from `%s'\n",
731               GNUNET_i2s (peer));
732 #endif
733   pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
734   if (pos == NULL)
735   {
736     GNUNET_break (0);
737     return;
738   }
739   if (pos->is_connected != GNUNET_YES)
740   {
741     GNUNET_break (0);
742     return;
743   }
744   pos->is_connected = GNUNET_NO;
745   connection_count--;
746   if (NULL != pos->hello_req)
747   {
748     GNUNET_CORE_notify_transmit_ready_cancel (pos->hello_req);
749     pos->hello_req = NULL;
750   }
751   if (GNUNET_SCHEDULER_NO_TASK != pos->hello_delay_task)
752   {
753     GNUNET_SCHEDULER_cancel (pos->hello_delay_task);
754     pos->hello_delay_task = GNUNET_SCHEDULER_NO_TASK;
755   }
756   GNUNET_STATISTICS_set (stats, gettext_noop ("# peers connected"),
757                          connection_count, GNUNET_NO);
758   if (pos->is_friend)
759   {
760     friend_count--;
761     GNUNET_STATISTICS_set (stats, gettext_noop ("# friends connected"),
762                            friend_count, GNUNET_NO);
763   }
764   if (((connection_count < target_connection_count) ||
765        (friend_count < minimum_friend_count)) &&
766       (GNUNET_SCHEDULER_NO_TASK == add_task))
767     add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL);
768   if ((friend_count < minimum_friend_count) && (blacklist == NULL))
769     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
770 }
771
772
773 /**
774  * Iterator called on each address.
775  *
776  * @param cls flag that we will set if we see any addresses
777  * @param tname name of the transport
778  * @param expiration when will the given address expire
779  * @param addr the address of the peer
780  * @param addrlen number of bytes in addr
781  * @return GNUNET_SYSERR always, to terminate iteration
782  */
783 static int
784 address_iterator (void *cls, const char *tname,
785                   struct GNUNET_TIME_Absolute expiration, const void *addr,
786                   uint16_t addrlen)
787 {
788   int *flag = cls;
789
790   *flag = GNUNET_YES;
791   return GNUNET_SYSERR;
792 }
793
794
795 /**
796  * We've gotten a HELLO from another peer.  Consider it for
797  * advertising.
798  *
799  * @param hello the HELLO we got
800  */
801 static void
802 consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
803 {
804   int have_address;
805   struct GNUNET_PeerIdentity pid;
806   struct GNUNET_TIME_Absolute dt;
807   struct GNUNET_HELLO_Message *nh;
808   struct Peer *peer;
809   uint16_t size;
810
811   if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
812   {
813     GNUNET_break (0);
814     return;
815   }
816   if (0 == memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
817     return;                     /* that's me! */
818   have_address = GNUNET_NO;
819   GNUNET_HELLO_iterate_addresses (hello, GNUNET_NO, &address_iterator,
820                                   &have_address);
821   if (GNUNET_NO == have_address)
822     return;                     /* no point in advertising this one... */
823   peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
824   if (peer == NULL)
825   {
826     peer = make_peer (&pid, hello, GNUNET_NO);
827   }
828   else if (peer->hello != NULL)
829   {
830     dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ());
831     if (dt.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
832       return;                   /* nothing new here */
833   }
834 #if DEBUG_TOPOLOGY
835   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
836               "Found `%s' from peer `%s' for advertising\n", "HELLO",
837               GNUNET_i2s (&pid));
838 #endif
839   if (peer->hello != NULL)
840   {
841     nh = GNUNET_HELLO_merge (peer->hello, hello);
842     GNUNET_free (peer->hello);
843     peer->hello = nh;
844   }
845   else
846   {
847     size = GNUNET_HELLO_size (hello);
848     peer->hello = GNUNET_malloc (size);
849     memcpy (peer->hello, hello, size);
850   }
851   if (peer->filter != NULL)
852     GNUNET_CONTAINER_bloomfilter_free (peer->filter);
853   setup_filter (peer);
854   /* since we have a new HELLO to pick from, re-schedule all
855    * HELLO requests that are not bound by the HELLO send rate! */
856   GNUNET_CONTAINER_multihashmap_iterate (peers, &reschedule_hellos, peer);
857 }
858
859
860 /**
861  * PEERINFO calls this function to let us know about a possible peer
862  * that we might want to connect to.
863  *
864  * @param cls closure (not used)
865  * @param peer potential peer to connect to
866  * @param hello HELLO for this peer (or NULL)
867  * @param err_msg NULL if successful, otherwise contains error message
868  */
869 static void
870 process_peer (void *cls, const struct GNUNET_PeerIdentity *peer,
871               const struct GNUNET_HELLO_Message *hello, const char *err_msg)
872 {
873   struct Peer *pos;
874
875   if (err_msg != NULL)
876   {
877     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
878                 _("Error in communication with PEERINFO service: %s\n"),
879                 err_msg);
880     GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
881     peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
882     return;
883   }
884   GNUNET_assert (peer != NULL);
885   if (0 == memcmp (&my_identity, peer, sizeof (struct GNUNET_PeerIdentity)))
886     return;                     /* that's me! */
887   if (hello == NULL)
888   {
889     /* free existing HELLO, if any */
890     pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
891     if (NULL != pos)
892     {
893       GNUNET_free_non_null (pos->hello);
894       pos->hello = NULL;
895       if (pos->filter != NULL)
896       {
897         GNUNET_CONTAINER_bloomfilter_free (pos->filter);
898         pos->filter = NULL;
899       }
900       if ((!pos->is_connected) && (!pos->is_friend) &&
901           (0 ==
902            GNUNET_TIME_absolute_get_remaining (pos->
903                                                greylisted_until).rel_value))
904         free_peer (NULL, &pos->pid.hashPubKey, pos);
905     }
906     return;
907   }
908   consider_for_advertising (hello);
909   pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
910   if (pos == NULL)
911     pos = make_peer (peer, hello, GNUNET_NO);
912   GNUNET_assert (NULL != pos);
913   if (GNUNET_YES == pos->is_connected)
914   {
915 #if DEBUG_TOPOLOGY
916     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already connected to peer `%s'\n",
917                 GNUNET_i2s (peer));
918 #endif
919     return;
920   }
921   if (GNUNET_TIME_absolute_get_remaining (pos->greylisted_until).rel_value > 0)
922   {
923 #if DEBUG_TOPOLOGY
924     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Already tried peer `%s' recently\n",
925                 GNUNET_i2s (peer));
926 #endif
927     return;                     /* peer still greylisted */
928   }
929 #if DEBUG_TOPOLOGY
930   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Considering connecting to peer `%s'\n",
931               GNUNET_i2s (peer));
932 #endif
933   attempt_connect (pos);
934 }
935
936
937 /**
938  * Function called after GNUNET_CORE_connect has succeeded
939  * (or failed for good).
940  *
941  * @param cls closure
942  * @param server handle to the server, NULL if we failed
943  * @param my_id ID of this peer, NULL if we failed
944  */
945 static void
946 core_init (void *cls, struct GNUNET_CORE_Handle *server,
947            const struct GNUNET_PeerIdentity *my_id)
948 {
949   if (server == NULL)
950   {
951     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
952                 _
953                 ("Failed to connect to core service, can not manage topology!\n"));
954     GNUNET_SCHEDULER_shutdown ();
955     return;
956   }
957   handle = server;
958   my_identity = *my_id;
959 #if DEBUG_TOPOLOGY
960   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
961 #endif
962   peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
963 }
964
965
966 /**
967  * Read the friends file.
968  */
969 static void
970 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
971 {
972   char *fn;
973   char *data;
974   size_t pos;
975   struct GNUNET_PeerIdentity pid;
976   struct stat frstat;
977   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
978   unsigned int entries_found;
979   struct Peer *fl;
980
981   if (GNUNET_OK !=
982       GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
983   {
984     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
985                 _("Option `%s' in section `%s' not specified!\n"), "FRIENDS",
986                 "TOPOLOGY");
987     return;
988   }
989   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
990     GNUNET_DISK_fn_write (fn, NULL, 0,
991                           GNUNET_DISK_PERM_USER_READ |
992                           GNUNET_DISK_PERM_USER_WRITE);
993   if (0 != STAT (fn, &frstat))
994   {
995     if ((friends_only) || (minimum_friend_count > 0))
996       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
997                   _("Could not read friends list `%s'\n"), fn);
998     GNUNET_free (fn);
999     return;
1000   }
1001   if (frstat.st_size == 0)
1002   {
1003     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1004                 fn);
1005     GNUNET_free (fn);
1006     return;
1007   }
1008   data = GNUNET_malloc_large (frstat.st_size);
1009   if (data == NULL)
1010   {
1011     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1012                 _("Failed to read friends list from `%s': out of memory\n"),
1013                 fn);
1014     GNUNET_free (fn);
1015     return;
1016   }
1017   if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1018   {
1019     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1020                 _("Failed to read friends list from `%s'\n"), fn);
1021     GNUNET_free (fn);
1022     GNUNET_free (data);
1023     return;
1024   }
1025   entries_found = 0;
1026   pos = 0;
1027   while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1028     pos++;
1029   while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1030          (pos <=
1031           frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1032   {
1033     memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1034     if (!isspace
1035         ((unsigned char)
1036          enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1037     {
1038       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1039                   _
1040                   ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1041                   (unsigned long long) pos);
1042       pos++;
1043       while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1044         pos++;
1045       continue;
1046     }
1047     enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1048     if (GNUNET_OK !=
1049         GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1050     {
1051       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1052                   _
1053                   ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1054                   (unsigned long long) pos, &enc);
1055     }
1056     else
1057     {
1058       if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1059       {
1060         entries_found++;
1061         fl = make_peer (&pid, NULL, GNUNET_YES);
1062         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1063                     _("Found friend `%s' in configuration\n"),
1064                     GNUNET_i2s (&fl->pid));
1065       }
1066       else
1067       {
1068         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1069                     _("Found myself `%s' in friend list (useless, ignored)\n"),
1070                     GNUNET_i2s (&pid));
1071       }
1072     }
1073     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1074     while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1075       pos++;
1076   }
1077   GNUNET_free (data);
1078   GNUNET_free (fn);
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                 _
1085                 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1086   }
1087   if ((minimum_friend_count > target_connection_count) &&
1088       (friends_only == GNUNET_NO))
1089   {
1090     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1091                 _
1092                 ("More friendly connections required than target total number of connections.\n"));
1093   }
1094 }
1095
1096
1097 /**
1098  * This function is called whenever an encrypted HELLO message is
1099  * received.
1100  *
1101  * @param cls closure
1102  * @param other the other peer involved (sender or receiver, NULL
1103  *        for loopback messages where we are both sender and receiver)
1104  * @param message the actual HELLO message
1105  * @param atsi performance data
1106  * @param atsi_count number of records in 'atsi'
1107  * @return GNUNET_OK to keep the connection open,
1108  *         GNUNET_SYSERR to close it (signal serious error)
1109  */
1110 static int
1111 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1112                         const struct GNUNET_MessageHeader *message,
1113                         const struct GNUNET_ATS_Information *atsi,
1114                         unsigned int atsi_count)
1115 {
1116   struct Peer *peer;
1117   struct GNUNET_PeerIdentity pid;
1118
1119 #if DEBUG_TOPOLOGY
1120   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1121               "HELLO", GNUNET_i2s (other));
1122 #endif
1123   if (GNUNET_OK !=
1124       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1125   {
1126     GNUNET_break_op (0);
1127     return GNUNET_SYSERR;
1128   }
1129   GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1130                             1, GNUNET_NO);
1131   peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1132   if (peer == NULL)
1133   {
1134     if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1135       return GNUNET_OK;
1136   }
1137   else
1138   {
1139     if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1140       return GNUNET_OK;
1141     if ((GNUNET_YES != peer->is_friend) &&
1142         (friend_count < minimum_friend_count))
1143       return GNUNET_OK;
1144   }
1145   if (transport != NULL)
1146     GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1147   return GNUNET_OK;
1148 }
1149
1150
1151 /**
1152  * Function to fill send buffer with HELLO.
1153  *
1154  * @param cls 'struct Peer' of the target peer
1155  * @param size number of bytes available in buf
1156  * @param buf where the callee should write the message
1157  * @return number of bytes written to buf
1158  */
1159 static size_t
1160 hello_advertising_ready (void *cls, size_t size, void *buf)
1161 {
1162   struct Peer *pl = cls;
1163   struct FindAdvHelloContext fah;
1164   size_t want;
1165
1166   pl->hello_req = NULL;
1167   GNUNET_assert (GNUNET_YES == pl->is_connected);
1168   /* find applicable HELLOs */
1169   fah.peer = pl;
1170   fah.result = NULL;
1171   fah.max_size = size;
1172   fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1173   GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1174   want = 0;
1175   if (fah.result != NULL)
1176   {
1177     want = GNUNET_HELLO_size (fah.result->hello);
1178     GNUNET_assert (want <= size);
1179     memcpy (buf, fah.result->hello, want);
1180     GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1181 #if DEBUG_TOPOLOGY
1182     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1183                 (unsigned int) want);
1184 #endif
1185     GNUNET_STATISTICS_update (stats,
1186                               gettext_noop ("# HELLO messages gossipped"), 1,
1187                               GNUNET_NO);
1188   }
1189
1190   if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1191     GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1192   pl->next_hello_allowed =
1193       GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1194   pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1195   return want;
1196 }
1197
1198
1199 /**
1200  * Last task run during shutdown.  Disconnects us from
1201  * the transport and core.
1202  *
1203  * @param cls unused, NULL
1204  * @param tc scheduler context
1205  */
1206 static void
1207 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1208 {
1209   if (NULL != peerinfo_notify)
1210   {
1211     GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1212     peerinfo_notify = NULL;
1213   }
1214   if (GNUNET_SCHEDULER_NO_TASK != add_task)
1215   {
1216     GNUNET_SCHEDULER_cancel (add_task);
1217     add_task = GNUNET_SCHEDULER_NO_TASK;
1218   }
1219   GNUNET_TRANSPORT_disconnect (transport);
1220   transport = NULL;
1221   GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1222   GNUNET_CONTAINER_multihashmap_destroy (peers);
1223   if (handle != NULL)
1224   {
1225     GNUNET_CORE_disconnect (handle);
1226     handle = NULL;
1227   }
1228   whitelist_peers ();
1229   if (stats != NULL)
1230   {
1231     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1232     stats = NULL;
1233   }
1234 }
1235
1236
1237 /**
1238  * Main function that will be run.
1239  *
1240  * @param cls closure
1241  * @param args remaining command-line arguments
1242  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1243  * @param c configuration
1244  */
1245 static void
1246 run (void *cls, char *const *args, const char *cfgfile,
1247      const struct GNUNET_CONFIGURATION_Handle *c)
1248 {
1249   static struct GNUNET_CORE_MessageHandler handlers[] = {
1250     {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1251     {NULL, 0, 0}
1252   };
1253   unsigned long long opt;
1254
1255   cfg = c;
1256   stats = GNUNET_STATISTICS_create ("topology", cfg);
1257   autoconnect =
1258       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "AUTOCONNECT");
1259   friends_only =
1260       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1261   if (GNUNET_OK !=
1262       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1263                                              &opt))
1264     opt = 0;
1265   minimum_friend_count = (unsigned int) opt;
1266   if (GNUNET_OK !=
1267       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1268                                              "TARGET-CONNECTION-COUNT", &opt))
1269     opt = 16;
1270   target_connection_count = (unsigned int) opt;
1271   peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1272
1273   if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1274     read_friends_file (cfg);
1275 #if DEBUG_TOPOLOGY
1276   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277               "Topology would like %u connections with at least %u friends (%s)\n",
1278               target_connection_count, minimum_friend_count,
1279               autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1280 #endif
1281   if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1282     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1283   transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1284   handle =
1285       GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_notify,
1286                            &disconnect_notify, NULL, GNUNET_NO, NULL, GNUNET_NO,
1287                            handlers);
1288   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1289                                 NULL);
1290   if (NULL == transport)
1291   {
1292     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1293                 _("Failed to connect to `%s' service.\n"), "transport");
1294     GNUNET_SCHEDULER_shutdown ();
1295     return;
1296   }
1297   if (NULL == handle)
1298   {
1299     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1300                 _("Failed to connect to `%s' service.\n"), "core");
1301     GNUNET_SCHEDULER_shutdown ();
1302     return;
1303   }
1304 }
1305
1306
1307 /**
1308  * The main function for the topology daemon.
1309  *
1310  * @param argc number of arguments from the command line
1311  * @param argv command line arguments
1312  * @return 0 ok, 1 on error
1313  */
1314 int
1315 main (int argc, char *const *argv)
1316 {
1317   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1318     GNUNET_GETOPT_OPTION_END
1319   };
1320   int ret;
1321
1322   ret =
1323       (GNUNET_OK ==
1324        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1325                            _
1326                            ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1327                            options, &run, NULL)) ? 0 : 1;
1328   return ret;
1329 }
1330
1331 /* end of gnunet-daemon-topology.c */