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"
46 * Set if we are allowed to advertise our hostlist to others.
48 static int advertising;
51 * Set if we are allowed to learn about peers by accessing
54 static int bootstrapping;
57 * Set if the user allows us to learn about new hostlists
63 * Set if the user wants us to run a hostlist server.
65 static int provide_hostlist;
70 static struct GNUNET_STATISTICS_Handle *stats;
73 * Handle to the core service (NULL until we've connected to it).
75 struct GNUNET_CORE_Handle *core;
78 * gnunet-daemon-hostlist command line options.
80 static struct GNUNET_GETOPT_CommandLineOption options[] = {
81 { 'a', "advertise", NULL,
82 gettext_noop ("advertise our hostlist to other peers"),
83 GNUNET_NO, &GNUNET_GETOPT_set_one, &advertising },
84 { 'b', "bootstrap", NULL,
85 gettext_noop ("bootstrap using hostlists (it is highly recommended that you always use this option)"),
86 GNUNET_NO, &GNUNET_GETOPT_set_one, &bootstrapping },
87 { 'e', "enable-learning", NULL,
88 gettext_noop ("enable learning about hostlist servers from other peers"),
89 GNUNET_NO, &GNUNET_GETOPT_set_one, &learning},
90 { 'p', "provide-hostlist", NULL,
91 gettext_noop ("provide a hostlist server"),
92 GNUNET_NO, &GNUNET_GETOPT_set_one, &provide_hostlist},
93 GNUNET_GETOPT_OPTION_END
99 struct GNUNET_CORE_Handle * server,
100 const struct GNUNET_PeerIdentity *
103 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
106 if (advertising && (NULL != server))
108 /* FIXME: provide "server" to 'hostlist' module */
113 * Core handler for p2p hostlist advertisements
115 static int handle_hostlist_advertisement (void *cls,
116 const struct GNUNET_PeerIdentity * peer,
117 const struct GNUNET_MessageHeader * message,
118 struct GNUNET_TIME_Relative latency,
121 #if DEBUG_HOSTLIST_LEARNING
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 _("Recieved hostlist advertisement\n"));
130 * Last task run during shutdown. Disconnects us from
131 * the other services.
134 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137 "Hostlist daemon is shutting down\n");
140 GNUNET_HOSTLIST_client_stop ();
142 if (provide_hostlist)
144 GNUNET_HOSTLIST_server_stop ();
148 GNUNET_CORE_disconnect (core);
153 GNUNET_STATISTICS_destroy (stats,
160 * List of handlers for the messages understood by this
163 static struct GNUNET_CORE_MessageHandler handlers[] = {
164 { &handle_hostlist_advertisement, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
169 * Main function that will be run.
172 * @param sched the scheduler to use
173 * @param args remaining command-line arguments
174 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
175 * @param cfg configuration
179 struct GNUNET_SCHEDULER_Handle * sched,
182 const struct GNUNET_CONFIGURATION_Handle * cfg)
184 GNUNET_CORE_ConnectEventHandler ch = NULL;
185 GNUNET_CORE_DisconnectEventHandler dh = NULL;
189 struct GNUNET_CORE_MessageHandler null_handler[] = {
193 struct GNUNET_CORE_MessageHandler *used_handler = null_handler;
195 if ( (! bootstrapping) &&
197 (! provide_hostlist) )
199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
200 _("None of the functions for the hostlist daemon were enabled. I have no reason to run!\n"));
203 stats = GNUNET_STATISTICS_create (sched, "hostlist", cfg);
206 GNUNET_HOSTLIST_client_start (cfg, sched, stats,
209 if (provide_hostlist)
211 GNUNET_HOSTLIST_server_start (cfg, sched, stats);
215 used_handler = handlers;
218 core = GNUNET_CORE_connect (sched, cfg,
219 GNUNET_TIME_UNIT_FOREVER_REL,
227 GNUNET_SCHEDULER_add_delayed (sched,
228 GNUNET_TIME_UNIT_FOREVER_REL,
229 &cleaning_task, NULL);
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 _("Failed to connect to `%s' service.\n"),
235 GNUNET_SCHEDULER_shutdown (sched);
242 * The main function for the hostlist daemon.
244 * @param argc number of arguments from the command line
245 * @param argv command line arguments
246 * @return 0 ok, 1 on error
249 main (int argc, char *const *argv)
252 GNUNET_log_setup ("hostlist","DEBUG",NULL);
255 GNUNET_PROGRAM_run (argc,
258 _("GNUnet hostlist server and client"),
260 &run, NULL)) ? 0 : 1;
265 /* end of gnunet-daemon-hostlist.c */