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