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