tell transport to connect, not core
[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  * @param publicKey public key of this peer, NULL if we failed
942  */
943 static void
944 core_init (void *cls, struct GNUNET_CORE_Handle *server,
945            const struct GNUNET_PeerIdentity *my_id,
946            const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
947 {
948   if (server == NULL)
949   {
950     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
951                 _
952                 ("Failed to connect to core service, can not manage topology!\n"));
953     GNUNET_SCHEDULER_shutdown ();
954     return;
955   }
956   handle = server;
957   my_identity = *my_id;
958 #if DEBUG_TOPOLOGY
959   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id));
960 #endif
961   peerinfo_notify = GNUNET_PEERINFO_notify (cfg, &process_peer, NULL);
962 }
963
964
965 /**
966  * Read the friends file.
967  */
968 static void
969 read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
970 {
971   char *fn;
972   char *data;
973   size_t pos;
974   struct GNUNET_PeerIdentity pid;
975   struct stat frstat;
976   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
977   unsigned int entries_found;
978   struct Peer *fl;
979
980   if (GNUNET_OK !=
981       GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
982   {
983     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
984                 _("Option `%s' in section `%s' not specified!\n"), "FRIENDS",
985                 "TOPOLOGY");
986     return;
987   }
988   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
989     GNUNET_DISK_fn_write (fn, NULL, 0,
990                           GNUNET_DISK_PERM_USER_READ |
991                           GNUNET_DISK_PERM_USER_WRITE);
992   if (0 != STAT (fn, &frstat))
993   {
994     if ((friends_only) || (minimum_friend_count > 0))
995       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
996                   _("Could not read friends list `%s'\n"), fn);
997     GNUNET_free (fn);
998     return;
999   }
1000   if (frstat.st_size == 0)
1001   {
1002     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1003                 fn);
1004     GNUNET_free (fn);
1005     return;
1006   }
1007   data = GNUNET_malloc_large (frstat.st_size);
1008   if (data == NULL)
1009   {
1010     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1011                 _("Failed to read friends list from `%s': out of memory\n"),
1012                 fn);
1013     GNUNET_free (fn);
1014     return;
1015   }
1016   if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1017   {
1018     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1019                 _("Failed to read friends list from `%s'\n"), fn);
1020     GNUNET_free (fn);
1021     GNUNET_free (data);
1022     return;
1023   }
1024   entries_found = 0;
1025   pos = 0;
1026   while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1027     pos++;
1028   while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1029          (pos <=
1030           frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1031   {
1032     memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1033     if (!isspace
1034         ((unsigned char)
1035          enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1036     {
1037       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038                   _
1039                   ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1040                   (unsigned long long) pos);
1041       pos++;
1042       while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1043         pos++;
1044       continue;
1045     }
1046     enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1047     if (GNUNET_OK !=
1048         GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1049     {
1050       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1051                   _
1052                   ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1053                   (unsigned long long) pos, &enc);
1054     }
1055     else
1056     {
1057       if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1058       {
1059         entries_found++;
1060         fl = make_peer (&pid, NULL, GNUNET_YES);
1061         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1062                     _("Found friend `%s' in configuration\n"),
1063                     GNUNET_i2s (&fl->pid));
1064       }
1065       else
1066       {
1067         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1068                     _("Found myself `%s' in friend list (useless, ignored)\n"),
1069                     GNUNET_i2s (&pid));
1070       }
1071     }
1072     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1073     while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1074       pos++;
1075   }
1076   GNUNET_free (data);
1077   GNUNET_free (fn);
1078   GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1079                             entries_found, GNUNET_NO);
1080   if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1081   {
1082     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1083                 _
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                 _
1091                 ("More friendly connections required than target total number of connections.\n"));
1092   }
1093 }
1094
1095
1096 /**
1097  * This function is called whenever an encrypted HELLO message is
1098  * received.
1099  *
1100  * @param cls closure
1101  * @param other the other peer involved (sender or receiver, NULL
1102  *        for loopback messages where we are both sender and receiver)
1103  * @param message the actual HELLO message
1104  * @param atsi performance data
1105  * @return GNUNET_OK to keep the connection open,
1106  *         GNUNET_SYSERR to close it (signal serious error)
1107  */
1108 static int
1109 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1110                         const struct GNUNET_MessageHeader *message,
1111                         const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1112 {
1113   struct Peer *peer;
1114   struct GNUNET_PeerIdentity pid;
1115
1116 #if DEBUG_TOPOLOGY
1117   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received encrypted `%s' from peer `%s'",
1118               "HELLO", GNUNET_i2s (other));
1119 #endif
1120   if (GNUNET_OK !=
1121       GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) message, &pid))
1122   {
1123     GNUNET_break_op (0);
1124     return GNUNET_SYSERR;
1125   }
1126   GNUNET_STATISTICS_update (stats, gettext_noop ("# HELLO messages received"),
1127                             1, GNUNET_NO);
1128   peer = GNUNET_CONTAINER_multihashmap_get (peers, &pid.hashPubKey);
1129   if (peer == NULL)
1130   {
1131     if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
1132       return GNUNET_OK;
1133   }
1134   else
1135   {
1136     if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
1137       return GNUNET_OK;
1138     if ((GNUNET_YES != peer->is_friend) &&
1139         (friend_count < minimum_friend_count))
1140       return GNUNET_OK;
1141   }
1142   if (transport != NULL)
1143     GNUNET_TRANSPORT_offer_hello (transport, message, NULL, NULL);
1144   return GNUNET_OK;
1145 }
1146
1147
1148 /**
1149  * Function to fill send buffer with HELLO.
1150  *
1151  * @param cls 'struct Peer' of the target peer
1152  * @param size number of bytes available in buf
1153  * @param buf where the callee should write the message
1154  * @return number of bytes written to buf
1155  */
1156 static size_t
1157 hello_advertising_ready (void *cls, size_t size, void *buf)
1158 {
1159   struct Peer *pl = cls;
1160   struct FindAdvHelloContext fah;
1161   size_t want;
1162
1163   pl->hello_req = NULL;
1164   GNUNET_assert (GNUNET_YES == pl->is_connected);
1165   /* find applicable HELLOs */
1166   fah.peer = pl;
1167   fah.result = NULL;
1168   fah.max_size = size;
1169   fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
1170   GNUNET_CONTAINER_multihashmap_iterate (peers, &find_advertisable_hello, &fah);
1171   want = 0;
1172   if (fah.result != NULL)
1173   {
1174     want = GNUNET_HELLO_size (fah.result->hello);
1175     GNUNET_assert (want <= size);
1176     memcpy (buf, fah.result->hello, want);
1177     GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &pl->pid.hashPubKey);
1178 #if DEBUG_TOPOLOGY
1179     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' with %u bytes", "HELLO",
1180                 (unsigned int) want);
1181 #endif
1182     GNUNET_STATISTICS_update (stats,
1183                               gettext_noop ("# HELLO messages gossipped"), 1,
1184                               GNUNET_NO);
1185   }
1186
1187   if (pl->hello_delay_task != GNUNET_SCHEDULER_NO_TASK)
1188     GNUNET_SCHEDULER_cancel (pl->hello_delay_task);
1189   pl->next_hello_allowed =
1190       GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY);
1191   pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl);
1192   return want;
1193 }
1194
1195
1196 /**
1197  * Last task run during shutdown.  Disconnects us from
1198  * the transport and core.
1199  *
1200  * @param cls unused, NULL
1201  * @param tc scheduler context
1202  */
1203 static void
1204 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1205 {
1206   if (NULL != peerinfo_notify)
1207   {
1208     GNUNET_PEERINFO_notify_cancel (peerinfo_notify);
1209     peerinfo_notify = NULL;
1210   }
1211   if (GNUNET_SCHEDULER_NO_TASK != add_task)
1212   {
1213     GNUNET_SCHEDULER_cancel (add_task);
1214     add_task = GNUNET_SCHEDULER_NO_TASK;
1215   }
1216   GNUNET_TRANSPORT_disconnect (transport);
1217   transport = NULL;
1218   GNUNET_CONTAINER_multihashmap_iterate (peers, &free_peer, NULL);
1219   GNUNET_CONTAINER_multihashmap_destroy (peers);
1220   if (handle != NULL)
1221   {
1222     GNUNET_CORE_disconnect (handle);
1223     handle = NULL;
1224   }
1225   whitelist_peers ();
1226   if (stats != NULL)
1227   {
1228     GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
1229     stats = NULL;
1230   }
1231 }
1232
1233
1234 /**
1235  * Main function that will be run.
1236  *
1237  * @param cls closure
1238  * @param args remaining command-line arguments
1239  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1240  * @param c configuration
1241  */
1242 static void
1243 run (void *cls, char *const *args, const char *cfgfile,
1244      const struct GNUNET_CONFIGURATION_Handle *c)
1245 {
1246   static struct GNUNET_CORE_MessageHandler handlers[] = {
1247     {&handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
1248     {NULL, 0, 0}
1249   };
1250   unsigned long long opt;
1251
1252   cfg = c;
1253   stats = GNUNET_STATISTICS_create ("topology", cfg);
1254   autoconnect =
1255       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "AUTOCONNECT");
1256   friends_only =
1257       GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY");
1258   if (GNUNET_OK !=
1259       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY", "MINIMUM-FRIENDS",
1260                                              &opt))
1261     opt = 0;
1262   minimum_friend_count = (unsigned int) opt;
1263   if (GNUNET_OK !=
1264       GNUNET_CONFIGURATION_get_value_number (cfg, "TOPOLOGY",
1265                                              "TARGET-CONNECTION-COUNT", &opt))
1266     opt = 16;
1267   target_connection_count = (unsigned int) opt;
1268   peers = GNUNET_CONTAINER_multihashmap_create (target_connection_count * 2);
1269
1270   if ((friends_only == GNUNET_YES) || (minimum_friend_count > 0))
1271     read_friends_file (cfg);
1272 #if DEBUG_TOPOLOGY
1273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1274               "Topology would like %u connections with at least %u friends (%s)\n",
1275               target_connection_count, minimum_friend_count,
1276               autoconnect ? "autoconnect enabled" : "autoconnect disabled");
1277 #endif
1278   if ((friend_count < minimum_friend_count) && (blacklist == NULL))
1279     blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL);
1280   transport = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL, NULL, NULL);
1281   handle =
1282       GNUNET_CORE_connect (cfg, 1, NULL, &core_init, &connect_notify,
1283                            &disconnect_notify, NULL, NULL, GNUNET_NO, NULL,
1284                            GNUNET_NO, handlers);
1285   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &cleaning_task,
1286                                 NULL);
1287   if (NULL == transport)
1288   {
1289     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1290                 _("Failed to connect to `%s' service.\n"), "transport");
1291     GNUNET_SCHEDULER_shutdown ();
1292     return;
1293   }
1294   if (NULL == handle)
1295   {
1296     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1297                 _("Failed to connect to `%s' service.\n"), "core");
1298     GNUNET_SCHEDULER_shutdown ();
1299     return;
1300   }
1301 }
1302
1303
1304 /**
1305  * The main function for the topology daemon.
1306  *
1307  * @param argc number of arguments from the command line
1308  * @param argv command line arguments
1309  * @return 0 ok, 1 on error
1310  */
1311 int
1312 main (int argc, char *const *argv)
1313 {
1314   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1315     GNUNET_GETOPT_OPTION_END
1316   };
1317   int ret;
1318
1319   ret =
1320       (GNUNET_OK ==
1321        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-topology",
1322                            _
1323                            ("GNUnet topology control (maintaining P2P mesh and F2F constraints)"),
1324                            options, &run, NULL)) ? 0 : 1;
1325   return ret;
1326 }
1327
1328 /* end of gnunet-daemon-topology.c */