2 This file is part of GNUnet.
3 (C) 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file hostlist/gnunet-daemon-hostlist.c
23 * @brief code for bootstrapping via hostlist servers
24 * @author Christian Grothoff
27 * - implement -a and -e switches (send P2P messages about our hostlist URL,
28 * receive such messages and automatically update our hostlist URL config
34 #include "hostlist-client.h"
35 #include "hostlist-server.h"
36 #include "gnunet_core_service.h"
37 #include "gnunet_getopt_lib.h"
38 #include "gnunet_protocols.h"
39 #include "gnunet_program_lib.h"
40 #include "gnunet_statistics_service.h"
41 #include "gnunet_strings_lib.h"
42 #include "gnunet_time_lib.h"
43 #include "gnunet_util_lib.h"
47 * Set if we are allowed to advertise our hostlist to others.
49 static int advertising;
52 * Set if we are allowed to learn about peers by accessing
55 static int bootstrapping;
58 * Set if the user allows us to learn about new hostlists
64 * Set if the user wants us to run a hostlist server.
66 static int provide_hostlist;
71 static struct GNUNET_STATISTICS_Handle *stats;
74 * Handle to the core service (NULL until we've connected to it).
76 struct GNUNET_CORE_Handle *core;
79 * gnunet-daemon-hostlist command line options.
81 static struct GNUNET_GETOPT_CommandLineOption options[] = {
82 { 'a', "advertise", NULL,
83 gettext_noop ("advertise our hostlist to other peers"),
84 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
85 { 'b', "bootstrap", NULL,
86 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
87 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
88 { 'e', "enable-learning", NULL,
89 gettext_noop ("enable learning about hostlist servers from other peers"),
90 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
91 { 'p', "provide-hostlist", NULL,
92 gettext_noop ("provide a hostlist server"),
93 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
94 GNUNET_GETOPT_OPTION_END
100 struct GNUNET_CORE_Handle * server,
101 const struct GNUNET_PeerIdentity *
104 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
107 if (advertising && (NULL != server))
109 /* FIXME: provide "server" to 'hostlist' module */
115 * Last task run during shutdown. Disconnects us from
116 * the other services.
119 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
122 "Hostlist daemon is shutting down.\n");
125 GNUNET_HOSTLIST_client_stop ();
127 if (provide_hostlist)
129 GNUNET_HOSTLIST_server_stop ();
133 GNUNET_CORE_disconnect (core);
138 GNUNET_STATISTICS_destroy (stats,
146 * Main function that will be run.
149 * @param sched the scheduler to use
150 * @param args remaining command-line arguments
151 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
152 * @param cfg configuration
156 struct GNUNET_SCHEDULER_Handle * sched,
159 const struct GNUNET_CONFIGURATION_Handle * cfg)
161 GNUNET_CORE_ClientEventHandler ch = NULL;
162 GNUNET_CORE_ClientEventHandler dh = NULL;
163 struct GNUNET_CORE_MessageHandler handlers[] =
168 if ( (! bootstrapping) &&
170 (! provide_hostlist) )
172 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
173 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
176 stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
179 /* FIXME (register handler with core for hostlist ads) */
183 GNUNET_HOSTLIST_client_start (cfg, sched, stats,
186 if (provide_hostlist)
188 GNUNET_HOSTLIST_server_start (cfg, sched, stats);
190 core = GNUNET_CORE_connect (sched, cfg,
191 GNUNET_TIME_UNIT_FOREVER_REL,
198 GNUNET_SCHEDULER_add_delayed (sched,
199 GNUNET_TIME_UNIT_FOREVER_REL,
200 &cleaning_task, NULL);
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
204 _("Failed to connect to `%s' service.\n"),
206 GNUNET_SCHEDULER_shutdown (sched);
213 * The main function for the hostlist daemon.
215 * @param argc number of arguments from the command line
216 * @param argv command line arguments
217 * @return 0 ok, 1 on error
220 main (int argc, char *const *argv)
225 GNUNET_PROGRAM_run (argc,
228 _("GNUnet hostlist server and client"),
230 &run, NULL)) ? 0 : 1;
234 /* end of gnunet-daemon-hostlist.c */