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