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