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