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