-simplify
[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   struct stat frstat;
974   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
975   unsigned int entries_found;
976   struct Peer *fl;
977
978   if (GNUNET_OK !=
979       GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn))
980   {
981     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
982                 _("Option `%s' in section `%s' not specified!\n"), "FRIENDS",
983                 "TOPOLOGY");
984     return;
985   }
986   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
987     GNUNET_DISK_fn_write (fn, NULL, 0,
988                           GNUNET_DISK_PERM_USER_READ |
989                           GNUNET_DISK_PERM_USER_WRITE);
990   if (0 != STAT (fn, &frstat))
991   {
992     if ((friends_only) || (minimum_friend_count > 0))
993       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994                   _("Could not read friends list `%s'\n"), fn);
995     GNUNET_free (fn);
996     return;
997   }
998   if (frstat.st_size == 0)
999   {
1000     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Friends file `%s' is empty.\n"),
1001                 fn);
1002     GNUNET_free (fn);
1003     return;
1004   }
1005   data = GNUNET_malloc_large (frstat.st_size);
1006   if (data == NULL)
1007   {
1008     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1009                 _("Failed to read friends list from `%s': out of memory\n"),
1010                 fn);
1011     GNUNET_free (fn);
1012     return;
1013   }
1014   if (frstat.st_size != GNUNET_DISK_fn_read (fn, data, frstat.st_size))
1015   {
1016     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1017                 _("Failed to read friends list from `%s'\n"), fn);
1018     GNUNET_free (fn);
1019     GNUNET_free (data);
1020     return;
1021   }
1022   entries_found = 0;
1023   pos = 0;
1024   while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1025     pos++;
1026   while ((frstat.st_size >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
1027          (pos <=
1028           frstat.st_size - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
1029   {
1030     memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
1031     if (!isspace
1032         ((unsigned char)
1033          enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
1034     {
1035       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1036                   _
1037                   ("Syntax error in topology specification at offset %llu, skipping bytes.\n"),
1038                   (unsigned long long) pos);
1039       pos++;
1040       while ((pos < frstat.st_size) && (!isspace ((unsigned char) data[pos])))
1041         pos++;
1042       continue;
1043     }
1044     enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
1045     if (GNUNET_OK !=
1046         GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
1047     {
1048       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1049                   _
1050                   ("Syntax error in topology specification at offset %llu, skipping bytes `%s'.\n"),
1051                   (unsigned long long) pos, &enc);
1052     }
1053     else
1054     {
1055       if (0 != memcmp (&pid, &my_identity, sizeof (struct GNUNET_PeerIdentity)))
1056       {
1057         entries_found++;
1058         fl = make_peer (&pid, NULL, GNUNET_YES);
1059         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1060                     _("Found friend `%s' in configuration\n"),
1061                     GNUNET_i2s (&fl->pid));
1062       }
1063       else
1064       {
1065         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1066                     _("Found myself `%s' in friend list (useless, ignored)\n"),
1067                     GNUNET_i2s (&pid));
1068       }
1069     }
1070     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
1071     while ((pos < frstat.st_size) && isspace ((unsigned char) data[pos]))
1072       pos++;
1073   }
1074   GNUNET_free (data);
1075   GNUNET_free (fn);
1076   GNUNET_STATISTICS_update (stats, gettext_noop ("# friends in configuration"),
1077                             entries_found, GNUNET_NO);
1078   if ((minimum_friend_count > entries_found) && (friends_only == GNUNET_NO))
1079   {
1080     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1081                 _
1082                 ("Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
1083   }
1084   if ((minimum_friend_count > target_connection_count) &&
1085       (friends_only == GNUNET_NO))
1086   {
1087     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1088                 _
1089                 ("More friendly connections required than target total number of connections.\n"));
1090   }
1091 }
1092
1093
1094 /**
1095  * This function is called whenever an encrypted HELLO message is
1096  * received.
1097  *
1098  * @param cls closure
1099  * @param other the other peer involved (sender or receiver, NULL
1100  *        for loopback messages where we are both sender and receiver)
1101  * @param message the actual HELLO message
1102  * @param atsi performance data
1103  * @param atsi_count number of records in 'atsi'
1104  * @return GNUNET_OK to keep the connection open,
1105  *         GNUNET_SYSERR to close it (signal serious error)
1106  */
1107 static int
1108 handle_encrypted_hello (void *cls, const struct GNUNET_PeerIdentity *other,
1109                         const struct GNUNET_MessageHeader *message,
1110                         const struct GNUNET_ATS_Information *atsi,
1111                         unsigned int atsi_count)
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, GNUNET_NO, NULL, GNUNET_NO,
1284                            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 */