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